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