xref: /dpdk/examples/bpf/t3.c (revision 25d11a86c56d50947af33d0b79ede622809bd8b9)
1*25d11a86SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*25d11a86SBruce Richardson  * Copyright(c) 2018 Intel Corporation
3*25d11a86SBruce Richardson  */
4*25d11a86SBruce Richardson 
5*25d11a86SBruce Richardson /*
6*25d11a86SBruce Richardson  * eBPF program sample.
7*25d11a86SBruce Richardson  * Accepts pointer to struct rte_mbuf as an input parameter.
8*25d11a86SBruce Richardson  * Dump the mbuf into stdout if it is an ARP packet (aka tcpdump 'arp').
9*25d11a86SBruce Richardson  *
10*25d11a86SBruce Richardson  * To compile on x86:
11*25d11a86SBruce Richardson  * clang -O2 -U __GNUC__ -I${RTE_SDK}/${RTE_TARGET}/include \
12*25d11a86SBruce Richardson  * -target bpf -Wno-int-to-void-pointer-cast -c t3.c
13*25d11a86SBruce Richardson  *
14*25d11a86SBruce Richardson  * To compile on ARM:
15*25d11a86SBruce Richardson  * clang -O2 -I/usr/include/aarch64-linux-gnu \
16*25d11a86SBruce Richardson  * -I${RTE_SDK}/${RTE_TARGET}/include -target bpf \
17*25d11a86SBruce Richardson  * -Wno-int-to-void-pointer-cast -c t3.c
18*25d11a86SBruce Richardson  */
19*25d11a86SBruce Richardson 
20*25d11a86SBruce Richardson #include <stdint.h>
21*25d11a86SBruce Richardson #include <stddef.h>
22*25d11a86SBruce Richardson #include <stdio.h>
23*25d11a86SBruce Richardson #include <net/ethernet.h>
24*25d11a86SBruce Richardson #include <rte_config.h>
25*25d11a86SBruce Richardson #include "mbuf.h"
26*25d11a86SBruce Richardson #include <arpa/inet.h>
27*25d11a86SBruce Richardson 
28*25d11a86SBruce Richardson extern void rte_pktmbuf_dump(FILE *, const struct rte_mbuf *, unsigned int);
29*25d11a86SBruce Richardson 
30*25d11a86SBruce Richardson uint64_t
31*25d11a86SBruce Richardson entry(const void *pkt)
32*25d11a86SBruce Richardson {
33*25d11a86SBruce Richardson 	const struct rte_mbuf *mb;
34*25d11a86SBruce Richardson 	const struct ether_header *eth;
35*25d11a86SBruce Richardson 
36*25d11a86SBruce Richardson 	mb = pkt;
37*25d11a86SBruce Richardson 	eth = rte_pktmbuf_mtod(mb, const struct ether_header *);
38*25d11a86SBruce Richardson 
39*25d11a86SBruce Richardson 	if (eth->ether_type == htons(ETHERTYPE_ARP))
40*25d11a86SBruce Richardson 		rte_pktmbuf_dump(stdout, mb, 64);
41*25d11a86SBruce Richardson 
42*25d11a86SBruce Richardson 	return 1;
43*25d11a86SBruce Richardson }
44