Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Consistent prefixes for interfaces in libbpf.h.

libbpf is used more and more outside kernel tree. That means the library
should follow good practices in library design and implementation to
play well with third party code that uses it.

One of such practices is to have a common prefix (or a few) for every
interface, function or data structure, library provides. I helps to
avoid name conflicts with other libraries and keeps API consistent.

Inconsistent names in libbpf already cause problems in real life. E.g.
an application can't use both libbpf and libnl due to conflicting
symbols.

Having common prefix will help to fix current and avoid future problems.

libbpf already uses the following prefixes for its interfaces:
* bpf_ for bpf system call wrappers, program/map/elf-object
abstractions and a few other things;
* btf_ for BTF related API;
* libbpf_ for everything else.

The patch adds libbpf_ prefix to functions and typedef in libbpf.h that
use none of mentioned above prefixes and doesn't fit well into the first
two categories.

Since affected part of API is used in bpftool, the patch applies
corresponding change to bpftool as well. Having it in a separate patch
will cause a state of tree where bpftool is broken what may not be a
good idea.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>

authored by

Andrey Ignatov and committed by
Daniel Borkmann
aae57780 434fe9d4

+45 -43
+15 -16
tools/bpf/bpftool/net.c
··· 127 127 tcinfo.array_len = 0; 128 128 129 129 tcinfo.is_qdisc = false; 130 - ret = nl_get_class(sock, nl_pid, dev->ifindex, dump_class_qdisc_nlmsg, 131 - &tcinfo); 130 + ret = libbpf_nl_get_class(sock, nl_pid, dev->ifindex, 131 + dump_class_qdisc_nlmsg, &tcinfo); 132 132 if (ret) 133 133 goto out; 134 134 135 135 tcinfo.is_qdisc = true; 136 - ret = nl_get_qdisc(sock, nl_pid, dev->ifindex, dump_class_qdisc_nlmsg, 137 - &tcinfo); 136 + ret = libbpf_nl_get_qdisc(sock, nl_pid, dev->ifindex, 137 + dump_class_qdisc_nlmsg, &tcinfo); 138 138 if (ret) 139 139 goto out; 140 140 ··· 142 142 filter_info.ifindex = dev->ifindex; 143 143 for (i = 0; i < tcinfo.used_len; i++) { 144 144 filter_info.kind = tcinfo.handle_array[i].kind; 145 - ret = nl_get_filter(sock, nl_pid, dev->ifindex, 146 - tcinfo.handle_array[i].handle, 147 - dump_filter_nlmsg, 148 - &filter_info); 145 + ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, 146 + tcinfo.handle_array[i].handle, 147 + dump_filter_nlmsg, &filter_info); 149 148 if (ret) 150 149 goto out; 151 150 } ··· 152 153 /* root, ingress and egress handle */ 153 154 handle = TC_H_ROOT; 154 155 filter_info.kind = "root"; 155 - ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle, 156 - dump_filter_nlmsg, &filter_info); 156 + ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle, 157 + dump_filter_nlmsg, &filter_info); 157 158 if (ret) 158 159 goto out; 159 160 160 161 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS); 161 162 filter_info.kind = "clsact/ingress"; 162 - ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle, 163 - dump_filter_nlmsg, &filter_info); 163 + ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle, 164 + dump_filter_nlmsg, &filter_info); 164 165 if (ret) 165 166 goto out; 166 167 167 168 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS); 168 169 filter_info.kind = "clsact/egress"; 169 - ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle, 170 - dump_filter_nlmsg, &filter_info); 170 + ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle, 171 + dump_filter_nlmsg, &filter_info); 171 172 if (ret) 172 173 goto out; 173 174 ··· 195 196 usage(); 196 197 } 197 198 198 - sock = bpf_netlink_open(&nl_pid); 199 + sock = libbpf_netlink_open(&nl_pid); 199 200 if (sock < 0) { 200 201 fprintf(stderr, "failed to open netlink sock\n"); 201 202 return -1; ··· 210 211 jsonw_start_array(json_wtr); 211 212 NET_START_OBJECT; 212 213 NET_START_ARRAY("xdp", "%s:\n"); 213 - ret = nl_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array); 214 + ret = libbpf_nl_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array); 214 215 NET_END_ARRAY("\n"); 215 216 216 217 if (!ret) {
+10 -10
tools/lib/bpf/libbpf.h
··· 305 305 bpf_perf_event_print_t fn, void *priv); 306 306 307 307 struct nlattr; 308 - typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); 309 - int bpf_netlink_open(unsigned int *nl_pid); 310 - int nl_get_link(int sock, unsigned int nl_pid, dump_nlmsg_t dump_link_nlmsg, 311 - void *cookie); 312 - int nl_get_class(int sock, unsigned int nl_pid, int ifindex, 313 - dump_nlmsg_t dump_class_nlmsg, void *cookie); 314 - int nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex, 315 - dump_nlmsg_t dump_qdisc_nlmsg, void *cookie); 316 - int nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 317 - dump_nlmsg_t dump_filter_nlmsg, void *cookie); 308 + typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); 309 + int libbpf_netlink_open(unsigned int *nl_pid); 310 + int libbpf_nl_get_link(int sock, unsigned int nl_pid, 311 + libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie); 312 + int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex, 313 + libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie); 314 + int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex, 315 + libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie); 316 + int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 317 + libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie); 318 318 #endif
+20 -17
tools/lib/bpf/netlink.c
··· 18 18 #define SOL_NETLINK 270 19 19 #endif 20 20 21 - typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t, 21 + typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, libbpf_dump_nlmsg_t, 22 22 void *cookie); 23 23 24 - int bpf_netlink_open(__u32 *nl_pid) 24 + int libbpf_netlink_open(__u32 *nl_pid) 25 25 { 26 26 struct sockaddr_nl sa; 27 27 socklen_t addrlen; ··· 65 65 } 66 66 67 67 static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq, 68 - __dump_nlmsg_t _fn, dump_nlmsg_t fn, 68 + __dump_nlmsg_t _fn, libbpf_dump_nlmsg_t fn, 69 69 void *cookie) 70 70 { 71 71 bool multipart = true; ··· 133 133 } req; 134 134 __u32 nl_pid; 135 135 136 - sock = bpf_netlink_open(&nl_pid); 136 + sock = libbpf_netlink_open(&nl_pid); 137 137 if (sock < 0) 138 138 return sock; 139 139 ··· 181 181 return ret; 182 182 } 183 183 184 - static int __dump_link_nlmsg(struct nlmsghdr *nlh, dump_nlmsg_t dump_link_nlmsg, 185 - void *cookie) 184 + static int __dump_link_nlmsg(struct nlmsghdr *nlh, 185 + libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie) 186 186 { 187 187 struct nlattr *tb[IFLA_MAX + 1], *attr; 188 188 struct ifinfomsg *ifi = NLMSG_DATA(nlh); ··· 196 196 return dump_link_nlmsg(cookie, ifi, tb); 197 197 } 198 198 199 - int nl_get_link(int sock, unsigned int nl_pid, dump_nlmsg_t dump_link_nlmsg, 200 - void *cookie) 199 + int libbpf_nl_get_link(int sock, unsigned int nl_pid, 200 + libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie) 201 201 { 202 202 struct { 203 203 struct nlmsghdr nlh; ··· 219 219 } 220 220 221 221 static int __dump_class_nlmsg(struct nlmsghdr *nlh, 222 - dump_nlmsg_t dump_class_nlmsg, void *cookie) 222 + libbpf_dump_nlmsg_t dump_class_nlmsg, 223 + void *cookie) 223 224 { 224 225 struct nlattr *tb[TCA_MAX + 1], *attr; 225 226 struct tcmsg *t = NLMSG_DATA(nlh); ··· 234 233 return dump_class_nlmsg(cookie, t, tb); 235 234 } 236 235 237 - int nl_get_class(int sock, unsigned int nl_pid, int ifindex, 238 - dump_nlmsg_t dump_class_nlmsg, void *cookie) 236 + int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex, 237 + libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie) 239 238 { 240 239 struct { 241 240 struct nlmsghdr nlh; ··· 258 257 } 259 258 260 259 static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh, 261 - dump_nlmsg_t dump_qdisc_nlmsg, void *cookie) 260 + libbpf_dump_nlmsg_t dump_qdisc_nlmsg, 261 + void *cookie) 262 262 { 263 263 struct nlattr *tb[TCA_MAX + 1], *attr; 264 264 struct tcmsg *t = NLMSG_DATA(nlh); ··· 273 271 return dump_qdisc_nlmsg(cookie, t, tb); 274 272 } 275 273 276 - int nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex, 277 - dump_nlmsg_t dump_qdisc_nlmsg, void *cookie) 274 + int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex, 275 + libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie) 278 276 { 279 277 struct { 280 278 struct nlmsghdr nlh; ··· 297 295 } 298 296 299 297 static int __dump_filter_nlmsg(struct nlmsghdr *nlh, 300 - dump_nlmsg_t dump_filter_nlmsg, void *cookie) 298 + libbpf_dump_nlmsg_t dump_filter_nlmsg, 299 + void *cookie) 301 300 { 302 301 struct nlattr *tb[TCA_MAX + 1], *attr; 303 302 struct tcmsg *t = NLMSG_DATA(nlh); ··· 312 309 return dump_filter_nlmsg(cookie, t, tb); 313 310 } 314 311 315 - int nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 316 - dump_nlmsg_t dump_filter_nlmsg, void *cookie) 312 + int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 313 + libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie) 317 314 { 318 315 struct { 319 316 struct nlmsghdr nlh;