ofp-msgs: Add support for ONF extension messages.
[cascardo/ovs.git] / include / openflow / nicira-ext.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 \f
28 /* Nicira vendor-specific error messages extension.
29  *
30  * OpenFlow 1.0 has a set of predefined error types (OFPET_*) and codes (which
31  * are specific to each type).  It does not have any provision for
32  * vendor-specific error codes, and it does not even provide "generic" error
33  * codes that can apply to problems not anticipated by the OpenFlow
34  * specification authors.
35  *
36  * This extension attempts to address the problem by adding a generic "error
37  * vendor extension".  The extension works as follows: use NXET_VENDOR as type
38  * and NXVC_VENDOR_ERROR as code, followed by struct nx_vendor_error with
39  * vendor-specific details, followed by at least 64 bytes of the failed
40  * request.
41  *
42  * It would be better to have a type-specific vendor extension, e.g. so that
43  * OFPET_BAD_ACTION could be used with vendor-specific code values.  But
44  * OFPET_BAD_ACTION and most other standardized types already specify that
45  * their 'data' values are (the start of) the OpenFlow message being replied
46  * to, so there is no room to insert a vendor ID.
47  *
48  * Currently this extension is only implemented by Open vSwitch, but it seems
49  * like a reasonable candidate for future standardization.
50  */
51
52 /* This is a random number to avoid accidental collision with any other
53  * vendor's extension. */
54 #define NXET_VENDOR 0xb0c2
55
56 /* ofp_error msg 'code' values for NXET_VENDOR. */
57 enum nx_vendor_code {
58     NXVC_VENDOR_ERROR           /* 'data' contains struct nx_vendor_error. */
59 };
60
61 /* 'data' for 'type' == NXET_VENDOR, 'code' == NXVC_VENDOR_ERROR. */
62 struct nx_vendor_error {
63     ovs_be32 vendor;            /* Vendor ID as in struct ofp_vendor_header. */
64     ovs_be16 type;              /* Vendor-defined type. */
65     ovs_be16 code;              /* Vendor-defined subtype. */
66     /* Followed by at least the first 64 bytes of the failed request. */
67 };
68 \f
69 /* Nicira vendor requests and replies. */
70
71 /* Header for Nicira vendor stats request and reply messages in OpenFlow
72  * 1.0. */
73 struct nicira10_stats_msg {
74     struct ofp10_vendor_stats_msg vsm; /* Vendor NX_VENDOR_ID. */
75     ovs_be32 subtype;           /* One of NXST_* below. */
76     uint8_t pad[4];             /* Align to 64-bits. */
77 };
78 OFP_ASSERT(sizeof(struct nicira10_stats_msg) == 24);
79
80 /* Fields to use when hashing flows. */
81 enum nx_hash_fields {
82     /* Ethernet source address (NXM_OF_ETH_SRC) only. */
83     NX_HASH_FIELDS_ETH_SRC,
84
85     /* L2 through L4, symmetric across src/dst.  Specifically, each of the
86      * following fields, if present, is hashed (slashes separate symmetric
87      * pairs):
88      *
89      *  - NXM_OF_ETH_DST / NXM_OF_ETH_SRC
90      *  - NXM_OF_ETH_TYPE
91      *  - The VID bits from NXM_OF_VLAN_TCI, ignoring PCP and CFI.
92      *  - NXM_OF_IP_PROTO
93      *  - NXM_OF_IP_SRC / NXM_OF_IP_DST
94      *  - NXM_OF_TCP_SRC / NXM_OF_TCP_DST
95      */
96     NX_HASH_FIELDS_SYMMETRIC_L4,
97
98     /* L3+L4 only, including the following fields:
99      *
100      *  - NXM_OF_IP_PROTO
101      *  - NXM_OF_IP_SRC / NXM_OF_IP_DST
102      *  - NXM_OF_SCTP_SRC / NXM_OF_SCTP_DST
103      *  - NXM_OF_TCP_SRC / NXM_OF_TCP_DST
104      */
105     NX_HASH_FIELDS_SYMMETRIC_L3L4,
106
107     /* L3+L4 only with UDP ports, including the following fields:
108      *
109      *  - NXM_OF_IP_PROTO
110      *  - NXM_OF_IP_SRC / NXM_OF_IP_DST
111      *  - NXM_OF_SCTP_SRC / NXM_OF_SCTP_DST
112      *  - NXM_OF_TCP_SRC / NXM_OF_TCP_DST
113      *  - NXM_OF_UDP_SRC / NXM_OF_UDP_DST
114      */
115     NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP
116
117
118 };
119
120 /* This command enables or disables an Open vSwitch extension that allows a
121  * controller to specify the OpenFlow table to which a flow should be added,
122  * instead of having the switch decide which table is most appropriate as
123  * required by OpenFlow 1.0.  Because NXM was designed as an extension to
124  * OpenFlow 1.0, the extension applies equally to ofp10_flow_mod and
125  * nx_flow_mod.  By default, the extension is disabled.
126  *
127  * When this feature is enabled, Open vSwitch treats struct ofp10_flow_mod's
128  * and struct nx_flow_mod's 16-bit 'command' member as two separate fields.
129  * The upper 8 bits are used as the table ID, the lower 8 bits specify the
130  * command as usual.  A table ID of 0xff is treated like a wildcarded table ID.
131  *
132  * The specific treatment of the table ID depends on the type of flow mod:
133  *
134  *    - OFPFC_ADD: Given a specific table ID, the flow is always placed in that
135  *      table.  If an identical flow already exists in that table only, then it
136  *      is replaced.  If the flow cannot be placed in the specified table,
137  *      either because the table is full or because the table cannot support
138  *      flows of the given type, the switch replies with an OFPFMFC_TABLE_FULL
139  *      error.  (A controller can distinguish these cases by comparing the
140  *      current and maximum number of entries reported in ofp_table_stats.)
141  *
142  *      If the table ID is wildcarded, the switch picks an appropriate table
143  *      itself.  If an identical flow already exist in the selected flow table,
144  *      then it is replaced.  The choice of table might depend on the flows
145  *      that are already in the switch; for example, if one table fills up then
146  *      the switch might fall back to another one.
147  *
148  *    - OFPFC_MODIFY, OFPFC_DELETE: Given a specific table ID, only flows
149  *      within that table are matched and modified or deleted.  If the table ID
150  *      is wildcarded, flows within any table may be matched and modified or
151  *      deleted.
152  *
153  *    - OFPFC_MODIFY_STRICT, OFPFC_DELETE_STRICT: Given a specific table ID,
154  *      only a flow within that table may be matched and modified or deleted.
155  *      If the table ID is wildcarded and exactly one flow within any table
156  *      matches, then it is modified or deleted; if flows in more than one
157  *      table match, then none is modified or deleted.
158  */
159 struct nx_flow_mod_table_id {
160     uint8_t set;                /* Nonzero to enable, zero to disable. */
161     uint8_t pad[7];
162 };
163 OFP_ASSERT(sizeof(struct nx_flow_mod_table_id) == 8);
164
165 enum nx_packet_in_format {
166     NXPIF_OPENFLOW10 = 0,       /* Standard OpenFlow 1.0 compatible. */
167     NXPIF_NXM = 1               /* Nicira Extended. */
168 };
169
170 /* NXT_SET_PACKET_IN_FORMAT request. */
171 struct nx_set_packet_in_format {
172     ovs_be32 format;            /* One of NXPIF_*. */
173 };
174 OFP_ASSERT(sizeof(struct nx_set_packet_in_format) == 4);
175
176 /* NXT_PACKET_IN (analogous to OFPT_PACKET_IN).
177  *
178  * NXT_PACKET_IN is similar to the OpenFlow 1.2 OFPT_PACKET_IN.  The
179  * differences are:
180  *
181  *     - NXT_PACKET_IN includes the cookie of the rule that triggered the
182  *       message.  (OpenFlow 1.3 OFPT_PACKET_IN also includes the cookie.)
183  *
184  *     - The metadata fields use NXM (instead of OXM) field numbers.
185  *
186  * Open vSwitch 1.9.0 and later omits metadata fields that are zero (as allowed
187  * by OpenFlow 1.2).  Earlier versions included all implemented metadata
188  * fields.
189  *
190  * Open vSwitch does not include non-metadata in the nx_match, because by
191  * definition that information can be found in the packet itself.  The format
192  * and the standards allow this, however, so controllers should be prepared to
193  * tolerate future changes.
194  *
195  * The NXM format is convenient for reporting metadata values, but it is
196  * important not to interpret the format as matching against a flow, because it
197  * does not.  Nothing is being matched; arbitrary metadata masks would not be
198  * meaningful.
199  *
200  * Whereas in most cases a controller can expect to only get back NXM fields
201  * that it set up itself (e.g. flow dumps will ordinarily report only NXM
202  * fields from flows that the controller added), NXT_PACKET_IN messages might
203  * contain fields that the controller does not understand, because the switch
204  * might support fields (new registers, new protocols, etc.) that the
205  * controller does not.  The controller must prepared to tolerate these.
206  *
207  * The 'cookie' field has no meaning when 'reason' is OFPR_NO_MATCH.  In this
208  * case it should be UINT64_MAX. */
209 struct nx_packet_in {
210     ovs_be32 buffer_id;       /* ID assigned by datapath. */
211     ovs_be16 total_len;       /* Full length of frame. */
212     uint8_t reason;           /* Reason packet is sent (one of OFPR_*). */
213     uint8_t table_id;         /* ID of the table that was looked up. */
214     ovs_be64 cookie;          /* Cookie of the rule that was looked up. */
215     ovs_be16 match_len;       /* Size of nx_match. */
216     uint8_t pad[6];           /* Align to 64-bits. */
217     /* Followed by:
218      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
219      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
220      *     all-zero bytes, then
221      *   - Exactly 2 all-zero padding bytes, then
222      *   - An Ethernet frame whose length is inferred from nxh.header.length.
223      *
224      * The padding bytes preceding the Ethernet frame ensure that the IP
225      * header (if any) following the Ethernet header is 32-bit aligned. */
226
227     /* uint8_t nxm_fields[...]; */ /* NXM headers. */
228     /* uint8_t pad[2]; */          /* Align to 64 bit + 16 bit. */
229     /* uint8_t data[0]; */         /* Ethernet frame. */
230 };
231 OFP_ASSERT(sizeof(struct nx_packet_in) == 24);
232
233 /* Configures the "role" of the sending controller.  The default role is:
234  *
235  *    - Other (NX_ROLE_OTHER), which allows the controller access to all
236  *      OpenFlow features.
237  *
238  * The other possible roles are a related pair:
239  *
240  *    - Master (NX_ROLE_MASTER) is equivalent to Other, except that there may
241  *      be at most one Master controller at a time: when a controller
242  *      configures itself as Master, any existing Master is demoted to the
243  *      Slave role.
244  *
245  *    - Slave (NX_ROLE_SLAVE) allows the controller read-only access to
246  *      OpenFlow features.  In particular attempts to modify the flow table
247  *      will be rejected with an OFPBRC_EPERM error.
248  *
249  *      Slave controllers do not receive OFPT_PACKET_IN or OFPT_FLOW_REMOVED
250  *      messages, but they do receive OFPT_PORT_STATUS messages.
251  */
252 struct nx_role_request {
253     ovs_be32 role;              /* One of NX_ROLE_*. */
254 };
255 OFP_ASSERT(sizeof(struct nx_role_request) == 4);
256
257 enum nx_role {
258     NX_ROLE_OTHER,              /* Default role, full access. */
259     NX_ROLE_MASTER,             /* Full access, at most one. */
260     NX_ROLE_SLAVE               /* Read-only access. */
261 };
262
263 /* NXT_SET_ASYNC_CONFIG.
264  *
265  * Sent by a controller, this message configures the asynchronous messages that
266  * the controller wants to receive.  Element 0 in each array specifies messages
267  * of interest when the controller has an "other" or "master" role; element 1,
268  * when the controller has a "slave" role.
269  *
270  * Each array element is a bitmask in which a 0-bit disables receiving a
271  * particular message and a 1-bit enables receiving it.  Each bit controls the
272  * message whose 'reason' corresponds to the bit index.  For example, the bit
273  * with value 1<<2 == 4 in port_status_mask[1] determines whether the
274  * controller will receive OFPT_PORT_STATUS messages with reason OFPPR_MODIFY
275  * (value 2) when the controller has a "slave" role.
276  *
277  * As a side effect, for service controllers, this message changes the
278  * miss_send_len from default of zero to OFP_DEFAULT_MISS_SEND_LEN (128).
279  */
280 struct nx_async_config {
281     ovs_be32 packet_in_mask[2];    /* Bitmasks of OFPR_* values. */
282     ovs_be32 port_status_mask[2];  /* Bitmasks of OFPRR_* values. */
283     ovs_be32 flow_removed_mask[2]; /* Bitmasks of OFPPR_* values. */
284 };
285 OFP_ASSERT(sizeof(struct nx_async_config) == 24);
286 \f
287 /* Flexible flow specifications (aka NXM = Nicira Extended Match).
288  *
289  * OpenFlow 1.0 has "struct ofp10_match" for specifying flow matches.  This
290  * structure is fixed-length and hence difficult to extend.  This section
291  * describes a more flexible, variable-length flow match, called "nx_match" for
292  * short, that is also supported by Open vSwitch.  This section also defines a
293  * replacement for each OpenFlow message that includes struct ofp10_match.
294  *
295  * OpenFlow 1.2+ introduced OpenFlow Extensible Match (OXM), adapting
296  * the design of NXM.  The format of NXM and OXM are compatible.
297  *
298  *
299  * Format
300  * ======
301  *
302  * An nx_match is a sequence of zero or more "nxm_entry"s, which are
303  * type-length-value (TLV) entries, each 5 to 259 (inclusive) bytes long.
304  * "nxm_entry"s are not aligned on or padded to any multibyte boundary.  The
305  * first 4 bytes of an nxm_entry are its "header", followed by the entry's
306  * "body".
307  *
308  * An nxm_entry's header is interpreted as a 32-bit word in network byte order:
309  *
310  * |<-------------------- nxm_type ------------------>|
311  * |                                                  |
312  * |31                              16 15            9| 8 7                0
313  * +----------------------------------+---------------+--+------------------+
314  * |            nxm_vendor            |   nxm_field   |hm|    nxm_length    |
315  * +----------------------------------+---------------+--+------------------+
316  *
317  * The most-significant 23 bits of the header are collectively "nxm_type".
318  * Bits 16...31 are "nxm_vendor", one of OFPXMC12_* values.  In case of
319  * NXM, it's either OFPXMC12_NXM_0 or OFPXMC12_NXM_1.
320  * Bits 9...15 are "nxm_field", which is a vendor-specific value.  nxm_type
321  * normally designates a protocol header, such as the Ethernet type, but it
322  * can also refer to packet metadata, such as the switch port on which a packet
323  * arrived.
324  *
325  * Bit 8 is "nxm_hasmask" (labeled "hm" above for space reasons).  The meaning
326  * of this bit is explained later.
327  *
328  * The least-significant 8 bits are "nxm_length", a positive integer.  The
329  * length of the nxm_entry, including the header, is exactly 4 + nxm_length
330  * bytes.
331  *
332  * For a given nxm_vendor, nxm_field, and nxm_hasmask value, nxm_length is a
333  * constant.  It is included only to allow software to minimally parse
334  * "nxm_entry"s of unknown types.  (Similarly, for a given nxm_vendor,
335  * nxm_field, and nxm_length, nxm_hasmask is a constant.)
336  *
337  *
338  * Semantics
339  * =========
340  *
341  * A zero-length nx_match (one with no "nxm_entry"s) matches every packet.
342  *
343  * An nxm_entry places a constraint on the packets matched by the nx_match:
344  *
345  *   - If nxm_hasmask is 0, the nxm_entry's body contains a value for the
346  *     field, called "nxm_value".  The nx_match matches only packets in which
347  *     the field equals nxm_value.
348  *
349  *   - If nxm_hasmask is 1, then the nxm_entry's body contains a value for the
350  *     field (nxm_value), followed by a bitmask of the same length as the
351  *     value, called "nxm_mask".  For each 1-bit in position J in nxm_mask, the
352  *     nx_match matches only packets for which bit J in the given field's value
353  *     matches bit J in nxm_value.  A 0-bit in nxm_mask causes the
354  *     corresponding bit in nxm_value is ignored (it should be 0; Open vSwitch
355  *     may enforce this someday), as is the corresponding bit in the field's
356  *     value.  (The sense of the nxm_mask bits is the opposite of that used by
357  *     the "wildcards" member of struct ofp10_match.)
358  *
359  *     When nxm_hasmask is 1, nxm_length is always even.
360  *
361  *     An all-zero-bits nxm_mask is equivalent to omitting the nxm_entry
362  *     entirely.  An all-one-bits nxm_mask is equivalent to specifying 0 for
363  *     nxm_hasmask.
364  *
365  * When there are multiple "nxm_entry"s, all of the constraints must be met.
366  *
367  *
368  * Mask Restrictions
369  * =================
370  *
371  * Masks may be restricted:
372  *
373  *   - Some nxm_types may not support masked wildcards, that is, nxm_hasmask
374  *     must always be 0 when these fields are specified.  For example, the
375  *     field that identifies the port on which a packet was received may not be
376  *     masked.
377  *
378  *   - Some nxm_types that do support masked wildcards may only support certain
379  *     nxm_mask patterns.  For example, fields that have IPv4 address values
380  *     may be restricted to CIDR masks.
381  *
382  * These restrictions should be noted in specifications for individual fields.
383  * A switch may accept an nxm_hasmask or nxm_mask value that the specification
384  * disallows, if the switch correctly implements support for that nxm_hasmask
385  * or nxm_mask value.  A switch must reject an attempt to set up a flow that
386  * contains a nxm_hasmask or nxm_mask value that it does not support.
387  *
388  *
389  * Prerequisite Restrictions
390  * =========================
391  *
392  * The presence of an nxm_entry with a given nxm_type may be restricted based
393  * on the presence of or values of other "nxm_entry"s.  For example:
394  *
395  *   - An nxm_entry for nxm_type=NXM_OF_IP_TOS is allowed only if it is
396  *     preceded by another entry with nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0,
397  *     and nxm_value=0x0800.  That is, matching on the IP source address is
398  *     allowed only if the Ethernet type is explicitly set to IP.
399  *
400  *   - An nxm_entry for nxm_type=NXM_OF_TCP_SRC is allowed only if it is
401  *     preceded by an entry with nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and
402  *     nxm_value either 0x0800 or 0x86dd, and another with
403  *     nxm_type=NXM_OF_IP_PROTO, nxm_hasmask=0, nxm_value=6, in that order.
404  *     That is, matching on the TCP source port is allowed only if the Ethernet
405  *     type is IP or IPv6 and the IP protocol is TCP.
406  *
407  * These restrictions should be noted in specifications for individual fields.
408  * A switch may implement relaxed versions of these restrictions.  A switch
409  * must reject an attempt to set up a flow that violates its restrictions.
410  *
411  *
412  * Ordering Restrictions
413  * =====================
414  *
415  * An nxm_entry that has prerequisite restrictions must appear after the
416  * "nxm_entry"s for its prerequisites.  Ordering of "nxm_entry"s within an
417  * nx_match is not otherwise constrained.
418  *
419  * Any given nxm_type may appear in an nx_match at most once.
420  *
421  *
422  * nxm_entry Examples
423  * ==================
424  *
425  * These examples show the format of a single nxm_entry with particular
426  * nxm_hasmask and nxm_length values.  The diagrams are labeled with field
427  * numbers and byte indexes.
428  *
429  *
430  * 8-bit nxm_value, nxm_hasmask=1, nxm_length=2:
431  *
432  *  0          3  4   5
433  * +------------+---+---+
434  * |   header   | v | m |
435  * +------------+---+---+
436  *
437  *
438  * 16-bit nxm_value, nxm_hasmask=0, nxm_length=2:
439  *
440  *  0          3 4    5
441  * +------------+------+
442  * |   header   | value|
443  * +------------+------+
444  *
445  *
446  * 32-bit nxm_value, nxm_hasmask=0, nxm_length=4:
447  *
448  *  0          3 4           7
449  * +------------+-------------+
450  * |   header   |  nxm_value  |
451  * +------------+-------------+
452  *
453  *
454  * 48-bit nxm_value, nxm_hasmask=0, nxm_length=6:
455  *
456  *  0          3 4                9
457  * +------------+------------------+
458  * |   header   |     nxm_value    |
459  * +------------+------------------+
460  *
461  *
462  * 48-bit nxm_value, nxm_hasmask=1, nxm_length=12:
463  *
464  *  0          3 4                9 10              15
465  * +------------+------------------+------------------+
466  * |   header   |     nxm_value    |      nxm_mask    |
467  * +------------+------------------+------------------+
468  *
469  *
470  * Error Reporting
471  * ===============
472  *
473  * A switch should report an error in an nx_match using error type
474  * OFPET_BAD_REQUEST and one of the NXBRC_NXM_* codes.  Ideally the switch
475  * should report a specific error code, if one is assigned for the particular
476  * problem, but NXBRC_NXM_INVALID is also available to report a generic
477  * nx_match error.
478  */
479
480 /* Number of registers allocated NXM field IDs. */
481 #define NXM_NX_MAX_REGS 16
482
483 /* Bits in the value of NXM_NX_IP_FRAG. */
484 #define NX_IP_FRAG_ANY   (1 << 0) /* Is this a fragment? */
485 #define NX_IP_FRAG_LATER (1 << 1) /* Is this a fragment with nonzero offset? */
486
487 /* Bits in the value of NXM_NX_TUN_FLAGS. */
488 #define NX_TUN_FLAG_OAM  (1 << 0) /* Is this an OAM packet? */
489
490 /* ## --------------------- ## */
491 /* ## Requests and replies. ## */
492 /* ## --------------------- ## */
493
494 enum nx_flow_format {
495     NXFF_OPENFLOW10 = 0,         /* Standard OpenFlow 1.0 compatible. */
496     NXFF_NXM = 2                 /* Nicira extended match. */
497 };
498
499 /* NXT_SET_FLOW_FORMAT request. */
500 struct nx_set_flow_format {
501     ovs_be32 format;            /* One of NXFF_*. */
502 };
503 OFP_ASSERT(sizeof(struct nx_set_flow_format) == 4);
504
505 /* NXT_FLOW_MOD (analogous to OFPT_FLOW_MOD).
506  *
507  * It is possible to limit flow deletions and modifications to certain
508  * cookies by using the NXM_NX_COOKIE(_W) matches.  The "cookie" field
509  * is used only to add or modify flow cookies.
510  */
511 struct nx_flow_mod {
512     ovs_be64 cookie;              /* Opaque controller-issued identifier. */
513     ovs_be16 command;             /* OFPFC_* + possibly a table ID (see comment
514                                    * on struct nx_flow_mod_table_id). */
515     ovs_be16 idle_timeout;        /* Idle time before discarding (seconds). */
516     ovs_be16 hard_timeout;        /* Max time before discarding (seconds). */
517     ovs_be16 priority;            /* Priority level of flow entry. */
518     ovs_be32 buffer_id;           /* Buffered packet to apply to (or -1).
519                                      Not meaningful for OFPFC_DELETE*. */
520     ovs_be16 out_port;            /* For OFPFC_DELETE* commands, require
521                                      matching entries to include this as an
522                                      output port.  A value of OFPP_NONE
523                                      indicates no restriction. */
524     ovs_be16 flags;               /* One of OFPFF_*. */
525     ovs_be16 match_len;           /* Size of nx_match. */
526     uint8_t pad[6];               /* Align to 64-bits. */
527     /* Followed by:
528      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
529      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
530      *     all-zero bytes, then
531      *   - Actions to fill out the remainder of the message length (always a
532      *     multiple of 8).
533      */
534 };
535 OFP_ASSERT(sizeof(struct nx_flow_mod) == 32);
536
537 /* NXT_FLOW_REMOVED (analogous to OFPT_FLOW_REMOVED).
538  *
539  * 'table_id' is present only in Open vSwitch 1.11 and later.  In earlier
540  * versions of Open vSwitch, this is a padding byte that is always zeroed.
541  * Therefore, a 'table_id' value of 0 indicates that the table ID is not known,
542  * and other values may be interpreted as one more than the flow's former table
543  * ID. */
544 struct nx_flow_removed {
545     ovs_be64 cookie;          /* Opaque controller-issued identifier. */
546     ovs_be16 priority;        /* Priority level of flow entry. */
547     uint8_t reason;           /* One of OFPRR_*. */
548     uint8_t table_id;         /* Flow's former table ID, plus one. */
549     ovs_be32 duration_sec;    /* Time flow was alive in seconds. */
550     ovs_be32 duration_nsec;   /* Time flow was alive in nanoseconds beyond
551                                  duration_sec. */
552     ovs_be16 idle_timeout;    /* Idle timeout from original flow mod. */
553     ovs_be16 match_len;       /* Size of nx_match. */
554     ovs_be64 packet_count;
555     ovs_be64 byte_count;
556     /* Followed by:
557      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
558      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
559      *     all-zero bytes. */
560 };
561 OFP_ASSERT(sizeof(struct nx_flow_removed) == 40);
562
563 /* Nicira vendor stats request of type NXST_FLOW (analogous to OFPST_FLOW
564  * request).
565  *
566  * It is possible to limit matches to certain cookies by using the
567  * NXM_NX_COOKIE and NXM_NX_COOKIE_W matches.
568  */
569 struct nx_flow_stats_request {
570     ovs_be16 out_port;        /* Require matching entries to include this
571                                  as an output port.  A value of OFPP_NONE
572                                  indicates no restriction. */
573     ovs_be16 match_len;       /* Length of nx_match. */
574     uint8_t table_id;         /* ID of table to read (from ofp_table_stats)
575                                  or 0xff for all tables. */
576     uint8_t pad[3];           /* Align to 64 bits. */
577     /* Followed by:
578      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
579      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
580      *     all-zero bytes, which must also exactly fill out the length of the
581      *     message.
582      */
583 };
584 OFP_ASSERT(sizeof(struct nx_flow_stats_request) == 8);
585
586 /* Body for Nicira vendor stats reply of type NXST_FLOW (analogous to
587  * OFPST_FLOW reply).
588  *
589  * The values of 'idle_age' and 'hard_age' are only meaningful when talking to
590  * a switch that implements the NXT_FLOW_AGE extension.  Zero means that the
591  * true value is unknown, perhaps because hardware does not track the value.
592  * (Zero is also the value that one should ordinarily expect to see talking to
593  * a switch that does not implement NXT_FLOW_AGE, since those switches zero the
594  * padding bytes that these fields replaced.)  A nonzero value X represents X-1
595  * seconds.  A value of 65535 represents 65534 or more seconds.
596  *
597  * 'idle_age' is the number of seconds that the flow has been idle, that is,
598  * the number of seconds since a packet passed through the flow.  'hard_age' is
599  * the number of seconds since the flow was last modified (e.g. OFPFC_MODIFY or
600  * OFPFC_MODIFY_STRICT).  (The 'duration_*' fields are the elapsed time since
601  * the flow was added, regardless of subsequent modifications.)
602  *
603  * For a flow with an idle or hard timeout, 'idle_age' or 'hard_age',
604  * respectively, will ordinarily be smaller than the timeout, but flow
605  * expiration times are only approximate and so one must be prepared to
606  * tolerate expirations that occur somewhat early or late.
607  */
608 struct nx_flow_stats {
609     ovs_be16 length;          /* Length of this entry. */
610     uint8_t table_id;         /* ID of table flow came from. */
611     uint8_t pad;
612     ovs_be32 duration_sec;    /* Time flow has been alive in seconds. */
613     ovs_be32 duration_nsec;   /* Time flow has been alive in nanoseconds
614                                  beyond duration_sec. */
615     ovs_be16 priority;        /* Priority of the entry. */
616     ovs_be16 idle_timeout;    /* Number of seconds idle before expiration. */
617     ovs_be16 hard_timeout;    /* Number of seconds before expiration. */
618     ovs_be16 match_len;       /* Length of nx_match. */
619     ovs_be16 idle_age;        /* Seconds since last packet, plus one. */
620     ovs_be16 hard_age;        /* Seconds since last modification, plus one. */
621     ovs_be64 cookie;          /* Opaque controller-issued identifier. */
622     ovs_be64 packet_count;    /* Number of packets, UINT64_MAX if unknown. */
623     ovs_be64 byte_count;      /* Number of bytes, UINT64_MAX if unknown. */
624     /* Followed by:
625      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
626      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
627      *     all-zero bytes, then
628      *   - Actions to fill out the remainder 'length' bytes (always a multiple
629      *     of 8).
630      */
631 };
632 OFP_ASSERT(sizeof(struct nx_flow_stats) == 48);
633
634 /* Nicira vendor stats request of type NXST_AGGREGATE (analogous to
635  * OFPST_AGGREGATE request).
636  *
637  * The reply format is identical to the reply format for OFPST_AGGREGATE,
638  * except for the header. */
639 struct nx_aggregate_stats_request {
640     ovs_be16 out_port;        /* Require matching entries to include this
641                                  as an output port.  A value of OFPP_NONE
642                                  indicates no restriction. */
643     ovs_be16 match_len;       /* Length of nx_match. */
644     uint8_t table_id;         /* ID of table to read (from ofp_table_stats)
645                                  or 0xff for all tables. */
646     uint8_t pad[3];           /* Align to 64 bits. */
647     /* Followed by:
648      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
649      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
650      *     all-zero bytes, which must also exactly fill out the length of the
651      *     message.
652      */
653 };
654 OFP_ASSERT(sizeof(struct nx_aggregate_stats_request) == 8);
655 \f
656 /* NXT_SET_CONTROLLER_ID.
657  *
658  * Each OpenFlow controller connection has a 16-bit identifier that is
659  * initially 0.  This message changes the connection's ID to 'id'.
660  *
661  * Controller connection IDs need not be unique.
662  *
663  * The NXAST_CONTROLLER action is the only current user of controller
664  * connection IDs. */
665 struct nx_controller_id {
666     uint8_t zero[6];            /* Must be zero. */
667     ovs_be16 controller_id;     /* New controller connection ID. */
668 };
669 OFP_ASSERT(sizeof(struct nx_controller_id) == 8);
670 \f
671 /* Flow Table Monitoring
672  * =====================
673  *
674  * NXST_FLOW_MONITOR allows a controller to keep track of changes to OpenFlow
675  * flow table(s) or subsets of them, with the following workflow:
676  *
677  * 1. The controller sends an NXST_FLOW_MONITOR request to begin monitoring
678  *    flows.  The 'id' in the request must be unique among all monitors that
679  *    the controller has started and not yet canceled on this OpenFlow
680  *    connection.
681  *
682  * 2. The switch responds with an NXST_FLOW_MONITOR reply.  If the request's
683  *    'flags' included NXFMF_INITIAL, the reply includes all the flows that
684  *    matched the request at the time of the request (with event NXFME_ADDED).
685  *    If 'flags' did not include NXFMF_INITIAL, the reply is empty.
686  *
687  *    The reply uses the xid of the request (as do all replies to OpenFlow
688  *    requests).
689  *
690  * 3. Whenever a change to a flow table entry matches some outstanding monitor
691  *    request's criteria and flags, the switch sends a notification to the
692  *    controller as an additional NXST_FLOW_MONITOR reply with xid 0.
693  *
694  *    When multiple outstanding monitors match a single change, only a single
695  *    notification is sent.  This merged notification includes the information
696  *    requested in any of the individual monitors.  That is, if any of the
697  *    matching monitors requests actions (NXFMF_ACTIONS), the notification
698  *    includes actions, and if any of the monitors request full changes for the
699  *    controller's own changes (NXFMF_OWN), the controller's own changes will
700  *    be included in full.
701  *
702  * 4. The controller may cancel a monitor with NXT_FLOW_MONITOR_CANCEL.  No
703  *    further notifications will be sent on the basis of the canceled monitor
704  *    afterward.
705  *
706  *
707  * Buffer Management
708  * =================
709  *
710  * OpenFlow messages for flow monitor notifications can overflow the buffer
711  * space available to the switch, either temporarily (e.g. due to network
712  * conditions slowing OpenFlow traffic) or more permanently (e.g. the sustained
713  * rate of flow table change exceeds the network bandwidth between switch and
714  * controller).
715  *
716  * When Open vSwitch's notification buffer space reaches a limiting threshold,
717  * OVS reacts as follows:
718  *
719  * 1. OVS sends an NXT_FLOW_MONITOR_PAUSED message to the controller, following
720  *    all the already queued notifications.  After it receives this message,
721  *    the controller knows that its view of the flow table, as represented by
722  *    flow monitor notifications, is incomplete.
723  *
724  * 2. As long as the notification buffer is not empty:
725  *
726  *        - NXMFE_ADD and NXFME_MODIFIED notifications will not be sent.
727  *
728  *        - NXFME_DELETED notifications will still be sent, but only for flows
729  *          that existed before OVS sent NXT_FLOW_MONITOR_PAUSED.
730  *
731  *        - NXFME_ABBREV notifications will not be sent.  They are treated as
732  *          the expanded version (and therefore only the NXFME_DELETED
733  *          components, if any, are sent).
734  *
735  * 3. When the notification buffer empties, OVS sends NXFME_ADD notifications
736  *    for flows added since the buffer reached its limit and NXFME_MODIFIED
737  *    notifications for flows that existed before the limit was reached and
738  *    changed after the limit was reached.
739  *
740  * 4. OVS sends an NXT_FLOW_MONITOR_RESUMED message to the controller.  After
741  *    it receives this message, the controller knows that its view of the flow
742  *    table, as represented by flow monitor notifications, is again complete.
743  *
744  * This allows the maximum buffer space requirement for notifications to be
745  * bounded by the limit plus the maximum number of supported flows.
746  *
747  *
748  * "Flow Removed" messages
749  * =======================
750  *
751  * The flow monitor mechanism is independent of OFPT_FLOW_REMOVED and
752  * NXT_FLOW_REMOVED.  Flow monitor updates for deletion are sent if
753  * NXFMF_DELETE is set on a monitor, regardless of whether the
754  * OFPFF_SEND_FLOW_REM flag was set when the flow was added. */
755
756 /* NXST_FLOW_MONITOR request.
757  *
758  * The NXST_FLOW_MONITOR request's body consists of an array of zero or more
759  * instances of this structure.  The request arranges to monitor the flows
760  * that match the specified criteria, which are interpreted in the same way as
761  * for NXST_FLOW.
762  *
763  * 'id' identifies a particular monitor for the purpose of allowing it to be
764  * canceled later with NXT_FLOW_MONITOR_CANCEL.  'id' must be unique among
765  * existing monitors that have not already been canceled.
766  *
767  * The reply includes the initial flow matches for monitors that have the
768  * NXFMF_INITIAL flag set.  No single flow will be included in the reply more
769  * than once, even if more than one requested monitor matches that flow.  The
770  * reply will be empty if none of the monitors has NXFMF_INITIAL set or if none
771  * of the monitors initially matches any flows.
772  *
773  * For NXFMF_ADD, an event will be reported if 'out_port' matches against the
774  * actions of the flow being added or, for a flow that is replacing an existing
775  * flow, if 'out_port' matches against the actions of the flow being replaced.
776  * For NXFMF_DELETE, 'out_port' matches against the actions of a flow being
777  * deleted.  For NXFMF_MODIFY, an event will be reported if 'out_port' matches
778  * either the old or the new actions. */
779 struct nx_flow_monitor_request {
780     ovs_be32 id;                /* Controller-assigned ID for this monitor. */
781     ovs_be16 flags;             /* NXFMF_*. */
782     ovs_be16 out_port;          /* Required output port, if not OFPP_NONE. */
783     ovs_be16 match_len;         /* Length of nx_match. */
784     uint8_t table_id;           /* One table's ID or 0xff for all tables. */
785     uint8_t zeros[5];           /* Align to 64 bits (must be zero). */
786     /* Followed by:
787      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
788      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
789      *     all-zero bytes. */
790 };
791 OFP_ASSERT(sizeof(struct nx_flow_monitor_request) == 16);
792
793 /* 'flags' bits in struct nx_flow_monitor_request. */
794 enum nx_flow_monitor_flags {
795     /* When to send updates. */
796     NXFMF_INITIAL = 1 << 0,     /* Initially matching flows. */
797     NXFMF_ADD = 1 << 1,         /* New matching flows as they are added. */
798     NXFMF_DELETE = 1 << 2,      /* Old matching flows as they are removed. */
799     NXFMF_MODIFY = 1 << 3,      /* Matching flows as they are changed. */
800
801     /* What to include in updates. */
802     NXFMF_ACTIONS = 1 << 4,     /* If set, actions are included. */
803     NXFMF_OWN = 1 << 5,         /* If set, include own changes in full. */
804 };
805
806 /* NXST_FLOW_MONITOR reply header.
807  *
808  * The body of an NXST_FLOW_MONITOR reply is an array of variable-length
809  * structures, each of which begins with this header.  The 'length' member may
810  * be used to traverse the array, and the 'event' member may be used to
811  * determine the particular structure.
812  *
813  * Every instance is a multiple of 8 bytes long. */
814 struct nx_flow_update_header {
815     ovs_be16 length;            /* Length of this entry. */
816     ovs_be16 event;             /* One of NXFME_*. */
817     /* ...other data depending on 'event'... */
818 };
819 OFP_ASSERT(sizeof(struct nx_flow_update_header) == 4);
820
821 /* 'event' values in struct nx_flow_update_header. */
822 enum nx_flow_update_event {
823     /* struct nx_flow_update_full. */
824     NXFME_ADDED = 0,            /* Flow was added. */
825     NXFME_DELETED = 1,          /* Flow was deleted. */
826     NXFME_MODIFIED = 2,         /* Flow (generally its actions) was changed. */
827
828     /* struct nx_flow_update_abbrev. */
829     NXFME_ABBREV = 3,           /* Abbreviated reply. */
830 };
831
832 /* NXST_FLOW_MONITOR reply for NXFME_ADDED, NXFME_DELETED, and
833  * NXFME_MODIFIED. */
834 struct nx_flow_update_full {
835     ovs_be16 length;            /* Length is 24. */
836     ovs_be16 event;             /* One of NXFME_*. */
837     ovs_be16 reason;            /* OFPRR_* for NXFME_DELETED, else zero. */
838     ovs_be16 priority;          /* Priority of the entry. */
839     ovs_be16 idle_timeout;      /* Number of seconds idle before expiration. */
840     ovs_be16 hard_timeout;      /* Number of seconds before expiration. */
841     ovs_be16 match_len;         /* Length of nx_match. */
842     uint8_t table_id;           /* ID of flow's table. */
843     uint8_t pad;                /* Reserved, currently zeroed. */
844     ovs_be64 cookie;            /* Opaque controller-issued identifier. */
845     /* Followed by:
846      *   - Exactly match_len (possibly 0) bytes containing the nx_match, then
847      *   - Exactly (match_len + 7)/8*8 - match_len (between 0 and 7) bytes of
848      *     all-zero bytes, then
849      *   - Actions to fill out the remainder 'length' bytes (always a multiple
850      *     of 8).  If NXFMF_ACTIONS was not specified, or 'event' is
851      *     NXFME_DELETED, no actions are included.
852      */
853 };
854 OFP_ASSERT(sizeof(struct nx_flow_update_full) == 24);
855
856 /* NXST_FLOW_MONITOR reply for NXFME_ABBREV.
857  *
858  * When the controller does not specify NXFMF_OWN in a monitor request, any
859  * flow tables changes due to the controller's own requests (on the same
860  * OpenFlow channel) will be abbreviated, when possible, to this form, which
861  * simply specifies the 'xid' of the OpenFlow request (e.g. an OFPT_FLOW_MOD or
862  * NXT_FLOW_MOD) that caused the change.
863  *
864  * Some changes cannot be abbreviated and will be sent in full:
865  *
866  *   - Changes that only partially succeed.  This can happen if, for example,
867  *     a flow_mod with type OFPFC_MODIFY affects multiple flows, but only some
868  *     of those modifications succeed (e.g. due to hardware limitations).
869  *
870  *     This cannot occur with the Open vSwitch software datapath.  This also
871  *     cannot occur in Open vSwitch 2.4 and later, because these versions only
872  *     execute any flow modifications if all of them will succeed.
873  *
874  *   - Changes that race with conflicting changes made by other controllers or
875  *     other flow_mods (not separated by barriers) by the same controller.
876  *
877  *     This cannot occur with the current Open vSwitch implementation
878  *     (regardless of datapath) because Open vSwitch internally serializes
879  *     potentially conflicting changes.
880  *
881  *   - Changes that occur when flow notification is paused (see "Buffer
882  *     Management" above).
883  *
884  * A flow_mod that does not change the flow table will not trigger any
885  * notification, even an abbreviated one.  For example, a "modify" or "delete"
886  * flow_mod that does not match any flows will not trigger a notification.
887  * Whether an "add" or "modify" that specifies all the same parameters that a
888  * flow already has triggers a notification is unspecified and subject to
889  * change in future versions of Open vSwitch.
890  *
891  * OVS will always send the notifications for a given flow table change before
892  * the reply to a OFPT_BARRIER_REQUEST request that follows the flow table
893  * change.  Thus, if the controller does not receive an abbreviated (or
894  * unabbreviated) notification for a flow_mod before the next
895  * OFPT_BARRIER_REPLY, it will never receive one. */
896 struct nx_flow_update_abbrev {
897     ovs_be16 length;            /* Length is 8. */
898     ovs_be16 event;             /* NXFME_ABBREV. */
899     ovs_be32 xid;               /* Controller-specified xid from flow_mod. */
900 };
901 OFP_ASSERT(sizeof(struct nx_flow_update_abbrev) == 8);
902
903 /* NXT_FLOW_MONITOR_CANCEL.
904  *
905  * Used by a controller to cancel an outstanding monitor. */
906 struct nx_flow_monitor_cancel {
907     ovs_be32 id;                /* 'id' from nx_flow_monitor_request. */
908 };
909 OFP_ASSERT(sizeof(struct nx_flow_monitor_cancel) == 4);
910
911 /* Variable-length option TLV table maintenance commands.
912  *
913  * The option in Type-Length-Value format is widely used in tunnel options,
914  * e.g., the base Geneve header is followed by zero or more options in TLV
915  * format. Each option consists of a four byte option header and a variable
916  * amount of option data interpreted according to the type. The generic TLV
917  * format in tunnel options is as following:
918  *
919  * 0                   1                   2                   3
920  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
921  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
922  * |          Option Class         |      Type     |R|R|R| Length  |
923  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
924  * |                      Variable Option Data                     |
925  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
926  *
927  * In order to work with this variable-length options in TLV format in
928  * tunnel options, we need to maintain a mapping table between an option
929  * TLV (defined by <class, type, length>) and an NXM field that can be
930  * operated on for the purposes of matches, actions, etc. This mapping
931  * must be explicitly specified by the user.
932  *
933  * There are two primary groups of OpenFlow messages that are introduced
934  * as Nicira extensions: modification commands (add, delete, clear mappings)
935  * and table status request/reply to dump the current table along with switch
936  * information.
937  *
938  * Note that mappings should not be changed while they are in active use by
939  * a flow. The result of doing so is undefined. */
940
941 /* TLV table commands */
942 enum nx_tlv_table_mod_command {
943     NXTTMC_ADD,          /* New mappings (fails if an option is already
944                             mapped). */
945     NXTTMC_DELETE,       /* Delete mappings, identified by index
946                           * (unmapped options are ignored). */
947     NXTTMC_CLEAR,        /* Clear all mappings. Additional information
948                             in this command is ignored. */
949 };
950
951 /* Map between an option TLV and an NXM field. */
952 struct nx_tlv_map {
953     ovs_be16 option_class; /* TLV class. */
954     uint8_t  option_type;  /* TLV type. */
955     uint8_t  option_len;   /* TLV length (multiple of 4). */
956     ovs_be16 index;        /* NXM_NX_TUN_METADATA<n> index */
957     uint8_t  pad[2];
958 };
959 OFP_ASSERT(sizeof(struct nx_tlv_map) == 8);
960
961 /* NXT_TLV_TABLE_MOD.
962  *
963  * Use to configure a mapping between option TLVs (class, type, length)
964  * and NXM fields (NXM_NX_TUN_METADATA<n> where 'index' is <n>).
965  *
966  * This command is atomic: all operations on different options will
967  * either succeed or fail. */
968 struct nx_tlv_table_mod {
969     ovs_be16 command;           /* One of NTTTMC_* */
970     uint8_t pad[6];
971     /* struct nx_tlv_map[0]; Array of maps between indicies and option
972                                 TLVs. The number of elements is inferred
973                                 from the length field in the header. */
974 };
975 OFP_ASSERT(sizeof(struct nx_tlv_table_mod) == 8);
976
977 /* NXT_TLV_TABLE_REPLY.
978  *
979  * Issued in reponse to an NXT_TLV_TABLE_REQUEST to give information
980  * about the current status of the TLV table in the switch. Provides
981  * both static information about the switch's capabilities as well as
982  * the configured TLV table. */
983 struct nx_tlv_table_reply {
984     ovs_be32 max_option_space; /* Maximum total of option sizes supported. */
985     ovs_be16 max_fields;       /* Maximum number of match fields supported. */
986     uint8_t reserved[10];
987     /* struct nx_tlv_map[0]; Array of maps between indicies and option
988                                 TLVs. The number of elements is inferred
989                                 from the length field in the header. */
990 };
991 OFP_ASSERT(sizeof(struct nx_tlv_table_reply) == 16);
992
993 #endif /* openflow/nicira-ext.h */