Commit 4dd3f50a authored by Daniel Borkmann's avatar Daniel Borkmann Committed by Stephen Hemminger

tc, bpf: add support for map pre/allocation

Follow-up to kernel commit 6c9059817432 ("bpf: pre-allocate hash map
elements"). Add flags support, so that we can pass in BPF_F_NO_PREALLOC
flag for disallowing preallocation. Update examples accordingly and also
remove the BPF_* map helper macros from them as they were not very useful.
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent afc1a200
...@@ -6,7 +6,14 @@ ...@@ -6,7 +6,14 @@
*/ */
#define JMP_MAP_ID 0xabccba #define JMP_MAP_ID 0xabccba
BPF_PROG_ARRAY(jmp_tc, JMP_MAP_ID, PIN_OBJECT_NS, 1); struct bpf_elf_map __section_maps jmp_tc = {
.type = BPF_MAP_TYPE_PROG_ARRAY,
.id = JMP_MAP_ID,
.size_key = sizeof(uint32_t),
.size_value = sizeof(uint32_t),
.pinning = PIN_OBJECT_NS,
.max_elem = 1,
};
__section_tail(JMP_MAP_ID, 0) __section_tail(JMP_MAP_ID, 0)
int cls_loop(struct __sk_buff *skb) int cls_loop(struct __sk_buff *skb)
......
...@@ -33,7 +33,13 @@ ...@@ -33,7 +33,13 @@
* [...] * [...]
*/ */
BPF_PROG_ARRAY(jmp_tc, 0, PIN_GLOBAL_NS, 1); struct bpf_elf_map __section_maps jmp_tc = {
.type = BPF_MAP_TYPE_PROG_ARRAY,
.size_key = sizeof(uint32_t),
.size_value = sizeof(uint32_t),
.pinning = PIN_GLOBAL_NS,
.max_elem = 1,
};
__section("aaa") __section("aaa")
int cls_aaa(struct __sk_buff *skb) int cls_aaa(struct __sk_buff *skb)
......
...@@ -192,6 +192,7 @@ struct bpf_elf_map __section("maps") map_proto = { ...@@ -192,6 +192,7 @@ struct bpf_elf_map __section("maps") map_proto = {
.size_key = sizeof(uint8_t), .size_key = sizeof(uint8_t),
.size_value = sizeof(struct count_tuple), .size_value = sizeof(struct count_tuple),
.max_elem = 256, .max_elem = 256,
.flags = BPF_F_NO_PREALLOC,
}; };
struct bpf_elf_map __section("maps") map_queue = { struct bpf_elf_map __section("maps") map_queue = {
...@@ -200,6 +201,7 @@ struct bpf_elf_map __section("maps") map_queue = { ...@@ -200,6 +201,7 @@ struct bpf_elf_map __section("maps") map_queue = {
.size_key = sizeof(uint32_t), .size_key = sizeof(uint32_t),
.size_value = sizeof(struct count_queue), .size_value = sizeof(struct count_queue),
.max_elem = 1024, .max_elem = 1024,
.flags = BPF_F_NO_PREALLOC,
}; };
struct bpf_elf_map __section("maps") map_drops = { struct bpf_elf_map __section("maps") map_drops = {
......
...@@ -18,7 +18,13 @@ ...@@ -18,7 +18,13 @@
* instance is being created. * instance is being created.
*/ */
BPF_ARRAY4(map_sh, 0, PIN_OBJECT_NS, 1); /* or PIN_GLOBAL_NS, or PIN_NONE */ struct bpf_elf_map __section_maps map_sh = {
.type = BPF_MAP_TYPE_ARRAY,
.size_key = sizeof(uint32_t),
.size_value = sizeof(uint32_t),
.pinning = PIN_OBJECT_NS, /* or PIN_GLOBAL_NS, or PIN_NONE */
.max_elem = 1,
};
__section("egress") __section("egress")
int emain(struct __sk_buff *skb) int emain(struct __sk_buff *skb)
......
...@@ -26,10 +26,31 @@ ...@@ -26,10 +26,31 @@
* classifier behaviour. * classifier behaviour.
*/ */
BPF_PROG_ARRAY(jmp_tc, FOO, PIN_OBJECT_NS, MAX_JMP_SIZE); struct bpf_elf_map __section_maps jmp_tc = {
BPF_PROG_ARRAY(jmp_ex, BAR, PIN_OBJECT_NS, 1); .type = BPF_MAP_TYPE_PROG_ARRAY,
.id = FOO,
BPF_ARRAY4(map_sh, 0, PIN_OBJECT_NS, 1); .size_key = sizeof(uint32_t),
.size_value = sizeof(uint32_t),
.pinning = PIN_OBJECT_NS,
.max_elem = MAX_JMP_SIZE,
};
struct bpf_elf_map __section_maps jmp_ex = {
.type = BPF_MAP_TYPE_PROG_ARRAY,
.id = BAR,
.size_key = sizeof(uint32_t),
.size_value = sizeof(uint32_t),
.pinning = PIN_OBJECT_NS,
.max_elem = 1,
};
struct bpf_elf_map __section_maps map_sh = {
.type = BPF_MAP_TYPE_ARRAY,
.size_key = sizeof(uint32_t),
.size_value = sizeof(uint32_t),
.pinning = PIN_OBJECT_NS,
.max_elem = 1,
};
__section_tail(FOO, ENTRY_0) __section_tail(FOO, ENTRY_0)
int cls_case1(struct __sk_buff *skb) int cls_case1(struct __sk_buff *skb)
......
...@@ -99,51 +99,6 @@ ...@@ -99,51 +99,6 @@
char ____license[] __section_license = NAME char ____license[] __section_license = NAME
#endif #endif
#ifndef __BPF_MAP
# define __BPF_MAP(NAME, TYPE, ID, SIZE_KEY, SIZE_VALUE, PIN, MAX_ELEM) \
struct bpf_elf_map __section_maps NAME = { \
.type = (TYPE), \
.id = (ID), \
.size_key = (SIZE_KEY), \
.size_value = (SIZE_VALUE), \
.pinning = (PIN), \
.max_elem = (MAX_ELEM), \
}
#endif
#ifndef BPF_HASH
# define BPF_HASH(NAME, ID, SIZE_KEY, SIZE_VALUE, PIN, MAX_ELEM) \
__BPF_MAP(NAME, BPF_MAP_TYPE_HASH, ID, SIZE_KEY, SIZE_VALUE, \
PIN, MAX_ELEM)
#endif
#ifndef BPF_ARRAY
# define BPF_ARRAY(NAME, ID, SIZE_VALUE, PIN, MAX_ELEM) \
__BPF_MAP(NAME, BPF_MAP_TYPE_ARRAY, ID, sizeof(uint32_t), \
SIZE_VALUE, PIN, MAX_ELEM)
#endif
#ifndef BPF_ARRAY2
# define BPF_ARRAY2(NAME, ID, PIN, MAX_ELEM) \
BPF_ARRAY(NAME, ID, sizeof(uint16_t), PIN, MAX_ELEM)
#endif
#ifndef BPF_ARRAY4
# define BPF_ARRAY4(NAME, ID, PIN, MAX_ELEM) \
BPF_ARRAY(NAME, ID, sizeof(uint32_t), PIN, MAX_ELEM)
#endif
#ifndef BPF_ARRAY8
# define BPF_ARRAY8(NAME, ID, PIN, MAX_ELEM) \
BPF_ARRAY(NAME, ID, sizeof(uint64_t), PIN, MAX_ELEM)
#endif
#ifndef BPF_PROG_ARRAY
# define BPF_PROG_ARRAY(NAME, ID, PIN, MAX_ELEM) \
__BPF_MAP(NAME, BPF_MAP_TYPE_PROG_ARRAY, ID, sizeof(uint32_t), \
sizeof(uint32_t), PIN, MAX_ELEM)
#endif
/** Classifier helper */ /** Classifier helper */
#ifndef BPF_H_DEFAULT #ifndef BPF_H_DEFAULT
......
...@@ -32,6 +32,7 @@ struct bpf_elf_map { ...@@ -32,6 +32,7 @@ struct bpf_elf_map {
__u32 size_key; __u32 size_key;
__u32 size_value; __u32 size_value;
__u32 max_elem; __u32 max_elem;
__u32 flags;
__u32 id; __u32 id;
__u32 pinning; __u32 pinning;
}; };
......
...@@ -231,6 +231,9 @@ static void bpf_map_pin_report(const struct bpf_elf_map *pin, ...@@ -231,6 +231,9 @@ static void bpf_map_pin_report(const struct bpf_elf_map *pin,
if (obj->max_elem != pin->max_elem) if (obj->max_elem != pin->max_elem)
fprintf(stderr, " - Max elems: %u (obj) != %u (pin)\n", fprintf(stderr, " - Max elems: %u (obj) != %u (pin)\n",
obj->max_elem, pin->max_elem); obj->max_elem, pin->max_elem);
if (obj->flags != pin->flags)
fprintf(stderr, " - Flags: %#x (obj) != %#x (pin)\n",
obj->flags, pin->flags);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
...@@ -261,6 +264,8 @@ static int bpf_map_selfcheck_pinned(int fd, const struct bpf_elf_map *map, ...@@ -261,6 +264,8 @@ static int bpf_map_selfcheck_pinned(int fd, const struct bpf_elf_map *map,
tmp.size_value = val; tmp.size_value = val;
else if (sscanf(buff, "max_entries:\t%u", &val) == 1) else if (sscanf(buff, "max_entries:\t%u", &val) == 1)
tmp.max_elem = val; tmp.max_elem = val;
else if (sscanf(buff, "map_flags:\t%i", &val) == 1)
tmp.flags = val;
} }
fclose(fp); fclose(fp);
...@@ -796,8 +801,9 @@ static int bpf_log_realloc(struct bpf_elf_ctx *ctx) ...@@ -796,8 +801,9 @@ static int bpf_log_realloc(struct bpf_elf_ctx *ctx)
return 0; return 0;
} }
static int bpf_map_create(enum bpf_map_type type, unsigned int size_key, static int bpf_map_create(enum bpf_map_type type, uint32_t size_key,
unsigned int size_value, unsigned int max_elem) uint32_t size_value, uint32_t max_elem,
uint32_t flags)
{ {
union bpf_attr attr; union bpf_attr attr;
...@@ -806,6 +812,7 @@ static int bpf_map_create(enum bpf_map_type type, unsigned int size_key, ...@@ -806,6 +812,7 @@ static int bpf_map_create(enum bpf_map_type type, unsigned int size_key,
attr.key_size = size_key; attr.key_size = size_key;
attr.value_size = size_value; attr.value_size = size_value;
attr.max_entries = max_elem; attr.max_entries = max_elem;
attr.map_flags = flags;
return bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
} }
...@@ -1147,7 +1154,8 @@ static void bpf_map_report(int fd, const char *name, ...@@ -1147,7 +1154,8 @@ static void bpf_map_report(int fd, const char *name,
fprintf(stderr, " - Pinning: %u\n", map->pinning); fprintf(stderr, " - Pinning: %u\n", map->pinning);
fprintf(stderr, " - Size key: %u\n", map->size_key); fprintf(stderr, " - Size key: %u\n", map->size_key);
fprintf(stderr, " - Size value: %u\n", map->size_value); fprintf(stderr, " - Size value: %u\n", map->size_value);
fprintf(stderr, " - Max elems: %u\n\n", map->max_elem); fprintf(stderr, " - Max elems: %u\n", map->max_elem);
fprintf(stderr, " - Flags: %#x\n\n", map->flags);
} }
static int bpf_map_attach(const char *name, const struct bpf_elf_map *map, static int bpf_map_attach(const char *name, const struct bpf_elf_map *map,
...@@ -1174,7 +1182,7 @@ static int bpf_map_attach(const char *name, const struct bpf_elf_map *map, ...@@ -1174,7 +1182,7 @@ static int bpf_map_attach(const char *name, const struct bpf_elf_map *map,
errno = 0; errno = 0;
fd = bpf_map_create(map->type, map->size_key, map->size_value, fd = bpf_map_create(map->type, map->size_key, map->size_value,
map->max_elem); map->max_elem, map->flags);
if (fd < 0 || ctx->verbose) { if (fd < 0 || ctx->verbose) {
bpf_map_report(fd, name, map, ctx); bpf_map_report(fd, name, map, ctx);
if (fd < 0) if (fd < 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment