Search code examples
ebpfbpflibbpf

ebpf-tc: how to keep unique information inside a ebpf instance when same program is attached to multiple interface


When we pin a MAP, we can able to share information from userspace to ebpf but it is system wide. But if i want to share different value to different instance from tc ingress/egress (array map with size of 1)

  1. Is there any way to pass argument ?
  2. Map (unpinned unique per instance) - update from userspace
  3. Any other way to communicate from userspace to kernel (while attaching or after)

Really appreciate your help.


Solution

  • Pinning a map doesn't make it system wide. Every map is always accessible system wide, pinning just adds a reference to the file system to make it easier to find and to make sure a map isn't removed even when not in use by any program.

    Is there any way to pass argument ?

    No, once a program is loaded, only the kernel can pass arguments(contexts) to a program, userspace can only use maps to communicate with eBPF programs.

    Map (unpinned unique per instance) - update from userspace

    Any userspace program with the right permissions can update any map as long as you can obtain a file descriptor to the map. Map FDs can be obtained:

    • By creating a new map
    • By opening a map pin
    • By using its unique ID (directly or by looping over all loaded maps)
    • By IPC from another process that already has the FD

    Any other way to communicate from userspace to kernel (while attaching or after)

    Maps are it. You can rewrite the program before loading it into the kernel by setting constants at specific locations, but not at attach time. One way which might be interesting is that on newer kernels, global data is supported. Which allow you to change the value of variables defined in the global scope from userspace. In this case the global data is packed in a array map with a single key/value.