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 rte_eth_macaddr_get(portid, &addr);
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 rte_eth_promiscuous_enable(BOND_PORT);
304 struct rte_ether_addr addr;
306 rte_eth_macaddr_get(BOND_PORT, &addr);
307 printf("Port %u MAC: ", (unsigned)BOND_PORT);
313 get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto)
315 size_t vlan_offset = 0;
317 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
318 struct rte_vlan_hdr *vlan_hdr =
319 (struct rte_vlan_hdr *)(eth_hdr + 1);
321 vlan_offset = sizeof(struct rte_vlan_hdr);
322 *proto = vlan_hdr->eth_proto;
324 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
325 vlan_hdr = vlan_hdr + 1;
327 *proto = vlan_hdr->eth_proto;
328 vlan_offset += sizeof(struct rte_vlan_hdr);
334 struct global_flag_stru_t {
335 int LcoreMainIsRunning;
337 uint32_t port_packets[4];
340 struct global_flag_stru_t global_flag_stru;
341 struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru;
344 * Main thread that does the work, reading from INPUT_PORT
345 * and writing to OUTPUT_PORT
347 static int lcore_main(__attribute__((unused)) void *arg1)
349 struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned;
350 struct rte_ether_addr d_addr;
352 struct rte_ether_hdr *eth_hdr;
353 struct rte_arp_hdr *arp_hdr;
354 struct rte_ipv4_hdr *ipv4_hdr;
355 uint16_t ether_type, offset;
362 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
363 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
365 rte_spinlock_trylock(&global_flag_stru_p->lock);
367 while (global_flag_stru_p->LcoreMainIsRunning) {
368 rte_spinlock_unlock(&global_flag_stru_p->lock);
369 rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST);
372 /* If didn't receive any packets, wait and go to next iteration */
378 /* Search incoming data for ARP packets and prepare response */
379 for (i = 0; i < rx_cnt; i++) {
380 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
381 global_flag_stru_p->port_packets[0]++;
382 rte_spinlock_unlock(&global_flag_stru_p->lock);
384 eth_hdr = rte_pktmbuf_mtod(pkts[i],
385 struct rte_ether_hdr *);
386 ether_type = eth_hdr->ether_type;
387 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
388 printf("VLAN taged frame, offset:");
389 offset = get_vlan_offset(eth_hdr, ðer_type);
391 printf("%d\n", offset);
392 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
393 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
394 global_flag_stru_p->port_packets[1]++;
395 rte_spinlock_unlock(&global_flag_stru_p->lock);
397 arp_hdr = (struct rte_arp_hdr *)(
398 (char *)(eth_hdr + 1) + offset);
399 if (arp_hdr->arp_data.arp_tip == bond_ip) {
400 if (arp_hdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) {
401 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REPLY);
402 /* Switch src and dst data and set bonding MAC */
403 rte_ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr);
404 rte_eth_macaddr_get(BOND_PORT, ð_hdr->s_addr);
405 rte_ether_addr_copy(&arp_hdr->arp_data.arp_sha,
406 &arp_hdr->arp_data.arp_tha);
407 arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip;
408 rte_eth_macaddr_get(BOND_PORT, &d_addr);
409 rte_ether_addr_copy(&d_addr, &arp_hdr->arp_data.arp_sha);
410 arp_hdr->arp_data.arp_sip = bond_ip;
411 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
414 rte_eth_tx_burst(BOND_PORT, 0, NULL, 0);
417 } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
418 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
419 global_flag_stru_p->port_packets[2]++;
420 rte_spinlock_unlock(&global_flag_stru_p->lock);
422 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)(eth_hdr + 1) + offset);
423 if (ipv4_hdr->dst_addr == bond_ip) {
424 rte_ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr);
425 rte_eth_macaddr_get(BOND_PORT, ð_hdr->s_addr);
426 ipv4_hdr->dst_addr = ipv4_hdr->src_addr;
427 ipv4_hdr->src_addr = bond_ip;
428 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
433 /* Free processed packets */
435 rte_pktmbuf_free(pkts[i]);
437 rte_spinlock_trylock(&global_flag_stru_p->lock);
439 rte_spinlock_unlock(&global_flag_stru_p->lock);
440 printf("BYE lcore_main\n");
444 struct cmd_obj_send_result {
445 cmdline_fixed_string_t action;
448 static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size)
450 snprintf(buf, size, NIPQUAD_FMT,
451 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[0]),
452 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[1]),
453 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[2]),
454 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[3])
457 static void cmd_obj_send_parsed(void *parsed_result,
458 __attribute__((unused)) struct cmdline *cl,
459 __attribute__((unused)) void *data)
462 struct cmd_obj_send_result *res = parsed_result;
463 char ip_str[INET6_ADDRSTRLEN];
465 struct rte_mbuf *created_pkt;
466 struct rte_ether_hdr *eth_hdr;
467 struct rte_arp_hdr *arp_hdr;
472 if (res->ip.family == AF_INET)
473 get_string(res, ip_str, INET_ADDRSTRLEN);
475 cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n");
477 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
478 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
480 created_pkt = rte_pktmbuf_alloc(mbuf_pool);
481 if (created_pkt == NULL) {
482 cmdline_printf(cl, "Failed to allocate mbuf\n");
486 pkt_size = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr);
487 created_pkt->data_len = pkt_size;
488 created_pkt->pkt_len = pkt_size;
490 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
491 rte_eth_macaddr_get(BOND_PORT, ð_hdr->s_addr);
492 memset(ð_hdr->d_addr, 0xFF, RTE_ETHER_ADDR_LEN);
493 eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP);
495 arp_hdr = (struct rte_arp_hdr *)(
496 (char *)eth_hdr + sizeof(struct rte_ether_hdr));
497 arp_hdr->arp_hardware = rte_cpu_to_be_16(RTE_ARP_HRD_ETHER);
498 arp_hdr->arp_protocol = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
499 arp_hdr->arp_hlen = RTE_ETHER_ADDR_LEN;
500 arp_hdr->arp_plen = sizeof(uint32_t);
501 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REQUEST);
503 rte_eth_macaddr_get(BOND_PORT, &arp_hdr->arp_data.arp_sha);
504 arp_hdr->arp_data.arp_sip = bond_ip;
505 memset(&arp_hdr->arp_data.arp_tha, 0, RTE_ETHER_ADDR_LEN);
506 arp_hdr->arp_data.arp_tip =
507 ((unsigned char *)&res->ip.addr.ipv4)[0] |
508 (((unsigned char *)&res->ip.addr.ipv4)[1] << 8) |
509 (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) |
510 (((unsigned char *)&res->ip.addr.ipv4)[3] << 24);
511 rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1);
514 cmdline_printf(cl, "\n");
517 cmdline_parse_token_string_t cmd_obj_action_send =
518 TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send");
519 cmdline_parse_token_ipaddr_t cmd_obj_ip =
520 TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip);
522 cmdline_parse_inst_t cmd_obj_send = {
523 .f = cmd_obj_send_parsed, /* function to call */
524 .data = NULL, /* 2nd arg of func */
525 .help_str = "send client_ip",
526 .tokens = { /* token list, NULL terminated */
527 (void *)&cmd_obj_action_send,
533 struct cmd_start_result {
534 cmdline_fixed_string_t start;
537 static void cmd_start_parsed(__attribute__((unused)) void *parsed_result,
539 __attribute__((unused)) void *data)
541 int slave_core_id = rte_lcore_id();
543 rte_spinlock_trylock(&global_flag_stru_p->lock);
544 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
545 if (rte_eal_get_lcore_state(global_flag_stru_p->LcoreMainCore)
547 rte_spinlock_unlock(&global_flag_stru_p->lock);
550 rte_spinlock_unlock(&global_flag_stru_p->lock);
552 cmdline_printf(cl, "lcore_main already running on core:%d\n",
553 global_flag_stru_p->LcoreMainCore);
554 rte_spinlock_unlock(&global_flag_stru_p->lock);
558 /* start lcore main on core != master_core - ARP response thread */
559 slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
560 if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
563 rte_spinlock_trylock(&global_flag_stru_p->lock);
564 global_flag_stru_p->LcoreMainIsRunning = 1;
565 rte_spinlock_unlock(&global_flag_stru_p->lock);
567 "Starting lcore_main on core %d:%d "
568 "Our IP:%d.%d.%d.%d\n",
570 rte_eal_remote_launch(lcore_main, NULL, slave_core_id),
578 cmdline_parse_token_string_t cmd_start_start =
579 TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start");
581 cmdline_parse_inst_t cmd_start = {
582 .f = cmd_start_parsed, /* function to call */
583 .data = NULL, /* 2nd arg of func */
584 .help_str = "starts listening if not started at startup",
585 .tokens = { /* token list, NULL terminated */
586 (void *)&cmd_start_start,
591 struct cmd_help_result {
592 cmdline_fixed_string_t help;
595 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
597 __attribute__((unused)) void *data)
600 "ALB - link bonding mode 6 example\n"
601 "send IP - sends one ARPrequest through bonding for IP.\n"
602 "start - starts listening ARPs.\n"
603 "stop - stops lcore_main.\n"
604 "show - shows some bond info: ex. active slaves etc.\n"
605 "help - prints help.\n"
606 "quit - terminate all threads and quit.\n"
610 cmdline_parse_token_string_t cmd_help_help =
611 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
613 cmdline_parse_inst_t cmd_help = {
614 .f = cmd_help_parsed, /* function to call */
615 .data = NULL, /* 2nd arg of func */
616 .help_str = "show help",
617 .tokens = { /* token list, NULL terminated */
618 (void *)&cmd_help_help,
623 struct cmd_stop_result {
624 cmdline_fixed_string_t stop;
627 static void cmd_stop_parsed(__attribute__((unused)) void *parsed_result,
629 __attribute__((unused)) void *data)
631 rte_spinlock_trylock(&global_flag_stru_p->lock);
632 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
634 "lcore_main not running on core:%d\n",
635 global_flag_stru_p->LcoreMainCore);
636 rte_spinlock_unlock(&global_flag_stru_p->lock);
639 global_flag_stru_p->LcoreMainIsRunning = 0;
640 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
642 "error: lcore_main can not stop on core:%d\n",
643 global_flag_stru_p->LcoreMainCore);
646 "lcore_main stopped on core:%d\n",
647 global_flag_stru_p->LcoreMainCore);
648 rte_spinlock_unlock(&global_flag_stru_p->lock);
651 cmdline_parse_token_string_t cmd_stop_stop =
652 TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop");
654 cmdline_parse_inst_t cmd_stop = {
655 .f = cmd_stop_parsed, /* function to call */
656 .data = NULL, /* 2nd arg of func */
657 .help_str = "this command do not handle any arguments",
658 .tokens = { /* token list, NULL terminated */
659 (void *)&cmd_stop_stop,
664 struct cmd_quit_result {
665 cmdline_fixed_string_t quit;
668 static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
670 __attribute__((unused)) void *data)
672 rte_spinlock_trylock(&global_flag_stru_p->lock);
673 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
675 "lcore_main not running on core:%d\n",
676 global_flag_stru_p->LcoreMainCore);
677 rte_spinlock_unlock(&global_flag_stru_p->lock);
681 global_flag_stru_p->LcoreMainIsRunning = 0;
682 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
684 "error: lcore_main can not stop on core:%d\n",
685 global_flag_stru_p->LcoreMainCore);
688 "lcore_main stopped on core:%d\n",
689 global_flag_stru_p->LcoreMainCore);
690 rte_spinlock_unlock(&global_flag_stru_p->lock);
694 cmdline_parse_token_string_t cmd_quit_quit =
695 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
697 cmdline_parse_inst_t cmd_quit = {
698 .f = cmd_quit_parsed, /* function to call */
699 .data = NULL, /* 2nd arg of func */
700 .help_str = "this command do not handle any arguments",
701 .tokens = { /* token list, NULL terminated */
702 (void *)&cmd_quit_quit,
707 struct cmd_show_result {
708 cmdline_fixed_string_t show;
711 static void cmd_show_parsed(__attribute__((unused)) void *parsed_result,
713 __attribute__((unused)) void *data)
715 uint16_t slaves[16] = {0};
717 struct rte_ether_addr addr;
720 while (i < slaves_count) {
721 rte_eth_macaddr_get(i, &addr);
727 rte_spinlock_trylock(&global_flag_stru_p->lock);
730 "packets received:Tot:%d Arp:%d IPv4:%d\n",
731 rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len),
732 global_flag_stru_p->port_packets[0],
733 global_flag_stru_p->port_packets[1],
734 global_flag_stru_p->port_packets[2]);
735 rte_spinlock_unlock(&global_flag_stru_p->lock);
738 cmdline_parse_token_string_t cmd_show_show =
739 TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show");
741 cmdline_parse_inst_t cmd_show = {
742 .f = cmd_show_parsed, /* function to call */
743 .data = NULL, /* 2nd arg of func */
744 .help_str = "this command do not handle any arguments",
745 .tokens = { /* token list, NULL terminated */
746 (void *)&cmd_show_show,
751 /****** CONTEXT (list of instruction) */
753 cmdline_parse_ctx_t main_ctx[] = {
754 (cmdline_parse_inst_t *)&cmd_start,
755 (cmdline_parse_inst_t *)&cmd_obj_send,
756 (cmdline_parse_inst_t *)&cmd_stop,
757 (cmdline_parse_inst_t *)&cmd_show,
758 (cmdline_parse_inst_t *)&cmd_quit,
759 (cmdline_parse_inst_t *)&cmd_help,
763 /* prompt function, called from main on MASTER lcore */
764 static void prompt(__attribute__((unused)) void *arg1)
768 cl = cmdline_stdin_new(main_ctx, "bond6>");
770 cmdline_interact(cl);
771 cmdline_stdin_exit(cl);
775 /* Main function, does initialisation and calls the per-lcore functions */
777 main(int argc, char *argv[])
779 int ret, slave_core_id;
780 uint16_t nb_ports, i;
783 ret = rte_eal_init(argc, argv);
784 rte_devargs_dump(stdout);
786 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
790 nb_ports = rte_eth_dev_count_avail();
792 rte_exit(EXIT_FAILURE, "Give at least one port\n");
793 else if (nb_ports > MAX_PORTS)
794 rte_exit(EXIT_FAILURE, "You can have max 4 ports\n");
796 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32,
797 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
798 if (mbuf_pool == NULL)
799 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
801 /* initialize all ports */
802 slaves_count = nb_ports;
803 RTE_ETH_FOREACH_DEV(i) {
804 slave_port_init(i, mbuf_pool);
808 bond_port_init(mbuf_pool);
810 rte_spinlock_init(&global_flag_stru_p->lock);
812 /* check state of lcores */
813 RTE_LCORE_FOREACH_SLAVE(slave_core_id) {
814 if (rte_eal_get_lcore_state(slave_core_id) != WAIT)
818 /* start lcore main on core != master_core - ARP response thread */
819 slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
820 if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
823 global_flag_stru_p->LcoreMainIsRunning = 1;
824 global_flag_stru_p->LcoreMainCore = slave_core_id;
825 printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n",
827 rte_eal_remote_launch((lcore_function_t *)lcore_main,
836 /* Start prompt for user interact */