Subject: [RFC v2 net-next 04/12] libbpf: set xdp program in tx path

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

Existing libbpf APIs to set/get XDP attributes of a link were
written for rx path. This patch extends the new APIs introduced
in last patch. We need to set the tx_path parameter in struct
bpf_link_xdp_opts to attach the program in tx path.

Signed-off-by: Prashant Bhole <[email-masked]>
---
 tools/lib/bpf/libbpf.h  |  4 ++++
 tools/lib/bpf/netlink.c | 36 ++++++++++++++++++++++++++++++------
 2 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 8178fd5a1e8f..c073d0eb3bf5 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -449,6 +449,7 @@ struct bpf_link_xdp_opts {
 	__u32 flags;
 	__u32 prog_id;
 	int prog_fd;
+	bool tx_path;
 };
 
 /*
@@ -459,13 +460,16 @@ struct bpf_link_xdp_opts {
  *	- link_info
  *	- link_info_sz
  *	- flags
+ *	- tx_path
  *
  * BPF_LINK_SET_XDP_FD uses fields:
  *	- flags
+ *	- tx_path
  *
  * BPF_LINK_SET_XDP_FD uses fields:
  *	- prog_fd
  *	- flags
+ *	- tx_path
  */
 enum bpf_link_cmd {
 	BPF_LINK_GET_XDP_INFO,
diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index 1274b540a9ad..c839495e8f03 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -133,6 +133,7 @@ static int __bpf_set_link_xdp_fd(int ifindex, struct bpf_link_xdp_opts *opts)
 {
 	int fd = opts->prog_fd;
 	__u32 flags = opts->flags;
+	bool tx = opts->tx_path;
 	int sock, seq = 0, ret;
 	struct nlattr *nla, *nla_xdp;
 	struct {
@@ -158,7 +159,8 @@ static int __bpf_set_link_xdp_fd(int ifindex, struct bpf_link_xdp_opts *opts)
 	/* started nested attribute for XDP */
 	nla = (struct nlattr *)(((char *)&req)
 				+ NLMSG_ALIGN(req.nh.nlmsg_len));
-	nla->nla_type = NLA_F_NESTED | IFLA_XDP;
+	nla->nla_type = NLA_F_NESTED;
+	nla->nla_type |= tx ? IFLA_XDP_TX : IFLA_XDP;
 	nla->nla_len = NLA_HDRLEN;
 
 	/* add XDP fd */
@@ -196,6 +198,7 @@ int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
 
 	opts.prog_fd = fd;
 	opts.flags = flags;
+	/* opts.tx_path is already 0 */
 
 	return bpf_set_link_opts(ifindex, &opts, BPF_LINK_SET_XDP_FD);
 }
@@ -215,20 +218,20 @@ static int __dump_link_nlmsg(struct nlmsghdr *nlh,
 	return dump_link_nlmsg(cookie, ifi, tb);
 }
 
-static int get_xdp_info(void *cookie, void *msg, struct nlattr **tb)
+static int __get_xdp_info(void *cookie, void *msg, struct nlattr **tb, bool tx)
 {
 	struct nlattr *xdp_tb[IFLA_XDP_MAX + 1];
 	struct xdp_id_md *xdp_id = cookie;
 	struct ifinfomsg *ifinfo = msg;
+	struct nlattr *attr;
 	int ret;
 
 	if (xdp_id->ifindex && xdp_id->ifindex != ifinfo->ifi_index)
 		return 0;
 
-	if (!tb[IFLA_XDP])
-		return 0;
+	attr = tx ? tb[IFLA_XDP_TX] : tb[IFLA_XDP];
 
-	ret = libbpf_nla_parse_nested(xdp_tb, IFLA_XDP_MAX, tb[IFLA_XDP], NULL);
+	ret = libbpf_nla_parse_nested(xdp_tb, IFLA_XDP_MAX, attr, NULL);
 	if (ret)
 		return ret;
 
@@ -260,12 +263,27 @@ static int get_xdp_info(void *cookie, void *msg, struct nlattr **tb)
 	return 0;
 }
 
+static int get_xdp_tx_info(void *cookie, void *msg, struct nlattr **tb)
+{
+	if (!tb[IFLA_XDP_TX])
+		return 0;
+	return __get_xdp_info(cookie, msg, tb, true);
+}
+
+static int get_xdp_info(void *cookie, void *msg, struct nlattr **tb)
+{
+	if (!tb[IFLA_XDP])
+		return 0;
+	return __get_xdp_info(cookie, msg, tb, false);
+}
+
 static int __bpf_get_link_xdp_info(int ifindex, struct bpf_link_xdp_opts *opts)
 {
 	struct xdp_link_info *info = opts->link_info;
 	size_t info_size = opts->link_info_sz;
 	struct xdp_id_md xdp_id = {};
 	__u32 flags = opts->flags;
+	int tx = opts->tx_path;
 	int sock, ret;
 	__u32 nl_pid;
 	__u32 mask;
@@ -286,7 +304,11 @@ static int __bpf_get_link_xdp_info(int ifindex, struct bpf_link_xdp_opts *opts)
 	xdp_id.ifindex = ifindex;
 	xdp_id.flags = flags;
 
-	ret = libbpf_nl_get_link(sock, nl_pid, get_xdp_info, &xdp_id);
+	if (tx)
+		ret = libbpf_nl_get_link(sock, nl_pid, get_xdp_tx_info,
+					 &xdp_id);
+	else
+		ret = libbpf_nl_get_link(sock, nl_pid, get_xdp_info, &xdp_id);
 	if (!ret) {
 		size_t sz = min(info_size, sizeof(xdp_id.info));
 
@@ -306,6 +328,7 @@ int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
 	opts.link_info = info;
 	opts.link_info_sz = info_size;
 	opts.flags = flags;
+	/* opts.tx_path is already 0 */
 
 	return bpf_get_link_opts(ifindex, &opts, BPF_LINK_GET_XDP_INFO);
 }
@@ -502,6 +525,7 @@ int bpf_get_link_opts(int ifindex, struct bpf_link_xdp_opts *opts,
 		int ret;
 
 		tmp_opts.flags = opts->flags;
+		tmp_opts.tx_path = opts->tx_path;
 		tmp_opts.link_info = &link_info;
 		tmp_opts.link_info_sz = sizeof(link_info);
 		ret = __bpf_get_link_xdp_info(ifindex, &tmp_opts);
-- 
2.21.0