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