P4C
The P4 Compiler
Loading...
Searching...
No Matches
ebpf_test.h
1/*
2 * Copyright 2018 VMware, Inc.
3 * SPDX-FileCopyrightText: 2018 VMware, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
13#ifndef BACKENDS_EBPF_RUNTIME_EBPF_USER_H_
14#define BACKENDS_EBPF_RUNTIME_EBPF_USER_H_
15
16#include "ebpf_registry.h"
17#include "ebpf_common.h"
18
19#include <endian.h>
20#include <stdio.h> // printf
21
23#define htonll(x) htobe64(x)
24#define ntohll(x) be64toh(x)
25
26#define bpf_htons(x) htobe16(x)
27#define bpf_ntohs(x) be16toh(x)
28#define bpf_htonl(x) htobe32(x)
29#define bpf_ntohl(x) be32toh(x)
30#define bpf_cpu_to_be64(x) htobe64(x)
31#define bpf_be64_to_cpu(x) be64toh(x)
32
33#define load_byte(data, b) (*(((u8*)(data)) + (b)))
34#define load_half(data, b) bpf_ntohs(*(u16 *)((u8*)(data) + (b)))
35#define load_word(data, b) bpf_ntohl(*(u32 *)((u8*)(data) + (b)))
36#define load_dword(data, b) bpf_be64_to_cpu(*(u64 *)((u8*)(data) + (b)))
37
38
39
40#define bpf_printk(fmt, ...) \
41 ({ \
42 char ____fmt[] = fmt; \
43 printf(____fmt, sizeof(____fmt), \
44 ##__VA_ARGS__); \
45 })
46
51#define SEC(NAME)
52
54struct sk_buff {
55 void *data;
56 u16 len;
57 u32 ifindex;
58};
59
61#define BPF_ANY 0
62#define BPF_NOEXIST 1
63#define BPF_EXIST 2
64#define BPF_F_LOCK 4
65
67enum bpf_map_type {
68 BPF_MAP_TYPE_HASH,
69 BPF_MAP_TYPE_ARRAY,
70};
71
72
73
74#define SK_BUFF struct sk_buff
75#define REGISTER_START() \
76struct bpf_table tables[] = {
77#define REGISTER_TABLE(NAME, TYPE, KEY_SIZE, VALUE_SIZE, MAX_ENTRIES) \
78 { MAP_PATH"/"#NAME, TYPE, KEY_SIZE, VALUE_SIZE, MAX_ENTRIES, NULL },
79#define REGISTER_END() \
80 { 0, 0, 0, 0, 0 } \
81};
82
83#define BPF_MAP_LOOKUP_ELEM(table, key) \
84 registry_lookup_table_elem(MAP_PATH"/"#table, key)
85#define BPF_MAP_UPDATE_ELEM(table, key, value, flags) \
86 registry_update_table(MAP_PATH"/"#table, key, value, flags)
87#define BPF_MAP_DELETE_ELEM(table, key) \
88 registry_delete_table_elem(MAP_PATH"/"#table, key)
89#define BPF_USER_MAP_UPDATE_ELEM(index, key, value, flags)\
90 registry_update_table_id(index, key, value, flags)
91#define BPF_OBJ_PIN(table, name) registry_add(table)
92#define BPF_OBJ_GET(name) registry_get_id(name)
93
94
96extern struct bpf_table tables[];
97extern int ebpf_filter(struct sk_buff *skb);
98
101static inline void print_n_bytes(void *receiveBuffer, int num) {
102 for (int i = 0; i < num; i++)
103 printf("%02x", ((unsigned char *)receiveBuffer)[i]);
104 printf("\n");
105}
106
107#endif // BACKENDS_EBPF_RUNTIME_EBPF_USER_H_
A helper structure used to describe attributes.
Definition ebpf_registry.h:27
simple descriptor which replaces the kernel sk_buff structure.
Definition ebpf_test.h:54