|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 |
| 2 | + * |
| 3 | + * modify it under the terms of version 2 of the GNU General Public |
| 4 | + * License as published by the Free Software Foundation. |
| 5 | + * |
| 6 | + * This program is distributed in the hope that it will be useful, but |
| 7 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 9 | + * General Public License for more details. |
| 10 | + */ |
| 11 | +#define KBUILD_MODNAME "foo" |
| 12 | +#include <uapi/linux/bpf.h> |
| 13 | +#include <linux/in.h> |
| 14 | +#include <linux/if_ether.h> |
| 15 | +#include <linux/ip.h> |
| 16 | +#include <linux/ipv6.h> |
| 17 | +#include <bpf/bpf_helpers.h> |
| 18 | + |
| 19 | +struct { |
| 20 | + __uint(type, BPF_MAP_TYPE_DEVMAP_HASH); |
| 21 | + __uint(key_size, sizeof(int)); |
| 22 | + __uint(value_size, sizeof(int)); |
| 23 | + __uint(max_entries, 32); |
| 24 | +} forward_map_general SEC(".maps"); |
| 25 | + |
| 26 | +struct { |
| 27 | + __uint(type, BPF_MAP_TYPE_DEVMAP_HASH); |
| 28 | + __uint(key_size, sizeof(int)); |
| 29 | + __uint(value_size, sizeof(struct bpf_devmap_val)); |
| 30 | + __uint(max_entries, 32); |
| 31 | +} forward_map_native SEC(".maps"); |
| 32 | + |
| 33 | +struct { |
| 34 | + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); |
| 35 | + __type(key, u32); |
| 36 | + __type(value, long); |
| 37 | + __uint(max_entries, 1); |
| 38 | +} rxcnt SEC(".maps"); |
| 39 | + |
| 40 | +/* map to stroe egress interfaces mac addresses, set the |
| 41 | + * max_entries to 1 and extend it in user sapce prog. |
| 42 | + */ |
| 43 | +struct { |
| 44 | + __uint(type, BPF_MAP_TYPE_ARRAY); |
| 45 | + __type(key, u32); |
| 46 | + __type(value, __be64); |
| 47 | + __uint(max_entries, 1); |
| 48 | +} mac_map SEC(".maps"); |
| 49 | + |
| 50 | +static int xdp_redirect_map(struct xdp_md *ctx, void *forward_map) |
| 51 | +{ |
| 52 | + long *value; |
| 53 | + u32 key = 0; |
| 54 | + |
| 55 | + /* count packet in global counter */ |
| 56 | + value = bpf_map_lookup_elem(&rxcnt, &key); |
| 57 | + if (value) |
| 58 | + *value += 1; |
| 59 | + |
| 60 | + return bpf_redirect_map_multi(forward_map, NULL, BPF_F_EXCLUDE_INGRESS); |
| 61 | +} |
| 62 | + |
| 63 | +SEC("xdp_redirect_general") |
| 64 | +int xdp_redirect_map_general(struct xdp_md *ctx) |
| 65 | +{ |
| 66 | + return xdp_redirect_map(ctx, &forward_map_general); |
| 67 | +} |
| 68 | + |
| 69 | +SEC("xdp_redirect_native") |
| 70 | +int xdp_redirect_map_native(struct xdp_md *ctx) |
| 71 | +{ |
| 72 | + return xdp_redirect_map(ctx, &forward_map_native); |
| 73 | +} |
| 74 | + |
| 75 | +SEC("xdp_devmap/map_prog") |
| 76 | +int xdp_devmap_prog(struct xdp_md *ctx) |
| 77 | +{ |
| 78 | + void *data_end = (void *)(long)ctx->data_end; |
| 79 | + void *data = (void *)(long)ctx->data; |
| 80 | + u32 key = ctx->egress_ifindex; |
| 81 | + struct ethhdr *eth = data; |
| 82 | + __be64 *mac; |
| 83 | + u64 nh_off; |
| 84 | + |
| 85 | + nh_off = sizeof(*eth); |
| 86 | + if (data + nh_off > data_end) |
| 87 | + return XDP_DROP; |
| 88 | + |
| 89 | + mac = bpf_map_lookup_elem(&mac_map, &key); |
| 90 | + if (mac) |
| 91 | + __builtin_memcpy(eth->h_source, mac, ETH_ALEN); |
| 92 | + |
| 93 | + return XDP_PASS; |
| 94 | +} |
| 95 | + |
| 96 | +char _license[] SEC("license") = "GPL"; |
0 commit comments