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