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