ofproto: Implement Nicira Extended Match flexible flow match (NXM).
[sliver-openvswitch.git] / include / openflow / nicira-ext.h
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks
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 implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef OPENFLOW_NICIRA_EXT_H
18 #define OPENFLOW_NICIRA_EXT_H 1
19
20 #include "openflow/openflow.h"
21 #include "openvswitch/types.h"
22
23 #define NICIRA_OUI_STR "002320"
24
25 /* The following vendor extensions, proposed by Nicira Networks, are not yet
26  * standardized, so they are not included in openflow.h.  Some of them may be
27  * suitable for standardization; others we never expect to standardize. */
28
29 #define NX_VENDOR_ID 0x00002320
30 \f
31 /* Nicira vendor-specific error messages extension.
32  *
33  * OpenFlow 1.0 has a set of predefined error types (OFPET_*) and codes (which
34  * are specific to each type).  It does not have any provision for
35  * vendor-specific error codes, and it does not even provide "generic" error
36  * codes that can apply to problems not anticipated by the OpenFlow
37  * specification authors.
38  *
39  * This extension attempts to address the problem by adding a generic "error
40  * vendor extension".  The extension works as follows: use NXET_VENDOR as type
41  * and NXVC_VENDOR_CODE as code, followed by struct nx_vendor_error with
42  * vendor-specific details, followed by at least 64 bytes of the failed
43  * request.
44  *
45  * It would be better to have a type-specific vendor extension, e.g. so that
46  * OFPET_BAD_ACTION could be used with vendor-specific code values.  But
47  * OFPET_BAD_ACTION and most other standardized types already specify that
48  * their 'data' values are (the start of) the OpenFlow message being replied
49  * to, so there is no room to insert a vendor ID.
50  *
51  * Currently this extension is only implemented by Open vSwitch, but it seems
52  * like a reasonable candidate for future standardization.
53  */
54
55 /* This is a random number to avoid accidental collision with any other
56  * vendor's extension. */
57 #define NXET_VENDOR 0xb0c2
58
59 /* ofp_error msg 'code' values for NXET_VENDOR. */
60 enum nx_vendor_code {
61     NXVC_VENDOR_ERROR           /* 'data' contains struct nx_vendor_error. */
62 };
63
64 /* 'data' for 'type' == NXET_VENDOR, 'code' == NXVC_VENDOR_ERROR. */
65 struct nx_vendor_error {
66     ovs_be32 vendor;            /* Vendor ID as in struct ofp_vendor_header. */
67     ovs_be16 type;              /* Vendor-defined type. */
68     ovs_be16 code;              /* Vendor-defined subtype. */
69     /* Followed by at least the first 64 bytes of the failed request. */
70 };
71 \f
72 /* Specific Nicira extension error numbers.
73  *
74  * These are the "code" values used in nx_vendor_error.  So far, the "type"
75  * values in nx_vendor_error are the same as those in ofp_error_msg.  That is,
76  * at Nicira so far we've only needed additional vendor-specific 'code' values,
77  * so we're using the existing 'type' values to avoid having to invent new ones
78  * that duplicate the current ones' meanings. */
79
80 /* Additional "code" values for OFPET_BAD_REQUEST. */
81 enum {
82 /* Nicira Extended Match (NXM) errors. */
83
84     /* Generic error code used when there is an error in an NXM sent to the
85      * switch.  The switch may use one of the more specific error codes below,
86      * if there is an appropriate one, to simplify debugging, but it is not
87      * required to do so. */
88     NXBRC_NXM_INVALID = 0x100,
89
90     /* The nxm_type, or nxm_type taken in combination with nxm_hasmask or
91      * nxm_length or both, is invalid or not implemented. */
92     NXBRC_NXM_BAD_TYPE = 0x101,
93
94     /* Invalid nxm_value. */
95     NXBRC_NXM_BAD_VALUE = 0x102,
96
97     /* Invalid nxm_mask. */
98     NXBRC_NXM_BAD_MASK = 0x103,
99
100     /* A prerequisite was not met. */
101     NXBRC_NXM_BAD_PREREQ = 0x104,
102
103     /* A given nxm_type was specified more than once. */
104     NXBRC_NXM_DUP_TYPE = 0x105
105 };
106
107 /* Additional "code" values for OFPET_FLOW_MOD_FAILED. */
108 enum {
109     /* Generic hardware error. */
110     NXFMFC_HARDWARE = 0x100,
111
112     /* A nonexistent table ID was specified in the "command" field of struct
113      * ofp_flow_mod, when the nxt_flow_mod_table_id extension is enabled.
114      * (This extension is not yet implemented on this branch of Open
115      * vSwitch.) */
116     NXFMFC_BAD_TABLE_ID = 0x101
117 };
118 \f
119 /* Nicira vendor requests and replies. */
120
121 /* Header for Nicira vendor requests and replies. */
122 struct nicira_header {
123     struct ofp_header header;
124     ovs_be32 vendor;            /* NX_VENDOR_ID. */
125     ovs_be32 subtype;           /* One of NXT_* below. */
126 };
127 OFP_ASSERT(sizeof(struct nicira_header) == 16);
128
129 /* Values for the 'subtype' member of struct nicira_header. */
130 enum nicira_type {
131     /* Switch status request.  The request body is an ASCII string that
132      * specifies a prefix of the key names to include in the output; if it is
133      * the null string, then all key-value pairs are included. */
134     NXT_STATUS_REQUEST,
135
136     /* Switch status reply.  The reply body is an ASCII string of key-value
137      * pairs in the form "key=value\n". */
138     NXT_STATUS_REPLY,
139
140     /* No longer used. */
141     NXT_ACT_SET_CONFIG__OBSOLETE,
142     NXT_ACT_GET_CONFIG__OBSOLETE,
143     NXT_COMMAND_REQUEST__OBSOLETE,
144     NXT_COMMAND_REPLY__OBSOLETE,
145     NXT_FLOW_END_CONFIG__OBSOLETE,
146     NXT_FLOW_END__OBSOLETE,
147     NXT_MGMT__OBSOLETE,
148
149     /* Use the high 32 bits of the cookie field as the tunnel ID in the flow
150      * match. */
151     NXT_TUN_ID_FROM_COOKIE,
152
153     /* Controller role support.  The request body is struct nx_role_request.
154      * The reply echos the request. */
155     NXT_ROLE_REQUEST,
156     NXT_ROLE_REPLY,
157
158     /* Flexible flow specification (aka NXM = Nicira Extended Match). */
159     NXT_SET_FLOW_FORMAT,        /* Set flow format. */
160     NXT_FLOW_MOD,               /* Analogous to OFPT_FLOW_MOD. */
161     NXT_FLOW_REMOVED            /* Analogous to OFPT_FLOW_REMOVED. */
162 };
163
164 /* Header for Nicira vendor stats request and reply messages. */
165 struct nicira_stats_msg {
166     struct ofp_header header;   /* OFPT_STATS_REQUEST or OFPT_STATS_REPLY. */
167     ovs_be16 type;              /* OFPST_VENDOR. */
168     ovs_be16 flags;             /* OFPSF_{REQ,REPLY}_*. */
169     ovs_be32 vendor;            /* NX_VENDOR_ID. */
170     ovs_be32 subtype;           /* One of NXST_* below. */
171     uint8_t pad[4];             /* Align to 64-bits. */
172 };
173 OFP_ASSERT(sizeof(struct nicira_stats_msg) == 24);
174
175 /* Values for the 'subtype' member of struct nicira_stats_msg. */
176 enum nicira_stats_type {
177     /* Flexible flow specification (aka NXM = Nicira Extended Match). */
178     NXST_FLOW,                  /* Analogous to OFPST_FLOW. */
179     NXST_AGGREGATE              /* Analogous to OFPST_AGGREGATE. */
180 };
181
182 /* NXT_TUN_ID_FROM_COOKIE request. */
183 struct nxt_tun_id_cookie {
184     struct ofp_header header;
185     uint32_t vendor;            /* NX_VENDOR_ID. */
186     uint32_t subtype;           /* NXT_TUN_ID_FROM_COOKIE */
187     uint8_t set;                /* Nonzero to enable, zero to disable. */
188     uint8_t pad[7];
189 };
190 OFP_ASSERT(sizeof(struct nxt_tun_id_cookie) == 24);
191
192 /* Configures the "role" of the sending controller.  The default role is:
193  *
194  *    - Other (NX_ROLE_OTHER), which allows the controller access to all
195  *      OpenFlow features.
196  *
197  * The other possible roles are a related pair:
198  *
199  *    - Master (NX_ROLE_MASTER) is equivalent to Other, except that there may
200  *      be at most one Master controller at a time: when a controller
201  *      configures itself as Master, any existing Master is demoted to the
202  *      Slave role.
203  *
204  *    - Slave (NX_ROLE_SLAVE) allows the controller read-only access to
205  *      OpenFlow features.  In particular attempts to modify the flow table
206  *      will be rejected with an OFPBRC_EPERM error.
207  *
208  *      Slave controllers also do not receive asynchronous messages
209  *      (OFPT_PACKET_IN, OFPT_FLOW_REMOVED, OFPT_PORT_STATUS).
210  */
211 struct nx_role_request {
212     struct nicira_header nxh;
213     uint32_t role;              /* One of NX_ROLE_*. */
214 };
215
216 enum nx_role {
217     NX_ROLE_OTHER,              /* Default role, full access. */
218     NX_ROLE_MASTER,             /* Full access, at most one. */
219     NX_ROLE_SLAVE               /* Read-only access. */
220 };
221 \f
222 /* Nicira vendor flow actions. */
223
224 enum nx_action_subtype {
225     NXAST_SNAT__OBSOLETE,       /* No longer used. */
226     NXAST_RESUBMIT,             /* struct nx_action_resubmit */
227     NXAST_SET_TUNNEL,           /* struct nx_action_set_tunnel */
228     NXAST_DROP_SPOOFED_ARP,     /* struct nx_action_drop_spoofed_arp */
229     NXAST_SET_QUEUE,            /* struct nx_action_set_queue */
230     NXAST_POP_QUEUE             /* struct nx_action_pop_queue */
231 };
232
233 /* Header for Nicira-defined actions. */
234 struct nx_action_header {
235     uint16_t type;                  /* OFPAT_VENDOR. */
236     uint16_t len;                   /* Length is 16. */
237     uint32_t vendor;                /* NX_VENDOR_ID. */
238     uint16_t subtype;               /* NXAST_*. */
239     uint8_t pad[6];
240 };
241 OFP_ASSERT(sizeof(struct nx_action_header) == 16);
242
243 /* Action structure for NXAST_RESUBMIT.
244  *
245  * NXAST_RESUBMIT searches the flow table again, using a flow that is slightly
246  * modified from the original lookup:
247  *
248  *    - The 'in_port' member of struct nx_action_resubmit is used as the flow's
249  *      in_port.
250  *
251  *    - If NXAST_RESUBMIT is preceded by actions that affect the flow
252  *      (e.g. OFPAT_SET_VLAN_VID), then the flow is updated with the new
253  *      values.
254  *
255  * Following the lookup, the original in_port is restored.
256  *
257  * If the modified flow matched in the flow table, then the corresponding
258  * actions are executed.  Afterward, actions following NXAST_RESUBMIT in the
259  * original set of actions, if any, are executed; any changes made to the
260  * packet (e.g. changes to VLAN) by secondary actions persist when those
261  * actions are executed, although the original in_port is restored.
262  *
263  * NXAST_RESUBMIT may be used any number of times within a set of actions.
264  *
265  * NXAST_RESUBMIT may nest to an implementation-defined depth.  Beyond this
266  * implementation-defined depth, further NXAST_RESUBMIT actions are simply
267  * ignored.  (Open vSwitch 1.0.1 and earlier did not support recursion.)
268  */
269 struct nx_action_resubmit {
270     uint16_t type;                  /* OFPAT_VENDOR. */
271     uint16_t len;                   /* Length is 16. */
272     uint32_t vendor;                /* NX_VENDOR_ID. */
273     uint16_t subtype;               /* NXAST_RESUBMIT. */
274     uint16_t in_port;               /* New in_port for checking flow table. */
275     uint8_t pad[4];
276 };
277 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
278
279 /* Action structure for NXAST_SET_TUNNEL.
280  *
281  * Sets the encapsulating tunnel ID. */
282 struct nx_action_set_tunnel {
283     uint16_t type;                  /* OFPAT_VENDOR. */
284     uint16_t len;                   /* Length is 16. */
285     uint32_t vendor;                /* NX_VENDOR_ID. */
286     uint16_t subtype;               /* NXAST_SET_TUNNEL. */
287     uint8_t pad[2];
288     uint32_t tun_id;                /* Tunnel ID. */
289 };
290 OFP_ASSERT(sizeof(struct nx_action_set_tunnel) == 16);
291
292 /* Action structure for NXAST_DROP_SPOOFED_ARP.
293  *
294  * Stops processing further actions, if the packet being processed is an
295  * Ethernet+IPv4 ARP packet for which the source Ethernet address inside the
296  * ARP packet differs from the source Ethernet address in the Ethernet header.
297  *
298  * This is useful because OpenFlow does not provide a way to match on the
299  * Ethernet addresses inside ARP packets, so there is no other way to drop
300  * spoofed ARPs other than sending every ARP packet to a controller. */
301 struct nx_action_drop_spoofed_arp {
302     uint16_t type;                  /* OFPAT_VENDOR. */
303     uint16_t len;                   /* Length is 16. */
304     uint32_t vendor;                /* NX_VENDOR_ID. */
305     uint16_t subtype;               /* NXAST_DROP_SPOOFED_ARP. */
306     uint8_t pad[6];
307 };
308 OFP_ASSERT(sizeof(struct nx_action_drop_spoofed_arp) == 16);
309
310 /* Action structure for NXAST_SET_QUEUE.
311  *
312  * Set the queue that should be used when packets are output.  This is similar
313  * to the OpenFlow OFPAT_ENQUEUE action, but does not take the output port as
314  * an argument.  This allows the queue to be defined before the port is
315  * known. */
316 struct nx_action_set_queue {
317     uint16_t type;                  /* OFPAT_VENDOR. */
318     uint16_t len;                   /* Length is 16. */
319     uint32_t vendor;                /* NX_VENDOR_ID. */
320     uint16_t subtype;               /* NXAST_SET_QUEUE. */
321     uint8_t pad[2];
322     uint32_t queue_id;              /* Where to enqueue packets. */
323 };
324 OFP_ASSERT(sizeof(struct nx_action_set_queue) == 16);
325
326 /* Action structure for NXAST_POP_QUEUE.
327  *
328  * Restores the queue to the value it was before any NXAST_SET_QUEUE actions
329  * were used.  Only the original queue can be restored this way; no stack is
330  * maintained. */
331 struct nx_action_pop_queue {
332     uint16_t type;                  /* OFPAT_VENDOR. */
333     uint16_t len;                   /* Length is 16. */
334     uint32_t vendor;                /* NX_VENDOR_ID. */
335     uint16_t subtype;               /* NXAST_POP_QUEUE. */
336     uint8_t pad[6];
337 };
338 OFP_ASSERT(sizeof(struct nx_action_pop_queue) == 16);
339
340 /* Wildcard for tunnel ID. */
341 #define NXFW_TUN_ID  (1 << 25)
342
343 #define NXFW_ALL NXFW_TUN_ID
344 #define OVSFW_ALL (OFPFW_ALL | NXFW_ALL)
345 \f
346 /* Flexible flow specifications (aka NXM = Nicira Extended Match).
347  *
348  * OpenFlow 1.0 has "struct ofp_match" for specifying flow matches.  This
349  * structure is fixed-length and hence difficult to extend.  This section
350  * describes a more flexible, variable-length flow match, called "nx_match" for
351  * short, that is also supported by Open vSwitch.  This section also defines a
352  * replacement for each OpenFlow message that includes struct ofp_match.
353  *
354  *
355  * Format
356  * ======
357  *
358  * An nx_match is a sequence of zero or more "nxm_entry"s, which are
359  * type-length-value (TLV) entries, each 5 to 259 (inclusive) bytes long.
360  * "nxm_entry"s are not aligned on or padded to any multibyte boundary.  The
361  * first 4 bytes of an nxm_entry are its "header", followed by the entry's
362  * "body".
363  *
364  * An nxm_entry's header is interpreted as a 32-bit word in network byte order:
365  *
366  * |<-------------------- nxm_type ------------------>|
367  * |                                                  |
368  * |31                              16 15            9| 8 7                0
369  * +----------------------------------+---------------+--+------------------+
370  * |            nxm_vendor            |   nxm_field   |hm|    nxm_length    |
371  * +----------------------------------+---------------+--+------------------+
372  *
373  * The most-significant 23 bits of the header are collectively "nxm_type".
374  * Bits 16...31 are "nxm_vendor", one of the NXM_VENDOR_* values below.  Bits
375  * 9...15 are "nxm_field", which is a vendor-specific value.  nxm_type normally
376  * designates a protocol header, such as the Ethernet type, but it can also
377  * refer to packet metadata, such as the switch port on which a packet arrived.
378  *
379  * Bit 8 is "nxm_hasmask" (labeled "hm" above for space reasons).  The meaning
380  * of this bit is explained later.
381  *
382  * The least-significant 8 bits are "nxm_length", a positive integer.  The
383  * length of the nxm_entry, including the header, is exactly 4 + nxm_length
384  * bytes.
385  *
386  * For a given nxm_vendor, nxm_field, and nxm_hasmask value, nxm_length is a
387  * constant.  It is included only to allow software to minimally parse
388  * "nxm_entry"s of unknown types.  (Similarly, for a given nxm_vendor,
389  * nxm_field, and nxm_length, nxm_hasmask is a constant.)
390  *
391  *
392  * Semantics
393  * =========
394  *
395  * A zero-length nx_match (one with no "nxm_entry"s) matches every packet.
396  *
397  * An nxm_entry places a constraint on the packets matched by the nx_match:
398  *
399  *   - If nxm_hasmask is 0, the nxm_entry's body contains a value for the
400  *     field, called "nxm_value".  The nx_match matches only packets in which
401  *     the field equals nxm_value.
402  *
403  *   - If nxm_hasmask is 1, then the nxm_entry's body contains a value for the
404  *     field (nxm_value), followed by a bitmask of the same length as the
405  *     value, called "nxm_mask".  For each 1-bit in position J in nxm_mask, the
406  *     nx_match matches only packets for which bit J in the given field's value
407  *     matches bit J in nxm_value.  A 0-bit in nxm_mask causes the
408  *     corresponding bits in nxm_value and the field's value to be ignored.
409  *     (The sense of the nxm_mask bits is the opposite of that used by the
410  *     "wildcards" member of struct ofp_match.)
411  *
412  *     When nxm_hasmask is 1, nxm_length is always even.
413  *
414  *     An all-zero-bits nxm_mask is equivalent to omitting the nxm_entry
415  *     entirely.  An all-one-bits nxm_mask is equivalent to specifying 0 for
416  *     nxm_hasmask.
417  *
418  * When there are multiple "nxm_entry"s, all of the constraints must be met.
419  *
420  *
421  * Mask Restrictions
422  * =================
423  *
424  * Masks may be restricted:
425  *
426  *   - Some nxm_types may not support masked wildcards, that is, nxm_hasmask
427  *     must always be 0 when these fields are specified.  For example, the
428  *     field that identifies the port on which a packet was received may not be
429  *     masked.
430  *
431  *   - Some nxm_types that do support masked wildcards may only support certain
432  *     nxm_mask patterns.  For example, fields that have IPv4 address values
433  *     may be restricted to CIDR masks.
434  *
435  * These restrictions should be noted in specifications for individual fields.
436  * A switch may accept an nxm_hasmask or nxm_mask value that the specification
437  * disallows, if the switch correctly implements support for that nxm_hasmask
438  * or nxm_mask value.  A switch must reject an attempt to set up a flow that
439  * contains a nxm_hasmask or nxm_mask value that it does not support.
440  *
441  *
442  * Prerequisite Restrictions
443  * =========================
444  *
445  * The presence of an nxm_entry with a given nxm_type may be restricted based
446  * on the presence of or values of other "nxm_entry"s.  For example:
447  *
448  *   - An nxm_entry for nxm_type=NXM_OF_IP_TOS is allowed only if it is
449  *     preceded by another entry with nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0,
450  *     and nxm_value=0x0800.  That is, matching on the IP source address is
451  *     allowed only if the Ethernet type is explicitly set to IP.
452  *
453  *   - An nxm_entry for nxm_type=NXM_OF_TCP_SRC is allowed only if it is preced
454  *     by an entry with nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0,
455  *     nxm_value=0x0800 and another with nxm_type=NXM_OF_IP_PROTO,
456  *     nxm_hasmask=0, nxm_value=6, in that order.  That is, matching on the TCP
457  *     source port is allowed only if the Ethernet type is IP and the IP
458  *     protocol is TCP.
459  *
460  * These restrictions should be noted in specifications for individual fields.
461  * A switch may implement relaxed versions of these restrictions.  A switch
462  * must reject an attempt to set up a flow that violates its restrictions.
463  *
464  *
465  * Ordering Restrictions
466  * =====================
467  *
468  * An nxm_entry that has prerequisite restrictions must appear after the
469  * "nxm_entry"s for its prerequisites.  Ordering of "nxm_entry"s within an
470  * nx_match is not otherwise constrained.
471  *
472  * Any given nxm_type may appear in an nx_match at most once.
473  *
474  *
475  * nxm_entry Examples
476  * ==================
477  *
478  * These examples show the format of a single nxm_entry with particular
479  * nxm_hasmask and nxm_length values.  The diagrams are labeled with field
480  * numbers and byte indexes.
481  *
482  *
483  * 8-bit nxm_value, nxm_hasmask=1, nxm_length=1:
484  *
485  *  0          3  4   5
486  * +------------+---+---+
487  * |   header   | v | m |
488  * +------------+---+---+
489  *
490  *
491  * 16-bit nxm_value, nxm_hasmask=0, nxm_length=2:
492  *
493  *  0          3 4    5
494  * +------------+------+
495  * |   header   | value|
496  * +------------+------+
497  *
498  *
499  * 32-bit nxm_value, nxm_hasmask=0, nxm_length=4:
500  *
501  *  0          3 4           7
502  * +------------+-------------+
503  * |   header   |  nxm_value  |
504  * +------------+-------------+
505  *
506  *
507  * 48-bit nxm_value, nxm_hasmask=0, nxm_length=6:
508  *
509  *  0          3 4                9
510  * +------------+------------------+
511  * |   header   |     nxm_value    |
512  * +------------+------------------+
513  *
514  *
515  * 48-bit nxm_value, nxm_hasmask=1, nxm_length=12:
516  *
517  *  0          3 4                9 10              15
518  * +------------+------------------+------------------+
519  * |   header   |     nxm_value    |      nxm_mask    |
520  * +------------+------------------+------------------+
521  *
522  *
523  * Error Reporting
524  * ===============
525  *
526  * A switch should report an error in an nx_match using error type
527  * OFPET_BAD_REQUEST and one of the NXBRC_NXM_* codes.  Ideally the switch
528  * should report a specific error code, if one is assigned for the particular
529  * problem, but NXBRC_NXM_INVALID is also available to report a generic
530  * nx_match error.
531  */
532
533 #define NXM_HEADER__(VENDOR, FIELD, HASMASK, LENGTH) \
534     (((VENDOR) << 16) | ((FIELD) << 9) | ((HASMASK) << 8) | (LENGTH))
535 #define NXM_HEADER(VENDOR, FIELD, LENGTH) \
536     NXM_HEADER__(VENDOR, FIELD, 0, LENGTH)
537 #define NXM_HEADER_W(VENDOR, FIELD, LENGTH) \
538     NXM_HEADER__(VENDOR, FIELD, 1, (LENGTH) * 2)
539 #define NXM_VENDOR(HEADER) ((HEADER) >> 16)
540 #define NXM_FIELD(HEADER) (((HEADER) >> 9) & 0x7f)
541 #define NXM_TYPE(HEADER) (((HEADER) >> 9) & 0x7fffff)
542 #define NXM_HASMASK(HEADER) (((HEADER) >> 8) & 1)
543 #define NXM_LENGTH(HEADER) ((HEADER) & 0xff)
544
545 #define NXM_MAKE_WILD_HEADER(HEADER) \
546         NXM_HEADER_W(NXM_VENDOR(HEADER), NXM_FIELD(HEADER), NXM_LENGTH(HEADER))
547
548 /* ## ------------------------------- ## */
549 /* ## OpenFlow 1.0-compatible fields. ## */
550 /* ## ------------------------------- ## */
551
552 /* Physical or virtual port on which the packet was received.
553  *
554  * Prereqs: None.
555  *
556  * Format: 16-bit integer in network byte order.
557  *
558  * Masking: Not maskable. */
559 #define NXM_OF_IN_PORT    NXM_HEADER  (0x0000,  0, 2)
560
561 /* Source or destination address in Ethernet header.
562  *
563  * Prereqs: None.
564  *
565  * Format: 48-bit Ethernet MAC address.
566  *
567  * Masking: Not maskable. */
568 #define NXM_OF_ETH_DST    NXM_HEADER  (0x0000,  1, 6)
569 #define NXM_OF_ETH_SRC    NXM_HEADER  (0x0000,  2, 6)
570
571 /* Packet's Ethernet type.
572  *
573  * For an Ethernet II packet this is taken from the Ethernet header.  For an
574  * 802.2 LLC+SNAP header with OUI 00-00-00 this is taken from the SNAP header.
575  * A packet that has neither format has value 0x05ff
576  * (OFP_DL_TYPE_NOT_ETH_TYPE).
577  *
578  * For a packet with an 802.1Q header, this is the type of the encapsulated
579  * frame.
580  *
581  * Prereqs: None.
582  *
583  * Format: 16-bit integer in network byte order.
584  *
585  * Masking: Not maskable. */
586 #define NXM_OF_ETH_TYPE   NXM_HEADER  (0x0000,  3, 2)
587
588 /* 802.1Q TCI.
589  *
590  * For a packet with an 802.1Q header, this is the Tag Control Information
591  * (TCI) field, with the CFI bit forced to 1.  For a packet with no 802.1Q
592  * header, this has value 0.
593  *
594  * Prereqs: None.
595  *
596  * Format: 16-bit integer in network byte order.
597  *
598  * Masking: Arbitrary masks.
599  *
600  * This field can be used in various ways:
601  *
602  *   - If it is not constrained at all, the nx_match matches packets without
603  *     an 802.1Q header or with an 802.1Q header that has any TCI value.
604  *
605  *   - Testing for an exact match with 0 matches only packets without an
606  *     802.1Q header.
607  *
608  *   - Testing for an exact match with a TCI value with CFI=1 matches packets
609  *     that have an 802.1Q header with a specified VID and PCP.
610  *
611  *   - Testing for an exact match with a nonzero TCI value with CFI=0 does
612  *     not make sense.  The switch may reject this combination.
613  *
614  *   - Testing with a specific VID and CFI=1, with nxm_mask=0x1fff, matches
615  *     packets that have an 802.1Q header with that VID (and any PCP).
616  *
617  *   - Testing with a specific PCP and CFI=1, with nxm_mask=0xf000, matches
618  *     packets that have an 802.1Q header with that PCP (and any VID).
619  *
620  *   - Testing with nxm_value=0, nxm_mask=0xe000 matches packets with no 802.1Q
621  *     header or with an 802.1Q header with a VID of 0.
622  */
623 #define NXM_OF_VLAN_TCI   NXM_HEADER  (0x0000,  4, 2)
624 #define NXM_OF_VLAN_TCI_W NXM_HEADER_W(0x0000,  4, 2)
625
626 /* The "type of service" byte of the IP header, with the ECN bits forced to 0.
627  *
628  * Prereqs: NXM_OF_ETH_TYPE must match 0x0800 exactly.
629  *
630  * Format: 8-bit integer with 2 least-significant bits forced to 0.
631  *
632  * Masking: Not maskable. */
633 #define NXM_OF_IP_TOS     NXM_HEADER  (0x0000,  5, 1)
634
635 /* The "protocol" byte in the IP header.
636  *
637  * Prereqs: NXM_OF_ETH_TYPE must match 0x0800 exactly.
638  *
639  * Format: 8-bit integer.
640  *
641  * Masking: Not maskable. */
642 #define NXM_OF_IP_PROTO   NXM_HEADER  (0x0000,  6, 1)
643
644 /* The source or destination address in the IP header.
645  *
646  * Prereqs: NXM_OF_ETH_TYPE must match 0x0800 exactly.
647  *
648  * Format: 32-bit integer in network byte order.
649  *
650  * Masking: Only CIDR masks are allowed, that is, masks that consist of N
651  *   high-order bits set to 1 and the other 32-N bits set to 0. */
652 #define NXM_OF_IP_SRC     NXM_HEADER  (0x0000,  7, 4)
653 #define NXM_OF_IP_SRC_W   NXM_HEADER_W(0x0000,  7, 4)
654 #define NXM_OF_IP_DST     NXM_HEADER  (0x0000,  8, 4)
655 #define NXM_OF_IP_DST_W   NXM_HEADER_W(0x0000,  8, 4)
656
657 /* The source or destination port in the TCP header.
658  *
659  * Prereqs:
660  *   NXM_OF_ETH_TYPE must match 0x0800 exactly.
661  *   NXM_OF_IP_PROTO must match 6 exactly.
662  *
663  * Format: 16-bit integer in network byte order.
664  *
665  * Masking: Not maskable. */
666 #define NXM_OF_TCP_SRC    NXM_HEADER  (0x0000,  9, 2)
667 #define NXM_OF_TCP_DST    NXM_HEADER  (0x0000, 10, 2)
668
669 /* The source or destination port in the UDP header.
670  *
671  * Prereqs:
672  *   NXM_OF_ETH_TYPE must match 0x0800 exactly.
673  *   NXM_OF_IP_PROTO must match 17 exactly.
674  *
675  * Format: 16-bit integer in network byte order.
676  *
677  * Masking: Not maskable. */
678 #define NXM_OF_UDP_SRC    NXM_HEADER  (0x0000, 11, 2)
679 #define NXM_OF_UDP_DST    NXM_HEADER  (0x0000, 12, 2)
680
681 /* The type or code in the ICMP header.
682  *
683  * Prereqs:
684  *   NXM_OF_ETH_TYPE must match 0x0800 exactly.
685  *   NXM_OF_IP_PROTO must match 1 exactly.
686  *
687  * Format: 8-bit integer.
688  *
689  * Masking: Not maskable. */
690 #define NXM_OF_ICMP_TYPE  NXM_HEADER  (0x0000, 13, 1)
691 #define NXM_OF_ICMP_CODE  NXM_HEADER  (0x0000, 14, 1)
692
693 /* ARP opcode.
694  *
695  * For an Ethernet+IP ARP packet, the opcode in the ARP header.  Always 0
696  * otherwise.  Only ARP opcodes between 1 and 255 should be specified for
697  * matching.
698  *
699  * Prereqs: NXM_OF_ETH_TYPE must match 0x0806 exactly.
700  *
701  * Format: 16-bit integer in network byte order.
702  *
703  * Masking: Not maskable. */
704 #define NXM_OF_ARP_OP     NXM_HEADER  (0x0000, 15, 2)
705
706 /* For an Ethernet+IP ARP packet, the source or target protocol address
707  * in the ARP header.  Always 0 otherwise.
708  *
709  * Prereqs: NXM_OF_ETH_TYPE must match 0x0806 exactly.
710  *
711  * Format: 32-bit integer in network byte order.
712  *
713  * Masking: Only CIDR masks are allowed, that is, masks that consist of N
714  *   high-order bits set to 1 and the other 32-N bits set to 0. */
715 #define NXM_OF_ARP_SPA    NXM_HEADER  (0x0000, 16, 4)
716 #define NXM_OF_ARP_SPA_W  NXM_HEADER_W(0x0000, 16, 4)
717 #define NXM_OF_ARP_TPA    NXM_HEADER  (0x0000, 17, 4)
718 #define NXM_OF_ARP_TPA_W  NXM_HEADER_W(0x0000, 17, 4)
719
720 /* ## ------------------------ ## */
721 /* ## Nicira match extensions. ## */
722 /* ## ------------------------ ## */
723
724 /* Tunnel ID.
725  *
726  * For a packet received via GRE tunnel including a (32-bit) key, the key is
727  * stored in the low 32-bits and the high bits are zeroed.  For other packets,
728  * the value is 0.
729  *
730  * Prereqs: None.
731  *
732  * Format: 64-bit integer in network byte order.
733  *
734  * Masking: Arbitrary masks. */
735 #define NXM_NX_TUN_ID     NXM_HEADER  (0x0001, 16, 8)
736 #define NXM_NX_TUN_ID_W   NXM_HEADER_W(0x0001, 16, 8)
737
738 /* ## --------------------- ## */
739 /* ## Requests and replies. ## */
740 /* ## --------------------- ## */
741
742 enum {
743     NXFF_OPENFLOW10 = 0,         /* Standard OpenFlow 1.0 compatible. */
744     NXFF_TUN_ID_FROM_COOKIE = 1, /* OpenFlow 1.0, plus obtain tunnel ID from
745                                   * cookie. */
746     NXFF_NXM = 2                 /* Nicira extended match. */
747 };
748
749 /* NXT_SET_FLOW_FORMAT request. */
750 struct nxt_set_flow_format {
751     struct ofp_header header;
752     ovs_be32 vendor;            /* NX_VENDOR_ID. */
753     ovs_be32 subtype;           /* NXT_SET_FLOW_FORMAT. */
754     ovs_be32 format;            /* One of NXFF_*. */
755 };
756 OFP_ASSERT(sizeof(struct nxt_set_flow_format) == 20);
757
758 /* NXT_FLOW_MOD (analogous to OFPT_FLOW_MOD). */
759 struct nx_flow_mod {
760     struct nicira_header nxh;
761     ovs_be64 cookie;              /* Opaque controller-issued identifier. */
762     ovs_be16 command;             /* One of OFPFC_*. */
763     ovs_be16 idle_timeout;        /* Idle time before discarding (seconds). */
764     ovs_be16 hard_timeout;        /* Max time before discarding (seconds). */
765     ovs_be16 priority;            /* Priority level of flow entry. */
766     ovs_be32 buffer_id;           /* Buffered packet to apply to (or -1).
767                                      Not meaningful for OFPFC_DELETE*. */
768     ovs_be16 out_port;            /* For OFPFC_DELETE* commands, require
769                                      matching entries to include this as an
770                                      output port.  A value of OFPP_NONE
771                                      indicates no restriction. */
772     ovs_be16 flags;               /* One of OFPFF_*. */
773     ovs_be16 match_len;           /* Size of nx_match. */
774     uint8_t pad[6];               /* Align to 64-bits. */
775     /* Followed by:
776      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
777      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
778      *     all-zero bytes, then
779      *   - Actions to fill out the remainder of the message length (always a
780      *     multiple of 8).
781      */
782 };
783 OFP_ASSERT(sizeof(struct nx_flow_mod) == 48);
784
785 /* NXT_FLOW_REMOVED (analogous to OFPT_FLOW_REMOVED). */
786 struct nx_flow_removed {
787     struct nicira_header nxh;
788     ovs_be64 cookie;          /* Opaque controller-issued identifier. */
789     ovs_be16 priority;        /* Priority level of flow entry. */
790     uint8_t reason;           /* One of OFPRR_*. */
791     uint8_t pad[1];           /* Align to 32-bits. */
792     ovs_be32 duration_sec;    /* Time flow was alive in seconds. */
793     ovs_be32 duration_nsec;   /* Time flow was alive in nanoseconds beyond
794                                  duration_sec. */
795     ovs_be16 idle_timeout;    /* Idle timeout from original flow mod. */
796     ovs_be16 match_len;       /* Size of nx_match. */
797     ovs_be64 packet_count;
798     ovs_be64 byte_count;
799     /* Followed by:
800      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
801      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
802      *     all-zero bytes. */
803 };
804 OFP_ASSERT(sizeof(struct nx_flow_removed) == 56);
805
806 /* Nicira vendor stats request of type NXST_FLOW (analogous to OFPST_FLOW
807  * request). */
808 struct nx_flow_stats_request {
809     struct nicira_stats_msg nsm;
810     ovs_be16 out_port;        /* Require matching entries to include this
811                                  as an output port.  A value of OFPP_NONE
812                                  indicates no restriction. */
813     ovs_be16 match_len;       /* Length of nx_match. */
814     uint8_t table_id;         /* ID of table to read (from ofp_table_stats)
815                                  or 0xff for all tables. */
816     uint8_t pad[3];           /* Align to 64 bits. */
817     /* Followed by:
818      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
819      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
820      *     all-zero bytes, which must also exactly fill out the length of the
821      *     message.
822      */
823 };
824 OFP_ASSERT(sizeof(struct nx_flow_stats_request) == 32);
825
826 /* Body for Nicira vendor stats reply of type NXST_FLOW (analogous to
827  * OFPST_FLOW reply). */
828 struct nx_flow_stats {
829     ovs_be16 length;          /* Length of this entry. */
830     uint8_t table_id;         /* ID of table flow came from. */
831     uint8_t pad;
832     ovs_be32 duration_sec;    /* Time flow has been alive in seconds. */
833     ovs_be32 duration_nsec;   /* Time flow has been alive in nanoseconds
834                                  beyond duration_sec. */
835     ovs_be16 priority;        /* Priority of the entry. Only meaningful
836                                  when this is not an exact-match entry. */
837     ovs_be16 idle_timeout;    /* Number of seconds idle before expiration. */
838     ovs_be16 hard_timeout;    /* Number of seconds before expiration. */
839     ovs_be16 match_len;       /* Length of nx_match. */
840     uint8_t pad2[4];          /* Align to 64 bits. */
841     ovs_be64 cookie;          /* Opaque controller-issued identifier. */
842     ovs_be64 packet_count;    /* Number of packets in flow. */
843     ovs_be64 byte_count;      /* Number of bytes in flow. */
844     /* Followed by:
845      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
846      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
847      *     all-zero bytes, then
848      *   - Actions to fill out the remainder 'length' bytes (always a multiple
849      *     of 8).
850      */
851 };
852 OFP_ASSERT(sizeof(struct nx_flow_stats) == 48);
853
854 /* Nicira vendor stats request of type NXST_AGGREGATE (analogous to
855  * OFPST_AGGREGATE request). */
856 struct nx_aggregate_stats_request {
857     struct nicira_stats_msg nsm;
858     ovs_be16 out_port;        /* Require matching entries to include this
859                                  as an output port.  A value of OFPP_NONE
860                                  indicates no restriction. */
861     ovs_be16 match_len;       /* Length of nx_match. */
862     uint8_t table_id;         /* ID of table to read (from ofp_table_stats)
863                                  or 0xff for all tables. */
864     uint8_t pad[3];           /* Align to 64 bits. */
865     /* Followed by:
866      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
867      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
868      *     all-zero bytes, which must also exactly fill out the length of the
869      *     message.
870      */
871 };
872 OFP_ASSERT(sizeof(struct nx_aggregate_stats_request) == 32);
873
874 /* Body for nicira_stats_msg reply of type NXST_AGGREGATE (analogous to
875  * OFPST_AGGREGATE reply).
876  *
877  * ofp_aggregate_stats_reply does not contain an ofp_match structure, so we
878  * reuse it entirely.  (It would be very odd to use OFPST_AGGREGATE to reply to
879  * an NXST_AGGREGATE request, so we don't do that.) */
880 struct nx_aggregate_stats_reply {
881     struct nicira_stats_msg nsm;
882     struct ofp_aggregate_stats_reply asr;
883 };
884 OFP_ASSERT(sizeof(struct nx_aggregate_stats_reply) == 48);
885
886 #endif /* openflow/nicira-ext.h */