]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
confile: add getters for lxc.network{.*}
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 31 May 2017 06:19:32 +0000 (08:19 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Wed, 31 May 2017 08:03:30 +0000 (10:03 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/confile.c

index 0de4016897e61eda8f5ea061129f5ebde73b8a6b..b87fcb11570b8fbc99daeeb8161f7d2e8972aff9 100644 (file)
@@ -147,6 +147,8 @@ static int set_config_network_script_up(const char *, const char *, struct lxc_c
 static int set_config_network_script_down(const char *, const char *, struct lxc_conf *);
 static int set_config_network_ipv6(const char *, const char *, struct lxc_conf *);
 static int set_config_network_ipv6_gateway(const char *, const char *, struct lxc_conf *);
+static int get_config_network_item(struct lxc_container *, const char *, char *, int);
+
 static int set_config_cap_drop(const char *, const char *, struct lxc_conf *);
 static int set_config_cap_keep(const char *, const char *, struct lxc_conf *);
 static int set_config_console(const char *, const char *, struct lxc_conf *);
@@ -202,22 +204,22 @@ static struct lxc_config_t config[] = {
        { "lxc.hook.clone",           set_config_hooks,                get_config_hooks,             NULL},
        { "lxc.hook.destroy",         set_config_hooks,                get_config_hooks,             NULL},
        { "lxc.hook",                 set_config_hooks,                get_config_hooks,             NULL},
-       { "lxc.network.type",         set_config_network_type,          NULL, NULL},
-       { "lxc.network.flags",        set_config_network_flags,         NULL, NULL},
-       { "lxc.network.link",         set_config_network_link,          NULL, NULL},
-       { "lxc.network.name",         set_config_network_name,          NULL, NULL},
-       { "lxc.network.macvlan.mode", set_config_network_macvlan_mode,  NULL, NULL},
-       { "lxc.network.veth.pair",    set_config_network_veth_pair,     NULL, NULL},
-       { "lxc.network.script.up",    set_config_network_script_up,     NULL, NULL},
-       { "lxc.network.script.down",  set_config_network_script_down,   NULL, NULL},
-       { "lxc.network.hwaddr",       set_config_network_hwaddr,        NULL, NULL},
-       { "lxc.network.mtu",          set_config_network_mtu,           NULL, NULL},
-       { "lxc.network.vlan.id",      set_config_network_vlan_id,       NULL, NULL},
-       { "lxc.network.ipv4.gateway", set_config_network_ipv4_gateway,  NULL, NULL},
-       { "lxc.network.ipv4",         set_config_network_ipv4,          NULL, NULL},
-       { "lxc.network.ipv6.gateway", set_config_network_ipv6_gateway,  NULL, NULL},
-       { "lxc.network.ipv6",         set_config_network_ipv6,          NULL, NULL},
-       { "lxc.network.",             set_config_network_nic,           NULL, NULL},
+       { "lxc.network.type",         set_config_network_type,         get_config_network_item,      NULL},
+       { "lxc.network.flags",        set_config_network_flags,        get_config_network_item,      NULL},
+       { "lxc.network.link",         set_config_network_link,         get_config_network_item,      NULL},
+       { "lxc.network.name",         set_config_network_name,         get_config_network_item,      NULL},
+       { "lxc.network.macvlan.mode", set_config_network_macvlan_mode, get_config_network_item,      NULL},
+       { "lxc.network.veth.pair",    set_config_network_veth_pair,    get_config_network_item,      NULL},
+       { "lxc.network.script.up",    set_config_network_script_up,    get_config_network_item,      NULL},
+       { "lxc.network.script.down",  set_config_network_script_down,  get_config_network_item,      NULL},
+       { "lxc.network.hwaddr",       set_config_network_hwaddr,       get_config_network_item,      NULL},
+       { "lxc.network.mtu",          set_config_network_mtu,          get_config_network_item,      NULL},
+       { "lxc.network.vlan.id",      set_config_network_vlan_id,      get_config_network_item,      NULL},
+       { "lxc.network.ipv4.gateway", set_config_network_ipv4_gateway, get_config_network_item,      NULL},
+       { "lxc.network.ipv4",         set_config_network_ipv4,         get_config_network_item,      NULL},
+       { "lxc.network.ipv6.gateway", set_config_network_ipv6_gateway, get_config_network_item,      NULL},
+       { "lxc.network.ipv6",         set_config_network_ipv6,         get_config_network_item,      NULL},
+       { "lxc.network.",             set_config_network_nic,          get_config_network_item,      NULL},
        { "lxc.network",              set_config_network,              get_config_network,           NULL},
        { "lxc.cap.drop",             set_config_cap_drop,              NULL, NULL},
        { "lxc.cap.keep",             set_config_cap_keep,              NULL, NULL},
@@ -2744,115 +2746,6 @@ static int lxc_get_item_cap_keep(struct lxc_conf *c, char *retv, int inlen)
        return fulllen;
 }
 
-/*
- * lxc.network.0.XXX, where XXX can be: name, type, link, flags, type,
- * macvlan.mode, veth.pair, vlan, ipv4, ipv6, script.up, hwaddr, mtu,
- * ipv4.gateway, ipv6.gateway.  ipvX.gateway can return 'auto' instead
- * of an address.  ipv4 and ipv6 return lists (newline-separated).
- * things like veth.pair return '' if invalid (i.e. if called for vlan
- * type).
- */
-static int lxc_get_item_nic(struct lxc_conf *c, char *retv, int inlen,
-                           const char *key)
-{
-       char *p1;
-       int len, fulllen = 0;
-       struct lxc_netdev *netdev;
-
-       if (!retv)
-               inlen = 0;
-       else
-               memset(retv, 0, inlen);
-
-       p1 = strchr(key, '.');
-       if (!p1 || *(p1+1) == '\0') return -1;
-       p1++;
-
-       netdev = get_netdev_from_key(key, &c->network);
-       if (!netdev)
-               return -1;
-       if (strcmp(p1, "name") == 0) {
-               if (netdev->name)
-                       strprint(retv, inlen, "%s", netdev->name);
-       } else if (strcmp(p1, "type") == 0) {
-               strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
-       } else if (strcmp(p1, "link") == 0) {
-               if (netdev->link)
-                       strprint(retv, inlen, "%s", netdev->link);
-       } else if (strcmp(p1, "flags") == 0) {
-               if (netdev->flags & IFF_UP)
-                       strprint(retv, inlen, "up");
-       } else if (strcmp(p1, "script.up") == 0) {
-               if (netdev->upscript)
-                       strprint(retv, inlen, "%s", netdev->upscript);
-       } else if (strcmp(p1, "script.down") == 0) {
-               if (netdev->downscript)
-                       strprint(retv, inlen, "%s", netdev->downscript);
-       } else if (strcmp(p1, "hwaddr") == 0) {
-               if (netdev->hwaddr)
-                       strprint(retv, inlen, "%s", netdev->hwaddr);
-       } else if (strcmp(p1, "mtu") == 0) {
-               if (netdev->mtu)
-                       strprint(retv, inlen, "%s", netdev->mtu);
-       } else if (strcmp(p1, "macvlan.mode") == 0) {
-               if (netdev->type == LXC_NET_MACVLAN) {
-                       const char *mode;
-                       switch (netdev->priv.macvlan_attr.mode) {
-                       case MACVLAN_MODE_PRIVATE: mode = "private"; break;
-                       case MACVLAN_MODE_VEPA: mode = "vepa"; break;
-                       case MACVLAN_MODE_BRIDGE: mode = "bridge"; break;
-                       case MACVLAN_MODE_PASSTHRU: mode = "passthru"; break;
-                       default: mode = "(invalid)"; break;
-                       }
-                       strprint(retv, inlen, "%s", mode);
-               }
-       } else if (strcmp(p1, "veth.pair") == 0) {
-               if (netdev->type == LXC_NET_VETH) {
-                       strprint(retv, inlen, "%s",
-                                netdev->priv.veth_attr.pair ?
-                                 netdev->priv.veth_attr.pair :
-                                 netdev->priv.veth_attr.veth1);
-               }
-       } else if (strcmp(p1, "vlan") == 0) {
-               if (netdev->type == LXC_NET_VLAN) {
-                       strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
-               }
-       } else if (strcmp(p1, "ipv4.gateway") == 0) {
-               if (netdev->ipv4_gateway_auto) {
-                       strprint(retv, inlen, "auto");
-               } else if (netdev->ipv4_gateway) {
-                       char buf[INET_ADDRSTRLEN];
-                       inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
-                       strprint(retv, inlen, "%s", buf);
-               }
-       } else if (strcmp(p1, "ipv4") == 0) {
-               struct lxc_list *it2;
-               lxc_list_for_each(it2, &netdev->ipv4) {
-                       struct lxc_inetdev *i = it2->elem;
-                       char buf[INET_ADDRSTRLEN];
-                       inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
-                       strprint(retv, inlen, "%s/%d\n", buf, i->prefix);
-               }
-       } else if (strcmp(p1, "ipv6.gateway") == 0) {
-               if (netdev->ipv6_gateway_auto) {
-                       strprint(retv, inlen, "auto");
-               } else if (netdev->ipv6_gateway) {
-                       char buf[INET6_ADDRSTRLEN];
-                       inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
-                       strprint(retv, inlen, "%s", buf);
-               }
-       } else if (strcmp(p1, "ipv6") == 0) {
-               struct lxc_list *it2;
-               lxc_list_for_each(it2, &netdev->ipv6) {
-                       struct lxc_inet6dev *i = it2->elem;
-                       char buf[INET6_ADDRSTRLEN];
-                       inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
-                       strprint(retv, inlen, "%s/%d\n", buf, i->prefix);
-               }
-       }
-       return fulllen;
-}
-
 int lxc_get_config_item(struct lxc_conf *c, const char *key, char *retv,
                        int inlen)
 {
@@ -2866,8 +2759,6 @@ int lxc_get_config_item(struct lxc_conf *c, const char *key, char *retv,
                return lxc_get_item_cap_drop(c, retv, inlen);
        else if (strcmp(key, "lxc.cap.keep") == 0)
                return lxc_get_item_cap_keep(c, retv, inlen);
-       else if (strncmp(key, "lxc.network.", 12) == 0)
-               return lxc_get_item_nic(c, retv, inlen, key + 12);
        else if (strcmp(key, "lxc.start.auto") == 0)
                return lxc_get_conf_int(c, retv, inlen, c->start_auto);
        else if (strcmp(key, "lxc.start.delay") == 0)
@@ -3871,3 +3762,130 @@ static int get_config_network(struct lxc_container *c, const char *key,
 
        return fulllen;
 }
+
+/*
+ * lxc.network.0.XXX, where XXX can be: name, type, link, flags, type,
+ * macvlan.mode, veth.pair, vlan, ipv4, ipv6, script.up, hwaddr, mtu,
+ * ipv4.gateway, ipv6.gateway.  ipvX.gateway can return 'auto' instead
+ * of an address.  ipv4 and ipv6 return lists (newline-separated).
+ * things like veth.pair return '' if invalid (i.e. if called for vlan
+ * type).
+ */
+static int get_config_network_item(struct lxc_container *c, const char *key,
+                                  char *retv, int inlen)
+{
+       char *p1;
+       int len, fulllen = 0;
+       struct lxc_netdev *netdev;
+
+       if (!retv)
+               inlen = 0;
+       else
+               memset(retv, 0, inlen);
+
+       if (!strncmp(key, "lxc.network.", 12))
+               key += 12;
+       else
+               return -1;
+
+       p1 = strchr(key, '.');
+       if (!p1 || *(p1 + 1) == '\0')
+               return -1;
+       p1++;
+
+       netdev = get_netdev_from_key(key, &c->lxc_conf->network);
+       if (!netdev)
+               return -1;
+       if (strcmp(p1, "name") == 0) {
+               if (netdev->name)
+                       strprint(retv, inlen, "%s", netdev->name);
+       } else if (strcmp(p1, "type") == 0) {
+               strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
+       } else if (strcmp(p1, "link") == 0) {
+               if (netdev->link)
+                       strprint(retv, inlen, "%s", netdev->link);
+       } else if (strcmp(p1, "flags") == 0) {
+               if (netdev->flags & IFF_UP)
+                       strprint(retv, inlen, "up");
+       } else if (strcmp(p1, "script.up") == 0) {
+               if (netdev->upscript)
+                       strprint(retv, inlen, "%s", netdev->upscript);
+       } else if (strcmp(p1, "script.down") == 0) {
+               if (netdev->downscript)
+                       strprint(retv, inlen, "%s", netdev->downscript);
+       } else if (strcmp(p1, "hwaddr") == 0) {
+               if (netdev->hwaddr)
+                       strprint(retv, inlen, "%s", netdev->hwaddr);
+       } else if (strcmp(p1, "mtu") == 0) {
+               if (netdev->mtu)
+                       strprint(retv, inlen, "%s", netdev->mtu);
+       } else if (strcmp(p1, "macvlan.mode") == 0) {
+               if (netdev->type == LXC_NET_MACVLAN) {
+                       const char *mode;
+                       switch (netdev->priv.macvlan_attr.mode) {
+                       case MACVLAN_MODE_PRIVATE:
+                               mode = "private";
+                               break;
+                       case MACVLAN_MODE_VEPA:
+                               mode = "vepa";
+                               break;
+                       case MACVLAN_MODE_BRIDGE:
+                               mode = "bridge";
+                               break;
+                       case MACVLAN_MODE_PASSTHRU:
+                               mode = "passthru";
+                               break;
+                       default:
+                               mode = "(invalid)";
+                               break;
+                       }
+                       strprint(retv, inlen, "%s", mode);
+               }
+       } else if (strcmp(p1, "veth.pair") == 0) {
+               if (netdev->type == LXC_NET_VETH) {
+                       strprint(retv, inlen, "%s",
+                                netdev->priv.veth_attr.pair
+                                    ? netdev->priv.veth_attr.pair
+                                    : netdev->priv.veth_attr.veth1);
+               }
+       } else if (strcmp(p1, "vlan") == 0) {
+               if (netdev->type == LXC_NET_VLAN) {
+                       strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
+               }
+       } else if (strcmp(p1, "ipv4.gateway") == 0) {
+               if (netdev->ipv4_gateway_auto) {
+                       strprint(retv, inlen, "auto");
+               } else if (netdev->ipv4_gateway) {
+                       char buf[INET_ADDRSTRLEN];
+                       inet_ntop(AF_INET, netdev->ipv4_gateway, buf,
+                                 sizeof(buf));
+                       strprint(retv, inlen, "%s", buf);
+               }
+       } else if (strcmp(p1, "ipv4") == 0) {
+               struct lxc_list *it2;
+               lxc_list_for_each(it2, &netdev->ipv4) {
+                       struct lxc_inetdev *i = it2->elem;
+                       char buf[INET_ADDRSTRLEN];
+                       inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
+                       strprint(retv, inlen, "%s/%d\n", buf, i->prefix);
+               }
+       } else if (strcmp(p1, "ipv6.gateway") == 0) {
+               if (netdev->ipv6_gateway_auto) {
+                       strprint(retv, inlen, "auto");
+               } else if (netdev->ipv6_gateway) {
+                       char buf[INET6_ADDRSTRLEN];
+                       inet_ntop(AF_INET6, netdev->ipv6_gateway, buf,
+                                 sizeof(buf));
+                       strprint(retv, inlen, "%s", buf);
+               }
+       } else if (strcmp(p1, "ipv6") == 0) {
+               struct lxc_list *it2;
+               lxc_list_for_each(it2, &netdev->ipv6) {
+                       struct lxc_inet6dev *i = it2->elem;
+                       char buf[INET6_ADDRSTRLEN];
+                       inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
+                       strprint(retv, inlen, "%s/%d\n", buf, i->prefix);
+               }
+       }
+       return fulllen;
+}