轉載https://www.cnblogs.com/fjlinww/p/11918279.html
[root@kunpeng82 devuser]# mn --controller=remote,ip=127.0.0.1,port=6633 *** Creating network *** Adding controller *** Adding hosts: h1 h2 *** Adding switches: s1 *** Adding links: (h1, s1) (h2, s1) *** Configuring hosts h1 h2 *** Starting controller c0 *** Starting 1 switches s1 ... *** Starting CLI: mininet> h1 ping h2 PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data. From 10.0.0.1 icmp_seq=1 Destination Host Unreachable From 10.0.0.1 icmp_seq=2 Destination Host Unreachable From 10.0.0.1 icmp_seq=3 Destination Host Unreachable From 10.0.0.1 icmp_seq=4 Destination Host Unreachable From 10.0.0.1 icmp_seq=5 Destination Host Unreachable From 10.0.0.1 icmp_seq=6 Destination Host Unreachable From 10.0.0.1 icmp_seq=7 Destination Host Unreachable From 10.0.0.1 icmp_seq=8 Destination Host Unreachable From 10.0.0.1 icmp_seq=9 Destination Host Unreachable From 10.0.0.1 icmp_seq=10 Destination Host Unreachable From 10.0.0.1 icmp_seq=11 Destination Host Unreachable From 10.0.0.1 icmp_seq=12 Destination Host Unreachable ^C
test1.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.packet import packet from ryu.lib.packet import ethernet from ryu.lib.packet import ether_types from ryu.lib.packet import ipv4 class SimpleSwitch(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION] def __init__(self, *args, **kwargs): super(SimpleSwitch, self).__init__(*args, **kwargs) @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 print ("PACKET_IN:") print (eth.ethertype) print ("ethernet:") print ("eth_src=",eth.src) print ("eth_dst=",eth.dst) if eth.ethertype == ether_types.ETH_TYPE_IP: _ipv4 = pkt.get_protocol(ipv4.ipv4) print ("ipv4:") print ("ip_src=",_ipv4.src) print ("ip_dst=",_ipv4.dst) dpid = datapath.id out_port = ofproto.OFPP_FLOOD actions = [datapath.ofproto_parser.OFPActionOutput(out_port)] data = None 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...") print
[root@kunpeng82 app]# ryu-manager --verbose test1.py
loading app test1.py
loading app ryu.controller.ofp_handler
instantiating app test1.py of SimpleSwitch
instantiating app ryu.controller.ofp_handler of OFPHandler
BRICK SimpleSwitch
CONSUMES EventOFPPacketIn
BRICK ofp_event
[root@kunpeng82 app]# ryu-manager --verbose test1.py loading app test1.py loading app ryu.controller.ofp_handler instantiating app test1.py of SimpleSwitch instantiating app ryu.controller.ofp_handler of OFPHandler BRICK SimpleSwitch CONSUMES EventOFPPacketIn BRICK ofp_event PROVIDES EventOFPPacketIn TO {'SimpleSwitch': {'main'}} CONSUMES EventOFPEchoReply CONSUMES EventOFPEchoRequest CONSUMES EventOFPErrorMsg CONSUMES EventOFPHello CONSUMES EventOFPPortDescStatsReply CONSUMES EventOFPPortStatus CONSUMES EventOFPSwitchFeatures connected socket:<eventlet.greenio.base.GreenSocket object at 0xffff7992ad68> address:('127.0.0.1', 52714) hello ev <ryu.controller.ofp_event.EventOFPHello object at 0xffff7992a780> move onto config mode switch features ev version=0x1,msg_type=0x6,msg_len=0xb0,xid=0xd921f2f2,OFPSwitchFeatures(actions=4095,capabilities=199,datapath_id=1,n_buffers=0,n_tables=254,ports={65534: OFPPhyPort(port_no=65534,hw_addr='1e:55:2d:a3:e7:4c',name=b's1',config=1,state=1,curr=0,advertised=0,supported=0,peer=0), 1: OFPPhyPort(port_no=1,hw_addr='6e:b6:52:7b:f3:46',name=b's1-eth1',config=0,state=0,curr=192,advertised=0,supported=0,peer=0), 2: OFPPhyPort(port_no=2,hw_addr='d2:ac:eb:71:c6:24',name=b's1-eth2',config=0,state=0,curr=192,advertised=0,supported=0,peer=0)}) move onto main mode EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2054 ethernet: eth_src= 1a:43:54:b4:96:50 eth_dst= ff:ff:ff:ff:ff:ff PACKET_OUT... EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2054 ethernet: eth_src= 1a:43:54:b4:96:50 eth_dst= ff:ff:ff:ff:ff:ff PACKET_OUT... EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2054 ethernet: eth_src= 1a:43:54:b4:96:50 eth_dst= ff:ff:ff:ff:ff:ff PACKET_OUT... EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2054 ethernet: eth_src= 1a:43:54:b4:96:50 eth_dst= ff:ff:ff:ff:ff:ff PACKET_OUT... EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2054 ethernet: eth_src= 1a:43:54:b4:96:50 eth_dst= ff:ff:ff:ff:ff:ff PACKET_OUT... EVENT ofp_event->SimpleSwitch EventOFPPacketIn EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2054 ethernet: eth_src= 1a:43:54:b4:96:50 eth_dst= ff:ff:ff:ff:ff:ff PACKET_OUT... EVENT ofp_event->SimpleSwitch EventOFPPacketIn EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2054 ethernet: eth_src= 1a:43:54:b4:96:50 eth_dst= ff:ff:ff:ff:ff:ff PACKET_OUT... EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2054 ethernet: eth_src= 1a:43:54:b4:96:50 eth_dst= ff:ff:ff:ff:ff:ff PACKET_OUT... EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2054 ethernet: eth_src= 1a:43:54:b4:96:50 eth_dst= ff:ff:ff:ff:ff:ff PACKET_OUT... EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2054 ethernet: eth_src= 1a:43:54:b4:96:50 eth_dst= ff:ff:ff:ff:ff:ff PACKET_OUT... EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN:
在機器上tcpdump lo
tcpdump -i lo -env -w pap.cap
增加如下語句h1 ping h2
data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data 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...") print
mininet> h2 ping h1 PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data. 64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=55.8 ms 64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.235 ms ^C --- 10.0.0.1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 0.235/28.051/55.867/27.816 ms mininet>
EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN: 2048 ethernet: eth_src= 2e:25:f2:98:49:13 eth_dst= da:54:a5:07:16:8d ipv4: ip_src= 10.0.0.2 ip_dst= 10.0.0.1 packet in 1 2e:25:f2:98:49:13 da:54:a5:07:16:8d 2 PACKET_OUT...
[root@kunpeng82 devuser]# ovs-ofctl dump-flows s1 [root@kunpeng82 devuser]# ovs-dpctl dump-flows [root@kunpeng82 devuser]#
流表是空的,每次ping都會產生
EVENT ofp_event->SimpleSwitch EventOFPPacketIn PACKET_IN
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.packet import packet from ryu.lib.packet import ethernet from ryu.lib.packet import ether_types from ryu.lib.packet import ipv4 class SimpleSwitch(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION] def __init__(self, *args, **kwargs): super(SimpleSwitch, self).__init__(*args, **kwargs) self.mac_to_port = {} @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 print ("PACKET_IN:") print (eth.ethertype) print ("ethernet:") print ("eth_src=",eth.src) print ("eth_dst=",eth.dst) dst = eth.dst src = eth.src if eth.ethertype == ether_types.ETH_TYPE_IP: _ipv4 = pkt.get_protocol(ipv4.ipv4) print ("ipv4:") print ("ip_src=",_ipv4.src) print ("ip_dst=",_ipv4.dst) dpid = datapath.id self.mac_to_port.setdefault(dpid, {}) self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port) out_port = ofproto.OFPP_FLOOD actions = [datapath.ofproto_parser.OFPActionOutput(out_port)] data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data 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...") print