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