bpf: add initial support for splitting map dscp value into ingress and egress
[project/qosify.git] / ubus.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
4 */
5 #include <libubus.h>
6
7 #include "qosify.h"
8
9 static struct blob_buf b;
10
11 static int
12 qosify_ubus_add_array(struct blob_attr *attr, struct qosify_dscp_val val,
13 enum qosify_map_id id)
14 {
15 struct blob_attr *cur;
16 int rem;
17
18 if (blobmsg_check_array(attr, BLOBMSG_TYPE_STRING) < 0)
19 return UBUS_STATUS_INVALID_ARGUMENT;
20
21 blobmsg_for_each_attr(cur, attr, rem)
22 qosify_map_set_entry(id, false, blobmsg_get_string(cur), val);
23
24 return 0;
25 }
26
27 static int
28 qosify_ubus_set_files(struct blob_attr *attr)
29 {
30 struct blob_attr *cur;
31 int rem;
32
33 if (blobmsg_check_array(attr, BLOBMSG_TYPE_STRING) < 0)
34 return UBUS_STATUS_INVALID_ARGUMENT;
35
36 qosify_map_clear_files();
37
38 blobmsg_for_each_attr(cur, attr, rem)
39 qosify_map_load_file(blobmsg_get_string(cur));
40
41 qosify_map_gc();
42
43 return 0;
44 }
45
46
47 enum {
48 CL_ADD_DSCP,
49 CL_ADD_TIMEOUT,
50 CL_ADD_IPV4,
51 CL_ADD_IPV6,
52 CL_ADD_TCP_PORT,
53 CL_ADD_UDP_PORT,
54 CL_ADD_DNS,
55 __CL_ADD_MAX
56 };
57
58 static const struct blobmsg_policy qosify_add_policy[__CL_ADD_MAX] = {
59 [CL_ADD_DSCP] = { "dscp", BLOBMSG_TYPE_STRING },
60 [CL_ADD_TIMEOUT] = { "timeout", BLOBMSG_TYPE_INT32 },
61 [CL_ADD_IPV4] = { "ipv4", BLOBMSG_TYPE_ARRAY },
62 [CL_ADD_IPV6] = { "ipv6", BLOBMSG_TYPE_ARRAY },
63 [CL_ADD_TCP_PORT] = { "tcp_port", BLOBMSG_TYPE_ARRAY },
64 [CL_ADD_UDP_PORT] = { "udp_port", BLOBMSG_TYPE_ARRAY },
65 [CL_ADD_DNS] = { "dns", BLOBMSG_TYPE_ARRAY },
66 };
67
68
69 static int
70 qosify_ubus_reload(struct ubus_context *ctx, struct ubus_object *obj,
71 struct ubus_request_data *req, const char *method,
72 struct blob_attr *msg)
73 {
74 qosify_map_reload();
75 return 0;
76 }
77
78
79 static int
80 qosify_ubus_add(struct ubus_context *ctx, struct ubus_object *obj,
81 struct ubus_request_data *req, const char *method,
82 struct blob_attr *msg)
83 {
84 int prev_timemout = qosify_map_timeout;
85 struct blob_attr *tb[__CL_ADD_MAX];
86 struct blob_attr *cur;
87 struct qosify_dscp_val dscp = { 0xff, 0xff };
88 int ret;
89
90 blobmsg_parse(qosify_add_policy, __CL_ADD_MAX, tb,
91 blobmsg_data(msg), blobmsg_len(msg));
92
93 if (!strcmp(method, "add")) {
94 if ((cur = tb[CL_ADD_DSCP]) == NULL ||
95 qosify_map_dscp_value(blobmsg_get_string(cur), &dscp))
96 return UBUS_STATUS_INVALID_ARGUMENT;
97
98 if ((cur = tb[CL_ADD_TIMEOUT]) != NULL)
99 qosify_map_timeout = blobmsg_get_u32(cur);
100 }
101
102 if ((cur = tb[CL_ADD_IPV4]) != NULL &&
103 (ret = qosify_ubus_add_array(cur, dscp, CL_MAP_IPV4_ADDR) != 0))
104 return ret;
105
106 if ((cur = tb[CL_ADD_IPV6]) != NULL &&
107 (ret = qosify_ubus_add_array(cur, dscp, CL_MAP_IPV6_ADDR) != 0))
108 return ret;
109
110 if ((cur = tb[CL_ADD_TCP_PORT]) != NULL &&
111 (ret = qosify_ubus_add_array(cur, dscp, CL_MAP_TCP_PORTS) != 0))
112 return ret;
113
114 if ((cur = tb[CL_ADD_UDP_PORT]) != NULL &&
115 (ret = qosify_ubus_add_array(cur, dscp, CL_MAP_UDP_PORTS) != 0))
116 return ret;
117
118 if ((cur = tb[CL_ADD_DNS]) != NULL &&
119 (ret = qosify_ubus_add_array(cur, dscp, CL_MAP_DNS) != 0))
120 return ret;
121
122 qosify_map_timeout = prev_timemout;
123
124 return 0;
125 }
126
127 enum {
128 CL_CONFIG_RESET,
129 CL_CONFIG_FILES,
130 CL_CONFIG_TIMEOUT,
131 CL_CONFIG_DSCP_UDP,
132 CL_CONFIG_DSCP_TCP,
133 CL_CONFIG_DSCP_PRIO,
134 CL_CONFIG_DSCP_BULK,
135 CL_CONFIG_DSCP_ICMP,
136 CL_CONFIG_BULK_TIMEOUT,
137 CL_CONFIG_BULK_PPS,
138 CL_CONFIG_PRIO_PKT_LEN,
139 CL_CONFIG_INTERFACES,
140 CL_CONFIG_DEVICES,
141 __CL_CONFIG_MAX
142 };
143
144 static const struct blobmsg_policy qosify_config_policy[__CL_CONFIG_MAX] = {
145 [CL_CONFIG_RESET] = { "reset", BLOBMSG_TYPE_BOOL },
146 [CL_CONFIG_FILES] = { "files", BLOBMSG_TYPE_ARRAY },
147 [CL_CONFIG_TIMEOUT] = { "timeout", BLOBMSG_TYPE_INT32 },
148 [CL_CONFIG_DSCP_UDP] = { "dscp_default_udp", BLOBMSG_TYPE_STRING },
149 [CL_CONFIG_DSCP_TCP] = { "dscp_default_tcp", BLOBMSG_TYPE_STRING },
150 [CL_CONFIG_DSCP_PRIO] = { "dscp_prio", BLOBMSG_TYPE_STRING },
151 [CL_CONFIG_DSCP_BULK] = { "dscp_bulk", BLOBMSG_TYPE_STRING },
152 [CL_CONFIG_DSCP_ICMP] = { "dscp_icmp", BLOBMSG_TYPE_STRING },
153 [CL_CONFIG_BULK_TIMEOUT] = { "bulk_trigger_timeout", BLOBMSG_TYPE_INT32 },
154 [CL_CONFIG_BULK_PPS] = { "bulk_trigger_pps", BLOBMSG_TYPE_INT32 },
155 [CL_CONFIG_PRIO_PKT_LEN] = { "prio_max_avg_pkt_len", BLOBMSG_TYPE_INT32 },
156 [CL_CONFIG_INTERFACES] = { "interfaces", BLOBMSG_TYPE_TABLE },
157 [CL_CONFIG_DEVICES] = { "devices", BLOBMSG_TYPE_TABLE },
158 };
159
160 static int __set_dscp(struct qosify_dscp_val *dest, struct blob_attr *attr, bool reset)
161 {
162 if (reset) {
163 dest->ingress = 0xff;
164 dest->egress = 0xff;
165 }
166
167 if (!attr)
168 return 0;
169
170 if (qosify_map_dscp_value(blobmsg_get_string(attr), dest))
171 return -1;
172
173 return 0;
174 }
175
176 static int
177 qosify_ubus_config(struct ubus_context *ctx, struct ubus_object *obj,
178 struct ubus_request_data *req, const char *method,
179 struct blob_attr *msg)
180 {
181 struct blob_attr *tb[__CL_CONFIG_MAX];
182 struct blob_attr *cur;
183 struct qosify_dscp_val dscp;
184 bool reset = false;
185 int ret;
186
187 blobmsg_parse(qosify_config_policy, __CL_CONFIG_MAX, tb,
188 blobmsg_data(msg), blobmsg_len(msg));
189
190 if ((cur = tb[CL_CONFIG_RESET]) != NULL)
191 reset = blobmsg_get_bool(cur);
192
193 if (reset)
194 qosify_map_reset_config();
195
196 if ((cur = tb[CL_CONFIG_TIMEOUT]) != NULL)
197 qosify_map_timeout = blobmsg_get_u32(cur);
198
199 if ((cur = tb[CL_CONFIG_FILES]) != NULL &&
200 (ret = qosify_ubus_set_files(cur) != 0))
201 return ret;
202
203 __set_dscp(&dscp, tb[CL_CONFIG_DSCP_UDP], true);
204 if (dscp.ingress != 0xff)
205 qosify_map_set_dscp_default(CL_MAP_UDP_PORTS, dscp);
206
207 __set_dscp(&dscp, tb[CL_CONFIG_DSCP_TCP], true);
208 if (dscp.ingress != 0xff)
209 qosify_map_set_dscp_default(CL_MAP_TCP_PORTS, dscp);
210
211 if (__set_dscp(&config.dscp_prio, tb[CL_CONFIG_DSCP_PRIO], reset) ||
212 __set_dscp(&config.dscp_bulk, tb[CL_CONFIG_DSCP_BULK], reset) ||
213 __set_dscp(&config.dscp_icmp, tb[CL_CONFIG_DSCP_ICMP], reset))
214 return UBUS_STATUS_INVALID_ARGUMENT;
215
216 if ((cur = tb[CL_CONFIG_BULK_TIMEOUT]) != NULL)
217 config.bulk_trigger_timeout = blobmsg_get_u32(cur);
218
219 if ((cur = tb[CL_CONFIG_BULK_PPS]) != NULL)
220 config.bulk_trigger_pps = blobmsg_get_u32(cur);
221
222 if ((cur = tb[CL_CONFIG_PRIO_PKT_LEN]) != NULL)
223 config.prio_max_avg_pkt_len = blobmsg_get_u32(cur);
224
225 qosify_map_update_config();
226
227 qosify_iface_config_update(tb[CL_CONFIG_INTERFACES], tb[CL_CONFIG_DEVICES]);
228
229 qosify_iface_check();
230
231 return 0;
232 }
233
234
235 static int
236 qosify_ubus_dump(struct ubus_context *ctx, struct ubus_object *obj,
237 struct ubus_request_data *req, const char *method,
238 struct blob_attr *msg)
239 {
240 blob_buf_init(&b, 0);
241 qosify_map_dump(&b);
242 ubus_send_reply(ctx, req, b.head);
243 blob_buf_free(&b);
244
245 return 0;
246 }
247
248 static int
249 qosify_ubus_status(struct ubus_context *ctx, struct ubus_object *obj,
250 struct ubus_request_data *req, const char *method,
251 struct blob_attr *msg)
252 {
253 blob_buf_init(&b, 0);
254 qosify_iface_status(&b);
255 ubus_send_reply(ctx, req, b.head);
256 blob_buf_free(&b);
257
258 return 0;
259 }
260
261 static int
262 qosify_ubus_check_devices(struct ubus_context *ctx, struct ubus_object *obj,
263 struct ubus_request_data *req, const char *method,
264 struct blob_attr *msg)
265 {
266 qosify_iface_check();
267
268 return 0;
269 }
270
271 enum {
272 CL_DNS_HOST_NAME,
273 CL_DNS_HOST_TYPE,
274 CL_DNS_HOST_ADDR,
275 CL_DNS_HOST_TTL,
276 __CL_DNS_HOST_MAX
277 };
278
279 static const struct blobmsg_policy qosify_dns_policy[__CL_DNS_HOST_MAX] = {
280 [CL_DNS_HOST_NAME] = { "name", BLOBMSG_TYPE_STRING },
281 [CL_DNS_HOST_TYPE] = { "type", BLOBMSG_TYPE_STRING },
282 [CL_DNS_HOST_ADDR] = { "address", BLOBMSG_TYPE_STRING },
283 [CL_DNS_HOST_TTL] = { "ttl", BLOBMSG_TYPE_INT32 },
284 };
285
286 static int
287 __qosify_ubus_add_dns_host(struct blob_attr *msg)
288 {
289 struct blob_attr *tb[__CL_DNS_HOST_MAX];
290 struct blob_attr *cur;
291 uint32_t ttl = 0;
292
293 blobmsg_parse(qosify_dns_policy, __CL_DNS_HOST_MAX, tb,
294 blobmsg_data(msg), blobmsg_len(msg));
295
296 if (!tb[CL_DNS_HOST_NAME] || !tb[CL_DNS_HOST_TYPE] ||
297 !tb[CL_DNS_HOST_ADDR])
298 return UBUS_STATUS_INVALID_ARGUMENT;
299
300 if ((cur = tb[CL_DNS_HOST_TTL]) != NULL)
301 ttl = blobmsg_get_u32(cur);
302
303 if (qosify_map_add_dns_host(blobmsg_get_string(tb[CL_DNS_HOST_NAME]),
304 blobmsg_get_string(tb[CL_DNS_HOST_ADDR]),
305 blobmsg_get_string(tb[CL_DNS_HOST_TYPE]),
306 ttl))
307 return UBUS_STATUS_INVALID_ARGUMENT;
308
309 return 0;
310 }
311
312 static int
313 qosify_ubus_add_dns_host(struct ubus_context *ctx, struct ubus_object *obj,
314 struct ubus_request_data *req, const char *method,
315 struct blob_attr *msg)
316 {
317 return __qosify_ubus_add_dns_host(msg);
318 }
319
320 static const struct ubus_method qosify_methods[] = {
321 UBUS_METHOD_NOARG("reload", qosify_ubus_reload),
322 UBUS_METHOD("add", qosify_ubus_add, qosify_add_policy),
323 UBUS_METHOD_MASK("remove", qosify_ubus_add, qosify_add_policy,
324 ((1 << __CL_ADD_MAX) - 1) & ~(1 << CL_ADD_DSCP)),
325 UBUS_METHOD("config", qosify_ubus_config, qosify_config_policy),
326 UBUS_METHOD_NOARG("dump", qosify_ubus_dump),
327 UBUS_METHOD_NOARG("status", qosify_ubus_status),
328 UBUS_METHOD("add_dns_host", qosify_ubus_add_dns_host, qosify_dns_policy),
329 UBUS_METHOD_NOARG("check_devices", qosify_ubus_check_devices),
330 };
331
332 static struct ubus_object_type qosify_object_type =
333 UBUS_OBJECT_TYPE("qosify", qosify_methods);
334
335 static struct ubus_object qosify_object = {
336 .name = "qosify",
337 .type = &qosify_object_type,
338 .methods = qosify_methods,
339 .n_methods = ARRAY_SIZE(qosify_methods),
340 };
341
342 static int
343 qosify_dnsmasq_cb(struct ubus_context *ctx, struct ubus_object *obj,
344 struct ubus_request_data *req, const char *method,
345 struct blob_attr *msg)
346 {
347 if (!strcmp(method, "dns_result"))
348 __qosify_ubus_add_dns_host(msg);
349
350 return 0;
351 }
352
353 static void
354 qosify_subscribe_dnsmasq(struct ubus_context *ctx)
355 {
356 static struct ubus_subscriber sub = {
357 .cb = qosify_dnsmasq_cb,
358 };
359 uint32_t id;
360
361 if (!sub.obj.id &&
362 ubus_register_subscriber(ctx, &sub))
363 return;
364
365 if (ubus_lookup_id(ctx, "dnsmasq.dns", &id))
366 return;
367
368 ubus_subscribe(ctx, &sub, id);
369 }
370
371 static void
372 qosify_ubus_event_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
373 const char *type, struct blob_attr *msg)
374 {
375 static const struct blobmsg_policy policy =
376 { "path", BLOBMSG_TYPE_STRING };
377 struct blob_attr *attr;
378 const char *path;
379
380 blobmsg_parse(&policy, 1, &attr, blobmsg_data(msg), blobmsg_len(msg));
381
382 if (!attr)
383 return;
384
385 path = blobmsg_get_string(attr);
386 if (!strcmp(path, "dnsmasq.dns"))
387 qosify_subscribe_dnsmasq(ctx);
388 }
389
390
391 static void
392 ubus_connect_handler(struct ubus_context *ctx)
393 {
394 static struct ubus_event_handler ev = {
395 .cb = qosify_ubus_event_cb
396 };
397
398 ubus_add_object(ctx, &qosify_object);
399 ubus_register_event_handler(ctx, &ev, "ubus.object.add");
400 qosify_subscribe_dnsmasq(ctx);
401 }
402
403 static struct ubus_auto_conn conn;
404
405 int qosify_ubus_init(void)
406 {
407 conn.cb = ubus_connect_handler;
408 ubus_auto_connect(&conn);
409
410 return 0;
411 }
412
413 void qosify_ubus_stop(void)
414 {
415 ubus_auto_shutdown(&conn);
416 }
417
418 struct iface_req {
419 char *name;
420 int len;
421 };
422
423 static void
424 netifd_if_cb(struct ubus_request *req, int type, struct blob_attr *msg)
425 {
426 struct iface_req *ifr = req->priv;
427 enum {
428 IFS_ATTR_UP,
429 IFS_ATTR_DEV,
430 __IFS_ATTR_MAX
431 };
432 static const struct blobmsg_policy policy[__IFS_ATTR_MAX] = {
433 [IFS_ATTR_UP] = { "up", BLOBMSG_TYPE_BOOL },
434 [IFS_ATTR_DEV] = { "l3_device", BLOBMSG_TYPE_STRING },
435 };
436 struct blob_attr *tb[__IFS_ATTR_MAX];
437
438 blobmsg_parse(policy, __IFS_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
439
440 if (!tb[IFS_ATTR_UP] || !tb[IFS_ATTR_DEV])
441 return;
442
443 if (!blobmsg_get_bool(tb[IFS_ATTR_UP]))
444 return;
445
446 snprintf(ifr->name, ifr->len, "%s", blobmsg_get_string(tb[IFS_ATTR_DEV]));
447 }
448
449 int qosify_ubus_check_interface(const char *name, char *ifname, int ifname_len)
450 {
451 struct iface_req req = { ifname, ifname_len };
452 char *obj_name = "network.interface.";
453 uint32_t id;
454
455 #define PREFIX "network.interface."
456 obj_name = alloca(sizeof(PREFIX) + strlen(name) + 1);
457 sprintf(obj_name, PREFIX "%s", name);
458 #undef PREFIX
459
460 ifname[0] = 0;
461
462 if (ubus_lookup_id(&conn.ctx, obj_name, &id))
463 return -1;
464
465 ubus_invoke(&conn.ctx, id, "status", b.head, netifd_if_cb, &req, 1000);
466
467 if (!ifname[0])
468 return -1;
469
470 return 0;
471 }