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