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