2 Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
15 * Neither the name of Intel Corporation nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 Command Line Sample Application
32 ===============================
34 This chapter describes the Command Line sample application that
35 is part of the Data Plane Development Kit (DPDK).
40 The Command Line sample application is a simple application that
41 demonstrates the use of the command line interface in the DPDK.
42 This application is a readline-like interface that can be used
43 to debug a DPDK application, in a Linux* application environment.
47 The rte_cmdline library should not be used in production code since
48 it is not validated to the same standard as other DPDK libraries.
49 See also the "rte_cmdline library should not be used in production code due to limited testing" item
50 in the "Known Issues" section of the Release Notes.
52 The Command Line sample application supports some of the features of the GNU readline library such as, completion,
53 cut/paste and some other special bindings that make configuration and debug faster and easier.
55 The application shows how the rte_cmdline application can be extended to handle a list of objects.
56 There are three simple commands:
58 * add obj_name IP: Add a new object with an IP/IPv6 address associated to it.
60 * del obj_name: Delete the specified object.
62 * show obj_name: Show the IP associated with the specified object.
66 To terminate the application, use **Ctrl-d**.
68 Compiling the Application
69 -------------------------
71 To compile the sample application see :doc:`compiling`
73 The application is located in the ``cmd_line`` sub-directory.
75 Running the Application
76 -----------------------
78 To run the application in linuxapp environment, issue the following command:
80 .. code-block:: console
82 $ ./build/cmdline -l 0-3 -n 4
84 Refer to the *DPDK Getting Started Guide* for general information on running applications
85 and the Environment Abstraction Layer (EAL) options.
90 The following sections provide some explanation of the code.
92 EAL Initialization and cmdline Start
93 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95 The first task is the initialization of the Environment Abstraction Layer (EAL).
96 This is achieved as follows:
100 int main(int argc, char **argv)
102 ret = rte_eal_init(argc, argv);
104 rte_panic("Cannot init EAL\n");
106 Then, a new command line object is created and started to interact with the user through the console:
110 cl = cmdline_stdin_new(main_ctx, "example> ");
111 cmdline_interact(cl);
112 cmdline_stdin_exit(cl);
114 The cmd line_interact() function returns when the user types **Ctrl-d** and in this case,
115 the application exits.
117 Defining a cmdline Context
118 ~~~~~~~~~~~~~~~~~~~~~~~~~~
120 A cmdline context is a list of commands that are listed in a NULL-terminated table, for example:
124 cmdline_parse_ctx_t main_ctx[] = {
125 (cmdline_parse_inst_t *) &cmd_obj_del_show,
126 (cmdline_parse_inst_t *) &cmd_obj_add,
127 (cmdline_parse_inst_t *) &cmd_help,
131 Each command (of type cmdline_parse_inst_t) is defined statically.
132 It contains a pointer to a callback function that is executed when the command is parsed,
133 an opaque pointer, a help string and a list of tokens in a NULL-terminated table.
135 The rte_cmdline application provides a list of pre-defined token types:
137 * String Token: Match a static string, a list of static strings or any string.
139 * Number Token: Match a number that can be signed or unsigned, from 8-bit to 32-bit.
141 * IP Address Token: Match an IPv4 or IPv6 address or network.
143 * Ethernet* Address Token: Match a MAC address.
145 In this example, a new token type obj_list is defined and implemented
146 in the parse_obj_list.c and parse_obj_list.h files.
148 For example, the cmd_obj_del_show command is defined as shown below:
152 struct cmd_obj_add_result {
153 cmdline_fixed_string_t action;
154 cmdline_fixed_string_t name;
158 static void cmd_obj_del_show_parsed(void *parsed_result, struct cmdline *cl, attribute ((unused)) void *data)
163 cmdline_parse_token_string_t cmd_obj_action = TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result, action, "show#del");
165 parse_token_obj_list_t cmd_obj_obj = TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj, &global_obj_list);
167 cmdline_parse_inst_t cmd_obj_del_show = {
168 .f = cmd_obj_del_show_parsed, /* function to call */
169 .data = NULL, /* 2nd arg of func */
170 .help_str = "Show/del an object",
171 .tokens = { /* token list, NULL terminated */
172 (void *)&cmd_obj_action,
173 (void *)&cmd_obj_obj,
178 This command is composed of two tokens:
180 * The first token is a string token that can be show or del.
182 * The second token is an object that was previously added using the add command in the global_obj_list variable.
184 Once the command is parsed, the rte_cmdline application fills a cmd_obj_del_show_result structure.
185 A pointer to this structure is given as an argument to the callback function and can be used in the body of this function.