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 = (192U << 24) | (18 << 16) | (0 << 8) | 1; 49 uint32_t tx_ip_dst_addr = (192U << 24) | (18 << 16) | (0 << 8) | 2; 50 51 #define IP_DEFTTL 64 /* from RFC 1340. */ 52 #define IP_VERSION 0x40 53 #define IP_HDRLEN 0x05 /* default IP header length == five 32-bits words. */ 54 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN) 55 56 static struct ipv4_hdr pkt_ip_hdr; /**< IP header of transmitted packets. */ 57 RTE_DEFINE_PER_LCORE(uint8_t, _ip_var); /**< IP address variation */ 58 static struct udp_hdr pkt_udp_hdr; /**< UDP header of transmitted packets. */ 59 60 static void 61 copy_buf_to_pkt_segs(void* buf, unsigned len, struct rte_mbuf *pkt, 62 unsigned offset) 63 { 64 struct rte_mbuf *seg; 65 void *seg_buf; 66 unsigned copy_len; 67 68 seg = pkt; 69 while (offset >= seg->data_len) { 70 offset -= seg->data_len; 71 seg = seg->next; 72 } 73 copy_len = seg->data_len - offset; 74 seg_buf = rte_pktmbuf_mtod_offset(seg, char *, offset); 75 while (len > copy_len) { 76 rte_memcpy(seg_buf, buf, (size_t) copy_len); 77 len -= copy_len; 78 buf = ((char*) buf + copy_len); 79 seg = seg->next; 80 seg_buf = rte_pktmbuf_mtod(seg, char *); 81 copy_len = seg->data_len; 82 } 83 rte_memcpy(seg_buf, buf, (size_t) len); 84 } 85 86 static inline void 87 copy_buf_to_pkt(void* buf, unsigned len, struct rte_mbuf *pkt, unsigned offset) 88 { 89 if (offset + len <= pkt->data_len) { 90 rte_memcpy(rte_pktmbuf_mtod_offset(pkt, char *, offset), 91 buf, (size_t) len); 92 return; 93 } 94 copy_buf_to_pkt_segs(buf, len, pkt, offset); 95 } 96 97 static void 98 setup_pkt_udp_ip_headers(struct ipv4_hdr *ip_hdr, 99 struct udp_hdr *udp_hdr, 100 uint16_t pkt_data_len) 101 { 102 uint16_t *ptr16; 103 uint32_t ip_cksum; 104 uint16_t pkt_len; 105 106 /* 107 * Initialize UDP header. 108 */ 109 pkt_len = (uint16_t) (pkt_data_len + sizeof(struct udp_hdr)); 110 udp_hdr->src_port = rte_cpu_to_be_16(tx_udp_src_port); 111 udp_hdr->dst_port = rte_cpu_to_be_16(tx_udp_dst_port); 112 udp_hdr->dgram_len = RTE_CPU_TO_BE_16(pkt_len); 113 udp_hdr->dgram_cksum = 0; /* No UDP checksum. */ 114 115 /* 116 * Initialize IP header. 117 */ 118 pkt_len = (uint16_t) (pkt_len + sizeof(struct ipv4_hdr)); 119 ip_hdr->version_ihl = IP_VHL_DEF; 120 ip_hdr->type_of_service = 0; 121 ip_hdr->fragment_offset = 0; 122 ip_hdr->time_to_live = IP_DEFTTL; 123 ip_hdr->next_proto_id = IPPROTO_UDP; 124 ip_hdr->packet_id = 0; 125 ip_hdr->total_length = RTE_CPU_TO_BE_16(pkt_len); 126 ip_hdr->src_addr = rte_cpu_to_be_32(tx_ip_src_addr); 127 ip_hdr->dst_addr = rte_cpu_to_be_32(tx_ip_dst_addr); 128 129 /* 130 * Compute IP header checksum. 131 */ 132 ptr16 = (unaligned_uint16_t*) ip_hdr; 133 ip_cksum = 0; 134 ip_cksum += ptr16[0]; ip_cksum += ptr16[1]; 135 ip_cksum += ptr16[2]; ip_cksum += ptr16[3]; 136 ip_cksum += ptr16[4]; 137 ip_cksum += ptr16[6]; ip_cksum += ptr16[7]; 138 ip_cksum += ptr16[8]; ip_cksum += ptr16[9]; 139 140 /* 141 * Reduce 32 bit checksum to 16 bits and complement it. 142 */ 143 ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) + 144 (ip_cksum & 0x0000FFFF); 145 if (ip_cksum > 65535) 146 ip_cksum -= 65535; 147 ip_cksum = (~ip_cksum) & 0x0000FFFF; 148 if (ip_cksum == 0) 149 ip_cksum = 0xFFFF; 150 ip_hdr->hdr_checksum = (uint16_t) ip_cksum; 151 } 152 153 static inline bool 154 pkt_burst_prepare(struct rte_mbuf *pkt, struct rte_mempool *mbp, 155 struct ether_hdr *eth_hdr, const uint16_t vlan_tci, 156 const uint16_t vlan_tci_outer, const uint64_t ol_flags) 157 { 158 struct rte_mbuf *pkt_segs[RTE_MAX_SEGS_PER_PKT]; 159 uint8_t ip_var = RTE_PER_LCORE(_ip_var); 160 struct rte_mbuf *pkt_seg; 161 uint32_t nb_segs, pkt_len; 162 uint8_t i; 163 164 if (unlikely(tx_pkt_split == TX_PKT_SPLIT_RND)) 165 nb_segs = random() % tx_pkt_nb_segs + 1; 166 else 167 nb_segs = tx_pkt_nb_segs; 168 169 if (nb_segs > 1) { 170 if (rte_mempool_get_bulk(mbp, (void **)pkt_segs, nb_segs - 1)) 171 return false; 172 } 173 174 rte_pktmbuf_reset_headroom(pkt); 175 pkt->data_len = tx_pkt_seg_lengths[0]; 176 pkt->ol_flags = ol_flags; 177 pkt->vlan_tci = vlan_tci; 178 pkt->vlan_tci_outer = vlan_tci_outer; 179 pkt->l2_len = sizeof(struct ether_hdr); 180 pkt->l3_len = sizeof(struct ipv4_hdr); 181 182 pkt_len = pkt->data_len; 183 pkt_seg = pkt; 184 for (i = 1; i < nb_segs; i++) { 185 pkt_seg->next = pkt_segs[i - 1]; 186 pkt_seg = pkt_seg->next; 187 pkt_seg->data_len = tx_pkt_seg_lengths[i]; 188 pkt_len += pkt_seg->data_len; 189 } 190 pkt_seg->next = NULL; /* Last segment of packet. */ 191 /* 192 * Copy headers in first packet segment(s). 193 */ 194 copy_buf_to_pkt(eth_hdr, sizeof(*eth_hdr), pkt, 0); 195 copy_buf_to_pkt(&pkt_ip_hdr, sizeof(pkt_ip_hdr), pkt, 196 sizeof(struct ether_hdr)); 197 if (txonly_multi_flow) { 198 struct ipv4_hdr *ip_hdr; 199 uint32_t addr; 200 201 ip_hdr = rte_pktmbuf_mtod_offset(pkt, 202 struct ipv4_hdr *, 203 sizeof(struct ether_hdr)); 204 /* 205 * Generate multiple flows by varying IP src addr. This 206 * enables packets are well distributed by RSS in 207 * receiver side if any and txonly mode can be a decent 208 * packet generator for developer's quick performance 209 * regression test. 210 */ 211 addr = (tx_ip_dst_addr | (ip_var++ << 8)) + rte_lcore_id(); 212 ip_hdr->src_addr = rte_cpu_to_be_32(addr); 213 } 214 copy_buf_to_pkt(&pkt_udp_hdr, sizeof(pkt_udp_hdr), pkt, 215 sizeof(struct ether_hdr) + 216 sizeof(struct ipv4_hdr)); 217 /* 218 * Complete first mbuf of packet and append it to the 219 * burst of packets to be transmitted. 220 */ 221 pkt->nb_segs = nb_segs; 222 pkt->pkt_len = pkt_len; 223 224 return true; 225 } 226 227 /* 228 * Transmit a burst of multi-segments packets. 229 */ 230 static void 231 pkt_burst_transmit(struct fwd_stream *fs) 232 { 233 struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; 234 struct rte_port *txp; 235 struct rte_mbuf *pkt; 236 struct rte_mempool *mbp; 237 struct ether_hdr eth_hdr; 238 uint16_t nb_tx; 239 uint16_t nb_pkt; 240 uint16_t vlan_tci, vlan_tci_outer; 241 uint32_t retry; 242 uint64_t ol_flags = 0; 243 uint64_t tx_offloads; 244 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES 245 uint64_t start_tsc; 246 uint64_t end_tsc; 247 uint64_t core_cycles; 248 #endif 249 250 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES 251 start_tsc = rte_rdtsc(); 252 #endif 253 254 mbp = current_fwd_lcore()->mbp; 255 txp = &ports[fs->tx_port]; 256 tx_offloads = txp->dev_conf.txmode.offloads; 257 vlan_tci = txp->tx_vlan_id; 258 vlan_tci_outer = txp->tx_vlan_id_outer; 259 if (tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT) 260 ol_flags = PKT_TX_VLAN_PKT; 261 if (tx_offloads & DEV_TX_OFFLOAD_QINQ_INSERT) 262 ol_flags |= PKT_TX_QINQ_PKT; 263 if (tx_offloads & DEV_TX_OFFLOAD_MACSEC_INSERT) 264 ol_flags |= PKT_TX_MACSEC; 265 266 /* 267 * Initialize Ethernet header. 268 */ 269 ether_addr_copy(&peer_eth_addrs[fs->peer_addr], ð_hdr.d_addr); 270 ether_addr_copy(&ports[fs->tx_port].eth_addr, ð_hdr.s_addr); 271 eth_hdr.ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4); 272 273 if (rte_mempool_get_bulk(mbp, (void **)pkts_burst, 274 nb_pkt_per_burst) == 0) { 275 for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) { 276 if (unlikely(!pkt_burst_prepare(pkts_burst[nb_pkt], mbp, 277 ð_hdr, vlan_tci, 278 vlan_tci_outer, 279 ol_flags))) { 280 rte_mempool_put_bulk(mbp, 281 (void **)&pkts_burst[nb_pkt], 282 nb_pkt_per_burst - nb_pkt); 283 break; 284 } 285 } 286 } else { 287 for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) { 288 pkt = rte_mbuf_raw_alloc(mbp); 289 if (pkt == NULL) 290 break; 291 if (unlikely(!pkt_burst_prepare(pkt, mbp, ð_hdr, 292 vlan_tci, 293 vlan_tci_outer, 294 ol_flags))) { 295 rte_pktmbuf_free(pkt); 296 break; 297 } 298 pkts_burst[nb_pkt] = pkt; 299 } 300 } 301 302 if (nb_pkt == 0) 303 return; 304 305 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_pkt); 306 /* 307 * Retry if necessary 308 */ 309 if (unlikely(nb_tx < nb_pkt) && fs->retry_enabled) { 310 retry = 0; 311 while (nb_tx < nb_pkt && retry++ < burst_tx_retry_num) { 312 rte_delay_us(burst_tx_delay_time); 313 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue, 314 &pkts_burst[nb_tx], nb_pkt - nb_tx); 315 } 316 } 317 fs->tx_packets += nb_tx; 318 319 if (txonly_multi_flow) 320 RTE_PER_LCORE(_ip_var) += nb_tx; 321 322 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS 323 fs->tx_burst_stats.pkt_burst_spread[nb_tx]++; 324 #endif 325 if (unlikely(nb_tx < nb_pkt)) { 326 if (verbose_level > 0 && fs->fwd_dropped == 0) 327 printf("port %d tx_queue %d - drop " 328 "(nb_pkt:%u - nb_tx:%u)=%u packets\n", 329 fs->tx_port, fs->tx_queue, 330 (unsigned) nb_pkt, (unsigned) nb_tx, 331 (unsigned) (nb_pkt - nb_tx)); 332 fs->fwd_dropped += (nb_pkt - nb_tx); 333 do { 334 rte_pktmbuf_free(pkts_burst[nb_tx]); 335 } while (++nb_tx < nb_pkt); 336 } 337 338 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES 339 end_tsc = rte_rdtsc(); 340 core_cycles = (end_tsc - start_tsc); 341 fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles); 342 #endif 343 } 344 345 static void 346 tx_only_begin(__attribute__((unused)) portid_t pi) 347 { 348 uint16_t pkt_data_len; 349 350 pkt_data_len = (uint16_t) (tx_pkt_length - (sizeof(struct ether_hdr) + 351 sizeof(struct ipv4_hdr) + 352 sizeof(struct udp_hdr))); 353 setup_pkt_udp_ip_headers(&pkt_ip_hdr, &pkt_udp_hdr, pkt_data_len); 354 } 355 356 struct fwd_engine tx_only_engine = { 357 .fwd_mode_name = "txonly", 358 .port_fwd_begin = tx_only_begin, 359 .port_fwd_end = NULL, 360 .packet_fwd = pkt_burst_transmit, 361 }; 362