-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.h
260 lines (233 loc) · 7.14 KB
/
common.h
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <stdint.h>
#include <linux/types.h>
#include <stdio.h>
#include "cjson/cJSON.h"
#include "bpf_util.h"
#include "bpf_load.h"
#include "data.h"
#include <linux/bpf.h>
#define NUM_MAP_PINS 9
#define MAX_PATH_FORMATTED 128
char bpf_pin_folder[MAX_PATH_FORMATTED];
#define KEY_NAME_MAX 32
#define NUM_VALUE_MAX 128
char key_name[KEY_NAME_MAX];
char num_value[NUM_VALUE_MAX];
char num_str[8];
int map_pin_fds[NUM_MAP_PINS];
struct map_pin_info {
char *name;
int (*format_value)(void *, char *, int);
int (*format_key)(void *, char *, int);
void (*dump)(int map_fd, cJSON *, int index);
char path_formatted[MAX_PATH_FORMATTED];
};
struct map_pin_info map_pins[NUM_MAP_PINS];
int format_long_hex(void *data, char *buf, int len) {
uint64_t *d = data;
return snprintf(buf, len, "0x%lx", (unsigned long)*d);
}
int format_int_hex(void *data, char *buf, int len) {
uint32_t *d = data;
return snprintf(buf, len, "0x%x", (unsigned int)*d);
}
int format_short_hex(void *data, char *buf, int len) {
uint16_t *d = data;
return snprintf(buf, len, "0x%04hx", (unsigned short)*d);
}
int format_host_info(void *data, char *buf, int len) {
struct host_info *hi = data;
return snprintf(buf, len, "host=0x%lx", (unsigned long)hi->host);
}
int format_flow_info(void *data, char *buf, int len) {
int c;
char *curr = buf;
struct flow_info *fi = data;
struct five_tuple *ft = &(fi->ft);
c = snprintf(
curr, len,
"saddr=0x%x,daddr=0x%x,",
ft->saddr, ft->daddr
);
curr = buf + c;
c += snprintf(
curr, len-c,
"sport=0x%x,dport=0x%x,",
ft->sport, ft->dport
);
curr = buf + c;
c += snprintf(
curr, len-c,
"proto=0x%x,",
ft->proto
);
curr = buf + c;
c += snprintf(
curr, len-c,
"flow_count=0x%x,packet_count=0x%x",
fi->flow_count, fi->packet_count
);
return c;
}
void dump_hash64(int map_fd, cJSON *map_data, int index) {
uint32_t key = -1, next_key;
unsigned int nr_cpus = bpf_num_possible_cpus();
unsigned int i;
uint64_t values[nr_cpus];
cJSON *cpu_objects[nr_cpus];
for (i=0; i<nr_cpus; i++)
cpu_objects[i] = cJSON_CreateObject();
while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
key = next_key;
if ((bpf_map_lookup_elem(map_fd, &key, &values)) != 0) {
fprintf(
stderr,
"ERR: failed to read key %x from map(%d): %s\n",
key, errno, strerror(errno)
);
}
for (i=0; i<nr_cpus; i++) {
if (values[i]) {
if ((map_pins[index].format_key)(&key, key_name, KEY_NAME_MAX) <= 0)
continue;
if ((map_pins[index].format_value)(
&values[i], num_value, NUM_VALUE_MAX
) <= 0)
continue;
if (cJSON_AddStringToObject(cpu_objects[i], key_name, num_value) == NULL)
fprintf(
stderr,
"ERR: Failed to add key(value) %x(%lu) to json\n",
key, values[i]
);
}
}
}
for (i=0; i<nr_cpus; i++) {
snprintf(num_str, 8, "%d", i);
cJSON_AddItemToObject(map_data, num_str, cpu_objects[i]);
}
}
void dump_hash_flowinfo(int map_fd, cJSON *map_data, int index) {
uint32_t key = -1, next_key;
unsigned int nr_cpus = bpf_num_possible_cpus();
unsigned int i;
struct flow_info values[nr_cpus];
cJSON *cpu_objects[nr_cpus];
memset(values, 0, sizeof(values));
for (i=0; i<nr_cpus; i++)
cpu_objects[i] = cJSON_CreateObject();
while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
key = next_key;
if ((bpf_map_lookup_elem(map_fd, &key, &values)) != 0) {
fprintf(
stderr,
"ERR: failed to read key %x from map(%d): %s\n",
key, errno, strerror(errno)
);
}
for (i=0; i<nr_cpus; i++) {
if (values[i].flow_count) {
if ((map_pins[index].format_key)(&key, key_name, KEY_NAME_MAX) <= 0) {
fprintf(stderr, "ERR: Failed to format key %d\n", i);
continue;
}
if ((map_pins[index].format_value)(
&values[i], num_value, NUM_VALUE_MAX
) <= 0) {
fprintf(stderr, "ERR: failed to format value for %d\n", i);
continue;
}
if (cJSON_AddStringToObject(cpu_objects[i], key_name, num_value) == NULL)
fprintf(
stderr,
"ERR: Failed to add key %x to json\n",
key
);
}
}
}
for (i=0; i<nr_cpus; i++) {
snprintf(num_str, 8, "%d", i);
cJSON_AddItemToObject(map_data, num_str, cpu_objects[i]);
}
}
struct map_pin_info map_pins[NUM_MAP_PINS] = {
{
.name = "bloomfilter",
.format_value = format_long_hex,
.format_key = format_int_hex,
.dump = dump_hash64,
},
{
.name = "flow_info",
.format_value = format_flow_info,
.format_key = format_int_hex,
.dump = dump_hash_flowinfo,
},
{
.name = "eth_proto_count",
.format_value = format_long_hex,
.format_key = format_short_hex,
.dump = dump_hash64,
},
{
.name = "ip_proto_count",
.format_value = format_long_hex,
.format_key = format_short_hex,
.dump = dump_hash64,
},
{
.name = "sport_count",
.format_value = format_long_hex,
.format_key = format_short_hex,
.dump = dump_hash64,
},
{
.name = "dport_count",
.format_value = format_long_hex,
.format_key = format_short_hex,
.dump = dump_hash64,
},
{
.name = "sip_count",
.format_value = format_long_hex,
.format_key = format_int_hex,
.dump = dump_hash64,
},
{
.name = "dip_count",
.format_value = format_long_hex,
.format_key = format_int_hex,
.dump = dump_hash64,
},
{
.name = "host_info",
.format_value = format_host_info,
.format_key = format_int_hex,
.dump = dump_hash64,
},
};
int format_map_paths(uint16_t host_num, uint8_t prefix_num) {
char prefix_str[MAX_PATH_FORMATTED];
for (int i=0; i<NUM_MAP_PINS; i++) {
if (snprintf(
prefix_str, MAX_PATH_FORMATTED,
"/sys/fs/bpf/%hhu/%u/", prefix_num, host_num
) <= 0) {
fprintf(stderr, "WARN: failed to format map path\n");
return -1;
}
if (snprintf(
map_pins[i].path_formatted, MAX_PATH_FORMATTED,
"%s%s", prefix_str, map_pins[i].name
) <= 0) {
fprintf(stderr, "WARN: falied to format map path\n");
return -1;
}
}
snprintf(
bpf_pin_folder, MAX_PATH_FORMATTED, "/sys/fs/bpf/%hhu/%u",
prefix_num, host_num);
return 0;
}