P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
ebpf_kernel.h
1/*
2Copyright 2018 VMware, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17/*
18 * This file contains all functions and definitions necessary for the kernel target C
19 * code to compile. It must be included with any file generated by the p4c-ebpf kernel
20 * compiler.
21 */
22
23#ifndef BACKENDS_EBPF_RUNTIME_EBPF_KERNEL_H_
24#define BACKENDS_EBPF_RUNTIME_EBPF_KERNEL_H_
25
26#include "ebpf_common.h"
27
28#include <bpf/bpf_endian.h> // definitions for bpf_ntohs etc...
29
30#undef htonl
31#undef htons
32#define htons(d) bpf_htons(d)
33#define htonl(d) bpf_htonl(d)
34#define htonll(d) bpf_cpu_to_be64(d)
35#define ntohll(x) bpf_be64_to_cpu(x)
36#ifndef bpf_htonll
37#define bpf_htonll(x) htonll(x)
38#endif
39
40#define load_byte(data, b) (*(((u8*)(data)) + (b)))
41#define load_half(data, b) bpf_ntohs(*(u16 *)((u8*)(data) + (b)))
42#define load_word(data, b) bpf_ntohl(*(u32 *)((u8*)(data) + (b)))
43#define load_dword(data, b) bpf_be64_to_cpu(*(u64 *)((u8*)(data) + (b)))
44
45
46/* If we operate in user space we only need to include bpf.h and
47 * define the userspace API macros.
48 * For kernel programs we need to specify a list of kernel helpers. These are
49 * taken from here: https://github.com/torvalds/linux/blob/master/tools/testing/selftests/bpf/bpf_helpers.h
50 */
51#ifdef CONTROL_PLANE // BEGIN EBPF USER SPACE DEFINITIONS
52
53#include <bpf/bpf.h> // bpf_obj_get/pin, bpf_map_update_elem
54
55#define BPF_USER_MAP_UPDATE_ELEM(index, key, value, flags)\
56 bpf_map_update_elem(index, key, value, flags)
57#define BPF_OBJ_PIN(table, name) bpf_obj_pin(table, name)
58#define BPF_OBJ_GET(name) bpf_obj_get(name)
59
60#else // BEGIN EBPF KERNEL DEFINITIONS
61
62#include <linux/pkt_cls.h> // TC_ACT_OK, TC_ACT_SHOT
63#include "linux/bpf.h" // types, and general bpf definitions
64// This file contains the definitions of all the kernel bpf essentials
65#include <bpf/bpf_helpers.h>
66
67/* a helper structure used by an eBPF C program
68 * to describe map attributes for the elf_bpf loader
69 * FIXME: We only need this because we are loading with iproute2
70 */
72 __u32 type;
73 __u32 size_key;
74 __u32 size_value;
75 __u32 max_elem;
76 __u32 flags;
77 __u32 id;
78 __u32 pinning;
79 __u32 inner_id;
80 __u32 inner_idx;
81};
82
83/* simple descriptor which replaces the kernel sk_buff structure */
84#define SK_BUFF struct __sk_buff
85
86/* from iproute2, annotate table with BTF which allows to read types at runtime */
87#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val) \
88 struct ____btf_map_##name { \
89 type_key key; \
90 type_val value; \
91 }; \
92 struct ____btf_map_##name \
93 __attribute__ ((section(".maps." #name), used)) \
94 ____btf_map_##name = {};
95
96#define REGISTER_START()
97#ifndef BTF
98/* Note: pinning exports the table name globally, do not remove */
99#define REGISTER_TABLE(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES) \
100struct bpf_elf_map SEC("maps") NAME = { \
101 .type = TYPE, \
102 .size_key = sizeof(KEY_TYPE), \
103 .size_value = sizeof(VALUE_TYPE), \
104 .max_elem = MAX_ENTRIES, \
105 .pinning = 2, \
106 .flags = 0, \
107};
108#define REGISTER_TABLE_INNER(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, ID, INNER_IDX) \
109struct bpf_elf_map SEC("maps") NAME = { \
110 .type = TYPE, \
111 .size_key = sizeof(KEY_TYPE), \
112 .size_value = sizeof(VALUE_TYPE), \
113 .max_elem = MAX_ENTRIES, \
114 .pinning = 2, \
115 .flags = 0, \
116 .id = ID, \
117 .inner_idx = INNER_IDX, \
118};
119#define REGISTER_TABLE_OUTER(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, INNER_ID, INNER_NAME) \
120struct bpf_elf_map SEC("maps") NAME = { \
121 .type = TYPE, \
122 .size_key = sizeof(KEY_TYPE), \
123 .size_value = sizeof(VALUE_TYPE), \
124 .max_elem = MAX_ENTRIES, \
125 .pinning = 2, \
126 .flags = 0, \
127 .inner_id = INNER_ID, \
128};
129#define REGISTER_TABLE_FLAGS(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, FLAGS) \
130struct bpf_elf_map SEC("maps") NAME = { \
131 .type = TYPE, \
132 .size_key = sizeof(KEY_TYPE), \
133 .size_value = sizeof(VALUE_TYPE), \
134 .max_elem = MAX_ENTRIES, \
135 .pinning = 2, \
136 .flags = FLAGS, \
137};
138#else
139#define REGISTER_TABLE(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES) \
140struct { \
141 __uint(type, TYPE); \
142 KEY_TYPE *key; \
143 VALUE_TYPE *value; \
144 __uint(max_entries, MAX_ENTRIES); \
145 __uint(pinning, LIBBPF_PIN_BY_NAME); \
146} NAME SEC(".maps");
147#define REGISTER_TABLE_FLAGS(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, FLAGS) \
148struct { \
149 __uint(type, TYPE); \
150 KEY_TYPE *key; \
151 VALUE_TYPE *value; \
152 __uint(max_entries, MAX_ENTRIES); \
153 __uint(pinning, LIBBPF_PIN_BY_NAME); \
154 __uint(map_flags, FLAGS); \
155} NAME SEC(".maps");
156#define REGISTER_TABLE_INNER(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, ID, INNER_IDX) \
157struct NAME { \
158 __uint(type, TYPE); \
159 KEY_TYPE *key; \
160 VALUE_TYPE *value; \
161 __uint(max_entries, MAX_ENTRIES); \
162} NAME SEC(".maps");
163#define REGISTER_TABLE_OUTER(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, INNER_ID, INNER_NAME) \
164struct { \
165 __uint(type, TYPE); \
166 KEY_TYPE *key; \
167 VALUE_TYPE *value; \
168 __uint(max_entries, MAX_ENTRIES); \
169 __uint(pinning, LIBBPF_PIN_BY_NAME); \
170 __array(values, struct INNER_NAME); \
171} NAME SEC(".maps");
172#define REGISTER_TABLE_NO_KEY_TYPE(NAME, TYPE, KEY_SIZE, VALUE_TYPE, MAX_ENTRIES) \
173struct { \
174 __uint(type, TYPE); \
175 __uint(key_size, KEY_SIZE); \
176 VALUE_TYPE *value; \
177 __uint(max_entries, MAX_ENTRIES); \
178 __uint(pinning, LIBBPF_PIN_BY_NAME); \
179} NAME SEC(".maps");
180#endif
181#define REGISTER_END()
182
183#define BPF_MAP_LOOKUP_ELEM(table, key) \
184 bpf_map_lookup_elem(&table, key)
185#define BPF_MAP_UPDATE_ELEM(table, key, value, flags) \
186 bpf_map_update_elem(&table, key, value, flags)
187#define BPF_MAP_DELETE_ELEM(table, key) \
188 bpf_map_delete_elem(&table, key)
189#define BPF_USER_MAP_UPDATE_ELEM(index, key, value, flags)\
190 bpf_update_elem(index, key, value, flags)
191#define BPF_OBJ_PIN(table, name) bpf_obj_pin(table, name)
192#define BPF_OBJ_GET(name) bpf_obj_get(name)
193
194#endif // END EBPF KERNEL DEFINITIONS
195
196#endif // BACKENDS_EBPF_RUNTIME_EBPF_KERNEL_H_
Definition ebpf_kernel.h:71