| 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 "codel.h" |
| 9 |
#include "log.h" |
| 10 |
#include "parse-util.h" |
| 11 |
#include "string-util.h" |
| 12 |
|
| 13 |
static int controlled_delay_init(QDisc *qdisc) { |
| 14 |
ControlledDelay *cd; |
| 15 |
|
| 16 |
assert(qdisc); |
| 17 |
|
| 18 |
cd = CODEL(qdisc); |
| 19 |
|
| 20 |
cd->ce_threshold_usec = USEC_INFINITY; |
| 21 |
cd->ecn = -1; |
| 22 |
|
| 23 |
return 0; |
| 24 |
} |
| 25 |
|
| 26 |
static int controlled_delay_fill_message(Link *link, QDisc *qdisc, sd_netlink_message *req) { |
| 27 |
ControlledDelay *cd; |
| 28 |
int r; |
| 29 |
|
| 30 |
assert(link); |
| 31 |
assert(qdisc); |
| 32 |
assert(req); |
| 33 |
|
| 34 |
assert_se(cd = CODEL(qdisc)); |
| 35 |
|
| 36 |
r = sd_netlink_message_open_container_union(req, TCA_OPTIONS, "codel"); |
| 37 |
if (r < 0) |
| 38 |
return r; |
| 39 |
|
| 40 |
if (cd->packet_limit > 0) { |
| 41 |
r = sd_netlink_message_append_u32(req, TCA_CODEL_LIMIT, cd->packet_limit); |
| 42 |
if (r < 0) |
| 43 |
return r; |
| 44 |
} |
| 45 |
|
| 46 |
if (cd->interval_usec > 0) { |
| 47 |
r = sd_netlink_message_append_u32(req, TCA_CODEL_INTERVAL, cd->interval_usec); |
| 48 |
if (r < 0) |
| 49 |
return r; |
| 50 |
} |
| 51 |
|
| 52 |
if (cd->target_usec > 0) { |
| 53 |
r = sd_netlink_message_append_u32(req, TCA_CODEL_TARGET, cd->target_usec); |
| 54 |
if (r < 0) |
| 55 |
return r; |
| 56 |
} |
| 57 |
|
| 58 |
if (cd->ecn >= 0) { |
| 59 |
r = sd_netlink_message_append_u32(req, TCA_CODEL_ECN, cd->ecn); |
| 60 |
if (r < 0) |
| 61 |
return r; |
| 62 |
} |
| 63 |
|
| 64 |
if (cd->ce_threshold_usec != USEC_INFINITY) { |
| 65 |
r = sd_netlink_message_append_u32(req, TCA_CODEL_CE_THRESHOLD, cd->ce_threshold_usec); |
| 66 |
if (r < 0) |
| 67 |
return r; |
| 68 |
} |
| 69 |
|
| 70 |
r = sd_netlink_message_close_container(req); |
| 71 |
if (r < 0) |
| 72 |
return r; |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
int config_parse_codel_u32( |
| 78 |
const char *unit, |
| 79 |
const char *filename, |
| 80 |
unsigned line, |
| 81 |
const char *section, |
| 82 |
unsigned section_line, |
| 83 |
const char *lvalue, |
| 84 |
int ltype, |
| 85 |
const char *rvalue, |
| 86 |
void *data, |
| 87 |
void *userdata) { |
| 88 |
|
| 89 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 90 |
ControlledDelay *cd; |
| 91 |
Network *network = ASSERT_PTR(data); |
| 92 |
int r; |
| 93 |
|
| 94 |
assert(filename); |
| 95 |
assert(lvalue); |
| 96 |
assert(rvalue); |
| 97 |
|
| 98 |
r = qdisc_new_static(QDISC_KIND_CODEL, network, filename, section_line, &qdisc); |
| 99 |
if (r == -ENOMEM) |
| 100 |
return log_oom(); |
| 101 |
if (r < 0) { |
| 102 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 103 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
cd = CODEL(qdisc); |
| 108 |
|
| 109 |
if (isempty(rvalue)) { |
| 110 |
cd->packet_limit = 0; |
| 111 |
|
| 112 |
qdisc = NULL; |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
r = safe_atou32(rvalue, &cd->packet_limit); |
| 117 |
if (r < 0) { |
| 118 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 119 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 120 |
lvalue, rvalue); |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
qdisc = NULL; |
| 125 |
|
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
int config_parse_codel_usec( |
| 130 |
const char *unit, |
| 131 |
const char *filename, |
| 132 |
unsigned line, |
| 133 |
const char *section, |
| 134 |
unsigned section_line, |
| 135 |
const char *lvalue, |
| 136 |
int ltype, |
| 137 |
const char *rvalue, |
| 138 |
void *data, |
| 139 |
void *userdata) { |
| 140 |
|
| 141 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 142 |
ControlledDelay *cd; |
| 143 |
Network *network = ASSERT_PTR(data); |
| 144 |
usec_t *p; |
| 145 |
int r; |
| 146 |
|
| 147 |
assert(filename); |
| 148 |
assert(lvalue); |
| 149 |
assert(rvalue); |
| 150 |
|
| 151 |
r = qdisc_new_static(QDISC_KIND_CODEL, network, filename, section_line, &qdisc); |
| 152 |
if (r == -ENOMEM) |
| 153 |
return log_oom(); |
| 154 |
if (r < 0) { |
| 155 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 156 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
cd = CODEL(qdisc); |
| 161 |
|
| 162 |
if (streq(lvalue, "TargetSec")) |
| 163 |
p = &cd->target_usec; |
| 164 |
else if (streq(lvalue, "IntervalSec")) |
| 165 |
p = &cd->interval_usec; |
| 166 |
else if (streq(lvalue, "CEThresholdSec")) |
| 167 |
p = &cd->ce_threshold_usec; |
| 168 |
else |
| 169 |
assert_not_reached(); |
| 170 |
|
| 171 |
if (isempty(rvalue)) { |
| 172 |
if (streq(lvalue, "CEThresholdSec")) |
| 173 |
*p = USEC_INFINITY; |
| 174 |
else |
| 175 |
*p = 0; |
| 176 |
|
| 177 |
qdisc = NULL; |
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
r = parse_sec(rvalue, p); |
| 182 |
if (r < 0) { |
| 183 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 184 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 185 |
lvalue, rvalue); |
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
qdisc = NULL; |
| 190 |
|
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
int config_parse_codel_bool( |
| 195 |
const char *unit, |
| 196 |
const char *filename, |
| 197 |
unsigned line, |
| 198 |
const char *section, |
| 199 |
unsigned section_line, |
| 200 |
const char *lvalue, |
| 201 |
int ltype, |
| 202 |
const char *rvalue, |
| 203 |
void *data, |
| 204 |
void *userdata) { |
| 205 |
|
| 206 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 207 |
ControlledDelay *cd; |
| 208 |
Network *network = ASSERT_PTR(data); |
| 209 |
int r; |
| 210 |
|
| 211 |
assert(filename); |
| 212 |
assert(lvalue); |
| 213 |
assert(rvalue); |
| 214 |
|
| 215 |
r = qdisc_new_static(QDISC_KIND_CODEL, network, filename, section_line, &qdisc); |
| 216 |
if (r == -ENOMEM) |
| 217 |
return log_oom(); |
| 218 |
if (r < 0) { |
| 219 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 220 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
cd = CODEL(qdisc); |
| 225 |
|
| 226 |
r = parse_tristate(rvalue, &cd->ecn); |
| 227 |
if (r < 0) { |
| 228 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 229 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 230 |
lvalue, rvalue); |
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
TAKE_PTR(qdisc); |
| 235 |
|
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
const QDiscVTable codel_vtable = { |
| 240 |
.object_size = sizeof(ControlledDelay), |
| 241 |
.tca_kind = "codel", |
| 242 |
.init = controlled_delay_init, |
| 243 |
.fill_message = controlled_delay_fill_message, |
| 244 |
}; |