-
Notifications
You must be signed in to change notification settings - Fork 7
/
mpls_bpf_user.c
225 lines (193 loc) · 6.08 KB
/
mpls_bpf_user.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <argp.h>
#include <errno.h>
#include <linux/bpf.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/*********************************************************************************
* Copied only relevant needed libbpf helpers from mini library
* found: https://elixir.bootlin.com/linux/v4.4/source/samples/bpf/libbpf.h#L19
*********************************************************************************/
/*
* When building perf, unistd.h is override. Define NR_bpf is
* required to be defined.
*/
/*
* When building perf, unistd.h is overridden. __NR_bpf is
* required to be defined explicitly.
*/
#ifndef __NR_bpf
#if defined(__i386__)
#define __NR_bpf 357
#elif defined(__x86_64__)
#define __NR_bpf 321
#elif defined(__aarch64__)
#define __NR_bpf 280
#elif defined(__sparc__)
#define __NR_bpf 349
#elif defined(__s390__)
#define __NR_bpf 351
#else
#error __NR_bpf not defined. libbpf does not support your arch.
#endif
#endif
/* flags for BPF_MAP_UPDATE_ELEM command */
#define BPF_ANY 0 /* create new element or update existing */
#define BPF_NOEXIST 1 /* create new element only if it didn't exist */
#define BPF_EXIST 2 /* only update existing element */
static unsigned long ptr_to_u64(const void *ptr) { return (unsigned long)ptr; }
static inline long sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
unsigned int size) {
return syscall(__NR_bpf, cmd, attr, size);
}
long bpf_obj_get(const char *pathname);
long bpf_map_update_elem(unsigned int fd, void *key, void *value,
unsigned long long flags);
long bpf_map_lookup_elem(unsigned int fd, void *key, void *value);
long bpf_obj_get(const char *pathname) {
union bpf_attr attr;
bzero(&attr, sizeof(attr));
attr.pathname = ptr_to_u64((const void *)pathname);
return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
}
long bpf_map_update_elem(unsigned int fd, void *key, void *value,
unsigned long long flags) {
union bpf_attr attr;
bzero(&attr, sizeof(attr));
attr.map_fd = fd;
attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value);
attr.flags = flags;
return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
}
long bpf_map_lookup_elem(unsigned int fd, void *key, void *value) {
union bpf_attr attr;
bzero(&attr, sizeof(attr));
attr.map_fd = fd;
attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value);
return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
}
/*********************************************************************************/
/**
* When PIN_GLOBAL_NS is used, this is deafult global namespace that is loaded.
*/
static const char *TC_GLOBAL_NS = "/sys/fs/bpf/tc/globals";
/**
* The name of the BPF MAP variable in mpls_bpf_berk.c
*/
static const char *BPF_MAP_NAME = "DEBUGS_MAP";
const char *argp_program_version = "mpls_bpf_user 1.0";
const char *argp_program_bug_address = "<[email protected]>";
/* Program documentation. */
static char doc[] = "MPLSoIP User-- a program to interfact with the eBPF code.";
/* A description of the arguments we accept. */
static char args_doc[] = "[show] [disable|enable]";
/*
OPTIONS. Field 1 in ARGP.
Order of fields: {NAME, KEY, ARG, FLAGS, DOC}.
*/
static struct argp_option options[] = {
{0, 0, 0, 0, 0, 0},
};
/* This structure is used by main to communicate with parse_opt. */
struct arguments {
void (*cmd)(void);
};
void show(void);
void disable(void);
void enable(void);
long get_map_fd(void);
long get_map_fd(void) {
char pinned_file[256];
snprintf(pinned_file, sizeof(pinned_file), "%s/%s", TC_GLOBAL_NS,
BPF_MAP_NAME);
return bpf_obj_get(pinned_file);
}
void show(void) {
long fd = get_map_fd();
if (fd < 0) {
fprintf(stderr, "could not find map %s [%s]. Default is false.\n",
BPF_MAP_NAME, strerror(errno));
return;
}
bool value = false;
int index = 0;
long ret = bpf_map_lookup_elem((unsigned int)fd, &index, &value);
if (ret != 0) {
fprintf(stderr, "Could not lookup value [%s].\n", strerror(errno));
} else {
printf("debug flag: %s\n", value ? "true" : "false");
}
}
void disable(void) {
long fd = get_map_fd();
if (fd < 0) {
fprintf(stderr, "could not find map %s [%s]. Cannot disable.\n",
BPF_MAP_NAME, strerror(errno));
return;
}
int index = 0;
bool value = false;
long ret = bpf_map_update_elem((unsigned int)fd, &index, &value, BPF_ANY);
if (ret != 0) {
fprintf(stderr, "Could not update element [%ld] [%s].\n", ret,
strerror(errno));
} else {
printf("Successfully disabled.\n");
}
}
void enable(void) {
long fd = get_map_fd();
if (fd < 0) {
fprintf(stderr, "could not find map %s [%s]. Cannot enable.\n",
BPF_MAP_NAME, strerror(errno));
return;
}
bool value = true;
int index = 0;
long ret = bpf_map_update_elem((unsigned int)fd, &index, &value, BPF_ANY);
if (ret != 0) {
fprintf(stderr, "Could not update element [%ld] [%s].\n", ret,
strerror(errno));
} else {
printf("Successfully enabled.\n");
}
}
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
switch (key) {
case ARGP_KEY_NO_ARGS:
argp_usage(state);
break;
case ARGP_KEY_ARG:
if (strcmp(arg, "show") == 0) {
arguments->cmd = &show;
} else if (strcmp(arg, "disable") == 0) {
arguments->cmd = &disable;
} else if (strcmp(arg, "enable") == 0) {
arguments->cmd = &enable;
} else {
argp_error(state, "%s is not a valid command", arg);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Our argp parser. */
static struct argp argp = {options, parse_opt, args_doc, doc, 0, 0, 0};
int main(int argc, char **argv) {
struct arguments arguments;
arguments.cmd = NULL;
/* Where the magic happens */
argp_parse(&argp, argc, argv, 0, 0, &arguments);
if (arguments.cmd != NULL) {
void (*cmd)(void) = arguments.cmd;
(*cmd)();
}
}