-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
347 lines (288 loc) · 19.8 KB
/
index.html
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
336
337
338
339
340
341
342
343
344
345
346
347
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8"/>
<title>blog.mortenvp.com</title>
<meta name="author" content="Morten V. Pedersen">
<link rel="stylesheet" href="http://blog.mortenvp.com/theme/css/main.css" type="text/css" />
<link href="http://blog.mortenvp.com/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="blog.mortenvp.com Atom Feed" />
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" type="text/javascript"></script>
<script type="text/javascript">
init_mathjax = function()
{
if (window.MathJax)
{
// MathJax loaded
MathJax.Hub.Config(
{
tex2jax:
{
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
},
displayAlign: 'left', // Change this to 'center' to center equations.
"HTML-CSS":
{
scale: 100,
styles: {'.MathJax_Display': {"margin": 0}, '.MathJax':{"font-size": "100%", "color": "black"}}
}
styles: {'.MathJax_Preview':{"font-size": "100%", "color": "black"}}
});
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
}
}
init_mathjax();
</script>
</head>
<body>
<div class="container">
<header role="banner">
<div class="feeds">
<a href="http://blog.mortenvp.com/feeds/all.atom.xml" rel="alternate"><img src="http://blog.mortenvp.com/theme/images/icons/feed-32px.png" alt="atom feed"/></a>
</div>
<a href="http://blog.mortenvp.com" class="title">blog.mortenvp.com</a>
</header>
<div class="wrapper">
<div role="main" class="content">
<article>
<h1><a href="http://blog.mortenvp.com/emulating-networks-with-mininet.html">Emulating Networks with Mininet</a></h1>
<p>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Some time ago I came across <a href="http://mininet.org/">Mininet</a> which allows us setup and emulate networks on a single machine. Mininet uses the <a href="http://en.wikipedia.org/wiki/LXC">Linux container</a> technology (similar to <a href="http://docker.com">Docker</a>) to run multiple lightweight isolated Linux environments on a single Linux host. I never had time to play with it, but recently I wanted to experiment with a new transport protocol and see how it reacted to lossy links and Mininet seemed like an good choice for emulating the network.</p>
<p>Mininet seems quite popular for people how wants to emulate and experiment with newer networking concepts such as SDN (Software Defined Networks). Beause the overhead of running Linux containers is very low, it allows us to emulate large networks with many connected hosts on a single computer (unfeasible with virtual machines). But, it can also be used to emulated simple networks like point-to-point links, with some loss and delay characteristics. Which is what we will do here. Another great strength of Mininet is that it allows us to run existing applications over these networks. So for example on one emulated host I can open the Firefox browser and on another host a webserver. I can even connect hosts on my emulated network to the real network and let them speak to services on the Internet. How great is that!?</p>
<p>Let get this thing up and running.</p>
<h2 id="installing-mininet">Installing mininet</h2>
<p>So the first thing we need to do is to install Mininet. There are a copule of options to choose from the easiest way it to use the prepared virtual machine. You can find additional instructions on how to set it up on the Mininet webpage <a href="http://mininet.org/vm-setup-notes/">here</a>.</p>
<p>Note: The Mininet setup guide suggests that you add a "Host-only Adapter", when I initially tried this, it gave me a "no host only network adapter selected" error. This was resolve following the suggestions described <a href="no%20host%20only%20network%20adapter%20selected">here</a>.</p>
<p>For some reason after booting the Mininet virtual machine the <em>host only</em> network was not brought up, the commands to make it accessible (run the following commands in the virtual machine):</p>
<pre><code>sudo ip link set dev eth1 up
sudo dhclient eth1
ip link show</code></pre>
<p>First we bring up the interface, then ask for an IP using DHCP and finally we show the available interfaces and their IPs.</p>
<p>Alternatively you can add the following to <code>/etc/network/interfaces</code> inside the virtual machine:</p>
<pre><code>auto eth1
iface eth1 inet dhcp</code></pre>
<p>Now I could ssh from my machine to the Mininet virtual machine using:</p>
<pre><code>ssh mininet@192.168.56.101</code></pre>
<p>To make things a bit easier I add the following to <code>.ssh/config</code>:</p>
<pre><code>Host mininet
User mininet
HostName 192.168.56.101</code></pre>
<p>Now I can login using <code>ssh mininet</code>, see the bottom of the <a href="http://mininet.org/vm-setup-notes/">Mininet VM setup notes</a> guide to see how to setup password less login.</p>
<h3 id="shared-folders">Shared folders</h3>
<p>To make it easier to move files between the virtual machine and the host computer you can setup a shared folder. In Virtualbox go to the settings for the Mininet virtual machine and you should be able to select a host folder that you want to share in the virtual machine. In my case is just created a folder called <code>vm_mininet</code> in my home directory.</p>
<p>To access shared folders inside the virtual machine we need to install the Virtualbox Guest Additions. Fortunately there is a package for that, so inside the Mininet virtual machine run:</p>
<pre><code>sudo apt-get install virtualbox-guest-utils</code></pre>
<p>As a final step you need to add the <code>mininet</code> user to the <code>vboxsf</code> group to have access to the folder:</p>
<pre><code>usermod -aG vboxsf mininet</code></pre>
<p>Rebooting the virtual machine, and my shared folder shows up under <code>/media/sf_vm_mininet</code>.</p>
<h2 id="creating-the-network">Creating the network</h2>
<p>Now that we have the Mininet virtual machine up and running we can start to emulate some networks. One of the really cool things about Mininet is how easy it is to define network topologies using Python scripts. As an example lets try to create a network with two hosts and ping from one to the other.</p>
<p>The script for setting this up is available <a href="https://cdn.rawgit.com/mortenvp/mortenvp-pelican/master/content/notebooks/emulating_networks_with_mininet/simple_ping.py">here</a>. The <code>%%file simple_ping.py</code> is an iPython cell magic command that will save the content of the cell to a file called <code>simple_ping.py</code>, so that we later can runn the script in our Mininet virtual machine.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In [8]:
</div>
<div class="input_area box-flex1">
<div class="highlight-ipynb"><pre class="ipynb"><span class="o">%%</span><span class="k">file</span> <span class="n">simple_ping</span><span class="o">.</span><span class="n">py</span>
<span class="c">#!/usr/bin/python</span>
<span class="sd">"""</span>
<span class="sd">Simple example of point-to-point link running ping</span>
<span class="sd">"""</span>
<span class="kn">from</span> <span class="nn">mininet.topo</span> <span class="kn">import</span> <span class="n">Topo</span>
<span class="kn">from</span> <span class="nn">mininet.net</span> <span class="kn">import</span> <span class="n">Mininet</span>
<span class="kn">from</span> <span class="nn">mininet.node</span> <span class="kn">import</span> <span class="n">OVSController</span>
<span class="kn">from</span> <span class="nn">mininet.link</span> <span class="kn">import</span> <span class="n">TCLink</span>
<span class="kn">from</span> <span class="nn">mininet.util</span> <span class="kn">import</span> <span class="n">pmonitor</span>
<span class="kn">from</span> <span class="nn">mininet.log</span> <span class="kn">import</span> <span class="n">setLogLevel</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">setLogLevel</span><span class="p">(</span><span class="s">'info'</span><span class="p">)</span>
<span class="c"># Build the topology we want to play with:</span>
<span class="c">#</span>
<span class="c"># +-----------------------+</span>
<span class="c"># | 10Mbit/s link |</span>
<span class="c"># | 5ms delay |</span>
<span class="c"># | 10% packet loss |</span>
<span class="c"># | |</span>
<span class="c"># +-----------+-----------+</span>
<span class="c"># |</span>
<span class="c"># |</span>
<span class="c"># |</span>
<span class="c"># |</span>
<span class="c"># +-------------+ v +-------------+</span>
<span class="c"># | | | |</span>
<span class="c"># | host 1 (h1) +------------------+ host 2 (h2) |</span>
<span class="c"># | | | |</span>
<span class="c"># +-------------+ +-------------+</span>
<span class="c"># </span>
<span class="n">topo</span> <span class="o">=</span> <span class="n">Topo</span><span class="p">()</span>
<span class="n">topo</span><span class="o">.</span><span class="n">addHost</span><span class="p">(</span><span class="s">'h1'</span><span class="p">)</span>
<span class="n">topo</span><span class="o">.</span><span class="n">addHost</span><span class="p">(</span><span class="s">'h2'</span><span class="p">)</span>
<span class="n">topo</span><span class="o">.</span><span class="n">addLink</span><span class="p">(</span><span class="s">'h1'</span><span class="p">,</span><span class="s">'h2'</span><span class="p">,</span> <span class="n">bw</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="s">'5ms'</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="c"># The TCLink is needed for use to set the bandwidth, delay and loss</span>
<span class="c"># constraints on the link</span>
<span class="c">#</span>
<span class="c"># waitConnected</span>
<span class="n">net</span> <span class="o">=</span> <span class="n">Mininet</span><span class="p">(</span><span class="n">topo</span><span class="o">=</span><span class="n">topo</span><span class="p">,</span>
<span class="n">link</span><span class="o">=</span><span class="n">TCLink</span><span class="p">,</span>
<span class="n">waitConnected</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">net</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="n">h1</span><span class="p">,</span> <span class="n">h2</span> <span class="o">=</span> <span class="n">net</span><span class="o">.</span><span class="n">getNodeByName</span><span class="p">(</span><span class="s">'h1'</span><span class="p">,</span> <span class="s">'h2'</span><span class="p">)</span>
<span class="c"># Will open the two ping processes on both hosts and read the output</span>
<span class="n">popens</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">popens</span><span class="p">[</span><span class="n">h1</span><span class="p">]</span> <span class="o">=</span> <span class="n">h1</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="s">'ping -c10 {}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">h2</span><span class="o">.</span><span class="n">IP</span><span class="p">()))</span>
<span class="n">popens</span><span class="p">[</span><span class="n">h2</span><span class="p">]</span> <span class="o">=</span> <span class="n">h2</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="s">'ping -c10 {}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">h1</span><span class="o">.</span><span class="n">IP</span><span class="p">()))</span>
<span class="k">for</span> <span class="n">host</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">pmonitor</span><span class="p">(</span><span class="n">popens</span><span class="p">):</span>
<span class="k">if</span> <span class="n">host</span><span class="p">:</span>
<span class="c"># Output each line written as "<hX>: some output", where X</span>
<span class="c"># will be replaced by the host number i.e. 1 or 2.</span>
<span class="k">print</span><span class="p">(</span><span class="s">"<{}>: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">host</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()))</span>
<span class="n">net</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="box-flex1 output_subarea output_stream output_stdout">
<pre class="ipynb">Overwriting simple_ping.py
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>To run the above network simply copy the <code>simple_ping.py</code> file to the Mininet virtual machine and run it (e.g. using the shared folder):</p>
<pre><code>sudo ./simple_ping.py</code></pre>
<p>The script should now generate a bunch of output, giving you diagnostics about the network and output from the two ping processes.</p>
<pre><code>mininet@mininet-vm:/media/sf_vm_mininet$ sudo ./simple_ping.py
*** Creating network
*** Adding controller
*** Adding hosts:
h1 h2
*** Adding switches:
*** Adding links:
(10.00Mbit 5ms delay 10% loss) (10.00Mbit 5ms delay 10% loss) (h1, h2)
*** Configuring hosts
h1 h2
*** Starting controller
c0
*** Starting 0 switches
*** Waiting for switches to connect
<h2>: PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
<h2>: 64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=12.5 ms
<h1>: PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
<h1>: 64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=17.8 ms
<h1>: 64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=10.4 ms
<h2>: 64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=10.2 ms
<h1>: 64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=10.2 ms
<h2>: 64 bytes from 10.0.0.1: icmp_seq=3 ttl=64 time=10.2 ms
<h1>: 64 bytes from 10.0.0.2: icmp_seq=4 ttl=64 time=10.2 ms
<h2>: 64 bytes from 10.0.0.1: icmp_seq=4 ttl=64 time=10.2 ms
<h2>: 64 bytes from 10.0.0.1: icmp_seq=5 ttl=64 time=10.1 ms
<h2>: 64 bytes from 10.0.0.1: icmp_seq=6 ttl=64 time=10.2 ms
<h1>: 64 bytes from 10.0.0.2: icmp_seq=6 ttl=64 time=10.2 ms
<h2>: 64 bytes from 10.0.0.1: icmp_seq=7 ttl=64 time=10.1 ms
<h2>: 64 bytes from 10.0.0.1: icmp_seq=8 ttl=64 time=10.1 ms
<h1>: 64 bytes from 10.0.0.2: icmp_seq=8 ttl=64 time=10.1 ms
<h2>: 64 bytes from 10.0.0.1: icmp_seq=9 ttl=64 time=10.1 ms
<h1>: 64 bytes from 10.0.0.2: icmp_seq=9 ttl=64 time=11.0 ms
<h2>: 64 bytes from 10.0.0.1: icmp_seq=10 ttl=64 time=10.1 ms
<h2>:
<h2>: --- 10.0.0.1 ping statistics ---
<h2>: 10 packets transmitted, 10 received, 0% packet loss, time 9014ms
<h2>: rtt min/avg/max/mdev = 10.139/10.442/12.594/0.725 ms
<h1>: 64 bytes from 10.0.0.2: icmp_seq=10 ttl=64 time=10.1 ms
<h1>:
<h1>: --- 10.0.0.2 ping statistics ---
<h1>: 10 packets transmitted, 8 received, 20% packet loss, time 9024ms
<h1>: rtt min/avg/max/mdev = 10.144/11.294/17.819/2.484 ms
*** Stopping 1 controllers
c0
*** Stopping 0 switches
*** Stopping 1 links
*** Stopping 2 hosts
h1 h2
*** Done</code></pre>
<p>Pretty cool right?</p>
<p>The code was tested with Mininet version <code>2.2.0</code>, to see which version you have run:</p>
<pre><code>>>> import mininet
>>> print(mininet.net.VERSION)
2.2.0</code></pre>
<p>For more examples see the official <a href="https://github.com/mininet/mininet/tree/master/examples">Mininet examples</a>. Also you can find lots more information on the API etc. on the Mininet pages.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><em>This blog post was written entirely in the IPython Notebook. The full notebook can be downloaded <a href="https://cdn.rawgit.com/mortenvp/mortenvp-pelican/master/content/notebooks/emulating_networks_with_mininet/emulating_networks_with_mininet.ipynb">here</a>, or viewed statically <a href="http://nbviewer.ipython.org/url/github.com/mortenvp/mortenvp-pelican/blob/master/content/notebooks/emulating_networks_with_mininet/emulating_networks_with_mininet.ipynb">here</a>.</em></p>
</div>
</div>
</div></p>
</article>
<hr />
<div>
<h3>Last posts</h3>
<ol class="archive">
<li>
<a href="http://blog.mortenvp.com/creating-slides-using-the-ipython-notebook.html" rel="bookmark" title="Permalink to Creating slides using the iPython notebook">Creating slides using the iPython notebook</a>
<time datetime="2015-03-03T12:43:00" pubdate>Tue 03 March 2015</time>
</a> </li>
<li>
<a href="http://blog.mortenvp.com/creating-a-pelican-powered-ipython-notebook-blog.html" rel="bookmark" title="Permalink to Creating a Pelican powered iPython notebook blog">Creating a Pelican powered iPython notebook blog</a>
<time datetime="2015-02-26T15:00:00" pubdate>Thu 26 February 2015</time>
</a> </li>
</ol>
</div>
</div>
<div class="sidebar">
<div class="sidebar-container" >
<nav>
<h2>Categories</h2>
<ul>
<li ><a href="http://blog.mortenvp.com/category/misc.html">misc</a></li>
</ul>
</nav>
<aside>
<h2>Social</h2>
<ul class="social">
<li><a href="#">You can add links in your config file</a><i></i></li>
<li><a href="#">Another social link</a><i></i></li>
</ul>
</aside>
<aside>
<h2>Blogroll</h2>
<ul>
<li><a href="http://getpelican.com/">Pelican</a></li>
<li><a href="http://python.org/">Python.org</a></li>
<li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
<li><a href="#">You can modify those links in your config file</a></li>
</ul>
</aside>
</div>
</div>
</div>
<footer>
<p role="contentinfo">
Morten V. Pedersen - Proudly powered by <a href="http://alexis.notmyidea.org/pelican/">pelican</a>. Theme <a href="https://github.com/fle/pelican-sober">pelican-sober</a>.
</p>
</footer>
</div>
</body>
</html>