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
21#ifndef BACKENDS_EBPF_RUNTIME_EBPF_KERNEL_H_
22#define BACKENDS_EBPF_RUNTIME_EBPF_KERNEL_H_
23
24#include "ebpf_common.h"
25
26#include <bpf/bpf_endian.h> // definitions for bpf_ntohs etc...
27
28#undef htonl
29#undef htons
30#define htons(d) bpf_htons(d)
31#define htonl(d) bpf_htonl(d)
32#define htonll(d) bpf_cpu_to_be64(d)
33#define ntohll(x) bpf_be64_to_cpu(x)
34#ifndef bpf_htonll
35#define bpf_htonll(x) htonll(x)
36#endif
37
38#define load_byte(data, b) (*(((u8*)(data)) + (b)))
39#define load_half(data, b) bpf_ntohs(*(u16 *)((u8*)(data) + (b)))
40#define load_word(data, b) bpf_ntohl(*(u32 *)((u8*)(data) + (b)))
41#define load_dword(data, b) bpf_be64_to_cpu(*(u64 *)((u8*)(data) + (b)))
42
47
48#ifdef CONTROL_PLANE // BEGIN EBPF USER SPACE DEFINITIONS
49
50#include <bpf/bpf.h> // bpf_obj_get/pin, bpf_map_update_elem
51
52#define BPF_USER_MAP_UPDATE_ELEM(index, key, value, flags)\
53 bpf_map_update_elem(index, key, value, flags)
54#define BPF_OBJ_PIN(table, name) bpf_obj_pin(table, name)
55#define BPF_OBJ_GET(name) bpf_obj_get(name)
56
57#else // BEGIN EBPF KERNEL DEFINITIONS
58
59#include <linux/pkt_cls.h> // TC_ACT_OK, TC_ACT_SHOT
60#include "linux/bpf.h" // types, and general bpf definitions
61// This file contains the definitions of all the kernel bpf essentials
62#include <bpf/bpf_helpers.h>
63
68 __u32 type;
69 __u32 size_key;
70 __u32 size_value;
71 __u32 max_elem;
72 __u32 flags;
73 __u32 id;
74 __u32 pinning;
75 __u32 inner_id;
76 __u32 inner_idx;
77};
78
80#define SK_BUFF struct __sk_buff
81
83#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val) \
84 struct ____btf_map_##name { \
85 type_key key; \
86 type_val value; \
87 }; \
88 struct ____btf_map_##name \
89 __attribute__ ((section(".maps." #name), used)) \
90 ____btf_map_##name = {};
91
92#define REGISTER_START()
93#ifndef BTF
95#define REGISTER_TABLE(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES) \
96struct bpf_elf_map SEC("maps") NAME = { \
97 .type = TYPE, \
98 .size_key = sizeof(KEY_TYPE), \
99 .size_value = sizeof(VALUE_TYPE), \
100 .max_elem = MAX_ENTRIES, \
101 .pinning = 2, \
102 .flags = 0, \
103};
104#define REGISTER_TABLE_INNER(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, ID, INNER_IDX) \
105struct bpf_elf_map SEC("maps") NAME = { \
106 .type = TYPE, \
107 .size_key = sizeof(KEY_TYPE), \
108 .size_value = sizeof(VALUE_TYPE), \
109 .max_elem = MAX_ENTRIES, \
110 .pinning = 2, \
111 .flags = 0, \
112 .id = ID, \
113 .inner_idx = INNER_IDX, \
114};
115#define REGISTER_TABLE_OUTER(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, INNER_ID, INNER_NAME) \
116struct bpf_elf_map SEC("maps") NAME = { \
117 .type = TYPE, \
118 .size_key = sizeof(KEY_TYPE), \
119 .size_value = sizeof(VALUE_TYPE), \
120 .max_elem = MAX_ENTRIES, \
121 .pinning = 2, \
122 .flags = 0, \
123 .inner_id = INNER_ID, \
124};
125#define REGISTER_TABLE_FLAGS(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, FLAGS) \
126struct bpf_elf_map SEC("maps") NAME = { \
127 .type = TYPE, \
128 .size_key = sizeof(KEY_TYPE), \
129 .size_value = sizeof(VALUE_TYPE), \
130 .max_elem = MAX_ENTRIES, \
131 .pinning = 2, \
132 .flags = FLAGS, \
133};
134#else
135#define REGISTER_TABLE(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES) \
136struct { \
137 __uint(type, TYPE); \
138 KEY_TYPE *key; \
139 VALUE_TYPE *value; \
140 __uint(max_entries, MAX_ENTRIES); \
141 __uint(pinning, LIBBPF_PIN_BY_NAME); \
142} NAME SEC(".maps");
143#define REGISTER_TABLE_FLAGS(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, FLAGS) \
144struct { \
145 __uint(type, TYPE); \
146 KEY_TYPE *key; \
147 VALUE_TYPE *value; \
148 __uint(max_entries, MAX_ENTRIES); \
149 __uint(pinning, LIBBPF_PIN_BY_NAME); \
150 __uint(map_flags, FLAGS); \
151} NAME SEC(".maps");
152#define REGISTER_TABLE_INNER(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, ID, INNER_IDX) \
153struct NAME { \
154 __uint(type, TYPE); \
155 KEY_TYPE *key; \
156 VALUE_TYPE *value; \
157 __uint(max_entries, MAX_ENTRIES); \
158} NAME SEC(".maps");
159#define REGISTER_TABLE_OUTER(NAME, TYPE, KEY_TYPE, VALUE_TYPE, MAX_ENTRIES, INNER_ID, INNER_NAME) \
160struct { \
161 __uint(type, TYPE); \
162 KEY_TYPE *key; \
163 VALUE_TYPE *value; \
164 __uint(max_entries, MAX_ENTRIES); \
165 __uint(pinning, LIBBPF_PIN_BY_NAME); \
166 __array(values, struct INNER_NAME); \
167} NAME SEC(".maps");
168#define REGISTER_TABLE_NO_KEY_TYPE(NAME, TYPE, KEY_SIZE, VALUE_TYPE, MAX_ENTRIES) \
169struct { \
170 __uint(type, TYPE); \
171 __uint(key_size, KEY_SIZE); \
172 VALUE_TYPE *value; \
173 __uint(max_entries, MAX_ENTRIES); \
174 __uint(pinning, LIBBPF_PIN_BY_NAME); \
175} NAME SEC(".maps");
176#endif
177#define REGISTER_END()
178
179#define BPF_MAP_LOOKUP_ELEM(table, key) \
180 bpf_map_lookup_elem(&table, key)
181#define BPF_MAP_UPDATE_ELEM(table, key, value, flags) \
182 bpf_map_update_elem(&table, key, value, flags)
183#define BPF_MAP_DELETE_ELEM(table, key) \
184 bpf_map_delete_elem(&table, key)
185#define BPF_USER_MAP_UPDATE_ELEM(index, key, value, flags)\
186 bpf_update_elem(index, key, value, flags)
187#define BPF_OBJ_PIN(table, name) bpf_obj_pin(table, name)
188#define BPF_OBJ_GET(name) bpf_obj_get(name)
189
190#endif // END EBPF KERNEL DEFINITIONS
191
192#endif // BACKENDS_EBPF_RUNTIME_EBPF_KERNEL_H_
Definition ebpf_kernel.h:67