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