1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
7 #include <sys/socket.h>
21 #include <rte_common.h>
23 #include <rte_memory.h>
24 #include <rte_memcpy.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>
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>
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>
59 #define RTE_LOGTYPE_DCB RTE_LOGTYPE_USER1
61 #define NB_MBUF (1024*8)
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 */
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.
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. */
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.
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. */
88 * Configurable number of RX/TX ring descriptors
90 #define RTE_RX_DESC_DEFAULT 1024
91 #define RTE_TX_DESC_DEFAULT 1024
98 /* not defined under linux */
100 #define NIPQUAD_FMT "%u.%u.%u.%u"
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))
108 uint16_t slaves[RTE_MAX_ETHPORTS];
109 uint16_t slaves_count;
111 static uint16_t BOND_PORT = 0xffff;
113 static struct rte_mempool *mbuf_pool;
115 static struct rte_eth_conf port_conf = {
117 .mq_mode = ETH_MQ_RX_NONE,
118 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
124 .rss_hf = ETH_RSS_IP,
128 .mq_mode = ETH_MQ_TX_NONE,
133 slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool)
136 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
137 uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
138 struct rte_eth_dev_info dev_info;
139 struct rte_eth_rxconf rxq_conf;
140 struct rte_eth_txconf txq_conf;
141 struct rte_eth_conf local_port_conf = port_conf;
143 if (!rte_eth_dev_is_valid_port(portid))
144 rte_exit(EXIT_FAILURE, "Invalid port\n");
146 retval = rte_eth_dev_info_get(portid, &dev_info);
148 rte_exit(EXIT_FAILURE,
149 "Error during getting device (port %u) info: %s\n",
150 portid, strerror(-retval));
152 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
153 local_port_conf.txmode.offloads |=
154 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
156 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
157 dev_info.flow_type_rss_offloads;
158 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
159 port_conf.rx_adv_conf.rss_conf.rss_hf) {
160 printf("Port %u modified RSS hash function based on hardware support,"
161 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
163 port_conf.rx_adv_conf.rss_conf.rss_hf,
164 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
167 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
169 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
172 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
174 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
175 "failed (res=%d)\n", portid, retval);
178 rxq_conf = dev_info.default_rxconf;
179 rxq_conf.offloads = local_port_conf.rxmode.offloads;
180 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
181 rte_eth_dev_socket_id(portid),
185 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
189 txq_conf = dev_info.default_txconf;
190 txq_conf.offloads = local_port_conf.txmode.offloads;
191 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
192 rte_eth_dev_socket_id(portid), &txq_conf);
195 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
198 retval = rte_eth_dev_start(portid);
201 "Start port %d failed (res=%d)",
204 struct rte_ether_addr addr;
206 retval = rte_eth_macaddr_get(portid, &addr);
209 "Mac address get port %d failed (res=%d)",
212 printf("Port %u MAC: ", portid);
218 bond_port_init(struct rte_mempool *mbuf_pool)
222 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
223 uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
224 struct rte_eth_dev_info dev_info;
225 struct rte_eth_rxconf rxq_conf;
226 struct rte_eth_txconf txq_conf;
227 struct rte_eth_conf local_port_conf = port_conf;
228 uint16_t wait_counter = 20;
230 retval = rte_eth_bond_create("net_bonding0", BONDING_MODE_ALB,
231 0 /*SOCKET_ID_ANY*/);
233 rte_exit(EXIT_FAILURE,
234 "Faled to create bond port\n");
238 retval = rte_eth_dev_info_get(BOND_PORT, &dev_info);
240 rte_exit(EXIT_FAILURE,
241 "Error during getting device (port %u) info: %s\n",
242 BOND_PORT, strerror(-retval));
244 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
245 local_port_conf.txmode.offloads |=
246 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
247 retval = rte_eth_dev_configure(BOND_PORT, 1, 1, &local_port_conf);
249 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
252 retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd);
254 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
255 "failed (res=%d)\n", BOND_PORT, retval);
257 for (i = 0; i < slaves_count; i++) {
258 if (rte_eth_bond_slave_add(BOND_PORT, slaves[i]) == -1)
259 rte_exit(-1, "Oooops! adding slave (%u) to bond (%u) failed!\n",
260 slaves[i], BOND_PORT);
265 rxq_conf = dev_info.default_rxconf;
266 rxq_conf.offloads = local_port_conf.rxmode.offloads;
267 retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd,
268 rte_eth_dev_socket_id(BOND_PORT),
269 &rxq_conf, mbuf_pool);
271 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
275 txq_conf = dev_info.default_txconf;
276 txq_conf.offloads = local_port_conf.txmode.offloads;
277 retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd,
278 rte_eth_dev_socket_id(BOND_PORT), &txq_conf);
281 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
284 retval = rte_eth_dev_start(BOND_PORT);
286 rte_exit(retval, "Start port %d failed (res=%d)", BOND_PORT, retval);
288 printf("Waiting for slaves to become active...");
289 while (wait_counter) {
290 uint16_t act_slaves[16] = {0};
291 if (rte_eth_bond_active_slaves_get(BOND_PORT, act_slaves, 16) ==
298 if (--wait_counter == 0)
299 rte_exit(-1, "\nFailed to activate slaves\n");
302 retval = rte_eth_promiscuous_enable(BOND_PORT);
304 rte_exit(EXIT_FAILURE,
305 "port %u: promiscuous mode enable failed: %s\n",
306 BOND_PORT, rte_strerror(-retval));
310 struct rte_ether_addr addr;
312 retval = rte_eth_macaddr_get(BOND_PORT, &addr);
314 rte_exit(retval, "port %u: Mac address get failed (res=%d)",
317 printf("Port %u MAC: ", (unsigned)BOND_PORT);
323 get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto)
325 size_t vlan_offset = 0;
327 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
328 struct rte_vlan_hdr *vlan_hdr =
329 (struct rte_vlan_hdr *)(eth_hdr + 1);
331 vlan_offset = sizeof(struct rte_vlan_hdr);
332 *proto = vlan_hdr->eth_proto;
334 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
335 vlan_hdr = vlan_hdr + 1;
337 *proto = vlan_hdr->eth_proto;
338 vlan_offset += sizeof(struct rte_vlan_hdr);
344 struct global_flag_stru_t {
345 int LcoreMainIsRunning;
347 uint32_t port_packets[4];
350 struct global_flag_stru_t global_flag_stru;
351 struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru;
354 * Main thread that does the work, reading from INPUT_PORT
355 * and writing to OUTPUT_PORT
357 static int lcore_main(__rte_unused void *arg1)
359 struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned;
360 struct rte_ether_addr dst_addr;
362 struct rte_ether_addr bond_mac_addr;
363 struct rte_ether_hdr *eth_hdr;
364 struct rte_arp_hdr *arp_hdr;
365 struct rte_ipv4_hdr *ipv4_hdr;
366 uint16_t ether_type, offset;
374 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
375 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
377 rte_spinlock_trylock(&global_flag_stru_p->lock);
379 while (global_flag_stru_p->LcoreMainIsRunning) {
380 rte_spinlock_unlock(&global_flag_stru_p->lock);
381 rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST);
384 /* If didn't receive any packets, wait and go to next iteration */
390 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
392 printf("Bond (port %u) MAC address get failed: %s.\n"
393 "%u packets dropped", BOND_PORT, strerror(-ret),
395 rte_pktmbuf_free(pkts[i]);
399 /* Search incoming data for ARP packets and prepare response */
400 for (i = 0; i < rx_cnt; i++) {
401 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
402 global_flag_stru_p->port_packets[0]++;
403 rte_spinlock_unlock(&global_flag_stru_p->lock);
405 eth_hdr = rte_pktmbuf_mtod(pkts[i],
406 struct rte_ether_hdr *);
407 ether_type = eth_hdr->ether_type;
408 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
409 printf("VLAN taged frame, offset:");
410 offset = get_vlan_offset(eth_hdr, ðer_type);
412 printf("%d\n", offset);
413 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
414 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
415 global_flag_stru_p->port_packets[1]++;
416 rte_spinlock_unlock(&global_flag_stru_p->lock);
418 arp_hdr = (struct rte_arp_hdr *)(
419 (char *)(eth_hdr + 1) + offset);
420 if (arp_hdr->arp_data.arp_tip == bond_ip) {
421 if (arp_hdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) {
422 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REPLY);
423 /* Switch src and dst data and set bonding MAC */
424 rte_ether_addr_copy(ð_hdr->src_addr, ð_hdr->dst_addr);
425 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->src_addr);
426 rte_ether_addr_copy(&arp_hdr->arp_data.arp_sha,
427 &arp_hdr->arp_data.arp_tha);
428 arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip;
429 rte_ether_addr_copy(&bond_mac_addr, &dst_addr);
430 rte_ether_addr_copy(&dst_addr, &arp_hdr->arp_data.arp_sha);
431 arp_hdr->arp_data.arp_sip = bond_ip;
432 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
435 rte_eth_tx_burst(BOND_PORT, 0, NULL, 0);
438 } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
439 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
440 global_flag_stru_p->port_packets[2]++;
441 rte_spinlock_unlock(&global_flag_stru_p->lock);
443 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)(eth_hdr + 1) + offset);
444 if (ipv4_hdr->dst_addr == bond_ip) {
445 rte_ether_addr_copy(ð_hdr->src_addr,
447 rte_ether_addr_copy(&bond_mac_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);
456 /* Free processed packets */
458 rte_pktmbuf_free(pkts[i]);
460 rte_spinlock_trylock(&global_flag_stru_p->lock);
462 rte_spinlock_unlock(&global_flag_stru_p->lock);
463 printf("BYE lcore_main\n");
467 struct cmd_obj_send_result {
468 cmdline_fixed_string_t action;
471 static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size)
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])
480 static void cmd_obj_send_parsed(void *parsed_result,
481 __rte_unused struct cmdline *cl,
482 __rte_unused void *data)
485 struct cmd_obj_send_result *res = parsed_result;
486 char ip_str[INET6_ADDRSTRLEN];
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;
497 if (res->ip.family == AF_INET)
498 get_string(res, ip_str, INET_ADDRSTRLEN);
500 cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n");
502 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
503 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
505 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
508 "Failed to get bond (port %u) MAC address: %s\n",
509 BOND_PORT, strerror(-ret));
512 created_pkt = rte_pktmbuf_alloc(mbuf_pool);
513 if (created_pkt == NULL) {
514 cmdline_printf(cl, "Failed to allocate mbuf\n");
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;
522 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
523 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->src_addr);
524 memset(ð_hdr->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN);
525 eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP);
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);
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);
546 cmdline_printf(cl, "\n");
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);
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,
565 struct cmd_start_result {
566 cmdline_fixed_string_t start;
569 static void cmd_start_parsed(__rte_unused void *parsed_result,
571 __rte_unused void *data)
573 int worker_core_id = rte_lcore_id();
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)
579 rte_spinlock_unlock(&global_flag_stru_p->lock);
582 rte_spinlock_unlock(&global_flag_stru_p->lock);
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);
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))
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);
599 "Starting lcore_main on core %d:%d "
600 "Our IP:%d.%d.%d.%d\n",
602 rte_eal_remote_launch(lcore_main, NULL, worker_core_id),
610 cmdline_parse_token_string_t cmd_start_start =
611 TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start");
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,
623 struct cmd_help_result {
624 cmdline_fixed_string_t help;
627 static void cmd_help_parsed(__rte_unused void *parsed_result,
629 __rte_unused void *data)
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"
642 cmdline_parse_token_string_t cmd_help_help =
643 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
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,
655 struct cmd_stop_result {
656 cmdline_fixed_string_t stop;
659 static void cmd_stop_parsed(__rte_unused void *parsed_result,
661 __rte_unused void *data)
663 rte_spinlock_trylock(&global_flag_stru_p->lock);
664 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
666 "lcore_main not running on core:%d\n",
667 global_flag_stru_p->LcoreMainCore);
668 rte_spinlock_unlock(&global_flag_stru_p->lock);
671 global_flag_stru_p->LcoreMainIsRunning = 0;
672 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
674 "error: lcore_main can not stop on core:%d\n",
675 global_flag_stru_p->LcoreMainCore);
678 "lcore_main stopped on core:%d\n",
679 global_flag_stru_p->LcoreMainCore);
680 rte_spinlock_unlock(&global_flag_stru_p->lock);
683 cmdline_parse_token_string_t cmd_stop_stop =
684 TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop");
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,
696 struct cmd_quit_result {
697 cmdline_fixed_string_t quit;
700 static void cmd_quit_parsed(__rte_unused void *parsed_result,
702 __rte_unused void *data)
704 rte_spinlock_trylock(&global_flag_stru_p->lock);
705 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
707 "lcore_main not running on core:%d\n",
708 global_flag_stru_p->LcoreMainCore);
709 rte_spinlock_unlock(&global_flag_stru_p->lock);
713 global_flag_stru_p->LcoreMainIsRunning = 0;
714 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
716 "error: lcore_main can not stop on core:%d\n",
717 global_flag_stru_p->LcoreMainCore);
720 "lcore_main stopped on core:%d\n",
721 global_flag_stru_p->LcoreMainCore);
722 rte_spinlock_unlock(&global_flag_stru_p->lock);
726 cmdline_parse_token_string_t cmd_quit_quit =
727 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
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,
739 struct cmd_show_result {
740 cmdline_fixed_string_t show;
743 static void cmd_show_parsed(__rte_unused void *parsed_result,
745 __rte_unused void *data)
747 uint16_t slaves[16] = {0};
749 struct rte_ether_addr addr;
753 for (i = 0; i < slaves_count; i++) {
754 ret = rte_eth_macaddr_get(i, &addr);
757 "Failed to get port %u MAC address: %s\n",
766 rte_spinlock_trylock(&global_flag_stru_p->lock);
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);
777 cmdline_parse_token_string_t cmd_show_show =
778 TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show");
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,
790 /****** CONTEXT (list of instruction) */
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,
802 /* prompt function, called from main on MAIN lcore */
803 static void prompt(__rte_unused void *arg1)
807 cl = cmdline_stdin_new(main_ctx, "bond6>");
809 cmdline_interact(cl);
810 cmdline_stdin_exit(cl);
814 /* Main function, does initialisation and calls the per-lcore functions */
816 main(int argc, char *argv[])
818 int ret, worker_core_id;
819 uint16_t nb_ports, i;
822 ret = rte_eal_init(argc, argv);
823 rte_devargs_dump(stdout);
825 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
829 nb_ports = rte_eth_dev_count_avail();
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");
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");
840 /* initialize all ports */
841 slaves_count = nb_ports;
842 RTE_ETH_FOREACH_DEV(i) {
843 slave_port_init(i, mbuf_pool);
847 bond_port_init(mbuf_pool);
849 rte_spinlock_init(&global_flag_stru_p->lock);
851 /* check state of lcores */
852 RTE_LCORE_FOREACH_WORKER(worker_core_id) {
853 if (rte_eal_get_lcore_state(worker_core_id) != WAIT)
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))
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",
866 rte_eal_remote_launch((lcore_function_t *)lcore_main,
875 /* Start prompt for user interact */
880 /* clean up the EAL */