7 // =============================================================================
11 // helper function to analyse bitrate speed specifications
12 static int bitrate_analyse (int nb, const char **com, int *ports) {
18 s=parseBitrate(com[i+1]);
19 if ( strcmp(com[i], "inout")==0 ) {
22 } else if ( strcmp(com[i], "in")==0 ) {
24 } else if ( strcmp(com[i], "out")==0 ) {
38 static bool do_bitrate_set (int nb, const char **com, struct ngadmin *nga) {
40 int i, k=0, defs[]={12, 12}, p, *ports=NULL;
41 const struct swi_attr *sa;
46 printf("Usage: bitrate set [all SPEEDSPEC] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n");
47 printf("SPEEDSPEC: [inout <speed>] [in <ispeed>] [out <ospeed>]\n");
52 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
53 printf("must be logged\n");
58 ports=malloc(2*sa->ports*sizeof(int));
60 // get defaults if present
61 if ( strcmp(com[k], "all")==0 ) {
63 k+=bitrate_analyse(nb-k, &com[k], defs);
67 for (i=0; i<sa->ports; ++i) {
68 memcpy(&ports[2*i], defs, sizeof(defs));
71 // get ports specifics
73 p=strtol(com[k++], NULL, 0)-1;
74 if ( p>=0 && p<sa->ports ) {
75 k+=bitrate_analyse(nb-k, &com[k], &ports[2*p]);
79 // send it to the switch
80 i=ngadmin_setBitrateLimits(nga, ports);
93 static bool do_bitrate_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
95 int i, ret=true, *ports=NULL;
96 const struct swi_attr *sa;
99 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
100 printf("must be logged\n");
106 ports=malloc(2*sa->ports*sizeof(int));
107 if ( (i=ngadmin_getBitrateLimits(nga, ports))!=ERR_OK ) {
113 for (i=0; i<sa->ports; ++i) {
114 printf("port %i: in %s, out %s\n", i+1, bitrates[ports[2*i+0]], bitrates[ports[2*i+1]]);
126 // =============================================================================
130 static bool do_defaults (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
133 const struct swi_attr *sa;
137 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
138 printf("must be logged\n");
144 printf("The switch settings will be CLEARED. Continue ? [y/N]: ");
147 if ( fgets(line, sizeof(line), stdin)!=NULL && strcasecmp(line, "y\n")==0 ) {
148 i=ngadmin_defaults(nga);
161 // =============================================================================
165 static bool do_firmware_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
167 const struct swi_attr *sa;
171 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
172 printf("must be logged\n");
188 static bool do_firmware_upgrade (int nb, const char **com UNUSED, struct ngadmin *nga) {
190 const struct swi_attr *sa;
195 printf("Usage: firmware upgrade <file>\n");
199 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
200 printf("must be logged\n");
205 printf("not implemented yet\n");
216 // =============================================================================
220 static bool do_list (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
223 const struct swi_attr *sa;
226 sa=ngadmin_getSwitchTab(nga, &n);
227 displaySwitchTab(sa, n);
236 // =============================================================================
240 static bool do_login (int nb, const char **com, struct ngadmin *nga) {
246 printf("Usage: login <num>\n");
251 i=strtol(com[0], NULL, 0);
252 i=ngadmin_login(nga, i);
262 // =============================================================================
266 static bool do_name_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
268 const struct swi_attr *sa;
271 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
272 printf("must be logged\n");
285 static bool do_name_set (int nb, const char **com, struct ngadmin *nga) {
288 const struct swi_attr *sa;
292 printf("Usage: name set <value>\n");
296 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
297 printf("must be logged\n");
301 i=ngadmin_setName(nga, com[0]);
311 static bool do_name_clear (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
314 const struct swi_attr *sa;
317 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
318 printf("must be logged\n");
322 i=ngadmin_setName(nga, NULL);
332 // =============================================================================
336 static bool do_password_change (int nb, const char **com, struct ngadmin *nga) {
339 const struct swi_attr *sa;
343 printf("Usage: password set <value>\n");
347 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
348 printf("must be logged\n");
352 i=ngadmin_changePassword(nga, com[0]);
362 static bool do_password_set (int nb, const char **com, struct ngadmin *nga) {
368 printf("Usage: password set <value>\n");
372 i=ngadmin_setPassword(nga, com[0]);
382 // =============================================================================
386 static bool do_ports_state (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
389 const struct swi_attr *sa;
390 unsigned char *ports=NULL;
394 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
395 printf("must be logged\n");
401 ports=malloc(sa->ports*sizeof(unsigned char));
402 if ( (i=ngadmin_getPortsStatus(nga, ports))<0 ) {
408 for (i=0; i<sa->ports; i++) {
409 printf("port %i: ", i+1);
410 switch ( ports[i] ) {
411 case 0: printf("down"); break;
412 case SPEED_10: printf("up, 10M"); break;
413 case SPEED_100: printf("up, 100M"); break;
414 case SPEED_1000: printf("up, 1000M"); break;
415 default: printf("unknown (%i)", ports[i]);
430 static bool do_ports_statistics_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
435 if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
436 printf("must be logged\n");
440 i=ngadmin_resetPortsStatistics(nga);
450 static bool do_ports_statistics_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
453 const struct swi_attr *sa;
455 struct port_stats *ps=NULL;
458 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
459 printf("must be logged\n");
464 ps=calloc(sa->ports, sizeof(struct port_stats));
465 if ( (i=ngadmin_getPortsStatistics(nga, ps))<0 ) {
471 printf("Port\tReceived\tSent\tCRC errors\n");
472 for (i=0; i<sa->ports; ++i) {
473 printf("% 4i%12llu%12llu%14llu\n", i+1, ps[i].recv, ps[i].sent, ps[i].crc);
487 // =============================================================================
492 static bool do_qos_mode (int nb, const char **com, struct ngadmin *nga) {
495 const struct swi_attr *sa;
499 printf("Usage: qos mode port|802.1p\n");
503 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
504 printf("must be logged\n");
510 if ( strcasecmp(com[0], "port")==0 ) {
512 } else if ( strcasecmp(com[0], "802.1p")==0 ) {
515 printf("Unknown QOS mode\n");
521 i=ngadmin_setQOSMode(nga, s);
533 static bool do_qos_set (int nb, const char **com, struct ngadmin *nga) {
536 const struct swi_attr *sa;
538 char d=PRIO_UNSPEC, *ports=NULL;
542 printf("Usage: qos set (all <prio0>)|(<port1> <prio1> [<port2> <prio2> ...])\n");
547 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
548 printf("must be logged\n");
554 ports=malloc(sa->ports*sizeof(char));
556 if ( strcmp(com[0], "all")==0 ) {
562 for (i=0; i<sa->ports; ++i) {
566 for (i=0; i<nb; i+=2) {
567 if ( (p=strtol(com[i], NULL, 0))<1 || p>sa->ports ) continue;
568 ports[p-1]=parsePrio(com[i+1]);
572 i=ngadmin_setQOSValues(nga, ports);
585 static bool do_qos_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
587 int i, s=0, ret=true;
588 const struct swi_attr *sa;
592 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
593 printf("must be logged\n");
598 if ( (i=ngadmin_getQOSMode(nga, &s))!=ERR_OK ) {
605 printf("QOS mode: ");
610 } else if ( s!=QOS_PORT ) {
611 printf("unknown (%i)\n", s);
615 printf("port based\n");
617 ports=malloc(sa->ports*sizeof(char));
619 if ( (i=ngadmin_getQOSValues(nga, ports))!=ERR_OK ) {
625 for (i=0; i<sa->ports; ++i) {
626 printf("port %i: %s\n", i+1, prio[(int)ports[i]]);
639 // =============================================================================
643 static bool do_quit (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
653 // =============================================================================
657 static bool do_restart (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
660 const struct swi_attr *sa;
664 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
665 printf("must be logged\n");
671 printf("The switch will be restarted. Continue ? [y/N]: ");
674 if ( fgets(line, sizeof(line), stdin)!=NULL && strcasecmp(line, "y\n")==0 ) {
675 i=ngadmin_restart(nga);
688 // =============================================================================
692 static bool do_scan (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
695 const struct swi_attr *sa;
698 if ( (i=ngadmin_scan(nga))<0 ) {
703 sa=ngadmin_getSwitchTab(nga, &nb);
704 displaySwitchTab(sa, nb);
713 // =============================================================================
717 static bool do_stormfilter_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
720 const struct swi_attr *sa;
723 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
724 printf("must be logged\n");
728 i=ngadmin_setStormFilterState(nga, 1);
738 static bool do_stormfilter_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
741 const struct swi_attr *sa;
744 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
745 printf("must be logged\n");
749 i=ngadmin_setStormFilterState(nga, 0);
759 static bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga) {
761 int i, d=BITRATE_UNSPEC, p, *ports=NULL;
762 const struct swi_attr *sa;
767 printf("Usage: stormfilt set (all <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\n");
772 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
773 printf("must be logged\n");
779 ports=malloc(sa->ports*sizeof(int));
781 if ( strcmp(com[0], "all")==0 ) {
782 d=parseBitrate(com[1]);
787 for (i=0; i<sa->ports; ++i) {
791 for (i=0; i<nb; i+=2) {
792 if ( (p=strtol(com[i], NULL, 0))<1 || p>sa->ports ) continue;
793 ports[p-1]=parseBitrate(com[i+1]);
797 i=ngadmin_setStormFilterValues(nga, ports);
810 static bool do_stormfilter_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
812 int i, s, ret=true, *ports=NULL;
813 const struct swi_attr *sa;
816 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
817 printf("must be logged\n");
822 if ( (i=ngadmin_getStormFilterState(nga, &s))!=ERR_OK ) {
830 printf("storm filter is disabled\n");
834 printf("storm filter is enabled\n");
836 ports=malloc(sa->ports*sizeof(int));
837 if ( (i=ngadmin_getStormFilterValues(nga, ports))!=ERR_OK ) {
843 for (i=0; i<sa->ports; ++i) {
844 printf("port %i: %s\n", i+1, bitrates[ports[i]]);
856 // =============================================================================
860 static void display_node (const struct TreeNode *tn, int depth) {
863 const struct TreeNode *s;
866 for (i=0; i<depth; ++i) {
872 if ( tn->sub==NULL ) return;
874 for (s=tn->sub; s->name!=NULL; ++s) {
875 display_node(s, depth+1);
882 static bool do_tree (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
885 display_node(&coms, 0);
894 // =============================================================================
900 COM_TERM(set, do_bitrate_set, true)
901 COM_TERM(show, do_bitrate_show, false)
904 COM_TERM(cabletest, NULL, true)
906 COM_TERM(defaults, do_defaults, false)
909 COM_TERM(show, do_firmware_show, false)
910 COM_TERM(upgrade, do_firmware_upgrade, true)
914 COM_TERM(set, NULL, false)
915 COM_TERM(show, NULL, false)
918 COM_TERM(list, do_list, false)
920 COM_TERM(login, do_login, true)
923 COM_TERM(enable, NULL, false)
924 COM_TERM(disable, NULL, false)
925 COM_TERM(set, NULL, true)
926 COM_TERM(show, NULL, false)
930 COM_TERM(show, do_name_show, false)
931 COM_TERM(set, do_name_set, true)
932 COM_TERM(clear, do_name_clear, false)
936 COM_TERM(show, NULL, false)
937 COM_TERM(set, NULL, true)
941 COM_TERM(change, do_password_change, true)
942 COM_TERM(set, do_password_set, true)
946 COM_TERM(state, do_ports_state, false)
947 COM_START(statistics)
948 COM_TERM(reset, do_ports_statistics_reset, false)
949 COM_TERM(show, do_ports_statistics_show, false)
954 COM_TERM(mode, do_qos_mode, true)
955 COM_TERM(set, do_qos_set, true)
956 COM_TERM(show, do_qos_show, false)
959 COM_TERM(quit, do_quit, false)
961 COM_TERM(restart, do_restart, false)
963 COM_TERM(scan, do_scan, false)
965 COM_START(stormfilter)
966 COM_TERM(enable, do_stormfilter_enable, false)
967 COM_TERM(disable, do_stormfilter_disable, false)
968 COM_TERM(set, do_stormfilter_set, true)
969 COM_TERM(show, do_stormfilter_show, false)
972 COM_TERM(tree, do_tree, false)
975 COM_TERM(show, NULL, false)
976 COM_TERM(mode, NULL, true)