from pox.core import core
from pox.lib.util import dpid_to_str
import pox.openflow.libopenflow_01 as of
import pox.lib.packet as pkt
from pox.lib.addresses import IPAddr, EthAddr
from random import randrange
from pox.lib.recoco import Timer
log = core.getLogger()
flows = 0 #flows counts the number of flows we want there in the end it’s defined as a global VAR
class testFlows (object):
def __init__ (self):
core.openflow.addListeners(self)
def _handle_ConnectionUp (self, event): # as soon as a switch starts up we’re going to start writing random flows to it
log.debug(“Switch %s has come up.”, dpid_to_str(event.dpid))
def pushflows():
global flows
if flows < 100: #Change this number to the maximum number of flows you want installed
print “starting timer in 1secs” #you can take or leave this one… just a test
for x in range(0, 10): # set the number of rules that you want per second to the second number in the range
macaddress = ’01:23:45:’+hex(randrange(16,255))[2:]+’:’+hex(randrange(16,255))[2:]+’:’+hex(randrange(16,255))[2:] # create a random mac. random number 16-255 and then convert to hex
port = randrange(2,5) # assign to random port in a range
msg = of.ofp_flow_mod()
#msg.priority = 32768 # set priority
msg.match.dl_dst = EthAddr(macaddress) # match our random mac address to a destination port
msg.actions.append(of.ofp_action_output(port = port)) # set destination port
event.connection.send(msg) # send our flow out
log.debug(“installing flow for destination of %s” % (macaddress)) # log it
flows = flows + 1
Timer(1, pushflows, recurring = True) #timer is set to 1 second
def launch ():
core.registerNew(testFlows) # startup and run class