RYU 灭龙战 fourth day (2)

前言

之前试过在ODL调用他们的rest api,一直想自己写一个基于ODL的rest api,结果还是无果而终。这个小目标却在RYU身上实现了。今日说法,为你带来,基于RYU的北向rest api开发

目的

  • mac地址表获取 API

    取得基于RYU 灭龙战 third day实验的mac地址表内容。即 对应的mac地址和连接端口 以JSON的形式回传

  • mac地址表注册 API

    向mac地址表加入新的mac地址和端口号,同时加到交换机的流表中

实验方案

附录源码

# 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. import json from ryu.app import simple_switch_13
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.app.wsgi import ControllerBase
from ryu.app.wsgi import Response
from ryu.app.wsgi import route
from ryu.app.wsgi import WSGIApplication
from ryu.lib import dpid as dpid_lib simple_switch_instance_name = 'simple_switch_api_app'
url = '/simpleswitch/mactable/{dpid}' #SimpleSwitchRest13用来扩展实验一的功能,让它可以更新mac地址表,其中switch_features_handler方法由于需要更新mac地址表,所以这个地方继承原方法,进行重写
class SimpleSwitchRest13(simple_switch_13.SimpleSwitch13): #指定RYU使用的为WSGI网页服务器
_CONTEXTS = {'wsgi': WSGIApplication} def __init__(self, *args, **kwargs):
super(SimpleSwitchRest13, self).__init__(*args, **kwargs)
#已连接交换机集合
self.switches = {}
wsgi = kwargs['wsgi']
wsgi.register(SimpleSwitchController,
{simple_switch_instance_name: self}) #接收消息为OPFSwitchFeatures,交换机状态为接收SwitchFeatures消息
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
#继承原方法
super(SimpleSwitchRest13, self).switch_features_handler(ev)
datapath = ev.msg.datapath
self.switches[datapath.id] = datapath
self.mac_to_port.setdefault(datapath.id, {}) def set_mac_to_port(self, dpid, entry):
mac_table = self.mac_to_port.setdefault(dpid, {})
datapath = self.switches.get(dpid) #entry用来存储已经注册的mac地址和端口
entry_port = entry['port']
entry_mac = entry['mac'] if datapath is not None:
parser = datapath.ofproto_parser
if entry_port not in mac_table.values(): for mac, port in mac_table.items(): # from known device to new device
actions = [parser.OFPActionOutput(entry_port)]
match = parser.OFPMatch(in_port=port, eth_dst=entry_mac)
self.add_flow(datapath, 1, match, actions) # from new device to known device
actions = [parser.OFPActionOutput(port)]
match = parser.OFPMatch(in_port=entry_port, eth_dst=mac)
self.add_flow(datapath, 1, match, actions) mac_table.update({entry_mac: entry_port})
return mac_table #SimpleSwitchController用来实现收到HTTP请求时所需要回应的方法
class SimpleSwitchController(ControllerBase): def __init__(self, req, link, data, **config):
super(SimpleSwitchController, self).__init__(req, link, data, **config)
self.simple_switch_app = data[simple_switch_instance_name] #参数说明,第一个参数任意名称,第二个参数url,指定url,使得对应的url为http://<IP>:8080/simpleswitch/mactable/<datapath ID>,第三个参数为GET方法,
# 第四个参数为指定的URL形式,即simpleswitch/mactable/<datapath ID>的<datapapath ID>要和目标文件的值相对应
@route('simpleswitch', url, methods=['GET'],
requirements={'dpid': dpid_lib.DPID_PATTERN})
def list_mac_table(self, req, **kwargs): simple_switch = self.simple_switch_app
dpid = dpid_lib.str_to_dpid(kwargs['dpid'])
#如果dpid不在表中的话,就会返回404
if dpid not in simple_switch.mac_to_port:
return Response(status=404)
#把对应的dpid对应的mac地址表用json的形式返回
mac_table = simple_switch.mac_to_port.get(dpid, {})
body = json.dumps(mac_table)
return Response(content_type='application/json', body=body) #同上
@route('simpleswitch', url, methods=['PUT'],
requirements={'dpid': dpid_lib.DPID_PATTERN})
def put_mac_table(self, req, **kwargs): simple_switch = self.simple_switch_app
dpid = dpid_lib.str_to_dpid(kwargs['dpid'])
try:
new_entry = req.json if req.body else {}
except ValueError:
raise Response(status=400) if dpid not in simple_switch.mac_to_port:
return Response(status=404) #调用set_mac_to_port方法,注册相应的mac,port,并下发流表
try:
mac_table = simple_switch.set_mac_to_port(dpid, new_entry)
body = json.dumps(mac_table)
return Response(content_type='application/json', body=body)
except Exception as e:
return Response(status=500)

实验过程

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

  • 另外一个终端,目录为ryu/app
ryu-manager --verbose ./simple_switch_rest_13.py

  • mininet端,让h1 ping h2 ,看RYU端的变化
h1 ping -c1 h2

  • 使用curl调用rest api进行获取mac地址表

  • 使用curl调用rest api进行mac地址表的注册

  • 在OVS上查看流表

可以看得到刚刚PUT的请求,也转化为流表的形式下发了

总结

学习到这里,基本了解一些RYU的一些大概框架和结构,知道如何相应事件去做相应的处理。接下来我将

  • 学一下RYU对LLDP报文的机制
  • 对该机制进行这3天来的实验汇总,功能包括对LLDP报文做相应处理,并在这基础上增加rest api的调用

RYU 灭龙战 fourth day (2)的更多相关文章

  1. RYU 灭龙战 fourth day (1)

    RYU 灭龙战 fourth day (1) 前言 对于流量的监控,对于一个网络管理人员来说是非常重要的,可以从可视化的角度,方便检测出哪里的设备出了问题:而在传统网络中,如果是哪里的设备出了问题的话 ...

  2. RYU 灭龙战 third day

    RYU 灭龙战 third day 前言 传统的交换机有自学习能力.然而你知道在SDN的世界里,脑子空空的OpenFlow交换机是如何学习的吗?今日说法带你领略SDN的mac学习能力. RYUBook ...

  3. RYU 灭龙战 second day(内容大部分引自网络)

    RYU 灭龙战 second day(内容大部分引自网络) 写好的markdown重启忘了保存...再写一次RLG 巨龙的稀有装备-RYU代码结构 RYU控制器代码结构的总结 RYU入门教程 RYU基 ...

  4. RYU 灭龙战 first day

    RYU 灭龙战 first day 前言 由于RYU翻译过来是龙的意思,此次主题就叫灭龙战吧 灵感来源 恶龙的三位真火-问题所在 参照了官方文档的基本操作 笔者以此执行 一个终端里 sudo mn - ...

  5. mininet和ryu控制器的连接

    1.执行ryu应用程式:ryu-manager --verbose ryu.app.simple_switch_13 2.启动mininet,配置如下:创建3个host,1个交换器(open vSwi ...

  6. Ubuntu下搭建ryu环境

    RYU环境搭建总共四步: step1:首先下载相应的python套件,并且更新pip $ sudo apt-get install python-pip python-dev build-essent ...

  7. Ryu

    What's Ryu? Ryu is a component-based software defined networking framework. Ryu provides software co ...

  8. PIC12F629帮我用C语言写个程序,控制三个LED亮灭

    http://power.baidu.com/question/240873584599025684.html?entry=browse_difficult PIC12F629帮我用C语言写个程序,控 ...

  9. (三)开关检测来控制LED灯的亮灭

    开关检测案例一: 具体电路图如下: K1--K4闭合,控制 D1—D4 亮灭 产生的问题: 1.关于 R8 R9 R7 R10 的阻值选择问题,倘若太大的话,  比如10K 不管开关断开还是闭合,好像 ...

随机推荐

  1. Universal-Image-Loader源码分析(一)——ImageLoaderConfiguration分析

    UIl与Volley一样是非常古老的框架,UIL实现了从网络获取图片,对图片进行缓存,以及根据个性化的设置来将图片加载到ImageView上. 这篇文章 主要分析UIl在初始化配置的源码 UIL初始化 ...

  2. Redis系列六:redis相关功能

    一. 慢查询原因分析 与mysql一样:当执行时间超过阀值,会将发生时间耗时的命令记录 redis命令生命周期:发送 排队 执行 返回慢查询只统计第3个执行步骤的时间 预设阀值:两种方式,默认为10毫 ...

  3. 使用iozone进行磁盘读写性能测试

    对于很多GIS工程师,经常需要对系统的磁盘性能进行测试,为了排查问题或者帮助用户进行系统设计. IOZone这个磁盘性能测试工具就是一个很方便的辅助工具. 下面就以个测试共享目录的读写性能为例,说明其 ...

  4. 如何利用Grunt生成对应的Source Map文件,线上代码压缩使用chrome浏览器便于调式

    如何利用Grunt生成对应的Source Map文件,线上代码压缩使用chrome浏览器便于调式 首先我们来说说为何要生成sourceMap文件呢?简单的说,sourceMap是为了压缩后的代码调式提 ...

  5. PAT B1022 D进制的A+B (20 分)

    输入两个非负 10 进制整数 A 和 B (≤),输出 A+B 的 D (1)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 D. 输出格式: 输出 A+B 的 D 进制数. 输入 ...

  6. 【转】软件质量之道:SourceMonitor

    转:https://blog.csdn.net/feng_ma_niu/article/details/40594799 SourceMonitor是一个源代码衡量工具,由http://www.cam ...

  7. TTL,COMS,USB,232,422,485电平之详细介绍及使用

    如有错误敬请指导! 今天来详细介绍一下TTL,COMS,USB,232,422,485电平,以及之间的转换问题. 有些地方的引脚图可能不是规范的,具体引脚以自己的模块资料为主,这篇文章着重介绍使用.. ...

  8. 详解分布式应用程序协调服务Zookeeper

    主从结构:HDFS.Yarn.HBase.storm.spark.zookeeper都存在单点故障问题 hadoop1.x没有解决方案 hadoop2.x利用zookeeper实现HA zookeep ...

  9. struts2_E_commerce_maven

    这是作业的第二题:使用struts实现电子商务网站,这是基于之前的代码的,所以,主要就是修改成为struts的mvc模式. 1.开始,先把以前做的一个maven项目修改成为另一个项目(简称重命名) 重 ...

  10. mfc 进程的优先级

    知识点:  进程优先级  获取当前进程句柄  优先级设置  优先级变动  优先级获取 一.进程优先级(优先级等级) 简单的说就是进程(线程)的优先级越高,那么就可以分占相对多的CPU时间片. ...