loxi-prep: move message parsing to ofp.message
diff --git a/src/python/of10/__init__.py b/src/python/of10/__init__.py
index 2810be8..1de296a 100644
--- a/src/python/of10/__init__.py
+++ b/src/python/of10/__init__.py
@@ -5,7 +5,7 @@
They are checked in to simplify running OFTest.
"""
-__all__ = ["action_list", "action", "cstruct", "error", "message", "parse"]
+__all__ = ["action_list", "action", "cstruct", "error", "message"]
# Allow accessing constants through the top-level module
from cstruct import *
@@ -13,4 +13,3 @@
# Allow accessing submodules without additional imports
import action
import message
-import parse
diff --git a/src/python/of10/message.py b/src/python/of10/message.py
index b52d40f..994e227 100644
--- a/src/python/of10/message.py
+++ b/src/python/of10/message.py
@@ -1,6 +1,7 @@
# Python OpenFlow message wrapper classes
+import logging
from cstruct import *
from action_list import action_list
from error import *
@@ -3477,3 +3478,138 @@
_flow_mod.__init__(self, **kwargs)
self.command = OFPFC_DELETE_STRICT
+# These message types are subclassed
+msg_type_subclassed = [
+ OFPT_STATS_REQUEST,
+ OFPT_STATS_REPLY,
+ OFPT_ERROR
+]
+
+# Maps from sub-types to classes
+stats_reply_to_class_map = {
+ OFPST_DESC : desc_stats_reply,
+ OFPST_AGGREGATE : aggregate_stats_reply,
+ OFPST_FLOW : flow_stats_reply,
+ OFPST_TABLE : table_stats_reply,
+ OFPST_PORT : port_stats_reply,
+ OFPST_QUEUE : queue_stats_reply
+}
+
+stats_request_to_class_map = {
+ OFPST_DESC : desc_stats_request,
+ OFPST_AGGREGATE : aggregate_stats_request,
+ OFPST_FLOW : flow_stats_request,
+ OFPST_TABLE : table_stats_request,
+ OFPST_PORT : port_stats_request,
+ OFPST_QUEUE : queue_stats_request
+}
+
+error_to_class_map = {
+ OFPET_HELLO_FAILED : hello_failed_error_msg,
+ OFPET_BAD_REQUEST : bad_request_error_msg,
+ OFPET_BAD_ACTION : bad_action_error_msg,
+ OFPET_FLOW_MOD_FAILED : flow_mod_failed_error_msg,
+ OFPET_PORT_MOD_FAILED : port_mod_failed_error_msg,
+ OFPET_QUEUE_OP_FAILED : queue_op_failed_error_msg
+}
+
+# Map from header type value to the underlieing message class
+msg_type_to_class_map = {
+ OFPT_HELLO : hello,
+ OFPT_ERROR : error,
+ OFPT_ECHO_REQUEST : echo_request,
+ OFPT_ECHO_REPLY : echo_reply,
+ OFPT_VENDOR : vendor,
+ OFPT_FEATURES_REQUEST : features_request,
+ OFPT_FEATURES_REPLY : features_reply,
+ OFPT_GET_CONFIG_REQUEST : get_config_request,
+ OFPT_GET_CONFIG_REPLY : get_config_reply,
+ OFPT_SET_CONFIG : set_config,
+ OFPT_PACKET_IN : packet_in,
+ OFPT_FLOW_REMOVED : flow_removed,
+ OFPT_PORT_STATUS : port_status,
+ OFPT_PACKET_OUT : packet_out,
+ OFPT_FLOW_MOD : flow_mod,
+ OFPT_PORT_MOD : port_mod,
+ OFPT_STATS_REQUEST : stats_request,
+ OFPT_STATS_REPLY : stats_reply,
+ OFPT_BARRIER_REQUEST : barrier_request,
+ OFPT_BARRIER_REPLY : barrier_reply,
+ OFPT_QUEUE_GET_CONFIG_REQUEST : queue_get_config_request,
+ OFPT_QUEUE_GET_CONFIG_REPLY : queue_get_config_reply
+}
+
+def _of_message_to_object(binary_string):
+ """
+ Map a binary string to the corresponding class.
+
+ Appropriately resolves subclasses
+ """
+ hdr = ofp_header()
+ hdr.unpack(binary_string)
+ logging.info(hdr.show())
+ # FIXME: Add error detection
+ if not hdr.type in msg_type_subclassed:
+ return msg_type_to_class_map[hdr.type]()
+ if hdr.type == OFPT_STATS_REQUEST:
+ sub_hdr = ofp_stats_request()
+ sub_hdr.unpack(binary_string)
+ try:
+ obj = stats_request_to_class_map[sub_hdr.stats_type]()
+ except KeyError:
+ obj = None
+ return obj
+ elif hdr.type == OFPT_STATS_REPLY:
+ sub_hdr = ofp_stats_reply()
+ sub_hdr.unpack(binary_string)
+ try:
+ obj = stats_reply_to_class_map[sub_hdr.stats_type]()
+ except KeyError:
+ obj = None
+ return obj
+ elif hdr.type == OFPT_ERROR:
+ sub_hdr = ofp_error_msg()
+ sub_hdr.unpack(binary_string)
+ return error_to_class_map[sub_hdr.err_type]()
+ else:
+ logging.error("Cannot parse pkt to message")
+ return None
+
+def parse_message(binary_string):
+ """
+ Parse an OpenFlow packet
+
+ Parses a raw OpenFlow packet into a Python class, with class
+ members fully populated.
+
+ @param binary_string The packet (string) to be parsed
+ @param raw If true, interpret the packet as an L2 packet. Not
+ yet supported.
+ @return An object of some message class or None if fails
+ Note that any data beyond that parsed is not returned
+
+ """
+ obj = _of_message_to_object(binary_string)
+ if obj:
+ obj.unpack(binary_string)
+ return obj
+
+
+def parse_header(binary_string):
+ """
+ Parse only the header from an OpenFlow packet
+
+ Parses the header from a raw OpenFlow packet into a
+ an ofp_header Python class.
+
+ @param binary_string The packet (string) to be parsed
+ @param raw If true, interpret the packet as an L2 packet. Not
+ yet supported.
+ @return An ofp_header object
+
+ """
+ hdr = ofp_header()
+ hdr.unpack(binary_string)
+ return hdr
+
+
diff --git a/src/python/oftest/controller.py b/src/python/oftest/controller.py
index 944117a..88e0ffb 100644
--- a/src/python/oftest/controller.py
+++ b/src/python/oftest/controller.py
@@ -204,7 +204,7 @@
offset = 0
while offset < len(pkt):
# Parse the header to get type
- hdr = ofp.parse.of_header_parse(pkt[offset:])
+ hdr = ofp.message.parse_header(pkt[offset:])
if not hdr or hdr.length == 0:
self.logger.error("Could not parse header")
self.logger.error("pkt len %d." % len(pkt))
@@ -233,7 +233,7 @@
self.disconnect()
return
- msg = ofp.parse.of_message_parse(rawmsg)
+ msg = ofp.message.parse_message(rawmsg)
if not msg:
self.parse_errors += 1
self.logger.warn("Could not parse message")
diff --git a/src/python/oftest/parse.py b/src/python/oftest/parse.py
index ac76a16..0005313 100644
--- a/src/python/oftest/parse.py
+++ b/src/python/oftest/parse.py
@@ -1,14 +1,10 @@
"""
-OpenFlow message parsing functions
+Utility parsing functions
"""
import sys
import logging
-import message
-import error
-import action
-import cstruct
-from action_list import action_list
+import of10 as ofp
try:
import scapy.all as scapy
except:
@@ -17,161 +13,6 @@
except:
sys.exit("Need to install scapy for packet parsing")
-"""
-of_message.py
-Contains wrapper functions and classes for the of_message namespace
-that are generated by hand. It includes the rest of the wrapper
-function information into the of_message namespace
-"""
-
-parse_logger = logging.getLogger("parse")
-#parse_logger.setLevel(logging.DEBUG)
-
-# These message types are subclassed
-msg_type_subclassed = [
- cstruct.OFPT_STATS_REQUEST,
- cstruct.OFPT_STATS_REPLY,
- cstruct.OFPT_ERROR
-]
-
-# Maps from sub-types to classes
-stats_reply_to_class_map = {
- cstruct.OFPST_DESC : message.desc_stats_reply,
- cstruct.OFPST_AGGREGATE : message.aggregate_stats_reply,
- cstruct.OFPST_FLOW : message.flow_stats_reply,
- cstruct.OFPST_TABLE : message.table_stats_reply,
- cstruct.OFPST_PORT : message.port_stats_reply,
- cstruct.OFPST_QUEUE : message.queue_stats_reply
-}
-
-stats_request_to_class_map = {
- cstruct.OFPST_DESC : message.desc_stats_request,
- cstruct.OFPST_AGGREGATE : message.aggregate_stats_request,
- cstruct.OFPST_FLOW : message.flow_stats_request,
- cstruct.OFPST_TABLE : message.table_stats_request,
- cstruct.OFPST_PORT : message.port_stats_request,
- cstruct.OFPST_QUEUE : message.queue_stats_request
-}
-
-error_to_class_map = {
- cstruct.OFPET_HELLO_FAILED : message.hello_failed_error_msg,
- cstruct.OFPET_BAD_REQUEST : message.bad_request_error_msg,
- cstruct.OFPET_BAD_ACTION : message.bad_action_error_msg,
- cstruct.OFPET_FLOW_MOD_FAILED : message.flow_mod_failed_error_msg,
- cstruct.OFPET_PORT_MOD_FAILED : message.port_mod_failed_error_msg,
- cstruct.OFPET_QUEUE_OP_FAILED : message.queue_op_failed_error_msg
-}
-
-# Map from header type value to the underlieing message class
-msg_type_to_class_map = {
- cstruct.OFPT_HELLO : message.hello,
- cstruct.OFPT_ERROR : message.error,
- cstruct.OFPT_ECHO_REQUEST : message.echo_request,
- cstruct.OFPT_ECHO_REPLY : message.echo_reply,
- cstruct.OFPT_VENDOR : message.vendor,
- cstruct.OFPT_FEATURES_REQUEST : message.features_request,
- cstruct.OFPT_FEATURES_REPLY : message.features_reply,
- cstruct.OFPT_GET_CONFIG_REQUEST : message.get_config_request,
- cstruct.OFPT_GET_CONFIG_REPLY : message.get_config_reply,
- cstruct.OFPT_SET_CONFIG : message.set_config,
- cstruct.OFPT_PACKET_IN : message.packet_in,
- cstruct.OFPT_FLOW_REMOVED : message.flow_removed,
- cstruct.OFPT_PORT_STATUS : message.port_status,
- cstruct.OFPT_PACKET_OUT : message.packet_out,
- cstruct.OFPT_FLOW_MOD : message.flow_mod,
- cstruct.OFPT_PORT_MOD : message.port_mod,
- cstruct.OFPT_STATS_REQUEST : message.stats_request,
- cstruct.OFPT_STATS_REPLY : message.stats_reply,
- cstruct.OFPT_BARRIER_REQUEST : message.barrier_request,
- cstruct.OFPT_BARRIER_REPLY : message.barrier_reply,
- cstruct.OFPT_QUEUE_GET_CONFIG_REQUEST : message.queue_get_config_request,
- cstruct.OFPT_QUEUE_GET_CONFIG_REPLY : message.queue_get_config_reply
-}
-
-def _of_message_to_object(binary_string):
- """
- Map a binary string to the corresponding class.
-
- Appropriately resolves subclasses
- """
- hdr = message.ofp_header()
- hdr.unpack(binary_string)
- logging.info(hdr.show())
- # FIXME: Add error detection
- if not hdr.type in msg_type_subclassed:
- return msg_type_to_class_map[hdr.type]()
- if hdr.type == cstruct.OFPT_STATS_REQUEST:
- sub_hdr = message.ofp_stats_request()
- sub_hdr.unpack(binary_string)
- try:
- obj = stats_request_to_class_map[sub_hdr.stats_type]()
- except KeyError:
- obj = None
- return obj
- elif hdr.type == cstruct.OFPT_STATS_REPLY:
- sub_hdr = message.ofp_stats_reply()
- sub_hdr.unpack(binary_string)
- try:
- obj = stats_reply_to_class_map[sub_hdr.stats_type]()
- except KeyError:
- obj = None
- return obj
- elif hdr.type == cstruct.OFPT_ERROR:
- sub_hdr = message.ofp_error_msg()
- sub_hdr.unpack(binary_string)
- return error_to_class_map[sub_hdr.err_type]()
- else:
- parse_logger.error("Cannot parse pkt to message")
- return None
-
-def of_message_parse(binary_string, raw=False):
- """
- Parse an OpenFlow packet
-
- Parses a raw OpenFlow packet into a Python class, with class
- members fully populated.
-
- @param binary_string The packet (string) to be parsed
- @param raw If true, interpret the packet as an L2 packet. Not
- yet supported.
- @return An object of some message class or None if fails
- Note that any data beyond that parsed is not returned
-
- """
-
- if raw:
- parse_logger.error("raw packet message parsing not supported")
- return None
-
- obj = _of_message_to_object(binary_string)
- if obj:
- obj.unpack(binary_string)
- return obj
-
-
-def of_header_parse(binary_string, raw=False):
- """
- Parse only the header from an OpenFlow packet
-
- Parses the header from a raw OpenFlow packet into a
- an ofp_header Python class.
-
- @param binary_string The packet (string) to be parsed
- @param raw If true, interpret the packet as an L2 packet. Not
- yet supported.
- @return An ofp_header object
-
- """
-
- if raw:
- parse_logger.error("raw packet message parsing not supported")
- return None
-
- hdr = message.ofp_header()
- hdr.unpack(binary_string)
-
- return hdr
-
map_wc_field_to_match_member = {
'OFPFW_DL_VLAN' : 'vlan_vid',
'OFPFW_DL_SRC' : 'eth_src',
@@ -270,7 +111,7 @@
#@todo check min length of packet
if pkt_format.upper() != "L2":
- parse_logger.error("Only L2 supported for packet_to_flow")
+ logging.error("Only L2 supported for packet_to_flow")
return None
if type(packet) == type(""):
@@ -282,63 +123,63 @@
try:
(dot1q, ip, tcp, udp, icmp, arp) = packet_type_classify(ether)
except:
- parse_logger.error("packet_to_flow_match: Classify error")
+ logging.error("packet_to_flow_match: Classify error")
return None
- match = cstruct.ofp_match()
- match.wildcards = cstruct.OFPFW_ALL
+ match = ofp.ofp_match()
+ match.wildcards = ofp.OFPFW_ALL
#@todo Check if packet is other than L2 format
match.eth_dst = parse_mac(ether.dst)
- match.wildcards &= ~cstruct.OFPFW_DL_DST
+ match.wildcards &= ~ofp.OFPFW_DL_DST
match.eth_src = parse_mac(ether.src)
- match.wildcards &= ~cstruct.OFPFW_DL_SRC
+ match.wildcards &= ~ofp.OFPFW_DL_SRC
match.eth_type = ether.type
- match.wildcards &= ~cstruct.OFPFW_DL_TYPE
+ match.wildcards &= ~ofp.OFPFW_DL_TYPE
if dot1q:
match.vlan_vid = dot1q.vlan
match.vlan_pcp = dot1q.prio
match.eth_type = dot1q.type
else:
- match.vlan_vid = cstruct.OFP_VLAN_NONE
+ match.vlan_vid = ofp.OFP_VLAN_NONE
match.vlan_pcp = 0
- match.wildcards &= ~cstruct.OFPFW_DL_VLAN
- match.wildcards &= ~cstruct.OFPFW_DL_VLAN_PCP
+ match.wildcards &= ~ofp.OFPFW_DL_VLAN
+ match.wildcards &= ~ofp.OFPFW_DL_VLAN_PCP
if ip:
match.ipv4_src = parse_ip(ip.src)
- match.wildcards &= ~cstruct.OFPFW_NW_SRC_MASK
+ match.wildcards &= ~ofp.OFPFW_NW_SRC_MASK
match.ipv4_dst = parse_ip(ip.dst)
- match.wildcards &= ~cstruct.OFPFW_NW_DST_MASK
+ match.wildcards &= ~ofp.OFPFW_NW_DST_MASK
match.ip_dscp = ip.tos
- match.wildcards &= ~cstruct.OFPFW_NW_TOS
+ match.wildcards &= ~ofp.OFPFW_NW_TOS
if tcp:
match.ip_proto = 6
- match.wildcards &= ~cstruct.OFPFW_NW_PROTO
+ match.wildcards &= ~ofp.OFPFW_NW_PROTO
elif not tcp and udp:
tcp = udp
match.ip_proto = 17
- match.wildcards &= ~cstruct.OFPFW_NW_PROTO
+ match.wildcards &= ~ofp.OFPFW_NW_PROTO
if tcp:
match.tcp_src = tcp.sport
- match.wildcards &= ~cstruct.OFPFW_TP_SRC
+ match.wildcards &= ~ofp.OFPFW_TP_SRC
match.tcp_dst = tcp.dport
- match.wildcards &= ~cstruct.OFPFW_TP_DST
+ match.wildcards &= ~ofp.OFPFW_TP_DST
if icmp:
match.ip_proto = 1
match.tcp_src = icmp.type
match.tcp_dst = icmp.code
- match.wildcards &= ~cstruct.OFPFW_NW_PROTO
+ match.wildcards &= ~ofp.OFPFW_NW_PROTO
if arp:
match.ip_proto = arp.op
- match.wildcards &= ~cstruct.OFPFW_NW_PROTO
+ match.wildcards &= ~ofp.OFPFW_NW_PROTO
match.ipv4_src = parse_ip(arp.psrc)
- match.wildcards &= ~cstruct.OFPFW_NW_SRC_MASK
+ match.wildcards &= ~ofp.OFPFW_NW_SRC_MASK
match.ipv4_dst = parse_ip(arp.pdst)
- match.wildcards &= ~cstruct.OFPFW_NW_DST_MASK
+ match.wildcards &= ~ofp.OFPFW_NW_DST_MASK
return match
diff --git a/src/python/oftest/testutils.py b/src/python/oftest/testutils.py
index a1db2e0..6e29dc3 100644
--- a/src/python/oftest/testutils.py
+++ b/src/python/oftest/testutils.py
@@ -19,7 +19,7 @@
import of10
import of10.message
import of10.action
-import of10.parse
+import oftest.parse
global skipped_test_count
skipped_test_count = 0
@@ -544,7 +544,7 @@
" != " + str(res_match.tcp_dst))
def packet_to_flow_match(parent, packet):
- match = of10.parse.packet_to_flow_match(packet)
+ match = oftest.parse.packet_to_flow_match(packet)
match.wildcards |= required_wildcards(parent)
return match
@@ -559,7 +559,7 @@
@param in_band if True, do not wildcard ingress port
@param egr_ports None (drop), single port or list of ports
"""
- match = of10.parse.packet_to_flow_match(pkt)
+ match = oftest.parse.packet_to_flow_match(pkt)
parent.assertTrue(match is not None, "Flow match from pkt failed")
if wildcards is None:
wildcards = required_wildcards(parent)
@@ -817,10 +817,10 @@
if field_to_mod == 'eth_dst':
act = of10.action.action_set_dl_dst()
- act.dl_addr = of10.parse.parse_mac(mod_field_vals['eth_dst'])
+ act.dl_addr = oftest.parse.parse_mac(mod_field_vals['eth_dst'])
elif field_to_mod == 'eth_src':
act = of10.action.action_set_dl_src()
- act.dl_addr = of10.parse.parse_mac(mod_field_vals['eth_src'])
+ act.dl_addr = oftest.parse.parse_mac(mod_field_vals['eth_src'])
elif field_to_mod == 'dl_vlan_enable':
if not mod_field_vals['dl_vlan_enable']: # Strip VLAN tag
act = of10.action.action_strip_vlan()
@@ -834,10 +834,10 @@
act.vlan_pcp = mod_field_vals['vlan_pcp']
elif field_to_mod == 'ip_src':
act = of10.action.action_set_nw_src()
- act.nw_addr = of10.parse.parse_ip(mod_field_vals['ip_src'])
+ act.nw_addr = oftest.parse.parse_ip(mod_field_vals['ip_src'])
elif field_to_mod == 'ip_dst':
act = of10.action.action_set_nw_dst()
- act.nw_addr = of10.parse.parse_ip(mod_field_vals['ip_dst'])
+ act.nw_addr = oftest.parse.parse_ip(mod_field_vals['ip_dst'])
elif field_to_mod == 'ip_tos':
act = of10.action.action_set_nw_tos()
act.nw_tos = mod_field_vals['ip_tos']