pktgen.sh 6.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#! /bin/bash

SB_BASE="$(readlink -f $(dirname ${BASH_SOURCE[0]})/../../..)"
RUN_DIR=$SB_BASE/experiments/out/pktgen/${1}h${2}b
#RUN_DIR=/tmp/simbricks/pktgen/${1}h${2}b
NUM_HOST=$1
ALL_PIDS=""
PKTGEN_PIDS=""
SWITCH_PIDS=""
# - inst num
# - pktgen bit rate
run_pktgen(){
    echo "starting host $1"
    PKTGEN_EXE=$SB_BASE/sims/net/pktgen/pktgen
    $PKTGEN_EXE -S 500 -E 500 -n $1 -h $RUN_DIR/eth.$1 -b $2 &
    pid=$!
    ALL_PIDS="$ALL_PIDS $pid"
    PKTGEN_PIDS="$PKTGEN_PIDS $pid"
    return $pid
}

# -num host connected
run_switch(){
    echo "Starting switch"
    SWITCH_EXE=$SB_BASE/sims/net/switch/net_switch
    args=""
    iface=0
    while [ $iface -lt $1 ]
    do
        args="$args -s $RUN_DIR/eth.$iface"
        ((iface++))
    done
    $SWITCH_EXE -S 500 -E 500 \
    $args > $RUN_DIR/log.switch &

    pid=$!
    ALL_PIDS="$ALL_PIDS $pid"
    return $pid
}

# - number of hosts
run_switch_dumbbell(){
    echo "Starting switch dumbbell"
    SWITCH_EXE=$SB_BASE/sims/net/switch/net_switch
    args_0=""
    args_1=""
    iface=0
    half=$(($1/2))
    while [ $iface -lt $half ]
    do
        args_0="$args_0 -s $RUN_DIR/eth.$iface"
        #((iface+=2))
        ((iface+=1))
    done

    #iface=1

    #num_inc=$(($1+1))
    #while [ $iface -lt $num_inc ]
    while [ $iface -lt $1 ]
    do
        args_1="$args_1 -s $RUN_DIR/eth.$iface"
        #((iface+=2))
        ((iface+=1))
    done
    $SWITCH_EXE -S 500 -E 500 \
    $args_0 -h $RUN_DIR/s0eth > $RUN_DIR/log.switch &

    pid=$!
    ALL_PIDS="$ALL_PIDS $pid"
    SWITCH_PIDS="$SWITCH_PIDS $pid"
    sleep 1

    $SWITCH_EXE -S 500 -E 500 \
    $args_1 -s $RUN_DIR/s0eth > $RUN_DIR/log.switch &
    pid=$!
    ALL_PIDS="$ALL_PIDS $pid"
    SWITCH_PIDS="$SWITCH_PIDS $pid"

    return $pid
}

# - number of hosts
# - number of middle switch
run_switch_chain(){
    echo "Starting switch chain"
    SWITCH_EXE=$SB_BASE/sims/net/switch/net_switch
    args_0=""
    args_1=""
    iface=0
    half=$(($1/2))
    while [ $iface -lt $half ]
    do
        args_0="$args_0 -s $RUN_DIR/eth.$iface"
        #((iface+=2))
        ((iface+=1))
    done

    #iface=1

    #num_inc=$(($1+1))
    #while [ $iface -lt $num_inc ]
    while [ $iface -lt $1 ]
    do
        args_1="$args_1 -s $RUN_DIR/eth.$iface"
        #((iface+=2))
        ((iface+=1))
    done

    nswitch=0
    nums_dec=$(($2-1))
    
    while [ $nswitch -lt $2 ]
    do
        pswitch=$(($nswitch-1))
        #the first 
        if [ $nswitch -eq 0 ]
        then
            $SWITCH_EXE -S 500 -E 500 \
            $args_0 -h $RUN_DIR/s0eth > $RUN_DIR/switch_${nswitch}.log &

            pid=$!
            ALL_PIDS="$ALL_PIDS $pid"
            SWITCH_PIDS="$SWITCH_PIDS $pid"
            sleep 1
        #the last
        elif [ $nswitch -eq $nums_dec ]      
        then
            $SWITCH_EXE -S 500 -E 500 \
            $args_1 -s $RUN_DIR/s${pswitch}eth > $RUN_DIR/switch_${nswitch}.log &
            pid=$!
            ALL_PIDS="$ALL_PIDS $pid"
            SWITCH_PIDS="$SWITCH_PIDS $pid"
        else
            $SWITCH_EXE -S 500 -E 500 \
            -s $RUN_DIR/s${pswitch}eth -h $RUN_DIR/s${nswitch}eth > $RUN_DIR/switch_${nswitch}.log &
            pid=$!
            ALL_PIDS="$ALL_PIDS $pid"
            SWITCH_PIDS="$SWITCH_PIDS $pid"
            sleep 1
        fi

        ((nswitch++))
    done

    return
}

# - num host connected per Tor switch
# - num of Tor switch
run_switch_tor(){
    echo "Starting ToR switch"
    SWITCH_EXE=$SB_BASE/sims/net/switch/net_switch
    per_sw=$(($1/$2))
    nswitch=0
    host_idx=0

    # Run Tor switches
    while [ $nswitch -lt $2 ]
    do
        args=""
        iface=0
        while [ $iface -lt $per_sw ]
        do
            args="$args -s $RUN_DIR/eth.$host_idx"
            ((iface++))
            ((host_idx++))
        done

        # add interface connecting to root switch
        args="$args -h $RUN_DIR/s.${nswitch}"
        ((nswitch++))
        $SWITCH_EXE -S 500 -E 500 \
        $args > $RUN_DIR/log.tor${nswitch} &

        pid=$!
        SWITCH_PIDS="$SWITCH_PIDS $pid"
        ALL_PIDS="$ALL_PIDS $pid"
    done

    sleep 2

    # Run root switch
    echo "Run root switch"
    args=""
    iface=0
    while [ $iface -lt $nswitch ]
    do
        args="$args -s $RUN_DIR/s.$iface"
        ((iface++))
    done
    
    $SWITCH_EXE -S 500 -E 500 \
    $args > $RUN_DIR/log.rswitch &

    pid=$!
    SWITCH_PIDS="$SWITCH_PIDS $pid"
    ALL_PIDS="$ALL_PIDS $pid"
}

# - number of hosts
# - number of layers should >= 2
run_switch_hierarchy(){
    echo "Starting switch hierarchy"
    SWITCH_EXE=$SB_BASE/sims/net/switch/net_switch
    
    
    layer=1
    #leave switch
    iface=0
    
    while [ $iface -lt $1 ]
    do
        $SWITCH_EXE -S 500 -E 500 \
        -h $RUN_DIR/s${layer}.$iface -s $RUN_DIR/eth.${iface}> $RUN_DIR/s${layer}.${iface}.log &
            
        pid=$!
        ALL_PIDS="$ALL_PIDS $pid"
        SWITCH_PIDS="$SWITCH_PIDS $pid"
        ((iface++))
    done 

    ((layer++))
    sleep 2
    #node switch
    while [ $layer -lt $2 ]
    do
        iface=0
        layer_dec=$(($layer-1))
        while [ $iface -lt $1 ]
        do
            $SWITCH_EXE -S 500 -E 500 \
            -s $RUN_DIR/s${layer_dec}.$iface -h  $RUN_DIR/s${layer}.$iface > $RUN_DIR/s${layer}.${iface}.log &
            
            pid=$!
            ALL_PIDS="$ALL_PIDS $pid"
            SWITCH_PIDS="$SWITCH_PIDS $pid"
            ((iface++))
        done

        ((layer++))
        sleep 2
    done
    sleep 2
    #root switch
    args=""
    iface=0
    layer_dec=$(($layer-1))
    while [ $iface -lt $1 ]
    do
        args="$args -s $RUN_DIR/s${layer_dec}.$iface"
        ((iface++))
    done
    $SWITCH_EXE -S 500 -E 500 \
    $args > $RUN_DIR/root_switch.log &

    pid=$!
    ALL_PIDS="$ALL_PIDS $pid"
    SWITCH_PIDS="$SWITCH_PIDS $pid"
}

cleanup() {
    echo "Cleaning up"

    for p in $ALL_PIDS ; do
        kill -KILL $p &>/dev/null
    done
    date
}

sighandler(){
    echo "caught Interrup, aborting..."
    cleanup
    date
    exit 1
}

trap "sighandler" SIGINT


echo -n "start: "
date +%s
rm -rf $RUN_DIR
mkdir -p $RUN_DIR
r=0
while [ $r -lt $1 ]
do
    run_pktgen $r $2
    ((r++))
done

sleep 3
#run_switch $1
#SWITCH_PID=$!
#run_switch_dumbbell $1
#run_switch_chain $1 $2

if [ "$#" -lt 3 ]; then
    echo "usage: ./pktgen.sh [num_host] [bit_rate] [opt] [net_config] "
fi

if [ "$#" -eq 4 ]; then
    if [ $4 == "run_switch_hierarchy" ]; then
        echo "run switch hierarchy"
        run_switch_hierarchy $1 $3
    elif [ $4 == "run_switch_tor" ]; then
        echo "run switch tor $3"
        run_switch_tor $1 $3
    else
        echo "no matching config"
        cleanup
        exit 0
    fi
elif [ $3 == "run_switch" ]; then
    run_switch $1
elif [ $3 == "run_switch_dumbbell" ]; then
    run_switch_dumbbell $1
else    
    echo "no argument match"
    cleanup
    exit 0
fi

for p in $PKTGEN_PIDS ; do
    wait $p
done

echo "Pktgen Done, kill switch"
#kill -9 $SWITCH_PID

for p in $SWITCH_PIDS ; do
    kill -9 $p
done
echo -n "end: "
date +%s