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