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