-
Notifications
You must be signed in to change notification settings - Fork 171
/
vxlan.c
384 lines (308 loc) · 8.58 KB
/
vxlan.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* vxlan.c
*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdbool.h>
#include "vr_os.h"
#include <sys/types.h>
#include <sys/socket.h>
#if defined(__linux__)
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <netinet/ether.h>
#elif defined(__FreeBSD__)
#include <net/if.h>
#include <net/ethernet.h>
#endif
#include "vr_types.h"
#include "vr_message.h"
#include "vr_vxlan.h"
#include "vr_genetlink.h"
#include "nl_util.h"
#include "ini_parser.h"
static struct nl_client *cl;
static bool dump_pending = false;
static bool response_pending = true;
unsigned int dump_marker= 0;
static int vxlan_vnid = 99999;
static int vxlan_nh = 99999;
static int create_set, delete_set, dump_set;
static int get_set, nh_set, vnid_set;
static int help_set, cmd_set;
static int vxlan_op = -1;
void
vr_vxlan_req_process(void *s_req)
{
vr_vxlan_req *req = (vr_vxlan_req *)s_req;
printf("%5d %7d\n", (req->vxlanr_vnid & 0xFFFF),
(req->vxlanr_nhid & 0xFFFF));
dump_marker = req->vxlanr_vnid;
if (vxlan_op != SANDESH_OP_DUMP)
response_pending = false;
return;
}
void
vr_response_process(void *s)
{
vr_response *resp = (vr_response *)s;
response_pending = false;
if (resp->resp_code < 0) {
printf("Error: %s\n", strerror(-resp->resp_code));
} else {
if (vxlan_op == SANDESH_OP_DUMP) {
if (resp->resp_code > 0)
response_pending = true;
if (resp->resp_code & VR_MESSAGE_DUMP_INCOMPLETE) {
dump_pending = true;
response_pending = true;
} else {
dump_pending = false;
}
}
}
}
static int
vr_vxlan_op(void)
{
vr_vxlan_req vxlan_req;
int ret, error, attr_len;
struct nl_response *resp;
struct nlmsghdr *nlh;
op_retry:
vxlan_req.h_op = vxlan_op;
vxlan_req.vxlanr_vnid = vxlan_vnid;
switch (vxlan_op) {
case SANDESH_OP_ADD:
vxlan_req.vxlanr_nhid = vxlan_nh;
break;
case SANDESH_OP_DUMP:
vxlan_req.vxlanr_vnid = dump_marker;
break;
default:
break;
}
/* nlmsg header */
ret = nl_build_nlh(cl, cl->cl_genl_family_id, NLM_F_REQUEST);
if (ret) {
return ret;
}
/* Generic nlmsg header */
ret = nl_build_genlh(cl, SANDESH_REQUEST, 0);
if (ret) {
return ret;
}
attr_len = nl_get_attr_hdr_size();
error = 0;
ret = sandesh_encode(&vxlan_req, "vr_vxlan_req", vr_find_sandesh_info,
(nl_get_buf_ptr(cl) + attr_len),
(nl_get_buf_len(cl) - attr_len), &error);
if ((ret <= 0) || error) {
return ret;
}
/* Add sandesh attribute */
nl_build_attr(cl, ret, NL_ATTR_VR_MESSAGE_PROTOCOL);
nl_update_nlh(cl);
response_pending = true;
/* Send the request to kernel */
ret = nl_sendmsg(cl);
while (response_pending) {
if ((ret = nl_recvmsg(cl)) > 0) {
resp = nl_parse_reply(cl);
if (resp->nl_op == SANDESH_REQUEST) {
sandesh_decode(resp->nl_data, resp->nl_len,
vr_find_sandesh_info, &ret);
} else if (resp->nl_type == NL_MSG_TYPE_DONE) {
response_pending = false;
}
}
nlh = (struct nlmsghdr *)cl->cl_buf;
if (!nlh->nlmsg_flags)
break;
}
if (dump_pending)
goto op_retry;
return 0;
}
enum opt_vxlan_index {
COMMAND_OPT_INDEX,
CREATE_OPT_INDEX,
DELETE_OPT_INDEX,
DUMP_OPT_INDEX,
GET_OPT_INDEX,
HELP_OPT_INDEX,
NEXTHOP_OPT_INDEX,
MAX_OPT_INDEX
};
static struct option long_options[] = {
[COMMAND_OPT_INDEX] = {"cmd", no_argument, &cmd_set, 1},
[CREATE_OPT_INDEX] = {"create", required_argument, &create_set, 1},
[DELETE_OPT_INDEX] = {"delete", required_argument, &delete_set, 1},
[DUMP_OPT_INDEX] = {"dump", no_argument, &dump_set, 1},
[GET_OPT_INDEX] = {"get", required_argument, &get_set, 1},
[HELP_OPT_INDEX] = {"help", no_argument, &help_set, 1},
[NEXTHOP_OPT_INDEX] = {"nh", required_argument, &nh_set, 1},
[MAX_OPT_INDEX] = { NULL, 0, 0, 0},
};
static void
usage_internal()
{
printf("Usage: vxlan --create <vnid> --nh <nh index>\n");
printf(" vxlan --delete <vnid>\n");
printf("\n");
printf("--create Create an entry for <vnid> in the vxlan table\n");
printf(" with nexthop set to <nh index>\n");
printf("--delete Delete the entry corresponding to <vnid>\n");
exit(1);
}
static void
Usage(void)
{
printf("Usage: vxlan --dump\n");
printf(" vxlan --get <vnid>\n");
printf(" vxlan --help\n");
printf("\n");
printf("--dump Dumps the vxlan table\n");
printf("--get Dumps the entry corresponding to <vnid>\n");
printf("--help Prints this help message\n");
exit(1);
}
static void
parse_long_opts(int opt_index, char *opt_arg)
{
errno = 0;
switch (opt_index) {
case COMMAND_OPT_INDEX:
usage_internal();
break;
case CREATE_OPT_INDEX:
vxlan_op = SANDESH_OP_ADD;
vxlan_vnid = strtoul(opt_arg, NULL, 0);
if (errno)
usage_internal();
break;
case DELETE_OPT_INDEX:
vxlan_op = SANDESH_OP_DELETE;
vxlan_vnid = strtoul(opt_arg, NULL, 0);
if (errno)
usage_internal();
break;
case DUMP_OPT_INDEX:
vxlan_op = SANDESH_OP_DUMP;
break;
case GET_OPT_INDEX:
vxlan_op = SANDESH_OP_GET;
vxlan_vnid = strtoul(opt_arg, NULL, 0);
if (errno)
Usage();
break;
case HELP_OPT_INDEX:
Usage();
break;
case NEXTHOP_OPT_INDEX:
vxlan_nh = strtoul(opt_arg, NULL, 0);
if (errno)
usage_internal();
break;
default:
Usage();
break;
}
return;
}
static void
validate_options(void)
{
int sum_op = create_set + delete_set + dump_set + get_set;
if (sum_op > 1 || vxlan_op < 0)
Usage();
if (create_set)
if (!nh_set)
usage_internal();
if (vnid_set)
if (!create_set || !delete_set || !get_set)
usage_internal();
return;
}
int main(int argc, char *argv[])
{
int ret, opt, option_index;
while ((opt = getopt_long(argc, argv, "bcdgn:l:",
long_options, &option_index)) >= 0) {
switch (opt) {
case 'c':
if (vxlan_op >= 0)
Usage();
vxlan_op = SANDESH_OP_ADD;
create_set = 1;
break;
case 'd':
if (vxlan_op >= 0)
Usage();
vxlan_op = SANDESH_OP_DELETE;
delete_set = 1;
break;
case 'g':
if (vxlan_op >= 0)
Usage();
vxlan_op = SANDESH_OP_GET;
get_set = 1;
break;
case 'b':
if (vxlan_op >= 0)
Usage();
vxlan_op = SANDESH_OP_DUMP;
dump_set = 1;
break;
case 'n':
vxlan_nh = atoi(optarg);
nh_set = 1;
break;
case 'l':
vxlan_vnid = strtoul(optarg, NULL, 10);
vnid_set = 1;
break;
case 0:
parse_long_opts(option_index, optarg);
break;
case '?':
default:
Usage();
}
}
validate_options();
if ((vxlan_op == SANDESH_OP_DUMP) ||
(vxlan_op == SANDESH_OP_GET)) {
printf("VXLAN Table\n\n");
printf(" VNID NextHop\n");
printf("----------------\n");
}
cl = nl_register_client();
if (!cl) {
exit(1);
}
parse_ini_file();
ret = nl_socket(cl, get_domain(), get_type(), get_protocol());
if (ret <= 0) {
exit(1);
}
ret = nl_connect(cl, get_ip(), get_port());
if (ret < 0) {
exit(1);
}
if (vrouter_get_family_id(cl) <= 0) {
return -1;
}
vr_vxlan_op();
return 0;
}