]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
dcb: Generalize dcb_set_attribute()
authorPetr Machata <me@pmachata.org>
Sat, 2 Jan 2021 00:03:37 +0000 (01:03 +0100)
committerDavid Ahern <dsahern@kernel.org>
Mon, 18 Jan 2021 04:09:29 +0000 (04:09 +0000)
The function dcb_set_attribute() takes a fully-formed payload as an
argument. For callers that need to build a nested attribute, such as is the
case for DCB APP table, this is not great, because with libmnl, they would
need to construct a separate netlink message just to pluck out the payload
and hand it over to this function.

Currently, dcb_set_attribute() also always wraps the payload in an
DCB_ATTR_IEEE container, because that is what all the dcb subtools so far
needed. But that is not appropriate for DCBX in particular, and in fact a
handful other attributes, as well as any CEE payloads.

Instead, generalize this code by adding parameters for constructing a
custom payload and for fetching the response from a custom response
attribute. Then add dcb_set_attribute_va(), which takes a callback to
invoke in the right place for the nest to be built, and
dcb_set_attribute_bare(), which is similar to dcb_set_attribute(), but does
not encapsulate the payload in an IEEE container. Rewrite
dcb_set_attribute() compatibly in terms of the new functions.

Signed-off-by: Petr Machata <me@pmachata.org>
Signed-off-by: David Ahern <dsahern@kernel.org>
dcb/dcb.c
dcb/dcb.h

index 6640deef5688bbd942ca6694fb92396f60432150..9bbbbfa74619031bd33e1a6d16326cc580e8db13 100644 (file)
--- a/dcb/dcb.c
+++ b/dcb/dcb.c
@@ -94,12 +94,17 @@ static int dcb_get_attribute_cb(const struct nlmsghdr *nlh, void *data)
        return mnl_attr_parse(nlh, sizeof(struct dcbmsg), dcb_get_attribute_attr_cb, data);
 }
 
+struct dcb_set_attribute_response {
+       int response_attr;
+};
+
 static int dcb_set_attribute_attr_cb(const struct nlattr *attr, void *data)
 {
+       struct dcb_set_attribute_response *resp = data;
        uint16_t len;
        uint8_t err;
 
-       if (mnl_attr_get_type(attr) != DCB_ATTR_IEEE)
+       if (mnl_attr_get_type(attr) != resp->response_attr)
                return MNL_CB_OK;
 
        len = mnl_attr_get_payload_len(attr);
@@ -172,19 +177,23 @@ int dcb_get_attribute(struct dcb *dcb, const char *dev, int attr, void *data, si
        return 0;
 }
 
-int dcb_set_attribute(struct dcb *dcb, const char *dev, int attr, const void *data, size_t data_len)
+static int __dcb_set_attribute(struct dcb *dcb, int command, const char *dev,
+                              int (*cb)(struct dcb *, struct nlmsghdr *, void *),
+                              void *data, int response_attr)
 {
+       struct dcb_set_attribute_response resp = {
+               .response_attr = response_attr,
+       };
        struct nlmsghdr *nlh;
-       struct nlattr *nest;
        int ret;
 
-       nlh = dcb_prepare(dcb, dev, RTM_SETDCB, DCB_CMD_IEEE_SET);
+       nlh = dcb_prepare(dcb, dev, RTM_SETDCB, command);
 
-       nest = mnl_attr_nest_start(nlh, DCB_ATTR_IEEE);
-       mnl_attr_put(nlh, attr, data_len, data);
-       mnl_attr_nest_end(nlh, nest);
+       ret = cb(dcb, nlh, data);
+       if (ret)
+               return ret;
 
-       ret = dcb_talk(dcb, nlh, dcb_set_attribute_cb, NULL);
+       ret = dcb_talk(dcb, nlh, dcb_set_attribute_cb, &resp);
        if (ret) {
                perror("Attribute write");
                return ret;
@@ -192,6 +201,80 @@ int dcb_set_attribute(struct dcb *dcb, const char *dev, int attr, const void *da
        return 0;
 }
 
+struct dcb_set_attribute_ieee_cb {
+       int (*cb)(struct dcb *dcb, struct nlmsghdr *nlh, void *data);
+       void *data;
+};
+
+static int dcb_set_attribute_ieee_cb(struct dcb *dcb, struct nlmsghdr *nlh, void *data)
+{
+       struct dcb_set_attribute_ieee_cb *ieee_data = data;
+       struct nlattr *nest;
+       int ret;
+
+       nest = mnl_attr_nest_start(nlh, DCB_ATTR_IEEE);
+       ret = ieee_data->cb(dcb, nlh, ieee_data->data);
+       if (ret)
+               return ret;
+       mnl_attr_nest_end(nlh, nest);
+
+       return 0;
+}
+
+int dcb_set_attribute_va(struct dcb *dcb, int command, const char *dev,
+                        int (*cb)(struct dcb *dcb, struct nlmsghdr *nlh, void *data),
+                        void *data)
+{
+       struct dcb_set_attribute_ieee_cb ieee_data = {
+               .cb = cb,
+               .data = data,
+       };
+
+       return __dcb_set_attribute(dcb, command, dev,
+                                  &dcb_set_attribute_ieee_cb, &ieee_data,
+                                  DCB_ATTR_IEEE);
+}
+
+struct dcb_set_attribute {
+       int attr;
+       const void *data;
+       size_t data_len;
+};
+
+static int dcb_set_attribute_put(struct dcb *dcb, struct nlmsghdr *nlh, void *data)
+{
+       struct dcb_set_attribute *dsa = data;
+
+       mnl_attr_put(nlh, dsa->attr, dsa->data_len, dsa->data);
+       return 0;
+}
+
+int dcb_set_attribute(struct dcb *dcb, const char *dev, int attr, const void *data, size_t data_len)
+{
+       struct dcb_set_attribute dsa = {
+               .attr = attr,
+               .data = data,
+               .data_len = data_len,
+       };
+
+       return dcb_set_attribute_va(dcb, DCB_CMD_IEEE_SET, dev,
+                                   &dcb_set_attribute_put, &dsa);
+}
+
+int dcb_set_attribute_bare(struct dcb *dcb, int command, const char *dev,
+                          int attr, const void *data, size_t data_len,
+                          int response_attr)
+{
+       struct dcb_set_attribute dsa = {
+               .attr = attr,
+               .data = data,
+               .data_len = data_len,
+       };
+
+       return __dcb_set_attribute(dcb, command, dev,
+                                  &dcb_set_attribute_put, &dsa, response_attr);
+}
+
 void dcb_print_array_u8(const __u8 *array, size_t size)
 {
        SPRINT_BUF(b);
index 388a4204b95c56b33fec835408440aef8680ae1d..da14937c89258d22e3f8a954ce5d9534369dc8fc 100644 (file)
--- a/dcb/dcb.h
+++ b/dcb/dcb.h
@@ -2,6 +2,7 @@
 #ifndef __DCB_H__
 #define __DCB_H__ 1
 
+#include <libmnl/libmnl.h>
 #include <stdbool.h>
 #include <stddef.h>
 
@@ -32,6 +33,12 @@ int dcb_get_attribute(struct dcb *dcb, const char *dev, int attr,
                      void *data, size_t data_len);
 int dcb_set_attribute(struct dcb *dcb, const char *dev, int attr,
                      const void *data, size_t data_len);
+int dcb_set_attribute_va(struct dcb *dcb, int command, const char *dev,
+                        int (*cb)(struct dcb *dcb, struct nlmsghdr *nlh, void *data),
+                        void *data);
+int dcb_set_attribute_bare(struct dcb *dcb, int command, const char *dev,
+                          int attr, const void *data, size_t data_len,
+                          int response_attr);
 
 void dcb_print_named_array(const char *json_name, const char *fp_name,
                           const __u8 *array, size_t size,