]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/tc/fifo.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / network / tc / fifo.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later
a7476065
SS
2 * Copyright © 2020 VMware, Inc. */
3
4#include <linux/pkt_sched.h>
5
6#include "alloc-util.h"
7#include "conf-parser.h"
8#include "fifo.h"
9#include "netlink-util.h"
10#include "parse-util.h"
11#include "string-util.h"
12
13static int fifo_fill_message(Link *link, QDisc *qdisc, sd_netlink_message *req) {
a7476065
SS
14 FirstInFirstOut *fifo;
15 int r;
16
17 assert(link);
18 assert(qdisc);
19 assert(req);
20
89346ac6 21 switch (qdisc->kind) {
c853f594 22 case QDISC_KIND_PFIFO:
16924f54 23 assert_se(fifo = PFIFO(qdisc));
c853f594
SS
24 break;
25 case QDISC_KIND_BFIFO:
16924f54 26 assert_se(fifo = BFIFO(qdisc));
c853f594 27 break;
053a2ddb 28 case QDISC_KIND_PFIFO_HEAD_DROP:
16924f54 29 assert_se(fifo = PFIFO_HEAD_DROP(qdisc));
053a2ddb 30 break;
c853f594 31 default:
04499a70 32 assert_not_reached();
c853f594 33 }
a7476065 34
16924f54
ZJS
35 const struct tc_fifo_qopt opt = { .limit = fifo->limit };
36 r = sd_netlink_message_append_data(req, TCA_OPTIONS, &opt, sizeof(opt));
a7476065 37 if (r < 0)
16924f54 38 return r;
a7476065
SS
39
40 return 0;
41}
42
c853f594 43int config_parse_pfifo_size(
a7476065
SS
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_free_or_set_invalidp) QDisc *qdisc = NULL;
99534007 56 Network *network = ASSERT_PTR(data);
a7476065
SS
57 FirstInFirstOut *fifo;
58 int r;
59
60 assert(filename);
61 assert(lvalue);
62 assert(rvalue);
a7476065 63
053a2ddb 64 r = qdisc_new_static(ltype, network, filename, section_line, &qdisc);
a7476065
SS
65 if (r == -ENOMEM)
66 return log_oom();
d96edb2c
YW
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 }
a7476065 72
89346ac6 73 switch (qdisc->kind) {
053a2ddb
SS
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:
04499a70 81 assert_not_reached();
053a2ddb 82 }
a7476065
SS
83
84 if (isempty(rvalue)) {
85 fifo->limit = 0;
86
0132453c 87 TAKE_PTR(qdisc);
a7476065
SS
88 return 0;
89 }
90
91 r = safe_atou32(rvalue, &fifo->limit);
92 if (r < 0) {
d96edb2c 93 log_syntax(unit, LOG_WARNING, filename, line, r,
a7476065
SS
94 "Failed to parse '%s=', ignoring assignment: %s",
95 lvalue, rvalue);
96 return 0;
97 }
98
0132453c 99 TAKE_PTR(qdisc);
a7476065
SS
100 return 0;
101}
102
c853f594
SS
103int 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_free_or_set_invalidp) QDisc *qdisc = NULL;
99534007 116 Network *network = ASSERT_PTR(data);
c853f594
SS
117 FirstInFirstOut *fifo;
118 uint64_t u;
119 int r;
120
121 assert(filename);
122 assert(lvalue);
123 assert(rvalue);
c853f594
SS
124
125 r = qdisc_new_static(QDISC_KIND_BFIFO, network, filename, section_line, &qdisc);
126 if (r == -ENOMEM)
127 return log_oom();
d96edb2c
YW
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 }
c853f594
SS
133
134 fifo = BFIFO(qdisc);
135
136 if (isempty(rvalue)) {
137 fifo->limit = 0;
138
0132453c 139 TAKE_PTR(qdisc);
c853f594
SS
140 return 0;
141 }
142
c03ef420 143 r = parse_size(rvalue, 1024, &u);
c853f594 144 if (r < 0) {
d96edb2c 145 log_syntax(unit, LOG_WARNING, filename, line, r,
c853f594
SS
146 "Failed to parse '%s=', ignoring assignment: %s",
147 lvalue, rvalue);
148 return 0;
149 }
150 if (u > UINT32_MAX) {
d96edb2c 151 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid '%s=', ignoring assignment: %s",
c853f594
SS
152 lvalue, rvalue);
153 return 0;
154 }
155
156 fifo->limit = (uint32_t) u;
157
0132453c 158 TAKE_PTR(qdisc);
c853f594
SS
159 return 0;
160}
161
a7476065
SS
162const QDiscVTable pfifo_vtable = {
163 .object_size = sizeof(FirstInFirstOut),
164 .tca_kind = "pfifo",
165 .fill_message = fifo_fill_message,
166};
c853f594
SS
167
168const QDiscVTable bfifo_vtable = {
169 .object_size = sizeof(FirstInFirstOut),
170 .tca_kind = "bfifo",
171 .fill_message = fifo_fill_message,
172};
053a2ddb
SS
173
174const QDiscVTable pfifo_head_drop_vtable = {
175 .object_size = sizeof(FirstInFirstOut),
176 .tca_kind = "pfifo_head_drop",
177 .fill_message = fifo_fill_message,
178};
1a95964b
SS
179
180const QDiscVTable pfifo_fast_vtable = {
181 .object_size = sizeof(FirstInFirstOut),
182 .tca_kind = "pfifo_fast",
183};