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