Debian Codegraph @

src/network/tc/qfq.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later
2 * Copyright © 2020 VMware, Inc. */
3
4 #include <linux/pkt_sched.h>
5 #include "sd-netlink.h"
6
7 #include "log.h"
8 #include "parse-util.h"
9 #include "qdisc.h"
10 #include "qfq.h"
11 #include "string-util.h"
12
13 #define QFQ_MAX_WEIGHT (1 << 10)
14 #define QFQ_MIN_MAX_PACKET 512
15 #define QFQ_MAX_MAX_PACKET (1 << 16)
16
17 const QDiscVTable qfq_vtable = {
18 .object_size = sizeof(QuickFairQueueing),
19 .tca_kind = "qfq",
20 };
21
22 static int quick_fair_queueing_class_fill_message(Link *link, TClass *tclass, sd_netlink_message *req) {
23 QuickFairQueueingClass *qfq;
24 int r;
25
26 assert(link);
27 assert(tclass);
28 assert(req);
29
30 assert_se(qfq = TCLASS_TO_QFQ(tclass));
31
32 r = sd_netlink_message_open_container_union(req, TCA_OPTIONS, "qfq");
33 if (r < 0)
34 return r;
35
36 if (qfq->weight > 0) {
37 r = sd_netlink_message_append_u32(req, TCA_QFQ_WEIGHT, qfq->weight);
38 if (r < 0)
39 return r;
40 }
41
42 if (qfq->max_packet > 0) {
43 r = sd_netlink_message_append_u32(req, TCA_QFQ_LMAX, qfq->max_packet);
44 if (r < 0)
45 return r;
46 }
47
48 r = sd_netlink_message_close_container(req);
49 if (r < 0)
50 return r;
51
52 return 0;
53 }
54
55 int config_parse_qfq_weight(
56 const char *unit,
57 const char *filename,
58 unsigned line,
59 const char *section,
60 unsigned section_line,
61 const char *lvalue,
62 int ltype,
63 const char *rvalue,
64 void *data,
65 void *userdata) {
66
67 _cleanup_(tclass_unref_or_set_invalidp) TClass *tclass = NULL;
68 QuickFairQueueingClass *qfq;
69 Network *network = ASSERT_PTR(data);
70 uint32_t v;
71 int r;
72
73 assert(filename);
74 assert(lvalue);
75 assert(rvalue);
76
77 r = tclass_new_static(TCLASS_KIND_QFQ, network, filename, section_line, &tclass);
78 if (r == -ENOMEM)
79 return log_oom();
80 if (r < 0) {
81 log_syntax(unit, LOG_WARNING, filename, line, r,
82 "Failed to create traffic control class, ignoring assignment: %m");
83 return 0;
84 }
85
86 qfq = TCLASS_TO_QFQ(tclass);
87
88 if (isempty(rvalue)) {
89 qfq->weight = 0;
90 TAKE_PTR(tclass);
91 return 0;
92 }
93
94 r = safe_atou32(rvalue, &v);
95 if (r < 0) {
96 log_syntax(unit, LOG_WARNING, filename, line, r,
97 "Failed to parse '%s=', ignoring assignment: %s",
98 lvalue, rvalue);
99 return 0;
100 }
101
102 if (v == 0 || v > QFQ_MAX_WEIGHT) {
103 log_syntax(unit, LOG_WARNING, filename, line, 0,
104 "Invalid '%s=', ignoring assignment: %s",
105 lvalue, rvalue);
106 return 0;
107 }
108
109 qfq->weight = v;
110 TAKE_PTR(tclass);
111
112 return 0;
113 }
114
115 int config_parse_qfq_max_packet(
116 const char *unit,
117 const char *filename,
118 unsigned line,
119 const char *section,
120 unsigned section_line,
121 const char *lvalue,
122 int ltype,
123 const char *rvalue,
124 void *data,
125 void *userdata) {
126
127 _cleanup_(tclass_unref_or_set_invalidp) TClass *tclass = NULL;
128 QuickFairQueueingClass *qfq;
129 Network *network = ASSERT_PTR(data);
130 uint64_t v;
131 int r;
132
133 assert(filename);
134 assert(lvalue);
135 assert(rvalue);
136
137 r = tclass_new_static(TCLASS_KIND_QFQ, network, filename, section_line, &tclass);
138 if (r == -ENOMEM)
139 return log_oom();
140 if (r < 0) {
141 log_syntax(unit, LOG_WARNING, filename, line, r,
142 "Failed to create traffic control class, ignoring assignment: %m");
143 return 0;
144 }
145
146 qfq = TCLASS_TO_QFQ(tclass);
147
148 if (isempty(rvalue)) {
149 qfq->max_packet = 0;
150 TAKE_PTR(tclass);
151 return 0;
152 }
153
154 r = parse_size(rvalue, 1024, &v);
155 if (r < 0) {
156 log_syntax(unit, LOG_WARNING, filename, line, r,
157 "Failed to parse '%s=', ignoring assignment: %s",
158 lvalue, rvalue);
159 return 0;
160 }
161
162 if (v < QFQ_MIN_MAX_PACKET || v > QFQ_MAX_MAX_PACKET) {
163 log_syntax(unit, LOG_WARNING, filename, line, 0,
164 "Invalid '%s=', ignoring assignment: %s",
165 lvalue, rvalue);
166 return 0;
167 }
168
169 qfq->max_packet = (uint32_t) v;
170 TAKE_PTR(tclass);
171
172 return 0;
173 }
174
175 const TClassVTable qfq_tclass_vtable = {
176 .object_size = sizeof(QuickFairQueueingClass),
177 .tca_kind = "qfq",
178 .fill_message = quick_fair_queueing_class_fill_message,
179 };