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