app: use SPDX tag for Intel copyright files
[dpdk.git] / app / test-pmd / txonly.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
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 #define UDP_SRC_PORT 1024
44 #define UDP_DST_PORT 1024
45
46 #define IP_SRC_ADDR ((192U << 24) | (168 << 16) | (0 << 8) | 1)
47 #define IP_DST_ADDR ((192U << 24) | (168 << 16) | (0 << 8) | 2)
48
49 #define IP_DEFTTL  64   /* from RFC 1340. */
50 #define IP_VERSION 0x40
51 #define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
52 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
53
54 static struct ipv4_hdr  pkt_ip_hdr;  /**< IP header of transmitted packets. */
55 static struct udp_hdr pkt_udp_hdr; /**< UDP header of transmitted packets. */
56
57 static void
58 copy_buf_to_pkt_segs(void* buf, unsigned len, struct rte_mbuf *pkt,
59                      unsigned offset)
60 {
61         struct rte_mbuf *seg;
62         void *seg_buf;
63         unsigned copy_len;
64
65         seg = pkt;
66         while (offset >= seg->data_len) {
67                 offset -= seg->data_len;
68                 seg = seg->next;
69         }
70         copy_len = seg->data_len - offset;
71         seg_buf = rte_pktmbuf_mtod_offset(seg, char *, offset);
72         while (len > copy_len) {
73                 rte_memcpy(seg_buf, buf, (size_t) copy_len);
74                 len -= copy_len;
75                 buf = ((char*) buf + copy_len);
76                 seg = seg->next;
77                 seg_buf = rte_pktmbuf_mtod(seg, char *);
78         }
79         rte_memcpy(seg_buf, buf, (size_t) len);
80 }
81
82 static inline void
83 copy_buf_to_pkt(void* buf, unsigned len, struct rte_mbuf *pkt, unsigned offset)
84 {
85         if (offset + len <= pkt->data_len) {
86                 rte_memcpy(rte_pktmbuf_mtod_offset(pkt, char *, offset),
87                         buf, (size_t) len);
88                 return;
89         }
90         copy_buf_to_pkt_segs(buf, len, pkt, offset);
91 }
92
93 static void
94 setup_pkt_udp_ip_headers(struct ipv4_hdr *ip_hdr,
95                          struct udp_hdr *udp_hdr,
96                          uint16_t pkt_data_len)
97 {
98         uint16_t *ptr16;
99         uint32_t ip_cksum;
100         uint16_t pkt_len;
101
102         /*
103          * Initialize UDP header.
104          */
105         pkt_len = (uint16_t) (pkt_data_len + sizeof(struct udp_hdr));
106         udp_hdr->src_port = rte_cpu_to_be_16(UDP_SRC_PORT);
107         udp_hdr->dst_port = rte_cpu_to_be_16(UDP_DST_PORT);
108         udp_hdr->dgram_len      = RTE_CPU_TO_BE_16(pkt_len);
109         udp_hdr->dgram_cksum    = 0; /* No UDP checksum. */
110
111         /*
112          * Initialize IP header.
113          */
114         pkt_len = (uint16_t) (pkt_len + sizeof(struct ipv4_hdr));
115         ip_hdr->version_ihl   = IP_VHL_DEF;
116         ip_hdr->type_of_service   = 0;
117         ip_hdr->fragment_offset = 0;
118         ip_hdr->time_to_live   = IP_DEFTTL;
119         ip_hdr->next_proto_id = IPPROTO_UDP;
120         ip_hdr->packet_id = 0;
121         ip_hdr->total_length   = RTE_CPU_TO_BE_16(pkt_len);
122         ip_hdr->src_addr = rte_cpu_to_be_32(IP_SRC_ADDR);
123         ip_hdr->dst_addr = rte_cpu_to_be_32(IP_DST_ADDR);
124
125         /*
126          * Compute IP header checksum.
127          */
128         ptr16 = (unaligned_uint16_t*) ip_hdr;
129         ip_cksum = 0;
130         ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
131         ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
132         ip_cksum += ptr16[4];
133         ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
134         ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
135
136         /*
137          * Reduce 32 bit checksum to 16 bits and complement it.
138          */
139         ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
140                 (ip_cksum & 0x0000FFFF);
141         if (ip_cksum > 65535)
142                 ip_cksum -= 65535;
143         ip_cksum = (~ip_cksum) & 0x0000FFFF;
144         if (ip_cksum == 0)
145                 ip_cksum = 0xFFFF;
146         ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
147 }
148
149 /*
150  * Transmit a burst of multi-segments packets.
151  */
152 static void
153 pkt_burst_transmit(struct fwd_stream *fs)
154 {
155         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
156         struct rte_port *txp;
157         struct rte_mbuf *pkt;
158         struct rte_mbuf *pkt_seg;
159         struct rte_mempool *mbp;
160         struct ether_hdr eth_hdr;
161         uint16_t nb_tx;
162         uint16_t nb_pkt;
163         uint16_t vlan_tci, vlan_tci_outer;
164         uint32_t retry;
165         uint64_t ol_flags = 0;
166         uint8_t  i;
167 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
168         uint64_t start_tsc;
169         uint64_t end_tsc;
170         uint64_t core_cycles;
171 #endif
172         uint32_t nb_segs, pkt_len;
173
174 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
175         start_tsc = rte_rdtsc();
176 #endif
177
178         mbp = current_fwd_lcore()->mbp;
179         txp = &ports[fs->tx_port];
180         vlan_tci = txp->tx_vlan_id;
181         vlan_tci_outer = txp->tx_vlan_id_outer;
182         if (txp->tx_ol_flags & TESTPMD_TX_OFFLOAD_INSERT_VLAN)
183                 ol_flags = PKT_TX_VLAN_PKT;
184         if (txp->tx_ol_flags & TESTPMD_TX_OFFLOAD_INSERT_QINQ)
185                 ol_flags |= PKT_TX_QINQ_PKT;
186         if (txp->tx_ol_flags & TESTPMD_TX_OFFLOAD_MACSEC)
187                 ol_flags |= PKT_TX_MACSEC;
188         for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
189                 pkt = rte_mbuf_raw_alloc(mbp);
190                 if (pkt == NULL) {
191                 nomore_mbuf:
192                         if (nb_pkt == 0)
193                                 return;
194                         break;
195                 }
196
197                 /*
198                  * Using raw alloc is good to improve performance,
199                  * but some consumers may use the headroom and so
200                  * decrement data_off. We need to make sure it is
201                  * reset to default value.
202                  */
203                 rte_pktmbuf_reset_headroom(pkt);
204                 pkt->data_len = tx_pkt_seg_lengths[0];
205                 pkt_seg = pkt;
206                 if (tx_pkt_split == TX_PKT_SPLIT_RND)
207                         nb_segs = random() % tx_pkt_nb_segs + 1;
208                 else
209                         nb_segs = tx_pkt_nb_segs;
210                 pkt_len = pkt->data_len;
211                 for (i = 1; i < nb_segs; i++) {
212                         pkt_seg->next = rte_mbuf_raw_alloc(mbp);
213                         if (pkt_seg->next == NULL) {
214                                 pkt->nb_segs = i;
215                                 rte_pktmbuf_free(pkt);
216                                 goto nomore_mbuf;
217                         }
218                         pkt_seg = pkt_seg->next;
219                         pkt_seg->data_len = tx_pkt_seg_lengths[i];
220                         pkt_len += pkt_seg->data_len;
221                 }
222                 pkt_seg->next = NULL; /* Last segment of packet. */
223
224                 /*
225                  * Initialize Ethernet header.
226                  */
227                 ether_addr_copy(&peer_eth_addrs[fs->peer_addr],&eth_hdr.d_addr);
228                 ether_addr_copy(&ports[fs->tx_port].eth_addr, &eth_hdr.s_addr);
229                 eth_hdr.ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
230
231                 /*
232                  * Copy headers in first packet segment(s).
233                  */
234                 copy_buf_to_pkt(&eth_hdr, sizeof(eth_hdr), pkt, 0);
235                 copy_buf_to_pkt(&pkt_ip_hdr, sizeof(pkt_ip_hdr), pkt,
236                                 sizeof(struct ether_hdr));
237                 copy_buf_to_pkt(&pkt_udp_hdr, sizeof(pkt_udp_hdr), pkt,
238                                 sizeof(struct ether_hdr) +
239                                 sizeof(struct ipv4_hdr));
240
241                 /*
242                  * Complete first mbuf of packet and append it to the
243                  * burst of packets to be transmitted.
244                  */
245                 pkt->nb_segs = nb_segs;
246                 pkt->pkt_len = pkt_len;
247                 pkt->ol_flags = ol_flags;
248                 pkt->vlan_tci = vlan_tci;
249                 pkt->vlan_tci_outer = vlan_tci_outer;
250                 pkt->l2_len = sizeof(struct ether_hdr);
251                 pkt->l3_len = sizeof(struct ipv4_hdr);
252                 pkts_burst[nb_pkt] = pkt;
253         }
254         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_pkt);
255         /*
256          * Retry if necessary
257          */
258         if (unlikely(nb_tx < nb_pkt) && fs->retry_enabled) {
259                 retry = 0;
260                 while (nb_tx < nb_pkt && retry++ < burst_tx_retry_num) {
261                         rte_delay_us(burst_tx_delay_time);
262                         nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
263                                         &pkts_burst[nb_tx], nb_pkt - nb_tx);
264                 }
265         }
266         fs->tx_packets += nb_tx;
267
268 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
269         fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
270 #endif
271         if (unlikely(nb_tx < nb_pkt)) {
272                 if (verbose_level > 0 && fs->fwd_dropped == 0)
273                         printf("port %d tx_queue %d - drop "
274                                "(nb_pkt:%u - nb_tx:%u)=%u packets\n",
275                                fs->tx_port, fs->tx_queue,
276                                (unsigned) nb_pkt, (unsigned) nb_tx,
277                                (unsigned) (nb_pkt - nb_tx));
278                 fs->fwd_dropped += (nb_pkt - nb_tx);
279                 do {
280                         rte_pktmbuf_free(pkts_burst[nb_tx]);
281                 } while (++nb_tx < nb_pkt);
282         }
283
284 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
285         end_tsc = rte_rdtsc();
286         core_cycles = (end_tsc - start_tsc);
287         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
288 #endif
289 }
290
291 static void
292 tx_only_begin(__attribute__((unused)) portid_t pi)
293 {
294         uint16_t pkt_data_len;
295
296         pkt_data_len = (uint16_t) (tx_pkt_length - (sizeof(struct ether_hdr) +
297                                                     sizeof(struct ipv4_hdr) +
298                                                     sizeof(struct udp_hdr)));
299         setup_pkt_udp_ip_headers(&pkt_ip_hdr, &pkt_udp_hdr, pkt_data_len);
300 }
301
302 struct fwd_engine tx_only_engine = {
303         .fwd_mode_name  = "txonly",
304         .port_fwd_begin = tx_only_begin,
305         .port_fwd_end   = NULL,
306         .packet_fwd     = pkt_burst_transmit,
307 };