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