Skip to contentSkip to conversation

ipvlan: Initial check-in of the IPVLAN driver.

PATCH Superseded Mahesh Bandewar 2 participants 2 messages Nov 24, 2014

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 | 108 +++++
 drivers/net/Kconfig                 |  18 +
 drivers/net/Makefile                |   1 +
 drivers/net/ipvlan/Makefile         |   7 +
 drivers/net/ipvlan/ipvlan.h         | 131 ++++++
 drivers/net/ipvlan/ipvlan_core.c    | 629 ++++++++++++++++++++++++++++
 drivers/net/ipvlan/ipvlan_main.c    | 815 ++++++++++++++++++++++++++++++++++++
 include/linux/netdevice.h           |   4 +
 include/uapi/linux/if_link.h        |  15 +
 9 files changed, 1728 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
Documentation/networking/ipvlan.txt new file +108 -0
new file mode 100644
index 000000000000..3f17bcb0ddc6
@@ -0,0 +1,108 @@
+
+                            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. 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.
+
+
+6. 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..df79910192d6
@@ -0,0 +1,7 @@
+#
+# Makefile for the Ethernet Ipvlan driver
+#
+
+obj-$(CONFIG_IPVLAN) += ipvlan.o
+
+ipvlan-objs := ipvlan_core.o ipvlan_main.o
drivers/net/ipvlan/ipvlan.h new file +131 -0

131 lines not on this page · raw message

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

629 lines not on this page · raw message

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

815 lines not on this page · raw message

include/linux/netdevice.h +4 -0
index 5cd508787572..2cb772495f7a 100644
@@ -1230,6 +1230,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
@@ -1255,6 +1257,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, 23 Nov 2014 17:19:14 -0800

drivers/net/ipvlan/ipvlan_core.c line 52

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

Please put an empty line between variable declarations and code.

drivers/net/ipvlan/ipvlan_core.c line 178

Quoted 6 lines
drivers/net/ipvlan/ipvlan_core.c new file +6 -0
+unsigned int ipvlan_mac_hash(const unsigned char *addr)
+{
+	u32 hash = jhash_1word(__get_unaligned_cpu32(addr+2),
+			       ipvlan_jhash_secret);
+	return hash & IPVLAN_MAC_FILTER_MASK;
+}

Likewise.

drivers/net/ipvlan/ipvlan_core.c line 290

Quoted 4 lines
drivers/net/ipvlan/ipvlan_core.c new file +4 -0
+	} else if (addr_type == IPVL_ICMPV6) {
+		struct nd_msg *ndmh;
+		struct in6_addr *i6addr;
+		ndmh = (struct nd_msg *)lyr3h;

Likewise.

drivers/net/ipvlan/ipvlan_core.c line 475

Quoted 6 lines
drivers/net/ipvlan/ipvlan_core.c new file +6 -0
+	if (ether_addr_equal(eth->h_dest, eth->h_source)) {
+		netif_dbg(ipvlan, pktdata, dev,
+			  "Comm betn 2 virt devs PROT=%x\n",
+			  ntohs(skb->protocol));
+		if ((lyr3h = ipvlan_get_L3_hdr(skb, &addr_type)) == NULL)
+			goto to_default;

Please don't mix assignments and if() statement tests, put the assignment by itself first, then test the variable.

Althought I'm not fundamentally against goto statements, it doesn't help in any way here, and this code reads must easier as:

		if (lyr3h) {
			addr = ipvlan_addr_lookup(...);
			if (addr)
				return ipvlan_rcv_frame(...);
		}

drivers/net/ipvlan/ipvlan_core.c line 482

Quoted 2 lines
drivers/net/ipvlan/ipvlan_core.c new file +2 -0
+		/* No matching ipvlan dev! Must be on the Physical device */
+to_default:

And then this label and even the comment above it can just be deleted.

drivers/net/ipvlan/ipvlan_core.c line 497

Quoted 4 lines
drivers/net/ipvlan/ipvlan_core.c new file +4 -0
+	} else if (is_multicast_ether_addr(eth->h_dest)) {
+		u8 ip_summed = skb->ip_summed;
+		/* Packet needs to be multicast-ed. */
+		skb->ip_summed = CHECKSUM_UNNECESSARY;

Empty line between variable declarations and code, please.

And I think because we're testing against a mutlicast address, you don't have to explicitly state that this is "the multicast code path."

Use comments when it truly adds information, rather than basically restating what the code is already explicitly saying to the reader.

Most of your comments in this driver just recap the code statements in one way or another.

Quoted 10 lines
drivers/net/ipvlan/ipvlan_core.c new file +10 -0
+int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+	struct ipvl_dev *ipvlan = netdev_priv(dev);
+	struct ipvl_port *port = ipvlan_port_get_rcu(ipvlan->phy_dev);
+
+	if (!port)
+		goto out;
+
+	if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
+		goto out;

drivers/net/ipvlan/ipvlan_core.c line 529

Quoted 10 lines
drivers/net/ipvlan/ipvlan_core.c new file +10 -0
+
+	switch(port->mode) {
+	case IPVLAN_MODE_L2:
+		return ipvlan_xmit_mode_l2(skb, dev);
+	case IPVLAN_MODE_L3:
+		return ipvlan_xmit_mode_l3(skb, dev);
+	}
+
+	/* Should not reach here */
+	BUG();

Only use BUG statements when the kernel cannot continue operating safely. Just because your own personal assertion is violated doesn't mean the whole machine needs to be taken down.

Free the packet and emit a log message (perhaps with WARN()).

Review

Quoted 2 lines
(quoted diff) +2 -0
+		if ((lyr3h = ipvlan_get_L3_hdr(skb, &addr_type)) == NULL)
+			return true;

Change this to:

		lyr3h = ipvlan_get_L3_hdr(...);
		if (!lyr3h)
			return true;
Quoted 7 lines
drivers/net/ipvlan/ipvlan_core.c new file +7 -0
+rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb)
+{
+	struct sk_buff *skb = *pskb;
+	struct ipvl_port *port = ipvlan_port_get_rcu(skb->dev);
+
+	if (!port)
+		goto out;

drivers/net/ipvlan/ipvlan_core.c line 626

Quoted 10 lines
drivers/net/ipvlan/ipvlan_core.c new file +10 -0
+
+	switch (port->mode) {
+	case IPVLAN_MODE_L2:
+		return ipvlan_handle_mode_l2(pskb, port);
+	case IPVLAN_MODE_L3:
+		return ipvlan_handle_mode_l3(pskb, port);
+	}
+
+	/* Should not reach here */
+	BUG();

Again, use WARN() not BUG().

drivers/net/ipvlan/ipvlan_main.c line 43

Quoted 2 lines
drivers/net/ipvlan/ipvlan_main.c new file +2 -0
+	if ((port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL)) == NULL)
+		return -ENOMEM;

Change this to:

	port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
	if (!port)
		return -ENOMEM;

drivers/net/ipvlan/ipvlan_main.c line 150

Quoted 3 lines
drivers/net/ipvlan/ipvlan_main.c new file +3 -0
+		list_for_each_entry(addr, &ipvlan->addrs, anode) {
+			ipvlan_ht_addr_add(ipvlan, addr);
+		}

Single line basic blocks do not require enclosing braces, please remove them.

drivers/net/ipvlan/ipvlan_main.c line 169

Quoted 3 lines
drivers/net/ipvlan/ipvlan_main.c new file +3 -0
+		list_for_each_entry(addr, &ipvlan->addrs, anode) {
+			ipvlan_ht_addr_del(addr, !dev->dismantle);
+		}

Likewise.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	struct ipvl_dev *ipvlan = netdev_priv(dev);
+	return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);

Empty line between local variable declarations and code.

drivers/net/ipvlan/ipvlan_main.c line 223

Quoted 7 lines
drivers/net/ipvlan/ipvlan_main.c new file +7 -0
+	if (set && !test_bit(hashbit, ipvlan->mac_filters)) {
+		/* Set broadcast hash-bit (for IPv4) */
+		__set_bit(hashbit, ipvlan->mac_filters);
+	} else if (!set && test_bit(hashbit, ipvlan->mac_filters)) {
+		/* Reset broadcast hash-bit */
+		__clear_bit(hashbit, ipvlan->mac_filters);
+	}

Comments are just restating the code, remove them. And then these basic blocks are single line and thus you can remove all of the braces as well.

drivers/net/ipvlan/ipvlan_main.c line 239

Quoted 3 lines
drivers/net/ipvlan/ipvlan_main.c new file +3 -0
+		netdev_for_each_mc_addr(ha, dev) {
+			__set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
+		}

Single line basic block, toss the curly braces.

drivers/net/ipvlan/ipvlan_main.c line 268

Quoted 1 line
drivers/net/ipvlan/ipvlan_main.c new file +1 -0
+			} while(u64_stats_fetch_retry_irq(&pcptr->syncp, strt));

Space needed between 'while' and the openning parenthesis.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	const struct ipvl_dev *ipvlan = netdev_priv(dev);
+	return __ethtool_get_settings(ipvlan->phy_dev, cmd);

Empty line between variable declarations and code.

drivers/net/ipvlan/ipvlan_main.c line 388

Quoted 4 lines
drivers/net/ipvlan/ipvlan_main.c new file +4 -0
+	if (data && data[IFLA_IPVLAN_MODE]) {
+		u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
+		ipvlan_set_port_mode(port, nmode);
+	}

Likewise.

drivers/net/ipvlan/ipvlan_main.c line 597

Quoted 3 lines
drivers/net/ipvlan/ipvlan_main.c new file +3 -0
+		list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
+			ipvlan_adjust_mtu(ipvlan, dev);
+		}

Single line basic block doesn't need curly braces, please remove.

drivers/net/ipvlan/ipvlan_main.c line 620

Quoted 2 lines
drivers/net/ipvlan/ipvlan_main.c new file +2 -0
+	if ((addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC)) == NULL)
+		return -ENOMEM;

Change this to:

	addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
	if (!addr)
		return -ENOMEM;

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if ((addr = ipvlan_ht_addr_lookup(ipvlan->port, ip6_addr, true)) ==NULL)
+		return;

Same kind of transformation needed here.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	/* Delete from the hash-table */
+	ipvlan_ht_addr_del(addr, true);

Comment just restates the code, remove.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	/* Delete from the logical's addr list */
+	list_del_rcu(&addr->anode);

Likewise.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if ((addr= ipvlan_ht_addr_lookup(ipvlan->port, ip4_addr, false)) ==NULL)
+		return;

This really looks terrible, bad spacing and combining an assignment with an if() test. After reading the rest of my feedback above you should know what to do with this.

Finally, please think seriously about all of your *_dbg() logging in this driver. Does it really have true value after initial driver development and debug? They take up a significant amount of lines in the driver, and make it seem bigger than it really is.

Thanks.