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