]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later |
ad8352f4 SS |
2 | * Copyright © 2020 VMware, Inc. */ |
3 | ||
4 | #include <linux/pkt_sched.h> | |
5 | ||
6 | #include "alloc-util.h" | |
7 | #include "cake.h" | |
8 | #include "conf-parser.h" | |
9 | #include "netlink-util.h" | |
10 | #include "parse-util.h" | |
11 | #include "qdisc.h" | |
12 | #include "string-util.h" | |
13 | ||
14 | static int cake_fill_message(Link *link, QDisc *qdisc, sd_netlink_message *req) { | |
15 | CommonApplicationsKeptEnhanced *c; | |
16 | int r; | |
17 | ||
18 | assert(link); | |
19 | assert(qdisc); | |
20 | assert(req); | |
21 | ||
22 | c = CAKE(qdisc); | |
23 | ||
24 | r = sd_netlink_message_open_container_union(req, TCA_OPTIONS, "cake"); | |
25 | if (r < 0) | |
26 | return log_link_error_errno(link, r, "Could not open container TCA_OPTIONS: %m"); | |
27 | ||
28 | if (c->bandwidth > 0) { | |
29 | r = sd_netlink_message_append_u64(req, TCA_CAKE_BASE_RATE64, c->bandwidth); | |
30 | if (r < 0) | |
31 | return log_link_error_errno(link, r, "Could not append TCA_CAKE_BASE_RATE64 attribute: %m"); | |
32 | } | |
33 | ||
34 | r = sd_netlink_message_append_s32(req, TCA_CAKE_OVERHEAD, c->overhead); | |
35 | if (r < 0) | |
36 | return log_link_error_errno(link, r, "Could not append TCA_CAKE_OVERHEAD attribute: %m"); | |
37 | ||
38 | r = sd_netlink_message_close_container(req); | |
39 | if (r < 0) | |
40 | return log_link_error_errno(link, r, "Could not close container TCA_OPTIONS: %m"); | |
41 | ||
42 | return 0; | |
43 | } | |
44 | ||
45 | int config_parse_cake_bandwidth( | |
46 | const char *unit, | |
47 | const char *filename, | |
48 | unsigned line, | |
49 | const char *section, | |
50 | unsigned section_line, | |
51 | const char *lvalue, | |
52 | int ltype, | |
53 | const char *rvalue, | |
54 | void *data, | |
55 | void *userdata) { | |
56 | ||
57 | _cleanup_(qdisc_free_or_set_invalidp) QDisc *qdisc = NULL; | |
58 | CommonApplicationsKeptEnhanced *c; | |
59 | Network *network = data; | |
60 | uint64_t k; | |
61 | int r; | |
62 | ||
63 | assert(filename); | |
64 | assert(lvalue); | |
65 | assert(rvalue); | |
66 | assert(data); | |
67 | ||
68 | r = qdisc_new_static(QDISC_KIND_CAKE, network, filename, section_line, &qdisc); | |
69 | if (r == -ENOMEM) | |
70 | return log_oom(); | |
d96edb2c YW |
71 | if (r < 0) { |
72 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
73 | "More than one kind of queueing discipline, ignoring assignment: %m"); | |
74 | return 0; | |
75 | } | |
ad8352f4 SS |
76 | |
77 | c = CAKE(qdisc); | |
78 | ||
79 | if (isempty(rvalue)) { | |
80 | c->bandwidth = 0; | |
81 | ||
82 | qdisc = NULL; | |
83 | return 0; | |
84 | } | |
85 | ||
86 | r = parse_size(rvalue, 1000, &k); | |
87 | if (r < 0) { | |
d96edb2c | 88 | log_syntax(unit, LOG_WARNING, filename, line, r, |
ad8352f4 SS |
89 | "Failed to parse '%s=', ignoring assignment: %s", |
90 | lvalue, rvalue); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | c->bandwidth = k/8; | |
95 | qdisc = NULL; | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | int config_parse_cake_overhead( | |
101 | const char *unit, | |
102 | const char *filename, | |
103 | unsigned line, | |
104 | const char *section, | |
105 | unsigned section_line, | |
106 | const char *lvalue, | |
107 | int ltype, | |
108 | const char *rvalue, | |
109 | void *data, | |
110 | void *userdata) { | |
111 | ||
112 | _cleanup_(qdisc_free_or_set_invalidp) QDisc *qdisc = NULL; | |
113 | CommonApplicationsKeptEnhanced *c; | |
114 | Network *network = data; | |
115 | int32_t v; | |
116 | int r; | |
117 | ||
118 | assert(filename); | |
119 | assert(lvalue); | |
120 | assert(rvalue); | |
121 | assert(data); | |
122 | ||
123 | r = qdisc_new_static(QDISC_KIND_CAKE, network, filename, section_line, &qdisc); | |
124 | if (r == -ENOMEM) | |
125 | return log_oom(); | |
d96edb2c YW |
126 | if (r < 0) { |
127 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
128 | "More than one kind of queueing discipline, ignoring assignment: %m"); | |
129 | return 0; | |
130 | } | |
ad8352f4 SS |
131 | |
132 | c = CAKE(qdisc); | |
133 | ||
134 | if (isempty(rvalue)) { | |
135 | c->overhead = 0; | |
136 | qdisc = NULL; | |
137 | return 0; | |
138 | } | |
139 | ||
140 | r = safe_atoi32(rvalue, &v); | |
141 | if (r < 0) { | |
d96edb2c | 142 | log_syntax(unit, LOG_WARNING, filename, line, r, |
c03ef420 YW |
143 | "Failed to parse '%s=', ignoring assignment: %s", |
144 | lvalue, rvalue); | |
ad8352f4 SS |
145 | return 0; |
146 | } | |
147 | if (v < -64 || v > 256) { | |
d96edb2c | 148 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
c03ef420 YW |
149 | "Invalid '%s=', ignoring assignment: %s", |
150 | lvalue, rvalue); | |
ad8352f4 SS |
151 | return 0; |
152 | } | |
153 | ||
154 | c->overhead = v; | |
155 | qdisc = NULL; | |
156 | return 0; | |
157 | } | |
158 | ||
159 | const QDiscVTable cake_vtable = { | |
160 | .object_size = sizeof(CommonApplicationsKeptEnhanced), | |
161 | .tca_kind = "cake", | |
162 | .fill_message = cake_fill_message, | |
163 | }; |