1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #include <sys/queue.h>
16 #include <rte_common.h>
17 #include <rte_byteorder.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>
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>
31 #include <rte_interrupts.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
38 #include <rte_string_fns.h>
43 /* use RFC863 Discard Protocol */
44 uint16_t tx_udp_src_port = 9;
45 uint16_t tx_udp_dst_port = 9;
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;
51 #define IP_DEFTTL 64 /* from RFC 1340. */
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. */
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];
67 copy_buf_to_pkt_segs(void* buf, unsigned len, struct rte_mbuf *pkt,
75 while (offset >= seg->data_len) {
76 offset -= seg->data_len;
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);
84 buf = ((char*) buf + copy_len);
86 seg_buf = rte_pktmbuf_mtod(seg, char *);
87 copy_len = seg->data_len;
89 rte_memcpy(seg_buf, buf, (size_t) len);
93 copy_buf_to_pkt(void* buf, unsigned len, struct rte_mbuf *pkt, unsigned offset)
95 if (offset + len <= pkt->data_len) {
96 rte_memcpy(rte_pktmbuf_mtod_offset(pkt, char *, offset),
100 copy_buf_to_pkt_segs(buf, len, pkt, offset);
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)
113 * Initialize UDP header.
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. */
122 * Initialize IP header.
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);
136 * Compute IP header checksum.
138 ptr16 = (unaligned_uint16_t*) ip_hdr;
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];
147 * Reduce 32 bit checksum to 16 bits and complement it.
149 ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
150 (ip_cksum & 0x0000FFFF);
151 if (ip_cksum > 65535)
153 ip_cksum = (~ip_cksum) & 0x0000FFFF;
156 ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
160 pkt_burst_prepare(struct rte_mbuf *pkt, struct rte_mempool *mbp,
161 struct rte_ether_hdr *eth_hdr, const uint16_t vlan_tci,
162 const uint16_t vlan_tci_outer, const uint64_t ol_flags,
163 const uint16_t idx, const struct fwd_stream *fs)
165 struct rte_mbuf *pkt_segs[RTE_MAX_SEGS_PER_PKT];
166 struct rte_mbuf *pkt_seg;
167 uint32_t nb_segs, pkt_len;
170 if (unlikely(tx_pkt_split == TX_PKT_SPLIT_RND))
171 nb_segs = rte_rand() % tx_pkt_nb_segs + 1;
173 nb_segs = tx_pkt_nb_segs;
176 if (rte_mempool_get_bulk(mbp, (void **)pkt_segs, nb_segs - 1))
180 rte_pktmbuf_reset_headroom(pkt);
181 pkt->data_len = tx_pkt_seg_lengths[0];
182 pkt->ol_flags &= EXT_ATTACHED_MBUF;
183 pkt->ol_flags |= ol_flags;
184 pkt->vlan_tci = vlan_tci;
185 pkt->vlan_tci_outer = vlan_tci_outer;
186 pkt->l2_len = sizeof(struct rte_ether_hdr);
187 pkt->l3_len = sizeof(struct rte_ipv4_hdr);
189 pkt_len = pkt->data_len;
191 for (i = 1; i < nb_segs; i++) {
192 pkt_seg->next = pkt_segs[i - 1];
193 pkt_seg = pkt_seg->next;
194 pkt_seg->data_len = tx_pkt_seg_lengths[i];
195 pkt_len += pkt_seg->data_len;
197 pkt_seg->next = NULL; /* Last segment of packet. */
199 * Copy headers in first packet segment(s).
201 copy_buf_to_pkt(eth_hdr, sizeof(*eth_hdr), pkt, 0);
202 copy_buf_to_pkt(&pkt_ip_hdr, sizeof(pkt_ip_hdr), pkt,
203 sizeof(struct rte_ether_hdr));
204 if (txonly_multi_flow) {
205 uint8_t ip_var = RTE_PER_LCORE(_ip_var);
206 struct rte_ipv4_hdr *ip_hdr;
209 ip_hdr = rte_pktmbuf_mtod_offset(pkt,
210 struct rte_ipv4_hdr *,
211 sizeof(struct rte_ether_hdr));
213 * Generate multiple flows by varying IP src addr. This
214 * enables packets are well distributed by RSS in
215 * receiver side if any and txonly mode can be a decent
216 * packet generator for developer's quick performance
219 addr = (tx_ip_dst_addr | (ip_var++ << 8)) + rte_lcore_id();
220 ip_hdr->src_addr = rte_cpu_to_be_32(addr);
221 RTE_PER_LCORE(_ip_var) = ip_var;
223 copy_buf_to_pkt(&pkt_udp_hdr, sizeof(pkt_udp_hdr), pkt,
224 sizeof(struct rte_ether_hdr) +
225 sizeof(struct rte_ipv4_hdr));
226 if (unlikely(timestamp_enable)) {
227 uint64_t skew = RTE_PER_LCORE(timestamp_qskew);
229 rte_be32_t signature;
231 rte_be16_t queue_idx;
235 if (unlikely(timestamp_init_req !=
236 RTE_PER_LCORE(timestamp_idone))) {
237 struct rte_eth_dev *dev = &rte_eth_devices[fs->tx_port];
238 unsigned int txqs_n = dev->data->nb_tx_queues;
239 uint64_t phase = tx_pkt_times_inter * fs->tx_queue /
240 (txqs_n ? txqs_n : 1);
242 * Initialize the scheduling time phase shift
243 * depending on queue index.
245 skew = timestamp_initial[fs->tx_port] +
246 tx_pkt_times_inter + phase;
247 RTE_PER_LCORE(timestamp_qskew) = skew;
248 RTE_PER_LCORE(timestamp_idone) = timestamp_init_req;
250 timestamp_mark.pkt_idx = rte_cpu_to_be_16(idx);
251 timestamp_mark.queue_idx = rte_cpu_to_be_16(fs->tx_queue);
252 timestamp_mark.signature = rte_cpu_to_be_32(0xBEEFC0DE);
253 if (unlikely(!idx)) {
254 skew += tx_pkt_times_inter;
255 pkt->ol_flags |= timestamp_mask;
257 (pkt, timestamp_off, uint64_t *) = skew;
258 RTE_PER_LCORE(timestamp_qskew) = skew;
259 timestamp_mark.ts = rte_cpu_to_be_64(skew);
260 } else if (tx_pkt_times_intra) {
261 skew += tx_pkt_times_intra;
262 pkt->ol_flags |= timestamp_mask;
264 (pkt, timestamp_off, uint64_t *) = skew;
265 RTE_PER_LCORE(timestamp_qskew) = skew;
266 timestamp_mark.ts = rte_cpu_to_be_64(skew);
268 timestamp_mark.ts = RTE_BE64(0);
270 copy_buf_to_pkt(×tamp_mark, sizeof(timestamp_mark), pkt,
271 sizeof(struct rte_ether_hdr) +
272 sizeof(struct rte_ipv4_hdr) +
273 sizeof(pkt_udp_hdr));
276 * Complete first mbuf of packet and append it to the
277 * burst of packets to be transmitted.
279 pkt->nb_segs = nb_segs;
280 pkt->pkt_len = pkt_len;
286 * Transmit a burst of multi-segments packets.
289 pkt_burst_transmit(struct fwd_stream *fs)
291 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
292 struct rte_port *txp;
293 struct rte_mbuf *pkt;
294 struct rte_mempool *mbp;
295 struct rte_ether_hdr eth_hdr;
298 uint16_t vlan_tci, vlan_tci_outer;
300 uint64_t ol_flags = 0;
301 uint64_t tx_offloads;
302 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
305 uint64_t core_cycles;
308 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
309 start_tsc = rte_rdtsc();
312 mbp = current_fwd_lcore()->mbp;
313 txp = &ports[fs->tx_port];
314 tx_offloads = txp->dev_conf.txmode.offloads;
315 vlan_tci = txp->tx_vlan_id;
316 vlan_tci_outer = txp->tx_vlan_id_outer;
317 if (tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT)
318 ol_flags = PKT_TX_VLAN_PKT;
319 if (tx_offloads & DEV_TX_OFFLOAD_QINQ_INSERT)
320 ol_flags |= PKT_TX_QINQ_PKT;
321 if (tx_offloads & DEV_TX_OFFLOAD_MACSEC_INSERT)
322 ol_flags |= PKT_TX_MACSEC;
325 * Initialize Ethernet header.
327 rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr], ð_hdr.d_addr);
328 rte_ether_addr_copy(&ports[fs->tx_port].eth_addr, ð_hdr.s_addr);
329 eth_hdr.ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
331 if (rte_mempool_get_bulk(mbp, (void **)pkts_burst,
332 nb_pkt_per_burst) == 0) {
333 for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
334 if (unlikely(!pkt_burst_prepare(pkts_burst[nb_pkt], mbp,
339 rte_mempool_put_bulk(mbp,
340 (void **)&pkts_burst[nb_pkt],
341 nb_pkt_per_burst - nb_pkt);
346 for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
347 pkt = rte_mbuf_raw_alloc(mbp);
350 if (unlikely(!pkt_burst_prepare(pkt, mbp, ð_hdr,
355 rte_pktmbuf_free(pkt);
358 pkts_burst[nb_pkt] = pkt;
365 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_pkt);
370 if (unlikely(nb_tx < nb_pkt) && fs->retry_enabled) {
372 while (nb_tx < nb_pkt && retry++ < burst_tx_retry_num) {
373 rte_delay_us(burst_tx_delay_time);
374 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
375 &pkts_burst[nb_tx], nb_pkt - nb_tx);
378 fs->tx_packets += nb_tx;
380 if (txonly_multi_flow)
381 RTE_PER_LCORE(_ip_var) -= nb_pkt - nb_tx;
383 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
384 fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
386 if (unlikely(nb_tx < nb_pkt)) {
387 if (verbose_level > 0 && fs->fwd_dropped == 0)
388 printf("port %d tx_queue %d - drop "
389 "(nb_pkt:%u - nb_tx:%u)=%u packets\n",
390 fs->tx_port, fs->tx_queue,
391 (unsigned) nb_pkt, (unsigned) nb_tx,
392 (unsigned) (nb_pkt - nb_tx));
393 fs->fwd_dropped += (nb_pkt - nb_tx);
395 rte_pktmbuf_free(pkts_burst[nb_tx]);
396 } while (++nb_tx < nb_pkt);
399 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
400 end_tsc = rte_rdtsc();
401 core_cycles = (end_tsc - start_tsc);
402 fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
407 tx_only_begin(portid_t pi)
409 uint16_t pkt_data_len;
412 pkt_data_len = (uint16_t) (tx_pkt_length - (
413 sizeof(struct rte_ether_hdr) +
414 sizeof(struct rte_ipv4_hdr) +
415 sizeof(struct rte_udp_hdr)));
416 setup_pkt_udp_ip_headers(&pkt_ip_hdr, &pkt_udp_hdr, pkt_data_len);
418 timestamp_enable = false;
421 RTE_PER_LCORE(timestamp_qskew) = 0;
422 dynf = rte_mbuf_dynflag_lookup
423 (RTE_MBUF_DYNFLAG_TX_TIMESTAMP_NAME, NULL);
425 timestamp_mask = 1ULL << dynf;
426 dynf = rte_mbuf_dynfield_lookup
427 (RTE_MBUF_DYNFIELD_TIMESTAMP_NAME, NULL);
429 timestamp_off = dynf;
430 timestamp_enable = tx_pkt_times_inter &&
432 timestamp_off >= 0 &&
433 !rte_eth_read_clock(pi, ×tamp_initial[pi]);
434 if (timestamp_enable)
435 timestamp_init_req++;
436 /* Make sure all settings are visible on forwarding cores.*/
440 struct fwd_engine tx_only_engine = {
441 .fwd_mode_name = "txonly",
442 .port_fwd_begin = tx_only_begin,
443 .port_fwd_end = NULL,
444 .packet_fwd = pkt_burst_transmit,