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