netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: William Tu <u9012063@gmail.com>
To: netdev@vger.kernel.org
Subject: [PATCH net 3/3] sample/bpf: fix erspan metadata
Date: Mon,  5 Feb 2018 13:35:36 -0800	[thread overview]
Message-ID: <1517866536-69908-4-git-send-email-u9012063@gmail.com> (raw)
In-Reply-To: <1517866536-69908-1-git-send-email-u9012063@gmail.com>

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>
---
 samples/bpf/tcbpf2_kern.c      | 41 ++++++++++++++++-------------------------
 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 @@
 #include <uapi/linux/tcp.h>
 #include <uapi/linux/filter.h>
 #include <uapi/linux/pkt_cls.h>
+#include <uapi/linux/erspan.h>
 #include <net/ipv6.h>
 #include "bpf_helpers.h"
 #include "bpf_endian.h"
@@ -35,24 +36,10 @@ 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 {
-	union {
-		__be32 index;
-		struct erspan_md2 md2;
-	} u;
-	int version;
-};
-
 SEC("gre_set_tunnel")
 int _gre_set_tunnel(struct __sk_buff *skb)
 {
@@ -156,13 +143,15 @@ int _erspan_set_tunnel(struct __sk_buff *skb)
 	__builtin_memset(&md, 0, sizeof(md));
 #ifdef ERSPAN_V1
 	md.version = 1;
-	md.u.index = htonl(123);
+	md.u.index = bpf_htonl(123);
 #else
 	u8 direction = 1;
-	u16 hwid = 7;
+	u8 hwid = 7;
 
 	md.version = 2;
-	md.u.md2.flags = htons((direction << 3) | (hwid << 4));
+	md.u.md2.dir = direction;
+	md.u.md2.hwid = hwid & 0xf;
+	md.u.md2.hwid_upper = (hwid >> 4) & 0x3;
 #endif
 
 	ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
@@ -207,9 +196,9 @@ int _erspan_get_tunnel(struct __sk_buff *skb)
 	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));
+			 md.u.md2.dir,
+			 (md.u.md2.hwid_upper << 4) + md.u.md2.hwid,
+			 bpf_ntohl(md.u.md2.timestamp));
 #endif
 
 	return TC_ACT_OK;
@@ -242,10 +231,12 @@ int _ip4ip6erspan_set_tunnel(struct __sk_buff *skb)
 	md.version = 1;
 #else
 	u8 direction = 0;
-	u16 hwid = 17;
+	u8 hwid = 17;
 
 	md.version = 2;
-	md.u.md2.flags = htons((direction << 3) | (hwid << 4));
+	md.u.md2.dir = direction;
+	md.u.md2.hwid = hwid & 0xf;
+	md.u.md2.hwid_upper = (hwid >> 4) & 0x3;
 #endif
 
 	ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
@@ -290,9 +281,9 @@ int _ip4ip6erspan_get_tunnel(struct __sk_buff *skb)
 	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));
+			 md.u.md2.dir,
+			 (md.u.md2.hwid_upper << 4) + md.u.md2.hwid,
+			 bpf_ntohl(md.u.md2.timestamp));
 #endif
 
 	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 {
 		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
+		erspan_ver 2 erspan_dir egress 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
@@ -97,7 +97,7 @@ function add_ip6erspan_tunnel {
 		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
+		erspan_ver 2 erspan_dir egress 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
-- 
2.7.4

  parent reply	other threads:[~2018-02-05 21:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-05 21:35 [PATCH net 0/3] net: erspan fixes William Tu
2018-02-05 21:35 ` [PATCH net 1/3] net: erspan: fix metadata extraction William Tu
2018-02-05 21:35 ` [PATCH net 2/3] net: erspan: fix erspan config overwrite William Tu
2018-02-05 21:35 ` William Tu [this message]
2018-02-06 16:33 ` [PATCH net 0/3] net: erspan fixes David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1517866536-69908-4-git-send-email-u9012063@gmail.com \
    --to=u9012063@gmail.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).