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 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[FF_MSG_NUM][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[FF_MSG_NUM]; 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, j; 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 msg_ring[i].ring[0] = create_ring(msg_ring[i].ring_name[0], 446 MSG_RING_SIZE, socketid, RING_F_SP_ENQ | RING_F_SC_DEQ); 447 if (msg_ring[i].ring[0] == NULL) 448 rte_panic("create ring::%s failed!\n", msg_ring[i].ring_name[0]); 449 450 for (j = FF_SYSCTL; j < FF_MSG_NUM; j++) { 451 snprintf(msg_ring[i].ring_name[j], RTE_RING_NAMESIZE, 452 "%s%u_%u", FF_MSG_RING_OUT, i, j); 453 msg_ring[i].ring[j] = create_ring(msg_ring[i].ring_name[j], 454 MSG_RING_SIZE, socketid, RING_F_SP_ENQ | RING_F_SC_DEQ); 455 if (msg_ring[i].ring[j] == NULL) 456 rte_panic("create ring::%s failed!\n", msg_ring[i].ring_name[j]); 457 } 458 } 459 460 return 0; 461 } 462 463 #ifdef FF_KNI 464 static int 465 init_kni(void) 466 { 467 int nb_ports = rte_eth_dev_count_avail(); 468 kni_accept = 0; 469 if(strcasecmp(ff_global_cfg.kni.method, "accept") == 0) 470 kni_accept = 1; 471 472 ff_kni_init(nb_ports, ff_global_cfg.kni.tcp_port, 473 ff_global_cfg.kni.udp_port); 474 475 unsigned socket_id = lcore_conf.socket_id; 476 struct rte_mempool *mbuf_pool = pktmbuf_pool[socket_id]; 477 478 nb_ports = ff_global_cfg.dpdk.nb_ports; 479 int i, ret; 480 for (i = 0; i < nb_ports; i++) { 481 uint16_t port_id = ff_global_cfg.dpdk.portid_list[i]; 482 ff_kni_alloc(port_id, socket_id, mbuf_pool, KNI_QUEUE_SIZE); 483 } 484 485 return 0; 486 } 487 #endif 488 489 static void 490 set_rss_table(uint16_t port_id, uint16_t reta_size, uint16_t nb_queues) 491 { 492 if (reta_size == 0) { 493 return; 494 } 495 496 int reta_conf_size = RTE_MAX(1, reta_size / RTE_RETA_GROUP_SIZE); 497 struct rte_eth_rss_reta_entry64 reta_conf[reta_conf_size]; 498 499 /* config HW indirection table */ 500 unsigned i, j, hash=0; 501 for (i = 0; i < reta_conf_size; i++) { 502 reta_conf[i].mask = ~0ULL; 503 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++) { 504 reta_conf[i].reta[j] = hash++ % nb_queues; 505 } 506 } 507 508 if (rte_eth_dev_rss_reta_update(port_id, reta_conf, reta_size)) { 509 rte_exit(EXIT_FAILURE, "port[%d], failed to update rss table\n", 510 port_id); 511 } 512 } 513 514 static int 515 init_port_start(void) 516 { 517 int nb_ports = ff_global_cfg.dpdk.nb_ports; 518 unsigned socketid = 0; 519 struct rte_mempool *mbuf_pool; 520 uint16_t i; 521 522 for (i = 0; i < nb_ports; i++) { 523 uint16_t port_id = ff_global_cfg.dpdk.portid_list[i]; 524 struct ff_port_cfg *pconf = &ff_global_cfg.dpdk.port_cfgs[port_id]; 525 uint16_t nb_queues = pconf->nb_lcores; 526 527 struct rte_eth_dev_info dev_info; 528 struct rte_eth_conf port_conf = {0}; 529 struct rte_eth_rxconf rxq_conf; 530 struct rte_eth_txconf txq_conf; 531 532 rte_eth_dev_info_get(port_id, &dev_info); 533 534 if (nb_queues > dev_info.max_rx_queues) { 535 rte_exit(EXIT_FAILURE, "num_procs[%d] bigger than max_rx_queues[%d]\n", 536 nb_queues, 537 dev_info.max_rx_queues); 538 } 539 540 if (nb_queues > dev_info.max_tx_queues) { 541 rte_exit(EXIT_FAILURE, "num_procs[%d] bigger than max_tx_queues[%d]\n", 542 nb_queues, 543 dev_info.max_tx_queues); 544 } 545 546 struct ether_addr addr; 547 rte_eth_macaddr_get(port_id, &addr); 548 printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8 549 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n", 550 (unsigned)port_id, 551 addr.addr_bytes[0], addr.addr_bytes[1], 552 addr.addr_bytes[2], addr.addr_bytes[3], 553 addr.addr_bytes[4], addr.addr_bytes[5]); 554 555 rte_memcpy(pconf->mac, 556 addr.addr_bytes, ETHER_ADDR_LEN); 557 558 /* Set RSS mode */ 559 uint64_t default_rss_hf = ETH_RSS_PROTO_MASK; 560 port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS; 561 port_conf.rx_adv_conf.rss_conf.rss_hf = default_rss_hf; 562 port_conf.rx_adv_conf.rss_conf.rss_key = default_rsskey_40bytes; 563 port_conf.rx_adv_conf.rss_conf.rss_key_len = 40; 564 port_conf.rx_adv_conf.rss_conf.rss_hf &= dev_info.flow_type_rss_offloads; 565 if (port_conf.rx_adv_conf.rss_conf.rss_hf != 566 ETH_RSS_PROTO_MASK) { 567 printf("Port %u modified RSS hash function based on hardware support," 568 "requested:%#"PRIx64" configured:%#"PRIx64"\n", 569 port_id, default_rss_hf, 570 port_conf.rx_adv_conf.rss_conf.rss_hf); 571 } 572 573 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) { 574 port_conf.txmode.offloads |= 575 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 576 } 577 578 /* Set Rx VLAN stripping */ 579 if (ff_global_cfg.dpdk.vlan_strip) { 580 if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_VLAN_STRIP) { 581 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_STRIP; 582 } 583 } 584 585 /* Enable HW CRC stripping */ 586 port_conf.rxmode.offloads &= ~DEV_RX_OFFLOAD_KEEP_CRC; 587 588 /* FIXME: Enable TCP LRO ?*/ 589 #if 0 590 if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_LRO) { 591 printf("LRO is supported\n"); 592 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_TCP_LRO; 593 pconf->hw_features.rx_lro = 1; 594 } 595 #endif 596 597 /* Set Rx checksum checking */ 598 if ((dev_info.rx_offload_capa & DEV_RX_OFFLOAD_IPV4_CKSUM) && 599 (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_UDP_CKSUM) && 600 (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_CKSUM)) { 601 printf("RX checksum offload supported\n"); 602 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_CHECKSUM; 603 pconf->hw_features.rx_csum = 1; 604 } 605 606 if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)) { 607 printf("TX ip checksum offload supported\n"); 608 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM; 609 pconf->hw_features.tx_csum_ip = 1; 610 } 611 612 if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_CKSUM) && 613 (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_CKSUM)) { 614 printf("TX TCP&UDP checksum offload supported\n"); 615 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM | DEV_TX_OFFLOAD_TCP_CKSUM; 616 pconf->hw_features.tx_csum_l4 = 1; 617 } 618 619 if (ff_global_cfg.dpdk.tso) { 620 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_TSO) { 621 printf("TSO is supported\n"); 622 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_TSO; 623 pconf->hw_features.tx_tso = 1; 624 } 625 } else { 626 printf("TSO is disabled\n"); 627 } 628 629 if (dev_info.reta_size) { 630 /* reta size must be power of 2 */ 631 assert((dev_info.reta_size & (dev_info.reta_size - 1)) == 0); 632 633 rss_reta_size[port_id] = dev_info.reta_size; 634 printf("port[%d]: rss table size: %d\n", port_id, 635 dev_info.reta_size); 636 } 637 638 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 639 continue; 640 } 641 642 int ret = rte_eth_dev_configure(port_id, nb_queues, nb_queues, &port_conf); 643 if (ret != 0) { 644 return ret; 645 } 646 647 static uint16_t nb_rxd = RX_QUEUE_SIZE; 648 static uint16_t nb_txd = TX_QUEUE_SIZE; 649 ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd); 650 if (ret < 0) 651 printf("Could not adjust number of descriptors " 652 "for port%u (%d)\n", (unsigned)port_id, ret); 653 654 uint16_t q; 655 for (q = 0; q < nb_queues; q++) { 656 if (numa_on) { 657 uint16_t lcore_id = lcore_conf.port_cfgs[port_id].lcore_list[q]; 658 socketid = rte_lcore_to_socket_id(lcore_id); 659 } 660 mbuf_pool = pktmbuf_pool[socketid]; 661 662 txq_conf = dev_info.default_txconf; 663 txq_conf.offloads = port_conf.txmode.offloads; 664 ret = rte_eth_tx_queue_setup(port_id, q, nb_txd, 665 socketid, &txq_conf); 666 if (ret < 0) { 667 return ret; 668 } 669 670 rxq_conf = dev_info.default_rxconf; 671 rxq_conf.offloads = port_conf.rxmode.offloads; 672 ret = rte_eth_rx_queue_setup(port_id, q, nb_rxd, 673 socketid, &rxq_conf, mbuf_pool); 674 if (ret < 0) { 675 return ret; 676 } 677 } 678 679 ret = rte_eth_dev_start(port_id); 680 if (ret < 0) { 681 return ret; 682 } 683 684 if (nb_queues > 1) { 685 /* set HW rss hash function to Toeplitz. */ 686 if (!rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_HASH)) { 687 struct rte_eth_hash_filter_info info = {0}; 688 info.info_type = RTE_ETH_HASH_FILTER_GLOBAL_CONFIG; 689 info.info.global_conf.hash_func = RTE_ETH_HASH_FUNCTION_TOEPLITZ; 690 691 if (rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_HASH, 692 RTE_ETH_FILTER_SET, &info) < 0) { 693 rte_exit(EXIT_FAILURE, "port[%d] set hash func failed\n", 694 port_id); 695 } 696 } 697 698 set_rss_table(port_id, dev_info.reta_size, nb_queues); 699 } 700 701 /* Enable RX in promiscuous mode for the Ethernet device. */ 702 if (ff_global_cfg.dpdk.promiscuous) { 703 rte_eth_promiscuous_enable(port_id); 704 ret = rte_eth_promiscuous_get(port_id); 705 if (ret == 1) { 706 printf("set port %u to promiscuous mode ok\n", port_id); 707 } else { 708 printf("set port %u to promiscuous mode error\n", port_id); 709 } 710 } 711 712 /* Enable pcap dump */ 713 if (pconf->pcap) { 714 ff_enable_pcap(pconf->pcap); 715 } 716 } 717 718 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 719 check_all_ports_link_status(); 720 } 721 722 return 0; 723 } 724 725 static int 726 init_clock(void) 727 { 728 rte_timer_subsystem_init(); 729 uint64_t hz = rte_get_timer_hz(); 730 uint64_t intrs = MS_PER_S/ff_global_cfg.freebsd.hz; 731 uint64_t tsc = (hz + MS_PER_S - 1) / MS_PER_S*intrs; 732 733 rte_timer_init(&freebsd_clock); 734 rte_timer_reset(&freebsd_clock, tsc, PERIODICAL, 735 rte_lcore_id(), &ff_hardclock_job, NULL); 736 737 ff_update_current_ts(); 738 739 return 0; 740 } 741 742 int 743 ff_dpdk_init(int argc, char **argv) 744 { 745 if (ff_global_cfg.dpdk.nb_procs < 1 || 746 ff_global_cfg.dpdk.nb_procs > RTE_MAX_LCORE || 747 ff_global_cfg.dpdk.proc_id >= ff_global_cfg.dpdk.nb_procs || 748 ff_global_cfg.dpdk.proc_id < 0) { 749 printf("param num_procs[%d] or proc_id[%d] error!\n", 750 ff_global_cfg.dpdk.nb_procs, 751 ff_global_cfg.dpdk.proc_id); 752 exit(1); 753 } 754 755 int ret = rte_eal_init(argc, argv); 756 if (ret < 0) { 757 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n"); 758 } 759 760 numa_on = ff_global_cfg.dpdk.numa_on; 761 762 idle_sleep = ff_global_cfg.dpdk.idle_sleep; 763 pkt_tx_delay = ff_global_cfg.dpdk.pkt_tx_delay > BURST_TX_DRAIN_US ? \ 764 BURST_TX_DRAIN_US : ff_global_cfg.dpdk.pkt_tx_delay; 765 766 init_lcore_conf(); 767 768 init_mem_pool(); 769 770 init_dispatch_ring(); 771 772 init_msg_ring(); 773 774 #ifdef FF_KNI 775 enable_kni = ff_global_cfg.kni.enable; 776 if (enable_kni) { 777 init_kni(); 778 } 779 #endif 780 781 #ifdef FF_USE_PAGE_ARRAY 782 ff_mmap_init(); 783 #endif 784 785 ret = init_port_start(); 786 if (ret < 0) { 787 rte_exit(EXIT_FAILURE, "init_port_start failed\n"); 788 } 789 790 init_clock(); 791 792 return 0; 793 } 794 795 static void 796 ff_veth_input(const struct ff_dpdk_if_context *ctx, struct rte_mbuf *pkt) 797 { 798 uint8_t rx_csum = ctx->hw_features.rx_csum; 799 if (rx_csum) { 800 if (pkt->ol_flags & (PKT_RX_IP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD)) { 801 rte_pktmbuf_free(pkt); 802 return; 803 } 804 } 805 806 /* 807 * FIXME: should we save pkt->vlan_tci 808 * if (pkt->ol_flags & PKT_RX_VLAN_PKT) 809 */ 810 811 void *data = rte_pktmbuf_mtod(pkt, void*); 812 uint16_t len = rte_pktmbuf_data_len(pkt); 813 814 void *hdr = ff_mbuf_gethdr(pkt, pkt->pkt_len, data, len, rx_csum); 815 if (hdr == NULL) { 816 rte_pktmbuf_free(pkt); 817 return; 818 } 819 820 struct rte_mbuf *pn = pkt->next; 821 void *prev = hdr; 822 while(pn != NULL) { 823 data = rte_pktmbuf_mtod(pn, void*); 824 len = rte_pktmbuf_data_len(pn); 825 826 void *mb = ff_mbuf_get(prev, data, len); 827 if (mb == NULL) { 828 ff_mbuf_free(hdr); 829 rte_pktmbuf_free(pkt); 830 return; 831 } 832 pn = pn->next; 833 prev = mb; 834 } 835 836 ff_veth_process_packet(ctx->ifp, hdr); 837 } 838 839 static enum FilterReturn 840 protocol_filter(const void *data, uint16_t len) 841 { 842 if(len < ETHER_HDR_LEN) 843 return FILTER_UNKNOWN; 844 845 const struct ether_hdr *hdr; 846 hdr = (const struct ether_hdr *)data; 847 uint16_t eth_frame_type = rte_be_to_cpu_16(hdr->ether_type); 848 849 if(eth_frame_type == ETHER_TYPE_ARP) 850 return FILTER_ARP; 851 852 #ifdef INET6 853 if (eth_frame_type == ETHER_TYPE_IPv6) { 854 return ff_kni_proto_filter(data + ETHER_HDR_LEN, 855 len - ETHER_HDR_LEN, eth_frame_type); 856 } 857 #endif 858 859 #ifndef FF_KNI 860 return FILTER_UNKNOWN; 861 #else 862 if (!enable_kni) { 863 return FILTER_UNKNOWN; 864 } 865 866 if(eth_frame_type != ETHER_TYPE_IPv4) 867 return FILTER_UNKNOWN; 868 869 return ff_kni_proto_filter(data + ETHER_HDR_LEN, 870 len - ETHER_HDR_LEN, eth_frame_type); 871 #endif 872 } 873 874 static inline void 875 pktmbuf_deep_attach(struct rte_mbuf *mi, const struct rte_mbuf *m) 876 { 877 struct rte_mbuf *md; 878 void *src, *dst; 879 880 dst = rte_pktmbuf_mtod(mi, void *); 881 src = rte_pktmbuf_mtod(m, void *); 882 883 mi->data_len = m->data_len; 884 rte_memcpy(dst, src, m->data_len); 885 886 mi->port = m->port; 887 mi->vlan_tci = m->vlan_tci; 888 mi->vlan_tci_outer = m->vlan_tci_outer; 889 mi->tx_offload = m->tx_offload; 890 mi->hash = m->hash; 891 mi->ol_flags = m->ol_flags; 892 mi->packet_type = m->packet_type; 893 } 894 895 /* copied from rte_pktmbuf_clone */ 896 static inline struct rte_mbuf * 897 pktmbuf_deep_clone(const struct rte_mbuf *md, 898 struct rte_mempool *mp) 899 { 900 struct rte_mbuf *mc, *mi, **prev; 901 uint32_t pktlen; 902 uint8_t nseg; 903 904 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL)) 905 return NULL; 906 907 mi = mc; 908 prev = &mi->next; 909 pktlen = md->pkt_len; 910 nseg = 0; 911 912 do { 913 nseg++; 914 pktmbuf_deep_attach(mi, md); 915 *prev = mi; 916 prev = &mi->next; 917 } while ((md = md->next) != NULL && 918 (mi = rte_pktmbuf_alloc(mp)) != NULL); 919 920 *prev = NULL; 921 mc->nb_segs = nseg; 922 mc->pkt_len = pktlen; 923 924 /* Allocation of new indirect segment failed */ 925 if (unlikely (mi == NULL)) { 926 rte_pktmbuf_free(mc); 927 return NULL; 928 } 929 930 __rte_mbuf_sanity_check(mc, 1); 931 return mc; 932 } 933 934 static inline void 935 process_packets(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **bufs, 936 uint16_t count, const struct ff_dpdk_if_context *ctx, int pkts_from_ring) 937 { 938 struct lcore_conf *qconf = &lcore_conf; 939 uint16_t nb_queues = qconf->nb_queue_list[port_id]; 940 941 uint16_t i; 942 for (i = 0; i < count; i++) { 943 struct rte_mbuf *rtem = bufs[i]; 944 945 if (unlikely(qconf->pcap[port_id] != NULL)) { 946 if (!pkts_from_ring) { 947 ff_dump_packets(qconf->pcap[port_id], rtem); 948 } 949 } 950 951 void *data = rte_pktmbuf_mtod(rtem, void*); 952 uint16_t len = rte_pktmbuf_data_len(rtem); 953 954 if (!pkts_from_ring) { 955 ff_traffic.rx_packets++; 956 ff_traffic.rx_bytes += len; 957 } 958 959 if (!pkts_from_ring && packet_dispatcher) { 960 int ret = (*packet_dispatcher)(data, &len, queue_id, nb_queues); 961 if (ret == FF_DISPATCH_RESPONSE) { 962 rte_pktmbuf_pkt_len(rtem) = rte_pktmbuf_data_len(rtem) = len; 963 send_single_packet(rtem, port_id); 964 continue; 965 } 966 967 if (ret == FF_DISPATCH_ERROR || ret >= nb_queues) { 968 rte_pktmbuf_free(rtem); 969 continue; 970 } 971 972 if (ret != queue_id) { 973 ret = rte_ring_enqueue(dispatch_ring[port_id][ret], rtem); 974 if (ret < 0) 975 rte_pktmbuf_free(rtem); 976 977 continue; 978 } 979 } 980 981 enum FilterReturn filter = protocol_filter(data, len); 982 #ifdef INET6 983 if (filter == FILTER_ARP || filter == FILTER_NDP) { 984 #else 985 if (filter == FILTER_ARP) { 986 #endif 987 struct rte_mempool *mbuf_pool; 988 struct rte_mbuf *mbuf_clone; 989 if (!pkts_from_ring) { 990 uint16_t j; 991 for(j = 0; j < nb_queues; ++j) { 992 if(j == queue_id) 993 continue; 994 995 unsigned socket_id = 0; 996 if (numa_on) { 997 uint16_t lcore_id = qconf->port_cfgs[port_id].lcore_list[j]; 998 socket_id = rte_lcore_to_socket_id(lcore_id); 999 } 1000 mbuf_pool = pktmbuf_pool[socket_id]; 1001 mbuf_clone = pktmbuf_deep_clone(rtem, mbuf_pool); 1002 if(mbuf_clone) { 1003 int ret = rte_ring_enqueue(dispatch_ring[port_id][j], 1004 mbuf_clone); 1005 if (ret < 0) 1006 rte_pktmbuf_free(mbuf_clone); 1007 } 1008 } 1009 } 1010 1011 #ifdef FF_KNI 1012 if (enable_kni && rte_eal_process_type() == RTE_PROC_PRIMARY) { 1013 mbuf_pool = pktmbuf_pool[qconf->socket_id]; 1014 mbuf_clone = pktmbuf_deep_clone(rtem, mbuf_pool); 1015 if(mbuf_clone) { 1016 ff_kni_enqueue(port_id, mbuf_clone); 1017 } 1018 } 1019 #endif 1020 ff_veth_input(ctx, rtem); 1021 #ifdef FF_KNI 1022 } else if (enable_kni && 1023 ((filter == FILTER_KNI && kni_accept) || 1024 (filter == FILTER_UNKNOWN && !kni_accept)) ) { 1025 ff_kni_enqueue(port_id, rtem); 1026 #endif 1027 } else { 1028 ff_veth_input(ctx, rtem); 1029 } 1030 } 1031 } 1032 1033 static inline int 1034 process_dispatch_ring(uint16_t port_id, uint16_t queue_id, 1035 struct rte_mbuf **pkts_burst, const struct ff_dpdk_if_context *ctx) 1036 { 1037 /* read packet from ring buf and to process */ 1038 uint16_t nb_rb; 1039 nb_rb = rte_ring_dequeue_burst(dispatch_ring[port_id][queue_id], 1040 (void **)pkts_burst, MAX_PKT_BURST, NULL); 1041 1042 if(nb_rb > 0) { 1043 process_packets(port_id, queue_id, pkts_burst, nb_rb, ctx, 1); 1044 } 1045 1046 return 0; 1047 } 1048 1049 static inline void 1050 handle_sysctl_msg(struct ff_msg *msg) 1051 { 1052 int ret = ff_sysctl(msg->sysctl.name, msg->sysctl.namelen, 1053 msg->sysctl.old, msg->sysctl.oldlenp, msg->sysctl.new, 1054 msg->sysctl.newlen); 1055 1056 if (ret < 0) { 1057 msg->result = errno; 1058 } else { 1059 msg->result = 0; 1060 } 1061 } 1062 1063 static inline void 1064 handle_ioctl_msg(struct ff_msg *msg) 1065 { 1066 int fd, ret; 1067 #ifdef INET6 1068 if (msg->msg_type == FF_IOCTL6) { 1069 fd = ff_socket(AF_INET6, SOCK_DGRAM, 0); 1070 } else 1071 #endif 1072 fd = ff_socket(AF_INET, SOCK_DGRAM, 0); 1073 1074 if (fd < 0) { 1075 ret = -1; 1076 goto done; 1077 } 1078 1079 ret = ff_ioctl_freebsd(fd, msg->ioctl.cmd, msg->ioctl.data); 1080 1081 ff_close(fd); 1082 1083 done: 1084 if (ret < 0) { 1085 msg->result = errno; 1086 } else { 1087 msg->result = 0; 1088 } 1089 } 1090 1091 static inline void 1092 handle_route_msg(struct ff_msg *msg) 1093 { 1094 int ret = ff_rtioctl(msg->route.fib, msg->route.data, 1095 &msg->route.len, msg->route.maxlen); 1096 if (ret < 0) { 1097 msg->result = errno; 1098 } else { 1099 msg->result = 0; 1100 } 1101 } 1102 1103 static inline void 1104 handle_top_msg(struct ff_msg *msg) 1105 { 1106 msg->top = ff_top_status; 1107 msg->result = 0; 1108 } 1109 1110 #ifdef FF_NETGRAPH 1111 static inline void 1112 handle_ngctl_msg(struct ff_msg *msg) 1113 { 1114 int ret = ff_ngctl(msg->ngctl.cmd, msg->ngctl.data); 1115 if (ret < 0) { 1116 msg->result = errno; 1117 } else { 1118 msg->result = 0; 1119 msg->ngctl.ret = ret; 1120 } 1121 } 1122 #endif 1123 1124 #ifdef FF_IPFW 1125 static inline void 1126 handle_ipfw_msg(struct ff_msg *msg) 1127 { 1128 int fd, ret; 1129 fd = ff_socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 1130 if (fd < 0) { 1131 ret = -1; 1132 goto done; 1133 } 1134 1135 switch (msg->ipfw.cmd) { 1136 case FF_IPFW_GET: 1137 ret = ff_getsockopt_freebsd(fd, msg->ipfw.level, 1138 msg->ipfw.optname, msg->ipfw.optval, 1139 msg->ipfw.optlen); 1140 break; 1141 case FF_IPFW_SET: 1142 ret = ff_setsockopt_freebsd(fd, msg->ipfw.level, 1143 msg->ipfw.optname, msg->ipfw.optval, 1144 *(msg->ipfw.optlen)); 1145 break; 1146 default: 1147 ret = -1; 1148 errno = ENOTSUP; 1149 break; 1150 } 1151 1152 ff_close(fd); 1153 1154 done: 1155 if (ret < 0) { 1156 msg->result = errno; 1157 } else { 1158 msg->result = 0; 1159 } 1160 } 1161 #endif 1162 1163 static inline void 1164 handle_traffic_msg(struct ff_msg *msg) 1165 { 1166 msg->traffic = ff_traffic; 1167 msg->result = 0; 1168 } 1169 1170 static inline void 1171 handle_default_msg(struct ff_msg *msg) 1172 { 1173 msg->result = ENOTSUP; 1174 } 1175 1176 static inline void 1177 handle_msg(struct ff_msg *msg, uint16_t proc_id) 1178 { 1179 switch (msg->msg_type) { 1180 case FF_SYSCTL: 1181 handle_sysctl_msg(msg); 1182 break; 1183 case FF_IOCTL: 1184 #ifdef INET6 1185 case FF_IOCTL6: 1186 #endif 1187 handle_ioctl_msg(msg); 1188 break; 1189 case FF_ROUTE: 1190 handle_route_msg(msg); 1191 break; 1192 case FF_TOP: 1193 handle_top_msg(msg); 1194 break; 1195 #ifdef FF_NETGRAPH 1196 case FF_NGCTL: 1197 handle_ngctl_msg(msg); 1198 break; 1199 #endif 1200 #ifdef FF_IPFW 1201 case FF_IPFW_CTL: 1202 handle_ipfw_msg(msg); 1203 break; 1204 #endif 1205 case FF_TRAFFIC: 1206 handle_traffic_msg(msg); 1207 break; 1208 default: 1209 handle_default_msg(msg); 1210 break; 1211 } 1212 rte_ring_enqueue(msg_ring[proc_id].ring[msg->msg_type], msg); 1213 } 1214 1215 static inline int 1216 process_msg_ring(uint16_t proc_id) 1217 { 1218 void *msg; 1219 int ret = rte_ring_dequeue(msg_ring[proc_id].ring[0], &msg); 1220 1221 if (unlikely(ret == 0)) { 1222 handle_msg((struct ff_msg *)msg, proc_id); 1223 } 1224 1225 return 0; 1226 } 1227 1228 /* Send burst of packets on an output interface */ 1229 static inline int 1230 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port) 1231 { 1232 struct rte_mbuf **m_table; 1233 int ret; 1234 uint16_t queueid; 1235 1236 queueid = qconf->tx_queue_id[port]; 1237 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table; 1238 1239 if (unlikely(qconf->pcap[port] != NULL)) { 1240 uint16_t i; 1241 for (i = 0; i < n; i++) { 1242 ff_dump_packets(qconf->pcap[port], m_table[i]); 1243 } 1244 } 1245 1246 ret = rte_eth_tx_burst(port, queueid, m_table, n); 1247 ff_traffic.tx_packets += ret; 1248 uint16_t i; 1249 for (i = 0; i < ret; i++) { 1250 ff_traffic.tx_bytes += rte_pktmbuf_pkt_len(m_table[i]); 1251 #ifdef FF_USE_PAGE_ARRAY 1252 if (qconf->tx_mbufs[port].bsd_m_table[i]) 1253 ff_enq_tx_bsdmbuf(port, qconf->tx_mbufs[port].bsd_m_table[i], m_table[i]->nb_segs); 1254 #endif 1255 } 1256 if (unlikely(ret < n)) { 1257 do { 1258 rte_pktmbuf_free(m_table[ret]); 1259 #ifdef FF_USE_PAGE_ARRAY 1260 if ( qconf->tx_mbufs[port].bsd_m_table[ret] ) 1261 ff_mbuf_free(qconf->tx_mbufs[port].bsd_m_table[ret]); 1262 #endif 1263 } while (++ret < n); 1264 } 1265 return 0; 1266 } 1267 1268 /* Enqueue a single packet, and send burst if queue is filled */ 1269 static inline int 1270 send_single_packet(struct rte_mbuf *m, uint8_t port) 1271 { 1272 uint16_t len; 1273 struct lcore_conf *qconf; 1274 1275 qconf = &lcore_conf; 1276 len = qconf->tx_mbufs[port].len; 1277 qconf->tx_mbufs[port].m_table[len] = m; 1278 len++; 1279 1280 /* enough pkts to be sent */ 1281 if (unlikely(len == MAX_PKT_BURST)) { 1282 send_burst(qconf, MAX_PKT_BURST, port); 1283 len = 0; 1284 } 1285 1286 qconf->tx_mbufs[port].len = len; 1287 return 0; 1288 } 1289 1290 int 1291 ff_dpdk_if_send(struct ff_dpdk_if_context *ctx, void *m, 1292 int total) 1293 { 1294 #ifdef FF_USE_PAGE_ARRAY 1295 struct lcore_conf *qconf = &lcore_conf; 1296 int len = 0; 1297 1298 len = ff_if_send_onepkt(ctx, m,total); 1299 if (unlikely(len == MAX_PKT_BURST)) { 1300 send_burst(qconf, MAX_PKT_BURST, ctx->port_id); 1301 len = 0; 1302 } 1303 qconf->tx_mbufs[ctx->port_id].len = len; 1304 return 0; 1305 #endif 1306 struct rte_mempool *mbuf_pool = pktmbuf_pool[lcore_conf.socket_id]; 1307 struct rte_mbuf *head = rte_pktmbuf_alloc(mbuf_pool); 1308 if (head == NULL) { 1309 ff_mbuf_free(m); 1310 return -1; 1311 } 1312 1313 head->pkt_len = total; 1314 head->nb_segs = 0; 1315 1316 int off = 0; 1317 struct rte_mbuf *cur = head, *prev = NULL; 1318 while(total > 0) { 1319 if (cur == NULL) { 1320 cur = rte_pktmbuf_alloc(mbuf_pool); 1321 if (cur == NULL) { 1322 rte_pktmbuf_free(head); 1323 ff_mbuf_free(m); 1324 return -1; 1325 } 1326 } 1327 1328 if (prev != NULL) { 1329 prev->next = cur; 1330 } 1331 head->nb_segs++; 1332 1333 prev = cur; 1334 void *data = rte_pktmbuf_mtod(cur, void*); 1335 int len = total > RTE_MBUF_DEFAULT_DATAROOM ? RTE_MBUF_DEFAULT_DATAROOM : total; 1336 int ret = ff_mbuf_copydata(m, data, off, len); 1337 if (ret < 0) { 1338 rte_pktmbuf_free(head); 1339 ff_mbuf_free(m); 1340 return -1; 1341 } 1342 1343 1344 cur->data_len = len; 1345 off += len; 1346 total -= len; 1347 cur = NULL; 1348 } 1349 1350 struct ff_tx_offload offload = {0}; 1351 ff_mbuf_tx_offload(m, &offload); 1352 1353 void *data = rte_pktmbuf_mtod(head, void*); 1354 1355 if (offload.ip_csum) { 1356 /* ipv6 not supported yet */ 1357 struct ipv4_hdr *iph; 1358 int iph_len; 1359 iph = (struct ipv4_hdr *)(data + ETHER_HDR_LEN); 1360 iph_len = (iph->version_ihl & 0x0f) << 2; 1361 1362 head->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4; 1363 head->l2_len = ETHER_HDR_LEN; 1364 head->l3_len = iph_len; 1365 } 1366 1367 if (ctx->hw_features.tx_csum_l4) { 1368 struct ipv4_hdr *iph; 1369 int iph_len; 1370 iph = (struct ipv4_hdr *)(data + ETHER_HDR_LEN); 1371 iph_len = (iph->version_ihl & 0x0f) << 2; 1372 1373 if (offload.tcp_csum) { 1374 head->ol_flags |= PKT_TX_TCP_CKSUM; 1375 head->l2_len = ETHER_HDR_LEN; 1376 head->l3_len = iph_len; 1377 } 1378 1379 /* 1380 * TCP segmentation offload. 1381 * 1382 * - set the PKT_TX_TCP_SEG flag in mbuf->ol_flags (this flag 1383 * implies PKT_TX_TCP_CKSUM) 1384 * - set the flag PKT_TX_IPV4 or PKT_TX_IPV6 1385 * - if it's IPv4, set the PKT_TX_IP_CKSUM flag and 1386 * write the IP checksum to 0 in the packet 1387 * - fill the mbuf offload information: l2_len, 1388 * l3_len, l4_len, tso_segsz 1389 * - calculate the pseudo header checksum without taking ip_len 1390 * in account, and set it in the TCP header. Refer to 1391 * rte_ipv4_phdr_cksum() and rte_ipv6_phdr_cksum() that can be 1392 * used as helpers. 1393 */ 1394 if (offload.tso_seg_size) { 1395 struct tcp_hdr *tcph; 1396 int tcph_len; 1397 tcph = (struct tcp_hdr *)((char *)iph + iph_len); 1398 tcph_len = (tcph->data_off & 0xf0) >> 2; 1399 tcph->cksum = rte_ipv4_phdr_cksum(iph, PKT_TX_TCP_SEG); 1400 1401 head->ol_flags |= PKT_TX_TCP_SEG; 1402 head->l4_len = tcph_len; 1403 head->tso_segsz = offload.tso_seg_size; 1404 } 1405 1406 if (offload.udp_csum) { 1407 head->ol_flags |= PKT_TX_UDP_CKSUM; 1408 head->l2_len = ETHER_HDR_LEN; 1409 head->l3_len = iph_len; 1410 } 1411 } 1412 1413 ff_mbuf_free(m); 1414 1415 return send_single_packet(head, ctx->port_id); 1416 } 1417 1418 static int 1419 main_loop(void *arg) 1420 { 1421 struct loop_routine *lr = (struct loop_routine *)arg; 1422 1423 struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; 1424 uint64_t prev_tsc, diff_tsc, cur_tsc, usch_tsc, div_tsc, usr_tsc, sys_tsc, end_tsc, idle_sleep_tsc; 1425 int i, j, nb_rx, idle; 1426 uint16_t port_id, queue_id; 1427 struct lcore_conf *qconf; 1428 uint64_t drain_tsc = 0; 1429 struct ff_dpdk_if_context *ctx; 1430 1431 if (pkt_tx_delay) { 1432 drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * pkt_tx_delay; 1433 } 1434 1435 prev_tsc = 0; 1436 usch_tsc = 0; 1437 1438 qconf = &lcore_conf; 1439 1440 while (1) { 1441 cur_tsc = rte_rdtsc(); 1442 if (unlikely(freebsd_clock.expire < cur_tsc)) { 1443 rte_timer_manage(); 1444 } 1445 1446 idle = 1; 1447 sys_tsc = 0; 1448 usr_tsc = 0; 1449 1450 /* 1451 * TX burst queue drain 1452 */ 1453 diff_tsc = cur_tsc - prev_tsc; 1454 if (unlikely(diff_tsc >= drain_tsc)) { 1455 for (i = 0; i < qconf->nb_tx_port; i++) { 1456 port_id = qconf->tx_port_id[i]; 1457 if (qconf->tx_mbufs[port_id].len == 0) 1458 continue; 1459 1460 idle = 0; 1461 1462 send_burst(qconf, 1463 qconf->tx_mbufs[port_id].len, 1464 port_id); 1465 qconf->tx_mbufs[port_id].len = 0; 1466 } 1467 1468 prev_tsc = cur_tsc; 1469 } 1470 1471 /* 1472 * Read packet from RX queues 1473 */ 1474 for (i = 0; i < qconf->nb_rx_queue; ++i) { 1475 port_id = qconf->rx_queue_list[i].port_id; 1476 queue_id = qconf->rx_queue_list[i].queue_id; 1477 ctx = veth_ctx[port_id]; 1478 1479 #ifdef FF_KNI 1480 if (enable_kni && rte_eal_process_type() == RTE_PROC_PRIMARY) { 1481 ff_kni_process(port_id, queue_id, pkts_burst, MAX_PKT_BURST); 1482 } 1483 #endif 1484 1485 process_dispatch_ring(port_id, queue_id, pkts_burst, ctx); 1486 1487 nb_rx = rte_eth_rx_burst(port_id, queue_id, pkts_burst, 1488 MAX_PKT_BURST); 1489 if (nb_rx == 0) 1490 continue; 1491 1492 idle = 0; 1493 1494 /* Prefetch first packets */ 1495 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) { 1496 rte_prefetch0(rte_pktmbuf_mtod( 1497 pkts_burst[j], void *)); 1498 } 1499 1500 /* Prefetch and handle already prefetched packets */ 1501 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) { 1502 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[ 1503 j + PREFETCH_OFFSET], void *)); 1504 process_packets(port_id, queue_id, &pkts_burst[j], 1, ctx, 0); 1505 } 1506 1507 /* Handle remaining prefetched packets */ 1508 for (; j < nb_rx; j++) { 1509 process_packets(port_id, queue_id, &pkts_burst[j], 1, ctx, 0); 1510 } 1511 } 1512 1513 process_msg_ring(qconf->proc_id); 1514 1515 div_tsc = rte_rdtsc(); 1516 1517 if (likely(lr->loop != NULL && (!idle || cur_tsc - usch_tsc >= drain_tsc))) { 1518 usch_tsc = cur_tsc; 1519 lr->loop(lr->arg); 1520 } 1521 1522 idle_sleep_tsc = rte_rdtsc(); 1523 if (likely(idle && idle_sleep)) { 1524 usleep(idle_sleep); 1525 end_tsc = rte_rdtsc(); 1526 } else { 1527 end_tsc = idle_sleep_tsc; 1528 } 1529 1530 if (usch_tsc == cur_tsc) { 1531 usr_tsc = idle_sleep_tsc - div_tsc; 1532 } 1533 1534 if (!idle) { 1535 sys_tsc = div_tsc - cur_tsc; 1536 ff_top_status.sys_tsc += sys_tsc; 1537 } 1538 1539 ff_top_status.usr_tsc += usr_tsc; 1540 ff_top_status.work_tsc += end_tsc - cur_tsc; 1541 ff_top_status.idle_tsc += end_tsc - cur_tsc - usr_tsc - sys_tsc; 1542 1543 ff_top_status.loops++; 1544 } 1545 1546 return 0; 1547 } 1548 1549 int 1550 ff_dpdk_if_up(void) { 1551 int i; 1552 struct lcore_conf *qconf = &lcore_conf; 1553 for (i = 0; i < qconf->nb_tx_port; i++) { 1554 uint16_t port_id = qconf->tx_port_id[i]; 1555 1556 struct ff_port_cfg *pconf = &qconf->port_cfgs[port_id]; 1557 veth_ctx[port_id] = ff_veth_attach(pconf); 1558 if (veth_ctx[port_id] == NULL) { 1559 rte_exit(EXIT_FAILURE, "ff_veth_attach failed"); 1560 } 1561 } 1562 1563 return 0; 1564 } 1565 1566 void 1567 ff_dpdk_run(loop_func_t loop, void *arg) { 1568 struct loop_routine *lr = rte_malloc(NULL, 1569 sizeof(struct loop_routine), 0); 1570 lr->loop = loop; 1571 lr->arg = arg; 1572 rte_eal_mp_remote_launch(main_loop, lr, CALL_MASTER); 1573 rte_eal_mp_wait_lcore(); 1574 rte_free(lr); 1575 } 1576 1577 void 1578 ff_dpdk_pktmbuf_free(void *m) 1579 { 1580 rte_pktmbuf_free((struct rte_mbuf *)m); 1581 } 1582 1583 static uint32_t 1584 toeplitz_hash(unsigned keylen, const uint8_t *key, 1585 unsigned datalen, const uint8_t *data) 1586 { 1587 uint32_t hash = 0, v; 1588 u_int i, b; 1589 1590 /* XXXRW: Perhaps an assertion about key length vs. data length? */ 1591 1592 v = (key[0]<<24) + (key[1]<<16) + (key[2] <<8) + key[3]; 1593 for (i = 0; i < datalen; i++) { 1594 for (b = 0; b < 8; b++) { 1595 if (data[i] & (1<<(7-b))) 1596 hash ^= v; 1597 v <<= 1; 1598 if ((i + 4) < keylen && 1599 (key[i+4] & (1<<(7-b)))) 1600 v |= 1; 1601 } 1602 } 1603 return (hash); 1604 } 1605 1606 int 1607 ff_rss_check(void *softc, uint32_t saddr, uint32_t daddr, 1608 uint16_t sport, uint16_t dport) 1609 { 1610 struct lcore_conf *qconf = &lcore_conf; 1611 struct ff_dpdk_if_context *ctx = ff_veth_softc_to_hostc(softc); 1612 uint16_t nb_queues = qconf->nb_queue_list[ctx->port_id]; 1613 1614 if (nb_queues <= 1) { 1615 return 1; 1616 } 1617 1618 uint16_t reta_size = rss_reta_size[ctx->port_id]; 1619 uint16_t queueid = qconf->tx_queue_id[ctx->port_id]; 1620 1621 uint8_t data[sizeof(saddr) + sizeof(daddr) + sizeof(sport) + 1622 sizeof(dport)]; 1623 1624 unsigned datalen = 0; 1625 1626 bcopy(&saddr, &data[datalen], sizeof(saddr)); 1627 datalen += sizeof(saddr); 1628 1629 bcopy(&daddr, &data[datalen], sizeof(daddr)); 1630 datalen += sizeof(daddr); 1631 1632 bcopy(&sport, &data[datalen], sizeof(sport)); 1633 datalen += sizeof(sport); 1634 1635 bcopy(&dport, &data[datalen], sizeof(dport)); 1636 datalen += sizeof(dport); 1637 1638 uint32_t hash = toeplitz_hash(sizeof(default_rsskey_40bytes), 1639 default_rsskey_40bytes, datalen, data); 1640 1641 return ((hash & (reta_size - 1)) % nb_queues) == queueid; 1642 } 1643 1644 void 1645 ff_regist_packet_dispatcher(dispatch_func_t func) 1646 { 1647 packet_dispatcher = func; 1648 } 1649 1650 uint64_t 1651 ff_get_tsc_ns() 1652 { 1653 uint64_t cur_tsc = rte_rdtsc(); 1654 uint64_t hz = rte_get_tsc_hz(); 1655 return ((double)cur_tsc/(double)hz) * NS_PER_S; 1656 } 1657 1658