forked from noxrepo/sdn-debugger
/
debugger.py
executable file
·202 lines (162 loc) · 7.55 KB
/
debugger.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
#!/bin/bash -
# If you have PyPy 1.6+ in a directory called pypy alongside pox.py, we
# use it.
# Otherwise, we try to use a Python interpreter called python2.7, which
# is a good idea if you're using Python from MacPorts, for example.
# We fall back to just "python" and hope that works.
''''echo -n
export OPT="-O"
export FLG=""
if [[ "$(basename $0)" == "debug-pox.py" ]]; then
export OPT=""
export FLG="--debug"
fi
if [ -x pypy/bin/pypy ]; then
exec pypy/bin/pypy $OPT "$0" $FLG "$@"
fi
if [ "$(type -P python2.7)" != "" ]; then
exec python2.7 $OPT "$0" $FLG "$@"
fi
exec python $OPT "$0" $FLG "$@"
'''
from sts.debugger import FuzzTester
from sts.deferred_io import DeferredIOWorker
from sts.procutils import kill_procs, popen_filtered
import sts.topology_generator as default_topology
from sts.topology_generator import TopologyGenerator
from pox.lib.ioworker.io_worker import RecocoIOLoop
from pox.lib.util import connect_socket_with_backoff
from sts.experiment_config_lib import Controller
from pox.lib.recoco.recoco import Scheduler
import signal
import sys
import string
import subprocess
import time
import argparse
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("sts")
# We use python as our DSL for specifying experiment configuration
# The module can define the following functions:
# controllers(command_line_args=[]) => returns a list of pox.sts.experiment_config_info.ControllerInfo objects
# switches() => returns a list of pox.sts.experiment_config_info.Switch objects
description = """
Run a debugger experiment.
Example usage:
$ %s ./pox/pox.py --no-cli openflow.of_01 --address=__address__ --port=__port__
""" % (sys.argv[0])
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description=description)
parser.add_argument("-n", "--non-interactive", help='run debugger non-interactively',
action="store_false", dest="interactive", default=True)
parser.add_argument("-C", "--check-interval", type=int,
help='Run correspondence checking every C timesteps (assume -n)',
dest="check_interval", default=35)
# TODO: add argument for trace injection interval
parser.add_argument("-D", "--delay", type=float, metavar="time",
default=0.1,
help="delay in seconds for non-interactive simulation steps")
parser.add_argument("-R", "--random-seed", type=float, metavar="rnd",
help="Seed for the pseduo random number generator", default=0.0)
parser.add_argument("-s", "--steps", type=int, metavar="nsteps",
help="number of steps to simulate", default=None)
parser.add_argument("-p", "--port", type=int, metavar="port",
help="base port to use for controllers", default=6633)
parser.add_argument("-l", "--snapshotport", type=int, metavar="snapshotport",
help="port to use for controllers for snapshotting", default=6634)
parser.add_argument("-f", "--fuzzer-params", default="fuzzer_params.cfg",
help="optional parameters for the fuzzer (e.g. fail rate)")
parser.add_argument("-F", "--fat-tree", type=bool, default=False,
dest="fattree",
help="optional parameters for the fuzzer (e.g. fail rate)")
# Major TODO: need to type-check trace file (i.e., every host in the trace must be present in the network!)
# this has already wasted several hours of time...
parser.add_argument("-t", "--trace-file", default=None,
help="optional dataplane trace file (see trace_generator.py)")
parser.add_argument("-N", "--num-switches", type=int, default=2,
help="number of switches to create in the network")
parser.add_argument("-c", "--config", help='optional experiment config file to load')
parser.add_argument('controller_args', metavar='controller arg', nargs=argparse.REMAINDER,
help='arguments to pass to the controller(s)')
#parser.disable_interspersed_args()
args = parser.parse_args()
if not args.controller_args:
print >> sys.stderr, "Warning: no controller arguments given"
# We use python as our DSL for specifying experiment configuration
# The module can define the following functions:
# controllers(command_line_args=[]) => returns a list of pox.sts.experiment_config_info.ControllerInfo objects
# switches() => returns a list of pox.sts.experiment_config_info.Switch objects
if args.config:
config = __import__(args.config)
else:
config = object()
boot_controllers = False
if hasattr(config, 'controllers'):
if hasattr(config.controllers, '__call__'):
controllers = config.controllers(args.controller_args)
else:
controllers = config.controllers
boot_controllers = config.boot_controllers if hasattr(config, 'boot_controllers') else (len(config.controllers)>0)
else:
controllers = [Controller(args.controller_args, port=args.port)]
boot_controllers = (len(args.controller_args)>0)
if hasattr(config, 'topology_generator'):
topology_generator = config.topology_generator
else:
topology_generator = TopologyGenerator()
child_processes = []
scheduler = None
def kill_children():
global child_processes
kill_procs(child_processes)
def kill_scheduler():
if scheduler and not scheduler._hasQuit:
sys.stderr.write("Stopping Recoco Scheduler...")
scheduler.quit()
sys.stderr.write(" OK\n")
def handle_int(signal, frame):
print >> sys.stderr, "Caught signal %d, stopping sdndebug" % signal
kill_children()
kill_scheduler()
sys.exit(0)
signal.signal(signal.SIGINT, handle_int)
signal.signal(signal.SIGTERM, handle_int)
try:
# Boot the controllers
if boot_controllers:
for (i, c) in enumerate(controllers):
command_line_args = map(lambda(x): string.replace(x, "__port__", str(c.port)),
map(lambda(x): string.replace(x, "__address__", str(c.address)), c.cmdline))
print command_line_args
child = popen_filtered("c%d" % i, command_line_args)
logger.info("Launched controller c%d: %s [PID %d]" % (i, " ".join(command_line_args), child.pid))
child_processes.append(child)
io_loop = RecocoIOLoop()
scheduler = Scheduler(daemon=True, useEpoll=False)
scheduler.schedule(io_loop)
#if hasattr(config, 'switches'):
# pass
create_worker = lambda(socket): DeferredIOWorker(io_loop.create_worker_for_socket(socket), scheduler.callLater)
# TODO: need a better way to choose FatTree vs. Mesh vs. whatever
# Also, abusing the "num_switches" command line arg -> num_pods
(panel,
switch_impls,
network_links,
hosts,
access_links) = topology_generator.populate_fat_tree(controllers,
create_worker,
num_pods=args.num_switches) \
if args.fattree else \
topology_generator.populate(controllers, create_worker, num_switches=args.num_switches)
# For instrumenting the controller
# TODO: This ugly hack has to be cleaned up ASAP ASAP
control_socket = None #connect_socket_with_backoff('', 6634)
simulator = FuzzTester(fuzzer_params=args.fuzzer_params, interactive=args.interactive,
check_interval=args.check_interval,
random_seed=args.random_seed, delay=args.delay,
dataplane_trace=args.trace_file, control_socket=control_socket)
simulator.simulate(panel, switch_impls, network_links, hosts, access_links, steps=args.steps)
finally:
kill_children()
kill_scheduler()