ofp-util: Don't return static data in ofputil_packet_in_reason_to_string().
[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 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         ids = out->l2;
213     }
214
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                                      uint8_t table_id,
1056                                      struct ofpbuf *ofpacts)
1057 {
1058     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1059     const struct ofp11_instruction *instructions;
1060     const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1061     enum ofperr error;
1062
1063     ofpbuf_clear(ofpacts);
1064
1065     if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1066         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1067                      "multiple of %d",
1068                      instructions_len, OFP11_INSTRUCTION_ALIGN);
1069         error = OFPERR_OFPBIC_BAD_LEN;
1070         goto exit;
1071     }
1072
1073     instructions = ofpbuf_try_pull(openflow, instructions_len);
1074     if (instructions == NULL) {
1075         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1076                      "remaining message length (%zu)",
1077                      instructions_len, openflow->size);
1078         error = OFPERR_OFPBIC_BAD_LEN;
1079         goto exit;
1080     }
1081
1082     error = decode_openflow11_instructions(
1083         instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1084         insts);
1085     if (error) {
1086         goto exit;
1087     }
1088
1089     if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1090         const union ofp_action *actions;
1091         size_t n_actions;
1092
1093         get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1094                                      &actions, &n_actions);
1095         error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1096         if (error) {
1097             goto exit;
1098         }
1099     }
1100     if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1101         instruction_get_OFPIT11_CLEAR_ACTIONS(
1102             insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1103         ofpact_put_CLEAR_ACTIONS(ofpacts);
1104     }
1105     /* XXX Write-Actions */
1106     if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1107         const struct ofp11_instruction_write_metadata *oiwm;
1108         struct ofpact_metadata *om;
1109
1110         oiwm = (const struct ofp11_instruction_write_metadata *)
1111             insts[OVSINST_OFPIT11_WRITE_METADATA];
1112
1113         om = ofpact_put_WRITE_METADATA(ofpacts);
1114         om->metadata = oiwm->metadata;
1115         om->mask = oiwm->metadata_mask;
1116     }
1117     if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1118         const struct ofp11_instruction_goto_table *oigt;
1119         struct ofpact_goto_table *ogt;
1120
1121         oigt = instruction_get_OFPIT11_GOTO_TABLE(
1122             insts[OVSINST_OFPIT11_GOTO_TABLE]);
1123         if (table_id >= oigt->table_id) {
1124             error = OFPERR_OFPBRC_BAD_TABLE_ID;
1125             goto exit;
1126         }
1127         ogt = ofpact_put_GOTO_TABLE(ofpacts);
1128         ogt->table_id = oigt->table_id;
1129     }
1130
1131     if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1132         error = OFPERR_OFPBIC_UNSUP_INST;
1133         goto exit;
1134     }
1135
1136     error = ofpacts_verify(ofpacts->data, ofpacts->size);
1137 exit:
1138     if (error) {
1139         ofpbuf_clear(ofpacts);
1140     }
1141     return error;
1142 }
1143 \f
1144 static enum ofperr
1145 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports,
1146                ovs_be16 *dl_type)
1147 {
1148     const struct ofpact_enqueue *enqueue;
1149
1150     switch (a->type) {
1151     case OFPACT_OUTPUT:
1152         return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1153                                          max_ports);
1154
1155     case OFPACT_CONTROLLER:
1156         return 0;
1157
1158     case OFPACT_ENQUEUE:
1159         enqueue = ofpact_get_ENQUEUE(a);
1160         if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
1161             && enqueue->port != OFPP_LOCAL) {
1162             return OFPERR_OFPBAC_BAD_OUT_PORT;
1163         }
1164         return 0;
1165
1166     case OFPACT_OUTPUT_REG:
1167         return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1168
1169     case OFPACT_BUNDLE:
1170         return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1171
1172     case OFPACT_SET_VLAN_VID:
1173     case OFPACT_SET_VLAN_PCP:
1174     case OFPACT_STRIP_VLAN:
1175     case OFPACT_PUSH_VLAN:
1176     case OFPACT_SET_ETH_SRC:
1177     case OFPACT_SET_ETH_DST:
1178     case OFPACT_SET_IPV4_SRC:
1179     case OFPACT_SET_IPV4_DST:
1180     case OFPACT_SET_IPV4_DSCP:
1181     case OFPACT_SET_L4_SRC_PORT:
1182     case OFPACT_SET_L4_DST_PORT:
1183         return 0;
1184
1185     case OFPACT_REG_MOVE:
1186         return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1187
1188     case OFPACT_REG_LOAD:
1189         return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1190
1191     case OFPACT_STACK_PUSH:
1192         return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1193
1194     case OFPACT_STACK_POP:
1195         return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1196
1197     case OFPACT_DEC_TTL:
1198     case OFPACT_SET_MPLS_TTL:
1199     case OFPACT_DEC_MPLS_TTL:
1200     case OFPACT_SET_TUNNEL:
1201     case OFPACT_SET_QUEUE:
1202     case OFPACT_POP_QUEUE:
1203     case OFPACT_FIN_TIMEOUT:
1204     case OFPACT_RESUBMIT:
1205         return 0;
1206
1207     case OFPACT_LEARN:
1208         return learn_check(ofpact_get_LEARN(a), flow);
1209
1210     case OFPACT_MULTIPATH:
1211         return multipath_check(ofpact_get_MULTIPATH(a), flow);
1212
1213     case OFPACT_NOTE:
1214     case OFPACT_EXIT:
1215         return 0;
1216
1217     case OFPACT_PUSH_MPLS:
1218         *dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1219         return 0;
1220
1221     case OFPACT_POP_MPLS:
1222         *dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1223         return 0;
1224
1225     case OFPACT_SAMPLE:
1226         return 0;
1227
1228     case OFPACT_CLEAR_ACTIONS:
1229     case OFPACT_WRITE_METADATA:
1230     case OFPACT_GOTO_TABLE:
1231         return 0;
1232
1233     default:
1234         NOT_REACHED();
1235     }
1236 }
1237
1238 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1239  * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1240  * switch with no more than 'max_ports' ports. */
1241 enum ofperr
1242 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1243               const struct flow *flow, int max_ports)
1244 {
1245     const struct ofpact *a;
1246     ovs_be16 dl_type = flow->dl_type;
1247     struct flow updated_flow;
1248
1249     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1250         enum ofperr error;
1251
1252         /* If the dl_type was changed by an action then its new value
1253          * should be present in the flow passed to ofpact_check__(). */
1254         if (flow->dl_type != dl_type) {
1255             /* Only copy flow at most once */
1256             if (flow != &updated_flow) {
1257                 updated_flow = *flow;
1258                 flow = &updated_flow;
1259             }
1260             updated_flow.dl_type = dl_type;
1261         }
1262
1263         error = ofpact_check__(a, flow, max_ports, &dl_type);
1264         if (error) {
1265             return error;
1266         }
1267     }
1268
1269     return 0;
1270 }
1271
1272 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1273  * in the appropriate order as defined by the OpenFlow spec. */
1274 enum ofperr
1275 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1276 {
1277     const struct ofpact *a;
1278     enum ovs_instruction_type inst;
1279
1280     inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1281     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1282         enum ovs_instruction_type next;
1283
1284         if (a->type == OFPACT_CLEAR_ACTIONS) {
1285             next = OVSINST_OFPIT11_CLEAR_ACTIONS;
1286         } else if (a->type == OFPACT_WRITE_METADATA) {
1287             next = OVSINST_OFPIT11_WRITE_METADATA;
1288         } else if (a->type == OFPACT_GOTO_TABLE) {
1289             next = OVSINST_OFPIT11_GOTO_TABLE;
1290         } else {
1291             next = OVSINST_OFPIT11_APPLY_ACTIONS;
1292         }
1293
1294         if (inst != OVSINST_OFPIT11_APPLY_ACTIONS && next <= inst) {
1295             const char *name = ofpact_instruction_name_from_type(inst);
1296             const char *next_name = ofpact_instruction_name_from_type(next);
1297
1298             if (next == inst) {
1299                 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1300                           "1.1+ compatibility", name);
1301             } else {
1302                 VLOG_WARN("invalid instruction ordering: %s must appear "
1303                           "before %s, for OpenFlow 1.1+ compatibility",
1304                           next_name, name);
1305             }
1306             return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1307         }
1308
1309         inst = next;
1310     }
1311
1312     return 0;
1313 }
1314 \f
1315 /* Converting ofpacts to Nicira OpenFlow extensions. */
1316
1317 static void
1318 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1319                                 struct ofpbuf *out)
1320 {
1321     struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1322
1323     naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1324                                            output_reg->src.n_bits);
1325     naor->src = htonl(output_reg->src.field->nxm_header);
1326     naor->max_len = htons(output_reg->max_len);
1327 }
1328
1329 static void
1330 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1331                          struct ofpbuf *out)
1332 {
1333     struct nx_action_resubmit *nar;
1334
1335     if (resubmit->table_id == 0xff
1336         && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1337         nar = ofputil_put_NXAST_RESUBMIT(out);
1338     } else {
1339         nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1340         nar->table = resubmit->table_id;
1341     }
1342     nar->in_port = htons(resubmit->in_port);
1343 }
1344
1345 static void
1346 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1347                            struct ofpbuf *out)
1348 {
1349     uint64_t tun_id = tunnel->tun_id;
1350
1351     if (tun_id <= UINT32_MAX
1352         && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1353         ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1354     } else {
1355         ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1356     }
1357 }
1358
1359 static void
1360 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1361                                struct ofpbuf *out)
1362 {
1363     struct nx_action_write_metadata *nawm;
1364
1365     nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1366     nawm->metadata = om->metadata;
1367     nawm->mask = om->mask;
1368 }
1369
1370 static void
1371 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1372 {
1373     size_t start_ofs = out->size;
1374     struct nx_action_note *nan;
1375     unsigned int remainder;
1376     unsigned int len;
1377
1378     nan = ofputil_put_NXAST_NOTE(out);
1379     out->size -= sizeof nan->note;
1380
1381     ofpbuf_put(out, note->data, note->length);
1382
1383     len = out->size - start_ofs;
1384     remainder = len % OFP_ACTION_ALIGN;
1385     if (remainder) {
1386         ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1387     }
1388     nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1389     nan->len = htons(out->size - start_ofs);
1390 }
1391
1392 static void
1393 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1394                            struct ofpbuf *out)
1395 {
1396     struct nx_action_controller *nac;
1397
1398     nac = ofputil_put_NXAST_CONTROLLER(out);
1399     nac->max_len = htons(oc->max_len);
1400     nac->controller_id = htons(oc->controller_id);
1401     nac->reason = oc->reason;
1402 }
1403
1404 static void
1405 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1406                         struct ofpbuf *out)
1407 {
1408     if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1409         ofputil_put_NXAST_DEC_TTL(out);
1410     } else {
1411         struct nx_action_cnt_ids *nac_ids =
1412             ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1413         int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1414         ovs_be16 *ids;
1415         size_t i;
1416
1417         nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1418         nac_ids->n_controllers = htons(oc_ids->n_controllers);
1419
1420         ids = ofpbuf_put_zeros(out, ids_len);
1421         for (i = 0; i < oc_ids->n_controllers; i++) {
1422             ids[i] = htons(oc_ids->cnt_ids[i]);
1423         }
1424     }
1425 }
1426
1427 static void
1428 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1429                             struct ofpbuf *out)
1430 {
1431     struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1432     naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1433     naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1434 }
1435
1436 static void
1437 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1438                        struct ofpbuf *out)
1439 {
1440     struct nx_action_sample *nas;
1441
1442     nas = ofputil_put_NXAST_SAMPLE(out);
1443     nas->probability = htons(os->probability);
1444     nas->collector_set_id = htonl(os->collector_set_id);
1445     nas->obs_domain_id = htonl(os->obs_domain_id);
1446     nas->obs_point_id = htonl(os->obs_point_id);
1447 }
1448
1449 static void
1450 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1451 {
1452     switch (a->type) {
1453     case OFPACT_CONTROLLER:
1454         ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1455         break;
1456
1457     case OFPACT_OUTPUT_REG:
1458         ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1459         break;
1460
1461     case OFPACT_BUNDLE:
1462         bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1463         break;
1464
1465     case OFPACT_REG_MOVE:
1466         nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1467         break;
1468
1469     case OFPACT_REG_LOAD:
1470         nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1471         break;
1472
1473     case OFPACT_STACK_PUSH:
1474         nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1475         break;
1476
1477     case OFPACT_STACK_POP:
1478         nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1479         break;
1480
1481     case OFPACT_DEC_TTL:
1482         ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1483         break;
1484
1485     case OFPACT_SET_MPLS_TTL:
1486         ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1487             = ofpact_get_SET_MPLS_TTL(a)->ttl;
1488         break;
1489
1490     case OFPACT_DEC_MPLS_TTL:
1491         ofputil_put_NXAST_DEC_MPLS_TTL(out);
1492         break;
1493
1494     case OFPACT_SET_TUNNEL:
1495         ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1496         break;
1497
1498     case OFPACT_WRITE_METADATA:
1499         ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1500         break;
1501
1502     case OFPACT_SET_QUEUE:
1503         ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1504             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1505         break;
1506
1507     case OFPACT_POP_QUEUE:
1508         ofputil_put_NXAST_POP_QUEUE(out);
1509         break;
1510
1511     case OFPACT_FIN_TIMEOUT:
1512         ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1513         break;
1514
1515     case OFPACT_RESUBMIT:
1516         ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1517         break;
1518
1519     case OFPACT_LEARN:
1520         learn_to_nxast(ofpact_get_LEARN(a), out);
1521         break;
1522
1523     case OFPACT_MULTIPATH:
1524         multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1525         break;
1526
1527     case OFPACT_NOTE:
1528         ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1529         break;
1530
1531     case OFPACT_EXIT:
1532         ofputil_put_NXAST_EXIT(out);
1533         break;
1534
1535     case OFPACT_PUSH_MPLS:
1536         ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1537             ofpact_get_PUSH_MPLS(a)->ethertype;
1538         break;
1539
1540     case OFPACT_POP_MPLS:
1541         ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1542             ofpact_get_POP_MPLS(a)->ethertype;
1543         break;
1544
1545     case OFPACT_SAMPLE:
1546         ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1547         break;
1548
1549     case OFPACT_OUTPUT:
1550     case OFPACT_ENQUEUE:
1551     case OFPACT_SET_VLAN_VID:
1552     case OFPACT_SET_VLAN_PCP:
1553     case OFPACT_STRIP_VLAN:
1554     case OFPACT_PUSH_VLAN:
1555     case OFPACT_SET_ETH_SRC:
1556     case OFPACT_SET_ETH_DST:
1557     case OFPACT_SET_IPV4_SRC:
1558     case OFPACT_SET_IPV4_DST:
1559     case OFPACT_SET_IPV4_DSCP:
1560     case OFPACT_SET_L4_SRC_PORT:
1561     case OFPACT_SET_L4_DST_PORT:
1562     case OFPACT_CLEAR_ACTIONS:
1563     case OFPACT_GOTO_TABLE:
1564         NOT_REACHED();
1565     }
1566 }
1567 \f
1568 /* Converting ofpacts to OpenFlow 1.0. */
1569
1570 static void
1571 ofpact_output_to_openflow10(const struct ofpact_output *output,
1572                             struct ofpbuf *out)
1573 {
1574     struct ofp10_action_output *oao;
1575
1576     oao = ofputil_put_OFPAT10_OUTPUT(out);
1577     oao->port = htons(output->port);
1578     oao->max_len = htons(output->max_len);
1579 }
1580
1581 static void
1582 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1583                              struct ofpbuf *out)
1584 {
1585     struct ofp10_action_enqueue *oae;
1586
1587     oae = ofputil_put_OFPAT10_ENQUEUE(out);
1588     oae->port = htons(enqueue->port);
1589     oae->queue_id = htonl(enqueue->queue);
1590 }
1591
1592 static void
1593 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1594 {
1595     switch (a->type) {
1596     case OFPACT_OUTPUT:
1597         ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1598         break;
1599
1600     case OFPACT_ENQUEUE:
1601         ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1602         break;
1603
1604     case OFPACT_SET_VLAN_VID:
1605         ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1606             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1607         break;
1608
1609     case OFPACT_SET_VLAN_PCP:
1610         ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1611             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1612         break;
1613
1614     case OFPACT_STRIP_VLAN:
1615         ofputil_put_OFPAT10_STRIP_VLAN(out);
1616         break;
1617
1618     case OFPACT_SET_ETH_SRC:
1619         memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1620                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1621         break;
1622
1623     case OFPACT_SET_ETH_DST:
1624         memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1625                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1626         break;
1627
1628     case OFPACT_SET_IPV4_SRC:
1629         ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1630             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1631         break;
1632
1633     case OFPACT_SET_IPV4_DST:
1634         ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1635             = ofpact_get_SET_IPV4_DST(a)->ipv4;
1636         break;
1637
1638     case OFPACT_SET_IPV4_DSCP:
1639         ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1640             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1641         break;
1642
1643     case OFPACT_SET_L4_SRC_PORT:
1644         ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1645             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1646         break;
1647
1648     case OFPACT_SET_L4_DST_PORT:
1649         ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1650             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1651         break;
1652
1653     case OFPACT_PUSH_VLAN:
1654     case OFPACT_CLEAR_ACTIONS:
1655     case OFPACT_GOTO_TABLE:
1656         /* XXX */
1657         break;
1658
1659     case OFPACT_CONTROLLER:
1660     case OFPACT_OUTPUT_REG:
1661     case OFPACT_BUNDLE:
1662     case OFPACT_REG_MOVE:
1663     case OFPACT_REG_LOAD:
1664     case OFPACT_STACK_PUSH:
1665     case OFPACT_STACK_POP:
1666     case OFPACT_DEC_TTL:
1667     case OFPACT_SET_MPLS_TTL:
1668     case OFPACT_DEC_MPLS_TTL:
1669     case OFPACT_SET_TUNNEL:
1670     case OFPACT_WRITE_METADATA:
1671     case OFPACT_SET_QUEUE:
1672     case OFPACT_POP_QUEUE:
1673     case OFPACT_FIN_TIMEOUT:
1674     case OFPACT_RESUBMIT:
1675     case OFPACT_LEARN:
1676     case OFPACT_MULTIPATH:
1677     case OFPACT_NOTE:
1678     case OFPACT_EXIT:
1679     case OFPACT_PUSH_MPLS:
1680     case OFPACT_POP_MPLS:
1681     case OFPACT_SAMPLE:
1682         ofpact_to_nxast(a, out);
1683         break;
1684     }
1685 }
1686
1687 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1688  * actions in 'openflow', appending the actions to any existing data in
1689  * 'openflow'. */
1690 void
1691 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1692                        struct ofpbuf *openflow)
1693 {
1694     const struct ofpact *a;
1695
1696     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1697         ofpact_to_openflow10(a, openflow);
1698     }
1699 }
1700 \f
1701 /* Converting ofpacts to OpenFlow 1.1. */
1702
1703 static void
1704 ofpact_output_to_openflow11(const struct ofpact_output *output,
1705                             struct ofpbuf *out)
1706 {
1707     struct ofp11_action_output *oao;
1708
1709     oao = ofputil_put_OFPAT11_OUTPUT(out);
1710     oao->port = ofputil_port_to_ofp11(output->port);
1711     oao->max_len = htons(output->max_len);
1712 }
1713
1714 static void
1715 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
1716                              struct ofpbuf *out)
1717 {
1718     if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
1719         && (!dec_ttl->ofpact.compat ||
1720             dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
1721         ofputil_put_OFPAT11_DEC_NW_TTL(out);
1722     } else {
1723         ofpact_dec_ttl_to_nxast(dec_ttl, out);
1724     }
1725 }
1726
1727 static void
1728 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1729 {
1730     switch (a->type) {
1731     case OFPACT_OUTPUT:
1732         return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1733
1734     case OFPACT_ENQUEUE:
1735         /* XXX */
1736         break;
1737
1738     case OFPACT_SET_VLAN_VID:
1739         ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1740             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1741         break;
1742
1743     case OFPACT_SET_VLAN_PCP:
1744         ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1745             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1746         break;
1747
1748     case OFPACT_STRIP_VLAN:
1749         ofputil_put_OFPAT11_POP_VLAN(out);
1750         break;
1751
1752     case OFPACT_PUSH_VLAN:
1753         /* XXX ETH_TYPE_VLAN_8021AD case */
1754         ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
1755             htons(ETH_TYPE_VLAN_8021Q);
1756         break;
1757
1758     case OFPACT_SET_QUEUE:
1759         ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
1760             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1761         break;
1762
1763     case OFPACT_SET_ETH_SRC:
1764         memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1765                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1766         break;
1767
1768     case OFPACT_SET_ETH_DST:
1769         memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1770                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1771         break;
1772
1773     case OFPACT_SET_IPV4_SRC:
1774         ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1775             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1776         break;
1777
1778     case OFPACT_SET_IPV4_DST:
1779         ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1780             = ofpact_get_SET_IPV4_DST(a)->ipv4;
1781         break;
1782
1783     case OFPACT_SET_IPV4_DSCP:
1784         ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1785             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1786         break;
1787
1788     case OFPACT_SET_L4_SRC_PORT:
1789         ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1790             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1791         break;
1792
1793     case OFPACT_SET_L4_DST_PORT:
1794         ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1795             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1796         break;
1797
1798     case OFPACT_DEC_TTL:
1799         ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
1800         break;
1801
1802     case OFPACT_SET_MPLS_TTL:
1803         ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
1804             = ofpact_get_SET_MPLS_TTL(a)->ttl;
1805         break;
1806
1807     case OFPACT_DEC_MPLS_TTL:
1808         ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
1809         break;
1810
1811     case OFPACT_WRITE_METADATA:
1812         /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
1813         break;
1814
1815     case OFPACT_PUSH_MPLS:
1816         ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
1817             ofpact_get_PUSH_MPLS(a)->ethertype;
1818         break;
1819
1820     case OFPACT_POP_MPLS:
1821         ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
1822             ofpact_get_POP_MPLS(a)->ethertype;
1823
1824         break;
1825
1826     case OFPACT_CLEAR_ACTIONS:
1827     case OFPACT_GOTO_TABLE:
1828         NOT_REACHED();
1829
1830     case OFPACT_CONTROLLER:
1831     case OFPACT_OUTPUT_REG:
1832     case OFPACT_BUNDLE:
1833     case OFPACT_REG_MOVE:
1834     case OFPACT_REG_LOAD:
1835     case OFPACT_STACK_PUSH:
1836     case OFPACT_STACK_POP:
1837     case OFPACT_SET_TUNNEL:
1838     case OFPACT_POP_QUEUE:
1839     case OFPACT_FIN_TIMEOUT:
1840     case OFPACT_RESUBMIT:
1841     case OFPACT_LEARN:
1842     case OFPACT_MULTIPATH:
1843     case OFPACT_NOTE:
1844     case OFPACT_EXIT:
1845     case OFPACT_SAMPLE:
1846         ofpact_to_nxast(a, out);
1847         break;
1848     }
1849 }
1850
1851 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1852  * 1.1 actions in 'openflow', appending the actions to any existing data in
1853  * 'openflow'. */
1854 size_t
1855 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1856                                size_t ofpacts_len, struct ofpbuf *openflow)
1857 {
1858     const struct ofpact *a;
1859     size_t start_size = openflow->size;
1860
1861     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1862         ofpact_to_openflow11(a, openflow);
1863     }
1864
1865     return openflow->size - start_size;
1866 }
1867
1868 static void
1869 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
1870 {
1871     struct ofp11_instruction_actions *oia;
1872
1873     /* Update the instruction's length (or, if it's empty, delete it). */
1874     oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1875     if (openflow->size > ofs + sizeof *oia) {
1876         oia->len = htons(openflow->size - ofs);
1877     } else {
1878         openflow->size = ofs;
1879     }
1880 }
1881
1882 void
1883 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1884                                     size_t ofpacts_len,
1885                                     struct ofpbuf *openflow)
1886 {
1887     const struct ofpact *a;
1888
1889     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1890         /* XXX Write-Actions */
1891
1892         if (a->type == OFPACT_CLEAR_ACTIONS) {
1893             instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
1894         } else if (a->type == OFPACT_GOTO_TABLE) {
1895             struct ofp11_instruction_goto_table *oigt;
1896
1897             oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1898             oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1899             memset(oigt->pad, 0, sizeof oigt->pad);
1900         } else if (a->type == OFPACT_WRITE_METADATA) {
1901             const struct ofpact_metadata *om;
1902             struct ofp11_instruction_write_metadata *oiwm;
1903
1904             om = ofpact_get_WRITE_METADATA(a);
1905             oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
1906             oiwm->metadata = om->metadata;
1907             oiwm->metadata_mask = om->mask;
1908         } else if (!ofpact_is_instruction(a)) {
1909             /* Apply-actions */
1910             const size_t ofs = openflow->size;
1911             const size_t ofpacts_len_left =
1912                 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1913             const struct ofpact *action;
1914             const struct ofpact *processed = a;
1915
1916             instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1917             OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1918                 if (ofpact_is_instruction(action)) {
1919                     break;
1920                 }
1921                 ofpact_to_openflow11(action, openflow);
1922                 processed = action;
1923             }
1924             ofpacts_update_instruction_actions(openflow, ofs);
1925             a = processed;
1926         }
1927     }
1928 }
1929 \f
1930 /* Returns true if 'action' outputs to 'port', false otherwise. */
1931 static bool
1932 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1933 {
1934     switch (ofpact->type) {
1935     case OFPACT_OUTPUT:
1936         return ofpact_get_OUTPUT(ofpact)->port == port;
1937     case OFPACT_ENQUEUE:
1938         return ofpact_get_ENQUEUE(ofpact)->port == port;
1939     case OFPACT_CONTROLLER:
1940         return port == OFPP_CONTROLLER;
1941
1942     case OFPACT_OUTPUT_REG:
1943     case OFPACT_BUNDLE:
1944     case OFPACT_SET_VLAN_VID:
1945     case OFPACT_SET_VLAN_PCP:
1946     case OFPACT_STRIP_VLAN:
1947     case OFPACT_PUSH_VLAN:
1948     case OFPACT_SET_ETH_SRC:
1949     case OFPACT_SET_ETH_DST:
1950     case OFPACT_SET_IPV4_SRC:
1951     case OFPACT_SET_IPV4_DST:
1952     case OFPACT_SET_IPV4_DSCP:
1953     case OFPACT_SET_L4_SRC_PORT:
1954     case OFPACT_SET_L4_DST_PORT:
1955     case OFPACT_REG_MOVE:
1956     case OFPACT_REG_LOAD:
1957     case OFPACT_STACK_PUSH:
1958     case OFPACT_STACK_POP:
1959     case OFPACT_DEC_TTL:
1960     case OFPACT_SET_MPLS_TTL:
1961     case OFPACT_DEC_MPLS_TTL:
1962     case OFPACT_SET_TUNNEL:
1963     case OFPACT_WRITE_METADATA:
1964     case OFPACT_SET_QUEUE:
1965     case OFPACT_POP_QUEUE:
1966     case OFPACT_FIN_TIMEOUT:
1967     case OFPACT_RESUBMIT:
1968     case OFPACT_LEARN:
1969     case OFPACT_MULTIPATH:
1970     case OFPACT_NOTE:
1971     case OFPACT_EXIT:
1972     case OFPACT_PUSH_MPLS:
1973     case OFPACT_POP_MPLS:
1974     case OFPACT_SAMPLE:
1975     case OFPACT_CLEAR_ACTIONS:
1976     case OFPACT_GOTO_TABLE:
1977     default:
1978         return false;
1979     }
1980 }
1981
1982 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1983  * to 'port', false otherwise. */
1984 bool
1985 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1986                        uint16_t port)
1987 {
1988     const struct ofpact *a;
1989
1990     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1991         if (ofpact_outputs_to_port(a, port)) {
1992             return true;
1993         }
1994     }
1995
1996     return false;
1997 }
1998
1999 bool
2000 ofpacts_equal(const struct ofpact *a, size_t a_len,
2001               const struct ofpact *b, size_t b_len)
2002 {
2003     return a_len == b_len && !memcmp(a, b, a_len);
2004 }
2005 \f
2006 /* Formatting ofpacts. */
2007
2008 static void
2009 print_note(const struct ofpact_note *note, struct ds *string)
2010 {
2011     size_t i;
2012
2013     ds_put_cstr(string, "note:");
2014     for (i = 0; i < note->length; i++) {
2015         if (i) {
2016             ds_put_char(string, '.');
2017         }
2018         ds_put_format(string, "%02"PRIx8, note->data[i]);
2019     }
2020 }
2021
2022 static void
2023 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2024               struct ds *s)
2025 {
2026     size_t i;
2027
2028     ds_put_cstr(s, "dec_ttl");
2029     if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2030         ds_put_cstr(s, "(");
2031         for (i = 0; i < ids->n_controllers; i++) {
2032             if (i) {
2033                 ds_put_cstr(s, ",");
2034             }
2035             ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2036         }
2037         ds_put_cstr(s, ")");
2038     }
2039 }
2040
2041 static void
2042 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2043                   struct ds *s)
2044 {
2045     ds_put_cstr(s, "fin_timeout(");
2046     if (fin_timeout->fin_idle_timeout) {
2047         ds_put_format(s, "idle_timeout=%"PRIu16",",
2048                       fin_timeout->fin_idle_timeout);
2049     }
2050     if (fin_timeout->fin_hard_timeout) {
2051         ds_put_format(s, "hard_timeout=%"PRIu16",",
2052                       fin_timeout->fin_hard_timeout);
2053     }
2054     ds_chomp(s, ',');
2055     ds_put_char(s, ')');
2056 }
2057
2058 static void
2059 ofpact_format(const struct ofpact *a, struct ds *s)
2060 {
2061     const struct ofpact_enqueue *enqueue;
2062     const struct ofpact_resubmit *resubmit;
2063     const struct ofpact_controller *controller;
2064     const struct ofpact_metadata *metadata;
2065     const struct ofpact_tunnel *tunnel;
2066     const struct ofpact_sample *sample;
2067     uint16_t port;
2068
2069     switch (a->type) {
2070     case OFPACT_OUTPUT:
2071         port = ofpact_get_OUTPUT(a)->port;
2072         if (port < OFPP_MAX) {
2073             ds_put_format(s, "output:%"PRIu16, port);
2074         } else {
2075             ofputil_format_port(port, s);
2076             if (port == OFPP_CONTROLLER) {
2077                 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2078             }
2079         }
2080         break;
2081
2082     case OFPACT_CONTROLLER:
2083         controller = ofpact_get_CONTROLLER(a);
2084         if (controller->reason == OFPR_ACTION &&
2085             controller->controller_id == 0) {
2086             ds_put_format(s, "CONTROLLER:%"PRIu16,
2087                           ofpact_get_CONTROLLER(a)->max_len);
2088         } else {
2089             enum ofp_packet_in_reason reason = controller->reason;
2090
2091             ds_put_cstr(s, "controller(");
2092             if (reason != OFPR_ACTION) {
2093                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2094
2095                 ds_put_format(s, "reason=%s,",
2096                               ofputil_packet_in_reason_to_string(
2097                                   reason, reasonbuf, sizeof reasonbuf));
2098             }
2099             if (controller->max_len != UINT16_MAX) {
2100                 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2101             }
2102             if (controller->controller_id != 0) {
2103                 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2104             }
2105             ds_chomp(s, ',');
2106             ds_put_char(s, ')');
2107         }
2108         break;
2109
2110     case OFPACT_ENQUEUE:
2111         enqueue = ofpact_get_ENQUEUE(a);
2112         ds_put_format(s, "enqueue:");
2113         ofputil_format_port(enqueue->port, s);
2114         ds_put_format(s, "q%"PRIu32, enqueue->queue);
2115         break;
2116
2117     case OFPACT_OUTPUT_REG:
2118         ds_put_cstr(s, "output:");
2119         mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2120         break;
2121
2122     case OFPACT_BUNDLE:
2123         bundle_format(ofpact_get_BUNDLE(a), s);
2124         break;
2125
2126     case OFPACT_SET_VLAN_VID:
2127         ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2128                       ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2129         break;
2130
2131     case OFPACT_SET_VLAN_PCP:
2132         ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2133                       ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2134         break;
2135
2136     case OFPACT_STRIP_VLAN:
2137         ds_put_cstr(s, "strip_vlan");
2138         break;
2139
2140     case OFPACT_PUSH_VLAN:
2141         /* XXX 802.1AD case*/
2142         ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2143         break;
2144
2145     case OFPACT_SET_ETH_SRC:
2146         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2147                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2148         break;
2149
2150     case OFPACT_SET_ETH_DST:
2151         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2152                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2153         break;
2154
2155     case OFPACT_SET_IPV4_SRC:
2156         ds_put_format(s, "mod_nw_src:"IP_FMT,
2157                       IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2158         break;
2159
2160     case OFPACT_SET_IPV4_DST:
2161         ds_put_format(s, "mod_nw_dst:"IP_FMT,
2162                       IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2163         break;
2164
2165     case OFPACT_SET_IPV4_DSCP:
2166         ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2167         break;
2168
2169     case OFPACT_SET_L4_SRC_PORT:
2170         ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2171         break;
2172
2173     case OFPACT_SET_L4_DST_PORT:
2174         ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2175         break;
2176
2177     case OFPACT_REG_MOVE:
2178         nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2179         break;
2180
2181     case OFPACT_REG_LOAD:
2182         nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2183         break;
2184
2185     case OFPACT_STACK_PUSH:
2186         nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2187         break;
2188
2189     case OFPACT_STACK_POP:
2190         nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2191         break;
2192
2193     case OFPACT_DEC_TTL:
2194         print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2195         break;
2196
2197     case OFPACT_SET_MPLS_TTL:
2198         ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2199                       ofpact_get_SET_MPLS_TTL(a)->ttl);
2200         break;
2201
2202     case OFPACT_DEC_MPLS_TTL:
2203         ds_put_cstr(s, "dec_mpls_ttl");
2204         break;
2205
2206     case OFPACT_SET_TUNNEL:
2207         tunnel = ofpact_get_SET_TUNNEL(a);
2208         ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2209                       (tunnel->tun_id > UINT32_MAX
2210                        || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2211                       tunnel->tun_id);
2212         break;
2213
2214     case OFPACT_SET_QUEUE:
2215         ds_put_format(s, "set_queue:%"PRIu32,
2216                       ofpact_get_SET_QUEUE(a)->queue_id);
2217         break;
2218
2219     case OFPACT_POP_QUEUE:
2220         ds_put_cstr(s, "pop_queue");
2221         break;
2222
2223     case OFPACT_FIN_TIMEOUT:
2224         print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2225         break;
2226
2227     case OFPACT_RESUBMIT:
2228         resubmit = ofpact_get_RESUBMIT(a);
2229         if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2230             ds_put_cstr(s, "resubmit:");
2231             ofputil_format_port(resubmit->in_port, s);
2232         } else {
2233             ds_put_format(s, "resubmit(");
2234             if (resubmit->in_port != OFPP_IN_PORT) {
2235                 ofputil_format_port(resubmit->in_port, s);
2236             }
2237             ds_put_char(s, ',');
2238             if (resubmit->table_id != 255) {
2239                 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2240             }
2241             ds_put_char(s, ')');
2242         }
2243         break;
2244
2245     case OFPACT_LEARN:
2246         learn_format(ofpact_get_LEARN(a), s);
2247         break;
2248
2249     case OFPACT_MULTIPATH:
2250         multipath_format(ofpact_get_MULTIPATH(a), s);
2251         break;
2252
2253     case OFPACT_NOTE:
2254         print_note(ofpact_get_NOTE(a), s);
2255         break;
2256
2257     case OFPACT_PUSH_MPLS:
2258         ds_put_format(s, "push_mpls:0x%04"PRIx16,
2259                       ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2260         break;
2261
2262     case OFPACT_POP_MPLS:
2263         ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2264                       ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2265         break;
2266
2267     case OFPACT_EXIT:
2268         ds_put_cstr(s, "exit");
2269         break;
2270
2271     case OFPACT_SAMPLE:
2272         sample = ofpact_get_SAMPLE(a);
2273         ds_put_format(
2274             s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2275             ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2276             sample->probability, sample->collector_set_id,
2277             sample->obs_domain_id, sample->obs_point_id);
2278         break;
2279
2280     case OFPACT_CLEAR_ACTIONS:
2281         ds_put_format(s, "%s",
2282                       ofpact_instruction_name_from_type(
2283                           OVSINST_OFPIT11_CLEAR_ACTIONS));
2284         break;
2285
2286     case OFPACT_WRITE_METADATA:
2287         metadata = ofpact_get_WRITE_METADATA(a);
2288         ds_put_format(s, "%s:%#"PRIx64,
2289                       ofpact_instruction_name_from_type(
2290                           OVSINST_OFPIT11_WRITE_METADATA),
2291                       ntohll(metadata->metadata));
2292         if (metadata->mask != htonll(UINT64_MAX)) {
2293             ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2294         }
2295         break;
2296
2297     case OFPACT_GOTO_TABLE:
2298         ds_put_format(s, "%s:%"PRIu8,
2299                       ofpact_instruction_name_from_type(
2300                           OVSINST_OFPIT11_GOTO_TABLE),
2301                       ofpact_get_GOTO_TABLE(a)->table_id);
2302         break;
2303     }
2304 }
2305
2306 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2307  * 'ofpacts' to 'string'. */
2308 void
2309 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2310                struct ds *string)
2311 {
2312     ds_put_cstr(string, "actions=");
2313     if (!ofpacts_len) {
2314         ds_put_cstr(string, "drop");
2315     } else {
2316         const struct ofpact *a;
2317
2318         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2319             if (a != ofpacts) {
2320                 ds_put_cstr(string, ",");
2321             }
2322
2323             /* XXX write-actions */
2324             ofpact_format(a, string);
2325         }
2326     }
2327 }
2328 \f
2329 /* Internal use by helpers. */
2330
2331 void *
2332 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2333 {
2334     struct ofpact *ofpact;
2335
2336     ofpact_pad(ofpacts);
2337     ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2338     ofpact_init(ofpact, type, len);
2339     return ofpact;
2340 }
2341
2342 void
2343 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2344 {
2345     memset(ofpact, 0, len);
2346     ofpact->type = type;
2347     ofpact->compat = OFPUTIL_ACTION_INVALID;
2348     ofpact->len = len;
2349 }
2350 \f
2351 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2352  * starting at 'ofpact'.
2353  *
2354  * This is the correct way to update a variable-length ofpact's length after
2355  * adding the variable-length part of the payload.  (See the large comment
2356  * near the end of ofp-actions.h for more information.) */
2357 void
2358 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2359 {
2360     ovs_assert(ofpact == ofpacts->l2);
2361     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2362 }
2363
2364 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
2365  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2366  * client must call this itself after adding the final ofpact to an array of
2367  * them.
2368  *
2369  * (The consequences of failing to call this function are probably not dire.
2370  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2371  * not dereference it.  That's undefined behavior, technically, but it will not
2372  * cause a real problem on common systems.  Still, it seems better to call
2373  * it.) */
2374 void
2375 ofpact_pad(struct ofpbuf *ofpacts)
2376 {
2377     unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2378     if (rem) {
2379         ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2380     }
2381 }
2382
2383 void
2384 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2385                       const void *src)
2386 {
2387     load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2388     load->dst.field = mf;
2389     load->dst.ofs = 0;
2390     load->dst.n_bits = mf->n_bits;
2391     bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2392                  &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);
2393 }