xref: /linux/tools/testing/selftests/bpf/progs/xdp_metadata.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1e2a46d54SStanislav Fomichev // SPDX-License-Identifier: GPL-2.0
2e2a46d54SStanislav Fomichev 
3e2a46d54SStanislav Fomichev #include <vmlinux.h>
4e2a46d54SStanislav Fomichev #include "xdp_metadata.h"
5e2a46d54SStanislav Fomichev #include <bpf/bpf_helpers.h>
6e2a46d54SStanislav Fomichev #include <bpf/bpf_endian.h>
7e2a46d54SStanislav Fomichev 
8e2a46d54SStanislav Fomichev struct {
9e2a46d54SStanislav Fomichev 	__uint(type, BPF_MAP_TYPE_XSKMAP);
10e2a46d54SStanislav Fomichev 	__uint(max_entries, 4);
11e2a46d54SStanislav Fomichev 	__type(key, __u32);
12e2a46d54SStanislav Fomichev 	__type(value, __u32);
13e2a46d54SStanislav Fomichev } xsk SEC(".maps");
14e2a46d54SStanislav Fomichev 
15e2a46d54SStanislav Fomichev struct {
16e2a46d54SStanislav Fomichev 	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
17e2a46d54SStanislav Fomichev 	__uint(max_entries, 1);
18e2a46d54SStanislav Fomichev 	__type(key, __u32);
19e2a46d54SStanislav Fomichev 	__type(value, __u32);
20e2a46d54SStanislav Fomichev } prog_arr SEC(".maps");
21e2a46d54SStanislav Fomichev 
22e2a46d54SStanislav Fomichev extern int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx,
23e2a46d54SStanislav Fomichev 					 __u64 *timestamp) __ksym;
240f26b74eSJesper Dangaard Brouer extern int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, __u32 *hash,
250f26b74eSJesper Dangaard Brouer 				    enum xdp_rss_hash_type *rss_type) __ksym;
26*4c6612f6SLarysa Zaremba extern int bpf_xdp_metadata_rx_vlan_tag(const struct xdp_md *ctx,
27*4c6612f6SLarysa Zaremba 					__be16 *vlan_proto,
28*4c6612f6SLarysa Zaremba 					__u16 *vlan_tci) __ksym;
29e2a46d54SStanislav Fomichev 
30e2a46d54SStanislav Fomichev SEC("xdp")
rx(struct xdp_md * ctx)31e2a46d54SStanislav Fomichev int rx(struct xdp_md *ctx)
32e2a46d54SStanislav Fomichev {
335ffb260fSStanislav Fomichev 	void *data, *data_meta, *data_end;
345ffb260fSStanislav Fomichev 	struct ipv6hdr *ip6h = NULL;
355ffb260fSStanislav Fomichev 	struct ethhdr *eth = NULL;
365ffb260fSStanislav Fomichev 	struct udphdr *udp = NULL;
375ffb260fSStanislav Fomichev 	struct iphdr *iph = NULL;
38e2a46d54SStanislav Fomichev 	struct xdp_meta *meta;
39e2a46d54SStanislav Fomichev 	u64 timestamp = -1;
40e2a46d54SStanislav Fomichev 	int ret;
41e2a46d54SStanislav Fomichev 
425ffb260fSStanislav Fomichev 	data = (void *)(long)ctx->data;
435ffb260fSStanislav Fomichev 	data_end = (void *)(long)ctx->data_end;
445ffb260fSStanislav Fomichev 	eth = data;
455ffb260fSStanislav Fomichev 	if (eth + 1 < data_end) {
465ffb260fSStanislav Fomichev 		if (eth->h_proto == bpf_htons(ETH_P_IP)) {
475ffb260fSStanislav Fomichev 			iph = (void *)(eth + 1);
485ffb260fSStanislav Fomichev 			if (iph + 1 < data_end && iph->protocol == IPPROTO_UDP)
495ffb260fSStanislav Fomichev 				udp = (void *)(iph + 1);
505ffb260fSStanislav Fomichev 		}
515ffb260fSStanislav Fomichev 		if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
525ffb260fSStanislav Fomichev 			ip6h = (void *)(eth + 1);
535ffb260fSStanislav Fomichev 			if (ip6h + 1 < data_end && ip6h->nexthdr == IPPROTO_UDP)
545ffb260fSStanislav Fomichev 				udp = (void *)(ip6h + 1);
555ffb260fSStanislav Fomichev 		}
565ffb260fSStanislav Fomichev 		if (udp && udp + 1 > data_end)
575ffb260fSStanislav Fomichev 			udp = NULL;
585ffb260fSStanislav Fomichev 	}
595ffb260fSStanislav Fomichev 
605ffb260fSStanislav Fomichev 	if (!udp)
615ffb260fSStanislav Fomichev 		return XDP_PASS;
625ffb260fSStanislav Fomichev 
635ffb260fSStanislav Fomichev 	/* Forwarding UDP:8080 to AF_XDP */
645ffb260fSStanislav Fomichev 	if (udp->dest != bpf_htons(8080))
655ffb260fSStanislav Fomichev 		return XDP_PASS;
665ffb260fSStanislav Fomichev 
67e2a46d54SStanislav Fomichev 	/* Reserve enough for all custom metadata. */
68e2a46d54SStanislav Fomichev 
69e2a46d54SStanislav Fomichev 	ret = bpf_xdp_adjust_meta(ctx, -(int)sizeof(struct xdp_meta));
70e2a46d54SStanislav Fomichev 	if (ret != 0)
71e2a46d54SStanislav Fomichev 		return XDP_DROP;
72e2a46d54SStanislav Fomichev 
73e2a46d54SStanislav Fomichev 	data = (void *)(long)ctx->data;
74e2a46d54SStanislav Fomichev 	data_meta = (void *)(long)ctx->data_meta;
75e2a46d54SStanislav Fomichev 
76e2a46d54SStanislav Fomichev 	if (data_meta + sizeof(struct xdp_meta) > data)
77e2a46d54SStanislav Fomichev 		return XDP_DROP;
78e2a46d54SStanislav Fomichev 
79e2a46d54SStanislav Fomichev 	meta = data_meta;
80e2a46d54SStanislav Fomichev 
81e2a46d54SStanislav Fomichev 	/* Export metadata. */
82e2a46d54SStanislav Fomichev 
83e2a46d54SStanislav Fomichev 	/* We expect veth bpf_xdp_metadata_rx_timestamp to return 0 HW
84e2a46d54SStanislav Fomichev 	 * timestamp, so put some non-zero value into AF_XDP frame for
85e2a46d54SStanislav Fomichev 	 * the userspace.
86e2a46d54SStanislav Fomichev 	 */
87e2a46d54SStanislav Fomichev 	bpf_xdp_metadata_rx_timestamp(ctx, &timestamp);
88e2a46d54SStanislav Fomichev 	if (timestamp == 0)
89e2a46d54SStanislav Fomichev 		meta->rx_timestamp = 1;
90e2a46d54SStanislav Fomichev 
910f26b74eSJesper Dangaard Brouer 	bpf_xdp_metadata_rx_hash(ctx, &meta->rx_hash, &meta->rx_hash_type);
92*4c6612f6SLarysa Zaremba 	bpf_xdp_metadata_rx_vlan_tag(ctx, &meta->rx_vlan_proto,
93*4c6612f6SLarysa Zaremba 				     &meta->rx_vlan_tci);
94e2a46d54SStanislav Fomichev 
95e2a46d54SStanislav Fomichev 	return bpf_redirect_map(&xsk, ctx->rx_queue_index, XDP_PASS);
96e2a46d54SStanislav Fomichev }
97e2a46d54SStanislav Fomichev 
98e2a46d54SStanislav Fomichev char _license[] SEC("license") = "GPL";
99