tools/testing/selftests/net/psock_fanout.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/psock_fanout.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/net/psock_fanout.c- Extension
.c- Size
- 16085 bytes
- Lines
- 612
- 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
arpa/inet.herrno.hfcntl.hlinux/unistd.hlinux/filter.hlinux/bpf.hlinux/if_packet.hnet/if.hnet/ethernet.hnetinet/ip.hnetinet/udp.hpoll.hsched.hstdint.hstdio.hstdlib.hstring.hsys/mman.hsys/socket.hsys/ioctl.hsys/stat.hsys/types.hunistd.hpsock_lib.hkselftest.h
Detected Declarations
function loopback_set_up_downfunction sock_fanout_openfunction sock_fanout_set_cbpffunction sock_fanout_getoptsfunction sock_fanout_set_ebpffunction sock_fanout_read_ringfunction sock_fanout_readfunction test_unbound_fanoutfunction test_control_singlefunction test_control_groupfunction test_control_group_max_num_membersfunction test_unique_fanout_group_idsfunction test_datapathfunction closefunction set_cpuaffinityfunction main
Annotated Snippet
if (err) {
perror("SIOCSIFFLAGS");
exit(1);
}
}
close(fd);
}
/* Open a socket in a given fanout mode.
* @return -1 if mode is bad, a valid socket otherwise */
static int sock_fanout_open(uint16_t typeflags, uint16_t group_id)
{
struct sockaddr_ll addr = {0};
struct fanout_args args;
int fd, val, err;
fd = socket(PF_PACKET, SOCK_RAW, 0);
if (fd < 0) {
perror("socket packet");
exit(1);
}
pair_udp_setfilter(fd);
addr.sll_family = AF_PACKET;
addr.sll_protocol = htons(ETH_P_IP);
addr.sll_ifindex = if_nametoindex("lo");
if (addr.sll_ifindex == 0) {
perror("if_nametoindex");
exit(1);
}
if (bind(fd, (void *) &addr, sizeof(addr))) {
perror("bind packet");
exit(1);
}
if (cfg_max_num_members) {
args.id = group_id;
args.type_flags = typeflags;
args.max_num_members = cfg_max_num_members;
err = setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &args,
sizeof(args));
} else {
val = (((int) typeflags) << 16) | group_id;
err = setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val,
sizeof(val));
}
if (err) {
if (close(fd)) {
perror("close packet");
exit(1);
}
return -1;
}
return fd;
}
static void sock_fanout_set_cbpf(int fd)
{
struct sock_filter bpf_filter[] = {
BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 80), /* ldb [80] */
BPF_STMT(BPF_RET | BPF_A, 0), /* ret A */
};
struct sock_fprog bpf_prog;
bpf_prog.filter = bpf_filter;
bpf_prog.len = ARRAY_SIZE(bpf_filter);
if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &bpf_prog,
sizeof(bpf_prog))) {
perror("fanout data cbpf");
exit(1);
}
}
static void sock_fanout_getopts(int fd, uint16_t *typeflags, uint16_t *group_id)
{
int sockopt;
socklen_t sockopt_len = sizeof(sockopt);
if (getsockopt(fd, SOL_PACKET, PACKET_FANOUT,
&sockopt, &sockopt_len)) {
perror("failed to getsockopt");
exit(1);
}
*typeflags = sockopt >> 16;
*group_id = sockopt & 0xfffff;
}
Annotation
- Immediate include surface: `arpa/inet.h`, `errno.h`, `fcntl.h`, `linux/unistd.h`, `linux/filter.h`, `linux/bpf.h`, `linux/if_packet.h`, `net/if.h`.
- Detected declarations: `function loopback_set_up_down`, `function sock_fanout_open`, `function sock_fanout_set_cbpf`, `function sock_fanout_getopts`, `function sock_fanout_set_ebpf`, `function sock_fanout_read_ring`, `function sock_fanout_read`, `function test_unbound_fanout`, `function test_control_single`, `function test_control_group`.
- 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.