forked from noxrepo/pox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
datapaths: Add dpctl functionality to pcap_switch
This actually makes a number of semi-related changes: * Refactors port generation so that switch subclasses can alter behavior * Adds a little datapaths.ctl framework and simple client * Fixes some minor bugs in switch base class * Makes switch base class call its own API functions * Refactors port/interface initialization in pcap_switch * Gives pcap_switch ability to add/remove ports while running * Implements add-port/del-port/show dpctl commands
- Loading branch information
Showing
3 changed files
with
330 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# Copyright 2013 James McCauley | ||
# | ||
# 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. | ||
|
||
""" | ||
Simple datapath control framework for POX datapaths | ||
""" | ||
|
||
from pox.core import core | ||
from pox.lib.ioworker.workers import * | ||
from pox.lib.ioworker import * | ||
from pox.lib.revent import * | ||
|
||
|
||
# IOLoop for our IO workers | ||
_ioloop = None | ||
|
||
# Log | ||
log = None | ||
|
||
|
||
class CommandEvent (Event): | ||
""" | ||
Event fired whenever a command is received | ||
""" | ||
def __init__ (self, worker, cmd): | ||
super(CommandEvent,self).__init__() | ||
self.worker = worker | ||
self.cmd = cmd | ||
|
||
@property | ||
def first (self): | ||
return self.cmd.strip().split()[0] | ||
|
||
@property | ||
def args (self): | ||
return self.cmd.strip().split()[1:] | ||
|
||
def __str__ (self): | ||
return "<%s: %s>" % (self.worker, self.cmd) | ||
|
||
|
||
class ServerWorker (TCPServerWorker, RecocoIOWorker): | ||
""" | ||
Worker to accept connections | ||
""" | ||
pass | ||
#TODO: Really should just add this to the ioworker package. | ||
|
||
|
||
class Worker (RecocoIOWorker): | ||
""" | ||
Worker to receive POX dpctl commands | ||
""" | ||
def __init__ (self, *args, **kw): | ||
super(Worker, self).__init__(*args, **kw) | ||
self._connecting = True | ||
self._buf = b'' | ||
|
||
def _process (self, data): | ||
self._buf += data | ||
while '\n' in self._buf: | ||
fore,self._buf = self._buf.split('\n', 1) | ||
core.ctld.raiseEventNoErrors(CommandEvent, self, fore) | ||
|
||
|
||
def _handle_rx (self): | ||
self._buf += self.read() | ||
self._process(self.read()) | ||
|
||
def _exec (self, msg): | ||
msg.split() | ||
|
||
|
||
class Server (EventMixin): | ||
""" | ||
Listens on a TCP socket for control | ||
""" | ||
_eventMixin_events = set([CommandEvent]) | ||
|
||
def __init__ (self, port = 7791): | ||
w = ServerWorker(child_worker_type=Worker, port = port) | ||
self.server_worker = w | ||
_ioloop.register_worker(w) | ||
|
||
|
||
def create_server (port = 7791): | ||
# Set up logging | ||
global log | ||
if not log: | ||
log = core.getLogger() | ||
|
||
# Set up IO loop | ||
global _ioloop | ||
if not _ioloop: | ||
_ioloop = RecocoIOLoop() | ||
#_ioloop.more_debugging = True | ||
_ioloop.start() | ||
|
||
c = Server(port = int(port)) | ||
return c | ||
|
||
|
||
def server (port = 7791): | ||
c = create_server(int(port)) | ||
core.register("ctld", c) | ||
|
||
|
||
def launch (cmd, address = None, port = 7791): | ||
core.quit() | ||
if not address: | ||
address = "127.0.0.1" | ||
import socket | ||
core.getLogger('core').setLevel(100) | ||
log = core.getLogger('ctl') | ||
try: | ||
s = socket.create_connection((address,port), timeout=2) | ||
except: | ||
log.error("Couldn't connect") | ||
return | ||
try: | ||
s.settimeout(2) | ||
s.send(cmd + "\n") | ||
d = s.recv(4096).strip() | ||
core.getLogger("ctl").info(d) | ||
except socket.timeout: | ||
log.warn("No response") | ||
except: | ||
log.exception("While communicating") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.