mirror of
				https://github.com/xdp-project/bpf-examples.git
				synced 2024-05-06 15:54:53 +00:00 
			
		
		
		
	AF_XDP-interaction: Import AF_XDP example from XDP-tutorial
https://github.com/xdp-project/xdp-tutorial/blob/master/advanced03-AF_XDP/ Give Eelco Chaudron credit as code Author. Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
This commit is contained in:
		
				
					committed by
					
						
						Jesper Dangaard Brouer
					
				
			
			
				
	
			
			
			
						parent
						
							3321c338d5
						
					
				
				
					commit
					68743eb1d3
				
			
							
								
								
									
										43
									
								
								AF_XDP-interaction/af_xdp_kern.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								AF_XDP-interaction/af_xdp_kern.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
/* SPDX-License-Identifier: GPL-2.0 */
 | 
			
		||||
 | 
			
		||||
#include <linux/bpf.h>
 | 
			
		||||
 | 
			
		||||
#include <bpf/bpf_helpers.h>
 | 
			
		||||
 | 
			
		||||
struct bpf_map_def SEC("maps") xsks_map = {
 | 
			
		||||
	.type = BPF_MAP_TYPE_XSKMAP,
 | 
			
		||||
	.key_size = sizeof(int),
 | 
			
		||||
	.value_size = sizeof(int),
 | 
			
		||||
	.max_entries = 64,  /* Assume netdev has no more than 64 queues */
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct bpf_map_def SEC("maps") xdp_stats_map = {
 | 
			
		||||
	.type        = BPF_MAP_TYPE_PERCPU_ARRAY,
 | 
			
		||||
	.key_size    = sizeof(int),
 | 
			
		||||
	.value_size  = sizeof(__u32),
 | 
			
		||||
	.max_entries = 64,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
SEC("xdp_sock")
 | 
			
		||||
int xdp_sock_prog(struct xdp_md *ctx)
 | 
			
		||||
{
 | 
			
		||||
	int index = ctx->rx_queue_index;
 | 
			
		||||
	__u32 *pkt_count;
 | 
			
		||||
 | 
			
		||||
	pkt_count = bpf_map_lookup_elem(&xdp_stats_map, &index);
 | 
			
		||||
	if (pkt_count) {
 | 
			
		||||
 | 
			
		||||
		/* We pass every other packet */
 | 
			
		||||
		if ((*pkt_count)++ & 1)
 | 
			
		||||
			return XDP_PASS;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* A set entry here means that the correspnding queue_id
 | 
			
		||||
	 * has an active AF_XDP socket bound to it. */
 | 
			
		||||
	if (bpf_map_lookup_elem(&xsks_map, &index))
 | 
			
		||||
		return bpf_redirect_map(&xsks_map, index, 0);
 | 
			
		||||
 | 
			
		||||
	return XDP_PASS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char _license[] SEC("license") = "GPL";
 | 
			
		||||
		Reference in New Issue
	
	Block a user