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