Cilium

Cilium

BPF头文件关于c文件的宏定义

bpf init

bpf_host.c/bpf_lxc.c

others

bpf map

identity

ingress

tail call(lib/tailcall.h)

BPF头文件

cilium dameon在启动的时候会生成一个BPF需要的头文件

1. daemon/cmd/daemon.gofunc(d *Daemon) init() error {
...
 d.createNodeConfigHeaderfile
}
2. pkg/datapath/linux/config/config.go
func (h *HeaderfileWriter) WriteNodeConfig(w io.Writer, cfg *datapath.LocalNodeConfiguration) error {}

这个头文件的格式: bpf/node_config.h, 这个里面定义的map的名字都加上了前缀“test_“, 实际生成的头文件中是没有这个前缀的。bpf程序使用的map定义在bpf/lib/map.h, map的名字是用的宏,宏的定义就是在前面生成的c的头文件中,通过这样的方式,让user mode下go需要的map和bpf里c需要的map对应起来

 13 struct bpf_elf_map __section_maps ENDPOINTS_MAP = {
 14  .type   = BPF_MAP_TYPE_HASH,
 15  .size_key = sizeof(struct endpoint_key),
 16  .size_value = sizeof(struct endpoint_info),
 17  .pinning = PIN_GLOBAL_NS,
 18  .max_elem = ENDPOINTS_MAP_SIZE,
 19  .flags  = CONDITIONAL_PREALLOC,
 20 };
 21
 22 struct bpf_elf_map __section_maps METRICS_MAP = {
 23  .type   = BPF_MAP_TYPE_PERCPU_HASH,
 24  .size_key = sizeof(struct metrics_key),
 25  .size_value = sizeof(struct metrics_value),
 26  .pinning = PIN_GLOBAL_NS,
 27  .max_elem = METRICS_MAP_SIZE,
 28  .flags  = CONDITIONAL_PREALLOC,
 29 };
 30
 75 #define ENDPOINTS_MAP test_cilium_lxc
 76 #define EVENTS_MAP test_cilium_events
 77 #define SIGNAL_MAP test_cilium_signals
 78 #define METRICS_MAP test_cilium_metrics
 79 #define POLICY_CALL_MAP test_cilium_policy
 80 #define SOCK_OPS_MAP test_sock_ops_map
 81 #define IPCACHE_MAP test_cilium_ipcache
 82 #define ENCRYPT_MAP test_cilium_encrypt_state
 83 #define TUNNEL_MAP test_cilium_tunnel_map
 84 #define EP_POLICY_MAP test_cilium_ep_to_policy
 85 #define LB6_REVERSE_NAT_MAP test_cilium_lb6_reverse_nat
 86 #define LB6_SERVICES_MAP_V2 test_cilium_lb6_services
 87 #define LB6_BACKEND_MAP test_cilium_lb6_backends
 88 #define LB6_REVERSE_NAT_SK_MAP test_cilium_lb6_reverse_sk
 89 #define LB6_REVERSE_NAT_SK_MAP_SIZE 262144
 90 #define LB4_REVERSE_NAT_MAP test_cilium_lb4_reverse_nat
 91 #define LB4_SERVICES_MAP_V2 test_cilium_lb4_services
 92 #define LB4_BACKEND_MAP test_cilium_lb4_backends
 93 #define LB4_REVERSE_NAT_SK_MAP test_cilium_lb4_reverse_sk
 94 #define LB4_REVERSE_NAT_SK_MAP_SIZE 262144
 95 #define LB4_AFFINITY_MAP test_cilium_lb4_affinity
 96 #define LB6_AFFINITY_MAP test_cilium_lb6_affinity
 97 #define LB_AFFINITY_MATCH_MAP test_cilium_lb_affinity_match
 98 #define LB_MAGLEV_LUT_SIZE 32749
 99 #define LB4_MAGLEV_MAP_INNER test_cilium_lb4_maglev_inner
100 #define LB4_MAGLEV_MAP_OUTER test_cilium_lb4_maglev_outer
101 #define LB6_MAGLEV_MAP_INNER test_cilium_lb6_maglev_inner
102 #define LB6_MAGLEV_MAP_OUTER test_cilium_lb6_maglev_outer
103 #define THROTTLE_MAP test_cilium_throttle

关于c文件的宏定义

在bpf程序里大量出现的宏定义来决定是否enable某些功能

#ifndef ENABLE_HOST_SERVICES_FULL
#ifdef ENABLE_IPV4

这些c代码的宏定义也是由go代码根据cilium的配置动态生成的

pkg/datapath/linux/config/config.go
func (h *HeaderfileWriter) WriteNodeConfig(w io.Writer, cfg *datapath.LocalNodeConfiguration) error {  cDefinesMap["ENABLE_HOST_SERVICES_FULL"] = 1
}