]>
Commit | Line | Data |
---|---|---|
ad8352f4 SS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ |
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(); | |
71 | if (r < 0) | |
72 | return log_syntax(unit, LOG_ERR, filename, line, r, | |
73 | "More than one kind of queueing discipline, ignoring assignment: %m"); | |
74 | ||
75 | c = CAKE(qdisc); | |
76 | ||
77 | if (isempty(rvalue)) { | |
78 | c->bandwidth = 0; | |
79 | ||
80 | qdisc = NULL; | |
81 | return 0; | |
82 | } | |
83 | ||
84 | r = parse_size(rvalue, 1000, &k); | |
85 | if (r < 0) { | |
86 | log_syntax(unit, LOG_ERR, filename, line, r, | |
87 | "Failed to parse '%s=', ignoring assignment: %s", | |
88 | lvalue, rvalue); | |
89 | return 0; | |
90 | } | |
91 | ||
92 | c->bandwidth = k/8; | |
93 | qdisc = NULL; | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
98 | int config_parse_cake_overhead( | |
99 | const char *unit, | |
100 | const char *filename, | |
101 | unsigned line, | |
102 | const char *section, | |
103 | unsigned section_line, | |
104 | const char *lvalue, | |
105 | int ltype, | |
106 | const char *rvalue, | |
107 | void *data, | |
108 | void *userdata) { | |
109 | ||
110 | _cleanup_(qdisc_free_or_set_invalidp) QDisc *qdisc = NULL; | |
111 | CommonApplicationsKeptEnhanced *c; | |
112 | Network *network = data; | |
113 | int32_t v; | |
114 | int r; | |
115 | ||
116 | assert(filename); | |
117 | assert(lvalue); | |
118 | assert(rvalue); | |
119 | assert(data); | |
120 | ||
121 | r = qdisc_new_static(QDISC_KIND_CAKE, network, filename, section_line, &qdisc); | |
122 | if (r == -ENOMEM) | |
123 | return log_oom(); | |
124 | if (r < 0) | |
125 | return log_syntax(unit, LOG_ERR, filename, line, r, | |
126 | "More than one kind of queueing discipline, ignoring assignment: %m"); | |
127 | ||
128 | c = CAKE(qdisc); | |
129 | ||
130 | if (isempty(rvalue)) { | |
131 | c->overhead = 0; | |
132 | qdisc = NULL; | |
133 | return 0; | |
134 | } | |
135 | ||
136 | r = safe_atoi32(rvalue, &v); | |
137 | if (r < 0) { | |
138 | log_syntax(unit, LOG_ERR, filename, line, r, | |
c03ef420 YW |
139 | "Failed to parse '%s=', ignoring assignment: %s", |
140 | lvalue, rvalue); | |
ad8352f4 SS |
141 | return 0; |
142 | } | |
143 | if (v < -64 || v > 256) { | |
144 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
c03ef420 YW |
145 | "Invalid '%s=', ignoring assignment: %s", |
146 | lvalue, rvalue); | |
ad8352f4 SS |
147 | return 0; |
148 | } | |
149 | ||
150 | c->overhead = v; | |
151 | qdisc = NULL; | |
152 | return 0; | |
153 | } | |
154 | ||
155 | const QDiscVTable cake_vtable = { | |
156 | .object_size = sizeof(CommonApplicationsKeptEnhanced), | |
157 | .tca_kind = "cake", | |
158 | .fill_message = cake_fill_message, | |
159 | }; |