Skip to content

Instantly share code, notes, and snippets.

@Eragoo
Created October 20, 2023 08:25
Show Gist options
  • Save Eragoo/732e69875fb0fc16a098d38035d82215 to your computer and use it in GitHub Desktop.
Save Eragoo/732e69875fb0fc16a098d38035d82215 to your computer and use it in GitHub Desktop.
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
class MySDNApp(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(MySDNApp, self).__init__(*args, **kwargs)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
datapath = ev.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
#Match ALL
match = parser.OFPMatch()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 1, match, actions)
def add_flow(self, datapath, priority, match, actions):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
buffer_id = ofproto.OFP_NO_BUFFER
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
priority=priority, match=match,
instructions=inst)
datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
self.logger.info("Got new packet!")
@Eragoo
Copy link
Author

Eragoo commented Oct 20, 2023

FROM osrg/ryu:latest
RUN apt-get update && apt-get install -y openvswitch-switch
COPY ryu.py /app/your_ryu_app.py
EXPOSE 6653
CMD ["ryu-manager", "/app/your_ryu_app.py"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment