1 /* SPDX-License-Identifier: LGPL-2.1-or-later
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 
fifo_fill_message(Link * link,QDisc * qdisc,sd_netlink_message * req)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 
config_parse_pfifo_size(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)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_free_or_set_invalidp) QDisc *qdisc = NULL;
56         Network *network = data;
57         FirstInFirstOut *fifo;
58         int r;
59 
60         assert(filename);
61         assert(lvalue);
62         assert(rvalue);
63         assert(data);
64 
65         r = qdisc_new_static(ltype, network, filename, section_line, &qdisc);
66         if (r == -ENOMEM)
67                 return log_oom();
68         if (r < 0) {
69                 log_syntax(unit, LOG_WARNING, filename, line, r,
70                            "More than one kind of queueing discipline, ignoring assignment: %m");
71                 return 0;
72         }
73 
74         switch (qdisc->kind) {
75         case QDISC_KIND_PFIFO:
76                 fifo = PFIFO(qdisc);
77                 break;
78         case QDISC_KIND_PFIFO_HEAD_DROP:
79                 fifo = PFIFO_HEAD_DROP(qdisc);
80                 break;
81         default:
82                 assert_not_reached();
83         }
84 
85         if (isempty(rvalue)) {
86                 fifo->limit = 0;
87 
88                 TAKE_PTR(qdisc);
89                 return 0;
90         }
91 
92         r = safe_atou32(rvalue, &fifo->limit);
93         if (r < 0) {
94                 log_syntax(unit, LOG_WARNING, filename, line, r,
95                            "Failed to parse '%s=', ignoring assignment: %s",
96                            lvalue, rvalue);
97                 return 0;
98         }
99 
100         TAKE_PTR(qdisc);
101         return 0;
102 }
103 
config_parse_bfifo_size(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)104 int config_parse_bfifo_size(
105                 const char *unit,
106                 const char *filename,
107                 unsigned line,
108                 const char *section,
109                 unsigned section_line,
110                 const char *lvalue,
111                 int ltype,
112                 const char *rvalue,
113                 void *data,
114                 void *userdata) {
115 
116         _cleanup_(qdisc_free_or_set_invalidp) QDisc *qdisc = NULL;
117         Network *network = data;
118         FirstInFirstOut *fifo;
119         uint64_t u;
120         int r;
121 
122         assert(filename);
123         assert(lvalue);
124         assert(rvalue);
125         assert(data);
126 
127         r = qdisc_new_static(QDISC_KIND_BFIFO, network, filename, section_line, &qdisc);
128         if (r == -ENOMEM)
129                 return log_oom();
130         if (r < 0) {
131                 log_syntax(unit, LOG_WARNING, filename, line, r,
132                            "More than one kind of queueing discipline, ignoring assignment: %m");
133                 return 0;
134         }
135 
136         fifo = BFIFO(qdisc);
137 
138         if (isempty(rvalue)) {
139                 fifo->limit = 0;
140 
141                 TAKE_PTR(qdisc);
142                 return 0;
143         }
144 
145         r = parse_size(rvalue, 1024, &u);
146         if (r < 0) {
147                 log_syntax(unit, LOG_WARNING, filename, line, r,
148                            "Failed to parse '%s=', ignoring assignment: %s",
149                            lvalue, rvalue);
150                 return 0;
151         }
152         if (u > UINT32_MAX) {
153                 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid '%s=', ignoring assignment: %s",
154                            lvalue, rvalue);
155                 return 0;
156         }
157 
158         fifo->limit = (uint32_t) u;
159 
160         TAKE_PTR(qdisc);
161         return 0;
162 }
163 
164 const QDiscVTable pfifo_vtable = {
165         .object_size = sizeof(FirstInFirstOut),
166         .tca_kind = "pfifo",
167         .fill_message = fifo_fill_message,
168 };
169 
170 const QDiscVTable bfifo_vtable = {
171        .object_size = sizeof(FirstInFirstOut),
172        .tca_kind = "bfifo",
173        .fill_message = fifo_fill_message,
174 };
175 
176 const QDiscVTable pfifo_head_drop_vtable = {
177        .object_size = sizeof(FirstInFirstOut),
178        .tca_kind = "pfifo_head_drop",
179        .fill_message = fifo_fill_message,
180 };
181 
182 const QDiscVTable pfifo_fast_vtable = {
183        .object_size = sizeof(FirstInFirstOut),
184        .tca_kind = "pfifo_fast",
185 };
186