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