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