forked from intel/openlldp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vdptool.c
1353 lines (1198 loc) · 31.9 KB
/
vdptool.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
LLDP Agent Daemon (LLDPAD) Software
Copyright(c) IBM Corp. 2014
Substantially modified from:
hostapd-0.5.7
Copyright (c) 2002-2007, Jouni Malinen <[email protected]> and
contributors
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
The full GNU General Public License is included in this distribution in
the file called "COPYING".
Contact Information:
open-lldp Mailing List <[email protected]>
*******************************************************************************/
/*
* Thomas Richter, IBM LTC Boeblingen, Germany, Feb 2014
*
* Command line interface tool to connect to vdp module of lldpad to
* set and query VSI profile settings.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <sys/queue.h>
#include "version.h"
#include "clif.h"
#include "clif_msgs.h"
#include "lldp_mod.h"
#include "qbg22.h"
#include "qbg_vdp22_clif.h"
#include "lldp_util.h"
#include "qbg_vdp22def.h"
#include "qbg_vdp22_oui.h"
#define OUI_ENCODE_HNDLR(name) name##_oui_encode_hndlr
#define OUI_PRNT_DECODE_HNDLR(name) name##_oui_print_decode_hndlr
struct lldp_head lldp_mod_head;
#define EXTERN_OUI_FN(name) \
extern bool name##_oui_encode_hndlr(char *, char *, size_t); \
extern void name##_oui_print_decode_hndlr(char *)
/* The handler declaration for encoding OUI specific information should be
* here. The corresponding decoder handler should be in lldpad.
*/
EXTERN_OUI_FN(cisco);
/* The OUI specific handlers should be added here */
vdptool_oui_hndlr_tbl_t oui_hndlr_tbl[] = {
{"cisco", OUI_ENCODE_HNDLR(cisco), OUI_PRNT_DECODE_HNDLR(cisco)}
};
static char *print_vdp_status(enum vdp22_cmd_status status)
{
char *str;
switch (status) {
case cmd_vdp_prot_no_support:
str = "VDP protocol not supported on interface";
break;
case cmd_vdp_nomem:
str = "Not enough memory";
break;
case cmd_vdp_busy:
str = "VSI association in progress";
break;
default:
str = "Unknown status";
break;
}
return str;
}
static char *print_status(cmd_status status)
{
char *str;
switch (status) {
case cmd_success:
str = "Successful";
break;
case cmd_failed:
str = "Failed";
break;
case cmd_device_not_found:
str = "Device not found or inactive";
break;
case cmd_agent_not_found:
str = "Agent instance for device not found";
break;
case cmd_invalid:
str = "Invalid command";
break;
case cmd_bad_params:
str = "Invalid parameters";
break;
case cmd_peer_not_present:
str = "Peer feature not present";
break;
case cmd_ctrl_vers_not_compatible:
str = "Version not compatible";
break;
case cmd_not_capable:
str = "Device not capable";
break;
case cmd_not_applicable:
str = "Command not applicable";
break;
case cmd_no_access:
str = "Access denied";
break;
case cmd_agent_not_supported:
str = "TLV does not support agent type";
break;
default:
str = print_vdp_status((enum vdp22_cmd_status)status);
break;
}
return str;
}
static void get_arg_value(char *str, char **arg, char **argval)
{
unsigned int i;
for (i = 0; i < strlen(str); i++)
if (!isprint(str[i]))
return;
for (i = 0; i < strlen(str); i++)
if (str[i] == '=')
break;
if (i < strlen(str)) {
str[i] = '\0';
*argval = &str[i+1];
}
*arg = str;
}
static char *get_oui_name(char *argvals)
{
char *oui_loc;
oui_loc = strchr(argvals, ',');
if (oui_loc == NULL)
return NULL;
*oui_loc = '\0';
return oui_loc + 1;
}
static void fill_oui_hdr(vdptool_oui_data_t *oui_data, char *oui_name)
{
STRNCPY_TERMINATED(oui_data->oui_name, oui_name, sizeof(oui_data->oui_name));
snprintf(oui_data->data, sizeof(oui_data->data), "%02x%s",
(unsigned int)strlen(oui_data->oui_name), oui_data->oui_name);
}
static bool run_vdptool_oui_hndlr(vdptool_oui_data_t *oui_data, char *argvals)
{
int cnt = 0, tbl_size;
char *dst;
size_t len = 0;
tbl_size = sizeof(oui_hndlr_tbl) / sizeof(vdptool_oui_hndlr_tbl_t);
for (cnt = 0; cnt < tbl_size; cnt++) {
if (!strncmp(oui_hndlr_tbl[cnt].oui_name, oui_data->oui_name,
VDP22_OUI_MAX_NAME)) {
len = strlen(oui_data->data);
if (len >= sizeof(oui_data->data))
return false;
dst = oui_data->data + len;
return oui_hndlr_tbl[cnt].oui_cli_encode_hndlr(dst,
argvals, len);
}
}
return false;
}
/*
* The OUI can be input in many ways.
* It could be vdptool .... -c oui=companyA,Data1 -c oui=companyB,Data2 \
* -c oui=companyA,Data3
* Or
* vdptool .... -c oui=companyA,Data1Data3 -c oui=companyB,data2
* This function takes care of both the case cases
*
* Anything after the comma in OUI data field is Org specific and it's upto
* the respective organization specific handlers to encode it so that it could
* be decoded appropriately by the ORG specific handlers inside lldpad.
* That is, Data1 and Data3 is ORG companyA specific and the OUI handlers
* of ORG companyA in vdptool and lldpad is responsible for encoding/decoding.
*
* Irrespective of how the command line interface to vdptool is, the input to
* lldpad is always the same. i.e. KeywordlenKeywordDatalenData
* For OUI, the data field will have the complete OUI data starting with
* ORG name (e.g companyA). So, in order to call the right handler routine,
* the OUI data field is split as OUInamelenOUInameOUIData.
* OUInamelen is 2B.
*/
static bool rewrite_oui_argval(char *argvals, vdptool_oui_data_t **oui_data,
int total_oui)
{
char *new_oui_argvals, *new_oui_name, *exist_oui_name;
bool flag = true, ret = false;
int cnt;
new_oui_argvals = get_oui_name(argvals);
if (!new_oui_argvals) {
printf("Incorrect OUI Value, missing comma as delimited for "
"OUI Type\n");
return false;
}
new_oui_name = argvals;
for (cnt = 0; cnt < total_oui; cnt++) {
if (!oui_data[cnt])
continue;
exist_oui_name = oui_data[cnt]->oui_name;
if (!strncmp(new_oui_name, exist_oui_name,
VDP22_OUI_MAX_NAME)) {
flag = false;
break;
}
}
if (flag) {
oui_data[total_oui] = calloc(1, sizeof(vdptool_oui_data_t));
fill_oui_hdr(oui_data[total_oui], new_oui_name);
ret = run_vdptool_oui_hndlr(oui_data[total_oui],
new_oui_argvals);
} else
ret = run_vdptool_oui_hndlr(oui_data[cnt], new_oui_argvals);
if (!ret)
return false;
return flag;
}
int render_cmd(struct cmd *cmd, int argc, char **args, char **argvals)
{
int len;
int i;
int fid = 0, oui = 0;
vdptool_oui_data_t **oui_data;
bool is_new;
len = sizeof(cmd->obuf);
/* To avoid another loop to figure the number of OUI's */
oui_data = calloc(argc, sizeof(vdptool_oui_data_t *));
if (!oui_data) {
printf("Not enough memory\n");
return 0;
}
if ((cmd->cmd == cmd_settlv) || (cmd->cmd == cmd_gettlv)) {
for (i = 0; i < argc; i++) {
if (!args[i])
continue;
if (!strncasecmp(args[i], "filter", strlen("filter")))
fid++;
else if (!strncasecmp(args[i], "oui", strlen("oui"))) {
is_new = rewrite_oui_argval(argvals[i],
oui_data,
oui);
if (is_new) {
argvals[i] = oui_data[oui]->data;
oui++;
} else {
args[i] = NULL;
argvals[i] = NULL;
}
}
}
}
cmd->ops |= (fid << OP_FID_POS) | (oui << OP_OUI_POS);
/* all command messages begin this way */
snprintf(cmd->obuf, len, "%c%08x%c%1x%02x%08x%02x%s%02x%08x",
MOD_CMD, cmd->module_id, CMD_REQUEST, CLIF_MSG_VERSION,
cmd->cmd, cmd->ops, (unsigned int) strlen(cmd->ifname),
cmd->ifname, cmd->type, cmd->tlvid);
/* Add any args and argvals to the command message */
for (i = 0; i < argc; i++) {
if (args[i])
snprintf(cmd->obuf + strlen(cmd->obuf),
len - strlen(cmd->obuf),
"%02x%s", (unsigned int)strlen(args[i]),
args[i]);
if (argvals[i])
snprintf(cmd->obuf + strlen(cmd->obuf),
len - strlen(cmd->obuf), "%04x%s",
(unsigned int)strlen(argvals[i]), argvals[i]);
}
for (i = 0; i < oui; i++) {
if (oui_data[i])
free(oui_data[i]);
}
free(oui_data);
return strlen(cmd->obuf);
}
int vdp_clif_command(struct clif *, char *, int);
static int vdp_cmd_getvsi(struct clif *clif, int argc, char *argv[],
struct cmd *cmd, int raw)
{
int numargs = 0;
char **args;
char **argvals;
int i;
if (cmd->cmd != cmd_gettlv)
return cmd_invalid;
args = calloc(argc, sizeof(char *));
if (!args)
return cmd_failed;
argvals = calloc(argc, sizeof(char *));
if (!argvals) {
free(args);
return cmd_failed;
}
for (i = 0; i < argc; i++)
get_arg_value(argv[i], &args[i], &argvals[i]);
numargs = i;
/* Default is local tlv query */
if (!(cmd->ops & op_neighbor))
cmd->ops |= op_local;
if (numargs) {
/* Only commands with the config option should have arguments.*/
if (!(cmd->ops & op_config)) {
printf("%s\n", print_status(cmd_invalid));
goto out;
}
/* Commands to get neighbor TLVs cannot have arguments. */
if (cmd->ops & op_neighbor) {
printf("%s\n", print_status(cmd_invalid));
goto out;
}
cmd->ops |= op_arg;
}
render_cmd(cmd, argc, args, argvals);
free(args);
free(argvals);
return vdp_clif_command(clif, cmd->obuf, raw);
out:
free(args);
free(argvals);
return cmd_invalid;
}
static int vdp_cmd_setvsi(struct clif *clif, int argc, char *argv[],
struct cmd *cmd, int raw)
{
int numargs = 0;
char **args;
char **argvals;
int i;
if (cmd->cmd != cmd_settlv)
return cmd_invalid;
args = calloc(argc, sizeof(char *));
if (!args)
return cmd_failed;
argvals = calloc(argc, sizeof(char *));
if (!argvals) {
free(args);
return cmd_failed;
}
for (i = 0; i < argc; i++)
get_arg_value(argv[i], &args[i], &argvals[i]);
numargs = i;
for (i = 0; i < numargs; i++) {
if (!argvals[i]) {
printf("%s\n", print_status(cmd_invalid));
goto out;
}
}
if (numargs)
cmd->ops |= (op_arg | op_argval);
render_cmd(cmd, argc, args, argvals);
free(args);
free(argvals);
return vdp_clif_command(clif, cmd->obuf, raw);
out:
free(args);
free(argvals);
return cmd_invalid;
}
static int hex2u8(char *b)
{
int hex = -1;
if (isxdigit(*b) && isxdigit(*(b + 1)))
sscanf(b, "%02x", &hex);
return hex;
}
static int hex2u16(char *b)
{
int hex = -1;
if (isxdigit(*b) && isxdigit(*(b + 1)) && isxdigit(*(b + 2))
&& isxdigit(*(b + 3)))
sscanf(b, "%04x", &hex);
return hex;
}
static int hex2u32(char *b)
{
int hex;
char *b_old = b;
for (hex = 0; hex < 8; ++hex)
if (!isxdigit(*b++))
return -1;
sscanf(b_old, "%08x", &hex);
return hex;
}
static int vdp_parse_response(char *buf)
{
return hex2u8(buf + CLIF_STAT_OFF);
}
void print_vsi_err_msg(char *key_val)
{
unsigned long errcode;
int resp_err, smi_err;
errcode = strtol(key_val, NULL, 10);
resp_err = errcode & 0xff;
smi_err = (errcode >> VDP22_STATUS_BITS) & 0xff;
switch (resp_err) {
case VDP22_RESP_INVALID_FORMAT:
printf("\tError returned by Bridge: %s\n",
VSI22_INVALID_FRMT_ERR_STR);
break;
case VDP22_RESP_NO_RESOURCES:
printf("\tError returned by Bridge: %s\n",
VSI22_NO_RES_ERR_STR);
break;
case VDP22_RESP_NO_VSIMGR:
printf("\tError returned by Bridge: %s\n",
VSI22_NO_VSIMGR_ERR_STR);
break;
case VDP22_RESP_OTHER:
printf("\tError returned by Bridge: %s\n", VSI22_OTHER_ERR_STR);
break;
case VDP22_RESP_NOADDR:
printf("\tError returned by Bridge: %s\n",
VSI22_NOADDR_ERR_STR);
break;
case VDP22_RESP_DEASSOC:
printf("\tError returned by Bridge: %s\n", VSI22_DEASS_ERR_STR);
break;
case VDP22_RESP_TIMEOUT:
printf("\tError returned by Bridge: %s\n",
VSI22_TIMEOUT_ERR_STR);
break;
case VDP22_RESP_KEEP:
printf("\tError returned by Bridge: %s\n", VSI22_KEEP_ERR_STR);
break;
default:
break;
}
if (smi_err & (1 << VDP22_KATO))
printf("\tInternal Error : %s\n", VSI22_KATO_ERR_STR);
if (smi_err & (1 << VDP22_ACKTO))
printf("\tInternal Error : %s\n", VSI22_ACKTO_ERR_STR);
if (smi_err & (1 << VDP22_TXERR))
printf("\tInternal Error : %s\n", VSI22_TX_ERR_STR);
}
static void print_oui_vals(char *argvals)
{
char oui_name[VDP22_OUI_MAX_NAME];
char *temp_argval = argvals;
char *oui_val;
int tbl_size, cnt;
u8 oui_name_len;
hexstr2bin(argvals, &oui_name_len, sizeof(oui_name_len));
if (oui_name_len >= VDP22_OUI_MAX_NAME)
return;
temp_argval = argvals + 2 * sizeof(oui_name_len);
oui_val = temp_argval + oui_name_len;
strncpy(oui_name, temp_argval, oui_name_len);
oui_name[oui_name_len] = '\0';
tbl_size = sizeof(oui_hndlr_tbl) / sizeof(vdptool_oui_hndlr_tbl_t);
for (cnt = 0; cnt < tbl_size; cnt++) {
if (!strncmp(oui_hndlr_tbl[cnt].oui_name, oui_name,
VDP22_OUI_MAX_NAME))
oui_hndlr_tbl[cnt].oui_print_decode_hndlr(oui_val);
}
}
static void print_vsi(char **args, char **argvals, int numargs,
bool err_flag)
{
int i;
for (i = 0; i < numargs; i++) {
if (err_flag && (!strcmp(args[i], VSI22_ARG_HINTS_STR)))
print_vsi_err_msg(argvals[i]);
else {
if (!strcmp(args[i], VSI22_ARG_OUI_STR)) {
print_oui_vals(argvals[i]);
} else {
printf("\t%s", args[i]);
printf(" = %s\n", argvals[i]);
}
}
}
}
int get_vsi_args(char *ibuf, bool print_err_code)
{
int ioff = 0;
char **args;
char **argvals;
int numargs;
int ilen = strlen(ibuf);
/* count args and argvalus */
numargs = get_vsistr_arg_count(ioff, ilen);
args = calloc(numargs, sizeof(char *));
if (!args)
return cmd_failed;
argvals = calloc(numargs, sizeof(char *));
if (!argvals) {
free(args);
return cmd_failed;
}
numargs = get_arg_val_list(ibuf, ilen, &ioff, args, argvals);
print_vsi(args, argvals, numargs, print_err_code);
free(args);
free(argvals);
return ioff;
}
static void print_all_vsis(char *ibuf, bool err_code, char *msg)
{
size_t ilen = strlen(ibuf);
u16 vsi_len;
int offset = 0, vsi_cnt = 0;
char tmp_ibuf[strlen(ibuf) + 1];
while (ilen > 0) {
vsi_len = hex2u16(ibuf + offset);
if (vsi_len > ilen)
return;
offset += 2 * sizeof(u16);
ilen -= 2 * sizeof(u16);
strncpy(tmp_ibuf, ibuf + offset, vsi_len);
tmp_ibuf[vsi_len] = '\0';
if (msg)
printf("%s\n", msg);
else
printf("%s %d:\n", "VSI ", vsi_cnt);
get_vsi_args(tmp_ibuf, err_code);
offset += vsi_len;
ilen -= vsi_len;
vsi_cnt++;
}
}
static void print_cmd_response(char *ibuf, int status)
{
struct cmd cmd;
unsigned char len;
int ioff;
if (status != cmd_success) {
printf("FAILED: %s\n", print_status(status));
return;
}
cmd.cmd = hex2u8(ibuf + CMD_CODE);
cmd.ops = hex2u32(ibuf + CMD_OPS);
len = hex2u8(ibuf + CMD_IF_LEN);
ioff = CMD_IF;
if (len < sizeof(cmd.ifname)) {
memcpy(cmd.ifname, ibuf + CMD_IF, len);
} else {
printf("Response ifname too long: %*s\n", (int)len, cmd.ifname);
return;
}
cmd.ifname[len] = '\0';
ioff += len;
if (cmd.cmd == cmd_gettlv || cmd.cmd == cmd_settlv) {
cmd.tlvid = hex2u32(ibuf + ioff);
ioff += 2 * sizeof(cmd.tlvid);
}
switch (cmd.cmd) {
case cmd_gettlv:
print_all_vsis(ibuf + ioff, false, NULL);
break;
case cmd_settlv:
printf("%s", ibuf + ioff);
break;
default:
return;
}
}
static void vdp_print_response(char *buf, int status)
{
switch (buf[CLIF_RSP_OFF]) {
case PING_CMD:
if (status)
printf("FAILED:%s\n", print_status(status));
else
printf("%s\n", buf + CLIF_RSP_OFF + 5);
break;
case ATTACH_CMD:
case DETACH_CMD:
if (status)
printf("FAILED:%s\n", print_status(status));
else
printf("OK\n");
break;
case CMD_REQUEST:
print_cmd_response(buf + CLIF_RSP_OFF, status);
break;
default:
printf("Unknown VDP command response: %s\n", buf);
break;
}
}
static void vdp_print_event_msg(char *buf)
{
printf("%s buf:%s\n", __func__, buf);
print_all_vsis(buf + CLIF_RSP_OFF, true, "Response from VDP");
}
/*
* Dummy function to avoid linkage of many sources
*/
int get_perm_hwaddr(UNUSED const char *ifname, UNUSED unsigned char *buf_perm,
UNUSED unsigned char *buf_san)
{
return -EIO;
}
static int show_raw;
static const char *cli_version =
"vdptool v" LLDPTOOL_VERSION "\n"
"Copyright (c) 2014, IBM Corporation\n";
static const char *cli_license =
"This program is free software. You can distribute it and/or modify it\n"
"under the terms of the GNU General Public License version 2.\n"
"\n";
/*
"Alternatively, this software may be distributed under the terms of the\n"
"BSD license. See README and COPYING for more details.\n";
*/
static const char *cli_full_license =
"This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License version 2 as\n"
"published by the Free Software Foundation.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
"\n"
"Alternatively, this software may be distributed under the terms of the\n"
"BSD license.\n"
"\n"
"Redistribution and use in source and binary forms, with or without\n"
"modification, are permitted provided that the following conditions are\n"
"met:\n"
"\n"
"1. Redistributions of source code must retain the above copyright\n"
" notice, this list of conditions and the following disclaimer.\n"
"\n"
"2. Redistributions in binary form must reproduce the above copyright\n"
" notice, this list of conditions and the following disclaimer in the\n"
" documentation and/or other materials provided with the distribution.\n"
"\n"
"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
" names of its contributors may be used to endorse or promote products\n"
" derived from this software without specific prior written permission.\n"
"\n"
"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
"\n";
static const char *commands_usage =
"Usage:\n"
" vdptool <command> [options] [arg] general command line usage format\n"
" vdptool go into interactive mode\n"
" <command> [options] [arg] general interactive command format\n";
static const char *commands_options =
"Options:\n"
" -i [ifname] network interface\n"
" -V [tlvid] TLV identifier\n"
" may be numeric or keyword (see below)\n"
" -c <argument list> used with get TLV command to specify\n"
" that the list of configuration elements\n"
" -n \"neighbor\" option for command (To be done)\n"
" -r show raw message\n"
" -R show only raw messages\n";
static const char *commands_help =
"Commands:\n"
" license show license information\n"
" -h|help show command usage information\n"
" -v|version show version\n"
" -p|ping ping lldpad and query pid of lldpad\n"
" -q|quit exit lldptool (interactive mode)\n"
" -t|get-vsi get VSI association(s)\n"
" -T|set-vsi set VSI association\n";
static struct clif *clif_conn;
static int cli_quit;
static int cli_attached;
/*
* insert to head, so first one is last
*/
struct lldp_module *(*register_tlv_table[])(void) = {
vdp22_cli_register,
NULL,
};
static void init_modules(void)
{
struct lldp_module *module;
struct lldp_module *premod = NULL;
int i = 0;
LIST_INIT(&lldp_mod_head);
for (i = 0; register_tlv_table[i]; i++) {
module = register_tlv_table[i]();
if (premod)
LIST_INSERT_AFTER(premod, module, lldp);
else
LIST_INSERT_HEAD(&lldp_mod_head, module, lldp);
premod = module;
}
}
void deinit_modules(void)
{
struct lldp_module *module;
while (lldp_mod_head.lh_first != NULL) {
module = lldp_mod_head.lh_first;
LIST_REMOVE(lldp_mod_head.lh_first, lldp);
module->ops->lldp_mod_unregister(module);
}
}
static void usage(void)
{
fprintf(stderr, "%s\n", cli_version);
fprintf(stderr, "\n%s\n%s\n%s\n",
commands_usage, commands_options, commands_help);
}
static void print_raw_message(char *msg, int print)
{
if (!print || !(print & SHOW_RAW))
return;
if (!(print & SHOW_RAW_ONLY)) {
switch (msg[MSG_TYPE]) {
case EVENT_MSG:
printf("event: ");
break;
case CMD_RESPONSE:
printf("rsp: ");
break;
default:
printf("cmd: ");
break;
}
}
printf("%s\n", msg);
}
static int parse_print_message(char *msg, int print)
{
int status = 0;
status = vdp_parse_response(msg);
print_raw_message(msg, print);
if (print & SHOW_RAW_ONLY)
return status;
if (msg[MSG_TYPE] == CMD_RESPONSE)
vdp_print_response(msg, status);
else if (msg[MSG_TYPE] == MOD_CMD && msg[MOD_MSG_TYPE] == EVENT_MSG)
vdp_print_event_msg(&msg[MOD_MSG_TYPE]);
return status;
}
static void cli_close_connection(void)
{
if (clif_conn == NULL)
return;
if (cli_attached) {
clif_detach(clif_conn);
cli_attached = 0;
}
clif_close(clif_conn);
clif_conn = NULL;
}
static void cli_msg_cb(char *msg, UNUSED size_t len)
{
parse_print_message(msg, SHOW_OUTPUT | show_raw);
}
/* structure of the print argument bitmap:
* SHOW_NO_OUTPUT (0x0) - don't print anything for the command
* SHOW_OUTPUT (0x01) - print output for the command
* SHOW_RAW (0x02) - print the raw clif command messages
* SHOW_RAW_ONLY (0x04) - print only the raw clif command messages
*/
static int _clif_command(struct clif *clif, char *cmd, int print)
{
char buf[MAX_CLIF_MSGBUF];
size_t len;
int ret;
int rc;
char reply[MAX_CLIF_MSGBUF];
size_t reply_len2 = sizeof(reply);
print_raw_message(cmd, print);
if (clif_conn == NULL) {
printf("Not connected to lldpad - command dropped.\n");
return -1;
}
len = sizeof(buf) - 1;
ret = clif_request(clif, cmd, strlen(cmd), buf, &len, cli_msg_cb);
if (ret == -2) {
printf("'%s' command timed out.\n", cmd);
return -2;
} else if (ret < 0) {
printf("'%s' command failed with error %s.\n", cmd,
strerror(errno));
return -1;
}
if (print) {
buf[len] = '\0';
ret = parse_print_message(buf, print);
}
if (cli_attached) {
rc = clif_vsievt(clif, reply, &reply_len2, 5);
if (!rc)
print_all_vsis(reply, true, "Response from VDP");
}
return ret;
}
int vdp_clif_command(struct clif *clif, char *cmd, int raw)
{
return _clif_command(clif, cmd, SHOW_OUTPUT | raw);
}
static int cli_cmd_ping(struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, int raw)
{
return vdp_clif_command(clif, "P", raw);
}
static int
cli_cmd_nop(UNUSED struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, UNUSED int raw)
{
return 0;
}
static int
cli_cmd_help(UNUSED struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, UNUSED int raw)
{
struct lldp_module *np;
printf("%s\n%s\n%s", commands_usage, commands_options, commands_help);
printf("\nTLV identifiers:\n");
LIST_FOREACH(np, &lldp_mod_head, lldp)
if (np->ops->print_help)
np->ops->print_help();
return 0;
}
static int
cli_cmd_version(UNUSED struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, UNUSED int raw)
{
printf("%s\n", cli_version);
return 0;
}
static int
cli_cmd_license(UNUSED struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, UNUSED int raw)
{
printf("%s\n", cli_full_license);
return 0;
}
static int
cli_cmd_quit(UNUSED struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, UNUSED int raw)
{
cli_quit = 1;
return 0;
}
static struct cli_cmd {
vdp22_cmd cmdcode;
const char *cmdstr;
int (*handler)(struct clif *clif, int argc, char *argv[],
struct cmd *cmd, int raw);
} cli_commands[] = {
{ cmd_ping, "ping", cli_cmd_ping },
{ cmd_help, "help", cli_cmd_help },
{ cmd_license, "license", cli_cmd_license },
{ cmd_version, "version", cli_cmd_version },
{ cmd_quit, "quit", cli_cmd_quit },
{ cmd_gettlv, "getvsi", vdp_cmd_getvsi },
{ cmd_gettlv, "get-vsi", vdp_cmd_getvsi },