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