ofp-actions: Prepare to treat OF1.2 actions as OF1.1 actions.
[sliver-openvswitch.git] / lib / ofp-actions.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 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 "autopath.h"
20 #include "bundle.h"
21 #include "byte-order.h"
22 #include "compiler.h"
23 #include "dynamic-string.h"
24 #include "learn.h"
25 #include "meta-flow.h"
26 #include "multipath.h"
27 #include "nx-match.h"
28 #include "ofp-util.h"
29 #include "ofpbuf.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 ofp_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 void
139 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
140 {
141     struct ofpact_note *note;
142     unsigned int length;
143
144     length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
145     note = ofpact_put(out, OFPACT_NOTE,
146                       offsetof(struct ofpact_note, data) + length);
147     note->length = length;
148     memcpy(note->data, nan->note, length);
149 }
150
151 static enum ofperr
152 dec_ttl_from_openflow(struct ofpbuf *out)
153 {
154     uint16_t id = 0;
155     struct ofpact_cnt_ids *ids;
156     enum ofperr error = 0;
157
158     ids = ofpact_put_DEC_TTL(out);
159     ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL;
160     ids->n_controllers = 1;
161     ofpbuf_put(out, &id, sizeof id);
162     ids = out->l2;
163     ofpact_update_len(out, &ids->ofpact);
164     return error;
165 }
166
167 static enum ofperr
168 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
169                       struct ofpbuf *out)
170 {
171     struct ofpact_cnt_ids *ids;
172     size_t ids_size;
173     int i;
174
175     ids = ofpact_put_DEC_TTL(out);
176     ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
177     ids->n_controllers = ntohs(nac_ids->n_controllers);
178     ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
179
180     if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
181         return OFPERR_NXBRC_MUST_BE_ZERO;
182     }
183
184     if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
185         VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
186                      "allocated for controller ids.  %zu bytes are required for "
187                      "%"PRIu16" controllers.", ids_size,
188                      ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
189         return OFPERR_OFPBAC_BAD_LEN;
190     }
191
192     for (i = 0; i < ids->n_controllers; i++) {
193         uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
194         ofpbuf_put(out, &id, sizeof id);
195     }
196
197     ids = out->l2;
198     ofpact_update_len(out, &ids->ofpact);
199
200     return 0;
201 }
202
203 static enum ofperr
204 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
205 {
206     const struct nx_action_header *nah = (const struct nx_action_header *) a;
207     uint16_t len = ntohs(a->header.len);
208
209     if (len < sizeof(struct nx_action_header)) {
210         return OFPERR_OFPBAC_BAD_LEN;
211     } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
212         return OFPERR_OFPBAC_BAD_VENDOR;
213     }
214
215     switch (nah->subtype) {
216 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
217         case CONSTANT_HTONS(ENUM):                      \
218             if (EXTENSIBLE                              \
219                 ? len >= sizeof(struct STRUCT)          \
220                 : len == sizeof(struct STRUCT)) {       \
221                 *code = OFPUTIL_##ENUM;                 \
222                 return 0;                               \
223             } else {                                    \
224                 return OFPERR_OFPBAC_BAD_LEN;           \
225             }                                           \
226             NOT_REACHED();
227 #include "ofp-util.def"
228
229     case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
230     case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
231     default:
232         return OFPERR_OFPBAC_BAD_TYPE;
233     }
234 }
235
236 /* Parses 'a' to determine its type.  On success stores the correct type into
237  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
238  * '*code' is indeterminate.
239  *
240  * The caller must have already verified that 'a''s length is potentially
241  * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
242  * ofp_action) and no longer than the amount of space allocated to 'a').
243  *
244  * This function verifies that 'a''s length is correct for the type of action
245  * that it represents. */
246 static enum ofperr
247 decode_openflow10_action(const union ofp_action *a,
248                          enum ofputil_action_code *code)
249 {
250     switch (a->type) {
251     case CONSTANT_HTONS(OFPAT10_VENDOR):
252         return decode_nxast_action(a, code);
253
254 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)                          \
255         case CONSTANT_HTONS(ENUM):                                  \
256             if (a->header.len == htons(sizeof(struct STRUCT))) {    \
257                 *code = OFPUTIL_##ENUM;                             \
258                 return 0;                                           \
259             } else {                                                \
260                 return OFPERR_OFPBAC_BAD_LEN;                       \
261             }                                                       \
262             break;
263 #include "ofp-util.def"
264
265     default:
266         return OFPERR_OFPBAC_BAD_TYPE;
267     }
268 }
269
270 static enum ofperr
271 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
272                   struct ofpbuf *out)
273 {
274     const struct nx_action_resubmit *nar;
275     const struct nx_action_set_tunnel *nast;
276     const struct nx_action_set_queue *nasq;
277     const struct nx_action_note *nan;
278     const struct nx_action_set_tunnel64 *nast64;
279     struct ofpact_tunnel *tunnel;
280     enum ofperr error = 0;
281
282     switch (code) {
283     case OFPUTIL_ACTION_INVALID:
284 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
285 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
286 #include "ofp-util.def"
287         NOT_REACHED();
288
289     case OFPUTIL_NXAST_RESUBMIT:
290         resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
291         break;
292
293     case OFPUTIL_NXAST_SET_TUNNEL:
294         nast = (const struct nx_action_set_tunnel *) a;
295         tunnel = ofpact_put_SET_TUNNEL(out);
296         tunnel->ofpact.compat = code;
297         tunnel->tun_id = ntohl(nast->tun_id);
298         break;
299
300     case OFPUTIL_NXAST_SET_QUEUE:
301         nasq = (const struct nx_action_set_queue *) a;
302         ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
303         break;
304
305     case OFPUTIL_NXAST_POP_QUEUE:
306         ofpact_put_POP_QUEUE(out);
307         break;
308
309     case OFPUTIL_NXAST_REG_MOVE:
310         error = nxm_reg_move_from_openflow(
311             (const struct nx_action_reg_move *) a, out);
312         break;
313
314     case OFPUTIL_NXAST_REG_LOAD:
315         error = nxm_reg_load_from_openflow(
316             (const struct nx_action_reg_load *) a, out);
317         break;
318
319     case OFPUTIL_NXAST_NOTE:
320         nan = (const struct nx_action_note *) a;
321         note_from_openflow(nan, out);
322         break;
323
324     case OFPUTIL_NXAST_SET_TUNNEL64:
325         nast64 = (const struct nx_action_set_tunnel64 *) a;
326         tunnel = ofpact_put_SET_TUNNEL(out);
327         tunnel->ofpact.compat = code;
328         tunnel->tun_id = ntohll(nast64->tun_id);
329         break;
330
331     case OFPUTIL_NXAST_MULTIPATH:
332         error = multipath_from_openflow((const struct nx_action_multipath *) a,
333                                         ofpact_put_MULTIPATH(out));
334         break;
335
336     case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
337         error = autopath_from_openflow((const struct nx_action_autopath *) a,
338                                        ofpact_put_AUTOPATH(out));
339         break;
340
341     case OFPUTIL_NXAST_BUNDLE:
342     case OFPUTIL_NXAST_BUNDLE_LOAD:
343         error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
344         break;
345
346     case OFPUTIL_NXAST_OUTPUT_REG:
347         error = output_reg_from_openflow(
348             (const struct nx_action_output_reg *) a, out);
349         break;
350
351     case OFPUTIL_NXAST_RESUBMIT_TABLE:
352         nar = (const struct nx_action_resubmit *) a;
353         error = resubmit_table_from_openflow(nar, out);
354         break;
355
356     case OFPUTIL_NXAST_LEARN:
357         error = learn_from_openflow((const struct nx_action_learn *) a, out);
358         break;
359
360     case OFPUTIL_NXAST_EXIT:
361         ofpact_put_EXIT(out);
362         break;
363
364     case OFPUTIL_NXAST_DEC_TTL:
365         error = dec_ttl_from_openflow(out);
366         break;
367
368     case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
369         error = dec_ttl_cnt_ids_from_openflow(
370                     (const struct nx_action_cnt_ids *) a, out);
371         break;
372
373     case OFPUTIL_NXAST_FIN_TIMEOUT:
374         fin_timeout_from_openflow(
375             (const struct nx_action_fin_timeout *) a, out);
376         break;
377
378     case OFPUTIL_NXAST_CONTROLLER:
379         controller_from_openflow((const struct nx_action_controller *) a, out);
380         break;
381     }
382
383     return error;
384 }
385
386 static enum ofperr
387 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
388 {
389     enum ofputil_action_code code;
390     enum ofperr error;
391
392     error = decode_openflow10_action(a, &code);
393     if (error) {
394         return error;
395     }
396
397     switch (code) {
398     case OFPUTIL_ACTION_INVALID:
399 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
400 #include "ofp-util.def"
401         NOT_REACHED();
402
403     case OFPUTIL_OFPAT10_OUTPUT:
404         return output_from_openflow10(&a->output10, out);
405
406     case OFPUTIL_OFPAT10_SET_VLAN_VID:
407         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
408             return OFPERR_OFPBAC_BAD_ARGUMENT;
409         }
410         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
411         break;
412
413     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
414         if (a->vlan_pcp.vlan_pcp & ~7) {
415             return OFPERR_OFPBAC_BAD_ARGUMENT;
416         }
417         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
418         break;
419
420     case OFPUTIL_OFPAT10_STRIP_VLAN:
421         ofpact_put_STRIP_VLAN(out);
422         break;
423
424     case OFPUTIL_OFPAT10_SET_DL_SRC:
425         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
426                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
427         break;
428
429     case OFPUTIL_OFPAT10_SET_DL_DST:
430         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
431                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
432         break;
433
434     case OFPUTIL_OFPAT10_SET_NW_SRC:
435         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
436         break;
437
438     case OFPUTIL_OFPAT10_SET_NW_DST:
439         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
440         break;
441
442     case OFPUTIL_OFPAT10_SET_NW_TOS:
443         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
444             return OFPERR_OFPBAC_BAD_ARGUMENT;
445         }
446         ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
447         break;
448
449     case OFPUTIL_OFPAT10_SET_TP_SRC:
450         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
451         break;
452
453     case OFPUTIL_OFPAT10_SET_TP_DST:
454         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
455
456         break;
457
458     case OFPUTIL_OFPAT10_ENQUEUE:
459         error = enqueue_from_openflow10((const struct ofp_action_enqueue *) a,
460                                         out);
461         break;
462
463 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
464 #include "ofp-util.def"
465         return ofpact_from_nxast(a, code, out);
466     }
467
468     return error;
469 }
470
471 static inline union ofp_action *
472 action_next(const union ofp_action *a)
473 {
474     return ((union ofp_action *) (void *)
475             ((uint8_t *) a + ntohs(a->header.len)));
476 }
477
478 static inline bool
479 action_is_valid(const union ofp_action *a, size_t n_actions)
480 {
481     uint16_t len = ntohs(a->header.len);
482     return (!(len % OFP_ACTION_ALIGN)
483             && len >= sizeof *a
484             && len / sizeof *a <= n_actions);
485 }
486
487 /* This macro is careful to check for actions with bad lengths. */
488 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS)                 \
489     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
490          (LEFT) > 0 && action_is_valid(ITER, LEFT);                     \
491          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
492           (ITER) = action_next(ITER)))
493
494 static void
495 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
496                enum ofperr error)
497 {
498     if (!VLOG_DROP_WARN(&rl)) {
499         struct ds s;
500
501         ds_init(&s);
502         ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
503         VLOG_WARN("bad action at offset %#zx (%s):\n%s",
504                   ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
505         ds_destroy(&s);
506     }
507 }
508
509 static enum ofperr
510 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
511                       struct ofpbuf *out,
512                       enum ofperr (*ofpact_from_openflow)(
513                           const union ofp_action *a, struct ofpbuf *out))
514 {
515     const union ofp_action *a;
516     size_t left;
517
518     ACTION_FOR_EACH (a, left, in, n_in) {
519         enum ofperr error = ofpact_from_openflow(a, out);
520         if (error) {
521             log_bad_action(in, n_in, a - in, error);
522             return error;
523         }
524     }
525     if (left) {
526         enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
527         log_bad_action(in, n_in, n_in - left, error);
528         return error;
529     }
530
531     ofpact_pad(out);
532     return 0;
533 }
534
535 static enum ofperr
536 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
537                         struct ofpbuf *out)
538 {
539     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
540 }
541
542 static enum ofperr
543 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
544                      struct ofpbuf *ofpacts,
545                      enum ofperr (*translate)(const union ofp_action *actions,
546                                               size_t n_actions,
547                                               struct ofpbuf *ofpacts))
548 {
549     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
550     const union ofp_action *actions;
551     enum ofperr error;
552
553     ofpbuf_clear(ofpacts);
554
555     if (actions_len % OFP_ACTION_ALIGN != 0) {
556         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
557                      "multiple of %d", actions_len, OFP_ACTION_ALIGN);
558         return OFPERR_OFPBRC_BAD_LEN;
559     }
560
561     actions = ofpbuf_try_pull(openflow, actions_len);
562     if (actions == NULL) {
563         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
564                      "remaining message length (%zu)",
565                      actions_len, openflow->size);
566         return OFPERR_OFPBRC_BAD_LEN;
567     }
568
569     error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
570     if (error) {
571         ofpbuf_clear(ofpacts);
572     }
573     return error;
574 }
575
576 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
577  * front of 'openflow' into ofpacts.  On success, replaces any existing content
578  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
579  * Returns 0 if successful, otherwise an OpenFlow error.
580  *
581  * The parsed actions are valid generically, but they may not be valid in a
582  * specific context.  For example, port numbers up to OFPP_MAX are valid
583  * generically, but specific datapaths may only support port numbers in a
584  * smaller range.  Use ofpacts_check() to additional check whether actions are
585  * valid in a specific context. */
586 enum ofperr
587 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
588                         struct ofpbuf *ofpacts)
589 {
590     return ofpacts_pull_actions(openflow, actions_len, ofpacts,
591                                 ofpacts_from_openflow10);
592 }
593 \f
594 /* OpenFlow 1.1 actions. */
595
596 /* Parses 'a' to determine its type.  On success stores the correct type into
597  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
598  * '*code' is indeterminate.
599  *
600  * The caller must have already verified that 'a''s length is potentially
601  * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
602  * ofp_action) and no longer than the amount of space allocated to 'a').
603  *
604  * This function verifies that 'a''s length is correct for the type of action
605  * that it represents. */
606 static enum ofperr
607 decode_openflow11_action(const union ofp_action *a,
608                          enum ofputil_action_code *code)
609 {
610     switch (a->type) {
611     case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
612         return decode_nxast_action(a, code);
613
614 #define OFPAT11_ACTION(ENUM, STRUCT, NAME)                          \
615         case CONSTANT_HTONS(ENUM):                                  \
616             if (a->header.len == htons(sizeof(struct STRUCT))) {    \
617                 *code = OFPUTIL_##ENUM;                             \
618                 return 0;                                           \
619             } else {                                                \
620                 return OFPERR_OFPBAC_BAD_LEN;                       \
621             }                                                       \
622             break;
623 #include "ofp-util.def"
624
625     default:
626         return OFPERR_OFPBAC_BAD_TYPE;
627     }
628 }
629
630 static enum ofperr
631 output_from_openflow11(const struct ofp11_action_output *oao,
632                        struct ofpbuf *out)
633 {
634     struct ofpact_output *output;
635     enum ofperr error;
636
637     output = ofpact_put_OUTPUT(out);
638     output->max_len = ntohs(oao->max_len);
639
640     error = ofputil_port_from_ofp11(oao->port, &output->port);
641     if (error) {
642         return error;
643     }
644
645     return ofputil_check_output_port(output->port, OFPP_MAX);
646 }
647
648 static enum ofperr
649 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
650 {
651     enum ofputil_action_code code;
652     enum ofperr error;
653
654     error = decode_openflow11_action(a, &code);
655     if (error) {
656         return error;
657     }
658
659     switch (code) {
660     case OFPUTIL_ACTION_INVALID:
661 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
662 #include "ofp-util.def"
663         NOT_REACHED();
664
665     case OFPUTIL_OFPAT11_OUTPUT:
666         return output_from_openflow11((const struct ofp11_action_output *) a,
667                                       out);
668
669     case OFPUTIL_OFPAT11_SET_VLAN_VID:
670         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
671             return OFPERR_OFPBAC_BAD_ARGUMENT;
672         }
673         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
674         break;
675
676     case OFPUTIL_OFPAT11_SET_VLAN_PCP:
677         if (a->vlan_pcp.vlan_pcp & ~7) {
678             return OFPERR_OFPBAC_BAD_ARGUMENT;
679         }
680         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
681         break;
682
683     case OFPUTIL_OFPAT11_SET_DL_SRC:
684         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
685                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
686         break;
687
688     case OFPUTIL_OFPAT11_SET_DL_DST:
689         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
690                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
691         break;
692
693     case OFPUTIL_OFPAT11_SET_NW_SRC:
694         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
695         break;
696
697     case OFPUTIL_OFPAT11_SET_NW_DST:
698         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
699         break;
700
701     case OFPUTIL_OFPAT11_SET_NW_TOS:
702         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
703             return OFPERR_OFPBAC_BAD_ARGUMENT;
704         }
705         ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
706         break;
707
708     case OFPUTIL_OFPAT11_SET_TP_SRC:
709         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
710         break;
711
712     case OFPUTIL_OFPAT11_SET_TP_DST:
713         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
714         break;
715
716     case OFPUTIL_OFPAT12_SET_FIELD:
717         /* Not yet implemented. */
718         break;
719
720 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
721 #include "ofp-util.def"
722         return ofpact_from_nxast(a, code, out);
723     }
724
725     return error;
726 }
727
728 static enum ofperr
729 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
730                         struct ofpbuf *out)
731 {
732     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
733 }
734 \f
735 /* OpenFlow 1.1 instructions. */
736
737 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
738     static inline const struct STRUCT *                         \
739     instruction_get_##ENUM(const struct ofp11_instruction *inst)\
740     {                                                           \
741         assert(inst->type == htons(ENUM));                      \
742         return (struct STRUCT *)inst;                           \
743     }                                                           \
744                                                                 \
745     static inline void                                          \
746     instruction_init_##ENUM(struct STRUCT *s)                   \
747     {                                                           \
748         memset(s, 0, sizeof *s);                                \
749         s->type = htons(ENUM);                                  \
750         s->len = htons(sizeof *s);                              \
751     }                                                           \
752                                                                 \
753     static inline struct STRUCT *                               \
754     instruction_put_##ENUM(struct ofpbuf *buf)                  \
755     {                                                           \
756         struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
757         instruction_init_##ENUM(s);                             \
758         return s;                                               \
759     }
760 OVS_INSTRUCTIONS
761 #undef DEFINE_INST
762
763 struct instruction_type_info {
764     enum ovs_instruction_type type;
765     const char *name;
766 };
767
768 static const struct instruction_type_info inst_info[] = {
769 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
770 OVS_INSTRUCTIONS
771 #undef DEFINE_INST
772 };
773
774 const char *
775 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
776 {
777     return inst_info[type].name;
778 }
779
780 int
781 ofpact_instruction_type_from_name(const char *name)
782 {
783     const struct instruction_type_info *p;
784     for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
785         if (!strcasecmp(name, p->name)) {
786             return p->type;
787         }
788     }
789     return -1;
790 }
791
792 static inline struct ofp11_instruction *
793 instruction_next(const struct ofp11_instruction *inst)
794 {
795     return ((struct ofp11_instruction *) (void *)
796             ((uint8_t *) inst + ntohs(inst->len)));
797 }
798
799 static inline bool
800 instruction_is_valid(const struct ofp11_instruction *inst,
801                      size_t n_instructions)
802 {
803     uint16_t len = ntohs(inst->len);
804     return (!(len % OFP11_INSTRUCTION_ALIGN)
805             && len >= sizeof *inst
806             && len / sizeof *inst <= n_instructions);
807 }
808
809 /* This macro is careful to check for instructions with bad lengths. */
810 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS)  \
811     for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS);            \
812          (LEFT) > 0 && instruction_is_valid(ITER, LEFT);                \
813          ((LEFT) -= (ntohs((ITER)->len)                                 \
814                      / sizeof(struct ofp11_instruction)),               \
815           (ITER) = instruction_next(ITER)))
816
817 static enum ofperr
818 decode_openflow11_instruction(const struct ofp11_instruction *inst,
819                               enum ovs_instruction_type *type)
820 {
821     uint16_t len = ntohs(inst->len);
822
823     switch (inst->type) {
824     case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
825         return OFPERR_OFPBIC_BAD_EXPERIMENTER;
826
827 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)     \
828         case CONSTANT_HTONS(ENUM):                      \
829             if (EXTENSIBLE                              \
830                 ? len >= sizeof(struct STRUCT)          \
831                 : len == sizeof(struct STRUCT)) {       \
832                 *type = OVSINST_##ENUM;                 \
833                 return 0;                               \
834             } else {                                    \
835                 return OFPERR_OFPBIC_BAD_LEN;           \
836             }
837 OVS_INSTRUCTIONS
838 #undef DEFINE_INST
839
840     default:
841         return OFPERR_OFPBIC_UNKNOWN_INST;
842     }
843 }
844
845 static enum ofperr
846 decode_openflow11_instructions(const struct ofp11_instruction insts[],
847                                size_t n_insts,
848                                const struct ofp11_instruction *out[])
849 {
850     const struct ofp11_instruction *inst;
851     size_t left;
852
853     memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
854     INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
855         enum ovs_instruction_type type;
856         enum ofperr error;
857
858         error = decode_openflow11_instruction(inst, &type);
859         if (error) {
860             return error;
861         }
862
863         if (out[type]) {
864             return OFPERR_OFPIT_BAD_INSTRUCTION;
865         }
866         out[type] = inst;
867     }
868
869     if (left) {
870         VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
871                      (n_insts - left) * sizeof *inst);
872         return OFPERR_OFPBIC_BAD_LEN;
873     }
874     return 0;
875 }
876
877 static void
878 get_actions_from_instruction(const struct ofp11_instruction *inst,
879                          const union ofp_action **actions,
880                          size_t *n_actions)
881 {
882     *actions = (const union ofp_action *) (inst + 1);
883     *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
884 }
885
886 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
887  * front of 'openflow' into ofpacts.  On success, replaces any existing content
888  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
889  * Returns 0 if successful, otherwise an OpenFlow error.
890  *
891  * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
892  * instructions, so you should call ofpacts_pull_openflow11_instructions()
893  * instead of this function.
894  *
895  * The parsed actions are valid generically, but they may not be valid in a
896  * specific context.  For example, port numbers up to OFPP_MAX are valid
897  * generically, but specific datapaths may only support port numbers in a
898  * smaller range.  Use ofpacts_check() to additional check whether actions are
899  * valid in a specific context. */
900 enum ofperr
901 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
902                                 unsigned int actions_len,
903                                 struct ofpbuf *ofpacts)
904 {
905     return ofpacts_pull_actions(openflow, actions_len, ofpacts,
906                                 ofpacts_from_openflow11);
907 }
908
909 enum ofperr
910 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
911                                      unsigned int instructions_len,
912                                      struct ofpbuf *ofpacts)
913 {
914     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
915     const struct ofp11_instruction *instructions;
916     const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
917     enum ofperr error;
918
919     ofpbuf_clear(ofpacts);
920
921     if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
922         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
923                      "multiple of %d",
924                      instructions_len, OFP11_INSTRUCTION_ALIGN);
925         error = OFPERR_OFPBIC_BAD_LEN;
926         goto exit;
927     }
928
929     instructions = ofpbuf_try_pull(openflow, instructions_len);
930     if (instructions == NULL) {
931         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
932                      "remaining message length (%zu)",
933                      instructions_len, openflow->size);
934         error = OFPERR_OFPBIC_BAD_LEN;
935         goto exit;
936     }
937
938     error = decode_openflow11_instructions(
939         instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
940         insts);
941     if (error) {
942         goto exit;
943     }
944
945     if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
946         const union ofp_action *actions;
947         size_t n_actions;
948
949         get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
950                                      &actions, &n_actions);
951         error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
952         if (error) {
953             goto exit;
954         }
955     }
956
957     if (insts[OVSINST_OFPIT11_GOTO_TABLE] ||
958         insts[OVSINST_OFPIT11_WRITE_METADATA] ||
959         insts[OVSINST_OFPIT11_WRITE_ACTIONS] ||
960         insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
961         error = OFPERR_OFPBIC_UNSUP_INST;
962         goto exit;
963     }
964
965 exit:
966     if (error) {
967         ofpbuf_clear(ofpacts);
968     }
969     return error;
970 }
971 \f
972 static enum ofperr
973 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
974 {
975     const struct ofpact_enqueue *enqueue;
976
977     switch (a->type) {
978     case OFPACT_OUTPUT:
979         return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
980                                          max_ports);
981
982     case OFPACT_CONTROLLER:
983         return 0;
984
985     case OFPACT_ENQUEUE:
986         enqueue = ofpact_get_ENQUEUE(a);
987         if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
988             && enqueue->port != OFPP_LOCAL) {
989             return OFPERR_OFPBAC_BAD_OUT_PORT;
990         }
991         return 0;
992
993     case OFPACT_OUTPUT_REG:
994         return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
995
996     case OFPACT_BUNDLE:
997         return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
998
999     case OFPACT_SET_VLAN_VID:
1000     case OFPACT_SET_VLAN_PCP:
1001     case OFPACT_STRIP_VLAN:
1002     case OFPACT_SET_ETH_SRC:
1003     case OFPACT_SET_ETH_DST:
1004     case OFPACT_SET_IPV4_SRC:
1005     case OFPACT_SET_IPV4_DST:
1006     case OFPACT_SET_IPV4_DSCP:
1007     case OFPACT_SET_L4_SRC_PORT:
1008     case OFPACT_SET_L4_DST_PORT:
1009         return 0;
1010
1011     case OFPACT_REG_MOVE:
1012         return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1013
1014     case OFPACT_REG_LOAD:
1015         return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1016
1017     case OFPACT_DEC_TTL:
1018     case OFPACT_SET_TUNNEL:
1019     case OFPACT_SET_QUEUE:
1020     case OFPACT_POP_QUEUE:
1021     case OFPACT_FIN_TIMEOUT:
1022     case OFPACT_RESUBMIT:
1023         return 0;
1024
1025     case OFPACT_LEARN:
1026         return learn_check(ofpact_get_LEARN(a), flow);
1027
1028     case OFPACT_MULTIPATH:
1029         return multipath_check(ofpact_get_MULTIPATH(a), flow);
1030
1031     case OFPACT_AUTOPATH:
1032         return autopath_check(ofpact_get_AUTOPATH(a), flow);
1033
1034     case OFPACT_NOTE:
1035     case OFPACT_EXIT:
1036         return 0;
1037
1038     default:
1039         NOT_REACHED();
1040     }
1041 }
1042
1043 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1044  * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1045  * switch with no more than 'max_ports' ports. */
1046 enum ofperr
1047 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1048               const struct flow *flow, int max_ports)
1049 {
1050     const struct ofpact *a;
1051
1052     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1053         enum ofperr error = ofpact_check__(a, flow, max_ports);
1054         if (error) {
1055             return error;
1056         }
1057     }
1058
1059     return 0;
1060 }
1061 \f
1062 /* Converting ofpacts to Nicira OpenFlow extensions. */
1063
1064 static void
1065 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1066                                 struct ofpbuf *out)
1067 {
1068     struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1069
1070     naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1071                                            output_reg->src.n_bits);
1072     naor->src = htonl(output_reg->src.field->nxm_header);
1073     naor->max_len = htons(output_reg->max_len);
1074 }
1075
1076 static void
1077 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1078                          struct ofpbuf *out)
1079 {
1080     struct nx_action_resubmit *nar;
1081
1082     if (resubmit->table_id == 0xff
1083         && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1084         nar = ofputil_put_NXAST_RESUBMIT(out);
1085     } else {
1086         nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1087         nar->table = resubmit->table_id;
1088     }
1089     nar->in_port = htons(resubmit->in_port);
1090 }
1091
1092 static void
1093 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1094                            struct ofpbuf *out)
1095 {
1096     uint64_t tun_id = tunnel->tun_id;
1097
1098     if (tun_id <= UINT32_MAX
1099         && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1100         ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1101     } else {
1102         ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1103     }
1104 }
1105
1106 static void
1107 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1108 {
1109     size_t start_ofs = out->size;
1110     struct nx_action_note *nan;
1111     unsigned int remainder;
1112     unsigned int len;
1113
1114     nan = ofputil_put_NXAST_NOTE(out);
1115     out->size -= sizeof nan->note;
1116
1117     ofpbuf_put(out, note->data, note->length);
1118
1119     len = out->size - start_ofs;
1120     remainder = len % OFP_ACTION_ALIGN;
1121     if (remainder) {
1122         ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1123     }
1124     nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1125     nan->len = htons(out->size - start_ofs);
1126 }
1127
1128 static void
1129 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1130                            struct ofpbuf *out)
1131 {
1132     struct nx_action_controller *nac;
1133
1134     nac = ofputil_put_NXAST_CONTROLLER(out);
1135     nac->max_len = htons(oc->max_len);
1136     nac->controller_id = htons(oc->controller_id);
1137     nac->reason = oc->reason;
1138 }
1139
1140 static void
1141 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1142                         struct ofpbuf *out)
1143 {
1144     if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1145         ofputil_put_NXAST_DEC_TTL(out);
1146     } else {
1147         struct nx_action_cnt_ids *nac_ids =
1148             ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1149         int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1150         ovs_be16 *ids;
1151         size_t i;
1152
1153         nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1154         nac_ids->n_controllers = htons(oc_ids->n_controllers);
1155
1156         ids = ofpbuf_put_zeros(out, ids_len);
1157         for (i = 0; i < oc_ids->n_controllers; i++) {
1158             ids[i] = htons(oc_ids->cnt_ids[i]);
1159         }
1160     }
1161 }
1162
1163 static void
1164 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1165                             struct ofpbuf *out)
1166 {
1167     struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1168     naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1169     naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1170 }
1171
1172 static void
1173 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1174 {
1175     switch (a->type) {
1176     case OFPACT_CONTROLLER:
1177         ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1178         break;
1179
1180     case OFPACT_OUTPUT_REG:
1181         ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1182         break;
1183
1184     case OFPACT_BUNDLE:
1185         bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1186         break;
1187
1188     case OFPACT_REG_MOVE:
1189         nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1190         break;
1191
1192     case OFPACT_REG_LOAD:
1193         nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1194         break;
1195
1196     case OFPACT_DEC_TTL:
1197         ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1198         break;
1199
1200     case OFPACT_SET_TUNNEL:
1201         ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1202         break;
1203
1204     case OFPACT_SET_QUEUE:
1205         ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1206             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1207         break;
1208
1209     case OFPACT_POP_QUEUE:
1210         ofputil_put_NXAST_POP_QUEUE(out);
1211         break;
1212
1213     case OFPACT_FIN_TIMEOUT:
1214         ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1215         break;
1216
1217     case OFPACT_RESUBMIT:
1218         ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1219         break;
1220
1221     case OFPACT_LEARN:
1222         learn_to_nxast(ofpact_get_LEARN(a), out);
1223         break;
1224
1225     case OFPACT_MULTIPATH:
1226         multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1227         break;
1228
1229     case OFPACT_AUTOPATH:
1230         autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1231         break;
1232
1233     case OFPACT_NOTE:
1234         ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1235         break;
1236
1237     case OFPACT_EXIT:
1238         ofputil_put_NXAST_EXIT(out);
1239         break;
1240
1241     case OFPACT_OUTPUT:
1242     case OFPACT_ENQUEUE:
1243     case OFPACT_SET_VLAN_VID:
1244     case OFPACT_SET_VLAN_PCP:
1245     case OFPACT_STRIP_VLAN:
1246     case OFPACT_SET_ETH_SRC:
1247     case OFPACT_SET_ETH_DST:
1248     case OFPACT_SET_IPV4_SRC:
1249     case OFPACT_SET_IPV4_DST:
1250     case OFPACT_SET_IPV4_DSCP:
1251     case OFPACT_SET_L4_SRC_PORT:
1252     case OFPACT_SET_L4_DST_PORT:
1253         NOT_REACHED();
1254     }
1255 }
1256 \f
1257 /* Converting ofpacts to OpenFlow 1.0. */
1258
1259 static void
1260 ofpact_output_to_openflow10(const struct ofpact_output *output,
1261                             struct ofpbuf *out)
1262 {
1263     struct ofp10_action_output *oao;
1264
1265     oao = ofputil_put_OFPAT10_OUTPUT(out);
1266     oao->port = htons(output->port);
1267     oao->max_len = htons(output->max_len);
1268 }
1269
1270 static void
1271 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1272                              struct ofpbuf *out)
1273 {
1274     struct ofp_action_enqueue *oae;
1275
1276     oae = ofputil_put_OFPAT10_ENQUEUE(out);
1277     oae->port = htons(enqueue->port);
1278     oae->queue_id = htonl(enqueue->queue);
1279 }
1280
1281 static void
1282 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1283 {
1284     switch (a->type) {
1285     case OFPACT_OUTPUT:
1286         ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1287         break;
1288
1289     case OFPACT_ENQUEUE:
1290         ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1291         break;
1292
1293     case OFPACT_SET_VLAN_VID:
1294         ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1295             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1296         break;
1297
1298     case OFPACT_SET_VLAN_PCP:
1299         ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1300             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1301         break;
1302
1303     case OFPACT_STRIP_VLAN:
1304         ofputil_put_OFPAT10_STRIP_VLAN(out);
1305         break;
1306
1307     case OFPACT_SET_ETH_SRC:
1308         memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1309                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1310         break;
1311
1312     case OFPACT_SET_ETH_DST:
1313         memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1314                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1315         break;
1316
1317     case OFPACT_SET_IPV4_SRC:
1318         ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1319             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1320         break;
1321
1322     case OFPACT_SET_IPV4_DST:
1323         ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1324             = ofpact_get_SET_IPV4_DST(a)->ipv4;
1325         break;
1326
1327     case OFPACT_SET_IPV4_DSCP:
1328         ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1329             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1330         break;
1331
1332     case OFPACT_SET_L4_SRC_PORT:
1333         ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1334             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1335         break;
1336
1337     case OFPACT_SET_L4_DST_PORT:
1338         ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1339             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1340         break;
1341
1342     case OFPACT_CONTROLLER:
1343     case OFPACT_OUTPUT_REG:
1344     case OFPACT_BUNDLE:
1345     case OFPACT_REG_MOVE:
1346     case OFPACT_REG_LOAD:
1347     case OFPACT_DEC_TTL:
1348     case OFPACT_SET_TUNNEL:
1349     case OFPACT_SET_QUEUE:
1350     case OFPACT_POP_QUEUE:
1351     case OFPACT_FIN_TIMEOUT:
1352     case OFPACT_RESUBMIT:
1353     case OFPACT_LEARN:
1354     case OFPACT_MULTIPATH:
1355     case OFPACT_AUTOPATH:
1356     case OFPACT_NOTE:
1357     case OFPACT_EXIT:
1358         ofpact_to_nxast(a, out);
1359         break;
1360     }
1361 }
1362
1363 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1364  * actions in 'openflow', appending the actions to any existing data in
1365  * 'openflow'. */
1366 void
1367 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1368                        struct ofpbuf *openflow)
1369 {
1370     const struct ofpact *a;
1371
1372     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1373         ofpact_to_openflow10(a, openflow);
1374     }
1375 }
1376 \f
1377 /* Converting ofpacts to OpenFlow 1.1. */
1378
1379 static void
1380 ofpact_output_to_openflow11(const struct ofpact_output *output,
1381                             struct ofpbuf *out)
1382 {
1383     struct ofp11_action_output *oao;
1384
1385     oao = ofputil_put_OFPAT11_OUTPUT(out);
1386     oao->port = ofputil_port_to_ofp11(output->port);
1387     oao->max_len = htons(output->max_len);
1388 }
1389
1390 static void
1391 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1392 {
1393     switch (a->type) {
1394     case OFPACT_OUTPUT:
1395         return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1396
1397     case OFPACT_ENQUEUE:
1398         /* XXX */
1399         break;
1400
1401     case OFPACT_SET_VLAN_VID:
1402         ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1403             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1404         break;
1405
1406     case OFPACT_SET_VLAN_PCP:
1407         ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1408             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1409         break;
1410
1411     case OFPACT_STRIP_VLAN:
1412         /* XXX */
1413         break;
1414
1415     case OFPACT_SET_ETH_SRC:
1416         memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1417                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1418         break;
1419
1420     case OFPACT_SET_ETH_DST:
1421         memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1422                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1423         break;
1424
1425     case OFPACT_SET_IPV4_SRC:
1426         ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1427             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1428         break;
1429
1430     case OFPACT_SET_IPV4_DST:
1431         ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1432             = ofpact_get_SET_IPV4_DST(a)->ipv4;
1433         break;
1434
1435     case OFPACT_SET_IPV4_DSCP:
1436         ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1437             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1438         break;
1439
1440     case OFPACT_SET_L4_SRC_PORT:
1441         ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1442             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1443         break;
1444
1445     case OFPACT_SET_L4_DST_PORT:
1446         ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1447             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1448         break;
1449
1450     case OFPACT_CONTROLLER:
1451     case OFPACT_OUTPUT_REG:
1452     case OFPACT_BUNDLE:
1453     case OFPACT_REG_MOVE:
1454     case OFPACT_REG_LOAD:
1455     case OFPACT_DEC_TTL:
1456     case OFPACT_SET_TUNNEL:
1457     case OFPACT_SET_QUEUE:
1458     case OFPACT_POP_QUEUE:
1459     case OFPACT_FIN_TIMEOUT:
1460     case OFPACT_RESUBMIT:
1461     case OFPACT_LEARN:
1462     case OFPACT_MULTIPATH:
1463     case OFPACT_AUTOPATH:
1464     case OFPACT_NOTE:
1465     case OFPACT_EXIT:
1466         ofpact_to_nxast(a, out);
1467         break;
1468     }
1469 }
1470
1471 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1472  * 1.1 actions in 'openflow', appending the actions to any existing data in
1473  * 'openflow'. */
1474 size_t
1475 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1476                                size_t ofpacts_len, struct ofpbuf *openflow)
1477 {
1478     const struct ofpact *a;
1479     size_t start_size = openflow->size;
1480
1481     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1482         ofpact_to_openflow11(a, openflow);
1483     }
1484
1485     return openflow->size - start_size;
1486 }
1487
1488 void
1489 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1490                                     size_t ofpacts_len,
1491                                     struct ofpbuf *openflow)
1492 {
1493     struct ofp11_instruction_actions *oia;
1494     size_t ofs;
1495
1496     /* Put an OFPIT11_APPLY_ACTIONS instruction and fill it in. */
1497     ofs = openflow->size;
1498     instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1499     ofpacts_put_openflow11_actions(ofpacts, ofpacts_len, openflow);
1500
1501     /* Update the instruction's length (or, if it's empty, delete it). */
1502     oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1503     if (openflow->size > ofs + sizeof *oia) {
1504         oia->len = htons(openflow->size - ofs);
1505     } else {
1506         openflow->size = ofs;
1507     }
1508 }
1509 \f
1510 /* Returns true if 'action' outputs to 'port', false otherwise. */
1511 static bool
1512 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1513 {
1514     switch (ofpact->type) {
1515     case OFPACT_OUTPUT:
1516         return ofpact_get_OUTPUT(ofpact)->port == port;
1517     case OFPACT_ENQUEUE:
1518         return ofpact_get_ENQUEUE(ofpact)->port == port;
1519     case OFPACT_CONTROLLER:
1520         return port == OFPP_CONTROLLER;
1521
1522     case OFPACT_OUTPUT_REG:
1523     case OFPACT_BUNDLE:
1524     case OFPACT_SET_VLAN_VID:
1525     case OFPACT_SET_VLAN_PCP:
1526     case OFPACT_STRIP_VLAN:
1527     case OFPACT_SET_ETH_SRC:
1528     case OFPACT_SET_ETH_DST:
1529     case OFPACT_SET_IPV4_SRC:
1530     case OFPACT_SET_IPV4_DST:
1531     case OFPACT_SET_IPV4_DSCP:
1532     case OFPACT_SET_L4_SRC_PORT:
1533     case OFPACT_SET_L4_DST_PORT:
1534     case OFPACT_REG_MOVE:
1535     case OFPACT_REG_LOAD:
1536     case OFPACT_DEC_TTL:
1537     case OFPACT_SET_TUNNEL:
1538     case OFPACT_SET_QUEUE:
1539     case OFPACT_POP_QUEUE:
1540     case OFPACT_FIN_TIMEOUT:
1541     case OFPACT_RESUBMIT:
1542     case OFPACT_LEARN:
1543     case OFPACT_MULTIPATH:
1544     case OFPACT_AUTOPATH:
1545     case OFPACT_NOTE:
1546     case OFPACT_EXIT:
1547     default:
1548         return false;
1549     }
1550 }
1551
1552 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1553  * to 'port', false otherwise. */
1554 bool
1555 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1556                        uint16_t port)
1557 {
1558     const struct ofpact *a;
1559
1560     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1561         if (ofpact_outputs_to_port(a, port)) {
1562             return true;
1563         }
1564     }
1565
1566     return false;
1567 }
1568
1569 bool
1570 ofpacts_equal(const struct ofpact *a, size_t a_len,
1571               const struct ofpact *b, size_t b_len)
1572 {
1573     return a_len == b_len && !memcmp(a, b, a_len);
1574 }
1575 \f
1576 /* Formatting ofpacts. */
1577
1578 static void
1579 print_note(const struct ofpact_note *note, struct ds *string)
1580 {
1581     size_t i;
1582
1583     ds_put_cstr(string, "note:");
1584     for (i = 0; i < note->length; i++) {
1585         if (i) {
1586             ds_put_char(string, '.');
1587         }
1588         ds_put_format(string, "%02"PRIx8, note->data[i]);
1589     }
1590 }
1591
1592 static void
1593 print_dec_ttl(const struct ofpact_cnt_ids *ids,
1594               struct ds *s)
1595 {
1596     size_t i;
1597
1598     ds_put_cstr(s, "dec_ttl");
1599     if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1600         ds_put_cstr(s, "(");
1601         for (i = 0; i < ids->n_controllers; i++) {
1602             if (i) {
1603                 ds_put_cstr(s, ",");
1604             }
1605             ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1606         }
1607         ds_put_cstr(s, ")");
1608     }
1609 }
1610
1611 static void
1612 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1613                   struct ds *s)
1614 {
1615     ds_put_cstr(s, "fin_timeout(");
1616     if (fin_timeout->fin_idle_timeout) {
1617         ds_put_format(s, "idle_timeout=%"PRIu16",",
1618                       fin_timeout->fin_idle_timeout);
1619     }
1620     if (fin_timeout->fin_hard_timeout) {
1621         ds_put_format(s, "hard_timeout=%"PRIu16",",
1622                       fin_timeout->fin_hard_timeout);
1623     }
1624     ds_chomp(s, ',');
1625     ds_put_char(s, ')');
1626 }
1627
1628 static void
1629 ofpact_format(const struct ofpact *a, struct ds *s)
1630 {
1631     const struct ofpact_enqueue *enqueue;
1632     const struct ofpact_resubmit *resubmit;
1633     const struct ofpact_autopath *autopath;
1634     const struct ofpact_controller *controller;
1635     const struct ofpact_tunnel *tunnel;
1636     uint16_t port;
1637
1638     switch (a->type) {
1639     case OFPACT_OUTPUT:
1640         port = ofpact_get_OUTPUT(a)->port;
1641         if (port < OFPP_MAX) {
1642             ds_put_format(s, "output:%"PRIu16, port);
1643         } else {
1644             ofputil_format_port(port, s);
1645             if (port == OFPP_CONTROLLER) {
1646                 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1647             }
1648         }
1649         break;
1650
1651     case OFPACT_CONTROLLER:
1652         controller = ofpact_get_CONTROLLER(a);
1653         if (controller->reason == OFPR_ACTION &&
1654             controller->controller_id == 0) {
1655             ds_put_format(s, "CONTROLLER:%"PRIu16,
1656                           ofpact_get_CONTROLLER(a)->max_len);
1657         } else {
1658             enum ofp_packet_in_reason reason = controller->reason;
1659
1660             ds_put_cstr(s, "controller(");
1661             if (reason != OFPR_ACTION) {
1662                 ds_put_format(s, "reason=%s,",
1663                               ofputil_packet_in_reason_to_string(reason));
1664             }
1665             if (controller->max_len != UINT16_MAX) {
1666                 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1667             }
1668             if (controller->controller_id != 0) {
1669                 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1670             }
1671             ds_chomp(s, ',');
1672             ds_put_char(s, ')');
1673         }
1674         break;
1675
1676     case OFPACT_ENQUEUE:
1677         enqueue = ofpact_get_ENQUEUE(a);
1678         ds_put_format(s, "enqueue:");
1679         ofputil_format_port(enqueue->port, s);
1680         ds_put_format(s, "q%"PRIu32, enqueue->queue);
1681         break;
1682
1683     case OFPACT_OUTPUT_REG:
1684         ds_put_cstr(s, "output:");
1685         mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1686         break;
1687
1688     case OFPACT_BUNDLE:
1689         bundle_format(ofpact_get_BUNDLE(a), s);
1690         break;
1691
1692     case OFPACT_SET_VLAN_VID:
1693         ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1694                       ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1695         break;
1696
1697     case OFPACT_SET_VLAN_PCP:
1698         ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1699                       ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1700         break;
1701
1702     case OFPACT_STRIP_VLAN:
1703         ds_put_cstr(s, "strip_vlan");
1704         break;
1705
1706     case OFPACT_SET_ETH_SRC:
1707         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1708                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1709         break;
1710
1711     case OFPACT_SET_ETH_DST:
1712         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1713                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1714         break;
1715
1716     case OFPACT_SET_IPV4_SRC:
1717         ds_put_format(s, "mod_nw_src:"IP_FMT,
1718                       IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1719         break;
1720
1721     case OFPACT_SET_IPV4_DST:
1722         ds_put_format(s, "mod_nw_dst:"IP_FMT,
1723                       IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1724         break;
1725
1726     case OFPACT_SET_IPV4_DSCP:
1727         ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1728         break;
1729
1730     case OFPACT_SET_L4_SRC_PORT:
1731         ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1732         break;
1733
1734     case OFPACT_SET_L4_DST_PORT:
1735         ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1736         break;
1737
1738     case OFPACT_REG_MOVE:
1739         nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1740         break;
1741
1742     case OFPACT_REG_LOAD:
1743         nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1744         break;
1745
1746     case OFPACT_DEC_TTL:
1747         print_dec_ttl(ofpact_get_DEC_TTL(a), s);
1748         break;
1749
1750     case OFPACT_SET_TUNNEL:
1751         tunnel = ofpact_get_SET_TUNNEL(a);
1752         ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1753                       (tunnel->tun_id > UINT32_MAX
1754                        || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1755                       tunnel->tun_id);
1756         break;
1757
1758     case OFPACT_SET_QUEUE:
1759         ds_put_format(s, "set_queue:%"PRIu32,
1760                       ofpact_get_SET_QUEUE(a)->queue_id);
1761         break;
1762
1763     case OFPACT_POP_QUEUE:
1764         ds_put_cstr(s, "pop_queue");
1765         break;
1766
1767     case OFPACT_FIN_TIMEOUT:
1768         print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1769         break;
1770
1771     case OFPACT_RESUBMIT:
1772         resubmit = ofpact_get_RESUBMIT(a);
1773         if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
1774             ds_put_cstr(s, "resubmit:");
1775             ofputil_format_port(resubmit->in_port, s);
1776         } else {
1777             ds_put_format(s, "resubmit(");
1778             if (resubmit->in_port != OFPP_IN_PORT) {
1779                 ofputil_format_port(resubmit->in_port, s);
1780             }
1781             ds_put_char(s, ',');
1782             if (resubmit->table_id != 255) {
1783                 ds_put_format(s, "%"PRIu8, resubmit->table_id);
1784             }
1785             ds_put_char(s, ')');
1786         }
1787         break;
1788
1789     case OFPACT_LEARN:
1790         learn_format(ofpact_get_LEARN(a), s);
1791         break;
1792
1793     case OFPACT_MULTIPATH:
1794         multipath_format(ofpact_get_MULTIPATH(a), s);
1795         break;
1796
1797     case OFPACT_AUTOPATH:
1798         autopath = ofpact_get_AUTOPATH(a);
1799         ds_put_cstr(s, "autopath(");
1800         ofputil_format_port(autopath->port, s);
1801         ds_put_char(s, ',');
1802         mf_format_subfield(&autopath->dst, s);
1803         ds_put_char(s, ')');
1804         break;
1805
1806     case OFPACT_NOTE:
1807         print_note(ofpact_get_NOTE(a), s);
1808         break;
1809
1810     case OFPACT_EXIT:
1811         ds_put_cstr(s, "exit");
1812         break;
1813     }
1814 }
1815
1816 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
1817  * 'ofpacts' to 'string'. */
1818 void
1819 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
1820                struct ds *string)
1821 {
1822     ds_put_cstr(string, "actions=");
1823     if (!ofpacts_len) {
1824         ds_put_cstr(string, "drop");
1825     } else {
1826         const struct ofpact *a;
1827
1828         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1829             if (a != ofpacts) {
1830                 ds_put_cstr(string, ",");
1831             }
1832             ofpact_format(a, string);
1833         }
1834     }
1835 }
1836 \f
1837 /* Internal use by helpers. */
1838
1839 void *
1840 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
1841 {
1842     struct ofpact *ofpact;
1843
1844     ofpact_pad(ofpacts);
1845     ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
1846     ofpact_init(ofpact, type, len);
1847     return ofpact;
1848 }
1849
1850 void
1851 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
1852 {
1853     memset(ofpact, 0, len);
1854     ofpact->type = type;
1855     ofpact->compat = OFPUTIL_ACTION_INVALID;
1856     ofpact->len = len;
1857 }
1858 \f
1859 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
1860  * starting at 'ofpact'.
1861  *
1862  * This is the correct way to update a variable-length ofpact's length after
1863  * adding the variable-length part of the payload.  (See the large comment
1864  * near the end of ofp-actions.h for more information.) */
1865 void
1866 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
1867 {
1868     assert(ofpact == ofpacts->l2);
1869     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
1870 }
1871
1872 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
1873  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
1874  * client must call this itself after adding the final ofpact to an array of
1875  * them.
1876  *
1877  * (The consequences of failing to call this function are probably not dire.
1878  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
1879  * not dereference it.  That's undefined behavior, technically, but it will not
1880  * cause a real problem on common systems.  Still, it seems better to call
1881  * it.) */
1882 void
1883 ofpact_pad(struct ofpbuf *ofpacts)
1884 {
1885     unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
1886     if (rem) {
1887         ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
1888     }
1889 }