1

Python で記述された Ryu オープン フロー コントローラー スイッチを使用して、仮想ミニネットのパケットを監視しています。3 つのホストがあり、ホスト 2 からホスト 3 へ、およびホスト 3 からホスト 2 への転送をブロックしています。その他のパケットは、スイッチ フロー テーブルに追加されます。私の問題は、フローが追加された後、それらがスイッチのフロー テーブルにルールを持つ 2 つのホスト間のパケットである場合、イベントがトリガーされないことです。たとえば、スイッチがホスト 1 からホスト 2 へのパケットを検出した場合、それは正当であるため、フローがテーブルに追加されますが、ホスト 1 からホスト 2 への別のパケットが送信された場合、そのパケットは再びメソッドを通過しません。Ryu ガイドを調べましたが、スイッチ フロー テーブルにフローが既に追加されている場合については何も見つかりませんでした。どうすればパケットをキャッチできますか?

前もって感謝します。

これが私のコードです:

import logging
import struct

from ryu.base import app_manager
from ryu.controller import mac_to_port
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_str




class SimpleSwitch(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
counterTraffic=0    
def __init__(self, *args, **kwargs):
    super(SimpleSwitch, self).__init__(*args, **kwargs)
    self.mac_to_port = {}

def add_flow(self, datapath, in_port, dst, actions):
    ofproto = datapath.ofproto

    wildcards = ofproto_v1_0.OFPFW_ALL
    wildcards &= ~ofproto_v1_0.OFPFW_IN_PORT
    wildcards &= ~ofproto_v1_0.OFPFW_DL_DST

    match = datapath.ofproto_parser.OFPMatch(
        wildcards, in_port, 0, dst,
        0, 0, 0, 0, 0, 0, 0, 0, 0)

    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)
    datapath.send_msg(mod)

@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)        
def _packet_in_handler(self, ev):

print("Im in main function")
    msg = ev.msg
    datapath = msg.datapath
    ofproto = datapath.ofproto

    dst, src, _eth_type = struct.unpack_from('!6s6sH', buffer(msg.data), 0)

    dpid = datapath.id
    self.mac_to_port.setdefault(dpid, {})

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

if (haddr_to_str(dst) == "00:00:00:00:00:01"):
    print "dst"
    self.counterTraffic +=1

if not ((haddr_to_str(src) == "00:00:00:00:00:02" and  haddr_to_str(dst) =="00:00:00:00:00:03")or (haddr_to_str(src) == "00:00:00:00:00:03" and  haddr_to_str(dst) =="00:00:00:00:00:02")):
        # learn a mac address to avoid FLOOD next time.
    print("after condition")
        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

            actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]

         # install a flow to avoid packet_in next time
        if out_port != ofproto.OFPP_FLOOD:
                self.add_flow(datapath, msg.in_port, dst, actions)

        out = datapath.ofproto_parser.OFPPacketOut(
        datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
                actions=actions)

        datapath.send_msg(out)
    if (haddr_to_str(src) == "00:00:00:00:00:01"):
        print "src"
                self.counterTraffic +=1
    print(self.counterTraffic)


@set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
def _port_status_handler(self, ev):
    msg = ev.msg
    reason = msg.reason
    port_no = msg.desc.port_no

    ofproto = msg.datapath.ofproto
    if reason == ofproto.OFPPR_ADD:
        self.logger.info("port added %s", port_no)
    elif reason == ofproto.OFPPR_DELETE:
        self.logger.info("port deleted %s", port_no)
    elif reason == ofproto.OFPPR_MODIFY:
        self.logger.info("port modified %s", port_no)
    else:
        self.logger.info("Illeagal port state %s %s", port_no, reason)
4

1 に答える 1

1

haddr_to_str(src), haddr_to_str(dst) を印刷しようとしたところ、00:00:00:00:00:03 と ff:ff:ff:ff:ff:ff が得られましたが、これは私が期待したものではありません。ソースとして2つ、宛先として3つ取得したかったのです。

簡単に言えば、宛先の MAC アドレスを正しくデコードしているということです...ただし、IP は MAC アドレスを解決するために ARP を使用する必要がff:ff:ff:ff:ff:ffあります

下のIPv4レイヤーまでデコードする完全なコントローラーを構築しました...

ryu スイッチ パケット デコーダの更新

raw をデコードしてきましたが、生のパケットをアンパックする代わりにryu Packet ライブラリstructsを使用する方がはるかに簡単です。これは、送信元と送信先の MAC アドレス、および上位層のプロトコルを出力するだけの の非常に簡単な置き換えです.​​..struct_packet_in_handler()

from ryu.lib.packet import packet, ethernet, arp, ipv4
import array

@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):

    ### Mike Pennington's logging modifications
    ## Set up to receive the ethernet src / dst addresses
    pkt = packet.Packet(array.array('B', ev.msg.data))
    eth_pkt = pkt.get_protocol(ethernet.ethernet)
    arp_pkt = pkt.get_protocol(arp.arp)
    ip4_pkt = pkt.get_protocol(ipv4.ipv4)
    if arp_pkt:
        pak = arp_pkt
    elif ip4_pkt:
        pak = ip4_pkt
    else:
        pak = eth_pkt
    self.logger.info('  _packet_in_handler: src_mac -> %s' % eth_pkt.src)
    self.logger.info('  _packet_in_handler: dst_mac -> %s' % eth_pkt.dst)
    self.logger.info('  _packet_in_handler: %s' % pak)
    self.logger.info('  ------')
    src = eth_pkt.src  # Set up the src and dst variables so you can use them
    dst = eth_pkt.dst
    ## Mike Pennington's modifications end here

    
    msg = ev.msg
    datapath = msg.datapath
    ofproto = datapath.ofproto

    dpid = datapath.id
    self.mac_to_port.setdefault(dpid, {})

    # 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

    actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]

    # install a flow to avoid packet_in next time
    if out_port != ofproto.OFPP_FLOOD:
        self.add_flow(datapath, msg.in_port, dst, actions)

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

これで、イーサネット パケットが送信されるたびに、ミニネット セッション内でこれが表示されます...

  _packet_in_handler: src_mac -> 00:00:00:00:00:03
  _packet_in_handler: dst_mac -> 33:33:00:00:00:02
  _packet_in_handler: ethernet(dst='33:33:00:00:00:02',ethertype=34525,src='00:00:00:00:00:03')
  ------

ARPパケットは次のようになります...

  _packet_in_handler: src_mac -> 00:00:00:00:00:01
  _packet_in_handler: dst_mac -> ff:ff:ff:ff:ff:ff
  _packet_in_handler: arp(dst_ip='10.0.0.2',dst_mac='00:00:00:00:00:00',hlen=6,hwtype=1,opcode=1,plen=4,proto=2048,src_ip='10.0.0.1',src_mac='00:00:00:00:00:01')
  ------

デモ

上記の変更されたコード (ソースの他の部分を含む) を として保存したとしますne_question.py

root@mininet-vm:/home/mininet# ryu-manager ne_question.py &
                 [1] 14073
loading app ne_question.py
loading app ryu.controller.ofp_handler
instantiating app ryu.controller.ofp_handler of OFPHandler
instantiating app ne_question.py of SimpleSwitch

root@mininet-vm:/home/mininet#
  • 次に、コメントで述べたように、スイッチ トポロジを構築します。
root@mininet-vm:/home/mininet# mn --topo single,3 --mac --switch ovsk --controller remote
*** Creating network
*** Adding controller
*** Adding hosts:
h1 h2 h3
*** Adding switches:
s1
*** Adding links:
(h1, s1) (h2, s1) (h3, s1)
*** Configuring hosts
h1 h2 h3
*** Starting controller
*** Starting 1 switches
s1
*** Starting CLI:
mininet>   _packet_in_handler: src_mac -> 00:00:00:00:00:02
  _packet_in_handler: dst_mac -> 33:33:00:00:00:02
  _packet_in_handler: ethernet(dst='33:33:00:00:00:02',ethertype=34525,src='00:00:00:00:00:02')
  ------
  _packet_in_handler: src_mac -> 00:00:00:00:00:01
  _packet_in_handler: dst_mac -> 33:33:00:00:00:02
  _packet_in_handler: ethernet(dst='33:33:00:00:00:02',ethertype=34525,src='00:00:00:00:00:01')
  ------
  _packet_in_handler: src_mac -> 00:00:00:00:00:03
  _packet_in_handler: dst_mac -> 33:33:00:00:00:02
  _packet_in_handler: ethernet(dst='33:33:00:00:00:02',ethertype=34525,src='00:00:00:00:00:03')
  ------
  • 最後に、Web サーバーを実行し、ページをプルしようとします... http GET の宛先アドレスを解決するために ARP が送信されることに注意してください。ARP の宛先アドレスはff:ff:ff:ff:ff:ff. ところで、 を に変更するwgeth2 wget h1、すべてが正しく機能します...
mininet> h1 python -m SimpleHTTPServer 80 &
mininet> h2 wget -O - h1
--2014-03-28 04:22:25--  http://10.0.0.1/
Connecting to 10.0.0.1:80...   _packet_in_handler: src_mac -> 00:00:00:00:00:02
  _packet_in_handler: dst_mac -> ff:ff:ff:ff:ff:ff
  _packet_in_handler: arp(dst_ip='10.0.0.1',dst_mac='00:00:00:00:00:00',hlen=6,hwtype=1,opcode=1,plen=4,proto=2048,src_ip='10.0.0.2',src_mac='00:00:00:00:00:02')
  ------
--2014-03-28 04:00:58--  http://10.0.0.1/
Connecting to 10.0.0.1:80...   _packet_in_handler: src_mac -> 00:00:00:00:00:02
  _packet_in_handler: dst_mac -> 00:00:00:00:00:01
  _packet_in_handler: ipv4(csum=33886,dst='10.0.0.1',flags=2,header_length=5,identification=41563,offset=0,option=None,proto=6,src='10.0.0.2',tos=0,total_length=60,ttl=64,version=4)
  ------
  _packet_in_handler: src_mac -> 00:00:00:00:00:01
  _packet_in_handler: dst_mac -> 00:00:00:00:00:02
  _packet_in_handler: ipv4(csum=9914,dst='10.0.0.2',flags=2,header_length=5,identification=0,offset=0,option=None,proto=6,src='10.0.0.1',tos=0,total_length=60,ttl=64,version=4)
  ------
connected.
HTTP request sent, awaiting response...   _packet_in_handler: src_mac -> 00:00:00:00:00:02
  _packet_in_handler: dst_mac -> 00:00:00:00:00:01
  _packet_in_handler: ipv4(csum=33893,dst='10.0.0.1',flags=2,header_length=5,identification=41564,offset=0,option=None,proto=6,src='10.0.0.2',tos=0,total_length=52,ttl=64,version=4)
  ------
  _packet_in_handler: src_mac -> 00:00:00:00:00:02
  _packet_in_handler: dst_mac -> 00:00:00:00:00:01
  _packet_in_handler: ipv4(csum=33784,dst='10.0.0.1',flags=2,header_length=5,identification=41565,offset=0,option=None,proto=6,src='10.0.0.2',tos=0,total_length=160,ttl=64,version=4)
  ------
  _packet_in_handler: src_mac -> 00:00:00:00:00:01
  _packet_in_handler: dst_mac -> 00:00:00:00:00:02
  _packet_in_handler: ipv4(csum=61034,dst='10.0.0.2',flags=2,header_length=5,identification=14423,offset=0,option=None,proto=6,src='10.0.0.1',tos=0,total_length=52,ttl=64,version=4)
  ------
  _packet_in_handler: src_mac -> 00:00:00:00:00:01
  _packet_in_handler: dst_mac -> 00:00:00:00:00:02
  _packet_in_handler: ipv4(csum=61016,dst='10.0.0.2',flags=2,header_length=5,identification=14424,offset=0,option=None,proto=6,src='10.0.0.1',tos=0,total_length=69,ttl=64,version=4)
  ------
  _packet_in_handler: src_mac -> 00:00:00:00:00:01
  _packet_in_handler: dst_mac -> 00:00:00:00:00:02
  _packet_in_handler: ipv4(csum=60037,dst='10.0.0.2',flags=2,header_length=5,identification=14425,offset=0,option=None,proto=6,src='10.0.0.1',tos=0,total_length=1047,ttl=64,version=4)
  ------
200 OK
Length: 858 [text/html]
Saving to: `STDOUT'
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
<li><a href=".bash_history">.bash_history</a>
<li><a href=".bash_logout">.bash_logout</a>
<li><a href=".bashrc">.bashrc</a>
<li><a href=".cache/">.cache/</a>
<li><a href=".gitconfig">.gitconfig</a>
<li><a href=".profile">.profile</a>
<li><a href=".rnd">.rnd</a>
<li><a href=".wireshark/">.wireshark/</a>
<li><a href="install-mininet-vm.sh">install-mininet-vm.sh</a>
<li><a href="mininet/">mininet/</a>
<li><a href="ne_question.py">ne_question.py</a>
<li><a href="ne_question.pyc">ne_question.pyc</a>
<li><a href="of-dissector/">of-dissector/</a>
<li><a href="oflops/">oflops/</a>
<li><a href="oftest/">oftest/</a>
<li><a href="openflow/">openflow/</a>
<li><a href="pox/">pox/</a>
</ul>
<hr>
</body>
</html>

     0K                                                       100%  161M=0s

2014-03-28 04:00:58 (161 MB/s) - written to stdout [858/858]

  _packet_in_handler: src_mac -> 00:00:00:00:00:02
mininet>   _packet_in_handler: dst_mac -> 00:00:00:00:00:01
  _packet_in_handler: ipv4(csum=33891,dst='10.0.0.1',flags=2,header_length=5,identification=41566,offset=0,option=None,proto=6,src='10.0.0.2',tos=0,total_length=52,ttl=64,version=4)
  ------
  _packet_in_handler: src_mac -> 00:00:00:00:00:02
  _packet_in_handler: dst_mac -> 00:00:00:00:00:01
  _packet_in_handler: ipv4(csum=33890,dst='10.0.0.1',flags=2,header_length=5,identification=41567,offset=0,option=None,proto=6,src='10.0.0.2',tos=0,total_length=52,ttl=64,version=4)
  ------
  _packet_in_handler: src_mac -> 00:00:00:00:00:02
  _packet_in_handler: dst_mac -> 00:00:00:00:00:01
  _packet_in_handler: ipv4(csum=33889,dst='10.0.0.1',flags=2,header_length=5,identification=41568,offset=0,option=None,proto=6,src='10.0.0.2',tos=0,total_length=52,ttl=64,version=4)
  ------
  _packet_in_handler: src_mac -> 00:00:00:00:00:01
  _packet_in_handler: dst_mac -> 00:00:00:00:00:02
  _packet_in_handler: ipv4(csum=9922,dst='10.0.0.2',flags=2,header_length=5,identification=0,offset=0,option=None,proto=6,src='10.0.0.1',tos=0,total_length=52,ttl=64,version=4)
  ------

mininet>
mininet>
于 2014-04-04T11:35:38.307 に答える