ofproto-dpif: Remove 'has_bundle_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 "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     odp_port_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 *, ofp_port_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 *, ofp_port_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, ofp_port_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.ofp_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_odp_port(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_src, 0xff, sizeof wc->masks.dl_src);
552     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
553     wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
554
555     in_bundle = lookup_input_bundle(ctx->ofproto, flow->in_port.ofp_port,
556                                     ctx->xin->packet != NULL, &in_port);
557     if (!in_bundle) {
558         xlate_report(ctx, "no input bundle, dropping");
559         return;
560     }
561
562     /* Drop malformed frames. */
563     if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
564         !(flow->vlan_tci & htons(VLAN_CFI))) {
565         if (ctx->xin->packet != NULL) {
566             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
567             VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
568                          "VLAN tag received on port %s",
569                          ctx->ofproto->up.name, in_bundle->name);
570         }
571         xlate_report(ctx, "partial VLAN tag, dropping");
572         return;
573     }
574
575     /* Drop frames on bundles reserved for mirroring. */
576     if (in_bundle->mirror_out) {
577         if (ctx->xin->packet != NULL) {
578             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
579             VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
580                          "%s, which is reserved exclusively for mirroring",
581                          ctx->ofproto->up.name, in_bundle->name);
582         }
583         xlate_report(ctx, "input port is mirror output port, dropping");
584         return;
585     }
586
587     /* Check VLAN. */
588     vid = vlan_tci_to_vid(flow->vlan_tci);
589     if (!input_vid_is_valid(vid, in_bundle, ctx->xin->packet != NULL)) {
590         xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
591         return;
592     }
593     vlan = input_vid_to_vlan(in_bundle, vid);
594
595     /* Check other admissibility requirements. */
596     if (in_port && !is_admissible(ctx, in_port, vlan)) {
597         return;
598     }
599
600     /* Learn source MAC. */
601     if (ctx->xin->may_learn) {
602         update_learning_table(ctx->ofproto, flow, wc, vlan, in_bundle);
603     }
604
605     /* Determine output bundle. */
606     mac = mac_learning_lookup(ctx->ofproto->ml, flow->dl_dst, vlan,
607                               &ctx->xout->tags);
608     if (mac) {
609         if (mac->port.p != in_bundle) {
610             xlate_report(ctx, "forwarding to learned port");
611             output_normal(ctx, mac->port.p, vlan);
612         } else {
613             xlate_report(ctx, "learned port is input port, dropping");
614         }
615     } else {
616         struct ofbundle *bundle;
617
618         xlate_report(ctx, "no learned MAC for destination, flooding");
619         HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
620             if (bundle != in_bundle
621                 && ofbundle_includes_vlan(bundle, vlan)
622                 && bundle->floodable
623                 && !bundle->mirror_out) {
624                 output_normal(ctx, bundle, vlan);
625             }
626         }
627         ctx->xout->nf_output_iface = NF_OUT_FLOOD;
628     }
629 }
630
631 /* Compose SAMPLE action for sFlow or IPFIX.  The given probability is
632  * the number of packets out of UINT32_MAX to sample.  The given
633  * cookie is passed back in the callback for each sampled packet.
634  */
635 static size_t
636 compose_sample_action(const struct ofproto_dpif *ofproto,
637                       struct ofpbuf *odp_actions,
638                       const struct flow *flow,
639                       const uint32_t probability,
640                       const union user_action_cookie *cookie,
641                       const size_t cookie_size)
642 {
643     size_t sample_offset, actions_offset;
644     int cookie_offset;
645
646     sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
647
648     nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
649
650     actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
651     cookie_offset = put_userspace_action(ofproto, odp_actions, flow, cookie,
652                                          cookie_size);
653
654     nl_msg_end_nested(odp_actions, actions_offset);
655     nl_msg_end_nested(odp_actions, sample_offset);
656     return cookie_offset;
657 }
658
659 static void
660 compose_sflow_cookie(const struct ofproto_dpif *ofproto,
661                      ovs_be16 vlan_tci, odp_port_t odp_port,
662                      unsigned int n_outputs, union user_action_cookie *cookie)
663 {
664     int ifindex;
665
666     cookie->type = USER_ACTION_COOKIE_SFLOW;
667     cookie->sflow.vlan_tci = vlan_tci;
668
669     /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
670      * port information") for the interpretation of cookie->output. */
671     switch (n_outputs) {
672     case 0:
673         /* 0x40000000 | 256 means "packet dropped for unknown reason". */
674         cookie->sflow.output = 0x40000000 | 256;
675         break;
676
677     case 1:
678         ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
679         if (ifindex) {
680             cookie->sflow.output = ifindex;
681             break;
682         }
683         /* Fall through. */
684     default:
685         /* 0x80000000 means "multiple output ports. */
686         cookie->sflow.output = 0x80000000 | n_outputs;
687         break;
688     }
689 }
690
691 /* Compose SAMPLE action for sFlow bridge sampling. */
692 static size_t
693 compose_sflow_action(const struct ofproto_dpif *ofproto,
694                      struct ofpbuf *odp_actions,
695                      const struct flow *flow,
696                      odp_port_t odp_port)
697 {
698     uint32_t probability;
699     union user_action_cookie cookie;
700
701     if (!ofproto->sflow || flow->in_port.ofp_port == OFPP_NONE) {
702         return 0;
703     }
704
705     probability = dpif_sflow_get_probability(ofproto->sflow);
706     compose_sflow_cookie(ofproto, htons(0), odp_port,
707                          odp_port == ODPP_NONE ? 0 : 1, &cookie);
708
709     return compose_sample_action(ofproto, odp_actions, flow,  probability,
710                                  &cookie, sizeof cookie.sflow);
711 }
712
713 static void
714 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
715                            uint32_t obs_domain_id, uint32_t obs_point_id,
716                            union user_action_cookie *cookie)
717 {
718     cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
719     cookie->flow_sample.probability = probability;
720     cookie->flow_sample.collector_set_id = collector_set_id;
721     cookie->flow_sample.obs_domain_id = obs_domain_id;
722     cookie->flow_sample.obs_point_id = obs_point_id;
723 }
724
725 static void
726 compose_ipfix_cookie(union user_action_cookie *cookie)
727 {
728     cookie->type = USER_ACTION_COOKIE_IPFIX;
729 }
730
731 /* Compose SAMPLE action for IPFIX bridge sampling. */
732 static void
733 compose_ipfix_action(const struct ofproto_dpif *ofproto,
734                      struct ofpbuf *odp_actions,
735                      const struct flow *flow)
736 {
737     uint32_t probability;
738     union user_action_cookie cookie;
739
740     if (!ofproto->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
741         return;
742     }
743
744     probability = dpif_ipfix_get_bridge_exporter_probability(ofproto->ipfix);
745     compose_ipfix_cookie(&cookie);
746
747     compose_sample_action(ofproto, odp_actions, flow,  probability,
748                           &cookie, sizeof cookie.ipfix);
749 }
750
751 /* SAMPLE action for sFlow must be first action in any given list of
752  * actions.  At this point we do not have all information required to
753  * build it. So try to build sample action as complete as possible. */
754 static void
755 add_sflow_action(struct xlate_ctx *ctx)
756 {
757     ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
758                                                    &ctx->xout->odp_actions,
759                                                    &ctx->xin->flow, ODPP_NONE);
760     ctx->sflow_odp_port = 0;
761     ctx->sflow_n_outputs = 0;
762 }
763
764 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
765  * of actions, eventually after the SAMPLE action for sFlow. */
766 static void
767 add_ipfix_action(struct xlate_ctx *ctx)
768 {
769     compose_ipfix_action(ctx->ofproto, &ctx->xout->odp_actions,
770                          &ctx->xin->flow);
771 }
772
773 /* Fix SAMPLE action according to data collected while composing ODP actions.
774  * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
775  * USERSPACE action's user-cookie which is required for sflow. */
776 static void
777 fix_sflow_action(struct xlate_ctx *ctx)
778 {
779     const struct flow *base = &ctx->base_flow;
780     union user_action_cookie *cookie;
781
782     if (!ctx->user_cookie_offset) {
783         return;
784     }
785
786     cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
787                        sizeof cookie->sflow);
788     ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
789
790     compose_sflow_cookie(ctx->ofproto, base->vlan_tci,
791                          ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
792 }
793
794 static enum slow_path_reason
795 process_special(struct xlate_ctx *ctx, const struct flow *flow,
796                 const struct ofport_dpif *ofport, const struct ofpbuf *packet)
797 {
798     struct ofproto_dpif *ofproto = ctx->ofproto;
799     struct flow_wildcards *wc = &ctx->xout->wc;
800
801     if (!ofport) {
802         return 0;
803     } else if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow, wc)) {
804         if (packet) {
805             cfm_process_heartbeat(ofport->cfm, packet);
806         }
807         return SLOW_CFM;
808     } else if (ofport->bfd && bfd_should_process_flow(flow, wc)) {
809         if (packet) {
810             bfd_process_packet(ofport->bfd, flow, packet);
811         }
812         return SLOW_BFD;
813     } else if (ofport->bundle && ofport->bundle->lacp
814                && flow->dl_type == htons(ETH_TYPE_LACP)) {
815         if (packet) {
816             lacp_process_packet(ofport->bundle->lacp, ofport, packet);
817         }
818         return SLOW_LACP;
819     } else if (ofproto->stp && stp_should_process_flow(flow, wc)) {
820         if (packet) {
821             stp_process_packet(ofport, packet);
822         }
823         return SLOW_STP;
824     } else {
825         return 0;
826     }
827 }
828
829 static void
830 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
831                         bool check_stp)
832 {
833     const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
834     struct flow_wildcards *wc = &ctx->xout->wc;
835     struct flow *flow = &ctx->xin->flow;
836     ovs_be16 flow_vlan_tci;
837     uint32_t flow_skb_mark;
838     uint8_t flow_nw_tos;
839     odp_port_t out_port, odp_port;
840     uint8_t dscp;
841
842     /* If 'struct flow' gets additional metadata, we'll need to zero it out
843      * before traversing a patch port. */
844     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
845
846     if (!ofport) {
847         xlate_report(ctx, "Nonexistent output port");
848         return;
849     } else if (ofport->up.pp.config & OFPUTIL_PC_NO_FWD) {
850         xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
851         return;
852     } else if (check_stp && !stp_forward_in_state(ofport->stp_state)) {
853         xlate_report(ctx, "STP not in forwarding state, skipping output");
854         return;
855     }
856
857     if (ofport->peer) {
858         struct ofport_dpif *peer = ofport->peer;
859         struct flow old_flow = ctx->xin->flow;
860         enum slow_path_reason special;
861
862         ctx->ofproto = ofproto_dpif_cast(peer->up.ofproto);
863         flow->in_port.ofp_port = peer->up.ofp_port;
864         flow->metadata = htonll(0);
865         memset(&flow->tunnel, 0, sizeof flow->tunnel);
866         memset(flow->regs, 0, sizeof flow->regs);
867
868         special = process_special(ctx, &ctx->xin->flow, peer,
869                                   ctx->xin->packet);
870         if (special) {
871             ctx->xout->slow = special;
872         } else if (may_receive(peer, ctx)) {
873             if (stp_forward_in_state(peer->stp_state)) {
874                 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
875             } else {
876                 /* Forwarding is disabled by STP.  Let OFPP_NORMAL and the
877                  * learning action look at the packet, then drop it. */
878                 struct flow old_base_flow = ctx->base_flow;
879                 size_t old_size = ctx->xout->odp_actions.size;
880                 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
881                 ctx->base_flow = old_base_flow;
882                 ctx->xout->odp_actions.size = old_size;
883             }
884         }
885
886         ctx->xin->flow = old_flow;
887         ctx->ofproto = ofproto_dpif_cast(ofport->up.ofproto);
888
889         if (ctx->xin->resubmit_stats) {
890             netdev_vport_inc_tx(ofport->up.netdev, ctx->xin->resubmit_stats);
891             netdev_vport_inc_rx(peer->up.netdev, ctx->xin->resubmit_stats);
892         }
893
894         return;
895     }
896
897     flow_vlan_tci = flow->vlan_tci;
898     flow_skb_mark = flow->skb_mark;
899     flow_nw_tos = flow->nw_tos;
900
901     if (ofproto_dpif_dscp_from_priority(ofport, flow->skb_priority, &dscp)) {
902         wc->masks.nw_tos |= IP_ECN_MASK;
903         flow->nw_tos &= ~IP_DSCP_MASK;
904         flow->nw_tos |= dscp;
905     }
906
907     if (ofport->tnl_port) {
908          /* Save tunnel metadata so that changes made due to
909           * the Logical (tunnel) Port are not visible for any further
910           * matches, while explicit set actions on tunnel metadata are.
911           */
912         struct flow_tnl flow_tnl = flow->tunnel;
913         odp_port = tnl_port_send(ofport->tnl_port, flow, &ctx->xout->wc);
914         if (odp_port == ODPP_NONE) {
915             xlate_report(ctx, "Tunneling decided against output");
916             goto out; /* restore flow_nw_tos */
917         }
918         if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
919             xlate_report(ctx, "Not tunneling to our own address");
920             goto out; /* restore flow_nw_tos */
921         }
922         if (ctx->xin->resubmit_stats) {
923             netdev_vport_inc_tx(ofport->up.netdev, ctx->xin->resubmit_stats);
924         }
925         out_port = odp_port;
926         commit_odp_tunnel_action(flow, &ctx->base_flow,
927                                  &ctx->xout->odp_actions);
928         flow->tunnel = flow_tnl; /* Restore tunnel metadata */
929     } else {
930         ofp_port_t vlandev_port;
931
932         odp_port = ofport->odp_port;
933         if (!hmap_is_empty(&ctx->ofproto->realdev_vid_map)) {
934             wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
935         }
936         vlandev_port = vsp_realdev_to_vlandev(ctx->ofproto, ofp_port,
937                                               flow->vlan_tci);
938         if (vlandev_port == ofp_port) {
939             out_port = odp_port;
940         } else {
941             out_port = ofp_port_to_odp_port(ctx->ofproto, vlandev_port);
942             flow->vlan_tci = htons(0);
943         }
944         flow->skb_mark &= ~IPSEC_MARK;
945     }
946
947     if (out_port != ODPP_NONE) {
948         commit_odp_actions(flow, &ctx->base_flow,
949                            &ctx->xout->odp_actions, &ctx->xout->wc);
950         nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
951                             out_port);
952
953         ctx->sflow_odp_port = odp_port;
954         ctx->sflow_n_outputs++;
955         ctx->xout->nf_output_iface = ofp_port;
956     }
957
958  out:
959     /* Restore flow */
960     flow->vlan_tci = flow_vlan_tci;
961     flow->skb_mark = flow_skb_mark;
962     flow->nw_tos = flow_nw_tos;
963 }
964
965 static void
966 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
967 {
968     compose_output_action__(ctx, ofp_port, true);
969 }
970
971 static void
972 tag_the_flow(struct xlate_ctx *ctx, struct rule_dpif *rule)
973 {
974     struct ofproto_dpif *ofproto = ctx->ofproto;
975     uint8_t table_id = ctx->table_id;
976
977     if (table_id > 0 && table_id < N_TABLES) {
978         struct table_dpif *table = &ofproto->tables[table_id];
979         if (table->other_table) {
980             ctx->xout->tags |= (rule && rule->tag
981                                 ? rule->tag
982                                 : rule_calculate_tag(&ctx->xin->flow,
983                                                      &table->other_table->mask,
984                                                      table->basis));
985         }
986     }
987 }
988
989 /* Common rule processing in one place to avoid duplicating code. */
990 static struct rule_dpif *
991 ctx_rule_hooks(struct xlate_ctx *ctx, struct rule_dpif *rule,
992                bool may_packet_in)
993 {
994     if (ctx->xin->resubmit_hook) {
995         ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
996     }
997     if (rule == NULL && may_packet_in) {
998         /* XXX
999          * check if table configuration flags
1000          * OFPTC_TABLE_MISS_CONTROLLER, default.
1001          * OFPTC_TABLE_MISS_CONTINUE,
1002          * OFPTC_TABLE_MISS_DROP
1003          * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do?
1004          */
1005         rule = rule_dpif_miss_rule(ctx->ofproto, &ctx->xin->flow);
1006     }
1007     if (rule && ctx->xin->resubmit_stats) {
1008         rule_credit_stats(rule, ctx->xin->resubmit_stats);
1009     }
1010     return rule;
1011 }
1012
1013 static void
1014 xlate_table_action(struct xlate_ctx *ctx,
1015                    ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1016 {
1017     if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
1018         struct rule_dpif *rule;
1019         ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1020         uint8_t old_table_id = ctx->table_id;
1021
1022         ctx->table_id = table_id;
1023
1024         /* Look up a flow with 'in_port' as the input port. */
1025         ctx->xin->flow.in_port.ofp_port = in_port;
1026         rule = rule_dpif_lookup_in_table(ctx->ofproto, &ctx->xin->flow,
1027                                          &ctx->xout->wc, table_id);
1028
1029         tag_the_flow(ctx, rule);
1030
1031         /* Restore the original input port.  Otherwise OFPP_NORMAL and
1032          * OFPP_IN_PORT will have surprising behavior. */
1033         ctx->xin->flow.in_port.ofp_port = old_in_port;
1034
1035         rule = ctx_rule_hooks(ctx, rule, may_packet_in);
1036
1037         if (rule) {
1038             struct rule_dpif *old_rule = ctx->rule;
1039
1040             ctx->recurse++;
1041             ctx->rule = rule;
1042             do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
1043             ctx->rule = old_rule;
1044             ctx->recurse--;
1045         }
1046
1047         ctx->table_id = old_table_id;
1048     } else {
1049         static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1050
1051         VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
1052                     MAX_RESUBMIT_RECURSION);
1053         ctx->max_resubmit_trigger = true;
1054     }
1055 }
1056
1057 static void
1058 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1059                       const struct ofpact_resubmit *resubmit)
1060 {
1061     ofp_port_t in_port;
1062     uint8_t table_id;
1063
1064     in_port = resubmit->in_port;
1065     if (in_port == OFPP_IN_PORT) {
1066         in_port = ctx->xin->flow.in_port.ofp_port;
1067     }
1068
1069     table_id = resubmit->table_id;
1070     if (table_id == 255) {
1071         table_id = ctx->table_id;
1072     }
1073
1074     xlate_table_action(ctx, in_port, table_id, false);
1075 }
1076
1077 static void
1078 flood_packets(struct xlate_ctx *ctx, bool all)
1079 {
1080     struct ofport_dpif *ofport;
1081
1082     HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
1083         ofp_port_t ofp_port = ofport->up.ofp_port;
1084
1085         if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
1086             continue;
1087         }
1088
1089         if (all) {
1090             compose_output_action__(ctx, ofp_port, false);
1091         } else if (!(ofport->up.pp.config & OFPUTIL_PC_NO_FLOOD)) {
1092             compose_output_action(ctx, ofp_port);
1093         }
1094     }
1095
1096     ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1097 }
1098
1099 static void
1100 execute_controller_action(struct xlate_ctx *ctx, int len,
1101                           enum ofp_packet_in_reason reason,
1102                           uint16_t controller_id)
1103 {
1104     struct ofputil_packet_in pin;
1105     struct ofpbuf *packet;
1106     struct flow key;
1107
1108     ovs_assert(!ctx->xout->slow || ctx->xout->slow == SLOW_CONTROLLER);
1109     ctx->xout->slow = SLOW_CONTROLLER;
1110     if (!ctx->xin->packet) {
1111         return;
1112     }
1113
1114     packet = ofpbuf_clone(ctx->xin->packet);
1115
1116     key.skb_priority = 0;
1117     key.skb_mark = 0;
1118     memset(&key.tunnel, 0, sizeof key.tunnel);
1119
1120     commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1121                        &ctx->xout->odp_actions, &ctx->xout->wc);
1122
1123     odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1124                         ctx->xout->odp_actions.size, NULL, NULL);
1125
1126     pin.packet = packet->data;
1127     pin.packet_len = packet->size;
1128     pin.reason = reason;
1129     pin.controller_id = controller_id;
1130     pin.table_id = ctx->table_id;
1131     pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
1132
1133     pin.send_len = len;
1134     flow_get_metadata(&ctx->xin->flow, &pin.fmd);
1135
1136     connmgr_send_packet_in(ctx->ofproto->up.connmgr, &pin);
1137     ofpbuf_delete(packet);
1138 }
1139
1140 static void
1141 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1142 {
1143     struct flow_wildcards *wc = &ctx->xout->wc;
1144     struct flow *flow = &ctx->xin->flow;
1145
1146     ovs_assert(eth_type_mpls(eth_type));
1147
1148     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1149     memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1150
1151     if (flow->mpls_depth) {
1152         flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1153         flow->mpls_depth++;
1154     } else {
1155         ovs_be32 label;
1156         uint8_t tc, ttl;
1157
1158         if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1159             label = htonl(0x2); /* IPV6 Explicit Null. */
1160         } else {
1161             label = htonl(0x0); /* IPV4 Explicit Null. */
1162         }
1163         wc->masks.nw_tos |= IP_DSCP_MASK;
1164         wc->masks.nw_ttl = 0xff;
1165         tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1166         ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1167         flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1168         flow->mpls_depth = 1;
1169     }
1170     flow->dl_type = eth_type;
1171 }
1172
1173 static void
1174 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1175 {
1176     struct flow_wildcards *wc = &ctx->xout->wc;
1177     struct flow *flow = &ctx->xin->flow;
1178
1179     ovs_assert(eth_type_mpls(ctx->xin->flow.dl_type));
1180     ovs_assert(!eth_type_mpls(eth_type));
1181
1182     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1183     memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1184
1185     if (flow->mpls_depth) {
1186         flow->mpls_depth--;
1187         flow->mpls_lse = htonl(0);
1188         if (!flow->mpls_depth) {
1189             flow->dl_type = eth_type;
1190         }
1191     }
1192 }
1193
1194 static bool
1195 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1196 {
1197     struct flow *flow = &ctx->xin->flow;
1198
1199     if (!is_ip_any(flow)) {
1200         return false;
1201     }
1202
1203     ctx->xout->wc.masks.nw_ttl = 0xff;
1204     if (flow->nw_ttl > 1) {
1205         flow->nw_ttl--;
1206         return false;
1207     } else {
1208         size_t i;
1209
1210         for (i = 0; i < ids->n_controllers; i++) {
1211             execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1212                                       ids->cnt_ids[i]);
1213         }
1214
1215         /* Stop processing for current table. */
1216         return true;
1217     }
1218 }
1219
1220 static bool
1221 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1222 {
1223     if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1224         return true;
1225     }
1226
1227     set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1228     return false;
1229 }
1230
1231 static bool
1232 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
1233 {
1234     struct flow *flow = &ctx->xin->flow;
1235     uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
1236     struct flow_wildcards *wc = &ctx->xout->wc;
1237
1238     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1239
1240     if (!eth_type_mpls(flow->dl_type)) {
1241         return false;
1242     }
1243
1244     if (ttl > 1) {
1245         ttl--;
1246         set_mpls_lse_ttl(&flow->mpls_lse, ttl);
1247         return false;
1248     } else {
1249         execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
1250
1251         /* Stop processing for current table. */
1252         return true;
1253     }
1254 }
1255
1256 static void
1257 xlate_output_action(struct xlate_ctx *ctx,
1258                     ofp_port_t port, uint16_t max_len, bool may_packet_in)
1259 {
1260     ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
1261
1262     ctx->xout->nf_output_iface = NF_OUT_DROP;
1263
1264     switch (port) {
1265     case OFPP_IN_PORT:
1266         compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
1267         break;
1268     case OFPP_TABLE:
1269         xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
1270                            0, may_packet_in);
1271         break;
1272     case OFPP_NORMAL:
1273         xlate_normal(ctx);
1274         break;
1275     case OFPP_FLOOD:
1276         flood_packets(ctx,  false);
1277         break;
1278     case OFPP_ALL:
1279         flood_packets(ctx, true);
1280         break;
1281     case OFPP_CONTROLLER:
1282         execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
1283         break;
1284     case OFPP_NONE:
1285         break;
1286     case OFPP_LOCAL:
1287     default:
1288         if (port != ctx->xin->flow.in_port.ofp_port) {
1289             compose_output_action(ctx, port);
1290         } else {
1291             xlate_report(ctx, "skipping output to input port");
1292         }
1293         break;
1294     }
1295
1296     if (prev_nf_output_iface == NF_OUT_FLOOD) {
1297         ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1298     } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1299         ctx->xout->nf_output_iface = prev_nf_output_iface;
1300     } else if (prev_nf_output_iface != NF_OUT_DROP &&
1301                ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1302         ctx->xout->nf_output_iface = NF_OUT_MULTI;
1303     }
1304 }
1305
1306 static void
1307 xlate_output_reg_action(struct xlate_ctx *ctx,
1308                         const struct ofpact_output_reg *or)
1309 {
1310     uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
1311     if (port <= UINT16_MAX) {
1312         union mf_subvalue value;
1313
1314         memset(&value, 0xff, sizeof value);
1315         mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
1316         xlate_output_action(ctx, u16_to_ofp(port),
1317                             or->max_len, false);
1318     }
1319 }
1320
1321 static void
1322 xlate_enqueue_action(struct xlate_ctx *ctx,
1323                      const struct ofpact_enqueue *enqueue)
1324 {
1325     ofp_port_t ofp_port = enqueue->port;
1326     uint32_t queue_id = enqueue->queue;
1327     uint32_t flow_priority, priority;
1328     int error;
1329
1330     /* Translate queue to priority. */
1331     error = ofproto_dpif_queue_to_priority(ctx->ofproto, queue_id, &priority);
1332     if (error) {
1333         /* Fall back to ordinary output action. */
1334         xlate_output_action(ctx, enqueue->port, 0, false);
1335         return;
1336     }
1337
1338     /* Check output port. */
1339     if (ofp_port == OFPP_IN_PORT) {
1340         ofp_port = ctx->xin->flow.in_port.ofp_port;
1341     } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
1342         return;
1343     }
1344
1345     /* Add datapath actions. */
1346     flow_priority = ctx->xin->flow.skb_priority;
1347     ctx->xin->flow.skb_priority = priority;
1348     compose_output_action(ctx, ofp_port);
1349     ctx->xin->flow.skb_priority = flow_priority;
1350
1351     /* Update NetFlow output port. */
1352     if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1353         ctx->xout->nf_output_iface = ofp_port;
1354     } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1355         ctx->xout->nf_output_iface = NF_OUT_MULTI;
1356     }
1357 }
1358
1359 static void
1360 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
1361 {
1362     uint32_t skb_priority;
1363
1364     if (!ofproto_dpif_queue_to_priority(ctx->ofproto, queue_id,
1365                                         &skb_priority)) {
1366         ctx->xin->flow.skb_priority = skb_priority;
1367     } else {
1368         /* Couldn't translate queue to a priority.  Nothing to do.  A warning
1369          * has already been logged. */
1370     }
1371 }
1372
1373 static bool
1374 slave_enabled_cb(ofp_port_t ofp_port, void *ofproto_)
1375 {
1376     struct ofproto_dpif *ofproto = ofproto_;
1377     struct ofport_dpif *port;
1378
1379     switch (ofp_port) {
1380     case OFPP_IN_PORT:
1381     case OFPP_TABLE:
1382     case OFPP_NORMAL:
1383     case OFPP_FLOOD:
1384     case OFPP_ALL:
1385     case OFPP_NONE:
1386         return true;
1387     case OFPP_CONTROLLER: /* Not supported by the bundle action. */
1388         return false;
1389     default:
1390         port = get_ofp_port(ofproto, ofp_port);
1391         return port ? port->may_enable : false;
1392     }
1393 }
1394
1395 static void
1396 xlate_bundle_action(struct xlate_ctx *ctx,
1397                     const struct ofpact_bundle *bundle)
1398 {
1399     ofp_port_t port;
1400
1401     port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
1402                           slave_enabled_cb, ctx->ofproto);
1403     if (bundle->dst.field) {
1404         nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow);
1405     } else {
1406         xlate_output_action(ctx, port, 0, false);
1407     }
1408 }
1409
1410 static void
1411 xlate_learn_action(struct xlate_ctx *ctx,
1412                    const struct ofpact_learn *learn)
1413 {
1414     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
1415     struct ofputil_flow_mod fm;
1416     uint64_t ofpacts_stub[1024 / 8];
1417     struct ofpbuf ofpacts;
1418     int error;
1419
1420     ctx->xout->has_learn = true;
1421
1422     learn_mask(learn, &ctx->xout->wc);
1423
1424     if (!ctx->xin->may_learn) {
1425         return;
1426     }
1427
1428     ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1429     learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
1430
1431     error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
1432     if (error && !VLOG_DROP_WARN(&rl)) {
1433         VLOG_WARN("learning action failed to modify flow table (%s)",
1434                   ofperr_get_name(error));
1435     }
1436
1437     ofpbuf_uninit(&ofpacts);
1438 }
1439
1440 /* Reduces '*timeout' to no more than 'max'.  A value of zero in either case
1441  * means "infinite". */
1442 static void
1443 reduce_timeout(uint16_t max, uint16_t *timeout)
1444 {
1445     if (max && (!*timeout || *timeout > max)) {
1446         *timeout = max;
1447     }
1448 }
1449
1450 static void
1451 xlate_fin_timeout(struct xlate_ctx *ctx,
1452                   const struct ofpact_fin_timeout *oft)
1453 {
1454     if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
1455         struct rule_dpif *rule = ctx->rule;
1456
1457         reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
1458         reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
1459     }
1460 }
1461
1462 static void
1463 xlate_sample_action(struct xlate_ctx *ctx,
1464                     const struct ofpact_sample *os)
1465 {
1466   union user_action_cookie cookie;
1467   /* Scale the probability from 16-bit to 32-bit while representing
1468    * the same percentage. */
1469   uint32_t probability = (os->probability << 16) | os->probability;
1470
1471   commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1472                      &ctx->xout->odp_actions, &ctx->xout->wc);
1473
1474   compose_flow_sample_cookie(os->probability, os->collector_set_id,
1475                              os->obs_domain_id, os->obs_point_id, &cookie);
1476   compose_sample_action(ctx->ofproto, &ctx->xout->odp_actions, &ctx->xin->flow,
1477                         probability, &cookie, sizeof cookie.flow_sample);
1478 }
1479
1480 static bool
1481 may_receive(const struct ofport_dpif *port, struct xlate_ctx *ctx)
1482 {
1483     if (port->up.pp.config & (eth_addr_equals(ctx->xin->flow.dl_dst,
1484                                               eth_addr_stp)
1485                               ? OFPUTIL_PC_NO_RECV_STP
1486                               : OFPUTIL_PC_NO_RECV)) {
1487         return false;
1488     }
1489
1490     /* Only drop packets here if both forwarding and learning are
1491      * disabled.  If just learning is enabled, we need to have
1492      * OFPP_NORMAL and the learning action have a look at the packet
1493      * before we can drop it. */
1494     if (!stp_forward_in_state(port->stp_state)
1495             && !stp_learn_in_state(port->stp_state)) {
1496         return false;
1497     }
1498
1499     return true;
1500 }
1501
1502 static bool
1503 tunnel_ecn_ok(struct xlate_ctx *ctx)
1504 {
1505     if (is_ip_any(&ctx->base_flow)
1506         && (ctx->xin->flow.tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
1507         if ((ctx->base_flow.nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
1508             VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
1509                          " but is not ECN capable");
1510             return false;
1511         } else {
1512             /* Set the ECN CE value in the tunneled packet. */
1513             ctx->xin->flow.nw_tos |= IP_ECN_CE;
1514         }
1515     }
1516
1517     return true;
1518 }
1519
1520 static void
1521 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
1522                  struct xlate_ctx *ctx)
1523 {
1524     struct flow_wildcards *wc = &ctx->xout->wc;
1525     struct flow *flow = &ctx->xin->flow;
1526     bool was_evictable = true;
1527     const struct ofpact *a;
1528
1529     if (ctx->rule) {
1530         /* Don't let the rule we're working on get evicted underneath us. */
1531         was_evictable = ctx->rule->up.evictable;
1532         ctx->rule->up.evictable = false;
1533     }
1534
1535  do_xlate_actions_again:
1536     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1537         struct ofpact_controller *controller;
1538         const struct ofpact_metadata *metadata;
1539
1540         if (ctx->exit) {
1541             break;
1542         }
1543
1544         switch (a->type) {
1545         case OFPACT_OUTPUT:
1546             xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
1547                                 ofpact_get_OUTPUT(a)->max_len, true);
1548             break;
1549
1550         case OFPACT_CONTROLLER:
1551             controller = ofpact_get_CONTROLLER(a);
1552             execute_controller_action(ctx, controller->max_len,
1553                                       controller->reason,
1554                                       controller->controller_id);
1555             break;
1556
1557         case OFPACT_ENQUEUE:
1558             xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
1559             break;
1560
1561         case OFPACT_SET_VLAN_VID:
1562             flow->vlan_tci &= ~htons(VLAN_VID_MASK);
1563             flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
1564                                | htons(VLAN_CFI));
1565             break;
1566
1567         case OFPACT_SET_VLAN_PCP:
1568             flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
1569             flow->vlan_tci |=
1570                 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
1571                       | VLAN_CFI);
1572             break;
1573
1574         case OFPACT_STRIP_VLAN:
1575             flow->vlan_tci = htons(0);
1576             break;
1577
1578         case OFPACT_PUSH_VLAN:
1579             /* XXX 802.1AD(QinQ) */
1580             flow->vlan_tci = htons(VLAN_CFI);
1581             break;
1582
1583         case OFPACT_SET_ETH_SRC:
1584             memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1585             break;
1586
1587         case OFPACT_SET_ETH_DST:
1588             memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1589             break;
1590
1591         case OFPACT_SET_IPV4_SRC:
1592             if (flow->dl_type == htons(ETH_TYPE_IP)) {
1593                 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1594             }
1595             break;
1596
1597         case OFPACT_SET_IPV4_DST:
1598             if (flow->dl_type == htons(ETH_TYPE_IP)) {
1599                 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
1600             }
1601             break;
1602
1603         case OFPACT_SET_IPV4_DSCP:
1604             /* OpenFlow 1.0 only supports IPv4. */
1605             if (flow->dl_type == htons(ETH_TYPE_IP)) {
1606                 flow->nw_tos &= ~IP_DSCP_MASK;
1607                 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
1608             }
1609             break;
1610
1611         case OFPACT_SET_L4_SRC_PORT:
1612             memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1613             if (is_ip_any(flow)) {
1614                 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1615             }
1616             break;
1617
1618         case OFPACT_SET_L4_DST_PORT:
1619             memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1620             if (is_ip_any(flow)) {
1621                 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1622             }
1623             break;
1624
1625         case OFPACT_RESUBMIT:
1626             xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
1627             break;
1628
1629         case OFPACT_SET_TUNNEL:
1630             flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
1631             break;
1632
1633         case OFPACT_SET_QUEUE:
1634             xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
1635             break;
1636
1637         case OFPACT_POP_QUEUE:
1638             flow->skb_priority = ctx->orig_skb_priority;
1639             break;
1640
1641         case OFPACT_REG_MOVE:
1642             nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
1643             break;
1644
1645         case OFPACT_REG_LOAD:
1646             nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
1647             break;
1648
1649         case OFPACT_STACK_PUSH:
1650             nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
1651                                    &ctx->stack);
1652             break;
1653
1654         case OFPACT_STACK_POP:
1655             nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, &ctx->stack);
1656             break;
1657
1658         case OFPACT_PUSH_MPLS:
1659             compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a)->ethertype);
1660             break;
1661
1662         case OFPACT_POP_MPLS:
1663             compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
1664             break;
1665
1666         case OFPACT_SET_MPLS_TTL:
1667             if (compose_set_mpls_ttl_action(ctx,
1668                                             ofpact_get_SET_MPLS_TTL(a)->ttl)) {
1669                 goto out;
1670             }
1671             break;
1672
1673         case OFPACT_DEC_MPLS_TTL:
1674             if (compose_dec_mpls_ttl_action(ctx)) {
1675                 goto out;
1676             }
1677             break;
1678
1679         case OFPACT_DEC_TTL:
1680             if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
1681                 goto out;
1682             }
1683             break;
1684
1685         case OFPACT_NOTE:
1686             /* Nothing to do. */
1687             break;
1688
1689         case OFPACT_MULTIPATH:
1690             multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
1691             break;
1692
1693         case OFPACT_BUNDLE:
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.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1711             ctx->xout->has_fin_timeout = true;
1712             xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
1713             break;
1714
1715         case OFPACT_CLEAR_ACTIONS:
1716             /* XXX
1717              * Nothing to do because writa-actions is not supported for now.
1718              * When writa-actions is supported, clear-actions also must
1719              * be supported at the same time.
1720              */
1721             break;
1722
1723         case OFPACT_WRITE_METADATA:
1724             metadata = ofpact_get_WRITE_METADATA(a);
1725             flow->metadata &= ~metadata->mask;
1726             flow->metadata |= metadata->metadata & metadata->mask;
1727             break;
1728
1729         case OFPACT_METER:
1730             /* Not implemented yet. */
1731             break;
1732
1733         case OFPACT_GOTO_TABLE: {
1734             /* It is assumed that goto-table is the last action. */
1735             struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
1736             struct rule_dpif *rule;
1737
1738             ovs_assert(ctx->table_id < ogt->table_id);
1739
1740             ctx->table_id = ogt->table_id;
1741
1742             /* Look up a flow from the new table. */
1743             rule = rule_dpif_lookup_in_table(ctx->ofproto, flow, wc,
1744                                              ctx->table_id);
1745
1746             tag_the_flow(ctx, rule);
1747
1748             rule = ctx_rule_hooks(ctx, rule, true);
1749
1750             if (rule) {
1751                 if (ctx->rule) {
1752                     ctx->rule->up.evictable = was_evictable;
1753                 }
1754                 ctx->rule = rule;
1755                 was_evictable = rule->up.evictable;
1756                 rule->up.evictable = false;
1757
1758                 /* Tail recursion removal. */
1759                 ofpacts = rule->up.ofpacts;
1760                 ofpacts_len = rule->up.ofpacts_len;
1761                 goto do_xlate_actions_again;
1762             }
1763             break;
1764         }
1765
1766         case OFPACT_SAMPLE:
1767             xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
1768             break;
1769         }
1770     }
1771
1772 out:
1773     if (ctx->rule) {
1774         ctx->rule->up.evictable = was_evictable;
1775     }
1776 }
1777
1778 void
1779 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
1780               const struct flow *flow, struct rule_dpif *rule,
1781               uint8_t tcp_flags, const struct ofpbuf *packet)
1782 {
1783     xin->ofproto = ofproto;
1784     xin->flow = *flow;
1785     xin->packet = packet;
1786     xin->may_learn = packet != NULL;
1787     xin->rule = rule;
1788     xin->ofpacts = NULL;
1789     xin->ofpacts_len = 0;
1790     xin->tcp_flags = tcp_flags;
1791     xin->resubmit_hook = NULL;
1792     xin->report_hook = NULL;
1793     xin->resubmit_stats = NULL;
1794 }
1795
1796 void
1797 xlate_out_uninit(struct xlate_out *xout)
1798 {
1799     if (xout) {
1800         ofpbuf_uninit(&xout->odp_actions);
1801     }
1802 }
1803
1804 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
1805  * into datapath actions, using 'ctx', and discards the datapath actions. */
1806 void
1807 xlate_actions_for_side_effects(struct xlate_in *xin)
1808 {
1809     struct xlate_out xout;
1810
1811     xlate_actions(xin, &xout);
1812     xlate_out_uninit(&xout);
1813 }
1814
1815 static void
1816 xlate_report(struct xlate_ctx *ctx, const char *s)
1817 {
1818     if (ctx->xin->report_hook) {
1819         ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
1820     }
1821 }
1822
1823 void
1824 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
1825 {
1826     dst->wc = src->wc;
1827     dst->tags = src->tags;
1828     dst->slow = src->slow;
1829     dst->has_learn = src->has_learn;
1830     dst->has_normal = src->has_normal;
1831     dst->has_fin_timeout = src->has_fin_timeout;
1832     dst->nf_output_iface = src->nf_output_iface;
1833     dst->mirrors = src->mirrors;
1834
1835     ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
1836                     sizeof dst->odp_actions_stub);
1837     ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
1838                src->odp_actions.size);
1839 }
1840 \f
1841
1842 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
1843  * into datapath actions in 'odp_actions', using 'ctx'. */
1844 void
1845 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
1846 {
1847     /* Normally false.  Set to true if we ever hit MAX_RESUBMIT_RECURSION, so
1848      * that in the future we always keep a copy of the original flow for
1849      * tracing purposes. */
1850     static bool hit_resubmit_limit;
1851
1852     struct flow_wildcards *wc = &xout->wc;
1853     struct flow *flow = &xin->flow;
1854
1855     enum slow_path_reason special;
1856     const struct ofpact *ofpacts;
1857     struct ofport_dpif *in_port;
1858     struct flow orig_flow;
1859     struct xlate_ctx ctx;
1860     size_t ofpacts_len;
1861
1862     COVERAGE_INC(ofproto_dpif_xlate);
1863
1864     /* Flow initialization rules:
1865      * - 'base_flow' must match the kernel's view of the packet at the
1866      *   time that action processing starts.  'flow' represents any
1867      *   transformations we wish to make through actions.
1868      * - By default 'base_flow' and 'flow' are the same since the input
1869      *   packet matches the output before any actions are applied.
1870      * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
1871      *   of the received packet as seen by the kernel.  If we later output
1872      *   to another device without any modifications this will cause us to
1873      *   insert a new tag since the original one was stripped off by the
1874      *   VLAN device.
1875      * - Tunnel metadata as received is retained in 'flow'. This allows
1876      *   tunnel metadata matching also in later tables.
1877      *   Since a kernel action for setting the tunnel metadata will only be
1878      *   generated with actual tunnel output, changing the tunnel metadata
1879      *   values in 'flow' (such as tun_id) will only have effect with a later
1880      *   tunnel output action.
1881      * - Tunnel 'base_flow' is completely cleared since that is what the
1882      *   kernel does.  If we wish to maintain the original values an action
1883      *   needs to be generated. */
1884
1885     ctx.xin = xin;
1886     ctx.xout = xout;
1887
1888     ctx.ofproto = xin->ofproto;
1889     ctx.rule = xin->rule;
1890
1891     ctx.base_flow = *flow;
1892     memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
1893     ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
1894
1895     flow_wildcards_init_catchall(wc);
1896     memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
1897     memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
1898     memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1899     wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
1900
1901     if (tnl_port_should_receive(&ctx.xin->flow)) {
1902         memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
1903     }
1904     if (xin->ofproto->netflow) {
1905         netflow_mask_wc(wc);
1906     }
1907
1908     ctx.xout->tags = 0;
1909     ctx.xout->slow = 0;
1910     ctx.xout->has_learn = false;
1911     ctx.xout->has_normal = false;
1912     ctx.xout->has_fin_timeout = false;
1913     ctx.xout->nf_output_iface = NF_OUT_DROP;
1914     ctx.xout->mirrors = 0;
1915
1916     ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
1917                     sizeof ctx.xout->odp_actions_stub);
1918     ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
1919
1920     ctx.recurse = 0;
1921     ctx.max_resubmit_trigger = false;
1922     ctx.orig_skb_priority = flow->skb_priority;
1923     ctx.table_id = 0;
1924     ctx.exit = false;
1925
1926     if (xin->ofpacts) {
1927         ofpacts = xin->ofpacts;
1928         ofpacts_len = xin->ofpacts_len;
1929     } else if (xin->rule) {
1930         ofpacts = xin->rule->up.ofpacts;
1931         ofpacts_len = xin->rule->up.ofpacts_len;
1932     } else {
1933         NOT_REACHED();
1934     }
1935
1936     ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
1937
1938     if (ctx.ofproto->has_mirrors || hit_resubmit_limit) {
1939         /* Do this conditionally because the copy is expensive enough that it
1940          * shows up in profiles. */
1941         orig_flow = *flow;
1942     }
1943
1944     if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
1945         switch (ctx.ofproto->up.frag_handling) {
1946         case OFPC_FRAG_NORMAL:
1947             /* We must pretend that transport ports are unavailable. */
1948             flow->tp_src = ctx.base_flow.tp_src = htons(0);
1949             flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
1950             break;
1951
1952         case OFPC_FRAG_DROP:
1953             return;
1954
1955         case OFPC_FRAG_REASM:
1956             NOT_REACHED();
1957
1958         case OFPC_FRAG_NX_MATCH:
1959             /* Nothing to do. */
1960             break;
1961
1962         case OFPC_INVALID_TTL_TO_CONTROLLER:
1963             NOT_REACHED();
1964         }
1965     }
1966
1967     in_port = get_ofp_port(ctx.ofproto, flow->in_port.ofp_port);
1968     special = process_special(&ctx, flow, in_port, ctx.xin->packet);
1969     if (special) {
1970         ctx.xout->slow = special;
1971     } else {
1972         static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1973         size_t sample_actions_len;
1974         odp_port_t local_odp_port;
1975
1976         if (flow->in_port.ofp_port
1977             != vsp_realdev_to_vlandev(ctx.ofproto, flow->in_port.ofp_port,
1978                                       flow->vlan_tci)) {
1979             ctx.base_flow.vlan_tci = 0;
1980         }
1981
1982         add_sflow_action(&ctx);
1983         add_ipfix_action(&ctx);
1984         sample_actions_len = ctx.xout->odp_actions.size;
1985
1986         if (tunnel_ecn_ok(&ctx) && (!in_port || may_receive(in_port, &ctx))) {
1987             do_xlate_actions(ofpacts, ofpacts_len, &ctx);
1988
1989             /* We've let OFPP_NORMAL and the learning action look at the
1990              * packet, so drop it now if forwarding is disabled. */
1991             if (in_port && !stp_forward_in_state(in_port->stp_state)) {
1992                 ctx.xout->odp_actions.size = sample_actions_len;
1993             }
1994         }
1995
1996         if (ctx.max_resubmit_trigger && !ctx.xin->resubmit_hook) {
1997             if (!hit_resubmit_limit) {
1998                 /* We didn't record the original flow.  Make sure we do from
1999                  * now on. */
2000                 hit_resubmit_limit = true;
2001             } else if (!VLOG_DROP_ERR(&trace_rl)) {
2002                 struct ds ds = DS_EMPTY_INITIALIZER;
2003
2004                 ofproto_trace(ctx.ofproto, &orig_flow, ctx.xin->packet, &ds);
2005                 VLOG_ERR("Trace triggered by excessive resubmit "
2006                          "recursion:\n%s", ds_cstr(&ds));
2007                 ds_destroy(&ds);
2008             }
2009         }
2010
2011         local_odp_port = ofp_port_to_odp_port(ctx.ofproto, OFPP_LOCAL);
2012         if (!connmgr_must_output_local(ctx.ofproto->up.connmgr, flow,
2013                                        local_odp_port,
2014                                        ctx.xout->odp_actions.data,
2015                                        ctx.xout->odp_actions.size)) {
2016             compose_output_action(&ctx, OFPP_LOCAL);
2017         }
2018         if (ctx.ofproto->has_mirrors) {
2019             add_mirror_actions(&ctx, &orig_flow);
2020         }
2021         fix_sflow_action(&ctx);
2022     }
2023
2024     ofpbuf_uninit(&ctx.stack);
2025
2026     /* Clear the metadata and register wildcard masks, because we won't
2027      * use non-header fields as part of the cache. */
2028     memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2029     memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
2030 }