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# Nicira extensions
18# Many of these definitions are common among OpenFlow versions.
19
20import sys
21from struct import calcsize
22from ryu.lib import type_desc
23from ryu.ofproto.ofproto_common import OFP_HEADER_SIZE
24from ryu.ofproto import oxm_fields
25
26# Action subtypes
27NXAST_RESUBMIT = 1
28NXAST_SET_TUNNEL = 2
29NXAST_DROP_SPOOFED_ARP__OBSOLETE = 3
30NXAST_SET_QUEUE = 4
31NXAST_POP_QUEUE = 5
32NXAST_REG_MOVE = 6
33NXAST_REG_LOAD = 7
34NXAST_NOTE = 8
35NXAST_SET_TUNNEL64 = 9
36NXAST_MULTIPATH = 10
37NXAST_AUTOPATH = 11
38NXAST_BUNDLE = 12
39NXAST_BUNDLE_LOAD = 13
40NXAST_RESUBMIT_TABLE = 14
41NXAST_OUTPUT_REG = 15
42NXAST_LEARN = 16
43NXAST_EXIT = 17
44NXAST_DEC_TTL = 18
45NXAST_FIN_TIMEOUT = 19
46NXAST_CONTROLLER = 20
47NXAST_DEC_TTL_CNT_IDS = 21
48NXAST_PUSH_MPLS = 23
49NXAST_POP_MPLS = 24
50NXAST_SET_MPLS_TTL = 25
51NXAST_DEC_MPLS_TTL = 26
52NXAST_STACK_PUSH = 27
53NXAST_STACK_POP = 28
54NXAST_SAMPLE = 29
55NXAST_SET_MPLS_LABEL = 30
56NXAST_SET_MPLS_TC = 31
57NXAST_OUTPUT_REG2 = 32
58NXAST_REG_LOAD2 = 33
59NXAST_CONJUNCTION = 34
60NXAST_CT = 35
61NXAST_NAT = 36
62NXAST_CONTROLLER2 = 37
63NXAST_SAMPLE2 = 38
64NXAST_OUTPUT_TRUNC = 39
65NXAST_CT_CLEAR = 43
66NXAST_DEC_NSH_TTL = 48
67
68NX_ACTION_RESUBMIT_PACK_STR = '!HHIHHB3x'
69NX_ACTION_RESUBMIT_SIZE = 16
70assert calcsize(NX_ACTION_RESUBMIT_PACK_STR) == NX_ACTION_RESUBMIT_SIZE
71
72NX_ACTION_SET_TUNNEL_PACK_STR = '!HHIH2xI'
73NX_ACTION_SET_TUNNEL_SIZE = 16
74assert calcsize(NX_ACTION_SET_TUNNEL_PACK_STR) == NX_ACTION_SET_TUNNEL_SIZE
75
76NX_ACTION_SET_QUEUE_PACK_STR = '!HHIH2xI'
77NX_ACTION_SET_QUEUE_SIZE = 16
78assert calcsize(NX_ACTION_SET_QUEUE_PACK_STR) == NX_ACTION_SET_QUEUE_SIZE
79
80NX_ACTION_POP_QUEUE_PACK_STR = '!HHIH6x'
81NX_ACTION_POP_QUEUE_SIZE = 16
82assert calcsize(NX_ACTION_POP_QUEUE_PACK_STR) == NX_ACTION_POP_QUEUE_SIZE
83
84NX_ACTION_REG_MOVE_PACK_STR = '!HHIHHHHII'
85NX_ACTION_REG_MOVE_SIZE = 24
86assert calcsize(NX_ACTION_REG_MOVE_PACK_STR) == NX_ACTION_REG_MOVE_SIZE
87
88NX_ACTION_REG_LOAD_PACK_STR = '!HHIHHIQ'
89NX_ACTION_REG_LOAD_SIZE = 24
90assert calcsize(NX_ACTION_REG_LOAD_PACK_STR) == NX_ACTION_REG_LOAD_SIZE
91
92NX_ACTION_SET_TUNNEL64_PACK_STR = '!HHIH6xQ'
93NX_ACTION_SET_TUNNEL64_SIZE = 24
94assert calcsize(NX_ACTION_SET_TUNNEL64_PACK_STR) == NX_ACTION_SET_TUNNEL64_SIZE
95
96NX_ACTION_MULTIPATH_PACK_STR = '!HHIHHH2xHHI2xHI'
97NX_ACTION_MULTIPATH_SIZE = 32
98assert calcsize(NX_ACTION_MULTIPATH_PACK_STR) == NX_ACTION_MULTIPATH_SIZE
99
100NX_ACTION_NOTE_PACK_STR = '!HHIH6B'
101NX_ACTION_NOTE_SIZE = 16
102assert calcsize(NX_ACTION_NOTE_PACK_STR) == NX_ACTION_NOTE_SIZE
103
104NX_ACTION_BUNDLE_PACK_STR = '!HHIHHHHIHHI4x'
105NX_ACTION_BUNDLE_SIZE = 32
106NX_ACTION_BUNDLE_0_SIZE = 24
107assert calcsize(NX_ACTION_BUNDLE_PACK_STR) == NX_ACTION_BUNDLE_SIZE
108
109NX_ACTION_AUTOPATH_PACK_STR = '!HHIHHII4x'
110NX_ACTION_AUTOPATH_SIZE = 24
111assert calcsize(NX_ACTION_AUTOPATH_PACK_STR) == NX_ACTION_AUTOPATH_SIZE
112
113NX_ACTION_OUTPUT_REG_PACK_STR = '!HHIHHIH6x'
114NX_ACTION_OUTPUT_REG_SIZE = 24
115assert calcsize(NX_ACTION_OUTPUT_REG_PACK_STR) == NX_ACTION_OUTPUT_REG_SIZE
116
117NX_ACTION_LEARN_PACK_STR = '!HHIHHHHQHBxHH'
118NX_ACTION_LEARN_SIZE = 32
119assert calcsize(NX_ACTION_LEARN_PACK_STR) == NX_ACTION_LEARN_SIZE
120
121NX_ACTION_CONTROLLER_PACK_STR = '!HHIHHHBB'
122NX_ACTION_CONTROLLER_SIZE = 16
123assert calcsize(NX_ACTION_CONTROLLER_PACK_STR) == NX_ACTION_CONTROLLER_SIZE
124
125NX_ACTION_FIN_TIMEOUT_PACK_STR = '!HHIHHH2x'
126NX_ACTION_FIN_TIMEOUT_SIZE = 16
127assert calcsize(NX_ACTION_FIN_TIMEOUT_PACK_STR) == NX_ACTION_FIN_TIMEOUT_SIZE
128
129NX_ACTION_HEADER_PACK_STR = '!HHIH6x'
130NX_ACTION_HEADER_SIZE = 16
131NX_ACTION_HEADER_0_SIZE = 2
132assert calcsize(NX_ACTION_HEADER_PACK_STR) == NX_ACTION_HEADER_SIZE
133
134# Messages
135NXT_ROLE_REQUEST = 10
136NXT_ROLE_REPLY = 11
137NXT_SET_FLOW_FORMAT = 12
138NXT_FLOW_MOD = 13
139NXT_FLOW_REMOVED = 14
140NXT_FLOW_MOD_TABLE_ID = 15
141NXT_SET_PACKET_IN_FORMAT = 16
142NXT_PACKET_IN = 17
143NXT_FLOW_AGE = 18
144NXT_SET_ASYNC_CONFIG = 19
145NXT_SET_CONTROLLER_ID = 20
146
147# enum nx_role
148NX_ROLE_OTHER = 0
149NX_ROLE_MASTER = 1
150NX_ROLE_SLAVE = 2
151
152# enum nx_flow_format
153NXFF_OPENFLOW10 = 0
154NXFF_NXM = 2
155
156# enum nx_packet_in_format
157NXPIF_OPENFLOW10 = 0
158NXPIF_NXM = 1
159
160# enum nx_stats_types
161NXST_FLOW = 0
162NXST_AGGREGATE = 1
163NXST_FLOW_MONITOR = 2
164
165# enum nx_action_controller2_prop_type
166NXAC2PT_MAX_LEN = 0
167NXAC2PT_CONTROLLER_ID = 1
168NXAC2PT_REASON = 2
169NXAC2PT_USERDATA = 3
170NXAC2PT_PAUSE = 4
171
172NICIRA_HEADER_PACK_STR = '!II'
173NICIRA_HEADER_SIZE = 16
174assert (calcsize(NICIRA_HEADER_PACK_STR) +
175        OFP_HEADER_SIZE == NICIRA_HEADER_SIZE)
176
177NX_ROLE_PACK_STR = '!I'
178NX_ROLE_SIZE = 20
179assert (calcsize(NX_ROLE_PACK_STR) +
180        NICIRA_HEADER_SIZE == NX_ROLE_SIZE)
181
182NX_FLOW_MOD_PACK_STR = '!Q4HI3H6x'
183NX_FLOW_MOD_SIZE = 48
184assert (calcsize(NX_FLOW_MOD_PACK_STR) +
185        NICIRA_HEADER_SIZE == NX_FLOW_MOD_SIZE)
186
187NX_SET_FLOW_FORMAT_PACK_STR = '!I'
188NX_SET_FLOW_FORMAT_SIZE = 20
189assert (calcsize(NX_SET_FLOW_FORMAT_PACK_STR) +
190        NICIRA_HEADER_SIZE == NX_SET_FLOW_FORMAT_SIZE)
191
192NX_FLOW_REMOVED_PACK_STR = '!QHBxIIHHQQ'
193NX_FLOW_REMOVED_SIZE = 56
194assert (calcsize(NX_FLOW_REMOVED_PACK_STR) +
195        NICIRA_HEADER_SIZE == NX_FLOW_REMOVED_SIZE)
196
197NX_FLOW_MOD_TABLE_ID_PACK_STR = '!B7x'
198NX_FLOW_MOD_TABLE_ID_SIZE = 24
199assert (calcsize(NX_FLOW_MOD_TABLE_ID_PACK_STR) +
200        NICIRA_HEADER_SIZE == NX_FLOW_MOD_TABLE_ID_SIZE)
201
202NX_SET_PACKET_IN_FORMAT_PACK_STR = '!I'
203NX_SET_PACKET_IN_FORMAT_SIZE = 20
204assert (calcsize(NX_SET_PACKET_IN_FORMAT_PACK_STR) +
205        NICIRA_HEADER_SIZE == NX_SET_PACKET_IN_FORMAT_SIZE)
206
207NX_PACKET_IN_PACK_STR = '!IHBBQH6x'
208NX_PACKET_IN_SIZE = 40
209assert (calcsize(NX_PACKET_IN_PACK_STR) +
210        NICIRA_HEADER_SIZE == NX_PACKET_IN_SIZE)
211
212NX_ASYNC_CONFIG_PACK_STR = '!IIIIII'
213NX_ASYNC_CONFIG_SIZE = 40
214assert (calcsize(NX_ASYNC_CONFIG_PACK_STR) +
215        NICIRA_HEADER_SIZE == NX_ASYNC_CONFIG_SIZE)
216
217NX_CONTROLLER_ID_PACK_STR = '!6xH'
218NX_CONTROLLER_ID_SIZE = 24
219assert (calcsize(NX_CONTROLLER_ID_PACK_STR) +
220        NICIRA_HEADER_SIZE == NX_CONTROLLER_ID_SIZE)
221
222NX_STATS_MSG_PACK_STR = '!I4x'
223NX_STATS_MSG0_SIZE = 8
224assert calcsize(NX_STATS_MSG_PACK_STR) == NX_STATS_MSG0_SIZE
225NX_STATS_MSG_SIZE = 24
226_OFP_VENDOR_STATS_MSG_SIZE = 16
227assert (calcsize(NX_STATS_MSG_PACK_STR) + _OFP_VENDOR_STATS_MSG_SIZE ==
228        NX_STATS_MSG_SIZE)
229
230NX_FLOW_STATS_REQUEST_PACK_STR = '!2HB3x'
231NX_FLOW_STATS_REQUEST_SIZE = 8
232assert (calcsize(NX_FLOW_STATS_REQUEST_PACK_STR) ==
233        NX_FLOW_STATS_REQUEST_SIZE)
234
235NX_FLOW_STATS_PACK_STR = '!HBxIIHHHHHHQQQ'
236NX_FLOW_STATS_SIZE = 48
237assert calcsize(NX_FLOW_STATS_PACK_STR) == NX_FLOW_STATS_SIZE
238
239NX_AGGREGATE_STATS_REQUEST_PACK_STR = '!2HB3x'
240NX_AGGREGATE_STATS_REQUEST_SIZE = 8
241assert (calcsize(NX_AGGREGATE_STATS_REQUEST_PACK_STR) ==
242        NX_AGGREGATE_STATS_REQUEST_SIZE)
243
244NX_AGGREGATE_STATS_REPLY_PACK_STR = '!QQI4x'
245NX_AGGREGATE_STATS_REPLY_SIZE = 24
246assert (calcsize(NX_AGGREGATE_STATS_REPLY_PACK_STR) ==
247        NX_AGGREGATE_STATS_REPLY_SIZE)
248
249# enum nx_hash_fields
250NX_HASH_FIELDS_ETH_SRC = 0
251NX_HASH_FIELDS_SYMMETRIC_L4 = 1
252
253# enum nx_mp_algorithm
254NX_MP_ALG_MODULO_N = 0
255NX_MP_ALG_HASH_THRESHOLD = 1
256NX_MP_ALG_HRW = 2
257NX_MP_ALG_ITER_HASH = 3
258
259# enum nx_bd_algorithm
260NX_BD_ALG_ACTIVE_BACKUP = 0
261NX_BD_ALG_HRW = 1
262
263# nx_learn constants
264NX_LEARN_N_BITS_MASK = 0x3ff
265NX_LEARN_SRC_FIELD = 0 << 13  # Copy from field.
266NX_LEARN_SRC_IMMEDIATE = 1 << 13  # Copy from immediate value.
267NX_LEARN_SRC_MASK = 1 << 13
268NX_LEARN_DST_MATCH = 0 << 11  # Add match criterion.
269NX_LEARN_DST_LOAD = 1 << 11  # Add NXAST_REG_LOAD action
270NX_LEARN_DST_OUTPUT = 2 << 11  # Add OFPAT_OUTPUT action.
271NX_LEARN_DST_RESERVED = 3 << 11  # Not yet defined.
272NX_LEARN_DST_MASK = 3 << 11
273
274# nx_ct constants
275NX_CT_F_COMMIT = 1 << 0
276NX_CT_F_FORCE = 1 << 1
277NX_CT_RECIRC_NONE = 0xff  # OFPTT_ALL
278
279# nx_nat constants
280NX_NAT_RANGE_IPV4_MIN = 1 << 0
281NX_NAT_RANGE_IPV4_MAX = 1 << 1
282NX_NAT_RANGE_IPV6_MIN = 1 << 2
283NX_NAT_RANGE_IPV6_MAX = 1 << 3
284NX_NAT_RANGE_PROTO_MIN = 1 << 4
285NX_NAT_RANGE_PROTO_MAX = 1 << 5
286
287# nx ip_frag constants
288FLOW_NW_FRAG_ANY = 1 << 0    # Set for any IP frag.
289FLOW_NW_FRAG_LATER = 1 << 1  # Set for IP frag with nonzero offset.
290FLOW_NW_FRAG_MASK = FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER
291
292# nx ip_frag match values
293NXM_IP_FRAG_NO = (0, FLOW_NW_FRAG_MASK)
294NXM_IP_FRAG_YES = (FLOW_NW_FRAG_ANY, FLOW_NW_FRAG_ANY)
295NXM_IP_FRAG_FIRST = (FLOW_NW_FRAG_ANY, FLOW_NW_FRAG_MASK)
296NXM_IP_FRAG_LATER = (FLOW_NW_FRAG_LATER, FLOW_NW_FRAG_LATER)
297NXM_IP_FRAG_NOT_LATER = (0, FLOW_NW_FRAG_LATER)
298
299
300def ofs_nbits(start, end):
301    r"""
302    The utility method for ofs_nbits
303
304    This method is used in the class to set the ofs_nbits.
305
306    This method converts start/end bits into ofs_nbits required to
307    specify the bit range of OXM/NXM fields.
308
309    ofs_nbits can be calculated as following::
310
311      ofs_nbits = (start << 6) + (end - start)
312
313    The parameter start/end  means the OXM/NXM field of ovs-ofctl command.
314
315    ..
316      field[start..end]
317    ..
318
319    +------------------------------------------+
320    | *field*\ **[**\ *start*\..\ *end*\ **]** |
321    +------------------------------------------+
322
323    ================ ======================================================
324    Attribute        Description
325    ================ ======================================================
326    start            Start bit for OXM/NXM field
327    end              End bit for OXM/NXM field
328    ================ ======================================================
329    """
330    return (start << 6) + (end - start)
331
332
333def nxm_header__(vendor, field, hasmask, length):
334    return (vendor << 16) | (field << 9) | (hasmask << 8) | length
335
336
337def nxm_header(vendor, field, length):
338    return nxm_header__(vendor, field, 0, length)
339
340
341def nxm_header_w(vendor, field, length):
342    return nxm_header__(vendor, field, 1, (length) * 2)
343
344
345NXM_OF_IN_PORT = nxm_header(0x0000, 0, 2)
346
347NXM_OF_ETH_DST = nxm_header(0x0000, 1, 6)
348NXM_OF_ETH_DST_W = nxm_header_w(0x0000, 1, 6)
349NXM_OF_ETH_SRC = nxm_header(0x0000, 2, 6)
350NXM_OF_ETH_SRC_W = nxm_header_w(0x0000, 2, 6)
351NXM_OF_ETH_TYPE = nxm_header(0x0000, 3, 2)
352
353NXM_OF_VLAN_TCI = nxm_header(0x0000, 4, 2)
354NXM_OF_VLAN_TCI_W = nxm_header_w(0x0000, 4, 2)
355
356NXM_OF_IP_TOS = nxm_header(0x0000, 5, 1)
357
358NXM_OF_IP_PROTO = nxm_header(0x0000, 6, 1)
359
360NXM_OF_IP_SRC = nxm_header(0x0000, 7, 4)
361NXM_OF_IP_SRC_W = nxm_header_w(0x0000, 7, 4)
362NXM_OF_IP_DST = nxm_header(0x0000, 8, 4)
363NXM_OF_IP_DST_W = nxm_header_w(0x0000, 8, 4)
364
365NXM_OF_TCP_SRC = nxm_header(0x0000, 9, 2)
366NXM_OF_TCP_SRC_W = nxm_header_w(0x0000, 9, 2)
367NXM_OF_TCP_DST = nxm_header(0x0000, 10, 2)
368NXM_OF_TCP_DST_W = nxm_header_w(0x0000, 10, 2)
369
370NXM_OF_UDP_SRC = nxm_header(0x0000, 11, 2)
371NXM_OF_UDP_SRC_W = nxm_header_w(0x0000, 11, 2)
372NXM_OF_UDP_DST = nxm_header(0x0000, 12, 2)
373NXM_OF_UDP_DST_W = nxm_header_w(0x0000, 12, 2)
374
375NXM_OF_ICMP_TYPE = nxm_header(0x0000, 13, 1)
376NXM_OF_ICMP_CODE = nxm_header(0x0000, 14, 1)
377
378NXM_OF_ARP_OP = nxm_header(0x0000, 15, 2)
379
380NXM_OF_ARP_SPA = nxm_header(0x0000, 16, 4)
381NXM_OF_ARP_SPA_W = nxm_header_w(0x0000, 16, 4)
382NXM_OF_ARP_TPA = nxm_header(0x0000, 17, 4)
383NXM_OF_ARP_TPA_W = nxm_header_w(0x0000, 17, 4)
384
385NXM_NX_TUN_ID = nxm_header(0x0001, 16, 8)
386NXM_NX_TUN_ID_W = nxm_header_w(0x0001, 16, 8)
387NXM_NX_TUN_IPV4_SRC = nxm_header(0x0001, 31, 4)
388NXM_NX_TUN_IPV4_SRC_W = nxm_header_w(0x0001, 31, 4)
389NXM_NX_TUN_IPV4_DST = nxm_header(0x0001, 32, 4)
390NXM_NX_TUN_IPV4_DST_W = nxm_header_w(0x0001, 32, 4)
391
392NXM_NX_ARP_SHA = nxm_header(0x0001, 17, 6)
393NXM_NX_ARP_THA = nxm_header(0x0001, 18, 6)
394
395NXM_NX_IPV6_SRC = nxm_header(0x0001, 19, 16)
396NXM_NX_IPV6_SRC_W = nxm_header_w(0x0001, 19, 16)
397NXM_NX_IPV6_DST = nxm_header(0x0001, 20, 16)
398NXM_NX_IPV6_DST_W = nxm_header_w(0x0001, 20, 16)
399
400NXM_NX_ICMPV6_TYPE = nxm_header(0x0001, 21, 1)
401NXM_NX_ICMPV6_CODE = nxm_header(0x0001, 22, 1)
402
403NXM_NX_ND_TARGET = nxm_header(0x0001, 23, 16)
404NXM_NX_ND_TARGET_W = nxm_header_w(0x0001, 23, 16)
405
406NXM_NX_ND_SLL = nxm_header(0x0001, 24, 6)
407
408NXM_NX_ND_TLL = nxm_header(0x0001, 25, 6)
409
410NXM_NX_IP_FRAG = nxm_header(0x0001, 26, 1)
411NXM_NX_IP_FRAG_W = nxm_header_w(0x0001, 26, 1)
412
413NXM_NX_IPV6_LABEL = nxm_header(0x0001, 27, 4)
414
415NXM_NX_IP_ECN = nxm_header(0x0001, 28, 1)
416
417NXM_NX_IP_TTL = nxm_header(0x0001, 29, 1)
418
419NXM_NX_PKT_MARK = nxm_header(0x0001, 33, 4)
420NXM_NX_PKT_MARK_W = nxm_header_w(0x0001, 33, 4)
421
422NXM_NX_TCP_FLAGS = nxm_header(0x0001, 34, 2)
423NXM_NX_TCP_FLAGS_W = nxm_header_w(0x0001, 34, 2)
424
425
426def nxm_nx_reg(idx):
427    return nxm_header(0x0001, idx, 4)
428
429
430def nxm_nx_reg_w(idx):
431    return nxm_header_w(0x0001, idx, 4)
432
433
434NXM_HEADER_PACK_STRING = '!I'
435
436#
437# The followings are implementations for OpenFlow 1.2+
438#
439
440sys.modules[__name__].__doc__ = """
441The API of this class is the same as ``OFPMatch``.
442
443You can define the flow match by the keyword arguments.
444The following arguments are available.
445
446================ =============== ==============================================
447Argument         Value           Description
448================ =============== ==============================================
449in_port_nxm      Integer 16bit   OpenFlow port number.
450eth_dst_nxm      MAC address     Ethernet destination address.
451eth_src_nxm      MAC address     Ethernet source address.
452eth_type_nxm     Integer 16bit   Ethernet type.  Needed to support Nicira
453                                 extensions that require the eth_type to
454                                 be set. (i.e. tcp_flags_nxm)
455vlan_tci         Integer 16bit   VLAN TCI. Basically same as vlan_vid plus
456                                 vlan_pcp.
457nw_tos           Integer 8bit    IP ToS or IPv6 traffic class field dscp.
458                                 Requires setting fields:
459                                 eth_type_nxm = [0x0800 (IPv4)|0x86dd (IPv6)]
460ip_proto_nxm     Integer 8bit    IP protocol. Needed to support Nicira
461                                 extensions that require the ip_proto to
462                                 be set. (i.e. tcp_flags_nxm)
463                                 Requires setting fields:
464                                 eth_type_nxm = [0x0800 (IPv4)|0x86dd (IPv6)]
465ipv4_src_nxm     IPv4 address    IPv4 source address.
466                                 Requires setting fields:
467                                 eth_type_nxm = 0x0800 (IPv4)
468ipv4_dst_nxm     IPv4 address    IPv4 destination address.
469                                 Requires setting fields:
470                                 eth_type_nxm = 0x0800 (IPv4)
471tcp_src_nxm      Integer 16bit   TCP source port.
472                                 Requires setting fields:
473                                 eth_type_nxm = [0x0800 (IPv4)|0x86dd (IPv6)]
474                                 and ip_proto_nxm = 6 (TCP)
475tcp_dst_nxm      Integer 16bit   TCP destination port.
476                                 Requires setting fields:
477                                 eth_type_nxm = [0x0800 (IPv4)|0x86dd (IPv6)]
478                                 and ip_proto_nxm = 6 (TCP)
479udp_src_nxm      Integer 16bit   UDP source port.
480                                 Requires setting fields:
481                                 eth_type_nxm = [0x0800 (IPv4)|0x86dd (IPv6)]
482                                 and ip_proto_nxm = 17 (UDP)
483udp_dst_nxm      Integer 16bit   UDP destination port.
484                                 eth_type_nxm = [0x0800 (IPv4)|0x86dd (IPv6)]
485                                 and ip_proto_nxm = 17 (UDP)
486icmpv4_type_nxm  Integer 8bit    Type  matches  the ICMP type and code matches
487                                 the ICMP code.
488                                 Requires setting fields:
489                                 eth_type_nxm = 0x0800 (IPv4) and
490                                 ip_proto_nxm = 1 (ICMP)
491icmpv4_code_nxm  Integer 8bit    Type  matches  the ICMP type and code matches
492                                 the ICMP code.
493                                 Requires setting fields:
494                                 eth_type_nxm = 0x0800 (IPv4) and
495                                 ip_proto_nxm = 1 (ICMP)
496arp_op_nxm       Integer 16bit   Only ARP opcodes between 1 and 255 should be
497                                 specified for matching.
498                                 Requires setting fields:
499                                 eth_type_nxm = 0x0806 (ARP)
500arp_spa_nxm      IPv4 address    An address may be specified as an IP address
501                                 or host name.
502                                 Requires setting fields:
503                                 eth_type_nxm = 0x0806 (ARP)
504arp_tpa_nxm      IPv4 address    An address may be specified as an IP address
505                                 or host name.
506                                 Requires setting fields:
507                                 eth_type_nxm = 0x0806 (ARP)
508tunnel_id_nxm    Integer 64bit   Tunnel identifier.
509arp_sha_nxm      MAC address     An address is specified as 6 pairs of
510                                 hexadecimal digits delimited by colons.
511                                 Requires setting fields:
512                                 eth_type_nxm = 0x0806 (ARP)
513arp_tha_nxm      MAC address     An address is specified as 6 pairs of
514                                 hexadecimal digits delimited by colons.
515                                 Requires setting fields:
516                                 eth_type_nxm = 0x0806 (ARP)
517ipv6_src_nxm     IPv6 address    IPv6 source address.
518                                 Requires setting fields:
519                                 eth_type_nxm = 0x86dd (IPv6)
520ipv6_dst_nxm     IPv6 address    IPv6 destination address.
521                                 Requires setting fields:
522                                 eth_type_nxm = 0x86dd (IPv6)
523icmpv6_type_nxm  Integer 8bit    Type  matches the ICMP type and code matches
524                                 the ICMP code.
525                                 Requires setting fields:
526                                 eth_type_nxm = 0x86dd (IPv6) and
527                                 ip_proto_nxm = 58 (ICMP for IPv6)
528icmpv6_code_nxm  Integer 8bit    Type  matches the ICMP type and code matches
529                                 the ICMP code.
530                                 Requires setting fields:
531                                 eth_type_nxm = 0x86dd (IPv6) and
532                                 ip_proto_nxm = 58 (ICMP for IPv6)
533nd_target        IPv6 address    The target address ipv6.
534                                 Requires setting fields:
535                                 eth_type_nxm = 0x86dd (IPv6) and
536                                 ip_proto_nxm = 58 (ICMP for IPv6)
537nd_sll           MAC address     The source link-layer address option.
538                                 Requires setting fields:
539                                 eth_type_nxm = 0x86dd (IPv6) and
540                                 ip_proto_nxm = 58 (ICMP for IPv6) and
541                                 icmpv6_type_nxm = 135 (Neighbor solicitation)
542nd_tll           MAC address     The target link-layer address option.
543                                 Requires setting fields:
544                                 eth_type_nxm = 0x86dd (IPv6) and
545                                 ip_proto_nxm = 58 (ICMP for IPv6) and
546                                 icmpv6_type_nxm = 136 (Neighbor advertisement)
547ip_frag          Integer 8bit    frag_type specifies what kind of IP fragments
548                                 or non-fragments to match.
549                                 Requires setting fields:
550                                 eth_type_nxm = [0x0800 (IPv4)|0x86dd (IPv6)]
551ipv6_label       Integer 32bit   Matches IPv6 flow label.
552                                 Requires setting fields:
553                                 eth_type_nxm = 0x86dd (IPv6)
554ip_ecn_nxm       Integer 8bit    Matches ecn bits in IP ToS or IPv6 traffic
555                                 class fields.
556                                 Requires setting fields:
557                                 eth_type_nxm = [0x0800 (IPv4)|0x86dd (IPv6)]
558nw_ttl           Integer 8bit    IP TTL or IPv6 hop limit value ttl.
559                                 Requires setting fields:
560                                 eth_type_nxm = [0x0800 (IPv4)|0x86dd (IPv6)]
561mpls_ttl         Integer 8bit    The TTL of the outer MPLS label stack entry
562                                 of a packet.
563                                 Requires setting fields:
564                                 eth_type_nxm = 0x8847 (MPLS Unicast)
565tun_ipv4_src     IPv4 address    Tunnel IPv4 source address.
566                                 Requires setting fields:
567                                 eth_type_nxm = 0x0800 (IPv4)
568tun_ipv4_dst     IPv4 address    Tunnel IPv4 destination address.
569                                 Requires setting fields:
570                                 eth_type_nxm = 0x0800 (IPv4)
571pkt_mark         Integer 32bit   Packet metadata mark.
572tcp_flags_nxm    Integer 16bit   TCP Flags.  Requires setting fields:
573                                 eth_type_nxm = [0x0800 (IP)|0x86dd (IPv6)] and
574                                 ip_proto_nxm = 6 (TCP)
575conj_id          Integer 32bit   Conjunction ID used only with
576                                 the conjunction action
577tun_gbp_id       Integer 16bit   The group policy identifier in the
578                                 VXLAN header.
579tun_gbp_flags    Integer 8bit    The group policy flags in the
580                                 VXLAN header.
581tun_flags        Integer 16bit   Flags indicating various aspects of
582                                 the tunnel encapsulation.
583ct_state         Integer 32bit   Conntrack state.
584ct_zone          Integer 16bit   Conntrack zone.
585ct_mark          Integer 32bit   Conntrack mark.
586ct_label         Integer 128bit  Conntrack label.
587tun_ipv6_src     IPv6 address    Tunnel IPv6 source address.
588                                 Requires setting fields:
589                                 eth_type_nxm = 0x86dd (IPv6)
590tun_ipv6_dst     IPv6 address    Tunnel IPv6 destination address.
591                                 Requires setting fields:
592                                 eth_type_nxm = 0x86dd (IPv6)
593_recirc_id       Integer 32bit   ID for recirculation.
594_dp_hash         Integer 32bit   Flow hash computed in Datapath.
595nsh_flags        Integer 8bit    Flags field in NSH Base Header.
596                                 Requires eth_type_nxm = 0x894f (NSH).
597                                 Since OpenFlow 1.3 and OVS v2.8.
598nsh_mdtype       Integer 8bit    Metadata Type in NSH Base Header.
599                                 Requires eth_type_nxm = 0x894f (NSH).
600                                 Since OpenFlow 1.3 and OVS v2.8.
601nsh_np           Integer 8bit    Next Protocol type in NSH Base Header.
602                                 Requires eth_type_nxm = 0x894f (NSH).
603                                 Since OpenFlow 1.3 and OVS v2.8.
604nsh_spi          Integer 32bit   Service Path Identifier in NSH Service Path
605                                 Header.
606                                 Requires eth_type_nxm = 0x894f (NSH).
607                                 Since OpenFlow 1.3 and OVS v2.8.
608nsh_si           Integer 8bit    Service Index in NSH Service Path Header.
609                                 Requires eth_type_nxm = 0x894f (NSH).
610                                 Since OpenFlow 1.3 and OVS v2.8.
611nsh_c<N>         Integer 32bit   Context fields in NSH Context Header.
612                                 <N> is a number of 1-4.
613                                 Requires eth_type_nxm = 0x894f (NSH).
614                                 Since OpenFlow 1.3 and OVS v2.8.
615nsh_ttl          Integer 8bit    TTL field in NSH Base Header.
616                                 Requires eth_type_nxm = 0x894f (NSH).
617                                 Since OpenFlow 1.3 and OVS v2.9.
618reg<idx>         Integer 32bit   Packet register.
619                                 <idx> is register number 0-15.
620xxreg<idx>       Integer 128bit  Packet extended-extended register.
621                                 <idx> is register number 0-3.
622================ =============== ==============================================
623
624.. Note::
625
626    Setting the TCP flags via the nicira extensions.
627    This is required when using OVS version < 2.4.
628    When using the nxm fields, you need to use any nxm prereq
629    fields as well or you will receive a OFPBMC_BAD_PREREQ error
630
631    Example::
632
633        # WILL NOT work
634        flag = tcp.TCP_ACK
635        match = parser.OFPMatch(
636            tcp_flags_nxm=(flag, flag),
637            ip_proto=inet.IPPROTO_TCP,
638            eth_type=eth_type)
639
640        # Works
641        flag = tcp.TCP_ACK
642        match = parser.OFPMatch(
643            tcp_flags_nxm=(flag, flag),
644            ip_proto_nxm=inet.IPPROTO_TCP,
645            eth_type_nxm=eth_type)
646"""
647
648oxm_types = [
649    # OFPXMC_NXM_0
650    oxm_fields.NiciraExtended0('in_port_nxm', 0, type_desc.Int2),
651    oxm_fields.NiciraExtended0('eth_dst_nxm', 1, type_desc.MacAddr),
652    oxm_fields.NiciraExtended0('eth_src_nxm', 2, type_desc.MacAddr),
653    oxm_fields.NiciraExtended0('eth_type_nxm', 3, type_desc.Int2),
654    oxm_fields.NiciraExtended0('vlan_tci', 4, type_desc.Int2),
655    oxm_fields.NiciraExtended0('nw_tos', 5, type_desc.Int1),
656    oxm_fields.NiciraExtended0('ip_proto_nxm', 6, type_desc.Int1),
657    oxm_fields.NiciraExtended0('ipv4_src_nxm', 7, type_desc.IPv4Addr),
658    oxm_fields.NiciraExtended0('ipv4_dst_nxm', 8, type_desc.IPv4Addr),
659    oxm_fields.NiciraExtended0('tcp_src_nxm', 9, type_desc.Int2),
660    oxm_fields.NiciraExtended0('tcp_dst_nxm', 10, type_desc.Int2),
661    oxm_fields.NiciraExtended0('udp_src_nxm', 11, type_desc.Int2),
662    oxm_fields.NiciraExtended0('udp_dst_nxm', 12, type_desc.Int2),
663    oxm_fields.NiciraExtended0('icmpv4_type_nxm', 13, type_desc.Int1),
664    oxm_fields.NiciraExtended0('icmpv4_code_nxm', 14, type_desc.Int1),
665    oxm_fields.NiciraExtended0('arp_op_nxm', 15, type_desc.Int2),
666    oxm_fields.NiciraExtended0('arp_spa_nxm', 16, type_desc.IPv4Addr),
667    oxm_fields.NiciraExtended0('arp_tpa_nxm', 17, type_desc.IPv4Addr),
668
669    # OFPXMC_NXM_1
670    oxm_fields.NiciraExtended1('tunnel_id_nxm', 16, type_desc.Int8),
671    oxm_fields.NiciraExtended1('arp_sha_nxm', 17, type_desc.MacAddr),
672    oxm_fields.NiciraExtended1('arp_tha_nxm', 18, type_desc.MacAddr),
673    oxm_fields.NiciraExtended1('ipv6_src_nxm', 19, type_desc.IPv6Addr),
674    oxm_fields.NiciraExtended1('ipv6_dst_nxm', 20, type_desc.IPv6Addr),
675    oxm_fields.NiciraExtended1('icmpv6_type_nxm', 21, type_desc.Int1),
676    oxm_fields.NiciraExtended1('icmpv6_code_nxm', 22, type_desc.Int1),
677    oxm_fields.NiciraExtended1('nd_target', 23, type_desc.IPv6Addr),
678    oxm_fields.NiciraExtended1('nd_sll', 24, type_desc.MacAddr),
679    oxm_fields.NiciraExtended1('nd_tll', 25, type_desc.MacAddr),
680    oxm_fields.NiciraExtended1('ip_frag', 26, type_desc.Int1),
681    oxm_fields.NiciraExtended1('ipv6_label', 27, type_desc.Int4),
682    oxm_fields.NiciraExtended1('ip_ecn_nxm', 28, type_desc.Int1),
683    oxm_fields.NiciraExtended1('nw_ttl', 29, type_desc.Int1),
684    oxm_fields.NiciraExtended1('mpls_ttl', 30, type_desc.Int1),
685    oxm_fields.NiciraExtended1('tun_ipv4_src', 31, type_desc.IPv4Addr),
686    oxm_fields.NiciraExtended1('tun_ipv4_dst', 32, type_desc.IPv4Addr),
687    oxm_fields.NiciraExtended1('pkt_mark', 33, type_desc.Int4),
688    oxm_fields.NiciraExtended1('tcp_flags_nxm', 34, type_desc.Int2),
689    oxm_fields.NiciraExtended1('conj_id', 37, type_desc.Int4),
690    oxm_fields.NiciraExtended1('tun_gbp_id', 38, type_desc.Int2),
691    oxm_fields.NiciraExtended1('tun_gbp_flags', 39, type_desc.Int1),
692    oxm_fields.NiciraExtended1('tun_flags', 104, type_desc.Int2),
693    oxm_fields.NiciraExtended1('ct_state', 105, type_desc.Int4),
694    oxm_fields.NiciraExtended1('ct_zone', 106, type_desc.Int2),
695    oxm_fields.NiciraExtended1('ct_mark', 107, type_desc.Int4),
696    oxm_fields.NiciraExtended1('ct_label', 108, type_desc.Int16),
697    oxm_fields.NiciraExtended1('tun_ipv6_src', 109, type_desc.IPv6Addr),
698    oxm_fields.NiciraExtended1('tun_ipv6_dst', 110, type_desc.IPv6Addr),
699
700    # Prefix the name with '_' to indicate this is not intended to be used
701    # in wild.
702    # Because the following definitions are supposed to be internal use only
703    # in OVS.
704    oxm_fields.NiciraExtended1('_recirc_id', 36, type_desc.Int4),
705
706    # The following definition is merely for testing 64-bit experimenter OXMs.
707    # Following Open vSwitch, we use dp_hash for this purpose.
708    # Prefix the name with '_' to indicate this is not intended to be used
709    # in wild.
710    oxm_fields.NiciraExperimenter('_dp_hash', 0, type_desc.Int4),
711
712    # Nicira Experimenter for Network Service Header
713    oxm_fields.NiciraNshExperimenter('nsh_flags', 1, type_desc.Int1),
714    oxm_fields.NiciraNshExperimenter('nsh_mdtype', 2, type_desc.Int1),
715    oxm_fields.NiciraNshExperimenter('nsh_np', 3, type_desc.Int1),
716    # aka "nsp"
717    oxm_fields.NiciraNshExperimenter('nsh_spi', 4, type_desc.Int4),
718    # aka "nsi"
719    oxm_fields.NiciraNshExperimenter('nsh_si', 5, type_desc.Int1),
720    # aka "nshc<N>"
721    oxm_fields.NiciraNshExperimenter('nsh_c1', 6, type_desc.Int4),
722    oxm_fields.NiciraNshExperimenter('nsh_c2', 7, type_desc.Int4),
723    oxm_fields.NiciraNshExperimenter('nsh_c3', 8, type_desc.Int4),
724    oxm_fields.NiciraNshExperimenter('nsh_c4', 9, type_desc.Int4),
725    oxm_fields.NiciraNshExperimenter('nsh_ttl', 10, type_desc.Int1),
726
727    # Support for matching/setting NX registers 0-15
728    oxm_fields.NiciraExtended1('reg0', 0, type_desc.Int4),
729    oxm_fields.NiciraExtended1('reg1', 1, type_desc.Int4),
730    oxm_fields.NiciraExtended1('reg2', 2, type_desc.Int4),
731    oxm_fields.NiciraExtended1('reg3', 3, type_desc.Int4),
732    oxm_fields.NiciraExtended1('reg4', 4, type_desc.Int4),
733    oxm_fields.NiciraExtended1('reg5', 5, type_desc.Int4),
734    oxm_fields.NiciraExtended1('reg6', 6, type_desc.Int4),
735    oxm_fields.NiciraExtended1('reg7', 7, type_desc.Int4),
736    oxm_fields.NiciraExtended1('reg8', 8, type_desc.Int4),
737    oxm_fields.NiciraExtended1('reg9', 9, type_desc.Int4),
738    oxm_fields.NiciraExtended1('reg10', 10, type_desc.Int4),
739    oxm_fields.NiciraExtended1('reg11', 11, type_desc.Int4),
740    oxm_fields.NiciraExtended1('reg12', 12, type_desc.Int4),
741    oxm_fields.NiciraExtended1('reg13', 13, type_desc.Int4),
742    oxm_fields.NiciraExtended1('reg14', 14, type_desc.Int4),
743    oxm_fields.NiciraExtended1('reg15', 15, type_desc.Int4),
744
745    # Support for matching/setting NX extended-extended registers 0-3
746    oxm_fields.NiciraExtended1('xxreg0', 111, type_desc.Int16),
747    oxm_fields.NiciraExtended1('xxreg1', 112, type_desc.Int16),
748    oxm_fields.NiciraExtended1('xxreg2', 113, type_desc.Int16),
749    oxm_fields.NiciraExtended1('xxreg3', 114, type_desc.Int16),
750
751]
752