RYU 灭龙战 fourth day (1)

前言

对于流量的监控,对于一个网络管理人员来说是非常重要的,可以从可视化的角度,方便检测出哪里的设备出了问题;而在传统网络中,如果是哪里的设备出了问题的话,则需要进行人工的排查,这种排查往往绝大部分依赖于经验上,这也是SDN一个小小的好处吧。这次实验就基于上次实验的基础上,加入流量监控。

实现方案

附录源码

  • 交换机状态

# Copyright (C) 2016 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 operator import attrgetter from ryu.app import simple_switch_13
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, DEAD_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.lib import hub #继承simple_switch_13,OpenFlow1.3 mac学习
class SimpleMonitor13(simple_switch_13.SimpleSwitch13): #初始化函数
def __init__(self, *args, **kwargs):
super(SimpleMonitor13, self).__init__(*args, **kwargs)
#数据路径的集合
self.datapaths = {}
#每次流表监控的时间戳
self.monitor_thread = hub.spawn(self._monitor) #消息类型StateChange,交换机的状态为一般状态(MAIN_DISPATCHER) 或者连接中断状态(DEAD_DISPATCHER)
@set_ev_cls(ofp_event.EventOFPStateChange,
[MAIN_DISPATCHER, DEAD_DISPATCHER])
def _state_change_handler(self, ev):
#得到数据路径
datapath = ev.datapath
#如果交换机状态为一般连接状态,并且该消息的数据路径id不在数据路径集合中,则把该数据路径id加入数据路径集合中
if ev.state == MAIN_DISPATCHER:
if datapath.id not in self.datapaths:
self.logger.debug('register datapath: %016x', datapath.id)
self.datapaths[datapath.id] = datapath
#如果交换机状态为断开链接状态,且该消息的数据路径在数据路径集合中,则把该数据路径id从数据路径集合中去除
elif ev.state == DEAD_DISPATCHER:
if datapath.id in self.datapaths:
self.logger.debug('unregister datapath: %016x', datapath.id)
del self.datapaths[datapath.id] #对于在数据路径集合中的交换机每间隔10s发送请求,来获得交换机的消息
def _monitor(self):
while True:
for dp in self.datapaths.values():
self._request_stats(dp)
hub.sleep(10) #_request_stats方法用来定期发送,OFPFlowStatsRequest、OFPPortStatsRequest消息给交换机
def _request_stats(self, datapath):
self.logger.debug('send stats request: %016x', datapath.id)
ofproto = datapath.ofproto
parser = datapath.ofproto_parser req = parser.OFPFlowStatsRequest(datapath)
datapath.send_msg(req) req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY)
datapath.send_msg(req) #用来处理EventOFPFlowStatsReply数据包,在交换机状态为一般链接状态
@set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
def _flow_stats_reply_handler(self, ev):
body = ev.msg.body self.logger.info('datapath '
'in-port eth-dst '
'out-port packets bytes')
self.logger.info('---------------- '
'-------- ----------------- '
'-------- -------- --------') #输出消息的数据路径id,入端口,目的mac,出端口,packets数目,以及byte大小,并按入端口,目的mac作为排序
for stat in sorted([flow for flow in body if flow.priority == 1],
key=lambda flow: (flow.match['in_port'],
flow.match['eth_dst'])):
self.logger.info('%016x %8x %17s %8x %8d %8d',
ev.msg.datapath.id,
stat.match['in_port'], stat.match['eth_dst'],
stat.instructions[0].actions[0].port,
stat.packet_count, stat.byte_count) # 用来处理EventOFPPortStatsReply数据包,在交换机状态为一般链接状态
@set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
def _port_stats_reply_handler(self, ev):
body = ev.msg.body self.logger.info('datapath port '
'rx-pkts rx-bytes rx-error '
'tx-pkts tx-bytes tx-error')
self.logger.info('---------------- -------- '
'-------- -------- -------- '
'-------- -------- --------')
#打印出对应的数据的数据路径id,以及交换机端口,以及接收数据包数目,接收字节数目,接收数据错误率,以及相应的发送数据包数目,发送字节数目,发送错误数
for stat in sorted(body, key=attrgetter('port_no')):
self.logger.info('%016x %8x %8d %8d %8d %8d %8d %8d',
ev.msg.datapath.id, stat.port_no,
stat.rx_packets, stat.rx_bytes, stat.rx_errors,
stat.tx_packets, stat.tx_bytes, stat.tx_errors)

实验过程

  • 打开一终端,打开mininet
sudo mn --topo single,3 --mac --switch ovsk,protocols=OpenFlow13 --controller remote

此时还没有执行ryu应用,所以对应的6633端口无应用相应,在wireshark查看可得各种TCP握手失败

  • 另一种 执行ryu应用,目录为.../ryu/app
ryu-manager --verbose ./simple_monitor_13.py

上次实验可知,此时还没有优先级大于0的流表,所以没有显示出来。此时模仿上次一样h1 ping h2让其下发流表

  • mininet端口互ping,触发控制器下发流表
h1 ping h2

可以看到流表增加了,可看到实时的流量检测

总结

基于上次实验的扩展吧

利用OpenFlow的相应消息可以做的一些特定功能

最新文章

  1. SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论
  2. r.js结合gulp等于webpack(angular为例)
  3. 玩KVM
  4. 深入浅出Mybatis系列(三)---配置详解之properties与environments(mybatis源码篇)
  5. blogilo在chinaunix发布博客的设置
  6. spring笔记4 spring MVC的基础知识4
  7. ado.net增删改查练习
  8. 容易网CEO陈从容:用分享成就生活之美
  9. js中字符串转换为日期和比较大小
  10. 前端开发利器—FIDDLER 转
  11. WPF中StringFormat 格式化 的用法
  12. 路由器安装ubuntu-16.04.1-server-amd64出现“无法安装busybox-initramfs”错误。向目标系统中安装busybox-initramfs软件包时出现一个错误。请检查/var/log/syslog或查看第四虚拟控制台以获得详细
  13. css笔记-文本样式
  14. tpshop使用自带极光推送
  15. sort函数比较cmp写法
  16. 定时调度系列之Quartz.Net详解(转)
  17. java实验报告一
  18. windows下Anaconda3配置TensorFlow深度学习库
  19. php--------删除一个路径下的所有文件夹和文件
  20. Squid普通代理&&透明代理&&反向代理学习

热门文章

  1. BZOJ2580:[USACO]Video Game(AC自动机,DP)
  2. 3130: [Sdoi2013]费用流
  3. Kubernetes1.91(K8s)安装部署过程(六)--node节点部署
  4. Android failed to start daemon
  5. awk、sed处理文件的简单例子
  6. 深入解析Java中的装箱和拆箱
  7. Maven私有仓库-使用docker部署Nexus
  8. python利用beautifulSoup写爬虫
  9. mfc 基类与子类
  10. 模拟赛 sutoringu