samples/bpf: add erspan v2 sample code
Extend the existing tests for ipv4 ipv6 erspan version 2. Signed-off-by: William Tu <u9012063@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Родитель
94d7d8f292
Коммит
ac80c2a165
|
@ -35,12 +35,22 @@ struct geneve_opt {
|
|||
u8 opt_data[8]; /* hard-coded to 8 byte */
|
||||
};
|
||||
|
||||
struct erspan_md2 {
|
||||
__be32 timestamp;
|
||||
__be16 sgt;
|
||||
__be16 flags;
|
||||
};
|
||||
|
||||
struct vxlan_metadata {
|
||||
u32 gbp;
|
||||
};
|
||||
|
||||
struct erspan_metadata {
|
||||
__be32 index;
|
||||
union {
|
||||
__be32 index;
|
||||
struct erspan_md2 md2;
|
||||
} u;
|
||||
int version;
|
||||
};
|
||||
|
||||
SEC("gre_set_tunnel")
|
||||
|
@ -143,7 +153,18 @@ int _erspan_set_tunnel(struct __sk_buff *skb)
|
|||
return TC_ACT_SHOT;
|
||||
}
|
||||
|
||||
md.index = htonl(123);
|
||||
__builtin_memset(&md, 0, sizeof(md));
|
||||
#ifdef ERSPAN_V1
|
||||
md.version = 1;
|
||||
md.u.index = htonl(123);
|
||||
#else
|
||||
u8 direction = 1;
|
||||
u16 hwid = 7;
|
||||
|
||||
md.version = 2;
|
||||
md.u.md2.flags = htons((direction << 3) | (hwid << 4));
|
||||
#endif
|
||||
|
||||
ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
|
||||
if (ret < 0) {
|
||||
ERROR(ret);
|
||||
|
@ -156,7 +177,7 @@ int _erspan_set_tunnel(struct __sk_buff *skb)
|
|||
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";
|
||||
char fmt[] = "key %d remote ip 0x%x erspan version %d\n";
|
||||
struct bpf_tunnel_key key;
|
||||
struct erspan_metadata md;
|
||||
u32 index;
|
||||
|
@ -174,9 +195,22 @@ int _erspan_get_tunnel(struct __sk_buff *skb)
|
|||
return TC_ACT_SHOT;
|
||||
}
|
||||
|
||||
index = bpf_ntohl(md.index);
|
||||
bpf_trace_printk(fmt, sizeof(fmt),
|
||||
key.tunnel_id, key.remote_ipv4, index);
|
||||
key.tunnel_id, key.remote_ipv4, md.version);
|
||||
|
||||
#ifdef ERSPAN_V1
|
||||
char fmt2[] = "\tindex %x\n";
|
||||
|
||||
index = bpf_ntohl(md.u.index);
|
||||
bpf_trace_printk(fmt2, sizeof(fmt2), index);
|
||||
#else
|
||||
char fmt2[] = "\tdirection %d hwid %x timestamp %u\n";
|
||||
|
||||
bpf_trace_printk(fmt2, sizeof(fmt2),
|
||||
(ntohs(md.u.md2.flags) >> 3) & 0x1,
|
||||
(ntohs(md.u.md2.flags) >> 4) & 0x3f,
|
||||
bpf_ntohl(md.u.md2.timestamp));
|
||||
#endif
|
||||
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
|
@ -201,7 +235,19 @@ int _ip4ip6erspan_set_tunnel(struct __sk_buff *skb)
|
|||
return TC_ACT_SHOT;
|
||||
}
|
||||
|
||||
md.index = htonl(123);
|
||||
__builtin_memset(&md, 0, sizeof(md));
|
||||
|
||||
#ifdef ERSPAN_V1
|
||||
md.u.index = htonl(123);
|
||||
md.version = 1;
|
||||
#else
|
||||
u8 direction = 0;
|
||||
u16 hwid = 17;
|
||||
|
||||
md.version = 2;
|
||||
md.u.md2.flags = htons((direction << 3) | (hwid << 4));
|
||||
#endif
|
||||
|
||||
ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
|
||||
if (ret < 0) {
|
||||
ERROR(ret);
|
||||
|
@ -214,7 +260,7 @@ int _ip4ip6erspan_set_tunnel(struct __sk_buff *skb)
|
|||
SEC("ip4ip6erspan_get_tunnel")
|
||||
int _ip4ip6erspan_get_tunnel(struct __sk_buff *skb)
|
||||
{
|
||||
char fmt[] = "key %d remote ip6 ::%x erspan index 0x%x\n";
|
||||
char fmt[] = "ip6erspan get key %d remote ip6 ::%x erspan version %d\n";
|
||||
struct bpf_tunnel_key key;
|
||||
struct erspan_metadata md;
|
||||
u32 index;
|
||||
|
@ -232,9 +278,22 @@ int _ip4ip6erspan_get_tunnel(struct __sk_buff *skb)
|
|||
return TC_ACT_SHOT;
|
||||
}
|
||||
|
||||
index = bpf_ntohl(md.index);
|
||||
bpf_trace_printk(fmt, sizeof(fmt),
|
||||
key.tunnel_id, key.remote_ipv6[0], index);
|
||||
key.tunnel_id, key.remote_ipv4, md.version);
|
||||
|
||||
#ifdef ERSPAN_V1
|
||||
char fmt2[] = "\tindex %x\n";
|
||||
|
||||
index = bpf_ntohl(md.u.index);
|
||||
bpf_trace_printk(fmt2, sizeof(fmt2), index);
|
||||
#else
|
||||
char fmt2[] = "\tdirection %d hwid %x timestamp %u\n";
|
||||
|
||||
bpf_trace_printk(fmt2, sizeof(fmt2),
|
||||
(ntohs(md.u.md2.flags) >> 3) & 0x1,
|
||||
(ntohs(md.u.md2.flags) >> 4) & 0x3f,
|
||||
bpf_ntohl(md.u.md2.timestamp));
|
||||
#endif
|
||||
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
|
|
|
@ -59,8 +59,17 @@ function add_ip6gretap_tunnel {
|
|||
|
||||
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
|
||||
if [ "$1" == "v1" ]; then
|
||||
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_ver 1 erspan 123
|
||||
else
|
||||
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_ver 2 erspan_dir 1 erspan_hwid 3
|
||||
fi
|
||||
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
|
||||
|
||||
|
@ -79,10 +88,17 @@ function add_ip6erspan_tunnel {
|
|||
ip link set dev veth1 up
|
||||
|
||||
# in namespace
|
||||
ip netns exec at_ns0 \
|
||||
ip link add dev $DEV_NS type $TYPE seq key 2 erspan 123 \
|
||||
local ::11 remote ::22
|
||||
|
||||
if [ "$1" == "v1" ]; then
|
||||
ip netns exec at_ns0 \
|
||||
ip link add dev $DEV_NS type $TYPE seq key 2 \
|
||||
local ::11 remote ::22 \
|
||||
erspan_ver 1 erspan 123
|
||||
else
|
||||
ip netns exec at_ns0 \
|
||||
ip link add dev $DEV_NS type $TYPE seq key 2 \
|
||||
local ::11 remote ::22 \
|
||||
erspan_ver 2 erspan_dir 1 erspan_hwid 7
|
||||
fi
|
||||
ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
|
||||
ip netns exec at_ns0 ip link set dev $DEV_NS up
|
||||
|
||||
|
@ -199,7 +215,7 @@ function test_erspan {
|
|||
DEV_NS=erspan00
|
||||
DEV=erspan11
|
||||
config_device
|
||||
add_erspan_tunnel
|
||||
add_erspan_tunnel $1
|
||||
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
|
||||
|
@ -211,7 +227,7 @@ function test_ip6erspan {
|
|||
DEV_NS=ip6erspan00
|
||||
DEV=ip6erspan11
|
||||
config_device
|
||||
add_ip6erspan_tunnel
|
||||
add_ip6erspan_tunnel $1
|
||||
attach_bpf $DEV ip4ip6erspan_set_tunnel ip4ip6erspan_get_tunnel
|
||||
ping6 -c 3 ::11
|
||||
ip netns exec at_ns0 ping -c 1 10.1.1.200
|
||||
|
@ -288,9 +304,11 @@ test_ip6gre
|
|||
echo "Testing IP6GRETAP tunnel..."
|
||||
test_ip6gretap
|
||||
echo "Testing ERSPAN tunnel..."
|
||||
test_erspan
|
||||
test_erspan v1
|
||||
test_erspan v2
|
||||
echo "Testing IP6ERSPAN tunnel..."
|
||||
test_ip6erspan
|
||||
test_ip6erspan v1
|
||||
test_ip6erspan v2
|
||||
echo "Testing VXLAN tunnel..."
|
||||
test_vxlan
|
||||
echo "Testing GENEVE tunnel..."
|
||||
|
|
Загрузка…
Ссылка в новой задаче