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