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