ofp-util: Add OFPUTIL_P_OF12 and NXFF_OPENFLOW12
[openvswitch] / include / openflow / nicira-ext.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 /* The following vendor extensions, proposed by Nicira, are not yet
24  * standardized, so they are not included in openflow.h.  Some of them may be
25  * suitable for standardization; others we never expect to standardize. */
26
27 #define NX_VENDOR_ID 0x00002320
28 \f
29 /* Nicira vendor-specific error messages extension.
30  *
31  * OpenFlow 1.0 has a set of predefined error types (OFPET_*) and codes (which
32  * are specific to each type).  It does not have any provision for
33  * vendor-specific error codes, and it does not even provide "generic" error
34  * codes that can apply to problems not anticipated by the OpenFlow
35  * specification authors.
36  *
37  * This extension attempts to address the problem by adding a generic "error
38  * vendor extension".  The extension works as follows: use NXET_VENDOR as type
39  * and NXVC_VENDOR_ERROR as code, followed by struct nx_vendor_error with
40  * vendor-specific details, followed by at least 64 bytes of the failed
41  * request.
42  *
43  * It would be better to have a type-specific vendor extension, e.g. so that
44  * OFPET_BAD_ACTION could be used with vendor-specific code values.  But
45  * OFPET_BAD_ACTION and most other standardized types already specify that
46  * their 'data' values are (the start of) the OpenFlow message being replied
47  * to, so there is no room to insert a vendor ID.
48  *
49  * Currently this extension is only implemented by Open vSwitch, but it seems
50  * like a reasonable candidate for future standardization.
51  */
52
53 /* This is a random number to avoid accidental collision with any other
54  * vendor's extension. */
55 #define NXET_VENDOR 0xb0c2
56
57 /* ofp_error msg 'code' values for NXET_VENDOR. */
58 enum nx_vendor_code {
59     NXVC_VENDOR_ERROR           /* 'data' contains struct nx_vendor_error. */
60 };
61
62 /* 'data' for 'type' == NXET_VENDOR, 'code' == NXVC_VENDOR_ERROR. */
63 struct nx_vendor_error {
64     ovs_be32 vendor;            /* Vendor ID as in struct ofp_vendor_header. */
65     ovs_be16 type;              /* Vendor-defined type. */
66     ovs_be16 code;              /* Vendor-defined subtype. */
67     /* Followed by at least the first 64 bytes of the failed request. */
68 };
69 \f
70 /* Nicira vendor requests and replies. */
71
72 /* Header for Nicira vendor requests and replies. */
73 struct nicira_header {
74     struct ofp_header header;
75     ovs_be32 vendor;            /* NX_VENDOR_ID. */
76     ovs_be32 subtype;           /* One of NXT_* below. */
77 };
78 OFP_ASSERT(sizeof(struct nicira_header) == 16);
79
80 /* Values for the 'subtype' member of struct nicira_header. */
81 enum nicira_type {
82     /* No longer used. */
83     NXT_STATUS_REQUEST__OBSOLETE = 0,
84     NXT_STATUS_REPLY__OBSOLETE = 1,
85     NXT_ACT_SET_CONFIG__OBSOLETE = 2,
86     NXT_ACT_GET_CONFIG__OBSOLETE = 3,
87     NXT_COMMAND_REQUEST__OBSOLETE = 4,
88     NXT_COMMAND_REPLY__OBSOLETE = 5,
89     NXT_FLOW_END_CONFIG__OBSOLETE = 6,
90     NXT_FLOW_END__OBSOLETE = 7,
91     NXT_MGMT__OBSOLETE = 8,
92     NXT_TUN_ID_FROM_COOKIE__OBSOLETE = 9,
93
94     /* Controller role support.  The request body is struct nx_role_request.
95      * The reply echos the request. */
96     NXT_ROLE_REQUEST = 10,
97     NXT_ROLE_REPLY = 11,
98
99     /* Flexible flow specification (aka NXM = Nicira Extended Match). */
100     NXT_SET_FLOW_FORMAT = 12,   /* Set flow format. */
101     NXT_FLOW_MOD = 13,          /* Analogous to OFPT_FLOW_MOD. */
102     NXT_FLOW_REMOVED = 14,      /* Analogous to OFPT_FLOW_REMOVED. */
103
104     /* Use the upper 8 bits of the 'command' member in struct ofp_flow_mod to
105      * designate the table to which a flow is to be added?  See the big comment
106      * on struct nx_flow_mod_table_id for more information. */
107     NXT_FLOW_MOD_TABLE_ID = 15,
108
109     /* Alternative PACKET_IN message formats. */
110     NXT_SET_PACKET_IN_FORMAT = 16, /* Set Packet In format. */
111     NXT_PACKET_IN = 17,            /* Nicira Packet In. */
112
113     /* Are the idle_age and hard_age members in struct nx_flow_stats supported?
114      * If so, the switch does not reply to this message (which consists only of
115      * a "struct nicira_header").  If not, the switch sends an error reply. */
116     NXT_FLOW_AGE = 18,
117
118     NXT_SET_ASYNC_CONFIG = 19,  /* struct nx_async_config. */
119     NXT_SET_CONTROLLER_ID = 20, /* struct nx_controller_id. */
120
121     /* Flow table monitoring (see also NXST_FLOW_MONITOR). */
122     NXT_FLOW_MONITOR_CANCEL = 21,  /* struct nx_flow_monitor_cancel. */
123     NXT_FLOW_MONITOR_PAUSED = 22,  /* struct nicira_header. */
124     NXT_FLOW_MONITOR_RESUMED = 23, /* struct nicira_header. */
125 };
126
127 /* Header for Nicira vendor stats request and reply messages. */
128 struct nicira_stats_msg {
129     struct ofp_vendor_stats_msg vsm; /* Vendor NX_VENDOR_ID. */
130     ovs_be32 subtype;           /* One of NXST_* below. */
131     uint8_t pad[4];             /* Align to 64-bits. */
132 };
133 OFP_ASSERT(sizeof(struct nicira_stats_msg) == 24);
134
135 /* Values for the 'subtype' member of struct nicira_stats_msg. */
136 enum nicira_stats_type {
137     /* Flexible flow specification (aka NXM = Nicira Extended Match). */
138     NXST_FLOW,                  /* Analogous to OFPST_FLOW. */
139     NXST_AGGREGATE,             /* Analogous to OFPST_AGGREGATE. */
140
141     /* Flow table monitoring. */
142     NXST_FLOW_MONITOR,
143 };
144
145 /* Fields to use when hashing flows. */
146 enum nx_hash_fields {
147     /* Ethernet source address (NXM_OF_ETH_SRC) only. */
148     NX_HASH_FIELDS_ETH_SRC,
149
150     /* L2 through L4, symmetric across src/dst.  Specifically, each of the
151      * following fields, if present, is hashed (slashes separate symmetric
152      * pairs):
153      *
154      *  - NXM_OF_ETH_DST / NXM_OF_ETH_SRC
155      *  - NXM_OF_ETH_TYPE
156      *  - The VID bits from NXM_OF_VLAN_TCI, ignoring PCP and CFI.
157      *  - NXM_OF_IP_PROTO
158      *  - NXM_OF_IP_SRC / NXM_OF_IP_DST
159      *  - NXM_OF_TCP_SRC / NXM_OF_TCP_DST
160      */
161     NX_HASH_FIELDS_SYMMETRIC_L4
162 };
163
164 /* This command enables or disables an Open vSwitch extension that allows a
165  * controller to specify the OpenFlow table to which a flow should be added,
166  * instead of having the switch decide which table is most appropriate as
167  * required by OpenFlow 1.0.  By default, the extension is disabled.
168  *
169  * When this feature is enabled, Open vSwitch treats struct ofp_flow_mod's
170  * 16-bit 'command' member as two separate fields.  The upper 8 bits are used
171  * as the table ID, the lower 8 bits specify the command as usual.  A table ID
172  * of 0xff is treated like a wildcarded table ID.
173  *
174  * The specific treatment of the table ID depends on the type of flow mod:
175  *
176  *    - OFPFC_ADD: Given a specific table ID, the flow is always placed in that
177  *      table.  If an identical flow already exists in that table only, then it
178  *      is replaced.  If the flow cannot be placed in the specified table,
179  *      either because the table is full or because the table cannot support
180  *      flows of the given type, the switch replies with an
181  *      OFPFMFC_ALL_TABLES_FULL error.  (A controller can distinguish these
182  *      cases by comparing the current and maximum number of entries reported
183  *      in ofp_table_stats.)
184  *
185  *      If the table ID is wildcarded, the switch picks an appropriate table
186  *      itself.  If an identical flow already exist in the selected flow table,
187  *      then it is replaced.  The choice of table might depend on the flows
188  *      that are already in the switch; for example, if one table fills up then
189  *      the switch might fall back to another one.
190  *
191  *    - OFPFC_MODIFY, OFPFC_DELETE: Given a specific table ID, only flows
192  *      within that table are matched and modified or deleted.  If the table ID
193  *      is wildcarded, flows within any table may be matched and modified or
194  *      deleted.
195  *
196  *    - OFPFC_MODIFY_STRICT, OFPFC_DELETE_STRICT: Given a specific table ID,
197  *      only a flow within that table may be matched and modified or deleted.
198  *      If the table ID is wildcarded and exactly one flow within any table
199  *      matches, then it is modified or deleted; if flows in more than one
200  *      table match, then none is modified or deleted.
201  */
202 struct nx_flow_mod_table_id {
203     struct ofp_header header;
204     ovs_be32 vendor;            /* NX_VENDOR_ID. */
205     ovs_be32 subtype;           /* NXT_FLOW_MOD_TABLE_ID. */
206     uint8_t set;                /* Nonzero to enable, zero to disable. */
207     uint8_t pad[7];
208 };
209 OFP_ASSERT(sizeof(struct nx_flow_mod_table_id) == 24);
210
211 enum nx_packet_in_format {
212     NXPIF_OPENFLOW10 = 0,       /* Standard OpenFlow 1.0 compatible. */
213     NXPIF_NXM = 1               /* Nicira Extended. */
214 };
215
216 /* NXT_SET_PACKET_IN_FORMAT request. */
217 struct nx_set_packet_in_format {
218     struct nicira_header nxh;
219     ovs_be32 format;            /* One of NXPIF_*. */
220 };
221 OFP_ASSERT(sizeof(struct nx_set_packet_in_format) == 20);
222
223 /* NXT_PACKET_IN (analogous to OFPT_PACKET_IN).
224  *
225  * The NXT_PACKET_IN format is intended to model the OpenFlow-1.2 PACKET_IN
226  * with some minor tweaks.  Most notably NXT_PACKET_IN includes the cookie of
227  * the rule which triggered the NXT_PACKET_IN message, and the match fields are
228  * in NXM format.
229  *
230  * The match fields in the NXT_PACKET_IN are intended to contain flow
231  * processing metadata collected at the time the NXT_PACKET_IN message was
232  * triggered.  It is minimally required to contain the NXM_OF_IN_PORT of the
233  * packet, but may include other NXM headers such as flow registers.  The match
234  * fields are allowed to contain non-metadata (e.g. NXM_OF_ETH_SRC etc).
235  * However, this information can typically be found in the packet directly, so
236  * it may be redundant.
237  *
238  * Whereas in most cases a controller can expect to only get back NXM fields
239  * that it set up itself (e.g. flow dumps will ordinarily report only NXM
240  * fields from flows that the controller added), NXT_PACKET_IN messages might
241  * contain fields that the controller does not understand, because the switch
242  * might support fields (new registers, new protocols, etc.) that the
243  * controller does not. Â The controller must prepared to tolerate these.
244  *
245  * The 'cookie' and 'table_id' fields have no meaning when 'reason' is
246  * OFPR_NO_MATCH.  In this case they should be set to 0. */
247 struct nx_packet_in {
248     struct nicira_header nxh;
249     ovs_be32 buffer_id;       /* ID assigned by datapath. */
250     ovs_be16 total_len;       /* Full length of frame. */
251     uint8_t reason;           /* Reason packet is sent (one of OFPR_*). */
252     uint8_t table_id;         /* ID of the table that was looked up. */
253     ovs_be64 cookie;          /* Cookie of the rule that was looked up. */
254     ovs_be16 match_len;       /* Size of nx_match. */
255     uint8_t pad[6];           /* Align to 64-bits. */
256     /* Followed by:
257      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
258      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
259      *     all-zero bytes, then
260      *   - Exactly 2 all-zero padding bytes, then
261      *   - An Ethernet frame whose length is inferred from nxh.header.length.
262      *
263      * The padding bytes preceding the Ethernet frame ensure that the IP
264      * header (if any) following the Ethernet header is 32-bit aligned. */
265
266     /* uint8_t nxm_fields[...]; */ /* Match. */
267     /* uint8_t pad[2]; */          /* Align to 64 bit + 16 bit. */
268     /* uint8_t data[0]; */         /* Ethernet frame. */
269 };
270 OFP_ASSERT(sizeof(struct nx_packet_in) == 40);
271
272 /* Configures the "role" of the sending controller.  The default role is:
273  *
274  *    - Other (NX_ROLE_OTHER), which allows the controller access to all
275  *      OpenFlow features.
276  *
277  * The other possible roles are a related pair:
278  *
279  *    - Master (NX_ROLE_MASTER) is equivalent to Other, except that there may
280  *      be at most one Master controller at a time: when a controller
281  *      configures itself as Master, any existing Master is demoted to the
282  *      Slave role.
283  *
284  *    - Slave (NX_ROLE_SLAVE) allows the controller read-only access to
285  *      OpenFlow features.  In particular attempts to modify the flow table
286  *      will be rejected with an OFPBRC_EPERM error.
287  *
288  *      Slave controllers do not receive OFPT_PACKET_IN or OFPT_FLOW_REMOVED
289  *      messages, but they do receive OFPT_PORT_STATUS messages.
290  */
291 struct nx_role_request {
292     struct nicira_header nxh;
293     ovs_be32 role;              /* One of NX_ROLE_*. */
294 };
295 OFP_ASSERT(sizeof(struct nx_role_request) == 20);
296
297 enum nx_role {
298     NX_ROLE_OTHER,              /* Default role, full access. */
299     NX_ROLE_MASTER,             /* Full access, at most one. */
300     NX_ROLE_SLAVE               /* Read-only access. */
301 };
302
303 /* NXT_SET_ASYNC_CONFIG.
304  *
305  * Sent by a controller, this message configures the asynchronous messages that
306  * the controller wants to receive.  Element 0 in each array specifies messages
307  * of interest when the controller has an "other" or "master" role; element 1,
308  * when the controller has a "slave" role.
309  *
310  * Each array element is a bitmask in which a 0-bit disables receiving a
311  * particular message and a 1-bit enables receiving it.  Each bit controls the
312  * message whose 'reason' corresponds to the bit index.  For example, the bit
313  * with value 1<<2 == 4 in port_status_mask[1] determines whether the
314  * controller will receive OFPT_PORT_STATUS messages with reason OFPPR_MODIFY
315  * (value 2) when the controller has a "slave" role.
316  *
317  * As a side effect, for service controllers, this message changes the
318  * miss_send_len from default of zero to OFP_DEFAULT_MISS_SEND_LEN (128).
319  */
320 struct nx_async_config {
321     struct nicira_header nxh;
322     ovs_be32 packet_in_mask[2];    /* Bitmasks of OFPR_* values. */
323     ovs_be32 port_status_mask[2];  /* Bitmasks of OFPRR_* values. */
324     ovs_be32 flow_removed_mask[2]; /* Bitmasks of OFPPR_* values. */
325 };
326 OFP_ASSERT(sizeof(struct nx_async_config) == 40);
327 \f
328 /* Nicira vendor flow actions. */
329
330 enum nx_action_subtype {
331     NXAST_SNAT__OBSOLETE,       /* No longer used. */
332     NXAST_RESUBMIT,             /* struct nx_action_resubmit */
333     NXAST_SET_TUNNEL,           /* struct nx_action_set_tunnel */
334     NXAST_DROP_SPOOFED_ARP__OBSOLETE,
335     NXAST_SET_QUEUE,            /* struct nx_action_set_queue */
336     NXAST_POP_QUEUE,            /* struct nx_action_pop_queue */
337     NXAST_REG_MOVE,             /* struct nx_action_reg_move */
338     NXAST_REG_LOAD,             /* struct nx_action_reg_load */
339     NXAST_NOTE,                 /* struct nx_action_note */
340     NXAST_SET_TUNNEL64,         /* struct nx_action_set_tunnel64 */
341     NXAST_MULTIPATH,            /* struct nx_action_multipath */
342     NXAST_AUTOPATH,             /* struct nx_action_autopath */
343     NXAST_BUNDLE,               /* struct nx_action_bundle */
344     NXAST_BUNDLE_LOAD,          /* struct nx_action_bundle */
345     NXAST_RESUBMIT_TABLE,       /* struct nx_action_resubmit */
346     NXAST_OUTPUT_REG,           /* struct nx_action_output_reg */
347     NXAST_LEARN,                /* struct nx_action_learn */
348     NXAST_EXIT,                 /* struct nx_action_header */
349     NXAST_DEC_TTL,              /* struct nx_action_header */
350     NXAST_FIN_TIMEOUT,          /* struct nx_action_fin_timeout */
351     NXAST_CONTROLLER,           /* struct nx_action_controller */
352 };
353
354 /* Header for Nicira-defined actions. */
355 struct nx_action_header {
356     ovs_be16 type;                  /* OFPAT_VENDOR. */
357     ovs_be16 len;                   /* Length is 16. */
358     ovs_be32 vendor;                /* NX_VENDOR_ID. */
359     ovs_be16 subtype;               /* NXAST_*. */
360     uint8_t pad[6];
361 };
362 OFP_ASSERT(sizeof(struct nx_action_header) == 16);
363
364 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
365  *
366  * These actions search one of the switch's flow tables:
367  *
368  *    - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
369  *      it specifies the table to search.
370  *
371  *    - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
372  *      NXAST_RESUBMIT regardless of 'table'), it searches the current flow
373  *      table, that is, the OpenFlow flow table that contains the flow from
374  *      which this action was obtained.  If this action did not come from a
375  *      flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
376  *      is the current table.
377  *
378  * The flow table lookup uses a flow that may be slightly modified from the
379  * original lookup:
380  *
381  *    - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
382  *      is used as the flow's in_port.
383  *
384  *    - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
385  *      then its value is used as the flow's in_port.  Otherwise, the original
386  *      in_port is used.
387  *
388  *    - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
389  *      resubmit action, then the flow is updated with the new values.
390  *
391  * Following the lookup, the original in_port is restored.
392  *
393  * If the modified flow matched in the flow table, then the corresponding
394  * actions are executed.  Afterward, actions following the resubmit in the
395  * original set of actions, if any, are executed; any changes made to the
396  * packet (e.g. changes to VLAN) by secondary actions persist when those
397  * actions are executed, although the original in_port is restored.
398  *
399  * Resubmit actions may be used any number of times within a set of actions.
400  *
401  * Resubmit actions may nest to an implementation-defined depth.  Beyond this
402  * implementation-defined depth, further resubmit actions are simply ignored.
403  *
404  * NXAST_RESUBMIT ignores 'table' and 'pad'.  NXAST_RESUBMIT_TABLE requires
405  * 'pad' to be all-bits-zero.
406  *
407  * Open vSwitch 1.0.1 and earlier did not support recursion.  Open vSwitch
408  * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
409  */
410 struct nx_action_resubmit {
411     ovs_be16 type;                  /* OFPAT_VENDOR. */
412     ovs_be16 len;                   /* Length is 16. */
413     ovs_be32 vendor;                /* NX_VENDOR_ID. */
414     ovs_be16 subtype;               /* NXAST_RESUBMIT. */
415     ovs_be16 in_port;               /* New in_port for checking flow table. */
416     uint8_t table;                  /* NXAST_RESUBMIT_TABLE: table to use. */
417     uint8_t pad[3];
418 };
419 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
420
421 /* Action structure for NXAST_SET_TUNNEL.
422  *
423  * Sets the encapsulating tunnel ID to a 32-bit value.  The most-significant 32
424  * bits of the tunnel ID are set to 0. */
425 struct nx_action_set_tunnel {
426     ovs_be16 type;                  /* OFPAT_VENDOR. */
427     ovs_be16 len;                   /* Length is 16. */
428     ovs_be32 vendor;                /* NX_VENDOR_ID. */
429     ovs_be16 subtype;               /* NXAST_SET_TUNNEL. */
430     uint8_t pad[2];
431     ovs_be32 tun_id;                /* Tunnel ID. */
432 };
433 OFP_ASSERT(sizeof(struct nx_action_set_tunnel) == 16);
434
435 /* Action structure for NXAST_SET_TUNNEL64.
436  *
437  * Sets the encapsulating tunnel ID to a 64-bit value. */
438 struct nx_action_set_tunnel64 {
439     ovs_be16 type;                  /* OFPAT_VENDOR. */
440     ovs_be16 len;                   /* Length is 24. */
441     ovs_be32 vendor;                /* NX_VENDOR_ID. */
442     ovs_be16 subtype;               /* NXAST_SET_TUNNEL64. */
443     uint8_t pad[6];
444     ovs_be64 tun_id;                /* Tunnel ID. */
445 };
446 OFP_ASSERT(sizeof(struct nx_action_set_tunnel64) == 24);
447
448 /* Action structure for NXAST_SET_QUEUE.
449  *
450  * Set the queue that should be used when packets are output.  This is similar
451  * to the OpenFlow OFPAT_ENQUEUE action, but does not take the output port as
452  * an argument.  This allows the queue to be defined before the port is
453  * known. */
454 struct nx_action_set_queue {
455     ovs_be16 type;                  /* OFPAT_VENDOR. */
456     ovs_be16 len;                   /* Length is 16. */
457     ovs_be32 vendor;                /* NX_VENDOR_ID. */
458     ovs_be16 subtype;               /* NXAST_SET_QUEUE. */
459     uint8_t pad[2];
460     ovs_be32 queue_id;              /* Where to enqueue packets. */
461 };
462 OFP_ASSERT(sizeof(struct nx_action_set_queue) == 16);
463
464 /* Action structure for NXAST_POP_QUEUE.
465  *
466  * Restores the queue to the value it was before any NXAST_SET_QUEUE actions
467  * were used.  Only the original queue can be restored this way; no stack is
468  * maintained. */
469 struct nx_action_pop_queue {
470     ovs_be16 type;                  /* OFPAT_VENDOR. */
471     ovs_be16 len;                   /* Length is 16. */
472     ovs_be32 vendor;                /* NX_VENDOR_ID. */
473     ovs_be16 subtype;               /* NXAST_POP_QUEUE. */
474     uint8_t pad[6];
475 };
476 OFP_ASSERT(sizeof(struct nx_action_pop_queue) == 16);
477
478 /* Action structure for NXAST_REG_MOVE.
479  *
480  * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
481  * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
482  * bit 'c').  Bit numbering starts at 0 for the least-significant bit, 1 for
483  * the next most significant bit, and so on.
484  *
485  * 'src' and 'dst' are nxm_header values with nxm_hasmask=0.  (It doesn't make
486  * sense to use nxm_hasmask=1 because the action does not do any kind of
487  * matching; it uses the actual value of a field.)
488  *
489  * The following nxm_header values are potentially acceptable as 'src':
490  *
491  *   - NXM_OF_IN_PORT
492  *   - NXM_OF_ETH_DST
493  *   - NXM_OF_ETH_SRC
494  *   - NXM_OF_ETH_TYPE
495  *   - NXM_OF_VLAN_TCI
496  *   - NXM_OF_IP_TOS
497  *   - NXM_OF_IP_PROTO
498  *   - NXM_OF_IP_SRC
499  *   - NXM_OF_IP_DST
500  *   - NXM_OF_TCP_SRC
501  *   - NXM_OF_TCP_DST
502  *   - NXM_OF_UDP_SRC
503  *   - NXM_OF_UDP_DST
504  *   - NXM_OF_ICMP_TYPE
505  *   - NXM_OF_ICMP_CODE
506  *   - NXM_OF_ARP_OP
507  *   - NXM_OF_ARP_SPA
508  *   - NXM_OF_ARP_TPA
509  *   - NXM_NX_TUN_ID
510  *   - NXM_NX_ARP_SHA
511  *   - NXM_NX_ARP_THA
512  *   - NXM_NX_ICMPV6_TYPE
513  *   - NXM_NX_ICMPV6_CODE
514  *   - NXM_NX_ND_SLL
515  *   - NXM_NX_ND_TLL
516  *   - NXM_NX_REG(idx) for idx in the switch's accepted range.
517  *
518  * The following nxm_header values are potentially acceptable as 'dst':
519  *
520  *   - NXM_OF_ETH_DST
521  *   - NXM_OF_ETH_SRC
522  *   - NXM_OF_IP_TOS
523  *   - NXM_OF_IP_SRC
524  *   - NXM_OF_IP_DST
525  *   - NXM_OF_TCP_SRC
526  *   - NXM_OF_TCP_DST
527  *   - NXM_OF_UDP_SRC
528  *   - NXM_OF_UDP_DST
529  *     Modifying any of the above fields changes the corresponding packet
530  *     header.
531  *
532  *   - NXM_NX_REG(idx) for idx in the switch's accepted range.
533  *
534  *   - NXM_OF_VLAN_TCI.  Modifying this field's value has side effects on the
535  *     packet's 802.1Q header.  Setting a value with CFI=0 removes the 802.1Q
536  *     header (if any), ignoring the other bits.  Setting a value with CFI=1
537  *     adds or modifies the 802.1Q header appropriately, setting the TCI field
538  *     to the field's new value (with the CFI bit masked out).
539  *
540  *   - NXM_NX_TUN_ID.  Modifying this value modifies the tunnel ID used for the
541  *     packet's next tunnel encapsulation.
542  *
543  * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
544  * nx_match satisfies its prerequisites.  For example, NXM_OF_IP_TOS may be
545  * used only if the flow's nx_match includes an nxm_entry that specifies
546  * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
547  *
548  * The switch will reject actions for which src_ofs+n_bits is greater than the
549  * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
550  * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
551  */
552 struct nx_action_reg_move {
553     ovs_be16 type;                  /* OFPAT_VENDOR. */
554     ovs_be16 len;                   /* Length is 24. */
555     ovs_be32 vendor;                /* NX_VENDOR_ID. */
556     ovs_be16 subtype;               /* NXAST_REG_MOVE. */
557     ovs_be16 n_bits;                /* Number of bits. */
558     ovs_be16 src_ofs;               /* Starting bit offset in source. */
559     ovs_be16 dst_ofs;               /* Starting bit offset in destination. */
560     ovs_be32 src;                   /* Source register. */
561     ovs_be32 dst;                   /* Destination register. */
562 };
563 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 24);
564
565 /* Action structure for NXAST_REG_LOAD.
566  *
567  * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
568  * within 'a' numbered 'b' through 'c' (not including bit 'c').  Bit numbering
569  * starts at 0 for the least-significant bit, 1 for the next most significant
570  * bit, and so on.
571  *
572  * 'dst' is an nxm_header with nxm_hasmask=0.  See the documentation for
573  * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
574  * loading them.
575  *
576  * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
577  * to avoid enlarging the structure by another 8 bytes.  To allow 'n_bits' to
578  * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
579  * also stored as one less than its true value:
580  *
581  *  15                           6 5                0
582  * +------------------------------+------------------+
583  * |              ofs             |    n_bits - 1    |
584  * +------------------------------+------------------+
585  *
586  * The switch will reject actions for which ofs+n_bits is greater than the
587  * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
588  * greater are set to 1, with error type OFPET_BAD_ACTION, code
589  * OFPBAC_BAD_ARGUMENT.
590  */
591 struct nx_action_reg_load {
592     ovs_be16 type;                  /* OFPAT_VENDOR. */
593     ovs_be16 len;                   /* Length is 24. */
594     ovs_be32 vendor;                /* NX_VENDOR_ID. */
595     ovs_be16 subtype;               /* NXAST_REG_LOAD. */
596     ovs_be16 ofs_nbits;             /* (ofs << 6) | (n_bits - 1). */
597     ovs_be32 dst;                   /* Destination register. */
598     ovs_be64 value;                 /* Immediate value. */
599 };
600 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
601
602 /* Action structure for NXAST_NOTE.
603  *
604  * This action has no effect.  It is variable length.  The switch does not
605  * attempt to interpret the user-defined 'note' data in any way.  A controller
606  * can use this action to attach arbitrary metadata to a flow.
607  *
608  * This action might go away in the future.
609  */
610 struct nx_action_note {
611     ovs_be16 type;                  /* OFPAT_VENDOR. */
612     ovs_be16 len;                   /* A multiple of 8, but at least 16. */
613     ovs_be32 vendor;                /* NX_VENDOR_ID. */
614     ovs_be16 subtype;               /* NXAST_NOTE. */
615     uint8_t note[6];                /* Start of user-defined data. */
616     /* Possibly followed by additional user-defined data. */
617 };
618 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
619
620 /* Action structure for NXAST_MULTIPATH.
621  *
622  * This action performs the following steps in sequence:
623  *
624  *    1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
625  *       Refer to the definition of "enum nx_mp_fields" for details.
626  *
627  *       The 'basis' value is used as a universal hash parameter, that is,
628  *       different values of 'basis' yield different hash functions.  The
629  *       particular universal hash function used is implementation-defined.
630  *
631  *       The hashed fields' values are drawn from the current state of the
632  *       flow, including all modifications that have been made by actions up to
633  *       this point.
634  *
635  *    2. Applies the multipath link choice algorithm specified by 'algorithm',
636  *       one of NX_MP_ALG_*.  Refer to the definition of "enum nx_mp_algorithm"
637  *       for details.
638  *
639  *       The output of the algorithm is 'link', an unsigned integer less than
640  *       or equal to 'max_link'.
641  *
642  *       Some algorithms use 'arg' as an additional argument.
643  *
644  *    3. Stores 'link' in dst[ofs:ofs+n_bits].  The format and semantics of
645  *       'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
646  *       action.
647  *
648  * The switch will reject actions that have an unknown 'fields', or an unknown
649  * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
650  * in which 'max_link' is greater than or equal to 2**n_bits, with error type
651  * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
652  */
653 struct nx_action_multipath {
654     ovs_be16 type;              /* OFPAT_VENDOR. */
655     ovs_be16 len;               /* Length is 32. */
656     ovs_be32 vendor;            /* NX_VENDOR_ID. */
657     ovs_be16 subtype;           /* NXAST_MULTIPATH. */
658
659     /* What fields to hash and how. */
660     ovs_be16 fields;            /* One of NX_HASH_FIELDS_*. */
661     ovs_be16 basis;             /* Universal hash parameter. */
662     ovs_be16 pad0;
663
664     /* Multipath link choice algorithm to apply to hash value. */
665     ovs_be16 algorithm;         /* One of NX_MP_ALG_*. */
666     ovs_be16 max_link;          /* Number of output links, minus 1. */
667     ovs_be32 arg;               /* Algorithm-specific argument. */
668     ovs_be16 pad1;
669
670     /* Where to store the result. */
671     ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
672     ovs_be32 dst;               /* Destination. */
673 };
674 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
675
676 /* NXAST_MULTIPATH: Multipath link choice algorithm to apply.
677  *
678  * In the descriptions below, 'n_links' is max_link + 1. */
679 enum nx_mp_algorithm {
680     /* link = hash(flow) % n_links.
681      *
682      * Redistributes all traffic when n_links changes.  O(1) performance.  See
683      * RFC 2992.
684      *
685      * Use UINT16_MAX for max_link to get a raw hash value. */
686     NX_MP_ALG_MODULO_N,
687
688     /* link = hash(flow) / (MAX_HASH / n_links).
689      *
690      * Redistributes between one-quarter and one-half of traffic when n_links
691      * changes.  O(1) performance.  See RFC 2992.
692      */
693     NX_MP_ALG_HASH_THRESHOLD,
694
695     /* for i in [0,n_links):
696      *   weights[i] = hash(flow, i)
697      * link = { i such that weights[i] >= weights[j] for all j != i }
698      *
699      * Redistributes 1/n_links of traffic when n_links changes.  O(n_links)
700      * performance.  If n_links is greater than a threshold (currently 64, but
701      * subject to change), Open vSwitch will substitute another algorithm
702      * automatically.  See RFC 2992. */
703     NX_MP_ALG_HRW,              /* Highest Random Weight. */
704
705     /* i = 0
706      * repeat:
707      *     i = i + 1
708      *     link = hash(flow, i) % arg
709      * while link > max_link
710      *
711      * Redistributes 1/n_links of traffic when n_links changes.  O(1)
712      * performance when arg/max_link is bounded by a constant.
713      *
714      * Redistributes all traffic when arg changes.
715      *
716      * arg must be greater than max_link and for best performance should be no
717      * more than approximately max_link * 2.  If arg is outside the acceptable
718      * range, Open vSwitch will automatically substitute the least power of 2
719      * greater than max_link.
720      *
721      * This algorithm is specific to Open vSwitch.
722      */
723     NX_MP_ALG_ITER_HASH         /* Iterative Hash. */
724 };
725 \f
726 /* Action structure for NXAST_LEARN.
727  *
728  * This action adds or modifies a flow in an OpenFlow table, similar to
729  * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'.  The new flow has the
730  * specified idle timeout, hard timeout, priority, cookie, and flags.  The new
731  * flow's match criteria and actions are built by applying each of the series
732  * of flow_mod_spec elements included as part of the action.
733  *
734  * A flow_mod_spec starts with a 16-bit header.  A header that is all-bits-0 is
735  * a no-op used for padding the action as a whole to a multiple of 8 bytes in
736  * length.  Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
737  * bits from a source to a destination.  In this case, the header contains
738  * multiple fields:
739  *
740  *  15  14  13 12  11 10                              0
741  * +------+---+------+---------------------------------+
742  * |   0  |src|  dst |             n_bits              |
743  * +------+---+------+---------------------------------+
744  *
745  * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'.  The
746  * following table summarizes the meaning of each possible combination.
747  * Details follow the table:
748  *
749  *   src dst  meaning
750  *   --- ---  ----------------------------------------------------------
751  *    0   0   Add match criteria based on value in a field.
752  *    1   0   Add match criteria based on an immediate value.
753  *    0   1   Add NXAST_REG_LOAD action to copy field into a different field.
754  *    1   1   Add NXAST_REG_LOAD action to load immediate value into a field.
755  *    0   2   Add OFPAT_OUTPUT action to output to port from specified field.
756  *   All other combinations are undefined and not allowed.
757  *
758  * The flow_mod_spec header is followed by a source specification and a
759  * destination specification.  The format and meaning of the source
760  * specification depends on 'src':
761  *
762  *   - If 'src' is 0, the source bits are taken from a field in the flow to
763  *     which this action is attached.  (This should be a wildcarded field.  If
764  *     its value is fully specified then the source bits being copied have
765  *     constant values.)
766  *
767  *     The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
768  *     'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
769  *     offset within that field.  The source bits are field[ofs:ofs+n_bits-1].
770  *     'field' and 'ofs' are subject to the same restrictions as the source
771  *     field in NXAST_REG_MOVE.
772  *
773  *   - If 'src' is 1, the source bits are a constant value.  The source
774  *     specification is (n_bits+15)/16*2 bytes long.  Taking those bytes as a
775  *     number in network order, the source bits are the 'n_bits'
776  *     least-significant bits.  The switch will report an error if other bits
777  *     in the constant are nonzero.
778  *
779  * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
780  * ovs_be32 'field' and an ovs_be16 'ofs'.  'field' is an nxm_header with
781  * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field.  The
782  * meaning of the flow_mod_spec depends on 'dst':
783  *
784  *   - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
785  *     flow.  The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
786  *     packet equal the source bits.  'field' may be any nxm_header with
787  *     nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
788  *
789  *     Order is significant.  Earlier flow_mod_specs must satisfy any
790  *     prerequisites for matching fields specified later, by copying constant
791  *     values into prerequisite fields.
792  *
793  *     The switch will reject flow_mod_specs that do not satisfy NXM masking
794  *     restrictions.
795  *
796  *   - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
797  *     the new flow.  The new flow copies the source bits into
798  *     field[ofs:ofs+n_bits-1].  Actions are executed in the same order as the
799  *     flow_mod_specs.
800  *
801  *     A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
802  *     greater than 64 yields multiple NXAST_REG_LOAD actions.
803  *
804  * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
805  * empty.  It has the following meaning:
806  *
807  *   - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
808  *     The new flow outputs to the OpenFlow port specified by the source field.
809  *     Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
810  *     OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported.  Other special ports
811  *     may not be used.
812  *
813  * Resource Management
814  * -------------------
815  *
816  * A switch has a finite amount of flow table space available for learning.
817  * When this space is exhausted, no new learning table entries will be learned
818  * until some existing flow table entries expire.  The controller should be
819  * prepared to handle this by flooding (which can be implemented as a
820  * low-priority flow).
821  *
822  * If a learned flow matches a single TCP stream with a relatively long
823  * timeout, one may make the best of resource constraints by setting
824  * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
825  * both, to shorter timeouts.  When either of these is specified as a nonzero
826  * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
827  * the learned flow.
828  *
829  * Examples
830  * --------
831  *
832  * The following examples give a prose description of the flow_mod_specs along
833  * with informal notation for how those would be represented and a hex dump of
834  * the bytes that would be required.
835  *
836  * These examples could work with various nx_action_learn parameters.  Typical
837  * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
838  * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
839  *
840  * 1. Learn input port based on the source MAC, with lookup into
841  *    NXM_NX_REG1[16:31] by resubmit to in_port=99:
842  *
843  *    Match on in_port=99:
844  *       ovs_be16(src=1, dst=0, n_bits=16),               20 10
845  *       ovs_be16(99),                                    00 63
846  *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
847  *
848  *    Match Ethernet destination on Ethernet source from packet:
849  *       ovs_be16(src=0, dst=0, n_bits=48),               00 30
850  *       ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0)            00 00 04 06 00 00
851  *       ovs_be32(NXM_OF_ETH_DST), ovs_be16(0)            00 00 02 06 00 00
852  *
853  *    Set NXM_NX_REG1[16:31] to the packet's input port:
854  *       ovs_be16(src=0, dst=1, n_bits=16),               08 10
855  *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
856  *       ovs_be32(NXM_NX_REG1), ovs_be16(16)              00 01 02 04 00 10
857  *
858  *    Given a packet that arrived on port A with Ethernet source address B,
859  *    this would set up the flow "in_port=99, dl_dst=B,
860  *    actions=load:A->NXM_NX_REG1[16..31]".
861  *
862  *    In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
863  *    NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
864  *    load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
865  *
866  * 2. Output to input port based on the source MAC and VLAN VID, with lookup
867  *    into NXM_NX_REG1[16:31]:
868  *
869  *    Match on same VLAN ID as packet:
870  *       ovs_be16(src=0, dst=0, n_bits=12),               00 0c
871  *       ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0)           00 00 08 02 00 00
872  *       ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0)           00 00 08 02 00 00
873  *
874  *    Match Ethernet destination on Ethernet source from packet:
875  *       ovs_be16(src=0, dst=0, n_bits=48),               00 30
876  *       ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0)            00 00 04 06 00 00
877  *       ovs_be32(NXM_OF_ETH_DST), ovs_be16(0)            00 00 02 06 00 00
878  *
879  *    Output to the packet's input port:
880  *       ovs_be16(src=0, dst=2, n_bits=16),               10 10
881  *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
882  *
883  *    Given a packet that arrived on port A with Ethernet source address B in
884  *    VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
885  *    actions=output:A".
886  *
887  *    In syntax accepted by ovs-ofctl, this action is:
888  *    learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
889  *    output:NXM_OF_IN_PORT[])
890  *
891  * 3. Here's a recipe for a very simple-minded MAC learning switch.  It uses a
892  *    10-second MAC expiration time to make it easier to see what's going on
893  *
894  *      ovs-vsctl del-controller br0
895  *      ovs-ofctl del-flows br0
896  *      ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
897           hard_timeout=10, NXM_OF_VLAN_TCI[0..11],             \
898           NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],                   \
899           output:NXM_OF_IN_PORT[]), resubmit(,1)"
900  *      ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
901  *
902  *    You can then dump the MAC learning table with:
903  *
904  *      ovs-ofctl dump-flows br0 table=1
905  *
906  * Usage Advice
907  * ------------
908  *
909  * For best performance, segregate learned flows into a table that is not used
910  * for any other flows except possibly for a lowest-priority "catch-all" flow
911  * (a flow with no match criteria).  If different learning actions specify
912  * different match criteria, use different tables for the learned flows.
913  *
914  * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
915  * These timeouts apply to the flow that is added, which means that a flow with
916  * an idle timeout will expire when no traffic has been sent *to* the learned
917  * address.  This is not usually the intent in MAC learning; instead, we want
918  * the MAC learn entry to expire when no traffic has been sent *from* the
919  * learned address.  Use a hard timeout for that.
920  */
921 struct nx_action_learn {
922     ovs_be16 type;              /* OFPAT_VENDOR. */
923     ovs_be16 len;               /* At least 24. */
924     ovs_be32 vendor;            /* NX_VENDOR_ID. */
925     ovs_be16 subtype;           /* NXAST_LEARN. */
926     ovs_be16 idle_timeout;      /* Idle time before discarding (seconds). */
927     ovs_be16 hard_timeout;      /* Max time before discarding (seconds). */
928     ovs_be16 priority;          /* Priority level of flow entry. */
929     ovs_be64 cookie;            /* Cookie for new flow. */
930     ovs_be16 flags;             /* Either 0 or OFPFF_SEND_FLOW_REM. */
931     uint8_t table_id;           /* Table to insert flow entry. */
932     uint8_t pad;                /* Must be zero. */
933     ovs_be16 fin_idle_timeout;  /* Idle timeout after FIN, if nonzero. */
934     ovs_be16 fin_hard_timeout;  /* Hard timeout after FIN, if nonzero. */
935     /* Followed by a sequence of flow_mod_spec elements, as described above,
936      * until the end of the action is reached. */
937 };
938 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
939
940 #define NX_LEARN_N_BITS_MASK    0x3ff
941
942 #define NX_LEARN_SRC_FIELD     (0 << 13) /* Copy from field. */
943 #define NX_LEARN_SRC_IMMEDIATE (1 << 13) /* Copy from immediate value. */
944 #define NX_LEARN_SRC_MASK      (1 << 13)
945
946 #define NX_LEARN_DST_MATCH     (0 << 11) /* Add match criterion. */
947 #define NX_LEARN_DST_LOAD      (1 << 11) /* Add NXAST_REG_LOAD action. */
948 #define NX_LEARN_DST_OUTPUT    (2 << 11) /* Add OFPAT_OUTPUT action. */
949 #define NX_LEARN_DST_RESERVED  (3 << 11) /* Not yet defined. */
950 #define NX_LEARN_DST_MASK      (3 << 11)
951
952 /* Action structure for NXAST_FIN_TIMEOUT.
953  *
954  * This action changes the idle timeout or hard timeout, or both, of this
955  * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
956  * When such a packet is observed, the action reduces the rule's idle timeout
957  * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'.  This
958  * action has no effect on an existing timeout that is already shorter than the
959  * one that the action specifies.  A 'fin_idle_timeout' or 'fin_hard_timeout'
960  * of zero has no effect on the respective timeout.
961  *
962  * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
963  * 'fin_hard_timeout' specifies time since the flow's creation, not since the
964  * receipt of the FIN or RST.
965  *
966  * This is useful for quickly discarding learned TCP flows that otherwise will
967  * take a long time to expire.
968  *
969  * This action is intended for use with an OpenFlow rule that matches only a
970  * single TCP flow.  If the rule matches multiple TCP flows (e.g. it wildcards
971  * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
972  * RST in any of those flows will cause the entire OpenFlow rule to expire
973  * early, which is not normally desirable.
974  */
975 struct nx_action_fin_timeout {
976     ovs_be16 type;              /* OFPAT_VENDOR. */
977     ovs_be16 len;               /* 16. */
978     ovs_be32 vendor;            /* NX_VENDOR_ID. */
979     ovs_be16 subtype;           /* NXAST_FIN_TIMEOUT. */
980     ovs_be16 fin_idle_timeout;  /* New idle timeout, if nonzero. */
981     ovs_be16 fin_hard_timeout;  /* New hard timeout, if nonzero. */
982     ovs_be16 pad;               /* Must be zero. */
983 };
984 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
985 \f
986 /* Action structure for NXAST_AUTOPATH.
987  *
988  * This action performs the following steps in sequence:
989  *
990  *    1. Hashes the flow using an implementation-defined hash function.
991  *
992  *       The hashed fields' values are drawn from the current state of the
993  *       flow, including all modifications that have been made by actions up to
994  *       this point.
995  *
996  *    2. Selects an OpenFlow 'port'.
997  *
998  *       'port' is selected in an implementation-defined manner, taking into
999  *       account 'id' and the hash value calculated in step 1.
1000  *
1001  *       Generally a switch will have been configured with a set of ports that
1002  *       may be chosen given 'id'.  The switch may take into account any number
1003  *       of factors when choosing 'port' from its configured set.  Factors may
1004  *       include carrier, load, and the results of configuration protocols such
1005  *       as LACP.
1006  *
1007  *    3. Stores 'port' in dst[ofs:ofs+n_bits].
1008  *
1009  *       The format and semantics of 'dst' and 'ofs_nbits' are similar to those
1010  *       for the NXAST_REG_LOAD action.
1011  *
1012  * The switch will reject actions in which ofs+n_bits is greater than the width
1013  * of 'dst', with error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1014  */
1015 struct nx_action_autopath {
1016     ovs_be16 type;              /* OFPAT_VENDOR. */
1017     ovs_be16 len;               /* Length is 24. */
1018     ovs_be32 vendor;            /* NX_VENDOR_ID. */
1019     ovs_be16 subtype;           /* NXAST_AUTOPATH. */
1020
1021     /* Where to store the result. */
1022     ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
1023     ovs_be32 dst;               /* Destination. */
1024
1025     ovs_be32 id;                /* Autopath ID. */
1026     ovs_be32 pad;
1027 };
1028 OFP_ASSERT(sizeof(struct nx_action_autopath) == 24);
1029 \f
1030 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1031  *
1032  * The bundle actions choose a slave from a supplied list of options.
1033  * NXAST_BUNDLE outputs to its selection.  NXAST_BUNDLE_LOAD writes its
1034  * selection to a register.
1035  *
1036  * The list of possible slaves follows the nx_action_bundle structure. The size
1037  * of each slave is governed by its type as indicated by the 'slave_type'
1038  * parameter. The list of slaves should be padded at its end with zeros to make
1039  * the total length of the action a multiple of 8.
1040  *
1041  * Switches infer from the 'slave_type' parameter the size of each slave.  All
1042  * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
1043  * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
1044  * 2 byte width.  Switches should reject actions which indicate unknown or
1045  * unsupported slave types.
1046  *
1047  * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1048  * slave.  If the switch does not support the specified 'algorithm' parameter,
1049  * it should reject the action.
1050  *
1051  * Several algorithms take into account liveness when selecting slaves.  The
1052  * liveness of a slave is implementation defined (with one exception), but will
1053  * generally take into account things like its carrier status and the results
1054  * of any link monitoring protocols which happen to be running on it.  In order
1055  * to give controllers a place-holder value, the OFPP_NONE port is always
1056  * considered live.
1057  *
1058  * Some slave selection strategies require the use of a hash function, in which
1059  * case the 'fields' and 'basis' parameters should be populated.  The 'fields'
1060  * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
1061  * hash.  Refer to the definition of "enum nx_hash_fields" for details.  The
1062  * 'basis' parameter is used as a universal hash parameter.  Different values
1063  * of 'basis' yield different hash results.
1064  *
1065  * The 'zero' parameter at the end of the action structure is reserved for
1066  * future use.  Switches are required to reject actions which have nonzero
1067  * bytes in the 'zero' field.
1068  *
1069  * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed.  Switches
1070  * should reject actions which have nonzero bytes in either of these fields.
1071  *
1072  * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1073  * dst[ofs:ofs+n_bits].  The format and semantics of 'dst' and 'ofs_nbits' are
1074  * similar to those for the NXAST_REG_LOAD action. */
1075 struct nx_action_bundle {
1076     ovs_be16 type;              /* OFPAT_VENDOR. */
1077     ovs_be16 len;               /* Length including slaves. */
1078     ovs_be32 vendor;            /* NX_VENDOR_ID. */
1079     ovs_be16 subtype;           /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1080
1081     /* Slave choice algorithm to apply to hash value. */
1082     ovs_be16 algorithm;         /* One of NX_BD_ALG_*. */
1083
1084     /* What fields to hash and how. */
1085     ovs_be16 fields;            /* One of NX_HASH_FIELDS_*. */
1086     ovs_be16 basis;             /* Universal hash parameter. */
1087
1088     ovs_be32 slave_type;        /* NXM_OF_IN_PORT. */
1089     ovs_be16 n_slaves;          /* Number of slaves. */
1090
1091     ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
1092     ovs_be32 dst;               /* Destination. */
1093
1094     uint8_t zero[4];            /* Reserved. Must be zero. */
1095 };
1096 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
1097
1098 /* NXAST_BUNDLE: Bundle slave choice algorithm to apply.
1099  *
1100  * In the descriptions below, 'slaves' is the list of possible slaves in the
1101  * order they appear in the OpenFlow action. */
1102 enum nx_bd_algorithm {
1103     /* Chooses the first live slave listed in the bundle.
1104      *
1105      * O(n_slaves) performance. */
1106     NX_BD_ALG_ACTIVE_BACKUP,
1107
1108     /* for i in [0,n_slaves):
1109      *   weights[i] = hash(flow, i)
1110      * slave = { slaves[i] such that weights[i] >= weights[j] for all j != i }
1111      *
1112      * Redistributes 1/n_slaves of traffic when a slave's liveness changes.
1113      * O(n_slaves) performance.
1114      *
1115      * Uses the 'fields' and 'basis' parameters. */
1116     NX_BD_ALG_HRW /* Highest Random Weight. */
1117 };
1118 \f
1119 /* Action structure for NXAST_OUTPUT_REG.
1120  *
1121  * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
1122  *
1123  * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
1124  * the NXAST_REG_LOAD action.
1125  *
1126  * The acceptable nxm_header values for 'src' are the same as the acceptable
1127  * nxm_header values for the 'src' field of NXAST_REG_MOVE.
1128  *
1129  * The 'max_len' field indicates the number of bytes to send when the chosen
1130  * port is OFPP_CONTROLLER.  Its semantics are equivalent to the 'max_len'
1131  * field of OFPAT_OUTPUT.
1132  *
1133  * The 'zero' field is required to be zeroed for forward compatibility. */
1134 struct nx_action_output_reg {
1135     ovs_be16 type;              /* OFPAT_VENDOR. */
1136     ovs_be16 len;               /* 24. */
1137     ovs_be32 vendor;            /* NX_VENDOR_ID. */
1138     ovs_be16 subtype;           /* NXAST_OUTPUT_REG. */
1139
1140     ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
1141     ovs_be32 src;               /* Source. */
1142
1143     ovs_be16 max_len;           /* Max length to send to controller. */
1144
1145     uint8_t zero[6];            /* Reserved, must be zero. */
1146 };
1147 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
1148 \f
1149 /* NXAST_EXIT
1150  *
1151  * Discontinues action processing.
1152  *
1153  * The NXAST_EXIT action causes the switch to immediately halt processing
1154  * actions for the flow.  Any actions which have already been processed are
1155  * executed by the switch.  However, any further actions, including those which
1156  * may be in different tables, or different levels of the NXAST_RESUBMIT
1157  * hierarchy, will be ignored.
1158  *
1159  * Uses the nx_action_header structure. */
1160 \f
1161 /* Flexible flow specifications (aka NXM = Nicira Extended Match).
1162  *
1163  * OpenFlow 1.0 has "struct ofp10_match" for specifying flow matches.  This
1164  * structure is fixed-length and hence difficult to extend.  This section
1165  * describes a more flexible, variable-length flow match, called "nx_match" for
1166  * short, that is also supported by Open vSwitch.  This section also defines a
1167  * replacement for each OpenFlow message that includes struct ofp10_match.
1168  *
1169  *
1170  * Format
1171  * ======
1172  *
1173  * An nx_match is a sequence of zero or more "nxm_entry"s, which are
1174  * type-length-value (TLV) entries, each 5 to 259 (inclusive) bytes long.
1175  * "nxm_entry"s are not aligned on or padded to any multibyte boundary.  The
1176  * first 4 bytes of an nxm_entry are its "header", followed by the entry's
1177  * "body".
1178  *
1179  * An nxm_entry's header is interpreted as a 32-bit word in network byte order:
1180  *
1181  * |<-------------------- nxm_type ------------------>|
1182  * |                                                  |
1183  * |31                              16 15            9| 8 7                0
1184  * +----------------------------------+---------------+--+------------------+
1185  * |            nxm_vendor            |   nxm_field   |hm|    nxm_length    |
1186  * +----------------------------------+---------------+--+------------------+
1187  *
1188  * The most-significant 23 bits of the header are collectively "nxm_type".
1189  * Bits 16...31 are "nxm_vendor", one of the NXM_VENDOR_* values below.  Bits
1190  * 9...15 are "nxm_field", which is a vendor-specific value.  nxm_type normally
1191  * designates a protocol header, such as the Ethernet type, but it can also
1192  * refer to packet metadata, such as the switch port on which a packet arrived.
1193  *
1194  * Bit 8 is "nxm_hasmask" (labeled "hm" above for space reasons).  The meaning
1195  * of this bit is explained later.
1196  *
1197  * The least-significant 8 bits are "nxm_length", a positive integer.  The
1198  * length of the nxm_entry, including the header, is exactly 4 + nxm_length
1199  * bytes.
1200  *
1201  * For a given nxm_vendor, nxm_field, and nxm_hasmask value, nxm_length is a
1202  * constant.  It is included only to allow software to minimally parse
1203  * "nxm_entry"s of unknown types.  (Similarly, for a given nxm_vendor,
1204  * nxm_field, and nxm_length, nxm_hasmask is a constant.)
1205  *
1206  *
1207  * Semantics
1208  * =========
1209  *
1210  * A zero-length nx_match (one with no "nxm_entry"s) matches every packet.
1211  *
1212  * An nxm_entry places a constraint on the packets matched by the nx_match:
1213  *
1214  *   - If nxm_hasmask is 0, the nxm_entry's body contains a value for the
1215  *     field, called "nxm_value".  The nx_match matches only packets in which
1216  *     the field equals nxm_value.
1217  *
1218  *   - If nxm_hasmask is 1, then the nxm_entry's body contains a value for the
1219  *     field (nxm_value), followed by a bitmask of the same length as the
1220  *     value, called "nxm_mask".  For each 1-bit in position J in nxm_mask, the
1221  *     nx_match matches only packets for which bit J in the given field's value
1222  *     matches bit J in nxm_value.  A 0-bit in nxm_mask causes the
1223  *     corresponding bits in nxm_value and the field's value to be ignored.
1224  *     (The sense of the nxm_mask bits is the opposite of that used by the
1225  *     "wildcards" member of struct ofp10_match.)
1226  *
1227  *     When nxm_hasmask is 1, nxm_length is always even.
1228  *
1229  *     An all-zero-bits nxm_mask is equivalent to omitting the nxm_entry
1230  *     entirely.  An all-one-bits nxm_mask is equivalent to specifying 0 for
1231  *     nxm_hasmask.
1232  *
1233  * When there are multiple "nxm_entry"s, all of the constraints must be met.
1234  *
1235  *
1236  * Mask Restrictions
1237  * =================
1238  *
1239  * Masks may be restricted:
1240  *
1241  *   - Some nxm_types may not support masked wildcards, that is, nxm_hasmask
1242  *     must always be 0 when these fields are specified.  For example, the
1243  *     field that identifies the port on which a packet was received may not be
1244  *     masked.
1245  *
1246  *   - Some nxm_types that do support masked wildcards may only support certain
1247  *     nxm_mask patterns.  For example, fields that have IPv4 address values
1248  *     may be restricted to CIDR masks.
1249  *
1250  * These restrictions should be noted in specifications for individual fields.
1251  * A switch may accept an nxm_hasmask or nxm_mask value that the specification
1252  * disallows, if the switch correctly implements support for that nxm_hasmask
1253  * or nxm_mask value.  A switch must reject an attempt to set up a flow that
1254  * contains a nxm_hasmask or nxm_mask value that it does not support.
1255  *
1256  *
1257  * Prerequisite Restrictions
1258  * =========================
1259  *
1260  * The presence of an nxm_entry with a given nxm_type may be restricted based
1261  * on the presence of or values of other "nxm_entry"s.  For example:
1262  *
1263  *   - An nxm_entry for nxm_type=NXM_OF_IP_TOS is allowed only if it is
1264  *     preceded by another entry with nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0,
1265  *     and nxm_value=0x0800.  That is, matching on the IP source address is
1266  *     allowed only if the Ethernet type is explicitly set to IP.
1267  *
1268  *   - An nxm_entry for nxm_type=NXM_OF_TCP_SRC is allowed only if it is
1269  *     preceded by an entry with nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and
1270  *     nxm_value either 0x0800 or 0x86dd, and another with
1271  *     nxm_type=NXM_OF_IP_PROTO, nxm_hasmask=0, nxm_value=6, in that order.
1272  *     That is, matching on the TCP source port is allowed only if the Ethernet
1273  *     type is IP or IPv6 and the IP protocol is TCP.
1274  *
1275  * These restrictions should be noted in specifications for individual fields.
1276  * A switch may implement relaxed versions of these restrictions.  A switch
1277  * must reject an attempt to set up a flow that violates its restrictions.
1278  *
1279  *
1280  * Ordering Restrictions
1281  * =====================
1282  *
1283  * An nxm_entry that has prerequisite restrictions must appear after the
1284  * "nxm_entry"s for its prerequisites.  Ordering of "nxm_entry"s within an
1285  * nx_match is not otherwise constrained.
1286  *
1287  * Any given nxm_type may appear in an nx_match at most once.
1288  *
1289  *
1290  * nxm_entry Examples
1291  * ==================
1292  *
1293  * These examples show the format of a single nxm_entry with particular
1294  * nxm_hasmask and nxm_length values.  The diagrams are labeled with field
1295  * numbers and byte indexes.
1296  *
1297  *
1298  * 8-bit nxm_value, nxm_hasmask=1, nxm_length=2:
1299  *
1300  *  0          3  4   5
1301  * +------------+---+---+
1302  * |   header   | v | m |
1303  * +------------+---+---+
1304  *
1305  *
1306  * 16-bit nxm_value, nxm_hasmask=0, nxm_length=2:
1307  *
1308  *  0          3 4    5
1309  * +------------+------+
1310  * |   header   | value|
1311  * +------------+------+
1312  *
1313  *
1314  * 32-bit nxm_value, nxm_hasmask=0, nxm_length=4:
1315  *
1316  *  0          3 4           7
1317  * +------------+-------------+
1318  * |   header   |  nxm_value  |
1319  * +------------+-------------+
1320  *
1321  *
1322  * 48-bit nxm_value, nxm_hasmask=0, nxm_length=6:
1323  *
1324  *  0          3 4                9
1325  * +------------+------------------+
1326  * |   header   |     nxm_value    |
1327  * +------------+------------------+
1328  *
1329  *
1330  * 48-bit nxm_value, nxm_hasmask=1, nxm_length=12:
1331  *
1332  *  0          3 4                9 10              15
1333  * +------------+------------------+------------------+
1334  * |   header   |     nxm_value    |      nxm_mask    |
1335  * +------------+------------------+------------------+
1336  *
1337  *
1338  * Error Reporting
1339  * ===============
1340  *
1341  * A switch should report an error in an nx_match using error type
1342  * OFPET_BAD_REQUEST and one of the NXBRC_NXM_* codes.  Ideally the switch
1343  * should report a specific error code, if one is assigned for the particular
1344  * problem, but NXBRC_NXM_INVALID is also available to report a generic
1345  * nx_match error.
1346  */
1347
1348 #define NXM_HEADER__(VENDOR, FIELD, HASMASK, LENGTH) \
1349     (((VENDOR) << 16) | ((FIELD) << 9) | ((HASMASK) << 8) | (LENGTH))
1350 #define NXM_HEADER(VENDOR, FIELD, LENGTH) \
1351     NXM_HEADER__(VENDOR, FIELD, 0, LENGTH)
1352 #define NXM_HEADER_W(VENDOR, FIELD, LENGTH) \
1353     NXM_HEADER__(VENDOR, FIELD, 1, (LENGTH) * 2)
1354 #define NXM_VENDOR(HEADER) ((HEADER) >> 16)
1355 #define NXM_FIELD(HEADER) (((HEADER) >> 9) & 0x7f)
1356 #define NXM_TYPE(HEADER) (((HEADER) >> 9) & 0x7fffff)
1357 #define NXM_HASMASK(HEADER) (((HEADER) >> 8) & 1)
1358 #define NXM_LENGTH(HEADER) ((HEADER) & 0xff)
1359
1360 #define NXM_MAKE_WILD_HEADER(HEADER) \
1361         NXM_HEADER_W(NXM_VENDOR(HEADER), NXM_FIELD(HEADER), NXM_LENGTH(HEADER))
1362
1363 /* ## ------------------------------- ## */
1364 /* ## OpenFlow 1.0-compatible fields. ## */
1365 /* ## ------------------------------- ## */
1366
1367 /* Physical or virtual port on which the packet was received.
1368  *
1369  * Prereqs: None.
1370  *
1371  * Format: 16-bit integer in network byte order.
1372  *
1373  * Masking: Not maskable. */
1374 #define NXM_OF_IN_PORT    NXM_HEADER  (0x0000,  0, 2)
1375
1376 /* Source or destination address in Ethernet header.
1377  *
1378  * Prereqs: None.
1379  *
1380  * Format: 48-bit Ethernet MAC address.
1381  *
1382  * Masking: Fully maskable, in versions 1.8 and later. Earlier versions only
1383  *   supported the following masks for NXM_OF_ETH_DST_W: 00:00:00:00:00:00,
1384  *   fe:ff:ff:ff:ff:ff, 01:00:00:00:00:00, ff:ff:ff:ff:ff:ff. */
1385 #define NXM_OF_ETH_DST    NXM_HEADER  (0x0000,  1, 6)
1386 #define NXM_OF_ETH_DST_W  NXM_HEADER_W(0x0000,  1, 6)
1387 #define NXM_OF_ETH_SRC    NXM_HEADER  (0x0000,  2, 6)
1388 #define NXM_OF_ETH_SRC_W  NXM_HEADER_W(0x0000,  2, 6)
1389
1390 /* Packet's Ethernet type.
1391  *
1392  * For an Ethernet II packet this is taken from the Ethernet header.  For an
1393  * 802.2 LLC+SNAP header with OUI 00-00-00 this is taken from the SNAP header.
1394  * A packet that has neither format has value 0x05ff
1395  * (OFP_DL_TYPE_NOT_ETH_TYPE).
1396  *
1397  * For a packet with an 802.1Q header, this is the type of the encapsulated
1398  * frame.
1399  *
1400  * Prereqs: None.
1401  *
1402  * Format: 16-bit integer in network byte order.
1403  *
1404  * Masking: Not maskable. */
1405 #define NXM_OF_ETH_TYPE   NXM_HEADER  (0x0000,  3, 2)
1406
1407 /* 802.1Q TCI.
1408  *
1409  * For a packet with an 802.1Q header, this is the Tag Control Information
1410  * (TCI) field, with the CFI bit forced to 1.  For a packet with no 802.1Q
1411  * header, this has value 0.
1412  *
1413  * Prereqs: None.
1414  *
1415  * Format: 16-bit integer in network byte order.
1416  *
1417  * Masking: Arbitrary masks.
1418  *
1419  * This field can be used in various ways:
1420  *
1421  *   - If it is not constrained at all, the nx_match matches packets without
1422  *     an 802.1Q header or with an 802.1Q header that has any TCI value.
1423  *
1424  *   - Testing for an exact match with 0 matches only packets without an
1425  *     802.1Q header.
1426  *
1427  *   - Testing for an exact match with a TCI value with CFI=1 matches packets
1428  *     that have an 802.1Q header with a specified VID and PCP.
1429  *
1430  *   - Testing for an exact match with a nonzero TCI value with CFI=0 does
1431  *     not make sense.  The switch may reject this combination.
1432  *
1433  *   - Testing with a specific VID and CFI=1, with nxm_mask=0x1fff, matches
1434  *     packets that have an 802.1Q header with that VID (and any PCP).
1435  *
1436  *   - Testing with a specific PCP and CFI=1, with nxm_mask=0xf000, matches
1437  *     packets that have an 802.1Q header with that PCP (and any VID).
1438  *
1439  *   - Testing with nxm_value=0, nxm_mask=0x0fff matches packets with no 802.1Q
1440  *     header or with an 802.1Q header with a VID of 0.
1441  *
1442  *   - Testing with nxm_value=0, nxm_mask=0xe000 matches packets with no 802.1Q
1443  *     header or with an 802.1Q header with a PCP of 0.
1444  *
1445  *   - Testing with nxm_value=0, nxm_mask=0xefff matches packets with no 802.1Q
1446  *     header or with an 802.1Q header with both VID and PCP of 0.
1447  */
1448 #define NXM_OF_VLAN_TCI   NXM_HEADER  (0x0000,  4, 2)
1449 #define NXM_OF_VLAN_TCI_W NXM_HEADER_W(0x0000,  4, 2)
1450
1451 /* The "type of service" byte of the IP header, with the ECN bits forced to 0.
1452  *
1453  * Prereqs: NXM_OF_ETH_TYPE must be either 0x0800 or 0x86dd.
1454  *
1455  * Format: 8-bit integer with 2 least-significant bits forced to 0.
1456  *
1457  * Masking: Not maskable. */
1458 #define NXM_OF_IP_TOS     NXM_HEADER  (0x0000,  5, 1)
1459
1460 /* The "protocol" byte in the IP header.
1461  *
1462  * Prereqs: NXM_OF_ETH_TYPE must be either 0x0800 or 0x86dd.
1463  *
1464  * Format: 8-bit integer.
1465  *
1466  * Masking: Not maskable. */
1467 #define NXM_OF_IP_PROTO   NXM_HEADER  (0x0000,  6, 1)
1468
1469 /* The source or destination address in the IP header.
1470  *
1471  * Prereqs: NXM_OF_ETH_TYPE must match 0x0800 exactly.
1472  *
1473  * Format: 32-bit integer in network byte order.
1474  *
1475  * Masking: Fully maskable, in Open vSwitch 1.8 and later.  In earlier
1476  *   versions, only CIDR masks are allowed, that is, masks that consist of N
1477  *   high-order bits set to 1 and the other 32-N bits set to 0. */
1478 #define NXM_OF_IP_SRC     NXM_HEADER  (0x0000,  7, 4)
1479 #define NXM_OF_IP_SRC_W   NXM_HEADER_W(0x0000,  7, 4)
1480 #define NXM_OF_IP_DST     NXM_HEADER  (0x0000,  8, 4)
1481 #define NXM_OF_IP_DST_W   NXM_HEADER_W(0x0000,  8, 4)
1482
1483 /* The source or destination port in the TCP header.
1484  *
1485  * Prereqs:
1486  *   NXM_OF_ETH_TYPE must be either 0x0800 or 0x86dd.
1487  *   NXM_OF_IP_PROTO must match 6 exactly.
1488  *
1489  * Format: 16-bit integer in network byte order.
1490  *
1491  * Masking: Fully maskable, in Open vSwitch 1.6 and later.  Not maskable, in
1492  *   earlier versions. */
1493 #define NXM_OF_TCP_SRC    NXM_HEADER  (0x0000,  9, 2)
1494 #define NXM_OF_TCP_SRC_W  NXM_HEADER_W(0x0000,  9, 2)
1495 #define NXM_OF_TCP_DST    NXM_HEADER  (0x0000, 10, 2)
1496 #define NXM_OF_TCP_DST_W  NXM_HEADER_W(0x0000, 10, 2)
1497
1498 /* The source or destination port in the UDP header.
1499  *
1500  * Prereqs:
1501  *   NXM_OF_ETH_TYPE must match either 0x0800 or 0x86dd.
1502  *   NXM_OF_IP_PROTO must match 17 exactly.
1503  *
1504  * Format: 16-bit integer in network byte order.
1505  *
1506  * Masking: Fully maskable, in Open vSwitch 1.6 and later.  Not maskable, in
1507  *   earlier versions. */
1508 #define NXM_OF_UDP_SRC    NXM_HEADER  (0x0000, 11, 2)
1509 #define NXM_OF_UDP_SRC_W  NXM_HEADER_W(0x0000, 11, 2)
1510 #define NXM_OF_UDP_DST    NXM_HEADER  (0x0000, 12, 2)
1511 #define NXM_OF_UDP_DST_W  NXM_HEADER_W(0x0000, 12, 2)
1512
1513 /* The type or code in the ICMP header.
1514  *
1515  * Prereqs:
1516  *   NXM_OF_ETH_TYPE must match 0x0800 exactly.
1517  *   NXM_OF_IP_PROTO must match 1 exactly.
1518  *
1519  * Format: 8-bit integer.
1520  *
1521  * Masking: Not maskable. */
1522 #define NXM_OF_ICMP_TYPE  NXM_HEADER  (0x0000, 13, 1)
1523 #define NXM_OF_ICMP_CODE  NXM_HEADER  (0x0000, 14, 1)
1524
1525 /* ARP opcode.
1526  *
1527  * For an Ethernet+IP ARP packet, the opcode in the ARP header.  Always 0
1528  * otherwise.  Only ARP opcodes between 1 and 255 should be specified for
1529  * matching.
1530  *
1531  * Prereqs: NXM_OF_ETH_TYPE must match 0x0806 exactly.
1532  *
1533  * Format: 16-bit integer in network byte order.
1534  *
1535  * Masking: Not maskable. */
1536 #define NXM_OF_ARP_OP     NXM_HEADER  (0x0000, 15, 2)
1537
1538 /* For an Ethernet+IP ARP packet, the source or target protocol address
1539  * in the ARP header.  Always 0 otherwise.
1540  *
1541  * Prereqs: NXM_OF_ETH_TYPE must match 0x0806 exactly.
1542  *
1543  * Format: 32-bit integer in network byte order.
1544  *
1545  * Masking: Fully maskable, in Open vSwitch 1.8 and later.  In earlier
1546  *   versions, only CIDR masks are allowed, that is, masks that consist of N
1547  *   high-order bits set to 1 and the other 32-N bits set to 0. */
1548 #define NXM_OF_ARP_SPA    NXM_HEADER  (0x0000, 16, 4)
1549 #define NXM_OF_ARP_SPA_W  NXM_HEADER_W(0x0000, 16, 4)
1550 #define NXM_OF_ARP_TPA    NXM_HEADER  (0x0000, 17, 4)
1551 #define NXM_OF_ARP_TPA_W  NXM_HEADER_W(0x0000, 17, 4)
1552
1553 /* ## ------------------------ ## */
1554 /* ## Nicira match extensions. ## */
1555 /* ## ------------------------ ## */
1556
1557 /* Metadata registers.
1558  *
1559  * Registers initially have value 0.  Actions allow register values to be
1560  * manipulated.
1561  *
1562  * Prereqs: None.
1563  *
1564  * Format: Array of 32-bit integer registers.  Space is reserved for up to
1565  *   NXM_NX_MAX_REGS registers, but switches may implement fewer.
1566  *
1567  * Masking: Arbitrary masks. */
1568 #define NXM_NX_MAX_REGS 16
1569 #define NXM_NX_REG(IDX)   NXM_HEADER  (0x0001, IDX, 4)
1570 #define NXM_NX_REG_W(IDX) NXM_HEADER_W(0x0001, IDX, 4)
1571 #define NXM_NX_REG_IDX(HEADER) NXM_FIELD(HEADER)
1572 #define NXM_IS_NX_REG(HEADER) (!((((HEADER) ^ NXM_NX_REG0)) & 0xffffe1ff))
1573 #define NXM_IS_NX_REG_W(HEADER) (!((((HEADER) ^ NXM_NX_REG0_W)) & 0xffffe1ff))
1574 #define NXM_NX_REG0       NXM_HEADER  (0x0001, 0, 4)
1575 #define NXM_NX_REG0_W     NXM_HEADER_W(0x0001, 0, 4)
1576 #define NXM_NX_REG1       NXM_HEADER  (0x0001, 1, 4)
1577 #define NXM_NX_REG1_W     NXM_HEADER_W(0x0001, 1, 4)
1578 #define NXM_NX_REG2       NXM_HEADER  (0x0001, 2, 4)
1579 #define NXM_NX_REG2_W     NXM_HEADER_W(0x0001, 2, 4)
1580 #define NXM_NX_REG3       NXM_HEADER  (0x0001, 3, 4)
1581 #define NXM_NX_REG3_W     NXM_HEADER_W(0x0001, 3, 4)
1582 #define NXM_NX_REG4       NXM_HEADER  (0x0001, 4, 4)
1583 #define NXM_NX_REG4_W     NXM_HEADER_W(0x0001, 4, 4)
1584 #define NXM_NX_REG5       NXM_HEADER  (0x0001, 5, 4)
1585 #define NXM_NX_REG5_W     NXM_HEADER_W(0x0001, 5, 4)
1586 #define NXM_NX_REG6       NXM_HEADER  (0x0001, 6, 4)
1587 #define NXM_NX_REG6_W     NXM_HEADER_W(0x0001, 6, 4)
1588 #define NXM_NX_REG7       NXM_HEADER  (0x0001, 7, 4)
1589 #define NXM_NX_REG7_W     NXM_HEADER_W(0x0001, 7, 4)
1590
1591 /* Tunnel ID.
1592  *
1593  * For a packet received via GRE tunnel including a (32-bit) key, the key is
1594  * stored in the low 32-bits and the high bits are zeroed.  For other packets,
1595  * the value is 0.
1596  *
1597  * Prereqs: None.
1598  *
1599  * Format: 64-bit integer in network byte order.
1600  *
1601  * Masking: Arbitrary masks. */
1602 #define NXM_NX_TUN_ID     NXM_HEADER  (0x0001, 16, 8)
1603 #define NXM_NX_TUN_ID_W   NXM_HEADER_W(0x0001, 16, 8)
1604
1605 /* For an Ethernet+IP ARP packet, the source or target hardware address
1606  * in the ARP header.  Always 0 otherwise.
1607  *
1608  * Prereqs: NXM_OF_ETH_TYPE must match 0x0806 exactly.
1609  *
1610  * Format: 48-bit Ethernet MAC address.
1611  *
1612  * Masking: Not maskable. */
1613 #define NXM_NX_ARP_SHA    NXM_HEADER  (0x0001, 17, 6)
1614 #define NXM_NX_ARP_THA    NXM_HEADER  (0x0001, 18, 6)
1615
1616 /* The source or destination address in the IPv6 header.
1617  *
1618  * Prereqs: NXM_OF_ETH_TYPE must match 0x86dd exactly.
1619  *
1620  * Format: 128-bit IPv6 address.
1621  *
1622  * Masking: Fully maskable, in Open vSwitch 1.8 and later.  In previous
1623  *   versions, only CIDR masks are allowed, that is, masks that consist of N
1624  *   high-order bits set to 1 and the other 128-N bits set to 0. */
1625 #define NXM_NX_IPV6_SRC    NXM_HEADER  (0x0001, 19, 16)
1626 #define NXM_NX_IPV6_SRC_W  NXM_HEADER_W(0x0001, 19, 16)
1627 #define NXM_NX_IPV6_DST    NXM_HEADER  (0x0001, 20, 16)
1628 #define NXM_NX_IPV6_DST_W  NXM_HEADER_W(0x0001, 20, 16)
1629
1630 /* The type or code in the ICMPv6 header.
1631  *
1632  * Prereqs:
1633  *   NXM_OF_ETH_TYPE must match 0x86dd exactly.
1634  *   NXM_OF_IP_PROTO must match 58 exactly.
1635  *
1636  * Format: 8-bit integer.
1637  *
1638  * Masking: Not maskable. */
1639 #define NXM_NX_ICMPV6_TYPE NXM_HEADER  (0x0001, 21, 1)
1640 #define NXM_NX_ICMPV6_CODE NXM_HEADER  (0x0001, 22, 1)
1641
1642 /* The target address in an IPv6 Neighbor Discovery message.
1643  *
1644  * Prereqs:
1645  *   NXM_OF_ETH_TYPE must match 0x86dd exactly.
1646  *   NXM_OF_IP_PROTO must match 58 exactly.
1647  *   NXM_OF_ICMPV6_TYPE must be either 135 or 136.
1648  *
1649  * Format: 128-bit IPv6 address.
1650  *
1651  * Masking: Fully maskable, in Open vSwitch 1.8 and later.  In previous
1652  *   versions, only CIDR masks are allowed, that is, masks that consist of N
1653  *   high-order bits set to 1 and the other 128-N bits set to 0. */
1654 #define NXM_NX_ND_TARGET     NXM_HEADER    (0x0001, 23, 16)
1655 #define NXM_NX_ND_TARGET_W   NXM_HEADER_W  (0x0001, 23, 16)
1656
1657 /* The source link-layer address option in an IPv6 Neighbor Discovery
1658  * message.
1659  *
1660  * Prereqs:
1661  *   NXM_OF_ETH_TYPE must match 0x86dd exactly.
1662  *   NXM_OF_IP_PROTO must match 58 exactly.
1663  *   NXM_OF_ICMPV6_TYPE must be exactly 135.
1664  *
1665  * Format: 48-bit Ethernet MAC address.
1666  *
1667  * Masking: Not maskable. */
1668 #define NXM_NX_ND_SLL      NXM_HEADER  (0x0001, 24, 6)
1669
1670 /* The target link-layer address option in an IPv6 Neighbor Discovery
1671  * message.
1672  *
1673  * Prereqs:
1674  *   NXM_OF_ETH_TYPE must match 0x86dd exactly.
1675  *   NXM_OF_IP_PROTO must match 58 exactly.
1676  *   NXM_OF_ICMPV6_TYPE must be exactly 136.
1677  *
1678  * Format: 48-bit Ethernet MAC address.
1679  *
1680  * Masking: Not maskable. */
1681 #define NXM_NX_ND_TLL      NXM_HEADER  (0x0001, 25, 6)
1682
1683 /* IP fragment information.
1684  *
1685  * Prereqs:
1686  *   NXM_OF_ETH_TYPE must be either 0x0800 or 0x86dd.
1687  *
1688  * Format: 8-bit value with one of the values 0, 1, or 3, as described below.
1689  *
1690  * Masking: Fully maskable.
1691  *
1692  * This field has three possible values:
1693  *
1694  *   - A packet that is not an IP fragment has value 0.
1695  *
1696  *   - A packet that is an IP fragment with offset 0 (the first fragment) has
1697  *     bit 0 set and thus value 1.
1698  *
1699  *   - A packet that is an IP fragment with nonzero offset has bits 0 and 1 set
1700  *     and thus value 3.
1701  *
1702  * NX_IP_FRAG_ANY and NX_IP_FRAG_LATER are declared to symbolically represent
1703  * the meanings of bits 0 and 1.
1704  *
1705  * The switch may reject matches against values that can never appear.
1706  *
1707  * It is important to understand how this field interacts with the OpenFlow IP
1708  * fragment handling mode:
1709  *
1710  *   - In OFPC_FRAG_DROP mode, the OpenFlow switch drops all IP fragments
1711  *     before they reach the flow table, so every packet that is available for
1712  *     matching will have value 0 in this field.
1713  *
1714  *   - Open vSwitch does not implement OFPC_FRAG_REASM mode, but if it did then
1715  *     IP fragments would be reassembled before they reached the flow table and
1716  *     again every packet available for matching would always have value 0.
1717  *
1718  *   - In OFPC_FRAG_NORMAL mode, all three values are possible, but OpenFlow
1719  *     1.0 says that fragments' transport ports are always 0, even for the
1720  *     first fragment, so this does not provide much extra information.
1721  *
1722  *   - In OFPC_FRAG_NX_MATCH mode, all three values are possible.  For
1723  *     fragments with offset 0, Open vSwitch makes L4 header information
1724  *     available.
1725  */
1726 #define NXM_NX_IP_FRAG     NXM_HEADER  (0x0001, 26, 1)
1727 #define NXM_NX_IP_FRAG_W   NXM_HEADER_W(0x0001, 26, 1)
1728
1729 /* Bits in the value of NXM_NX_IP_FRAG. */
1730 #define NX_IP_FRAG_ANY   (1 << 0) /* Is this a fragment? */
1731 #define NX_IP_FRAG_LATER (1 << 1) /* Is this a fragment with nonzero offset? */
1732
1733 /* The flow label in the IPv6 header.
1734  *
1735  * Prereqs: NXM_OF_ETH_TYPE must match 0x86dd exactly.
1736  *
1737  * Format: 20-bit IPv6 flow label in least-significant bits.
1738  *
1739  * Masking: Not maskable. */
1740 #define NXM_NX_IPV6_LABEL  NXM_HEADER  (0x0001, 27, 4)
1741
1742 /* The ECN of the IP header.
1743  *
1744  * Prereqs: NXM_OF_ETH_TYPE must be either 0x0800 or 0x86dd.
1745  *
1746  * Format: ECN in the low-order 2 bits.
1747  *
1748  * Masking: Not maskable. */
1749 #define NXM_NX_IP_ECN      NXM_HEADER  (0x0001, 28, 1)
1750
1751 /* The time-to-live/hop limit of the IP header.
1752  *
1753  * Prereqs: NXM_OF_ETH_TYPE must be either 0x0800 or 0x86dd.
1754  *
1755  * Format: 8-bit integer.
1756  *
1757  * Masking: Not maskable. */
1758 #define NXM_NX_IP_TTL      NXM_HEADER  (0x0001, 29, 1)
1759
1760 /* Flow cookie.
1761  *
1762  * This may be used to gain the OpenFlow 1.1-like ability to restrict
1763  * certain NXM-based Flow Mod and Flow Stats Request messages to flows
1764  * with specific cookies.  See the "nx_flow_mod" and "nx_flow_stats_request"
1765  * structure definitions for more details.  This match is otherwise not
1766  * allowed.
1767  *
1768  * Prereqs: None.
1769  *
1770  * Format: 64-bit integer in network byte order.
1771  *
1772  * Masking: Arbitrary masks. */
1773 #define NXM_NX_COOKIE     NXM_HEADER  (0x0001, 30, 8)
1774 #define NXM_NX_COOKIE_W   NXM_HEADER_W(0x0001, 30, 8)
1775
1776 /* ## --------------------- ## */
1777 /* ## Requests and replies. ## */
1778 /* ## --------------------- ## */
1779
1780 enum nx_flow_format {
1781     NXFF_OPENFLOW10 = 0,         /* Standard OpenFlow 1.0 compatible. */
1782     NXFF_NXM = 2,                /* Nicira extended match. */
1783     NXFF_OPENFLOW12 = 3          /* OpenFlow 1.2 format. */
1784 };
1785
1786 /* NXT_SET_FLOW_FORMAT request. */
1787 struct nx_set_flow_format {
1788     struct nicira_header nxh;
1789     ovs_be32 format;            /* One of NXFF_*. */
1790 };
1791 OFP_ASSERT(sizeof(struct nx_set_flow_format) == 20);
1792
1793 /* NXT_FLOW_MOD (analogous to OFPT_FLOW_MOD).
1794  *
1795  * It is possible to limit flow deletions and modifications to certain
1796  * cookies by using the NXM_NX_COOKIE(_W) matches.  The "cookie" field
1797  * is used only to add or modify flow cookies.
1798  */
1799 struct nx_flow_mod {
1800     struct nicira_header nxh;
1801     ovs_be64 cookie;              /* Opaque controller-issued identifier. */
1802     ovs_be16 command;             /* One of OFPFC_*. */
1803     ovs_be16 idle_timeout;        /* Idle time before discarding (seconds). */
1804     ovs_be16 hard_timeout;        /* Max time before discarding (seconds). */
1805     ovs_be16 priority;            /* Priority level of flow entry. */
1806     ovs_be32 buffer_id;           /* Buffered packet to apply to (or -1).
1807                                      Not meaningful for OFPFC_DELETE*. */
1808     ovs_be16 out_port;            /* For OFPFC_DELETE* commands, require
1809                                      matching entries to include this as an
1810                                      output port.  A value of OFPP_NONE
1811                                      indicates no restriction. */
1812     ovs_be16 flags;               /* One of OFPFF_*. */
1813     ovs_be16 match_len;           /* Size of nx_match. */
1814     uint8_t pad[6];               /* Align to 64-bits. */
1815     /* Followed by:
1816      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
1817      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
1818      *     all-zero bytes, then
1819      *   - Actions to fill out the remainder of the message length (always a
1820      *     multiple of 8).
1821      */
1822 };
1823 OFP_ASSERT(sizeof(struct nx_flow_mod) == 48);
1824
1825 /* NXT_FLOW_REMOVED (analogous to OFPT_FLOW_REMOVED). */
1826 struct nx_flow_removed {
1827     struct nicira_header nxh;
1828     ovs_be64 cookie;          /* Opaque controller-issued identifier. */
1829     ovs_be16 priority;        /* Priority level of flow entry. */
1830     uint8_t reason;           /* One of OFPRR_*. */
1831     uint8_t pad[1];           /* Align to 32-bits. */
1832     ovs_be32 duration_sec;    /* Time flow was alive in seconds. */
1833     ovs_be32 duration_nsec;   /* Time flow was alive in nanoseconds beyond
1834                                  duration_sec. */
1835     ovs_be16 idle_timeout;    /* Idle timeout from original flow mod. */
1836     ovs_be16 match_len;       /* Size of nx_match. */
1837     ovs_be64 packet_count;
1838     ovs_be64 byte_count;
1839     /* Followed by:
1840      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
1841      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
1842      *     all-zero bytes. */
1843 };
1844 OFP_ASSERT(sizeof(struct nx_flow_removed) == 56);
1845
1846 /* Nicira vendor stats request of type NXST_FLOW (analogous to OFPST_FLOW
1847  * request).
1848  *
1849  * It is possible to limit matches to certain cookies by using the
1850  * NXM_NX_COOKIE and NXM_NX_COOKIE_W matches.
1851  */
1852 struct nx_flow_stats_request {
1853     struct nicira_stats_msg nsm;
1854     ovs_be16 out_port;        /* Require matching entries to include this
1855                                  as an output port.  A value of OFPP_NONE
1856                                  indicates no restriction. */
1857     ovs_be16 match_len;       /* Length of nx_match. */
1858     uint8_t table_id;         /* ID of table to read (from ofp_table_stats)
1859                                  or 0xff for all tables. */
1860     uint8_t pad[3];           /* Align to 64 bits. */
1861     /* Followed by:
1862      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
1863      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
1864      *     all-zero bytes, which must also exactly fill out the length of the
1865      *     message.
1866      */
1867 };
1868 OFP_ASSERT(sizeof(struct nx_flow_stats_request) == 32);
1869
1870 /* Body for Nicira vendor stats reply of type NXST_FLOW (analogous to
1871  * OFPST_FLOW reply).
1872  *
1873  * The values of 'idle_age' and 'hard_age' are only meaningful when talking to
1874  * a switch that implements the NXT_FLOW_AGE extension.  Zero means that the
1875  * true value is unknown, perhaps because hardware does not track the value.
1876  * (Zero is also the value that one should ordinarily expect to see talking to
1877  * a switch that does not implement NXT_FLOW_AGE, since those switches zero the
1878  * padding bytes that these fields replaced.)  A nonzero value X represents X-1
1879  * seconds.  A value of 65535 represents 65534 or more seconds.
1880  *
1881  * 'idle_age' is the number of seconds that the flow has been idle, that is,
1882  * the number of seconds since a packet passed through the flow.  'hard_age' is
1883  * the number of seconds since the flow was last modified (e.g. OFPFC_MODIFY or
1884  * OFPFC_MODIFY_STRICT).  (The 'duration_*' fields are the elapsed time since
1885  * the flow was added, regardless of subsequent modifications.)
1886  *
1887  * For a flow with an idle or hard timeout, 'idle_age' or 'hard_age',
1888  * respectively, will ordinarily be smaller than the timeout, but flow
1889  * expiration times are only approximate and so one must be prepared to
1890  * tolerate expirations that occur somewhat early or late.
1891  */
1892 struct nx_flow_stats {
1893     ovs_be16 length;          /* Length of this entry. */
1894     uint8_t table_id;         /* ID of table flow came from. */
1895     uint8_t pad;
1896     ovs_be32 duration_sec;    /* Time flow has been alive in seconds. */
1897     ovs_be32 duration_nsec;   /* Time flow has been alive in nanoseconds
1898                                  beyond duration_sec. */
1899     ovs_be16 priority;        /* Priority of the entry. */
1900     ovs_be16 idle_timeout;    /* Number of seconds idle before expiration. */
1901     ovs_be16 hard_timeout;    /* Number of seconds before expiration. */
1902     ovs_be16 match_len;       /* Length of nx_match. */
1903     ovs_be16 idle_age;        /* Seconds since last packet, plus one. */
1904     ovs_be16 hard_age;        /* Seconds since last modification, plus one. */
1905     ovs_be64 cookie;          /* Opaque controller-issued identifier. */
1906     ovs_be64 packet_count;    /* Number of packets, UINT64_MAX if unknown. */
1907     ovs_be64 byte_count;      /* Number of bytes, UINT64_MAX if unknown. */
1908     /* Followed by:
1909      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
1910      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
1911      *     all-zero bytes, then
1912      *   - Actions to fill out the remainder 'length' bytes (always a multiple
1913      *     of 8).
1914      */
1915 };
1916 OFP_ASSERT(sizeof(struct nx_flow_stats) == 48);
1917
1918 /* Nicira vendor stats request of type NXST_AGGREGATE (analogous to
1919  * OFPST_AGGREGATE request). */
1920 struct nx_aggregate_stats_request {
1921     struct nicira_stats_msg nsm;
1922     ovs_be16 out_port;        /* Require matching entries to include this
1923                                  as an output port.  A value of OFPP_NONE
1924                                  indicates no restriction. */
1925     ovs_be16 match_len;       /* Length of nx_match. */
1926     uint8_t table_id;         /* ID of table to read (from ofp_table_stats)
1927                                  or 0xff for all tables. */
1928     uint8_t pad[3];           /* Align to 64 bits. */
1929     /* Followed by:
1930      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
1931      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
1932      *     all-zero bytes, which must also exactly fill out the length of the
1933      *     message.
1934      */
1935 };
1936 OFP_ASSERT(sizeof(struct nx_aggregate_stats_request) == 32);
1937
1938 /* Body for nicira_stats_msg reply of type NXST_AGGREGATE (analogous to
1939  * OFPST_AGGREGATE reply). */
1940 struct nx_aggregate_stats_reply {
1941     struct nicira_stats_msg nsm;
1942     ovs_be64 packet_count;     /* Number of packets, UINT64_MAX if unknown. */
1943     ovs_be64 byte_count;       /* Number of bytes, UINT64_MAX if unknown. */
1944     ovs_be32 flow_count;       /* Number of flows. */
1945     uint8_t pad[4];            /* Align to 64 bits. */
1946 };
1947 OFP_ASSERT(sizeof(struct nx_aggregate_stats_reply) == 48);
1948 \f
1949 /* NXT_SET_CONTROLLER_ID.
1950  *
1951  * Each OpenFlow controller connection has a 16-bit identifier that is
1952  * initially 0.  This message changes the connection's ID to 'id'.
1953  *
1954  * Controller connection IDs need not be unique.
1955  *
1956  * The NXAST_CONTROLLER action is the only current user of controller
1957  * connection IDs. */
1958 struct nx_controller_id {
1959     struct nicira_header nxh;
1960     uint8_t zero[6];            /* Must be zero. */
1961     ovs_be16 controller_id;     /* New controller connection ID. */
1962 };
1963 OFP_ASSERT(sizeof(struct nx_controller_id) == 24);
1964
1965 /* Action structure for NXAST_CONTROLLER.
1966  *
1967  * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER.  In
1968  * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
1969  * specifying:
1970  *
1971  *    - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
1972  *
1973  *    - 'controller_id': The ID of the controller connection to which the
1974  *      ofp_packet_in should be sent.  The ofp_packet_in or nx_packet_in is
1975  *      sent only to controllers that have the specified controller connection
1976  *      ID.  See "struct nx_controller_id" for more information. */
1977 struct nx_action_controller {
1978     ovs_be16 type;                  /* OFPAT_VENDOR. */
1979     ovs_be16 len;                   /* Length is 16. */
1980     ovs_be32 vendor;                /* NX_VENDOR_ID. */
1981     ovs_be16 subtype;               /* NXAST_CONTROLLER. */
1982     ovs_be16 max_len;               /* Maximum length to send to controller. */
1983     ovs_be16 controller_id;         /* Controller ID to send packet-in. */
1984     uint8_t reason;                 /* enum ofp_packet_in_reason (OFPR_*). */
1985     uint8_t zero;                   /* Must be zero. */
1986 };
1987 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
1988 \f
1989 /* Flow Table Monitoring
1990  * =====================
1991  *
1992  * NXST_FLOW_MONITOR allows a controller to keep track of changes to OpenFlow
1993  * flow table(s) or subsets of them, with the following workflow:
1994  *
1995  * 1. The controller sends an NXST_FLOW_MONITOR request to begin monitoring
1996  *    flows.  The 'id' in the request must be unique among all monitors that
1997  *    the controller has started and not yet canceled on this OpenFlow
1998  *    connection.
1999  *
2000  * 2. The switch responds with an NXST_FLOW_MONITOR reply.  If the request's
2001  *    'flags' included NXFMF_INITIAL, the reply includes all the flows that
2002  *    matched the request at the time of the request (with event NXFME_ADDED).
2003  *    If 'flags' did not include NXFMF_INITIAL, the reply is empty.
2004  *
2005  *    The reply uses the xid of the request (as do all replies to OpenFlow
2006  *    requests).
2007  *
2008  * 3. Whenever a change to a flow table entry matches some outstanding monitor
2009  *    request's criteria and flags, the switch sends a notification to the
2010  *    controller as an additional NXST_FLOW_MONITOR reply with xid 0.
2011  *
2012  *    When multiple outstanding monitors match a single change, only a single
2013  *    notification is sent.  This merged notification includes the information
2014  *    requested in any of the individual monitors.  That is, if any of the
2015  *    matching monitors requests actions (NXFMF_ACTIONS), the notification
2016  *    includes actions, and if any of the monitors request full changes for the
2017  *    controller's own changes (NXFMF_OWN), the controller's own changes will
2018  *    be included in full.
2019  *
2020  * 4. The controller may cancel a monitor with NXT_FLOW_MONITOR_CANCEL.  No
2021  *    further notifications will be sent on the basis of the canceled monitor
2022  *    afterward.
2023  *
2024  *
2025  * Buffer Management
2026  * =================
2027  *
2028  * OpenFlow messages for flow monitor notifications can overflow the buffer
2029  * space available to the switch, either temporarily (e.g. due to network
2030  * conditions slowing OpenFlow traffic) or more permanently (e.g. the sustained
2031  * rate of flow table change exceeds the network bandwidth between switch and
2032  * controller).
2033  *
2034  * When Open vSwitch's notification buffer space reaches a limiting threshold,
2035  * OVS reacts as follows:
2036  *
2037  * 1. OVS sends an NXT_FLOW_MONITOR_PAUSED message to the controller, following
2038  *    all the already queued notifications.  After it receives this message,
2039  *    the controller knows that its view of the flow table, as represented by
2040  *    flow monitor notifications, is incomplete.
2041  *
2042  * 2. As long as the notification buffer is not empty:
2043  *
2044  *        - NXMFE_ADD and NXFME_MODIFIED notifications will not be sent.
2045  *
2046  *        - NXFME_DELETED notifications will still be sent, but only for flows
2047  *          that existed before OVS sent NXT_FLOW_MONITOR_PAUSED.
2048  *
2049  *        - NXFME_ABBREV notifications will not be sent.  They are treated as
2050  *          the expanded version (and therefore only the NXFME_DELETED
2051  *          components, if any, are sent).
2052  *
2053  * 3. When the notification buffer empties, OVS sends NXFME_ADD notifications
2054  *    for flows added since the buffer reached its limit and NXFME_MODIFIED
2055  *    notifications for flows that existed before the limit was reached and
2056  *    changed after the limit was reached.
2057  *
2058  * 4. OVS sends an NXT_FLOW_MONITOR_RESUMED message to the controller.  After
2059  *    it receives this message, the controller knows that its view of the flow
2060  *    table, as represented by flow monitor notifications, is again complete.
2061  *
2062  * This allows the maximum buffer space requirement for notifications to be
2063  * bounded by the limit plus the maximum number of supported flows.
2064  *
2065  *
2066  * "Flow Removed" messages
2067  * =======================
2068  *
2069  * The flow monitor mechanism is independent of OFPT_FLOW_REMOVED and
2070  * NXT_FLOW_REMOVED.  Flow monitor updates for deletion are sent if
2071  * NXFMF_DELETE is set on a monitor, regardless of whether the
2072  * OFPFF_SEND_FLOW_REM flag was set when the flow was added. */
2073
2074 /* NXST_FLOW_MONITOR request.
2075  *
2076  * The NXST_FLOW_MONITOR request's body consists of an array of zero or more
2077  * instances of this structure.  The request arranges to monitor the flows
2078  * that match the specified criteria, which are interpreted in the same way as
2079  * for NXST_FLOW.
2080  *
2081  * 'id' identifies a particular monitor for the purpose of allowing it to be
2082  * canceled later with NXT_FLOW_MONITOR_CANCEL.  'id' must be unique among
2083  * existing monitors that have not already been canceled.
2084  *
2085  * The reply includes the initial flow matches for monitors that have the
2086  * NXFMF_INITIAL flag set.  No single flow will be included in the reply more
2087  * than once, even if more than one requested monitor matches that flow.  The
2088  * reply will be empty if none of the monitors has NXFMF_INITIAL set or if none
2089  * of the monitors initially matches any flows.
2090  *
2091  * For NXFMF_ADD, an event will be reported if 'out_port' matches against the
2092  * actions of the flow being added or, for a flow that is replacing an existing
2093  * flow, if 'out_port' matches against the actions of the flow being replaced.
2094  * For NXFMF_DELETE, 'out_port' matches against the actions of a flow being
2095  * deleted.  For NXFMF_MODIFY, an event will be reported if 'out_port' matches
2096  * either the old or the new actions. */
2097 struct nx_flow_monitor_request {
2098     ovs_be32 id;                /* Controller-assigned ID for this monitor. */
2099     ovs_be16 flags;             /* NXFMF_*. */
2100     ovs_be16 out_port;          /* Required output port, if not OFPP_NONE. */
2101     ovs_be16 match_len;         /* Length of nx_match. */
2102     uint8_t table_id;           /* One table's ID or 0xff for all tables. */
2103     uint8_t zeros[5];           /* Align to 64 bits (must be zero). */
2104     /* Followed by:
2105      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
2106      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
2107      *     all-zero bytes. */
2108 };
2109 OFP_ASSERT(sizeof(struct nx_flow_monitor_request) == 16);
2110
2111 /* 'flags' bits in struct nx_flow_monitor_request. */
2112 enum nx_flow_monitor_flags {
2113     /* When to send updates. */
2114     NXFMF_INITIAL = 1 << 0,     /* Initially matching flows. */
2115     NXFMF_ADD = 1 << 1,         /* New matching flows as they are added. */
2116     NXFMF_DELETE = 1 << 2,      /* Old matching flows as they are removed. */
2117     NXFMF_MODIFY = 1 << 3,      /* Matching flows as they are changed. */
2118
2119     /* What to include in updates. */
2120     NXFMF_ACTIONS = 1 << 4,     /* If set, actions are included. */
2121     NXFMF_OWN = 1 << 5,         /* If set, include own changes in full. */
2122 };
2123
2124 /* NXST_FLOW_MONITOR reply header.
2125  *
2126  * The body of an NXST_FLOW_MONITOR reply is an array of variable-length
2127  * structures, each of which begins with this header.  The 'length' member may
2128  * be used to traverse the array, and the 'event' member may be used to
2129  * determine the particular structure.
2130  *
2131  * Every instance is a multiple of 8 bytes long. */
2132 struct nx_flow_update_header {
2133     ovs_be16 length;            /* Length of this entry. */
2134     ovs_be16 event;             /* One of NXFME_*. */
2135     /* ...other data depending on 'event'... */
2136 };
2137 OFP_ASSERT(sizeof(struct nx_flow_update_header) == 4);
2138
2139 /* 'event' values in struct nx_flow_update_header. */
2140 enum nx_flow_update_event {
2141     /* struct nx_flow_update_full. */
2142     NXFME_ADDED = 0,            /* Flow was added. */
2143     NXFME_DELETED = 1,          /* Flow was deleted. */
2144     NXFME_MODIFIED = 2,         /* Flow (generally its actions) was changed. */
2145
2146     /* struct nx_flow_update_abbrev. */
2147     NXFME_ABBREV = 3,           /* Abbreviated reply. */
2148 };
2149
2150 /* NXST_FLOW_MONITOR reply for NXFME_ADDED, NXFME_DELETED, and
2151  * NXFME_MODIFIED. */
2152 struct nx_flow_update_full {
2153     ovs_be16 length;            /* Length is 24. */
2154     ovs_be16 event;             /* One of NXFME_*. */
2155     ovs_be16 reason;            /* OFPRR_* for NXFME_DELETED, else zero. */
2156     ovs_be16 priority;          /* Priority of the entry. */
2157     ovs_be16 idle_timeout;      /* Number of seconds idle before expiration. */
2158     ovs_be16 hard_timeout;      /* Number of seconds before expiration. */
2159     ovs_be16 match_len;         /* Length of nx_match. */
2160     uint8_t table_id;           /* ID of flow's table. */
2161     uint8_t pad;                /* Reserved, currently zeroed. */
2162     ovs_be64 cookie;            /* Opaque controller-issued identifier. */
2163     /* Followed by:
2164      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
2165      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
2166      *     all-zero bytes, then
2167      *   - Actions to fill out the remainder 'length' bytes (always a multiple
2168      *     of 8).  If NXFMF_ACTIONS was not specified, or 'event' is
2169      *     NXFME_DELETED, no actions are included.
2170      */
2171 };
2172 OFP_ASSERT(sizeof(struct nx_flow_update_full) == 24);
2173
2174 /* NXST_FLOW_MONITOR reply for NXFME_ABBREV.
2175  *
2176  * When the controller does not specify NXFMF_OWN in a monitor request, any
2177  * flow tables changes due to the controller's own requests (on the same
2178  * OpenFlow channel) will be abbreviated, when possible, to this form, which
2179  * simply specifies the 'xid' of the OpenFlow request (e.g. an OFPT_FLOW_MOD or
2180  * NXT_FLOW_MOD) that caused the change.
2181  *
2182  * Some changes cannot be abbreviated and will be sent in full:
2183  *
2184  *   - Changes that only partially succeed.  This can happen if, for example,
2185  *     a flow_mod with type OFPFC_MODIFY affects multiple flows, but only some
2186  *     of those modifications succeed (e.g. due to hardware limitations).
2187  *
2188  *     This cannot occur with the current implementation of the Open vSwitch
2189  *     software datapath.  It could happen with other datapath implementations.
2190  *
2191  *   - Changes that race with conflicting changes made by other controllers or
2192  *     other flow_mods (not separated by barriers) by the same controller.
2193  *
2194  *     This cannot occur with the current Open vSwitch implementation
2195  *     (regardless of datapath) because Open vSwitch internally serializes
2196  *     potentially conflicting changes.
2197  *
2198  * A flow_mod that does not change the flow table will not trigger any
2199  * notification, even an abbreviated one.  For example, a "modify" or "delete"
2200  * flow_mod that does not match any flows will not trigger a notification.
2201  * Whether an "add" or "modify" that specifies all the same parameters that a
2202  * flow already has triggers a notification is unspecified and subject to
2203  * change in future versions of Open vSwitch.
2204  *
2205  * OVS will always send the notifications for a given flow table change before
2206  * the reply to a OFPT_BARRIER_REQUEST request that precedes the flow table
2207  * change.  Thus, if the controller does not receive an abbreviated
2208  * notification for a flow_mod before the next OFPT_BARRIER_REPLY, it will
2209  * never receive one. */
2210 struct nx_flow_update_abbrev {
2211     ovs_be16 length;            /* Length is 8. */
2212     ovs_be16 event;             /* NXFME_ABBREV. */
2213     ovs_be32 xid;               /* Controller-specified xid from flow_mod. */
2214 };
2215 OFP_ASSERT(sizeof(struct nx_flow_update_abbrev) == 8);
2216
2217 /* Used by a controller to cancel an outstanding monitor. */
2218 struct nx_flow_monitor_cancel {
2219     struct nicira_header nxh;   /* Type NXT_FLOW_MONITOR_CANCEL. */
2220     ovs_be32 id;                /* 'id' from nx_flow_monitor_request. */
2221 };
2222 OFP_ASSERT(sizeof(struct nx_flow_monitor_cancel) == 20);
2223
2224 #endif /* openflow/nicira-ext.h */