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