P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
ebpf_test.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 user space
19 * target C code to compile. It is part of the extended ebpf testing framework
20 * and must be included with any file generated by the p4c-ebpf test compiler.
21 * This file also depends on the ebpf_registry, which defines a central table
22 * repository as well as various ebpf map operations.
23 */
24
25#ifndef BACKENDS_EBPF_RUNTIME_EBPF_USER_H_
26#define BACKENDS_EBPF_RUNTIME_EBPF_USER_H_
27
28#include "ebpf_registry.h"
29#include "ebpf_common.h"
30
31#include <endian.h>
32
33/* define some byte order conversions, these mimic bpf_endian.h */
34#define htonll(x) htobe64(x)
35#define ntohll(x) be64toh(x)
36
37#define bpf_htons(x) htobe16(x)
38#define bpf_ntohs(x) be16toh(x)
39#define bpf_htonl(x) htobe32(x)
40#define bpf_ntohl(x) be32toh(x)
41#define bpf_cpu_to_be64(x) htobe64(x)
42#define bpf_be64_to_cpu(x) be64toh(x)
43
44#define load_byte(data, b) (*(((u8*)(data)) + (b)))
45#define load_half(data, b) bpf_ntohs(*(u16 *)((u8*)(data) + (b)))
46#define load_word(data, b) bpf_ntohl(*(u32 *)((u8*)(data) + (b)))
47#define load_dword(data, b) bpf_be64_to_cpu(*(u64 *)((u8*)(data) + (b)))
48
49
50
51#define bpf_printk(fmt, ...) \
52 ({ \
53 char ____fmt[] = fmt; \
54 printf(____fmt, sizeof(____fmt), \
55 ##__VA_ARGS__); \
56 })
57
63#define SEC(NAME)
64
65/* simple descriptor which replaces the kernel sk_buff structure */
66struct sk_buff {
67 void *data;
68 u16 len;
69 u32 ifindex;
70};
71
72/* flags for BPF_MAP_UPDATE_ELEM command, copied from "linux/bpf" */
73#define BPF_ANY 0 /* create new element or update existing */
74#define BPF_NOEXIST 1 /* create new element if it didn't exist */
75#define BPF_EXIST 2 /* update existing element */
76#define BPF_F_LOCK 4 /* spin_lock-ed map_lookup/map_update */
77
78/* Supported bpf map types */
79enum bpf_map_type {
80 BPF_MAP_TYPE_HASH,
81 BPF_MAP_TYPE_ARRAY,
82};
83
84
85
86#define SK_BUFF struct sk_buff
87#define REGISTER_START() \
88struct bpf_table tables[] = {
89#define REGISTER_TABLE(NAME, TYPE, KEY_SIZE, VALUE_SIZE, MAX_ENTRIES) \
90 { MAP_PATH"/"#NAME, TYPE, KEY_SIZE, VALUE_SIZE, MAX_ENTRIES, NULL },
91#define REGISTER_END() \
92 { 0, 0, 0, 0, 0 } \
93};
94
95#define BPF_MAP_LOOKUP_ELEM(table, key) \
96 registry_lookup_table_elem(MAP_PATH"/"#table, key)
97#define BPF_MAP_UPDATE_ELEM(table, key, value, flags) \
98 registry_update_table(MAP_PATH"/"#table, key, value, flags)
99#define BPF_MAP_DELETE_ELEM(table, key) \
100 registry_delete_table_elem(MAP_PATH"/"#table, key)
101#define BPF_USER_MAP_UPDATE_ELEM(index, key, value, flags)\
102 registry_update_table_id(index, key, value, flags)
103#define BPF_OBJ_PIN(table, name) registry_add(table)
104#define BPF_OBJ_GET(name) registry_get_id(name)
105
106
107/* These should be automatically generated and included in the generated x.h header file */
108extern struct bpf_table tables[];
109extern int ebpf_filter(struct sk_buff *skb);
110
111
112#endif // BACKENDS_EBPF_RUNTIME_EBPF_USER_H_
A helper structure used to describe attributes.
Definition ebpf_registry.h:41
Definition ebpf_test.h:66