Add OF11 SET MPLS LABEL and SET MPLS TC actions.
[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_OFPIT11_APPLY_ACTIONS;
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 (inst == OVSINST_OFPIT11_APPLY_ACTIONS
2159             ? next < inst
2160             : next <= inst) {
2161             const char *name = ovs_instruction_name_from_type(inst);
2162             const char *next_name = ovs_instruction_name_from_type(next);
2163
2164             if (next == inst) {
2165                 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
2166                           "1.1+ compatibility", name);
2167             } else {
2168                 VLOG_WARN("invalid instruction ordering: %s must appear "
2169                           "before %s, for OpenFlow 1.1+ compatibility",
2170                           next_name, name);
2171             }
2172             return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
2173         }
2174
2175         inst = next;
2176     }
2177
2178     return 0;
2179 }
2180 \f
2181 /* Converting ofpacts to Nicira OpenFlow extensions. */
2182
2183 static void
2184 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
2185                                 struct ofpbuf *out)
2186 {
2187     struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
2188
2189     naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
2190                                            output_reg->src.n_bits);
2191     naor->src = htonl(output_reg->src.field->nxm_header);
2192     naor->max_len = htons(output_reg->max_len);
2193 }
2194
2195 static void
2196 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
2197                          struct ofpbuf *out)
2198 {
2199     struct nx_action_resubmit *nar;
2200
2201     if (resubmit->table_id == 0xff
2202         && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
2203         nar = ofputil_put_NXAST_RESUBMIT(out);
2204     } else {
2205         nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
2206         nar->table = resubmit->table_id;
2207     }
2208     nar->in_port = htons(ofp_to_u16(resubmit->in_port));
2209 }
2210
2211 static void
2212 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
2213                            struct ofpbuf *out)
2214 {
2215     uint64_t tun_id = tunnel->tun_id;
2216
2217     if (tun_id <= UINT32_MAX
2218         && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
2219         ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
2220     } else {
2221         ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
2222     }
2223 }
2224
2225 static void
2226 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
2227                                struct ofpbuf *out)
2228 {
2229     struct nx_action_write_metadata *nawm;
2230
2231     nawm = ofputil_put_NXAST_WRITE_METADATA(out);
2232     nawm->metadata = om->metadata;
2233     nawm->mask = om->mask;
2234 }
2235
2236 static void
2237 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
2238 {
2239     size_t start_ofs = out->size;
2240     struct nx_action_note *nan;
2241     unsigned int remainder;
2242     unsigned int len;
2243
2244     nan = ofputil_put_NXAST_NOTE(out);
2245     out->size -= sizeof nan->note;
2246
2247     ofpbuf_put(out, note->data, note->length);
2248
2249     len = out->size - start_ofs;
2250     remainder = len % OFP_ACTION_ALIGN;
2251     if (remainder) {
2252         ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
2253     }
2254     nan = ofpbuf_at(out, start_ofs, sizeof *nan);
2255     nan->len = htons(out->size - start_ofs);
2256 }
2257
2258 static void
2259 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
2260                            struct ofpbuf *out)
2261 {
2262     struct nx_action_controller *nac;
2263
2264     nac = ofputil_put_NXAST_CONTROLLER(out);
2265     nac->max_len = htons(oc->max_len);
2266     nac->controller_id = htons(oc->controller_id);
2267     nac->reason = oc->reason;
2268 }
2269
2270 static void
2271 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
2272                         struct ofpbuf *out)
2273 {
2274     if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
2275         ofputil_put_NXAST_DEC_TTL(out);
2276     } else {
2277         struct nx_action_cnt_ids *nac_ids =
2278             ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
2279         int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
2280         ovs_be16 *ids;
2281         size_t i;
2282
2283         nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2284         nac_ids->n_controllers = htons(oc_ids->n_controllers);
2285
2286         ids = ofpbuf_put_zeros(out, ids_len);
2287         for (i = 0; i < oc_ids->n_controllers; i++) {
2288             ids[i] = htons(oc_ids->cnt_ids[i]);
2289         }
2290     }
2291 }
2292
2293 static void
2294 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
2295                             struct ofpbuf *out)
2296 {
2297     struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
2298     naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
2299     naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
2300 }
2301
2302 static void
2303 ofpact_sample_to_nxast(const struct ofpact_sample *os,
2304                        struct ofpbuf *out)
2305 {
2306     struct nx_action_sample *nas;
2307
2308     nas = ofputil_put_NXAST_SAMPLE(out);
2309     nas->probability = htons(os->probability);
2310     nas->collector_set_id = htonl(os->collector_set_id);
2311     nas->obs_domain_id = htonl(os->obs_domain_id);
2312     nas->obs_point_id = htonl(os->obs_point_id);
2313 }
2314
2315 static void
2316 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
2317 {
2318     switch (a->type) {
2319     case OFPACT_CONTROLLER:
2320         ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
2321         break;
2322
2323     case OFPACT_OUTPUT_REG:
2324         ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
2325         break;
2326
2327     case OFPACT_BUNDLE:
2328         bundle_to_nxast(ofpact_get_BUNDLE(a), out);
2329         break;
2330
2331     case OFPACT_REG_MOVE:
2332         nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
2333         break;
2334
2335     case OFPACT_REG_LOAD:
2336         nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
2337         break;
2338
2339     case OFPACT_STACK_PUSH:
2340         nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
2341         break;
2342
2343     case OFPACT_STACK_POP:
2344         nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
2345         break;
2346
2347     case OFPACT_DEC_TTL:
2348         ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
2349         break;
2350
2351     case OFPACT_SET_MPLS_LABEL:
2352         ofputil_put_NXAST_SET_MPLS_LABEL(out)->label
2353             = ofpact_get_SET_MPLS_LABEL(a)->label;
2354         break;
2355
2356     case OFPACT_SET_MPLS_TC:
2357         ofputil_put_NXAST_SET_MPLS_TC(out)->tc
2358             = ofpact_get_SET_MPLS_TC(a)->tc;
2359         break;
2360
2361     case OFPACT_SET_MPLS_TTL:
2362         ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
2363             = ofpact_get_SET_MPLS_TTL(a)->ttl;
2364         break;
2365
2366     case OFPACT_DEC_MPLS_TTL:
2367         ofputil_put_NXAST_DEC_MPLS_TTL(out);
2368         break;
2369
2370     case OFPACT_SET_TUNNEL:
2371         ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2372         break;
2373
2374     case OFPACT_WRITE_METADATA:
2375         ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2376         break;
2377
2378     case OFPACT_SET_QUEUE:
2379         ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2380             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2381         break;
2382
2383     case OFPACT_POP_QUEUE:
2384         ofputil_put_NXAST_POP_QUEUE(out);
2385         break;
2386
2387     case OFPACT_FIN_TIMEOUT:
2388         ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2389         break;
2390
2391     case OFPACT_RESUBMIT:
2392         ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2393         break;
2394
2395     case OFPACT_LEARN:
2396         learn_to_nxast(ofpact_get_LEARN(a), out);
2397         break;
2398
2399     case OFPACT_MULTIPATH:
2400         multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2401         break;
2402
2403     case OFPACT_NOTE:
2404         ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2405         break;
2406
2407     case OFPACT_EXIT:
2408         ofputil_put_NXAST_EXIT(out);
2409         break;
2410
2411     case OFPACT_PUSH_MPLS:
2412         ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2413             ofpact_get_PUSH_MPLS(a)->ethertype;
2414         break;
2415
2416     case OFPACT_POP_MPLS:
2417         ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2418             ofpact_get_POP_MPLS(a)->ethertype;
2419         break;
2420
2421     case OFPACT_SAMPLE:
2422         ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2423         break;
2424
2425     case OFPACT_GROUP:
2426     case OFPACT_OUTPUT:
2427     case OFPACT_ENQUEUE:
2428     case OFPACT_SET_VLAN_VID:
2429     case OFPACT_SET_VLAN_PCP:
2430     case OFPACT_STRIP_VLAN:
2431     case OFPACT_PUSH_VLAN:
2432     case OFPACT_SET_ETH_SRC:
2433     case OFPACT_SET_ETH_DST:
2434     case OFPACT_SET_IPV4_SRC:
2435     case OFPACT_SET_IPV4_DST:
2436     case OFPACT_SET_IP_DSCP:
2437     case OFPACT_SET_IP_ECN:
2438     case OFPACT_SET_IP_TTL:
2439     case OFPACT_SET_L4_SRC_PORT:
2440     case OFPACT_SET_L4_DST_PORT:
2441     case OFPACT_WRITE_ACTIONS:
2442     case OFPACT_CLEAR_ACTIONS:
2443     case OFPACT_GOTO_TABLE:
2444     case OFPACT_METER:
2445     case OFPACT_SET_FIELD:
2446         NOT_REACHED();
2447     }
2448 }
2449 \f
2450 /* Converting ofpacts to OpenFlow 1.0. */
2451
2452 static void
2453 ofpact_output_to_openflow10(const struct ofpact_output *output,
2454                             struct ofpbuf *out)
2455 {
2456     struct ofp10_action_output *oao;
2457
2458     oao = ofputil_put_OFPAT10_OUTPUT(out);
2459     oao->port = htons(ofp_to_u16(output->port));
2460     oao->max_len = htons(output->max_len);
2461 }
2462
2463 static void
2464 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2465                              struct ofpbuf *out)
2466 {
2467     struct ofp10_action_enqueue *oae;
2468
2469     oae = ofputil_put_OFPAT10_ENQUEUE(out);
2470     oae->port = htons(ofp_to_u16(enqueue->port));
2471     oae->queue_id = htonl(enqueue->queue);
2472 }
2473
2474 static void
2475 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2476 {
2477     switch (a->type) {
2478     case OFPACT_OUTPUT:
2479         ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2480         break;
2481
2482     case OFPACT_ENQUEUE:
2483         ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2484         break;
2485
2486     case OFPACT_SET_VLAN_VID:
2487         ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2488             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2489         break;
2490
2491     case OFPACT_SET_VLAN_PCP:
2492         ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2493             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2494         break;
2495
2496     case OFPACT_STRIP_VLAN:
2497         ofputil_put_OFPAT10_STRIP_VLAN(out);
2498         break;
2499
2500     case OFPACT_SET_ETH_SRC:
2501         memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2502                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2503         break;
2504
2505     case OFPACT_SET_ETH_DST:
2506         memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2507                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2508         break;
2509
2510     case OFPACT_SET_IPV4_SRC:
2511         ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2512             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2513         break;
2514
2515     case OFPACT_SET_IPV4_DST:
2516         ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2517             = ofpact_get_SET_IPV4_DST(a)->ipv4;
2518         break;
2519
2520     case OFPACT_SET_IP_DSCP:
2521         ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2522             = ofpact_get_SET_IP_DSCP(a)->dscp;
2523         break;
2524
2525     case OFPACT_SET_L4_SRC_PORT:
2526         ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2527             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2528         break;
2529
2530     case OFPACT_SET_L4_DST_PORT:
2531         ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2532             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2533         break;
2534
2535     case OFPACT_PUSH_VLAN:
2536         /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2537          * follow this action. */
2538         break;
2539
2540     case OFPACT_CLEAR_ACTIONS:
2541     case OFPACT_WRITE_ACTIONS:
2542     case OFPACT_GOTO_TABLE:
2543     case OFPACT_METER:
2544         /* XXX */
2545         break;
2546
2547     case OFPACT_GROUP:
2548         break;
2549
2550     case OFPACT_SET_FIELD:
2551         set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2552         break;
2553
2554     case OFPACT_CONTROLLER:
2555     case OFPACT_OUTPUT_REG:
2556     case OFPACT_BUNDLE:
2557     case OFPACT_REG_MOVE:
2558     case OFPACT_REG_LOAD:
2559     case OFPACT_STACK_PUSH:
2560     case OFPACT_STACK_POP:
2561     case OFPACT_DEC_TTL:
2562     case OFPACT_SET_IP_ECN:
2563     case OFPACT_SET_IP_TTL:
2564     case OFPACT_SET_MPLS_LABEL:
2565     case OFPACT_SET_MPLS_TC:
2566     case OFPACT_SET_MPLS_TTL:
2567     case OFPACT_DEC_MPLS_TTL:
2568     case OFPACT_SET_TUNNEL:
2569     case OFPACT_WRITE_METADATA:
2570     case OFPACT_SET_QUEUE:
2571     case OFPACT_POP_QUEUE:
2572     case OFPACT_FIN_TIMEOUT:
2573     case OFPACT_RESUBMIT:
2574     case OFPACT_LEARN:
2575     case OFPACT_MULTIPATH:
2576     case OFPACT_NOTE:
2577     case OFPACT_EXIT:
2578     case OFPACT_PUSH_MPLS:
2579     case OFPACT_POP_MPLS:
2580     case OFPACT_SAMPLE:
2581         ofpact_to_nxast(a, out);
2582         break;
2583     }
2584 }
2585 \f
2586 /* Converting ofpacts to OpenFlow 1.1. */
2587
2588 static void
2589 ofpact_output_to_openflow11(const struct ofpact_output *output,
2590                             struct ofpbuf *out)
2591 {
2592     struct ofp11_action_output *oao;
2593
2594     oao = ofputil_put_OFPAT11_OUTPUT(out);
2595     oao->port = ofputil_port_to_ofp11(output->port);
2596     oao->max_len = htons(output->max_len);
2597 }
2598
2599 static void
2600 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2601                              struct ofpbuf *out)
2602 {
2603     if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2604         && (!dec_ttl->ofpact.compat ||
2605             dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2606         ofputil_put_OFPAT11_DEC_NW_TTL(out);
2607     } else {
2608         ofpact_dec_ttl_to_nxast(dec_ttl, out);
2609     }
2610 }
2611
2612 static void
2613 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2614 {
2615     switch (a->type) {
2616     case OFPACT_OUTPUT:
2617         return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2618
2619     case OFPACT_ENQUEUE:
2620         /* XXX */
2621         break;
2622
2623     case OFPACT_SET_VLAN_VID:
2624         /* Push a VLAN tag, if one was not seen at action validation time. */
2625         if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2626             && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2627             ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2628                 = htons(ETH_TYPE_VLAN_8021Q);
2629         }
2630         ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2631             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2632         break;
2633
2634     case OFPACT_SET_VLAN_PCP:
2635         /* Push a VLAN tag, if one was not seen at action validation time. */
2636         if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2637             && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2638             ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2639                 = htons(ETH_TYPE_VLAN_8021Q);
2640         }
2641         ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2642             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2643         break;
2644
2645     case OFPACT_STRIP_VLAN:
2646         ofputil_put_OFPAT11_POP_VLAN(out);
2647         break;
2648
2649     case OFPACT_PUSH_VLAN:
2650         /* XXX ETH_TYPE_VLAN_8021AD case */
2651         ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2652             htons(ETH_TYPE_VLAN_8021Q);
2653         break;
2654
2655     case OFPACT_SET_QUEUE:
2656         ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2657             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2658         break;
2659
2660     case OFPACT_SET_ETH_SRC:
2661         memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2662                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2663         break;
2664
2665     case OFPACT_SET_ETH_DST:
2666         memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2667                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2668         break;
2669
2670     case OFPACT_SET_IPV4_SRC:
2671         ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2672             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2673         break;
2674
2675     case OFPACT_SET_IPV4_DST:
2676         ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2677             = ofpact_get_SET_IPV4_DST(a)->ipv4;
2678         break;
2679
2680     case OFPACT_SET_IP_DSCP:
2681         ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2682             = ofpact_get_SET_IP_DSCP(a)->dscp;
2683         break;
2684
2685     case OFPACT_SET_IP_ECN:
2686         ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2687             = ofpact_get_SET_IP_ECN(a)->ecn;
2688         break;
2689
2690     case OFPACT_SET_IP_TTL:
2691         ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2692             = ofpact_get_SET_IP_TTL(a)->ttl;
2693         break;
2694
2695     case OFPACT_SET_L4_SRC_PORT:
2696         ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2697             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2698         break;
2699
2700     case OFPACT_SET_L4_DST_PORT:
2701         ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2702             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2703         break;
2704
2705     case OFPACT_DEC_TTL:
2706         ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2707         break;
2708
2709     case OFPACT_SET_MPLS_LABEL:
2710         ofputil_put_OFPAT11_SET_MPLS_LABEL(out)->mpls_label
2711             = ofpact_get_SET_MPLS_LABEL(a)->label;
2712         break;
2713
2714     case OFPACT_SET_MPLS_TC:
2715         ofputil_put_OFPAT11_SET_MPLS_TC(out)->mpls_tc
2716             = ofpact_get_SET_MPLS_TC(a)->tc;
2717         break;
2718
2719     case OFPACT_SET_MPLS_TTL:
2720         ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2721             = ofpact_get_SET_MPLS_TTL(a)->ttl;
2722         break;
2723
2724     case OFPACT_DEC_MPLS_TTL:
2725         ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2726         break;
2727
2728     case OFPACT_WRITE_METADATA:
2729         /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2730         break;
2731
2732     case OFPACT_PUSH_MPLS:
2733         ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2734             ofpact_get_PUSH_MPLS(a)->ethertype;
2735         break;
2736
2737     case OFPACT_POP_MPLS:
2738         ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2739             ofpact_get_POP_MPLS(a)->ethertype;
2740
2741         break;
2742
2743     case OFPACT_CLEAR_ACTIONS:
2744     case OFPACT_WRITE_ACTIONS:
2745     case OFPACT_GOTO_TABLE:
2746     case OFPACT_METER:
2747         NOT_REACHED();
2748
2749     case OFPACT_GROUP:
2750         ofputil_put_OFPAT11_GROUP(out)->group_id =
2751             htonl(ofpact_get_GROUP(a)->group_id);
2752         break;
2753
2754     case OFPACT_SET_FIELD:
2755         set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2756         break;
2757
2758     case OFPACT_CONTROLLER:
2759     case OFPACT_OUTPUT_REG:
2760     case OFPACT_BUNDLE:
2761     case OFPACT_REG_MOVE:
2762     case OFPACT_REG_LOAD:
2763     case OFPACT_STACK_PUSH:
2764     case OFPACT_STACK_POP:
2765     case OFPACT_SET_TUNNEL:
2766     case OFPACT_POP_QUEUE:
2767     case OFPACT_FIN_TIMEOUT:
2768     case OFPACT_RESUBMIT:
2769     case OFPACT_LEARN:
2770     case OFPACT_MULTIPATH:
2771     case OFPACT_NOTE:
2772     case OFPACT_EXIT:
2773     case OFPACT_SAMPLE:
2774         ofpact_to_nxast(a, out);
2775         break;
2776     }
2777 }
2778
2779 /* Output deprecated set actions as set_field actions. */
2780 static void
2781 ofpact_to_openflow12(const struct ofpact *a, struct ofpbuf *out)
2782 {
2783     enum mf_field_id field;
2784     union mf_value value;
2785     struct ofpact_l4_port *l4port;
2786     uint8_t proto;
2787
2788     /*
2789      * Convert actions deprecated in OpenFlow 1.2 to Set Field actions,
2790      * if possible.
2791      */
2792     switch ((int)a->type) {
2793     case OFPACT_SET_VLAN_VID:
2794     case OFPACT_SET_VLAN_PCP:
2795     case OFPACT_SET_ETH_SRC:
2796     case OFPACT_SET_ETH_DST:
2797     case OFPACT_SET_IPV4_SRC:
2798     case OFPACT_SET_IPV4_DST:
2799     case OFPACT_SET_IP_DSCP:
2800     case OFPACT_SET_IP_ECN:
2801     case OFPACT_SET_L4_SRC_PORT:
2802     case OFPACT_SET_L4_DST_PORT:
2803     case OFPACT_SET_MPLS_LABEL:
2804     case OFPACT_SET_MPLS_TC:
2805     case OFPACT_SET_TUNNEL:  /* Convert to a set_field, too. */
2806
2807         switch ((int)a->type) {
2808
2809         case OFPACT_SET_VLAN_VID:
2810             if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan &&
2811                 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2812                 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2813                     = htons(ETH_TYPE_VLAN_8021Q);
2814             }
2815             field = MFF_VLAN_VID;
2816             /* Set-Field on OXM_OF_VLAN_VID must have OFPVID_PRESENT set. */
2817             value.be16 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid
2818                                | OFPVID12_PRESENT);
2819             break;
2820
2821         case OFPACT_SET_VLAN_PCP:
2822             if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan &&
2823                 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2824                 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2825                     = htons(ETH_TYPE_VLAN_8021Q);
2826             }
2827             field = MFF_VLAN_PCP;
2828             value.u8 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2829             break;
2830
2831         case OFPACT_SET_ETH_SRC:
2832             field = MFF_ETH_SRC;
2833             memcpy(value.mac, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2834             break;
2835
2836         case OFPACT_SET_ETH_DST:
2837             field = MFF_ETH_DST;
2838             memcpy(value.mac, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2839             break;
2840
2841         case OFPACT_SET_IPV4_SRC:
2842             field = MFF_IPV4_SRC;
2843             value.be32 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2844             break;
2845
2846         case OFPACT_SET_IPV4_DST:
2847             field = MFF_IPV4_DST;
2848             value.be32 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2849             break;
2850
2851         case OFPACT_SET_IP_DSCP:
2852             field = MFF_IP_DSCP_SHIFTED; /* OXM_OF_IP_DSCP */
2853             value.u8 = ofpact_get_SET_IP_DSCP(a)->dscp >> 2;
2854             break;
2855
2856         case OFPACT_SET_IP_ECN:
2857             field = MFF_IP_ECN;
2858             value.u8 = ofpact_get_SET_IP_ECN(a)->ecn;
2859             break;
2860
2861         case OFPACT_SET_L4_SRC_PORT:
2862             /* We keep track of IP protocol while translating actions to be
2863              * able to translate to the proper OXM type.
2864              * If the IP protocol type is unknown, the translation cannot
2865              * be performed and we will send the action using the original
2866              * action type. */
2867             l4port = ofpact_get_SET_L4_SRC_PORT(a);
2868             proto = l4port->flow_ip_proto;
2869             field = proto == IPPROTO_TCP ? MFF_TCP_SRC
2870                 : proto == IPPROTO_UDP ? MFF_UDP_SRC
2871                 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
2872                 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
2873             value.be16 = htons(l4port->port);
2874             break;
2875
2876         case OFPACT_SET_L4_DST_PORT:
2877             l4port = ofpact_get_SET_L4_DST_PORT(a);
2878             proto = l4port->flow_ip_proto;
2879             field = proto == IPPROTO_TCP ? MFF_TCP_DST
2880                 : proto == IPPROTO_UDP ? MFF_UDP_DST
2881                 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2882                 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
2883             value.be16 = htons(l4port->port);
2884             break;
2885
2886         case OFPACT_SET_MPLS_LABEL:
2887             field = MFF_MPLS_LABEL;
2888             value.be32 = ofpact_get_SET_MPLS_LABEL(a)->label;
2889             break;
2890
2891         case OFPACT_SET_MPLS_TC:
2892             field = MFF_MPLS_TC;
2893             value.u8 = ofpact_get_SET_MPLS_TC(a)->tc;
2894             break;
2895
2896         case OFPACT_SET_TUNNEL:
2897             field = MFF_TUN_ID;
2898             value.be64 = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2899             break;
2900
2901         default:
2902             field = MFF_N_IDS;
2903         }
2904
2905         /* Put the action out as a set field action, if possible. */
2906         if (field < MFF_N_IDS) {
2907             uint64_t ofpacts_stub[128 / 8];
2908             struct ofpbuf sf_act;
2909             struct ofpact_set_field *sf;
2910
2911             ofpbuf_use_stub(&sf_act, ofpacts_stub, sizeof ofpacts_stub);
2912             sf = ofpact_put_SET_FIELD(&sf_act);
2913             sf->field = mf_from_id(field);
2914             memcpy(&sf->value, &value, sf->field->n_bytes);
2915             set_field_to_openflow(sf, out);
2916             return;
2917         }
2918     }
2919
2920     ofpact_to_openflow11(a, out);
2921 }
2922
2923 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
2924  * actions in 'openflow', appending the actions to any existing data in
2925  * 'openflow'. */
2926 size_t
2927 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
2928                              struct ofpbuf *openflow,
2929                              enum ofp_version ofp_version)
2930 {
2931     const struct ofpact *a;
2932     size_t start_size = openflow->size;
2933
2934     void (*translate)(const struct ofpact *a, struct ofpbuf *out) =
2935         (ofp_version == OFP10_VERSION) ? ofpact_to_openflow10 :
2936         (ofp_version == OFP11_VERSION) ? ofpact_to_openflow11 :
2937         ofpact_to_openflow12;
2938
2939     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2940         translate(a, openflow);
2941     }
2942     return openflow->size - start_size;
2943 }
2944
2945 static void
2946 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2947 {
2948     struct ofp11_instruction_actions *oia;
2949
2950     /* Update the instruction's length (or, if it's empty, delete it). */
2951     oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2952     if (openflow->size > ofs + sizeof *oia) {
2953         oia->len = htons(openflow->size - ofs);
2954     } else {
2955         openflow->size = ofs;
2956     }
2957 }
2958
2959 void
2960 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
2961                                   size_t ofpacts_len,
2962                                   struct ofpbuf *openflow,
2963                                   enum ofp_version ofp_version)
2964 {
2965     const struct ofpact *a;
2966
2967     ovs_assert(ofp_version >= OFP11_VERSION);
2968
2969     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2970         switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2971         case OVSINST_OFPIT11_CLEAR_ACTIONS:
2972             instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2973             break;
2974
2975         case OVSINST_OFPIT11_GOTO_TABLE: {
2976             struct ofp11_instruction_goto_table *oigt;
2977             oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2978             oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2979             memset(oigt->pad, 0, sizeof oigt->pad);
2980             break;
2981         }
2982
2983         case OVSINST_OFPIT11_WRITE_METADATA: {
2984             const struct ofpact_metadata *om;
2985             struct ofp11_instruction_write_metadata *oiwm;
2986
2987             om = ofpact_get_WRITE_METADATA(a);
2988             oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2989             oiwm->metadata = om->metadata;
2990             oiwm->metadata_mask = om->mask;
2991             break;
2992         }
2993
2994         case OVSINST_OFPIT13_METER:
2995             if (ofp_version >= OFP13_VERSION) {
2996                 const struct ofpact_meter *om;
2997                 struct ofp13_instruction_meter *oim;
2998
2999                 om = ofpact_get_METER(a);
3000                 oim = instruction_put_OFPIT13_METER(openflow);
3001                 oim->meter_id = htonl(om->meter_id);
3002             }
3003             break;
3004
3005         case OVSINST_OFPIT11_APPLY_ACTIONS: {
3006             const size_t ofs = openflow->size;
3007             const size_t ofpacts_len_left =
3008                 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
3009             const struct ofpact *action;
3010             const struct ofpact *processed = a;
3011
3012             instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
3013             OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
3014                 if (ovs_instruction_type_from_ofpact_type(action->type)
3015                     != OVSINST_OFPIT11_APPLY_ACTIONS) {
3016                     break;
3017                 }
3018                 if (ofp_version == OFP11_VERSION) {
3019                     ofpact_to_openflow11(action, openflow);
3020                 } else {
3021                     ofpact_to_openflow12(action, openflow);
3022                 }
3023                 processed = action;
3024             }
3025             ofpacts_update_instruction_actions(openflow, ofs);
3026             a = processed;
3027             break;
3028         }
3029
3030         case OVSINST_OFPIT11_WRITE_ACTIONS: {
3031             const size_t ofs = openflow->size;
3032             const struct ofpact_nest *on;
3033
3034             on = ofpact_get_WRITE_ACTIONS(a);
3035             instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
3036             ofpacts_put_openflow_actions(on->actions,
3037                                          ofpact_nest_get_action_len(on),
3038                                          openflow, ofp_version);
3039             ofpacts_update_instruction_actions(openflow, ofs);
3040
3041             break;
3042         }
3043         }
3044     }
3045 }
3046 \f
3047 /* Returns true if 'action' outputs to 'port', false otherwise. */
3048 static bool
3049 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
3050 {
3051     switch (ofpact->type) {
3052     case OFPACT_OUTPUT:
3053         return ofpact_get_OUTPUT(ofpact)->port == port;
3054     case OFPACT_ENQUEUE:
3055         return ofpact_get_ENQUEUE(ofpact)->port == port;
3056     case OFPACT_CONTROLLER:
3057         return port == OFPP_CONTROLLER;
3058
3059     case OFPACT_OUTPUT_REG:
3060     case OFPACT_BUNDLE:
3061     case OFPACT_SET_VLAN_VID:
3062     case OFPACT_SET_VLAN_PCP:
3063     case OFPACT_STRIP_VLAN:
3064     case OFPACT_PUSH_VLAN:
3065     case OFPACT_SET_ETH_SRC:
3066     case OFPACT_SET_ETH_DST:
3067     case OFPACT_SET_IPV4_SRC:
3068     case OFPACT_SET_IPV4_DST:
3069     case OFPACT_SET_IP_DSCP:
3070     case OFPACT_SET_IP_ECN:
3071     case OFPACT_SET_IP_TTL:
3072     case OFPACT_SET_L4_SRC_PORT:
3073     case OFPACT_SET_L4_DST_PORT:
3074     case OFPACT_REG_MOVE:
3075     case OFPACT_REG_LOAD:
3076     case OFPACT_SET_FIELD:
3077     case OFPACT_STACK_PUSH:
3078     case OFPACT_STACK_POP:
3079     case OFPACT_DEC_TTL:
3080     case OFPACT_SET_MPLS_LABEL:
3081     case OFPACT_SET_MPLS_TC:
3082     case OFPACT_SET_MPLS_TTL:
3083     case OFPACT_DEC_MPLS_TTL:
3084     case OFPACT_SET_TUNNEL:
3085     case OFPACT_WRITE_METADATA:
3086     case OFPACT_SET_QUEUE:
3087     case OFPACT_POP_QUEUE:
3088     case OFPACT_FIN_TIMEOUT:
3089     case OFPACT_RESUBMIT:
3090     case OFPACT_LEARN:
3091     case OFPACT_MULTIPATH:
3092     case OFPACT_NOTE:
3093     case OFPACT_EXIT:
3094     case OFPACT_PUSH_MPLS:
3095     case OFPACT_POP_MPLS:
3096     case OFPACT_SAMPLE:
3097     case OFPACT_CLEAR_ACTIONS:
3098     case OFPACT_WRITE_ACTIONS:
3099     case OFPACT_GOTO_TABLE:
3100     case OFPACT_METER:
3101     case OFPACT_GROUP:
3102     default:
3103         return false;
3104     }
3105 }
3106
3107 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3108  * to 'port', false otherwise. */
3109 bool
3110 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
3111                        ofp_port_t port)
3112 {
3113     const struct ofpact *a;
3114
3115     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3116         if (ofpact_outputs_to_port(a, port)) {
3117             return true;
3118         }
3119     }
3120
3121     return false;
3122 }
3123
3124 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3125  * to 'group', false otherwise. */
3126 bool
3127 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
3128                         uint32_t group_id)
3129 {
3130     const struct ofpact *a;
3131
3132     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3133         if (a->type == OFPACT_GROUP
3134             && ofpact_get_GROUP(a)->group_id == group_id) {
3135             return true;
3136         }
3137     }
3138
3139     return false;
3140 }
3141
3142 bool
3143 ofpacts_equal(const struct ofpact *a, size_t a_len,
3144               const struct ofpact *b, size_t b_len)
3145 {
3146     return a_len == b_len && !memcmp(a, b, a_len);
3147 }
3148
3149 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
3150  * 'ofpacts'.  If found, returns its meter ID; if not, returns 0.
3151  *
3152  * This function relies on the order of 'ofpacts' being correct (as checked by
3153  * ofpacts_verify()). */
3154 uint32_t
3155 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
3156 {
3157     const struct ofpact *a;
3158
3159     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3160         enum ovs_instruction_type inst;
3161
3162         inst = ovs_instruction_type_from_ofpact_type(a->type);
3163         if (a->type == OFPACT_METER) {
3164             return ofpact_get_METER(a)->meter_id;
3165         } else if (inst > OVSINST_OFPIT13_METER) {
3166             break;
3167         }
3168     }
3169
3170     return 0;
3171 }
3172 \f
3173 /* Formatting ofpacts. */
3174
3175 static void
3176 print_note(const struct ofpact_note *note, struct ds *string)
3177 {
3178     size_t i;
3179
3180     ds_put_cstr(string, "note:");
3181     for (i = 0; i < note->length; i++) {
3182         if (i) {
3183             ds_put_char(string, '.');
3184         }
3185         ds_put_format(string, "%02"PRIx8, note->data[i]);
3186     }
3187 }
3188
3189 static void
3190 print_dec_ttl(const struct ofpact_cnt_ids *ids,
3191               struct ds *s)
3192 {
3193     size_t i;
3194
3195     ds_put_cstr(s, "dec_ttl");
3196     if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
3197         ds_put_cstr(s, "(");
3198         for (i = 0; i < ids->n_controllers; i++) {
3199             if (i) {
3200                 ds_put_cstr(s, ",");
3201             }
3202             ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
3203         }
3204         ds_put_cstr(s, ")");
3205     }
3206 }
3207
3208 static void
3209 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
3210                   struct ds *s)
3211 {
3212     ds_put_cstr(s, "fin_timeout(");
3213     if (fin_timeout->fin_idle_timeout) {
3214         ds_put_format(s, "idle_timeout=%"PRIu16",",
3215                       fin_timeout->fin_idle_timeout);
3216     }
3217     if (fin_timeout->fin_hard_timeout) {
3218         ds_put_format(s, "hard_timeout=%"PRIu16",",
3219                       fin_timeout->fin_hard_timeout);
3220     }
3221     ds_chomp(s, ',');
3222     ds_put_char(s, ')');
3223 }
3224
3225 static void
3226 ofpact_format(const struct ofpact *a, struct ds *s)
3227 {
3228     const struct ofpact_enqueue *enqueue;
3229     const struct ofpact_resubmit *resubmit;
3230     const struct ofpact_controller *controller;
3231     const struct ofpact_metadata *metadata;
3232     const struct ofpact_tunnel *tunnel;
3233     const struct ofpact_sample *sample;
3234     const struct ofpact_set_field *set_field;
3235     const struct mf_field *mf;
3236     ofp_port_t port;
3237
3238     switch (a->type) {
3239     case OFPACT_OUTPUT:
3240         port = ofpact_get_OUTPUT(a)->port;
3241         if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
3242             ds_put_format(s, "output:%"PRIu16, port);
3243         } else {
3244             ofputil_format_port(port, s);
3245             if (port == OFPP_CONTROLLER) {
3246                 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
3247             }
3248         }
3249         break;
3250
3251     case OFPACT_CONTROLLER:
3252         controller = ofpact_get_CONTROLLER(a);
3253         if (controller->reason == OFPR_ACTION &&
3254             controller->controller_id == 0) {
3255             ds_put_format(s, "CONTROLLER:%"PRIu16,
3256                           ofpact_get_CONTROLLER(a)->max_len);
3257         } else {
3258             enum ofp_packet_in_reason reason = controller->reason;
3259
3260             ds_put_cstr(s, "controller(");
3261             if (reason != OFPR_ACTION) {
3262                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3263
3264                 ds_put_format(s, "reason=%s,",
3265                               ofputil_packet_in_reason_to_string(
3266                                   reason, reasonbuf, sizeof reasonbuf));
3267             }
3268             if (controller->max_len != UINT16_MAX) {
3269                 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
3270             }
3271             if (controller->controller_id != 0) {
3272                 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
3273             }
3274             ds_chomp(s, ',');
3275             ds_put_char(s, ')');
3276         }
3277         break;
3278
3279     case OFPACT_ENQUEUE:
3280         enqueue = ofpact_get_ENQUEUE(a);
3281         ds_put_format(s, "enqueue:");
3282         ofputil_format_port(enqueue->port, s);
3283         ds_put_format(s, "q%"PRIu32, enqueue->queue);
3284         break;
3285
3286     case OFPACT_OUTPUT_REG:
3287         ds_put_cstr(s, "output:");
3288         mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
3289         break;
3290
3291     case OFPACT_BUNDLE:
3292         bundle_format(ofpact_get_BUNDLE(a), s);
3293         break;
3294
3295     case OFPACT_SET_VLAN_VID:
3296         ds_put_format(s, "%s:%"PRIu16,
3297                       (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
3298                        ? "set_vlan_vid"
3299                        : "mod_vlan_vid"),
3300                       ofpact_get_SET_VLAN_VID(a)->vlan_vid);
3301         break;
3302
3303     case OFPACT_SET_VLAN_PCP:
3304         ds_put_format(s, "%s:%"PRIu8,
3305                       (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
3306                        ? "set_vlan_pcp"
3307                        : "mod_vlan_pcp"),
3308                       ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
3309         break;
3310
3311     case OFPACT_STRIP_VLAN:
3312         ds_put_cstr(s, a->compat == OFPUTIL_OFPAT11_POP_VLAN
3313                     ? "pop_vlan" : "strip_vlan");
3314         break;
3315
3316     case OFPACT_PUSH_VLAN:
3317         /* XXX 802.1AD case*/
3318         ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
3319         break;
3320
3321     case OFPACT_SET_ETH_SRC:
3322         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
3323                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
3324         break;
3325
3326     case OFPACT_SET_ETH_DST:
3327         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
3328                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
3329         break;
3330
3331     case OFPACT_SET_IPV4_SRC:
3332         ds_put_format(s, "mod_nw_src:"IP_FMT,
3333                       IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
3334         break;
3335
3336     case OFPACT_SET_IPV4_DST:
3337         ds_put_format(s, "mod_nw_dst:"IP_FMT,
3338                       IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
3339         break;
3340
3341     case OFPACT_SET_IP_DSCP:
3342         ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
3343         break;
3344
3345     case OFPACT_SET_IP_ECN:
3346         ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
3347         break;
3348
3349     case OFPACT_SET_IP_TTL:
3350         ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
3351         break;
3352
3353     case OFPACT_SET_L4_SRC_PORT:
3354         ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
3355         break;
3356
3357     case OFPACT_SET_L4_DST_PORT:
3358         ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
3359         break;
3360
3361     case OFPACT_REG_MOVE:
3362         nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
3363         break;
3364
3365     case OFPACT_REG_LOAD:
3366         nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
3367         break;
3368
3369     case OFPACT_SET_FIELD:
3370         set_field = ofpact_get_SET_FIELD(a);
3371         mf = set_field->field;
3372         ds_put_format(s, "set_field:");
3373         mf_format(mf, &set_field->value, NULL, s);
3374         ds_put_format(s, "->%s", mf->name);
3375         break;
3376
3377     case OFPACT_STACK_PUSH:
3378         nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
3379         break;
3380
3381     case OFPACT_STACK_POP:
3382         nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
3383         break;
3384
3385     case OFPACT_DEC_TTL:
3386         print_dec_ttl(ofpact_get_DEC_TTL(a), s);
3387         break;
3388
3389     case OFPACT_SET_MPLS_LABEL:
3390         ds_put_format(s, "set_mpls_label(%"PRIu32")",
3391                       ntohl(ofpact_get_SET_MPLS_LABEL(a)->label));
3392         break;
3393
3394     case OFPACT_SET_MPLS_TC:
3395         ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3396                       ofpact_get_SET_MPLS_TC(a)->tc);
3397         break;
3398
3399     case OFPACT_SET_MPLS_TTL:
3400         ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3401                       ofpact_get_SET_MPLS_TTL(a)->ttl);
3402         break;
3403
3404     case OFPACT_DEC_MPLS_TTL:
3405         ds_put_cstr(s, "dec_mpls_ttl");
3406         break;
3407
3408     case OFPACT_SET_TUNNEL:
3409         tunnel = ofpact_get_SET_TUNNEL(a);
3410         ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3411                       (tunnel->tun_id > UINT32_MAX
3412                        || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
3413                       tunnel->tun_id);
3414         break;
3415
3416     case OFPACT_SET_QUEUE:
3417         ds_put_format(s, "set_queue:%"PRIu32,
3418                       ofpact_get_SET_QUEUE(a)->queue_id);
3419         break;
3420
3421     case OFPACT_POP_QUEUE:
3422         ds_put_cstr(s, "pop_queue");
3423         break;
3424
3425     case OFPACT_FIN_TIMEOUT:
3426         print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
3427         break;
3428
3429     case OFPACT_RESUBMIT:
3430         resubmit = ofpact_get_RESUBMIT(a);
3431         if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
3432             ds_put_cstr(s, "resubmit:");
3433             ofputil_format_port(resubmit->in_port, s);
3434         } else {
3435             ds_put_format(s, "resubmit(");
3436             if (resubmit->in_port != OFPP_IN_PORT) {
3437                 ofputil_format_port(resubmit->in_port, s);
3438             }
3439             ds_put_char(s, ',');
3440             if (resubmit->table_id != 255) {
3441                 ds_put_format(s, "%"PRIu8, resubmit->table_id);
3442             }
3443             ds_put_char(s, ')');
3444         }
3445         break;
3446
3447     case OFPACT_LEARN:
3448         learn_format(ofpact_get_LEARN(a), s);
3449         break;
3450
3451     case OFPACT_MULTIPATH:
3452         multipath_format(ofpact_get_MULTIPATH(a), s);
3453         break;
3454
3455     case OFPACT_NOTE:
3456         print_note(ofpact_get_NOTE(a), s);
3457         break;
3458
3459     case OFPACT_PUSH_MPLS:
3460         ds_put_format(s, "push_mpls:0x%04"PRIx16,
3461                       ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
3462         break;
3463
3464     case OFPACT_POP_MPLS:
3465         ds_put_format(s, "pop_mpls:0x%04"PRIx16,
3466                       ntohs(ofpact_get_POP_MPLS(a)->ethertype));
3467         break;
3468
3469     case OFPACT_EXIT:
3470         ds_put_cstr(s, "exit");
3471         break;
3472
3473     case OFPACT_SAMPLE:
3474         sample = ofpact_get_SAMPLE(a);
3475         ds_put_format(
3476             s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
3477             ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
3478             sample->probability, sample->collector_set_id,
3479             sample->obs_domain_id, sample->obs_point_id);
3480         break;
3481
3482     case OFPACT_WRITE_ACTIONS: {
3483         struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3484         ds_put_format(s, "%s(",
3485                       ovs_instruction_name_from_type(
3486                           OVSINST_OFPIT11_WRITE_ACTIONS));
3487         ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
3488         ds_put_char(s, ')');
3489         break;
3490     }
3491
3492     case OFPACT_CLEAR_ACTIONS:
3493         ds_put_format(s, "%s",
3494                       ovs_instruction_name_from_type(
3495                           OVSINST_OFPIT11_CLEAR_ACTIONS));
3496         break;
3497
3498     case OFPACT_WRITE_METADATA:
3499         metadata = ofpact_get_WRITE_METADATA(a);
3500         ds_put_format(s, "%s:%#"PRIx64,
3501                       ovs_instruction_name_from_type(
3502                           OVSINST_OFPIT11_WRITE_METADATA),
3503                       ntohll(metadata->metadata));
3504         if (metadata->mask != OVS_BE64_MAX) {
3505             ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
3506         }
3507         break;
3508
3509     case OFPACT_GOTO_TABLE:
3510         ds_put_format(s, "%s:%"PRIu8,
3511                       ovs_instruction_name_from_type(
3512                           OVSINST_OFPIT11_GOTO_TABLE),
3513                       ofpact_get_GOTO_TABLE(a)->table_id);
3514         break;
3515
3516     case OFPACT_METER:
3517         ds_put_format(s, "%s:%"PRIu32,
3518                       ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
3519                       ofpact_get_METER(a)->meter_id);
3520         break;
3521
3522     case OFPACT_GROUP:
3523         ds_put_format(s, "group:%"PRIu32,
3524                       ofpact_get_GROUP(a)->group_id);
3525         break;
3526     }
3527 }
3528
3529 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
3530  * 'ofpacts' to 'string'. */
3531 void
3532 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
3533                struct ds *string)
3534 {
3535     if (!ofpacts_len) {
3536         ds_put_cstr(string, "drop");
3537     } else {
3538         const struct ofpact *a;
3539
3540         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3541             if (a != ofpacts) {
3542                 ds_put_cstr(string, ",");
3543             }
3544
3545             /* XXX write-actions */
3546             ofpact_format(a, string);
3547         }
3548     }
3549 }
3550 \f
3551 /* Internal use by helpers. */
3552
3553 void *
3554 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
3555 {
3556     struct ofpact *ofpact;
3557
3558     ofpact_pad(ofpacts);
3559     ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
3560     ofpact_init(ofpact, type, len);
3561     return ofpact;
3562 }
3563
3564 void
3565 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3566 {
3567     memset(ofpact, 0, len);
3568     ofpact->type = type;
3569     ofpact->compat = OFPUTIL_ACTION_INVALID;
3570     ofpact->len = len;
3571 }
3572 \f
3573 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3574  * starting at 'ofpact'.
3575  *
3576  * This is the correct way to update a variable-length ofpact's length after
3577  * adding the variable-length part of the payload.  (See the large comment
3578  * near the end of ofp-actions.h for more information.) */
3579 void
3580 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3581 {
3582     ovs_assert(ofpact == ofpacts->l2);
3583     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3584 }
3585
3586 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
3587  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3588  * client must call this itself after adding the final ofpact to an array of
3589  * them.
3590  *
3591  * (The consequences of failing to call this function are probably not dire.
3592  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3593  * not dereference it.  That's undefined behavior, technically, but it will not
3594  * cause a real problem on common systems.  Still, it seems better to call
3595  * it.) */
3596 void
3597 ofpact_pad(struct ofpbuf *ofpacts)
3598 {
3599     unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
3600     if (rem) {
3601         ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
3602     }
3603 }