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