Search code examples
clinuxebpfbpfxdp-bpf

XDP alternate to bpf_get_current_pid


How do I fetch current process id in xdp

my program:

#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/udp.h>
#include <linux/sched.h>
#include <linux/if_packet.h>
#include <linux/if_vlan.h>
#include <uapi/linux/bpf.h>
#include <net/sock.h>
#include <bcc/proto.h>


int udpfilter(struct xdp_md *ctx) {

  bpf_trace_printk("got a packet\n");
  u32 cpu = bpf_get_smp_processor_id();
  //bpf_trace_printk("%s looking\n",cpu);
  u32 pid = bpf_get_current_pid_tgid();

  return XDP_DROP;
}

Is there any alternate function to fetch current pid in xdp, I was taking reference of this https://github.com/iovisor/bcc/blob/master/docs/kernel-versions.md#program-types


Solution

  • Reading your other questions, I'm guessing you're trying to retrieve the PID of the destination process for the packet. That is not possible at the XDP hook because that information is simply not computed by the kernel yet.