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