New upstream version 17.11-rc3
[deb_dpdk.git] / doc / guides / sample_app_ug / cmd_line.rst
1 ..  BSD LICENSE
2     Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
3     All rights reserved.
4
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions
7     are met:
8
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
14     distribution.
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.
18
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.
30
31 Command Line Sample Application
32 ===============================
33
34 This chapter describes the Command Line sample application that
35 is part of the Data Plane Development Kit (DPDK).
36
37 Overview
38 --------
39
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.
44
45 .. note::
46
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.
51
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.
54
55 The application shows how the rte_cmdline application can be extended to handle a list of objects.
56 There are three simple commands:
57
58 *   add obj_name IP: Add a new object with an IP/IPv6 address associated to it.
59
60 *   del obj_name: Delete the specified object.
61
62 *   show obj_name: Show the IP associated with the specified object.
63
64 .. note::
65
66     To terminate the application, use **Ctrl-d**.
67
68 Compiling the Application
69 -------------------------
70
71 To compile the sample application see :doc:`compiling`
72
73 The application is located in the ``cmd_line`` sub-directory.
74
75 Running the Application
76 -----------------------
77
78 To run the application in linuxapp environment, issue the following command:
79
80 .. code-block:: console
81
82     $ ./build/cmdline -l 0-3 -n 4
83
84 Refer to the *DPDK Getting Started Guide* for general information on running applications
85 and the Environment Abstraction Layer (EAL) options.
86
87 Explanation
88 -----------
89
90 The following sections provide some explanation of the code.
91
92 EAL Initialization and cmdline Start
93 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94
95 The first task is the initialization of the Environment Abstraction Layer (EAL).
96 This is achieved as follows:
97
98 .. code-block:: c
99
100     int main(int argc, char **argv)
101     {
102         ret = rte_eal_init(argc, argv);
103         if (ret < 0)
104             rte_panic("Cannot init EAL\n");
105
106 Then, a new command line object is created and started to interact with the user through the console:
107
108 .. code-block:: c
109
110     cl = cmdline_stdin_new(main_ctx, "example> ");
111     cmdline_interact(cl);
112     cmdline_stdin_exit(cl);
113
114 The cmd line_interact() function returns when the user types **Ctrl-d** and in this case,
115 the application exits.
116
117 Defining a cmdline Context
118 ~~~~~~~~~~~~~~~~~~~~~~~~~~
119
120 A cmdline context is a list of commands that are listed in a NULL-terminated table, for example:
121
122 .. code-block:: c
123
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,
128          NULL,
129     };
130
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.
134
135 The rte_cmdline application provides a list of pre-defined token types:
136
137 *   String Token: Match a static string, a list of static strings or any string.
138
139 *   Number Token: Match a number that can be signed or unsigned, from 8-bit to 32-bit.
140
141 *   IP Address Token: Match an IPv4 or IPv6 address or network.
142
143 *   Ethernet* Address Token: Match a MAC address.
144
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.
147
148 For example, the cmd_obj_del_show command is defined as shown below:
149
150 .. code-block:: c
151
152     struct cmd_obj_add_result {
153         cmdline_fixed_string_t action;
154         cmdline_fixed_string_t name;
155         struct object *obj;
156     };
157
158     static void cmd_obj_del_show_parsed(void *parsed_result, struct cmdline *cl, attribute ((unused)) void *data)
159     {
160        /* ... */
161     }
162
163     cmdline_parse_token_string_t cmd_obj_action = TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result, action, "show#del");
164
165     parse_token_obj_list_t cmd_obj_obj = TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj, &global_obj_list);
166
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,
174              NULL,
175         },
176     };
177
178 This command is composed of two tokens:
179
180 *   The first token is a string token that can be show or del.
181
182 *   The second token is an object that was previously added using the add command in the global_obj_list variable.
183
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.