Skip to contentSkip to conversation

BPF infrastructure enhancements

RFC New Stephen Hemminger 1 participant 6 messages Jan 5, 2024 Network drivers

lore
Share & tools
lore
#0 lore raw

cover BPF infrastructure enhancements

While investigating the BPF program load in TAP device found a number of minor issues that should be addressed.

Stephen Hemminger (5):
tap: move forward declaration of bpf_load
tap: remove unnecessary bzero() calls in bpf api
tap: remove unnecessary cast in call to bpf_load
tap: get errors from kernel on bpf load failure
tap: stop "vendoring" linux bpf header

 drivers/net/tap/bpf/bpf_extract.py |   1 -
 drivers/net/tap/tap_bpf.h          | 121 -----------------------------
 drivers/net/tap/tap_bpf_api.c      |  73 +++++++++++------
 drivers/net/tap/tap_bpf_insns.h    |   1 -
 drivers/net/tap/tap_flow.c         |  16 ++--
 drivers/net/tap/tap_flow.h         |   4 +-
 6 files changed, 60 insertions(+), 156 deletions(-)
 delete mode 100644 drivers/net/tap/tap_bpf.h
Signature · 1 line
2.43.0
#1 lore raw

RFC 1/5 tap: move forward declaration of bpf_load

The local function bpf_load forward declaration should be in the one file using it.

---
 drivers/net/tap/tap_bpf.h     | 3 ---
 drivers/net/tap/tap_bpf_api.c | 3 +++
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/net/tap/tap_bpf.h +0 -3
index 0d38bc111fe0..aa5a733525e1 100644
@@ -115,7 +115,4 @@ enum {
 	BPF_MAP_ID_SIMPLE,
 };
 
-static int bpf_load(enum bpf_prog_type type, const struct bpf_insn *insns,
-		size_t insns_cnt, const char *license);
-
 #endif /* __TAP_BPF_H__ */
drivers/net/tap/tap_bpf_api.c +3 -0
index 15283f8917ed..a6adec855dda 100644
@@ -15,6 +15,9 @@
 #include <tap_bpf.h>
 #include <tap_bpf_insns.h>
 
+static int bpf_load(enum bpf_prog_type type, const struct bpf_insn *insns,
+		    size_t insns_cnt, const char *license);
+
 /**
  * Load BPF program (section cls_q) into the kernel and return a bpf fd
  *
Signature · 1 line
2.43.0
#2 lore raw

RFC 2/5 tap: remove unnecessary bzero() calls in bpf api

In reply to Stephen Hemminger

The structures are already fully initialized, bzero() or memset is redundant.

---
 drivers/net/tap/tap_bpf_api.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/net/tap/tap_bpf_api.c +0 -4
index a6adec855dda..d176da0802eb 100644
@@ -120,7 +120,6 @@ static int bpf_load(enum bpf_prog_type type,
 {
 	union bpf_attr attr = {};
 
-	bzero(&attr, sizeof(attr));
 	attr.prog_type = type;
 	attr.insn_cnt = (__u32)insns_cnt;
 	attr.insns = ptr_to_u64(insns);
@@ -153,7 +152,6 @@ int tap_flow_bpf_rss_map_create(unsigned int key_size,
 {
 	union bpf_attr attr = {};
 
-	bzero(&attr, sizeof(attr));
 	attr.map_type    = BPF_MAP_TYPE_HASH;
 	attr.key_size    = key_size;
 	attr.value_size  = value_size;
@@ -181,8 +179,6 @@ int tap_flow_bpf_update_rss_elem(int fd, void *key, void *value)
 {
 	union bpf_attr attr = {};
 
-	bzero(&attr, sizeof(attr));
-
 	attr.map_type = BPF_MAP_TYPE_HASH;
 	attr.map_fd = fd;
 	attr.key = ptr_to_u64(key);
Signature · 1 line
2.43.0
#3 lore raw

RFC 3/5 tap: remove unnecessary cast in call to bpf_load

In reply to Stephen Hemminger

The callers already have the correct data type.

---
 drivers/net/tap/tap_bpf_api.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/net/tap/tap_bpf_api.c +4 -6
index d176da0802eb..c754c167a764 100644
@@ -32,9 +32,8 @@ int tap_flow_bpf_cls_q(__u32 queue_idx)
 	cls_q_insns[1].imm = queue_idx;
 
 	return bpf_load(BPF_PROG_TYPE_SCHED_CLS,
-		(struct bpf_insn *)cls_q_insns,
-		RTE_DIM(cls_q_insns),
-		"Dual BSD/GPL");
+			cls_q_insns, RTE_DIM(cls_q_insns),
+			"Dual BSD/GPL");
 }
 
 /**
@@ -55,9 +54,8 @@ int tap_flow_bpf_calc_l3_l4_hash(__u32 key_idx, int map_fd)
 	l3_l4_hash_insns[9].imm = map_fd;
 
 	return bpf_load(BPF_PROG_TYPE_SCHED_ACT,
-		(struct bpf_insn *)l3_l4_hash_insns,
-		RTE_DIM(l3_l4_hash_insns),
-		"Dual BSD/GPL");
+			l3_l4_hash_insns, RTE_DIM(l3_l4_hash_insns),
+			"Dual BSD/GPL");
 }
 
 /**
Signature · 1 line
2.43.0
#4 lore raw

RFC 4/5 tap: get errors from kernel on bpf load failure

In reply to Stephen Hemminger

The bpf load kernel API can provide some useful diagnostics on failure.

---
 drivers/net/tap/tap_bpf_api.c | 44 +++++++++++++++++++++++++++--------
 drivers/net/tap/tap_flow.c    | 16 ++++++++-----
 drivers/net/tap/tap_flow.h    |  4 ++--
 3 files changed, 46 insertions(+), 18 deletions(-)
drivers/net/tap/tap_bpf_api.c +34 -10
index c754c167a764..29223b7f0ea7 100644
@@ -15,8 +15,10 @@
 #include <tap_bpf.h>
 #include <tap_bpf_insns.h>
 
-static int bpf_load(enum bpf_prog_type type, const struct bpf_insn *insns,
-		    size_t insns_cnt, const char *license);
+static int bpf_load(enum bpf_prog_type type,
+		    const struct bpf_insn *insns, size_t insns_cnt,
+		    char *log_buf, size_t log_size,
+		    const char *license);
 
 /**
  * Load BPF program (section cls_q) into the kernel and return a bpf fd
@@ -24,15 +26,22 @@ static int bpf_load(enum bpf_prog_type type, const struct bpf_insn *insns,
  * @param queue_idx
  *   Queue index matching packet cb
  *
+ * @param log_buf
+ *   Buffer to place resulting error message (optional)
+ *
+ * @param log_size
+ *   Size of log_buf
+ *
  * @return
  *   -1 if the BPF program couldn't be loaded. An fd (int) otherwise.
  */
-int tap_flow_bpf_cls_q(__u32 queue_idx)
+int tap_flow_bpf_cls_q(__u32 queue_idx, char *log_buf, size_t log_size)
 {
 	cls_q_insns[1].imm = queue_idx;
 
 	return bpf_load(BPF_PROG_TYPE_SCHED_CLS,
 			cls_q_insns, RTE_DIM(cls_q_insns),
+			log_buf, log_size,
 			"Dual BSD/GPL");
 }
 
@@ -45,16 +54,23 @@ int tap_flow_bpf_cls_q(__u32 queue_idx)
  * @param[in] map_fd
  *   BPF RSS map file descriptor
  *
+ * @param log_buf
+ *   Buffer to place resulting error message (optional)
+ *
+ * @param log_size
+ *   Size of log_buf
+ *
  * @return
  *   -1 if the BPF program couldn't be loaded. An fd (int) otherwise.
  */
-int tap_flow_bpf_calc_l3_l4_hash(__u32 key_idx, int map_fd)
+int tap_flow_bpf_calc_l3_l4_hash(__u32 key_idx, int map_fd, char *log_buf, size_t log_size)
 {
 	l3_l4_hash_insns[4].imm = key_idx;
 	l3_l4_hash_insns[9].imm = map_fd;
 
 	return bpf_load(BPF_PROG_TYPE_SCHED_ACT,
 			l3_l4_hash_insns, RTE_DIM(l3_l4_hash_insns),
+			log_buf, log_size,
 			"Dual BSD/GPL");
 }
 
@@ -105,6 +121,12 @@ static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  * @param[in] insns_cnt
  *   Number of BPF instructions (size of array)
  *
+ * @param[out] log_buf
+ *   Space for log message
+ *
+ * @param[in] log_size
+ *   Number of characters available in log_buf
+ *
  * @param[in] license
  *   License string that must be acknowledged by the kernel
  *
@@ -112,9 +134,8 @@ static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  *   -1 if the BPF program couldn't be loaded, fd (file descriptor) otherwise
  */
 static int bpf_load(enum bpf_prog_type type,
-		  const struct bpf_insn *insns,
-		  size_t insns_cnt,
-		  const char *license)
+		    const struct bpf_insn *insns, size_t insns_cnt,
+		    char *log_buf, size_t log_size, const char *license)
 {
 	union bpf_attr attr = {};
 
@@ -122,9 +143,12 @@ static int bpf_load(enum bpf_prog_type type,
 	attr.insn_cnt = (__u32)insns_cnt;
 	attr.insns = ptr_to_u64(insns);
 	attr.license = ptr_to_u64(license);
-	attr.log_buf = ptr_to_u64(NULL);
-	attr.log_level = 0;
-	attr.kern_version = 0;
+
+	if (log_size > 0) {
+		attr.log_level = 2;
+		attr.log_buf = ptr_to_u64(log_buf);
+		attr.log_size = log_size;
+	}
 
 	return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
 }
drivers/net/tap/tap_flow.c +10 -6
index ed4d42f92f9f..897d71acbad1 100644
@@ -18,6 +18,8 @@
 #include <tap_tcmsgs.h>
 #include <tap_rss.h>
 
+#define BPF_LOG_BUFSIZ	1024
+
 #ifndef HAVE_TC_FLOWER
 /*
  * For kernels < 4.2, this enum is not defined. Runtime checks will be made to
@@ -1885,11 +1887,13 @@ static int rss_enable(struct pmd_internals *pmd,
 	 * the correct queue.
 	 */
 	for (i = 0; i < pmd->dev->data->nb_rx_queues; i++) {
-		pmd->bpf_fd[i] = tap_flow_bpf_cls_q(i);
+		char log_buf[BPF_LOG_BUFSIZ];
+
+		pmd->bpf_fd[i] = tap_flow_bpf_cls_q(i, log_buf, sizeof(log_buf));
 		if (pmd->bpf_fd[i] < 0) {
 			TAP_LOG(ERR,
-				"Failed to load BPF section %s for queue %d",
-				SEC_NAME_CLS_Q, i);
+				"Failed to load BPF section %s for queue %u: %s",
+				SEC_NAME_CLS_Q, i, log_buf);
 			rte_flow_error_set(
 				error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
 				NULL,
@@ -2074,6 +2078,7 @@ static int rss_add_actions(struct rte_flow *flow, struct pmd_internals *pmd,
 	/* 4096 is the maximum number of instructions for a BPF program */
 	unsigned int i;
 	int err;
+	char log_buf[BPF_LOG_BUFSIZ];
 	struct rss_key rss_entry = { .hash_fields = 0,
 				     .key_size = 0 };
 
@@ -2124,9 +2129,8 @@ static int rss_add_actions(struct rte_flow *flow, struct pmd_internals *pmd,
 	/*
 	 * Load bpf rules to calculate hash for this key_idx
 	 */
-
-	flow->bpf_fd[SEC_L3_L4] =
-		tap_flow_bpf_calc_l3_l4_hash(flow->key_idx, pmd->map_fd);
+	flow->bpf_fd[SEC_L3_L4] = tap_flow_bpf_calc_l3_l4_hash(flow->key_idx, pmd->map_fd,
+							       log_buf, sizeof(log_buf));
 	if (flow->bpf_fd[SEC_L3_L4] < 0) {
 		TAP_LOG(ERR,
 			"Failed to load BPF section %s (%d): %s",
drivers/net/tap/tap_flow.h +2 -2
index 240fbc3dfaef..fb30b495fda1 100644
@@ -57,8 +57,8 @@ int tap_flow_implicit_destroy(struct pmd_internals *pmd,
 int tap_flow_implicit_flush(struct pmd_internals *pmd,
 			    struct rte_flow_error *error);
 
-int tap_flow_bpf_cls_q(__u32 queue_idx);
-int tap_flow_bpf_calc_l3_l4_hash(__u32 key_idx, int map_fd);
+int tap_flow_bpf_cls_q(__u32 queue_idx, char *log_buf, size_t log_size);
+int tap_flow_bpf_calc_l3_l4_hash(__u32 key_idx, int map_fd, char *log_buf, size_t log_size);
 int tap_flow_bpf_rss_map_create(unsigned int key_size, unsigned int value_size,
 			unsigned int max_entries);
 int tap_flow_bpf_update_rss_elem(int fd, void *key, void *value);
Signature · 1 line
2.43.0
#5 lore raw

RFC 5/5 tap: stop "vendoring" linux bpf header

In reply to Stephen Hemminger

The proper place for finding bpf structures and functions is in linux/bpf.h. The original version was trying to workaround the case where the build environment was running on old pre BPF version of Glibc, but the target environment had BPF. This is not a supportable build method, and not how rest of DPDK works.

Having own private (and divergent) version headers leads to future problems when BPF definitions evolve.

---
 drivers/net/tap/bpf/bpf_extract.py |   1 -
 drivers/net/tap/tap_bpf.h          | 118 -----------------------------
 drivers/net/tap/tap_bpf_api.c      |  16 ++--
 drivers/net/tap/tap_bpf_insns.h    |   1 -
 4 files changed, 9 insertions(+), 127 deletions(-)
 delete mode 100644 drivers/net/tap/tap_bpf.h
drivers/net/tap/bpf/bpf_extract.py +0 -1
index b630c42b809f..73c4dafe4eca 100644
@@ -65,7 +65,6 @@ def write_header(out, source):
         print(f' * Auto-generated from {source}', file=out)
     print(" * This not the original source file. Do NOT edit it.", file=out)
     print(" */\n", file=out)
-    print("#include <tap_bpf.h>", file=out)
 
 
 def main():
drivers/net/tap/tap_bpf.h deleted +0 -118

118 lines not on this page · raw message

drivers/net/tap/tap_bpf_api.c +9 -7
index 29223b7f0ea7..54e469ebf5ff 100644
@@ -2,17 +2,13 @@
  * Copyright 2017 Mellanox Technologies, Ltd
  */
 
-#include <errno.h>
-#include <string.h>
 #include <unistd.h>
-#include <sys/queue.h>
+#include <syscall.h>
+#include <linux/bpf.h>
 
-#include <rte_malloc.h>
-#include <rte_eth_tap.h>
 #include <tap_flow.h>
 #include <tap_autoconf.h>
-#include <tap_tcmsgs.h>
-#include <tap_bpf.h>
+
 #include <tap_bpf_insns.h>
 
 static int bpf_load(enum bpf_prog_type type,
@@ -106,7 +102,13 @@ static inline __u64 ptr_to_u64(const void *ptr)
 static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
 			unsigned int size)
 {
+#ifdef __NR_bpf
 	return syscall(__NR_bpf, cmd, attr, size);
+#else
+	fprintf(stderr, "No bpf syscall, kernel headers too old?\n");
+	errno = ENOSYS;
+	return -1;
+#endif
 }
 
 /**
drivers/net/tap/tap_bpf_insns.h +0 -1
index 53fa76c4e6b0..88aec6885a06 100644
@@ -3,7 +3,6 @@
  * This not the original source file. Do NOT edit it.
  */
 
-#include <tap_bpf.h>
 
 static struct bpf_insn cls_q_insns[] = {
 	{0x61,    2,    1,       52, 0x00000000},
Signature · 1 line
2.43.0