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_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>
43 #include <rte_spinlock.h>
45 #include <cmdline_rdline.h>
46 #include <cmdline_parse.h>
47 #include <cmdline_parse_num.h>
48 #include <cmdline_parse_string.h>
49 #include <cmdline_parse_ipaddr.h>
50 #include <cmdline_parse_etheraddr.h>
51 #include <cmdline_socket.h>
56 #include <rte_devargs.h>
59 #include "rte_byteorder.h"
60 #include "rte_cpuflags.h"
61 #include "rte_eth_bond.h"
63 #define RTE_LOGTYPE_DCB RTE_LOGTYPE_USER1
65 #define NB_MBUF (1024*8)
67 #define MAX_PKT_BURST 32
68 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
69 #define BURST_RX_INTERVAL_NS (10) /* RX poll interval ~100ns */
72 * RX and TX Prefetch, Host, and Write-back threshold values should be
73 * carefully set for optimal performance. Consult the network
74 * controller's datasheet and supporting DPDK documentation for guidance
75 * on how these parameters should be set.
77 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
78 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
79 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
80 #define RX_FTHRESH (MAX_PKT_BURST * 2)/**< Default values of RX free threshold reg. */
83 * These default values are optimized for use with the Intel(R) 82599 10 GbE
84 * Controller and the DPDK ixgbe PMD. Consider using other values for other
85 * network controllers and/or network drivers.
87 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
88 #define TX_HTHRESH 0 /**< Default values of TX host threshold reg. */
89 #define TX_WTHRESH 0 /**< Default values of TX write-back threshold reg. */
92 * Configurable number of RX/TX ring descriptors
94 #define RTE_RX_DESC_DEFAULT 1024
95 #define RTE_TX_DESC_DEFAULT 1024
102 /* not defined under linux */
104 #define NIPQUAD_FMT "%u.%u.%u.%u"
108 #define PRINT_MAC(addr) printf("%02"PRIx8":%02"PRIx8":%02"PRIx8 \
109 ":%02"PRIx8":%02"PRIx8":%02"PRIx8, \
110 addr.addr_bytes[0], addr.addr_bytes[1], addr.addr_bytes[2], \
111 addr.addr_bytes[3], addr.addr_bytes[4], addr.addr_bytes[5])
113 uint16_t slaves[RTE_MAX_ETHPORTS];
114 uint16_t slaves_count;
116 static uint16_t BOND_PORT = 0xffff;
118 static struct rte_mempool *mbuf_pool;
120 static struct rte_eth_conf port_conf = {
122 .mq_mode = ETH_MQ_RX_NONE,
123 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
129 .rss_hf = ETH_RSS_IP,
133 .mq_mode = ETH_MQ_TX_NONE,
138 slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool)
141 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
142 uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
143 struct rte_eth_dev_info dev_info;
144 struct rte_eth_rxconf rxq_conf;
145 struct rte_eth_txconf txq_conf;
146 struct rte_eth_conf local_port_conf = port_conf;
148 if (!rte_eth_dev_is_valid_port(portid))
149 rte_exit(EXIT_FAILURE, "Invalid port\n");
151 retval = rte_eth_dev_info_get(portid, &dev_info);
153 rte_exit(EXIT_FAILURE,
154 "Error during getting device (port %u) info: %s\n",
155 portid, strerror(-retval));
157 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
158 local_port_conf.txmode.offloads |=
159 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
161 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
162 dev_info.flow_type_rss_offloads;
163 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
164 port_conf.rx_adv_conf.rss_conf.rss_hf) {
165 printf("Port %u modified RSS hash function based on hardware support,"
166 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
168 port_conf.rx_adv_conf.rss_conf.rss_hf,
169 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
172 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
174 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
177 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
179 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
180 "failed (res=%d)\n", portid, retval);
183 rxq_conf = dev_info.default_rxconf;
184 rxq_conf.offloads = local_port_conf.rxmode.offloads;
185 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
186 rte_eth_dev_socket_id(portid),
190 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
194 txq_conf = dev_info.default_txconf;
195 txq_conf.offloads = local_port_conf.txmode.offloads;
196 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
197 rte_eth_dev_socket_id(portid), &txq_conf);
200 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
203 retval = rte_eth_dev_start(portid);
206 "Start port %d failed (res=%d)",
209 struct rte_ether_addr addr;
211 retval = rte_eth_macaddr_get(portid, &addr);
214 "Mac address get port %d failed (res=%d)",
217 printf("Port %u MAC: ", portid);
223 bond_port_init(struct rte_mempool *mbuf_pool)
227 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
228 uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
229 struct rte_eth_dev_info dev_info;
230 struct rte_eth_rxconf rxq_conf;
231 struct rte_eth_txconf txq_conf;
232 struct rte_eth_conf local_port_conf = port_conf;
233 uint16_t wait_counter = 20;
235 retval = rte_eth_bond_create("net_bonding0", BONDING_MODE_ALB,
236 0 /*SOCKET_ID_ANY*/);
238 rte_exit(EXIT_FAILURE,
239 "Faled to create bond port\n");
243 retval = rte_eth_dev_info_get(BOND_PORT, &dev_info);
245 rte_exit(EXIT_FAILURE,
246 "Error during getting device (port %u) info: %s\n",
247 BOND_PORT, strerror(-retval));
249 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
250 local_port_conf.txmode.offloads |=
251 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
252 retval = rte_eth_dev_configure(BOND_PORT, 1, 1, &local_port_conf);
254 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
257 retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd);
259 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
260 "failed (res=%d)\n", BOND_PORT, retval);
262 for (i = 0; i < slaves_count; i++) {
263 if (rte_eth_bond_slave_add(BOND_PORT, slaves[i]) == -1)
264 rte_exit(-1, "Oooops! adding slave (%u) to bond (%u) failed!\n",
265 slaves[i], BOND_PORT);
270 rxq_conf = dev_info.default_rxconf;
271 rxq_conf.offloads = local_port_conf.rxmode.offloads;
272 retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd,
273 rte_eth_dev_socket_id(BOND_PORT),
274 &rxq_conf, mbuf_pool);
276 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
280 txq_conf = dev_info.default_txconf;
281 txq_conf.offloads = local_port_conf.txmode.offloads;
282 retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd,
283 rte_eth_dev_socket_id(BOND_PORT), &txq_conf);
286 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
289 retval = rte_eth_dev_start(BOND_PORT);
291 rte_exit(retval, "Start port %d failed (res=%d)", BOND_PORT, retval);
293 printf("Waiting for slaves to become active...");
294 while (wait_counter) {
295 uint16_t act_slaves[16] = {0};
296 if (rte_eth_bond_active_slaves_get(BOND_PORT, act_slaves, 16) ==
303 if (--wait_counter == 0)
304 rte_exit(-1, "\nFailed to activate slaves\n");
307 retval = rte_eth_promiscuous_enable(BOND_PORT);
309 rte_exit(EXIT_FAILURE,
310 "port %u: promiscuous mode enable failed: %s\n",
311 BOND_PORT, rte_strerror(-retval));
315 struct rte_ether_addr addr;
317 retval = rte_eth_macaddr_get(BOND_PORT, &addr);
319 rte_exit(retval, "port %u: Mac address get failed (res=%d)",
322 printf("Port %u MAC: ", (unsigned)BOND_PORT);
328 get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto)
330 size_t vlan_offset = 0;
332 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
333 struct rte_vlan_hdr *vlan_hdr =
334 (struct rte_vlan_hdr *)(eth_hdr + 1);
336 vlan_offset = sizeof(struct rte_vlan_hdr);
337 *proto = vlan_hdr->eth_proto;
339 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
340 vlan_hdr = vlan_hdr + 1;
342 *proto = vlan_hdr->eth_proto;
343 vlan_offset += sizeof(struct rte_vlan_hdr);
349 struct global_flag_stru_t {
350 int LcoreMainIsRunning;
352 uint32_t port_packets[4];
355 struct global_flag_stru_t global_flag_stru;
356 struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru;
359 * Main thread that does the work, reading from INPUT_PORT
360 * and writing to OUTPUT_PORT
362 static int lcore_main(__rte_unused void *arg1)
364 struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned;
365 struct rte_ether_addr d_addr;
367 struct rte_ether_addr bond_mac_addr;
368 struct rte_ether_hdr *eth_hdr;
369 struct rte_arp_hdr *arp_hdr;
370 struct rte_ipv4_hdr *ipv4_hdr;
371 uint16_t ether_type, offset;
379 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
380 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
382 rte_spinlock_trylock(&global_flag_stru_p->lock);
384 while (global_flag_stru_p->LcoreMainIsRunning) {
385 rte_spinlock_unlock(&global_flag_stru_p->lock);
386 rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST);
389 /* If didn't receive any packets, wait and go to next iteration */
395 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
397 printf("Bond (port %u) MAC address get failed: %s.\n"
398 "%u packets dropped", BOND_PORT, strerror(-ret),
400 rte_pktmbuf_free(pkts[i]);
404 /* Search incoming data for ARP packets and prepare response */
405 for (i = 0; i < rx_cnt; i++) {
406 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
407 global_flag_stru_p->port_packets[0]++;
408 rte_spinlock_unlock(&global_flag_stru_p->lock);
410 eth_hdr = rte_pktmbuf_mtod(pkts[i],
411 struct rte_ether_hdr *);
412 ether_type = eth_hdr->ether_type;
413 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
414 printf("VLAN taged frame, offset:");
415 offset = get_vlan_offset(eth_hdr, ðer_type);
417 printf("%d\n", offset);
418 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
419 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
420 global_flag_stru_p->port_packets[1]++;
421 rte_spinlock_unlock(&global_flag_stru_p->lock);
423 arp_hdr = (struct rte_arp_hdr *)(
424 (char *)(eth_hdr + 1) + offset);
425 if (arp_hdr->arp_data.arp_tip == bond_ip) {
426 if (arp_hdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) {
427 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REPLY);
428 /* Switch src and dst data and set bonding MAC */
429 rte_ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr);
430 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->s_addr);
431 rte_ether_addr_copy(&arp_hdr->arp_data.arp_sha,
432 &arp_hdr->arp_data.arp_tha);
433 arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip;
434 rte_ether_addr_copy(&bond_mac_addr, &d_addr);
435 rte_ether_addr_copy(&d_addr, &arp_hdr->arp_data.arp_sha);
436 arp_hdr->arp_data.arp_sip = bond_ip;
437 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
440 rte_eth_tx_burst(BOND_PORT, 0, NULL, 0);
443 } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
444 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
445 global_flag_stru_p->port_packets[2]++;
446 rte_spinlock_unlock(&global_flag_stru_p->lock);
448 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)(eth_hdr + 1) + offset);
449 if (ipv4_hdr->dst_addr == bond_ip) {
450 rte_ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr);
451 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->s_addr);
452 ipv4_hdr->dst_addr = ipv4_hdr->src_addr;
453 ipv4_hdr->src_addr = bond_ip;
454 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
459 /* Free processed packets */
461 rte_pktmbuf_free(pkts[i]);
463 rte_spinlock_trylock(&global_flag_stru_p->lock);
465 rte_spinlock_unlock(&global_flag_stru_p->lock);
466 printf("BYE lcore_main\n");
470 struct cmd_obj_send_result {
471 cmdline_fixed_string_t action;
474 static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size)
476 snprintf(buf, size, NIPQUAD_FMT,
477 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[0]),
478 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[1]),
479 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[2]),
480 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[3])
483 static void cmd_obj_send_parsed(void *parsed_result,
484 __rte_unused struct cmdline *cl,
485 __rte_unused void *data)
488 struct cmd_obj_send_result *res = parsed_result;
489 char ip_str[INET6_ADDRSTRLEN];
491 struct rte_ether_addr bond_mac_addr;
492 struct rte_mbuf *created_pkt;
493 struct rte_ether_hdr *eth_hdr;
494 struct rte_arp_hdr *arp_hdr;
500 if (res->ip.family == AF_INET)
501 get_string(res, ip_str, INET_ADDRSTRLEN);
503 cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n");
505 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
506 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
508 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
511 "Failed to get bond (port %u) MAC address: %s\n",
512 BOND_PORT, strerror(-ret));
515 created_pkt = rte_pktmbuf_alloc(mbuf_pool);
516 if (created_pkt == NULL) {
517 cmdline_printf(cl, "Failed to allocate mbuf\n");
521 pkt_size = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr);
522 created_pkt->data_len = pkt_size;
523 created_pkt->pkt_len = pkt_size;
525 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
526 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->s_addr);
527 memset(ð_hdr->d_addr, 0xFF, RTE_ETHER_ADDR_LEN);
528 eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP);
530 arp_hdr = (struct rte_arp_hdr *)(
531 (char *)eth_hdr + sizeof(struct rte_ether_hdr));
532 arp_hdr->arp_hardware = rte_cpu_to_be_16(RTE_ARP_HRD_ETHER);
533 arp_hdr->arp_protocol = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
534 arp_hdr->arp_hlen = RTE_ETHER_ADDR_LEN;
535 arp_hdr->arp_plen = sizeof(uint32_t);
536 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REQUEST);
538 rte_ether_addr_copy(&bond_mac_addr, &arp_hdr->arp_data.arp_sha);
539 arp_hdr->arp_data.arp_sip = bond_ip;
540 memset(&arp_hdr->arp_data.arp_tha, 0, RTE_ETHER_ADDR_LEN);
541 arp_hdr->arp_data.arp_tip =
542 ((unsigned char *)&res->ip.addr.ipv4)[0] |
543 (((unsigned char *)&res->ip.addr.ipv4)[1] << 8) |
544 (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) |
545 (((unsigned char *)&res->ip.addr.ipv4)[3] << 24);
546 rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1);
549 cmdline_printf(cl, "\n");
552 cmdline_parse_token_string_t cmd_obj_action_send =
553 TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send");
554 cmdline_parse_token_ipaddr_t cmd_obj_ip =
555 TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip);
557 cmdline_parse_inst_t cmd_obj_send = {
558 .f = cmd_obj_send_parsed, /* function to call */
559 .data = NULL, /* 2nd arg of func */
560 .help_str = "send client_ip",
561 .tokens = { /* token list, NULL terminated */
562 (void *)&cmd_obj_action_send,
568 struct cmd_start_result {
569 cmdline_fixed_string_t start;
572 static void cmd_start_parsed(__rte_unused void *parsed_result,
574 __rte_unused void *data)
576 int slave_core_id = rte_lcore_id();
578 rte_spinlock_trylock(&global_flag_stru_p->lock);
579 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
580 if (rte_eal_get_lcore_state(global_flag_stru_p->LcoreMainCore)
582 rte_spinlock_unlock(&global_flag_stru_p->lock);
585 rte_spinlock_unlock(&global_flag_stru_p->lock);
587 cmdline_printf(cl, "lcore_main already running on core:%d\n",
588 global_flag_stru_p->LcoreMainCore);
589 rte_spinlock_unlock(&global_flag_stru_p->lock);
593 /* start lcore main on core != master_core - ARP response thread */
594 slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
595 if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
598 rte_spinlock_trylock(&global_flag_stru_p->lock);
599 global_flag_stru_p->LcoreMainIsRunning = 1;
600 rte_spinlock_unlock(&global_flag_stru_p->lock);
602 "Starting lcore_main on core %d:%d "
603 "Our IP:%d.%d.%d.%d\n",
605 rte_eal_remote_launch(lcore_main, NULL, slave_core_id),
613 cmdline_parse_token_string_t cmd_start_start =
614 TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start");
616 cmdline_parse_inst_t cmd_start = {
617 .f = cmd_start_parsed, /* function to call */
618 .data = NULL, /* 2nd arg of func */
619 .help_str = "starts listening if not started at startup",
620 .tokens = { /* token list, NULL terminated */
621 (void *)&cmd_start_start,
626 struct cmd_help_result {
627 cmdline_fixed_string_t help;
630 static void cmd_help_parsed(__rte_unused void *parsed_result,
632 __rte_unused void *data)
635 "ALB - link bonding mode 6 example\n"
636 "send IP - sends one ARPrequest through bonding for IP.\n"
637 "start - starts listening ARPs.\n"
638 "stop - stops lcore_main.\n"
639 "show - shows some bond info: ex. active slaves etc.\n"
640 "help - prints help.\n"
641 "quit - terminate all threads and quit.\n"
645 cmdline_parse_token_string_t cmd_help_help =
646 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
648 cmdline_parse_inst_t cmd_help = {
649 .f = cmd_help_parsed, /* function to call */
650 .data = NULL, /* 2nd arg of func */
651 .help_str = "show help",
652 .tokens = { /* token list, NULL terminated */
653 (void *)&cmd_help_help,
658 struct cmd_stop_result {
659 cmdline_fixed_string_t stop;
662 static void cmd_stop_parsed(__rte_unused void *parsed_result,
664 __rte_unused void *data)
666 rte_spinlock_trylock(&global_flag_stru_p->lock);
667 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
669 "lcore_main not running on core:%d\n",
670 global_flag_stru_p->LcoreMainCore);
671 rte_spinlock_unlock(&global_flag_stru_p->lock);
674 global_flag_stru_p->LcoreMainIsRunning = 0;
675 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
677 "error: lcore_main can not stop on core:%d\n",
678 global_flag_stru_p->LcoreMainCore);
681 "lcore_main stopped on core:%d\n",
682 global_flag_stru_p->LcoreMainCore);
683 rte_spinlock_unlock(&global_flag_stru_p->lock);
686 cmdline_parse_token_string_t cmd_stop_stop =
687 TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop");
689 cmdline_parse_inst_t cmd_stop = {
690 .f = cmd_stop_parsed, /* function to call */
691 .data = NULL, /* 2nd arg of func */
692 .help_str = "this command do not handle any arguments",
693 .tokens = { /* token list, NULL terminated */
694 (void *)&cmd_stop_stop,
699 struct cmd_quit_result {
700 cmdline_fixed_string_t quit;
703 static void cmd_quit_parsed(__rte_unused void *parsed_result,
705 __rte_unused void *data)
707 rte_spinlock_trylock(&global_flag_stru_p->lock);
708 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
710 "lcore_main not running on core:%d\n",
711 global_flag_stru_p->LcoreMainCore);
712 rte_spinlock_unlock(&global_flag_stru_p->lock);
716 global_flag_stru_p->LcoreMainIsRunning = 0;
717 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
719 "error: lcore_main can not stop on core:%d\n",
720 global_flag_stru_p->LcoreMainCore);
723 "lcore_main stopped on core:%d\n",
724 global_flag_stru_p->LcoreMainCore);
725 rte_spinlock_unlock(&global_flag_stru_p->lock);
729 cmdline_parse_token_string_t cmd_quit_quit =
730 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
732 cmdline_parse_inst_t cmd_quit = {
733 .f = cmd_quit_parsed, /* function to call */
734 .data = NULL, /* 2nd arg of func */
735 .help_str = "this command do not handle any arguments",
736 .tokens = { /* token list, NULL terminated */
737 (void *)&cmd_quit_quit,
742 struct cmd_show_result {
743 cmdline_fixed_string_t show;
746 static void cmd_show_parsed(__rte_unused void *parsed_result,
748 __rte_unused void *data)
750 uint16_t slaves[16] = {0};
752 struct rte_ether_addr addr;
756 for (i = 0; i < slaves_count; i++) {
757 ret = rte_eth_macaddr_get(i, &addr);
760 "Failed to get port %u MAC address: %s\n",
769 rte_spinlock_trylock(&global_flag_stru_p->lock);
772 "packets received:Tot:%d Arp:%d IPv4:%d\n",
773 rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len),
774 global_flag_stru_p->port_packets[0],
775 global_flag_stru_p->port_packets[1],
776 global_flag_stru_p->port_packets[2]);
777 rte_spinlock_unlock(&global_flag_stru_p->lock);
780 cmdline_parse_token_string_t cmd_show_show =
781 TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show");
783 cmdline_parse_inst_t cmd_show = {
784 .f = cmd_show_parsed, /* function to call */
785 .data = NULL, /* 2nd arg of func */
786 .help_str = "this command do not handle any arguments",
787 .tokens = { /* token list, NULL terminated */
788 (void *)&cmd_show_show,
793 /****** CONTEXT (list of instruction) */
795 cmdline_parse_ctx_t main_ctx[] = {
796 (cmdline_parse_inst_t *)&cmd_start,
797 (cmdline_parse_inst_t *)&cmd_obj_send,
798 (cmdline_parse_inst_t *)&cmd_stop,
799 (cmdline_parse_inst_t *)&cmd_show,
800 (cmdline_parse_inst_t *)&cmd_quit,
801 (cmdline_parse_inst_t *)&cmd_help,
805 /* prompt function, called from main on MASTER lcore */
806 static void prompt(__rte_unused void *arg1)
810 cl = cmdline_stdin_new(main_ctx, "bond6>");
812 cmdline_interact(cl);
813 cmdline_stdin_exit(cl);
817 /* Main function, does initialisation and calls the per-lcore functions */
819 main(int argc, char *argv[])
821 int ret, slave_core_id;
822 uint16_t nb_ports, i;
825 ret = rte_eal_init(argc, argv);
826 rte_devargs_dump(stdout);
828 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
832 nb_ports = rte_eth_dev_count_avail();
834 rte_exit(EXIT_FAILURE, "Give at least one port\n");
835 else if (nb_ports > MAX_PORTS)
836 rte_exit(EXIT_FAILURE, "You can have max 4 ports\n");
838 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32,
839 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
840 if (mbuf_pool == NULL)
841 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
843 /* initialize all ports */
844 slaves_count = nb_ports;
845 RTE_ETH_FOREACH_DEV(i) {
846 slave_port_init(i, mbuf_pool);
850 bond_port_init(mbuf_pool);
852 rte_spinlock_init(&global_flag_stru_p->lock);
854 /* check state of lcores */
855 RTE_LCORE_FOREACH_SLAVE(slave_core_id) {
856 if (rte_eal_get_lcore_state(slave_core_id) != WAIT)
860 /* start lcore main on core != master_core - ARP response thread */
861 slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
862 if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
865 global_flag_stru_p->LcoreMainIsRunning = 1;
866 global_flag_stru_p->LcoreMainCore = slave_core_id;
867 printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n",
869 rte_eal_remote_launch((lcore_function_t *)lcore_main,
878 /* Start prompt for user interact */