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