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