backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / mininet / run_mnet-test.sh
1 #!/bin/sh
2
3 RUN_DIR=`dirname $0`
4 CMD_NAME=`basename $0 .sh`
5 CMD_PATH=`readlink -f $0`
6 CMD_DIR=`dirname $CMD_PATH`
7 DUMP_SEC=10
8 DUMP_DELAY=2
9 DUMP_DIR=/tmp/test-mn/dump
10 TEST_LIST=
11 TEST_SUFFIX=.mn
12 MN_PRE_FILE=/tmp/test-mn/mn-pre
13 MN_POST_FILE=/tmp/test-mn/mn-post
14 PKG_LIST="tshark tcpreplay mz"
15 RTN=0
16
17 # usage
18 usage() {
19     echo "Usage: $0 [OPTION] [TEST DIR or FILE]..."
20     echo ""
21     echo "Run Ryu's test in mininet"
22     echo "ex.) $ $0 l2 l3/icmp/ICMP_ping.mn"
23     echo ""
24     echo "Options:"
25     echo "  -h, --help  show this help message and exit"
26     exit 0
27 }
28
29 # set default environment
30 set_env() {
31     POST_IF=h1-eth0
32     DUMP_HOST=h2
33     DUMP_IF=h2-eth0
34     TEST_NAME=
35     DUMP_FILE=
36     RYU_APP=
37     RYU_LOG=
38     PCAP_MZ=
39     PCAP_FILE=
40     PCAP_FILTER=
41     PCAP_COM=
42     CACHE_HIT=
43 }
44
45 # making mininet-test-pre-file
46 mn_pre() {
47     exec 3>&1
48     exec >$MN_PRE_FILE
49     echo "sh echo '----------------------------------'"
50     echo "sh echo '(pre) mininet topology dump.'"
51     echo "sh echo '----------------------------------'"
52     echo "dump"
53     echo "net"
54     echo "sh echo '----------------------------------'"
55     echo "sh echo '(pre) tshark start.'"
56     echo "sh echo '----------------------------------'"
57     echo "$DUMP_HOST tshark -i $DUMP_IF -a duration:$DUMP_SEC -w $DUMP_FILE &"
58     echo "sh sleep $DUMP_DELAY"
59     echo "sh echo '----------------------------------'"
60     exec 1>&3
61 }
62
63 # making mininet-test-post-file
64 mn_post() {
65     exec 3>&1
66     exec >$MN_POST_FILE
67     echo "sh ovs-vsctl del-controller s1"
68     echo "sh ovs-vsctl set bridge s1 protocols='[OpenFlow10,OpenFlow12]'"
69     echo "sh ovs-vsctl set-controller s1 tcp:127.0.0.1"
70     echo "sh echo '----------------------------------'"
71     echo "sh echo '(post) packet sending...'"
72     echo "sh echo '----------------------------------'"
73     echo $PCAP_COM
74     echo "sh sleep 1"
75     echo "sh echo '----------------------------------'"
76     echo "sh echo '(post) dump flows.'"
77     echo "sh echo '----------------------------------'"
78     echo "sh ovs-ofctl dump-flows s1"
79     echo "sh echo '----------------------------------'"
80     exec 1>&3
81 }
82
83 # ovs cache-hit incremental check
84 ovs_cache_hit() {
85     expr `sudo ovs-dpctl show|sed -n 's|lookups: hit:||gp'|awk '{print $1}'` - ${1:-0}
86 }
87
88 # starting ryu-manager
89 run_ryu() {
90     ERRSTAT=0
91     ERRTAG="run_ryu() :"
92
93     echo "Inf: RYU_APP=$RYU_APP"
94     echo "Inf: ryu-manager starting..."
95     ryu-manager --verbose $RYU_APP 2>$DUMP_DIR/$RYU_LOG &
96     PID_RYU=$!
97     sleep 1
98     [ -d /proc/$PID_RYU ] || err $ERRTAG "failed to start ryu-manager."
99
100     return $ERRSTAT
101 }
102
103 # starting mininet and test-script
104 run_mn() {
105     echo "Info: mininet starting..."
106     sudo mn --mac --test none --pre $MN_PRE_FILE --post $MN_POST_FILE \
107             --controller remote 127.0.0.1
108 }
109
110 # cleaning after mininet
111 clean_mn() {
112     wait_ryu
113     rm -f $MN_PRE_FILE $MN_POST_FILE
114 }
115
116 # check packet and chache-hit
117 check() {
118     PACKET=`tshark -r $DUMP_FILE -R "$PCAP_FILTER" 2>/dev/null`
119     if [ ! "$PACKET" ]; then
120         RESULT=NG
121         REASON="(unmatched packet. please check $DUMP_FILE)"
122     elif [ "$CACHE_HIT" ] && [ `ovs_cache_hit $CACHE_HIT` -eq 0 ]; then
123         RESULT=NG
124         REASON="(ovs cache hit miss.)"
125     else
126         RESULT=OK; REASON=
127     fi
128     echo
129     echo "TEST ${TEST_NAME} : $RESULT $REASON"
130 }
131
132 # stoping ryu-manager
133 wait_ryu() {
134     kill -2 $PID_RYU
135     wait $PID_RYU
136 }
137
138 # test-main
139 test_mn() {
140     DUMP_FILE=$DUMP_DIR/$DUMP_FILE
141     touch $DUMP_FILE
142     sudo chmod o+w $DUMP_FILE
143     [ "$CACHE_HIT" ] && CACHE_HIT=`ovs_cache_hit 0`
144     mn_pre
145     mn_post
146     run_ryu; [ $? -ne 0 ] && return 1
147     run_mn; [ $? -ne 0 ] && return 1
148     check
149
150     return 0
151 }
152
153 err() {
154     echo Error: $*
155     ERRSTAT=1
156 }
157
158 mnfile_check() {
159     test=`basename $1 $TEST_SUFFIX`
160     file=`readlink -f $1`
161     TEST_DIR=`dirname $file`
162     ERRSTAT=0
163     ERRTAG="mnfile_check() :"
164
165     # test-file check
166     if [ ! -r $file ]; then
167         err $ERRTAG "cannot open the file: $file"
168         return $ERRSTAT
169     fi
170
171     . $file || err $ERRTAG "failed to include $file"
172
173     # parameter check
174     [ "$RYU_APP" ] || err $ERRTAG: "RYU_APP is not defined"
175     [ "$PCAP_FILE" -o "$PCAP_MZ" ] || err $ERRTAG: "PCAP_FILE or PCAP_MZ is not defined"
176     [ "$PCAP_FILTER" ] || err $ERRTAG "PCAP_FILTER is not defined"
177     [ "$TEST_NAME" ] || TEST_NAME=$test
178     [ "$DUMP_FILE" ] || DUMP_FILE=$test.dump
179     [ "$RYU_LOG" ] || RYU_LOG=ryu-manager.$test.log
180     [ $ERRSTAT -ne 0 ] && return $ERRSTAT
181
182     # pcap check (pcap-file or mz-option)
183     if [ "$PCAP_FILE" ]; then
184         PCAP_FILE=$TEST_DIR/$PCAP_FILE
185         [ -r $PCAP_FILE ] || err $ERRTAG "PCAP_FILE[$PCAP_FILE] cannot read"
186         PCAP_COM="h1 tcpreplay -l 3 -i $POST_IF $PCAP_FILE"
187     elif [ "$PCAP_MZ" ]; then
188         PCAP_COM="h1 mz $POST_IF $PCAP_MZ"
189     fi
190     [ $ERRSTAT -ne 0 ] && return $ERRSTAT
191
192     # ryu-app check
193     [ -r $TEST_DIR/$RYU_APP -o -r $TEST_DIR/${RYU_APP}.py ] && RYU_APP=$TEST_DIR/$RYU_APP
194
195     return $ERRSTAT
196 }
197
198 arg_check() {
199     ARGLIST=
200     ERRTAG="argcheck() :"
201
202     case "$1" in
203         -h|--help) usage;;
204     esac
205
206     if [ $# -ne 0 ]; then
207         ARGLIST=$*
208     else
209         ARGLIST=`find . -type f -name "*$TEST_SUFFIX"`
210     fi
211
212     for arg in $ARGLIST; do
213         if [ -d $arg ]; then
214             file=`find $arg -type f -name "*$TEST_SUFFIX"`
215         elif [ -f $arg ]; then
216             file=$arg
217         else
218             err $ERRTAG "$arg is not found"
219             file=
220         fi
221
222         TEST_LIST="$TEST_LIST $file"
223     done
224 }
225
226 pkg_check() {
227     no_pkg=
228     for pkg in $PKG_LIST; do
229         [ ! `which $pkg` ] && no_pkg="$no_pkg $pkg"
230     done
231     for pkg in $no_pkg; do
232         echo "Error: Package [ $pkg ] is not found. Please install."
233     done
234     [ "$no_pkg" ] && exit 1
235 }
236
237 ### main
238 [ -d $DUMP_DIR ] || mkdir -p $DUMP_DIR
239
240 pkg_check
241 arg_check $*
242 echo "\n---------- test target ----------"
243 for testfile in $TEST_LIST; do echo $testfile; done
244
245 count=0
246 for testfile in $TEST_LIST; do
247     echo "\n---------- test [$testfile] start ----------"
248     set_env
249     mnfile_check $testfile && test_mn
250     case $? in
251         0) msg="finished : $RESULT" ;;
252         *) msg="skipped with error"; RESULT="skip" ;;
253     esac
254     eval RESULT_${count}=\$RESULT
255     eval REASON_${count}=\$REASON
256     count=`expr $count + 1`
257     num=`eval echo \\${num_$RESULT:-0}`
258     eval num_${RESULT}=`expr $num + 1`
259     [ "$RESULT" != "OK" ] && RTN=1
260     clean_mn
261     echo "\n---------- test [$testfile] $msg ----------"
262 done
263
264 # output summary results
265 echo "\n---------- test results ----------"
266 count=0
267 for testfile in $TEST_LIST; do
268     eval echo \$testfile : \$RESULT_${count} \$REASON_${count}
269     count=`expr $count + 1`
270 done
271 echo "----------------------------------"
272 echo "Ran $count tests. Result: ${num_OK:+OK=}$num_OK ${num_NG:+NG=}$num_NG ${num_skip:+skip=}$num_skip"
273
274 exit $RTN