tools/testing/selftests/net/lib/xdp_metadata.bpf.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/lib/xdp_metadata.bpf.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/net/lib/xdp_metadata.bpf.c
Extension
.c
Size
3751 bytes
Lines
164
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

// SPDX-License-Identifier: GPL-2.0

#include <stddef.h>
#include <linux/bpf.h>
#include <linux/in.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/udp.h>
#include <linux/tcp.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>

enum {
	XDP_PORT = 1,
	XDP_PROTO = 4,
} xdp_map_setup_keys;

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 5);
	__type(key, __u32);
	__type(value, __s32);
} map_xdp_setup SEC(".maps");

/* RSS hash results: key 0 = hash, key 1 = hash type,
 * key 2 = packet count, key 3 = error count.
 */
enum {
	RSS_KEY_HASH = 0,
	RSS_KEY_TYPE = 1,
	RSS_KEY_PKT_CNT = 2,
	RSS_KEY_ERR_CNT = 3,
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__type(key, __u32);
	__type(value, __u32);
	__uint(max_entries, 4);
} map_rss SEC(".maps");

/* Mirror of enum xdp_rss_hash_type from include/net/xdp.h.
 * Needed because the enum is not part of UAPI headers.
 */
enum xdp_rss_hash_type {
	XDP_RSS_L3_IPV4 = 1U << 0,
	XDP_RSS_L3_IPV6 = 1U << 1,
	XDP_RSS_L3_DYNHDR = 1U << 2,
	XDP_RSS_L4 = 1U << 3,
	XDP_RSS_L4_TCP = 1U << 4,
	XDP_RSS_L4_UDP = 1U << 5,
	XDP_RSS_L4_SCTP = 1U << 6,
	XDP_RSS_L4_IPSEC = 1U << 7,
	XDP_RSS_L4_ICMP = 1U << 8,
};

extern int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, __u32 *hash,
				    enum xdp_rss_hash_type *rss_type) __ksym;

static __always_inline __u16 get_dest_port(void *l4, void *data_end,
					   __u8 protocol)
{
	if (protocol == IPPROTO_UDP) {
		struct udphdr *udp = l4;

		if ((void *)(udp + 1) > data_end)
			return 0;
		return udp->dest;
	} else if (protocol == IPPROTO_TCP) {
		struct tcphdr *tcp = l4;

		if ((void *)(tcp + 1) > data_end)
			return 0;
		return tcp->dest;
	}

	return 0;
}

SEC("xdp")
int xdp_rss_hash(struct xdp_md *ctx)
{
	void *data_end = (void *)(long)ctx->data_end;
	void *data = (void *)(long)ctx->data;
	enum xdp_rss_hash_type rss_type = 0;
	struct ethhdr *eth = data;
	__u8 l4_proto = 0;
	__u32 hash = 0;
	__u32 key, val;

Annotation

Implementation Notes