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