Таблица групп OVS для настройки алгоритмов маршрутизации - PullRequest
0 голосов
/ 05 ноября 2019

У меня есть исходный код на Python, и я работаю в поле SDN, используя контроллер Ryu и mininet в качестве программы эмулятора. Тем не менее, я хочу установить правила потока в каждом переключателе. Таким образом, если пакет вставлен в s1, он будет перенаправлен через несколько коммутаторов на хост назначения в другом коммутаторе. Вопрос в том, как установить правила потока на основе алгоритмов маршрутизации?

У меня есть исходный код статической маршрутизации, скажите мне, где и что я должен изменить, чтобы применить разные алгоритмы маршрутизации через настройку правил потока в каждомswitch?

Любая помощь будет высоко оценена.

'' '

> 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 from ryu.lib.packet import packet from ryu.lib.packet
> import ethernet from ryu.lib.packet import ether_types


class SimpleSwitch13(app_manager.RyuApp):
    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]

def __init__(self, *args, **kwargs):
    super(SimpleSwitch13, self).__init__(*args, **kwargs)
    self.mac_to_port = {}

@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

    # install table-miss flow entry
    '''
    match = parser.OFPMatch()
    actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
                                      ofproto.OFPCML_NO_BUFFER)]
    self.add_flow(datapath, 0, match, actions)
    '''
    # switch s1
    if datapath.id == 1:
        # add group tables
        self.send_group_mod(datapath)
        actions = [parser.OFPActionGroup(group_id=50)]
        match = parser.OFPMatch(in_port=3)
        self.add_flow(datapath, 10, match, actions)

        #add the return flow for h1 in s1.  
        # h1 is connected to port 3.
        actions = [parser.OFPActionOutput(3)]
        match = parser.OFPMatch(in_port=1)
        self.add_flow(datapath, 10, match, actions)

        actions = [parser.OFPActionOutput(3)]
        match = parser.OFPMatch(in_port=2)
        self.add_flow(datapath, 10, match, actions)


    # switch s4
    if datapath.id == 4:
    # add group tables
        self.send_group_mod(datapath)
        actions = [parser.OFPActionGroup(group_id=50)]
        match = parser.OFPMatch(in_port=3)
        self.add_flow(datapath, 10, match, actions)


        #add the return flow for h2 in s4.  
        # h2 is connected to port 3.
        actions = [parser.OFPActionOutput(3)]
        match = parser.OFPMatch(in_port=1)
        self.add_flow(datapath, 10, match, actions)

        actions = [parser.OFPActionOutput(3)]
        match = parser.OFPMatch(in_port=2)
        self.add_flow(datapath, 10, match, actions)


    # switch s2
    if datapath.id == 2:
        # h1 is connected to port 3.
        actions = [parser.OFPActionOutput(2)]
        match = parser.OFPMatch(in_port=1)
        self.add_flow(datapath, 10, match, actions)

        actions = [parser.OFPActionOutput(1)]
        match = parser.OFPMatch(in_port=2)
        self.add_flow(datapath, 10, match, actions)


    # switch s3
    if datapath.id == 3:
        # h1 is connected to port 3.
        actions = [parser.OFPActionOutput(2)]
        match = parser.OFPMatch(in_port=1)
        self.add_flow(datapath, 10, match, actions)

        actions = [parser.OFPActionOutput(1)]
        match = parser.OFPMatch(in_port=2)
        self.add_flow(datapath, 10, match, actions)

def add_flow(self, datapath, priority, match, actions, buffer_id=None):
    ofproto = datapath.ofproto
    parser = datapath.ofproto_parser

    inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                         actions)]
    if buffer_id:
        mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
                                priority=priority, match=match,
                                instructions=inst)
    else:
        mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
                                match=match, instructions=inst)
    datapath.send_msg(mod)

@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
    # If you hit this you might want to increase
    # the "miss_send_length" of your switch
    if ev.msg.msg_len < ev.msg.total_len:
        self.logger.debug("packet truncated: only %s of %s bytes",
                          ev.msg.msg_len, ev.msg.total_len)
    msg = ev.msg
    datapath = msg.datapath
    ofproto = datapath.ofproto
    parser = datapath.ofproto_parser
    in_port = msg.match['in_port']

    pkt = packet.Packet(msg.data)
    eth = pkt.get_protocols(ethernet.ethernet)[0]

    if eth.ethertype == ether_types.ETH_TYPE_LLDP:
        # ignore lldp packet
        return
    dst = eth.dst
    src = eth.src

    dpid = datapath.id
    if dst[:5] == "33:33":
        self.logger.info("drop ipv6 multicast packet %s", dst)
        return

    self.mac_to_port.setdefault(dpid, {})

    self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)

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

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

    actions = [parser.OFPActionOutput(out_port)]

    # install a flow to avoid packet_in next time
    if out_port != ofproto.OFPP_FLOOD:
        match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src)
        # verify if we have a valid buffer_id, if yes avoid to send both
        # flow_mod & packet_out
        if msg.buffer_id != ofproto.OFP_NO_BUFFER:
            self.add_flow(datapath, 1, match, actions, msg.buffer_id)
            return
        else:
            self.add_flow(datapath, 1, match, actions)
    data = None
    if msg.buffer_id == ofproto.OFP_NO_BUFFER:
        data = msg.data

    out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
                              in_port=in_port, actions=actions, data=data)
    datapath.send_msg(out)



def send_group_mod(self, datapath):
    ofproto = datapath.ofproto
    parser = datapath.ofproto_parser

    # Hardcoding the stuff, as we already know the topology diagram.
    # Group table1
    # Receiver port3 (host connected), forward it to port1(switch) and Port2(switch)
    LB_WEIGHT1 = 30 #percentage
    LB_WEIGHT2 = 70 #percentage

    watch_port = ofproto_v1_3.OFPP_ANY
    watch_group = ofproto_v1_3.OFPQ_ALL

    actions1 = [parser.OFPActionOutput(1)]
    actions2 = [parser.OFPActionOutput(2)]
    buckets = [parser.OFPBucket(LB_WEIGHT1, watch_port, watch_group, actions=actions1),
               parser.OFPBucket(LB_WEIGHT2, watch_port, watch_group, actions=actions2)]

    req = parser.OFPGroupMod(datapath, ofproto.OFPGC_ADD,
                             ofproto.OFPGT_SELECT, 50, buckets)
    datapath.send_msg(req)

' ''

...