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