Introduce ofpacts, an abstraction of OpenFlow actions.
[cascardo/ovs.git] / lib / ofp-actions.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofp-actions.h"
19 #include "autopath.h"
20 #include "bundle.h"
21 #include "byte-order.h"
22 #include "compiler.h"
23 #include "dynamic-string.h"
24 #include "learn.h"
25 #include "meta-flow.h"
26 #include "multipath.h"
27 #include "nx-match.h"
28 #include "ofp-util.h"
29 #include "ofpbuf.h"
30 #include "vlog.h"
31
32 VLOG_DEFINE_THIS_MODULE(ofp_actions);
33
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35 \f
36 /* Converting OpenFlow 1.0 to ofpacts. */
37
38 static enum ofperr
39 output_from_openflow10(const struct ofp_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_openflow10__(const union ofp_action *a, struct ofpbuf *out)
220 {
221     const struct nx_action_resubmit *nar;
222     const struct nx_action_set_tunnel *nast;
223     const struct nx_action_set_queue *nasq;
224     const struct nx_action_note *nan;
225     const struct nx_action_set_tunnel64 *nast64;
226     struct ofpact_tunnel *tunnel;
227     enum ofputil_action_code code;
228     enum ofperr error;
229
230     error = decode_openflow10_action(a, &code);
231     if (error) {
232         return error;
233     }
234
235     switch (code) {
236     case OFPUTIL_ACTION_INVALID:
237         NOT_REACHED();
238
239     case OFPUTIL_OFPAT10_OUTPUT:
240         return output_from_openflow10((const struct ofp_action_output *) a,
241                                       out);
242
243     case OFPUTIL_OFPAT10_SET_VLAN_VID:
244         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
245             return OFPERR_OFPBAC_BAD_ARGUMENT;
246         }
247         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
248         break;
249
250     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
251         if (a->vlan_pcp.vlan_pcp & ~7) {
252             return OFPERR_OFPBAC_BAD_ARGUMENT;
253         }
254         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
255         break;
256
257     case OFPUTIL_OFPAT10_STRIP_VLAN:
258         ofpact_put_STRIP_VLAN(out);
259         break;
260
261     case OFPUTIL_OFPAT10_SET_DL_SRC:
262         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
263                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
264         break;
265
266     case OFPUTIL_OFPAT10_SET_DL_DST:
267         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
268                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
269         break;
270
271     case OFPUTIL_OFPAT10_SET_NW_SRC:
272         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
273         break;
274
275     case OFPUTIL_OFPAT10_SET_NW_DST:
276         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
277         break;
278
279     case OFPUTIL_OFPAT10_SET_NW_TOS:
280         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
281             return OFPERR_OFPBAC_BAD_ARGUMENT;
282         }
283         ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
284         break;
285
286     case OFPUTIL_OFPAT10_SET_TP_SRC:
287         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
288         break;
289
290     case OFPUTIL_OFPAT10_SET_TP_DST:
291         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
292
293         break;
294
295     case OFPUTIL_OFPAT10_ENQUEUE:
296         error = enqueue_from_openflow10((const struct ofp_action_enqueue *) a,
297                                         out);
298         break;
299
300     case OFPUTIL_NXAST_RESUBMIT:
301         resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
302         break;
303
304     case OFPUTIL_NXAST_SET_TUNNEL:
305         nast = (const struct nx_action_set_tunnel *) a;
306         tunnel = ofpact_put_SET_TUNNEL(out);
307         tunnel->ofpact.compat = code;
308         tunnel->tun_id = ntohl(nast->tun_id);
309         break;
310
311     case OFPUTIL_NXAST_SET_QUEUE:
312         nasq = (const struct nx_action_set_queue *) a;
313         ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
314         break;
315
316     case OFPUTIL_NXAST_POP_QUEUE:
317         ofpact_put_POP_QUEUE(out);
318         break;
319
320     case OFPUTIL_NXAST_REG_MOVE:
321         error = nxm_reg_move_from_openflow(
322             (const struct nx_action_reg_move *) a, out);
323         break;
324
325     case OFPUTIL_NXAST_REG_LOAD:
326         error = nxm_reg_load_from_openflow(
327             (const struct nx_action_reg_load *) a, out);
328         break;
329
330     case OFPUTIL_NXAST_NOTE:
331         nan = (const struct nx_action_note *) a;
332         note_from_openflow(nan, out);
333         break;
334
335     case OFPUTIL_NXAST_SET_TUNNEL64:
336         nast64 = (const struct nx_action_set_tunnel64 *) a;
337         tunnel = ofpact_put_SET_TUNNEL(out);
338         tunnel->ofpact.compat = code;
339         tunnel->tun_id = ntohll(nast64->tun_id);
340         break;
341
342     case OFPUTIL_NXAST_MULTIPATH:
343         error = multipath_from_openflow((const struct nx_action_multipath *) a,
344                                         ofpact_put_MULTIPATH(out));
345         break;
346
347     case OFPUTIL_NXAST_AUTOPATH:
348         error = autopath_from_openflow((const struct nx_action_autopath *) a,
349                                        ofpact_put_AUTOPATH(out));
350         break;
351
352     case OFPUTIL_NXAST_BUNDLE:
353     case OFPUTIL_NXAST_BUNDLE_LOAD:
354         error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
355         break;
356
357     case OFPUTIL_NXAST_OUTPUT_REG:
358         error = output_reg_from_openflow(
359             (const struct nx_action_output_reg *) a, out);
360         break;
361
362     case OFPUTIL_NXAST_RESUBMIT_TABLE:
363         nar = (const struct nx_action_resubmit *) a;
364         error = resubmit_table_from_openflow(nar, out);
365         break;
366
367     case OFPUTIL_NXAST_LEARN:
368         error = learn_from_openflow((const struct nx_action_learn *) a, out);
369         break;
370
371     case OFPUTIL_NXAST_EXIT:
372         ofpact_put_EXIT(out);
373         break;
374
375     case OFPUTIL_NXAST_DEC_TTL:
376         ofpact_put_DEC_TTL(out);
377         break;
378
379     case OFPUTIL_NXAST_FIN_TIMEOUT:
380         fin_timeout_from_openflow(
381             (const struct nx_action_fin_timeout *) a, out);
382         break;
383
384     case OFPUTIL_NXAST_CONTROLLER:
385         controller_from_openflow((const struct nx_action_controller *) a, out);
386         break;
387     }
388
389     return error;
390 }
391
392 static inline union ofp_action *
393 action_next(const union ofp_action *a)
394 {
395     return ((union ofp_action *) (void *)
396             ((uint8_t *) a + ntohs(a->header.len)));
397 }
398
399 static inline bool
400 action_is_valid(const union ofp_action *a, size_t n_actions)
401 {
402     uint16_t len = ntohs(a->header.len);
403     return (!(len % OFP_ACTION_ALIGN)
404             && len >= sizeof *a
405             && len / sizeof *a <= n_actions);
406 }
407
408 /* This macro is careful to check for actions with bad lengths. */
409 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS)                 \
410     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
411          (LEFT) > 0 && action_is_valid(ITER, LEFT);                     \
412          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
413           (ITER) = action_next(ITER)))
414
415 static enum ofperr
416 ofpact_from_openflow10(const union ofp_action *in, size_t n_in,
417                        struct ofpbuf *out)
418 {
419     const union ofp_action *a;
420     size_t left;
421
422     ACTION_FOR_EACH (a, left, in, n_in) {
423         enum ofperr error = ofpact_from_openflow10__(a, out);
424         if (error) {
425             VLOG_WARN_RL(&rl, "bad action at offset %td (%s)",
426                          (a - in) * sizeof *a, ofperr_get_name(error));
427             return error;
428         }
429     }
430     if (left) {
431         VLOG_WARN_RL(&rl, "bad action format at offset %zu",
432                      (n_in - left) * sizeof *a);
433         return OFPERR_OFPBAC_BAD_LEN;
434     }
435
436     ofpact_pad(out);
437     return 0;
438 }
439
440 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
441  * of 'openflow' into ofpacts.  On success, replaces any existing content in
442  * 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.  Returns 0
443  * if successful, otherwise an OpenFlow error.
444  *
445  * This function does not check that the actions are valid in a given context.
446  * The caller should do so, with ofpacts_check(). */
447 enum ofperr
448 ofpacts_pull_openflow(struct ofpbuf *openflow, unsigned int actions_len,
449                       struct ofpbuf *ofpacts)
450 {
451     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
452     const union ofp_action *actions;
453     enum ofperr error;
454
455     ofpbuf_clear(ofpacts);
456
457     if (actions_len % OFP_ACTION_ALIGN != 0) {
458         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
459                      "multiple of %d", actions_len, OFP_ACTION_ALIGN);
460         return OFPERR_OFPBRC_BAD_LEN;
461     }
462
463     actions = ofpbuf_try_pull(openflow, actions_len);
464     if (actions == NULL) {
465         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
466                      "remaining message length (%zu)",
467                      actions_len, openflow->size);
468         return OFPERR_OFPBRC_BAD_LEN;
469     }
470
471     error = ofpact_from_openflow10(actions, actions_len / OFP_ACTION_ALIGN,
472                                    ofpacts);
473     if (error) {
474         ofpbuf_clear(ofpacts);
475     }
476     return 0;
477 }
478 \f
479 static enum ofperr
480 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
481 {
482     const struct ofpact_enqueue *enqueue;
483
484     switch (a->type) {
485     case OFPACT_OUTPUT:
486         return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
487                                          max_ports);
488
489     case OFPACT_CONTROLLER:
490         return 0;
491
492     case OFPACT_ENQUEUE:
493         enqueue = ofpact_get_ENQUEUE(a);
494         if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
495             && enqueue->port != OFPP_LOCAL) {
496             return OFPERR_OFPBAC_BAD_OUT_PORT;
497         }
498         return 0;
499
500     case OFPACT_OUTPUT_REG:
501         return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
502
503     case OFPACT_BUNDLE:
504         return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
505
506     case OFPACT_SET_VLAN_VID:
507     case OFPACT_SET_VLAN_PCP:
508     case OFPACT_STRIP_VLAN:
509     case OFPACT_SET_ETH_SRC:
510     case OFPACT_SET_ETH_DST:
511     case OFPACT_SET_IPV4_SRC:
512     case OFPACT_SET_IPV4_DST:
513     case OFPACT_SET_IPV4_DSCP:
514     case OFPACT_SET_L4_SRC_PORT:
515     case OFPACT_SET_L4_DST_PORT:
516         return 0;
517
518     case OFPACT_REG_MOVE:
519         return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
520
521     case OFPACT_REG_LOAD:
522         return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
523
524     case OFPACT_DEC_TTL:
525     case OFPACT_SET_TUNNEL:
526     case OFPACT_SET_QUEUE:
527     case OFPACT_POP_QUEUE:
528     case OFPACT_FIN_TIMEOUT:
529     case OFPACT_RESUBMIT:
530         return 0;
531
532     case OFPACT_LEARN:
533         return learn_check(ofpact_get_LEARN(a), flow);
534
535     case OFPACT_MULTIPATH:
536         return multipath_check(ofpact_get_MULTIPATH(a), flow);
537
538     case OFPACT_AUTOPATH:
539         return autopath_check(ofpact_get_AUTOPATH(a), flow);
540
541     case OFPACT_NOTE:
542     case OFPACT_EXIT:
543         return 0;
544
545     default:
546         NOT_REACHED();
547     }
548 }
549
550 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
551  * appropriate for a packet with the prerequisites satisfied by 'flow' in a
552  * switch with no more than 'max_ports' ports. */
553 enum ofperr
554 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
555               const struct flow *flow, int max_ports)
556 {
557     const struct ofpact *a;
558
559     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
560         enum ofperr error = ofpact_check__(a, flow, max_ports);
561         if (error) {
562             return error;
563         }
564     }
565
566     return 0;
567 }
568 \f
569 /* Converting ofpacts to Nicira OpenFlow extensions. */
570
571 static void
572 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
573                                 struct ofpbuf *out)
574 {
575     struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
576
577     naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
578                                            output_reg->src.n_bits);
579     naor->src = htonl(output_reg->src.field->nxm_header);
580     naor->max_len = htons(output_reg->max_len);
581 }
582
583 static void
584 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
585                          struct ofpbuf *out)
586 {
587     struct nx_action_resubmit *nar;
588
589     if (resubmit->table_id == 0xff
590         && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
591         nar = ofputil_put_NXAST_RESUBMIT(out);
592     } else {
593         nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
594         nar->table = resubmit->table_id;
595     }
596     nar->in_port = htons(resubmit->in_port);
597 }
598
599 static void
600 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
601                            struct ofpbuf *out)
602 {
603     uint64_t tun_id = tunnel->tun_id;
604
605     if (tun_id <= UINT32_MAX
606         && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
607         ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
608     } else {
609         ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
610     }
611 }
612
613 static void
614 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
615 {
616     size_t start_ofs = out->size;
617     struct nx_action_note *nan;
618     unsigned int remainder;
619     unsigned int len;
620
621     nan = ofputil_put_NXAST_NOTE(out);
622     out->size -= sizeof nan->note;
623
624     ofpbuf_put(out, note->data, note->length);
625
626     len = out->size - start_ofs;
627     remainder = len % OFP_ACTION_ALIGN;
628     if (remainder) {
629         ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
630     }
631     nan = (struct nx_action_note *)((char *)out->data + start_ofs);
632     nan->len = htons(out->size - start_ofs);
633 }
634
635 static void
636 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
637                            struct ofpbuf *out)
638 {
639     struct nx_action_controller *nac;
640
641     nac = ofputil_put_NXAST_CONTROLLER(out);
642     nac->max_len = htons(oc->max_len);
643     nac->controller_id = htons(oc->controller_id);
644     nac->reason = oc->reason;
645 }
646
647 static void
648 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
649                             struct ofpbuf *out)
650 {
651     struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
652     naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
653     naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
654 }
655
656 static void
657 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
658 {
659     switch (a->type) {
660     case OFPACT_CONTROLLER:
661         ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
662         break;
663
664     case OFPACT_OUTPUT_REG:
665         ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
666         break;
667
668     case OFPACT_BUNDLE:
669         bundle_to_nxast(ofpact_get_BUNDLE(a), out);
670         break;
671
672     case OFPACT_REG_MOVE:
673         nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
674         break;
675
676     case OFPACT_REG_LOAD:
677         nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
678         break;
679
680     case OFPACT_DEC_TTL:
681         ofputil_put_NXAST_DEC_TTL(out);
682         break;
683
684     case OFPACT_SET_TUNNEL:
685         ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
686         break;
687
688     case OFPACT_SET_QUEUE:
689         ofputil_put_NXAST_SET_QUEUE(out)->queue_id
690             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
691         break;
692
693     case OFPACT_POP_QUEUE:
694         ofputil_put_NXAST_POP_QUEUE(out);
695         break;
696
697     case OFPACT_FIN_TIMEOUT:
698         ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
699         break;
700
701     case OFPACT_RESUBMIT:
702         ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
703         break;
704
705     case OFPACT_LEARN:
706         learn_to_nxast(ofpact_get_LEARN(a), out);
707         break;
708
709     case OFPACT_MULTIPATH:
710         multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
711         break;
712
713     case OFPACT_AUTOPATH:
714         autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
715         break;
716
717     case OFPACT_NOTE:
718         ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
719         break;
720
721     case OFPACT_EXIT:
722         ofputil_put_NXAST_EXIT(out);
723         break;
724
725     case OFPACT_OUTPUT:
726     case OFPACT_ENQUEUE:
727     case OFPACT_SET_VLAN_VID:
728     case OFPACT_SET_VLAN_PCP:
729     case OFPACT_STRIP_VLAN:
730     case OFPACT_SET_ETH_SRC:
731     case OFPACT_SET_ETH_DST:
732     case OFPACT_SET_IPV4_SRC:
733     case OFPACT_SET_IPV4_DST:
734     case OFPACT_SET_IPV4_DSCP:
735     case OFPACT_SET_L4_SRC_PORT:
736     case OFPACT_SET_L4_DST_PORT:
737         NOT_REACHED();
738     }
739 }
740 \f
741 /* Converting ofpacts to OpenFlow 1.0. */
742
743 static void
744 ofpact_output_to_openflow10(const struct ofpact_output *output,
745                             struct ofpbuf *out)
746 {
747     struct ofp_action_output *oao;
748
749     oao = ofputil_put_OFPAT10_OUTPUT(out);
750     oao->port = htons(output->port);
751     oao->max_len = htons(output->max_len);
752 }
753
754 static void
755 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
756                              struct ofpbuf *out)
757 {
758     struct ofp_action_enqueue *oae;
759
760     oae = ofputil_put_OFPAT10_ENQUEUE(out);
761     oae->port = htons(enqueue->port);
762     oae->queue_id = htonl(enqueue->queue);
763 }
764
765 static void
766 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
767 {
768     switch (a->type) {
769     case OFPACT_OUTPUT:
770         ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
771         break;
772
773     case OFPACT_ENQUEUE:
774         ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
775         break;
776
777     case OFPACT_SET_VLAN_VID:
778         ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
779             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
780         break;
781
782     case OFPACT_SET_VLAN_PCP:
783         ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
784             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
785         break;
786
787     case OFPACT_STRIP_VLAN:
788         ofputil_put_OFPAT10_STRIP_VLAN(out);
789         break;
790
791     case OFPACT_SET_ETH_SRC:
792         memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
793                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
794         break;
795
796     case OFPACT_SET_ETH_DST:
797         memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
798                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
799         break;
800
801     case OFPACT_SET_IPV4_SRC:
802         ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
803             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
804         break;
805
806     case OFPACT_SET_IPV4_DST:
807         ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
808             = ofpact_get_SET_IPV4_DST(a)->ipv4;
809         break;
810
811     case OFPACT_SET_IPV4_DSCP:
812         ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
813             = ofpact_get_SET_IPV4_DSCP(a)->dscp;
814         break;
815
816     case OFPACT_SET_L4_SRC_PORT:
817         ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
818             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
819         break;
820
821     case OFPACT_SET_L4_DST_PORT:
822         ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
823             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
824         break;
825
826     case OFPACT_CONTROLLER:
827     case OFPACT_OUTPUT_REG:
828     case OFPACT_BUNDLE:
829     case OFPACT_REG_MOVE:
830     case OFPACT_REG_LOAD:
831     case OFPACT_DEC_TTL:
832     case OFPACT_SET_TUNNEL:
833     case OFPACT_SET_QUEUE:
834     case OFPACT_POP_QUEUE:
835     case OFPACT_FIN_TIMEOUT:
836     case OFPACT_RESUBMIT:
837     case OFPACT_LEARN:
838     case OFPACT_MULTIPATH:
839     case OFPACT_AUTOPATH:
840     case OFPACT_NOTE:
841     case OFPACT_EXIT:
842         ofpact_to_nxast(a, out);
843         break;
844     }
845 }
846
847 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
848  * actions in 'openflow', appending the actions to any existing data in
849  * 'openflow'. */
850 void
851 ofpacts_to_openflow(const struct ofpact ofpacts[], size_t ofpacts_len,
852                     struct ofpbuf *openflow)
853 {
854     const struct ofpact *a;
855
856     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
857         ofpact_to_openflow10(a, openflow);
858     }
859 }
860 \f
861 /* Returns true if 'action' outputs to 'port', false otherwise. */
862 static bool
863 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
864 {
865     switch (ofpact->type) {
866     case OFPACT_OUTPUT:
867         return ofpact_get_OUTPUT(ofpact)->port == port;
868     case OFPACT_ENQUEUE:
869         return ofpact_get_ENQUEUE(ofpact)->port == port;
870     case OFPACT_CONTROLLER:
871         return port == OFPP_CONTROLLER;
872
873     case OFPACT_OUTPUT_REG:
874     case OFPACT_BUNDLE:
875     case OFPACT_SET_VLAN_VID:
876     case OFPACT_SET_VLAN_PCP:
877     case OFPACT_STRIP_VLAN:
878     case OFPACT_SET_ETH_SRC:
879     case OFPACT_SET_ETH_DST:
880     case OFPACT_SET_IPV4_SRC:
881     case OFPACT_SET_IPV4_DST:
882     case OFPACT_SET_IPV4_DSCP:
883     case OFPACT_SET_L4_SRC_PORT:
884     case OFPACT_SET_L4_DST_PORT:
885     case OFPACT_REG_MOVE:
886     case OFPACT_REG_LOAD:
887     case OFPACT_DEC_TTL:
888     case OFPACT_SET_TUNNEL:
889     case OFPACT_SET_QUEUE:
890     case OFPACT_POP_QUEUE:
891     case OFPACT_FIN_TIMEOUT:
892     case OFPACT_RESUBMIT:
893     case OFPACT_LEARN:
894     case OFPACT_MULTIPATH:
895     case OFPACT_AUTOPATH:
896     case OFPACT_NOTE:
897     case OFPACT_EXIT:
898     default:
899         return false;
900     }
901 }
902
903 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
904  * to 'port', false otherwise. */
905 bool
906 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
907                        uint16_t port)
908 {
909     const struct ofpact *a;
910
911     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
912         if (ofpact_outputs_to_port(a, port)) {
913             return true;
914         }
915     }
916
917     return false;
918 }
919
920 bool
921 ofpacts_equal(const struct ofpact *a, size_t a_len,
922               const struct ofpact *b, size_t b_len)
923 {
924     return a_len == b_len && !memcmp(a, b, a_len);
925 }
926 \f
927 /* Formatting ofpacts. */
928
929 static void
930 print_note(const struct ofpact_note *note, struct ds *string)
931 {
932     size_t i;
933
934     ds_put_cstr(string, "note:");
935     for (i = 0; i < note->length; i++) {
936         if (i) {
937             ds_put_char(string, '.');
938         }
939         ds_put_format(string, "%02"PRIx8, note->data[i]);
940     }
941 }
942
943 static void
944 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
945                   struct ds *s)
946 {
947     ds_put_cstr(s, "fin_timeout(");
948     if (fin_timeout->fin_idle_timeout) {
949         ds_put_format(s, "idle_timeout=%"PRIu16",",
950                       fin_timeout->fin_idle_timeout);
951     }
952     if (fin_timeout->fin_hard_timeout) {
953         ds_put_format(s, "hard_timeout=%"PRIu16",",
954                       fin_timeout->fin_hard_timeout);
955     }
956     ds_chomp(s, ',');
957     ds_put_char(s, ')');
958 }
959
960 static void
961 ofpact_format(const struct ofpact *a, struct ds *s)
962 {
963     const struct ofpact_enqueue *enqueue;
964     const struct ofpact_resubmit *resubmit;
965     const struct ofpact_autopath *autopath;
966     const struct ofpact_controller *controller;
967     const struct ofpact_tunnel *tunnel;
968     uint16_t port;
969
970     switch (a->type) {
971     case OFPACT_OUTPUT:
972         port = ofpact_get_OUTPUT(a)->port;
973         if (port < OFPP_MAX) {
974             ds_put_format(s, "output:%"PRIu16, port);
975         } else {
976             ofputil_format_port(port, s);
977             if (port == OFPP_CONTROLLER) {
978                 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
979             }
980         }
981         break;
982
983     case OFPACT_CONTROLLER:
984         controller = ofpact_get_CONTROLLER(a);
985         if (controller->reason == OFPR_ACTION &&
986             controller->controller_id == 0) {
987             ds_put_format(s, "CONTROLLER:%"PRIu16,
988                           ofpact_get_CONTROLLER(a)->max_len);
989         } else {
990             enum ofp_packet_in_reason reason = controller->reason;
991
992             ds_put_cstr(s, "controller(");
993             if (reason != OFPR_ACTION) {
994                 ds_put_format(s, "reason=%s,",
995                               ofputil_packet_in_reason_to_string(reason));
996             }
997             if (controller->max_len != UINT16_MAX) {
998                 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
999             }
1000             if (controller->controller_id != 0) {
1001                 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1002             }
1003             ds_chomp(s, ',');
1004             ds_put_char(s, ')');
1005         }
1006         break;
1007
1008     case OFPACT_ENQUEUE:
1009         enqueue = ofpact_get_ENQUEUE(a);
1010         ds_put_format(s, "enqueue:");
1011         ofputil_format_port(enqueue->port, s);
1012         ds_put_format(s, "q%"PRIu32, enqueue->queue);
1013         break;
1014
1015     case OFPACT_OUTPUT_REG:
1016         ds_put_cstr(s, "output:");
1017         mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1018         break;
1019
1020     case OFPACT_BUNDLE:
1021         bundle_format(ofpact_get_BUNDLE(a), s);
1022         break;
1023
1024     case OFPACT_SET_VLAN_VID:
1025         ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1026                       ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1027         break;
1028
1029     case OFPACT_SET_VLAN_PCP:
1030         ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1031                       ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1032         break;
1033
1034     case OFPACT_STRIP_VLAN:
1035         ds_put_cstr(s, "strip_vlan");
1036         break;
1037
1038     case OFPACT_SET_ETH_SRC:
1039         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1040                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1041         break;
1042
1043     case OFPACT_SET_ETH_DST:
1044         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1045                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1046         break;
1047
1048     case OFPACT_SET_IPV4_SRC:
1049         ds_put_format(s, "mod_nw_src:"IP_FMT,
1050                       IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1051         break;
1052
1053     case OFPACT_SET_IPV4_DST:
1054         ds_put_format(s, "mod_nw_dst:"IP_FMT,
1055                       IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1056         break;
1057
1058     case OFPACT_SET_IPV4_DSCP:
1059         ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1060         break;
1061
1062     case OFPACT_SET_L4_SRC_PORT:
1063         ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1064         break;
1065
1066     case OFPACT_SET_L4_DST_PORT:
1067         ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1068         break;
1069
1070     case OFPACT_REG_MOVE:
1071         nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1072         break;
1073
1074     case OFPACT_REG_LOAD:
1075         nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1076         break;
1077
1078     case OFPACT_DEC_TTL:
1079         ds_put_cstr(s, "dec_ttl");
1080         break;
1081
1082     case OFPACT_SET_TUNNEL:
1083         tunnel = ofpact_get_SET_TUNNEL(a);
1084         ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1085                       (tunnel->tun_id > UINT32_MAX
1086                        || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1087                       tunnel->tun_id);
1088         break;
1089
1090     case OFPACT_SET_QUEUE:
1091         ds_put_format(s, "set_queue:%"PRIu32,
1092                       ofpact_get_SET_QUEUE(a)->queue_id);
1093         break;
1094
1095     case OFPACT_POP_QUEUE:
1096         ds_put_cstr(s, "pop_queue");
1097         break;
1098
1099     case OFPACT_FIN_TIMEOUT:
1100         print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1101         break;
1102
1103     case OFPACT_RESUBMIT:
1104         resubmit = ofpact_get_RESUBMIT(a);
1105         if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
1106             ds_put_format(s, "resubmit:%"PRIu16, resubmit->in_port);
1107         } else {
1108             ds_put_format(s, "resubmit(");
1109             if (resubmit->in_port != OFPP_IN_PORT) {
1110                 ofputil_format_port(resubmit->in_port, s);
1111             }
1112             ds_put_char(s, ',');
1113             if (resubmit->table_id != 255) {
1114                 ds_put_format(s, "%"PRIu8, resubmit->table_id);
1115             }
1116             ds_put_char(s, ')');
1117         }
1118         break;
1119
1120     case OFPACT_LEARN:
1121         learn_format(ofpact_get_LEARN(a), s);
1122         break;
1123
1124     case OFPACT_MULTIPATH:
1125         multipath_format(ofpact_get_MULTIPATH(a), s);
1126         break;
1127
1128     case OFPACT_AUTOPATH:
1129         autopath = ofpact_get_AUTOPATH(a);
1130         ds_put_format(s, "autopath(%u,", autopath->port);
1131         mf_format_subfield(&autopath->dst, s);
1132         ds_put_char(s, ')');
1133         break;
1134
1135     case OFPACT_NOTE:
1136         print_note(ofpact_get_NOTE(a), s);
1137         break;
1138
1139     case OFPACT_EXIT:
1140         ds_put_cstr(s, "exit");
1141         break;
1142     }
1143 }
1144
1145 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
1146  * 'ofpacts' to 'string'. */
1147 void
1148 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
1149                struct ds *string)
1150 {
1151     ds_put_cstr(string, "actions=");
1152     if (!ofpacts_len) {
1153         ds_put_cstr(string, "drop");
1154     } else {
1155         const struct ofpact *a;
1156
1157         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1158             if (a != ofpacts) {
1159                 ds_put_cstr(string, ",");
1160             }
1161             ofpact_format(a, string);
1162         }
1163     }
1164 }
1165 \f
1166 /* Internal use by helpers. */
1167
1168 void *
1169 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
1170 {
1171     struct ofpact *ofpact;
1172
1173     ofpact_pad(ofpacts);
1174     ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
1175     ofpact_init(ofpact, type, len);
1176     return ofpact;
1177 }
1178
1179 void
1180 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
1181 {
1182     memset(ofpact, 0, len);
1183     ofpact->type = type;
1184     ofpact->compat = OFPUTIL_ACTION_INVALID;
1185     ofpact->len = len;
1186 }
1187 \f
1188 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
1189  * starting at 'ofpact'.
1190  *
1191  * This is the correct way to update a variable-length ofpact's length after
1192  * adding the variable-length part of the payload.  (See the large comment
1193  * near the end of ofp-actions.h for more information.) */
1194 void
1195 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
1196 {
1197     assert(ofpact == ofpacts->l2);
1198     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
1199 }
1200
1201 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
1202  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
1203  * client must call this itself after adding the final ofpact to an array of
1204  * them.
1205  *
1206  * (The consequences of failing to call this function are probably not dire.
1207  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
1208  * not dereference it.  That's undefined behavior, technically, but it will not
1209  * cause a real problem on common systems.  Still, it seems better to call
1210  * it.) */
1211 void
1212 ofpact_pad(struct ofpbuf *ofpacts)
1213 {
1214     unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
1215     if (rem) {
1216         ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
1217     }
1218 }