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