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