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