[Python] Pingall and ping between hosts is failing?

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    CUSTOM MININET TOPOLOGY ( this is the custom topology I created)

    from mininet.topo import Topo
    from mininet.net import Mininet
    from mininet.node import RemoteController, OVSSwitch
    from mininet.cli import CLI`enter code here`
    from mininet.log import setLogLevel

    class CustomTopo(Topo):
    def build(self):
    s1 = self.addSwitch('s1', protocols='OpenFlow13')
    h1 = self.addHost('h1', ip='10.0.0.1/24', mac='00:00:00:00:00:01')
    h2 = self.addHost('h2', ip='10.0.0.2/24', mac='00:00:00:00:00:02')
    self.addLink(h1, s1)
    self.addLink(h2, s1)

    def run():
    topo = CustomTopo()
    net = Mininet(topo=topo,
    controller=lambda name: RemoteController(name, ip='127.0.0.1', port=6653),
    switch=OVSSwitch,
    autoSetMacs=True)
    net.start()
    CLI(net)
    net.stop()

    if __name__ == '__main__':
    setLogLevel('info')
    run()


    RYU CONTROLLER AS ROUTER CODE (when I run Ryu it runs fine [Ryu] Controller started with ARP table:.... [Ryu] Switch connected and features received... when I pingall pr h1 ping h2 custom topo.py Ryu Controller output is: Packet-in Received ARP Packet Received Sent ARP reply to 10.0.0.1 with MAC 00:00:00:00:00:02

    but on mininet terminal pingall output is : h1 -> X h2 -> X Results: 100 % dropped also h1 ping h2 is not working

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

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

    def __init__(self, *args, **kwargs):
    super(SimpleRouter, self).__init__(*args, **kwargs)
    self.arp_table = {
    '10.0.0.1': '00:00:00:00:00:01',
    '10.0.0.2': '00:00:00:00:00:02',
    }

    @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
    def switch_features_handler(self, ev):
    datapath = ev.msg.datapath
    parser = datapath.ofproto_parser
    ofproto = datapath.ofproto
    match = parser.OFPMatch()
    actions = [parser.OFPActionOutput(ofproto_OFPP_CONTROLLER, ofproto_OFPCML_NO_BUFFER)]
    inst = [parser.OFPInstructionActions(ofproto_OFPIT_APPLY_ACTIONS, actions)]
    mod = parser.OFPFlowMod(datapath=datapath, priority=0, match=match, instructions=inst)
    datapath.send_msg(mod)

    def add_flow(self, datapath, priority, match, actions):
    parser = datapath.ofproto_parser
    ofproto = datapath.ofproto
    inst = [parser.OFPInstructionActions(ofproto_OFPIT_APPLY_ACTIONS, actions)]
    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):
    msg = ev.msg
    datapath = msg.datapath
    parser = datapath.ofproto_parser
    in_port = msg.match['in_port']
    pkt = packet.Packet(msg.data)
    eth = pkt.get_protocol(ethernet.ethernet)

    if eth.ethertype == ether_types.ETH_TYPE_ARP:
    self.handle_arp(datapath, pkt, in_port)
    elif eth.ethertype == ether_types.ETH_TYPE_IP:
    ip_pkt = pkt.get_protocol(ipv4.ipv4)
    dst_ip = ip_pkt.dst
    if dst_ip in self.arp_table:
    out_port = 2 if in_port == 1 else 1
    actions = [parser.OFPActionOutput(out_port)]
    match = parser.OFPMatch(
    eth_type=ether_types.ETH_TYPE_IP,
    ipv4_dst=dst_ip
    )
    self.add_flow(datapath, 1, match, actions)
    datapath.send_packet_out(
    buffer_id=msg.buffer_id,
    in_port=in_port,
    actions=actions,
    data=msg.data
    )

    def handle_arp(self, datapath, pkt, port):
    arp_pkt = pkt.get_protocol(arp.arp)
    dst_ip = arp_pkt.dst_ip
    if dst_ip not in self.arp_table:
    return
    src_mac = self.arp_table[dst_ip]
    dst_mac = arp_pkt.src_mac
    src_ip = dst_ip
    dst_ip = arp_pkt.src_ip

    e = ethernet.ethernet(dst=dst_mac, src=src_mac, ethertype=ether_types.ETH_TYPE_ARP)
    a = arp.arp(opcode=arp.ARP_REPLY,
    src_mac=src_mac, src_ip=src_ip,
    dst_mac=dst_mac, dst_ip=dst_ip)
    p = packet.Packet()
    p.add_protocol(e)
    p.add_protocol(a)
    p.serialize()
    actions = [datapath.ofproto_parser.OFPActionOutput(port)]
    datapath.send_packet_out(
    buffer_id=0xffffffff,
    in_port=datapath.ofproto_OFPP_CONTROLLER,
    actions=actions,
    data=p.data
    )

    Continue reading...

Compartilhe esta Página