tools/testing/selftests/bpf/progs/test_lwt_redirect.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/test_lwt_redirect.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/test_lwt_redirect.c- Extension
.c- Size
- 1909 bytes
- Lines
- 91
- 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
linux/bpf.hbpf/bpf_endian.hbpf/bpf_helpers.hlinux/ip.hlinux/if_ether.h
Detected Declarations
function prepend_dummy_macfunction get_redirect_targetfunction test_lwt_redirect_infunction test_lwt_redirect_outfunction test_lwt_redirect_out_nomacfunction test_lwt_redirect_in_nomac
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#include <linux/ip.h>
#include <linux/if_ether.h>
/* We don't care about whether the packet can be received by network stack.
* Just care if the packet is sent to the correct device at correct direction
* and not panic the kernel.
*/
static int prepend_dummy_mac(struct __sk_buff *skb)
{
char mac[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0xf,
0xe, 0xd, 0xc, 0xb, 0xa, 0x08, 0x00};
if (bpf_skb_change_head(skb, ETH_HLEN, 0))
return -1;
if (bpf_skb_store_bytes(skb, 0, mac, sizeof(mac), 0))
return -1;
return 0;
}
/* Use the last byte of IP address to redirect the packet */
static int get_redirect_target(struct __sk_buff *skb)
{
struct iphdr *iph = NULL;
void *start = (void *)(long)skb->data;
void *end = (void *)(long)skb->data_end;
if (start + sizeof(*iph) > end)
return -1;
iph = (struct iphdr *)start;
return bpf_ntohl(iph->daddr) & 0xff;
}
SEC("redir_ingress")
int test_lwt_redirect_in(struct __sk_buff *skb)
{
int target = get_redirect_target(skb);
if (target < 0)
return BPF_OK;
if (prepend_dummy_mac(skb))
return BPF_DROP;
return bpf_redirect(target, BPF_F_INGRESS);
}
SEC("redir_egress")
int test_lwt_redirect_out(struct __sk_buff *skb)
{
int target = get_redirect_target(skb);
if (target < 0)
return BPF_OK;
if (prepend_dummy_mac(skb))
return BPF_DROP;
return bpf_redirect(target, 0);
}
SEC("redir_egress_nomac")
int test_lwt_redirect_out_nomac(struct __sk_buff *skb)
{
int target = get_redirect_target(skb);
if (target < 0)
return BPF_OK;
return bpf_redirect(target, 0);
}
SEC("redir_ingress_nomac")
int test_lwt_redirect_in_nomac(struct __sk_buff *skb)
{
int target = get_redirect_target(skb);
if (target < 0)
return BPF_OK;
return bpf_redirect(target, BPF_F_INGRESS);
}
char _license[] SEC("license") = "GPL";
Annotation
- Immediate include surface: `linux/bpf.h`, `bpf/bpf_endian.h`, `bpf/bpf_helpers.h`, `linux/ip.h`, `linux/if_ether.h`.
- Detected declarations: `function prepend_dummy_mac`, `function get_redirect_target`, `function test_lwt_redirect_in`, `function test_lwt_redirect_out`, `function test_lwt_redirect_out_nomac`, `function test_lwt_redirect_in_nomac`.
- 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.