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