~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/progs/xdp_hw_metadata.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 #include <vmlinux.h>
  4 #include "xdp_metadata.h"
  5 #include <bpf/bpf_helpers.h>
  6 #include <bpf/bpf_endian.h>
  7 
  8 struct {
  9         __uint(type, BPF_MAP_TYPE_XSKMAP);
 10         __uint(max_entries, 256);
 11         __type(key, __u32);
 12         __type(value, __u32);
 13 } xsk SEC(".maps");
 14 
 15 __u64 pkts_skip = 0;
 16 __u64 pkts_fail = 0;
 17 __u64 pkts_redir = 0;
 18 
 19 extern int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx,
 20                                          __u64 *timestamp) __ksym;
 21 extern int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, __u32 *hash,
 22                                     enum xdp_rss_hash_type *rss_type) __ksym;
 23 extern int bpf_xdp_metadata_rx_vlan_tag(const struct xdp_md *ctx,
 24                                         __be16 *vlan_proto,
 25                                         __u16 *vlan_tci) __ksym;
 26 
 27 SEC("xdp.frags")
 28 int rx(struct xdp_md *ctx)
 29 {
 30         void *data, *data_meta, *data_end;
 31         struct ipv6hdr *ip6h = NULL;
 32         struct udphdr *udp = NULL;
 33         struct iphdr *iph = NULL;
 34         struct xdp_meta *meta;
 35         struct ethhdr *eth;
 36         int err;
 37 
 38         data = (void *)(long)ctx->data;
 39         data_end = (void *)(long)ctx->data_end;
 40         eth = data;
 41 
 42         if (eth + 1 < data_end && (eth->h_proto == bpf_htons(ETH_P_8021AD) ||
 43                                    eth->h_proto == bpf_htons(ETH_P_8021Q)))
 44                 eth = (void *)eth + sizeof(struct vlan_hdr);
 45 
 46         if (eth + 1 < data_end && eth->h_proto == bpf_htons(ETH_P_8021Q))
 47                 eth = (void *)eth + sizeof(struct vlan_hdr);
 48 
 49         if (eth + 1 < data_end) {
 50                 if (eth->h_proto == bpf_htons(ETH_P_IP)) {
 51                         iph = (void *)(eth + 1);
 52                         if (iph + 1 < data_end && iph->protocol == IPPROTO_UDP)
 53                                 udp = (void *)(iph + 1);
 54                 }
 55                 if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
 56                         ip6h = (void *)(eth + 1);
 57                         if (ip6h + 1 < data_end && ip6h->nexthdr == IPPROTO_UDP)
 58                                 udp = (void *)(ip6h + 1);
 59                 }
 60                 if (udp && udp + 1 > data_end)
 61                         udp = NULL;
 62         }
 63 
 64         if (!udp) {
 65                 __sync_add_and_fetch(&pkts_skip, 1);
 66                 return XDP_PASS;
 67         }
 68 
 69         /* Forwarding UDP:9091 to AF_XDP */
 70         if (udp->dest != bpf_htons(9091)) {
 71                 __sync_add_and_fetch(&pkts_skip, 1);
 72                 return XDP_PASS;
 73         }
 74 
 75         err = bpf_xdp_adjust_meta(ctx, -(int)sizeof(struct xdp_meta));
 76         if (err) {
 77                 __sync_add_and_fetch(&pkts_fail, 1);
 78                 return XDP_PASS;
 79         }
 80 
 81         data = (void *)(long)ctx->data;
 82         data_meta = (void *)(long)ctx->data_meta;
 83         meta = data_meta;
 84 
 85         if (meta + 1 > data) {
 86                 __sync_add_and_fetch(&pkts_fail, 1);
 87                 return XDP_PASS;
 88         }
 89 
 90         meta->hint_valid = 0;
 91 
 92         meta->xdp_timestamp = bpf_ktime_get_tai_ns();
 93         err = bpf_xdp_metadata_rx_timestamp(ctx, &meta->rx_timestamp);
 94         if (err)
 95                 meta->rx_timestamp_err = err;
 96         else
 97                 meta->hint_valid |= XDP_META_FIELD_TS;
 98 
 99         err = bpf_xdp_metadata_rx_hash(ctx, &meta->rx_hash,
100                                        &meta->rx_hash_type);
101         if (err)
102                 meta->rx_hash_err = err;
103         else
104                 meta->hint_valid |= XDP_META_FIELD_RSS;
105 
106         err = bpf_xdp_metadata_rx_vlan_tag(ctx, &meta->rx_vlan_proto,
107                                            &meta->rx_vlan_tci);
108         if (err)
109                 meta->rx_vlan_tag_err = err;
110         else
111                 meta->hint_valid |= XDP_META_FIELD_VLAN_TAG;
112 
113         __sync_add_and_fetch(&pkts_redir, 1);
114         return bpf_redirect_map(&xsk, ctx->rx_queue_index, XDP_PASS);
115 }
116 
117 char _license[] SEC("license") = "GPL";
118 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php