Revert "conntrack: Add support for NAT."
[cascardo/ovs.git] / lib / ofp-actions.c
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 #include <config.h>
18 #include <netinet/in.h>
19
20 #include "ofp-actions.h"
21 #include "bundle.h"
22 #include "byte-order.h"
23 #include "compiler.h"
24 #include "dummy.h"
25 #include "dynamic-string.h"
26 #include "hmap.h"
27 #include "learn.h"
28 #include "meta-flow.h"
29 #include "multipath.h"
30 #include "nx-match.h"
31 #include "ofp-parse.h"
32 #include "ofp-util.h"
33 #include "ofpbuf.h"
34 #include "unaligned.h"
35 #include "util.h"
36 #include "openvswitch/vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(ofp_actions);
39
40 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
41
42 struct ofp_action_header;
43
44 /* Raw identifiers for OpenFlow actions.
45  *
46  * Decoding and encoding OpenFlow actions across multiple versions is difficult
47  * to do in a clean, consistent way.  This enumeration lays out all of the
48  * forms of actions that Open vSwitch supports.
49  *
50  * The comments here must follow a stylized form because the
51  * "extract-ofp-actions" program parses them at build time to generate data
52  * tables.
53  *
54  *   - The first part of each comment specifies the vendor, OpenFlow versions,
55  *     and type for each protocol that supports the action:
56  *
57  *         # The vendor is OF for standard OpenFlow actions, NX for Nicira
58  *           extension actions.  (Support for other vendors can be added, but
59  *           it can't be done just based on a vendor ID definition alone
60  *           because OpenFlow doesn't define a standard way to specify a
61  *           subtype for vendor actions, so other vendors might do it different
62  *           from Nicira.)
63  *
64  *         # The version can specify a specific OpenFlow version, a version
65  *           range delimited by "-", or an open-ended range with "+".
66  *
67  *         # The type, in parentheses, is the action type number (for standard
68  *           OpenFlow actions) or subtype (for vendor extension actions).
69  *
70  *         # Optionally one may add "is deprecated" followed by a
71  *           human-readable reason in parentheses (which will be used in log
72  *           messages), if a particular action should no longer be used.
73  *
74  *     Multiple such specifications may be separated by commas.
75  *
76  *   - The second part describes the action's wire format.  It may be:
77  *
78  *         # "struct <name>": The struct fully specifies the wire format.  The
79  *           action is exactly the size of the struct.  (Thus, the struct must
80  *           be an exact multiple of 8 bytes in size.)
81  *
82  *         # "struct <name>, ...": The struct specifies the beginning of the
83  *           wire format.  An instance of the action is either the struct's
84  *           exact size, or a multiple of 8 bytes longer.
85  *
86  *         # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
87  *           vendor extension) header, followed by 0 or more pad bytes to align
88  *           to a multiple of <N> bits, followed by an argument of the given
89  *           type, followed by 0 or more pad bytes to bring the total action up
90  *           to a multiple of 8 bytes.
91  *
92  *         # "void": The action is just a (standard or vendor extension)
93  *           header.
94  *
95  *   - Optional additional text enclosed in square brackets is commentary for
96  *     the human reader.
97  */
98 enum ofp_raw_action_type {
99 /* ## ----------------- ## */
100 /* ## Standard actions. ## */
101 /* ## ----------------- ## */
102
103     /* OF1.0(0): struct ofp10_action_output. */
104     OFPAT_RAW10_OUTPUT,
105     /* OF1.1+(0): struct ofp11_action_output. */
106     OFPAT_RAW11_OUTPUT,
107
108     /* OF1.0(1): uint16_t. */
109     OFPAT_RAW10_SET_VLAN_VID,
110     /* OF1.0(2): uint8_t. */
111     OFPAT_RAW10_SET_VLAN_PCP,
112
113     /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
114      *
115      * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
116      * modification actions: the 1.0 versions push a VLAN header if none is
117      * present, but the 1.1 versions do not.  That is the only reason that we
118      * distinguish their raw action types.] */
119     OFPAT_RAW11_SET_VLAN_VID,
120     /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
121     OFPAT_RAW11_SET_VLAN_PCP,
122
123     /* OF1.1+(17): ovs_be16.
124      *
125      * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
126      * TCI.] */
127     OFPAT_RAW11_PUSH_VLAN,
128
129     /* OF1.0(3): void. */
130     OFPAT_RAW10_STRIP_VLAN,
131     /* OF1.1+(18): void. */
132     OFPAT_RAW11_POP_VLAN,
133
134     /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
135      * ofp_action_dl_addr. */
136     OFPAT_RAW_SET_DL_SRC,
137
138     /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
139      * ofp_action_dl_addr. */
140     OFPAT_RAW_SET_DL_DST,
141
142     /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
143      * ovs_be32. */
144     OFPAT_RAW_SET_NW_SRC,
145
146     /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
147      * ovs_be32. */
148     OFPAT_RAW_SET_NW_DST,
149
150     /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
151     OFPAT_RAW_SET_NW_TOS,
152
153     /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
154     OFPAT_RAW11_SET_NW_ECN,
155
156     /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
157      * ovs_be16. */
158     OFPAT_RAW_SET_TP_SRC,
159
160     /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
161      * ovs_be16. */
162     OFPAT_RAW_SET_TP_DST,
163
164     /* OF1.0(11): struct ofp10_action_enqueue. */
165     OFPAT_RAW10_ENQUEUE,
166
167     /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
168      * ovs_be32. */
169     OFPAT_RAW_SET_MPLS_LABEL,
170
171     /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
172      * uint8_t. */
173     OFPAT_RAW_SET_MPLS_TC,
174
175     /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
176      * uint8_t. */
177     OFPAT_RAW_SET_MPLS_TTL,
178
179     /* NX1.0(26), OF1.1+(16): void. */
180     OFPAT_RAW_DEC_MPLS_TTL,
181
182     /* NX1.0(23), OF1.1+(19): ovs_be16.
183      *
184      * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
185     OFPAT_RAW_PUSH_MPLS,
186
187     /* NX1.0(24), OF1.1+(20): ovs_be16.
188      *
189      * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
190      * ETH_TYPE_MPLS otherwise, not the label.] */
191     OFPAT_RAW_POP_MPLS,
192
193     /* NX1.0(4), OF1.1+(21): uint32_t. */
194     OFPAT_RAW_SET_QUEUE,
195
196     /* OF1.1+(22): uint32_t. */
197     OFPAT_RAW11_GROUP,
198
199     /* OF1.1+(23): uint8_t. */
200     OFPAT_RAW11_SET_NW_TTL,
201
202     /* NX1.0(18), OF1.1+(24): void. */
203     OFPAT_RAW_DEC_NW_TTL,
204     /* NX1.0+(21): struct nx_action_cnt_ids, ... */
205     NXAST_RAW_DEC_TTL_CNT_IDS,
206
207     /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
208     OFPAT_RAW12_SET_FIELD,
209     /* OF1.5+(25): struct ofp12_action_set_field, ... */
210     OFPAT_RAW15_SET_FIELD,
211     /* NX1.0-1.4(7): struct nx_action_reg_load.
212      *
213      * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
214      * we drop reg_load.] */
215     NXAST_RAW_REG_LOAD,
216     /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
217      *
218      * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
219      * we drop reg_load2.] */
220     NXAST_RAW_REG_LOAD2,
221
222     /* OF1.5+(28): struct ofp15_action_copy_field, ... */
223     OFPAT_RAW15_COPY_FIELD,
224     /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
225     ONFACT_RAW13_COPY_FIELD,
226     /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
227     NXAST_RAW_REG_MOVE,
228
229 /* ## ------------------------- ## */
230 /* ## Nicira extension actions. ## */
231 /* ## ------------------------- ## */
232
233 /* Actions similar to standard actions are listed with the standard actions. */
234
235     /* NX1.0+(1): uint16_t. */
236     NXAST_RAW_RESUBMIT,
237     /* NX1.0+(14): struct nx_action_resubmit. */
238     NXAST_RAW_RESUBMIT_TABLE,
239
240     /* NX1.0+(2): uint32_t. */
241     NXAST_RAW_SET_TUNNEL,
242     /* NX1.0+(9): uint64_t. */
243     NXAST_RAW_SET_TUNNEL64,
244
245     /* NX1.0+(5): void. */
246     NXAST_RAW_POP_QUEUE,
247
248     /* NX1.0+(8): struct nx_action_note, ... */
249     NXAST_RAW_NOTE,
250
251     /* NX1.0+(10): struct nx_action_multipath. */
252     NXAST_RAW_MULTIPATH,
253
254     /* NX1.0+(12): struct nx_action_bundle, ... */
255     NXAST_RAW_BUNDLE,
256     /* NX1.0+(13): struct nx_action_bundle, ... */
257     NXAST_RAW_BUNDLE_LOAD,
258
259     /* NX1.0+(15): struct nx_action_output_reg. */
260     NXAST_RAW_OUTPUT_REG,
261     /* NX1.0+(32): struct nx_action_output_reg2. */
262     NXAST_RAW_OUTPUT_REG2,
263
264     /* NX1.0+(16): struct nx_action_learn, ... */
265     NXAST_RAW_LEARN,
266
267     /* NX1.0+(17): void. */
268     NXAST_RAW_EXIT,
269
270     /* NX1.0+(19): struct nx_action_fin_timeout. */
271     NXAST_RAW_FIN_TIMEOUT,
272
273     /* NX1.0+(20): struct nx_action_controller. */
274     NXAST_RAW_CONTROLLER,
275
276     /* NX1.0+(22): struct nx_action_write_metadata. */
277     NXAST_RAW_WRITE_METADATA,
278
279     /* NX1.0+(27): struct nx_action_stack. */
280     NXAST_RAW_STACK_PUSH,
281
282     /* NX1.0+(28): struct nx_action_stack. */
283     NXAST_RAW_STACK_POP,
284
285     /* NX1.0+(29): struct nx_action_sample. */
286     NXAST_RAW_SAMPLE,
287
288     /* NX1.0+(34): struct nx_action_conjunction. */
289     NXAST_RAW_CONJUNCTION,
290
291     /* NX1.0+(35): struct nx_action_conntrack, ... */
292     NXAST_RAW_CT,
293
294 /* ## ------------------ ## */
295 /* ## Debugging actions. ## */
296 /* ## ------------------ ## */
297
298 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
299 /* accepts them only if started with --enable-dummy. */
300
301     /* NX1.0+(255): void. */
302     NXAST_RAW_DEBUG_RECIRC,
303 };
304
305 /* OpenFlow actions are always a multiple of 8 bytes in length. */
306 #define OFP_ACTION_ALIGN 8
307
308 /* Define a few functions for working with instructions. */
309 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
310     static inline const struct STRUCT * OVS_UNUSED              \
311     instruction_get_##ENUM(const struct ofp11_instruction *inst)\
312     {                                                           \
313         ovs_assert(inst->type == htons(ENUM));                  \
314         return ALIGNED_CAST(struct STRUCT *, inst);             \
315     }                                                           \
316                                                                 \
317     static inline void OVS_UNUSED                               \
318     instruction_init_##ENUM(struct STRUCT *s)                   \
319     {                                                           \
320         memset(s, 0, sizeof *s);                                \
321         s->type = htons(ENUM);                                  \
322         s->len = htons(sizeof *s);                              \
323     }                                                           \
324                                                                 \
325     static inline struct STRUCT * OVS_UNUSED                    \
326     instruction_put_##ENUM(struct ofpbuf *buf)                  \
327     {                                                           \
328         struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
329         instruction_init_##ENUM(s);                             \
330         return s;                                               \
331     }
332 OVS_INSTRUCTIONS
333 #undef DEFINE_INST
334
335 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
336                                                size_t ofs);
337 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
338
339 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
340                                   uint32_t allowed_ovsinsts,
341                                   enum ofpact_type outer_action);
342
343 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
344                                  enum mf_field_id, uint64_t value);
345
346 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
347                                    enum ofp_raw_action_type *, uint64_t *arg);
348 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
349                             enum ofp_raw_action_type, uint64_t arg);
350
351 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
352     char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
353     bool allow_instructions, enum ofpact_type outer_action);
354 static enum ofperr ofpacts_pull_openflow_actions__(
355     struct ofpbuf *openflow, unsigned int actions_len,
356     enum ofp_version version, uint32_t allowed_ovsinsts,
357     struct ofpbuf *ofpacts, enum ofpact_type outer_action);
358 static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
359     const char *s_, struct ofpbuf *ofpacts,
360     enum ofputil_protocol *usable_protocols,
361     bool allow_instructions, enum ofpact_type outer_action);
362
363 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
364  * nested ofpacts it returns the first one. */
365 struct ofpact *
366 ofpact_next_flattened(const struct ofpact *ofpact)
367 {
368     switch (ofpact->type) {
369     case OFPACT_OUTPUT:
370     case OFPACT_GROUP:
371     case OFPACT_CONTROLLER:
372     case OFPACT_ENQUEUE:
373     case OFPACT_OUTPUT_REG:
374     case OFPACT_BUNDLE:
375     case OFPACT_SET_FIELD:
376     case OFPACT_SET_VLAN_VID:
377     case OFPACT_SET_VLAN_PCP:
378     case OFPACT_STRIP_VLAN:
379     case OFPACT_PUSH_VLAN:
380     case OFPACT_SET_ETH_SRC:
381     case OFPACT_SET_ETH_DST:
382     case OFPACT_SET_IPV4_SRC:
383     case OFPACT_SET_IPV4_DST:
384     case OFPACT_SET_IP_DSCP:
385     case OFPACT_SET_IP_ECN:
386     case OFPACT_SET_IP_TTL:
387     case OFPACT_SET_L4_SRC_PORT:
388     case OFPACT_SET_L4_DST_PORT:
389     case OFPACT_REG_MOVE:
390     case OFPACT_STACK_PUSH:
391     case OFPACT_STACK_POP:
392     case OFPACT_DEC_TTL:
393     case OFPACT_SET_MPLS_LABEL:
394     case OFPACT_SET_MPLS_TC:
395     case OFPACT_SET_MPLS_TTL:
396     case OFPACT_DEC_MPLS_TTL:
397     case OFPACT_PUSH_MPLS:
398     case OFPACT_POP_MPLS:
399     case OFPACT_SET_TUNNEL:
400     case OFPACT_SET_QUEUE:
401     case OFPACT_POP_QUEUE:
402     case OFPACT_FIN_TIMEOUT:
403     case OFPACT_RESUBMIT:
404     case OFPACT_LEARN:
405     case OFPACT_CONJUNCTION:
406     case OFPACT_MULTIPATH:
407     case OFPACT_NOTE:
408     case OFPACT_EXIT:
409     case OFPACT_SAMPLE:
410     case OFPACT_UNROLL_XLATE:
411     case OFPACT_DEBUG_RECIRC:
412     case OFPACT_METER:
413     case OFPACT_CLEAR_ACTIONS:
414     case OFPACT_WRITE_METADATA:
415     case OFPACT_GOTO_TABLE:
416         return ofpact_next(ofpact);
417
418     case OFPACT_CT:
419         return ofpact_get_CT(ofpact)->actions;
420
421     case OFPACT_WRITE_ACTIONS:
422         return ofpact_get_WRITE_ACTIONS(ofpact)->actions;
423     }
424
425     OVS_NOT_REACHED();
426 }
427
428 /* Pull off existing actions or instructions. Used by nesting actions to keep
429  * ofpacts_parse() oblivious of actions nesting.
430  *
431  * Push the actions back on after nested parsing, e.g.:
432  *
433  *     size_t ofs = ofpacts_pull(ofpacts);
434  *     ...nested parsing...
435  *     ofpbuf_push_uninit(ofpacts, ofs);
436  */
437 static size_t
438 ofpacts_pull(struct ofpbuf *ofpacts)
439 {
440     size_t ofs;
441
442     ofpact_pad(ofpacts);
443     ofs = ofpacts->size;
444     ofpbuf_pull(ofpacts, ofs);
445
446     return ofs;
447 }
448
449 #include "ofp-actions.inc1"
450 \f
451 /* Output actions. */
452
453 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
454  * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
455  * number of bytes to send.  A 'max_len' of zero means no bytes of the
456  * packet should be sent. */
457 struct ofp10_action_output {
458     ovs_be16 type;                  /* OFPAT10_OUTPUT. */
459     ovs_be16 len;                   /* Length is 8. */
460     ovs_be16 port;                  /* Output port. */
461     ovs_be16 max_len;               /* Max length to send to controller. */
462 };
463 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
464
465 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
466    * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
467    * number of bytes to send. A 'max_len' of zero means no bytes of the
468    * packet should be sent.*/
469 struct ofp11_action_output {
470     ovs_be16 type;                    /* OFPAT11_OUTPUT. */
471     ovs_be16 len;                     /* Length is 16. */
472     ovs_be32 port;                    /* Output port. */
473     ovs_be16 max_len;                 /* Max length to send to controller. */
474     uint8_t pad[6];                   /* Pad to 64 bits. */
475 };
476 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
477
478 static enum ofperr
479 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
480                           enum ofp_version ofp_version OVS_UNUSED,
481                           struct ofpbuf *out)
482 {
483     struct ofpact_output *output;
484
485     output = ofpact_put_OUTPUT(out);
486     output->port = u16_to_ofp(ntohs(oao->port));
487     output->max_len = ntohs(oao->max_len);
488
489     return ofpact_check_output_port(output->port, OFPP_MAX);
490 }
491
492 static enum ofperr
493 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
494                           enum ofp_version ofp_version OVS_UNUSED,
495                           struct ofpbuf *out)
496 {
497     struct ofpact_output *output;
498     enum ofperr error;
499
500     output = ofpact_put_OUTPUT(out);
501     output->max_len = ntohs(oao->max_len);
502
503     error = ofputil_port_from_ofp11(oao->port, &output->port);
504     if (error) {
505         return error;
506     }
507
508     return ofpact_check_output_port(output->port, OFPP_MAX);
509 }
510
511 static void
512 encode_OUTPUT(const struct ofpact_output *output,
513               enum ofp_version ofp_version, struct ofpbuf *out)
514 {
515     if (ofp_version == OFP10_VERSION) {
516         struct ofp10_action_output *oao;
517
518         oao = put_OFPAT10_OUTPUT(out);
519         oao->port = htons(ofp_to_u16(output->port));
520         oao->max_len = htons(output->max_len);
521     } else {
522         struct ofp11_action_output *oao;
523
524         oao = put_OFPAT11_OUTPUT(out);
525         oao->port = ofputil_port_to_ofp11(output->port);
526         oao->max_len = htons(output->max_len);
527     }
528 }
529
530 static char * OVS_WARN_UNUSED_RESULT
531 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
532              enum ofputil_protocol *usable_protocols OVS_UNUSED)
533 {
534     if (strchr(arg, '[')) {
535         struct ofpact_output_reg *output_reg;
536
537         output_reg = ofpact_put_OUTPUT_REG(ofpacts);
538         output_reg->max_len = UINT16_MAX;
539         return mf_parse_subfield(&output_reg->src, arg);
540     } else {
541         struct ofpact_output *output;
542
543         output = ofpact_put_OUTPUT(ofpacts);
544         if (!ofputil_port_from_string(arg, &output->port)) {
545             return xasprintf("%s: output to unknown port", arg);
546         }
547         output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
548         return NULL;
549     }
550 }
551
552 static void
553 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
554 {
555     if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
556         ds_put_format(s, "output:%"PRIu16, a->port);
557     } else {
558         ofputil_format_port(a->port, s);
559         if (a->port == OFPP_CONTROLLER) {
560             ds_put_format(s, ":%"PRIu16, a->max_len);
561         }
562     }
563 }
564 \f
565 /* Group actions. */
566
567 static enum ofperr
568 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
569                          enum ofp_version ofp_version OVS_UNUSED,
570                          struct ofpbuf *out)
571 {
572     ofpact_put_GROUP(out)->group_id = group_id;
573     return 0;
574 }
575
576 static void
577 encode_GROUP(const struct ofpact_group *group,
578              enum ofp_version ofp_version, struct ofpbuf *out)
579 {
580     if (ofp_version == OFP10_VERSION) {
581         /* XXX */
582     } else {
583         put_OFPAT11_GROUP(out, group->group_id);
584     }
585 }
586
587 static char * OVS_WARN_UNUSED_RESULT
588 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
589                     enum ofputil_protocol *usable_protocols OVS_UNUSED)
590 {
591     return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
592 }
593
594 static void
595 format_GROUP(const struct ofpact_group *a, struct ds *s)
596 {
597     ds_put_format(s, "group:%"PRIu32, a->group_id);
598 }
599 \f
600 /* Action structure for NXAST_CONTROLLER.
601  *
602  * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER.  In
603  * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
604  * specifying:
605  *
606  *    - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
607  *
608  *    - 'controller_id': The ID of the controller connection to which the
609  *      ofp_packet_in should be sent.  The ofp_packet_in or nx_packet_in is
610  *      sent only to controllers that have the specified controller connection
611  *      ID.  See "struct nx_controller_id" for more information. */
612 struct nx_action_controller {
613     ovs_be16 type;                  /* OFPAT_VENDOR. */
614     ovs_be16 len;                   /* Length is 16. */
615     ovs_be32 vendor;                /* NX_VENDOR_ID. */
616     ovs_be16 subtype;               /* NXAST_CONTROLLER. */
617     ovs_be16 max_len;               /* Maximum length to send to controller. */
618     ovs_be16 controller_id;         /* Controller ID to send packet-in. */
619     uint8_t reason;                 /* enum ofp_packet_in_reason (OFPR_*). */
620     uint8_t zero;                   /* Must be zero. */
621 };
622 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
623
624 static enum ofperr
625 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
626                             enum ofp_version ofp_version OVS_UNUSED,
627                             struct ofpbuf *out)
628 {
629     struct ofpact_controller *oc;
630
631     oc = ofpact_put_CONTROLLER(out);
632     oc->max_len = ntohs(nac->max_len);
633     oc->controller_id = ntohs(nac->controller_id);
634     oc->reason = nac->reason;
635     return 0;
636 }
637
638 static void
639 encode_CONTROLLER(const struct ofpact_controller *controller,
640                   enum ofp_version ofp_version OVS_UNUSED,
641                   struct ofpbuf *out)
642 {
643     struct nx_action_controller *nac;
644
645     nac = put_NXAST_CONTROLLER(out);
646     nac->max_len = htons(controller->max_len);
647     nac->controller_id = htons(controller->controller_id);
648     nac->reason = controller->reason;
649 }
650
651 static char * OVS_WARN_UNUSED_RESULT
652 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
653                   enum ofputil_protocol *usable_protocols OVS_UNUSED)
654 {
655     enum ofp_packet_in_reason reason = OFPR_ACTION;
656     uint16_t controller_id = 0;
657     uint16_t max_len = UINT16_MAX;
658
659     if (!arg[0]) {
660         /* Use defaults. */
661     } else if (strspn(arg, "0123456789") == strlen(arg)) {
662         char *error = str_to_u16(arg, "max_len", &max_len);
663         if (error) {
664             return error;
665         }
666     } else {
667         char *name, *value;
668
669         while (ofputil_parse_key_value(&arg, &name, &value)) {
670             if (!strcmp(name, "reason")) {
671                 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
672                     return xasprintf("unknown reason \"%s\"", value);
673                 }
674             } else if (!strcmp(name, "max_len")) {
675                 char *error = str_to_u16(value, "max_len", &max_len);
676                 if (error) {
677                     return error;
678                 }
679             } else if (!strcmp(name, "id")) {
680                 char *error = str_to_u16(value, "id", &controller_id);
681                 if (error) {
682                     return error;
683                 }
684             } else {
685                 return xasprintf("unknown key \"%s\" parsing controller "
686                                  "action", name);
687             }
688         }
689     }
690
691     if (reason == OFPR_ACTION && controller_id == 0) {
692         struct ofpact_output *output;
693
694         output = ofpact_put_OUTPUT(ofpacts);
695         output->port = OFPP_CONTROLLER;
696         output->max_len = max_len;
697     } else {
698         struct ofpact_controller *controller;
699
700         controller = ofpact_put_CONTROLLER(ofpacts);
701         controller->max_len = max_len;
702         controller->reason = reason;
703         controller->controller_id = controller_id;
704     }
705
706     return NULL;
707 }
708
709 static void
710 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
711 {
712     if (a->reason == OFPR_ACTION && a->controller_id == 0) {
713         ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
714     } else {
715         enum ofp_packet_in_reason reason = a->reason;
716
717         ds_put_cstr(s, "controller(");
718         if (reason != OFPR_ACTION) {
719             char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
720
721             ds_put_format(s, "reason=%s,",
722                           ofputil_packet_in_reason_to_string(
723                               reason, reasonbuf, sizeof reasonbuf));
724         }
725         if (a->max_len != UINT16_MAX) {
726             ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
727         }
728         if (a->controller_id != 0) {
729             ds_put_format(s, "id=%"PRIu16",", a->controller_id);
730         }
731         ds_chomp(s, ',');
732         ds_put_char(s, ')');
733     }
734 }
735 \f
736 /* Enqueue action. */
737 struct ofp10_action_enqueue {
738     ovs_be16 type;            /* OFPAT10_ENQUEUE. */
739     ovs_be16 len;             /* Len is 16. */
740     ovs_be16 port;            /* Port that queue belongs. Should
741                                  refer to a valid physical port
742                                  (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
743     uint8_t pad[6];           /* Pad for 64-bit alignment. */
744     ovs_be32 queue_id;        /* Where to enqueue the packets. */
745 };
746 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
747
748 static enum ofperr
749 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
750                            enum ofp_version ofp_version OVS_UNUSED,
751                            struct ofpbuf *out)
752 {
753     struct ofpact_enqueue *enqueue;
754
755     enqueue = ofpact_put_ENQUEUE(out);
756     enqueue->port = u16_to_ofp(ntohs(oae->port));
757     enqueue->queue = ntohl(oae->queue_id);
758     if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
759         && enqueue->port != OFPP_IN_PORT
760         && enqueue->port != OFPP_LOCAL) {
761         return OFPERR_OFPBAC_BAD_OUT_PORT;
762     }
763     return 0;
764 }
765
766 static void
767 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
768                enum ofp_version ofp_version, struct ofpbuf *out)
769 {
770     if (ofp_version == OFP10_VERSION) {
771         struct ofp10_action_enqueue *oae;
772
773         oae = put_OFPAT10_ENQUEUE(out);
774         oae->port = htons(ofp_to_u16(enqueue->port));
775         oae->queue_id = htonl(enqueue->queue);
776     } else {
777         /* XXX */
778     }
779 }
780
781 static char * OVS_WARN_UNUSED_RESULT
782 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
783               enum ofputil_protocol *usable_protocols OVS_UNUSED)
784 {
785     char *sp = NULL;
786     char *port = strtok_r(arg, ":q,", &sp);
787     char *queue = strtok_r(NULL, "", &sp);
788     struct ofpact_enqueue *enqueue;
789
790     if (port == NULL || queue == NULL) {
791         return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
792                        "\"enqueue(PORT,QUEUE)\"");
793     }
794
795     enqueue = ofpact_put_ENQUEUE(ofpacts);
796     if (!ofputil_port_from_string(port, &enqueue->port)) {
797         return xasprintf("%s: enqueue to unknown port", port);
798     }
799     return str_to_u32(queue, &enqueue->queue);
800 }
801
802 static void
803 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
804 {
805     ds_put_format(s, "enqueue:");
806     ofputil_format_port(a->port, s);
807     ds_put_format(s, ":%"PRIu32, a->queue);
808 }
809 \f
810 /* Action structure for NXAST_OUTPUT_REG.
811  *
812  * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
813  *
814  * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
815  * the NXAST_REG_LOAD action.
816  *
817  * The acceptable nxm_header values for 'src' are the same as the acceptable
818  * nxm_header values for the 'src' field of NXAST_REG_MOVE.
819  *
820  * The 'max_len' field indicates the number of bytes to send when the chosen
821  * port is OFPP_CONTROLLER.  Its semantics are equivalent to the 'max_len'
822  * field of OFPAT_OUTPUT.
823  *
824  * The 'zero' field is required to be zeroed for forward compatibility. */
825 struct nx_action_output_reg {
826     ovs_be16 type;              /* OFPAT_VENDOR. */
827     ovs_be16 len;               /* 24. */
828     ovs_be32 vendor;            /* NX_VENDOR_ID. */
829     ovs_be16 subtype;           /* NXAST_OUTPUT_REG. */
830
831     ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
832     ovs_be32 src;               /* Source. */
833
834     ovs_be16 max_len;           /* Max length to send to controller. */
835
836     uint8_t zero[6];            /* Reserved, must be zero. */
837 };
838 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
839
840 /* Action structure for NXAST_OUTPUT_REG2.
841  *
842  * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
843  * experimenter OXM as 'src'.
844  */
845 struct nx_action_output_reg2 {
846     ovs_be16 type;              /* OFPAT_VENDOR. */
847     ovs_be16 len;               /* 24. */
848     ovs_be32 vendor;            /* NX_VENDOR_ID. */
849     ovs_be16 subtype;           /* NXAST_OUTPUT_REG2. */
850
851     ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
852     ovs_be16 max_len;           /* Max length to send to controller. */
853
854     /* Followed by:
855      * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
856      * - Enough 0-bytes to pad the action out to 24 bytes. */
857     uint8_t pad[10];
858 };
859 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
860
861 static enum ofperr
862 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
863                             enum ofp_version ofp_version OVS_UNUSED,
864                             struct ofpbuf *out)
865 {
866     struct ofpact_output_reg *output_reg;
867
868     if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
869         return OFPERR_OFPBAC_BAD_ARGUMENT;
870     }
871
872     output_reg = ofpact_put_OUTPUT_REG(out);
873     output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
874     output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
875     output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
876     output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
877     output_reg->max_len = ntohs(naor->max_len);
878
879     return mf_check_src(&output_reg->src, NULL);
880 }
881
882 static enum ofperr
883 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
884                              enum ofp_version ofp_version OVS_UNUSED,
885                              struct ofpbuf *out)
886 {
887     struct ofpact_output_reg *output_reg;
888     enum ofperr error;
889     struct ofpbuf b;
890
891     output_reg = ofpact_put_OUTPUT_REG(out);
892     output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
893     output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
894     output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
895     output_reg->max_len = ntohs(naor->max_len);
896
897     ofpbuf_use_const(&b, naor, ntohs(naor->len));
898     ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
899     error = nx_pull_header(&b, &output_reg->src.field, NULL);
900     if (error) {
901         return error;
902     }
903     if (!is_all_zeros(b.data, b.size)) {
904         return OFPERR_NXBRC_MUST_BE_ZERO;
905     }
906
907     return mf_check_src(&output_reg->src, NULL);
908 }
909
910 static void
911 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
912                   enum ofp_version ofp_version OVS_UNUSED,
913                   struct ofpbuf *out)
914 {
915     /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
916      * cannot be encoded in the older form, encode it as
917      * NXAST_RAW_OUTPUT_REG2. */
918     if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
919         || !mf_nxm_header(output_reg->src.field->id)) {
920         struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
921         size_t size = out->size;
922
923         naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
924                                                output_reg->src.n_bits);
925         naor->max_len = htons(output_reg->max_len);
926
927         out->size = size - sizeof naor->pad;
928         nx_put_header(out, output_reg->src.field->id, 0, false);
929         out->size = size;
930     } else {
931         struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
932
933         naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
934                                                output_reg->src.n_bits);
935         naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
936         naor->max_len = htons(output_reg->max_len);
937     }
938 }
939
940 static char * OVS_WARN_UNUSED_RESULT
941 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
942                  enum ofputil_protocol *usable_protocols OVS_UNUSED)
943 {
944     return parse_OUTPUT(arg, ofpacts, usable_protocols);
945 }
946
947 static void
948 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
949 {
950     ds_put_cstr(s, "output:");
951     mf_format_subfield(&a->src, s);
952 }
953 \f
954 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
955  *
956  * The bundle actions choose a slave from a supplied list of options.
957  * NXAST_BUNDLE outputs to its selection.  NXAST_BUNDLE_LOAD writes its
958  * selection to a register.
959  *
960  * The list of possible slaves follows the nx_action_bundle structure. The size
961  * of each slave is governed by its type as indicated by the 'slave_type'
962  * parameter. The list of slaves should be padded at its end with zeros to make
963  * the total length of the action a multiple of 8.
964  *
965  * Switches infer from the 'slave_type' parameter the size of each slave.  All
966  * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
967  * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
968  * 2 byte width.  Switches should reject actions which indicate unknown or
969  * unsupported slave types.
970  *
971  * Switches use a strategy dictated by the 'algorithm' parameter to choose a
972  * slave.  If the switch does not support the specified 'algorithm' parameter,
973  * it should reject the action.
974  *
975  * Several algorithms take into account liveness when selecting slaves.  The
976  * liveness of a slave is implementation defined (with one exception), but will
977  * generally take into account things like its carrier status and the results
978  * of any link monitoring protocols which happen to be running on it.  In order
979  * to give controllers a place-holder value, the OFPP_NONE port is always
980  * considered live.
981  *
982  * Some slave selection strategies require the use of a hash function, in which
983  * case the 'fields' and 'basis' parameters should be populated.  The 'fields'
984  * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
985  * hash.  Refer to the definition of "enum nx_hash_fields" for details.  The
986  * 'basis' parameter is used as a universal hash parameter.  Different values
987  * of 'basis' yield different hash results.
988  *
989  * The 'zero' parameter at the end of the action structure is reserved for
990  * future use.  Switches are required to reject actions which have nonzero
991  * bytes in the 'zero' field.
992  *
993  * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed.  Switches
994  * should reject actions which have nonzero bytes in either of these fields.
995  *
996  * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
997  * dst[ofs:ofs+n_bits].  The format and semantics of 'dst' and 'ofs_nbits' are
998  * similar to those for the NXAST_REG_LOAD action. */
999 struct nx_action_bundle {
1000     ovs_be16 type;              /* OFPAT_VENDOR. */
1001     ovs_be16 len;               /* Length including slaves. */
1002     ovs_be32 vendor;            /* NX_VENDOR_ID. */
1003     ovs_be16 subtype;           /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1004
1005     /* Slave choice algorithm to apply to hash value. */
1006     ovs_be16 algorithm;         /* One of NX_BD_ALG_*. */
1007
1008     /* What fields to hash and how. */
1009     ovs_be16 fields;            /* One of NX_HASH_FIELDS_*. */
1010     ovs_be16 basis;             /* Universal hash parameter. */
1011
1012     ovs_be32 slave_type;        /* NXM_OF_IN_PORT. */
1013     ovs_be16 n_slaves;          /* Number of slaves. */
1014
1015     ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
1016     ovs_be32 dst;               /* Destination. */
1017
1018     uint8_t zero[4];            /* Reserved. Must be zero. */
1019 };
1020 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
1021
1022 static enum ofperr
1023 decode_bundle(bool load, const struct nx_action_bundle *nab,
1024               struct ofpbuf *ofpacts)
1025 {
1026     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1027     struct ofpact_bundle *bundle;
1028     uint32_t slave_type;
1029     size_t slaves_size, i;
1030     enum ofperr error;
1031
1032     bundle = ofpact_put_BUNDLE(ofpacts);
1033
1034     bundle->n_slaves = ntohs(nab->n_slaves);
1035     bundle->basis = ntohs(nab->basis);
1036     bundle->fields = ntohs(nab->fields);
1037     bundle->algorithm = ntohs(nab->algorithm);
1038     slave_type = ntohl(nab->slave_type);
1039     slaves_size = ntohs(nab->len) - sizeof *nab;
1040
1041     error = OFPERR_OFPBAC_BAD_ARGUMENT;
1042     if (!flow_hash_fields_valid(bundle->fields)) {
1043         VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
1044     } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
1045         VLOG_WARN_RL(&rl, "too many slaves");
1046     } else if (bundle->algorithm != NX_BD_ALG_HRW
1047                && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
1048         VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
1049     } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
1050         VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
1051     } else {
1052         error = 0;
1053     }
1054
1055     if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
1056         VLOG_WARN_RL(&rl, "reserved field is nonzero");
1057         error = OFPERR_OFPBAC_BAD_ARGUMENT;
1058     }
1059
1060     if (load) {
1061         bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
1062         bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1063         bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
1064
1065         if (bundle->dst.n_bits < 16) {
1066             VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
1067                          "destination.");
1068             error = OFPERR_OFPBAC_BAD_ARGUMENT;
1069         }
1070     } else {
1071         if (nab->ofs_nbits || nab->dst) {
1072             VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1073             error = OFPERR_OFPBAC_BAD_ARGUMENT;
1074         }
1075     }
1076
1077     if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1078         VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1079                      "allocated for slaves.  %"PRIuSIZE" bytes are required "
1080                      "for %"PRIu16" slaves.",
1081                      load ? "bundle_load" : "bundle", slaves_size,
1082                      bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1083         error = OFPERR_OFPBAC_BAD_LEN;
1084     }
1085
1086     for (i = 0; i < bundle->n_slaves; i++) {
1087         uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1088         ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1089     }
1090
1091     bundle = ofpacts->header;
1092     ofpact_update_len(ofpacts, &bundle->ofpact);
1093
1094     if (!error) {
1095         error = bundle_check(bundle, OFPP_MAX, NULL);
1096     }
1097     return error;
1098 }
1099
1100 static enum ofperr
1101 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1102                         enum ofp_version ofp_version OVS_UNUSED,
1103                         struct ofpbuf *out)
1104 {
1105     return decode_bundle(false, nab, out);
1106 }
1107
1108 static enum ofperr
1109 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1110                              enum ofp_version ofp_version OVS_UNUSED,
1111                              struct ofpbuf *out)
1112 {
1113     return decode_bundle(true, nab, out);
1114 }
1115
1116 static void
1117 encode_BUNDLE(const struct ofpact_bundle *bundle,
1118               enum ofp_version ofp_version OVS_UNUSED,
1119               struct ofpbuf *out)
1120 {
1121     int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1122     struct nx_action_bundle *nab;
1123     ovs_be16 *slaves;
1124     size_t i;
1125
1126     nab = (bundle->dst.field
1127            ? put_NXAST_BUNDLE_LOAD(out)
1128            : put_NXAST_BUNDLE(out));
1129     nab->len = htons(ntohs(nab->len) + slaves_len);
1130     nab->algorithm = htons(bundle->algorithm);
1131     nab->fields = htons(bundle->fields);
1132     nab->basis = htons(bundle->basis);
1133     nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1134     nab->n_slaves = htons(bundle->n_slaves);
1135     if (bundle->dst.field) {
1136         nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1137                                               bundle->dst.n_bits);
1138         nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1139     }
1140
1141     slaves = ofpbuf_put_zeros(out, slaves_len);
1142     for (i = 0; i < bundle->n_slaves; i++) {
1143         slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1144     }
1145 }
1146
1147 static char * OVS_WARN_UNUSED_RESULT
1148 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1149              enum ofputil_protocol *usable_protocols OVS_UNUSED)
1150 {
1151     return bundle_parse(arg, ofpacts);
1152 }
1153
1154 static char * OVS_WARN_UNUSED_RESULT
1155 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1156 {
1157     return bundle_parse_load(arg, ofpacts);
1158 }
1159
1160 static void
1161 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1162 {
1163     bundle_format(a, s);
1164 }
1165 \f
1166 /* Set VLAN actions. */
1167
1168 static enum ofperr
1169 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1170 {
1171     if (vid & ~0xfff) {
1172         return OFPERR_OFPBAC_BAD_ARGUMENT;
1173     } else {
1174         struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1175         vlan_vid->vlan_vid = vid;
1176         vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1177         return 0;
1178     }
1179 }
1180
1181 static enum ofperr
1182 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1183                                 enum ofp_version ofp_version OVS_UNUSED,
1184                                 struct ofpbuf *out)
1185 {
1186     return decode_set_vlan_vid(vid, true, out);
1187 }
1188
1189 static enum ofperr
1190 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1191                                 enum ofp_version ofp_version OVS_UNUSED,
1192                                 struct ofpbuf *out)
1193 {
1194     return decode_set_vlan_vid(vid, false, out);
1195 }
1196
1197 static void
1198 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1199                     enum ofp_version ofp_version, struct ofpbuf *out)
1200 {
1201     uint16_t vid = vlan_vid->vlan_vid;
1202
1203     /* Push a VLAN tag, if none is present and this form of the action calls
1204      * for such a feature. */
1205     if (ofp_version > OFP10_VERSION
1206         && vlan_vid->push_vlan_if_needed
1207         && !vlan_vid->flow_has_vlan) {
1208         put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1209     }
1210
1211     if (ofp_version == OFP10_VERSION) {
1212         put_OFPAT10_SET_VLAN_VID(out, vid);
1213     } else if (ofp_version == OFP11_VERSION) {
1214         put_OFPAT11_SET_VLAN_VID(out, vid);
1215     } else {
1216         ofpact_put_set_field(out, ofp_version,
1217                              MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1218     }
1219 }
1220
1221 static char * OVS_WARN_UNUSED_RESULT
1222 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1223 {
1224     struct ofpact_vlan_vid *vlan_vid;
1225     uint16_t vid;
1226     char *error;
1227
1228     error = str_to_u16(arg, "VLAN VID", &vid);
1229     if (error) {
1230         return error;
1231     }
1232
1233     if (vid & ~VLAN_VID_MASK) {
1234         return xasprintf("%s: not a valid VLAN VID", arg);
1235     }
1236     vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1237     vlan_vid->vlan_vid = vid;
1238     vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1239     return NULL;
1240 }
1241
1242 static char * OVS_WARN_UNUSED_RESULT
1243 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1244                    enum ofputil_protocol *usable_protocols OVS_UNUSED)
1245 {
1246     return parse_set_vlan_vid(arg, ofpacts, false);
1247 }
1248
1249 static void
1250 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1251 {
1252     ds_put_format(s, "%s:%"PRIu16,
1253                   a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1254                   a->vlan_vid);
1255 }
1256 \f
1257 /* Set PCP actions. */
1258
1259 static enum ofperr
1260 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1261 {
1262     if (pcp & ~7) {
1263         return OFPERR_OFPBAC_BAD_ARGUMENT;
1264     } else {
1265         struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1266         vlan_pcp->vlan_pcp = pcp;
1267         vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1268         return 0;
1269     }
1270 }
1271
1272 static enum ofperr
1273 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1274                                 enum ofp_version ofp_version OVS_UNUSED,
1275                                 struct ofpbuf *out)
1276 {
1277     return decode_set_vlan_pcp(pcp, true, out);
1278 }
1279
1280 static enum ofperr
1281 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1282                                 enum ofp_version ofp_version OVS_UNUSED,
1283                                 struct ofpbuf *out)
1284 {
1285     return decode_set_vlan_pcp(pcp, false, out);
1286 }
1287
1288 static void
1289 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1290                     enum ofp_version ofp_version, struct ofpbuf *out)
1291 {
1292     uint8_t pcp = vlan_pcp->vlan_pcp;
1293
1294     /* Push a VLAN tag, if none is present and this form of the action calls
1295      * for such a feature. */
1296     if (ofp_version > OFP10_VERSION
1297         && vlan_pcp->push_vlan_if_needed
1298         && !vlan_pcp->flow_has_vlan) {
1299         put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1300     }
1301
1302     if (ofp_version == OFP10_VERSION) {
1303         put_OFPAT10_SET_VLAN_PCP(out, pcp);
1304     } else if (ofp_version == OFP11_VERSION) {
1305         put_OFPAT11_SET_VLAN_PCP(out, pcp);
1306     } else {
1307         ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1308     }
1309 }
1310
1311 static char * OVS_WARN_UNUSED_RESULT
1312 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1313 {
1314     struct ofpact_vlan_pcp *vlan_pcp;
1315     uint8_t pcp;
1316     char *error;
1317
1318     error = str_to_u8(arg, "VLAN PCP", &pcp);
1319     if (error) {
1320         return error;
1321     }
1322
1323     if (pcp & ~7) {
1324         return xasprintf("%s: not a valid VLAN PCP", arg);
1325     }
1326     vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1327     vlan_pcp->vlan_pcp = pcp;
1328     vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1329     return NULL;
1330 }
1331
1332 static char * OVS_WARN_UNUSED_RESULT
1333 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1334                    enum ofputil_protocol *usable_protocols OVS_UNUSED)
1335 {
1336     return parse_set_vlan_pcp(arg, ofpacts, false);
1337 }
1338
1339 static void
1340 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1341 {
1342     ds_put_format(s, "%s:%"PRIu8,
1343                   a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1344                   a->vlan_pcp);
1345 }
1346 \f
1347 /* Strip VLAN actions. */
1348
1349 static enum ofperr
1350 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1351 {
1352     ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1353     return 0;
1354 }
1355
1356 static enum ofperr
1357 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1358 {
1359     ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1360     return 0;
1361 }
1362
1363 static void
1364 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1365                   enum ofp_version ofp_version, struct ofpbuf *out)
1366 {
1367     if (ofp_version == OFP10_VERSION) {
1368         put_OFPAT10_STRIP_VLAN(out);
1369     } else {
1370         put_OFPAT11_POP_VLAN(out);
1371     }
1372 }
1373
1374 static char * OVS_WARN_UNUSED_RESULT
1375 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1376                  enum ofputil_protocol *usable_protocols OVS_UNUSED)
1377 {
1378     ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1379     return NULL;
1380 }
1381
1382 static char * OVS_WARN_UNUSED_RESULT
1383 parse_pop_vlan(struct ofpbuf *ofpacts)
1384 {
1385     ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1386     return NULL;
1387 }
1388
1389 static void
1390 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1391 {
1392     ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1393                     ? "pop_vlan"
1394                     : "strip_vlan"));
1395 }
1396 \f
1397 /* Push VLAN action. */
1398
1399 static enum ofperr
1400 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1401                              enum ofp_version ofp_version OVS_UNUSED,
1402                              struct ofpbuf *out)
1403 {
1404     if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1405         /* XXX 802.1AD(QinQ) isn't supported at the moment */
1406         return OFPERR_OFPBAC_BAD_ARGUMENT;
1407     }
1408     ofpact_put_PUSH_VLAN(out);
1409     return 0;
1410 }
1411
1412 static void
1413 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1414                  enum ofp_version ofp_version, struct ofpbuf *out)
1415 {
1416     if (ofp_version == OFP10_VERSION) {
1417         /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1418          * follow this action. */
1419     } else {
1420         /* XXX ETH_TYPE_VLAN_8021AD case */
1421         put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1422     }
1423 }
1424
1425 static char * OVS_WARN_UNUSED_RESULT
1426 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1427                 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1428 {
1429     uint16_t ethertype;
1430     char *error;
1431
1432     *usable_protocols &= OFPUTIL_P_OF11_UP;
1433     error = str_to_u16(arg, "ethertype", &ethertype);
1434     if (error) {
1435         return error;
1436     }
1437
1438     if (ethertype != ETH_TYPE_VLAN_8021Q) {
1439         /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1440         return xasprintf("%s: not a valid VLAN ethertype", arg);
1441     }
1442
1443     ofpact_put_PUSH_VLAN(ofpacts);
1444     return NULL;
1445 }
1446
1447 static void
1448 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1449 {
1450     /* XXX 802.1AD case*/
1451     ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1452 }
1453 \f
1454 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1455 struct ofp_action_dl_addr {
1456     ovs_be16 type;                  /* Type. */
1457     ovs_be16 len;                   /* Length is 16. */
1458     struct eth_addr dl_addr;        /* Ethernet address. */
1459     uint8_t pad[6];
1460 };
1461 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1462
1463 static enum ofperr
1464 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1465                             enum ofp_version ofp_version OVS_UNUSED,
1466                             struct ofpbuf *out)
1467 {
1468     ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1469     return 0;
1470 }
1471
1472 static enum ofperr
1473 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1474                             enum ofp_version ofp_version OVS_UNUSED,
1475                             struct ofpbuf *out)
1476 {
1477     ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1478     return 0;
1479 }
1480
1481 static void
1482 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1483                     enum ofp_raw_action_type raw, enum mf_field_id field,
1484                     struct ofpbuf *out)
1485 {
1486     if (ofp_version < OFP12_VERSION) {
1487         struct ofp_action_dl_addr *oada;
1488
1489         oada = ofpact_put_raw(out, ofp_version, raw, 0);
1490         oada->dl_addr = mac->mac;
1491     } else {
1492         ofpact_put_set_field(out, ofp_version, field,
1493                              eth_addr_to_uint64(mac->mac));
1494     }
1495 }
1496
1497 static void
1498 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1499                    struct ofpbuf *out)
1500 {
1501     encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1502                         out);
1503
1504 }
1505
1506 static void
1507 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1508                                enum ofp_version ofp_version,
1509                                struct ofpbuf *out)
1510 {
1511     encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1512                         out);
1513
1514 }
1515
1516 static char * OVS_WARN_UNUSED_RESULT
1517 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1518                  enum ofputil_protocol *usable_protocols OVS_UNUSED)
1519 {
1520     return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1521 }
1522
1523 static char * OVS_WARN_UNUSED_RESULT
1524 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1525                  enum ofputil_protocol *usable_protocols OVS_UNUSED)
1526 {
1527     return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1528 }
1529
1530 static void
1531 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1532 {
1533     ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1534 }
1535
1536 static void
1537 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1538 {
1539     ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1540 }
1541 \f
1542 /* Set IPv4 address actions. */
1543
1544 static enum ofperr
1545 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1546                             enum ofp_version ofp_version OVS_UNUSED,
1547                             struct ofpbuf *out)
1548 {
1549     ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1550     return 0;
1551 }
1552
1553 static enum ofperr
1554 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1555                             enum ofp_version ofp_version OVS_UNUSED,
1556                             struct ofpbuf *out)
1557 {
1558     ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1559     return 0;
1560 }
1561
1562 static void
1563 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1564                      enum ofp_version ofp_version,
1565                      enum ofp_raw_action_type raw, enum mf_field_id field,
1566                      struct ofpbuf *out)
1567 {
1568     ovs_be32 addr = ipv4->ipv4;
1569     if (ofp_version < OFP12_VERSION) {
1570         ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1571     } else {
1572         ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1573     }
1574 }
1575
1576 static void
1577 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1578                     enum ofp_version ofp_version, struct ofpbuf *out)
1579 {
1580     encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1581                          out);
1582 }
1583
1584 static void
1585 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1586                     enum ofp_version ofp_version, struct ofpbuf *out)
1587 {
1588     encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1589                          out);
1590 }
1591
1592 static char * OVS_WARN_UNUSED_RESULT
1593 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1594                    enum ofputil_protocol *usable_protocols OVS_UNUSED)
1595 {
1596     return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1597 }
1598
1599 static char * OVS_WARN_UNUSED_RESULT
1600 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1601                    enum ofputil_protocol *usable_protocols OVS_UNUSED)
1602 {
1603     return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1604 }
1605
1606 static void
1607 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1608 {
1609     ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1610 }
1611
1612 static void
1613 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1614 {
1615     ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1616 }
1617 \f
1618 /* Set IPv4/v6 TOS actions. */
1619
1620 static enum ofperr
1621 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1622                             enum ofp_version ofp_version OVS_UNUSED,
1623                             struct ofpbuf *out)
1624 {
1625     if (dscp & ~IP_DSCP_MASK) {
1626         return OFPERR_OFPBAC_BAD_ARGUMENT;
1627     } else {
1628         ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1629         return 0;
1630     }
1631 }
1632
1633 static void
1634 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1635                    enum ofp_version ofp_version, struct ofpbuf *out)
1636 {
1637     if (ofp_version < OFP12_VERSION) {
1638         put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1639     } else {
1640         ofpact_put_set_field(out, ofp_version,
1641                              MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1642     }
1643 }
1644
1645 static char * OVS_WARN_UNUSED_RESULT
1646 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1647                  enum ofputil_protocol *usable_protocols OVS_UNUSED)
1648 {
1649     uint8_t tos;
1650     char *error;
1651
1652     error = str_to_u8(arg, "TOS", &tos);
1653     if (error) {
1654         return error;
1655     }
1656
1657     if (tos & ~IP_DSCP_MASK) {
1658         return xasprintf("%s: not a valid TOS", arg);
1659     }
1660     ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1661     return NULL;
1662 }
1663
1664 static void
1665 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1666 {
1667     ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1668 }
1669 \f
1670 /* Set IPv4/v6 ECN actions. */
1671
1672 static enum ofperr
1673 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1674                               enum ofp_version ofp_version OVS_UNUSED,
1675                               struct ofpbuf *out)
1676 {
1677     if (ecn & ~IP_ECN_MASK) {
1678         return OFPERR_OFPBAC_BAD_ARGUMENT;
1679     } else {
1680         ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1681         return 0;
1682     }
1683 }
1684
1685 static void
1686 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1687                   enum ofp_version ofp_version, struct ofpbuf *out)
1688 {
1689     uint8_t ecn = ip_ecn->ecn;
1690     if (ofp_version == OFP10_VERSION) {
1691         /* XXX */
1692     } else if (ofp_version == OFP11_VERSION) {
1693         put_OFPAT11_SET_NW_ECN(out, ecn);
1694     } else {
1695         ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1696     }
1697 }
1698
1699 static char * OVS_WARN_UNUSED_RESULT
1700 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1701                  enum ofputil_protocol *usable_protocols OVS_UNUSED)
1702 {
1703     uint8_t ecn;
1704     char *error;
1705
1706     error = str_to_u8(arg, "ECN", &ecn);
1707     if (error) {
1708         return error;
1709     }
1710
1711     if (ecn & ~IP_ECN_MASK) {
1712         return xasprintf("%s: not a valid ECN", arg);
1713     }
1714     ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1715     return NULL;
1716 }
1717
1718 static void
1719 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1720 {
1721     ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1722 }
1723 \f
1724 /* Set IPv4/v6 TTL actions. */
1725
1726 static enum ofperr
1727 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1728                               enum ofp_version ofp_version OVS_UNUSED,
1729                               struct ofpbuf *out)
1730 {
1731     ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1732     return 0;
1733 }
1734
1735 static void
1736 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1737                   enum ofp_version ofp_version, struct ofpbuf *out)
1738 {
1739     if (ofp_version >= OFP11_VERSION) {
1740         put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1741     } else {
1742         /* XXX */
1743     }
1744 }
1745
1746 static char * OVS_WARN_UNUSED_RESULT
1747 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1748                   enum ofputil_protocol *usable_protocols OVS_UNUSED)
1749 {
1750     uint8_t ttl;
1751     char *error;
1752
1753     error = str_to_u8(arg, "TTL", &ttl);
1754     if (error) {
1755         return error;
1756     }
1757
1758     ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1759     return NULL;
1760 }
1761
1762 static void
1763 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1764 {
1765     ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1766 }
1767 \f
1768 /* Set TCP/UDP/SCTP port actions. */
1769
1770 static enum ofperr
1771 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1772                             enum ofp_version ofp_version OVS_UNUSED,
1773                             struct ofpbuf *out)
1774 {
1775     ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1776     return 0;
1777 }
1778
1779 static enum ofperr
1780 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1781                             enum ofp_version ofp_version OVS_UNUSED,
1782                             struct ofpbuf *out)
1783 {
1784     ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1785     return 0;
1786 }
1787
1788 static void
1789 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1790                    enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1791                    enum mf_field_id field, struct ofpbuf *out)
1792 {
1793     uint16_t port = l4_port->port;
1794
1795     if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1796         ofpact_put_set_field(out, ofp_version, field, port);
1797     } else {
1798         ofpact_put_raw(out, ofp_version, raw, port);
1799     }
1800 }
1801
1802 static void
1803 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1804                        enum ofp_version ofp_version, struct ofpbuf *out)
1805 {
1806     uint8_t proto = l4_port->flow_ip_proto;
1807     enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1808                               : proto == IPPROTO_UDP ? MFF_UDP_SRC
1809                               : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1810                               : MFF_N_IDS);
1811
1812     encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1813 }
1814
1815 static void
1816 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1817                        enum ofp_version ofp_version,
1818                        struct ofpbuf *out)
1819 {
1820     uint8_t proto = l4_port->flow_ip_proto;
1821     enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1822                               : proto == IPPROTO_UDP ? MFF_UDP_DST
1823                               : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1824                               : MFF_N_IDS);
1825
1826     encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1827 }
1828
1829 static char * OVS_WARN_UNUSED_RESULT
1830 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1831                       enum ofputil_protocol *usable_protocols OVS_UNUSED)
1832 {
1833     return str_to_u16(arg, "source port",
1834                       &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1835 }
1836
1837 static char * OVS_WARN_UNUSED_RESULT
1838 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1839                       enum ofputil_protocol *usable_protocols OVS_UNUSED)
1840 {
1841     return str_to_u16(arg, "destination port",
1842                       &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1843 }
1844
1845 static void
1846 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1847 {
1848     ds_put_format(s, "mod_tp_src:%d", a->port);
1849 }
1850
1851 static void
1852 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1853 {
1854     ds_put_format(s, "mod_tp_dst:%d", a->port);
1855 }
1856 \f
1857 /* Action structure for OFPAT_COPY_FIELD. */
1858 struct ofp15_action_copy_field {
1859     ovs_be16 type;              /* OFPAT_COPY_FIELD. */
1860     ovs_be16 len;               /* Length is padded to 64 bits. */
1861     ovs_be16 n_bits;            /* Number of bits to copy. */
1862     ovs_be16 src_offset;        /* Starting bit offset in source. */
1863     ovs_be16 dst_offset;        /* Starting bit offset in destination. */
1864     uint8_t pad[2];
1865     /* Followed by:
1866      * - OXM header for source field.
1867      * - OXM header for destination field.
1868      * - Padding with 0-bytes to a multiple of 8 bytes.
1869      * The "pad2" member is the beginning of the above. */
1870     uint8_t pad2[4];
1871 };
1872 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1873
1874 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1875 struct onf_action_copy_field {
1876     ovs_be16 type;              /* OFPAT_EXPERIMENTER. */
1877     ovs_be16 len;               /* Length is padded to 64 bits. */
1878     ovs_be32 experimenter;      /* ONF_VENDOR_ID. */
1879     ovs_be16 exp_type;          /* 3200. */
1880     uint8_t pad[2];             /* Not used. */
1881     ovs_be16 n_bits;            /* Number of bits to copy. */
1882     ovs_be16 src_offset;        /* Starting bit offset in source. */
1883     ovs_be16 dst_offset;        /* Starting bit offset in destination. */
1884     uint8_t pad2[2];            /* Not used. */
1885     /* Followed by:
1886      * - OXM header for source field.
1887      * - OXM header for destination field.
1888      * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1889      * The "pad3" member is the beginning of the above. */
1890     uint8_t pad3[4];            /* Not used. */
1891 };
1892 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1893
1894 /* Action structure for NXAST_REG_MOVE.
1895  *
1896  * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1897  * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1898  * bit 'c').  Bit numbering starts at 0 for the least-significant bit, 1 for
1899  * the next most significant bit, and so on.
1900  *
1901  * 'src' and 'dst' are nxm_header values with nxm_hasmask=0.  (It doesn't make
1902  * sense to use nxm_hasmask=1 because the action does not do any kind of
1903  * matching; it uses the actual value of a field.)
1904  *
1905  * The following nxm_header values are potentially acceptable as 'src':
1906  *
1907  *   - NXM_OF_IN_PORT
1908  *   - NXM_OF_ETH_DST
1909  *   - NXM_OF_ETH_SRC
1910  *   - NXM_OF_ETH_TYPE
1911  *   - NXM_OF_VLAN_TCI
1912  *   - NXM_OF_IP_TOS
1913  *   - NXM_OF_IP_PROTO
1914  *   - NXM_OF_IP_SRC
1915  *   - NXM_OF_IP_DST
1916  *   - NXM_OF_TCP_SRC
1917  *   - NXM_OF_TCP_DST
1918  *   - NXM_OF_UDP_SRC
1919  *   - NXM_OF_UDP_DST
1920  *   - NXM_OF_ICMP_TYPE
1921  *   - NXM_OF_ICMP_CODE
1922  *   - NXM_OF_ARP_OP
1923  *   - NXM_OF_ARP_SPA
1924  *   - NXM_OF_ARP_TPA
1925  *   - NXM_NX_TUN_ID
1926  *   - NXM_NX_ARP_SHA
1927  *   - NXM_NX_ARP_THA
1928  *   - NXM_NX_ICMPV6_TYPE
1929  *   - NXM_NX_ICMPV6_CODE
1930  *   - NXM_NX_ND_SLL
1931  *   - NXM_NX_ND_TLL
1932  *   - NXM_NX_REG(idx) for idx in the switch's accepted range.
1933  *   - NXM_NX_PKT_MARK
1934  *   - NXM_NX_TUN_IPV4_SRC
1935  *   - NXM_NX_TUN_IPV4_DST
1936  *
1937  * The following nxm_header values are potentially acceptable as 'dst':
1938  *
1939  *   - NXM_OF_ETH_DST
1940  *   - NXM_OF_ETH_SRC
1941  *   - NXM_OF_IP_TOS
1942  *   - NXM_OF_IP_SRC
1943  *   - NXM_OF_IP_DST
1944  *   - NXM_OF_TCP_SRC
1945  *   - NXM_OF_TCP_DST
1946  *   - NXM_OF_UDP_SRC
1947  *   - NXM_OF_UDP_DST
1948  *   - NXM_OF_ICMP_TYPE
1949  *   - NXM_OF_ICMP_CODE
1950  *   - NXM_NX_ICMPV6_TYPE
1951  *   - NXM_NX_ICMPV6_CODE
1952  *   - NXM_NX_ARP_SHA
1953  *   - NXM_NX_ARP_THA
1954  *   - NXM_OF_ARP_OP
1955  *   - NXM_OF_ARP_SPA
1956  *   - NXM_OF_ARP_TPA
1957  *     Modifying any of the above fields changes the corresponding packet
1958  *     header.
1959  *
1960  *   - NXM_OF_IN_PORT
1961  *
1962  *   - NXM_NX_REG(idx) for idx in the switch's accepted range.
1963  *
1964  *   - NXM_NX_PKT_MARK
1965  *
1966  *   - NXM_OF_VLAN_TCI.  Modifying this field's value has side effects on the
1967  *     packet's 802.1Q header.  Setting a value with CFI=0 removes the 802.1Q
1968  *     header (if any), ignoring the other bits.  Setting a value with CFI=1
1969  *     adds or modifies the 802.1Q header appropriately, setting the TCI field
1970  *     to the field's new value (with the CFI bit masked out).
1971  *
1972  *   - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST.  Modifying
1973  *     any of these values modifies the corresponding tunnel header field used
1974  *     for the packet's next tunnel encapsulation, if allowed by the
1975  *     configuration of the output tunnel port.
1976  *
1977  * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1978  * nx_match satisfies its prerequisites.  For example, NXM_OF_IP_TOS may be
1979  * used only if the flow's nx_match includes an nxm_entry that specifies
1980  * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1981  *
1982  * The switch will reject actions for which src_ofs+n_bits is greater than the
1983  * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1984  * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1985  *
1986  * This action behaves properly when 'src' overlaps with 'dst', that is, it
1987  * behaves as if 'src' were copied out to a temporary buffer, then the
1988  * temporary buffer copied to 'dst'.
1989  */
1990 struct nx_action_reg_move {
1991     ovs_be16 type;                  /* OFPAT_VENDOR. */
1992     ovs_be16 len;                   /* Length is 24. */
1993     ovs_be32 vendor;                /* NX_VENDOR_ID. */
1994     ovs_be16 subtype;               /* NXAST_REG_MOVE. */
1995     ovs_be16 n_bits;                /* Number of bits. */
1996     ovs_be16 src_ofs;               /* Starting bit offset in source. */
1997     ovs_be16 dst_ofs;               /* Starting bit offset in destination. */
1998     /* Followed by:
1999      * - OXM/NXM header for source field (4 or 8 bytes).
2000      * - OXM/NXM header for destination field (4 or 8 bytes).
2001      * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2002 };
2003 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
2004
2005 static enum ofperr
2006 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
2007                     const void *action, ovs_be16 action_len, size_t oxm_offset,
2008                     struct ofpbuf *ofpacts)
2009 {
2010     struct ofpact_reg_move *move;
2011     enum ofperr error;
2012     struct ofpbuf b;
2013
2014     move = ofpact_put_REG_MOVE(ofpacts);
2015     move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
2016     move->src.ofs = ntohs(src_offset);
2017     move->src.n_bits = ntohs(n_bits);
2018     move->dst.ofs = ntohs(dst_offset);
2019     move->dst.n_bits = ntohs(n_bits);
2020
2021     ofpbuf_use_const(&b, action, ntohs(action_len));
2022     ofpbuf_pull(&b, oxm_offset);
2023     error = nx_pull_header(&b, &move->src.field, NULL);
2024     if (error) {
2025         return error;
2026     }
2027     error = nx_pull_header(&b, &move->dst.field, NULL);
2028     if (error) {
2029         return error;
2030     }
2031
2032     if (!is_all_zeros(b.data, b.size)) {
2033         return OFPERR_NXBRC_MUST_BE_ZERO;
2034     }
2035
2036     return nxm_reg_move_check(move, NULL);
2037 }
2038
2039 static enum ofperr
2040 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
2041                               enum ofp_version ofp_version OVS_UNUSED,
2042                               struct ofpbuf *ofpacts)
2043 {
2044     return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2045                                oacf->n_bits, oacf, oacf->len,
2046                                OBJECT_OFFSETOF(oacf, pad2), ofpacts);
2047 }
2048
2049 static enum ofperr
2050 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
2051                                enum ofp_version ofp_version OVS_UNUSED,
2052                                struct ofpbuf *ofpacts)
2053 {
2054     return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2055                                oacf->n_bits, oacf, oacf->len,
2056                                OBJECT_OFFSETOF(oacf, pad3), ofpacts);
2057 }
2058
2059 static enum ofperr
2060 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
2061                           enum ofp_version ofp_version OVS_UNUSED,
2062                           struct ofpbuf *ofpacts)
2063 {
2064     struct ofpact_reg_move *move;
2065     enum ofperr error;
2066     struct ofpbuf b;
2067
2068     move = ofpact_put_REG_MOVE(ofpacts);
2069     move->ofpact.raw = NXAST_RAW_REG_MOVE;
2070     move->src.ofs = ntohs(narm->src_ofs);
2071     move->src.n_bits = ntohs(narm->n_bits);
2072     move->dst.ofs = ntohs(narm->dst_ofs);
2073     move->dst.n_bits = ntohs(narm->n_bits);
2074
2075     ofpbuf_use_const(&b, narm, ntohs(narm->len));
2076     ofpbuf_pull(&b, sizeof *narm);
2077     error = nx_pull_header(&b, &move->src.field, NULL);
2078     if (error) {
2079         return error;
2080     }
2081     error = nx_pull_header(&b, &move->dst.field, NULL);
2082     if (error) {
2083         return error;
2084     }
2085     if (!is_all_zeros(b.data, b.size)) {
2086         return OFPERR_NXBRC_MUST_BE_ZERO;
2087     }
2088
2089     return nxm_reg_move_check(move, NULL);
2090 }
2091
2092 static void
2093 encode_REG_MOVE(const struct ofpact_reg_move *move,
2094                 enum ofp_version ofp_version, struct ofpbuf *out)
2095 {
2096     /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2097      * NXAST_RAW_REG_MOVE is somewhat difficult.  Neither one is guaranteed to
2098      * be supported by every OpenFlow 1.3 implementation.  It would be ideal to
2099      * probe for support.  Until we have that ability, we currently prefer
2100      * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2101      * versions.  */
2102     size_t start_ofs = out->size;
2103     if (ofp_version >= OFP15_VERSION) {
2104         struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2105         copy->n_bits = htons(move->dst.n_bits);
2106         copy->src_offset = htons(move->src.ofs);
2107         copy->dst_offset = htons(move->dst.ofs);
2108         out->size = out->size - sizeof copy->pad2;
2109         nx_put_header(out, move->src.field->id, ofp_version, false);
2110         nx_put_header(out, move->dst.field->id, ofp_version, false);
2111     } else if (ofp_version == OFP13_VERSION
2112                && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2113         struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2114         copy->n_bits = htons(move->dst.n_bits);
2115         copy->src_offset = htons(move->src.ofs);
2116         copy->dst_offset = htons(move->dst.ofs);
2117         out->size = out->size - sizeof copy->pad3;
2118         nx_put_header(out, move->src.field->id, ofp_version, false);
2119         nx_put_header(out, move->dst.field->id, ofp_version, false);
2120     } else {
2121         struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2122         narm->n_bits = htons(move->dst.n_bits);
2123         narm->src_ofs = htons(move->src.ofs);
2124         narm->dst_ofs = htons(move->dst.ofs);
2125         nx_put_header(out, move->src.field->id, 0, false);
2126         nx_put_header(out, move->dst.field->id, 0, false);
2127     }
2128     pad_ofpat(out, start_ofs);
2129 }
2130
2131 static char * OVS_WARN_UNUSED_RESULT
2132 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2133                enum ofputil_protocol *usable_protocols OVS_UNUSED)
2134 {
2135     struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2136     const char *full_arg = arg;
2137     char *error;
2138
2139     error = mf_parse_subfield__(&move->src, &arg);
2140     if (error) {
2141         return error;
2142     }
2143     if (strncmp(arg, "->", 2)) {
2144         return xasprintf("%s: missing `->' following source", full_arg);
2145     }
2146     arg += 2;
2147     error = mf_parse_subfield(&move->dst, arg);
2148     if (error) {
2149         return error;
2150     }
2151
2152     if (move->src.n_bits != move->dst.n_bits) {
2153         return xasprintf("%s: source field is %d bits wide but destination is "
2154                          "%d bits wide", full_arg,
2155                          move->src.n_bits, move->dst.n_bits);
2156     }
2157     return NULL;
2158 }
2159
2160 static void
2161 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2162 {
2163     nxm_format_reg_move(a, s);
2164 }
2165 \f
2166 /* Action structure for OFPAT12_SET_FIELD. */
2167 struct ofp12_action_set_field {
2168     ovs_be16 type;                  /* OFPAT12_SET_FIELD. */
2169     ovs_be16 len;                   /* Length is padded to 64 bits. */
2170
2171     /* Followed by:
2172      * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2173      * - Enough 0-bytes to pad out to a multiple of 64 bits.
2174      *
2175      * The "pad" member is the beginning of the above. */
2176     uint8_t pad[4];
2177 };
2178 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2179
2180 /* Action structure for NXAST_REG_LOAD.
2181  *
2182  * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2183  * within 'a' numbered 'b' through 'c' (not including bit 'c').  Bit numbering
2184  * starts at 0 for the least-significant bit, 1 for the next most significant
2185  * bit, and so on.
2186  *
2187  * 'dst' is an nxm_header with nxm_hasmask=0.  See the documentation for
2188  * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2189  * loading them.
2190  *
2191  * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2192  * to avoid enlarging the structure by another 8 bytes.  To allow 'n_bits' to
2193  * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2194  * also stored as one less than its true value:
2195  *
2196  *  15                           6 5                0
2197  * +------------------------------+------------------+
2198  * |              ofs             |    n_bits - 1    |
2199  * +------------------------------+------------------+
2200  *
2201  * The switch will reject actions for which ofs+n_bits is greater than the
2202  * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2203  * greater are set to 1, with error type OFPET_BAD_ACTION, code
2204  * OFPBAC_BAD_ARGUMENT.
2205  */
2206 struct nx_action_reg_load {
2207     ovs_be16 type;                  /* OFPAT_VENDOR. */
2208     ovs_be16 len;                   /* Length is 24. */
2209     ovs_be32 vendor;                /* NX_VENDOR_ID. */
2210     ovs_be16 subtype;               /* NXAST_REG_LOAD. */
2211     ovs_be16 ofs_nbits;             /* (ofs << 6) | (n_bits - 1). */
2212     ovs_be32 dst;                   /* Destination register. */
2213     ovs_be64 value;                 /* Immediate value. */
2214 };
2215 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2216
2217 /* Action structure for NXAST_REG_LOAD2.
2218  *
2219  * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2220  * in any OpenFlow version.  Compared to NXAST_REG_LOAD, we can use this to set
2221  * OXM experimenter fields. */
2222 struct nx_action_reg_load2 {
2223     ovs_be16 type;                  /* OFPAT_VENDOR. */
2224     ovs_be16 len;                   /* At least 16. */
2225     ovs_be32 vendor;                /* NX_VENDOR_ID. */
2226     ovs_be16 subtype;               /* NXAST_SET_FIELD. */
2227
2228     /* Followed by:
2229      * - An NXM/OXM header, value, and optionally a mask.
2230      * - Enough 0-bytes to pad out to a multiple of 64 bits.
2231      *
2232      * The "pad" member is the beginning of the above. */
2233     uint8_t pad[6];
2234 };
2235 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2236
2237 static enum ofperr
2238 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2239                        bool may_mask, struct ofpbuf *ofpacts)
2240 {
2241     struct ofpact_set_field *sf;
2242     enum ofperr error;
2243     struct ofpbuf b;
2244
2245     sf = ofpact_put_SET_FIELD(ofpacts);
2246
2247     ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2248     ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2249     error = nx_pull_entry(&b, &sf->field, &sf->value,
2250                           may_mask ? &sf->mask : NULL);
2251     if (error) {
2252         return (error == OFPERR_OFPBMC_BAD_MASK
2253                 ? OFPERR_OFPBAC_BAD_SET_MASK
2254                 : error);
2255     }
2256     if (!may_mask) {
2257         memset(&sf->mask, 0xff, sf->field->n_bytes);
2258     }
2259
2260     if (!is_all_zeros(b.data, b.size)) {
2261         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2262     }
2263
2264     /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2265      * Set-Field. */
2266     if (sf->field->id == MFF_IN_PORT_OXM) {
2267         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2268     }
2269
2270     /* oxm_length is now validated to be compatible with mf_value. */
2271     if (!sf->field->writable) {
2272         VLOG_WARN_RL(&rl, "destination field %s is not writable",
2273                      sf->field->name);
2274         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2275     }
2276
2277     /* The value must be valid for match.  OpenFlow 1.5 also says,
2278      * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2279      * a 1-bit in oxm_value and in oxm_mask." */
2280     if (!mf_is_value_valid(sf->field, &sf->value)
2281         || (sf->field->id == MFF_VLAN_VID
2282             && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2283                 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2284         struct ds ds = DS_EMPTY_INITIALIZER;
2285         mf_format(sf->field, &sf->value, NULL, &ds);
2286         VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2287                      sf->field->name, ds_cstr(&ds));
2288         ds_destroy(&ds);
2289
2290         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2291     }
2292     return 0;
2293 }
2294
2295 static enum ofperr
2296 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2297                              enum ofp_version ofp_version OVS_UNUSED,
2298                              struct ofpbuf *ofpacts)
2299 {
2300     return decode_ofpat_set_field(oasf, false, ofpacts);
2301 }
2302
2303 static enum ofperr
2304 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2305                              enum ofp_version ofp_version OVS_UNUSED,
2306                              struct ofpbuf *ofpacts)
2307 {
2308     return decode_ofpat_set_field(oasf, true, ofpacts);
2309 }
2310
2311 static enum ofperr
2312 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2313                           enum ofp_version ofp_version OVS_UNUSED,
2314                           struct ofpbuf *out)
2315 {
2316     struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2317     struct mf_subfield dst;
2318     enum ofperr error;
2319
2320     sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2321
2322     dst.field = mf_from_nxm_header(ntohl(narl->dst));
2323     dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2324     dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2325     error = mf_check_dst(&dst, NULL);
2326     if (error) {
2327         return error;
2328     }
2329
2330     /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2331      * narl->value. */
2332     if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2333         return OFPERR_OFPBAC_BAD_ARGUMENT;
2334     }
2335
2336     sf->field = dst.field;
2337     bitwise_put(ntohll(narl->value),
2338                 &sf->value, dst.field->n_bytes, dst.ofs,
2339                 dst.n_bits);
2340     bitwise_put(UINT64_MAX,
2341                 &sf->mask, dst.field->n_bytes, dst.ofs,
2342                 dst.n_bits);
2343
2344     return 0;
2345 }
2346
2347 static enum ofperr
2348 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2349                            enum ofp_version ofp_version OVS_UNUSED,
2350                            struct ofpbuf *out)
2351 {
2352     struct ofpact_set_field *sf;
2353     enum ofperr error;
2354     struct ofpbuf b;
2355
2356     sf = ofpact_put_SET_FIELD(out);
2357     sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2358
2359     ofpbuf_use_const(&b, narl, ntohs(narl->len));
2360     ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2361     error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2362     if (error) {
2363         return error;
2364     }
2365     if (!is_all_zeros(b.data, b.size)) {
2366         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2367     }
2368
2369     if (!sf->field->writable) {
2370         VLOG_WARN_RL(&rl, "destination field %s is not writable",
2371                      sf->field->name);
2372         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2373     }
2374     return 0;
2375 }
2376
2377 static void
2378 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2379                      enum mf_field_id field, uint64_t value_)
2380 {
2381     struct ofp12_action_set_field *oasf OVS_UNUSED;
2382     int n_bytes = mf_from_id(field)->n_bytes;
2383     size_t start_ofs = openflow->size;
2384     union mf_value value;
2385
2386     value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2387
2388     oasf = put_OFPAT12_SET_FIELD(openflow);
2389     openflow->size = openflow->size - sizeof oasf->pad;
2390     nx_put_entry(openflow, field, ofp_version, &value, NULL);
2391     pad_ofpat(openflow, start_ofs);
2392 }
2393
2394 static bool
2395 next_load_segment(const struct ofpact_set_field *sf,
2396                   struct mf_subfield *dst, uint64_t *value)
2397 {
2398     int n_bits = sf->field->n_bits;
2399     int n_bytes = sf->field->n_bytes;
2400     int start = dst->ofs + dst->n_bits;
2401
2402     if (start < n_bits) {
2403         dst->field = sf->field;
2404         dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2405         if (dst->ofs < n_bits) {
2406             dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2407                                        MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2408             *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2409             return true;
2410         }
2411     }
2412     return false;
2413 }
2414
2415 /* Convert 'sf' to a series of REG_LOADs. */
2416 static void
2417 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2418 {
2419     /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2420      * experimenter OXM or is variable length (or if it came in as
2421      * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2.  Otherwise use
2422      * NXAST_REG_LOAD, which is backward compatible. */
2423     if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2424         || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2425         struct nx_action_reg_load2 *narl OVS_UNUSED;
2426         size_t start_ofs = openflow->size;
2427
2428         narl = put_NXAST_REG_LOAD2(openflow);
2429         openflow->size = openflow->size - sizeof narl->pad;
2430         nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2431         pad_ofpat(openflow, start_ofs);
2432     } else {
2433         struct mf_subfield dst;
2434         uint64_t value;
2435
2436         dst.ofs = dst.n_bits = 0;
2437         while (next_load_segment(sf, &dst, &value)) {
2438             struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2439             narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2440             narl->dst = htonl(mf_nxm_header(dst.field->id));
2441             narl->value = htonll(value);
2442         }
2443     }
2444 }
2445
2446 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2447  * actions, if we can, falling back to Nicira extensions if we must.
2448  *
2449  * We check only meta-flow types that can appear within set field actions and
2450  * that have a mapping to compatible action types.  These struct mf_field
2451  * definitions have a defined OXM or NXM header value and specify the field as
2452  * writable. */
2453 static void
2454 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2455                              enum ofp_version ofp_version,
2456                              struct ofpbuf *out)
2457 {
2458     switch ((int) sf->field->id) {
2459     case MFF_VLAN_TCI: {
2460         ovs_be16 tci = sf->value.be16;
2461         bool cfi = (tci & htons(VLAN_CFI)) != 0;
2462         uint16_t vid = vlan_tci_to_vid(tci);
2463         uint8_t pcp = vlan_tci_to_pcp(tci);
2464
2465         if (ofp_version < OFP11_VERSION) {
2466             /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2467              *
2468              * If CFI=1, Add or modify VLAN VID & PCP.
2469              * If CFI=0, strip VLAN header, if any.
2470              */
2471             if (cfi) {
2472                 put_OFPAT10_SET_VLAN_VID(out, vid);
2473                 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2474             } else {
2475                 put_OFPAT10_STRIP_VLAN(out);
2476             }
2477         } else {
2478             /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2479              *
2480              * If CFI=1, Add or modify VLAN VID & PCP.
2481              *    OpenFlow 1.1 set actions only apply if the packet
2482              *    already has VLAN tags.  To be sure that is the case
2483              *    we have to push a VLAN header.  As we do not support
2484              *    multiple layers of VLANs, this is a no-op, if a VLAN
2485              *    header already exists.  This may backfire, however,
2486              *    when we start supporting multiple layers of VLANs.
2487              * If CFI=0, strip VLAN header, if any.
2488              */
2489             if (cfi) {
2490                 /* Push a VLAN tag, if one was not seen at action validation
2491                  * time. */
2492                 if (!sf->flow_has_vlan) {
2493                     put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2494                 }
2495                 put_OFPAT11_SET_VLAN_VID(out, vid);
2496                 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2497             } else {
2498                 /* If the flow did not match on vlan, we have no way of
2499                  * knowing if the vlan tag exists, so we must POP just to be
2500                  * sure. */
2501                 put_OFPAT11_POP_VLAN(out);
2502             }
2503         }
2504         break;
2505     }
2506
2507     case MFF_VLAN_VID: {
2508         uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2509         if (ofp_version == OFP10_VERSION) {
2510             put_OFPAT10_SET_VLAN_VID(out, vid);
2511         } else {
2512             put_OFPAT11_SET_VLAN_VID(out, vid);
2513         }
2514         break;
2515     }
2516
2517     case MFF_VLAN_PCP:
2518         if (ofp_version == OFP10_VERSION) {
2519             put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2520         } else {
2521             put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2522         }
2523         break;
2524
2525     case MFF_ETH_SRC:
2526         put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2527         break;
2528
2529     case MFF_ETH_DST:
2530         put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2531         break;
2532
2533     case MFF_IPV4_SRC:
2534         put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2535         break;
2536
2537     case MFF_IPV4_DST:
2538         put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2539         break;
2540
2541     case MFF_IP_DSCP:
2542         put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2543         break;
2544
2545     case MFF_IP_DSCP_SHIFTED:
2546         put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2547         break;
2548
2549     case MFF_TCP_SRC:
2550     case MFF_UDP_SRC:
2551         put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2552         break;
2553
2554     case MFF_TCP_DST:
2555     case MFF_UDP_DST:
2556         put_OFPAT_SET_TP_DST(out, sf->value.be16);
2557         break;
2558
2559     default:
2560         set_field_to_nxast(sf, out);
2561         break;
2562     }
2563 }
2564
2565 static void
2566 set_field_to_set_field(const struct ofpact_set_field *sf,
2567                        enum ofp_version ofp_version, struct ofpbuf *out)
2568 {
2569     struct ofp12_action_set_field *oasf OVS_UNUSED;
2570     size_t start_ofs = out->size;
2571
2572     oasf = put_OFPAT12_SET_FIELD(out);
2573     out->size = out->size - sizeof oasf->pad;
2574     nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2575     pad_ofpat(out, start_ofs);
2576 }
2577
2578 static void
2579 encode_SET_FIELD(const struct ofpact_set_field *sf,
2580                  enum ofp_version ofp_version, struct ofpbuf *out)
2581 {
2582     if (ofp_version >= OFP15_VERSION) {
2583         /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2584          * entirely). */
2585         set_field_to_set_field(sf, ofp_version, out);
2586     } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2587                sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2588         /* It came in as reg_load, send it out the same way. */
2589         set_field_to_nxast(sf, out);
2590     } else if (ofp_version < OFP12_VERSION) {
2591         /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2592         set_field_to_legacy_openflow(sf, ofp_version, out);
2593     } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2594         /* We're encoding to OpenFlow 1.2, 1.3, or 1.4.  The action sets an
2595          * entire field, so encode it as OFPAT_SET_FIELD. */
2596         set_field_to_set_field(sf, ofp_version, out);
2597     } else {
2598         /* We're encoding to OpenFlow 1.2, 1.3, or 1.4.  The action cannot be
2599          * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2600          * so encode it as reg_load. */
2601         set_field_to_nxast(sf, out);
2602     }
2603 }
2604
2605 /* Parses the input argument 'arg' into the key, value, and delimiter
2606  * components that are common across the reg_load and set_field action format.
2607  *
2608  * With an argument like "1->metadata", sets the following pointers to
2609  * point within 'arg':
2610  * key: "metadata"
2611  * value: "1"
2612  * delim: "->"
2613  *
2614  * Returns NULL if successful, otherwise a malloc()'d string describing the
2615  * error.  The caller is responsible for freeing the returned string. */
2616 static char * OVS_WARN_UNUSED_RESULT
2617 set_field_split_str(char *arg, char **key, char **value, char **delim)
2618 {
2619     char *value_end;
2620
2621     *value = arg;
2622     value_end = strstr(arg, "->");
2623     *key = value_end + strlen("->");
2624     if (delim) {
2625         *delim = value_end;
2626     }
2627
2628     if (!value_end) {
2629         return xasprintf("%s: missing `->'", arg);
2630     }
2631     if (strlen(value_end) <= strlen("->")) {
2632         return xasprintf("%s: missing field name following `->'", arg);
2633     }
2634
2635     return NULL;
2636 }
2637
2638 /* Parses a "set_field" action with argument 'arg', appending the parsed
2639  * action to 'ofpacts'.
2640  *
2641  * Returns NULL if successful, otherwise a malloc()'d string describing the
2642  * error.  The caller is responsible for freeing the returned string. */
2643 static char * OVS_WARN_UNUSED_RESULT
2644 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2645                   enum ofputil_protocol *usable_protocols)
2646 {
2647     struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2648     char *value;
2649     char *delim;
2650     char *key;
2651     const struct mf_field *mf;
2652     char *error;
2653
2654     error = set_field_split_str(arg, &key, &value, &delim);
2655     if (error) {
2656         return error;
2657     }
2658
2659     mf = mf_from_name(key);
2660     if (!mf) {
2661         return xasprintf("%s is not a valid OXM field name", key);
2662     }
2663     if (!mf->writable) {
2664         return xasprintf("%s is read-only", key);
2665     }
2666     sf->field = mf;
2667     delim[0] = '\0';
2668     error = mf_parse(mf, value, &sf->value, &sf->mask);
2669     if (error) {
2670         return error;
2671     }
2672
2673     if (!mf_is_value_valid(mf, &sf->value)) {
2674         return xasprintf("%s is not a valid value for field %s", value, key);
2675     }
2676
2677     *usable_protocols &= mf->usable_protocols_exact;
2678     return NULL;
2679 }
2680
2681 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2682  * action to 'ofpacts'.
2683  *
2684  * Returns NULL if successful, otherwise a malloc()'d string describing the
2685  * error.  The caller is responsible for freeing the returned string. */
2686 static char * OVS_WARN_UNUSED_RESULT
2687 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2688                 enum ofputil_protocol *usable_protocols)
2689 {
2690     char *copy = xstrdup(arg);
2691     char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2692     free(copy);
2693     return error;
2694 }
2695
2696 static char * OVS_WARN_UNUSED_RESULT
2697 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2698 {
2699     struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2700     struct mf_subfield dst;
2701     char *key, *value_str;
2702     union mf_value value;
2703     char *error;
2704
2705     error = set_field_split_str(arg, &key, &value_str, NULL);
2706     if (error) {
2707         return error;
2708     }
2709
2710     error = mf_parse_subfield(&dst, key);
2711     if (error) {
2712         return error;
2713     }
2714
2715     if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2716                          &key)) {
2717         return xasprintf("%s: cannot parse integer value", arg);
2718     }
2719
2720     if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2721                               dst.field->n_bytes * 8 - dst.n_bits)) {
2722         struct ds ds;
2723
2724         ds_init(&ds);
2725         mf_format(dst.field, &value, NULL, &ds);
2726         error = xasprintf("%s: value %s does not fit into %d bits",
2727                           arg, ds_cstr(&ds), dst.n_bits);
2728         ds_destroy(&ds);
2729         return error;
2730     }
2731
2732     sf->field = dst.field;
2733     memset(&sf->value, 0, sizeof sf->value);
2734     bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2735                  dst.field->n_bytes, dst.ofs, dst.n_bits);
2736     bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2737
2738     return NULL;
2739 }
2740
2741 static void
2742 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2743 {
2744     if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2745         struct mf_subfield dst;
2746         uint64_t value;
2747
2748         dst.ofs = dst.n_bits = 0;
2749         while (next_load_segment(a, &dst, &value)) {
2750             ds_put_format(s, "load:%#"PRIx64"->", value);
2751             mf_format_subfield(&dst, s);
2752             ds_put_char(s, ',');
2753         }
2754         ds_chomp(s, ',');
2755     } else {
2756         ds_put_cstr(s, "set_field:");
2757         mf_format(a->field, &a->value, &a->mask, s);
2758         ds_put_format(s, "->%s", a->field->name);
2759     }
2760 }
2761
2762 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it.  The ofpact
2763  * is marked such that, if possible, it will be translated to OpenFlow as
2764  * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2765  * that was the way that the action was expressed when it came into OVS or for
2766  * backward compatibility. */
2767 struct ofpact_set_field *
2768 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2769 {
2770     struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2771     sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2772     return sf;
2773 }
2774 \f
2775 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2776  *
2777  * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2778  * top of the stack.
2779  */
2780 struct nx_action_stack {
2781     ovs_be16 type;                  /* OFPAT_VENDOR. */
2782     ovs_be16 len;                   /* Length is 16. */
2783     ovs_be32 vendor;                /* NX_VENDOR_ID. */
2784     ovs_be16 subtype;               /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2785     ovs_be16 offset;                /* Bit offset into the field. */
2786     /* Followed by:
2787      * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2788      * - ovs_be16 'n_bits', the number of bits to extract from the field.
2789      * - Enough 0-bytes to pad out the action to 24 bytes. */
2790     uint8_t pad[12];                /* See above. */
2791 };
2792 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2793
2794 static enum ofperr
2795 decode_stack_action(const struct nx_action_stack *nasp,
2796                     struct ofpact_stack *stack_action)
2797 {
2798     enum ofperr error;
2799     struct ofpbuf b;
2800
2801     stack_action->subfield.ofs = ntohs(nasp->offset);
2802
2803     ofpbuf_use_const(&b, nasp, sizeof *nasp);
2804     ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2805     error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2806     if (error) {
2807         return error;
2808     }
2809     stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2810     ofpbuf_pull(&b, 2);
2811     if (!is_all_zeros(b.data, b.size)) {
2812         return OFPERR_NXBRC_MUST_BE_ZERO;
2813     }
2814
2815     return 0;
2816 }
2817
2818 static enum ofperr
2819 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2820                             enum ofp_version ofp_version OVS_UNUSED,
2821                             struct ofpbuf *ofpacts)
2822 {
2823     struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2824     enum ofperr error = decode_stack_action(nasp, push);
2825     return error ? error : nxm_stack_push_check(push, NULL);
2826 }
2827
2828 static enum ofperr
2829 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2830                            enum ofp_version ofp_version OVS_UNUSED,
2831                            struct ofpbuf *ofpacts)
2832 {
2833     struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2834     enum ofperr error = decode_stack_action(nasp, pop);
2835     return error ? error : nxm_stack_pop_check(pop, NULL);
2836 }
2837
2838 static void
2839 encode_STACK_op(const struct ofpact_stack *stack_action,
2840                 struct nx_action_stack *nasp)
2841 {
2842     struct ofpbuf b;
2843     ovs_be16 n_bits;
2844
2845     nasp->offset = htons(stack_action->subfield.ofs);
2846
2847     ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2848     ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2849     nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2850     n_bits = htons(stack_action->subfield.n_bits);
2851     ofpbuf_put(&b, &n_bits, sizeof n_bits);
2852 }
2853
2854 static void
2855 encode_STACK_PUSH(const struct ofpact_stack *stack,
2856                   enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2857 {
2858     encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2859 }
2860
2861 static void
2862 encode_STACK_POP(const struct ofpact_stack *stack,
2863                  enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2864 {
2865     encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2866 }
2867
2868 static char * OVS_WARN_UNUSED_RESULT
2869 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2870                  enum ofputil_protocol *usable_protocols OVS_UNUSED)
2871 {
2872     return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2873 }
2874
2875 static char * OVS_WARN_UNUSED_RESULT
2876 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2877                 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2878 {
2879     return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2880 }
2881
2882 static void
2883 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2884 {
2885     nxm_format_stack_push(a, s);
2886 }
2887
2888 static void
2889 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2890 {
2891     nxm_format_stack_pop(a, s);
2892 }
2893 \f
2894 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2895  *
2896  * If the packet is not IPv4 or IPv6, does nothing.  For IPv4 or IPv6, if the
2897  * TTL or hop limit is at least 2, decrements it by 1.  Otherwise, if TTL or
2898  * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2899  * 'n_controllers' controller IDs specified in 'cnt_ids'.
2900  *
2901  * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2902  * sent only to controllers with id 0.)
2903  */
2904 struct nx_action_cnt_ids {
2905     ovs_be16 type;              /* OFPAT_VENDOR. */
2906     ovs_be16 len;               /* Length including slaves. */
2907     ovs_be32 vendor;            /* NX_VENDOR_ID. */
2908     ovs_be16 subtype;           /* NXAST_DEC_TTL_CNT_IDS. */
2909
2910     ovs_be16 n_controllers;     /* Number of controllers. */
2911     uint8_t zeros[4];           /* Must be zero. */
2912
2913     /* Followed by 1 or more controller ids.
2914      *
2915      * uint16_t cnt_ids[];        // Controller ids.
2916      * uint8_t pad[];           // Must be 0 to 8-byte align cnt_ids[].
2917      */
2918 };
2919 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2920
2921 static enum ofperr
2922 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2923 {
2924     uint16_t id = 0;
2925     struct ofpact_cnt_ids *ids;
2926     enum ofperr error = 0;
2927
2928     ids = ofpact_put_DEC_TTL(out);
2929     ids->n_controllers = 1;
2930     ofpbuf_put(out, &id, sizeof id);
2931     ids = out->header;
2932     ofpact_update_len(out, &ids->ofpact);
2933     return error;
2934 }
2935
2936 static enum ofperr
2937 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2938                                  enum ofp_version ofp_version OVS_UNUSED,
2939                                  struct ofpbuf *out)
2940 {
2941     struct ofpact_cnt_ids *ids;
2942     size_t ids_size;
2943     int i;
2944
2945     ids = ofpact_put_DEC_TTL(out);
2946     ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2947     ids->n_controllers = ntohs(nac_ids->n_controllers);
2948     ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2949
2950     if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2951         return OFPERR_NXBRC_MUST_BE_ZERO;
2952     }
2953
2954     if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2955         VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2956                      "bytes allocated for controller ids.  %"PRIuSIZE" bytes "
2957                      "are required for %"PRIu16" controllers.",
2958                      ids_size, ids->n_controllers * sizeof(ovs_be16),
2959                      ids->n_controllers);
2960         return OFPERR_OFPBAC_BAD_LEN;
2961     }
2962
2963     for (i = 0; i < ids->n_controllers; i++) {
2964         uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2965         ofpbuf_put(out, &id, sizeof id);
2966         ids = out->header;
2967     }
2968
2969     ofpact_update_len(out, &ids->ofpact);
2970
2971     return 0;
2972 }
2973
2974 static void
2975 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2976                enum ofp_version ofp_version, struct ofpbuf *out)
2977 {
2978     if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2979         || dec_ttl->n_controllers != 1
2980         || dec_ttl->cnt_ids[0] != 0) {
2981         struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2982         int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2983         ovs_be16 *ids;
2984         size_t i;
2985
2986         nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2987         nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2988
2989         ids = ofpbuf_put_zeros(out, ids_len);
2990         for (i = 0; i < dec_ttl->n_controllers; i++) {
2991             ids[i] = htons(dec_ttl->cnt_ids[i]);
2992         }
2993     } else {
2994         put_OFPAT_DEC_NW_TTL(out, ofp_version);
2995     }
2996 }
2997
2998 static void
2999 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
3000 {
3001     struct ofpact_cnt_ids *ids;
3002     uint16_t id = 0;
3003
3004     ofpact_put_DEC_TTL(ofpacts);
3005     ofpbuf_put(ofpacts, &id, sizeof id);
3006     ids = ofpacts->header;
3007     ids->n_controllers++;
3008     ofpact_update_len(ofpacts, &ids->ofpact);
3009 }
3010
3011 static char * OVS_WARN_UNUSED_RESULT
3012 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
3013               enum ofputil_protocol *usable_protocols OVS_UNUSED)
3014 {
3015     if (*arg == '\0') {
3016         parse_noargs_dec_ttl(ofpacts);
3017     } else {
3018         struct ofpact_cnt_ids *ids;
3019         char *cntr;
3020
3021         ids = ofpact_put_DEC_TTL(ofpacts);
3022         ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3023         for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
3024              cntr = strtok_r(NULL, ", ", &arg)) {
3025             uint16_t id = atoi(cntr);
3026
3027             ofpbuf_put(ofpacts, &id, sizeof id);
3028             ids = ofpacts->header;
3029             ids->n_controllers++;
3030         }
3031         if (!ids->n_controllers) {
3032             return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3033                            "id.");
3034         }
3035         ofpact_update_len(ofpacts, &ids->ofpact);
3036     }
3037     return NULL;
3038 }
3039
3040 static void
3041 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
3042 {
3043     size_t i;
3044
3045     ds_put_cstr(s, "dec_ttl");
3046     if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
3047         ds_put_cstr(s, "(");
3048         for (i = 0; i < a->n_controllers; i++) {
3049             if (i) {
3050                 ds_put_cstr(s, ",");
3051             }
3052             ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
3053         }
3054         ds_put_cstr(s, ")");
3055     }
3056 }
3057 \f
3058 /* Set MPLS label actions. */
3059
3060 static enum ofperr
3061 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3062                                 enum ofp_version ofp_version OVS_UNUSED,
3063                                 struct ofpbuf *out)
3064 {
3065     ofpact_put_SET_MPLS_LABEL(out)->label = label;
3066     return 0;
3067 }
3068
3069 static void
3070 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3071                       enum ofp_version ofp_version,
3072                                   struct ofpbuf *out)
3073 {
3074     if (ofp_version < OFP12_VERSION) {
3075         put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3076     } else {
3077         ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3078                              ntohl(label->label));
3079     }
3080 }
3081
3082 static char * OVS_WARN_UNUSED_RESULT
3083 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3084                      enum ofputil_protocol *usable_protocols OVS_UNUSED)
3085 {
3086     struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3087     if (*arg == '\0') {
3088         return xstrdup("set_mpls_label: expected label.");
3089     }
3090
3091     mpls_label->label = htonl(atoi(arg));
3092     return NULL;
3093 }
3094
3095 static void
3096 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3097 {
3098     ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
3099 }
3100 \f
3101 /* Set MPLS TC actions. */
3102
3103 static enum ofperr
3104 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3105                              enum ofp_version ofp_version OVS_UNUSED,
3106                              struct ofpbuf *out)
3107 {
3108     ofpact_put_SET_MPLS_TC(out)->tc = tc;
3109     return 0;
3110 }
3111
3112 static void
3113 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3114                    enum ofp_version ofp_version, struct ofpbuf *out)
3115 {
3116     if (ofp_version < OFP12_VERSION) {
3117         put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3118     } else {
3119         ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3120     }
3121 }
3122
3123 static char * OVS_WARN_UNUSED_RESULT
3124 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3125                   enum ofputil_protocol *usable_protocols OVS_UNUSED)
3126 {
3127     struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3128
3129     if (*arg == '\0') {
3130         return xstrdup("set_mpls_tc: expected tc.");
3131     }
3132
3133     mpls_tc->tc = atoi(arg);
3134     return NULL;
3135 }
3136
3137 static void
3138 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3139 {
3140     ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3141 }
3142 \f
3143 /* Set MPLS TTL actions. */
3144
3145 static enum ofperr
3146 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3147                               enum ofp_version ofp_version OVS_UNUSED,
3148                               struct ofpbuf *out)
3149 {
3150     ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3151     return 0;
3152 }
3153
3154 static void
3155 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3156                     enum ofp_version ofp_version, struct ofpbuf *out)
3157 {
3158     put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3159 }
3160
3161 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3162  * action to 'ofpacts'.
3163  *
3164  * Returns NULL if successful, otherwise a malloc()'d string describing the
3165  * error.  The caller is responsible for freeing the returned string. */
3166 static char * OVS_WARN_UNUSED_RESULT
3167 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3168                    enum ofputil_protocol *usable_protocols OVS_UNUSED)
3169 {
3170     struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3171
3172     if (*arg == '\0') {
3173         return xstrdup("set_mpls_ttl: expected ttl.");
3174     }
3175
3176     mpls_ttl->ttl = atoi(arg);
3177     return NULL;
3178 }
3179
3180 static void
3181 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3182 {
3183     ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3184 }
3185 \f
3186 /* Decrement MPLS TTL actions. */
3187
3188 static enum ofperr
3189 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3190 {
3191     ofpact_put_DEC_MPLS_TTL(out);
3192     return 0;
3193 }
3194
3195 static void
3196 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3197                     enum ofp_version ofp_version, struct ofpbuf *out)
3198 {
3199     put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3200 }
3201
3202 static char * OVS_WARN_UNUSED_RESULT
3203 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3204                    enum ofputil_protocol *usable_protocols OVS_UNUSED)
3205 {
3206     ofpact_put_DEC_MPLS_TTL(ofpacts);
3207     return NULL;
3208 }
3209
3210 static void
3211 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3212 {
3213     ds_put_cstr(s, "dec_mpls_ttl");
3214 }
3215 \f
3216 /* Push MPLS label action. */
3217
3218 static enum ofperr
3219 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3220                            enum ofp_version ofp_version OVS_UNUSED,
3221                            struct ofpbuf *out)
3222 {
3223     struct ofpact_push_mpls *oam;
3224
3225     if (!eth_type_mpls(ethertype)) {
3226         return OFPERR_OFPBAC_BAD_ARGUMENT;
3227     }
3228     oam = ofpact_put_PUSH_MPLS(out);
3229     oam->ethertype = ethertype;
3230
3231     return 0;
3232 }
3233
3234 static void
3235 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3236                  enum ofp_version ofp_version, struct ofpbuf *out)
3237 {
3238     put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3239 }
3240
3241 static char * OVS_WARN_UNUSED_RESULT
3242 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3243                 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3244 {
3245     uint16_t ethertype;
3246     char *error;
3247
3248     error = str_to_u16(arg, "push_mpls", &ethertype);
3249     if (!error) {
3250         ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3251     }
3252     return error;
3253 }
3254
3255 static void
3256 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3257 {
3258     ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3259 }
3260 \f
3261 /* Pop MPLS label action. */
3262
3263 static enum ofperr
3264 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3265                           enum ofp_version ofp_version OVS_UNUSED,
3266                           struct ofpbuf *out)
3267 {
3268     ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3269     return 0;
3270 }
3271
3272 static void
3273 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3274                 enum ofp_version ofp_version, struct ofpbuf *out)
3275 {
3276     put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3277 }
3278
3279 static char * OVS_WARN_UNUSED_RESULT
3280 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3281                     enum ofputil_protocol *usable_protocols OVS_UNUSED)
3282 {
3283     uint16_t ethertype;
3284     char *error;
3285
3286     error = str_to_u16(arg, "pop_mpls", &ethertype);
3287     if (!error) {
3288         ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3289     }
3290     return error;
3291 }
3292
3293 static void
3294 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3295 {
3296     ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3297 }
3298 \f
3299 /* Set tunnel ID actions. */
3300
3301 static enum ofperr
3302 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3303                             enum ofp_version ofp_version OVS_UNUSED,
3304                             struct ofpbuf *out)
3305 {
3306     struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3307     tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3308     tunnel->tun_id = tun_id;
3309     return 0;
3310 }
3311
3312 static enum ofperr
3313 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3314                               enum ofp_version ofp_version OVS_UNUSED,
3315                               struct ofpbuf *out)
3316 {
3317     struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3318     tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3319     tunnel->tun_id = tun_id;
3320     return 0;
3321 }
3322
3323 static void
3324 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3325                   enum ofp_version ofp_version, struct ofpbuf *out)
3326 {
3327     uint64_t tun_id = tunnel->tun_id;
3328
3329     if (ofp_version < OFP12_VERSION) {
3330         if (tun_id <= UINT32_MAX
3331             && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3332             put_NXAST_SET_TUNNEL(out, tun_id);
3333         } else {
3334             put_NXAST_SET_TUNNEL64(out, tun_id);
3335         }
3336     } else {
3337         ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3338     }
3339 }
3340
3341 static char * OVS_WARN_UNUSED_RESULT
3342 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3343                  enum ofp_raw_action_type raw)
3344 {
3345     struct ofpact_tunnel *tunnel;
3346
3347     tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3348     tunnel->ofpact.raw = raw;
3349     return str_to_u64(arg, &tunnel->tun_id);
3350 }
3351
3352 static char * OVS_WARN_UNUSED_RESULT
3353 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3354                  enum ofputil_protocol *usable_protocols OVS_UNUSED)
3355 {
3356     return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3357 }
3358
3359 static void
3360 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3361 {
3362     ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3363                   (a->tun_id > UINT32_MAX
3364                    || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3365                   a->tun_id);
3366 }
3367 \f
3368 /* Set queue action. */
3369
3370 static enum ofperr
3371 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3372                            enum ofp_version ofp_version OVS_UNUSED,
3373                            struct ofpbuf *out)
3374 {
3375     ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3376     return 0;
3377 }
3378
3379 static void
3380 encode_SET_QUEUE(const struct ofpact_queue *queue,
3381                  enum ofp_version ofp_version, struct ofpbuf *out)
3382 {
3383     put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3384 }
3385
3386 static char * OVS_WARN_UNUSED_RESULT
3387 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3388                 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3389 {
3390     return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3391 }
3392
3393 static void
3394 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3395 {
3396     ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3397 }
3398 \f
3399 /* Pop queue action. */
3400
3401 static enum ofperr
3402 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3403 {
3404     ofpact_put_POP_QUEUE(out);
3405     return 0;
3406 }
3407
3408 static void
3409 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3410                  enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3411 {
3412     put_NXAST_POP_QUEUE(out);
3413 }
3414
3415 static char * OVS_WARN_UNUSED_RESULT
3416 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3417                 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3418 {
3419     ofpact_put_POP_QUEUE(ofpacts);
3420     return NULL;
3421 }
3422
3423 static void
3424 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3425 {
3426     ds_put_cstr(s, "pop_queue");
3427 }
3428 \f
3429 /* Action structure for NXAST_FIN_TIMEOUT.
3430  *
3431  * This action changes the idle timeout or hard timeout, or both, of this
3432  * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3433  * When such a packet is observed, the action reduces the rule's idle timeout
3434  * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'.  This
3435  * action has no effect on an existing timeout that is already shorter than the
3436  * one that the action specifies.  A 'fin_idle_timeout' or 'fin_hard_timeout'
3437  * of zero has no effect on the respective timeout.
3438  *
3439  * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3440  * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3441  * receipt of the FIN or RST.
3442  *
3443  * This is useful for quickly discarding learned TCP flows that otherwise will
3444  * take a long time to expire.
3445  *
3446  * This action is intended for use with an OpenFlow rule that matches only a
3447  * single TCP flow.  If the rule matches multiple TCP flows (e.g. it wildcards
3448  * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3449  * RST in any of those flows will cause the entire OpenFlow rule to expire
3450  * early, which is not normally desirable.
3451  */
3452 struct nx_action_fin_timeout {
3453     ovs_be16 type;              /* OFPAT_VENDOR. */
3454     ovs_be16 len;               /* 16. */
3455     ovs_be32 vendor;            /* NX_VENDOR_ID. */
3456     ovs_be16 subtype;           /* NXAST_FIN_TIMEOUT. */
3457     ovs_be16 fin_idle_timeout;  /* New idle timeout, if nonzero. */
3458     ovs_be16 fin_hard_timeout;  /* New hard timeout, if nonzero. */
3459     ovs_be16 pad;               /* Must be zero. */
3460 };
3461 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3462
3463 static enum ofperr
3464 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3465                              enum ofp_version ofp_version OVS_UNUSED,
3466                              struct ofpbuf *out)
3467 {
3468     struct ofpact_fin_timeout *oft;
3469
3470     oft = ofpact_put_FIN_TIMEOUT(out);
3471     oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3472     oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3473     return 0;
3474 }
3475
3476 static void
3477 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3478                    enum ofp_version ofp_version OVS_UNUSED,
3479                    struct ofpbuf *out)
3480 {
3481     struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3482     naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3483     naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3484 }
3485
3486 static char * OVS_WARN_UNUSED_RESULT
3487 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3488                   enum ofputil_protocol *usable_protocols OVS_UNUSED)
3489 {
3490     struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3491     char *key, *value;
3492
3493     while (ofputil_parse_key_value(&arg, &key, &value)) {
3494         char *error;
3495
3496         if (!strcmp(key, "idle_timeout")) {
3497             error =  str_to_u16(value, key, &oft->fin_idle_timeout);
3498         } else if (!strcmp(key, "hard_timeout")) {
3499             error = str_to_u16(value, key, &oft->fin_hard_timeout);
3500         } else {
3501             error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3502                               key);
3503         }
3504
3505         if (error) {
3506             return error;
3507         }
3508     }
3509     return NULL;
3510 }
3511
3512 static void
3513 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3514 {
3515     ds_put_cstr(s, "fin_timeout(");
3516     if (a->fin_idle_timeout) {
3517         ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3518     }
3519     if (a->fin_hard_timeout) {
3520         ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3521     }
3522     ds_chomp(s, ',');
3523     ds_put_char(s, ')');
3524 }
3525 \f
3526 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3527  *
3528  * These actions search one of the switch's flow tables:
3529  *
3530  *    - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3531  *      it specifies the table to search.
3532  *
3533  *    - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3534  *      NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3535  *      table, that is, the OpenFlow flow table that contains the flow from
3536  *      which this action was obtained.  If this action did not come from a
3537  *      flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3538  *      is the current table.
3539  *
3540  * The flow table lookup uses a flow that may be slightly modified from the
3541  * original lookup:
3542  *
3543  *    - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3544  *      is used as the flow's in_port.
3545  *
3546  *    - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3547  *      then its value is used as the flow's in_port.  Otherwise, the original
3548  *      in_port is used.
3549  *
3550  *    - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3551  *      resubmit action, then the flow is updated with the new values.
3552  *
3553  * Following the lookup, the original in_port is restored.
3554  *
3555  * If the modified flow matched in the flow table, then the corresponding
3556  * actions are executed.  Afterward, actions following the resubmit in the
3557  * original set of actions, if any, are executed; any changes made to the
3558  * packet (e.g. changes to VLAN) by secondary actions persist when those
3559  * actions are executed, although the original in_port is restored.
3560  *
3561  * Resubmit actions may be used any number of times within a set of actions.
3562  *
3563  * Resubmit actions may nest to an implementation-defined depth.  Beyond this
3564  * implementation-defined depth, further resubmit actions are simply ignored.
3565  *
3566  * NXAST_RESUBMIT ignores 'table' and 'pad'.  NXAST_RESUBMIT_TABLE requires
3567  * 'pad' to be all-bits-zero.
3568  *
3569  * Open vSwitch 1.0.1 and earlier did not support recursion.  Open vSwitch
3570  * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3571  */
3572 struct nx_action_resubmit {
3573     ovs_be16 type;                  /* OFPAT_VENDOR. */
3574     ovs_be16 len;                   /* Length is 16. */
3575     ovs_be32 vendor;                /* NX_VENDOR_ID. */
3576     ovs_be16 subtype;               /* NXAST_RESUBMIT. */
3577     ovs_be16 in_port;               /* New in_port for checking flow table. */
3578     uint8_t table;                  /* NXAST_RESUBMIT_TABLE: table to use. */
3579     uint8_t pad[3];
3580 };
3581 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3582
3583 static enum ofperr
3584 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3585                           enum ofp_version ofp_version OVS_UNUSED,
3586                           struct ofpbuf *out)
3587 {
3588     struct ofpact_resubmit *resubmit;
3589
3590     resubmit = ofpact_put_RESUBMIT(out);
3591     resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3592     resubmit->in_port = u16_to_ofp(port);
3593     resubmit->table_id = 0xff;
3594     return 0;
3595 }
3596
3597 static enum ofperr
3598 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3599                                 enum ofp_version ofp_version OVS_UNUSED,
3600                                 struct ofpbuf *out)
3601 {
3602     struct ofpact_resubmit *resubmit;
3603
3604     if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3605         return OFPERR_OFPBAC_BAD_ARGUMENT;
3606     }
3607
3608     resubmit = ofpact_put_RESUBMIT(out);
3609     resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3610     resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3611     resubmit->table_id = nar->table;
3612     return 0;
3613 }
3614
3615 static void
3616 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3617                 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3618 {
3619     uint16_t in_port = ofp_to_u16(resubmit->in_port);
3620
3621     if (resubmit->table_id == 0xff
3622         && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3623         put_NXAST_RESUBMIT(out, in_port);
3624     } else {
3625         struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3626         nar->table = resubmit->table_id;
3627         nar->in_port = htons(in_port);
3628     }
3629 }
3630
3631 static char * OVS_WARN_UNUSED_RESULT
3632 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3633                enum ofputil_protocol *usable_protocols OVS_UNUSED)
3634 {
3635     struct ofpact_resubmit *resubmit;
3636     char *in_port_s, *table_s;
3637
3638     resubmit = ofpact_put_RESUBMIT(ofpacts);
3639
3640     in_port_s = strsep(&arg, ",");
3641     if (in_port_s && in_port_s[0]) {
3642         if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3643             return xasprintf("%s: resubmit to unknown port", in_port_s);
3644         }
3645     } else {
3646         resubmit->in_port = OFPP_IN_PORT;
3647     }
3648
3649     table_s = strsep(&arg, ",");
3650     if (table_s && table_s[0]) {
3651         uint32_t table_id = 0;
3652         char *error;
3653
3654         error = str_to_u32(table_s, &table_id);
3655         if (error) {
3656             return error;
3657         }
3658         resubmit->table_id = table_id;
3659     } else {
3660         resubmit->table_id = 255;
3661     }
3662
3663     if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3664         return xstrdup("at least one \"in_port\" or \"table\" must be "
3665                        "specified  on resubmit");
3666     }
3667     return NULL;
3668 }
3669
3670 static void
3671 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3672 {
3673     if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3674         ds_put_cstr(s, "resubmit:");
3675         ofputil_format_port(a->in_port, s);
3676     } else {
3677         ds_put_format(s, "resubmit(");
3678         if (a->in_port != OFPP_IN_PORT) {
3679             ofputil_format_port(a->in_port, s);
3680         }
3681         ds_put_char(s, ',');
3682         if (a->table_id != 255) {
3683             ds_put_format(s, "%"PRIu8, a->table_id);
3684         }
3685         ds_put_char(s, ')');
3686     }
3687 }
3688 \f
3689 /* Action structure for NXAST_LEARN.
3690  *
3691  * This action adds or modifies a flow in an OpenFlow table, similar to
3692  * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'.  The new flow has the
3693  * specified idle timeout, hard timeout, priority, cookie, and flags.  The new
3694  * flow's match criteria and actions are built by applying each of the series
3695  * of flow_mod_spec elements included as part of the action.
3696  *
3697  * A flow_mod_spec starts with a 16-bit header.  A header that is all-bits-0 is
3698  * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3699  * length.  Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3700  * bits from a source to a destination.  In this case, the header contains
3701  * multiple fields:
3702  *
3703  *  15  14  13 12  11 10                              0
3704  * +------+---+------+---------------------------------+
3705  * |   0  |src|  dst |             n_bits              |
3706  * +------+---+------+---------------------------------+
3707  *
3708  * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'.  The
3709  * following table summarizes the meaning of each possible combination.
3710  * Details follow the table:
3711  *
3712  *   src dst  meaning
3713  *   --- ---  ----------------------------------------------------------
3714  *    0   0   Add match criteria based on value in a field.
3715  *    1   0   Add match criteria based on an immediate value.
3716  *    0   1   Add NXAST_REG_LOAD action to copy field into a different field.
3717  *    1   1   Add NXAST_REG_LOAD action to load immediate value into a field.
3718  *    0   2   Add OFPAT_OUTPUT action to output to port from specified field.
3719  *   All other combinations are undefined and not allowed.
3720  *
3721  * The flow_mod_spec header is followed by a source specification and a
3722  * destination specification.  The format and meaning of the source
3723  * specification depends on 'src':
3724  *
3725  *   - If 'src' is 0, the source bits are taken from a field in the flow to
3726  *     which this action is attached.  (This should be a wildcarded field.  If
3727  *     its value is fully specified then the source bits being copied have
3728  *     constant values.)
3729  *
3730  *     The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3731  *     'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3732  *     offset within that field.  The source bits are field[ofs:ofs+n_bits-1].
3733  *     'field' and 'ofs' are subject to the same restrictions as the source
3734  *     field in NXAST_REG_MOVE.
3735  *
3736  *   - If 'src' is 1, the source bits are a constant value.  The source
3737  *     specification is (n_bits+15)/16*2 bytes long.  Taking those bytes as a
3738  *     number in network order, the source bits are the 'n_bits'
3739  *     least-significant bits.  The switch will report an error if other bits
3740  *     in the constant are nonzero.
3741  *
3742  * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3743  * ovs_be32 'field' and an ovs_be16 'ofs'.  'field' is an nxm_header with
3744  * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field.  The
3745  * meaning of the flow_mod_spec depends on 'dst':
3746  *
3747  *   - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3748  *     flow.  The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3749  *     packet equal the source bits.  'field' may be any nxm_header with
3750  *     nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3751  *
3752  *     Order is significant.  Earlier flow_mod_specs must satisfy any
3753  *     prerequisites for matching fields specified later, by copying constant
3754  *     values into prerequisite fields.
3755  *
3756  *     The switch will reject flow_mod_specs that do not satisfy NXM masking
3757  *     restrictions.
3758  *
3759  *   - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3760  *     the new flow.  The new flow copies the source bits into
3761  *     field[ofs:ofs+n_bits-1].  Actions are executed in the same order as the
3762  *     flow_mod_specs.
3763  *
3764  *     A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3765  *     greater than 64 yields multiple NXAST_REG_LOAD actions.
3766  *
3767  * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3768  * empty.  It has the following meaning:
3769  *
3770  *   - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3771  *     The new flow outputs to the OpenFlow port specified by the source field.
3772  *     Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3773  *     OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported.  Other special ports
3774  *     may not be used.
3775  *
3776  * Resource Management
3777  * -------------------
3778  *
3779  * A switch has a finite amount of flow table space available for learning.
3780  * When this space is exhausted, no new learning table entries will be learned
3781  * until some existing flow table entries expire.  The controller should be
3782  * prepared to handle this by flooding (which can be implemented as a
3783  * low-priority flow).
3784  *
3785  * If a learned flow matches a single TCP stream with a relatively long
3786  * timeout, one may make the best of resource constraints by setting
3787  * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3788  * both, to shorter timeouts.  When either of these is specified as a nonzero
3789  * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3790  * the learned flow.
3791  *
3792  * Examples
3793  * --------
3794  *
3795  * The following examples give a prose description of the flow_mod_specs along
3796  * with informal notation for how those would be represented and a hex dump of
3797  * the bytes that would be required.
3798  *
3799  * These examples could work with various nx_action_learn parameters.  Typical
3800  * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3801  * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3802  *
3803  * 1. Learn input port based on the source MAC, with lookup into
3804  *    NXM_NX_REG1[16:31] by resubmit to in_port=99:
3805  *
3806  *    Match on in_port=99:
3807  *       ovs_be16(src=1, dst=0, n_bits=16),               20 10
3808  *       ovs_be16(99),                                    00 63
3809  *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
3810  *
3811  *    Match Ethernet destination on Ethernet source from packet:
3812  *       ovs_be16(src=0, dst=0, n_bits=48),               00 30
3813  *       ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0)            00 00 04 06 00 00
3814  *       ovs_be32(NXM_OF_ETH_DST), ovs_be16(0)            00 00 02 06 00 00
3815  *
3816  *    Set NXM_NX_REG1[16:31] to the packet's input port:
3817  *       ovs_be16(src=0, dst=1, n_bits=16),               08 10
3818  *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
3819  *       ovs_be32(NXM_NX_REG1), ovs_be16(16)              00 01 02 04 00 10
3820  *
3821  *    Given a packet that arrived on port A with Ethernet source address B,
3822  *    this would set up the flow "in_port=99, dl_dst=B,
3823  *    actions=load:A->NXM_NX_REG1[16..31]".
3824  *
3825  *    In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3826  *    NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3827  *    load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3828  *
3829  * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3830  *    into NXM_NX_REG1[16:31]:
3831  *
3832  *    Match on same VLAN ID as packet:
3833  *       ovs_be16(src=0, dst=0, n_bits=12),               00 0c
3834  *       ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0)           00 00 08 02 00 00
3835  *       ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0)           00 00 08 02 00 00
3836  *
3837  *    Match Ethernet destination on Ethernet source from packet:
3838  *       ovs_be16(src=0, dst=0, n_bits=48),               00 30
3839  *       ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0)            00 00 04 06 00 00
3840  *       ovs_be32(NXM_OF_ETH_DST), ovs_be16(0)            00 00 02 06 00 00
3841  *
3842  *    Output to the packet's input port:
3843  *       ovs_be16(src=0, dst=2, n_bits=16),               10 10
3844  *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
3845  *
3846  *    Given a packet that arrived on port A with Ethernet source address B in
3847  *    VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3848  *    actions=output:A".
3849  *
3850  *    In syntax accepted by ovs-ofctl, this action is:
3851  *    learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3852  *    output:NXM_OF_IN_PORT[])
3853  *
3854  * 3. Here's a recipe for a very simple-minded MAC learning switch.  It uses a
3855  *    10-second MAC expiration time to make it easier to see what's going on
3856  *
3857  *      ovs-vsctl del-controller br0
3858  *      ovs-ofctl del-flows br0
3859  *      ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3860           hard_timeout=10, NXM_OF_VLAN_TCI[0..11],             \
3861           NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],                   \
3862           output:NXM_OF_IN_PORT[]), resubmit(,1)"
3863  *      ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3864  *
3865  *    You can then dump the MAC learning table with:
3866  *
3867  *      ovs-ofctl dump-flows br0 table=1
3868  *
3869  * Usage Advice
3870  * ------------
3871  *
3872  * For best performance, segregate learned flows into a table that is not used
3873  * for any other flows except possibly for a lowest-priority "catch-all" flow
3874  * (a flow with no match criteria).  If different learning actions specify
3875  * different match criteria, use different tables for the learned flows.
3876  *
3877  * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3878  * These timeouts apply to the flow that is added, which means that a flow with
3879  * an idle timeout will expire when no traffic has been sent *to* the learned
3880  * address.  This is not usually the intent in MAC learning; instead, we want
3881  * the MAC learn entry to expire when no traffic has been sent *from* the
3882  * learned address.  Use a hard timeout for that.
3883  *
3884  *
3885  * Visibility of Changes
3886  * ---------------------
3887  *
3888  * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3889  * flow translation are visible to flow table lookups made later in the flow
3890  * translation.  This means that, in the example above, a MAC learned by the
3891  * learn action in table 0 would be found in table 1 (if the packet being
3892  * processed had the same source and destination MAC address).
3893  *
3894  * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3895  * modify a flow) by a "learn" action are visible only for later flow
3896  * translations, not for later lookups within the same flow translation.  In
3897  * the MAC learning example, a MAC learned by the learn action in table 0 would
3898  * not be found in table 1 if the flow translation would resubmit to table 1
3899  * after the processing of the learn action, meaning that if this MAC had not
3900  * been learned before then the packet would be flooded. */
3901 struct nx_action_learn {
3902     ovs_be16 type;              /* OFPAT_VENDOR. */
3903     ovs_be16 len;               /* At least 24. */
3904     ovs_be32 vendor;            /* NX_VENDOR_ID. */
3905     ovs_be16 subtype;           /* NXAST_LEARN. */
3906     ovs_be16 idle_timeout;      /* Idle time before discarding (seconds). */
3907     ovs_be16 hard_timeout;      /* Max time before discarding (seconds). */
3908     ovs_be16 priority;          /* Priority level of flow entry. */
3909     ovs_be64 cookie;            /* Cookie for new flow. */
3910     ovs_be16 flags;             /* NX_LEARN_F_*. */
3911     uint8_t table_id;           /* Table to insert flow entry. */
3912     uint8_t pad;                /* Must be zero. */
3913     ovs_be16 fin_idle_timeout;  /* Idle timeout after FIN, if nonzero. */
3914     ovs_be16 fin_hard_timeout;  /* Hard timeout after FIN, if nonzero. */
3915     /* Followed by a sequence of flow_mod_spec elements, as described above,
3916      * until the end of the action is reached. */
3917 };
3918 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3919
3920 static ovs_be16
3921 get_be16(const void **pp)
3922 {
3923     const ovs_be16 *p = *pp;
3924     ovs_be16 value = *p;
3925     *pp = p + 1;
3926     return value;
3927 }
3928
3929 static ovs_be32
3930 get_be32(const void **pp)
3931 {
3932     const ovs_be32 *p = *pp;
3933     ovs_be32 value = get_unaligned_be32(p);
3934     *pp = p + 1;
3935     return value;
3936 }
3937
3938 static void
3939 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3940 {
3941     sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3942     sf->ofs = ntohs(get_be16(p));
3943     sf->n_bits = n_bits;
3944 }
3945
3946 static unsigned int
3947 learn_min_len(uint16_t header)
3948 {
3949     int n_bits = header & NX_LEARN_N_BITS_MASK;
3950     int src_type = header & NX_LEARN_SRC_MASK;
3951     int dst_type = header & NX_LEARN_DST_MASK;
3952     unsigned int min_len;
3953
3954     min_len = 0;
3955     if (src_type == NX_LEARN_SRC_FIELD) {
3956         min_len += sizeof(ovs_be32); /* src_field */
3957         min_len += sizeof(ovs_be16); /* src_ofs */
3958     } else {
3959         min_len += DIV_ROUND_UP(n_bits, 16);
3960     }
3961     if (dst_type == NX_LEARN_DST_MATCH ||
3962         dst_type == NX_LEARN_DST_LOAD) {
3963         min_len += sizeof(ovs_be32); /* dst_field */
3964         min_len += sizeof(ovs_be16); /* dst_ofs */
3965     }
3966     return min_len;
3967 }
3968
3969 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3970  * 'ofpacts'.  Returns 0 if successful, otherwise an OFPERR_*. */
3971 static enum ofperr
3972 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3973                        enum ofp_version ofp_version OVS_UNUSED,
3974                        struct ofpbuf *ofpacts)
3975 {
3976     struct ofpact_learn *learn;
3977     const void *p, *end;
3978
3979     if (nal->pad) {
3980         return OFPERR_OFPBAC_BAD_ARGUMENT;
3981     }
3982
3983     learn = ofpact_put_LEARN(ofpacts);
3984
3985     learn->idle_timeout = ntohs(nal->idle_timeout);
3986     learn->hard_timeout = ntohs(nal->hard_timeout);
3987     learn->priority = ntohs(nal->priority);
3988     learn->cookie = nal->cookie;
3989     learn->table_id = nal->table_id;
3990     learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3991     learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3992
3993     learn->flags = ntohs(nal->flags);
3994     if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3995                          NX_LEARN_F_DELETE_LEARNED)) {
3996         return OFPERR_OFPBAC_BAD_ARGUMENT;
3997     }
3998
3999     if (learn->table_id == 0xff) {
4000         return OFPERR_OFPBAC_BAD_ARGUMENT;
4001     }
4002
4003     end = (char *) nal + ntohs(nal->len);
4004     for (p = nal + 1; p != end; ) {
4005         struct ofpact_learn_spec *spec;
4006         uint16_t header = ntohs(get_be16(&p));
4007
4008         if (!header) {
4009             break;
4010         }
4011
4012         spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
4013         learn = ofpacts->header;
4014         learn->n_specs++;
4015
4016         spec->src_type = header & NX_LEARN_SRC_MASK;
4017         spec->dst_type = header & NX_LEARN_DST_MASK;
4018         spec->n_bits = header & NX_LEARN_N_BITS_MASK;
4019
4020         /* Check for valid src and dst type combination. */
4021         if (spec->dst_type == NX_LEARN_DST_MATCH ||
4022             spec->dst_type == NX_LEARN_DST_LOAD ||
4023             (spec->dst_type == NX_LEARN_DST_OUTPUT &&
4024              spec->src_type == NX_LEARN_SRC_FIELD)) {
4025             /* OK. */
4026         } else {
4027             return OFPERR_OFPBAC_BAD_ARGUMENT;
4028         }
4029
4030         /* Check that the arguments don't overrun the end of the action. */
4031         if ((char *) end - (char *) p < learn_min_len(header)) {
4032             return OFPERR_OFPBAC_BAD_LEN;
4033         }
4034
4035         /* Get the source. */
4036         if (spec->src_type == NX_LEARN_SRC_FIELD) {
4037             get_subfield(spec->n_bits, &p, &spec->src);
4038         } else {
4039             int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4040
4041             bitwise_copy(p, p_bytes, 0,
4042                          &spec->src_imm, sizeof spec->src_imm, 0,
4043                          spec->n_bits);
4044             p = (const uint8_t *) p + p_bytes;
4045         }
4046
4047         /* Get the destination. */
4048         if (spec->dst_type == NX_LEARN_DST_MATCH ||
4049             spec->dst_type == NX_LEARN_DST_LOAD) {
4050             get_subfield(spec->n_bits, &p, &spec->dst);
4051         }
4052     }
4053     ofpact_update_len(ofpacts, &learn->ofpact);
4054
4055     if (!is_all_zeros(p, (char *) end - (char *) p)) {
4056         return OFPERR_OFPBAC_BAD_ARGUMENT;
4057     }
4058
4059     return 0;
4060 }
4061
4062 static void
4063 put_be16(struct ofpbuf *b, ovs_be16 x)
4064 {
4065     ofpbuf_put(b, &x, sizeof x);
4066 }
4067
4068 static void
4069 put_be32(struct ofpbuf *b, ovs_be32 x)
4070 {
4071     ofpbuf_put(b, &x, sizeof x);
4072 }
4073
4074 static void
4075 put_u16(struct ofpbuf *b, uint16_t x)
4076 {
4077     put_be16(b, htons(x));
4078 }
4079
4080 static void
4081 put_u32(struct ofpbuf *b, uint32_t x)
4082 {
4083     put_be32(b, htonl(x));
4084 }
4085
4086 static void
4087 encode_LEARN(const struct ofpact_learn *learn,
4088              enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4089 {
4090     const struct ofpact_learn_spec *spec;
4091     struct nx_action_learn *nal;
4092     size_t start_ofs;
4093
4094     start_ofs = out->size;
4095     nal = put_NXAST_LEARN(out);
4096     nal->idle_timeout = htons(learn->idle_timeout);
4097     nal->hard_timeout = htons(learn->hard_timeout);
4098     nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4099     nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4100     nal->priority = htons(learn->priority);
4101     nal->cookie = learn->cookie;
4102     nal->flags = htons(learn->flags);
4103     nal->table_id = learn->table_id;
4104
4105     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4106         put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4107
4108         if (spec->src_type == NX_LEARN_SRC_FIELD) {
4109             put_u32(out, mf_nxm_header(spec->src.field->id));
4110             put_u16(out, spec->src.ofs);
4111         } else {
4112             size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4113             uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4114             bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4115                          bits, n_dst_bytes, 0,
4116                          spec->n_bits);
4117         }
4118
4119         if (spec->dst_type == NX_LEARN_DST_MATCH ||
4120             spec->dst_type == NX_LEARN_DST_LOAD) {
4121             put_u32(out, mf_nxm_header(spec->dst.field->id));
4122             put_u16(out, spec->dst.ofs);
4123         }
4124     }
4125
4126     pad_ofpat(out, start_ofs);
4127 }
4128
4129 static char * OVS_WARN_UNUSED_RESULT
4130 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4131             enum ofputil_protocol *usable_protocols OVS_UNUSED)
4132 {
4133     return learn_parse(arg, ofpacts);
4134 }
4135
4136 static void
4137 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4138 {
4139     learn_format(a, s);
4140 }
4141 \f
4142 /* Action structure for NXAST_CONJUNCTION. */
4143 struct nx_action_conjunction {
4144     ovs_be16 type;                  /* OFPAT_VENDOR. */
4145     ovs_be16 len;                   /* At least 16. */
4146     ovs_be32 vendor;                /* NX_VENDOR_ID. */
4147     ovs_be16 subtype;               /* See enum ofp_raw_action_type. */
4148     uint8_t clause;
4149     uint8_t n_clauses;
4150     ovs_be32 id;
4151 };
4152 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4153
4154 static void
4155 add_conjunction(struct ofpbuf *out,
4156                 uint32_t id, uint8_t clause, uint8_t n_clauses)
4157 {
4158     struct ofpact_conjunction *oc;
4159
4160     oc = ofpact_put_CONJUNCTION(out);
4161     oc->id = id;
4162     oc->clause = clause;
4163     oc->n_clauses = n_clauses;
4164 }
4165
4166 static enum ofperr
4167 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4168                              enum ofp_version ofp_version OVS_UNUSED,
4169                              struct ofpbuf *out)
4170 {
4171     if (nac->n_clauses < 2 || nac->n_clauses > 64
4172         || nac->clause >= nac->n_clauses) {
4173         return OFPERR_NXBAC_BAD_CONJUNCTION;
4174     } else {
4175         add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4176         return 0;
4177     }
4178 }
4179
4180 static void
4181 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4182                    enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4183 {
4184     struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4185     nac->clause = oc->clause;
4186     nac->n_clauses = oc->n_clauses;
4187     nac->id = htonl(oc->id);
4188 }
4189
4190 static void
4191 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4192 {
4193     ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4194                   oc->id, oc->clause + 1, oc->n_clauses);
4195 }
4196
4197 static char * OVS_WARN_UNUSED_RESULT
4198 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4199                   enum ofputil_protocol *usable_protocols OVS_UNUSED)
4200 {
4201     uint8_t n_clauses;
4202     uint8_t clause;
4203     uint32_t id;
4204     int n;
4205
4206     if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4207                   &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4208         return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4209     }
4210
4211     if (n_clauses < 2) {
4212         return xstrdup("conjunction must have at least 2 clauses");
4213     } else if (n_clauses > 64) {
4214         return xstrdup("conjunction must have at most 64 clauses");
4215     } else if (clause < 1) {
4216         return xstrdup("clause index must be positive");
4217     } else if (clause > n_clauses) {
4218         return xstrdup("clause index must be less than or equal to "
4219                        "number of clauses");
4220     }
4221
4222     add_conjunction(ofpacts, id, clause - 1, n_clauses);
4223     return NULL;
4224 }
4225 \f
4226 /* Action structure for NXAST_MULTIPATH.
4227  *
4228  * This action performs the following steps in sequence:
4229  *
4230  *    1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4231  *       Refer to the definition of "enum nx_mp_fields" for details.
4232  *
4233  *       The 'basis' value is used as a universal hash parameter, that is,
4234  *       different values of 'basis' yield different hash functions.  The
4235  *       particular universal hash function used is implementation-defined.
4236  *
4237  *       The hashed fields' values are drawn from the current state of the
4238  *       flow, including all modifications that have been made by actions up to
4239  *       this point.
4240  *
4241  *    2. Applies the multipath link choice algorithm specified by 'algorithm',
4242  *       one of NX_MP_ALG_*.  Refer to the definition of "enum nx_mp_algorithm"
4243  *       for details.
4244  *
4245  *       The output of the algorithm is 'link', an unsigned integer less than
4246  *       or equal to 'max_link'.
4247  *
4248  *       Some algorithms use 'arg' as an additional argument.
4249  *
4250  *    3. Stores 'link' in dst[ofs:ofs+n_bits].  The format and semantics of
4251  *       'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4252  *       action.
4253  *
4254  * The switch will reject actions that have an unknown 'fields', or an unknown
4255  * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4256  * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4257  * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4258  */
4259 struct nx_action_multipath {
4260     ovs_be16 type;              /* OFPAT_VENDOR. */
4261     ovs_be16 len;               /* Length is 32. */
4262     ovs_be32 vendor;            /* NX_VENDOR_ID. */
4263     ovs_be16 subtype;           /* NXAST_MULTIPATH. */
4264
4265     /* What fields to hash and how. */
4266     ovs_be16 fields;            /* One of NX_HASH_FIELDS_*. */
4267     ovs_be16 basis;             /* Universal hash parameter. */
4268     ovs_be16 pad0;
4269
4270     /* Multipath link choice algorithm to apply to hash value. */
4271     ovs_be16 algorithm;         /* One of NX_MP_ALG_*. */
4272     ovs_be16 max_link;          /* Number of output links, minus 1. */
4273     ovs_be32 arg;               /* Algorithm-specific argument. */
4274     ovs_be16 pad1;
4275
4276     /* Where to store the result. */
4277     ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
4278     ovs_be32 dst;               /* Destination. */
4279 };
4280 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4281
4282 static enum ofperr
4283 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4284                            enum ofp_version ofp_version OVS_UNUSED,
4285                            struct ofpbuf *out)
4286 {
4287     uint32_t n_links = ntohs(nam->max_link) + 1;
4288     size_t min_n_bits = log_2_ceil(n_links);
4289     struct ofpact_multipath *mp;
4290
4291     mp = ofpact_put_MULTIPATH(out);
4292     mp->fields = ntohs(nam->fields);
4293     mp->basis = ntohs(nam->basis);
4294     mp->algorithm = ntohs(nam->algorithm);
4295     mp->max_link = ntohs(nam->max_link);
4296     mp->arg = ntohl(nam->arg);
4297     mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4298     mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4299     mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4300
4301     if (!flow_hash_fields_valid(mp->fields)) {
4302         VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4303         return OFPERR_OFPBAC_BAD_ARGUMENT;
4304     } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4305                && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4306                && mp->algorithm != NX_MP_ALG_HRW
4307                && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4308         VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4309         return OFPERR_OFPBAC_BAD_ARGUMENT;
4310     } else if (mp->dst.n_bits < min_n_bits) {
4311         VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4312                      "%"PRIu32" links", min_n_bits, n_links);
4313         return OFPERR_OFPBAC_BAD_ARGUMENT;
4314     }
4315
4316     return multipath_check(mp, NULL);
4317 }
4318
4319 static void
4320 encode_MULTIPATH(const struct ofpact_multipath *mp,
4321                  enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4322 {
4323     struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4324
4325     nam->fields = htons(mp->fields);
4326     nam->basis = htons(mp->basis);
4327     nam->algorithm = htons(mp->algorithm);
4328     nam->max_link = htons(mp->max_link);
4329     nam->arg = htonl(mp->arg);
4330     nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4331     nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4332 }
4333
4334 static char * OVS_WARN_UNUSED_RESULT
4335 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4336                 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4337 {
4338     return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4339 }
4340
4341 static void
4342 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4343 {
4344     multipath_format(a, s);
4345 }
4346 \f
4347 /* Action structure for NXAST_NOTE.
4348  *
4349  * This action has no effect.  It is variable length.  The switch does not
4350  * attempt to interpret the user-defined 'note' data in any way.  A controller
4351  * can use this action to attach arbitrary metadata to a flow.
4352  *
4353  * This action might go away in the future.
4354  */
4355 struct nx_action_note {
4356     ovs_be16 type;                  /* OFPAT_VENDOR. */
4357     ovs_be16 len;                   /* A multiple of 8, but at least 16. */
4358     ovs_be32 vendor;                /* NX_VENDOR_ID. */
4359     ovs_be16 subtype;               /* NXAST_NOTE. */
4360     uint8_t note[6];                /* Start of user-defined data. */
4361     /* Possibly followed by additional user-defined data. */
4362 };
4363 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4364
4365 static enum ofperr
4366 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4367                       enum ofp_version ofp_version OVS_UNUSED,
4368                       struct ofpbuf *out)
4369 {
4370     struct ofpact_note *note;
4371     unsigned int length;
4372
4373     length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4374     note = ofpact_put(out, OFPACT_NOTE,
4375                       offsetof(struct ofpact_note, data) + length);
4376     note->length = length;
4377     memcpy(note->data, nan->note, length);
4378
4379     return 0;
4380 }
4381
4382 static void
4383 encode_NOTE(const struct ofpact_note *note,
4384             enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4385 {
4386     size_t start_ofs = out->size;
4387     struct nx_action_note *nan;
4388     unsigned int remainder;
4389     unsigned int len;
4390
4391     put_NXAST_NOTE(out);
4392     out->size = out->size - sizeof nan->note;
4393
4394     ofpbuf_put(out, note->data, note->length);
4395
4396     len = out->size - start_ofs;
4397     remainder = len % OFP_ACTION_ALIGN;
4398     if (remainder) {
4399         ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4400     }
4401     nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4402     nan->len = htons(out->size - start_ofs);
4403 }
4404
4405 static char * OVS_WARN_UNUSED_RESULT
4406 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4407            enum ofputil_protocol *usable_protocols OVS_UNUSED)
4408 {
4409     struct ofpact_note *note;
4410
4411     note = ofpact_put_NOTE(ofpacts);
4412     while (*arg != '\0') {
4413         uint8_t byte;
4414         bool ok;
4415
4416         if (*arg == '.') {
4417             arg++;
4418         }
4419         if (*arg == '\0') {
4420             break;
4421         }
4422
4423         byte = hexits_value(arg, 2, &ok);
4424         if (!ok) {
4425             return xstrdup("bad hex digit in `note' argument");
4426         }
4427         ofpbuf_put(ofpacts, &byte, 1);
4428
4429         note = ofpacts->header;
4430         note->length++;
4431
4432         arg += 2;
4433     }
4434     ofpact_update_len(ofpacts, &note->ofpact);
4435     return NULL;
4436 }
4437
4438 static void
4439 format_NOTE(const struct ofpact_note *a, struct ds *s)
4440 {
4441     size_t i;
4442
4443     ds_put_cstr(s, "note:");
4444     for (i = 0; i < a->length; i++) {
4445         if (i) {
4446             ds_put_char(s, '.');
4447         }
4448         ds_put_format(s, "%02"PRIx8, a->data[i]);
4449     }
4450 }
4451 \f
4452 /* Exit action. */
4453
4454 static enum ofperr
4455 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4456 {
4457     ofpact_put_EXIT(out);
4458     return 0;
4459 }
4460
4461 static void
4462 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4463             enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4464 {
4465     put_NXAST_EXIT(out);
4466 }
4467
4468 static char * OVS_WARN_UNUSED_RESULT
4469 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4470            enum ofputil_protocol *usable_protocols OVS_UNUSED)
4471 {
4472     ofpact_put_EXIT(ofpacts);
4473     return NULL;
4474 }
4475
4476 static void
4477 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4478 {
4479     ds_put_cstr(s, "exit");
4480 }
4481 \f
4482 /* Unroll xlate action. */
4483
4484 static void
4485 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4486                     enum ofp_version ofp_version OVS_UNUSED,
4487                     struct ofpbuf *out OVS_UNUSED)
4488 {
4489     OVS_NOT_REACHED();
4490 }
4491
4492 static char * OVS_WARN_UNUSED_RESULT
4493 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4494                    enum ofputil_protocol *usable_protocols OVS_UNUSED)
4495 {
4496     OVS_NOT_REACHED();
4497     return NULL;
4498 }
4499
4500 static void
4501 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4502                     struct ds *s)
4503 {
4504     ds_put_cstr(s, "unroll_xlate");
4505 }
4506 \f
4507 /* Action structure for NXAST_SAMPLE.
4508  *
4509  * Samples matching packets with the given probability and sends them
4510  * each to the set of collectors identified with the given ID.  The
4511  * probability is expressed as a number of packets to be sampled out
4512  * of USHRT_MAX packets, and must be >0.
4513  *
4514  * When sending packet samples to IPFIX collectors, the IPFIX flow
4515  * record sent for each sampled packet is associated with the given
4516  * observation domain ID and observation point ID.  Each IPFIX flow
4517  * record contain the sampled packet's headers when executing this
4518  * rule.  If a sampled packet's headers are modified by previous
4519  * actions in the flow, those modified headers are sent. */
4520 struct nx_action_sample {
4521     ovs_be16 type;                  /* OFPAT_VENDOR. */
4522     ovs_be16 len;                   /* Length is 24. */
4523     ovs_be32 vendor;                /* NX_VENDOR_ID. */
4524     ovs_be16 subtype;               /* NXAST_SAMPLE. */
4525     ovs_be16 probability;           /* Fraction of packets to sample. */
4526     ovs_be32 collector_set_id;      /* ID of collector set in OVSDB. */
4527     ovs_be32 obs_domain_id;         /* ID of sampling observation domain. */
4528     ovs_be32 obs_point_id;          /* ID of sampling observation point. */
4529 };
4530 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4531
4532 static enum ofperr
4533 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4534                         enum ofp_version ofp_version OVS_UNUSED,
4535                         struct ofpbuf *out)
4536 {
4537     struct ofpact_sample *sample;
4538
4539     sample = ofpact_put_SAMPLE(out);
4540     sample->probability = ntohs(nas->probability);
4541     sample->collector_set_id = ntohl(nas->collector_set_id);
4542     sample->obs_domain_id = ntohl(nas->obs_domain_id);
4543     sample->obs_point_id = ntohl(nas->obs_point_id);
4544
4545     if (sample->probability == 0) {
4546         return OFPERR_OFPBAC_BAD_ARGUMENT;
4547     }
4548
4549     return 0;
4550 }
4551
4552 static void
4553 encode_SAMPLE(const struct ofpact_sample *sample,
4554               enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4555 {
4556     struct nx_action_sample *nas;
4557
4558     nas = put_NXAST_SAMPLE(out);
4559     nas->probability = htons(sample->probability);
4560     nas->collector_set_id = htonl(sample->collector_set_id);
4561     nas->obs_domain_id = htonl(sample->obs_domain_id);
4562     nas->obs_point_id = htonl(sample->obs_point_id);
4563 }
4564
4565 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4566  * action to 'ofpacts'.
4567  *
4568  * Returns NULL if successful, otherwise a malloc()'d string describing the
4569  * error.  The caller is responsible for freeing the returned string. */
4570 static char * OVS_WARN_UNUSED_RESULT
4571 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4572              enum ofputil_protocol *usable_protocols OVS_UNUSED)
4573 {
4574     struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4575     char *key, *value;
4576
4577     while (ofputil_parse_key_value(&arg, &key, &value)) {
4578         char *error = NULL;
4579
4580         if (!strcmp(key, "probability")) {
4581             error = str_to_u16(value, "probability", &os->probability);
4582             if (!error && os->probability == 0) {
4583                 error = xasprintf("invalid probability value \"%s\"", value);
4584             }
4585         } else if (!strcmp(key, "collector_set_id")) {
4586             error = str_to_u32(value, &os->collector_set_id);
4587         } else if (!strcmp(key, "obs_domain_id")) {
4588             error = str_to_u32(value, &os->obs_domain_id);
4589         } else if (!strcmp(key, "obs_point_id")) {
4590             error = str_to_u32(value, &os->obs_point_id);
4591         } else {
4592             error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4593                               key);
4594         }
4595         if (error) {
4596             return error;
4597         }
4598     }
4599     if (os->probability == 0) {
4600         return xstrdup("non-zero \"probability\" must be specified on sample");
4601     }
4602     return NULL;
4603 }
4604
4605 static void
4606 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4607 {
4608     ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4609                   ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4610                   a->probability, a->collector_set_id,
4611                   a->obs_domain_id, a->obs_point_id);
4612 }
4613 \f
4614 /* debug_recirc instruction. */
4615
4616 static bool enable_debug;
4617
4618 void
4619 ofpact_dummy_enable(void)
4620 {
4621     enable_debug = true;
4622 }
4623
4624 static enum ofperr
4625 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4626 {
4627     if (!enable_debug) {
4628         return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4629     }
4630
4631     ofpact_put_DEBUG_RECIRC(out);
4632     return 0;
4633 }
4634
4635 static void
4636 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4637                     enum ofp_version ofp_version OVS_UNUSED,
4638                     struct ofpbuf *out)
4639 {
4640     put_NXAST_DEBUG_RECIRC(out);
4641 }
4642
4643 static char * OVS_WARN_UNUSED_RESULT
4644 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4645                    enum ofputil_protocol *usable_protocols OVS_UNUSED)
4646 {
4647     ofpact_put_DEBUG_RECIRC(ofpacts);
4648     return NULL;
4649 }
4650
4651 static void
4652 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4653 {
4654     ds_put_cstr(s, "debug_recirc");
4655 }
4656
4657 /* Action structure for NXAST_CT.
4658  *
4659  * Pass traffic to the connection tracker.
4660  *
4661  * There are two important concepts to understanding the connection tracking
4662  * interface: Packet state and Connection state. Packets may be "Untracked" or
4663  * "Tracked". Connections may be "Uncommitted" or "Committed".
4664  *
4665  *   - Packet State:
4666  *
4667  *      Untracked packets have not yet passed through the connection tracker,
4668  *      and the connection state for such packets is unknown. In most cases,
4669  *      packets entering the OpenFlow pipeline will initially be in the
4670  *      untracked state. Untracked packets may become tracked by executing
4671  *      NXAST_CT with a "recirc_table" specified. This makes various aspects
4672  *      about the connection available, in particular the connection state.
4673  *
4674  *      Tracked packets have previously passed through the connection tracker.
4675  *      These packets will remain tracked through until the end of the OpenFlow
4676  *      pipeline. Tracked packets which have NXAST_CT executed with a
4677  *      "recirc_table" specified will return to the tracked state.
4678  *
4679  *      The packet state is only significant for the duration of packet
4680  *      processing within the OpenFlow pipeline.
4681  *
4682  *   - Connection State:
4683  *
4684  *      Multiple packets may be associated with a single connection. Initially,
4685  *      all connections are uncommitted. The connection state corresponding to
4686  *      a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4687  *
4688  *      Uncommitted connections have no state stored about them. Uncommitted
4689  *      connections may transition into the committed state by executing
4690  *      NXAST_CT with the NX_CT_F_COMMIT flag.
4691  *
4692  *      Once a connection becomes committed, information may be gathered about
4693  *      the connection by passing subsequent packets through the connection
4694  *      tracker, and the state of the connection will be stored beyond the
4695  *      lifetime of packet processing.
4696  *
4697  *      Connections may transition back into the uncommitted state due to
4698  *      external timers, or due to the contents of packets that are sent to the
4699  *      connection tracker. This behaviour is outside of the scope of the
4700  *      OpenFlow interface.
4701  *
4702  * The "zone" specifies a context within which the tracking is done:
4703  *
4704  *      The connection tracking zone is a 16-bit number. Each zone is an
4705  *      independent connection tracking context. The connection state for each
4706  *      connection is completely separate for each zone, so if a connection
4707  *      is committed to zone A, then it will remain uncommitted in zone B.
4708  *      If NXAST_CT is executed with the same zone multiple times, later
4709  *      executions have no effect.
4710  *
4711  *      If 'zone_src' is nonzero, this specifies that the zone should be
4712  *      sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4713  *      of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4714  *      NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4715  *      are the same as the acceptable nxm_header values for the 'src' field of
4716  *      NXAST_REG_MOVE.
4717  *
4718  *      If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4719  *      connection tracking zone.
4720  *
4721  * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4722  *
4723  *      If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4724  *      packet will be logically cloned prior to executing this action. One
4725  *      copy will be sent to the connection tracker, then will be re-injected
4726  *      into the OpenFlow pipeline beginning at the OpenFlow table specified in
4727  *      this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4728  *      fields will be populated. The original instance of the packet will
4729  *      continue the current actions list. This can be thought of as similar to
4730  *      the effect of the "output" action: One copy is sent out (in this case,
4731  *      to the connection tracker), but the current copy continues processing.
4732  *
4733  *      It is strongly recommended that this table is later than the current
4734  *      table, to prevent loops.
4735  *
4736  * The "alg" attaches protocol-specific behaviour to this action:
4737  *
4738  *      The ALG is a 16-bit number which specifies that additional
4739  *      processing should be applied to this traffic.
4740  *
4741  *      Protocol | Value | Meaning
4742  *      --------------------------------------------------------------------
4743  *      None     |     0 | No protocol-specific behaviour.
4744  *      FTP      |    21 | Parse FTP control connections and observe the
4745  *               |       | negotiation of related data connections.
4746  *      Other    | Other | Unsupported protocols.
4747  *
4748  *      By way of example, if FTP control connections have this action applied
4749  *      with the ALG set to FTP (21), then the connection tracker will observe
4750  *      the negotiation of data connections. This allows the connection
4751  *      tracker to identify subsequent data connections as "related" to this
4752  *      existing connection. The "related" flag will be populated in the
4753  *      NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
4754  *      specified.
4755  *
4756  * Zero or more actions may immediately follow this action. These actions will
4757  * be executed within the context of the connection tracker, and they require
4758  * the NX_CT_F_COMMIT flag to be set.
4759  */
4760 struct nx_action_conntrack {
4761     ovs_be16 type;              /* OFPAT_VENDOR. */
4762     ovs_be16 len;               /* At least 24. */
4763     ovs_be32 vendor;            /* NX_VENDOR_ID. */
4764     ovs_be16 subtype;           /* NXAST_CT. */
4765     ovs_be16 flags;             /* Zero or more NX_CT_F_* flags.
4766                                  * Unspecified flag bits must be zero. */
4767     ovs_be32 zone_src;          /* Connection tracking context. */
4768     union {
4769         ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4770         ovs_be16 zone_imm;      /* Immediate value for zone. */
4771     };
4772     uint8_t recirc_table;       /* Recirculate to a specific table, or
4773                                    NX_CT_RECIRC_NONE for no recirculation. */
4774     uint8_t pad[3];             /* Zeroes */
4775     ovs_be16 alg;               /* Well-known port number for the protocol.
4776                                  * 0 indicates no ALG is required. */
4777     /* Followed by a sequence of zero or more OpenFlow actions. The length of
4778      * these is included in 'len'. */
4779 };
4780 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4781
4782 static enum ofperr
4783 decode_ct_zone(const struct nx_action_conntrack *nac,
4784                struct ofpact_conntrack *out)
4785 {
4786     if (nac->zone_src) {
4787         enum ofperr error;
4788
4789         out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4790         out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4791         out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4792         error = mf_check_src(&out->zone_src, NULL);
4793         if (error) {
4794             return error;
4795         }
4796
4797         if (out->zone_src.n_bits != 16) {
4798             VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4799                          out->zone_src.n_bits);
4800             return OFPERR_OFPBAC_BAD_SET_LEN;
4801         }
4802     } else {
4803         out->zone_src.field = NULL;
4804         out->zone_imm = ntohs(nac->zone_imm);
4805     }
4806
4807     return 0;
4808 }
4809
4810 static enum ofperr
4811 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
4812                     enum ofp_version ofp_version, struct ofpbuf *out)
4813 {
4814     const size_t ct_offset = ofpacts_pull(out);
4815     struct ofpact_conntrack *conntrack;
4816     struct ofpbuf openflow;
4817     int error = 0;
4818
4819     conntrack = ofpact_put_CT(out);
4820     conntrack->flags = ntohs(nac->flags);
4821     error = decode_ct_zone(nac, conntrack);
4822     if (error) {
4823         goto out;
4824     }
4825     conntrack->recirc_table = nac->recirc_table;
4826     conntrack->alg = ntohs(nac->alg);
4827
4828     ofpbuf_pull(out, sizeof(*conntrack));
4829
4830     ofpbuf_use_const(&openflow, nac + 1, ntohs(nac->len) - sizeof(*nac));
4831     error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
4832                                             ofp_version,
4833                                             1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4834                                             out, OFPACT_CT);
4835     if (error) {
4836         goto out;
4837     }
4838
4839     conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
4840     out->header = &conntrack->ofpact;
4841     ofpact_update_len(out, &conntrack->ofpact);
4842
4843     if (conntrack->ofpact.len > sizeof(*conntrack)
4844         && !(conntrack->flags & NX_CT_F_COMMIT)) {
4845         VLOG_WARN_RL(&rl, "CT action requires commit flag if actions are "
4846                      "specified.");
4847         error = OFPERR_OFPBAC_BAD_ARGUMENT;
4848     }
4849
4850 out:
4851     ofpbuf_push_uninit(out, ct_offset);
4852     return error;
4853 }
4854
4855 static void
4856 encode_CT(const struct ofpact_conntrack *conntrack,
4857           enum ofp_version ofp_version, struct ofpbuf *out)
4858 {
4859     struct nx_action_conntrack *nac;
4860     const size_t ofs = out->size;
4861     size_t len;
4862
4863     nac = put_NXAST_CT(out);
4864     nac->flags = htons(conntrack->flags);
4865     if (conntrack->zone_src.field) {
4866         nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
4867         nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
4868                                                    conntrack->zone_src.n_bits);
4869     } else {
4870         nac->zone_src = htonl(0);
4871         nac->zone_imm = htons(conntrack->zone_imm);
4872     }
4873     nac->recirc_table = conntrack->recirc_table;
4874     nac->alg = htons(conntrack->alg);
4875
4876     len = ofpacts_put_openflow_actions(conntrack->actions,
4877                                        ofpact_ct_get_action_len(conntrack),
4878                                        out, ofp_version);
4879     len += sizeof(*nac);
4880     nac = ofpbuf_at(out, ofs, sizeof(*nac));
4881     nac->len = htons(len);
4882 }
4883
4884 /* Parses 'arg' as the argument to a "ct" action, and appends such an
4885  * action to 'ofpacts'.
4886  *
4887  * Returns NULL if successful, otherwise a malloc()'d string describing the
4888  * error.  The caller is responsible for freeing the returned string. */
4889 static char * OVS_WARN_UNUSED_RESULT
4890 parse_CT(char *arg, struct ofpbuf *ofpacts,
4891          enum ofputil_protocol *usable_protocols)
4892 {
4893     const size_t ct_offset = ofpacts_pull(ofpacts);
4894     struct ofpact_conntrack *oc;
4895     char *error = NULL;
4896     char *key, *value;
4897
4898     oc = ofpact_put_CT(ofpacts);
4899     oc->flags = 0;
4900     oc->recirc_table = NX_CT_RECIRC_NONE;
4901     while (ofputil_parse_key_value(&arg, &key, &value)) {
4902         if (!strcmp(key, "commit")) {
4903             oc->flags |= NX_CT_F_COMMIT;
4904         } else if (!strcmp(key, "table")) {
4905             error = str_to_u8(value, "recirc_table", &oc->recirc_table);
4906             if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
4907                 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
4908             }
4909         } else if (!strcmp(key, "zone")) {
4910             error = str_to_u16(value, "zone", &oc->zone_imm);
4911
4912             if (error) {
4913                 free(error);
4914                 error = mf_parse_subfield(&oc->zone_src, value);
4915                 if (error) {
4916                     return error;
4917                 }
4918             }
4919         } else if (!strcmp(key, "alg")) {
4920             error = str_to_connhelper(value, &oc->alg);
4921         } else if (!strcmp(key, "exec")) {
4922             /* Hide existing actions from ofpacts_parse_copy(), so the
4923              * nesting can be handled transparently. */
4924             enum ofputil_protocol usable_protocols2;
4925
4926             ofpbuf_pull(ofpacts, sizeof(*oc));
4927             /* Initializes 'usable_protocol2', fold it back to
4928              * '*usable_protocols' afterwards, so that we do not lose
4929              * restrictions already in there. */
4930             error = ofpacts_parse_copy(value, ofpacts, &usable_protocols2,
4931                                        false, OFPACT_CT);
4932             *usable_protocols &= usable_protocols2;
4933             ofpact_pad(ofpacts);
4934             ofpacts->header = ofpbuf_push_uninit(ofpacts, sizeof(*oc));
4935             oc = ofpacts->header;
4936         } else {
4937             error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
4938         }
4939         if (error) {
4940             break;
4941         }
4942     }
4943
4944     ofpact_update_len(ofpacts, &oc->ofpact);
4945     ofpbuf_push_uninit(ofpacts, ct_offset);
4946     return error;
4947 }
4948
4949 static void
4950 format_alg(int port, struct ds *s)
4951 {
4952     if (port == IPPORT_FTP) {
4953         ds_put_format(s, "alg=ftp,");
4954     } else if (port) {
4955         ds_put_format(s, "alg=%d,", port);
4956     }
4957 }
4958
4959 static void
4960 format_CT(const struct ofpact_conntrack *a, struct ds *s)
4961 {
4962     ds_put_cstr(s, "ct(");
4963     if (a->flags & NX_CT_F_COMMIT) {
4964         ds_put_cstr(s, "commit,");
4965     }
4966     if (a->recirc_table != NX_CT_RECIRC_NONE) {
4967         ds_put_format(s, "table=%"PRIu8",", a->recirc_table);
4968     }
4969     if (a->zone_src.field) {
4970         ds_put_format(s, "zone=");
4971         mf_format_subfield(&a->zone_src, s);
4972         ds_put_char(s, ',');
4973     } else if (a->zone_imm) {
4974         ds_put_format(s, "zone=%"PRIu16",", a->zone_imm);
4975     }
4976     if (ofpact_ct_get_action_len(a)) {
4977         ds_put_cstr(s, "exec(");
4978         ofpacts_format(a->actions, ofpact_ct_get_action_len(a), s);
4979         ds_put_format(s, "),");
4980     }
4981     format_alg(a->alg, s);
4982     ds_chomp(s, ',');
4983     ds_put_char(s, ')');
4984 }
4985 \f
4986 /* Meter instruction. */
4987
4988 static void
4989 encode_METER(const struct ofpact_meter *meter,
4990              enum ofp_version ofp_version, struct ofpbuf *out)
4991 {
4992     if (ofp_version >= OFP13_VERSION) {
4993         instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4994     }
4995 }
4996
4997 static char * OVS_WARN_UNUSED_RESULT
4998 parse_METER(char *arg, struct ofpbuf *ofpacts,
4999             enum ofputil_protocol *usable_protocols)
5000 {
5001     *usable_protocols &= OFPUTIL_P_OF13_UP;
5002     return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
5003 }
5004
5005 static void
5006 format_METER(const struct ofpact_meter *a, struct ds *s)
5007 {
5008     ds_put_format(s, "meter:%"PRIu32, a->meter_id);
5009 }
5010 \f
5011 /* Clear-Actions instruction. */
5012
5013 static void
5014 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
5015                      enum ofp_version ofp_version OVS_UNUSED,
5016                      struct ofpbuf *out OVS_UNUSED)
5017 {
5018     if (ofp_version > OFP10_VERSION) {
5019         instruction_put_OFPIT11_CLEAR_ACTIONS(out);
5020     }
5021 }
5022
5023 static char * OVS_WARN_UNUSED_RESULT
5024 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
5025                     enum ofputil_protocol *usable_protocols OVS_UNUSED)
5026 {
5027     ofpact_put_CLEAR_ACTIONS(ofpacts);
5028     return NULL;
5029 }
5030
5031 static void
5032 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
5033 {
5034     ds_put_cstr(s, "clear_actions");
5035 }
5036 \f
5037 /* Write-Actions instruction. */
5038
5039 static void
5040 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
5041                      enum ofp_version ofp_version, struct ofpbuf *out)
5042 {
5043     if (ofp_version > OFP10_VERSION) {
5044         const size_t ofs = out->size;
5045
5046         instruction_put_OFPIT11_WRITE_ACTIONS(out);
5047         ofpacts_put_openflow_actions(actions->actions,
5048                                      ofpact_nest_get_action_len(actions),
5049                                      out, ofp_version);
5050         ofpacts_update_instruction_actions(out, ofs);
5051     }
5052 }
5053
5054 static char * OVS_WARN_UNUSED_RESULT
5055 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
5056                     enum ofputil_protocol *usable_protocols)
5057 {
5058     size_t ofs = ofpacts_pull(ofpacts);
5059     struct ofpact_nest *on;
5060     char *error;
5061
5062     /* Add a Write-Actions instruction and then pull it off. */
5063     ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
5064     ofpbuf_pull(ofpacts, sizeof *on);
5065
5066     /* Parse nested actions.
5067      *
5068      * We pulled off "write-actions" and the previous actions because the
5069      * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5070      * that it doesn't actually include the nested actions.  That means that
5071      * ofpacts_parse() would reject them as being part of an Apply-Actions that
5072      * follows a Write-Actions, which is an invalid order.  */
5073     error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
5074                           OFPACT_WRITE_ACTIONS);
5075
5076     /* Put the Write-Actions back on and update its length. */
5077     on = ofpbuf_push_uninit(ofpacts, sizeof *on);
5078     on->ofpact.len = ofpacts->size;
5079
5080     /* Put any previous actions or instructions back on. */
5081     ofpbuf_push_uninit(ofpacts, ofs);
5082
5083     return error;
5084 }
5085
5086 static void
5087 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
5088 {
5089     ds_put_cstr(s, "write_actions(");
5090     ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
5091     ds_put_char(s, ')');
5092 }
5093 \f
5094 /* Action structure for NXAST_WRITE_METADATA.
5095  *
5096  * Modifies the 'mask' bits of the metadata value. */
5097 struct nx_action_write_metadata {
5098     ovs_be16 type;                  /* OFPAT_VENDOR. */
5099     ovs_be16 len;                   /* Length is 32. */
5100     ovs_be32 vendor;                /* NX_VENDOR_ID. */
5101     ovs_be16 subtype;               /* NXAST_WRITE_METADATA. */
5102     uint8_t zeros[6];               /* Must be zero. */
5103     ovs_be64 metadata;              /* Metadata register. */
5104     ovs_be64 mask;                  /* Metadata mask. */
5105 };
5106 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
5107
5108 static enum ofperr
5109 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
5110                                 enum ofp_version ofp_version OVS_UNUSED,
5111                                 struct ofpbuf *out)
5112 {
5113     struct ofpact_metadata *om;
5114
5115     if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
5116         return OFPERR_NXBRC_MUST_BE_ZERO;
5117     }
5118
5119     om = ofpact_put_WRITE_METADATA(out);
5120     om->metadata = nawm->metadata;
5121     om->mask = nawm->mask;
5122
5123     return 0;
5124 }
5125
5126 static void
5127 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
5128                       enum ofp_version ofp_version, struct ofpbuf *out)
5129 {
5130     if (ofp_version == OFP10_VERSION) {
5131         struct nx_action_write_metadata *nawm;
5132
5133         nawm = put_NXAST_WRITE_METADATA(out);
5134         nawm->metadata = metadata->metadata;
5135         nawm->mask = metadata->mask;
5136     } else {
5137         struct ofp11_instruction_write_metadata *oiwm;
5138
5139         oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
5140         oiwm->metadata = metadata->metadata;
5141         oiwm->metadata_mask = metadata->mask;
5142     }
5143 }
5144
5145 static char * OVS_WARN_UNUSED_RESULT
5146 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
5147                      enum ofputil_protocol *usable_protocols)
5148 {
5149     struct ofpact_metadata *om;
5150     char *mask = strchr(arg, '/');
5151
5152     *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
5153
5154     om = ofpact_put_WRITE_METADATA(ofpacts);
5155     if (mask) {
5156         char *error;
5157
5158         *mask = '\0';
5159         error = str_to_be64(mask + 1, &om->mask);
5160         if (error) {
5161             return error;
5162         }
5163     } else {
5164         om->mask = OVS_BE64_MAX;
5165     }
5166
5167     return str_to_be64(arg, &om->metadata);
5168 }
5169
5170 static void
5171 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5172 {
5173     ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
5174     if (a->mask != OVS_BE64_MAX) {
5175         ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5176     }
5177 }
5178 \f
5179 /* Goto-Table instruction. */
5180
5181 static void
5182 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5183                   enum ofp_version ofp_version, struct ofpbuf *out)
5184 {
5185     if (ofp_version == OFP10_VERSION) {
5186         struct nx_action_resubmit *nar;
5187
5188         nar = put_NXAST_RESUBMIT_TABLE(out);
5189         nar->table = goto_table->table_id;
5190         nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5191     } else {
5192         struct ofp11_instruction_goto_table *oigt;
5193
5194         oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5195         oigt->table_id = goto_table->table_id;
5196         memset(oigt->pad, 0, sizeof oigt->pad);
5197     }
5198 }
5199
5200 static char * OVS_WARN_UNUSED_RESULT
5201 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5202                  enum ofputil_protocol *usable_protocols OVS_UNUSED)
5203 {
5204     struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5205     char *table_s = strsep(&arg, ",");
5206     if (!table_s || !table_s[0]) {
5207         return xstrdup("instruction goto-table needs table id");
5208     }
5209     return str_to_u8(table_s, "table", &ogt->table_id);
5210 }
5211
5212 static void
5213 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5214 {
5215     ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
5216 }
5217 \f
5218 static void
5219 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5220                const struct ofp_action_header *bad_action, enum ofperr error)
5221 {
5222     if (!VLOG_DROP_WARN(&rl)) {
5223         struct ds s;
5224
5225         ds_init(&s);
5226         ds_put_hex_dump(&s, actions, actions_len, 0, false);
5227         VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5228                   (char *)bad_action - (char *)actions,
5229                   ofperr_get_name(error), ds_cstr(&s));
5230         ds_destroy(&s);
5231     }
5232 }
5233
5234 static enum ofperr
5235 ofpacts_decode(const void *actions, size_t actions_len,
5236                enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5237 {
5238     struct ofpbuf openflow;
5239
5240     ofpbuf_use_const(&openflow, actions, actions_len);
5241     while (openflow.size) {
5242         const struct ofp_action_header *action = openflow.data;
5243         enum ofp_raw_action_type raw;
5244         enum ofperr error;
5245         uint64_t arg;
5246
5247         error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5248         if (!error) {
5249             error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5250         }
5251
5252         if (error) {
5253             log_bad_action(actions, actions_len, action, error);
5254             return error;
5255         }
5256     }
5257
5258     ofpact_pad(ofpacts);
5259     return 0;
5260 }
5261
5262 static enum ofperr
5263 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5264                                 unsigned int actions_len,
5265                                 enum ofp_version version,
5266                                 uint32_t allowed_ovsinsts,
5267                                 struct ofpbuf *ofpacts,
5268                                 enum ofpact_type outer_action)
5269 {
5270     const struct ofp_action_header *actions;
5271     enum ofperr error;
5272
5273     if (!outer_action) {
5274         ofpbuf_clear(ofpacts);
5275     }
5276
5277     if (actions_len % OFP_ACTION_ALIGN != 0) {
5278         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5279                      "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5280         return OFPERR_OFPBRC_BAD_LEN;
5281     }
5282
5283     actions = ofpbuf_try_pull(openflow, actions_len);
5284     if (actions == NULL) {
5285         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5286                      "remaining message length (%"PRIu32")",
5287                      actions_len, openflow->size);
5288         return OFPERR_OFPBRC_BAD_LEN;
5289     }
5290
5291     error = ofpacts_decode(actions, actions_len, version, ofpacts);
5292     if (error) {
5293         ofpbuf_clear(ofpacts);
5294         return error;
5295     }
5296
5297     error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5298                            outer_action);
5299     if (error) {
5300         ofpbuf_clear(ofpacts);
5301     }
5302     return error;
5303 }
5304
5305 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
5306  * front of 'openflow' into ofpacts.  On success, replaces any existing content
5307  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
5308  * Returns 0 if successful, otherwise an OpenFlow error.
5309  *
5310  * Actions are processed according to their OpenFlow version which
5311  * is provided in the 'version' parameter.
5312  *
5313  * In most places in OpenFlow, actions appear encapsulated in instructions, so
5314  * you should call ofpacts_pull_openflow_instructions() instead of this
5315  * function.
5316  *
5317  * The parsed actions are valid generically, but they may not be valid in a
5318  * specific context.  For example, port numbers up to OFPP_MAX are valid
5319  * generically, but specific datapaths may only support port numbers in a
5320  * smaller range.  Use ofpacts_check() to additional check whether actions are
5321  * valid in a specific context. */
5322 enum ofperr
5323 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5324                               unsigned int actions_len,
5325                               enum ofp_version version,
5326                               struct ofpbuf *ofpacts)
5327 {
5328     return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5329                                            1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5330                                            ofpacts, 0);
5331 }
5332 \f
5333 /* OpenFlow 1.1 actions. */
5334
5335
5336 /* True if an action sets the value of a field
5337  * in a way that is compatibile with the action set.
5338  * The field can be set via either a set or a move action.
5339  * False otherwise. */
5340 static bool
5341 ofpact_is_set_or_move_action(const struct ofpact *a)
5342 {
5343     switch (a->type) {
5344     case OFPACT_SET_FIELD:
5345     case OFPACT_REG_MOVE:
5346     case OFPACT_SET_ETH_DST:
5347     case OFPACT_SET_ETH_SRC:
5348     case OFPACT_SET_IP_DSCP:
5349     case OFPACT_SET_IP_ECN:
5350     case OFPACT_SET_IP_TTL:
5351     case OFPACT_SET_IPV4_DST:
5352     case OFPACT_SET_IPV4_SRC:
5353     case OFPACT_SET_L4_DST_PORT:
5354     case OFPACT_SET_L4_SRC_PORT:
5355     case OFPACT_SET_MPLS_LABEL:
5356     case OFPACT_SET_MPLS_TC:
5357     case OFPACT_SET_MPLS_TTL:
5358     case OFPACT_SET_QUEUE:
5359     case OFPACT_SET_TUNNEL:
5360     case OFPACT_SET_VLAN_PCP:
5361     case OFPACT_SET_VLAN_VID:
5362         return true;
5363     case OFPACT_BUNDLE:
5364     case OFPACT_CLEAR_ACTIONS:
5365     case OFPACT_CT:
5366     case OFPACT_CONTROLLER:
5367     case OFPACT_DEC_MPLS_TTL:
5368     case OFPACT_DEC_TTL:
5369     case OFPACT_ENQUEUE:
5370     case OFPACT_EXIT:
5371     case OFPACT_UNROLL_XLATE:
5372     case OFPACT_FIN_TIMEOUT:
5373     case OFPACT_GOTO_TABLE:
5374     case OFPACT_GROUP:
5375     case OFPACT_LEARN:
5376     case OFPACT_CONJUNCTION:
5377     case OFPACT_METER:
5378     case OFPACT_MULTIPATH:
5379     case OFPACT_NOTE:
5380     case OFPACT_OUTPUT:
5381     case OFPACT_OUTPUT_REG:
5382     case OFPACT_POP_MPLS:
5383     case OFPACT_POP_QUEUE:
5384     case OFPACT_PUSH_MPLS:
5385     case OFPACT_PUSH_VLAN:
5386     case OFPACT_RESUBMIT:
5387     case OFPACT_SAMPLE:
5388     case OFPACT_STACK_POP:
5389     case OFPACT_STACK_PUSH:
5390     case OFPACT_STRIP_VLAN:
5391     case OFPACT_WRITE_ACTIONS:
5392     case OFPACT_WRITE_METADATA:
5393     case OFPACT_DEBUG_RECIRC:
5394         return false;
5395     default:
5396         OVS_NOT_REACHED();
5397     }
5398 }
5399
5400 /* True if an action is allowed in the action set.
5401  * False otherwise. */
5402 static bool
5403 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5404 {
5405     switch (a->type) {
5406     case OFPACT_DEC_MPLS_TTL:
5407     case OFPACT_DEC_TTL:
5408     case OFPACT_GROUP:
5409     case OFPACT_OUTPUT:
5410     case OFPACT_POP_MPLS:
5411     case OFPACT_PUSH_MPLS:
5412     case OFPACT_PUSH_VLAN:
5413     case OFPACT_REG_MOVE:
5414     case OFPACT_SET_FIELD:
5415     case OFPACT_SET_ETH_DST:
5416     case OFPACT_SET_ETH_SRC:
5417     case OFPACT_SET_IP_DSCP:
5418     case OFPACT_SET_IP_ECN:
5419     case OFPACT_SET_IP_TTL:
5420     case OFPACT_SET_IPV4_DST:
5421     case OFPACT_SET_IPV4_SRC:
5422     case OFPACT_SET_L4_DST_PORT:
5423     case OFPACT_SET_L4_SRC_PORT:
5424     case OFPACT_SET_MPLS_LABEL:
5425     case OFPACT_SET_MPLS_TC:
5426     case OFPACT_SET_MPLS_TTL:
5427     case OFPACT_SET_QUEUE:
5428     case OFPACT_SET_TUNNEL:
5429     case OFPACT_SET_VLAN_PCP:
5430     case OFPACT_SET_VLAN_VID:
5431     case OFPACT_STRIP_VLAN:
5432         return true;
5433
5434     /* In general these actions are excluded because they are not part of
5435      * the OpenFlow specification nor map to actions that are defined in
5436      * the specification.  Thus the order in which they should be applied
5437      * in the action set is undefined. */
5438     case OFPACT_BUNDLE:
5439     case OFPACT_CONTROLLER:
5440     case OFPACT_CT:
5441     case OFPACT_ENQUEUE:
5442     case OFPACT_EXIT:
5443     case OFPACT_UNROLL_XLATE:
5444     case OFPACT_FIN_TIMEOUT:
5445     case OFPACT_LEARN:
5446     case OFPACT_CONJUNCTION:
5447     case OFPACT_MULTIPATH:
5448     case OFPACT_NOTE:
5449     case OFPACT_OUTPUT_REG:
5450     case OFPACT_POP_QUEUE:
5451     case OFPACT_RESUBMIT:
5452     case OFPACT_SAMPLE:
5453     case OFPACT_STACK_POP:
5454     case OFPACT_STACK_PUSH:
5455     case OFPACT_DEBUG_RECIRC:
5456
5457     /* The action set may only include actions and thus
5458      * may not include any instructions */
5459     case OFPACT_CLEAR_ACTIONS:
5460     case OFPACT_GOTO_TABLE:
5461     case OFPACT_METER:
5462     case OFPACT_WRITE_ACTIONS:
5463     case OFPACT_WRITE_METADATA:
5464         return false;
5465     default:
5466         OVS_NOT_REACHED();
5467     }
5468 }
5469
5470 /* Append ofpact 'a' onto the tail of 'out' */
5471 static void
5472 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5473 {
5474     ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5475 }
5476
5477 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5478 static bool
5479 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5480                   enum ofpact_type filter)
5481 {
5482     const struct ofpact *target;
5483     const struct ofpact *a;
5484
5485     target = NULL;
5486     OFPACT_FOR_EACH (a, in->data, in->size) {
5487         if (a->type == filter) {
5488             target = a;
5489         }
5490     }
5491     if (target) {
5492         ofpact_copy(out, target);
5493     }
5494     return target != NULL;
5495 }
5496
5497 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5498  * The order of appended ofpacts is preserved between 'in' and 'out' */
5499 static void
5500 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5501                  bool (*filter)(const struct ofpact *))
5502 {
5503     const struct ofpact *a;
5504
5505     OFPACT_FOR_EACH (a, in->data, in->size) {
5506         if (filter(a)) {
5507             ofpact_copy(out, a);
5508         }
5509     }
5510 }
5511
5512 /* Reads 'action_set', which contains ofpacts accumulated by
5513  * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5514  * executed directly into 'action_list'.  (These names correspond to the
5515  * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5516  *
5517  * In general this involves appending the last instance of each action that is
5518  * admissible in the action set in the order described in the OpenFlow
5519  * specification.
5520  *
5521  * Exceptions:
5522  * + output action is only appended if no group action was present in 'in'.
5523  * + As a simplification all set actions are copied in the order the are
5524  *   provided in 'in' as many set actions applied to a field has the same
5525  *   affect as only applying the last action that sets a field and
5526  *   duplicates are removed by do_xlate_actions().
5527  *   This has an unwanted side-effect of compsoting multiple
5528  *   LOAD_REG actions that touch different regions of the same field. */
5529 void
5530 ofpacts_execute_action_set(struct ofpbuf *action_list,
5531                            const struct ofpbuf *action_set)
5532 {
5533     /* The OpenFlow spec "Action Set" section specifies this order. */
5534     ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5535     ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5536     ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5537     ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5538     ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5539     ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5540     ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5541     ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5542
5543     /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5544      * we should execute only OFPACT_GROUP.
5545      *
5546      * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5547      * all the actions because there's no point in modifying a packet that will
5548      * not be sent anywhere. */
5549     if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5550         !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5551         !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5552         ofpbuf_clear(action_list);
5553     }
5554 }
5555
5556
5557 static enum ofperr
5558 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5559                               size_t n_in, enum ofp_version version,
5560                               struct ofpbuf *out)
5561 {
5562     enum ofperr error;
5563     struct ofpact *a;
5564     size_t start = out->size;
5565
5566     error = ofpacts_decode(in, n_in, version, out);
5567
5568     if (error) {
5569         return error;
5570     }
5571
5572     OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5573         if (!ofpact_is_allowed_in_actions_set(a)) {
5574             VLOG_WARN_RL(&rl, "disallowed action in action set");
5575             return OFPERR_OFPBAC_BAD_TYPE;
5576         }
5577     }
5578
5579     return 0;
5580 }
5581 \f
5582 /* OpenFlow 1.1 instructions. */
5583
5584 struct instruction_type_info {
5585     enum ovs_instruction_type type;
5586     const char *name;
5587 };
5588
5589 static const struct instruction_type_info inst_info[] = {
5590 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
5591 OVS_INSTRUCTIONS
5592 #undef DEFINE_INST
5593 };
5594
5595 const char *
5596 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5597 {
5598     return inst_info[type].name;
5599 }
5600
5601 int
5602 ovs_instruction_type_from_name(const char *name)
5603 {
5604     const struct instruction_type_info *p;
5605     for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5606         if (!strcasecmp(name, p->name)) {
5607             return p->type;
5608         }
5609     }
5610     return -1;
5611 }
5612
5613 enum ovs_instruction_type
5614 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5615 {
5616     switch (type) {
5617     case OFPACT_METER:
5618         return OVSINST_OFPIT13_METER;
5619     case OFPACT_CLEAR_ACTIONS:
5620         return OVSINST_OFPIT11_CLEAR_ACTIONS;
5621     case OFPACT_WRITE_ACTIONS:
5622         return OVSINST_OFPIT11_WRITE_ACTIONS;
5623     case OFPACT_WRITE_METADATA:
5624         return OVSINST_OFPIT11_WRITE_METADATA;
5625     case OFPACT_GOTO_TABLE:
5626         return OVSINST_OFPIT11_GOTO_TABLE;
5627     case OFPACT_OUTPUT:
5628     case OFPACT_GROUP:
5629     case OFPACT_CONTROLLER:
5630     case OFPACT_ENQUEUE:
5631     case OFPACT_OUTPUT_REG:
5632     case OFPACT_BUNDLE:
5633     case OFPACT_SET_VLAN_VID:
5634     case OFPACT_SET_VLAN_PCP:
5635     case OFPACT_STRIP_VLAN:
5636     case OFPACT_PUSH_VLAN:
5637     case OFPACT_SET_ETH_SRC:
5638     case OFPACT_SET_ETH_DST:
5639     case OFPACT_SET_IPV4_SRC:
5640     case OFPACT_SET_IPV4_DST:
5641     case OFPACT_SET_IP_DSCP:
5642     case OFPACT_SET_IP_ECN:
5643     case OFPACT_SET_IP_TTL:
5644     case OFPACT_SET_L4_SRC_PORT:
5645     case OFPACT_SET_L4_DST_PORT:
5646     case OFPACT_REG_MOVE:
5647     case OFPACT_SET_FIELD:
5648     case OFPACT_STACK_PUSH:
5649     case OFPACT_STACK_POP:
5650     case OFPACT_DEC_TTL:
5651     case OFPACT_SET_MPLS_LABEL:
5652     case OFPACT_SET_MPLS_TC:
5653     case OFPACT_SET_MPLS_TTL:
5654     case OFPACT_DEC_MPLS_TTL:
5655     case OFPACT_PUSH_MPLS:
5656     case OFPACT_POP_MPLS:
5657     case OFPACT_SET_TUNNEL:
5658     case OFPACT_SET_QUEUE:
5659     case OFPACT_POP_QUEUE:
5660     case OFPACT_FIN_TIMEOUT:
5661     case OFPACT_RESUBMIT:
5662     case OFPACT_LEARN:
5663     case OFPACT_CONJUNCTION:
5664     case OFPACT_MULTIPATH:
5665     case OFPACT_NOTE:
5666     case OFPACT_EXIT:
5667     case OFPACT_UNROLL_XLATE:
5668     case OFPACT_SAMPLE:
5669     case OFPACT_DEBUG_RECIRC:
5670     case OFPACT_CT:
5671     default:
5672         return OVSINST_OFPIT11_APPLY_ACTIONS;
5673     }
5674 }
5675
5676 enum ofperr
5677 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5678                                     const uint16_t inst_type)
5679 {
5680     switch (inst_type) {
5681
5682 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5683     case ENUM:                                      \
5684         *instruction_type = OVSINST_##ENUM;         \
5685         return 0;
5686 OVS_INSTRUCTIONS
5687 #undef DEFINE_INST
5688
5689     default:
5690         return OFPERR_OFPBIC_UNKNOWN_INST;
5691     }
5692 }
5693
5694 /* Two-way translation between OVS's internal "OVSINST_*" representation of
5695  * instructions and the "OFPIT_*" representation used in OpenFlow. */
5696 struct ovsinst_map {
5697     enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
5698     int ofpit;                         /* OFPIT_* number from OpenFlow spec. */
5699 };
5700
5701 static const struct ovsinst_map *
5702 get_ovsinst_map(enum ofp_version version)
5703 {
5704     /* OpenFlow 1.1 and 1.2 instructions. */
5705     static const struct ovsinst_map of11[] = {
5706         { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5707         { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5708         { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5709         { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5710         { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5711         { 0, -1 },
5712     };
5713
5714     /* OpenFlow 1.3+ instructions. */
5715     static const struct ovsinst_map of13[] = {
5716         { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5717         { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5718         { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5719         { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5720         { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5721         { OVSINST_OFPIT13_METER, 6 },
5722         { 0, -1 },
5723     };
5724
5725     return version < OFP13_VERSION ? of11 : of13;
5726 }
5727
5728 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5729  * values, into a bitmap of instructions suitable for OpenFlow 'version'
5730  * (OFP11_VERSION or later), and returns the result. */
5731 ovs_be32
5732 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
5733 {
5734     uint32_t ofpit_bitmap = 0;
5735     const struct ovsinst_map *x;
5736
5737     for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5738         if (ovsinst_bitmap & (1u << x->ovsinst)) {
5739             ofpit_bitmap |= 1u << x->ofpit;
5740         }
5741     }
5742     return htonl(ofpit_bitmap);
5743 }
5744
5745 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5746  * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5747  * correspond to OVSINST_* values, and returns the result. */
5748 uint32_t
5749 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5750 {
5751     uint32_t ovsinst_bitmap = 0;
5752     const struct ovsinst_map *x;
5753
5754     for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5755         if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5756             ovsinst_bitmap |= 1u << x->ovsinst;
5757         }
5758     }
5759     return ovsinst_bitmap;
5760 }
5761
5762 static inline struct ofp11_instruction *
5763 instruction_next(const struct ofp11_instruction *inst)
5764 {
5765     return ((struct ofp11_instruction *) (void *)
5766             ((uint8_t *) inst + ntohs(inst->len)));
5767 }
5768
5769 static inline bool
5770 instruction_is_valid(const struct ofp11_instruction *inst,
5771                      size_t n_instructions)
5772 {
5773     uint16_t len = ntohs(inst->len);
5774     return (!(len % OFP11_INSTRUCTION_ALIGN)
5775             && len >= sizeof *inst
5776             && len / sizeof *inst <= n_instructions);
5777 }
5778
5779 /* This macro is careful to check for instructions with bad lengths. */
5780 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS)  \
5781     for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS);            \
5782          (LEFT) > 0 && instruction_is_valid(ITER, LEFT);                \
5783          ((LEFT) -= (ntohs((ITER)->len)                                 \
5784                      / sizeof(struct ofp11_instruction)),               \
5785           (ITER) = instruction_next(ITER)))
5786
5787 static enum ofperr
5788 decode_openflow11_instruction(const struct ofp11_instruction *inst,
5789                               enum ovs_instruction_type *type)
5790 {
5791     uint16_t len = ntohs(inst->len);
5792
5793     switch (inst->type) {
5794     case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5795         return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5796
5797 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)     \
5798         case CONSTANT_HTONS(ENUM):                      \
5799             if (EXTENSIBLE                              \
5800                 ? len >= sizeof(struct STRUCT)          \
5801                 : len == sizeof(struct STRUCT)) {       \
5802                 *type = OVSINST_##ENUM;                 \
5803                 return 0;                               \
5804             } else {                                    \
5805                 return OFPERR_OFPBIC_BAD_LEN;           \
5806             }
5807 OVS_INSTRUCTIONS
5808 #undef DEFINE_INST
5809
5810     default:
5811         return OFPERR_OFPBIC_UNKNOWN_INST;
5812     }
5813 }
5814
5815 static enum ofperr
5816 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5817                                size_t n_insts,
5818                                const struct ofp11_instruction *out[])
5819 {
5820     const struct ofp11_instruction *inst;
5821     size_t left;
5822
5823     memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5824     INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5825         enum ovs_instruction_type type;
5826         enum ofperr error;
5827
5828         error = decode_openflow11_instruction(inst, &type);
5829         if (error) {
5830             return error;
5831         }
5832
5833         if (out[type]) {
5834             return OFPERR_OFPBIC_DUP_INST;
5835         }
5836         out[type] = inst;
5837     }
5838
5839     if (left) {
5840         VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5841                      (n_insts - left) * sizeof *inst);
5842         return OFPERR_OFPBIC_BAD_LEN;
5843     }
5844     return 0;
5845 }
5846
5847 static void
5848 get_actions_from_instruction(const struct ofp11_instruction *inst,
5849                              const struct ofp_action_header **actions,
5850                              size_t *actions_len)
5851 {
5852     *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5853     *actions_len = ntohs(inst->len) - sizeof *inst;
5854 }
5855
5856 enum ofperr
5857 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5858                                    unsigned int instructions_len,
5859                                    enum ofp_version version,
5860                                    struct ofpbuf *ofpacts)
5861 {
5862     const struct ofp11_instruction *instructions;
5863     const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5864     enum ofperr error;
5865
5866     if (version == OFP10_VERSION) {
5867         return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5868                                                version,
5869                                                (1u << N_OVS_INSTRUCTIONS) - 1,
5870                                                ofpacts, 0);
5871     }
5872
5873     ofpbuf_clear(ofpacts);
5874
5875     if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5876         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5877                      "multiple of %d",
5878                      instructions_len, OFP11_INSTRUCTION_ALIGN);
5879         error = OFPERR_OFPBIC_BAD_LEN;
5880         goto exit;
5881     }
5882
5883     instructions = ofpbuf_try_pull(openflow, instructions_len);
5884     if (instructions == NULL) {
5885         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5886                      "remaining message length (%"PRIu32")",
5887                      instructions_len, openflow->size);
5888         error = OFPERR_OFPBIC_BAD_LEN;
5889         goto exit;
5890     }
5891
5892     error = decode_openflow11_instructions(
5893         instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5894         insts);
5895     if (error) {
5896         goto exit;
5897     }
5898
5899     if (insts[OVSINST_OFPIT13_METER]) {
5900         const struct ofp13_instruction_meter *oim;
5901         struct ofpact_meter *om;
5902
5903         oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5904                            insts[OVSINST_OFPIT13_METER]);
5905
5906         om = ofpact_put_METER(ofpacts);
5907         om->meter_id = ntohl(oim->meter_id);
5908     }
5909     if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5910         const struct ofp_action_header *actions;
5911         size_t actions_len;
5912
5913         get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5914                                      &actions, &actions_len);
5915         error = ofpacts_decode(actions, actions_len, version, ofpacts);
5916         if (error) {
5917             goto exit;
5918         }
5919     }
5920     if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5921         instruction_get_OFPIT11_CLEAR_ACTIONS(
5922             insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5923         ofpact_put_CLEAR_ACTIONS(ofpacts);
5924     }
5925     if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5926         struct ofpact_nest *on;
5927         const struct ofp_action_header *actions;
5928         size_t actions_len;
5929         size_t start;
5930
5931         ofpact_pad(ofpacts);
5932         start = ofpacts->size;
5933         ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5934                    offsetof(struct ofpact_nest, actions));
5935         get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5936                                      &actions, &actions_len);
5937         error = ofpacts_decode_for_action_set(actions, actions_len,
5938                                               version, ofpacts);
5939         if (error) {
5940             goto exit;
5941         }
5942         on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5943         on->ofpact.len = ofpacts->size - start;
5944     }
5945     if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5946         const struct ofp11_instruction_write_metadata *oiwm;
5947         struct ofpact_metadata *om;
5948
5949         oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5950                             insts[OVSINST_OFPIT11_WRITE_METADATA]);
5951
5952         om = ofpact_put_WRITE_METADATA(ofpacts);
5953         om->metadata = oiwm->metadata;
5954         om->mask = oiwm->metadata_mask;
5955     }
5956     if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5957         const struct ofp11_instruction_goto_table *oigt;
5958         struct ofpact_goto_table *ogt;
5959
5960         oigt = instruction_get_OFPIT11_GOTO_TABLE(
5961             insts[OVSINST_OFPIT11_GOTO_TABLE]);
5962         ogt = ofpact_put_GOTO_TABLE(ofpacts);
5963         ogt->table_id = oigt->table_id;
5964     }
5965
5966     error = ofpacts_verify(ofpacts->data, ofpacts->size,
5967                            (1u << N_OVS_INSTRUCTIONS) - 1, 0);
5968 exit:
5969     if (error) {
5970         ofpbuf_clear(ofpacts);
5971     }
5972     return error;
5973 }
5974
5975 /* Update the length of the instruction that begins at offset 'ofs' within
5976  * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5977  * If the instruction contains no nested actions, deletes it entirely. */
5978 static void
5979 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5980 {
5981     struct ofp11_instruction_actions *oia;
5982
5983     oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5984     if (openflow->size > ofs + sizeof *oia) {
5985         oia->len = htons(openflow->size - ofs);
5986     } else {
5987         openflow->size = ofs;
5988     }
5989 }
5990 \f
5991 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5992  * switch will never have more than 'max_ports' ports.  Returns 0 if 'port' is
5993  * valid, otherwise an OpenFlow error code. */
5994 enum ofperr
5995 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5996 {
5997     switch (port) {
5998     case OFPP_IN_PORT:
5999     case OFPP_TABLE:
6000     case OFPP_NORMAL:
6001     case OFPP_FLOOD:
6002     case OFPP_ALL:
6003     case OFPP_CONTROLLER:
6004     case OFPP_LOCAL:
6005         return 0;
6006
6007     case OFPP_NONE:
6008         return OFPERR_OFPBAC_BAD_OUT_PORT;
6009
6010     default:
6011         if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
6012             return 0;
6013         }
6014         return OFPERR_OFPBAC_BAD_OUT_PORT;
6015     }
6016 }
6017
6018 /* Removes the protocols that require consistency between match and actions
6019  * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6020  *
6021  * (An example of an inconsistency between match and actions is a flow that
6022  * does not match on an MPLS Ethertype but has an action that pops an MPLS
6023  * label.) */
6024 static void
6025 inconsistent_match(enum ofputil_protocol *usable_protocols)
6026 {
6027     *usable_protocols &= OFPUTIL_P_OF10_ANY;
6028 }
6029
6030 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6031  * caller must restore them.
6032  *
6033  * Modifies some actions, filling in fields that could not be properly set
6034  * without context. */
6035 static enum ofperr
6036 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
6037                struct flow *flow, ofp_port_t max_ports,
6038                uint8_t table_id, uint8_t n_tables)
6039 {
6040     const struct ofpact_enqueue *enqueue;
6041     const struct mf_field *mf;
6042
6043     switch (a->type) {
6044     case OFPACT_OUTPUT:
6045         return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
6046                                         max_ports);
6047
6048     case OFPACT_CONTROLLER:
6049         return 0;
6050
6051     case OFPACT_ENQUEUE:
6052         enqueue = ofpact_get_ENQUEUE(a);
6053         if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
6054             && enqueue->port != OFPP_IN_PORT
6055             && enqueue->port != OFPP_LOCAL) {
6056             return OFPERR_OFPBAC_BAD_OUT_PORT;
6057         }
6058         return 0;
6059
6060     case OFPACT_OUTPUT_REG:
6061         return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
6062
6063     case OFPACT_BUNDLE:
6064         return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
6065
6066     case OFPACT_SET_VLAN_VID:
6067         /* Remember if we saw a vlan tag in the flow to aid translating to
6068          * OpenFlow 1.1+ if need be. */
6069         ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
6070             (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6071         if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6072             !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
6073             inconsistent_match(usable_protocols);
6074         }
6075         /* Temporary mark that we have a vlan tag. */
6076         flow->vlan_tci |= htons(VLAN_CFI);
6077         return 0;
6078
6079     case OFPACT_SET_VLAN_PCP:
6080         /* Remember if we saw a vlan tag in the flow to aid translating to
6081          * OpenFlow 1.1+ if need be. */
6082         ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
6083             (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6084         if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6085             !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
6086             inconsistent_match(usable_protocols);
6087         }
6088         /* Temporary mark that we have a vlan tag. */
6089         flow->vlan_tci |= htons(VLAN_CFI);
6090         return 0;
6091
6092     case OFPACT_STRIP_VLAN:
6093         if (!(flow->vlan_tci & htons(VLAN_CFI))) {
6094             inconsistent_match(usable_protocols);
6095         }
6096         /* Temporary mark that we have no vlan tag. */
6097         flow->vlan_tci = htons(0);
6098         return 0;
6099
6100     case OFPACT_PUSH_VLAN:
6101         if (flow->vlan_tci & htons(VLAN_CFI)) {
6102             /* Multiple VLAN headers not supported. */
6103             return OFPERR_OFPBAC_BAD_TAG;
6104         }
6105         /* Temporary mark that we have a vlan tag. */
6106         flow->vlan_tci |= htons(VLAN_CFI);
6107         return 0;
6108
6109     case OFPACT_SET_ETH_SRC:
6110     case OFPACT_SET_ETH_DST:
6111         return 0;
6112
6113     case OFPACT_SET_IPV4_SRC:
6114     case OFPACT_SET_IPV4_DST:
6115         if (flow->dl_type != htons(ETH_TYPE_IP)) {
6116             inconsistent_match(usable_protocols);
6117         }
6118         return 0;
6119
6120     case OFPACT_SET_IP_DSCP:
6121     case OFPACT_SET_IP_ECN:
6122     case OFPACT_SET_IP_TTL:
6123     case OFPACT_DEC_TTL:
6124         if (!is_ip_any(flow)) {
6125             inconsistent_match(usable_protocols);
6126         }
6127         return 0;
6128
6129     case OFPACT_SET_L4_SRC_PORT:
6130     case OFPACT_SET_L4_DST_PORT:
6131         if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
6132             (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
6133              && flow->nw_proto != IPPROTO_SCTP)) {
6134             inconsistent_match(usable_protocols);
6135         }
6136         /* Note on which transport protocol the port numbers are set.
6137          * This allows this set action to be converted to an OF1.2 set field
6138          * action. */
6139         if (a->type == OFPACT_SET_L4_SRC_PORT) {
6140             ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
6141         } else {
6142             ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
6143         }
6144         return 0;
6145
6146     case OFPACT_REG_MOVE:
6147         return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
6148
6149     case OFPACT_SET_FIELD:
6150         mf = ofpact_get_SET_FIELD(a)->field;
6151         /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6152         if (!mf_are_prereqs_ok(mf, flow) ||
6153             (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
6154             VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
6155                          mf->name);
6156             return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6157         }
6158         /* Remember if we saw a vlan tag in the flow to aid translating to
6159          * OpenFlow 1.1 if need be. */
6160         ofpact_get_SET_FIELD(a)->flow_has_vlan =
6161             (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6162         if (mf->id == MFF_VLAN_TCI) {
6163             /* The set field may add or remove the vlan tag,
6164              * Mark the status temporarily. */
6165             flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
6166         }
6167         return 0;
6168
6169     case OFPACT_STACK_PUSH:
6170         return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6171
6172     case OFPACT_STACK_POP:
6173         return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6174
6175     case OFPACT_SET_MPLS_LABEL:
6176     case OFPACT_SET_MPLS_TC:
6177     case OFPACT_SET_MPLS_TTL:
6178     case OFPACT_DEC_MPLS_TTL:
6179         if (!eth_type_mpls(flow->dl_type)) {
6180             inconsistent_match(usable_protocols);
6181         }
6182         return 0;
6183
6184     case OFPACT_SET_TUNNEL:
6185     case OFPACT_SET_QUEUE:
6186     case OFPACT_POP_QUEUE:
6187     case OFPACT_RESUBMIT:
6188         return 0;
6189
6190     case OFPACT_FIN_TIMEOUT:
6191         if (flow->nw_proto != IPPROTO_TCP) {
6192             inconsistent_match(usable_protocols);
6193         }
6194         return 0;
6195
6196     case OFPACT_LEARN:
6197         return learn_check(ofpact_get_LEARN(a), flow);
6198
6199     case OFPACT_CONJUNCTION:
6200         return 0;
6201
6202     case OFPACT_MULTIPATH:
6203         return multipath_check(ofpact_get_MULTIPATH(a), flow);
6204
6205     case OFPACT_NOTE:
6206     case OFPACT_EXIT:
6207         return 0;
6208
6209     case OFPACT_PUSH_MPLS:
6210         flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6211         /* The packet is now MPLS and the MPLS payload is opaque.
6212          * Thus nothing can be assumed about the network protocol.
6213          * Temporarily mark that we have no nw_proto. */
6214         flow->nw_proto = 0;
6215         return 0;
6216
6217     case OFPACT_POP_MPLS:
6218         if (!eth_type_mpls(flow->dl_type)) {
6219             inconsistent_match(usable_protocols);
6220         }
6221         flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6222         return 0;
6223
6224     case OFPACT_SAMPLE:
6225         return 0;
6226
6227     case OFPACT_CT: {
6228         struct ofpact_conntrack *oc = ofpact_get_CT(a);
6229         enum ofperr err;
6230
6231         if (!dl_type_is_ip_any(flow->dl_type)
6232             || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6233             inconsistent_match(usable_protocols);
6234         }
6235
6236         if (oc->zone_src.field) {
6237             return mf_check_src(&oc->zone_src, flow);
6238         }
6239
6240         err = ofpacts_check(oc->actions, ofpact_ct_get_action_len(oc),
6241                             flow, max_ports, table_id, n_tables,
6242                             usable_protocols);
6243         return err;
6244     }
6245
6246     case OFPACT_CLEAR_ACTIONS:
6247         return 0;
6248
6249     case OFPACT_WRITE_ACTIONS: {
6250         /* Use a temporary copy of 'usable_protocols' because we can't check
6251          * consistency of an action set. */
6252         struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6253         enum ofputil_protocol p = *usable_protocols;
6254         return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6255                              flow, max_ports, table_id, n_tables, &p);
6256     }
6257
6258     case OFPACT_WRITE_METADATA:
6259         return 0;
6260
6261     case OFPACT_METER: {
6262         uint32_t mid = ofpact_get_METER(a)->meter_id;
6263         if (mid == 0 || mid > OFPM13_MAX) {
6264             return OFPERR_OFPMMFC_INVALID_METER;
6265         }
6266         return 0;
6267     }
6268
6269     case OFPACT_GOTO_TABLE: {
6270         uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6271         if ((table_id != 255 && goto_table <= table_id)
6272             || (n_tables != 255 && goto_table >= n_tables)) {
6273             return OFPERR_OFPBIC_BAD_TABLE_ID;
6274         }
6275         return 0;
6276     }
6277
6278     case OFPACT_GROUP:
6279         return 0;
6280
6281     case OFPACT_UNROLL_XLATE:
6282         /* UNROLL is an internal action that should never be seen via
6283          * OpenFlow. */
6284         return OFPERR_OFPBAC_BAD_TYPE;
6285
6286     case OFPACT_DEBUG_RECIRC:
6287         return 0;
6288
6289     default:
6290         OVS_NOT_REACHED();
6291     }
6292 }
6293
6294 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6295  * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6296  * switch with no more than 'max_ports' ports.
6297  *
6298  * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6299  * '*usable_protocols' the protocols that forbid the inconsistency.  (An
6300  * example of an inconsistency between match and actions is a flow that does
6301  * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6302  *
6303  * May annotate ofpacts with information gathered from the 'flow'.
6304  *
6305  * May temporarily modify 'flow', but restores the changes before returning. */
6306 enum ofperr
6307 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6308               struct flow *flow, ofp_port_t max_ports,
6309               uint8_t table_id, uint8_t n_tables,
6310               enum ofputil_protocol *usable_protocols)
6311 {
6312     struct ofpact *a;
6313     ovs_be16 dl_type = flow->dl_type;
6314     ovs_be16 vlan_tci = flow->vlan_tci;
6315     uint8_t nw_proto = flow->nw_proto;
6316     enum ofperr error = 0;
6317
6318     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6319         error = ofpact_check__(usable_protocols, a, flow,
6320                                max_ports, table_id, n_tables);
6321         if (error) {
6322             break;
6323         }
6324     }
6325     /* Restore fields that may have been modified. */
6326     flow->dl_type = dl_type;
6327     flow->vlan_tci = vlan_tci;
6328     flow->nw_proto = nw_proto;
6329     return error;
6330 }
6331
6332 /* Like ofpacts_check(), but reports inconsistencies as
6333  * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6334 enum ofperr
6335 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6336                           struct flow *flow, ofp_port_t max_ports,
6337                           uint8_t table_id, uint8_t n_tables,
6338                           enum ofputil_protocol usable_protocols)
6339 {
6340     enum ofputil_protocol p = usable_protocols;
6341     enum ofperr error;
6342
6343     error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6344                           table_id, n_tables, &p);
6345     return (error ? error
6346             : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6347             : 0);
6348 }
6349
6350 /* Returns the destination field that 'ofpact' would write to, or NULL
6351  * if the action would not write to an mf_field. */
6352 const struct mf_field *
6353 ofpact_get_mf_dst(const struct ofpact *ofpact)
6354 {
6355     if (ofpact->type == OFPACT_SET_FIELD) {
6356         const struct ofpact_set_field *orl;
6357
6358         orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
6359         return orl->field;
6360     } else if (ofpact->type == OFPACT_REG_MOVE) {
6361         const struct ofpact_reg_move *orm;
6362
6363         orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
6364         return orm->dst.field;
6365     }
6366
6367     return NULL;
6368 }
6369
6370 static enum ofperr
6371 unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action)
6372 {
6373     VLOG_WARN("%s action doesn't support nested action %s",
6374               ofpact_name(outer_action), ofpact_name(action));
6375     return OFPERR_OFPBAC_BAD_ARGUMENT;
6376 }
6377
6378 static bool
6379 field_requires_ct(enum mf_field_id field)
6380 {
6381     return field == MFF_CT_MARK || field == MFF_CT_LABEL;
6382 }
6383
6384 /* Apply nesting constraints for actions */
6385 static enum ofperr
6386 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6387 {
6388     const struct mf_field *field = ofpact_get_mf_dst(a);
6389
6390     if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
6391         VLOG_WARN("cannot set CT fields outside of ct action");
6392         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6393     }
6394
6395     if (outer_action) {
6396         ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
6397                    || outer_action == OFPACT_CT);
6398
6399         if (outer_action == OFPACT_CT) {
6400             if (!field) {
6401                 return unsupported_nesting(a->type, outer_action);
6402             } else if (!field_requires_ct(field->id)) {
6403                 VLOG_WARN("%s action doesn't support nested modification "
6404                           "of %s", ofpact_name(outer_action), field->name);
6405                 return OFPERR_OFPBAC_BAD_ARGUMENT;
6406             }
6407         }
6408     }
6409
6410     return 0;
6411 }
6412
6413 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6414  * appropriate order as defined by the OpenFlow spec and as required by Open
6415  * vSwitch.
6416  *
6417  * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
6418  * instructions that are allowed within 'ofpacts[]'.
6419  *
6420  * If 'outer_action' is not zero, it specifies that the actions are nested
6421  * within another action of type 'outer_action'. */
6422 static enum ofperr
6423 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
6424                uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
6425 {
6426     const struct ofpact *a;
6427     enum ovs_instruction_type inst;
6428
6429     inst = OVSINST_OFPIT13_METER;
6430     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6431         enum ovs_instruction_type next;
6432         enum ofperr error;
6433
6434         if (a->type == OFPACT_CONJUNCTION) {
6435             OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6436                 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6437                     VLOG_WARN("\"conjunction\" actions may be used along with "
6438                               "\"note\" but not any other kind of action "
6439                               "(such as the \"%s\" action used here)",
6440                               ofpact_name(a->type));
6441                     return OFPERR_NXBAC_BAD_CONJUNCTION;
6442                 }
6443             }
6444             return 0;
6445         }
6446
6447         error = ofpacts_verify_nested(a, outer_action);
6448         if (error) {
6449             return error;
6450         }
6451
6452         next = ovs_instruction_type_from_ofpact_type(a->type);
6453         if (a > ofpacts
6454             && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
6455                 ? next < inst
6456                 : next <= inst)) {
6457             const char *name = ovs_instruction_name_from_type(inst);
6458             const char *next_name = ovs_instruction_name_from_type(next);
6459
6460             if (next == inst) {
6461                 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
6462                           "1.1+ compatibility", name);
6463             } else {
6464                 VLOG_WARN("invalid instruction ordering: %s must appear "
6465                           "before %s, for OpenFlow 1.1+ compatibility",
6466                           next_name, name);
6467             }
6468             return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
6469         }
6470         if (!((1u << next) & allowed_ovsinsts)) {
6471             const char *name = ovs_instruction_name_from_type(next);
6472
6473             VLOG_WARN("%s instruction not allowed here", name);
6474             return OFPERR_OFPBIC_UNSUP_INST;
6475         }
6476
6477         inst = next;
6478     }
6479
6480     return 0;
6481 }
6482 \f
6483 /* Converting ofpacts to OpenFlow. */
6484
6485 static void
6486 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6487               struct ofpbuf *out)
6488 {
6489     switch (a->type) {
6490 #define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
6491         case OFPACT_##ENUM:                                             \
6492             encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out);      \
6493             return;
6494         OFPACTS
6495 #undef OFPACT
6496     default:
6497         OVS_NOT_REACHED();
6498     }
6499 }
6500
6501 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6502  * actions in 'openflow', appending the actions to any existing data in
6503  * 'openflow'. */
6504 size_t
6505 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6506                              struct ofpbuf *openflow,
6507                              enum ofp_version ofp_version)
6508 {
6509     const struct ofpact *a;
6510     size_t start_size = openflow->size;
6511
6512     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6513         encode_ofpact(a, ofp_version, openflow);
6514     }
6515     return openflow->size - start_size;
6516 }
6517
6518 static enum ovs_instruction_type
6519 ofpact_is_apply_actions(const struct ofpact *a)
6520 {
6521     return (ovs_instruction_type_from_ofpact_type(a->type)
6522             == OVSINST_OFPIT11_APPLY_ACTIONS);
6523 }
6524
6525 void
6526 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
6527                                   size_t ofpacts_len,
6528                                   struct ofpbuf *openflow,
6529                                   enum ofp_version ofp_version)
6530 {
6531     const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
6532     const struct ofpact *a;
6533
6534     if (ofp_version == OFP10_VERSION) {
6535         ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
6536                                      ofp_version);
6537         return;
6538     }
6539
6540     a = ofpacts;
6541     while (a < end) {
6542         if (ofpact_is_apply_actions(a)) {
6543             size_t ofs = openflow->size;
6544
6545             instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
6546             do {
6547                 encode_ofpact(a, ofp_version, openflow);
6548                 a = ofpact_next(a);
6549             } while (a < end && ofpact_is_apply_actions(a));
6550             ofpacts_update_instruction_actions(openflow, ofs);
6551         } else {
6552             encode_ofpact(a, ofp_version, openflow);
6553             a = ofpact_next(a);
6554         }
6555     }
6556 }
6557 \f
6558 /* Sets of supported actions. */
6559
6560 /* Two-way translation between OVS's internal "OFPACT_*" representation of
6561  * actions and the "OFPAT_*" representation used in some OpenFlow version.
6562  * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6563  * instance is specific to one OpenFlow version.) */
6564 struct ofpact_map {
6565     enum ofpact_type ofpact;    /* Internal name for action type. */
6566     int ofpat;                  /* OFPAT_* number from OpenFlow spec. */
6567 };
6568
6569 static const struct ofpact_map *
6570 get_ofpact_map(enum ofp_version version)
6571 {
6572     /* OpenFlow 1.0 actions. */
6573     static const struct ofpact_map of10[] = {
6574         { OFPACT_OUTPUT, 0 },
6575         { OFPACT_SET_VLAN_VID, 1 },
6576         { OFPACT_SET_VLAN_PCP, 2 },
6577         { OFPACT_STRIP_VLAN, 3 },
6578         { OFPACT_SET_ETH_SRC, 4 },
6579         { OFPACT_SET_ETH_DST, 5 },
6580         { OFPACT_SET_IPV4_SRC, 6 },
6581         { OFPACT_SET_IPV4_DST, 7 },
6582         { OFPACT_SET_IP_DSCP, 8 },
6583         { OFPACT_SET_L4_SRC_PORT, 9 },
6584         { OFPACT_SET_L4_DST_PORT, 10 },
6585         { OFPACT_ENQUEUE, 11 },
6586         { 0, -1 },
6587     };
6588
6589     /* OpenFlow 1.1 actions. */
6590     static const struct ofpact_map of11[] = {
6591         { OFPACT_OUTPUT, 0 },
6592         { OFPACT_SET_VLAN_VID, 1 },
6593         { OFPACT_SET_VLAN_PCP, 2 },
6594         { OFPACT_SET_ETH_SRC, 3 },
6595         { OFPACT_SET_ETH_DST, 4 },
6596         { OFPACT_SET_IPV4_SRC, 5 },
6597         { OFPACT_SET_IPV4_DST, 6 },
6598         { OFPACT_SET_IP_DSCP, 7 },
6599         { OFPACT_SET_IP_ECN, 8 },
6600         { OFPACT_SET_L4_SRC_PORT, 9 },
6601         { OFPACT_SET_L4_DST_PORT, 10 },
6602         /* OFPAT_COPY_TTL_OUT (11) not supported. */
6603         /* OFPAT_COPY_TTL_IN (12) not supported. */
6604         { OFPACT_SET_MPLS_LABEL, 13 },
6605         { OFPACT_SET_MPLS_TC, 14 },
6606         { OFPACT_SET_MPLS_TTL, 15 },
6607         { OFPACT_DEC_MPLS_TTL, 16 },
6608         { OFPACT_PUSH_VLAN, 17 },
6609         { OFPACT_STRIP_VLAN, 18 },
6610         { OFPACT_PUSH_MPLS, 19 },
6611         { OFPACT_POP_MPLS, 20 },
6612         { OFPACT_SET_QUEUE, 21 },
6613         { OFPACT_GROUP, 22 },
6614         { OFPACT_SET_IP_TTL, 23 },
6615         { OFPACT_DEC_TTL, 24 },
6616         { 0, -1 },
6617     };
6618
6619     /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6620     static const struct ofpact_map of12[] = {
6621         { OFPACT_OUTPUT, 0 },
6622         /* OFPAT_COPY_TTL_OUT (11) not supported. */
6623         /* OFPAT_COPY_TTL_IN (12) not supported. */
6624         { OFPACT_SET_MPLS_TTL, 15 },
6625         { OFPACT_DEC_MPLS_TTL, 16 },
6626         { OFPACT_PUSH_VLAN, 17 },
6627         { OFPACT_STRIP_VLAN, 18 },
6628         { OFPACT_PUSH_MPLS, 19 },
6629         { OFPACT_POP_MPLS, 20 },
6630         { OFPACT_SET_QUEUE, 21 },
6631         { OFPACT_GROUP, 22 },
6632         { OFPACT_SET_IP_TTL, 23 },
6633         { OFPACT_DEC_TTL, 24 },
6634         { OFPACT_SET_FIELD, 25 },
6635         /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6636         /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6637         { 0, -1 },
6638     };
6639
6640     switch (version) {
6641     case OFP10_VERSION:
6642         return of10;
6643
6644     case OFP11_VERSION:
6645         return of11;
6646
6647     case OFP12_VERSION:
6648     case OFP13_VERSION:
6649     case OFP14_VERSION:
6650     case OFP15_VERSION:
6651     default:
6652         return of12;
6653     }
6654 }
6655
6656 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6657  * values, into a bitmap of actions suitable for OpenFlow 'version', and
6658  * returns the result. */
6659 ovs_be32
6660 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6661 {
6662     uint32_t openflow_bitmap = 0;
6663     const struct ofpact_map *x;
6664
6665     for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6666         if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6667             openflow_bitmap |= 1u << x->ofpat;
6668         }
6669     }
6670     return htonl(openflow_bitmap);
6671 }
6672
6673 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6674  * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6675  * and returns the result. */
6676 uint64_t
6677 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6678 {
6679     uint64_t ofpact_bitmap = 0;
6680     const struct ofpact_map *x;
6681
6682     for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6683         if (ofpat_bitmap & htonl(1u << x->ofpat)) {
6684             ofpact_bitmap |= UINT64_C(1) << x->ofpact;
6685         }
6686     }
6687     return ofpact_bitmap;
6688 }
6689
6690 /* Appends to 's' a string representation of the set of OFPACT_* represented
6691  * by 'ofpacts_bitmap'. */
6692 void
6693 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
6694 {
6695     if (!ofpacts_bitmap) {
6696         ds_put_cstr(s, "<none>");
6697     } else {
6698         while (ofpacts_bitmap) {
6699             ds_put_format(s, "%s ",
6700                           ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
6701             ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
6702         }
6703         ds_chomp(s, ' ');
6704     }
6705 }
6706 \f
6707 /* Returns true if 'action' outputs to 'port', false otherwise. */
6708 static bool
6709 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
6710 {
6711     switch (ofpact->type) {
6712     case OFPACT_OUTPUT:
6713         return ofpact_get_OUTPUT(ofpact)->port == port;
6714     case OFPACT_ENQUEUE:
6715         return ofpact_get_ENQUEUE(ofpact)->port == port;
6716     case OFPACT_CONTROLLER:
6717         return port == OFPP_CONTROLLER;
6718
6719     case OFPACT_OUTPUT_REG:
6720     case OFPACT_BUNDLE:
6721     case OFPACT_SET_VLAN_VID:
6722     case OFPACT_SET_VLAN_PCP:
6723     case OFPACT_STRIP_VLAN:
6724     case OFPACT_PUSH_VLAN:
6725     case OFPACT_SET_ETH_SRC:
6726     case OFPACT_SET_ETH_DST:
6727     case OFPACT_SET_IPV4_SRC:
6728     case OFPACT_SET_IPV4_DST:
6729     case OFPACT_SET_IP_DSCP:
6730     case OFPACT_SET_IP_ECN:
6731     case OFPACT_SET_IP_TTL:
6732     case OFPACT_SET_L4_SRC_PORT:
6733     case OFPACT_SET_L4_DST_PORT:
6734     case OFPACT_REG_MOVE:
6735     case OFPACT_SET_FIELD:
6736     case OFPACT_STACK_PUSH:
6737     case OFPACT_STACK_POP:
6738     case OFPACT_DEC_TTL:
6739     case OFPACT_SET_MPLS_LABEL:
6740     case OFPACT_SET_MPLS_TC:
6741     case OFPACT_SET_MPLS_TTL:
6742     case OFPACT_DEC_MPLS_TTL:
6743     case OFPACT_SET_TUNNEL:
6744     case OFPACT_WRITE_METADATA:
6745     case OFPACT_SET_QUEUE:
6746     case OFPACT_POP_QUEUE:
6747     case OFPACT_FIN_TIMEOUT:
6748     case OFPACT_RESUBMIT:
6749     case OFPACT_LEARN:
6750     case OFPACT_CONJUNCTION:
6751     case OFPACT_MULTIPATH:
6752     case OFPACT_NOTE:
6753     case OFPACT_EXIT:
6754     case OFPACT_UNROLL_XLATE:
6755     case OFPACT_PUSH_MPLS:
6756     case OFPACT_POP_MPLS:
6757     case OFPACT_SAMPLE:
6758     case OFPACT_CLEAR_ACTIONS:
6759     case OFPACT_WRITE_ACTIONS:
6760     case OFPACT_GOTO_TABLE:
6761     case OFPACT_METER:
6762     case OFPACT_GROUP:
6763     case OFPACT_DEBUG_RECIRC:
6764     case OFPACT_CT:
6765     default:
6766         return false;
6767     }
6768 }
6769
6770 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6771  * to 'port', false otherwise. */
6772 bool
6773 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
6774                        ofp_port_t port)
6775 {
6776     const struct ofpact *a;
6777
6778     OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
6779         if (ofpact_outputs_to_port(a, port)) {
6780             return true;
6781         }
6782     }
6783
6784     return false;
6785 }
6786
6787 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6788  * to 'group', false otherwise. */
6789 bool
6790 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
6791                         uint32_t group_id)
6792 {
6793     const struct ofpact *a;
6794
6795     OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
6796         if (a->type == OFPACT_GROUP
6797             && ofpact_get_GROUP(a)->group_id == group_id) {
6798             return true;
6799         }
6800     }
6801
6802     return false;
6803 }
6804
6805 bool
6806 ofpacts_equal(const struct ofpact *a, size_t a_len,
6807               const struct ofpact *b, size_t b_len)
6808 {
6809     return a_len == b_len && !memcmp(a, b, a_len);
6810 }
6811
6812 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6813  * 'ofpacts'.  If found, returns its meter ID; if not, returns 0.
6814  *
6815  * This function relies on the order of 'ofpacts' being correct (as checked by
6816  * ofpacts_verify()). */
6817 uint32_t
6818 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
6819 {
6820     const struct ofpact *a;
6821
6822     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6823         enum ovs_instruction_type inst;
6824
6825         inst = ovs_instruction_type_from_ofpact_type(a->type);
6826         if (a->type == OFPACT_METER) {
6827             return ofpact_get_METER(a)->meter_id;
6828         } else if (inst > OVSINST_OFPIT13_METER) {
6829             break;
6830         }
6831     }
6832
6833     return 0;
6834 }
6835 \f
6836 /* Formatting ofpacts. */
6837
6838 static void
6839 ofpact_format(const struct ofpact *a, struct ds *s)
6840 {
6841     switch (a->type) {
6842 #define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
6843         case OFPACT_##ENUM:                                             \
6844             format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s);   \
6845             break;
6846         OFPACTS
6847 #undef OFPACT
6848     default:
6849         OVS_NOT_REACHED();
6850     }
6851 }
6852
6853 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6854  * 'ofpacts' to 'string'. */
6855 void
6856 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
6857                struct ds *string)
6858 {
6859     if (!ofpacts_len) {
6860         ds_put_cstr(string, "drop");
6861     } else {
6862         const struct ofpact *a;
6863
6864         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6865             if (a != ofpacts) {
6866                 ds_put_cstr(string, ",");
6867             }
6868
6869             /* XXX write-actions */
6870             ofpact_format(a, string);
6871         }
6872     }
6873 }
6874 \f
6875 /* Internal use by helpers. */
6876
6877 void *
6878 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6879 {
6880     struct ofpact *ofpact;
6881
6882     ofpact_pad(ofpacts);
6883     ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
6884     ofpact = ofpacts->header;
6885     ofpact_init(ofpact, type, len);
6886     return ofpact;
6887 }
6888
6889 void
6890 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6891 {
6892     memset(ofpact, 0, len);
6893     ofpact->type = type;
6894     ofpact->raw = -1;
6895     ofpact->len = len;
6896 }
6897 \f
6898 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6899  * starting at 'ofpact'.
6900  *
6901  * This is the correct way to update a variable-length ofpact's length after
6902  * adding the variable-length part of the payload.  (See the large comment
6903  * near the end of ofp-actions.h for more information.) */
6904 void
6905 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6906 {
6907     ovs_assert(ofpact == ofpacts->header);
6908     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6909 }
6910
6911 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
6912  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6913  * client must call this itself after adding the final ofpact to an array of
6914  * them.
6915  *
6916  * (The consequences of failing to call this function are probably not dire.
6917  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6918  * not dereference it.  That's undefined behavior, technically, but it will not
6919  * cause a real problem on common systems.  Still, it seems better to call
6920  * it.) */
6921 void
6922 ofpact_pad(struct ofpbuf *ofpacts)
6923 {
6924     unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
6925     if (pad) {
6926         ofpbuf_put_zeros(ofpacts, pad);
6927     }
6928 }
6929 \f
6930
6931
6932
6933 static char * OVS_WARN_UNUSED_RESULT
6934 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6935              enum ofputil_protocol *usable_protocols)
6936 {
6937     switch (type) {
6938 #define OFPACT(ENUM, STRUCT, MEMBER, NAME)                            \
6939         case OFPACT_##ENUM:                                             \
6940             return parse_##ENUM(value, ofpacts, usable_protocols);
6941         OFPACTS
6942 #undef OFPACT
6943     default:
6944         OVS_NOT_REACHED();
6945     }
6946 }
6947
6948 static bool
6949 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6950 {
6951 #define OFPACT(ENUM, STRUCT, MEMBER, NAME)                            \
6952     if (!strcasecmp(name, NAME)) {                                    \
6953         *type = OFPACT_##ENUM;                                          \
6954         return true;                                                    \
6955     }
6956     OFPACTS
6957 #undef OFPACT
6958
6959     return false;
6960 }
6961
6962 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6963  *
6964  * Returns NULL if successful, otherwise a malloc()'d string describing the
6965  * error.  The caller is responsible for freeing the returned string.
6966  *
6967  * If 'outer_action' is specified, indicates that the actions being parsed
6968  * are nested within another action of the type specified in 'outer_action'. */
6969 static char * OVS_WARN_UNUSED_RESULT
6970 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6971                 enum ofputil_protocol *usable_protocols,
6972                 bool allow_instructions, enum ofpact_type outer_action)
6973 {
6974     int prev_inst = -1;
6975     enum ofperr retval;
6976     char *key, *value;
6977     bool drop = false;
6978     char *pos;
6979
6980     pos = str;
6981     while (ofputil_parse_key_value(&pos, &key, &value)) {
6982         enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6983         enum ofpact_type type;
6984         char *error = NULL;
6985         ofp_port_t port;
6986
6987         if (ofpact_type_from_name(key, &type)) {
6988             error = ofpact_parse(type, value, ofpacts, usable_protocols);
6989             inst = ovs_instruction_type_from_ofpact_type(type);
6990         } else if (!strcasecmp(key, "mod_vlan_vid")) {
6991             error = parse_set_vlan_vid(value, ofpacts, true);
6992         } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6993             error = parse_set_vlan_pcp(value, ofpacts, true);
6994         } else if (!strcasecmp(key, "set_nw_ttl")) {
6995             error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6996         } else if (!strcasecmp(key, "pop_vlan")) {
6997             error = parse_pop_vlan(ofpacts);
6998         } else if (!strcasecmp(key, "set_tunnel64")) {
6999             error = parse_set_tunnel(value, ofpacts,
7000                                      NXAST_RAW_SET_TUNNEL64);
7001         } else if (!strcasecmp(key, "load")) {
7002             error = parse_reg_load(value, ofpacts);
7003         } else if (!strcasecmp(key, "bundle_load")) {
7004             error = parse_bundle_load(value, ofpacts);
7005         } else if (!strcasecmp(key, "drop")) {
7006             drop = true;
7007         } else if (!strcasecmp(key, "apply_actions")) {
7008             return xstrdup("apply_actions is the default instruction");
7009         } else if (ofputil_port_from_string(key, &port)) {
7010             ofpact_put_OUTPUT(ofpacts)->port = port;
7011         } else {
7012             return xasprintf("unknown action %s", key);
7013         }
7014         if (error) {
7015             return error;
7016         }
7017
7018         if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
7019             if (!allow_instructions) {
7020                 return xasprintf("only actions are allowed here (not "
7021                                  "instruction %s)",
7022                                  ovs_instruction_name_from_type(inst));
7023             }
7024             if (inst == prev_inst) {
7025                 return xasprintf("instruction %s may be specified only once",
7026                                  ovs_instruction_name_from_type(inst));
7027             }
7028         }
7029         if (prev_inst != -1 && inst < prev_inst) {
7030             return xasprintf("instruction %s must be specified before %s",
7031                              ovs_instruction_name_from_type(inst),
7032                              ovs_instruction_name_from_type(prev_inst));
7033         }
7034         prev_inst = inst;
7035     }
7036     ofpact_pad(ofpacts);
7037
7038     if (drop && ofpacts->size) {
7039         return xstrdup("\"drop\" must not be accompanied by any other action "
7040                        "or instruction");
7041     }
7042
7043     retval = ofpacts_verify(ofpacts->data, ofpacts->size,
7044                             (allow_instructions
7045                              ? (1u << N_OVS_INSTRUCTIONS) - 1
7046                              : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
7047                             outer_action);
7048     if (retval) {
7049         return xstrdup("Incorrect instruction ordering");
7050     }
7051
7052     return NULL;
7053 }
7054
7055 static char * OVS_WARN_UNUSED_RESULT
7056 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
7057               enum ofputil_protocol *usable_protocols, bool allow_instructions,
7058               enum ofpact_type outer_action)
7059 {
7060     uint32_t orig_size = ofpacts->size;
7061     char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
7062                                   allow_instructions, outer_action);
7063     if (error) {
7064         ofpacts->size = orig_size;
7065     }
7066     return error;
7067 }
7068
7069 static char * OVS_WARN_UNUSED_RESULT
7070 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
7071                    enum ofputil_protocol *usable_protocols,
7072                    bool allow_instructions, enum ofpact_type outer_action)
7073 {
7074     char *error, *s;
7075
7076     *usable_protocols = OFPUTIL_P_ANY;
7077
7078     s = xstrdup(s_);
7079     error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
7080                           outer_action);
7081     free(s);
7082
7083     return error;
7084 }
7085
7086 /* Parses 's' as a set of OpenFlow actions and appends the actions to
7087  * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7088  * that are nested within the action of type 'outer_action'.
7089  *
7090  * Returns NULL if successful, otherwise a malloc()'d string describing the
7091  * error.  The caller is responsible for freeing the returned string. */
7092 char * OVS_WARN_UNUSED_RESULT
7093 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
7094                       enum ofputil_protocol *usable_protocols)
7095 {
7096     return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
7097 }
7098
7099 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7100  * 'ofpacts'.
7101  *
7102  * Returns NULL if successful, otherwise a malloc()'d string describing the
7103  * error.  The caller is responsible for freeing the returned string. */
7104 char * OVS_WARN_UNUSED_RESULT
7105 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
7106                            enum ofputil_protocol *usable_protocols)
7107 {
7108     return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
7109 }
7110
7111 const char *
7112 ofpact_name(enum ofpact_type type)
7113 {
7114     switch (type) {
7115 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7116         OFPACTS
7117 #undef OFPACT
7118     }
7119     return "<unknown>";
7120 }
7121 \f
7122 /* Low-level action decoding and encoding functions. */
7123
7124 /* Everything needed to identify a particular OpenFlow action. */
7125 struct ofpact_hdrs {
7126     uint32_t vendor;              /* 0 if standard, otherwise a vendor code. */
7127     uint16_t type;                /* Type if standard, otherwise subtype. */
7128     uint8_t ofp_version;          /* From ofp_header. */
7129 };
7130
7131 /* Information about a particular OpenFlow action. */
7132 struct ofpact_raw_instance {
7133     /* The action's identity. */
7134     struct ofpact_hdrs hdrs;
7135     enum ofp_raw_action_type raw;
7136
7137     /* Looking up the action. */
7138     struct hmap_node decode_node; /* Based on 'hdrs'. */
7139     struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
7140
7141     /* The action's encoded size.
7142      *
7143      * If this action is fixed-length, 'min_length' == 'max_length'.
7144      * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7145      * OFP_ACTION_ALIGN) == 65528. */
7146     unsigned short int min_length;
7147     unsigned short int max_length;
7148
7149     /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7150      * offset of that argument from the beginning of the action and 'arg_len'
7151      * its length, both in bytes.
7152      *
7153      * For actions that take other forms, these are both zero. */
7154     unsigned short int arg_ofs;
7155     unsigned short int arg_len;
7156
7157     /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7158     const char *name;
7159
7160     /* If this action is deprecated, a human-readable string with a brief
7161      * explanation. */
7162     const char *deprecation;
7163 };
7164
7165 /* Action header. */
7166 struct ofp_action_header {
7167     /* The meaning of other values of 'type' generally depends on the OpenFlow
7168      * version (see enum ofp_raw_action_type).
7169      *
7170      * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7171      * designates an OpenFlow vendor ID and that the remainder of the action
7172      * structure has a vendor-defined meaning.
7173      */
7174 #define OFPAT_VENDOR 0xffff
7175     ovs_be16 type;
7176
7177     /* Always a multiple of 8. */
7178     ovs_be16 len;
7179
7180     /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7181      * ONF_VENDOR_ID.  Other 'type's use this space for some other purpose. */
7182     ovs_be32 vendor;
7183 };
7184 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
7185
7186 /* Header for Nicira-defined actions and for ONF vendor extensions.
7187  *
7188  * This cannot be used as an entirely generic vendor extension action header,
7189  * because OpenFlow does not specify the location or size of the action
7190  * subtype; it just happens that ONF extensions and Nicira extensions share
7191  * this format. */
7192 struct ext_action_header {
7193     ovs_be16 type;                  /* OFPAT_VENDOR. */
7194     ovs_be16 len;                   /* At least 16. */
7195     ovs_be32 vendor;                /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7196     ovs_be16 subtype;               /* See enum ofp_raw_action_type. */
7197     uint8_t pad[6];
7198 };
7199 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
7200
7201 static bool
7202 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
7203                   const struct ofpact_hdrs *b)
7204 {
7205     return (a->vendor == b->vendor
7206             && a->type == b->type
7207             && a->ofp_version == b->ofp_version);
7208 }
7209
7210 static uint32_t
7211 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
7212 {
7213     return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
7214 }
7215
7216 #include "ofp-actions.inc2"
7217
7218 static struct hmap *
7219 ofpact_decode_hmap(void)
7220 {
7221     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7222     static struct hmap hmap;
7223
7224     if (ovsthread_once_start(&once)) {
7225         struct ofpact_raw_instance *inst;
7226
7227         hmap_init(&hmap);
7228         for (inst = all_raw_instances;
7229              inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7230              inst++) {
7231             hmap_insert(&hmap, &inst->decode_node,
7232                         ofpact_hdrs_hash(&inst->hdrs));
7233         }
7234         ovsthread_once_done(&once);
7235     }
7236     return &hmap;
7237 }
7238
7239 static struct hmap *
7240 ofpact_encode_hmap(void)
7241 {
7242     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7243     static struct hmap hmap;
7244
7245     if (ovsthread_once_start(&once)) {
7246         struct ofpact_raw_instance *inst;
7247
7248         hmap_init(&hmap);
7249         for (inst = all_raw_instances;
7250              inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7251              inst++) {
7252             hmap_insert(&hmap, &inst->encode_node,
7253                         hash_2words(inst->raw, inst->hdrs.ofp_version));
7254         }
7255         ovsthread_once_done(&once);
7256     }
7257     return &hmap;
7258 }
7259
7260 static enum ofperr
7261 ofpact_decode_raw(enum ofp_version ofp_version,
7262                   const struct ofp_action_header *oah, size_t length,
7263                   const struct ofpact_raw_instance **instp)
7264 {
7265     const struct ofpact_raw_instance *inst;
7266     struct ofpact_hdrs hdrs;
7267
7268     *instp = NULL;
7269     if (length < sizeof *oah) {
7270         return OFPERR_OFPBAC_BAD_LEN;
7271     }
7272
7273     /* Get base action type. */
7274     if (oah->type == htons(OFPAT_VENDOR)) {
7275         /* Get vendor. */
7276         hdrs.vendor = ntohl(oah->vendor);
7277         if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7278             /* Get extension subtype. */
7279             const struct ext_action_header *nah;
7280
7281             nah = ALIGNED_CAST(const struct ext_action_header *, oah);
7282             if (length < sizeof *nah) {
7283                 return OFPERR_OFPBAC_BAD_LEN;
7284             }
7285             hdrs.type = ntohs(nah->subtype);
7286         } else {
7287             VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7288                          hdrs.vendor);
7289             return OFPERR_OFPBAC_BAD_VENDOR;
7290         }
7291     } else {
7292         hdrs.vendor = 0;
7293         hdrs.type = ntohs(oah->type);
7294     }
7295
7296     hdrs.ofp_version = ofp_version;
7297     HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7298                              ofpact_decode_hmap()) {
7299         if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7300             *instp = inst;
7301             return 0;
7302         }
7303     }
7304
7305     return (hdrs.vendor
7306             ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7307             : OFPERR_OFPBAC_BAD_TYPE);
7308 }
7309
7310 static enum ofperr
7311 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7312                 enum ofp_raw_action_type *raw, uint64_t *arg)
7313 {
7314     const struct ofp_action_header *oah = buf->data;
7315     const struct ofpact_raw_instance *action;
7316     unsigned int length;
7317     enum ofperr error;
7318
7319     *raw = *arg = 0;
7320     error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
7321     if (error) {
7322         return error;
7323     }
7324
7325     if (action->deprecation) {
7326         VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7327                      action->name, ofputil_version_to_string(ofp_version),
7328                      action->deprecation);
7329     }
7330
7331     length = ntohs(oah->len);
7332     if (length > buf->size) {
7333         VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
7334                      "length %"PRIu32, action->name, length, buf->size);
7335         return OFPERR_OFPBAC_BAD_LEN;
7336     }
7337     if (length < action->min_length || length > action->max_length) {
7338         VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7339                      "[%hu,%hu]", action->name, length,
7340                      action->min_length, action->max_length);
7341         return OFPERR_OFPBAC_BAD_LEN;
7342     }
7343     if (length % 8) {
7344         VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7345                      "of 8", action->name, length);
7346         return OFPERR_OFPBAC_BAD_LEN;
7347     }
7348
7349     *raw = action->raw;
7350     *arg = 0;
7351     if (action->arg_len) {
7352         const uint8_t *p;
7353         int i;
7354
7355         p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7356         for (i = 0; i < action->arg_len; i++) {
7357             *arg = (*arg << 8) | p[i];
7358         }
7359     }
7360
7361     ofpbuf_pull(buf, length);
7362
7363     return 0;
7364 }
7365
7366 static const struct ofpact_raw_instance *
7367 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7368 {
7369     const struct ofpact_raw_instance *inst;
7370
7371     HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7372                              ofpact_encode_hmap()) {
7373         if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7374             return inst;
7375         }
7376     }
7377     OVS_NOT_REACHED();
7378 }
7379
7380 static void *
7381 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7382                enum ofp_raw_action_type raw, uint64_t arg)
7383 {
7384     const struct ofpact_raw_instance *inst;
7385     struct ofp_action_header *oah;
7386     const struct ofpact_hdrs *hdrs;
7387
7388     inst = ofpact_raw_lookup(ofp_version, raw);
7389     hdrs = &inst->hdrs;
7390
7391     oah = ofpbuf_put_zeros(buf, inst->min_length);
7392     oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7393     oah->len = htons(inst->min_length);
7394     oah->vendor = htonl(hdrs->vendor);
7395
7396     switch (hdrs->vendor) {
7397     case 0:
7398         break;
7399
7400     case NX_VENDOR_ID:
7401     case ONF_VENDOR_ID: {
7402         struct ext_action_header *nah = (struct ext_action_header *) oah;
7403         nah->subtype = htons(hdrs->type);
7404         break;
7405     }
7406
7407     default:
7408         OVS_NOT_REACHED();
7409     }
7410
7411     if (inst->arg_len) {
7412         uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7413         int i;
7414
7415         for (i = 0; i < inst->arg_len; i++) {
7416             *--p = arg;
7417             arg >>= 8;
7418         }
7419     } else {
7420         ovs_assert(!arg);
7421     }
7422
7423     return oah;
7424 }
7425
7426 static void
7427 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7428 {
7429     struct ofp_action_header *oah;
7430
7431     ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
7432
7433     oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
7434     oah->len = htons(openflow->size - start_ofs);
7435 }
7436