Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Andrey Ignatov and committed by
Daniel Borkmann
f04bc8a4 aae57780

+94 -81
+7 -3
tools/bpf/bpftool/net.c
··· 69 69 snprintf(netinfo->devices[netinfo->used_len].devname, 70 70 sizeof(netinfo->devices[netinfo->used_len].devname), 71 71 "%s", 72 - tb[IFLA_IFNAME] ? nla_getattr_str(tb[IFLA_IFNAME]) : ""); 72 + tb[IFLA_IFNAME] 73 + ? libbpf_nla_getattr_str(tb[IFLA_IFNAME]) 74 + : ""); 73 75 netinfo->used_len++; 74 76 75 77 return do_xdp_dump(ifinfo, tb); ··· 85 83 if (tcinfo->is_qdisc) { 86 84 /* skip clsact qdisc */ 87 85 if (tb[TCA_KIND] && 88 - strcmp(nla_data(tb[TCA_KIND]), "clsact") == 0) 86 + strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0) 89 87 return 0; 90 88 if (info->tcm_handle == 0) 91 89 return 0; ··· 103 101 snprintf(tcinfo->handle_array[tcinfo->used_len].kind, 104 102 sizeof(tcinfo->handle_array[tcinfo->used_len].kind), 105 103 "%s", 106 - tb[TCA_KIND] ? nla_getattr_str(tb[TCA_KIND]) : "unknown"); 104 + tb[TCA_KIND] 105 + ? libbpf_nla_getattr_str(tb[TCA_KIND]) 106 + : "unknown"); 107 107 tcinfo->used_len++; 108 108 109 109 return 0;
+5 -5
tools/lib/bpf/netlink.c
··· 103 103 if (!err->error) 104 104 continue; 105 105 ret = err->error; 106 - nla_dump_errormsg(nh); 106 + libbpf_nla_dump_errormsg(nh); 107 107 goto done; 108 108 case NLMSG_DONE: 109 109 return 0; ··· 190 190 191 191 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)); 192 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 194 return -LIBBPF_ERRNO__NLPARSE; 195 195 196 196 return dump_link_nlmsg(cookie, ifi, tb); ··· 228 228 229 229 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 230 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 232 return -LIBBPF_ERRNO__NLPARSE; 233 233 234 234 return dump_class_nlmsg(cookie, t, tb); ··· 267 267 268 268 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 269 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 271 return -LIBBPF_ERRNO__NLPARSE; 272 272 273 273 return dump_qdisc_nlmsg(cookie, t, tb); ··· 306 306 307 307 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 308 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 310 return -LIBBPF_ERRNO__NLPARSE; 311 311 312 312 return dump_filter_nlmsg(cookie, t, tb);
+34 -30
tools/lib/bpf/nlattr.c
··· 17 17 #include <string.h> 18 18 #include <stdio.h> 19 19 20 - static uint16_t nla_attr_minlen[NLA_TYPE_MAX+1] = { 21 - [NLA_U8] = sizeof(uint8_t), 22 - [NLA_U16] = sizeof(uint16_t), 23 - [NLA_U32] = sizeof(uint32_t), 24 - [NLA_U64] = sizeof(uint64_t), 25 - [NLA_STRING] = 1, 26 - [NLA_FLAG] = 0, 20 + static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = { 21 + [LIBBPF_NLA_U8] = sizeof(uint8_t), 22 + [LIBBPF_NLA_U16] = sizeof(uint16_t), 23 + [LIBBPF_NLA_U32] = sizeof(uint32_t), 24 + [LIBBPF_NLA_U64] = sizeof(uint64_t), 25 + [LIBBPF_NLA_STRING] = 1, 26 + [LIBBPF_NLA_FLAG] = 0, 27 27 }; 28 28 29 29 static struct nlattr *nla_next(const struct nlattr *nla, int *remaining) ··· 47 47 } 48 48 49 49 static 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 53 unsigned int minlen = 0; 54 54 int type = nla_type(nla); 55 55 ··· 58 58 59 59 pt = &policy[type]; 60 60 61 - if (pt->type > NLA_TYPE_MAX) 61 + if (pt->type > LIBBPF_NLA_TYPE_MAX) 62 62 return 0; 63 63 64 64 if (pt->minlen) 65 65 minlen = pt->minlen; 66 - else if (pt->type != NLA_UNSPEC) 66 + else if (pt->type != LIBBPF_NLA_UNSPEC) 67 67 minlen = nla_attr_minlen[pt->type]; 68 68 69 - if (nla_len(nla) < minlen) 69 + if (libbpf_nla_len(nla) < minlen) 70 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 73 return -1; 74 74 75 - if (pt->type == NLA_STRING) { 76 - char *data = nla_data(nla); 77 - if (data[nla_len(nla) - 1] != '\0') 75 + if (pt->type == LIBBPF_NLA_STRING) { 76 + char *data = libbpf_nla_data(nla); 77 + 78 + if (data[libbpf_nla_len(nla) - 1] != '\0') 78 79 return -1; 79 80 } 80 81 ··· 105 104 * @see nla_validate 106 105 * @return 0 on success or a negative error code. 107 106 */ 108 - int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, 109 - struct nla_policy *policy) 107 + int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, 108 + int len, struct libbpf_nla_policy *policy) 110 109 { 111 110 struct nlattr *nla; 112 111 int rem, err; 113 112 114 113 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); 115 114 116 - nla_for_each_attr(nla, head, len, rem) { 115 + libbpf_nla_for_each_attr(nla, head, len, rem) { 117 116 int type = nla_type(nla); 118 117 119 118 if (type > maxtype) ··· 145 144 * @arg policy Attribute validation policy. 146 145 * 147 146 * Feeds the stream of attributes nested into the specified attribute 148 - * to nla_parse(). 147 + * to libbpf_nla_parse(). 149 148 * 150 - * @see nla_parse 149 + * @see libbpf_nla_parse 151 150 * @return 0 on success or a negative error code. 152 151 */ 153 - int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, 154 - struct nla_policy *policy) 152 + int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype, 153 + struct nlattr *nla, 154 + struct libbpf_nla_policy *policy) 155 155 { 156 - return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy); 156 + return libbpf_nla_parse(tb, maxtype, libbpf_nla_data(nla), 157 + libbpf_nla_len(nla), policy); 157 158 } 158 159 159 160 /* dump netlink extended ack error message */ 160 - int nla_dump_errormsg(struct nlmsghdr *nlh) 161 + int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh) 161 162 { 162 - struct nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = { 163 - [NLMSGERR_ATTR_MSG] = { .type = NLA_STRING }, 164 - [NLMSGERR_ATTR_OFFS] = { .type = NLA_U32 }, 163 + struct libbpf_nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = { 164 + [NLMSGERR_ATTR_MSG] = { .type = LIBBPF_NLA_STRING }, 165 + [NLMSGERR_ATTR_OFFS] = { .type = LIBBPF_NLA_U32 }, 165 166 }; 166 167 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr; 167 168 struct nlmsgerr *err; ··· 184 181 attr = (struct nlattr *) ((void *) err + hlen); 185 182 alen = nlh->nlmsg_len - hlen; 186 183 187 - if (nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen, extack_policy) != 0) { 184 + if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen, 185 + extack_policy) != 0) { 188 186 fprintf(stderr, 189 187 "Failed to parse extended error attributes\n"); 190 188 return 0; 191 189 } 192 190 193 191 if (tb[NLMSGERR_ATTR_MSG]) 194 - errmsg = (char *) nla_data(tb[NLMSGERR_ATTR_MSG]); 192 + errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]); 195 193 196 194 fprintf(stderr, "Kernel error message: %s\n", errmsg); 197 195
+30 -29
tools/lib/bpf/nlattr.h
··· 23 23 * Standard attribute types to specify validation policy 24 24 */ 25 25 enum { 26 - NLA_UNSPEC, /**< Unspecified type, binary data chunk */ 27 - NLA_U8, /**< 8 bit integer */ 28 - NLA_U16, /**< 16 bit integer */ 29 - NLA_U32, /**< 32 bit integer */ 30 - NLA_U64, /**< 64 bit integer */ 31 - NLA_STRING, /**< NUL terminated character string */ 32 - NLA_FLAG, /**< Flag */ 33 - NLA_MSECS, /**< Micro seconds (64bit) */ 34 - NLA_NESTED, /**< Nested attributes */ 35 - __NLA_TYPE_MAX, 26 + LIBBPF_NLA_UNSPEC, /**< Unspecified type, binary data chunk */ 27 + LIBBPF_NLA_U8, /**< 8 bit integer */ 28 + LIBBPF_NLA_U16, /**< 16 bit integer */ 29 + LIBBPF_NLA_U32, /**< 32 bit integer */ 30 + LIBBPF_NLA_U64, /**< 64 bit integer */ 31 + LIBBPF_NLA_STRING, /**< NUL terminated character string */ 32 + LIBBPF_NLA_FLAG, /**< Flag */ 33 + LIBBPF_NLA_MSECS, /**< Micro seconds (64bit) */ 34 + LIBBPF_NLA_NESTED, /**< Nested attributes */ 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 41 * @ingroup attr ··· 43 43 * 44 44 * See section @core_doc{core_attr_parse,Attribute Parsing} for more details. 45 45 */ 46 - struct nla_policy { 47 - /** Type of attribute or NLA_UNSPEC */ 46 + struct libbpf_nla_policy { 47 + /** Type of attribute or LIBBPF_NLA_UNSPEC */ 48 48 uint16_t type; 49 49 50 50 /** Minimal length of payload required */ ··· 62 62 * @arg len length of attribute stream 63 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 66 for (pos = head, rem = len; \ 67 67 nla_ok(pos, rem); \ 68 68 pos = nla_next(pos, &(rem))) 69 69 70 70 /** 71 - * nla_data - head of payload 71 + * libbpf_nla_data - head of payload 72 72 * @nla: netlink attribute 73 73 */ 74 - static inline void *nla_data(const struct nlattr *nla) 74 + static inline void *libbpf_nla_data(const struct nlattr *nla) 75 75 { 76 76 return (char *) nla + NLA_HDRLEN; 77 77 } 78 78 79 - static inline uint8_t nla_getattr_u8(const struct nlattr *nla) 79 + static 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 84 - static inline uint32_t nla_getattr_u32(const struct nlattr *nla) 84 + static 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 89 - static inline const char *nla_getattr_str(const struct nlattr *nla) 89 + static 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 96 * @nla: netlink attribute 97 97 */ 98 - static inline int nla_len(const struct nlattr *nla) 98 + static inline int libbpf_nla_len(const struct nlattr *nla) 99 99 { 100 100 return nla->nla_len - NLA_HDRLEN; 101 101 } 102 102 103 - int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, 104 - struct nla_policy *policy); 105 - int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, 106 - struct nla_policy *policy); 103 + int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, 104 + int len, struct libbpf_nla_policy *policy); 105 + int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype, 106 + struct nlattr *nla, 107 + struct libbpf_nla_policy *policy); 107 108 108 - int nla_dump_errormsg(struct nlmsghdr *nlh); 109 + int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh); 109 110 110 111 #endif /* __NLATTR_H */