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