Skip to contentSkip to conversation

gre: introduce native tunnel support for ERSPAN

PATCH Superseded William Tu 2 participants 3 messages Aug 18, 2017 – Aug 22, 2017

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

William Tu

author
#0 lore raw

PATCH v3 1/1 gre: introduce native tunnel support for ERSPAN

The patch adds ERSPAN type II tunnel support. The implementation is based on the draft at [1]. One of the purposes is for Linux box to be able to receive ERSPAN monitoring traffic sent from the Cisco switch, by creating a ERSPAN tunnel device. In addition, the patch also adds ERSPAN TX, so traffic can also be encapsulated into ERSPAN and sent out.

The implementation reuses the key as ERSPAN session ID, and field 'erspan' as ERSPAN Index fields:
./ip link add dev ers11 type erspan seq key 100 erspan 123 \

			local 172.16.1.200 remote 172.16.1.100

To use the above device as ERSPAN receiver, on configure Nexus 5000 switch as below:

monitor session 1 type erspan-source
erspan-id 100
vrf default
destination ip 172.16.1.200
source interface Ethernet1/11 both
source interface Ethernet1/12 both
no shut
monitor erspan origin ip-address 172.16.1.100 global

[1] https://tools.ietf.org/html/draft-foschiano-erspan-01
[2] iproute2 patch: http://marc.info/?l=linux-netdev&m=150306086924951&w=2
[3] test script: http://marc.info/?l=linux-netdev&m=150231021807304&w=2

Cc Alexey Kuznetsov <[email protected]>
Cc Hideaki YOSHIFUJI <[email protected]>

---
v2->v3:
add skb_may_pull check at erspan_rcv
a couple of minor fixes and checks for index value
v1->v2:
Add missing erspan.h header

---
 include/net/erspan.h           |  61 ++++++++++
 include/net/ip_tunnels.h       |   3 +
 include/uapi/linux/if_ether.h  |   1 +
 include/uapi/linux/if_tunnel.h |   1 +
 net/ipv4/ip_gre.c              | 267 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 333 insertions(+)
 create mode 100644 include/net/erspan.h
include/net/erspan.h new file +61 -0
new file mode 100644
index 000000000000..ca94fc86865e
@@ -0,0 +1,61 @@
+#ifndef __LINUX_ERSPAN_H
+#define __LINUX_ERSPAN_H
+
+/*
+ * GRE header for ERSPAN encapsulation (8 octets [34:41]) -- 8 bytes
+ *       0                   1                   2                   3
+ *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *     |0|0|0|1|0|00000|000000000|00000|    Protocol Type for ERSPAN   |
+ *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *     |      Sequence Number (increments per packet per session)      |
+ *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *
+ *  Note that in the above GRE header [RFC1701] out of the C, R, K, S,
+ *  s, Recur, Flags, Version fields only S (bit 03) is set to 1. The
+ *  other fields are set to zero, so only a sequence number follows.
+ *
+ *  ERSPAN Type II header (8 octets [42:49])
+ *  0                   1                   2                   3
+ *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |  Ver  |          VLAN         | COS | En|T|    Session ID     |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |      Reserved         |                  Index                |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *
+ * GRE proto ERSPAN type II = 0x88BE, type III = 0x22EB
+ */
+
+#define ERSPAN_VERSION	0x1
+
+#define VER_MASK	0xf000
+#define VLAN_MASK	0x0fff
+#define COS_MASK	0xe000
+#define EN_MASK		0x1800
+#define T_MASK		0x0400
+#define ID_MASK		0x03ff
+#define INDEX_MASK	0xfffff
+
+enum erspan_encap_type {
+	ERSPAN_ENCAP_NOVLAN = 0x0,	/* originally without VLAN tag */
+	ERSPAN_ENCAP_ISL = 0x1,		/* originally ISL encapsulated */
+	ERSPAN_ENCAP_8021Q = 0x2,	/* originally 802.1Q encapsulated */
+	ERSPAN_ENCAP_INFRAME = 0x3,	/* VLAN tag perserved in frame */
+};
+
+struct erspan_metadata {
+	__be32 index;   /* type II */
+};
+
+struct erspanhdr {
+	__be16 ver_vlan;
+#define VER_OFFSET  12
+	__be16 session_id;
+#define COS_OFFSET  13
+#define EN_OFFSET   11
+#define T_OFFSET    10
+	struct erspan_metadata md;
+};
+
+#endif
include/net/ip_tunnels.h +3 -0
index 520809912f03..625c29329372 100644
@@ -115,6 +115,9 @@ struct ip_tunnel {
 	u32		o_seqno;	/* The last output seqno */
 	int		tun_hlen;	/* Precalculated header length */
 
+	/* This field used only by ERSPAN */
+	u32		index;		/* ERSPAN type II index */
+
 	struct dst_cache dst_cache;
 
 	struct ip_tunnel_parm parms;
include/uapi/linux/if_ether.h +1 -0
index 5bc9bfd816b7..efeb1190c2ca 100644
@@ -66,6 +66,7 @@
 #define ETH_P_ATALK	0x809B		/* Appletalk DDP		*/
 #define ETH_P_AARP	0x80F3		/* Appletalk AARP		*/
 #define ETH_P_8021Q	0x8100          /* 802.1Q VLAN Extended Header  */
+#define ETH_P_ERSPAN	0x88BE		/* ERSPAN type II		*/
 #define ETH_P_IPX	0x8137		/* IPX over DIX			*/
 #define ETH_P_IPV6	0x86DD		/* IPv6 over bluebook		*/
 #define ETH_P_PAUSE	0x8808		/* IEEE Pause frames. See 802.3 31B */
include/uapi/linux/if_tunnel.h +1 -0
index 6792d1967d31..2e520883c054 100644
@@ -134,6 +134,7 @@ enum {
 	IFLA_GRE_COLLECT_METADATA,
 	IFLA_GRE_IGNORE_DF,
 	IFLA_GRE_FWMARK,
+	IFLA_GRE_ERSPAN_INDEX,
 	__IFLA_GRE_MAX,
 };
 
net/ipv4/ip_gre.c +267 -0

384 lines not on this page · raw message

Signature · 1 line
2.7.4
maintainer
#1 lore raw

From: William Tu <[email protected]>
Date: Fri, 18 Aug 2017 06:24:40 -0700

net/ipv4/ip_gre.c static void gre_err(struct sk_buff *skb, u32 info) · line 262

Quoted 12 lines
net/ipv4/ip_gre.c +12 -0
+static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
+		      int gre_hdr_len)
+{
+	struct net *net = dev_net(skb->dev);
+	struct ip_tunnel_net *itn;
+	struct ip_tunnel *tunnel;
+	struct metadata_dst *tun_dst = NULL;
+	const struct iphdr *iph;
+	struct erspanhdr *ershdr;
+	__be32 index;
+	__be32 session_id;
+	int len;

Please order local variables from longest to shortest line, ie. reverse christmas tree format.

net/ipv4/ip_gre.c static void gre_err(struct sk_buff *skb, u32 info) · line 269

Quoted 7 lines
net/ipv4/ip_gre.c +7 -0
+
+	itn = net_generic(net, erspan_net_id);
+	iph = ip_hdr(skb);
+	len =  iph->ihl * 4 + gre_hdr_len + sizeof(*ershdr);
+
+	if (unlikely(!pskb_may_pull(skb, len)))
+		return -ENOMEM;

I think the len passed here is wrong, it should be "gre_hdr_len + sizeof(*ershdr)".

net/ipv4/ip_gre.c static netdev_tx_t ipgre_xmit(struct sk_buff *skb, · line 583

Quoted 12 lines
net/ipv4/ip_gre.c +12 -0
+static void erspan_build_header(struct sk_buff *skb,
+				__be32 id, u32 index, bool truncate)
+{
+	struct erspanhdr *ershdr;
+	struct iphdr *iphdr = ip_hdr(skb);
+	struct ethhdr *eth = eth_hdr(skb);
+	struct qtag_prefix {
+		__be16 eth_type;
+		__be16 tci;
+	} *qp;
+	u16 vlan_tci = 0;
+	enum erspan_encap_type enc_type = ERSPAN_ENCAP_NOVLAN;

Reverse christmas tree for the local variables, please.

net/ipv4/ip_gre.c static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[], · line 965

Quoted 5 lines
net/ipv4/ip_gre.c +5 -0
+static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
+			   struct netlink_ext_ack *extack)
+{
+	int ret;
+	__be16 flags = 0;

Likewise.

author
#2 lore raw
Quoted 10 lines

net/ipv4/ip_gre.c static void gre_err(struct sk_buff *skb, u32 info) · line 262

Quoted 6 lines
net/ipv4/ip_gre.c +6 -0
+     struct metadata_dst *tun_dst = NULL;
+     const struct iphdr *iph;
+     struct erspanhdr *ershdr;
+     __be32 index;
+     __be32 session_id;
+     int len;

Please order local variables from longest to shortest line, ie. reverse christmas tree format.

thanks for the review, I will fix the ordering in next version.

Quoted 10 lines

net/ipv4/ip_gre.c static void gre_err(struct sk_buff *skb, u32 info) · line 269

Quoted 7 lines
net/ipv4/ip_gre.c +7 -0
+
+     itn = net_generic(net, erspan_net_id);
+     iph = ip_hdr(skb);
+     len =  iph->ihl * 4 + gre_hdr_len + sizeof(*ershdr);
+
+     if (unlikely(!pskb_may_pull(skb, len)))
+             return -ENOMEM;

I think the len passed here is wrong, it should be "gre_hdr_len + sizeof(*ershdr)".

yes, thanks.
The skb->data at this point points to the beginning of GRE header. So I should check pull of gre + erspan header.

Regards,
William