]> git.sur5r.net Git - ngadmin/blob - cli/commands.c
Added support for clearing the switch settings to the defaults.
[ngadmin] / cli / commands.c
1
2 #include "commands.h"
3
4
5
6
7 // =============================================================================
8 // bitrate
9
10
11 // helper function to analyse bitrate speed specifications
12 static int bitrate_analyse (int nb, const char **com, int *ports) {
13  
14  int i=0, s;
15  
16  
17  while ( i<nb-1 ) {
18   s=parseBitrate(com[i+1]);
19   if ( strcmp(com[i], "inout")==0 ) {
20    ports[0]=s;
21    ports[1]=s;
22   } else if ( strcmp(com[i], "in")==0 ) {
23    ports[0]=s;
24   } else if ( strcmp(com[i], "out")==0 ) {
25    ports[1]=s;
26   } else {
27    break;
28   }
29   i+=2;
30  }
31  
32  
33  return i;
34  
35 }
36
37
38 static bool do_bitrate_set (int nb, const char **com, struct ngadmin *nga) {
39  
40  int i, k=0, defs[]={12, 12}, p, *ports=NULL;
41  const struct swi_attr *sa;
42  bool ret=true;
43  
44  
45  if ( nb<2 ) {
46   printf("Usage: bitrate set [all SPEEDSPEC] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n");
47   printf("SPEEDSPEC: [inout <speed>] [in <ispeed>] [out <ospeed>]\n");
48   ret=false;
49   goto end;
50  }
51  
52  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
53   printf("must be logged\n");
54   ret=false;
55   goto end;
56  }
57  
58  ports=malloc(2*sa->ports*sizeof(int));
59  
60  // get defaults if present
61  if ( strcmp(com[k], "all")==0 ) {
62   ++k;
63   k+=bitrate_analyse(nb-k, &com[k], defs);
64  }
65  
66  // apply defaults
67  for (i=0; i<sa->ports; ++i) {
68   memcpy(&ports[2*i], defs, sizeof(defs));
69  }
70  
71  // get ports specifics
72  while ( k<nb ) {
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]);
76   }
77  }
78  
79  // send it to the switch
80  i=ngadmin_setBitrateLimits(nga, ports);
81  printErrCode(i);
82  
83  
84  end:
85  free(ports);
86  
87  return ret;
88  
89 }
90
91
92
93 static bool do_bitrate_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
94  
95  int i, ret=true, *ports=NULL;
96  const struct swi_attr *sa;
97  
98  
99  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
100   printf("must be logged\n");
101   ret=false;
102   goto end;
103  }
104  
105  
106  ports=malloc(2*sa->ports*sizeof(int));
107  if ( (i=ngadmin_getBitrateLimits(nga, ports))!=ERR_OK ) {
108   printErrCode(i);
109   ret=false;
110   goto end;
111  }
112  
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]]);
115  }
116  
117  end:
118  free(ports);
119  
120  return ret;
121  
122 }
123
124
125
126 // =============================================================================
127 // defaults
128
129
130 static bool do_defaults (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
131  
132  int i, ret=true;
133  const struct swi_attr *sa;
134  char line[16];
135  
136  
137  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
138   printf("must be logged\n");
139   ret=false;
140   goto end;
141  }
142  
143  
144  printf("The switch settings will be CLEARED. Continue ? [y/N]: ");
145  fflush(stdout);
146  
147  if ( fgets(line, sizeof(line), stdin)!=NULL && strcasecmp(line, "y\n")==0 ) {
148   i=ngadmin_defaults(nga);
149   printErrCode(i);
150  }
151  
152  
153  end:
154  
155  return ret;
156  
157 }
158
159
160
161 // =============================================================================
162 // firmware
163
164
165 static bool do_firmware_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
166  
167  const struct swi_attr *sa;
168  bool ret=true;
169  
170  
171  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
172   printf("must be logged\n");
173   ret=false;
174   goto end;
175  }
176  
177  puts(sa->firmware);
178  
179  
180  end:
181  
182  return ret;
183  
184 }
185
186
187
188 static bool do_firmware_upgrade (int nb, const char **com UNUSED, struct ngadmin *nga) {
189  
190  const struct swi_attr *sa;
191  bool ret=true;
192  
193  
194  if ( nb!=1 ) {
195   printf("Usage: firmware upgrade <file>\n");
196   ret=false;
197  }
198  
199  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
200   printf("must be logged\n");
201   ret=false;
202   goto end;
203  }
204  
205  printf("not implemented yet\n");
206  
207  
208  end:
209  
210  return ret;
211  
212 }
213
214
215
216 // =============================================================================
217 // list
218
219
220 static bool do_list (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
221  
222  int n;
223  const struct swi_attr *sa;
224  
225  
226  sa=ngadmin_getSwitchTab(nga, &n);
227  displaySwitchTab(sa, n);
228  
229  
230  return true;
231  
232 }
233
234
235
236 // =============================================================================
237 // login
238
239
240 static bool do_login (int nb, const char **com, struct ngadmin *nga) {
241  
242  int i;
243  
244  
245  if ( nb!=1 ) {
246   printf("Usage: login <num>\n");
247   return false;
248  }
249  
250  
251  i=strtol(com[0], NULL, 0);
252  i=ngadmin_login(nga, i);
253  printErrCode(i);
254  
255  
256  return true;
257  
258 }
259
260
261
262 // =============================================================================
263 // name
264
265
266 static bool do_name_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
267  
268  const struct swi_attr *sa;
269  
270  
271  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
272   printf("must be logged\n");
273   return false;
274  }
275  
276  puts(sa->name);
277  
278  
279  return true;
280  
281 }
282
283
284
285 static bool do_name_set (int nb, const char **com, struct ngadmin *nga) {
286  
287  int i;
288  const struct swi_attr *sa;
289  
290  
291  if ( nb!=1 ) {
292   printf("Usage: name set <value>\n");
293   return false;
294  }
295  
296  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
297   printf("must be logged\n");
298   return false;
299  }
300  
301  i=ngadmin_setName(nga, com[0]);
302  printErrCode(i);
303  
304  
305  return true;
306  
307 }
308
309
310
311 static bool do_name_clear (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
312  
313  int i;
314  const struct swi_attr *sa;
315  
316  
317  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
318   printf("must be logged\n");
319   return false;
320  }
321  
322  i=ngadmin_setName(nga, NULL);
323  printErrCode(i);
324  
325  
326  return true;
327  
328 }
329
330
331
332 // =============================================================================
333 // password
334
335
336 static bool do_password_change (int nb, const char **com, struct ngadmin *nga) {
337  
338  int i;
339  const struct swi_attr *sa;
340  
341  
342  if ( nb!=1 ) {
343   printf("Usage: password set <value>\n");
344   return false;
345  }
346  
347  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
348   printf("must be logged\n");
349   return false;
350  }
351  
352  i=ngadmin_changePassword(nga, com[0]);
353  printErrCode(i);
354  
355  
356  return true;
357  
358 }
359
360
361
362 static bool do_password_set (int nb, const char **com, struct ngadmin *nga) {
363  
364  int i;
365  
366  
367  if ( nb!=1 ) {
368   printf("Usage: password set <value>\n");
369   return false;
370  }
371  
372  i=ngadmin_setPassword(nga, com[0]);
373  printErrCode(i);
374  
375  
376  return true;
377  
378 }
379
380
381
382 // =============================================================================
383 // ports
384
385
386 static bool do_ports_state (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
387  
388  int i;
389  const struct swi_attr *sa;
390  unsigned char *ports=NULL;
391  bool ret=true;
392  
393  
394  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
395   printf("must be logged\n");
396   ret=false;
397   goto end;
398  }
399  
400  
401  ports=malloc(sa->ports*sizeof(unsigned char));
402  if ( (i=ngadmin_getPortsStatus(nga, ports))<0 ) {
403   printErrCode(i);
404   ret=false;
405   goto end;
406  }
407  
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]);
416   }
417   putchar('\n');
418  }
419  
420  end:
421  free(ports);
422  
423  
424  return ret;
425  
426 }
427
428
429
430 static bool do_ports_statistics_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
431  
432  int i;
433  
434  
435  if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
436   printf("must be logged\n");
437   return false;
438  }
439  
440  i=ngadmin_resetPortsStatistics(nga);
441  printErrCode(i);
442  
443  
444  return true;
445  
446 }
447
448
449
450 static bool do_ports_statistics_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
451  
452  int i;
453  const struct swi_attr *sa;
454  bool ret=true;
455  struct port_stats *ps=NULL;
456  
457  
458  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
459   printf("must be logged\n");
460   ret=false;
461   goto end;
462  }
463  
464  ps=calloc(sa->ports, sizeof(struct port_stats));
465  if ( (i=ngadmin_getPortsStatistics(nga, ps))<0 ) {
466   printErrCode(i);
467   ret=false;
468   goto end;
469  }
470  
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);
474  }
475  
476  end:
477  free(ps);
478  
479  
480  return ret;
481  
482 }
483
484
485
486
487 // =============================================================================
488 // qos
489
490
491
492 static bool do_qos_mode (int nb, const char **com, struct ngadmin *nga) {
493  
494  int i, s, ret=true;
495  const struct swi_attr *sa;
496  
497  
498  if ( nb==0 ) {
499   printf("Usage: qos mode port|802.1p\n");
500   goto end;
501  }
502  
503  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
504   printf("must be logged\n");
505   ret=false;
506   goto end;
507  }
508  
509  
510  if ( strcasecmp(com[0], "port")==0 ) {
511   s=QOS_PORT;
512  } else if ( strcasecmp(com[0], "802.1p")==0 ) {
513   s=QOS_DOT;
514  } else {
515   printf("Unknown QOS mode\n");
516   ret=false;
517   goto end;
518  }
519  
520  
521  i=ngadmin_setQOSMode(nga, s);
522  printErrCode(i);
523  
524  
525  end:
526  
527  return ret;
528  
529 }
530
531
532
533 static bool do_qos_set (int nb, const char **com, struct ngadmin *nga) {
534  
535  int i, p;
536  const struct swi_attr *sa;
537  bool ret=true;
538  char d=PRIO_UNSPEC, *ports=NULL;
539  
540  
541  if ( nb<2 ) {
542   printf("Usage: qos set (all <prio0>)|(<port1> <prio1> [<port2> <prio2> ...])\n");
543   ret=false;
544   goto end;
545  }
546  
547  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
548   printf("must be logged\n");
549   ret=false;
550   goto end;
551  }
552  
553  
554  ports=malloc(sa->ports*sizeof(char));
555  
556  if ( strcmp(com[0], "all")==0 ) {
557   d=parsePrio(com[1]);
558   com+=2;
559   nb-=2;
560  }
561  
562  for (i=0; i<sa->ports; ++i) {
563   ports[i]=d;
564  }
565  
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]);
569  }
570  
571  
572  i=ngadmin_setQOSValues(nga, ports);
573  printErrCode(i);
574  
575  
576  end:
577  free(ports);
578  
579  return ret;
580  
581 }
582
583
584
585 static bool do_qos_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
586  
587  int i, s=0, ret=true;
588  const struct swi_attr *sa;
589  char *ports=NULL;
590  
591  
592  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
593   printf("must be logged\n");
594   ret=false;
595   goto end;
596  }
597  
598  if ( (i=ngadmin_getQOSMode(nga, &s))!=ERR_OK ) {
599   printErrCode(i);
600   ret=false;
601   goto end;
602  }
603  
604  
605  printf("QOS mode: ");
606  
607  if ( s==QOS_DOT ) {
608   printf("802.1p\n");
609   goto end;
610  } else if ( s!=QOS_PORT ) {
611   printf("unknown (%i)\n", s);
612   goto end;
613  }
614  
615  printf("port based\n");
616  
617  ports=malloc(sa->ports*sizeof(char));
618  
619  if ( (i=ngadmin_getQOSValues(nga, ports))!=ERR_OK ) {
620   printErrCode(i);
621   ret=false;
622   goto end;
623  }
624  
625  for (i=0; i<sa->ports; ++i) {
626   printf("port %i: %s\n", i+1, prio[(int)ports[i]]);
627  }
628  
629  
630  end:
631  free(ports);
632  
633  return ret;
634  
635 }
636
637
638
639 // =============================================================================
640 // quit
641
642
643 static bool do_quit (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
644  
645  cont=0;
646  
647  return true;
648  
649 }
650
651
652
653 // =============================================================================
654 // restart
655
656
657 static bool do_restart (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
658  
659  int i, ret=true;
660  const struct swi_attr *sa;
661  char line[16];
662  
663  
664  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
665   printf("must be logged\n");
666   ret=false;
667   goto end;
668  }
669  
670  
671  printf("The switch will be restarted. Continue ? [y/N]: ");
672  fflush(stdout);
673  
674  if ( fgets(line, sizeof(line), stdin)!=NULL && strcasecmp(line, "y\n")==0 ) {
675   i=ngadmin_restart(nga);
676   printErrCode(i);
677  }
678  
679  
680  end:
681  
682  return ret;
683  
684 }
685
686
687
688 // =============================================================================
689 // scan
690
691
692 static bool do_scan (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
693  
694  int i;
695  const struct swi_attr *sa;
696  
697  
698  if ( (i=ngadmin_scan(nga))<0 ) {
699   printErrCode(i);
700   return false;
701  }
702  
703  sa=ngadmin_getSwitchTab(nga, &nb);
704  displaySwitchTab(sa, nb);
705  
706  
707  return true;
708  
709 }
710
711
712
713 // =============================================================================
714 // stormfilter
715
716
717 static bool do_stormfilter_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
718  
719  int i;
720  const struct swi_attr *sa;
721  
722  
723  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
724   printf("must be logged\n");
725   return false;
726  }
727  
728  i=ngadmin_setStormFilterState(nga, 1);
729  printErrCode(i);
730  
731  
732  return true;
733  
734 }
735
736
737
738 static bool do_stormfilter_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
739  
740  int i;
741  const struct swi_attr *sa;
742  
743  
744  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
745   printf("must be logged\n");
746   return false;
747  }
748  
749  i=ngadmin_setStormFilterState(nga, 0);
750  printErrCode(i);
751  
752  
753  return true;
754  
755 }
756
757
758
759 static bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga) {
760  
761  int i, d=BITRATE_UNSPEC, p, *ports=NULL;
762  const struct swi_attr *sa;
763  bool ret=true;
764  
765  
766  if ( nb<2 ) {
767   printf("Usage: stormfilt set (all <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\n");
768   ret=false;
769   goto end;
770  }
771  
772  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
773   printf("must be logged\n");
774   ret=false;
775   goto end;
776  }
777  
778  
779  ports=malloc(sa->ports*sizeof(int));
780  
781  if ( strcmp(com[0], "all")==0 ) {
782   d=parseBitrate(com[1]);
783   com+=2;
784   nb-=2;
785  }
786  
787  for (i=0; i<sa->ports; ++i) {
788   ports[i]=d;
789  }
790  
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]);
794  }
795  
796  
797  i=ngadmin_setStormFilterValues(nga, ports);
798  printErrCode(i);
799  
800  
801  end:
802  free(ports);
803  
804  return ret;
805  
806 }
807
808
809
810 static bool do_stormfilter_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
811  
812  int i, s, ret=true, *ports=NULL;
813  const struct swi_attr *sa;
814  
815  
816  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
817   printf("must be logged\n");
818   ret=false;
819   goto end;
820  }
821  
822  if ( (i=ngadmin_getStormFilterState(nga, &s))!=ERR_OK ) {
823   printErrCode(i);
824   ret=false;
825   goto end;
826  }
827  
828  
829  if ( !s ) {
830   printf("storm filter is disabled\n");
831   goto end;
832  }
833  
834  printf("storm filter is enabled\n");
835  
836  ports=malloc(sa->ports*sizeof(int));
837  if ( (i=ngadmin_getStormFilterValues(nga, ports))!=ERR_OK ) {
838   printErrCode(i);
839   ret=false;
840   goto end;
841  }
842  
843  for (i=0; i<sa->ports; ++i) {
844   printf("port %i: %s\n", i+1, bitrates[ports[i]]);
845  }
846  
847  end:
848  free(ports);
849  
850  return ret;
851  
852 }
853
854
855
856 // =============================================================================
857 // tree
858
859
860 static void display_node (const struct TreeNode *tn, int depth) {
861  
862  int i;
863  const struct TreeNode *s;
864  
865  
866  for (i=0; i<depth; ++i) {
867   putchar('\t');
868  }
869  
870  puts(tn->name);
871  
872  if ( tn->sub==NULL ) return;
873  
874  for (s=tn->sub; s->name!=NULL; ++s) {
875   display_node(s, depth+1);
876  }
877  
878  
879 }
880
881
882 static bool do_tree (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
883  
884  
885  display_node(&coms, 0);
886  
887  
888  return true;
889  
890 }
891
892
893
894 // =============================================================================
895
896
897 COM_ROOT_START(coms)
898  
899  COM_START(bitrate)
900   COM_TERM(set, do_bitrate_set, true)
901   COM_TERM(show, do_bitrate_show, false)
902  COM_END
903  
904  COM_TERM(cabletest, NULL, true)
905  
906  COM_TERM(defaults, do_defaults, false)
907  
908  COM_START(firmware)
909   COM_TERM(show, do_firmware_show, false)
910   COM_TERM(upgrade, do_firmware_upgrade, true)
911  COM_END
912  
913  COM_START(igmp)
914   COM_TERM(set, NULL, false)
915   COM_TERM(show, NULL, false)
916  COM_END
917  
918  COM_TERM(list, do_list, false)
919  
920  COM_TERM(login, do_login, true)
921  
922  COM_START(mirror)
923   COM_TERM(enable, NULL, false)
924   COM_TERM(disable, NULL, false)
925   COM_TERM(set, NULL, true)
926   COM_TERM(show, NULL, false)
927  COM_END
928  
929  COM_START(name)
930   COM_TERM(show, do_name_show, false)
931   COM_TERM(set, do_name_set, true)
932   COM_TERM(clear, do_name_clear, false)
933  COM_END
934  
935  COM_START(netconf)
936   COM_TERM(show, NULL, false)
937   COM_TERM(set, NULL, true)
938  COM_END
939  
940  COM_START(password)
941   COM_TERM(change, do_password_change, true)
942   COM_TERM(set, do_password_set, true)
943  COM_END
944  
945  COM_START(ports)
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)
950   COM_END
951  COM_END
952  
953  COM_START(qos)
954   COM_TERM(mode, do_qos_mode, true)
955   COM_TERM(set, do_qos_set, true)
956   COM_TERM(show, do_qos_show, false)
957  COM_END
958  
959  COM_TERM(quit, do_quit, false)
960  
961  COM_TERM(restart, do_restart, false)
962  
963  COM_TERM(scan, do_scan, false)
964  
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)
970  COM_END
971  
972  COM_TERM(tree, do_tree, false)
973  
974  COM_START(vlan)
975   COM_TERM(show, NULL, false)
976   COM_TERM(mode, NULL, true)
977  COM_END
978  
979 COM_ROOT_END
980
981
982