aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Borkmann <daniel@iogearbox.net>2018-10-04 10:04:17 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2018-10-04 10:04:18 -0400
commitfc1dc7665145e978216a2dbe06e2db6270fc7310 (patch)
treedb5900dbeb05babf22b5797b47fe99808fdea01b
parentd71019b54bff4b8f52903e354bf3b8cb2b4dfa75 (diff)
parente5b0863c2064f2d40de9de4862317f9db4ccffff (diff)
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>
-rw-r--r--tools/bpf/bpftool/net.c41
-rw-r--r--tools/bpf/bpftool/netlink_dumper.c32
-rw-r--r--tools/lib/bpf/bpf.h6
-rw-r--r--tools/lib/bpf/btf.h6
-rw-r--r--tools/lib/bpf/libbpf.c22
-rw-r--r--tools/lib/bpf/libbpf.h31
-rw-r--r--tools/lib/bpf/netlink.c48
-rw-r--r--tools/lib/bpf/nlattr.c64
-rw-r--r--tools/lib/bpf/nlattr.h65
-rw-r--r--tools/lib/bpf/str_error.c2
-rw-r--r--tools/lib/bpf/str_error.h8
11 files changed, 171 insertions, 154 deletions
diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
index ed205ee57655..d441bb7035ca 100644
--- a/tools/bpf/bpftool/net.c
+++ b/tools/bpf/bpftool/net.c
@@ -69,7 +69,9 @@ static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
69 snprintf(netinfo->devices[netinfo->used_len].devname, 69 snprintf(netinfo->devices[netinfo->used_len].devname,
70 sizeof(netinfo->devices[netinfo->used_len].devname), 70 sizeof(netinfo->devices[netinfo->used_len].devname),
71 "%s", 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 netinfo->used_len++; 75 netinfo->used_len++;
74 76
75 return do_xdp_dump(ifinfo, tb); 77 return do_xdp_dump(ifinfo, tb);
@@ -83,7 +85,7 @@ static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
83 if (tcinfo->is_qdisc) { 85 if (tcinfo->is_qdisc) {
84 /* skip clsact qdisc */ 86 /* skip clsact qdisc */
85 if (tb[TCA_KIND] && 87 if (tb[TCA_KIND] &&
86 strcmp(nla_data(tb[TCA_KIND]), "clsact") == 0) 88 strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0)
87 return 0; 89 return 0;
88 if (info->tcm_handle == 0) 90 if (info->tcm_handle == 0)
89 return 0; 91 return 0;
@@ -101,7 +103,9 @@ static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
101 snprintf(tcinfo->handle_array[tcinfo->used_len].kind, 103 snprintf(tcinfo->handle_array[tcinfo->used_len].kind,
102 sizeof(tcinfo->handle_array[tcinfo->used_len].kind), 104 sizeof(tcinfo->handle_array[tcinfo->used_len].kind),
103 "%s", 105 "%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");
105 tcinfo->used_len++; 109 tcinfo->used_len++;
106 110
107 return 0; 111 return 0;
@@ -127,14 +131,14 @@ static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
127 tcinfo.array_len = 0; 131 tcinfo.array_len = 0;
128 132
129 tcinfo.is_qdisc = false; 133 tcinfo.is_qdisc = false;
130 ret = nl_get_class(sock, nl_pid, dev->ifindex, dump_class_qdisc_nlmsg, 134 ret = libbpf_nl_get_class(sock, nl_pid, dev->ifindex,
131 &tcinfo); 135 dump_class_qdisc_nlmsg, &tcinfo);
132 if (ret) 136 if (ret)
133 goto out; 137 goto out;
134 138
135 tcinfo.is_qdisc = true; 139 tcinfo.is_qdisc = true;
136 ret = nl_get_qdisc(sock, nl_pid, dev->ifindex, dump_class_qdisc_nlmsg, 140 ret = libbpf_nl_get_qdisc(sock, nl_pid, dev->ifindex,
137 &tcinfo); 141 dump_class_qdisc_nlmsg, &tcinfo);
138 if (ret) 142 if (ret)
139 goto out; 143 goto out;
140 144
@@ -142,10 +146,9 @@ static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
142 filter_info.ifindex = dev->ifindex; 146 filter_info.ifindex = dev->ifindex;
143 for (i = 0; i < tcinfo.used_len; i++) { 147 for (i = 0; i < tcinfo.used_len; i++) {
144 filter_info.kind = tcinfo.handle_array[i].kind; 148 filter_info.kind = tcinfo.handle_array[i].kind;
145 ret = nl_get_filter(sock, nl_pid, dev->ifindex, 149 ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex,
146 tcinfo.handle_array[i].handle, 150 tcinfo.handle_array[i].handle,
147 dump_filter_nlmsg, 151 dump_filter_nlmsg, &filter_info);
148 &filter_info);
149 if (ret) 152 if (ret)
150 goto out; 153 goto out;
151 } 154 }
@@ -153,22 +156,22 @@ static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
153 /* root, ingress and egress handle */ 156 /* root, ingress and egress handle */
154 handle = TC_H_ROOT; 157 handle = TC_H_ROOT;
155 filter_info.kind = "root"; 158 filter_info.kind = "root";
156 ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle, 159 ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
157 dump_filter_nlmsg, &filter_info); 160 dump_filter_nlmsg, &filter_info);
158 if (ret) 161 if (ret)
159 goto out; 162 goto out;
160 163
161 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS); 164 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
162 filter_info.kind = "clsact/ingress"; 165 filter_info.kind = "clsact/ingress";
163 ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle, 166 ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
164 dump_filter_nlmsg, &filter_info); 167 dump_filter_nlmsg, &filter_info);
165 if (ret) 168 if (ret)
166 goto out; 169 goto out;
167 170
168 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS); 171 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
169 filter_info.kind = "clsact/egress"; 172 filter_info.kind = "clsact/egress";
170 ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle, 173 ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
171 dump_filter_nlmsg, &filter_info); 174 dump_filter_nlmsg, &filter_info);
172 if (ret) 175 if (ret)
173 goto out; 176 goto out;
174 177
@@ -196,7 +199,7 @@ static int do_show(int argc, char **argv)
196 usage(); 199 usage();
197 } 200 }
198 201
199 sock = bpf_netlink_open(&nl_pid); 202 sock = libbpf_netlink_open(&nl_pid);
200 if (sock < 0) { 203 if (sock < 0) {
201 fprintf(stderr, "failed to open netlink sock\n"); 204 fprintf(stderr, "failed to open netlink sock\n");
202 return -1; 205 return -1;
@@ -211,7 +214,7 @@ static int do_show(int argc, char **argv)
211 jsonw_start_array(json_wtr); 214 jsonw_start_array(json_wtr);
212 NET_START_OBJECT; 215 NET_START_OBJECT;
213 NET_START_ARRAY("xdp", "%s:\n"); 216 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);
215 NET_END_ARRAY("\n"); 218 NET_END_ARRAY("\n");
216 219
217 if (!ret) { 220 if (!ret) {
diff --git a/tools/bpf/bpftool/netlink_dumper.c b/tools/bpf/bpftool/netlink_dumper.c
index 6f5e9cc6836c..4e9f4531269f 100644
--- a/tools/bpf/bpftool/netlink_dumper.c
+++ b/tools/bpf/bpftool/netlink_dumper.c
@@ -21,7 +21,7 @@ static void xdp_dump_prog_id(struct nlattr **tb, int attr,
21 if (new_json_object) 21 if (new_json_object)
22 NET_START_OBJECT 22 NET_START_OBJECT
23 NET_DUMP_STR("mode", " %s", mode); 23 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]))
25 if (new_json_object) 25 if (new_json_object)
26 NET_END_OBJECT 26 NET_END_OBJECT
27} 27}
@@ -32,13 +32,13 @@ static int do_xdp_dump_one(struct nlattr *attr, unsigned int ifindex,
32 struct nlattr *tb[IFLA_XDP_MAX + 1]; 32 struct nlattr *tb[IFLA_XDP_MAX + 1];
33 unsigned char mode; 33 unsigned char mode;
34 34
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)
36 return -1; 36 return -1;
37 37
38 if (!tb[IFLA_XDP_ATTACHED]) 38 if (!tb[IFLA_XDP_ATTACHED])
39 return 0; 39 return 0;
40 40
41 mode = nla_getattr_u8(tb[IFLA_XDP_ATTACHED]); 41 mode = libbpf_nla_getattr_u8(tb[IFLA_XDP_ATTACHED]);
42 if (mode == XDP_ATTACHED_NONE) 42 if (mode == XDP_ATTACHED_NONE)
43 return 0; 43 return 0;
44 44
@@ -75,14 +75,14 @@ int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb)
75 return 0; 75 return 0;
76 76
77 return do_xdp_dump_one(tb[IFLA_XDP], ifinfo->ifi_index, 77 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]));
79} 79}
80 80
81static int do_bpf_dump_one_act(struct nlattr *attr) 81static int do_bpf_dump_one_act(struct nlattr *attr)
82{ 82{
83 struct nlattr *tb[TCA_ACT_BPF_MAX + 1]; 83 struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
84 84
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)
86 return -LIBBPF_ERRNO__NLPARSE; 86 return -LIBBPF_ERRNO__NLPARSE;
87 87
88 if (!tb[TCA_ACT_BPF_PARMS]) 88 if (!tb[TCA_ACT_BPF_PARMS])
@@ -91,10 +91,10 @@ static int do_bpf_dump_one_act(struct nlattr *attr)
91 NET_START_OBJECT_NESTED2; 91 NET_START_OBJECT_NESTED2;
92 if (tb[TCA_ACT_BPF_NAME]) 92 if (tb[TCA_ACT_BPF_NAME])
93 NET_DUMP_STR("name", "%s", 93 NET_DUMP_STR("name", "%s",
94 nla_getattr_str(tb[TCA_ACT_BPF_NAME])); 94 libbpf_nla_getattr_str(tb[TCA_ACT_BPF_NAME]));
95 if (tb[TCA_ACT_BPF_ID]) 95 if (tb[TCA_ACT_BPF_ID])
96 NET_DUMP_UINT("id", " id %u", 96 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]));
98 NET_END_OBJECT_NESTED; 98 NET_END_OBJECT_NESTED;
99 return 0; 99 return 0;
100} 100}
@@ -106,10 +106,11 @@ static int do_dump_one_act(struct nlattr *attr)
106 if (!attr) 106 if (!attr)
107 return 0; 107 return 0;
108 108
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)
110 return -LIBBPF_ERRNO__NLPARSE; 110 return -LIBBPF_ERRNO__NLPARSE;
111 111
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)
113 return do_bpf_dump_one_act(tb[TCA_ACT_OPTIONS]); 114 return do_bpf_dump_one_act(tb[TCA_ACT_OPTIONS]);
114 115
115 return 0; 116 return 0;
@@ -120,7 +121,7 @@ static int do_bpf_act_dump(struct nlattr *attr)
120 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; 121 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
121 int act, ret; 122 int act, ret;
122 123
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)
124 return -LIBBPF_ERRNO__NLPARSE; 125 return -LIBBPF_ERRNO__NLPARSE;
125 126
126 NET_START_ARRAY("act", " %s ["); 127 NET_START_ARRAY("act", " %s [");
@@ -139,13 +140,15 @@ static int do_bpf_filter_dump(struct nlattr *attr)
139 struct nlattr *tb[TCA_BPF_MAX + 1]; 140 struct nlattr *tb[TCA_BPF_MAX + 1];
140 int ret; 141 int ret;
141 142
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)
143 return -LIBBPF_ERRNO__NLPARSE; 144 return -LIBBPF_ERRNO__NLPARSE;
144 145
145 if (tb[TCA_BPF_NAME]) 146 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]));
147 if (tb[TCA_BPF_ID]) 149 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]));
149 if (tb[TCA_BPF_ACT]) { 152 if (tb[TCA_BPF_ACT]) {
150 ret = do_bpf_act_dump(tb[TCA_BPF_ACT]); 153 ret = do_bpf_act_dump(tb[TCA_BPF_ACT]);
151 if (ret) 154 if (ret)
@@ -160,7 +163,8 @@ int do_filter_dump(struct tcmsg *info, struct nlattr **tb, const char *kind,
160{ 163{
161 int ret = 0; 164 int ret = 0;
162 165
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) {
164 NET_START_OBJECT; 168 NET_START_OBJECT;
165 if (devname[0] != '\0') 169 if (devname[0] != '\0')
166 NET_DUMP_STR("devname", "%s", devname); 170 NET_DUMP_STR("devname", "%s", devname);
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 6f38164b2618..4c78f61b7c71 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -20,8 +20,8 @@
20 * You should have received a copy of the GNU Lesser General Public 20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this program; if not, see <http://www.gnu.org/licenses> 21 * License along with this program; if not, see <http://www.gnu.org/licenses>
22 */ 22 */
23#ifndef __BPF_BPF_H 23#ifndef __LIBBPF_BPF_H
24#define __BPF_BPF_H 24#define __LIBBPF_BPF_H
25 25
26#include <linux/bpf.h> 26#include <linux/bpf.h>
27#include <stdbool.h> 27#include <stdbool.h>
@@ -111,4 +111,4 @@ int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
111int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len, 111int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
112 __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset, 112 __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
113 __u64 *probe_addr); 113 __u64 *probe_addr);
114#endif 114#endif /* __LIBBPF_BPF_H */
diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index 4897e0724d4e..d5d20682eeb6 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -1,8 +1,8 @@
1/* SPDX-License-Identifier: LGPL-2.1 */ 1/* SPDX-License-Identifier: LGPL-2.1 */
2/* Copyright (c) 2018 Facebook */ 2/* Copyright (c) 2018 Facebook */
3 3
4#ifndef __BPF_BTF_H 4#ifndef __LIBBPF_BTF_H
5#define __BPF_BTF_H 5#define __LIBBPF_BTF_H
6 6
7#include <linux/types.h> 7#include <linux/types.h>
8 8
@@ -23,4 +23,4 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id);
23int btf__fd(const struct btf *btf); 23int btf__fd(const struct btf *btf);
24const char *btf__name_by_offset(const struct btf *btf, __u32 offset); 24const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
25 25
26#endif 26#endif /* __LIBBPF_BTF_H */
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 9e68fd9fcfca..85de1ebd4cb0 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -470,7 +470,8 @@ static int bpf_object__elf_init(struct bpf_object *obj)
470 obj->efile.fd = open(obj->path, O_RDONLY); 470 obj->efile.fd = open(obj->path, O_RDONLY);
471 if (obj->efile.fd < 0) { 471 if (obj->efile.fd < 0) {
472 char errmsg[STRERR_BUFSIZE]; 472 char errmsg[STRERR_BUFSIZE];
473 char *cp = str_error(errno, errmsg, sizeof(errmsg)); 473 char *cp = libbpf_strerror_r(errno, errmsg,
474 sizeof(errmsg));
474 475
475 pr_warning("failed to open %s: %s\n", obj->path, cp); 476 pr_warning("failed to open %s: %s\n", obj->path, cp);
476 return -errno; 477 return -errno;
@@ -811,7 +812,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
811 data->d_size, name, idx); 812 data->d_size, name, idx);
812 if (err) { 813 if (err) {
813 char errmsg[STRERR_BUFSIZE]; 814 char errmsg[STRERR_BUFSIZE];
814 char *cp = str_error(-err, errmsg, sizeof(errmsg)); 815 char *cp = libbpf_strerror_r(-err, errmsg,
816 sizeof(errmsg));
815 817
816 pr_warning("failed to alloc program %s (%s): %s", 818 pr_warning("failed to alloc program %s (%s): %s",
817 name, obj->path, cp); 819 name, obj->path, cp);
@@ -1140,7 +1142,7 @@ bpf_object__create_maps(struct bpf_object *obj)
1140 1142
1141 *pfd = bpf_create_map_xattr(&create_attr); 1143 *pfd = bpf_create_map_xattr(&create_attr);
1142 if (*pfd < 0 && create_attr.btf_key_type_id) { 1144 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));
1144 pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n", 1146 pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
1145 map->name, cp, errno); 1147 map->name, cp, errno);
1146 create_attr.btf_fd = 0; 1148 create_attr.btf_fd = 0;
@@ -1155,7 +1157,7 @@ bpf_object__create_maps(struct bpf_object *obj)
1155 size_t j; 1157 size_t j;
1156 1158
1157 err = *pfd; 1159 err = *pfd;
1158 cp = str_error(errno, errmsg, sizeof(errmsg)); 1160 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1159 pr_warning("failed to create map (name: '%s'): %s\n", 1161 pr_warning("failed to create map (name: '%s'): %s\n",
1160 map->name, cp); 1162 map->name, cp);
1161 for (j = 0; j < i; j++) 1163 for (j = 0; j < i; j++)
@@ -1339,7 +1341,7 @@ load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
1339 } 1341 }
1340 1342
1341 ret = -LIBBPF_ERRNO__LOAD; 1343 ret = -LIBBPF_ERRNO__LOAD;
1342 cp = str_error(errno, errmsg, sizeof(errmsg)); 1344 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1343 pr_warning("load bpf program failed: %s\n", cp); 1345 pr_warning("load bpf program failed: %s\n", cp);
1344 1346
1345 if (log_buf && log_buf[0] != '\0') { 1347 if (log_buf && log_buf[0] != '\0') {
@@ -1377,7 +1379,7 @@ out:
1377 1379
1378int 1380int
1379bpf_program__load(struct bpf_program *prog, 1381bpf_program__load(struct bpf_program *prog,
1380 char *license, u32 kern_version) 1382 char *license, __u32 kern_version)
1381{ 1383{
1382 int err = 0, fd, i; 1384 int err = 0, fd, i;
1383 1385
@@ -1655,7 +1657,7 @@ static int check_path(const char *path)
1655 1657
1656 dir = dirname(dname); 1658 dir = dirname(dname);
1657 if (statfs(dir, &st_fs)) { 1659 if (statfs(dir, &st_fs)) {
1658 cp = str_error(errno, errmsg, sizeof(errmsg)); 1660 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1659 pr_warning("failed to statfs %s: %s\n", dir, cp); 1661 pr_warning("failed to statfs %s: %s\n", dir, cp);
1660 err = -errno; 1662 err = -errno;
1661 } 1663 }
@@ -1691,7 +1693,7 @@ int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1691 } 1693 }
1692 1694
1693 if (bpf_obj_pin(prog->instances.fds[instance], path)) { 1695 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));
1695 pr_warning("failed to pin program: %s\n", cp); 1697 pr_warning("failed to pin program: %s\n", cp);
1696 return -errno; 1698 return -errno;
1697 } 1699 }
@@ -1709,7 +1711,7 @@ static int make_dir(const char *path)
1709 err = -errno; 1711 err = -errno;
1710 1712
1711 if (err) { 1713 if (err) {
1712 cp = str_error(-err, errmsg, sizeof(errmsg)); 1714 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
1713 pr_warning("failed to mkdir %s: %s\n", path, cp); 1715 pr_warning("failed to mkdir %s: %s\n", path, cp);
1714 } 1716 }
1715 return err; 1717 return err;
@@ -1771,7 +1773,7 @@ int bpf_map__pin(struct bpf_map *map, const char *path)
1771 } 1773 }
1772 1774
1773 if (bpf_obj_pin(map->fd, path)) { 1775 if (bpf_obj_pin(map->fd, path)) {
1774 cp = str_error(errno, errmsg, sizeof(errmsg)); 1776 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1775 pr_warning("failed to pin map: %s\n", cp); 1777 pr_warning("failed to pin map: %s\n", cp);
1776 return -errno; 1778 return -errno;
1777 } 1779 }
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 2ed24d3f80b3..fbfc2aec0f0d 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -20,8 +20,8 @@
20 * You should have received a copy of the GNU Lesser General Public 20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this program; if not, see <http://www.gnu.org/licenses> 21 * License along with this program; if not, see <http://www.gnu.org/licenses>
22 */ 22 */
23#ifndef __BPF_LIBBPF_H 23#ifndef __LIBBPF_LIBBPF_H
24#define __BPF_LIBBPF_H 24#define __LIBBPF_LIBBPF_H
25 25
26#include <stdio.h> 26#include <stdio.h>
27#include <stdint.h> 27#include <stdint.h>
@@ -129,7 +129,7 @@ void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex);
129const char *bpf_program__title(struct bpf_program *prog, bool needs_copy); 129const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
130 130
131int bpf_program__load(struct bpf_program *prog, char *license, 131int bpf_program__load(struct bpf_program *prog, char *license,
132 u32 kern_version); 132 __u32 kern_version);
133int bpf_program__fd(struct bpf_program *prog); 133int bpf_program__fd(struct bpf_program *prog);
134int bpf_program__pin_instance(struct bpf_program *prog, const char *path, 134int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
135 int instance); 135 int instance);
@@ -304,18 +304,15 @@ int bpf_perf_event_read_simple(void *mem, unsigned long size,
304 void **buf, size_t *buf_len, 304 void **buf, size_t *buf_len,
305 bpf_perf_event_print_t fn, void *priv); 305 bpf_perf_event_print_t fn, void *priv);
306 306
307struct nlmsghdr;
308struct nlattr; 307struct nlattr;
309typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); 308typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
310typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t, 309int libbpf_netlink_open(unsigned int *nl_pid);
311 void *cookie); 310int libbpf_nl_get_link(int sock, unsigned int nl_pid,
312int bpf_netlink_open(unsigned int *nl_pid); 311 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
313int nl_get_link(int sock, unsigned int nl_pid, dump_nlmsg_t dump_link_nlmsg, 312int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
314 void *cookie); 313 libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
315int nl_get_class(int sock, unsigned int nl_pid, int ifindex, 314int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
316 dump_nlmsg_t dump_class_nlmsg, void *cookie); 315 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
317int nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex, 316int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
318 dump_nlmsg_t dump_qdisc_nlmsg, void *cookie); 317 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
319int nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 318#endif /* __LIBBPF_LIBBPF_H */
320 dump_nlmsg_t dump_filter_nlmsg, void *cookie);
321#endif
diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index fde1d7bf8199..2d2edbbd8ae8 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -18,7 +18,10 @@
18#define SOL_NETLINK 270 18#define SOL_NETLINK 270
19#endif 19#endif
20 20
21int bpf_netlink_open(__u32 *nl_pid) 21typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, libbpf_dump_nlmsg_t,
22 void *cookie);
23
24int libbpf_netlink_open(__u32 *nl_pid)
22{ 25{
23 struct sockaddr_nl sa; 26 struct sockaddr_nl sa;
24 socklen_t addrlen; 27 socklen_t addrlen;
@@ -62,7 +65,7 @@ cleanup:
62} 65}
63 66
64static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq, 67static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq,
65 __dump_nlmsg_t _fn, dump_nlmsg_t fn, 68 __dump_nlmsg_t _fn, libbpf_dump_nlmsg_t fn,
66 void *cookie) 69 void *cookie)
67{ 70{
68 bool multipart = true; 71 bool multipart = true;
@@ -100,7 +103,7 @@ static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq,
100 if (!err->error) 103 if (!err->error)
101 continue; 104 continue;
102 ret = err->error; 105 ret = err->error;
103 nla_dump_errormsg(nh); 106 libbpf_nla_dump_errormsg(nh);
104 goto done; 107 goto done;
105 case NLMSG_DONE: 108 case NLMSG_DONE:
106 return 0; 109 return 0;
@@ -130,7 +133,7 @@ int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
130 } req; 133 } req;
131 __u32 nl_pid; 134 __u32 nl_pid;
132 135
133 sock = bpf_netlink_open(&nl_pid); 136 sock = libbpf_netlink_open(&nl_pid);
134 if (sock < 0) 137 if (sock < 0)
135 return sock; 138 return sock;
136 139
@@ -178,8 +181,8 @@ cleanup:
178 return ret; 181 return ret;
179} 182}
180 183
181static int __dump_link_nlmsg(struct nlmsghdr *nlh, dump_nlmsg_t dump_link_nlmsg, 184static int __dump_link_nlmsg(struct nlmsghdr *nlh,
182 void *cookie) 185 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
183{ 186{
184 struct nlattr *tb[IFLA_MAX + 1], *attr; 187 struct nlattr *tb[IFLA_MAX + 1], *attr;
185 struct ifinfomsg *ifi = NLMSG_DATA(nlh); 188 struct ifinfomsg *ifi = NLMSG_DATA(nlh);
@@ -187,14 +190,14 @@ static int __dump_link_nlmsg(struct nlmsghdr *nlh, dump_nlmsg_t dump_link_nlmsg,
187 190
188 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)); 191 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
189 attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi))); 192 attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
190 if (nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0) 193 if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
191 return -LIBBPF_ERRNO__NLPARSE; 194 return -LIBBPF_ERRNO__NLPARSE;
192 195
193 return dump_link_nlmsg(cookie, ifi, tb); 196 return dump_link_nlmsg(cookie, ifi, tb);
194} 197}
195 198
196int nl_get_link(int sock, unsigned int nl_pid, dump_nlmsg_t dump_link_nlmsg, 199int libbpf_nl_get_link(int sock, unsigned int nl_pid,
197 void *cookie) 200 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
198{ 201{
199 struct { 202 struct {
200 struct nlmsghdr nlh; 203 struct nlmsghdr nlh;
@@ -216,7 +219,8 @@ int nl_get_link(int sock, unsigned int nl_pid, dump_nlmsg_t dump_link_nlmsg,
216} 219}
217 220
218static int __dump_class_nlmsg(struct nlmsghdr *nlh, 221static int __dump_class_nlmsg(struct nlmsghdr *nlh,
219 dump_nlmsg_t dump_class_nlmsg, void *cookie) 222 libbpf_dump_nlmsg_t dump_class_nlmsg,
223 void *cookie)
220{ 224{
221 struct nlattr *tb[TCA_MAX + 1], *attr; 225 struct nlattr *tb[TCA_MAX + 1], *attr;
222 struct tcmsg *t = NLMSG_DATA(nlh); 226 struct tcmsg *t = NLMSG_DATA(nlh);
@@ -224,14 +228,14 @@ static int __dump_class_nlmsg(struct nlmsghdr *nlh,
224 228
225 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 229 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
226 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 230 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
227 if (nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 231 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
228 return -LIBBPF_ERRNO__NLPARSE; 232 return -LIBBPF_ERRNO__NLPARSE;
229 233
230 return dump_class_nlmsg(cookie, t, tb); 234 return dump_class_nlmsg(cookie, t, tb);
231} 235}
232 236
233int nl_get_class(int sock, unsigned int nl_pid, int ifindex, 237int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
234 dump_nlmsg_t dump_class_nlmsg, void *cookie) 238 libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie)
235{ 239{
236 struct { 240 struct {
237 struct nlmsghdr nlh; 241 struct nlmsghdr nlh;
@@ -254,7 +258,8 @@ int nl_get_class(int sock, unsigned int nl_pid, int ifindex,
254} 258}
255 259
256static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh, 260static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
257 dump_nlmsg_t dump_qdisc_nlmsg, void *cookie) 261 libbpf_dump_nlmsg_t dump_qdisc_nlmsg,
262 void *cookie)
258{ 263{
259 struct nlattr *tb[TCA_MAX + 1], *attr; 264 struct nlattr *tb[TCA_MAX + 1], *attr;
260 struct tcmsg *t = NLMSG_DATA(nlh); 265 struct tcmsg *t = NLMSG_DATA(nlh);
@@ -262,14 +267,14 @@ static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
262 267
263 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 268 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
264 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 269 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
265 if (nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 270 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
266 return -LIBBPF_ERRNO__NLPARSE; 271 return -LIBBPF_ERRNO__NLPARSE;
267 272
268 return dump_qdisc_nlmsg(cookie, t, tb); 273 return dump_qdisc_nlmsg(cookie, t, tb);
269} 274}
270 275
271int nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex, 276int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
272 dump_nlmsg_t dump_qdisc_nlmsg, void *cookie) 277 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
273{ 278{
274 struct { 279 struct {
275 struct nlmsghdr nlh; 280 struct nlmsghdr nlh;
@@ -292,7 +297,8 @@ int nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
292} 297}
293 298
294static int __dump_filter_nlmsg(struct nlmsghdr *nlh, 299static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
295 dump_nlmsg_t dump_filter_nlmsg, void *cookie) 300 libbpf_dump_nlmsg_t dump_filter_nlmsg,
301 void *cookie)
296{ 302{
297 struct nlattr *tb[TCA_MAX + 1], *attr; 303 struct nlattr *tb[TCA_MAX + 1], *attr;
298 struct tcmsg *t = NLMSG_DATA(nlh); 304 struct tcmsg *t = NLMSG_DATA(nlh);
@@ -300,14 +306,14 @@ static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
300 306
301 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 307 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
302 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 308 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
303 if (nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 309 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
304 return -LIBBPF_ERRNO__NLPARSE; 310 return -LIBBPF_ERRNO__NLPARSE;
305 311
306 return dump_filter_nlmsg(cookie, t, tb); 312 return dump_filter_nlmsg(cookie, t, tb);
307} 313}
308 314
309int nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 315int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
310 dump_nlmsg_t dump_filter_nlmsg, void *cookie) 316 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie)
311{ 317{
312 struct { 318 struct {
313 struct nlmsghdr nlh; 319 struct nlmsghdr nlh;
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..7198584a3040 100644
--- a/tools/lib/bpf/nlattr.h
+++ b/tools/lib/bpf/nlattr.h
@@ -11,8 +11,8 @@
11 * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch> 11 * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
12 */ 12 */
13 13
14#ifndef __NLATTR_H 14#ifndef __LIBBPF_NLATTR_H
15#define __NLATTR_H 15#define __LIBBPF_NLATTR_H
16 16
17#include <stdint.h> 17#include <stdint.h>
18#include <linux/netlink.h> 18#include <linux/netlink.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 /* __LIBBPF_NLATTR_H */
diff --git a/tools/lib/bpf/str_error.c b/tools/lib/bpf/str_error.c
index b8798114a357..3d211b642cb5 100644
--- a/tools/lib/bpf/str_error.c
+++ b/tools/lib/bpf/str_error.c
@@ -9,7 +9,7 @@
9 * libc, while checking strerror_r() return to avoid having to check this in 9 * libc, while checking strerror_r() return to avoid having to check this in
10 * all places calling it. 10 * all places calling it.
11 */ 11 */
12char *str_error(int err, char *dst, int len) 12char *libbpf_strerror_r(int err, char *dst, int len)
13{ 13{
14 int ret = strerror_r(err, dst, len); 14 int ret = strerror_r(err, dst, len);
15 if (ret) 15 if (ret)
diff --git a/tools/lib/bpf/str_error.h b/tools/lib/bpf/str_error.h
index 355b1db571d1..b9157f5eebde 100644
--- a/tools/lib/bpf/str_error.h
+++ b/tools/lib/bpf/str_error.h
@@ -1,6 +1,6 @@
1// SPDX-License-Identifier: LGPL-2.1 1// SPDX-License-Identifier: LGPL-2.1
2#ifndef BPF_STR_ERROR 2#ifndef __LIBBPF_STR_ERROR_H
3#define BPF_STR_ERROR 3#define __LIBBPF_STR_ERROR_H
4 4
5char *str_error(int err, char *dst, int len); 5char *libbpf_strerror_r(int err, char *dst, int len);
6#endif // BPF_STR_ERROR 6#endif /* __LIBBPF_STR_ERROR_H */