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