xref: /f-stack/dpdk/examples/bpf/t3.c (revision 2d9fd380)
14418919fSjohnjiang /* SPDX-License-Identifier: BSD-3-Clause
24418919fSjohnjiang  * Copyright(c) 2018 Intel Corporation
34418919fSjohnjiang  */
44418919fSjohnjiang 
54418919fSjohnjiang /*
64418919fSjohnjiang  * eBPF program sample.
74418919fSjohnjiang  * Accepts pointer to struct rte_mbuf as an input parameter.
84418919fSjohnjiang  * Dump the mbuf into stdout if it is an ARP packet (aka tcpdump 'arp').
94418919fSjohnjiang  *
104418919fSjohnjiang  * To compile on x86:
11*2d9fd380Sjfb8856606  * clang -O2 -U __GNUC__ -target bpf -Wno-int-to-void-pointer-cast -c t3.c
124418919fSjohnjiang  *
134418919fSjohnjiang  * To compile on ARM:
14*2d9fd380Sjfb8856606  * clang -O2 -I/usr/include/aarch64-linux-gnu -target bpf \
154418919fSjohnjiang  * -Wno-int-to-void-pointer-cast -c t3.c
16*2d9fd380Sjfb8856606  *
17*2d9fd380Sjfb8856606  * NOTE: if DPDK is not installed system-wide, add compiler flag with path
18*2d9fd380Sjfb8856606  * to DPDK rte_mbuf.h file to above commands,
19*2d9fd380Sjfb8856606  * e.g. "clang -I/path/to/dpdk/headers -O2 ..."
204418919fSjohnjiang  */
214418919fSjohnjiang 
224418919fSjohnjiang #include <stdint.h>
234418919fSjohnjiang #include <stddef.h>
244418919fSjohnjiang #include <stdio.h>
254418919fSjohnjiang #include <net/ethernet.h>
264418919fSjohnjiang #include <rte_config.h>
274418919fSjohnjiang #include <rte_mbuf_core.h>
284418919fSjohnjiang #include <arpa/inet.h>
294418919fSjohnjiang 
304418919fSjohnjiang extern void rte_pktmbuf_dump(FILE *, const struct rte_mbuf *, unsigned int);
314418919fSjohnjiang 
324418919fSjohnjiang uint64_t
entry(const void * pkt)334418919fSjohnjiang entry(const void *pkt)
344418919fSjohnjiang {
354418919fSjohnjiang 	const struct rte_mbuf *mb;
364418919fSjohnjiang 	const struct ether_header *eth;
374418919fSjohnjiang 
384418919fSjohnjiang 	mb = pkt;
394418919fSjohnjiang 	eth = rte_pktmbuf_mtod(mb, const struct ether_header *);
404418919fSjohnjiang 
414418919fSjohnjiang 	if (eth->ether_type == htons(ETHERTYPE_ARP))
424418919fSjohnjiang 		rte_pktmbuf_dump(stdout, mb, 64);
434418919fSjohnjiang 
444418919fSjohnjiang 	return 1;
454418919fSjohnjiang }
46