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