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