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