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