Skip to contentSkip to conversation

ipvlan: Initial check-in of the IPVLAN driver.

PATCH Superseded Mahesh Bandewar 3 participants 6 messages Nov 17, 2014 – Nov 21, 2014

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ipvlan: Initial check-in of the IPVLAN driver.

This driver is very similar to the macvlan driver except that it uses L3 on the frame to determine the logical interface while functioning as packet dispatcher. It inherits L2 of the master device hence the packets on wire will have the same L2 for all the packets originating from all virtual devices off of the same master device.

This driver was developed keeping the namespace use-case in mind. Hence most of the examples given here take that as the base setup where main-device belongs to the default-ns and virtual devices are assigned to the additional namespaces.

The device operates in two different modes and the difference in these two modes in primarily in the TX side.

(a) L2 mode : In this mode, the device behaves as a L2 device. TX processing upto L2 happens on the stack of the virtual device associated with (namespace). Packets are switched after that into the main device (default-ns) and queued for xmit.

RX processing is simple and all multicast, broadcast (if applicable), and unicast belonging to the address(es) are delivered to the virtual devices.

(b) L3 mode : In this mode, the device behaves like a L3 device. TX processing upto L3 happens on the stack of the virtual device associated with (namespace). Packets are switched to the main-device (default-ns) for the L2 processing. Hence the routing table of the default-ns will be used in this mode.

RX processins is somewhat similar to the L2 mode except that in this mode only Unicast packets are delivered to the virtual device while main-dev will handle all other packets.

The devices can be added using the "ip" command from the iproute2 package -

ip link add link <master> <virtual> type ipvlan mode [ l2 | l3 ]

Cc Eric Dumazet <[email protected]>
Cc Maciej Żenczykowski <[email protected]>
Cc Laurent Chavey <[email protected]>
Cc Tim Hockin <[email protected]>
Cc Brandon Philips <[email protected]>
Cc Pavel Emelianov <[email protected]>
---
 Documentation/networking/ipvlan.txt | 114 +++++
 drivers/net/Kconfig                 |  18 +
 drivers/net/Makefile                |   1 +
 drivers/net/ipvlan/Makefile         |   7 +
 drivers/net/ipvlan/ipvlan.h         | 157 +++++++
 drivers/net/ipvlan/ipvlan_core.c    | 630 +++++++++++++++++++++++++++
 drivers/net/ipvlan/ipvlan_main.c    | 830 ++++++++++++++++++++++++++++++++++++
 drivers/net/ipvlan/ipvlan_sysfs.c   | 119 ++++++
 include/linux/netdevice.h           |   4 +
 include/uapi/linux/if_link.h        |  15 +
 10 files changed, 1895 insertions(+)
 create mode 100644 Documentation/networking/ipvlan.txt
 create mode 100644 drivers/net/ipvlan/Makefile
 create mode 100644 drivers/net/ipvlan/ipvlan.h
 create mode 100644 drivers/net/ipvlan/ipvlan_core.c
 create mode 100644 drivers/net/ipvlan/ipvlan_main.c
 create mode 100644 drivers/net/ipvlan/ipvlan_sysfs.c
Documentation/networking/ipvlan.txt new file +114 -0
new file mode 100644
index 000000000000..1f5d34cb4841
@@ -0,0 +1,114 @@
+
+                            IPVLAN Driver HOWTO
+
+Initial Release:
+	Mahesh Bandewar <maheshb AT google.com>
+
+1. Introduction:
+	This is conceptually very similar to the macvlan driver with one major
+exception of using L3 for mux-ing /demux-ing among slaves. This property makes
+the master device share the L2 with it's slave devices. I have developed this
+driver in conjuntion with network namespaces and not sure if there is use case
+outside of it.
+
+
+2. Building and Installation:
+	In order to build the driver, please select the config item CONFIG_IPVLAN.
+The driver can be built into the kernel (CONFIG_IPVLAN=y) or as a module
+(CONFIG_IPVLAN=m).
+
+
+3. Configuration:
+	There are no module parameters for this driver and it can be configured
+using IProute2/ip utility.
+
+	ip link add link <master-dev> <slave-dev> type ipvlan mode { l2 | L3 }
+
+	e.g. ip link add link ipvl0 eth0 type ipvlan mode l2
+
+
+4. Operating modes:
+	IPvlan has two modes of operation - L2 and L3. For a given master device,
+you can select one of these two modes and all slaves on that master will
+operate in the same (selected) mode. The RX mode is almost identical except
+that in L3 mode the slaves wont receive any multicast / broadcast traffic.
+L3 mode is more restrictive since routing is controlled from the other (mostly)
+default namespace.
+
+4.1 L2 mode:
+	In this mode TX processing happens on the stack instance attached to the
+slave device and packets are switched and queued to the master device to send
+out. In this mode the slaves will RX/TX multicast and broadcast (if applicable)
+as well.
+
+4.2 L3 mode:
+	In this mode TX processing upto L3 happens on the stack instance attached
+to the slave device and packets are switched to the stack instance of the
+master device for the L2 processing and routing from that instance will be
+used before packets are queued on the outbound device. In this mode the slaves
+will not receive nor can send multicast / broadcast traffic.
+
+
+5. Sysfs interface:
+	Currently the mode of operation is available at -
+		 /sys/class/net/<master>/ipvlan/mode
+The value can be 0 or 1; where 0 :=> L2, 1 := L3 mode
+
+
+6. What to choose (macvlan vs. ipvlan)?
+	These two devices are very similar in many regards and the specific use
+case could very well define which device to choose. if one of the following
+situations defines your use case then you can choose to use ipvlan -
+	(a) The Linux host that is connected to the external switch / router has
+policy configured that allows only one mac per port.
+	(b) No of virtual devices created on a master exceed the mac capacity and
+puts the NIC in promiscous mode and degraded performance is a concern.
+	(c) If the slave device is to be put into the hostile / untrusted network
+namespace where L2 on the slave could be changed / misused.
+
+
+7. Example configuration:
+
+  +=============================================================+
+  |  Host: host1                                                |
+  |                                                             |
+  |   +----------------------+      +----------------------+    |
+  |   |   NS:ns0             |      |  NS:ns1              |    |
+  |   |                      |      |                      |    |
+  |   |                      |      |                      |    |
+  |   |        ipvl0         |      |         ipvl1        |    |
+  |   +----------#-----------+      +-----------#----------+    |
+  |              #                              #               |
+  |              ################################               |
+  |                              # eth0                         |
+  +==============================#==============================+
+
+
+	(a) Create two network namespaces - ns0, ns1
+		ip netns add ns0
+		ip netns add ns1
+
+	(b) Create two ipvlan slaves on eth0 (master device)
+		ip link add link eth0 ipvl0 type ipvlan mode l2
+		ip link add link eth0 ipvl1 type ipvlan mode l2
+
+	(c) Assign slaves to the respective network namespaces
+		ip link set dev ipvl0 netns ns0
+		ip link set dev ipvl1 netns ns1
+
+	(d) Now switch to the namespace (ns0 or ns1) to configure the slave devices
+		- For ns0
+			(1) ip netns exec ns0 bash
+			(2) ip link set dev ipvl0 up
+			(3) ip link set dev lo up
+			(4) ip -4 addr add 127.0.0.1 dev lo
+			(5) ip -4 addr add $IPADDR dev ipvl0
+			(6) ip -4 route add default via $ROUTER dev ipvl0
+		- For ns1
+			(1) ip netns exec ns1 bash
+			(2) ip link set dev ipvl1 up
+			(3) ip link set dev lo up
+			(4) ip -4 addr add 127.0.0.1 dev lo
+			(5) ip -4 addr add $IPADDR dev ipvl1
+			(6) ip -4 route add default via $ROUTER dev ipvl1
+
drivers/net/Kconfig +18 -0
index f9009be3f307..b6d64f546574 100644
@@ -145,6 +145,24 @@ config MACVTAP
 	  To compile this driver as a module, choose M here: the module
 	  will be called macvtap.
 
+
+config IPVLAN
+    tristate "IP-VLAN support"
+    ---help---
+      This allows one to create virtual devices off of a main interface
+      and packets will be delivered based on the dest L3 (IPv6/IPv4 addr)
+      on packets. All interfaces (including the main interface) share L2
+      making it transparent to the connected L2 switch.
+
+      Ipvlan devices can be added using the "ip" command from the
+      iproute2 package starting with the iproute2-X.Y.ZZ release:
+
+      "ip link add link <main-dev> [ NAME ] type ipvlan"
+
+      To compile this driver as a module, choose M here: the module
+      will be called ipvlan.
+
+
 config VXLAN
        tristate "Virtual eXtensible Local Area Network (VXLAN)"
        depends on INET
drivers/net/Makefile +1 -0
index 61aefdd1e173..e25fdd7d905e 100644
@@ -6,6 +6,7 @@
 # Networking Core Drivers
 #
 obj-$(CONFIG_BONDING) += bonding/
+obj-$(CONFIG_IPVLAN) += ipvlan/
 obj-$(CONFIG_DUMMY) += dummy.o
 obj-$(CONFIG_EQUALIZER) += eql.o
 obj-$(CONFIG_IFB) += ifb.o
drivers/net/ipvlan/Makefile new file +7 -0
new file mode 100644
index 000000000000..2efff4e9bb40
@@ -0,0 +1,7 @@
+#
+# Makefile for the Ethernet Ipvlan driver
+#
+
+obj-$(CONFIG_IPVLAN) += ipvlan.o
+
+ipvlan-objs := ipvlan_core.o ipvlan_main.o ipvlan_sysfs.o
drivers/net/ipvlan/ipvlan.h new file +157 -0

157 lines not on this page · raw message

drivers/net/ipvlan/ipvlan_core.c new file +630 -0

630 lines not on this page · raw message

drivers/net/ipvlan/ipvlan_main.c new file +830 -0

830 lines not on this page · raw message

drivers/net/ipvlan/ipvlan_sysfs.c new file +119 -0

119 lines not on this page · raw message

include/linux/netdevice.h +4 -0
index db63cf459ba1..c6bbc8d4b4f8 100644
@@ -1228,6 +1228,8 @@ enum netdev_priv_flags {
 	IFF_LIVE_ADDR_CHANGE		= 1<<20,
 	IFF_MACVLAN			= 1<<21,
 	IFF_XMIT_DST_RELEASE_PERM	= 1<<22,
+	IFF_IPVLAN_MASTER		= 1<<23,
+	IFF_IPVLAN_SLAVE		= 1<<24,
 };
 
 #define IFF_802_1Q_VLAN			IFF_802_1Q_VLAN
@@ -1253,6 +1255,8 @@ enum netdev_priv_flags {
 #define IFF_LIVE_ADDR_CHANGE		IFF_LIVE_ADDR_CHANGE
 #define IFF_MACVLAN			IFF_MACVLAN
 #define IFF_XMIT_DST_RELEASE_PERM	IFF_XMIT_DST_RELEASE_PERM
+#define IFF_IPVLAN_MASTER		IFF_IPVLAN_MASTER
+#define IFF_IPVLAN_SLAVE		IFF_IPVLAN_SLAVE
 
 /**
  *	struct net_device - The DEVICE structure.
include/uapi/linux/if_link.h +15 -0
index 7072d8325016..36bddc233633 100644
@@ -330,6 +330,21 @@ enum macvlan_macaddr_mode {
 
 #define MACVLAN_FLAG_NOPROMISC	1
 
+/* IPVLAN section */
+enum {
+	IFLA_IPVLAN_UNSPEC,
+	IFLA_IPVLAN_MODE,
+	__IFLA_IPVLAN_MAX
+};
+
+#define IFLA_IPVLAN_MAX (__IFLA_IPVLAN_MAX - 1)
+
+enum ipvlan_mode {
+	IPVLAN_MODE_L2 = 0,
+	IPVLAN_MODE_L3,
+	IPVLAN_MODE_MAX
+};
+
 /* VXLAN section */
 enum {
 	IFLA_VXLAN_UNSPEC,
Signature · 1 line
2.1.0.rc2.206.gedb03e5
maintainer
#1 lore raw

From: Mahesh Bandewar <[email protected]>
Date: Sun, 16 Nov 2014 22:27:14 -0800

Quoted 6 lines
drivers/net/ipvlan/ipvlan.h new file +6 -0
+/* Define IPVL_DEBUG and set the appropriate dbg_level for debugging. */
+#ifdef	IPVL_DEBUG
+/*
+ * 1 : non-datapath debugging
+ * 2 : Custom
+ * 3 : function enters and exists.

drivers/net/ipvlan/ipvlan.h line 56

Quoted 10 lines
drivers/net/ipvlan/ipvlan.h new file +10 -0
+ * 4 : printk in data path (be careful!)
+ */
+#define IPVL_DBG_LEVEL 1
+#define ipvlan_dbg(level, msg...)	do { \
+						if (level <= IPVL_DBG_LEVEL) \
+						printk(KERN_DEBUG msg); \
+					} while (0)
+#else
+#define ipvlan_dbg(level, msg...) do { ; } while (0)
+#endif

The day of having code use custom local debug logging facilities is long gone, please use a standard mechanism for this rather than home cooked reimplementations.

drivers/net/ipvlan/ipvlan.h line 138

Quoted 2 lines
drivers/net/ipvlan/ipvlan.h new file +2 -0
+/* ---- Prototype declarations ---- */
+/* ---- ipvlan_main.c ---- */

Everone knows these are prototype declarations by virtue of them being functions declarations in a header file.

Anyone who wants to know where these are defined can run grep.

So these comments are superfluous, please remove them across this entire submission.

drivers/net/ipvlan/ipvlan_core.c line 52

Quoted 3 lines
drivers/net/ipvlan/ipvlan_core.c new file +3 -0
+	const struct in_addr *ip4_addr = iaddr;
+	return jhash_1word(ip4_addr->s_addr, ipvlan_jhash_secret)
+	       & IPVLAN_HASH_MASK;

When an expression spans multiple lines, lines end rather than begin with an operator.

Review

Quoted 2 lines
(quoted diff) +2 -0
+		if (is_v6 && addr->atype == IPVL_IPV6 &&
+			ipv6_addr_equal(&addr->ip6addr, iaddr))

For multi-line if() statements, the second line and subsequent lines must begine exactly at the first column after the openning parenthesis of the first line.

If you are purely using TAB characters to indent, you are likely doing it wrong.

drivers/net/ipvlan/ipvlan_core.c line 100

Quoted 4 lines
drivers/net/ipvlan/ipvlan_core.c new file +4 -0
+		if ((is_v6 && addr->atype == IPVL_IPV6 &&
+		     ipv6_addr_equal(&addr->ip6addr, iaddr))
+		   || (!is_v6 && addr->atype == IPVL_IPV4 &&
+		      addr->ip4addr.s_addr == ((struct in_addr *)iaddr)->s_addr)

Lines end, not begin, with operators.

drivers/net/ipvlan/ipvlan_sysfs.c

Quoted 2 lines
drivers/net/ipvlan/ipvlan_sysfs.c new file +0 -0

Please do not add sysfs configuration knobs, netlink is sufficient.

#2 lore raw
Quoted 16 lines · David Miller

From: Mahesh Bandewar <[email protected]>
Date: Sun, 16 Nov 2014 22:27:14 -0800

Quoted 12 lines
drivers/net/ipvlan/ipvlan.h new file +12 -0
+/* Define IPVL_DEBUG and set the appropriate dbg_level for debugging. */
+#ifdef       IPVL_DEBUG
+/*
+ * 1 : non-datapath debugging
+ * 2 : Custom
+ * 3 : function enters and exists.
+ * 4 : printk in data path (be careful!)
+ */
+#define IPVL_DBG_LEVEL 1
+#define ipvlan_dbg(level, msg...)    do { \
+                                             if (level <= IPVL_DBG_LEVEL) \
+                                             printk(KERN_DEBUG msg); \
Quoted 9 lines

drivers/net/ipvlan/ipvlan.h line 56

Quoted 4 lines
drivers/net/ipvlan/ipvlan.h new file +4 -0
+                                     } while (0)
+#else
+#define ipvlan_dbg(level, msg...) do { ; } while (0)
+#endif

The day of having code use custom local debug logging facilities is long gone, please use a standard mechanism for this rather than home cooked reimplementations.

Do you mean pr_err() / pr_warn() / pr_debug() etc.? or something else?

Quoted 32 lines

drivers/net/ipvlan/ipvlan.h line 138

Quoted 2 lines
drivers/net/ipvlan/ipvlan.h new file +2 -0
+/* ---- Prototype declarations ---- */
+/* ---- ipvlan_main.c ---- */

Everone knows these are prototype declarations by virtue of them being functions declarations in a header file.

Anyone who wants to know where these are defined can run grep.

So these comments are superfluous, please remove them across this entire submission.

drivers/net/ipvlan/ipvlan_core.c line 52

Quoted 3 lines
drivers/net/ipvlan/ipvlan_core.c new file +3 -0
+     const struct in_addr *ip4_addr = iaddr;
+     return jhash_1word(ip4_addr->s_addr, ipvlan_jhash_secret)
+            & IPVLAN_HASH_MASK;

When an expression spans multiple lines, lines end rather than begin with an operator.

Review

Quoted 2 lines
(quoted diff) +2 -0
+             if (is_v6 && addr->atype == IPVL_IPV6 &&
+                     ipv6_addr_equal(&addr->ip6addr, iaddr))

For multi-line if() statements, the second line and subsequent lines must begine exactly at the first column after the openning parenthesis of the first line.

If you are purely using TAB characters to indent, you are likely doing it wrong.

drivers/net/ipvlan/ipvlan_core.c line 100

Quoted 4 lines
drivers/net/ipvlan/ipvlan_core.c new file +4 -0
+             if ((is_v6 && addr->atype == IPVL_IPV6 &&
+                  ipv6_addr_equal(&addr->ip6addr, iaddr))
+                || (!is_v6 && addr->atype == IPVL_IPV4 &&
+                   addr->ip4addr.s_addr == ((struct in_addr *)iaddr)->s_addr)
Quoted 6 lines

Lines end, not begin, with operators.

drivers/net/ipvlan/ipvlan_sysfs.c

Quoted 2 lines
drivers/net/ipvlan/ipvlan_sysfs.c new file +0 -0

Please do not add sysfs configuration knobs, netlink is sufficient.

Having it does neither add complexity nor too much code, wo why not? May be I'm missing something! But I was think that this might provide foundation for adding other things like config locking from master's namespace etc. (discussed in the thread) in the future.

Thanks,
--mahesh..

maintainer
#3 lore raw

From: Mahesh Bandewar <[email protected]>
Date: Tue, 18 Nov 2014 21:27:48 -0800

Quoted 26 lines · David Miller
Quoted 16 lines

From: Mahesh Bandewar <[email protected]>
Date: Sun, 16 Nov 2014 22:27:14 -0800

Quoted 12 lines
drivers/net/ipvlan/ipvlan.h new file +12 -0
+/* Define IPVL_DEBUG and set the appropriate dbg_level for debugging. */
+#ifdef       IPVL_DEBUG
+/*
+ * 1 : non-datapath debugging
+ * 2 : Custom
+ * 3 : function enters and exists.
+ * 4 : printk in data path (be careful!)
+ */
+#define IPVL_DBG_LEVEL 1
+#define ipvlan_dbg(level, msg...)    do { \
+                                             if (level <= IPVL_DBG_LEVEL) \
+                                             printk(KERN_DEBUG msg); \
Quoted 9 lines

drivers/net/ipvlan/ipvlan.h line 56

Quoted 4 lines
drivers/net/ipvlan/ipvlan.h new file +4 -0
+                                     } while (0)
+#else
+#define ipvlan_dbg(level, msg...) do { ; } while (0)
+#endif

The day of having code use custom local debug logging facilities is long gone, please use a standard mechanism for this rather than home cooked reimplementations.

Quoted 1 line

Do you mean pr_err() / pr_warn() / pr_debug() etc.? or something else?

And netdev_dbg(), etc.

And about sysfs, we're not having two ways to do the same exact thing. Whatever functionality you want to provide, implement it via netlink and that's the end of it.

#4 lore raw
In reply to Mahesh Bandewar
Quoted 26 lines · Mahesh Bandewar
Quoted 16 lines · David Miller

From: Mahesh Bandewar <[email protected]>
Date: Sun, 16 Nov 2014 22:27:14 -0800

Quoted 12 lines
drivers/net/ipvlan/ipvlan.h new file +12 -0
+/* Define IPVL_DEBUG and set the appropriate dbg_level for debugging. */
+#ifdef       IPVL_DEBUG
+/*
+ * 1 : non-datapath debugging
+ * 2 : Custom
+ * 3 : function enters and exists.
+ * 4 : printk in data path (be careful!)
+ */
+#define IPVL_DBG_LEVEL 1
+#define ipvlan_dbg(level, msg...)    do { \
+                                             if (level <= IPVL_DBG_LEVEL) \
+                                             printk(KERN_DEBUG msg); \
Quoted 9 lines

drivers/net/ipvlan/ipvlan.h line 56

Quoted 4 lines
drivers/net/ipvlan/ipvlan.h new file +4 -0
+                                     } while (0)
+#else
+#define ipvlan_dbg(level, msg...) do { ; } while (0)
+#endif

The day of having code use custom local debug logging facilities is long gone, please use a standard mechanism for this rather than home cooked reimplementations.

Quoted 1 line

Do you mean pr_err() / pr_warn() / pr_debug() etc.? or something else?

[...]

There are also netdev_err() etc. for logging with a net device as context.

You can use the netif_err(), netif_dbg() etc. functions and implement the ethtool {get,set}_msglevel operations to allow turning on and off broad classes of log messages.

Finally, if CONFIG_DYNAMIC_DEBUG is enabled then debug logging messages can be turned on and off at run-time (individually or by function, file or module).

Ben.

Signature · 3 lines
Ben Hutchings
Beware of bugs in the above code;
I have only proved it correct, not tried it. - Donald Knuth
#5 lore raw
Quoted 33 lines · Ben Hutchings
Quoted 26 lines · Mahesh Bandewar
Quoted 16 lines · David Miller

From: Mahesh Bandewar <[email protected]>
Date: Sun, 16 Nov 2014 22:27:14 -0800

Quoted 12 lines
drivers/net/ipvlan/ipvlan.h new file +12 -0
+/* Define IPVL_DEBUG and set the appropriate dbg_level for debugging. */
+#ifdef       IPVL_DEBUG
+/*
+ * 1 : non-datapath debugging
+ * 2 : Custom
+ * 3 : function enters and exists.
+ * 4 : printk in data path (be careful!)
+ */
+#define IPVL_DBG_LEVEL 1
+#define ipvlan_dbg(level, msg...)    do { \
+                                             if (level <= IPVL_DBG_LEVEL) \
+                                             printk(KERN_DEBUG msg); \
Quoted 9 lines

drivers/net/ipvlan/ipvlan.h line 56

Quoted 4 lines
drivers/net/ipvlan/ipvlan.h new file +4 -0
+                                     } while (0)
+#else
+#define ipvlan_dbg(level, msg...) do { ; } while (0)
+#endif

The day of having code use custom local debug logging facilities is long gone, please use a standard mechanism for this rather than home cooked reimplementations.

Quoted 1 line

Do you mean pr_err() / pr_warn() / pr_debug() etc.? or something else?

[...]

There are also netdev_err() etc. for logging with a net device as context.

Quoted 8 lines

You can use the netif_err(), netif_dbg() etc. functions and implement the ethtool {get,set}_msglevel operations to allow turning on and off broad classes of log messages.

Finally, if CONFIG_DYNAMIC_DEBUG is enabled then debug logging messages can be turned on and off at run-time (individually or by function, file or module).

Thanks Ben, I'll try using netif_dbg().

Quoted 6 lines

Ben.

--
Ben Hutchings
Beware of bugs in the above code;
I have only proved it correct, not tried it. - Donald Knuth