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