aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf
diff options
context:
space:
mode:
authorAndrey Ignatov <rdna@fb.com>2018-10-03 18:26:40 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2018-10-04 10:04:16 -0400
commitf04bc8a436e1b32f842a631ff889954bdf56b720 (patch)
treec598e9e8e3ea958f2e786da456dce11e3f6bf664 /tools/lib/bpf
parentaae57780107d92de2463e605cb054656ebd233d1 (diff)
libbpf: Consistent prefixes for interfaces in nlattr.h.
libbpf is used more and more outside kernel tree. That means the library should follow good practices in library design and implementation to play well with third party code that uses it. One of such practices is to have a common prefix (or a few) for every interface, function or data structure, library provides. I helps to avoid name conflicts with other libraries and keeps API consistent. Inconsistent names in libbpf already cause problems in real life. E.g. an application can't use both libbpf and libnl due to conflicting symbols. Having common prefix will help to fix current and avoid future problems. libbpf already uses the following prefixes for its interfaces: * bpf_ for bpf system call wrappers, program/map/elf-object abstractions and a few other things; * btf_ for BTF related API; * libbpf_ for everything else. The patch adds libbpf_ prefix to interfaces in nlattr.h that use none of mentioned above prefixes and doesn't fit well into the first two categories. Since affected part of API is used in bpftool, the patch applies corresponding change to bpftool as well. Having it in a separate patch will cause a state of tree where bpftool is broken what may not be a good idea. Signed-off-by: Andrey Ignatov <rdna@fb.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/lib/bpf')
-rw-r--r--tools/lib/bpf/netlink.c10
-rw-r--r--tools/lib/bpf/nlattr.c64
-rw-r--r--tools/lib/bpf/nlattr.h59
3 files changed, 69 insertions, 64 deletions
diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index 506bdfdbcab0..2d2edbbd8ae8 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -103,7 +103,7 @@ static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq,
103 if (!err->error) 103 if (!err->error)
104 continue; 104 continue;
105 ret = err->error; 105 ret = err->error;
106 nla_dump_errormsg(nh); 106 libbpf_nla_dump_errormsg(nh);
107 goto done; 107 goto done;
108 case NLMSG_DONE: 108 case NLMSG_DONE:
109 return 0; 109 return 0;
@@ -190,7 +190,7 @@ static int __dump_link_nlmsg(struct nlmsghdr *nlh,
190 190
191 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)); 191 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
192 attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi))); 192 attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
193 if (nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0) 193 if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
194 return -LIBBPF_ERRNO__NLPARSE; 194 return -LIBBPF_ERRNO__NLPARSE;
195 195
196 return dump_link_nlmsg(cookie, ifi, tb); 196 return dump_link_nlmsg(cookie, ifi, tb);
@@ -228,7 +228,7 @@ static int __dump_class_nlmsg(struct nlmsghdr *nlh,
228 228
229 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 229 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
230 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 230 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
231 if (nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 231 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
232 return -LIBBPF_ERRNO__NLPARSE; 232 return -LIBBPF_ERRNO__NLPARSE;
233 233
234 return dump_class_nlmsg(cookie, t, tb); 234 return dump_class_nlmsg(cookie, t, tb);
@@ -267,7 +267,7 @@ static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
267 267
268 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 268 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
269 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 269 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
270 if (nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 270 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
271 return -LIBBPF_ERRNO__NLPARSE; 271 return -LIBBPF_ERRNO__NLPARSE;
272 272
273 return dump_qdisc_nlmsg(cookie, t, tb); 273 return dump_qdisc_nlmsg(cookie, t, tb);
@@ -306,7 +306,7 @@ static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
306 306
307 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 307 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
308 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 308 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
309 if (nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 309 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
310 return -LIBBPF_ERRNO__NLPARSE; 310 return -LIBBPF_ERRNO__NLPARSE;
311 311
312 return dump_filter_nlmsg(cookie, t, tb); 312 return dump_filter_nlmsg(cookie, t, tb);
diff --git a/tools/lib/bpf/nlattr.c b/tools/lib/bpf/nlattr.c
index 49f514119bdb..e52257a7367a 100644
--- a/tools/lib/bpf/nlattr.c
+++ b/tools/lib/bpf/nlattr.c
@@ -17,13 +17,13 @@
17#include <string.h> 17#include <string.h>
18#include <stdio.h> 18#include <stdio.h>
19 19
20static uint16_t nla_attr_minlen[NLA_TYPE_MAX+1] = { 20static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = {
21 [NLA_U8] = sizeof(uint8_t), 21 [LIBBPF_NLA_U8] = sizeof(uint8_t),
22 [NLA_U16] = sizeof(uint16_t), 22 [LIBBPF_NLA_U16] = sizeof(uint16_t),
23 [NLA_U32] = sizeof(uint32_t), 23 [LIBBPF_NLA_U32] = sizeof(uint32_t),
24 [NLA_U64] = sizeof(uint64_t), 24 [LIBBPF_NLA_U64] = sizeof(uint64_t),
25 [NLA_STRING] = 1, 25 [LIBBPF_NLA_STRING] = 1,
26 [NLA_FLAG] = 0, 26 [LIBBPF_NLA_FLAG] = 0,
27}; 27};
28 28
29static struct nlattr *nla_next(const struct nlattr *nla, int *remaining) 29static struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
@@ -47,9 +47,9 @@ static int nla_type(const struct nlattr *nla)
47} 47}
48 48
49static int validate_nla(struct nlattr *nla, int maxtype, 49static int validate_nla(struct nlattr *nla, int maxtype,
50 struct nla_policy *policy) 50 struct libbpf_nla_policy *policy)
51{ 51{
52 struct nla_policy *pt; 52 struct libbpf_nla_policy *pt;
53 unsigned int minlen = 0; 53 unsigned int minlen = 0;
54 int type = nla_type(nla); 54 int type = nla_type(nla);
55 55
@@ -58,23 +58,24 @@ static int validate_nla(struct nlattr *nla, int maxtype,
58 58
59 pt = &policy[type]; 59 pt = &policy[type];
60 60
61 if (pt->type > NLA_TYPE_MAX) 61 if (pt->type > LIBBPF_NLA_TYPE_MAX)
62 return 0; 62 return 0;
63 63
64 if (pt->minlen) 64 if (pt->minlen)
65 minlen = pt->minlen; 65 minlen = pt->minlen;
66 else if (pt->type != NLA_UNSPEC) 66 else if (pt->type != LIBBPF_NLA_UNSPEC)
67 minlen = nla_attr_minlen[pt->type]; 67 minlen = nla_attr_minlen[pt->type];
68 68
69 if (nla_len(nla) < minlen) 69 if (libbpf_nla_len(nla) < minlen)
70 return -1; 70 return -1;
71 71
72 if (pt->maxlen && nla_len(nla) > pt->maxlen) 72 if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
73 return -1; 73 return -1;
74 74
75 if (pt->type == NLA_STRING) { 75 if (pt->type == LIBBPF_NLA_STRING) {
76 char *data = nla_data(nla); 76 char *data = libbpf_nla_data(nla);
77 if (data[nla_len(nla) - 1] != '\0') 77
78 if (data[libbpf_nla_len(nla) - 1] != '\0')
78 return -1; 79 return -1;
79 } 80 }
80 81
@@ -104,15 +105,15 @@ static inline int nlmsg_len(const struct nlmsghdr *nlh)
104 * @see nla_validate 105 * @see nla_validate
105 * @return 0 on success or a negative error code. 106 * @return 0 on success or a negative error code.
106 */ 107 */
107int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, 108int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
108 struct nla_policy *policy) 109 int len, struct libbpf_nla_policy *policy)
109{ 110{
110 struct nlattr *nla; 111 struct nlattr *nla;
111 int rem, err; 112 int rem, err;
112 113
113 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); 114 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
114 115
115 nla_for_each_attr(nla, head, len, rem) { 116 libbpf_nla_for_each_attr(nla, head, len, rem) {
116 int type = nla_type(nla); 117 int type = nla_type(nla);
117 118
118 if (type > maxtype) 119 if (type > maxtype)
@@ -144,23 +145,25 @@ errout:
144 * @arg policy Attribute validation policy. 145 * @arg policy Attribute validation policy.
145 * 146 *
146 * Feeds the stream of attributes nested into the specified attribute 147 * Feeds the stream of attributes nested into the specified attribute
147 * to nla_parse(). 148 * to libbpf_nla_parse().
148 * 149 *
149 * @see nla_parse 150 * @see libbpf_nla_parse
150 * @return 0 on success or a negative error code. 151 * @return 0 on success or a negative error code.
151 */ 152 */
152int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, 153int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
153 struct nla_policy *policy) 154 struct nlattr *nla,
155 struct libbpf_nla_policy *policy)
154{ 156{
155 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy); 157 return libbpf_nla_parse(tb, maxtype, libbpf_nla_data(nla),
158 libbpf_nla_len(nla), policy);
156} 159}
157 160
158/* dump netlink extended ack error message */ 161/* dump netlink extended ack error message */
159int nla_dump_errormsg(struct nlmsghdr *nlh) 162int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh)
160{ 163{
161 struct nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = { 164 struct libbpf_nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = {
162 [NLMSGERR_ATTR_MSG] = { .type = NLA_STRING }, 165 [NLMSGERR_ATTR_MSG] = { .type = LIBBPF_NLA_STRING },
163 [NLMSGERR_ATTR_OFFS] = { .type = NLA_U32 }, 166 [NLMSGERR_ATTR_OFFS] = { .type = LIBBPF_NLA_U32 },
164 }; 167 };
165 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr; 168 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr;
166 struct nlmsgerr *err; 169 struct nlmsgerr *err;
@@ -181,14 +184,15 @@ int nla_dump_errormsg(struct nlmsghdr *nlh)
181 attr = (struct nlattr *) ((void *) err + hlen); 184 attr = (struct nlattr *) ((void *) err + hlen);
182 alen = nlh->nlmsg_len - hlen; 185 alen = nlh->nlmsg_len - hlen;
183 186
184 if (nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen, extack_policy) != 0) { 187 if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen,
188 extack_policy) != 0) {
185 fprintf(stderr, 189 fprintf(stderr,
186 "Failed to parse extended error attributes\n"); 190 "Failed to parse extended error attributes\n");
187 return 0; 191 return 0;
188 } 192 }
189 193
190 if (tb[NLMSGERR_ATTR_MSG]) 194 if (tb[NLMSGERR_ATTR_MSG])
191 errmsg = (char *) nla_data(tb[NLMSGERR_ATTR_MSG]); 195 errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]);
192 196
193 fprintf(stderr, "Kernel error message: %s\n", errmsg); 197 fprintf(stderr, "Kernel error message: %s\n", errmsg);
194 198
diff --git a/tools/lib/bpf/nlattr.h b/tools/lib/bpf/nlattr.h
index a6e2396bce7c..755a3312c87f 100644
--- a/tools/lib/bpf/nlattr.h
+++ b/tools/lib/bpf/nlattr.h
@@ -23,19 +23,19 @@
23 * Standard attribute types to specify validation policy 23 * Standard attribute types to specify validation policy
24 */ 24 */
25enum { 25enum {
26 NLA_UNSPEC, /**< Unspecified type, binary data chunk */ 26 LIBBPF_NLA_UNSPEC, /**< Unspecified type, binary data chunk */
27 NLA_U8, /**< 8 bit integer */ 27 LIBBPF_NLA_U8, /**< 8 bit integer */
28 NLA_U16, /**< 16 bit integer */ 28 LIBBPF_NLA_U16, /**< 16 bit integer */
29 NLA_U32, /**< 32 bit integer */ 29 LIBBPF_NLA_U32, /**< 32 bit integer */
30 NLA_U64, /**< 64 bit integer */ 30 LIBBPF_NLA_U64, /**< 64 bit integer */
31 NLA_STRING, /**< NUL terminated character string */ 31 LIBBPF_NLA_STRING, /**< NUL terminated character string */
32 NLA_FLAG, /**< Flag */ 32 LIBBPF_NLA_FLAG, /**< Flag */
33 NLA_MSECS, /**< Micro seconds (64bit) */ 33 LIBBPF_NLA_MSECS, /**< Micro seconds (64bit) */
34 NLA_NESTED, /**< Nested attributes */ 34 LIBBPF_NLA_NESTED, /**< Nested attributes */
35 __NLA_TYPE_MAX, 35 __LIBBPF_NLA_TYPE_MAX,
36}; 36};
37 37
38#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) 38#define LIBBPF_NLA_TYPE_MAX (__LIBBPF_NLA_TYPE_MAX - 1)
39 39
40/** 40/**
41 * @ingroup attr 41 * @ingroup attr
@@ -43,8 +43,8 @@ enum {
43 * 43 *
44 * See section @core_doc{core_attr_parse,Attribute Parsing} for more details. 44 * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
45 */ 45 */
46struct nla_policy { 46struct libbpf_nla_policy {
47 /** Type of attribute or NLA_UNSPEC */ 47 /** Type of attribute or LIBBPF_NLA_UNSPEC */
48 uint16_t type; 48 uint16_t type;
49 49
50 /** Minimal length of payload required */ 50 /** Minimal length of payload required */
@@ -62,49 +62,50 @@ struct nla_policy {
62 * @arg len length of attribute stream 62 * @arg len length of attribute stream
63 * @arg rem initialized to len, holds bytes currently remaining in stream 63 * @arg rem initialized to len, holds bytes currently remaining in stream
64 */ 64 */
65#define nla_for_each_attr(pos, head, len, rem) \ 65#define libbpf_nla_for_each_attr(pos, head, len, rem) \
66 for (pos = head, rem = len; \ 66 for (pos = head, rem = len; \
67 nla_ok(pos, rem); \ 67 nla_ok(pos, rem); \
68 pos = nla_next(pos, &(rem))) 68 pos = nla_next(pos, &(rem)))
69 69
70/** 70/**
71 * nla_data - head of payload 71 * libbpf_nla_data - head of payload
72 * @nla: netlink attribute 72 * @nla: netlink attribute
73 */ 73 */
74static inline void *nla_data(const struct nlattr *nla) 74static inline void *libbpf_nla_data(const struct nlattr *nla)
75{ 75{
76 return (char *) nla + NLA_HDRLEN; 76 return (char *) nla + NLA_HDRLEN;
77} 77}
78 78
79static inline uint8_t nla_getattr_u8(const struct nlattr *nla) 79static inline uint8_t libbpf_nla_getattr_u8(const struct nlattr *nla)
80{ 80{
81 return *(uint8_t *)nla_data(nla); 81 return *(uint8_t *)libbpf_nla_data(nla);
82} 82}
83 83
84static inline uint32_t nla_getattr_u32(const struct nlattr *nla) 84static inline uint32_t libbpf_nla_getattr_u32(const struct nlattr *nla)
85{ 85{
86 return *(uint32_t *)nla_data(nla); 86 return *(uint32_t *)libbpf_nla_data(nla);
87} 87}
88 88
89static inline const char *nla_getattr_str(const struct nlattr *nla) 89static inline const char *libbpf_nla_getattr_str(const struct nlattr *nla)
90{ 90{
91 return (const char *)nla_data(nla); 91 return (const char *)libbpf_nla_data(nla);
92} 92}
93 93
94/** 94/**
95 * nla_len - length of payload 95 * libbpf_nla_len - length of payload
96 * @nla: netlink attribute 96 * @nla: netlink attribute
97 */ 97 */
98static inline int nla_len(const struct nlattr *nla) 98static inline int libbpf_nla_len(const struct nlattr *nla)
99{ 99{
100 return nla->nla_len - NLA_HDRLEN; 100 return nla->nla_len - NLA_HDRLEN;
101} 101}
102 102
103int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, 103int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
104 struct nla_policy *policy); 104 int len, struct libbpf_nla_policy *policy);
105int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, 105int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
106 struct nla_policy *policy); 106 struct nlattr *nla,
107 struct libbpf_nla_policy *policy);
107 108
108int nla_dump_errormsg(struct nlmsghdr *nlh); 109int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh);
109 110
110#endif /* __NLATTR_H */ 111#endif /* __NLATTR_H */