Skip to contentSkip to conversation

Virtual ethernet tunnel (v.2)

PATCH In review Pavel Emelianov 5 participants 13 messages Jun 7, 2007 – Jun 11, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Virtual ethernet tunnel (v.2)

Veth stands for Virtual ETHernet. It is a simple tunnel driver that works at the link layer and looks like a pair of ethernet devices interconnected with each other.

Mainly it allows to communicate between network namespaces but it can be used as is as well.

Eric recently sent a similar driver called etun. This implementation uses another interface - the RTM_NRELINK message introduced by Patric.

The newlink callback is organized that way to make it easy to create the peer device in the separate namespace when we have them in kernel.

Changes from v.1:
* percpu statistics;
* standard convention for nla policy names;
* module alias added;
* xmit function fixes noticed by Patric;
* code cleanup.

The patch for an ip utility is also provided.

Since ethtool interface was taken from Eric's patch, I think that he would like to see his Signed-off line as well (however he didn't answer yesterday).

---

drivers/net/Kconfig +6 -0
index 7d57f4a..7e144be 100644
@@ -119,6 +119,12 @@ config TUN
 
 	  If you don't know what to use this for, you don't need it.
 
+config VETH
+	tristate "Virtual ethernet device"
+	---help---
+	  The device is an ethernet tunnel. Devices are created in pairs. When
+	  one end receives the packet it appears on its pair and vice versa.
+
 config NET_SB1000
 	tristate "General Instruments Surfboard 1000"
 	depends on PNP
drivers/net/Makefile +1 -0
index a77affa..4764119 100644
@@ -185,6 +185,7 @@ obj-$(CONFIG_MACSONIC) += macsonic.o
 obj-$(CONFIG_MACMACE) += macmace.o
 obj-$(CONFIG_MAC89x0) += mac89x0.o
 obj-$(CONFIG_TUN) += tun.o
+obj-$(CONFIG_VETH) += veth.o
 obj-$(CONFIG_NET_NETX) += netx-eth.o
 obj-$(CONFIG_DL2K) += dl2k.o
 obj-$(CONFIG_R8169) += r8169.o
drivers/net/veth.c new file +442 -0

442 lines not on this page · raw message

include/net/veth.h new file +15 -0
new file mode 100644
index 0000000..d52e0c5
@@ -0,0 +1,15 @@
+#ifndef __NET_VETH_H__
+#define __NET_VETH_H__
+
+enum {
+	VETH_INFO_UNSPEC,
+	VETH_INFO_MAC,
+	VETH_INFO_PEER,
+	VETH_INFO_PEER_MAC,
+
+	__VETH_INFO_MAX
+};
+
+#define VETH_INFO_MAX	(__VETH_INFO_MAX - 1)
+
+#endif
#1 lore raw

PATCH 1/1 Module for ip utility to support veth device (v.2)

The usage is

# ip link add [name] type veth [peer <name>] [mac <mac>] [peer_mac <mac>]

This version doesn't include the fix for ip/iplink.c as Patrick said that he had included it into his patches already.

---

ip/Makefile +5 -1
index 9a5bfe3..b46bce3 100644
@@ -8,8 +8,9 @@ RTMONOBJ=rtmon.o
 ALLOBJ=$(IPOBJ) $(RTMONOBJ)
 SCRIPTS=ifcfg rtpr routel routef
 TARGETS=ip rtmon
+LIBS=link_veth.so
 
-all: $(TARGETS) $(SCRIPTS)
+all: $(TARGETS) $(SCRIPTS) $(LIBS)
 
 ip: $(IPOBJ) $(LIBNETLINK) $(LIBUTIL)
 
@@ -24,3 +25,6 @@ clean:
 
 LDLIBS	+= -ldl
 LDFLAGS	+= -Wl,-export-dynamic
+
+%.so: %.c
+	$(CC) $(CFLAGS) -shared $< -o $@
ip/link_veth.c new file +86 -0

86 lines not on this page · raw message

ip/veth.h new file +15 -0
new file mode 100644
index 0000000..d52e0c5
@@ -0,0 +1,15 @@
+#ifndef __NET_VETH_H__
+#define __NET_VETH_H__
+
+enum {
+	VETH_INFO_UNSPEC,
+	VETH_INFO_MAC,
+	VETH_INFO_PEER,
+	VETH_INFO_PEER_MAC,
+
+	__VETH_INFO_MAX
+};
+
+#define VETH_INFO_MAX	(__VETH_INFO_MAX - 1)
+
+#endif
#2 lore raw

Re: [PATCH] Module for ip utility to support veth device (v.2)

Quoted 7 lines · On Thu, 07 Jun 2007 15:16:34 +0400 Pavel Emelianov <[email protected]> wrote:

The usage is

# ip link add [name] type veth [peer <name>] [mac <mac>] [peer_mac <mac>]

This version doesn't include the fix for ip/iplink.c as Patrick said that he had included it into his patches already.

If this goes in to the mainline kernel, I'll add it to iproute2. Don't want to put anything in now because of possible interface changes.

Signature · 1 line
Stephen Hemminger <[email protected]>
#3 lore raw
In reply to Pavel Emelianov
Quoted 4 lines · Pavel Emelianov

Veth stands for Virtual ETHernet. It is a simple tunnel driver that works at the link layer and looks like a pair of ethernet devices interconnected with each other.

As Dave mentioned, there is already a driver known as 'veth'. Maybe borrow the etun name as well?

I would also like some way to identify veth from other device types, preferably
something like a value in sysfs. However, that should not hold up consideration of
this patch, and I am willing to submit a patch after this goes in to add the functionality I want...

Quoted 11 lines
drivers/net/veth.c new file +10 -0
+/*
+ * xmit
+ */
+
+static int veth_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+	struct net_device *rcv = NULL;
+	struct veth_device_stats *stats;
+	struct veth_priv *priv, *rcv_priv;
+	int length, cpu;

drivers/net/veth.c line 161

Quoted 10 lines
drivers/net/veth.c new file +10 -0
+
+	skb_orphan(skb);
+
+	priv = netdev_priv(dev);
+	cpu = smp_processor_id();
+	stats = per_cpu_ptr(priv->stats, cpu);
+	rcv = priv->peer;
+
+	if (!(rcv->flags & IFF_UP))
+		goto outf;

I think you need at least the option to zero out the time-stamp, otherwise it will not be re-calculated when received on the peer, and it potentially spent significant time since it was last calculated (think netem delay or similar).

+ /* Zero out the time-stamp so that receiving code is forced
+ * to recalculate it.
+ */
+ skb->tstamp.off_sec = 0;
+ skb->tstamp.off_usec = 0;

Quoted 5 lines
drivers/net/veth.c new file +4 -0
+
+	rcv_priv = netdev_priv(rcv);
+	skb->pkt_type = PACKET_HOST;
+	skb->protocol = eth_type_trans(skb, rcv);

drivers/net/veth.c line 175

Quoted 10 lines
drivers/net/veth.c new file +10 -0
+	if (dev->features & NETIF_F_NO_CSUM)
+		skb->ip_summed = rcv_priv->ip_summed;
+
+	dst_release(skb->dst);
+	skb->dst = NULL;
+	secpath_reset(skb);
+	nf_reset(skb);
+	skb->mark = 0;
+
+	length = skb->len;

This should be done before you do the eth_type_trans, as that pulls the header and your byte counters will be off.

Quoted 8 lines
drivers/net/veth.c new file +6 -0
+
+	stats->tx_bytes += length;
+	stats->tx_packets++;
 
+
+	stats = per_cpu_ptr(rcv_priv->stats, cpu);
+	stats->rx_bytes += length;

drivers/net/veth.c line 191

Quoted 10 lines
drivers/net/veth.c new file +10 -0
+	stats->rx_packets++;
+
+	netif_rx(skb);
+	return 0;
+
+outf:
+	kfree_skb(skb);
+	stats->tx_dropped++;
+	return 0;
+}

Thanks,
Ben

Signature · 1 line
Ben Greear <[email protected]> Candela Technologies Inc  http://www.candelatech.com
#4 lore raw
Quoted 7 lines · Ben Greear
Quoted 4 lines · Pavel Emelianov

Veth stands for Virtual ETHernet. It is a simple tunnel driver that works at the link layer and looks like a pair of ethernet devices interconnected with each other.

As Dave mentioned, there is already a driver known as 'veth'. Maybe borrow the etun name as well?

We have already seen that this driver uses ethXXX names for its devices and Dave agreed with veth one. Moreover Alexey Kuznetsov said that he would prefer the name veth for etun.

Quoted 3 lines

I would also like some way to identify veth from other device types, preferably
something like a value in sysfs. However, that should not hold up

We can do this with ethtool. It can get and print the driver name of the device.

Quoted 4 lines

consideration of
this patch, and I am willing to submit a patch after this goes in to add the functionality
I want...

Ok. Thanks.

Quoted 43 lines
Quoted 11 lines
drivers/net/veth.c new file +10 -0
+/*
+ * xmit
+ */
+
+static int veth_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+    struct net_device *rcv = NULL;
+    struct veth_device_stats *stats;
+    struct veth_priv *priv, *rcv_priv;
+    int length, cpu;

drivers/net/veth.c line 161

Quoted 10 lines
drivers/net/veth.c new file +10 -0
+
+    skb_orphan(skb);
+
+    priv = netdev_priv(dev);
+    cpu = smp_processor_id();
+    stats = per_cpu_ptr(priv->stats, cpu);
+    rcv = priv->peer;
+
+    if (!(rcv->flags & IFF_UP))
+        goto outf;

I think you need at least the option to zero out the time-stamp, otherwise it will
not be re-calculated when received on the peer, and it potentially spent significant
time since it was last calculated (think netem delay or similar).

(quoted diff) +5 -0
+        /* Zero out the time-stamp so that receiving code is forced
+         * to recalculate it.
+         */
+        skb->tstamp.off_sec = 0;
+        skb->tstamp.off_usec = 0;
Quoted 11 lines
drivers/net/veth.c new file +10 -0
+
+    rcv_priv = netdev_priv(rcv);
+    skb->pkt_type = PACKET_HOST;
+    skb->protocol = eth_type_trans(skb, rcv);
+    if (dev->features & NETIF_F_NO_CSUM)
+        skb->ip_summed = rcv_priv->ip_summed;
+
+    dst_release(skb->dst);
+    skb->dst = NULL;
+    secpath_reset(skb);
Quoted 8 lines

drivers/net/veth.c line 175

Quoted 5 lines
drivers/net/veth.c new file +4 -0
+    nf_reset(skb);
+    skb->mark = 0;
+
+    length = skb->len;

This should be done before you do the eth_type_trans, as that pulls the header and your
byte counters will be off.

This will be ETH_HLEN larger, do you mean this? I think this is normal as this device tries to look like an "iron" ethernet card :)

Quoted 20 lines
Quoted 7 lines
drivers/net/veth.c new file +5 -0
+
+    stats->tx_bytes += length;
+    stats->tx_packets++;
  +
+    stats = per_cpu_ptr(rcv_priv->stats, cpu);
+    stats->rx_bytes += length;

drivers/net/veth.c line 191

Quoted 10 lines
drivers/net/veth.c new file +10 -0
+    stats->rx_packets++;
+
+    netif_rx(skb);
+    return 0;
+
+outf:
+    kfree_skb(skb);
+    stats->tx_dropped++;
+    return 0;
+}

Thanks,
Ben

#5 lore raw
Quoted 9 lines · Pavel Emelianov
Quoted 7 lines · Ben Greear
Quoted 5 lines · Pavel Emelianov

Veth stands for Virtual ETHernet. It is a simple tunnel driver that works at the link layer and looks like a pair of ethernet devices interconnected with each other.

Quoted 8 lines
Quoted 3 lines

As Dave mentioned, there is already a driver known as 'veth'. Maybe borrow the etun name as well?

We have already seen that this driver uses ethXXX names for its devices and Dave agreed with veth one. Moreover Alexey Kuznetsov said that he would prefer the name veth for etun.

Ok, fine by me. I started reading mail from the wrong direction this morning :)

Quoted 8 lines
Quoted 5 lines

I would also like some way to identify veth from other device types, preferably
something like a value in sysfs. However, that should not hold up

We can do this with ethtool. It can get and print the driver name of the device.

I think I'd like something in sysfs that we could query for any interface. Possible return strings could be:
VLAN
VETH
ETH
PPP
BRIDGE
AP /* wifi access point interface */
STA /* wifi station */
....

I will cook up a patch for consideration after veth goes in.

Quoted 34 lines
Quoted 24 lines

I think you need at least the option to zero out the time-stamp, otherwise it will
not be re-calculated when received on the peer, and it potentially spent significant
time since it was last calculated (think netem delay or similar).

(quoted diff) +5 -0
+        /* Zero out the time-stamp so that receiving code is forced
+         * to recalculate it.
+         */
+        skb->tstamp.off_sec = 0;
+        skb->tstamp.off_usec = 0;
Quoted 13 lines
drivers/net/veth.c new file +11 -0
+
+    rcv_priv = netdev_priv(rcv);
+    skb->pkt_type = PACKET_HOST;
+    skb->protocol = eth_type_trans(skb, rcv);
+    if (dev->features & NETIF_F_NO_CSUM)
+        skb->ip_summed = rcv_priv->ip_summed;
+
+    dst_release(skb->dst);
+    skb->dst = NULL;
+    secpath_reset(skb);
+    nf_reset(skb);
Quoted 9 lines

drivers/net/veth.c line 175

Quoted 5 lines
drivers/net/veth.c new file +3 -0
+    skb->mark = 0;
+
+    length = skb->len;

This should be done before you do the eth_type_trans, as that pulls the header and your
byte counters will be off.

Quoted 3 lines

This will be ETH_HLEN larger, do you mean this? I think this is normal as this device tries to look like an "iron" ethernet card :)

For device counters, it should count the number of bytes received, including all headers,
but excluding the ethernet FCS. If an 'iron' card did differently, I'd consider it a bug.

Thanks,
Ben

Signature · 1 line
Ben Greear <[email protected]> Candela Technologies Inc  http://www.candelatech.com
#6 lore raw
Quoted 41 lines · Ben Greear
Quoted 8 lines · Pavel Emelianov
Quoted 6 lines · Ben Greear
Quoted 4 lines · Pavel Emelianov

Veth stands for Virtual ETHernet. It is a simple tunnel driver that works at the link layer and looks like a pair of ethernet devices interconnected with each other.

Quoted 9 lines
Quoted 4 lines

As Dave mentioned, there is already a driver known as 'veth'. Maybe borrow
the etun name as well?

We have already seen that this driver uses ethXXX names for its devices and Dave agreed with veth one. Moreover Alexey Kuznetsov said that he would prefer the name veth for etun.

Ok, fine by me. I started reading mail from the wrong direction this morning :)

Quoted 9 lines
Quoted 5 lines

I would also like some way to identify veth from other device types, preferably
something like a value in sysfs. However, that should not hold up

We can do this with ethtool. It can get and print the driver name of the device.

I think I'd like something in sysfs that we could query for any interface. Possible return
strings could be:
VLAN
VETH
ETH
PPP
BRIDGE
AP /* wifi access point interface */
STA /* wifi station */
....

Quoted 1 line

I will cook up a patch for consideration after veth goes in.

OK.

Quoted 33 lines
Quoted 33 lines
Quoted 32 lines

I think you need at least the option to zero out the time-stamp, otherwise it will
not be re-calculated when received on the peer, and it potentially spent significant
time since it was last calculated (think netem delay or similar).

(quoted diff) +5 -0
+        /* Zero out the time-stamp so that receiving code is forced
+         * to recalculate it.
+         */
+        skb->tstamp.off_sec = 0;
+        skb->tstamp.off_usec = 0;
Quoted 7 lines
drivers/net/veth.c new file +4 -0
+
+    rcv_priv = netdev_priv(rcv);
+    skb->pkt_type = PACKET_HOST;
+    skb->protocol = eth_type_trans(skb, rcv);

drivers/net/veth.c line 175

Quoted 10 lines
drivers/net/veth.c new file +10 -0
+    if (dev->features & NETIF_F_NO_CSUM)
+        skb->ip_summed = rcv_priv->ip_summed;
+
+    dst_release(skb->dst);
+    skb->dst = NULL;
+    secpath_reset(skb);
+    nf_reset(skb);
+    skb->mark = 0;
+
+    length = skb->len;

This should be done before you do the eth_type_trans, as that pulls the header and your
byte counters will be off.

Quoted 7 lines
Quoted 3 lines

This will be ETH_HLEN larger, do you mean this? I think this is normal as this device tries to look like an "iron" ethernet card :)

For device counters, it should count the number of bytes received, including all headers,
but excluding the ethernet FCS. If an 'iron' card did differently, I'd
consider it a bug.

Hmm... The loopback must be doing bad things then. It first calls eth_type_trans and then accounts for the new skb->len.

Quoted 3 lines

Thanks,
Ben

#7 lore raw
Quoted 2 lines · Pavel Emelianov

Hmm... The loopback must be doing bad things then. It first calls eth_type_trans and then accounts for the new skb->len.

Perhaps it should be changed. e100 calculates the entire frame as far as I can tell, and e1000 and tg3 do it in hardware (not sure what all they are counting, but I *think* it includes the header...)

VLANs calculate before pulling it's header, though the ethernet header has already been pulled by the time VLAN sees the skb.

Thanks,
Ben

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com
#8 lore raw
In reply to Ben Greear

Ben Greear wrote:

[snip]

Quoted 20 lines
Quoted 8 lines
Quoted 4 lines

I would also like some way to identify veth from other device types, preferably
something like a value in sysfs. However, that should not hold up

We can do this with ethtool. It can get and print the driver name of the device.

I think I'd like something in sysfs that we could query for any interface. Possible return
strings could be:
VLAN
VETH
ETH
PPP
BRIDGE
AP /* wifi access point interface */
STA /* wifi station */
....

Quoted 2 lines

I will cook up a patch for consideration after veth goes in.

Ben, could you please tell what sysfs features do you plan to implement?

Thanks,
Pavel

#9 lore raw
Quoted 26 lines · Pavel Emelianov

Ben Greear wrote:

[snip]

Quoted 21 lines
Quoted 9 lines
Quoted 5 lines

I would also like some way to identify veth from other device types, preferably
something like a value in sysfs. However, that should not hold up

We can do this with ethtool. It can get and print the driver name of the device.

I think I'd like something in sysfs that we could query for any interface. Possible return
strings could be:
VLAN
VETH
ETH
PPP
BRIDGE
AP /* wifi access point interface */
STA /* wifi station */
....

Quoted 7 lines
Quoted 3 lines

I will cook up a patch for consideration after veth goes in.

Ben, could you please tell what sysfs features do you plan to implement?

I think this is the only thing that has a chance of getting into the kernel. Basically, I have a user-space app and I want to be able to definitively know the type for all interfaces. Currently, I have a hodge-podge of logic to query various ioctls and /proc files and finally, guess by name if nothing else works. There must be a better way :P

I have another sysfs patch that allows setting a default skb->mark for an interface so that you can set the skb->mark before it hits the connection tracking logic, but I'm been told this one has very little chance
of getting into the kernel. The skb->mark patch is only useful (as far as I can tell) if you
also include a patch Patrick McHardy did for me that allowed the conn-tracking logic to
use skb->mark as part of it's tuple. This allows me to do NAT between virtual routers
(routing tables) on the same machine using veth-equivalent drivers to connect the
routers. He thinks this will probably not ever get into the kernel either.

I have another sysctl related send-to-self patch that also has little chance of getting into the kernel, but it might be quite useful with veth (it's useful to me..but my needs aren't exactly mainstream :)) I'll post this separately for consideration....

Thanks,
Ben

Signature · 1 line
Ben Greear <[email protected]> Candela Technologies Inc  http://www.candelatech.com
#10 lore raw
Quoted 5 lines · Ben Greear

I have another sysfs patch that allows setting a default skb->mark for an interface so that you can set the skb->mark
before it hits the connection tracking logic, but I'm been told this one has very little chance
of getting into the kernel. The skb->mark patch is only useful (as far

Quoted 8 lines

as I can tell) if you
also include a patch Patrick McHardy did for me that allowed the conn-tracking logic to
use skb->mark as part of it's tuple. This allows me to do NAT between virtual routers
(routing tables) on the same machine using veth-equivalent drivers to connect the
routers. He thinks this will probably not ever get into the kernel either.

Are these patches available somewhere? I'm currently doing NAT between virtual routers by some advanced iproute2/iptables trickery, but I have no way to handle the occasional tuple conflict.

Regards,
Carl-Daniel

#11 lore raw
Quoted 15 lines · Carl-Daniel Hailfinger
Quoted 5 lines · Ben Greear

I have another sysfs patch that allows setting a default skb->mark for an interface so that you can set the skb->mark
before it hits the connection tracking logic, but I'm been told this one has very little chance
of getting into the kernel. The skb->mark patch is only useful (as far

Quoted 8 lines

as I can tell) if you
also include a patch Patrick McHardy did for me that allowed the conn-tracking logic to
use skb->mark as part of it's tuple. This allows me to do NAT between virtual routers
(routing tables) on the same machine using veth-equivalent drivers to connect the
routers. He thinks this will probably not ever get into the kernel either.

Quoted 3 lines

Are these patches available somewhere? I'm currently doing NAT between virtual routers by some advanced iproute2/iptables trickery, but I have no way to handle the occasional tuple conflict.

A consolidated patch against 2.6.20.12 is here. It has a lot more than just the patches mentioned above, but it shouldn't hurt anything to have the whole patch applied:

http://www.candelatech.com/oss/candela_2.6.20.patch

The original patch for using skb->mark as a tuple was written by Patrick McHardy, and is here:

http://www.candelatech.com/oss/skb_mark_conntrack.patch

His patch merged with my patch to sysfs to set skb->mark on ingress is here:
http://www.candelatech.com/oss/conntrack_mark_with_ssyctl.patch

Thanks,
Ben

Quoted 3 lines

Regards,
Carl-Daniel

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com
#12 lore raw
In reply to Ben Greear
Quoted 24 lines · Ben Greear
Quoted 9 lines · Pavel Emelianov
Quoted 4 lines

I would also like some way to identify veth from other device types, preferably
something like a value in sysfs. However, that should not hold up

We can do this with ethtool. It can get and print the driver name of the device.

I think I'd like something in sysfs that we could query for any interface. Possible return
strings could be:
VLAN
VETH
ETH
PPP
BRIDGE
AP /* wifi access point interface */
STA /* wifi station */
....

Quoted 1 line

I will cook up a patch for consideration after veth goes in.

The rtnl_link API gives you the name of the driver (IFLA_INFO_KIND).