]> git.sur5r.net Git - ngadmin/blob - cli/com_qos.c
Let commands handle themselves absence of arguments
[ngadmin] / cli / com_qos.c
1
2 #include "commands.h"
3
4
5 bool do_qos_mode (int argc, const char **argv, struct ngadmin *nga)
6 {
7         int i, s, ret = true;
8         const struct swi_attr *sa;
9         
10         
11         if (argc == 0) {
12                 printf("usage: qos mode port|802.1p\n");
13                 goto end;
14         }
15         
16         sa = ngadmin_getCurrentSwitch(nga);
17         if (sa == NULL) {
18                 printf("must be logged\n");
19                 ret = false;
20                 goto end;
21         }
22         
23         if (strcasecmp(argv[0], "port") == 0) {
24                 s = QOS_PORT;
25         } else if (strcasecmp(argv[0], "802.1p") == 0) {
26                 s = QOS_DOT;
27         } else {
28                 printf("Unknown QOS mode\n");
29                 ret = false;
30                 goto end;
31         }
32         
33         i = ngadmin_setQOSMode(nga, s);
34         printErrCode(i);
35         
36 end:
37         
38         return ret;
39 }
40
41
42 bool do_qos_set (int argc, const char **argv, struct ngadmin *nga)
43 {
44         int i, p;
45         const struct swi_attr *sa;
46         bool ret = true;
47         char d = PRIO_UNSPEC, *ports = NULL;
48         
49         
50         if (argc < 2) {
51                 printf("usage: qos set (all <prio0>)|(<port1> <prio1> [<port2> <prio2> ...])\n");
52                 ret = false;
53                 goto end;
54         }
55         
56         sa = ngadmin_getCurrentSwitch(nga);
57         if (sa ==NULL) {
58                 printf("must be logged\n");
59                 ret = false;
60                 goto end;
61         }
62         
63         ports = malloc(sa->ports * sizeof(char));
64         
65         /* read defaults */
66         if (strcmp(argv[0], "all") == 0) {
67                 d = parsePrio(argv[1]);
68                 argv += 2;
69                 argc -= 2;
70         }
71         
72         /* apply defaults */
73         for (i = 0; i < sa->ports; i++)
74                 ports[i] = d;
75         
76         /* read and apply port specifics */
77         for (i = 0; i < argc; i += 2) {
78                 p = strtol(argv[i], NULL, 0);
79                 if (p < 1 || p > sa->ports)
80                         continue;
81                 ports[p - 1] = parsePrio(argv[i + 1]);
82         }
83         
84         /* send the new configuration to the switch */
85         i = ngadmin_setQOSValues(nga, ports);
86         printErrCode(i);
87         
88 end:
89         free(ports);
90         
91         return ret;
92 }
93
94
95 bool do_qos_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
96 {
97         int i, s = 0, ret = true;
98         const struct swi_attr *sa;
99         char *ports = NULL;
100         
101         
102         if (argc > 0) {
103                 printf("this command takes no argument\n");
104                 ret = false;
105                 goto end;
106         }
107         
108         sa = ngadmin_getCurrentSwitch(nga);
109         if (sa == NULL) {
110                 printf("must be logged\n");
111                 ret = false;
112                 goto end;
113         }
114         
115         i = ngadmin_getQOSMode(nga, &s);
116         if (i != ERR_OK) {
117                 printErrCode(i);
118                 ret = false;
119                 goto end;
120         }
121         
122         printf("QoS mode: ");
123         switch (s) {
124         
125         case QOS_DOT:
126                 printf("802.1p\n");
127                 goto end;
128         
129         case QOS_PORT:
130                 printf("port based\n");
131                 break;
132                 
133         default:
134                 printf("unknown (%i)\n", s);
135                 goto end;
136         }
137         
138         ports = malloc(sa->ports * sizeof(char));
139         i = ngadmin_getQOSValues(nga, ports);
140         if (i != ERR_OK) {
141                 printErrCode(i);
142                 ret = false;
143                 goto end;
144         }
145         
146         for (i = 0; i < sa->ports; i++)
147                 printf("port %i: %s\n", i + 1, prio[(int)ports[i]]);
148         
149 end:
150         free(ports);
151         
152         return ret;
153 }
154
155