ip6_gre: add erspan native tunnel for ipv6
Subsystems (3)
v5 cover ip6_gre: add erspan native tunnel for ipv6
The patch series add support for ERSPAN tunnel over ipv6. The first patch refectors the existing ipv4 gre implementation and the second refactors the ipv6 gre's xmit code. Finally the last patch introduces erspan protocol.
change in v5:
- add cover-letter description
change in v4:
- rebase on top of net-next
- use log_ecn_error in ip6_tnl_rcv
change in v3:
- add inline for functions in header
- rebase on top of net-next
change in v2:
- remove inline
- fix some indent
- fix errors reports by clang and scan-build
William Tu (3):
ip_gre: Refector the erpsan tunnel code.
ip6_gre: Refactor ip6gre xmit codes
ip6_gre: Add ERSPAN native tunnel support
include/net/erspan.h | 51 ++++++ include/net/ip6_tunnel.h | 1 + net/ipv4/ip_gre.c | 54 +------ net/ipv6/ip6_gre.c | 393 ++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 398 insertions(+), 101 deletions(-)
--
A test script is provided below:
#!/bin/bash
# In the namespace NS0, create veth0 and ip6erspan00 # Out of the namespace, create veth1 and ip6erspan11 # Ping in and out of namespace using ERSPAN protocol
# Patch v2 for iproute2
# https://marc.info/?l=linux-netdev&m=151002165705772&w=2
cleanup() {
set +ex
ip netns del ns0
ip link del ip6erspan11
ip link del veth1
}
main() {
trap cleanup 0 2 3 9
ip netns add ns0
ip link add veth0 type veth peer name veth1
ip link set veth0 netns ns0
# non-namespace
ip addr add dev veth1 fc00:100::2/96
ip link add dev ip6erspan11 type ip6erspan seq key 102 erspan 123 \
local fc00:100::2 \
remote fc00:100::1
ip addr add dev ip6erspan11 fc00:200::2/96
ip addr add dev ip6erspan11 10.10.200.2/24
# namespace: ns0
ip netns exec ns0 ip addr add fc00:100::1/96 dev veth0
# Tunnel
ip netns exec ns0 ip link add dev ip6erspan00 type ip6erspan seq key 102 erspan 12 \
local fc00:100::1 \
remote fc00:100::2
ip netns exec ns0 ip addr add dev ip6erspan00 fc00:200::1/96
ip netns exec ns0 ip addr add dev ip6erspan00 10.10.200.1/24
ip link set dev veth1 up
ip link set dev ip6erspan11 up
ip netns exec ns0 ip link set dev ip6erspan00 up
ip netns exec ns0 ip link set dev veth0 up
}main
# Ping underlying
ping6 -c 1 fc00:100::1 || true
# ping overlay ping -c 3 10.10.200.1 ping6 -c 3 fc00:200::1
Signature · 1 line
2.7.4
PATCH v5 1/3 ip_gre: Refector the erpsan tunnel code.
Move two erspan functions to header file, erspan.h, so ipv6 erspan implementation can use it.
--- include/net/erspan.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ net/ipv4/ip_gre.c | 54 +++++----------------------------------------------- 2 files changed, 56 insertions(+), 49 deletions(-)
include/net/erspan.h +51 -0
@@ -58,4 +58,55 @@ struct erspanhdr {struct erspan_metadata md; }; +static inline u8 tos_to_cos (u8 tos) +{ +u8 dscp, cos; + + dscp = tos >>2 ; + cos = dscp >>3 ; +return cos; +} + +static inline void erspan_build_header (struct sk_buff *skb, +__be32 id,u32 index, +bool truncate,bool is_ipv4) +{ +struct ethhdr *eth =eth_hdr (skb); +enum erspan_encap_type enc_type; +struct erspanhdr *ershdr; +struct qtag_prefix { +__be16 eth_type; +__be16 tci; + } *qp; +u16 vlan_tci =0 ; +u8 tos; + + tos = is_ipv4 ?ip_hdr (skb)->tos : + (ipv6_hdr (skb)->priority <<4 ) + + (ipv6_hdr (skb)->flow_lbl[0 ] >>4 ); + + enc_type =ERSPAN_ENCAP_NOVLAN ; + +/* If mirrored packet has vlan tag, extract tci and +* perserve vlan header in the mirrored frame. +*/ +if (eth->h_proto ==htons (ETH_P_8021Q )) { + qp = (struct qtag_prefix *)(skb->data +2 *ETH_ALEN ); + vlan_tci =ntohs (qp->tci); + enc_type =ERSPAN_ENCAP_INFRAME ; + } + +skb_push (skb,sizeof (*ershdr)); + ershdr = (struct erspanhdr *)skb->data; +memset (ershdr,0 ,sizeof (*ershdr)); + + ershdr->ver_vlan =htons ((vlan_tci &VLAN_MASK ) | + (ERSPAN_VERSION <<VER_OFFSET )); + ershdr->session_id =htons ((u16)(ntohl (id) &ID_MASK ) | + ((tos_to_cos (tos) <<COS_OFFSET ) &COS_MASK ) | + (enc_type <<EN_OFFSET &EN_MASK ) | + ((truncate <<T_OFFSET ) &T_MASK )); + ershdr->md.index =htonl (index &INDEX_MASK ); +} +#endif
net/ipv4/ip_gre.c +5 -49
78 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v5 2/3 ip6_gre: Refactor ip6gre xmit codes
In reply to William TuThis patch refactors the ip6gre_xmit_{ipv4, ipv6}. It is a prep work to add the ip6erspan tunnel.
--- net/ipv6/ip6_gre.c | 123 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 75 insertions(+), 48 deletions(-)
net/ipv6/ip6_gre.c +75 -48
@@ -496,6 +496,78 @@ static int gre_handle_offloads(struct sk_buff *skb, bool csum) csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); } +static void prepare_ip6gre_xmit_ipv4 (struct sk_buff *skb, +struct net_device *dev, +struct flowi6 *fl6,__u8 *dsfield, +int *encap_limit) +{ +const struct iphdr *iph =ip_hdr (skb); +struct ip6_tnl *t =netdev_priv (dev); + +if (!(t->parms.flags &IP6_TNL_F_IGN_ENCAP_LIMIT )) + *encap_limit = t->parms.encap_limit; + +memcpy (fl6, &t->fl.u.ip6,sizeof (*fl6)); + +if (t->parms.flags &IP6_TNL_F_USE_ORIG_TCLASS ) + *dsfield =ipv4_get_dsfield (iph); +else + *dsfield =ip6_tclass (t->parms.flowinfo); + +if (t->parms.flags &IP6_TNL_F_USE_ORIG_FWMARK ) + fl6->flowi6_mark = skb->mark; +else + fl6->flowi6_mark = t->parms.fwmark; + + fl6->flowi6_uid =sock_net_uid (dev_net (dev),NULL ); +} + +static int prepare_ip6gre_xmit_ipv6 (struct sk_buff *skb, +struct net_device *dev, +struct flowi6 *fl6,__u8 *dsfield, +int *encap_limit) +{ +struct ipv6hdr *ipv6h =ipv6_hdr (skb); +struct ip6_tnl *t =netdev_priv (dev); +__u16 offset; + + offset =ip6_tnl_parse_tlv_enc_lim (skb,skb_network_header (skb)); +/* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */ + +if (offset >0 ) { +struct ipv6_tlv_tnl_enc_lim *tel; + + tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header (skb)[offset]; +if (tel->encap_limit ==0 ) { +icmpv6_send (skb,ICMPV6_PARAMPROB , +ICMPV6_HDR_FIELD , offset +2 ); +return -1 ; + } + *encap_limit = tel->encap_limit -1 ; + }else if (!(t->parms.flags &IP6_TNL_F_IGN_ENCAP_LIMIT )) { + *encap_limit = t->parms.encap_limit; + } + +memcpy (fl6, &t->fl.u.ip6,sizeof (*fl6)); + +if (t->parms.flags &IP6_TNL_F_USE_ORIG_TCLASS ) + *dsfield =ipv6_get_dsfield (ipv6h); +else + *dsfield =ip6_tclass (t->parms.flowinfo); + +if (t->parms.flags &IP6_TNL_F_USE_ORIG_FLOWLABEL ) + fl6->flowlabel |=ip6_flowlabel (ipv6h); + +if (t->parms.flags &IP6_TNL_F_USE_ORIG_FWMARK ) + fl6->flowi6_mark = skb->mark; +else + fl6->flowi6_mark = t->parms.fwmark; + + fl6->flowi6_uid =sock_net_uid (dev_net (dev),NULL ); + +return 0 ; +} + static netdev_tx_t __gre6_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield, struct flowi6 *fl6, int encap_limit,
@@ -527,7 +599,6 @@ static netdev_tx_t __gre6_xmit(struct sk_buff *skb,static inline int ip6gre_xmit_ipv4 (struct sk_buff *skb,struct net_device *dev) {struct ip6_tnl *t =netdev_priv (dev); -const struct iphdr *iph =ip_hdr (skb);int encap_limit =-1 ;struct flowi6 fl6;__u8 dsfield;
@@ -536,21 +607,7 @@ static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)memset (&(IPCB (skb)->opt),0 ,sizeof (IPCB (skb)->opt)); -if (!(t->parms.flags &IP6_TNL_F_IGN_ENCAP_LIMIT )) - encap_limit = t->parms.encap_limit; - -memcpy (&fl6, &t->fl.u.ip6,sizeof (fl6)); - -if (t->parms.flags &IP6_TNL_F_USE_ORIG_TCLASS ) - dsfield =ipv4_get_dsfield (iph); -else - dsfield =ip6_tclass (t->parms.flowinfo); -if (t->parms.flags &IP6_TNL_F_USE_ORIG_FWMARK ) - fl6.flowi6_mark = skb->mark; -else - fl6.flowi6_mark = t->parms.fwmark; - - fl6.flowi6_uid =sock_net_uid (dev_net (dev),NULL ); +prepare_ip6gre_xmit_ipv4 (skb, dev, &fl6, &dsfield, &encap_limit); err =gre_handle_offloads (skb, !!(t->parms.o_flags &TUNNEL_CSUM ));if (err)
@@ -574,7 +631,6 @@ static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)struct ip6_tnl *t =netdev_priv (dev);struct ipv6hdr *ipv6h =ipv6_hdr (skb);int encap_limit =-1 ; -__u16 offset;struct flowi6 fl6;__u8 dsfield;__u32 mtu;
@@ -583,37 +639,8 @@ static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)if (ipv6_addr_equal (&t->parms.raddr, &ipv6h->saddr))return -1 ; - offset =ip6_tnl_parse_tlv_enc_lim (skb,skb_network_header (skb)); -/* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */ - ipv6h =ipv6_hdr (skb); - -if (offset >0 ) { -struct ipv6_tlv_tnl_enc_lim *tel; - tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header (skb)[offset]; -if (tel->encap_limit ==0 ) { -icmpv6_send (skb,ICMPV6_PARAMPROB , -ICMPV6_HDR_FIELD , offset +2 ); -return -1 ; - } - encap_limit = tel->encap_limit -1 ; - }else if (!(t->parms.flags &IP6_TNL_F_IGN_ENCAP_LIMIT )) - encap_limit = t->parms.encap_limit; - -memcpy (&fl6, &t->fl.u.ip6,sizeof (fl6)); - -if (t->parms.flags &IP6_TNL_F_USE_ORIG_TCLASS ) - dsfield =ipv6_get_dsfield (ipv6h); -else - dsfield =ip6_tclass (t->parms.flowinfo); - -if (t->parms.flags &IP6_TNL_F_USE_ORIG_FLOWLABEL ) - fl6.flowlabel |=ip6_flowlabel (ipv6h); -if (t->parms.flags &IP6_TNL_F_USE_ORIG_FWMARK ) - fl6.flowi6_mark = skb->mark; -else - fl6.flowi6_mark = t->parms.fwmark; - - fl6.flowi6_uid =sock_net_uid (dev_net (dev),NULL ); +if (prepare_ip6gre_xmit_ipv6 (skb, dev, &fl6, &dsfield, &encap_limit)) +return -1 ;if (gre_handle_offloads (skb, !!(t->parms.o_flags &TUNNEL_CSUM )))return -1 ;
Signature · 1 line
2.7.4
PATCH v5 3/3 ip6_gre: Add ERSPAN native tunnel support
In reply to William TuThe patch adds support for ERSPAN tunnel over ipv6.
--- include/net/ip6_tunnel.h | 1 + net/ipv6/ip6_gre.c | 270 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 267 insertions(+), 4 deletions(-)
include/net/ip6_tunnel.h +1 -0
@@ -36,6 +36,7 @@ struct __ip6_tnl_parm {__be32 o_key;__u32 fwmark; +__u32 index;/* ERSPAN type II index */ };/* IPv6 tunnel */
net/ipv6/ip6_gre.c +266 -4
377 lines not on this page · raw message
Signature · 1 line
2.7.4
David Miller
maintainerFrom: William Tu <[email protected]>
Date: Thu, 30 Nov 2017 11:51:26 -0800
Quoted 3 lines
The patch series add support for ERSPAN tunnel over ipv6. The first patch refectors the existing ipv4 gre implementation and the second refactors the ipv6 gre's xmit code. Finally the last patch introduces erspan protocol.
Series applied, thanks William.