forked from iwilson16/mstpd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctl_main.c
1783 lines (1626 loc) · 57.8 KB
/
ctl_main.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
/*****************************************************************************
Copyright (c) 2011 Factor-SPE
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that 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., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
The full GNU General Public License is included in this distribution in the
file called LICENSE.
Authors: Vitalii Demianets <[email protected]>
******************************************************************************/
#include <string.h>
#include <getopt.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>
#include "ctl_socket_client.h"
#include "log.h"
#ifdef __LIBC_HAS_VERSIONSORT__
#define sorting_func versionsort
#else
#define sorting_func alphasort
#endif
static int get_index_die(const char *ifname, const char *doc, bool die)
{
int r = if_nametoindex(ifname);
if(0 == r)
{
fprintf(stderr,
"Can't find index for %s %s. Not a valid interface.\n",
doc, ifname);
if(die)
exit(1);
return -1;
}
return r;
}
static inline int get_index(const char *ifname, const char *doc)
{
return get_index_die(ifname, doc, true);
}
static inline int get_id(const char *str, const char *doc, unsigned int max_id)
{
int id = strtol(str, NULL, 10);
if((0 > id) || (max_id < id)
|| ((0 == id) && ('0' != str[0]))
)
{
fprintf(stderr, "Bad %s %s\n", doc, str);
return -1;
}
return id;
}
#define GET_NUM_FROM_PRIO(p) (__be16_to_cpu(p) & 0x0FFF)
#define BR_ID_FMT "%01hhX.%03hX.%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX"
#define BR_ID_ARGS(x) ((GET_PRIORITY_FROM_IDENTIFIER(x) >> 4) & 0x0F), \
GET_NUM_FROM_PRIO((x).s.priority), \
x.s.mac_address[0], x.s.mac_address[1], x.s.mac_address[2], \
x.s.mac_address[3], x.s.mac_address[4], x.s.mac_address[5]
#define PRT_ID_FMT "%01hhX.%03hX"
#define PRT_ID_ARGS(x) ((GET_PRIORITY_FROM_IDENTIFIER(x) >> 4) & 0x0F), \
GET_NUM_FROM_PRIO(x)
#define BOOL_STR(x) ((x) ? "yes" : "no")
#define PROTO_VERS_STR(x) ((protoRSTP == (x)) ? "rstp" : \
((protoMSTP <= (x)) ? "mstp" : "stp"))
typedef enum {
PARAM_NULL = 0,
/* bridge params */
PARAM_ENABLED,
PARAM_BRID,
PARAM_DSGNROOT,
PARAM_REGNROOT,
PARAM_ROOTPORT,
PARAM_PATHCOST,
PARAM_INTPATHCOST,
PARAM_MAXAGE,
PARAM_BRMAXAGE,
PARAM_FWDDELAY,
PARAM_BRFWDDELAY,
PARAM_TXHOLDCNT,
PARAM_MAXHOPS,
PARAM_BRHELLO,
PARAM_BRAGEING,
PARAM_FORCEPROTVERS,
PARAM_TOPCHNGTIME,
PARAM_TOPCHNGCNT,
PARAM_TOPCHNGSTATE,
/* port params */
PARAM_ROLE,
PARAM_STATE,
PARAM_PORTID,
PARAM_EXTPORTCOST,
PARAM_ADMINEXTCOST,
PARAM_INTPORTCOST,
PARAM_ADMININTCOST,
PARAM_DSGNEXTCOST,
PARAM_DSGNRROOT,
PARAM_DSGNINTCOST,
PARAM_DSGNBR,
PARAM_DSGNPORT,
PARAM_ADMINEDGEPORT,
PARAM_AUTOEDGEPORT,
PARAM_OPEREDGEPORT,
PARAM_TOPCHNGACK,
PARAM_P2P,
PARAM_ADMINP2P,
PARAM_RESTRROLE,
PARAM_RESTRTCN,
PARAM_PORTHELLOTIME,
PARAM_DISPUTED,
PARAM_BPDUGUARDPORT,
PARAM_BPDUGUARDERROR,
PARAM_NETWORKPORT,
PARAM_BA_INCONSISTENT,
} param_id_t;
typedef struct {
param_id_t id;
char *str;
} cmd_param_t;
static const cmd_param_t cist_bridge_params[] = {
{ PARAM_ENABLED, "enabled" },
{ PARAM_BRID, "bridge-id" },
{ PARAM_DSGNROOT, "designated-root" },
{ PARAM_REGNROOT, "regional-root" },
{ PARAM_ROOTPORT, "root-port" },
{ PARAM_PATHCOST, "path-cost" },
{ PARAM_INTPATHCOST, "internal-path-cost" },
{ PARAM_MAXAGE, "max-age" },
{ PARAM_BRMAXAGE, "bridge-max-age" },
{ PARAM_FWDDELAY, "forward-delay" },
{ PARAM_BRFWDDELAY, "bridge-forward-delay" },
{ PARAM_TXHOLDCNT, "tx-hold-count" },
{ PARAM_MAXHOPS, "max-hops" },
{ PARAM_BRHELLO, "hello-time" },
{ PARAM_BRAGEING, "ageing-time" },
{ PARAM_FORCEPROTVERS,"force-protocol-version" },
{ PARAM_TOPCHNGTIME, "time-since-topology-change" },
{ PARAM_TOPCHNGCNT, "topology-change-count" },
{ PARAM_TOPCHNGSTATE, "topology-change" },
};
static int do_showbridge(const char *br_name, param_id_t param_id)
{
CIST_BridgeStatus s;
char root_port_name[IFNAMSIZ];
unsigned int root_portno;
int br_index = get_index_die(br_name, "bridge", false);
if(0 > br_index)
return br_index;
if(CTL_get_cist_bridge_status(br_index, &s, root_port_name))
return -1;
switch(param_id)
{
case PARAM_NULL:
printf("%s CIST info\n", br_name);
printf(" enabled %s\n", BOOL_STR(s.enabled));
printf(" bridge id "BR_ID_FMT"\n",
BR_ID_ARGS(s.bridge_id));
printf(" designated root "BR_ID_FMT"\n",
BR_ID_ARGS(s.designated_root));
printf(" regional root "BR_ID_FMT"\n",
BR_ID_ARGS(s.regional_root));
printf(" root port ");
if(0 != (root_portno = GET_NUM_FROM_PRIO(s.root_port_id)))
printf("%s (#%u)\n", root_port_name, root_portno);
else
printf("none\n");
printf(" path cost %-10u ", s.root_path_cost);
printf("internal path cost %u\n", s.internal_path_cost);
printf(" max age %-10hhu ", s.root_max_age);
printf("bridge max age %hhu\n", s.bridge_max_age);
printf(" forward delay %-10hhu ", s.root_forward_delay);
printf("bridge forward delay %hhu\n", s.bridge_forward_delay);
printf(" tx hold count %-10u ", s.tx_hold_count);
printf("max hops %hhu\n", s.max_hops);
printf(" hello time %-10u ", s.bridge_hello_time);
printf("ageing time %u\n", s.Ageing_Time);
printf(" force protocol version %s\n",
PROTO_VERS_STR(s.protocol_version));
printf(" time since topology change %u\n",
s.time_since_topology_change);
printf(" topology change count %u\n",
s.topology_change_count);
printf(" topology change %s\n",
BOOL_STR(s.topology_change));
printf(" topology change port %s\n",
s.topology_change_port);
printf(" last topology change port %s\n",
s.last_topology_change_port);
break;
case PARAM_ENABLED:
printf("%s\n", BOOL_STR(s.enabled));
break;
case PARAM_BRID:
printf(BR_ID_FMT"\n", BR_ID_ARGS(s.bridge_id));
break;
case PARAM_DSGNROOT:
printf(BR_ID_FMT"\n", BR_ID_ARGS(s.designated_root));
break;
case PARAM_REGNROOT:
printf(BR_ID_FMT"\n", BR_ID_ARGS(s.regional_root));
break;
case PARAM_ROOTPORT:
if(0 != (root_portno = GET_NUM_FROM_PRIO(s.root_port_id)))
printf("%s\n", root_port_name);
else
printf("\n");
break;
case PARAM_PATHCOST:
printf("%u\n", s.root_path_cost);
break;
case PARAM_INTPATHCOST:
printf("%u\n", s.internal_path_cost);
break;
case PARAM_MAXAGE:
printf("%hhu\n", s.root_max_age);
break;
case PARAM_BRMAXAGE:
printf("%hhu\n", s.bridge_max_age);
break;
case PARAM_FWDDELAY:
printf("%hhu\n", s.root_forward_delay);
break;
case PARAM_BRFWDDELAY:
printf("%hhu\n", s.bridge_forward_delay);
break;
case PARAM_TXHOLDCNT:
printf("%u\n", s.tx_hold_count);
break;
case PARAM_MAXHOPS:
printf("%hhu\n", s.max_hops);
break;
case PARAM_BRHELLO:
printf("%hhu\n", s.bridge_hello_time);
break;
case PARAM_BRAGEING:
printf("%u\n", s.Ageing_Time);
break;
case PARAM_FORCEPROTVERS:
printf("%s\n", PROTO_VERS_STR(s.protocol_version));
break;
case PARAM_TOPCHNGTIME:
printf("%u\n", s.time_since_topology_change);
break;
case PARAM_TOPCHNGCNT:
printf("%u\n", s.topology_change_count);
break;
case PARAM_TOPCHNGSTATE:
printf("%s\n", BOOL_STR(s.topology_change));
break;
default:
return -2; /* -2 = unknown param */
}
return 0;
}
#define SYSFS_PATH_MAX 256
#ifndef SYSFS_CLASS_NET
#define SYSFS_CLASS_NET "/sys/class/net"
#endif
static int isbridge(const struct dirent *entry)
{
char path[SYSFS_PATH_MAX];
int save_errno;
bool result;
struct stat st;
snprintf(path, SYSFS_PATH_MAX, SYSFS_CLASS_NET "/%s/bridge",
entry->d_name);
save_errno = errno;
result = (0 == stat(path, &st)) && S_ISDIR(st.st_mode);
errno = save_errno;
return result;
}
static inline int get_bridge_list(struct dirent ***namelist)
{
return scandir(SYSFS_CLASS_NET, namelist, isbridge, sorting_func);
}
static int cmd_showbridge(int argc, char *const *argv)
{
int i, count = 0;
int r = 0;
struct dirent **namelist;
param_id_t param_id = PARAM_NULL;
if(1 < argc)
{
count = argc - 1;
if(1 < count)
{ /* check if last argument is known parameter name */
for(i = 0; i < COUNT_OF(cist_bridge_params); ++i)
{
if(0 == strcmp(argv[count], cist_bridge_params[i].str))
{
param_id = cist_bridge_params[i].id;
--count;
break;
}
}
}
}
else
{
count = get_bridge_list(&namelist);
if(0 > count)
{
fprintf(stderr, "Error getting list of all bridges\n");
return -1;
}
}
for(i = 0; i < count; ++i)
{
const char *name;
if(1 < argc)
name = argv[i + 1];
else
name = namelist[i]->d_name;
int err = do_showbridge(name, param_id);
if(err)
r = err;
}
if(1 >= argc)
{
for(i = 0; i < count; ++i)
free(namelist[i]);
free(namelist);
}
return r;
}
static int cmd_showtree(int argc, char *const *argv)
{
MSTI_BridgeStatus s;
char root_port_name[IFNAMSIZ];
unsigned int root_portno;
int br_index = get_index(argv[1], "bridge");
if(0 > br_index)
return br_index;
int mstid = get_id(argv[2], "mstid", MAX_MSTID);
if(0 > mstid)
return mstid;
if(CTL_get_msti_bridge_status(br_index, mstid, &s, root_port_name))
return -1;
printf("%s MSTI %hu info\n", argv[1], mstid);
printf(" bridge id "BR_ID_FMT"\n", BR_ID_ARGS(s.bridge_id));
printf(" regional root "BR_ID_FMT"\n", BR_ID_ARGS(s.regional_root));
printf(" root port ");
if(0 != (root_portno = GET_NUM_FROM_PRIO(s.root_port_id)))
printf("%s (#%u)\n", root_port_name, root_portno);
else
printf("none\n");
printf(" internal path cost %u\n", s.internal_path_cost);
printf(" time since topology change %u\n", s.time_since_topology_change);
printf(" topology change count %u\n", s.topology_change_count);
printf(" topology change %s\n", BOOL_STR(s.topology_change));
printf(" topology change port %s\n", s.topology_change_port);
printf(" last topology change port %s\n", s.last_topology_change_port);
return 0;
}
#define STATE_STR(_state) \
({ \
int _s = _state; \
char *_str = "unknown"; \
switch(_s) \
{ \
case BR_STATE_DISABLED: \
case BR_STATE_BLOCKING: \
case BR_STATE_LISTENING: _str = "discarding"; break; \
case BR_STATE_LEARNING: _str = "learning"; break; \
case BR_STATE_FORWARDING:_str = "forwarding"; break; \
} \
_str; \
})
#define SHORT_STATE_STR(_state) \
({ \
int _s = _state; \
char *_str = "unkn"; \
switch(_s) \
{ \
case BR_STATE_DISABLED: \
case BR_STATE_BLOCKING: \
case BR_STATE_LISTENING: _str = "disc"; break; \
case BR_STATE_LEARNING: _str = "lear"; break; \
case BR_STATE_FORWARDING:_str = "forw"; break; \
} \
_str; \
})
#define ADMIN_P2P_STR(_state) \
({ \
admin_p2p_t _s = _state; \
char *_str = "unkn"; \
switch(_s) \
{ \
case p2pForceFalse:_str = "no"; break; \
case p2pForceTrue: _str = "yes"; break; \
case p2pAuto: _str = "auto"; break; \
} \
_str; \
})
#define ROLE_STR(_role) \
({ \
port_role_t _r = _role; \
char *_str = "Unknown"; \
switch(_r) \
{ \
case roleRoot: _str = "Root"; break; \
case roleDesignated:_str = "Designated"; break; \
case roleAlternate: _str = "Alternate"; break; \
case roleBackup: _str = "Backup"; break; \
case roleMaster: _str = "Master"; break; \
case roleDisabled: _str = "Disabled"; break; \
} \
_str; \
})
#define SHORT_ROLE_STR(_role) \
({ \
port_role_t _r = _role; \
char *_str = "Unkn"; \
switch(_r) \
{ \
case roleRoot: _str = "Root"; break; \
case roleDesignated:_str = "Desg"; break; \
case roleAlternate: _str = "Altn"; break; \
case roleBackup: _str = "Back"; break; \
case roleMaster: _str = "Mstr"; break; \
case roleDisabled: _str = "Disa"; break; \
} \
_str; \
})
static const cmd_param_t cist_port_params[] = {
{ PARAM_ENABLED, "enabled" },
{ PARAM_ROLE, "role" },
{ PARAM_STATE, "state" },
{ PARAM_PORTID, "port-id" },
{ PARAM_EXTPORTCOST, "external-port-cost" },
{ PARAM_ADMINEXTCOST, "admin-external-cost" },
{ PARAM_INTPORTCOST, "internal-port-cost" },
{ PARAM_ADMININTCOST, "admin-internal-cost" },
{ PARAM_DSGNROOT, "designated-root" },
{ PARAM_DSGNEXTCOST, "dsgn-external-cost" },
{ PARAM_DSGNRROOT, "dsgn-regional-root" },
{ PARAM_DSGNINTCOST, "dsgn-internal-cost" },
{ PARAM_DSGNBR, "designated-bridge" },
{ PARAM_DSGNPORT, "designated-port" },
{ PARAM_ADMINEDGEPORT, "admin-edge-port" },
{ PARAM_AUTOEDGEPORT, "auto-edge-port" },
{ PARAM_OPEREDGEPORT, "oper-edge-port" },
{ PARAM_TOPCHNGACK, "topology-change-ack" },
{ PARAM_P2P, "point-to-point" },
{ PARAM_ADMINP2P, "admin-point-to-point" },
{ PARAM_RESTRROLE, "restricted-role" },
{ PARAM_RESTRTCN, "restricted-TCN" },
{ PARAM_PORTHELLOTIME, "port-hello-time" },
{ PARAM_DISPUTED, "disputed" },
{ PARAM_BPDUGUARDPORT, "bpdu-guard-port" },
{ PARAM_BPDUGUARDERROR, "bpdu-guard-error" },
{ PARAM_NETWORKPORT, "network-port" },
{ PARAM_BA_INCONSISTENT,"ba-inconsistent" },
};
static int detail = 0;
static int do_showport(int br_index, const char *bridge_name,
const char *port_name, param_id_t param_id)
{
CIST_PortStatus s;
int r = 0;
int port_index = get_index_die(port_name, "port", false);
if(0 > port_index)
return port_index;
if((r = CTL_get_cist_port_status(br_index, port_index, &s)))
{
fprintf(stderr, "%s:%s Failed to get port state\n",
bridge_name, port_name);
return -1;
}
switch(param_id)
{
case PARAM_NULL:
if(detail)
{
printf("%s:%s CIST info\n", bridge_name, port_name);
printf(" enabled %-23s ", BOOL_STR(s.enabled));
printf("role %s\n", ROLE_STR(s.role));
printf(" port id "PRT_ID_FMT" ",
PRT_ID_ARGS(s.port_id));
printf("state %s\n", STATE_STR(s.state));
printf(" external port cost %-23u ",
s.external_port_path_cost);
printf("admin external cost %u\n",
s.admin_external_port_path_cost);
printf(" internal port cost %-23u ",
s.internal_port_path_cost);
printf("admin internal cost %u\n",
s.admin_internal_port_path_cost);
printf(" designated root "BR_ID_FMT" ",
BR_ID_ARGS(s.designated_root));
printf("dsgn external cost %u\n",
s.designated_external_cost);
printf(" dsgn regional root "BR_ID_FMT" ",
BR_ID_ARGS(s.designated_regional_root));
printf("dsgn internal cost %u\n",
s.designated_internal_cost);
printf(" designated bridge "BR_ID_FMT" ",
BR_ID_ARGS(s.designated_bridge));
printf("designated port "PRT_ID_FMT"\n",
PRT_ID_ARGS(s.designated_port));
printf(" admin edge port %-23s ",
BOOL_STR(s.admin_edge_port));
printf("auto edge port %s\n",
BOOL_STR(s.auto_edge_port));
printf(" oper edge port %-23s ",
BOOL_STR(s.oper_edge_port));
printf("topology change ack %s\n", BOOL_STR(s.tc_ack));
printf(" point-to-point %-23s ", BOOL_STR(s.oper_p2p));
printf("admin point-to-point %s\n",
ADMIN_P2P_STR(s.admin_p2p));
printf(" restricted role %-23s ",
BOOL_STR(s.restricted_role));
printf("restricted TCN %s\n",
BOOL_STR(s.restricted_tcn));
printf(" port hello time %-23hhu ", s.port_hello_time);
printf("disputed %s\n", BOOL_STR(s.disputed));
printf(" bpdu guard port %-23s ",
BOOL_STR(s.bpdu_guard_port));
printf("bpdu guard error %s\n",
BOOL_STR(s.bpdu_guard_error));
printf(" network port %-23s ",
BOOL_STR(s.network_port));
printf("BA inconsistent %s\n",
BOOL_STR(s.ba_inconsistent));
printf(" Num TX BPDU %-23u ", s.num_tx_bpdu);
printf("Num TX TCN %u\n", s.num_tx_tcn);
printf(" Num RX BPDU %-23u ", s.num_rx_bpdu);
printf("Num RX TCN %u\n", s.num_rx_tcn);
printf(" Num Transition FWD %-23u ", s.num_trans_fwd);
printf("Num Transition BLK %u\n", s.num_trans_blk);
printf(" Rcvd BPDU %-23s ", BOOL_STR(s.rcvdBpdu));
printf("Rcvd STP %s\n", BOOL_STR(s.rcvdSTP));
printf(" Rcvd RSTP %-23s ", BOOL_STR(s.rcvdRSTP));
printf("Send RSTP %s\n", BOOL_STR(s.sendRSTP));
printf(" Rcvd TC Ack %-23s ", BOOL_STR(s.rcvdTcAck));
printf("Rcvd TCN %s\n", BOOL_STR(s.rcvdTcn));
}
else
{
printf("%c%c %-5s "PRT_ID_FMT" %4s "BR_ID_FMT" "BR_ID_FMT" "
PRT_ID_FMT" %s\n",
(s.oper_p2p) ? ' ' : '*',
(s.oper_edge_port) ? 'E' : ' ',
port_name,
PRT_ID_ARGS(s.port_id),
s.enabled ? SHORT_STATE_STR(s.state) : "down",
BR_ID_ARGS(s.designated_root),
BR_ID_ARGS(s.designated_bridge),
PRT_ID_ARGS(s.designated_port),
SHORT_ROLE_STR(s.role));
}
break;
case PARAM_ENABLED:
printf("%s\n", BOOL_STR(s.enabled));
break;
case PARAM_ROLE:
printf("%s\n", ROLE_STR(s.role));
break;
case PARAM_STATE:
printf("%s\n", STATE_STR(s.state));
break;
case PARAM_PORTID:
printf(PRT_ID_FMT"\n", PRT_ID_ARGS(s.port_id));
break;
case PARAM_EXTPORTCOST:
printf("%u\n", s.external_port_path_cost);
break;
case PARAM_ADMINEXTCOST:
printf("%u\n", s.admin_external_port_path_cost);
break;
case PARAM_INTPORTCOST:
printf("%u\n", s.internal_port_path_cost);
break;
case PARAM_ADMININTCOST:
printf("%u\n", s.admin_internal_port_path_cost);
break;
case PARAM_DSGNROOT:
printf(BR_ID_FMT"\n", BR_ID_ARGS(s.designated_root));
break;
case PARAM_DSGNEXTCOST:
printf("%u\n", s.designated_external_cost);
break;
case PARAM_DSGNRROOT:
printf(BR_ID_FMT"\n", BR_ID_ARGS(s.designated_regional_root));
break;
case PARAM_DSGNINTCOST:
printf("%u\n", s.designated_internal_cost);
break;
case PARAM_DSGNBR:
printf(BR_ID_FMT"\n", BR_ID_ARGS(s.designated_bridge));
break;
case PARAM_DSGNPORT:
printf(PRT_ID_FMT"\n", PRT_ID_ARGS(s.designated_port));
break;
case PARAM_ADMINEDGEPORT:
printf("%s\n", BOOL_STR(s.admin_edge_port));
break;
case PARAM_AUTOEDGEPORT:
printf("%s\n", BOOL_STR(s.auto_edge_port));
break;
case PARAM_OPEREDGEPORT:
printf("%s\n", BOOL_STR(s.oper_edge_port));
break;
case PARAM_TOPCHNGACK:
printf("%s\n", BOOL_STR(s.tc_ack));
break;
case PARAM_P2P:
printf("%s\n", BOOL_STR(s.oper_p2p));
break;
case PARAM_ADMINP2P:
printf("%s\n", ADMIN_P2P_STR(s.admin_p2p));
break;
case PARAM_RESTRROLE:
printf("%s\n", BOOL_STR(s.restricted_role));
break;
case PARAM_RESTRTCN:
printf("%s\n", BOOL_STR(s.restricted_tcn));
break;
case PARAM_PORTHELLOTIME:
printf("%hhu\n", s.port_hello_time);
break;
case PARAM_DISPUTED:
printf("%s\n", BOOL_STR(s.disputed));
break;
case PARAM_BPDUGUARDPORT:
printf("%s\n", BOOL_STR(s.bpdu_guard_port));
break;
case PARAM_BPDUGUARDERROR:
printf("%s\n", BOOL_STR(s.bpdu_guard_error));
break;
case PARAM_NETWORKPORT:
printf("%s\n", BOOL_STR(s.network_port));
break;
case PARAM_BA_INCONSISTENT:
printf("%s\n", BOOL_STR(s.ba_inconsistent));
break;
default:
return -2; /* -2 = unknown param */
}
return 0;
}
static int not_dot_dotdot(const struct dirent *entry)
{
const char *n = entry->d_name;
return !('.' == n[0] && (0 == n[1] || ('.' == n[1] && 0 == n[2])));
}
static int get_port_list(const char *br_ifname, struct dirent ***namelist)
{
int res;
char buf[SYSFS_PATH_MAX];
snprintf(buf, sizeof(buf), SYSFS_CLASS_NET "/%s/brif", br_ifname);
if(0 > (res = scandir(buf, namelist, not_dot_dotdot, sorting_func)))
fprintf(stderr, "Error getting list of all ports of bridge %s\n",
br_ifname);
return res;
}
static int cmd_showport(int argc, char *const *argv)
{
int r = 0;
int br_index = get_index(argv[1], "bridge");
if(0 > br_index)
return br_index;
int i, count = 0;
struct dirent **namelist;
param_id_t param_id = PARAM_NULL;
if(2 < argc)
{
count = argc - 2;
if(1 < count)
{ /* check if last argument is known parameter name */
for(i = 0; i < COUNT_OF(cist_port_params); ++i)
{
if(0 == strcmp(argv[argc - 1], cist_port_params[i].str))
{
param_id = cist_port_params[i].id;
--count;
break;
}
}
}
}
else
{
if(0 > (count = get_port_list(argv[1], &namelist)))
return count;
}
for(i = 0; i < count; ++i)
{
const char *name;
if(2 < argc)
name = argv[i + 2];
else
name = namelist[i]->d_name;
int err = do_showport(br_index, argv[1], name, param_id);
if(err)
r = err;
}
if(2 >= argc)
{
for(i = 0; i < count; ++i)
free(namelist[i]);
free(namelist);
}
return r;
}
static int cmd_showportdetail(int argc, char *const *argv)
{
detail = 1;
return cmd_showport(argc, argv);
}
static int cmd_showtreeport(int argc, char *const *argv)
{
MSTI_PortStatus s;
int br_index = get_index(argv[1], "bridge");
if(0 > br_index)
return br_index;
int port_index = get_index(argv[2], "port");
if(0 > port_index)
return port_index;
int mstid = get_id(argv[3], "mstid", MAX_MSTID);
if(0 > mstid)
return mstid;
if(CTL_get_msti_port_status(br_index, port_index, mstid, &s))
return -1;
printf("%s:%s MSTI %hu info\n", argv[1], argv[2], mstid);
printf(" role %-23s ", ROLE_STR(s.role));
printf("port id "PRT_ID_FMT"\n", PRT_ID_ARGS(s.port_id));
printf(" state %-23s ", STATE_STR(s.state));
printf("disputed %s\n", BOOL_STR(s.disputed));
printf(" internal port cost %-23u ", s.internal_port_path_cost);
printf("admin internal cost %u\n", s.admin_internal_port_path_cost);
printf(" dsgn regional root "BR_ID_FMT" ",
BR_ID_ARGS(s.designated_regional_root));
printf("dsgn internal cost %u\n", s.designated_internal_cost);
printf(" designated bridge "BR_ID_FMT" ",
BR_ID_ARGS(s.designated_bridge));
printf("designated port "PRT_ID_FMT"\n",
PRT_ID_ARGS(s.designated_port));
return 0;
}
static int cmd_addbridge(int argc, char *const *argv)
{
int i, j, res, ifcount, brcount = argc - 1;
int *br_array;
int* *ifaces_lists;
if(NULL == (br_array = malloc((brcount + 1) * sizeof(int))))
{
out_of_memory_exit:
fprintf(stderr, "out of memory, brcount = %d\n", brcount);
return -1;
}
if(NULL == (ifaces_lists = malloc(brcount * sizeof(int*))))
{
free(br_array);
goto out_of_memory_exit;
}
br_array[0] = brcount;
for(i = 1; i <= brcount; ++i)
{
struct dirent **namelist;
br_array[i] = get_index(argv[i], "bridge");
if(0 > (ifcount = get_port_list(argv[i], &namelist)))
{
ifaces_error_exit:
for(i -= 2; i >= 0; --i)
free(ifaces_lists[i]);
free(ifaces_lists);
free(br_array);
return ifcount;
}
if(NULL == (ifaces_lists[i - 1] = malloc((ifcount + 1) * sizeof(int))))
{
fprintf(stderr, "out of memory, bridge %s, ifcount = %d\n",
argv[i], ifcount);
for(j = 0; j < ifcount; ++j)
free(namelist[j]);
free(namelist);
ifcount = -1;
goto ifaces_error_exit;
}
ifaces_lists[i - 1][0] = ifcount;
for(j = 1; j <= ifcount; ++j)
{
ifaces_lists[i - 1][j] = get_index(namelist[j - 1]->d_name, "port");
free(namelist[j - 1]);
}
free(namelist);
}
res = CTL_add_bridges(br_array, ifaces_lists);
for(i = 0; i < brcount; ++i)
free(ifaces_lists[i]);
free(ifaces_lists);
free(br_array);
return res;
}
static int cmd_delbridge(int argc, char *const *argv)
{
int i, res, brcount = argc - 1;
int *br_array;
if(NULL == (br_array = malloc((brcount + 1) * sizeof(int))))
{
fprintf(stderr, "out of memory, brcount = %d\n", brcount);
return -1;
}
br_array[0] = brcount;
for(i = 1; i <= brcount; ++i)
br_array[i] = get_index(argv[i], "bridge");
res = CTL_del_bridges(br_array);
free(br_array);
return res;
}
static unsigned int getuint(const char *s)
{
char *end;
long l;
l = strtoul(s, &end, 0);
if(0 == *s || 0 != *end || INT_MAX < l)
{
fprintf(stderr, "Invalid unsigned int arg %s\n", s);
exit(1);
}
return l;
}
static int getenum(const char *s, const char *opt[])
{
int i;
for(i = 0; opt[i] != NULL; ++i)
if(0 == strcmp(s, opt[i]))
return i;
fprintf(stderr, "Invalid argument %s: expecting one of ", s);
for(i = 0; opt[i] != NULL; ++i)
fprintf(stderr, "%s%s", opt[i], (opt[i + 1] ? ", " : "\n"));
exit(1);
}
static int getyesno(const char *s, const char *yes, const char *no)
{
/* Reverse yes and no so error message looks more normal */
const char *opt[] = { yes, no, NULL };
return 1 - getenum(s, opt);
}
static int cmd_setmstconfid(int argc, char *const *argv)
{
int br_index = get_index(argv[1], "bridge");
if(0 > br_index)
return br_index;
unsigned int revision = getuint(argv[2]);
if(revision > 0xFFFF)
{
fprintf(stderr, "Bad revision %s\n", argv[2]);
return -1;
}
return CTL_set_mstconfid(br_index, revision, (__u8 *)argv[3]);
}
#define set_bridge_cfg(field, value) \
({ \
CIST_BridgeConfig c; \
memset(&c, 0, sizeof(c)); \
c.field = value; \
c.set_ ## field = true; \
int r = CTL_set_cist_bridge_config(br_index, &c); \
if(r) \
printf("Couldn't change bridge " #field "\n"); \
r; \
})
#define set_port_cfg(field, value) \
({ \
CIST_PortConfig c; \
memset(&c, 0, sizeof(c)); \
c.field = value; \
c.set_ ## field = true; \
int r = CTL_set_cist_port_config(br_index, port_index, &c); \
if(r) \
printf("Couldn't change port " #field "\n"); \
r; \
})
#define set_tree_port_cfg(field, value) \
({ \
MSTI_PortConfig c; \
memset(&c, 0, sizeof(c)); \
c.field = value; \
c.set_ ## field = true; \
int r = CTL_set_msti_port_config(br_index, port_index, mstid, &c); \
if(r) \
printf("Couldn't change per-tree port " #field "\n"); \
r; \
})
static int cmd_setbridgemaxage(int argc, char *const *argv)
{
int br_index = get_index(argv[1], "bridge");
if(0 > br_index)
return br_index;
unsigned int max_age = getuint(argv[2]);
if(max_age > 255)
max_age = 255;
return set_bridge_cfg(bridge_max_age, max_age);
}
static int cmd_setbridgehello(int argc, char *const *argv)
{
int br_index = get_index(argv[1], "bridge");
if(0 > br_index)
return br_index;
unsigned int hello_time = getuint(argv[2]);
if(hello_time > 255)
hello_time = 255;