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 d_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->s_addr, ð_hdr->d_addr); 426 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->s_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, &d_addr); 431 rte_ether_addr_copy(&d_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->s_addr, ð_hdr->d_addr); 447 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->s_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->s_addr); 523 memset(ð_hdr->d_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