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