]> git.sur5r.net Git - ngadmin/blob - cli/com_vlan.c
Optimize port based VLAN code, send only modified VLANs
[ngadmin] / cli / com_vlan.c
1
2 #include "commands.h"
3
4
5
6 static char vlan_char (int t)
7 {
8         switch (t) {
9         
10         case VLAN_TAGGED:
11                 return 'T';
12         
13         case VLAN_UNTAGGED:
14                 return 'U';
15         
16         case VLAN_NO:
17                 return ' ';
18         
19         default:
20                 return '?';
21         }
22 }
23
24
25 int do_vlan_8021q_del (int argc, const char **argv, struct ngadmin *nga)
26 {
27         const struct swi_attr *sa;
28         unsigned short vlan;
29         int i;
30         
31         
32         if (argc != 1) {
33                 printf("usage: vlan 8021q del <vlan>\n");
34                 return 1;
35         }
36         
37         sa = ngadmin_getCurrentSwitch(nga);
38         if (sa == NULL) {
39                 printf("must be logged\n");
40                 return 1;
41         }
42         
43         vlan=strtoul(argv[0], NULL, 0);
44         if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) {
45                 printf("vlan out of range\n");
46                 return 1;
47         }
48         
49         i = ngadmin_VLANDestroy(nga, vlan);
50         printErrCode(i);
51         
52         
53         return 0;
54 }
55
56
57 int do_vlan_port_set (int argc, const char **argv, struct ngadmin *nga)
58 {
59         unsigned char vlan, port, *ports = NULL;
60         const struct swi_attr *sa;
61         int i, k = 0, ret = 0;
62         
63         
64         if (argc < 2) {
65                 printf("usage: vlan port set [all <vlan>] [<port1> <vlan>] [<port2> <vlan>] [...]\n");
66                 ret = 1;
67                 goto end;
68         }
69         
70         sa = ngadmin_getCurrentSwitch(nga);
71         if (sa == NULL) {
72                 printf("must be logged\n");
73                 ret = 1;
74                 goto end;
75         }
76         
77         ports = malloc(sa->ports * sizeof(unsigned char));
78
79         /* read defaults */
80         port = 0;
81         if (strcmp(argv[k], "all") == 0) {
82                 k++;
83                 port = strtoul(argv[k++], NULL, 0);
84                 if (port < 1 || port > sa->ports) {
85                         printf("port out of range");
86                         ret = 1;
87                         goto end;
88                 }
89         }
90         
91         /* apply defaults */
92         memset(ports, port, sa->ports);
93         
94         /* read and apply port specifics */
95         while (k < argc - 1) {
96                 /* read port */
97                 port = strtoul(argv[k++], NULL, 0);
98                 if (port < 1 || port > sa->ports) {
99                         printf("port out of range");
100                         ret = 1;
101                         goto end;
102                 }
103                 
104                 /* read vlan */
105                 vlan = strtoul(argv[k++], NULL, 0);
106                 if (vlan < VLAN_MIN || vlan > VLAN_PORT_MAX) {
107                         printf("vlan out of range\n");
108                         ret = 1;
109                         goto end;
110                 }
111                 
112                 ports[port - 1] = vlan;
113         }
114         
115         /* set conf */
116         i = ngadmin_setVLANPortConf(nga, ports);
117         printErrCode(i);
118         
119 end:
120         free(ports);
121         
122         return ret;
123 }
124
125
126 int do_vlan_port_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
127 {
128         unsigned char *ports = NULL;
129         const struct swi_attr *sa;
130         int i, ret = 0;
131         
132         
133         if (argc > 0) {
134                 printf("this command takes no argument\n");
135                 ret = 1;
136                 goto end;
137         }
138         
139         sa = ngadmin_getCurrentSwitch(nga);
140         if (sa == NULL) {
141                 printf("must be logged\n");
142                 ret = 1;
143                 goto end;
144         }
145         
146         ports = malloc(sa->ports * sizeof(unsigned char));
147         
148         /* request all VLANs config */
149         i = ngadmin_getVLANPortConf(nga, ports);
150         
151         if (i != ERR_OK) {
152                 printErrCode(i);
153                 ret = 1;
154                 goto end;
155         }
156         
157         printf("Ports configuration: \n");
158         printf("Port\t");
159         for (i = 1; i <= sa->ports; i++)
160                 printf("%i\t", i);
161         putchar('\n');
162         
163         /* show all VLANs */
164         printf("VLAN\t");
165         for (i = 0; i < sa->ports; i++)
166                 printf("%u\t", ports[i]);
167         putchar('\n');
168         
169 end:
170         free(ports);
171         
172         return ret;
173 }
174
175
176 int do_vlan_8021q_set (int argc, const char **argv, struct ngadmin *nga)
177 {
178         unsigned char *ports = NULL, p, def = VLAN_UNSPEC;
179         const struct swi_attr *sa;
180         unsigned short vlan;
181         int i, k = 0, ret = 0;
182         
183         
184         if (argc == 0) {
185                 printf("usage: vlan 802.1q set <vlan> [all unspec|no|untagged|tagged] [<port1> unspec|no|untagged|tagged ...]\n");
186                 ret = 1;
187                 goto end;
188         }
189         
190         sa = ngadmin_getCurrentSwitch(nga);
191         if (sa == NULL) {
192                 printf("must be logged\n");
193                 ret = 1;
194                 goto end;
195         }
196         
197         /* read vlan */
198         vlan = strtoul(argv[k++], NULL, 0);
199         
200         if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) {
201                 printf("vlan out of range\n");
202                 ret = 1;
203                 goto end;
204         }
205         
206         /* read defaults */
207         if (k < argc - 1 && strcasecmp(argv[k], "all") == 0) {
208                 k++;
209                 if (strcasecmp(argv[k], "tagged") == 0) {
210                         def = VLAN_TAGGED;
211                 } else if (strcasecmp(argv[k], "untagged") == 0) {
212                         def = VLAN_UNTAGGED;
213                 } else if (strcasecmp(argv[k], "no") == 0) {
214                         def = VLAN_NO;
215                 } else if (strcasecmp(argv[k], "unspec") == 0) {
216                         def = VLAN_UNSPEC;
217                 } else {
218                         printf("incorrect type\n");
219                         ret = 1;
220                         goto end;
221                 }
222                 k++;
223         }
224         
225         ports = malloc(sa->ports * sizeof(unsigned char));
226         
227         /* apply defaults */
228         memset(ports, def, sa->ports);
229         
230         /* read and apply port specifics */
231         while (k < argc - 1) {
232                 p = strtoul(argv[k++], NULL, 0) - 1;
233                 if (p >= sa->ports) {
234                         printf("port out of range\n");
235                         ret = 1;
236                         goto end;
237                 }
238                 if (strcasecmp(argv[k], "tagged") ==0) {
239                         ports[p] = VLAN_TAGGED;
240                 } else if (strcasecmp(argv[k], "untagged") == 0) {
241                         ports[p] = VLAN_UNTAGGED;
242                 } else if (strcasecmp(argv[k], "no") == 0) {
243                         ports[p] = VLAN_NO;
244                 } else if (strcasecmp(argv[k], "unspec") == 0) {
245                         ports[p] = VLAN_UNSPEC;
246                 } else {
247                         printf("incorrect type\n");
248                         ret = 1;
249                         goto end;
250                 }
251                 k++;
252         }
253         
254         /* set conf */
255         i = ngadmin_setVLANDotConf(nga, vlan, ports);
256         printErrCode(i);
257         
258 end:
259         free(ports);
260         
261         return ret;
262 }
263
264
265 int do_vlan_8021q_show (int argc, const char **argv, struct ngadmin *nga)
266 {
267         unsigned short vl = 0, *vlans = NULL;
268         unsigned char *ports = NULL;
269         const struct swi_attr *sa;
270         int i, j, n = 16, ret = 0;
271         
272         
273         sa = ngadmin_getCurrentSwitch(nga);
274         if (sa == NULL) {
275                 printf("must be logged\n");
276                 ret = 1;
277                 goto end;
278         }
279         
280         if (argc > 0)
281                 vl = strtoul(argv[0], NULL, 0);
282         
283         ports = malloc(sa->ports * n * sizeof(unsigned char));
284         
285         if (vl == 0) {
286                 /* request all VLANs config */
287                 vlans = malloc(n * sizeof(unsigned short));
288                 ports = malloc(sa->ports * n * sizeof(unsigned char));
289                 i = ngadmin_getVLANDotAllConf(nga, vlans, ports, &n);
290         } else {
291                 /* request single VLAN config */
292                 ports = malloc(sa->ports * sizeof(unsigned char));
293                 i = ngadmin_getVLANDotConf(nga, vl, ports);
294         }
295         
296         if (i != ERR_OK) {
297                 printErrCode(i);
298                 ret = 1;
299                 goto end;
300         }
301         
302         printf("Ports configuration: \n");
303         printf("VLAN\t");
304         for (i = 1; i <= sa->ports; i++)
305                 printf("%i\t", i);
306         putchar('\n');
307         
308         if (vl == 0) {
309                 /* show all VLANs */
310                 for (i = 0; i < n; i++) {
311                         printf("%u\t", vlans[i]);
312                         for (j = 0; j < sa->ports; j++)
313                                 printf("%c\t", vlan_char(ports[i * sa->ports + j]));
314                         putchar('\n');
315                 }
316         } else {
317                 /* show single VLAN config */
318                 printf("%u\t", vl);
319                 for (j = 0; j < sa->ports; j++)
320                         printf("%c\t", vlan_char(ports[j]));
321                 putchar('\n');
322         }
323         
324 end:
325         free(vlans);
326         free(ports);
327         
328         return ret;
329 }
330
331
332 int do_vlan_mode_set (int argc, const char **argv, struct ngadmin *nga)
333 {
334         int mode, i;
335         
336         
337         if (argc == 0) {
338                 printf(
339                 "usage: vlan mode set <mode>\n"
340                 "1 - basic port based\n"
341                 "2 - advanced port based\n"
342                 "3 - basic 802.1Q\n"
343                 "4 - advanced 802.1Q\n"
344                 );
345                 return 0;
346         }
347         
348         if (ngadmin_getCurrentSwitch(nga) == NULL) {
349                 printf("must be logged\n");
350                 return 1;
351         }
352         
353         mode = strtoul(argv[0], NULL, 0);
354         if (mode < 1 || mode > 4) {
355                 printf("mode out of range\n");
356                 return 1;
357         }
358         
359         i = ngadmin_setVLANType(nga, mode);
360         printErrCode(i);
361         
362         
363         return 0;
364 }
365
366
367 int do_vlan_mode_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
368 {
369         int i, t, ret = 0;
370         
371         
372         if (argc > 0) {
373                 printf("this command takes no argument\n");
374                 ret = 1;
375                 goto end;
376         }
377         
378         if (ngadmin_getCurrentSwitch(nga) == NULL) {
379                 printf("must be logged\n");
380                 ret = 1;
381                 goto end;
382         }
383         
384         i = ngadmin_getVLANType(nga, &t);
385         if (i != ERR_OK) {
386                 printErrCode(i);
387                 ret = 1;
388                 goto end;
389         }
390         
391         printf("VLAN type: ");
392         switch (t) {
393         
394         case VLAN_DISABLED:
395                 printf("disabled\n");
396                 break;
397         
398         case VLAN_PORT_BASIC:
399                 printf("port basic\n");
400                 break;
401         
402         case VLAN_PORT_ADV:
403                 printf("port advanced\n");
404                 break;
405         
406         case VLAN_DOT_BASIC:
407                 printf("802.1Q basic\n");
408                 break;
409         
410         case VLAN_DOT_ADV:
411                 printf("802.1Q advanced\n");
412                 break;
413         
414         default:
415                 printf("unknown (%i)\n", t);
416         }
417         
418 end:
419         
420         return ret;
421 }
422
423
424 int do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga)
425 {
426         const struct swi_attr *sa;
427         unsigned char port;
428         unsigned short vlan;
429         int i;
430         
431         
432         if (argc != 2) {
433                 printf("usage: vlan pvid set <port> <vlan>\n");
434                 return 1;
435         }
436         
437         sa = ngadmin_getCurrentSwitch(nga);
438         if (sa == NULL) {
439                 printf("must be logged\n");
440                 return 1;
441         }
442         
443         port = strtoul(argv[0], NULL, 0);
444         vlan = strtoul(argv[1], NULL, 0);
445         
446         if (port < 1 || port > sa->ports) {
447                 printf("port out of range\n");
448                 return 1;
449         }
450         
451         if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) {
452                 printf("vlan out of range\n");
453                 return 1;
454         }
455         
456         i = ngadmin_setPVID(nga, port, vlan);
457         printErrCode(i);
458         
459         
460         return 0;
461 }
462
463
464 int do_vlan_pvid_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
465 {
466         unsigned short *ports = NULL;
467         const struct swi_attr *sa;
468         int i, ret = 0;
469         
470         
471         if (argc > 0) {
472                 printf("this command takes no argument\n");
473                 ret = 1;
474                 goto end;
475         }
476         
477         sa = ngadmin_getCurrentSwitch(nga);
478         if (sa == NULL) {
479                 printf("must be logged\n");
480                 ret = 1;
481                 goto end;
482         }
483         
484         ports = malloc(sa->ports * sizeof(unsigned short));
485         i = ngadmin_getAllPVID(nga, ports);
486         if (i != ERR_OK) {
487                 printErrCode(i);
488                 ret = 1;
489                 goto end;
490         }
491         
492         printf("Port\t");
493         for (i = 1; i <= sa->ports; i++)
494                 printf("%i\t", i);
495         putchar('\n');
496         
497         printf("VLAN\t");
498         for (i = 0; i < sa->ports; i++)
499                 printf("%u\t", ports[i]);
500         putchar('\n');
501         
502 end:
503         free(ports);
504         
505         return ret;
506 }
507
508