doc: use code snippets in sample app guides
[dpdk.git] / doc / guides / sample_app_ug / cmd_line.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright(c) 2010-2014 Intel Corporation.
3
4 Command Line Sample Application
5 ===============================
6
7 This chapter describes the Command Line sample application that
8 is part of the Data Plane Development Kit (DPDK).
9
10 Overview
11 --------
12
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.
17
18 .. note::
19
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.
24
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.
27
28 The application shows how the rte_cmdline application can be extended to handle a list of objects.
29 There are three simple commands:
30
31 *   add obj_name IP: Add a new object with an IP/IPv6 address associated to it.
32
33 *   del obj_name: Delete the specified object.
34
35 *   show obj_name: Show the IP associated with the specified object.
36
37 .. note::
38
39     To terminate the application, use **Ctrl-d**.
40
41 Compiling the Application
42 -------------------------
43
44 To compile the sample application see :doc:`compiling`
45
46 The application is located in the ``cmd_line`` sub-directory.
47
48 Running the Application
49 -----------------------
50
51 To run the application in linux environment, issue the following command:
52
53 .. code-block:: console
54
55     $ ./<build_dir>/examples/dpdk-cmdline -l 0-3 -n 4
56
57 Refer to the *DPDK Getting Started Guide* for general information on running applications
58 and the Environment Abstraction Layer (EAL) options.
59
60 Explanation
61 -----------
62
63 The following sections provide some explanation of the code.
64
65 EAL Initialization and cmdline Start
66 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67
68 The first task is the initialization of the Environment Abstraction Layer (EAL).
69 This is achieved as follows:
70
71 .. literalinclude:: ../../../examples/cmdline/main.c
72     :language: c
73     :start-after: Initialization of the Environment Abstraction Layer (EAL). 8<
74     :end-before: >8 End of initialization of Environment Abstraction Layer (EAL).
75
76 Then, a new command line object is created and started to interact with the user through the console:
77
78 .. literalinclude:: ../../../examples/cmdline/main.c
79     :language: c
80     :start-after: Creating a new command line object. 8<
81     :end-before: >8 End of creating a new command line object.
82     :dedent: 1
83
84 The cmd line_interact() function returns when the user types **Ctrl-d** and in this case,
85 the application exits.
86
87 Defining a cmdline Context
88 ~~~~~~~~~~~~~~~~~~~~~~~~~~
89
90 A cmdline context is a list of commands that are listed in a NULL-terminated table, for example:
91
92 .. literalinclude:: ../../../examples/cmdline/commands.c
93     :language: c
94     :start-after: Cmdline context list of commands in NULL-terminated table. 8<
95     :end-before: >8 End of context list.
96
97 Each command (of type cmdline_parse_inst_t) is defined statically.
98 It contains a pointer to a callback function that is executed when the command is parsed,
99 an opaque pointer, a help string and a list of tokens in a NULL-terminated table.
100
101 The rte_cmdline application provides a list of pre-defined token types:
102
103 *   String Token: Match a static string, a list of static strings or any string.
104
105 *   Number Token: Match a number that can be signed or unsigned, from 8-bit to 32-bit.
106
107 *   IP Address Token: Match an IPv4 or IPv6 address or network.
108
109 *   Ethernet* Address Token: Match a MAC address.
110
111 In this example, a new token type obj_list is defined and implemented
112 in the parse_obj_list.c and parse_obj_list.h files.
113
114 For example, the cmd_obj_del_show command is defined as shown below:
115
116 .. literalinclude:: ../../../examples/cmdline/commands.c
117     :language: c
118     :start-after: Show or delete tokens. 8<
119     :end-before: >8 End of show or delete tokens.
120
121 This command is composed of two tokens:
122
123 *   The first token is a string token that can be show or del.
124
125 *   The second token is an object that was previously added using the add command in the global_obj_list variable.
126
127 Once the command is parsed, the rte_cmdline application fills a cmd_obj_del_show_result structure.
128 A pointer to this structure is given as an argument to the callback function and can be used in the body of this function.