Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #define KBUILD_MODNAME "foo"
0003 
0004 #include "vmlinux.h"
0005 #include "xdp_sample.bpf.h"
0006 #include "xdp_sample_shared.h"
0007 
0008 struct {
0009     __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
0010     __uint(key_size, sizeof(int));
0011     __uint(value_size, sizeof(int));
0012     __uint(max_entries, 32);
0013 } forward_map_general SEC(".maps");
0014 
0015 struct {
0016     __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
0017     __uint(key_size, sizeof(int));
0018     __uint(value_size, sizeof(struct bpf_devmap_val));
0019     __uint(max_entries, 32);
0020 } forward_map_native SEC(".maps");
0021 
0022 /* map to store egress interfaces mac addresses */
0023 struct {
0024     __uint(type, BPF_MAP_TYPE_HASH);
0025     __type(key, u32);
0026     __type(value, __be64);
0027     __uint(max_entries, 32);
0028 } mac_map SEC(".maps");
0029 
0030 static int xdp_redirect_map(struct xdp_md *ctx, void *forward_map)
0031 {
0032     u32 key = bpf_get_smp_processor_id();
0033     struct datarec *rec;
0034 
0035     rec = bpf_map_lookup_elem(&rx_cnt, &key);
0036     if (!rec)
0037         return XDP_PASS;
0038     NO_TEAR_INC(rec->processed);
0039 
0040     return bpf_redirect_map(forward_map, 0,
0041                 BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
0042 }
0043 
0044 SEC("xdp")
0045 int xdp_redirect_map_general(struct xdp_md *ctx)
0046 {
0047     return xdp_redirect_map(ctx, &forward_map_general);
0048 }
0049 
0050 SEC("xdp")
0051 int xdp_redirect_map_native(struct xdp_md *ctx)
0052 {
0053     return xdp_redirect_map(ctx, &forward_map_native);
0054 }
0055 
0056 SEC("xdp/devmap")
0057 int xdp_devmap_prog(struct xdp_md *ctx)
0058 {
0059     void *data_end = (void *)(long)ctx->data_end;
0060     void *data = (void *)(long)ctx->data;
0061     u32 key = ctx->egress_ifindex;
0062     struct ethhdr *eth = data;
0063     __be64 *mac;
0064     u64 nh_off;
0065 
0066     nh_off = sizeof(*eth);
0067     if (data + nh_off > data_end)
0068         return XDP_DROP;
0069 
0070     mac = bpf_map_lookup_elem(&mac_map, &key);
0071     if (mac)
0072         __builtin_memcpy(eth->h_source, mac, ETH_ALEN);
0073 
0074     return XDP_PASS;
0075 }
0076 
0077 char _license[] SEC("license") = "GPL";