Demo entry 6628274



Submitted by anonymous on Jul 03, 2017 at 15:13
Language: Python. Code size: 4.5 kB.

from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.topology import event, switches
from ryu.topology.api import get_switch, get_link
import networkx as nx

class ExampleShortestForwarding(app_manager.RyuApp):
    """docstring for ExampleShortestForwarding"""

    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]

    def __init__(self, *args, **kwargs):
        super(ExampleShortestForwarding, self).__init__(*args, **kwargs)
        self.topology_api_app = self
        self.paths = {}

    @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
    def switch_features_handler(self, ev):
        datapath = ev.msg.datapath
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser

        # install the table-miss flow entry.
        match = ofp_parser.OFPMatch()
        actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
        self.add_flow(datapath, 0, match, actions)

    def add_flow(self, datapath, priority, match, actions):
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser

        # construct flow_mod message and send it.
        inst = [ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
        mod = ofp_parser.OFPFlowMod(datapath=datapath, priority=priority,
                               match=match, instructions=inst)

    @set_ev_cls(event.EventSwitchEnter, [CONFIG_DISPATCHER, MAIN_DISPATCHER])
    def get_topology(self, ev):
        # get switches and store them into
        switch_list = get_switch(self.topology_api_app, None)   
        switches=[ for switch in switch_list]
        # get links and store them into
        links_list = get_link(self.topology_api_app, None)
        links=[(link.src.dpid,link.dst.dpid,{'port':link.src.port_no}) for link in links_list]

        # reverse link.
        links=[(link.dst.dpid,link.src.dpid,{'port':link.dst.port_no}) for link in links_list]

    def get_out_port(self, src, dst, datapath, in_port):
        dpid =
        # add link between host and ingress switch.
        if src not in
            self.paths.setdefault(src, {})

        if dst in
            # if path is not existed, calculate it and save it.
            if dst not in self.paths[src]:
                path = nx.shortest_path(,src,dst) 
                self.paths[src][dst] = path

            # find out_port to next hop.
            path = self.paths[src][dst]
            print "path: ", path
            next_hop = path[path.index(dpid)+1]
            out_port =[dpid][next_hop]['port']
            out_port = datapath.ofproto.OFPP_FLOOD

        return out_port

    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
    def packet_in_handler(self, ev):
        msg = ev.msg
        datapath = msg.datapath
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser

        pkt = packet.Packet(
        eth = pkt.get_protocol(ethernet.ethernet)
        in_port = msg.match['in_port']

        out_port = self.get_out_port(eth.src, eth.dst, datapath, in_port)
        actions = [ofp_parser.OFPActionOutput(out_port)]
        # install flow_mod to avoid packet_in next time.
        if out_port != ofproto.OFPP_FLOOD:
            match = ofp_parser.OFPMatch(in_port=in_port, eth_dst=eth.dst)
            self.add_flow(datapath, 1, match, actions)

        # send packet_out msg to flood packet.
        out = ofp_parser.OFPPacketOut(

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).