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