summaryrefslogtreecommitdiff
path: root/dcb/dcb_app.c
blob: eeb78e70f63f305daf90adbd106c680151f580e5 (plain)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
// SPDX-License-Identifier: GPL-2.0+

#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <libmnl/libmnl.h>
#include <linux/dcbnl.h>

#include "dcb.h"
#include "utils.h"
#include "rt_names.h"

#define DCB_APP_PCP_MAX 15

static const char *const pcp_names[DCB_APP_PCP_MAX + 1] = {
	"0nd", "1nd", "2nd", "3nd", "4nd", "5nd", "6nd", "7nd",
	"0de", "1de", "2de", "3de", "4de", "5de", "6de", "7de"
};

static const char *const ieee_attrs_app_names[__DCB_ATTR_IEEE_APP_MAX] = {
	[DCB_ATTR_IEEE_APP] = "DCB_ATTR_IEEE_APP",
	[DCB_ATTR_DCB_APP] = "DCB_ATTR_DCB_APP"
};

static void dcb_app_help_add(void)
{
	fprintf(stderr,
		"Usage: dcb app { add | del | replace } dev STRING\n"
		"           [ default-prio PRIO ]\n"
		"           [ ethtype-prio ET:PRIO ]\n"
		"           [ stream-port-prio PORT:PRIO ]\n"
		"           [ dgram-port-prio PORT:PRIO ]\n"
		"           [ port-prio PORT:PRIO ]\n"
		"           [ dscp-prio INTEGER:PRIO ]\n"
		"           [ pcp-prio PCP:PRIO ]\n"
		"\n"
		" where PRIO := { 0 .. 7 }\n"
		"       ET := { 0x600 .. 0xffff }\n"
		"       PORT := { 1 .. 65535 }\n"
		"       DSCP := { 0 .. 63 }\n"
		"       PCP := { 0(nd/de) .. 7(nd/de) }\n"
		"\n"
	);
}

static void dcb_app_help_show_flush(void)
{
	fprintf(stderr,
		"Usage: dcb app { show | flush } dev STRING\n"
		"           [ default-prio ]\n"
		"           [ ethtype-prio ]\n"
		"           [ stream-port-prio ]\n"
		"           [ dgram-port-prio ]\n"
		"           [ port-prio ]\n"
		"           [ dscp-prio ]\n"
		"           [ pcp-prio ]\n"
		"\n"
	);
}

static void dcb_app_help(void)
{
	fprintf(stderr,
		"Usage: dcb app help\n"
		"\n"
	);
	dcb_app_help_show_flush();
	dcb_app_help_add();
}

struct dcb_app_table {
	struct dcb_app *apps;
	size_t n_apps;
};

enum ieee_attrs_app dcb_app_attr_type_get(__u8 selector)
{
	switch (selector) {
	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
	case IEEE_8021QAZ_APP_SEL_STREAM:
	case IEEE_8021QAZ_APP_SEL_DGRAM:
	case IEEE_8021QAZ_APP_SEL_ANY:
	case IEEE_8021QAZ_APP_SEL_DSCP:
		return DCB_ATTR_IEEE_APP;
	case DCB_APP_SEL_PCP:
		return DCB_ATTR_DCB_APP;
	default:
		return DCB_ATTR_IEEE_APP_UNSPEC;
	}
}

bool dcb_app_attr_type_validate(enum ieee_attrs_app type)
{
	switch (type) {
	case DCB_ATTR_IEEE_APP:
	case DCB_ATTR_DCB_APP:
		return true;
	default:
		return false;
	}
}

bool dcb_app_selector_validate(enum ieee_attrs_app type, __u8 selector)
{
	return dcb_app_attr_type_get(selector) == type;
}

static void dcb_app_table_fini(struct dcb_app_table *tab)
{
	free(tab->apps);
}

static int dcb_app_table_push(struct dcb_app_table *tab, struct dcb_app *app)
{
	struct dcb_app *apps = realloc(tab->apps, (tab->n_apps + 1) * sizeof(*tab->apps));

	if (apps == NULL) {
		perror("Cannot allocate APP table");
		return -ENOMEM;
	}

	tab->apps = apps;
	tab->apps[tab->n_apps++] = *app;
	return 0;
}

static void dcb_app_table_remove_existing(struct dcb_app_table *a,
					  const struct dcb_app_table *b)
{
	size_t ia, ja;
	size_t ib;

	for (ia = 0, ja = 0; ia < a->n_apps; ia++) {
		struct dcb_app *aa = &a->apps[ia];
		bool found = false;

		for (ib = 0; ib < b->n_apps; ib++) {
			const struct dcb_app *ab = &b->apps[ib];

			if (aa->selector == ab->selector &&
			    aa->protocol == ab->protocol &&
			    aa->priority == ab->priority) {
				found = true;
				break;
			}
		}

		if (!found)
			a->apps[ja++] = *aa;
	}

	a->n_apps = ja;
}

static void dcb_app_table_remove_replaced(struct dcb_app_table *a,
					  const struct dcb_app_table *b)
{
	size_t ia, ja;
	size_t ib;

	for (ia = 0, ja = 0; ia < a->n_apps; ia++) {
		struct dcb_app *aa = &a->apps[ia];
		bool present = false;
		bool found = false;

		for (ib = 0; ib < b->n_apps; ib++) {
			const struct dcb_app *ab = &b->apps[ib];

			if (aa->selector == ab->selector &&
			    aa->protocol == ab->protocol)
				present = true;
			else
				continue;

			if (aa->priority == ab->priority) {
				found = true;
				break;
			}
		}

		/* Entries that remain in A will be removed, so keep in the
		 * table only APP entries whose sel/pid is mentioned in B,
		 * but that do not have the full sel/pid/prio match.
		 */
		if (present && !found)
			a->apps[ja++] = *aa;
	}

	a->n_apps = ja;
}

static int dcb_app_table_copy(struct dcb_app_table *a,
			      const struct dcb_app_table *b)
{
	size_t i;
	int ret;

	for (i = 0; i < b->n_apps; i++) {
		ret = dcb_app_table_push(a, &b->apps[i]);
		if (ret != 0)
			return ret;
	}
	return 0;
}

static int dcb_app_cmp(const struct dcb_app *a, const struct dcb_app *b)
{
	if (a->protocol < b->protocol)
		return -1;
	if (a->protocol > b->protocol)
		return 1;
	return a->priority - b->priority;
}

static int dcb_app_cmp_cb(const void *a, const void *b)
{
	return dcb_app_cmp(a, b);
}

static void dcb_app_table_sort(struct dcb_app_table *tab)
{
	qsort(tab->apps, tab->n_apps, sizeof(*tab->apps), dcb_app_cmp_cb);
}

struct dcb_app_parse_mapping {
	__u8 selector;
	struct dcb_app_table *tab;
	int err;
};

static void dcb_app_parse_mapping_cb(__u32 key, __u64 value, void *data)
{
	struct dcb_app_parse_mapping *pm = data;
	struct dcb_app app = {
		.selector = pm->selector,
		.priority = value,
		.protocol = key,
	};

	if (pm->err)
		return;

	pm->err = dcb_app_table_push(pm->tab, &app);
}

static int dcb_app_parse_mapping_ethtype_prio(__u32 key, char *value, void *data)
{
	__u8 prio;

	if (key < 0x600) {
		fprintf(stderr, "Protocol IDs < 0x600 are reserved for EtherType\n");
		return -EINVAL;
	}

	if (get_u8(&prio, value, 0))
		return -EINVAL;

	return dcb_parse_mapping("ETHTYPE", key, 0xffff,
				 "PRIO", prio, IEEE_8021QAZ_MAX_TCS - 1,
				 dcb_app_parse_mapping_cb, data);
}

static int dcb_app_parse_pcp(__u32 *key, const char *arg)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(pcp_names); i++) {
		if (pcp_names[i] && strcmp(arg, pcp_names[i]) == 0) {
			*key = i;
			return 0;
		}
	}

	return -EINVAL;
}

static int dcb_app_parse_mapping_pcp_prio(__u32 key, char *value, void *data)
{
	__u8 prio;

	if (get_u8(&prio, value, 0))
		return -EINVAL;

	return dcb_parse_mapping("PCP", key, DCB_APP_PCP_MAX,
				 "PRIO", prio, IEEE_8021QAZ_MAX_TCS - 1,
				 dcb_app_parse_mapping_cb, data);
}

static int dcb_app_parse_dscp(__u32 *key, const char *arg)
{
	if (parse_mapping_num_all(key, arg) == 0)
		return 0;

	if (rtnl_dsfield_a2n(key, arg) != 0)
		return -1;

	if (*key & 0x03) {
		fprintf(stderr, "The values `%s' uses non-DSCP bits.\n", arg);
		return -1;
	}

	/* Unshift the value to convert it from dsfield to DSCP. */
	*key >>= 2;
	return 0;
}

static int dcb_app_parse_mapping_dscp_prio(__u32 key, char *value, void *data)
{
	__u8 prio;

	if (get_u8(&prio, value, 0))
		return -EINVAL;

	return dcb_parse_mapping("DSCP", key, 63,
				 "PRIO", prio, IEEE_8021QAZ_MAX_TCS - 1,
				 dcb_app_parse_mapping_cb, data);
}

static int dcb_app_parse_mapping_port_prio(__u32 key, char *value, void *data)
{
	__u8 prio;

	if (key == 0) {
		fprintf(stderr, "Port ID of 0 is invalid\n");
		return -EINVAL;
	}

	if (get_u8(&prio, value, 0))
		return -EINVAL;

	return dcb_parse_mapping("PORT", key, 0xffff,
				 "PRIO", prio, IEEE_8021QAZ_MAX_TCS - 1,
				 dcb_app_parse_mapping_cb, data);
}

static int dcb_app_parse_default_prio(int *argcp, char ***argvp, struct dcb_app_table *tab)
{
	int argc = *argcp;
	char **argv = *argvp;
	int ret = 0;

	while (argc > 0) {
		struct dcb_app app;
		__u8 prio;

		if (get_u8(&prio, *argv, 0)) {
			ret = 1;
			break;
		}

		app = (struct dcb_app){
			.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
			.protocol = 0,
			.priority = prio,
		};
		ret = dcb_app_table_push(tab, &app);
		if (ret != 0)
			break;

		argc--, argv++;
	}

	*argcp = argc;
	*argvp = argv;
	return ret;
}

static bool dcb_app_is_ethtype(const struct dcb_app *app)
{
	return app->selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
	       app->protocol != 0;
}

static bool dcb_app_is_default(const struct dcb_app *app)
{
	return app->selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
	       app->protocol == 0;
}

static bool dcb_app_is_dscp(const struct dcb_app *app)
{
	return app->selector == IEEE_8021QAZ_APP_SEL_DSCP;
}

static bool dcb_app_is_pcp(const struct dcb_app *app)
{
	return app->selector == DCB_APP_SEL_PCP;
}

static bool dcb_app_is_stream_port(const struct dcb_app *app)
{
	return app->selector == IEEE_8021QAZ_APP_SEL_STREAM;
}

static bool dcb_app_is_dgram_port(const struct dcb_app *app)
{
	return app->selector == IEEE_8021QAZ_APP_SEL_DGRAM;
}

static bool dcb_app_is_port(const struct dcb_app *app)
{
	return app->selector == IEEE_8021QAZ_APP_SEL_ANY;
}

static int dcb_app_print_key_dec(__u16 protocol)
{
	return print_uint(PRINT_ANY, NULL, "%d:", protocol);
}

static int dcb_app_print_key_hex(__u16 protocol)
{
	return print_uint(PRINT_ANY, NULL, "%x:", protocol);
}

static int dcb_app_print_key_dscp(__u16 protocol)
{
	const char *name = rtnl_dsfield_get_name(protocol << 2);


	if (!is_json_context() && name != NULL)
		return print_string(PRINT_FP, NULL, "%s:", name);
	return print_uint(PRINT_ANY, NULL, "%d:", protocol);
}

static int dcb_app_print_key_pcp(__u16 protocol)
{
	/* Print in numerical form, if protocol value is out-of-range */
	if (protocol > DCB_APP_PCP_MAX)
		return print_uint(PRINT_ANY, NULL, "%d:", protocol);

	return print_string(PRINT_ANY, NULL, "%s:", pcp_names[protocol]);
}

static void dcb_app_print_filtered(const struct dcb_app_table *tab,
				   bool (*filter)(const struct dcb_app *),
				   int (*print_key)(__u16 protocol),
				   const char *json_name,
				   const char *fp_name)
{
	bool first = true;
	size_t i;

	for (i = 0; i < tab->n_apps; i++) {
		struct dcb_app *app = &tab->apps[i];

		if (!filter(app))
			continue;
		if (first) {
			open_json_array(PRINT_JSON, json_name);
			print_string(PRINT_FP, NULL, "%s ", fp_name);
			first = false;
		}

		open_json_array(PRINT_JSON, NULL);
		print_key(app->protocol);
		print_uint(PRINT_ANY, NULL, "%d ", app->priority);
		close_json_array(PRINT_JSON, NULL);
	}

	if (!first) {
		close_json_array(PRINT_JSON, json_name);
		print_nl();
	}
}

static void dcb_app_print_ethtype_prio(const struct dcb_app_table *tab)
{
	dcb_app_print_filtered(tab, dcb_app_is_ethtype,  dcb_app_print_key_hex,
			       "ethtype_prio", "ethtype-prio");
}

static void dcb_app_print_pcp_prio(const struct dcb *dcb,
				   const struct dcb_app_table *tab)
{
	dcb_app_print_filtered(tab, dcb_app_is_pcp,
			       dcb->numeric ? dcb_app_print_key_dec
					    : dcb_app_print_key_pcp,
			       "pcp_prio", "pcp-prio");
}

static void dcb_app_print_dscp_prio(const struct dcb *dcb,
				    const struct dcb_app_table *tab)
{
	dcb_app_print_filtered(tab, dcb_app_is_dscp,
			       dcb->numeric ? dcb_app_print_key_dec
					    : dcb_app_print_key_dscp,
			       "dscp_prio", "dscp-prio");
}

static void dcb_app_print_stream_port_prio(const struct dcb_app_table *tab)
{
	dcb_app_print_filtered(tab, dcb_app_is_stream_port, dcb_app_print_key_dec,
			       "stream_port_prio", "stream-port-prio");
}

static void dcb_app_print_dgram_port_prio(const struct dcb_app_table *tab)
{
	dcb_app_print_filtered(tab, dcb_app_is_dgram_port, dcb_app_print_key_dec,
			       "dgram_port_prio", "dgram-port-prio");
}

static void dcb_app_print_port_prio(const struct dcb_app_table *tab)
{
	dcb_app_print_filtered(tab, dcb_app_is_port, dcb_app_print_key_dec,
			       "port_prio", "port-prio");
}

static void dcb_app_print_default_prio(const struct dcb_app_table *tab)
{
	bool first = true;
	size_t i;

	for (i = 0; i < tab->n_apps; i++) {
		if (!dcb_app_is_default(&tab->apps[i]))
			continue;
		if (first) {
			open_json_array(PRINT_JSON, "default_prio");
			print_string(PRINT_FP, NULL, "default-prio ", NULL);
			first = false;
		}
		print_uint(PRINT_ANY, NULL, "%d ", tab->apps[i].priority);
	}

	if (!first) {
		close_json_array(PRINT_JSON, "default_prio");
		print_nl();
	}
}

static void dcb_app_print(const struct dcb *dcb, const struct dcb_app_table *tab)
{
	dcb_app_print_ethtype_prio(tab);
	dcb_app_print_default_prio(tab);
	dcb_app_print_dscp_prio(dcb, tab);
	dcb_app_print_stream_port_prio(tab);
	dcb_app_print_dgram_port_prio(tab);
	dcb_app_print_port_prio(tab);
	dcb_app_print_pcp_prio(dcb, tab);
}

static int dcb_app_get_table_attr_cb(const struct nlattr *attr, void *data)
{
	struct dcb_app_table *tab = data;
	struct dcb_app *app;
	uint16_t type;
	int ret;

	type = mnl_attr_get_type(attr);

	if (!dcb_app_attr_type_validate(type)) {
		fprintf(stderr,
			"Unknown attribute in DCB_ATTR_IEEE_APP_TABLE: %d\n",
			type);
		return MNL_CB_OK;
	}
	if (mnl_attr_get_payload_len(attr) < sizeof(struct dcb_app)) {
		fprintf(stderr,
			"%s payload expected to have size %zd, not %d\n",
			ieee_attrs_app_names[type], sizeof(struct dcb_app),
			mnl_attr_get_payload_len(attr));
		return MNL_CB_OK;
	}

	app = mnl_attr_get_payload(attr);

	/* Check that selector is encapsulated in the right attribute */
	if (!dcb_app_selector_validate(type, app->selector)) {
		fprintf(stderr, "Wrong selector for type: %s\n",
			ieee_attrs_app_names[type]);
		return MNL_CB_OK;
	}

	ret = dcb_app_table_push(tab, app);
	if (ret != 0)
		return MNL_CB_ERROR;

	return MNL_CB_OK;
}

static int dcb_app_get(struct dcb *dcb, const char *dev, struct dcb_app_table *tab)
{
	uint16_t payload_len;
	void *payload;
	int ret;

	ret = dcb_get_attribute_va(dcb, dev, DCB_ATTR_IEEE_APP_TABLE, &payload, &payload_len);
	if (ret != 0)
		return ret;

	ret = mnl_attr_parse_payload(payload, payload_len, dcb_app_get_table_attr_cb, tab);
	if (ret != MNL_CB_OK)
		return -EINVAL;

	return 0;
}

struct dcb_app_add_del {
	const struct dcb_app_table *tab;
	bool (*filter)(const struct dcb_app *app);
};

static int dcb_app_add_del_cb(struct dcb *dcb, struct nlmsghdr *nlh, void *data)
{
	struct dcb_app_add_del *add_del = data;
	enum ieee_attrs_app type;
	struct nlattr *nest;
	size_t i;

	nest = mnl_attr_nest_start(nlh, DCB_ATTR_IEEE_APP_TABLE);

	for (i = 0; i < add_del->tab->n_apps; i++) {
		const struct dcb_app *app = &add_del->tab->apps[i];
		type = dcb_app_attr_type_get(app->selector);

		if (add_del->filter == NULL || add_del->filter(app))
			mnl_attr_put(nlh, type, sizeof(*app), app);
	}

	mnl_attr_nest_end(nlh, nest);
	return 0;
}

static int dcb_app_add_del(struct dcb *dcb, const char *dev, int command,
			   const struct dcb_app_table *tab,
			   bool (*filter)(const struct dcb_app *))
{
	struct dcb_app_add_del add_del = {
		.tab = tab,
		.filter = filter,
	};

	if (tab->n_apps == 0)
		return 0;

	return dcb_set_attribute_va(dcb, command, dev, dcb_app_add_del_cb, &add_del);
}

static int dcb_cmd_app_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_app_help_add();
		return 0;
	}

	do {
		if (matches(*argv, "help") == 0) {
			dcb_app_help_add();
			return 0;
		} else if (matches(*argv, "ethtype-prio") == 0) {
			NEXT_ARG();
			pm.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
			ret = parse_mapping(&argc, &argv, false,
					    &dcb_app_parse_mapping_ethtype_prio,
					    &pm);
		} else if (matches(*argv, "default-prio") == 0) {
			NEXT_ARG();
			ret = dcb_app_parse_default_prio(&argc, &argv, pm.tab);
			if (ret != 0) {
				fprintf(stderr, "Invalid default priority %s\n", *argv);
				return ret;
			}
		} else if (matches(*argv, "dscp-prio") == 0) {
			NEXT_ARG();
			pm.selector = IEEE_8021QAZ_APP_SEL_DSCP;
			ret = parse_mapping_gen(&argc, &argv,
						&dcb_app_parse_dscp,
						&dcb_app_parse_mapping_dscp_prio,
						&pm);
		} else if (matches(*argv, "stream-port-prio") == 0) {
			NEXT_ARG();
			pm.selector = IEEE_8021QAZ_APP_SEL_STREAM;
			ret = parse_mapping(&argc, &argv, false,
					    &dcb_app_parse_mapping_port_prio,
					    &pm);
		} else if (matches(*argv, "dgram-port-prio") == 0) {
			NEXT_ARG();
			pm.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
			ret = parse_mapping(&argc, &argv, false,
					    &dcb_app_parse_mapping_port_prio,
					    &pm);
		} else if (matches(*argv, "port-prio") == 0) {
			NEXT_ARG();
			pm.selector = IEEE_8021QAZ_APP_SEL_ANY;
			ret = parse_mapping(&argc, &argv, false,
					    &dcb_app_parse_mapping_port_prio,
					    &pm);
		} else if (strcmp(*argv, "pcp-prio") == 0) {
			NEXT_ARG();
			pm.selector = DCB_APP_SEL_PCP;
			ret = parse_mapping_gen(&argc, &argv, &dcb_app_parse_pcp,
						&dcb_app_parse_mapping_pcp_prio,
						&pm);
		} else {
			fprintf(stderr, "What is \"%s\"?\n", *argv);
			dcb_app_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_app_add(struct dcb *dcb, const char *dev, int argc, char **argv)
{
	struct dcb_app_table tab = {};
	int ret;

	ret = dcb_cmd_app_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_app_del(struct dcb *dcb, const char *dev, int argc, char **argv)
{
	struct dcb_app_table tab = {};
	int ret;

	ret = dcb_cmd_app_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_app_show(struct dcb *dcb, const char *dev, int argc, char **argv)
{
	struct dcb_app_table tab = {};
	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_app_print(dcb, &tab);
		goto out;
	}

	do {
		if (matches(*argv, "help") == 0) {
			dcb_app_help_show_flush();
			goto out;
		} else if (matches(*argv, "ethtype-prio") == 0) {
			dcb_app_print_ethtype_prio(&tab);
		} else if (matches(*argv, "dscp-prio") == 0) {
			dcb_app_print_dscp_prio(dcb, &tab);
		} else if (matches(*argv, "stream-port-prio") == 0) {
			dcb_app_print_stream_port_prio(&tab);
		} else if (matches(*argv, "dgram-port-prio") == 0) {
			dcb_app_print_dgram_port_prio(&tab);
		} else if (matches(*argv, "port-prio") == 0) {
			dcb_app_print_port_prio(&tab);
		} else if (matches(*argv, "default-prio") == 0) {
			dcb_app_print_default_prio(&tab);
		} else if (strcmp(*argv, "pcp-prio") == 0) {
			dcb_app_print_pcp_prio(dcb, &tab);
		} else {
			fprintf(stderr, "What is \"%s\"?\n", *argv);
			dcb_app_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_app_flush(struct dcb *dcb, const char *dev, int argc, char **argv)
{
	struct dcb_app_table tab = {};
	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 (matches(*argv, "help") == 0) {
			dcb_app_help_show_flush();
			goto out;
		} else if (matches(*argv, "ethtype-prio") == 0) {
			ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_DEL, &tab,
					      &dcb_app_is_ethtype);
			if (ret != 0)
				goto out;
		} else if (matches(*argv, "default-prio") == 0) {
			ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_DEL, &tab,
					      &dcb_app_is_default);
			if (ret != 0)
				goto out;
		} else if (matches(*argv, "dscp-prio") == 0) {
			ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_DEL, &tab,
					      &dcb_app_is_dscp);
			if (ret != 0)
				goto out;
		} else if (strcmp(*argv, "pcp-prio") == 0) {
			ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_DEL, &tab,
					      &dcb_app_is_pcp);
			if (ret != 0)
				goto out;
		} else {
			fprintf(stderr, "What is \"%s\"?\n", *argv);
			dcb_app_help_show_flush();
			ret = -EINVAL;
			goto out;
		}

		NEXT_ARG_FWD();
	} while (argc > 0);

out:
	dcb_app_table_fini(&tab);
	return ret;
}

static int dcb_cmd_app_replace(struct dcb *dcb, const char *dev, int argc, char **argv)
{
	struct dcb_app_table orig = {};
	struct dcb_app_table tab = {};
	struct dcb_app_table new = {};
	int ret;

	ret = dcb_app_get(dcb, dev, &orig);
	if (ret != 0)
		return ret;

	ret = dcb_cmd_app_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 APP entries\n");
		goto out;
	}

	/* Remove the obsolete entries. */
	dcb_app_table_remove_replaced(&orig, &tab);
	ret = dcb_app_add_del(dcb, dev, DCB_CMD_IEEE_DEL, &orig, NULL);
	if (ret != 0) {
		fprintf(stderr, "Could not remove replaced APP entries\n");
		goto out;
	}

out:
	dcb_app_table_fini(&new);
	dcb_app_table_fini(&tab);
	dcb_app_table_fini(&orig);
	return 0;
}

int dcb_cmd_app(struct dcb *dcb, int argc, char **argv)
{
	if (!argc || matches(*argv, "help") == 0) {
		dcb_app_help();
		return 0;
	} else if (matches(*argv, "show") == 0) {
		NEXT_ARG_FWD();
		return dcb_cmd_parse_dev(dcb, argc, argv,
					 dcb_cmd_app_show, dcb_app_help_show_flush);
	} else if (matches(*argv, "flush") == 0) {
		NEXT_ARG_FWD();
		return dcb_cmd_parse_dev(dcb, argc, argv,
					 dcb_cmd_app_flush, dcb_app_help_show_flush);
	} else if (matches(*argv, "add") == 0) {
		NEXT_ARG_FWD();
		return dcb_cmd_parse_dev(dcb, argc, argv,
					 dcb_cmd_app_add, dcb_app_help_add);
	} else if (matches(*argv, "del") == 0) {
		NEXT_ARG_FWD();
		return dcb_cmd_parse_dev(dcb, argc, argv,
					 dcb_cmd_app_del, dcb_app_help_add);
	} else if (matches(*argv, "replace") == 0) {
		NEXT_ARG_FWD();
		return dcb_cmd_parse_dev(dcb, argc, argv,
					 dcb_cmd_app_replace, dcb_app_help_add);
	} else {
		fprintf(stderr, "What is \"%s\"?\n", *argv);
		dcb_app_help();
		return -EINVAL;
	}
}