root@d4a934d047cc:~/ryu/ryu/app# cat monitor_arp.py
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
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
from ryu.ofproto import inet
from ryu.lib.packet import ipv4
from ryu.lib.packet import icmp
import os
import json
from ryu.lib.monitor import counts, count_lock
class MonitorArp(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(MonitorArp, self).__init__(*args, **kwargs)
self.mac_to_port = {}
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def _switch_features_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
kwargs = dict(eth_type=ether_types.ETH_TYPE_IP,eth_src="00:00:00:00:00:01",
ip_proto=inet.IPPROTO_ICMP)
kwargs1 = dict(eth_type=ether_types.ETH_TYPE_IP,eth_src="00:00:00:00:00:02",
ip_proto=inet.IPPROTO_ICMP)
actions = [parser.OFPActionOutput(port=ofproto.OFPP_CONTROLLER,
max_len=ofproto.OFPCML_NO_BUFFER)]
inst = [parser.OFPInstructionActions(type_=ofproto.OFPIT_APPLY_ACTIONS,
actions=actions)]
mod = parser.OFPFlowMod(datapath=datapath,
priority=0,
match=parser.OFPMatch(**kwargs),
instructions=inst)
datapath.send_msg(mod)
mod1 = parser.OFPFlowMod(datapath=datapath,
priority=0,
match=parser.OFPMatch(**kwargs1),
instructions=inst)
datapath.send_msg(mod1)
@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
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
pkt_icmp = pkt.get_protocol(icmp.icmp)
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
# ignore lldp packet
return
with count_lock:
key = ("%s_%s"%(pkt_ipv4.src,pkt_ipv4.dst))
if key not in counts:
counts[key]={"srcip":pkt_ipv4.src,"dstip":pkt_ipv4.dst,"count":0}
value = counts[key]
value["count"] = value["count"]+1
root@d4a934d047cc:~/ryu/ryu/app#
def get_monitor(self, req, **_kwargs):
output = []
with count_lock:
for key in counts:
output.append(counts[key])
body = json.dumps(output)
return Response(content_type='application/json', body=body)
阅读(5930) | 评论(0) | 转发(0) |