summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Wiles <keith.wiles@intel.com>2017-09-09 17:59:45 -0500
committerKeith Wiles <keith.wiles@intel.com>2017-11-25 07:43:12 -0600
commitc3c6aa6c3966ecee912d72a2ca3806fb89b28ebb (patch)
tree4988736e834cce2fed9b950fd5f28e72722868b4
parent1c3e091801ff23992d0891ed958e0411f73b62b4 (diff)
downloaddpdk-draft-cli-c3c6aa6c3966ecee912d72a2ca3806fb89b28ebb.zip
dpdk-draft-cli-c3c6aa6c3966ecee912d72a2ca3806fb89b28ebb.tar.gz
dpdk-draft-cli-c3c6aa6c3966ecee912d72a2ca3806fb89b28ebb.tar.xz
examples: add cli example code
-rw-r--r--examples/Makefile1
-rw-r--r--examples/cli/Makefile50
-rw-r--r--examples/cli/main.c233
-rw-r--r--examples/cli/main.h49
4 files changed, 333 insertions, 0 deletions
diff --git a/examples/Makefile b/examples/Makefile
index 9f7974a..1038acb 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -39,6 +39,7 @@ include $(RTE_SDK)/mk/rte.vars.mk
DIRS-$(CONFIG_RTE_LIBRTE_PMD_BOND) += bond
DIRS-y += cmdline
+DIRS-$(CONFIG_RTE_LIBRTE_CLI) += cli
DIRS-$(CONFIG_RTE_LIBRTE_DISTRIBUTOR) += distributor
DIRS-y += ethtool
DIRS-y += exception_path
diff --git a/examples/cli/Makefile b/examples/cli/Makefile
new file mode 100644
index 0000000..2796bb6
--- /dev/null
+++ b/examples/cli/Makefile
@@ -0,0 +1,50 @@
+# BSD LICENSE
+#
+# Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of Intel Corporation nor the names of its
+# contributors may be used to endorse or promote products derived
+# from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ifeq ($(RTE_SDK),)
+$(error "Please define RTE_SDK environment variable")
+endif
+
+# Default target, can be overriden by command line or environment
+RTE_TARGET ?= x86_64-native-linuxapp-gcc
+
+include $(RTE_SDK)/mk/rte.vars.mk
+
+# binary name
+APP = cli
+
+# all source are stored in SRCS-y
+SRCS-y := main.c
+
+CFLAGS += -O3 -D_GNU_SOURCE
+CFLAGS += $(WERROR_FLAGS)
+
+include $(RTE_SDK)/mk/rte.extapp.mk
diff --git a/examples/cli/main.c b/examples/cli/main.c
new file mode 100644
index 0000000..84e8959
--- /dev/null
+++ b/examples/cli/main.c
@@ -0,0 +1,233 @@
+/*-
+ * BSD LICENSE
+ *
+ * Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* Created 2016 by Keith Wiles @ intel.com */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <libgen.h>
+#include <getopt.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+
+#include <rte_version.h>
+#include <rte_cycles.h>
+#include <rte_ether.h>
+#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>
+
+#include "main.h"
+
+static int
+hello_cmd(int argc, char **argv)
+{
+ int i, opt;
+
+ optind = 1;
+ while ((opt = getopt(argc, argv, "?")) != -1) {
+ switch (opt) {
+ case '?':
+ cli_usage();
+ return 0;
+ default:
+ break;
+ }
+ }
+
+ cli_printf("Hello command said: Hello World!! ");
+ for (i = 1; i < argc; i++)
+ cli_printf("%s ", argv[i]);
+ cli_printf("\n");
+
+ return 0;
+}
+
+static int
+pci_file(struct cli_node *node, char *buff, int len, uint32_t opt)
+{
+ if (is_file_open(opt)) {
+ FILE *f;
+
+ if (node->file_data && (node->fflags & CLI_FREE_DATA))
+ free(node->file_data);
+
+ node->file_data = malloc(32 * 1024);
+ if (!node->file_data)
+ return -1;
+ node->file_size = 32 * 1024;
+ node->fflags = CLI_DATA_RDONLY | CLI_FREE_DATA;
+
+ f = popen("lspci", "r");
+ if (!f)
+ return -1;
+
+ node->file_size = fread(node->file_data, 1, node->file_size, f);
+
+ pclose(f);
+ return 0;
+ }
+ return cli_file_handler(node, buff, len, opt);
+}
+
+static struct cli_map show_map[] = {
+ {10, "show %P"},
+ {20, "show %P mac %m"},
+ {30, "show %P vlan %d mac %m"},
+ {40, "show %P %|vlan|mac"},
+ {-1, NULL}};
+
+static const char *show_help[] = {
+ "show <portlist>",
+ "show <portlist> mac <ether_addr>",
+ "show <portlist> vlan <vlanid> mac <ether_addr>",
+ "show <portlist> [vlan|mac]",
+ CLI_HELP_PAUSE,
+ NULL};
+
+static int
+show_cmd(int argc, char **argv)
+{
+ struct cli_map *m;
+ portlist_t portlist;
+ struct ether_addr mac;
+
+ m = cli_mapping(show_map, argc, argv);
+ if (!m)
+ return -1;
+
+ switch (m->index) {
+ case 10:
+ rte_parse_portlist(argv[1], &portlist);
+ cli_printf(" Show Portlist: %16lx\n", portlist);
+ break;
+ case 20:
+ rte_parse_portlist(argv[1], &portlist);
+ rte_ether_aton(argv[3], &mac);
+ cli_printf(" Show Portlist: %016lx, MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
+ portlist,
+ mac.addr_bytes[0],
+ mac.addr_bytes[1],
+ mac.addr_bytes[2],
+ mac.addr_bytes[3],
+ mac.addr_bytes[4],
+ mac.addr_bytes[5]);
+ break;
+ case 30:
+ rte_parse_portlist(argv[1], &portlist);
+ rte_ether_aton(argv[5], &mac);
+ cli_printf(" Show Portlist: %016lx vlan %d MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
+ portlist,
+ atoi(argv[3]),
+ mac.addr_bytes[0],
+ mac.addr_bytes[1],
+ mac.addr_bytes[2],
+ mac.addr_bytes[3],
+ mac.addr_bytes[4],
+ mac.addr_bytes[5]);
+ break;
+ case 40:
+ rte_parse_portlist(argv[1], &portlist);
+ rte_ether_aton("1234:4567:8901", &mac);
+ cli_printf(" Show Portlist: %016lx %s: ",
+ portlist, argv[2]);
+ if (argv[2][0] == 'm')
+ cli_printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
+ mac.addr_bytes[0],
+ mac.addr_bytes[1],
+ mac.addr_bytes[2],
+ mac.addr_bytes[3],
+ mac.addr_bytes[4],
+ mac.addr_bytes[5]);
+ else
+ cli_printf("%d\n", 101);
+ break;
+ default:
+ cli_help_show_group("Show");
+ return -1;
+ }
+ return 0;
+}
+
+static struct cli_tree my_tree[] = {
+ c_dir("/data"),
+ c_file("pci", pci_file, "display lspci information"),
+ c_dir("/bin"),
+ c_cmd("show", show_cmd, "show mapping options"),
+ c_cmd("hello", hello_cmd, "Hello-World!!"),
+ c_alias("h", "history", "display history commands"),
+ c_alias("ll", "ls -l", "long directory listing alias"),
+ c_end()};
+
+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_help_add("Show", show_map, show_help);
+
+ return cli_add_bin_path("/bin");
+}
+
+int rte_eal_timer_init(void);
+
+int
+main(int argc __rte_unused, char *argv[] __rte_unused)
+{
+ rte_eal_timer_init();
+
+ /* Can use cli_create() or cli_create_with_defaults() */
+ if (cli_create_with_tree(init_tree) == 0) {
+
+ cli_start(NULL); /* Start collecting user input */
+
+ cli_destroy(); /* Destroy the CLI data */
+ }
+
+ return 0;
+}
diff --git a/examples/cli/main.h b/examples/cli/main.h
new file mode 100644
index 0000000..75fbc68
--- /dev/null
+++ b/examples/cli/main.h
@@ -0,0 +1,49 @@
+/*-
+ * BSD LICENSE
+ *
+ * Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* Created 2016 by Keith Wiles @ intel.com */
+
+#ifndef __MAIN_H
+#define __MAIN_H
+
+#ifdef __cplusplus
+#include <stdint.h>
+extern "C" {
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ifndef __MAIN_H */