-
Notifications
You must be signed in to change notification settings - Fork 15
/
ovsdb_mon.c
698 lines (646 loc) · 16.9 KB
/
ovsdb_mon.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
* BSD LICENSE
*
* Copyright(c) 2016-2017 Netronome.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Netronome nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <rte_version.h>
#if RTE_VERSION_NUM(17, 5, 0, 0) <= RTE_VERSION
#include <rte_vhost.h>
#else
#include <rte_virtio_net.h>
#endif
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdlib.h>
#include "ovsdb_mon.h"
#define __MODULE__ "ovsdb_mon"
#include "log.h"
#include "rte_ethdev.h"
#include "virtio_worker.h"
#include "sriov.h"
#define MAX_READ 65535
/*
* Monitor ovsdb for ports added with virtio_forwarder value in external_ids column,
* e.g.
* ovs-vsctl add-port br-virtio eth1 \
* -- set interface eth1 external_ids:virtio_forwarder=1
*/
struct port_config {
int virtio_enabled;
long int virtio_id;
};
static struct ovsdb_mon_conf ovsdb_conf;
static const char tok_initial[] = "initial";
static const char tok_insert[] = "insert";
static const char tok_delete[] = "delete";
static const char tok_old[] = "old";
static const char tok_new[] = "new";
static const char tok_relay_id[] = "virtio_relay";
static const char tok_forwarder[] = "virtio_forwarder";
static pthread_t ovsdb_mon_thread;
static int running;
static int must_stop;
static int extract_field(const char* buf, int start, char* field)
{
char* end;
size_t dstptr;
size_t srcptr;
buf = buf + start;
if (buf[0] == '"') {
/* Quoted string. */
srcptr = 1;
dstptr = 0;
while (srcptr < strlen(buf)) {
if (buf[srcptr] == '"') {
if (buf[srcptr + 1] == '"') {
field[dstptr++] = buf[srcptr++];
srcptr++;
} else if (buf[srcptr + 1] == '\0' ||
buf[srcptr + 1] == ',') {
srcptr++;
break;
} else {
log_error("Warning, truncating badly quoted string\n");
while(srcptr < strlen(buf) &&
buf[srcptr] != '\0' &&
buf[srcptr] != ',') {
srcptr++;
}
return start + srcptr + 1;
}
} else {
field[dstptr++] = buf[srcptr++];
}
}
field[dstptr] = '\0';
return start + srcptr + 1;
} else {
/* Unquoted string. */
end = strchr(buf, ',');
if (!end)
end = strchr(buf, '\0');
memcpy(field, buf, end - buf);
field[end - buf] = '\0';
return start + (end - buf) + 1;
}
}
static void unquote(char* str)
{
size_t dstptr;
size_t srcptr;
char tmp[MAX_READ];
if (str[0] == '"') {
strcpy(tmp, str);
srcptr = 1;
dstptr = 0;
while (srcptr < strlen(tmp)) {
if (tmp[srcptr] == '"') {
if (tmp[srcptr + 1] == '\0') {
/*String done!*/
break;
} else {
log_error("Warning, truncating badly quoted string\n");
break;
}
} else if (tmp[srcptr] == '\\') {
if (tmp[srcptr + 1] == '\0') {
log_error("Warning, badly quoted string\n");
break;
} else {
/*Escaped character*/
srcptr++;
}
str[dstptr++] = tmp[srcptr++];
} else {
str[dstptr++] = tmp[srcptr++];
}
}
str[dstptr] = '\0';
}
}
enum map_parse_state {
STATE_START,
STATE_KEY,
STATE_VAL,
STATE_EQ,
STATE_SEP,
STATE_DONE
};
static void parse_map(const char* map, struct port_config* port_config)
{
enum map_parse_state parser_state;
char key[MAX_READ];
char val[MAX_READ];
int srcptr;
int dstptr;
char *endptr;
parser_state = STATE_START;
port_config->virtio_enabled = 0;
port_config->virtio_id = 0;
srcptr = 0;
while(parser_state != STATE_DONE) {
/* Sink whitespace. */
while (isspace(map[srcptr]))
srcptr++;
switch(parser_state) {
case STATE_START:
if (map[srcptr] != '{') {
log_error("Warning, expected '{'");
srcptr++;
while (map[srcptr] != '{' && map[srcptr] != '\0')
srcptr++;
}
if (map[srcptr] == '{') {
srcptr++;
parser_state = STATE_KEY;
} else {
parser_state = STATE_DONE;
}
break;
case STATE_KEY:
if (map[srcptr] == '}') {
parser_state = STATE_DONE;
break;
} else if (map[srcptr] == '"') {
/* Quoted string. */
srcptr++;
dstptr = 0;
key[dstptr] = '\0';
while (parser_state == STATE_KEY) {
if (map[srcptr] == '\0') {
log_error("Warning, badly quoted string.");
parser_state = STATE_DONE;
break;
}
if (map[srcptr] == '"') {
/* String done! */
srcptr++;
key[dstptr] = '\0';
parser_state = STATE_EQ;
break;
} else if (map[srcptr] == '\\') {
/* Escaped character. */
srcptr++;
}
key[dstptr++] = map[srcptr++];
}
} else {
dstptr = 0;
while (!isspace(map[srcptr]) &&
map[srcptr] != '\0' &&
map[srcptr] != '=') {
key[dstptr++] = map[srcptr++];
}
key[dstptr] = '\0';
parser_state = STATE_EQ;
}
break;
case STATE_VAL:
if (map[srcptr] == '"') {
/*quoted string*/
srcptr++;
dstptr = 0;
val[dstptr] = '\0';
while (parser_state == STATE_VAL) {
if (map[srcptr] == '\0') {
log_error("Warning, badly quoted string.");
parser_state = STATE_DONE;
break;
}
if (map[srcptr] == '"') {
/*String done!*/
srcptr++;
val[dstptr] = '\0';
parser_state = STATE_SEP;
break;
} else if (map[srcptr] == '\\') {
/*Escaped character*/
srcptr++;
}
val[dstptr++] = map[srcptr++];
}
} else {
dstptr = 0;
while (!isspace(map[srcptr]) &&
map[srcptr] != '\0' &&
map[srcptr] != ',' &&
map[srcptr] != '}') {
val[dstptr++] = map[srcptr++];
}
val[dstptr] = '\0';
parser_state = STATE_SEP;
}
if (strcmp(tok_forwarder, key) == 0 ||
strcmp(tok_relay_id, key) == 0) {
port_config->virtio_enabled = 1;
errno = 0;
port_config->virtio_id = strtol(val, &endptr, 0);
if ((errno == ERANGE &&
(port_config->virtio_id ==
LONG_MAX ||
port_config->virtio_id ==
LONG_MIN)) ||
(errno != 0 &&
port_config->virtio_id == 0)) {
log_error("Error getting relay id: %m");
port_config->virtio_enabled = 0;
}
if (endptr == val) {
log_error("Empty virtio_id\n");
port_config->virtio_enabled = 0;
}
if (*endptr != '\0') /* Not necessarily an error... */
log_warning("Warning, trailing characters on virtio_id\n");
}
break;
case STATE_EQ:
if (map[srcptr] != '=') {
log_error("Warning, expected '='");
srcptr++;
while (map[srcptr] != '=' && map[srcptr] != '\0') {
srcptr++;
}
}
if (map[srcptr] == '=') {
srcptr++;
parser_state = STATE_VAL;
} else {
parser_state = STATE_DONE;
}
break;
case STATE_SEP:
if (map[srcptr] != ',' && map[srcptr] != '}') {
log_error("Warning, expected ',' or '}'");
srcptr++;
while (map[srcptr] != ',' &&
map[srcptr] != '\0' &&
map[srcptr] != '}' ) {
srcptr++;
}
}
if (map[srcptr] == ',') {
srcptr++;
parser_state = STATE_KEY;
} else {
parser_state = STATE_DONE;
}
break;
case STATE_DONE:
break;
}
}
}
static int is_ovspidfile(const struct dirent *d)
{
if (strncmp(d->d_name, "vovsdbc", 7) == 0)
return (strcmp(d->d_name + strlen(d->d_name) - 4, ".pid") ==
0 ? 1 : 0);
return 0;
}
static void clean_old(void)
{
#define BUF_SIZE (256 + 64)
struct dirent **namelist;
int n;
n = scandir("/tmp", &namelist, is_ovspidfile, alphasort);
if (n > 0) {
while (n--) {
char buf[BUF_SIZE];
log_debug("Found old pidfile: '%s'",
namelist[n]->d_name);
snprintf(buf, BUF_SIZE, "/usr/bin/pkill -F /tmp/%s ovsdb-client &>/dev/null",
namelist[n]->d_name);
if (system(buf) == -1)
log_debug("Error killing stale ovsdb-client");
unlink(namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}
static int check_ovsdb_sock(void)
{
struct stat statbuf;
if (stat(ovsdb_conf.ovsdb_sock_path, &statbuf) == 0) {
if ((statbuf.st_mode&(S_IFSOCK | S_IRWXU)) !=
(S_IFSOCK | S_IRWXU)) {
log_error("'%s' is not a user read/write/executable socket path!",
ovsdb_conf.ovsdb_sock_path);
return 1;
}
} else {
log_error("Could not open ovsdb socket path '%s'!",
ovsdb_conf.ovsdb_sock_path);
return 1;
}
return 0;
}
static FILE*
setup_ovsdb_mon(const char *cmd, const char *pidfile, pid_t *ovsdbclient_pid,
int *cmd_sock)
{
FILE *cmd_stdout;
char pid[16];
int bytes;
int readpid;
cmd_stdout = popen(cmd, "r");
if (cmd_stdout == NULL) {
log_error("Error connecting to ovsdb!");
return NULL;
}
sleep(1);
readpid = open(pidfile, O_RDONLY);
if (readpid < 0) {
log_error("Error opening ovsdb-client PID file '%s'!", pidfile);
pclose(cmd_stdout);
return NULL;
}
if ((bytes=read(readpid, pid, 16))<=0) {
close(readpid);
log_error("Error reading ovsdb-client PID file!");
pclose(cmd_stdout);
return NULL;
}
close(readpid);
*ovsdbclient_pid=(pid_t)strtol(pid, 0, 10);
*cmd_sock = fileno(cmd_stdout);
log_debug("ovsdb-client subprocess running with PID %u",
(unsigned)*ovsdbclient_pid);
return cmd_stdout;
}
static void
process_external_ids(char *action, char *name, struct port_config *port_config,
int *virtio_id_change)
{
struct sriov_info vfinfo;
if (port_config->virtio_enabled && (strcmp(action, tok_insert) == 0 ||
strcmp(action, tok_initial) == 0)) {
if (get_VF_PCIE_from_netdev(name, &vfinfo) == 0) {
#ifndef VIRTIO_ECHO
log_info("Adding netdev '%s' (%s) to virtio %u",
name, vfinfo.dbdf, vfinfo.vf);
if (virtio_forwarder_add_vf2(vfinfo.dbdf, vfinfo.vf, 1) != 0)
log_warning("Error adding netdev '%s' (%s) to virtio %u!",
name, vfinfo.dbdf, vfinfo.vf);
#endif
} else {
log_warning("Failed to retrieve VF PCI info for netdev '%s'!",
name);
}
} else if (port_config->virtio_enabled &&
strcmp(action, tok_delete) == 0) {
struct sriov_info vfinfo;
if (get_VF_PCIE_from_netdev(name, &vfinfo) == 0) {
#ifndef VIRTIO_ECHO
log_info("Removing netdev '%s' (%s) from virtio %u",
name, vfinfo.dbdf, vfinfo.vf);
if (virtio_forwarder_remove_vf2(vfinfo.dbdf, vfinfo.vf, 1) != 0)
log_warning("Error removing netdev '%s' (%s) from virtio %u",
name, vfinfo.dbdf, vfinfo.vf);
#endif
} else {
log_warning("Failed to retrieve VF PCI info for netdev '%s'!",
name);
}
} else if (strcmp(action, tok_old) == 0) {
if (port_config->virtio_enabled)
*virtio_id_change = port_config->virtio_id;
else
*virtio_id_change = -1;
} else if (strcmp(action, tok_new) == 0) {
#ifndef VIRTIO_ECHO
struct sriov_info vfinfo;
if (get_VF_PCIE_from_netdev(name, &vfinfo) == 0) {
if (*virtio_id_change != -1) {
log_info("Removing netdev '%s' (%s) from virtio %u",
name, vfinfo.dbdf, vfinfo.vf);
if (virtio_forwarder_remove_vf2(vfinfo.dbdf, vfinfo.vf, 1) != 0)
log_warning("Error removing netdev '%s' (%s) from virtio %u",
name, vfinfo.dbdf, vfinfo.vf);
*virtio_id_change = -1;
}
if (port_config->virtio_enabled) {
log_info("Adding netdev '%s' (%s) to virtio %u",
name, vfinfo.dbdf, vfinfo.vf);
if (virtio_forwarder_add_vf2(vfinfo.dbdf, vfinfo.vf, 1) != 0)
log_info("Error adding netdev '%s' (%s) to virtio %u",
name, vfinfo.dbdf, vfinfo.vf);
}
} else {
log_warning("Failed to retrieve VF PCI info for netdev '%s'!",
name);
}
#endif
} else {
log_debug("Ignoring token '%s'", action);
}
}
static void parse_ovsdb_client(int cmd_sock)
{
int remain;
int len;
ssize_t count;
char buf[MAX_READ];
char tmp[MAX_READ];
char action[MAX_READ];
char name[MAX_READ];
char *nlpos;
size_t pos;
struct port_config port_config;
int virtio_id_change = -1;
remain = 0;
count = read(cmd_sock, buf + remain, MAX_READ - remain - 1);
while (remain + count > 0) {
len = remain + count;
buf[len] = '\0';
remain = len;
nlpos = strchr(buf, '\n');
while (nlpos) {
/* Parse a single line in the form:
* hash_nr,action,"""name""","{virtio_forwarder=""1""}" */
nlpos[0] = '\0';
if (strlen(buf)) {
pos = 0;
/* Parse hash */
pos = extract_field(buf, pos, tmp);
if (pos >= strlen(buf)) {
log_error("Warning, truncated row.");
}
/* Parse action */
pos = extract_field(buf, pos, action);
if (pos >= strlen(buf)) {
log_error("Warning, truncated row.");
}
/* Parse name */
pos = extract_field(buf, pos, name);
if (pos >= strlen(buf)) {
log_error("Warning, truncated row.");
}
unquote(name);
/* Parse external_ids */
pos = extract_field(buf, pos, tmp);
parse_map(tmp, &port_config);
process_external_ids(action, name, &port_config,
&virtio_id_change);
}
/* Copy the data of the next line to the
* beginning of buf. */
len = strlen(nlpos + 1);
memmove(buf, nlpos + 1, len + 1);
remain = len;
nlpos = strchr(buf, '\n');
}
if (remain == (MAX_READ - 1)) {
log_error("Warning, ignoring long string!");
count = read(cmd_sock, buf, 1);
while (count > 0) {
if (buf[0] == '\n') {
remain = 0;
break;
}
count = read(cmd_sock, buf, 1);
}
if (count < 1) {
break;
}
}
count = read(cmd_sock, buf + remain, MAX_READ - remain - 1);
if (count < 1)
break;
}
}
static void* ovsdb_mon_threadmain(void *arg __attribute__((unused)))
{
char cmd[256];
FILE* cmd_stdout = NULL;
int cmd_sock = -1;
fd_set readfds;
struct timeval sel_timeout;
static pid_t ovsdbclient_pid;
char pidfile[32];
bool ovsdb_conn_down = true;
log_debug("Starting ovsdb_mon thread");
snprintf(pidfile, 32, "/tmp/vovsdbc%u.pid", (unsigned)getpid());
snprintf(cmd, 256, "ovsdb-client --pidfile=%s -f csv --no-headings "
"monitor Interface name,external_ids 2>/dev/null",
pidfile);
must_stop = 0;
running = 1;
while (running && !must_stop) {
int ret;
/* Poll ovsdb. */
while (ovsdb_conn_down) {
if (check_ovsdb_sock() == 0) {
clean_old();
if ((cmd_stdout = setup_ovsdb_mon(cmd, pidfile,
&ovsdbclient_pid, &cmd_sock))
!= NULL) {
ovsdb_conn_down = false;
break;
}
}
sleep(5);
}
if (check_ovsdb_sock()) {
ovsdb_conn_down = true;
pclose(cmd_stdout);
continue;
}
/*
* Connection is up.
*/
FD_ZERO(&readfds);
FD_SET(cmd_sock, &readfds);
sel_timeout.tv_sec = 0;
sel_timeout.tv_usec = 100000; /* 100ms */
ret = select(cmd_sock+1, &readfds, 0, 0, &sel_timeout);
if (ret == 0)
continue;
if (ret < 0) {
log_error("Error reading ovsdb: %m");
ovsdb_conn_down = true;
pclose(cmd_stdout);
continue;
}
if (!FD_ISSET(cmd_sock, &readfds)) {
log_warning("Unexpectedly returned from select with nothing to read!");
continue;
}
parse_ovsdb_client(cmd_sock);
}
log_debug("stopping ovsdb-client subprocess (PID %u)",
(unsigned)ovsdbclient_pid);
kill(ovsdbclient_pid, SIGTERM);
usleep(100000);
kill(ovsdbclient_pid, SIGKILL);
unlink(pidfile);
if (!ovsdb_conn_down)
pclose(cmd_stdout);
running = 0;
log_debug("ovsdb_mon thread ending");
return 0;
}
int ovsdb_mon_start(const struct ovsdb_mon_conf *conf)
{
log_debug("Creating ovsdb_mon thread");
ovsdb_conf = *conf;
pthread_create(&ovsdb_mon_thread, 0, ovsdb_mon_threadmain, 0);
return 0;
}
void ovsdb_mon_stop(void)
{
struct timespec ts;
struct timeval tv;
must_stop = 1;
gettimeofday(&tv, 0);
tv.tv_sec += 2;
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec*1000;
log_debug("Stopping ovsdb_mon thread");
if (pthread_timedjoin_np(ovsdb_mon_thread, 0, &ts) != 0) {
log_debug("Timeout waiting for ovsdb_mon_thread, cancelling thread...");
pthread_cancel(ovsdb_mon_thread);
}
pthread_join(ovsdb_mon_thread, 0);
log_debug("Stopped ovsdb_mon thread");
}