ofproto-dpif: Handle dest mirrors in compose_output_action().
[sliver-openvswitch.git] / ofproto / ofproto-dpif-xlate.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14
15 #include <config.h>
16
17 #include "ofproto/ofproto-dpif-xlate.h"
18
19 #include "bfd.h"
20 #include "bitmap.h"
21 #include "bond.h"
22 #include "bundle.h"
23 #include "byte-order.h"
24 #include "cfm.h"
25 #include "connmgr.h"
26 #include "coverage.h"
27 #include "dpif.h"
28 #include "dynamic-string.h"
29 #include "in-band.h"
30 #include "lacp.h"
31 #include "learn.h"
32 #include "mac-learning.h"
33 #include "meta-flow.h"
34 #include "multipath.h"
35 #include "netdev-vport.h"
36 #include "netlink.h"
37 #include "nx-match.h"
38 #include "odp-execute.h"
39 #include "ofp-actions.h"
40 #include "ofproto/ofproto-dpif-ipfix.h"
41 #include "ofproto/ofproto-dpif-sflow.h"
42 #include "ofproto/ofproto-dpif.h"
43 #include "tunnel.h"
44 #include "vlog.h"
45
46 COVERAGE_DEFINE(ofproto_dpif_xlate);
47
48 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
49
50 /* Maximum depth of flow table recursion (due to resubmit actions) in a
51  * flow translation. */
52 #define MAX_RESUBMIT_RECURSION 64
53
54 struct xlate_ctx {
55     struct xlate_in *xin;
56     struct xlate_out *xout;
57
58     struct ofproto_dpif *ofproto;
59
60     /* Flow at the last commit. */
61     struct flow base_flow;
62
63     /* Tunnel IP destination address as received.  This is stored separately
64      * as the base_flow.tunnel is cleared on init to reflect the datapath
65      * behavior.  Used to make sure not to send tunneled output to ourselves,
66      * which might lead to an infinite loop.  This could happen easily
67      * if a tunnel is marked as 'ip_remote=flow', and the flow does not
68      * actually set the tun_dst field. */
69     ovs_be32 orig_tunnel_ip_dst;
70
71     /* Stack for the push and pop actions.  Each stack element is of type
72      * "union mf_subvalue". */
73     union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
74     struct ofpbuf stack;
75
76     /* The rule that we are currently translating, or NULL. */
77     struct rule_dpif *rule;
78
79     int recurse;                /* Recursion level, via xlate_table_action. */
80     bool max_resubmit_trigger;  /* Recursed too deeply during translation. */
81     uint32_t orig_skb_priority; /* Priority when packet arrived. */
82     uint8_t table_id;           /* OpenFlow table ID where flow was found. */
83     uint32_t sflow_n_outputs;   /* Number of output ports. */
84     odp_port_t sflow_odp_port;  /* Output port for composing sFlow action. */
85     uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
86     bool exit;                  /* No further actions should be processed. */
87 };
88
89 /* A controller may use OFPP_NONE as the ingress port to indicate that
90  * it did not arrive on a "real" port.  'ofpp_none_bundle' exists for
91  * when an input bundle is needed for validation (e.g., mirroring or
92  * OFPP_NORMAL processing).  It is not connected to an 'ofproto' or have
93  * any 'port' structs, so care must be taken when dealing with it. */
94 static struct ofbundle ofpp_none_bundle = {
95     .name      = "OFPP_NONE",
96     .vlan_mode = PORT_VLAN_TRUNK
97 };
98
99 static bool may_receive(const struct ofport_dpif *, struct xlate_ctx *);
100 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
101                              struct xlate_ctx *);
102 static void xlate_normal(struct xlate_ctx *);
103 static void xlate_report(struct xlate_ctx *, const char *);
104 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
105                                uint8_t table_id, bool may_packet_in);
106 static bool input_vid_is_valid(uint16_t vid, struct ofbundle *, bool warn);
107 static uint16_t input_vid_to_vlan(const struct ofbundle *, uint16_t vid);
108 static void output_normal(struct xlate_ctx *, const struct ofbundle *,
109                           uint16_t vlan);
110 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
111
112 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
113
114 static bool
115 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
116 {
117     return (bundle->vlan_mode != PORT_VLAN_ACCESS
118             && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
119 }
120
121 static bool
122 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
123 {
124     return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
125 }
126
127 static bool
128 vlan_is_mirrored(const struct ofmirror *m, int vlan)
129 {
130     return !m->vlans || bitmap_is_set(m->vlans, vlan);
131 }
132
133 static struct ofbundle *
134 lookup_input_bundle(const struct ofproto_dpif *ofproto, ofp_port_t in_port,
135                     bool warn, struct ofport_dpif **in_ofportp)
136 {
137     struct ofport_dpif *ofport;
138
139     /* Find the port and bundle for the received packet. */
140     ofport = get_ofp_port(ofproto, in_port);
141     if (in_ofportp) {
142         *in_ofportp = ofport;
143     }
144     if (ofport && ofport->bundle) {
145         return ofport->bundle;
146     }
147
148     /* Special-case OFPP_NONE, which a controller may use as the ingress
149      * port for traffic that it is sourcing. */
150     if (in_port == OFPP_NONE) {
151         return &ofpp_none_bundle;
152     }
153
154     /* Odd.  A few possible reasons here:
155      *
156      * - We deleted a port but there are still a few packets queued up
157      *   from it.
158      *
159      * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
160      *   we don't know about.
161      *
162      * - The ofproto client didn't configure the port as part of a bundle.
163      *   This is particularly likely to happen if a packet was received on the
164      *   port after it was created, but before the client had a chance to
165      *   configure its bundle.
166      */
167     if (warn) {
168         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
169
170         VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
171                      "port %"PRIu16, ofproto->up.name, in_port);
172     }
173     return NULL;
174 }
175
176 static void
177 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
178 {
179     struct ofproto_dpif *ofproto = ctx->ofproto;
180     mirror_mask_t mirrors;
181     struct ofbundle *in_bundle;
182     uint16_t vlan;
183     uint16_t vid;
184
185     mirrors = ctx->xout->mirrors;
186     ctx->xout->mirrors = 0;
187
188     in_bundle = lookup_input_bundle(ctx->ofproto, orig_flow->in_port.ofp_port,
189                                     ctx->xin->packet != NULL, NULL);
190     if (!in_bundle) {
191         return;
192     }
193     mirrors |= in_bundle->src_mirrors;
194
195     /* Drop frames on bundles reserved for mirroring. */
196     if (in_bundle->mirror_out) {
197         if (ctx->xin->packet != NULL) {
198             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
199             VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
200                          "%s, which is reserved exclusively for mirroring",
201                          ctx->ofproto->up.name, in_bundle->name);
202         }
203         return;
204     }
205
206     /* Check VLAN. */
207     vid = vlan_tci_to_vid(orig_flow->vlan_tci);
208     if (!input_vid_is_valid(vid, in_bundle, ctx->xin->packet != NULL)) {
209         return;
210     }
211     vlan = input_vid_to_vlan(in_bundle, vid);
212
213     if (!mirrors) {
214         return;
215     }
216
217     /* Restore the original packet before adding the mirror actions. */
218     ctx->xin->flow = *orig_flow;
219
220     while (mirrors) {
221         struct ofmirror *m;
222
223         m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
224
225         if (m->vlans) {
226             ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
227         }
228
229         if (!vlan_is_mirrored(m, vlan)) {
230             mirrors = zero_rightmost_1bit(mirrors);
231             continue;
232         }
233
234         mirrors &= ~m->dup_mirrors;
235         ctx->xout->mirrors |= m->dup_mirrors;
236         if (m->out) {
237             output_normal(ctx, m->out, vlan);
238         } else if (vlan != m->out_vlan
239                    && !eth_addr_is_reserved(orig_flow->dl_dst)) {
240             struct ofbundle *bundle;
241
242             HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
243                 if (ofbundle_includes_vlan(bundle, m->out_vlan)
244                     && !bundle->mirror_out) {
245                     output_normal(ctx, bundle, m->out_vlan);
246                 }
247             }
248         }
249     }
250 }
251
252 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
253  * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
254  * the bundle on which the packet was received, returns the VLAN to which the
255  * packet belongs.
256  *
257  * Both 'vid' and the return value are in the range 0...4095. */
258 static uint16_t
259 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
260 {
261     switch (in_bundle->vlan_mode) {
262     case PORT_VLAN_ACCESS:
263         return in_bundle->vlan;
264         break;
265
266     case PORT_VLAN_TRUNK:
267         return vid;
268
269     case PORT_VLAN_NATIVE_UNTAGGED:
270     case PORT_VLAN_NATIVE_TAGGED:
271         return vid ? vid : in_bundle->vlan;
272
273     default:
274         NOT_REACHED();
275     }
276 }
277
278 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
279  * If so, returns true.  Otherwise, returns false and, if 'warn' is true, logs
280  * a warning.
281  *
282  * 'vid' should be the VID obtained from the 802.1Q header that was received as
283  * part of a packet (specify 0 if there was no 802.1Q header), in the range
284  * 0...4095. */
285 static bool
286 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
287 {
288     /* Allow any VID on the OFPP_NONE port. */
289     if (in_bundle == &ofpp_none_bundle) {
290         return true;
291     }
292
293     switch (in_bundle->vlan_mode) {
294     case PORT_VLAN_ACCESS:
295         if (vid) {
296             if (warn) {
297                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
298                 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
299                              "packet received on port %s configured as VLAN "
300                              "%"PRIu16" access port",
301                              in_bundle->ofproto->up.name, vid,
302                              in_bundle->name, in_bundle->vlan);
303             }
304             return false;
305         }
306         return true;
307
308     case PORT_VLAN_NATIVE_UNTAGGED:
309     case PORT_VLAN_NATIVE_TAGGED:
310         if (!vid) {
311             /* Port must always carry its native VLAN. */
312             return true;
313         }
314         /* Fall through. */
315     case PORT_VLAN_TRUNK:
316         if (!ofbundle_includes_vlan(in_bundle, vid)) {
317             if (warn) {
318                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
319                 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
320                              "received on port %s not configured for trunking "
321                              "VLAN %"PRIu16,
322                              in_bundle->ofproto->up.name, vid,
323                              in_bundle->name, vid);
324             }
325             return false;
326         }
327         return true;
328
329     default:
330         NOT_REACHED();
331     }
332
333 }
334
335 /* Given 'vlan', the VLAN that a packet belongs to, and
336  * 'out_bundle', a bundle on which the packet is to be output, returns the VID
337  * that should be included in the 802.1Q header.  (If the return value is 0,
338  * then the 802.1Q header should only be included in the packet if there is a
339  * nonzero PCP.)
340  *
341  * Both 'vlan' and the return value are in the range 0...4095. */
342 static uint16_t
343 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
344 {
345     switch (out_bundle->vlan_mode) {
346     case PORT_VLAN_ACCESS:
347         return 0;
348
349     case PORT_VLAN_TRUNK:
350     case PORT_VLAN_NATIVE_TAGGED:
351         return vlan;
352
353     case PORT_VLAN_NATIVE_UNTAGGED:
354         return vlan == out_bundle->vlan ? 0 : vlan;
355
356     default:
357         NOT_REACHED();
358     }
359 }
360
361 static void
362 output_normal(struct xlate_ctx *ctx, const struct ofbundle *out_bundle,
363               uint16_t vlan)
364 {
365     ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
366     struct ofport_dpif *port;
367     uint16_t vid;
368     ovs_be16 tci, old_tci;
369
370     vid = output_vlan_to_vid(out_bundle, vlan);
371     if (!out_bundle->bond) {
372         port = ofbundle_get_a_port(out_bundle);
373     } else {
374         port = bond_choose_output_slave(out_bundle->bond, &ctx->xin->flow,
375                                         &ctx->xout->wc, vid, &ctx->xout->tags);
376         if (!port) {
377             /* No slaves enabled, so drop packet. */
378             return;
379         }
380     }
381
382     old_tci = *flow_tci;
383     tci = htons(vid);
384     if (tci || out_bundle->use_priority_tags) {
385         tci |= *flow_tci & htons(VLAN_PCP_MASK);
386         if (tci) {
387             tci |= htons(VLAN_CFI);
388         }
389     }
390     *flow_tci = tci;
391
392     compose_output_action(ctx, port->up.ofp_port);
393     *flow_tci = old_tci;
394 }
395
396 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
397  * migration.  Older Citrix-patched Linux DomU used gratuitous ARP replies to
398  * indicate this; newer upstream kernels use gratuitous ARP requests. */
399 static bool
400 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
401 {
402     if (flow->dl_type != htons(ETH_TYPE_ARP)) {
403         return false;
404     }
405
406     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
407     if (!eth_addr_is_broadcast(flow->dl_dst)) {
408         return false;
409     }
410
411     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
412     if (flow->nw_proto == ARP_OP_REPLY) {
413         return true;
414     } else if (flow->nw_proto == ARP_OP_REQUEST) {
415         memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
416         memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
417
418         return flow->nw_src == flow->nw_dst;
419     } else {
420         return false;
421     }
422 }
423
424 static void
425 update_learning_table(struct ofproto_dpif *ofproto,
426                       const struct flow *flow, struct flow_wildcards *wc,
427                       int vlan, struct ofbundle *in_bundle)
428 {
429     struct mac_entry *mac;
430
431     /* Don't learn the OFPP_NONE port. */
432     if (in_bundle == &ofpp_none_bundle) {
433         return;
434     }
435
436     if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
437         return;
438     }
439
440     mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
441     if (is_gratuitous_arp(flow, wc)) {
442         /* We don't want to learn from gratuitous ARP packets that are
443          * reflected back over bond slaves so we lock the learning table. */
444         if (!in_bundle->bond) {
445             mac_entry_set_grat_arp_lock(mac);
446         } else if (mac_entry_is_grat_arp_locked(mac)) {
447             return;
448         }
449     }
450
451     if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
452         /* The log messages here could actually be useful in debugging,
453          * so keep the rate limit relatively high. */
454         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
455         VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
456                     "on port %s in VLAN %d",
457                     ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
458                     in_bundle->name, vlan);
459
460         mac->port.p = in_bundle;
461         mac_learning_changed(ofproto->ml, mac);
462     }
463 }
464
465 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
466  * dropped.  Returns true if they may be forwarded, false if they should be
467  * dropped.
468  *
469  * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
470  * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
471  *
472  * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
473  * returned by input_vid_to_vlan().  It must be a valid VLAN for 'in_port', as
474  * checked by input_vid_is_valid().
475  *
476  * May also add tags to '*tags', although the current implementation only does
477  * so in one special case.
478  */
479 static bool
480 is_admissible(struct xlate_ctx *ctx, struct ofport_dpif *in_port,
481               uint16_t vlan)
482 {
483     struct ofproto_dpif *ofproto = ctx->ofproto;
484     struct flow *flow = &ctx->xin->flow;
485     struct ofbundle *in_bundle = in_port->bundle;
486
487     /* Drop frames for reserved multicast addresses
488      * only if forward_bpdu option is absent. */
489     if (!ofproto->up.forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
490         xlate_report(ctx, "packet has reserved destination MAC, dropping");
491         return false;
492     }
493
494     if (in_bundle->bond) {
495         struct mac_entry *mac;
496
497         switch (bond_check_admissibility(in_bundle->bond, in_port,
498                                          flow->dl_dst, &ctx->xout->tags)) {
499         case BV_ACCEPT:
500             break;
501
502         case BV_DROP:
503             xlate_report(ctx, "bonding refused admissibility, dropping");
504             return false;
505
506         case BV_DROP_IF_MOVED:
507             mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
508             if (mac && mac->port.p != in_bundle &&
509                 (!is_gratuitous_arp(flow, &ctx->xout->wc)
510                  || mac_entry_is_grat_arp_locked(mac))) {
511                 xlate_report(ctx, "SLB bond thinks this packet looped back, "
512                             "dropping");
513                 return false;
514             }
515             break;
516         }
517     }
518
519     return true;
520 }
521
522 static void
523 xlate_normal(struct xlate_ctx *ctx)
524 {
525     struct flow_wildcards *wc = &ctx->xout->wc;
526     struct flow *flow = &ctx->xin->flow;
527     struct ofport_dpif *in_port;
528     struct ofbundle *in_bundle;
529     struct mac_entry *mac;
530     uint16_t vlan;
531     uint16_t vid;
532
533     ctx->xout->has_normal = true;
534
535     memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
536     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
537     wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
538
539     in_bundle = lookup_input_bundle(ctx->ofproto, flow->in_port.ofp_port,
540                                     ctx->xin->packet != NULL, &in_port);
541     if (!in_bundle) {
542         xlate_report(ctx, "no input bundle, dropping");
543         return;
544     }
545
546     /* Drop malformed frames. */
547     if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
548         !(flow->vlan_tci & htons(VLAN_CFI))) {
549         if (ctx->xin->packet != NULL) {
550             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
551             VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
552                          "VLAN tag received on port %s",
553                          ctx->ofproto->up.name, in_bundle->name);
554         }
555         xlate_report(ctx, "partial VLAN tag, dropping");
556         return;
557     }
558
559     /* Drop frames on bundles reserved for mirroring. */
560     if (in_bundle->mirror_out) {
561         if (ctx->xin->packet != NULL) {
562             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
563             VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
564                          "%s, which is reserved exclusively for mirroring",
565                          ctx->ofproto->up.name, in_bundle->name);
566         }
567         xlate_report(ctx, "input port is mirror output port, dropping");
568         return;
569     }
570
571     /* Check VLAN. */
572     vid = vlan_tci_to_vid(flow->vlan_tci);
573     if (!input_vid_is_valid(vid, in_bundle, ctx->xin->packet != NULL)) {
574         xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
575         return;
576     }
577     vlan = input_vid_to_vlan(in_bundle, vid);
578
579     /* Check other admissibility requirements. */
580     if (in_port && !is_admissible(ctx, in_port, vlan)) {
581         return;
582     }
583
584     /* Learn source MAC. */
585     if (ctx->xin->may_learn) {
586         update_learning_table(ctx->ofproto, flow, wc, vlan, in_bundle);
587     }
588
589     /* Determine output bundle. */
590     mac = mac_learning_lookup(ctx->ofproto->ml, flow->dl_dst, vlan,
591                               &ctx->xout->tags);
592     if (mac) {
593         if (mac->port.p != in_bundle) {
594             xlate_report(ctx, "forwarding to learned port");
595             output_normal(ctx, mac->port.p, vlan);
596         } else {
597             xlate_report(ctx, "learned port is input port, dropping");
598         }
599     } else {
600         struct ofbundle *bundle;
601
602         xlate_report(ctx, "no learned MAC for destination, flooding");
603         HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
604             if (bundle != in_bundle
605                 && ofbundle_includes_vlan(bundle, vlan)
606                 && bundle->floodable
607                 && !bundle->mirror_out) {
608                 output_normal(ctx, bundle, vlan);
609             }
610         }
611         ctx->xout->nf_output_iface = NF_OUT_FLOOD;
612     }
613 }
614
615 /* Compose SAMPLE action for sFlow or IPFIX.  The given probability is
616  * the number of packets out of UINT32_MAX to sample.  The given
617  * cookie is passed back in the callback for each sampled packet.
618  */
619 static size_t
620 compose_sample_action(const struct ofproto_dpif *ofproto,
621                       struct ofpbuf *odp_actions,
622                       const struct flow *flow,
623                       const uint32_t probability,
624                       const union user_action_cookie *cookie,
625                       const size_t cookie_size)
626 {
627     size_t sample_offset, actions_offset;
628     int cookie_offset;
629
630     sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
631
632     nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
633
634     actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
635     cookie_offset = put_userspace_action(ofproto, odp_actions, flow, cookie,
636                                          cookie_size);
637
638     nl_msg_end_nested(odp_actions, actions_offset);
639     nl_msg_end_nested(odp_actions, sample_offset);
640     return cookie_offset;
641 }
642
643 static void
644 compose_sflow_cookie(const struct ofproto_dpif *ofproto,
645                      ovs_be16 vlan_tci, odp_port_t odp_port,
646                      unsigned int n_outputs, union user_action_cookie *cookie)
647 {
648     int ifindex;
649
650     cookie->type = USER_ACTION_COOKIE_SFLOW;
651     cookie->sflow.vlan_tci = vlan_tci;
652
653     /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
654      * port information") for the interpretation of cookie->output. */
655     switch (n_outputs) {
656     case 0:
657         /* 0x40000000 | 256 means "packet dropped for unknown reason". */
658         cookie->sflow.output = 0x40000000 | 256;
659         break;
660
661     case 1:
662         ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
663         if (ifindex) {
664             cookie->sflow.output = ifindex;
665             break;
666         }
667         /* Fall through. */
668     default:
669         /* 0x80000000 means "multiple output ports. */
670         cookie->sflow.output = 0x80000000 | n_outputs;
671         break;
672     }
673 }
674
675 /* Compose SAMPLE action for sFlow bridge sampling. */
676 static size_t
677 compose_sflow_action(const struct ofproto_dpif *ofproto,
678                      struct ofpbuf *odp_actions,
679                      const struct flow *flow,
680                      odp_port_t odp_port)
681 {
682     uint32_t probability;
683     union user_action_cookie cookie;
684
685     if (!ofproto->sflow || flow->in_port.ofp_port == OFPP_NONE) {
686         return 0;
687     }
688
689     probability = dpif_sflow_get_probability(ofproto->sflow);
690     compose_sflow_cookie(ofproto, htons(0), odp_port,
691                          odp_port == ODPP_NONE ? 0 : 1, &cookie);
692
693     return compose_sample_action(ofproto, odp_actions, flow,  probability,
694                                  &cookie, sizeof cookie.sflow);
695 }
696
697 static void
698 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
699                            uint32_t obs_domain_id, uint32_t obs_point_id,
700                            union user_action_cookie *cookie)
701 {
702     cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
703     cookie->flow_sample.probability = probability;
704     cookie->flow_sample.collector_set_id = collector_set_id;
705     cookie->flow_sample.obs_domain_id = obs_domain_id;
706     cookie->flow_sample.obs_point_id = obs_point_id;
707 }
708
709 static void
710 compose_ipfix_cookie(union user_action_cookie *cookie)
711 {
712     cookie->type = USER_ACTION_COOKIE_IPFIX;
713 }
714
715 /* Compose SAMPLE action for IPFIX bridge sampling. */
716 static void
717 compose_ipfix_action(const struct ofproto_dpif *ofproto,
718                      struct ofpbuf *odp_actions,
719                      const struct flow *flow)
720 {
721     uint32_t probability;
722     union user_action_cookie cookie;
723
724     if (!ofproto->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
725         return;
726     }
727
728     probability = dpif_ipfix_get_bridge_exporter_probability(ofproto->ipfix);
729     compose_ipfix_cookie(&cookie);
730
731     compose_sample_action(ofproto, odp_actions, flow,  probability,
732                           &cookie, sizeof cookie.ipfix);
733 }
734
735 /* SAMPLE action for sFlow must be first action in any given list of
736  * actions.  At this point we do not have all information required to
737  * build it. So try to build sample action as complete as possible. */
738 static void
739 add_sflow_action(struct xlate_ctx *ctx)
740 {
741     ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
742                                                    &ctx->xout->odp_actions,
743                                                    &ctx->xin->flow, ODPP_NONE);
744     ctx->sflow_odp_port = 0;
745     ctx->sflow_n_outputs = 0;
746 }
747
748 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
749  * of actions, eventually after the SAMPLE action for sFlow. */
750 static void
751 add_ipfix_action(struct xlate_ctx *ctx)
752 {
753     compose_ipfix_action(ctx->ofproto, &ctx->xout->odp_actions,
754                          &ctx->xin->flow);
755 }
756
757 /* Fix SAMPLE action according to data collected while composing ODP actions.
758  * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
759  * USERSPACE action's user-cookie which is required for sflow. */
760 static void
761 fix_sflow_action(struct xlate_ctx *ctx)
762 {
763     const struct flow *base = &ctx->base_flow;
764     union user_action_cookie *cookie;
765
766     if (!ctx->user_cookie_offset) {
767         return;
768     }
769
770     cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
771                        sizeof cookie->sflow);
772     ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
773
774     compose_sflow_cookie(ctx->ofproto, base->vlan_tci,
775                          ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
776 }
777
778 static enum slow_path_reason
779 process_special(struct xlate_ctx *ctx, const struct flow *flow,
780                 const struct ofport_dpif *ofport, const struct ofpbuf *packet)
781 {
782     struct ofproto_dpif *ofproto = ctx->ofproto;
783     struct flow_wildcards *wc = &ctx->xout->wc;
784
785     if (!ofport) {
786         return 0;
787     } else if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow, wc)) {
788         if (packet) {
789             cfm_process_heartbeat(ofport->cfm, packet);
790         }
791         return SLOW_CFM;
792     } else if (ofport->bfd && bfd_should_process_flow(flow, wc)) {
793         if (packet) {
794             bfd_process_packet(ofport->bfd, flow, packet);
795         }
796         return SLOW_BFD;
797     } else if (ofport->bundle && ofport->bundle->lacp
798                && flow->dl_type == htons(ETH_TYPE_LACP)) {
799         if (packet) {
800             lacp_process_packet(ofport->bundle->lacp, ofport, packet);
801         }
802         return SLOW_LACP;
803     } else if (ofproto->stp && stp_should_process_flow(flow, wc)) {
804         if (packet) {
805             stp_process_packet(ofport, packet);
806         }
807         return SLOW_STP;
808     } else {
809         return 0;
810     }
811 }
812
813 static void
814 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
815                         bool check_stp)
816 {
817     const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
818     struct flow_wildcards *wc = &ctx->xout->wc;
819     struct flow *flow = &ctx->xin->flow;
820     ovs_be16 flow_vlan_tci;
821     uint32_t flow_skb_mark;
822     uint8_t flow_nw_tos;
823     odp_port_t out_port, odp_port;
824     uint8_t dscp;
825
826     /* If 'struct flow' gets additional metadata, we'll need to zero it out
827      * before traversing a patch port. */
828     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
829
830     if (!ofport) {
831         xlate_report(ctx, "Nonexistent output port");
832         return;
833     } else if (ofport->up.pp.config & OFPUTIL_PC_NO_FWD) {
834         xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
835         return;
836     } else if (check_stp && !stp_forward_in_state(ofport->stp_state)) {
837         xlate_report(ctx, "STP not in forwarding state, skipping output");
838         return;
839     }
840
841     if (ctx->ofproto->has_mirrors && ofport->bundle) {
842         ctx->xout->mirrors |= ofport->bundle->dst_mirrors;
843     }
844
845     if (ofport->peer) {
846         struct ofport_dpif *peer = ofport->peer;
847         struct flow old_flow = ctx->xin->flow;
848         enum slow_path_reason special;
849
850         ctx->ofproto = ofproto_dpif_cast(peer->up.ofproto);
851         flow->in_port.ofp_port = peer->up.ofp_port;
852         flow->metadata = htonll(0);
853         memset(&flow->tunnel, 0, sizeof flow->tunnel);
854         memset(flow->regs, 0, sizeof flow->regs);
855
856         special = process_special(ctx, &ctx->xin->flow, peer,
857                                   ctx->xin->packet);
858         if (special) {
859             ctx->xout->slow = special;
860         } else if (may_receive(peer, ctx)) {
861             if (stp_forward_in_state(peer->stp_state)) {
862                 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
863             } else {
864                 /* Forwarding is disabled by STP.  Let OFPP_NORMAL and the
865                  * learning action look at the packet, then drop it. */
866                 struct flow old_base_flow = ctx->base_flow;
867                 size_t old_size = ctx->xout->odp_actions.size;
868                 mirror_mask_t old_mirrors = ctx->xout->mirrors;
869                 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
870                 ctx->xout->mirrors = old_mirrors;
871                 ctx->base_flow = old_base_flow;
872                 ctx->xout->odp_actions.size = old_size;
873             }
874         }
875
876         ctx->xin->flow = old_flow;
877         ctx->ofproto = ofproto_dpif_cast(ofport->up.ofproto);
878
879         if (ctx->xin->resubmit_stats) {
880             netdev_vport_inc_tx(ofport->up.netdev, ctx->xin->resubmit_stats);
881             netdev_vport_inc_rx(peer->up.netdev, ctx->xin->resubmit_stats);
882         }
883
884         return;
885     }
886
887     flow_vlan_tci = flow->vlan_tci;
888     flow_skb_mark = flow->skb_mark;
889     flow_nw_tos = flow->nw_tos;
890
891     if (ofproto_dpif_dscp_from_priority(ofport, flow->skb_priority, &dscp)) {
892         wc->masks.nw_tos |= IP_ECN_MASK;
893         flow->nw_tos &= ~IP_DSCP_MASK;
894         flow->nw_tos |= dscp;
895     }
896
897     if (ofport->is_tunnel) {
898          /* Save tunnel metadata so that changes made due to
899           * the Logical (tunnel) Port are not visible for any further
900           * matches, while explicit set actions on tunnel metadata are.
901           */
902         struct flow_tnl flow_tnl = flow->tunnel;
903         odp_port = tnl_port_send(ofport, flow, &ctx->xout->wc);
904         if (odp_port == ODPP_NONE) {
905             xlate_report(ctx, "Tunneling decided against output");
906             goto out; /* restore flow_nw_tos */
907         }
908         if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
909             xlate_report(ctx, "Not tunneling to our own address");
910             goto out; /* restore flow_nw_tos */
911         }
912         if (ctx->xin->resubmit_stats) {
913             netdev_vport_inc_tx(ofport->up.netdev, ctx->xin->resubmit_stats);
914         }
915         out_port = odp_port;
916         commit_odp_tunnel_action(flow, &ctx->base_flow,
917                                  &ctx->xout->odp_actions);
918         flow->tunnel = flow_tnl; /* Restore tunnel metadata */
919     } else {
920         ofp_port_t vlandev_port;
921
922         odp_port = ofport->odp_port;
923         if (!hmap_is_empty(&ctx->ofproto->realdev_vid_map)) {
924             wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
925         }
926         vlandev_port = vsp_realdev_to_vlandev(ctx->ofproto, ofp_port,
927                                               flow->vlan_tci);
928         if (vlandev_port == ofp_port) {
929             out_port = odp_port;
930         } else {
931             out_port = ofp_port_to_odp_port(ctx->ofproto, vlandev_port);
932             flow->vlan_tci = htons(0);
933         }
934         flow->skb_mark &= ~IPSEC_MARK;
935     }
936
937     if (out_port != ODPP_NONE) {
938         commit_odp_actions(flow, &ctx->base_flow,
939                            &ctx->xout->odp_actions, &ctx->xout->wc);
940         nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
941                             out_port);
942
943         ctx->sflow_odp_port = odp_port;
944         ctx->sflow_n_outputs++;
945         ctx->xout->nf_output_iface = ofp_port;
946     }
947
948  out:
949     /* Restore flow */
950     flow->vlan_tci = flow_vlan_tci;
951     flow->skb_mark = flow_skb_mark;
952     flow->nw_tos = flow_nw_tos;
953 }
954
955 static void
956 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
957 {
958     compose_output_action__(ctx, ofp_port, true);
959 }
960
961 /* Common rule processing in one place to avoid duplicating code. */
962 static struct rule_dpif *
963 ctx_rule_hooks(struct xlate_ctx *ctx, struct rule_dpif *rule,
964                bool may_packet_in)
965 {
966     if (ctx->xin->resubmit_hook) {
967         ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
968     }
969     if (rule == NULL && may_packet_in) {
970         /* XXX
971          * check if table configuration flags
972          * OFPTC_TABLE_MISS_CONTROLLER, default.
973          * OFPTC_TABLE_MISS_CONTINUE,
974          * OFPTC_TABLE_MISS_DROP
975          * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do?
976          */
977         rule = rule_dpif_miss_rule(ctx->ofproto, &ctx->xin->flow);
978     }
979     if (rule && ctx->xin->resubmit_stats) {
980         rule_credit_stats(rule, ctx->xin->resubmit_stats);
981     }
982     return rule;
983 }
984
985 static void
986 xlate_table_action(struct xlate_ctx *ctx,
987                    ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
988 {
989     if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
990         struct rule_dpif *rule;
991         ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
992         uint8_t old_table_id = ctx->table_id;
993
994         ctx->table_id = table_id;
995
996         /* Look up a flow with 'in_port' as the input port. */
997         ctx->xin->flow.in_port.ofp_port = in_port;
998         rule = rule_dpif_lookup_in_table(ctx->ofproto, &ctx->xin->flow,
999                                          &ctx->xout->wc, table_id);
1000
1001         ctx->xout->tags |= calculate_flow_tag(ctx->ofproto, &ctx->xin->flow,
1002                                               ctx->table_id, rule);
1003
1004         /* Restore the original input port.  Otherwise OFPP_NORMAL and
1005          * OFPP_IN_PORT will have surprising behavior. */
1006         ctx->xin->flow.in_port.ofp_port = old_in_port;
1007
1008         rule = ctx_rule_hooks(ctx, rule, may_packet_in);
1009
1010         if (rule) {
1011             struct rule_dpif *old_rule = ctx->rule;
1012
1013             ctx->recurse++;
1014             ctx->rule = rule;
1015             do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
1016             ctx->rule = old_rule;
1017             ctx->recurse--;
1018         }
1019
1020         ctx->table_id = old_table_id;
1021     } else {
1022         static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1023
1024         VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
1025                     MAX_RESUBMIT_RECURSION);
1026         ctx->max_resubmit_trigger = true;
1027     }
1028 }
1029
1030 static void
1031 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1032                       const struct ofpact_resubmit *resubmit)
1033 {
1034     ofp_port_t in_port;
1035     uint8_t table_id;
1036
1037     in_port = resubmit->in_port;
1038     if (in_port == OFPP_IN_PORT) {
1039         in_port = ctx->xin->flow.in_port.ofp_port;
1040     }
1041
1042     table_id = resubmit->table_id;
1043     if (table_id == 255) {
1044         table_id = ctx->table_id;
1045     }
1046
1047     xlate_table_action(ctx, in_port, table_id, false);
1048 }
1049
1050 static void
1051 flood_packets(struct xlate_ctx *ctx, bool all)
1052 {
1053     struct ofport_dpif *ofport;
1054
1055     HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
1056         ofp_port_t ofp_port = ofport->up.ofp_port;
1057
1058         if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
1059             continue;
1060         }
1061
1062         if (all) {
1063             compose_output_action__(ctx, ofp_port, false);
1064         } else if (!(ofport->up.pp.config & OFPUTIL_PC_NO_FLOOD)) {
1065             compose_output_action(ctx, ofp_port);
1066         }
1067     }
1068
1069     ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1070 }
1071
1072 static void
1073 execute_controller_action(struct xlate_ctx *ctx, int len,
1074                           enum ofp_packet_in_reason reason,
1075                           uint16_t controller_id)
1076 {
1077     struct ofputil_packet_in pin;
1078     struct ofpbuf *packet;
1079     struct flow key;
1080
1081     ovs_assert(!ctx->xout->slow || ctx->xout->slow == SLOW_CONTROLLER);
1082     ctx->xout->slow = SLOW_CONTROLLER;
1083     if (!ctx->xin->packet) {
1084         return;
1085     }
1086
1087     packet = ofpbuf_clone(ctx->xin->packet);
1088
1089     key.skb_priority = 0;
1090     key.skb_mark = 0;
1091     memset(&key.tunnel, 0, sizeof key.tunnel);
1092
1093     commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1094                        &ctx->xout->odp_actions, &ctx->xout->wc);
1095
1096     odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1097                         ctx->xout->odp_actions.size, NULL, NULL);
1098
1099     pin.packet = packet->data;
1100     pin.packet_len = packet->size;
1101     pin.reason = reason;
1102     pin.controller_id = controller_id;
1103     pin.table_id = ctx->table_id;
1104     pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
1105
1106     pin.send_len = len;
1107     flow_get_metadata(&ctx->xin->flow, &pin.fmd);
1108
1109     connmgr_send_packet_in(ctx->ofproto->up.connmgr, &pin);
1110     ofpbuf_delete(packet);
1111 }
1112
1113 static void
1114 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1115 {
1116     struct flow_wildcards *wc = &ctx->xout->wc;
1117     struct flow *flow = &ctx->xin->flow;
1118
1119     ovs_assert(eth_type_mpls(eth_type));
1120
1121     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1122     memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1123
1124     if (flow->mpls_depth) {
1125         flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1126         flow->mpls_depth++;
1127     } else {
1128         ovs_be32 label;
1129         uint8_t tc, ttl;
1130
1131         if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1132             label = htonl(0x2); /* IPV6 Explicit Null. */
1133         } else {
1134             label = htonl(0x0); /* IPV4 Explicit Null. */
1135         }
1136         wc->masks.nw_tos |= IP_DSCP_MASK;
1137         wc->masks.nw_ttl = 0xff;
1138         tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1139         ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1140         flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1141         flow->mpls_depth = 1;
1142     }
1143     flow->dl_type = eth_type;
1144 }
1145
1146 static void
1147 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1148 {
1149     struct flow_wildcards *wc = &ctx->xout->wc;
1150     struct flow *flow = &ctx->xin->flow;
1151
1152     ovs_assert(eth_type_mpls(ctx->xin->flow.dl_type));
1153     ovs_assert(!eth_type_mpls(eth_type));
1154
1155     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1156     memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1157
1158     if (flow->mpls_depth) {
1159         flow->mpls_depth--;
1160         flow->mpls_lse = htonl(0);
1161         if (!flow->mpls_depth) {
1162             flow->dl_type = eth_type;
1163         }
1164     }
1165 }
1166
1167 static bool
1168 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1169 {
1170     struct flow *flow = &ctx->xin->flow;
1171
1172     if (!is_ip_any(flow)) {
1173         return false;
1174     }
1175
1176     ctx->xout->wc.masks.nw_ttl = 0xff;
1177     if (flow->nw_ttl > 1) {
1178         flow->nw_ttl--;
1179         return false;
1180     } else {
1181         size_t i;
1182
1183         for (i = 0; i < ids->n_controllers; i++) {
1184             execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1185                                       ids->cnt_ids[i]);
1186         }
1187
1188         /* Stop processing for current table. */
1189         return true;
1190     }
1191 }
1192
1193 static bool
1194 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1195 {
1196     if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1197         return true;
1198     }
1199
1200     set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1201     return false;
1202 }
1203
1204 static bool
1205 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
1206 {
1207     struct flow *flow = &ctx->xin->flow;
1208     uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
1209     struct flow_wildcards *wc = &ctx->xout->wc;
1210
1211     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1212
1213     if (!eth_type_mpls(flow->dl_type)) {
1214         return false;
1215     }
1216
1217     if (ttl > 1) {
1218         ttl--;
1219         set_mpls_lse_ttl(&flow->mpls_lse, ttl);
1220         return false;
1221     } else {
1222         execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
1223
1224         /* Stop processing for current table. */
1225         return true;
1226     }
1227 }
1228
1229 static void
1230 xlate_output_action(struct xlate_ctx *ctx,
1231                     ofp_port_t port, uint16_t max_len, bool may_packet_in)
1232 {
1233     ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
1234
1235     ctx->xout->nf_output_iface = NF_OUT_DROP;
1236
1237     switch (port) {
1238     case OFPP_IN_PORT:
1239         compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
1240         break;
1241     case OFPP_TABLE:
1242         xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
1243                            0, may_packet_in);
1244         break;
1245     case OFPP_NORMAL:
1246         xlate_normal(ctx);
1247         break;
1248     case OFPP_FLOOD:
1249         flood_packets(ctx,  false);
1250         break;
1251     case OFPP_ALL:
1252         flood_packets(ctx, true);
1253         break;
1254     case OFPP_CONTROLLER:
1255         execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
1256         break;
1257     case OFPP_NONE:
1258         break;
1259     case OFPP_LOCAL:
1260     default:
1261         if (port != ctx->xin->flow.in_port.ofp_port) {
1262             compose_output_action(ctx, port);
1263         } else {
1264             xlate_report(ctx, "skipping output to input port");
1265         }
1266         break;
1267     }
1268
1269     if (prev_nf_output_iface == NF_OUT_FLOOD) {
1270         ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1271     } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1272         ctx->xout->nf_output_iface = prev_nf_output_iface;
1273     } else if (prev_nf_output_iface != NF_OUT_DROP &&
1274                ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1275         ctx->xout->nf_output_iface = NF_OUT_MULTI;
1276     }
1277 }
1278
1279 static void
1280 xlate_output_reg_action(struct xlate_ctx *ctx,
1281                         const struct ofpact_output_reg *or)
1282 {
1283     uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
1284     if (port <= UINT16_MAX) {
1285         union mf_subvalue value;
1286
1287         memset(&value, 0xff, sizeof value);
1288         mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
1289         xlate_output_action(ctx, u16_to_ofp(port),
1290                             or->max_len, false);
1291     }
1292 }
1293
1294 static void
1295 xlate_enqueue_action(struct xlate_ctx *ctx,
1296                      const struct ofpact_enqueue *enqueue)
1297 {
1298     ofp_port_t ofp_port = enqueue->port;
1299     uint32_t queue_id = enqueue->queue;
1300     uint32_t flow_priority, priority;
1301     int error;
1302
1303     /* Translate queue to priority. */
1304     error = ofproto_dpif_queue_to_priority(ctx->ofproto, queue_id, &priority);
1305     if (error) {
1306         /* Fall back to ordinary output action. */
1307         xlate_output_action(ctx, enqueue->port, 0, false);
1308         return;
1309     }
1310
1311     /* Check output port. */
1312     if (ofp_port == OFPP_IN_PORT) {
1313         ofp_port = ctx->xin->flow.in_port.ofp_port;
1314     } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
1315         return;
1316     }
1317
1318     /* Add datapath actions. */
1319     flow_priority = ctx->xin->flow.skb_priority;
1320     ctx->xin->flow.skb_priority = priority;
1321     compose_output_action(ctx, ofp_port);
1322     ctx->xin->flow.skb_priority = flow_priority;
1323
1324     /* Update NetFlow output port. */
1325     if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1326         ctx->xout->nf_output_iface = ofp_port;
1327     } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1328         ctx->xout->nf_output_iface = NF_OUT_MULTI;
1329     }
1330 }
1331
1332 static void
1333 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
1334 {
1335     uint32_t skb_priority;
1336
1337     if (!ofproto_dpif_queue_to_priority(ctx->ofproto, queue_id,
1338                                         &skb_priority)) {
1339         ctx->xin->flow.skb_priority = skb_priority;
1340     } else {
1341         /* Couldn't translate queue to a priority.  Nothing to do.  A warning
1342          * has already been logged. */
1343     }
1344 }
1345
1346 static bool
1347 slave_enabled_cb(ofp_port_t ofp_port, void *ofproto_)
1348 {
1349     struct ofproto_dpif *ofproto = ofproto_;
1350     struct ofport_dpif *port;
1351
1352     switch (ofp_port) {
1353     case OFPP_IN_PORT:
1354     case OFPP_TABLE:
1355     case OFPP_NORMAL:
1356     case OFPP_FLOOD:
1357     case OFPP_ALL:
1358     case OFPP_NONE:
1359         return true;
1360     case OFPP_CONTROLLER: /* Not supported by the bundle action. */
1361         return false;
1362     default:
1363         port = get_ofp_port(ofproto, ofp_port);
1364         return port ? port->may_enable : false;
1365     }
1366 }
1367
1368 static void
1369 xlate_bundle_action(struct xlate_ctx *ctx,
1370                     const struct ofpact_bundle *bundle)
1371 {
1372     ofp_port_t port;
1373
1374     port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
1375                           slave_enabled_cb, ctx->ofproto);
1376     if (bundle->dst.field) {
1377         nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow);
1378     } else {
1379         xlate_output_action(ctx, port, 0, false);
1380     }
1381 }
1382
1383 static void
1384 xlate_learn_action(struct xlate_ctx *ctx,
1385                    const struct ofpact_learn *learn)
1386 {
1387     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
1388     struct ofputil_flow_mod fm;
1389     uint64_t ofpacts_stub[1024 / 8];
1390     struct ofpbuf ofpacts;
1391     int error;
1392
1393     ctx->xout->has_learn = true;
1394
1395     learn_mask(learn, &ctx->xout->wc);
1396
1397     if (!ctx->xin->may_learn) {
1398         return;
1399     }
1400
1401     ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1402     learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
1403
1404     error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
1405     if (error && !VLOG_DROP_WARN(&rl)) {
1406         VLOG_WARN("learning action failed to modify flow table (%s)",
1407                   ofperr_get_name(error));
1408     }
1409
1410     ofpbuf_uninit(&ofpacts);
1411 }
1412
1413 /* Reduces '*timeout' to no more than 'max'.  A value of zero in either case
1414  * means "infinite". */
1415 static void
1416 reduce_timeout(uint16_t max, uint16_t *timeout)
1417 {
1418     if (max && (!*timeout || *timeout > max)) {
1419         *timeout = max;
1420     }
1421 }
1422
1423 static void
1424 xlate_fin_timeout(struct xlate_ctx *ctx,
1425                   const struct ofpact_fin_timeout *oft)
1426 {
1427     if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
1428         struct rule_dpif *rule = ctx->rule;
1429
1430         reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
1431         reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
1432     }
1433 }
1434
1435 static void
1436 xlate_sample_action(struct xlate_ctx *ctx,
1437                     const struct ofpact_sample *os)
1438 {
1439   union user_action_cookie cookie;
1440   /* Scale the probability from 16-bit to 32-bit while representing
1441    * the same percentage. */
1442   uint32_t probability = (os->probability << 16) | os->probability;
1443
1444   commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1445                      &ctx->xout->odp_actions, &ctx->xout->wc);
1446
1447   compose_flow_sample_cookie(os->probability, os->collector_set_id,
1448                              os->obs_domain_id, os->obs_point_id, &cookie);
1449   compose_sample_action(ctx->ofproto, &ctx->xout->odp_actions, &ctx->xin->flow,
1450                         probability, &cookie, sizeof cookie.flow_sample);
1451 }
1452
1453 static bool
1454 may_receive(const struct ofport_dpif *port, struct xlate_ctx *ctx)
1455 {
1456     if (port->up.pp.config & (eth_addr_equals(ctx->xin->flow.dl_dst,
1457                                               eth_addr_stp)
1458                               ? OFPUTIL_PC_NO_RECV_STP
1459                               : OFPUTIL_PC_NO_RECV)) {
1460         return false;
1461     }
1462
1463     /* Only drop packets here if both forwarding and learning are
1464      * disabled.  If just learning is enabled, we need to have
1465      * OFPP_NORMAL and the learning action have a look at the packet
1466      * before we can drop it. */
1467     if (!stp_forward_in_state(port->stp_state)
1468             && !stp_learn_in_state(port->stp_state)) {
1469         return false;
1470     }
1471
1472     return true;
1473 }
1474
1475 static bool
1476 tunnel_ecn_ok(struct xlate_ctx *ctx)
1477 {
1478     if (is_ip_any(&ctx->base_flow)
1479         && (ctx->xin->flow.tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
1480         if ((ctx->base_flow.nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
1481             VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
1482                          " but is not ECN capable");
1483             return false;
1484         } else {
1485             /* Set the ECN CE value in the tunneled packet. */
1486             ctx->xin->flow.nw_tos |= IP_ECN_CE;
1487         }
1488     }
1489
1490     return true;
1491 }
1492
1493 static void
1494 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
1495                  struct xlate_ctx *ctx)
1496 {
1497     struct flow_wildcards *wc = &ctx->xout->wc;
1498     struct flow *flow = &ctx->xin->flow;
1499     bool was_evictable = true;
1500     const struct ofpact *a;
1501
1502     if (ctx->rule) {
1503         /* Don't let the rule we're working on get evicted underneath us. */
1504         was_evictable = ctx->rule->up.evictable;
1505         ctx->rule->up.evictable = false;
1506     }
1507
1508  do_xlate_actions_again:
1509     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1510         struct ofpact_controller *controller;
1511         const struct ofpact_metadata *metadata;
1512
1513         if (ctx->exit) {
1514             break;
1515         }
1516
1517         switch (a->type) {
1518         case OFPACT_OUTPUT:
1519             xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
1520                                 ofpact_get_OUTPUT(a)->max_len, true);
1521             break;
1522
1523         case OFPACT_CONTROLLER:
1524             controller = ofpact_get_CONTROLLER(a);
1525             execute_controller_action(ctx, controller->max_len,
1526                                       controller->reason,
1527                                       controller->controller_id);
1528             break;
1529
1530         case OFPACT_ENQUEUE:
1531             xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
1532             break;
1533
1534         case OFPACT_SET_VLAN_VID:
1535             flow->vlan_tci &= ~htons(VLAN_VID_MASK);
1536             flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
1537                                | htons(VLAN_CFI));
1538             break;
1539
1540         case OFPACT_SET_VLAN_PCP:
1541             flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
1542             flow->vlan_tci |=
1543                 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
1544                       | VLAN_CFI);
1545             break;
1546
1547         case OFPACT_STRIP_VLAN:
1548             flow->vlan_tci = htons(0);
1549             break;
1550
1551         case OFPACT_PUSH_VLAN:
1552             /* XXX 802.1AD(QinQ) */
1553             flow->vlan_tci = htons(VLAN_CFI);
1554             break;
1555
1556         case OFPACT_SET_ETH_SRC:
1557             memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1558             break;
1559
1560         case OFPACT_SET_ETH_DST:
1561             memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1562             break;
1563
1564         case OFPACT_SET_IPV4_SRC:
1565             if (flow->dl_type == htons(ETH_TYPE_IP)) {
1566                 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1567             }
1568             break;
1569
1570         case OFPACT_SET_IPV4_DST:
1571             if (flow->dl_type == htons(ETH_TYPE_IP)) {
1572                 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
1573             }
1574             break;
1575
1576         case OFPACT_SET_IPV4_DSCP:
1577             /* OpenFlow 1.0 only supports IPv4. */
1578             if (flow->dl_type == htons(ETH_TYPE_IP)) {
1579                 flow->nw_tos &= ~IP_DSCP_MASK;
1580                 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
1581             }
1582             break;
1583
1584         case OFPACT_SET_L4_SRC_PORT:
1585             memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1586             if (is_ip_any(flow)) {
1587                 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1588             }
1589             break;
1590
1591         case OFPACT_SET_L4_DST_PORT:
1592             memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1593             if (is_ip_any(flow)) {
1594                 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1595             }
1596             break;
1597
1598         case OFPACT_RESUBMIT:
1599             xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
1600             break;
1601
1602         case OFPACT_SET_TUNNEL:
1603             flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
1604             break;
1605
1606         case OFPACT_SET_QUEUE:
1607             xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
1608             break;
1609
1610         case OFPACT_POP_QUEUE:
1611             flow->skb_priority = ctx->orig_skb_priority;
1612             break;
1613
1614         case OFPACT_REG_MOVE:
1615             nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
1616             break;
1617
1618         case OFPACT_REG_LOAD:
1619             nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
1620             break;
1621
1622         case OFPACT_STACK_PUSH:
1623             nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
1624                                    &ctx->stack);
1625             break;
1626
1627         case OFPACT_STACK_POP:
1628             nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, &ctx->stack);
1629             break;
1630
1631         case OFPACT_PUSH_MPLS:
1632             compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a)->ethertype);
1633             break;
1634
1635         case OFPACT_POP_MPLS:
1636             compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
1637             break;
1638
1639         case OFPACT_SET_MPLS_TTL:
1640             if (compose_set_mpls_ttl_action(ctx,
1641                                             ofpact_get_SET_MPLS_TTL(a)->ttl)) {
1642                 goto out;
1643             }
1644             break;
1645
1646         case OFPACT_DEC_MPLS_TTL:
1647             if (compose_dec_mpls_ttl_action(ctx)) {
1648                 goto out;
1649             }
1650             break;
1651
1652         case OFPACT_DEC_TTL:
1653             if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
1654                 goto out;
1655             }
1656             break;
1657
1658         case OFPACT_NOTE:
1659             /* Nothing to do. */
1660             break;
1661
1662         case OFPACT_MULTIPATH:
1663             multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
1664             break;
1665
1666         case OFPACT_BUNDLE:
1667             xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
1668             break;
1669
1670         case OFPACT_OUTPUT_REG:
1671             xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
1672             break;
1673
1674         case OFPACT_LEARN:
1675             xlate_learn_action(ctx, ofpact_get_LEARN(a));
1676             break;
1677
1678         case OFPACT_EXIT:
1679             ctx->exit = true;
1680             break;
1681
1682         case OFPACT_FIN_TIMEOUT:
1683             memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1684             ctx->xout->has_fin_timeout = true;
1685             xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
1686             break;
1687
1688         case OFPACT_CLEAR_ACTIONS:
1689             /* XXX
1690              * Nothing to do because writa-actions is not supported for now.
1691              * When writa-actions is supported, clear-actions also must
1692              * be supported at the same time.
1693              */
1694             break;
1695
1696         case OFPACT_WRITE_METADATA:
1697             metadata = ofpact_get_WRITE_METADATA(a);
1698             flow->metadata &= ~metadata->mask;
1699             flow->metadata |= metadata->metadata & metadata->mask;
1700             break;
1701
1702         case OFPACT_METER:
1703             /* Not implemented yet. */
1704             break;
1705
1706         case OFPACT_GOTO_TABLE: {
1707             /* It is assumed that goto-table is the last action. */
1708             struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
1709             struct rule_dpif *rule;
1710
1711             ovs_assert(ctx->table_id < ogt->table_id);
1712
1713             ctx->table_id = ogt->table_id;
1714
1715             /* Look up a flow from the new table. */
1716             rule = rule_dpif_lookup_in_table(ctx->ofproto, flow, wc,
1717                                              ctx->table_id);
1718
1719             ctx->xout->tags = calculate_flow_tag(ctx->ofproto, &ctx->xin->flow,
1720                                                  ctx->table_id, rule);
1721
1722             rule = ctx_rule_hooks(ctx, rule, true);
1723
1724             if (rule) {
1725                 if (ctx->rule) {
1726                     ctx->rule->up.evictable = was_evictable;
1727                 }
1728                 ctx->rule = rule;
1729                 was_evictable = rule->up.evictable;
1730                 rule->up.evictable = false;
1731
1732                 /* Tail recursion removal. */
1733                 ofpacts = rule->up.ofpacts;
1734                 ofpacts_len = rule->up.ofpacts_len;
1735                 goto do_xlate_actions_again;
1736             }
1737             break;
1738         }
1739
1740         case OFPACT_SAMPLE:
1741             xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
1742             break;
1743         }
1744     }
1745
1746 out:
1747     if (ctx->rule) {
1748         ctx->rule->up.evictable = was_evictable;
1749     }
1750 }
1751
1752 void
1753 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
1754               const struct flow *flow, struct rule_dpif *rule,
1755               uint8_t tcp_flags, const struct ofpbuf *packet)
1756 {
1757     xin->ofproto = ofproto;
1758     xin->flow = *flow;
1759     xin->packet = packet;
1760     xin->may_learn = packet != NULL;
1761     xin->rule = rule;
1762     xin->ofpacts = NULL;
1763     xin->ofpacts_len = 0;
1764     xin->tcp_flags = tcp_flags;
1765     xin->resubmit_hook = NULL;
1766     xin->report_hook = NULL;
1767     xin->resubmit_stats = NULL;
1768 }
1769
1770 void
1771 xlate_out_uninit(struct xlate_out *xout)
1772 {
1773     if (xout) {
1774         ofpbuf_uninit(&xout->odp_actions);
1775     }
1776 }
1777
1778 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
1779  * into datapath actions, using 'ctx', and discards the datapath actions. */
1780 void
1781 xlate_actions_for_side_effects(struct xlate_in *xin)
1782 {
1783     struct xlate_out xout;
1784
1785     xlate_actions(xin, &xout);
1786     xlate_out_uninit(&xout);
1787 }
1788
1789 static void
1790 xlate_report(struct xlate_ctx *ctx, const char *s)
1791 {
1792     if (ctx->xin->report_hook) {
1793         ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
1794     }
1795 }
1796
1797 void
1798 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
1799 {
1800     dst->wc = src->wc;
1801     dst->tags = src->tags;
1802     dst->slow = src->slow;
1803     dst->has_learn = src->has_learn;
1804     dst->has_normal = src->has_normal;
1805     dst->has_fin_timeout = src->has_fin_timeout;
1806     dst->nf_output_iface = src->nf_output_iface;
1807     dst->mirrors = src->mirrors;
1808
1809     ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
1810                     sizeof dst->odp_actions_stub);
1811     ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
1812                src->odp_actions.size);
1813 }
1814 \f
1815 static bool
1816 actions_output_to_local_port(const struct xlate_ctx *ctx)
1817 {
1818     odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->ofproto, OFPP_LOCAL);
1819     const struct nlattr *a;
1820     unsigned int left;
1821
1822     NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
1823                              ctx->xout->odp_actions.size) {
1824         if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
1825             && nl_attr_get_odp_port(a) == local_odp_port) {
1826             return true;
1827         }
1828     }
1829     return false;
1830 }
1831
1832 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
1833  * into datapath actions in 'odp_actions', using 'ctx'. */
1834 void
1835 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
1836 {
1837     /* Normally false.  Set to true if we ever hit MAX_RESUBMIT_RECURSION, so
1838      * that in the future we always keep a copy of the original flow for
1839      * tracing purposes. */
1840     static bool hit_resubmit_limit;
1841
1842     struct flow_wildcards *wc = &xout->wc;
1843     struct flow *flow = &xin->flow;
1844
1845     enum slow_path_reason special;
1846     const struct ofpact *ofpacts;
1847     struct ofport_dpif *in_port;
1848     struct flow orig_flow;
1849     struct xlate_ctx ctx;
1850     size_t ofpacts_len;
1851
1852     COVERAGE_INC(ofproto_dpif_xlate);
1853
1854     /* Flow initialization rules:
1855      * - 'base_flow' must match the kernel's view of the packet at the
1856      *   time that action processing starts.  'flow' represents any
1857      *   transformations we wish to make through actions.
1858      * - By default 'base_flow' and 'flow' are the same since the input
1859      *   packet matches the output before any actions are applied.
1860      * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
1861      *   of the received packet as seen by the kernel.  If we later output
1862      *   to another device without any modifications this will cause us to
1863      *   insert a new tag since the original one was stripped off by the
1864      *   VLAN device.
1865      * - Tunnel metadata as received is retained in 'flow'. This allows
1866      *   tunnel metadata matching also in later tables.
1867      *   Since a kernel action for setting the tunnel metadata will only be
1868      *   generated with actual tunnel output, changing the tunnel metadata
1869      *   values in 'flow' (such as tun_id) will only have effect with a later
1870      *   tunnel output action.
1871      * - Tunnel 'base_flow' is completely cleared since that is what the
1872      *   kernel does.  If we wish to maintain the original values an action
1873      *   needs to be generated. */
1874
1875     ctx.xin = xin;
1876     ctx.xout = xout;
1877
1878     ctx.ofproto = xin->ofproto;
1879     ctx.rule = xin->rule;
1880
1881     ctx.base_flow = *flow;
1882     memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
1883     ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
1884
1885     flow_wildcards_init_catchall(wc);
1886     memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
1887     memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
1888     memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1889     wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
1890
1891     if (tnl_port_should_receive(&ctx.xin->flow)) {
1892         memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
1893     }
1894     if (xin->ofproto->netflow) {
1895         netflow_mask_wc(flow, wc);
1896     }
1897
1898     ctx.xout->tags = 0;
1899     ctx.xout->slow = 0;
1900     ctx.xout->has_learn = false;
1901     ctx.xout->has_normal = false;
1902     ctx.xout->has_fin_timeout = false;
1903     ctx.xout->nf_output_iface = NF_OUT_DROP;
1904     ctx.xout->mirrors = 0;
1905
1906     ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
1907                     sizeof ctx.xout->odp_actions_stub);
1908     ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
1909
1910     ctx.recurse = 0;
1911     ctx.max_resubmit_trigger = false;
1912     ctx.orig_skb_priority = flow->skb_priority;
1913     ctx.table_id = 0;
1914     ctx.exit = false;
1915
1916     if (xin->ofpacts) {
1917         ofpacts = xin->ofpacts;
1918         ofpacts_len = xin->ofpacts_len;
1919     } else if (xin->rule) {
1920         ofpacts = xin->rule->up.ofpacts;
1921         ofpacts_len = xin->rule->up.ofpacts_len;
1922     } else {
1923         NOT_REACHED();
1924     }
1925
1926     ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
1927
1928     if (ctx.ofproto->has_mirrors || hit_resubmit_limit) {
1929         /* Do this conditionally because the copy is expensive enough that it
1930          * shows up in profiles. */
1931         orig_flow = *flow;
1932     }
1933
1934     if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
1935         switch (ctx.ofproto->up.frag_handling) {
1936         case OFPC_FRAG_NORMAL:
1937             /* We must pretend that transport ports are unavailable. */
1938             flow->tp_src = ctx.base_flow.tp_src = htons(0);
1939             flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
1940             break;
1941
1942         case OFPC_FRAG_DROP:
1943             return;
1944
1945         case OFPC_FRAG_REASM:
1946             NOT_REACHED();
1947
1948         case OFPC_FRAG_NX_MATCH:
1949             /* Nothing to do. */
1950             break;
1951
1952         case OFPC_INVALID_TTL_TO_CONTROLLER:
1953             NOT_REACHED();
1954         }
1955     }
1956
1957     in_port = get_ofp_port(ctx.ofproto, flow->in_port.ofp_port);
1958     special = process_special(&ctx, flow, in_port, ctx.xin->packet);
1959     if (special) {
1960         ctx.xout->slow = special;
1961     } else {
1962         static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1963         size_t sample_actions_len;
1964
1965         if (flow->in_port.ofp_port
1966             != vsp_realdev_to_vlandev(ctx.ofproto, flow->in_port.ofp_port,
1967                                       flow->vlan_tci)) {
1968             ctx.base_flow.vlan_tci = 0;
1969         }
1970
1971         add_sflow_action(&ctx);
1972         add_ipfix_action(&ctx);
1973         sample_actions_len = ctx.xout->odp_actions.size;
1974
1975         if (tunnel_ecn_ok(&ctx) && (!in_port || may_receive(in_port, &ctx))) {
1976             do_xlate_actions(ofpacts, ofpacts_len, &ctx);
1977
1978             /* We've let OFPP_NORMAL and the learning action look at the
1979              * packet, so drop it now if forwarding is disabled. */
1980             if (in_port && !stp_forward_in_state(in_port->stp_state)) {
1981                 ctx.xout->odp_actions.size = sample_actions_len;
1982             }
1983         }
1984
1985         if (ctx.max_resubmit_trigger && !ctx.xin->resubmit_hook) {
1986             if (!hit_resubmit_limit) {
1987                 /* We didn't record the original flow.  Make sure we do from
1988                  * now on. */
1989                 hit_resubmit_limit = true;
1990             } else if (!VLOG_DROP_ERR(&trace_rl)) {
1991                 struct ds ds = DS_EMPTY_INITIALIZER;
1992
1993                 ofproto_trace(ctx.ofproto, &orig_flow, ctx.xin->packet, &ds);
1994                 VLOG_ERR("Trace triggered by excessive resubmit "
1995                          "recursion:\n%s", ds_cstr(&ds));
1996                 ds_destroy(&ds);
1997             }
1998         }
1999
2000         if (connmgr_has_in_band(ctx.ofproto->up.connmgr)
2001             && in_band_must_output_to_local_port(flow)
2002             && !actions_output_to_local_port(&ctx)) {
2003             compose_output_action(&ctx, OFPP_LOCAL);
2004         }
2005         if (ctx.ofproto->has_mirrors) {
2006             add_mirror_actions(&ctx, &orig_flow);
2007         }
2008         fix_sflow_action(&ctx);
2009     }
2010
2011     ofpbuf_uninit(&ctx.stack);
2012
2013     /* Clear the metadata and register wildcard masks, because we won't
2014      * use non-header fields as part of the cache. */
2015     memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2016     memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
2017 }