Skip to content

Commit f04bc8a

Browse files
rdnaborkmann
authored andcommitted
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>
1 parent aae5778 commit f04bc8a

File tree

5 files changed

+94
-81
lines changed

5 files changed

+94
-81
lines changed

tools/bpf/bpftool/net.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
6969
snprintf(netinfo->devices[netinfo->used_len].devname,
7070
sizeof(netinfo->devices[netinfo->used_len].devname),
7171
"%s",
72-
tb[IFLA_IFNAME] ? nla_getattr_str(tb[IFLA_IFNAME]) : "");
72+
tb[IFLA_IFNAME]
73+
? libbpf_nla_getattr_str(tb[IFLA_IFNAME])
74+
: "");
7375
netinfo->used_len++;
7476

7577
return do_xdp_dump(ifinfo, tb);
@@ -83,7 +85,7 @@ static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
8385
if (tcinfo->is_qdisc) {
8486
/* skip clsact qdisc */
8587
if (tb[TCA_KIND] &&
86-
strcmp(nla_data(tb[TCA_KIND]), "clsact") == 0)
88+
strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0)
8789
return 0;
8890
if (info->tcm_handle == 0)
8991
return 0;
@@ -101,7 +103,9 @@ static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
101103
snprintf(tcinfo->handle_array[tcinfo->used_len].kind,
102104
sizeof(tcinfo->handle_array[tcinfo->used_len].kind),
103105
"%s",
104-
tb[TCA_KIND] ? nla_getattr_str(tb[TCA_KIND]) : "unknown");
106+
tb[TCA_KIND]
107+
? libbpf_nla_getattr_str(tb[TCA_KIND])
108+
: "unknown");
105109
tcinfo->used_len++;
106110

107111
return 0;

tools/bpf/bpftool/netlink_dumper.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void xdp_dump_prog_id(struct nlattr **tb, int attr,
2121
if (new_json_object)
2222
NET_START_OBJECT
2323
NET_DUMP_STR("mode", " %s", mode);
24-
NET_DUMP_UINT("id", " id %u", nla_getattr_u32(tb[attr]))
24+
NET_DUMP_UINT("id", " id %u", libbpf_nla_getattr_u32(tb[attr]))
2525
if (new_json_object)
2626
NET_END_OBJECT
2727
}
@@ -32,13 +32,13 @@ static int do_xdp_dump_one(struct nlattr *attr, unsigned int ifindex,
3232
struct nlattr *tb[IFLA_XDP_MAX + 1];
3333
unsigned char mode;
3434

35-
if (nla_parse_nested(tb, IFLA_XDP_MAX, attr, NULL) < 0)
35+
if (libbpf_nla_parse_nested(tb, IFLA_XDP_MAX, attr, NULL) < 0)
3636
return -1;
3737

3838
if (!tb[IFLA_XDP_ATTACHED])
3939
return 0;
4040

41-
mode = nla_getattr_u8(tb[IFLA_XDP_ATTACHED]);
41+
mode = libbpf_nla_getattr_u8(tb[IFLA_XDP_ATTACHED]);
4242
if (mode == XDP_ATTACHED_NONE)
4343
return 0;
4444

@@ -75,14 +75,14 @@ int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb)
7575
return 0;
7676

7777
return do_xdp_dump_one(tb[IFLA_XDP], ifinfo->ifi_index,
78-
nla_getattr_str(tb[IFLA_IFNAME]));
78+
libbpf_nla_getattr_str(tb[IFLA_IFNAME]));
7979
}
8080

8181
static int do_bpf_dump_one_act(struct nlattr *attr)
8282
{
8383
struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
8484

85-
if (nla_parse_nested(tb, TCA_ACT_BPF_MAX, attr, NULL) < 0)
85+
if (libbpf_nla_parse_nested(tb, TCA_ACT_BPF_MAX, attr, NULL) < 0)
8686
return -LIBBPF_ERRNO__NLPARSE;
8787

8888
if (!tb[TCA_ACT_BPF_PARMS])
@@ -91,10 +91,10 @@ static int do_bpf_dump_one_act(struct nlattr *attr)
9191
NET_START_OBJECT_NESTED2;
9292
if (tb[TCA_ACT_BPF_NAME])
9393
NET_DUMP_STR("name", "%s",
94-
nla_getattr_str(tb[TCA_ACT_BPF_NAME]));
94+
libbpf_nla_getattr_str(tb[TCA_ACT_BPF_NAME]));
9595
if (tb[TCA_ACT_BPF_ID])
9696
NET_DUMP_UINT("id", " id %u",
97-
nla_getattr_u32(tb[TCA_ACT_BPF_ID]));
97+
libbpf_nla_getattr_u32(tb[TCA_ACT_BPF_ID]));
9898
NET_END_OBJECT_NESTED;
9999
return 0;
100100
}
@@ -106,10 +106,11 @@ static int do_dump_one_act(struct nlattr *attr)
106106
if (!attr)
107107
return 0;
108108

109-
if (nla_parse_nested(tb, TCA_ACT_MAX, attr, NULL) < 0)
109+
if (libbpf_nla_parse_nested(tb, TCA_ACT_MAX, attr, NULL) < 0)
110110
return -LIBBPF_ERRNO__NLPARSE;
111111

112-
if (tb[TCA_ACT_KIND] && strcmp(nla_data(tb[TCA_ACT_KIND]), "bpf") == 0)
112+
if (tb[TCA_ACT_KIND] &&
113+
strcmp(libbpf_nla_data(tb[TCA_ACT_KIND]), "bpf") == 0)
113114
return do_bpf_dump_one_act(tb[TCA_ACT_OPTIONS]);
114115

115116
return 0;
@@ -120,7 +121,7 @@ static int do_bpf_act_dump(struct nlattr *attr)
120121
struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
121122
int act, ret;
122123

123-
if (nla_parse_nested(tb, TCA_ACT_MAX_PRIO, attr, NULL) < 0)
124+
if (libbpf_nla_parse_nested(tb, TCA_ACT_MAX_PRIO, attr, NULL) < 0)
124125
return -LIBBPF_ERRNO__NLPARSE;
125126

126127
NET_START_ARRAY("act", " %s [");
@@ -139,13 +140,15 @@ static int do_bpf_filter_dump(struct nlattr *attr)
139140
struct nlattr *tb[TCA_BPF_MAX + 1];
140141
int ret;
141142

142-
if (nla_parse_nested(tb, TCA_BPF_MAX, attr, NULL) < 0)
143+
if (libbpf_nla_parse_nested(tb, TCA_BPF_MAX, attr, NULL) < 0)
143144
return -LIBBPF_ERRNO__NLPARSE;
144145

145146
if (tb[TCA_BPF_NAME])
146-
NET_DUMP_STR("name", " %s", nla_getattr_str(tb[TCA_BPF_NAME]));
147+
NET_DUMP_STR("name", " %s",
148+
libbpf_nla_getattr_str(tb[TCA_BPF_NAME]));
147149
if (tb[TCA_BPF_ID])
148-
NET_DUMP_UINT("id", " id %u", nla_getattr_u32(tb[TCA_BPF_ID]));
150+
NET_DUMP_UINT("id", " id %u",
151+
libbpf_nla_getattr_u32(tb[TCA_BPF_ID]));
149152
if (tb[TCA_BPF_ACT]) {
150153
ret = do_bpf_act_dump(tb[TCA_BPF_ACT]);
151154
if (ret)
@@ -160,7 +163,8 @@ int do_filter_dump(struct tcmsg *info, struct nlattr **tb, const char *kind,
160163
{
161164
int ret = 0;
162165

163-
if (tb[TCA_OPTIONS] && strcmp(nla_data(tb[TCA_KIND]), "bpf") == 0) {
166+
if (tb[TCA_OPTIONS] &&
167+
strcmp(libbpf_nla_data(tb[TCA_KIND]), "bpf") == 0) {
164168
NET_START_OBJECT;
165169
if (devname[0] != '\0')
166170
NET_DUMP_STR("devname", "%s", devname);

tools/lib/bpf/netlink.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq,
103103
if (!err->error)
104104
continue;
105105
ret = err->error;
106-
nla_dump_errormsg(nh);
106+
libbpf_nla_dump_errormsg(nh);
107107
goto done;
108108
case NLMSG_DONE:
109109
return 0;
@@ -190,7 +190,7 @@ static int __dump_link_nlmsg(struct nlmsghdr *nlh,
190190

191191
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
192192
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)
194194
return -LIBBPF_ERRNO__NLPARSE;
195195

196196
return dump_link_nlmsg(cookie, ifi, tb);
@@ -228,7 +228,7 @@ static int __dump_class_nlmsg(struct nlmsghdr *nlh,
228228

229229
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
230230
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)
232232
return -LIBBPF_ERRNO__NLPARSE;
233233

234234
return dump_class_nlmsg(cookie, t, tb);
@@ -267,7 +267,7 @@ static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
267267

268268
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
269269
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)
271271
return -LIBBPF_ERRNO__NLPARSE;
272272

273273
return dump_qdisc_nlmsg(cookie, t, tb);
@@ -306,7 +306,7 @@ static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
306306

307307
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
308308
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)
310310
return -LIBBPF_ERRNO__NLPARSE;
311311

312312
return dump_filter_nlmsg(cookie, t, tb);

tools/lib/bpf/nlattr.c

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
#include <string.h>
1818
#include <stdio.h>
1919

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,
2727
};
2828

2929
static struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
@@ -47,9 +47,9 @@ static int nla_type(const struct nlattr *nla)
4747
}
4848

4949
static int validate_nla(struct nlattr *nla, int maxtype,
50-
struct nla_policy *policy)
50+
struct libbpf_nla_policy *policy)
5151
{
52-
struct nla_policy *pt;
52+
struct libbpf_nla_policy *pt;
5353
unsigned int minlen = 0;
5454
int type = nla_type(nla);
5555

@@ -58,23 +58,24 @@ static int validate_nla(struct nlattr *nla, int maxtype,
5858

5959
pt = &policy[type];
6060

61-
if (pt->type > NLA_TYPE_MAX)
61+
if (pt->type > LIBBPF_NLA_TYPE_MAX)
6262
return 0;
6363

6464
if (pt->minlen)
6565
minlen = pt->minlen;
66-
else if (pt->type != NLA_UNSPEC)
66+
else if (pt->type != LIBBPF_NLA_UNSPEC)
6767
minlen = nla_attr_minlen[pt->type];
6868

69-
if (nla_len(nla) < minlen)
69+
if (libbpf_nla_len(nla) < minlen)
7070
return -1;
7171

72-
if (pt->maxlen && nla_len(nla) > pt->maxlen)
72+
if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
7373
return -1;
7474

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')
7879
return -1;
7980
}
8081

@@ -104,15 +105,15 @@ static inline int nlmsg_len(const struct nlmsghdr *nlh)
104105
* @see nla_validate
105106
* @return 0 on success or a negative error code.
106107
*/
107-
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
108-
struct nla_policy *policy)
108+
int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
109+
int len, struct libbpf_nla_policy *policy)
109110
{
110111
struct nlattr *nla;
111112
int rem, err;
112113

113114
memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
114115

115-
nla_for_each_attr(nla, head, len, rem) {
116+
libbpf_nla_for_each_attr(nla, head, len, rem) {
116117
int type = nla_type(nla);
117118

118119
if (type > maxtype)
@@ -144,23 +145,25 @@ int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
144145
* @arg policy Attribute validation policy.
145146
*
146147
* Feeds the stream of attributes nested into the specified attribute
147-
* to nla_parse().
148+
* to libbpf_nla_parse().
148149
*
149-
* @see nla_parse
150+
* @see libbpf_nla_parse
150151
* @return 0 on success or a negative error code.
151152
*/
152-
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla,
153-
struct nla_policy *policy)
153+
int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
154+
struct nlattr *nla,
155+
struct libbpf_nla_policy *policy)
154156
{
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);
156159
}
157160

158161
/* dump netlink extended ack error message */
159-
int nla_dump_errormsg(struct nlmsghdr *nlh)
162+
int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh)
160163
{
161-
struct nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = {
162-
[NLMSGERR_ATTR_MSG] = { .type = NLA_STRING },
163-
[NLMSGERR_ATTR_OFFS] = { .type = NLA_U32 },
164+
struct libbpf_nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = {
165+
[NLMSGERR_ATTR_MSG] = { .type = LIBBPF_NLA_STRING },
166+
[NLMSGERR_ATTR_OFFS] = { .type = LIBBPF_NLA_U32 },
164167
};
165168
struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr;
166169
struct nlmsgerr *err;
@@ -181,14 +184,15 @@ int nla_dump_errormsg(struct nlmsghdr *nlh)
181184
attr = (struct nlattr *) ((void *) err + hlen);
182185
alen = nlh->nlmsg_len - hlen;
183186

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) {
185189
fprintf(stderr,
186190
"Failed to parse extended error attributes\n");
187191
return 0;
188192
}
189193

190194
if (tb[NLMSGERR_ATTR_MSG])
191-
errmsg = (char *) nla_data(tb[NLMSGERR_ATTR_MSG]);
195+
errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]);
192196

193197
fprintf(stderr, "Kernel error message: %s\n", errmsg);
194198

0 commit comments

Comments
 (0)