Chinaunix首页 | 论坛 | 博客
  • 博客访问: 809821
  • 博文数量: 626
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3897
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
  • 认证徽章:
个人简介

To be a better coder

文章分类

全部博文(626)

文章存档

2019年(181)

2018年(217)

2017年(147)

2016年(82)

分类: LINUX

2019-10-08 17:02:47

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
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# 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)



阅读(936) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册