User-Space MPLS actions and matches
[sliver-openvswitch.git] / lib / ofp-actions.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofp-actions.h"
19 #include "autopath.h"
20 #include "bundle.h"
21 #include "byte-order.h"
22 #include "compiler.h"
23 #include "dynamic-string.h"
24 #include "learn.h"
25 #include "meta-flow.h"
26 #include "multipath.h"
27 #include "nx-match.h"
28 #include "ofp-util.h"
29 #include "ofpbuf.h"
30 #include "util.h"
31 #include "vlog.h"
32
33 VLOG_DEFINE_THIS_MODULE(ofp_actions);
34
35 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
36 \f
37 /* Converting OpenFlow 1.0 to ofpacts. */
38
39 static enum ofperr
40 output_from_openflow10(const struct ofp10_action_output *oao,
41                        struct ofpbuf *out)
42 {
43     struct ofpact_output *output;
44
45     output = ofpact_put_OUTPUT(out);
46     output->port = ntohs(oao->port);
47     output->max_len = ntohs(oao->max_len);
48
49     return ofputil_check_output_port(output->port, OFPP_MAX);
50 }
51
52 static enum ofperr
53 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
54                         struct ofpbuf *out)
55 {
56     struct ofpact_enqueue *enqueue;
57
58     enqueue = ofpact_put_ENQUEUE(out);
59     enqueue->port = ntohs(oae->port);
60     enqueue->queue = ntohl(oae->queue_id);
61     if (enqueue->port >= OFPP_MAX && enqueue->port != OFPP_IN_PORT
62         && enqueue->port != OFPP_LOCAL) {
63         return OFPERR_OFPBAC_BAD_OUT_PORT;
64     }
65     return 0;
66 }
67
68 static void
69 resubmit_from_openflow(const struct nx_action_resubmit *nar,
70                        struct ofpbuf *out)
71 {
72     struct ofpact_resubmit *resubmit;
73
74     resubmit = ofpact_put_RESUBMIT(out);
75     resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
76     resubmit->in_port = ntohs(nar->in_port);
77     resubmit->table_id = 0xff;
78 }
79
80 static enum ofperr
81 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
82                              struct ofpbuf *out)
83 {
84     struct ofpact_resubmit *resubmit;
85
86     if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
87         return OFPERR_OFPBAC_BAD_ARGUMENT;
88     }
89
90     resubmit = ofpact_put_RESUBMIT(out);
91     resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
92     resubmit->in_port = ntohs(nar->in_port);
93     resubmit->table_id = nar->table;
94     return 0;
95 }
96
97 static enum ofperr
98 output_reg_from_openflow(const struct nx_action_output_reg *naor,
99                          struct ofpbuf *out)
100 {
101     struct ofpact_output_reg *output_reg;
102
103     if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
104         return OFPERR_OFPBAC_BAD_ARGUMENT;
105     }
106
107     output_reg = ofpact_put_OUTPUT_REG(out);
108     output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
109     output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
110     output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
111     output_reg->max_len = ntohs(naor->max_len);
112
113     return mf_check_src(&output_reg->src, NULL);
114 }
115
116 static void
117 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
118                           struct ofpbuf *out)
119 {
120     struct ofpact_fin_timeout *oft;
121
122     oft = ofpact_put_FIN_TIMEOUT(out);
123     oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
124     oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
125 }
126
127 static void
128 controller_from_openflow(const struct nx_action_controller *nac,
129                          struct ofpbuf *out)
130 {
131     struct ofpact_controller *oc;
132
133     oc = ofpact_put_CONTROLLER(out);
134     oc->max_len = ntohs(nac->max_len);
135     oc->controller_id = ntohs(nac->controller_id);
136     oc->reason = nac->reason;
137 }
138
139 static enum ofperr
140 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
141                     struct ofpbuf *out)
142 {
143     struct ofpact_metadata *om;
144
145     if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
146         return OFPERR_NXBRC_MUST_BE_ZERO;
147     }
148
149     om = ofpact_put_WRITE_METADATA(out);
150     om->metadata = nawm->metadata;
151     om->mask = nawm->mask;
152
153     return 0;
154 }
155
156 static void
157 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
158 {
159     struct ofpact_note *note;
160     unsigned int length;
161
162     length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
163     note = ofpact_put(out, OFPACT_NOTE,
164                       offsetof(struct ofpact_note, data) + length);
165     note->length = length;
166     memcpy(note->data, nan->note, length);
167 }
168
169 static enum ofperr
170 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
171 {
172     uint16_t id = 0;
173     struct ofpact_cnt_ids *ids;
174     enum ofperr error = 0;
175
176     ids = ofpact_put_DEC_TTL(out);
177     ids->ofpact.compat = compat;
178     ids->n_controllers = 1;
179     ofpbuf_put(out, &id, sizeof id);
180     ids = out->l2;
181     ofpact_update_len(out, &ids->ofpact);
182     return error;
183 }
184
185 static enum ofperr
186 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
187                       struct ofpbuf *out)
188 {
189     struct ofpact_cnt_ids *ids;
190     size_t ids_size;
191     int i;
192
193     ids = ofpact_put_DEC_TTL(out);
194     ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
195     ids->n_controllers = ntohs(nac_ids->n_controllers);
196     ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
197
198     if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
199         return OFPERR_NXBRC_MUST_BE_ZERO;
200     }
201
202     if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
203         VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
204                      "allocated for controller ids.  %zu bytes are required for "
205                      "%"PRIu16" controllers.", ids_size,
206                      ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
207         return OFPERR_OFPBAC_BAD_LEN;
208     }
209
210     for (i = 0; i < ids->n_controllers; i++) {
211         uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
212         ofpbuf_put(out, &id, sizeof id);
213     }
214
215     ids = out->l2;
216     ofpact_update_len(out, &ids->ofpact);
217
218     return 0;
219 }
220
221 static enum ofperr
222 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
223 {
224     const struct nx_action_header *nah = (const struct nx_action_header *) a;
225     uint16_t len = ntohs(a->header.len);
226
227     if (len < sizeof(struct nx_action_header)) {
228         return OFPERR_OFPBAC_BAD_LEN;
229     } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
230         return OFPERR_OFPBAC_BAD_VENDOR;
231     }
232
233     switch (nah->subtype) {
234 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
235         case CONSTANT_HTONS(ENUM):                      \
236             if (EXTENSIBLE                              \
237                 ? len >= sizeof(struct STRUCT)          \
238                 : len == sizeof(struct STRUCT)) {       \
239                 *code = OFPUTIL_##ENUM;                 \
240                 return 0;                               \
241             } else {                                    \
242                 return OFPERR_OFPBAC_BAD_LEN;           \
243             }                                           \
244             NOT_REACHED();
245 #include "ofp-util.def"
246
247     case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
248     case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
249     default:
250         return OFPERR_OFPBAC_BAD_TYPE;
251     }
252 }
253
254 /* Parses 'a' to determine its type.  On success stores the correct type into
255  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
256  * '*code' is indeterminate.
257  *
258  * The caller must have already verified that 'a''s length is potentially
259  * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
260  * ofp_action) and no longer than the amount of space allocated to 'a').
261  *
262  * This function verifies that 'a''s length is correct for the type of action
263  * that it represents. */
264 static enum ofperr
265 decode_openflow10_action(const union ofp_action *a,
266                          enum ofputil_action_code *code)
267 {
268     switch (a->type) {
269     case CONSTANT_HTONS(OFPAT10_VENDOR):
270         return decode_nxast_action(a, code);
271
272 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)                          \
273         case CONSTANT_HTONS(ENUM):                                  \
274             if (a->header.len == htons(sizeof(struct STRUCT))) {    \
275                 *code = OFPUTIL_##ENUM;                             \
276                 return 0;                                           \
277             } else {                                                \
278                 return OFPERR_OFPBAC_BAD_LEN;                       \
279             }                                                       \
280             break;
281 #include "ofp-util.def"
282
283     default:
284         return OFPERR_OFPBAC_BAD_TYPE;
285     }
286 }
287
288 static enum ofperr
289 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
290                   struct ofpbuf *out)
291 {
292     const struct nx_action_resubmit *nar;
293     const struct nx_action_set_tunnel *nast;
294     const struct nx_action_set_queue *nasq;
295     const struct nx_action_note *nan;
296     const struct nx_action_set_tunnel64 *nast64;
297     const struct nx_action_write_metadata *nawm;
298     struct ofpact_tunnel *tunnel;
299     enum ofperr error = 0;
300
301     switch (code) {
302     case OFPUTIL_ACTION_INVALID:
303 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
304 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
305 #include "ofp-util.def"
306         NOT_REACHED();
307
308     case OFPUTIL_NXAST_RESUBMIT:
309         resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
310         break;
311
312     case OFPUTIL_NXAST_SET_TUNNEL:
313         nast = (const struct nx_action_set_tunnel *) a;
314         tunnel = ofpact_put_SET_TUNNEL(out);
315         tunnel->ofpact.compat = code;
316         tunnel->tun_id = ntohl(nast->tun_id);
317         break;
318
319     case OFPUTIL_NXAST_WRITE_METADATA:
320         nawm = (const struct nx_action_write_metadata *) a;
321         error = metadata_from_nxast(nawm, out);
322         break;
323
324     case OFPUTIL_NXAST_SET_QUEUE:
325         nasq = (const struct nx_action_set_queue *) a;
326         ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
327         break;
328
329     case OFPUTIL_NXAST_POP_QUEUE:
330         ofpact_put_POP_QUEUE(out);
331         break;
332
333     case OFPUTIL_NXAST_REG_MOVE:
334         error = nxm_reg_move_from_openflow(
335             (const struct nx_action_reg_move *) a, out);
336         break;
337
338     case OFPUTIL_NXAST_REG_LOAD:
339         error = nxm_reg_load_from_openflow(
340             (const struct nx_action_reg_load *) a, out);
341         break;
342
343     case OFPUTIL_NXAST_NOTE:
344         nan = (const struct nx_action_note *) a;
345         note_from_openflow(nan, out);
346         break;
347
348     case OFPUTIL_NXAST_SET_TUNNEL64:
349         nast64 = (const struct nx_action_set_tunnel64 *) a;
350         tunnel = ofpact_put_SET_TUNNEL(out);
351         tunnel->ofpact.compat = code;
352         tunnel->tun_id = ntohll(nast64->tun_id);
353         break;
354
355     case OFPUTIL_NXAST_MULTIPATH:
356         error = multipath_from_openflow((const struct nx_action_multipath *) a,
357                                         ofpact_put_MULTIPATH(out));
358         break;
359
360     case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
361         error = autopath_from_openflow((const struct nx_action_autopath *) a,
362                                        ofpact_put_AUTOPATH(out));
363         break;
364
365     case OFPUTIL_NXAST_BUNDLE:
366     case OFPUTIL_NXAST_BUNDLE_LOAD:
367         error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
368         break;
369
370     case OFPUTIL_NXAST_OUTPUT_REG:
371         error = output_reg_from_openflow(
372             (const struct nx_action_output_reg *) a, out);
373         break;
374
375     case OFPUTIL_NXAST_RESUBMIT_TABLE:
376         nar = (const struct nx_action_resubmit *) a;
377         error = resubmit_table_from_openflow(nar, out);
378         break;
379
380     case OFPUTIL_NXAST_LEARN:
381         error = learn_from_openflow((const struct nx_action_learn *) a, out);
382         break;
383
384     case OFPUTIL_NXAST_EXIT:
385         ofpact_put_EXIT(out);
386         break;
387
388     case OFPUTIL_NXAST_DEC_TTL:
389         error = dec_ttl_from_openflow(out, code);
390         break;
391
392     case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
393         error = dec_ttl_cnt_ids_from_openflow(
394                     (const struct nx_action_cnt_ids *) a, out);
395         break;
396
397     case OFPUTIL_NXAST_FIN_TIMEOUT:
398         fin_timeout_from_openflow(
399             (const struct nx_action_fin_timeout *) a, out);
400         break;
401
402     case OFPUTIL_NXAST_CONTROLLER:
403         controller_from_openflow((const struct nx_action_controller *) a, out);
404         break;
405
406     case OFPUTIL_NXAST_PUSH_MPLS: {
407         struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
408         if (!eth_type_mpls(nxapm->ethertype)) {
409             return OFPERR_OFPBAC_BAD_ARGUMENT;
410         }
411         ofpact_put_PUSH_MPLS(out)->ethertype = nxapm->ethertype;
412         break;
413     }
414
415     case OFPUTIL_NXAST_POP_MPLS: {
416         struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
417         if (eth_type_mpls(nxapm->ethertype)) {
418             return OFPERR_OFPBAC_BAD_ARGUMENT;
419         }
420         ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
421         break;
422     }
423     }
424
425     return error;
426 }
427
428 static enum ofperr
429 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
430 {
431     enum ofputil_action_code code;
432     enum ofperr error;
433
434     error = decode_openflow10_action(a, &code);
435     if (error) {
436         return error;
437     }
438
439     switch (code) {
440     case OFPUTIL_ACTION_INVALID:
441 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
442 #include "ofp-util.def"
443         NOT_REACHED();
444
445     case OFPUTIL_OFPAT10_OUTPUT:
446         return output_from_openflow10(&a->output10, out);
447
448     case OFPUTIL_OFPAT10_SET_VLAN_VID:
449         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
450             return OFPERR_OFPBAC_BAD_ARGUMENT;
451         }
452         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
453         break;
454
455     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
456         if (a->vlan_pcp.vlan_pcp & ~7) {
457             return OFPERR_OFPBAC_BAD_ARGUMENT;
458         }
459         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
460         break;
461
462     case OFPUTIL_OFPAT10_STRIP_VLAN:
463         ofpact_put_STRIP_VLAN(out);
464         break;
465
466     case OFPUTIL_OFPAT10_SET_DL_SRC:
467         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
468                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
469         break;
470
471     case OFPUTIL_OFPAT10_SET_DL_DST:
472         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
473                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
474         break;
475
476     case OFPUTIL_OFPAT10_SET_NW_SRC:
477         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
478         break;
479
480     case OFPUTIL_OFPAT10_SET_NW_DST:
481         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
482         break;
483
484     case OFPUTIL_OFPAT10_SET_NW_TOS:
485         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
486             return OFPERR_OFPBAC_BAD_ARGUMENT;
487         }
488         ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
489         break;
490
491     case OFPUTIL_OFPAT10_SET_TP_SRC:
492         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
493         break;
494
495     case OFPUTIL_OFPAT10_SET_TP_DST:
496         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
497
498         break;
499
500     case OFPUTIL_OFPAT10_ENQUEUE:
501         error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
502                                         out);
503         break;
504
505 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
506 #include "ofp-util.def"
507         return ofpact_from_nxast(a, code, out);
508     }
509
510     return error;
511 }
512
513 static inline union ofp_action *
514 action_next(const union ofp_action *a)
515 {
516     return ((union ofp_action *) (void *)
517             ((uint8_t *) a + ntohs(a->header.len)));
518 }
519
520 static inline bool
521 action_is_valid(const union ofp_action *a, size_t n_actions)
522 {
523     uint16_t len = ntohs(a->header.len);
524     return (!(len % OFP_ACTION_ALIGN)
525             && len >= sizeof *a
526             && len / sizeof *a <= n_actions);
527 }
528
529 /* This macro is careful to check for actions with bad lengths. */
530 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS)                 \
531     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
532          (LEFT) > 0 && action_is_valid(ITER, LEFT);                     \
533          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
534           (ITER) = action_next(ITER)))
535
536 static void
537 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
538                enum ofperr error)
539 {
540     if (!VLOG_DROP_WARN(&rl)) {
541         struct ds s;
542
543         ds_init(&s);
544         ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
545         VLOG_WARN("bad action at offset %#zx (%s):\n%s",
546                   ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
547         ds_destroy(&s);
548     }
549 }
550
551 static enum ofperr
552 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
553                       struct ofpbuf *out,
554                       enum ofperr (*ofpact_from_openflow)(
555                           const union ofp_action *a, struct ofpbuf *out))
556 {
557     const union ofp_action *a;
558     size_t left;
559
560     ACTION_FOR_EACH (a, left, in, n_in) {
561         enum ofperr error = ofpact_from_openflow(a, out);
562         if (error) {
563             log_bad_action(in, n_in, a - in, error);
564             return error;
565         }
566     }
567     if (left) {
568         enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
569         log_bad_action(in, n_in, n_in - left, error);
570         return error;
571     }
572
573     ofpact_pad(out);
574     return 0;
575 }
576
577 static enum ofperr
578 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
579                         struct ofpbuf *out)
580 {
581     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
582 }
583
584 static enum ofperr
585 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
586                      struct ofpbuf *ofpacts,
587                      enum ofperr (*translate)(const union ofp_action *actions,
588                                               size_t n_actions,
589                                               struct ofpbuf *ofpacts))
590 {
591     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
592     const union ofp_action *actions;
593     enum ofperr error;
594
595     ofpbuf_clear(ofpacts);
596
597     if (actions_len % OFP_ACTION_ALIGN != 0) {
598         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
599                      "multiple of %d", actions_len, OFP_ACTION_ALIGN);
600         return OFPERR_OFPBRC_BAD_LEN;
601     }
602
603     actions = ofpbuf_try_pull(openflow, actions_len);
604     if (actions == NULL) {
605         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
606                      "remaining message length (%zu)",
607                      actions_len, openflow->size);
608         return OFPERR_OFPBRC_BAD_LEN;
609     }
610
611     error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
612     if (error) {
613         ofpbuf_clear(ofpacts);
614         return error;
615     }
616
617     error = ofpacts_verify(ofpacts->data, ofpacts->size);
618     if (error) {
619         ofpbuf_clear(ofpacts);
620     }
621     return error;
622 }
623
624 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
625  * front of 'openflow' into ofpacts.  On success, replaces any existing content
626  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
627  * Returns 0 if successful, otherwise an OpenFlow error.
628  *
629  * The parsed actions are valid generically, but they may not be valid in a
630  * specific context.  For example, port numbers up to OFPP_MAX are valid
631  * generically, but specific datapaths may only support port numbers in a
632  * smaller range.  Use ofpacts_check() to additional check whether actions are
633  * valid in a specific context. */
634 enum ofperr
635 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
636                         struct ofpbuf *ofpacts)
637 {
638     return ofpacts_pull_actions(openflow, actions_len, ofpacts,
639                                 ofpacts_from_openflow10);
640 }
641 \f
642 /* OpenFlow 1.1 actions. */
643
644 /* Parses 'a' to determine its type.  On success stores the correct type into
645  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
646  * '*code' is indeterminate.
647  *
648  * The caller must have already verified that 'a''s length is potentially
649  * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
650  * ofp_action) and no longer than the amount of space allocated to 'a').
651  *
652  * This function verifies that 'a''s length is correct for the type of action
653  * that it represents. */
654 static enum ofperr
655 decode_openflow11_action(const union ofp_action *a,
656                          enum ofputil_action_code *code)
657 {
658     uint16_t len;
659
660     switch (a->type) {
661     case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
662         return decode_nxast_action(a, code);
663
664 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
665         case CONSTANT_HTONS(ENUM):                      \
666             len = ntohs(a->header.len);                 \
667             if (EXTENSIBLE                              \
668                 ? len >= sizeof(struct STRUCT)          \
669                 : len == sizeof(struct STRUCT)) {       \
670                 *code = OFPUTIL_##ENUM;                 \
671                 return 0;                               \
672             } else {                                    \
673                 return OFPERR_OFPBAC_BAD_LEN;           \
674             }                                           \
675             NOT_REACHED();
676 #include "ofp-util.def"
677
678     default:
679         return OFPERR_OFPBAC_BAD_TYPE;
680     }
681 }
682
683 static enum ofperr
684 output_from_openflow11(const struct ofp11_action_output *oao,
685                        struct ofpbuf *out)
686 {
687     struct ofpact_output *output;
688     enum ofperr error;
689
690     output = ofpact_put_OUTPUT(out);
691     output->max_len = ntohs(oao->max_len);
692
693     error = ofputil_port_from_ofp11(oao->port, &output->port);
694     if (error) {
695         return error;
696     }
697
698     return ofputil_check_output_port(output->port, OFPP_MAX);
699 }
700
701 static enum ofperr
702 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
703 {
704     enum ofputil_action_code code;
705     enum ofperr error;
706
707     error = decode_openflow11_action(a, &code);
708     if (error) {
709         return error;
710     }
711
712     switch (code) {
713     case OFPUTIL_ACTION_INVALID:
714 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
715 #include "ofp-util.def"
716         NOT_REACHED();
717
718     case OFPUTIL_OFPAT11_OUTPUT:
719         return output_from_openflow11((const struct ofp11_action_output *) a,
720                                       out);
721
722     case OFPUTIL_OFPAT11_SET_VLAN_VID:
723         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
724             return OFPERR_OFPBAC_BAD_ARGUMENT;
725         }
726         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
727         break;
728
729     case OFPUTIL_OFPAT11_SET_VLAN_PCP:
730         if (a->vlan_pcp.vlan_pcp & ~7) {
731             return OFPERR_OFPBAC_BAD_ARGUMENT;
732         }
733         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
734         break;
735
736     case OFPUTIL_OFPAT11_PUSH_VLAN:
737         if (((const struct ofp11_action_push *)a)->ethertype !=
738             htons(ETH_TYPE_VLAN_8021Q)) {
739             /* XXX 802.1AD(QinQ) isn't supported at the moment */
740             return OFPERR_OFPBAC_BAD_ARGUMENT;
741         }
742         ofpact_put_PUSH_VLAN(out);
743         break;
744
745     case OFPUTIL_OFPAT11_POP_VLAN:
746         ofpact_put_STRIP_VLAN(out);
747         break;
748
749     case OFPUTIL_OFPAT11_SET_QUEUE:
750         ofpact_put_SET_QUEUE(out)->queue_id =
751             ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
752         break;
753
754     case OFPUTIL_OFPAT11_SET_DL_SRC:
755         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
756                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
757         break;
758
759     case OFPUTIL_OFPAT11_SET_DL_DST:
760         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
761                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
762         break;
763
764     case OFPUTIL_OFPAT11_DEC_NW_TTL:
765         dec_ttl_from_openflow(out, code);
766         break;
767
768     case OFPUTIL_OFPAT11_SET_NW_SRC:
769         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
770         break;
771
772     case OFPUTIL_OFPAT11_SET_NW_DST:
773         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
774         break;
775
776     case OFPUTIL_OFPAT11_SET_NW_TOS:
777         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
778             return OFPERR_OFPBAC_BAD_ARGUMENT;
779         }
780         ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
781         break;
782
783     case OFPUTIL_OFPAT11_SET_TP_SRC:
784         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
785         break;
786
787     case OFPUTIL_OFPAT11_SET_TP_DST:
788         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
789         break;
790
791     case OFPUTIL_OFPAT12_SET_FIELD:
792         return nxm_reg_load_from_openflow12_set_field(
793             (const struct ofp12_action_set_field *)a, out);
794
795     case OFPUTIL_OFPAT11_PUSH_MPLS: {
796         struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
797         if (!eth_type_mpls(oap->ethertype)) {
798             return OFPERR_OFPBAC_BAD_ARGUMENT;
799         }
800         ofpact_put_PUSH_MPLS(out)->ethertype = oap->ethertype;
801         break;
802     }
803
804     case OFPUTIL_OFPAT11_POP_MPLS: {
805         struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
806         if (eth_type_mpls(oapm->ethertype)) {
807             return OFPERR_OFPBAC_BAD_ARGUMENT;
808         }
809         ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
810         break;
811     }
812
813 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
814 #include "ofp-util.def"
815         return ofpact_from_nxast(a, code, out);
816     }
817
818     return error;
819 }
820
821 static enum ofperr
822 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
823                         struct ofpbuf *out)
824 {
825     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
826 }
827 \f
828 /* OpenFlow 1.1 instructions. */
829
830 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
831     static inline const struct STRUCT *                         \
832     instruction_get_##ENUM(const struct ofp11_instruction *inst)\
833     {                                                           \
834         ovs_assert(inst->type == htons(ENUM));                  \
835         return (struct STRUCT *)inst;                           \
836     }                                                           \
837                                                                 \
838     static inline void                                          \
839     instruction_init_##ENUM(struct STRUCT *s)                   \
840     {                                                           \
841         memset(s, 0, sizeof *s);                                \
842         s->type = htons(ENUM);                                  \
843         s->len = htons(sizeof *s);                              \
844     }                                                           \
845                                                                 \
846     static inline struct STRUCT *                               \
847     instruction_put_##ENUM(struct ofpbuf *buf)                  \
848     {                                                           \
849         struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
850         instruction_init_##ENUM(s);                             \
851         return s;                                               \
852     }
853 OVS_INSTRUCTIONS
854 #undef DEFINE_INST
855
856 struct instruction_type_info {
857     enum ovs_instruction_type type;
858     const char *name;
859 };
860
861 static const struct instruction_type_info inst_info[] = {
862 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
863 OVS_INSTRUCTIONS
864 #undef DEFINE_INST
865 };
866
867 const char *
868 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
869 {
870     return inst_info[type].name;
871 }
872
873 int
874 ofpact_instruction_type_from_name(const char *name)
875 {
876     const struct instruction_type_info *p;
877     for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
878         if (!strcasecmp(name, p->name)) {
879             return p->type;
880         }
881     }
882     return -1;
883 }
884
885 static inline struct ofp11_instruction *
886 instruction_next(const struct ofp11_instruction *inst)
887 {
888     return ((struct ofp11_instruction *) (void *)
889             ((uint8_t *) inst + ntohs(inst->len)));
890 }
891
892 static inline bool
893 instruction_is_valid(const struct ofp11_instruction *inst,
894                      size_t n_instructions)
895 {
896     uint16_t len = ntohs(inst->len);
897     return (!(len % OFP11_INSTRUCTION_ALIGN)
898             && len >= sizeof *inst
899             && len / sizeof *inst <= n_instructions);
900 }
901
902 /* This macro is careful to check for instructions with bad lengths. */
903 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS)  \
904     for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS);            \
905          (LEFT) > 0 && instruction_is_valid(ITER, LEFT);                \
906          ((LEFT) -= (ntohs((ITER)->len)                                 \
907                      / sizeof(struct ofp11_instruction)),               \
908           (ITER) = instruction_next(ITER)))
909
910 static enum ofperr
911 decode_openflow11_instruction(const struct ofp11_instruction *inst,
912                               enum ovs_instruction_type *type)
913 {
914     uint16_t len = ntohs(inst->len);
915
916     switch (inst->type) {
917     case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
918         return OFPERR_OFPBIC_BAD_EXPERIMENTER;
919
920 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)     \
921         case CONSTANT_HTONS(ENUM):                      \
922             if (EXTENSIBLE                              \
923                 ? len >= sizeof(struct STRUCT)          \
924                 : len == sizeof(struct STRUCT)) {       \
925                 *type = OVSINST_##ENUM;                 \
926                 return 0;                               \
927             } else {                                    \
928                 return OFPERR_OFPBIC_BAD_LEN;           \
929             }
930 OVS_INSTRUCTIONS
931 #undef DEFINE_INST
932
933     default:
934         return OFPERR_OFPBIC_UNKNOWN_INST;
935     }
936 }
937
938 static enum ofperr
939 decode_openflow11_instructions(const struct ofp11_instruction insts[],
940                                size_t n_insts,
941                                const struct ofp11_instruction *out[])
942 {
943     const struct ofp11_instruction *inst;
944     size_t left;
945
946     memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
947     INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
948         enum ovs_instruction_type type;
949         enum ofperr error;
950
951         error = decode_openflow11_instruction(inst, &type);
952         if (error) {
953             return error;
954         }
955
956         if (out[type]) {
957             return OFPERR_OFPBAC_UNSUPPORTED_ORDER; /* No specific code for
958                                                      * a duplicate instruction
959                                                      * exist */
960         }
961         out[type] = inst;
962     }
963
964     if (left) {
965         VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
966                      (n_insts - left) * sizeof *inst);
967         return OFPERR_OFPBIC_BAD_LEN;
968     }
969     return 0;
970 }
971
972 static void
973 get_actions_from_instruction(const struct ofp11_instruction *inst,
974                          const union ofp_action **actions,
975                          size_t *n_actions)
976 {
977     *actions = (const union ofp_action *) (inst + 1);
978     *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
979 }
980
981 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
982  * front of 'openflow' into ofpacts.  On success, replaces any existing content
983  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
984  * Returns 0 if successful, otherwise an OpenFlow error.
985  *
986  * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
987  * instructions, so you should call ofpacts_pull_openflow11_instructions()
988  * instead of this function.
989  *
990  * The parsed actions are valid generically, but they may not be valid in a
991  * specific context.  For example, port numbers up to OFPP_MAX are valid
992  * generically, but specific datapaths may only support port numbers in a
993  * smaller range.  Use ofpacts_check() to additional check whether actions are
994  * valid in a specific context. */
995 enum ofperr
996 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
997                                 unsigned int actions_len,
998                                 struct ofpbuf *ofpacts)
999 {
1000     return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1001                                 ofpacts_from_openflow11);
1002 }
1003
1004 enum ofperr
1005 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1006                                      unsigned int instructions_len,
1007                                      struct ofpbuf *ofpacts)
1008 {
1009     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1010     const struct ofp11_instruction *instructions;
1011     const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1012     enum ofperr error;
1013
1014     ofpbuf_clear(ofpacts);
1015
1016     if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1017         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1018                      "multiple of %d",
1019                      instructions_len, OFP11_INSTRUCTION_ALIGN);
1020         error = OFPERR_OFPBIC_BAD_LEN;
1021         goto exit;
1022     }
1023
1024     instructions = ofpbuf_try_pull(openflow, instructions_len);
1025     if (instructions == NULL) {
1026         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1027                      "remaining message length (%zu)",
1028                      instructions_len, openflow->size);
1029         error = OFPERR_OFPBIC_BAD_LEN;
1030         goto exit;
1031     }
1032
1033     error = decode_openflow11_instructions(
1034         instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1035         insts);
1036     if (error) {
1037         goto exit;
1038     }
1039
1040     if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1041         const union ofp_action *actions;
1042         size_t n_actions;
1043
1044         get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1045                                      &actions, &n_actions);
1046         error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1047         if (error) {
1048             goto exit;
1049         }
1050     }
1051     if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1052         instruction_get_OFPIT11_CLEAR_ACTIONS(
1053             insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1054         ofpact_put_CLEAR_ACTIONS(ofpacts);
1055     }
1056     /* XXX Write-Actions */
1057     if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1058         const struct ofp11_instruction_write_metadata *oiwm;
1059         struct ofpact_metadata *om;
1060
1061         oiwm = (const struct ofp11_instruction_write_metadata *)
1062             insts[OVSINST_OFPIT11_WRITE_METADATA];
1063
1064         om = ofpact_put_WRITE_METADATA(ofpacts);
1065         om->metadata = oiwm->metadata;
1066         om->mask = oiwm->metadata_mask;
1067     }
1068     if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1069         const struct ofp11_instruction_goto_table *oigt;
1070         struct ofpact_goto_table *ogt;
1071
1072         oigt = instruction_get_OFPIT11_GOTO_TABLE(
1073             insts[OVSINST_OFPIT11_GOTO_TABLE]);
1074         ogt = ofpact_put_GOTO_TABLE(ofpacts);
1075         ogt->table_id = oigt->table_id;
1076     }
1077
1078     if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1079         error = OFPERR_OFPBIC_UNSUP_INST;
1080         goto exit;
1081     }
1082
1083     error = ofpacts_verify(ofpacts->data, ofpacts->size);
1084 exit:
1085     if (error) {
1086         ofpbuf_clear(ofpacts);
1087     }
1088     return error;
1089 }
1090 \f
1091 static enum ofperr
1092 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports,
1093                ovs_be16 *dl_type)
1094 {
1095     const struct ofpact_enqueue *enqueue;
1096
1097     switch (a->type) {
1098     case OFPACT_OUTPUT:
1099         return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1100                                          max_ports);
1101
1102     case OFPACT_CONTROLLER:
1103         return 0;
1104
1105     case OFPACT_ENQUEUE:
1106         enqueue = ofpact_get_ENQUEUE(a);
1107         if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
1108             && enqueue->port != OFPP_LOCAL) {
1109             return OFPERR_OFPBAC_BAD_OUT_PORT;
1110         }
1111         return 0;
1112
1113     case OFPACT_OUTPUT_REG:
1114         return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1115
1116     case OFPACT_BUNDLE:
1117         return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1118
1119     case OFPACT_SET_VLAN_VID:
1120     case OFPACT_SET_VLAN_PCP:
1121     case OFPACT_STRIP_VLAN:
1122     case OFPACT_PUSH_VLAN:
1123     case OFPACT_SET_ETH_SRC:
1124     case OFPACT_SET_ETH_DST:
1125     case OFPACT_SET_IPV4_SRC:
1126     case OFPACT_SET_IPV4_DST:
1127     case OFPACT_SET_IPV4_DSCP:
1128     case OFPACT_SET_L4_SRC_PORT:
1129     case OFPACT_SET_L4_DST_PORT:
1130         return 0;
1131
1132     case OFPACT_REG_MOVE:
1133         return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1134
1135     case OFPACT_REG_LOAD:
1136         if (*dl_type != flow->dl_type) {
1137             struct flow updated_flow = *flow;
1138             updated_flow.dl_type = *dl_type;
1139             return nxm_reg_load_check(ofpact_get_REG_LOAD(a), &updated_flow);
1140         } else {
1141             return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1142         }
1143
1144     case OFPACT_DEC_TTL:
1145     case OFPACT_SET_TUNNEL:
1146     case OFPACT_SET_QUEUE:
1147     case OFPACT_POP_QUEUE:
1148     case OFPACT_FIN_TIMEOUT:
1149     case OFPACT_RESUBMIT:
1150         return 0;
1151
1152     case OFPACT_LEARN:
1153         return learn_check(ofpact_get_LEARN(a), flow);
1154
1155     case OFPACT_MULTIPATH:
1156         return multipath_check(ofpact_get_MULTIPATH(a), flow);
1157
1158     case OFPACT_AUTOPATH:
1159         return autopath_check(ofpact_get_AUTOPATH(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_SET_TUNNEL:
1396         ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1397         break;
1398
1399     case OFPACT_WRITE_METADATA:
1400         ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1401         break;
1402
1403     case OFPACT_SET_QUEUE:
1404         ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1405             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1406         break;
1407
1408     case OFPACT_POP_QUEUE:
1409         ofputil_put_NXAST_POP_QUEUE(out);
1410         break;
1411
1412     case OFPACT_FIN_TIMEOUT:
1413         ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1414         break;
1415
1416     case OFPACT_RESUBMIT:
1417         ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1418         break;
1419
1420     case OFPACT_LEARN:
1421         learn_to_nxast(ofpact_get_LEARN(a), out);
1422         break;
1423
1424     case OFPACT_MULTIPATH:
1425         multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1426         break;
1427
1428     case OFPACT_AUTOPATH:
1429         autopath_to_nxast(ofpact_get_AUTOPATH(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_SET_TUNNEL:
1567     case OFPACT_WRITE_METADATA:
1568     case OFPACT_SET_QUEUE:
1569     case OFPACT_POP_QUEUE:
1570     case OFPACT_FIN_TIMEOUT:
1571     case OFPACT_RESUBMIT:
1572     case OFPACT_LEARN:
1573     case OFPACT_MULTIPATH:
1574     case OFPACT_AUTOPATH:
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_WRITE_METADATA:
1700         /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
1701         break;
1702
1703     case OFPACT_PUSH_MPLS:
1704         ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
1705             ofpact_get_PUSH_MPLS(a)->ethertype;
1706         break;
1707
1708     case OFPACT_POP_MPLS:
1709         ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
1710             ofpact_get_POP_MPLS(a)->ethertype;
1711
1712         break;
1713
1714     case OFPACT_CLEAR_ACTIONS:
1715     case OFPACT_GOTO_TABLE:
1716         NOT_REACHED();
1717
1718     case OFPACT_CONTROLLER:
1719     case OFPACT_OUTPUT_REG:
1720     case OFPACT_BUNDLE:
1721     case OFPACT_REG_MOVE:
1722     case OFPACT_REG_LOAD:
1723     case OFPACT_SET_TUNNEL:
1724     case OFPACT_POP_QUEUE:
1725     case OFPACT_FIN_TIMEOUT:
1726     case OFPACT_RESUBMIT:
1727     case OFPACT_LEARN:
1728     case OFPACT_MULTIPATH:
1729     case OFPACT_AUTOPATH:
1730     case OFPACT_NOTE:
1731     case OFPACT_EXIT:
1732         ofpact_to_nxast(a, out);
1733         break;
1734     }
1735 }
1736
1737 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1738  * 1.1 actions in 'openflow', appending the actions to any existing data in
1739  * 'openflow'. */
1740 size_t
1741 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1742                                size_t ofpacts_len, struct ofpbuf *openflow)
1743 {
1744     const struct ofpact *a;
1745     size_t start_size = openflow->size;
1746
1747     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1748         ofpact_to_openflow11(a, openflow);
1749     }
1750
1751     return openflow->size - start_size;
1752 }
1753
1754 static void
1755 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
1756 {
1757     struct ofp11_instruction_actions *oia;
1758
1759     /* Update the instruction's length (or, if it's empty, delete it). */
1760     oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1761     if (openflow->size > ofs + sizeof *oia) {
1762         oia->len = htons(openflow->size - ofs);
1763     } else {
1764         openflow->size = ofs;
1765     }
1766 }
1767
1768 void
1769 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1770                                     size_t ofpacts_len,
1771                                     struct ofpbuf *openflow)
1772 {
1773     const struct ofpact *a;
1774
1775     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1776         /* XXX Write-Actions */
1777
1778         if (a->type == OFPACT_CLEAR_ACTIONS) {
1779             instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
1780         } else if (a->type == OFPACT_GOTO_TABLE) {
1781             struct ofp11_instruction_goto_table *oigt;
1782
1783             oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1784             oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1785             memset(oigt->pad, 0, sizeof oigt->pad);
1786         } else if (a->type == OFPACT_WRITE_METADATA) {
1787             const struct ofpact_metadata *om;
1788             struct ofp11_instruction_write_metadata *oiwm;
1789
1790             om = ofpact_get_WRITE_METADATA(a);
1791             oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
1792             oiwm->metadata = om->metadata;
1793             oiwm->metadata_mask = om->mask;
1794         } else if (!ofpact_is_instruction(a)) {
1795             /* Apply-actions */
1796             const size_t ofs = openflow->size;
1797             const size_t ofpacts_len_left =
1798                 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1799             const struct ofpact *action;
1800             const struct ofpact *processed = a;
1801
1802             instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1803             OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1804                 if (ofpact_is_instruction(action)) {
1805                     break;
1806                 }
1807                 ofpact_to_openflow11(action, openflow);
1808                 processed = action;
1809             }
1810             ofpacts_update_instruction_actions(openflow, ofs);
1811             a = processed;
1812         }
1813     }
1814 }
1815 \f
1816 /* Returns true if 'action' outputs to 'port', false otherwise. */
1817 static bool
1818 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1819 {
1820     switch (ofpact->type) {
1821     case OFPACT_OUTPUT:
1822         return ofpact_get_OUTPUT(ofpact)->port == port;
1823     case OFPACT_ENQUEUE:
1824         return ofpact_get_ENQUEUE(ofpact)->port == port;
1825     case OFPACT_CONTROLLER:
1826         return port == OFPP_CONTROLLER;
1827
1828     case OFPACT_OUTPUT_REG:
1829     case OFPACT_BUNDLE:
1830     case OFPACT_SET_VLAN_VID:
1831     case OFPACT_SET_VLAN_PCP:
1832     case OFPACT_STRIP_VLAN:
1833     case OFPACT_PUSH_VLAN:
1834     case OFPACT_SET_ETH_SRC:
1835     case OFPACT_SET_ETH_DST:
1836     case OFPACT_SET_IPV4_SRC:
1837     case OFPACT_SET_IPV4_DST:
1838     case OFPACT_SET_IPV4_DSCP:
1839     case OFPACT_SET_L4_SRC_PORT:
1840     case OFPACT_SET_L4_DST_PORT:
1841     case OFPACT_REG_MOVE:
1842     case OFPACT_REG_LOAD:
1843     case OFPACT_DEC_TTL:
1844     case OFPACT_SET_TUNNEL:
1845     case OFPACT_WRITE_METADATA:
1846     case OFPACT_SET_QUEUE:
1847     case OFPACT_POP_QUEUE:
1848     case OFPACT_FIN_TIMEOUT:
1849     case OFPACT_RESUBMIT:
1850     case OFPACT_LEARN:
1851     case OFPACT_MULTIPATH:
1852     case OFPACT_AUTOPATH:
1853     case OFPACT_NOTE:
1854     case OFPACT_EXIT:
1855     case OFPACT_PUSH_MPLS:
1856     case OFPACT_POP_MPLS:
1857     case OFPACT_CLEAR_ACTIONS:
1858     case OFPACT_GOTO_TABLE:
1859     default:
1860         return false;
1861     }
1862 }
1863
1864 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1865  * to 'port', false otherwise. */
1866 bool
1867 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1868                        uint16_t port)
1869 {
1870     const struct ofpact *a;
1871
1872     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1873         if (ofpact_outputs_to_port(a, port)) {
1874             return true;
1875         }
1876     }
1877
1878     return false;
1879 }
1880
1881 bool
1882 ofpacts_equal(const struct ofpact *a, size_t a_len,
1883               const struct ofpact *b, size_t b_len)
1884 {
1885     return a_len == b_len && !memcmp(a, b, a_len);
1886 }
1887 \f
1888 /* Formatting ofpacts. */
1889
1890 static void
1891 print_note(const struct ofpact_note *note, struct ds *string)
1892 {
1893     size_t i;
1894
1895     ds_put_cstr(string, "note:");
1896     for (i = 0; i < note->length; i++) {
1897         if (i) {
1898             ds_put_char(string, '.');
1899         }
1900         ds_put_format(string, "%02"PRIx8, note->data[i]);
1901     }
1902 }
1903
1904 static void
1905 print_dec_ttl(const struct ofpact_cnt_ids *ids,
1906               struct ds *s)
1907 {
1908     size_t i;
1909
1910     ds_put_cstr(s, "dec_ttl");
1911     if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1912         ds_put_cstr(s, "(");
1913         for (i = 0; i < ids->n_controllers; i++) {
1914             if (i) {
1915                 ds_put_cstr(s, ",");
1916             }
1917             ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1918         }
1919         ds_put_cstr(s, ")");
1920     }
1921 }
1922
1923 static void
1924 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1925                   struct ds *s)
1926 {
1927     ds_put_cstr(s, "fin_timeout(");
1928     if (fin_timeout->fin_idle_timeout) {
1929         ds_put_format(s, "idle_timeout=%"PRIu16",",
1930                       fin_timeout->fin_idle_timeout);
1931     }
1932     if (fin_timeout->fin_hard_timeout) {
1933         ds_put_format(s, "hard_timeout=%"PRIu16",",
1934                       fin_timeout->fin_hard_timeout);
1935     }
1936     ds_chomp(s, ',');
1937     ds_put_char(s, ')');
1938 }
1939
1940 static void
1941 ofpact_format(const struct ofpact *a, struct ds *s)
1942 {
1943     const struct ofpact_enqueue *enqueue;
1944     const struct ofpact_resubmit *resubmit;
1945     const struct ofpact_autopath *autopath;
1946     const struct ofpact_controller *controller;
1947     const struct ofpact_metadata *metadata;
1948     const struct ofpact_tunnel *tunnel;
1949     uint16_t port;
1950
1951     switch (a->type) {
1952     case OFPACT_OUTPUT:
1953         port = ofpact_get_OUTPUT(a)->port;
1954         if (port < OFPP_MAX) {
1955             ds_put_format(s, "output:%"PRIu16, port);
1956         } else {
1957             ofputil_format_port(port, s);
1958             if (port == OFPP_CONTROLLER) {
1959                 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1960             }
1961         }
1962         break;
1963
1964     case OFPACT_CONTROLLER:
1965         controller = ofpact_get_CONTROLLER(a);
1966         if (controller->reason == OFPR_ACTION &&
1967             controller->controller_id == 0) {
1968             ds_put_format(s, "CONTROLLER:%"PRIu16,
1969                           ofpact_get_CONTROLLER(a)->max_len);
1970         } else {
1971             enum ofp_packet_in_reason reason = controller->reason;
1972
1973             ds_put_cstr(s, "controller(");
1974             if (reason != OFPR_ACTION) {
1975                 ds_put_format(s, "reason=%s,",
1976                               ofputil_packet_in_reason_to_string(reason));
1977             }
1978             if (controller->max_len != UINT16_MAX) {
1979                 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1980             }
1981             if (controller->controller_id != 0) {
1982                 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1983             }
1984             ds_chomp(s, ',');
1985             ds_put_char(s, ')');
1986         }
1987         break;
1988
1989     case OFPACT_ENQUEUE:
1990         enqueue = ofpact_get_ENQUEUE(a);
1991         ds_put_format(s, "enqueue:");
1992         ofputil_format_port(enqueue->port, s);
1993         ds_put_format(s, "q%"PRIu32, enqueue->queue);
1994         break;
1995
1996     case OFPACT_OUTPUT_REG:
1997         ds_put_cstr(s, "output:");
1998         mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1999         break;
2000
2001     case OFPACT_BUNDLE:
2002         bundle_format(ofpact_get_BUNDLE(a), s);
2003         break;
2004
2005     case OFPACT_SET_VLAN_VID:
2006         ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2007                       ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2008         break;
2009
2010     case OFPACT_SET_VLAN_PCP:
2011         ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2012                       ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2013         break;
2014
2015     case OFPACT_STRIP_VLAN:
2016         ds_put_cstr(s, "strip_vlan");
2017         break;
2018
2019     case OFPACT_PUSH_VLAN:
2020         /* XXX 802.1AD case*/
2021         ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2022         break;
2023
2024     case OFPACT_SET_ETH_SRC:
2025         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2026                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2027         break;
2028
2029     case OFPACT_SET_ETH_DST:
2030         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2031                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2032         break;
2033
2034     case OFPACT_SET_IPV4_SRC:
2035         ds_put_format(s, "mod_nw_src:"IP_FMT,
2036                       IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2037         break;
2038
2039     case OFPACT_SET_IPV4_DST:
2040         ds_put_format(s, "mod_nw_dst:"IP_FMT,
2041                       IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2042         break;
2043
2044     case OFPACT_SET_IPV4_DSCP:
2045         ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2046         break;
2047
2048     case OFPACT_SET_L4_SRC_PORT:
2049         ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2050         break;
2051
2052     case OFPACT_SET_L4_DST_PORT:
2053         ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2054         break;
2055
2056     case OFPACT_REG_MOVE:
2057         nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2058         break;
2059
2060     case OFPACT_REG_LOAD:
2061         nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2062         break;
2063
2064     case OFPACT_DEC_TTL:
2065         print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2066         break;
2067
2068     case OFPACT_SET_TUNNEL:
2069         tunnel = ofpact_get_SET_TUNNEL(a);
2070         ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2071                       (tunnel->tun_id > UINT32_MAX
2072                        || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2073                       tunnel->tun_id);
2074         break;
2075
2076     case OFPACT_SET_QUEUE:
2077         ds_put_format(s, "set_queue:%"PRIu32,
2078                       ofpact_get_SET_QUEUE(a)->queue_id);
2079         break;
2080
2081     case OFPACT_POP_QUEUE:
2082         ds_put_cstr(s, "pop_queue");
2083         break;
2084
2085     case OFPACT_FIN_TIMEOUT:
2086         print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2087         break;
2088
2089     case OFPACT_RESUBMIT:
2090         resubmit = ofpact_get_RESUBMIT(a);
2091         if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2092             ds_put_cstr(s, "resubmit:");
2093             ofputil_format_port(resubmit->in_port, s);
2094         } else {
2095             ds_put_format(s, "resubmit(");
2096             if (resubmit->in_port != OFPP_IN_PORT) {
2097                 ofputil_format_port(resubmit->in_port, s);
2098             }
2099             ds_put_char(s, ',');
2100             if (resubmit->table_id != 255) {
2101                 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2102             }
2103             ds_put_char(s, ')');
2104         }
2105         break;
2106
2107     case OFPACT_LEARN:
2108         learn_format(ofpact_get_LEARN(a), s);
2109         break;
2110
2111     case OFPACT_MULTIPATH:
2112         multipath_format(ofpact_get_MULTIPATH(a), s);
2113         break;
2114
2115     case OFPACT_AUTOPATH:
2116         autopath = ofpact_get_AUTOPATH(a);
2117         ds_put_cstr(s, "autopath(");
2118         ofputil_format_port(autopath->port, s);
2119         ds_put_char(s, ',');
2120         mf_format_subfield(&autopath->dst, s);
2121         ds_put_char(s, ')');
2122         break;
2123
2124     case OFPACT_NOTE:
2125         print_note(ofpact_get_NOTE(a), s);
2126         break;
2127
2128     case OFPACT_PUSH_MPLS:
2129         ds_put_format(s, "push_mpls:0x%04"PRIx16,
2130                       ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2131         break;
2132
2133     case OFPACT_POP_MPLS:
2134         ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2135                       ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2136         break;
2137
2138     case OFPACT_EXIT:
2139         ds_put_cstr(s, "exit");
2140         break;
2141
2142     case OFPACT_CLEAR_ACTIONS:
2143         ds_put_format(s, "%s",
2144                       ofpact_instruction_name_from_type(
2145                           OVSINST_OFPIT11_CLEAR_ACTIONS));
2146         break;
2147
2148     case OFPACT_WRITE_METADATA:
2149         metadata = ofpact_get_WRITE_METADATA(a);
2150         ds_put_format(s, "%s:%#"PRIx64,
2151                       ofpact_instruction_name_from_type(
2152                           OVSINST_OFPIT11_WRITE_METADATA),
2153                       ntohll(metadata->metadata));
2154         if (metadata->mask != htonll(UINT64_MAX)) {
2155             ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2156         }
2157         break;
2158
2159     case OFPACT_GOTO_TABLE:
2160         ds_put_format(s, "%s:%"PRIu8,
2161                       ofpact_instruction_name_from_type(
2162                           OVSINST_OFPIT11_GOTO_TABLE),
2163                       ofpact_get_GOTO_TABLE(a)->table_id);
2164         break;
2165     }
2166 }
2167
2168 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2169  * 'ofpacts' to 'string'. */
2170 void
2171 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2172                struct ds *string)
2173 {
2174     ds_put_cstr(string, "actions=");
2175     if (!ofpacts_len) {
2176         ds_put_cstr(string, "drop");
2177     } else {
2178         const struct ofpact *a;
2179
2180         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2181             if (a != ofpacts) {
2182                 ds_put_cstr(string, ",");
2183             }
2184
2185             /* XXX write-actions */
2186             ofpact_format(a, string);
2187         }
2188     }
2189 }
2190 \f
2191 /* Internal use by helpers. */
2192
2193 void *
2194 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2195 {
2196     struct ofpact *ofpact;
2197
2198     ofpact_pad(ofpacts);
2199     ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2200     ofpact_init(ofpact, type, len);
2201     return ofpact;
2202 }
2203
2204 void
2205 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2206 {
2207     memset(ofpact, 0, len);
2208     ofpact->type = type;
2209     ofpact->compat = OFPUTIL_ACTION_INVALID;
2210     ofpact->len = len;
2211 }
2212 \f
2213 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2214  * starting at 'ofpact'.
2215  *
2216  * This is the correct way to update a variable-length ofpact's length after
2217  * adding the variable-length part of the payload.  (See the large comment
2218  * near the end of ofp-actions.h for more information.) */
2219 void
2220 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2221 {
2222     ovs_assert(ofpact == ofpacts->l2);
2223     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2224 }
2225
2226 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
2227  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2228  * client must call this itself after adding the final ofpact to an array of
2229  * them.
2230  *
2231  * (The consequences of failing to call this function are probably not dire.
2232  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2233  * not dereference it.  That's undefined behavior, technically, but it will not
2234  * cause a real problem on common systems.  Still, it seems better to call
2235  * it.) */
2236 void
2237 ofpact_pad(struct ofpbuf *ofpacts)
2238 {
2239     unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2240     if (rem) {
2241         ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2242     }
2243 }
2244
2245 void
2246 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2247                       const void *src)
2248 {
2249     load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2250     load->dst.field = mf;
2251     load->dst.ofs = 0;
2252     load->dst.n_bits = mf->n_bits;
2253     bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2254                  &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);
2255 }