summaryrefslogtreecommitdiff
path: root/examples/cli/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cli/main.c')
-rw-r--r--examples/cli/main.c233
1 files changed, 233 insertions, 0 deletions
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;
+}