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