samples: bpf: update map definition to new syntax BTF-defined map
Subsystems (4)
cover samples: bpf: update map definition to new syntax BTF-defined map
Since, the new syntax of BTF-defined map has been introduced, the syntax for using maps under samples directory are mixed up. For example, some are already using the new syntax, and some are using existing syntax by calling them as 'legacy'.
As stated at commit abd29c931459 ("libbpf: allow specifying map definitions using BTF"), the BTF-defined map has more compatablility with extending supported map definition features.
Also, unifying the map definition to BTF-defined map will help reduce confusion between new syntax and existing syntax.
The commit doesn't replace all of the map to new BTF-defined map, because some of the samples still use bpf_load instead of libbpf, which can't properly create BTF-defined map.
This will only updates the samples which uses libbpf API for loading bpf program. (ex. bpf_prog_load_xattr)
This patchset fixes some of the outdated error message regarded to loading bpf program (load_bpf_file -> bpf_prog_load_xattr), and updates map definition to new syntax of BTF-defined map.
Daniel T. Lee (2):
samples: bpf: update outdated error message
samples: bpf: update map definition to new syntax BTF-defined map
samples/bpf/hbm.c | 2 +- samples/bpf/sockex1_kern.c | 12 ++-- samples/bpf/sockex2_kern.c | 12 ++-- samples/bpf/xdp1_kern.c | 12 ++-- samples/bpf/xdp1_user.c | 2 +- samples/bpf/xdp2_kern.c | 12 ++-- samples/bpf/xdp_adjust_tail_kern.c | 12 ++-- samples/bpf/xdp_fwd_kern.c | 13 ++-- samples/bpf/xdp_redirect_cpu_kern.c | 108 ++++++++++++++-------------- samples/bpf/xdp_redirect_kern.c | 24 +++---- samples/bpf/xdp_redirect_map_kern.c | 24 +++---- samples/bpf/xdp_router_ipv4_kern.c | 64 ++++++++--------- samples/bpf/xdp_rxq_info_kern.c | 36 +++++----- samples/bpf/xdp_rxq_info_user.c | 6 +- samples/bpf/xdp_sample_pkts_user.c | 2 +- samples/bpf/xdp_tx_iptunnel_kern.c | 26 +++---- samples/bpf/xdp_tx_iptunnel_user.c | 2 +- 17 files changed, 184 insertions(+), 185 deletions(-)
Signature · 1 line
2.23.0
PATCH 1/2 samples: bpf: update outdated error message
Currently, under samples, several methods are being used to load bpf program.
Since using libbpf is preferred solution, lots of previously used 'load_bpf_file' from bpf_load are replaced with 'bpf_prog_load_xattr' from libbpf.
But some of the error messages still show up as 'load_bpf_file' instead of 'bpf_prog_load_xattr'.
This commit fixes outdated errror messages under samples and fixes some code style issues.
--- samples/bpf/hbm.c | 2 +- samples/bpf/xdp1_user.c | 2 +- samples/bpf/xdp_rxq_info_user.c | 6 +++--- samples/bpf/xdp_sample_pkts_user.c | 2 +- samples/bpf/xdp_tx_iptunnel_user.c | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-)
samples/bpf/hbm.c +1 -1
@@ -147,7 +147,7 @@ static int prog_load(char *prog) }if (ret) { -printf ("ERROR: load_bpf_file failed for: %s\n" , prog); +printf ("ERROR: bpf_prog_load_xattr failed for: %s\n" , prog);printf (" Output from verifier:\n%s\n------\n" , bpf_log_buf); ret =-1 ; }else {
samples/bpf/xdp1_user.c +1 -1
@@ -139,7 +139,7 @@ int main(int argc, char **argv) map_fd =bpf_map__fd (map);if (!prog_fd) { -printf ("load_bpf_file: %s\n" ,strerror (errno)); +printf ("bpf_prog_load_xattr: %s\n" ,strerror (errno));return 1 ; }
samples/bpf/xdp_rxq_info_user.c +3 -3
@@ -51,8 +51,8 @@ static const struct option long_options[] = { {"sec", required_argument, NULL, 's' }, {"no-separators", no_argument, NULL, 'z' }, {"action", required_argument, NULL, 'a' }, - {"readmem", no_argument, NULL, 'r' }, - {"swapmac", no_argument, NULL, 'm' }, + {"readmem", no_argument, NULL, 'r' }, + {"swapmac", no_argument, NULL, 'm' }, {"force", no_argument, NULL, 'F' }, {0, 0, NULL, 0 } };
@@ -499,7 +499,7 @@ int main(int argc, char **argv) map_fd =bpf_map__fd (map);if (!prog_fd) { -fprintf (stderr,"ERR: load_bpf_file: %s\n" ,strerror (errno)); +fprintf (stderr,"ERR: bpf_prog_load_xattr: %s\n" ,strerror (errno));return EXIT_FAIL ; }
samples/bpf/xdp_sample_pkts_user.c +1 -1
@@ -150,7 +150,7 @@ int main(int argc, char **argv)return 1 ;if (!prog_fd) { -printf ("load_bpf_file: %s\n" ,strerror (errno)); +printf ("bpf_prog_load_xattr: %s\n" ,strerror (errno));return 1 ; }
samples/bpf/xdp_tx_iptunnel_user.c +1 -1
@@ -268,7 +268,7 @@ int main(int argc, char **argv)return 1 ;if (!prog_fd) { -printf ("load_bpf_file: %s\n" ,strerror (errno)); +printf ("bpf_prog_load_xattr: %s\n" ,strerror (errno));return 1 ; }
Signature · 1 line
2.23.0
PATCH 2/2 samples: bpf: update map definition to new syntax BTF-defined map
In reply to Daniel T. LeeSince, the new syntax of BTF-defined map has been introduced, the syntax for using maps under samples directory are mixed up. For example, some are already using the new syntax, and some are using existing syntax by calling them as 'legacy'.
As stated at commit abd29c931459 ("libbpf: allow specifying map definitions using BTF"), the BTF-defined map has more compatablility with extending supported map definition features.
The commit doesn't replace all of the map to new BTF-defined map, because some of the samples still use bpf_load instead of libbpf, which can't properly create BTF-defined map.
This will only updates the samples which uses libbpf API for loading bpf program. (ex. bpf_prog_load_xattr)
--- samples/bpf/sockex1_kern.c | 12 ++-- samples/bpf/sockex2_kern.c | 12 ++-- samples/bpf/xdp1_kern.c | 12 ++-- samples/bpf/xdp2_kern.c | 12 ++-- samples/bpf/xdp_adjust_tail_kern.c | 12 ++-- samples/bpf/xdp_fwd_kern.c | 13 ++-- samples/bpf/xdp_redirect_cpu_kern.c | 108 ++++++++++++++-------------- samples/bpf/xdp_redirect_kern.c | 24 +++---- samples/bpf/xdp_redirect_map_kern.c | 24 +++---- samples/bpf/xdp_router_ipv4_kern.c | 64 ++++++++--------- samples/bpf/xdp_rxq_info_kern.c | 36 +++++----- samples/bpf/xdp_tx_iptunnel_kern.c | 26 +++---- 12 files changed, 177 insertions(+), 178 deletions(-)
samples/bpf/sockex1_kern.c +6 -6
@@ -5,12 +5,12 @@#include "bpf_helpers.h" #include "bpf_legacy.h" -struct bpf_map_def SEC("maps") my_map = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 256, -}; +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 256); +} my_map SEC(".maps"); SEC("socket1")int bpf_prog1 (struct __sk_buff *skb)
samples/bpf/sockex2_kern.c +6 -6
@@ -190,12 +190,12 @@ struct pair {long bytes; }; -struct bpf_map_def SEC("maps") hash_map = { - .type = BPF_MAP_TYPE_HASH, - .key_size = sizeof(__be32), - .value_size = sizeof(struct pair), - .max_entries = 1024, -}; +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__be32)); + __uint(value_size, sizeof(struct pair)); + __uint(max_entries, 1024); +} hash_map SEC(".maps"); SEC("socket2")int bpf_prog2 (struct __sk_buff *skb)
samples/bpf/xdp1_kern.c +6 -6
@@ -14,12 +14,12 @@#include <linux/ipv6.h> #include "bpf_helpers.h" -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 256, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 256); +} rxcnt SEC(".maps");static int parse_ipv4 (void *data,u64 nh_off,void *data_end) {
samples/bpf/xdp2_kern.c +6 -6
@@ -14,12 +14,12 @@#include <linux/ipv6.h> #include "bpf_helpers.h" -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 256, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 256); +} rxcnt SEC(".maps");static void swap_src_dst_mac (void *data) {
samples/bpf/xdp_adjust_tail_kern.c +6 -6
@@ -28,12 +28,12 @@/* volatile to prevent compiler optimizations */ static volatile __u32 max_pcktsz =MAX_PCKT_SIZE ; -struct bpf_map_def SEC("maps") icmpcnt = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(__u32), - .value_size = sizeof(__u64), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u64)); + __uint(max_entries, 1); +} icmpcnt SEC(".maps"); static __always_inline void count_icmp(void) {
samples/bpf/xdp_fwd_kern.c +6 -7
@@ -23,13 +23,12 @@#define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF) -/* For TX-traffic redirect requires net_device ifindex to be in this devmap */ -struct bpf_map_def SEC("maps") xdp_tx_ports = { - .type = BPF_MAP_TYPE_DEVMAP, - .key_size = sizeof(int), - .value_size = sizeof(int), - .max_entries = 64, -}; +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __uint(max_entries, 64); +} xdp_tx_ports SEC(".maps");/* from include/net/ip.h */ static __always_inline int ip_decrease_ttl(struct iphdr *iph)
samples/bpf/xdp_redirect_cpu_kern.c +54 -54
133 lines not on this page · raw message
samples/bpf/xdp_redirect_kern.c +12 -12
@@ -19,22 +19,22 @@#include <linux/ipv6.h> #include "bpf_helpers.h" -struct bpf_map_def SEC("maps") tx_port = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(int), - .value_size = sizeof(int), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __uint(max_entries, 1); +} tx_port SEC(".maps");/* Count RX packets, as XDP bpf_prog doesn't get direct TX-success * feedback. Redirect TX errors can be caught via a tracepoint. */ -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 1); +} rxcnt SEC(".maps");static void swap_src_dst_mac (void *data) {
samples/bpf/xdp_redirect_map_kern.c +12 -12
@@ -19,22 +19,22 @@#include <linux/ipv6.h> #include "bpf_helpers.h" -struct bpf_map_def SEC("maps") tx_port = { - .type = BPF_MAP_TYPE_DEVMAP, - .key_size = sizeof(int), - .value_size = sizeof(int), - .max_entries = 100, -}; +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __uint(max_entries, 100); +} tx_port SEC(".maps");/* Count RX packets, as XDP bpf_prog doesn't get direct TX-success * feedback. Redirect TX errors can be caught via a tracepoint. */ -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 1); +} rxcnt SEC(".maps");static void swap_src_dst_mac (void *data) {
samples/bpf/xdp_router_ipv4_kern.c +32 -32
76 lines not on this page · raw message
samples/bpf/xdp_rxq_info_kern.c +19 -18
@@ -23,12 +23,13 @@ enum cfg_options_flags {READ_MEM =0x1U , SWAP_MAC = 0x2U, }; -struct bpf_map_def SEC("maps") config_map = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(int), - .value_size = sizeof(struct config), - .max_entries = 1, -}; + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(struct config)); + __uint(max_entries, 1); +} config_map SEC(".maps");/* Common stats data record (shared with userspace) */ struct datarec {
@@ -36,22 +37,22 @@ struct datarec {__u64 issue; }; -struct bpf_map_def SEC("maps") stats_global_map = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(struct datarec)); + __uint(max_entries, 1); +} stats_global_map SEC(".maps");#define MAX_RXQs 64/* Stats per rx_queue_index (per CPU) */ -struct bpf_map_def SEC("maps") rx_queue_index_map = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = MAX_RXQs + 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(struct datarec)); + __uint(max_entries, MAX_RXQs + 1); +} rx_queue_index_map SEC(".maps");static __always_inline void swap_src_dst_mac(void *data)
samples/bpf/xdp_tx_iptunnel_kern.c +13 -13
@@ -19,19 +19,19 @@#include "bpf_helpers.h" #include "xdp_tx_iptunnel_common.h" -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(__u32), - .value_size = sizeof(__u64), - .max_entries = 256, -}; - -struct bpf_map_def SEC("maps") vip2tnl = { - .type = BPF_MAP_TYPE_HASH, - .key_size = sizeof(struct vip), - .value_size = sizeof(struct iptnl_info), - .max_entries = MAX_IPTNL_ENTRIES, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u64)); + __uint(max_entries, 256); +} rxcnt SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(struct vip)); + __uint(value_size, sizeof(struct iptnl_info)); + __uint(max_entries, MAX_IPTNL_ENTRIES); +} vip2tnl SEC(".maps"); static __always_inline void count_tx(u32 protocol) {
Signature · 1 line
2.23.0
Quoted 14 lines · Daniel T. Lee
Since, the new syntax of BTF-defined map has been introduced, the syntax for using maps under samples directory are mixed up. For example, some are already using the new syntax, and some are using existing syntax by calling them as 'legacy'.
As stated at commit
abd29c931459("libbpf: allow specifying map definitions using BTF"), the BTF-defined map has more compatablility with extending supported map definition features.The commit doesn't replace all of the map to new BTF-defined map, because some of the samples still use
bpf_loadinstead of libbpf, which can't properly create BTF-defined map.
Quoted 5 lines
This will only updates the samples which uses libbpf API for loading bpf program. (ex.
bpf_prog_load_xattr)Signed-off-by Daniel T. Lee <[email protected]>---
Please try to stick to __type() for key/value, where possible (all the arrays, hashes, per-cpu arrays definitely work). Some of the special CPUMAP, DEVMAP might not work. Not sure about TRIEs, please try.
Quoted 45 lines
samples/bpf/sockex1_kern.c | 12 ++-- samples/bpf/sockex2_kern.c | 12 ++-- samples/bpf/xdp1_kern.c | 12 ++-- samples/bpf/xdp2_kern.c | 12 ++-- samples/bpf/xdp_adjust_tail_kern.c | 12 ++-- samples/bpf/xdp_fwd_kern.c | 13 ++-- samples/bpf/xdp_redirect_cpu_kern.c | 108 ++++++++++++++-------------- samples/bpf/xdp_redirect_kern.c | 24 +++---- samples/bpf/xdp_redirect_map_kern.c | 24 +++---- samples/bpf/xdp_router_ipv4_kern.c | 64 ++++++++--------- samples/bpf/xdp_rxq_info_kern.c | 36 +++++----- samples/bpf/xdp_tx_iptunnel_kern.c | 26 +++---- 12 files changed, 177 insertions(+), 178 deletions(-)samples/bpf/sockex1_kern.c +6
-6@@ -5,12 +5,12 @@#include "bpf_helpers.h" #include "bpf_legacy.h" -struct bpf_map_def SEC("maps") my_map = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 256, -}; +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 256); +} my_map SEC(".maps"); SEC("socket1")int bpf_prog1 (struct __sk_buff *skb)samples/bpf/sockex2_kern.c +0
-0@@ -190,12 +190,12 @@ struct pair {long bytes; };
samples/bpf/sockex2_kern.c struct pair { · line 196
Quoted 10 lines
samples/bpf/sockex2_kern.c +4
-6-struct bpf_map_def SEC("maps") hash_map = { - .type = BPF_MAP_TYPE_HASH, - .key_size = sizeof(__be32), - .value_size = sizeof(struct pair), - .max_entries = 1024, -}; +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__be32)); + __uint(value_size, sizeof(struct pair));
let's use __type(key, __be32) and __type(value, struct pair) here and in most other places where we have maps supporting BTF
Quoted 85 lines
samples/bpf/sockex2_kern.c +2
-0+__uint (max_entries,1024 ); +} hash_map SEC(".maps"); SEC("socket2")int bpf_prog2 (struct __sk_buff *skb)samples/bpf/xdp1_kern.c +6
-6@@ -14,12 +14,12 @@#include <linux/ipv6.h> #include "bpf_helpers.h" -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 256, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 256); +} rxcnt SEC(".maps");static int parse_ipv4 (void *data,u64 nh_off,void *data_end) {samples/bpf/xdp2_kern.c +6
-6@@ -14,12 +14,12 @@#include <linux/ipv6.h> #include "bpf_helpers.h" -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 256, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 256); +} rxcnt SEC(".maps");static void swap_src_dst_mac (void *data) {samples/bpf/xdp_adjust_tail_kern.c +6
-6@@ -28,12 +28,12 @@/* volatile to prevent compiler optimizations */ static volatile __u32 max_pcktsz =MAX_PCKT_SIZE ; -struct bpf_map_def SEC("maps") icmpcnt = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(__u32), - .value_size = sizeof(__u64), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u64)); + __uint(max_entries, 1); +} icmpcnt SEC(".maps"); static __always_inline void count_icmp(void) {samples/bpf/xdp_fwd_kern.c +0
-3@@ -23,13 +23,12 @@#define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF) -/* For TX-traffic redirect requires net_device ifindex to be in this devmap */ -struct bpf_map_def SEC("maps") xdp_tx_ports = { - .type = BPF_MAP_TYPE_DEVMAP,
samples/bpf/xdp_fwd_kern.c line 31
Quoted 10 lines
samples/bpf/xdp_fwd_kern.c +6
-4- .key_size = sizeof(int), - .value_size = sizeof(int), - .max_entries = 64, -}; +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __uint(max_entries, 64); +} xdp_tx_ports SEC(".maps");
DEVMAP might be special, I don't remember, so key_size/value_size might be necessary
Quoted 14 lines
samples/bpf/xdp_fwd_kern.c +0
-0/* from include/net/ip.h */ static __always_inline int ip_decrease_ttl(struct iphdr *iph)samples/bpf/xdp_redirect_cpu_kern.c +0
-3@@ -18,12 +18,12 @@#define MAX_CPUS 64/* WARNING - sync with _user.c */ /* Special map type that can XDP_REDIRECT frames to another CPU */ -struct bpf_map_def SEC("maps") cpu_map = { - .type = BPF_MAP_TYPE_CPUMAP, - .key_size = sizeof(u32),
samples/bpf/xdp_redirect_cpu_kern.c line 27
Quoted 10 lines
samples/bpf/xdp_redirect_cpu_kern.c +6
-3- .value_size = sizeof(u32), - .max_entries = MAX_CPUS, -}; +struct { + __uint(type, BPF_MAP_TYPE_CPUMAP); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(u32)); + __uint(max_entries, MAX_CPUS); +} cpu_map SEC(".maps");
same for CPUMAP, but would be nice to double-check.
Quoted 376 lines
samples/bpf/xdp_redirect_cpu_kern.c +48
-48/* Common stats data record to keep userspace more simple */ struct datarec {@@ -35,67 +35,67 @@ struct datarec {/* Count RX packets, as XDP bpf_prog doesn't get direct TX-success * feedback. Redirect TX errors can be caught via a tracepoint. */ -struct bpf_map_def SEC("maps") rx_cnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(struct datarec)); + __uint(max_entries, 1); +} rx_cnt SEC(".maps");/* Used by trace point */ -struct bpf_map_def SEC("maps") redirect_err_cnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = 2, +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(struct datarec)); + __uint(max_entries, 2);/* TODO: have entries for all possible errno's */ -}; +} redirect_err_cnt SEC(".maps");/* Used by trace point */ -struct bpf_map_def SEC("maps") cpumap_enqueue_cnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = MAX_CPUS, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(struct datarec)); + __uint(max_entries, MAX_CPUS); +} cpumap_enqueue_cnt SEC(".maps");/* Used by trace point */ -struct bpf_map_def SEC("maps") cpumap_kthread_cnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(struct datarec)); + __uint(max_entries, 1); +} cpumap_kthread_cnt SEC(".maps");/* Set of maps controlling available CPU, and for iterating through * selectable redirect CPUs. */ -struct bpf_map_def SEC("maps") cpus_available = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(u32), - .max_entries = MAX_CPUS, -}; -struct bpf_map_def SEC("maps") cpus_count = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(u32), - .max_entries = 1, -}; -struct bpf_map_def SEC("maps") cpus_iterator = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(u32), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(u32)); + __uint(max_entries, MAX_CPUS); +} cpus_available SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(u32)); + __uint(max_entries, 1); +} cpus_count SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(u32)); + __uint(max_entries, 1); +} cpus_iterator SEC(".maps");/* Used by trace point */ -struct bpf_map_def SEC("maps") exception_cnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(struct datarec)); + __uint(max_entries, 1); +} exception_cnt SEC(".maps");/* Helper parse functions */ samples/bpf/xdp_redirect_kern.c +12
-12@@ -19,22 +19,22 @@#include <linux/ipv6.h> #include "bpf_helpers.h" -struct bpf_map_def SEC("maps") tx_port = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(int), - .value_size = sizeof(int), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __uint(max_entries, 1); +} tx_port SEC(".maps");/* Count RX packets, as XDP bpf_prog doesn't get direct TX-success * feedback. Redirect TX errors can be caught via a tracepoint. */ -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 1); +} rxcnt SEC(".maps");static void swap_src_dst_mac (void *data) {samples/bpf/xdp_redirect_map_kern.c +12
-12@@ -19,22 +19,22 @@#include <linux/ipv6.h> #include "bpf_helpers.h" -struct bpf_map_def SEC("maps") tx_port = { - .type = BPF_MAP_TYPE_DEVMAP, - .key_size = sizeof(int), - .value_size = sizeof(int), - .max_entries = 100, -}; +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __uint(max_entries, 100); +} tx_port SEC(".maps");/* Count RX packets, as XDP bpf_prog doesn't get direct TX-success * feedback. Redirect TX errors can be caught via a tracepoint. */ -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 1); +} rxcnt SEC(".maps");static void swap_src_dst_mac (void *data) {samples/bpf/xdp_router_ipv4_kern.c +32
-32@@ -42,44 +42,44 @@ struct direct_map { };/* Map for trie implementation*/ -struct bpf_map_def SEC("maps") lpm_map = { - .type = BPF_MAP_TYPE_LPM_TRIE, - .key_size = 8, - .value_size = sizeof(struct trie_value), - .max_entries = 50, - .map_flags = BPF_F_NO_PREALLOC, -}; +struct { + __uint(type, BPF_MAP_TYPE_LPM_TRIE); + __uint(key_size, 8); + __uint(value_size, sizeof(struct trie_value)); + __uint(max_entries, 50); + __uint(map_flags, BPF_F_NO_PREALLOC); +} lpm_map SEC(".maps");/* Map for counter*/ -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(u64), - .max_entries = 256, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(u64)); + __uint(max_entries, 256); +} rxcnt SEC(".maps");/* Map for ARP table*/ -struct bpf_map_def SEC("maps") arp_table = { - .type = BPF_MAP_TYPE_HASH, - .key_size = sizeof(__be32), - .value_size = sizeof(__be64), - .max_entries = 50, -}; +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__be32)); + __uint(value_size, sizeof(__be64)); + __uint(max_entries, 50); +} arp_table SEC(".maps");/* Map to keep the exact match entries in the route table*/ -struct bpf_map_def SEC("maps") exact_match = { - .type = BPF_MAP_TYPE_HASH, - .key_size = sizeof(__be32), - .value_size = sizeof(struct direct_map), - .max_entries = 50, -}; - -struct bpf_map_def SEC("maps") tx_port = { - .type = BPF_MAP_TYPE_DEVMAP, - .key_size = sizeof(int), - .value_size = sizeof(int), - .max_entries = 100, -}; +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__be32)); + __uint(value_size, sizeof(struct direct_map)); + __uint(max_entries, 50); +} exact_match SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __uint(max_entries, 100); +} tx_port SEC(".maps");/* Function to set source and destination mac of the packet */ static inline void set_src_dst_mac (void *data,void *src,void *dst)samples/bpf/xdp_rxq_info_kern.c +19
-18@@ -23,12 +23,13 @@ enum cfg_options_flags {READ_MEM =0x1U , SWAP_MAC = 0x2U, }; -struct bpf_map_def SEC("maps") config_map = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(int), - .value_size = sizeof(struct config), - .max_entries = 1, -}; + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(struct config)); + __uint(max_entries, 1); +} config_map SEC(".maps");/* Common stats data record (shared with userspace) */ struct datarec {@@ -36,22 +37,22 @@ struct datarec {__u64 issue; }; -struct bpf_map_def SEC("maps") stats_global_map = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(struct datarec)); + __uint(max_entries, 1); +} stats_global_map SEC(".maps");#define MAX_RXQs 64/* Stats per rx_queue_index (per CPU) */ -struct bpf_map_def SEC("maps") rx_queue_index_map = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = MAX_RXQs + 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(struct datarec)); + __uint(max_entries, MAX_RXQs + 1); +} rx_queue_index_map SEC(".maps");static __always_inline void swap_src_dst_mac(void *data)samples/bpf/xdp_tx_iptunnel_kern.c +13
-14@@ -19,19 +19,19 @@#include "bpf_helpers.h" #include "xdp_tx_iptunnel_common.h" -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(__u32), - .value_size = sizeof(__u64), - .max_entries = 256, -}; - -struct bpf_map_def SEC("maps") vip2tnl = { - .type = BPF_MAP_TYPE_HASH, - .key_size = sizeof(struct vip), - .value_size = sizeof(struct iptnl_info), - .max_entries = MAX_IPTNL_ENTRIES, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u64)); + __uint(max_entries, 256); +} rxcnt SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(struct vip)); + __uint(value_size, sizeof(struct iptnl_info)); + __uint(max_entries, MAX_IPTNL_ENTRIES); +} vip2tnl SEC(".maps"); static __always_inline void count_tx(u32 protocol) { --2.23.0
Re: [PATCH,bpf-next 1/2] samples: bpf: update outdated error message
In reply to Daniel T. LeeQuoted 8 lines · Daniel T. Lee
Currently, under samples, several methods are being used to load bpf program.
Since using libbpf is preferred solution, lots of previously used '
load_bpf_file' frombpf_loadare replaced with 'bpf_prog_load_xattr' from libbpf.
Quoted 8 lines
But some of the error messages still show up as '
load_bpf_file' instead of 'bpf_prog_load_xattr'.This commit fixes outdated errror messages under samples and fixes some code style issues.
Signed-off-by Daniel T. Lee <[email protected]>---
[...]
Quoted 19 lines · Andrii Nakryiko
Quoted 17 lines · Daniel T. Lee
Since, the new syntax of BTF-defined map has been introduced, the syntax for using maps under samples directory are mixed up. For example, some are already using the new syntax, and some are using existing syntax by calling them as 'legacy'.
As stated at commit
abd29c931459("libbpf: allow specifying map definitions using BTF"), the BTF-defined map has more compatablility with extending supported map definition features.The commit doesn't replace all of the map to new BTF-defined map, because some of the samples still use
bpf_loadinstead of libbpf, which can't properly create BTF-defined map.This will only updates the samples which uses libbpf API for loading bpf program. (ex.
bpf_prog_load_xattr)
Quoted 8 lines
Quoted 2 lines
Signed-off-by Daniel T. Lee <[email protected]>---
Please try to stick to
__type()for key/value, where possible (all the arrays, hashes, per-cpu arrays definitely work). Some of the special CPUMAP, DEVMAP might not work. Not sure about TRIEs, please try.
Will apply feedback right away!
Thanks for the review!
Quoted 141 lines
Quoted 45 lines
samples/bpf/sockex1_kern.c | 12 ++-- samples/bpf/sockex2_kern.c | 12 ++-- samples/bpf/xdp1_kern.c | 12 ++-- samples/bpf/xdp2_kern.c | 12 ++-- samples/bpf/xdp_adjust_tail_kern.c | 12 ++-- samples/bpf/xdp_fwd_kern.c | 13 ++-- samples/bpf/xdp_redirect_cpu_kern.c | 108 ++++++++++++++-------------- samples/bpf/xdp_redirect_kern.c | 24 +++---- samples/bpf/xdp_redirect_map_kern.c | 24 +++---- samples/bpf/xdp_router_ipv4_kern.c | 64 ++++++++--------- samples/bpf/xdp_rxq_info_kern.c | 36 +++++----- samples/bpf/xdp_tx_iptunnel_kern.c | 26 +++---- 12 files changed, 177 insertions(+), 178 deletions(-)samples/bpf/sockex1_kern.c +6
-6@@ -5,12 +5,12 @@#include "bpf_helpers.h" #include "bpf_legacy.h" -struct bpf_map_def SEC("maps") my_map = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 256, -}; +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 256); +} my_map SEC(".maps"); SEC("socket1")int bpf_prog1 (struct __sk_buff *skb)samples/bpf/sockex2_kern.c +0
-0@@ -190,12 +190,12 @@ struct pair {long bytes; };samples/bpf/sockex2_kern.c struct pair { · line 196
Quoted 10 lines
samples/bpf/sockex2_kern.c +4
-6-struct bpf_map_def SEC("maps") hash_map = { - .type = BPF_MAP_TYPE_HASH, - .key_size = sizeof(__be32), - .value_size = sizeof(struct pair), - .max_entries = 1024, -}; +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(__be32)); + __uint(value_size, sizeof(struct pair));let's use
__type(key, __be32)and__type(value, struct pair)here and in most other places where we have maps supporting BTFQuoted 82 lines
samples/bpf/sockex2_kern.c +2
-0+__uint (max_entries,1024 ); +} hash_map SEC(".maps"); SEC("socket2")int bpf_prog2 (struct __sk_buff *skb)samples/bpf/xdp1_kern.c +6
-6@@ -14,12 +14,12 @@#include <linux/ipv6.h> #include "bpf_helpers.h" -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 256, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 256); +} rxcnt SEC(".maps");static int parse_ipv4 (void *data,u64 nh_off,void *data_end) {samples/bpf/xdp2_kern.c +6
-6@@ -14,12 +14,12 @@#include <linux/ipv6.h> #include "bpf_helpers.h" -struct bpf_map_def SEC("maps") rxcnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(long), - .max_entries = 256, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(long)); + __uint(max_entries, 256); +} rxcnt SEC(".maps");static void swap_src_dst_mac (void *data) {samples/bpf/xdp_adjust_tail_kern.c +6
-6@@ -28,12 +28,12 @@/* volatile to prevent compiler optimizations */ static volatile __u32 max_pcktsz =MAX_PCKT_SIZE ; -struct bpf_map_def SEC("maps") icmpcnt = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(__u32), - .value_size = sizeof(__u64), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u64)); + __uint(max_entries, 1); +} icmpcnt SEC(".maps"); static __always_inline void count_icmp(void) {samples/bpf/xdp_fwd_kern.c +0
-0@@ -23,13 +23,12 @@#define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF)
Quoted 16 lines
samples/bpf/xdp_fwd_kern.c line 31
Quoted 13 lines
samples/bpf/xdp_fwd_kern.c +6
-7-/* For TX-traffic redirect requires net_device ifindex to be in this devmap */ -struct bpf_map_def SEC("maps") xdp_tx_ports = { - .type = BPF_MAP_TYPE_DEVMAP, - .key_size = sizeof(int), - .value_size = sizeof(int), - .max_entries = 64, -}; +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __uint(max_entries, 64); +} xdp_tx_ports SEC(".maps");DEVMAP might be special, I don't remember, so key_size/value_size might be necessary
As you've mentioned, DEVMAP does not work with __type.
Quoted 11 lines
Quoted 11 lines
samples/bpf/xdp_fwd_kern.c +0
-0/* from include/net/ip.h */ static __always_inline int ip_decrease_ttl(struct iphdr *iph)samples/bpf/xdp_redirect_cpu_kern.c +0
-0@@ -18,12 +18,12 @@#define MAX_CPUS 64/* WARNING - sync with _user.c */
Quoted 17 lines
Quoted 4 lines
samples/bpf/xdp_redirect_cpu_kern.c +0
-3/* Special map type that can XDP_REDIRECT frames to another CPU */ -struct bpf_map_def SEC("maps") cpu_map = { - .type = BPF_MAP_TYPE_CPUMAP, - .key_size = sizeof(u32),samples/bpf/xdp_redirect_cpu_kern.c line 27
Quoted 10 lines
samples/bpf/xdp_redirect_cpu_kern.c +6
-3- .value_size = sizeof(u32), - .max_entries = MAX_CPUS, -}; +struct { + __uint(type, BPF_MAP_TYPE_CPUMAP); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(u32)); + __uint(max_entries, MAX_CPUS); +} cpu_map SEC(".maps");same for CPUMAP, but would be nice to double-check.
Same for the CPUMAP. Just checked.
Seems TRIE doesn't work either. it uses __uint(key_size, 8); and __type(key, 8); is not acceptable.
[...]
Thank you for your time and effort for the review.
Thanks,
Daniel