From: alardam@gmail.com <alardam@gmail.com>
To: intel-wired-lan@osuosl.org
Subject: [Intel-wired-lan] [PATCH 5/8] libbpf: extend netlink attribute API
Date: Mon, 16 Nov 2020 10:34:49 +0100 [thread overview]
Message-ID: <20201116093452.7541-6-marekx.majtyka@intel.com> (raw)
In-Reply-To: <20201116093452.7541-1-marekx.majtyka@intel.com>
From: Marek Majtyka <marekx.majtyka@intel.com>
Extend netlink attribute API to put a different attribute into
the netlink message (nest{start, end}, string, u32, flag, etc).
Add new API to parse attribute array.
Signed-off-by: Marek Majtyka <marekx.majtyka@intel.com>
---
tools/lib/bpf/nlattr.c | 105 +++++++++++++++++++++++++++++++++++++++++
tools/lib/bpf/nlattr.h | 22 +++++++++
2 files changed, 127 insertions(+)
diff --git a/tools/lib/bpf/nlattr.c b/tools/lib/bpf/nlattr.c
index b607fa9852b1..b37b4d266832 100644
--- a/tools/lib/bpf/nlattr.c
+++ b/tools/lib/bpf/nlattr.c
@@ -83,6 +83,52 @@ static inline int nlmsg_len(const struct nlmsghdr *nlh)
return nlh->nlmsg_len - NLMSG_HDRLEN;
}
+/**
+ * Create attribute index table based on a stream of attributes.
+ * @arg tb Index array to be filled (indexed from 0 to elem-1).
+ * @arg elem Number of attributes in the table.
+ * @arg maxtype Maximum attribute type expected and accepted.
+ * @arg head Head of attribute stream.
+ * @arg policy Attribute validation policy.
+ *
+ * Iterates over the stream of attributes and stores a pointer to each
+ * attribute in the index array using incremented counter as index to
+ * the array. Attribute with a index greater than or equal to the elem value
+ * specified will be ignored and function terminates with error. If a policy
+ * is not NULL, the attribute will be validated using the specified policy.
+ *
+ * @see nla_validate
+ * @return 0 on success or a negative error code.
+ */
+int libbpf_nla_parse_table(struct nlattr *tb[], int elem, struct nlattr *head,
+ int maxtype, struct libbpf_nla_policy *policy)
+{
+ struct nlattr *nla;
+ int rem, err = 0;
+ int idx = 0;
+
+ memset(tb, 0, sizeof(struct nlattr *) * elem);
+
+ libbpf_nla_for_each_attr(nla, libbpf_nla_data(head), libbpf_nla_len(head), rem) {
+ if (idx >= elem) {
+ err = -EMSGSIZE;
+ goto errout;
+ }
+
+ if (policy) {
+ err = validate_nla(nla, maxtype, policy);
+ if (err < 0)
+ goto errout;
+ }
+
+ tb[idx] = nla;
+ idx++;
+ }
+
+errout:
+ return err;
+}
+
/**
* Create attribute index based on a stream of attributes.
* @arg tb Index array to be filled (maxtype+1 elements).
@@ -193,3 +239,62 @@ int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh)
return 0;
}
+
+struct nlattr *libbpf_nla_next(struct nlattr *current)
+{
+ return current + NLA_ALIGN(current->nla_len) / sizeof(struct nlattr);
+}
+
+struct nlattr *libbpf_nla_nest_start(struct nlattr *start, int attrtype)
+{
+ start->nla_len = NLA_HDRLEN;
+ start->nla_type = attrtype | NLA_F_NESTED;
+ return start + 1;
+}
+
+int libbpf_nla_nest_end(struct nlattr *start, struct nlattr *next)
+{
+ start->nla_len += (unsigned char *)next - (unsigned char *)start - NLA_HDRLEN;
+ return start->nla_len;
+}
+
+struct nlattr *libbpf_nla_put_u32(struct nlattr *start, int attrtype, uint32_t val)
+{
+ struct nlattr *next = start + 1;
+
+ start->nla_type = attrtype;
+ start->nla_len = NLA_HDRLEN + NLA_ALIGN(sizeof(uint32_t));
+ memcpy((char *)next, &val, sizeof(uint32_t));
+
+ return next + 1;
+}
+
+struct nlattr *libbpf_nla_put_str(struct nlattr *start, int attrtype,
+ const char *string, int max_len)
+{
+ struct nlattr *next = start + 1;
+ size_t len = max_len > 0 ? strnlen(string, max_len - 1) : 0;
+ char *ptr = ((char *)next) + len;
+
+ start->nla_type = attrtype;
+ start->nla_len = NLA_HDRLEN + NLA_ALIGN(len + 1);
+ strncpy((char *)next, string, len);
+
+ for (size_t idx = len; idx < start->nla_len; ++idx, ptr++)
+ *ptr = '\0';
+
+ return libbpf_nla_next(start);
+}
+
+struct nlattr *libbpf_nla_put_flag(struct nlattr *start, int attrtype)
+{
+ start->nla_type = attrtype;
+ start->nla_len = NLA_HDRLEN;
+
+ return start + 1;
+}
+
+int libbpf_nla_attrs_length(struct nlattr *start, struct nlattr *next)
+{
+ return ((unsigned char *)next - (unsigned char *)start);
+}
diff --git a/tools/lib/bpf/nlattr.h b/tools/lib/bpf/nlattr.h
index 6cc3ac91690f..93e18accfce5 100644
--- a/tools/lib/bpf/nlattr.h
+++ b/tools/lib/bpf/nlattr.h
@@ -76,6 +76,11 @@ static inline uint8_t libbpf_nla_getattr_u8(const struct nlattr *nla)
return *(uint8_t *)libbpf_nla_data(nla);
}
+static inline uint16_t libbpf_nla_getattr_u16(const struct nlattr *nla)
+{
+ return *(uint16_t *)libbpf_nla_data(nla);
+}
+
static inline uint32_t libbpf_nla_getattr_u32(const struct nlattr *nla)
{
return *(uint32_t *)libbpf_nla_data(nla);
@@ -100,7 +105,24 @@ int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
struct nlattr *nla,
struct libbpf_nla_policy *policy);
+int libbpf_nla_parse_table(struct nlattr *tb[], int elem, struct nlattr *head,
+ int type, struct libbpf_nla_policy *policy);
int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh);
+struct nlattr *libbpf_nla_next(struct nlattr *current);
+
+struct nlattr *libbpf_nla_nest_start(struct nlattr *start, int attrtype);
+
+int libbpf_nla_nest_end(struct nlattr *start, struct nlattr *next);
+
+struct nlattr *libbpf_nla_put_u32(struct nlattr *start, int attrtype, uint32_t val);
+
+struct nlattr *libbpf_nla_put_str(struct nlattr *start, int attrtype,
+ const char *string, int max_len);
+
+struct nlattr *libbpf_nla_put_flag(struct nlattr *start, int attrtype);
+
+int libbpf_nla_attrs_length(struct nlattr *start, struct nlattr *next);
+
#endif /* __LIBBPF_NLATTR_H */
--
2.20.1
next prev parent reply other threads:[~2020-11-16 9:34 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-16 9:34 [Intel-wired-lan] [PATCH 0/8] New netdev feature flags for XDP alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 1/8] net: ethtool: extend netdev_features flag set alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 2/8] drivers/net: turn XDP flags on alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 3/8] xsk: add usage of xdp netdev_features flags alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 4/8] xsk: add check for full support of XDP in bind alardam
2020-11-16 9:34 ` alardam [this message]
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 6/8] libbpf: add functions to get XSK modes alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 7/8] libbpf: add API to get XSK/XDP caps alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 8/8] samples/bpf/xdp: apply netdev XDP/XSK modes info alardam
2020-11-16 13:25 ` [Intel-wired-lan] [PATCH 0/8] New netdev feature flags for XDP Toke =?unknown-8bit?q?H=C3=B8iland-J=C3=B8rgensen?=
2020-11-17 7:37 ` Magnus Karlsson
2020-11-17 8:55 ` Marek Majtyka
2020-11-17 18:38 ` Toke =?unknown-8bit?q?H=C3=B8iland-J=C3=B8rgensen?=
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201116093452.7541-6-marekx.majtyka@intel.com \
--to=alardam@gmail.com \
--cc=intel-wired-lan@osuosl.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox