xref: /dpdk/doc/guides/freebsd_gsg/build_dpdk.rst (revision bd89cca3ca34d255e48fa4246998c89bb38301d4)
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.. _building_from_source:
32
33Compiling the DPDK Target from Source
34=====================================
35
36.. note::
37
38    Testing has been performed using FreeBSD* 10.0-RELEASE (x86_64) and requires the
39    installation of the kernel sources, which should be included during the
40    installation of FreeBSD*. The DPDK also requires the use of FreeBSD*
41    ports to compile and function.
42
43System Requirements
44-------------------
45
46The DPDK and its applications require the GNU make system (gmake)
47to build on FreeBSD*. Optionally, gcc may also be used in place of clang
48to build the DPDK, in which case it too must be installed prior to
49compiling the DPDK. The installation of these tools is covered in this
50section.
51
52Compiling the DPDK requires the FreeBSD kernel sources, which should be
53included during the installation of FreeBSD* on the development platform.
54The DPDK also requires the use of FreeBSD* ports to compile and function.
55
56To use the FreeBSD* ports system, it is required to update and extract the FreeBSD*
57ports tree by issuing the following commands:
58
59.. code-block:: console
60
61    root@host:~ # portsnap fetch
62    root@host:~ # portsnap extract
63
64If the environment requires proxies for external communication, these can be set
65using:
66
67.. code-block:: console
68
69    root@host:~ # setenv http_proxy <my_proxy_host>:<port>
70    root@host:~ # setenv ftp_proxy <my_proxy_host>:<port>
71
72The FreeBSD* ports below need to be installed prior to building the DPDK.
73In general these can be installed using the following set of commands:
74
75#.  cd /usr/ports/<port_location>
76
77#.  make config-recursive
78
79#.  make install
80
81#.  make clean
82
83Each port location can be found using:
84
85.. code-block:: console
86
87    user@host:~ # whereis <port_name>
88
89The ports required and their locations are as follows:
90
91dialog4ports
92   /usr/ports/ports-mgmt/dialog4ports
93
94GNU make(gmake)
95   /usr/ports/devel/gmake
96
97coreutils
98   /usr/ports/sysutils/coreutils
99
100For compiling and using the DPDK with gcc, it too must be installed
101from the ports collection:
102
103gcc: version 4.8 is recommended
104   /usr/ports/lang/gcc48
105   (Ensure that CPU_OPTS is selected (default is OFF))
106
107When running the make config-recursive command, a dialog may be presented to the
108user. For the installation of the DPDK, the default options were used.
109
110.. note::
111
112    To avoid multiple dialogs being presented to the user during make install,
113    it is advisable before running the make install command to re-run the
114    make config -recursive command until no more dialogs are seen.
115
116
117Install the DPDK and Browse Sources
118-----------------------------------
119
120First, uncompress the archive and move to the DPDK source directory:
121
122.. code-block:: console
123
124    user@host:~ # unzip DPDK-<version>zip
125    user@host:~ # cd DPDK-<version>
126    user@host:~/DPDK # ls
127    app/ config/ examples/ lib/ LICENSE.GPL LICENSE.LGPL Makefile mk/ scripts/ tools/
128
129The DPDK is composed of several directories:
130
131*   lib: Source code of DPDK libraries
132
133*   app: Source code of DPDK applications (automatic tests)
134
135*   examples: Source code of DPDK applications
136
137*   config, tools, scripts, mk: Framework-related makefiles, scripts and configuration
138
139Installation of the DPDK Target Environments
140--------------------------------------------
141
142The format of a DPDK target is:
143
144ARCH-MACHINE-EXECENV-TOOLCHAIN
145
146Where:
147
148*   ARCH is:   x86_64
149
150*   MACHINE is: native
151
152*   EXECENV is: bsdapp
153
154*   TOOLCHAIN is: gcc | clang
155
156The configuration files for the DPDK targets can be found in the DPDK/config
157directory in the form of:
158
159::
160
161    defconfig_ARCH-MACHINE-EXECENV-TOOLCHAIN
162
163.. note::
164
165    Configuration files are provided with the RTE_MACHINE optimization level set.
166    Within the configuration files, the RTE_MACHINE configuration value is set
167    to native, which means that the compiled software is tuned for the platform
168    on which it is built.  For more information on this setting, and its
169    possible values, see the *DPDK Programmers Guide*.
170
171To install and make the target, use "gmake install T=<target>".
172
173For example to compile for FreeBSD* use:
174
175.. code-block:: console
176
177    gmake install T=x86_64-native-bsdapp-clang
178
179.. note::
180
181	If the compiler binary to be used does not correspond to that given in the
182	TOOLCHAIN part of the target, the compiler command may need to be explicitly
183	specified. For example, if compiling for gcc, where the gcc binary is called
184	gcc4.8, the command would need to be "gmake install T=<target> CC=gcc4.8".
185
186Browsing the Installed DPDK Environment Target
187----------------------------------------------
188
189Once a target is created, it contains all the libraries and header files for the
190DPDK environment that are required to build customer applications.
191In addition, the test and testpmd applications are built under the build/app
192directory, which may be used for testing.  A kmod directory is also present that
193contains the kernel modules to install:
194
195.. code-block:: console
196
197    user@host:~/DPDK # ls x86_64-native-bsdapp-gcc
198    app   build    hostapp    include    kmod    lib    Makefile
199
200
201.. _loading_contigmem:
202
203Loading the DPDK contigmem Module
204---------------------------------
205
206To run a DPDK application, physically contiguous memory is required.
207In the absence of non-transparent superpages, the included sources for the
208contigmem kernel module provides the ability to present contiguous blocks of
209memory for the DPDK to use. The contigmem module must be loaded into the
210running kernel before any DPDK is run.  The module is found in the kmod
211sub-directory of the DPDK target directory.
212
213The amount of physically contiguous memory along with the number of physically
214contiguous blocks to be reserved by the module can be set at runtime prior to
215module loading using:
216
217.. code-block:: console
218
219    root@host:~ # kenv hw.contigmem.num_buffers=n
220    root@host:~ # kenv hw.contigmem.buffer_size=m
221
222The kernel environment variables can also be specified during boot by placing the
223following in /boot/loader.conf:
224
225::
226
227    hw.contigmem.num_buffers=n hw.contigmem.buffer_size=m
228
229The variables can be inspected using the following command:
230
231.. code-block:: console
232
233    root@host:~ # sysctl -a hw.contigmem
234
235Where n is the number of blocks and m is the size in bytes of each area of
236contiguous memory.  A default of two buffers of size 1073741824 bytes (1 Gigabyte)
237each is set during module load if they are not specified in the environment.
238
239The module can then be loaded using kldload (assuming that the current directory
240is the DPDK target directory):
241
242.. code-block:: console
243
244    kldload ./kmod/contigmem.ko
245
246It is advisable to include the loading of the contigmem module during the boot
247process to avoid issues with potential memory fragmentation during later system
248up time.  This can be achieved by copying the module to the /boot/kernel/
249directory and placing the following into /boot/loader.conf:
250
251::
252
253    contigmem_load="YES"
254
255.. note::
256
257    The contigmem_load directive should be placed after any definitions of
258    hw.contigmem.num_buffers and hw.contigmem.buffer_size if the default values
259    are not to be used.
260
261An error such as:
262
263.. code-block:: console
264
265    kldload: can't load ./x86_64-native-bsdapp-gcc/kmod/contigmem.ko: Exec format error
266
267is generally attributed to not having enough contiguous memory
268available and can be verified via dmesg or /var/log/messages:
269
270.. code-block:: console
271
272    kernel: contigmalloc failed for buffer <n>
273
274To avoid this error, reduce the number of buffers or the buffer size.
275
276.. _loading_nic_uio:
277
278Loading the DPDK nic_uio Module
279-------------------------------
280
281After loading the contigmem module, the nic_uio must also be loaded into the
282running kernel prior to running any DPDK application.  This module must
283be loaded using the kldload command as shown below (assuming that the current
284directory is the DPDK target directory).
285
286.. code-block:: console
287
288    kldload ./kmod/nic_uio.ko
289
290.. note::
291
292    If the ports to be used are currently bound to a existing kernel driver
293    then the hw.nic_uio.bdfs sysctl value will need to be set before loading the
294    module. Setting this value is described in the next section below.
295
296Currently loaded modules can be seen by using the "kldstat" command and a module
297can be removed from the running kernel by using "kldunload <module_name>".
298
299To load the module during boot, copy the nic_uio module to /boot/kernel
300and place the following into /boot/loader.conf:
301
302::
303
304    nic_uio_load="YES"
305
306.. note::
307
308    nic_uio_load="YES" must appear after the contigmem_load directive, if it exists.
309
310By default, the nic_uio module will take ownership of network ports if they are
311recognized DPDK devices and are not owned by another module. However, since
312the FreeBSD kernel includes support, either built-in, or via a separate driver
313module, for most network card devices, it is likely that the ports to be used are
314already bound to a driver other than nic_uio. The following sub-section describe
315how to query and modify the device ownership of the ports to be used by
316DPDK applications.
317
318.. _binding_network_ports:
319
320Binding Network Ports to the nic_uio Module
321~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
322
323Device ownership can be viewed using the pciconf -l command. The example below shows
324four Intel® 82599 network ports under "if_ixgbe" module ownership.
325
326.. code-block:: console
327
328    user@host:~ # pciconf -l
329    ix0@pci0:1:0:0: class=0x020000 card=0x00038086 chip=0x10fb8086 rev=0x01 hdr=0x00
330    ix1@pci0:1:0:1: class=0x020000 card=0x00038086 chip=0x10fb8086 rev=0x01 hdr=0x00
331    ix2@pci0:2:0:0: class=0x020000 card=0x00038086 chip=0x10fb8086 rev=0x01 hdr=0x00
332    ix3@pci0:2:0:1: class=0x020000 card=0x00038086 chip=0x10fb8086 rev=0x01 hdr=0x00
333
334The first column constitutes three components:
335
336#.  Device name: ixN
337
338#.  Unit name:  pci0
339
340#.  Selector (Bus:Device:Function):   1:0:0
341
342Where no driver is associated with a device, the device name will be none.
343
344By default, the FreeBSD* kernel will include built-in drivers for the most common
345devices; a kernel rebuild would normally be required to either remove the drivers
346or configure them as loadable modules.
347
348To avoid building a custom kernel, the nic_uio module can detach a network port
349from its current device driver.  This is achieved by setting the hw.nic_uio.bdfs
350kernel environment variable prior to loading nic_uio, as follows:
351
352::
353
354    hw.nic_uio.bdfs="b:d:f,b:d:f,..."
355
356Where a comma separated list of selectors is set, the list must not contain any
357whitespace.
358
359For example to re-bind "ix2\@pci0:2:0:0" and "ix3\@pci0:2:0:1" to the nic_uio module
360upon loading, use the following command:
361
362.. code-block:: console
363
364    kenv hw.nic_uio.bdfs="2:0:0,2:0:1"
365
366The variable can also be specified during boot by placing the following into
367"/boot/loader.conf", before the previously-described "nic_uio_load" line - as
368shown.
369
370::
371
372    hw.nic_uio.bdfs="2:0:0,2:0:1"
373    nic_uio_load="YES"
374
375Binding Network Ports Back to their Original Kernel Driver
376~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
377
378If the original driver for a network port has been compiled into the kernel,
379it is necessary to reboot FreeBSD* to restore the original device binding. Before
380doing so, update or remove the "hw.nic_uio.bdfs" in "/boot/loader.conf".
381
382If rebinding to a driver that is a loadable module, the network port binding can
383be reset without rebooting. To do so, unload both the target kernel module and the
384nic_uio module, modify or clear the "hw.nic_uio.bdfs" kernel environment (kenv)
385value, and reload the two drivers - first the original kernel driver, and then
386the nic_uio driver. [The latter does not need to be reloaded unless there are
387ports that are still to be bound to it].
388
389Example commands to perform these steps are shown below:
390
391.. code-block:: console
392
393    kldunload nic_uio
394    kldunload <original_driver>
395
396    kenv -u hw.nic_uio.bdfs  # to clear the value completely
397
398    kenv hw.nic_uio.bdfs="b:d:f,b:d:f,..." # to update the list of ports to bind
399
400    kldload <original_driver>
401
402    kldload nic_uio  # optional
403