tools/lib/bpf/netlink.c
Source file repositories/reference/linux-study-clean/tools/lib/bpf/netlink.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/bpf/netlink.c- Extension
.c- Size
- 22724 bytes
- Lines
- 939
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdlib.hmemory.hunistd.harpa/inet.hlinux/bpf.hlinux/if_ether.hlinux/pkt_cls.hlinux/rtnetlink.hlinux/netdev.hsys/socket.herrno.htime.hbpf.hlibbpf.hlibbpf_internal.hnlattr.h
Detected Declarations
struct xdp_link_infostruct xdp_id_mdstruct xdp_features_mdstruct bpf_cb_ctxfunction libbpf_netlink_openfunction libbpf_netlink_closefunction netlink_recvmsgfunction alloc_iovfunction libbpf_netlink_recvfunction libbpf_netlink_send_recvfunction parse_genl_family_idfunction libbpf_netlink_resolve_genl_family_idfunction __bpf_set_link_xdp_fd_replacefunction bpf_xdp_attachfunction bpf_xdp_detachfunction __dump_link_nlmsgfunction get_xdp_infofunction parse_xdp_featuresfunction bpf_xdp_queryfunction bpf_xdp_query_idfunction clsact_configfunction qdisc_configfunction attach_point_to_configfunction tc_get_tcm_parentfunction tc_qdisc_modifyfunction tc_qdisc_create_exclfunction tc_qdisc_deletefunction bpf_tc_hook_createfunction bpf_tc_hook_destroyfunction __get_tc_infofunction get_tc_infofunction tc_add_fd_and_namefunction bpf_tc_attachfunction __bpf_tc_detachfunction bpf_tc_detachfunction bpf_tc_query
Annotated Snippet
struct xdp_link_info {
__u32 prog_id;
__u32 drv_prog_id;
__u32 hw_prog_id;
__u32 skb_prog_id;
__u8 attach_mode;
};
struct xdp_id_md {
int ifindex;
__u32 flags;
struct xdp_link_info info;
__u64 feature_flags;
};
struct xdp_features_md {
int ifindex;
__u32 xdp_zc_max_segs;
__u64 flags;
};
static int libbpf_netlink_open(__u32 *nl_pid, int proto)
{
struct sockaddr_nl sa;
socklen_t addrlen;
int one = 1, ret;
int sock;
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
sock = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, proto);
if (sock < 0)
return -errno;
if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
&one, sizeof(one)) < 0) {
pr_warn("Netlink error reporting not supported\n");
}
if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
ret = -errno;
goto cleanup;
}
addrlen = sizeof(sa);
if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
ret = -errno;
goto cleanup;
}
if (addrlen != sizeof(sa)) {
ret = -LIBBPF_ERRNO__INTERNAL;
goto cleanup;
}
*nl_pid = sa.nl_pid;
return sock;
cleanup:
close(sock);
return ret;
}
static void libbpf_netlink_close(int sock)
{
close(sock);
}
enum {
NL_CONT,
NL_NEXT,
NL_DONE,
};
static int netlink_recvmsg(int sock, struct msghdr *mhdr, int flags)
{
int len;
do {
len = recvmsg(sock, mhdr, flags);
} while (len < 0 && (errno == EINTR || errno == EAGAIN));
if (len < 0)
return -errno;
return len;
}
static int alloc_iov(struct iovec *iov, int len)
{
Annotation
- Immediate include surface: `stdlib.h`, `memory.h`, `unistd.h`, `arpa/inet.h`, `linux/bpf.h`, `linux/if_ether.h`, `linux/pkt_cls.h`, `linux/rtnetlink.h`.
- Detected declarations: `struct xdp_link_info`, `struct xdp_id_md`, `struct xdp_features_md`, `struct bpf_cb_ctx`, `function libbpf_netlink_open`, `function libbpf_netlink_close`, `function netlink_recvmsg`, `function alloc_iov`, `function libbpf_netlink_recv`, `function libbpf_netlink_send_recv`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.