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