tools/testing/selftests/bpf/progs/xdp_hw_metadata.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/xdp_hw_metadata.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/xdp_hw_metadata.c
Extension
.c
Size
2957 bytes
Lines
118
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.

Dependency Surface

Detected Declarations

Annotated Snippet

if (eth->h_proto == bpf_htons(ETH_P_IP)) {
			iph = (void *)(eth + 1);
			if (iph + 1 < data_end && iph->protocol == IPPROTO_UDP)
				udp = (void *)(iph + 1);
		}
		if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
			ip6h = (void *)(eth + 1);
			if (ip6h + 1 < data_end && ip6h->nexthdr == IPPROTO_UDP)
				udp = (void *)(ip6h + 1);
		}
		if (udp && udp + 1 > data_end)
			udp = NULL;
	}

	if (!udp) {
		__sync_add_and_fetch(&pkts_skip, 1);
		return XDP_PASS;
	}

	/* Forwarding UDP:9091 to AF_XDP */
	if (udp->dest != bpf_htons(9091)) {
		__sync_add_and_fetch(&pkts_skip, 1);
		return XDP_PASS;
	}

	err = bpf_xdp_adjust_meta(ctx, -(int)sizeof(struct xdp_meta));
	if (err) {
		__sync_add_and_fetch(&pkts_fail, 1);
		return XDP_PASS;
	}

	data = (void *)(long)ctx->data;
	data_meta = (void *)(long)ctx->data_meta;
	meta = data_meta;

	if (meta + 1 > data) {
		__sync_add_and_fetch(&pkts_fail, 1);
		return XDP_PASS;
	}

	meta->hint_valid = 0;

	meta->xdp_timestamp = bpf_ktime_get_tai_ns();
	err = bpf_xdp_metadata_rx_timestamp(ctx, &meta->rx_timestamp);
	if (err)
		meta->rx_timestamp_err = err;
	else
		meta->hint_valid |= XDP_META_FIELD_TS;

	err = bpf_xdp_metadata_rx_hash(ctx, &meta->rx_hash,
				       &meta->rx_hash_type);
	if (err)
		meta->rx_hash_err = err;
	else
		meta->hint_valid |= XDP_META_FIELD_RSS;

	err = bpf_xdp_metadata_rx_vlan_tag(ctx, &meta->rx_vlan_proto,
					   &meta->rx_vlan_tci);
	if (err)
		meta->rx_vlan_tag_err = err;
	else
		meta->hint_valid |= XDP_META_FIELD_VLAN_TAG;

	__sync_add_and_fetch(&pkts_redir, 1);
	return bpf_redirect_map(&xsk, ctx->rx_queue_index, XDP_PASS);
}

char _license[] SEC("license") = "GPL";

Annotation

Implementation Notes