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