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