xref: /dpdk/doc/guides/sample_app_ug/cmd_line.rst (revision 8750576fb2a9a067ffbcce4bab6481f3bfa47097)
15630257fSFerruh Yigit..  SPDX-License-Identifier: BSD-3-Clause
25630257fSFerruh Yigit    Copyright(c) 2010-2014 Intel Corporation.
3d0dff9baSBernard Iremonger
4d0dff9baSBernard IremongerCommand Line Sample Application
5d0dff9baSBernard Iremonger===============================
6d0dff9baSBernard Iremonger
7d0dff9baSBernard IremongerThis chapter describes the Command Line sample application that
8e0c7c473SSiobhan Butleris part of the Data Plane Development Kit (DPDK).
9d0dff9baSBernard Iremonger
10d0dff9baSBernard IremongerOverview
11d0dff9baSBernard Iremonger--------
12d0dff9baSBernard Iremonger
13d0dff9baSBernard IremongerThe Command Line sample application is a simple application that
14e0c7c473SSiobhan Butlerdemonstrates the use of the command line interface in the DPDK.
15d0dff9baSBernard IremongerThis application is a readline-like interface that can be used
16*8750576fSNandini Persadto debug a DPDK application in a Linux* application environment.
17d0dff9baSBernard Iremonger
18d0dff9baSBernard Iremonger.. note::
19d0dff9baSBernard Iremonger
20d0dff9baSBernard Iremonger    The rte_cmdline library should not be used in production code since
214b152471SThomas Monjalon    it is not validated to the same standard as other DPDK libraries.
22d0dff9baSBernard Iremonger    See also the "rte_cmdline library should not be used in production code due to limited testing" item
23d0dff9baSBernard Iremonger    in the "Known Issues" section of the Release Notes.
24d0dff9baSBernard Iremonger
25*8750576fSNandini PersadThe Command Line sample application supports some of the features of the GNU readline library
26*8750576fSNandini Persadsuch as completion, cut/paste and other special bindings
27*8750576fSNandini Persadthat make configuration and debug faster and easier.
28d0dff9baSBernard Iremonger
29*8750576fSNandini PersadThe application shows how the ``cmdline`` library can be extended
30*8750576fSNandini Persadto handle a list of objects.
31*8750576fSNandini Persad
32d0dff9baSBernard IremongerThere are three simple commands:
33d0dff9baSBernard Iremonger
34d0dff9baSBernard Iremonger*   add obj_name IP: Add a new object with an IP/IPv6 address associated to it.
35d0dff9baSBernard Iremonger
36d0dff9baSBernard Iremonger*   del obj_name: Delete the specified object.
37d0dff9baSBernard Iremonger
38d0dff9baSBernard Iremonger*   show obj_name: Show the IP associated with the specified object.
39d0dff9baSBernard Iremonger
40d0dff9baSBernard Iremonger.. note::
41d0dff9baSBernard Iremonger
42d0dff9baSBernard Iremonger    To terminate the application, use **Ctrl-d**.
43d0dff9baSBernard Iremonger
44d0dff9baSBernard IremongerCompiling the Application
45d0dff9baSBernard Iremonger-------------------------
46d0dff9baSBernard Iremonger
477cacb056SHerakliusz LipiecTo compile the sample application see :doc:`compiling`
48d0dff9baSBernard Iremonger
497cacb056SHerakliusz LipiecThe application is located in the ``cmd_line`` sub-directory.
50d0dff9baSBernard Iremonger
51d0dff9baSBernard IremongerRunning the Application
52d0dff9baSBernard Iremonger-----------------------
53d0dff9baSBernard Iremonger
54*8750576fSNandini PersadTo run the application in a Linux environment, issue the following command:
55d0dff9baSBernard Iremonger
56d0dff9baSBernard Iremonger.. code-block:: console
57d0dff9baSBernard Iremonger
58e2a94f9aSCiara Power    $ ./<build_dir>/examples/dpdk-cmdline -l 0-3 -n 4
59d0dff9baSBernard Iremonger
60e0c7c473SSiobhan ButlerRefer to the *DPDK Getting Started Guide* for general information on running applications
61d0dff9baSBernard Iremongerand the Environment Abstraction Layer (EAL) options.
62d0dff9baSBernard Iremonger
63d0dff9baSBernard IremongerExplanation
64d0dff9baSBernard Iremonger-----------
65d0dff9baSBernard Iremonger
66*8750576fSNandini PersadThe following sections provide explanation of the code.
67d0dff9baSBernard Iremonger
68d0dff9baSBernard IremongerEAL Initialization and cmdline Start
69d0dff9baSBernard Iremonger~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70d0dff9baSBernard Iremonger
71d0dff9baSBernard IremongerThe first task is the initialization of the Environment Abstraction Layer (EAL).
72d0dff9baSBernard IremongerThis is achieved as follows:
73d0dff9baSBernard Iremonger
749a212dc0SConor Fogarty.. literalinclude:: ../../../examples/cmdline/main.c
759a212dc0SConor Fogarty    :language: c
769a212dc0SConor Fogarty    :start-after: Initialization of the Environment Abstraction Layer (EAL). 8<
779a212dc0SConor Fogarty    :end-before: >8 End of initialization of Environment Abstraction Layer (EAL).
78d0dff9baSBernard Iremonger
79*8750576fSNandini PersadThen, a new command line object is created and starts to interact with the user through the console:
80d0dff9baSBernard Iremonger
819a212dc0SConor Fogarty.. literalinclude:: ../../../examples/cmdline/main.c
829a212dc0SConor Fogarty    :language: c
839a212dc0SConor Fogarty    :start-after: Creating a new command line object. 8<
849a212dc0SConor Fogarty    :end-before: >8 End of creating a new command line object.
859a212dc0SConor Fogarty    :dedent: 1
86d0dff9baSBernard Iremonger
87*8750576fSNandini PersadThe ``cmdline_interact()`` function returns when the user types **Ctrl-d** and,
88*8750576fSNandini Persadin this case, the application exits.
89d0dff9baSBernard Iremonger
90d0dff9baSBernard IremongerDefining a cmdline Context
91d0dff9baSBernard Iremonger~~~~~~~~~~~~~~~~~~~~~~~~~~
92d0dff9baSBernard Iremonger
93*8750576fSNandini PersadA cmdline context is a list of commands that are listed in a NULL-terminated table.
94*8750576fSNandini PersadFor example:
95d0dff9baSBernard Iremonger
969a212dc0SConor Fogarty.. literalinclude:: ../../../examples/cmdline/commands.c
979a212dc0SConor Fogarty    :language: c
989a212dc0SConor Fogarty    :start-after: Cmdline context list of commands in NULL-terminated table. 8<
999a212dc0SConor Fogarty    :end-before: >8 End of context list.
100d0dff9baSBernard Iremonger
101d0dff9baSBernard IremongerEach command (of type cmdline_parse_inst_t) is defined statically.
102d0dff9baSBernard IremongerIt contains a pointer to a callback function that is executed when the command is parsed,
103d0dff9baSBernard Iremongeran opaque pointer, a help string and a list of tokens in a NULL-terminated table.
104d0dff9baSBernard Iremonger
105d0dff9baSBernard IremongerThe rte_cmdline application provides a list of pre-defined token types:
106d0dff9baSBernard Iremonger
107d0dff9baSBernard Iremonger*   String Token: Match a static string, a list of static strings or any string.
108d0dff9baSBernard Iremonger
109d0dff9baSBernard Iremonger*   Number Token: Match a number that can be signed or unsigned, from 8-bit to 32-bit.
110d0dff9baSBernard Iremonger
111d0dff9baSBernard Iremonger*   IP Address Token: Match an IPv4 or IPv6 address or network.
112d0dff9baSBernard Iremonger
113d0dff9baSBernard Iremonger*   Ethernet* Address Token: Match a MAC address.
114d0dff9baSBernard Iremonger
115d0dff9baSBernard IremongerIn this example, a new token type obj_list is defined and implemented
116d0dff9baSBernard Iremongerin the parse_obj_list.c and parse_obj_list.h files.
117d0dff9baSBernard Iremonger
118d0dff9baSBernard IremongerFor example, the cmd_obj_del_show command is defined as shown below:
119d0dff9baSBernard Iremonger
1209a212dc0SConor Fogarty.. literalinclude:: ../../../examples/cmdline/commands.c
1219a212dc0SConor Fogarty    :language: c
1229a212dc0SConor Fogarty    :start-after: Show or delete tokens. 8<
1239a212dc0SConor Fogarty    :end-before: >8 End of show or delete tokens.
124d0dff9baSBernard Iremonger
125d0dff9baSBernard IremongerThis command is composed of two tokens:
126d0dff9baSBernard Iremonger
127d0dff9baSBernard Iremonger*   The first token is a string token that can be show or del.
128d0dff9baSBernard Iremonger
129d0dff9baSBernard Iremonger*   The second token is an object that was previously added using the add command in the global_obj_list variable.
130d0dff9baSBernard Iremonger
131d0dff9baSBernard IremongerOnce the command is parsed, the rte_cmdline application fills a cmd_obj_del_show_result structure.
132d0dff9baSBernard IremongerA pointer to this structure is given as an argument to the callback function and can be used in the body of this function.
133