Skip to content

Commit fc1dc76

Browse files
committed
Merge branch 'bpf-libbpf-consistent-iface'
Andrey Ignatov says: ==================== This patch set renames a few interfaces in libbpf, mostly netlink related, so that all symbols provided by the library have only three possible prefixes: % nm -D tools/lib/bpf/libbpf.so | \ awk '$2 == "T" {sub(/[_\(].*/, "", $3); if ($3) print $3}' | \ sort | \ uniq -c 91 bpf 8 btf 14 libbpf 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. It helps to avoid name conflicts with other libraries and keeps API/ABI 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 (specifically nla_parse, nla_parse_nested and a few others). Some of problematic global symbols are not part of ABI and can be restricted from export with either visibility attribute/pragma or export map (what is useful by itself and can be done in addition). That won't solve the problem for those that are part of ABI though. Also export restrictions would help only in DSO case. If third party application links libbpf statically it won't help, and people do it (e.g. Facebook links most of libraries statically, including libbpf). 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 that use none of mentioned above prefixes and don't fit well into the first two categories. Long term benefits of having common prefix should outweigh possible inconvenience of changing API for those functions now. Patches 2-4 add libbpf_ prefix to libbpf interfaces: separate patch per header. Other patches are simple improvements in API. ==================== Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2 parents d71019b + e5b0863 commit fc1dc76

File tree

11 files changed

+171
-154
lines changed

11 files changed

+171
-154
lines changed

tools/bpf/bpftool/net.c

Lines changed: 22 additions & 19 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;
@@ -127,48 +131,47 @@ static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
127131
tcinfo.array_len = 0;
128132

129133
tcinfo.is_qdisc = false;
130-
ret = nl_get_class(sock, nl_pid, dev->ifindex, dump_class_qdisc_nlmsg,
131-
&tcinfo);
134+
ret = libbpf_nl_get_class(sock, nl_pid, dev->ifindex,
135+
dump_class_qdisc_nlmsg, &tcinfo);
132136
if (ret)
133137
goto out;
134138

135139
tcinfo.is_qdisc = true;
136-
ret = nl_get_qdisc(sock, nl_pid, dev->ifindex, dump_class_qdisc_nlmsg,
137-
&tcinfo);
140+
ret = libbpf_nl_get_qdisc(sock, nl_pid, dev->ifindex,
141+
dump_class_qdisc_nlmsg, &tcinfo);
138142
if (ret)
139143
goto out;
140144

141145
filter_info.devname = dev->devname;
142146
filter_info.ifindex = dev->ifindex;
143147
for (i = 0; i < tcinfo.used_len; i++) {
144148
filter_info.kind = tcinfo.handle_array[i].kind;
145-
ret = nl_get_filter(sock, nl_pid, dev->ifindex,
146-
tcinfo.handle_array[i].handle,
147-
dump_filter_nlmsg,
148-
&filter_info);
149+
ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex,
150+
tcinfo.handle_array[i].handle,
151+
dump_filter_nlmsg, &filter_info);
149152
if (ret)
150153
goto out;
151154
}
152155

153156
/* root, ingress and egress handle */
154157
handle = TC_H_ROOT;
155158
filter_info.kind = "root";
156-
ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle,
157-
dump_filter_nlmsg, &filter_info);
159+
ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
160+
dump_filter_nlmsg, &filter_info);
158161
if (ret)
159162
goto out;
160163

161164
handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
162165
filter_info.kind = "clsact/ingress";
163-
ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle,
164-
dump_filter_nlmsg, &filter_info);
166+
ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
167+
dump_filter_nlmsg, &filter_info);
165168
if (ret)
166169
goto out;
167170

168171
handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
169172
filter_info.kind = "clsact/egress";
170-
ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle,
171-
dump_filter_nlmsg, &filter_info);
173+
ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
174+
dump_filter_nlmsg, &filter_info);
172175
if (ret)
173176
goto out;
174177

@@ -196,7 +199,7 @@ static int do_show(int argc, char **argv)
196199
usage();
197200
}
198201

199-
sock = bpf_netlink_open(&nl_pid);
202+
sock = libbpf_netlink_open(&nl_pid);
200203
if (sock < 0) {
201204
fprintf(stderr, "failed to open netlink sock\n");
202205
return -1;
@@ -211,7 +214,7 @@ static int do_show(int argc, char **argv)
211214
jsonw_start_array(json_wtr);
212215
NET_START_OBJECT;
213216
NET_START_ARRAY("xdp", "%s:\n");
214-
ret = nl_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
217+
ret = libbpf_nl_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
215218
NET_END_ARRAY("\n");
216219

217220
if (!ret) {

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/bpf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
* You should have received a copy of the GNU Lesser General Public
2121
* License along with this program; if not, see <http://www.gnu.org/licenses>
2222
*/
23-
#ifndef __BPF_BPF_H
24-
#define __BPF_BPF_H
23+
#ifndef __LIBBPF_BPF_H
24+
#define __LIBBPF_BPF_H
2525

2626
#include <linux/bpf.h>
2727
#include <stdbool.h>
@@ -111,4 +111,4 @@ int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
111111
int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
112112
__u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
113113
__u64 *probe_addr);
114-
#endif
114+
#endif /* __LIBBPF_BPF_H */

tools/lib/bpf/btf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* SPDX-License-Identifier: LGPL-2.1 */
22
/* Copyright (c) 2018 Facebook */
33

4-
#ifndef __BPF_BTF_H
5-
#define __BPF_BTF_H
4+
#ifndef __LIBBPF_BTF_H
5+
#define __LIBBPF_BTF_H
66

77
#include <linux/types.h>
88

@@ -23,4 +23,4 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id);
2323
int btf__fd(const struct btf *btf);
2424
const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
2525

26-
#endif
26+
#endif /* __LIBBPF_BTF_H */

tools/lib/bpf/libbpf.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ static int bpf_object__elf_init(struct bpf_object *obj)
470470
obj->efile.fd = open(obj->path, O_RDONLY);
471471
if (obj->efile.fd < 0) {
472472
char errmsg[STRERR_BUFSIZE];
473-
char *cp = str_error(errno, errmsg, sizeof(errmsg));
473+
char *cp = libbpf_strerror_r(errno, errmsg,
474+
sizeof(errmsg));
474475

475476
pr_warning("failed to open %s: %s\n", obj->path, cp);
476477
return -errno;
@@ -811,7 +812,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
811812
data->d_size, name, idx);
812813
if (err) {
813814
char errmsg[STRERR_BUFSIZE];
814-
char *cp = str_error(-err, errmsg, sizeof(errmsg));
815+
char *cp = libbpf_strerror_r(-err, errmsg,
816+
sizeof(errmsg));
815817

816818
pr_warning("failed to alloc program %s (%s): %s",
817819
name, obj->path, cp);
@@ -1140,7 +1142,7 @@ bpf_object__create_maps(struct bpf_object *obj)
11401142

11411143
*pfd = bpf_create_map_xattr(&create_attr);
11421144
if (*pfd < 0 && create_attr.btf_key_type_id) {
1143-
cp = str_error(errno, errmsg, sizeof(errmsg));
1145+
cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
11441146
pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
11451147
map->name, cp, errno);
11461148
create_attr.btf_fd = 0;
@@ -1155,7 +1157,7 @@ bpf_object__create_maps(struct bpf_object *obj)
11551157
size_t j;
11561158

11571159
err = *pfd;
1158-
cp = str_error(errno, errmsg, sizeof(errmsg));
1160+
cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
11591161
pr_warning("failed to create map (name: '%s'): %s\n",
11601162
map->name, cp);
11611163
for (j = 0; j < i; j++)
@@ -1339,7 +1341,7 @@ load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
13391341
}
13401342

13411343
ret = -LIBBPF_ERRNO__LOAD;
1342-
cp = str_error(errno, errmsg, sizeof(errmsg));
1344+
cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
13431345
pr_warning("load bpf program failed: %s\n", cp);
13441346

13451347
if (log_buf && log_buf[0] != '\0') {
@@ -1377,7 +1379,7 @@ load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
13771379

13781380
int
13791381
bpf_program__load(struct bpf_program *prog,
1380-
char *license, u32 kern_version)
1382+
char *license, __u32 kern_version)
13811383
{
13821384
int err = 0, fd, i;
13831385

@@ -1655,7 +1657,7 @@ static int check_path(const char *path)
16551657

16561658
dir = dirname(dname);
16571659
if (statfs(dir, &st_fs)) {
1658-
cp = str_error(errno, errmsg, sizeof(errmsg));
1660+
cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
16591661
pr_warning("failed to statfs %s: %s\n", dir, cp);
16601662
err = -errno;
16611663
}
@@ -1691,7 +1693,7 @@ int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
16911693
}
16921694

16931695
if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1694-
cp = str_error(errno, errmsg, sizeof(errmsg));
1696+
cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
16951697
pr_warning("failed to pin program: %s\n", cp);
16961698
return -errno;
16971699
}
@@ -1709,7 +1711,7 @@ static int make_dir(const char *path)
17091711
err = -errno;
17101712

17111713
if (err) {
1712-
cp = str_error(-err, errmsg, sizeof(errmsg));
1714+
cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
17131715
pr_warning("failed to mkdir %s: %s\n", path, cp);
17141716
}
17151717
return err;
@@ -1771,7 +1773,7 @@ int bpf_map__pin(struct bpf_map *map, const char *path)
17711773
}
17721774

17731775
if (bpf_obj_pin(map->fd, path)) {
1774-
cp = str_error(errno, errmsg, sizeof(errmsg));
1776+
cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
17751777
pr_warning("failed to pin map: %s\n", cp);
17761778
return -errno;
17771779
}

tools/lib/bpf/libbpf.h

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
* You should have received a copy of the GNU Lesser General Public
2121
* License along with this program; if not, see <http://www.gnu.org/licenses>
2222
*/
23-
#ifndef __BPF_LIBBPF_H
24-
#define __BPF_LIBBPF_H
23+
#ifndef __LIBBPF_LIBBPF_H
24+
#define __LIBBPF_LIBBPF_H
2525

2626
#include <stdio.h>
2727
#include <stdint.h>
@@ -129,7 +129,7 @@ void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex);
129129
const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
130130

131131
int bpf_program__load(struct bpf_program *prog, char *license,
132-
u32 kern_version);
132+
__u32 kern_version);
133133
int bpf_program__fd(struct bpf_program *prog);
134134
int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
135135
int instance);
@@ -304,18 +304,15 @@ int bpf_perf_event_read_simple(void *mem, unsigned long size,
304304
void **buf, size_t *buf_len,
305305
bpf_perf_event_print_t fn, void *priv);
306306

307-
struct nlmsghdr;
308307
struct nlattr;
309-
typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
310-
typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t,
311-
void *cookie);
312-
int bpf_netlink_open(unsigned int *nl_pid);
313-
int nl_get_link(int sock, unsigned int nl_pid, dump_nlmsg_t dump_link_nlmsg,
314-
void *cookie);
315-
int nl_get_class(int sock, unsigned int nl_pid, int ifindex,
316-
dump_nlmsg_t dump_class_nlmsg, void *cookie);
317-
int nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
318-
dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
319-
int nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
320-
dump_nlmsg_t dump_filter_nlmsg, void *cookie);
321-
#endif
308+
typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
309+
int libbpf_netlink_open(unsigned int *nl_pid);
310+
int libbpf_nl_get_link(int sock, unsigned int nl_pid,
311+
libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
312+
int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
313+
libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
314+
int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
315+
libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
316+
int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
317+
libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
318+
#endif /* __LIBBPF_LIBBPF_H */

0 commit comments

Comments
 (0)