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