diff options
Diffstat (limited to 'src/spdk/dpdk/examples/bpf')
-rw-r--r-- | src/spdk/dpdk/examples/bpf/README | 8 | ||||
-rw-r--r-- | src/spdk/dpdk/examples/bpf/dummy.c | 20 | ||||
-rw-r--r-- | src/spdk/dpdk/examples/bpf/t1.c | 56 | ||||
-rw-r--r-- | src/spdk/dpdk/examples/bpf/t2.c | 31 | ||||
-rw-r--r-- | src/spdk/dpdk/examples/bpf/t3.c | 43 |
5 files changed, 158 insertions, 0 deletions
diff --git a/src/spdk/dpdk/examples/bpf/README b/src/spdk/dpdk/examples/bpf/README new file mode 100644 index 000000000..d714180a5 --- /dev/null +++ b/src/spdk/dpdk/examples/bpf/README @@ -0,0 +1,8 @@ +This folder contains example BPF programs for use with the DPDK bpf +library. To get details of each program and how to compile it, see +the header on the '.c' file itself. + +Once compiled, these example programs can be loaded into `testpmd` +for execution on a packet stream. See `bpf-load` and `bpf-unload` +commands documented in the `Testpmd Application User Guide` for +details on how to do so. diff --git a/src/spdk/dpdk/examples/bpf/dummy.c b/src/spdk/dpdk/examples/bpf/dummy.c new file mode 100644 index 000000000..5851469e7 --- /dev/null +++ b/src/spdk/dpdk/examples/bpf/dummy.c @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2018 Intel Corporation + */ + +/* + * eBPF program sample. + * does nothing always return success. + * used to measure BPF infrastructure overhead. + * To compile: + * clang -O2 -target bpf -c dummy.c + */ + +#include <stdint.h> +#include <stddef.h> + +uint64_t +entry(void *arg) +{ + return 1; +} diff --git a/src/spdk/dpdk/examples/bpf/t1.c b/src/spdk/dpdk/examples/bpf/t1.c new file mode 100644 index 000000000..3364b4f1e --- /dev/null +++ b/src/spdk/dpdk/examples/bpf/t1.c @@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2018 Intel Corporation + */ + +/* + * eBPF program sample. + * Accepts pointer to first segment packet data as an input parameter. + * analog of tcpdump -s 1 -d 'dst 1.2.3.4 && udp && dst port 5000' + * (000) ldh [12] + * (001) jeq #0x800 jt 2 jf 12 + * (002) ld [30] + * (003) jeq #0x1020304 jt 4 jf 12 + * (004) ldb [23] + * (005) jeq #0x11 jt 6 jf 12 + * (006) ldh [20] + * (007) jset #0x1fff jt 12 jf 8 + * (008) ldxb 4*([14]&0xf) + * (009) ldh [x + 16] + * (010) jeq #0x1388 jt 11 jf 12 + * (011) ret #1 + * (012) ret #0 + * + * To compile on x86: + * clang -O2 -U __GNUC__ -target bpf -c t1.c + * + * To compile on ARM: + * clang -O2 -I/usr/include/aarch64-linux-gnu/ -target bpf -c t1.c + */ + +#include <stdint.h> +#include <net/ethernet.h> +#include <netinet/ip.h> +#include <netinet/udp.h> +#include <arpa/inet.h> + +uint64_t +entry(void *pkt) +{ + struct ether_header *ether_header = (void *)pkt; + + if (ether_header->ether_type != htons(0x0800)) + return 0; + + struct iphdr *iphdr = (void *)(ether_header + 1); + if (iphdr->protocol != 17 || (iphdr->frag_off & 0x1ffff) != 0 || + iphdr->daddr != htonl(0x1020304)) + return 0; + + int hlen = iphdr->ihl * 4; + struct udphdr *udphdr = (void *)iphdr + hlen; + + if (udphdr->dest != htons(5000)) + return 0; + + return 1; +} diff --git a/src/spdk/dpdk/examples/bpf/t2.c b/src/spdk/dpdk/examples/bpf/t2.c new file mode 100644 index 000000000..9878eaf7b --- /dev/null +++ b/src/spdk/dpdk/examples/bpf/t2.c @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2018 Intel Corporation + */ + +/* + * eBPF program sample. + * Accepts pointer to struct rte_mbuf as an input parameter. + * cleanup mbuf's vlan_tci and all related RX flags + * (PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED). + * Doesn't touch contents of packet data. + * To compile: + * clang -O2 -I${RTE_SDK}/${RTE_TARGET}/include \ + * -target bpf -Wno-int-to-void-pointer-cast -c t2.c + */ + +#include <stdint.h> +#include <stddef.h> +#include <rte_config.h> +#include <rte_mbuf_core.h> + +uint64_t +entry(void *pkt) +{ + struct rte_mbuf *mb; + + mb = pkt; + mb->vlan_tci = 0; + mb->ol_flags &= ~(PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED); + + return 1; +} diff --git a/src/spdk/dpdk/examples/bpf/t3.c b/src/spdk/dpdk/examples/bpf/t3.c new file mode 100644 index 000000000..f58ff64b3 --- /dev/null +++ b/src/spdk/dpdk/examples/bpf/t3.c @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2018 Intel Corporation + */ + +/* + * eBPF program sample. + * Accepts pointer to struct rte_mbuf as an input parameter. + * Dump the mbuf into stdout if it is an ARP packet (aka tcpdump 'arp'). + * + * To compile on x86: + * clang -O2 -U __GNUC__ -I${RTE_SDK}/${RTE_TARGET}/include \ + * -target bpf -Wno-int-to-void-pointer-cast -c t3.c + * + * To compile on ARM: + * clang -O2 -I/usr/include/aarch64-linux-gnu \ + * -I${RTE_SDK}/${RTE_TARGET}/include -target bpf \ + * -Wno-int-to-void-pointer-cast -c t3.c + */ + +#include <stdint.h> +#include <stddef.h> +#include <stdio.h> +#include <net/ethernet.h> +#include <rte_config.h> +#include <rte_mbuf_core.h> +#include <arpa/inet.h> + +extern void rte_pktmbuf_dump(FILE *, const struct rte_mbuf *, unsigned int); + +uint64_t +entry(const void *pkt) +{ + const struct rte_mbuf *mb; + const struct ether_header *eth; + + mb = pkt; + eth = rte_pktmbuf_mtod(mb, const struct ether_header *); + + if (eth->ether_type == htons(ETHERTYPE_ARP)) + rte_pktmbuf_dump(stdout, mb, 64); + + return 1; +} |