-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4_mininet.py
217 lines (188 loc) · 6.95 KB
/
p4_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
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
# Copyright 2013-present Barefoot Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from mininet.net import Mininet
from mininet.node import Switch, Host
from mininet.log import setLogLevel, info
class P4Host(Host):
def config(self, **params):
r = super(Host, self).config(**params)
self.defaultIntf().rename("eth0")
for off in ["rx", "tx", "sg"]:
cmd = "/sbin/ethtool --offload eth0 %s off" % off
self.cmd(cmd)
# disable IPv6
self.cmd("sysctl -w net.ipv6.conf.all.disable_ipv6=1")
self.cmd("sysctl -w net.ipv6.conf.default.disable_ipv6=1")
self.cmd("sysctl -w net.ipv6.conf.lo.disable_ipv6=1")
return r
def describe(self):
print "**********"
print self.name
print "default interface: %s\t%s\t%s" % (
self.defaultIntf().name,
self.defaultIntf().IP(),
self.defaultIntf().MAC()
)
print "**********"
class P4Switch(Switch):
"""P4 virtual switch"""
device_id = 0
def __init__(self, name, sw_path=None, json_path=None, grpc_port=None,
thrift_port=None,
pcap_dump=False,
verbose=False,
device_id=None,
enable_debugger=False,
cpu_port=None,
**kwargs):
Switch.__init__(self, name, **kwargs)
assert (sw_path)
self.sw_path = sw_path
self.json_path = json_path
self.verbose = verbose
# logfile = 'p4s.%s.log' % self.name
# self.output = open(logfile, 'w')
# self.thrift_port = thrift_port
self.pcap_dump = pcap_dump
self.enable_debugger = enable_debugger
self.cpu_port = cpu_port
if device_id is not None:
self.device_id = device_id
P4Switch.device_id = max(P4Switch.device_id, device_id)
else:
self.device_id = P4Switch.device_id
P4Switch.device_id += 1
self.nanomsg = "ipc:///tmp/bm-%d-log.ipc" % self.device_id
@classmethod
def setup(cls):
pass
def start(self, controllers):
"Start up a new P4 switch"
print "Starting P4 switch", self.name
args = [self.sw_path]
# args.extend( ['--name', self.name] )
# args.extend( ['--dpid', self.dpid] )
for port, intf in self.intfs.items():
if not intf.IP():
args.extend(['-i', str(port) + "@" + intf.name])
if self.cpu_port:
args.extend(['-i', "64@" + self.cpu_port])
if self.pcap_dump:
args.append("--pcap")
# args.append("--log-console")
# args.append("--log-file s%s.log" % self.name)
# args.append("--log-level debug")
# args.append("--log-flush")
# args.append("--useFiles")
# if self.thrift_port:
# args.extend( ['--thrift-port', str(self.thrift_port)] )
# if self.nanomsg:
# args.extend( ['--nanolog', self.nanomsg] )
args.extend(['--device-id', str(self.device_id)])
P4Switch.device_id += 1
notificationAddr = 'ipc:///tmp/bmv2-' + str(self.device_id) + '-notifications.ipc'
args.extend(['--notifications-addr', str(notificationAddr)])
if self.json_path:
args.append(self.json_path)
else:
args.append("--no-p4")
if self.enable_debugger:
args.append("--debugger")
args.append("-- --enable-swap")
logfile = 'p4s.%s.log' % self.name
print ' '.join(args)
self.cmd(' '.join(args) + ' >' + logfile + ' 2>&1 &')
# self.cmd( ' '.join(args) + ' > /dev/null 2>&1 &' )
print "switch has been started"
def stop(self):
"Terminate IVS switch."
self.output.flush()
self.cmd('kill %' + self.sw_path)
self.cmd('wait')
self.deleteIntfs()
def attach(self, intf):
"Connect a data port"
assert (0)
def detach(self, intf):
"Disconnect a data port"
assert (0)
class P4GrpcSwitch(Switch):
"""P4 virtual switch"""
device_id = 0
def __init__(self, name, sw_path=None, json_path=None,
thrift_port=None,
grpc_port=None,
pcap_dump=False,
verbose=False,
device_id=None,
enable_debugger=False,
cpu_port=None,
**kwargs):
Switch.__init__(self, name, **kwargs)
assert (sw_path)
self.sw_path = sw_path
self.json_path = json_path
self.verbose = verbose
self.thrift_port = thrift_port
self.grpc_port = grpc_port
# self.pcap_dump = pcap_dump
self.enable_debugger = enable_debugger
self.cpu_port = cpu_port
if device_id is not None:
self.device_id = device_id
P4Switch.device_id = max(P4Switch.device_id, device_id)
# self.thrift_port = 9090 + device_id
else:
self.device_id = P4Switch.device_id
P4Switch.device_id += 1
@classmethod
def setup(cls):
pass
def start(self, controllers):
"Start up a new P4 switch"
print "Starting P4 switch", self.name
args = [self.sw_path]
for port, intf in self.intfs.items():
if not intf.IP():
args.extend(['-i', str(port) + "@" + intf.name])
if self.cpu_port:
args.extend(['-i', "64@" + self.cpu_port])
if self.thrift_port:
args.extend(['--thrift-port', str(self.thrift_port)])
args.extend(['--device-id', str(self.device_id)])
P4Switch.device_id += 1
if self.json_path:
args.append(self.json_path)
else:
args.append("--no-p4")
args.append("--log-flush --log-level trace --log-file %s.log" % self.name)
if self.grpc_port:
args.append("-- --grpc-server-addr 0.0.0.0:" + str(self.grpc_port) + " --cpu-port 64")
print ' '.join(args)
self.cmd(' '.join(args) + ' > %s.log 2>&1 &' % self.name)
# self.cmd( ' '.join(args) + ' > /dev/null 2>&1 &' )
print "switch has been started"
def stop(self):
"Terminate IVS switch."
# self.output.flush()
self.cmd('kill %' + self.sw_path)
self.cmd('wait')
self.deleteIntfs()
def attach(self, intf):
"Connect a data port"
assert (0)
def detach(self, intf):
"Disconnect a data port"
assert (0)