tools/testing/selftests/bpf/network_helpers.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/network_helpers.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/network_helpers.c- Extension
.c- Size
- 28409 bytes
- Lines
- 1281
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
errno.hstdbool.hstdio.hstring.hunistd.hsched.harpa/inet.hsys/mount.hsys/stat.hsys/types.hsys/un.hsys/eventfd.hlinux/err.hlinux/in.hlinux/in6.hlinux/limits.hlinux/ip.hnetinet/udp.hnetinet/tcp.hnet/if.hbpf_util.hnetwork_helpers.htest_progs.hpcap/pcap.hpcap/dlt.h
Detected Declarations
struct nstokenstruct send_recv_argstruct tmonitor_ctxfunction settimeofunction start_server_addrfunction start_server_strfunction start_serverfunction reuseport_cbfunction free_fdsfunction fastopen_connectfunction client_socketfunction connect_to_addrfunction connect_to_addr_strfunction connect_to_fd_optsfunction connect_to_fdfunction connect_fd_to_fdfunction make_sockaddrfunction append_tidfunction remove_netnsfunction make_netnsfunction close_netnsfunction open_tuntapfunction get_socket_local_portfunction get_hw_ring_sizefunction set_hw_ring_sizefunction send_recv_datafunction tc_prog_attachfunction __base_prfunction traffic_monitor_set_printfunction tm_printfunction is_ethernetfunction show_transportfunction show_ipv6_packetfunction show_ipv4_packetfunction encode_test_namefunction traffic_monitor_releasefunction traffic_monitor_stop
Annotated Snippet
struct nstoken {
int orig_netns_fd;
};
struct nstoken *open_netns(const char *name)
{
int nsfd;
char nspath[PATH_MAX];
int err;
struct nstoken *token;
token = calloc(1, sizeof(struct nstoken));
if (!token) {
log_err("Failed to malloc token");
return NULL;
}
token->orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
if (token->orig_netns_fd == -1) {
log_err("Failed to open(/proc/self/ns/net)");
goto fail;
}
snprintf(nspath, sizeof(nspath), "%s/%s", "/var/run/netns", name);
nsfd = open(nspath, O_RDONLY | O_CLOEXEC);
if (nsfd == -1) {
log_err("Failed to open(%s)", nspath);
goto fail;
}
err = setns(nsfd, CLONE_NEWNET);
close(nsfd);
if (err) {
log_err("Failed to setns(nsfd)");
goto fail;
}
return token;
fail:
if (token->orig_netns_fd != -1)
close(token->orig_netns_fd);
free(token);
return NULL;
}
void close_netns(struct nstoken *token)
{
if (!token)
return;
if (setns(token->orig_netns_fd, CLONE_NEWNET))
log_err("Failed to setns(orig_netns_fd)");
close(token->orig_netns_fd);
free(token);
}
int open_tuntap(const char *dev_name, bool need_mac)
{
int err = 0;
struct ifreq ifr;
int fd = open("/dev/net/tun", O_RDWR);
if (!ASSERT_GE(fd, 0, "open(/dev/net/tun)"))
return -1;
ifr.ifr_flags = IFF_NO_PI | (need_mac ? IFF_TAP : IFF_TUN);
strscpy(ifr.ifr_name, dev_name);
err = ioctl(fd, TUNSETIFF, &ifr);
if (!ASSERT_OK(err, "ioctl(TUNSETIFF)")) {
close(fd);
return -1;
}
err = fcntl(fd, F_SETFL, O_NONBLOCK);
if (!ASSERT_OK(err, "fcntl(O_NONBLOCK)")) {
close(fd);
return -1;
}
return fd;
}
int get_socket_local_port(int sock_fd)
{
struct sockaddr_storage addr;
socklen_t addrlen = sizeof(addr);
int err;
err = getsockname(sock_fd, (struct sockaddr *)&addr, &addrlen);
Annotation
- Immediate include surface: `errno.h`, `stdbool.h`, `stdio.h`, `string.h`, `unistd.h`, `sched.h`, `arpa/inet.h`, `sys/mount.h`.
- Detected declarations: `struct nstoken`, `struct send_recv_arg`, `struct tmonitor_ctx`, `function settimeo`, `function start_server_addr`, `function start_server_str`, `function start_server`, `function reuseport_cb`, `function free_fds`, `function fastopen_connect`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.