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