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