1 /* 2 * Copyright (C) 2017 THL A29 Limited, a Tencent company. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 */ 26 #include <assert.h> 27 #include <unistd.h> 28 #include <sys/mman.h> 29 #include <errno.h> 30 31 #include <rte_common.h> 32 #include <rte_byteorder.h> 33 #include <rte_log.h> 34 #include <rte_memory.h> 35 #include <rte_memcpy.h> 36 #include <rte_memzone.h> 37 #include <rte_config.h> 38 #include <rte_eal.h> 39 #include <rte_pci.h> 40 #include <rte_mbuf.h> 41 #include <rte_memory.h> 42 #include <rte_lcore.h> 43 #include <rte_launch.h> 44 #include <rte_ethdev.h> 45 #include <rte_debug.h> 46 #include <rte_common.h> 47 #include <rte_ether.h> 48 #include <rte_malloc.h> 49 #include <rte_cycles.h> 50 #include <rte_timer.h> 51 #include <rte_thash.h> 52 #include <rte_ip.h> 53 #include <rte_tcp.h> 54 #include <rte_udp.h> 55 56 #include "ff_dpdk_if.h" 57 #include "ff_dpdk_pcap.h" 58 #include "ff_dpdk_kni.h" 59 #include "ff_config.h" 60 #include "ff_veth.h" 61 #include "ff_host_interface.h" 62 #include "ff_msg.h" 63 #include "ff_api.h" 64 #include "ff_memory.h" 65 66 #ifdef FF_KNI 67 #define KNI_MBUF_MAX 2048 68 #define KNI_QUEUE_SIZE 2048 69 70 static int enable_kni; 71 static int kni_accept; 72 #endif 73 74 static int numa_on; 75 76 static unsigned idle_sleep; 77 static unsigned pkt_tx_delay; 78 79 static struct rte_timer freebsd_clock; 80 81 // Mellanox Linux's driver key 82 static uint8_t default_rsskey_40bytes[40] = { 83 0xd1, 0x81, 0xc6, 0x2c, 0xf7, 0xf4, 0xdb, 0x5b, 84 0x19, 0x83, 0xa2, 0xfc, 0x94, 0x3e, 0x1a, 0xdb, 85 0xd9, 0x38, 0x9e, 0x6b, 0xd1, 0x03, 0x9c, 0x2c, 86 0xa7, 0x44, 0x99, 0xad, 0x59, 0x3d, 0x56, 0xd9, 87 0xf3, 0x25, 0x3c, 0x06, 0x2a, 0xdc, 0x1f, 0xfc 88 }; 89 90 struct lcore_conf lcore_conf; 91 92 struct rte_mempool *pktmbuf_pool[NB_SOCKETS]; 93 94 static struct rte_ring **dispatch_ring[RTE_MAX_ETHPORTS]; 95 static dispatch_func_t packet_dispatcher; 96 97 static uint16_t rss_reta_size[RTE_MAX_ETHPORTS]; 98 99 static inline int send_single_packet(struct rte_mbuf *m, uint8_t port); 100 101 struct ff_msg_ring { 102 char ring_name[2][RTE_RING_NAMESIZE]; 103 /* ring[0] for lcore recv msg, other send */ 104 /* ring[1] for lcore send msg, other read */ 105 struct rte_ring *ring[2]; 106 } __rte_cache_aligned; 107 108 static struct ff_msg_ring msg_ring[RTE_MAX_LCORE]; 109 static struct rte_mempool *message_pool; 110 static struct ff_dpdk_if_context *veth_ctx[RTE_MAX_ETHPORTS]; 111 112 static struct ff_top_args ff_top_status; 113 static struct ff_traffic_args ff_traffic; 114 extern void ff_hardclock(void); 115 116 static void 117 ff_hardclock_job(__rte_unused struct rte_timer *timer, 118 __rte_unused void *arg) { 119 ff_hardclock(); 120 ff_update_current_ts(); 121 } 122 123 struct ff_dpdk_if_context * 124 ff_dpdk_register_if(void *sc, void *ifp, struct ff_port_cfg *cfg) 125 { 126 struct ff_dpdk_if_context *ctx; 127 128 ctx = calloc(1, sizeof(struct ff_dpdk_if_context)); 129 if (ctx == NULL) 130 return NULL; 131 132 ctx->sc = sc; 133 ctx->ifp = ifp; 134 ctx->port_id = cfg->port_id; 135 ctx->hw_features = cfg->hw_features; 136 137 return ctx; 138 } 139 140 void 141 ff_dpdk_deregister_if(struct ff_dpdk_if_context *ctx) 142 { 143 free(ctx); 144 } 145 146 static void 147 check_all_ports_link_status(void) 148 { 149 #define CHECK_INTERVAL 100 /* 100ms */ 150 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 151 152 uint16_t portid; 153 uint8_t count, all_ports_up, print_flag = 0; 154 struct rte_eth_link link; 155 156 printf("\nChecking link status"); 157 fflush(stdout); 158 159 int i, nb_ports; 160 nb_ports = ff_global_cfg.dpdk.nb_ports; 161 for (count = 0; count <= MAX_CHECK_TIME; count++) { 162 all_ports_up = 1; 163 for (i = 0; i < nb_ports; i++) { 164 uint16_t portid = ff_global_cfg.dpdk.portid_list[i]; 165 memset(&link, 0, sizeof(link)); 166 rte_eth_link_get_nowait(portid, &link); 167 168 /* print link status if flag set */ 169 if (print_flag == 1) { 170 if (link.link_status) { 171 printf("Port %d Link Up - speed %u " 172 "Mbps - %s\n", (int)portid, 173 (unsigned)link.link_speed, 174 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ? 175 ("full-duplex") : ("half-duplex\n")); 176 } else { 177 printf("Port %d Link Down\n", (int)portid); 178 } 179 continue; 180 } 181 /* clear all_ports_up flag if any link down */ 182 if (link.link_status == 0) { 183 all_ports_up = 0; 184 break; 185 } 186 } 187 188 /* after finally printing all link status, get out */ 189 if (print_flag == 1) 190 break; 191 192 if (all_ports_up == 0) { 193 printf("."); 194 fflush(stdout); 195 rte_delay_ms(CHECK_INTERVAL); 196 } 197 198 /* set the print_flag if all ports up or timeout */ 199 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 200 print_flag = 1; 201 printf("done\n"); 202 } 203 } 204 } 205 206 static int 207 init_lcore_conf(void) 208 { 209 uint8_t nb_dev_ports = rte_eth_dev_count_avail(); 210 if (nb_dev_ports == 0) { 211 rte_exit(EXIT_FAILURE, "No probed ethernet devices\n"); 212 } 213 214 if (ff_global_cfg.dpdk.max_portid >= nb_dev_ports) { 215 rte_exit(EXIT_FAILURE, "this machine doesn't have port %d.\n", 216 ff_global_cfg.dpdk.max_portid); 217 } 218 219 lcore_conf.port_cfgs = ff_global_cfg.dpdk.port_cfgs; 220 lcore_conf.proc_id = ff_global_cfg.dpdk.proc_id; 221 222 uint16_t proc_id; 223 for (proc_id = 0; proc_id < ff_global_cfg.dpdk.nb_procs; proc_id++) { 224 uint16_t lcore_id = ff_global_cfg.dpdk.proc_lcore[proc_id]; 225 if (!lcore_config[lcore_id].detected) { 226 rte_exit(EXIT_FAILURE, "lcore %u unavailable\n", lcore_id); 227 } 228 } 229 230 uint16_t socket_id = 0; 231 if (numa_on) { 232 socket_id = rte_lcore_to_socket_id(rte_lcore_id()); 233 } 234 235 lcore_conf.socket_id = socket_id; 236 237 uint16_t lcore_id = ff_global_cfg.dpdk.proc_lcore[lcore_conf.proc_id]; 238 int j; 239 for (j = 0; j < ff_global_cfg.dpdk.nb_ports; ++j) { 240 uint16_t port_id = ff_global_cfg.dpdk.portid_list[j]; 241 struct ff_port_cfg *pconf = &ff_global_cfg.dpdk.port_cfgs[port_id]; 242 243 int queueid = -1; 244 int i; 245 for (i = 0; i < pconf->nb_lcores; i++) { 246 if (pconf->lcore_list[i] == lcore_id) { 247 queueid = i; 248 } 249 } 250 if (queueid < 0) { 251 continue; 252 } 253 printf("lcore: %u, port: %u, queue: %u\n", lcore_id, port_id, queueid); 254 uint16_t nb_rx_queue = lcore_conf.nb_rx_queue; 255 lcore_conf.rx_queue_list[nb_rx_queue].port_id = port_id; 256 lcore_conf.rx_queue_list[nb_rx_queue].queue_id = queueid; 257 lcore_conf.nb_rx_queue++; 258 259 lcore_conf.tx_queue_id[port_id] = queueid; 260 lcore_conf.tx_port_id[lcore_conf.nb_tx_port] = port_id; 261 lcore_conf.nb_tx_port++; 262 263 lcore_conf.pcap[port_id] = pconf->pcap; 264 lcore_conf.nb_queue_list[port_id] = pconf->nb_lcores; 265 } 266 267 if (lcore_conf.nb_rx_queue == 0) { 268 rte_exit(EXIT_FAILURE, "lcore %u has nothing to do\n", lcore_id); 269 } 270 271 return 0; 272 } 273 274 static int 275 init_mem_pool(void) 276 { 277 uint8_t nb_ports = ff_global_cfg.dpdk.nb_ports; 278 uint32_t nb_lcores = ff_global_cfg.dpdk.nb_procs; 279 uint32_t nb_tx_queue = nb_lcores; 280 uint32_t nb_rx_queue = lcore_conf.nb_rx_queue * nb_lcores; 281 282 unsigned nb_mbuf = RTE_ALIGN_CEIL ( 283 (nb_rx_queue*RX_QUEUE_SIZE + 284 nb_ports*nb_lcores*MAX_PKT_BURST + 285 nb_ports*nb_tx_queue*TX_QUEUE_SIZE + 286 nb_lcores*MEMPOOL_CACHE_SIZE + 287 #ifdef FF_KNI 288 nb_ports*KNI_MBUF_MAX + 289 nb_ports*KNI_QUEUE_SIZE + 290 #endif 291 nb_lcores*nb_ports*DISPATCH_RING_SIZE), 292 (unsigned)8192); 293 294 unsigned socketid = 0; 295 uint16_t i, lcore_id; 296 char s[64]; 297 298 for (i = 0; i < ff_global_cfg.dpdk.nb_procs; i++) { 299 lcore_id = ff_global_cfg.dpdk.proc_lcore[i]; 300 if (numa_on) { 301 socketid = rte_lcore_to_socket_id(lcore_id); 302 } 303 304 if (socketid >= NB_SOCKETS) { 305 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n", 306 socketid, i, NB_SOCKETS); 307 } 308 309 if (pktmbuf_pool[socketid] != NULL) { 310 continue; 311 } 312 313 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 314 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid); 315 pktmbuf_pool[socketid] = 316 rte_pktmbuf_pool_create(s, nb_mbuf, 317 MEMPOOL_CACHE_SIZE, 0, 318 RTE_MBUF_DEFAULT_BUF_SIZE, socketid); 319 } else { 320 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid); 321 pktmbuf_pool[socketid] = rte_mempool_lookup(s); 322 } 323 324 if (pktmbuf_pool[socketid] == NULL) { 325 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool on socket %d\n", socketid); 326 } else { 327 printf("create mbuf pool on socket %d\n", socketid); 328 } 329 330 #ifdef FF_USE_PAGE_ARRAY 331 nb_mbuf = RTE_ALIGN_CEIL ( 332 nb_ports*nb_lcores*MAX_PKT_BURST + 333 nb_ports*nb_tx_queue*TX_QUEUE_SIZE + 334 nb_lcores*MEMPOOL_CACHE_SIZE, 335 (unsigned)4096); 336 ff_init_ref_pool(nb_mbuf, socketid); 337 #endif 338 } 339 340 return 0; 341 } 342 343 static struct rte_ring * 344 create_ring(const char *name, unsigned count, int socket_id, unsigned flags) 345 { 346 struct rte_ring *ring; 347 348 if (name == NULL) { 349 rte_exit(EXIT_FAILURE, "create ring failed, no name!\n"); 350 } 351 352 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 353 ring = rte_ring_create(name, count, socket_id, flags); 354 } else { 355 ring = rte_ring_lookup(name); 356 } 357 358 if (ring == NULL) { 359 rte_exit(EXIT_FAILURE, "create ring:%s failed!\n", name); 360 } 361 362 return ring; 363 } 364 365 static int 366 init_dispatch_ring(void) 367 { 368 int j; 369 char name_buf[RTE_RING_NAMESIZE]; 370 int queueid; 371 372 unsigned socketid = lcore_conf.socket_id; 373 374 /* Create ring according to ports actually being used. */ 375 int nb_ports = ff_global_cfg.dpdk.nb_ports; 376 for (j = 0; j < nb_ports; j++) { 377 uint16_t portid = ff_global_cfg.dpdk.portid_list[j]; 378 struct ff_port_cfg *pconf = &ff_global_cfg.dpdk.port_cfgs[portid]; 379 int nb_queues = pconf->nb_lcores; 380 if (dispatch_ring[portid] == NULL) { 381 snprintf(name_buf, RTE_RING_NAMESIZE, "ring_ptr_p%d", portid); 382 383 dispatch_ring[portid] = rte_zmalloc(name_buf, 384 sizeof(struct rte_ring *) * nb_queues, 385 RTE_CACHE_LINE_SIZE); 386 if (dispatch_ring[portid] == NULL) { 387 rte_exit(EXIT_FAILURE, "rte_zmalloc(%s (struct rte_ring*)) " 388 "failed\n", name_buf); 389 } 390 } 391 392 for(queueid = 0; queueid < nb_queues; ++queueid) { 393 snprintf(name_buf, RTE_RING_NAMESIZE, "dispatch_ring_p%d_q%d", 394 portid, queueid); 395 dispatch_ring[portid][queueid] = create_ring(name_buf, 396 DISPATCH_RING_SIZE, socketid, RING_F_SC_DEQ); 397 398 if (dispatch_ring[portid][queueid] == NULL) 399 rte_panic("create ring:%s failed!\n", name_buf); 400 401 printf("create ring:%s success, %u ring entries are now free!\n", 402 name_buf, rte_ring_free_count(dispatch_ring[portid][queueid])); 403 } 404 } 405 406 return 0; 407 } 408 409 static void 410 ff_msg_init(struct rte_mempool *mp, 411 __attribute__((unused)) void *opaque_arg, 412 void *obj, __attribute__((unused)) unsigned i) 413 { 414 struct ff_msg *msg = (struct ff_msg *)obj; 415 msg->msg_type = FF_UNKNOWN; 416 msg->buf_addr = (char *)msg + sizeof(struct ff_msg); 417 msg->buf_len = mp->elt_size - sizeof(struct ff_msg); 418 } 419 420 static int 421 init_msg_ring(void) 422 { 423 uint16_t i; 424 uint16_t nb_procs = ff_global_cfg.dpdk.nb_procs; 425 unsigned socketid = lcore_conf.socket_id; 426 427 /* Create message buffer pool */ 428 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 429 message_pool = rte_mempool_create(FF_MSG_POOL, 430 MSG_RING_SIZE * 2 * nb_procs, 431 MAX_MSG_BUF_SIZE, MSG_RING_SIZE / 2, 0, 432 NULL, NULL, ff_msg_init, NULL, 433 socketid, 0); 434 } else { 435 message_pool = rte_mempool_lookup(FF_MSG_POOL); 436 } 437 438 if (message_pool == NULL) { 439 rte_panic("Create msg mempool failed\n"); 440 } 441 442 for(i = 0; i < nb_procs; ++i) { 443 snprintf(msg_ring[i].ring_name[0], RTE_RING_NAMESIZE, 444 "%s%u", FF_MSG_RING_IN, i); 445 snprintf(msg_ring[i].ring_name[1], RTE_RING_NAMESIZE, 446 "%s%u", FF_MSG_RING_OUT, i); 447 448 msg_ring[i].ring[0] = create_ring(msg_ring[i].ring_name[0], 449 MSG_RING_SIZE, socketid, RING_F_SP_ENQ | RING_F_SC_DEQ); 450 if (msg_ring[i].ring[0] == NULL) 451 rte_panic("create ring::%s failed!\n", msg_ring[i].ring_name[0]); 452 453 msg_ring[i].ring[1] = create_ring(msg_ring[i].ring_name[1], 454 MSG_RING_SIZE, socketid, RING_F_SP_ENQ | RING_F_SC_DEQ); 455 if (msg_ring[i].ring[1] == NULL) 456 rte_panic("create ring::%s failed!\n", msg_ring[i].ring_name[0]); 457 } 458 459 return 0; 460 } 461 462 #ifdef FF_KNI 463 static int 464 init_kni(void) 465 { 466 int nb_ports = rte_eth_dev_count_avail(); 467 kni_accept = 0; 468 if(strcasecmp(ff_global_cfg.kni.method, "accept") == 0) 469 kni_accept = 1; 470 471 ff_kni_init(nb_ports, ff_global_cfg.kni.tcp_port, 472 ff_global_cfg.kni.udp_port); 473 474 unsigned socket_id = lcore_conf.socket_id; 475 struct rte_mempool *mbuf_pool = pktmbuf_pool[socket_id]; 476 477 nb_ports = ff_global_cfg.dpdk.nb_ports; 478 int i, ret; 479 for (i = 0; i < nb_ports; i++) { 480 uint16_t port_id = ff_global_cfg.dpdk.portid_list[i]; 481 ff_kni_alloc(port_id, socket_id, mbuf_pool, KNI_QUEUE_SIZE); 482 } 483 484 return 0; 485 } 486 #endif 487 488 static void 489 set_rss_table(uint16_t port_id, uint16_t reta_size, uint16_t nb_queues) 490 { 491 if (reta_size == 0) { 492 return; 493 } 494 495 int reta_conf_size = RTE_MAX(1, reta_size / RTE_RETA_GROUP_SIZE); 496 struct rte_eth_rss_reta_entry64 reta_conf[reta_conf_size]; 497 498 /* config HW indirection table */ 499 unsigned i, j, hash=0; 500 for (i = 0; i < reta_conf_size; i++) { 501 reta_conf[i].mask = ~0ULL; 502 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++) { 503 reta_conf[i].reta[j] = hash++ % nb_queues; 504 } 505 } 506 507 if (rte_eth_dev_rss_reta_update(port_id, reta_conf, reta_size)) { 508 rte_exit(EXIT_FAILURE, "port[%d], failed to update rss table\n", 509 port_id); 510 } 511 } 512 513 static int 514 init_port_start(void) 515 { 516 int nb_ports = ff_global_cfg.dpdk.nb_ports; 517 unsigned socketid = 0; 518 struct rte_mempool *mbuf_pool; 519 uint16_t i; 520 521 for (i = 0; i < nb_ports; i++) { 522 uint16_t port_id = ff_global_cfg.dpdk.portid_list[i]; 523 struct ff_port_cfg *pconf = &ff_global_cfg.dpdk.port_cfgs[port_id]; 524 uint16_t nb_queues = pconf->nb_lcores; 525 526 struct rte_eth_dev_info dev_info; 527 struct rte_eth_conf port_conf = {0}; 528 struct rte_eth_rxconf rxq_conf; 529 struct rte_eth_txconf txq_conf; 530 531 rte_eth_dev_info_get(port_id, &dev_info); 532 533 if (nb_queues > dev_info.max_rx_queues) { 534 rte_exit(EXIT_FAILURE, "num_procs[%d] bigger than max_rx_queues[%d]\n", 535 nb_queues, 536 dev_info.max_rx_queues); 537 } 538 539 if (nb_queues > dev_info.max_tx_queues) { 540 rte_exit(EXIT_FAILURE, "num_procs[%d] bigger than max_tx_queues[%d]\n", 541 nb_queues, 542 dev_info.max_tx_queues); 543 } 544 545 struct ether_addr addr; 546 rte_eth_macaddr_get(port_id, &addr); 547 printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8 548 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n", 549 (unsigned)port_id, 550 addr.addr_bytes[0], addr.addr_bytes[1], 551 addr.addr_bytes[2], addr.addr_bytes[3], 552 addr.addr_bytes[4], addr.addr_bytes[5]); 553 554 rte_memcpy(pconf->mac, 555 addr.addr_bytes, ETHER_ADDR_LEN); 556 557 /* Set RSS mode */ 558 uint64_t default_rss_hf = ETH_RSS_PROTO_MASK; 559 port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS; 560 port_conf.rx_adv_conf.rss_conf.rss_hf = default_rss_hf; 561 port_conf.rx_adv_conf.rss_conf.rss_key = default_rsskey_40bytes; 562 port_conf.rx_adv_conf.rss_conf.rss_key_len = 40; 563 port_conf.rx_adv_conf.rss_conf.rss_hf &= dev_info.flow_type_rss_offloads; 564 if (port_conf.rx_adv_conf.rss_conf.rss_hf != 565 ETH_RSS_PROTO_MASK) { 566 printf("Port %u modified RSS hash function based on hardware support," 567 "requested:%#"PRIx64" configured:%#"PRIx64"\n", 568 port_id, default_rss_hf, 569 port_conf.rx_adv_conf.rss_conf.rss_hf); 570 } 571 572 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) { 573 port_conf.txmode.offloads |= 574 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 575 } 576 577 /* Set Rx VLAN stripping */ 578 if (ff_global_cfg.dpdk.vlan_strip) { 579 if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_VLAN_STRIP) { 580 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_STRIP; 581 } 582 } 583 584 /* Enable HW CRC stripping */ 585 port_conf.rxmode.offloads &= ~DEV_RX_OFFLOAD_KEEP_CRC; 586 587 /* FIXME: Enable TCP LRO ?*/ 588 #if 0 589 if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_LRO) { 590 printf("LRO is supported\n"); 591 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_TCP_LRO; 592 pconf->hw_features.rx_lro = 1; 593 } 594 #endif 595 596 /* Set Rx checksum checking */ 597 if ((dev_info.rx_offload_capa & DEV_RX_OFFLOAD_IPV4_CKSUM) && 598 (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_UDP_CKSUM) && 599 (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_CKSUM)) { 600 printf("RX checksum offload supported\n"); 601 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_CHECKSUM; 602 pconf->hw_features.rx_csum = 1; 603 } 604 605 if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)) { 606 printf("TX ip checksum offload supported\n"); 607 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM; 608 pconf->hw_features.tx_csum_ip = 1; 609 } 610 611 if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_CKSUM) && 612 (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_CKSUM)) { 613 printf("TX TCP&UDP checksum offload supported\n"); 614 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM | DEV_TX_OFFLOAD_TCP_CKSUM; 615 pconf->hw_features.tx_csum_l4 = 1; 616 } 617 618 if (ff_global_cfg.dpdk.tso) { 619 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_TSO) { 620 printf("TSO is supported\n"); 621 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_TSO; 622 pconf->hw_features.tx_tso = 1; 623 } 624 } else { 625 printf("TSO is disabled\n"); 626 } 627 628 if (dev_info.reta_size) { 629 /* reta size must be power of 2 */ 630 assert((dev_info.reta_size & (dev_info.reta_size - 1)) == 0); 631 632 rss_reta_size[port_id] = dev_info.reta_size; 633 printf("port[%d]: rss table size: %d\n", port_id, 634 dev_info.reta_size); 635 } 636 637 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 638 continue; 639 } 640 641 int ret = rte_eth_dev_configure(port_id, nb_queues, nb_queues, &port_conf); 642 if (ret != 0) { 643 return ret; 644 } 645 646 static uint16_t nb_rxd = RX_QUEUE_SIZE; 647 static uint16_t nb_txd = TX_QUEUE_SIZE; 648 ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd); 649 if (ret < 0) 650 printf("Could not adjust number of descriptors " 651 "for port%u (%d)\n", (unsigned)port_id, ret); 652 653 uint16_t q; 654 for (q = 0; q < nb_queues; q++) { 655 if (numa_on) { 656 uint16_t lcore_id = lcore_conf.port_cfgs[port_id].lcore_list[q]; 657 socketid = rte_lcore_to_socket_id(lcore_id); 658 } 659 mbuf_pool = pktmbuf_pool[socketid]; 660 661 txq_conf = dev_info.default_txconf; 662 txq_conf.offloads = port_conf.txmode.offloads; 663 ret = rte_eth_tx_queue_setup(port_id, q, nb_txd, 664 socketid, &txq_conf); 665 if (ret < 0) { 666 return ret; 667 } 668 669 rxq_conf = dev_info.default_rxconf; 670 rxq_conf.offloads = port_conf.rxmode.offloads; 671 ret = rte_eth_rx_queue_setup(port_id, q, nb_rxd, 672 socketid, &rxq_conf, mbuf_pool); 673 if (ret < 0) { 674 return ret; 675 } 676 } 677 678 ret = rte_eth_dev_start(port_id); 679 if (ret < 0) { 680 return ret; 681 } 682 683 if (nb_queues > 1) { 684 /* set HW rss hash function to Toeplitz. */ 685 if (!rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_HASH)) { 686 struct rte_eth_hash_filter_info info = {0}; 687 info.info_type = RTE_ETH_HASH_FILTER_GLOBAL_CONFIG; 688 info.info.global_conf.hash_func = RTE_ETH_HASH_FUNCTION_TOEPLITZ; 689 690 if (rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_HASH, 691 RTE_ETH_FILTER_SET, &info) < 0) { 692 rte_exit(EXIT_FAILURE, "port[%d] set hash func failed\n", 693 port_id); 694 } 695 } 696 697 set_rss_table(port_id, dev_info.reta_size, nb_queues); 698 } 699 700 /* Enable RX in promiscuous mode for the Ethernet device. */ 701 if (ff_global_cfg.dpdk.promiscuous) { 702 rte_eth_promiscuous_enable(port_id); 703 ret = rte_eth_promiscuous_get(port_id); 704 if (ret == 1) { 705 printf("set port %u to promiscuous mode ok\n", port_id); 706 } else { 707 printf("set port %u to promiscuous mode error\n", port_id); 708 } 709 } 710 711 /* Enable pcap dump */ 712 if (pconf->pcap) { 713 ff_enable_pcap(pconf->pcap); 714 } 715 } 716 717 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 718 check_all_ports_link_status(); 719 } 720 721 return 0; 722 } 723 724 static int 725 init_clock(void) 726 { 727 rte_timer_subsystem_init(); 728 uint64_t hz = rte_get_timer_hz(); 729 uint64_t intrs = MS_PER_S/ff_global_cfg.freebsd.hz; 730 uint64_t tsc = (hz + MS_PER_S - 1) / MS_PER_S*intrs; 731 732 rte_timer_init(&freebsd_clock); 733 rte_timer_reset(&freebsd_clock, tsc, PERIODICAL, 734 rte_lcore_id(), &ff_hardclock_job, NULL); 735 736 ff_update_current_ts(); 737 738 return 0; 739 } 740 741 int 742 ff_dpdk_init(int argc, char **argv) 743 { 744 if (ff_global_cfg.dpdk.nb_procs < 1 || 745 ff_global_cfg.dpdk.nb_procs > RTE_MAX_LCORE || 746 ff_global_cfg.dpdk.proc_id >= ff_global_cfg.dpdk.nb_procs || 747 ff_global_cfg.dpdk.proc_id < 0) { 748 printf("param num_procs[%d] or proc_id[%d] error!\n", 749 ff_global_cfg.dpdk.nb_procs, 750 ff_global_cfg.dpdk.proc_id); 751 exit(1); 752 } 753 754 int ret = rte_eal_init(argc, argv); 755 if (ret < 0) { 756 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n"); 757 } 758 759 numa_on = ff_global_cfg.dpdk.numa_on; 760 761 idle_sleep = ff_global_cfg.dpdk.idle_sleep; 762 pkt_tx_delay = ff_global_cfg.dpdk.pkt_tx_delay > BURST_TX_DRAIN_US ? \ 763 BURST_TX_DRAIN_US : ff_global_cfg.dpdk.pkt_tx_delay; 764 765 init_lcore_conf(); 766 767 init_mem_pool(); 768 769 init_dispatch_ring(); 770 771 init_msg_ring(); 772 773 #ifdef FF_KNI 774 enable_kni = ff_global_cfg.kni.enable; 775 if (enable_kni) { 776 init_kni(); 777 } 778 #endif 779 780 #ifdef FF_USE_PAGE_ARRAY 781 ff_mmap_init(); 782 #endif 783 784 ret = init_port_start(); 785 if (ret < 0) { 786 rte_exit(EXIT_FAILURE, "init_port_start failed\n"); 787 } 788 789 init_clock(); 790 791 return 0; 792 } 793 794 static void 795 ff_veth_input(const struct ff_dpdk_if_context *ctx, struct rte_mbuf *pkt) 796 { 797 uint8_t rx_csum = ctx->hw_features.rx_csum; 798 if (rx_csum) { 799 if (pkt->ol_flags & (PKT_RX_IP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD)) { 800 rte_pktmbuf_free(pkt); 801 return; 802 } 803 } 804 805 /* 806 * FIXME: should we save pkt->vlan_tci 807 * if (pkt->ol_flags & PKT_RX_VLAN_PKT) 808 */ 809 810 void *data = rte_pktmbuf_mtod(pkt, void*); 811 uint16_t len = rte_pktmbuf_data_len(pkt); 812 813 void *hdr = ff_mbuf_gethdr(pkt, pkt->pkt_len, data, len, rx_csum); 814 if (hdr == NULL) { 815 rte_pktmbuf_free(pkt); 816 return; 817 } 818 819 struct rte_mbuf *pn = pkt->next; 820 void *prev = hdr; 821 while(pn != NULL) { 822 data = rte_pktmbuf_mtod(pn, void*); 823 len = rte_pktmbuf_data_len(pn); 824 825 void *mb = ff_mbuf_get(prev, data, len); 826 if (mb == NULL) { 827 ff_mbuf_free(hdr); 828 rte_pktmbuf_free(pkt); 829 return; 830 } 831 pn = pn->next; 832 prev = mb; 833 } 834 835 ff_veth_process_packet(ctx->ifp, hdr); 836 } 837 838 static enum FilterReturn 839 protocol_filter(const void *data, uint16_t len) 840 { 841 if(len < ETHER_HDR_LEN) 842 return FILTER_UNKNOWN; 843 844 const struct ether_hdr *hdr; 845 hdr = (const struct ether_hdr *)data; 846 uint16_t eth_frame_type = rte_be_to_cpu_16(hdr->ether_type); 847 848 if(eth_frame_type == ETHER_TYPE_ARP) 849 return FILTER_ARP; 850 851 #ifndef FF_KNI 852 return FILTER_UNKNOWN; 853 #else 854 if (!enable_kni) { 855 return FILTER_UNKNOWN; 856 } 857 858 if(eth_frame_type != ETHER_TYPE_IPv4 859 #ifdef INET6 860 && eth_frame_type != ETHER_TYPE_IPv6 861 #endif 862 ) 863 return FILTER_UNKNOWN; 864 865 return ff_kni_proto_filter(data + ETHER_HDR_LEN, 866 len - ETHER_HDR_LEN, eth_frame_type); 867 #endif 868 } 869 870 static inline void 871 pktmbuf_deep_attach(struct rte_mbuf *mi, const struct rte_mbuf *m) 872 { 873 struct rte_mbuf *md; 874 void *src, *dst; 875 876 dst = rte_pktmbuf_mtod(mi, void *); 877 src = rte_pktmbuf_mtod(m, void *); 878 879 mi->data_len = m->data_len; 880 rte_memcpy(dst, src, m->data_len); 881 882 mi->port = m->port; 883 mi->vlan_tci = m->vlan_tci; 884 mi->vlan_tci_outer = m->vlan_tci_outer; 885 mi->tx_offload = m->tx_offload; 886 mi->hash = m->hash; 887 mi->ol_flags = m->ol_flags; 888 mi->packet_type = m->packet_type; 889 } 890 891 /* copied from rte_pktmbuf_clone */ 892 static inline struct rte_mbuf * 893 pktmbuf_deep_clone(const struct rte_mbuf *md, 894 struct rte_mempool *mp) 895 { 896 struct rte_mbuf *mc, *mi, **prev; 897 uint32_t pktlen; 898 uint8_t nseg; 899 900 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL)) 901 return NULL; 902 903 mi = mc; 904 prev = &mi->next; 905 pktlen = md->pkt_len; 906 nseg = 0; 907 908 do { 909 nseg++; 910 pktmbuf_deep_attach(mi, md); 911 *prev = mi; 912 prev = &mi->next; 913 } while ((md = md->next) != NULL && 914 (mi = rte_pktmbuf_alloc(mp)) != NULL); 915 916 *prev = NULL; 917 mc->nb_segs = nseg; 918 mc->pkt_len = pktlen; 919 920 /* Allocation of new indirect segment failed */ 921 if (unlikely (mi == NULL)) { 922 rte_pktmbuf_free(mc); 923 return NULL; 924 } 925 926 __rte_mbuf_sanity_check(mc, 1); 927 return mc; 928 } 929 930 static inline void 931 process_packets(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **bufs, 932 uint16_t count, const struct ff_dpdk_if_context *ctx, int pkts_from_ring) 933 { 934 struct lcore_conf *qconf = &lcore_conf; 935 uint16_t nb_queues = qconf->nb_queue_list[port_id]; 936 937 uint16_t i; 938 for (i = 0; i < count; i++) { 939 struct rte_mbuf *rtem = bufs[i]; 940 941 if (unlikely(qconf->pcap[port_id] != NULL)) { 942 if (!pkts_from_ring) { 943 ff_dump_packets(qconf->pcap[port_id], rtem); 944 } 945 } 946 947 void *data = rte_pktmbuf_mtod(rtem, void*); 948 uint16_t len = rte_pktmbuf_data_len(rtem); 949 950 if (!pkts_from_ring) { 951 ff_traffic.rx_packets++; 952 ff_traffic.rx_bytes += len; 953 } 954 955 if (!pkts_from_ring && packet_dispatcher) { 956 int ret = (*packet_dispatcher)(data, &len, queue_id, nb_queues); 957 if (ret == FF_DISPATCH_RESPONSE) { 958 rte_pktmbuf_pkt_len(rtem) = rte_pktmbuf_data_len(rtem) = len; 959 send_single_packet(rtem, port_id); 960 continue; 961 } 962 963 if (ret == FF_DISPATCH_ERROR || ret >= nb_queues) { 964 rte_pktmbuf_free(rtem); 965 continue; 966 } 967 968 if (ret != queue_id) { 969 ret = rte_ring_enqueue(dispatch_ring[port_id][ret], rtem); 970 if (ret < 0) 971 rte_pktmbuf_free(rtem); 972 973 continue; 974 } 975 } 976 977 enum FilterReturn filter = protocol_filter(data, len); 978 if (filter == FILTER_ARP) { 979 struct rte_mempool *mbuf_pool; 980 struct rte_mbuf *mbuf_clone; 981 if (!pkts_from_ring) { 982 uint16_t j; 983 for(j = 0; j < nb_queues; ++j) { 984 if(j == queue_id) 985 continue; 986 987 unsigned socket_id = 0; 988 if (numa_on) { 989 uint16_t lcore_id = qconf->port_cfgs[port_id].lcore_list[j]; 990 socket_id = rte_lcore_to_socket_id(lcore_id); 991 } 992 mbuf_pool = pktmbuf_pool[socket_id]; 993 mbuf_clone = pktmbuf_deep_clone(rtem, mbuf_pool); 994 if(mbuf_clone) { 995 int ret = rte_ring_enqueue(dispatch_ring[port_id][j], 996 mbuf_clone); 997 if (ret < 0) 998 rte_pktmbuf_free(mbuf_clone); 999 } 1000 } 1001 } 1002 1003 #ifdef FF_KNI 1004 if (enable_kni && rte_eal_process_type() == RTE_PROC_PRIMARY) { 1005 mbuf_pool = pktmbuf_pool[qconf->socket_id]; 1006 mbuf_clone = pktmbuf_deep_clone(rtem, mbuf_pool); 1007 if(mbuf_clone) { 1008 ff_kni_enqueue(port_id, mbuf_clone); 1009 } 1010 } 1011 #endif 1012 ff_veth_input(ctx, rtem); 1013 #ifdef FF_KNI 1014 } else if (enable_kni && 1015 ((filter == FILTER_KNI && kni_accept) || 1016 (filter == FILTER_UNKNOWN && !kni_accept)) ) { 1017 ff_kni_enqueue(port_id, rtem); 1018 #endif 1019 } else { 1020 ff_veth_input(ctx, rtem); 1021 } 1022 } 1023 } 1024 1025 static inline int 1026 process_dispatch_ring(uint16_t port_id, uint16_t queue_id, 1027 struct rte_mbuf **pkts_burst, const struct ff_dpdk_if_context *ctx) 1028 { 1029 /* read packet from ring buf and to process */ 1030 uint16_t nb_rb; 1031 nb_rb = rte_ring_dequeue_burst(dispatch_ring[port_id][queue_id], 1032 (void **)pkts_burst, MAX_PKT_BURST, NULL); 1033 1034 if(nb_rb > 0) { 1035 process_packets(port_id, queue_id, pkts_burst, nb_rb, ctx, 1); 1036 } 1037 1038 return 0; 1039 } 1040 1041 static inline void 1042 handle_sysctl_msg(struct ff_msg *msg) 1043 { 1044 int ret = ff_sysctl(msg->sysctl.name, msg->sysctl.namelen, 1045 msg->sysctl.old, msg->sysctl.oldlenp, msg->sysctl.new, 1046 msg->sysctl.newlen); 1047 1048 if (ret < 0) { 1049 msg->result = errno; 1050 } else { 1051 msg->result = 0; 1052 } 1053 } 1054 1055 static inline void 1056 handle_ioctl_msg(struct ff_msg *msg) 1057 { 1058 int fd, ret; 1059 #ifdef INET6 1060 if (msg->msg_type == FF_IOCTL6) { 1061 fd = ff_socket(AF_INET6, SOCK_DGRAM, 0); 1062 } else 1063 #endif 1064 fd = ff_socket(AF_INET, SOCK_DGRAM, 0); 1065 1066 if (fd < 0) { 1067 ret = -1; 1068 goto done; 1069 } 1070 1071 ret = ff_ioctl_freebsd(fd, msg->ioctl.cmd, msg->ioctl.data); 1072 1073 ff_close(fd); 1074 1075 done: 1076 if (ret < 0) { 1077 msg->result = errno; 1078 } else { 1079 msg->result = 0; 1080 } 1081 } 1082 1083 static inline void 1084 handle_route_msg(struct ff_msg *msg) 1085 { 1086 int ret = ff_rtioctl(msg->route.fib, msg->route.data, 1087 &msg->route.len, msg->route.maxlen); 1088 if (ret < 0) { 1089 msg->result = errno; 1090 } else { 1091 msg->result = 0; 1092 } 1093 } 1094 1095 static inline void 1096 handle_top_msg(struct ff_msg *msg) 1097 { 1098 msg->top = ff_top_status; 1099 msg->result = 0; 1100 } 1101 1102 #ifdef FF_NETGRAPH 1103 static inline void 1104 handle_ngctl_msg(struct ff_msg *msg) 1105 { 1106 int ret = ff_ngctl(msg->ngctl.cmd, msg->ngctl.data); 1107 if (ret < 0) { 1108 msg->result = errno; 1109 } else { 1110 msg->result = 0; 1111 msg->ngctl.ret = ret; 1112 } 1113 } 1114 #endif 1115 1116 #ifdef FF_IPFW 1117 static inline void 1118 handle_ipfw_msg(struct ff_msg *msg) 1119 { 1120 int fd, ret; 1121 fd = ff_socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 1122 if (fd < 0) { 1123 ret = -1; 1124 goto done; 1125 } 1126 1127 switch (msg->ipfw.cmd) { 1128 case FF_IPFW_GET: 1129 ret = ff_getsockopt_freebsd(fd, msg->ipfw.level, 1130 msg->ipfw.optname, msg->ipfw.optval, 1131 msg->ipfw.optlen); 1132 break; 1133 case FF_IPFW_SET: 1134 ret = ff_setsockopt_freebsd(fd, msg->ipfw.level, 1135 msg->ipfw.optname, msg->ipfw.optval, 1136 *(msg->ipfw.optlen)); 1137 break; 1138 default: 1139 ret = -1; 1140 errno = ENOTSUP; 1141 break; 1142 } 1143 1144 ff_close(fd); 1145 1146 done: 1147 if (ret < 0) { 1148 msg->result = errno; 1149 } else { 1150 msg->result = 0; 1151 } 1152 } 1153 #endif 1154 1155 static inline void 1156 handle_traffic_msg(struct ff_msg *msg) 1157 { 1158 msg->traffic = ff_traffic; 1159 msg->result = 0; 1160 } 1161 1162 static inline void 1163 handle_default_msg(struct ff_msg *msg) 1164 { 1165 msg->result = ENOTSUP; 1166 } 1167 1168 static inline void 1169 handle_msg(struct ff_msg *msg, uint16_t proc_id) 1170 { 1171 switch (msg->msg_type) { 1172 case FF_SYSCTL: 1173 handle_sysctl_msg(msg); 1174 break; 1175 case FF_IOCTL: 1176 #ifdef INET6 1177 case FF_IOCTL6: 1178 #endif 1179 handle_ioctl_msg(msg); 1180 break; 1181 case FF_ROUTE: 1182 handle_route_msg(msg); 1183 break; 1184 case FF_TOP: 1185 handle_top_msg(msg); 1186 break; 1187 #ifdef FF_NETGRAPH 1188 case FF_NGCTL: 1189 handle_ngctl_msg(msg); 1190 break; 1191 #endif 1192 #ifdef FF_IPFW 1193 case FF_IPFW_CTL: 1194 handle_ipfw_msg(msg); 1195 break; 1196 #endif 1197 case FF_TRAFFIC: 1198 handle_traffic_msg(msg); 1199 break; 1200 default: 1201 handle_default_msg(msg); 1202 break; 1203 } 1204 rte_ring_enqueue(msg_ring[proc_id].ring[1], msg); 1205 } 1206 1207 static inline int 1208 process_msg_ring(uint16_t proc_id) 1209 { 1210 void *msg; 1211 int ret = rte_ring_dequeue(msg_ring[proc_id].ring[0], &msg); 1212 1213 if (unlikely(ret == 0)) { 1214 handle_msg((struct ff_msg *)msg, proc_id); 1215 } 1216 1217 return 0; 1218 } 1219 1220 /* Send burst of packets on an output interface */ 1221 static inline int 1222 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port) 1223 { 1224 struct rte_mbuf **m_table; 1225 int ret; 1226 uint16_t queueid; 1227 1228 queueid = qconf->tx_queue_id[port]; 1229 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table; 1230 1231 if (unlikely(qconf->pcap[port] != NULL)) { 1232 uint16_t i; 1233 for (i = 0; i < n; i++) { 1234 ff_dump_packets(qconf->pcap[port], m_table[i]); 1235 } 1236 } 1237 1238 ret = rte_eth_tx_burst(port, queueid, m_table, n); 1239 ff_traffic.tx_packets += ret; 1240 uint16_t i; 1241 for (i = 0; i < ret; i++) { 1242 ff_traffic.tx_bytes += rte_pktmbuf_pkt_len(m_table[i]); 1243 #ifdef FF_USE_PAGE_ARRAY 1244 if (qconf->tx_mbufs[port].bsd_m_table[i]) 1245 ff_enq_tx_bsdmbuf(port, qconf->tx_mbufs[port].bsd_m_table[i], m_table[i]->nb_segs); 1246 #endif 1247 } 1248 if (unlikely(ret < n)) { 1249 do { 1250 rte_pktmbuf_free(m_table[ret]); 1251 #ifdef FF_USE_PAGE_ARRAY 1252 if ( qconf->tx_mbufs[port].bsd_m_table[ret] ) 1253 ff_mbuf_free(qconf->tx_mbufs[port].bsd_m_table[ret]); 1254 #endif 1255 } while (++ret < n); 1256 } 1257 return 0; 1258 } 1259 1260 /* Enqueue a single packet, and send burst if queue is filled */ 1261 static inline int 1262 send_single_packet(struct rte_mbuf *m, uint8_t port) 1263 { 1264 uint16_t len; 1265 struct lcore_conf *qconf; 1266 1267 qconf = &lcore_conf; 1268 len = qconf->tx_mbufs[port].len; 1269 qconf->tx_mbufs[port].m_table[len] = m; 1270 len++; 1271 1272 /* enough pkts to be sent */ 1273 if (unlikely(len == MAX_PKT_BURST)) { 1274 send_burst(qconf, MAX_PKT_BURST, port); 1275 len = 0; 1276 } 1277 1278 qconf->tx_mbufs[port].len = len; 1279 return 0; 1280 } 1281 1282 int 1283 ff_dpdk_if_send(struct ff_dpdk_if_context *ctx, void *m, 1284 int total) 1285 { 1286 #ifdef FF_USE_PAGE_ARRAY 1287 struct lcore_conf *qconf = &lcore_conf; 1288 int len = 0; 1289 1290 len = ff_if_send_onepkt(ctx, m,total); 1291 if (unlikely(len == MAX_PKT_BURST)) { 1292 send_burst(qconf, MAX_PKT_BURST, ctx->port_id); 1293 len = 0; 1294 } 1295 qconf->tx_mbufs[ctx->port_id].len = len; 1296 return 0; 1297 #endif 1298 struct rte_mempool *mbuf_pool = pktmbuf_pool[lcore_conf.socket_id]; 1299 struct rte_mbuf *head = rte_pktmbuf_alloc(mbuf_pool); 1300 if (head == NULL) { 1301 ff_mbuf_free(m); 1302 return -1; 1303 } 1304 1305 head->pkt_len = total; 1306 head->nb_segs = 0; 1307 1308 int off = 0; 1309 struct rte_mbuf *cur = head, *prev = NULL; 1310 while(total > 0) { 1311 if (cur == NULL) { 1312 cur = rte_pktmbuf_alloc(mbuf_pool); 1313 if (cur == NULL) { 1314 rte_pktmbuf_free(head); 1315 ff_mbuf_free(m); 1316 return -1; 1317 } 1318 } 1319 1320 if (prev != NULL) { 1321 prev->next = cur; 1322 } 1323 head->nb_segs++; 1324 1325 prev = cur; 1326 void *data = rte_pktmbuf_mtod(cur, void*); 1327 int len = total > RTE_MBUF_DEFAULT_DATAROOM ? RTE_MBUF_DEFAULT_DATAROOM : total; 1328 int ret = ff_mbuf_copydata(m, data, off, len); 1329 if (ret < 0) { 1330 rte_pktmbuf_free(head); 1331 ff_mbuf_free(m); 1332 return -1; 1333 } 1334 1335 1336 cur->data_len = len; 1337 off += len; 1338 total -= len; 1339 cur = NULL; 1340 } 1341 1342 struct ff_tx_offload offload = {0}; 1343 ff_mbuf_tx_offload(m, &offload); 1344 1345 void *data = rte_pktmbuf_mtod(head, void*); 1346 1347 if (offload.ip_csum) { 1348 /* ipv6 not supported yet */ 1349 struct ipv4_hdr *iph; 1350 int iph_len; 1351 iph = (struct ipv4_hdr *)(data + ETHER_HDR_LEN); 1352 iph_len = (iph->version_ihl & 0x0f) << 2; 1353 1354 head->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4; 1355 head->l2_len = ETHER_HDR_LEN; 1356 head->l3_len = iph_len; 1357 } 1358 1359 if (ctx->hw_features.tx_csum_l4) { 1360 struct ipv4_hdr *iph; 1361 int iph_len; 1362 iph = (struct ipv4_hdr *)(data + ETHER_HDR_LEN); 1363 iph_len = (iph->version_ihl & 0x0f) << 2; 1364 1365 if (offload.tcp_csum) { 1366 head->ol_flags |= PKT_TX_TCP_CKSUM; 1367 head->l2_len = ETHER_HDR_LEN; 1368 head->l3_len = iph_len; 1369 } 1370 1371 /* 1372 * TCP segmentation offload. 1373 * 1374 * - set the PKT_TX_TCP_SEG flag in mbuf->ol_flags (this flag 1375 * implies PKT_TX_TCP_CKSUM) 1376 * - set the flag PKT_TX_IPV4 or PKT_TX_IPV6 1377 * - if it's IPv4, set the PKT_TX_IP_CKSUM flag and 1378 * write the IP checksum to 0 in the packet 1379 * - fill the mbuf offload information: l2_len, 1380 * l3_len, l4_len, tso_segsz 1381 * - calculate the pseudo header checksum without taking ip_len 1382 * in account, and set it in the TCP header. Refer to 1383 * rte_ipv4_phdr_cksum() and rte_ipv6_phdr_cksum() that can be 1384 * used as helpers. 1385 */ 1386 if (offload.tso_seg_size) { 1387 struct tcp_hdr *tcph; 1388 int tcph_len; 1389 tcph = (struct tcp_hdr *)((char *)iph + iph_len); 1390 tcph_len = (tcph->data_off & 0xf0) >> 2; 1391 tcph->cksum = rte_ipv4_phdr_cksum(iph, PKT_TX_TCP_SEG); 1392 1393 head->ol_flags |= PKT_TX_TCP_SEG; 1394 head->l4_len = tcph_len; 1395 head->tso_segsz = offload.tso_seg_size; 1396 } 1397 1398 if (offload.udp_csum) { 1399 head->ol_flags |= PKT_TX_UDP_CKSUM; 1400 head->l2_len = ETHER_HDR_LEN; 1401 head->l3_len = iph_len; 1402 } 1403 } 1404 1405 ff_mbuf_free(m); 1406 1407 return send_single_packet(head, ctx->port_id); 1408 } 1409 1410 static int 1411 main_loop(void *arg) 1412 { 1413 struct loop_routine *lr = (struct loop_routine *)arg; 1414 1415 struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; 1416 uint64_t prev_tsc, diff_tsc, cur_tsc, usch_tsc, div_tsc, usr_tsc, sys_tsc, end_tsc, idle_sleep_tsc; 1417 int i, j, nb_rx, idle; 1418 uint16_t port_id, queue_id; 1419 struct lcore_conf *qconf; 1420 uint64_t drain_tsc = 0; 1421 struct ff_dpdk_if_context *ctx; 1422 1423 if (pkt_tx_delay) { 1424 drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * pkt_tx_delay; 1425 } 1426 1427 prev_tsc = 0; 1428 usch_tsc = 0; 1429 1430 qconf = &lcore_conf; 1431 1432 while (1) { 1433 cur_tsc = rte_rdtsc(); 1434 if (unlikely(freebsd_clock.expire < cur_tsc)) { 1435 rte_timer_manage(); 1436 } 1437 1438 idle = 1; 1439 sys_tsc = 0; 1440 usr_tsc = 0; 1441 1442 /* 1443 * TX burst queue drain 1444 */ 1445 diff_tsc = cur_tsc - prev_tsc; 1446 if (unlikely(diff_tsc >= drain_tsc)) { 1447 for (i = 0; i < qconf->nb_tx_port; i++) { 1448 port_id = qconf->tx_port_id[i]; 1449 if (qconf->tx_mbufs[port_id].len == 0) 1450 continue; 1451 1452 idle = 0; 1453 1454 send_burst(qconf, 1455 qconf->tx_mbufs[port_id].len, 1456 port_id); 1457 qconf->tx_mbufs[port_id].len = 0; 1458 } 1459 1460 prev_tsc = cur_tsc; 1461 } 1462 1463 /* 1464 * Read packet from RX queues 1465 */ 1466 for (i = 0; i < qconf->nb_rx_queue; ++i) { 1467 port_id = qconf->rx_queue_list[i].port_id; 1468 queue_id = qconf->rx_queue_list[i].queue_id; 1469 ctx = veth_ctx[port_id]; 1470 1471 #ifdef FF_KNI 1472 if (enable_kni && rte_eal_process_type() == RTE_PROC_PRIMARY) { 1473 ff_kni_process(port_id, queue_id, pkts_burst, MAX_PKT_BURST); 1474 } 1475 #endif 1476 1477 process_dispatch_ring(port_id, queue_id, pkts_burst, ctx); 1478 1479 nb_rx = rte_eth_rx_burst(port_id, queue_id, pkts_burst, 1480 MAX_PKT_BURST); 1481 if (nb_rx == 0) 1482 continue; 1483 1484 idle = 0; 1485 1486 /* Prefetch first packets */ 1487 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) { 1488 rte_prefetch0(rte_pktmbuf_mtod( 1489 pkts_burst[j], void *)); 1490 } 1491 1492 /* Prefetch and handle already prefetched packets */ 1493 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) { 1494 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[ 1495 j + PREFETCH_OFFSET], void *)); 1496 process_packets(port_id, queue_id, &pkts_burst[j], 1, ctx, 0); 1497 } 1498 1499 /* Handle remaining prefetched packets */ 1500 for (; j < nb_rx; j++) { 1501 process_packets(port_id, queue_id, &pkts_burst[j], 1, ctx, 0); 1502 } 1503 } 1504 1505 process_msg_ring(qconf->proc_id); 1506 1507 div_tsc = rte_rdtsc(); 1508 1509 if (likely(lr->loop != NULL && (!idle || cur_tsc - usch_tsc >= drain_tsc))) { 1510 usch_tsc = cur_tsc; 1511 lr->loop(lr->arg); 1512 } 1513 1514 idle_sleep_tsc = rte_rdtsc(); 1515 if (likely(idle && idle_sleep)) { 1516 usleep(idle_sleep); 1517 end_tsc = rte_rdtsc(); 1518 } else { 1519 end_tsc = idle_sleep_tsc; 1520 } 1521 1522 if (usch_tsc == cur_tsc) { 1523 usr_tsc = idle_sleep_tsc - div_tsc; 1524 } 1525 1526 if (!idle) { 1527 sys_tsc = div_tsc - cur_tsc; 1528 ff_top_status.sys_tsc += sys_tsc; 1529 } 1530 1531 ff_top_status.usr_tsc += usr_tsc; 1532 ff_top_status.work_tsc += end_tsc - cur_tsc; 1533 ff_top_status.idle_tsc += end_tsc - cur_tsc - usr_tsc - sys_tsc; 1534 1535 ff_top_status.loops++; 1536 } 1537 1538 return 0; 1539 } 1540 1541 int 1542 ff_dpdk_if_up(void) { 1543 int i; 1544 struct lcore_conf *qconf = &lcore_conf; 1545 for (i = 0; i < qconf->nb_tx_port; i++) { 1546 uint16_t port_id = qconf->tx_port_id[i]; 1547 1548 struct ff_port_cfg *pconf = &qconf->port_cfgs[port_id]; 1549 veth_ctx[port_id] = ff_veth_attach(pconf); 1550 if (veth_ctx[port_id] == NULL) { 1551 rte_exit(EXIT_FAILURE, "ff_veth_attach failed"); 1552 } 1553 } 1554 1555 return 0; 1556 } 1557 1558 void 1559 ff_dpdk_run(loop_func_t loop, void *arg) { 1560 struct loop_routine *lr = rte_malloc(NULL, 1561 sizeof(struct loop_routine), 0); 1562 lr->loop = loop; 1563 lr->arg = arg; 1564 rte_eal_mp_remote_launch(main_loop, lr, CALL_MASTER); 1565 rte_eal_mp_wait_lcore(); 1566 rte_free(lr); 1567 } 1568 1569 void 1570 ff_dpdk_pktmbuf_free(void *m) 1571 { 1572 rte_pktmbuf_free((struct rte_mbuf *)m); 1573 } 1574 1575 static uint32_t 1576 toeplitz_hash(unsigned keylen, const uint8_t *key, 1577 unsigned datalen, const uint8_t *data) 1578 { 1579 uint32_t hash = 0, v; 1580 u_int i, b; 1581 1582 /* XXXRW: Perhaps an assertion about key length vs. data length? */ 1583 1584 v = (key[0]<<24) + (key[1]<<16) + (key[2] <<8) + key[3]; 1585 for (i = 0; i < datalen; i++) { 1586 for (b = 0; b < 8; b++) { 1587 if (data[i] & (1<<(7-b))) 1588 hash ^= v; 1589 v <<= 1; 1590 if ((i + 4) < keylen && 1591 (key[i+4] & (1<<(7-b)))) 1592 v |= 1; 1593 } 1594 } 1595 return (hash); 1596 } 1597 1598 int 1599 ff_rss_check(void *softc, uint32_t saddr, uint32_t daddr, 1600 uint16_t sport, uint16_t dport) 1601 { 1602 struct lcore_conf *qconf = &lcore_conf; 1603 struct ff_dpdk_if_context *ctx = ff_veth_softc_to_hostc(softc); 1604 uint16_t nb_queues = qconf->nb_queue_list[ctx->port_id]; 1605 1606 if (nb_queues <= 1) { 1607 return 1; 1608 } 1609 1610 uint16_t reta_size = rss_reta_size[ctx->port_id]; 1611 uint16_t queueid = qconf->tx_queue_id[ctx->port_id]; 1612 1613 uint8_t data[sizeof(saddr) + sizeof(daddr) + sizeof(sport) + 1614 sizeof(dport)]; 1615 1616 unsigned datalen = 0; 1617 1618 bcopy(&saddr, &data[datalen], sizeof(saddr)); 1619 datalen += sizeof(saddr); 1620 1621 bcopy(&daddr, &data[datalen], sizeof(daddr)); 1622 datalen += sizeof(daddr); 1623 1624 bcopy(&sport, &data[datalen], sizeof(sport)); 1625 datalen += sizeof(sport); 1626 1627 bcopy(&dport, &data[datalen], sizeof(dport)); 1628 datalen += sizeof(dport); 1629 1630 uint32_t hash = toeplitz_hash(sizeof(default_rsskey_40bytes), 1631 default_rsskey_40bytes, datalen, data); 1632 1633 return ((hash & (reta_size - 1)) % nb_queues) == queueid; 1634 } 1635 1636 void 1637 ff_regist_packet_dispatcher(dispatch_func_t func) 1638 { 1639 packet_dispatcher = func; 1640 } 1641 1642 uint64_t 1643 ff_get_tsc_ns() 1644 { 1645 uint64_t cur_tsc = rte_rdtsc(); 1646 uint64_t hz = rte_get_tsc_hz(); 1647 return ((double)cur_tsc/(double)hz) * NS_PER_S; 1648 } 1649 1650