mirror of
https://github.com/xdp-project/bpf-examples.git
synced 2024-05-06 15:54:53 +00:00
85 lines
1.9 KiB
C
85 lines
1.9 KiB
C
![]() |
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||
|
#include <linux/bpf.h>
|
||
|
#include <bpf/bpf_helpers.h>
|
||
|
#include <xdp/parsing_helpers.h>
|
||
|
#include <linux/pkt_cls.h>
|
||
|
|
||
|
#include "pkt-loop-filter.h"
|
||
|
|
||
|
/* We use an LRU map to avoid having to do cleanup: We just rely on the LRU
|
||
|
* mechanism to evict old entries as the map fills up.
|
||
|
*/
|
||
|
struct {
|
||
|
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
||
|
__type(key, struct pkt_loop_key);
|
||
|
__type(value, struct pkt_loop_data);
|
||
|
__uint(max_entries, 16384);
|
||
|
} iface_state SEC(".maps");
|
||
|
|
||
|
static int parse_pkt(struct __sk_buff *skb, struct pkt_loop_key *key)
|
||
|
{
|
||
|
void *data_end = (void *)(unsigned long long)skb->data_end;
|
||
|
void *data = (void *)(unsigned long long)skb->data;
|
||
|
struct hdr_cursor nh = { .pos = data };
|
||
|
struct ethhdr *eth;
|
||
|
int eth_type;
|
||
|
|
||
|
/* Parse Ethernet and IP/IPv6 headers */
|
||
|
eth_type = parse_ethhdr(&nh, data_end, ð);
|
||
|
if (eth_type < 0)
|
||
|
return eth_type;
|
||
|
|
||
|
__builtin_memcpy(key->src_mac, eth->h_source, ETH_ALEN);
|
||
|
key->src_vlan = skb->vlan_tci;
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
SEC("tc")
|
||
|
int record_egress_pkt(struct __sk_buff *skb)
|
||
|
{
|
||
|
struct pkt_loop_data value = { .ifindex = skb->ifindex }, *v;
|
||
|
struct pkt_loop_key key;
|
||
|
int err;
|
||
|
|
||
|
err = parse_pkt(skb, &key);
|
||
|
if (err)
|
||
|
goto out;
|
||
|
|
||
|
v = bpf_map_lookup_elem(&iface_state, &key);
|
||
|
if (!v) {
|
||
|
bpf_map_update_elem(&iface_state, &key, &value, BPF_NOEXIST);
|
||
|
v = bpf_map_lookup_elem(&iface_state, &key);
|
||
|
if (!v)
|
||
|
goto out;
|
||
|
}
|
||
|
v->expiry_time = bpf_ktime_get_coarse_ns() + STATE_LIFETIME;
|
||
|
v->ifindex = skb->ifindex;
|
||
|
out:
|
||
|
return TC_ACT_OK;
|
||
|
}
|
||
|
|
||
|
SEC("tc")
|
||
|
int filter_ingress_pkt(struct __sk_buff *skb)
|
||
|
{
|
||
|
struct pkt_loop_data *value;
|
||
|
struct pkt_loop_key key;
|
||
|
int err;
|
||
|
|
||
|
err = parse_pkt(skb, &key);
|
||
|
if (err)
|
||
|
goto out;
|
||
|
|
||
|
value = bpf_map_lookup_elem(&iface_state, &key);
|
||
|
if (value && value->expiry_time > bpf_ktime_get_coarse_ns()) {
|
||
|
value->drops++;
|
||
|
return TC_ACT_SHOT;
|
||
|
|
||
|
}
|
||
|
|
||
|
out:
|
||
|
return TC_ACT_OK;
|
||
|
}
|
||
|
|
||
|
char _license[] SEC("license") = "GPL";
|