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