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