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