interface: switch to using clsact for filters
[project/qosify.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 39a775ce54ebce57e6247f108423e0517350891d..d38d53f3681751e9cfcb48c6b728a9fa12156ea8 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -9,8 +9,7 @@
 static struct blob_buf b;
 
 static int
-qosify_ubus_add_array(struct blob_attr *attr, struct qosify_dscp_val val,
-                     enum qosify_map_id id)
+qosify_ubus_add_array(struct blob_attr *attr, uint8_t val, enum qosify_map_id id)
 {
        struct blob_attr *cur;
        int rem;
@@ -84,7 +83,7 @@ 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;
-       struct qosify_dscp_val dscp = { 0xff, 0xff };
+       uint8_t dscp = 0xff;
        int ret;
 
        blobmsg_parse(qosify_add_policy, __CL_ADD_MAX, tb,
@@ -130,15 +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_ALIASES,
+       CL_CONFIG_CLASSES,
        __CL_CONFIG_MAX
 };
 
@@ -148,33 +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_ALIASES] = { "aliases", BLOBMSG_TYPE_TABLE },
+       [CL_CONFIG_CLASSES] = { "classes", BLOBMSG_TYPE_TABLE },
 };
 
-static int __set_dscp(struct qosify_dscp_val *dest, struct blob_attr *attr, bool reset)
-{
-       if (reset) {
-                dest->ingress = 0xff;
-                dest->egress = 0xff;
-       }
-
-       if (!attr)
-               return 0;
-
-       if (qosify_map_dscp_value(blobmsg_get_string(attr), dest))
-               return -1;
-
-       return 0;
-}
-
 static int
 qosify_ubus_config(struct ubus_context *ctx, struct ubus_object *obj,
                   struct ubus_request_data *req, const char *method,
@@ -182,7 +155,7 @@ qosify_ubus_config(struct ubus_context *ctx, struct ubus_object *obj,
 {
        struct blob_attr *tb[__CL_CONFIG_MAX];
        struct blob_attr *cur;
-       struct qosify_dscp_val dscp;
+       uint8_t dscp;
        bool reset = false;
        int ret;
 
@@ -195,8 +168,8 @@ qosify_ubus_config(struct ubus_context *ctx, struct ubus_object *obj,
        if (reset)
                qosify_map_reset_config();
 
-       if ((cur = tb[CL_CONFIG_ALIASES]) != NULL || reset)
-               qosify_map_set_aliases(cur);
+       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);
@@ -205,27 +178,17 @@ 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 (dscp.ingress != 0xff)
-               qosify_map_set_dscp_default(CL_MAP_UDP_PORTS, dscp);
-
-       __set_dscp(&dscp, tb[CL_CONFIG_DSCP_TCP], true);
-       if (dscp.ingress != 0xff)
-               qosify_map_set_dscp_default(CL_MAP_TCP_PORTS, dscp);
-
-       if (__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 (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;
 
-       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);
+       map_fill_dscp_value(&dscp, tb[CL_CONFIG_DSCP_UDP], true);
+       if (dscp != 0xff)
+               qosify_map_set_dscp_default(CL_MAP_UDP_PORTS, dscp);
 
-       if ((cur = tb[CL_CONFIG_PRIO_PKT_LEN]) != NULL)
-               config.prio_max_avg_pkt_len = blobmsg_get_u32(cur);
+       map_fill_dscp_value(&dscp, tb[CL_CONFIG_DSCP_TCP], true);
+       if (dscp != 0xff)
+               qosify_map_set_dscp_default(CL_MAP_TCP_PORTS, dscp);
 
        qosify_map_update_config();
 
@@ -344,54 +307,10 @@ static struct ubus_object qosify_object = {
        .n_methods = ARRAY_SIZE(qosify_methods),
 };
 
-static void
-qosify_subscribe_dnsmasq(struct ubus_context *ctx)
-{
-       static struct ubus_subscriber sub = {
-               .cb = qosify_ubus_add_dns_host,
-       };
-       uint32_t id;
-
-       if (!sub.obj.id &&
-           ubus_register_subscriber(ctx, &sub))
-               return;
-
-       if (ubus_lookup_id(ctx, "dnsmasq.dns", &id))
-               return;
-
-       ubus_subscribe(ctx, &sub, id);
-}
-
-static void
-qosify_ubus_event_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
-                    const char *type, struct blob_attr *msg)
-{
-       static const struct blobmsg_policy policy =
-               { "path", BLOBMSG_TYPE_STRING };
-       struct blob_attr *attr;
-       const char *path;
-
-       blobmsg_parse(&policy, 1, &attr, blobmsg_data(msg), blobmsg_len(msg));
-
-       if (!attr)
-               return;
-
-       path = blobmsg_get_string(attr);
-       if (!strcmp(path, "dnsmasq.dns"))
-               qosify_subscribe_dnsmasq(ctx);
-}
-
-
 static void
 ubus_connect_handler(struct ubus_context *ctx)
 {
-       static struct ubus_event_handler ev = {
-               .cb = qosify_ubus_event_cb
-       };
-
        ubus_add_object(ctx, &qosify_object);
-       ubus_register_event_handler(ctx, &ev, "ubus.object.add");
-       qosify_subscribe_dnsmasq(ctx);
 }
 
 static struct ubus_auto_conn conn;
@@ -456,6 +375,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])