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

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

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ 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 /* Copyright (c) 2022 Intel */
  3 
  4 #include <linux/bpf.h>
  5 #include <bpf/bpf_helpers.h>
  6 #include <linux/if_ether.h>
  7 #include "xsk_xdp_common.h"
  8 
  9 struct {
 10         __uint(type, BPF_MAP_TYPE_XSKMAP);
 11         __uint(max_entries, 2);
 12         __uint(key_size, sizeof(int));
 13         __uint(value_size, sizeof(int));
 14 } xsk SEC(".maps");
 15 
 16 static unsigned int idx;
 17 int count = 0;
 18 
 19 SEC("xdp.frags") int xsk_def_prog(struct xdp_md *xdp)
 20 {
 21         return bpf_redirect_map(&xsk, 0, XDP_DROP);
 22 }
 23 
 24 SEC("xdp.frags") int xsk_xdp_drop(struct xdp_md *xdp)
 25 {
 26         /* Drop every other packet */
 27         if (idx++ % 2)
 28                 return XDP_DROP;
 29 
 30         return bpf_redirect_map(&xsk, 0, XDP_DROP);
 31 }
 32 
 33 SEC("xdp.frags") int xsk_xdp_populate_metadata(struct xdp_md *xdp)
 34 {
 35         void *data, *data_meta;
 36         struct xdp_info *meta;
 37         int err;
 38 
 39         /* Reserve enough for all custom metadata. */
 40         err = bpf_xdp_adjust_meta(xdp, -(int)sizeof(struct xdp_info));
 41         if (err)
 42                 return XDP_DROP;
 43 
 44         data = (void *)(long)xdp->data;
 45         data_meta = (void *)(long)xdp->data_meta;
 46 
 47         if (data_meta + sizeof(struct xdp_info) > data)
 48                 return XDP_DROP;
 49 
 50         meta = data_meta;
 51         meta->count = count++;
 52 
 53         return bpf_redirect_map(&xsk, 0, XDP_DROP);
 54 }
 55 
 56 SEC("xdp") int xsk_xdp_shared_umem(struct xdp_md *xdp)
 57 {
 58         void *data = (void *)(long)xdp->data;
 59         void *data_end = (void *)(long)xdp->data_end;
 60         struct ethhdr *eth = data;
 61 
 62         if (eth + 1 > data_end)
 63                 return XDP_DROP;
 64 
 65         /* Redirecting packets based on the destination MAC address */
 66         idx = ((unsigned int)(eth->h_dest[5])) / 2;
 67         if (idx > MAX_SOCKETS)
 68                 return XDP_DROP;
 69 
 70         return bpf_redirect_map(&xsk, idx, XDP_DROP);
 71 }
 72 
 73 char _license[] SEC("license") = "GPL";
 74 

~ [ 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