]> git.sur5r.net Git - ngadmin/blob - cli/com_qos.c
Cli: refactor, change coding style
[ngadmin] / cli / com_qos.c
1
2 #include "commands.h"
3
4
5 bool do_qos_mode (int nb, const char **com, struct ngadmin *nga)
6 {
7         int i, s, ret = true;
8         const struct swi_attr *sa;
9         
10         
11         if (nb == 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(com[0], "port") == 0) {
24                 s = QOS_PORT;
25         } else if (strcasecmp(com[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 nb, const char **com, 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 (nb < 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         if (strcmp(com[0], "all") == 0) {
66                 d = parsePrio(com[1]);
67                 com += 2;
68                 nb -= 2;
69         }
70         
71         for (i = 0; i < sa->ports; i++)
72                 ports[i] = d;
73         
74         for (i = 0; i < nb; i += 2) {
75                 p = strtol(com[i], NULL, 0);
76                 if (p < 1 || p > sa->ports)
77                         continue;
78                 ports[p - 1] = parsePrio(com[i + 1]);
79         }
80         
81         i = ngadmin_setQOSValues(nga, ports);
82         printErrCode(i);
83         
84 end:
85         free(ports);
86         
87         return ret;
88 }
89
90
91 bool do_qos_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
92 {
93         int i, s = 0, ret = true;
94         const struct swi_attr *sa;
95         char *ports = NULL;
96         
97         
98         sa = ngadmin_getCurrentSwitch(nga);
99         if (sa == NULL) {
100                 printf("must be logged\n");
101                 ret = false;
102                 goto end;
103         }
104         
105         i = ngadmin_getQOSMode(nga, &s);
106         if (i != ERR_OK) {
107                 printErrCode(i);
108                 ret = false;
109                 goto end;
110         }
111         
112         printf("QoS mode: ");
113         switch (s) {
114         
115         case QOS_DOT:
116                 printf("802.1p\n");
117                 goto end;
118         
119         case QOS_PORT:
120                 printf("port based\n");
121                 break;
122                 
123         default:
124                 printf("unknown (%i)\n", s);
125                 goto end;
126         }
127         
128         ports = malloc(sa->ports * sizeof(char));
129         i = ngadmin_getQOSValues(nga, ports);
130         if (i != ERR_OK) {
131                 printErrCode(i);
132                 ret = false;
133                 goto end;
134         }
135         
136         for (i = 0; i < sa->ports; i++)
137                 printf("port %i: %s\n", i + 1, prio[(int)ports[i]]);
138         
139 end:
140         free(ports);
141         
142         return ret;
143 }
144
145