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