2019 SDN fifth job

1. Visit the official website RYU RYU learn to install and develop introductory tutorial, tutorials Submit your understanding of the code, including, but not limited to, RYU controller:

(1) describe the official tutorial implements a kind of switch function?

The received packet to all ports of the function

(2) What version of the OpenFlow controller setting switches support?

Support OpenFlow1

(3) The controller sets the switch how to handle a packet?

The received broadcast data packet sent to all ports, specific code as follows:

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

        actions = [ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)]
        out = ofp_parser.OFPPacketOut(
            datapath=dp, buffer_id=msg.buffer_id, in_port=msg.in_port,
            actions=actions)
        dp.send_msg(out)

2, according to the official tutorial and sample code (SimpleSwitch.py) provided with a switch to the code (SelfLearning.py) self-learning function of the full complement

from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_0

from ryu.lib.mac import haddr_to_bin
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types


class SimpleSwitch(app_manager.RyuApp):
    # TODO define OpenFlow 1.0 version for the switch
    OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]

    def __init__(self, *args, **kwargs):
        super(SimpleSwitch, self).__init__(*args, **kwargs)
        self.mac_to_port = {}
    
    
    def add_flow(self, datapath, in_port, dst, src, actions):
        ofproto = datapath.ofproto

        match = datapath.ofproto_parser.OFPMatch(
            in_port=in_port,
            dl_dst=haddr_to_bin(dst), dl_src=haddr_to_bin(src))

        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)
        # TODO send modified message out
        datapath.send_msg(mod)

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

        pkt = packet.Packet(msg.data)
        eth = pkt.get_protocol(ethernet.ethernet)

        if eth.ethertype == ether_types.ETH_TYPE_LLDP:
            # ignore lldp packet
            return
        if eth.ethertype == ether_types.ETH_TYPE_IPV6:
            # ignore ipv6 packet
            return       
        
        dst = eth.dst
        src = eth.src
        dpid = datapath.id
        self.mac_to_port.setdefault(dpid, {})

        self.logger.info("packet in DPID:%s MAC_SRC:%s MAC_DST:%s IN_PORT:%s", dpid, src, dst, msg.in_port)

        # learn a mac address to avoid FLOOD next time.
        self.mac_to_port[dpid][src] = msg.in_port

        if dst in self.mac_to_port[dpid]:
            out_port = self.mac_to_port[dpid][dst]
        else:
            out_port = ofproto.OFPP_FLOOD

        # TODO define the action for output
        actions =[datapath.ofproto_parser.OFPActionOutput(out_port)]
    


        # install a flow to avoid packet_in next time
        if out_port != ofproto.OFPP_FLOOD:
            self.logger.info("add flow s:DPID:%s Match:[ MAC_SRC:%s MAC_DST:%s IN_PORT:%s ], Action:[OUT_PUT:%s] ", dpid, src, dst, msg.in_port, out_port)
            self.add_flow(datapath, msg.in_port, dst, src, actions)

        data = None
        if msg.buffer_id == ofproto.OFP_NO_BUFFER:
            data = msg.data
        

        # TODO define the OpenFlow Packet Out
        out = datapath.ofproto_parser.OFPPacketOut(datapath=datapath,buffer_id=msg.buffer_id,in_port=msg.in_port,actions=actions, data=data)
        datapath.send_msg(out)

    print ("PACKET_OUT...")

3. Create a simple topology in most mininet, and a controller connected to RYU


code show as below:

from mininet.topo import Topo

class MyTopo(Topo):

    def __init__(self):

        # initilaize topology
        Topo.__init__(self)

        # add hosts and switches
        h1 = self.addHost('h1')
        h2 = self.addHost('h2')

        s1 = self.addSwitch('s1')

        # add links
        self.addLink(h1, s1, 1, 1)
        self.addLink(h2, s1, 1, 2)
        
topos = {'mytopo': (lambda: MyTopo())}


开启ryu

连接ryu

4, self-learning function verification switch, analysis and verification of results submitted

No command of the controller forward, h1 and h2 are unable to achieve two-way communication with the

run RYU, guidance switch performs forwarding, you can see the controller at the time of execution h1 ping h2 hair flow guidance forwarding table

can now ping the

view s1 flow table

5, write down your experience experiment

The experiment is the hardest ryu installation, always various problems. After installing the rest of the steps it is not difficult.

Guess you like

Origin www.cnblogs.com/jiang1436/p/11978467.html