forked from xdp-project/xdp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdp_prog_kern.c
42 lines (35 loc) · 920 Bytes
/
xdp_prog_kern.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/if_vlan.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <stdbool.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
/* to u64 in host order */
static inline __u64 ether_addr_to_u64(const __u8 *addr)
{
__u64 u = 0;
int i;
for (i = ETH_ALEN - 1; i >= 0; i--)
u = u << 8 | addr[i];
return u;
}
SEC("xdp")
int xdp_prog_simple(struct xdp_md *ctx)
{
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
__u64 offset = sizeof(*eth);
if ((void *)eth + offset > data_end)
return 0;
bpf_printk("src: %llu, dst: %llu, proto: %u\n",
ether_addr_to_u64(eth->h_source),
ether_addr_to_u64(eth->h_dest),
bpf_ntohs(eth->h_proto));
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";