Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def f(event):
if event.reason == openflow.OFPPR_ADD:
reason = openflow.OFPPR_ADD
elif event.reason == openflow.OFPPR_DELETE:
reason = openflow.OFPPR_DELETE
elif event.reason == openflow.OFPPR_MODIFY:
reason = openflow.OFPPR_MODIFY
else:
print 'port_status reason type %u unknown...just passing along' % event.reason
reason = event.reason
config = event.port['config']
state = event.port['state']
event.port['link'] = (state & openflow.OFPPS_LINK_DOWN) == 0
event.port['enabled'] = (config & openflow.OFPPC_PORT_DOWN) == 0
event.port['flood'] = (config & openflow.OFPPC_NO_FLOOD) == 0
ret = f.cb(event.datapath_id, reason, event.port)
def make_action_array(self, actions):
action_str = ""
for action in actions:
if action[0] == openflow.OFPAT_OUTPUT \
and isinstance(action[1],list) \
and len(action[1]) == 2:
a = struct.pack("!HHHH", action[0], 8,
action[1][1], action[1][0])
elif action[0] == openflow.OFPAT_SET_VLAN_VID:
a = struct.pack("!HHHH", action[0], 8, action[1], 0)
elif action[0] == openflow.OFPAT_SET_VLAN_PCP:
a = struct.pack("!HHBBH", action[0], 8, action[1], 0, 0)
elif action[0] == openflow.OFPAT_STRIP_VLAN:
a = struct.pack("!HHI", action[0], 8, 0)
elif action[0] == openflow.OFPAT_SET_DL_SRC \
or action[0] == openflow.OFPAT_SET_DL_DST:
eaddr = convert_to_eaddr(action[1])
if eaddr == None:
print 'invalid ethernet addr'
return None
a = struct.pack("!HH6sHI", action[0], 16, eaddr.binary_str(), 0, 0)
elif action[0] == openflow.OFPAT_SET_NW_SRC \
or action[0] == openflow.OFPAT_SET_NW_DST:
iaddr = convert_to_ipaddr(action[1])
if iaddr == None:
def send_flow_stats_request(self, dpid, match, table_id, xid=-1):
"""Send a flow stats request to a switch (dpid).
@param dpid - datapath/switch to contact
@param match - ofp_match structure
@param table_id - table to query
"""
# Create the stats request header
request = of.ofp_stats_request()
if xid == -1:
request.header.xid = c_htonl(long(self.sg.collection_epoch))
else:
request.header.xid = c_htonl(xid)
request.header.type = openflow.OFPT_STATS_REQUEST
request.type = openflow.OFPST_FLOW
request.flags = 0
# Create the stats request body
body = of.ofp_flow_stats_request()
body.match = match
body.table_id = table_id
body.out_port = openflow.OFPP_NONE
request.header.length = len(request.pack()) + len(body.pack())
self.send_openflow_command(dpid, request.pack() + body.pack())
def f(event):
if event.reason == openflow.OFPPR_ADD:
reason = openflow.OFPPR_ADD
elif event.reason == openflow.OFPPR_DELETE:
reason = openflow.OFPPR_DELETE
elif event.reason == openflow.OFPPR_MODIFY:
reason = openflow.OFPPR_MODIFY
else:
print 'port_status reason type %u unknown...just passing along' % event.reason
reason = event.reason
config = event.port['config']
state = event.port['state']
event.port['link'] = (state & openflow.OFPPS_LINK_DOWN) == 0
event.port['enabled'] = (config & openflow.OFPPC_PORT_DOWN) == 0
event.port['flood'] = (config & openflow.OFPPC_NO_FLOOD) == 0
ret = f.cb(event.datapath_id, reason, event.port)
if ret == None:
return CONTINUE
return ret
f.cb = handler
def send_flow_command(self, dp_id, command, attrs,
priority=openflow.OFP_DEFAULT_PRIORITY,
add_args=None,
hard_timeout=openflow.OFP_FLOW_PERMANENT,
xid=0):
m = set_match(attrs)
if m == None:
return False
if command == openflow.OFPFC_DELETE or command == openflow.OFPFC_DELETE_STRICT:
idle_timeout = 0
oactions = ""
buffer_id = UINT32_MAX
else:
(idle_timeout, actions, buffer_id) = add_args
oactions = self.make_action_array(actions)
if oactions == None:
return False
cookie = 0
return self.ctxt.send_flow_command(dp_id, command, m, idle_timeout, hard_timeout, oactions, buffer_id, priority, cookie, xid)
def f(event):
if event.reason == openflow.OFPR_NO_MATCH:
reason = openflow.OFPR_NO_MATCH
elif event.reason == openflow.OFPR_ACTION:
reason = openflow.OFPR_ACTION
else:
print 'packet_in reason type %u unknown...just passing along' % event.reason
reason = event.reason
if event.buffer_id == UINT32_MAX:
buffer_id = None
else:
buffer_id = event.buffer_id
try:
packet = ethernet(array.array('B', event.buf))
except IncompletePacket, e:
log.err('Incomplete Ethernet header',system='pyapi')
def send_openflow_buffer(self, dp_id, buffer_id, actions,
inport=openflow.OFPP_CONTROLLER):
"""
Tells a datapath to send out a buffer
dp_id - datapath to send packet to
buffer_id - id of buffer to send out
actions - list of actions or dp port to send out of
inport - dp port to mark as source (defaults to Controller port)
"""
if type(actions) == types.IntType:
self.ctxt.send_openflow_buffer_port(dp_id, buffer_id, actions,
inport)
elif type(actions) == types.ListType:
oactions = self.make_action_array(actions)
if oactions == None:
raise Exception('Bad action')
self.ctxt.send_openflow_buffer_acts(dp_id, buffer_id, oactions,
def f(event):
attrs = {}
attrs[core.N_BUFFERS] = event.n_buffers
attrs[core.N_TABLES] = event.n_tables
attrs[core.CAPABILITES] = event.capabilities
attrs[core.ACTIONS] = event.actions
attrs[core.PORTS] = event.ports
for i in range(0, len(attrs[core.PORTS])):
port = attrs[core.PORTS][i]
config = port['config']
state = port['state']
attrs[core.PORTS][i]['link'] = (state & openflow.OFPPS_LINK_DOWN) == 0
attrs[core.PORTS][i]['enabled'] = (config & openflow.OFPPC_PORT_DOWN) == 0
attrs[core.PORTS][i]['flood'] = (config & openflow.OFPPC_NO_FLOOD) == 0
ret = f.cb(event.datapath_id, attrs)
if ret == None:
return CONTINUE
return ret
f.cb = handler
"""
# Create the stats request header
request = of.ofp_stats_request()
if xid == -1:
request.header.xid = c_htonl(long(self.sg.collection_epoch))
else:
request.header.xid = c_htonl(xid)
request.header.type = openflow.OFPT_STATS_REQUEST
request.type = openflow.OFPST_FLOW
request.flags = 0
# Create the stats request body
body = of.ofp_flow_stats_request()
body.match = match
body.table_id = table_id
body.out_port = openflow.OFPP_NONE
request.header.length = len(request.pack()) + len(body.pack())
self.send_openflow_command(dpid, request.pack() + body.pack())
def send_flow_command(self, dp_id, command, attrs,
priority=openflow.OFP_DEFAULT_PRIORITY,
add_args=None,
hard_timeout=openflow.OFP_FLOW_PERMANENT):
m = set_match(attrs)
if m == None:
return False
if command == openflow.OFPFC_ADD:
(idle_timeout, actions, buffer_id) = add_args
oactions = self.make_action_array(actions)
if oactions == None:
return False
else:
idle_timeout = 0
oactions = ""
buffer_id = UINT32_MAX