| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
* Copyright © 2020 VMware, Inc. */ |
| 3 |
|
| 4 |
#include <linux/pkt_sched.h> |
| 5 |
|
| 6 |
#include "sd-netlink.h" |
| 7 |
|
| 8 |
#include "fifo.h" |
| 9 |
#include "log.h" |
| 10 |
#include "parse-util.h" |
| 11 |
#include "string-util.h" |
| 12 |
|
| 13 |
static int fifo_fill_message(Link *link, QDisc *qdisc, sd_netlink_message *req) { |
| 14 |
FirstInFirstOut *fifo; |
| 15 |
int r; |
| 16 |
|
| 17 |
assert(link); |
| 18 |
assert(qdisc); |
| 19 |
assert(req); |
| 20 |
|
| 21 |
switch (qdisc->kind) { |
| 22 |
case QDISC_KIND_PFIFO: |
| 23 |
assert_se(fifo = PFIFO(qdisc)); |
| 24 |
break; |
| 25 |
case QDISC_KIND_BFIFO: |
| 26 |
assert_se(fifo = BFIFO(qdisc)); |
| 27 |
break; |
| 28 |
case QDISC_KIND_PFIFO_HEAD_DROP: |
| 29 |
assert_se(fifo = PFIFO_HEAD_DROP(qdisc)); |
| 30 |
break; |
| 31 |
default: |
| 32 |
assert_not_reached(); |
| 33 |
} |
| 34 |
|
| 35 |
const struct tc_fifo_qopt opt = { .limit = fifo->limit }; |
| 36 |
r = sd_netlink_message_append_data(req, TCA_OPTIONS, &opt, sizeof(opt)); |
| 37 |
if (r < 0) |
| 38 |
return r; |
| 39 |
|
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
int config_parse_pfifo_size( |
| 44 |
const char *unit, |
| 45 |
const char *filename, |
| 46 |
unsigned line, |
| 47 |
const char *section, |
| 48 |
unsigned section_line, |
| 49 |
const char *lvalue, |
| 50 |
int ltype, |
| 51 |
const char *rvalue, |
| 52 |
void *data, |
| 53 |
void *userdata) { |
| 54 |
|
| 55 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 56 |
Network *network = ASSERT_PTR(data); |
| 57 |
FirstInFirstOut *fifo; |
| 58 |
int r; |
| 59 |
|
| 60 |
assert(filename); |
| 61 |
assert(lvalue); |
| 62 |
assert(rvalue); |
| 63 |
|
| 64 |
r = qdisc_new_static(ltype, network, filename, section_line, &qdisc); |
| 65 |
if (r == -ENOMEM) |
| 66 |
return log_oom(); |
| 67 |
if (r < 0) { |
| 68 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 69 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
switch (qdisc->kind) { |
| 74 |
case QDISC_KIND_PFIFO: |
| 75 |
fifo = PFIFO(qdisc); |
| 76 |
break; |
| 77 |
case QDISC_KIND_PFIFO_HEAD_DROP: |
| 78 |
fifo = PFIFO_HEAD_DROP(qdisc); |
| 79 |
break; |
| 80 |
default: |
| 81 |
assert_not_reached(); |
| 82 |
} |
| 83 |
|
| 84 |
if (isempty(rvalue)) { |
| 85 |
fifo->limit = 0; |
| 86 |
|
| 87 |
TAKE_PTR(qdisc); |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
r = safe_atou32(rvalue, &fifo->limit); |
| 92 |
if (r < 0) { |
| 93 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 94 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 95 |
lvalue, rvalue); |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
TAKE_PTR(qdisc); |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
int config_parse_bfifo_size( |
| 104 |
const char *unit, |
| 105 |
const char *filename, |
| 106 |
unsigned line, |
| 107 |
const char *section, |
| 108 |
unsigned section_line, |
| 109 |
const char *lvalue, |
| 110 |
int ltype, |
| 111 |
const char *rvalue, |
| 112 |
void *data, |
| 113 |
void *userdata) { |
| 114 |
|
| 115 |
_cleanup_(qdisc_unref_or_set_invalidp) QDisc *qdisc = NULL; |
| 116 |
Network *network = ASSERT_PTR(data); |
| 117 |
FirstInFirstOut *fifo; |
| 118 |
uint64_t u; |
| 119 |
int r; |
| 120 |
|
| 121 |
assert(filename); |
| 122 |
assert(lvalue); |
| 123 |
assert(rvalue); |
| 124 |
|
| 125 |
r = qdisc_new_static(QDISC_KIND_BFIFO, network, filename, section_line, &qdisc); |
| 126 |
if (r == -ENOMEM) |
| 127 |
return log_oom(); |
| 128 |
if (r < 0) { |
| 129 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 130 |
"More than one kind of queueing discipline, ignoring assignment: %m"); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
fifo = BFIFO(qdisc); |
| 135 |
|
| 136 |
if (isempty(rvalue)) { |
| 137 |
fifo->limit = 0; |
| 138 |
|
| 139 |
TAKE_PTR(qdisc); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
r = parse_size(rvalue, 1024, &u); |
| 144 |
if (r < 0) { |
| 145 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 146 |
"Failed to parse '%s=', ignoring assignment: %s", |
| 147 |
lvalue, rvalue); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
if (u > UINT32_MAX) { |
| 151 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid '%s=', ignoring assignment: %s", |
| 152 |
lvalue, rvalue); |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
fifo->limit = (uint32_t) u; |
| 157 |
|
| 158 |
TAKE_PTR(qdisc); |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
const QDiscVTable pfifo_vtable = { |
| 163 |
.object_size = sizeof(FirstInFirstOut), |
| 164 |
.tca_kind = "pfifo", |
| 165 |
.fill_message = fifo_fill_message, |
| 166 |
}; |
| 167 |
|
| 168 |
const QDiscVTable bfifo_vtable = { |
| 169 |
.object_size = sizeof(FirstInFirstOut), |
| 170 |
.tca_kind = "bfifo", |
| 171 |
.fill_message = fifo_fill_message, |
| 172 |
}; |
| 173 |
|
| 174 |
const QDiscVTable pfifo_head_drop_vtable = { |
| 175 |
.object_size = sizeof(FirstInFirstOut), |
| 176 |
.tca_kind = "pfifo_head_drop", |
| 177 |
.fill_message = fifo_fill_message, |
| 178 |
}; |
| 179 |
|
| 180 |
const QDiscVTable pfifo_fast_vtable = { |
| 181 |
.object_size = sizeof(FirstInFirstOut), |
| 182 |
.tca_kind = "pfifo_fast", |
| 183 |
}; |