From 537085581631ab56ce66e2837446bf0ba920b9d3 Mon Sep 17 00:00:00 2001 From: Y7n05h Date: Sun, 3 Jul 2022 10:20:03 +0800 Subject: [PATCH] Add pin support for xsk map (#4061) Add pin support for xsk map. Signed-off-by: Y7n05h --- docs/reference_guide.md | 4 ++-- src/cc/export/helpers.h | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/reference_guide.md b/docs/reference_guide.md index ff18ab93e676..0474f46849ff 100644 --- a/docs/reference_guide.md +++ b/docs/reference_guide.md @@ -1145,9 +1145,9 @@ Examples in situ: ### 13. BPF_XSKMAP -Syntax: ```BPF_XSKMAP(name, size)``` +Syntax: ```BPF_XSKMAP(name, size [, "/sys/fs/bpf/xyz"])``` -This creates a xsk map named ```name``` with ```size``` entries. Each entry represents one NIC's queue id. This map is only used in XDP to redirect packet to an AF_XDP socket. If the AF_XDP socket is binded to a queue which is different than the current packet's queue id, the packet will be dropped. For kernel v5.3 and latter, `lookup` method is available and can be used to check whether and AF_XDP socket is available for the current packet's queue id. More details at [AF_XDP](https://www.kernel.org/doc/html/latest/networking/af_xdp.html). +This creates a xsk map named ```name``` with ```size``` entries and pin it to the bpffs as a FILE. Each entry represents one NIC's queue id. This map is only used in XDP to redirect packet to an AF_XDP socket. If the AF_XDP socket is binded to a queue which is different than the current packet's queue id, the packet will be dropped. For kernel v5.3 and latter, `lookup` method is available and can be used to check whether and AF_XDP socket is available for the current packet's queue id. More details at [AF_XDP](https://www.kernel.org/doc/html/latest/networking/af_xdp.html). For example: ```C diff --git a/src/cc/export/helpers.h b/src/cc/export/helpers.h index e2de995fc25f..82dc0fe13aec 100644 --- a/src/cc/export/helpers.h +++ b/src/cc/export/helpers.h @@ -390,7 +390,7 @@ struct _name##_table_t _name = { .max_entries = (_max_entries) } #define BPF_CPUMAP(_name, _max_entries) \ BPF_XDP_REDIRECT_MAP("cpumap", u32, _name, _max_entries) -#define BPF_XSKMAP(_name, _max_entries) \ +#define _BPF_XSKMAP(_name, _max_entries, _pinned) \ struct _name##_table_t { \ u32 key; \ int leaf; \ @@ -399,8 +399,12 @@ struct _name##_table_t { \ u64 (*redirect_map) (int, int); \ u32 max_entries; \ }; \ -__attribute__((section("maps/xskmap"))) \ +__attribute__((section("maps/xskmap" _pinned))) \ struct _name##_table_t _name = { .max_entries = (_max_entries) } +#define BPF_XSKMAP2(_name, _max_entries) _BPF_XSKMAP(_name, _max_entries, "") +#define BPF_XSKMAP3(_name, _max_entries, _pinned) _BPF_XSKMAP(_name, _max_entries, ":" _pinned) +#define BPF_XSKMAPX(_1, _2, _3, NAME, ...) NAME +#define BPF_XSKMAP(...) BPF_XSKMAPX(__VA_ARGS__, BPF_XSKMAP3, BPF_XSKMAP2)(__VA_ARGS__) #define BPF_ARRAY_OF_MAPS(_name, _inner_map_name, _max_entries) \ BPF_TABLE("array_of_maps$" _inner_map_name, int, int, _name, _max_entries)