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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Zebra Traffic Control (TC) interaction with the kernel using netlink.
*
* Copyright (C) 2022 Shichu Yang
*/
#include <zebra.h>
#ifdef HAVE_NETLINK
#include <linux/pkt_cls.h>
#include <linux/pkt_sched.h>
#include <netinet/if_ether.h>
#include <sys/socket.h>
#include "if.h"
#include "prefix.h"
#include "vrf.h"
#include "zebra/zserv.h"
#include "zebra/zebra_ns.h"
#include "zebra/rt.h"
#include "zebra/interface.h"
#include "zebra/debug.h"
#include "zebra/kernel_netlink.h"
#include "zebra/tc_netlink.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_dplane.h"
#include "zebra/zebra_tc.h"
#include "zebra/zebra_trace.h"
#define TC_FREQ_DEFAULT (100)
/* some magic number */
#define TC_QDISC_MAJOR_ZEBRA (0xbeef0000u)
#define TC_MINOR_NOCLASS (0xffffu)
#define TIME_UNITS_PER_SEC (1000000)
#define xmittime(r, s) (TIME_UNITS_PER_SEC * ((double)(s) / (double)(r)))
static uint32_t tc_get_freq(void)
{
int freq = 0;
FILE *fp = fopen("/proc/net/psched", "r");
if (fp) {
uint32_t nom, denom;
if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2) {
if (nom == 1000000)
freq = denom;
}
fclose(fp);
}
return freq == 0 ? TC_FREQ_DEFAULT : freq;
}
static void tc_calc_rate_table(struct tc_ratespec *ratespec, uint32_t *table,
uint32_t mtu)
{
if (mtu == 0)
mtu = 2047;
int cell_log = -1;
if (cell_log < 0) {
cell_log = 0;
while ((mtu >> cell_log) > 255)
cell_log++;
}
for (int i = 0; i < 256; i++)
table[i] = xmittime(ratespec->rate, (i + 1) << cell_log);
ratespec->cell_align = -1;
ratespec->cell_log = cell_log;
ratespec->linklayer = TC_LINKLAYER_ETHERNET;
}
static int tc_flower_get_inet_prefix(const struct prefix *prefix,
struct inet_prefix *addr)
{
addr->family = prefix->family;
if (addr->family == AF_INET) {
addr->bytelen = 4;
addr->bitlen = prefix->prefixlen;
addr->flags = 0;
addr->flags |= PREFIXLEN_SPECIFIED;
addr->flags |= ADDRTYPE_INET;
memcpy(addr->data, prefix->u.val32, sizeof(prefix->u.val32));
} else if (addr->family == AF_INET6) {
addr->bytelen = 16;
addr->bitlen = prefix->prefixlen;
addr->flags = 0;
addr->flags |= PREFIXLEN_SPECIFIED;
addr->flags |= ADDRTYPE_INET;
memcpy(addr->data, prefix->u.val, sizeof(prefix->u.val));
} else {
return -1;
}
return 0;
}
static int tc_flower_get_inet_mask(const struct prefix *prefix,
struct inet_prefix *addr)
{
addr->family = prefix->family;
if (addr->family == AF_INET) {
addr->bytelen = 4;
addr->bitlen = prefix->prefixlen;
addr->flags = 0;
addr->flags |= PREFIXLEN_SPECIFIED;
addr->flags |= ADDRTYPE_INET;
} else if (addr->family == AF_INET6) {
addr->bytelen = 16;
addr->bitlen = prefix->prefixlen;
addr->flags = 0;
addr->flags |= PREFIXLEN_SPECIFIED;
addr->flags |= ADDRTYPE_INET;
} else {
return -1;
}
memset(addr->data, 0xff, addr->bytelen);
int rest = prefix->prefixlen;
for (int i = 0; i < addr->bytelen / 4; i++) {
if (!rest) {
addr->data[i] = 0;
} else if (rest / 32 >= 1) {
rest -= 32;
} else {
addr->data[i] <<= 32 - rest;
addr->data[i] = htonl(addr->data[i]);
rest = 0;
}
}
return 0;
}
/*
* Traffic control queue discipline encoding (only "htb" supported)
*/
static ssize_t netlink_qdisc_msg_encode(int cmd, struct zebra_dplane_ctx *ctx,
void *data, size_t datalen)
{
struct nlsock *nl;
const char *kind_str = NULL;
struct rtattr *nest;
struct {
struct nlmsghdr n;
struct tcmsg t;
char buf[0];
} *req = data;
if (datalen < sizeof(*req))
return 0;
nl = kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
memset(req, 0, sizeof(*req));
req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
req->n.nlmsg_flags |= NLM_F_REPLACE;
req->n.nlmsg_type = cmd;
req->n.nlmsg_pid = nl->snl.nl_pid;
req->t.tcm_family = AF_UNSPEC;
req->t.tcm_ifindex = dplane_ctx_get_ifindex(ctx);
req->t.tcm_info = 0;
req->t.tcm_handle = 0;
req->t.tcm_parent = TC_H_ROOT;
if (cmd == RTM_NEWQDISC) {
req->t.tcm_handle = TC_H_MAKE(TC_QDISC_MAJOR_ZEBRA, 0);
kind_str = dplane_ctx_tc_qdisc_get_kind_str(ctx);
nl_attr_put(&req->n, datalen, TCA_KIND, kind_str,
strlen(kind_str) + 1);
nest = nl_attr_nest(&req->n, datalen, TCA_OPTIONS);
switch (dplane_ctx_tc_qdisc_get_kind(ctx)) {
case TC_QDISC_HTB: {
struct tc_htb_glob htb_glob = {
.rate2quantum = 10,
.version = 3,
.defcls = TC_MINOR_NOCLASS};
nl_attr_put(&req->n, datalen, TCA_HTB_INIT, &htb_glob,
sizeof(htb_glob));
break;
}
case TC_QDISC_NOQUEUE:
break;
default:
break;
/* not implemented */
}
nl_attr_nest_end(&req->n, nest);
} else {
/* ifindex are enough for del/get qdisc */
}
return NLMSG_ALIGN(req->n.nlmsg_len);
}
/*
* Traffic control class encoding
*/
static ssize_t netlink_tclass_msg_encode(int cmd, struct zebra_dplane_ctx *ctx,
void *data, size_t datalen)
{
enum dplane_op_e op = dplane_ctx_get_op(ctx);
struct nlsock *nl;
const char *kind_str = NULL;
struct rtattr *nest;
struct {
struct nlmsghdr n;
struct tcmsg t;
char buf[0];
} *req = data;
if (datalen < sizeof(*req))
return 0;
nl = kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
memset(req, 0, sizeof(*req));
req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
if (op == DPLANE_OP_TC_CLASS_UPDATE)
req->n.nlmsg_flags |= NLM_F_REPLACE;
req->n.nlmsg_type = cmd;
req->n.nlmsg_pid = nl->snl.nl_pid;
req->t.tcm_family = AF_UNSPEC;
req->t.tcm_ifindex = dplane_ctx_get_ifindex(ctx);
req->t.tcm_handle = TC_H_MAKE(TC_QDISC_MAJOR_ZEBRA,
dplane_ctx_tc_class_get_handle(ctx));
req->t.tcm_parent = TC_H_MAKE(TC_QDISC_MAJOR_ZEBRA, 0);
req->t.tcm_info = 0;
kind_str = dplane_ctx_tc_class_get_kind_str(ctx);
if (op == DPLANE_OP_TC_CLASS_ADD || op == DPLANE_OP_TC_CLASS_UPDATE) {
zlog_debug("netlink tclass encoder: op: %s kind: %s handle: %u",
op == DPLANE_OP_TC_CLASS_UPDATE ? "update" : "add",
kind_str, dplane_ctx_tc_class_get_handle(ctx));
nl_attr_put(&req->n, datalen, TCA_KIND, kind_str,
strlen(kind_str) + 1);
nest = nl_attr_nest(&req->n, datalen, TCA_OPTIONS);
switch (dplane_ctx_tc_class_get_kind(ctx)) {
case TC_QDISC_HTB: {
struct tc_htb_opt htb_opt = {};
uint64_t rate = dplane_ctx_tc_class_get_rate(ctx),
ceil = dplane_ctx_tc_class_get_ceil(ctx);
uint64_t buffer, cbuffer;
/* TODO: fetch mtu from interface */
uint32_t mtu = 1500;
uint32_t rtab[256];
uint32_t ctab[256];
ceil = MAX(rate, ceil);
htb_opt.rate.rate = (rate >> 32 != 0) ? ~0U : rate;
htb_opt.ceil.rate = (ceil >> 32 != 0) ? ~0U : ceil;
buffer = rate / tc_get_freq() + mtu;
cbuffer = ceil / tc_get_freq() + mtu;
htb_opt.buffer = buffer;
htb_opt.cbuffer = cbuffer;
tc_calc_rate_table(&htb_opt.rate, rtab, mtu);
tc_calc_rate_table(&htb_opt.ceil, ctab, mtu);
htb_opt.ceil.mpu = htb_opt.rate.mpu = 0;
htb_opt.ceil.overhead = htb_opt.rate.overhead = 0;
if (rate >> 32 != 0) {
nl_attr_put(&req->n, datalen, TCA_HTB_RATE64,
&rate, sizeof(rate));
}
if (ceil >> 32 != 0) {
nl_attr_put(&req->n, datalen, TCA_HTB_CEIL64,
&ceil, sizeof(ceil));
}
nl_attr_put(&req->n, datalen, TCA_HTB_PARMS, &htb_opt,
sizeof(htb_opt));
nl_attr_put(&req->n, datalen, TCA_HTB_RTAB, rtab,
sizeof(rtab));
nl_attr_put(&req->n, datalen, TCA_HTB_CTAB, ctab,
sizeof(ctab));
break;
}
default:
break;
}
nl_attr_nest_end(&req->n, nest);
}
return NLMSG_ALIGN(req->n.nlmsg_len);
}
static int netlink_tfilter_flower_port_type(uint8_t ip_proto, bool src)
{
if (ip_proto == IPPROTO_TCP)
return src ? TCA_FLOWER_KEY_TCP_SRC : TCA_FLOWER_KEY_TCP_DST;
else if (ip_proto == IPPROTO_UDP)
return src ? TCA_FLOWER_KEY_UDP_SRC : TCA_FLOWER_KEY_UDP_DST;
else if (ip_proto == IPPROTO_SCTP)
return src ? TCA_FLOWER_KEY_SCTP_SRC : TCA_FLOWER_KEY_SCTP_DST;
else
return -1;
}
static void netlink_tfilter_flower_put_options(struct nlmsghdr *n,
size_t datalen,
struct zebra_dplane_ctx *ctx)
{
struct inet_prefix addr;
uint32_t flags = 0, classid;
uint8_t protocol = htons(dplane_ctx_tc_filter_get_eth_proto(ctx));
uint32_t filter_bm = dplane_ctx_tc_filter_get_filter_bm(ctx);
if (filter_bm & TC_FLOWER_SRC_IP) {
const struct prefix *src_p =
dplane_ctx_tc_filter_get_src_ip(ctx);
if (tc_flower_get_inet_prefix(src_p, &addr) != 0)
return;
nl_attr_put(n, datalen,
(addr.family == AF_INET) ? TCA_FLOWER_KEY_IPV4_SRC
: TCA_FLOWER_KEY_IPV6_SRC,
addr.data, addr.bytelen);
if (tc_flower_get_inet_mask(src_p, &addr) != 0)
return;
nl_attr_put(n, datalen,
(addr.family == AF_INET)
? TCA_FLOWER_KEY_IPV4_SRC_MASK
: TCA_FLOWER_KEY_IPV6_SRC_MASK,
addr.data, addr.bytelen);
}
if (filter_bm & TC_FLOWER_DST_IP) {
const struct prefix *dst_p =
dplane_ctx_tc_filter_get_dst_ip(ctx);
if (tc_flower_get_inet_prefix(dst_p, &addr) != 0)
return;
nl_attr_put(n, datalen,
(addr.family == AF_INET) ? TCA_FLOWER_KEY_IPV4_DST
: TCA_FLOWER_KEY_IPV6_DST,
addr.data, addr.bytelen);
if (tc_flower_get_inet_mask(dst_p, &addr) != 0)
return;
nl_attr_put(n, datalen,
(addr.family == AF_INET)
? TCA_FLOWER_KEY_IPV4_DST_MASK
: TCA_FLOWER_KEY_IPV6_DST_MASK,
addr.data, addr.bytelen);
}
if (filter_bm & TC_FLOWER_IP_PROTOCOL) {
nl_attr_put8(n, datalen, TCA_FLOWER_KEY_IP_PROTO,
dplane_ctx_tc_filter_get_ip_proto(ctx));
}
if (filter_bm & TC_FLOWER_SRC_PORT) {
uint16_t min, max;
min = dplane_ctx_tc_filter_get_src_port_min(ctx);
max = dplane_ctx_tc_filter_get_src_port_max(ctx);
if (max > min) {
nl_attr_put16(n, datalen, TCA_FLOWER_KEY_PORT_SRC_MIN,
htons(min));
nl_attr_put16(n, datalen, TCA_FLOWER_KEY_PORT_SRC_MAX,
htons(max));
} else {
int type = netlink_tfilter_flower_port_type(
dplane_ctx_tc_filter_get_ip_proto(ctx), true);
if (type < 0)
return;
nl_attr_put16(n, datalen, type, htons(min));
}
}
if (filter_bm & TC_FLOWER_DST_PORT) {
uint16_t min = dplane_ctx_tc_filter_get_dst_port_min(ctx),
max = dplane_ctx_tc_filter_get_dst_port_max(ctx);
if (max > min) {
nl_attr_put16(n, datalen, TCA_FLOWER_KEY_PORT_DST_MIN,
htons(min));
nl_attr_put16(n, datalen, TCA_FLOWER_KEY_PORT_DST_MAX,
htons(max));
} else {
int type = netlink_tfilter_flower_port_type(
dplane_ctx_tc_filter_get_ip_proto(ctx), false);
if (type < 0)
return;
nl_attr_put16(n, datalen, type, htons(min));
}
}
if (filter_bm & TC_FLOWER_DSFIELD) {
nl_attr_put8(n, datalen, TCA_FLOWER_KEY_IP_TOS,
dplane_ctx_tc_filter_get_dsfield(ctx));
nl_attr_put8(n, datalen, TCA_FLOWER_KEY_IP_TOS_MASK,
dplane_ctx_tc_filter_get_dsfield_mask(ctx));
}
classid = TC_H_MAKE(TC_QDISC_MAJOR_ZEBRA,
dplane_ctx_tc_filter_get_classid(ctx));
nl_attr_put32(n, datalen, TCA_FLOWER_CLASSID, classid);
nl_attr_put32(n, datalen, TCA_FLOWER_FLAGS, flags);
nl_attr_put16(n, datalen, TCA_FLOWER_KEY_ETH_TYPE, protocol);
}
/*
* Traffic control filter encoding
*/
static ssize_t netlink_tfilter_msg_encode(int cmd, struct zebra_dplane_ctx *ctx,
void *data, size_t datalen)
{
enum dplane_op_e op = dplane_ctx_get_op(ctx);
struct nlsock *nl;
const char *kind_str = NULL;
struct rtattr *nest;
uint16_t priority;
uint16_t protocol;
struct {
struct nlmsghdr n;
struct tcmsg t;
char buf[0];
} *req = data;
if (datalen < sizeof(*req))
return 0;
nl = kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
memset(req, 0, sizeof(*req));
req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
if (op == DPLANE_OP_TC_FILTER_UPDATE)
req->n.nlmsg_flags |= NLM_F_REPLACE;
req->n.nlmsg_type = cmd;
req->n.nlmsg_pid = nl->snl.nl_pid;
req->t.tcm_family = AF_UNSPEC;
req->t.tcm_ifindex = dplane_ctx_get_ifindex(ctx);
priority = dplane_ctx_tc_filter_get_priority(ctx);
protocol = htons(dplane_ctx_tc_filter_get_eth_proto(ctx));
req->t.tcm_info = TC_H_MAKE(priority << 16, protocol);
req->t.tcm_handle = dplane_ctx_tc_filter_get_handle(ctx);
req->t.tcm_parent = TC_H_MAKE(TC_QDISC_MAJOR_ZEBRA, 0);
kind_str = dplane_ctx_tc_filter_get_kind_str(ctx);
if (op == DPLANE_OP_TC_FILTER_ADD || op == DPLANE_OP_TC_FILTER_UPDATE) {
nl_attr_put(&req->n, datalen, TCA_KIND, kind_str,
strlen(kind_str) + 1);
zlog_debug(
"netlink tfilter encoder: op: %s priority: %u protocol: %u kind: %s handle: %u filter_bm: %u ip_proto: %u",
op == DPLANE_OP_TC_FILTER_UPDATE ? "update" : "add",
priority, protocol, kind_str,
dplane_ctx_tc_filter_get_handle(ctx),
dplane_ctx_tc_filter_get_filter_bm(ctx),
dplane_ctx_tc_filter_get_ip_proto(ctx));
nest = nl_attr_nest(&req->n, datalen, TCA_OPTIONS);
switch (dplane_ctx_tc_filter_get_kind(ctx)) {
case TC_FILTER_FLOWER: {
netlink_tfilter_flower_put_options(&req->n, datalen,
ctx);
break;
}
default:
break;
}
nl_attr_nest_end(&req->n, nest);
}
return NLMSG_ALIGN(req->n.nlmsg_len);
}
static ssize_t netlink_newqdisc_msg_encoder(struct zebra_dplane_ctx *ctx,
void *buf, size_t buflen)
{
return netlink_qdisc_msg_encode(RTM_NEWQDISC, ctx, buf, buflen);
}
static ssize_t netlink_delqdisc_msg_encoder(struct zebra_dplane_ctx *ctx,
void *buf, size_t buflen)
{
return netlink_qdisc_msg_encode(RTM_DELQDISC, ctx, buf, buflen);
}
static ssize_t netlink_newtclass_msg_encoder(struct zebra_dplane_ctx *ctx,
void *buf, size_t buflen)
{
return netlink_tclass_msg_encode(RTM_NEWTCLASS, ctx, buf, buflen);
}
static ssize_t netlink_deltclass_msg_encoder(struct zebra_dplane_ctx *ctx,
void *buf, size_t buflen)
{
return netlink_tclass_msg_encode(RTM_DELTCLASS, ctx, buf, buflen);
}
static ssize_t netlink_newtfilter_msg_encoder(struct zebra_dplane_ctx *ctx,
void *buf, size_t buflen)
{
return netlink_tfilter_msg_encode(RTM_NEWTFILTER, ctx, buf, buflen);
}
static ssize_t netlink_deltfilter_msg_encoder(struct zebra_dplane_ctx *ctx,
void *buf, size_t buflen)
{
return netlink_tfilter_msg_encode(RTM_DELTFILTER, ctx, buf, buflen);
}
enum netlink_msg_status
netlink_put_tc_qdisc_update_msg(struct nl_batch *bth,
struct zebra_dplane_ctx *ctx)
{
enum dplane_op_e op;
enum netlink_msg_status ret;
op = dplane_ctx_get_op(ctx);
if (op == DPLANE_OP_TC_QDISC_INSTALL) {
ret = netlink_batch_add_msg(
bth, ctx, netlink_newqdisc_msg_encoder, false);
} else if (op == DPLANE_OP_TC_QDISC_UNINSTALL) {
ret = netlink_batch_add_msg(
bth, ctx, netlink_delqdisc_msg_encoder, false);
} else {
return FRR_NETLINK_ERROR;
}
return ret;
}
enum netlink_msg_status
netlink_put_tc_class_update_msg(struct nl_batch *bth,
struct zebra_dplane_ctx *ctx)
{
enum dplane_op_e op;
enum netlink_msg_status ret;
op = dplane_ctx_get_op(ctx);
if (op == DPLANE_OP_TC_CLASS_ADD || op == DPLANE_OP_TC_CLASS_UPDATE) {
ret = netlink_batch_add_msg(
bth, ctx, netlink_newtclass_msg_encoder, false);
} else if (op == DPLANE_OP_TC_CLASS_DELETE) {
ret = netlink_batch_add_msg(
bth, ctx, netlink_deltclass_msg_encoder, false);
} else {
return FRR_NETLINK_ERROR;
}
return ret;
}
enum netlink_msg_status
netlink_put_tc_filter_update_msg(struct nl_batch *bth,
struct zebra_dplane_ctx *ctx)
{
enum dplane_op_e op;
enum netlink_msg_status ret;
op = dplane_ctx_get_op(ctx);
if (op == DPLANE_OP_TC_FILTER_ADD) {
ret = netlink_batch_add_msg(
bth, ctx, netlink_newtfilter_msg_encoder, false);
} else if (op == DPLANE_OP_TC_FILTER_UPDATE) {
/*
* Replace will fail if either filter type or the number of
* filter options is changed, so DEL then NEW
*
* TFILTER may have refs to TCLASS.
*/
(void)netlink_batch_add_msg(
bth, ctx, netlink_deltfilter_msg_encoder, false);
ret = netlink_batch_add_msg(
bth, ctx, netlink_newtfilter_msg_encoder, false);
} else if (op == DPLANE_OP_TC_FILTER_DELETE) {
ret = netlink_batch_add_msg(
bth, ctx, netlink_deltfilter_msg_encoder, false);
} else {
return FRR_NETLINK_ERROR;
}
return ret;
}
/*
* Request filters from the kernel
*/
static int netlink_request_filters(struct zebra_ns *zns, int family, int type,
ifindex_t ifindex)
{
struct {
struct nlmsghdr n;
struct tcmsg tc;
} req;
memset(&req, 0, sizeof(req));
req.n.nlmsg_type = type;
req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
req.tc.tcm_family = family;
req.tc.tcm_ifindex = ifindex;
return netlink_request(&zns->netlink_cmd, &req);
}
/*
* Request queue discipline from the kernel
*/
static int netlink_request_qdiscs(struct zebra_ns *zns, int family, int type)
{
struct {
struct nlmsghdr n;
struct tcmsg tc;
} req;
memset(&req, 0, sizeof(req));
req.n.nlmsg_type = type;
req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
req.tc.tcm_family = family;
return netlink_request(&zns->netlink_cmd, &req);
}
int netlink_qdisc_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
{
struct tcmsg *tcm;
struct zebra_tc_qdisc qdisc = {};
enum tc_qdisc_kind kind = TC_QDISC_UNSPEC;
const char *kind_str = "Unknown";
int len;
struct rtattr *tb[TCA_MAX + 1];
frrtrace(3, frr_zebra, netlink_tc_qdisc_change, h, ns_id, startup);
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct tcmsg));
if (len < 0) {
zlog_err(
"%s: Message received from netlink is of a broken size %d %zu",
__func__, h->nlmsg_len,
(size_t)NLMSG_LENGTH(sizeof(struct tcmsg)));
return -1;
}
tcm = NLMSG_DATA(h);
netlink_parse_rtattr(tb, TCA_MAX, TCA_RTA(tcm), len);
if (RTA_DATA(tb[TCA_KIND])) {
kind_str = (const char *)RTA_DATA(tb[TCA_KIND]);
kind = tc_qdisc_str2kind(kind_str);
}
qdisc.qdisc.ifindex = tcm->tcm_ifindex;
switch (kind) {
case TC_QDISC_NOQUEUE:
/* "noqueue" is the default qdisc */
break;
case TC_QDISC_HTB:
case TC_QDISC_UNSPEC:
break;
}
if (tb[TCA_OPTIONS] != NULL) {
struct rtattr *options[TCA_HTB_MAX + 1];
netlink_parse_rtattr_nested(options, TCA_HTB_MAX,
tb[TCA_OPTIONS]);
/* TODO: more details */
/* struct tc_htb_glob *glob = RTA_DATA(options[TCA_HTB_INIT]);
*/
}
if (h->nlmsg_type == RTM_NEWQDISC) {
if (startup &&
TC_H_MAJ(tcm->tcm_handle) == TC_QDISC_MAJOR_ZEBRA) {
enum zebra_dplane_result ret;
ret = dplane_tc_qdisc_uninstall(&qdisc);
zlog_debug("%s: %s leftover qdisc: ifindex %d kind %s",
__func__,
((ret == ZEBRA_DPLANE_REQUEST_FAILURE)
? "Failed to remove"
: "Removed"),
qdisc.qdisc.ifindex, kind_str);
}
}
return 0;
}
int netlink_tclass_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
{
struct tcmsg *tcm;
int len;
struct rtattr *tb[TCA_MAX + 1];
frrtrace(3, frr_zebra, netlink_tc_class_change, h, ns_id, startup);
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct tcmsg));
if (len < 0) {
zlog_err(
"%s: Message received from netlink is of a broken size %d %zu",
__func__, h->nlmsg_len,
(size_t)NLMSG_LENGTH(sizeof(struct tcmsg)));
return -1;
}
tcm = NLMSG_DATA(h);
netlink_parse_rtattr(tb, TCA_MAX, TCA_RTA(tcm), len);
if (tb[TCA_OPTIONS] != NULL) {
struct rtattr *options[TCA_HTB_MAX + 1];
netlink_parse_rtattr_nested(options, TCA_HTB_MAX,
tb[TCA_OPTIONS]);
/* TODO: more details */
/* struct tc_htb_opt *opt = RTA_DATA(options[TCA_HTB_PARMS]); */
}
return 0;
}
int netlink_tfilter_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
{
struct tcmsg *tcm;
int len;
struct rtattr *tb[TCA_MAX + 1];
frrtrace(3, frr_zebra, netlink_tc_filter_change, h, ns_id, startup);
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct tcmsg));
if (len < 0) {
zlog_err(
"%s: Message received from netlink is of a broken size %d %zu",
__func__, h->nlmsg_len,
(size_t)NLMSG_LENGTH(sizeof(struct tcmsg)));
return -1;
}
tcm = NLMSG_DATA(h);
netlink_parse_rtattr(tb, TCA_MAX, TCA_RTA(tcm), len);
return 0;
}
int netlink_qdisc_read(struct zebra_ns *zns)
{
int ret;
struct zebra_dplane_info dp_info;
zebra_dplane_info_from_zns(&dp_info, zns, true);
ret = netlink_request_qdiscs(zns, AF_UNSPEC, RTM_GETQDISC);
if (ret < 0)
return ret;
ret = netlink_parse_info(netlink_qdisc_change, &zns->netlink_cmd,
&dp_info, 0, true);
if (ret < 0)
return ret;
return 0;
}
int netlink_tfilter_read_for_interface(struct zebra_ns *zns, ifindex_t ifindex)
{
int ret;
struct zebra_dplane_info dp_info;
zebra_dplane_info_from_zns(&dp_info, zns, true);
ret = netlink_request_filters(zns, AF_UNSPEC, RTM_GETTFILTER, ifindex);
if (ret < 0)
return ret;
ret = netlink_parse_info(netlink_tfilter_change, &zns->netlink_cmd,
&dp_info, 0, true);
if (ret < 0)
return ret;
return 0;
}
#endif /* HAVE_NETLINK */
|