forked from stratum/stratum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stratum.py
297 lines (247 loc) · 9.19 KB
/
stratum.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
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
# coding=utf-8
# Copyright 2018-present Open Networking Foundation
# SPDX-License-Identifier: Apache-2.0
'''
This module contains a switch class for Mininet: StratumBmv2Switch
Prerequisites
-------------
1. Docker- mininet+stratum_bmv2 image:
$ cd stratum
$ docker build -t <some tag> -f tools/mininet/Dockerfile .
Usage
-----
From withing the Docker container, you can run Mininet using the following:
$ mn --custom /root/stratum.py --switch stratum-bmv2 --controller none
Advanced Usage
--------------
You can use this class in a Mininet topology script by including:
from stratum import ONOSStratumBmv2Switch
You will probably need to update your Python path. From within the Docker image:
PYTHONPATH=$PYTHONPATH:/root ./<your script>.py
Notes
-----
This code has been adapted from the ONOSBmv2Switch class defined in the ONOS project
(tools/dev/mininet/bmv2.py).
'''
import json
import multiprocessing
import os
import socket
import threading
import time
from mininet.log import warn
from mininet.node import Switch, Host
DEFAULT_NODE_ID = 1
DEFAULT_CPU_PORT = 255
DEFAULT_PIPECONF = "org.onosproject.pipelines.basic"
STRATUM_BMV2 = 'stratum_bmv2'
STRATUM_INIT_PIPELINE = '/root/dummy.json'
MAX_CONTROLLERS_PER_NODE = 10
BMV2_LOG_LINES = 5
def writeToFile(path, value):
with open(path, "w") as f:
f.write(str(value))
def pickUnusedPort():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 0))
addr, port = s.getsockname()
s.close()
return port
def watchdog(sw):
try:
writeToFile(sw.keepaliveFile,
"Remove this file to terminate %s" % sw.name)
while True:
if StratumBmv2Switch.mininet_exception == 1 \
or not os.path.isfile(sw.keepaliveFile):
sw.stop()
return
if sw.stopped:
return
if sw.bmv2popen.poll() is None:
# All good, no return code, still running.
time.sleep(1)
else:
warn("\n*** WARN: switch %s died ☠️ \n" % sw.name)
sw.printLog()
print("-" * 80) + "\n"
# Close log file, set as stopped etc.
sw.stop()
return
except Exception as e:
warn("*** ERROR: " + e.message)
sw.stop()
class StratumBmv2Switch(Switch):
# Shared value used to notify to all instances of this class that a Mininet
# exception occurred. Mininet exception handling doesn't call the stop()
# method, so the mn process would hang after clean-up since Bmv2 would still
# be running.
mininet_exception = multiprocessing.Value('i', 0)
nextGrpcPort = 50001
def __init__(self, name, json=STRATUM_INIT_PIPELINE, loglevel="warn",
cpuport=DEFAULT_CPU_PORT, pipeconf=DEFAULT_PIPECONF,
onosdevid=None,
**kwargs):
Switch.__init__(self, name, **kwargs)
self.grpcPort = StratumBmv2Switch.nextGrpcPort
StratumBmv2Switch.nextGrpcPort += 1
self.cpuPort = cpuport
self.json = json
self.loglevel = loglevel
self.tmpDir = '/tmp/%s' % self.name
self.logfile = '%s/stratum_bmv2.log' % self.tmpDir
self.netcfgFile = '%s/onos-netcfg.json' % self.tmpDir
self.chassisConfigFile = '%s/chassis-config.txt' % self.tmpDir
self.pipeconfId = pipeconf
self.longitude = kwargs['longitude'] if 'longitude' in kwargs else None
self.latitude = kwargs['latitude'] if 'latitude' in kwargs else None
if onosdevid is not None and len(onosdevid) > 0:
self.onosDeviceId = onosdevid
else:
# The "device:" prefix is required by ONOS.
self.onosDeviceId = "device:%s" % self.name
self.nodeId = DEFAULT_NODE_ID
self.logfd = None
self.bmv2popen = None
self.stopped = True
# In case of exceptions, mininet removes *.out files from /tmp. We use
# this as a signal to terminate the switch instance (if active).
self.keepaliveFile = '/tmp/%s-watchdog.out' % self.name
# Remove files from previous executions
self.cleanupTmpFiles()
os.mkdir(self.tmpDir)
def getOnosNetcfg(self):
basicCfg = {
"managementAddress": "grpc://localhost:%d?device_id=%d" % (
self.grpcPort, self.nodeId),
"driver": "stratum-bmv2",
"pipeconf": self.pipeconfId
}
if self.longitude and self.latitude:
basicCfg["longitude"] = self.longitude
basicCfg["latitude"] = self.latitude
netcfg = {
"devices": {
self.onosDeviceId: {
"basic": basicCfg
}
}
}
return netcfg
def getChassisConfig(self):
config = """description: "stratum_bmv2 {name}"
chassis {{
platform: PLT_P4_SOFT_SWITCH
name: "{name}"
}}
nodes {{
id: {nodeId}
name: "{name} node {nodeId}"
slot: 1
index: 1
}}\n""".format(name=self.name, nodeId=self.nodeId)
intf_number = 1
for intf_name in self.intfNames():
if intf_name == 'lo':
continue
config = config + """singleton_ports {{
id: {intfNumber}
name: "{intfName}"
slot: 1
port: {intfNumber}
channel: 1
speed_bps: 10000000000
config_params {{
admin_state: ADMIN_STATE_ENABLED
}}
node: {nodeId}
}}\n""".format(intfName=intf_name, intfNumber=intf_number, nodeId=self.nodeId)
intf_number += 1
return config
def start(self, controllers):
if not self.stopped:
warn("*** %s is already running!\n" % self.name)
return
writeToFile("%s/grpc-port.txt" % self.tmpDir, self.grpcPort)
with open(self.chassisConfigFile, 'w') as fp:
fp.write(self.getChassisConfig())
with open(self.netcfgFile, 'w') as fp:
json.dump(self.getOnosNetcfg(), fp, indent=2)
args = [
STRATUM_BMV2,
'-device_id=%d' % self.nodeId,
'-chassis_config_file=%s' % self.chassisConfigFile,
'-forwarding_pipeline_configs_file=%s/pipe.txt' % self.tmpDir,
'-persistent_config_dir=%s' % self.tmpDir,
'-initial_pipeline=%s' % self.json,
'-cpu_port=%s' % self.cpuPort,
'-external_stratum_urls=0.0.0.0:%d' % self.grpcPort,
'-local_stratum_url=localhost:%d' % pickUnusedPort(),
'-max_num_controllers_per_node=%d' % MAX_CONTROLLERS_PER_NODE,
'-write_req_log_file=%s/write-reqs.txt' % self.tmpDir,
'-logtostderr=true',
'-bmv2_log_level=%s' % self.loglevel,
]
cmd_string = " ".join(args)
try:
# Write cmd_string to log for debugging.
self.logfd = open(self.logfile, "w")
self.logfd.write(cmd_string + "\n\n" + "-" * 80 + "\n\n")
self.logfd.flush()
self.bmv2popen = self.popen(cmd_string, stdout=self.logfd, stderr=self.logfd)
print "⚡️ %s @ %d" % (STRATUM_BMV2, self.grpcPort)
# We want to be notified if stratum_bmv2 quits prematurely...
self.stopped = False
threading.Thread(target=watchdog, args=[self]).start()
except Exception:
StratumBmv2Switch.mininet_exception = 1
self.stop()
self.printLog()
raise
def printLog(self):
if os.path.isfile(self.logfile):
print "-" * 80
print "%s log (from %s):" % (self.name, self.logfile)
with open(self.logfile, 'r') as f:
lines = f.readlines()
if len(lines) > BMV2_LOG_LINES:
print "..."
for line in lines[-BMV2_LOG_LINES:]:
print line.rstrip()
def cleanupTmpFiles(self):
self.cmd("rm -rf %s" % self.tmpDir)
def stop(self, deleteIntfs=True):
"""Terminate switch."""
self.stopped = True
if self.bmv2popen is not None:
if self.bmv2popen.poll() is None:
self.bmv2popen.terminate()
self.bmv2popen.wait()
self.bmv2popen = None
if self.logfd is not None:
self.logfd.close()
self.logfd = None
Switch.stop(self, deleteIntfs)
class NoOffloadHost(Host):
def __init__(self, name, inNamespace=True, **params):
Host.__init__(self, name, inNamespace=inNamespace, **params)
def config(self, **params):
r = super(Host, self).config(**params)
for off in ["rx", "tx", "sg"]:
cmd = "/sbin/ethtool --offload %s %s off" \
% (self.defaultIntf(), off)
self.cmd(cmd)
return r
class NoIpv6OffloadHost(NoOffloadHost):
def __init__(self, name, inNamespace=True, **params):
NoOffloadHost.__init__(self, name, inNamespace=inNamespace, **params)
def config(self, **params):
r = super(NoOffloadHost, self).config(**params)
self.cmd("sysctl net.ipv6.conf.%s.disable_ipv6=1" % (self.defaultIntf()))
return r
# Exports for bin/mn
switches = {'stratum-bmv2': StratumBmv2Switch}
hosts = {
'no-offload-host': NoOffloadHost,
'no-ipv6-host': NoIpv6OffloadHost
}