Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpf: convert majority of bpf_elf_map definitions to BTF map definitions #17640

Merged
merged 5 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions bpf/bpf_sock.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2019-2020 Authors of Cilium */
/* Copyright (C) 2019-2021 Authors of Cilium */

#include <bpf/ctx/unspec.h>
#include <bpf/api.h>
Expand Down Expand Up @@ -179,13 +179,13 @@ bool sock_proto_enabled(__u32 proto)

#ifdef ENABLE_IPV4
#if defined(ENABLE_HOST_SERVICES_UDP) || defined(ENABLE_HOST_SERVICES_PEER)
struct bpf_elf_map __section_maps LB4_REVERSE_NAT_SK_MAP = {
.type = BPF_MAP_TYPE_LRU_HASH,
.size_key = sizeof(struct ipv4_revnat_tuple),
.size_value = sizeof(struct ipv4_revnat_entry),
.pinning = PIN_GLOBAL_NS,
.max_elem = LB4_REVERSE_NAT_SK_MAP_SIZE,
};
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__type(key, struct ipv4_revnat_tuple);
__type(value, struct ipv4_revnat_entry);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, LB4_REVERSE_NAT_SK_MAP_SIZE);
} LB4_REVERSE_NAT_SK_MAP __section_maps_btf;

static __always_inline int sock4_update_revnat(struct bpf_sock_addr *ctx,
const struct lb4_backend *backend,
Expand Down Expand Up @@ -622,13 +622,13 @@ int sock4_getpeername(struct bpf_sock_addr *ctx)
#if defined(ENABLE_IPV6) || defined(ENABLE_IPV4)
#ifdef ENABLE_IPV6
#if defined(ENABLE_HOST_SERVICES_UDP) || defined(ENABLE_HOST_SERVICES_PEER)
struct bpf_elf_map __section_maps LB6_REVERSE_NAT_SK_MAP = {
.type = BPF_MAP_TYPE_LRU_HASH,
.size_key = sizeof(struct ipv6_revnat_tuple),
.size_value = sizeof(struct ipv6_revnat_entry),
.pinning = PIN_GLOBAL_NS,
.max_elem = LB6_REVERSE_NAT_SK_MAP_SIZE,
};
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__type(key, struct ipv6_revnat_tuple);
__type(value, struct ipv6_revnat_entry);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, LB6_REVERSE_NAT_SK_MAP_SIZE);
} LB6_REVERSE_NAT_SK_MAP __section_maps_btf;

static __always_inline int sock6_update_revnat(struct bpf_sock_addr *ctx,
const struct lb6_backend *backend,
Expand Down
67 changes: 34 additions & 33 deletions bpf/bpf_xdp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2017-2020 Authors of Cilium */
/* Copyright (C) 2017-2021 Authors of Cilium */

#include <bpf/ctx/xdp.h>
#include <bpf/api.h>
Expand Down Expand Up @@ -44,46 +44,47 @@
#endif

#ifdef CIDR4_FILTER
struct bpf_elf_map __section_maps CIDR4_HMAP_NAME = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(struct lpm_v4_key),
.size_value = sizeof(struct lpm_val),
.flags = BPF_F_NO_PREALLOC,
.pinning = PIN_GLOBAL_NS,
.max_elem = CIDR4_HMAP_ELEMS,
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, struct lpm_v4_key);
__type(value, struct lpm_val);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CIDR4_HMAP_ELEMS);
__uint(map_flags, BPF_F_NO_PREALLOC);
} CIDR4_HMAP_NAME __section_maps_btf;

#ifdef CIDR4_LPM_PREFILTER
struct bpf_elf_map __section_maps CIDR4_LMAP_NAME = {
.type = BPF_MAP_TYPE_LPM_TRIE,
.size_key = sizeof(struct lpm_v4_key),
.size_value = sizeof(struct lpm_val),
.flags = BPF_F_NO_PREALLOC,
.pinning = PIN_GLOBAL_NS,
.max_elem = CIDR4_LMAP_ELEMS,
};
struct {
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
__type(key, struct lpm_v4_key);
__type(value, struct lpm_val);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CIDR4_LMAP_ELEMS);
__uint(map_flags, BPF_F_NO_PREALLOC);
} CIDR4_LMAP_NAME __section_maps_btf;

#endif /* CIDR4_LPM_PREFILTER */
#endif /* CIDR4_FILTER */

#ifdef CIDR6_FILTER
struct bpf_elf_map __section_maps CIDR6_HMAP_NAME = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(struct lpm_v6_key),
.size_value = sizeof(struct lpm_val),
.flags = BPF_F_NO_PREALLOC,
.pinning = PIN_GLOBAL_NS,
.max_elem = CIDR4_HMAP_ELEMS,
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, struct lpm_v6_key);
__type(value, struct lpm_val);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CIDR4_HMAP_ELEMS);
__uint(map_flags, BPF_F_NO_PREALLOC);
} CIDR6_HMAP_NAME __section_maps_btf;

#ifdef CIDR6_LPM_PREFILTER
struct bpf_elf_map __section_maps CIDR6_LMAP_NAME = {
.type = BPF_MAP_TYPE_LPM_TRIE,
.size_key = sizeof(struct lpm_v6_key),
.size_value = sizeof(struct lpm_val),
.flags = BPF_F_NO_PREALLOC,
.pinning = PIN_GLOBAL_NS,
.max_elem = CIDR4_LMAP_ELEMS,
};
struct {
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
__type(key, struct lpm_v6_key);
__type(value, struct lpm_val);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CIDR4_LMAP_ELEMS);
__uint(map_flags, BPF_F_NO_PREALLOC);
} CIDR6_LMAP_NAME __section_maps_btf;
#endif /* CIDR6_LPM_PREFILTER */
#endif /* CIDR6_FILTER */
#endif /* ENABLE_PREFILTER */
Expand Down
12 changes: 6 additions & 6 deletions bpf/custom/bytecount.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
/* Copyright (C) 2021 Authors of Cilium */

struct bpf_elf_map __section_maps bytecount_map = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(__u32),
.size_value = sizeof(__u64),
.max_elem = 1024,
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __u32);
__type(value, __u64);
__uint(max_entries, 1024);
} bytecount_map __section_maps_btf;

static __always_inline
void custom_prog(const struct __ctx_buff *ctx, __u32 identity)
Expand Down
16 changes: 8 additions & 8 deletions bpf/include/bpf/ctx/xdp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (C) 2020 Authors of Cilium */
/* Copyright (C) 2020-2021 Authors of Cilium */

#ifndef __BPF_CTX_XDP_H_
#define __BPF_CTX_XDP_H_
Expand Down Expand Up @@ -271,13 +271,13 @@ ctx_wire_len(const struct xdp_md *ctx)
return ctx_full_len(ctx);
}

struct bpf_elf_map __section_maps cilium_xdp_scratch = {
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
.size_key = sizeof(int),
.size_value = META_PIVOT,
.pinning = PIN_GLOBAL_NS,
.max_elem = 1,
};
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__type(key, int);
__uint(value_size, META_PIVOT);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, 1);
} cilium_xdp_scratch __section_maps_btf;

static __always_inline __maybe_unused void
ctx_store_meta(struct xdp_md *ctx __maybe_unused, const __u64 off, __u32 datum)
Expand Down
10 changes: 7 additions & 3 deletions bpf/include/bpf/loader.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (C) 2016-2020 Authors of Cilium */
/* Copyright (C) 2016-2021 Authors of Cilium */

#ifndef __BPF_LOADER__
#define __BPF_LOADER__

#include <linux/types.h>

#define __uint(name, val) int(*(name))[val]
#define __type(name, val) typeof(val) *(name)
#define __array(name, val) typeof(val) *(name)[]
ti-mo marked this conversation as resolved.
Show resolved Hide resolved

#define PIN_NONE 0
#define PIN_OBJECT_NS 1
#define PIN_GLOBAL_NS 2

#define LIBBPF_PIN_BY_NAME 1

struct bpf_elf_map {
__u32 type;
__u32 size_key;
Expand All @@ -22,6 +28,4 @@ struct bpf_elf_map {
__u32 inner_idx;
};

#define NO_PREPOPULATE -1

#endif /* __BPF_LOADER__ */
6 changes: 5 additions & 1 deletion bpf/include/bpf/section.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (C) 2016-2020 Authors of Cilium */
/* Copyright (C) 2016-2021 Authors of Cilium */

#ifndef __BPF_SECTION__
#define __BPF_SECTION__
Expand All @@ -18,6 +18,10 @@
# define __section_maps __section("maps")
#endif

#ifndef __section_maps_btf
# define __section_maps_btf __section(".maps")
#endif

#ifndef BPF_LICENSE
# define BPF_LICENSE(NAME) \
char ____license[] __section_license = NAME
Expand Down
5 changes: 0 additions & 5 deletions bpf/lib/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,6 @@ enum {
/* We cap key index at 4 bits because mark value is used to map ctx to key */
#define MAX_KEY_INDEX 15

/* encrypt_key is the index into the encrypt map */
struct encrypt_key {
__u32 ctx;
} __packed;

/* encrypt_config is the current encryption context on the node */
struct encrypt_config {
__u8 encrypt_key;
Expand Down
70 changes: 35 additions & 35 deletions bpf/lib/conntrack_map.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (C) 2016-2020 Authors of Cilium */
/* Copyright (C) 2016-2021 Authors of Cilium */

#ifndef __LIB_CONNTRACK_MAP_H_
#define __LIB_CONNTRACK_MAP_H_
Expand All @@ -15,29 +15,29 @@
#endif

#ifdef ENABLE_IPV6
struct bpf_elf_map __section_maps CT_MAP_TCP6 = {
.type = CT_MAP_TYPE,
.size_key = sizeof(struct ipv6_ct_tuple),
.size_value = sizeof(struct ct_entry),
.pinning = PIN_GLOBAL_NS,
.max_elem = CT_MAP_SIZE_TCP,
struct {
__uint(type, CT_MAP_TYPE);
__type(key, struct ipv6_ct_tuple);
__type(value, struct ct_entry);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CT_MAP_SIZE_TCP);
#ifndef HAVE_LRU_HASH_MAP_TYPE
.flags = CONDITIONAL_PREALLOC,
__uint(map_flags, CONDITIONAL_PREALLOC);
#endif
};
} CT_MAP_TCP6 __section_maps_btf;

struct bpf_elf_map __section_maps CT_MAP_ANY6 = {
.type = CT_MAP_TYPE,
.size_key = sizeof(struct ipv6_ct_tuple),
.size_value = sizeof(struct ct_entry),
.pinning = PIN_GLOBAL_NS,
.max_elem = CT_MAP_SIZE_ANY,
struct {
__uint(type, CT_MAP_TYPE);
__type(key, struct ipv6_ct_tuple);
__type(value, struct ct_entry);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CT_MAP_SIZE_ANY);
#ifndef HAVE_LRU_HASH_MAP_TYPE
.flags = CONDITIONAL_PREALLOC,
__uint(map_flags, CONDITIONAL_PREALLOC);
#endif
};
} CT_MAP_ANY6 __section_maps_btf;

static __always_inline struct bpf_elf_map *
static __always_inline void *
get_ct_map6(const struct ipv6_ct_tuple *tuple)
{
if (tuple->nexthdr == IPPROTO_TCP)
Expand All @@ -48,29 +48,29 @@ get_ct_map6(const struct ipv6_ct_tuple *tuple)
#endif

#ifdef ENABLE_IPV4
struct bpf_elf_map __section_maps CT_MAP_TCP4 = {
.type = CT_MAP_TYPE,
.size_key = sizeof(struct ipv4_ct_tuple),
.size_value = sizeof(struct ct_entry),
.pinning = PIN_GLOBAL_NS,
.max_elem = CT_MAP_SIZE_TCP,
struct {
__uint(type, CT_MAP_TYPE);
__type(key, struct ipv4_ct_tuple);
__type(value, struct ct_entry);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CT_MAP_SIZE_TCP);
#ifndef HAVE_LRU_HASH_MAP_TYPE
.flags = CONDITIONAL_PREALLOC,
__uint(map_flags, CONDITIONAL_PREALLOC);
#endif
};
} CT_MAP_TCP4 __section_maps_btf;

struct bpf_elf_map __section_maps CT_MAP_ANY4 = {
.type = CT_MAP_TYPE,
.size_key = sizeof(struct ipv4_ct_tuple),
.size_value = sizeof(struct ct_entry),
.pinning = PIN_GLOBAL_NS,
.max_elem = CT_MAP_SIZE_ANY,
struct {
__uint(type, CT_MAP_TYPE);
__type(key, struct ipv4_ct_tuple);
__type(value, struct ct_entry);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CT_MAP_SIZE_ANY);
#ifndef HAVE_LRU_HASH_MAP_TYPE
.flags = CONDITIONAL_PREALLOC,
__uint(map_flags, CONDITIONAL_PREALLOC);
#endif
};
} CT_MAP_ANY4 __section_maps_btf;

static __always_inline struct bpf_elf_map *
static __always_inline void *
get_ct_map4(const struct ipv4_ct_tuple *tuple)
{
if (tuple->nexthdr == IPPROTO_TCP)
Expand Down
8 changes: 4 additions & 4 deletions bpf/lib/eps.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (C) 2017-2020 Authors of Cilium */
/* Copyright (C) 2017-2021 Authors of Cilium */

#ifndef __LIB_EPS_H_
#define __LIB_EPS_H_
Expand Down Expand Up @@ -72,7 +72,7 @@ lookup_ip4_endpoint_policy_map(__u32 ip)
#define V6_CACHE_KEY_LEN (sizeof(union v6addr)*8)

static __always_inline __maybe_unused struct remote_endpoint_info *
ipcache_lookup6(struct bpf_elf_map *map, const union v6addr *addr,
ipcache_lookup6(const void *map, const union v6addr *addr,
__u32 prefix)
{
struct ipcache_key key = {
Expand All @@ -87,7 +87,7 @@ ipcache_lookup6(struct bpf_elf_map *map, const union v6addr *addr,
#define V4_CACHE_KEY_LEN (sizeof(__u32)*8)

static __always_inline __maybe_unused struct remote_endpoint_info *
ipcache_lookup4(struct bpf_elf_map *map, __be32 addr, __u32 prefix)
ipcache_lookup4(const void *map, __be32 addr, __u32 prefix)
{
struct ipcache_key key = {
.lpm_key = { IPCACHE_PREFIX_LEN(prefix), {} },
Expand Down Expand Up @@ -140,7 +140,7 @@ LPM_LOOKUP_FN(lookup_ip4_remote_endpoint, __be32, IPCACHE4_PREFIXES,

#ifdef ENABLE_EGRESS_GATEWAY
static __always_inline __maybe_unused struct egress_info *
egress_lookup4(struct bpf_elf_map *map, __be32 sip, __be32 dip)
egress_lookup4(const void *map, __be32 sip, __be32 dip)
{
struct egress_key key = {
.lpm_key = { EGRESS_IPV4_PREFIX, {} },
Expand Down