]> git.sur5r.net Git - ngadmin/blob - cli/com_ports.c
Code reorganized.
[ngadmin] / cli / com_ports.c
1
2 #include "common.h"
3
4
5
6
7 static bool do_ports_state (const struct TreeNode *tn UNUSED, int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
8  
9  int i;
10  const struct swi_attr *sa;
11  unsigned char *ports=NULL;
12  bool ret=true;
13  
14  
15  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
16   printf("must be logged\n");
17   ret=false;
18   goto end;
19  }
20  
21  
22  ports=malloc(sa->ports*sizeof(unsigned char));
23  if ( (i=ngadmin_getPortsStatus(nga, ports))<0 ) {
24   printErrCode(i);
25   ret=false;
26   goto end;
27  }
28  
29  for (i=0; i<sa->ports; i++) {
30   printf("port %i: ", i+1);
31   switch ( ports[i] ) {
32    case 0: printf("down"); break;
33    case SPEED_10: printf("up, 10M"); break;
34    case SPEED_100: printf("up, 100M"); break;
35    case SPEED_1000: printf("up, 1000M"); break;
36    default: printf("unknown (%i)", ports[i]);
37   }
38   putchar('\n');
39  }
40  
41  end:
42  free(ports);
43  
44  
45  return ret;
46  
47 }
48
49
50
51 static bool do_ports_stats_show (const struct TreeNode *tn UNUSED, int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
52  
53  int i;
54  const struct swi_attr *sa;
55  bool ret=true;
56  struct port_stats *ps=NULL;
57  
58  
59  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
60   printf("must be logged\n");
61   ret=false;
62   goto end;
63  }
64   
65  ps=calloc(sa->ports, sizeof(struct port_stats));
66  if ( (i=ngadmin_getPortsStatistics(nga, ps))<0 ) {
67   printErrCode(i);
68   ret=false;
69   goto end;
70  }
71  
72  printf("Port\tReceived\tSent\t\tCRC errors\n");
73  for (i=0; i<sa->ports; ++i) {
74   printf("%i\t%8llu\t%8llu\t%8llu\n", i+1, ps[i].recv, ps[i].sent, ps[i].crc);
75  }
76  
77  end:
78  free(ps);
79  
80  
81  return ret;
82  
83 }
84
85
86
87 static bool do_ports_stats_reset (const struct TreeNode *tn UNUSED, int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
88  
89  int i;
90  
91  
92  if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
93   printf("must be logged\n");
94   return false;
95  }
96  
97  i=ngadmin_resetPortsStatistics(nga);
98  printErrCode(i);
99  
100  
101  return true;
102  
103 }
104
105
106
107 static const struct TreeNode com_ports_state=COM("state", do_ports_state, false, NULL);
108
109 static const struct TreeNode com_ports_stats_show=COM("show", do_ports_stats_show, false, NULL);
110 static const struct TreeNode com_ports_stats_reset=COM("reset", do_ports_stats_reset, false, NULL);
111 static const struct TreeNode com_ports_stats=COM("stats", NULL, false, &com_ports_stats_reset, &com_ports_stats_show, NULL);
112
113 const struct TreeNode com_ports=COM("ports", NULL, false, &com_ports_state, &com_ports_stats, NULL);
114
115
116