e8f01f61dfe15662f6b6f09206a30f6f26974024
[dpdk.git] / tools / setup.sh
1 #! /bin/bash
2
3 #   BSD LICENSE
4
5 #   Copyright(c) 2010-2012 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 #
36 # Run with "source /path/to/setup.sh"
37 #
38
39 #
40 # Change to DPDK directory ( <this-script's-dir>/.. ), and export it as RTE_SDK
41 #
42 cd $(dirname ${BASH_SOURCE[0]})/..
43 export RTE_SDK=$PWD
44 echo "------------------------------------------------------------------------------"
45 echo " RTE_SDK exported as $RTE_SDK"
46 echo "------------------------------------------------------------------------------"
47
48 #
49 # Application EAL parameters for setting memory options (amount/channels/ranks).
50 #
51 EAL_PARAMS='-n 4'
52
53 #
54 # Sets QUIT variable so script will finish.
55 #
56 quit()
57 {
58         QUIT=$1
59 }
60
61 #
62 # Sets up environmental variables for ICC.
63 #
64 setup_icc()
65 {
66         DEFAULT_PATH=/opt/intel/bin/iccvars.sh
67         param=$1
68         shpath=`which iccvars.sh 2> /dev/null`
69         if [ $? -eq 0 ] ; then
70                 echo "Loading iccvars.sh from $shpath for $param"
71                 source $shpath $param
72         elif [ -f $DEFAULT_PATH ] ; then
73                 echo "Loading iccvars.sh from $DEFAULT_PATH for $param"
74                 source $DEFAULT_PATH $param
75         else
76                 echo "## ERROR: cannot find 'iccvars.sh' script to set up ICC."
77                 echo "##     To fix, please add the directory that contains"
78                 echo "##     iccvars.sh  to your 'PATH' environment variable."
79                 quit
80         fi
81 }
82
83 #
84 # Sets RTE_TARGET and does a "make install".
85 #
86 setup_target()
87 {
88         option=$1
89         export RTE_TARGET=${TARGETS[option]}
90
91         compiler=${RTE_TARGET##*-}
92         if [ "$compiler" == "icc" ] ; then
93                 platform=${RTE_TARGET%%-*}
94                 if [ "$platform" == "x86_64" ] ; then
95                         setup_icc intel64
96                 else
97                         setup_icc ia32
98                 fi
99         fi
100         if [ "$QUIT" == "0" ] ; then
101                 make install T=${RTE_TARGET}
102         fi
103         echo "------------------------------------------------------------------------------"
104         echo " RTE_TARGET exported as $RTE_TARGET"
105         echo "------------------------------------------------------------------------------"
106 }
107
108 #
109 # Uninstall all targets.
110 #
111 uninstall_targets()
112 {
113         make uninstall
114 }
115
116 #
117 # Creates hugepage filesystem.
118 #
119 create_mnt_huge()
120 {
121         echo "Creating /mnt/huge and mounting as hugetlbfs"
122         sudo mkdir -p /mnt/huge
123
124         grep -s '/mnt/huge' /proc/mounts > /dev/null
125         if [ $? -ne 0 ] ; then
126                 sudo mount -t hugetlbfs nodev /mnt/huge
127         fi
128 }
129
130 #
131 # Removes hugepage filesystem.
132 #
133 remove_mnt_huge()
134 {
135         echo "Unmounting /mnt/huge and removing directory"
136         grep -s '/mnt/huge' /proc/mounts > /dev/null
137         if [ $? -eq 0 ] ; then
138                 sudo umount /mnt/huge
139         fi
140
141         if [ -d /mnt/huge ] ; then
142                 sudo rm -R /mnt/huge
143         fi
144 }
145
146 #
147 # Unloads igb_uio.ko.
148 #
149 remove_igb_uio_module()
150 {
151         echo "Unloading any existing DPDK UIO module"
152         /sbin/lsmod | grep -s igb_uio > /dev/null
153         if [ $? -eq 0 ] ; then
154                 sudo /sbin/rmmod igb_uio
155         fi
156 }
157
158 #
159 # Loads new igb_uio.ko (and uio module if needed).
160 #
161 load_igb_uio_module()
162 {
163         if [ ! -f $RTE_SDK/$RTE_TARGET/kmod/igb_uio.ko ];then
164                 echo "## ERROR: Target does not have the DPDK UIO Kernel Module."
165                 echo "       To fix, please try to rebuild target."
166                 return
167         fi
168
169         remove_igb_uio_module
170
171         /sbin/lsmod | grep -s uio > /dev/null
172         if [ $? -ne 0 ] ; then
173                 if [ -f /lib/modules/$(uname -r)/kernel/drivers/uio/uio.ko ] ; then
174                         echo "Loading uio module"
175                         sudo /sbin/modprobe uio
176                 fi
177         fi
178
179         # UIO may be compiled into kernel, so it may not be an error if it can't
180         # be loaded.
181
182         echo "Loading DPDK UIO module"
183         sudo /sbin/insmod $RTE_SDK/$RTE_TARGET/kmod/igb_uio.ko
184         if [ $? -ne 0 ] ; then
185                 echo "## ERROR: Could not load kmod/igb_uio.ko."
186                 quit
187         fi
188 }
189
190 #
191 # Unloads the rte_kni.ko module.
192 #
193 remove_kni_module()
194 {
195         echo "Unloading any existing DPDK KNI module"
196         /sbin/lsmod | grep -s rte_kni > /dev/null
197         if [ $? -eq 0 ] ; then
198                 sudo /sbin/rmmod rte_kni
199         fi
200 }
201
202 #
203 # Loads the rte_kni.ko module.
204 #
205 load_kni_module()
206 {
207     # Check that the KNI module is already built.
208         if [ ! -f $RTE_SDK/$RTE_TARGET/kmod/rte_kni.ko ];then
209                 echo "## ERROR: Target does not have the DPDK KNI Module."
210                 echo "       To fix, please try to rebuild target."
211                 return
212         fi
213
214     # Unload existing version if present.
215         remove_kni_module
216
217     # Now try load the KNI module.
218         echo "Loading DPDK KNI module"
219         sudo /sbin/insmod $RTE_SDK/$RTE_TARGET/kmod/rte_kni.ko
220         if [ $? -ne 0 ] ; then
221                 echo "## ERROR: Could not load kmod/rte_kni.ko."
222                 quit
223         fi
224 }
225
226 #
227 # Removes all reserved hugepages.
228 #
229 clear_huge_pages()
230 {
231         echo > .echo_tmp
232         for d in /sys/devices/system/node/node? ; do
233                 echo "echo 0 > $d/hugepages/hugepages-2048kB/nr_hugepages" >> .echo_tmp
234         done
235         echo "Removing currently reserved hugepages"
236         sudo sh .echo_tmp
237         rm -f .echo_tmp
238
239         remove_mnt_huge
240 }
241
242 #
243 # Creates hugepages.
244 #
245 set_non_numa_pages()
246 {
247         clear_huge_pages
248
249         echo ""
250         echo "  Input the number of 2MB pages"
251         echo "  Example: to have 128MB of hugepages available, enter '64' to"
252         echo "  reserve 64 * 2MB pages"
253         echo -n "Number of pages: "
254         read Pages
255
256         echo "echo $Pages > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages" > .echo_tmp
257
258         echo "Reserving hugepages"
259         sudo sh .echo_tmp
260         rm -f .echo_tmp
261
262         create_mnt_huge
263 }
264
265 #
266 # Creates hugepages on specific NUMA nodes.
267 #
268 set_numa_pages()
269 {
270         clear_huge_pages
271
272         echo ""
273         echo "  Input the number of 2MB pages for each node"
274         echo "  Example: to have 128MB of hugepages available per node,"
275         echo "  enter '64' to reserve 64 * 2MB pages on each node"
276
277         echo > .echo_tmp
278         for d in /sys/devices/system/node/node? ; do
279                 node=$(basename $d)
280                 echo -n "Number of pages for $node: "
281                 read Pages
282                 echo "echo $Pages > $d/hugepages/hugepages-2048kB/nr_hugepages" >> .echo_tmp
283         done
284         echo "Reserving hugepages"
285         sudo sh .echo_tmp
286         rm -f .echo_tmp
287
288         create_mnt_huge
289 }
290
291 #
292 # Run unit test application.
293 #
294 run_test_app()
295 {
296         echo ""
297         echo "  Enter hex bitmask of cores to execute test app on"
298         echo "  Example: to execute app on cores 0 to 7, enter 0xff"
299         echo -n "bitmask: "
300         read Bitmask
301         echo "Launching app"
302         sudo ${RTE_TARGET}/app/test -c $Bitmask $EAL_PARAMS
303 }
304
305 #
306 # Run unit testpmd application.
307 #
308 run_testpmd_app()
309 {
310         echo ""
311         echo "  Enter hex bitmask of cores to execute testpmd app on"
312         echo "  Example: to execute app on cores 0 to 7, enter 0xff"
313         echo -n "bitmask: "
314         read Bitmask
315         echo "Launching app"
316         sudo ${RTE_TARGET}/app/testpmd -c $Bitmask $EAL_PARAMS -- -i
317 }
318
319 #
320 # Print hugepage information.
321 #
322 grep_meminfo()
323 {
324         grep -i huge /proc/meminfo
325 }
326
327 #
328 # List all hugepage file references
329 #
330 ls_mnt_huge()
331 {
332         ls -lh /mnt/huge
333 }
334
335 #
336 # Options for building a target. Note that this step MUST be first as it sets
337 # up TARGETS[] starting from 1, and this is accessed in setup_target using the
338 # user entered option.
339 #
340 step1_func()
341 {
342         TITLE="Select the DPDK environment to build"
343         CONFIG_NUM=1
344         for cfg in config/defconfig_* ; do
345                 cfg=${cfg/config\/defconfig_/}
346                 TEXT[$CONFIG_NUM]="$cfg"
347                 TARGETS[$CONFIG_NUM]=$cfg
348                 FUNC[$CONFIG_NUM]="setup_target"
349                 let "CONFIG_NUM+=1"
350         done
351 }
352
353 #
354 # Options for setting up environment.
355 #
356 step2_func()
357 {
358         TITLE="Setup linuxapp environment"
359
360         TEXT[1]="Insert IGB UIO module"
361         FUNC[1]="load_igb_uio_module"
362
363         TEXT[2]="Insert KNI module"
364         FUNC[2]="load_kni_module"
365
366         TEXT[3]="Setup hugepage mappings for non-NUMA systems"
367         FUNC[3]="set_non_numa_pages"
368
369         TEXT[4]="Setup hugepage mappings for NUMA systems"
370         FUNC[4]="set_numa_pages"
371 }
372
373 #
374 # Options for running applications.
375 #
376 step3_func()
377 {
378         TITLE="Run test application for linuxapp environment"
379
380         TEXT[1]="Run test application (\$RTE_TARGET/app/test)"
381         FUNC[1]="run_test_app"
382
383         TEXT[2]="Run testpmd application in interactive mode (\$RTE_TARGET/app/testpmd)"
384         FUNC[2]="run_testpmd_app"
385 }
386
387 #
388 # Other options
389 #
390 step4_func()
391 {
392         TITLE="Other tools"
393
394         TEXT[1]="List hugepage info from /proc/meminfo"
395         FUNC[1]="grep_meminfo"
396
397         TEXT[2]="List hugepage files in /mnt/huge"
398         FUNC[2]="ls_mnt_huge"
399 }
400
401 #
402 # Options for cleaning up the system
403 #
404 step5_func()
405 {
406         TITLE="Uninstall and system cleanup"
407
408         TEXT[1]="Uninstall all targets"
409         FUNC[1]="uninstall_targets"
410
411         TEXT[2]="Remove IGB UIO module"
412         FUNC[2]="remove_igb_uio_module"
413
414         TEXT[3]="Remove KNI module"
415         FUNC[3]="remove_kni_module"
416
417         TEXT[4]="Remove hugepage mappings"
418         FUNC[4]="clear_huge_pages"
419 }
420
421 STEPS[1]="step1_func"
422 STEPS[2]="step2_func"
423 STEPS[3]="step3_func"
424 STEPS[4]="step4_func"
425 STEPS[5]="step5_func"
426
427 QUIT=0
428
429 while [ "$QUIT" == "0" ]; do
430         OPTION_NUM=1
431
432         for s in $(seq ${#STEPS[@]}) ; do
433                 ${STEPS[s]}
434
435                 echo "----------------------------------------------------------"
436                 echo " Step $s: ${TITLE}"
437                 echo "----------------------------------------------------------"
438
439                 for i in $(seq ${#TEXT[@]}) ; do
440                         echo "[$OPTION_NUM] ${TEXT[i]}"
441                         OPTIONS[$OPTION_NUM]=${FUNC[i]}
442                         let "OPTION_NUM+=1"
443                 done
444
445                 # Clear TEXT and FUNC arrays before next step
446                 unset TEXT
447                 unset FUNC
448
449                 echo ""
450         done
451
452         echo "[$OPTION_NUM] Exit Script"
453         OPTIONS[$OPTION_NUM]="quit"
454         echo ""
455         echo -n "Option: "
456         read our_entry
457         echo ""
458         ${OPTIONS[our_entry]} ${our_entry}
459         echo
460         echo -n "Press enter to continue ..."; read
461 done