1 .. SPDX-License-Identifier: BSD-3-Clause
2 Copyright(c) 2010-2014 Intel Corporation.
4 Command Line Sample Application
5 ===============================
7 This chapter describes the Command Line sample application that
8 is part of the Data Plane Development Kit (DPDK).
13 The Command Line sample application is a simple application that
14 demonstrates the use of the command line interface in the DPDK.
15 This application is a readline-like interface that can be used
16 to debug a DPDK application, in a Linux* application environment.
20 The rte_cmdline library should not be used in production code since
21 it is not validated to the same standard as other DPDK libraries.
22 See also the "rte_cmdline library should not be used in production code due to limited testing" item
23 in the "Known Issues" section of the Release Notes.
25 The Command Line sample application supports some of the features of the GNU readline library such as, completion,
26 cut/paste and some other special bindings that make configuration and debug faster and easier.
28 The application shows how the rte_cmdline application can be extended to handle a list of objects.
29 There are three simple commands:
31 * add obj_name IP: Add a new object with an IP/IPv6 address associated to it.
33 * del obj_name: Delete the specified object.
35 * show obj_name: Show the IP associated with the specified object.
39 To terminate the application, use **Ctrl-d**.
41 Compiling the Application
42 -------------------------
44 To compile the sample application see :doc:`compiling`
46 The application is located in the ``cmd_line`` sub-directory.
48 Running the Application
49 -----------------------
51 To run the application in linuxapp environment, issue the following command:
53 .. code-block:: console
55 $ ./build/cmdline -l 0-3 -n 4
57 Refer to the *DPDK Getting Started Guide* for general information on running applications
58 and the Environment Abstraction Layer (EAL) options.
63 The following sections provide some explanation of the code.
65 EAL Initialization and cmdline Start
66 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68 The first task is the initialization of the Environment Abstraction Layer (EAL).
69 This is achieved as follows:
73 int main(int argc, char **argv)
75 ret = rte_eal_init(argc, argv);
77 rte_panic("Cannot init EAL\n");
79 Then, a new command line object is created and started to interact with the user through the console:
83 cl = cmdline_stdin_new(main_ctx, "example> ");
85 cmdline_stdin_exit(cl);
87 The cmd line_interact() function returns when the user types **Ctrl-d** and in this case,
88 the application exits.
90 Defining a cmdline Context
91 ~~~~~~~~~~~~~~~~~~~~~~~~~~
93 A cmdline context is a list of commands that are listed in a NULL-terminated table, for example:
97 cmdline_parse_ctx_t main_ctx[] = {
98 (cmdline_parse_inst_t *) &cmd_obj_del_show,
99 (cmdline_parse_inst_t *) &cmd_obj_add,
100 (cmdline_parse_inst_t *) &cmd_help,
104 Each command (of type cmdline_parse_inst_t) is defined statically.
105 It contains a pointer to a callback function that is executed when the command is parsed,
106 an opaque pointer, a help string and a list of tokens in a NULL-terminated table.
108 The rte_cmdline application provides a list of pre-defined token types:
110 * String Token: Match a static string, a list of static strings or any string.
112 * Number Token: Match a number that can be signed or unsigned, from 8-bit to 32-bit.
114 * IP Address Token: Match an IPv4 or IPv6 address or network.
116 * Ethernet* Address Token: Match a MAC address.
118 In this example, a new token type obj_list is defined and implemented
119 in the parse_obj_list.c and parse_obj_list.h files.
121 For example, the cmd_obj_del_show command is defined as shown below:
125 struct cmd_obj_add_result {
126 cmdline_fixed_string_t action;
127 cmdline_fixed_string_t name;
131 static void cmd_obj_del_show_parsed(void *parsed_result, struct cmdline *cl, attribute ((unused)) void *data)
136 cmdline_parse_token_string_t cmd_obj_action = TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result, action, "show#del");
138 parse_token_obj_list_t cmd_obj_obj = TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj, &global_obj_list);
140 cmdline_parse_inst_t cmd_obj_del_show = {
141 .f = cmd_obj_del_show_parsed, /* function to call */
142 .data = NULL, /* 2nd arg of func */
143 .help_str = "Show/del an object",
144 .tokens = { /* token list, NULL terminated */
145 (void *)&cmd_obj_action,
146 (void *)&cmd_obj_obj,
151 This command is composed of two tokens:
153 * The first token is a string token that can be show or del.
155 * The second token is an object that was previously added using the add command in the global_obj_list variable.
157 Once the command is parsed, the rte_cmdline application fills a cmd_obj_del_show_result structure.
158 A pointer to this structure is given as an argument to the callback function and can be used in the body of this function.