Subject: [RFC v2 net-next 05/12] samples/bpf: xdp1, add XDP tx support

From: Prashant Bhole <[email-masked]>
Date: Thu, 26 Dec 2019 11:31:53 +0900

xdp1 and xdp2 now accept -T flag to set XDP program in the tx path.
The user program uses new APIs bpf_get_link_opts/bpf_set_link_opts.

Signed-off-by: Prashant Bhole <[email-masked]>
---
 samples/bpf/xdp1_user.c | 42 +++++++++++++++++++++++++++++++----------
 1 file changed, 32 insertions(+), 10 deletions(-)

diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c
index 3e553eed95a7..d8e27d4f785c 100644
--- a/samples/bpf/xdp1_user.c
+++ b/samples/bpf/xdp1_user.c
@@ -21,21 +21,31 @@
 static int ifindex;
 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
 static __u32 prog_id;
+static bool tx_path;
 
 static void int_exit(int sig)
 {
-	__u32 curr_prog_id = 0;
+	struct bpf_link_xdp_opts xdp_opts = {};
+	int err;
+
+	xdp_opts.flags = xdp_flags;
+	xdp_opts.tx_path = tx_path;
 
-	if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
-		printf("bpf_get_link_xdp_id failed\n");
+	err = bpf_get_link_opts(ifindex, &xdp_opts, BPF_LINK_GET_XDP_ID);
+	if (err) {
+		printf("getting xdp program id failed\n");
 		exit(1);
 	}
-	if (prog_id == curr_prog_id)
-		bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
-	else if (!curr_prog_id)
+
+	if (prog_id == xdp_opts.prog_id) {
+		xdp_opts.prog_fd = -1;
+		err = bpf_set_link_opts(ifindex, &xdp_opts,
+					BPF_LINK_SET_XDP_FD);
+	} else if (!xdp_opts.prog_id) {
 		printf("couldn't find a prog id on a given interface\n");
-	else
+	} else {
 		printf("program on interface changed, not removing\n");
+	}
 	exit(0);
 }
 
@@ -73,7 +83,8 @@ static void usage(const char *prog)
 		"OPTS:\n"
 		"    -S    use skb-mode\n"
 		"    -N    enforce native mode\n"
-		"    -F    force loading prog\n",
+		"    -F    force loading prog\n"
+		"    -T    TX path prog\n",
 		prog);
 }
 
@@ -83,9 +94,10 @@ int main(int argc, char **argv)
 	struct bpf_prog_load_attr prog_load_attr = {
 		.prog_type	= BPF_PROG_TYPE_XDP,
 	};
+	struct bpf_link_xdp_opts xdp_opts = {0};
 	struct bpf_prog_info info = {};
 	__u32 info_len = sizeof(info);
-	const char *optstr = "FSN";
+	const char *optstr = "FSNT";
 	int prog_fd, map_fd, opt;
 	struct bpf_object *obj;
 	struct bpf_map *map;
@@ -103,6 +115,9 @@ int main(int argc, char **argv)
 		case 'F':
 			xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
 			break;
+		case 'T':
+			tx_path = true;
+			break;
 		default:
 			usage(basename(argv[0]));
 			return 1;
@@ -127,6 +142,8 @@ int main(int argc, char **argv)
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 	prog_load_attr.file = filename;
+	if (tx_path)
+		prog_load_attr.expected_attach_type = BPF_XDP_EGRESS;
 
 	if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
 		return 1;
@@ -146,7 +163,12 @@ int main(int argc, char **argv)
 	signal(SIGINT, int_exit);
 	signal(SIGTERM, int_exit);
 
-	if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
+	xdp_opts.prog_fd = prog_fd;
+	xdp_opts.flags = xdp_flags;
+	xdp_opts.tx_path = tx_path;
+
+	err = bpf_set_link_opts(ifindex, &xdp_opts, BPF_LINK_SET_XDP_FD);
+	if (err < 0) {
 		printf("link set xdp fd failed\n");
 		return 1;
 	}
-- 
2.21.0