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