计算机网络 April 19, 2021

Mininet的使用方法

Words count 12k Reading time 11 mins. Read count 0

一、工具环境安装

这里集成了shell脚本进行环境的安装:

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
sudo apt-get update
sudo apt-get install curl
sudo apt-get install iproute2
sudo apt-get install iputils-ping
sudo apt-get install mininet
sudo apt-get install net-tools
sudo apt-get install openvswitch-switch
sudo apt-get install openvswitch-testcontroller
sudo apt install ryu-bin
sudo pip install mininet
sudo pip install numpy

安装好了后就可以网络拓扑的编程实现了,假设一个简单的网络拓扑如下:

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
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.node import RemoteController, OVSKernelSwitch, OVSSwitch, DefaultController
from mininet.log import setLogLevel
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from functools import partial
import time
from mininet.link import Link, TCLink
from flows_generator import generate_flows
# GLOBAL VARIABLES
experiment_duration = 180 # seconds
n_mice_flows = 45
n_elephant_flows = 5

log_dir = "."

def Mininet_Init():
net = Mininet()
# Creating nodes in the network
c0 = net.addController()
h1 = net.addHost('h1')
h2 = net.addHost('h2')
h11 = net.addHost('h11')
h22 = net.addHost('h22')
s1 = net.addSwitch('s1')
s2 = net.addSwitch('s2')
s3 = net.addSwitch('s3')
s4 = net.addSwitch('s4')
s5 = net.addSwitch('s5')
# Creating links between nodes in network
net.addLink(h1, s1)
net.addLink(h11, s1)
net.addLink(h2, s2)
net.addLink(h22, s2)

net.addLink(s1, s3)
net.addLink(s2, s3)
# net.addLink(s1, s4)
# net.addLink(s2, s4)
# net.addLink(s1, s5)
# net.addLink(s2, s3)
# net.addLink(s2, s4)
# net.addLink(s2, s5)
# configuration of IP address in interfaces
h1.setIP('10.0.0.1', 8)
h2.setIP('10.0.0.2', 8)
h11.setIP('10.0.0.3', 8)
h22.setIP('10.0.0.4', 8)
return net


def Test():
net = Mininet_Init()
net.start()

while True:
# if user enters CTRL + D then treat it as quit
try:
user_input = raw_input("GEN/CLI/QUIT: ")
except EOFError as error:
user_input = "QUIT"

if user_input.upper() == "GEN":
experiment_duration = int(raw_input("Experiment duration: "))
n_elephant_flows = int(raw_input("Numb of elephant flows: "))
n_mice_flows = int(raw_input("Numb of mice flows: "))

generate_flows(n_elephant_flows, n_mice_flows, experiment_duration, net, log_dir)

elif user_input.upper() == "CLI":
info("Running CLI...\n")
CLI(net)

elif user_input.upper() == "QUIT":
info("Terminating...\n")
info("Dumping host connections")

dumpNodeConnections(net.hosts)
print "Testing network connectivity"
net.stop()
break

else:
print("Command not found")

if __name__ == '__main__':
# Tell mininet to print useful information
setLogLevel('info')
Test()

其实就是linux下的组网技术再现而已,只不过用代码形式表现出来,而不是图形用户界面,运行效果如下:

image-20210419001102005

0%