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 "drr.h"
9 #include "netlink-util.h"
10 #include "parse-util.h"
11 #include "string-util.h"
12 
13 const QDiscVTable drr_vtable = {
14         .object_size = sizeof(DeficitRoundRobinScheduler),
15         .tca_kind = "drr",
16 };
17 
drr_class_fill_message(Link * link,TClass * tclass,sd_netlink_message * req)18 static int drr_class_fill_message(Link *link, TClass *tclass, sd_netlink_message *req) {
19         DeficitRoundRobinSchedulerClass *drr;
20         int r;
21 
22         assert(link);
23         assert(tclass);
24         assert(req);
25 
26         assert_se(drr = TCLASS_TO_DRR(tclass));
27 
28         r = sd_netlink_message_open_container_union(req, TCA_OPTIONS, "drr");
29         if (r < 0)
30                 return r;
31 
32         if (drr->quantum > 0) {
33                 r = sd_netlink_message_append_u32(req, TCA_DRR_QUANTUM, drr->quantum);
34                 if (r < 0)
35                         return r;
36         }
37 
38         r = sd_netlink_message_close_container(req);
39         if (r < 0)
40                 return r;
41 
42         return 0;
43 }
44 
config_parse_drr_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)45 int config_parse_drr_size(
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_(tclass_free_or_set_invalidp) TClass *tclass = NULL;
58         DeficitRoundRobinSchedulerClass *drr;
59         Network *network = data;
60         uint64_t u;
61         int r;
62 
63         assert(filename);
64         assert(lvalue);
65         assert(rvalue);
66         assert(data);
67 
68         r = tclass_new_static(TCLASS_KIND_DRR, network, filename, section_line, &tclass);
69         if (r == -ENOMEM)
70                 return log_oom();
71         if (r < 0) {
72                 log_syntax(unit, LOG_WARNING, filename, line, r,
73                            "Failed to create traffic control class, ignoring assignment: %m");
74                 return 0;
75         }
76 
77         drr = TCLASS_TO_DRR(tclass);
78 
79         if (isempty(rvalue)) {
80                 drr->quantum = 0;
81 
82                 TAKE_PTR(tclass);
83                 return 0;
84         }
85 
86         r = parse_size(rvalue, 1024, &u);
87         if (r < 0) {
88                 log_syntax(unit, LOG_WARNING, filename, line, r,
89                            "Failed to parse '%s=', ignoring assignment: %s",
90                            lvalue, rvalue);
91                 return 0;
92         }
93         if (u > UINT32_MAX) {
94                 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid '%s=', ignoring assignment: %s",
95                            lvalue, rvalue);
96                 return 0;
97         }
98 
99         drr->quantum = (uint32_t) u;
100 
101         TAKE_PTR(tclass);
102         return 0;
103 }
104 
105 const TClassVTable drr_tclass_vtable = {
106         .object_size = sizeof(DeficitRoundRobinSchedulerClass),
107         .tca_kind = "drr",
108         .fill_message = drr_class_fill_message,
109 };
110