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

samples/bpf: extend test_tunnel_bpf.sh with ERSPAN

Extend existing tests for vxlan, gre, geneve, ipip to
include ERSPAN tunnel.
Signed-off-by: default avatarWilliam Tu <u9012063@gmail.com>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 1a66a836
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <uapi/linux/pkt_cls.h> #include <uapi/linux/pkt_cls.h>
#include <net/ipv6.h> #include <net/ipv6.h>
#include "bpf_helpers.h" #include "bpf_helpers.h"
#include "bpf_endian.h"
#define _htonl __builtin_bswap32 #define _htonl __builtin_bswap32
#define ERROR(ret) do {\ #define ERROR(ret) do {\
...@@ -38,6 +39,10 @@ struct vxlan_metadata { ...@@ -38,6 +39,10 @@ struct vxlan_metadata {
u32 gbp; u32 gbp;
}; };
struct erspan_metadata {
__be32 index;
};
SEC("gre_set_tunnel") SEC("gre_set_tunnel")
int _gre_set_tunnel(struct __sk_buff *skb) int _gre_set_tunnel(struct __sk_buff *skb)
{ {
...@@ -76,6 +81,63 @@ int _gre_get_tunnel(struct __sk_buff *skb) ...@@ -76,6 +81,63 @@ int _gre_get_tunnel(struct __sk_buff *skb)
return TC_ACT_OK; return TC_ACT_OK;
} }
SEC("erspan_set_tunnel")
int _erspan_set_tunnel(struct __sk_buff *skb)
{
struct bpf_tunnel_key key;
struct erspan_metadata md;
int ret;
__builtin_memset(&key, 0x0, sizeof(key));
key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
key.tunnel_id = 2;
key.tunnel_tos = 0;
key.tunnel_ttl = 64;
ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
if (ret < 0) {
ERROR(ret);
return TC_ACT_SHOT;
}
md.index = htonl(123);
ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
if (ret < 0) {
ERROR(ret);
return TC_ACT_SHOT;
}
return TC_ACT_OK;
}
SEC("erspan_get_tunnel")
int _erspan_get_tunnel(struct __sk_buff *skb)
{
char fmt[] = "key %d remote ip 0x%x erspan index 0x%x\n";
struct bpf_tunnel_key key;
struct erspan_metadata md;
u32 index;
int ret;
ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
if (ret < 0) {
ERROR(ret);
return TC_ACT_SHOT;
}
ret = bpf_skb_get_tunnel_opt(skb, &md, sizeof(md));
if (ret < 0) {
ERROR(ret);
return TC_ACT_SHOT;
}
index = bpf_ntohl(md.index);
bpf_trace_printk(fmt, sizeof(fmt),
key.tunnel_id, key.remote_ipv4, index);
return TC_ACT_OK;
}
SEC("vxlan_set_tunnel") SEC("vxlan_set_tunnel")
int _vxlan_set_tunnel(struct __sk_buff *skb) int _vxlan_set_tunnel(struct __sk_buff *skb)
{ {
...@@ -378,5 +440,4 @@ int _ip6ip6_get_tunnel(struct __sk_buff *skb) ...@@ -378,5 +440,4 @@ int _ip6ip6_get_tunnel(struct __sk_buff *skb)
return TC_ACT_OK; return TC_ACT_OK;
} }
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";
...@@ -32,6 +32,19 @@ function add_gre_tunnel { ...@@ -32,6 +32,19 @@ function add_gre_tunnel {
ip addr add dev $DEV 10.1.1.200/24 ip addr add dev $DEV 10.1.1.200/24
} }
function add_erspan_tunnel {
# in namespace
ip netns exec at_ns0 \
ip link add dev $DEV_NS type $TYPE seq key 2 local 172.16.1.100 remote 172.16.1.200 erspan 123
ip netns exec at_ns0 ip link set dev $DEV_NS up
ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
# out of namespace
ip link add dev $DEV type $TYPE external
ip link set dev $DEV up
ip addr add dev $DEV 10.1.1.200/24
}
function add_vxlan_tunnel { function add_vxlan_tunnel {
# Set static ARP entry here because iptables set-mark works # Set static ARP entry here because iptables set-mark works
# on L3 packet, as a result not applying to ARP packets, # on L3 packet, as a result not applying to ARP packets,
...@@ -99,6 +112,18 @@ function test_gre { ...@@ -99,6 +112,18 @@ function test_gre {
cleanup cleanup
} }
function test_erspan {
TYPE=erspan
DEV_NS=erspan00
DEV=erspan11
config_device
add_erspan_tunnel
attach_bpf $DEV erspan_set_tunnel erspan_get_tunnel
ping -c 1 10.1.1.100
ip netns exec at_ns0 ping -c 1 10.1.1.200
cleanup
}
function test_vxlan { function test_vxlan {
TYPE=vxlan TYPE=vxlan
DEV_NS=vxlan00 DEV_NS=vxlan00
...@@ -151,14 +176,18 @@ function cleanup { ...@@ -151,14 +176,18 @@ function cleanup {
ip link del gretap11 ip link del gretap11
ip link del vxlan11 ip link del vxlan11
ip link del geneve11 ip link del geneve11
ip link del erspan11
pkill tcpdump pkill tcpdump
pkill cat pkill cat
set -ex set -ex
} }
trap cleanup 0 2 3 6 9
cleanup cleanup
echo "Testing GRE tunnel..." echo "Testing GRE tunnel..."
test_gre test_gre
echo "Testing ERSPAN tunnel..."
test_erspan
echo "Testing VXLAN tunnel..." echo "Testing VXLAN tunnel..."
test_vxlan test_vxlan
echo "Testing GENEVE tunnel..." echo "Testing GENEVE tunnel..."
......
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