-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_mininet.py
90 lines (73 loc) · 3.12 KB
/
run_mininet.py
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
from mininet.net import Mininet
from mininet.topo import Topo
from mininet.log import setLogLevel, info
from mininet.cli import CLI
from mininet.link import TCLink
from p4_mininet import P4Switch, P4Host, P4GrpcSwitch
#from p4runtime_switch import P4RuntimeSwitch
import random
import argparse
from time import sleep
import subprocess
import sys
import os
import psutil
parser = argparse.ArgumentParser(description='Mininet demo')
#parser.add_argument('--thrift-port', help='Thrift server port for table updates', type=int, action="store", default=9090)
parser.add_argument('--num-hosts', help='Number of hosts to connect to switch', type=int, action="store", default=2)
parser.add_argument('--p4-file', help='Path to P4 file', type=str, action="store", required=False)
args = parser.parse_args()
def get_all_virtual_interfaces():
try:
return subprocess.check_output(['ip addr | grep s.-eth. | cut -d\':\' -f2 | cut -d\'@\' -f1'], shell=True).split('\n')
except subprocess.CalledProcessError as e:
print_error('Cannot retrieve interfaces.')
print_error(e)
return ''
class SingleSwitchTopo(Topo):
"Single switch connected to n (< 256) hosts."
def __init__(self, sw_path, json_path, n, **opts):
# Initialize topology and default options
Topo.__init__(self, **opts)
switch = self.addSwitch('s0', sw_path = sw_path, json_path = json_path, grpc_port = 50051, device_id = 1)
for h in xrange(n):
host = self.addHost('h%d' % (h + 1), ip = "10.10.10.%d/16" % (h + 1), mac = '00:04:00:00:00:%02x' %h)
self.addLink(host, switch)
server = self.addHost('s1', ip = "10.10.3.3/16", mac = '00:00:01:01:01:01')
# self.addLink('h1','h2')
# self.addLink('h2','s1')
# self.addLink('s1','h1')
self.addLink(server, switch)
def main():
num_hosts = int(args.num_hosts)
result = os.system("p4c --target bmv2 --arch v1model --p4runtime-files firmeware.p4info.txt "+ args.p4_file)
p4_file = args.p4_file.split('/')[-1]
json_file = p4_file.split('.')[0] + ".json"
# json_file = "/home/hpdn/Downloads/p4-researching-master/src/fundamental/learning-switch/basic_tutorial_switch.json"
topo = SingleSwitchTopo("simple_switch_grpc",
json_file,
num_hosts)
net = Mininet(topo = topo,
host = P4Host,
switch = P4GrpcSwitch,
link = TCLink,
controller = None)
net.start()
interfaces = get_all_virtual_interfaces()
for i in interfaces:
if i!="":
os.system("ip link set {} mtu 1600 > /dev/null".format(i))
os.system('ethtool --offload {} rx off tx off > /dev/null'.format(i))
net.staticArp()
if result !=0:
print "Error while compiling!"
exit()
switch_running="simple_switch_grpc" in (p.name() for p in psutil.process_iter())
if switch_running==False:
print "The switch didnt start correctly! Check the path to your P4 file!!"
exit()
print "Starting mininet!"
CLI(net)
if __name__ == '__main__':
setLogLevel( 'info' )
main()