Skip to content

Commit 7084566

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
tools/bpftool: Remove libbpf_internal.h usage in bpftool
Most netlink-related functions were unique to bpftool usage, so I moved them into net.c. Few functions are still used by both bpftool and libbpf itself internally, so I've copy-pasted them (libbpf_nl_get_link, libbpf_netlink_open). It's a bit of duplication of code, but better separation of libbpf as a library with public API and bpftool, relying on unexposed functions in libbpf. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200819013607.3607269-3-andriin@fb.com
1 parent 029258d commit 7084566

4 files changed

Lines changed: 288 additions & 150 deletions

File tree

‎tools/bpf/bpftool/gen.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
#include <sys/mman.h>
2020
#include <bpf/btf.h>
2121

22-
#include "bpf/libbpf_internal.h"
2322
#include "json_writer.h"
2423
#include "main.h"
2524

26-
2725
#define MAX_OBJ_NAME_LEN 64
2826

2927
static void sanitize_identifier(char *name)

‎tools/bpf/bpftool/net.c‎

Lines changed: 282 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,27 @@
66
#include <fcntl.h>
77
#include <stdlib.h>
88
#include <string.h>
9+
#include <time.h>
910
#include <unistd.h>
1011
#include <bpf/bpf.h>
1112
#include <bpf/libbpf.h>
1213
#include <net/if.h>
1314
#include <linux/if.h>
1415
#include <linux/rtnetlink.h>
16+
#include <linux/socket.h>
1517
#include <linux/tc_act/tc_bpf.h>
1618
#include <sys/socket.h>
1719
#include <sys/stat.h>
1820
#include <sys/types.h>
1921

2022
#include "bpf/nlattr.h"
21-
#include "bpf/libbpf_internal.h"
2223
#include "main.h"
2324
#include "netlink_dumper.h"
2425

26+
#ifndef SOL_NETLINK
27+
#define SOL_NETLINK 270
28+
#endif
29+
2530
struct ip_devname_ifindex {
2631
char devname[64];
2732
int ifindex;
@@ -85,6 +90,266 @@ static enum net_attach_type parse_attach_type(const char *str)
8590
return net_attach_type_size;
8691
}
8792

93+
typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
94+
95+
typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t, void *cookie);
96+
97+
static int netlink_open(__u32 *nl_pid)
98+
{
99+
struct sockaddr_nl sa;
100+
socklen_t addrlen;
101+
int one = 1, ret;
102+
int sock;
103+
104+
memset(&sa, 0, sizeof(sa));
105+
sa.nl_family = AF_NETLINK;
106+
107+
sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
108+
if (sock < 0)
109+
return -errno;
110+
111+
if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
112+
&one, sizeof(one)) < 0) {
113+
p_err("Netlink error reporting not supported");
114+
}
115+
116+
if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
117+
ret = -errno;
118+
goto cleanup;
119+
}
120+
121+
addrlen = sizeof(sa);
122+
if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
123+
ret = -errno;
124+
goto cleanup;
125+
}
126+
127+
if (addrlen != sizeof(sa)) {
128+
ret = -LIBBPF_ERRNO__INTERNAL;
129+
goto cleanup;
130+
}
131+
132+
*nl_pid = sa.nl_pid;
133+
return sock;
134+
135+
cleanup:
136+
close(sock);
137+
return ret;
138+
}
139+
140+
static int netlink_recv(int sock, __u32 nl_pid, __u32 seq,
141+
__dump_nlmsg_t _fn, dump_nlmsg_t fn,
142+
void *cookie)
143+
{
144+
bool multipart = true;
145+
struct nlmsgerr *err;
146+
struct nlmsghdr *nh;
147+
char buf[4096];
148+
int len, ret;
149+
150+
while (multipart) {
151+
multipart = false;
152+
len = recv(sock, buf, sizeof(buf), 0);
153+
if (len < 0) {
154+
ret = -errno;
155+
goto done;
156+
}
157+
158+
if (len == 0)
159+
break;
160+
161+
for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
162+
nh = NLMSG_NEXT(nh, len)) {
163+
if (nh->nlmsg_pid != nl_pid) {
164+
ret = -LIBBPF_ERRNO__WRNGPID;
165+
goto done;
166+
}
167+
if (nh->nlmsg_seq != seq) {
168+
ret = -LIBBPF_ERRNO__INVSEQ;
169+
goto done;
170+
}
171+
if (nh->nlmsg_flags & NLM_F_MULTI)
172+
multipart = true;
173+
switch (nh->nlmsg_type) {
174+
case NLMSG_ERROR:
175+
err = (struct nlmsgerr *)NLMSG_DATA(nh);
176+
if (!err->error)
177+
continue;
178+
ret = err->error;
179+
libbpf_nla_dump_errormsg(nh);
180+
goto done;
181+
case NLMSG_DONE:
182+
return 0;
183+
default:
184+
break;
185+
}
186+
if (_fn) {
187+
ret = _fn(nh, fn, cookie);
188+
if (ret)
189+
return ret;
190+
}
191+
}
192+
}
193+
ret = 0;
194+
done:
195+
return ret;
196+
}
197+
198+
static int __dump_class_nlmsg(struct nlmsghdr *nlh,
199+
dump_nlmsg_t dump_class_nlmsg,
200+
void *cookie)
201+
{
202+
struct nlattr *tb[TCA_MAX + 1], *attr;
203+
struct tcmsg *t = NLMSG_DATA(nlh);
204+
int len;
205+
206+
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
207+
attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
208+
if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
209+
return -LIBBPF_ERRNO__NLPARSE;
210+
211+
return dump_class_nlmsg(cookie, t, tb);
212+
}
213+
214+
static int netlink_get_class(int sock, unsigned int nl_pid, int ifindex,
215+
dump_nlmsg_t dump_class_nlmsg, void *cookie)
216+
{
217+
struct {
218+
struct nlmsghdr nlh;
219+
struct tcmsg t;
220+
} req = {
221+
.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
222+
.nlh.nlmsg_type = RTM_GETTCLASS,
223+
.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
224+
.t.tcm_family = AF_UNSPEC,
225+
.t.tcm_ifindex = ifindex,
226+
};
227+
int seq = time(NULL);
228+
229+
req.nlh.nlmsg_seq = seq;
230+
if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
231+
return -errno;
232+
233+
return netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg,
234+
dump_class_nlmsg, cookie);
235+
}
236+
237+
static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
238+
dump_nlmsg_t dump_qdisc_nlmsg,
239+
void *cookie)
240+
{
241+
struct nlattr *tb[TCA_MAX + 1], *attr;
242+
struct tcmsg *t = NLMSG_DATA(nlh);
243+
int len;
244+
245+
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
246+
attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
247+
if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
248+
return -LIBBPF_ERRNO__NLPARSE;
249+
250+
return dump_qdisc_nlmsg(cookie, t, tb);
251+
}
252+
253+
static int netlink_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
254+
dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
255+
{
256+
struct {
257+
struct nlmsghdr nlh;
258+
struct tcmsg t;
259+
} req = {
260+
.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
261+
.nlh.nlmsg_type = RTM_GETQDISC,
262+
.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
263+
.t.tcm_family = AF_UNSPEC,
264+
.t.tcm_ifindex = ifindex,
265+
};
266+
int seq = time(NULL);
267+
268+
req.nlh.nlmsg_seq = seq;
269+
if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
270+
return -errno;
271+
272+
return netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg,
273+
dump_qdisc_nlmsg, cookie);
274+
}
275+
276+
static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
277+
dump_nlmsg_t dump_filter_nlmsg,
278+
void *cookie)
279+
{
280+
struct nlattr *tb[TCA_MAX + 1], *attr;
281+
struct tcmsg *t = NLMSG_DATA(nlh);
282+
int len;
283+
284+
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
285+
attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
286+
if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
287+
return -LIBBPF_ERRNO__NLPARSE;
288+
289+
return dump_filter_nlmsg(cookie, t, tb);
290+
}
291+
292+
static int netlink_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
293+
dump_nlmsg_t dump_filter_nlmsg, void *cookie)
294+
{
295+
struct {
296+
struct nlmsghdr nlh;
297+
struct tcmsg t;
298+
} req = {
299+
.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
300+
.nlh.nlmsg_type = RTM_GETTFILTER,
301+
.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
302+
.t.tcm_family = AF_UNSPEC,
303+
.t.tcm_ifindex = ifindex,
304+
.t.tcm_parent = handle,
305+
};
306+
int seq = time(NULL);
307+
308+
req.nlh.nlmsg_seq = seq;
309+
if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
310+
return -errno;
311+
312+
return netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg,
313+
dump_filter_nlmsg, cookie);
314+
}
315+
316+
static int __dump_link_nlmsg(struct nlmsghdr *nlh,
317+
dump_nlmsg_t dump_link_nlmsg, void *cookie)
318+
{
319+
struct nlattr *tb[IFLA_MAX + 1], *attr;
320+
struct ifinfomsg *ifi = NLMSG_DATA(nlh);
321+
int len;
322+
323+
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
324+
attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
325+
if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
326+
return -LIBBPF_ERRNO__NLPARSE;
327+
328+
return dump_link_nlmsg(cookie, ifi, tb);
329+
}
330+
331+
static int netlink_get_link(int sock, unsigned int nl_pid,
332+
dump_nlmsg_t dump_link_nlmsg, void *cookie)
333+
{
334+
struct {
335+
struct nlmsghdr nlh;
336+
struct ifinfomsg ifm;
337+
} req = {
338+
.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
339+
.nlh.nlmsg_type = RTM_GETLINK,
340+
.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
341+
.ifm.ifi_family = AF_PACKET,
342+
};
343+
int seq = time(NULL);
344+
345+
req.nlh.nlmsg_seq = seq;
346+
if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
347+
return -errno;
348+
349+
return netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg,
350+
dump_link_nlmsg, cookie);
351+
}
352+
88353
static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
89354
{
90355
struct bpf_netdev_t *netinfo = cookie;
@@ -168,47 +433,47 @@ static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
168433
tcinfo.array_len = 0;
169434

170435
tcinfo.is_qdisc = false;
171-
ret = libbpf_nl_get_class(sock, nl_pid, dev->ifindex,
172-
dump_class_qdisc_nlmsg, &tcinfo);
436+
ret = netlink_get_class(sock, nl_pid, dev->ifindex,
437+
dump_class_qdisc_nlmsg, &tcinfo);
173438
if (ret)
174439
goto out;
175440

176441
tcinfo.is_qdisc = true;
177-
ret = libbpf_nl_get_qdisc(sock, nl_pid, dev->ifindex,
178-
dump_class_qdisc_nlmsg, &tcinfo);
442+
ret = netlink_get_qdisc(sock, nl_pid, dev->ifindex,
443+
dump_class_qdisc_nlmsg, &tcinfo);
179444
if (ret)
180445
goto out;
181446

182447
filter_info.devname = dev->devname;
183448
filter_info.ifindex = dev->ifindex;
184449
for (i = 0; i < tcinfo.used_len; i++) {
185450
filter_info.kind = tcinfo.handle_array[i].kind;
186-
ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex,
187-
tcinfo.handle_array[i].handle,
188-
dump_filter_nlmsg, &filter_info);
451+
ret = netlink_get_filter(sock, nl_pid, dev->ifindex,
452+
tcinfo.handle_array[i].handle,
453+
dump_filter_nlmsg, &filter_info);
189454
if (ret)
190455
goto out;
191456
}
192457

193458
/* root, ingress and egress handle */
194459
handle = TC_H_ROOT;
195460
filter_info.kind = "root";
196-
ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
197-
dump_filter_nlmsg, &filter_info);
461+
ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
462+
dump_filter_nlmsg, &filter_info);
198463
if (ret)
199464
goto out;
200465

201466
handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
202467
filter_info.kind = "clsact/ingress";
203-
ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
204-
dump_filter_nlmsg, &filter_info);
468+
ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
469+
dump_filter_nlmsg, &filter_info);
205470
if (ret)
206471
goto out;
207472

208473
handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
209474
filter_info.kind = "clsact/egress";
210-
ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
211-
dump_filter_nlmsg, &filter_info);
475+
ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
476+
dump_filter_nlmsg, &filter_info);
212477
if (ret)
213478
goto out;
214479

@@ -386,7 +651,7 @@ static int do_show(int argc, char **argv)
386651
struct bpf_attach_info attach_info = {};
387652
int i, sock, ret, filter_idx = -1;
388653
struct bpf_netdev_t dev_array;
389-
unsigned int nl_pid;
654+
unsigned int nl_pid = 0;
390655
char err_buf[256];
391656

392657
if (argc == 2) {
@@ -401,7 +666,7 @@ static int do_show(int argc, char **argv)
401666
if (ret)
402667
return -1;
403668

404-
sock = libbpf_netlink_open(&nl_pid);
669+
sock = netlink_open(&nl_pid);
405670
if (sock < 0) {
406671
fprintf(stderr, "failed to open netlink sock\n");
407672
return -1;
@@ -416,7 +681,7 @@ static int do_show(int argc, char **argv)
416681
jsonw_start_array(json_wtr);
417682
NET_START_OBJECT;
418683
NET_START_ARRAY("xdp", "%s:\n");
419-
ret = libbpf_nl_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
684+
ret = netlink_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
420685
NET_END_ARRAY("\n");
421686

422687
if (!ret) {

0 commit comments

Comments
 (0)