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