]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
network: Adds mode param (bridge, router) to veth network setting
authorThomas Parrott <thomas.parrott@canonical.com>
Fri, 5 Jul 2019 13:46:49 +0000 (14:46 +0100)
committerThomas Parrott <thomas.parrott@canonical.com>
Thu, 11 Jul 2019 11:37:23 +0000 (12:37 +0100)
Defaulting to bridge mode.

Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
src/lxc/confile.c
src/lxc/confile_utils.c
src/lxc/confile_utils.h
src/lxc/macro.h
src/lxc/network.h

index b08aa0174fda2feeb76dc5feb3e402edfded4fba..36d62cbcac140a9df525f390cd8a39ba1b694a28 100644 (file)
@@ -139,6 +139,7 @@ lxc_config_define(net_nic);
 lxc_config_define(net_script_down);
 lxc_config_define(net_script_up);
 lxc_config_define(net_type);
+lxc_config_define(net_veth_mode);
 lxc_config_define(net_veth_pair);
 lxc_config_define(net_veth_ipv4_route);
 lxc_config_define(net_veth_ipv6_route);
@@ -234,6 +235,7 @@ static struct lxc_config_t config_jump_table[] = {
        { "lxc.net.script.up",             set_config_net_script_up,               get_config_net_script_up,               clr_config_net_script_up,             },
        { "lxc.net.type",                  set_config_net_type,                    get_config_net_type,                    clr_config_net_type,                  },
        { "lxc.net.vlan.id",               set_config_net_vlan_id,                 get_config_net_vlan_id,                 clr_config_net_vlan_id,               },
+       { "lxc.net.veth.mode",             set_config_net_veth_mode,               get_config_net_veth_mode,               clr_config_net_veth_mode,             },
        { "lxc.net.veth.pair",             set_config_net_veth_pair,               get_config_net_veth_pair,               clr_config_net_veth_pair,             },
        { "lxc.net.veth.ipv4.route",       set_config_net_veth_ipv4_route,         get_config_net_veth_ipv4_route,         clr_config_net_veth_ipv4_route,       },
        { "lxc.net.veth.ipv6.route",       set_config_net_veth_ipv6_route,         get_config_net_veth_ipv6_route,         clr_config_net_veth_ipv6_route,       },
@@ -303,6 +305,7 @@ static int set_config_net_type(const char *key, const char *value,
                netdev->type = LXC_NET_VETH;
                lxc_list_init(&netdev->priv.veth_attr.ipv4_routes);
                lxc_list_init(&netdev->priv.veth_attr.ipv6_routes);
+               lxc_veth_mode_to_flag(&netdev->priv.veth_attr.mode, "bridge");
        } else if (strcmp(value, "macvlan") == 0) {
                netdev->type = LXC_NET_MACVLAN;
                lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, "private");
@@ -450,6 +453,21 @@ static int set_config_net_name(const char *key, const char *value,
        return network_ifname(netdev->name, value, sizeof(netdev->name));
 }
 
+
+static int set_config_net_veth_mode(const char *key, const char *value,
+                                      struct lxc_conf *lxc_conf, void *data)
+{
+       struct lxc_netdev *netdev = data;
+
+       if (lxc_config_value_empty(value))
+               return clr_config_net_veth_mode(key, lxc_conf, data);
+
+       if (!netdev)
+               return -1;
+
+       return lxc_veth_mode_to_flag(&netdev->priv.veth_attr.mode, value);
+}
+
 static int set_config_net_veth_pair(const char *key, const char *value,
                                    struct lxc_conf *lxc_conf, void *data)
 {
@@ -5094,6 +5112,22 @@ static int clr_config_net_ipvlan_isolation(const char *key,
        return 0;
 }
 
+static int clr_config_net_veth_mode(const char *key,
+                                      struct lxc_conf *lxc_conf, void *data)
+{
+       struct lxc_netdev *netdev = data;
+
+       if (!netdev)
+               return minus_one_set_errno(EINVAL);
+
+       if (netdev->type != LXC_NET_VETH)
+               return 0;
+
+       netdev->priv.veth_attr.mode = -1;
+
+       return 0;
+}
+
 static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
                                    void *data)
 {
@@ -5516,6 +5550,42 @@ static int get_config_net_ipvlan_isolation(const char *key, char *retv, int inle
        return fulllen;
 }
 
+static int get_config_net_veth_mode(const char *key, char *retv, int inlen,
+                                      struct lxc_conf *c, void *data)
+{
+       int len;
+       int fulllen = 0;
+       const char *mode;
+       struct lxc_netdev *netdev = data;
+
+       if (!retv)
+               inlen = 0;
+       else
+               memset(retv, 0, inlen);
+
+       if (!netdev)
+               return minus_one_set_errno(EINVAL);
+
+       if (netdev->type != LXC_NET_VETH)
+               return 0;
+
+       switch (netdev->priv.veth_attr.mode) {
+       case VETH_MODE_BRIDGE:
+               mode = "bridge";
+               break;
+       case VETH_MODE_ROUTER:
+               mode = "router";
+               break;
+       default:
+               mode = "(invalid)";
+               break;
+       }
+
+       strprint(retv, inlen, "%s", mode);
+
+       return fulllen;
+}
+
 static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
                                    struct lxc_conf *c, void *data)
 {
index 4394458854066cbf5ba100342861b1560cd28af2..6941f402638ad405d8b68ce583fa43996e6788ec 100644 (file)
@@ -501,6 +501,28 @@ void lxc_free_networks(struct lxc_list *networks)
        lxc_list_init(networks);
 }
 
+
+static struct lxc_veth_mode {
+       char *name;
+       int mode;
+} veth_mode[] = {
+    { "bridge", VETH_MODE_BRIDGE },
+    { "router", VETH_MODE_ROUTER },
+};
+
+int lxc_veth_mode_to_flag(int *mode, const char *value)
+{
+       for (size_t i = 0; i < sizeof(veth_mode) / sizeof(veth_mode[0]); i++) {
+               if (strcmp(veth_mode[i].name, value) != 0)
+                       continue;
+
+               *mode = veth_mode[i].mode;
+               return 0;
+       }
+
+       return minus_one_set_errno(EINVAL);
+}
+
 static struct lxc_macvlan_mode {
        char *name;
        int mode;
index cfed91dc0907b53be166c3beb991725ab23f2548..f68f9604fecfc07a86e36ce4b949427a2fe6657b 100644 (file)
@@ -56,6 +56,7 @@ 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_list *networks);
+extern int lxc_veth_mode_to_flag(int *mode, const char *value);
 extern int lxc_macvlan_mode_to_flag(int *mode, const char *value);
 extern char *lxc_macvlan_flag_to_mode(int mode);
 extern int lxc_ipvlan_mode_to_flag(int *mode, const char *value);
index 288f5a8e6fe4483373fe50d669b19a2e16601961..a054176c628934227a6bcb97f4832af6b71697b8 100644 (file)
@@ -281,6 +281,14 @@ extern int __build_bug_on_failed;
 #define VETH_INFO_PEER 1
 #endif
 
+#ifndef VETH_MODE_BRIDGE
+#define VETH_MODE_BRIDGE 1
+#endif
+
+#ifndef VETH_MODE_ROUTER
+#define VETH_MODE_ROUTER 2
+#endif
+
 #ifndef IFLA_MACVLAN_MODE
 #define IFLA_MACVLAN_MODE 1
 #endif
index 483fbb8cd430a1d14e8c14ce580760899c95bfd6..7361ce5c16cb4f257aab91957f344d3b2eac985d 100644 (file)
@@ -98,6 +98,7 @@ struct ifla_veth {
        int ifindex;
        struct lxc_list ipv4_routes;
        struct lxc_list ipv6_routes;
+       int mode; /* bridge, router */
 };
 
 struct ifla_vlan {