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