1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
13 #include <libubox/uloop.h>
17 static int qosify_map_entry_cmp(const void *k1
, const void *k2
, void *ptr
);
19 static int qosify_map_fds
[__CL_MAP_MAX
];
20 static AVL_TREE(map_data
, qosify_map_entry_cmp
, false, NULL
);
21 static LIST_HEAD(map_files
);
22 static uint32_t next_timeout
;
23 static uint8_t qosify_dscp_default
[2] = { 0xff, 0xff };
24 int qosify_map_timeout
= 3600;
25 struct qosify_config config
;
27 struct qosify_map_file
{
28 struct list_head list
;
34 const char *type_name
;
35 } qosify_map_info
[] = {
36 [CL_MAP_TCP_PORTS
] = { "tcp_ports", "tcp_port" },
37 [CL_MAP_UDP_PORTS
] = { "udp_ports", "udp_port" },
38 [CL_MAP_IPV4_ADDR
] = { "ipv4_map", "ipv4_addr" },
39 [CL_MAP_IPV6_ADDR
] = { "ipv6_map", "ipv6_addr" },
40 [CL_MAP_CONFIG
] = { "config", "config" },
71 static void qosify_map_timer_cb(struct uloop_timeout
*t
)
76 static struct uloop_timeout qosify_map_timer
= {
77 .cb
= qosify_map_timer_cb
,
80 static uint32_t qosify_gettime(void)
84 clock_gettime(CLOCK_MONOTONIC
, &ts
);
90 qosify_map_path(enum qosify_map_id id
)
92 static char path
[128];
95 if (id
>= ARRAY_SIZE(qosify_map_info
))
98 name
= qosify_map_info
[id
].name
;
102 snprintf(path
, sizeof(path
), "%s/%s", CLASSIFY_DATA_PATH
, name
);
107 static int qosify_map_get_fd(enum qosify_map_id id
)
109 const char *path
= qosify_map_path(id
);
115 fd
= bpf_obj_get(path
);
117 fprintf(stderr
, "Failed to open map %s: %s\n", path
, strerror(errno
));
122 static void qosify_map_clear_list(enum qosify_map_id id
)
124 int fd
= qosify_map_fds
[id
];
127 while (bpf_map_get_next_key(fd
, &key
, &key
) != -1)
128 bpf_map_delete_elem(fd
, &key
);
131 static void __qosify_map_set_dscp_default(enum qosify_map_id id
, uint8_t val
)
133 struct qosify_map_data data
= {
136 int fd
= qosify_map_fds
[id
];
139 val
|= QOSIFY_DSCP_DEFAULT_FLAG
;
141 for (i
= 0; i
< (1 << 16); i
++) {
142 data
.addr
.port
= htons(i
);
143 if (avl_find(&map_data
, &data
))
146 bpf_map_update_elem(fd
, &data
.addr
, &val
, BPF_ANY
);
150 void qosify_map_set_dscp_default(enum qosify_map_id id
, uint8_t val
)
154 if (id
== CL_MAP_TCP_PORTS
)
156 else if (id
== CL_MAP_UDP_PORTS
)
161 if (qosify_dscp_default
[udp
] == val
)
164 qosify_dscp_default
[udp
] = val
;
165 __qosify_map_set_dscp_default(id
, val
);
168 int qosify_map_init(void)
172 for (i
= 0; i
< ARRAY_SIZE(qosify_map_fds
); i
++) {
173 qosify_map_fds
[i
] = qosify_map_get_fd(i
);
174 if (qosify_map_fds
[i
] < 0)
178 qosify_map_clear_list(CL_MAP_IPV4_ADDR
);
179 qosify_map_clear_list(CL_MAP_IPV6_ADDR
);
180 qosify_map_reset_config();
185 static char *str_skip(char *str
, bool space
)
187 while (*str
&& isspace(*str
) == space
)
194 qosify_map_codepoint(const char *val
)
198 for (i
= 0; i
< ARRAY_SIZE(codepoints
); i
++)
199 if (!strcmp(codepoints
[i
].name
, val
))
200 return codepoints
[i
].val
;
205 static int qosify_map_entry_cmp(const void *k1
, const void *k2
, void *ptr
)
207 const struct qosify_map_data
*d1
= k1
;
208 const struct qosify_map_data
*d2
= k2
;
210 if (d1
->id
!= d2
->id
)
211 return d2
->id
- d1
->id
;
213 return memcmp(&d1
->addr
, &d2
->addr
, sizeof(d1
->addr
));
216 static void __qosify_map_set_entry(struct qosify_map_data
*data
)
218 int fd
= qosify_map_fds
[data
->id
];
219 struct qosify_map_entry
*e
;
220 bool file
= data
->file
;
222 bool add
= data
->dscp
!= 0xff;
223 uint8_t prev_dscp
= 0xff;
225 e
= avl_find_element(&map_data
, data
, e
, avl
);
230 e
= calloc(1, sizeof(*e
));
231 e
->avl
.key
= &e
->data
;
232 e
->data
.id
= data
->id
;
233 memcpy(&e
->data
.addr
, &data
->addr
, sizeof(e
->data
.addr
));
234 avl_insert(&map_data
, &e
->avl
);
236 prev_dscp
= e
->data
.dscp
;
246 e
->data
.file_dscp
= data
->dscp
;
247 if (!e
->data
.user
|| !file
)
248 e
->data
.dscp
= data
->dscp
;
249 } else if (e
->data
.file
&& !file
) {
250 e
->data
.dscp
= e
->data
.file_dscp
;
253 if (e
->data
.dscp
!= prev_dscp
)
254 bpf_map_update_elem(fd
, &data
->addr
, &e
->data
.dscp
, BPF_ANY
);
257 if (qosify_map_timeout
== ~0 || file
) {
262 e
->timeout
= qosify_gettime() + qosify_map_timeout
;
263 delta
= e
->timeout
- next_timeout
;
264 if (next_timeout
&& delta
>= 0)
268 uloop_timeout_set(&qosify_map_timer
, 1);
272 qosify_map_set_port(struct qosify_map_data
*data
, const char *str
)
274 unsigned long start_port
, end_port
;
278 start_port
= end_port
= strtoul(str
, &err
, 0);
281 end_port
= strtoul(err
+ 1, &err
, 0);
286 if (!start_port
|| end_port
< start_port
||
290 for (i
= start_port
; i
<= end_port
; i
++) {
291 data
->addr
.port
= htons(i
);
292 __qosify_map_set_entry(data
);
299 qosify_map_fill_ip(struct qosify_map_data
*data
, const char *str
)
303 if (data
->id
== CL_MAP_IPV6_ADDR
)
308 if (inet_pton(af
, str
, &data
->addr
) != 1)
314 int qosify_map_set_entry(enum qosify_map_id id
, bool file
, const char *str
, uint8_t dscp
)
316 struct qosify_map_data data
= {
323 case CL_MAP_TCP_PORTS
:
324 case CL_MAP_UDP_PORTS
:
325 return qosify_map_set_port(&data
, str
);
326 case CL_MAP_IPV4_ADDR
:
327 case CL_MAP_IPV6_ADDR
:
328 if (qosify_map_fill_ip(&data
, str
))
335 __qosify_map_set_entry(&data
);
340 int qosify_map_dscp_value(const char *val
)
344 bool fallback
= false;
351 dscp
= strtoul(val
, &err
, 0);
353 dscp
= qosify_map_codepoint(val
);
358 return dscp
+ (fallback
<< 6);
362 qosify_map_dscp_codepoint_str(char *dest
, int len
, uint8_t dscp
)
366 if (dscp
& QOSIFY_DSCP_FALLBACK_FLAG
) {
369 dscp
&= ~QOSIFY_DSCP_FALLBACK_FLAG
;
372 for (i
= 0; i
< ARRAY_SIZE(codepoints
); i
++) {
373 if (codepoints
[i
].val
!= dscp
)
376 snprintf(dest
, len
, "%s", codepoints
[i
].name
);
380 snprintf(dest
, len
, "0x%x", dscp
);
384 qosify_map_parse_line(char *str
)
386 const char *key
, *value
;
389 str
= str_skip(str
, true);
392 str
= str_skip(str
, false);
397 str
= str_skip(str
, true);
400 dscp
= qosify_map_dscp_value(value
);
404 if (!strncmp(key
, "tcp:", 4))
405 qosify_map_set_entry(CL_MAP_TCP_PORTS
, true, key
+ 4, dscp
);
406 else if (!strncmp(key
, "udp:", 4))
407 qosify_map_set_entry(CL_MAP_UDP_PORTS
, true, key
+ 4, dscp
);
408 else if (strchr(key
, ':'))
409 qosify_map_set_entry(CL_MAP_IPV6_ADDR
, true, key
, dscp
);
410 else if (strchr(key
, '.'))
411 qosify_map_set_entry(CL_MAP_IPV4_ADDR
, true, key
, dscp
);
414 static int __qosify_map_load_file(const char *file
)
423 f
= fopen(file
, "r");
425 fprintf(stderr
, "Can't open data file %s\n", file
);
429 while (fgets(line
, sizeof(line
), f
)) {
430 cur
= strchr(line
, '#');
434 cur
= line
+ strlen(line
);
438 while (cur
> line
&& isspace(cur
[-1]))
442 qosify_map_parse_line(line
);
450 int qosify_map_load_file(const char *file
)
452 struct qosify_map_file
*f
;
457 f
= calloc(1, sizeof(*f
) + strlen(file
) + 1);
458 strcpy(f
->filename
, file
);
459 list_add_tail(&f
->list
, &map_files
);
461 return __qosify_map_load_file(file
);
464 static void qosify_map_reset_file_entries(void)
466 struct qosify_map_entry
*e
;
468 avl_for_each_element(&map_data
, e
, avl
)
469 e
->data
.file
= false;
472 void qosify_map_clear_files(void)
474 struct qosify_map_file
*f
, *tmp
;
476 qosify_map_reset_file_entries();
478 list_for_each_entry_safe(f
, tmp
, &map_files
, list
) {
484 void qosify_map_reset_config(void)
486 qosify_map_clear_files();
487 qosify_map_set_dscp_default(CL_MAP_TCP_PORTS
, 0);
488 qosify_map_set_dscp_default(CL_MAP_UDP_PORTS
, 0);
489 qosify_map_timeout
= 3600;
491 memset(&config
, 0, sizeof(config
));
492 config
.dscp_prio
= 0xff;
493 config
.dscp_bulk
= 0xff;
494 config
.dscp_icmp
= 0xff;
497 void qosify_map_reload(void)
499 struct qosify_map_file
*f
;
501 qosify_map_reset_file_entries();
503 list_for_each_entry(f
, &map_files
, list
)
504 __qosify_map_load_file(f
->filename
);
509 void qosify_map_gc(void)
511 struct qosify_map_entry
*e
, *tmp
;
513 uint32_t cur_time
= qosify_gettime();
517 avl_for_each_element_safe(&map_data
, e
, avl
, tmp
) {
520 if (e
->data
.user
&& e
->timeout
!= ~0) {
521 cur_timeout
= e
->timeout
- cur_time
;
522 if (cur_timeout
<= 0) {
523 e
->data
.user
= false;
524 e
->data
.dscp
= e
->data
.file_dscp
;
525 } else if (!timeout
|| cur_timeout
< timeout
) {
526 timeout
= cur_timeout
;
527 next_timeout
= e
->timeout
;
531 if (e
->data
.file
|| e
->data
.user
)
534 avl_delete(&map_data
, &e
->avl
);
535 fd
= qosify_map_fds
[e
->data
.id
];
536 bpf_map_delete_elem(fd
, &e
->data
.addr
);
543 uloop_timeout_set(&qosify_map_timer
, timeout
* 1000);
546 void qosify_map_dump(struct blob_buf
*b
)
548 struct qosify_map_entry
*e
;
549 uint32_t cur_time
= qosify_gettime();
550 int buf_len
= INET6_ADDRSTRLEN
+ 1;
555 a
= blobmsg_open_array(b
, "entries");
556 avl_for_each_element(&map_data
, e
, avl
) {
559 if (!e
->data
.file
&& !e
->data
.user
)
562 c
= blobmsg_open_table(b
, NULL
);
563 if (e
->data
.user
&& e
->timeout
!= ~0) {
564 int32_t cur_timeout
= e
->timeout
- cur_time
;
569 blobmsg_add_u32(b
, "timeout", cur_timeout
);
572 blobmsg_add_u8(b
, "file", e
->data
.file
);
573 blobmsg_add_u8(b
, "user", e
->data
.user
);
575 buf
= blobmsg_alloc_string_buffer(b
, "dscp", buf_len
);
576 qosify_map_dscp_codepoint_str(buf
, buf_len
, e
->data
.dscp
);
577 blobmsg_add_string_buffer(b
);
579 blobmsg_add_string(b
, "type", qosify_map_info
[e
->data
.id
].type_name
);
581 buf
= blobmsg_alloc_string_buffer(b
, "value", buf_len
);
582 switch (e
->data
.id
) {
583 case CL_MAP_TCP_PORTS
:
584 case CL_MAP_UDP_PORTS
:
585 snprintf(buf
, buf_len
, "%d", ntohs(e
->data
.addr
.port
));
587 case CL_MAP_IPV4_ADDR
:
588 case CL_MAP_IPV6_ADDR
:
589 af
= e
->data
.id
== CL_MAP_IPV6_ADDR
? AF_INET6
: AF_INET
;
590 inet_ntop(af
, &e
->data
.addr
, buf
, buf_len
);
596 blobmsg_add_string_buffer(b
);
597 blobmsg_close_table(b
, c
);
599 blobmsg_close_array(b
, a
);
602 void qosify_map_update_config(void)
604 int fd
= qosify_map_fds
[CL_MAP_CONFIG
];
607 bpf_map_update_elem(fd
, &key
, &config
, BPF_ANY
);