Last active
August 29, 2015 14:25
-
-
Save Yi-Tseng/17de39c8be1619b3c4ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import ast | |
| from ryu.app import simple_switch | |
| from ryu.lib.ip import ipv4_to_bin | |
| from ryu.lib.mac import haddr_to_bin | |
| from ryu.ofproto import ofproto_v1_0 | |
| from webob import Response | |
| from ryu.controller import ofp_event | |
| from ryu.app.wsgi import ControllerBase, WSGIApplication, route | |
| from ryu.controller.handler import CONFIG_DISPATCHER | |
| from ryu.controller.handler import set_ev_cls | |
| simple_switch_instance_name = 'simple_switch_api_app' | |
| url = '/selfdefine/add/{dpid}' | |
| def convert_ip_to_int(ip): | |
| ''' | |
| From string to int | |
| 10.0.0.2 -> 167772162 | |
| ''' | |
| tmp = 0 | |
| for b in ipv4_to_bin(ip): | |
| tmp |= ord(b) | |
| tmp = tmp << 8 | |
| tmp = tmp >> 8 | |
| return tmp | |
| class SimpleSwitchRest10(simple_switch.SimpleSwitch): | |
| OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION] | |
| _CONTEXTS = {'wsgi': WSGIApplication} | |
| def __init__(self, *args , **kwargs): | |
| super(SimpleSwitchRest10, self).__init__(*args, **kwargs) | |
| self.switches = {} | |
| wsgi = kwargs['wsgi'] | |
| wsgi.register(SimpleSwitchController, {simple_switch_instance_name : self}) | |
| @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER) | |
| def switch_features_handler(self, ev): | |
| datapath = ev.msg.datapath | |
| dpid = datapath.id | |
| self.switches[dpid] = datapath | |
| def set_def_rule(self, dpid, entry): | |
| datapath = self.switches.get(dpid) | |
| fromdst = entry['dst_IP'] | |
| fromdst = convert_ip_to_int(fromdst) | |
| dstMAC = entry['dst_MAC'] | |
| todstIP = entry['to_dst_ip'] | |
| ofp = datapath.ofproto | |
| parser = datapath.ofproto_parser | |
| match = parser.OFPMatch(nw_proto=6, | |
| tp_dst=1111, | |
| nw_dst=fromdst) | |
| todstIP = convert_ip_to_int(todstIP) | |
| actions = [ | |
| parser.OFPActionSetNwDst(todstIP), | |
| parser.OFPActionSetDlDst(haddr_to_bin(dstMAC)) | |
| ] | |
| mod = parser.OFPFlowMod(datapath=datapath, | |
| match=match, | |
| cookie=0, | |
| command=ofp.OFPFC_ADD, | |
| idle_timeout=0, | |
| hard_timeout=0, | |
| priority=5, | |
| actions=actions) | |
| datapath.send_msg(mod) | |
| class SimpleSwitchController(ControllerBase): | |
| def __init__(self, req, link, data, **config): | |
| super(SimpleSwitchController, self).__init__(req, link, data, **config) | |
| self.simpl_switch_spp = data[simple_switch_instance_name] | |
| @route('simpleswitch', '/simpleswitch/addflow', methods=['POST']) | |
| def get_rule_inf(self, req, **kwargs): | |
| simple10_switch = self.simpl_switch_spp | |
| flow = ast.literal_eval(req.body) | |
| dpid = int(flow['dpid']) | |
| try: | |
| simple10_switch.set_def_rule(dpid, flow) | |
| return Response(status=200) | |
| except Exception as e: | |
| print e | |
| return Response(status=500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment