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