mac-learning: Implement per-port MAC learning fairness.
[cascardo/ovs.git] / ofproto / ofproto-dpif-xlate.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 <errno.h>
20 #include <arpa/inet.h>
21 #include <net/if.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24
25 #include "tnl-arp-cache.h"
26 #include "bfd.h"
27 #include "bitmap.h"
28 #include "bond.h"
29 #include "bundle.h"
30 #include "byte-order.h"
31 #include "cfm.h"
32 #include "connmgr.h"
33 #include "coverage.h"
34 #include "dpif.h"
35 #include "dynamic-string.h"
36 #include "in-band.h"
37 #include "lacp.h"
38 #include "learn.h"
39 #include "list.h"
40 #include "mac-learning.h"
41 #include "mcast-snooping.h"
42 #include "meta-flow.h"
43 #include "multipath.h"
44 #include "netdev-vport.h"
45 #include "netlink.h"
46 #include "nx-match.h"
47 #include "odp-execute.h"
48 #include "ofp-actions.h"
49 #include "ofproto/ofproto-dpif-ipfix.h"
50 #include "ofproto/ofproto-dpif-mirror.h"
51 #include "ofproto/ofproto-dpif-monitor.h"
52 #include "ofproto/ofproto-dpif-sflow.h"
53 #include "ofproto/ofproto-dpif.h"
54 #include "ofproto/ofproto-provider.h"
55 #include "packet-dpif.h"
56 #include "ovs-router.h"
57 #include "tnl-ports.h"
58 #include "tunnel.h"
59 #include "openvswitch/vlog.h"
60
61 COVERAGE_DEFINE(xlate_actions);
62 COVERAGE_DEFINE(xlate_actions_oversize);
63 COVERAGE_DEFINE(xlate_actions_too_many_output);
64
65 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
66
67 /* Maximum depth of flow table recursion (due to resubmit actions) in a
68  * flow translation. */
69 #define MAX_RESUBMIT_RECURSION 64
70 #define MAX_INTERNAL_RESUBMITS 1   /* Max resbmits allowed using rules in
71                                       internal table. */
72
73 /* Timeout for internal rules created to handle recirculation */
74 #define RECIRC_TIMEOUT 60
75
76 /* Maximum number of resubmit actions in a flow translation, whether they are
77  * recursive or not. */
78 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
79
80 struct xbridge {
81     struct hmap_node hmap_node;   /* Node in global 'xbridges' map. */
82     struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
83
84     struct ovs_list xbundles;     /* Owned xbundles. */
85     struct hmap xports;           /* Indexed by ofp_port. */
86
87     char *name;                   /* Name used in log messages. */
88     struct dpif *dpif;            /* Datapath interface. */
89     struct mac_learning *ml;      /* Mac learning handle. */
90     struct mcast_snooping *ms;    /* Multicast Snooping handle. */
91     struct mbridge *mbridge;      /* Mirroring. */
92     struct dpif_sflow *sflow;     /* SFlow handle, or null. */
93     struct dpif_ipfix *ipfix;     /* Ipfix handle, or null. */
94     struct netflow *netflow;      /* Netflow handle, or null. */
95     struct stp *stp;              /* STP or null if disabled. */
96     struct rstp *rstp;            /* RSTP or null if disabled. */
97
98     bool has_in_band;             /* Bridge has in band control? */
99     bool forward_bpdu;            /* Bridge forwards STP BPDUs? */
100
101     /* True if the datapath supports recirculation. */
102     bool enable_recirc;
103
104     /* True if the datapath supports variable-length
105      * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
106      * False if the datapath supports only 8-byte (or shorter) userdata. */
107     bool variable_length_userdata;
108
109     /* Number of MPLS label stack entries that the datapath supports
110      * in matches. */
111     size_t max_mpls_depth;
112
113     /* True if the datapath supports masked data in OVS_ACTION_ATTR_SET
114      * actions. */
115     bool masked_set_action;
116 };
117
118 struct xbundle {
119     struct hmap_node hmap_node;    /* In global 'xbundles' map. */
120     struct ofbundle *ofbundle;     /* Key in global 'xbundles' map. */
121
122     struct ovs_list list_node;     /* In parent 'xbridges' list. */
123     struct xbridge *xbridge;       /* Parent xbridge. */
124
125     struct ovs_list xports;        /* Contains "struct xport"s. */
126
127     char *name;                    /* Name used in log messages. */
128     struct bond *bond;             /* Nonnull iff more than one port. */
129     struct lacp *lacp;             /* LACP handle or null. */
130
131     enum port_vlan_mode vlan_mode; /* VLAN mode. */
132     int vlan;                      /* -1=trunk port, else a 12-bit VLAN ID. */
133     unsigned long *trunks;         /* Bitmap of trunked VLANs, if 'vlan' == -1.
134                                     * NULL if all VLANs are trunked. */
135     bool use_priority_tags;        /* Use 802.1p tag for frames in VLAN 0? */
136     bool floodable;                /* No port has OFPUTIL_PC_NO_FLOOD set? */
137 };
138
139 struct xport {
140     struct hmap_node hmap_node;      /* Node in global 'xports' map. */
141     struct ofport_dpif *ofport;      /* Key in global 'xports map. */
142
143     struct hmap_node ofp_node;       /* Node in parent xbridge 'xports' map. */
144     ofp_port_t ofp_port;             /* Key in parent xbridge 'xports' map. */
145
146     odp_port_t odp_port;             /* Datapath port number or ODPP_NONE. */
147
148     struct ovs_list bundle_node;     /* In parent xbundle (if it exists). */
149     struct xbundle *xbundle;         /* Parent xbundle or null. */
150
151     struct netdev *netdev;           /* 'ofport''s netdev. */
152
153     struct xbridge *xbridge;         /* Parent bridge. */
154     struct xport *peer;              /* Patch port peer or null. */
155
156     enum ofputil_port_config config; /* OpenFlow port configuration. */
157     enum ofputil_port_state state;   /* OpenFlow port state. */
158     int stp_port_no;                 /* STP port number or -1 if not in use. */
159     struct rstp_port *rstp_port;     /* RSTP port or null. */
160
161     struct hmap skb_priorities;      /* Map of 'skb_priority_to_dscp's. */
162
163     bool may_enable;                 /* May be enabled in bonds. */
164     bool is_tunnel;                  /* Is a tunnel port. */
165
166     struct cfm *cfm;                 /* CFM handle or null. */
167     struct bfd *bfd;                 /* BFD handle or null. */
168 };
169
170 struct xlate_ctx {
171     struct xlate_in *xin;
172     struct xlate_out *xout;
173
174     const struct xbridge *xbridge;
175
176     /* Flow at the last commit. */
177     struct flow base_flow;
178
179     /* Tunnel IP destination address as received.  This is stored separately
180      * as the base_flow.tunnel is cleared on init to reflect the datapath
181      * behavior.  Used to make sure not to send tunneled output to ourselves,
182      * which might lead to an infinite loop.  This could happen easily
183      * if a tunnel is marked as 'ip_remote=flow', and the flow does not
184      * actually set the tun_dst field. */
185     ovs_be32 orig_tunnel_ip_dst;
186
187     /* Stack for the push and pop actions.  Each stack element is of type
188      * "union mf_subvalue". */
189     union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
190     struct ofpbuf stack;
191
192     /* The rule that we are currently translating, or NULL. */
193     struct rule_dpif *rule;
194
195     /* Resubmit statistics, via xlate_table_action(). */
196     int recurse;                /* Current resubmit nesting depth. */
197     int resubmits;              /* Total number of resubmits. */
198     bool in_group;              /* Currently translating ofgroup, if true. */
199     bool in_action_set;         /* Currently translating action_set, if true. */
200
201     uint32_t orig_skb_priority; /* Priority when packet arrived. */
202     uint8_t table_id;           /* OpenFlow table ID where flow was found. */
203     uint32_t sflow_n_outputs;   /* Number of output ports. */
204     odp_port_t sflow_odp_port;  /* Output port for composing sFlow action. */
205     uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
206     bool exit;                  /* No further actions should be processed. */
207
208     bool use_recirc;            /* Should generate recirc? */
209     struct xlate_recirc recirc; /* Information used for generating
210                                  * recirculation actions */
211
212     /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
213      * This is a trigger for recirculation in cases where translating an action
214      * or looking up a flow requires access to the fields of the packet after
215      * the MPLS label stack that was originally present. */
216     bool was_mpls;
217
218     /* OpenFlow 1.1+ action set.
219      *
220      * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
221      * When translation is otherwise complete, ofpacts_execute_action_set()
222      * converts it to a set of "struct ofpact"s that can be translated into
223      * datapath actions.   */
224     bool action_set_has_group;  /* Action set contains OFPACT_GROUP? */
225     struct ofpbuf action_set;   /* Action set. */
226     uint64_t action_set_stub[1024 / 8];
227 };
228
229 /* A controller may use OFPP_NONE as the ingress port to indicate that
230  * it did not arrive on a "real" port.  'ofpp_none_bundle' exists for
231  * when an input bundle is needed for validation (e.g., mirroring or
232  * OFPP_NORMAL processing).  It is not connected to an 'ofproto' or have
233  * any 'port' structs, so care must be taken when dealing with it. */
234 static struct xbundle ofpp_none_bundle = {
235     .name      = "OFPP_NONE",
236     .vlan_mode = PORT_VLAN_TRUNK
237 };
238
239 /* Node in 'xport''s 'skb_priorities' map.  Used to maintain a map from
240  * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
241  * traffic egressing the 'ofport' with that priority should be marked with. */
242 struct skb_priority_to_dscp {
243     struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
244     uint32_t skb_priority;      /* Priority of this queue (see struct flow). */
245
246     uint8_t dscp;               /* DSCP bits to mark outgoing traffic with. */
247 };
248
249 enum xc_type {
250     XC_RULE,
251     XC_BOND,
252     XC_NETDEV,
253     XC_NETFLOW,
254     XC_MIRROR,
255     XC_LEARN,
256     XC_NORMAL,
257     XC_FIN_TIMEOUT,
258     XC_GROUP,
259     XC_TNL_ARP,
260 };
261
262 /* xlate_cache entries hold enough information to perform the side effects of
263  * xlate_actions() for a rule, without needing to perform rule translation
264  * from scratch. The primary usage of these is to submit statistics to objects
265  * that a flow relates to, although they may be used for other effects as well
266  * (for instance, refreshing hard timeouts for learned flows). */
267 struct xc_entry {
268     enum xc_type type;
269     union {
270         struct rule_dpif *rule;
271         struct {
272             struct netdev *tx;
273             struct netdev *rx;
274             struct bfd *bfd;
275         } dev;
276         struct {
277             struct netflow *netflow;
278             struct flow *flow;
279             ofp_port_t iface;
280         } nf;
281         struct {
282             struct mbridge *mbridge;
283             mirror_mask_t mirrors;
284         } mirror;
285         struct {
286             struct bond *bond;
287             struct flow *flow;
288             uint16_t vid;
289         } bond;
290         struct {
291             struct ofproto_dpif *ofproto;
292             struct ofputil_flow_mod *fm;
293             struct ofpbuf *ofpacts;
294         } learn;
295         struct {
296             struct ofproto_dpif *ofproto;
297             struct flow *flow;
298             int vlan;
299         } normal;
300         struct {
301             struct rule_dpif *rule;
302             uint16_t idle;
303             uint16_t hard;
304         } fin;
305         struct {
306             struct group_dpif *group;
307             struct ofputil_bucket *bucket;
308         } group;
309         struct {
310             char br_name[IFNAMSIZ];
311             ovs_be32 d_ip;
312         } tnl_arp_cache;
313     } u;
314 };
315
316 #define XC_ENTRY_FOR_EACH(entry, entries, xcache)               \
317     entries = xcache->entries;                                  \
318     for (entry = ofpbuf_try_pull(&entries, sizeof *entry);      \
319          entry;                                                 \
320          entry = ofpbuf_try_pull(&entries, sizeof *entry))
321
322 struct xlate_cache {
323     struct ofpbuf entries;
324 };
325
326 /* Xlate config contains hash maps of all bridges, bundles and ports.
327  * Xcfgp contains the pointer to the current xlate configuration.
328  * When the main thread needs to change the configuration, it copies xcfgp to
329  * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
330  * does not block handler and revalidator threads. */
331 struct xlate_cfg {
332     struct hmap xbridges;
333     struct hmap xbundles;
334     struct hmap xports;
335 };
336 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
337 static struct xlate_cfg *new_xcfg = NULL;
338
339 static bool may_receive(const struct xport *, struct xlate_ctx *);
340 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
341                              struct xlate_ctx *);
342 static void xlate_normal(struct xlate_ctx *);
343 static inline void xlate_report(struct xlate_ctx *, const char *);
344 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
345                                uint8_t table_id, bool may_packet_in,
346                                bool honor_table_miss);
347 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
348 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
349 static void output_normal(struct xlate_ctx *, const struct xbundle *,
350                           uint16_t vlan);
351 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
352
353 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
354                                       const struct ofproto_dpif *);
355 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
356                                       const struct ofbundle *);
357 static struct xport *xport_lookup(struct xlate_cfg *,
358                                   const struct ofport_dpif *);
359 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
360 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
361                                                      uint32_t skb_priority);
362 static void clear_skb_priorities(struct xport *);
363 static size_t count_skb_priorities(const struct xport *);
364 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
365                                    uint8_t *dscp);
366
367 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
368                                               enum xc_type type);
369 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
370 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
371 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
372 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
373                               const struct mac_learning *, struct stp *,
374                               struct rstp *, const struct mcast_snooping *,
375                               const struct mbridge *,
376                               const struct dpif_sflow *,
377                               const struct dpif_ipfix *,
378                               const struct netflow *,
379                               bool forward_bpdu, bool has_in_band,
380                               bool enable_recirc,
381                               bool variable_length_userdata,
382                               size_t max_mpls_depth,
383                               bool masked_set_action);
384 static void xlate_xbundle_set(struct xbundle *xbundle,
385                               enum port_vlan_mode vlan_mode, int vlan,
386                               unsigned long *trunks, bool use_priority_tags,
387                               const struct bond *bond, const struct lacp *lacp,
388                               bool floodable);
389 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
390                             const struct netdev *netdev, const struct cfm *cfm,
391                             const struct bfd *bfd, int stp_port_no,
392                             const struct rstp_port *rstp_port,
393                             enum ofputil_port_config config,
394                             enum ofputil_port_state state, bool is_tunnel,
395                             bool may_enable);
396 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
397 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
398 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
399 static void xlate_xbridge_copy(struct xbridge *);
400 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
401 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
402                              struct xport *);
403 static void xlate_xcfg_free(struct xlate_cfg *);
404
405 static inline void
406 xlate_report(struct xlate_ctx *ctx, const char *s)
407 {
408     if (OVS_UNLIKELY(ctx->xin->report_hook)) {
409         ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
410     }
411 }
412
413 static void
414 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
415 {
416     list_init(&xbridge->xbundles);
417     hmap_init(&xbridge->xports);
418     hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
419                 hash_pointer(xbridge->ofproto, 0));
420 }
421
422 static void
423 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
424 {
425     list_init(&xbundle->xports);
426     list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
427     hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
428                 hash_pointer(xbundle->ofbundle, 0));
429 }
430
431 static void
432 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
433 {
434     hmap_init(&xport->skb_priorities);
435     hmap_insert(&xcfg->xports, &xport->hmap_node,
436                 hash_pointer(xport->ofport, 0));
437     hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
438                 hash_ofp_port(xport->ofp_port));
439 }
440
441 static void
442 xlate_xbridge_set(struct xbridge *xbridge,
443                   struct dpif *dpif,
444                   const struct mac_learning *ml, struct stp *stp,
445                   struct rstp *rstp, const struct mcast_snooping *ms,
446                   const struct mbridge *mbridge,
447                   const struct dpif_sflow *sflow,
448                   const struct dpif_ipfix *ipfix,
449                   const struct netflow *netflow,
450                   bool forward_bpdu, bool has_in_band,
451                   bool enable_recirc,
452                   bool variable_length_userdata,
453                   size_t max_mpls_depth,
454                   bool masked_set_action)
455 {
456     if (xbridge->ml != ml) {
457         mac_learning_unref(xbridge->ml);
458         xbridge->ml = mac_learning_ref(ml);
459     }
460
461     if (xbridge->ms != ms) {
462         mcast_snooping_unref(xbridge->ms);
463         xbridge->ms = mcast_snooping_ref(ms);
464     }
465
466     if (xbridge->mbridge != mbridge) {
467         mbridge_unref(xbridge->mbridge);
468         xbridge->mbridge = mbridge_ref(mbridge);
469     }
470
471     if (xbridge->sflow != sflow) {
472         dpif_sflow_unref(xbridge->sflow);
473         xbridge->sflow = dpif_sflow_ref(sflow);
474     }
475
476     if (xbridge->ipfix != ipfix) {
477         dpif_ipfix_unref(xbridge->ipfix);
478         xbridge->ipfix = dpif_ipfix_ref(ipfix);
479     }
480
481     if (xbridge->stp != stp) {
482         stp_unref(xbridge->stp);
483         xbridge->stp = stp_ref(stp);
484     }
485
486     if (xbridge->rstp != rstp) {
487         rstp_unref(xbridge->rstp);
488         xbridge->rstp = rstp_ref(rstp);
489     }
490
491     if (xbridge->netflow != netflow) {
492         netflow_unref(xbridge->netflow);
493         xbridge->netflow = netflow_ref(netflow);
494     }
495
496     xbridge->dpif = dpif;
497     xbridge->forward_bpdu = forward_bpdu;
498     xbridge->has_in_band = has_in_band;
499     xbridge->enable_recirc = enable_recirc;
500     xbridge->variable_length_userdata = variable_length_userdata;
501     xbridge->max_mpls_depth = max_mpls_depth;
502     xbridge->masked_set_action = masked_set_action;
503 }
504
505 static void
506 xlate_xbundle_set(struct xbundle *xbundle,
507                   enum port_vlan_mode vlan_mode, int vlan,
508                   unsigned long *trunks, bool use_priority_tags,
509                   const struct bond *bond, const struct lacp *lacp,
510                   bool floodable)
511 {
512     ovs_assert(xbundle->xbridge);
513
514     xbundle->vlan_mode = vlan_mode;
515     xbundle->vlan = vlan;
516     xbundle->trunks = trunks;
517     xbundle->use_priority_tags = use_priority_tags;
518     xbundle->floodable = floodable;
519
520     if (xbundle->bond != bond) {
521         bond_unref(xbundle->bond);
522         xbundle->bond = bond_ref(bond);
523     }
524
525     if (xbundle->lacp != lacp) {
526         lacp_unref(xbundle->lacp);
527         xbundle->lacp = lacp_ref(lacp);
528     }
529 }
530
531 static void
532 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
533                 const struct netdev *netdev, const struct cfm *cfm,
534                 const struct bfd *bfd, int stp_port_no,
535                 const struct rstp_port* rstp_port,
536                 enum ofputil_port_config config, enum ofputil_port_state state,
537                 bool is_tunnel, bool may_enable)
538 {
539     xport->config = config;
540     xport->state = state;
541     xport->stp_port_no = stp_port_no;
542     xport->is_tunnel = is_tunnel;
543     xport->may_enable = may_enable;
544     xport->odp_port = odp_port;
545
546     if (xport->rstp_port != rstp_port) {
547         rstp_port_unref(xport->rstp_port);
548         xport->rstp_port = rstp_port_ref(rstp_port);
549     }
550
551     if (xport->cfm != cfm) {
552         cfm_unref(xport->cfm);
553         xport->cfm = cfm_ref(cfm);
554     }
555
556     if (xport->bfd != bfd) {
557         bfd_unref(xport->bfd);
558         xport->bfd = bfd_ref(bfd);
559     }
560
561     if (xport->netdev != netdev) {
562         netdev_close(xport->netdev);
563         xport->netdev = netdev_ref(netdev);
564     }
565 }
566
567 static void
568 xlate_xbridge_copy(struct xbridge *xbridge)
569 {
570     struct xbundle *xbundle;
571     struct xport *xport;
572     struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
573     new_xbridge->ofproto = xbridge->ofproto;
574     new_xbridge->name = xstrdup(xbridge->name);
575     xlate_xbridge_init(new_xcfg, new_xbridge);
576
577     xlate_xbridge_set(new_xbridge,
578                       xbridge->dpif, xbridge->ml, xbridge->stp,
579                       xbridge->rstp, xbridge->ms, xbridge->mbridge,
580                       xbridge->sflow, xbridge->ipfix, xbridge->netflow,
581                       xbridge->forward_bpdu,
582                       xbridge->has_in_band, xbridge->enable_recirc,
583                       xbridge->variable_length_userdata,
584                       xbridge->max_mpls_depth, xbridge->masked_set_action);
585     LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
586         xlate_xbundle_copy(new_xbridge, xbundle);
587     }
588
589     /* Copy xports which are not part of a xbundle */
590     HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
591         if (!xport->xbundle) {
592             xlate_xport_copy(new_xbridge, NULL, xport);
593         }
594     }
595 }
596
597 static void
598 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
599 {
600     struct xport *xport;
601     struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
602     new_xbundle->ofbundle = xbundle->ofbundle;
603     new_xbundle->xbridge = xbridge;
604     new_xbundle->name = xstrdup(xbundle->name);
605     xlate_xbundle_init(new_xcfg, new_xbundle);
606
607     xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
608                       xbundle->vlan, xbundle->trunks,
609                       xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
610                       xbundle->floodable);
611     LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
612         xlate_xport_copy(xbridge, new_xbundle, xport);
613     }
614 }
615
616 static void
617 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
618                  struct xport *xport)
619 {
620     struct skb_priority_to_dscp *pdscp, *new_pdscp;
621     struct xport *new_xport = xzalloc(sizeof *xport);
622     new_xport->ofport = xport->ofport;
623     new_xport->ofp_port = xport->ofp_port;
624     new_xport->xbridge = xbridge;
625     xlate_xport_init(new_xcfg, new_xport);
626
627     xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
628                     xport->bfd, xport->stp_port_no, xport->rstp_port,
629                     xport->config, xport->state, xport->is_tunnel,
630                     xport->may_enable);
631
632     if (xport->peer) {
633         struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
634         if (peer) {
635             new_xport->peer = peer;
636             new_xport->peer->peer = new_xport;
637         }
638     }
639
640     if (xbundle) {
641         new_xport->xbundle = xbundle;
642         list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
643     }
644
645     HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
646         new_pdscp = xmalloc(sizeof *pdscp);
647         new_pdscp->skb_priority = pdscp->skb_priority;
648         new_pdscp->dscp = pdscp->dscp;
649         hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
650                     hash_int(new_pdscp->skb_priority, 0));
651     }
652 }
653
654 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
655  * configuration in xcfgp.
656  *
657  * This needs to be called after editing the xlate configuration.
658  *
659  * Functions that edit the new xlate configuration are
660  * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
661  *
662  * A sample workflow:
663  *
664  * xlate_txn_start();
665  * ...
666  * edit_xlate_configuration();
667  * ...
668  * xlate_txn_commit(); */
669 void
670 xlate_txn_commit(void)
671 {
672     struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
673
674     ovsrcu_set(&xcfgp, new_xcfg);
675     ovsrcu_synchronize();
676     xlate_xcfg_free(xcfg);
677     new_xcfg = NULL;
678 }
679
680 /* Copies the current xlate configuration in xcfgp to new_xcfg.
681  *
682  * This needs to be called prior to editing the xlate configuration. */
683 void
684 xlate_txn_start(void)
685 {
686     struct xbridge *xbridge;
687     struct xlate_cfg *xcfg;
688
689     ovs_assert(!new_xcfg);
690
691     new_xcfg = xmalloc(sizeof *new_xcfg);
692     hmap_init(&new_xcfg->xbridges);
693     hmap_init(&new_xcfg->xbundles);
694     hmap_init(&new_xcfg->xports);
695
696     xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
697     if (!xcfg) {
698         return;
699     }
700
701     HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
702         xlate_xbridge_copy(xbridge);
703     }
704 }
705
706
707 static void
708 xlate_xcfg_free(struct xlate_cfg *xcfg)
709 {
710     struct xbridge *xbridge, *next_xbridge;
711
712     if (!xcfg) {
713         return;
714     }
715
716     HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
717         xlate_xbridge_remove(xcfg, xbridge);
718     }
719
720     hmap_destroy(&xcfg->xbridges);
721     hmap_destroy(&xcfg->xbundles);
722     hmap_destroy(&xcfg->xports);
723     free(xcfg);
724 }
725
726 void
727 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
728                   struct dpif *dpif,
729                   const struct mac_learning *ml, struct stp *stp,
730                   struct rstp *rstp, const struct mcast_snooping *ms,
731                   const struct mbridge *mbridge,
732                   const struct dpif_sflow *sflow,
733                   const struct dpif_ipfix *ipfix,
734                   const struct netflow *netflow,
735                   bool forward_bpdu, bool has_in_band, bool enable_recirc,
736                   bool variable_length_userdata, size_t max_mpls_depth,
737                   bool masked_set_action)
738 {
739     struct xbridge *xbridge;
740
741     ovs_assert(new_xcfg);
742
743     xbridge = xbridge_lookup(new_xcfg, ofproto);
744     if (!xbridge) {
745         xbridge = xzalloc(sizeof *xbridge);
746         xbridge->ofproto = ofproto;
747
748         xlate_xbridge_init(new_xcfg, xbridge);
749     }
750
751     free(xbridge->name);
752     xbridge->name = xstrdup(name);
753
754     xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
755                       netflow, forward_bpdu, has_in_band, enable_recirc,
756                       variable_length_userdata, max_mpls_depth,
757                       masked_set_action);
758 }
759
760 static void
761 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
762 {
763     struct xbundle *xbundle, *next_xbundle;
764     struct xport *xport, *next_xport;
765
766     if (!xbridge) {
767         return;
768     }
769
770     HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
771         xlate_xport_remove(xcfg, xport);
772     }
773
774     LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
775         xlate_xbundle_remove(xcfg, xbundle);
776     }
777
778     hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
779     mac_learning_unref(xbridge->ml);
780     mcast_snooping_unref(xbridge->ms);
781     mbridge_unref(xbridge->mbridge);
782     dpif_sflow_unref(xbridge->sflow);
783     dpif_ipfix_unref(xbridge->ipfix);
784     stp_unref(xbridge->stp);
785     rstp_unref(xbridge->rstp);
786     hmap_destroy(&xbridge->xports);
787     free(xbridge->name);
788     free(xbridge);
789 }
790
791 void
792 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
793 {
794     struct xbridge *xbridge;
795
796     ovs_assert(new_xcfg);
797
798     xbridge = xbridge_lookup(new_xcfg, ofproto);
799     xlate_xbridge_remove(new_xcfg, xbridge);
800 }
801
802 void
803 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
804                  const char *name, enum port_vlan_mode vlan_mode, int vlan,
805                  unsigned long *trunks, bool use_priority_tags,
806                  const struct bond *bond, const struct lacp *lacp,
807                  bool floodable)
808 {
809     struct xbundle *xbundle;
810
811     ovs_assert(new_xcfg);
812
813     xbundle = xbundle_lookup(new_xcfg, ofbundle);
814     if (!xbundle) {
815         xbundle = xzalloc(sizeof *xbundle);
816         xbundle->ofbundle = ofbundle;
817         xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
818
819         xlate_xbundle_init(new_xcfg, xbundle);
820     }
821
822     free(xbundle->name);
823     xbundle->name = xstrdup(name);
824
825     xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
826                       use_priority_tags, bond, lacp, floodable);
827 }
828
829 static void
830 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
831 {
832     struct xport *xport, *next;
833
834     if (!xbundle) {
835         return;
836     }
837
838     LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
839         list_remove(&xport->bundle_node);
840         xport->xbundle = NULL;
841     }
842
843     hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
844     list_remove(&xbundle->list_node);
845     bond_unref(xbundle->bond);
846     lacp_unref(xbundle->lacp);
847     free(xbundle->name);
848     free(xbundle);
849 }
850
851 void
852 xlate_bundle_remove(struct ofbundle *ofbundle)
853 {
854     struct xbundle *xbundle;
855
856     ovs_assert(new_xcfg);
857
858     xbundle = xbundle_lookup(new_xcfg, ofbundle);
859     xlate_xbundle_remove(new_xcfg, xbundle);
860 }
861
862 void
863 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
864                  struct ofport_dpif *ofport, ofp_port_t ofp_port,
865                  odp_port_t odp_port, const struct netdev *netdev,
866                  const struct cfm *cfm, const struct bfd *bfd,
867                  struct ofport_dpif *peer, int stp_port_no,
868                  const struct rstp_port *rstp_port,
869                  const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
870                  enum ofputil_port_config config,
871                  enum ofputil_port_state state, bool is_tunnel,
872                  bool may_enable)
873 {
874     size_t i;
875     struct xport *xport;
876
877     ovs_assert(new_xcfg);
878
879     xport = xport_lookup(new_xcfg, ofport);
880     if (!xport) {
881         xport = xzalloc(sizeof *xport);
882         xport->ofport = ofport;
883         xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
884         xport->ofp_port = ofp_port;
885
886         xlate_xport_init(new_xcfg, xport);
887     }
888
889     ovs_assert(xport->ofp_port == ofp_port);
890
891     xlate_xport_set(xport, odp_port, netdev, cfm, bfd, stp_port_no,
892                     rstp_port, config, state, is_tunnel, may_enable);
893
894     if (xport->peer) {
895         xport->peer->peer = NULL;
896     }
897     xport->peer = xport_lookup(new_xcfg, peer);
898     if (xport->peer) {
899         xport->peer->peer = xport;
900     }
901
902     if (xport->xbundle) {
903         list_remove(&xport->bundle_node);
904     }
905     xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
906     if (xport->xbundle) {
907         list_insert(&xport->xbundle->xports, &xport->bundle_node);
908     }
909
910     clear_skb_priorities(xport);
911     for (i = 0; i < n_qdscp; i++) {
912         struct skb_priority_to_dscp *pdscp;
913         uint32_t skb_priority;
914
915         if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
916                                    &skb_priority)) {
917             continue;
918         }
919
920         pdscp = xmalloc(sizeof *pdscp);
921         pdscp->skb_priority = skb_priority;
922         pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
923         hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
924                     hash_int(pdscp->skb_priority, 0));
925     }
926 }
927
928 static void
929 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
930 {
931     if (!xport) {
932         return;
933     }
934
935     if (xport->peer) {
936         xport->peer->peer = NULL;
937         xport->peer = NULL;
938     }
939
940     if (xport->xbundle) {
941         list_remove(&xport->bundle_node);
942     }
943
944     clear_skb_priorities(xport);
945     hmap_destroy(&xport->skb_priorities);
946
947     hmap_remove(&xcfg->xports, &xport->hmap_node);
948     hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
949
950     netdev_close(xport->netdev);
951     rstp_port_unref(xport->rstp_port);
952     cfm_unref(xport->cfm);
953     bfd_unref(xport->bfd);
954     free(xport);
955 }
956
957 void
958 xlate_ofport_remove(struct ofport_dpif *ofport)
959 {
960     struct xport *xport;
961
962     ovs_assert(new_xcfg);
963
964     xport = xport_lookup(new_xcfg, ofport);
965     xlate_xport_remove(new_xcfg, xport);
966 }
967
968 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
969  * returns the corresponding struct xport, or NULL if none is found. */
970 static struct xport *
971 xlate_lookup_xport(const struct dpif_backer *backer, const struct flow *flow)
972 {
973     struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
974
975     return xport_lookup(xcfg, tnl_port_should_receive(flow)
976                          ? tnl_port_receive(flow)
977                          : odp_port_to_ofport(backer, flow->in_port.odp_port));
978 }
979
980 static struct ofproto_dpif *
981 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
982                       ofp_port_t *ofp_in_port, const struct xport **xportp)
983 {
984     struct ofproto_dpif *recv_ofproto = NULL;
985     struct ofproto_dpif *recirc_ofproto = NULL;
986     const struct xport *xport;
987     ofp_port_t in_port = OFPP_NONE;
988
989     *xportp = xport = xlate_lookup_xport(backer, flow);
990
991     if (xport) {
992         recv_ofproto = xport->xbridge->ofproto;
993         in_port = xport->ofp_port;
994     }
995
996     /* When recirc_id is set in 'flow', checks whether the ofproto_dpif that
997      * corresponds to the recirc_id is same as the receiving bridge.  If they
998      * are the same, uses the 'recv_ofproto' and keeps the 'ofp_in_port' as
999      * assigned.  Otherwise, uses the 'recirc_ofproto' that owns recirc_id and
1000      * assigns OFPP_NONE to 'ofp_in_port'.  Doing this is in that, the
1001      * recirculated flow must be processced by the ofproto which originates
1002      * the recirculation, and as bridges can only see their own ports, the
1003      * in_port of the 'recv_ofproto' should not be passed to the
1004      * 'recirc_ofproto'.
1005      *
1006      * Admittedly, setting the 'ofp_in_port' to OFPP_NONE limits the
1007      * 'recirc_ofproto' from meaningfully matching on in_port of recirculated
1008      * flow, and should be fixed in the near future.
1009      *
1010      * TODO: Restore the original patch port.
1011      */
1012     if (recv_ofproto && flow->recirc_id) {
1013         recirc_ofproto = ofproto_dpif_recirc_get_ofproto(backer,
1014                                                          flow->recirc_id);
1015         if (recv_ofproto != recirc_ofproto) {
1016             *xportp = xport = NULL;
1017             in_port = OFPP_NONE;
1018         }
1019     }
1020
1021     if (ofp_in_port) {
1022         *ofp_in_port = in_port;
1023     }
1024
1025     return xport ? recv_ofproto : recirc_ofproto;
1026 }
1027
1028 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1029  * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1030 struct ofproto_dpif *
1031 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1032                      ofp_port_t *ofp_in_port)
1033 {
1034     const struct xport *xport;
1035
1036     return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1037 }
1038
1039 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1040  * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1041  * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1042  * handles for those protocols if they're enabled.  Caller may use the returned
1043  * pointers until quiescing, for longer term use additional references must
1044  * be taken.
1045  *
1046  * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1047  */
1048 int
1049 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1050              struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1051              struct dpif_sflow **sflow, struct netflow **netflow,
1052              ofp_port_t *ofp_in_port)
1053 {
1054     struct ofproto_dpif *ofproto;
1055     const struct xport *xport;
1056
1057     ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1058
1059     if (!ofproto) {
1060         return ENODEV;
1061     }
1062
1063     if (ofprotop) {
1064         *ofprotop = ofproto;
1065     }
1066
1067     if (ipfix) {
1068         *ipfix = xport ? xport->xbridge->ipfix : NULL;
1069     }
1070
1071     if (sflow) {
1072         *sflow = xport ? xport->xbridge->sflow : NULL;
1073     }
1074
1075     if (netflow) {
1076         *netflow = xport ? xport->xbridge->netflow : NULL;
1077     }
1078
1079     return 0;
1080 }
1081
1082 static struct xbridge *
1083 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1084 {
1085     struct hmap *xbridges;
1086     struct xbridge *xbridge;
1087
1088     if (!ofproto || !xcfg) {
1089         return NULL;
1090     }
1091
1092     xbridges = &xcfg->xbridges;
1093
1094     HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1095                              xbridges) {
1096         if (xbridge->ofproto == ofproto) {
1097             return xbridge;
1098         }
1099     }
1100     return NULL;
1101 }
1102
1103 static struct xbundle *
1104 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1105 {
1106     struct hmap *xbundles;
1107     struct xbundle *xbundle;
1108
1109     if (!ofbundle || !xcfg) {
1110         return NULL;
1111     }
1112
1113     xbundles = &xcfg->xbundles;
1114
1115     HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1116                              xbundles) {
1117         if (xbundle->ofbundle == ofbundle) {
1118             return xbundle;
1119         }
1120     }
1121     return NULL;
1122 }
1123
1124 static struct xport *
1125 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1126 {
1127     struct hmap *xports;
1128     struct xport *xport;
1129
1130     if (!ofport || !xcfg) {
1131         return NULL;
1132     }
1133
1134     xports = &xcfg->xports;
1135
1136     HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1137                              xports) {
1138         if (xport->ofport == ofport) {
1139             return xport;
1140         }
1141     }
1142     return NULL;
1143 }
1144
1145 static struct stp_port *
1146 xport_get_stp_port(const struct xport *xport)
1147 {
1148     return xport->xbridge->stp && xport->stp_port_no != -1
1149         ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1150         : NULL;
1151 }
1152
1153 static bool
1154 xport_stp_learn_state(const struct xport *xport)
1155 {
1156     struct stp_port *sp = xport_get_stp_port(xport);
1157     return sp
1158         ? stp_learn_in_state(stp_port_get_state(sp))
1159         : true;
1160 }
1161
1162 static bool
1163 xport_stp_forward_state(const struct xport *xport)
1164 {
1165     struct stp_port *sp = xport_get_stp_port(xport);
1166     return sp
1167         ? stp_forward_in_state(stp_port_get_state(sp))
1168         : true;
1169 }
1170
1171 static bool
1172 xport_stp_should_forward_bpdu(const struct xport *xport)
1173 {
1174     struct stp_port *sp = xport_get_stp_port(xport);
1175     return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1176 }
1177
1178 /* Returns true if STP should process 'flow'.  Sets fields in 'wc' that
1179  * were used to make the determination.*/
1180 static bool
1181 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1182 {
1183     /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1184     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1185     return is_stp(flow);
1186 }
1187
1188 static void
1189 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
1190 {
1191     struct stp_port *sp = xport_get_stp_port(xport);
1192     struct ofpbuf payload = *packet;
1193     struct eth_header *eth = ofpbuf_data(&payload);
1194
1195     /* Sink packets on ports that have STP disabled when the bridge has
1196      * STP enabled. */
1197     if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1198         return;
1199     }
1200
1201     /* Trim off padding on payload. */
1202     if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1203         ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1204     }
1205
1206     if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1207         stp_received_bpdu(sp, ofpbuf_data(&payload), ofpbuf_size(&payload));
1208     }
1209 }
1210
1211 static enum rstp_state
1212 xport_get_rstp_port_state(const struct xport *xport)
1213 {
1214     return xport->rstp_port
1215         ? rstp_port_get_state(xport->rstp_port)
1216         : RSTP_DISABLED;
1217 }
1218
1219 static bool
1220 xport_rstp_learn_state(const struct xport *xport)
1221 {
1222     return xport->xbridge->rstp && xport->rstp_port
1223         ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1224         : true;
1225 }
1226
1227 static bool
1228 xport_rstp_forward_state(const struct xport *xport)
1229 {
1230     return xport->xbridge->rstp && xport->rstp_port
1231         ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1232         : true;
1233 }
1234
1235 static bool
1236 xport_rstp_should_manage_bpdu(const struct xport *xport)
1237 {
1238     return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1239 }
1240
1241 static void
1242 rstp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
1243 {
1244     struct ofpbuf payload = *packet;
1245     struct eth_header *eth = ofpbuf_data(&payload);
1246
1247     /* Sink packets on ports that have no RSTP. */
1248     if (!xport->rstp_port) {
1249         return;
1250     }
1251
1252     /* Trim off padding on payload. */
1253     if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1254         ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1255     }
1256
1257     if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1258         rstp_port_received_bpdu(xport->rstp_port, ofpbuf_data(&payload),
1259                                 ofpbuf_size(&payload));
1260     }
1261 }
1262
1263 static struct xport *
1264 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1265 {
1266     struct xport *xport;
1267
1268     HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1269                              &xbridge->xports) {
1270         if (xport->ofp_port == ofp_port) {
1271             return xport;
1272         }
1273     }
1274     return NULL;
1275 }
1276
1277 static odp_port_t
1278 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1279 {
1280     const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1281     return xport ? xport->odp_port : ODPP_NONE;
1282 }
1283
1284 static bool
1285 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1286 {
1287     struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1288     return xport && xport->may_enable;
1289 }
1290
1291 static struct ofputil_bucket *
1292 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1293                         int depth);
1294
1295 static bool
1296 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1297 {
1298     struct group_dpif *group;
1299
1300     if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1301         struct ofputil_bucket *bucket;
1302
1303         bucket = group_first_live_bucket(ctx, group, depth);
1304         group_dpif_unref(group);
1305         return bucket == NULL;
1306     }
1307
1308     return false;
1309 }
1310
1311 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1312
1313 static bool
1314 bucket_is_alive(const struct xlate_ctx *ctx,
1315                 struct ofputil_bucket *bucket, int depth)
1316 {
1317     if (depth >= MAX_LIVENESS_RECURSION) {
1318         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1319
1320         VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1321                      MAX_LIVENESS_RECURSION);
1322         return false;
1323     }
1324
1325     return (!ofputil_bucket_has_liveness(bucket)
1326             || (bucket->watch_port != OFPP_ANY
1327                && odp_port_is_alive(ctx, bucket->watch_port))
1328             || (bucket->watch_group != OFPG_ANY
1329                && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1330 }
1331
1332 static struct ofputil_bucket *
1333 group_first_live_bucket(const struct xlate_ctx *ctx,
1334                         const struct group_dpif *group, int depth)
1335 {
1336     struct ofputil_bucket *bucket;
1337     const struct ovs_list *buckets;
1338
1339     group_dpif_get_buckets(group, &buckets);
1340     LIST_FOR_EACH (bucket, list_node, buckets) {
1341         if (bucket_is_alive(ctx, bucket, depth)) {
1342             return bucket;
1343         }
1344     }
1345
1346     return NULL;
1347 }
1348
1349 static struct ofputil_bucket *
1350 group_best_live_bucket(const struct xlate_ctx *ctx,
1351                        const struct group_dpif *group,
1352                        uint32_t basis)
1353 {
1354     struct ofputil_bucket *best_bucket = NULL;
1355     uint32_t best_score = 0;
1356     int i = 0;
1357
1358     struct ofputil_bucket *bucket;
1359     const struct ovs_list *buckets;
1360
1361     group_dpif_get_buckets(group, &buckets);
1362     LIST_FOR_EACH (bucket, list_node, buckets) {
1363         if (bucket_is_alive(ctx, bucket, 0)) {
1364             uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1365             if (score >= best_score) {
1366                 best_bucket = bucket;
1367                 best_score = score;
1368             }
1369         }
1370         i++;
1371     }
1372
1373     return best_bucket;
1374 }
1375
1376 static bool
1377 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1378 {
1379     return (bundle->vlan_mode != PORT_VLAN_ACCESS
1380             && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1381 }
1382
1383 static bool
1384 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1385 {
1386     return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1387 }
1388
1389 static mirror_mask_t
1390 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1391 {
1392     return xbundle != &ofpp_none_bundle
1393         ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1394         : 0;
1395 }
1396
1397 static mirror_mask_t
1398 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1399 {
1400     return xbundle != &ofpp_none_bundle
1401         ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1402         : 0;
1403 }
1404
1405 static mirror_mask_t
1406 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1407 {
1408     return xbundle != &ofpp_none_bundle
1409         ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1410         : 0;
1411 }
1412
1413 static struct xbundle *
1414 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1415                     bool warn, struct xport **in_xportp)
1416 {
1417     struct xport *xport;
1418
1419     /* Find the port and bundle for the received packet. */
1420     xport = get_ofp_port(xbridge, in_port);
1421     if (in_xportp) {
1422         *in_xportp = xport;
1423     }
1424     if (xport && xport->xbundle) {
1425         return xport->xbundle;
1426     }
1427
1428     /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1429      * which a controller may use as the ingress port for traffic that
1430      * it is sourcing. */
1431     if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1432         return &ofpp_none_bundle;
1433     }
1434
1435     /* Odd.  A few possible reasons here:
1436      *
1437      * - We deleted a port but there are still a few packets queued up
1438      *   from it.
1439      *
1440      * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1441      *   we don't know about.
1442      *
1443      * - The ofproto client didn't configure the port as part of a bundle.
1444      *   This is particularly likely to happen if a packet was received on the
1445      *   port after it was created, but before the client had a chance to
1446      *   configure its bundle.
1447      */
1448     if (warn) {
1449         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1450
1451         VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1452                      "port %"PRIu16, xbridge->name, in_port);
1453     }
1454     return NULL;
1455 }
1456
1457 static void
1458 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1459 {
1460     const struct xbridge *xbridge = ctx->xbridge;
1461     mirror_mask_t mirrors;
1462     struct xbundle *in_xbundle;
1463     uint16_t vlan;
1464     uint16_t vid;
1465
1466     mirrors = ctx->xout->mirrors;
1467     ctx->xout->mirrors = 0;
1468
1469     in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1470                                      ctx->xin->packet != NULL, NULL);
1471     if (!in_xbundle) {
1472         return;
1473     }
1474     mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1475
1476     /* Drop frames on bundles reserved for mirroring. */
1477     if (xbundle_mirror_out(xbridge, in_xbundle)) {
1478         if (ctx->xin->packet != NULL) {
1479             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1480             VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1481                          "%s, which is reserved exclusively for mirroring",
1482                          ctx->xbridge->name, in_xbundle->name);
1483         }
1484         ofpbuf_clear(ctx->xout->odp_actions);
1485         return;
1486     }
1487
1488     /* Check VLAN. */
1489     vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1490     if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1491         return;
1492     }
1493     vlan = input_vid_to_vlan(in_xbundle, vid);
1494
1495     if (!mirrors) {
1496         return;
1497     }
1498
1499     /* Restore the original packet before adding the mirror actions. */
1500     ctx->xin->flow = *orig_flow;
1501
1502     while (mirrors) {
1503         mirror_mask_t dup_mirrors;
1504         struct ofbundle *out;
1505         unsigned long *vlans;
1506         bool vlan_mirrored;
1507         bool has_mirror;
1508         int out_vlan;
1509
1510         has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1511                                 &vlans, &dup_mirrors, &out, &out_vlan);
1512         ovs_assert(has_mirror);
1513
1514         if (vlans) {
1515             ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1516         }
1517         vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1518         free(vlans);
1519
1520         if (!vlan_mirrored) {
1521             mirrors = zero_rightmost_1bit(mirrors);
1522             continue;
1523         }
1524
1525         mirrors &= ~dup_mirrors;
1526         ctx->xout->mirrors |= dup_mirrors;
1527         if (out) {
1528             struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1529             struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1530             if (out_xbundle) {
1531                 output_normal(ctx, out_xbundle, vlan);
1532             }
1533         } else if (vlan != out_vlan
1534                    && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1535             struct xbundle *xbundle;
1536
1537             LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1538                 if (xbundle_includes_vlan(xbundle, out_vlan)
1539                     && !xbundle_mirror_out(xbridge, xbundle)) {
1540                     output_normal(ctx, xbundle, out_vlan);
1541                 }
1542             }
1543         }
1544     }
1545 }
1546
1547 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1548  * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1549  * the bundle on which the packet was received, returns the VLAN to which the
1550  * packet belongs.
1551  *
1552  * Both 'vid' and the return value are in the range 0...4095. */
1553 static uint16_t
1554 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1555 {
1556     switch (in_xbundle->vlan_mode) {
1557     case PORT_VLAN_ACCESS:
1558         return in_xbundle->vlan;
1559         break;
1560
1561     case PORT_VLAN_TRUNK:
1562         return vid;
1563
1564     case PORT_VLAN_NATIVE_UNTAGGED:
1565     case PORT_VLAN_NATIVE_TAGGED:
1566         return vid ? vid : in_xbundle->vlan;
1567
1568     default:
1569         OVS_NOT_REACHED();
1570     }
1571 }
1572
1573 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1574  * If so, returns true.  Otherwise, returns false and, if 'warn' is true, logs
1575  * a warning.
1576  *
1577  * 'vid' should be the VID obtained from the 802.1Q header that was received as
1578  * part of a packet (specify 0 if there was no 802.1Q header), in the range
1579  * 0...4095. */
1580 static bool
1581 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1582 {
1583     /* Allow any VID on the OFPP_NONE port. */
1584     if (in_xbundle == &ofpp_none_bundle) {
1585         return true;
1586     }
1587
1588     switch (in_xbundle->vlan_mode) {
1589     case PORT_VLAN_ACCESS:
1590         if (vid) {
1591             if (warn) {
1592                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1593                 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1594                              "packet received on port %s configured as VLAN "
1595                              "%"PRIu16" access port", vid, in_xbundle->name,
1596                              in_xbundle->vlan);
1597             }
1598             return false;
1599         }
1600         return true;
1601
1602     case PORT_VLAN_NATIVE_UNTAGGED:
1603     case PORT_VLAN_NATIVE_TAGGED:
1604         if (!vid) {
1605             /* Port must always carry its native VLAN. */
1606             return true;
1607         }
1608         /* Fall through. */
1609     case PORT_VLAN_TRUNK:
1610         if (!xbundle_includes_vlan(in_xbundle, vid)) {
1611             if (warn) {
1612                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1613                 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1614                              "received on port %s not configured for trunking "
1615                              "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1616             }
1617             return false;
1618         }
1619         return true;
1620
1621     default:
1622         OVS_NOT_REACHED();
1623     }
1624
1625 }
1626
1627 /* Given 'vlan', the VLAN that a packet belongs to, and
1628  * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1629  * that should be included in the 802.1Q header.  (If the return value is 0,
1630  * then the 802.1Q header should only be included in the packet if there is a
1631  * nonzero PCP.)
1632  *
1633  * Both 'vlan' and the return value are in the range 0...4095. */
1634 static uint16_t
1635 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1636 {
1637     switch (out_xbundle->vlan_mode) {
1638     case PORT_VLAN_ACCESS:
1639         return 0;
1640
1641     case PORT_VLAN_TRUNK:
1642     case PORT_VLAN_NATIVE_TAGGED:
1643         return vlan;
1644
1645     case PORT_VLAN_NATIVE_UNTAGGED:
1646         return vlan == out_xbundle->vlan ? 0 : vlan;
1647
1648     default:
1649         OVS_NOT_REACHED();
1650     }
1651 }
1652
1653 static void
1654 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1655               uint16_t vlan)
1656 {
1657     ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1658     uint16_t vid;
1659     ovs_be16 tci, old_tci;
1660     struct xport *xport;
1661
1662     vid = output_vlan_to_vid(out_xbundle, vlan);
1663     if (list_is_empty(&out_xbundle->xports)) {
1664         /* Partially configured bundle with no slaves.  Drop the packet. */
1665         return;
1666     } else if (!out_xbundle->bond) {
1667         ctx->use_recirc = false;
1668         xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1669                              bundle_node);
1670     } else {
1671         struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1672         struct flow_wildcards *wc = &ctx->xout->wc;
1673         struct xlate_recirc *xr = &ctx->recirc;
1674         struct ofport_dpif *ofport;
1675
1676         if (ctx->xbridge->enable_recirc) {
1677             ctx->use_recirc = bond_may_recirc(
1678                 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
1679
1680             if (ctx->use_recirc) {
1681                 /* Only TCP mode uses recirculation. */
1682                 xr->hash_alg = OVS_HASH_ALG_L4;
1683                 bond_update_post_recirc_rules(out_xbundle->bond, false);
1684
1685                 /* Recirculation does not require unmasking hash fields. */
1686                 wc = NULL;
1687             }
1688         }
1689
1690         ofport = bond_choose_output_slave(out_xbundle->bond,
1691                                           &ctx->xin->flow, wc, vid);
1692         xport = xport_lookup(xcfg, ofport);
1693
1694         if (!xport) {
1695             /* No slaves enabled, so drop packet. */
1696             return;
1697         }
1698
1699         /* If ctx->xout->use_recirc is set, the main thread will handle stats
1700          * accounting for this bond. */
1701         if (!ctx->use_recirc) {
1702             if (ctx->xin->resubmit_stats) {
1703                 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1704                              ctx->xin->resubmit_stats->n_bytes);
1705             }
1706             if (ctx->xin->xcache) {
1707                 struct xc_entry *entry;
1708                 struct flow *flow;
1709
1710                 flow = &ctx->xin->flow;
1711                 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1712                 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1713                 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1714                 entry->u.bond.vid = vid;
1715             }
1716         }
1717     }
1718
1719     old_tci = *flow_tci;
1720     tci = htons(vid);
1721     if (tci || out_xbundle->use_priority_tags) {
1722         tci |= *flow_tci & htons(VLAN_PCP_MASK);
1723         if (tci) {
1724             tci |= htons(VLAN_CFI);
1725         }
1726     }
1727     *flow_tci = tci;
1728
1729     compose_output_action(ctx, xport->ofp_port);
1730     *flow_tci = old_tci;
1731 }
1732
1733 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1734  * migration.  Older Citrix-patched Linux DomU used gratuitous ARP replies to
1735  * indicate this; newer upstream kernels use gratuitous ARP requests. */
1736 static bool
1737 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1738 {
1739     if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1740         return false;
1741     }
1742
1743     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1744     if (!eth_addr_is_broadcast(flow->dl_dst)) {
1745         return false;
1746     }
1747
1748     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1749     if (flow->nw_proto == ARP_OP_REPLY) {
1750         return true;
1751     } else if (flow->nw_proto == ARP_OP_REQUEST) {
1752         memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1753         memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1754
1755         return flow->nw_src == flow->nw_dst;
1756     } else {
1757         return false;
1758     }
1759 }
1760
1761 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1762  * dropped.  Returns true if they may be forwarded, false if they should be
1763  * dropped.
1764  *
1765  * 'in_port' must be the xport that corresponds to flow->in_port.
1766  * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1767  *
1768  * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1769  * returned by input_vid_to_vlan().  It must be a valid VLAN for 'in_port', as
1770  * checked by input_vid_is_valid().
1771  *
1772  * May also add tags to '*tags', although the current implementation only does
1773  * so in one special case.
1774  */
1775 static bool
1776 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1777               uint16_t vlan)
1778 {
1779     struct xbundle *in_xbundle = in_port->xbundle;
1780     const struct xbridge *xbridge = ctx->xbridge;
1781     struct flow *flow = &ctx->xin->flow;
1782
1783     /* Drop frames for reserved multicast addresses
1784      * only if forward_bpdu option is absent. */
1785     if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1786         xlate_report(ctx, "packet has reserved destination MAC, dropping");
1787         return false;
1788     }
1789
1790     if (in_xbundle->bond) {
1791         struct mac_entry *mac;
1792
1793         switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1794                                          flow->dl_dst)) {
1795         case BV_ACCEPT:
1796             break;
1797
1798         case BV_DROP:
1799             xlate_report(ctx, "bonding refused admissibility, dropping");
1800             return false;
1801
1802         case BV_DROP_IF_MOVED:
1803             ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1804             mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1805             if (mac
1806                 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1807                 && (!is_gratuitous_arp(flow, &ctx->xout->wc)
1808                     || mac_entry_is_grat_arp_locked(mac))) {
1809                 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1810                 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1811                              "dropping");
1812                 return false;
1813             }
1814             ovs_rwlock_unlock(&xbridge->ml->rwlock);
1815             break;
1816         }
1817     }
1818
1819     return true;
1820 }
1821
1822 /* Checks whether a MAC learning update is necessary for MAC learning table
1823  * 'ml' given that a packet matching 'flow' was received  on 'in_xbundle' in
1824  * 'vlan'.
1825  *
1826  * Most packets processed through the MAC learning table do not actually
1827  * change it in any way.  This function requires only a read lock on the MAC
1828  * learning table, so it is much cheaper in this common case.
1829  *
1830  * Keep the code here synchronized with that in update_learning_table__()
1831  * below. */
1832 static bool
1833 is_mac_learning_update_needed(const struct mac_learning *ml,
1834                               const struct flow *flow,
1835                               struct flow_wildcards *wc,
1836                               int vlan, struct xbundle *in_xbundle)
1837 OVS_REQ_RDLOCK(ml->rwlock)
1838 {
1839     struct mac_entry *mac;
1840
1841     if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1842         return false;
1843     }
1844
1845     mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1846     if (!mac || mac_entry_age(ml, mac)) {
1847         return true;
1848     }
1849
1850     if (is_gratuitous_arp(flow, wc)) {
1851         /* We don't want to learn from gratuitous ARP packets that are
1852          * reflected back over bond slaves so we lock the learning table. */
1853         if (!in_xbundle->bond) {
1854             return true;
1855         } else if (mac_entry_is_grat_arp_locked(mac)) {
1856             return false;
1857         }
1858     }
1859
1860     return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
1861 }
1862
1863
1864 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1865  * received on 'in_xbundle' in 'vlan'.
1866  *
1867  * This code repeats all the checks in is_mac_learning_update_needed() because
1868  * the lock was released between there and here and thus the MAC learning state
1869  * could have changed.
1870  *
1871  * Keep the code here synchronized with that in is_mac_learning_update_needed()
1872  * above. */
1873 static void
1874 update_learning_table__(const struct xbridge *xbridge,
1875                         const struct flow *flow, struct flow_wildcards *wc,
1876                         int vlan, struct xbundle *in_xbundle)
1877 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1878 {
1879     struct mac_entry *mac;
1880
1881     if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1882         return;
1883     }
1884
1885     mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1886     if (is_gratuitous_arp(flow, wc)) {
1887         /* We don't want to learn from gratuitous ARP packets that are
1888          * reflected back over bond slaves so we lock the learning table. */
1889         if (!in_xbundle->bond) {
1890             mac_entry_set_grat_arp_lock(mac);
1891         } else if (mac_entry_is_grat_arp_locked(mac)) {
1892             return;
1893         }
1894     }
1895
1896     if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
1897         /* The log messages here could actually be useful in debugging,
1898          * so keep the rate limit relatively high. */
1899         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1900
1901         VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1902                     "on port %s in VLAN %d",
1903                     xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1904                     in_xbundle->name, vlan);
1905
1906         mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
1907     }
1908 }
1909
1910 static void
1911 update_learning_table(const struct xbridge *xbridge,
1912                       const struct flow *flow, struct flow_wildcards *wc,
1913                       int vlan, struct xbundle *in_xbundle)
1914 {
1915     bool need_update;
1916
1917     /* Don't learn the OFPP_NONE port. */
1918     if (in_xbundle == &ofpp_none_bundle) {
1919         return;
1920     }
1921
1922     /* First try the common case: no change to MAC learning table. */
1923     ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1924     need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1925                                                 in_xbundle);
1926     ovs_rwlock_unlock(&xbridge->ml->rwlock);
1927
1928     if (need_update) {
1929         /* Slow path: MAC learning table might need an update. */
1930         ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1931         update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1932         ovs_rwlock_unlock(&xbridge->ml->rwlock);
1933     }
1934 }
1935
1936 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1937  * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
1938 static void
1939 update_mcast_snooping_table__(const struct xbridge *xbridge,
1940                               const struct flow *flow,
1941                               struct mcast_snooping *ms,
1942                               ovs_be32 ip4, int vlan,
1943                               struct xbundle *in_xbundle)
1944     OVS_REQ_WRLOCK(ms->rwlock)
1945 {
1946     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
1947
1948     switch (ntohs(flow->tp_src)) {
1949     case IGMP_HOST_MEMBERSHIP_REPORT:
1950     case IGMPV2_HOST_MEMBERSHIP_REPORT:
1951         if (mcast_snooping_add_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1952             VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
1953                         IP_FMT" is on port %s in VLAN %d",
1954                         xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1955         }
1956         break;
1957     case IGMP_HOST_LEAVE_MESSAGE:
1958         if (mcast_snooping_leave_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1959             VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
1960                         IP_FMT" is on port %s in VLAN %d",
1961                         xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1962         }
1963         break;
1964     case IGMP_HOST_MEMBERSHIP_QUERY:
1965         if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
1966             in_xbundle->ofbundle)) {
1967             VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
1968                         IP_FMT" is on port %s in VLAN %d",
1969                         xbridge->name, IP_ARGS(flow->nw_src),
1970                         in_xbundle->name, vlan);
1971         }
1972         break;
1973     }
1974 }
1975
1976 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1977  * was received on 'in_xbundle' in 'vlan'. */
1978 static void
1979 update_mcast_snooping_table(const struct xbridge *xbridge,
1980                             const struct flow *flow, int vlan,
1981                             struct xbundle *in_xbundle)
1982 {
1983     struct mcast_snooping *ms = xbridge->ms;
1984     struct xlate_cfg *xcfg;
1985     struct xbundle *mcast_xbundle;
1986     struct mcast_port_bundle *fport;
1987
1988     /* Don't learn the OFPP_NONE port. */
1989     if (in_xbundle == &ofpp_none_bundle) {
1990         return;
1991     }
1992
1993     /* Don't learn from flood ports */
1994     mcast_xbundle = NULL;
1995     ovs_rwlock_wrlock(&ms->rwlock);
1996     xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1997     LIST_FOR_EACH(fport, node, &ms->fport_list) {
1998         mcast_xbundle = xbundle_lookup(xcfg, fport->port);
1999         if (mcast_xbundle == in_xbundle) {
2000             break;
2001         }
2002     }
2003
2004     if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2005         update_mcast_snooping_table__(xbridge, flow, ms, flow->igmp_group_ip4,
2006                                       vlan, in_xbundle);
2007     }
2008     ovs_rwlock_unlock(&ms->rwlock);
2009 }
2010
2011 /* send the packet to ports having the multicast group learned */
2012 static void
2013 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2014                               struct mcast_snooping *ms OVS_UNUSED,
2015                               struct mcast_group *grp,
2016                               struct xbundle *in_xbundle, uint16_t vlan)
2017     OVS_REQ_RDLOCK(ms->rwlock)
2018 {
2019     struct xlate_cfg *xcfg;
2020     struct mcast_group_bundle *b;
2021     struct xbundle *mcast_xbundle;
2022
2023     xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2024     LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2025         mcast_xbundle = xbundle_lookup(xcfg, b->port);
2026         if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2027             xlate_report(ctx, "forwarding to mcast group port");
2028             output_normal(ctx, mcast_xbundle, vlan);
2029         } else if (!mcast_xbundle) {
2030             xlate_report(ctx, "mcast group port is unknown, dropping");
2031         } else {
2032             xlate_report(ctx, "mcast group port is input port, dropping");
2033         }
2034     }
2035 }
2036
2037 /* send the packet to ports connected to multicast routers */
2038 static void
2039 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2040                                  struct mcast_snooping *ms,
2041                                  struct xbundle *in_xbundle, uint16_t vlan)
2042     OVS_REQ_RDLOCK(ms->rwlock)
2043 {
2044     struct xlate_cfg *xcfg;
2045     struct mcast_mrouter_bundle *mrouter;
2046     struct xbundle *mcast_xbundle;
2047
2048     xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2049     LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2050         mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2051         if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2052             xlate_report(ctx, "forwarding to mcast router port");
2053             output_normal(ctx, mcast_xbundle, vlan);
2054         } else if (!mcast_xbundle) {
2055             xlate_report(ctx, "mcast router port is unknown, dropping");
2056         } else {
2057             xlate_report(ctx, "mcast router port is input port, dropping");
2058         }
2059     }
2060 }
2061
2062 /* send the packet to ports flagged to be flooded */
2063 static void
2064 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2065                                struct mcast_snooping *ms,
2066                                struct xbundle *in_xbundle, uint16_t vlan)
2067     OVS_REQ_RDLOCK(ms->rwlock)
2068 {
2069     struct xlate_cfg *xcfg;
2070     struct mcast_port_bundle *fport;
2071     struct xbundle *mcast_xbundle;
2072
2073     xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2074     LIST_FOR_EACH(fport, node, &ms->fport_list) {
2075         mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2076         if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2077             xlate_report(ctx, "forwarding to mcast flood port");
2078             output_normal(ctx, mcast_xbundle, vlan);
2079         } else if (!mcast_xbundle) {
2080             xlate_report(ctx, "mcast flood port is unknown, dropping");
2081         } else {
2082             xlate_report(ctx, "mcast flood port is input port, dropping");
2083         }
2084     }
2085 }
2086
2087 /* forward the Reports to configured ports */
2088 static void
2089 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2090                                struct mcast_snooping *ms,
2091                                struct xbundle *in_xbundle, uint16_t vlan)
2092     OVS_REQ_RDLOCK(ms->rwlock)
2093 {
2094     struct xlate_cfg *xcfg;
2095     struct mcast_port_bundle *rport;
2096     struct xbundle *mcast_xbundle;
2097
2098     xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2099     LIST_FOR_EACH(rport, node, &ms->rport_list) {
2100         mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2101         if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2102             xlate_report(ctx, "forwarding Report to mcast flagged port");
2103             output_normal(ctx, mcast_xbundle, vlan);
2104         } else if (!mcast_xbundle) {
2105             xlate_report(ctx, "mcast port is unknown, dropping the Report");
2106         } else {
2107             xlate_report(ctx, "mcast port is input port, dropping the Report");
2108         }
2109     }
2110 }
2111
2112 static void
2113 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2114                    uint16_t vlan)
2115 {
2116     struct xbundle *xbundle;
2117
2118     LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2119         if (xbundle != in_xbundle
2120             && xbundle_includes_vlan(xbundle, vlan)
2121             && xbundle->floodable
2122             && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2123             output_normal(ctx, xbundle, vlan);
2124         }
2125     }
2126     ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2127 }
2128
2129 static void
2130 xlate_normal(struct xlate_ctx *ctx)
2131 {
2132     struct flow_wildcards *wc = &ctx->xout->wc;
2133     struct flow *flow = &ctx->xin->flow;
2134     struct xbundle *in_xbundle;
2135     struct xport *in_port;
2136     struct mac_entry *mac;
2137     void *mac_port;
2138     uint16_t vlan;
2139     uint16_t vid;
2140
2141     ctx->xout->has_normal = true;
2142
2143     memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2144     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2145     wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2146
2147     in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2148                                      ctx->xin->packet != NULL, &in_port);
2149     if (!in_xbundle) {
2150         xlate_report(ctx, "no input bundle, dropping");
2151         return;
2152     }
2153
2154     /* Drop malformed frames. */
2155     if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2156         !(flow->vlan_tci & htons(VLAN_CFI))) {
2157         if (ctx->xin->packet != NULL) {
2158             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2159             VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2160                          "VLAN tag received on port %s",
2161                          ctx->xbridge->name, in_xbundle->name);
2162         }
2163         xlate_report(ctx, "partial VLAN tag, dropping");
2164         return;
2165     }
2166
2167     /* Drop frames on bundles reserved for mirroring. */
2168     if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2169         if (ctx->xin->packet != NULL) {
2170             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2171             VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2172                          "%s, which is reserved exclusively for mirroring",
2173                          ctx->xbridge->name, in_xbundle->name);
2174         }
2175         xlate_report(ctx, "input port is mirror output port, dropping");
2176         return;
2177     }
2178
2179     /* Check VLAN. */
2180     vid = vlan_tci_to_vid(flow->vlan_tci);
2181     if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2182         xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2183         return;
2184     }
2185     vlan = input_vid_to_vlan(in_xbundle, vid);
2186
2187     /* Check other admissibility requirements. */
2188     if (in_port && !is_admissible(ctx, in_port, vlan)) {
2189         return;
2190     }
2191
2192     /* Learn source MAC. */
2193     if (ctx->xin->may_learn) {
2194         update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2195     }
2196     if (ctx->xin->xcache) {
2197         struct xc_entry *entry;
2198
2199         /* Save enough info to update mac learning table later. */
2200         entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2201         entry->u.normal.ofproto = ctx->xbridge->ofproto;
2202         entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2203         entry->u.normal.vlan = vlan;
2204     }
2205
2206     /* Determine output bundle. */
2207     if (mcast_snooping_enabled(ctx->xbridge->ms)
2208         && !eth_addr_is_broadcast(flow->dl_dst)
2209         && eth_addr_is_multicast(flow->dl_dst)
2210         && flow->dl_type == htons(ETH_TYPE_IP)) {
2211         struct mcast_snooping *ms = ctx->xbridge->ms;
2212         struct mcast_group *grp;
2213
2214         if (flow->nw_proto == IPPROTO_IGMP) {
2215             if (ctx->xin->may_learn) {
2216                 if (mcast_snooping_is_membership(flow->tp_src) ||
2217                     mcast_snooping_is_query(flow->tp_src)) {
2218                     update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2219                                                 in_xbundle);
2220                     }
2221             }
2222
2223             if (mcast_snooping_is_membership(flow->tp_src)) {
2224                 ovs_rwlock_rdlock(&ms->rwlock);
2225                 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2226                 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2227                  * forward IGMP Membership Reports only to those ports where
2228                  * multicast routers are attached.  Alternatively stated: a
2229                  * snooping switch should not forward IGMP Membership Reports
2230                  * to ports on which only hosts are attached.
2231                  * An administrative control may be provided to override this
2232                  * restriction, allowing the report messages to be flooded to
2233                  * other ports. */
2234                 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2235                 ovs_rwlock_unlock(&ms->rwlock);
2236             } else {
2237                 xlate_report(ctx, "multicast traffic, flooding");
2238                 xlate_normal_flood(ctx, in_xbundle, vlan);
2239             }
2240             return;
2241         } else {
2242             if (ip_is_local_multicast(flow->nw_dst)) {
2243                 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2244                  * address in the 224.0.0.x range which are not IGMP must
2245                  * be forwarded on all ports */
2246                 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2247                 xlate_normal_flood(ctx, in_xbundle, vlan);
2248                 return;
2249             }
2250         }
2251
2252         /* forwarding to group base ports */
2253         ovs_rwlock_rdlock(&ms->rwlock);
2254         grp = mcast_snooping_lookup(ms, flow->nw_dst, vlan);
2255         if (grp) {
2256             xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2257             xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2258             xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2259         } else {
2260             if (mcast_snooping_flood_unreg(ms)) {
2261                 xlate_report(ctx, "unregistered multicast, flooding");
2262                 xlate_normal_flood(ctx, in_xbundle, vlan);
2263             } else {
2264                 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2265                 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2266             }
2267         }
2268         ovs_rwlock_unlock(&ms->rwlock);
2269     } else {
2270         ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2271         mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2272         mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2273         ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2274
2275         if (mac_port) {
2276             struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2277             struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2278             if (mac_xbundle && mac_xbundle != in_xbundle) {
2279                 xlate_report(ctx, "forwarding to learned port");
2280                 output_normal(ctx, mac_xbundle, vlan);
2281             } else if (!mac_xbundle) {
2282                 xlate_report(ctx, "learned port is unknown, dropping");
2283             } else {
2284                 xlate_report(ctx, "learned port is input port, dropping");
2285             }
2286         } else {
2287             xlate_report(ctx, "no learned MAC for destination, flooding");
2288             xlate_normal_flood(ctx, in_xbundle, vlan);
2289         }
2290     }
2291 }
2292
2293 /* Compose SAMPLE action for sFlow or IPFIX.  The given probability is
2294  * the number of packets out of UINT32_MAX to sample.  The given
2295  * cookie is passed back in the callback for each sampled packet.
2296  */
2297 static size_t
2298 compose_sample_action(const struct xbridge *xbridge,
2299                       struct ofpbuf *odp_actions,
2300                       const struct flow *flow,
2301                       const uint32_t probability,
2302                       const union user_action_cookie *cookie,
2303                       const size_t cookie_size,
2304                       const odp_port_t tunnel_out_port)
2305 {
2306     size_t sample_offset, actions_offset;
2307     odp_port_t odp_port;
2308     int cookie_offset;
2309     uint32_t pid;
2310
2311     sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2312
2313     nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2314
2315     actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
2316
2317     odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
2318     pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2319                             flow_hash_5tuple(flow, 0));
2320     cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2321                                              tunnel_out_port, odp_actions);
2322
2323     nl_msg_end_nested(odp_actions, actions_offset);
2324     nl_msg_end_nested(odp_actions, sample_offset);
2325     return cookie_offset;
2326 }
2327
2328 static void
2329 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2330                      odp_port_t odp_port, unsigned int n_outputs,
2331                      union user_action_cookie *cookie)
2332 {
2333     int ifindex;
2334
2335     cookie->type = USER_ACTION_COOKIE_SFLOW;
2336     cookie->sflow.vlan_tci = vlan_tci;
2337
2338     /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2339      * port information") for the interpretation of cookie->output. */
2340     switch (n_outputs) {
2341     case 0:
2342         /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2343         cookie->sflow.output = 0x40000000 | 256;
2344         break;
2345
2346     case 1:
2347         ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
2348         if (ifindex) {
2349             cookie->sflow.output = ifindex;
2350             break;
2351         }
2352         /* Fall through. */
2353     default:
2354         /* 0x80000000 means "multiple output ports. */
2355         cookie->sflow.output = 0x80000000 | n_outputs;
2356         break;
2357     }
2358 }
2359
2360 /* Compose SAMPLE action for sFlow bridge sampling. */
2361 static size_t
2362 compose_sflow_action(const struct xbridge *xbridge,
2363                      struct ofpbuf *odp_actions,
2364                      const struct flow *flow,
2365                      odp_port_t odp_port)
2366 {
2367     uint32_t probability;
2368     union user_action_cookie cookie;
2369
2370     if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
2371         return 0;
2372     }
2373
2374     probability = dpif_sflow_get_probability(xbridge->sflow);
2375     compose_sflow_cookie(xbridge, htons(0), odp_port,
2376                          odp_port == ODPP_NONE ? 0 : 1, &cookie);
2377
2378     return compose_sample_action(xbridge, odp_actions, flow,  probability,
2379                                  &cookie, sizeof cookie.sflow, ODPP_NONE);
2380 }
2381
2382 static void
2383 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2384                            uint32_t obs_domain_id, uint32_t obs_point_id,
2385                            union user_action_cookie *cookie)
2386 {
2387     cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2388     cookie->flow_sample.probability = probability;
2389     cookie->flow_sample.collector_set_id = collector_set_id;
2390     cookie->flow_sample.obs_domain_id = obs_domain_id;
2391     cookie->flow_sample.obs_point_id = obs_point_id;
2392 }
2393
2394 static void
2395 compose_ipfix_cookie(union user_action_cookie *cookie,
2396                      odp_port_t output_odp_port)
2397 {
2398     cookie->type = USER_ACTION_COOKIE_IPFIX;
2399     cookie->ipfix.output_odp_port = output_odp_port;
2400 }
2401
2402 /* Compose SAMPLE action for IPFIX bridge sampling. */
2403 static void
2404 compose_ipfix_action(const struct xbridge *xbridge,
2405                      struct ofpbuf *odp_actions,
2406                      const struct flow *flow,
2407                      odp_port_t output_odp_port)
2408 {
2409     uint32_t probability;
2410     union user_action_cookie cookie;
2411     odp_port_t tunnel_out_port = ODPP_NONE;
2412
2413     if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2414         return;
2415     }
2416
2417     /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2418      * number. */
2419     if (output_odp_port == ODPP_NONE &&
2420         !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2421         return;
2422     }
2423
2424     /* For output case, output_odp_port is valid*/
2425     if (output_odp_port != ODPP_NONE) {
2426         if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2427             return;
2428         }
2429         /* If tunnel sampling is enabled, put an additional option attribute:
2430          * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2431          */
2432         if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2433             dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2434            tunnel_out_port = output_odp_port;
2435         }
2436     }
2437
2438     probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2439     compose_ipfix_cookie(&cookie, output_odp_port);
2440
2441     compose_sample_action(xbridge, odp_actions, flow,  probability,
2442                           &cookie, sizeof cookie.ipfix, tunnel_out_port);
2443 }
2444
2445 /* SAMPLE action for sFlow must be first action in any given list of
2446  * actions.  At this point we do not have all information required to
2447  * build it. So try to build sample action as complete as possible. */
2448 static void
2449 add_sflow_action(struct xlate_ctx *ctx)
2450 {
2451     ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2452                                                    ctx->xout->odp_actions,
2453                                                    &ctx->xin->flow, ODPP_NONE);
2454     ctx->sflow_odp_port = 0;
2455     ctx->sflow_n_outputs = 0;
2456 }
2457
2458 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2459  * of actions, eventually after the SAMPLE action for sFlow. */
2460 static void
2461 add_ipfix_action(struct xlate_ctx *ctx)
2462 {
2463     compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2464                          &ctx->xin->flow, ODPP_NONE);
2465 }
2466
2467 static void
2468 add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2469 {
2470     compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2471                          &ctx->xin->flow, port);
2472 }
2473
2474 /* Fix SAMPLE action according to data collected while composing ODP actions.
2475  * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2476  * USERSPACE action's user-cookie which is required for sflow. */
2477 static void
2478 fix_sflow_action(struct xlate_ctx *ctx)
2479 {
2480     const struct flow *base = &ctx->base_flow;
2481     union user_action_cookie *cookie;
2482
2483     if (!ctx->user_cookie_offset) {
2484         return;
2485     }
2486
2487     cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
2488                        sizeof cookie->sflow);
2489     ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2490
2491     compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2492                          ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2493 }
2494
2495 static enum slow_path_reason
2496 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2497                 const struct xport *xport, const struct ofpbuf *packet)
2498 {
2499     struct flow_wildcards *wc = &ctx->xout->wc;
2500     const struct xbridge *xbridge = ctx->xbridge;
2501
2502     if (!xport) {
2503         return 0;
2504     } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2505         if (packet) {
2506             cfm_process_heartbeat(xport->cfm, packet);
2507         }
2508         return SLOW_CFM;
2509     } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2510         if (packet) {
2511             bfd_process_packet(xport->bfd, flow, packet);
2512             /* If POLL received, immediately sends FINAL back. */
2513             if (bfd_should_send_packet(xport->bfd)) {
2514                 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2515             }
2516         }
2517         return SLOW_BFD;
2518     } else if (xport->xbundle && xport->xbundle->lacp
2519                && flow->dl_type == htons(ETH_TYPE_LACP)) {
2520         if (packet) {
2521             lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2522         }
2523         return SLOW_LACP;
2524     } else if ((xbridge->stp || xbridge->rstp) &&
2525                stp_should_process_flow(flow, wc)) {
2526         if (packet) {
2527             xbridge->stp
2528                 ? stp_process_packet(xport, packet)
2529                 : rstp_process_packet(xport, packet);
2530         }
2531         return SLOW_STP;
2532     } else {
2533         return 0;
2534     }
2535 }
2536
2537 static int
2538 tnl_route_lookup_flow(const struct flow *oflow,
2539                       ovs_be32 *ip, struct xport **out_port)
2540 {
2541     char out_dev[IFNAMSIZ];
2542     struct xbridge *xbridge;
2543     struct xlate_cfg *xcfg;
2544     ovs_be32 gw;
2545
2546     if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2547         return -ENOENT;
2548     }
2549
2550     if (gw) {
2551         *ip = gw;
2552     } else {
2553         *ip = oflow->tunnel.ip_dst;
2554     }
2555
2556     xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2557     ovs_assert(xcfg);
2558
2559     HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2560         if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2561             struct xport *port;
2562
2563             HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2564                 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2565                     *out_port = port;
2566                     return 0;
2567                 }
2568             }
2569         }
2570     }
2571     return -ENOENT;
2572 }
2573
2574 static int
2575 xlate_flood_packet(struct xbridge *xbridge, struct ofpbuf *packet)
2576 {
2577     struct ofpact_output output;
2578     struct flow flow;
2579
2580     ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2581     /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2582     flow_extract(packet, NULL, &flow);
2583     flow.in_port.ofp_port = OFPP_NONE;
2584     output.port = OFPP_FLOOD;
2585     output.max_len = 0;
2586
2587     return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
2588                                         &output.ofpact, sizeof output,
2589                                         packet);
2590 }
2591
2592 static void
2593 tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
2594                      ovs_be32 ip_src, ovs_be32 ip_dst)
2595 {
2596     struct xbridge *xbridge = out_dev->xbridge;
2597     struct ofpbuf packet;
2598
2599     ofpbuf_init(&packet, 0);
2600     compose_arp(&packet, eth_src, ip_src, ip_dst);
2601
2602     xlate_flood_packet(xbridge, &packet);
2603     ofpbuf_uninit(&packet);
2604 }
2605
2606 static int
2607 build_tunnel_send(const struct xlate_ctx *ctx, const struct xport *xport,
2608                   const struct flow *flow, odp_port_t tunnel_odp_port)
2609 {
2610     struct ovs_action_push_tnl tnl_push_data;
2611     struct xport *out_dev = NULL;
2612     ovs_be32 s_ip, d_ip = 0;
2613     uint8_t smac[ETH_ADDR_LEN];
2614     uint8_t dmac[ETH_ADDR_LEN];
2615     int err;
2616
2617     err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2618     if (err) {
2619         return err;
2620     }
2621
2622     /* Use mac addr of bridge port of the peer. */
2623     err = netdev_get_etheraddr(out_dev->netdev, smac);
2624     if (err) {
2625         return err;
2626     }
2627
2628     err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2629     if (err) {
2630         return err;
2631     }
2632
2633     err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
2634     if (err) {
2635         tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
2636         return err;
2637     }
2638     if (ctx->xin->xcache) {
2639         struct xc_entry *entry;
2640
2641         entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
2642         strncpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name, IFNAMSIZ);
2643         entry->u.tnl_arp_cache.d_ip = d_ip;
2644     }
2645     err = tnl_port_build_header(xport->ofport, flow,
2646                                 dmac, smac, s_ip, &tnl_push_data);
2647     if (err) {
2648         return err;
2649     }
2650     tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2651     tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2652     odp_put_tnl_push_action(ctx->xout->odp_actions, &tnl_push_data);
2653     return 0;
2654 }
2655
2656 static void
2657 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2658                         bool check_stp)
2659 {
2660     const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2661     struct flow_wildcards *wc = &ctx->xout->wc;
2662     struct flow *flow = &ctx->xin->flow;
2663     struct flow_tnl flow_tnl;
2664     ovs_be16 flow_vlan_tci;
2665     uint32_t flow_pkt_mark;
2666     uint8_t flow_nw_tos;
2667     odp_port_t out_port, odp_port;
2668     bool tnl_push_pop_send = false;
2669     uint8_t dscp;
2670
2671     /* If 'struct flow' gets additional metadata, we'll need to zero it out
2672      * before traversing a patch port. */
2673     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 30);
2674     memset(&flow_tnl, 0, sizeof flow_tnl);
2675
2676     if (!xport) {
2677         xlate_report(ctx, "Nonexistent output port");
2678         return;
2679     } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2680         xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2681         return;
2682     } else if (check_stp) {
2683         if (is_stp(&ctx->base_flow)) {
2684             if (!xport_stp_should_forward_bpdu(xport) &&
2685                 !xport_rstp_should_manage_bpdu(xport)) {
2686                 if (ctx->xbridge->stp != NULL) {
2687                     xlate_report(ctx, "STP not in listening state, "
2688                             "skipping bpdu output");
2689                 } else if (ctx->xbridge->rstp != NULL) {
2690                     xlate_report(ctx, "RSTP not managing BPDU in this state, "
2691                             "skipping bpdu output");
2692                 }
2693                 return;
2694             }
2695         } else if (!xport_stp_forward_state(xport) ||
2696                    !xport_rstp_forward_state(xport)) {
2697             if (ctx->xbridge->stp != NULL) {
2698                 xlate_report(ctx, "STP not in forwarding state, "
2699                         "skipping output");
2700             } else if (ctx->xbridge->rstp != NULL) {
2701                 xlate_report(ctx, "RSTP not in forwarding state, "
2702                         "skipping output");
2703             }
2704             return;
2705         }
2706     }
2707
2708     if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2709         ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2710                                                  xport->xbundle);
2711     }
2712
2713     if (xport->peer) {
2714         const struct xport *peer = xport->peer;
2715         struct flow old_flow = ctx->xin->flow;
2716         enum slow_path_reason special;
2717         uint8_t table_id = rule_dpif_lookup_get_init_table_id(&ctx->xin->flow);
2718
2719         ctx->xbridge = peer->xbridge;
2720         flow->in_port.ofp_port = peer->ofp_port;
2721         flow->metadata = htonll(0);
2722         memset(&flow->tunnel, 0, sizeof flow->tunnel);
2723         memset(flow->regs, 0, sizeof flow->regs);
2724         flow->actset_output = OFPP_UNSET;
2725
2726         special = process_special(ctx, &ctx->xin->flow, peer,
2727                                   ctx->xin->packet);
2728         if (special) {
2729             ctx->xout->slow |= special;
2730         } else if (may_receive(peer, ctx)) {
2731             if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2732                 xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
2733                                    true, true);
2734             } else {
2735                 /* Forwarding is disabled by STP and RSTP.  Let OFPP_NORMAL and
2736                  * the learning action look at the packet, then drop it. */
2737                 struct flow old_base_flow = ctx->base_flow;
2738                 size_t old_size = ofpbuf_size(ctx->xout->odp_actions);
2739                 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2740                 xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
2741                                    true, true);
2742                 ctx->xout->mirrors = old_mirrors;
2743                 ctx->base_flow = old_base_flow;
2744                 ofpbuf_set_size(ctx->xout->odp_actions, old_size);
2745             }
2746         }
2747
2748         ctx->xin->flow = old_flow;
2749         ctx->xbridge = xport->xbridge;
2750
2751         if (ctx->xin->resubmit_stats) {
2752             netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2753             netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2754             if (peer->bfd) {
2755                 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2756             }
2757         }
2758         if (ctx->xin->xcache) {
2759             struct xc_entry *entry;
2760
2761             entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2762             entry->u.dev.tx = netdev_ref(xport->netdev);
2763             entry->u.dev.rx = netdev_ref(peer->netdev);
2764             entry->u.dev.bfd = bfd_ref(peer->bfd);
2765         }
2766         return;
2767     }
2768
2769     flow_vlan_tci = flow->vlan_tci;
2770     flow_pkt_mark = flow->pkt_mark;
2771     flow_nw_tos = flow->nw_tos;
2772
2773     if (count_skb_priorities(xport)) {
2774         memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2775         if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2776             wc->masks.nw_tos |= IP_DSCP_MASK;
2777             flow->nw_tos &= ~IP_DSCP_MASK;
2778             flow->nw_tos |= dscp;
2779         }
2780     }
2781
2782     if (xport->is_tunnel) {
2783          /* Save tunnel metadata so that changes made due to
2784           * the Logical (tunnel) Port are not visible for any further
2785           * matches, while explicit set actions on tunnel metadata are.
2786           */
2787         flow_tnl = flow->tunnel;
2788         odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
2789         if (odp_port == ODPP_NONE) {
2790             xlate_report(ctx, "Tunneling decided against output");
2791             goto out; /* restore flow_nw_tos */
2792         }
2793         if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
2794             xlate_report(ctx, "Not tunneling to our own address");
2795             goto out; /* restore flow_nw_tos */
2796         }
2797         if (ctx->xin->resubmit_stats) {
2798             netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2799         }
2800         if (ctx->xin->xcache) {
2801             struct xc_entry *entry;
2802
2803             entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2804             entry->u.dev.tx = netdev_ref(xport->netdev);
2805         }
2806         out_port = odp_port;
2807         if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2808             tnl_push_pop_send = true;
2809         } else {
2810             commit_odp_tunnel_action(flow, &ctx->base_flow,
2811                                      ctx->xout->odp_actions);
2812             flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2813         }
2814     } else {
2815         odp_port = xport->odp_port;
2816         out_port = odp_port;
2817         if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
2818             ofp_port_t vlandev_port;
2819
2820             wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2821             vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2822                                                   ofp_port, flow->vlan_tci);
2823             if (vlandev_port != ofp_port) {
2824                 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2825                 flow->vlan_tci = htons(0);
2826             }
2827         }
2828     }
2829
2830     if (out_port != ODPP_NONE) {
2831         ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2832                                               ctx->xout->odp_actions,
2833                                               wc,
2834                                               ctx->xbridge->masked_set_action);
2835
2836         if (ctx->use_recirc) {
2837             struct ovs_action_hash *act_hash;
2838             struct xlate_recirc *xr = &ctx->recirc;
2839
2840             /* Hash action. */
2841             act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
2842                                                 OVS_ACTION_ATTR_HASH,
2843                                                 sizeof *act_hash);
2844             act_hash->hash_alg = xr->hash_alg;
2845             act_hash->hash_basis = xr->hash_basis;
2846
2847             /* Recirc action. */
2848             nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
2849                            xr->recirc_id);
2850         } else {
2851
2852             if (tnl_push_pop_send) {
2853                 build_tunnel_send(ctx, xport, flow, odp_port);
2854                 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2855             } else {
2856                 odp_port_t odp_tnl_port = ODPP_NONE;
2857
2858                 /* XXX: Write better Filter for tunnel port. We can use inport
2859                 * int tunnel-port flow to avoid these checks completely. */
2860                 if (ofp_port == OFPP_LOCAL &&
2861                     ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2862
2863                     odp_tnl_port = tnl_port_map_lookup(flow, wc);
2864                 }
2865
2866                 if (odp_tnl_port != ODPP_NONE) {
2867                     nl_msg_put_odp_port(ctx->xout->odp_actions,
2868                                         OVS_ACTION_ATTR_TUNNEL_POP,
2869                                         odp_tnl_port);
2870                 } else {
2871                     /* Tunnel push-pop action is not compatible with
2872                      * IPFIX action. */
2873                     add_ipfix_output_action(ctx, out_port);
2874                     nl_msg_put_odp_port(ctx->xout->odp_actions,
2875                                         OVS_ACTION_ATTR_OUTPUT,
2876                                         out_port);
2877                }
2878            }
2879         }
2880
2881         ctx->sflow_odp_port = odp_port;
2882         ctx->sflow_n_outputs++;
2883         ctx->xout->nf_output_iface = ofp_port;
2884     }
2885
2886  out:
2887     /* Restore flow */
2888     flow->vlan_tci = flow_vlan_tci;
2889     flow->pkt_mark = flow_pkt_mark;
2890     flow->nw_tos = flow_nw_tos;
2891 }
2892
2893 static void
2894 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
2895 {
2896     compose_output_action__(ctx, ofp_port, true);
2897 }
2898
2899 static void
2900 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
2901 {
2902     struct rule_dpif *old_rule = ctx->rule;
2903     const struct rule_actions *actions;
2904
2905     if (ctx->xin->resubmit_stats) {
2906         rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
2907     }
2908
2909     ctx->resubmits++;
2910     ctx->recurse++;
2911     ctx->rule = rule;
2912     actions = rule_dpif_get_actions(rule);
2913     do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
2914     ctx->rule = old_rule;
2915     ctx->recurse--;
2916 }
2917
2918 static bool
2919 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
2920 {
2921     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2922
2923     if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
2924         VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2925                     MAX_RESUBMIT_RECURSION);
2926     } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
2927         VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
2928     } else if (ofpbuf_size(ctx->xout->odp_actions) > UINT16_MAX) {
2929         VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
2930     } else if (ofpbuf_size(&ctx->stack) >= 65536) {
2931         VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2932     } else {
2933         return true;
2934     }
2935
2936     return false;
2937 }
2938
2939 static void
2940 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2941                    bool may_packet_in, bool honor_table_miss)
2942 {
2943     if (xlate_resubmit_resource_check(ctx)) {
2944         struct flow_wildcards *wc;
2945         uint8_t old_table_id = ctx->table_id;
2946         struct rule_dpif *rule;
2947
2948         ctx->table_id = table_id;
2949         wc = (ctx->xin->skip_wildcards) ? NULL : &ctx->xout->wc;
2950
2951         rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2952                                            &ctx->xin->flow, wc,
2953                                            ctx->xin->xcache != NULL,
2954                                            ctx->xin->resubmit_stats,
2955                                            &ctx->table_id, in_port,
2956                                            may_packet_in, honor_table_miss);
2957
2958         if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
2959             ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
2960         }
2961
2962         if (rule) {
2963             /* Fill in the cache entry here instead of xlate_recursively
2964              * to make the reference counting more explicit.  We take a
2965              * reference in the lookups above if we are going to cache the
2966              * rule. */
2967             if (ctx->xin->xcache) {
2968                 struct xc_entry *entry;
2969
2970                 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
2971                 entry->u.rule = rule;
2972             }
2973             xlate_recursively(ctx, rule);
2974         }
2975
2976         ctx->table_id = old_table_id;
2977         return;
2978     }
2979
2980     ctx->exit = true;
2981 }
2982
2983 static void
2984 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
2985                   struct ofputil_bucket *bucket)
2986 {
2987     if (ctx->xin->resubmit_stats) {
2988         group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
2989     }
2990     if (ctx->xin->xcache) {
2991         struct xc_entry *entry;
2992
2993         entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
2994         entry->u.group.group = group_dpif_ref(group);
2995         entry->u.group.bucket = bucket;
2996     }
2997 }
2998
2999 static void
3000 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3001 {
3002     uint64_t action_list_stub[1024 / 8];
3003     struct ofpbuf action_list, action_set;
3004
3005     ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3006     ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3007
3008     ofpacts_execute_action_set(&action_list, &action_set);
3009     ctx->recurse++;
3010     do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
3011     ctx->recurse--;
3012
3013     ofpbuf_uninit(&action_set);
3014     ofpbuf_uninit(&action_list);
3015 }
3016
3017 static void
3018 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3019 {
3020     struct ofputil_bucket *bucket;
3021     const struct ovs_list *buckets;
3022     struct flow old_flow = ctx->xin->flow;
3023
3024     group_dpif_get_buckets(group, &buckets);
3025
3026     LIST_FOR_EACH (bucket, list_node, buckets) {
3027         xlate_group_bucket(ctx, bucket);
3028         /* Roll back flow to previous state.
3029          * This is equivalent to cloning the packet for each bucket.
3030          *
3031          * As a side effect any subsequently applied actions will
3032          * also effectively be applied to a clone of the packet taken
3033          * just before applying the all or indirect group. */
3034         ctx->xin->flow = old_flow;
3035     }
3036     xlate_group_stats(ctx, group, NULL);
3037 }
3038
3039 static void
3040 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3041 {
3042     struct ofputil_bucket *bucket;
3043
3044     bucket = group_first_live_bucket(ctx, group, 0);
3045     if (bucket) {
3046         xlate_group_bucket(ctx, bucket);
3047         xlate_group_stats(ctx, group, bucket);
3048     }
3049 }
3050
3051 static void
3052 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3053 {
3054     struct flow_wildcards *wc = &ctx->xout->wc;
3055     struct ofputil_bucket *bucket;
3056     uint32_t basis;
3057
3058     basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3059     bucket = group_best_live_bucket(ctx, group, basis);
3060     if (bucket) {
3061         memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3062         memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3063         memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3064         memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3065         memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3066         memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3067         memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3068         memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3069         memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3070
3071         xlate_group_bucket(ctx, bucket);
3072         xlate_group_stats(ctx, group, bucket);
3073     }
3074 }
3075
3076 static void
3077 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3078 {
3079     ctx->in_group = true;
3080
3081     switch (group_dpif_get_type(group)) {
3082     case OFPGT11_ALL:
3083     case OFPGT11_INDIRECT:
3084         xlate_all_group(ctx, group);
3085         break;
3086     case OFPGT11_SELECT:
3087         xlate_select_group(ctx, group);
3088         break;
3089     case OFPGT11_FF:
3090         xlate_ff_group(ctx, group);
3091         break;
3092     default:
3093         OVS_NOT_REACHED();
3094     }
3095     group_dpif_unref(group);
3096
3097     ctx->in_group = false;
3098 }
3099
3100 static bool
3101 xlate_group_resource_check(struct xlate_ctx *ctx)
3102 {
3103     if (!xlate_resubmit_resource_check(ctx)) {
3104         return false;
3105     } else if (ctx->in_group) {
3106         /* Prevent nested translation of OpenFlow groups.
3107          *
3108          * OpenFlow allows this restriction.  We enforce this restriction only
3109          * because, with the current architecture, we would otherwise have to
3110          * take a possibly recursive read lock on the ofgroup rwlock, which is
3111          * unsafe given that POSIX allows taking a read lock to block if there
3112          * is a thread blocked on taking the write lock.  Other solutions
3113          * without this restriction are also possible, but seem unwarranted
3114          * given the current limited use of groups. */
3115         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3116
3117         VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
3118         return false;
3119     } else {
3120         return true;
3121     }
3122 }
3123
3124 static bool
3125 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3126 {
3127     if (xlate_group_resource_check(ctx)) {
3128         struct group_dpif *group;
3129         bool got_group;
3130
3131         got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3132         if (got_group) {
3133             xlate_group_action__(ctx, group);
3134         } else {
3135             return true;
3136         }
3137     }
3138
3139     return false;
3140 }
3141
3142 static void
3143 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3144                       const struct ofpact_resubmit *resubmit)
3145 {
3146     ofp_port_t in_port;
3147     uint8_t table_id;
3148     bool may_packet_in = false;
3149     bool honor_table_miss = false;
3150
3151     if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3152         /* Still allow missed packets to be sent to the controller
3153          * if resubmitting from an internal table. */
3154         may_packet_in = true;
3155         honor_table_miss = true;
3156     }
3157
3158     in_port = resubmit->in_port;
3159     if (in_port == OFPP_IN_PORT) {
3160         in_port = ctx->xin->flow.in_port.ofp_port;
3161     }
3162
3163     table_id = resubmit->table_id;
3164     if (table_id == 255) {
3165         table_id = ctx->table_id;
3166     }
3167
3168     xlate_table_action(ctx, in_port, table_id, may_packet_in,
3169                        honor_table_miss);
3170 }
3171
3172 static void
3173 flood_packets(struct xlate_ctx *ctx, bool all)
3174 {
3175     const struct xport *xport;
3176
3177     HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3178         if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3179             continue;
3180         }
3181
3182         if (all) {
3183             compose_output_action__(ctx, xport->ofp_port, false);
3184         } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3185             compose_output_action(ctx, xport->ofp_port);
3186         }
3187     }
3188
3189     ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3190 }
3191
3192 static void
3193 execute_controller_action(struct xlate_ctx *ctx, int len,
3194                           enum ofp_packet_in_reason reason,
3195                           uint16_t controller_id)
3196 {
3197     struct ofproto_packet_in *pin;
3198     struct dpif_packet *packet;
3199
3200     ctx->xout->slow |= SLOW_CONTROLLER;
3201     if (!ctx->xin->packet) {
3202         return;
3203     }
3204
3205     packet = dpif_packet_clone_from_ofpbuf(ctx->xin->packet);
3206
3207     ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3208                                           ctx->xout->odp_actions,
3209                                           &ctx->xout->wc,
3210                                           ctx->xbridge->masked_set_action);
3211
3212     odp_execute_actions(NULL, &packet, 1, false,
3213                         ofpbuf_data(ctx->xout->odp_actions),
3214                         ofpbuf_size(ctx->xout->odp_actions), NULL);
3215
3216     pin = xmalloc(sizeof *pin);
3217     pin->up.packet_len = ofpbuf_size(&packet->ofpbuf);
3218     pin->up.packet = ofpbuf_steal_data(&packet->ofpbuf);
3219     pin->up.reason = reason;
3220     pin->up.table_id = ctx->table_id;
3221     pin->up.cookie = (ctx->rule
3222                       ? rule_dpif_get_flow_cookie(ctx->rule)
3223                       : OVS_BE64_MAX);
3224
3225     flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
3226
3227     pin->controller_id = controller_id;
3228     pin->send_len = len;
3229     /* If a rule is a table-miss rule then this is
3230      * a table-miss handled by a table-miss rule.
3231      *
3232      * Else, if rule is internal and has a controller action,
3233      * the later being implied by the rule being processed here,
3234      * then this is a table-miss handled without a table-miss rule.
3235      *
3236      * Otherwise this is not a table-miss. */
3237     pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3238     if (ctx->rule) {
3239         if (rule_dpif_is_table_miss(ctx->rule)) {
3240             pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3241         } else if (rule_dpif_is_internal(ctx->rule)) {
3242             pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3243         }
3244     }
3245     ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
3246     dpif_packet_delete(packet);
3247 }
3248
3249 static void
3250 compose_recirculate_action(struct xlate_ctx *ctx,
3251                            const struct ofpact *ofpacts_base,
3252                            const struct ofpact *ofpact_current,
3253                            size_t ofpacts_base_len)
3254 {
3255     uint32_t id;
3256     int error;
3257     unsigned ofpacts_len;
3258     struct match match;
3259     struct rule *rule;
3260     struct ofpbuf ofpacts;
3261
3262     ctx->exit = true;
3263
3264     ofpacts_len = ofpacts_base_len -
3265         ((uint8_t *)ofpact_current - (uint8_t *)ofpacts_base);
3266
3267     if (ctx->rule) {
3268         id = rule_dpif_get_recirc_id(ctx->rule);
3269     } else {
3270         /* In the case where ctx has no rule then allocate a recirc id.
3271          * The life-cycle of this recirc id is managed by associating it
3272          * with the internal rule that is created to to handle
3273          * recirculation below.
3274          *
3275          * The known use-case of this is packet_out which
3276          * translates actions without a rule */
3277         id = ofproto_dpif_alloc_recirc_id(ctx->xbridge->ofproto);
3278     }
3279     if (!id) {
3280         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3281         VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3282         return;
3283     }
3284
3285     match_init_catchall(&match);
3286     match_set_recirc_id(&match, id);
3287     ofpbuf_use_const(&ofpacts, ofpact_current, ofpacts_len);
3288     error = ofproto_dpif_add_internal_flow(ctx->xbridge->ofproto, &match,
3289                                            RECIRC_RULE_PRIORITY,
3290                                            RECIRC_TIMEOUT, &ofpacts, &rule);
3291     if (error) {
3292         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3293         VLOG_ERR_RL(&rl, "Failed to add post recirculation flow %s",
3294                     match_to_string(&match, 0));
3295         return;
3296     }
3297     /* If ctx has no rule then associate the recirc id, which
3298      * was allocated above, with the internal rule. This allows
3299      * the recirc id to be released when the internal rule times out. */
3300     if (!ctx->rule) {
3301         rule_set_recirc_id(rule, id);
3302     }
3303
3304     ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3305                                           ctx->xout->odp_actions,
3306                                           &ctx->xout->wc,
3307                                           ctx->xbridge->masked_set_action);
3308     nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3309 }
3310
3311 static void
3312 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3313 {
3314     struct flow_wildcards *wc = &ctx->xout->wc;
3315     struct flow *flow = &ctx->xin->flow;
3316     int n;
3317
3318     ovs_assert(eth_type_mpls(mpls->ethertype));
3319
3320     n = flow_count_mpls_labels(flow, wc);
3321     if (!n) {
3322         ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3323                                               ctx->xout->odp_actions,
3324                                               &ctx->xout->wc,
3325                                               ctx->xbridge->masked_set_action);
3326     } else if (n >= FLOW_MAX_MPLS_LABELS) {
3327         if (ctx->xin->packet != NULL) {
3328             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3329             VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3330                          "MPLS push action can't be performed as it would "
3331                          "have more MPLS LSEs than the %d supported.",
3332                          ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3333         }
3334         ctx->exit = true;
3335         return;
3336     }
3337
3338     flow_push_mpls(flow, n, mpls->ethertype, wc);
3339 }
3340
3341 static void
3342 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3343 {
3344     struct flow_wildcards *wc = &ctx->xout->wc;
3345     struct flow *flow = &ctx->xin->flow;
3346     int n = flow_count_mpls_labels(flow, wc);
3347
3348     if (flow_pop_mpls(flow, n, eth_type, wc)) {
3349         if (ctx->xbridge->enable_recirc) {
3350             ctx->was_mpls = true;
3351         }
3352     } else if (n >= FLOW_MAX_MPLS_LABELS) {
3353         if (ctx->xin->packet != NULL) {
3354             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3355             VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3356                          "MPLS pop action can't be performed as it has "
3357                          "more MPLS LSEs than the %d supported.",
3358                          ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3359         }
3360         ctx->exit = true;
3361         ofpbuf_clear(ctx->xout->odp_actions);
3362     }
3363 }
3364
3365 static bool
3366 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3367 {
3368     struct flow *flow = &ctx->xin->flow;
3369
3370     if (!is_ip_any(flow)) {
3371         return false;
3372     }
3373
3374     ctx->xout->wc.masks.nw_ttl = 0xff;
3375     if (flow->nw_ttl > 1) {
3376         flow->nw_ttl--;
3377         return false;
3378     } else {
3379         size_t i;
3380
3381         for (i = 0; i < ids->n_controllers; i++) {
3382             execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3383                                       ids->cnt_ids[i]);
3384         }
3385
3386         /* Stop processing for current table. */
3387         return true;
3388     }
3389 }
3390
3391 static void
3392 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3393 {
3394     if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3395         ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3396         set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3397     }
3398 }
3399
3400 static void
3401 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3402 {
3403     if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3404         ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3405         set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3406     }
3407 }
3408
3409 static void
3410 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3411 {
3412     if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3413         ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3414         set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3415     }
3416 }
3417
3418 static bool
3419 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3420 {
3421     struct flow *flow = &ctx->xin->flow;
3422     struct flow_wildcards *wc = &ctx->xout->wc;
3423
3424     if (eth_type_mpls(flow->dl_type)) {
3425         uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3426
3427         wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3428         if (ttl > 1) {
3429             ttl--;
3430             set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3431             return false;
3432         } else {
3433             execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3434         }
3435     }
3436
3437     /* Stop processing for current table. */
3438     return true;
3439 }
3440
3441 static void
3442 xlate_output_action(struct xlate_ctx *ctx,
3443                     ofp_port_t port, uint16_t max_len, bool may_packet_in)
3444 {
3445     ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
3446
3447     ctx->xout->nf_output_iface = NF_OUT_DROP;
3448
3449     switch (port) {
3450     case OFPP_IN_PORT:
3451         compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
3452         break;
3453     case OFPP_TABLE:
3454         xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3455                            0, may_packet_in, true);
3456         break;
3457     case OFPP_NORMAL:
3458         xlate_normal(ctx);
3459         break;
3460     case OFPP_FLOOD:
3461         flood_packets(ctx,  false);
3462         break;
3463     case OFPP_ALL:
3464         flood_packets(ctx, true);
3465         break;
3466     case OFPP_CONTROLLER:
3467         execute_controller_action(ctx, max_len,
3468                                   (ctx->in_group ? OFPR_GROUP
3469                                    : ctx->in_action_set ? OFPR_ACTION_SET
3470                                    : OFPR_ACTION),
3471                                   0);
3472         break;
3473     case OFPP_NONE:
3474         break;
3475     case OFPP_LOCAL:
3476     default:
3477         if (port != ctx->xin->flow.in_port.ofp_port) {
3478             compose_output_action(ctx, port);
3479         } else {
3480             xlate_report(ctx, "skipping output to input port");
3481         }
3482         break;
3483     }
3484
3485     if (prev_nf_output_iface == NF_OUT_FLOOD) {
3486         ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3487     } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3488         ctx->xout->nf_output_iface = prev_nf_output_iface;
3489     } else if (prev_nf_output_iface != NF_OUT_DROP &&
3490                ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3491         ctx->xout->nf_output_iface = NF_OUT_MULTI;
3492     }
3493 }
3494
3495 static void
3496 xlate_output_reg_action(struct xlate_ctx *ctx,
3497                         const struct ofpact_output_reg *or)
3498 {
3499     uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3500     if (port <= UINT16_MAX) {
3501         union mf_subvalue value;
3502
3503         memset(&value, 0xff, sizeof value);
3504         mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
3505         xlate_output_action(ctx, u16_to_ofp(port),
3506                             or->max_len, false);
3507     }
3508 }
3509
3510 static void
3511 xlate_enqueue_action(struct xlate_ctx *ctx,
3512                      const struct ofpact_enqueue *enqueue)
3513 {
3514     ofp_port_t ofp_port = enqueue->port;
3515     uint32_t queue_id = enqueue->queue;
3516     uint32_t flow_priority, priority;
3517     int error;
3518
3519     /* Translate queue to priority. */
3520     error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3521     if (error) {
3522         /* Fall back to ordinary output action. */
3523         xlate_output_action(ctx, enqueue->port, 0, false);
3524         return;
3525     }
3526
3527     /* Check output port. */
3528     if (ofp_port == OFPP_IN_PORT) {
3529         ofp_port = ctx->xin->flow.in_port.ofp_port;
3530     } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3531         return;
3532     }
3533
3534     /* Add datapath actions. */
3535     flow_priority = ctx->xin->flow.skb_priority;
3536     ctx->xin->flow.skb_priority = priority;
3537     compose_output_action(ctx, ofp_port);
3538     ctx->xin->flow.skb_priority = flow_priority;
3539
3540     /* Update NetFlow output port. */
3541     if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3542         ctx->xout->nf_output_iface = ofp_port;
3543     } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3544         ctx->xout->nf_output_iface = NF_OUT_MULTI;
3545     }
3546 }
3547
3548 static void
3549 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3550 {
3551     uint32_t skb_priority;
3552
3553     if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3554         ctx->xin->flow.skb_priority = skb_priority;
3555     } else {
3556         /* Couldn't translate queue to a priority.  Nothing to do.  A warning
3557          * has already been logged. */
3558     }
3559 }
3560
3561 static bool
3562 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3563 {
3564     const struct xbridge *xbridge = xbridge_;
3565     struct xport *port;
3566
3567     switch (ofp_port) {
3568     case OFPP_IN_PORT:
3569     case OFPP_TABLE:
3570     case OFPP_NORMAL:
3571     case OFPP_FLOOD:
3572     case OFPP_ALL:
3573     case OFPP_NONE:
3574         return true;
3575     case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3576         return false;
3577     default:
3578         port = get_ofp_port(xbridge, ofp_port);
3579         return port ? port->may_enable : false;
3580     }
3581 }
3582
3583 static void
3584 xlate_bundle_action(struct xlate_ctx *ctx,
3585                     const struct ofpact_bundle *bundle)
3586 {
3587     ofp_port_t port;
3588
3589     port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
3590                           slave_enabled_cb,
3591                           CONST_CAST(struct xbridge *, ctx->xbridge));
3592     if (bundle->dst.field) {
3593         nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
3594                      &ctx->xout->wc);
3595     } else {
3596         xlate_output_action(ctx, port, 0, false);
3597     }
3598 }
3599
3600 static void
3601 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3602                      struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3603 {
3604     learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3605     if (ctx->xin->may_learn) {
3606         ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3607     }
3608 }
3609
3610 static void
3611 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3612 {
3613     ctx->xout->has_learn = true;
3614     learn_mask(learn, &ctx->xout->wc);
3615
3616     if (ctx->xin->xcache) {
3617         struct xc_entry *entry;
3618
3619         entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3620         entry->u.learn.ofproto = ctx->xbridge->ofproto;
3621         entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3622         entry->u.learn.ofpacts = ofpbuf_new(64);
3623         xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3624                              entry->u.learn.ofpacts);
3625     } else if (ctx->xin->may_learn) {
3626         uint64_t ofpacts_stub[1024 / 8];
3627         struct ofputil_flow_mod fm;
3628         struct ofpbuf ofpacts;
3629
3630         ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3631         xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3632         ofpbuf_uninit(&ofpacts);
3633     }
3634 }
3635
3636 static void
3637 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3638                     uint16_t idle_timeout, uint16_t hard_timeout)
3639 {
3640     if (tcp_flags & (TCP_FIN | TCP_RST)) {
3641         rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3642     }
3643 }
3644
3645 static void
3646 xlate_fin_timeout(struct xlate_ctx *ctx,
3647                   const struct ofpact_fin_timeout *oft)
3648 {
3649     if (ctx->rule) {
3650         xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3651                             oft->fin_idle_timeout, oft->fin_hard_timeout);
3652         if (ctx->xin->xcache) {
3653             struct xc_entry *entry;
3654
3655             entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3656             /* XC_RULE already holds a reference on the rule, none is taken
3657              * here. */
3658             entry->u.fin.rule = ctx->rule;
3659             entry->u.fin.idle = oft->fin_idle_timeout;
3660             entry->u.fin.hard = oft->fin_hard_timeout;
3661         }
3662     }
3663 }
3664
3665 static void
3666 xlate_sample_action(struct xlate_ctx *ctx,
3667                     const struct ofpact_sample *os)
3668 {
3669   union user_action_cookie cookie;
3670   /* Scale the probability from 16-bit to 32-bit while representing
3671    * the same percentage. */
3672   uint32_t probability = (os->probability << 16) | os->probability;
3673
3674   if (!ctx->xbridge->variable_length_userdata) {
3675       static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3676
3677       VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3678                   "lacks support (needs Linux 3.10+ or kernel module from "
3679                   "OVS 1.11+)");
3680       return;
3681   }
3682
3683   ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3684                                         ctx->xout->odp_actions,
3685                                         &ctx->xout->wc,
3686                                         ctx->xbridge->masked_set_action);
3687
3688   compose_flow_sample_cookie(os->probability, os->collector_set_id,
3689                              os->obs_domain_id, os->obs_point_id, &cookie);
3690   compose_sample_action(ctx->xbridge, ctx->xout->odp_actions, &ctx->xin->flow,
3691                         probability, &cookie, sizeof cookie.flow_sample,
3692                         ODPP_NONE);
3693 }
3694
3695 static bool
3696 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3697 {
3698     if (xport->config & (is_stp(&ctx->xin->flow)
3699                          ? OFPUTIL_PC_NO_RECV_STP
3700                          : OFPUTIL_PC_NO_RECV)) {
3701         return false;
3702     }
3703
3704     /* Only drop packets here if both forwarding and learning are
3705      * disabled.  If just learning is enabled, we need to have
3706      * OFPP_NORMAL and the learning action have a look at the packet
3707      * before we can drop it. */
3708     if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3709         (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
3710         return false;
3711     }
3712
3713     return true;
3714 }
3715
3716 static void
3717 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3718 {
3719     const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3720     size_t on_len = ofpact_nest_get_action_len(on);
3721     const struct ofpact *inner;
3722
3723     /* Maintain actset_output depending on the contents of the action set:
3724      *
3725      *   - OFPP_UNSET, if there is no "output" action.
3726      *
3727      *   - The output port, if there is an "output" action and no "group"
3728      *     action.
3729      *
3730      *   - OFPP_UNSET, if there is a "group" action.
3731      */
3732     if (!ctx->action_set_has_group) {
3733         OFPACT_FOR_EACH (inner, on->actions, on_len) {
3734             if (inner->type == OFPACT_OUTPUT) {
3735                 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
3736             } else if (inner->type == OFPACT_GROUP) {
3737                 ctx->xin->flow.actset_output = OFPP_UNSET;
3738                 ctx->action_set_has_group = true;
3739             }
3740         }
3741     }
3742
3743     ofpbuf_put(&ctx->action_set, on->actions, on_len);
3744     ofpact_pad(&ctx->action_set);
3745 }
3746
3747 static void
3748 xlate_action_set(struct xlate_ctx *ctx)
3749 {
3750     uint64_t action_list_stub[1024 / 64];
3751     struct ofpbuf action_list;
3752
3753     ctx->in_action_set = true;
3754     ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3755     ofpacts_execute_action_set(&action_list, &ctx->action_set);
3756     do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
3757     ctx->in_action_set = false;
3758     ofpbuf_uninit(&action_list);
3759 }
3760
3761 static bool
3762 ofpact_needs_recirculation_after_mpls(const struct ofpact *a, struct xlate_ctx *ctx)
3763 {
3764     struct flow_wildcards *wc = &ctx->xout->wc;
3765     struct flow *flow = &ctx->xin->flow;
3766
3767     if (!ctx->was_mpls) {
3768         return false;
3769     }
3770
3771     switch (a->type) {
3772     case OFPACT_OUTPUT:
3773     case OFPACT_GROUP:
3774     case OFPACT_CONTROLLER:
3775     case OFPACT_STRIP_VLAN:
3776     case OFPACT_SET_VLAN_PCP:
3777     case OFPACT_SET_VLAN_VID:
3778     case OFPACT_ENQUEUE:
3779     case OFPACT_PUSH_VLAN:
3780     case OFPACT_SET_ETH_SRC:
3781     case OFPACT_SET_ETH_DST:
3782     case OFPACT_SET_TUNNEL:
3783     case OFPACT_SET_QUEUE:
3784     case OFPACT_POP_QUEUE:
3785     case OFPACT_CONJUNCTION:
3786     case OFPACT_NOTE:
3787     case OFPACT_OUTPUT_REG:
3788     case OFPACT_EXIT:
3789     case OFPACT_METER:
3790     case OFPACT_WRITE_METADATA:
3791     case OFPACT_WRITE_ACTIONS:
3792     case OFPACT_CLEAR_ACTIONS:
3793     case OFPACT_SAMPLE:
3794         return false;
3795
3796     case OFPACT_POP_MPLS:
3797     case OFPACT_DEC_MPLS_TTL:
3798     case OFPACT_SET_MPLS_TTL:
3799     case OFPACT_SET_MPLS_TC:
3800     case OFPACT_SET_MPLS_LABEL:
3801     case OFPACT_SET_IPV4_SRC:
3802     case OFPACT_SET_IPV4_DST:
3803     case OFPACT_SET_IP_DSCP:
3804     case OFPACT_SET_IP_ECN:
3805     case OFPACT_SET_IP_TTL:
3806     case OFPACT_SET_L4_SRC_PORT:
3807     case OFPACT_SET_L4_DST_PORT:
3808     case OFPACT_RESUBMIT:
3809     case OFPACT_STACK_PUSH:
3810     case OFPACT_STACK_POP:
3811     case OFPACT_DEC_TTL:
3812     case OFPACT_MULTIPATH:
3813     case OFPACT_BUNDLE:
3814     case OFPACT_LEARN:
3815     case OFPACT_FIN_TIMEOUT:
3816     case OFPACT_GOTO_TABLE:
3817         return true;
3818
3819     case OFPACT_REG_MOVE:
3820         return (mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
3821                 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
3822
3823     case OFPACT_SET_FIELD:
3824         return mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field);
3825
3826     case OFPACT_PUSH_MPLS:
3827         /* Recirculate if it is an IP packet with a zero ttl.  This may
3828          * indicate that the packet was previously MPLS and an MPLS pop action
3829          * converted it to IP. In this case recirculating should reveal the IP
3830          * TTL which is used as the basis for a new MPLS LSE. */
3831         return (!flow_count_mpls_labels(flow, wc)
3832                 && flow->nw_ttl == 0
3833                 && is_ip_any(flow));
3834     }
3835
3836     OVS_NOT_REACHED();
3837 }
3838
3839 static void
3840 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
3841                  struct xlate_ctx *ctx)
3842 {
3843     struct flow_wildcards *wc = &ctx->xout->wc;
3844     struct flow *flow = &ctx->xin->flow;
3845     const struct ofpact *a;
3846
3847     if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3848         tnl_arp_snoop(flow, wc, ctx->xbridge->name);
3849     }
3850     /* dl_type already in the mask, not set below. */
3851
3852     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3853         struct ofpact_controller *controller;
3854         const struct ofpact_metadata *metadata;
3855         const struct ofpact_set_field *set_field;
3856         const struct mf_field *mf;
3857
3858         if (ctx->exit) {
3859             break;
3860         }
3861
3862         if (ofpact_needs_recirculation_after_mpls(a, ctx)) {
3863             compose_recirculate_action(ctx, ofpacts, a, ofpacts_len);
3864             return;
3865         }
3866
3867         switch (a->type) {
3868         case OFPACT_OUTPUT:
3869             xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
3870                                 ofpact_get_OUTPUT(a)->max_len, true);
3871             break;
3872
3873         case OFPACT_GROUP:
3874             if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
3875                 return;
3876             }
3877             break;
3878
3879         case OFPACT_CONTROLLER:
3880             controller = ofpact_get_CONTROLLER(a);
3881             execute_controller_action(ctx, controller->max_len,
3882                                       controller->reason,
3883                                       controller->controller_id);
3884             break;
3885
3886         case OFPACT_ENQUEUE:
3887             memset(&wc->masks.skb_priority, 0xff,
3888                    sizeof wc->masks.skb_priority);
3889             xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
3890             break;
3891
3892         case OFPACT_SET_VLAN_VID:
3893             wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3894             if (flow->vlan_tci & htons(VLAN_CFI) ||
3895                 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
3896                 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
3897                 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
3898                                    | htons(VLAN_CFI));
3899             }
3900             break;
3901
3902         case OFPACT_SET_VLAN_PCP:
3903             wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
3904             if (flow->vlan_tci & htons(VLAN_CFI) ||
3905                 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
3906                 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
3907                 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
3908                                          << VLAN_PCP_SHIFT) | VLAN_CFI);
3909             }
3910             break;
3911
3912         case OFPACT_STRIP_VLAN:
3913             memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3914             flow->vlan_tci = htons(0);
3915             break;
3916
3917         case OFPACT_PUSH_VLAN:
3918             /* XXX 802.1AD(QinQ) */
3919             memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3920             flow->vlan_tci = htons(VLAN_CFI);
3921             break;
3922
3923         case OFPACT_SET_ETH_SRC:
3924             memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3925             memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
3926             break;
3927
3928         case OFPACT_SET_ETH_DST:
3929             memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3930             memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
3931             break;
3932
3933         case OFPACT_SET_IPV4_SRC:
3934             if (flow->dl_type == htons(ETH_TYPE_IP)) {
3935                 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3936                 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
3937             }
3938             break;
3939
3940         case OFPACT_SET_IPV4_DST:
3941             if (flow->dl_type == htons(ETH_TYPE_IP)) {
3942                 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3943                 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
3944             }
3945             break;
3946
3947         case OFPACT_SET_IP_DSCP:
3948             if (is_ip_any(flow)) {
3949                 wc->masks.nw_tos |= IP_DSCP_MASK;
3950                 flow->nw_tos &= ~IP_DSCP_MASK;
3951                 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
3952             }
3953             break;
3954
3955         case OFPACT_SET_IP_ECN:
3956             if (is_ip_any(flow)) {
3957                 wc->masks.nw_tos |= IP_ECN_MASK;
3958                 flow->nw_tos &= ~IP_ECN_MASK;
3959                 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
3960             }
3961             break;
3962
3963         case OFPACT_SET_IP_TTL:
3964             if (is_ip_any(flow)) {
3965                 wc->masks.nw_ttl = 0xff;
3966                 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
3967             }
3968             break;
3969
3970         case OFPACT_SET_L4_SRC_PORT:
3971             if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3972                 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3973                 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3974                 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
3975             }
3976             break;
3977
3978         case OFPACT_SET_L4_DST_PORT:
3979             if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3980                 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3981                 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3982                 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
3983             }
3984             break;
3985
3986         case OFPACT_RESUBMIT:
3987             xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
3988             break;
3989
3990         case OFPACT_SET_TUNNEL:
3991             flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
3992             break;
3993
3994         case OFPACT_SET_QUEUE:
3995             memset(&wc->masks.skb_priority, 0xff,
3996                    sizeof wc->masks.skb_priority);
3997             xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
3998             break;
3999
4000         case OFPACT_POP_QUEUE:
4001             memset(&wc->masks.skb_priority, 0xff,
4002                    sizeof wc->masks.skb_priority);
4003             flow->skb_priority = ctx->orig_skb_priority;
4004             break;
4005
4006         case OFPACT_REG_MOVE:
4007             nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4008             break;
4009
4010         case OFPACT_SET_FIELD:
4011             set_field = ofpact_get_SET_FIELD(a);
4012             mf = set_field->field;
4013
4014             /* Set field action only ever overwrites packet's outermost
4015              * applicable header fields.  Do nothing if no header exists. */
4016             if (mf->id == MFF_VLAN_VID) {
4017                 wc->masks.vlan_tci |= htons(VLAN_CFI);
4018                 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4019                     break;
4020                 }
4021             } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4022                        /* 'dl_type' is already unwildcarded. */
4023                        && !eth_type_mpls(flow->dl_type)) {
4024                 break;
4025             }
4026             /* A flow may wildcard nw_frag.  Do nothing if setting a trasport
4027              * header field on a packet that does not have them. */
4028             mf_mask_field_and_prereqs(mf, &wc->masks);
4029             if (mf_are_prereqs_ok(mf, flow)) {
4030                 mf_set_flow_value_masked(mf, &set_field->value,
4031                                          &set_field->mask, flow);
4032             }
4033             break;
4034
4035         case OFPACT_STACK_PUSH:
4036             nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4037                                    &ctx->stack);
4038             break;
4039
4040         case OFPACT_STACK_POP:
4041             nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4042                                   &ctx->stack);
4043             break;
4044
4045         case OFPACT_PUSH_MPLS:
4046             compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4047             break;
4048
4049         case OFPACT_POP_MPLS:
4050             compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4051             break;
4052
4053         case OFPACT_SET_MPLS_LABEL:
4054             compose_set_mpls_label_action(
4055                 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4056         break;
4057
4058         case OFPACT_SET_MPLS_TC:
4059             compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4060             break;
4061
4062         case OFPACT_SET_MPLS_TTL:
4063             compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4064             break;
4065
4066         case OFPACT_DEC_MPLS_TTL:
4067             if (compose_dec_mpls_ttl_action(ctx)) {
4068                 return;
4069             }
4070             break;
4071
4072         case OFPACT_DEC_TTL:
4073             wc->masks.nw_ttl = 0xff;
4074             if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4075                 return;
4076             }
4077             break;
4078
4079         case OFPACT_NOTE:
4080             /* Nothing to do. */
4081             break;
4082
4083         case OFPACT_MULTIPATH:
4084             multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4085             break;
4086
4087         case OFPACT_BUNDLE:
4088             xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4089             break;
4090
4091         case OFPACT_OUTPUT_REG:
4092             xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4093             break;
4094
4095         case OFPACT_LEARN:
4096             xlate_learn_action(ctx, ofpact_get_LEARN(a));
4097             break;
4098
4099         case OFPACT_CONJUNCTION: {
4100             /* A flow with a "conjunction" action represents part of a special
4101              * kind of "set membership match".  Such a flow should not actually
4102              * get executed, but it could via, say, a "packet-out", even though
4103              * that wouldn't be useful.  Log it to help debugging. */
4104             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4105             VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4106             break;
4107         }
4108
4109         case OFPACT_EXIT:
4110             ctx->exit = true;
4111             break;
4112
4113         case OFPACT_FIN_TIMEOUT:
4114             memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4115             ctx->xout->has_fin_timeout = true;
4116             xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4117             break;
4118
4119         case OFPACT_CLEAR_ACTIONS:
4120             ofpbuf_clear(&ctx->action_set);
4121             ctx->xin->flow.actset_output = OFPP_UNSET;
4122             ctx->action_set_has_group = false;
4123             break;
4124
4125         case OFPACT_WRITE_ACTIONS:
4126             xlate_write_actions(ctx, a);
4127             break;
4128
4129         case OFPACT_WRITE_METADATA:
4130             metadata = ofpact_get_WRITE_METADATA(a);
4131             flow->metadata &= ~metadata->mask;
4132             flow->metadata |= metadata->metadata & metadata->mask;
4133             break;
4134
4135         case OFPACT_METER:
4136             /* Not implemented yet. */
4137             break;
4138
4139         case OFPACT_GOTO_TABLE: {
4140             struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4141
4142             /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4143              * than ogt->table_id. This is to allow goto_table actions that
4144              * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4145              * after recirculation. */
4146             ovs_assert(ctx->table_id == TBL_INTERNAL
4147                        || ctx->table_id < ogt->table_id);
4148             xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4149                                ogt->table_id, true, true);
4150             break;
4151         }
4152
4153         case OFPACT_SAMPLE:
4154             xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4155             break;
4156         }
4157     }
4158 }
4159
4160 void
4161 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4162               const struct flow *flow, ofp_port_t in_port,
4163               struct rule_dpif *rule, uint16_t tcp_flags,
4164               const struct ofpbuf *packet)
4165 {
4166     xin->ofproto = ofproto;
4167     xin->flow = *flow;
4168     xin->flow.in_port.ofp_port = in_port;
4169     xin->flow.actset_output = OFPP_UNSET;
4170     xin->packet = packet;
4171     xin->may_learn = packet != NULL;
4172     xin->rule = rule;
4173     xin->xcache = NULL;
4174     xin->ofpacts = NULL;
4175     xin->ofpacts_len = 0;
4176     xin->tcp_flags = tcp_flags;
4177     xin->resubmit_hook = NULL;
4178     xin->report_hook = NULL;
4179     xin->resubmit_stats = NULL;
4180     xin->skip_wildcards = false;
4181     xin->odp_actions = NULL;
4182 }
4183
4184 void
4185 xlate_out_uninit(struct xlate_out *xout)
4186 {
4187     if (xout && xout->odp_actions == &xout->odp_actions_buf) {
4188         ofpbuf_uninit(xout->odp_actions);
4189     }
4190 }
4191
4192 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4193  * into datapath actions, using 'ctx', and discards the datapath actions. */
4194 void
4195 xlate_actions_for_side_effects(struct xlate_in *xin)
4196 {
4197     struct xlate_out xout;
4198
4199     xlate_actions(xin, &xout);
4200     xlate_out_uninit(&xout);
4201 }
4202
4203 void
4204 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
4205 {
4206     dst->wc = src->wc;
4207     dst->slow = src->slow;
4208     dst->has_learn = src->has_learn;
4209     dst->has_normal = src->has_normal;
4210     dst->has_fin_timeout = src->has_fin_timeout;
4211     dst->nf_output_iface = src->nf_output_iface;
4212     dst->mirrors = src->mirrors;
4213
4214     dst->odp_actions = &dst->odp_actions_buf;
4215     ofpbuf_use_stub(dst->odp_actions, dst->odp_actions_stub,
4216                     sizeof dst->odp_actions_stub);
4217     ofpbuf_put(dst->odp_actions, ofpbuf_data(src->odp_actions),
4218                ofpbuf_size(src->odp_actions));
4219 }
4220 \f
4221 static struct skb_priority_to_dscp *
4222 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4223 {
4224     struct skb_priority_to_dscp *pdscp;
4225     uint32_t hash;
4226
4227     hash = hash_int(skb_priority, 0);
4228     HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4229         if (pdscp->skb_priority == skb_priority) {
4230             return pdscp;
4231         }
4232     }
4233     return NULL;
4234 }
4235
4236 static bool
4237 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4238                        uint8_t *dscp)
4239 {
4240     struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4241     *dscp = pdscp ? pdscp->dscp : 0;
4242     return pdscp != NULL;
4243 }
4244
4245 static size_t
4246 count_skb_priorities(const struct xport *xport)
4247 {
4248     return hmap_count(&xport->skb_priorities);
4249 }
4250
4251 static void
4252 clear_skb_priorities(struct xport *xport)
4253 {
4254     struct skb_priority_to_dscp *pdscp, *next;
4255
4256     HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4257         hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4258         free(pdscp);
4259     }
4260 }
4261
4262 static bool
4263 actions_output_to_local_port(const struct xlate_ctx *ctx)
4264 {
4265     odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4266     const struct nlattr *a;
4267     unsigned int left;
4268
4269     NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(ctx->xout->odp_actions),
4270                              ofpbuf_size(ctx->xout->odp_actions)) {
4271         if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4272             && nl_attr_get_odp_port(a) == local_odp_port) {
4273             return true;
4274         }
4275     }
4276     return false;
4277 }
4278
4279 #if defined(__linux__)
4280 /* Returns the maximum number of packets that the Linux kernel is willing to
4281  * queue up internally to certain kinds of software-implemented ports, or the
4282  * default (and rarely modified) value if it cannot be determined. */
4283 static int
4284 netdev_max_backlog(void)
4285 {
4286     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4287     static int max_backlog = 1000; /* The normal default value. */
4288
4289     if (ovsthread_once_start(&once)) {
4290         static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4291         FILE *stream;
4292         int n;
4293
4294         stream = fopen(filename, "r");
4295         if (!stream) {
4296             VLOG_WARN("%s: open failed (%s)", filename, ovs_strerror(errno));
4297         } else {
4298             if (fscanf(stream, "%d", &n) != 1) {
4299                 VLOG_WARN("%s: read error", filename);
4300             } else if (n <= 100) {
4301                 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4302             } else {
4303                 max_backlog = n;
4304             }
4305             fclose(stream);
4306         }
4307         ovsthread_once_done(&once);
4308
4309         VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4310     }
4311
4312     return max_backlog;
4313 }
4314
4315 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4316  * 'odp_actions'. */
4317 static int
4318 count_output_actions(const struct ofpbuf *odp_actions)
4319 {
4320     const struct nlattr *a;
4321     size_t left;
4322     int n = 0;
4323
4324     NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(odp_actions),
4325                              ofpbuf_size(odp_actions)) {
4326         if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4327             n++;
4328         }
4329     }
4330     return n;
4331 }
4332 #endif /* defined(__linux__) */
4333
4334 /* Returns true if 'odp_actions' contains more output actions than the datapath
4335  * can reliably handle in one go.  On Linux, this is the value of the
4336  * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4337  * packets that the kernel is willing to queue up for processing while the
4338  * datapath is processing a set of actions. */
4339 static bool
4340 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4341 {
4342 #ifdef __linux__
4343     return (ofpbuf_size(odp_actions) / NL_A_U32_SIZE > netdev_max_backlog()
4344             && count_output_actions(odp_actions) > netdev_max_backlog());
4345 #else
4346     /* OSes other than Linux might have similar limits, but we don't know how
4347      * to determine them.*/
4348     return false;
4349 #endif
4350 }
4351
4352 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
4353  * into datapath actions in 'odp_actions', using 'ctx'.
4354  *
4355  * The caller must take responsibility for eventually freeing 'xout', with
4356  * xlate_out_uninit(). */
4357 void
4358 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
4359 {
4360     struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4361     struct flow_wildcards *wc = &xout->wc;
4362     struct flow *flow = &xin->flow;
4363     struct rule_dpif *rule = NULL;
4364
4365     enum slow_path_reason special;
4366     const struct ofpact *ofpacts;
4367     struct xport *in_port;
4368     struct flow orig_flow;
4369     struct xlate_ctx ctx;
4370     size_t ofpacts_len;
4371     bool tnl_may_send;
4372     bool is_icmp;
4373
4374     COVERAGE_INC(xlate_actions);
4375
4376     /* Flow initialization rules:
4377      * - 'base_flow' must match the kernel's view of the packet at the
4378      *   time that action processing starts.  'flow' represents any
4379      *   transformations we wish to make through actions.
4380      * - By default 'base_flow' and 'flow' are the same since the input
4381      *   packet matches the output before any actions are applied.
4382      * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4383      *   of the received packet as seen by the kernel.  If we later output
4384      *   to another device without any modifications this will cause us to
4385      *   insert a new tag since the original one was stripped off by the
4386      *   VLAN device.
4387      * - Tunnel metadata as received is retained in 'flow'. This allows
4388      *   tunnel metadata matching also in later tables.
4389      *   Since a kernel action for setting the tunnel metadata will only be
4390      *   generated with actual tunnel output, changing the tunnel metadata
4391      *   values in 'flow' (such as tun_id) will only have effect with a later
4392      *   tunnel output action.
4393      * - Tunnel 'base_flow' is completely cleared since that is what the
4394      *   kernel does.  If we wish to maintain the original values an action
4395      *   needs to be generated. */
4396
4397     ctx.xin = xin;
4398     ctx.xout = xout;
4399     ctx.xout->slow = 0;
4400     ctx.xout->has_learn = false;
4401     ctx.xout->has_normal = false;
4402     ctx.xout->has_fin_timeout = false;
4403     ctx.xout->nf_output_iface = NF_OUT_DROP;
4404     ctx.xout->mirrors = 0;
4405
4406     xout->odp_actions = xin->odp_actions;
4407     if (!xout->odp_actions) {
4408         xout->odp_actions = &xout->odp_actions_buf;
4409         ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
4410                         sizeof xout->odp_actions_stub);
4411     }
4412     ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
4413
4414     ctx.xbridge = xbridge_lookup(xcfg, xin->ofproto);
4415     if (!ctx.xbridge) {
4416         return;
4417     }
4418
4419     ctx.rule = xin->rule;
4420
4421     ctx.base_flow = *flow;
4422     memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
4423     ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
4424
4425     flow_wildcards_init_catchall(wc);
4426     memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
4427     memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
4428     if (is_ip_any(flow)) {
4429         wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4430     }
4431     is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
4432
4433     tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
4434     if (ctx.xbridge->netflow) {
4435         netflow_mask_wc(flow, wc);
4436     }
4437
4438     ctx.recurse = 0;
4439     ctx.resubmits = 0;
4440     ctx.in_group = false;
4441     ctx.in_action_set = false;
4442     ctx.orig_skb_priority = flow->skb_priority;
4443     ctx.table_id = 0;
4444     ctx.exit = false;
4445     ctx.use_recirc = false;
4446     ctx.was_mpls = false;
4447
4448     if (!xin->ofpacts && !ctx.rule) {
4449         rule = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
4450                                 xin->skip_wildcards ? NULL : wc,
4451                                 ctx.xin->xcache != NULL,
4452                                 ctx.xin->resubmit_stats, &ctx.table_id);
4453         if (ctx.xin->resubmit_stats) {
4454             rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
4455         }
4456         if (ctx.xin->xcache) {
4457             struct xc_entry *entry;
4458
4459             entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
4460             entry->u.rule = rule;
4461         }
4462         ctx.rule = rule;
4463
4464         if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
4465             ctx.xin->resubmit_hook(ctx.xin, rule, 0);
4466         }
4467     }
4468     xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
4469
4470     if (xin->ofpacts) {
4471         ofpacts = xin->ofpacts;
4472         ofpacts_len = xin->ofpacts_len;
4473     } else if (ctx.rule) {
4474         const struct rule_actions *actions = rule_dpif_get_actions(ctx.rule);
4475
4476         ofpacts = actions->ofpacts;
4477         ofpacts_len = actions->ofpacts_len;
4478     } else {
4479         OVS_NOT_REACHED();
4480     }
4481
4482     ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
4483
4484     ctx.action_set_has_group = false;
4485     ofpbuf_use_stub(&ctx.action_set,
4486                     ctx.action_set_stub, sizeof ctx.action_set_stub);
4487
4488     if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4489         /* Do this conditionally because the copy is expensive enough that it
4490          * shows up in profiles. */
4491         orig_flow = *flow;
4492     }
4493
4494     in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
4495     if (in_port && in_port->is_tunnel) {
4496         if (ctx.xin->resubmit_stats) {
4497             netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4498             if (in_port->bfd) {
4499                 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4500             }
4501         }
4502         if (ctx.xin->xcache) {
4503             struct xc_entry *entry;
4504
4505             entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4506             entry->u.dev.rx = netdev_ref(in_port->netdev);
4507             entry->u.dev.bfd = bfd_ref(in_port->bfd);
4508         }
4509     }
4510
4511     special = process_special(&ctx, flow, in_port, ctx.xin->packet);
4512     if (special) {
4513         ctx.xout->slow |= special;
4514     } else {
4515         size_t sample_actions_len;
4516
4517         if (flow->in_port.ofp_port
4518             != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
4519                                       flow->in_port.ofp_port,
4520                                       flow->vlan_tci)) {
4521             ctx.base_flow.vlan_tci = 0;
4522         }
4523
4524         add_sflow_action(&ctx);
4525         add_ipfix_action(&ctx);
4526         sample_actions_len = ofpbuf_size(ctx.xout->odp_actions);
4527
4528         if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
4529             do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4530
4531             /* We've let OFPP_NORMAL and the learning action look at the
4532              * packet, so drop it now if forwarding is disabled. */
4533             if (in_port && (!xport_stp_forward_state(in_port) ||
4534                             !xport_rstp_forward_state(in_port))) {
4535                 ofpbuf_set_size(ctx.xout->odp_actions, sample_actions_len);
4536             }
4537         }
4538
4539         if (ofpbuf_size(&ctx.action_set)) {
4540             xlate_action_set(&ctx);
4541         }
4542
4543         if (ctx.xbridge->has_in_band
4544             && in_band_must_output_to_local_port(flow)
4545             && !actions_output_to_local_port(&ctx)) {
4546             compose_output_action(&ctx, OFPP_LOCAL);
4547         }
4548
4549         fix_sflow_action(&ctx);
4550
4551         if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4552             add_mirror_actions(&ctx, &orig_flow);
4553         }
4554     }
4555
4556     if (nl_attr_oversized(ofpbuf_size(ctx.xout->odp_actions))) {
4557         /* These datapath actions are too big for a Netlink attribute, so we
4558          * can't hand them to the kernel directly.  dpif_execute() can execute
4559          * them one by one with help, so just mark the result as SLOW_ACTION to
4560          * prevent the flow from being installed. */
4561         COVERAGE_INC(xlate_actions_oversize);
4562         ctx.xout->slow |= SLOW_ACTION;
4563     } else if (too_many_output_actions(ctx.xout->odp_actions)) {
4564         COVERAGE_INC(xlate_actions_too_many_output);
4565         ctx.xout->slow |= SLOW_ACTION;
4566     }
4567
4568     if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4569         if (ctx.xin->resubmit_stats) {
4570             mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
4571                                 ctx.xin->resubmit_stats->n_packets,
4572                                 ctx.xin->resubmit_stats->n_bytes);
4573         }
4574         if (ctx.xin->xcache) {
4575             struct xc_entry *entry;
4576
4577             entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
4578             entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
4579             entry->u.mirror.mirrors = xout->mirrors;
4580         }
4581     }
4582
4583     if (ctx.xbridge->netflow) {
4584         /* Only update netflow if we don't have controller flow.  We don't
4585          * report NetFlow expiration messages for such facets because they
4586          * are just part of the control logic for the network, not real
4587          * traffic. */
4588         if (ofpacts_len == 0
4589             || ofpacts->type != OFPACT_CONTROLLER
4590             || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
4591             if (ctx.xin->resubmit_stats) {
4592                 netflow_flow_update(ctx.xbridge->netflow, flow,
4593                                     xout->nf_output_iface,
4594                                     ctx.xin->resubmit_stats);
4595             }
4596             if (ctx.xin->xcache) {
4597                 struct xc_entry *entry;
4598
4599                 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
4600                 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
4601                 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
4602                 entry->u.nf.iface = xout->nf_output_iface;
4603             }
4604         }
4605     }
4606
4607     ofpbuf_uninit(&ctx.stack);
4608     ofpbuf_uninit(&ctx.action_set);
4609
4610     /* Clear the metadata and register wildcard masks, because we won't
4611      * use non-header fields as part of the cache. */
4612     flow_wildcards_clear_non_packet_fields(wc);
4613
4614     /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields.  struct flow uses
4615      * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
4616      * these fields.  The datapath interface, on the other hand, represents
4617      * them with just 8 bits each.  This means that if the high 8 bits of the
4618      * masks for these fields somehow become set, then they will get chopped
4619      * off by a round trip through the datapath, and revalidation will spot
4620      * that as an inconsistency and delete the flow.  Avoid the problem here by
4621      * making sure that only the low 8 bits of either field can be unwildcarded
4622      * for ICMP.
4623      */
4624     if (is_icmp) {
4625         wc->masks.tp_src &= htons(UINT8_MAX);
4626         wc->masks.tp_dst &= htons(UINT8_MAX);
4627     }
4628 }
4629
4630 /* Sends 'packet' out 'ofport'.
4631  * May modify 'packet'.
4632  * Returns 0 if successful, otherwise a positive errno value. */
4633 int
4634 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
4635 {
4636     struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4637     struct xport *xport;
4638     struct ofpact_output output;
4639     struct flow flow;
4640
4641     ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
4642     /* Use OFPP_NONE as the in_port to avoid special packet processing. */
4643     flow_extract(packet, NULL, &flow);
4644     flow.in_port.ofp_port = OFPP_NONE;
4645
4646     xport = xport_lookup(xcfg, ofport);
4647     if (!xport) {
4648         return EINVAL;
4649     }
4650     output.port = xport->ofp_port;
4651     output.max_len = 0;
4652
4653     return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
4654                                         &output.ofpact, sizeof output,
4655                                         packet);
4656 }
4657
4658 struct xlate_cache *
4659 xlate_cache_new(void)
4660 {
4661     struct xlate_cache *xcache = xmalloc(sizeof *xcache);
4662
4663     ofpbuf_init(&xcache->entries, 512);
4664     return xcache;
4665 }
4666
4667 static struct xc_entry *
4668 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
4669 {
4670     struct xc_entry *entry;
4671
4672     entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
4673     entry->type = type;
4674
4675     return entry;
4676 }
4677
4678 static void
4679 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
4680 {
4681     if (entry->u.dev.tx) {
4682         netdev_vport_inc_tx(entry->u.dev.tx, stats);
4683     }
4684     if (entry->u.dev.rx) {
4685         netdev_vport_inc_rx(entry->u.dev.rx, stats);
4686     }
4687     if (entry->u.dev.bfd) {
4688         bfd_account_rx(entry->u.dev.bfd, stats);
4689     }
4690 }
4691
4692 static void
4693 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
4694 {
4695     struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4696     struct xbridge *xbridge;
4697     struct xbundle *xbundle;
4698     struct flow_wildcards wc;
4699
4700     xbridge = xbridge_lookup(xcfg, ofproto);
4701     if (!xbridge) {
4702         return;
4703     }
4704
4705     xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
4706                                   NULL);
4707     if (!xbundle) {
4708         return;
4709     }
4710
4711     update_learning_table(xbridge, flow, &wc, vlan, xbundle);
4712 }
4713
4714 /* Push stats and perform side effects of flow translation. */
4715 void
4716 xlate_push_stats(struct xlate_cache *xcache,
4717                  const struct dpif_flow_stats *stats)
4718 {
4719     struct xc_entry *entry;
4720     struct ofpbuf entries = xcache->entries;
4721     uint8_t dmac[ETH_ADDR_LEN];
4722
4723     if (!stats->n_packets) {
4724         return;
4725     }
4726
4727     XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4728         switch (entry->type) {
4729         case XC_RULE:
4730             rule_dpif_credit_stats(entry->u.rule, stats);
4731             break;
4732         case XC_BOND:
4733             bond_account(entry->u.bond.bond, entry->u.bond.flow,
4734                          entry->u.bond.vid, stats->n_bytes);
4735             break;
4736         case XC_NETDEV:
4737             xlate_cache_netdev(entry, stats);
4738             break;
4739         case XC_NETFLOW:
4740             netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
4741                                 entry->u.nf.iface, stats);
4742             break;
4743         case XC_MIRROR:
4744             mirror_update_stats(entry->u.mirror.mbridge,
4745                                 entry->u.mirror.mirrors,
4746                                 stats->n_packets, stats->n_bytes);
4747             break;
4748         case XC_LEARN:
4749             ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
4750             break;
4751         case XC_NORMAL:
4752             xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
4753                                entry->u.normal.vlan);
4754             break;
4755         case XC_FIN_TIMEOUT:
4756             xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
4757                                 entry->u.fin.idle, entry->u.fin.hard);
4758             break;
4759         case XC_GROUP:
4760             group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
4761                                     stats);
4762             break;
4763         case XC_TNL_ARP:
4764             /* Lookup arp to avoid arp timeout. */
4765             tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
4766             break;
4767         default:
4768             OVS_NOT_REACHED();
4769         }
4770     }
4771 }
4772
4773 static void
4774 xlate_dev_unref(struct xc_entry *entry)
4775 {
4776     if (entry->u.dev.tx) {
4777         netdev_close(entry->u.dev.tx);
4778     }
4779     if (entry->u.dev.rx) {
4780         netdev_close(entry->u.dev.rx);
4781     }
4782     if (entry->u.dev.bfd) {
4783         bfd_unref(entry->u.dev.bfd);
4784     }
4785 }
4786
4787 static void
4788 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
4789 {
4790     netflow_flow_clear(netflow, flow);
4791     netflow_unref(netflow);
4792     free(flow);
4793 }
4794
4795 void
4796 xlate_cache_clear(struct xlate_cache *xcache)
4797 {
4798     struct xc_entry *entry;
4799     struct ofpbuf entries;
4800
4801     if (!xcache) {
4802         return;
4803     }
4804
4805     XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4806         switch (entry->type) {
4807         case XC_RULE:
4808             rule_dpif_unref(entry->u.rule);
4809             break;
4810         case XC_BOND:
4811             free(entry->u.bond.flow);
4812             bond_unref(entry->u.bond.bond);
4813             break;
4814         case XC_NETDEV:
4815             xlate_dev_unref(entry);
4816             break;
4817         case XC_NETFLOW:
4818             xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
4819             break;
4820         case XC_MIRROR:
4821             mbridge_unref(entry->u.mirror.mbridge);
4822             break;
4823         case XC_LEARN:
4824             free(entry->u.learn.fm);
4825             ofpbuf_delete(entry->u.learn.ofpacts);
4826             break;
4827         case XC_NORMAL:
4828             free(entry->u.normal.flow);
4829             break;
4830         case XC_FIN_TIMEOUT:
4831             /* 'u.fin.rule' is always already held as a XC_RULE, which
4832              * has already released it's reference above. */
4833             break;
4834         case XC_GROUP:
4835             group_dpif_unref(entry->u.group.group);
4836             break;
4837         case XC_TNL_ARP:
4838             break;
4839         default:
4840             OVS_NOT_REACHED();
4841         }
4842     }
4843
4844     ofpbuf_clear(&xcache->entries);
4845 }
4846
4847 void
4848 xlate_cache_delete(struct xlate_cache *xcache)
4849 {
4850     xlate_cache_clear(xcache);
4851     ofpbuf_uninit(&xcache->entries);
4852     free(xcache);
4853 }