lib: support OF11 dec_nw_ttl
[cascardo/ovs.git] / lib / ofp-actions.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofp-actions.h"
19 #include "autopath.h"
20 #include "bundle.h"
21 #include "byte-order.h"
22 #include "compiler.h"
23 #include "dynamic-string.h"
24 #include "learn.h"
25 #include "meta-flow.h"
26 #include "multipath.h"
27 #include "nx-match.h"
28 #include "ofp-util.h"
29 #include "ofpbuf.h"
30 #include "vlog.h"
31
32 VLOG_DEFINE_THIS_MODULE(ofp_actions);
33
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35 \f
36 /* Converting OpenFlow 1.0 to ofpacts. */
37
38 static enum ofperr
39 output_from_openflow10(const struct ofp10_action_output *oao,
40                        struct ofpbuf *out)
41 {
42     struct ofpact_output *output;
43
44     output = ofpact_put_OUTPUT(out);
45     output->port = ntohs(oao->port);
46     output->max_len = ntohs(oao->max_len);
47
48     return ofputil_check_output_port(output->port, OFPP_MAX);
49 }
50
51 static enum ofperr
52 enqueue_from_openflow10(const struct ofp_action_enqueue *oae,
53                         struct ofpbuf *out)
54 {
55     struct ofpact_enqueue *enqueue;
56
57     enqueue = ofpact_put_ENQUEUE(out);
58     enqueue->port = ntohs(oae->port);
59     enqueue->queue = ntohl(oae->queue_id);
60     if (enqueue->port >= OFPP_MAX && enqueue->port != OFPP_IN_PORT
61         && enqueue->port != OFPP_LOCAL) {
62         return OFPERR_OFPBAC_BAD_OUT_PORT;
63     }
64     return 0;
65 }
66
67 static void
68 resubmit_from_openflow(const struct nx_action_resubmit *nar,
69                        struct ofpbuf *out)
70 {
71     struct ofpact_resubmit *resubmit;
72
73     resubmit = ofpact_put_RESUBMIT(out);
74     resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
75     resubmit->in_port = ntohs(nar->in_port);
76     resubmit->table_id = 0xff;
77 }
78
79 static enum ofperr
80 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
81                              struct ofpbuf *out)
82 {
83     struct ofpact_resubmit *resubmit;
84
85     if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
86         return OFPERR_OFPBAC_BAD_ARGUMENT;
87     }
88
89     resubmit = ofpact_put_RESUBMIT(out);
90     resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
91     resubmit->in_port = ntohs(nar->in_port);
92     resubmit->table_id = nar->table;
93     return 0;
94 }
95
96 static enum ofperr
97 output_reg_from_openflow(const struct nx_action_output_reg *naor,
98                          struct ofpbuf *out)
99 {
100     struct ofpact_output_reg *output_reg;
101
102     if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
103         return OFPERR_OFPBAC_BAD_ARGUMENT;
104     }
105
106     output_reg = ofpact_put_OUTPUT_REG(out);
107     output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
108     output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
109     output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
110     output_reg->max_len = ntohs(naor->max_len);
111
112     return mf_check_src(&output_reg->src, NULL);
113 }
114
115 static void
116 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
117                           struct ofpbuf *out)
118 {
119     struct ofpact_fin_timeout *oft;
120
121     oft = ofpact_put_FIN_TIMEOUT(out);
122     oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
123     oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
124 }
125
126 static void
127 controller_from_openflow(const struct nx_action_controller *nac,
128                          struct ofpbuf *out)
129 {
130     struct ofpact_controller *oc;
131
132     oc = ofpact_put_CONTROLLER(out);
133     oc->max_len = ntohs(nac->max_len);
134     oc->controller_id = ntohs(nac->controller_id);
135     oc->reason = nac->reason;
136 }
137
138 static void
139 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
140 {
141     struct ofpact_note *note;
142     unsigned int length;
143
144     length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
145     note = ofpact_put(out, OFPACT_NOTE,
146                       offsetof(struct ofpact_note, data) + length);
147     note->length = length;
148     memcpy(note->data, nan->note, length);
149 }
150
151 static enum ofperr
152 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
153 {
154     uint16_t id = 0;
155     struct ofpact_cnt_ids *ids;
156     enum ofperr error = 0;
157
158     ids = ofpact_put_DEC_TTL(out);
159     ids->ofpact.compat = compat;
160     ids->n_controllers = 1;
161     ofpbuf_put(out, &id, sizeof id);
162     ids = out->l2;
163     ofpact_update_len(out, &ids->ofpact);
164     return error;
165 }
166
167 static enum ofperr
168 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
169                       struct ofpbuf *out)
170 {
171     struct ofpact_cnt_ids *ids;
172     size_t ids_size;
173     int i;
174
175     ids = ofpact_put_DEC_TTL(out);
176     ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
177     ids->n_controllers = ntohs(nac_ids->n_controllers);
178     ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
179
180     if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
181         return OFPERR_NXBRC_MUST_BE_ZERO;
182     }
183
184     if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
185         VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
186                      "allocated for controller ids.  %zu bytes are required for "
187                      "%"PRIu16" controllers.", ids_size,
188                      ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
189         return OFPERR_OFPBAC_BAD_LEN;
190     }
191
192     for (i = 0; i < ids->n_controllers; i++) {
193         uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
194         ofpbuf_put(out, &id, sizeof id);
195     }
196
197     ids = out->l2;
198     ofpact_update_len(out, &ids->ofpact);
199
200     return 0;
201 }
202
203 static enum ofperr
204 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
205 {
206     const struct nx_action_header *nah = (const struct nx_action_header *) a;
207     uint16_t len = ntohs(a->header.len);
208
209     if (len < sizeof(struct nx_action_header)) {
210         return OFPERR_OFPBAC_BAD_LEN;
211     } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
212         return OFPERR_OFPBAC_BAD_VENDOR;
213     }
214
215     switch (nah->subtype) {
216 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
217         case CONSTANT_HTONS(ENUM):                      \
218             if (EXTENSIBLE                              \
219                 ? len >= sizeof(struct STRUCT)          \
220                 : len == sizeof(struct STRUCT)) {       \
221                 *code = OFPUTIL_##ENUM;                 \
222                 return 0;                               \
223             } else {                                    \
224                 return OFPERR_OFPBAC_BAD_LEN;           \
225             }                                           \
226             NOT_REACHED();
227 #include "ofp-util.def"
228
229     case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
230     case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
231     default:
232         return OFPERR_OFPBAC_BAD_TYPE;
233     }
234 }
235
236 /* Parses 'a' to determine its type.  On success stores the correct type into
237  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
238  * '*code' is indeterminate.
239  *
240  * The caller must have already verified that 'a''s length is potentially
241  * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
242  * ofp_action) and no longer than the amount of space allocated to 'a').
243  *
244  * This function verifies that 'a''s length is correct for the type of action
245  * that it represents. */
246 static enum ofperr
247 decode_openflow10_action(const union ofp_action *a,
248                          enum ofputil_action_code *code)
249 {
250     switch (a->type) {
251     case CONSTANT_HTONS(OFPAT10_VENDOR):
252         return decode_nxast_action(a, code);
253
254 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)                          \
255         case CONSTANT_HTONS(ENUM):                                  \
256             if (a->header.len == htons(sizeof(struct STRUCT))) {    \
257                 *code = OFPUTIL_##ENUM;                             \
258                 return 0;                                           \
259             } else {                                                \
260                 return OFPERR_OFPBAC_BAD_LEN;                       \
261             }                                                       \
262             break;
263 #include "ofp-util.def"
264
265     default:
266         return OFPERR_OFPBAC_BAD_TYPE;
267     }
268 }
269
270 static enum ofperr
271 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
272                   struct ofpbuf *out)
273 {
274     const struct nx_action_resubmit *nar;
275     const struct nx_action_set_tunnel *nast;
276     const struct nx_action_set_queue *nasq;
277     const struct nx_action_note *nan;
278     const struct nx_action_set_tunnel64 *nast64;
279     struct ofpact_tunnel *tunnel;
280     enum ofperr error = 0;
281
282     switch (code) {
283     case OFPUTIL_ACTION_INVALID:
284 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
285 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
286 #include "ofp-util.def"
287         NOT_REACHED();
288
289     case OFPUTIL_NXAST_RESUBMIT:
290         resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
291         break;
292
293     case OFPUTIL_NXAST_SET_TUNNEL:
294         nast = (const struct nx_action_set_tunnel *) a;
295         tunnel = ofpact_put_SET_TUNNEL(out);
296         tunnel->ofpact.compat = code;
297         tunnel->tun_id = ntohl(nast->tun_id);
298         break;
299
300     case OFPUTIL_NXAST_SET_QUEUE:
301         nasq = (const struct nx_action_set_queue *) a;
302         ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
303         break;
304
305     case OFPUTIL_NXAST_POP_QUEUE:
306         ofpact_put_POP_QUEUE(out);
307         break;
308
309     case OFPUTIL_NXAST_REG_MOVE:
310         error = nxm_reg_move_from_openflow(
311             (const struct nx_action_reg_move *) a, out);
312         break;
313
314     case OFPUTIL_NXAST_REG_LOAD:
315         error = nxm_reg_load_from_openflow(
316             (const struct nx_action_reg_load *) a, out);
317         break;
318
319     case OFPUTIL_NXAST_NOTE:
320         nan = (const struct nx_action_note *) a;
321         note_from_openflow(nan, out);
322         break;
323
324     case OFPUTIL_NXAST_SET_TUNNEL64:
325         nast64 = (const struct nx_action_set_tunnel64 *) a;
326         tunnel = ofpact_put_SET_TUNNEL(out);
327         tunnel->ofpact.compat = code;
328         tunnel->tun_id = ntohll(nast64->tun_id);
329         break;
330
331     case OFPUTIL_NXAST_MULTIPATH:
332         error = multipath_from_openflow((const struct nx_action_multipath *) a,
333                                         ofpact_put_MULTIPATH(out));
334         break;
335
336     case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
337         error = autopath_from_openflow((const struct nx_action_autopath *) a,
338                                        ofpact_put_AUTOPATH(out));
339         break;
340
341     case OFPUTIL_NXAST_BUNDLE:
342     case OFPUTIL_NXAST_BUNDLE_LOAD:
343         error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
344         break;
345
346     case OFPUTIL_NXAST_OUTPUT_REG:
347         error = output_reg_from_openflow(
348             (const struct nx_action_output_reg *) a, out);
349         break;
350
351     case OFPUTIL_NXAST_RESUBMIT_TABLE:
352         nar = (const struct nx_action_resubmit *) a;
353         error = resubmit_table_from_openflow(nar, out);
354         break;
355
356     case OFPUTIL_NXAST_LEARN:
357         error = learn_from_openflow((const struct nx_action_learn *) a, out);
358         break;
359
360     case OFPUTIL_NXAST_EXIT:
361         ofpact_put_EXIT(out);
362         break;
363
364     case OFPUTIL_NXAST_DEC_TTL:
365         error = dec_ttl_from_openflow(out, code);
366         break;
367
368     case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
369         error = dec_ttl_cnt_ids_from_openflow(
370                     (const struct nx_action_cnt_ids *) a, out);
371         break;
372
373     case OFPUTIL_NXAST_FIN_TIMEOUT:
374         fin_timeout_from_openflow(
375             (const struct nx_action_fin_timeout *) a, out);
376         break;
377
378     case OFPUTIL_NXAST_CONTROLLER:
379         controller_from_openflow((const struct nx_action_controller *) a, out);
380         break;
381     }
382
383     return error;
384 }
385
386 static enum ofperr
387 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
388 {
389     enum ofputil_action_code code;
390     enum ofperr error;
391
392     error = decode_openflow10_action(a, &code);
393     if (error) {
394         return error;
395     }
396
397     switch (code) {
398     case OFPUTIL_ACTION_INVALID:
399 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
400 #include "ofp-util.def"
401         NOT_REACHED();
402
403     case OFPUTIL_OFPAT10_OUTPUT:
404         return output_from_openflow10(&a->output10, out);
405
406     case OFPUTIL_OFPAT10_SET_VLAN_VID:
407         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
408             return OFPERR_OFPBAC_BAD_ARGUMENT;
409         }
410         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
411         break;
412
413     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
414         if (a->vlan_pcp.vlan_pcp & ~7) {
415             return OFPERR_OFPBAC_BAD_ARGUMENT;
416         }
417         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
418         break;
419
420     case OFPUTIL_OFPAT10_STRIP_VLAN:
421         ofpact_put_STRIP_VLAN(out);
422         break;
423
424     case OFPUTIL_OFPAT10_SET_DL_SRC:
425         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
426                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
427         break;
428
429     case OFPUTIL_OFPAT10_SET_DL_DST:
430         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
431                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
432         break;
433
434     case OFPUTIL_OFPAT10_SET_NW_SRC:
435         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
436         break;
437
438     case OFPUTIL_OFPAT10_SET_NW_DST:
439         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
440         break;
441
442     case OFPUTIL_OFPAT10_SET_NW_TOS:
443         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
444             return OFPERR_OFPBAC_BAD_ARGUMENT;
445         }
446         ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
447         break;
448
449     case OFPUTIL_OFPAT10_SET_TP_SRC:
450         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
451         break;
452
453     case OFPUTIL_OFPAT10_SET_TP_DST:
454         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
455
456         break;
457
458     case OFPUTIL_OFPAT10_ENQUEUE:
459         error = enqueue_from_openflow10((const struct ofp_action_enqueue *) a,
460                                         out);
461         break;
462
463 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
464 #include "ofp-util.def"
465         return ofpact_from_nxast(a, code, out);
466     }
467
468     return error;
469 }
470
471 static inline union ofp_action *
472 action_next(const union ofp_action *a)
473 {
474     return ((union ofp_action *) (void *)
475             ((uint8_t *) a + ntohs(a->header.len)));
476 }
477
478 static inline bool
479 action_is_valid(const union ofp_action *a, size_t n_actions)
480 {
481     uint16_t len = ntohs(a->header.len);
482     return (!(len % OFP_ACTION_ALIGN)
483             && len >= sizeof *a
484             && len / sizeof *a <= n_actions);
485 }
486
487 /* This macro is careful to check for actions with bad lengths. */
488 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS)                 \
489     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
490          (LEFT) > 0 && action_is_valid(ITER, LEFT);                     \
491          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
492           (ITER) = action_next(ITER)))
493
494 static void
495 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
496                enum ofperr error)
497 {
498     if (!VLOG_DROP_WARN(&rl)) {
499         struct ds s;
500
501         ds_init(&s);
502         ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
503         VLOG_WARN("bad action at offset %#zx (%s):\n%s",
504                   ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
505         ds_destroy(&s);
506     }
507 }
508
509 static enum ofperr
510 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
511                       struct ofpbuf *out,
512                       enum ofperr (*ofpact_from_openflow)(
513                           const union ofp_action *a, struct ofpbuf *out))
514 {
515     const union ofp_action *a;
516     size_t left;
517
518     ACTION_FOR_EACH (a, left, in, n_in) {
519         enum ofperr error = ofpact_from_openflow(a, out);
520         if (error) {
521             log_bad_action(in, n_in, a - in, error);
522             return error;
523         }
524     }
525     if (left) {
526         enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
527         log_bad_action(in, n_in, n_in - left, error);
528         return error;
529     }
530
531     ofpact_pad(out);
532     return 0;
533 }
534
535 static enum ofperr
536 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
537                         struct ofpbuf *out)
538 {
539     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
540 }
541
542 static enum ofperr
543 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
544                      struct ofpbuf *ofpacts,
545                      enum ofperr (*translate)(const union ofp_action *actions,
546                                               size_t n_actions,
547                                               struct ofpbuf *ofpacts))
548 {
549     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
550     const union ofp_action *actions;
551     enum ofperr error;
552
553     ofpbuf_clear(ofpacts);
554
555     if (actions_len % OFP_ACTION_ALIGN != 0) {
556         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
557                      "multiple of %d", actions_len, OFP_ACTION_ALIGN);
558         return OFPERR_OFPBRC_BAD_LEN;
559     }
560
561     actions = ofpbuf_try_pull(openflow, actions_len);
562     if (actions == NULL) {
563         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
564                      "remaining message length (%zu)",
565                      actions_len, openflow->size);
566         return OFPERR_OFPBRC_BAD_LEN;
567     }
568
569     error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
570     if (error) {
571         ofpbuf_clear(ofpacts);
572     }
573     return error;
574 }
575
576 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
577  * front of 'openflow' into ofpacts.  On success, replaces any existing content
578  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
579  * Returns 0 if successful, otherwise an OpenFlow error.
580  *
581  * The parsed actions are valid generically, but they may not be valid in a
582  * specific context.  For example, port numbers up to OFPP_MAX are valid
583  * generically, but specific datapaths may only support port numbers in a
584  * smaller range.  Use ofpacts_check() to additional check whether actions are
585  * valid in a specific context. */
586 enum ofperr
587 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
588                         struct ofpbuf *ofpacts)
589 {
590     return ofpacts_pull_actions(openflow, actions_len, ofpacts,
591                                 ofpacts_from_openflow10);
592 }
593 \f
594 /* OpenFlow 1.1 actions. */
595
596 /* Parses 'a' to determine its type.  On success stores the correct type into
597  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
598  * '*code' is indeterminate.
599  *
600  * The caller must have already verified that 'a''s length is potentially
601  * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
602  * ofp_action) and no longer than the amount of space allocated to 'a').
603  *
604  * This function verifies that 'a''s length is correct for the type of action
605  * that it represents. */
606 static enum ofperr
607 decode_openflow11_action(const union ofp_action *a,
608                          enum ofputil_action_code *code)
609 {
610     uint16_t len;
611
612     switch (a->type) {
613     case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
614         return decode_nxast_action(a, code);
615
616 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
617         case CONSTANT_HTONS(ENUM):                      \
618             len = ntohs(a->header.len);                 \
619             if (EXTENSIBLE                              \
620                 ? len >= sizeof(struct STRUCT)          \
621                 : len == sizeof(struct STRUCT)) {       \
622                 *code = OFPUTIL_##ENUM;                 \
623                 return 0;                               \
624             } else {                                    \
625                 return OFPERR_OFPBAC_BAD_LEN;           \
626             }                                           \
627             NOT_REACHED();
628 #include "ofp-util.def"
629
630     default:
631         return OFPERR_OFPBAC_BAD_TYPE;
632     }
633 }
634
635 static enum ofperr
636 output_from_openflow11(const struct ofp11_action_output *oao,
637                        struct ofpbuf *out)
638 {
639     struct ofpact_output *output;
640     enum ofperr error;
641
642     output = ofpact_put_OUTPUT(out);
643     output->max_len = ntohs(oao->max_len);
644
645     error = ofputil_port_from_ofp11(oao->port, &output->port);
646     if (error) {
647         return error;
648     }
649
650     return ofputil_check_output_port(output->port, OFPP_MAX);
651 }
652
653 static enum ofperr
654 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
655 {
656     enum ofputil_action_code code;
657     enum ofperr error;
658
659     error = decode_openflow11_action(a, &code);
660     if (error) {
661         return error;
662     }
663
664     switch (code) {
665     case OFPUTIL_ACTION_INVALID:
666 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
667 #include "ofp-util.def"
668         NOT_REACHED();
669
670     case OFPUTIL_OFPAT11_OUTPUT:
671         return output_from_openflow11((const struct ofp11_action_output *) a,
672                                       out);
673
674     case OFPUTIL_OFPAT11_SET_VLAN_VID:
675         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
676             return OFPERR_OFPBAC_BAD_ARGUMENT;
677         }
678         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
679         break;
680
681     case OFPUTIL_OFPAT11_SET_VLAN_PCP:
682         if (a->vlan_pcp.vlan_pcp & ~7) {
683             return OFPERR_OFPBAC_BAD_ARGUMENT;
684         }
685         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
686         break;
687
688     case OFPUTIL_OFPAT11_SET_DL_SRC:
689         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
690                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
691         break;
692
693     case OFPUTIL_OFPAT11_SET_DL_DST:
694         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
695                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
696         break;
697
698     case OFPUTIL_OFPAT11_DEC_NW_TTL:
699         dec_ttl_from_openflow(out, code);
700         break;
701
702     case OFPUTIL_OFPAT11_SET_NW_SRC:
703         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
704         break;
705
706     case OFPUTIL_OFPAT11_SET_NW_DST:
707         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
708         break;
709
710     case OFPUTIL_OFPAT11_SET_NW_TOS:
711         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
712             return OFPERR_OFPBAC_BAD_ARGUMENT;
713         }
714         ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
715         break;
716
717     case OFPUTIL_OFPAT11_SET_TP_SRC:
718         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
719         break;
720
721     case OFPUTIL_OFPAT11_SET_TP_DST:
722         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
723         break;
724
725     case OFPUTIL_OFPAT12_SET_FIELD:
726         return nxm_reg_load_from_openflow12_set_field(
727             (const struct ofp12_action_set_field *)a, out);
728
729 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
730 #include "ofp-util.def"
731         return ofpact_from_nxast(a, code, out);
732     }
733
734     return error;
735 }
736
737 static enum ofperr
738 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
739                         struct ofpbuf *out)
740 {
741     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
742 }
743 \f
744 /* OpenFlow 1.1 instructions. */
745
746 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
747     static inline const struct STRUCT *                         \
748     instruction_get_##ENUM(const struct ofp11_instruction *inst)\
749     {                                                           \
750         assert(inst->type == htons(ENUM));                      \
751         return (struct STRUCT *)inst;                           \
752     }                                                           \
753                                                                 \
754     static inline void                                          \
755     instruction_init_##ENUM(struct STRUCT *s)                   \
756     {                                                           \
757         memset(s, 0, sizeof *s);                                \
758         s->type = htons(ENUM);                                  \
759         s->len = htons(sizeof *s);                              \
760     }                                                           \
761                                                                 \
762     static inline struct STRUCT *                               \
763     instruction_put_##ENUM(struct ofpbuf *buf)                  \
764     {                                                           \
765         struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
766         instruction_init_##ENUM(s);                             \
767         return s;                                               \
768     }
769 OVS_INSTRUCTIONS
770 #undef DEFINE_INST
771
772 struct instruction_type_info {
773     enum ovs_instruction_type type;
774     const char *name;
775 };
776
777 static const struct instruction_type_info inst_info[] = {
778 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
779 OVS_INSTRUCTIONS
780 #undef DEFINE_INST
781 };
782
783 const char *
784 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
785 {
786     return inst_info[type].name;
787 }
788
789 int
790 ofpact_instruction_type_from_name(const char *name)
791 {
792     const struct instruction_type_info *p;
793     for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
794         if (!strcasecmp(name, p->name)) {
795             return p->type;
796         }
797     }
798     return -1;
799 }
800
801 static inline struct ofp11_instruction *
802 instruction_next(const struct ofp11_instruction *inst)
803 {
804     return ((struct ofp11_instruction *) (void *)
805             ((uint8_t *) inst + ntohs(inst->len)));
806 }
807
808 static inline bool
809 instruction_is_valid(const struct ofp11_instruction *inst,
810                      size_t n_instructions)
811 {
812     uint16_t len = ntohs(inst->len);
813     return (!(len % OFP11_INSTRUCTION_ALIGN)
814             && len >= sizeof *inst
815             && len / sizeof *inst <= n_instructions);
816 }
817
818 /* This macro is careful to check for instructions with bad lengths. */
819 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS)  \
820     for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS);            \
821          (LEFT) > 0 && instruction_is_valid(ITER, LEFT);                \
822          ((LEFT) -= (ntohs((ITER)->len)                                 \
823                      / sizeof(struct ofp11_instruction)),               \
824           (ITER) = instruction_next(ITER)))
825
826 static enum ofperr
827 decode_openflow11_instruction(const struct ofp11_instruction *inst,
828                               enum ovs_instruction_type *type)
829 {
830     uint16_t len = ntohs(inst->len);
831
832     switch (inst->type) {
833     case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
834         return OFPERR_OFPBIC_BAD_EXPERIMENTER;
835
836 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)     \
837         case CONSTANT_HTONS(ENUM):                      \
838             if (EXTENSIBLE                              \
839                 ? len >= sizeof(struct STRUCT)          \
840                 : len == sizeof(struct STRUCT)) {       \
841                 *type = OVSINST_##ENUM;                 \
842                 return 0;                               \
843             } else {                                    \
844                 return OFPERR_OFPBIC_BAD_LEN;           \
845             }
846 OVS_INSTRUCTIONS
847 #undef DEFINE_INST
848
849     default:
850         return OFPERR_OFPBIC_UNKNOWN_INST;
851     }
852 }
853
854 static enum ofperr
855 decode_openflow11_instructions(const struct ofp11_instruction insts[],
856                                size_t n_insts,
857                                const struct ofp11_instruction *out[])
858 {
859     const struct ofp11_instruction *inst;
860     size_t left;
861
862     memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
863     INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
864         enum ovs_instruction_type type;
865         enum ofperr error;
866
867         error = decode_openflow11_instruction(inst, &type);
868         if (error) {
869             return error;
870         }
871
872         if (out[type]) {
873             return OFPERR_OFPIT_BAD_INSTRUCTION;
874         }
875         out[type] = inst;
876     }
877
878     if (left) {
879         VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
880                      (n_insts - left) * sizeof *inst);
881         return OFPERR_OFPBIC_BAD_LEN;
882     }
883     return 0;
884 }
885
886 static void
887 get_actions_from_instruction(const struct ofp11_instruction *inst,
888                          const union ofp_action **actions,
889                          size_t *n_actions)
890 {
891     *actions = (const union ofp_action *) (inst + 1);
892     *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
893 }
894
895 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
896  * front of 'openflow' into ofpacts.  On success, replaces any existing content
897  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
898  * Returns 0 if successful, otherwise an OpenFlow error.
899  *
900  * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
901  * instructions, so you should call ofpacts_pull_openflow11_instructions()
902  * instead of this function.
903  *
904  * The parsed actions are valid generically, but they may not be valid in a
905  * specific context.  For example, port numbers up to OFPP_MAX are valid
906  * generically, but specific datapaths may only support port numbers in a
907  * smaller range.  Use ofpacts_check() to additional check whether actions are
908  * valid in a specific context. */
909 enum ofperr
910 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
911                                 unsigned int actions_len,
912                                 struct ofpbuf *ofpacts)
913 {
914     return ofpacts_pull_actions(openflow, actions_len, ofpacts,
915                                 ofpacts_from_openflow11);
916 }
917
918 enum ofperr
919 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
920                                      unsigned int instructions_len,
921                                      struct ofpbuf *ofpacts)
922 {
923     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
924     const struct ofp11_instruction *instructions;
925     const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
926     enum ofperr error;
927
928     ofpbuf_clear(ofpacts);
929
930     if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
931         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
932                      "multiple of %d",
933                      instructions_len, OFP11_INSTRUCTION_ALIGN);
934         error = OFPERR_OFPBIC_BAD_LEN;
935         goto exit;
936     }
937
938     instructions = ofpbuf_try_pull(openflow, instructions_len);
939     if (instructions == NULL) {
940         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
941                      "remaining message length (%zu)",
942                      instructions_len, openflow->size);
943         error = OFPERR_OFPBIC_BAD_LEN;
944         goto exit;
945     }
946
947     error = decode_openflow11_instructions(
948         instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
949         insts);
950     if (error) {
951         goto exit;
952     }
953
954     if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
955         const union ofp_action *actions;
956         size_t n_actions;
957
958         get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
959                                      &actions, &n_actions);
960         error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
961         if (error) {
962             goto exit;
963         }
964     }
965     if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
966         instruction_get_OFPIT11_CLEAR_ACTIONS(
967             insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
968         ofpact_put_CLEAR_ACTIONS(ofpacts);
969     }
970     /* TODO:XXX Write-Actions */
971     /* TODO:XXX Write-Metadata */
972     if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
973         const struct ofp11_instruction_goto_table *oigt;
974         struct ofpact_goto_table *ogt;
975
976         oigt = instruction_get_OFPIT11_GOTO_TABLE(
977             insts[OVSINST_OFPIT11_GOTO_TABLE]);
978         ogt = ofpact_put_GOTO_TABLE(ofpacts);
979         ogt->table_id = oigt->table_id;
980     }
981
982     if (insts[OVSINST_OFPIT11_WRITE_METADATA] ||
983         insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
984         error = OFPERR_OFPBIC_UNSUP_INST;
985         goto exit;
986     }
987
988 exit:
989     if (error) {
990         ofpbuf_clear(ofpacts);
991     }
992     return error;
993 }
994 \f
995 static enum ofperr
996 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
997 {
998     const struct ofpact_enqueue *enqueue;
999
1000     switch (a->type) {
1001     case OFPACT_OUTPUT:
1002         return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1003                                          max_ports);
1004
1005     case OFPACT_CONTROLLER:
1006         return 0;
1007
1008     case OFPACT_ENQUEUE:
1009         enqueue = ofpact_get_ENQUEUE(a);
1010         if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
1011             && enqueue->port != OFPP_LOCAL) {
1012             return OFPERR_OFPBAC_BAD_OUT_PORT;
1013         }
1014         return 0;
1015
1016     case OFPACT_OUTPUT_REG:
1017         return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1018
1019     case OFPACT_BUNDLE:
1020         return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1021
1022     case OFPACT_SET_VLAN_VID:
1023     case OFPACT_SET_VLAN_PCP:
1024     case OFPACT_STRIP_VLAN:
1025     case OFPACT_SET_ETH_SRC:
1026     case OFPACT_SET_ETH_DST:
1027     case OFPACT_SET_IPV4_SRC:
1028     case OFPACT_SET_IPV4_DST:
1029     case OFPACT_SET_IPV4_DSCP:
1030     case OFPACT_SET_L4_SRC_PORT:
1031     case OFPACT_SET_L4_DST_PORT:
1032         return 0;
1033
1034     case OFPACT_REG_MOVE:
1035         return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1036
1037     case OFPACT_REG_LOAD:
1038         return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1039
1040     case OFPACT_DEC_TTL:
1041     case OFPACT_SET_TUNNEL:
1042     case OFPACT_SET_QUEUE:
1043     case OFPACT_POP_QUEUE:
1044     case OFPACT_FIN_TIMEOUT:
1045     case OFPACT_RESUBMIT:
1046         return 0;
1047
1048     case OFPACT_LEARN:
1049         return learn_check(ofpact_get_LEARN(a), flow);
1050
1051     case OFPACT_MULTIPATH:
1052         return multipath_check(ofpact_get_MULTIPATH(a), flow);
1053
1054     case OFPACT_AUTOPATH:
1055         return autopath_check(ofpact_get_AUTOPATH(a), flow);
1056
1057     case OFPACT_NOTE:
1058     case OFPACT_EXIT:
1059         return 0;
1060
1061     case OFPACT_CLEAR_ACTIONS:
1062     case OFPACT_GOTO_TABLE:
1063         return 0;
1064
1065     default:
1066         NOT_REACHED();
1067     }
1068 }
1069
1070 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1071  * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1072  * switch with no more than 'max_ports' ports. */
1073 enum ofperr
1074 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1075               const struct flow *flow, int max_ports)
1076 {
1077     const struct ofpact *a;
1078
1079     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1080         enum ofperr error = ofpact_check__(a, flow, max_ports);
1081         if (error) {
1082             return error;
1083         }
1084     }
1085
1086     return 0;
1087 }
1088 \f
1089 /* Converting ofpacts to Nicira OpenFlow extensions. */
1090
1091 static void
1092 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1093                                 struct ofpbuf *out)
1094 {
1095     struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1096
1097     naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1098                                            output_reg->src.n_bits);
1099     naor->src = htonl(output_reg->src.field->nxm_header);
1100     naor->max_len = htons(output_reg->max_len);
1101 }
1102
1103 static void
1104 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1105                          struct ofpbuf *out)
1106 {
1107     struct nx_action_resubmit *nar;
1108
1109     if (resubmit->table_id == 0xff
1110         && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1111         nar = ofputil_put_NXAST_RESUBMIT(out);
1112     } else {
1113         nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1114         nar->table = resubmit->table_id;
1115     }
1116     nar->in_port = htons(resubmit->in_port);
1117 }
1118
1119 static void
1120 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1121                            struct ofpbuf *out)
1122 {
1123     uint64_t tun_id = tunnel->tun_id;
1124
1125     if (tun_id <= UINT32_MAX
1126         && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1127         ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1128     } else {
1129         ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1130     }
1131 }
1132
1133 static void
1134 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1135 {
1136     size_t start_ofs = out->size;
1137     struct nx_action_note *nan;
1138     unsigned int remainder;
1139     unsigned int len;
1140
1141     nan = ofputil_put_NXAST_NOTE(out);
1142     out->size -= sizeof nan->note;
1143
1144     ofpbuf_put(out, note->data, note->length);
1145
1146     len = out->size - start_ofs;
1147     remainder = len % OFP_ACTION_ALIGN;
1148     if (remainder) {
1149         ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1150     }
1151     nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1152     nan->len = htons(out->size - start_ofs);
1153 }
1154
1155 static void
1156 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1157                            struct ofpbuf *out)
1158 {
1159     struct nx_action_controller *nac;
1160
1161     nac = ofputil_put_NXAST_CONTROLLER(out);
1162     nac->max_len = htons(oc->max_len);
1163     nac->controller_id = htons(oc->controller_id);
1164     nac->reason = oc->reason;
1165 }
1166
1167 static void
1168 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1169                         struct ofpbuf *out)
1170 {
1171     if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1172         ofputil_put_NXAST_DEC_TTL(out);
1173     } else {
1174         struct nx_action_cnt_ids *nac_ids =
1175             ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1176         int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1177         ovs_be16 *ids;
1178         size_t i;
1179
1180         nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1181         nac_ids->n_controllers = htons(oc_ids->n_controllers);
1182
1183         ids = ofpbuf_put_zeros(out, ids_len);
1184         for (i = 0; i < oc_ids->n_controllers; i++) {
1185             ids[i] = htons(oc_ids->cnt_ids[i]);
1186         }
1187     }
1188 }
1189
1190 static void
1191 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1192                             struct ofpbuf *out)
1193 {
1194     struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1195     naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1196     naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1197 }
1198
1199 static void
1200 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1201 {
1202     switch (a->type) {
1203     case OFPACT_CONTROLLER:
1204         ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1205         break;
1206
1207     case OFPACT_OUTPUT_REG:
1208         ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1209         break;
1210
1211     case OFPACT_BUNDLE:
1212         bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1213         break;
1214
1215     case OFPACT_REG_MOVE:
1216         nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1217         break;
1218
1219     case OFPACT_REG_LOAD:
1220         nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1221         break;
1222
1223     case OFPACT_DEC_TTL:
1224         ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1225         break;
1226
1227     case OFPACT_SET_TUNNEL:
1228         ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1229         break;
1230
1231     case OFPACT_SET_QUEUE:
1232         ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1233             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1234         break;
1235
1236     case OFPACT_POP_QUEUE:
1237         ofputil_put_NXAST_POP_QUEUE(out);
1238         break;
1239
1240     case OFPACT_FIN_TIMEOUT:
1241         ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1242         break;
1243
1244     case OFPACT_RESUBMIT:
1245         ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1246         break;
1247
1248     case OFPACT_LEARN:
1249         learn_to_nxast(ofpact_get_LEARN(a), out);
1250         break;
1251
1252     case OFPACT_MULTIPATH:
1253         multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1254         break;
1255
1256     case OFPACT_AUTOPATH:
1257         autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1258         break;
1259
1260     case OFPACT_NOTE:
1261         ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1262         break;
1263
1264     case OFPACT_EXIT:
1265         ofputil_put_NXAST_EXIT(out);
1266         break;
1267
1268     case OFPACT_OUTPUT:
1269     case OFPACT_ENQUEUE:
1270     case OFPACT_SET_VLAN_VID:
1271     case OFPACT_SET_VLAN_PCP:
1272     case OFPACT_STRIP_VLAN:
1273     case OFPACT_SET_ETH_SRC:
1274     case OFPACT_SET_ETH_DST:
1275     case OFPACT_SET_IPV4_SRC:
1276     case OFPACT_SET_IPV4_DST:
1277     case OFPACT_SET_IPV4_DSCP:
1278     case OFPACT_SET_L4_SRC_PORT:
1279     case OFPACT_SET_L4_DST_PORT:
1280     case OFPACT_CLEAR_ACTIONS:
1281     case OFPACT_GOTO_TABLE:
1282         NOT_REACHED();
1283     }
1284 }
1285 \f
1286 /* Converting ofpacts to OpenFlow 1.0. */
1287
1288 static void
1289 ofpact_output_to_openflow10(const struct ofpact_output *output,
1290                             struct ofpbuf *out)
1291 {
1292     struct ofp10_action_output *oao;
1293
1294     oao = ofputil_put_OFPAT10_OUTPUT(out);
1295     oao->port = htons(output->port);
1296     oao->max_len = htons(output->max_len);
1297 }
1298
1299 static void
1300 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1301                              struct ofpbuf *out)
1302 {
1303     struct ofp_action_enqueue *oae;
1304
1305     oae = ofputil_put_OFPAT10_ENQUEUE(out);
1306     oae->port = htons(enqueue->port);
1307     oae->queue_id = htonl(enqueue->queue);
1308 }
1309
1310 static void
1311 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1312 {
1313     switch (a->type) {
1314     case OFPACT_OUTPUT:
1315         ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1316         break;
1317
1318     case OFPACT_ENQUEUE:
1319         ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1320         break;
1321
1322     case OFPACT_SET_VLAN_VID:
1323         ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1324             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1325         break;
1326
1327     case OFPACT_SET_VLAN_PCP:
1328         ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1329             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1330         break;
1331
1332     case OFPACT_STRIP_VLAN:
1333         ofputil_put_OFPAT10_STRIP_VLAN(out);
1334         break;
1335
1336     case OFPACT_SET_ETH_SRC:
1337         memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1338                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1339         break;
1340
1341     case OFPACT_SET_ETH_DST:
1342         memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1343                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1344         break;
1345
1346     case OFPACT_SET_IPV4_SRC:
1347         ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1348             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1349         break;
1350
1351     case OFPACT_SET_IPV4_DST:
1352         ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1353             = ofpact_get_SET_IPV4_DST(a)->ipv4;
1354         break;
1355
1356     case OFPACT_SET_IPV4_DSCP:
1357         ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1358             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1359         break;
1360
1361     case OFPACT_SET_L4_SRC_PORT:
1362         ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1363             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1364         break;
1365
1366     case OFPACT_SET_L4_DST_PORT:
1367         ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1368             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1369         break;
1370
1371     case OFPACT_CLEAR_ACTIONS:
1372     case OFPACT_GOTO_TABLE:
1373         /* TODO:XXX */
1374         break;
1375
1376     case OFPACT_CONTROLLER:
1377     case OFPACT_OUTPUT_REG:
1378     case OFPACT_BUNDLE:
1379     case OFPACT_REG_MOVE:
1380     case OFPACT_REG_LOAD:
1381     case OFPACT_DEC_TTL:
1382     case OFPACT_SET_TUNNEL:
1383     case OFPACT_SET_QUEUE:
1384     case OFPACT_POP_QUEUE:
1385     case OFPACT_FIN_TIMEOUT:
1386     case OFPACT_RESUBMIT:
1387     case OFPACT_LEARN:
1388     case OFPACT_MULTIPATH:
1389     case OFPACT_AUTOPATH:
1390     case OFPACT_NOTE:
1391     case OFPACT_EXIT:
1392         ofpact_to_nxast(a, out);
1393         break;
1394     }
1395 }
1396
1397 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1398  * actions in 'openflow', appending the actions to any existing data in
1399  * 'openflow'. */
1400 void
1401 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1402                        struct ofpbuf *openflow)
1403 {
1404     const struct ofpact *a;
1405
1406     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1407         ofpact_to_openflow10(a, openflow);
1408     }
1409 }
1410 \f
1411 /* Converting ofpacts to OpenFlow 1.1. */
1412
1413 static void
1414 ofpact_output_to_openflow11(const struct ofpact_output *output,
1415                             struct ofpbuf *out)
1416 {
1417     struct ofp11_action_output *oao;
1418
1419     oao = ofputil_put_OFPAT11_OUTPUT(out);
1420     oao->port = ofputil_port_to_ofp11(output->port);
1421     oao->max_len = htons(output->max_len);
1422 }
1423
1424 static void
1425 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1426 {
1427     switch (a->type) {
1428     case OFPACT_OUTPUT:
1429         return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1430
1431     case OFPACT_ENQUEUE:
1432         /* XXX */
1433         break;
1434
1435     case OFPACT_SET_VLAN_VID:
1436         ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1437             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1438         break;
1439
1440     case OFPACT_SET_VLAN_PCP:
1441         ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1442             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1443         break;
1444
1445     case OFPACT_STRIP_VLAN:
1446         /* XXX */
1447         break;
1448
1449     case OFPACT_SET_ETH_SRC:
1450         memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1451                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1452         break;
1453
1454     case OFPACT_SET_ETH_DST:
1455         memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1456                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1457         break;
1458
1459     case OFPACT_SET_IPV4_SRC:
1460         ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1461             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1462         break;
1463
1464     case OFPACT_SET_IPV4_DST:
1465         ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1466             = ofpact_get_SET_IPV4_DST(a)->ipv4;
1467         break;
1468
1469     case OFPACT_SET_IPV4_DSCP:
1470         ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1471             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1472         break;
1473
1474     case OFPACT_SET_L4_SRC_PORT:
1475         ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1476             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1477         break;
1478
1479     case OFPACT_SET_L4_DST_PORT:
1480         ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1481             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1482         break;
1483
1484     case OFPACT_DEC_TTL:
1485         if (a->compat == OFPUTIL_OFPAT11_DEC_NW_TTL) {
1486             ofputil_put_OFPAT11_DEC_NW_TTL(out);
1487         } else {
1488             ofpact_to_nxast(a, out);
1489         }
1490         break;
1491
1492     case OFPACT_CLEAR_ACTIONS:
1493     case OFPACT_GOTO_TABLE:
1494         NOT_REACHED();
1495
1496     case OFPACT_CONTROLLER:
1497     case OFPACT_OUTPUT_REG:
1498     case OFPACT_BUNDLE:
1499     case OFPACT_REG_MOVE:
1500     case OFPACT_REG_LOAD:
1501     case OFPACT_SET_TUNNEL:
1502     case OFPACT_SET_QUEUE:
1503     case OFPACT_POP_QUEUE:
1504     case OFPACT_FIN_TIMEOUT:
1505     case OFPACT_RESUBMIT:
1506     case OFPACT_LEARN:
1507     case OFPACT_MULTIPATH:
1508     case OFPACT_AUTOPATH:
1509     case OFPACT_NOTE:
1510     case OFPACT_EXIT:
1511         ofpact_to_nxast(a, out);
1512         break;
1513     }
1514 }
1515
1516 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1517  * 1.1 actions in 'openflow', appending the actions to any existing data in
1518  * 'openflow'. */
1519 size_t
1520 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1521                                size_t ofpacts_len, struct ofpbuf *openflow)
1522 {
1523     const struct ofpact *a;
1524     size_t start_size = openflow->size;
1525
1526     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1527         ofpact_to_openflow11(a, openflow);
1528     }
1529
1530     return openflow->size - start_size;
1531 }
1532
1533 static void
1534 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
1535 {
1536     struct ofp11_instruction_actions *oia;
1537
1538     /* Update the instruction's length (or, if it's empty, delete it). */
1539     oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1540     if (openflow->size > ofs + sizeof *oia) {
1541         oia->len = htons(openflow->size - ofs);
1542     } else {
1543         openflow->size = ofs;
1544     }
1545 }
1546
1547 void
1548 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1549                                     size_t ofpacts_len,
1550                                     struct ofpbuf *openflow)
1551 {
1552     const struct ofpact *a;
1553
1554     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1555         /* TODO:XXX Write-Actions */
1556         /* TODO:XXX Write-Metadata */
1557         if (a->type == OFPACT_CLEAR_ACTIONS) {
1558             instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
1559         } else if (a->type == OFPACT_GOTO_TABLE) {
1560             struct ofp11_instruction_goto_table *oigt;
1561
1562             oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1563             oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1564             memset(oigt->pad, 0, sizeof oigt->pad);
1565         } else if (!ofpact_is_instruction(a)) {
1566             /* Apply-actions */
1567             const size_t ofs = openflow->size;
1568             const size_t ofpacts_len_left =
1569                 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1570             const struct ofpact *action;
1571             const struct ofpact *processed = a;
1572
1573             instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1574             OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1575                 if (ofpact_is_instruction(action)) {
1576                     break;
1577                 }
1578                 ofpact_to_openflow11(action, openflow);
1579                 processed = action;
1580             }
1581             ofpacts_update_instruction_actions(openflow, ofs);
1582             a = processed;
1583         }
1584     }
1585 }
1586 \f
1587 /* Returns true if 'action' outputs to 'port', false otherwise. */
1588 static bool
1589 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1590 {
1591     switch (ofpact->type) {
1592     case OFPACT_OUTPUT:
1593         return ofpact_get_OUTPUT(ofpact)->port == port;
1594     case OFPACT_ENQUEUE:
1595         return ofpact_get_ENQUEUE(ofpact)->port == port;
1596     case OFPACT_CONTROLLER:
1597         return port == OFPP_CONTROLLER;
1598
1599     case OFPACT_OUTPUT_REG:
1600     case OFPACT_BUNDLE:
1601     case OFPACT_SET_VLAN_VID:
1602     case OFPACT_SET_VLAN_PCP:
1603     case OFPACT_STRIP_VLAN:
1604     case OFPACT_SET_ETH_SRC:
1605     case OFPACT_SET_ETH_DST:
1606     case OFPACT_SET_IPV4_SRC:
1607     case OFPACT_SET_IPV4_DST:
1608     case OFPACT_SET_IPV4_DSCP:
1609     case OFPACT_SET_L4_SRC_PORT:
1610     case OFPACT_SET_L4_DST_PORT:
1611     case OFPACT_REG_MOVE:
1612     case OFPACT_REG_LOAD:
1613     case OFPACT_DEC_TTL:
1614     case OFPACT_SET_TUNNEL:
1615     case OFPACT_SET_QUEUE:
1616     case OFPACT_POP_QUEUE:
1617     case OFPACT_FIN_TIMEOUT:
1618     case OFPACT_RESUBMIT:
1619     case OFPACT_LEARN:
1620     case OFPACT_MULTIPATH:
1621     case OFPACT_AUTOPATH:
1622     case OFPACT_NOTE:
1623     case OFPACT_EXIT:
1624     case OFPACT_CLEAR_ACTIONS:
1625     case OFPACT_GOTO_TABLE:
1626     default:
1627         return false;
1628     }
1629 }
1630
1631 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1632  * to 'port', false otherwise. */
1633 bool
1634 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1635                        uint16_t port)
1636 {
1637     const struct ofpact *a;
1638
1639     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1640         if (ofpact_outputs_to_port(a, port)) {
1641             return true;
1642         }
1643     }
1644
1645     return false;
1646 }
1647
1648 bool
1649 ofpacts_equal(const struct ofpact *a, size_t a_len,
1650               const struct ofpact *b, size_t b_len)
1651 {
1652     return a_len == b_len && !memcmp(a, b, a_len);
1653 }
1654 \f
1655 /* Formatting ofpacts. */
1656
1657 static void
1658 print_note(const struct ofpact_note *note, struct ds *string)
1659 {
1660     size_t i;
1661
1662     ds_put_cstr(string, "note:");
1663     for (i = 0; i < note->length; i++) {
1664         if (i) {
1665             ds_put_char(string, '.');
1666         }
1667         ds_put_format(string, "%02"PRIx8, note->data[i]);
1668     }
1669 }
1670
1671 static void
1672 print_dec_ttl(const struct ofpact_cnt_ids *ids,
1673               struct ds *s)
1674 {
1675     size_t i;
1676
1677     ds_put_cstr(s, "dec_ttl");
1678     if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1679         ds_put_cstr(s, "(");
1680         for (i = 0; i < ids->n_controllers; i++) {
1681             if (i) {
1682                 ds_put_cstr(s, ",");
1683             }
1684             ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1685         }
1686         ds_put_cstr(s, ")");
1687     }
1688 }
1689
1690 static void
1691 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1692                   struct ds *s)
1693 {
1694     ds_put_cstr(s, "fin_timeout(");
1695     if (fin_timeout->fin_idle_timeout) {
1696         ds_put_format(s, "idle_timeout=%"PRIu16",",
1697                       fin_timeout->fin_idle_timeout);
1698     }
1699     if (fin_timeout->fin_hard_timeout) {
1700         ds_put_format(s, "hard_timeout=%"PRIu16",",
1701                       fin_timeout->fin_hard_timeout);
1702     }
1703     ds_chomp(s, ',');
1704     ds_put_char(s, ')');
1705 }
1706
1707 static void
1708 ofpact_format(const struct ofpact *a, struct ds *s)
1709 {
1710     const struct ofpact_enqueue *enqueue;
1711     const struct ofpact_resubmit *resubmit;
1712     const struct ofpact_autopath *autopath;
1713     const struct ofpact_controller *controller;
1714     const struct ofpact_tunnel *tunnel;
1715     uint16_t port;
1716
1717     switch (a->type) {
1718     case OFPACT_OUTPUT:
1719         port = ofpact_get_OUTPUT(a)->port;
1720         if (port < OFPP_MAX) {
1721             ds_put_format(s, "output:%"PRIu16, port);
1722         } else {
1723             ofputil_format_port(port, s);
1724             if (port == OFPP_CONTROLLER) {
1725                 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1726             }
1727         }
1728         break;
1729
1730     case OFPACT_CONTROLLER:
1731         controller = ofpact_get_CONTROLLER(a);
1732         if (controller->reason == OFPR_ACTION &&
1733             controller->controller_id == 0) {
1734             ds_put_format(s, "CONTROLLER:%"PRIu16,
1735                           ofpact_get_CONTROLLER(a)->max_len);
1736         } else {
1737             enum ofp_packet_in_reason reason = controller->reason;
1738
1739             ds_put_cstr(s, "controller(");
1740             if (reason != OFPR_ACTION) {
1741                 ds_put_format(s, "reason=%s,",
1742                               ofputil_packet_in_reason_to_string(reason));
1743             }
1744             if (controller->max_len != UINT16_MAX) {
1745                 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1746             }
1747             if (controller->controller_id != 0) {
1748                 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1749             }
1750             ds_chomp(s, ',');
1751             ds_put_char(s, ')');
1752         }
1753         break;
1754
1755     case OFPACT_ENQUEUE:
1756         enqueue = ofpact_get_ENQUEUE(a);
1757         ds_put_format(s, "enqueue:");
1758         ofputil_format_port(enqueue->port, s);
1759         ds_put_format(s, "q%"PRIu32, enqueue->queue);
1760         break;
1761
1762     case OFPACT_OUTPUT_REG:
1763         ds_put_cstr(s, "output:");
1764         mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1765         break;
1766
1767     case OFPACT_BUNDLE:
1768         bundle_format(ofpact_get_BUNDLE(a), s);
1769         break;
1770
1771     case OFPACT_SET_VLAN_VID:
1772         ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1773                       ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1774         break;
1775
1776     case OFPACT_SET_VLAN_PCP:
1777         ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1778                       ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1779         break;
1780
1781     case OFPACT_STRIP_VLAN:
1782         ds_put_cstr(s, "strip_vlan");
1783         break;
1784
1785     case OFPACT_SET_ETH_SRC:
1786         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1787                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1788         break;
1789
1790     case OFPACT_SET_ETH_DST:
1791         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1792                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1793         break;
1794
1795     case OFPACT_SET_IPV4_SRC:
1796         ds_put_format(s, "mod_nw_src:"IP_FMT,
1797                       IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1798         break;
1799
1800     case OFPACT_SET_IPV4_DST:
1801         ds_put_format(s, "mod_nw_dst:"IP_FMT,
1802                       IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1803         break;
1804
1805     case OFPACT_SET_IPV4_DSCP:
1806         ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1807         break;
1808
1809     case OFPACT_SET_L4_SRC_PORT:
1810         ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1811         break;
1812
1813     case OFPACT_SET_L4_DST_PORT:
1814         ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1815         break;
1816
1817     case OFPACT_REG_MOVE:
1818         nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1819         break;
1820
1821     case OFPACT_REG_LOAD:
1822         nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1823         break;
1824
1825     case OFPACT_DEC_TTL:
1826         print_dec_ttl(ofpact_get_DEC_TTL(a), s);
1827         break;
1828
1829     case OFPACT_SET_TUNNEL:
1830         tunnel = ofpact_get_SET_TUNNEL(a);
1831         ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1832                       (tunnel->tun_id > UINT32_MAX
1833                        || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1834                       tunnel->tun_id);
1835         break;
1836
1837     case OFPACT_SET_QUEUE:
1838         ds_put_format(s, "set_queue:%"PRIu32,
1839                       ofpact_get_SET_QUEUE(a)->queue_id);
1840         break;
1841
1842     case OFPACT_POP_QUEUE:
1843         ds_put_cstr(s, "pop_queue");
1844         break;
1845
1846     case OFPACT_FIN_TIMEOUT:
1847         print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1848         break;
1849
1850     case OFPACT_RESUBMIT:
1851         resubmit = ofpact_get_RESUBMIT(a);
1852         if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
1853             ds_put_cstr(s, "resubmit:");
1854             ofputil_format_port(resubmit->in_port, s);
1855         } else {
1856             ds_put_format(s, "resubmit(");
1857             if (resubmit->in_port != OFPP_IN_PORT) {
1858                 ofputil_format_port(resubmit->in_port, s);
1859             }
1860             ds_put_char(s, ',');
1861             if (resubmit->table_id != 255) {
1862                 ds_put_format(s, "%"PRIu8, resubmit->table_id);
1863             }
1864             ds_put_char(s, ')');
1865         }
1866         break;
1867
1868     case OFPACT_LEARN:
1869         learn_format(ofpact_get_LEARN(a), s);
1870         break;
1871
1872     case OFPACT_MULTIPATH:
1873         multipath_format(ofpact_get_MULTIPATH(a), s);
1874         break;
1875
1876     case OFPACT_AUTOPATH:
1877         autopath = ofpact_get_AUTOPATH(a);
1878         ds_put_cstr(s, "autopath(");
1879         ofputil_format_port(autopath->port, s);
1880         ds_put_char(s, ',');
1881         mf_format_subfield(&autopath->dst, s);
1882         ds_put_char(s, ')');
1883         break;
1884
1885     case OFPACT_NOTE:
1886         print_note(ofpact_get_NOTE(a), s);
1887         break;
1888
1889     case OFPACT_EXIT:
1890         ds_put_cstr(s, "exit");
1891         break;
1892
1893     case OFPACT_CLEAR_ACTIONS:
1894         ds_put_format(s, "%s",
1895                       ofpact_instruction_name_from_type(
1896                           OVSINST_OFPIT11_CLEAR_ACTIONS));
1897         break;
1898
1899     case OFPACT_GOTO_TABLE:
1900         ds_put_format(s, "%s:%"PRIu8,
1901                       ofpact_instruction_name_from_type(
1902                           OVSINST_OFPIT11_GOTO_TABLE),
1903                       ofpact_get_GOTO_TABLE(a)->table_id);
1904         break;
1905     }
1906 }
1907
1908 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
1909  * 'ofpacts' to 'string'. */
1910 void
1911 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
1912                struct ds *string)
1913 {
1914     ds_put_cstr(string, "actions=");
1915     if (!ofpacts_len) {
1916         ds_put_cstr(string, "drop");
1917     } else {
1918         const struct ofpact *a;
1919
1920         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1921             if (a != ofpacts) {
1922                 ds_put_cstr(string, ",");
1923             }
1924
1925             /* TODO:XXX write-actions */
1926             /* TODO:XXX write-metadata */
1927             ofpact_format(a, string);
1928         }
1929     }
1930 }
1931 \f
1932 /* Internal use by helpers. */
1933
1934 void *
1935 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
1936 {
1937     struct ofpact *ofpact;
1938
1939     ofpact_pad(ofpacts);
1940     ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
1941     ofpact_init(ofpact, type, len);
1942     return ofpact;
1943 }
1944
1945 void
1946 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
1947 {
1948     memset(ofpact, 0, len);
1949     ofpact->type = type;
1950     ofpact->compat = OFPUTIL_ACTION_INVALID;
1951     ofpact->len = len;
1952 }
1953 \f
1954 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
1955  * starting at 'ofpact'.
1956  *
1957  * This is the correct way to update a variable-length ofpact's length after
1958  * adding the variable-length part of the payload.  (See the large comment
1959  * near the end of ofp-actions.h for more information.) */
1960 void
1961 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
1962 {
1963     assert(ofpact == ofpacts->l2);
1964     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
1965 }
1966
1967 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
1968  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
1969  * client must call this itself after adding the final ofpact to an array of
1970  * them.
1971  *
1972  * (The consequences of failing to call this function are probably not dire.
1973  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
1974  * not dereference it.  That's undefined behavior, technically, but it will not
1975  * cause a real problem on common systems.  Still, it seems better to call
1976  * it.) */
1977 void
1978 ofpact_pad(struct ofpbuf *ofpacts)
1979 {
1980     unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
1981     if (rem) {
1982         ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
1983     }
1984 }
1985
1986 void
1987 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
1988                       const void *src)
1989 {
1990     load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
1991     load->dst.field = mf;
1992     load->dst.ofs = 0;
1993     load->dst.n_bits = mf->n_bits;
1994     bitwise_copy(src, mf->n_bytes, load->dst.ofs,
1995                  &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);
1996 }