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