2019 SDN-에 다섯 번째 작업

1. 방문 RYU RYU 설치와 소개 튜토리얼을 개발하는 법을 배워야 공식 웹 사이트, 자습서를 포함, 코드의 이해를 제출하지만, RYU 컨트롤러에 국한되지 :

공식 튜토리얼은 스위치 기능 어떤 종류의 실현에 대해 설명?

实现将接收到的数据包发送到所有端口

OpenFlow 컨트롤러 설정의 어떤 버전이 지원 스위치?

OpenFlow 1.0

컨트롤러는 어떻게 패킷을 처리하는 스위치를 설정?

当Ryu收到OpenFlow交换机送来的packet_in消息时调用,set_ev_cls的第一个参数也声明了。
set_ev_cls的第二个参数MAIN_DISPATCHER意味着当Ryu和交换机握手过程(即hello, features request/reply, Set Config等)完毕,才会调用packet_in_handler。
之后定义packet_in消息数据结构,交换机datapath,OpenFlow协议和解析过程;
定义发给交换机packet_out的动作,要求交换机将数据包泛洪广播;定义Ryu向交换机发送的packet_out内容,最后发送消息。
至此,一个能够接收packet和转发packet的交换机完成了(接收packet,广播packet)。
ev.msg是表示packet_in数据结构的对象。
msg.dp是代表数据路径(开关)的对象。
dp.ofproto和dp.ofproto_parser是代表Ryu和交换机协商的OpenFlow协议的对象。

코드 (SelfLearning.py)에 스위치와 함께 제공되는 공식 튜토리얼 및 샘플 코드 (SimpleSwitch.py) 전체 보완의 자기 학습 기능에 따라

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 SelfLearning(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(SelfLearning, 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...")

RYU에 연결된 대부분의 mininet에서 간단한 토폴로지 및 컨트롤러를 작성합니다

파이썬 스크립트를
mininet.topo 가져 오기 토포에서

class Mytopo(Topo):

def __init__(self):

    Topo.__init__(self)

    sw=self.addSwitch('s1')

    count=1

    for i in range(2):

            host = self.addHost('h{}'.format(count))

            self.addLink(host,sw,1,count)

            count = count + 1

topos = {'mytopo': (lambda:Mytopo())}

자기 학습 기능 스위치, 제출 과정을 확인하고 검증 결과를 분석 (4)

당신의 경험 실험을 기록 (5)

ryu的安装很坑,python的空格·····

추천

출처www.cnblogs.com/huaranmeng/p/11954611.html