| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
* Copyright © 2019 VMware, Inc. */ |
| 3 |
|
| 4 |
#include <linux/pkt_sched.h> |
| 5 |
|
| 6 |
#include "sd-netlink.h" |
| 7 |
|
| 8 |
#include "log-link.h" |
| 9 |
#include "networkd-link.h" |
| 10 |
#include "parse-util.h" |
| 11 |
#include "strv.h" |
| 12 |
#include "tbf.h" |
| 13 |
#include "tc-util.h" |
| 14 |
#include "string-util.h" |
| 15 |
|
| 16 |
static int token_bucket_filter_fill_message(Link *link, QDisc *qdisc, sd_netlink_message *req) { |
| 17 |
uint32_t rtab[256], ptab[256]; |
| 18 |
TokenBucketFilter *tbf; |
| 19 |
int r; |
| 20 |
|
| 21 |
assert(link); |
| 22 |
assert(qdisc); |
| 23 |
assert(req); |
| 24 |
|
| 25 |
assert_se(tbf = TBF(qdisc)); |
| 26 |
|
| 27 |
struct tc_tbf_qopt opt = { |
| 28 |
.rate.rate = tbf->rate >= (1ULL << 32) ? ~0U : tbf->rate, |
| 29 |
.peakrate.rate = tbf->peak_rate >= (1ULL << 32) ? ~0U : tbf->peak_rate, |
| 30 |
.rate.mpu = tbf->mpu, |
| 31 |
}; |
| 32 |
|
| 33 |
if (tbf->limit > 0) |
| 34 |
opt.limit = tbf->limit; |
| 35 |
else { |
| 36 |
double lim, lim2; |
| 37 |
|
| 38 |
lim = tbf->rate * (double) tbf->latency / USEC_PER_SEC + tbf->burst; |
| 39 |
if (tbf->peak_rate > 0) { |
| 40 |
lim2 = tbf->peak_rate * (double) tbf->latency / USEC_PER_SEC + tbf->mtu; |
| 41 |
lim = MIN(lim, lim2); |
| 42 |
} |
| 43 |
opt.limit = lim; |
| 44 |
} |
| 45 |
|
| 46 |
r = tc_fill_ratespec_and_table(&opt.rate, rtab, tbf->mtu); |
| 47 |
if (r < 0) |
| 48 |
return log_link_debug_errno(link, r, "Failed to calculate ratespec: %m"); |
| 49 |
|
| 50 |
r = tc_transmit_time(opt.rate.rate, tbf->burst, &opt.buffer); |
| 51 |
if (r < 0) |
| 52 |
return log_link_debug_errno(link, r, "Failed to calculate buffer size: %m"); |
| 53 |
|
| 54 |
if (opt.peakrate.rate > 0) { |
| 55 |
opt.peakrate.mpu = tbf->mpu; |
| 56 |
|
| 57 |
r = tc_fill_ratespec_and_table(&opt.peakrate, ptab, tbf->mtu); |
| 58 |
if (r < 0) |
| 59 |
return log_link_debug_errno(link, r, "Failed to calculate ratespec: %m"); |
| 60 |
|
| 61 |
r = tc_transmit_time(opt.peakrate.rate, tbf->mtu, &opt.mtu); |
| 62 |
if (r < 0) |
| 63 |
return log_link_debug_errno(link, r, "Failed to calculate mtu size: %m"); |
| 64 |
} |
| 65 |
|
| 66 |
r = sd_netlink_message_open_container_union(req, TCA_OPTIONS, "tbf"); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
|
| 70 |
r = sd_netlink_message_append_data(req, TCA_TBF_PARMS, &opt, sizeof(opt)); |
| 71 |
if (r < 0) |
| 72 |
return r; |
| 73 |
|
| 74 |
r = sd_netlink_message_append_data(req, TCA_TBF_BURST, &tbf->burst, sizeof(tbf->burst)); |
| 75 |
if (r < 0) |
| 76 |
return r; |
| 77 |
|
| 78 |
if (tbf->rate >= (1ULL << 32)) { |
| 79 |
r = sd_netlink_message_append_u64(req, TCA_TBF_RATE64, tbf->rate); |
| 80 |
if (r < 0) |
| 81 |
return r; |
| 82 |
} |
| 83 |
|
| 84 |
r = sd_netlink_message_append_data(req, TCA_TBF_RTAB, rtab, sizeof(rtab)); |
| 85 |
if (r < 0) |
| 86 |
return r; |
| 87 |
|
| 88 |
if (opt.peakrate.rate > 0) { |
| 89 |
if (tbf->peak_rate >= (1ULL << 32)) { |
| 90 |
r = sd_netlink_message_append_u64(req, TCA_TBF_PRATE64, tbf->peak_rate); |
| 91 |
if (r < 0) |
| 92 |
return r; |
| 93 |
} |
| 94 |
|
| 95 |
r = sd_netlink_message_append_u32(req, TCA_TBF_PBURST, tbf->mtu); |
| 96 |
if (r < 0) |
| 97 |
return r; |
| 98 |
|
| 99 |
r = sd_netlink_message_append_data(req, TCA_TBF_PTAB, ptab, sizeof(ptab)); |
| 100 |
if (r < 0) |
| 101 |
return r; |
| 102 |
} |
| 103 |
|
| 104 |
r = sd_netlink_message_close_container(req); |
| 105 |
if (r < 0) |
| 106 |
return r; |
| 107 |
|
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
int config_parse_tbf_size( |
| 112 |
const char *unit, |
| 113 |
const char *filename, |
| 114 |
unsigned line, |
| 115 |
const char *section, |
| 116 |
unsigned section_line, |
| 117 |
const char *lvalue, |
| 118 |
int ltype, |
| 119 |
const char *rvalue, |
| 120 |
void *data, |
| 121 |
void *userdata) { |
| 122 |
|
| 123 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 124 |
Network *network = ASSERT_PTR(data); |
| 125 |
TokenBucketFilter *tbf; |
| 126 |
uint64_t k; |
| 127 |
int r; |
| 128 |
|
| 129 |
assert(filename); |
| 130 |
assert(lvalue); |
| 131 |
assert(rvalue); |
| 132 |
|
| 133 |
r = qdisc_new_static(QDISC_KIND_TBF, network, filename, section_line, &qdisc); |
| 134 |
if (r == -ENOMEM) |
| 135 |
return log_oom(); |
| 136 |
if (r < 0) { |
| 137 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 138 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
tbf = TBF(qdisc); |
| 143 |
|
| 144 |
if (isempty(rvalue)) { |
| 145 |
if (STR_IN_SET(lvalue, "BurstBytes", "Burst")) |
| 146 |
tbf->burst = 0; |
| 147 |
else if (STR_IN_SET(lvalue, "LimitBytes", "LimitSize")) |
| 148 |
tbf->limit = 0; |
| 149 |
else if (streq(lvalue, "MTUBytes")) |
| 150 |
tbf->mtu = 0; |
| 151 |
else if (streq(lvalue, "MPUBytes")) |
| 152 |
tbf->mpu = 0; |
| 153 |
else |
| 154 |
assert_not_reached(); |
| 155 |
|
| 156 |
TAKE_PTR(qdisc); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
r = parse_size(rvalue, 1024, &k); |
| 161 |
if (r < 0) { |
| 162 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 163 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 164 |
lvalue, rvalue); |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
if (STR_IN_SET(lvalue, "BurstBytes", "Burst")) |
| 169 |
tbf->burst = k; |
| 170 |
else if (STR_IN_SET(lvalue, "LimitBytes", "LimitSize")) |
| 171 |
tbf->limit = k; |
| 172 |
else if (streq(lvalue, "MPUBytes")) |
| 173 |
tbf->mpu = k; |
| 174 |
else if (streq(lvalue, "MTUBytes")) |
| 175 |
tbf->mtu = k; |
| 176 |
else |
| 177 |
assert_not_reached(); |
| 178 |
|
| 179 |
TAKE_PTR(qdisc); |
| 180 |
|
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
int config_parse_tbf_rate( |
| 185 |
const char *unit, |
| 186 |
const char *filename, |
| 187 |
unsigned line, |
| 188 |
const char *section, |
| 189 |
unsigned section_line, |
| 190 |
const char *lvalue, |
| 191 |
int ltype, |
| 192 |
const char *rvalue, |
| 193 |
void *data, |
| 194 |
void *userdata) { |
| 195 |
|
| 196 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 197 |
Network *network = ASSERT_PTR(data); |
| 198 |
TokenBucketFilter *tbf; |
| 199 |
uint64_t k, *p; |
| 200 |
int r; |
| 201 |
|
| 202 |
assert(filename); |
| 203 |
assert(lvalue); |
| 204 |
assert(rvalue); |
| 205 |
|
| 206 |
r = qdisc_new_static(QDISC_KIND_TBF, network, filename, section_line, &qdisc); |
| 207 |
if (r == -ENOMEM) |
| 208 |
return log_oom(); |
| 209 |
if (r < 0) { |
| 210 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 211 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
tbf = TBF(qdisc); |
| 216 |
if (streq(lvalue, "Rate")) |
| 217 |
p = &tbf->rate; |
| 218 |
else if (streq(lvalue, "PeakRate")) |
| 219 |
p = &tbf->peak_rate; |
| 220 |
else |
| 221 |
assert_not_reached(); |
| 222 |
|
| 223 |
if (isempty(rvalue)) { |
| 224 |
*p = 0; |
| 225 |
|
| 226 |
TAKE_PTR(qdisc); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
r = parse_size(rvalue, 1000, &k); |
| 231 |
if (r < 0) { |
| 232 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 233 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 234 |
lvalue, rvalue); |
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
*p = k / 8; |
| 239 |
|
| 240 |
qdisc = NULL; |
| 241 |
|
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
int config_parse_tbf_latency( |
| 246 |
const char *unit, |
| 247 |
const char *filename, |
| 248 |
unsigned line, |
| 249 |
const char *section, |
| 250 |
unsigned section_line, |
| 251 |
const char *lvalue, |
| 252 |
int ltype, |
| 253 |
const char *rvalue, |
| 254 |
void *data, |
| 255 |
void *userdata) { |
| 256 |
|
| 257 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 258 |
Network *network = ASSERT_PTR(data); |
| 259 |
TokenBucketFilter *tbf; |
| 260 |
usec_t u; |
| 261 |
int r; |
| 262 |
|
| 263 |
assert(filename); |
| 264 |
assert(lvalue); |
| 265 |
assert(rvalue); |
| 266 |
|
| 267 |
r = qdisc_new_static(QDISC_KIND_TBF, network, filename, section_line, &qdisc); |
| 268 |
if (r == -ENOMEM) |
| 269 |
return log_oom(); |
| 270 |
if (r < 0) { |
| 271 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 272 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
tbf = TBF(qdisc); |
| 277 |
|
| 278 |
if (isempty(rvalue)) { |
| 279 |
tbf->latency = 0; |
| 280 |
|
| 281 |
qdisc = NULL; |
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
r = parse_sec(rvalue, &u); |
| 286 |
if (r < 0) { |
| 287 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 288 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 289 |
lvalue, rvalue); |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
tbf->latency = u; |
| 294 |
|
| 295 |
qdisc = NULL; |
| 296 |
|
| 297 |
return 0; |
| 298 |
} |
| 299 |
|
| 300 |
static int token_bucket_filter_verify(QDisc *qdisc) { |
| 301 |
TokenBucketFilter *tbf = TBF(qdisc); |
| 302 |
|
| 303 |
if (tbf->limit > 0 && tbf->latency > 0) |
| 304 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 305 |
"%s: Specifying both LimitBytes= and LatencySec= is not allowed. " |
| 306 |
"Ignoring [TokenBucketFilter] section from line %u.", |
| 307 |
qdisc->section->filename, qdisc->section->line); |
| 308 |
|
| 309 |
if (tbf->limit == 0 && tbf->latency == 0) |
| 310 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 311 |
"%s: Either LimitBytes= or LatencySec= is required. " |
| 312 |
"Ignoring [TokenBucketFilter] section from line %u.", |
| 313 |
qdisc->section->filename, qdisc->section->line); |
| 314 |
|
| 315 |
if (tbf->rate == 0) |
| 316 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 317 |
"%s: Rate= is mandatory. " |
| 318 |
"Ignoring [TokenBucketFilter] section from line %u.", |
| 319 |
qdisc->section->filename, qdisc->section->line); |
| 320 |
|
| 321 |
if (tbf->burst == 0) |
| 322 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 323 |
"%s: BurstBytes= is mandatory. " |
| 324 |
"Ignoring [TokenBucketFilter] section from line %u.", |
| 325 |
qdisc->section->filename, qdisc->section->line); |
| 326 |
|
| 327 |
if (tbf->peak_rate > 0 && tbf->mtu == 0) |
| 328 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 329 |
"%s: MTUBytes= is mandatory when PeakRate= is specified. " |
| 330 |
"Ignoring [TokenBucketFilter] section from line %u.", |
| 331 |
qdisc->section->filename, qdisc->section->line); |
| 332 |
|
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
const QDiscVTable tbf_vtable = { |
| 337 |
.object_size = sizeof(TokenBucketFilter), |
| 338 |
.tca_kind = "tbf", |
| 339 |
.fill_message = token_bucket_filter_fill_message, |
| 340 |
.verify = token_bucket_filter_verify |
| 341 |
}; |