Commit 1d7e2ed2 authored by William Tu's avatar William Tu Committed by David S. Miller

net: erspan: refactor existing erspan code

The patch refactors the existing erspan implementation in order
to support erspan version 2, which has additional metadata.  So, in
stead of having one 'struct erspanhdr' holding erspan version 1,
breaks it into 'struct erspan_base_hdr' and 'struct erspan_metadata'.
Signed-off-by: default avatarWilliam Tu <u9012063@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 4650b751
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* s, Recur, Flags, Version fields only S (bit 03) is set to 1. The * 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. * other fields are set to zero, so only a sequence number follows.
* *
* ERSPAN Type II header (8 octets [42:49]) * ERSPAN Version 1 (Type II) header (8 octets [42:49])
* 0 1 2 3 * 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 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
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
* GRE proto ERSPAN type II = 0x88BE, type III = 0x22EB * GRE proto ERSPAN type II = 0x88BE, type III = 0x22EB
*/ */
#define ERSPAN_VERSION 0x1 #define ERSPAN_VERSION 0x1 /* ERSPAN type II */
#define VER_MASK 0xf000 #define VER_MASK 0xf000
#define VLAN_MASK 0x0fff #define VLAN_MASK 0x0fff
...@@ -44,20 +44,29 @@ enum erspan_encap_type { ...@@ -44,20 +44,29 @@ enum erspan_encap_type {
ERSPAN_ENCAP_INFRAME = 0x3, /* VLAN tag perserved in frame */ ERSPAN_ENCAP_INFRAME = 0x3, /* VLAN tag perserved in frame */
}; };
#define ERSPAN_V1_MDSIZE 4
#define ERSPAN_V2_MDSIZE 8
struct erspan_metadata { struct erspan_metadata {
__be32 index; /* type II */ union {
__be32 index; /* Version 1 (type II)*/
} u;
}; };
struct erspanhdr { struct erspan_base_hdr {
__be16 ver_vlan; __be16 ver_vlan;
#define VER_OFFSET 12 #define VER_OFFSET 12
__be16 session_id; __be16 session_id;
#define COS_OFFSET 13 #define COS_OFFSET 13
#define EN_OFFSET 11 #define EN_OFFSET 11
#define T_OFFSET 10 #define T_OFFSET 10
struct erspan_metadata md;
}; };
static inline int erspan_hdr_len(int version)
{
return sizeof(struct erspan_base_hdr) +
(version == 1 ? ERSPAN_V1_MDSIZE : ERSPAN_V2_MDSIZE);
}
static inline u8 tos_to_cos(u8 tos) static inline u8 tos_to_cos(u8 tos)
{ {
u8 dscp, cos; u8 dscp, cos;
...@@ -73,7 +82,8 @@ static inline void erspan_build_header(struct sk_buff *skb, ...@@ -73,7 +82,8 @@ static inline void erspan_build_header(struct sk_buff *skb,
{ {
struct ethhdr *eth = eth_hdr(skb); struct ethhdr *eth = eth_hdr(skb);
enum erspan_encap_type enc_type; enum erspan_encap_type enc_type;
struct erspanhdr *ershdr; struct erspan_base_hdr *ershdr;
struct erspan_metadata *ersmd;
struct qtag_prefix { struct qtag_prefix {
__be16 eth_type; __be16 eth_type;
__be16 tci; __be16 tci;
...@@ -96,17 +106,21 @@ static inline void erspan_build_header(struct sk_buff *skb, ...@@ -96,17 +106,21 @@ static inline void erspan_build_header(struct sk_buff *skb,
enc_type = ERSPAN_ENCAP_INFRAME; enc_type = ERSPAN_ENCAP_INFRAME;
} }
skb_push(skb, sizeof(*ershdr)); skb_push(skb, sizeof(*ershdr) + ERSPAN_V1_MDSIZE);
ershdr = (struct erspanhdr *)skb->data; ershdr = (struct erspan_base_hdr *)skb->data;
memset(ershdr, 0, sizeof(*ershdr)); memset(ershdr, 0, sizeof(*ershdr) + ERSPAN_V1_MDSIZE);
/* Build base header */
ershdr->ver_vlan = htons((vlan_tci & VLAN_MASK) | ershdr->ver_vlan = htons((vlan_tci & VLAN_MASK) |
(ERSPAN_VERSION << VER_OFFSET)); (ERSPAN_VERSION << VER_OFFSET));
ershdr->session_id = htons((u16)(ntohl(id) & ID_MASK) | ershdr->session_id = htons((u16)(ntohl(id) & ID_MASK) |
((tos_to_cos(tos) << COS_OFFSET) & COS_MASK) | ((tos_to_cos(tos) << COS_OFFSET) & COS_MASK) |
(enc_type << EN_OFFSET & EN_MASK) | (enc_type << EN_OFFSET & EN_MASK) |
((truncate << T_OFFSET) & T_MASK)); ((truncate << T_OFFSET) & T_MASK));
ershdr->md.index = htonl(index & INDEX_MASK);
/* Build metadata */
ersmd = (struct erspan_metadata *)(ershdr + 1);
ersmd->u.index = htonl(index & INDEX_MASK);
} }
#endif #endif
...@@ -256,34 +256,41 @@ static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, ...@@ -256,34 +256,41 @@ static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
{ {
struct net *net = dev_net(skb->dev); struct net *net = dev_net(skb->dev);
struct metadata_dst *tun_dst = NULL; struct metadata_dst *tun_dst = NULL;
struct erspan_base_hdr *ershdr;
struct erspan_metadata *pkt_md;
struct ip_tunnel_net *itn; struct ip_tunnel_net *itn;
struct ip_tunnel *tunnel; struct ip_tunnel *tunnel;
struct erspanhdr *ershdr;
const struct iphdr *iph; const struct iphdr *iph;
__be32 index; int ver;
int len; int len;
itn = net_generic(net, erspan_net_id); itn = net_generic(net, erspan_net_id);
len = gre_hdr_len + sizeof(*ershdr); len = gre_hdr_len + sizeof(*ershdr);
/* Check based hdr len */
if (unlikely(!pskb_may_pull(skb, len))) if (unlikely(!pskb_may_pull(skb, len)))
return -ENOMEM; return -ENOMEM;
iph = ip_hdr(skb); iph = ip_hdr(skb);
ershdr = (struct erspanhdr *)(skb->data + gre_hdr_len); ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len);
ver = (ntohs(ershdr->ver_vlan) & VER_MASK) >> VER_OFFSET;
/* The original GRE header does not have key field, /* The original GRE header does not have key field,
* Use ERSPAN 10-bit session ID as key. * Use ERSPAN 10-bit session ID as key.
*/ */
tpi->key = cpu_to_be32(ntohs(ershdr->session_id) & ID_MASK); tpi->key = cpu_to_be32(ntohs(ershdr->session_id) & ID_MASK);
index = ershdr->md.index; pkt_md = (struct erspan_metadata *)(ershdr + 1);
tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
tpi->flags | TUNNEL_KEY, tpi->flags | TUNNEL_KEY,
iph->saddr, iph->daddr, tpi->key); iph->saddr, iph->daddr, tpi->key);
if (tunnel) { if (tunnel) {
len = gre_hdr_len + erspan_hdr_len(ver);
if (unlikely(!pskb_may_pull(skb, len)))
return -ENOMEM;
if (__iptunnel_pull_header(skb, if (__iptunnel_pull_header(skb,
gre_hdr_len + sizeof(*ershdr), len,
htons(ETH_P_TEB), htons(ETH_P_TEB),
false, false) < 0) false, false) < 0)
goto drop; goto drop;
...@@ -307,12 +314,12 @@ static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, ...@@ -307,12 +314,12 @@ static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
if (!md) if (!md)
return PACKET_REJECT; return PACKET_REJECT;
md->index = index; memcpy(md, pkt_md, sizeof(*md));
info = &tun_dst->u.tun_info; info = &tun_dst->u.tun_info;
info->key.tun_flags |= TUNNEL_ERSPAN_OPT; info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
info->options_len = sizeof(*md); info->options_len = sizeof(*md);
} else { } else {
tunnel->index = ntohl(index); tunnel->index = ntohl(pkt_md->u.index);
} }
skb_reset_mac_header(skb); skb_reset_mac_header(skb);
...@@ -571,7 +578,7 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev, ...@@ -571,7 +578,7 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev,
key = &tun_info->key; key = &tun_info->key;
/* ERSPAN has fixed 8 byte GRE header */ /* ERSPAN has fixed 8 byte GRE header */
tunnel_hlen = 8 + sizeof(struct erspanhdr); tunnel_hlen = 8 + sizeof(struct erspan_base_hdr) + ERSPAN_V1_MDSIZE;
rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen); rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen);
if (!rt) if (!rt)
...@@ -590,7 +597,7 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev, ...@@ -590,7 +597,7 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev,
goto err_free_rt; goto err_free_rt;
erspan_build_header(skb, tunnel_id_to_key32(key->tun_id), erspan_build_header(skb, tunnel_id_to_key32(key->tun_id),
ntohl(md->index), truncate, true); ntohl(md->u.index), truncate, true);
gre_build_header(skb, 8, TUNNEL_SEQ, gre_build_header(skb, 8, TUNNEL_SEQ,
htons(ETH_P_ERSPAN), 0, htonl(tunnel->o_seqno++)); htons(ETH_P_ERSPAN), 0, htonl(tunnel->o_seqno++));
...@@ -1238,7 +1245,7 @@ static int erspan_tunnel_init(struct net_device *dev) ...@@ -1238,7 +1245,7 @@ static int erspan_tunnel_init(struct net_device *dev)
tunnel->tun_hlen = 8; tunnel->tun_hlen = 8;
tunnel->parms.iph.protocol = IPPROTO_GRE; tunnel->parms.iph.protocol = IPPROTO_GRE;
tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
sizeof(struct erspanhdr); sizeof(struct erspan_base_hdr) + ERSPAN_V1_MDSIZE;
t_hlen = tunnel->hlen + sizeof(struct iphdr); t_hlen = tunnel->hlen + sizeof(struct iphdr);
dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4; dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4;
......
...@@ -501,25 +501,32 @@ static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi) ...@@ -501,25 +501,32 @@ static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len, static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len,
struct tnl_ptk_info *tpi) struct tnl_ptk_info *tpi)
{ {
struct erspan_base_hdr *ershdr;
struct erspan_metadata *pkt_md;
const struct ipv6hdr *ipv6h; const struct ipv6hdr *ipv6h;
struct erspanhdr *ershdr;
struct ip6_tnl *tunnel; struct ip6_tnl *tunnel;
__be32 index; u8 ver;
ipv6h = ipv6_hdr(skb); ipv6h = ipv6_hdr(skb);
ershdr = (struct erspanhdr *)skb->data; ershdr = (struct erspan_base_hdr *)skb->data;
if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr)))) if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
return PACKET_REJECT; return PACKET_REJECT;
ver = (ntohs(ershdr->ver_vlan) & VER_MASK) >> VER_OFFSET;
tpi->key = cpu_to_be32(ntohs(ershdr->session_id) & ID_MASK); tpi->key = cpu_to_be32(ntohs(ershdr->session_id) & ID_MASK);
index = ershdr->md.index; pkt_md = (struct erspan_metadata *)(ershdr + 1);
tunnel = ip6gre_tunnel_lookup(skb->dev, tunnel = ip6gre_tunnel_lookup(skb->dev,
&ipv6h->saddr, &ipv6h->daddr, tpi->key, &ipv6h->saddr, &ipv6h->daddr, tpi->key,
tpi->proto); tpi->proto);
if (tunnel) { if (tunnel) {
if (__iptunnel_pull_header(skb, sizeof(*ershdr), int len = erspan_hdr_len(ver);
if (unlikely(!pskb_may_pull(skb, len)))
return -ENOMEM;
if (__iptunnel_pull_header(skb, len,
htons(ETH_P_TEB), htons(ETH_P_TEB),
false, false) < 0) false, false) < 0)
return PACKET_REJECT; return PACKET_REJECT;
...@@ -545,14 +552,14 @@ static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len, ...@@ -545,14 +552,14 @@ static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len,
if (!md) if (!md)
return PACKET_REJECT; return PACKET_REJECT;
md->index = index; memcpy(md, pkt_md, sizeof(*md));
info->key.tun_flags |= TUNNEL_ERSPAN_OPT; info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
info->options_len = sizeof(*md); info->options_len = sizeof(*md);
ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
} else { } else {
tunnel->parms.index = ntohl(index); tunnel->parms.index = ntohl(pkt_md->u.index);
ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error); ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
} }
...@@ -921,7 +928,7 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, ...@@ -921,7 +928,7 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
goto tx_err; goto tx_err;
erspan_build_header(skb, tunnel_id_to_key32(key->tun_id), erspan_build_header(skb, tunnel_id_to_key32(key->tun_id),
ntohl(md->index), truncate, false); ntohl(md->u.index), truncate, false);
} else { } else {
switch (skb->protocol) { switch (skb->protocol) {
...@@ -1657,7 +1664,7 @@ static int ip6erspan_tap_init(struct net_device *dev) ...@@ -1657,7 +1664,7 @@ static int ip6erspan_tap_init(struct net_device *dev)
tunnel->tun_hlen = 8; tunnel->tun_hlen = 8;
tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
sizeof(struct erspanhdr); sizeof(struct erspan_base_hdr) + ERSPAN_V1_MDSIZE;
t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
dev->hard_header_len = LL_MAX_HEADER + t_hlen; dev->hard_header_len = LL_MAX_HEADER + t_hlen;
......
...@@ -644,12 +644,12 @@ static int erspan_tun_opt_from_nlattr(const struct nlattr *attr, ...@@ -644,12 +644,12 @@ static int erspan_tun_opt_from_nlattr(const struct nlattr *attr,
BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts)); BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
opts.index = nla_get_be32(attr); opts.u.index = nla_get_be32(attr);
/* Index has only 20-bit */ /* Index has only 20-bit */
if (ntohl(opts.index) & ~INDEX_MASK) { if (ntohl(opts.u.index) & ~INDEX_MASK) {
OVS_NLERR(log, "ERSPAN index number %x too large.", OVS_NLERR(log, "ERSPAN index number %x too large.",
ntohl(opts.index)); ntohl(opts.u.index));
return -EINVAL; return -EINVAL;
} }
...@@ -907,7 +907,7 @@ static int __ip_tun_to_nlattr(struct sk_buff *skb, ...@@ -907,7 +907,7 @@ static int __ip_tun_to_nlattr(struct sk_buff *skb,
return -EMSGSIZE; return -EMSGSIZE;
else if (output->tun_flags & TUNNEL_ERSPAN_OPT && else if (output->tun_flags & TUNNEL_ERSPAN_OPT &&
nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
((struct erspan_metadata *)tun_opts)->index)) ((struct erspan_metadata *)tun_opts)->u.index))
return -EMSGSIZE; return -EMSGSIZE;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment