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