network: preserve backwards compatibility

The new network parser will support specifying multiple networks in the old format where each new non-indexed "lxc.network.type" line starts a new network configuration. This way we don't break users. For now, we just print a deprecation warning. We will KILL this in LXC 3.0. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 8c582655
......@@ -2508,11 +2508,13 @@ static int setup_netdev(struct lxc_netdev *netdev)
return 0;
}
static int setup_network(struct lxc_list *network)
static int setup_network(const struct lxc_conf *conf, struct lxc_list *network)
{
struct lxc_list *iterator;
struct lxc_netdev *netdev;
lxc_log_configured_netdevs(conf);
lxc_list_for_each(iterator, network) {
netdev = iterator->elem;
......@@ -3044,8 +3046,6 @@ int lxc_create_network(struct lxc_handler *handler)
if (!am_root)
return 0;
lxc_log_configured_netdevs(handler->conf);
lxc_list_for_each(iterator, network) {
netdev = iterator->elem;
......@@ -4110,7 +4110,7 @@ int lxc_setup(struct lxc_handler *handler)
}
}
if (setup_network(&lxc_conf->network)) {
if (setup_network(lxc_conf, &lxc_conf->network)) {
ERROR("failed to setup the network for '%s'", name);
return -1;
}
......@@ -4504,7 +4504,7 @@ void lxc_conf_free(struct lxc_conf *conf)
free(conf->unexpanded_config);
free(conf->pty_names);
free(conf->syslog);
lxc_free_networks(conf);
lxc_free_networks(&conf->network);
free(conf->lsm_aa_profile);
free(conf->lsm_se_context);
lxc_seccomp_free(conf);
......
......@@ -124,7 +124,7 @@ union netdev_p {
* @idx : network counter
*/
struct lxc_netdev {
unsigned int idx;
ssize_t idx;
int type;
int flags;
int ifindex;
......
......@@ -166,32 +166,10 @@ bool lxc_config_value_empty(const char *value)
return true;
}
/* Takes care of finding the correct netdev struct in the networks list or
* allocates a new one if it couldn't be found.
*/
struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
unsigned int idx, bool allocate)
struct lxc_netdev *lxc_network_add(struct lxc_list *networks, int idx, bool tail)
{
struct lxc_list *newlist;
struct lxc_netdev *netdev = NULL;
struct lxc_list *networks = &conf->network;
struct lxc_list *insert = networks;
/* lookup network */
if (!lxc_list_empty(networks)) {
lxc_list_for_each(insert, networks) {
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 */
netdev = malloc(sizeof(*netdev));
......@@ -215,14 +193,40 @@ struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
lxc_list_init(newlist);
newlist->elem = netdev;
/* Insert will now point to the correct position to insert the new
* netdev.
*/
lxc_list_add_tail(insert, newlist);
if (tail)
lxc_list_add_tail(networks, newlist);
else
lxc_list_add(networks, newlist);
return netdev;
}
/* Takes care of finding the correct netdev struct in the networks list or
* allocates a new one if it couldn't be found.
*/
struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
unsigned int idx, bool allocate)
{
struct lxc_netdev *netdev = NULL;
struct lxc_list *networks = &conf->network;
struct lxc_list *insert = networks;
/* lookup network */
if (!lxc_list_empty(networks)) {
lxc_list_for_each(insert, networks) {
netdev = insert->elem;
if (netdev->idx == idx)
return netdev;
else if (netdev->idx > idx)
break;
}
}
if (!allocate)
return NULL;
return lxc_network_add(insert, idx, true);
}
void lxc_log_configured_netdevs(const struct lxc_conf *conf)
{
struct lxc_netdev *netdev;
......@@ -240,7 +244,7 @@ void lxc_log_configured_netdevs(const struct lxc_conf *conf)
lxc_list_for_each(it, &conf->network) {
netdev = it->elem;
TRACE("index: %d", netdev->idx);
TRACE("index: %zd", netdev->idx);
switch (netdev->type) {
case LXC_NET_VETH:
TRACE("type: veth");
......@@ -336,17 +340,17 @@ bool lxc_remove_nic_by_idx(struct lxc_conf *conf, unsigned int idx)
return true;
}
void lxc_free_networks(struct lxc_conf *conf)
void lxc_free_networks(struct lxc_list *networks)
{
struct lxc_list *cur, *next;
struct lxc_netdev *netdev;
lxc_list_for_each_safe(cur, &conf->network, next) {
lxc_list_for_each_safe(cur, networks, next) {
netdev = cur->elem;
lxc_free_netdev(netdev);
free(cur);
}
/* prevent segfaults */
lxc_list_init(&conf->network);
lxc_list_init(networks);
}
......@@ -28,10 +28,12 @@ extern int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
unsigned long *hostid, unsigned long *range);
extern bool lxc_config_value_empty(const char *value);
extern struct lxc_netdev *lxc_network_add(struct lxc_list *networks, int idx,
bool tail);
extern struct lxc_netdev *
lxc_get_netdev_by_idx(struct lxc_conf *conf, unsigned int idx, bool allocate);
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 void lxc_free_networks(struct lxc_conf *conf);
extern void lxc_free_networks(struct lxc_list *networks);
#endif /* __LXC_CONFILE_UTILS_H */
......@@ -298,7 +298,7 @@ int main(int argc, char *argv[])
}
printf("%d: get_config_item(lxc.network) returned %d %s\n", __LINE__, ret, v2);
if (!c->set_config_item(c, "lxc.network.ipv4", "10.2.3.4")) {
if (!c->set_config_item(c, "lxc.network.0.ipv4", "10.2.3.4")) {
fprintf(stderr, "%d: failed to set ipv4\n", __LINE__);
goto out;
}
......@@ -318,7 +318,7 @@ int main(int argc, char *argv[])
goto out;
}
if (!c->set_config_item(c, "lxc.network.ipv4.gateway", "10.2.3.254")) {
if (!c->set_config_item(c, "lxc.network.0.ipv4.gateway", "10.2.3.254")) {
fprintf(stderr, "%d: failed to set ipv4.gateway\n", __LINE__);
goto out;
}
......
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