| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/pkt_sched.h> |
| 4 |
|
| 5 |
#include "sd-netlink.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "ets.h" |
| 9 |
#include "extract-word.h" |
| 10 |
#include "log.h" |
| 11 |
#include "parse-util.h" |
| 12 |
#include "qdisc.h" |
| 13 |
#include "string-util.h" |
| 14 |
|
| 15 |
static int enhanced_transmission_selection_fill_message(Link *link, QDisc *qdisc, sd_netlink_message *req) { |
| 16 |
EnhancedTransmissionSelection *ets; |
| 17 |
int r; |
| 18 |
|
| 19 |
assert(link); |
| 20 |
assert(qdisc); |
| 21 |
assert(req); |
| 22 |
|
| 23 |
assert_se(ets = ETS(qdisc)); |
| 24 |
|
| 25 |
r = sd_netlink_message_open_container_union(req, TCA_OPTIONS, "ets"); |
| 26 |
if (r < 0) |
| 27 |
return r; |
| 28 |
|
| 29 |
r = sd_netlink_message_append_u8(req, TCA_ETS_NBANDS, ets->n_bands); |
| 30 |
if (r < 0) |
| 31 |
return r; |
| 32 |
|
| 33 |
if (ets->n_strict > 0) { |
| 34 |
r = sd_netlink_message_append_u8(req, TCA_ETS_NSTRICT, ets->n_strict); |
| 35 |
if (r < 0) |
| 36 |
return r; |
| 37 |
} |
| 38 |
|
| 39 |
if (ets->n_quanta > 0) { |
| 40 |
r = sd_netlink_message_open_container(req, TCA_ETS_QUANTA); |
| 41 |
if (r < 0) |
| 42 |
return r; |
| 43 |
|
| 44 |
for (unsigned i = 0; i < ets->n_quanta; i++) { |
| 45 |
r = sd_netlink_message_append_u32(req, TCA_ETS_QUANTA_BAND, ets->quanta[i]); |
| 46 |
if (r < 0) |
| 47 |
return r; |
| 48 |
} |
| 49 |
|
| 50 |
r = sd_netlink_message_close_container(req); |
| 51 |
if (r < 0) |
| 52 |
return r; |
| 53 |
} |
| 54 |
|
| 55 |
if (ets->n_prio > 0) { |
| 56 |
r = sd_netlink_message_open_container(req, TCA_ETS_PRIOMAP); |
| 57 |
if (r < 0) |
| 58 |
return r; |
| 59 |
|
| 60 |
for (unsigned i = 0; i < ets->n_prio; i++) { |
| 61 |
r = sd_netlink_message_append_u8(req, TCA_ETS_PRIOMAP_BAND, ets->prio[i]); |
| 62 |
if (r < 0) |
| 63 |
return r; |
| 64 |
} |
| 65 |
|
| 66 |
r = sd_netlink_message_close_container(req); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
} |
| 70 |
|
| 71 |
r = sd_netlink_message_close_container(req); |
| 72 |
if (r < 0) |
| 73 |
return r; |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
int config_parse_ets_u8( |
| 79 |
const char *unit, |
| 80 |
const char *filename, |
| 81 |
unsigned line, |
| 82 |
const char *section, |
| 83 |
unsigned section_line, |
| 84 |
const char *lvalue, |
| 85 |
int ltype, |
| 86 |
const char *rvalue, |
| 87 |
void *data, |
| 88 |
void *userdata) { |
| 89 |
|
| 90 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 91 |
EnhancedTransmissionSelection *ets; |
| 92 |
Network *network = ASSERT_PTR(data); |
| 93 |
uint8_t v, *p; |
| 94 |
int r; |
| 95 |
|
| 96 |
assert(filename); |
| 97 |
assert(lvalue); |
| 98 |
assert(rvalue); |
| 99 |
|
| 100 |
r = qdisc_new_static(QDISC_KIND_ETS, network, filename, section_line, &qdisc); |
| 101 |
if (r == -ENOMEM) |
| 102 |
return log_oom(); |
| 103 |
if (r < 0) { |
| 104 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 105 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
ets = ETS(qdisc); |
| 110 |
if (streq(lvalue, "Bands")) |
| 111 |
p = &ets->n_bands; |
| 112 |
else if (streq(lvalue, "StrictBands")) |
| 113 |
p = &ets->n_strict; |
| 114 |
else |
| 115 |
assert_not_reached(); |
| 116 |
|
| 117 |
if (isempty(rvalue)) { |
| 118 |
*p = 0; |
| 119 |
|
| 120 |
qdisc = NULL; |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
r = safe_atou8(rvalue, &v); |
| 125 |
if (r < 0) { |
| 126 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 127 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 128 |
lvalue, rvalue); |
| 129 |
return 0; |
| 130 |
} |
| 131 |
if (v > TCQ_ETS_MAX_BANDS) { |
| 132 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 133 |
"Invalid '%s='. The value must be <= %d, ignoring assignment: %s", |
| 134 |
lvalue, TCQ_ETS_MAX_BANDS, rvalue); |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
*p = v; |
| 139 |
qdisc = NULL; |
| 140 |
|
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
int config_parse_ets_quanta( |
| 145 |
const char *unit, |
| 146 |
const char *filename, |
| 147 |
unsigned line, |
| 148 |
const char *section, |
| 149 |
unsigned section_line, |
| 150 |
const char *lvalue, |
| 151 |
int ltype, |
| 152 |
const char *rvalue, |
| 153 |
void *data, |
| 154 |
void *userdata) { |
| 155 |
|
| 156 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 157 |
EnhancedTransmissionSelection *ets; |
| 158 |
Network *network = ASSERT_PTR(data); |
| 159 |
int r; |
| 160 |
|
| 161 |
assert(filename); |
| 162 |
assert(lvalue); |
| 163 |
assert(rvalue); |
| 164 |
|
| 165 |
r = qdisc_new_static(QDISC_KIND_ETS, network, filename, section_line, &qdisc); |
| 166 |
if (r == -ENOMEM) |
| 167 |
return log_oom(); |
| 168 |
if (r < 0) { |
| 169 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 170 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
ets = ETS(qdisc); |
| 175 |
|
| 176 |
if (isempty(rvalue)) { |
| 177 |
memzero(ets->quanta, sizeof(uint32_t) * TCQ_ETS_MAX_BANDS); |
| 178 |
ets->n_quanta = 0; |
| 179 |
|
| 180 |
qdisc = NULL; |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
for (const char *p = rvalue;;) { |
| 185 |
_cleanup_free_ char *word = NULL; |
| 186 |
uint64_t v; |
| 187 |
|
| 188 |
r = extract_first_word(&p, &word, NULL, 0); |
| 189 |
if (r == -ENOMEM) |
| 190 |
return log_oom(); |
| 191 |
if (r < 0) { |
| 192 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 193 |
"Failed to extract next value, ignoring: %m"); |
| 194 |
break; |
| 195 |
} |
| 196 |
if (r == 0) |
| 197 |
break; |
| 198 |
|
| 199 |
r = parse_size(word, 1024, &v); |
| 200 |
if (r < 0) { |
| 201 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 202 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 203 |
lvalue, word); |
| 204 |
continue; |
| 205 |
} |
| 206 |
if (v == 0 || v > UINT32_MAX) { |
| 207 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 208 |
"Invalid '%s=', ignoring assignment: %s", |
| 209 |
lvalue, word); |
| 210 |
continue; |
| 211 |
} |
| 212 |
if (ets->n_quanta >= TCQ_ETS_MAX_BANDS) { |
| 213 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 214 |
"Too many quanta in '%s=', ignoring assignment: %s", |
| 215 |
lvalue, word); |
| 216 |
continue; |
| 217 |
} |
| 218 |
|
| 219 |
ets->quanta[ets->n_quanta++] = v; |
| 220 |
} |
| 221 |
|
| 222 |
qdisc = NULL; |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
int config_parse_ets_prio( |
| 228 |
const char *unit, |
| 229 |
const char *filename, |
| 230 |
unsigned line, |
| 231 |
const char *section, |
| 232 |
unsigned section_line, |
| 233 |
const char *lvalue, |
| 234 |
int ltype, |
| 235 |
const char *rvalue, |
| 236 |
void *data, |
| 237 |
void *userdata) { |
| 238 |
|
| 239 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 240 |
EnhancedTransmissionSelection *ets; |
| 241 |
Network *network = ASSERT_PTR(data); |
| 242 |
int r; |
| 243 |
|
| 244 |
assert(filename); |
| 245 |
assert(lvalue); |
| 246 |
assert(rvalue); |
| 247 |
|
| 248 |
r = qdisc_new_static(QDISC_KIND_ETS, network, filename, section_line, &qdisc); |
| 249 |
if (r == -ENOMEM) |
| 250 |
return log_oom(); |
| 251 |
if (r < 0) { |
| 252 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 253 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
ets = ETS(qdisc); |
| 258 |
|
| 259 |
if (isempty(rvalue)) { |
| 260 |
memzero(ets->prio, sizeof(uint8_t) * (TC_PRIO_MAX + 1)); |
| 261 |
ets->n_prio = 0; |
| 262 |
|
| 263 |
qdisc = NULL; |
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
for (const char *p = rvalue;;) { |
| 268 |
_cleanup_free_ char *word = NULL; |
| 269 |
uint8_t v; |
| 270 |
|
| 271 |
r = extract_first_word(&p, &word, NULL, 0); |
| 272 |
if (r == -ENOMEM) |
| 273 |
return log_oom(); |
| 274 |
if (r < 0) { |
| 275 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 276 |
"Failed to extract next value, ignoring: %m"); |
| 277 |
break; |
| 278 |
} |
| 279 |
if (r == 0) |
| 280 |
break; |
| 281 |
|
| 282 |
r = safe_atou8(word, &v); |
| 283 |
if (r < 0) { |
| 284 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 285 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 286 |
lvalue, word); |
| 287 |
continue; |
| 288 |
} |
| 289 |
if (ets->n_prio > TC_PRIO_MAX) { |
| 290 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 291 |
"Too many priomap in '%s=', ignoring assignment: %s", |
| 292 |
lvalue, word); |
| 293 |
continue; |
| 294 |
} |
| 295 |
|
| 296 |
ets->prio[ets->n_prio++] = v; |
| 297 |
} |
| 298 |
|
| 299 |
qdisc = NULL; |
| 300 |
|
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
static int enhanced_transmission_selection_verify(QDisc *qdisc) { |
| 305 |
EnhancedTransmissionSelection *ets; |
| 306 |
|
| 307 |
assert(qdisc); |
| 308 |
|
| 309 |
ets = ETS(qdisc); |
| 310 |
|
| 311 |
if (ets->n_bands == 0) |
| 312 |
ets->n_bands = ets->n_strict + ets->n_quanta; |
| 313 |
|
| 314 |
if (ets->n_bands == 0) |
| 315 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 316 |
"%s: At least one of Band=, Strict=, or Quanta= must be specified. " |
| 317 |
"Ignoring [EnhancedTransmissionSelection] section from line %u.", |
| 318 |
qdisc->section->filename, qdisc->section->line); |
| 319 |
|
| 320 |
if (ets->n_bands < ets->n_strict + ets->n_quanta) |
| 321 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 322 |
"%s: Not enough total bands to cover all the strict bands and quanta. " |
| 323 |
"Ignoring [EnhancedTransmissionSelection] section from line %u.", |
| 324 |
qdisc->section->filename, qdisc->section->line); |
| 325 |
|
| 326 |
for (unsigned i = 0; i < ets->n_prio; i++) |
| 327 |
if (ets->prio[i] >= ets->n_bands) |
| 328 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 329 |
"%s: PriorityMap= element is out of bands. " |
| 330 |
"Ignoring [EnhancedTransmissionSelection] section from line %u.", |
| 331 |
qdisc->section->filename, qdisc->section->line); |
| 332 |
|
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
const QDiscVTable ets_vtable = { |
| 337 |
.object_size = sizeof(EnhancedTransmissionSelection), |
| 338 |
.tca_kind = "ets", |
| 339 |
.fill_message = enhanced_transmission_selection_fill_message, |
| 340 |
.verify = enhanced_transmission_selection_verify, |
| 341 |
}; |