Mahimahiのmm-onoff でネットワークの接続・切断を繰り返す状態のエミュレーション

紹介

Mahimahiとはネットワークエミュレーターです。遅延、帯域設定、ロス設定などができます。特徴として、「より正確に、マルチサーバーの環境を再現できる」、「ネットワークトラフィックを独立した環境に閉じ込めることができるので、複数の異なるインスタンスを同時に走らせることができる」、「composable (構成可能)なシェル、使いやすく拡張性もある」といった特徴を持っています。

mahimahi.mit.edu

mm-onoff はMahimahiに含まれる、ネットワークが接続・切断を繰り返すような状態をエミュレーションするためのコマンドです。

mm-onoff コマンドの使い方は、manで確認すると以下のようになっています。

mm-onoff uplink|downlink mean-on-time mean-off-time [command...]

The uplink or downlink will be intermittent and will switch between connected and disconnected states according to a Poisson point process with specified average durations spent "on" and "off".

uplinkまたはdownlinkに対して、ポアソン分布に従ったon(接続されている状態)とoff(切断されている状態)の平均時間を指定できます。

使い方

さっそく使ってみます。以下のようにコマンドを打ちます。

$ mm-onoff uplink 10 5
[onoff (up) on=10s off=5s] $ 

Mahimahiの特徴として、シェルを起動する形になるため、コマンドを実行すると、[onoff (up) on=10s off=5s] というのが表示されます。これは、onの状態が平均10秒続いて、offの状態が平均5秒続くということを表しています。

この状態から、pingを1秒間隔で30秒間投げてみます。

[onoff (up) on=10s off=5s] $ ping 8.8.8.8 -i 1 -c 30
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=8 ttl=53 time=7.33 ms             
64 bytes from 8.8.8.8: icmp_seq=9 ttl=53 time=10.5 ms
64 bytes from 8.8.8.8: icmp_seq=10 ttl=53 time=7.83 ms
64 bytes from 8.8.8.8: icmp_seq=11 ttl=53 time=6.83 ms
64 bytes from 8.8.8.8: icmp_seq=12 ttl=53 time=17.9 ms
64 bytes from 8.8.8.8: icmp_seq=13 ttl=53 time=7.17 ms
64 bytes from 8.8.8.8: icmp_seq=14 ttl=53 time=8.14 ms
64 bytes from 8.8.8.8: icmp_seq=15 ttl=53 time=6.92 ms
64 bytes from 8.8.8.8: icmp_seq=16 ttl=53 time=6.73 ms
64 bytes from 8.8.8.8: icmp_seq=17 ttl=53 time=6.69 ms
64 bytes from 8.8.8.8: icmp_seq=18 ttl=53 time=7.85 ms
64 bytes from 8.8.8.8: icmp_seq=19 ttl=53 time=6.60 ms
64 bytes from 8.8.8.8: icmp_seq=20 ttl=53 time=5.96 ms
64 bytes from 8.8.8.8: icmp_seq=21 ttl=53 time=7.27 ms
64 bytes from 8.8.8.8: icmp_seq=22 ttl=53 time=7.19 ms
64 bytes from 8.8.8.8: icmp_seq=23 ttl=53 time=6.20 ms
64 bytes from 8.8.8.8: icmp_seq=24 ttl=53 time=6.33 ms
64 bytes from 8.8.8.8: icmp_seq=25 ttl=53 time=7.16 ms
64 bytes from 8.8.8.8: icmp_seq=26 ttl=53 time=7.10 ms

--- 8.8.8.8 ping statistics ---
30 packets transmitted, 19 received, 36% packet loss, time 29352ms
rtt min/avg/max/mdev = 5.963/7.782/17.923/2.577 ms

pingの結果から、0~7、24~30のパケットがロスしていることが分かります。このタイミングで切断状態になっていたことが分かります。

同様に、wiresharkを使ってpingを実行している状態をキャプチャしてみます。

mm-onoffを起動しているシェル内からwiresharkを立ち上げる場合は、ingressというインターフェースを選択します。

f:id:neko--suki:20191121225337p:plain

別のシェルからwiresharkを立ち上げる場合は、onoff-xxxx というインターフェースを選択します。mahimahiではコマンドを起動するとコマンドに応じたネットワークインターフェースが作られるようです。mm-onoffの場合は、onoff-xxxx という名前のインターフェースが生成されます。

f:id:neko--suki:20191121230832p:plain

まずは、内部で立ち上げたwiresharkの結果です。開始12秒から16秒の場所で、Echo requestへのresponseがないと表示されています。

f:id:neko--suki:20191121231102p:plain

外部で立ち上げたwiresharkだと、12秒から16秒のところはecho requestも存在していません。

f:id:neko--suki:20191121231140p:plain

ちなみに、$ mm-onoff uplink 10 5 ping -c 30 8.8.8.8 のようにコマンドを実行することもできます。

以上紹介したようにmm-onoff を使うと、ネットワークが接続・切断状態を繰り返す状態をエミュレーションできました。on/offの時間はランダムなので完全に再現できるわけではないのが少し使いにくいかもしれません。