qosify: add support for keeping stats
[project/qosify.git] / qosify.h
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
4 */
5 #ifndef __QOS_CLASSIFY_H
6 #define __QOS_CLASSIFY_H
7
8 #include <stdbool.h>
9 #include <regex.h>
10
11 #include <bpf/bpf.h>
12 #include <bpf/libbpf.h>
13
14 #include "qosify-bpf.h"
15
16 #include <libubox/utils.h>
17 #include <libubox/avl.h>
18 #include <libubox/blobmsg.h>
19 #include <libubox/ulog.h>
20
21 #include <netinet/in.h>
22
23 #define CLASSIFY_PROG_PATH "/lib/bpf/qosify-bpf.o"
24 #define CLASSIFY_PIN_PATH "/sys/fs/bpf/qosify"
25 #define CLASSIFY_DATA_PATH "/sys/fs/bpf/qosify_data"
26
27 #define QOSIFY_DNS_IFNAME "ifb-dns"
28
29 #define QOSIFY_PRIO_BASE 0x110
30
31 enum qosify_map_id {
32 CL_MAP_TCP_PORTS,
33 CL_MAP_UDP_PORTS,
34 CL_MAP_IPV4_ADDR,
35 CL_MAP_IPV6_ADDR,
36 CL_MAP_CLASS,
37 CL_MAP_CONFIG,
38 CL_MAP_DNS,
39 __CL_MAP_MAX,
40 };
41
42 struct qosify_map_data {
43 enum qosify_map_id id;
44
45 bool file : 1;
46 bool user : 1;
47
48 uint8_t dscp;
49 uint8_t file_dscp;
50
51 union {
52 uint32_t port;
53 struct in_addr ip;
54 struct in6_addr ip6;
55 struct {
56 uint32_t seq : 30;
57 uint32_t only_cname : 1;
58 const char *pattern;
59 regex_t regex;
60 } dns;
61 } addr;
62 };
63
64 struct qosify_map_entry {
65 struct avl_node avl;
66
67 uint32_t timeout;
68
69 struct qosify_map_data data;
70 };
71
72
73 extern int qosify_map_timeout;
74 extern int qosify_active_timeout;
75 extern struct qosify_config config;
76 extern struct qosify_flow_config flow_config;
77
78 int qosify_run_cmd(char *cmd, bool ignore_error);
79
80 int qosify_loader_init(void);
81 const char *qosify_get_program(uint32_t flags, int *fd);
82
83 int qosify_map_init(void);
84 int qosify_map_dscp_value(const char *val, uint8_t *dscp);
85 int qosify_map_load_file(const char *file);
86 void __qosify_map_set_entry(struct qosify_map_data *data);
87 int qosify_map_set_entry(enum qosify_map_id id, bool file, const char *str,
88 uint8_t dscp);
89 void qosify_map_reload(void);
90 void qosify_map_clear_files(void);
91 void qosify_map_gc(void);
92 void qosify_map_dump(struct blob_buf *b);
93 void qosify_map_stats(struct blob_buf *b, bool reset);
94 void qosify_map_set_dscp_default(enum qosify_map_id id, uint8_t val);
95 void qosify_map_reset_config(void);
96 void qosify_map_update_config(void);
97 void qosify_map_set_classes(struct blob_attr *val);
98 int qosify_map_lookup_dns_entry(char *host, bool cname, uint8_t *dscp, uint32_t *seq);
99 int qosify_map_add_dns_host(char *host, const char *addr, const char *type, int ttl);
100 int map_parse_flow_config(struct qosify_flow_config *cfg, struct blob_attr *attr,
101 bool reset);
102 int map_fill_dscp_value(uint8_t *dest, struct blob_attr *attr, bool reset);
103
104 int qosify_iface_init(void);
105 void qosify_iface_config_update(struct blob_attr *ifaces, struct blob_attr *devs);
106 void qosify_iface_check(void);
107 void qosify_iface_status(struct blob_buf *b);
108 void qosify_iface_get_devices(struct blob_buf *b);
109 void qosify_iface_stop(void);
110
111 int qosify_dns_init(void);
112 void qosify_dns_stop(void);
113
114 int qosify_ubus_init(void);
115 void qosify_ubus_stop(void);
116 int qosify_ubus_check_interface(const char *name, char *ifname, int ifname_len);
117 void qosify_ubus_update_bridger(bool shutdown);
118
119 #endif