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