ofp-actions: Fix some ofpbuf usage problems in write-actions code.
[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 = ALIGNED_CAST(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             ALIGNED_CAST(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 = ALIGNED_CAST(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(
406             ALIGNED_CAST(const struct nx_action_learn *, a), out);
407         break;
408
409     case OFPUTIL_NXAST_EXIT:
410         ofpact_put_EXIT(out);
411         break;
412
413     case OFPUTIL_NXAST_DEC_TTL:
414         error = dec_ttl_from_openflow(out, code);
415         break;
416
417     case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
418         error = dec_ttl_cnt_ids_from_openflow(
419                     (const struct nx_action_cnt_ids *) a, out);
420         break;
421
422     case OFPUTIL_NXAST_FIN_TIMEOUT:
423         fin_timeout_from_openflow(
424             (const struct nx_action_fin_timeout *) a, out);
425         break;
426
427     case OFPUTIL_NXAST_CONTROLLER:
428         controller_from_openflow((const struct nx_action_controller *) a, out);
429         break;
430
431     case OFPUTIL_NXAST_PUSH_MPLS: {
432         struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
433         if (!eth_type_mpls(nxapm->ethertype)) {
434             return OFPERR_OFPBAC_BAD_ARGUMENT;
435         }
436         ofpact_put_PUSH_MPLS(out)->ethertype = nxapm->ethertype;
437         break;
438     }
439
440     case OFPUTIL_NXAST_SET_MPLS_TTL: {
441         struct nx_action_mpls_ttl *nxamt = (struct nx_action_mpls_ttl *)a;
442         ofpact_put_SET_MPLS_TTL(out)->ttl = nxamt->ttl;
443         break;
444     }
445
446     case OFPUTIL_NXAST_DEC_MPLS_TTL:
447         ofpact_put_DEC_MPLS_TTL(out);
448         break;
449
450     case OFPUTIL_NXAST_POP_MPLS: {
451         struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
452         if (eth_type_mpls(nxapm->ethertype)) {
453             return OFPERR_OFPBAC_BAD_ARGUMENT;
454         }
455         ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
456         break;
457     }
458
459     case OFPUTIL_NXAST_SAMPLE:
460         error = sample_from_openflow(
461             (const struct nx_action_sample *) a, out);
462         break;
463     }
464
465     return error;
466 }
467
468 static enum ofperr
469 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
470 {
471     enum ofputil_action_code code;
472     enum ofperr error;
473
474     error = decode_openflow10_action(a, &code);
475     if (error) {
476         return error;
477     }
478
479     switch (code) {
480     case OFPUTIL_ACTION_INVALID:
481 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
482 #include "ofp-util.def"
483         NOT_REACHED();
484
485     case OFPUTIL_OFPAT10_OUTPUT:
486         return output_from_openflow10(&a->output10, out);
487
488     case OFPUTIL_OFPAT10_SET_VLAN_VID:
489         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
490             return OFPERR_OFPBAC_BAD_ARGUMENT;
491         }
492         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
493         break;
494
495     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
496         if (a->vlan_pcp.vlan_pcp & ~7) {
497             return OFPERR_OFPBAC_BAD_ARGUMENT;
498         }
499         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
500         break;
501
502     case OFPUTIL_OFPAT10_STRIP_VLAN:
503         ofpact_put_STRIP_VLAN(out);
504         break;
505
506     case OFPUTIL_OFPAT10_SET_DL_SRC:
507         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
508                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
509         break;
510
511     case OFPUTIL_OFPAT10_SET_DL_DST:
512         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
513                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
514         break;
515
516     case OFPUTIL_OFPAT10_SET_NW_SRC:
517         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
518         break;
519
520     case OFPUTIL_OFPAT10_SET_NW_DST:
521         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
522         break;
523
524     case OFPUTIL_OFPAT10_SET_NW_TOS:
525         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
526             return OFPERR_OFPBAC_BAD_ARGUMENT;
527         }
528         ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
529         break;
530
531     case OFPUTIL_OFPAT10_SET_TP_SRC:
532         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
533         break;
534
535     case OFPUTIL_OFPAT10_SET_TP_DST:
536         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
537
538         break;
539
540     case OFPUTIL_OFPAT10_ENQUEUE:
541         error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
542                                         out);
543         break;
544
545 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
546 #include "ofp-util.def"
547         return ofpact_from_nxast(a, code, out);
548     }
549
550     return error;
551 }
552
553 static inline union ofp_action *
554 action_next(const union ofp_action *a)
555 {
556     return ((union ofp_action *) (void *)
557             ((uint8_t *) a + ntohs(a->header.len)));
558 }
559
560 static inline bool
561 action_is_valid(const union ofp_action *a, size_t n_actions)
562 {
563     uint16_t len = ntohs(a->header.len);
564     return (!(len % OFP_ACTION_ALIGN)
565             && len >= sizeof *a
566             && len / sizeof *a <= n_actions);
567 }
568
569 /* This macro is careful to check for actions with bad lengths. */
570 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS)                 \
571     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
572          (LEFT) > 0 && action_is_valid(ITER, LEFT);                     \
573          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
574           (ITER) = action_next(ITER)))
575
576 static void
577 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
578                enum ofperr error)
579 {
580     if (!VLOG_DROP_WARN(&rl)) {
581         struct ds s;
582
583         ds_init(&s);
584         ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
585         VLOG_WARN("bad action at offset %#zx (%s):\n%s",
586                   ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
587         ds_destroy(&s);
588     }
589 }
590
591 static enum ofperr
592 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
593                       struct ofpbuf *out,
594                       enum ofperr (*ofpact_from_openflow)(
595                           const union ofp_action *a, struct ofpbuf *out))
596 {
597     const union ofp_action *a;
598     size_t left;
599
600     ACTION_FOR_EACH (a, left, in, n_in) {
601         enum ofperr error = ofpact_from_openflow(a, out);
602         if (error) {
603             log_bad_action(in, n_in, a - in, error);
604             return error;
605         }
606     }
607     if (left) {
608         enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
609         log_bad_action(in, n_in, n_in - left, error);
610         return error;
611     }
612
613     ofpact_pad(out);
614     return 0;
615 }
616
617 static enum ofperr
618 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
619                         struct ofpbuf *out)
620 {
621     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
622 }
623
624 static enum ofperr
625 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
626                      struct ofpbuf *ofpacts,
627                      enum ofperr (*translate)(const union ofp_action *actions,
628                                               size_t n_actions,
629                                               struct ofpbuf *ofpacts))
630 {
631     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
632     const union ofp_action *actions;
633     enum ofperr error;
634
635     ofpbuf_clear(ofpacts);
636
637     if (actions_len % OFP_ACTION_ALIGN != 0) {
638         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
639                      "multiple of %d", actions_len, OFP_ACTION_ALIGN);
640         return OFPERR_OFPBRC_BAD_LEN;
641     }
642
643     actions = ofpbuf_try_pull(openflow, actions_len);
644     if (actions == NULL) {
645         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
646                      "remaining message length (%zu)",
647                      actions_len, openflow->size);
648         return OFPERR_OFPBRC_BAD_LEN;
649     }
650
651     error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
652     if (error) {
653         ofpbuf_clear(ofpacts);
654         return error;
655     }
656
657     error = ofpacts_verify(ofpacts->data, ofpacts->size);
658     if (error) {
659         ofpbuf_clear(ofpacts);
660     }
661     return error;
662 }
663
664 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
665  * front of 'openflow' into ofpacts.  On success, replaces any existing content
666  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
667  * Returns 0 if successful, otherwise an OpenFlow error.
668  *
669  * The parsed actions are valid generically, but they may not be valid in a
670  * specific context.  For example, port numbers up to OFPP_MAX are valid
671  * generically, but specific datapaths may only support port numbers in a
672  * smaller range.  Use ofpacts_check() to additional check whether actions are
673  * valid in a specific context. */
674 enum ofperr
675 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
676                         struct ofpbuf *ofpacts)
677 {
678     return ofpacts_pull_actions(openflow, actions_len, ofpacts,
679                                 ofpacts_from_openflow10);
680 }
681 \f
682 /* OpenFlow 1.1 actions. */
683
684 /* Parses 'a' to determine its type.  On success stores the correct type into
685  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
686  * '*code' is indeterminate.
687  *
688  * The caller must have already verified that 'a''s length is potentially
689  * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
690  * ofp_action) and no longer than the amount of space allocated to 'a').
691  *
692  * This function verifies that 'a''s length is correct for the type of action
693  * that it represents. */
694 static enum ofperr
695 decode_openflow11_action(const union ofp_action *a,
696                          enum ofputil_action_code *code)
697 {
698     uint16_t len;
699
700     switch (a->type) {
701     case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
702         return decode_nxast_action(a, code);
703
704 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
705         case CONSTANT_HTONS(ENUM):                      \
706             len = ntohs(a->header.len);                 \
707             if (EXTENSIBLE                              \
708                 ? len >= sizeof(struct STRUCT)          \
709                 : len == sizeof(struct STRUCT)) {       \
710                 *code = OFPUTIL_##ENUM;                 \
711                 return 0;                               \
712             } else {                                    \
713                 return OFPERR_OFPBAC_BAD_LEN;           \
714             }                                           \
715             NOT_REACHED();
716 #include "ofp-util.def"
717
718     default:
719         return OFPERR_OFPBAC_BAD_TYPE;
720     }
721 }
722
723 static enum ofperr
724 output_from_openflow11(const struct ofp11_action_output *oao,
725                        struct ofpbuf *out)
726 {
727     struct ofpact_output *output;
728     enum ofperr error;
729
730     output = ofpact_put_OUTPUT(out);
731     output->max_len = ntohs(oao->max_len);
732
733     error = ofputil_port_from_ofp11(oao->port, &output->port);
734     if (error) {
735         return error;
736     }
737
738     return ofputil_check_output_port(output->port, OFPP_MAX);
739 }
740
741 static enum ofperr
742 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
743 {
744     enum ofputil_action_code code;
745     enum ofperr error;
746
747     error = decode_openflow11_action(a, &code);
748     if (error) {
749         return error;
750     }
751
752     switch (code) {
753     case OFPUTIL_ACTION_INVALID:
754 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
755 #include "ofp-util.def"
756         NOT_REACHED();
757
758     case OFPUTIL_OFPAT11_OUTPUT:
759         return output_from_openflow11((const struct ofp11_action_output *) a,
760                                       out);
761
762     case OFPUTIL_OFPAT11_SET_VLAN_VID:
763         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
764             return OFPERR_OFPBAC_BAD_ARGUMENT;
765         }
766         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
767         break;
768
769     case OFPUTIL_OFPAT11_SET_VLAN_PCP:
770         if (a->vlan_pcp.vlan_pcp & ~7) {
771             return OFPERR_OFPBAC_BAD_ARGUMENT;
772         }
773         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
774         break;
775
776     case OFPUTIL_OFPAT11_PUSH_VLAN:
777         if (((const struct ofp11_action_push *)a)->ethertype !=
778             htons(ETH_TYPE_VLAN_8021Q)) {
779             /* XXX 802.1AD(QinQ) isn't supported at the moment */
780             return OFPERR_OFPBAC_BAD_ARGUMENT;
781         }
782         ofpact_put_PUSH_VLAN(out);
783         break;
784
785     case OFPUTIL_OFPAT11_POP_VLAN:
786         ofpact_put_STRIP_VLAN(out);
787         break;
788
789     case OFPUTIL_OFPAT11_SET_QUEUE:
790         ofpact_put_SET_QUEUE(out)->queue_id =
791             ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
792         break;
793
794     case OFPUTIL_OFPAT11_SET_DL_SRC:
795         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
796                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
797         break;
798
799     case OFPUTIL_OFPAT11_SET_DL_DST:
800         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
801                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
802         break;
803
804     case OFPUTIL_OFPAT11_DEC_NW_TTL:
805         dec_ttl_from_openflow(out, code);
806         break;
807
808     case OFPUTIL_OFPAT11_SET_NW_SRC:
809         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
810         break;
811
812     case OFPUTIL_OFPAT11_SET_NW_DST:
813         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
814         break;
815
816     case OFPUTIL_OFPAT11_SET_NW_TOS:
817         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
818             return OFPERR_OFPBAC_BAD_ARGUMENT;
819         }
820         ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
821         break;
822
823     case OFPUTIL_OFPAT11_SET_TP_SRC:
824         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
825         break;
826
827     case OFPUTIL_OFPAT11_SET_TP_DST:
828         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
829         break;
830
831     case OFPUTIL_OFPAT12_SET_FIELD:
832         return nxm_reg_load_from_openflow12_set_field(
833             (const struct ofp12_action_set_field *)a, out);
834
835     case OFPUTIL_OFPAT11_SET_MPLS_TTL: {
836         struct ofp11_action_mpls_ttl *oamt = (struct ofp11_action_mpls_ttl *)a;
837         ofpact_put_SET_MPLS_TTL(out)->ttl = oamt->mpls_ttl;
838         break;
839     }
840
841     case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
842         ofpact_put_DEC_MPLS_TTL(out);
843         break;
844
845     case OFPUTIL_OFPAT11_PUSH_MPLS: {
846         struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
847         if (!eth_type_mpls(oap->ethertype)) {
848             return OFPERR_OFPBAC_BAD_ARGUMENT;
849         }
850         ofpact_put_PUSH_MPLS(out)->ethertype = oap->ethertype;
851         break;
852     }
853
854     case OFPUTIL_OFPAT11_POP_MPLS: {
855         struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
856         if (eth_type_mpls(oapm->ethertype)) {
857             return OFPERR_OFPBAC_BAD_ARGUMENT;
858         }
859         ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
860         break;
861     }
862
863     case OFPUTIL_OFPAT11_GROUP: {
864         struct ofp11_action_group *oag = (struct ofp11_action_group *)a;
865         ofpact_put_GROUP(out)->group_id = ntohl(oag->group_id);
866         break;
867     }
868
869 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
870 #include "ofp-util.def"
871         return ofpact_from_nxast(a, code, out);
872     }
873
874     return error;
875 }
876
877 static enum ofperr
878 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
879                         struct ofpbuf *out)
880 {
881     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
882 }
883
884 /* True if an action sets the value of a field
885  * in a way that is compatibile with the action set.
886  * False otherwise. */
887 static bool
888 ofpact_is_set_action(const struct ofpact *a)
889 {
890     switch (a->type) {
891     case OFPACT_REG_LOAD:
892     case OFPACT_SET_ETH_DST:
893     case OFPACT_SET_ETH_SRC:
894     case OFPACT_SET_IPV4_DSCP:
895     case OFPACT_SET_IPV4_DST:
896     case OFPACT_SET_IPV4_SRC:
897     case OFPACT_SET_L4_DST_PORT:
898     case OFPACT_SET_L4_SRC_PORT:
899     case OFPACT_SET_MPLS_TTL:
900     case OFPACT_SET_QUEUE:
901     case OFPACT_SET_TUNNEL:
902     case OFPACT_SET_VLAN_PCP:
903     case OFPACT_SET_VLAN_VID:
904         return true;
905     case OFPACT_BUNDLE:
906     case OFPACT_CLEAR_ACTIONS:
907     case OFPACT_CONTROLLER:
908     case OFPACT_DEC_MPLS_TTL:
909     case OFPACT_DEC_TTL:
910     case OFPACT_ENQUEUE:
911     case OFPACT_EXIT:
912     case OFPACT_FIN_TIMEOUT:
913     case OFPACT_GOTO_TABLE:
914     case OFPACT_GROUP:
915     case OFPACT_LEARN:
916     case OFPACT_METER:
917     case OFPACT_MULTIPATH:
918     case OFPACT_NOTE:
919     case OFPACT_OUTPUT:
920     case OFPACT_OUTPUT_REG:
921     case OFPACT_POP_MPLS:
922     case OFPACT_POP_QUEUE:
923     case OFPACT_PUSH_MPLS:
924     case OFPACT_PUSH_VLAN:
925     case OFPACT_REG_MOVE:
926     case OFPACT_RESUBMIT:
927     case OFPACT_SAMPLE:
928     case OFPACT_STACK_POP:
929     case OFPACT_STACK_PUSH:
930     case OFPACT_STRIP_VLAN:
931     case OFPACT_WRITE_ACTIONS:
932     case OFPACT_WRITE_METADATA:
933         return false;
934     default:
935         NOT_REACHED();
936     }
937 }
938
939 /* True if an action is allowed in the action set.
940  * False otherwise. */
941 static bool
942 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
943 {
944     switch (a->type) {
945     case OFPACT_DEC_MPLS_TTL:
946     case OFPACT_DEC_TTL:
947     case OFPACT_GROUP:
948     case OFPACT_OUTPUT:
949     case OFPACT_POP_MPLS:
950     case OFPACT_PUSH_MPLS:
951     case OFPACT_PUSH_VLAN:
952     case OFPACT_REG_LOAD:
953     case OFPACT_SET_ETH_DST:
954     case OFPACT_SET_ETH_SRC:
955     case OFPACT_SET_IPV4_DSCP:
956     case OFPACT_SET_IPV4_DST:
957     case OFPACT_SET_IPV4_SRC:
958     case OFPACT_SET_L4_DST_PORT:
959     case OFPACT_SET_L4_SRC_PORT:
960     case OFPACT_SET_MPLS_TTL:
961     case OFPACT_SET_QUEUE:
962     case OFPACT_SET_TUNNEL:
963     case OFPACT_SET_VLAN_PCP:
964     case OFPACT_SET_VLAN_VID:
965     case OFPACT_STRIP_VLAN:
966         return true;
967
968     /* In general these actions are excluded because they are not part of
969      * the OpenFlow specification nor map to actions that are defined in
970      * the specification.  Thus the order in which they should be applied
971      * in the action set is undefined. */
972     case OFPACT_BUNDLE:
973     case OFPACT_CONTROLLER:
974     case OFPACT_ENQUEUE:
975     case OFPACT_EXIT:
976     case OFPACT_FIN_TIMEOUT:
977     case OFPACT_LEARN:
978     case OFPACT_MULTIPATH:
979     case OFPACT_NOTE:
980     case OFPACT_OUTPUT_REG:
981     case OFPACT_POP_QUEUE:
982     case OFPACT_REG_MOVE:
983     case OFPACT_RESUBMIT:
984     case OFPACT_SAMPLE:
985     case OFPACT_STACK_POP:
986     case OFPACT_STACK_PUSH:
987
988     /* The action set may only include actions and thus
989      * may not include any instructions */
990     case OFPACT_CLEAR_ACTIONS:
991     case OFPACT_GOTO_TABLE:
992     case OFPACT_METER:
993     case OFPACT_WRITE_ACTIONS:
994     case OFPACT_WRITE_METADATA:
995         return false;
996     default:
997         NOT_REACHED();
998     }
999 }
1000
1001 /* Append ofpact 'a' onto the tail of 'out' */
1002 static void
1003 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1004 {
1005     ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1006 }
1007
1008 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1009 static bool
1010 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1011                   enum ofpact_type filter)
1012 {
1013     const struct ofpact *target;
1014     const struct ofpact *a;
1015
1016     target = NULL;
1017     OFPACT_FOR_EACH (a, in->data, in->size) {
1018         if (a->type == filter) {
1019             target = a;
1020         }
1021     }
1022     if (target) {
1023         ofpact_copy(out, target);
1024     }
1025     return target != NULL;
1026 }
1027
1028 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1029  * The order of appended ofpacts is preserved between 'in' and 'out' */
1030 static void
1031 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1032                  bool (*filter)(const struct ofpact *))
1033 {
1034     const struct ofpact *a;
1035
1036     OFPACT_FOR_EACH (a, in->data, in->size) {
1037         if (filter(a)) {
1038             ofpact_copy(out, a);
1039         }
1040     }
1041 }
1042
1043 /* Reads 'action_set', which contains ofpacts accumulated by
1044  * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1045  * executed directly into 'action_list'.  (These names correspond to the
1046  * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1047  *
1048  * In general this involves appending the last instance of each action that is
1049  * adimissible in the action set in the order described in the OpenFlow
1050  * specification.
1051  *
1052  * Exceptions:
1053  * + output action is only appended if no group action was present in 'in'.
1054  * + As a simplification all set actions are copied in the order the are
1055  *   provided in 'in' as many set actions applied to a field has the same
1056  *   affect as only applying the last action that sets a field and
1057  *   duplicates are removed by do_xlate_actions().
1058  *   This has an unwanted side-effect of compsoting multiple
1059  *   LOAD_REG actions that touch different regions of the same field. */
1060 void
1061 ofpacts_execute_action_set(struct ofpbuf *action_list,
1062                            const struct ofpbuf *action_set)
1063 {
1064     /* The OpenFlow spec "Action Set" section specifies this order. */
1065     ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1066     ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1067     ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1068     ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1069     ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1070     ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1071     ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1072     ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1073
1074     /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1075      * we should execute only OFPACT_GROUP.
1076      *
1077      * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1078      * all the actions because there's no point in modifying a packet that will
1079      * not be sent anywhere. */
1080     if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1081         !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1082         ofpbuf_clear(action_list);
1083     }
1084 }
1085
1086
1087 static enum ofperr
1088 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1089                                        size_t n_in, struct ofpbuf *out)
1090 {
1091     enum ofperr error;
1092     struct ofpact *a;
1093     size_t start = out->size;
1094
1095     error = ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
1096     if (error) {
1097         return error;
1098     }
1099
1100     OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1101         if (!ofpact_is_allowed_in_actions_set(a)) {
1102             VLOG_WARN_RL(&rl, "disallowed action in action set");
1103             return OFPERR_OFPBAC_BAD_TYPE;
1104         }
1105     }
1106
1107     return 0;
1108 }
1109
1110 \f
1111 /* OpenFlow 1.1 instructions. */
1112
1113 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
1114     static inline const struct STRUCT * OVS_UNUSED              \
1115     instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1116     {                                                           \
1117         ovs_assert(inst->type == htons(ENUM));                  \
1118         return ALIGNED_CAST(struct STRUCT *, inst);             \
1119     }                                                           \
1120                                                                 \
1121     static inline void OVS_UNUSED                               \
1122     instruction_init_##ENUM(struct STRUCT *s)                   \
1123     {                                                           \
1124         memset(s, 0, sizeof *s);                                \
1125         s->type = htons(ENUM);                                  \
1126         s->len = htons(sizeof *s);                              \
1127     }                                                           \
1128                                                                 \
1129     static inline struct STRUCT * OVS_UNUSED                    \
1130     instruction_put_##ENUM(struct ofpbuf *buf)                  \
1131     {                                                           \
1132         struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
1133         instruction_init_##ENUM(s);                             \
1134         return s;                                               \
1135     }
1136 OVS_INSTRUCTIONS
1137 #undef DEFINE_INST
1138
1139 struct instruction_type_info {
1140     enum ovs_instruction_type type;
1141     const char *name;
1142 };
1143
1144 static const struct instruction_type_info inst_info[] = {
1145 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
1146 OVS_INSTRUCTIONS
1147 #undef DEFINE_INST
1148 };
1149
1150 const char *
1151 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1152 {
1153     return inst_info[type].name;
1154 }
1155
1156 int
1157 ovs_instruction_type_from_name(const char *name)
1158 {
1159     const struct instruction_type_info *p;
1160     for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1161         if (!strcasecmp(name, p->name)) {
1162             return p->type;
1163         }
1164     }
1165     return -1;
1166 }
1167
1168 enum ovs_instruction_type
1169 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1170 {
1171     switch (type) {
1172     case OFPACT_METER:
1173         return OVSINST_OFPIT13_METER;
1174     case OFPACT_CLEAR_ACTIONS:
1175         return OVSINST_OFPIT11_CLEAR_ACTIONS;
1176     case OFPACT_WRITE_ACTIONS:
1177         return OVSINST_OFPIT11_WRITE_ACTIONS;
1178     case OFPACT_WRITE_METADATA:
1179         return OVSINST_OFPIT11_WRITE_METADATA;
1180     case OFPACT_GOTO_TABLE:
1181         return OVSINST_OFPIT11_GOTO_TABLE;
1182     case OFPACT_OUTPUT:
1183     case OFPACT_GROUP:
1184     case OFPACT_CONTROLLER:
1185     case OFPACT_ENQUEUE:
1186     case OFPACT_OUTPUT_REG:
1187     case OFPACT_BUNDLE:
1188     case OFPACT_SET_VLAN_VID:
1189     case OFPACT_SET_VLAN_PCP:
1190     case OFPACT_STRIP_VLAN:
1191     case OFPACT_PUSH_VLAN:
1192     case OFPACT_SET_ETH_SRC:
1193     case OFPACT_SET_ETH_DST:
1194     case OFPACT_SET_IPV4_SRC:
1195     case OFPACT_SET_IPV4_DST:
1196     case OFPACT_SET_IPV4_DSCP:
1197     case OFPACT_SET_L4_SRC_PORT:
1198     case OFPACT_SET_L4_DST_PORT:
1199     case OFPACT_REG_MOVE:
1200     case OFPACT_REG_LOAD:
1201     case OFPACT_STACK_PUSH:
1202     case OFPACT_STACK_POP:
1203     case OFPACT_DEC_TTL:
1204     case OFPACT_SET_MPLS_TTL:
1205     case OFPACT_DEC_MPLS_TTL:
1206     case OFPACT_PUSH_MPLS:
1207     case OFPACT_POP_MPLS:
1208     case OFPACT_SET_TUNNEL:
1209     case OFPACT_SET_QUEUE:
1210     case OFPACT_POP_QUEUE:
1211     case OFPACT_FIN_TIMEOUT:
1212     case OFPACT_RESUBMIT:
1213     case OFPACT_LEARN:
1214     case OFPACT_MULTIPATH:
1215     case OFPACT_NOTE:
1216     case OFPACT_EXIT:
1217     case OFPACT_SAMPLE:
1218     default:
1219         return OVSINST_OFPIT11_APPLY_ACTIONS;
1220     }
1221 }
1222
1223 static inline struct ofp11_instruction *
1224 instruction_next(const struct ofp11_instruction *inst)
1225 {
1226     return ((struct ofp11_instruction *) (void *)
1227             ((uint8_t *) inst + ntohs(inst->len)));
1228 }
1229
1230 static inline bool
1231 instruction_is_valid(const struct ofp11_instruction *inst,
1232                      size_t n_instructions)
1233 {
1234     uint16_t len = ntohs(inst->len);
1235     return (!(len % OFP11_INSTRUCTION_ALIGN)
1236             && len >= sizeof *inst
1237             && len / sizeof *inst <= n_instructions);
1238 }
1239
1240 /* This macro is careful to check for instructions with bad lengths. */
1241 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS)  \
1242     for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS);            \
1243          (LEFT) > 0 && instruction_is_valid(ITER, LEFT);                \
1244          ((LEFT) -= (ntohs((ITER)->len)                                 \
1245                      / sizeof(struct ofp11_instruction)),               \
1246           (ITER) = instruction_next(ITER)))
1247
1248 static enum ofperr
1249 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1250                               enum ovs_instruction_type *type)
1251 {
1252     uint16_t len = ntohs(inst->len);
1253
1254     switch (inst->type) {
1255     case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1256         return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1257
1258 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)     \
1259         case CONSTANT_HTONS(ENUM):                      \
1260             if (EXTENSIBLE                              \
1261                 ? len >= sizeof(struct STRUCT)          \
1262                 : len == sizeof(struct STRUCT)) {       \
1263                 *type = OVSINST_##ENUM;                 \
1264                 return 0;                               \
1265             } else {                                    \
1266                 return OFPERR_OFPBIC_BAD_LEN;           \
1267             }
1268 OVS_INSTRUCTIONS
1269 #undef DEFINE_INST
1270
1271     default:
1272         return OFPERR_OFPBIC_UNKNOWN_INST;
1273     }
1274 }
1275
1276 static enum ofperr
1277 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1278                                size_t n_insts,
1279                                const struct ofp11_instruction *out[])
1280 {
1281     const struct ofp11_instruction *inst;
1282     size_t left;
1283
1284     memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1285     INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1286         enum ovs_instruction_type type;
1287         enum ofperr error;
1288
1289         error = decode_openflow11_instruction(inst, &type);
1290         if (error) {
1291             return error;
1292         }
1293
1294         if (out[type]) {
1295             return OFPERR_ONFBIC_DUP_INSTRUCTION;
1296         }
1297         out[type] = inst;
1298     }
1299
1300     if (left) {
1301         VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1302                      (n_insts - left) * sizeof *inst);
1303         return OFPERR_OFPBIC_BAD_LEN;
1304     }
1305     return 0;
1306 }
1307
1308 static void
1309 get_actions_from_instruction(const struct ofp11_instruction *inst,
1310                              const union ofp_action **actions,
1311                              size_t *n_actions)
1312 {
1313     *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1314     *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1315 }
1316
1317 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
1318  * front of 'openflow' into ofpacts.  On success, replaces any existing content
1319  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1320  * Returns 0 if successful, otherwise an OpenFlow error.
1321  *
1322  * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1323  * instructions, so you should call ofpacts_pull_openflow11_instructions()
1324  * instead of this function.
1325  *
1326  * The parsed actions are valid generically, but they may not be valid in a
1327  * specific context.  For example, port numbers up to OFPP_MAX are valid
1328  * generically, but specific datapaths may only support port numbers in a
1329  * smaller range.  Use ofpacts_check() to additional check whether actions are
1330  * valid in a specific context. */
1331 enum ofperr
1332 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1333                                 unsigned int actions_len,
1334                                 struct ofpbuf *ofpacts)
1335 {
1336     return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1337                                 ofpacts_from_openflow11);
1338 }
1339
1340 enum ofperr
1341 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1342                                      unsigned int instructions_len,
1343                                      struct ofpbuf *ofpacts)
1344 {
1345     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1346     const struct ofp11_instruction *instructions;
1347     const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1348     enum ofperr error;
1349
1350     ofpbuf_clear(ofpacts);
1351
1352     if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1353         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1354                      "multiple of %d",
1355                      instructions_len, OFP11_INSTRUCTION_ALIGN);
1356         error = OFPERR_OFPBIC_BAD_LEN;
1357         goto exit;
1358     }
1359
1360     instructions = ofpbuf_try_pull(openflow, instructions_len);
1361     if (instructions == NULL) {
1362         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1363                      "remaining message length (%zu)",
1364                      instructions_len, openflow->size);
1365         error = OFPERR_OFPBIC_BAD_LEN;
1366         goto exit;
1367     }
1368
1369     error = decode_openflow11_instructions(
1370         instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1371         insts);
1372     if (error) {
1373         goto exit;
1374     }
1375
1376     if (insts[OVSINST_OFPIT13_METER]) {
1377         const struct ofp13_instruction_meter *oim;
1378         struct ofpact_meter *om;
1379
1380         oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1381                            insts[OVSINST_OFPIT13_METER]);
1382
1383         om = ofpact_put_METER(ofpacts);
1384         om->meter_id = ntohl(oim->meter_id);
1385     }
1386     if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1387         const union ofp_action *actions;
1388         size_t n_actions;
1389
1390         get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1391                                      &actions, &n_actions);
1392         error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1393         if (error) {
1394             goto exit;
1395         }
1396     }
1397     if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1398         instruction_get_OFPIT11_CLEAR_ACTIONS(
1399             insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1400         ofpact_put_CLEAR_ACTIONS(ofpacts);
1401     }
1402     if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1403         struct ofpact_nest *on;
1404         const union ofp_action *actions;
1405         size_t n_actions;
1406         size_t start;
1407
1408         ofpact_pad(ofpacts);
1409         start = ofpacts->size;
1410         on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1411                         offsetof(struct ofpact_nest, actions));
1412         get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1413                                      &actions, &n_actions);
1414         error = ofpacts_from_openflow11_for_action_set(actions, n_actions,
1415                                                        ofpacts);
1416         if (error) {
1417             goto exit;
1418         }
1419         on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1420         on->ofpact.len = ofpacts->size - start;
1421     }
1422     if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1423         const struct ofp11_instruction_write_metadata *oiwm;
1424         struct ofpact_metadata *om;
1425
1426         oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1427                             insts[OVSINST_OFPIT11_WRITE_METADATA]);
1428
1429         om = ofpact_put_WRITE_METADATA(ofpacts);
1430         om->metadata = oiwm->metadata;
1431         om->mask = oiwm->metadata_mask;
1432     }
1433     if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1434         const struct ofp11_instruction_goto_table *oigt;
1435         struct ofpact_goto_table *ogt;
1436
1437         oigt = instruction_get_OFPIT11_GOTO_TABLE(
1438             insts[OVSINST_OFPIT11_GOTO_TABLE]);
1439         ogt = ofpact_put_GOTO_TABLE(ofpacts);
1440         ogt->table_id = oigt->table_id;
1441     }
1442
1443     error = ofpacts_verify(ofpacts->data, ofpacts->size);
1444 exit:
1445     if (error) {
1446         ofpbuf_clear(ofpacts);
1447     }
1448     return error;
1449 }
1450 \f
1451 /* May modify flow->dl_type, caller must restore it. */
1452 static enum ofperr
1453 ofpact_check__(const struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1454                uint8_t table_id)
1455 {
1456     const struct ofpact_enqueue *enqueue;
1457
1458     switch (a->type) {
1459     case OFPACT_OUTPUT:
1460         return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1461                                          max_ports);
1462
1463     case OFPACT_CONTROLLER:
1464         return 0;
1465
1466     case OFPACT_ENQUEUE:
1467         enqueue = ofpact_get_ENQUEUE(a);
1468         if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1469             && enqueue->port != OFPP_IN_PORT
1470             && enqueue->port != OFPP_LOCAL) {
1471             return OFPERR_OFPBAC_BAD_OUT_PORT;
1472         }
1473         return 0;
1474
1475     case OFPACT_OUTPUT_REG:
1476         return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1477
1478     case OFPACT_BUNDLE:
1479         return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1480
1481     case OFPACT_SET_VLAN_VID:
1482     case OFPACT_SET_VLAN_PCP:
1483     case OFPACT_STRIP_VLAN:
1484     case OFPACT_PUSH_VLAN:
1485     case OFPACT_SET_ETH_SRC:
1486     case OFPACT_SET_ETH_DST:
1487     case OFPACT_SET_IPV4_SRC:
1488     case OFPACT_SET_IPV4_DST:
1489     case OFPACT_SET_IPV4_DSCP:
1490     case OFPACT_SET_L4_SRC_PORT:
1491     case OFPACT_SET_L4_DST_PORT:
1492         return 0;
1493
1494     case OFPACT_REG_MOVE:
1495         return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1496
1497     case OFPACT_REG_LOAD:
1498         return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1499
1500     case OFPACT_STACK_PUSH:
1501         return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1502
1503     case OFPACT_STACK_POP:
1504         return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1505
1506     case OFPACT_DEC_TTL:
1507     case OFPACT_SET_MPLS_TTL:
1508     case OFPACT_DEC_MPLS_TTL:
1509     case OFPACT_SET_TUNNEL:
1510     case OFPACT_SET_QUEUE:
1511     case OFPACT_POP_QUEUE:
1512     case OFPACT_FIN_TIMEOUT:
1513     case OFPACT_RESUBMIT:
1514         return 0;
1515
1516     case OFPACT_LEARN:
1517         return learn_check(ofpact_get_LEARN(a), flow);
1518
1519     case OFPACT_MULTIPATH:
1520         return multipath_check(ofpact_get_MULTIPATH(a), flow);
1521
1522     case OFPACT_NOTE:
1523     case OFPACT_EXIT:
1524         return 0;
1525
1526     case OFPACT_PUSH_MPLS:
1527         flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1528         return 0;
1529
1530     case OFPACT_POP_MPLS:
1531         flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1532         return 0;
1533
1534     case OFPACT_SAMPLE:
1535         return 0;
1536
1537     case OFPACT_CLEAR_ACTIONS:
1538         return 0;
1539
1540     case OFPACT_WRITE_ACTIONS: {
1541         struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
1542         return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
1543                              flow, max_ports, table_id);
1544     }
1545
1546     case OFPACT_WRITE_METADATA:
1547         return 0;
1548
1549     case OFPACT_METER: {
1550         uint32_t mid = ofpact_get_METER(a)->meter_id;
1551         if (mid == 0 || mid > OFPM13_MAX) {
1552             return OFPERR_OFPMMFC_INVALID_METER;
1553         }
1554         return 0;
1555     }
1556
1557     case OFPACT_GOTO_TABLE:
1558         if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1559             return OFPERR_OFPBRC_BAD_TABLE_ID;
1560         }
1561         return 0;
1562
1563     case OFPACT_GROUP:
1564         return 0;
1565
1566     default:
1567         NOT_REACHED();
1568     }
1569 }
1570
1571 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1572  * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1573  * switch with no more than 'max_ports' ports.
1574  *
1575  * May temporarily modify 'flow', but restores the changes before returning. */
1576 enum ofperr
1577 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1578               struct flow *flow, ofp_port_t max_ports, uint8_t table_id)
1579 {
1580     const struct ofpact *a;
1581     ovs_be16 dl_type = flow->dl_type;
1582     enum ofperr error = 0;
1583
1584     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1585         error = ofpact_check__(a, flow, max_ports, table_id);
1586         if (error) {
1587             break;
1588         }
1589     }
1590     flow->dl_type = dl_type; /* Restore. */
1591     return error;
1592 }
1593
1594 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1595  * in the appropriate order as defined by the OpenFlow spec. */
1596 enum ofperr
1597 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1598 {
1599     const struct ofpact *a;
1600     enum ovs_instruction_type inst;
1601
1602     inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1603     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1604         enum ovs_instruction_type next;
1605
1606         next = ovs_instruction_type_from_ofpact_type(a->type);
1607         if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
1608             ? next < inst
1609             : next <= inst) {
1610             const char *name = ovs_instruction_name_from_type(inst);
1611             const char *next_name = ovs_instruction_name_from_type(next);
1612
1613             if (next == inst) {
1614                 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1615                           "1.1+ compatibility", name);
1616             } else {
1617                 VLOG_WARN("invalid instruction ordering: %s must appear "
1618                           "before %s, for OpenFlow 1.1+ compatibility",
1619                           next_name, name);
1620             }
1621             return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1622         }
1623
1624         inst = next;
1625     }
1626
1627     return 0;
1628 }
1629 \f
1630 /* Converting ofpacts to Nicira OpenFlow extensions. */
1631
1632 static void
1633 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1634                                 struct ofpbuf *out)
1635 {
1636     struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1637
1638     naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1639                                            output_reg->src.n_bits);
1640     naor->src = htonl(output_reg->src.field->nxm_header);
1641     naor->max_len = htons(output_reg->max_len);
1642 }
1643
1644 static void
1645 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1646                          struct ofpbuf *out)
1647 {
1648     struct nx_action_resubmit *nar;
1649
1650     if (resubmit->table_id == 0xff
1651         && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1652         nar = ofputil_put_NXAST_RESUBMIT(out);
1653     } else {
1654         nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1655         nar->table = resubmit->table_id;
1656     }
1657     nar->in_port = htons(ofp_to_u16(resubmit->in_port));
1658 }
1659
1660 static void
1661 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1662                            struct ofpbuf *out)
1663 {
1664     uint64_t tun_id = tunnel->tun_id;
1665
1666     if (tun_id <= UINT32_MAX
1667         && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1668         ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1669     } else {
1670         ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1671     }
1672 }
1673
1674 static void
1675 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1676                                struct ofpbuf *out)
1677 {
1678     struct nx_action_write_metadata *nawm;
1679
1680     nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1681     nawm->metadata = om->metadata;
1682     nawm->mask = om->mask;
1683 }
1684
1685 static void
1686 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1687 {
1688     size_t start_ofs = out->size;
1689     struct nx_action_note *nan;
1690     unsigned int remainder;
1691     unsigned int len;
1692
1693     nan = ofputil_put_NXAST_NOTE(out);
1694     out->size -= sizeof nan->note;
1695
1696     ofpbuf_put(out, note->data, note->length);
1697
1698     len = out->size - start_ofs;
1699     remainder = len % OFP_ACTION_ALIGN;
1700     if (remainder) {
1701         ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1702     }
1703     nan = ofpbuf_at(out, start_ofs, sizeof *nan);
1704     nan->len = htons(out->size - start_ofs);
1705 }
1706
1707 static void
1708 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1709                            struct ofpbuf *out)
1710 {
1711     struct nx_action_controller *nac;
1712
1713     nac = ofputil_put_NXAST_CONTROLLER(out);
1714     nac->max_len = htons(oc->max_len);
1715     nac->controller_id = htons(oc->controller_id);
1716     nac->reason = oc->reason;
1717 }
1718
1719 static void
1720 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1721                         struct ofpbuf *out)
1722 {
1723     if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1724         ofputil_put_NXAST_DEC_TTL(out);
1725     } else {
1726         struct nx_action_cnt_ids *nac_ids =
1727             ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1728         int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1729         ovs_be16 *ids;
1730         size_t i;
1731
1732         nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1733         nac_ids->n_controllers = htons(oc_ids->n_controllers);
1734
1735         ids = ofpbuf_put_zeros(out, ids_len);
1736         for (i = 0; i < oc_ids->n_controllers; i++) {
1737             ids[i] = htons(oc_ids->cnt_ids[i]);
1738         }
1739     }
1740 }
1741
1742 static void
1743 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1744                             struct ofpbuf *out)
1745 {
1746     struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1747     naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1748     naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1749 }
1750
1751 static void
1752 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1753                        struct ofpbuf *out)
1754 {
1755     struct nx_action_sample *nas;
1756
1757     nas = ofputil_put_NXAST_SAMPLE(out);
1758     nas->probability = htons(os->probability);
1759     nas->collector_set_id = htonl(os->collector_set_id);
1760     nas->obs_domain_id = htonl(os->obs_domain_id);
1761     nas->obs_point_id = htonl(os->obs_point_id);
1762 }
1763
1764 static void
1765 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1766 {
1767     switch (a->type) {
1768     case OFPACT_CONTROLLER:
1769         ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1770         break;
1771
1772     case OFPACT_OUTPUT_REG:
1773         ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1774         break;
1775
1776     case OFPACT_BUNDLE:
1777         bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1778         break;
1779
1780     case OFPACT_REG_MOVE:
1781         nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1782         break;
1783
1784     case OFPACT_REG_LOAD:
1785         nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1786         break;
1787
1788     case OFPACT_STACK_PUSH:
1789         nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1790         break;
1791
1792     case OFPACT_STACK_POP:
1793         nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1794         break;
1795
1796     case OFPACT_DEC_TTL:
1797         ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1798         break;
1799
1800     case OFPACT_SET_MPLS_TTL:
1801         ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1802             = ofpact_get_SET_MPLS_TTL(a)->ttl;
1803         break;
1804
1805     case OFPACT_DEC_MPLS_TTL:
1806         ofputil_put_NXAST_DEC_MPLS_TTL(out);
1807         break;
1808
1809     case OFPACT_SET_TUNNEL:
1810         ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1811         break;
1812
1813     case OFPACT_WRITE_METADATA:
1814         ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1815         break;
1816
1817     case OFPACT_SET_QUEUE:
1818         ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1819             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1820         break;
1821
1822     case OFPACT_POP_QUEUE:
1823         ofputil_put_NXAST_POP_QUEUE(out);
1824         break;
1825
1826     case OFPACT_FIN_TIMEOUT:
1827         ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1828         break;
1829
1830     case OFPACT_RESUBMIT:
1831         ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1832         break;
1833
1834     case OFPACT_LEARN:
1835         learn_to_nxast(ofpact_get_LEARN(a), out);
1836         break;
1837
1838     case OFPACT_MULTIPATH:
1839         multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1840         break;
1841
1842     case OFPACT_NOTE:
1843         ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1844         break;
1845
1846     case OFPACT_EXIT:
1847         ofputil_put_NXAST_EXIT(out);
1848         break;
1849
1850     case OFPACT_PUSH_MPLS:
1851         ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1852             ofpact_get_PUSH_MPLS(a)->ethertype;
1853         break;
1854
1855     case OFPACT_POP_MPLS:
1856         ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1857             ofpact_get_POP_MPLS(a)->ethertype;
1858         break;
1859
1860     case OFPACT_SAMPLE:
1861         ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1862         break;
1863
1864     case OFPACT_GROUP:
1865     case OFPACT_OUTPUT:
1866     case OFPACT_ENQUEUE:
1867     case OFPACT_SET_VLAN_VID:
1868     case OFPACT_SET_VLAN_PCP:
1869     case OFPACT_STRIP_VLAN:
1870     case OFPACT_PUSH_VLAN:
1871     case OFPACT_SET_ETH_SRC:
1872     case OFPACT_SET_ETH_DST:
1873     case OFPACT_SET_IPV4_SRC:
1874     case OFPACT_SET_IPV4_DST:
1875     case OFPACT_SET_IPV4_DSCP:
1876     case OFPACT_SET_L4_SRC_PORT:
1877     case OFPACT_SET_L4_DST_PORT:
1878     case OFPACT_WRITE_ACTIONS:
1879     case OFPACT_CLEAR_ACTIONS:
1880     case OFPACT_GOTO_TABLE:
1881     case OFPACT_METER:
1882         NOT_REACHED();
1883     }
1884 }
1885 \f
1886 /* Converting ofpacts to OpenFlow 1.0. */
1887
1888 static void
1889 ofpact_output_to_openflow10(const struct ofpact_output *output,
1890                             struct ofpbuf *out)
1891 {
1892     struct ofp10_action_output *oao;
1893
1894     oao = ofputil_put_OFPAT10_OUTPUT(out);
1895     oao->port = htons(ofp_to_u16(output->port));
1896     oao->max_len = htons(output->max_len);
1897 }
1898
1899 static void
1900 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1901                              struct ofpbuf *out)
1902 {
1903     struct ofp10_action_enqueue *oae;
1904
1905     oae = ofputil_put_OFPAT10_ENQUEUE(out);
1906     oae->port = htons(ofp_to_u16(enqueue->port));
1907     oae->queue_id = htonl(enqueue->queue);
1908 }
1909
1910 static void
1911 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1912 {
1913     switch (a->type) {
1914     case OFPACT_OUTPUT:
1915         ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1916         break;
1917
1918     case OFPACT_ENQUEUE:
1919         ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1920         break;
1921
1922     case OFPACT_SET_VLAN_VID:
1923         ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1924             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1925         break;
1926
1927     case OFPACT_SET_VLAN_PCP:
1928         ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1929             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1930         break;
1931
1932     case OFPACT_STRIP_VLAN:
1933         ofputil_put_OFPAT10_STRIP_VLAN(out);
1934         break;
1935
1936     case OFPACT_SET_ETH_SRC:
1937         memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1938                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1939         break;
1940
1941     case OFPACT_SET_ETH_DST:
1942         memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1943                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1944         break;
1945
1946     case OFPACT_SET_IPV4_SRC:
1947         ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1948             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1949         break;
1950
1951     case OFPACT_SET_IPV4_DST:
1952         ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1953             = ofpact_get_SET_IPV4_DST(a)->ipv4;
1954         break;
1955
1956     case OFPACT_SET_IPV4_DSCP:
1957         ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1958             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1959         break;
1960
1961     case OFPACT_SET_L4_SRC_PORT:
1962         ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1963             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1964         break;
1965
1966     case OFPACT_SET_L4_DST_PORT:
1967         ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1968             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1969         break;
1970
1971     case OFPACT_PUSH_VLAN:
1972     case OFPACT_CLEAR_ACTIONS:
1973     case OFPACT_WRITE_ACTIONS:
1974     case OFPACT_GOTO_TABLE:
1975     case OFPACT_METER:
1976         /* XXX */
1977         break;
1978
1979     case OFPACT_GROUP:
1980         break;
1981
1982     case OFPACT_CONTROLLER:
1983     case OFPACT_OUTPUT_REG:
1984     case OFPACT_BUNDLE:
1985     case OFPACT_REG_MOVE:
1986     case OFPACT_REG_LOAD:
1987     case OFPACT_STACK_PUSH:
1988     case OFPACT_STACK_POP:
1989     case OFPACT_DEC_TTL:
1990     case OFPACT_SET_MPLS_TTL:
1991     case OFPACT_DEC_MPLS_TTL:
1992     case OFPACT_SET_TUNNEL:
1993     case OFPACT_WRITE_METADATA:
1994     case OFPACT_SET_QUEUE:
1995     case OFPACT_POP_QUEUE:
1996     case OFPACT_FIN_TIMEOUT:
1997     case OFPACT_RESUBMIT:
1998     case OFPACT_LEARN:
1999     case OFPACT_MULTIPATH:
2000     case OFPACT_NOTE:
2001     case OFPACT_EXIT:
2002     case OFPACT_PUSH_MPLS:
2003     case OFPACT_POP_MPLS:
2004     case OFPACT_SAMPLE:
2005         ofpact_to_nxast(a, out);
2006         break;
2007     }
2008 }
2009
2010 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
2011  * actions in 'openflow', appending the actions to any existing data in
2012  * 'openflow'. */
2013 void
2014 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
2015                        struct ofpbuf *openflow)
2016 {
2017     const struct ofpact *a;
2018
2019     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2020         ofpact_to_openflow10(a, openflow);
2021     }
2022 }
2023 \f
2024 /* Converting ofpacts to OpenFlow 1.1. */
2025
2026 static void
2027 ofpact_output_to_openflow11(const struct ofpact_output *output,
2028                             struct ofpbuf *out)
2029 {
2030     struct ofp11_action_output *oao;
2031
2032     oao = ofputil_put_OFPAT11_OUTPUT(out);
2033     oao->port = ofputil_port_to_ofp11(output->port);
2034     oao->max_len = htons(output->max_len);
2035 }
2036
2037 static void
2038 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2039                              struct ofpbuf *out)
2040 {
2041     if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2042         && (!dec_ttl->ofpact.compat ||
2043             dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2044         ofputil_put_OFPAT11_DEC_NW_TTL(out);
2045     } else {
2046         ofpact_dec_ttl_to_nxast(dec_ttl, out);
2047     }
2048 }
2049
2050 static void
2051 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2052 {
2053     switch (a->type) {
2054     case OFPACT_OUTPUT:
2055         return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2056
2057     case OFPACT_ENQUEUE:
2058         /* XXX */
2059         break;
2060
2061     case OFPACT_SET_VLAN_VID:
2062         ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2063             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2064         break;
2065
2066     case OFPACT_SET_VLAN_PCP:
2067         ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2068             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2069         break;
2070
2071     case OFPACT_STRIP_VLAN:
2072         ofputil_put_OFPAT11_POP_VLAN(out);
2073         break;
2074
2075     case OFPACT_PUSH_VLAN:
2076         /* XXX ETH_TYPE_VLAN_8021AD case */
2077         ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2078             htons(ETH_TYPE_VLAN_8021Q);
2079         break;
2080
2081     case OFPACT_SET_QUEUE:
2082         ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2083             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2084         break;
2085
2086     case OFPACT_SET_ETH_SRC:
2087         memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2088                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2089         break;
2090
2091     case OFPACT_SET_ETH_DST:
2092         memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2093                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2094         break;
2095
2096     case OFPACT_SET_IPV4_SRC:
2097         ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2098             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2099         break;
2100
2101     case OFPACT_SET_IPV4_DST:
2102         ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2103             = ofpact_get_SET_IPV4_DST(a)->ipv4;
2104         break;
2105
2106     case OFPACT_SET_IPV4_DSCP:
2107         ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2108             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
2109         break;
2110
2111     case OFPACT_SET_L4_SRC_PORT:
2112         ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2113             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2114         break;
2115
2116     case OFPACT_SET_L4_DST_PORT:
2117         ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2118             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2119         break;
2120
2121     case OFPACT_DEC_TTL:
2122         ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2123         break;
2124
2125     case OFPACT_SET_MPLS_TTL:
2126         ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2127             = ofpact_get_SET_MPLS_TTL(a)->ttl;
2128         break;
2129
2130     case OFPACT_DEC_MPLS_TTL:
2131         ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2132         break;
2133
2134     case OFPACT_WRITE_METADATA:
2135         /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2136         break;
2137
2138     case OFPACT_PUSH_MPLS:
2139         ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2140             ofpact_get_PUSH_MPLS(a)->ethertype;
2141         break;
2142
2143     case OFPACT_POP_MPLS:
2144         ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2145             ofpact_get_POP_MPLS(a)->ethertype;
2146
2147         break;
2148
2149     case OFPACT_CLEAR_ACTIONS:
2150     case OFPACT_WRITE_ACTIONS:
2151     case OFPACT_GOTO_TABLE:
2152     case OFPACT_METER:
2153         NOT_REACHED();
2154
2155     case OFPACT_GROUP:
2156         ofputil_put_OFPAT11_GROUP(out)->group_id =
2157             htonl(ofpact_get_GROUP(a)->group_id);
2158         break;
2159
2160     case OFPACT_CONTROLLER:
2161     case OFPACT_OUTPUT_REG:
2162     case OFPACT_BUNDLE:
2163     case OFPACT_REG_MOVE:
2164     case OFPACT_REG_LOAD:
2165     case OFPACT_STACK_PUSH:
2166     case OFPACT_STACK_POP:
2167     case OFPACT_SET_TUNNEL:
2168     case OFPACT_POP_QUEUE:
2169     case OFPACT_FIN_TIMEOUT:
2170     case OFPACT_RESUBMIT:
2171     case OFPACT_LEARN:
2172     case OFPACT_MULTIPATH:
2173     case OFPACT_NOTE:
2174     case OFPACT_EXIT:
2175     case OFPACT_SAMPLE:
2176         ofpact_to_nxast(a, out);
2177         break;
2178     }
2179 }
2180
2181 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
2182  * 1.1 actions in 'openflow', appending the actions to any existing data in
2183  * 'openflow'. */
2184 size_t
2185 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
2186                                size_t ofpacts_len, struct ofpbuf *openflow)
2187 {
2188     const struct ofpact *a;
2189     size_t start_size = openflow->size;
2190
2191     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2192         ofpact_to_openflow11(a, openflow);
2193     }
2194
2195     return openflow->size - start_size;
2196 }
2197
2198 static void
2199 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2200 {
2201     struct ofp11_instruction_actions *oia;
2202
2203     /* Update the instruction's length (or, if it's empty, delete it). */
2204     oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2205     if (openflow->size > ofs + sizeof *oia) {
2206         oia->len = htons(openflow->size - ofs);
2207     } else {
2208         openflow->size = ofs;
2209     }
2210 }
2211
2212 void
2213 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
2214                                     size_t ofpacts_len,
2215                                     struct ofpbuf *openflow)
2216 {
2217     const struct ofpact *a;
2218
2219     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2220         switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2221         case OVSINST_OFPIT11_CLEAR_ACTIONS:
2222             instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2223             break;
2224
2225         case OVSINST_OFPIT11_GOTO_TABLE: {
2226             struct ofp11_instruction_goto_table *oigt;
2227             oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2228             oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2229             memset(oigt->pad, 0, sizeof oigt->pad);
2230             break;
2231         }
2232
2233         case OVSINST_OFPIT11_WRITE_METADATA: {
2234             const struct ofpact_metadata *om;
2235             struct ofp11_instruction_write_metadata *oiwm;
2236
2237             om = ofpact_get_WRITE_METADATA(a);
2238             oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2239             oiwm->metadata = om->metadata;
2240             oiwm->metadata_mask = om->mask;
2241             break;
2242         }
2243
2244         case OVSINST_OFPIT13_METER: {
2245             const struct ofpact_meter *om;
2246             struct ofp13_instruction_meter *oim;
2247
2248             om = ofpact_get_METER(a);
2249             oim = instruction_put_OFPIT13_METER(openflow);
2250             oim->meter_id = htonl(om->meter_id);
2251             break;
2252         }
2253
2254         case OVSINST_OFPIT11_APPLY_ACTIONS: {
2255             const size_t ofs = openflow->size;
2256             const size_t ofpacts_len_left =
2257                 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2258             const struct ofpact *action;
2259             const struct ofpact *processed = a;
2260
2261             instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2262             OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2263                 if (ovs_instruction_type_from_ofpact_type(action->type)
2264                     != OVSINST_OFPIT11_APPLY_ACTIONS) {
2265                     break;
2266                 }
2267                 ofpact_to_openflow11(action, openflow);
2268                 processed = action;
2269             }
2270             ofpacts_update_instruction_actions(openflow, ofs);
2271             a = processed;
2272             break;
2273         }
2274
2275         case OVSINST_OFPIT11_WRITE_ACTIONS: {
2276             const size_t ofs = openflow->size;
2277             const struct ofpact_nest *on;
2278
2279             on = ofpact_get_WRITE_ACTIONS(a);
2280             instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2281             ofpacts_put_openflow11_actions(on->actions,
2282                                            ofpact_nest_get_action_len(on),
2283                                            openflow);
2284             ofpacts_update_instruction_actions(openflow, ofs);
2285
2286             break;
2287         }
2288         }
2289     }
2290 }
2291 \f
2292 /* Returns true if 'action' outputs to 'port', false otherwise. */
2293 static bool
2294 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2295 {
2296     switch (ofpact->type) {
2297     case OFPACT_OUTPUT:
2298         return ofpact_get_OUTPUT(ofpact)->port == port;
2299     case OFPACT_ENQUEUE:
2300         return ofpact_get_ENQUEUE(ofpact)->port == port;
2301     case OFPACT_CONTROLLER:
2302         return port == OFPP_CONTROLLER;
2303
2304     case OFPACT_OUTPUT_REG:
2305     case OFPACT_BUNDLE:
2306     case OFPACT_SET_VLAN_VID:
2307     case OFPACT_SET_VLAN_PCP:
2308     case OFPACT_STRIP_VLAN:
2309     case OFPACT_PUSH_VLAN:
2310     case OFPACT_SET_ETH_SRC:
2311     case OFPACT_SET_ETH_DST:
2312     case OFPACT_SET_IPV4_SRC:
2313     case OFPACT_SET_IPV4_DST:
2314     case OFPACT_SET_IPV4_DSCP:
2315     case OFPACT_SET_L4_SRC_PORT:
2316     case OFPACT_SET_L4_DST_PORT:
2317     case OFPACT_REG_MOVE:
2318     case OFPACT_REG_LOAD:
2319     case OFPACT_STACK_PUSH:
2320     case OFPACT_STACK_POP:
2321     case OFPACT_DEC_TTL:
2322     case OFPACT_SET_MPLS_TTL:
2323     case OFPACT_DEC_MPLS_TTL:
2324     case OFPACT_SET_TUNNEL:
2325     case OFPACT_WRITE_METADATA:
2326     case OFPACT_SET_QUEUE:
2327     case OFPACT_POP_QUEUE:
2328     case OFPACT_FIN_TIMEOUT:
2329     case OFPACT_RESUBMIT:
2330     case OFPACT_LEARN:
2331     case OFPACT_MULTIPATH:
2332     case OFPACT_NOTE:
2333     case OFPACT_EXIT:
2334     case OFPACT_PUSH_MPLS:
2335     case OFPACT_POP_MPLS:
2336     case OFPACT_SAMPLE:
2337     case OFPACT_CLEAR_ACTIONS:
2338     case OFPACT_WRITE_ACTIONS:
2339     case OFPACT_GOTO_TABLE:
2340     case OFPACT_METER:
2341     case OFPACT_GROUP:
2342     default:
2343         return false;
2344     }
2345 }
2346
2347 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2348  * to 'port', false otherwise. */
2349 bool
2350 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
2351                        ofp_port_t port)
2352 {
2353     const struct ofpact *a;
2354
2355     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2356         if (ofpact_outputs_to_port(a, port)) {
2357             return true;
2358         }
2359     }
2360
2361     return false;
2362 }
2363
2364 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2365  * to 'group', false otherwise. */
2366 bool
2367 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2368                         uint32_t group_id)
2369 {
2370     const struct ofpact *a;
2371
2372     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2373         if (a->type == OFPACT_GROUP
2374             && ofpact_get_GROUP(a)->group_id == group_id) {
2375             return true;
2376         }
2377     }
2378
2379     return false;
2380 }
2381
2382 bool
2383 ofpacts_equal(const struct ofpact *a, size_t a_len,
2384               const struct ofpact *b, size_t b_len)
2385 {
2386     return a_len == b_len && !memcmp(a, b, a_len);
2387 }
2388
2389 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2390  * 'ofpacts'.  If found, returns its meter ID; if not, returns 0.
2391  *
2392  * This function relies on the order of 'ofpacts' being correct (as checked by
2393  * ofpacts_verify()). */
2394 uint32_t
2395 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2396 {
2397     const struct ofpact *a;
2398
2399     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2400         enum ovs_instruction_type inst;
2401
2402         inst = ovs_instruction_type_from_ofpact_type(a->type);
2403         if (a->type == OFPACT_METER) {
2404             return ofpact_get_METER(a)->meter_id;
2405         } else if (inst > OVSINST_OFPIT13_METER) {
2406             break;
2407         }
2408     }
2409
2410     return 0;
2411 }
2412 \f
2413 /* Formatting ofpacts. */
2414
2415 static void
2416 print_note(const struct ofpact_note *note, struct ds *string)
2417 {
2418     size_t i;
2419
2420     ds_put_cstr(string, "note:");
2421     for (i = 0; i < note->length; i++) {
2422         if (i) {
2423             ds_put_char(string, '.');
2424         }
2425         ds_put_format(string, "%02"PRIx8, note->data[i]);
2426     }
2427 }
2428
2429 static void
2430 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2431               struct ds *s)
2432 {
2433     size_t i;
2434
2435     ds_put_cstr(s, "dec_ttl");
2436     if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2437         ds_put_cstr(s, "(");
2438         for (i = 0; i < ids->n_controllers; i++) {
2439             if (i) {
2440                 ds_put_cstr(s, ",");
2441             }
2442             ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2443         }
2444         ds_put_cstr(s, ")");
2445     }
2446 }
2447
2448 static void
2449 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2450                   struct ds *s)
2451 {
2452     ds_put_cstr(s, "fin_timeout(");
2453     if (fin_timeout->fin_idle_timeout) {
2454         ds_put_format(s, "idle_timeout=%"PRIu16",",
2455                       fin_timeout->fin_idle_timeout);
2456     }
2457     if (fin_timeout->fin_hard_timeout) {
2458         ds_put_format(s, "hard_timeout=%"PRIu16",",
2459                       fin_timeout->fin_hard_timeout);
2460     }
2461     ds_chomp(s, ',');
2462     ds_put_char(s, ')');
2463 }
2464
2465 static void
2466 ofpact_format(const struct ofpact *a, struct ds *s)
2467 {
2468     const struct ofpact_enqueue *enqueue;
2469     const struct ofpact_resubmit *resubmit;
2470     const struct ofpact_controller *controller;
2471     const struct ofpact_metadata *metadata;
2472     const struct ofpact_tunnel *tunnel;
2473     const struct ofpact_sample *sample;
2474     ofp_port_t port;
2475
2476     switch (a->type) {
2477     case OFPACT_OUTPUT:
2478         port = ofpact_get_OUTPUT(a)->port;
2479         if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
2480             ds_put_format(s, "output:%"PRIu16, port);
2481         } else {
2482             ofputil_format_port(port, s);
2483             if (port == OFPP_CONTROLLER) {
2484                 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2485             }
2486         }
2487         break;
2488
2489     case OFPACT_CONTROLLER:
2490         controller = ofpact_get_CONTROLLER(a);
2491         if (controller->reason == OFPR_ACTION &&
2492             controller->controller_id == 0) {
2493             ds_put_format(s, "CONTROLLER:%"PRIu16,
2494                           ofpact_get_CONTROLLER(a)->max_len);
2495         } else {
2496             enum ofp_packet_in_reason reason = controller->reason;
2497
2498             ds_put_cstr(s, "controller(");
2499             if (reason != OFPR_ACTION) {
2500                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2501
2502                 ds_put_format(s, "reason=%s,",
2503                               ofputil_packet_in_reason_to_string(
2504                                   reason, reasonbuf, sizeof reasonbuf));
2505             }
2506             if (controller->max_len != UINT16_MAX) {
2507                 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2508             }
2509             if (controller->controller_id != 0) {
2510                 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2511             }
2512             ds_chomp(s, ',');
2513             ds_put_char(s, ')');
2514         }
2515         break;
2516
2517     case OFPACT_ENQUEUE:
2518         enqueue = ofpact_get_ENQUEUE(a);
2519         ds_put_format(s, "enqueue:");
2520         ofputil_format_port(enqueue->port, s);
2521         ds_put_format(s, "q%"PRIu32, enqueue->queue);
2522         break;
2523
2524     case OFPACT_OUTPUT_REG:
2525         ds_put_cstr(s, "output:");
2526         mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2527         break;
2528
2529     case OFPACT_BUNDLE:
2530         bundle_format(ofpact_get_BUNDLE(a), s);
2531         break;
2532
2533     case OFPACT_SET_VLAN_VID:
2534         ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2535                       ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2536         break;
2537
2538     case OFPACT_SET_VLAN_PCP:
2539         ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2540                       ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2541         break;
2542
2543     case OFPACT_STRIP_VLAN:
2544         ds_put_cstr(s, "strip_vlan");
2545         break;
2546
2547     case OFPACT_PUSH_VLAN:
2548         /* XXX 802.1AD case*/
2549         ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2550         break;
2551
2552     case OFPACT_SET_ETH_SRC:
2553         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2554                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2555         break;
2556
2557     case OFPACT_SET_ETH_DST:
2558         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2559                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2560         break;
2561
2562     case OFPACT_SET_IPV4_SRC:
2563         ds_put_format(s, "mod_nw_src:"IP_FMT,
2564                       IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2565         break;
2566
2567     case OFPACT_SET_IPV4_DST:
2568         ds_put_format(s, "mod_nw_dst:"IP_FMT,
2569                       IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2570         break;
2571
2572     case OFPACT_SET_IPV4_DSCP:
2573         ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2574         break;
2575
2576     case OFPACT_SET_L4_SRC_PORT:
2577         ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2578         break;
2579
2580     case OFPACT_SET_L4_DST_PORT:
2581         ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2582         break;
2583
2584     case OFPACT_REG_MOVE:
2585         nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2586         break;
2587
2588     case OFPACT_REG_LOAD:
2589         nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2590         break;
2591
2592     case OFPACT_STACK_PUSH:
2593         nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2594         break;
2595
2596     case OFPACT_STACK_POP:
2597         nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2598         break;
2599
2600     case OFPACT_DEC_TTL:
2601         print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2602         break;
2603
2604     case OFPACT_SET_MPLS_TTL:
2605         ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2606                       ofpact_get_SET_MPLS_TTL(a)->ttl);
2607         break;
2608
2609     case OFPACT_DEC_MPLS_TTL:
2610         ds_put_cstr(s, "dec_mpls_ttl");
2611         break;
2612
2613     case OFPACT_SET_TUNNEL:
2614         tunnel = ofpact_get_SET_TUNNEL(a);
2615         ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2616                       (tunnel->tun_id > UINT32_MAX
2617                        || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2618                       tunnel->tun_id);
2619         break;
2620
2621     case OFPACT_SET_QUEUE:
2622         ds_put_format(s, "set_queue:%"PRIu32,
2623                       ofpact_get_SET_QUEUE(a)->queue_id);
2624         break;
2625
2626     case OFPACT_POP_QUEUE:
2627         ds_put_cstr(s, "pop_queue");
2628         break;
2629
2630     case OFPACT_FIN_TIMEOUT:
2631         print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2632         break;
2633
2634     case OFPACT_RESUBMIT:
2635         resubmit = ofpact_get_RESUBMIT(a);
2636         if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2637             ds_put_cstr(s, "resubmit:");
2638             ofputil_format_port(resubmit->in_port, s);
2639         } else {
2640             ds_put_format(s, "resubmit(");
2641             if (resubmit->in_port != OFPP_IN_PORT) {
2642                 ofputil_format_port(resubmit->in_port, s);
2643             }
2644             ds_put_char(s, ',');
2645             if (resubmit->table_id != 255) {
2646                 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2647             }
2648             ds_put_char(s, ')');
2649         }
2650         break;
2651
2652     case OFPACT_LEARN:
2653         learn_format(ofpact_get_LEARN(a), s);
2654         break;
2655
2656     case OFPACT_MULTIPATH:
2657         multipath_format(ofpact_get_MULTIPATH(a), s);
2658         break;
2659
2660     case OFPACT_NOTE:
2661         print_note(ofpact_get_NOTE(a), s);
2662         break;
2663
2664     case OFPACT_PUSH_MPLS:
2665         ds_put_format(s, "push_mpls:0x%04"PRIx16,
2666                       ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2667         break;
2668
2669     case OFPACT_POP_MPLS:
2670         ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2671                       ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2672         break;
2673
2674     case OFPACT_EXIT:
2675         ds_put_cstr(s, "exit");
2676         break;
2677
2678     case OFPACT_SAMPLE:
2679         sample = ofpact_get_SAMPLE(a);
2680         ds_put_format(
2681             s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2682             ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2683             sample->probability, sample->collector_set_id,
2684             sample->obs_domain_id, sample->obs_point_id);
2685         break;
2686
2687     case OFPACT_WRITE_ACTIONS: {
2688         struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2689         ds_put_format(s, "%s(",
2690                       ovs_instruction_name_from_type(
2691                           OVSINST_OFPIT11_WRITE_ACTIONS));
2692         ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
2693         ds_put_char(s, ')');
2694         break;
2695     }
2696
2697     case OFPACT_CLEAR_ACTIONS:
2698         ds_put_format(s, "%s",
2699                       ovs_instruction_name_from_type(
2700                           OVSINST_OFPIT11_CLEAR_ACTIONS));
2701         break;
2702
2703     case OFPACT_WRITE_METADATA:
2704         metadata = ofpact_get_WRITE_METADATA(a);
2705         ds_put_format(s, "%s:%#"PRIx64,
2706                       ovs_instruction_name_from_type(
2707                           OVSINST_OFPIT11_WRITE_METADATA),
2708                       ntohll(metadata->metadata));
2709         if (metadata->mask != OVS_BE64_MAX) {
2710             ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2711         }
2712         break;
2713
2714     case OFPACT_GOTO_TABLE:
2715         ds_put_format(s, "%s:%"PRIu8,
2716                       ovs_instruction_name_from_type(
2717                           OVSINST_OFPIT11_GOTO_TABLE),
2718                       ofpact_get_GOTO_TABLE(a)->table_id);
2719         break;
2720
2721     case OFPACT_METER:
2722         ds_put_format(s, "%s:%"PRIu32,
2723                       ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
2724                       ofpact_get_METER(a)->meter_id);
2725         break;
2726
2727     case OFPACT_GROUP:
2728         ds_put_format(s, "group:%"PRIu32,
2729                       ofpact_get_GROUP(a)->group_id);
2730         break;
2731     }
2732 }
2733
2734 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2735  * 'ofpacts' to 'string'. */
2736 void
2737 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2738                struct ds *string)
2739 {
2740     if (!ofpacts_len) {
2741         ds_put_cstr(string, "drop");
2742     } else {
2743         const struct ofpact *a;
2744
2745         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2746             if (a != ofpacts) {
2747                 ds_put_cstr(string, ",");
2748             }
2749
2750             /* XXX write-actions */
2751             ofpact_format(a, string);
2752         }
2753     }
2754 }
2755 \f
2756 /* Internal use by helpers. */
2757
2758 void *
2759 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2760 {
2761     struct ofpact *ofpact;
2762
2763     ofpact_pad(ofpacts);
2764     ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2765     ofpact_init(ofpact, type, len);
2766     return ofpact;
2767 }
2768
2769 void
2770 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2771 {
2772     memset(ofpact, 0, len);
2773     ofpact->type = type;
2774     ofpact->compat = OFPUTIL_ACTION_INVALID;
2775     ofpact->len = len;
2776 }
2777 \f
2778 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2779  * starting at 'ofpact'.
2780  *
2781  * This is the correct way to update a variable-length ofpact's length after
2782  * adding the variable-length part of the payload.  (See the large comment
2783  * near the end of ofp-actions.h for more information.) */
2784 void
2785 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2786 {
2787     ovs_assert(ofpact == ofpacts->l2);
2788     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2789 }
2790
2791 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
2792  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2793  * client must call this itself after adding the final ofpact to an array of
2794  * them.
2795  *
2796  * (The consequences of failing to call this function are probably not dire.
2797  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2798  * not dereference it.  That's undefined behavior, technically, but it will not
2799  * cause a real problem on common systems.  Still, it seems better to call
2800  * it.) */
2801 void
2802 ofpact_pad(struct ofpbuf *ofpacts)
2803 {
2804     unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2805     if (rem) {
2806         ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2807     }
2808 }
2809
2810 void
2811 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2812                       const void *src)
2813 {
2814     load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2815     load->dst.field = mf;
2816     load->dst.ofs = 0;
2817     load->dst.n_bits = mf->n_bits;
2818     bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2819                  &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);
2820 }