POX flow_stats2.py analysis by uestc_gremount

该程序是POX WIKI上的程序,我只是将统计的报文修改了以下,并做了对这个程序运行流程的分析:

1.程序从launch开始运行

2.监听2个事件,如果监听到“FlowStatsReceived”就交由_handle_flowstats_received()来处理,另一个事件同样道理

3.每5s执行以下_time_func(),这是一步实时监视的处理

4._time_func()启动后发送2个状态请求给所有的switch

5.如果监听到上面说的两个事件,就执行相应的handle部分,其中_handle_flowstats_received()部分会统计源IP为“10.0.0.1”的报文字节数,报文数和报文流数量,并将结果输出出来,可以在terminal上看到

ps: of_json的作用是将openflow的数据结构转化为json的友好数据结构


运行方法可以是:

terminal 1:  sudo python topo1.py    ---运行一个任意topo

terminal 2: ./pox.py forwarding.l2_learning flow.stats2  ---运行pox主程序,2层交换程序和流量监控程序


代码如下:

#!/usr/bin/python
# Copyright 2012 William Yu
# wyu@ateneo.edu
#
# This file is part of POX.
#
# POX is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# POX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with POX. If not, see <http://www.gnu.org/licenses/>.
#

"""
This is a demonstration file created to show how to obtain flow 
and port statistics from OpenFlow 1.0-enabled switches. The flow
statistics handler contains a summary of web-only traffic.
"""

# standard includes
from pox.core import core
from pox.lib.util import dpidToStr
import pox.openflow.libopenflow_01 as of

# include as part of the betta branch
from pox.openflow.of_json import *

log = core.getLogger()

# handler for timer function that sends the requests to all the
# switches connected to the controller.
def _timer_func ():
  for connection in core.openflow._connections.values():
    connection.send(of.ofp_stats_request(body=of.ofp_flow_stats_request()))
    connection.send(of.ofp_stats_request(body=of.ofp_port_stats_request()))
  log.debug("Sent %i flow/port stats request(s)", len(core.openflow._connections))

# handler to display flow statistics received in JSON format
# structure of event.stats is defined by ofp_flow_stats()
def _handle_flowstats_received (event):
  stats = flow_stats_to_list(event.stats) # wsc:this step is just for debugging, so it can ba deleted.
  log.debug("FlowStatsReceived from %s: %s", 
    dpidToStr(event.connection.dpid), stats)

  # Get number of bytes/packets in flows for web traffic only
  web_bytes = 0
  web_flows = 0
  web_packet = 0
  for f in event.stats:
    if f.match.nw_src == IPAddr("10.0.0.1"):
      web_bytes += f.byte_count
      web_packet += f.packet_count
      web_flows += 1
  log.info("Web traffic from %s: %s bytes (%s packets) over %s flows", 
    dpidToStr(event.connection.dpid), web_bytes, web_packet, web_flows)

# handler to display port statistics received in JSON format
def _handle_portstats_received (event):
  stats = flow_stats_to_list(event.stats)
  log.debug("PortStatsReceived from %s: %s", 
    dpidToStr(event.connection.dpid), stats)
    
# main functiont to launch the module
def launch ():
  from pox.lib.recoco import Timer

  # attach handsers to listners
  core.openflow.addListenerByName("FlowStatsReceived", 
    _handle_flowstats_received) 
  core.openflow.addListenerByName("PortStatsReceived", 
    _handle_portstats_received) 

  # timer set to execute every five seconds
  Timer(5, _timer_func, recurring=True)


Pox的文件夹下新建一个名为`SendFlowRuleSingle3.py`的Python文件,可以按照以下步骤操作: 1. **打开文本编辑器**: 打开你喜欢的文本编辑器,如`nano`, `vim`, `gedit`, 或者直接在终端中使用`python -m texteditor`创建新文件。 2. **创建文件结构**: 到达Pox的根目录(通常是`pox`文件夹),如果尚未存在,可以使用`cd`命令切换到该目录: ``` cd path/to/pox/ ``` 3. **创建新文件**: 使用编辑器创建一个新的Python文件,例如: ``` nano SendFlowRuleSingle3.py ``` 然后按`i`键进入插入模式,在这里输入你的代码内容。 4. **编写`SendFlowRuleSingle3.py`内容**: 在文件中,你可以定义Pox的控制器逻辑,例如发送单条OpenFlow流量规则。一个简单的示例可能是: ```python #!/usr/bin/env python """ This is an example of a simple flow rule sender in Pox. """ import pox.openflow.libopenflow_01 as of def _handle_ConnectionUp(event): conn = event.connection print("Connection up with switch %s" % str(conn)) # Replace the below line with your specific flow rule configuration msg = of.ofp_flow_mod( match=of.ofp_match(), command=of.OFPFC_ADD, priority=100, buffer_id=None, out_port=of.OFPP_NONE, out_group=of.OFPG_ANY, actions=[], ) conn.send(msg) def main(): pox.core.listenTo(_handle_ConnectionUp) print("SendFlowRuleSingle3 started") if __name__ == '__main__': main() ``` 把上述代码复制粘贴到文件里,然后保存并关闭编辑器。 5. **执行文件**: 使用`python`命令运行这个新的Python文件: ``` python SendFlowRuleSingle3.py ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值