ofp-actions: Pretend that OpenFlow 1.0 has instructions.
[cascardo/ovs.git] / lib / ofp-actions.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 ofp11_action_nw_ecn nw_ecn;
48     struct ofp11_action_nw_ttl nw_ttl;
49     struct ofp_action_tp_port tp_port;
50     struct ofp_action_dl_addr dl_addr;
51     struct ofp10_action_enqueue enqueue;
52     struct ofp11_action_output ofp11_output;
53     struct ofp11_action_push push;
54     struct ofp11_action_pop_mpls ofp11_pop_mpls;
55     struct ofp11_action_set_queue ofp11_set_queue;
56     struct ofp11_action_mpls_label ofp11_mpls_label;
57     struct ofp11_action_mpls_tc ofp11_mpls_tc;
58     struct ofp11_action_mpls_ttl ofp11_mpls_ttl;
59     struct ofp11_action_group group;
60     struct ofp12_action_set_field set_field;
61     struct nx_action_header nxa_header;
62     struct nx_action_resubmit resubmit;
63     struct nx_action_set_tunnel set_tunnel;
64     struct nx_action_set_tunnel64 set_tunnel64;
65     struct nx_action_write_metadata write_metadata;
66     struct nx_action_set_queue set_queue;
67     struct nx_action_reg_move reg_move;
68     struct nx_action_reg_load reg_load;
69     struct nx_action_stack stack;
70     struct nx_action_note note;
71     struct nx_action_multipath multipath;
72     struct nx_action_bundle bundle;
73     struct nx_action_output_reg output_reg;
74     struct nx_action_cnt_ids cnt_ids;
75     struct nx_action_fin_timeout fin_timeout;
76     struct nx_action_controller controller;
77     struct nx_action_push_mpls push_mpls;
78     struct nx_action_mpls_ttl mpls_ttl;
79     struct nx_action_pop_mpls pop_mpls;
80     struct nx_action_sample sample;
81     struct nx_action_learn learn;
82     struct nx_action_mpls_label mpls_label;
83     struct nx_action_mpls_tc mpls_tc;
84 };
85
86 static enum ofperr
87 output_from_openflow10(const struct ofp10_action_output *oao,
88                        struct ofpbuf *out)
89 {
90     struct ofpact_output *output;
91
92     output = ofpact_put_OUTPUT(out);
93     output->port = u16_to_ofp(ntohs(oao->port));
94     output->max_len = ntohs(oao->max_len);
95
96     return ofpact_check_output_port(output->port, OFPP_MAX);
97 }
98
99 static enum ofperr
100 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
101                         struct ofpbuf *out)
102 {
103     struct ofpact_enqueue *enqueue;
104
105     enqueue = ofpact_put_ENQUEUE(out);
106     enqueue->port = u16_to_ofp(ntohs(oae->port));
107     enqueue->queue = ntohl(oae->queue_id);
108     if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
109         && enqueue->port != OFPP_IN_PORT
110         && enqueue->port != OFPP_LOCAL) {
111         return OFPERR_OFPBAC_BAD_OUT_PORT;
112     }
113     return 0;
114 }
115
116 static void
117 resubmit_from_openflow(const struct nx_action_resubmit *nar,
118                        struct ofpbuf *out)
119 {
120     struct ofpact_resubmit *resubmit;
121
122     resubmit = ofpact_put_RESUBMIT(out);
123     resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
124     resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
125     resubmit->table_id = 0xff;
126 }
127
128 static enum ofperr
129 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
130                              struct ofpbuf *out)
131 {
132     struct ofpact_resubmit *resubmit;
133
134     if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
135         return OFPERR_OFPBAC_BAD_ARGUMENT;
136     }
137
138     resubmit = ofpact_put_RESUBMIT(out);
139     resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
140     resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
141     resubmit->table_id = nar->table;
142     return 0;
143 }
144
145 static enum ofperr
146 output_reg_from_openflow(const struct nx_action_output_reg *naor,
147                          struct ofpbuf *out)
148 {
149     struct ofpact_output_reg *output_reg;
150
151     if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
152         return OFPERR_OFPBAC_BAD_ARGUMENT;
153     }
154
155     output_reg = ofpact_put_OUTPUT_REG(out);
156     output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
157     output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
158     output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
159     output_reg->max_len = ntohs(naor->max_len);
160
161     return mf_check_src(&output_reg->src, NULL);
162 }
163
164 static void
165 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
166                           struct ofpbuf *out)
167 {
168     struct ofpact_fin_timeout *oft;
169
170     oft = ofpact_put_FIN_TIMEOUT(out);
171     oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
172     oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
173 }
174
175 static void
176 controller_from_openflow(const struct nx_action_controller *nac,
177                          struct ofpbuf *out)
178 {
179     struct ofpact_controller *oc;
180
181     oc = ofpact_put_CONTROLLER(out);
182     oc->max_len = ntohs(nac->max_len);
183     oc->controller_id = ntohs(nac->controller_id);
184     oc->reason = nac->reason;
185 }
186
187 static enum ofperr
188 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
189                     struct ofpbuf *out)
190 {
191     struct ofpact_metadata *om;
192
193     if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
194         return OFPERR_NXBRC_MUST_BE_ZERO;
195     }
196
197     om = ofpact_put_WRITE_METADATA(out);
198     om->metadata = nawm->metadata;
199     om->mask = nawm->mask;
200
201     return 0;
202 }
203
204 static void
205 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
206 {
207     struct ofpact_note *note;
208     unsigned int length;
209
210     length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
211     note = ofpact_put(out, OFPACT_NOTE,
212                       offsetof(struct ofpact_note, data) + length);
213     note->length = length;
214     memcpy(note->data, nan->note, length);
215 }
216
217 static enum ofperr
218 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
219 {
220     uint16_t id = 0;
221     struct ofpact_cnt_ids *ids;
222     enum ofperr error = 0;
223
224     ids = ofpact_put_DEC_TTL(out);
225     ids->ofpact.compat = compat;
226     ids->n_controllers = 1;
227     ofpbuf_put(out, &id, sizeof id);
228     ids = out->frame;
229     ofpact_update_len(out, &ids->ofpact);
230     return error;
231 }
232
233 static enum ofperr
234 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
235                               struct ofpbuf *out)
236 {
237     struct ofpact_cnt_ids *ids;
238     size_t ids_size;
239     int i;
240
241     ids = ofpact_put_DEC_TTL(out);
242     ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
243     ids->n_controllers = ntohs(nac_ids->n_controllers);
244     ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
245
246     if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
247         return OFPERR_NXBRC_MUST_BE_ZERO;
248     }
249
250     if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
251         VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" bytes "
252                      "allocated for controller ids.  %"PRIuSIZE" bytes are required for "
253                      "%"PRIu16" controllers.", ids_size,
254                      ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
255         return OFPERR_OFPBAC_BAD_LEN;
256     }
257
258     for (i = 0; i < ids->n_controllers; i++) {
259         uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
260         ofpbuf_put(out, &id, sizeof id);
261         ids = out->frame;
262     }
263
264     ofpact_update_len(out, &ids->ofpact);
265
266     return 0;
267 }
268
269 static enum ofperr
270 sample_from_openflow(const struct nx_action_sample *nas,
271                      struct ofpbuf *out)
272 {
273     struct ofpact_sample *sample;
274
275     sample = ofpact_put_SAMPLE(out);
276     sample->probability = ntohs(nas->probability);
277     sample->collector_set_id = ntohl(nas->collector_set_id);
278     sample->obs_domain_id = ntohl(nas->obs_domain_id);
279     sample->obs_point_id = ntohl(nas->obs_point_id);
280
281     if (sample->probability == 0) {
282         return OFPERR_OFPBAC_BAD_ARGUMENT;
283     }
284
285     return 0;
286 }
287
288 static enum ofperr
289 push_mpls_from_openflow(ovs_be16 ethertype, struct ofpbuf *out)
290 {
291     struct ofpact_push_mpls *oam;
292
293     if (!eth_type_mpls(ethertype)) {
294         return OFPERR_OFPBAC_BAD_ARGUMENT;
295     }
296     oam = ofpact_put_PUSH_MPLS(out);
297     oam->ethertype = ethertype;
298
299     return 0;
300 }
301
302 static enum ofperr
303 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
304 {
305     const struct nx_action_header *nah = &a->nxa_header;
306     uint16_t len = ntohs(a->header.len);
307
308     if (len < sizeof(struct nx_action_header)) {
309         return OFPERR_OFPBAC_BAD_LEN;
310     } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
311         return OFPERR_OFPBAC_BAD_VENDOR;
312     }
313
314     switch (nah->subtype) {
315 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
316         case CONSTANT_HTONS(ENUM):                      \
317             if (EXTENSIBLE                              \
318                 ? len >= sizeof(struct STRUCT)          \
319                 : len == sizeof(struct STRUCT)) {       \
320                 *code = OFPUTIL_##ENUM;                 \
321                 return 0;                               \
322             } else {                                    \
323                 return OFPERR_OFPBAC_BAD_LEN;           \
324             }                                           \
325             OVS_NOT_REACHED();
326 #include "ofp-util.def"
327
328     case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
329     case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
330     default:
331         return OFPERR_OFPBAC_BAD_TYPE;
332     }
333 }
334
335 /* Parses 'a' to determine its type.  On success stores the correct type into
336  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
337  * '*code' is indeterminate.
338  *
339  * The caller must have already verified that 'a''s length is potentially
340  * correct (that is, a->header.len is nonzero and a multiple of
341  * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
342  *
343  * This function verifies that 'a''s length is correct for the type of action
344  * that it represents. */
345 static enum ofperr
346 decode_openflow10_action(const union ofp_action *a,
347                          enum ofputil_action_code *code)
348 {
349     switch (a->type) {
350     case CONSTANT_HTONS(OFPAT10_VENDOR):
351         return decode_nxast_action(a, code);
352
353 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)                          \
354         case CONSTANT_HTONS(ENUM):                                  \
355             if (a->header.len == htons(sizeof(struct STRUCT))) {    \
356                 *code = OFPUTIL_##ENUM;                             \
357                 return 0;                                           \
358             } else {                                                \
359                 return OFPERR_OFPBAC_BAD_LEN;                       \
360             }                                                       \
361             break;
362 #include "ofp-util.def"
363
364     default:
365         return OFPERR_OFPBAC_BAD_TYPE;
366     }
367 }
368
369 static enum ofperr
370 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
371                   struct ofpbuf *out)
372 {
373     struct ofpact_tunnel *tunnel;
374     enum ofperr error = 0;
375
376     switch (code) {
377     case OFPUTIL_ACTION_INVALID:
378 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
379 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
380 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
381 #include "ofp-util.def"
382         OVS_NOT_REACHED();
383
384     case OFPUTIL_NXAST_RESUBMIT:
385         resubmit_from_openflow(&a->resubmit, out);
386         break;
387
388     case OFPUTIL_NXAST_SET_TUNNEL:
389         tunnel = ofpact_put_SET_TUNNEL(out);
390         tunnel->ofpact.compat = code;
391         tunnel->tun_id = ntohl(a->set_tunnel.tun_id);
392         break;
393
394     case OFPUTIL_NXAST_WRITE_METADATA:
395         error = metadata_from_nxast(&a->write_metadata, out);
396         break;
397
398     case OFPUTIL_NXAST_SET_QUEUE:
399         ofpact_put_SET_QUEUE(out)->queue_id = ntohl(a->set_queue.queue_id);
400         break;
401
402     case OFPUTIL_NXAST_POP_QUEUE:
403         ofpact_put_POP_QUEUE(out);
404         break;
405
406     case OFPUTIL_NXAST_REG_MOVE:
407         error = nxm_reg_move_from_openflow(&a->reg_move, out);
408         break;
409
410     case OFPUTIL_NXAST_REG_LOAD:
411         error = nxm_reg_load_from_openflow(&a->reg_load, out);
412         break;
413
414     case OFPUTIL_NXAST_STACK_PUSH:
415         error = nxm_stack_push_from_openflow(&a->stack, out);
416         break;
417
418     case OFPUTIL_NXAST_STACK_POP:
419         error = nxm_stack_pop_from_openflow(&a->stack, out);
420         break;
421
422     case OFPUTIL_NXAST_NOTE:
423         note_from_openflow(&a->note, out);
424         break;
425
426     case OFPUTIL_NXAST_SET_TUNNEL64:
427         tunnel = ofpact_put_SET_TUNNEL(out);
428         tunnel->ofpact.compat = code;
429         tunnel->tun_id = ntohll(a->set_tunnel64.tun_id);
430         break;
431
432     case OFPUTIL_NXAST_MULTIPATH:
433         error = multipath_from_openflow(&a->multipath,
434                                         ofpact_put_MULTIPATH(out));
435         break;
436
437     case OFPUTIL_NXAST_BUNDLE:
438     case OFPUTIL_NXAST_BUNDLE_LOAD:
439         error = bundle_from_openflow(&a->bundle, out);
440         break;
441
442     case OFPUTIL_NXAST_OUTPUT_REG:
443         error = output_reg_from_openflow(&a->output_reg, out);
444         break;
445
446     case OFPUTIL_NXAST_RESUBMIT_TABLE:
447         error = resubmit_table_from_openflow(&a->resubmit, out);
448         break;
449
450     case OFPUTIL_NXAST_LEARN:
451         error = learn_from_openflow(&a->learn, out);
452         break;
453
454     case OFPUTIL_NXAST_EXIT:
455         ofpact_put_EXIT(out);
456         break;
457
458     case OFPUTIL_NXAST_DEC_TTL:
459         error = dec_ttl_from_openflow(out, code);
460         break;
461
462     case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
463         error = dec_ttl_cnt_ids_from_openflow(&a->cnt_ids, out);
464         break;
465
466     case OFPUTIL_NXAST_FIN_TIMEOUT:
467         fin_timeout_from_openflow(&a->fin_timeout, out);
468         break;
469
470     case OFPUTIL_NXAST_CONTROLLER:
471         controller_from_openflow(&a->controller, out);
472         break;
473
474     case OFPUTIL_NXAST_PUSH_MPLS:
475         error = push_mpls_from_openflow(a->push_mpls.ethertype, out);
476         break;
477
478     case OFPUTIL_NXAST_SET_MPLS_LABEL:
479         ofpact_put_SET_MPLS_LABEL(out)->label = a->mpls_label.label;
480         break;
481
482     case OFPUTIL_NXAST_SET_MPLS_TC:
483         ofpact_put_SET_MPLS_TC(out)->tc = a->mpls_tc.tc;
484         break;
485
486     case OFPUTIL_NXAST_SET_MPLS_TTL:
487         ofpact_put_SET_MPLS_TTL(out)->ttl = a->mpls_ttl.ttl;
488         break;
489
490     case OFPUTIL_NXAST_DEC_MPLS_TTL:
491         ofpact_put_DEC_MPLS_TTL(out);
492         break;
493
494     case OFPUTIL_NXAST_POP_MPLS:
495         ofpact_put_POP_MPLS(out)->ethertype = a->pop_mpls.ethertype;
496         break;
497
498     case OFPUTIL_NXAST_SAMPLE:
499         error = sample_from_openflow(&a->sample, out);
500         break;
501     }
502
503     return error;
504 }
505
506 static enum ofperr
507 ofpact_from_openflow10(const union ofp_action *a,
508                        enum ofp_version version OVS_UNUSED,
509                        struct ofpbuf *out)
510 {
511     enum ofputil_action_code code;
512     enum ofperr error;
513     struct ofpact_vlan_vid *vlan_vid;
514     struct ofpact_vlan_pcp *vlan_pcp;
515
516     error = decode_openflow10_action(a, &code);
517     if (error) {
518         return error;
519     }
520
521     switch (code) {
522     case OFPUTIL_ACTION_INVALID:
523 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
524 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
525 #include "ofp-util.def"
526         OVS_NOT_REACHED();
527
528     case OFPUTIL_OFPAT10_OUTPUT:
529         return output_from_openflow10(&a->output10, out);
530
531     case OFPUTIL_OFPAT10_SET_VLAN_VID:
532         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
533             return OFPERR_OFPBAC_BAD_ARGUMENT;
534         }
535         vlan_vid = ofpact_put_SET_VLAN_VID(out);
536         vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
537         vlan_vid->push_vlan_if_needed = true;
538         vlan_vid->ofpact.compat = code;
539         break;
540
541     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
542         if (a->vlan_pcp.vlan_pcp & ~7) {
543             return OFPERR_OFPBAC_BAD_ARGUMENT;
544         }
545         vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
546         vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
547         vlan_pcp->push_vlan_if_needed = true;
548         vlan_pcp->ofpact.compat = code;
549         break;
550
551     case OFPUTIL_OFPAT10_STRIP_VLAN:
552         ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
553         break;
554
555     case OFPUTIL_OFPAT10_SET_DL_SRC:
556         memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
557                ETH_ADDR_LEN);
558         break;
559
560     case OFPUTIL_OFPAT10_SET_DL_DST:
561         memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
562                ETH_ADDR_LEN);
563         break;
564
565     case OFPUTIL_OFPAT10_SET_NW_SRC:
566         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
567         break;
568
569     case OFPUTIL_OFPAT10_SET_NW_DST:
570         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
571         break;
572
573     case OFPUTIL_OFPAT10_SET_NW_TOS:
574         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
575             return OFPERR_OFPBAC_BAD_ARGUMENT;
576         }
577         ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
578         break;
579
580     case OFPUTIL_OFPAT10_SET_TP_SRC:
581         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
582         break;
583
584     case OFPUTIL_OFPAT10_SET_TP_DST:
585         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
586
587         break;
588
589     case OFPUTIL_OFPAT10_ENQUEUE:
590         error = enqueue_from_openflow10(&a->enqueue, out);
591         break;
592
593 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
594 #include "ofp-util.def"
595         return ofpact_from_nxast(a, code, out);
596     }
597
598     return error;
599 }
600
601 static enum ofperr ofpact_from_openflow11(const union ofp_action *,
602                                           enum ofp_version,
603                                           struct ofpbuf *out);
604
605 static inline union ofp_action *
606 action_next(const union ofp_action *a)
607 {
608     return ((union ofp_action *) (void *)
609             ((uint8_t *) a + ntohs(a->header.len)));
610 }
611
612 static inline bool
613 action_is_valid(const union ofp_action *a, size_t max_actions)
614 {
615     uint16_t len = ntohs(a->header.len);
616     return (!(len % OFP_ACTION_ALIGN)
617             && len >= OFP_ACTION_ALIGN
618             && len / OFP_ACTION_ALIGN <= max_actions);
619 }
620
621 /* This macro is careful to check for actions with bad lengths. */
622 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, MAX_ACTIONS)                 \
623     for ((ITER) = (ACTIONS), (LEFT) = (MAX_ACTIONS);                      \
624          (LEFT) > 0 && action_is_valid(ITER, LEFT);                     \
625          ((LEFT) -= ntohs((ITER)->header.len) / OFP_ACTION_ALIGN, \
626           (ITER) = action_next(ITER)))
627
628 static void
629 log_bad_action(const union ofp_action *actions, size_t max_actions,
630                const union ofp_action *bad_action, enum ofperr error)
631 {
632     if (!VLOG_DROP_WARN(&rl)) {
633         struct ds s;
634
635         ds_init(&s);
636         ds_put_hex_dump(&s, actions, max_actions * OFP_ACTION_ALIGN, 0, false);
637         VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
638                   (char *)bad_action - (char *)actions,
639                   ofperr_get_name(error), ds_cstr(&s));
640         ds_destroy(&s);
641     }
642 }
643
644 static enum ofperr
645 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
646                       enum ofp_version version, struct ofpbuf *out)
647 {
648     const union ofp_action *a;
649     size_t left;
650
651     enum ofperr (*ofpact_from_openflow)(const union ofp_action *a,
652                                         enum ofp_version,
653                                         struct ofpbuf *out) =
654         (version == OFP10_VERSION) ?
655         ofpact_from_openflow10 : ofpact_from_openflow11;
656
657     ACTION_FOR_EACH (a, left, in, n_in) {
658         enum ofperr error = ofpact_from_openflow(a, version, out);
659         if (error) {
660             log_bad_action(in, n_in, a, error);
661             return error;
662         }
663     }
664     if (left) {
665         enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
666         log_bad_action(in, n_in, a, error);
667         return error;
668     }
669
670     ofpact_pad(out);
671     return 0;
672 }
673
674 static enum ofperr
675 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
676                                 unsigned int actions_len,
677                                 enum ofp_version version,
678                                 uint32_t allowed_ovsinsts,
679                                 struct ofpbuf *ofpacts)
680 {
681     const union ofp_action *actions;
682     enum ofperr error;
683
684     ofpbuf_clear(ofpacts);
685
686     if (actions_len % OFP_ACTION_ALIGN != 0) {
687         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
688                      "multiple of %d", actions_len, OFP_ACTION_ALIGN);
689         return OFPERR_OFPBRC_BAD_LEN;
690     }
691
692     actions = ofpbuf_try_pull(openflow, actions_len);
693     if (actions == NULL) {
694         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
695                      "remaining message length (%"PRIu32")",
696                      actions_len, ofpbuf_size(openflow));
697         return OFPERR_OFPBRC_BAD_LEN;
698     }
699
700     error = ofpacts_from_openflow(actions, actions_len / OFP_ACTION_ALIGN,
701                                   version, ofpacts);
702     if (error) {
703         ofpbuf_clear(ofpacts);
704         return error;
705     }
706
707     error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
708                            allowed_ovsinsts);
709     if (error) {
710         ofpbuf_clear(ofpacts);
711     }
712     return error;
713 }
714
715 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
716  * front of 'openflow' into ofpacts.  On success, replaces any existing content
717  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
718  * Returns 0 if successful, otherwise an OpenFlow error.
719  *
720  * Actions are processed according to their OpenFlow version which
721  * is provided in the 'version' parameter.
722  *
723  * In most places in OpenFlow, actions appear encapsulated in instructions, so
724  * you should call ofpacts_pull_openflow_instructions() instead of this
725  * function.
726  *
727  * The parsed actions are valid generically, but they may not be valid in a
728  * specific context.  For example, port numbers up to OFPP_MAX are valid
729  * generically, but specific datapaths may only support port numbers in a
730  * smaller range.  Use ofpacts_check() to additional check whether actions are
731  * valid in a specific context. */
732 enum ofperr
733 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
734                               unsigned int actions_len,
735                               enum ofp_version version,
736                               struct ofpbuf *ofpacts)
737 {
738     return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
739                                            1u << OVSINST_OFPIT11_APPLY_ACTIONS,
740                                            ofpacts);
741 }
742 \f
743 /* OpenFlow 1.1 actions. */
744
745 /* Parses 'a' to determine its type.  On success stores the correct type into
746  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
747  * '*code' is indeterminate.
748  *
749  * The caller must have already verified that 'a''s length is potentially
750  * correct (that is, a->header.len is nonzero and a multiple of
751  * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
752  *
753  * This function verifies that 'a''s length is correct for the type of action
754  * that it represents. */
755 static enum ofperr
756 decode_openflow11_action(const union ofp_action *a,
757                          enum ofputil_action_code *code)
758 {
759     uint16_t len;
760
761     switch (a->type) {
762     case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
763         return decode_nxast_action(a, code);
764
765 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
766         case CONSTANT_HTONS(ENUM):                      \
767             len = ntohs(a->header.len);                 \
768             if (EXTENSIBLE                              \
769                 ? len >= sizeof(struct STRUCT)          \
770                 : len == sizeof(struct STRUCT)) {       \
771                 *code = OFPUTIL_##ENUM;                 \
772                 return 0;                               \
773             } else {                                    \
774                 return OFPERR_OFPBAC_BAD_LEN;           \
775             }                                           \
776             OVS_NOT_REACHED();
777 #include "ofp-util.def"
778
779     default:
780         return OFPERR_OFPBAC_BAD_TYPE;
781     }
782 }
783
784 static enum ofperr
785 set_field_from_openflow(const struct ofp12_action_set_field *oasf,
786                         struct ofpbuf *ofpacts)
787 {
788     uint16_t oasf_len = ntohs(oasf->len);
789     uint32_t oxm_header = ntohl(oasf->dst);
790     uint8_t oxm_length = NXM_LENGTH(oxm_header);
791     struct ofpact_set_field *sf;
792     const struct mf_field *mf;
793
794     /* ofp12_action_set_field is padded to 64 bits by zero */
795     if (oasf_len != ROUND_UP(sizeof *oasf + oxm_length, 8)) {
796         return OFPERR_OFPBAC_BAD_SET_LEN;
797     }
798     if (!is_all_zeros((const uint8_t *)oasf + sizeof *oasf + oxm_length,
799                       oasf_len - oxm_length - sizeof *oasf)) {
800         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
801     }
802
803     if (NXM_HASMASK(oxm_header)) {
804         return OFPERR_OFPBAC_BAD_SET_TYPE;
805     }
806     mf = mf_from_nxm_header(oxm_header);
807     if (!mf) {
808         return OFPERR_OFPBAC_BAD_SET_TYPE;
809     }
810     ovs_assert(mf->n_bytes == oxm_length);
811     /* oxm_length is now validated to be compatible with mf_value. */
812     if (!mf->writable) {
813         VLOG_WARN_RL(&rl, "destination field %s is not writable", mf->name);
814         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
815     }
816     sf = ofpact_put_SET_FIELD(ofpacts);
817     sf->field = mf;
818     memcpy(&sf->value, oasf + 1, mf->n_bytes);
819
820     /* The value must be valid for match and must have the OFPVID_PRESENT bit
821      * on for OXM_OF_VLAN_VID. */
822     if (!mf_is_value_valid(mf, &sf->value)
823         || (mf->id == MFF_VLAN_VID
824             && !(sf->value.be16 & htons(OFPVID12_PRESENT)))) {
825         struct ds ds = DS_EMPTY_INITIALIZER;
826         mf_format(mf, &sf->value, NULL, &ds);
827         VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
828                      mf->name, ds_cstr(&ds));
829         ds_destroy(&ds);
830
831         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
832     }
833     return 0;
834 }
835
836 static void
837 set_field_to_openflow12(const struct ofpact_set_field *sf,
838                         struct ofpbuf *openflow,
839                         enum ofp_version version)
840 {
841     uint16_t padded_value_len = ROUND_UP(sf->field->n_bytes, 8);
842     struct ofp12_action_set_field *oasf;
843     char *value;
844
845     oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
846     oasf->dst = htonl(mf_oxm_header(sf->field->id, version));
847     oasf->len = htons(sizeof *oasf + padded_value_len);
848
849     value = ofpbuf_put_zeros(openflow, padded_value_len);
850     memcpy(value, &sf->value, sf->field->n_bytes);
851 }
852
853 /* Convert 'sf' to one or two REG_LOADs. */
854 static void
855 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
856 {
857     const struct mf_field *mf = sf->field;
858     struct nx_action_reg_load *narl;
859
860     if (mf->n_bits > 64) {
861         ovs_assert(mf->n_bytes == 16); /* IPv6 addr. */
862         /* Split into 64bit chunks */
863         /* Lower bits first. */
864         narl = ofputil_put_NXAST_REG_LOAD(openflow);
865         narl->ofs_nbits = nxm_encode_ofs_nbits(0, 64);
866         narl->dst = htonl(mf->nxm_header);
867         memcpy(&narl->value, &sf->value.ipv6.s6_addr[8], sizeof narl->value);
868         /* Higher bits next. */
869         narl = ofputil_put_NXAST_REG_LOAD(openflow);
870         narl->ofs_nbits = nxm_encode_ofs_nbits(64, mf->n_bits - 64);
871         narl->dst = htonl(mf->nxm_header);
872         memcpy(&narl->value, &sf->value.ipv6.s6_addr[0], sizeof narl->value);
873     } else {
874         narl = ofputil_put_NXAST_REG_LOAD(openflow);
875         narl->ofs_nbits = nxm_encode_ofs_nbits(0, mf->n_bits);
876         narl->dst = htonl(mf->nxm_header);
877         memset(&narl->value, 0, 8 - mf->n_bytes);
878         memcpy((char*)&narl->value + (8 - mf->n_bytes),
879                &sf->value, mf->n_bytes);
880     }
881 }
882
883 /* Convert 'sf' to standard OpenFlow 1.1 actions, if we can, falling back
884  * to Nicira extensions if we must.
885  *
886  * We check only meta-flow types that can appear within set field actions and
887  * that have a mapping to compatible action types.  These struct mf_field
888  * definitions have a defined OXM or NXM header value and specify the field as
889  * writable. */
890 static void
891 set_field_to_openflow11(const struct ofpact_set_field *sf,
892                         struct ofpbuf *openflow)
893 {
894     switch ((int) sf->field->id) {
895     case MFF_VLAN_TCI:
896         /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
897          *
898          * If CFI=1, Add or modify VLAN VID & PCP.
899          *    OpenFlow 1.1 set actions only apply if the packet
900          *    already has VLAN tags.  To be sure that is the case
901          *    we have to push a VLAN header.  As we do not support
902          *    multiple layers of VLANs, this is a no-op, if a VLAN
903          *    header already exists.  This may backfire, however,
904          *    when we start supporting multiple layers of VLANs.
905          * If CFI=0, strip VLAN header, if any.
906          */
907         if (sf->value.be16 & htons(VLAN_CFI)) {
908             /* Push a VLAN tag, if one was not seen at action validation
909              * time. */
910             if (!sf->flow_has_vlan) {
911                 ofputil_put_OFPAT11_PUSH_VLAN(openflow)->ethertype
912                     = htons(ETH_TYPE_VLAN_8021Q);
913             }
914             ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
915                 = sf->value.be16 & htons(VLAN_VID_MASK);
916             ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp
917                 = vlan_tci_to_pcp(sf->value.be16);
918         } else {
919             /* If the flow did not match on vlan, we have no way of
920              * knowing if the vlan tag exists, so we must POP just to be
921              * sure. */
922             ofputil_put_OFPAT11_POP_VLAN(openflow);
923         }
924         break;
925
926     case MFF_VLAN_VID:
927         /* OXM VLAN_PCP to OpenFlow 1.1.
928          * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
929          * tag.  Clear the OFPVID_PRESENT bit.
930          */
931         ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
932             = sf->value.be16 & htons(VLAN_VID_MASK);
933         break;
934
935     case MFF_VLAN_PCP:
936         /* OXM VLAN_PCP to OpenFlow 1.1.
937          * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
938         ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
939         break;
940
941     case MFF_ETH_SRC:
942         memcpy(ofputil_put_OFPAT11_SET_DL_SRC(openflow)->dl_addr,
943                sf->value.mac, ETH_ADDR_LEN);
944         break;
945
946     case MFF_ETH_DST:
947         memcpy(ofputil_put_OFPAT11_SET_DL_DST(openflow)->dl_addr,
948                sf->value.mac, ETH_ADDR_LEN);
949         break;
950
951     case MFF_MPLS_LABEL:
952         ofputil_put_OFPAT11_SET_MPLS_LABEL(openflow)->mpls_label =
953             sf->value.be32;
954         break;
955
956     case MFF_MPLS_TC:
957         ofputil_put_OFPAT11_SET_MPLS_TC(openflow)->mpls_tc = sf->value.u8;
958         break;
959
960     case MFF_IPV4_SRC:
961         ofputil_put_OFPAT11_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
962         break;
963
964     case MFF_IPV4_DST:
965         ofputil_put_OFPAT11_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
966         break;
967
968     case MFF_IP_DSCP:
969         ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
970         break;
971
972     case MFF_IP_DSCP_SHIFTED:
973         ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
974         break;
975
976     case MFF_IP_ECN:
977         ofputil_put_OFPAT11_SET_NW_ECN(openflow)->nw_ecn = sf->value.u8;
978         break;
979
980     case MFF_IP_TTL:
981         ofputil_put_OFPAT11_SET_NW_TTL(openflow)->nw_ttl = sf->value.u8;
982         break;
983
984     case MFF_TCP_SRC:
985     case MFF_UDP_SRC:
986     case MFF_SCTP_SRC:
987         ofputil_put_OFPAT11_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
988         break;
989
990     case MFF_TCP_DST:
991     case MFF_UDP_DST:
992     case MFF_SCTP_DST:
993         ofputil_put_OFPAT11_SET_TP_DST(openflow)->tp_port = sf->value.be16;
994         break;
995
996     default:
997         set_field_to_nxast(sf, openflow);
998         break;
999     }
1000 }
1001
1002 /* Convert 'sf' to standard OpenFlow 1.0 actions, if we can, falling back
1003  * to Nicira extensions if we must.
1004  *
1005  * We check only meta-flow types that can appear within set field actions and
1006  * that have a mapping to compatible action types.  These struct mf_field
1007  * definitions have a defined OXM or NXM header value and specify the field as
1008  * writable. */
1009 static void
1010 set_field_to_openflow10(const struct ofpact_set_field *sf,
1011                         struct ofpbuf *openflow)
1012 {
1013     switch ((int) sf->field->id) {
1014     case MFF_VLAN_TCI:
1015         /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
1016          *
1017          * If CFI=1, Add or modify VLAN VID & PCP.
1018          * If CFI=0, strip VLAN header, if any.
1019          */
1020         if (sf->value.be16 & htons(VLAN_CFI)) {
1021             ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1022                 = sf->value.be16 & htons(VLAN_VID_MASK);
1023             ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp
1024                 = vlan_tci_to_pcp(sf->value.be16);
1025         } else {
1026             ofputil_put_OFPAT10_STRIP_VLAN(openflow);
1027         }
1028         break;
1029
1030     case MFF_VLAN_VID:
1031         /* OXM VLAN_VID to OpenFlow 1.0.
1032          * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
1033          * tag.  Clear the OFPVID_PRESENT bit.
1034          */
1035         ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1036             = sf->value.be16 & htons(VLAN_VID_MASK);
1037         break;
1038
1039     case MFF_VLAN_PCP:
1040         /* OXM VLAN_PCP to OpenFlow 1.0.
1041          * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
1042         ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
1043         break;
1044
1045     case MFF_ETH_SRC:
1046         memcpy(ofputil_put_OFPAT10_SET_DL_SRC(openflow)->dl_addr,
1047                sf->value.mac, ETH_ADDR_LEN);
1048         break;
1049
1050     case MFF_ETH_DST:
1051         memcpy(ofputil_put_OFPAT10_SET_DL_DST(openflow)->dl_addr,
1052                sf->value.mac, ETH_ADDR_LEN);
1053         break;
1054
1055     case MFF_IPV4_SRC:
1056         ofputil_put_OFPAT10_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
1057         break;
1058
1059     case MFF_IPV4_DST:
1060         ofputil_put_OFPAT10_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
1061         break;
1062
1063     case MFF_IP_DSCP:
1064         ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
1065         break;
1066
1067     case MFF_IP_DSCP_SHIFTED:
1068         ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
1069         break;
1070
1071     case MFF_TCP_SRC:
1072     case MFF_UDP_SRC:
1073         ofputil_put_OFPAT10_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
1074         break;
1075
1076     case MFF_TCP_DST:
1077     case MFF_UDP_DST:
1078         ofputil_put_OFPAT10_SET_TP_DST(openflow)->tp_port = sf->value.be16;
1079         break;
1080
1081     default:
1082         set_field_to_nxast(sf, openflow);
1083         break;
1084     }
1085 }
1086
1087 static void
1088 set_field_to_openflow(const struct ofpact_set_field *sf,
1089                       struct ofpbuf *openflow)
1090 {
1091     struct ofp_header *oh = (struct ofp_header *)openflow->frame;
1092
1093     if (oh->version >= OFP12_VERSION) {
1094         set_field_to_openflow12(sf, openflow, oh->version);
1095     } else if (oh->version == OFP11_VERSION) {
1096         set_field_to_openflow11(sf, openflow);
1097     } else if (oh->version == OFP10_VERSION) {
1098         set_field_to_openflow10(sf, openflow);
1099     } else {
1100         OVS_NOT_REACHED();
1101     }
1102 }
1103
1104 static enum ofperr
1105 output_from_openflow11(const struct ofp11_action_output *oao,
1106                        struct ofpbuf *out)
1107 {
1108     struct ofpact_output *output;
1109     enum ofperr error;
1110
1111     output = ofpact_put_OUTPUT(out);
1112     output->max_len = ntohs(oao->max_len);
1113
1114     error = ofputil_port_from_ofp11(oao->port, &output->port);
1115     if (error) {
1116         return error;
1117     }
1118
1119     return ofpact_check_output_port(output->port, OFPP_MAX);
1120 }
1121
1122 static enum ofperr
1123 ofpact_from_openflow11(const union ofp_action *a, enum ofp_version version,
1124                        struct ofpbuf *out)
1125 {
1126     enum ofputil_action_code code;
1127     enum ofperr error;
1128     struct ofpact_vlan_vid *vlan_vid;
1129     struct ofpact_vlan_pcp *vlan_pcp;
1130
1131     error = decode_openflow11_action(a, &code);
1132     if (error) {
1133         return error;
1134     }
1135
1136     if (version >= OFP12_VERSION) {
1137         switch ((int)code) {
1138         case OFPUTIL_OFPAT11_SET_VLAN_VID:
1139         case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1140         case OFPUTIL_OFPAT11_SET_DL_SRC:
1141         case OFPUTIL_OFPAT11_SET_DL_DST:
1142         case OFPUTIL_OFPAT11_SET_NW_SRC:
1143         case OFPUTIL_OFPAT11_SET_NW_DST:
1144         case OFPUTIL_OFPAT11_SET_NW_TOS:
1145         case OFPUTIL_OFPAT11_SET_NW_ECN:
1146         case OFPUTIL_OFPAT11_SET_TP_SRC:
1147         case OFPUTIL_OFPAT11_SET_TP_DST:
1148             VLOG_WARN_RL(&rl, "Deprecated action %s received over %s",
1149                          ofputil_action_name_from_code(code),
1150                          ofputil_version_to_string(version));
1151         }
1152     }
1153
1154     switch (code) {
1155     case OFPUTIL_ACTION_INVALID:
1156 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
1157 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
1158 #include "ofp-util.def"
1159         OVS_NOT_REACHED();
1160
1161     case OFPUTIL_OFPAT11_OUTPUT:
1162         return output_from_openflow11(&a->ofp11_output, out);
1163
1164     case OFPUTIL_OFPAT11_SET_VLAN_VID:
1165         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1166             return OFPERR_OFPBAC_BAD_ARGUMENT;
1167         }
1168         vlan_vid = ofpact_put_SET_VLAN_VID(out);
1169         vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
1170         vlan_vid->push_vlan_if_needed = false;
1171         vlan_vid->ofpact.compat = code;
1172         break;
1173
1174     case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1175         if (a->vlan_pcp.vlan_pcp & ~7) {
1176             return OFPERR_OFPBAC_BAD_ARGUMENT;
1177         }
1178         vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1179         vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
1180         vlan_pcp->push_vlan_if_needed = false;
1181         vlan_pcp->ofpact.compat = code;
1182         break;
1183
1184     case OFPUTIL_OFPAT11_PUSH_VLAN:
1185         if (a->push.ethertype != htons(ETH_TYPE_VLAN_8021Q)) {
1186             /* XXX 802.1AD(QinQ) isn't supported at the moment */
1187             return OFPERR_OFPBAC_BAD_ARGUMENT;
1188         }
1189         ofpact_put_PUSH_VLAN(out);
1190         break;
1191
1192     case OFPUTIL_OFPAT11_POP_VLAN:
1193         ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
1194         break;
1195
1196     case OFPUTIL_OFPAT11_SET_QUEUE:
1197         ofpact_put_SET_QUEUE(out)->queue_id =
1198             ntohl(a->ofp11_set_queue.queue_id);
1199         break;
1200
1201     case OFPUTIL_OFPAT11_SET_DL_SRC:
1202         memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
1203                ETH_ADDR_LEN);
1204         break;
1205
1206     case OFPUTIL_OFPAT11_SET_DL_DST:
1207         memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
1208                ETH_ADDR_LEN);
1209         break;
1210
1211     case OFPUTIL_OFPAT11_DEC_NW_TTL:
1212         dec_ttl_from_openflow(out, code);
1213         break;
1214
1215     case OFPUTIL_OFPAT11_SET_NW_SRC:
1216         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
1217         break;
1218
1219     case OFPUTIL_OFPAT11_SET_NW_DST:
1220         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
1221         break;
1222
1223     case OFPUTIL_OFPAT11_SET_NW_TOS:
1224         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
1225             return OFPERR_OFPBAC_BAD_ARGUMENT;
1226         }
1227         ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
1228         break;
1229
1230     case OFPUTIL_OFPAT11_SET_NW_ECN:
1231         if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
1232             return OFPERR_OFPBAC_BAD_ARGUMENT;
1233         }
1234         ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
1235         break;
1236
1237     case OFPUTIL_OFPAT11_SET_NW_TTL:
1238         ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
1239         break;
1240
1241     case OFPUTIL_OFPAT11_SET_TP_SRC:
1242         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
1243         break;
1244
1245     case OFPUTIL_OFPAT11_SET_TP_DST:
1246         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
1247         break;
1248
1249     case OFPUTIL_OFPAT12_SET_FIELD:
1250         return set_field_from_openflow(&a->set_field, out);
1251
1252     case OFPUTIL_OFPAT11_SET_MPLS_LABEL:
1253         ofpact_put_SET_MPLS_LABEL(out)->label = a->ofp11_mpls_label.mpls_label;
1254         break;
1255
1256     case OFPUTIL_OFPAT11_SET_MPLS_TC:
1257         ofpact_put_SET_MPLS_TC(out)->tc = a->ofp11_mpls_tc.mpls_tc;
1258         break;
1259
1260     case OFPUTIL_OFPAT11_SET_MPLS_TTL:
1261         ofpact_put_SET_MPLS_TTL(out)->ttl = a->ofp11_mpls_ttl.mpls_ttl;
1262         break;
1263
1264     case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
1265         ofpact_put_DEC_MPLS_TTL(out);
1266         break;
1267
1268     case OFPUTIL_OFPAT11_PUSH_MPLS:
1269         error = push_mpls_from_openflow(a->push.ethertype, out);
1270         break;
1271
1272     case OFPUTIL_OFPAT11_POP_MPLS:
1273         ofpact_put_POP_MPLS(out)->ethertype = a->ofp11_pop_mpls.ethertype;
1274         break;
1275
1276     case OFPUTIL_OFPAT11_GROUP:
1277         ofpact_put_GROUP(out)->group_id = ntohl(a->group.group_id);
1278         break;
1279
1280 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
1281 #include "ofp-util.def"
1282         return ofpact_from_nxast(a, code, out);
1283     }
1284
1285     return error;
1286 }
1287
1288 /* True if an action sets the value of a field
1289  * in a way that is compatibile with the action set.
1290  * False otherwise. */
1291 static bool
1292 ofpact_is_set_action(const struct ofpact *a)
1293 {
1294     switch (a->type) {
1295     case OFPACT_SET_FIELD:
1296     case OFPACT_REG_LOAD:
1297     case OFPACT_SET_ETH_DST:
1298     case OFPACT_SET_ETH_SRC:
1299     case OFPACT_SET_IP_DSCP:
1300     case OFPACT_SET_IP_ECN:
1301     case OFPACT_SET_IP_TTL:
1302     case OFPACT_SET_IPV4_DST:
1303     case OFPACT_SET_IPV4_SRC:
1304     case OFPACT_SET_L4_DST_PORT:
1305     case OFPACT_SET_L4_SRC_PORT:
1306     case OFPACT_SET_MPLS_LABEL:
1307     case OFPACT_SET_MPLS_TC:
1308     case OFPACT_SET_MPLS_TTL:
1309     case OFPACT_SET_QUEUE:
1310     case OFPACT_SET_TUNNEL:
1311     case OFPACT_SET_VLAN_PCP:
1312     case OFPACT_SET_VLAN_VID:
1313         return true;
1314     case OFPACT_BUNDLE:
1315     case OFPACT_CLEAR_ACTIONS:
1316     case OFPACT_CONTROLLER:
1317     case OFPACT_DEC_MPLS_TTL:
1318     case OFPACT_DEC_TTL:
1319     case OFPACT_ENQUEUE:
1320     case OFPACT_EXIT:
1321     case OFPACT_FIN_TIMEOUT:
1322     case OFPACT_GOTO_TABLE:
1323     case OFPACT_GROUP:
1324     case OFPACT_LEARN:
1325     case OFPACT_METER:
1326     case OFPACT_MULTIPATH:
1327     case OFPACT_NOTE:
1328     case OFPACT_OUTPUT:
1329     case OFPACT_OUTPUT_REG:
1330     case OFPACT_POP_MPLS:
1331     case OFPACT_POP_QUEUE:
1332     case OFPACT_PUSH_MPLS:
1333     case OFPACT_PUSH_VLAN:
1334     case OFPACT_REG_MOVE:
1335     case OFPACT_RESUBMIT:
1336     case OFPACT_SAMPLE:
1337     case OFPACT_STACK_POP:
1338     case OFPACT_STACK_PUSH:
1339     case OFPACT_STRIP_VLAN:
1340     case OFPACT_WRITE_ACTIONS:
1341     case OFPACT_WRITE_METADATA:
1342         return false;
1343     default:
1344         OVS_NOT_REACHED();
1345     }
1346 }
1347
1348 /* True if an action is allowed in the action set.
1349  * False otherwise. */
1350 static bool
1351 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
1352 {
1353     switch (a->type) {
1354     case OFPACT_DEC_MPLS_TTL:
1355     case OFPACT_DEC_TTL:
1356     case OFPACT_GROUP:
1357     case OFPACT_OUTPUT:
1358     case OFPACT_POP_MPLS:
1359     case OFPACT_PUSH_MPLS:
1360     case OFPACT_PUSH_VLAN:
1361     case OFPACT_REG_LOAD:
1362     case OFPACT_SET_FIELD:
1363     case OFPACT_SET_ETH_DST:
1364     case OFPACT_SET_ETH_SRC:
1365     case OFPACT_SET_IP_DSCP:
1366     case OFPACT_SET_IP_ECN:
1367     case OFPACT_SET_IP_TTL:
1368     case OFPACT_SET_IPV4_DST:
1369     case OFPACT_SET_IPV4_SRC:
1370     case OFPACT_SET_L4_DST_PORT:
1371     case OFPACT_SET_L4_SRC_PORT:
1372     case OFPACT_SET_MPLS_LABEL:
1373     case OFPACT_SET_MPLS_TC:
1374     case OFPACT_SET_MPLS_TTL:
1375     case OFPACT_SET_QUEUE:
1376     case OFPACT_SET_TUNNEL:
1377     case OFPACT_SET_VLAN_PCP:
1378     case OFPACT_SET_VLAN_VID:
1379     case OFPACT_STRIP_VLAN:
1380         return true;
1381
1382     /* In general these actions are excluded because they are not part of
1383      * the OpenFlow specification nor map to actions that are defined in
1384      * the specification.  Thus the order in which they should be applied
1385      * in the action set is undefined. */
1386     case OFPACT_BUNDLE:
1387     case OFPACT_CONTROLLER:
1388     case OFPACT_ENQUEUE:
1389     case OFPACT_EXIT:
1390     case OFPACT_FIN_TIMEOUT:
1391     case OFPACT_LEARN:
1392     case OFPACT_MULTIPATH:
1393     case OFPACT_NOTE:
1394     case OFPACT_OUTPUT_REG:
1395     case OFPACT_POP_QUEUE:
1396     case OFPACT_REG_MOVE:
1397     case OFPACT_RESUBMIT:
1398     case OFPACT_SAMPLE:
1399     case OFPACT_STACK_POP:
1400     case OFPACT_STACK_PUSH:
1401
1402     /* The action set may only include actions and thus
1403      * may not include any instructions */
1404     case OFPACT_CLEAR_ACTIONS:
1405     case OFPACT_GOTO_TABLE:
1406     case OFPACT_METER:
1407     case OFPACT_WRITE_ACTIONS:
1408     case OFPACT_WRITE_METADATA:
1409         return false;
1410     default:
1411         OVS_NOT_REACHED();
1412     }
1413 }
1414
1415 /* Append ofpact 'a' onto the tail of 'out' */
1416 static void
1417 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1418 {
1419     ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1420 }
1421
1422 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1423 static bool
1424 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1425                   enum ofpact_type filter)
1426 {
1427     const struct ofpact *target;
1428     const struct ofpact *a;
1429
1430     target = NULL;
1431     OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
1432         if (a->type == filter) {
1433             target = a;
1434         }
1435     }
1436     if (target) {
1437         ofpact_copy(out, target);
1438     }
1439     return target != NULL;
1440 }
1441
1442 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1443  * The order of appended ofpacts is preserved between 'in' and 'out' */
1444 static void
1445 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1446                  bool (*filter)(const struct ofpact *))
1447 {
1448     const struct ofpact *a;
1449
1450     OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
1451         if (filter(a)) {
1452             ofpact_copy(out, a);
1453         }
1454     }
1455 }
1456
1457 /* Reads 'action_set', which contains ofpacts accumulated by
1458  * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1459  * executed directly into 'action_list'.  (These names correspond to the
1460  * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1461  *
1462  * In general this involves appending the last instance of each action that is
1463  * adimissible in the action set in the order described in the OpenFlow
1464  * specification.
1465  *
1466  * Exceptions:
1467  * + output action is only appended if no group action was present in 'in'.
1468  * + As a simplification all set actions are copied in the order the are
1469  *   provided in 'in' as many set actions applied to a field has the same
1470  *   affect as only applying the last action that sets a field and
1471  *   duplicates are removed by do_xlate_actions().
1472  *   This has an unwanted side-effect of compsoting multiple
1473  *   LOAD_REG actions that touch different regions of the same field. */
1474 void
1475 ofpacts_execute_action_set(struct ofpbuf *action_list,
1476                            const struct ofpbuf *action_set)
1477 {
1478     /* The OpenFlow spec "Action Set" section specifies this order. */
1479     ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1480     ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1481     ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1482     ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1483     ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1484     ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1485     ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1486     ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1487
1488     /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1489      * we should execute only OFPACT_GROUP.
1490      *
1491      * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1492      * all the actions because there's no point in modifying a packet that will
1493      * not be sent anywhere. */
1494     if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1495         !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1496         ofpbuf_clear(action_list);
1497     }
1498 }
1499
1500
1501 static enum ofperr
1502 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1503                                        size_t n_in, enum ofp_version version,
1504                                        struct ofpbuf *out)
1505 {
1506     enum ofperr error;
1507     struct ofpact *a;
1508     size_t start = ofpbuf_size(out);
1509
1510     error = ofpacts_from_openflow(in, n_in, version, out);
1511
1512     if (error) {
1513         return error;
1514     }
1515
1516     OFPACT_FOR_EACH (a, ofpact_end(ofpbuf_data(out), start), ofpbuf_size(out) - start) {
1517         if (!ofpact_is_allowed_in_actions_set(a)) {
1518             VLOG_WARN_RL(&rl, "disallowed action in action set");
1519             return OFPERR_OFPBAC_BAD_TYPE;
1520         }
1521     }
1522
1523     return 0;
1524 }
1525
1526 \f
1527 /* OpenFlow 1.1 instructions. */
1528
1529 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
1530     static inline const struct STRUCT * OVS_UNUSED              \
1531     instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1532     {                                                           \
1533         ovs_assert(inst->type == htons(ENUM));                  \
1534         return ALIGNED_CAST(struct STRUCT *, inst);             \
1535     }                                                           \
1536                                                                 \
1537     static inline void OVS_UNUSED                               \
1538     instruction_init_##ENUM(struct STRUCT *s)                   \
1539     {                                                           \
1540         memset(s, 0, sizeof *s);                                \
1541         s->type = htons(ENUM);                                  \
1542         s->len = htons(sizeof *s);                              \
1543     }                                                           \
1544                                                                 \
1545     static inline struct STRUCT * OVS_UNUSED                    \
1546     instruction_put_##ENUM(struct ofpbuf *buf)                  \
1547     {                                                           \
1548         struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
1549         instruction_init_##ENUM(s);                             \
1550         return s;                                               \
1551     }
1552 OVS_INSTRUCTIONS
1553 #undef DEFINE_INST
1554
1555 struct instruction_type_info {
1556     enum ovs_instruction_type type;
1557     const char *name;
1558 };
1559
1560 static const struct instruction_type_info inst_info[] = {
1561 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
1562 OVS_INSTRUCTIONS
1563 #undef DEFINE_INST
1564 };
1565
1566 const char *
1567 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1568 {
1569     return inst_info[type].name;
1570 }
1571
1572 int
1573 ovs_instruction_type_from_name(const char *name)
1574 {
1575     const struct instruction_type_info *p;
1576     for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1577         if (!strcasecmp(name, p->name)) {
1578             return p->type;
1579         }
1580     }
1581     return -1;
1582 }
1583
1584 enum ovs_instruction_type
1585 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1586 {
1587     switch (type) {
1588     case OFPACT_METER:
1589         return OVSINST_OFPIT13_METER;
1590     case OFPACT_CLEAR_ACTIONS:
1591         return OVSINST_OFPIT11_CLEAR_ACTIONS;
1592     case OFPACT_WRITE_ACTIONS:
1593         return OVSINST_OFPIT11_WRITE_ACTIONS;
1594     case OFPACT_WRITE_METADATA:
1595         return OVSINST_OFPIT11_WRITE_METADATA;
1596     case OFPACT_GOTO_TABLE:
1597         return OVSINST_OFPIT11_GOTO_TABLE;
1598     case OFPACT_OUTPUT:
1599     case OFPACT_GROUP:
1600     case OFPACT_CONTROLLER:
1601     case OFPACT_ENQUEUE:
1602     case OFPACT_OUTPUT_REG:
1603     case OFPACT_BUNDLE:
1604     case OFPACT_SET_VLAN_VID:
1605     case OFPACT_SET_VLAN_PCP:
1606     case OFPACT_STRIP_VLAN:
1607     case OFPACT_PUSH_VLAN:
1608     case OFPACT_SET_ETH_SRC:
1609     case OFPACT_SET_ETH_DST:
1610     case OFPACT_SET_IPV4_SRC:
1611     case OFPACT_SET_IPV4_DST:
1612     case OFPACT_SET_IP_DSCP:
1613     case OFPACT_SET_IP_ECN:
1614     case OFPACT_SET_IP_TTL:
1615     case OFPACT_SET_L4_SRC_PORT:
1616     case OFPACT_SET_L4_DST_PORT:
1617     case OFPACT_REG_MOVE:
1618     case OFPACT_REG_LOAD:
1619     case OFPACT_SET_FIELD:
1620     case OFPACT_STACK_PUSH:
1621     case OFPACT_STACK_POP:
1622     case OFPACT_DEC_TTL:
1623     case OFPACT_SET_MPLS_LABEL:
1624     case OFPACT_SET_MPLS_TC:
1625     case OFPACT_SET_MPLS_TTL:
1626     case OFPACT_DEC_MPLS_TTL:
1627     case OFPACT_PUSH_MPLS:
1628     case OFPACT_POP_MPLS:
1629     case OFPACT_SET_TUNNEL:
1630     case OFPACT_SET_QUEUE:
1631     case OFPACT_POP_QUEUE:
1632     case OFPACT_FIN_TIMEOUT:
1633     case OFPACT_RESUBMIT:
1634     case OFPACT_LEARN:
1635     case OFPACT_MULTIPATH:
1636     case OFPACT_NOTE:
1637     case OFPACT_EXIT:
1638     case OFPACT_SAMPLE:
1639     default:
1640         return OVSINST_OFPIT11_APPLY_ACTIONS;
1641     }
1642 }
1643
1644 enum ofperr
1645 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
1646                                     const uint16_t inst_type)
1647 {
1648     switch (inst_type) {
1649
1650 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1651     case ENUM:                                      \
1652         *instruction_type = OVSINST_##ENUM;         \
1653         return 0;
1654 OVS_INSTRUCTIONS
1655 #undef DEFINE_INST
1656
1657     default:
1658         return OFPERR_OFPBIC_UNKNOWN_INST;
1659     }
1660 }
1661
1662 /* Two-way translation between OVS's internal "OVSINST_*" representation of
1663  * instructions and the "OFPIT_*" representation used in OpenFlow. */
1664 struct ovsinst_map {
1665     enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
1666     int ofpit;                         /* OFPIT_* number from OpenFlow spec. */
1667 };
1668
1669 static const struct ovsinst_map *
1670 get_ovsinst_map(enum ofp_version version)
1671 {
1672     /* OpenFlow 1.1 and 1.2 instructions. */
1673     static const struct ovsinst_map of11[] = {
1674         { OVSINST_OFPIT11_GOTO_TABLE, 1 },
1675         { OVSINST_OFPIT11_WRITE_METADATA, 2 },
1676         { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
1677         { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
1678         { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
1679         { 0, -1 },
1680     };
1681
1682     /* OpenFlow 1.3+ instructions. */
1683     static const struct ovsinst_map of13[] = {
1684         { OVSINST_OFPIT11_GOTO_TABLE, 1 },
1685         { OVSINST_OFPIT11_WRITE_METADATA, 2 },
1686         { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
1687         { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
1688         { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
1689         { OVSINST_OFPIT13_METER, 6 },
1690         { 0, -1 },
1691     };
1692
1693     return version < OFP13_VERSION ? of11 : of13;
1694 }
1695
1696 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
1697  * values, into a bitmap of instructions suitable for OpenFlow 'version'
1698  * (OFP11_VERSION or later), and returns the result. */
1699 ovs_be32
1700 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
1701 {
1702     uint32_t ofpit_bitmap = 0;
1703     const struct ovsinst_map *x;
1704
1705     for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
1706         if (ovsinst_bitmap & (1u << x->ovsinst)) {
1707             ofpit_bitmap |= 1u << x->ofpit;
1708         }
1709     }
1710     return htonl(ofpit_bitmap);
1711 }
1712
1713 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
1714  * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
1715  * correspond to OVSINST_* values, and returns the result. */
1716 uint32_t
1717 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap_, enum ofp_version version)
1718 {
1719     uint32_t ofpit_bitmap = ntohl(ofpit_bitmap_);
1720     uint32_t ovsinst_bitmap = 0;
1721     const struct ovsinst_map *x;
1722
1723     for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
1724         if (ofpit_bitmap & (1u << x->ofpit)) {
1725             ovsinst_bitmap |= 1u << x->ovsinst;
1726         }
1727     }
1728     return ovsinst_bitmap;
1729 }
1730
1731 static inline struct ofp11_instruction *
1732 instruction_next(const struct ofp11_instruction *inst)
1733 {
1734     return ((struct ofp11_instruction *) (void *)
1735             ((uint8_t *) inst + ntohs(inst->len)));
1736 }
1737
1738 static inline bool
1739 instruction_is_valid(const struct ofp11_instruction *inst,
1740                      size_t n_instructions)
1741 {
1742     uint16_t len = ntohs(inst->len);
1743     return (!(len % OFP11_INSTRUCTION_ALIGN)
1744             && len >= sizeof *inst
1745             && len / sizeof *inst <= n_instructions);
1746 }
1747
1748 /* This macro is careful to check for instructions with bad lengths. */
1749 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS)  \
1750     for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS);            \
1751          (LEFT) > 0 && instruction_is_valid(ITER, LEFT);                \
1752          ((LEFT) -= (ntohs((ITER)->len)                                 \
1753                      / sizeof(struct ofp11_instruction)),               \
1754           (ITER) = instruction_next(ITER)))
1755
1756 static enum ofperr
1757 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1758                               enum ovs_instruction_type *type)
1759 {
1760     uint16_t len = ntohs(inst->len);
1761
1762     switch (inst->type) {
1763     case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1764         return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1765
1766 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)     \
1767         case CONSTANT_HTONS(ENUM):                      \
1768             if (EXTENSIBLE                              \
1769                 ? len >= sizeof(struct STRUCT)          \
1770                 : len == sizeof(struct STRUCT)) {       \
1771                 *type = OVSINST_##ENUM;                 \
1772                 return 0;                               \
1773             } else {                                    \
1774                 return OFPERR_OFPBIC_BAD_LEN;           \
1775             }
1776 OVS_INSTRUCTIONS
1777 #undef DEFINE_INST
1778
1779     default:
1780         return OFPERR_OFPBIC_UNKNOWN_INST;
1781     }
1782 }
1783
1784 static enum ofperr
1785 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1786                                size_t n_insts,
1787                                const struct ofp11_instruction *out[])
1788 {
1789     const struct ofp11_instruction *inst;
1790     size_t left;
1791
1792     memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1793     INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1794         enum ovs_instruction_type type;
1795         enum ofperr error;
1796
1797         error = decode_openflow11_instruction(inst, &type);
1798         if (error) {
1799             return error;
1800         }
1801
1802         if (out[type]) {
1803             return OFPERR_OFPBIC_DUP_INST;
1804         }
1805         out[type] = inst;
1806     }
1807
1808     if (left) {
1809         VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
1810                      (n_insts - left) * sizeof *inst);
1811         return OFPERR_OFPBIC_BAD_LEN;
1812     }
1813     return 0;
1814 }
1815
1816 static void
1817 get_actions_from_instruction(const struct ofp11_instruction *inst,
1818                              const union ofp_action **actions,
1819                              size_t *max_actions)
1820 {
1821     *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1822     *max_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1823 }
1824
1825 enum ofperr
1826 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
1827                                    unsigned int instructions_len,
1828                                    enum ofp_version version,
1829                                    struct ofpbuf *ofpacts)
1830 {
1831     const struct ofp11_instruction *instructions;
1832     const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1833     enum ofperr error;
1834
1835     if (version == OFP10_VERSION) {
1836         return ofpacts_pull_openflow_actions__(openflow, instructions_len,
1837                                                version,
1838                                                (1u << N_OVS_INSTRUCTIONS) - 1,
1839                                                ofpacts);
1840     }
1841
1842     ofpbuf_clear(ofpacts);
1843
1844     if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1845         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1846                      "multiple of %d",
1847                      instructions_len, OFP11_INSTRUCTION_ALIGN);
1848         error = OFPERR_OFPBIC_BAD_LEN;
1849         goto exit;
1850     }
1851
1852     instructions = ofpbuf_try_pull(openflow, instructions_len);
1853     if (instructions == NULL) {
1854         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1855                      "remaining message length (%"PRIu32")",
1856                      instructions_len, ofpbuf_size(openflow));
1857         error = OFPERR_OFPBIC_BAD_LEN;
1858         goto exit;
1859     }
1860
1861     error = decode_openflow11_instructions(
1862         instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1863         insts);
1864     if (error) {
1865         goto exit;
1866     }
1867
1868     if (insts[OVSINST_OFPIT13_METER]) {
1869         const struct ofp13_instruction_meter *oim;
1870         struct ofpact_meter *om;
1871
1872         oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1873                            insts[OVSINST_OFPIT13_METER]);
1874
1875         om = ofpact_put_METER(ofpacts);
1876         om->meter_id = ntohl(oim->meter_id);
1877     }
1878     if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1879         const union ofp_action *actions;
1880         size_t max_actions;
1881
1882         get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1883                                      &actions, &max_actions);
1884         error = ofpacts_from_openflow(actions, max_actions, version, ofpacts);
1885         if (error) {
1886             goto exit;
1887         }
1888     }
1889     if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1890         instruction_get_OFPIT11_CLEAR_ACTIONS(
1891             insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1892         ofpact_put_CLEAR_ACTIONS(ofpacts);
1893     }
1894     if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1895         struct ofpact_nest *on;
1896         const union ofp_action *actions;
1897         size_t max_actions;
1898         size_t start;
1899
1900         ofpact_pad(ofpacts);
1901         start = ofpbuf_size(ofpacts);
1902         ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1903                    offsetof(struct ofpact_nest, actions));
1904         get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1905                                      &actions, &max_actions);
1906         error = ofpacts_from_openflow11_for_action_set(actions, max_actions,
1907                                                        version, ofpacts);
1908         if (error) {
1909             goto exit;
1910         }
1911         on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1912         on->ofpact.len = ofpbuf_size(ofpacts) - start;
1913     }
1914     if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1915         const struct ofp11_instruction_write_metadata *oiwm;
1916         struct ofpact_metadata *om;
1917
1918         oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1919                             insts[OVSINST_OFPIT11_WRITE_METADATA]);
1920
1921         om = ofpact_put_WRITE_METADATA(ofpacts);
1922         om->metadata = oiwm->metadata;
1923         om->mask = oiwm->metadata_mask;
1924     }
1925     if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1926         const struct ofp11_instruction_goto_table *oigt;
1927         struct ofpact_goto_table *ogt;
1928
1929         oigt = instruction_get_OFPIT11_GOTO_TABLE(
1930             insts[OVSINST_OFPIT11_GOTO_TABLE]);
1931         ogt = ofpact_put_GOTO_TABLE(ofpacts);
1932         ogt->table_id = oigt->table_id;
1933     }
1934
1935     error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
1936                            (1u << N_OVS_INSTRUCTIONS) - 1);
1937 exit:
1938     if (error) {
1939         ofpbuf_clear(ofpacts);
1940     }
1941     return error;
1942 }
1943 \f
1944 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
1945  * switch will never have more than 'max_ports' ports.  Returns 0 if 'port' is
1946  * valid, otherwise an OpenFlow error code. */
1947 enum ofperr
1948 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
1949 {
1950     switch (port) {
1951     case OFPP_IN_PORT:
1952     case OFPP_TABLE:
1953     case OFPP_NORMAL:
1954     case OFPP_FLOOD:
1955     case OFPP_ALL:
1956     case OFPP_CONTROLLER:
1957     case OFPP_NONE:
1958     case OFPP_LOCAL:
1959         return 0;
1960
1961     default:
1962         if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
1963             return 0;
1964         }
1965         return OFPERR_OFPBAC_BAD_OUT_PORT;
1966     }
1967 }
1968
1969 /* Removes the protocols that require consistency between match and actions
1970  * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
1971  *
1972  * (An example of an inconsistency between match and actions is a flow that
1973  * does not match on an MPLS Ethertype but has an action that pops an MPLS
1974  * label.) */
1975 static void
1976 inconsistent_match(enum ofputil_protocol *usable_protocols)
1977 {
1978     *usable_protocols &= OFPUTIL_P_OF10_ANY;
1979 }
1980
1981 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
1982  * caller must restore them.
1983  *
1984  * Modifies some actions, filling in fields that could not be properly set
1985  * without context. */
1986 static enum ofperr
1987 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
1988                struct flow *flow, ofp_port_t max_ports,
1989                uint8_t table_id, uint8_t n_tables)
1990 {
1991     const struct ofpact_enqueue *enqueue;
1992     const struct mf_field *mf;
1993
1994     switch (a->type) {
1995     case OFPACT_OUTPUT:
1996         return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
1997                                         max_ports);
1998
1999     case OFPACT_CONTROLLER:
2000         return 0;
2001
2002     case OFPACT_ENQUEUE:
2003         enqueue = ofpact_get_ENQUEUE(a);
2004         if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
2005             && enqueue->port != OFPP_IN_PORT
2006             && enqueue->port != OFPP_LOCAL) {
2007             return OFPERR_OFPBAC_BAD_OUT_PORT;
2008         }
2009         return 0;
2010
2011     case OFPACT_OUTPUT_REG:
2012         return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
2013
2014     case OFPACT_BUNDLE:
2015         return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
2016
2017     case OFPACT_SET_VLAN_VID:
2018         /* Remember if we saw a vlan tag in the flow to aid translating to
2019          * OpenFlow 1.1+ if need be. */
2020         ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
2021             (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
2022         if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
2023             !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2024             inconsistent_match(usable_protocols);
2025         }
2026         /* Temporary mark that we have a vlan tag. */
2027         flow->vlan_tci |= htons(VLAN_CFI);
2028         return 0;
2029
2030     case OFPACT_SET_VLAN_PCP:
2031         /* Remember if we saw a vlan tag in the flow to aid translating to
2032          * OpenFlow 1.1+ if need be. */
2033         ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
2034             (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
2035         if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
2036             !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2037             inconsistent_match(usable_protocols);
2038         }
2039         /* Temporary mark that we have a vlan tag. */
2040         flow->vlan_tci |= htons(VLAN_CFI);
2041         return 0;
2042
2043     case OFPACT_STRIP_VLAN:
2044         if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2045             inconsistent_match(usable_protocols);
2046         }
2047         /* Temporary mark that we have no vlan tag. */
2048         flow->vlan_tci = htons(0);
2049         return 0;
2050
2051     case OFPACT_PUSH_VLAN:
2052         if (flow->vlan_tci & htons(VLAN_CFI)) {
2053             /* Multiple VLAN headers not supported. */
2054             return OFPERR_OFPBAC_BAD_TAG;
2055         }
2056         /* Temporary mark that we have a vlan tag. */
2057         flow->vlan_tci |= htons(VLAN_CFI);
2058         return 0;
2059
2060     case OFPACT_SET_ETH_SRC:
2061     case OFPACT_SET_ETH_DST:
2062         return 0;
2063
2064     case OFPACT_SET_IPV4_SRC:
2065     case OFPACT_SET_IPV4_DST:
2066         if (flow->dl_type != htons(ETH_TYPE_IP)) {
2067             inconsistent_match(usable_protocols);
2068         }
2069         return 0;
2070
2071     case OFPACT_SET_IP_DSCP:
2072     case OFPACT_SET_IP_ECN:
2073     case OFPACT_SET_IP_TTL:
2074     case OFPACT_DEC_TTL:
2075         if (!is_ip_any(flow)) {
2076             inconsistent_match(usable_protocols);
2077         }
2078         return 0;
2079
2080     case OFPACT_SET_L4_SRC_PORT:
2081         if (!is_ip_any(flow) ||
2082             (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
2083              && flow->nw_proto != IPPROTO_SCTP)) {
2084             inconsistent_match(usable_protocols);
2085         }
2086         /* Note on which transport protocol the port numbers are set.
2087          * This allows this set action to be converted to an OF1.2 set field
2088          * action. */
2089         ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
2090         return 0;
2091
2092     case OFPACT_SET_L4_DST_PORT:
2093         if (!is_ip_any(flow) ||
2094             (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
2095              && flow->nw_proto != IPPROTO_SCTP)) {
2096             inconsistent_match(usable_protocols);
2097         }
2098         /* Note on which transport protocol the port numbers are set.
2099          * This allows this set action to be converted to an OF1.2 set field
2100          * action. */
2101         ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
2102         return 0;
2103
2104     case OFPACT_REG_MOVE:
2105         return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
2106
2107     case OFPACT_REG_LOAD:
2108         return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
2109
2110     case OFPACT_SET_FIELD:
2111         mf = ofpact_get_SET_FIELD(a)->field;
2112         /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
2113         if (!mf_are_prereqs_ok(mf, flow) ||
2114             (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
2115             VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
2116                          mf->name);
2117             return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2118         }
2119         /* Remember if we saw a vlan tag in the flow to aid translating to
2120          * OpenFlow 1.1 if need be. */
2121         ofpact_get_SET_FIELD(a)->flow_has_vlan =
2122             (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
2123         if (mf->id == MFF_VLAN_TCI) {
2124             /* The set field may add or remove the vlan tag,
2125              * Mark the status temporarily. */
2126             flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
2127         }
2128         return 0;
2129
2130     case OFPACT_STACK_PUSH:
2131         return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
2132
2133     case OFPACT_STACK_POP:
2134         return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
2135
2136     case OFPACT_SET_MPLS_LABEL:
2137     case OFPACT_SET_MPLS_TC:
2138     case OFPACT_SET_MPLS_TTL:
2139     case OFPACT_DEC_MPLS_TTL:
2140         if (!eth_type_mpls(flow->dl_type)) {
2141             inconsistent_match(usable_protocols);
2142         }
2143         return 0;
2144
2145     case OFPACT_SET_TUNNEL:
2146     case OFPACT_SET_QUEUE:
2147     case OFPACT_POP_QUEUE:
2148     case OFPACT_RESUBMIT:
2149         return 0;
2150
2151     case OFPACT_FIN_TIMEOUT:
2152         if (flow->nw_proto != IPPROTO_TCP) {
2153             inconsistent_match(usable_protocols);
2154         }
2155         return 0;
2156
2157     case OFPACT_LEARN:
2158         return learn_check(ofpact_get_LEARN(a), flow);
2159
2160     case OFPACT_MULTIPATH:
2161         return multipath_check(ofpact_get_MULTIPATH(a), flow);
2162
2163     case OFPACT_NOTE:
2164     case OFPACT_EXIT:
2165         return 0;
2166
2167     case OFPACT_PUSH_MPLS:
2168         flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
2169         /* The packet is now MPLS and the MPLS payload is opaque.
2170          * Thus nothing can be assumed about the network protocol.
2171          * Temporarily mark that we have no nw_proto. */
2172         flow->nw_proto = 0;
2173         return 0;
2174
2175     case OFPACT_POP_MPLS:
2176         if (!eth_type_mpls(flow->dl_type)) {
2177             inconsistent_match(usable_protocols);
2178         }
2179         flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
2180         return 0;
2181
2182     case OFPACT_SAMPLE:
2183         return 0;
2184
2185     case OFPACT_CLEAR_ACTIONS:
2186         return 0;
2187
2188     case OFPACT_WRITE_ACTIONS: {
2189         /* Use a temporary copy of 'usable_protocols' because we can't check
2190          * consistency of an action set. */
2191         struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2192         enum ofputil_protocol p = *usable_protocols;
2193         return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
2194                              flow, max_ports, table_id, n_tables, &p);
2195     }
2196
2197     case OFPACT_WRITE_METADATA:
2198         return 0;
2199
2200     case OFPACT_METER: {
2201         uint32_t mid = ofpact_get_METER(a)->meter_id;
2202         if (mid == 0 || mid > OFPM13_MAX) {
2203             return OFPERR_OFPMMFC_INVALID_METER;
2204         }
2205         return 0;
2206     }
2207
2208     case OFPACT_GOTO_TABLE: {
2209         uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
2210         if ((table_id != 255 && goto_table <= table_id)
2211             || (n_tables != 255 && goto_table >= n_tables)) {
2212             return OFPERR_OFPBRC_BAD_TABLE_ID;
2213         }
2214         return 0;
2215     }
2216
2217     case OFPACT_GROUP:
2218         return 0;
2219
2220     default:
2221         OVS_NOT_REACHED();
2222     }
2223 }
2224
2225 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2226  * appropriate for a packet with the prerequisites satisfied by 'flow' in a
2227  * switch with no more than 'max_ports' ports.
2228  *
2229  * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
2230  * '*usable_protocols' the protocols that forbid the inconsistency.  (An
2231  * example of an inconsistency between match and actions is a flow that does
2232  * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
2233  *
2234  * May annotate ofpacts with information gathered from the 'flow'.
2235  *
2236  * May temporarily modify 'flow', but restores the changes before returning. */
2237 enum ofperr
2238 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
2239               struct flow *flow, ofp_port_t max_ports,
2240               uint8_t table_id, uint8_t n_tables,
2241               enum ofputil_protocol *usable_protocols)
2242 {
2243     struct ofpact *a;
2244     ovs_be16 dl_type = flow->dl_type;
2245     ovs_be16 vlan_tci = flow->vlan_tci;
2246     uint8_t nw_proto = flow->nw_proto;
2247     enum ofperr error = 0;
2248
2249     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2250         error = ofpact_check__(usable_protocols, a, flow,
2251                                max_ports, table_id, n_tables);
2252         if (error) {
2253             break;
2254         }
2255     }
2256     /* Restore fields that may have been modified. */
2257     flow->dl_type = dl_type;
2258     flow->vlan_tci = vlan_tci;
2259     flow->nw_proto = nw_proto;
2260     return error;
2261 }
2262
2263 /* Like ofpacts_check(), but reports inconsistencies as
2264  * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
2265 enum ofperr
2266 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
2267                           struct flow *flow, ofp_port_t max_ports,
2268                           uint8_t table_id, uint8_t n_tables,
2269                           enum ofputil_protocol usable_protocols)
2270 {
2271     enum ofputil_protocol p = usable_protocols;
2272     enum ofperr error;
2273
2274     error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
2275                           table_id, n_tables, &p);
2276     return (error ? error
2277             : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
2278             : 0);
2279 }
2280
2281 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2282  * in the appropriate order as defined by the OpenFlow spec. */
2283 enum ofperr
2284 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
2285                uint32_t allowed_ovsinsts)
2286 {
2287     const struct ofpact *a;
2288     enum ovs_instruction_type inst;
2289
2290     inst = OVSINST_OFPIT13_METER;
2291     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2292         enum ovs_instruction_type next;
2293
2294         next = ovs_instruction_type_from_ofpact_type(a->type);
2295         if (a > ofpacts
2296             && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
2297                 ? next < inst
2298                 : next <= inst)) {
2299             const char *name = ovs_instruction_name_from_type(inst);
2300             const char *next_name = ovs_instruction_name_from_type(next);
2301
2302             if (next == inst) {
2303                 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
2304                           "1.1+ compatibility", name);
2305             } else {
2306                 VLOG_WARN("invalid instruction ordering: %s must appear "
2307                           "before %s, for OpenFlow 1.1+ compatibility",
2308                           next_name, name);
2309             }
2310             return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
2311         }
2312         if (!((1u << next) & allowed_ovsinsts)) {
2313             const char *name = ovs_instruction_name_from_type(next);
2314
2315             VLOG_WARN("%s instruction not allowed here", name);
2316             return OFPERR_OFPBIC_UNSUP_INST;
2317         }
2318
2319         inst = next;
2320     }
2321
2322     return 0;
2323 }
2324 \f
2325 /* Converting ofpacts to Nicira OpenFlow extensions. */
2326
2327 static void
2328 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
2329                                 struct ofpbuf *out)
2330 {
2331     struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
2332
2333     naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
2334                                            output_reg->src.n_bits);
2335     naor->src = htonl(output_reg->src.field->nxm_header);
2336     naor->max_len = htons(output_reg->max_len);
2337 }
2338
2339 static void
2340 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
2341                          struct ofpbuf *out)
2342 {
2343     struct nx_action_resubmit *nar;
2344
2345     if (resubmit->table_id == 0xff
2346         && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
2347         nar = ofputil_put_NXAST_RESUBMIT(out);
2348     } else {
2349         nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
2350         nar->table = resubmit->table_id;
2351     }
2352     nar->in_port = htons(ofp_to_u16(resubmit->in_port));
2353 }
2354
2355 static void
2356 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
2357                            struct ofpbuf *out)
2358 {
2359     uint64_t tun_id = tunnel->tun_id;
2360
2361     if (tun_id <= UINT32_MAX
2362         && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
2363         ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
2364     } else {
2365         ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
2366     }
2367 }
2368
2369 static void
2370 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
2371                                struct ofpbuf *out)
2372 {
2373     struct nx_action_write_metadata *nawm;
2374
2375     nawm = ofputil_put_NXAST_WRITE_METADATA(out);
2376     nawm->metadata = om->metadata;
2377     nawm->mask = om->mask;
2378 }
2379
2380 static void
2381 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
2382 {
2383     size_t start_ofs = ofpbuf_size(out);
2384     struct nx_action_note *nan;
2385     unsigned int remainder;
2386     unsigned int len;
2387
2388     ofputil_put_NXAST_NOTE(out);
2389     ofpbuf_set_size(out, ofpbuf_size(out) - sizeof nan->note);
2390
2391     ofpbuf_put(out, note->data, note->length);
2392
2393     len = ofpbuf_size(out) - start_ofs;
2394     remainder = len % OFP_ACTION_ALIGN;
2395     if (remainder) {
2396         ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
2397     }
2398     nan = ofpbuf_at(out, start_ofs, sizeof *nan);
2399     nan->len = htons(ofpbuf_size(out) - start_ofs);
2400 }
2401
2402 static void
2403 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
2404                            struct ofpbuf *out)
2405 {
2406     struct nx_action_controller *nac;
2407
2408     nac = ofputil_put_NXAST_CONTROLLER(out);
2409     nac->max_len = htons(oc->max_len);
2410     nac->controller_id = htons(oc->controller_id);
2411     nac->reason = oc->reason;
2412 }
2413
2414 static void
2415 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
2416                         struct ofpbuf *out)
2417 {
2418     if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
2419         ofputil_put_NXAST_DEC_TTL(out);
2420     } else {
2421         struct nx_action_cnt_ids *nac_ids =
2422             ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
2423         int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
2424         ovs_be16 *ids;
2425         size_t i;
2426
2427         nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2428         nac_ids->n_controllers = htons(oc_ids->n_controllers);
2429
2430         ids = ofpbuf_put_zeros(out, ids_len);
2431         for (i = 0; i < oc_ids->n_controllers; i++) {
2432             ids[i] = htons(oc_ids->cnt_ids[i]);
2433         }
2434     }
2435 }
2436
2437 static void
2438 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
2439                             struct ofpbuf *out)
2440 {
2441     struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
2442     naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
2443     naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
2444 }
2445
2446 static void
2447 ofpact_sample_to_nxast(const struct ofpact_sample *os,
2448                        struct ofpbuf *out)
2449 {
2450     struct nx_action_sample *nas;
2451
2452     nas = ofputil_put_NXAST_SAMPLE(out);
2453     nas->probability = htons(os->probability);
2454     nas->collector_set_id = htonl(os->collector_set_id);
2455     nas->obs_domain_id = htonl(os->obs_domain_id);
2456     nas->obs_point_id = htonl(os->obs_point_id);
2457 }
2458
2459 static void
2460 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
2461 {
2462     switch (a->type) {
2463     case OFPACT_CONTROLLER:
2464         ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
2465         break;
2466
2467     case OFPACT_OUTPUT_REG:
2468         ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
2469         break;
2470
2471     case OFPACT_BUNDLE:
2472         bundle_to_nxast(ofpact_get_BUNDLE(a), out);
2473         break;
2474
2475     case OFPACT_REG_MOVE:
2476         nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
2477         break;
2478
2479     case OFPACT_REG_LOAD:
2480         nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
2481         break;
2482
2483     case OFPACT_STACK_PUSH:
2484         nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
2485         break;
2486
2487     case OFPACT_STACK_POP:
2488         nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
2489         break;
2490
2491     case OFPACT_DEC_TTL:
2492         ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
2493         break;
2494
2495     case OFPACT_SET_MPLS_LABEL:
2496         ofputil_put_NXAST_SET_MPLS_LABEL(out)->label
2497             = ofpact_get_SET_MPLS_LABEL(a)->label;
2498         break;
2499
2500     case OFPACT_SET_MPLS_TC:
2501         ofputil_put_NXAST_SET_MPLS_TC(out)->tc
2502             = ofpact_get_SET_MPLS_TC(a)->tc;
2503         break;
2504
2505     case OFPACT_SET_MPLS_TTL:
2506         ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
2507             = ofpact_get_SET_MPLS_TTL(a)->ttl;
2508         break;
2509
2510     case OFPACT_DEC_MPLS_TTL:
2511         ofputil_put_NXAST_DEC_MPLS_TTL(out);
2512         break;
2513
2514     case OFPACT_SET_TUNNEL:
2515         ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2516         break;
2517
2518     case OFPACT_WRITE_METADATA:
2519         ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2520         break;
2521
2522     case OFPACT_SET_QUEUE:
2523         ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2524             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2525         break;
2526
2527     case OFPACT_POP_QUEUE:
2528         ofputil_put_NXAST_POP_QUEUE(out);
2529         break;
2530
2531     case OFPACT_FIN_TIMEOUT:
2532         ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2533         break;
2534
2535     case OFPACT_RESUBMIT:
2536         ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2537         break;
2538
2539     case OFPACT_LEARN:
2540         learn_to_nxast(ofpact_get_LEARN(a), out);
2541         break;
2542
2543     case OFPACT_MULTIPATH:
2544         multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2545         break;
2546
2547     case OFPACT_NOTE:
2548         ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2549         break;
2550
2551     case OFPACT_EXIT:
2552         ofputil_put_NXAST_EXIT(out);
2553         break;
2554
2555     case OFPACT_PUSH_MPLS:
2556         ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2557             ofpact_get_PUSH_MPLS(a)->ethertype;
2558         break;
2559
2560     case OFPACT_POP_MPLS:
2561         ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2562             ofpact_get_POP_MPLS(a)->ethertype;
2563         break;
2564
2565     case OFPACT_SAMPLE:
2566         ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2567         break;
2568
2569     case OFPACT_GROUP:
2570     case OFPACT_OUTPUT:
2571     case OFPACT_ENQUEUE:
2572     case OFPACT_SET_VLAN_VID:
2573     case OFPACT_SET_VLAN_PCP:
2574     case OFPACT_STRIP_VLAN:
2575     case OFPACT_PUSH_VLAN:
2576     case OFPACT_SET_ETH_SRC:
2577     case OFPACT_SET_ETH_DST:
2578     case OFPACT_SET_IPV4_SRC:
2579     case OFPACT_SET_IPV4_DST:
2580     case OFPACT_SET_IP_DSCP:
2581     case OFPACT_SET_IP_ECN:
2582     case OFPACT_SET_IP_TTL:
2583     case OFPACT_SET_L4_SRC_PORT:
2584     case OFPACT_SET_L4_DST_PORT:
2585     case OFPACT_WRITE_ACTIONS:
2586     case OFPACT_CLEAR_ACTIONS:
2587     case OFPACT_GOTO_TABLE:
2588     case OFPACT_METER:
2589     case OFPACT_SET_FIELD:
2590         OVS_NOT_REACHED();
2591     }
2592 }
2593 \f
2594 /* Converting ofpacts to OpenFlow 1.0. */
2595
2596 static void
2597 ofpact_output_to_openflow10(const struct ofpact_output *output,
2598                             struct ofpbuf *out)
2599 {
2600     struct ofp10_action_output *oao;
2601
2602     oao = ofputil_put_OFPAT10_OUTPUT(out);
2603     oao->port = htons(ofp_to_u16(output->port));
2604     oao->max_len = htons(output->max_len);
2605 }
2606
2607 static void
2608 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2609                              struct ofpbuf *out)
2610 {
2611     struct ofp10_action_enqueue *oae;
2612
2613     oae = ofputil_put_OFPAT10_ENQUEUE(out);
2614     oae->port = htons(ofp_to_u16(enqueue->port));
2615     oae->queue_id = htonl(enqueue->queue);
2616 }
2617
2618 static void
2619 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2620 {
2621     switch (a->type) {
2622     case OFPACT_OUTPUT:
2623         ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2624         break;
2625
2626     case OFPACT_ENQUEUE:
2627         ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2628         break;
2629
2630     case OFPACT_SET_VLAN_VID:
2631         ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2632             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2633         break;
2634
2635     case OFPACT_SET_VLAN_PCP:
2636         ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2637             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2638         break;
2639
2640     case OFPACT_STRIP_VLAN:
2641         ofputil_put_OFPAT10_STRIP_VLAN(out);
2642         break;
2643
2644     case OFPACT_SET_ETH_SRC:
2645         memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2646                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2647         break;
2648
2649     case OFPACT_SET_ETH_DST:
2650         memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2651                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2652         break;
2653
2654     case OFPACT_SET_IPV4_SRC:
2655         ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2656             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2657         break;
2658
2659     case OFPACT_SET_IPV4_DST:
2660         ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2661             = ofpact_get_SET_IPV4_DST(a)->ipv4;
2662         break;
2663
2664     case OFPACT_SET_IP_DSCP:
2665         ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2666             = ofpact_get_SET_IP_DSCP(a)->dscp;
2667         break;
2668
2669     case OFPACT_SET_L4_SRC_PORT:
2670         ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2671             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2672         break;
2673
2674     case OFPACT_SET_L4_DST_PORT:
2675         ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2676             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2677         break;
2678
2679     case OFPACT_PUSH_VLAN:
2680         /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2681          * follow this action. */
2682         break;
2683
2684     case OFPACT_SET_IP_ECN:
2685     case OFPACT_SET_IP_TTL:
2686     case OFPACT_CLEAR_ACTIONS:
2687     case OFPACT_WRITE_ACTIONS:
2688     case OFPACT_GOTO_TABLE:
2689     case OFPACT_METER:
2690         /* XXX */
2691         break;
2692
2693     case OFPACT_GROUP:
2694         break;
2695
2696     case OFPACT_SET_FIELD:
2697         set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2698         break;
2699
2700     case OFPACT_CONTROLLER:
2701     case OFPACT_OUTPUT_REG:
2702     case OFPACT_BUNDLE:
2703     case OFPACT_REG_MOVE:
2704     case OFPACT_REG_LOAD:
2705     case OFPACT_STACK_PUSH:
2706     case OFPACT_STACK_POP:
2707     case OFPACT_DEC_TTL:
2708     case OFPACT_SET_MPLS_LABEL:
2709     case OFPACT_SET_MPLS_TC:
2710     case OFPACT_SET_MPLS_TTL:
2711     case OFPACT_DEC_MPLS_TTL:
2712     case OFPACT_SET_TUNNEL:
2713     case OFPACT_WRITE_METADATA:
2714     case OFPACT_SET_QUEUE:
2715     case OFPACT_POP_QUEUE:
2716     case OFPACT_FIN_TIMEOUT:
2717     case OFPACT_RESUBMIT:
2718     case OFPACT_LEARN:
2719     case OFPACT_MULTIPATH:
2720     case OFPACT_NOTE:
2721     case OFPACT_EXIT:
2722     case OFPACT_PUSH_MPLS:
2723     case OFPACT_POP_MPLS:
2724     case OFPACT_SAMPLE:
2725         ofpact_to_nxast(a, out);
2726         break;
2727     }
2728 }
2729 \f
2730 /* Converting ofpacts to OpenFlow 1.1. */
2731
2732 static void
2733 ofpact_output_to_openflow11(const struct ofpact_output *output,
2734                             struct ofpbuf *out)
2735 {
2736     struct ofp11_action_output *oao;
2737
2738     oao = ofputil_put_OFPAT11_OUTPUT(out);
2739     oao->port = ofputil_port_to_ofp11(output->port);
2740     oao->max_len = htons(output->max_len);
2741 }
2742
2743 static void
2744 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2745                              struct ofpbuf *out)
2746 {
2747     if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2748         && (!dec_ttl->ofpact.compat ||
2749             dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2750         ofputil_put_OFPAT11_DEC_NW_TTL(out);
2751     } else {
2752         ofpact_dec_ttl_to_nxast(dec_ttl, out);
2753     }
2754 }
2755
2756 static void
2757 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2758 {
2759     switch (a->type) {
2760     case OFPACT_OUTPUT:
2761         return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2762
2763     case OFPACT_ENQUEUE:
2764         /* XXX */
2765         break;
2766
2767     case OFPACT_SET_VLAN_VID:
2768         /* Push a VLAN tag, if one was not seen at action validation time. */
2769         if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2770             && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2771             ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2772                 = htons(ETH_TYPE_VLAN_8021Q);
2773         }
2774         ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2775             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2776         break;
2777
2778     case OFPACT_SET_VLAN_PCP:
2779         /* Push a VLAN tag, if one was not seen at action validation time. */
2780         if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2781             && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2782             ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2783                 = htons(ETH_TYPE_VLAN_8021Q);
2784         }
2785         ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2786             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2787         break;
2788
2789     case OFPACT_STRIP_VLAN:
2790         ofputil_put_OFPAT11_POP_VLAN(out);
2791         break;
2792
2793     case OFPACT_PUSH_VLAN:
2794         /* XXX ETH_TYPE_VLAN_8021AD case */
2795         ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2796             htons(ETH_TYPE_VLAN_8021Q);
2797         break;
2798
2799     case OFPACT_SET_QUEUE:
2800         ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2801             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2802         break;
2803
2804     case OFPACT_SET_ETH_SRC:
2805         memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2806                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2807         break;
2808
2809     case OFPACT_SET_ETH_DST:
2810         memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2811                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2812         break;
2813
2814     case OFPACT_SET_IPV4_SRC:
2815         ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2816             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2817         break;
2818
2819     case OFPACT_SET_IPV4_DST:
2820         ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2821             = ofpact_get_SET_IPV4_DST(a)->ipv4;
2822         break;
2823
2824     case OFPACT_SET_IP_DSCP:
2825         ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2826             = ofpact_get_SET_IP_DSCP(a)->dscp;
2827         break;
2828
2829     case OFPACT_SET_IP_ECN:
2830         ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2831             = ofpact_get_SET_IP_ECN(a)->ecn;
2832         break;
2833
2834     case OFPACT_SET_IP_TTL:
2835         ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2836             = ofpact_get_SET_IP_TTL(a)->ttl;
2837         break;
2838
2839     case OFPACT_SET_L4_SRC_PORT:
2840         ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2841             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2842         break;
2843
2844     case OFPACT_SET_L4_DST_PORT:
2845         ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2846             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2847         break;
2848
2849     case OFPACT_DEC_TTL:
2850         ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2851         break;
2852
2853     case OFPACT_SET_MPLS_LABEL:
2854         ofputil_put_OFPAT11_SET_MPLS_LABEL(out)->mpls_label
2855             = ofpact_get_SET_MPLS_LABEL(a)->label;
2856         break;
2857
2858     case OFPACT_SET_MPLS_TC:
2859         ofputil_put_OFPAT11_SET_MPLS_TC(out)->mpls_tc
2860             = ofpact_get_SET_MPLS_TC(a)->tc;
2861         break;
2862
2863     case OFPACT_SET_MPLS_TTL:
2864         ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2865             = ofpact_get_SET_MPLS_TTL(a)->ttl;
2866         break;
2867
2868     case OFPACT_DEC_MPLS_TTL:
2869         ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2870         break;
2871
2872     case OFPACT_WRITE_METADATA:
2873         /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2874         break;
2875
2876     case OFPACT_PUSH_MPLS:
2877         ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2878             ofpact_get_PUSH_MPLS(a)->ethertype;
2879         break;
2880
2881     case OFPACT_POP_MPLS:
2882         ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2883             ofpact_get_POP_MPLS(a)->ethertype;
2884
2885         break;
2886
2887     case OFPACT_CLEAR_ACTIONS:
2888     case OFPACT_WRITE_ACTIONS:
2889     case OFPACT_GOTO_TABLE:
2890     case OFPACT_METER:
2891         OVS_NOT_REACHED();
2892
2893     case OFPACT_GROUP:
2894         ofputil_put_OFPAT11_GROUP(out)->group_id =
2895             htonl(ofpact_get_GROUP(a)->group_id);
2896         break;
2897
2898     case OFPACT_SET_FIELD:
2899         set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2900         break;
2901
2902     case OFPACT_CONTROLLER:
2903     case OFPACT_OUTPUT_REG:
2904     case OFPACT_BUNDLE:
2905     case OFPACT_REG_MOVE:
2906     case OFPACT_REG_LOAD:
2907     case OFPACT_STACK_PUSH:
2908     case OFPACT_STACK_POP:
2909     case OFPACT_SET_TUNNEL:
2910     case OFPACT_POP_QUEUE:
2911     case OFPACT_FIN_TIMEOUT:
2912     case OFPACT_RESUBMIT:
2913     case OFPACT_LEARN:
2914     case OFPACT_MULTIPATH:
2915     case OFPACT_NOTE:
2916     case OFPACT_EXIT:
2917     case OFPACT_SAMPLE:
2918         ofpact_to_nxast(a, out);
2919         break;
2920     }
2921 }
2922
2923 /* Output deprecated set actions as set_field actions. */
2924 static void
2925 ofpact_to_openflow12(const struct ofpact *a, struct ofpbuf *out)
2926 {
2927     enum mf_field_id field;
2928     union mf_value value;
2929     struct ofpact_l4_port *l4port;
2930     uint8_t proto;
2931
2932     /*
2933      * Convert actions deprecated in OpenFlow 1.2 to Set Field actions,
2934      * if possible.
2935      */
2936     switch ((int)a->type) {
2937     case OFPACT_SET_VLAN_VID:
2938     case OFPACT_SET_VLAN_PCP:
2939     case OFPACT_SET_ETH_SRC:
2940     case OFPACT_SET_ETH_DST:
2941     case OFPACT_SET_IPV4_SRC:
2942     case OFPACT_SET_IPV4_DST:
2943     case OFPACT_SET_IP_DSCP:
2944     case OFPACT_SET_IP_ECN:
2945     case OFPACT_SET_L4_SRC_PORT:
2946     case OFPACT_SET_L4_DST_PORT:
2947     case OFPACT_SET_MPLS_LABEL:
2948     case OFPACT_SET_MPLS_TC:
2949     case OFPACT_SET_TUNNEL:  /* Convert to a set_field, too. */
2950
2951         switch ((int)a->type) {
2952
2953         case OFPACT_SET_VLAN_VID:
2954             if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan &&
2955                 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2956                 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2957                     = htons(ETH_TYPE_VLAN_8021Q);
2958             }
2959             field = MFF_VLAN_VID;
2960             /* Set-Field on OXM_OF_VLAN_VID must have OFPVID_PRESENT set. */
2961             value.be16 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid
2962                                | OFPVID12_PRESENT);
2963             break;
2964
2965         case OFPACT_SET_VLAN_PCP:
2966             if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan &&
2967                 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2968                 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2969                     = htons(ETH_TYPE_VLAN_8021Q);
2970             }
2971             field = MFF_VLAN_PCP;
2972             value.u8 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2973             break;
2974
2975         case OFPACT_SET_ETH_SRC:
2976             field = MFF_ETH_SRC;
2977             memcpy(value.mac, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2978             break;
2979
2980         case OFPACT_SET_ETH_DST:
2981             field = MFF_ETH_DST;
2982             memcpy(value.mac, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2983             break;
2984
2985         case OFPACT_SET_IPV4_SRC:
2986             field = MFF_IPV4_SRC;
2987             value.be32 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2988             break;
2989
2990         case OFPACT_SET_IPV4_DST:
2991             field = MFF_IPV4_DST;
2992             value.be32 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2993             break;
2994
2995         case OFPACT_SET_IP_DSCP:
2996             field = MFF_IP_DSCP_SHIFTED; /* OXM_OF_IP_DSCP */
2997             value.u8 = ofpact_get_SET_IP_DSCP(a)->dscp >> 2;
2998             break;
2999
3000         case OFPACT_SET_IP_ECN:
3001             field = MFF_IP_ECN;
3002             value.u8 = ofpact_get_SET_IP_ECN(a)->ecn;
3003             break;
3004
3005         case OFPACT_SET_L4_SRC_PORT:
3006             /* We keep track of IP protocol while translating actions to be
3007              * able to translate to the proper OXM type.
3008              * If the IP protocol type is unknown, the translation cannot
3009              * be performed and we will send the action using the original
3010              * action type. */
3011             l4port = ofpact_get_SET_L4_SRC_PORT(a);
3012             proto = l4port->flow_ip_proto;
3013             field = proto == IPPROTO_TCP ? MFF_TCP_SRC
3014                 : proto == IPPROTO_UDP ? MFF_UDP_SRC
3015                 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
3016                 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
3017             value.be16 = htons(l4port->port);
3018             break;
3019
3020         case OFPACT_SET_L4_DST_PORT:
3021             l4port = ofpact_get_SET_L4_DST_PORT(a);
3022             proto = l4port->flow_ip_proto;
3023             field = proto == IPPROTO_TCP ? MFF_TCP_DST
3024                 : proto == IPPROTO_UDP ? MFF_UDP_DST
3025                 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
3026                 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
3027             value.be16 = htons(l4port->port);
3028             break;
3029
3030         case OFPACT_SET_MPLS_LABEL:
3031             field = MFF_MPLS_LABEL;
3032             value.be32 = ofpact_get_SET_MPLS_LABEL(a)->label;
3033             break;
3034
3035         case OFPACT_SET_MPLS_TC:
3036             field = MFF_MPLS_TC;
3037             value.u8 = ofpact_get_SET_MPLS_TC(a)->tc;
3038             break;
3039
3040         case OFPACT_SET_TUNNEL:
3041             field = MFF_TUN_ID;
3042             value.be64 = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
3043             break;
3044
3045         default:
3046             field = MFF_N_IDS;
3047         }
3048
3049         /* Put the action out as a set field action, if possible. */
3050         if (field < MFF_N_IDS) {
3051             uint64_t ofpacts_stub[128 / 8];
3052             struct ofpbuf sf_act;
3053             struct ofpact_set_field *sf;
3054
3055             ofpbuf_use_stub(&sf_act, ofpacts_stub, sizeof ofpacts_stub);
3056             sf = ofpact_put_SET_FIELD(&sf_act);
3057             sf->field = mf_from_id(field);
3058             memcpy(&sf->value, &value, sf->field->n_bytes);
3059             set_field_to_openflow(sf, out);
3060             return;
3061         }
3062     }
3063
3064     ofpact_to_openflow11(a, out);
3065 }
3066
3067 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
3068  * actions in 'openflow', appending the actions to any existing data in
3069  * 'openflow'. */
3070 size_t
3071 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
3072                              struct ofpbuf *openflow,
3073                              enum ofp_version ofp_version)
3074 {
3075     const struct ofpact *a;
3076     size_t start_size = ofpbuf_size(openflow);
3077
3078     void (*translate)(const struct ofpact *a, struct ofpbuf *out) =
3079         (ofp_version == OFP10_VERSION) ? ofpact_to_openflow10 :
3080         (ofp_version == OFP11_VERSION) ? ofpact_to_openflow11 :
3081         ofpact_to_openflow12;
3082
3083     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3084         translate(a, openflow);
3085     }
3086     return ofpbuf_size(openflow) - start_size;
3087 }
3088
3089 static void
3090 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
3091 {
3092     struct ofp11_instruction_actions *oia;
3093
3094     /* Update the instruction's length (or, if it's empty, delete it). */
3095     oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
3096     if (ofpbuf_size(openflow) > ofs + sizeof *oia) {
3097         oia->len = htons(ofpbuf_size(openflow) - ofs);
3098     } else {
3099         ofpbuf_set_size(openflow, ofs);
3100     }
3101 }
3102
3103 void
3104 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
3105                                   size_t ofpacts_len,
3106                                   struct ofpbuf *openflow,
3107                                   enum ofp_version ofp_version)
3108 {
3109     const struct ofpact *a;
3110
3111     if (ofp_version == OFP10_VERSION) {
3112         ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
3113                                      ofp_version);
3114         return;
3115     }
3116
3117     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3118         switch (ovs_instruction_type_from_ofpact_type(a->type)) {
3119         case OVSINST_OFPIT11_CLEAR_ACTIONS:
3120             instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
3121             break;
3122
3123         case OVSINST_OFPIT11_GOTO_TABLE: {
3124             struct ofp11_instruction_goto_table *oigt;
3125             oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
3126             oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
3127             memset(oigt->pad, 0, sizeof oigt->pad);
3128             break;
3129         }
3130
3131         case OVSINST_OFPIT11_WRITE_METADATA: {
3132             const struct ofpact_metadata *om;
3133             struct ofp11_instruction_write_metadata *oiwm;
3134
3135             om = ofpact_get_WRITE_METADATA(a);
3136             oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
3137             oiwm->metadata = om->metadata;
3138             oiwm->metadata_mask = om->mask;
3139             break;
3140         }
3141
3142         case OVSINST_OFPIT13_METER:
3143             if (ofp_version >= OFP13_VERSION) {
3144                 const struct ofpact_meter *om;
3145                 struct ofp13_instruction_meter *oim;
3146
3147                 om = ofpact_get_METER(a);
3148                 oim = instruction_put_OFPIT13_METER(openflow);
3149                 oim->meter_id = htonl(om->meter_id);
3150             }
3151             break;
3152
3153         case OVSINST_OFPIT11_APPLY_ACTIONS: {
3154             const size_t ofs = ofpbuf_size(openflow);
3155             const size_t ofpacts_len_left =
3156                 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
3157             const struct ofpact *action;
3158             const struct ofpact *processed = a;
3159
3160             instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
3161             OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
3162                 if (ovs_instruction_type_from_ofpact_type(action->type)
3163                     != OVSINST_OFPIT11_APPLY_ACTIONS) {
3164                     break;
3165                 }
3166                 if (ofp_version == OFP11_VERSION) {
3167                     ofpact_to_openflow11(action, openflow);
3168                 } else {
3169                     ofpact_to_openflow12(action, openflow);
3170                 }
3171                 processed = action;
3172             }
3173             ofpacts_update_instruction_actions(openflow, ofs);
3174             a = processed;
3175             break;
3176         }
3177
3178         case OVSINST_OFPIT11_WRITE_ACTIONS: {
3179             const size_t ofs = ofpbuf_size(openflow);
3180             const struct ofpact_nest *on;
3181
3182             on = ofpact_get_WRITE_ACTIONS(a);
3183             instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
3184             ofpacts_put_openflow_actions(on->actions,
3185                                          ofpact_nest_get_action_len(on),
3186                                          openflow, ofp_version);
3187             ofpacts_update_instruction_actions(openflow, ofs);
3188
3189             break;
3190         }
3191         }
3192     }
3193 }
3194 \f
3195 /* Sets of supported actions. */
3196
3197 /* Two-way translation between OVS's internal "OFPACT_*" representation of
3198  * actions and the "OFPAT_*" representation used in some OpenFlow version.
3199  * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
3200  * instance is specific to one OpenFlow version.) */
3201 struct ofpact_map {
3202     enum ofpact_type ofpact;    /* Internal name for action type. */
3203     int ofpat;                  /* OFPAT_* number from OpenFlow spec. */
3204 };
3205
3206 static const struct ofpact_map *
3207 get_ofpact_map(enum ofp_version version)
3208 {
3209     /* OpenFlow 1.0 actions. */
3210     static const struct ofpact_map of10[] = {
3211         { OFPACT_OUTPUT, 0 },
3212         { OFPACT_SET_VLAN_VID, 1 },
3213         { OFPACT_SET_VLAN_PCP, 2 },
3214         { OFPACT_STRIP_VLAN, 3 },
3215         { OFPACT_SET_ETH_SRC, 4 },
3216         { OFPACT_SET_ETH_DST, 5 },
3217         { OFPACT_SET_IPV4_SRC, 6 },
3218         { OFPACT_SET_IPV4_DST, 7 },
3219         { OFPACT_SET_IP_DSCP, 8 },
3220         { OFPACT_SET_L4_SRC_PORT, 9 },
3221         { OFPACT_SET_L4_DST_PORT, 10 },
3222         { OFPACT_ENQUEUE, 11 },
3223         { 0, -1 },
3224     };
3225
3226     /* OpenFlow 1.1 actions. */
3227     static const struct ofpact_map of11[] = {
3228         { OFPACT_OUTPUT, 0 },
3229         { OFPACT_SET_VLAN_VID, 1 },
3230         { OFPACT_SET_VLAN_PCP, 2 },
3231         { OFPACT_SET_ETH_SRC, 3 },
3232         { OFPACT_SET_ETH_DST, 4 },
3233         { OFPACT_SET_IPV4_SRC, 5 },
3234         { OFPACT_SET_IPV4_DST, 6 },
3235         { OFPACT_SET_IP_DSCP, 7 },
3236         { OFPACT_SET_IP_ECN, 8 },
3237         { OFPACT_SET_L4_SRC_PORT, 9 },
3238         { OFPACT_SET_L4_DST_PORT, 10 },
3239         /* OFPAT_COPY_TTL_OUT (11) not supported. */
3240         /* OFPAT_COPY_TTL_IN (12) not supported. */
3241         { OFPACT_SET_MPLS_LABEL, 13 },
3242         { OFPACT_SET_MPLS_TC, 14 },
3243         { OFPACT_SET_MPLS_TTL, 15 },
3244         { OFPACT_DEC_MPLS_TTL, 16 },
3245         { OFPACT_PUSH_VLAN, 17 },
3246         { OFPACT_STRIP_VLAN, 18 },
3247         { OFPACT_PUSH_MPLS, 19 },
3248         { OFPACT_POP_MPLS, 20 },
3249         { OFPACT_SET_QUEUE, 21 },
3250         { OFPACT_GROUP, 22 },
3251         { OFPACT_SET_IP_TTL, 23 },
3252         { OFPACT_DEC_TTL, 24 },
3253         { 0, -1 },
3254     };
3255
3256     /* OpenFlow 1.2, 1.3, and 1.4 actions. */
3257     static const struct ofpact_map of12[] = {
3258         { OFPACT_OUTPUT, 0 },
3259         /* OFPAT_COPY_TTL_OUT (11) not supported. */
3260         /* OFPAT_COPY_TTL_IN (12) not supported. */
3261         { OFPACT_SET_MPLS_TTL, 15 },
3262         { OFPACT_DEC_MPLS_TTL, 16 },
3263         { OFPACT_PUSH_VLAN, 17 },
3264         { OFPACT_STRIP_VLAN, 18 },
3265         { OFPACT_PUSH_MPLS, 19 },
3266         { OFPACT_POP_MPLS, 20 },
3267         { OFPACT_SET_QUEUE, 21 },
3268         { OFPACT_GROUP, 22 },
3269         { OFPACT_SET_IP_TTL, 23 },
3270         { OFPACT_DEC_TTL, 24 },
3271         { OFPACT_SET_FIELD, 25 },
3272         /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
3273         /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
3274         { 0, -1 },
3275     };
3276
3277     switch (version) {
3278     case OFP10_VERSION:
3279         return of10;
3280
3281     case OFP11_VERSION:
3282         return of11;
3283
3284     case OFP12_VERSION:
3285     case OFP13_VERSION:
3286     case OFP14_VERSION:
3287     case OFP15_VERSION:
3288     default:
3289         return of12;
3290     }
3291 }
3292
3293 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
3294  * values, into a bitmap of actions suitable for OpenFlow 'version', and
3295  * returns the result. */
3296 ovs_be32
3297 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
3298 {
3299     uint32_t openflow_bitmap = 0;
3300     const struct ofpact_map *x;
3301
3302     for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
3303         if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
3304             openflow_bitmap |= 1u << x->ofpat;
3305         }
3306     }
3307     return htonl(openflow_bitmap);
3308 }
3309
3310 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
3311  * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
3312  * and returns the result. */
3313 uint64_t
3314 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
3315 {
3316     uint64_t ofpact_bitmap = 0;
3317     const struct ofpact_map *x;
3318
3319     for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
3320         if (ofpat_bitmap & htonl(1u << x->ofpat)) {
3321             ofpact_bitmap |= UINT64_C(1) << x->ofpact;
3322         }
3323     }
3324     return ofpact_bitmap;
3325 }
3326
3327 /* Appends to 's' a string representation of the set of OFPACT_* represented
3328  * by 'ofpacts_bitmap'. */
3329 void
3330 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
3331 {
3332     if (!ofpacts_bitmap) {
3333         ds_put_cstr(s, "<none>");
3334     } else {
3335         while (ofpacts_bitmap) {
3336             ds_put_format(s, "%s ",
3337                           ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
3338             ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
3339         }
3340         ds_chomp(s, ' ');
3341     }
3342 }
3343 \f
3344 /* Returns true if 'action' outputs to 'port', false otherwise. */
3345 static bool
3346 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
3347 {
3348     switch (ofpact->type) {
3349     case OFPACT_OUTPUT:
3350         return ofpact_get_OUTPUT(ofpact)->port == port;
3351     case OFPACT_ENQUEUE:
3352         return ofpact_get_ENQUEUE(ofpact)->port == port;
3353     case OFPACT_CONTROLLER:
3354         return port == OFPP_CONTROLLER;
3355
3356     case OFPACT_OUTPUT_REG:
3357     case OFPACT_BUNDLE:
3358     case OFPACT_SET_VLAN_VID:
3359     case OFPACT_SET_VLAN_PCP:
3360     case OFPACT_STRIP_VLAN:
3361     case OFPACT_PUSH_VLAN:
3362     case OFPACT_SET_ETH_SRC:
3363     case OFPACT_SET_ETH_DST:
3364     case OFPACT_SET_IPV4_SRC:
3365     case OFPACT_SET_IPV4_DST:
3366     case OFPACT_SET_IP_DSCP:
3367     case OFPACT_SET_IP_ECN:
3368     case OFPACT_SET_IP_TTL:
3369     case OFPACT_SET_L4_SRC_PORT:
3370     case OFPACT_SET_L4_DST_PORT:
3371     case OFPACT_REG_MOVE:
3372     case OFPACT_REG_LOAD:
3373     case OFPACT_SET_FIELD:
3374     case OFPACT_STACK_PUSH:
3375     case OFPACT_STACK_POP:
3376     case OFPACT_DEC_TTL:
3377     case OFPACT_SET_MPLS_LABEL:
3378     case OFPACT_SET_MPLS_TC:
3379     case OFPACT_SET_MPLS_TTL:
3380     case OFPACT_DEC_MPLS_TTL:
3381     case OFPACT_SET_TUNNEL:
3382     case OFPACT_WRITE_METADATA:
3383     case OFPACT_SET_QUEUE:
3384     case OFPACT_POP_QUEUE:
3385     case OFPACT_FIN_TIMEOUT:
3386     case OFPACT_RESUBMIT:
3387     case OFPACT_LEARN:
3388     case OFPACT_MULTIPATH:
3389     case OFPACT_NOTE:
3390     case OFPACT_EXIT:
3391     case OFPACT_PUSH_MPLS:
3392     case OFPACT_POP_MPLS:
3393     case OFPACT_SAMPLE:
3394     case OFPACT_CLEAR_ACTIONS:
3395     case OFPACT_WRITE_ACTIONS:
3396     case OFPACT_GOTO_TABLE:
3397     case OFPACT_METER:
3398     case OFPACT_GROUP:
3399     default:
3400         return false;
3401     }
3402 }
3403
3404 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3405  * to 'port', false otherwise. */
3406 bool
3407 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
3408                        ofp_port_t port)
3409 {
3410     const struct ofpact *a;
3411
3412     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3413         if (ofpact_outputs_to_port(a, port)) {
3414             return true;
3415         }
3416     }
3417
3418     return false;
3419 }
3420
3421 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3422  * to 'group', false otherwise. */
3423 bool
3424 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
3425                         uint32_t group_id)
3426 {
3427     const struct ofpact *a;
3428
3429     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3430         if (a->type == OFPACT_GROUP
3431             && ofpact_get_GROUP(a)->group_id == group_id) {
3432             return true;
3433         }
3434     }
3435
3436     return false;
3437 }
3438
3439 bool
3440 ofpacts_equal(const struct ofpact *a, size_t a_len,
3441               const struct ofpact *b, size_t b_len)
3442 {
3443     return a_len == b_len && !memcmp(a, b, a_len);
3444 }
3445
3446 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
3447  * 'ofpacts'.  If found, returns its meter ID; if not, returns 0.
3448  *
3449  * This function relies on the order of 'ofpacts' being correct (as checked by
3450  * ofpacts_verify()). */
3451 uint32_t
3452 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
3453 {
3454     const struct ofpact *a;
3455
3456     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3457         enum ovs_instruction_type inst;
3458
3459         inst = ovs_instruction_type_from_ofpact_type(a->type);
3460         if (a->type == OFPACT_METER) {
3461             return ofpact_get_METER(a)->meter_id;
3462         } else if (inst > OVSINST_OFPIT13_METER) {
3463             break;
3464         }
3465     }
3466
3467     return 0;
3468 }
3469 \f
3470 /* Formatting ofpacts. */
3471
3472 static void
3473 print_note(const struct ofpact_note *note, struct ds *string)
3474 {
3475     size_t i;
3476
3477     ds_put_cstr(string, "note:");
3478     for (i = 0; i < note->length; i++) {
3479         if (i) {
3480             ds_put_char(string, '.');
3481         }
3482         ds_put_format(string, "%02"PRIx8, note->data[i]);
3483     }
3484 }
3485
3486 static void
3487 print_dec_ttl(const struct ofpact_cnt_ids *ids,
3488               struct ds *s)
3489 {
3490     size_t i;
3491
3492     ds_put_cstr(s, "dec_ttl");
3493     if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
3494         ds_put_cstr(s, "(");
3495         for (i = 0; i < ids->n_controllers; i++) {
3496             if (i) {
3497                 ds_put_cstr(s, ",");
3498             }
3499             ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
3500         }
3501         ds_put_cstr(s, ")");
3502     }
3503 }
3504
3505 static void
3506 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
3507                   struct ds *s)
3508 {
3509     ds_put_cstr(s, "fin_timeout(");
3510     if (fin_timeout->fin_idle_timeout) {
3511         ds_put_format(s, "idle_timeout=%"PRIu16",",
3512                       fin_timeout->fin_idle_timeout);
3513     }
3514     if (fin_timeout->fin_hard_timeout) {
3515         ds_put_format(s, "hard_timeout=%"PRIu16",",
3516                       fin_timeout->fin_hard_timeout);
3517     }
3518     ds_chomp(s, ',');
3519     ds_put_char(s, ')');
3520 }
3521
3522 static void
3523 ofpact_format(const struct ofpact *a, struct ds *s)
3524 {
3525     const struct ofpact_enqueue *enqueue;
3526     const struct ofpact_resubmit *resubmit;
3527     const struct ofpact_controller *controller;
3528     const struct ofpact_metadata *metadata;
3529     const struct ofpact_tunnel *tunnel;
3530     const struct ofpact_sample *sample;
3531     const struct ofpact_set_field *set_field;
3532     const struct mf_field *mf;
3533     ofp_port_t port;
3534
3535     switch (a->type) {
3536     case OFPACT_OUTPUT:
3537         port = ofpact_get_OUTPUT(a)->port;
3538         if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
3539             ds_put_format(s, "output:%"PRIu16, port);
3540         } else {
3541             ofputil_format_port(port, s);
3542             if (port == OFPP_CONTROLLER) {
3543                 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
3544             }
3545         }
3546         break;
3547
3548     case OFPACT_CONTROLLER:
3549         controller = ofpact_get_CONTROLLER(a);
3550         if (controller->reason == OFPR_ACTION &&
3551             controller->controller_id == 0) {
3552             ds_put_format(s, "CONTROLLER:%"PRIu16,
3553                           ofpact_get_CONTROLLER(a)->max_len);
3554         } else {
3555             enum ofp_packet_in_reason reason = controller->reason;
3556
3557             ds_put_cstr(s, "controller(");
3558             if (reason != OFPR_ACTION) {
3559                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3560
3561                 ds_put_format(s, "reason=%s,",
3562                               ofputil_packet_in_reason_to_string(
3563                                   reason, reasonbuf, sizeof reasonbuf));
3564             }
3565             if (controller->max_len != UINT16_MAX) {
3566                 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
3567             }
3568             if (controller->controller_id != 0) {
3569                 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
3570             }
3571             ds_chomp(s, ',');
3572             ds_put_char(s, ')');
3573         }
3574         break;
3575
3576     case OFPACT_ENQUEUE:
3577         enqueue = ofpact_get_ENQUEUE(a);
3578         ds_put_format(s, "enqueue:");
3579         ofputil_format_port(enqueue->port, s);
3580         ds_put_format(s, ":%"PRIu32, enqueue->queue);
3581         break;
3582
3583     case OFPACT_OUTPUT_REG:
3584         ds_put_cstr(s, "output:");
3585         mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
3586         break;
3587
3588     case OFPACT_BUNDLE:
3589         bundle_format(ofpact_get_BUNDLE(a), s);
3590         break;
3591
3592     case OFPACT_SET_VLAN_VID:
3593         ds_put_format(s, "%s:%"PRIu16,
3594                       (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
3595                        ? "set_vlan_vid"
3596                        : "mod_vlan_vid"),
3597                       ofpact_get_SET_VLAN_VID(a)->vlan_vid);
3598         break;
3599
3600     case OFPACT_SET_VLAN_PCP:
3601         ds_put_format(s, "%s:%"PRIu8,
3602                       (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
3603                        ? "set_vlan_pcp"
3604                        : "mod_vlan_pcp"),
3605                       ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
3606         break;
3607
3608     case OFPACT_STRIP_VLAN:
3609         ds_put_cstr(s, a->compat == OFPUTIL_OFPAT11_POP_VLAN
3610                     ? "pop_vlan" : "strip_vlan");
3611         break;
3612
3613     case OFPACT_PUSH_VLAN:
3614         /* XXX 802.1AD case*/
3615         ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
3616         break;
3617
3618     case OFPACT_SET_ETH_SRC:
3619         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
3620                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
3621         break;
3622
3623     case OFPACT_SET_ETH_DST:
3624         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
3625                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
3626         break;
3627
3628     case OFPACT_SET_IPV4_SRC:
3629         ds_put_format(s, "mod_nw_src:"IP_FMT,
3630                       IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
3631         break;
3632
3633     case OFPACT_SET_IPV4_DST:
3634         ds_put_format(s, "mod_nw_dst:"IP_FMT,
3635                       IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
3636         break;
3637
3638     case OFPACT_SET_IP_DSCP:
3639         ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
3640         break;
3641
3642     case OFPACT_SET_IP_ECN:
3643         ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
3644         break;
3645
3646     case OFPACT_SET_IP_TTL:
3647         ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
3648         break;
3649
3650     case OFPACT_SET_L4_SRC_PORT:
3651         ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
3652         break;
3653
3654     case OFPACT_SET_L4_DST_PORT:
3655         ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
3656         break;
3657
3658     case OFPACT_REG_MOVE:
3659         nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
3660         break;
3661
3662     case OFPACT_REG_LOAD:
3663         nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
3664         break;
3665
3666     case OFPACT_SET_FIELD:
3667         set_field = ofpact_get_SET_FIELD(a);
3668         mf = set_field->field;
3669         ds_put_format(s, "set_field:");
3670         mf_format(mf, &set_field->value, NULL, s);
3671         ds_put_format(s, "->%s", mf->name);
3672         break;
3673
3674     case OFPACT_STACK_PUSH:
3675         nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
3676         break;
3677
3678     case OFPACT_STACK_POP:
3679         nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
3680         break;
3681
3682     case OFPACT_DEC_TTL:
3683         print_dec_ttl(ofpact_get_DEC_TTL(a), s);
3684         break;
3685
3686     case OFPACT_SET_MPLS_LABEL:
3687         ds_put_format(s, "set_mpls_label(%"PRIu32")",
3688                       ntohl(ofpact_get_SET_MPLS_LABEL(a)->label));
3689         break;
3690
3691     case OFPACT_SET_MPLS_TC:
3692         ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3693                       ofpact_get_SET_MPLS_TC(a)->tc);
3694         break;
3695
3696     case OFPACT_SET_MPLS_TTL:
3697         ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3698                       ofpact_get_SET_MPLS_TTL(a)->ttl);
3699         break;
3700
3701     case OFPACT_DEC_MPLS_TTL:
3702         ds_put_cstr(s, "dec_mpls_ttl");
3703         break;
3704
3705     case OFPACT_SET_TUNNEL:
3706         tunnel = ofpact_get_SET_TUNNEL(a);
3707         ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3708                       (tunnel->tun_id > UINT32_MAX
3709                        || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
3710                       tunnel->tun_id);
3711         break;
3712
3713     case OFPACT_SET_QUEUE:
3714         ds_put_format(s, "set_queue:%"PRIu32,
3715                       ofpact_get_SET_QUEUE(a)->queue_id);
3716         break;
3717
3718     case OFPACT_POP_QUEUE:
3719         ds_put_cstr(s, "pop_queue");
3720         break;
3721
3722     case OFPACT_FIN_TIMEOUT:
3723         print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
3724         break;
3725
3726     case OFPACT_RESUBMIT:
3727         resubmit = ofpact_get_RESUBMIT(a);
3728         if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
3729             ds_put_cstr(s, "resubmit:");
3730             ofputil_format_port(resubmit->in_port, s);
3731         } else {
3732             ds_put_format(s, "resubmit(");
3733             if (resubmit->in_port != OFPP_IN_PORT) {
3734                 ofputil_format_port(resubmit->in_port, s);
3735             }
3736             ds_put_char(s, ',');
3737             if (resubmit->table_id != 255) {
3738                 ds_put_format(s, "%"PRIu8, resubmit->table_id);
3739             }
3740             ds_put_char(s, ')');
3741         }
3742         break;
3743
3744     case OFPACT_LEARN:
3745         learn_format(ofpact_get_LEARN(a), s);
3746         break;
3747
3748     case OFPACT_MULTIPATH:
3749         multipath_format(ofpact_get_MULTIPATH(a), s);
3750         break;
3751
3752     case OFPACT_NOTE:
3753         print_note(ofpact_get_NOTE(a), s);
3754         break;
3755
3756     case OFPACT_PUSH_MPLS:
3757         ds_put_format(s, "push_mpls:0x%04"PRIx16,
3758                       ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
3759         break;
3760
3761     case OFPACT_POP_MPLS:
3762         ds_put_format(s, "pop_mpls:0x%04"PRIx16,
3763                       ntohs(ofpact_get_POP_MPLS(a)->ethertype));
3764         break;
3765
3766     case OFPACT_EXIT:
3767         ds_put_cstr(s, "exit");
3768         break;
3769
3770     case OFPACT_SAMPLE:
3771         sample = ofpact_get_SAMPLE(a);
3772         ds_put_format(
3773             s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
3774             ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
3775             sample->probability, sample->collector_set_id,
3776             sample->obs_domain_id, sample->obs_point_id);
3777         break;
3778
3779     case OFPACT_WRITE_ACTIONS: {
3780         struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3781         ds_put_format(s, "%s(",
3782                       ovs_instruction_name_from_type(
3783                           OVSINST_OFPIT11_WRITE_ACTIONS));
3784         ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
3785         ds_put_char(s, ')');
3786         break;
3787     }
3788
3789     case OFPACT_CLEAR_ACTIONS:
3790         ds_put_format(s, "%s",
3791                       ovs_instruction_name_from_type(
3792                           OVSINST_OFPIT11_CLEAR_ACTIONS));
3793         break;
3794
3795     case OFPACT_WRITE_METADATA:
3796         metadata = ofpact_get_WRITE_METADATA(a);
3797         ds_put_format(s, "%s:%#"PRIx64,
3798                       ovs_instruction_name_from_type(
3799                           OVSINST_OFPIT11_WRITE_METADATA),
3800                       ntohll(metadata->metadata));
3801         if (metadata->mask != OVS_BE64_MAX) {
3802             ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
3803         }
3804         break;
3805
3806     case OFPACT_GOTO_TABLE:
3807         ds_put_format(s, "%s:%"PRIu8,
3808                       ovs_instruction_name_from_type(
3809                           OVSINST_OFPIT11_GOTO_TABLE),
3810                       ofpact_get_GOTO_TABLE(a)->table_id);
3811         break;
3812
3813     case OFPACT_METER:
3814         ds_put_format(s, "%s:%"PRIu32,
3815                       ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
3816                       ofpact_get_METER(a)->meter_id);
3817         break;
3818
3819     case OFPACT_GROUP:
3820         ds_put_format(s, "group:%"PRIu32,
3821                       ofpact_get_GROUP(a)->group_id);
3822         break;
3823     }
3824 }
3825
3826 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
3827  * 'ofpacts' to 'string'. */
3828 void
3829 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
3830                struct ds *string)
3831 {
3832     if (!ofpacts_len) {
3833         ds_put_cstr(string, "drop");
3834     } else {
3835         const struct ofpact *a;
3836
3837         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3838             if (a != ofpacts) {
3839                 ds_put_cstr(string, ",");
3840             }
3841
3842             /* XXX write-actions */
3843             ofpact_format(a, string);
3844         }
3845     }
3846 }
3847 \f
3848 /* Internal use by helpers. */
3849
3850 void *
3851 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
3852 {
3853     struct ofpact *ofpact;
3854
3855     ofpact_pad(ofpacts);
3856     ofpact = ofpacts->frame = ofpbuf_put_uninit(ofpacts, len);
3857     ofpact_init(ofpact, type, len);
3858     return ofpact;
3859 }
3860
3861 void
3862 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3863 {
3864     memset(ofpact, 0, len);
3865     ofpact->type = type;
3866     ofpact->compat = OFPUTIL_ACTION_INVALID;
3867     ofpact->len = len;
3868 }
3869 \f
3870 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3871  * starting at 'ofpact'.
3872  *
3873  * This is the correct way to update a variable-length ofpact's length after
3874  * adding the variable-length part of the payload.  (See the large comment
3875  * near the end of ofp-actions.h for more information.) */
3876 void
3877 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3878 {
3879     ovs_assert(ofpact == ofpacts->frame);
3880     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3881 }
3882
3883 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
3884  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3885  * client must call this itself after adding the final ofpact to an array of
3886  * them.
3887  *
3888  * (The consequences of failing to call this function are probably not dire.
3889  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3890  * not dereference it.  That's undefined behavior, technically, but it will not
3891  * cause a real problem on common systems.  Still, it seems better to call
3892  * it.) */
3893 void
3894 ofpact_pad(struct ofpbuf *ofpacts)
3895 {
3896     unsigned int pad = PAD_SIZE(ofpbuf_size(ofpacts), OFPACT_ALIGNTO);
3897     if (pad) {
3898         ofpbuf_put_zeros(ofpacts, pad);
3899     }
3900 }
3901
3902 const char *
3903 ofpact_name(enum ofpact_type type)
3904 {
3905     switch (type) {
3906 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
3907         OFPACTS
3908 #undef OFPACT
3909     }
3910     return "<unknown>";
3911 }