1

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

pchaigno
  • 11,313
  • 2
  • 29
  • 54
zexapod
  • 45
  • 6
  • Reading your other questions, I'm guessing you're trying to retrieve the PID of the destination process for the packet? – pchaigno Nov 29 '21 at 09:55

1 Answers1

2

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.

pchaigno
  • 11,313
  • 2
  • 29
  • 54