tools/testing/selftests/bpf/prog_tests/tc_redirect.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/tc_redirect.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/tc_redirect.c- Extension
.c- Size
- 35981 bytes
- Lines
- 1304
- 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.hlinux/if_tun.hlinux/limits.hlinux/sysctl.hlinux/time_types.hlinux/net_tstamp.hnet/if.hstdbool.hstdio.hsys/stat.hunistd.htest_progs.hnetwork_helpers.hnetlink_helpers.htest_tc_neigh_fib.skel.htest_tc_neigh.skel.htest_tc_peer.skel.htest_tc_dtime.skel.h
Detected Declarations
struct netns_setup_resultenum dev_modefunction write_filefunction netns_setup_namespacesfunction netns_setup_namespaces_nofailfunction get_ifaddrfunction create_netkitfunction netns_setup_links_and_routesfunction qdisc_clsact_createfunction xgress_filter_addfunction netns_load_bpffunction test_tcpfunction test_pingfunction test_connectivityfunction set_forwardingfunction __rcv_tstampfunction rcv_tstampfunction wait_netstamp_needed_keyfunction snd_tstampfunction test_inet_dtimefunction netns_load_dtime_bpffunction test_tcp_clear_dtimefunction test_tcp_dtimefunction test_udp_dtimefunction test_tc_redirect_dtimefunction test_tc_redirect_neigh_fibfunction test_tc_redirect_neighfunction test_tc_redirect_peerfunction tun_openfunction tun_relay_loopfunction test_tc_redirect_peer_l3function test_tc_redirect
Annotated Snippet
struct netns_setup_result {
enum dev_mode dev_mode;
int ifindex_src;
int ifindex_src_fwd;
int ifindex_dst;
int ifindex_dst_fwd;
};
static int get_ifaddr(const char *name, char *ifaddr)
{
char path[PATH_MAX];
FILE *f;
int ret;
snprintf(path, PATH_MAX, "/sys/class/net/%s/address", name);
f = fopen(path, "r");
if (!ASSERT_OK_PTR(f, path))
return -1;
ret = fread(ifaddr, 1, IFADDR_STR_LEN, f);
if (!ASSERT_EQ(ret, IFADDR_STR_LEN, "fread ifaddr")) {
fclose(f);
return -1;
}
fclose(f);
return 0;
}
static int create_netkit(int mode, char *prim, char *peer)
{
struct rtattr *linkinfo, *data, *peer_info;
struct rtnl_handle rth = { .fd = -1 };
const char *type = "netkit";
struct {
struct nlmsghdr n;
struct ifinfomsg i;
char buf[1024];
} req = {};
int err;
err = rtnl_open(&rth, 0);
if (!ASSERT_OK(err, "open_rtnetlink"))
return err;
memset(&req, 0, sizeof(req));
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
req.n.nlmsg_type = RTM_NEWLINK;
req.i.ifi_family = AF_UNSPEC;
addattr_l(&req.n, sizeof(req), IFLA_IFNAME, prim, strlen(prim));
linkinfo = addattr_nest(&req.n, sizeof(req), IFLA_LINKINFO);
addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type, strlen(type));
data = addattr_nest(&req.n, sizeof(req), IFLA_INFO_DATA);
addattr32(&req.n, sizeof(req), IFLA_NETKIT_MODE, mode);
peer_info = addattr_nest(&req.n, sizeof(req), IFLA_NETKIT_PEER_INFO);
req.n.nlmsg_len += sizeof(struct ifinfomsg);
addattr_l(&req.n, sizeof(req), IFLA_IFNAME, peer, strlen(peer));
addattr_nest_end(&req.n, peer_info);
addattr_nest_end(&req.n, data);
addattr_nest_end(&req.n, linkinfo);
err = rtnl_talk(&rth, &req.n, NULL);
ASSERT_OK(err, "talk_rtnetlink");
rtnl_close(&rth);
return err;
}
static int netns_setup_links_and_routes(struct netns_setup_result *result)
{
struct nstoken *nstoken = NULL;
char src_fwd_addr[IFADDR_STR_LEN+1] = {};
char src_addr[IFADDR_STR_LEN + 1] = {};
int err;
if (result->dev_mode == MODE_VETH) {
SYS(fail, "ip link add src address " MAC_SRC " type veth "
"peer name src_fwd address " MAC_SRC_FWD);
SYS(fail, "ip link add dst address " MAC_DST " type veth "
"peer name dst_fwd address " MAC_DST_FWD);
} else if (result->dev_mode == MODE_NETKIT) {
err = create_netkit(NETKIT_L3, "src", "src_fwd");
if (!ASSERT_OK(err, "create_ifindex_src"))
goto fail;
err = create_netkit(NETKIT_L3, "dst", "dst_fwd");
if (!ASSERT_OK(err, "create_ifindex_dst"))
goto fail;
}
if (get_ifaddr("src_fwd", src_fwd_addr))
Annotation
- Immediate include surface: `arpa/inet.h`, `linux/if_tun.h`, `linux/limits.h`, `linux/sysctl.h`, `linux/time_types.h`, `linux/net_tstamp.h`, `net/if.h`, `stdbool.h`.
- Detected declarations: `struct netns_setup_result`, `enum dev_mode`, `function write_file`, `function netns_setup_namespaces`, `function netns_setup_namespaces_nofail`, `function get_ifaddr`, `function create_netkit`, `function netns_setup_links_and_routes`, `function qdisc_clsact_create`, `function xgress_filter_add`.
- 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.