2 * SPDX-License-Identifier: GPL-2.0 OR Apache-2.0
3 * Dual-licensed under GPL version 2.0 or Apache License version 2.0
4 * Copyright (c) 2020 Cisco and/or its affiliates.
7 #include <bpf/bpf_helpers.h>
8 #include <xdp/xdp_helpers.h>
10 #include <linux/if_ether.h>
12 #include <linux/udp.h>
14 #define XDP_METADATA_SECTION "xdp_metadata"
15 #define XSK_PROG_VERSION 1
18 * when compiled, debug print can be viewed with eg.
19 * sudo cat /sys/kernel/debug/tracing/trace_pipe
24 #define x_(fmt) __FILE__ ":" s_(__LINE__) ": " fmt "\n"
25 #define DEBUG_PRINT_(fmt, ...) do { \
26 const char fmt__[] = fmt; \
27 bpf_trace_printk(fmt__, sizeof(fmt), ## __VA_ARGS__); } while(0)
28 #define DEBUG_PRINT(fmt, ...) DEBUG_PRINT_ (x_(fmt), ## __VA_ARGS__)
30 #define DEBUG_PRINT(fmt, ...)
33 #define ntohs(x) __constant_ntohs (x)
35 #define DEFAULT_QUEUE_IDS 64
39 __uint (type, BPF_MAP_TYPE_XSKMAP);
40 __uint (key_size, sizeof (int));
41 __uint (value_size, sizeof (int));
42 __uint (max_entries, DEFAULT_QUEUE_IDS);
43 } xsks_map SEC (".maps");
47 __uint (priority, 10);
49 } XDP_RUN_CONFIG (xdp_sock_prog);
53 xdp_sock_prog (struct xdp_md *ctx)
55 const void *data = (void *) (long) ctx->data;
56 const void *data_end = (void *) (long) ctx->data_end;
58 DEBUG_PRINT ("rx %ld bytes packet", (long) data_end - (long) data);
60 /* smallest packet we are interesting in is ip-ip */
61 if (data + sizeof (struct ethhdr) + 2 * sizeof (struct iphdr) > data_end)
63 DEBUG_PRINT ("packet too small");
67 const struct ethhdr *eth = data;
68 if (eth->h_proto != ntohs (ETH_P_IP))
70 DEBUG_PRINT ("unsupported eth proto %x", (int) eth->h_proto);
74 const struct iphdr *ip = (void *) (eth + 1);
79 const struct udphdr *udp = (void *) (ip + 1);
80 if (udp->dest != ntohs (4789)) /* VxLAN dest port */
82 DEBUG_PRINT ("unsupported udp dst port %x", (int) udp->dest);
90 DEBUG_PRINT ("unsupported ip proto %x", (int) ip->protocol);
94 return bpf_redirect_map (&xsks_map, ctx->rx_queue_index, XDP_PASS);
97 /* actually Dual GPLv2/Apache2, but GPLv2 as far as kernel is concerned */
98 char _license[] SEC ("license") = "GPL";
99 __uint (xsk_prog_version, XSK_PROG_VERSION) SEC (XDP_METADATA_SECTION);