Debian Codegraph @

src/network/tc/netem.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later
2 * Copyright © 2019 VMware, Inc. */
3
4 #include <linux/pkt_sched.h>
5 #include "sd-netlink.h"
6
7 #include "log-link.h"
8 #include "netem.h"
9 #include "networkd-link.h"
10 #include "parse-util.h"
11 #include "qdisc.h"
12 #include "string-util.h"
13 #include "strv.h"
14 #include "tc-util.h"
15
16 static int network_emulator_fill_message(Link *link, QDisc *qdisc, sd_netlink_message *req) {
17 NetworkEmulator *ne;
18 int r;
19
20 assert(link);
21 assert(qdisc);
22 assert(req);
23
24 assert_se(ne = NETEM(qdisc));
25
26 struct tc_netem_qopt opt = {
27 .limit = ne->limit > 0 ? ne->limit : 1000,
28 .loss = ne->loss,
29 .duplicate = ne->duplicate,
30 };
31
32 if (ne->delay != USEC_INFINITY) {
33 r = tc_time_to_tick(ne->delay, &opt.latency);
34 if (r < 0)
35 return log_link_error_errno(link, r, "Failed to calculate latency in TCA_OPTION: %m");
36 }
37
38 if (ne->jitter != USEC_INFINITY) {
39 r = tc_time_to_tick(ne->jitter, &opt.jitter);
40 if (r < 0)
41 return log_link_error_errno(link, r, "Failed to calculate jitter in TCA_OPTION: %m");
42 }
43
44 r = sd_netlink_message_append_data(req, TCA_OPTIONS, &opt, sizeof(opt));
45 if (r < 0)
46 return r;
47
48 return 0;
49 }
50
51 int config_parse_netem_delay(
52 const char *unit,
53 const char *filename,
54 unsigned line,
55 const char *section,
56 unsigned section_line,
57 const char *lvalue,
58 int ltype,
59 const char *rvalue,
60 void *data,
61 void *userdata) {
62
63 _cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL;
64 Network *network = ASSERT_PTR(data);
65 NetworkEmulator *ne;
66 usec_t u;
67 int r;
68
69 assert(filename);
70 assert(lvalue);
71 assert(rvalue);
72
73 r = qdisc_new_static(QDISC_KIND_NETEM, network, filename, section_line, &qdisc);
74 if (r == -ENOMEM)
75 return log_oom();
76 if (r < 0) {
77 log_syntax(unit, LOG_WARNING, filename, line, r,
78 "More than one kind of queueing discipline, ignoring assignment: %m");
79 return 0;
80 }
81
82 ne = NETEM(qdisc);
83
84 if (isempty(rvalue)) {
85 if (STR_IN_SET(lvalue, "DelaySec", "NetworkEmulatorDelaySec"))
86 ne->delay = USEC_INFINITY;
87 else if (STR_IN_SET(lvalue, "DelayJitterSec", "NetworkEmulatorDelayJitterSec"))
88 ne->jitter = USEC_INFINITY;
89
90 TAKE_PTR(qdisc);
91 return 0;
92 }
93
94 r = parse_sec(rvalue, &u);
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 (STR_IN_SET(lvalue, "DelaySec", "NetworkEmulatorDelaySec"))
103 ne->delay = u;
104 else if (STR_IN_SET(lvalue, "DelayJitterSec", "NetworkEmulatorDelayJitterSec"))
105 ne->jitter = u;
106
107 TAKE_PTR(qdisc);
108
109 return 0;
110 }
111
112 int config_parse_netem_rate(
113 const char *unit,
114 const char *filename,
115 unsigned line,
116 const char *section,
117 unsigned section_line,
118 const char *lvalue,
119 int ltype,
120 const char *rvalue,
121 void *data,
122 void *userdata) {
123
124 _cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL;
125 Network *network = ASSERT_PTR(data);
126 NetworkEmulator *ne;
127 uint32_t rate;
128 int r;
129
130 assert(filename);
131 assert(lvalue);
132 assert(rvalue);
133
134 r = qdisc_new_static(QDISC_KIND_NETEM, network, filename, section_line, &qdisc);
135 if (r == -ENOMEM)
136 return log_oom();
137 if (r < 0) {
138 log_syntax(unit, LOG_WARNING, filename, line, r,
139 "More than one kind of queueing discipline, ignoring assignment: %m");
140 return 0;
141 }
142
143 ne = NETEM(qdisc);
144
145 if (isempty(rvalue)) {
146 if (STR_IN_SET(lvalue, "LossRate", "NetworkEmulatorLossRate"))
147 ne->loss = 0;
148 else if (STR_IN_SET(lvalue, "DuplicateRate", "NetworkEmulatorDuplicateRate"))
149 ne->duplicate = 0;
150
151 TAKE_PTR(qdisc);
152 return 0;
153 }
154
155 r = parse_tc_percent(rvalue, &rate);
156 if (r < 0) {
157 log_syntax(unit, LOG_WARNING, filename, line, r,
158 "Failed to parse '%s=', ignoring assignment: %s",
159 lvalue, rvalue);
160 return 0;
161 }
162
163 if (STR_IN_SET(lvalue, "LossRate", "NetworkEmulatorLossRate"))
164 ne->loss = rate;
165 else if (STR_IN_SET(lvalue, "DuplicateRate", "NetworkEmulatorDuplicateRate"))
166 ne->duplicate = rate;
167
168 TAKE_PTR(qdisc);
169 return 0;
170 }
171
172 int config_parse_netem_packet_limit(
173 const char *unit,
174 const char *filename,
175 unsigned line,
176 const char *section,
177 unsigned section_line,
178 const char *lvalue,
179 int ltype,
180 const char *rvalue,
181 void *data,
182 void *userdata) {
183
184 _cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL;
185 Network *network = ASSERT_PTR(data);
186 NetworkEmulator *ne;
187 int r;
188
189 assert(filename);
190 assert(lvalue);
191 assert(rvalue);
192
193 r = qdisc_new_static(QDISC_KIND_NETEM, network, filename, section_line, &qdisc);
194 if (r == -ENOMEM)
195 return log_oom();
196 if (r < 0) {
197 log_syntax(unit, LOG_WARNING, filename, line, r,
198 "More than one kind of queueing discipline, ignoring assignment: %m");
199 return 0;
200 }
201
202 ne = NETEM(qdisc);
203
204 if (isempty(rvalue)) {
205 ne->limit = 0;
206
207 TAKE_PTR(qdisc);
208 return 0;
209 }
210
211 r = safe_atou(rvalue, &ne->limit);
212 if (r < 0) {
213 log_syntax(unit, LOG_WARNING, filename, line, r,
214 "Failed to parse '%s=', ignoring assignment: %s",
215 lvalue, rvalue);
216 return 0;
217 }
218
219 TAKE_PTR(qdisc);
220 return 0;
221 }
222
223 const QDiscVTable netem_vtable = {
224 .object_size = sizeof(NetworkEmulator),
225 .tca_kind = "netem",
226 .fill_message = network_emulator_fill_message,
227 };