首先我是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:来源Mac是00:00:00:00:00:01
DEBUG:misc.Custom_firewall_2:目的地Mac是00:00:00:00:00:02
DEBUG:misc.Custom_firewall_2:1
DEBUG:misc.Custom_firewall_2:2
DEBUG:misc.Custom_firewall_2:安装在00-00-00-00-00-09的防火墙规则 - > 已安装第一条规则
DEBUG:misc.Custom_firewall_2:来源Mac是00:00:00:00:00:04
DEBUG:misc.Custom_firewall_2:目的地Mac是00:00:00:00:00:03
DEBUG:misc.Custom_firewall_2:1
DEBUG:misc.Custom_firewall_2:2
DEBUG:openflow.of_01:[00-00-00--09-09 33]套接字错误:通过对等方重置连接 - > 第二条规则无法安装
INFO:openflow.of_01:[00-00-00--09-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)我会收到连接重置问题
我不确定导致此问题的原因。有人可以帮我这个吗?
答案 0 :(得分: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)
<循环体中的。只是为了看看你是否可以向交换机添加任何流(即不使用Deny类)。
我建议使用Ryu而不是pox。 POX仅支持OF1.0,而RYU支持1.4,并且具有更好的文档。