samples: bpf: update map definition to new syntax BTF-defined map
authorDaniel T. Lee <danieltimlee@gmail.com>
Thu, 7 Nov 2019 00:51:53 +0000 (09:51 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 26 Jan 2020 09:00:58 +0000 (10:00 +0100)
commit 451d1dc886b548d6e18c933adca326c1307023c9 upstream.

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_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)

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
12 files changed:
samples/bpf/sockex1_kern.c
samples/bpf/sockex2_kern.c
samples/bpf/xdp1_kern.c
samples/bpf/xdp2_kern.c
samples/bpf/xdp_adjust_tail_kern.c
samples/bpf/xdp_fwd_kern.c
samples/bpf/xdp_redirect_cpu_kern.c
samples/bpf/xdp_redirect_kern.c
samples/bpf/xdp_redirect_map_kern.c
samples/bpf/xdp_router_ipv4_kern.c
samples/bpf/xdp_rxq_info_kern.c
samples/bpf/xdp_tx_iptunnel_kern.c

index ed18e9a4909c77899d6561680261e97a3dbea235..43e38ce594d47cc90db1c3fa90dab0529944a0e4 100644 (file)
@@ -4,12 +4,12 @@
 #include <uapi/linux/ip.h>
 #include "bpf_helpers.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);
+       __type(key, u32);
+       __type(value, long);
+       __uint(max_entries, 256);
+} my_map SEC(".maps");
 
 SEC("socket1")
 int bpf_prog1(struct __sk_buff *skb)
index f2f9dbc021b0d48c8c66d23cdee444076c25a43d..ae4bdc89b599a198c7283327d743844a74a1324e 100644 (file)
@@ -189,12 +189,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);
+       __type(key, __be32);
+       __type(value, struct pair);
+       __uint(max_entries, 1024);
+} hash_map SEC(".maps");
 
 SEC("socket2")
 int bpf_prog2(struct __sk_buff *skb)
index 219742106bfdca272bd8f57463f12b39f5310f78..db6870aee42c0f0b09abca2ece222550d6565d31 100644 (file)
 #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);
+       __type(key, u32);
+       __type(value, long);
+       __uint(max_entries, 256);
+} rxcnt SEC(".maps");
 
 static int parse_ipv4(void *data, u64 nh_off, void *data_end)
 {
index e01288867d155b7f83a7881785f4405f2d6e42bf..c74b52c6d945924cffceb645154e5fe1629998c0 100644 (file)
 #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);
+       __type(key, u32);
+       __type(value, long);
+       __uint(max_entries, 256);
+} rxcnt SEC(".maps");
 
 static void swap_src_dst_mac(void *data)
 {
index 411fdb21f8bcf62efac06bdf8a80997e3615c7f5..cd9ff2a40a3985b37dbc0cd5139ed4f3241f6461 100644 (file)
 #define ICMP_TOOBIG_SIZE 98
 #define ICMP_TOOBIG_PAYLOAD_SIZE 92
 
-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);
+       __type(key, __u32);
+       __type(value, __u64);
+       __uint(max_entries, 1);
+} icmpcnt SEC(".maps");
 
 static __always_inline void count_icmp(void)
 {
index 701a30f258b17d34f804ec358db976b8175511f5..d013029aeaa2a8f1a2d507f6a803c6625e47703d 100644 (file)
 
 #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)
index a306d1c756227fa86187f1ae5932afab31d87df9..cfcc31e51197846d06ecdfb70da46c0a10b37f92 100644 (file)
 #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),
-       .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");
 
 /* 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);
+       __type(key, u32);
+       __type(value, 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);
+       __type(key, u32);
+       __type(value, 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);
+       __type(key, u32);
+       __type(value, 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);
+       __type(key, u32);
+       __type(value, 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);
+       __type(key, u32);
+       __type(value, u32);
+       __uint(max_entries, MAX_CPUS);
+} cpus_available SEC(".maps");
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __type(key, u32);
+       __type(value, u32);
+       __uint(max_entries, 1);
+} cpus_count SEC(".maps");
+struct {
+       __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+       __type(key, u32);
+       __type(value, 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);
+       __type(key, u32);
+       __type(value, struct datarec);
+       __uint(max_entries, 1);
+} exception_cnt SEC(".maps");
 
 /* Helper parse functions */
 
index 8abb151e385f9cc8775cfdb929b2800c17d61c70..1f0b7d05abb2fa0ef2c1eacb066aef2b08784381 100644 (file)
 #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);
+       __type(key, int);
+       __type(value, 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);
+       __type(key, u32);
+       __type(value, long);
+       __uint(max_entries, 1);
+} rxcnt SEC(".maps");
 
 static void swap_src_dst_mac(void *data)
 {
index 740a529ba84f238c91d5195614fe47f10a623bef..4631b484c43202333a8b579dc0b3bee629f3c501 100644 (file)
 #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);
+       __type(key, u32);
+       __type(value, long);
+       __uint(max_entries, 1);
+} rxcnt SEC(".maps");
 
 static void swap_src_dst_mac(void *data)
 {
index 993f56bc7b9a8179d6d5c5311faf324e2278df30..bf11efc8e9494b5aa63c3ff028c88b4421497c2c 100644 (file)
@@ -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);
+       __type(key, u32);
+       __type(value, 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);
+       __type(key, __be32);
+       __type(value, __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);
+       __type(key, __be32);
+       __type(value, 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)
index 222a83eed1cbf0b213998a47153334b3fd4127b9..272d0f82a6b5687bca3c7cb61001eff1b7ca5a04 100644 (file)
@@ -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);
+       __type(key, int);
+       __type(value, 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);
+       __type(key, u32);
+       __type(value, 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);
+       __type(key, u32);
+       __type(value, struct datarec);
+       __uint(max_entries, MAX_RXQs + 1);
+} rx_queue_index_map SEC(".maps");
 
 static __always_inline
 void swap_src_dst_mac(void *data)
index 0f4f6e8c8611e3dea0e758215a65cc185ecdae02..6db450a5c1ca3e444c93f206f8d096c3015ff1b2 100644 (file)
 #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);
+       __type(key, __u32);
+       __type(value, __u64);
+       __uint(max_entries, 256);
+} rxcnt SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __type(key, struct vip);
+       __type(value, struct iptnl_info);
+       __uint(max_entries, MAX_IPTNL_ENTRIES);
+} vip2tnl SEC(".maps");
 
 static __always_inline void count_tx(u32 protocol)
 {