diff options
| author | William Tu <u9012063@gmail.com> | 2018-02-05 16:35:36 -0500 |
|---|---|---|
| committer | David S. Miller <davem@davemloft.net> | 2018-02-06 11:32:49 -0500 |
| commit | 9c33ca4317c81d9a5d030bbc60aeb2d16edf172b (patch) | |
| tree | 9f6a56f4d4d7226b735237e1e99510d2ac194b75 | |
| parent | 39f57f6799cdd437277122d4cd1c470c08f527c0 (diff) | |
sample/bpf: fix erspan metadata
The commit c69de58ba84f ("net: erspan: use bitfield instead of
mask and offset") changes the erspan header to use bitfield, and
commit d350a823020e ("net: erspan: create erspan metadata uapi header")
creates a uapi header file. The above two commit breaks the current
erspan test. This patch fixes it by adapting the above two changes.
Fixes: ac80c2a165af ("samples/bpf: add erspan v2 sample code")
Fixes: ef88f89c830f ("samples/bpf: extend test_tunnel_bpf.sh with ERSPAN")
Signed-off-by: William Tu <u9012063@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
| -rw-r--r-- | samples/bpf/tcbpf2_kern.c | 41 | ||||
| -rwxr-xr-x | samples/bpf/test_tunnel_bpf.sh | 4 |
2 files changed, 18 insertions, 27 deletions
diff --git a/samples/bpf/tcbpf2_kern.c b/samples/bpf/tcbpf2_kern.c index f6bbf8f50da3..efdc16d195ff 100644 --- a/samples/bpf/tcbpf2_kern.c +++ b/samples/bpf/tcbpf2_kern.c | |||
| @@ -15,6 +15,7 @@ | |||
| 15 | #include <uapi/linux/tcp.h> | 15 | #include <uapi/linux/tcp.h> |
| 16 | #include <uapi/linux/filter.h> | 16 | #include <uapi/linux/filter.h> |
| 17 | #include <uapi/linux/pkt_cls.h> | 17 | #include <uapi/linux/pkt_cls.h> |
| 18 | #include <uapi/linux/erspan.h> | ||
| 18 | #include <net/ipv6.h> | 19 | #include <net/ipv6.h> |
| 19 | #include "bpf_helpers.h" | 20 | #include "bpf_helpers.h" |
| 20 | #include "bpf_endian.h" | 21 | #include "bpf_endian.h" |
| @@ -35,24 +36,10 @@ struct geneve_opt { | |||
| 35 | u8 opt_data[8]; /* hard-coded to 8 byte */ | 36 | u8 opt_data[8]; /* hard-coded to 8 byte */ |
| 36 | }; | 37 | }; |
| 37 | 38 | ||
| 38 | struct erspan_md2 { | ||
| 39 | __be32 timestamp; | ||
| 40 | __be16 sgt; | ||
| 41 | __be16 flags; | ||
| 42 | }; | ||
| 43 | |||
| 44 | struct vxlan_metadata { | 39 | struct vxlan_metadata { |
| 45 | u32 gbp; | 40 | u32 gbp; |
| 46 | }; | 41 | }; |
| 47 | 42 | ||
| 48 | struct erspan_metadata { | ||
| 49 | union { | ||
| 50 | __be32 index; | ||
| 51 | struct erspan_md2 md2; | ||
| 52 | } u; | ||
| 53 | int version; | ||
| 54 | }; | ||
| 55 | |||
| 56 | SEC("gre_set_tunnel") | 43 | SEC("gre_set_tunnel") |
| 57 | int _gre_set_tunnel(struct __sk_buff *skb) | 44 | int _gre_set_tunnel(struct __sk_buff *skb) |
| 58 | { | 45 | { |
| @@ -156,13 +143,15 @@ int _erspan_set_tunnel(struct __sk_buff *skb) | |||
| 156 | __builtin_memset(&md, 0, sizeof(md)); | 143 | __builtin_memset(&md, 0, sizeof(md)); |
| 157 | #ifdef ERSPAN_V1 | 144 | #ifdef ERSPAN_V1 |
| 158 | md.version = 1; | 145 | md.version = 1; |
| 159 | md.u.index = htonl(123); | 146 | md.u.index = bpf_htonl(123); |
| 160 | #else | 147 | #else |
| 161 | u8 direction = 1; | 148 | u8 direction = 1; |
| 162 | u16 hwid = 7; | 149 | u8 hwid = 7; |
| 163 | 150 | ||
| 164 | md.version = 2; | 151 | md.version = 2; |
| 165 | md.u.md2.flags = htons((direction << 3) | (hwid << 4)); | 152 | md.u.md2.dir = direction; |
| 153 | md.u.md2.hwid = hwid & 0xf; | ||
| 154 | md.u.md2.hwid_upper = (hwid >> 4) & 0x3; | ||
| 166 | #endif | 155 | #endif |
| 167 | 156 | ||
| 168 | ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md)); | 157 | ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md)); |
| @@ -207,9 +196,9 @@ int _erspan_get_tunnel(struct __sk_buff *skb) | |||
| 207 | char fmt2[] = "\tdirection %d hwid %x timestamp %u\n"; | 196 | char fmt2[] = "\tdirection %d hwid %x timestamp %u\n"; |
| 208 | 197 | ||
| 209 | bpf_trace_printk(fmt2, sizeof(fmt2), | 198 | bpf_trace_printk(fmt2, sizeof(fmt2), |
| 210 | (ntohs(md.u.md2.flags) >> 3) & 0x1, | 199 | md.u.md2.dir, |
| 211 | (ntohs(md.u.md2.flags) >> 4) & 0x3f, | 200 | (md.u.md2.hwid_upper << 4) + md.u.md2.hwid, |
| 212 | bpf_ntohl(md.u.md2.timestamp)); | 201 | bpf_ntohl(md.u.md2.timestamp)); |
| 213 | #endif | 202 | #endif |
| 214 | 203 | ||
| 215 | return TC_ACT_OK; | 204 | return TC_ACT_OK; |
| @@ -242,10 +231,12 @@ int _ip4ip6erspan_set_tunnel(struct __sk_buff *skb) | |||
| 242 | md.version = 1; | 231 | md.version = 1; |
| 243 | #else | 232 | #else |
| 244 | u8 direction = 0; | 233 | u8 direction = 0; |
| 245 | u16 hwid = 17; | 234 | u8 hwid = 17; |
| 246 | 235 | ||
| 247 | md.version = 2; | 236 | md.version = 2; |
| 248 | md.u.md2.flags = htons((direction << 3) | (hwid << 4)); | 237 | md.u.md2.dir = direction; |
| 238 | md.u.md2.hwid = hwid & 0xf; | ||
| 239 | md.u.md2.hwid_upper = (hwid >> 4) & 0x3; | ||
| 249 | #endif | 240 | #endif |
| 250 | 241 | ||
| 251 | ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md)); | 242 | ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md)); |
| @@ -290,9 +281,9 @@ int _ip4ip6erspan_get_tunnel(struct __sk_buff *skb) | |||
| 290 | char fmt2[] = "\tdirection %d hwid %x timestamp %u\n"; | 281 | char fmt2[] = "\tdirection %d hwid %x timestamp %u\n"; |
| 291 | 282 | ||
| 292 | bpf_trace_printk(fmt2, sizeof(fmt2), | 283 | bpf_trace_printk(fmt2, sizeof(fmt2), |
| 293 | (ntohs(md.u.md2.flags) >> 3) & 0x1, | 284 | md.u.md2.dir, |
| 294 | (ntohs(md.u.md2.flags) >> 4) & 0x3f, | 285 | (md.u.md2.hwid_upper << 4) + md.u.md2.hwid, |
| 295 | bpf_ntohl(md.u.md2.timestamp)); | 286 | bpf_ntohl(md.u.md2.timestamp)); |
| 296 | #endif | 287 | #endif |
| 297 | 288 | ||
| 298 | return TC_ACT_OK; | 289 | return TC_ACT_OK; |
diff --git a/samples/bpf/test_tunnel_bpf.sh b/samples/bpf/test_tunnel_bpf.sh index ae7f7c38309b..43ce049996ee 100755 --- a/samples/bpf/test_tunnel_bpf.sh +++ b/samples/bpf/test_tunnel_bpf.sh | |||
| @@ -68,7 +68,7 @@ function add_erspan_tunnel { | |||
| 68 | ip netns exec at_ns0 \ | 68 | ip netns exec at_ns0 \ |
| 69 | ip link add dev $DEV_NS type $TYPE seq key 2 \ | 69 | ip link add dev $DEV_NS type $TYPE seq key 2 \ |
| 70 | local 172.16.1.100 remote 172.16.1.200 \ | 70 | local 172.16.1.100 remote 172.16.1.200 \ |
| 71 | erspan_ver 2 erspan_dir 1 erspan_hwid 3 | 71 | erspan_ver 2 erspan_dir egress erspan_hwid 3 |
| 72 | fi | 72 | fi |
| 73 | ip netns exec at_ns0 ip link set dev $DEV_NS up | 73 | ip netns exec at_ns0 ip link set dev $DEV_NS up |
| 74 | ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24 | 74 | ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24 |
| @@ -97,7 +97,7 @@ function add_ip6erspan_tunnel { | |||
| 97 | ip netns exec at_ns0 \ | 97 | ip netns exec at_ns0 \ |
| 98 | ip link add dev $DEV_NS type $TYPE seq key 2 \ | 98 | ip link add dev $DEV_NS type $TYPE seq key 2 \ |
| 99 | local ::11 remote ::22 \ | 99 | local ::11 remote ::22 \ |
| 100 | erspan_ver 2 erspan_dir 1 erspan_hwid 7 | 100 | erspan_ver 2 erspan_dir egress erspan_hwid 7 |
| 101 | fi | 101 | fi |
| 102 | ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24 | 102 | ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24 |
| 103 | ip netns exec at_ns0 ip link set dev $DEV_NS up | 103 | ip netns exec at_ns0 ip link set dev $DEV_NS up |
