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