]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
network: preserve backwards compatibility
authorChristian Brauner <christian.brauner@ubuntu.com>
Sat, 17 Jun 2017 06:53:41 +0000 (08:53 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 18 Jun 2017 09:55:34 +0000 (11:55 +0200)
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: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.c
src/lxc/conf.h
src/lxc/confile.c
src/lxc/confile_utils.c
src/lxc/confile_utils.h
src/tests/get_item.c

index 9d0f6e9e1f8ae0226b15fcdbb5bb9227dd63004e..b8b81fd902e4baf683a4e66287ab4e58f287a67b 100644 (file)
@@ -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);
index 7bf624395aebf04fcca3e70a630031d14c92e67a..e2a90cac52a827280a33f75c8493475e56e52d33 100644 (file)
@@ -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;
index 0190b2983d0a4ee62d4d76fbc82822bce5ae50f4..8f99b886f7fc96d1bf28d7c99f82f7338c4c4a51 100644 (file)
@@ -695,11 +695,29 @@ static int set_config_network_type(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_type(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
+
+               /* We maintain a negative count for legacy network devices. */
+               ssize_t negidx = -1;
                WARN("WARNING: using \"lxc.network.type\" without an index "
                     "(e.g.\"lxc.network.0.type\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               if (!lxc_list_empty(&lxc_conf->network)) {
+                       struct lxc_netdev *ndv;
+                       ndv = lxc_list_first_elem(&lxc_conf->network);
+                       if (ndv->idx < 0) {
+                               negidx = ndv->idx;
+                               negidx--;
+                       }
+               }
+               if (negidx == INT_MIN) {
+                       SYSERROR("number of configured networks would overflow "
+                                "the counter... what are you doing?");
+                       return -1;
+               }
+               netdev = lxc_network_add(&lxc_conf->network, negidx, false);
        } else {
                netdev = data;
        }
@@ -917,11 +935,13 @@ static int set_config_network_flags(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_flags(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.flags\" without an index "
                     "(e.g.\"lxc.network.0.flags\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -933,22 +953,6 @@ static int set_config_network_flags(const char *key, const char *value,
        return 0;
 }
 
-static int set_network_link(const char *key, const char *value,
-                           struct lxc_conf *lxc_conf, void *data)
-{
-       struct lxc_netdev *netdev;
-
-       /* lxc.network.* without an index */
-       if (!data)
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
-       else
-               netdev = data;
-       if (!netdev)
-               return -1;
-
-       return network_ifname(&netdev->link, value);
-}
-
 static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf,
                                  struct lxc_netdev *netdev)
 {
@@ -974,8 +978,8 @@ static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf,
                        ret = set_config_network_type(type_key, tmpvalue,
                                                      lxc_conf, netdev);
                        if (!ret) {
-                               ret = set_network_link(link_key, ifa->ifa_name,
-                                                      lxc_conf, netdev);
+                               ret = set_config_network_link(
+                                   link_key, ifa->ifa_name, lxc_conf, netdev);
                                if (ret) {
                                        ERROR("failed to create matched ifnames");
                                        break;
@@ -1002,11 +1006,13 @@ static int set_config_network_link(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_link(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.link\" without an index "
                     "(e.g.\"lxc.network.0.link\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1029,11 +1035,13 @@ static int set_config_network_name(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_name(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.name\" without an index "
                     "(e.g.\"lxc.network.0.name\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1051,11 +1059,13 @@ static int set_config_network_veth_pair(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_veth_pair(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.veth.pair\" without an "
                     "index (e.g. \"lxc.network.0.veth.pair\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1076,10 +1086,13 @@ static int set_config_network_macvlan_mode(const char *key, const char *value,
 
        /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.macvlan.mode\" without an "
                     "index (e.g. \"lxc.network.0.macvlan.mode\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1098,11 +1111,13 @@ static int set_config_network_hwaddr(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_hwaddr(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.hwaddr\" without an index "
                     "(e.g. \"lxc.network.0.hwaddr\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1134,11 +1149,13 @@ static int set_config_network_vlan_id(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_vlan_id(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.vlan.id\" without an index "
                     "(e.g. \"lxc.network.0.vlan.id\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1159,11 +1176,13 @@ static int set_config_network_mtu(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_mtu(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.mtu\" without an index "
                     "(e.g. \"lxc.network.0.mtu\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1185,11 +1204,13 @@ static int set_config_network_ipv4(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_ipv4(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv4\" without an index "
                     "(e.g. \"lxc.network.0.ipv4\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1281,12 +1302,14 @@ static int set_config_network_ipv4_gateway(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_ipv4_gateway(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv4.gateway\" without an "
                     "index (e.g. \"lxc.network.0.ipv4.gateway\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1333,11 +1356,13 @@ static int set_config_network_ipv6(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_ipv6(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv6\" without an index "
                     "(e.g. \"lxc.network.0.ipv6\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1401,12 +1426,14 @@ static int set_config_network_ipv6_gateway(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_ipv6_gateway(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv6.gateway\" without an "
                     "index (e.g. \"lxc.network.0.ipv6.gateway\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1450,12 +1477,14 @@ static int set_config_network_script_up(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_script_up(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.script.up\" without an "
                     "index (e.g. \"lxc.network.0.script.up\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -1473,12 +1502,14 @@ static int set_config_network_script_down(const char *key, const char *value,
        if (lxc_config_value_empty(value))
                return clr_config_network_script_down(key, lxc_conf, data);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.script.down\" without an "
                     "index (e.g. \"lxc.network.0.script.down\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, true);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4103,7 +4134,8 @@ static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
 static inline int clr_config_network(const char *key, struct lxc_conf *c,
                                     void *data)
 {
-       lxc_free_networks(c);
+       lxc_free_networks(&c->network);
+
        return 0;
 }
 
@@ -4305,8 +4337,9 @@ get_network_config_ops(const char *key, struct lxc_conf *lxc_conf, ssize_t *idx)
 
                /* This, of course is utterly nonsensical on so many levels, but
                 * better safe than sorry.
+                * (Checking for INT_MAX here is intentional.)
                 */
-               if (tmpidx == UINT_MAX) {
+               if (tmpidx == INT_MAX) {
                        SYSERROR(
                            "number of configured networks would overflow the "
                            "counter... what are you doing?");
@@ -4409,11 +4442,13 @@ static int clr_config_network_type(const char *key, struct lxc_conf *lxc_conf,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.type\" without an index "
                     "(e.g. \"lxc.network.0.type\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4430,11 +4465,13 @@ static int clr_config_network_name(const char *key, struct lxc_conf *lxc_conf,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.name\" without an index "
                     "(e.g. \"lxc.network.0.name\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4453,11 +4490,13 @@ static int clr_config_network_flags(const char *key, struct lxc_conf *lxc_conf,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.flags\" without an index "
                     "(e.g. \"lxc.network.0.flags\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4474,11 +4513,13 @@ static int clr_config_network_link(const char *key, struct lxc_conf *lxc_conf,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.link\" without an index "
                     "(e.g. \"lxc.network.0.link\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4497,12 +4538,14 @@ static int clr_config_network_macvlan_mode(const char *key,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.macvlan.mode\" without an "
                     "index (e.g. \"lxc.network.0.macvlan.mode\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4522,12 +4565,14 @@ static int clr_config_network_veth_pair(const char *key,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.veth.pair\" without an "
                     "index (e.g. \"lxc.network.0.veth.pair\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4545,12 +4590,14 @@ static int clr_config_network_script_up(const char *key,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.script.up\" without an "
                     "index (e.g. \"lxc.network.0.script.up\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4568,12 +4615,14 @@ static int clr_config_network_script_down(const char *key,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.script.down\" without an "
                     "index (e.g. \"lxc.network.0.script.down\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4591,11 +4640,13 @@ static int clr_config_network_hwaddr(const char *key, struct lxc_conf *lxc_conf,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.hwaddr\" without an index "
                     "(e.g. \"lxc.network.0.hwaddr\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4613,11 +4664,13 @@ static int clr_config_network_mtu(const char *key, struct lxc_conf *lxc_conf,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.mtu\" without an index "
                     "(e.g. \"lxc.network.0.mtu\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4635,11 +4688,13 @@ static int clr_config_network_vlan_id(const char *key,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.vlan.id\" without an index "
                     "(e.g. \"lxc.network.0.vlan.id\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4657,12 +4712,14 @@ static int clr_config_network_ipv4_gateway(const char *key,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv4.gateway\" without an "
                     "index (e.g. \"lxc.network.0.ipv4.gateway\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4681,11 +4738,13 @@ static int clr_config_network_ipv4(const char *key, struct lxc_conf *lxc_conf,
        struct lxc_netdev *netdev;
        struct lxc_list *cur, *next;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv4\" without an index "
                     "(e.g. \"lxc.network.0.ipv4\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4707,12 +4766,14 @@ static int clr_config_network_ipv6_gateway(const char *key,
 {
        struct lxc_netdev *netdev;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv6.gateway\" without an "
                     "index (e.g. \"lxc.network.0.ipv6.gateway\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4731,11 +4792,13 @@ static int clr_config_network_ipv6(const char *key, struct lxc_conf *lxc_conf,
        struct lxc_netdev *netdev;
        struct lxc_list *cur, *next;
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv6\" without an index "
                     "(e.g. \"lxc.network.0.ipv6\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(lxc_conf, 0, false);
+               netdev = lxc_list_first_elem(&lxc_conf->network);
        } else {
                netdev = data;
        }
@@ -4780,11 +4843,13 @@ static int get_config_network_type(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.type\" without an index "
                     "(e.g. \"lxc.network.0.type\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -4807,11 +4872,13 @@ static int get_config_network_flags(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.flags\" without an index "
                     "(e.g. \"lxc.network.0.flags\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -4835,11 +4902,13 @@ static int get_config_network_link(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.link\" without an index "
                     "(e.g. \"lxc.network.0.link\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -4863,11 +4932,13 @@ static int get_config_network_name(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.name\" without an index "
                     "(e.g. \"lxc.network.0.name\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -4893,12 +4964,14 @@ static int get_config_network_macvlan_mode(const char *key, char *retv,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.macvlan.mode\" without an "
                     "index (e.g. \"lxc.network.0.macvlan.mode\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -4942,12 +5015,14 @@ static int get_config_network_veth_pair(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.veth.pair\" without an "
                     "index (e.g. \"lxc.network.0.veth.pair\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -4975,12 +5050,14 @@ static int get_config_network_script_up(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.script.up\" without an "
                     "index (e.g. \"lxc.network.0.script.up\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -5005,12 +5082,14 @@ static int get_config_network_script_down(const char *key, char *retv,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.script.down\" without an "
                     "index (e.g. \"lxc.network.0.script.down\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -5034,11 +5113,13 @@ static int get_config_network_hwaddr(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.hwaddr\" without an index "
                     "(e.g. \"lxc.network.0.hwaddr\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -5062,11 +5143,13 @@ static int get_config_network_mtu(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.mtu\" without an index "
                     "(e.g. \"lxc.network.0.mtu\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -5090,11 +5173,13 @@ static int get_config_network_vlan_id(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.vlan.id\" without an index "
                     "(e.g. \"lxc.network.0.vlan.id\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -5122,12 +5207,14 @@ static int get_config_network_ipv4_gateway(const char *key, char *retv,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv4.gateway\" without an "
                     "index (e.g. \"lxc.network.0.ipv4.gateway\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -5158,11 +5245,13 @@ static int get_config_network_ipv4(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv4\" without an index "
                     "(e.g. \"lxc.network.0.ipv4\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -5193,12 +5282,14 @@ static int get_config_network_ipv6_gateway(const char *key, char *retv,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv6.gateway\" without an "
                     "index (e.g. \"lxc.network.0.ipv6.gateway\" is "
                     "DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
@@ -5229,11 +5320,13 @@ static int get_config_network_ipv6(const char *key, char *retv, int inlen,
        else
                memset(retv, 0, inlen);
 
-       /* lxc.network.* without an index */
        if (!data) {
+               /* REMOVE IN LXC 3.0:
+                * lxc.network.* without an index
+                */
                WARN("WARNING: using \"lxc.network.ipv6\" without an index "
                     "(e.g. \"lxc.network.0.ipv6\") is DEPRECATED.");
-               netdev = lxc_get_netdev_by_idx(c, 0, false);
+               netdev = lxc_list_first_elem(&c->network);
        } else {
                netdev = data;
        }
index 86076b51b0a3aca12d98625530d22e01deceb2f3..9bc61628419ceadf8ef6fe65d63ecd25df295d1f 100644 (file)
@@ -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);
 }
index 94902b1cb66de8f8fd9a653c159b26a1f620b9ea..f50dc336543a32bbac9933b8ffb42ca3ce182a41 100644 (file)
@@ -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 */
index 4e4a51196ade24a27dd419ac449b2a7bf912816a..6283e36fa257ca475144ed666f36f9dd59bef3dc 100644 (file)
@@ -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;
        }