Project

General

Profile

Simulate Network Latency ยป virtual-link.sh

automatically set up a network namespace with latency on a link to/from it - neels, 08/31/2021 01:35 PM

 
1
#!/bin/sh
2
#
3
# usage:
4
#
5
#  virtual-link.sh <netem-cmd> <namespace-name> <subnet-prefix>
6
#
7
# e.g.
8
#
9
#  virtual-link.sh "delay 200ms 50ms 50%" abis 10.9.8.
10
#
11
# (omit any number of trailing arguments for default values)
12

    
13
netem_cmd="${1:-delay 200ms 50ms 50%}"
14
ns="${2:-abis}"
15
link_in="${ns}-in"
16
link_out="${ns}-out"
17
subnet_prefix="${3:-10.9.8.}"
18
addr_out="${subnet_prefix}1"
19
addr_in="${subnet_prefix}2"
20
addr_mask_out="${addr_out}/24"
21
addr_mask_in="${addr_in}/24"
22

    
23
set -e -x
24

    
25
test $USER = root
26

    
27
ip netns add $ns
28
ip link add $link_in type veth peer name $link_out
29
ip link set $link_in netns $ns
30

    
31
ip link set $link_out up
32
ip addr add $addr_mask_out dev $link_out
33
ip a
34

    
35
ip netns exec abis  ip link set $link_in up
36
ip netns exec abis  ip link set lo up
37
ip netns exec abis  ip addr add $addr_mask_in dev $link_in
38
ip netns exec abis  ip a
39

    
40
tc qdisc add dev $link_out root handle 1:0 netem $netem_cmd
41
ip netns exec abis  tc qdisc add dev $link_in root handle 1:0 netem $netem_cmd
42

    
43
ping -c 3 $addr_in
44
ip netns exec abis  ping -c 3 $addr_out
45

    
46
set +x
47
echo To clean up:  netns delete $ns
    (1-1/1)
    Add picture from clipboard (Maximum size: 48.8 MB)