xref: /dpdk/doc/guides/sample_app_ug/cmd_line.rst (revision 3e0ceb9f17fff027fc6c8f18de35e11719ffa61e)
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
31Command Line Sample Application
32===============================
33
34This chapter describes the Command Line sample application that
35is part of the Data Plane Development Kit (DPDK).
36
37Overview
38--------
39
40The Command Line sample application is a simple application that
41demonstrates the use of the command line interface in the DPDK.
42This application is a readline-like interface that can be used
43to 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
52The Command Line sample application supports some of the features of the GNU readline library such as, completion,
53cut/paste and some other special bindings that make configuration and debug faster and easier.
54
55The application shows how the rte_cmdline application can be extended to handle a list of objects.
56There 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
68Compiling the Application
69-------------------------
70
71To compile the sample application see :doc:`compiling`
72
73The application is located in the ``cmd_line`` sub-directory.
74
75Running the Application
76-----------------------
77
78To 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
84Refer to the *DPDK Getting Started Guide* for general information on running applications
85and the Environment Abstraction Layer (EAL) options.
86
87Explanation
88-----------
89
90The following sections provide some explanation of the code.
91
92EAL Initialization and cmdline Start
93~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94
95The first task is the initialization of the Environment Abstraction Layer (EAL).
96This 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
106Then, 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
114The cmd line_interact() function returns when the user types **Ctrl-d** and in this case,
115the application exits.
116
117Defining a cmdline Context
118~~~~~~~~~~~~~~~~~~~~~~~~~~
119
120A 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
131Each command (of type cmdline_parse_inst_t) is defined statically.
132It contains a pointer to a callback function that is executed when the command is parsed,
133an opaque pointer, a help string and a list of tokens in a NULL-terminated table.
134
135The 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
145In this example, a new token type obj_list is defined and implemented
146in the parse_obj_list.c and parse_obj_list.h files.
147
148For 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
178This 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
184Once the command is parsed, the rte_cmdline application fills a cmd_obj_del_show_result structure.
185A pointer to this structure is given as an argument to the callback function and can be used in the body of this function.
186