summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Wiles <keith.wiles@intel.com>2017-09-11 18:34:36 -0500
committerKeith Wiles <keith.wiles@intel.com>2017-11-25 07:48:58 -0600
commit7823a3d9eb407643d8ae6862743988930b54a012 (patch)
tree9f1fb1982229aae2f6437ba170b1e44acedbc049
parenta85878759683cdbdc2718fc4fe4ae03d390f5ecb (diff)
downloaddpdk-draft-cli-7823a3d9eb407643d8ae6862743988930b54a012.zip
dpdk-draft-cli-7823a3d9eb407643d8ae6862743988930b54a012.tar.gz
dpdk-draft-cli-7823a3d9eb407643d8ae6862743988930b54a012.tar.xz
cli: add cli_quit() function
Signed-off-by: Keith Wiles <keith.wiles@intel.com>
-rw-r--r--examples/bond/main.c262
-rw-r--r--lib/librte_cli/cli.c2
-rw-r--r--lib/librte_cli/cli.h10
-rw-r--r--lib/librte_cli/cli_cmds.c2
4 files changed, 273 insertions, 3 deletions
diff --git a/examples/bond/main.c b/examples/bond/main.c
index 8e3b1f3..59f2bba 100644
--- a/examples/bond/main.c
+++ b/examples/bond/main.c
@@ -71,6 +71,7 @@
#include <rte_arp.h>
#include <rte_spinlock.h>
+#ifndef RTE_LIBRTE_CLI
#include <cmdline_rdline.h>
#include <cmdline_parse.h>
#include <cmdline_parse_num.h>
@@ -79,6 +80,15 @@
#include <cmdline_parse_etheraddr.h>
#include <cmdline_socket.h>
#include <cmdline.h>
+#else
+#include <rte_string_fns.h>
+
+#include <cli.h>
+#include <cli_file.h>
+#include <cli_map.h>
+#include <cli_help.h>
+#include <cli_string_fns.h>
+#endif
#include "main.h"
@@ -409,6 +419,7 @@ static int lcore_main(__attribute__((unused)) void *arg1)
return 0;
}
+#ifndef RTE_LIBRTE_CLI
struct cmd_obj_send_result {
cmdline_fixed_string_t action;
cmdline_ipaddr_t ip;
@@ -732,6 +743,255 @@ static void prompt(__attribute__((unused)) void *arg1)
cmdline_stdin_exit(cl);
}
}
+#else
+
+static struct cli_map my_map[] = {
+ { 0, "send %P" },
+ { 10, "start" },
+ { 20, "stop" },
+ { 30, "show" },
+ { 40, "help" },
+ { 50, "exit" },
+ { -1, NULL}
+};
+
+struct pg_ipaddr {
+ uint8_t family;
+ union {
+ struct in_addr ipv4;
+ struct in6_addr ipv6;
+ };
+ unsigned int prefixlen; /* in case of network only */
+};
+
+#define PG_IPADDR_V4 0x01
+#define PG_IPADDR_V6 0x02
+#define PG_IPADDR_NETWORK 0x04
+
+static int
+my_cmd(int argc, char **argv)
+{
+ struct cli_map *m;
+
+ m = cli_mapping(my_map, argc, argv);
+ if (!m)
+ return -1;
+
+ switch(m->index) {
+ case 0: {
+ struct pg_ipaddr ip;
+
+ struct rte_mbuf *created_pkt;
+ struct ether_hdr *eth_hdr;
+ struct arp_hdr *arp_hdr;
+
+ uint32_t bond_ip;
+ size_t pkt_size;
+
+ rte_atoip(argv[1], PG_IPADDR_V4, &ip, sizeof(ip));
+ if (ip.family != AF_INET)
+ cli_printf("Wrong IP format. Only IPv4 is supported\n");
+
+ bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
+ (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
+
+ created_pkt = rte_pktmbuf_alloc(mbuf_pool);
+ pkt_size = sizeof(struct ether_hdr) + sizeof(struct arp_hdr);
+ created_pkt->data_len = pkt_size;
+ created_pkt->pkt_len = pkt_size;
+
+ eth_hdr = rte_pktmbuf_mtod(created_pkt, struct ether_hdr *);
+ rte_eth_macaddr_get(BOND_PORT, &eth_hdr->s_addr);
+ memset(&eth_hdr->d_addr, 0xFF, ETHER_ADDR_LEN);
+ eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_ARP);
+
+ arp_hdr = (struct arp_hdr *)((char *)eth_hdr + sizeof(struct ether_hdr));
+ arp_hdr->arp_hrd = rte_cpu_to_be_16(ARP_HRD_ETHER);
+ arp_hdr->arp_pro = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
+ arp_hdr->arp_hln = ETHER_ADDR_LEN;
+ arp_hdr->arp_pln = sizeof(uint32_t);
+ arp_hdr->arp_op = rte_cpu_to_be_16(ARP_OP_REQUEST);
+
+ rte_eth_macaddr_get(BOND_PORT, &arp_hdr->arp_data.arp_sha);
+ arp_hdr->arp_data.arp_sip = bond_ip;
+ memset(&arp_hdr->arp_data.arp_tha, 0, ETHER_ADDR_LEN);
+ arp_hdr->arp_data.arp_tip =
+ ((unsigned char *)&ip.ipv4)[0] |
+ (((unsigned char *)&ip.ipv4)[1] << 8) |
+ (((unsigned char *)&ip.ipv4)[2] << 16) |
+ (((unsigned char *)&ip.ipv4)[3] << 24);
+ rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1);
+
+ rte_delay_ms(100);
+ cli_printf("\n");
+ }
+ break;
+ case 10: {
+ int slave_core_id = rte_lcore_id();
+
+ rte_spinlock_trylock(&global_flag_stru_p->lock);
+ if (global_flag_stru_p->LcoreMainIsRunning == 0) {
+ if (lcore_config[global_flag_stru_p->LcoreMainCore].state != WAIT) {
+ rte_spinlock_unlock(&global_flag_stru_p->lock);
+ return 0;
+ }
+ rte_spinlock_unlock(&global_flag_stru_p->lock);
+ } else {
+ cli_printf("lcore_main already running on core:%d\n",
+ global_flag_stru_p->LcoreMainCore);
+ rte_spinlock_unlock(&global_flag_stru_p->lock);
+ return 0;
+ }
+
+ /* start lcore main on core != master_core - ARP response thread */
+ slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
+ if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
+ return 0;
+
+ rte_spinlock_trylock(&global_flag_stru_p->lock);
+ global_flag_stru_p->LcoreMainIsRunning = 1;
+ rte_spinlock_unlock(&global_flag_stru_p->lock);
+ cli_printf("Starting lcore_main on core %d:%d "
+ "Our IP:%d.%d.%d.%d\n",
+ slave_core_id,
+ rte_eal_remote_launch(lcore_main, NULL, slave_core_id),
+ BOND_IP_1,
+ BOND_IP_2,
+ BOND_IP_3,
+ BOND_IP_4);
+ }
+ break;
+ case 20:
+ rte_spinlock_trylock(&global_flag_stru_p->lock);
+ if (global_flag_stru_p->LcoreMainIsRunning == 0) {
+ cli_printf("lcore_main not running on core:%d\n",
+ global_flag_stru_p->LcoreMainCore);
+ rte_spinlock_unlock(&global_flag_stru_p->lock);
+ return 0;
+ }
+ global_flag_stru_p->LcoreMainIsRunning = 0;
+ if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
+ cli_printf("error: lcore_main can not stop on core:%d\n",
+ global_flag_stru_p->LcoreMainCore);
+ else
+ cli_printf("lcore_main stopped on core:%d\n",
+ global_flag_stru_p->LcoreMainCore);
+ rte_spinlock_unlock(&global_flag_stru_p->lock);
+ break;
+ case 30: {
+ uint8_t slaves[16] = {0};
+ uint8_t len = 16;
+ struct ether_addr addr;
+ uint8_t i = 0;
+
+ while (i < slaves_count) {
+ rte_eth_macaddr_get(i, &addr);
+ PRINT_MAC(addr);
+ printf("\n");
+ i++;
+ }
+
+ rte_spinlock_trylock(&global_flag_stru_p->lock);
+ cli_printf("Active_slaves:%d "
+ "packets received:Tot:%d Arp:%d IPv4:%d\n",
+ rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len),
+ global_flag_stru_p->port_packets[0],
+ global_flag_stru_p->port_packets[1],
+ global_flag_stru_p->port_packets[2]);
+ rte_spinlock_unlock(&global_flag_stru_p->lock);
+ }
+ break;
+ case 40:
+ cli_printf(
+ "ALB - link bonding mode 6 example\n"
+ "send I - sends one ARPrequest through bonding for IP.\n"
+ "start - starts listening ARPs.\n"
+ "stop - stops lcore_main.\n"
+ "show - shows some bond info: ex. active slaves etc.\n"
+ "help - prints help.\n"
+ "exit - terminate all threads and quit.\n");
+ break;
+ case 50: {
+ rte_spinlock_trylock(&global_flag_stru_p->lock);
+ if (global_flag_stru_p->LcoreMainIsRunning == 0) {
+ cli_printf("lcore_main not running on core:%d\n",
+ global_flag_stru_p->LcoreMainCore);
+ rte_spinlock_unlock(&global_flag_stru_p->lock);
+ cli_quit();
+ return 0;
+ }
+ global_flag_stru_p->LcoreMainIsRunning = 0;
+ if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
+ cli_printf("error: lcore_main can not stop on core:%d\n",
+ global_flag_stru_p->LcoreMainCore);
+ else
+ cli_printf("lcore_main stopped on core:%d\n",
+ global_flag_stru_p->LcoreMainCore);
+ rte_spinlock_unlock(&global_flag_stru_p->lock);
+ cli_quit();
+ }
+ break;
+ }
+ return 0;
+}
+
+static struct cli_tree my_tree[] = {
+ c_dir("/bin"),
+ c_cmd("send", my_cmd, "Send one ARP request"),
+ c_cmd("start", my_cmd, "starts listening ARPs"),
+ c_cmd("stop", my_cmd, "stops lcore_main"),
+ c_cmd("show", my_cmd, "show some bond information"),
+ c_cmd("help", my_cmd, "print help"),
+ c_cmd("exit", my_cmd, "quit program"),
+ c_end()
+};
+
+static void
+my_prompt(int cont __rte_unused)
+{
+ char *str = cli_cwd_path();
+
+ if (strlen(str) > 1) /* trim the trailing '/' from string */
+ str[strlen(str) - 1] = '\0';
+
+ vt100_color(SCRN_GREEN, SCRN_NO_CHANGE, SCRN_BOLD);
+ cli_printf("%s:", (cont) ? " >> " : "bond6");
+ vt100_color(SCRN_BLUE, SCRN_NO_CHANGE, SCRN_BOLD);
+ cli_printf("%s", str);
+ vt100_color(SCRN_DEFAULT_FG, SCRN_DEFAULT_BG, SCRN_OFF);
+ cli_printf("> ");
+}
+
+static int
+init_tree(void)
+{
+ /*
+ * Root is created already and using system default cmds and dirs, the
+ * developer is not required to use the system default cmds/dirs.
+ */
+ if (cli_default_tree_init())
+ return -1;
+
+ /* Using NULL here to start at root directory */
+ if (cli_add_tree(NULL, my_tree))
+ return -1;
+
+ cli_set_prompt(my_prompt);
+
+ return cli_add_bin_path("/bin");
+}
+
+/* prompt function, called from main on MASTER lcore */
+static void
+prompt(__attribute__((unused)) void *arg1)
+{
+ if (cli_create_with_tree(init_tree) == 0) {
+ cli_start(NULL);
+
+ cli_destroy();
+ }
+}
+#endif
/* Main function, does initialisation and calls the per-lcore functions */
int
@@ -752,7 +1012,7 @@ main(int argc, char *argv[])
if (nb_ports == 0)
rte_exit(EXIT_FAILURE, "Give at least one port\n");
else if (nb_ports > MAX_PORTS)
- rte_exit(EXIT_FAILURE, "You can have max 4 ports\n");
+ rte_exit(EXIT_FAILURE, "You can have max 4 ports %d\n", nb_ports);
mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32,
0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
diff --git a/lib/librte_cli/cli.c b/lib/librte_cli/cli.c
index b069f9a..6dfdcf1 100644
--- a/lib/librte_cli/cli.c
+++ b/lib/librte_cli/cli.c
@@ -671,7 +671,7 @@ cli_poll(char *c)
if (read(fds.fd, c, 1) > 0)
return 1;
} else
- this_cli->quit_flag = 1;
+ cli_quit();
}
return 0;
}
diff --git a/lib/librte_cli/cli.h b/lib/librte_cli/cli.h
index 0e9cefb..bf5aba1 100644
--- a/lib/librte_cli/cli.h
+++ b/lib/librte_cli/cli.h
@@ -1160,6 +1160,16 @@ int cli_nodes_unlimited(void);
*/
int cli_yield_io(void);
+/**
+ * shutdown the CLI command interface.
+ *
+ */
+static inline void
+cli_quit(void)
+{
+ this_cli->quit_flag = 1;
+}
+
#ifdef __cplusplus
}
#endif
diff --git a/lib/librte_cli/cli_cmds.c b/lib/librte_cli/cli_cmds.c
index 130de56..9e8ca55 100644
--- a/lib/librte_cli/cli_cmds.c
+++ b/lib/librte_cli/cli_cmds.c
@@ -279,7 +279,7 @@ scrn_cmd(int argc __rte_unused, char **argv __rte_unused)
static int
quit_cmd(int argc __rte_unused, char **argv __rte_unused)
{
- this_cli->quit_flag = 1;
+ cli_quit();
return 0;
}