Created
February 7, 2015 11:38
-
-
Save upa/42420792d6d207f94ba5 to your computer and use it in GitHub Desktop.
OpenFlow Patch(er)
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
""" | |
OpenFlow Patcher | |
""" | |
import json | |
import types | |
import logging | |
import struct | |
from ryu.base import app_manager | |
from ryu.controller import mac_to_port | |
from ryu.controller import ofp_event | |
from ryu.controller.handler import MAIN_DISPATCHER | |
from ryu.controller.handler import set_ev_cls | |
from ryu.controller import dpset | |
from ryu.ofproto import ofproto_v1_0 | |
from ryu.ofproto import ether | |
from ryu.lib import addrconv | |
from ryu.lib.mac import haddr_to_bin, haddr_to_str | |
from ryu.lib.packet import packet | |
from ryu.lib.packet import ethernet | |
class OFPatcher (app_manager.RyuApp) : | |
OFP_VERSIONS = [ ofproto_v1_0.OFP_VERSION ] | |
_CONTEXTS = { 'dpset' : dpset.DPSet, } | |
def __init__ (self, *args, **kwargs) : | |
super (OFPatcher, self).__init__ (*args, **kwargs) | |
print "OpenFlow Patcher is now starting" | |
try : | |
f = open ('patchinfo.json') | |
self.patchinfo = json.load (f) | |
f.close () | |
except : | |
print "failed to load ofpatcher.json" | |
raise | |
print "Configuration file" | |
print json.dumps(self.patchinfo, sort_keys = True, indent = 4) | |
return | |
@set_ev_cls (dpset.EventDP) | |
def dp_handler (self, ev) : | |
if ev.enter : | |
print "New DPID %s [ %s ]" \ | |
% (ev.dp.id, " ".join (map (str, ev.dp.ports))) | |
else : | |
print "DPID %s leaved" % (ev.dp.id) | |
return | |
print "Install Patch flow entrys" | |
for patch in self.patchinfo : | |
self.install_patch_flow (ev, patch[0], patch[1]) | |
self.install_patch_flow (ev, patch[1], patch[0]) | |
return | |
def install_patch_flow (self, ev, ingress, egress) : | |
print "from %d -> %d [ %s ]" % (ingress["port"], egress["port"], | |
egress["egress-mac"]) | |
datapath = ev.dp | |
ofproto = datapath.ofproto | |
# set flow entry from patch[0] to patch[1] | |
match = datapath.ofproto_parser.OFPMatch ( | |
in_port = ingress["port"]) | |
actions = [ | |
datapath.ofproto_parser.OFPActionSetDlDst ( | |
haddr_to_bin (egress["egress-mac"])), | |
datapath.ofproto_parser.OFPActionOutput ( | |
egress["port"]) | |
] | |
mod = datapath.ofproto_parser.OFPFlowMod ( | |
datapath = datapath, match = match, cookie = 0, | |
command = ofproto.OFPFC_ADD, idle_timeout = 0, hard_timeout = 0, | |
priority = ofproto.OFP_DEFAULT_PRIORITY, | |
flags = ofproto.OFPFF_SEND_FLOW_REM, actions = actions) | |
datapath.send_msg (mod) |
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
[ | |
[ | |
{ | |
"port": 49, | |
"egress-mac": "90:e2:ba:3a:24:fd" | |
}, | |
{ | |
"port": 52, | |
"egress-mac": "90:e2:ba:2c:e9:58" | |
} | |
], | |
[ | |
{ | |
"port": 50, | |
"egress-mac": "10:00:01:50:50:50" | |
}, | |
{ | |
"port": 51, | |
"egress-mac": "10:00:01:51:51:51" | |
} | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment