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