Repository Structure with ebpf maps

Table of Contents


The main function in the program is xdp_prog_func, which is the actual XDP hook function.

  • This function is executed whenever a packet passes through the XDP hook.

  • The function first retrieves the data record associated with the XDP_PASS action from the xdp_stats_map using the bpf_map_lookup_elem() function.

  • If the lookup is successful, the function increments the packet counter associated with the XDP_PASS action using an atomic add operation (lock_xadd()).



The common_kern_user.h header file is used by both the kernel-side BPF programs and userspace programs to share common structures and definitions.

struct datarec

In this specific case, the struct datarec is defined in common_kern_user.h as a data record that will be stored in a BPF map.

  • It has a single field rx_packets of type __u64, which is an unsigned 64-bit integer that represents the number of received packets.



The XDP_ACTION_MAX is also defined in common_kern_user.h and represents the maximum number of actions that can be performed by an XDP (eXpress Data Path) program.

  • It is defined as XDP_REDIRECT + 1, where XDP_REDIRECT .
  • XDP_REDIRECT is a predefined constant that represents the maximum value of the enum xdp_action enumeration, which is an enum used to define different actions that can be taken by an XDP (eXpress Data Path) program in the Linux kernel.
enum xdp_action {
  • In the provided code, the value of XDP_REDIRECT is used as the maximum number of entries in the xdp_stats_map BPF array map, which is used to store statistics for each possible XDP action.
  • By setting XDP_REDIRECT + 1 as the maximum number of entries, the xdp_stats_map array map will have enough space to store statistics for all possible XDP actions, including XDP_REDIRECT.
  • Therefore, the value of XDP_REDIRECT is used to determine the size of the array map and ensure that it has enough entries to accommodate all possible actions.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top