ofp-actions: Prefer OFPAT11_DEC_TTL to NXAST_DEC_TTL for OpenFlow 1.1+.
[sliver-openvswitch.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_POP_VLAN:
689         ofpact_put_STRIP_VLAN(out);
690         break;
691
692     case OFPUTIL_OFPAT11_SET_DL_SRC:
693         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
694                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
695         break;
696
697     case OFPUTIL_OFPAT11_SET_DL_DST:
698         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
699                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
700         break;
701
702     case OFPUTIL_OFPAT11_DEC_NW_TTL:
703         dec_ttl_from_openflow(out, code);
704         break;
705
706     case OFPUTIL_OFPAT11_SET_NW_SRC:
707         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
708         break;
709
710     case OFPUTIL_OFPAT11_SET_NW_DST:
711         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
712         break;
713
714     case OFPUTIL_OFPAT11_SET_NW_TOS:
715         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
716             return OFPERR_OFPBAC_BAD_ARGUMENT;
717         }
718         ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
719         break;
720
721     case OFPUTIL_OFPAT11_SET_TP_SRC:
722         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
723         break;
724
725     case OFPUTIL_OFPAT11_SET_TP_DST:
726         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
727         break;
728
729     case OFPUTIL_OFPAT12_SET_FIELD:
730         return nxm_reg_load_from_openflow12_set_field(
731             (const struct ofp12_action_set_field *)a, out);
732
733 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
734 #include "ofp-util.def"
735         return ofpact_from_nxast(a, code, out);
736     }
737
738     return error;
739 }
740
741 static enum ofperr
742 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
743                         struct ofpbuf *out)
744 {
745     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
746 }
747 \f
748 /* OpenFlow 1.1 instructions. */
749
750 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
751     static inline const struct STRUCT *                         \
752     instruction_get_##ENUM(const struct ofp11_instruction *inst)\
753     {                                                           \
754         assert(inst->type == htons(ENUM));                      \
755         return (struct STRUCT *)inst;                           \
756     }                                                           \
757                                                                 \
758     static inline void                                          \
759     instruction_init_##ENUM(struct STRUCT *s)                   \
760     {                                                           \
761         memset(s, 0, sizeof *s);                                \
762         s->type = htons(ENUM);                                  \
763         s->len = htons(sizeof *s);                              \
764     }                                                           \
765                                                                 \
766     static inline struct STRUCT *                               \
767     instruction_put_##ENUM(struct ofpbuf *buf)                  \
768     {                                                           \
769         struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
770         instruction_init_##ENUM(s);                             \
771         return s;                                               \
772     }
773 OVS_INSTRUCTIONS
774 #undef DEFINE_INST
775
776 struct instruction_type_info {
777     enum ovs_instruction_type type;
778     const char *name;
779 };
780
781 static const struct instruction_type_info inst_info[] = {
782 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
783 OVS_INSTRUCTIONS
784 #undef DEFINE_INST
785 };
786
787 const char *
788 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
789 {
790     return inst_info[type].name;
791 }
792
793 int
794 ofpact_instruction_type_from_name(const char *name)
795 {
796     const struct instruction_type_info *p;
797     for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
798         if (!strcasecmp(name, p->name)) {
799             return p->type;
800         }
801     }
802     return -1;
803 }
804
805 static inline struct ofp11_instruction *
806 instruction_next(const struct ofp11_instruction *inst)
807 {
808     return ((struct ofp11_instruction *) (void *)
809             ((uint8_t *) inst + ntohs(inst->len)));
810 }
811
812 static inline bool
813 instruction_is_valid(const struct ofp11_instruction *inst,
814                      size_t n_instructions)
815 {
816     uint16_t len = ntohs(inst->len);
817     return (!(len % OFP11_INSTRUCTION_ALIGN)
818             && len >= sizeof *inst
819             && len / sizeof *inst <= n_instructions);
820 }
821
822 /* This macro is careful to check for instructions with bad lengths. */
823 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS)  \
824     for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS);            \
825          (LEFT) > 0 && instruction_is_valid(ITER, LEFT);                \
826          ((LEFT) -= (ntohs((ITER)->len)                                 \
827                      / sizeof(struct ofp11_instruction)),               \
828           (ITER) = instruction_next(ITER)))
829
830 static enum ofperr
831 decode_openflow11_instruction(const struct ofp11_instruction *inst,
832                               enum ovs_instruction_type *type)
833 {
834     uint16_t len = ntohs(inst->len);
835
836     switch (inst->type) {
837     case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
838         return OFPERR_OFPBIC_BAD_EXPERIMENTER;
839
840 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)     \
841         case CONSTANT_HTONS(ENUM):                      \
842             if (EXTENSIBLE                              \
843                 ? len >= sizeof(struct STRUCT)          \
844                 : len == sizeof(struct STRUCT)) {       \
845                 *type = OVSINST_##ENUM;                 \
846                 return 0;                               \
847             } else {                                    \
848                 return OFPERR_OFPBIC_BAD_LEN;           \
849             }
850 OVS_INSTRUCTIONS
851 #undef DEFINE_INST
852
853     default:
854         return OFPERR_OFPBIC_UNKNOWN_INST;
855     }
856 }
857
858 static enum ofperr
859 decode_openflow11_instructions(const struct ofp11_instruction insts[],
860                                size_t n_insts,
861                                const struct ofp11_instruction *out[])
862 {
863     const struct ofp11_instruction *inst;
864     size_t left;
865
866     memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
867     INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
868         enum ovs_instruction_type type;
869         enum ofperr error;
870
871         error = decode_openflow11_instruction(inst, &type);
872         if (error) {
873             return error;
874         }
875
876         if (out[type]) {
877             return OFPERR_OFPIT_BAD_INSTRUCTION;
878         }
879         out[type] = inst;
880     }
881
882     if (left) {
883         VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
884                      (n_insts - left) * sizeof *inst);
885         return OFPERR_OFPBIC_BAD_LEN;
886     }
887     return 0;
888 }
889
890 static void
891 get_actions_from_instruction(const struct ofp11_instruction *inst,
892                          const union ofp_action **actions,
893                          size_t *n_actions)
894 {
895     *actions = (const union ofp_action *) (inst + 1);
896     *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
897 }
898
899 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
900  * front of 'openflow' into ofpacts.  On success, replaces any existing content
901  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
902  * Returns 0 if successful, otherwise an OpenFlow error.
903  *
904  * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
905  * instructions, so you should call ofpacts_pull_openflow11_instructions()
906  * instead of this function.
907  *
908  * The parsed actions are valid generically, but they may not be valid in a
909  * specific context.  For example, port numbers up to OFPP_MAX are valid
910  * generically, but specific datapaths may only support port numbers in a
911  * smaller range.  Use ofpacts_check() to additional check whether actions are
912  * valid in a specific context. */
913 enum ofperr
914 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
915                                 unsigned int actions_len,
916                                 struct ofpbuf *ofpacts)
917 {
918     return ofpacts_pull_actions(openflow, actions_len, ofpacts,
919                                 ofpacts_from_openflow11);
920 }
921
922 enum ofperr
923 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
924                                      unsigned int instructions_len,
925                                      struct ofpbuf *ofpacts)
926 {
927     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
928     const struct ofp11_instruction *instructions;
929     const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
930     enum ofperr error;
931
932     ofpbuf_clear(ofpacts);
933
934     if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
935         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
936                      "multiple of %d",
937                      instructions_len, OFP11_INSTRUCTION_ALIGN);
938         error = OFPERR_OFPBIC_BAD_LEN;
939         goto exit;
940     }
941
942     instructions = ofpbuf_try_pull(openflow, instructions_len);
943     if (instructions == NULL) {
944         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
945                      "remaining message length (%zu)",
946                      instructions_len, openflow->size);
947         error = OFPERR_OFPBIC_BAD_LEN;
948         goto exit;
949     }
950
951     error = decode_openflow11_instructions(
952         instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
953         insts);
954     if (error) {
955         goto exit;
956     }
957
958     if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
959         const union ofp_action *actions;
960         size_t n_actions;
961
962         get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
963                                      &actions, &n_actions);
964         error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
965         if (error) {
966             goto exit;
967         }
968     }
969     if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
970         instruction_get_OFPIT11_CLEAR_ACTIONS(
971             insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
972         ofpact_put_CLEAR_ACTIONS(ofpacts);
973     }
974     /* TODO:XXX Write-Actions */
975     /* TODO:XXX Write-Metadata */
976     if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
977         const struct ofp11_instruction_goto_table *oigt;
978         struct ofpact_goto_table *ogt;
979
980         oigt = instruction_get_OFPIT11_GOTO_TABLE(
981             insts[OVSINST_OFPIT11_GOTO_TABLE]);
982         ogt = ofpact_put_GOTO_TABLE(ofpacts);
983         ogt->table_id = oigt->table_id;
984     }
985
986     if (insts[OVSINST_OFPIT11_WRITE_METADATA] ||
987         insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
988         error = OFPERR_OFPBIC_UNSUP_INST;
989         goto exit;
990     }
991
992 exit:
993     if (error) {
994         ofpbuf_clear(ofpacts);
995     }
996     return error;
997 }
998 \f
999 static enum ofperr
1000 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
1001 {
1002     const struct ofpact_enqueue *enqueue;
1003
1004     switch (a->type) {
1005     case OFPACT_OUTPUT:
1006         return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1007                                          max_ports);
1008
1009     case OFPACT_CONTROLLER:
1010         return 0;
1011
1012     case OFPACT_ENQUEUE:
1013         enqueue = ofpact_get_ENQUEUE(a);
1014         if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
1015             && enqueue->port != OFPP_LOCAL) {
1016             return OFPERR_OFPBAC_BAD_OUT_PORT;
1017         }
1018         return 0;
1019
1020     case OFPACT_OUTPUT_REG:
1021         return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1022
1023     case OFPACT_BUNDLE:
1024         return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1025
1026     case OFPACT_SET_VLAN_VID:
1027     case OFPACT_SET_VLAN_PCP:
1028     case OFPACT_STRIP_VLAN:
1029     case OFPACT_SET_ETH_SRC:
1030     case OFPACT_SET_ETH_DST:
1031     case OFPACT_SET_IPV4_SRC:
1032     case OFPACT_SET_IPV4_DST:
1033     case OFPACT_SET_IPV4_DSCP:
1034     case OFPACT_SET_L4_SRC_PORT:
1035     case OFPACT_SET_L4_DST_PORT:
1036         return 0;
1037
1038     case OFPACT_REG_MOVE:
1039         return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1040
1041     case OFPACT_REG_LOAD:
1042         return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1043
1044     case OFPACT_DEC_TTL:
1045     case OFPACT_SET_TUNNEL:
1046     case OFPACT_SET_QUEUE:
1047     case OFPACT_POP_QUEUE:
1048     case OFPACT_FIN_TIMEOUT:
1049     case OFPACT_RESUBMIT:
1050         return 0;
1051
1052     case OFPACT_LEARN:
1053         return learn_check(ofpact_get_LEARN(a), flow);
1054
1055     case OFPACT_MULTIPATH:
1056         return multipath_check(ofpact_get_MULTIPATH(a), flow);
1057
1058     case OFPACT_AUTOPATH:
1059         return autopath_check(ofpact_get_AUTOPATH(a), flow);
1060
1061     case OFPACT_NOTE:
1062     case OFPACT_EXIT:
1063         return 0;
1064
1065     case OFPACT_CLEAR_ACTIONS:
1066     case OFPACT_GOTO_TABLE:
1067         return 0;
1068
1069     default:
1070         NOT_REACHED();
1071     }
1072 }
1073
1074 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1075  * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1076  * switch with no more than 'max_ports' ports. */
1077 enum ofperr
1078 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1079               const struct flow *flow, int max_ports)
1080 {
1081     const struct ofpact *a;
1082
1083     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1084         enum ofperr error = ofpact_check__(a, flow, max_ports);
1085         if (error) {
1086             return error;
1087         }
1088     }
1089
1090     return 0;
1091 }
1092 \f
1093 /* Converting ofpacts to Nicira OpenFlow extensions. */
1094
1095 static void
1096 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1097                                 struct ofpbuf *out)
1098 {
1099     struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1100
1101     naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1102                                            output_reg->src.n_bits);
1103     naor->src = htonl(output_reg->src.field->nxm_header);
1104     naor->max_len = htons(output_reg->max_len);
1105 }
1106
1107 static void
1108 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1109                          struct ofpbuf *out)
1110 {
1111     struct nx_action_resubmit *nar;
1112
1113     if (resubmit->table_id == 0xff
1114         && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1115         nar = ofputil_put_NXAST_RESUBMIT(out);
1116     } else {
1117         nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1118         nar->table = resubmit->table_id;
1119     }
1120     nar->in_port = htons(resubmit->in_port);
1121 }
1122
1123 static void
1124 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1125                            struct ofpbuf *out)
1126 {
1127     uint64_t tun_id = tunnel->tun_id;
1128
1129     if (tun_id <= UINT32_MAX
1130         && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1131         ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1132     } else {
1133         ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1134     }
1135 }
1136
1137 static void
1138 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1139 {
1140     size_t start_ofs = out->size;
1141     struct nx_action_note *nan;
1142     unsigned int remainder;
1143     unsigned int len;
1144
1145     nan = ofputil_put_NXAST_NOTE(out);
1146     out->size -= sizeof nan->note;
1147
1148     ofpbuf_put(out, note->data, note->length);
1149
1150     len = out->size - start_ofs;
1151     remainder = len % OFP_ACTION_ALIGN;
1152     if (remainder) {
1153         ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1154     }
1155     nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1156     nan->len = htons(out->size - start_ofs);
1157 }
1158
1159 static void
1160 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1161                            struct ofpbuf *out)
1162 {
1163     struct nx_action_controller *nac;
1164
1165     nac = ofputil_put_NXAST_CONTROLLER(out);
1166     nac->max_len = htons(oc->max_len);
1167     nac->controller_id = htons(oc->controller_id);
1168     nac->reason = oc->reason;
1169 }
1170
1171 static void
1172 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1173                         struct ofpbuf *out)
1174 {
1175     if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1176         ofputil_put_NXAST_DEC_TTL(out);
1177     } else {
1178         struct nx_action_cnt_ids *nac_ids =
1179             ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1180         int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1181         ovs_be16 *ids;
1182         size_t i;
1183
1184         nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1185         nac_ids->n_controllers = htons(oc_ids->n_controllers);
1186
1187         ids = ofpbuf_put_zeros(out, ids_len);
1188         for (i = 0; i < oc_ids->n_controllers; i++) {
1189             ids[i] = htons(oc_ids->cnt_ids[i]);
1190         }
1191     }
1192 }
1193
1194 static void
1195 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1196                             struct ofpbuf *out)
1197 {
1198     struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1199     naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1200     naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1201 }
1202
1203 static void
1204 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1205 {
1206     switch (a->type) {
1207     case OFPACT_CONTROLLER:
1208         ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1209         break;
1210
1211     case OFPACT_OUTPUT_REG:
1212         ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1213         break;
1214
1215     case OFPACT_BUNDLE:
1216         bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1217         break;
1218
1219     case OFPACT_REG_MOVE:
1220         nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1221         break;
1222
1223     case OFPACT_REG_LOAD:
1224         nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1225         break;
1226
1227     case OFPACT_DEC_TTL:
1228         ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1229         break;
1230
1231     case OFPACT_SET_TUNNEL:
1232         ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1233         break;
1234
1235     case OFPACT_SET_QUEUE:
1236         ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1237             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1238         break;
1239
1240     case OFPACT_POP_QUEUE:
1241         ofputil_put_NXAST_POP_QUEUE(out);
1242         break;
1243
1244     case OFPACT_FIN_TIMEOUT:
1245         ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1246         break;
1247
1248     case OFPACT_RESUBMIT:
1249         ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1250         break;
1251
1252     case OFPACT_LEARN:
1253         learn_to_nxast(ofpact_get_LEARN(a), out);
1254         break;
1255
1256     case OFPACT_MULTIPATH:
1257         multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1258         break;
1259
1260     case OFPACT_AUTOPATH:
1261         autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1262         break;
1263
1264     case OFPACT_NOTE:
1265         ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1266         break;
1267
1268     case OFPACT_EXIT:
1269         ofputil_put_NXAST_EXIT(out);
1270         break;
1271
1272     case OFPACT_OUTPUT:
1273     case OFPACT_ENQUEUE:
1274     case OFPACT_SET_VLAN_VID:
1275     case OFPACT_SET_VLAN_PCP:
1276     case OFPACT_STRIP_VLAN:
1277     case OFPACT_SET_ETH_SRC:
1278     case OFPACT_SET_ETH_DST:
1279     case OFPACT_SET_IPV4_SRC:
1280     case OFPACT_SET_IPV4_DST:
1281     case OFPACT_SET_IPV4_DSCP:
1282     case OFPACT_SET_L4_SRC_PORT:
1283     case OFPACT_SET_L4_DST_PORT:
1284     case OFPACT_CLEAR_ACTIONS:
1285     case OFPACT_GOTO_TABLE:
1286         NOT_REACHED();
1287     }
1288 }
1289 \f
1290 /* Converting ofpacts to OpenFlow 1.0. */
1291
1292 static void
1293 ofpact_output_to_openflow10(const struct ofpact_output *output,
1294                             struct ofpbuf *out)
1295 {
1296     struct ofp10_action_output *oao;
1297
1298     oao = ofputil_put_OFPAT10_OUTPUT(out);
1299     oao->port = htons(output->port);
1300     oao->max_len = htons(output->max_len);
1301 }
1302
1303 static void
1304 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1305                              struct ofpbuf *out)
1306 {
1307     struct ofp_action_enqueue *oae;
1308
1309     oae = ofputil_put_OFPAT10_ENQUEUE(out);
1310     oae->port = htons(enqueue->port);
1311     oae->queue_id = htonl(enqueue->queue);
1312 }
1313
1314 static void
1315 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1316 {
1317     switch (a->type) {
1318     case OFPACT_OUTPUT:
1319         ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1320         break;
1321
1322     case OFPACT_ENQUEUE:
1323         ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1324         break;
1325
1326     case OFPACT_SET_VLAN_VID:
1327         ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1328             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1329         break;
1330
1331     case OFPACT_SET_VLAN_PCP:
1332         ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1333             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1334         break;
1335
1336     case OFPACT_STRIP_VLAN:
1337         ofputil_put_OFPAT10_STRIP_VLAN(out);
1338         break;
1339
1340     case OFPACT_SET_ETH_SRC:
1341         memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1342                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1343         break;
1344
1345     case OFPACT_SET_ETH_DST:
1346         memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1347                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1348         break;
1349
1350     case OFPACT_SET_IPV4_SRC:
1351         ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1352             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1353         break;
1354
1355     case OFPACT_SET_IPV4_DST:
1356         ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1357             = ofpact_get_SET_IPV4_DST(a)->ipv4;
1358         break;
1359
1360     case OFPACT_SET_IPV4_DSCP:
1361         ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1362             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1363         break;
1364
1365     case OFPACT_SET_L4_SRC_PORT:
1366         ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1367             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1368         break;
1369
1370     case OFPACT_SET_L4_DST_PORT:
1371         ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1372             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1373         break;
1374
1375     case OFPACT_CLEAR_ACTIONS:
1376     case OFPACT_GOTO_TABLE:
1377         /* TODO:XXX */
1378         break;
1379
1380     case OFPACT_CONTROLLER:
1381     case OFPACT_OUTPUT_REG:
1382     case OFPACT_BUNDLE:
1383     case OFPACT_REG_MOVE:
1384     case OFPACT_REG_LOAD:
1385     case OFPACT_DEC_TTL:
1386     case OFPACT_SET_TUNNEL:
1387     case OFPACT_SET_QUEUE:
1388     case OFPACT_POP_QUEUE:
1389     case OFPACT_FIN_TIMEOUT:
1390     case OFPACT_RESUBMIT:
1391     case OFPACT_LEARN:
1392     case OFPACT_MULTIPATH:
1393     case OFPACT_AUTOPATH:
1394     case OFPACT_NOTE:
1395     case OFPACT_EXIT:
1396         ofpact_to_nxast(a, out);
1397         break;
1398     }
1399 }
1400
1401 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1402  * actions in 'openflow', appending the actions to any existing data in
1403  * 'openflow'. */
1404 void
1405 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1406                        struct ofpbuf *openflow)
1407 {
1408     const struct ofpact *a;
1409
1410     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1411         ofpact_to_openflow10(a, openflow);
1412     }
1413 }
1414 \f
1415 /* Converting ofpacts to OpenFlow 1.1. */
1416
1417 static void
1418 ofpact_output_to_openflow11(const struct ofpact_output *output,
1419                             struct ofpbuf *out)
1420 {
1421     struct ofp11_action_output *oao;
1422
1423     oao = ofputil_put_OFPAT11_OUTPUT(out);
1424     oao->port = ofputil_port_to_ofp11(output->port);
1425     oao->max_len = htons(output->max_len);
1426 }
1427
1428 static void
1429 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
1430                              struct ofpbuf *out)
1431 {
1432     if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
1433         && (!dec_ttl->ofpact.compat ||
1434             dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
1435         ofputil_put_OFPAT11_DEC_NW_TTL(out);
1436     } else {
1437         ofpact_dec_ttl_to_nxast(dec_ttl, out);
1438     }
1439 }
1440
1441 static void
1442 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1443 {
1444     switch (a->type) {
1445     case OFPACT_OUTPUT:
1446         return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1447
1448     case OFPACT_ENQUEUE:
1449         /* XXX */
1450         break;
1451
1452     case OFPACT_SET_VLAN_VID:
1453         ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1454             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1455         break;
1456
1457     case OFPACT_SET_VLAN_PCP:
1458         ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1459             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1460         break;
1461
1462     case OFPACT_STRIP_VLAN:
1463         ofputil_put_OFPAT11_POP_VLAN(out);
1464         break;
1465
1466     case OFPACT_SET_ETH_SRC:
1467         memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1468                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1469         break;
1470
1471     case OFPACT_SET_ETH_DST:
1472         memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1473                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1474         break;
1475
1476     case OFPACT_SET_IPV4_SRC:
1477         ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1478             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1479         break;
1480
1481     case OFPACT_SET_IPV4_DST:
1482         ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1483             = ofpact_get_SET_IPV4_DST(a)->ipv4;
1484         break;
1485
1486     case OFPACT_SET_IPV4_DSCP:
1487         ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1488             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1489         break;
1490
1491     case OFPACT_SET_L4_SRC_PORT:
1492         ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1493             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1494         break;
1495
1496     case OFPACT_SET_L4_DST_PORT:
1497         ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1498             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1499         break;
1500
1501     case OFPACT_DEC_TTL:
1502         ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
1503         break;
1504
1505     case OFPACT_CLEAR_ACTIONS:
1506     case OFPACT_GOTO_TABLE:
1507         NOT_REACHED();
1508
1509     case OFPACT_CONTROLLER:
1510     case OFPACT_OUTPUT_REG:
1511     case OFPACT_BUNDLE:
1512     case OFPACT_REG_MOVE:
1513     case OFPACT_REG_LOAD:
1514     case OFPACT_SET_TUNNEL:
1515     case OFPACT_SET_QUEUE:
1516     case OFPACT_POP_QUEUE:
1517     case OFPACT_FIN_TIMEOUT:
1518     case OFPACT_RESUBMIT:
1519     case OFPACT_LEARN:
1520     case OFPACT_MULTIPATH:
1521     case OFPACT_AUTOPATH:
1522     case OFPACT_NOTE:
1523     case OFPACT_EXIT:
1524         ofpact_to_nxast(a, out);
1525         break;
1526     }
1527 }
1528
1529 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1530  * 1.1 actions in 'openflow', appending the actions to any existing data in
1531  * 'openflow'. */
1532 size_t
1533 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1534                                size_t ofpacts_len, struct ofpbuf *openflow)
1535 {
1536     const struct ofpact *a;
1537     size_t start_size = openflow->size;
1538
1539     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1540         ofpact_to_openflow11(a, openflow);
1541     }
1542
1543     return openflow->size - start_size;
1544 }
1545
1546 static void
1547 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
1548 {
1549     struct ofp11_instruction_actions *oia;
1550
1551     /* Update the instruction's length (or, if it's empty, delete it). */
1552     oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1553     if (openflow->size > ofs + sizeof *oia) {
1554         oia->len = htons(openflow->size - ofs);
1555     } else {
1556         openflow->size = ofs;
1557     }
1558 }
1559
1560 void
1561 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1562                                     size_t ofpacts_len,
1563                                     struct ofpbuf *openflow)
1564 {
1565     const struct ofpact *a;
1566
1567     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1568         /* TODO:XXX Write-Actions */
1569         /* TODO:XXX Write-Metadata */
1570         if (a->type == OFPACT_CLEAR_ACTIONS) {
1571             instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
1572         } else if (a->type == OFPACT_GOTO_TABLE) {
1573             struct ofp11_instruction_goto_table *oigt;
1574
1575             oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1576             oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1577             memset(oigt->pad, 0, sizeof oigt->pad);
1578         } else if (!ofpact_is_instruction(a)) {
1579             /* Apply-actions */
1580             const size_t ofs = openflow->size;
1581             const size_t ofpacts_len_left =
1582                 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1583             const struct ofpact *action;
1584             const struct ofpact *processed = a;
1585
1586             instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1587             OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1588                 if (ofpact_is_instruction(action)) {
1589                     break;
1590                 }
1591                 ofpact_to_openflow11(action, openflow);
1592                 processed = action;
1593             }
1594             ofpacts_update_instruction_actions(openflow, ofs);
1595             a = processed;
1596         }
1597     }
1598 }
1599 \f
1600 /* Returns true if 'action' outputs to 'port', false otherwise. */
1601 static bool
1602 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1603 {
1604     switch (ofpact->type) {
1605     case OFPACT_OUTPUT:
1606         return ofpact_get_OUTPUT(ofpact)->port == port;
1607     case OFPACT_ENQUEUE:
1608         return ofpact_get_ENQUEUE(ofpact)->port == port;
1609     case OFPACT_CONTROLLER:
1610         return port == OFPP_CONTROLLER;
1611
1612     case OFPACT_OUTPUT_REG:
1613     case OFPACT_BUNDLE:
1614     case OFPACT_SET_VLAN_VID:
1615     case OFPACT_SET_VLAN_PCP:
1616     case OFPACT_STRIP_VLAN:
1617     case OFPACT_SET_ETH_SRC:
1618     case OFPACT_SET_ETH_DST:
1619     case OFPACT_SET_IPV4_SRC:
1620     case OFPACT_SET_IPV4_DST:
1621     case OFPACT_SET_IPV4_DSCP:
1622     case OFPACT_SET_L4_SRC_PORT:
1623     case OFPACT_SET_L4_DST_PORT:
1624     case OFPACT_REG_MOVE:
1625     case OFPACT_REG_LOAD:
1626     case OFPACT_DEC_TTL:
1627     case OFPACT_SET_TUNNEL:
1628     case OFPACT_SET_QUEUE:
1629     case OFPACT_POP_QUEUE:
1630     case OFPACT_FIN_TIMEOUT:
1631     case OFPACT_RESUBMIT:
1632     case OFPACT_LEARN:
1633     case OFPACT_MULTIPATH:
1634     case OFPACT_AUTOPATH:
1635     case OFPACT_NOTE:
1636     case OFPACT_EXIT:
1637     case OFPACT_CLEAR_ACTIONS:
1638     case OFPACT_GOTO_TABLE:
1639     default:
1640         return false;
1641     }
1642 }
1643
1644 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1645  * to 'port', false otherwise. */
1646 bool
1647 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1648                        uint16_t port)
1649 {
1650     const struct ofpact *a;
1651
1652     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1653         if (ofpact_outputs_to_port(a, port)) {
1654             return true;
1655         }
1656     }
1657
1658     return false;
1659 }
1660
1661 bool
1662 ofpacts_equal(const struct ofpact *a, size_t a_len,
1663               const struct ofpact *b, size_t b_len)
1664 {
1665     return a_len == b_len && !memcmp(a, b, a_len);
1666 }
1667 \f
1668 /* Formatting ofpacts. */
1669
1670 static void
1671 print_note(const struct ofpact_note *note, struct ds *string)
1672 {
1673     size_t i;
1674
1675     ds_put_cstr(string, "note:");
1676     for (i = 0; i < note->length; i++) {
1677         if (i) {
1678             ds_put_char(string, '.');
1679         }
1680         ds_put_format(string, "%02"PRIx8, note->data[i]);
1681     }
1682 }
1683
1684 static void
1685 print_dec_ttl(const struct ofpact_cnt_ids *ids,
1686               struct ds *s)
1687 {
1688     size_t i;
1689
1690     ds_put_cstr(s, "dec_ttl");
1691     if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1692         ds_put_cstr(s, "(");
1693         for (i = 0; i < ids->n_controllers; i++) {
1694             if (i) {
1695                 ds_put_cstr(s, ",");
1696             }
1697             ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1698         }
1699         ds_put_cstr(s, ")");
1700     }
1701 }
1702
1703 static void
1704 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1705                   struct ds *s)
1706 {
1707     ds_put_cstr(s, "fin_timeout(");
1708     if (fin_timeout->fin_idle_timeout) {
1709         ds_put_format(s, "idle_timeout=%"PRIu16",",
1710                       fin_timeout->fin_idle_timeout);
1711     }
1712     if (fin_timeout->fin_hard_timeout) {
1713         ds_put_format(s, "hard_timeout=%"PRIu16",",
1714                       fin_timeout->fin_hard_timeout);
1715     }
1716     ds_chomp(s, ',');
1717     ds_put_char(s, ')');
1718 }
1719
1720 static void
1721 ofpact_format(const struct ofpact *a, struct ds *s)
1722 {
1723     const struct ofpact_enqueue *enqueue;
1724     const struct ofpact_resubmit *resubmit;
1725     const struct ofpact_autopath *autopath;
1726     const struct ofpact_controller *controller;
1727     const struct ofpact_tunnel *tunnel;
1728     uint16_t port;
1729
1730     switch (a->type) {
1731     case OFPACT_OUTPUT:
1732         port = ofpact_get_OUTPUT(a)->port;
1733         if (port < OFPP_MAX) {
1734             ds_put_format(s, "output:%"PRIu16, port);
1735         } else {
1736             ofputil_format_port(port, s);
1737             if (port == OFPP_CONTROLLER) {
1738                 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1739             }
1740         }
1741         break;
1742
1743     case OFPACT_CONTROLLER:
1744         controller = ofpact_get_CONTROLLER(a);
1745         if (controller->reason == OFPR_ACTION &&
1746             controller->controller_id == 0) {
1747             ds_put_format(s, "CONTROLLER:%"PRIu16,
1748                           ofpact_get_CONTROLLER(a)->max_len);
1749         } else {
1750             enum ofp_packet_in_reason reason = controller->reason;
1751
1752             ds_put_cstr(s, "controller(");
1753             if (reason != OFPR_ACTION) {
1754                 ds_put_format(s, "reason=%s,",
1755                               ofputil_packet_in_reason_to_string(reason));
1756             }
1757             if (controller->max_len != UINT16_MAX) {
1758                 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1759             }
1760             if (controller->controller_id != 0) {
1761                 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1762             }
1763             ds_chomp(s, ',');
1764             ds_put_char(s, ')');
1765         }
1766         break;
1767
1768     case OFPACT_ENQUEUE:
1769         enqueue = ofpact_get_ENQUEUE(a);
1770         ds_put_format(s, "enqueue:");
1771         ofputil_format_port(enqueue->port, s);
1772         ds_put_format(s, "q%"PRIu32, enqueue->queue);
1773         break;
1774
1775     case OFPACT_OUTPUT_REG:
1776         ds_put_cstr(s, "output:");
1777         mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1778         break;
1779
1780     case OFPACT_BUNDLE:
1781         bundle_format(ofpact_get_BUNDLE(a), s);
1782         break;
1783
1784     case OFPACT_SET_VLAN_VID:
1785         ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1786                       ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1787         break;
1788
1789     case OFPACT_SET_VLAN_PCP:
1790         ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1791                       ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1792         break;
1793
1794     case OFPACT_STRIP_VLAN:
1795         ds_put_cstr(s, "strip_vlan");
1796         break;
1797
1798     case OFPACT_SET_ETH_SRC:
1799         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1800                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1801         break;
1802
1803     case OFPACT_SET_ETH_DST:
1804         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1805                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1806         break;
1807
1808     case OFPACT_SET_IPV4_SRC:
1809         ds_put_format(s, "mod_nw_src:"IP_FMT,
1810                       IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1811         break;
1812
1813     case OFPACT_SET_IPV4_DST:
1814         ds_put_format(s, "mod_nw_dst:"IP_FMT,
1815                       IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1816         break;
1817
1818     case OFPACT_SET_IPV4_DSCP:
1819         ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1820         break;
1821
1822     case OFPACT_SET_L4_SRC_PORT:
1823         ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1824         break;
1825
1826     case OFPACT_SET_L4_DST_PORT:
1827         ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1828         break;
1829
1830     case OFPACT_REG_MOVE:
1831         nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1832         break;
1833
1834     case OFPACT_REG_LOAD:
1835         nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1836         break;
1837
1838     case OFPACT_DEC_TTL:
1839         print_dec_ttl(ofpact_get_DEC_TTL(a), s);
1840         break;
1841
1842     case OFPACT_SET_TUNNEL:
1843         tunnel = ofpact_get_SET_TUNNEL(a);
1844         ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1845                       (tunnel->tun_id > UINT32_MAX
1846                        || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1847                       tunnel->tun_id);
1848         break;
1849
1850     case OFPACT_SET_QUEUE:
1851         ds_put_format(s, "set_queue:%"PRIu32,
1852                       ofpact_get_SET_QUEUE(a)->queue_id);
1853         break;
1854
1855     case OFPACT_POP_QUEUE:
1856         ds_put_cstr(s, "pop_queue");
1857         break;
1858
1859     case OFPACT_FIN_TIMEOUT:
1860         print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1861         break;
1862
1863     case OFPACT_RESUBMIT:
1864         resubmit = ofpact_get_RESUBMIT(a);
1865         if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
1866             ds_put_cstr(s, "resubmit:");
1867             ofputil_format_port(resubmit->in_port, s);
1868         } else {
1869             ds_put_format(s, "resubmit(");
1870             if (resubmit->in_port != OFPP_IN_PORT) {
1871                 ofputil_format_port(resubmit->in_port, s);
1872             }
1873             ds_put_char(s, ',');
1874             if (resubmit->table_id != 255) {
1875                 ds_put_format(s, "%"PRIu8, resubmit->table_id);
1876             }
1877             ds_put_char(s, ')');
1878         }
1879         break;
1880
1881     case OFPACT_LEARN:
1882         learn_format(ofpact_get_LEARN(a), s);
1883         break;
1884
1885     case OFPACT_MULTIPATH:
1886         multipath_format(ofpact_get_MULTIPATH(a), s);
1887         break;
1888
1889     case OFPACT_AUTOPATH:
1890         autopath = ofpact_get_AUTOPATH(a);
1891         ds_put_cstr(s, "autopath(");
1892         ofputil_format_port(autopath->port, s);
1893         ds_put_char(s, ',');
1894         mf_format_subfield(&autopath->dst, s);
1895         ds_put_char(s, ')');
1896         break;
1897
1898     case OFPACT_NOTE:
1899         print_note(ofpact_get_NOTE(a), s);
1900         break;
1901
1902     case OFPACT_EXIT:
1903         ds_put_cstr(s, "exit");
1904         break;
1905
1906     case OFPACT_CLEAR_ACTIONS:
1907         ds_put_format(s, "%s",
1908                       ofpact_instruction_name_from_type(
1909                           OVSINST_OFPIT11_CLEAR_ACTIONS));
1910         break;
1911
1912     case OFPACT_GOTO_TABLE:
1913         ds_put_format(s, "%s:%"PRIu8,
1914                       ofpact_instruction_name_from_type(
1915                           OVSINST_OFPIT11_GOTO_TABLE),
1916                       ofpact_get_GOTO_TABLE(a)->table_id);
1917         break;
1918     }
1919 }
1920
1921 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
1922  * 'ofpacts' to 'string'. */
1923 void
1924 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
1925                struct ds *string)
1926 {
1927     ds_put_cstr(string, "actions=");
1928     if (!ofpacts_len) {
1929         ds_put_cstr(string, "drop");
1930     } else {
1931         const struct ofpact *a;
1932
1933         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1934             if (a != ofpacts) {
1935                 ds_put_cstr(string, ",");
1936             }
1937
1938             /* TODO:XXX write-actions */
1939             /* TODO:XXX write-metadata */
1940             ofpact_format(a, string);
1941         }
1942     }
1943 }
1944 \f
1945 /* Internal use by helpers. */
1946
1947 void *
1948 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
1949 {
1950     struct ofpact *ofpact;
1951
1952     ofpact_pad(ofpacts);
1953     ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
1954     ofpact_init(ofpact, type, len);
1955     return ofpact;
1956 }
1957
1958 void
1959 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
1960 {
1961     memset(ofpact, 0, len);
1962     ofpact->type = type;
1963     ofpact->compat = OFPUTIL_ACTION_INVALID;
1964     ofpact->len = len;
1965 }
1966 \f
1967 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
1968  * starting at 'ofpact'.
1969  *
1970  * This is the correct way to update a variable-length ofpact's length after
1971  * adding the variable-length part of the payload.  (See the large comment
1972  * near the end of ofp-actions.h for more information.) */
1973 void
1974 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
1975 {
1976     assert(ofpact == ofpacts->l2);
1977     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
1978 }
1979
1980 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
1981  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
1982  * client must call this itself after adding the final ofpact to an array of
1983  * them.
1984  *
1985  * (The consequences of failing to call this function are probably not dire.
1986  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
1987  * not dereference it.  That's undefined behavior, technically, but it will not
1988  * cause a real problem on common systems.  Still, it seems better to call
1989  * it.) */
1990 void
1991 ofpact_pad(struct ofpbuf *ofpacts)
1992 {
1993     unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
1994     if (rem) {
1995         ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
1996     }
1997 }
1998
1999 void
2000 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2001                       const void *src)
2002 {
2003     load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2004     load->dst.field = mf;
2005     load->dst.ofs = 0;
2006     load->dst.n_bits = mf->n_bits;
2007     bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2008                  &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);
2009 }