doc: whitespace changes in licenses
[dpdk.git] / tools / setup.sh
1 #! /bin/bash
2
3 #   BSD LICENSE
4
5 #   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
6 #   All rights reserved.
7
8 #   Redistribution and use in source and binary forms, with or without
9 #   modification, are permitted provided that the following conditions
10 #   are met:
11
12 #     * Redistributions of source code must retain the above copyright
13 #       notice, this list of conditions and the following disclaimer.
14 #     * Redistributions in binary form must reproduce the above copyright
15 #       notice, this list of conditions and the following disclaimer in
16 #       the documentation and/or other materials provided with the
17 #       distribution.
18 #     * Neither the name of Intel Corporation nor the names of its
19 #       contributors may be used to endorse or promote products derived
20 #       from this software without specific prior written permission.
21
22 #   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 #   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 #   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 #   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 #   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 #   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 #   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 #   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 #   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 #
35 # Run with "source /path/to/setup.sh"
36 #
37
38 #
39 # Change to DPDK directory ( <this-script's-dir>/.. ), and export it as RTE_SDK
40 #
41 cd $(dirname ${BASH_SOURCE[0]})/..
42 export RTE_SDK=$PWD
43 echo "------------------------------------------------------------------------------"
44 echo " RTE_SDK exported as $RTE_SDK"
45 echo "------------------------------------------------------------------------------"
46
47 #
48 # Application EAL parameters for setting memory options (amount/channels/ranks).
49 #
50 EAL_PARAMS='-n 4'
51
52 #
53 # Sets QUIT variable so script will finish.
54 #
55 quit()
56 {
57         QUIT=$1
58 }
59
60 #
61 # Sets up environmental variables for ICC.
62 #
63 setup_icc()
64 {
65         DEFAULT_PATH=/opt/intel/bin/iccvars.sh
66         param=$1
67         shpath=`which iccvars.sh 2> /dev/null`
68         if [ $? -eq 0 ] ; then
69                 echo "Loading iccvars.sh from $shpath for $param"
70                 source $shpath $param
71         elif [ -f $DEFAULT_PATH ] ; then
72                 echo "Loading iccvars.sh from $DEFAULT_PATH for $param"
73                 source $DEFAULT_PATH $param
74         else
75                 echo "## ERROR: cannot find 'iccvars.sh' script to set up ICC."
76                 echo "##     To fix, please add the directory that contains"
77                 echo "##     iccvars.sh  to your 'PATH' environment variable."
78                 quit
79         fi
80 }
81
82 #
83 # Sets RTE_TARGET and does a "make install".
84 #
85 setup_target()
86 {
87         option=$1
88         export RTE_TARGET=${TARGETS[option]}
89
90         compiler=${RTE_TARGET##*-}
91         if [ "$compiler" == "icc" ] ; then
92                 platform=${RTE_TARGET%%-*}
93                 if [ "$platform" == "x86_64" ] ; then
94                         setup_icc intel64
95                 else
96                         setup_icc ia32
97                 fi
98         fi
99         if [ "$QUIT" == "0" ] ; then
100                 make install T=${RTE_TARGET}
101         fi
102         echo "------------------------------------------------------------------------------"
103         echo " RTE_TARGET exported as $RTE_TARGET"
104         echo "------------------------------------------------------------------------------"
105 }
106
107 #
108 # Uninstall all targets.
109 #
110 uninstall_targets()
111 {
112         make uninstall
113 }
114
115 #
116 # Creates hugepage filesystem.
117 #
118 create_mnt_huge()
119 {
120         echo "Creating /mnt/huge and mounting as hugetlbfs"
121         sudo mkdir -p /mnt/huge
122
123         grep -s '/mnt/huge' /proc/mounts > /dev/null
124         if [ $? -ne 0 ] ; then
125                 sudo mount -t hugetlbfs nodev /mnt/huge
126         fi
127 }
128
129 #
130 # Removes hugepage filesystem.
131 #
132 remove_mnt_huge()
133 {
134         echo "Unmounting /mnt/huge and removing directory"
135         grep -s '/mnt/huge' /proc/mounts > /dev/null
136         if [ $? -eq 0 ] ; then
137                 sudo umount /mnt/huge
138         fi
139
140         if [ -d /mnt/huge ] ; then
141                 sudo rm -R /mnt/huge
142         fi
143 }
144
145 #
146 # Unloads igb_uio.ko.
147 #
148 remove_igb_uio_module()
149 {
150         echo "Unloading any existing DPDK UIO module"
151         /sbin/lsmod | grep -s igb_uio > /dev/null
152         if [ $? -eq 0 ] ; then
153                 sudo /sbin/rmmod igb_uio
154         fi
155 }
156
157 #
158 # Loads new igb_uio.ko (and uio module if needed).
159 #
160 load_igb_uio_module()
161 {
162         if [ ! -f $RTE_SDK/$RTE_TARGET/kmod/igb_uio.ko ];then
163                 echo "## ERROR: Target does not have the DPDK UIO Kernel Module."
164                 echo "       To fix, please try to rebuild target."
165                 return
166         fi
167
168         remove_igb_uio_module
169
170         /sbin/lsmod | grep -s uio > /dev/null
171         if [ $? -ne 0 ] ; then
172                 if [ -f /lib/modules/$(uname -r)/kernel/drivers/uio/uio.ko ] ; then
173                         echo "Loading uio module"
174                         sudo /sbin/modprobe uio
175                 fi
176         fi
177
178         # UIO may be compiled into kernel, so it may not be an error if it can't
179         # be loaded.
180
181         echo "Loading DPDK UIO module"
182         sudo /sbin/insmod $RTE_SDK/$RTE_TARGET/kmod/igb_uio.ko
183         if [ $? -ne 0 ] ; then
184                 echo "## ERROR: Could not load kmod/igb_uio.ko."
185                 quit
186         fi
187 }
188
189 #
190 # Unloads the rte_kni.ko module.
191 #
192 remove_kni_module()
193 {
194         echo "Unloading any existing DPDK KNI module"
195         /sbin/lsmod | grep -s rte_kni > /dev/null
196         if [ $? -eq 0 ] ; then
197                 sudo /sbin/rmmod rte_kni
198         fi
199 }
200
201 #
202 # Loads the rte_kni.ko module.
203 #
204 load_kni_module()
205 {
206     # Check that the KNI module is already built.
207         if [ ! -f $RTE_SDK/$RTE_TARGET/kmod/rte_kni.ko ];then
208                 echo "## ERROR: Target does not have the DPDK KNI Module."
209                 echo "       To fix, please try to rebuild target."
210                 return
211         fi
212
213     # Unload existing version if present.
214         remove_kni_module
215
216     # Now try load the KNI module.
217         echo "Loading DPDK KNI module"
218         sudo /sbin/insmod $RTE_SDK/$RTE_TARGET/kmod/rte_kni.ko
219         if [ $? -ne 0 ] ; then
220                 echo "## ERROR: Could not load kmod/rte_kni.ko."
221                 quit
222         fi
223 }
224
225 #
226 # Removes all reserved hugepages.
227 #
228 clear_huge_pages()
229 {
230         echo > .echo_tmp
231         for d in /sys/devices/system/node/node? ; do
232                 echo "echo 0 > $d/hugepages/hugepages-2048kB/nr_hugepages" >> .echo_tmp
233         done
234         echo "Removing currently reserved hugepages"
235         sudo sh .echo_tmp
236         rm -f .echo_tmp
237
238         remove_mnt_huge
239 }
240
241 #
242 # Creates hugepages.
243 #
244 set_non_numa_pages()
245 {
246         clear_huge_pages
247
248         echo ""
249         echo "  Input the number of 2MB pages"
250         echo "  Example: to have 128MB of hugepages available, enter '64' to"
251         echo "  reserve 64 * 2MB pages"
252         echo -n "Number of pages: "
253         read Pages
254
255         echo "echo $Pages > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages" > .echo_tmp
256
257         echo "Reserving hugepages"
258         sudo sh .echo_tmp
259         rm -f .echo_tmp
260
261         create_mnt_huge
262 }
263
264 #
265 # Creates hugepages on specific NUMA nodes.
266 #
267 set_numa_pages()
268 {
269         clear_huge_pages
270
271         echo ""
272         echo "  Input the number of 2MB pages for each node"
273         echo "  Example: to have 128MB of hugepages available per node,"
274         echo "  enter '64' to reserve 64 * 2MB pages on each node"
275
276         echo > .echo_tmp
277         for d in /sys/devices/system/node/node? ; do
278                 node=$(basename $d)
279                 echo -n "Number of pages for $node: "
280                 read Pages
281                 echo "echo $Pages > $d/hugepages/hugepages-2048kB/nr_hugepages" >> .echo_tmp
282         done
283         echo "Reserving hugepages"
284         sudo sh .echo_tmp
285         rm -f .echo_tmp
286
287         create_mnt_huge
288 }
289
290 #
291 # Run unit test application.
292 #
293 run_test_app()
294 {
295         echo ""
296         echo "  Enter hex bitmask of cores to execute test app on"
297         echo "  Example: to execute app on cores 0 to 7, enter 0xff"
298         echo -n "bitmask: "
299         read Bitmask
300         echo "Launching app"
301         sudo ${RTE_TARGET}/app/test -c $Bitmask $EAL_PARAMS
302 }
303
304 #
305 # Run unit testpmd application.
306 #
307 run_testpmd_app()
308 {
309         echo ""
310         echo "  Enter hex bitmask of cores to execute testpmd app on"
311         echo "  Example: to execute app on cores 0 to 7, enter 0xff"
312         echo -n "bitmask: "
313         read Bitmask
314         echo "Launching app"
315         sudo ${RTE_TARGET}/app/testpmd -c $Bitmask $EAL_PARAMS -- -i
316 }
317
318 #
319 # Print hugepage information.
320 #
321 grep_meminfo()
322 {
323         grep -i huge /proc/meminfo
324 }
325
326 #
327 # Calls pci_unbind.py --status to show the NIC and what they
328 # are all bound to, in terms of drivers.
329 #
330 show_nics()
331 {
332         if  /sbin/lsmod  | grep -q igb_uio ; then 
333                 ${RTE_SDK}/tools/pci_unbind.py --status
334         else 
335                 echo "# Please load the 'igb_uio' kernel module before querying or "
336                 echo "# adjusting NIC device bindings"
337         fi
338 }
339
340 #
341 # Uses pci_unbind.py to move devices to work with igb_uio
342 #
343 bind_nics()
344 {
345         if  /sbin/lsmod  | grep -q igb_uio ; then 
346                 ${RTE_SDK}/tools/pci_unbind.py --status
347                 echo ""
348                 echo -n "Enter PCI address of device to bind to IGB UIO driver: "
349                 read PCI_PATH
350                 sudo ${RTE_SDK}/tools/pci_unbind.py -b igb_uio $PCI_PATH && echo "OK"
351         else 
352                 echo "# Please load the 'igb_uio' kernel module before querying or "
353                 echo "# adjusting NIC device bindings"
354         fi
355 }
356
357 #
358 # Uses pci_unbind.py to move devices to work with kernel drivers again
359 #
360 unbind_nics()
361 {
362         ${RTE_SDK}/tools/pci_unbind.py --status
363         echo ""
364         echo -n "Enter PCI address of device to bind to IGB UIO driver: "
365         read PCI_PATH
366         echo ""
367         echo -n "Enter name of kernel driver to bind the device to: "
368         read DRV
369         sudo ${RTE_SDK}/tools/pci_unbind.py -b $DRV $PCI_PATH && echo "OK"
370 }
371
372 #
373 # Options for building a target. Note that this step MUST be first as it sets
374 # up TARGETS[] starting from 1, and this is accessed in setup_target using the
375 # user entered option.
376 #
377 step1_func()
378 {
379         TITLE="Select the DPDK environment to build"
380         CONFIG_NUM=1
381         for cfg in config/defconfig_* ; do
382                 cfg=${cfg/config\/defconfig_/}
383                 TEXT[$CONFIG_NUM]="$cfg"
384                 TARGETS[$CONFIG_NUM]=$cfg
385                 FUNC[$CONFIG_NUM]="setup_target"
386                 let "CONFIG_NUM+=1"
387         done
388 }
389
390 #
391 # Options for setting up environment.
392 #
393 step2_func()
394 {
395         TITLE="Setup linuxapp environment"
396
397         TEXT[1]="Insert IGB UIO module"
398         FUNC[1]="load_igb_uio_module"
399
400         TEXT[2]="Insert KNI module"
401         FUNC[2]="load_kni_module"
402
403         TEXT[3]="Setup hugepage mappings for non-NUMA systems"
404         FUNC[3]="set_non_numa_pages"
405
406         TEXT[4]="Setup hugepage mappings for NUMA systems"
407         FUNC[4]="set_numa_pages"
408
409         TEXT[5]="Display current Ethernet device settings"
410         FUNC[5]="show_nics"
411
412         TEXT[6]="Bind Ethernet device to IGB UIO module"
413         FUNC[6]="bind_nics"
414 }
415
416 #
417 # Options for running applications.
418 #
419 step3_func()
420 {
421         TITLE="Run test application for linuxapp environment"
422
423         TEXT[1]="Run test application (\$RTE_TARGET/app/test)"
424         FUNC[1]="run_test_app"
425
426         TEXT[2]="Run testpmd application in interactive mode (\$RTE_TARGET/app/testpmd)"
427         FUNC[2]="run_testpmd_app"
428 }
429
430 #
431 # Other options
432 #
433 step4_func()
434 {
435         TITLE="Other tools"
436
437         TEXT[1]="List hugepage info from /proc/meminfo"
438         FUNC[1]="grep_meminfo"
439
440 }
441
442 #
443 # Options for cleaning up the system
444 #
445 step5_func()
446 {
447         TITLE="Uninstall and system cleanup"
448
449         TEXT[1]="Uninstall all targets"
450         FUNC[1]="uninstall_targets"
451
452         TEXT[2]="Unbind NICs from IGB UIO driver"
453         FUNC[2]="unbind_nics"
454
455         TEXT[3]="Remove IGB UIO module"
456         FUNC[3]="remove_igb_uio_module"
457
458         TEXT[4]="Remove KNI module"
459         FUNC[4]="remove_kni_module"
460
461         TEXT[5]="Remove hugepage mappings"
462         FUNC[5]="clear_huge_pages"
463 }
464
465 STEPS[1]="step1_func"
466 STEPS[2]="step2_func"
467 STEPS[3]="step3_func"
468 STEPS[4]="step4_func"
469 STEPS[5]="step5_func"
470
471 QUIT=0
472
473 while [ "$QUIT" == "0" ]; do
474         OPTION_NUM=1
475
476         for s in $(seq ${#STEPS[@]}) ; do
477                 ${STEPS[s]}
478
479                 echo "----------------------------------------------------------"
480                 echo " Step $s: ${TITLE}"
481                 echo "----------------------------------------------------------"
482
483                 for i in $(seq ${#TEXT[@]}) ; do
484                         echo "[$OPTION_NUM] ${TEXT[i]}"
485                         OPTIONS[$OPTION_NUM]=${FUNC[i]}
486                         let "OPTION_NUM+=1"
487                 done
488
489                 # Clear TEXT and FUNC arrays before next step
490                 unset TEXT
491                 unset FUNC
492
493                 echo ""
494         done
495
496         echo "[$OPTION_NUM] Exit Script"
497         OPTIONS[$OPTION_NUM]="quit"
498         echo ""
499         echo -n "Option: "
500         read our_entry
501         echo ""
502         ${OPTIONS[our_entry]} ${our_entry}
503         echo
504         echo -n "Press enter to continue ..."; read
505 done