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