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