]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip link: Add ipvlan support to the iproute2/ip util
authorMahesh Bandewar <maheshb@google.com>
Mon, 24 Nov 2014 02:06:11 +0000 (18:06 -0800)
committerStephen Hemminger <shemming@brocade.com>
Wed, 3 Dec 2014 17:37:37 +0000 (09:37 -0800)
Adding basic support to create virtual devices using 'ip'
utility. Following is the syntax -

ip link add link <master> <virtual> type ipvlan mode [ l2 | l3 ]
e.g. ip link add link eth0 ipvl0 type ipvlan mode l3

Signed-off-by: Mahesh Bandewar <maheshb@google.com>
Cc: Stephen Hemminger <stephen@networkplumber.org>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Maciej Żenczykowski <maze@google.com>
Cc: Laurent Chavey <chavey@google.com>
Cc: Tim Hockin <thockin@google.com>
Cc: Brandon Philips <brandon.philips@coreos.com>
Cc: Pavel Emelianov <xemul@parallels.com>
ip/Makefile
ip/iplink.c
ip/iplink_ipvlan.c [new file with mode: 0644]

index 1f50848aed1f6262f42d87dd574e54507fee3326..2c742f305fefaa244d5c9d1657c8699d8dd8225c 100644 (file)
@@ -6,7 +6,7 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \
     iplink_macvlan.o iplink_macvtap.o ipl2tp.o link_vti.o link_vti6.o \
     iplink_vxlan.o tcp_metrics.o iplink_ipoib.o ipnetconf.o link_ip6tnl.o \
     link_iptnl.o link_gre6.o iplink_bond.o iplink_bond_slave.o iplink_hsr.o \
-    iplink_bridge.o iplink_bridge_slave.o ipfou.o
+    iplink_bridge.o iplink_bridge_slave.o ipfou.o iplink_ipvlan.o
 
 RTMONOBJ=rtmon.o
 
index ce6eb3e1f7c906678c79e9f3be30eaf11efbc479..1a967d1e5dfbd2a77ce1aae4bf90a591fafbc70a 100644 (file)
@@ -90,7 +90,7 @@ void iplink_usage(void)
                fprintf(stderr, "TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | macvtap |\n");
                fprintf(stderr, "          bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan |\n");
                fprintf(stderr, "          gre | gretap | ip6gre | ip6gretap | vti | nlmon |\n");
-               fprintf(stderr, "          bond_slave }\n");
+               fprintf(stderr, "          bond_slave | ipvlan }\n");
        }
        exit(-1);
 }
diff --git a/ip/iplink_ipvlan.c b/ip/iplink_ipvlan.c
new file mode 100644 (file)
index 0000000..e08fc39
--- /dev/null
@@ -0,0 +1,98 @@
+/* iplink_ipvlan.c     IPVLAN device support
+ *
+ *              This program is free software; you can redistribute it and/or
+ *              modify it under the terms of the GNU General Public License
+ *              as published by the Free Software Foundation; either version
+ *              2 of the License, or (at your option) any later version.
+ *
+ * Authors:     Mahesh Bandewar <maheshb@google.com>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <linux/if_link.h>
+
+#include "rt_names.h"
+#include "utils.h"
+#include "ip_common.h"
+
+static void ipvlan_explain(FILE *f)
+{
+       fprintf(f, "Usage: ... ipvlan [ mode { l2 | l3 } ]\n");
+}
+
+static void explain(void)
+{
+       ipvlan_explain(stderr);
+}
+
+static int mode_arg(void)
+{
+       fprintf(stderr, "Error: argument of \"mode\" must be either \"l2\", "
+               "or \"l3\"\n");
+       return -1;
+}
+
+static int ipvlan_parse_opt(struct link_util *lu, int argc, char **argv,
+                           struct nlmsghdr *n)
+{
+       while (argc > 0) {
+               if (matches(*argv, "mode") == 0) {
+                       __u16 mode = 0;
+                       NEXT_ARG();
+
+                       if (strcmp(*argv, "l2") == 0)
+                               mode = IPVLAN_MODE_L2;
+                       else if (strcmp(*argv, "l3") == 0)
+                               mode = IPVLAN_MODE_L3;
+                       else
+                               return mode_arg();
+
+                       addattr16(n, 1024, IFLA_IPVLAN_MODE, mode);
+               } else if (matches(*argv, "help") == 0) {
+                       explain();
+                       return -1;
+               } else {
+                       fprintf(stderr, "ipvlan: unknown option \"%s\"?\n",
+                               *argv);
+                       explain();
+                       return -1;
+               }
+               argc--, argv++;
+       }
+
+       return 0;
+}
+
+static void ipvlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
+{
+
+       if (!tb)
+               return;
+
+       if (tb[IFLA_IPVLAN_MODE]) {
+               if (RTA_PAYLOAD(tb[IFLA_IPVLAN_MODE]) == sizeof(__u16)) {
+                       __u16 mode = rta_getattr_u16(tb[IFLA_IPVLAN_MODE]);
+
+                       fprintf(f, " mode %s ",
+                               mode == IPVLAN_MODE_L2 ? "l2" :
+                               mode == IPVLAN_MODE_L3 ? "l3" : "unknown");
+               }
+       }
+}
+
+static void ipvlan_print_help(struct link_util *lu, int argc, char **argv,
+                             FILE *f)
+{
+       ipvlan_explain(f);
+}
+
+struct link_util ipvlan_link_util = {
+       .id             = "ipvlan",
+       .maxattr        = IFLA_IPVLAN_MAX,
+       .parse_opt      = ipvlan_parse_opt,
+       .print_opt      = ipvlan_print_opt,
+       .print_help     = ipvlan_print_help,
+};