0

首先,我是 python 新手!最近我在代码中遇到了一些问题。这是我将两个流规则安装到多个开关中的代码:

    import inspect
    from pox.core import core
    import pox.openflow.libopenflow_01 as of
    from pox.lib.revent import *
    from pox.lib.util import dpidToStr
    from pox.lib.addresses import EthAddr, IPAddr
    import pox.lib.packet as pkt
    from collections import namedtuple
    import os
    import csv
    from csv import DictReader
    import time

    log = core.getLogger()
    FirewallPolicies = "%s/pox/pox/misc/firewall-policies.csv" % os.environ[ 'HOME' ]

    class CustomFirewall (EventMixin):

    def __init__ (self):
    self.listenTo(core.openflow)
    log.debug("Enabling Firewall Module")

    def _handle_ConnectionUp (self, event):
    ''' Add your logic here ... '''
    ReadFile = open(FirewallPolicies, 'r')
    ReaderFile = csv.DictReader(ReadFile)
    Deny = {}
    for row in ReaderFile:
            Deny[row['id']] = ({'mac_0':row['mac_0'],'mac_1':row['mac_1']})
    log.debug("Deny table - %s",Deny)
    for x in Deny.values():
            log.debug("Source Mac is %s",x['mac_0'])
            log.debug("Destination Mac is %s",x['mac_1'])
            log.debug("1")
            match = of.ofp_match(dl_src = x['mac_0'], dl_dst = x['mac_1'])
            log.debug("2")
            fm = of.ofp_flow_mod()
            fm.priority = 20
            fm.match = match
            event.connection.send(fm)
            log.debug("Firewall rules installed on %s", dpidToStr(event.dpid))

    def launch ():
    '''
    Starting the Firewall module
    '''
    log.debug("Jyoti's Custom firewall launched")
    core.registerNew(CustomFirewall)

问题:我能够将第一条规则安装到交换机中,但无法安装第二条规则。

错误消息:DEBUG:misc.Custom_firewall_2:Source Mac is 00:00:00:00:00:01

DEBUG:misc.Custom_firewall_2:Destination Mac is 00:00:00:00:00:02

调试:misc.Custom_firewall_2:1

调试:misc.Custom_firewall_2:2

DEBUG:misc.Custom_firewall_2:Firewall rules installed on 00-00-00-00-00-09 ->安装第一条规则

DEBUG:misc.Custom_firewall_2:源 Mac 是 00:00:00:00:00:04

DEBUG:misc.Custom_firewall_2:Destination Mac 是 00:00:00:00:00:03

调试:misc.Custom_firewall_2:1

调试:misc.Custom_firewall_2:2

DEBUG:openflow.of_01:[00-00-00-00-00-09 33] 套接字错误:对等方重置连接 ->第二条规则安装失败

信息:openflow.of_01:[00-00-00-00-00-09 33] 已断开连接

DEBUG:misc.Custom_firewall_2:防火墙规则安装在 00-00-00-00-00-09

错误:openflow.of_01:[00-00-00-00-00-0f 37] OpenFlow

错误:[00-00-00-00-00-0f 37]

错误:标题:[00-00-00-00-00-0f 37]

错误:版本:1 [00-00-00-00-00-0f 37]

错误:类型:1(OFPT_ERROR)

...

...

... 很快

笔记:

如果我使用 self.connection.send(fm) 我得到:

    self.connection.send(fm)
    AttributeError: 'CustomFirewall' object has no attribute 'connection'

如果我使用 event.connection.send(fm) 我得到连接重置问题

我不确定是什么导致了这个问题。谁能帮我解决这个问题?

4

1 回答 1

0

您对流没有任何操作。所以我假设你想丢弃这些数据包对吗?

尝试放这样的东西

packet = event.parsed
msg = of.ofp_flow_mod()
msg.match = of.ofp_match.from_packet(packet)
msg.idle_timeout = 10
msg.hard_timeout = 30
msg.buffer_id = event.ofp.buffer_id
msg.data = event.ofp # 6a
event.connection.send(fm)

在 for 循环体中。只是看看您是否可以向开关添加任何流(即不使用 Deny 类)。

我建议使用 Ryu 而不是 pox。POX 只支持 OF1.0 而 RYU 支持到 1.4 并且有更好的文档。

于 2015-08-26T19:01:42.283 回答