1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
// SPDX-License-Identifier: GPL-2.0+
#include <errno.h>
#include <linux/dcbnl.h>
#include <stdio.h>
#include "dcb.h"
#include "utils.h"
static void dcb_rewr_help_add(void)
{
fprintf(stderr,
"Usage: dcb rewr { add | del | replace } dev STRING\n"
" [ prio-pcp PRIO:PCP ]\n"
" [ prio-dscp PRIO:DSCP ]\n"
"\n"
" where PRIO := { 0 .. 7 }\n"
" PCP := { 0(nd/de) .. 7(nd/de) }\n"
" DSCP := { 0 .. 63 }\n"
"\n"
);
}
static void dcb_rewr_help_show_flush(void)
{
fprintf(stderr,
"Usage: dcb rewr { show | flush } dev STRING\n"
" [ prio-pcp ]\n"
" [ prio-dscp ]\n"
"\n"
);
}
static void dcb_rewr_help(void)
{
fprintf(stderr,
"Usage: dcb rewr help\n"
"\n"
);
dcb_rewr_help_show_flush();
dcb_rewr_help_add();
}
static void dcb_rewr_parse_mapping_cb(__u32 key, __u64 value, void *data)
{
struct dcb_app_parse_mapping *pm = data;
struct dcb_app app = {
.selector = pm->selector,
.priority = key,
.protocol = value,
};
if (pm->err)
return;
pm->err = dcb_app_table_push(pm->tab, &app);
}
static int dcb_rewr_parse_mapping_prio_pcp(__u32 key, char *value, void *data)
{
__u32 pcp;
if (dcb_app_parse_pcp(&pcp, value))
return -EINVAL;
return dcb_parse_mapping("PRIO", key, IEEE_8021QAZ_MAX_TCS - 1,
"PCP", pcp, DCB_APP_PCP_MAX,
dcb_rewr_parse_mapping_cb, data);
}
static int dcb_rewr_parse_mapping_prio_dscp(__u32 key, char *value, void *data)
{
__u32 dscp;
if (dcb_app_parse_dscp(&dscp, value))
return -EINVAL;
return dcb_parse_mapping("PRIO", key, IEEE_8021QAZ_MAX_TCS - 1,
"DSCP", dscp, DCB_APP_DSCP_MAX,
dcb_rewr_parse_mapping_cb, data);
}
static void dcb_rewr_print_prio_pid(int (*print_pid)(__u16 protocol),
const struct dcb_app *app)
{
print_uint(PRINT_ANY, NULL, "%u:", app->priority);
print_pid(app->protocol);
}
static void dcb_rewr_print_prio_pcp(const struct dcb *dcb,
const struct dcb_app_table *tab)
{
dcb_app_print_filtered(tab, dcb_app_is_pcp,
dcb_rewr_print_prio_pid,
dcb->numeric ? dcb_app_print_pid_dec :
dcb_app_print_pid_pcp,
"prio_pcp", "prio-pcp");
}
static void dcb_rewr_print_prio_dscp(const struct dcb *dcb,
const struct dcb_app_table *tab)
{
dcb_app_print_filtered(tab, dcb_app_is_dscp,
dcb_rewr_print_prio_pid,
dcb->numeric ? dcb_app_print_pid_dec :
dcb_app_print_pid_dscp,
"prio_dscp", "prio-dscp");
}
static void dcb_rewr_print(const struct dcb *dcb,
const struct dcb_app_table *tab)
{
dcb_rewr_print_prio_pcp(dcb, tab);
dcb_rewr_print_prio_dscp(dcb, tab);
}
static bool dcb_rewr_prio_eq(const struct dcb_app *aa, const struct dcb_app *ab)
{
return aa->selector == ab->selector &&
aa->priority == ab->priority;
}
static int dcb_cmd_rewr_parse_add_del(struct dcb *dcb, const char *dev,
int argc, char **argv,
struct dcb_app_table *tab)
{
struct dcb_app_parse_mapping pm = {
.tab = tab,
};
int ret;
if (!argc) {
dcb_rewr_help_add();
return 0;
}
do {
if (strcmp(*argv, "help") == 0) {
dcb_rewr_help_add();
return 0;
} else if (strcmp(*argv, "prio-pcp") == 0) {
NEXT_ARG();
pm.selector = DCB_APP_SEL_PCP;
ret = parse_mapping(&argc, &argv, false,
&dcb_rewr_parse_mapping_prio_pcp,
&pm);
} else if (strcmp(*argv, "prio-dscp") == 0) {
NEXT_ARG();
pm.selector = IEEE_8021QAZ_APP_SEL_DSCP;
ret = parse_mapping(&argc, &argv, false,
&dcb_rewr_parse_mapping_prio_dscp,
&pm);
} else {
fprintf(stderr, "What is \"%s\"?\n", *argv);
dcb_rewr_help_add();
return -EINVAL;
}
if (ret != 0) {
fprintf(stderr, "Invalid mapping %s\n", *argv);
return ret;
}
if (pm.err)
return pm.err;
} while (argc > 0);
return 0;
}
static int dcb_cmd_rewr_add(struct dcb *dcb, const char *dev, int argc,
char **argv)
{
struct dcb_app_table tab = { .attr = DCB_ATTR_DCB_REWR_TABLE };
int ret;
ret = dcb_cmd_rewr_parse_add_del(dcb, dev, argc, argv, &tab);
if (ret != 0)
return ret;
ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_SET, &tab, NULL);
dcb_app_table_fini(&tab);
return ret;
}
static int dcb_cmd_rewr_del(struct dcb *dcb, const char *dev, int argc,
char **argv)
{
struct dcb_app_table tab = { .attr = DCB_ATTR_DCB_REWR_TABLE };
int ret;
ret = dcb_cmd_rewr_parse_add_del(dcb, dev, argc, argv, &tab);
if (ret != 0)
return ret;
ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_DEL, &tab, NULL);
dcb_app_table_fini(&tab);
return ret;
}
static int dcb_cmd_rewr_replace(struct dcb *dcb, const char *dev, int argc,
char **argv)
{
struct dcb_app_table orig = { .attr = DCB_ATTR_DCB_REWR_TABLE };
struct dcb_app_table tab = { .attr = DCB_ATTR_DCB_REWR_TABLE };
struct dcb_app_table new = { .attr = DCB_ATTR_DCB_REWR_TABLE };
int ret;
ret = dcb_app_get(dcb, dev, &orig);
if (ret != 0)
return ret;
ret = dcb_cmd_rewr_parse_add_del(dcb, dev, argc, argv, &tab);
if (ret != 0)
goto out;
/* Attempts to add an existing entry would be rejected, so drop
* these entries from tab.
*/
ret = dcb_app_table_copy(&new, &tab);
if (ret != 0)
goto out;
dcb_app_table_remove_existing(&new, &orig);
ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_SET, &new, NULL);
if (ret != 0) {
fprintf(stderr, "Could not add new rewrite entries\n");
goto out;
}
/* Remove the obsolete entries. */
dcb_app_table_remove_replaced(&orig, &tab, dcb_rewr_prio_eq);
ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_DEL, &orig, NULL);
if (ret != 0) {
fprintf(stderr, "Could not remove replaced rewrite entries\n");
goto out;
}
out:
dcb_app_table_fini(&new);
dcb_app_table_fini(&tab);
dcb_app_table_fini(&orig);
return 0;
}
static int dcb_cmd_rewr_show(struct dcb *dcb, const char *dev, int argc,
char **argv)
{
struct dcb_app_table tab = { .attr = DCB_ATTR_DCB_REWR_TABLE };
int ret;
ret = dcb_app_get(dcb, dev, &tab);
if (ret != 0)
return ret;
dcb_app_table_sort(&tab);
open_json_object(NULL);
if (!argc) {
dcb_rewr_print(dcb, &tab);
goto out;
}
do {
if (strcmp(*argv, "help") == 0) {
dcb_rewr_help_show_flush();
goto out;
} else if (strcmp(*argv, "prio-pcp") == 0) {
dcb_rewr_print_prio_pcp(dcb, &tab);
} else if (strcmp(*argv, "prio-dscp") == 0) {
dcb_rewr_print_prio_dscp(dcb, &tab);
} else {
fprintf(stderr, "What is \"%s\"?\n", *argv);
dcb_rewr_help_show_flush();
ret = -EINVAL;
goto out;
}
NEXT_ARG_FWD();
} while (argc > 0);
out:
close_json_object();
dcb_app_table_fini(&tab);
return ret;
}
static int dcb_cmd_rewr_flush(struct dcb *dcb, const char *dev, int argc,
char **argv)
{
struct dcb_app_table tab = { .attr = DCB_ATTR_DCB_REWR_TABLE };
int ret;
ret = dcb_app_get(dcb, dev, &tab);
if (ret != 0)
return ret;
if (!argc) {
ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_DEL, &tab,
NULL);
goto out;
}
do {
if (strcmp(*argv, "help") == 0) {
dcb_rewr_help_show_flush();
goto out;
} else if (strcmp(*argv, "prio-pcp") == 0) {
ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_DEL, &tab,
&dcb_app_is_pcp);
if (ret != 0)
goto out;
} else if (strcmp(*argv, "prio-dscp") == 0) {
ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_DEL, &tab,
&dcb_app_is_dscp);
if (ret != 0)
goto out;
} else {
fprintf(stderr, "What is \"%s\"?\n", *argv);
dcb_rewr_help_show_flush();
ret = -EINVAL;
goto out;
}
NEXT_ARG_FWD();
} while (argc > 0);
out:
dcb_app_table_fini(&tab);
return ret;
}
int dcb_cmd_rewr(struct dcb *dcb, int argc, char **argv)
{
if (!argc || strcmp(*argv, "help") == 0) {
dcb_rewr_help();
return 0;
} else if (strcmp(*argv, "show") == 0) {
NEXT_ARG_FWD();
return dcb_cmd_parse_dev(dcb, argc, argv, dcb_cmd_rewr_show,
dcb_rewr_help_show_flush);
} else if (strcmp(*argv, "flush") == 0) {
NEXT_ARG_FWD();
return dcb_cmd_parse_dev(dcb, argc, argv, dcb_cmd_rewr_flush,
dcb_rewr_help_show_flush);
} else if (strcmp(*argv, "add") == 0) {
NEXT_ARG_FWD();
return dcb_cmd_parse_dev(dcb, argc, argv, dcb_cmd_rewr_add,
dcb_rewr_help_add);
} else if (strcmp(*argv, "del") == 0) {
NEXT_ARG_FWD();
return dcb_cmd_parse_dev(dcb, argc, argv, dcb_cmd_rewr_del,
dcb_rewr_help_add);
} else if (strcmp(*argv, "replace") == 0) {
NEXT_ARG_FWD();
return dcb_cmd_parse_dev(dcb, argc, argv, dcb_cmd_rewr_replace,
dcb_rewr_help_add);
} else {
fprintf(stderr, "What is \"%s\"?\n", *argv);
dcb_rewr_help();
return -EINVAL;
}
}
|