POX 控制器之 ofp_stats_reply 消息结构

收到 ofp_stats_reply 数据包,但 POX 框架相关教程很少,没有解析方法教学。其实可以直接用 print 方法打印出来,POX 在这方面的支持很友好,格式缩进什么的也很体贴。

如下是 MAC 地址为 00-00-00-00-00-04,DPID 为 9 的交换机上报的 ofp_stats_reply 消息:

[00-00-00-00-00-04 9] ofp_stats_reply
[00-00-00-00-00-04 9]   header: 
[00-00-00-00-00-04 9]     version: 1
[00-00-00-00-00-04 9]     type:    17 (OFPT_STATS_REPLY)
[00-00-00-00-00-04 9]     length:  396
[00-00-00-00-00-04 9]     xid:     330
[00-00-00-00-00-04 9]   type: 1
[00-00-00-00-00-04 9]   flags: 0
[00-00-00-00-00-04 9]   body:
[00-00-00-00-00-04 9]       length: 96
[00-00-00-00-00-04 9]       table_id: 0
[00-00-00-00-00-04 9]       match: 
[00-00-00-00-00-04 9]         wildcards: nw_tos|tp_dst|dl_dst|dl_src|dl_vlan_pcp|dl_vlan|tp_src (1100000000000011001110 = 3000ce)
[00-00-00-00-00-04 9]         in_port: 2
[00-00-00-00-00-04 9]         dl_type: 0x800
[00-00-00-00-00-04 9]         nw_proto: 17
[00-00-00-00-00-04 9]         nw_src: 10.0.0.11
[00-00-00-00-00-04 9]         nw_dst: 10.0.0.8
[00-00-00-00-00-04 9]       duration_sec: 58
[00-00-00-00-00-04 9]       duration_nsec: 696000000
[00-00-00-00-00-04 9]       priority: 3
[00-00-00-00-00-04 9]       idle_timeout: 0
[00-00-00-00-00-04 9]       hard_timeout: 0
[00-00-00-00-00-04 9]       cookie: 0
[00-00-00-00-00-04 9]       packet_count: 0
[00-00-00-00-00-04 9]       byte_count: 0
[00-00-00-00-00-04 9]       actions: 
[00-00-00-00-00-04 9]         type: 0
[00-00-00-00-00-04 9]         len: 8
[00-00-00-00-00-04 9]         port: 4
[00-00-00-00-00-04 9]         max_len: 0
[00-00-00-00-00-04 9] 
[00-00-00-00-00-04 9]       length: 96
[00-00-00-00-00-04 9]       table_id: 0
[00-00-00-00-00-04 9]       match: 
[00-00-00-00-00-04 9]         wildcards: nw_tos|tp_dst|dl_dst|dl_src|dl_vlan_pcp|dl_vlan|tp_src (1100000000000011001110 = 3000ce)
[00-00-00-00-00-04 9]         in_port: 4
[00-00-00-00-00-04 9]         dl_type: 0x800
[00-00-00-00-00-04 9]         nw_proto: 17
[00-00-00-00-00-04 9]         nw_src: 10.0.0.8
[00-00-00-00-00-04 9]         nw_dst: 10.0.0.11
[00-00-00-00-00-04 9]       duration_sec: 58
[00-00-00-00-00-04 9]       duration_nsec: 656000000
[00-00-00-00-00-04 9]       priority: 3
[00-00-00-00-00-04 9]       idle_timeout: 0
[00-00-00-00-00-04 9]       hard_timeout: 0
[00-00-00-00-00-04 9]       cookie: 0
[00-00-00-00-00-04 9]       packet_count: 0
[00-00-00-00-00-04 9]       byte_count: 0
[00-00-00-00-00-04 9]       actions: 
[00-00-00-00-00-04 9]         type: 0
[00-00-00-00-00-04 9]         len: 8
[00-00-00-00-00-04 9]         port: 2
[00-00-00-00-00-04 9]         max_len: 0
[00-00-00-00-00-04 9] 
[00-00-00-00-00-04 9]       length: 96
[00-00-00-00-00-04 9]       table_id: 0
[00-00-00-00-00-04 9]       match: 
[00-00-00-00-00-04 9]         wildcards: nw_tos|tp_dst|dl_dst|dl_src|dl_vlan_pcp|dl_vlan|tp_src (1100000000000011001110 = 3000ce)
[00-00-00-00-00-04 9]         in_port: 4
[00-00-00-00-00-04 9]         dl_type: 0x800
[00-00-00-00-00-04 9]         nw_proto: 17
[00-00-00-00-00-04 9]         nw_src: 10.0.0.19
[00-00-00-00-00-04 9]         nw_dst: 10.0.0.12
[00-00-00-00-00-04 9]       duration_sec: 58
[00-00-00-00-00-04 9]       duration_nsec: 621000000
[00-00-00-00-00-04 9]       priority: 3
[00-00-00-00-00-04 9]       idle_timeout: 0
[00-00-00-00-00-04 9]       hard_timeout: 0
[00-00-00-00-00-04 9]       cookie: 0
[00-00-00-00-00-04 9]       packet_count: 0
[00-00-00-00-00-04 9]       byte_count: 0
[00-00-00-00-00-04 9]       actions: 
[00-00-00-00-00-04 9]         type: 0
[00-00-00-00-00-04 9]         len: 8
[00-00-00-00-00-04 9]         port: 3
[00-00-00-00-00-04 9]         max_len: 0
[00-00-00-00-00-04 9] 
[00-00-00-00-00-04 9]       length: 96
[00-00-00-00-00-04 9]       table_id: 0
[00-00-00-00-00-04 9]       match: 
[00-00-00-00-00-04 9]         wildcards: nw_tos|tp_dst|dl_dst|dl_src|dl_vlan_pcp|dl_vlan|tp_src (1100000000000011001110 = 3000ce)
[00-00-00-00-00-04 9]         in_port: 3
[00-00-00-00-00-04 9]         dl_type: 0x800
[00-00-00-00-00-04 9]         nw_proto: 17
[00-00-00-00-00-04 9]         nw_src: 10.0.0.12
[00-00-00-00-00-04 9]         nw_dst: 10.0.0.19
[00-00-00-00-00-04 9]       duration_sec: 58
[00-00-00-00-00-04 9]       duration_nsec: 621000000
[00-00-00-00-00-04 9]       priority: 3
[00-00-00-00-00-04 9]       idle_timeout: 0
[00-00-00-00-00-04 9]       hard_timeout: 0
[00-00-00-00-00-04 9]       cookie: 0
[00-00-00-00-00-04 9]       packet_count: 0
[00-00-00-00-00-04 9]       byte_count: 0
[00-00-00-00-00-04 9]       actions: 
[00-00-00-00-00-04 9]         type: 0
[00-00-00-00-00-04 9]         len: 8
[00-00-00-00-00-04 9]         port: 4
[00-00-00-00-00-04 9]         max_len: 0

数据包主要分为四部分:

  • header
  • type
  • flags
  • body:是一个 list,有多少条流表就包含多少个元素,应使用 for 循环依次解析。

接下来依次分析这四个部分。

header

header 内包含四个字段:

  • version:OpenFlow 版本号,上例中为 1(0x01) 即为 OpenFlow 1.0 版本,后续版本 1.1、1.2、1.3 的 version 值递增,即为 2、3、4。
  • type:OpenFlow 消息类型,定义在枚举类 ofp_type 中,主要包括:

enum ofp_type {
/* Immutable messages. /
OFPT_HELLO, /
Symmetric message /
OFPT_ERROR, /
Symmetric message /
OFPT_ECHO_REQUEST, /
Symmetric message /
OFPT_ECHO_REPLY, /
Symmetric message /
OFPT_VENDOR, /
Symmetric message /
/
Switch configuration messages. /
OFPT_FEATURES_REQUEST, /
Controller/switch message /
OFPT_FEATURES_REPLY, /
Controller/switch message /
OFPT_GET_CONFIG_REQUEST, /
Controller/switch message /
OFPT_GET_CONFIG_REPLY, /
Controller/switch message /
OFPT_SET_CONFIG, /
Controller/switch message /
/
Asynchronous messages. /
OFPT_PACKET_IN, /
Async message /
OFPT_FLOW_REMOVED, /
Async message /
OFPT_PORT_STATUS, /
Async message /
/
Controller command messages. /
OFPT_PACKET_OUT, /
Controller/switch message /
OFPT_FLOW_MOD, /
Controller/switch message /
OFPT_PORT_MOD, /
Controller/switch message /
/
Statistics messages. /
OFPT_STATS_REQUEST, /
Controller/switch message /
OFPT_STATS_REPLY, /
Controller/switch message /
/
Barrier messages. /
OFPT_BARRIER_REQUEST, /
Controller/switch message /
OFPT_BARRIER_REPLY, /
Controller/switch message /
/
Queue Configuration messages. /
OFPT_QUEUE_GET_CONFIG_REQUEST, /
Controller/switch message /
OFPT_QUEUE_GET_CONFIG_REPLY /
Controller/switch message */
};

  • length:消息总长度,包含头部,单位为字节。
  • xid:事件 ID,同一事件的 ID一致。如 ofp_stats_request 和对应的 ofp_stats_reply 就使用同一个事件 ID。

type

type 表示这个 ofp_stats_request/ofp_stats_reply 消息请求/回应的统计类型,决定如何解析 body 字段。类型如表所示:

取值 消息类型 说明 请求 body 回复 body
0 OFPMP_DESC 整体统计信息 ofp_desc_stats(制造商、软硬件描述、序列号、可读的数据通道描述)
1 OFPMP_FLOW 单流请求信息 ofp_aggregate_stats_request(要读的 TABLE_ID 或 OFPPT_ALL 代表读所有表) ofp_aggregate_stats_reply(这个项的长度、TABLE ID、流已生成的时间、超过已生成时间的时间、优先级、硬超时时间、未命中时间、包数目、字节数)
2 OFPMP_AGGREGATE 多流请求信息 ofp_aggregate_stats_request(要读的 TABLE_ID) ofp_aggregate_stats_replay(包数目、字节数、流数目)
3 OFPMP_TABLE 流表请求信息 ofp_table_stats(表的标识符、通配符、最大支持的流表项、活跃流表项的数量、被查找表的数量、匹配表的数量)
4 FPMP_PORT_STATS 端口信息请求 fp_port_stats_request(端口号或 OFPP_ANY 代表所有端口) ofp_port_stats(收到包的数量、已传送包的数量、收到的字节数、传送的字节数、被 RX 丢弃的包数、被 RX 丢弃的包数、接收到错误的包数、帧调整的错误数量、在 RX 溢出的数据包数、CRC 错误数、冲突数量)
5 OFPMP_QUEUE 队列请求信息 - -
6 - vendor 请求信息 - -

本例中的类型号为 1,代表是单流请求信息。

flags

未定义。

body

已在 type 部分介绍,需按照类型解析。

编程实现 POX 控制器发送 ofp_stats_request 和接收 ofp_stats_reply 并解析

只粘贴相关部分。

def req_for_flow_stats():
    tik = 30
    while True:
        time.sleep(1)
        tik = (tik - 1 + 30) % 30
        if tik == 0:
            allCon = core.openflow.connections
            for con in allCon:
                con.send(of.ofp_stats_request(body=of.ofp_flow_stats_request()))

class DefaultOpenFlowHandlers (OpenFlowHandlers):
  """
  Basic OpenFlow message handling functionality

  There is generally a single instance of this class which is shared by all
  Connections.
  """
  @staticmethod
  def handle_STATS_REPLY (con, msg):
    e = con.ofnexus.raiseEventNoErrors(RawStatsReply, con, msg)
    if e is None or e.halt != True:
      con.raiseEventNoErrors(RawStatsReply, con, msg)
    con._incoming_stats_reply(msg)

    dpid = con.dpid
    if msg.type == of.OFPST_FLOW:
        for f in msg.body:
            print(f)
            pkts = f.packet_count
            byts = f.byte_count
            if input_pkts != 0:
                print("[byte, packet]: [", f.byte_count, f.packet_count, "]")
                print(dpid, ", Path Loss Rate =", (input_pkts - output_pkts) * 1.0 / input_pkts * 100, "%")

完整实现见LXiao2015's GitHub

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,372评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,368评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,415评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,157评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,171评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,125评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,028评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,887评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,310评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,533评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,690评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,411评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,004评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,812评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,693评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,577评论 2 353

推荐阅读更多精彩内容