tools/testing/selftests/bpf/prog_tests/connect_force_port.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/connect_force_port.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/connect_force_port.c- Extension
.c- Size
- 4650 bytes
- Lines
- 186
- 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
test_progs.hcgroup_helpers.hnetwork_helpers.h
Detected Declarations
function verify_portsfunction run_testfunction test_connect_force_port
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
#include "cgroup_helpers.h"
#include "network_helpers.h"
static int verify_ports(int family, int fd,
__u16 expected_local, __u16 expected_peer)
{
struct sockaddr_storage addr;
socklen_t len = sizeof(addr);
__u16 port;
if (getsockname(fd, (struct sockaddr *)&addr, &len)) {
log_err("Failed to get server addr");
return -1;
}
if (family == AF_INET)
port = ((struct sockaddr_in *)&addr)->sin_port;
else
port = ((struct sockaddr_in6 *)&addr)->sin6_port;
if (ntohs(port) != expected_local) {
log_err("Unexpected local port %d, expected %d", ntohs(port),
expected_local);
return -1;
}
if (getpeername(fd, (struct sockaddr *)&addr, &len)) {
log_err("Failed to get peer addr");
return -1;
}
if (family == AF_INET)
port = ((struct sockaddr_in *)&addr)->sin_port;
else
port = ((struct sockaddr_in6 *)&addr)->sin6_port;
if (ntohs(port) != expected_peer) {
log_err("Unexpected peer port %d, expected %d", ntohs(port),
expected_peer);
return -1;
}
return 0;
}
static int run_test(int cgroup_fd, int server_fd, int family, int type)
{
bool v4 = family == AF_INET;
__u16 expected_local_port = v4 ? 22222 : 22223;
__u16 expected_peer_port = 60000;
struct bpf_program *prog;
struct bpf_object *obj;
struct bpf_map *map;
__u16 *port_ptr;
size_t port_size;
const char *obj_file = v4 ? "connect_force_port4.bpf.o" : "connect_force_port6.bpf.o";
int fd, err;
__u32 duration = 0;
obj = bpf_object__open_file(obj_file, NULL);
if (!ASSERT_OK_PTR(obj, "bpf_obj_open"))
return -1;
map = bpf_object__find_map_by_name(obj, ".bss");
if (!ASSERT_OK_PTR(map, "find bss map")) {
err = -EIO;
goto close_bpf_object;
}
port_ptr = bpf_map__initial_value(map, &port_size);
if (!ASSERT_OK_PTR(port_ptr, "get bss initial value")) {
err = -EIO;
goto close_bpf_object;
}
/* Auto assigns the port according to availability */
*port_ptr = ntohs(get_socket_local_port(server_fd));
err = bpf_object__load(obj);
if (!ASSERT_OK(err, "bpf_obj_load")) {
err = -EIO;
goto close_bpf_object;
}
prog = bpf_object__find_program_by_name(obj, v4 ?
"connect4" :
"connect6");
Annotation
- Immediate include surface: `test_progs.h`, `cgroup_helpers.h`, `network_helpers.h`.
- Detected declarations: `function verify_ports`, `function run_test`, `function test_connect_force_port`.
- 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.