Revert "ubus: remove dnsmasq subscriber"
[project/qosify.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index d9b03cf727974d8751ca9cccc3e44629511ef5ad..644df1a8533bc4ec53c680d95a92c70a0a7b1182 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -83,24 +83,19 @@ qosify_ubus_add(struct ubus_context *ctx, struct ubus_object *obj,
        int prev_timemout = qosify_map_timeout;
        struct blob_attr *tb[__CL_ADD_MAX];
        struct blob_attr *cur;
-       int dscp = -1;
+       uint8_t dscp = 0xff;
        int ret;
 
        blobmsg_parse(qosify_add_policy, __CL_ADD_MAX, tb,
                      blobmsg_data(msg), blobmsg_len(msg));
 
        if (!strcmp(method, "add")) {
-               if ((cur = tb[CL_ADD_DSCP]) != NULL)
-                       dscp = qosify_map_dscp_value(blobmsg_get_string(cur));
-               else
-                       return UBUS_STATUS_INVALID_ARGUMENT;
-               if (dscp < 0)
+               if ((cur = tb[CL_ADD_DSCP]) == NULL ||
+                   qosify_map_dscp_value(blobmsg_get_string(cur), &dscp))
                        return UBUS_STATUS_INVALID_ARGUMENT;
 
                if ((cur = tb[CL_ADD_TIMEOUT]) != NULL)
                        qosify_map_timeout = blobmsg_get_u32(cur);
-       } else {
-               dscp = 0xff;
        }
 
        if ((cur = tb[CL_ADD_IPV4]) != NULL &&
@@ -134,14 +129,10 @@ enum {
        CL_CONFIG_TIMEOUT,
        CL_CONFIG_DSCP_UDP,
        CL_CONFIG_DSCP_TCP,
-       CL_CONFIG_DSCP_PRIO,
-       CL_CONFIG_DSCP_BULK,
        CL_CONFIG_DSCP_ICMP,
-       CL_CONFIG_BULK_TIMEOUT,
-       CL_CONFIG_BULK_PPS,
-       CL_CONFIG_PRIO_PKT_LEN,
        CL_CONFIG_INTERFACES,
        CL_CONFIG_DEVICES,
+       CL_CONFIG_CLASSES,
        __CL_CONFIG_MAX
 };
 
@@ -151,35 +142,12 @@ static const struct blobmsg_policy qosify_config_policy[__CL_CONFIG_MAX] = {
        [CL_CONFIG_TIMEOUT] = { "timeout", BLOBMSG_TYPE_INT32 },
        [CL_CONFIG_DSCP_UDP] = { "dscp_default_udp", BLOBMSG_TYPE_STRING },
        [CL_CONFIG_DSCP_TCP] = { "dscp_default_tcp", BLOBMSG_TYPE_STRING },
-       [CL_CONFIG_DSCP_PRIO] = { "dscp_prio", BLOBMSG_TYPE_STRING },
-       [CL_CONFIG_DSCP_BULK] = { "dscp_bulk", BLOBMSG_TYPE_STRING },
        [CL_CONFIG_DSCP_ICMP] = { "dscp_icmp", BLOBMSG_TYPE_STRING },
-       [CL_CONFIG_BULK_TIMEOUT] = { "bulk_trigger_timeout", BLOBMSG_TYPE_INT32 },
-       [CL_CONFIG_BULK_PPS] = { "bulk_trigger_pps", BLOBMSG_TYPE_INT32 },
-       [CL_CONFIG_PRIO_PKT_LEN] = { "prio_max_avg_pkt_len", BLOBMSG_TYPE_INT32 },
        [CL_CONFIG_INTERFACES] = { "interfaces", BLOBMSG_TYPE_TABLE },
        [CL_CONFIG_DEVICES] = { "devices", BLOBMSG_TYPE_TABLE },
+       [CL_CONFIG_CLASSES] = { "classes", BLOBMSG_TYPE_TABLE },
 };
 
-static int __set_dscp(uint8_t *dest, struct blob_attr *attr, bool reset)
-{
-       int dscp;
-
-       if (reset)
-               *dest = 0xff;
-
-       if (!attr)
-               return 0;
-
-       dscp = qosify_map_dscp_value(blobmsg_get_string(attr));
-       if (dscp < 0)
-               return -1;
-
-       *dest = dscp;
-
-       return 0;
-}
-
 static int
 qosify_ubus_config(struct ubus_context *ctx, struct ubus_object *obj,
                   struct ubus_request_data *req, const char *method,
@@ -200,6 +168,9 @@ qosify_ubus_config(struct ubus_context *ctx, struct ubus_object *obj,
        if (reset)
                qosify_map_reset_config();
 
+       if ((cur = tb[CL_CONFIG_CLASSES]) != NULL || reset)
+               qosify_map_set_classes(cur);
+
        if ((cur = tb[CL_CONFIG_TIMEOUT]) != NULL)
                qosify_map_timeout = blobmsg_get_u32(cur);
 
@@ -207,27 +178,18 @@ qosify_ubus_config(struct ubus_context *ctx, struct ubus_object *obj,
            (ret = qosify_ubus_set_files(cur) != 0))
                return ret;
 
-       __set_dscp(&dscp, tb[CL_CONFIG_DSCP_UDP], true);
+       if (map_parse_flow_config(&flow_config, msg, reset) ||
+           map_fill_dscp_value(&config.dscp_icmp, tb[CL_CONFIG_DSCP_ICMP], reset))
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       map_fill_dscp_value(&dscp, tb[CL_CONFIG_DSCP_UDP], true);
        if (dscp != 0xff)
                qosify_map_set_dscp_default(CL_MAP_UDP_PORTS, dscp);
 
-       __set_dscp(&dscp, tb[CL_CONFIG_DSCP_TCP], true);
+       map_fill_dscp_value(&dscp, tb[CL_CONFIG_DSCP_TCP], true);
        if (dscp != 0xff)
                qosify_map_set_dscp_default(CL_MAP_TCP_PORTS, dscp);
 
-       __set_dscp(&config.dscp_prio, tb[CL_CONFIG_DSCP_PRIO], reset);
-       __set_dscp(&config.dscp_bulk, tb[CL_CONFIG_DSCP_BULK], reset);
-       __set_dscp(&config.dscp_icmp, tb[CL_CONFIG_DSCP_ICMP], reset);
-
-       if ((cur = tb[CL_CONFIG_BULK_TIMEOUT]) != NULL)
-               config.bulk_trigger_timeout = blobmsg_get_u32(cur);
-
-       if ((cur = tb[CL_CONFIG_BULK_PPS]) != NULL)
-               config.bulk_trigger_pps = blobmsg_get_u32(cur);
-
-       if ((cur = tb[CL_CONFIG_PRIO_PKT_LEN]) != NULL)
-               config.prio_max_avg_pkt_len = blobmsg_get_u32(cur);
-
        qosify_map_update_config();
 
        qosify_iface_config_update(tb[CL_CONFIG_INTERFACES], tb[CL_CONFIG_DEVICES]);
@@ -345,22 +307,11 @@ static struct ubus_object qosify_object = {
        .n_methods = ARRAY_SIZE(qosify_methods),
 };
 
-static int
-qosify_dnsmasq_cb(struct ubus_context *ctx, struct ubus_object *obj,
-                 struct ubus_request_data *req, const char *method,
-                 struct blob_attr *msg)
-{
-       if (!strcmp(method, "dns_result"))
-               __qosify_ubus_add_dns_host(msg);
-
-       return 0;
-}
-
 static void
 qosify_subscribe_dnsmasq(struct ubus_context *ctx)
 {
        static struct ubus_subscriber sub = {
-               .cb = qosify_dnsmasq_cb,
+               .cb = qosify_ubus_add_dns_host,
        };
        uint32_t id;
 
@@ -468,6 +419,7 @@ int qosify_ubus_check_interface(const char *name, char *ifname, int ifname_len)
        if (ubus_lookup_id(&conn.ctx, obj_name, &id))
                return -1;
 
+       blob_buf_init(&b, 0);
        ubus_invoke(&conn.ctx, id, "status", b.head, netifd_if_cb, &req, 1000);
 
        if (!ifname[0])