New upstream version 17.11-rc3
[deb_dpdk.git] / doc / guides / sample_app_ug / hello_world.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 Hello World Sample Application
32 ==============================
33
34 The Hello World sample application is an example of the simplest DPDK application that can be written.
35 The application simply prints an "helloworld" message on every enabled lcore.
36
37 Compiling the Application
38 -------------------------
39
40 To compile the sample application see :doc:`compiling`.
41
42 The application is located in the ``helloworld`` sub-directory.
43
44 Running the Application
45 -----------------------
46
47 To run the example in a linuxapp environment:
48
49 .. code-block:: console
50
51     $ ./build/helloworld -l 0-3 -n 4
52
53 Refer to *DPDK Getting Started Guide* for general information on running applications
54 and the Environment Abstraction Layer (EAL) options.
55
56 Explanation
57 -----------
58
59 The following sections provide some explanation of code.
60
61 EAL Initialization
62 ~~~~~~~~~~~~~~~~~~
63
64 The first task is to initialize the Environment Abstraction Layer (EAL).
65 This is done in the main() function using the following code:
66
67 .. code-block:: c
68
69     int
70
71     main(int argc, char **argv)
72
73     {
74         ret = rte_eal_init(argc, argv);
75         if (ret < 0)
76             rte_panic("Cannot init EAL\n");
77
78 This call finishes the initialization process that was started before main() is called (in case of a Linuxapp environment).
79 The argc and argv arguments are provided to the rte_eal_init() function.
80 The value returned is the number of parsed arguments.
81
82 Starting Application Unit Lcores
83 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84
85 Once the EAL is initialized, the application is ready to launch a function on an lcore.
86 In this example, lcore_hello() is called on every available lcore.
87 The following is the definition of the function:
88
89 .. code-block:: c
90
91     static int
92     lcore_hello( attribute ((unused)) void *arg)
93     {
94         unsigned lcore_id;
95
96         lcore_id = rte_lcore_id();
97         printf("hello from core %u\n", lcore_id);
98         return 0;
99     }
100
101 The code that launches the function on each lcore is as follows:
102
103 .. code-block:: c
104
105     /* call lcore_hello() on every slave lcore */
106
107     RTE_LCORE_FOREACH_SLAVE(lcore_id) {
108        rte_eal_remote_launch(lcore_hello, NULL, lcore_id);
109     }
110
111     /* call it on master lcore too */
112
113     lcore_hello(NULL);
114
115 The following code is equivalent and simpler:
116
117 .. code-block:: c
118
119     rte_eal_mp_remote_launch(lcore_hello, NULL, CALL_MASTER);
120
121 Refer to the *DPDK API Reference* for detailed information on the rte_eal_mp_remote_launch() function.