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