1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2015 Intel Corporation 3 */ 4 5 #include <stdint.h> 6 #include <sys/queue.h> 7 #include <sys/socket.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <stdio.h> 11 #include <assert.h> 12 #include <errno.h> 13 #include <signal.h> 14 #include <stdarg.h> 15 #include <inttypes.h> 16 #include <getopt.h> 17 #include <termios.h> 18 #include <unistd.h> 19 #include <pthread.h> 20 21 #include <rte_common.h> 22 #include <rte_log.h> 23 #include <rte_memory.h> 24 #include <rte_memcpy.h> 25 #include <rte_eal.h> 26 #include <rte_launch.h> 27 #include <rte_atomic.h> 28 #include <rte_cycles.h> 29 #include <rte_prefetch.h> 30 #include <rte_lcore.h> 31 #include <rte_per_lcore.h> 32 #include <rte_branch_prediction.h> 33 #include <rte_interrupts.h> 34 #include <rte_random.h> 35 #include <rte_debug.h> 36 #include <rte_ether.h> 37 #include <rte_ethdev.h> 38 #include <rte_mempool.h> 39 #include <rte_mbuf.h> 40 #include <rte_ip.h> 41 #include <rte_tcp.h> 42 #include <rte_arp.h> 43 #include <rte_spinlock.h> 44 #include <rte_devargs.h> 45 #include <rte_byteorder.h> 46 #include <rte_cpuflags.h> 47 #include <rte_eth_bond.h> 48 49 #include <cmdline_rdline.h> 50 #include <cmdline_parse.h> 51 #include <cmdline_parse_num.h> 52 #include <cmdline_parse_string.h> 53 #include <cmdline_parse_ipaddr.h> 54 #include <cmdline_parse_etheraddr.h> 55 #include <cmdline_socket.h> 56 #include <cmdline.h> 57 58 #include "main.h" 59 60 #define RTE_LOGTYPE_DCB RTE_LOGTYPE_USER1 61 62 #define NB_MBUF (1024*8) 63 64 #define MAX_PKT_BURST 32 65 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ 66 #define BURST_RX_INTERVAL_NS (10) /* RX poll interval ~100ns */ 67 68 /* 69 * RX and TX Prefetch, Host, and Write-back threshold values should be 70 * carefully set for optimal performance. Consult the network 71 * controller's datasheet and supporting DPDK documentation for guidance 72 * on how these parameters should be set. 73 */ 74 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */ 75 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */ 76 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */ 77 #define RX_FTHRESH (MAX_PKT_BURST * 2)/**< Default values of RX free threshold reg. */ 78 79 /* 80 * These default values are optimized for use with the Intel(R) 82599 10 GbE 81 * Controller and the DPDK ixgbe PMD. Consider using other values for other 82 * network controllers and/or network drivers. 83 */ 84 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */ 85 #define TX_HTHRESH 0 /**< Default values of TX host threshold reg. */ 86 #define TX_WTHRESH 0 /**< Default values of TX write-back threshold reg. */ 87 88 /* 89 * Configurable number of RX/TX ring descriptors 90 */ 91 #define RTE_RX_DESC_DEFAULT 1024 92 #define RTE_TX_DESC_DEFAULT 1024 93 94 #define BOND_IP_1 7 95 #define BOND_IP_2 0 96 #define BOND_IP_3 0 97 #define BOND_IP_4 10 98 99 /* not defined under linux */ 100 #ifndef NIPQUAD 101 #define NIPQUAD_FMT "%u.%u.%u.%u" 102 #endif 103 104 #define MAX_PORTS 4 105 #define PRINT_MAC(addr) printf("%02"PRIx8":%02"PRIx8":%02"PRIx8 \ 106 ":%02"PRIx8":%02"PRIx8":%02"PRIx8, \ 107 addr.addr_bytes[0], addr.addr_bytes[1], addr.addr_bytes[2], \ 108 addr.addr_bytes[3], addr.addr_bytes[4], addr.addr_bytes[5]) 109 110 uint16_t slaves[RTE_MAX_ETHPORTS]; 111 uint16_t slaves_count; 112 113 static uint16_t BOND_PORT = 0xffff; 114 115 static struct rte_mempool *mbuf_pool; 116 117 static struct rte_eth_conf port_conf = { 118 .rxmode = { 119 .mq_mode = ETH_MQ_RX_NONE, 120 .max_rx_pkt_len = RTE_ETHER_MAX_LEN, 121 .split_hdr_size = 0, 122 }, 123 .rx_adv_conf = { 124 .rss_conf = { 125 .rss_key = NULL, 126 .rss_hf = ETH_RSS_IP, 127 }, 128 }, 129 .txmode = { 130 .mq_mode = ETH_MQ_TX_NONE, 131 }, 132 }; 133 134 static void 135 slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool) 136 { 137 int retval; 138 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT; 139 uint16_t nb_txd = RTE_TX_DESC_DEFAULT; 140 struct rte_eth_dev_info dev_info; 141 struct rte_eth_rxconf rxq_conf; 142 struct rte_eth_txconf txq_conf; 143 struct rte_eth_conf local_port_conf = port_conf; 144 145 if (!rte_eth_dev_is_valid_port(portid)) 146 rte_exit(EXIT_FAILURE, "Invalid port\n"); 147 148 retval = rte_eth_dev_info_get(portid, &dev_info); 149 if (retval != 0) 150 rte_exit(EXIT_FAILURE, 151 "Error during getting device (port %u) info: %s\n", 152 portid, strerror(-retval)); 153 154 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) 155 local_port_conf.txmode.offloads |= 156 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 157 158 local_port_conf.rx_adv_conf.rss_conf.rss_hf &= 159 dev_info.flow_type_rss_offloads; 160 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf != 161 port_conf.rx_adv_conf.rss_conf.rss_hf) { 162 printf("Port %u modified RSS hash function based on hardware support," 163 "requested:%#"PRIx64" configured:%#"PRIx64"\n", 164 portid, 165 port_conf.rx_adv_conf.rss_conf.rss_hf, 166 local_port_conf.rx_adv_conf.rss_conf.rss_hf); 167 } 168 169 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf); 170 if (retval != 0) 171 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n", 172 portid, retval); 173 174 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd); 175 if (retval != 0) 176 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc " 177 "failed (res=%d)\n", portid, retval); 178 179 /* RX setup */ 180 rxq_conf = dev_info.default_rxconf; 181 rxq_conf.offloads = local_port_conf.rxmode.offloads; 182 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd, 183 rte_eth_dev_socket_id(portid), 184 &rxq_conf, 185 mbuf_pool); 186 if (retval < 0) 187 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)", 188 portid, retval); 189 190 /* TX setup */ 191 txq_conf = dev_info.default_txconf; 192 txq_conf.offloads = local_port_conf.txmode.offloads; 193 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd, 194 rte_eth_dev_socket_id(portid), &txq_conf); 195 196 if (retval < 0) 197 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)", 198 portid, retval); 199 200 retval = rte_eth_dev_start(portid); 201 if (retval < 0) 202 rte_exit(retval, 203 "Start port %d failed (res=%d)", 204 portid, retval); 205 206 struct rte_ether_addr addr; 207 208 retval = rte_eth_macaddr_get(portid, &addr); 209 if (retval != 0) 210 rte_exit(retval, 211 "Mac address get port %d failed (res=%d)", 212 portid, retval); 213 214 printf("Port %u MAC: ", portid); 215 PRINT_MAC(addr); 216 printf("\n"); 217 } 218 219 static void 220 bond_port_init(struct rte_mempool *mbuf_pool) 221 { 222 int retval; 223 uint8_t i; 224 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT; 225 uint16_t nb_txd = RTE_TX_DESC_DEFAULT; 226 struct rte_eth_dev_info dev_info; 227 struct rte_eth_rxconf rxq_conf; 228 struct rte_eth_txconf txq_conf; 229 struct rte_eth_conf local_port_conf = port_conf; 230 uint16_t wait_counter = 20; 231 232 retval = rte_eth_bond_create("net_bonding0", BONDING_MODE_ALB, 233 0 /*SOCKET_ID_ANY*/); 234 if (retval < 0) 235 rte_exit(EXIT_FAILURE, 236 "Faled to create bond port\n"); 237 238 BOND_PORT = retval; 239 240 retval = rte_eth_dev_info_get(BOND_PORT, &dev_info); 241 if (retval != 0) 242 rte_exit(EXIT_FAILURE, 243 "Error during getting device (port %u) info: %s\n", 244 BOND_PORT, strerror(-retval)); 245 246 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) 247 local_port_conf.txmode.offloads |= 248 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 249 retval = rte_eth_dev_configure(BOND_PORT, 1, 1, &local_port_conf); 250 if (retval != 0) 251 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n", 252 BOND_PORT, retval); 253 254 retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd); 255 if (retval != 0) 256 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc " 257 "failed (res=%d)\n", BOND_PORT, retval); 258 259 for (i = 0; i < slaves_count; i++) { 260 if (rte_eth_bond_slave_add(BOND_PORT, slaves[i]) == -1) 261 rte_exit(-1, "Oooops! adding slave (%u) to bond (%u) failed!\n", 262 slaves[i], BOND_PORT); 263 264 } 265 266 /* RX setup */ 267 rxq_conf = dev_info.default_rxconf; 268 rxq_conf.offloads = local_port_conf.rxmode.offloads; 269 retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd, 270 rte_eth_dev_socket_id(BOND_PORT), 271 &rxq_conf, mbuf_pool); 272 if (retval < 0) 273 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)", 274 BOND_PORT, retval); 275 276 /* TX setup */ 277 txq_conf = dev_info.default_txconf; 278 txq_conf.offloads = local_port_conf.txmode.offloads; 279 retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd, 280 rte_eth_dev_socket_id(BOND_PORT), &txq_conf); 281 282 if (retval < 0) 283 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)", 284 BOND_PORT, retval); 285 286 retval = rte_eth_dev_start(BOND_PORT); 287 if (retval < 0) 288 rte_exit(retval, "Start port %d failed (res=%d)", BOND_PORT, retval); 289 290 printf("Waiting for slaves to become active..."); 291 while (wait_counter) { 292 uint16_t act_slaves[16] = {0}; 293 if (rte_eth_bond_active_slaves_get(BOND_PORT, act_slaves, 16) == 294 slaves_count) { 295 printf("\n"); 296 break; 297 } 298 sleep(1); 299 printf("..."); 300 if (--wait_counter == 0) 301 rte_exit(-1, "\nFailed to activate slaves\n"); 302 } 303 304 retval = rte_eth_promiscuous_enable(BOND_PORT); 305 if (retval != 0) { 306 rte_exit(EXIT_FAILURE, 307 "port %u: promiscuous mode enable failed: %s\n", 308 BOND_PORT, rte_strerror(-retval)); 309 return; 310 } 311 312 struct rte_ether_addr addr; 313 314 retval = rte_eth_macaddr_get(BOND_PORT, &addr); 315 if (retval != 0) 316 rte_exit(retval, "port %u: Mac address get failed (res=%d)", 317 BOND_PORT, retval); 318 319 printf("Port %u MAC: ", (unsigned)BOND_PORT); 320 PRINT_MAC(addr); 321 printf("\n"); 322 } 323 324 static inline size_t 325 get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto) 326 { 327 size_t vlan_offset = 0; 328 329 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) { 330 struct rte_vlan_hdr *vlan_hdr = 331 (struct rte_vlan_hdr *)(eth_hdr + 1); 332 333 vlan_offset = sizeof(struct rte_vlan_hdr); 334 *proto = vlan_hdr->eth_proto; 335 336 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) { 337 vlan_hdr = vlan_hdr + 1; 338 339 *proto = vlan_hdr->eth_proto; 340 vlan_offset += sizeof(struct rte_vlan_hdr); 341 } 342 } 343 return vlan_offset; 344 } 345 346 struct global_flag_stru_t { 347 int LcoreMainIsRunning; 348 int LcoreMainCore; 349 uint32_t port_packets[4]; 350 rte_spinlock_t lock; 351 }; 352 struct global_flag_stru_t global_flag_stru; 353 struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru; 354 355 /* 356 * Main thread that does the work, reading from INPUT_PORT 357 * and writing to OUTPUT_PORT 358 */ 359 static int lcore_main(__rte_unused void *arg1) 360 { 361 struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned; 362 struct rte_ether_addr d_addr; 363 364 struct rte_ether_addr bond_mac_addr; 365 struct rte_ether_hdr *eth_hdr; 366 struct rte_arp_hdr *arp_hdr; 367 struct rte_ipv4_hdr *ipv4_hdr; 368 uint16_t ether_type, offset; 369 370 uint16_t rx_cnt; 371 uint32_t bond_ip; 372 int i = 0; 373 uint8_t is_free; 374 int ret; 375 376 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) | 377 (BOND_IP_3 << 16) | (BOND_IP_4 << 24); 378 379 rte_spinlock_trylock(&global_flag_stru_p->lock); 380 381 while (global_flag_stru_p->LcoreMainIsRunning) { 382 rte_spinlock_unlock(&global_flag_stru_p->lock); 383 rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST); 384 is_free = 0; 385 386 /* If didn't receive any packets, wait and go to next iteration */ 387 if (rx_cnt == 0) { 388 rte_delay_us(50); 389 continue; 390 } 391 392 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr); 393 if (ret != 0) { 394 printf("Bond (port %u) MAC address get failed: %s.\n" 395 "%u packets dropped", BOND_PORT, strerror(-ret), 396 rx_cnt); 397 rte_pktmbuf_free(pkts[i]); 398 continue; 399 } 400 401 /* Search incoming data for ARP packets and prepare response */ 402 for (i = 0; i < rx_cnt; i++) { 403 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) { 404 global_flag_stru_p->port_packets[0]++; 405 rte_spinlock_unlock(&global_flag_stru_p->lock); 406 } 407 eth_hdr = rte_pktmbuf_mtod(pkts[i], 408 struct rte_ether_hdr *); 409 ether_type = eth_hdr->ether_type; 410 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) 411 printf("VLAN taged frame, offset:"); 412 offset = get_vlan_offset(eth_hdr, ðer_type); 413 if (offset > 0) 414 printf("%d\n", offset); 415 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) { 416 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) { 417 global_flag_stru_p->port_packets[1]++; 418 rte_spinlock_unlock(&global_flag_stru_p->lock); 419 } 420 arp_hdr = (struct rte_arp_hdr *)( 421 (char *)(eth_hdr + 1) + offset); 422 if (arp_hdr->arp_data.arp_tip == bond_ip) { 423 if (arp_hdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) { 424 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REPLY); 425 /* Switch src and dst data and set bonding MAC */ 426 rte_ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr); 427 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->s_addr); 428 rte_ether_addr_copy(&arp_hdr->arp_data.arp_sha, 429 &arp_hdr->arp_data.arp_tha); 430 arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip; 431 rte_ether_addr_copy(&bond_mac_addr, &d_addr); 432 rte_ether_addr_copy(&d_addr, &arp_hdr->arp_data.arp_sha); 433 arp_hdr->arp_data.arp_sip = bond_ip; 434 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1); 435 is_free = 1; 436 } else { 437 rte_eth_tx_burst(BOND_PORT, 0, NULL, 0); 438 } 439 } 440 } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) { 441 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) { 442 global_flag_stru_p->port_packets[2]++; 443 rte_spinlock_unlock(&global_flag_stru_p->lock); 444 } 445 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)(eth_hdr + 1) + offset); 446 if (ipv4_hdr->dst_addr == bond_ip) { 447 rte_ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr); 448 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->s_addr); 449 ipv4_hdr->dst_addr = ipv4_hdr->src_addr; 450 ipv4_hdr->src_addr = bond_ip; 451 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1); 452 } 453 454 } 455 456 /* Free processed packets */ 457 if (is_free == 0) 458 rte_pktmbuf_free(pkts[i]); 459 } 460 rte_spinlock_trylock(&global_flag_stru_p->lock); 461 } 462 rte_spinlock_unlock(&global_flag_stru_p->lock); 463 printf("BYE lcore_main\n"); 464 return 0; 465 } 466 467 struct cmd_obj_send_result { 468 cmdline_fixed_string_t action; 469 cmdline_ipaddr_t ip; 470 }; 471 static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size) 472 { 473 snprintf(buf, size, NIPQUAD_FMT, 474 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[0]), 475 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[1]), 476 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[2]), 477 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[3]) 478 ); 479 } 480 static void cmd_obj_send_parsed(void *parsed_result, 481 __rte_unused struct cmdline *cl, 482 __rte_unused void *data) 483 { 484 485 struct cmd_obj_send_result *res = parsed_result; 486 char ip_str[INET6_ADDRSTRLEN]; 487 488 struct rte_ether_addr bond_mac_addr; 489 struct rte_mbuf *created_pkt; 490 struct rte_ether_hdr *eth_hdr; 491 struct rte_arp_hdr *arp_hdr; 492 493 uint32_t bond_ip; 494 size_t pkt_size; 495 int ret; 496 497 if (res->ip.family == AF_INET) 498 get_string(res, ip_str, INET_ADDRSTRLEN); 499 else 500 cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n"); 501 502 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) | 503 (BOND_IP_3 << 16) | (BOND_IP_4 << 24); 504 505 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr); 506 if (ret != 0) { 507 cmdline_printf(cl, 508 "Failed to get bond (port %u) MAC address: %s\n", 509 BOND_PORT, strerror(-ret)); 510 } 511 512 created_pkt = rte_pktmbuf_alloc(mbuf_pool); 513 if (created_pkt == NULL) { 514 cmdline_printf(cl, "Failed to allocate mbuf\n"); 515 return; 516 } 517 518 pkt_size = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr); 519 created_pkt->data_len = pkt_size; 520 created_pkt->pkt_len = pkt_size; 521 522 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *); 523 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->s_addr); 524 memset(ð_hdr->d_addr, 0xFF, RTE_ETHER_ADDR_LEN); 525 eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP); 526 527 arp_hdr = (struct rte_arp_hdr *)( 528 (char *)eth_hdr + sizeof(struct rte_ether_hdr)); 529 arp_hdr->arp_hardware = rte_cpu_to_be_16(RTE_ARP_HRD_ETHER); 530 arp_hdr->arp_protocol = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4); 531 arp_hdr->arp_hlen = RTE_ETHER_ADDR_LEN; 532 arp_hdr->arp_plen = sizeof(uint32_t); 533 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REQUEST); 534 535 rte_ether_addr_copy(&bond_mac_addr, &arp_hdr->arp_data.arp_sha); 536 arp_hdr->arp_data.arp_sip = bond_ip; 537 memset(&arp_hdr->arp_data.arp_tha, 0, RTE_ETHER_ADDR_LEN); 538 arp_hdr->arp_data.arp_tip = 539 ((unsigned char *)&res->ip.addr.ipv4)[0] | 540 (((unsigned char *)&res->ip.addr.ipv4)[1] << 8) | 541 (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) | 542 (((unsigned char *)&res->ip.addr.ipv4)[3] << 24); 543 rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1); 544 545 rte_delay_ms(100); 546 cmdline_printf(cl, "\n"); 547 } 548 549 cmdline_parse_token_string_t cmd_obj_action_send = 550 TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send"); 551 cmdline_parse_token_ipaddr_t cmd_obj_ip = 552 TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip); 553 554 cmdline_parse_inst_t cmd_obj_send = { 555 .f = cmd_obj_send_parsed, /* function to call */ 556 .data = NULL, /* 2nd arg of func */ 557 .help_str = "send client_ip", 558 .tokens = { /* token list, NULL terminated */ 559 (void *)&cmd_obj_action_send, 560 (void *)&cmd_obj_ip, 561 NULL, 562 }, 563 }; 564 565 struct cmd_start_result { 566 cmdline_fixed_string_t start; 567 }; 568 569 static void cmd_start_parsed(__rte_unused void *parsed_result, 570 struct cmdline *cl, 571 __rte_unused void *data) 572 { 573 int worker_core_id = rte_lcore_id(); 574 575 rte_spinlock_trylock(&global_flag_stru_p->lock); 576 if (global_flag_stru_p->LcoreMainIsRunning == 0) { 577 if (rte_eal_get_lcore_state(global_flag_stru_p->LcoreMainCore) 578 != WAIT) { 579 rte_spinlock_unlock(&global_flag_stru_p->lock); 580 return; 581 } 582 rte_spinlock_unlock(&global_flag_stru_p->lock); 583 } else { 584 cmdline_printf(cl, "lcore_main already running on core:%d\n", 585 global_flag_stru_p->LcoreMainCore); 586 rte_spinlock_unlock(&global_flag_stru_p->lock); 587 return; 588 } 589 590 /* start lcore main on core != main_core - ARP response thread */ 591 worker_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0); 592 if ((worker_core_id >= RTE_MAX_LCORE) || (worker_core_id == 0)) 593 return; 594 595 rte_spinlock_trylock(&global_flag_stru_p->lock); 596 global_flag_stru_p->LcoreMainIsRunning = 1; 597 rte_spinlock_unlock(&global_flag_stru_p->lock); 598 cmdline_printf(cl, 599 "Starting lcore_main on core %d:%d " 600 "Our IP:%d.%d.%d.%d\n", 601 worker_core_id, 602 rte_eal_remote_launch(lcore_main, NULL, worker_core_id), 603 BOND_IP_1, 604 BOND_IP_2, 605 BOND_IP_3, 606 BOND_IP_4 607 ); 608 } 609 610 cmdline_parse_token_string_t cmd_start_start = 611 TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start"); 612 613 cmdline_parse_inst_t cmd_start = { 614 .f = cmd_start_parsed, /* function to call */ 615 .data = NULL, /* 2nd arg of func */ 616 .help_str = "starts listening if not started at startup", 617 .tokens = { /* token list, NULL terminated */ 618 (void *)&cmd_start_start, 619 NULL, 620 }, 621 }; 622 623 struct cmd_help_result { 624 cmdline_fixed_string_t help; 625 }; 626 627 static void cmd_help_parsed(__rte_unused void *parsed_result, 628 struct cmdline *cl, 629 __rte_unused void *data) 630 { 631 cmdline_printf(cl, 632 "ALB - link bonding mode 6 example\n" 633 "send IP - sends one ARPrequest through bonding for IP.\n" 634 "start - starts listening ARPs.\n" 635 "stop - stops lcore_main.\n" 636 "show - shows some bond info: ex. active slaves etc.\n" 637 "help - prints help.\n" 638 "quit - terminate all threads and quit.\n" 639 ); 640 } 641 642 cmdline_parse_token_string_t cmd_help_help = 643 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help"); 644 645 cmdline_parse_inst_t cmd_help = { 646 .f = cmd_help_parsed, /* function to call */ 647 .data = NULL, /* 2nd arg of func */ 648 .help_str = "show help", 649 .tokens = { /* token list, NULL terminated */ 650 (void *)&cmd_help_help, 651 NULL, 652 }, 653 }; 654 655 struct cmd_stop_result { 656 cmdline_fixed_string_t stop; 657 }; 658 659 static void cmd_stop_parsed(__rte_unused void *parsed_result, 660 struct cmdline *cl, 661 __rte_unused void *data) 662 { 663 rte_spinlock_trylock(&global_flag_stru_p->lock); 664 if (global_flag_stru_p->LcoreMainIsRunning == 0) { 665 cmdline_printf(cl, 666 "lcore_main not running on core:%d\n", 667 global_flag_stru_p->LcoreMainCore); 668 rte_spinlock_unlock(&global_flag_stru_p->lock); 669 return; 670 } 671 global_flag_stru_p->LcoreMainIsRunning = 0; 672 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0) 673 cmdline_printf(cl, 674 "error: lcore_main can not stop on core:%d\n", 675 global_flag_stru_p->LcoreMainCore); 676 else 677 cmdline_printf(cl, 678 "lcore_main stopped on core:%d\n", 679 global_flag_stru_p->LcoreMainCore); 680 rte_spinlock_unlock(&global_flag_stru_p->lock); 681 } 682 683 cmdline_parse_token_string_t cmd_stop_stop = 684 TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop"); 685 686 cmdline_parse_inst_t cmd_stop = { 687 .f = cmd_stop_parsed, /* function to call */ 688 .data = NULL, /* 2nd arg of func */ 689 .help_str = "this command do not handle any arguments", 690 .tokens = { /* token list, NULL terminated */ 691 (void *)&cmd_stop_stop, 692 NULL, 693 }, 694 }; 695 696 struct cmd_quit_result { 697 cmdline_fixed_string_t quit; 698 }; 699 700 static void cmd_quit_parsed(__rte_unused void *parsed_result, 701 struct cmdline *cl, 702 __rte_unused void *data) 703 { 704 rte_spinlock_trylock(&global_flag_stru_p->lock); 705 if (global_flag_stru_p->LcoreMainIsRunning == 0) { 706 cmdline_printf(cl, 707 "lcore_main not running on core:%d\n", 708 global_flag_stru_p->LcoreMainCore); 709 rte_spinlock_unlock(&global_flag_stru_p->lock); 710 cmdline_quit(cl); 711 return; 712 } 713 global_flag_stru_p->LcoreMainIsRunning = 0; 714 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0) 715 cmdline_printf(cl, 716 "error: lcore_main can not stop on core:%d\n", 717 global_flag_stru_p->LcoreMainCore); 718 else 719 cmdline_printf(cl, 720 "lcore_main stopped on core:%d\n", 721 global_flag_stru_p->LcoreMainCore); 722 rte_spinlock_unlock(&global_flag_stru_p->lock); 723 cmdline_quit(cl); 724 } 725 726 cmdline_parse_token_string_t cmd_quit_quit = 727 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit"); 728 729 cmdline_parse_inst_t cmd_quit = { 730 .f = cmd_quit_parsed, /* function to call */ 731 .data = NULL, /* 2nd arg of func */ 732 .help_str = "this command do not handle any arguments", 733 .tokens = { /* token list, NULL terminated */ 734 (void *)&cmd_quit_quit, 735 NULL, 736 }, 737 }; 738 739 struct cmd_show_result { 740 cmdline_fixed_string_t show; 741 }; 742 743 static void cmd_show_parsed(__rte_unused void *parsed_result, 744 struct cmdline *cl, 745 __rte_unused void *data) 746 { 747 uint16_t slaves[16] = {0}; 748 uint8_t len = 16; 749 struct rte_ether_addr addr; 750 uint16_t i; 751 int ret; 752 753 for (i = 0; i < slaves_count; i++) { 754 ret = rte_eth_macaddr_get(i, &addr); 755 if (ret != 0) { 756 cmdline_printf(cl, 757 "Failed to get port %u MAC address: %s\n", 758 i, strerror(-ret)); 759 continue; 760 } 761 762 PRINT_MAC(addr); 763 printf("\n"); 764 } 765 766 rte_spinlock_trylock(&global_flag_stru_p->lock); 767 cmdline_printf(cl, 768 "Active_slaves:%d " 769 "packets received:Tot:%d Arp:%d IPv4:%d\n", 770 rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len), 771 global_flag_stru_p->port_packets[0], 772 global_flag_stru_p->port_packets[1], 773 global_flag_stru_p->port_packets[2]); 774 rte_spinlock_unlock(&global_flag_stru_p->lock); 775 } 776 777 cmdline_parse_token_string_t cmd_show_show = 778 TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show"); 779 780 cmdline_parse_inst_t cmd_show = { 781 .f = cmd_show_parsed, /* function to call */ 782 .data = NULL, /* 2nd arg of func */ 783 .help_str = "this command do not handle any arguments", 784 .tokens = { /* token list, NULL terminated */ 785 (void *)&cmd_show_show, 786 NULL, 787 }, 788 }; 789 790 /****** CONTEXT (list of instruction) */ 791 792 cmdline_parse_ctx_t main_ctx[] = { 793 (cmdline_parse_inst_t *)&cmd_start, 794 (cmdline_parse_inst_t *)&cmd_obj_send, 795 (cmdline_parse_inst_t *)&cmd_stop, 796 (cmdline_parse_inst_t *)&cmd_show, 797 (cmdline_parse_inst_t *)&cmd_quit, 798 (cmdline_parse_inst_t *)&cmd_help, 799 NULL, 800 }; 801 802 /* prompt function, called from main on MAIN lcore */ 803 static void prompt(__rte_unused void *arg1) 804 { 805 struct cmdline *cl; 806 807 cl = cmdline_stdin_new(main_ctx, "bond6>"); 808 if (cl != NULL) { 809 cmdline_interact(cl); 810 cmdline_stdin_exit(cl); 811 } 812 } 813 814 /* Main function, does initialisation and calls the per-lcore functions */ 815 int 816 main(int argc, char *argv[]) 817 { 818 int ret, worker_core_id; 819 uint16_t nb_ports, i; 820 821 /* init EAL */ 822 ret = rte_eal_init(argc, argv); 823 rte_devargs_dump(stdout); 824 if (ret < 0) 825 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n"); 826 argc -= ret; 827 argv += ret; 828 829 nb_ports = rte_eth_dev_count_avail(); 830 if (nb_ports == 0) 831 rte_exit(EXIT_FAILURE, "Give at least one port\n"); 832 else if (nb_ports > MAX_PORTS) 833 rte_exit(EXIT_FAILURE, "You can have max 4 ports\n"); 834 835 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32, 836 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 837 if (mbuf_pool == NULL) 838 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); 839 840 /* initialize all ports */ 841 slaves_count = nb_ports; 842 RTE_ETH_FOREACH_DEV(i) { 843 slave_port_init(i, mbuf_pool); 844 slaves[i] = i; 845 } 846 847 bond_port_init(mbuf_pool); 848 849 rte_spinlock_init(&global_flag_stru_p->lock); 850 851 /* check state of lcores */ 852 RTE_LCORE_FOREACH_WORKER(worker_core_id) { 853 if (rte_eal_get_lcore_state(worker_core_id) != WAIT) 854 return -EBUSY; 855 } 856 857 /* start lcore main on core != main_core - ARP response thread */ 858 worker_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0); 859 if ((worker_core_id >= RTE_MAX_LCORE) || (worker_core_id == 0)) 860 return -EPERM; 861 862 global_flag_stru_p->LcoreMainIsRunning = 1; 863 global_flag_stru_p->LcoreMainCore = worker_core_id; 864 printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n", 865 worker_core_id, 866 rte_eal_remote_launch((lcore_function_t *)lcore_main, 867 NULL, 868 worker_core_id), 869 BOND_IP_1, 870 BOND_IP_2, 871 BOND_IP_3, 872 BOND_IP_4 873 ); 874 875 /* Start prompt for user interact */ 876 prompt(NULL); 877 878 rte_delay_ms(100); 879 return 0; 880 } 881