~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/bpf/bpftool/net.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /tools/bpf/bpftool/net.c (Version linux-6.12-rc7) and /tools/bpf/bpftool/net.c (Version linux-5.6.19)


  1 // SPDX-License-Identifier: (GPL-2.0-only OR B      1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2 // Copyright (C) 2018 Facebook                      2 // Copyright (C) 2018 Facebook
  3                                                     3 
  4 #ifndef _GNU_SOURCE                            << 
  5 #define _GNU_SOURCE                                 4 #define _GNU_SOURCE
  6 #endif                                         << 
  7 #include <errno.h>                                  5 #include <errno.h>
  8 #include <fcntl.h>                                  6 #include <fcntl.h>
  9 #include <stdlib.h>                                 7 #include <stdlib.h>
 10 #include <string.h>                                 8 #include <string.h>
 11 #include <time.h>                              << 
 12 #include <unistd.h>                                 9 #include <unistd.h>
 13 #include <bpf/bpf.h>                               10 #include <bpf/bpf.h>
 14 #include <bpf/libbpf.h>                            11 #include <bpf/libbpf.h>
 15 #include <net/if.h>                                12 #include <net/if.h>
                                                   >>  13 #include <linux/if.h>
 16 #include <linux/rtnetlink.h>                       14 #include <linux/rtnetlink.h>
 17 #include <linux/socket.h>                      << 
 18 #include <linux/tc_act/tc_bpf.h>                   15 #include <linux/tc_act/tc_bpf.h>
 19 #include <sys/socket.h>                            16 #include <sys/socket.h>
 20 #include <sys/stat.h>                              17 #include <sys/stat.h>
 21 #include <sys/types.h>                             18 #include <sys/types.h>
 22                                                    19 
 23 #include "bpf/nlattr.h"                            20 #include "bpf/nlattr.h"
                                                   >>  21 #include "bpf/libbpf_internal.h"
 24 #include "main.h"                                  22 #include "main.h"
 25 #include "netlink_dumper.h"                        23 #include "netlink_dumper.h"
 26                                                    24 
 27 #ifndef SOL_NETLINK                            << 
 28 #define SOL_NETLINK 270                        << 
 29 #endif                                         << 
 30                                                << 
 31 struct ip_devname_ifindex {                        25 struct ip_devname_ifindex {
 32         char    devname[64];                       26         char    devname[64];
 33         int     ifindex;                           27         int     ifindex;
 34 };                                                 28 };
 35                                                    29 
 36 struct bpf_netdev_t {                              30 struct bpf_netdev_t {
 37         struct ip_devname_ifindex *devices;        31         struct ip_devname_ifindex *devices;
 38         int     used_len;                          32         int     used_len;
 39         int     array_len;                         33         int     array_len;
 40         int     filter_idx;                        34         int     filter_idx;
 41 };                                                 35 };
 42                                                    36 
 43 struct tc_kind_handle {                            37 struct tc_kind_handle {
 44         char    kind[64];                          38         char    kind[64];
 45         int     handle;                            39         int     handle;
 46 };                                                 40 };
 47                                                    41 
 48 struct bpf_tcinfo_t {                              42 struct bpf_tcinfo_t {
 49         struct tc_kind_handle   *handle_array;     43         struct tc_kind_handle   *handle_array;
 50         int                     used_len;          44         int                     used_len;
 51         int                     array_len;         45         int                     array_len;
 52         bool                    is_qdisc;          46         bool                    is_qdisc;
 53 };                                                 47 };
 54                                                    48 
 55 struct bpf_filter_t {                              49 struct bpf_filter_t {
 56         const char      *kind;                     50         const char      *kind;
 57         const char      *devname;                  51         const char      *devname;
 58         int             ifindex;                   52         int             ifindex;
 59 };                                                 53 };
 60                                                    54 
 61 struct bpf_attach_info {                           55 struct bpf_attach_info {
 62         __u32 flow_dissector_id;                   56         __u32 flow_dissector_id;
 63 };                                                 57 };
 64                                                    58 
 65 enum net_attach_type {                             59 enum net_attach_type {
 66         NET_ATTACH_TYPE_XDP,                       60         NET_ATTACH_TYPE_XDP,
 67         NET_ATTACH_TYPE_XDP_GENERIC,               61         NET_ATTACH_TYPE_XDP_GENERIC,
 68         NET_ATTACH_TYPE_XDP_DRIVER,                62         NET_ATTACH_TYPE_XDP_DRIVER,
 69         NET_ATTACH_TYPE_XDP_OFFLOAD,               63         NET_ATTACH_TYPE_XDP_OFFLOAD,
 70         NET_ATTACH_TYPE_TCX_INGRESS,           << 
 71         NET_ATTACH_TYPE_TCX_EGRESS,            << 
 72 };                                                 64 };
 73                                                    65 
 74 static const char * const attach_type_strings[     66 static const char * const attach_type_strings[] = {
 75         [NET_ATTACH_TYPE_XDP]           = "xdp     67         [NET_ATTACH_TYPE_XDP]           = "xdp",
 76         [NET_ATTACH_TYPE_XDP_GENERIC]   = "xdp     68         [NET_ATTACH_TYPE_XDP_GENERIC]   = "xdpgeneric",
 77         [NET_ATTACH_TYPE_XDP_DRIVER]    = "xdp     69         [NET_ATTACH_TYPE_XDP_DRIVER]    = "xdpdrv",
 78         [NET_ATTACH_TYPE_XDP_OFFLOAD]   = "xdp     70         [NET_ATTACH_TYPE_XDP_OFFLOAD]   = "xdpoffload",
 79         [NET_ATTACH_TYPE_TCX_INGRESS]   = "tcx << 
 80         [NET_ATTACH_TYPE_TCX_EGRESS]    = "tcx << 
 81 };                                             << 
 82                                                << 
 83 static const char * const attach_loc_strings[] << 
 84         [BPF_TCX_INGRESS]               = "tcx << 
 85         [BPF_TCX_EGRESS]                = "tcx << 
 86         [BPF_NETKIT_PRIMARY]            = "net << 
 87         [BPF_NETKIT_PEER]               = "net << 
 88 };                                                 71 };
 89                                                    72 
 90 const size_t net_attach_type_size = ARRAY_SIZE     73 const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings);
 91                                                    74 
 92 static enum net_attach_type parse_attach_type(     75 static enum net_attach_type parse_attach_type(const char *str)
 93 {                                                  76 {
 94         enum net_attach_type type;                 77         enum net_attach_type type;
 95                                                    78 
 96         for (type = 0; type < net_attach_type_     79         for (type = 0; type < net_attach_type_size; type++) {
 97                 if (attach_type_strings[type]      80                 if (attach_type_strings[type] &&
 98                     is_prefix(str, attach_type     81                     is_prefix(str, attach_type_strings[type]))
 99                         return type;               82                         return type;
100         }                                          83         }
101                                                    84 
102         return net_attach_type_size;               85         return net_attach_type_size;
103 }                                                  86 }
104                                                    87 
105 typedef int (*dump_nlmsg_t)(void *cookie, void << 
106                                                << 
107 typedef int (*__dump_nlmsg_t)(struct nlmsghdr  << 
108                                                << 
109 static int netlink_open(__u32 *nl_pid)         << 
110 {                                              << 
111         struct sockaddr_nl sa;                 << 
112         socklen_t addrlen;                     << 
113         int one = 1, ret;                      << 
114         int sock;                              << 
115                                                << 
116         memset(&sa, 0, sizeof(sa));            << 
117         sa.nl_family = AF_NETLINK;             << 
118                                                << 
119         sock = socket(AF_NETLINK, SOCK_RAW, NE << 
120         if (sock < 0)                          << 
121                 return -errno;                 << 
122                                                << 
123         if (setsockopt(sock, SOL_NETLINK, NETL << 
124                        &one, sizeof(one)) < 0) << 
125                 p_err("Netlink error reporting << 
126         }                                      << 
127                                                << 
128         if (bind(sock, (struct sockaddr *)&sa, << 
129                 ret = -errno;                  << 
130                 goto cleanup;                  << 
131         }                                      << 
132                                                << 
133         addrlen = sizeof(sa);                  << 
134         if (getsockname(sock, (struct sockaddr << 
135                 ret = -errno;                  << 
136                 goto cleanup;                  << 
137         }                                      << 
138                                                << 
139         if (addrlen != sizeof(sa)) {           << 
140                 ret = -LIBBPF_ERRNO__INTERNAL; << 
141                 goto cleanup;                  << 
142         }                                      << 
143                                                << 
144         *nl_pid = sa.nl_pid;                   << 
145         return sock;                           << 
146                                                << 
147 cleanup:                                       << 
148         close(sock);                           << 
149         return ret;                            << 
150 }                                              << 
151                                                << 
152 static int netlink_recv(int sock, __u32 nl_pid << 
153                             __dump_nlmsg_t _fn << 
154                             void *cookie)      << 
155 {                                              << 
156         bool multipart = true;                 << 
157         struct nlmsgerr *err;                  << 
158         struct nlmsghdr *nh;                   << 
159         char buf[4096];                        << 
160         int len, ret;                          << 
161                                                << 
162         while (multipart) {                    << 
163                 multipart = false;             << 
164                 len = recv(sock, buf, sizeof(b << 
165                 if (len < 0) {                 << 
166                         ret = -errno;          << 
167                         goto done;             << 
168                 }                              << 
169                                                << 
170                 if (len == 0)                  << 
171                         break;                 << 
172                                                << 
173                 for (nh = (struct nlmsghdr *)b << 
174                      nh = NLMSG_NEXT(nh, len)) << 
175                         if (nh->nlmsg_pid != n << 
176                                 ret = -LIBBPF_ << 
177                                 goto done;     << 
178                         }                      << 
179                         if (nh->nlmsg_seq != s << 
180                                 ret = -LIBBPF_ << 
181                                 goto done;     << 
182                         }                      << 
183                         if (nh->nlmsg_flags &  << 
184                                 multipart = tr << 
185                         switch (nh->nlmsg_type << 
186                         case NLMSG_ERROR:      << 
187                                 err = (struct  << 
188                                 if (!err->erro << 
189                                         contin << 
190                                 ret = err->err << 
191                                 libbpf_nla_dum << 
192                                 goto done;     << 
193                         case NLMSG_DONE:       << 
194                                 return 0;      << 
195                         default:               << 
196                                 break;         << 
197                         }                      << 
198                         if (_fn) {             << 
199                                 ret = _fn(nh,  << 
200                                 if (ret)       << 
201                                         return << 
202                         }                      << 
203                 }                              << 
204         }                                      << 
205         ret = 0;                               << 
206 done:                                          << 
207         return ret;                            << 
208 }                                              << 
209                                                << 
210 static int __dump_class_nlmsg(struct nlmsghdr  << 
211                               dump_nlmsg_t dum << 
212                               void *cookie)    << 
213 {                                              << 
214         struct nlattr *tb[TCA_MAX + 1], *attr; << 
215         struct tcmsg *t = NLMSG_DATA(nlh);     << 
216         int len;                               << 
217                                                << 
218         len = nlh->nlmsg_len - NLMSG_LENGTH(si << 
219         attr = (struct nlattr *) ((void *) t + << 
220         if (libbpf_nla_parse(tb, TCA_MAX, attr << 
221                 return -LIBBPF_ERRNO__NLPARSE; << 
222                                                << 
223         return dump_class_nlmsg(cookie, t, tb) << 
224 }                                              << 
225                                                << 
226 static int netlink_get_class(int sock, unsigne << 
227                              dump_nlmsg_t dump << 
228 {                                              << 
229         struct {                               << 
230                 struct nlmsghdr nlh;           << 
231                 struct tcmsg t;                << 
232         } req = {                              << 
233                 .nlh.nlmsg_len = NLMSG_LENGTH( << 
234                 .nlh.nlmsg_type = RTM_GETTCLAS << 
235                 .nlh.nlmsg_flags = NLM_F_DUMP  << 
236                 .t.tcm_family = AF_UNSPEC,     << 
237                 .t.tcm_ifindex = ifindex,      << 
238         };                                     << 
239         int seq = time(NULL);                  << 
240                                                << 
241         req.nlh.nlmsg_seq = seq;               << 
242         if (send(sock, &req, req.nlh.nlmsg_len << 
243                 return -errno;                 << 
244                                                << 
245         return netlink_recv(sock, nl_pid, seq, << 
246                             dump_class_nlmsg,  << 
247 }                                              << 
248                                                << 
249 static int __dump_qdisc_nlmsg(struct nlmsghdr  << 
250                               dump_nlmsg_t dum << 
251                               void *cookie)    << 
252 {                                              << 
253         struct nlattr *tb[TCA_MAX + 1], *attr; << 
254         struct tcmsg *t = NLMSG_DATA(nlh);     << 
255         int len;                               << 
256                                                << 
257         len = nlh->nlmsg_len - NLMSG_LENGTH(si << 
258         attr = (struct nlattr *) ((void *) t + << 
259         if (libbpf_nla_parse(tb, TCA_MAX, attr << 
260                 return -LIBBPF_ERRNO__NLPARSE; << 
261                                                << 
262         return dump_qdisc_nlmsg(cookie, t, tb) << 
263 }                                              << 
264                                                << 
265 static int netlink_get_qdisc(int sock, unsigne << 
266                              dump_nlmsg_t dump << 
267 {                                              << 
268         struct {                               << 
269                 struct nlmsghdr nlh;           << 
270                 struct tcmsg t;                << 
271         } req = {                              << 
272                 .nlh.nlmsg_len = NLMSG_LENGTH( << 
273                 .nlh.nlmsg_type = RTM_GETQDISC << 
274                 .nlh.nlmsg_flags = NLM_F_DUMP  << 
275                 .t.tcm_family = AF_UNSPEC,     << 
276                 .t.tcm_ifindex = ifindex,      << 
277         };                                     << 
278         int seq = time(NULL);                  << 
279                                                << 
280         req.nlh.nlmsg_seq = seq;               << 
281         if (send(sock, &req, req.nlh.nlmsg_len << 
282                 return -errno;                 << 
283                                                << 
284         return netlink_recv(sock, nl_pid, seq, << 
285                             dump_qdisc_nlmsg,  << 
286 }                                              << 
287                                                << 
288 static int __dump_filter_nlmsg(struct nlmsghdr << 
289                                dump_nlmsg_t du << 
290                                void *cookie)   << 
291 {                                              << 
292         struct nlattr *tb[TCA_MAX + 1], *attr; << 
293         struct tcmsg *t = NLMSG_DATA(nlh);     << 
294         int len;                               << 
295                                                << 
296         len = nlh->nlmsg_len - NLMSG_LENGTH(si << 
297         attr = (struct nlattr *) ((void *) t + << 
298         if (libbpf_nla_parse(tb, TCA_MAX, attr << 
299                 return -LIBBPF_ERRNO__NLPARSE; << 
300                                                << 
301         return dump_filter_nlmsg(cookie, t, tb << 
302 }                                              << 
303                                                << 
304 static int netlink_get_filter(int sock, unsign << 
305                               dump_nlmsg_t dum << 
306 {                                              << 
307         struct {                               << 
308                 struct nlmsghdr nlh;           << 
309                 struct tcmsg t;                << 
310         } req = {                              << 
311                 .nlh.nlmsg_len = NLMSG_LENGTH( << 
312                 .nlh.nlmsg_type = RTM_GETTFILT << 
313                 .nlh.nlmsg_flags = NLM_F_DUMP  << 
314                 .t.tcm_family = AF_UNSPEC,     << 
315                 .t.tcm_ifindex = ifindex,      << 
316                 .t.tcm_parent = handle,        << 
317         };                                     << 
318         int seq = time(NULL);                  << 
319                                                << 
320         req.nlh.nlmsg_seq = seq;               << 
321         if (send(sock, &req, req.nlh.nlmsg_len << 
322                 return -errno;                 << 
323                                                << 
324         return netlink_recv(sock, nl_pid, seq, << 
325                             dump_filter_nlmsg, << 
326 }                                              << 
327                                                << 
328 static int __dump_link_nlmsg(struct nlmsghdr * << 
329                              dump_nlmsg_t dump << 
330 {                                              << 
331         struct nlattr *tb[IFLA_MAX + 1], *attr << 
332         struct ifinfomsg *ifi = NLMSG_DATA(nlh << 
333         int len;                               << 
334                                                << 
335         len = nlh->nlmsg_len - NLMSG_LENGTH(si << 
336         attr = (struct nlattr *) ((void *) ifi << 
337         if (libbpf_nla_parse(tb, IFLA_MAX, att << 
338                 return -LIBBPF_ERRNO__NLPARSE; << 
339                                                << 
340         return dump_link_nlmsg(cookie, ifi, tb << 
341 }                                              << 
342                                                << 
343 static int netlink_get_link(int sock, unsigned << 
344                             dump_nlmsg_t dump_ << 
345 {                                              << 
346         struct {                               << 
347                 struct nlmsghdr nlh;           << 
348                 struct ifinfomsg ifm;          << 
349         } req = {                              << 
350                 .nlh.nlmsg_len = NLMSG_LENGTH( << 
351                 .nlh.nlmsg_type = RTM_GETLINK, << 
352                 .nlh.nlmsg_flags = NLM_F_DUMP  << 
353                 .ifm.ifi_family = AF_PACKET,   << 
354         };                                     << 
355         int seq = time(NULL);                  << 
356                                                << 
357         req.nlh.nlmsg_seq = seq;               << 
358         if (send(sock, &req, req.nlh.nlmsg_len << 
359                 return -errno;                 << 
360                                                << 
361         return netlink_recv(sock, nl_pid, seq, << 
362                             dump_link_nlmsg, c << 
363 }                                              << 
364                                                << 
365 static int dump_link_nlmsg(void *cookie, void      88 static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
366 {                                                  89 {
367         struct bpf_netdev_t *netinfo = cookie;     90         struct bpf_netdev_t *netinfo = cookie;
368         struct ifinfomsg *ifinfo = msg;            91         struct ifinfomsg *ifinfo = msg;
369                                                    92 
370         if (netinfo->filter_idx > 0 && netinfo     93         if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
371                 return 0;                          94                 return 0;
372                                                    95 
373         if (netinfo->used_len == netinfo->arra     96         if (netinfo->used_len == netinfo->array_len) {
374                 netinfo->devices = realloc(net     97                 netinfo->devices = realloc(netinfo->devices,
375                         (netinfo->array_len +      98                         (netinfo->array_len + 16) *
376                         sizeof(struct ip_devna     99                         sizeof(struct ip_devname_ifindex));
377                 if (!netinfo->devices)            100                 if (!netinfo->devices)
378                         return -ENOMEM;           101                         return -ENOMEM;
379                                                   102 
380                 netinfo->array_len += 16;         103                 netinfo->array_len += 16;
381         }                                         104         }
382         netinfo->devices[netinfo->used_len].if    105         netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
383         snprintf(netinfo->devices[netinfo->use    106         snprintf(netinfo->devices[netinfo->used_len].devname,
384                  sizeof(netinfo->devices[netin    107                  sizeof(netinfo->devices[netinfo->used_len].devname),
385                  "%s",                            108                  "%s",
386                  tb[IFLA_IFNAME]                  109                  tb[IFLA_IFNAME]
387                          ? libbpf_nla_getattr_    110                          ? libbpf_nla_getattr_str(tb[IFLA_IFNAME])
388                          : "");                   111                          : "");
389         netinfo->used_len++;                      112         netinfo->used_len++;
390                                                   113 
391         return do_xdp_dump(ifinfo, tb);           114         return do_xdp_dump(ifinfo, tb);
392 }                                                 115 }
393                                                   116 
394 static int dump_class_qdisc_nlmsg(void *cookie    117 static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
395 {                                                 118 {
396         struct bpf_tcinfo_t *tcinfo = cookie;     119         struct bpf_tcinfo_t *tcinfo = cookie;
397         struct tcmsg *info = msg;                 120         struct tcmsg *info = msg;
398                                                   121 
399         if (tcinfo->is_qdisc) {                   122         if (tcinfo->is_qdisc) {
400                 /* skip clsact qdisc */           123                 /* skip clsact qdisc */
401                 if (tb[TCA_KIND] &&               124                 if (tb[TCA_KIND] &&
402                     strcmp(libbpf_nla_data(tb[    125                     strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0)
403                         return 0;                 126                         return 0;
404                 if (info->tcm_handle == 0)        127                 if (info->tcm_handle == 0)
405                         return 0;                 128                         return 0;
406         }                                         129         }
407                                                   130 
408         if (tcinfo->used_len == tcinfo->array_    131         if (tcinfo->used_len == tcinfo->array_len) {
409                 tcinfo->handle_array = realloc    132                 tcinfo->handle_array = realloc(tcinfo->handle_array,
410                         (tcinfo->array_len + 1    133                         (tcinfo->array_len + 16) * sizeof(struct tc_kind_handle));
411                 if (!tcinfo->handle_array)        134                 if (!tcinfo->handle_array)
412                         return -ENOMEM;           135                         return -ENOMEM;
413                                                   136 
414                 tcinfo->array_len += 16;          137                 tcinfo->array_len += 16;
415         }                                         138         }
416         tcinfo->handle_array[tcinfo->used_len]    139         tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle;
417         snprintf(tcinfo->handle_array[tcinfo->    140         snprintf(tcinfo->handle_array[tcinfo->used_len].kind,
418                  sizeof(tcinfo->handle_array[t    141                  sizeof(tcinfo->handle_array[tcinfo->used_len].kind),
419                  "%s",                            142                  "%s",
420                  tb[TCA_KIND]                     143                  tb[TCA_KIND]
421                          ? libbpf_nla_getattr_    144                          ? libbpf_nla_getattr_str(tb[TCA_KIND])
422                          : "unknown");            145                          : "unknown");
423         tcinfo->used_len++;                       146         tcinfo->used_len++;
424                                                   147 
425         return 0;                                 148         return 0;
426 }                                                 149 }
427                                                   150 
428 static int dump_filter_nlmsg(void *cookie, voi    151 static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
429 {                                                 152 {
430         const struct bpf_filter_t *filter_info    153         const struct bpf_filter_t *filter_info = cookie;
431                                                   154 
432         return do_filter_dump((struct tcmsg *)    155         return do_filter_dump((struct tcmsg *)msg, tb, filter_info->kind,
433                               filter_info->dev    156                               filter_info->devname, filter_info->ifindex);
434 }                                                 157 }
435                                                   158 
436 static int __show_dev_tc_bpf_name(__u32 id, ch !! 159 static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
437 {                                              !! 160                            struct ip_devname_ifindex *dev)
438         struct bpf_prog_info info = {};        << 
439         __u32 ilen = sizeof(info);             << 
440         int fd, ret;                           << 
441                                                << 
442         fd = bpf_prog_get_fd_by_id(id);        << 
443         if (fd < 0)                            << 
444                 return fd;                     << 
445         ret = bpf_obj_get_info_by_fd(fd, &info << 
446         if (ret < 0)                           << 
447                 goto out;                      << 
448         ret = -ENOENT;                         << 
449         if (info.name[0]) {                    << 
450                 get_prog_full_name(&info, fd,  << 
451                 ret = 0;                       << 
452         }                                      << 
453 out:                                           << 
454         close(fd);                             << 
455         return ret;                            << 
456 }                                              << 
457                                                << 
458 static void __show_dev_tc_bpf(const struct ip_ << 
459                               const enum bpf_a << 
460 {                                              << 
461         __u32 prog_flags[64] = {}, link_flags[ << 
462         __u32 prog_ids[64] = {}, link_ids[64]  << 
463         LIBBPF_OPTS(bpf_prog_query_opts, optq) << 
464         char prog_name[MAX_PROG_FULL_NAME];    << 
465         int ret;                               << 
466                                                << 
467         optq.prog_ids = prog_ids;              << 
468         optq.prog_attach_flags = prog_flags;   << 
469         optq.link_ids = link_ids;              << 
470         optq.link_attach_flags = link_flags;   << 
471         optq.count = ARRAY_SIZE(prog_ids);     << 
472                                                << 
473         ret = bpf_prog_query_opts(dev->ifindex << 
474         if (ret)                               << 
475                 return;                        << 
476         for (i = 0; i < optq.count; i++) {     << 
477                 NET_START_OBJECT;              << 
478                 NET_DUMP_STR("devname", "%s",  << 
479                 NET_DUMP_UINT("ifindex", "(%u) << 
480                 NET_DUMP_STR("kind", " %s", at << 
481                 ret = __show_dev_tc_bpf_name(p << 
482                                              s << 
483                 if (!ret)                      << 
484                         NET_DUMP_STR("name", " << 
485                 NET_DUMP_UINT("prog_id", " pro << 
486                 if (prog_flags[i] || json_outp << 
487                         NET_START_ARRAY("prog_ << 
488                         for (j = 0; prog_flags << 
489                                 if (!(prog_fla << 
490                                         contin << 
491                                 NET_DUMP_UINT_ << 
492                         }                      << 
493                         NET_END_ARRAY("");     << 
494                 }                              << 
495                 if (link_ids[i] || json_output << 
496                         NET_DUMP_UINT("link_id << 
497                         if (link_flags[i] || j << 
498                                 NET_START_ARRA << 
499                                 for (j = 0; li << 
500                                         if (!( << 
501                                                << 
502                                         NET_DU << 
503                                 }              << 
504                                 NET_END_ARRAY( << 
505                         }                      << 
506                 }                              << 
507                 NET_END_OBJECT_FINAL;          << 
508         }                                      << 
509 }                                              << 
510                                                << 
511 static void show_dev_tc_bpf(struct ip_devname_ << 
512 {                                              << 
513         __show_dev_tc_bpf(dev, BPF_TCX_INGRESS << 
514         __show_dev_tc_bpf(dev, BPF_TCX_EGRESS) << 
515                                                << 
516         __show_dev_tc_bpf(dev, BPF_NETKIT_PRIM << 
517         __show_dev_tc_bpf(dev, BPF_NETKIT_PEER << 
518 }                                              << 
519                                                << 
520 static int show_dev_tc_bpf_classic(int sock, u << 
521                                    struct ip_d << 
522 {                                                 161 {
523         struct bpf_filter_t filter_info;          162         struct bpf_filter_t filter_info;
524         struct bpf_tcinfo_t tcinfo;               163         struct bpf_tcinfo_t tcinfo;
525         int i, handle, ret = 0;                   164         int i, handle, ret = 0;
526                                                   165 
527         tcinfo.handle_array = NULL;               166         tcinfo.handle_array = NULL;
528         tcinfo.used_len = 0;                      167         tcinfo.used_len = 0;
529         tcinfo.array_len = 0;                     168         tcinfo.array_len = 0;
530                                                   169 
531         tcinfo.is_qdisc = false;                  170         tcinfo.is_qdisc = false;
532         ret = netlink_get_class(sock, nl_pid,  !! 171         ret = libbpf_nl_get_class(sock, nl_pid, dev->ifindex,
533                                 dump_class_qdi !! 172                                   dump_class_qdisc_nlmsg, &tcinfo);
534         if (ret)                                  173         if (ret)
535                 goto out;                         174                 goto out;
536                                                   175 
537         tcinfo.is_qdisc = true;                   176         tcinfo.is_qdisc = true;
538         ret = netlink_get_qdisc(sock, nl_pid,  !! 177         ret = libbpf_nl_get_qdisc(sock, nl_pid, dev->ifindex,
539                                 dump_class_qdi !! 178                                   dump_class_qdisc_nlmsg, &tcinfo);
540         if (ret)                                  179         if (ret)
541                 goto out;                         180                 goto out;
542                                                   181 
543         filter_info.devname = dev->devname;       182         filter_info.devname = dev->devname;
544         filter_info.ifindex = dev->ifindex;       183         filter_info.ifindex = dev->ifindex;
545         for (i = 0; i < tcinfo.used_len; i++)     184         for (i = 0; i < tcinfo.used_len; i++) {
546                 filter_info.kind = tcinfo.hand    185                 filter_info.kind = tcinfo.handle_array[i].kind;
547                 ret = netlink_get_filter(sock, !! 186                 ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex,
548                                          tcinf !! 187                                            tcinfo.handle_array[i].handle,
549                                          dump_ !! 188                                            dump_filter_nlmsg, &filter_info);
550                 if (ret)                          189                 if (ret)
551                         goto out;                 190                         goto out;
552         }                                         191         }
553                                                   192 
554         /* root, ingress and egress handle */     193         /* root, ingress and egress handle */
555         handle = TC_H_ROOT;                       194         handle = TC_H_ROOT;
556         filter_info.kind = "root";                195         filter_info.kind = "root";
557         ret = netlink_get_filter(sock, nl_pid, !! 196         ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
558                                  dump_filter_n !! 197                                    dump_filter_nlmsg, &filter_info);
559         if (ret)                                  198         if (ret)
560                 goto out;                         199                 goto out;
561                                                   200 
562         handle = TC_H_MAKE(TC_H_CLSACT, TC_H_M    201         handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
563         filter_info.kind = "clsact/ingress";      202         filter_info.kind = "clsact/ingress";
564         ret = netlink_get_filter(sock, nl_pid, !! 203         ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
565                                  dump_filter_n !! 204                                    dump_filter_nlmsg, &filter_info);
566         if (ret)                                  205         if (ret)
567                 goto out;                         206                 goto out;
568                                                   207 
569         handle = TC_H_MAKE(TC_H_CLSACT, TC_H_M    208         handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
570         filter_info.kind = "clsact/egress";       209         filter_info.kind = "clsact/egress";
571         ret = netlink_get_filter(sock, nl_pid, !! 210         ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
572                                  dump_filter_n !! 211                                    dump_filter_nlmsg, &filter_info);
573         if (ret)                                  212         if (ret)
574                 goto out;                         213                 goto out;
575                                                   214 
576 out:                                              215 out:
577         free(tcinfo.handle_array);                216         free(tcinfo.handle_array);
578         return 0;                                 217         return 0;
579 }                                                 218 }
580                                                   219 
581 static int query_flow_dissector(struct bpf_att    220 static int query_flow_dissector(struct bpf_attach_info *attach_info)
582 {                                                 221 {
583         __u32 attach_flags;                       222         __u32 attach_flags;
584         __u32 prog_ids[1];                        223         __u32 prog_ids[1];
585         __u32 prog_cnt;                           224         __u32 prog_cnt;
586         int err;                                  225         int err;
587         int fd;                                   226         int fd;
588                                                   227 
589         fd = open("/proc/self/ns/net", O_RDONL    228         fd = open("/proc/self/ns/net", O_RDONLY);
590         if (fd < 0) {                             229         if (fd < 0) {
591                 p_err("can't open /proc/self/n    230                 p_err("can't open /proc/self/ns/net: %s",
592                       strerror(errno));           231                       strerror(errno));
593                 return -1;                        232                 return -1;
594         }                                         233         }
595         prog_cnt = ARRAY_SIZE(prog_ids);          234         prog_cnt = ARRAY_SIZE(prog_ids);
596         err = bpf_prog_query(fd, BPF_FLOW_DISS    235         err = bpf_prog_query(fd, BPF_FLOW_DISSECTOR, 0,
597                              &attach_flags, pr    236                              &attach_flags, prog_ids, &prog_cnt);
598         close(fd);                                237         close(fd);
599         if (err) {                                238         if (err) {
600                 if (errno == EINVAL) {            239                 if (errno == EINVAL) {
601                         /* Older kernel's don'    240                         /* Older kernel's don't support querying
602                          * flow dissector prog    241                          * flow dissector programs.
603                          */                       242                          */
604                         errno = 0;                243                         errno = 0;
605                         return 0;                 244                         return 0;
606                 }                                 245                 }
607                 p_err("can't query prog: %s",     246                 p_err("can't query prog: %s", strerror(errno));
608                 return -1;                        247                 return -1;
609         }                                         248         }
610                                                   249 
611         if (prog_cnt == 1)                        250         if (prog_cnt == 1)
612                 attach_info->flow_dissector_id    251                 attach_info->flow_dissector_id = prog_ids[0];
613                                                   252 
614         return 0;                                 253         return 0;
615 }                                                 254 }
616                                                   255 
617 static int net_parse_dev(int *argc, char ***ar    256 static int net_parse_dev(int *argc, char ***argv)
618 {                                                 257 {
619         int ifindex;                              258         int ifindex;
620                                                   259 
621         if (is_prefix(**argv, "dev")) {           260         if (is_prefix(**argv, "dev")) {
622                 NEXT_ARGP();                      261                 NEXT_ARGP();
623                                                   262 
624                 ifindex = if_nametoindex(**arg    263                 ifindex = if_nametoindex(**argv);
625                 if (!ifindex)                     264                 if (!ifindex)
626                         p_err("invalid devname    265                         p_err("invalid devname %s", **argv);
627                                                   266 
628                 NEXT_ARGP();                      267                 NEXT_ARGP();
629         } else {                                  268         } else {
630                 p_err("expected 'dev', got: '%    269                 p_err("expected 'dev', got: '%s'?", **argv);
631                 return -1;                        270                 return -1;
632         }                                         271         }
633                                                   272 
634         return ifindex;                           273         return ifindex;
635 }                                                 274 }
636                                                   275 
637 static int do_attach_detach_xdp(int progfd, en    276 static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type,
638                                 int ifindex, b    277                                 int ifindex, bool overwrite)
639 {                                                 278 {
640         __u32 flags = 0;                          279         __u32 flags = 0;
641                                                   280 
642         if (!overwrite)                           281         if (!overwrite)
643                 flags = XDP_FLAGS_UPDATE_IF_NO    282                 flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
644         if (attach_type == NET_ATTACH_TYPE_XDP    283         if (attach_type == NET_ATTACH_TYPE_XDP_GENERIC)
645                 flags |= XDP_FLAGS_SKB_MODE;      284                 flags |= XDP_FLAGS_SKB_MODE;
646         if (attach_type == NET_ATTACH_TYPE_XDP    285         if (attach_type == NET_ATTACH_TYPE_XDP_DRIVER)
647                 flags |= XDP_FLAGS_DRV_MODE;      286                 flags |= XDP_FLAGS_DRV_MODE;
648         if (attach_type == NET_ATTACH_TYPE_XDP    287         if (attach_type == NET_ATTACH_TYPE_XDP_OFFLOAD)
649                 flags |= XDP_FLAGS_HW_MODE;       288                 flags |= XDP_FLAGS_HW_MODE;
650                                                   289 
651         return bpf_xdp_attach(ifindex, progfd, !! 290         return bpf_set_link_xdp_fd(ifindex, progfd, flags);
652 }                                              << 
653                                                << 
654 static int get_tcx_type(enum net_attach_type a << 
655 {                                              << 
656         switch (attach_type) {                 << 
657         case NET_ATTACH_TYPE_TCX_INGRESS:      << 
658                 return BPF_TCX_INGRESS;        << 
659         case NET_ATTACH_TYPE_TCX_EGRESS:       << 
660                 return BPF_TCX_EGRESS;         << 
661         default:                               << 
662                 return -1;                     << 
663         }                                      << 
664 }                                              << 
665                                                << 
666 static int do_attach_tcx(int progfd, enum net_ << 
667 {                                              << 
668         int type = get_tcx_type(attach_type);  << 
669                                                << 
670         return bpf_prog_attach(progfd, ifindex << 
671 }                                              << 
672                                                << 
673 static int do_detach_tcx(int targetfd, enum ne << 
674 {                                              << 
675         int type = get_tcx_type(attach_type);  << 
676                                                << 
677         return bpf_prog_detach(targetfd, type) << 
678 }                                                 291 }
679                                                   292 
680 static int do_attach(int argc, char **argv)       293 static int do_attach(int argc, char **argv)
681 {                                                 294 {
682         enum net_attach_type attach_type;         295         enum net_attach_type attach_type;
683         int progfd, ifindex, err = 0;             296         int progfd, ifindex, err = 0;
684         bool overwrite = false;                   297         bool overwrite = false;
685                                                   298 
686         /* parse attach args */                   299         /* parse attach args */
687         if (!REQ_ARGS(5))                         300         if (!REQ_ARGS(5))
688                 return -EINVAL;                   301                 return -EINVAL;
689                                                   302 
690         attach_type = parse_attach_type(*argv)    303         attach_type = parse_attach_type(*argv);
691         if (attach_type == net_attach_type_siz    304         if (attach_type == net_attach_type_size) {
692                 p_err("invalid net attach/deta    305                 p_err("invalid net attach/detach type: %s", *argv);
693                 return -EINVAL;                   306                 return -EINVAL;
694         }                                         307         }
695         NEXT_ARG();                               308         NEXT_ARG();
696                                                   309 
697         progfd = prog_parse_fd(&argc, &argv);     310         progfd = prog_parse_fd(&argc, &argv);
698         if (progfd < 0)                           311         if (progfd < 0)
699                 return -EINVAL;                   312                 return -EINVAL;
700                                                   313 
701         ifindex = net_parse_dev(&argc, &argv);    314         ifindex = net_parse_dev(&argc, &argv);
702         if (ifindex < 1) {                        315         if (ifindex < 1) {
703                 err = -EINVAL;                 !! 316                 close(progfd);
704                 goto cleanup;                  !! 317                 return -EINVAL;
705         }                                         318         }
706                                                   319 
707         if (argc) {                               320         if (argc) {
708                 if (is_prefix(*argv, "overwrit    321                 if (is_prefix(*argv, "overwrite")) {
709                         overwrite = true;         322                         overwrite = true;
710                 } else {                          323                 } else {
711                         p_err("expected 'overw    324                         p_err("expected 'overwrite', got: '%s'?", *argv);
712                         err = -EINVAL;         !! 325                         close(progfd);
713                         goto cleanup;          !! 326                         return -EINVAL;
714                 }                                 327                 }
715         }                                         328         }
716                                                   329 
717         switch (attach_type) {                 << 
718         /* attach xdp prog */                     330         /* attach xdp prog */
719         case NET_ATTACH_TYPE_XDP:              !! 331         if (is_prefix("xdp", attach_type_strings[attach_type]))
720         case NET_ATTACH_TYPE_XDP_GENERIC:      !! 332                 err = do_attach_detach_xdp(progfd, attach_type, ifindex,
721         case NET_ATTACH_TYPE_XDP_DRIVER:       !! 333                                            overwrite);
722         case NET_ATTACH_TYPE_XDP_OFFLOAD:      << 
723                 err = do_attach_detach_xdp(pro << 
724                 break;                         << 
725         /* attach tcx prog */                  << 
726         case NET_ATTACH_TYPE_TCX_INGRESS:      << 
727         case NET_ATTACH_TYPE_TCX_EGRESS:       << 
728                 err = do_attach_tcx(progfd, at << 
729                 break;                         << 
730         default:                               << 
731                 break;                         << 
732         }                                      << 
733                                                   334 
734         if (err) {                             !! 335         if (err < 0) {
735                 p_err("interface %s attach fai    336                 p_err("interface %s attach failed: %s",
736                       attach_type_strings[atta    337                       attach_type_strings[attach_type], strerror(-err));
737                 goto cleanup;                  !! 338                 return err;
738         }                                         339         }
739                                                   340 
740         if (json_output)                          341         if (json_output)
741                 jsonw_null(json_wtr);             342                 jsonw_null(json_wtr);
742 cleanup:                                       !! 343 
743         close(progfd);                         !! 344         return 0;
744         return err;                            << 
745 }                                                 345 }
746                                                   346 
747 static int do_detach(int argc, char **argv)       347 static int do_detach(int argc, char **argv)
748 {                                                 348 {
749         enum net_attach_type attach_type;         349         enum net_attach_type attach_type;
750         int progfd, ifindex, err = 0;             350         int progfd, ifindex, err = 0;
751                                                   351 
752         /* parse detach args */                   352         /* parse detach args */
753         if (!REQ_ARGS(3))                         353         if (!REQ_ARGS(3))
754                 return -EINVAL;                   354                 return -EINVAL;
755                                                   355 
756         attach_type = parse_attach_type(*argv)    356         attach_type = parse_attach_type(*argv);
757         if (attach_type == net_attach_type_siz    357         if (attach_type == net_attach_type_size) {
758                 p_err("invalid net attach/deta    358                 p_err("invalid net attach/detach type: %s", *argv);
759                 return -EINVAL;                   359                 return -EINVAL;
760         }                                         360         }
761         NEXT_ARG();                               361         NEXT_ARG();
762                                                   362 
763         ifindex = net_parse_dev(&argc, &argv);    363         ifindex = net_parse_dev(&argc, &argv);
764         if (ifindex < 1)                          364         if (ifindex < 1)
765                 return -EINVAL;                   365                 return -EINVAL;
766                                                   366 
767         switch (attach_type) {                 << 
768         /* detach xdp prog */                     367         /* detach xdp prog */
769         case NET_ATTACH_TYPE_XDP:              !! 368         progfd = -1;
770         case NET_ATTACH_TYPE_XDP_GENERIC:      !! 369         if (is_prefix("xdp", attach_type_strings[attach_type]))
771         case NET_ATTACH_TYPE_XDP_DRIVER:       << 
772         case NET_ATTACH_TYPE_XDP_OFFLOAD:      << 
773                 progfd = -1;                   << 
774                 err = do_attach_detach_xdp(pro    370                 err = do_attach_detach_xdp(progfd, attach_type, ifindex, NULL);
775                 break;                         << 
776         /* detach tcx prog */                  << 
777         case NET_ATTACH_TYPE_TCX_INGRESS:      << 
778         case NET_ATTACH_TYPE_TCX_EGRESS:       << 
779                 err = do_detach_tcx(ifindex, a << 
780                 break;                         << 
781         default:                               << 
782                 break;                         << 
783         }                                      << 
784                                                   371 
785         if (err < 0) {                            372         if (err < 0) {
786                 p_err("interface %s detach fai    373                 p_err("interface %s detach failed: %s",
787                       attach_type_strings[atta    374                       attach_type_strings[attach_type], strerror(-err));
788                 return err;                       375                 return err;
789         }                                         376         }
790                                                   377 
791         if (json_output)                          378         if (json_output)
792                 jsonw_null(json_wtr);             379                 jsonw_null(json_wtr);
793                                                   380 
794         return 0;                                 381         return 0;
795 }                                                 382 }
796                                                   383 
797 static int netfilter_link_compar(const void *a << 
798 {                                              << 
799         const struct bpf_link_info *nfa = a;   << 
800         const struct bpf_link_info *nfb = b;   << 
801         int delta;                             << 
802                                                << 
803         delta = nfa->netfilter.pf - nfb->netfi << 
804         if (delta)                             << 
805                 return delta;                  << 
806                                                << 
807         delta = nfa->netfilter.hooknum - nfb-> << 
808         if (delta)                             << 
809                 return delta;                  << 
810                                                << 
811         if (nfa->netfilter.priority < nfb->net << 
812                 return -1;                     << 
813         if (nfa->netfilter.priority > nfb->net << 
814                 return 1;                      << 
815                                                << 
816         return nfa->netfilter.flags - nfb->net << 
817 }                                              << 
818                                                << 
819 static void show_link_netfilter(void)          << 
820 {                                              << 
821         unsigned int nf_link_len = 0, nf_link_ << 
822         struct bpf_link_info *nf_link_info = N << 
823         __u32 id = 0;                          << 
824                                                << 
825         while (true) {                         << 
826                 struct bpf_link_info info;     << 
827                 int fd, err;                   << 
828                 __u32 len;                     << 
829                                                << 
830                 err = bpf_link_get_next_id(id, << 
831                 if (err) {                     << 
832                         if (errno == ENOENT)   << 
833                                 break;         << 
834                         p_err("can't get next  << 
835                         break;                 << 
836                 }                              << 
837                                                << 
838                 fd = bpf_link_get_fd_by_id(id) << 
839                 if (fd < 0) {                  << 
840                         p_err("can't get link  << 
841                         continue;              << 
842                 }                              << 
843                                                << 
844                 memset(&info, 0, sizeof(info)) << 
845                 len = sizeof(info);            << 
846                                                << 
847                 err = bpf_link_get_info_by_fd( << 
848                                                << 
849                 close(fd);                     << 
850                                                << 
851                 if (err) {                     << 
852                         p_err("can't get link  << 
853                         continue;              << 
854                 }                              << 
855                                                << 
856                 if (info.type != BPF_LINK_TYPE << 
857                         continue;              << 
858                                                << 
859                 if (nf_link_count >= nf_link_l << 
860                         static const unsigned  << 
861                         struct bpf_link_info * << 
862                                                << 
863                         if (nf_link_count > ma << 
864                                 p_err("cannot  << 
865                                 break;         << 
866                         }                      << 
867                                                << 
868                         nf_link_len += 16;     << 
869                                                << 
870                         expand = realloc(nf_li << 
871                         if (!expand) {         << 
872                                 p_err("realloc << 
873                                 break;         << 
874                         }                      << 
875                                                << 
876                         nf_link_info = expand; << 
877                 }                              << 
878                                                << 
879                 nf_link_info[nf_link_count] =  << 
880                 nf_link_count++;               << 
881         }                                      << 
882                                                << 
883         if (!nf_link_info)                     << 
884                 return;                        << 
885                                                << 
886         qsort(nf_link_info, nf_link_count, siz << 
887                                                << 
888         for (id = 0; id < nf_link_count; id++) << 
889                 NET_START_OBJECT;              << 
890                 if (json_output)               << 
891                         netfilter_dump_json(&n << 
892                 else                           << 
893                         netfilter_dump_plain(& << 
894                                                << 
895                 NET_DUMP_UINT("id", " prog_id  << 
896                 NET_END_OBJECT;                << 
897         }                                      << 
898                                                << 
899         free(nf_link_info);                    << 
900 }                                              << 
901                                                << 
902 static int do_show(int argc, char **argv)         384 static int do_show(int argc, char **argv)
903 {                                                 385 {
904         struct bpf_attach_info attach_info = {    386         struct bpf_attach_info attach_info = {};
905         int i, sock, ret, filter_idx = -1;        387         int i, sock, ret, filter_idx = -1;
906         struct bpf_netdev_t dev_array;            388         struct bpf_netdev_t dev_array;
907         unsigned int nl_pid = 0;               !! 389         unsigned int nl_pid;
908         char err_buf[256];                        390         char err_buf[256];
909                                                   391 
910         if (argc == 2) {                          392         if (argc == 2) {
911                 filter_idx = net_parse_dev(&ar    393                 filter_idx = net_parse_dev(&argc, &argv);
912                 if (filter_idx < 1)               394                 if (filter_idx < 1)
913                         return -1;                395                         return -1;
914         } else if (argc != 0) {                   396         } else if (argc != 0) {
915                 usage();                          397                 usage();
916         }                                         398         }
917                                                   399 
918         ret = query_flow_dissector(&attach_inf    400         ret = query_flow_dissector(&attach_info);
919         if (ret)                                  401         if (ret)
920                 return -1;                        402                 return -1;
921                                                   403 
922         sock = netlink_open(&nl_pid);          !! 404         sock = libbpf_netlink_open(&nl_pid);
923         if (sock < 0) {                           405         if (sock < 0) {
924                 fprintf(stderr, "failed to ope    406                 fprintf(stderr, "failed to open netlink sock\n");
925                 return -1;                        407                 return -1;
926         }                                         408         }
927                                                   409 
928         dev_array.devices = NULL;                 410         dev_array.devices = NULL;
929         dev_array.used_len = 0;                   411         dev_array.used_len = 0;
930         dev_array.array_len = 0;                  412         dev_array.array_len = 0;
931         dev_array.filter_idx = filter_idx;        413         dev_array.filter_idx = filter_idx;
932                                                   414 
933         if (json_output)                          415         if (json_output)
934                 jsonw_start_array(json_wtr);      416                 jsonw_start_array(json_wtr);
935         NET_START_OBJECT;                         417         NET_START_OBJECT;
936         NET_START_ARRAY("xdp", "%s:\n");          418         NET_START_ARRAY("xdp", "%s:\n");
937         ret = netlink_get_link(sock, nl_pid, d !! 419         ret = libbpf_nl_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
938         NET_END_ARRAY("\n");                      420         NET_END_ARRAY("\n");
939                                                   421 
940         if (!ret) {                               422         if (!ret) {
941                 NET_START_ARRAY("tc", "%s:\n")    423                 NET_START_ARRAY("tc", "%s:\n");
942                 for (i = 0; i < dev_array.used    424                 for (i = 0; i < dev_array.used_len; i++) {
943                         show_dev_tc_bpf(&dev_a !! 425                         ret = show_dev_tc_bpf(sock, nl_pid,
944                         ret = show_dev_tc_bpf_ !! 426                                               &dev_array.devices[i]);
945                                                << 
946                         if (ret)                  427                         if (ret)
947                                 break;            428                                 break;
948                 }                                 429                 }
949                 NET_END_ARRAY("\n");              430                 NET_END_ARRAY("\n");
950         }                                         431         }
951                                                   432 
952         NET_START_ARRAY("flow_dissector", "%s:    433         NET_START_ARRAY("flow_dissector", "%s:\n");
953         if (attach_info.flow_dissector_id > 0)    434         if (attach_info.flow_dissector_id > 0)
954                 NET_DUMP_UINT("id", "id %u", a    435                 NET_DUMP_UINT("id", "id %u", attach_info.flow_dissector_id);
955         NET_END_ARRAY("\n");                      436         NET_END_ARRAY("\n");
956                                                   437 
957         NET_START_ARRAY("netfilter", "%s:\n"); << 
958         show_link_netfilter();                 << 
959         NET_END_ARRAY("\n");                   << 
960                                                << 
961         NET_END_OBJECT;                           438         NET_END_OBJECT;
962         if (json_output)                          439         if (json_output)
963                 jsonw_end_array(json_wtr);        440                 jsonw_end_array(json_wtr);
964                                                   441 
965         if (ret) {                                442         if (ret) {
966                 if (json_output)                  443                 if (json_output)
967                         jsonw_null(json_wtr);     444                         jsonw_null(json_wtr);
968                 libbpf_strerror(ret, err_buf,     445                 libbpf_strerror(ret, err_buf, sizeof(err_buf));
969                 fprintf(stderr, "Error: %s\n",    446                 fprintf(stderr, "Error: %s\n", err_buf);
970         }                                         447         }
971         free(dev_array.devices);                  448         free(dev_array.devices);
972         close(sock);                              449         close(sock);
973         return ret;                               450         return ret;
974 }                                                 451 }
975                                                   452 
976 static int do_help(int argc, char **argv)         453 static int do_help(int argc, char **argv)
977 {                                                 454 {
978         if (json_output) {                        455         if (json_output) {
979                 jsonw_null(json_wtr);             456                 jsonw_null(json_wtr);
980                 return 0;                         457                 return 0;
981         }                                         458         }
982                                                   459 
983         fprintf(stderr,                           460         fprintf(stderr,
984                 "Usage: %1$s %2$s { show | lis !! 461                 "Usage: %s %s { show | list } [dev <devname>]\n"
985                 "       %1$s %2$s attach ATTAC !! 462                 "       %s %s attach ATTACH_TYPE PROG dev <devname> [ overwrite ]\n"
986                 "       %1$s %2$s detach ATTAC !! 463                 "       %s %s detach ATTACH_TYPE dev <devname>\n"
987                 "       %1$s %2$s help\n"      !! 464                 "       %s %s help\n"
988                 "\n"                              465                 "\n"
989                 "       " HELP_SPEC_PROGRAM "\    466                 "       " HELP_SPEC_PROGRAM "\n"
990                 "       ATTACH_TYPE := { xdp | !! 467                 "       ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n"
991                 "                        | tcx << 
992                 "       " HELP_SPEC_OPTIONS "  << 
993                 "\n"                              468                 "\n"
994                 "Note: Only xdp, tcx, tc, netk !! 469                 "Note: Only xdp and tc attachments are supported now.\n"
995                 "      are currently supported << 
996                 "      For progs attached to c    470                 "      For progs attached to cgroups, use \"bpftool cgroup\"\n"
997                 "      to dump program attachm    471                 "      to dump program attachments. For program types\n"
998                 "      sk_{filter,skb,msg,reus    472                 "      sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"
999                 "      consult iproute2.\n"    !! 473                 "      consult iproute2.\n",
1000                 "",                           !! 474                 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1001                 bin_name, argv[-2]);             475                 bin_name, argv[-2]);
1002                                                  476 
1003         return 0;                                477         return 0;
1004 }                                                478 }
1005                                                  479 
1006 static const struct cmd cmds[] = {               480 static const struct cmd cmds[] = {
1007         { "show",       do_show },               481         { "show",       do_show },
1008         { "list",       do_show },               482         { "list",       do_show },
1009         { "attach",     do_attach },             483         { "attach",     do_attach },
1010         { "detach",     do_detach },             484         { "detach",     do_detach },
1011         { "help",       do_help },               485         { "help",       do_help },
1012         { 0 }                                    486         { 0 }
1013 };                                               487 };
1014                                                  488 
1015 int do_net(int argc, char **argv)                489 int do_net(int argc, char **argv)
1016 {                                                490 {
1017         return cmd_select(cmds, argc, argv, d    491         return cmd_select(cmds, argc, argv, do_help);
1018 }                                                492 }
1019                                                  493 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php