xref: /dpdk/app/test-pmd/txonly.c (revision cfe4ca10)
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 /* use RFC863 Discard Protocol */
44 uint16_t tx_udp_src_port = 9;
45 uint16_t tx_udp_dst_port = 9;
46 
47 /* use RFC5735 / RFC2544 reserved network test addresses */
48 uint32_t tx_ip_src_addr = (198U << 24) | (18 << 16) | (0 << 8) | 1;
49 uint32_t tx_ip_dst_addr = (198U << 24) | (18 << 16) | (0 << 8) | 2;
50 
51 #define IP_DEFTTL  64   /* from RFC 1340. */
52 
53 static struct rte_ipv4_hdr pkt_ip_hdr; /**< IP header of transmitted packets. */
54 RTE_DEFINE_PER_LCORE(uint8_t, _ip_var); /**< IP address variation */
55 static struct rte_udp_hdr pkt_udp_hdr; /**< UDP header of tx packets. */
56 RTE_DEFINE_PER_LCORE(uint64_t, timestamp_qskew);
57 					/**< Timestamp offset per queue */
58 static uint64_t timestamp_mask; /**< Timestamp dynamic flag mask */
59 static int32_t timestamp_off; /**< Timestamp dynamic field offset */
60 static bool timestamp_enable; /**< Timestamp enable */
61 static uint64_t timestamp_initial[RTE_MAX_ETHPORTS];
62 
63 static void
64 copy_buf_to_pkt_segs(void* buf, unsigned len, struct rte_mbuf *pkt,
65 		     unsigned offset)
66 {
67 	struct rte_mbuf *seg;
68 	void *seg_buf;
69 	unsigned copy_len;
70 
71 	seg = pkt;
72 	while (offset >= seg->data_len) {
73 		offset -= seg->data_len;
74 		seg = seg->next;
75 	}
76 	copy_len = seg->data_len - offset;
77 	seg_buf = rte_pktmbuf_mtod_offset(seg, char *, offset);
78 	while (len > copy_len) {
79 		rte_memcpy(seg_buf, buf, (size_t) copy_len);
80 		len -= copy_len;
81 		buf = ((char*) buf + copy_len);
82 		seg = seg->next;
83 		seg_buf = rte_pktmbuf_mtod(seg, char *);
84 		copy_len = seg->data_len;
85 	}
86 	rte_memcpy(seg_buf, buf, (size_t) len);
87 }
88 
89 static inline void
90 copy_buf_to_pkt(void* buf, unsigned len, struct rte_mbuf *pkt, unsigned offset)
91 {
92 	if (offset + len <= pkt->data_len) {
93 		rte_memcpy(rte_pktmbuf_mtod_offset(pkt, char *, offset),
94 			buf, (size_t) len);
95 		return;
96 	}
97 	copy_buf_to_pkt_segs(buf, len, pkt, offset);
98 }
99 
100 static void
101 setup_pkt_udp_ip_headers(struct rte_ipv4_hdr *ip_hdr,
102 			 struct rte_udp_hdr *udp_hdr,
103 			 uint16_t pkt_data_len)
104 {
105 	uint16_t *ptr16;
106 	uint32_t ip_cksum;
107 	uint16_t pkt_len;
108 
109 	/*
110 	 * Initialize UDP header.
111 	 */
112 	pkt_len = (uint16_t) (pkt_data_len + sizeof(struct rte_udp_hdr));
113 	udp_hdr->src_port = rte_cpu_to_be_16(tx_udp_src_port);
114 	udp_hdr->dst_port = rte_cpu_to_be_16(tx_udp_dst_port);
115 	udp_hdr->dgram_len      = RTE_CPU_TO_BE_16(pkt_len);
116 	udp_hdr->dgram_cksum    = 0; /* No UDP checksum. */
117 
118 	/*
119 	 * Initialize IP header.
120 	 */
121 	pkt_len = (uint16_t) (pkt_len + sizeof(struct rte_ipv4_hdr));
122 	ip_hdr->version_ihl   = RTE_IPV4_VHL_DEF;
123 	ip_hdr->type_of_service   = 0;
124 	ip_hdr->fragment_offset = 0;
125 	ip_hdr->time_to_live   = IP_DEFTTL;
126 	ip_hdr->next_proto_id = IPPROTO_UDP;
127 	ip_hdr->packet_id = 0;
128 	ip_hdr->total_length   = RTE_CPU_TO_BE_16(pkt_len);
129 	ip_hdr->src_addr = rte_cpu_to_be_32(tx_ip_src_addr);
130 	ip_hdr->dst_addr = rte_cpu_to_be_32(tx_ip_dst_addr);
131 
132 	/*
133 	 * Compute IP header checksum.
134 	 */
135 	ptr16 = (unaligned_uint16_t*) ip_hdr;
136 	ip_cksum = 0;
137 	ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
138 	ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
139 	ip_cksum += ptr16[4];
140 	ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
141 	ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
142 
143 	/*
144 	 * Reduce 32 bit checksum to 16 bits and complement it.
145 	 */
146 	ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
147 		(ip_cksum & 0x0000FFFF);
148 	if (ip_cksum > 65535)
149 		ip_cksum -= 65535;
150 	ip_cksum = (~ip_cksum) & 0x0000FFFF;
151 	if (ip_cksum == 0)
152 		ip_cksum = 0xFFFF;
153 	ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
154 }
155 
156 static inline bool
157 pkt_burst_prepare(struct rte_mbuf *pkt, struct rte_mempool *mbp,
158 		struct rte_ether_hdr *eth_hdr, const uint16_t vlan_tci,
159 		const uint16_t vlan_tci_outer, const uint64_t ol_flags,
160 		const uint16_t idx, const struct fwd_stream *fs)
161 {
162 	struct rte_mbuf *pkt_segs[RTE_MAX_SEGS_PER_PKT];
163 	struct rte_mbuf *pkt_seg;
164 	uint32_t nb_segs, pkt_len;
165 	uint8_t i;
166 
167 	if (unlikely(tx_pkt_split == TX_PKT_SPLIT_RND))
168 		nb_segs = rte_rand() % tx_pkt_nb_segs + 1;
169 	else
170 		nb_segs = tx_pkt_nb_segs;
171 
172 	if (nb_segs > 1) {
173 		if (rte_mempool_get_bulk(mbp, (void **)pkt_segs, nb_segs - 1))
174 			return false;
175 	}
176 
177 	rte_pktmbuf_reset_headroom(pkt);
178 	pkt->data_len = tx_pkt_seg_lengths[0];
179 	pkt->ol_flags &= EXT_ATTACHED_MBUF;
180 	pkt->ol_flags |= ol_flags;
181 	pkt->vlan_tci = vlan_tci;
182 	pkt->vlan_tci_outer = vlan_tci_outer;
183 	pkt->l2_len = sizeof(struct rte_ether_hdr);
184 	pkt->l3_len = sizeof(struct rte_ipv4_hdr);
185 
186 	pkt_len = pkt->data_len;
187 	pkt_seg = pkt;
188 	for (i = 1; i < nb_segs; i++) {
189 		pkt_seg->next = pkt_segs[i - 1];
190 		pkt_seg = pkt_seg->next;
191 		pkt_seg->data_len = tx_pkt_seg_lengths[i];
192 		pkt_len += pkt_seg->data_len;
193 	}
194 	pkt_seg->next = NULL; /* Last segment of packet. */
195 	/*
196 	 * Copy headers in first packet segment(s).
197 	 */
198 	copy_buf_to_pkt(eth_hdr, sizeof(*eth_hdr), pkt, 0);
199 	copy_buf_to_pkt(&pkt_ip_hdr, sizeof(pkt_ip_hdr), pkt,
200 			sizeof(struct rte_ether_hdr));
201 	if (txonly_multi_flow) {
202 		uint8_t  ip_var = RTE_PER_LCORE(_ip_var);
203 		struct rte_ipv4_hdr *ip_hdr;
204 		uint32_t addr;
205 
206 		ip_hdr = rte_pktmbuf_mtod_offset(pkt,
207 				struct rte_ipv4_hdr *,
208 				sizeof(struct rte_ether_hdr));
209 		/*
210 		 * Generate multiple flows by varying IP src addr. This
211 		 * enables packets are well distributed by RSS in
212 		 * receiver side if any and txonly mode can be a decent
213 		 * packet generator for developer's quick performance
214 		 * regression test.
215 		 */
216 		addr = (tx_ip_dst_addr | (ip_var++ << 8)) + rte_lcore_id();
217 		ip_hdr->src_addr = rte_cpu_to_be_32(addr);
218 		RTE_PER_LCORE(_ip_var) = ip_var;
219 	}
220 	copy_buf_to_pkt(&pkt_udp_hdr, sizeof(pkt_udp_hdr), pkt,
221 			sizeof(struct rte_ether_hdr) +
222 			sizeof(struct rte_ipv4_hdr));
223 	if (unlikely(timestamp_enable)) {
224 		uint64_t skew = RTE_PER_LCORE(timestamp_qskew);
225 		struct {
226 			rte_be32_t signature;
227 			rte_be16_t pkt_idx;
228 			rte_be16_t queue_idx;
229 			rte_be64_t ts;
230 		} timestamp_mark;
231 
232 		if (unlikely(!skew)) {
233 			struct rte_eth_dev *dev = &rte_eth_devices[fs->tx_port];
234 			unsigned int txqs_n = dev->data->nb_tx_queues;
235 			uint64_t phase = tx_pkt_times_inter * fs->tx_queue /
236 					 (txqs_n ? txqs_n : 1);
237 			/*
238 			 * Initialize the scheduling time phase shift
239 			 * depending on queue index.
240 			 */
241 			skew = timestamp_initial[fs->tx_port] +
242 			       tx_pkt_times_inter + phase;
243 			RTE_PER_LCORE(timestamp_qskew) = skew;
244 		}
245 		timestamp_mark.pkt_idx = rte_cpu_to_be_16(idx);
246 		timestamp_mark.queue_idx = rte_cpu_to_be_16(fs->tx_queue);
247 		timestamp_mark.signature = rte_cpu_to_be_32(0xBEEFC0DE);
248 		if (unlikely(!idx)) {
249 			skew +=	tx_pkt_times_inter;
250 			pkt->ol_flags |= timestamp_mask;
251 			*RTE_MBUF_DYNFIELD
252 				(pkt, timestamp_off, uint64_t *) = skew;
253 			RTE_PER_LCORE(timestamp_qskew) = skew;
254 			timestamp_mark.ts = rte_cpu_to_be_64(skew);
255 		} else if (tx_pkt_times_intra) {
256 			skew +=	tx_pkt_times_intra;
257 			pkt->ol_flags |= timestamp_mask;
258 			*RTE_MBUF_DYNFIELD
259 				(pkt, timestamp_off, uint64_t *) = skew;
260 			RTE_PER_LCORE(timestamp_qskew) = skew;
261 			timestamp_mark.ts = rte_cpu_to_be_64(skew);
262 		} else {
263 			timestamp_mark.ts = RTE_BE64(0);
264 		}
265 		copy_buf_to_pkt(&timestamp_mark, sizeof(timestamp_mark), pkt,
266 			sizeof(struct rte_ether_hdr) +
267 			sizeof(struct rte_ipv4_hdr) +
268 			sizeof(pkt_udp_hdr));
269 	}
270 	/*
271 	 * Complete first mbuf of packet and append it to the
272 	 * burst of packets to be transmitted.
273 	 */
274 	pkt->nb_segs = nb_segs;
275 	pkt->pkt_len = pkt_len;
276 
277 	return true;
278 }
279 
280 /*
281  * Transmit a burst of multi-segments packets.
282  */
283 static void
284 pkt_burst_transmit(struct fwd_stream *fs)
285 {
286 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
287 	struct rte_port *txp;
288 	struct rte_mbuf *pkt;
289 	struct rte_mempool *mbp;
290 	struct rte_ether_hdr eth_hdr;
291 	uint16_t nb_tx;
292 	uint16_t nb_pkt;
293 	uint16_t vlan_tci, vlan_tci_outer;
294 	uint32_t retry;
295 	uint64_t ol_flags = 0;
296 	uint64_t tx_offloads;
297 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
298 	uint64_t start_tsc;
299 	uint64_t end_tsc;
300 	uint64_t core_cycles;
301 #endif
302 
303 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
304 	start_tsc = rte_rdtsc();
305 #endif
306 
307 	mbp = current_fwd_lcore()->mbp;
308 	txp = &ports[fs->tx_port];
309 	tx_offloads = txp->dev_conf.txmode.offloads;
310 	vlan_tci = txp->tx_vlan_id;
311 	vlan_tci_outer = txp->tx_vlan_id_outer;
312 	if (tx_offloads	& DEV_TX_OFFLOAD_VLAN_INSERT)
313 		ol_flags = PKT_TX_VLAN_PKT;
314 	if (tx_offloads & DEV_TX_OFFLOAD_QINQ_INSERT)
315 		ol_flags |= PKT_TX_QINQ_PKT;
316 	if (tx_offloads & DEV_TX_OFFLOAD_MACSEC_INSERT)
317 		ol_flags |= PKT_TX_MACSEC;
318 
319 	/*
320 	 * Initialize Ethernet header.
321 	 */
322 	rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr], &eth_hdr.d_addr);
323 	rte_ether_addr_copy(&ports[fs->tx_port].eth_addr, &eth_hdr.s_addr);
324 	eth_hdr.ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
325 
326 	if (rte_mempool_get_bulk(mbp, (void **)pkts_burst,
327 				nb_pkt_per_burst) == 0) {
328 		for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
329 			if (unlikely(!pkt_burst_prepare(pkts_burst[nb_pkt], mbp,
330 							&eth_hdr, vlan_tci,
331 							vlan_tci_outer,
332 							ol_flags,
333 							nb_pkt, fs))) {
334 				rte_mempool_put_bulk(mbp,
335 						(void **)&pkts_burst[nb_pkt],
336 						nb_pkt_per_burst - nb_pkt);
337 				break;
338 			}
339 		}
340 	} else {
341 		for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
342 			pkt = rte_mbuf_raw_alloc(mbp);
343 			if (pkt == NULL)
344 				break;
345 			if (unlikely(!pkt_burst_prepare(pkt, mbp, &eth_hdr,
346 							vlan_tci,
347 							vlan_tci_outer,
348 							ol_flags,
349 							nb_pkt, fs))) {
350 				rte_pktmbuf_free(pkt);
351 				break;
352 			}
353 			pkts_burst[nb_pkt] = pkt;
354 		}
355 	}
356 
357 	if (nb_pkt == 0)
358 		return;
359 
360 	nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_pkt);
361 
362 	/*
363 	 * Retry if necessary
364 	 */
365 	if (unlikely(nb_tx < nb_pkt) && fs->retry_enabled) {
366 		retry = 0;
367 		while (nb_tx < nb_pkt && retry++ < burst_tx_retry_num) {
368 			rte_delay_us(burst_tx_delay_time);
369 			nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
370 					&pkts_burst[nb_tx], nb_pkt - nb_tx);
371 		}
372 	}
373 	fs->tx_packets += nb_tx;
374 
375 	if (txonly_multi_flow)
376 		RTE_PER_LCORE(_ip_var) -= nb_pkt - nb_tx;
377 
378 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
379 	fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
380 #endif
381 	if (unlikely(nb_tx < nb_pkt)) {
382 		if (verbose_level > 0 && fs->fwd_dropped == 0)
383 			printf("port %d tx_queue %d - drop "
384 			       "(nb_pkt:%u - nb_tx:%u)=%u packets\n",
385 			       fs->tx_port, fs->tx_queue,
386 			       (unsigned) nb_pkt, (unsigned) nb_tx,
387 			       (unsigned) (nb_pkt - nb_tx));
388 		fs->fwd_dropped += (nb_pkt - nb_tx);
389 		do {
390 			rte_pktmbuf_free(pkts_burst[nb_tx]);
391 		} while (++nb_tx < nb_pkt);
392 	}
393 
394 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
395 	end_tsc = rte_rdtsc();
396 	core_cycles = (end_tsc - start_tsc);
397 	fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
398 #endif
399 }
400 
401 static void
402 tx_only_begin(portid_t pi)
403 {
404 	uint16_t pkt_data_len;
405 	int dynf;
406 
407 	pkt_data_len = (uint16_t) (tx_pkt_length - (
408 					sizeof(struct rte_ether_hdr) +
409 					sizeof(struct rte_ipv4_hdr) +
410 					sizeof(struct rte_udp_hdr)));
411 	setup_pkt_udp_ip_headers(&pkt_ip_hdr, &pkt_udp_hdr, pkt_data_len);
412 
413 	timestamp_enable = false;
414 	timestamp_mask = 0;
415 	timestamp_off = -1;
416 	RTE_PER_LCORE(timestamp_qskew) = 0;
417 	dynf = rte_mbuf_dynflag_lookup
418 				(RTE_MBUF_DYNFLAG_TX_TIMESTAMP_NAME, NULL);
419 	if (dynf >= 0)
420 		timestamp_mask = 1ULL << dynf;
421 	dynf = rte_mbuf_dynfield_lookup
422 				(RTE_MBUF_DYNFIELD_TIMESTAMP_NAME, NULL);
423 	if (dynf >= 0)
424 		timestamp_off = dynf;
425 	timestamp_enable = tx_pkt_times_inter &&
426 			   timestamp_mask &&
427 			   timestamp_off >= 0 &&
428 			   !rte_eth_read_clock(pi, &timestamp_initial[pi]);
429 }
430 
431 struct fwd_engine tx_only_engine = {
432 	.fwd_mode_name  = "txonly",
433 	.port_fwd_begin = tx_only_begin,
434 	.port_fwd_end   = NULL,
435 	.packet_fwd     = pkt_burst_transmit,
436 };
437