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