network: switch to single find + allocation fun

parent ecbb3790
...@@ -166,35 +166,11 @@ bool lxc_config_value_empty(const char *value) ...@@ -166,35 +166,11 @@ bool lxc_config_value_empty(const char *value)
return true; return true;
} }
struct lxc_netdev *lxc_find_netdev_by_idx(struct lxc_conf *conf,
unsigned int idx)
{
struct lxc_netdev *netdev = NULL;
struct lxc_list *networks = &conf->network;
struct lxc_list *insert = networks;
/* lookup network */
if (lxc_list_empty(networks))
return NULL;
lxc_list_for_each(insert, networks) {
netdev = insert->elem;
if (netdev->idx >= idx)
break;
}
/* network already exists */
if (netdev->idx == idx)
return netdev;
return NULL;
}
/* Takes care of finding the correct netdev struct in the networks list or /* Takes care of finding the correct netdev struct in the networks list or
* allocates a new one if it couldn't be found. * allocates a new one if it couldn't be found.
*/ */
struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf, struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
unsigned int idx) unsigned int idx, bool allocate)
{ {
struct lxc_list *newlist; struct lxc_list *newlist;
struct lxc_netdev *netdev = NULL; struct lxc_netdev *netdev = NULL;
...@@ -202,9 +178,20 @@ struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf, ...@@ -202,9 +178,20 @@ struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
struct lxc_list *insert = networks; struct lxc_list *insert = networks;
/* lookup network */ /* lookup network */
netdev = lxc_find_netdev_by_idx(conf, idx); if (!lxc_list_empty(networks)) {
if (netdev) lxc_list_for_each(insert, networks) {
return netdev; netdev = insert->elem;
if (netdev->idx >= idx)
break;
}
/* network already exists */
if (netdev->idx == idx)
return netdev;
}
if (!allocate)
return NULL;
/* network does not exist */ /* network does not exist */
netdev = malloc(sizeof(*netdev)); netdev = malloc(sizeof(*netdev));
......
...@@ -28,10 +28,8 @@ extern int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, ...@@ -28,10 +28,8 @@ extern int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
unsigned long *hostid, unsigned long *range); unsigned long *hostid, unsigned long *range);
extern bool lxc_config_value_empty(const char *value); extern bool lxc_config_value_empty(const char *value);
extern struct lxc_netdev *lxc_find_netdev_by_idx(struct lxc_conf *conf, extern struct lxc_netdev *
unsigned int idx); lxc_get_netdev_by_idx(struct lxc_conf *conf, unsigned int idx, bool allocate);
extern struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
unsigned int idx);
extern void lxc_log_configured_netdevs(const struct lxc_conf *conf); extern void lxc_log_configured_netdevs(const struct lxc_conf *conf);
extern bool lxc_remove_nic_by_idx(struct lxc_conf *conf, unsigned int idx); extern bool lxc_remove_nic_by_idx(struct lxc_conf *conf, unsigned int idx);
extern void lxc_free_networks(struct lxc_conf *conf); extern void lxc_free_networks(struct lxc_conf *conf);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment