1# Copyright (C) 2011, 2012 Nippon Telegraph and Telephone Corporation.
2# Copyright (C) 2011, 2012 Isaku Yamahata <yamahata at valinux co jp>
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18OpenFlow 1.0 definitions.
19"""
20
21from ryu.ofproto import ofproto_utils
22from ryu.ofproto.nicira_ext import *  # For API compat
23
24MAX_XID = 0xffffffff
25
26# define constants
27OFP_VERSION = 0x01
28OFP_MAX_TABLE_NAME_LEN = 32
29OFP_MAX_TABLE_NAME_LEN_STR = str(OFP_MAX_TABLE_NAME_LEN)
30OFP_MAX_PORT_NAME_LEN = 16
31OFP_TCP_PORT = 6633
32OFP_SSL_PORT = 6633
33OFP_ETH_ALEN = 6
34OFP_ETH_ALEN_STR = str(OFP_ETH_ALEN)
35
36OFP_NO_BUFFER = 0xffffffff
37
38# enum ofp_port
39OFPP_MAX = 0xff00
40OFPP_IN_PORT = 0xfff8   # Send the packet out the input port. This
41                        # virtual port must be explicitly used
42                        # in order to send back out of the input
43                        # port.
44OFPP_TABLE = 0xfff9     # Perform actions in flow table.
45                        # NB: This can only be the destination
46                        # port for packet-out messages.
47OFPP_NORMAL = 0xfffa    # Process with normal L2/L3 switching.
48OFPP_FLOOD = 0xfffb     # All physical ports except input port and
49                        # those disabled by STP.
50OFPP_ALL = 0xfffc       # All physical ports except input port.
51OFPP_CONTROLLER = 0xfffd        # Send to controller.
52OFPP_LOCAL = 0xfffe     # Local openflow "port".
53OFPP_NONE = 0xffff      # Not associated with a physical port.
54
55# enum ofp_type
56OFPT_HELLO = 0  # Symmetric message
57OFPT_ERROR = 1  # Symmetric message
58OFPT_ECHO_REQUEST = 2   # Symmetric message
59OFPT_ECHO_REPLY = 3     # Symmetric message
60OFPT_VENDOR = 4         # Symmetric message
61OFPT_FEATURES_REQUEST = 5       # Controller/switch message
62OFPT_FEATURES_REPLY = 6         # Controller/switch message
63OFPT_GET_CONFIG_REQUEST = 7     # Controller/switch message
64OFPT_GET_CONFIG_REPLY = 8       # Controller/switch message
65OFPT_SET_CONFIG = 9      # Controller/switch message
66OFPT_PACKET_IN = 10      # Async message
67OFPT_FLOW_REMOVED = 11   # Async message
68OFPT_PORT_STATUS = 12    # Async message
69OFPT_PACKET_OUT = 13     # Controller/switch message
70OFPT_FLOW_MOD = 14       # Controller/switch message
71OFPT_PORT_MOD = 15       # Controller/switch message
72OFPT_STATS_REQUEST = 16  # Controller/switch message
73OFPT_STATS_REPLY = 17    # Controller/switch message
74OFPT_BARRIER_REQUEST = 18       # Controller/switch message
75OFPT_BARRIER_REPLY = 19  # Controller/switch message
76OFPT_QUEUE_GET_CONFIG_REQUEST = 20      # Controller/switch message
77OFPT_QUEUE_GET_CONFIG_REPLY = 21        # Controller/switch message
78
79OFP_HEADER_PACK_STR = '!BBHI'
80OFP_HEADER_SIZE = 8
81OFP_MSG_SIZE_MAX = 65535
82assert calcsize(OFP_HEADER_PACK_STR) == OFP_HEADER_SIZE
83
84# define constants
85OFP_DEFAULT_MISS_SEND_LEN = 128
86
87# enum ofp_config_flags
88OFPC_FRAG_NORMAL = 0    # No special handling for fragments.
89OFPC_FRAG_DROP = 1      # Drop fragments.
90OFPC_FRAG_REASM = 2     # Reassemble (only if OFPC_IP_REASM set).
91OFPC_FRAG_NX_MATCH = 3  # Make first fragments available for matching.
92OFPC_FRAG_MASK = 3
93
94OFP_SWITCH_CONFIG_PACK_STR = '!HH'
95OFP_SWITCH_CONFIG_SIZE = 12
96assert (calcsize(OFP_SWITCH_CONFIG_PACK_STR) + OFP_HEADER_SIZE ==
97        OFP_SWITCH_CONFIG_SIZE)
98
99# enum ofp_capabilities
100OFPC_FLOW_STATS = 1 << 0        # Flow statistics.
101OFPC_TABLE_STATS = 1 << 1       # Table statistics.
102OFPC_PORT_STATS = 1 << 2        # Port statistics.
103OFPC_STP = 1 << 3               # 802.1d spanning tree.
104OFPC_RESERVED = 1 << 4          # Reserved, must not be set.
105OFPC_IP_REASM = 1 << 5          # Can reassemble IP fragments.
106OFPC_QUEUE_STATS = 1 << 6       # Queue statistics.
107OFPC_ARP_MATCH_IP = 1 << 7      # Match IP addresses in ARP pkts.
108
109# enum ofp_port_config
110OFPPC_PORT_DOWN = 1 << 0        # Port is administratively down.
111OFPPC_NO_STP = 1 << 1           # Disable 802.1D spanning tree on port.
112OFPPC_NO_RECV = 1 << 2          # Drop all packets except 802.1D
113                                # spanning tree packets
114OFPPC_NO_RECV_STP = 1 << 3      # Drop received 802.1D STP packets.
115OFPPC_NO_FLOOD = 1 << 4         # Do not include this port when flooding.
116OFPPC_NO_FWD = 1 << 5           # Drop packets forwarded to port.
117OFPPC_NO_PACKET_IN = 1 << 6     # Do not send packet-in msgs for port.
118
119# enum ofp_port_state
120OFPPS_LINK_DOWN = 1 << 0        # No physical link present.
121OFPPS_STP_LISTEN = 0 << 8       # Not learning or relaying frames.
122OFPPS_STP_LEARN = 1 << 8        # Learning but not relaying frames.
123OFPPS_STP_FORWARD = 2 << 8      # Learning and relaying frames.
124OFPPS_STP_BLOCK = 3 << 8        # Not part of spanning tree.
125OFPPS_STP_MASK = 3 << 8         # Bit mask for OFPPS_STP_* values.
126
127# enum ofp_port_features
128OFPPF_10MB_HD = 1 << 0          # 10 Mb half-duplex rate support.
129OFPPF_10MB_FD = 1 << 1          # 10 Mb full-duplex rate support.
130OFPPF_100MB_HD = 1 << 2         # 100 Mb half-duplex rate support.
131OFPPF_100MB_FD = 1 << 3         # 100 Mb full-duplex rate support.
132OFPPF_1GB_HD = 1 << 4           # 1 Gb half-duplex rate support.
133OFPPF_1GB_FD = 1 << 5           # 1 Gb full-duplex rate support.
134OFPPF_10GB_FD = 1 << 6          # 10 Gb full-duplex rate support.
135OFPPF_COPPER = 1 << 7           # Copper medium.
136OFPPF_FIBER = 1 << 8            # Fiber medium.
137OFPPF_AUTONEG = 1 << 9          # Auto-negotiation.
138OFPPF_PAUSE = 1 << 10           # Pause.
139OFPPF_PAUSE_ASYM = 1 << 11      # Asymmetric pause.
140
141_OFP_PHY_PORT_PACK_STR = 'H' + OFP_ETH_ALEN_STR + 's' + \
142                         str(OFP_MAX_PORT_NAME_LEN) + 'sIIIIII'
143OFP_PHY_PORT_PACK_STR = '!' + _OFP_PHY_PORT_PACK_STR
144OFP_PHY_PORT_SIZE = 48
145assert calcsize(OFP_PHY_PORT_PACK_STR) == OFP_PHY_PORT_SIZE
146
147OFP_SWITCH_FEATURES_PACK_STR = '!QIB3xII'
148OFP_SWITCH_FEATURES_SIZE = 32
149assert (calcsize(OFP_SWITCH_FEATURES_PACK_STR) + OFP_HEADER_SIZE ==
150        OFP_SWITCH_FEATURES_SIZE)
151
152# enum ofp_port_reason
153OFPPR_ADD = 0           # The port was added.
154OFPPR_DELETE = 1        # The port was removed.
155OFPPR_MODIFY = 2        # Some attribute of the port has changed.
156
157OFP_PORT_STATUS_PACK_STR = '!B7x' + _OFP_PHY_PORT_PACK_STR
158OFP_PORT_STATUS_DESC_OFFSET = OFP_HEADER_SIZE + 8
159OFP_PORT_STATUS_SIZE = 64
160assert (calcsize(OFP_PORT_STATUS_PACK_STR) + OFP_HEADER_SIZE ==
161        OFP_PORT_STATUS_SIZE)
162
163OFP_PORT_MOD_PACK_STR = '!H' + OFP_ETH_ALEN_STR + 'sIII4x'
164OFP_PORT_MOD_SIZE = 32
165assert calcsize(OFP_PORT_MOD_PACK_STR) + OFP_HEADER_SIZE == OFP_PORT_MOD_SIZE
166
167# enum ofp_packet_in_reason
168OFPR_NO_MATCH = 0       # No matching flow.
169OFPR_ACTION = 1         # Action explicitly output to controller.
170
171# OF1.0 spec says OFP_ASSERT(sizeof(struct ofp_packet_in) == 20).
172# It's quite bogus as it assumes a specific class of C implementations.
173# (well, if it was C.  it's unclear from the spec itself.)
174# We just use the real size of the structure as this is not C.  This
175# agrees with on-wire messages OpenFlow Reference Release and Open vSwitch
176# produce.
177OFP_PACKET_IN_PACK_STR = '!IHHBx'
178OFP_PACKET_IN_SIZE = 18
179assert calcsize(OFP_PACKET_IN_PACK_STR) + OFP_HEADER_SIZE == OFP_PACKET_IN_SIZE
180
181# enum ofp_action_type
182OFPAT_OUTPUT = 0        # Output to switch port.
183OFPAT_SET_VLAN_VID = 1  # Set the 802.1q VLAN id.
184OFPAT_SET_VLAN_PCP = 2  # Set the 802.1q priority.
185OFPAT_STRIP_VLAN = 3    # Strip the 802.1q header.
186OFPAT_SET_DL_SRC = 4    # Ethernet source address.
187OFPAT_SET_DL_DST = 5    # Ethernet destination address.
188OFPAT_SET_NW_SRC = 6    # IP source address.
189OFPAT_SET_NW_DST = 7    # IP destination address.
190OFPAT_SET_NW_TOS = 8    # IP ToS (DSCP field, 6 bits).
191OFPAT_SET_TP_SRC = 9    # TCP/UDP source port.
192OFPAT_SET_TP_DST = 10   # TCP/UDP destination port.
193OFPAT_ENQUEUE = 11      # Output to queue.
194OFPAT_VENDOR = 0xffff
195
196OFP_ACTION_OUTPUT_PACK_STR = '!HHHH'
197OFP_ACTION_OUTPUT_SIZE = 8
198assert calcsize(OFP_ACTION_OUTPUT_PACK_STR) == OFP_ACTION_OUTPUT_SIZE
199
200OFP_ACTION_VLAN_VID_PACK_STR = '!HHH2x'
201OFP_ACTION_VLAN_VID_SIZE = 8
202assert calcsize(OFP_ACTION_VLAN_VID_PACK_STR) == OFP_ACTION_VLAN_VID_SIZE
203
204OFP_ACTION_VLAN_PCP_PACK_STR = '!HHB3x'
205OFP_ACTION_VLAN_PCP_SIZE = 8
206assert calcsize(OFP_ACTION_VLAN_PCP_PACK_STR) == OFP_ACTION_VLAN_PCP_SIZE
207
208OFP_ACTION_DL_ADDR_PACK_STR = '!HH' + OFP_ETH_ALEN_STR + 's6x'
209OFP_ACTION_DL_ADDR_SIZE = 16
210assert calcsize(OFP_ACTION_DL_ADDR_PACK_STR) == OFP_ACTION_DL_ADDR_SIZE
211
212OFP_ACTION_NW_ADDR_PACK_STR = '!HHI'
213OFP_ACTION_NW_ADDR_SIZE = 8
214assert calcsize(OFP_ACTION_NW_ADDR_PACK_STR) == OFP_ACTION_NW_ADDR_SIZE
215
216OFP_ACTION_NW_TOS_PACK_STR = '!HHB3x'
217OFP_ACTION_NW_TOS_SIZE = 8
218assert calcsize(OFP_ACTION_NW_TOS_PACK_STR) == OFP_ACTION_NW_TOS_SIZE
219
220OFP_ACTION_TP_PORT_PACK_STR = '!HHH2x'
221OFP_ACTION_TP_PORT_SIZE = 8
222assert calcsize(OFP_ACTION_TP_PORT_PACK_STR) == OFP_ACTION_TP_PORT_SIZE
223
224OFP_ACTION_VENDOR_HEADER_PACK_STR = '!HHI'
225OFP_ACTION_VENDOR_HEADER_SIZE = 8
226assert (calcsize(OFP_ACTION_VENDOR_HEADER_PACK_STR) ==
227        OFP_ACTION_VENDOR_HEADER_SIZE)
228# OpenFlow1.2 or later compatible
229OFP_ACTION_EXPERIMENTER_HEADER_SIZE = OFP_ACTION_VENDOR_HEADER_SIZE
230
231OFP_ACTION_HEADER_PACK_STR = '!HH4x'
232OFP_ACTION_HEADER_SIZE = 8
233assert calcsize(OFP_ACTION_HEADER_PACK_STR) == OFP_ACTION_HEADER_SIZE
234
235OFP_ACTION_ENQUEUE_PACK_STR = '!HHH6xI'
236OFP_ACTION_ENQUEUE_SIZE = 16
237assert calcsize(OFP_ACTION_ENQUEUE_PACK_STR) == OFP_ACTION_ENQUEUE_SIZE
238
239OFP_ACTION_PACK_STR = '!H'
240# because of union ofp_action
241# OFP_ACTION_SIZE = 8
242# assert calcsize(OFP_ACTION_PACK_STR) == OFP_ACTION_SIZE
243
244OFP_PACKET_OUT_PACK_STR = '!IHH'
245OFP_PACKET_OUT_SIZE = 16
246assert (calcsize(OFP_PACKET_OUT_PACK_STR) + OFP_HEADER_SIZE ==
247        OFP_PACKET_OUT_SIZE)
248
249# enum ofp_flow_mod_command
250OFPFC_ADD = 0               # New flow.
251OFPFC_MODIFY = 1            # Modify all matching flows.
252OFPFC_MODIFY_STRICT = 2     # Modify entry strictly matching wildcards
253OFPFC_DELETE = 3            # Delete all matching flows.
254OFPFC_DELETE_STRICT = 4     # Strictly match wildcards and priority.
255
256# enum ofp_flow_wildcards
257OFPFW_IN_PORT = 1 << 0      # Switch input port.
258OFPFW_DL_VLAN = 1 << 1      # VLAN vid.
259OFPFW_DL_SRC = 1 << 2       # Ethernet source address.
260OFPFW_DL_DST = 1 << 3       # Ethernet destination address.
261OFPFW_DL_TYPE = 1 << 4      # Ethernet frame type.
262OFPFW_NW_PROTO = 1 << 5     # IP protocol.
263OFPFW_TP_SRC = 1 << 6       # TCP/UDP source port.
264OFPFW_TP_DST = 1 << 7       # TCP/UDP destination port.
265OFPFW_NW_SRC_SHIFT = 8
266OFPFW_NW_SRC_BITS = 6
267OFPFW_NW_SRC_MASK = ((1 << OFPFW_NW_SRC_BITS) - 1) << OFPFW_NW_SRC_SHIFT
268OFPFW_NW_SRC = OFPFW_NW_SRC_MASK  # IP source address (not in OF Spec).
269OFPFW_NW_SRC_ALL = 32 << OFPFW_NW_SRC_SHIFT
270OFPFW_NW_DST_SHIFT = 14
271OFPFW_NW_DST_BITS = 6
272OFPFW_NW_DST_MASK = ((1 << OFPFW_NW_DST_BITS) - 1) << OFPFW_NW_DST_SHIFT
273OFPFW_NW_DST = OFPFW_NW_DST_MASK  # IP destination address (not in OF Spec).
274OFPFW_NW_DST_ALL = 32 << OFPFW_NW_DST_SHIFT
275OFPFW_DL_VLAN_PCP = 1 << 20     # VLAN priority.
276OFPFW_NW_TOS = 1 << 21  # IP ToS (DSCP field, 6 bits).
277OFPFW_ALL = ((1 << 22) - 1)
278
279# define constants
280OFPFW_ICMP_TYPE = OFPFW_TP_SRC
281OFPFW_ICMP_CODE = OFPFW_TP_DST
282OFP_DL_TYPE_ETH2_CUTOFF = 0x0600
283OFP_DL_TYPE_NOT_ETH_TYPE = 0x05ff
284OFP_VLAN_NONE = 0xffff
285
286_OFP_MATCH_PACK_STR = 'IH' + OFP_ETH_ALEN_STR + 's' + OFP_ETH_ALEN_STR + \
287                      'sHBxHBB2xIIHH'
288OFP_MATCH_PACK_STR = '!' + _OFP_MATCH_PACK_STR
289OFP_MATCH_SIZE = 40
290assert calcsize(OFP_MATCH_PACK_STR) == OFP_MATCH_SIZE
291
292OFP_FLOW_PERMANENT = 0
293OFP_DEFAULT_PRIORITY = 0x8000
294
295# enum ofp_flow_mod_flags
296OFPFF_SEND_FLOW_REM = 1 << 0    # Send flow removed message when flow
297                                # expires or is deleted.
298OFPFF_CHECK_OVERLAP = 1 << 1    # Check for overlapping entries first.
299OFPFF_EMERG = 1 << 2            # Ramark this is for emergency.
300
301_OFP_FLOW_MOD_PACK_STR0 = 'QHHHHIHH'
302OFP_FLOW_MOD_PACK_STR = '!' + _OFP_MATCH_PACK_STR + _OFP_FLOW_MOD_PACK_STR0
303OFP_FLOW_MOD_PACK_STR0 = '!' + _OFP_FLOW_MOD_PACK_STR0
304OFP_FLOW_MOD_SIZE = 72
305assert calcsize(OFP_FLOW_MOD_PACK_STR) + OFP_HEADER_SIZE == OFP_FLOW_MOD_SIZE
306
307# enum ofp_flow_removed_reason
308OFPRR_IDLE_TIMEOUT = 0  # Flow idle time exceeded idle_timeout.
309OFPRR_HARD_TIMEOUT = 1  # Time exceeded hard_timeout.
310OFPRR_DELETE = 2        # Evicted by a DELETE flow mod.
311
312_OFP_FLOW_REMOVED_PACK_STR0 = 'QHBxIIH2xQQ'
313OFP_FLOW_REMOVED_PACK_STR = '!' + _OFP_MATCH_PACK_STR + \
314                            _OFP_FLOW_REMOVED_PACK_STR0
315OFP_FLOW_REMOVED_PACK_STR0 = '!' + _OFP_FLOW_REMOVED_PACK_STR0
316OFP_FLOW_REMOVED_SIZE = 88
317assert (calcsize(OFP_FLOW_REMOVED_PACK_STR) + OFP_HEADER_SIZE ==
318        OFP_FLOW_REMOVED_SIZE)
319
320
321# enum ofp_error_type
322OFPET_HELLO_FAILED = 0  # Hello protocol failed.
323OFPET_BAD_REQUEST = 1   # Request was not understood.
324OFPET_BAD_ACTION = 2    # Error in action description.
325OFPET_FLOW_MOD_FAILED = 3       # Problem modifying flow entry.
326OFPET_PORT_MOD_FAILED = 4       # OFPT_PORT_MOD failed.
327OFPET_QUEUE_OP_FAILED = 5       # Queue operation failed.
328
329# enum ofp_hello_failed_code
330OFPHFC_INCOMPATIBLE = 0  # No compatible version.
331OFPHFC_EPERM = 1         # Permissions error.
332
333# enum ofp_bad_request_code
334OFPBRC_BAD_VERSION = 0          # ofp_header.version not supported.
335OFPBRC_BAD_TYPE = 1             # ofp_header.type not supported.
336OFPBRC_BAD_STAT = 2             # ofp_stats_msg.type not supported.
337OFPBRC_BAD_VENDOR = 3           # Vendor not supported (in ofp_vendor_header
338                                # or ofp_stats_msg).
339OFPBRC_BAD_SUBTYPE = 4          # Vendor subtype not supported.
340OFPBRC_EPERM = 5                # Permissions error.
341OFPBRC_BAD_LEN = 6              # Wrong request length for type.
342OFPBRC_BUFFER_EMPTY = 7         # Specified buffer has already been used.
343OFPBRC_BUFFER_UNKNOWN = 8       # Specified buffer does not exist.
344
345# enum ofp_bad_action_code
346OFPBAC_BAD_TYPE = 0         # Unknown action type.
347OFPBAC_BAD_LEN = 1          # Length problem in actions.
348OFPBAC_BAD_VENDOR = 2       # Unknown vendor id specified.
349OFPBAC_BAD_VENDOR_TYPE = 3  # Unknown action type for vendor id.
350OFPBAC_BAD_OUT_PORT = 4     # Problem validating output action.
351OFPBAC_BAD_ARGUMENT = 5     # Bad action argument.
352OFPBAC_EPERM = 6            # Permissions error.
353OFPBAC_TOO_MANY = 7         # Can't handle this many actions.
354OFPBAC_BAD_QUEUE = 8        # Problem validating output queue.
355
356# enum ofp_flow_mod_failed_code
357OFPFMFC_ALL_TABLES_FULL = 0     # Flow not added because of full tables.
358OFPFMFC_OVERLAP = 1             # Attempted to add overlapping flow with
359                                # CHECK_OVERLAP flags set.
360OFPFMFC_EPERM = 2               # Permissions error.
361OFPFMFC_BAD_EMERG_TIMEOUT = 3   # Flow not added because of non-zero idle/hard
362                                # timeout.
363OFPFMFC_BAD_COMMAND = 4         # Unknown command.
364OFPFMFC_UNSUPPORTED = 5         # Unsupported action list - cannot process in
365                                # the order specified.
366
367# enum ofp_port_mod_failed_code
368OFPPMFC_BAD_PORT = 0        # Specified port does not exist.
369OFPPMFC_BAD_HW_ADDR = 1     # Specified hardware address is wrong.
370
371# enum ofp_queue_op_failed_code
372OFPQOFC_BAD_PORT = 0    # Invalid port (or port does not exist).
373OFPQOFC_BAD_QUEUE = 1   # Queue does not exist.
374OFPQOFC_EPERM = 2       # Permissions error.
375
376OFP_ERROR_MSG_PACK_STR = '!HH'
377OFP_ERROR_MSG_SIZE = 12
378assert calcsize(OFP_ERROR_MSG_PACK_STR) + OFP_HEADER_SIZE == OFP_ERROR_MSG_SIZE
379
380# enum ofp_stats_types
381OFPST_DESC = 0
382OFPST_FLOW = 1
383OFPST_AGGREGATE = 2
384OFPST_TABLE = 3
385OFPST_PORT = 4
386OFPST_QUEUE = 5
387OFPST_VENDOR = 0xffff
388
389_OFP_STATS_MSG_PACK_STR = 'HH'
390OFP_STATS_MSG_PACK_STR = '!' + _OFP_STATS_MSG_PACK_STR
391OFP_STATS_MSG_SIZE = 12
392assert calcsize(OFP_STATS_MSG_PACK_STR) + OFP_HEADER_SIZE == OFP_STATS_MSG_SIZE
393
394# enum ofp_stats_reply_flags
395OFPSF_REPLY_MORE = 1 << 0       # More replies to follow.
396
397# define constants
398DESC_STR_LEN = 256
399DESC_STR_LEN_STR = str(DESC_STR_LEN)
400SERIAL_NUM_LEN = 32
401SERIAL_NUM_LEN_STR = str(SERIAL_NUM_LEN)
402
403OFP_DESC_STATS_PACK_STR = '!' + \
404                          DESC_STR_LEN_STR + 's' + \
405                          DESC_STR_LEN_STR + 's' + \
406                          DESC_STR_LEN_STR + 's' + \
407                          SERIAL_NUM_LEN_STR + 's' + \
408                          DESC_STR_LEN_STR + 's'
409OFP_DESC_STATS_SIZE = 1068
410assert (calcsize(OFP_DESC_STATS_PACK_STR) + OFP_STATS_MSG_SIZE ==
411        OFP_DESC_STATS_SIZE)
412
413_OFP_FLOW_STATS_REQUEST_ID_PORT_STR = 'BxH'
414OFP_FLOW_STATS_REQUEST_ID_PORT_STR = '!' + _OFP_FLOW_STATS_REQUEST_ID_PORT_STR
415OFP_FLOW_STATS_REQUEST_PACK_STR = '!' + _OFP_MATCH_PACK_STR + \
416                                  _OFP_FLOW_STATS_REQUEST_ID_PORT_STR
417OFP_FLOW_STATS_REQUEST_SIZE = 56
418assert (calcsize(OFP_FLOW_STATS_REQUEST_PACK_STR) + OFP_STATS_MSG_SIZE ==
419        OFP_FLOW_STATS_REQUEST_SIZE)
420
421_OFP_FLOW_STATS_0_PACK_STR = 'HBx'
422OFP_FLOW_STATS_0_PACK_STR = '!' + _OFP_FLOW_STATS_0_PACK_STR
423OFP_FLOW_STATS_0_SIZE = 4
424assert calcsize(OFP_FLOW_STATS_0_PACK_STR) == OFP_FLOW_STATS_0_SIZE
425_OFP_FLOW_STATS_1_PACK_STR = 'IIHHH6xQQQ'
426OFP_FLOW_STATS_1_PACK_STR = '!' + _OFP_FLOW_STATS_1_PACK_STR
427OFP_FLOW_STATS_1_SIZE = 44
428assert calcsize(OFP_FLOW_STATS_1_PACK_STR) == OFP_FLOW_STATS_1_SIZE
429OFP_FLOW_STATS_PACK_STR = '!' + _OFP_FLOW_STATS_0_PACK_STR +\
430                          _OFP_MATCH_PACK_STR + _OFP_FLOW_STATS_1_PACK_STR
431OFP_FLOW_STATS_SIZE = 88
432assert calcsize(OFP_FLOW_STATS_PACK_STR) == OFP_FLOW_STATS_SIZE
433
434OFP_AGGREGATE_STATS_REPLY_PACK_STR = '!QQI4x'
435OFP_AGGREGATE_STATS_REPLY_SIZE = 36
436assert (calcsize(OFP_AGGREGATE_STATS_REPLY_PACK_STR) +
437        OFP_STATS_MSG_SIZE == OFP_AGGREGATE_STATS_REPLY_SIZE)
438
439OFP_TABLE_STATS_PACK_STR = '!B3x' + OFP_MAX_TABLE_NAME_LEN_STR + 'sIIIQQ'
440OFP_TABLE_STATS_SIZE = 64
441assert calcsize(OFP_TABLE_STATS_PACK_STR) == OFP_TABLE_STATS_SIZE
442
443OFP_PORT_STATS_REQUEST_PACK_STR = '!H6x'
444OFP_PORT_STATS_REQUEST_SIZE = 20
445assert (calcsize(OFP_PORT_STATS_REQUEST_PACK_STR) + OFP_STATS_MSG_SIZE ==
446        OFP_PORT_STATS_REQUEST_SIZE)
447
448OFP_PORT_STATS_PACK_STR = '!H6xQQQQQQQQQQQQ'
449OFP_PORT_STATS_SIZE = 104
450assert calcsize(OFP_PORT_STATS_PACK_STR) == OFP_PORT_STATS_SIZE
451
452OFPQ_ALL = 0xffffffff
453
454OFP_QUEUE_STATS_REQUEST_PACK_STR = '!HxxI'
455OFP_QUEUE_STATS_REQUEST_SIZE = 8
456assert (calcsize(OFP_QUEUE_STATS_REQUEST_PACK_STR) ==
457        OFP_QUEUE_STATS_REQUEST_SIZE)
458
459OFP_QUEUE_STATS_PACK_STR = '!H2xIQQQ'
460OFP_QUEUE_STATS_SIZE = 32
461assert calcsize(OFP_QUEUE_STATS_PACK_STR) == OFP_QUEUE_STATS_SIZE
462
463OFP_VENDOR_STATS_MSG_PACK_STR = '!I'
464OFP_VENDOR_STATS_MSG_SIZE = 16
465assert (calcsize(OFP_VENDOR_STATS_MSG_PACK_STR) + OFP_STATS_MSG_SIZE ==
466        OFP_VENDOR_STATS_MSG_SIZE)
467
468OFP_VENDOR_HEADER_PACK_STR = '!I'
469OFP_VENDOR_HEADER_SIZE = 12
470assert (calcsize(OFP_VENDOR_HEADER_PACK_STR) + OFP_HEADER_SIZE ==
471        OFP_VENDOR_HEADER_SIZE)
472
473OFP_QUEUE_GET_CONFIG_REQUEST_PACK_STR = '!H2x'
474OFP_QUEUE_GET_CONFIG_REQUEST_SIZE = 12
475assert (calcsize(OFP_QUEUE_GET_CONFIG_REQUEST_PACK_STR) + OFP_HEADER_SIZE ==
476        OFP_QUEUE_GET_CONFIG_REQUEST_SIZE)
477
478OFP_QUEUE_GET_CONFIG_REPLY_PACK_STR = '!H6x'
479OFP_QUEUE_GET_CONFIG_REPLY_SIZE = 16
480assert (calcsize(OFP_QUEUE_GET_CONFIG_REPLY_PACK_STR) + OFP_HEADER_SIZE ==
481        OFP_QUEUE_GET_CONFIG_REPLY_SIZE)
482
483OFP_PACKET_QUEUE_PQCK_STR = '!IH2x'
484OFP_PACKET_QUEUE_SIZE = 8
485assert calcsize(OFP_PACKET_QUEUE_PQCK_STR) == OFP_PACKET_QUEUE_SIZE
486
487OFPQT_NONE = 0
488OFPQT_MIN_RATE = 1
489
490OFP_QUEUE_PROP_HEADER_PACK_STR = '!HH4x'
491OFP_QUEUE_PROP_HEADER_SIZE = 8
492assert calcsize(OFP_QUEUE_PROP_HEADER_PACK_STR) == OFP_QUEUE_PROP_HEADER_SIZE
493
494OFP_QUEUE_PROP_MIN_RATE_PACK_STR = '!H6x'
495OFP_QUEUE_PROP_MIN_RATE_SIZE = 16
496assert (calcsize(OFP_QUEUE_PROP_MIN_RATE_PACK_STR) +
497        OFP_QUEUE_PROP_HEADER_SIZE == OFP_QUEUE_PROP_MIN_RATE_SIZE)
498
499# OXM
500
501# enum ofp_oxm_class
502OFPXMC_OPENFLOW_BASIC = 0x8000  # Basic class for OpenFlow
503
504
505def _oxm_tlv_header(class_, field, hasmask, length):
506    return (class_ << 16) | (field << 9) | (hasmask << 8) | length
507
508
509def oxm_tlv_header(field, length):
510    return _oxm_tlv_header(OFPXMC_OPENFLOW_BASIC, field, 0, length)
511
512
513def oxm_tlv_header_w(field, length):
514    return _oxm_tlv_header(OFPXMC_OPENFLOW_BASIC, field, 1, length * 2)
515
516
517def oxm_tlv_header_extract_hasmask(header):
518    return (header >> 8) & 1
519
520
521def oxm_tlv_header_extract_length(header):
522    if oxm_tlv_header_extract_hasmask(header):
523        length = (header & 0xff) // 2
524    else:
525        length = header & 0xff
526    return length
527
528
529oxm_fields.generate(__name__)
530
531# generate utility methods
532ofproto_utils.generate(__name__)
533