bpf: allow self-xor operation
[dpdk.git] / app / test-pmd / flowgen.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2014-2020 Mellanox Technologies, Ltd
3  */
4
5 #include <stdarg.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <stdint.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12
13 #include <sys/queue.h>
14 #include <sys/stat.h>
15
16 #include <rte_common.h>
17 #include <rte_byteorder.h>
18 #include <rte_log.h>
19 #include <rte_debug.h>
20 #include <rte_cycles.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
23 #include <rte_launch.h>
24 #include <rte_eal.h>
25 #include <rte_per_lcore.h>
26 #include <rte_lcore.h>
27 #include <rte_atomic.h>
28 #include <rte_branch_prediction.h>
29 #include <rte_mempool.h>
30 #include <rte_mbuf.h>
31 #include <rte_interrupts.h>
32 #include <rte_pci.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
35 #include <rte_ip.h>
36 #include <rte_tcp.h>
37 #include <rte_udp.h>
38 #include <rte_string_fns.h>
39 #include <rte_flow.h>
40
41 #include "testpmd.h"
42
43 static uint32_t cfg_ip_src      = RTE_IPV4(10, 254, 0, 0);
44 static uint32_t cfg_ip_dst      = RTE_IPV4(10, 253, 0, 0);
45 static uint16_t cfg_udp_src     = 1000;
46 static uint16_t cfg_udp_dst     = 1001;
47 static struct rte_ether_addr cfg_ether_src =
48         {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x00 }};
49 static struct rte_ether_addr cfg_ether_dst =
50         {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x01 }};
51
52 #define IP_DEFTTL  64   /* from RFC 1340. */
53
54 RTE_DEFINE_PER_LCORE(int, _next_flow);
55
56 /*
57  * Multi-flow generation mode.
58  *
59  * We originate a bunch of flows (varying destination IP addresses), and
60  * terminate receive traffic.  Received traffic is simply discarded, but we
61  * still do so in order to maintain traffic statistics.
62  */
63 static void
64 pkt_burst_flow_gen(struct fwd_stream *fs)
65 {
66         unsigned pkt_size = tx_pkt_length - 4;  /* Adjust FCS */
67         struct rte_mbuf  *pkts_burst[MAX_PKT_BURST];
68         struct rte_mempool *mbp;
69         struct rte_mbuf  *pkt = NULL;
70         struct rte_ether_hdr *eth_hdr;
71         struct rte_ipv4_hdr *ip_hdr;
72         struct rte_udp_hdr *udp_hdr;
73         uint16_t vlan_tci, vlan_tci_outer;
74         uint64_t ol_flags = 0;
75         uint16_t nb_rx;
76         uint16_t nb_tx;
77         uint16_t nb_dropped;
78         uint16_t nb_pkt;
79         uint16_t nb_clones = nb_pkt_flowgen_clones;
80         uint16_t i;
81         uint32_t retry;
82         uint64_t tx_offloads;
83         uint64_t start_tsc = 0;
84         int next_flow = RTE_PER_LCORE(_next_flow);
85
86         get_start_cycles(&start_tsc);
87
88         /* Receive a burst of packets and discard them. */
89         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
90                                  nb_pkt_per_burst);
91         inc_rx_burst_stats(fs, nb_rx);
92         fs->rx_packets += nb_rx;
93
94         for (i = 0; i < nb_rx; i++)
95                 rte_pktmbuf_free(pkts_burst[i]);
96
97         mbp = current_fwd_lcore()->mbp;
98         vlan_tci = ports[fs->tx_port].tx_vlan_id;
99         vlan_tci_outer = ports[fs->tx_port].tx_vlan_id_outer;
100
101         tx_offloads = ports[fs->tx_port].dev_conf.txmode.offloads;
102         if (tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT)
103                 ol_flags |= PKT_TX_VLAN_PKT;
104         if (tx_offloads & DEV_TX_OFFLOAD_QINQ_INSERT)
105                 ol_flags |= PKT_TX_QINQ_PKT;
106         if (tx_offloads & DEV_TX_OFFLOAD_MACSEC_INSERT)
107                 ol_flags |= PKT_TX_MACSEC;
108
109         for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
110                 if (!nb_pkt || !nb_clones) {
111                         nb_clones = nb_pkt_flowgen_clones;
112                         /* Logic limitation */
113                         if (nb_clones > nb_pkt_per_burst)
114                                 nb_clones = nb_pkt_per_burst;
115
116                         pkt = rte_mbuf_raw_alloc(mbp);
117                         if (!pkt)
118                                 break;
119
120                         pkt->data_len = pkt_size;
121                         pkt->next = NULL;
122
123                         /* Initialize Ethernet header. */
124                         eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
125                         rte_ether_addr_copy(&cfg_ether_dst, &eth_hdr->dst_addr);
126                         rte_ether_addr_copy(&cfg_ether_src, &eth_hdr->src_addr);
127                         eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
128
129                         /* Initialize IP header. */
130                         ip_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
131                         memset(ip_hdr, 0, sizeof(*ip_hdr));
132                         ip_hdr->version_ihl     = RTE_IPV4_VHL_DEF;
133                         ip_hdr->type_of_service = 0;
134                         ip_hdr->fragment_offset = 0;
135                         ip_hdr->time_to_live    = IP_DEFTTL;
136                         ip_hdr->next_proto_id   = IPPROTO_UDP;
137                         ip_hdr->packet_id       = 0;
138                         ip_hdr->src_addr        = rte_cpu_to_be_32(cfg_ip_src);
139                         ip_hdr->dst_addr        = rte_cpu_to_be_32(cfg_ip_dst +
140                                                                    next_flow);
141                         ip_hdr->total_length    = RTE_CPU_TO_BE_16(pkt_size -
142                                                                    sizeof(*eth_hdr));
143                         ip_hdr->hdr_checksum    = rte_ipv4_cksum(ip_hdr);
144
145                         /* Initialize UDP header. */
146                         udp_hdr = (struct rte_udp_hdr *)(ip_hdr + 1);
147                         udp_hdr->src_port       = rte_cpu_to_be_16(cfg_udp_src);
148                         udp_hdr->dst_port       = rte_cpu_to_be_16(cfg_udp_dst);
149                         udp_hdr->dgram_cksum    = 0; /* No UDP checksum. */
150                         udp_hdr->dgram_len      = RTE_CPU_TO_BE_16(pkt_size -
151                                                                    sizeof(*eth_hdr) -
152                                                                    sizeof(*ip_hdr));
153                         pkt->nb_segs            = 1;
154                         pkt->pkt_len            = pkt_size;
155                         pkt->ol_flags           &= EXT_ATTACHED_MBUF;
156                         pkt->ol_flags           |= ol_flags;
157                         pkt->vlan_tci           = vlan_tci;
158                         pkt->vlan_tci_outer     = vlan_tci_outer;
159                         pkt->l2_len             = sizeof(struct rte_ether_hdr);
160                         pkt->l3_len             = sizeof(struct rte_ipv4_hdr);
161                 } else {
162                         nb_clones--;
163                         rte_mbuf_refcnt_update(pkt, 1);
164                 }
165                 pkts_burst[nb_pkt] = pkt;
166
167                 if (++next_flow >= nb_flows_flowgen)
168                         next_flow = 0;
169         }
170
171         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_pkt);
172         /*
173          * Retry if necessary
174          */
175         if (unlikely(nb_tx < nb_pkt) && fs->retry_enabled) {
176                 retry = 0;
177                 while (nb_tx < nb_pkt && retry++ < burst_tx_retry_num) {
178                         rte_delay_us(burst_tx_delay_time);
179                         nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
180                                         &pkts_burst[nb_tx], nb_pkt - nb_tx);
181                 }
182         }
183         fs->tx_packets += nb_tx;
184
185         inc_tx_burst_stats(fs, nb_tx);
186         nb_dropped = nb_pkt - nb_tx;
187         if (unlikely(nb_dropped > 0)) {
188                 /* Back out the flow counter. */
189                 next_flow -= nb_dropped;
190                 while (next_flow < 0)
191                         next_flow += nb_flows_flowgen;
192
193                 fs->fwd_dropped += nb_dropped;
194                 do {
195                         rte_pktmbuf_free(pkts_burst[nb_tx]);
196                 } while (++nb_tx < nb_pkt);
197         }
198
199         RTE_PER_LCORE(_next_flow) = next_flow;
200
201         get_end_cycles(fs, start_tsc);
202 }
203
204 static int
205 flowgen_begin(portid_t pi)
206 {
207         printf("  number of flows for port %u: %d\n", pi, nb_flows_flowgen);
208         return 0;
209 }
210
211 struct fwd_engine flow_gen_engine = {
212         .fwd_mode_name  = "flowgen",
213         .port_fwd_begin = flowgen_begin,
214         .port_fwd_end   = NULL,
215         .packet_fwd     = pkt_burst_flow_gen,
216 };