This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
test_ebpf_map.c
74 lines (65 loc) · 1.53 KB
/
test_ebpf_map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Tool for accessing eBPF maps generated by P4
* $gcc test_bpf_map.c libbpf.o -o test_bpf_map
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <linux/bpf.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdint.h>
#include "libbpf.h"
#define MAP_PATH "/sys/fs/bpf/tc/globals/match_action"
typedef uint32_t u32;
// FIXME: This is copied from ovs.c
struct match_action_key {
u32 field0;
};
enum match_action_actions {
Reject,
NoAction_1,
};
struct match_action_value {
enum match_action_actions action;
union {
struct {
u32 addr;
} Reject;
struct {
} NoAction_1;
} u;
};
int main(void)
{
int ret;
int fd;
struct match_action_key key;
struct match_action_value value;
struct in_addr inp;
memset(&value, 0, sizeof(value));
memset(&key, 0, sizeof(key));
value.action = NoAction_1;
ret = inet_aton("192.168.218.1", &inp);
if (ret != 1)
return 1;
key.field0 = (u32)htonl(inp.s_addr);
printf("=== Open BPF map: %s ===\n", MAP_PATH);
fd = bpf_obj_get(MAP_PATH);
if (fd < 0) {
printf("BPF match_action map not loaded\n");
return 1;
}
printf("=== Write to eBPF map ===\n");
printf("key = %x value = %x\n", key.field0, value.action);
ret = bpf_update_elem(fd, &key, &value, BPF_ANY);
if (ret) {
perror("error updating map element\n");
return 1;
}
return 0;
}