Add OF11 SET IP TTL action.
[sliver-openvswitch.git] / lib / ofp-actions.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofp-actions.h"
19 #include "bundle.h"
20 #include "byte-order.h"
21 #include "compiler.h"
22 #include "dynamic-string.h"
23 #include "learn.h"
24 #include "meta-flow.h"
25 #include "multipath.h"
26 #include "nx-match.h"
27 #include "ofp-util.h"
28 #include "ofpbuf.h"
29 #include "util.h"
30 #include "vlog.h"
31
32 VLOG_DEFINE_THIS_MODULE(ofp_actions);
33
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35 \f
36 /* Converting OpenFlow 1.0 to ofpacts. */
37
38 union ofp_action {
39     ovs_be16 type;
40     struct ofp_action_header header;
41     struct ofp_action_vendor_header vendor;
42     struct ofp10_action_output output10;
43     struct ofp_action_vlan_vid vlan_vid;
44     struct ofp_action_vlan_pcp vlan_pcp;
45     struct ofp_action_nw_addr nw_addr;
46     struct ofp_action_nw_tos nw_tos;
47     struct ofp11_action_nw_ecn nw_ecn;
48     struct ofp11_action_nw_ttl nw_ttl;
49     struct ofp_action_tp_port tp_port;
50 };
51 OFP_ASSERT(sizeof(union ofp_action) == 8);
52
53 static enum ofperr
54 output_from_openflow10(const struct ofp10_action_output *oao,
55                        struct ofpbuf *out)
56 {
57     struct ofpact_output *output;
58
59     output = ofpact_put_OUTPUT(out);
60     output->port = u16_to_ofp(ntohs(oao->port));
61     output->max_len = ntohs(oao->max_len);
62
63     return ofputil_check_output_port(output->port, OFPP_MAX);
64 }
65
66 static enum ofperr
67 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
68                         struct ofpbuf *out)
69 {
70     struct ofpact_enqueue *enqueue;
71
72     enqueue = ofpact_put_ENQUEUE(out);
73     enqueue->port = u16_to_ofp(ntohs(oae->port));
74     enqueue->queue = ntohl(oae->queue_id);
75     if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
76         && enqueue->port != OFPP_IN_PORT
77         && enqueue->port != OFPP_LOCAL) {
78         return OFPERR_OFPBAC_BAD_OUT_PORT;
79     }
80     return 0;
81 }
82
83 static void
84 resubmit_from_openflow(const struct nx_action_resubmit *nar,
85                        struct ofpbuf *out)
86 {
87     struct ofpact_resubmit *resubmit;
88
89     resubmit = ofpact_put_RESUBMIT(out);
90     resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
91     resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
92     resubmit->table_id = 0xff;
93 }
94
95 static enum ofperr
96 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
97                              struct ofpbuf *out)
98 {
99     struct ofpact_resubmit *resubmit;
100
101     if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
102         return OFPERR_OFPBAC_BAD_ARGUMENT;
103     }
104
105     resubmit = ofpact_put_RESUBMIT(out);
106     resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
107     resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
108     resubmit->table_id = nar->table;
109     return 0;
110 }
111
112 static enum ofperr
113 output_reg_from_openflow(const struct nx_action_output_reg *naor,
114                          struct ofpbuf *out)
115 {
116     struct ofpact_output_reg *output_reg;
117
118     if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
119         return OFPERR_OFPBAC_BAD_ARGUMENT;
120     }
121
122     output_reg = ofpact_put_OUTPUT_REG(out);
123     output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
124     output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
125     output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
126     output_reg->max_len = ntohs(naor->max_len);
127
128     return mf_check_src(&output_reg->src, NULL);
129 }
130
131 static void
132 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
133                           struct ofpbuf *out)
134 {
135     struct ofpact_fin_timeout *oft;
136
137     oft = ofpact_put_FIN_TIMEOUT(out);
138     oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
139     oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
140 }
141
142 static void
143 controller_from_openflow(const struct nx_action_controller *nac,
144                          struct ofpbuf *out)
145 {
146     struct ofpact_controller *oc;
147
148     oc = ofpact_put_CONTROLLER(out);
149     oc->max_len = ntohs(nac->max_len);
150     oc->controller_id = ntohs(nac->controller_id);
151     oc->reason = nac->reason;
152 }
153
154 static enum ofperr
155 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
156                     struct ofpbuf *out)
157 {
158     struct ofpact_metadata *om;
159
160     if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
161         return OFPERR_NXBRC_MUST_BE_ZERO;
162     }
163
164     om = ofpact_put_WRITE_METADATA(out);
165     om->metadata = nawm->metadata;
166     om->mask = nawm->mask;
167
168     return 0;
169 }
170
171 static void
172 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
173 {
174     struct ofpact_note *note;
175     unsigned int length;
176
177     length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
178     note = ofpact_put(out, OFPACT_NOTE,
179                       offsetof(struct ofpact_note, data) + length);
180     note->length = length;
181     memcpy(note->data, nan->note, length);
182 }
183
184 static enum ofperr
185 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
186 {
187     uint16_t id = 0;
188     struct ofpact_cnt_ids *ids;
189     enum ofperr error = 0;
190
191     ids = ofpact_put_DEC_TTL(out);
192     ids->ofpact.compat = compat;
193     ids->n_controllers = 1;
194     ofpbuf_put(out, &id, sizeof id);
195     ids = out->l2;
196     ofpact_update_len(out, &ids->ofpact);
197     return error;
198 }
199
200 static enum ofperr
201 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
202                       struct ofpbuf *out)
203 {
204     struct ofpact_cnt_ids *ids;
205     size_t ids_size;
206     int i;
207
208     ids = ofpact_put_DEC_TTL(out);
209     ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
210     ids->n_controllers = ntohs(nac_ids->n_controllers);
211     ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
212
213     if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
214         return OFPERR_NXBRC_MUST_BE_ZERO;
215     }
216
217     if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
218         VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
219                      "allocated for controller ids.  %zu bytes are required for "
220                      "%"PRIu16" controllers.", ids_size,
221                      ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
222         return OFPERR_OFPBAC_BAD_LEN;
223     }
224
225     for (i = 0; i < ids->n_controllers; i++) {
226         uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
227         ofpbuf_put(out, &id, sizeof id);
228         ids = out->l2;
229     }
230
231     ofpact_update_len(out, &ids->ofpact);
232
233     return 0;
234 }
235
236 static enum ofperr
237 sample_from_openflow(const struct nx_action_sample *nas,
238                      struct ofpbuf *out)
239 {
240     struct ofpact_sample *sample;
241
242     sample = ofpact_put_SAMPLE(out);
243     sample->probability = ntohs(nas->probability);
244     sample->collector_set_id = ntohl(nas->collector_set_id);
245     sample->obs_domain_id = ntohl(nas->obs_domain_id);
246     sample->obs_point_id = ntohl(nas->obs_point_id);
247
248     if (sample->probability == 0) {
249         return OFPERR_OFPBAC_BAD_ARGUMENT;
250     }
251
252     return 0;
253 }
254
255 static enum ofperr
256 push_mpls_from_openflow(ovs_be16 ethertype, enum ofpact_mpls_position position,
257                         struct ofpbuf *out)
258 {
259     struct ofpact_push_mpls *oam;
260
261     if (!eth_type_mpls(ethertype)) {
262         return OFPERR_OFPBAC_BAD_ARGUMENT;
263     }
264     oam = ofpact_put_PUSH_MPLS(out);
265     oam->ethertype = ethertype;
266     oam->position = position;
267
268     return 0;
269 }
270
271 static enum ofperr
272 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
273 {
274     const struct nx_action_header *nah = (const struct nx_action_header *) a;
275     uint16_t len = ntohs(a->header.len);
276
277     if (len < sizeof(struct nx_action_header)) {
278         return OFPERR_OFPBAC_BAD_LEN;
279     } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
280         return OFPERR_OFPBAC_BAD_VENDOR;
281     }
282
283     switch (nah->subtype) {
284 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
285         case CONSTANT_HTONS(ENUM):                      \
286             if (EXTENSIBLE                              \
287                 ? len >= sizeof(struct STRUCT)          \
288                 : len == sizeof(struct STRUCT)) {       \
289                 *code = OFPUTIL_##ENUM;                 \
290                 return 0;                               \
291             } else {                                    \
292                 return OFPERR_OFPBAC_BAD_LEN;           \
293             }                                           \
294             NOT_REACHED();
295 #include "ofp-util.def"
296
297     case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
298     case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
299     default:
300         return OFPERR_OFPBAC_BAD_TYPE;
301     }
302 }
303
304 /* Parses 'a' to determine its type.  On success stores the correct type into
305  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
306  * '*code' is indeterminate.
307  *
308  * The caller must have already verified that 'a''s length is potentially
309  * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
310  * ofp_action) and no longer than the amount of space allocated to 'a').
311  *
312  * This function verifies that 'a''s length is correct for the type of action
313  * that it represents. */
314 static enum ofperr
315 decode_openflow10_action(const union ofp_action *a,
316                          enum ofputil_action_code *code)
317 {
318     switch (a->type) {
319     case CONSTANT_HTONS(OFPAT10_VENDOR):
320         return decode_nxast_action(a, code);
321
322 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)                          \
323         case CONSTANT_HTONS(ENUM):                                  \
324             if (a->header.len == htons(sizeof(struct STRUCT))) {    \
325                 *code = OFPUTIL_##ENUM;                             \
326                 return 0;                                           \
327             } else {                                                \
328                 return OFPERR_OFPBAC_BAD_LEN;                       \
329             }                                                       \
330             break;
331 #include "ofp-util.def"
332
333     default:
334         return OFPERR_OFPBAC_BAD_TYPE;
335     }
336 }
337
338 static enum ofperr
339 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
340                   struct ofpbuf *out)
341 {
342     const struct nx_action_resubmit *nar;
343     const struct nx_action_set_tunnel *nast;
344     const struct nx_action_set_queue *nasq;
345     const struct nx_action_note *nan;
346     const struct nx_action_set_tunnel64 *nast64;
347     const struct nx_action_write_metadata *nawm;
348     struct ofpact_tunnel *tunnel;
349     enum ofperr error = 0;
350
351     switch (code) {
352     case OFPUTIL_ACTION_INVALID:
353 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
354 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
355 #include "ofp-util.def"
356         NOT_REACHED();
357
358     case OFPUTIL_NXAST_RESUBMIT:
359         resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
360         break;
361
362     case OFPUTIL_NXAST_SET_TUNNEL:
363         nast = (const struct nx_action_set_tunnel *) a;
364         tunnel = ofpact_put_SET_TUNNEL(out);
365         tunnel->ofpact.compat = code;
366         tunnel->tun_id = ntohl(nast->tun_id);
367         break;
368
369     case OFPUTIL_NXAST_WRITE_METADATA:
370         nawm = ALIGNED_CAST(const struct nx_action_write_metadata *, a);
371         error = metadata_from_nxast(nawm, out);
372         break;
373
374     case OFPUTIL_NXAST_SET_QUEUE:
375         nasq = (const struct nx_action_set_queue *) a;
376         ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
377         break;
378
379     case OFPUTIL_NXAST_POP_QUEUE:
380         ofpact_put_POP_QUEUE(out);
381         break;
382
383     case OFPUTIL_NXAST_REG_MOVE:
384         error = nxm_reg_move_from_openflow(
385             (const struct nx_action_reg_move *) a, out);
386         break;
387
388     case OFPUTIL_NXAST_REG_LOAD:
389         error = nxm_reg_load_from_openflow(
390             ALIGNED_CAST(const struct nx_action_reg_load *, a), out);
391         break;
392
393     case OFPUTIL_NXAST_STACK_PUSH:
394         error = nxm_stack_push_from_openflow(
395             (const struct nx_action_stack *) a, out);
396         break;
397
398     case OFPUTIL_NXAST_STACK_POP:
399         error = nxm_stack_pop_from_openflow(
400             (const struct nx_action_stack *) a, out);
401         break;
402
403     case OFPUTIL_NXAST_NOTE:
404         nan = (const struct nx_action_note *) a;
405         note_from_openflow(nan, out);
406         break;
407
408     case OFPUTIL_NXAST_SET_TUNNEL64:
409         nast64 = ALIGNED_CAST(const struct nx_action_set_tunnel64 *, a);
410         tunnel = ofpact_put_SET_TUNNEL(out);
411         tunnel->ofpact.compat = code;
412         tunnel->tun_id = ntohll(nast64->tun_id);
413         break;
414
415     case OFPUTIL_NXAST_MULTIPATH:
416         error = multipath_from_openflow((const struct nx_action_multipath *) a,
417                                         ofpact_put_MULTIPATH(out));
418         break;
419
420     case OFPUTIL_NXAST_BUNDLE:
421     case OFPUTIL_NXAST_BUNDLE_LOAD:
422         error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
423         break;
424
425     case OFPUTIL_NXAST_OUTPUT_REG:
426         error = output_reg_from_openflow(
427             (const struct nx_action_output_reg *) a, out);
428         break;
429
430     case OFPUTIL_NXAST_RESUBMIT_TABLE:
431         nar = (const struct nx_action_resubmit *) a;
432         error = resubmit_table_from_openflow(nar, out);
433         break;
434
435     case OFPUTIL_NXAST_LEARN:
436         error = learn_from_openflow(
437             ALIGNED_CAST(const struct nx_action_learn *, a), out);
438         break;
439
440     case OFPUTIL_NXAST_EXIT:
441         ofpact_put_EXIT(out);
442         break;
443
444     case OFPUTIL_NXAST_DEC_TTL:
445         error = dec_ttl_from_openflow(out, code);
446         break;
447
448     case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
449         error = dec_ttl_cnt_ids_from_openflow(
450                     (const struct nx_action_cnt_ids *) a, out);
451         break;
452
453     case OFPUTIL_NXAST_FIN_TIMEOUT:
454         fin_timeout_from_openflow(
455             (const struct nx_action_fin_timeout *) a, out);
456         break;
457
458     case OFPUTIL_NXAST_CONTROLLER:
459         controller_from_openflow((const struct nx_action_controller *) a, out);
460         break;
461
462     case OFPUTIL_NXAST_PUSH_MPLS: {
463         struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
464         error = push_mpls_from_openflow(nxapm->ethertype,
465                                         OFPACT_MPLS_AFTER_VLAN, out);
466         break;
467     }
468
469     case OFPUTIL_NXAST_SET_MPLS_TTL: {
470         struct nx_action_mpls_ttl *nxamt = (struct nx_action_mpls_ttl *)a;
471         ofpact_put_SET_MPLS_TTL(out)->ttl = nxamt->ttl;
472         break;
473     }
474
475     case OFPUTIL_NXAST_DEC_MPLS_TTL:
476         ofpact_put_DEC_MPLS_TTL(out);
477         break;
478
479     case OFPUTIL_NXAST_POP_MPLS: {
480         struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
481         if (eth_type_mpls(nxapm->ethertype)) {
482             return OFPERR_OFPBAC_BAD_ARGUMENT;
483         }
484         ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
485         break;
486     }
487
488     case OFPUTIL_NXAST_SAMPLE:
489         error = sample_from_openflow(
490             (const struct nx_action_sample *) a, out);
491         break;
492     }
493
494     return error;
495 }
496
497 static enum ofperr
498 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
499 {
500     enum ofputil_action_code code;
501     enum ofperr error;
502
503     error = decode_openflow10_action(a, &code);
504     if (error) {
505         return error;
506     }
507
508     switch (code) {
509     case OFPUTIL_ACTION_INVALID:
510 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
511 #include "ofp-util.def"
512         NOT_REACHED();
513
514     case OFPUTIL_OFPAT10_OUTPUT:
515         return output_from_openflow10(&a->output10, out);
516
517     case OFPUTIL_OFPAT10_SET_VLAN_VID:
518         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
519             return OFPERR_OFPBAC_BAD_ARGUMENT;
520         }
521         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
522         break;
523
524     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
525         if (a->vlan_pcp.vlan_pcp & ~7) {
526             return OFPERR_OFPBAC_BAD_ARGUMENT;
527         }
528         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
529         break;
530
531     case OFPUTIL_OFPAT10_STRIP_VLAN:
532         ofpact_put_STRIP_VLAN(out);
533         break;
534
535     case OFPUTIL_OFPAT10_SET_DL_SRC:
536         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
537                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
538         break;
539
540     case OFPUTIL_OFPAT10_SET_DL_DST:
541         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
542                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
543         break;
544
545     case OFPUTIL_OFPAT10_SET_NW_SRC:
546         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
547         break;
548
549     case OFPUTIL_OFPAT10_SET_NW_DST:
550         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
551         break;
552
553     case OFPUTIL_OFPAT10_SET_NW_TOS:
554         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
555             return OFPERR_OFPBAC_BAD_ARGUMENT;
556         }
557         ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
558         break;
559
560     case OFPUTIL_OFPAT10_SET_TP_SRC:
561         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
562         break;
563
564     case OFPUTIL_OFPAT10_SET_TP_DST:
565         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
566
567         break;
568
569     case OFPUTIL_OFPAT10_ENQUEUE:
570         error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
571                                         out);
572         break;
573
574 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
575 #include "ofp-util.def"
576         return ofpact_from_nxast(a, code, out);
577     }
578
579     return error;
580 }
581
582 static inline union ofp_action *
583 action_next(const union ofp_action *a)
584 {
585     return ((union ofp_action *) (void *)
586             ((uint8_t *) a + ntohs(a->header.len)));
587 }
588
589 static inline bool
590 action_is_valid(const union ofp_action *a, size_t n_actions)
591 {
592     uint16_t len = ntohs(a->header.len);
593     return (!(len % OFP_ACTION_ALIGN)
594             && len >= sizeof *a
595             && len / sizeof *a <= n_actions);
596 }
597
598 /* This macro is careful to check for actions with bad lengths. */
599 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS)                 \
600     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
601          (LEFT) > 0 && action_is_valid(ITER, LEFT);                     \
602          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
603           (ITER) = action_next(ITER)))
604
605 static void
606 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
607                enum ofperr error)
608 {
609     if (!VLOG_DROP_WARN(&rl)) {
610         struct ds s;
611
612         ds_init(&s);
613         ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
614         VLOG_WARN("bad action at offset %#zx (%s):\n%s",
615                   ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
616         ds_destroy(&s);
617     }
618 }
619
620 static enum ofperr
621 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
622                       struct ofpbuf *out,
623                       enum ofperr (*ofpact_from_openflow)(
624                           const union ofp_action *a, struct ofpbuf *out))
625 {
626     const union ofp_action *a;
627     size_t left;
628
629     ACTION_FOR_EACH (a, left, in, n_in) {
630         enum ofperr error = ofpact_from_openflow(a, out);
631         if (error) {
632             log_bad_action(in, n_in, a - in, error);
633             return error;
634         }
635     }
636     if (left) {
637         enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
638         log_bad_action(in, n_in, n_in - left, error);
639         return error;
640     }
641
642     ofpact_pad(out);
643     return 0;
644 }
645
646 static enum ofperr
647 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
648                         struct ofpbuf *out)
649 {
650     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
651 }
652
653 static enum ofperr
654 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
655                      struct ofpbuf *ofpacts,
656                      enum ofperr (*translate)(const union ofp_action *actions,
657                                               size_t n_actions,
658                                               struct ofpbuf *ofpacts))
659 {
660     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
661     const union ofp_action *actions;
662     enum ofperr error;
663
664     ofpbuf_clear(ofpacts);
665
666     if (actions_len % OFP_ACTION_ALIGN != 0) {
667         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
668                      "multiple of %d", actions_len, OFP_ACTION_ALIGN);
669         return OFPERR_OFPBRC_BAD_LEN;
670     }
671
672     actions = ofpbuf_try_pull(openflow, actions_len);
673     if (actions == NULL) {
674         VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
675                      "remaining message length (%zu)",
676                      actions_len, openflow->size);
677         return OFPERR_OFPBRC_BAD_LEN;
678     }
679
680     error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
681     if (error) {
682         ofpbuf_clear(ofpacts);
683         return error;
684     }
685
686     error = ofpacts_verify(ofpacts->data, ofpacts->size);
687     if (error) {
688         ofpbuf_clear(ofpacts);
689     }
690     return error;
691 }
692
693 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
694  * front of 'openflow' into ofpacts.  On success, replaces any existing content
695  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
696  * Returns 0 if successful, otherwise an OpenFlow error.
697  *
698  * The parsed actions are valid generically, but they may not be valid in a
699  * specific context.  For example, port numbers up to OFPP_MAX are valid
700  * generically, but specific datapaths may only support port numbers in a
701  * smaller range.  Use ofpacts_check() to additional check whether actions are
702  * valid in a specific context. */
703 enum ofperr
704 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
705                         struct ofpbuf *ofpacts)
706 {
707     return ofpacts_pull_actions(openflow, actions_len, ofpacts,
708                                 ofpacts_from_openflow10);
709 }
710 \f
711 /* OpenFlow 1.1 actions. */
712
713 /* Parses 'a' to determine its type.  On success stores the correct type into
714  * '*code' and returns 0.  On failure returns an OFPERR_* error code and
715  * '*code' is indeterminate.
716  *
717  * The caller must have already verified that 'a''s length is potentially
718  * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
719  * ofp_action) and no longer than the amount of space allocated to 'a').
720  *
721  * This function verifies that 'a''s length is correct for the type of action
722  * that it represents. */
723 static enum ofperr
724 decode_openflow11_action(const union ofp_action *a,
725                          enum ofputil_action_code *code)
726 {
727     uint16_t len;
728
729     switch (a->type) {
730     case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
731         return decode_nxast_action(a, code);
732
733 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
734         case CONSTANT_HTONS(ENUM):                      \
735             len = ntohs(a->header.len);                 \
736             if (EXTENSIBLE                              \
737                 ? len >= sizeof(struct STRUCT)          \
738                 : len == sizeof(struct STRUCT)) {       \
739                 *code = OFPUTIL_##ENUM;                 \
740                 return 0;                               \
741             } else {                                    \
742                 return OFPERR_OFPBAC_BAD_LEN;           \
743             }                                           \
744             NOT_REACHED();
745 #include "ofp-util.def"
746
747     default:
748         return OFPERR_OFPBAC_BAD_TYPE;
749     }
750 }
751
752 static enum ofperr
753 output_from_openflow11(const struct ofp11_action_output *oao,
754                        struct ofpbuf *out)
755 {
756     struct ofpact_output *output;
757     enum ofperr error;
758
759     output = ofpact_put_OUTPUT(out);
760     output->max_len = ntohs(oao->max_len);
761
762     error = ofputil_port_from_ofp11(oao->port, &output->port);
763     if (error) {
764         return error;
765     }
766
767     return ofputil_check_output_port(output->port, OFPP_MAX);
768 }
769
770 static enum ofperr
771 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
772 {
773     enum ofputil_action_code code;
774     enum ofperr error;
775
776     error = decode_openflow11_action(a, &code);
777     if (error) {
778         return error;
779     }
780
781     switch (code) {
782     case OFPUTIL_ACTION_INVALID:
783 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
784 #include "ofp-util.def"
785         NOT_REACHED();
786
787     case OFPUTIL_OFPAT11_OUTPUT:
788         return output_from_openflow11((const struct ofp11_action_output *) a,
789                                       out);
790
791     case OFPUTIL_OFPAT11_SET_VLAN_VID:
792         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
793             return OFPERR_OFPBAC_BAD_ARGUMENT;
794         }
795         ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
796         break;
797
798     case OFPUTIL_OFPAT11_SET_VLAN_PCP:
799         if (a->vlan_pcp.vlan_pcp & ~7) {
800             return OFPERR_OFPBAC_BAD_ARGUMENT;
801         }
802         ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
803         break;
804
805     case OFPUTIL_OFPAT11_PUSH_VLAN:
806         if (((const struct ofp11_action_push *)a)->ethertype !=
807             htons(ETH_TYPE_VLAN_8021Q)) {
808             /* XXX 802.1AD(QinQ) isn't supported at the moment */
809             return OFPERR_OFPBAC_BAD_ARGUMENT;
810         }
811         ofpact_put_PUSH_VLAN(out);
812         break;
813
814     case OFPUTIL_OFPAT11_POP_VLAN:
815         ofpact_put_STRIP_VLAN(out);
816         break;
817
818     case OFPUTIL_OFPAT11_SET_QUEUE:
819         ofpact_put_SET_QUEUE(out)->queue_id =
820             ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
821         break;
822
823     case OFPUTIL_OFPAT11_SET_DL_SRC:
824         memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
825                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
826         break;
827
828     case OFPUTIL_OFPAT11_SET_DL_DST:
829         memcpy(ofpact_put_SET_ETH_DST(out)->mac,
830                ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
831         break;
832
833     case OFPUTIL_OFPAT11_DEC_NW_TTL:
834         dec_ttl_from_openflow(out, code);
835         break;
836
837     case OFPUTIL_OFPAT11_SET_NW_SRC:
838         ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
839         break;
840
841     case OFPUTIL_OFPAT11_SET_NW_DST:
842         ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
843         break;
844
845     case OFPUTIL_OFPAT11_SET_NW_TOS:
846         if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
847             return OFPERR_OFPBAC_BAD_ARGUMENT;
848         }
849         ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
850         break;
851
852     case OFPUTIL_OFPAT11_SET_NW_ECN:
853         if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
854             return OFPERR_OFPBAC_BAD_ARGUMENT;
855         }
856         ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
857         break;
858
859     case OFPUTIL_OFPAT11_SET_NW_TTL:
860         ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
861         break;
862
863     case OFPUTIL_OFPAT11_SET_TP_SRC:
864         ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
865         break;
866
867     case OFPUTIL_OFPAT11_SET_TP_DST:
868         ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
869         break;
870
871     case OFPUTIL_OFPAT12_SET_FIELD:
872         return nxm_reg_load_from_openflow12_set_field(
873             (const struct ofp12_action_set_field *)a, out);
874
875     case OFPUTIL_OFPAT11_SET_MPLS_TTL: {
876         struct ofp11_action_mpls_ttl *oamt = (struct ofp11_action_mpls_ttl *)a;
877         ofpact_put_SET_MPLS_TTL(out)->ttl = oamt->mpls_ttl;
878         break;
879     }
880
881     case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
882         ofpact_put_DEC_MPLS_TTL(out);
883         break;
884
885     case OFPUTIL_OFPAT11_PUSH_MPLS: {
886         struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
887         error = push_mpls_from_openflow(oap->ethertype,
888                                         OFPACT_MPLS_AFTER_VLAN, out);
889         break;
890     }
891
892     case OFPUTIL_OFPAT11_POP_MPLS: {
893         struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
894         if (eth_type_mpls(oapm->ethertype)) {
895             return OFPERR_OFPBAC_BAD_ARGUMENT;
896         }
897         ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
898         break;
899     }
900
901     case OFPUTIL_OFPAT11_GROUP: {
902         struct ofp11_action_group *oag = (struct ofp11_action_group *)a;
903         ofpact_put_GROUP(out)->group_id = ntohl(oag->group_id);
904         break;
905     }
906
907 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
908 #include "ofp-util.def"
909         return ofpact_from_nxast(a, code, out);
910     }
911
912     return error;
913 }
914
915 static enum ofperr
916 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
917                         struct ofpbuf *out)
918 {
919     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
920 }
921
922 /* True if an action sets the value of a field
923  * in a way that is compatibile with the action set.
924  * False otherwise. */
925 static bool
926 ofpact_is_set_action(const struct ofpact *a)
927 {
928     switch (a->type) {
929     case OFPACT_REG_LOAD:
930     case OFPACT_SET_ETH_DST:
931     case OFPACT_SET_ETH_SRC:
932     case OFPACT_SET_IP_DSCP:
933     case OFPACT_SET_IP_ECN:
934     case OFPACT_SET_IP_TTL:
935     case OFPACT_SET_IPV4_DST:
936     case OFPACT_SET_IPV4_SRC:
937     case OFPACT_SET_L4_DST_PORT:
938     case OFPACT_SET_L4_SRC_PORT:
939     case OFPACT_SET_MPLS_TTL:
940     case OFPACT_SET_QUEUE:
941     case OFPACT_SET_TUNNEL:
942     case OFPACT_SET_VLAN_PCP:
943     case OFPACT_SET_VLAN_VID:
944         return true;
945     case OFPACT_BUNDLE:
946     case OFPACT_CLEAR_ACTIONS:
947     case OFPACT_CONTROLLER:
948     case OFPACT_DEC_MPLS_TTL:
949     case OFPACT_DEC_TTL:
950     case OFPACT_ENQUEUE:
951     case OFPACT_EXIT:
952     case OFPACT_FIN_TIMEOUT:
953     case OFPACT_GOTO_TABLE:
954     case OFPACT_GROUP:
955     case OFPACT_LEARN:
956     case OFPACT_METER:
957     case OFPACT_MULTIPATH:
958     case OFPACT_NOTE:
959     case OFPACT_OUTPUT:
960     case OFPACT_OUTPUT_REG:
961     case OFPACT_POP_MPLS:
962     case OFPACT_POP_QUEUE:
963     case OFPACT_PUSH_MPLS:
964     case OFPACT_PUSH_VLAN:
965     case OFPACT_REG_MOVE:
966     case OFPACT_RESUBMIT:
967     case OFPACT_SAMPLE:
968     case OFPACT_STACK_POP:
969     case OFPACT_STACK_PUSH:
970     case OFPACT_STRIP_VLAN:
971     case OFPACT_WRITE_ACTIONS:
972     case OFPACT_WRITE_METADATA:
973         return false;
974     default:
975         NOT_REACHED();
976     }
977 }
978
979 /* True if an action is allowed in the action set.
980  * False otherwise. */
981 static bool
982 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
983 {
984     switch (a->type) {
985     case OFPACT_DEC_MPLS_TTL:
986     case OFPACT_DEC_TTL:
987     case OFPACT_GROUP:
988     case OFPACT_OUTPUT:
989     case OFPACT_POP_MPLS:
990     case OFPACT_PUSH_MPLS:
991     case OFPACT_PUSH_VLAN:
992     case OFPACT_REG_LOAD:
993     case OFPACT_SET_ETH_DST:
994     case OFPACT_SET_ETH_SRC:
995     case OFPACT_SET_IP_DSCP:
996     case OFPACT_SET_IP_ECN:
997     case OFPACT_SET_IP_TTL:
998     case OFPACT_SET_IPV4_DST:
999     case OFPACT_SET_IPV4_SRC:
1000     case OFPACT_SET_L4_DST_PORT:
1001     case OFPACT_SET_L4_SRC_PORT:
1002     case OFPACT_SET_MPLS_TTL:
1003     case OFPACT_SET_QUEUE:
1004     case OFPACT_SET_TUNNEL:
1005     case OFPACT_SET_VLAN_PCP:
1006     case OFPACT_SET_VLAN_VID:
1007     case OFPACT_STRIP_VLAN:
1008         return true;
1009
1010     /* In general these actions are excluded because they are not part of
1011      * the OpenFlow specification nor map to actions that are defined in
1012      * the specification.  Thus the order in which they should be applied
1013      * in the action set is undefined. */
1014     case OFPACT_BUNDLE:
1015     case OFPACT_CONTROLLER:
1016     case OFPACT_ENQUEUE:
1017     case OFPACT_EXIT:
1018     case OFPACT_FIN_TIMEOUT:
1019     case OFPACT_LEARN:
1020     case OFPACT_MULTIPATH:
1021     case OFPACT_NOTE:
1022     case OFPACT_OUTPUT_REG:
1023     case OFPACT_POP_QUEUE:
1024     case OFPACT_REG_MOVE:
1025     case OFPACT_RESUBMIT:
1026     case OFPACT_SAMPLE:
1027     case OFPACT_STACK_POP:
1028     case OFPACT_STACK_PUSH:
1029
1030     /* The action set may only include actions and thus
1031      * may not include any instructions */
1032     case OFPACT_CLEAR_ACTIONS:
1033     case OFPACT_GOTO_TABLE:
1034     case OFPACT_METER:
1035     case OFPACT_WRITE_ACTIONS:
1036     case OFPACT_WRITE_METADATA:
1037         return false;
1038     default:
1039         NOT_REACHED();
1040     }
1041 }
1042
1043 /* Append ofpact 'a' onto the tail of 'out' */
1044 static void
1045 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1046 {
1047     ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1048 }
1049
1050 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1051 static bool
1052 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1053                   enum ofpact_type filter)
1054 {
1055     const struct ofpact *target;
1056     const struct ofpact *a;
1057
1058     target = NULL;
1059     OFPACT_FOR_EACH (a, in->data, in->size) {
1060         if (a->type == filter) {
1061             target = a;
1062         }
1063     }
1064     if (target) {
1065         ofpact_copy(out, target);
1066     }
1067     return target != NULL;
1068 }
1069
1070 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1071  * The order of appended ofpacts is preserved between 'in' and 'out' */
1072 static void
1073 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1074                  bool (*filter)(const struct ofpact *))
1075 {
1076     const struct ofpact *a;
1077
1078     OFPACT_FOR_EACH (a, in->data, in->size) {
1079         if (filter(a)) {
1080             ofpact_copy(out, a);
1081         }
1082     }
1083 }
1084
1085 /* Reads 'action_set', which contains ofpacts accumulated by
1086  * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1087  * executed directly into 'action_list'.  (These names correspond to the
1088  * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1089  *
1090  * In general this involves appending the last instance of each action that is
1091  * adimissible in the action set in the order described in the OpenFlow
1092  * specification.
1093  *
1094  * Exceptions:
1095  * + output action is only appended if no group action was present in 'in'.
1096  * + As a simplification all set actions are copied in the order the are
1097  *   provided in 'in' as many set actions applied to a field has the same
1098  *   affect as only applying the last action that sets a field and
1099  *   duplicates are removed by do_xlate_actions().
1100  *   This has an unwanted side-effect of compsoting multiple
1101  *   LOAD_REG actions that touch different regions of the same field. */
1102 void
1103 ofpacts_execute_action_set(struct ofpbuf *action_list,
1104                            const struct ofpbuf *action_set)
1105 {
1106     /* The OpenFlow spec "Action Set" section specifies this order. */
1107     ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1108     ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1109     ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1110     ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1111     ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1112     ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1113     ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1114     ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1115
1116     /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1117      * we should execute only OFPACT_GROUP.
1118      *
1119      * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1120      * all the actions because there's no point in modifying a packet that will
1121      * not be sent anywhere. */
1122     if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1123         !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1124         ofpbuf_clear(action_list);
1125     }
1126 }
1127
1128
1129 static enum ofperr
1130 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1131                                        size_t n_in, struct ofpbuf *out)
1132 {
1133     enum ofperr error;
1134     struct ofpact *a;
1135     size_t start = out->size;
1136
1137     error = ofpacts_from_openflow11(in, n_in, out);
1138     if (error) {
1139         return error;
1140     }
1141
1142     OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1143         if (!ofpact_is_allowed_in_actions_set(a)) {
1144             VLOG_WARN_RL(&rl, "disallowed action in action set");
1145             return OFPERR_OFPBAC_BAD_TYPE;
1146         }
1147     }
1148
1149     return 0;
1150 }
1151
1152 \f
1153 static enum ofperr
1154 ofpact_from_openflow13(const union ofp_action *a, struct ofpbuf *out)
1155 {
1156     enum ofputil_action_code code;
1157     enum ofperr error;
1158
1159     error = decode_openflow11_action(a, &code);
1160     if (error) {
1161         return error;
1162     }
1163
1164     if (code == OFPUTIL_OFPAT11_PUSH_MPLS) {
1165         struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
1166         error = push_mpls_from_openflow(oap->ethertype,
1167                                         OFPACT_MPLS_BEFORE_VLAN, out);
1168     } else {
1169         error = ofpact_from_openflow11(a, out);
1170     }
1171
1172     return error;
1173 }
1174
1175 static enum ofperr
1176 ofpacts_from_openflow13(const union ofp_action *in, size_t n_in,
1177                         struct ofpbuf *out)
1178 {
1179     return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow13);
1180 }
1181 \f
1182 /* OpenFlow 1.1 instructions. */
1183
1184 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
1185     static inline const struct STRUCT * OVS_UNUSED              \
1186     instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1187     {                                                           \
1188         ovs_assert(inst->type == htons(ENUM));                  \
1189         return ALIGNED_CAST(struct STRUCT *, inst);             \
1190     }                                                           \
1191                                                                 \
1192     static inline void OVS_UNUSED                               \
1193     instruction_init_##ENUM(struct STRUCT *s)                   \
1194     {                                                           \
1195         memset(s, 0, sizeof *s);                                \
1196         s->type = htons(ENUM);                                  \
1197         s->len = htons(sizeof *s);                              \
1198     }                                                           \
1199                                                                 \
1200     static inline struct STRUCT * OVS_UNUSED                    \
1201     instruction_put_##ENUM(struct ofpbuf *buf)                  \
1202     {                                                           \
1203         struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
1204         instruction_init_##ENUM(s);                             \
1205         return s;                                               \
1206     }
1207 OVS_INSTRUCTIONS
1208 #undef DEFINE_INST
1209
1210 struct instruction_type_info {
1211     enum ovs_instruction_type type;
1212     const char *name;
1213 };
1214
1215 static const struct instruction_type_info inst_info[] = {
1216 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
1217 OVS_INSTRUCTIONS
1218 #undef DEFINE_INST
1219 };
1220
1221 const char *
1222 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1223 {
1224     return inst_info[type].name;
1225 }
1226
1227 int
1228 ovs_instruction_type_from_name(const char *name)
1229 {
1230     const struct instruction_type_info *p;
1231     for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1232         if (!strcasecmp(name, p->name)) {
1233             return p->type;
1234         }
1235     }
1236     return -1;
1237 }
1238
1239 enum ovs_instruction_type
1240 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1241 {
1242     switch (type) {
1243     case OFPACT_METER:
1244         return OVSINST_OFPIT13_METER;
1245     case OFPACT_CLEAR_ACTIONS:
1246         return OVSINST_OFPIT11_CLEAR_ACTIONS;
1247     case OFPACT_WRITE_ACTIONS:
1248         return OVSINST_OFPIT11_WRITE_ACTIONS;
1249     case OFPACT_WRITE_METADATA:
1250         return OVSINST_OFPIT11_WRITE_METADATA;
1251     case OFPACT_GOTO_TABLE:
1252         return OVSINST_OFPIT11_GOTO_TABLE;
1253     case OFPACT_OUTPUT:
1254     case OFPACT_GROUP:
1255     case OFPACT_CONTROLLER:
1256     case OFPACT_ENQUEUE:
1257     case OFPACT_OUTPUT_REG:
1258     case OFPACT_BUNDLE:
1259     case OFPACT_SET_VLAN_VID:
1260     case OFPACT_SET_VLAN_PCP:
1261     case OFPACT_STRIP_VLAN:
1262     case OFPACT_PUSH_VLAN:
1263     case OFPACT_SET_ETH_SRC:
1264     case OFPACT_SET_ETH_DST:
1265     case OFPACT_SET_IPV4_SRC:
1266     case OFPACT_SET_IPV4_DST:
1267     case OFPACT_SET_IP_DSCP:
1268     case OFPACT_SET_IP_ECN:
1269     case OFPACT_SET_IP_TTL:
1270     case OFPACT_SET_L4_SRC_PORT:
1271     case OFPACT_SET_L4_DST_PORT:
1272     case OFPACT_REG_MOVE:
1273     case OFPACT_REG_LOAD:
1274     case OFPACT_STACK_PUSH:
1275     case OFPACT_STACK_POP:
1276     case OFPACT_DEC_TTL:
1277     case OFPACT_SET_MPLS_TTL:
1278     case OFPACT_DEC_MPLS_TTL:
1279     case OFPACT_PUSH_MPLS:
1280     case OFPACT_POP_MPLS:
1281     case OFPACT_SET_TUNNEL:
1282     case OFPACT_SET_QUEUE:
1283     case OFPACT_POP_QUEUE:
1284     case OFPACT_FIN_TIMEOUT:
1285     case OFPACT_RESUBMIT:
1286     case OFPACT_LEARN:
1287     case OFPACT_MULTIPATH:
1288     case OFPACT_NOTE:
1289     case OFPACT_EXIT:
1290     case OFPACT_SAMPLE:
1291     default:
1292         return OVSINST_OFPIT11_APPLY_ACTIONS;
1293     }
1294 }
1295
1296 static inline struct ofp11_instruction *
1297 instruction_next(const struct ofp11_instruction *inst)
1298 {
1299     return ((struct ofp11_instruction *) (void *)
1300             ((uint8_t *) inst + ntohs(inst->len)));
1301 }
1302
1303 static inline bool
1304 instruction_is_valid(const struct ofp11_instruction *inst,
1305                      size_t n_instructions)
1306 {
1307     uint16_t len = ntohs(inst->len);
1308     return (!(len % OFP11_INSTRUCTION_ALIGN)
1309             && len >= sizeof *inst
1310             && len / sizeof *inst <= n_instructions);
1311 }
1312
1313 /* This macro is careful to check for instructions with bad lengths. */
1314 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS)  \
1315     for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS);            \
1316          (LEFT) > 0 && instruction_is_valid(ITER, LEFT);                \
1317          ((LEFT) -= (ntohs((ITER)->len)                                 \
1318                      / sizeof(struct ofp11_instruction)),               \
1319           (ITER) = instruction_next(ITER)))
1320
1321 static enum ofperr
1322 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1323                               enum ovs_instruction_type *type)
1324 {
1325     uint16_t len = ntohs(inst->len);
1326
1327     switch (inst->type) {
1328     case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1329         return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1330
1331 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)     \
1332         case CONSTANT_HTONS(ENUM):                      \
1333             if (EXTENSIBLE                              \
1334                 ? len >= sizeof(struct STRUCT)          \
1335                 : len == sizeof(struct STRUCT)) {       \
1336                 *type = OVSINST_##ENUM;                 \
1337                 return 0;                               \
1338             } else {                                    \
1339                 return OFPERR_OFPBIC_BAD_LEN;           \
1340             }
1341 OVS_INSTRUCTIONS
1342 #undef DEFINE_INST
1343
1344     default:
1345         return OFPERR_OFPBIC_UNKNOWN_INST;
1346     }
1347 }
1348
1349 static enum ofperr
1350 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1351                                size_t n_insts,
1352                                const struct ofp11_instruction *out[])
1353 {
1354     const struct ofp11_instruction *inst;
1355     size_t left;
1356
1357     memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1358     INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1359         enum ovs_instruction_type type;
1360         enum ofperr error;
1361
1362         error = decode_openflow11_instruction(inst, &type);
1363         if (error) {
1364             return error;
1365         }
1366
1367         if (out[type]) {
1368             return OFPERR_ONFBIC_DUP_INSTRUCTION;
1369         }
1370         out[type] = inst;
1371     }
1372
1373     if (left) {
1374         VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1375                      (n_insts - left) * sizeof *inst);
1376         return OFPERR_OFPBIC_BAD_LEN;
1377     }
1378     return 0;
1379 }
1380
1381 static void
1382 get_actions_from_instruction(const struct ofp11_instruction *inst,
1383                              const union ofp_action **actions,
1384                              size_t *n_actions)
1385 {
1386     *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1387     *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1388 }
1389
1390 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
1391  * front of 'openflow' into ofpacts.  On success, replaces any existing content
1392  * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1393  * Returns 0 if successful, otherwise an OpenFlow error.
1394  *
1395  * Actions are processed according to their OpenFlow version which
1396  * is provided in the 'version' parameter.
1397  *
1398  * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1399  * instructions, so you should call ofpacts_pull_openflow11_instructions()
1400  * instead of this function.
1401  *
1402  * The parsed actions are valid generically, but they may not be valid in a
1403  * specific context.  For example, port numbers up to OFPP_MAX are valid
1404  * generically, but specific datapaths may only support port numbers in a
1405  * smaller range.  Use ofpacts_check() to additional check whether actions are
1406  * valid in a specific context. */
1407 enum ofperr
1408 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1409                                 enum ofp_version version,
1410                                 unsigned int actions_len,
1411                                 struct ofpbuf *ofpacts)
1412 {
1413     switch (version) {
1414     case OFP10_VERSION:
1415     case OFP11_VERSION:
1416     case OFP12_VERSION:
1417         return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1418                                     ofpacts_from_openflow11);
1419     case OFP13_VERSION:
1420         return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1421                                     ofpacts_from_openflow13);
1422     default:
1423         NOT_REACHED();
1424     }
1425 }
1426
1427 enum ofperr
1428 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1429                                      enum ofp_version version,
1430                                      unsigned int instructions_len,
1431                                      struct ofpbuf *ofpacts)
1432 {
1433     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1434     const struct ofp11_instruction *instructions;
1435     const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1436     enum ofperr error;
1437
1438     ofpbuf_clear(ofpacts);
1439
1440     if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1441         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1442                      "multiple of %d",
1443                      instructions_len, OFP11_INSTRUCTION_ALIGN);
1444         error = OFPERR_OFPBIC_BAD_LEN;
1445         goto exit;
1446     }
1447
1448     instructions = ofpbuf_try_pull(openflow, instructions_len);
1449     if (instructions == NULL) {
1450         VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1451                      "remaining message length (%zu)",
1452                      instructions_len, openflow->size);
1453         error = OFPERR_OFPBIC_BAD_LEN;
1454         goto exit;
1455     }
1456
1457     error = decode_openflow11_instructions(
1458         instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1459         insts);
1460     if (error) {
1461         goto exit;
1462     }
1463
1464     if (insts[OVSINST_OFPIT13_METER]) {
1465         const struct ofp13_instruction_meter *oim;
1466         struct ofpact_meter *om;
1467
1468         oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1469                            insts[OVSINST_OFPIT13_METER]);
1470
1471         om = ofpact_put_METER(ofpacts);
1472         om->meter_id = ntohl(oim->meter_id);
1473     }
1474     if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1475         const union ofp_action *actions;
1476         size_t n_actions;
1477
1478         get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1479                                      &actions, &n_actions);
1480         switch (version) {
1481         case OFP10_VERSION:
1482         case OFP11_VERSION:
1483         case OFP12_VERSION:
1484             error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1485             break;
1486         case OFP13_VERSION:
1487             error = ofpacts_from_openflow13(actions, n_actions, ofpacts);
1488             break;
1489         default:
1490             NOT_REACHED();
1491         }
1492         if (error) {
1493             goto exit;
1494         }
1495     }
1496     if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1497         instruction_get_OFPIT11_CLEAR_ACTIONS(
1498             insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1499         ofpact_put_CLEAR_ACTIONS(ofpacts);
1500     }
1501     if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1502         struct ofpact_nest *on;
1503         const union ofp_action *actions;
1504         size_t n_actions;
1505         size_t start;
1506
1507         ofpact_pad(ofpacts);
1508         start = ofpacts->size;
1509         on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1510                         offsetof(struct ofpact_nest, actions));
1511         get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1512                                      &actions, &n_actions);
1513         error = ofpacts_from_openflow11_for_action_set(actions, n_actions,
1514                                                        ofpacts);
1515         if (error) {
1516             goto exit;
1517         }
1518         on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1519         on->ofpact.len = ofpacts->size - start;
1520     }
1521     if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1522         const struct ofp11_instruction_write_metadata *oiwm;
1523         struct ofpact_metadata *om;
1524
1525         oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1526                             insts[OVSINST_OFPIT11_WRITE_METADATA]);
1527
1528         om = ofpact_put_WRITE_METADATA(ofpacts);
1529         om->metadata = oiwm->metadata;
1530         om->mask = oiwm->metadata_mask;
1531     }
1532     if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1533         const struct ofp11_instruction_goto_table *oigt;
1534         struct ofpact_goto_table *ogt;
1535
1536         oigt = instruction_get_OFPIT11_GOTO_TABLE(
1537             insts[OVSINST_OFPIT11_GOTO_TABLE]);
1538         ogt = ofpact_put_GOTO_TABLE(ofpacts);
1539         ogt->table_id = oigt->table_id;
1540     }
1541
1542     error = ofpacts_verify(ofpacts->data, ofpacts->size);
1543 exit:
1544     if (error) {
1545         ofpbuf_clear(ofpacts);
1546     }
1547     return error;
1548 }
1549 \f
1550 /* May modify flow->dl_type, caller must restore it. */
1551 static enum ofperr
1552 ofpact_check__(const struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1553                uint8_t table_id)
1554 {
1555     const struct ofpact_enqueue *enqueue;
1556
1557     switch (a->type) {
1558     case OFPACT_OUTPUT:
1559         return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1560                                          max_ports);
1561
1562     case OFPACT_CONTROLLER:
1563         return 0;
1564
1565     case OFPACT_ENQUEUE:
1566         enqueue = ofpact_get_ENQUEUE(a);
1567         if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1568             && enqueue->port != OFPP_IN_PORT
1569             && enqueue->port != OFPP_LOCAL) {
1570             return OFPERR_OFPBAC_BAD_OUT_PORT;
1571         }
1572         return 0;
1573
1574     case OFPACT_OUTPUT_REG:
1575         return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1576
1577     case OFPACT_BUNDLE:
1578         return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1579
1580     case OFPACT_SET_VLAN_VID:
1581     case OFPACT_SET_VLAN_PCP:
1582     case OFPACT_STRIP_VLAN:
1583     case OFPACT_PUSH_VLAN:
1584     case OFPACT_SET_ETH_SRC:
1585     case OFPACT_SET_ETH_DST:
1586     case OFPACT_SET_IPV4_SRC:
1587     case OFPACT_SET_IPV4_DST:
1588     case OFPACT_SET_IP_DSCP:
1589     case OFPACT_SET_IP_ECN:
1590     case OFPACT_SET_IP_TTL:
1591     case OFPACT_SET_L4_SRC_PORT:
1592     case OFPACT_SET_L4_DST_PORT:
1593         return 0;
1594
1595     case OFPACT_REG_MOVE:
1596         return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1597
1598     case OFPACT_REG_LOAD:
1599         return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1600
1601     case OFPACT_STACK_PUSH:
1602         return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1603
1604     case OFPACT_STACK_POP:
1605         return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1606
1607     case OFPACT_DEC_TTL:
1608     case OFPACT_SET_MPLS_TTL:
1609     case OFPACT_DEC_MPLS_TTL:
1610     case OFPACT_SET_TUNNEL:
1611     case OFPACT_SET_QUEUE:
1612     case OFPACT_POP_QUEUE:
1613     case OFPACT_FIN_TIMEOUT:
1614     case OFPACT_RESUBMIT:
1615         return 0;
1616
1617     case OFPACT_LEARN:
1618         return learn_check(ofpact_get_LEARN(a), flow);
1619
1620     case OFPACT_MULTIPATH:
1621         return multipath_check(ofpact_get_MULTIPATH(a), flow);
1622
1623     case OFPACT_NOTE:
1624     case OFPACT_EXIT:
1625         return 0;
1626
1627     case OFPACT_PUSH_MPLS:
1628         flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1629         return 0;
1630
1631     case OFPACT_POP_MPLS:
1632         flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1633         return 0;
1634
1635     case OFPACT_SAMPLE:
1636         return 0;
1637
1638     case OFPACT_CLEAR_ACTIONS:
1639         return 0;
1640
1641     case OFPACT_WRITE_ACTIONS: {
1642         struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
1643         return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
1644                              flow, max_ports, table_id);
1645     }
1646
1647     case OFPACT_WRITE_METADATA:
1648         return 0;
1649
1650     case OFPACT_METER: {
1651         uint32_t mid = ofpact_get_METER(a)->meter_id;
1652         if (mid == 0 || mid > OFPM13_MAX) {
1653             return OFPERR_OFPMMFC_INVALID_METER;
1654         }
1655         return 0;
1656     }
1657
1658     case OFPACT_GOTO_TABLE:
1659         if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1660             return OFPERR_OFPBRC_BAD_TABLE_ID;
1661         }
1662         return 0;
1663
1664     case OFPACT_GROUP:
1665         return 0;
1666
1667     default:
1668         NOT_REACHED();
1669     }
1670 }
1671
1672 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1673  * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1674  * switch with no more than 'max_ports' ports.
1675  *
1676  * May temporarily modify 'flow', but restores the changes before returning. */
1677 enum ofperr
1678 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1679               struct flow *flow, ofp_port_t max_ports, uint8_t table_id)
1680 {
1681     const struct ofpact *a;
1682     ovs_be16 dl_type = flow->dl_type;
1683     enum ofperr error = 0;
1684
1685     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1686         error = ofpact_check__(a, flow, max_ports, table_id);
1687         if (error) {
1688             break;
1689         }
1690     }
1691     flow->dl_type = dl_type; /* Restore. */
1692     return error;
1693 }
1694
1695 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1696  * in the appropriate order as defined by the OpenFlow spec. */
1697 enum ofperr
1698 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1699 {
1700     const struct ofpact *a;
1701     enum ovs_instruction_type inst;
1702
1703     inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1704     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1705         enum ovs_instruction_type next;
1706
1707         next = ovs_instruction_type_from_ofpact_type(a->type);
1708         if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
1709             ? next < inst
1710             : next <= inst) {
1711             const char *name = ovs_instruction_name_from_type(inst);
1712             const char *next_name = ovs_instruction_name_from_type(next);
1713
1714             if (next == inst) {
1715                 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1716                           "1.1+ compatibility", name);
1717             } else {
1718                 VLOG_WARN("invalid instruction ordering: %s must appear "
1719                           "before %s, for OpenFlow 1.1+ compatibility",
1720                           next_name, name);
1721             }
1722             return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1723         }
1724
1725         inst = next;
1726     }
1727
1728     return 0;
1729 }
1730 \f
1731 /* Converting ofpacts to Nicira OpenFlow extensions. */
1732
1733 static void
1734 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1735                                 struct ofpbuf *out)
1736 {
1737     struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1738
1739     naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1740                                            output_reg->src.n_bits);
1741     naor->src = htonl(output_reg->src.field->nxm_header);
1742     naor->max_len = htons(output_reg->max_len);
1743 }
1744
1745 static void
1746 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1747                          struct ofpbuf *out)
1748 {
1749     struct nx_action_resubmit *nar;
1750
1751     if (resubmit->table_id == 0xff
1752         && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1753         nar = ofputil_put_NXAST_RESUBMIT(out);
1754     } else {
1755         nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1756         nar->table = resubmit->table_id;
1757     }
1758     nar->in_port = htons(ofp_to_u16(resubmit->in_port));
1759 }
1760
1761 static void
1762 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1763                            struct ofpbuf *out)
1764 {
1765     uint64_t tun_id = tunnel->tun_id;
1766
1767     if (tun_id <= UINT32_MAX
1768         && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1769         ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1770     } else {
1771         ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1772     }
1773 }
1774
1775 static void
1776 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1777                                struct ofpbuf *out)
1778 {
1779     struct nx_action_write_metadata *nawm;
1780
1781     nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1782     nawm->metadata = om->metadata;
1783     nawm->mask = om->mask;
1784 }
1785
1786 static void
1787 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1788 {
1789     size_t start_ofs = out->size;
1790     struct nx_action_note *nan;
1791     unsigned int remainder;
1792     unsigned int len;
1793
1794     nan = ofputil_put_NXAST_NOTE(out);
1795     out->size -= sizeof nan->note;
1796
1797     ofpbuf_put(out, note->data, note->length);
1798
1799     len = out->size - start_ofs;
1800     remainder = len % OFP_ACTION_ALIGN;
1801     if (remainder) {
1802         ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1803     }
1804     nan = ofpbuf_at(out, start_ofs, sizeof *nan);
1805     nan->len = htons(out->size - start_ofs);
1806 }
1807
1808 static void
1809 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1810                            struct ofpbuf *out)
1811 {
1812     struct nx_action_controller *nac;
1813
1814     nac = ofputil_put_NXAST_CONTROLLER(out);
1815     nac->max_len = htons(oc->max_len);
1816     nac->controller_id = htons(oc->controller_id);
1817     nac->reason = oc->reason;
1818 }
1819
1820 static void
1821 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1822                         struct ofpbuf *out)
1823 {
1824     if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1825         ofputil_put_NXAST_DEC_TTL(out);
1826     } else {
1827         struct nx_action_cnt_ids *nac_ids =
1828             ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1829         int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1830         ovs_be16 *ids;
1831         size_t i;
1832
1833         nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1834         nac_ids->n_controllers = htons(oc_ids->n_controllers);
1835
1836         ids = ofpbuf_put_zeros(out, ids_len);
1837         for (i = 0; i < oc_ids->n_controllers; i++) {
1838             ids[i] = htons(oc_ids->cnt_ids[i]);
1839         }
1840     }
1841 }
1842
1843 static void
1844 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1845                             struct ofpbuf *out)
1846 {
1847     struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1848     naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1849     naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1850 }
1851
1852 static void
1853 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1854                        struct ofpbuf *out)
1855 {
1856     struct nx_action_sample *nas;
1857
1858     nas = ofputil_put_NXAST_SAMPLE(out);
1859     nas->probability = htons(os->probability);
1860     nas->collector_set_id = htonl(os->collector_set_id);
1861     nas->obs_domain_id = htonl(os->obs_domain_id);
1862     nas->obs_point_id = htonl(os->obs_point_id);
1863 }
1864
1865 static void
1866 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1867 {
1868     switch (a->type) {
1869     case OFPACT_CONTROLLER:
1870         ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1871         break;
1872
1873     case OFPACT_OUTPUT_REG:
1874         ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1875         break;
1876
1877     case OFPACT_BUNDLE:
1878         bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1879         break;
1880
1881     case OFPACT_REG_MOVE:
1882         nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1883         break;
1884
1885     case OFPACT_REG_LOAD:
1886         nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1887         break;
1888
1889     case OFPACT_STACK_PUSH:
1890         nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1891         break;
1892
1893     case OFPACT_STACK_POP:
1894         nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1895         break;
1896
1897     case OFPACT_DEC_TTL:
1898         ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1899         break;
1900
1901     case OFPACT_SET_MPLS_TTL:
1902         ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1903             = ofpact_get_SET_MPLS_TTL(a)->ttl;
1904         break;
1905
1906     case OFPACT_DEC_MPLS_TTL:
1907         ofputil_put_NXAST_DEC_MPLS_TTL(out);
1908         break;
1909
1910     case OFPACT_SET_TUNNEL:
1911         ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1912         break;
1913
1914     case OFPACT_WRITE_METADATA:
1915         ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1916         break;
1917
1918     case OFPACT_SET_QUEUE:
1919         ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1920             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1921         break;
1922
1923     case OFPACT_POP_QUEUE:
1924         ofputil_put_NXAST_POP_QUEUE(out);
1925         break;
1926
1927     case OFPACT_FIN_TIMEOUT:
1928         ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1929         break;
1930
1931     case OFPACT_RESUBMIT:
1932         ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1933         break;
1934
1935     case OFPACT_LEARN:
1936         learn_to_nxast(ofpact_get_LEARN(a), out);
1937         break;
1938
1939     case OFPACT_MULTIPATH:
1940         multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1941         break;
1942
1943     case OFPACT_NOTE:
1944         ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1945         break;
1946
1947     case OFPACT_EXIT:
1948         ofputil_put_NXAST_EXIT(out);
1949         break;
1950
1951     case OFPACT_PUSH_MPLS:
1952         ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1953             ofpact_get_PUSH_MPLS(a)->ethertype;
1954         break;
1955
1956     case OFPACT_POP_MPLS:
1957         ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1958             ofpact_get_POP_MPLS(a)->ethertype;
1959         break;
1960
1961     case OFPACT_SAMPLE:
1962         ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1963         break;
1964
1965     case OFPACT_GROUP:
1966     case OFPACT_OUTPUT:
1967     case OFPACT_ENQUEUE:
1968     case OFPACT_SET_VLAN_VID:
1969     case OFPACT_SET_VLAN_PCP:
1970     case OFPACT_STRIP_VLAN:
1971     case OFPACT_PUSH_VLAN:
1972     case OFPACT_SET_ETH_SRC:
1973     case OFPACT_SET_ETH_DST:
1974     case OFPACT_SET_IPV4_SRC:
1975     case OFPACT_SET_IPV4_DST:
1976     case OFPACT_SET_IP_DSCP:
1977     case OFPACT_SET_IP_ECN:
1978     case OFPACT_SET_IP_TTL:
1979     case OFPACT_SET_L4_SRC_PORT:
1980     case OFPACT_SET_L4_DST_PORT:
1981     case OFPACT_WRITE_ACTIONS:
1982     case OFPACT_CLEAR_ACTIONS:
1983     case OFPACT_GOTO_TABLE:
1984     case OFPACT_METER:
1985         NOT_REACHED();
1986     }
1987 }
1988 \f
1989 /* Converting ofpacts to OpenFlow 1.0. */
1990
1991 static void
1992 ofpact_output_to_openflow10(const struct ofpact_output *output,
1993                             struct ofpbuf *out)
1994 {
1995     struct ofp10_action_output *oao;
1996
1997     oao = ofputil_put_OFPAT10_OUTPUT(out);
1998     oao->port = htons(ofp_to_u16(output->port));
1999     oao->max_len = htons(output->max_len);
2000 }
2001
2002 static void
2003 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2004                              struct ofpbuf *out)
2005 {
2006     struct ofp10_action_enqueue *oae;
2007
2008     oae = ofputil_put_OFPAT10_ENQUEUE(out);
2009     oae->port = htons(ofp_to_u16(enqueue->port));
2010     oae->queue_id = htonl(enqueue->queue);
2011 }
2012
2013 static void
2014 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2015 {
2016     switch (a->type) {
2017     case OFPACT_OUTPUT:
2018         ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2019         break;
2020
2021     case OFPACT_ENQUEUE:
2022         ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2023         break;
2024
2025     case OFPACT_SET_VLAN_VID:
2026         ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2027             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2028         break;
2029
2030     case OFPACT_SET_VLAN_PCP:
2031         ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2032             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2033         break;
2034
2035     case OFPACT_STRIP_VLAN:
2036         ofputil_put_OFPAT10_STRIP_VLAN(out);
2037         break;
2038
2039     case OFPACT_SET_ETH_SRC:
2040         memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2041                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2042         break;
2043
2044     case OFPACT_SET_ETH_DST:
2045         memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2046                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2047         break;
2048
2049     case OFPACT_SET_IPV4_SRC:
2050         ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2051             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2052         break;
2053
2054     case OFPACT_SET_IPV4_DST:
2055         ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2056             = ofpact_get_SET_IPV4_DST(a)->ipv4;
2057         break;
2058
2059     case OFPACT_SET_IP_DSCP:
2060         ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2061             = ofpact_get_SET_IP_DSCP(a)->dscp;
2062         break;
2063
2064     case OFPACT_SET_L4_SRC_PORT:
2065         ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2066             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2067         break;
2068
2069     case OFPACT_SET_L4_DST_PORT:
2070         ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2071             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2072         break;
2073
2074     case OFPACT_PUSH_VLAN:
2075     case OFPACT_CLEAR_ACTIONS:
2076     case OFPACT_WRITE_ACTIONS:
2077     case OFPACT_GOTO_TABLE:
2078     case OFPACT_METER:
2079         /* XXX */
2080         break;
2081
2082     case OFPACT_GROUP:
2083         break;
2084
2085     case OFPACT_CONTROLLER:
2086     case OFPACT_OUTPUT_REG:
2087     case OFPACT_BUNDLE:
2088     case OFPACT_REG_MOVE:
2089     case OFPACT_REG_LOAD:
2090     case OFPACT_STACK_PUSH:
2091     case OFPACT_STACK_POP:
2092     case OFPACT_DEC_TTL:
2093     case OFPACT_SET_IP_ECN:
2094     case OFPACT_SET_IP_TTL:
2095     case OFPACT_SET_MPLS_TTL:
2096     case OFPACT_DEC_MPLS_TTL:
2097     case OFPACT_SET_TUNNEL:
2098     case OFPACT_WRITE_METADATA:
2099     case OFPACT_SET_QUEUE:
2100     case OFPACT_POP_QUEUE:
2101     case OFPACT_FIN_TIMEOUT:
2102     case OFPACT_RESUBMIT:
2103     case OFPACT_LEARN:
2104     case OFPACT_MULTIPATH:
2105     case OFPACT_NOTE:
2106     case OFPACT_EXIT:
2107     case OFPACT_PUSH_MPLS:
2108     case OFPACT_POP_MPLS:
2109     case OFPACT_SAMPLE:
2110         ofpact_to_nxast(a, out);
2111         break;
2112     }
2113 }
2114
2115 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
2116  * actions in 'openflow', appending the actions to any existing data in
2117  * 'openflow'. */
2118 void
2119 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
2120                        struct ofpbuf *openflow)
2121 {
2122     const struct ofpact *a;
2123
2124     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2125         ofpact_to_openflow10(a, openflow);
2126     }
2127 }
2128 \f
2129 /* Converting ofpacts to OpenFlow 1.1. */
2130
2131 static void
2132 ofpact_output_to_openflow11(const struct ofpact_output *output,
2133                             struct ofpbuf *out)
2134 {
2135     struct ofp11_action_output *oao;
2136
2137     oao = ofputil_put_OFPAT11_OUTPUT(out);
2138     oao->port = ofputil_port_to_ofp11(output->port);
2139     oao->max_len = htons(output->max_len);
2140 }
2141
2142 static void
2143 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2144                              struct ofpbuf *out)
2145 {
2146     if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2147         && (!dec_ttl->ofpact.compat ||
2148             dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2149         ofputil_put_OFPAT11_DEC_NW_TTL(out);
2150     } else {
2151         ofpact_dec_ttl_to_nxast(dec_ttl, out);
2152     }
2153 }
2154
2155 static void
2156 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2157 {
2158     switch (a->type) {
2159     case OFPACT_OUTPUT:
2160         return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2161
2162     case OFPACT_ENQUEUE:
2163         /* XXX */
2164         break;
2165
2166     case OFPACT_SET_VLAN_VID:
2167         ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2168             = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2169         break;
2170
2171     case OFPACT_SET_VLAN_PCP:
2172         ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2173             = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2174         break;
2175
2176     case OFPACT_STRIP_VLAN:
2177         ofputil_put_OFPAT11_POP_VLAN(out);
2178         break;
2179
2180     case OFPACT_PUSH_VLAN:
2181         /* XXX ETH_TYPE_VLAN_8021AD case */
2182         ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2183             htons(ETH_TYPE_VLAN_8021Q);
2184         break;
2185
2186     case OFPACT_SET_QUEUE:
2187         ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2188             = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2189         break;
2190
2191     case OFPACT_SET_ETH_SRC:
2192         memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2193                ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2194         break;
2195
2196     case OFPACT_SET_ETH_DST:
2197         memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2198                ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2199         break;
2200
2201     case OFPACT_SET_IPV4_SRC:
2202         ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2203             = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2204         break;
2205
2206     case OFPACT_SET_IPV4_DST:
2207         ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2208             = ofpact_get_SET_IPV4_DST(a)->ipv4;
2209         break;
2210
2211     case OFPACT_SET_IP_DSCP:
2212         ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2213             = ofpact_get_SET_IP_DSCP(a)->dscp;
2214         break;
2215
2216     case OFPACT_SET_IP_ECN:
2217         ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2218             = ofpact_get_SET_IP_ECN(a)->ecn;
2219         break;
2220
2221     case OFPACT_SET_IP_TTL:
2222         ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2223             = ofpact_get_SET_IP_TTL(a)->ttl;
2224         break;
2225
2226     case OFPACT_SET_L4_SRC_PORT:
2227         ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2228             = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2229         break;
2230
2231     case OFPACT_SET_L4_DST_PORT:
2232         ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2233             = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2234         break;
2235
2236     case OFPACT_DEC_TTL:
2237         ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2238         break;
2239
2240     case OFPACT_SET_MPLS_TTL:
2241         ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2242             = ofpact_get_SET_MPLS_TTL(a)->ttl;
2243         break;
2244
2245     case OFPACT_DEC_MPLS_TTL:
2246         ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2247         break;
2248
2249     case OFPACT_WRITE_METADATA:
2250         /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2251         break;
2252
2253     case OFPACT_PUSH_MPLS:
2254         ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2255             ofpact_get_PUSH_MPLS(a)->ethertype;
2256         break;
2257
2258     case OFPACT_POP_MPLS:
2259         ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2260             ofpact_get_POP_MPLS(a)->ethertype;
2261
2262         break;
2263
2264     case OFPACT_CLEAR_ACTIONS:
2265     case OFPACT_WRITE_ACTIONS:
2266     case OFPACT_GOTO_TABLE:
2267     case OFPACT_METER:
2268         NOT_REACHED();
2269
2270     case OFPACT_GROUP:
2271         ofputil_put_OFPAT11_GROUP(out)->group_id =
2272             htonl(ofpact_get_GROUP(a)->group_id);
2273         break;
2274
2275     case OFPACT_CONTROLLER:
2276     case OFPACT_OUTPUT_REG:
2277     case OFPACT_BUNDLE:
2278     case OFPACT_REG_MOVE:
2279     case OFPACT_REG_LOAD:
2280     case OFPACT_STACK_PUSH:
2281     case OFPACT_STACK_POP:
2282     case OFPACT_SET_TUNNEL:
2283     case OFPACT_POP_QUEUE:
2284     case OFPACT_FIN_TIMEOUT:
2285     case OFPACT_RESUBMIT:
2286     case OFPACT_LEARN:
2287     case OFPACT_MULTIPATH:
2288     case OFPACT_NOTE:
2289     case OFPACT_EXIT:
2290     case OFPACT_SAMPLE:
2291         ofpact_to_nxast(a, out);
2292         break;
2293     }
2294 }
2295
2296 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
2297  * 1.1 actions in 'openflow', appending the actions to any existing data in
2298  * 'openflow'. */
2299 size_t
2300 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
2301                                size_t ofpacts_len, struct ofpbuf *openflow)
2302 {
2303     const struct ofpact *a;
2304     size_t start_size = openflow->size;
2305
2306     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2307         ofpact_to_openflow11(a, openflow);
2308     }
2309
2310     return openflow->size - start_size;
2311 }
2312
2313 static void
2314 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2315 {
2316     struct ofp11_instruction_actions *oia;
2317
2318     /* Update the instruction's length (or, if it's empty, delete it). */
2319     oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2320     if (openflow->size > ofs + sizeof *oia) {
2321         oia->len = htons(openflow->size - ofs);
2322     } else {
2323         openflow->size = ofs;
2324     }
2325 }
2326
2327 void
2328 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
2329                                     size_t ofpacts_len,
2330                                     struct ofpbuf *openflow)
2331 {
2332     const struct ofpact *a;
2333
2334     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2335         switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2336         case OVSINST_OFPIT11_CLEAR_ACTIONS:
2337             instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2338             break;
2339
2340         case OVSINST_OFPIT11_GOTO_TABLE: {
2341             struct ofp11_instruction_goto_table *oigt;
2342             oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2343             oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2344             memset(oigt->pad, 0, sizeof oigt->pad);
2345             break;
2346         }
2347
2348         case OVSINST_OFPIT11_WRITE_METADATA: {
2349             const struct ofpact_metadata *om;
2350             struct ofp11_instruction_write_metadata *oiwm;
2351
2352             om = ofpact_get_WRITE_METADATA(a);
2353             oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2354             oiwm->metadata = om->metadata;
2355             oiwm->metadata_mask = om->mask;
2356             break;
2357         }
2358
2359         case OVSINST_OFPIT13_METER: {
2360             const struct ofpact_meter *om;
2361             struct ofp13_instruction_meter *oim;
2362
2363             om = ofpact_get_METER(a);
2364             oim = instruction_put_OFPIT13_METER(openflow);
2365             oim->meter_id = htonl(om->meter_id);
2366             break;
2367         }
2368
2369         case OVSINST_OFPIT11_APPLY_ACTIONS: {
2370             const size_t ofs = openflow->size;
2371             const size_t ofpacts_len_left =
2372                 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2373             const struct ofpact *action;
2374             const struct ofpact *processed = a;
2375
2376             instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2377             OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2378                 if (ovs_instruction_type_from_ofpact_type(action->type)
2379                     != OVSINST_OFPIT11_APPLY_ACTIONS) {
2380                     break;
2381                 }
2382                 ofpact_to_openflow11(action, openflow);
2383                 processed = action;
2384             }
2385             ofpacts_update_instruction_actions(openflow, ofs);
2386             a = processed;
2387             break;
2388         }
2389
2390         case OVSINST_OFPIT11_WRITE_ACTIONS: {
2391             const size_t ofs = openflow->size;
2392             const struct ofpact_nest *on;
2393
2394             on = ofpact_get_WRITE_ACTIONS(a);
2395             instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2396             ofpacts_put_openflow11_actions(on->actions,
2397                                            ofpact_nest_get_action_len(on),
2398                                            openflow);
2399             ofpacts_update_instruction_actions(openflow, ofs);
2400
2401             break;
2402         }
2403         }
2404     }
2405 }
2406 \f
2407 /* Returns true if 'action' outputs to 'port', false otherwise. */
2408 static bool
2409 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2410 {
2411     switch (ofpact->type) {
2412     case OFPACT_OUTPUT:
2413         return ofpact_get_OUTPUT(ofpact)->port == port;
2414     case OFPACT_ENQUEUE:
2415         return ofpact_get_ENQUEUE(ofpact)->port == port;
2416     case OFPACT_CONTROLLER:
2417         return port == OFPP_CONTROLLER;
2418
2419     case OFPACT_OUTPUT_REG:
2420     case OFPACT_BUNDLE:
2421     case OFPACT_SET_VLAN_VID:
2422     case OFPACT_SET_VLAN_PCP:
2423     case OFPACT_STRIP_VLAN:
2424     case OFPACT_PUSH_VLAN:
2425     case OFPACT_SET_ETH_SRC:
2426     case OFPACT_SET_ETH_DST:
2427     case OFPACT_SET_IPV4_SRC:
2428     case OFPACT_SET_IPV4_DST:
2429     case OFPACT_SET_IP_DSCP:
2430     case OFPACT_SET_IP_ECN:
2431     case OFPACT_SET_IP_TTL:
2432     case OFPACT_SET_L4_SRC_PORT:
2433     case OFPACT_SET_L4_DST_PORT:
2434     case OFPACT_REG_MOVE:
2435     case OFPACT_REG_LOAD:
2436     case OFPACT_STACK_PUSH:
2437     case OFPACT_STACK_POP:
2438     case OFPACT_DEC_TTL:
2439     case OFPACT_SET_MPLS_TTL:
2440     case OFPACT_DEC_MPLS_TTL:
2441     case OFPACT_SET_TUNNEL:
2442     case OFPACT_WRITE_METADATA:
2443     case OFPACT_SET_QUEUE:
2444     case OFPACT_POP_QUEUE:
2445     case OFPACT_FIN_TIMEOUT:
2446     case OFPACT_RESUBMIT:
2447     case OFPACT_LEARN:
2448     case OFPACT_MULTIPATH:
2449     case OFPACT_NOTE:
2450     case OFPACT_EXIT:
2451     case OFPACT_PUSH_MPLS:
2452     case OFPACT_POP_MPLS:
2453     case OFPACT_SAMPLE:
2454     case OFPACT_CLEAR_ACTIONS:
2455     case OFPACT_WRITE_ACTIONS:
2456     case OFPACT_GOTO_TABLE:
2457     case OFPACT_METER:
2458     case OFPACT_GROUP:
2459     default:
2460         return false;
2461     }
2462 }
2463
2464 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2465  * to 'port', false otherwise. */
2466 bool
2467 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
2468                        ofp_port_t port)
2469 {
2470     const struct ofpact *a;
2471
2472     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2473         if (ofpact_outputs_to_port(a, port)) {
2474             return true;
2475         }
2476     }
2477
2478     return false;
2479 }
2480
2481 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2482  * to 'group', false otherwise. */
2483 bool
2484 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2485                         uint32_t group_id)
2486 {
2487     const struct ofpact *a;
2488
2489     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2490         if (a->type == OFPACT_GROUP
2491             && ofpact_get_GROUP(a)->group_id == group_id) {
2492             return true;
2493         }
2494     }
2495
2496     return false;
2497 }
2498
2499 bool
2500 ofpacts_equal(const struct ofpact *a, size_t a_len,
2501               const struct ofpact *b, size_t b_len)
2502 {
2503     return a_len == b_len && !memcmp(a, b, a_len);
2504 }
2505
2506 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2507  * 'ofpacts'.  If found, returns its meter ID; if not, returns 0.
2508  *
2509  * This function relies on the order of 'ofpacts' being correct (as checked by
2510  * ofpacts_verify()). */
2511 uint32_t
2512 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2513 {
2514     const struct ofpact *a;
2515
2516     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2517         enum ovs_instruction_type inst;
2518
2519         inst = ovs_instruction_type_from_ofpact_type(a->type);
2520         if (a->type == OFPACT_METER) {
2521             return ofpact_get_METER(a)->meter_id;
2522         } else if (inst > OVSINST_OFPIT13_METER) {
2523             break;
2524         }
2525     }
2526
2527     return 0;
2528 }
2529 \f
2530 /* Formatting ofpacts. */
2531
2532 static void
2533 print_note(const struct ofpact_note *note, struct ds *string)
2534 {
2535     size_t i;
2536
2537     ds_put_cstr(string, "note:");
2538     for (i = 0; i < note->length; i++) {
2539         if (i) {
2540             ds_put_char(string, '.');
2541         }
2542         ds_put_format(string, "%02"PRIx8, note->data[i]);
2543     }
2544 }
2545
2546 static void
2547 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2548               struct ds *s)
2549 {
2550     size_t i;
2551
2552     ds_put_cstr(s, "dec_ttl");
2553     if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2554         ds_put_cstr(s, "(");
2555         for (i = 0; i < ids->n_controllers; i++) {
2556             if (i) {
2557                 ds_put_cstr(s, ",");
2558             }
2559             ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2560         }
2561         ds_put_cstr(s, ")");
2562     }
2563 }
2564
2565 static void
2566 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2567                   struct ds *s)
2568 {
2569     ds_put_cstr(s, "fin_timeout(");
2570     if (fin_timeout->fin_idle_timeout) {
2571         ds_put_format(s, "idle_timeout=%"PRIu16",",
2572                       fin_timeout->fin_idle_timeout);
2573     }
2574     if (fin_timeout->fin_hard_timeout) {
2575         ds_put_format(s, "hard_timeout=%"PRIu16",",
2576                       fin_timeout->fin_hard_timeout);
2577     }
2578     ds_chomp(s, ',');
2579     ds_put_char(s, ')');
2580 }
2581
2582 static void
2583 ofpact_format(const struct ofpact *a, struct ds *s)
2584 {
2585     const struct ofpact_enqueue *enqueue;
2586     const struct ofpact_resubmit *resubmit;
2587     const struct ofpact_controller *controller;
2588     const struct ofpact_metadata *metadata;
2589     const struct ofpact_tunnel *tunnel;
2590     const struct ofpact_sample *sample;
2591     ofp_port_t port;
2592
2593     switch (a->type) {
2594     case OFPACT_OUTPUT:
2595         port = ofpact_get_OUTPUT(a)->port;
2596         if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
2597             ds_put_format(s, "output:%"PRIu16, port);
2598         } else {
2599             ofputil_format_port(port, s);
2600             if (port == OFPP_CONTROLLER) {
2601                 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2602             }
2603         }
2604         break;
2605
2606     case OFPACT_CONTROLLER:
2607         controller = ofpact_get_CONTROLLER(a);
2608         if (controller->reason == OFPR_ACTION &&
2609             controller->controller_id == 0) {
2610             ds_put_format(s, "CONTROLLER:%"PRIu16,
2611                           ofpact_get_CONTROLLER(a)->max_len);
2612         } else {
2613             enum ofp_packet_in_reason reason = controller->reason;
2614
2615             ds_put_cstr(s, "controller(");
2616             if (reason != OFPR_ACTION) {
2617                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2618
2619                 ds_put_format(s, "reason=%s,",
2620                               ofputil_packet_in_reason_to_string(
2621                                   reason, reasonbuf, sizeof reasonbuf));
2622             }
2623             if (controller->max_len != UINT16_MAX) {
2624                 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2625             }
2626             if (controller->controller_id != 0) {
2627                 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2628             }
2629             ds_chomp(s, ',');
2630             ds_put_char(s, ')');
2631         }
2632         break;
2633
2634     case OFPACT_ENQUEUE:
2635         enqueue = ofpact_get_ENQUEUE(a);
2636         ds_put_format(s, "enqueue:");
2637         ofputil_format_port(enqueue->port, s);
2638         ds_put_format(s, "q%"PRIu32, enqueue->queue);
2639         break;
2640
2641     case OFPACT_OUTPUT_REG:
2642         ds_put_cstr(s, "output:");
2643         mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2644         break;
2645
2646     case OFPACT_BUNDLE:
2647         bundle_format(ofpact_get_BUNDLE(a), s);
2648         break;
2649
2650     case OFPACT_SET_VLAN_VID:
2651         ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2652                       ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2653         break;
2654
2655     case OFPACT_SET_VLAN_PCP:
2656         ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2657                       ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2658         break;
2659
2660     case OFPACT_STRIP_VLAN:
2661         ds_put_cstr(s, "strip_vlan");
2662         break;
2663
2664     case OFPACT_PUSH_VLAN:
2665         /* XXX 802.1AD case*/
2666         ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2667         break;
2668
2669     case OFPACT_SET_ETH_SRC:
2670         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2671                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2672         break;
2673
2674     case OFPACT_SET_ETH_DST:
2675         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2676                       ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2677         break;
2678
2679     case OFPACT_SET_IPV4_SRC:
2680         ds_put_format(s, "mod_nw_src:"IP_FMT,
2681                       IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2682         break;
2683
2684     case OFPACT_SET_IPV4_DST:
2685         ds_put_format(s, "mod_nw_dst:"IP_FMT,
2686                       IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2687         break;
2688
2689     case OFPACT_SET_IP_DSCP:
2690         ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
2691         break;
2692
2693     case OFPACT_SET_IP_ECN:
2694         ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
2695         break;
2696
2697     case OFPACT_SET_IP_TTL:
2698         ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
2699         break;
2700
2701     case OFPACT_SET_L4_SRC_PORT:
2702         ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2703         break;
2704
2705     case OFPACT_SET_L4_DST_PORT:
2706         ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2707         break;
2708
2709     case OFPACT_REG_MOVE:
2710         nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2711         break;
2712
2713     case OFPACT_REG_LOAD:
2714         nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2715         break;
2716
2717     case OFPACT_STACK_PUSH:
2718         nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2719         break;
2720
2721     case OFPACT_STACK_POP:
2722         nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2723         break;
2724
2725     case OFPACT_DEC_TTL:
2726         print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2727         break;
2728
2729     case OFPACT_SET_MPLS_TTL:
2730         ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2731                       ofpact_get_SET_MPLS_TTL(a)->ttl);
2732         break;
2733
2734     case OFPACT_DEC_MPLS_TTL:
2735         ds_put_cstr(s, "dec_mpls_ttl");
2736         break;
2737
2738     case OFPACT_SET_TUNNEL:
2739         tunnel = ofpact_get_SET_TUNNEL(a);
2740         ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2741                       (tunnel->tun_id > UINT32_MAX
2742                        || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2743                       tunnel->tun_id);
2744         break;
2745
2746     case OFPACT_SET_QUEUE:
2747         ds_put_format(s, "set_queue:%"PRIu32,
2748                       ofpact_get_SET_QUEUE(a)->queue_id);
2749         break;
2750
2751     case OFPACT_POP_QUEUE:
2752         ds_put_cstr(s, "pop_queue");
2753         break;
2754
2755     case OFPACT_FIN_TIMEOUT:
2756         print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2757         break;
2758
2759     case OFPACT_RESUBMIT:
2760         resubmit = ofpact_get_RESUBMIT(a);
2761         if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2762             ds_put_cstr(s, "resubmit:");
2763             ofputil_format_port(resubmit->in_port, s);
2764         } else {
2765             ds_put_format(s, "resubmit(");
2766             if (resubmit->in_port != OFPP_IN_PORT) {
2767                 ofputil_format_port(resubmit->in_port, s);
2768             }
2769             ds_put_char(s, ',');
2770             if (resubmit->table_id != 255) {
2771                 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2772             }
2773             ds_put_char(s, ')');
2774         }
2775         break;
2776
2777     case OFPACT_LEARN:
2778         learn_format(ofpact_get_LEARN(a), s);
2779         break;
2780
2781     case OFPACT_MULTIPATH:
2782         multipath_format(ofpact_get_MULTIPATH(a), s);
2783         break;
2784
2785     case OFPACT_NOTE:
2786         print_note(ofpact_get_NOTE(a), s);
2787         break;
2788
2789     case OFPACT_PUSH_MPLS:
2790         ds_put_format(s, "push_mpls:0x%04"PRIx16,
2791                       ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2792         break;
2793
2794     case OFPACT_POP_MPLS:
2795         ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2796                       ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2797         break;
2798
2799     case OFPACT_EXIT:
2800         ds_put_cstr(s, "exit");
2801         break;
2802
2803     case OFPACT_SAMPLE:
2804         sample = ofpact_get_SAMPLE(a);
2805         ds_put_format(
2806             s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2807             ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2808             sample->probability, sample->collector_set_id,
2809             sample->obs_domain_id, sample->obs_point_id);
2810         break;
2811
2812     case OFPACT_WRITE_ACTIONS: {
2813         struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2814         ds_put_format(s, "%s(",
2815                       ovs_instruction_name_from_type(
2816                           OVSINST_OFPIT11_WRITE_ACTIONS));
2817         ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
2818         ds_put_char(s, ')');
2819         break;
2820     }
2821
2822     case OFPACT_CLEAR_ACTIONS:
2823         ds_put_format(s, "%s",
2824                       ovs_instruction_name_from_type(
2825                           OVSINST_OFPIT11_CLEAR_ACTIONS));
2826         break;
2827
2828     case OFPACT_WRITE_METADATA:
2829         metadata = ofpact_get_WRITE_METADATA(a);
2830         ds_put_format(s, "%s:%#"PRIx64,
2831                       ovs_instruction_name_from_type(
2832                           OVSINST_OFPIT11_WRITE_METADATA),
2833                       ntohll(metadata->metadata));
2834         if (metadata->mask != OVS_BE64_MAX) {
2835             ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2836         }
2837         break;
2838
2839     case OFPACT_GOTO_TABLE:
2840         ds_put_format(s, "%s:%"PRIu8,
2841                       ovs_instruction_name_from_type(
2842                           OVSINST_OFPIT11_GOTO_TABLE),
2843                       ofpact_get_GOTO_TABLE(a)->table_id);
2844         break;
2845
2846     case OFPACT_METER:
2847         ds_put_format(s, "%s:%"PRIu32,
2848                       ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
2849                       ofpact_get_METER(a)->meter_id);
2850         break;
2851
2852     case OFPACT_GROUP:
2853         ds_put_format(s, "group:%"PRIu32,
2854                       ofpact_get_GROUP(a)->group_id);
2855         break;
2856     }
2857 }
2858
2859 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2860  * 'ofpacts' to 'string'. */
2861 void
2862 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2863                struct ds *string)
2864 {
2865     if (!ofpacts_len) {
2866         ds_put_cstr(string, "drop");
2867     } else {
2868         const struct ofpact *a;
2869
2870         OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2871             if (a != ofpacts) {
2872                 ds_put_cstr(string, ",");
2873             }
2874
2875             /* XXX write-actions */
2876             ofpact_format(a, string);
2877         }
2878     }
2879 }
2880 \f
2881 /* Internal use by helpers. */
2882
2883 void *
2884 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2885 {
2886     struct ofpact *ofpact;
2887
2888     ofpact_pad(ofpacts);
2889     ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2890     ofpact_init(ofpact, type, len);
2891     return ofpact;
2892 }
2893
2894 void
2895 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2896 {
2897     memset(ofpact, 0, len);
2898     ofpact->type = type;
2899     ofpact->compat = OFPUTIL_ACTION_INVALID;
2900     ofpact->len = len;
2901 }
2902 \f
2903 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2904  * starting at 'ofpact'.
2905  *
2906  * This is the correct way to update a variable-length ofpact's length after
2907  * adding the variable-length part of the payload.  (See the large comment
2908  * near the end of ofp-actions.h for more information.) */
2909 void
2910 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2911 {
2912     ovs_assert(ofpact == ofpacts->l2);
2913     ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2914 }
2915
2916 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length.  Each
2917  * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2918  * client must call this itself after adding the final ofpact to an array of
2919  * them.
2920  *
2921  * (The consequences of failing to call this function are probably not dire.
2922  * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2923  * not dereference it.  That's undefined behavior, technically, but it will not
2924  * cause a real problem on common systems.  Still, it seems better to call
2925  * it.) */
2926 void
2927 ofpact_pad(struct ofpbuf *ofpacts)
2928 {
2929     unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2930     if (rem) {
2931         ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2932     }
2933 }
2934
2935 void
2936 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2937                       const void *src)
2938 {
2939     load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2940     load->dst.field = mf;
2941     load->dst.ofs = 0;
2942     load->dst.n_bits = mf->n_bits;
2943     bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2944                  &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);
2945 }