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 "xsk_xdp_metadata.h" 7 8 struct { 9 __uint(type, BPF_MAP_TYPE_XSKMAP); 10 __uint(max_entries, 1); 11 __uint(key_size, sizeof(int)); 12 __uint(value_size, sizeof(int)); 13 } xsk SEC(".maps"); 14 15 static unsigned int idx; 16 int count = 0; 17 18 SEC("xdp.frags") int xsk_def_prog(struct xdp_md *xdp) 19 { 20 return bpf_redirect_map(&xsk, 0, XDP_DROP); 21 } 22 23 SEC("xdp.frags") int xsk_xdp_drop(struct xdp_md *xdp) 24 { 25 /* Drop every other packet */ 26 if (idx++ % 2) 27 return XDP_DROP; 28 29 return bpf_redirect_map(&xsk, 0, XDP_DROP); 30 } 31 32 SEC("xdp.frags") int xsk_xdp_populate_metadata(struct xdp_md *xdp) 33 { 34 void *data, *data_meta; 35 struct xdp_info *meta; 36 int err; 37 38 /* Reserve enough for all custom metadata. */ 39 err = bpf_xdp_adjust_meta(xdp, -(int)sizeof(struct xdp_info)); 40 if (err) 41 return XDP_DROP; 42 43 data = (void *)(long)xdp->data; 44 data_meta = (void *)(long)xdp->data_meta; 45 46 if (data_meta + sizeof(struct xdp_info) > data) 47 return XDP_DROP; 48 49 meta = data_meta; 50 meta->count = count++; 51 52 return bpf_redirect_map(&xsk, 0, XDP_DROP); 53 } 54 55 char _license[] SEC("license") = "GPL"; 56