linux/tools/testing/selftests/bpf/progs/test_xdp_with_devmap_helpers.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2#include <linux/bpf.h>
   3#include <bpf/bpf_helpers.h>
   4
   5struct {
   6        __uint(type, BPF_MAP_TYPE_DEVMAP);
   7        __uint(key_size, sizeof(__u32));
   8        __uint(value_size, sizeof(struct bpf_devmap_val));
   9        __uint(max_entries, 4);
  10} dm_ports SEC(".maps");
  11
  12SEC("xdp_redir")
  13int xdp_redir_prog(struct xdp_md *ctx)
  14{
  15        return bpf_redirect_map(&dm_ports, 1, 0);
  16}
  17
  18/* invalid program on DEVMAP entry;
  19 * SEC name means expected attach type not set
  20 */
  21SEC("xdp_dummy")
  22int xdp_dummy_prog(struct xdp_md *ctx)
  23{
  24        return XDP_PASS;
  25}
  26
  27/* valid program on DEVMAP entry via SEC name;
  28 * has access to egress and ingress ifindex
  29 */
  30SEC("xdp_devmap/map_prog")
  31int xdp_dummy_dm(struct xdp_md *ctx)
  32{
  33        char fmt[] = "devmap redirect: dev %u -> dev %u len %u\n";
  34        void *data_end = (void *)(long)ctx->data_end;
  35        void *data = (void *)(long)ctx->data;
  36        unsigned int len = data_end - data;
  37
  38        bpf_trace_printk(fmt, sizeof(fmt),
  39                         ctx->ingress_ifindex, ctx->egress_ifindex, len);
  40
  41        return XDP_PASS;
  42}
  43char _license[] SEC("license") = "GPL";
  44