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