gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/xdp/cmd/bpf/tcpdump.ebpf.c (about) 1 // Copyright 2022 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // clang-format off 16 // Contains types needed by later headers. 17 #include <linux/types.h> 18 // clang-format on 19 #include <bpf/bpf_helpers.h> 20 #include <linux/bpf.h> 21 22 #define section(secname) __attribute__((section(secname), used)) 23 24 char __license[] section("license") = "Apache-2.0"; 25 26 // Note: bpf_helpers.h includes a struct definition for bpf_map_def in some, but 27 // not all, environments. Define our own equivalent struct to avoid issues with 28 // multiple declarations. 29 struct gvisor_bpf_map_def { 30 unsigned int type; 31 unsigned int key_size; 32 unsigned int value_size; 33 unsigned int max_entries; 34 unsigned int map_flags; 35 }; 36 37 // A map of RX queue number to AF_XDP socket. We only ever use one key: 0. 38 struct gvisor_bpf_map_def section("maps") sock_map = { 39 .type = BPF_MAP_TYPE_XSKMAP, // Note: "XSK" means AF_XDP socket. 40 .key_size = sizeof(int), 41 .value_size = sizeof(int), 42 .max_entries = 1, 43 }; 44 45 section("xdp") int xdp_prog(struct xdp_md *ctx) { 46 // Lookup the socket for the current RX queue. Veth devices by default have 47 // only one RX queue. If one is found, redirect the packet to that socket. 48 // Otherwise pass it on to the kernel network stack. 49 return bpf_redirect_map(&sock_map, ctx->rx_queue_index, XDP_PASS); 50 }