From 74e0222eeb9e62f4d5073a5b3d9208678782a198 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Sun, 17 May 2020 20:39:44 +0200 Subject: [PATCH] vlandev: support setting ingress/egress QoS mappings It allows setting mappings for instance this way: """ config device option name 'vlan41' option type '8021q' option vid '41' option ifname 'eth1' list ingress_qos_mapping '1:2' list ingress_qos_mapping '2:5' list egress_qos_mapping '0:3' """ Signed-off-by: Pau Espin Pedrol Tested-by: Pedro --- system-linux.c | 25 +++++++++++++++++- system.h | 9 +++++++ vlandev.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/system-linux.c b/system-linux.c index 62636c4..c225175 100644 --- a/system-linux.c +++ b/system-linux.c @@ -74,6 +74,7 @@ #include "netifd.h" #include "device.h" #include "system.h" +#include "utils.h" struct event_socket { struct uloop_fd uloop; @@ -1401,8 +1402,10 @@ int system_vlan_del(struct device *dev) int system_vlandev_add(struct device *vlandev, struct device *dev, struct vlandev_config *cfg) { struct nl_msg *msg; - struct nlattr *linkinfo, *data; + struct nlattr *linkinfo, *data, *qos; struct ifinfomsg iim = { .ifi_family = AF_UNSPEC }; + struct vlan_qos_mapping *elem; + struct ifla_vlan_qos_mapping nl_qos_map; int rv; msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL); @@ -1431,6 +1434,26 @@ int system_vlandev_add(struct device *vlandev, struct device *dev, struct vlande netifd_log_message(L_WARNING, "%s Your kernel is older than linux 3.10.0, 802.1ad is not supported defaulting to 802.1q", vlandev->type->name); #endif + if (!(qos = nla_nest_start(msg, IFLA_VLAN_INGRESS_QOS))) + goto nla_put_failure; + + vlist_simple_for_each_element(&cfg->ingress_qos_mapping_list, elem, node) { + nl_qos_map.from = elem->from; + nl_qos_map.to = elem->to; + nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map); + } + nla_nest_end(msg, qos); + + if (!(qos = nla_nest_start(msg, IFLA_VLAN_EGRESS_QOS))) + goto nla_put_failure; + + vlist_simple_for_each_element(&cfg->egress_qos_mapping_list, elem, node) { + nl_qos_map.from = elem->from; + nl_qos_map.to = elem->to; + nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map); + } + nla_nest_end(msg, qos); + nla_nest_end(msg, data); nla_nest_end(msg, linkinfo); diff --git a/system.h b/system.h index b377416..252fd92 100644 --- a/system.h +++ b/system.h @@ -21,6 +21,7 @@ #include "device.h" #include "interface-ip.h" #include "iprule.h" +#include "utils.h" enum tunnel_param { TUNNEL_ATTR_TYPE, @@ -158,9 +159,17 @@ enum vlan_proto { VLAN_PROTO_8021AD = 0x88A8 }; +struct vlan_qos_mapping { + struct vlist_simple_node node; /* entry in vlandev_config->{e,in}gress_qos_mapping_list */ + uint32_t from; + uint32_t to; +}; + struct vlandev_config { enum vlan_proto proto; uint16_t vid; + struct vlist_simple_tree ingress_qos_mapping_list; /* list of struct vlan_qos_mapping */ + struct vlist_simple_tree egress_qos_mapping_list; /* list of struct vlan_qos_mapping */ }; static inline int system_get_addr_family(unsigned int flags) diff --git a/vlandev.c b/vlandev.c index ceaeb3e..10b78e2 100644 --- a/vlandev.c +++ b/vlandev.c @@ -13,21 +13,27 @@ */ #include +#include #include "netifd.h" #include "device.h" #include "interface.h" #include "system.h" +#include "utils.h" enum { VLANDEV_ATTR_IFNAME, VLANDEV_ATTR_VID, + VLANDEV_ATTR_INGRESS_QOS_MAPPING, + VLANDEV_ATTR_EGRESS_QOS_MAPPING, __VLANDEV_ATTR_MAX }; static const struct blobmsg_policy vlandev_attrs[__VLANDEV_ATTR_MAX] = { [VLANDEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_STRING }, [VLANDEV_ATTR_VID] = { "vid", BLOBMSG_TYPE_INT32 }, + [VLANDEV_ATTR_INGRESS_QOS_MAPPING] = { "ingress_qos_mapping", BLOBMSG_TYPE_ARRAY }, + [VLANDEV_ATTR_EGRESS_QOS_MAPPING] = { "egress_qos_mapping", BLOBMSG_TYPE_ARRAY }, }; static const struct uci_blob_param_list vlandev_attr_list = { @@ -126,9 +132,30 @@ vlandev_free(struct device *dev) mvdev = container_of(dev, struct vlandev_device, dev); device_remove_user(&mvdev->parent); free(mvdev->config_data); + vlist_simple_flush_all(&mvdev->config.ingress_qos_mapping_list); + vlist_simple_flush_all(&mvdev->config.egress_qos_mapping_list); free(mvdev); } +static void vlandev_qos_mapping_dump(struct blob_buf *b, const char *name, const struct vlist_simple_tree *qos_mapping_li) +{ + const struct vlan_qos_mapping *elem; + void *a, *t; + + a = blobmsg_open_array(b, name); + + vlist_simple_for_each_element(qos_mapping_li, elem, node) { + t = blobmsg_open_table(b, NULL); + + blobmsg_add_u32(b, "from", elem->from); + blobmsg_add_u32(b, "to", elem->to); + + blobmsg_close_table(b, t); + } + + blobmsg_close_array(b, a); +} + static void vlandev_dump_info(struct device *dev, struct blob_buf *b) { @@ -137,6 +164,8 @@ vlandev_dump_info(struct device *dev, struct blob_buf *b) mvdev = container_of(dev, struct vlandev_device, dev); blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname); system_if_dump_info(dev, b); + vlandev_qos_mapping_dump(b, "ingress_qos_mapping", &mvdev->config.ingress_qos_mapping_list); + vlandev_qos_mapping_dump(b, "egress_qos_mapping", &mvdev->config.egress_qos_mapping_list); } static void @@ -152,6 +181,32 @@ vlandev_config_init(struct device *dev) device_add_user(&mvdev->parent, basedev); } +static void vlandev_qos_mapping_list_apply(struct vlist_simple_tree *qos_mapping_li, struct blob_attr *list) +{ + struct blob_attr *cur; + struct vlan_qos_mapping *qos_mapping; + int rem, rc; + + blobmsg_for_each_attr(cur, list, rem) { + if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) + continue; + + if (!blobmsg_check_attr(cur, false)) + continue; + + qos_mapping = calloc(1, sizeof(*qos_mapping)); + if (!qos_mapping) + continue; + + rc = sscanf(blobmsg_data(cur), "%" PRIu32 ":%" PRIu32, &qos_mapping->from, &qos_mapping->to); + if (rc != 2) { + free(qos_mapping); + continue; + } + vlist_simple_add(qos_mapping_li, &qos_mapping->node); + } +} + static void vlandev_apply_settings(struct vlandev_device *mvdev, struct blob_attr **tb) { @@ -162,8 +217,20 @@ vlandev_apply_settings(struct vlandev_device *mvdev, struct blob_attr **tb) VLAN_PROTO_8021Q : VLAN_PROTO_8021AD; cfg->vid = 1; + vlist_simple_update(&cfg->ingress_qos_mapping_list); + vlist_simple_update(&cfg->egress_qos_mapping_list); + if ((cur = tb[VLANDEV_ATTR_VID])) cfg->vid = (uint16_t) blobmsg_get_u32(cur); + + if ((cur = tb[VLANDEV_ATTR_INGRESS_QOS_MAPPING])) + vlandev_qos_mapping_list_apply(&cfg->ingress_qos_mapping_list, cur); + + if ((cur = tb[VLANDEV_ATTR_EGRESS_QOS_MAPPING])) + vlandev_qos_mapping_list_apply(&cfg->egress_qos_mapping_list, cur); + + vlist_simple_flush(&cfg->ingress_qos_mapping_list); + vlist_simple_flush(&cfg->egress_qos_mapping_list); } static enum dev_change_type @@ -221,6 +288,11 @@ vlandev_create(const char *name, struct device_type *devtype, if (!mvdev) return NULL; + vlist_simple_init(&mvdev->config.ingress_qos_mapping_list, + struct vlan_qos_mapping, node); + vlist_simple_init(&mvdev->config.egress_qos_mapping_list, + struct vlan_qos_mapping, node); + dev = &mvdev->dev; if (device_init(dev, devtype, name) < 0) { -- 2.30.2