ofproto-dpif-xlate: Simplify the xlate_send_packet() function.
[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         ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1664                                               &ctx->xout->odp_actions,
1665                                               &ctx->xout->wc,
1666                                               &ctx->mpls_depth_delta);
1667         nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1668                             out_port);
1669
1670         ctx->sflow_odp_port = odp_port;
1671         ctx->sflow_n_outputs++;
1672         ctx->xout->nf_output_iface = ofp_port;
1673     }
1674
1675  out:
1676     /* Restore flow */
1677     flow->vlan_tci = flow_vlan_tci;
1678     flow->pkt_mark = flow_pkt_mark;
1679     flow->nw_tos = flow_nw_tos;
1680 }
1681
1682 static void
1683 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1684 {
1685     compose_output_action__(ctx, ofp_port, true);
1686 }
1687
1688 static void
1689 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1690 {
1691     struct rule_dpif *old_rule = ctx->rule;
1692     struct rule_actions *actions;
1693
1694     if (ctx->xin->resubmit_stats) {
1695         rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
1696     }
1697
1698     ctx->resubmits++;
1699     ctx->recurse++;
1700     ctx->rule = rule;
1701     actions = rule_dpif_get_actions(rule);
1702     do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1703     rule_actions_unref(actions);
1704     ctx->rule = old_rule;
1705     ctx->recurse--;
1706 }
1707
1708 static void
1709 xlate_table_action(struct xlate_ctx *ctx,
1710                    ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1711 {
1712     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1713
1714     if (ctx->recurse >= MAX_RESUBMIT_RECURSION) {
1715         VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1716                     MAX_RESUBMIT_RECURSION);
1717     } else if (ctx->resubmits >= MAX_RESUBMITS) {
1718         VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1719     } else if (ctx->xout->odp_actions.size > UINT16_MAX) {
1720         VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1721     } else if (ctx->stack.size >= 65536) {
1722         VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1723     } else {
1724         struct rule_dpif *rule;
1725         ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1726         uint8_t old_table_id = ctx->table_id;
1727
1728         ctx->table_id = table_id;
1729
1730         /* Look up a flow with 'in_port' as the input port.  Then restore the
1731          * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1732          * have surprising behavior). */
1733         ctx->xin->flow.in_port.ofp_port = in_port;
1734         rule_dpif_lookup_in_table(ctx->xbridge->ofproto,
1735                                   &ctx->xin->flow, &ctx->xout->wc,
1736                                   table_id, &rule);
1737         ctx->xin->flow.in_port.ofp_port = old_in_port;
1738
1739         if (ctx->xin->resubmit_hook) {
1740             ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1741         }
1742
1743         if (!rule && may_packet_in) {
1744             struct xport *xport;
1745
1746             /* XXX
1747              * check if table configuration flags
1748              * OFPTC_TABLE_MISS_CONTROLLER, default.
1749              * OFPTC_TABLE_MISS_CONTINUE,
1750              * OFPTC_TABLE_MISS_DROP
1751              * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do? */
1752             xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
1753             choose_miss_rule(xport ? xport->config : 0,
1754                              ctx->xbridge->miss_rule,
1755                              ctx->xbridge->no_packet_in_rule, &rule);
1756         }
1757         if (rule) {
1758             xlate_recursively(ctx, rule);
1759             rule_dpif_unref(rule);
1760         }
1761
1762         ctx->table_id = old_table_id;
1763         return;
1764     }
1765
1766     ctx->exit = true;
1767 }
1768
1769 static void
1770 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1771                       const struct ofpact_resubmit *resubmit)
1772 {
1773     ofp_port_t in_port;
1774     uint8_t table_id;
1775
1776     in_port = resubmit->in_port;
1777     if (in_port == OFPP_IN_PORT) {
1778         in_port = ctx->xin->flow.in_port.ofp_port;
1779     }
1780
1781     table_id = resubmit->table_id;
1782     if (table_id == 255) {
1783         table_id = ctx->table_id;
1784     }
1785
1786     xlate_table_action(ctx, in_port, table_id, false);
1787 }
1788
1789 static void
1790 flood_packets(struct xlate_ctx *ctx, bool all)
1791 {
1792     const struct xport *xport;
1793
1794     HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
1795         if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
1796             continue;
1797         }
1798
1799         if (all) {
1800             compose_output_action__(ctx, xport->ofp_port, false);
1801         } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
1802             compose_output_action(ctx, xport->ofp_port);
1803         }
1804     }
1805
1806     ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1807 }
1808
1809 static void
1810 execute_controller_action(struct xlate_ctx *ctx, int len,
1811                           enum ofp_packet_in_reason reason,
1812                           uint16_t controller_id)
1813 {
1814     struct ofputil_packet_in *pin;
1815     struct ofpbuf *packet;
1816     struct flow key;
1817
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     ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1830                                           &ctx->xout->odp_actions,
1831                                           &ctx->xout->wc,
1832                                           &ctx->mpls_depth_delta);
1833
1834     odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1835                         ctx->xout->odp_actions.size, NULL, NULL);
1836
1837     pin = xmalloc(sizeof *pin);
1838     pin->packet_len = packet->size;
1839     pin->packet = ofpbuf_steal_data(packet);
1840     pin->reason = reason;
1841     pin->controller_id = controller_id;
1842     pin->table_id = ctx->table_id;
1843     pin->cookie = ctx->rule ? rule_dpif_get_flow_cookie(ctx->rule) : 0;
1844
1845     pin->send_len = len;
1846     flow_get_metadata(&ctx->xin->flow, &pin->fmd);
1847
1848     ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
1849     ofpbuf_delete(packet);
1850 }
1851
1852 static bool
1853 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1854 {
1855     struct flow_wildcards *wc = &ctx->xout->wc;
1856     struct flow *flow = &ctx->xin->flow;
1857
1858     ovs_assert(eth_type_mpls(eth_type));
1859
1860     /* If mpls_depth_delta is negative then an MPLS POP action has been
1861      * composed and the resulting MPLS label stack is unknown.  This means
1862      * an MPLS PUSH action can't be composed as it needs to know either the
1863      * top-most MPLS LSE to use as a template for the new MPLS LSE, or that
1864      * there is no MPLS label stack present.  Thus, stop processing.
1865      *
1866      * If mpls_depth_delta is positive then an MPLS PUSH action has been
1867      * composed and no further MPLS PUSH action may be performed without
1868      * losing MPLS LSE and ether type information held in xtx->xin->flow.
1869      * Thus, stop processing.
1870      *
1871      * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE
1872      * has been updated.  Performing a MPLS PUSH action may be would result in
1873      * losing MPLS LSE and ether type information held in xtx->xin->flow.
1874      * Thus, stop processing.
1875      *
1876      * It is planned that in the future this case will be handled
1877      * by recirculation */
1878     if (ctx->mpls_depth_delta ||
1879         ctx->xin->flow.mpls_lse != ctx->base_flow.mpls_lse) {
1880         return true;
1881     }
1882
1883     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1884
1885     ctx->pre_push_mpls_lse = ctx->xin->flow.mpls_lse;
1886
1887     if (eth_type_mpls(ctx->xin->flow.dl_type)) {
1888         flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1889     } else {
1890         ovs_be32 label;
1891         uint8_t tc, ttl;
1892
1893         if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1894             label = htonl(0x2); /* IPV6 Explicit Null. */
1895         } else {
1896             label = htonl(0x0); /* IPV4 Explicit Null. */
1897         }
1898         wc->masks.nw_tos |= IP_DSCP_MASK;
1899         wc->masks.nw_ttl = 0xff;
1900         tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1901         ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1902         flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1903     }
1904     flow->dl_type = eth_type;
1905     ctx->mpls_depth_delta++;
1906
1907     return false;
1908 }
1909
1910 static bool
1911 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1912 {
1913     struct flow_wildcards *wc = &ctx->xout->wc;
1914
1915     if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1916         return true;
1917     }
1918
1919     /* If mpls_depth_delta is negative then an MPLS POP action has been
1920      * composed.  Performing another MPLS POP action
1921      * would result in losing ether type that results from
1922      * the already composed MPLS POP. Thus, stop processing.
1923      *
1924      * It is planned that in the future this case will be handled
1925      * by recirculation */
1926     if (ctx->mpls_depth_delta < 0) {
1927         return true;
1928     }
1929
1930     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1931
1932     /* If mpls_depth_delta is positive then an MPLS PUSH action has been
1933      * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The
1934      * flow's MPLS LSE should be restored to that value to allow any
1935      * subsequent actions that update of the LSE to be executed correctly.
1936      */
1937     if (ctx->mpls_depth_delta > 0) {
1938         ctx->xin->flow.mpls_lse = ctx->pre_push_mpls_lse;
1939     }
1940
1941     ctx->xin->flow.dl_type = eth_type;
1942     ctx->mpls_depth_delta--;
1943
1944     return false;
1945 }
1946
1947 static bool
1948 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1949 {
1950     struct flow *flow = &ctx->xin->flow;
1951
1952     if (!is_ip_any(flow)) {
1953         return false;
1954     }
1955
1956     ctx->xout->wc.masks.nw_ttl = 0xff;
1957     if (flow->nw_ttl > 1) {
1958         flow->nw_ttl--;
1959         return false;
1960     } else {
1961         size_t i;
1962
1963         for (i = 0; i < ids->n_controllers; i++) {
1964             execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1965                                       ids->cnt_ids[i]);
1966         }
1967
1968         /* Stop processing for current table. */
1969         return true;
1970     }
1971 }
1972
1973 static bool
1974 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1975 {
1976     if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1977         return true;
1978     }
1979
1980     /* If mpls_depth_delta is negative then an MPLS POP action has been
1981      * executed and the resulting MPLS label stack is unknown.  This means
1982      * a SET MPLS TTL push action can't be executed as it needs to manipulate
1983      * the top-most MPLS LSE. Thus, stop processing.
1984      *
1985      * It is planned that in the future this case will be handled
1986      * by recirculation.
1987      */
1988     if (ctx->mpls_depth_delta < 0) {
1989         return true;
1990     }
1991
1992     ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
1993     set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1994     return false;
1995 }
1996
1997 static bool
1998 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
1999 {
2000     struct flow *flow = &ctx->xin->flow;
2001     uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
2002     struct flow_wildcards *wc = &ctx->xout->wc;
2003
2004     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2005
2006     if (!eth_type_mpls(flow->dl_type)) {
2007         return false;
2008     }
2009
2010     if (ttl > 1) {
2011         ttl--;
2012         set_mpls_lse_ttl(&flow->mpls_lse, ttl);
2013         return false;
2014     } else {
2015         execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2016
2017         /* Stop processing for current table. */
2018         return true;
2019     }
2020 }
2021
2022 static void
2023 xlate_output_action(struct xlate_ctx *ctx,
2024                     ofp_port_t port, uint16_t max_len, bool may_packet_in)
2025 {
2026     ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2027
2028     ctx->xout->nf_output_iface = NF_OUT_DROP;
2029
2030     switch (port) {
2031     case OFPP_IN_PORT:
2032         compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2033         break;
2034     case OFPP_TABLE:
2035         xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2036                            0, may_packet_in);
2037         break;
2038     case OFPP_NORMAL:
2039         xlate_normal(ctx);
2040         break;
2041     case OFPP_FLOOD:
2042         flood_packets(ctx,  false);
2043         break;
2044     case OFPP_ALL:
2045         flood_packets(ctx, true);
2046         break;
2047     case OFPP_CONTROLLER:
2048         execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2049         break;
2050     case OFPP_NONE:
2051         break;
2052     case OFPP_LOCAL:
2053     default:
2054         if (port != ctx->xin->flow.in_port.ofp_port) {
2055             compose_output_action(ctx, port);
2056         } else {
2057             xlate_report(ctx, "skipping output to input port");
2058         }
2059         break;
2060     }
2061
2062     if (prev_nf_output_iface == NF_OUT_FLOOD) {
2063         ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2064     } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2065         ctx->xout->nf_output_iface = prev_nf_output_iface;
2066     } else if (prev_nf_output_iface != NF_OUT_DROP &&
2067                ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2068         ctx->xout->nf_output_iface = NF_OUT_MULTI;
2069     }
2070 }
2071
2072 static void
2073 xlate_output_reg_action(struct xlate_ctx *ctx,
2074                         const struct ofpact_output_reg *or)
2075 {
2076     uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2077     if (port <= UINT16_MAX) {
2078         union mf_subvalue value;
2079
2080         memset(&value, 0xff, sizeof value);
2081         mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2082         xlate_output_action(ctx, u16_to_ofp(port),
2083                             or->max_len, false);
2084     }
2085 }
2086
2087 static void
2088 xlate_enqueue_action(struct xlate_ctx *ctx,
2089                      const struct ofpact_enqueue *enqueue)
2090 {
2091     ofp_port_t ofp_port = enqueue->port;
2092     uint32_t queue_id = enqueue->queue;
2093     uint32_t flow_priority, priority;
2094     int error;
2095
2096     /* Translate queue to priority. */
2097     error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2098     if (error) {
2099         /* Fall back to ordinary output action. */
2100         xlate_output_action(ctx, enqueue->port, 0, false);
2101         return;
2102     }
2103
2104     /* Check output port. */
2105     if (ofp_port == OFPP_IN_PORT) {
2106         ofp_port = ctx->xin->flow.in_port.ofp_port;
2107     } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2108         return;
2109     }
2110
2111     /* Add datapath actions. */
2112     flow_priority = ctx->xin->flow.skb_priority;
2113     ctx->xin->flow.skb_priority = priority;
2114     compose_output_action(ctx, ofp_port);
2115     ctx->xin->flow.skb_priority = flow_priority;
2116
2117     /* Update NetFlow output port. */
2118     if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2119         ctx->xout->nf_output_iface = ofp_port;
2120     } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2121         ctx->xout->nf_output_iface = NF_OUT_MULTI;
2122     }
2123 }
2124
2125 static void
2126 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2127 {
2128     uint32_t skb_priority;
2129
2130     if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2131         ctx->xin->flow.skb_priority = skb_priority;
2132     } else {
2133         /* Couldn't translate queue to a priority.  Nothing to do.  A warning
2134          * has already been logged. */
2135     }
2136 }
2137
2138 static bool
2139 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2140 {
2141     const struct xbridge *xbridge = xbridge_;
2142     struct xport *port;
2143
2144     switch (ofp_port) {
2145     case OFPP_IN_PORT:
2146     case OFPP_TABLE:
2147     case OFPP_NORMAL:
2148     case OFPP_FLOOD:
2149     case OFPP_ALL:
2150     case OFPP_NONE:
2151         return true;
2152     case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2153         return false;
2154     default:
2155         port = get_ofp_port(xbridge, ofp_port);
2156         return port ? port->may_enable : false;
2157     }
2158 }
2159
2160 static void
2161 xlate_bundle_action(struct xlate_ctx *ctx,
2162                     const struct ofpact_bundle *bundle)
2163 {
2164     ofp_port_t port;
2165
2166     port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2167                           slave_enabled_cb,
2168                           CONST_CAST(struct xbridge *, ctx->xbridge));
2169     if (bundle->dst.field) {
2170         nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2171                      &ctx->xout->wc);
2172     } else {
2173         xlate_output_action(ctx, port, 0, false);
2174     }
2175 }
2176
2177 static void
2178 xlate_learn_action(struct xlate_ctx *ctx,
2179                    const struct ofpact_learn *learn)
2180 {
2181     uint64_t ofpacts_stub[1024 / 8];
2182     struct ofputil_flow_mod fm;
2183     struct ofpbuf ofpacts;
2184
2185     ctx->xout->has_learn = true;
2186
2187     learn_mask(learn, &ctx->xout->wc);
2188
2189     if (!ctx->xin->may_learn) {
2190         return;
2191     }
2192
2193     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2194     learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2195     ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2196     ofpbuf_uninit(&ofpacts);
2197 }
2198
2199 static void
2200 xlate_fin_timeout(struct xlate_ctx *ctx,
2201                   const struct ofpact_fin_timeout *oft)
2202 {
2203     if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2204         rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2205                                   oft->fin_hard_timeout);
2206     }
2207 }
2208
2209 static void
2210 xlate_sample_action(struct xlate_ctx *ctx,
2211                     const struct ofpact_sample *os)
2212 {
2213   union user_action_cookie cookie;
2214   /* Scale the probability from 16-bit to 32-bit while representing
2215    * the same percentage. */
2216   uint32_t probability = (os->probability << 16) | os->probability;
2217
2218   ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2219                                         &ctx->xout->odp_actions,
2220                                         &ctx->xout->wc,
2221                                         &ctx->mpls_depth_delta);
2222
2223   compose_flow_sample_cookie(os->probability, os->collector_set_id,
2224                              os->obs_domain_id, os->obs_point_id, &cookie);
2225   compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2226                         probability, &cookie, sizeof cookie.flow_sample);
2227 }
2228
2229 static bool
2230 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2231 {
2232     if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2233                          ? OFPUTIL_PC_NO_RECV_STP
2234                          : OFPUTIL_PC_NO_RECV)) {
2235         return false;
2236     }
2237
2238     /* Only drop packets here if both forwarding and learning are
2239      * disabled.  If just learning is enabled, we need to have
2240      * OFPP_NORMAL and the learning action have a look at the packet
2241      * before we can drop it. */
2242     if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2243         return false;
2244     }
2245
2246     return true;
2247 }
2248
2249 static void
2250 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2251                  struct xlate_ctx *ctx)
2252 {
2253     struct flow_wildcards *wc = &ctx->xout->wc;
2254     struct flow *flow = &ctx->xin->flow;
2255     const struct ofpact *a;
2256
2257     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2258         struct ofpact_controller *controller;
2259         const struct ofpact_metadata *metadata;
2260
2261         if (ctx->exit) {
2262             break;
2263         }
2264
2265         switch (a->type) {
2266         case OFPACT_OUTPUT:
2267             xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2268                                 ofpact_get_OUTPUT(a)->max_len, true);
2269             break;
2270
2271         case OFPACT_GROUP:
2272             /* XXX not yet implemented */
2273             break;
2274
2275         case OFPACT_CONTROLLER:
2276             controller = ofpact_get_CONTROLLER(a);
2277             execute_controller_action(ctx, controller->max_len,
2278                                       controller->reason,
2279                                       controller->controller_id);
2280             break;
2281
2282         case OFPACT_ENQUEUE:
2283             xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2284             break;
2285
2286         case OFPACT_SET_VLAN_VID:
2287             wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2288             flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2289             flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2290                                | htons(VLAN_CFI));
2291             break;
2292
2293         case OFPACT_SET_VLAN_PCP:
2294             wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2295             flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2296             flow->vlan_tci |=
2297                 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
2298                       | VLAN_CFI);
2299             break;
2300
2301         case OFPACT_STRIP_VLAN:
2302             memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2303             flow->vlan_tci = htons(0);
2304             break;
2305
2306         case OFPACT_PUSH_VLAN:
2307             /* XXX 802.1AD(QinQ) */
2308             memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2309             flow->vlan_tci = htons(VLAN_CFI);
2310             break;
2311
2312         case OFPACT_SET_ETH_SRC:
2313             memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2314             memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2315             break;
2316
2317         case OFPACT_SET_ETH_DST:
2318             memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2319             memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2320             break;
2321
2322         case OFPACT_SET_IPV4_SRC:
2323             memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2324             if (flow->dl_type == htons(ETH_TYPE_IP)) {
2325                 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2326             }
2327             break;
2328
2329         case OFPACT_SET_IPV4_DST:
2330             memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2331             if (flow->dl_type == htons(ETH_TYPE_IP)) {
2332                 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2333             }
2334             break;
2335
2336         case OFPACT_SET_IPV4_DSCP:
2337             wc->masks.nw_tos |= IP_DSCP_MASK;
2338             /* OpenFlow 1.0 only supports IPv4. */
2339             if (flow->dl_type == htons(ETH_TYPE_IP)) {
2340                 flow->nw_tos &= ~IP_DSCP_MASK;
2341                 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
2342             }
2343             break;
2344
2345         case OFPACT_SET_L4_SRC_PORT:
2346             memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2347             memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2348             if (is_ip_any(flow)) {
2349                 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2350             }
2351             break;
2352
2353         case OFPACT_SET_L4_DST_PORT:
2354             memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2355             memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2356             if (is_ip_any(flow)) {
2357                 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2358             }
2359             break;
2360
2361         case OFPACT_RESUBMIT:
2362             xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2363             break;
2364
2365         case OFPACT_SET_TUNNEL:
2366             flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2367             break;
2368
2369         case OFPACT_SET_QUEUE:
2370             xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2371             break;
2372
2373         case OFPACT_POP_QUEUE:
2374             flow->skb_priority = ctx->orig_skb_priority;
2375             break;
2376
2377         case OFPACT_REG_MOVE:
2378             nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2379             break;
2380
2381         case OFPACT_REG_LOAD:
2382             nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
2383             break;
2384
2385         case OFPACT_STACK_PUSH:
2386             nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2387                                    &ctx->stack);
2388             break;
2389
2390         case OFPACT_STACK_POP:
2391             nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2392                                   &ctx->stack);
2393             break;
2394
2395         case OFPACT_PUSH_MPLS:
2396             if (compose_mpls_push_action(ctx,
2397                                          ofpact_get_PUSH_MPLS(a)->ethertype)) {
2398                 return;
2399             }
2400             break;
2401
2402         case OFPACT_POP_MPLS:
2403             if (compose_mpls_pop_action(ctx,
2404                                         ofpact_get_POP_MPLS(a)->ethertype)) {
2405                 return;
2406             }
2407             break;
2408
2409         case OFPACT_SET_MPLS_TTL:
2410             if (compose_set_mpls_ttl_action(ctx,
2411                                             ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2412                 return;
2413             }
2414             break;
2415
2416         case OFPACT_DEC_MPLS_TTL:
2417             if (compose_dec_mpls_ttl_action(ctx)) {
2418                 return;
2419             }
2420             break;
2421
2422         case OFPACT_DEC_TTL:
2423             wc->masks.nw_ttl = 0xff;
2424             if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2425                 return;
2426             }
2427             break;
2428
2429         case OFPACT_NOTE:
2430             /* Nothing to do. */
2431             break;
2432
2433         case OFPACT_MULTIPATH:
2434             multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2435             break;
2436
2437         case OFPACT_BUNDLE:
2438             xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2439             break;
2440
2441         case OFPACT_OUTPUT_REG:
2442             xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2443             break;
2444
2445         case OFPACT_LEARN:
2446             xlate_learn_action(ctx, ofpact_get_LEARN(a));
2447             break;
2448
2449         case OFPACT_EXIT:
2450             ctx->exit = true;
2451             break;
2452
2453         case OFPACT_FIN_TIMEOUT:
2454             memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2455             ctx->xout->has_fin_timeout = true;
2456             xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2457             break;
2458
2459         case OFPACT_CLEAR_ACTIONS:
2460             /* XXX
2461              * Nothing to do because writa-actions is not supported for now.
2462              * When writa-actions is supported, clear-actions also must
2463              * be supported at the same time.
2464              */
2465             break;
2466
2467         case OFPACT_WRITE_METADATA:
2468             metadata = ofpact_get_WRITE_METADATA(a);
2469             flow->metadata &= ~metadata->mask;
2470             flow->metadata |= metadata->metadata & metadata->mask;
2471             break;
2472
2473         case OFPACT_METER:
2474             /* Not implemented yet. */
2475             break;
2476
2477         case OFPACT_GOTO_TABLE: {
2478             /* It is assumed that goto-table is the last action. */
2479             struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2480
2481             ovs_assert(ctx->table_id < ogt->table_id);
2482             xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2483                                ogt->table_id, true);
2484             break;
2485         }
2486
2487         case OFPACT_SAMPLE:
2488             xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2489             break;
2490         }
2491     }
2492 }
2493
2494 void
2495 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2496               const struct flow *flow, struct rule_dpif *rule,
2497               uint8_t tcp_flags, const struct ofpbuf *packet)
2498 {
2499     xin->ofproto = ofproto;
2500     xin->flow = *flow;
2501     xin->packet = packet;
2502     xin->may_learn = packet != NULL;
2503     xin->rule = rule;
2504     xin->ofpacts = NULL;
2505     xin->ofpacts_len = 0;
2506     xin->tcp_flags = tcp_flags;
2507     xin->resubmit_hook = NULL;
2508     xin->report_hook = NULL;
2509     xin->resubmit_stats = NULL;
2510 }
2511
2512 void
2513 xlate_out_uninit(struct xlate_out *xout)
2514 {
2515     if (xout) {
2516         ofpbuf_uninit(&xout->odp_actions);
2517     }
2518 }
2519
2520 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2521  * into datapath actions, using 'ctx', and discards the datapath actions. */
2522 void
2523 xlate_actions_for_side_effects(struct xlate_in *xin)
2524 {
2525     struct xlate_out xout;
2526
2527     xlate_actions(xin, &xout);
2528     xlate_out_uninit(&xout);
2529 }
2530
2531 static void
2532 xlate_report(struct xlate_ctx *ctx, const char *s)
2533 {
2534     if (ctx->xin->report_hook) {
2535         ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2536     }
2537 }
2538
2539 void
2540 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2541 {
2542     dst->wc = src->wc;
2543     dst->slow = src->slow;
2544     dst->has_learn = src->has_learn;
2545     dst->has_normal = src->has_normal;
2546     dst->has_fin_timeout = src->has_fin_timeout;
2547     dst->nf_output_iface = src->nf_output_iface;
2548     dst->mirrors = src->mirrors;
2549
2550     ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2551                     sizeof dst->odp_actions_stub);
2552     ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2553                src->odp_actions.size);
2554 }
2555
2556 /* Returns a reference to the sflow handled associated with ofproto, or NULL if
2557  * there is none.  The caller is responsible for decrementing the results ref
2558  * count with dpif_sflow_unref(). */
2559 struct dpif_sflow *
2560 xlate_get_sflow(const struct ofproto_dpif *ofproto)
2561 {
2562     struct dpif_sflow *sflow = NULL;
2563     struct xbridge *xbridge;
2564
2565     ovs_rwlock_rdlock(&xlate_rwlock);
2566     xbridge = xbridge_lookup(ofproto);
2567     if (xbridge) {
2568         sflow = dpif_sflow_ref(xbridge->sflow);
2569     }
2570     ovs_rwlock_unlock(&xlate_rwlock);
2571
2572     return sflow;
2573 }
2574
2575 /* Returns a reference to the ipfix handled associated with ofproto, or NULL if
2576  * there is none.  The caller is responsible for decrementing the results ref
2577  * count with dpif_ipfix_unref(). */
2578 struct dpif_ipfix *
2579 xlate_get_ipfix(const struct ofproto_dpif *ofproto)
2580 {
2581     struct dpif_ipfix *ipfix = NULL;
2582     struct xbridge *xbridge;
2583
2584     ovs_rwlock_rdlock(&xlate_rwlock);
2585     xbridge = xbridge_lookup(ofproto);
2586     if (xbridge) {
2587         ipfix = dpif_ipfix_ref(xbridge->ipfix);
2588     }
2589     ovs_rwlock_unlock(&xlate_rwlock);
2590
2591     return ipfix;
2592 }
2593 \f
2594 static struct skb_priority_to_dscp *
2595 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2596 {
2597     struct skb_priority_to_dscp *pdscp;
2598     uint32_t hash;
2599
2600     hash = hash_int(skb_priority, 0);
2601     HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2602         if (pdscp->skb_priority == skb_priority) {
2603             return pdscp;
2604         }
2605     }
2606     return NULL;
2607 }
2608
2609 static bool
2610 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2611                        uint8_t *dscp)
2612 {
2613     struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2614     *dscp = pdscp ? pdscp->dscp : 0;
2615     return pdscp != NULL;
2616 }
2617
2618 static void
2619 clear_skb_priorities(struct xport *xport)
2620 {
2621     struct skb_priority_to_dscp *pdscp, *next;
2622
2623     HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2624         hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2625         free(pdscp);
2626     }
2627 }
2628
2629 static bool
2630 actions_output_to_local_port(const struct xlate_ctx *ctx)
2631 {
2632     odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2633     const struct nlattr *a;
2634     unsigned int left;
2635
2636     NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2637                              ctx->xout->odp_actions.size) {
2638         if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2639             && nl_attr_get_odp_port(a) == local_odp_port) {
2640             return true;
2641         }
2642     }
2643     return false;
2644 }
2645
2646 /* Thread safe call to xlate_actions__(). */
2647 void
2648 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
2649 {
2650     ovs_rwlock_rdlock(&xlate_rwlock);
2651     xlate_actions__(xin, xout);
2652     ovs_rwlock_unlock(&xlate_rwlock);
2653 }
2654
2655 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2656  * into datapath actions in 'odp_actions', using 'ctx'.
2657  *
2658  * The caller must take responsibility for eventually freeing 'xout', with
2659  * xlate_out_uninit(). */
2660 static void
2661 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
2662     OVS_REQ_RDLOCK(xlate_rwlock)
2663 {
2664     struct flow_wildcards *wc = &xout->wc;
2665     struct flow *flow = &xin->flow;
2666     struct rule_dpif *rule = NULL;
2667
2668     struct rule_actions *actions = NULL;
2669     enum slow_path_reason special;
2670     const struct ofpact *ofpacts;
2671     struct xport *in_port;
2672     struct flow orig_flow;
2673     struct xlate_ctx ctx;
2674     size_t ofpacts_len;
2675     bool tnl_may_send;
2676
2677     COVERAGE_INC(xlate_actions);
2678
2679     /* Flow initialization rules:
2680      * - 'base_flow' must match the kernel's view of the packet at the
2681      *   time that action processing starts.  'flow' represents any
2682      *   transformations we wish to make through actions.
2683      * - By default 'base_flow' and 'flow' are the same since the input
2684      *   packet matches the output before any actions are applied.
2685      * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
2686      *   of the received packet as seen by the kernel.  If we later output
2687      *   to another device without any modifications this will cause us to
2688      *   insert a new tag since the original one was stripped off by the
2689      *   VLAN device.
2690      * - Tunnel metadata as received is retained in 'flow'. This allows
2691      *   tunnel metadata matching also in later tables.
2692      *   Since a kernel action for setting the tunnel metadata will only be
2693      *   generated with actual tunnel output, changing the tunnel metadata
2694      *   values in 'flow' (such as tun_id) will only have effect with a later
2695      *   tunnel output action.
2696      * - Tunnel 'base_flow' is completely cleared since that is what the
2697      *   kernel does.  If we wish to maintain the original values an action
2698      *   needs to be generated. */
2699
2700     ctx.xin = xin;
2701     ctx.xout = xout;
2702     ctx.xout->slow = 0;
2703     ctx.xout->has_learn = false;
2704     ctx.xout->has_normal = false;
2705     ctx.xout->has_fin_timeout = false;
2706     ctx.xout->nf_output_iface = NF_OUT_DROP;
2707     ctx.xout->mirrors = 0;
2708     ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
2709                     sizeof ctx.xout->odp_actions_stub);
2710     ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
2711
2712     ctx.xbridge = xbridge_lookup(xin->ofproto);
2713     if (!ctx.xbridge) {
2714         goto out;
2715     }
2716
2717     ctx.rule = xin->rule;
2718
2719     ctx.base_flow = *flow;
2720     memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
2721     ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
2722
2723     flow_wildcards_init_catchall(wc);
2724     memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
2725     memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2726     memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
2727     wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
2728
2729     tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
2730     if (ctx.xbridge->has_netflow) {
2731         netflow_mask_wc(flow, wc);
2732     }
2733
2734     ctx.recurse = 0;
2735     ctx.resubmits = 0;
2736     ctx.orig_skb_priority = flow->skb_priority;
2737     ctx.table_id = 0;
2738     ctx.exit = false;
2739     ctx.mpls_depth_delta = 0;
2740
2741     if (!xin->ofpacts && !ctx.rule) {
2742         rule_dpif_lookup(ctx.xbridge->ofproto, flow, wc, &rule);
2743         if (ctx.xin->resubmit_stats) {
2744             rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
2745         }
2746         ctx.rule = rule;
2747     }
2748     xout->fail_open = ctx.rule && rule_dpif_fail_open(ctx.rule);
2749
2750     if (xin->ofpacts) {
2751         ofpacts = xin->ofpacts;
2752         ofpacts_len = xin->ofpacts_len;
2753     } else if (ctx.rule) {
2754         actions = rule_dpif_get_actions(ctx.rule);
2755         ofpacts = actions->ofpacts;
2756         ofpacts_len = actions->ofpacts_len;
2757     } else {
2758         NOT_REACHED();
2759     }
2760
2761     ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
2762
2763     if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2764         /* Do this conditionally because the copy is expensive enough that it
2765          * shows up in profiles. */
2766         orig_flow = *flow;
2767     }
2768
2769     if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
2770         switch (ctx.xbridge->frag) {
2771         case OFPC_FRAG_NORMAL:
2772             /* We must pretend that transport ports are unavailable. */
2773             flow->tp_src = ctx.base_flow.tp_src = htons(0);
2774             flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
2775             break;
2776
2777         case OFPC_FRAG_DROP:
2778             goto out;
2779
2780         case OFPC_FRAG_REASM:
2781             NOT_REACHED();
2782
2783         case OFPC_FRAG_NX_MATCH:
2784             /* Nothing to do. */
2785             break;
2786
2787         case OFPC_INVALID_TTL_TO_CONTROLLER:
2788             NOT_REACHED();
2789         }
2790     }
2791
2792     in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
2793     special = process_special(&ctx, flow, in_port, ctx.xin->packet);
2794     if (special) {
2795         ctx.xout->slow |= special;
2796     } else {
2797         size_t sample_actions_len;
2798
2799         if (flow->in_port.ofp_port
2800             != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
2801                                       flow->in_port.ofp_port,
2802                                       flow->vlan_tci)) {
2803             ctx.base_flow.vlan_tci = 0;
2804         }
2805
2806         add_sflow_action(&ctx);
2807         add_ipfix_action(&ctx);
2808         sample_actions_len = ctx.xout->odp_actions.size;
2809
2810         if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
2811             do_xlate_actions(ofpacts, ofpacts_len, &ctx);
2812
2813             /* We've let OFPP_NORMAL and the learning action look at the
2814              * packet, so drop it now if forwarding is disabled. */
2815             if (in_port && !xport_stp_forward_state(in_port)) {
2816                 ctx.xout->odp_actions.size = sample_actions_len;
2817             }
2818         }
2819
2820         if (ctx.xbridge->has_in_band
2821             && in_band_must_output_to_local_port(flow)
2822             && !actions_output_to_local_port(&ctx)) {
2823             compose_output_action(&ctx, OFPP_LOCAL);
2824         }
2825
2826         fix_sflow_action(&ctx);
2827
2828         if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2829             add_mirror_actions(&ctx, &orig_flow);
2830         }
2831     }
2832
2833     if (nl_attr_oversized(ctx.xout->odp_actions.size)) {
2834         /* These datapath actions are too big for a Netlink attribute, so we
2835          * can't execute them. */
2836         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2837
2838         VLOG_ERR_RL(&rl, "discarding oversize datapath actions");
2839         ofpbuf_clear(&ctx.xout->odp_actions);
2840     }
2841
2842     ofpbuf_uninit(&ctx.stack);
2843
2844     /* Clear the metadata and register wildcard masks, because we won't
2845      * use non-header fields as part of the cache. */
2846     memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2847     memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
2848
2849 out:
2850     rule_actions_unref(actions);
2851     rule_dpif_unref(rule);
2852 }
2853
2854 /* Sends 'packet' out 'ofport'.
2855  * May modify 'packet'.
2856  * Returns 0 if successful, otherwise a positive errno value. */
2857 int
2858 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
2859 {
2860     struct xport *xport;
2861     struct ofpact_output output;
2862     struct flow flow;
2863     union flow_in_port in_port_;
2864     int error;
2865
2866     ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2867     /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2868     in_port_.ofp_port = OFPP_NONE;
2869     flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
2870
2871     ovs_rwlock_rdlock(&xlate_rwlock);
2872     xport = xport_lookup(ofport);
2873     if (!xport) {
2874         ovs_rwlock_unlock(&xlate_rwlock);
2875         return EINVAL;
2876     }
2877     output.port = xport->ofp_port;
2878     output.max_len = 0;
2879     error =  ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
2880                                           &output.ofpact, sizeof output,
2881                                           packet);
2882     ovs_rwlock_unlock(&xlate_rwlock);
2883     return error;
2884 }