~ubuntu-branches/ubuntu/wily/openvswitch/wily

« back to all changes in this revision

Viewing changes to ofproto/ofproto-dpif-xlate.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-08-10 11:35:15 UTC
  • mfrom: (1.1.30)
  • Revision ID: package-import@ubuntu.com-20150810113515-575vj06oq29emxsn
Tags: 2.4.0~git20150810.97bab95-0ubuntu1
* New upstream snapshot from 2.4 branch:
  - d/*: Align any relevant packaging changes with upstream.
* d/*: wrap-and-sort.
* d/openvswitch-{common,vswitch}.install: Correct install location for
  bash completion files.
* d/tests/openflow.py: Explicitly use ovs-testcontroller as provided
  by 2.4.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#include "ofproto/ofproto-dpif-xlate.h"
18
18
 
19
19
#include <errno.h>
 
20
#include <arpa/inet.h>
 
21
#include <net/if.h>
 
22
#include <sys/socket.h>
 
23
#include <netinet/in.h>
20
24
 
 
25
#include "tnl-arp-cache.h"
21
26
#include "bfd.h"
22
27
#include "bitmap.h"
23
28
#include "bond.h"
26
31
#include "cfm.h"
27
32
#include "connmgr.h"
28
33
#include "coverage.h"
 
34
#include "dp-packet.h"
29
35
#include "dpif.h"
30
36
#include "dynamic-string.h"
31
37
#include "in-band.h"
32
38
#include "lacp.h"
33
39
#include "learn.h"
34
40
#include "list.h"
 
41
#include "ovs-lldp.h"
35
42
#include "mac-learning.h"
 
43
#include "mcast-snooping.h"
36
44
#include "meta-flow.h"
37
45
#include "multipath.h"
38
46
#include "netdev-vport.h"
46
54
#include "ofproto/ofproto-dpif-sflow.h"
47
55
#include "ofproto/ofproto-dpif.h"
48
56
#include "ofproto/ofproto-provider.h"
 
57
#include "ovs-router.h"
 
58
#include "tnl-ports.h"
49
59
#include "tunnel.h"
50
 
#include "vlog.h"
 
60
#include "openvswitch/vlog.h"
51
61
 
52
62
COVERAGE_DEFINE(xlate_actions);
53
63
COVERAGE_DEFINE(xlate_actions_oversize);
54
64
COVERAGE_DEFINE(xlate_actions_too_many_output);
55
 
COVERAGE_DEFINE(xlate_actions_mpls_overflow);
56
65
 
57
66
VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
58
67
 
66
75
 * recursive or not. */
67
76
#define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
68
77
 
69
 
struct fat_rwlock xlate_rwlock;
70
 
 
71
78
struct xbridge {
72
79
    struct hmap_node hmap_node;   /* Node in global 'xbridges' map. */
73
80
    struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
74
81
 
75
 
    struct list xbundles;         /* Owned xbundles. */
 
82
    struct ovs_list xbundles;     /* Owned xbundles. */
76
83
    struct hmap xports;           /* Indexed by ofp_port. */
77
84
 
78
85
    char *name;                   /* Name used in log messages. */
79
86
    struct dpif *dpif;            /* Datapath interface. */
80
87
    struct mac_learning *ml;      /* Mac learning handle. */
 
88
    struct mcast_snooping *ms;    /* Multicast Snooping handle. */
81
89
    struct mbridge *mbridge;      /* Mirroring. */
82
90
    struct dpif_sflow *sflow;     /* SFlow handle, or null. */
83
91
    struct dpif_ipfix *ipfix;     /* Ipfix handle, or null. */
84
92
    struct netflow *netflow;      /* Netflow handle, or null. */
85
93
    struct stp *stp;              /* STP or null if disabled. */
86
 
 
87
 
    /* Special rules installed by ofproto-dpif. */
88
 
    struct rule_dpif *miss_rule;
89
 
    struct rule_dpif *no_packet_in_rule;
90
 
 
91
 
    enum ofp_config_flags frag;   /* Fragmentation handling. */
 
94
    struct rstp *rstp;            /* RSTP or null if disabled. */
 
95
 
92
96
    bool has_in_band;             /* Bridge has in band control? */
93
97
    bool forward_bpdu;            /* Bridge forwards STP BPDUs? */
94
98
 
95
 
    /* True if the datapath supports recirculation. */
96
 
    bool enable_recirc;
97
 
 
98
 
    /* True if the datapath supports variable-length
99
 
     * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
100
 
     * False if the datapath supports only 8-byte (or shorter) userdata. */
101
 
    bool variable_length_userdata;
102
 
 
103
 
    /* Number of MPLS label stack entries that the datapath supports
104
 
     * in matches. */
105
 
    size_t max_mpls_depth;
 
99
    /* Datapath feature support. */
 
100
    struct dpif_backer_support support;
106
101
};
107
102
 
108
103
struct xbundle {
109
104
    struct hmap_node hmap_node;    /* In global 'xbundles' map. */
110
105
    struct ofbundle *ofbundle;     /* Key in global 'xbundles' map. */
111
106
 
112
 
    struct list list_node;         /* In parent 'xbridges' list. */
 
107
    struct ovs_list list_node;     /* In parent 'xbridges' list. */
113
108
    struct xbridge *xbridge;       /* Parent xbridge. */
114
109
 
115
 
    struct list xports;            /* Contains "struct xport"s. */
 
110
    struct ovs_list xports;        /* Contains "struct xport"s. */
116
111
 
117
112
    char *name;                    /* Name used in log messages. */
118
113
    struct bond *bond;             /* Nonnull iff more than one port. */
135
130
 
136
131
    odp_port_t odp_port;             /* Datapath port number or ODPP_NONE. */
137
132
 
138
 
    struct list bundle_node;         /* In parent xbundle (if it exists). */
 
133
    struct ovs_list bundle_node;     /* In parent xbundle (if it exists). */
139
134
    struct xbundle *xbundle;         /* Parent xbundle or null. */
140
135
 
141
136
    struct netdev *netdev;           /* 'ofport''s netdev. */
146
141
    enum ofputil_port_config config; /* OpenFlow port configuration. */
147
142
    enum ofputil_port_state state;   /* OpenFlow port state. */
148
143
    int stp_port_no;                 /* STP port number or -1 if not in use. */
 
144
    struct rstp_port *rstp_port;     /* RSTP port or null. */
149
145
 
150
146
    struct hmap skb_priorities;      /* Map of 'skb_priority_to_dscp's. */
151
147
 
154
150
 
155
151
    struct cfm *cfm;                 /* CFM handle or null. */
156
152
    struct bfd *bfd;                 /* BFD handle or null. */
 
153
    struct lldp *lldp;               /* LLDP handle or null. */
157
154
};
158
155
 
159
156
struct xlate_ctx {
162
159
 
163
160
    const struct xbridge *xbridge;
164
161
 
 
162
    /* Flow tables version at the beginning of the translation. */
 
163
    cls_version_t tables_version;
 
164
 
165
165
    /* Flow at the last commit. */
166
166
    struct flow base_flow;
167
167
 
185
185
    int recurse;                /* Current resubmit nesting depth. */
186
186
    int resubmits;              /* Total number of resubmits. */
187
187
    bool in_group;              /* Currently translating ofgroup, if true. */
 
188
    bool in_action_set;         /* Currently translating action_set, if true. */
188
189
 
 
190
    uint8_t table_id;           /* OpenFlow table ID where flow was found. */
 
191
    ovs_be64 rule_cookie;       /* Cookie of the rule being translated. */
189
192
    uint32_t orig_skb_priority; /* Priority when packet arrived. */
190
 
    uint8_t table_id;           /* OpenFlow table ID where flow was found. */
191
193
    uint32_t sflow_n_outputs;   /* Number of output ports. */
192
194
    odp_port_t sflow_odp_port;  /* Output port for composing sFlow action. */
193
195
    uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
194
196
    bool exit;                  /* No further actions should be processed. */
195
197
 
196
 
    bool use_recirc;            /* Should generate recirc? */
197
 
    struct xlate_recirc recirc; /* Information used for generating
198
 
                                 * recirculation actions */
 
198
   /* These are used for non-bond recirculation.  The recirculation IDs are
 
199
    * stored in xout and must be associated with a datapath flow (ukey),
 
200
    * otherwise they will be freed when the xout is uninitialized.
 
201
    *
 
202
    *
 
203
    * Steps in Recirculation Translation
 
204
    * ==================================
 
205
    *
 
206
    * At some point during translation, the code recognizes the need for
 
207
    * recirculation.  For example, recirculation is necessary when, after
 
208
    * popping the last MPLS label, an action or a match tries to examine or
 
209
    * modify a field that has been newly revealed following the MPLS label.
 
210
    *
 
211
    * The simplest part of the work to be done is to commit existing changes to
 
212
    * the packet, which produces datapath actions corresponding to the changes,
 
213
    * and after this, add an OVS_ACTION_ATTR_RECIRC datapath action.
 
214
    *
 
215
    * The main problem here is preserving state.  When the datapath executes
 
216
    * OVS_ACTION_ATTR_RECIRC, it will upcall to userspace to get a translation
 
217
    * for the post-recirculation actions.  At this point userspace has to
 
218
    * resume the translation where it left off, which means that it has to
 
219
    * execute the following:
 
220
    *
 
221
    *     - The action that prompted recirculation, and any actions following
 
222
    *       it within the same flow.
 
223
    *
 
224
    *     - If the action that prompted recirculation was invoked within a
 
225
    *       NXAST_RESUBMIT, then any actions following the resubmit.  These
 
226
    *       "resubmit"s can be nested, so this has to go all the way up the
 
227
    *       control stack.
 
228
    *
 
229
    *     - The OpenFlow 1.1+ action set.
 
230
    *
 
231
    * State that actions and flow table lookups can depend on, such as the
 
232
    * following, must also be preserved:
 
233
    *
 
234
    *     - Metadata fields (input port, registers, OF1.1+ metadata, ...).
 
235
    *
 
236
    *     - Action set, stack
 
237
    *
 
238
    *     - The table ID and cookie of the flow being translated at each level
 
239
    *       of the control stack (since OFPAT_CONTROLLER actions send these to
 
240
    *       the controller).
 
241
    *
 
242
    * Translation allows for the control of this state preservation via these
 
243
    * members.  When a need for recirculation is identified, the translation
 
244
    * process:
 
245
    *
 
246
    * 1. Sets 'recirc_action_offset' to the current size of 'action_set'.  The
 
247
    *    action set is part of what needs to be preserved, so this allows the
 
248
    *    action set and the additional state to share the 'action_set' buffer.
 
249
    *    Later steps can tell that setup for recirculation is in progress from
 
250
    *    the nonnegative value of 'recirc_action_offset'.
 
251
    *
 
252
    * 2. Sets 'exit' to true to tell later steps that we're exiting from the
 
253
    *    translation process.
 
254
    *
 
255
    * 3. Adds an OFPACT_UNROLL_XLATE action to 'action_set'.  This action
 
256
    *    holds the current table ID and cookie so that they can be restored
 
257
    *    during a post-recirculation upcall translation.
 
258
    *
 
259
    * 4. Adds the action that prompted recirculation and any actions following
 
260
    *    it within the same flow to 'action_set', so that they can be executed
 
261
    *    during a post-recirculation upcall translation.
 
262
    *
 
263
    * 5. Returns.
 
264
    *
 
265
    * 6. The action that prompted recirculation might be nested in a stack of
 
266
    *    nested "resubmit"s that have actions remaining.  Each of these notices
 
267
    *    that we're exiting (from 'exit') and that recirculation setup is in
 
268
    *    progress (from 'recirc_action_offset') and responds by adding more
 
269
    *    OFPACT_UNROLL_XLATE actions to 'action_set', as necessary, and any
 
270
    *    actions that were yet unprocessed.
 
271
    *
 
272
    * The caller stores all the state produced by this process associated with
 
273
    * the recirculation ID.  For post-recirculation upcall translation, the
 
274
    * caller passes it back in for the new translation to execute.  The
 
275
    * process yielded a set of ofpacts that can be translated directly, so it
 
276
    * is not much of a special case at that point.
 
277
    */
 
278
    int recirc_action_offset;   /* Offset in 'action_set' to actions to be
 
279
                                 * executed after recirculation, or -1. */
 
280
    int last_unroll_offset;     /* Offset in 'action_set' to the latest unroll
 
281
                                 * action, or -1. */
 
282
 
 
283
    /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
 
284
     * This is a trigger for recirculation in cases where translating an action
 
285
     * or looking up a flow requires access to the fields of the packet after
 
286
     * the MPLS label stack that was originally present. */
 
287
    bool was_mpls;
199
288
 
200
289
    /* OpenFlow 1.1+ action set.
201
290
     *
202
291
     * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
203
292
     * When translation is otherwise complete, ofpacts_execute_action_set()
204
293
     * converts it to a set of "struct ofpact"s that can be translated into
205
 
     * datapath actions.   */
 
294
     * datapath actions. */
 
295
    bool action_set_has_group;  /* Action set contains OFPACT_GROUP? */
206
296
    struct ofpbuf action_set;   /* Action set. */
207
297
    uint64_t action_set_stub[1024 / 8];
208
298
};
209
299
 
 
300
static void xlate_action_set(struct xlate_ctx *ctx);
 
301
 
 
302
static void
 
303
ctx_trigger_recirculation(struct xlate_ctx *ctx)
 
304
{
 
305
    ctx->exit = true;
 
306
    ctx->recirc_action_offset = ctx->action_set.size;
 
307
}
 
308
 
 
309
static bool
 
310
ctx_first_recirculation_action(const struct xlate_ctx *ctx)
 
311
{
 
312
    return ctx->recirc_action_offset == ctx->action_set.size;
 
313
}
 
314
 
 
315
static inline bool
 
316
exit_recirculates(const struct xlate_ctx *ctx)
 
317
{
 
318
    /* When recirculating the 'recirc_action_offset' has a non-negative value.
 
319
     */
 
320
    return ctx->recirc_action_offset >= 0;
 
321
}
 
322
 
 
323
static void compose_recirculate_action(struct xlate_ctx *ctx);
 
324
 
210
325
/* A controller may use OFPP_NONE as the ingress port to indicate that
211
326
 * it did not arrive on a "real" port.  'ofpp_none_bundle' exists for
212
327
 * when an input bundle is needed for validation (e.g., mirroring or
236
351
    XC_LEARN,
237
352
    XC_NORMAL,
238
353
    XC_FIN_TIMEOUT,
 
354
    XC_GROUP,
 
355
    XC_TNL_ARP,
239
356
};
240
357
 
241
358
/* xlate_cache entries hold enough information to perform the side effects of
281
398
            uint16_t idle;
282
399
            uint16_t hard;
283
400
        } fin;
 
401
        struct {
 
402
            struct group_dpif *group;
 
403
            struct ofputil_bucket *bucket;
 
404
        } group;
 
405
        struct {
 
406
            char br_name[IFNAMSIZ];
 
407
            ovs_be32 d_ip;
 
408
        } tnl_arp_cache;
284
409
    } u;
285
410
};
286
411
 
294
419
    struct ofpbuf entries;
295
420
};
296
421
 
297
 
static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
298
 
static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
299
 
static struct hmap xports = HMAP_INITIALIZER(&xports);
 
422
/* Xlate config contains hash maps of all bridges, bundles and ports.
 
423
 * Xcfgp contains the pointer to the current xlate configuration.
 
424
 * When the main thread needs to change the configuration, it copies xcfgp to
 
425
 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
 
426
 * does not block handler and revalidator threads. */
 
427
struct xlate_cfg {
 
428
    struct hmap xbridges;
 
429
    struct hmap xbundles;
 
430
    struct hmap xports;
 
431
};
 
432
static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
 
433
static struct xlate_cfg *new_xcfg = NULL;
300
434
 
301
435
static bool may_receive(const struct xport *, struct xlate_ctx *);
302
436
static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
303
437
                             struct xlate_ctx *);
304
 
static void xlate_actions__(struct xlate_in *, struct xlate_out *)
305
 
    OVS_REQ_RDLOCK(xlate_rwlock);
306
438
static void xlate_normal(struct xlate_ctx *);
307
 
static void xlate_report(struct xlate_ctx *, const char *);
 
439
static inline void xlate_report(struct xlate_ctx *, const char *);
308
440
static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
309
441
                               uint8_t table_id, bool may_packet_in,
310
442
                               bool honor_table_miss);
312
444
static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
313
445
static void output_normal(struct xlate_ctx *, const struct xbundle *,
314
446
                          uint16_t vlan);
315
 
static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
316
 
 
317
 
static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
318
 
static struct xbundle *xbundle_lookup(const struct ofbundle *);
319
 
static struct xport *xport_lookup(const struct ofport_dpif *);
 
447
 
 
448
/* Optional bond recirculation parameter to compose_output_action(). */
 
449
struct xlate_bond_recirc {
 
450
    uint32_t recirc_id;  /* !0 Use recirculation instead of output. */
 
451
    uint8_t  hash_alg;   /* !0 Compute hash for recirc before. */
 
452
    uint32_t hash_basis;  /* Compute hash for recirc before. */
 
453
};
 
454
 
 
455
static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
 
456
                                  const struct xlate_bond_recirc *xr);
 
457
 
 
458
static struct xbridge *xbridge_lookup(struct xlate_cfg *,
 
459
                                      const struct ofproto_dpif *);
 
460
static struct xbundle *xbundle_lookup(struct xlate_cfg *,
 
461
                                      const struct ofbundle *);
 
462
static struct xport *xport_lookup(struct xlate_cfg *,
 
463
                                  const struct ofport_dpif *);
320
464
static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
321
465
static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
322
466
                                                     uint32_t skb_priority);
323
467
static void clear_skb_priorities(struct xport *);
 
468
static size_t count_skb_priorities(const struct xport *);
324
469
static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
325
470
                                   uint8_t *dscp);
326
471
 
327
472
static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
328
473
                                              enum xc_type type);
329
 
 
330
 
void
331
 
xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
332
 
                  struct dpif *dpif, struct rule_dpif *miss_rule,
333
 
                  struct rule_dpif *no_packet_in_rule,
 
474
static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
 
475
static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
 
476
static void xlate_xport_init(struct xlate_cfg *, struct xport *);
 
477
static void xlate_xbridge_set(struct xbridge *, struct dpif *,
 
478
                              const struct mac_learning *, struct stp *,
 
479
                              struct rstp *, const struct mcast_snooping *,
 
480
                              const struct mbridge *,
 
481
                              const struct dpif_sflow *,
 
482
                              const struct dpif_ipfix *,
 
483
                              const struct netflow *,
 
484
                              bool forward_bpdu, bool has_in_band,
 
485
                              const struct dpif_backer_support *);
 
486
static void xlate_xbundle_set(struct xbundle *xbundle,
 
487
                              enum port_vlan_mode vlan_mode, int vlan,
 
488
                              unsigned long *trunks, bool use_priority_tags,
 
489
                              const struct bond *bond, const struct lacp *lacp,
 
490
                              bool floodable);
 
491
static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
 
492
                            const struct netdev *netdev, const struct cfm *cfm,
 
493
                            const struct bfd *bfd, const struct lldp *lldp,
 
494
                            int stp_port_no, const struct rstp_port *rstp_port,
 
495
                            enum ofputil_port_config config,
 
496
                            enum ofputil_port_state state, bool is_tunnel,
 
497
                            bool may_enable);
 
498
static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
 
499
static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
 
500
static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
 
501
static void xlate_xbridge_copy(struct xbridge *);
 
502
static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
 
503
static void xlate_xport_copy(struct xbridge *, struct xbundle *,
 
504
                             struct xport *);
 
505
static void xlate_xcfg_free(struct xlate_cfg *);
 
506
 
 
507
static inline void
 
508
xlate_report(struct xlate_ctx *ctx, const char *s)
 
509
{
 
510
    if (OVS_UNLIKELY(ctx->xin->report_hook)) {
 
511
        ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
 
512
    }
 
513
}
 
514
 
 
515
static void
 
516
xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
 
517
{
 
518
    list_init(&xbridge->xbundles);
 
519
    hmap_init(&xbridge->xports);
 
520
    hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
 
521
                hash_pointer(xbridge->ofproto, 0));
 
522
}
 
523
 
 
524
static void
 
525
xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
 
526
{
 
527
    list_init(&xbundle->xports);
 
528
    list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
 
529
    hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
 
530
                hash_pointer(xbundle->ofbundle, 0));
 
531
}
 
532
 
 
533
static void
 
534
xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
 
535
{
 
536
    hmap_init(&xport->skb_priorities);
 
537
    hmap_insert(&xcfg->xports, &xport->hmap_node,
 
538
                hash_pointer(xport->ofport, 0));
 
539
    hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
 
540
                hash_ofp_port(xport->ofp_port));
 
541
}
 
542
 
 
543
static void
 
544
xlate_xbridge_set(struct xbridge *xbridge,
 
545
                  struct dpif *dpif,
334
546
                  const struct mac_learning *ml, struct stp *stp,
 
547
                  struct rstp *rstp, const struct mcast_snooping *ms,
335
548
                  const struct mbridge *mbridge,
336
549
                  const struct dpif_sflow *sflow,
337
550
                  const struct dpif_ipfix *ipfix,
338
 
                  const struct netflow *netflow, enum ofp_config_flags frag,
 
551
                  const struct netflow *netflow,
339
552
                  bool forward_bpdu, bool has_in_band,
340
 
                  bool enable_recirc,
341
 
                  bool variable_length_userdata,
342
 
                  size_t max_mpls_depth)
 
553
                  const struct dpif_backer_support *support)
343
554
{
344
 
    struct xbridge *xbridge = xbridge_lookup(ofproto);
345
 
 
346
 
    if (!xbridge) {
347
 
        xbridge = xzalloc(sizeof *xbridge);
348
 
        xbridge->ofproto = ofproto;
349
 
 
350
 
        hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
351
 
        hmap_init(&xbridge->xports);
352
 
        list_init(&xbridge->xbundles);
353
 
    }
354
 
 
355
555
    if (xbridge->ml != ml) {
356
556
        mac_learning_unref(xbridge->ml);
357
557
        xbridge->ml = mac_learning_ref(ml);
358
558
    }
359
559
 
 
560
    if (xbridge->ms != ms) {
 
561
        mcast_snooping_unref(xbridge->ms);
 
562
        xbridge->ms = mcast_snooping_ref(ms);
 
563
    }
 
564
 
360
565
    if (xbridge->mbridge != mbridge) {
361
566
        mbridge_unref(xbridge->mbridge);
362
567
        xbridge->mbridge = mbridge_ref(mbridge);
377
582
        xbridge->stp = stp_ref(stp);
378
583
    }
379
584
 
 
585
    if (xbridge->rstp != rstp) {
 
586
        rstp_unref(xbridge->rstp);
 
587
        xbridge->rstp = rstp_ref(rstp);
 
588
    }
 
589
 
380
590
    if (xbridge->netflow != netflow) {
381
591
        netflow_unref(xbridge->netflow);
382
592
        xbridge->netflow = netflow_ref(netflow);
383
593
    }
384
594
 
385
 
    free(xbridge->name);
386
 
    xbridge->name = xstrdup(name);
387
 
 
388
595
    xbridge->dpif = dpif;
389
596
    xbridge->forward_bpdu = forward_bpdu;
390
597
    xbridge->has_in_band = has_in_band;
391
 
    xbridge->frag = frag;
392
 
    xbridge->miss_rule = miss_rule;
393
 
    xbridge->no_packet_in_rule = no_packet_in_rule;
394
 
    xbridge->enable_recirc = enable_recirc;
395
 
    xbridge->variable_length_userdata = variable_length_userdata;
396
 
    xbridge->max_mpls_depth = max_mpls_depth;
397
 
}
398
 
 
399
 
void
400
 
xlate_remove_ofproto(struct ofproto_dpif *ofproto)
401
 
{
402
 
    struct xbridge *xbridge = xbridge_lookup(ofproto);
 
598
    xbridge->support = *support;
 
599
}
 
600
 
 
601
static void
 
602
xlate_xbundle_set(struct xbundle *xbundle,
 
603
                  enum port_vlan_mode vlan_mode, int vlan,
 
604
                  unsigned long *trunks, bool use_priority_tags,
 
605
                  const struct bond *bond, const struct lacp *lacp,
 
606
                  bool floodable)
 
607
{
 
608
    ovs_assert(xbundle->xbridge);
 
609
 
 
610
    xbundle->vlan_mode = vlan_mode;
 
611
    xbundle->vlan = vlan;
 
612
    xbundle->trunks = trunks;
 
613
    xbundle->use_priority_tags = use_priority_tags;
 
614
    xbundle->floodable = floodable;
 
615
 
 
616
    if (xbundle->bond != bond) {
 
617
        bond_unref(xbundle->bond);
 
618
        xbundle->bond = bond_ref(bond);
 
619
    }
 
620
 
 
621
    if (xbundle->lacp != lacp) {
 
622
        lacp_unref(xbundle->lacp);
 
623
        xbundle->lacp = lacp_ref(lacp);
 
624
    }
 
625
}
 
626
 
 
627
static void
 
628
xlate_xport_set(struct xport *xport, odp_port_t odp_port,
 
629
                const struct netdev *netdev, const struct cfm *cfm,
 
630
                const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
 
631
                const struct rstp_port* rstp_port,
 
632
                enum ofputil_port_config config, enum ofputil_port_state state,
 
633
                bool is_tunnel, bool may_enable)
 
634
{
 
635
    xport->config = config;
 
636
    xport->state = state;
 
637
    xport->stp_port_no = stp_port_no;
 
638
    xport->is_tunnel = is_tunnel;
 
639
    xport->may_enable = may_enable;
 
640
    xport->odp_port = odp_port;
 
641
 
 
642
    if (xport->rstp_port != rstp_port) {
 
643
        rstp_port_unref(xport->rstp_port);
 
644
        xport->rstp_port = rstp_port_ref(rstp_port);
 
645
    }
 
646
 
 
647
    if (xport->cfm != cfm) {
 
648
        cfm_unref(xport->cfm);
 
649
        xport->cfm = cfm_ref(cfm);
 
650
    }
 
651
 
 
652
    if (xport->bfd != bfd) {
 
653
        bfd_unref(xport->bfd);
 
654
        xport->bfd = bfd_ref(bfd);
 
655
    }
 
656
 
 
657
    if (xport->lldp != lldp) {
 
658
        lldp_unref(xport->lldp);
 
659
        xport->lldp = lldp_ref(lldp);
 
660
    }
 
661
 
 
662
    if (xport->netdev != netdev) {
 
663
        netdev_close(xport->netdev);
 
664
        xport->netdev = netdev_ref(netdev);
 
665
    }
 
666
}
 
667
 
 
668
static void
 
669
xlate_xbridge_copy(struct xbridge *xbridge)
 
670
{
 
671
    struct xbundle *xbundle;
 
672
    struct xport *xport;
 
673
    struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
 
674
    new_xbridge->ofproto = xbridge->ofproto;
 
675
    new_xbridge->name = xstrdup(xbridge->name);
 
676
    xlate_xbridge_init(new_xcfg, new_xbridge);
 
677
 
 
678
    xlate_xbridge_set(new_xbridge,
 
679
                      xbridge->dpif, xbridge->ml, xbridge->stp,
 
680
                      xbridge->rstp, xbridge->ms, xbridge->mbridge,
 
681
                      xbridge->sflow, xbridge->ipfix, xbridge->netflow,
 
682
                      xbridge->forward_bpdu, xbridge->has_in_band,
 
683
                      &xbridge->support);
 
684
    LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
 
685
        xlate_xbundle_copy(new_xbridge, xbundle);
 
686
    }
 
687
 
 
688
    /* Copy xports which are not part of a xbundle */
 
689
    HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
 
690
        if (!xport->xbundle) {
 
691
            xlate_xport_copy(new_xbridge, NULL, xport);
 
692
        }
 
693
    }
 
694
}
 
695
 
 
696
static void
 
697
xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
 
698
{
 
699
    struct xport *xport;
 
700
    struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
 
701
    new_xbundle->ofbundle = xbundle->ofbundle;
 
702
    new_xbundle->xbridge = xbridge;
 
703
    new_xbundle->name = xstrdup(xbundle->name);
 
704
    xlate_xbundle_init(new_xcfg, new_xbundle);
 
705
 
 
706
    xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
 
707
                      xbundle->vlan, xbundle->trunks,
 
708
                      xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
 
709
                      xbundle->floodable);
 
710
    LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
 
711
        xlate_xport_copy(xbridge, new_xbundle, xport);
 
712
    }
 
713
}
 
714
 
 
715
static void
 
716
xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
 
717
                 struct xport *xport)
 
718
{
 
719
    struct skb_priority_to_dscp *pdscp, *new_pdscp;
 
720
    struct xport *new_xport = xzalloc(sizeof *xport);
 
721
    new_xport->ofport = xport->ofport;
 
722
    new_xport->ofp_port = xport->ofp_port;
 
723
    new_xport->xbridge = xbridge;
 
724
    xlate_xport_init(new_xcfg, new_xport);
 
725
 
 
726
    xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
 
727
                    xport->bfd, xport->lldp, xport->stp_port_no,
 
728
                    xport->rstp_port, xport->config, xport->state,
 
729
                    xport->is_tunnel, xport->may_enable);
 
730
 
 
731
    if (xport->peer) {
 
732
        struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
 
733
        if (peer) {
 
734
            new_xport->peer = peer;
 
735
            new_xport->peer->peer = new_xport;
 
736
        }
 
737
    }
 
738
 
 
739
    if (xbundle) {
 
740
        new_xport->xbundle = xbundle;
 
741
        list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
 
742
    }
 
743
 
 
744
    HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
 
745
        new_pdscp = xmalloc(sizeof *pdscp);
 
746
        new_pdscp->skb_priority = pdscp->skb_priority;
 
747
        new_pdscp->dscp = pdscp->dscp;
 
748
        hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
 
749
                    hash_int(new_pdscp->skb_priority, 0));
 
750
    }
 
751
}
 
752
 
 
753
/* Sets the current xlate configuration to new_xcfg and frees the old xlate
 
754
 * configuration in xcfgp.
 
755
 *
 
756
 * This needs to be called after editing the xlate configuration.
 
757
 *
 
758
 * Functions that edit the new xlate configuration are
 
759
 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
 
760
 *
 
761
 * A sample workflow:
 
762
 *
 
763
 * xlate_txn_start();
 
764
 * ...
 
765
 * edit_xlate_configuration();
 
766
 * ...
 
767
 * xlate_txn_commit(); */
 
768
void
 
769
xlate_txn_commit(void)
 
770
{
 
771
    struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
772
 
 
773
    ovsrcu_set(&xcfgp, new_xcfg);
 
774
    ovsrcu_synchronize();
 
775
    xlate_xcfg_free(xcfg);
 
776
    new_xcfg = NULL;
 
777
}
 
778
 
 
779
/* Copies the current xlate configuration in xcfgp to new_xcfg.
 
780
 *
 
781
 * This needs to be called prior to editing the xlate configuration. */
 
782
void
 
783
xlate_txn_start(void)
 
784
{
 
785
    struct xbridge *xbridge;
 
786
    struct xlate_cfg *xcfg;
 
787
 
 
788
    ovs_assert(!new_xcfg);
 
789
 
 
790
    new_xcfg = xmalloc(sizeof *new_xcfg);
 
791
    hmap_init(&new_xcfg->xbridges);
 
792
    hmap_init(&new_xcfg->xbundles);
 
793
    hmap_init(&new_xcfg->xports);
 
794
 
 
795
    xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
796
    if (!xcfg) {
 
797
        return;
 
798
    }
 
799
 
 
800
    HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
 
801
        xlate_xbridge_copy(xbridge);
 
802
    }
 
803
}
 
804
 
 
805
 
 
806
static void
 
807
xlate_xcfg_free(struct xlate_cfg *xcfg)
 
808
{
 
809
    struct xbridge *xbridge, *next_xbridge;
 
810
 
 
811
    if (!xcfg) {
 
812
        return;
 
813
    }
 
814
 
 
815
    HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
 
816
        xlate_xbridge_remove(xcfg, xbridge);
 
817
    }
 
818
 
 
819
    hmap_destroy(&xcfg->xbridges);
 
820
    hmap_destroy(&xcfg->xbundles);
 
821
    hmap_destroy(&xcfg->xports);
 
822
    free(xcfg);
 
823
}
 
824
 
 
825
void
 
826
xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
 
827
                  struct dpif *dpif,
 
828
                  const struct mac_learning *ml, struct stp *stp,
 
829
                  struct rstp *rstp, const struct mcast_snooping *ms,
 
830
                  const struct mbridge *mbridge,
 
831
                  const struct dpif_sflow *sflow,
 
832
                  const struct dpif_ipfix *ipfix,
 
833
                  const struct netflow *netflow,
 
834
                  bool forward_bpdu, bool has_in_band,
 
835
                  const struct dpif_backer_support *support)
 
836
{
 
837
    struct xbridge *xbridge;
 
838
 
 
839
    ovs_assert(new_xcfg);
 
840
 
 
841
    xbridge = xbridge_lookup(new_xcfg, ofproto);
 
842
    if (!xbridge) {
 
843
        xbridge = xzalloc(sizeof *xbridge);
 
844
        xbridge->ofproto = ofproto;
 
845
 
 
846
        xlate_xbridge_init(new_xcfg, xbridge);
 
847
    }
 
848
 
 
849
    free(xbridge->name);
 
850
    xbridge->name = xstrdup(name);
 
851
 
 
852
    xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
 
853
                      netflow, forward_bpdu, has_in_band, support);
 
854
}
 
855
 
 
856
static void
 
857
xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
 
858
{
403
859
    struct xbundle *xbundle, *next_xbundle;
404
860
    struct xport *xport, *next_xport;
405
861
 
408
864
    }
409
865
 
410
866
    HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
411
 
        xlate_ofport_remove(xport->ofport);
 
867
        xlate_xport_remove(xcfg, xport);
412
868
    }
413
869
 
414
870
    LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
415
 
        xlate_bundle_remove(xbundle->ofbundle);
 
871
        xlate_xbundle_remove(xcfg, xbundle);
416
872
    }
417
873
 
418
 
    hmap_remove(&xbridges, &xbridge->hmap_node);
 
874
    hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
419
875
    mac_learning_unref(xbridge->ml);
 
876
    mcast_snooping_unref(xbridge->ms);
420
877
    mbridge_unref(xbridge->mbridge);
421
878
    dpif_sflow_unref(xbridge->sflow);
422
879
    dpif_ipfix_unref(xbridge->ipfix);
423
880
    stp_unref(xbridge->stp);
 
881
    rstp_unref(xbridge->rstp);
424
882
    hmap_destroy(&xbridge->xports);
425
883
    free(xbridge->name);
426
884
    free(xbridge);
427
885
}
428
886
 
429
887
void
 
888
xlate_remove_ofproto(struct ofproto_dpif *ofproto)
 
889
{
 
890
    struct xbridge *xbridge;
 
891
 
 
892
    ovs_assert(new_xcfg);
 
893
 
 
894
    xbridge = xbridge_lookup(new_xcfg, ofproto);
 
895
    xlate_xbridge_remove(new_xcfg, xbridge);
 
896
}
 
897
 
 
898
void
430
899
xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
431
900
                 const char *name, enum port_vlan_mode vlan_mode, int vlan,
432
901
                 unsigned long *trunks, bool use_priority_tags,
433
902
                 const struct bond *bond, const struct lacp *lacp,
434
903
                 bool floodable)
435
904
{
436
 
    struct xbundle *xbundle = xbundle_lookup(ofbundle);
437
 
 
 
905
    struct xbundle *xbundle;
 
906
 
 
907
    ovs_assert(new_xcfg);
 
908
 
 
909
    xbundle = xbundle_lookup(new_xcfg, ofbundle);
438
910
    if (!xbundle) {
439
911
        xbundle = xzalloc(sizeof *xbundle);
440
912
        xbundle->ofbundle = ofbundle;
441
 
        xbundle->xbridge = xbridge_lookup(ofproto);
 
913
        xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
442
914
 
443
 
        hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
444
 
        list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
445
 
        list_init(&xbundle->xports);
 
915
        xlate_xbundle_init(new_xcfg, xbundle);
446
916
    }
447
917
 
448
 
    ovs_assert(xbundle->xbridge);
449
 
 
450
918
    free(xbundle->name);
451
919
    xbundle->name = xstrdup(name);
452
920
 
453
 
    xbundle->vlan_mode = vlan_mode;
454
 
    xbundle->vlan = vlan;
455
 
    xbundle->trunks = trunks;
456
 
    xbundle->use_priority_tags = use_priority_tags;
457
 
    xbundle->floodable = floodable;
458
 
 
459
 
    if (xbundle->bond != bond) {
460
 
        bond_unref(xbundle->bond);
461
 
        xbundle->bond = bond_ref(bond);
462
 
    }
463
 
 
464
 
    if (xbundle->lacp != lacp) {
465
 
        lacp_unref(xbundle->lacp);
466
 
        xbundle->lacp = lacp_ref(lacp);
467
 
    }
 
921
    xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
 
922
                      use_priority_tags, bond, lacp, floodable);
468
923
}
469
924
 
470
 
void
471
 
xlate_bundle_remove(struct ofbundle *ofbundle)
 
925
static void
 
926
xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
472
927
{
473
 
    struct xbundle *xbundle = xbundle_lookup(ofbundle);
474
 
    struct xport *xport, *next;
 
928
    struct xport *xport;
475
929
 
476
930
    if (!xbundle) {
477
931
        return;
478
932
    }
479
933
 
480
 
    LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
481
 
        list_remove(&xport->bundle_node);
 
934
    LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
482
935
        xport->xbundle = NULL;
483
936
    }
484
937
 
485
 
    hmap_remove(&xbundles, &xbundle->hmap_node);
 
938
    hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
486
939
    list_remove(&xbundle->list_node);
487
940
    bond_unref(xbundle->bond);
488
941
    lacp_unref(xbundle->lacp);
491
944
}
492
945
 
493
946
void
 
947
xlate_bundle_remove(struct ofbundle *ofbundle)
 
948
{
 
949
    struct xbundle *xbundle;
 
950
 
 
951
    ovs_assert(new_xcfg);
 
952
 
 
953
    xbundle = xbundle_lookup(new_xcfg, ofbundle);
 
954
    xlate_xbundle_remove(new_xcfg, xbundle);
 
955
}
 
956
 
 
957
void
494
958
xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
495
959
                 struct ofport_dpif *ofport, ofp_port_t ofp_port,
496
960
                 odp_port_t odp_port, const struct netdev *netdev,
497
961
                 const struct cfm *cfm, const struct bfd *bfd,
498
 
                 struct ofport_dpif *peer, int stp_port_no,
 
962
                 const struct lldp *lldp, struct ofport_dpif *peer,
 
963
                 int stp_port_no, const struct rstp_port *rstp_port,
499
964
                 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
500
965
                 enum ofputil_port_config config,
501
966
                 enum ofputil_port_state state, bool is_tunnel,
502
967
                 bool may_enable)
503
968
{
504
 
    struct xport *xport = xport_lookup(ofport);
505
969
    size_t i;
506
 
 
 
970
    struct xport *xport;
 
971
 
 
972
    ovs_assert(new_xcfg);
 
973
 
 
974
    xport = xport_lookup(new_xcfg, ofport);
507
975
    if (!xport) {
508
976
        xport = xzalloc(sizeof *xport);
509
977
        xport->ofport = ofport;
510
 
        xport->xbridge = xbridge_lookup(ofproto);
 
978
        xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
511
979
        xport->ofp_port = ofp_port;
512
980
 
513
 
        hmap_init(&xport->skb_priorities);
514
 
        hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
515
 
        hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
516
 
                    hash_ofp_port(xport->ofp_port));
 
981
        xlate_xport_init(new_xcfg, xport);
517
982
    }
518
983
 
519
984
    ovs_assert(xport->ofp_port == ofp_port);
520
985
 
521
 
    xport->config = config;
522
 
    xport->state = state;
523
 
    xport->stp_port_no = stp_port_no;
524
 
    xport->is_tunnel = is_tunnel;
525
 
    xport->may_enable = may_enable;
526
 
    xport->odp_port = odp_port;
527
 
 
528
 
    if (xport->netdev != netdev) {
529
 
        netdev_close(xport->netdev);
530
 
        xport->netdev = netdev_ref(netdev);
531
 
    }
532
 
 
533
 
    if (xport->cfm != cfm) {
534
 
        cfm_unref(xport->cfm);
535
 
        xport->cfm = cfm_ref(cfm);
536
 
    }
537
 
 
538
 
    if (xport->bfd != bfd) {
539
 
        bfd_unref(xport->bfd);
540
 
        xport->bfd = bfd_ref(bfd);
541
 
    }
 
986
    xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
 
987
                    stp_port_no, rstp_port, config, state, is_tunnel,
 
988
                    may_enable);
542
989
 
543
990
    if (xport->peer) {
544
991
        xport->peer->peer = NULL;
545
992
    }
546
 
    xport->peer = xport_lookup(peer);
 
993
    xport->peer = xport_lookup(new_xcfg, peer);
547
994
    if (xport->peer) {
548
995
        xport->peer->peer = xport;
549
996
    }
551
998
    if (xport->xbundle) {
552
999
        list_remove(&xport->bundle_node);
553
1000
    }
554
 
    xport->xbundle = xbundle_lookup(ofbundle);
 
1001
    xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
555
1002
    if (xport->xbundle) {
556
1003
        list_insert(&xport->xbundle->xports, &xport->bundle_node);
557
1004
    }
574
1021
    }
575
1022
}
576
1023
 
577
 
void
578
 
xlate_ofport_remove(struct ofport_dpif *ofport)
 
1024
static void
 
1025
xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
579
1026
{
580
 
    struct xport *xport = xport_lookup(ofport);
581
 
 
582
1027
    if (!xport) {
583
1028
        return;
584
1029
    }
595
1040
    clear_skb_priorities(xport);
596
1041
    hmap_destroy(&xport->skb_priorities);
597
1042
 
598
 
    hmap_remove(&xports, &xport->hmap_node);
 
1043
    hmap_remove(&xcfg->xports, &xport->hmap_node);
599
1044
    hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
600
1045
 
601
1046
    netdev_close(xport->netdev);
 
1047
    rstp_port_unref(xport->rstp_port);
602
1048
    cfm_unref(xport->cfm);
603
1049
    bfd_unref(xport->bfd);
 
1050
    lldp_unref(xport->lldp);
604
1051
    free(xport);
605
1052
}
606
1053
 
607
 
/* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
608
 
 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
609
 
 * Optionally populates 'ofproto' with the ofproto_dpif, 'odp_in_port' with
610
 
 * the datapath in_port, that 'packet' ingressed, and 'ipfix', 'sflow', and
611
 
 * 'netflow' with the appropriate handles for those protocols if they're
612
 
 * enabled.  Caller is responsible for unrefing them.
613
 
 *
614
 
 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist.  Otherwise sets
615
 
 * 'flow''s in_port to OFPP_NONE.
616
 
 *
617
 
 * This function does post-processing on data returned from
618
 
 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
619
 
 * of the upcall processing logic.  In particular, if the extracted in_port is
620
 
 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
621
 
 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
622
 
 * a VLAN header onto 'packet' (if it is nonnull).
623
 
 *
624
 
 * Similarly, this function also includes some logic to help with tunnels.  It
625
 
 * may modify 'flow' as necessary to make the tunneling implementation
626
 
 * transparent to the upcall processing logic.
627
 
 *
628
 
 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
629
 
 * or some other positive errno if there are other problems. */
630
 
int
631
 
xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
632
 
              const struct nlattr *key, size_t key_len, struct flow *flow,
633
 
              struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
634
 
              struct dpif_sflow **sflow, struct netflow **netflow,
635
 
              odp_port_t *odp_in_port)
636
 
{
637
 
    struct ofproto_dpif *recv_ofproto = NULL;
638
 
    struct ofproto_dpif *recirc_ofproto = NULL;
 
1054
void
 
1055
xlate_ofport_remove(struct ofport_dpif *ofport)
 
1056
{
 
1057
    struct xport *xport;
 
1058
 
 
1059
    ovs_assert(new_xcfg);
 
1060
 
 
1061
    xport = xport_lookup(new_xcfg, ofport);
 
1062
    xlate_xport_remove(new_xcfg, xport);
 
1063
}
 
1064
 
 
1065
static struct ofproto_dpif *
 
1066
xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
 
1067
                      ofp_port_t *ofp_in_port, const struct xport **xportp)
 
1068
{
 
1069
    struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
639
1070
    const struct xport *xport;
640
 
    int error = ENODEV;
641
 
 
642
 
    fat_rwlock_rdlock(&xlate_rwlock);
643
 
    if (odp_flow_key_to_flow(key, key_len, flow) == ODP_FIT_ERROR) {
644
 
        error = EINVAL;
645
 
        goto exit;
646
 
    }
647
 
 
648
 
    if (odp_in_port) {
649
 
        *odp_in_port = flow->in_port.odp_port;
650
 
    }
651
 
 
652
 
    xport = xport_lookup(tnl_port_should_receive(flow)
 
1071
 
 
1072
    xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
653
1073
                         ? tnl_port_receive(flow)
654
1074
                         : odp_port_to_ofport(backer, flow->in_port.odp_port));
655
 
 
656
 
    flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
657
 
    if (!xport) {
658
 
        goto exit;
659
 
    }
660
 
    recv_ofproto = xport->xbridge->ofproto;
661
 
 
662
 
    if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
663
 
        if (packet) {
664
 
            /* Make the packet resemble the flow, so that it gets sent to
665
 
             * an OpenFlow controller properly, so that it looks correct
666
 
             * for sFlow, and so that flow_extract() will get the correct
667
 
             * vlan_tci if it is called on 'packet'. */
668
 
            eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
669
 
        }
670
 
    }
671
 
    error = 0;
672
 
 
673
 
    /* When recirc_id is set in 'flow', checks whether the ofproto_dpif that
674
 
     * corresponds to the recirc_id is same as the receiving bridge.  If they
675
 
     * are the same, uses the 'recv_ofproto' and keeps the 'ofp_in_port' as
676
 
     * assigned.  Otherwise, uses the 'recirc_ofproto' that owns recirc_id and
677
 
     * assigns OFPP_NONE to 'ofp_in_port'.  Doing this is in that, the
678
 
     * recirculated flow must be processced by the ofproto which originates
679
 
     * the recirculation, and as bridges can only see their own ports, the
680
 
     * in_port of the 'recv_ofproto' should not be passed to the
681
 
     * 'recirc_ofproto'.
682
 
     *
683
 
     * Admittedly, setting the 'ofp_in_port' to OFPP_NONE limits the
684
 
     * 'recirc_ofproto' from meaningfully matching on in_port of recirculated
685
 
     * flow, and should be fixed in the near future.
686
 
     *
687
 
     * TODO: Restore the original patch port.
688
 
     */
689
 
    if (flow->recirc_id) {
690
 
        recirc_ofproto = ofproto_dpif_recirc_get_ofproto(backer,
691
 
                                                         flow->recirc_id);
692
 
        /* Returns error if could not find recirculation bridge */
693
 
        if (!recirc_ofproto) {
694
 
            error = ENOENT;
695
 
            goto exit;
696
 
        }
697
 
 
698
 
        if (recv_ofproto != recirc_ofproto) {
699
 
            xport = NULL;
700
 
            flow->in_port.ofp_port = OFPP_NONE;
701
 
            if (odp_in_port) {
702
 
                *odp_in_port = ODPP_NONE;
703
 
            }
704
 
        }
705
 
    }
706
 
 
707
 
    if (ofproto) {
708
 
        *ofproto = xport ? recv_ofproto : recirc_ofproto;
 
1075
    if (OVS_UNLIKELY(!xport)) {
 
1076
        return NULL;
 
1077
    }
 
1078
    *xportp = xport;
 
1079
    if (ofp_in_port) {
 
1080
        *ofp_in_port = xport->ofp_port;
 
1081
    }
 
1082
    return xport->xbridge->ofproto;
 
1083
}
 
1084
 
 
1085
/* Given a datapath and flow metadata ('backer', and 'flow' respectively)
 
1086
 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
 
1087
struct ofproto_dpif *
 
1088
xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
 
1089
                     ofp_port_t *ofp_in_port)
 
1090
{
 
1091
    const struct xport *xport;
 
1092
 
 
1093
    return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
 
1094
}
 
1095
 
 
1096
/* Given a datapath and flow metadata ('backer', and 'flow' respectively),
 
1097
 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
 
1098
 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
 
1099
 * handles for those protocols if they're enabled.  Caller may use the returned
 
1100
 * pointers until quiescing, for longer term use additional references must
 
1101
 * be taken.
 
1102
 *
 
1103
 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
 
1104
 */
 
1105
int
 
1106
xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
 
1107
             struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
 
1108
             struct dpif_sflow **sflow, struct netflow **netflow,
 
1109
             ofp_port_t *ofp_in_port)
 
1110
{
 
1111
    struct ofproto_dpif *ofproto;
 
1112
    const struct xport *xport;
 
1113
 
 
1114
    ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
 
1115
 
 
1116
    if (!ofproto) {
 
1117
        return ENODEV;
 
1118
    }
 
1119
 
 
1120
    if (ofprotop) {
 
1121
        *ofprotop = ofproto;
709
1122
    }
710
1123
 
711
1124
    if (ipfix) {
712
 
        *ipfix = xport ? dpif_ipfix_ref(xport->xbridge->ipfix) : NULL;
 
1125
        *ipfix = xport ? xport->xbridge->ipfix : NULL;
713
1126
    }
714
1127
 
715
1128
    if (sflow) {
716
 
        *sflow = xport ? dpif_sflow_ref(xport->xbridge->sflow) : NULL;
 
1129
        *sflow = xport ? xport->xbridge->sflow : NULL;
717
1130
    }
718
1131
 
719
1132
    if (netflow) {
720
 
        *netflow = xport ? netflow_ref(xport->xbridge->netflow) : NULL;
 
1133
        *netflow = xport ? xport->xbridge->netflow : NULL;
721
1134
    }
722
1135
 
723
 
exit:
724
 
    fat_rwlock_unlock(&xlate_rwlock);
725
 
    return error;
 
1136
    return 0;
726
1137
}
727
1138
 
728
1139
static struct xbridge *
729
 
xbridge_lookup(const struct ofproto_dpif *ofproto)
 
1140
xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
730
1141
{
 
1142
    struct hmap *xbridges;
731
1143
    struct xbridge *xbridge;
732
1144
 
733
 
    if (!ofproto) {
 
1145
    if (!ofproto || !xcfg) {
734
1146
        return NULL;
735
1147
    }
736
1148
 
 
1149
    xbridges = &xcfg->xbridges;
 
1150
 
737
1151
    HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
738
 
                             &xbridges) {
 
1152
                             xbridges) {
739
1153
        if (xbridge->ofproto == ofproto) {
740
1154
            return xbridge;
741
1155
        }
744
1158
}
745
1159
 
746
1160
static struct xbundle *
747
 
xbundle_lookup(const struct ofbundle *ofbundle)
 
1161
xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
748
1162
{
 
1163
    struct hmap *xbundles;
749
1164
    struct xbundle *xbundle;
750
1165
 
751
 
    if (!ofbundle) {
 
1166
    if (!ofbundle || !xcfg) {
752
1167
        return NULL;
753
1168
    }
754
1169
 
 
1170
    xbundles = &xcfg->xbundles;
 
1171
 
755
1172
    HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
756
 
                             &xbundles) {
 
1173
                             xbundles) {
757
1174
        if (xbundle->ofbundle == ofbundle) {
758
1175
            return xbundle;
759
1176
        }
762
1179
}
763
1180
 
764
1181
static struct xport *
765
 
xport_lookup(const struct ofport_dpif *ofport)
 
1182
xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
766
1183
{
 
1184
    struct hmap *xports;
767
1185
    struct xport *xport;
768
1186
 
769
 
    if (!ofport) {
 
1187
    if (!ofport || !xcfg) {
770
1188
        return NULL;
771
1189
    }
772
1190
 
 
1191
    xports = &xcfg->xports;
 
1192
 
773
1193
    HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
774
 
                             &xports) {
 
1194
                             xports) {
775
1195
        if (xport->ofport == ofport) {
776
1196
            return xport;
777
1197
        }
791
1211
xport_stp_learn_state(const struct xport *xport)
792
1212
{
793
1213
    struct stp_port *sp = xport_get_stp_port(xport);
794
 
    return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
 
1214
    return sp
 
1215
        ? stp_learn_in_state(stp_port_get_state(sp))
 
1216
        : true;
795
1217
}
796
1218
 
797
1219
static bool
798
1220
xport_stp_forward_state(const struct xport *xport)
799
1221
{
800
1222
    struct stp_port *sp = xport_get_stp_port(xport);
801
 
    return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
 
1223
    return sp
 
1224
        ? stp_forward_in_state(stp_port_get_state(sp))
 
1225
        : true;
802
1226
}
803
1227
 
804
1228
static bool
819
1243
}
820
1244
 
821
1245
static void
822
 
stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
 
1246
stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
823
1247
{
824
1248
    struct stp_port *sp = xport_get_stp_port(xport);
825
 
    struct ofpbuf payload = *packet;
826
 
    struct eth_header *eth = ofpbuf_data(&payload);
 
1249
    struct dp_packet payload = *packet;
 
1250
    struct eth_header *eth = dp_packet_data(&payload);
827
1251
 
828
1252
    /* Sink packets on ports that have STP disabled when the bridge has
829
1253
     * STP enabled. */
832
1256
    }
833
1257
 
834
1258
    /* Trim off padding on payload. */
835
 
    if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
836
 
        ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
837
 
    }
838
 
 
839
 
    if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
840
 
        stp_received_bpdu(sp, ofpbuf_data(&payload), ofpbuf_size(&payload));
 
1259
    if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
 
1260
        dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
 
1261
    }
 
1262
 
 
1263
    if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
 
1264
        stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
 
1265
    }
 
1266
}
 
1267
 
 
1268
static enum rstp_state
 
1269
xport_get_rstp_port_state(const struct xport *xport)
 
1270
{
 
1271
    return xport->rstp_port
 
1272
        ? rstp_port_get_state(xport->rstp_port)
 
1273
        : RSTP_DISABLED;
 
1274
}
 
1275
 
 
1276
static bool
 
1277
xport_rstp_learn_state(const struct xport *xport)
 
1278
{
 
1279
    return xport->xbridge->rstp && xport->rstp_port
 
1280
        ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
 
1281
        : true;
 
1282
}
 
1283
 
 
1284
static bool
 
1285
xport_rstp_forward_state(const struct xport *xport)
 
1286
{
 
1287
    return xport->xbridge->rstp && xport->rstp_port
 
1288
        ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
 
1289
        : true;
 
1290
}
 
1291
 
 
1292
static bool
 
1293
xport_rstp_should_manage_bpdu(const struct xport *xport)
 
1294
{
 
1295
    return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
 
1296
}
 
1297
 
 
1298
static void
 
1299
rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
 
1300
{
 
1301
    struct dp_packet payload = *packet;
 
1302
    struct eth_header *eth = dp_packet_data(&payload);
 
1303
 
 
1304
    /* Sink packets on ports that have no RSTP. */
 
1305
    if (!xport->rstp_port) {
 
1306
        return;
 
1307
    }
 
1308
 
 
1309
    /* Trim off padding on payload. */
 
1310
    if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
 
1311
        dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
 
1312
    }
 
1313
 
 
1314
    if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
 
1315
        rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
 
1316
                                dp_packet_size(&payload));
841
1317
    }
842
1318
}
843
1319
 
865
1341
static bool
866
1342
odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
867
1343
{
868
 
    struct xport *xport;
869
 
 
870
 
    xport = get_ofp_port(ctx->xbridge, ofp_port);
871
 
    if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
872
 
        xport->state & OFPUTIL_PS_LINK_DOWN) {
873
 
        return false;
874
 
    }
875
 
 
876
 
    return true;
 
1344
    struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
 
1345
    return xport && xport->may_enable;
877
1346
}
878
1347
 
879
 
static const struct ofputil_bucket *
 
1348
static struct ofputil_bucket *
880
1349
group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
881
1350
                        int depth);
882
1351
 
884
1353
group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
885
1354
{
886
1355
    struct group_dpif *group;
887
 
    bool hit;
888
 
 
889
 
    hit = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
890
 
    if (!hit) {
891
 
        return false;
 
1356
 
 
1357
    if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
 
1358
        struct ofputil_bucket *bucket;
 
1359
 
 
1360
        bucket = group_first_live_bucket(ctx, group, depth);
 
1361
        group_dpif_unref(group);
 
1362
        return bucket == NULL;
892
1363
    }
893
1364
 
894
 
    hit = group_first_live_bucket(ctx, group, depth) != NULL;
895
 
 
896
 
    group_dpif_release(group);
897
 
    return hit;
 
1365
    return false;
898
1366
}
899
1367
 
900
1368
#define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
901
1369
 
902
1370
static bool
903
1371
bucket_is_alive(const struct xlate_ctx *ctx,
904
 
                const struct ofputil_bucket *bucket, int depth)
 
1372
                struct ofputil_bucket *bucket, int depth)
905
1373
{
906
1374
    if (depth >= MAX_LIVENESS_RECURSION) {
907
1375
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
911
1379
        return false;
912
1380
    }
913
1381
 
914
 
    return !ofputil_bucket_has_liveness(bucket) ||
915
 
        (bucket->watch_port != OFPP_ANY &&
916
 
         odp_port_is_alive(ctx, bucket->watch_port)) ||
917
 
        (bucket->watch_group != OFPG_ANY &&
918
 
         group_is_alive(ctx, bucket->watch_group, depth + 1));
 
1382
    return (!ofputil_bucket_has_liveness(bucket)
 
1383
            || (bucket->watch_port != OFPP_ANY
 
1384
               && odp_port_is_alive(ctx, bucket->watch_port))
 
1385
            || (bucket->watch_group != OFPG_ANY
 
1386
               && group_is_alive(ctx, bucket->watch_group, depth + 1)));
919
1387
}
920
1388
 
921
 
static const struct ofputil_bucket *
 
1389
static struct ofputil_bucket *
922
1390
group_first_live_bucket(const struct xlate_ctx *ctx,
923
1391
                        const struct group_dpif *group, int depth)
924
1392
{
925
1393
    struct ofputil_bucket *bucket;
926
 
    const struct list *buckets;
 
1394
    const struct ovs_list *buckets;
927
1395
 
928
1396
    group_dpif_get_buckets(group, &buckets);
929
1397
    LIST_FOR_EACH (bucket, list_node, buckets) {
935
1403
    return NULL;
936
1404
}
937
1405
 
938
 
static const struct ofputil_bucket *
 
1406
static struct ofputil_bucket *
939
1407
group_best_live_bucket(const struct xlate_ctx *ctx,
940
1408
                       const struct group_dpif *group,
941
1409
                       uint32_t basis)
942
1410
{
943
 
    const struct ofputil_bucket *best_bucket = NULL;
 
1411
    struct ofputil_bucket *best_bucket = NULL;
944
1412
    uint32_t best_score = 0;
945
1413
    int i = 0;
946
1414
 
947
 
    const struct ofputil_bucket *bucket;
948
 
    const struct list *buckets;
 
1415
    struct ofputil_bucket *bucket;
 
1416
    const struct ovs_list *buckets;
949
1417
 
950
1418
    group_dpif_get_buckets(group, &buckets);
951
1419
    LIST_FOR_EACH (bucket, list_node, buckets) {
1070
1538
                         "%s, which is reserved exclusively for mirroring",
1071
1539
                         ctx->xbridge->name, in_xbundle->name);
1072
1540
        }
1073
 
        ofpbuf_clear(&ctx->xout->odp_actions);
 
1541
        ofpbuf_clear(ctx->xout->odp_actions);
1074
1542
        return;
1075
1543
    }
1076
1544
 
1091
1559
    while (mirrors) {
1092
1560
        mirror_mask_t dup_mirrors;
1093
1561
        struct ofbundle *out;
1094
 
        unsigned long *vlans;
 
1562
        const unsigned long *vlans;
1095
1563
        bool vlan_mirrored;
1096
1564
        bool has_mirror;
1097
1565
        int out_vlan;
1104
1572
            ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1105
1573
        }
1106
1574
        vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1107
 
        free(vlans);
1108
1575
 
1109
1576
        if (!vlan_mirrored) {
1110
1577
            mirrors = zero_rightmost_1bit(mirrors);
1114
1581
        mirrors &= ~dup_mirrors;
1115
1582
        ctx->xout->mirrors |= dup_mirrors;
1116
1583
        if (out) {
1117
 
            struct xbundle *out_xbundle = xbundle_lookup(out);
 
1584
            struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
1585
            struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1118
1586
            if (out_xbundle) {
1119
1587
                output_normal(ctx, out_xbundle, vlan);
1120
1588
            }
1246
1714
    uint16_t vid;
1247
1715
    ovs_be16 tci, old_tci;
1248
1716
    struct xport *xport;
 
1717
    struct xlate_bond_recirc xr;
 
1718
    bool use_recirc = false;
1249
1719
 
1250
1720
    vid = output_vlan_to_vid(out_xbundle, vlan);
1251
1721
    if (list_is_empty(&out_xbundle->xports)) {
1252
1722
        /* Partially configured bundle with no slaves.  Drop the packet. */
1253
1723
        return;
1254
1724
    } else if (!out_xbundle->bond) {
1255
 
        ctx->use_recirc = false;
1256
1725
        xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1257
1726
                             bundle_node);
1258
1727
    } else {
 
1728
        struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
1729
        struct flow_wildcards *wc = &ctx->xout->wc;
1259
1730
        struct ofport_dpif *ofport;
1260
 
        struct xlate_recirc *xr = &ctx->recirc;
1261
 
        struct flow_wildcards *wc = &ctx->xout->wc;
1262
 
 
1263
 
        if (ctx->xbridge->enable_recirc) {
1264
 
            ctx->use_recirc = bond_may_recirc(
1265
 
                out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
1266
 
 
1267
 
            if (ctx->use_recirc) {
 
1731
 
 
1732
        if (ctx->xbridge->support.recirc) {
 
1733
            use_recirc = bond_may_recirc(
 
1734
                out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
 
1735
 
 
1736
            if (use_recirc) {
1268
1737
                /* Only TCP mode uses recirculation. */
1269
 
                xr->hash_alg = OVS_HASH_ALG_L4;
 
1738
                xr.hash_alg = OVS_HASH_ALG_L4;
1270
1739
                bond_update_post_recirc_rules(out_xbundle->bond, false);
1271
1740
 
1272
1741
                /* Recirculation does not require unmasking hash fields. */
1276
1745
 
1277
1746
        ofport = bond_choose_output_slave(out_xbundle->bond,
1278
1747
                                          &ctx->xin->flow, wc, vid);
1279
 
        xport = xport_lookup(ofport);
 
1748
        xport = xport_lookup(xcfg, ofport);
1280
1749
 
1281
1750
        if (!xport) {
1282
1751
            /* No slaves enabled, so drop packet. */
1283
1752
            return;
1284
1753
        }
1285
1754
 
1286
 
        /* If ctx->xout->use_recirc is set, the main thread will handle stats
 
1755
        /* If use_recirc is set, the main thread will handle stats
1287
1756
         * accounting for this bond. */
1288
 
        if (!ctx->use_recirc) {
 
1757
        if (!use_recirc) {
1289
1758
            if (ctx->xin->resubmit_stats) {
1290
1759
                bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1291
1760
                             ctx->xin->resubmit_stats->n_bytes);
1313
1782
    }
1314
1783
    *flow_tci = tci;
1315
1784
 
1316
 
    compose_output_action(ctx, xport->ofp_port);
 
1785
    compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
1317
1786
    *flow_tci = old_tci;
1318
1787
}
1319
1788
 
1345
1814
    }
1346
1815
}
1347
1816
 
 
1817
/* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
 
1818
 * dropped.  Returns true if they may be forwarded, false if they should be
 
1819
 * dropped.
 
1820
 *
 
1821
 * 'in_port' must be the xport that corresponds to flow->in_port.
 
1822
 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
 
1823
 *
 
1824
 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
 
1825
 * returned by input_vid_to_vlan().  It must be a valid VLAN for 'in_port', as
 
1826
 * checked by input_vid_is_valid().
 
1827
 *
 
1828
 * May also add tags to '*tags', although the current implementation only does
 
1829
 * so in one special case.
 
1830
 */
 
1831
static bool
 
1832
is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
 
1833
              uint16_t vlan)
 
1834
{
 
1835
    struct xbundle *in_xbundle = in_port->xbundle;
 
1836
    const struct xbridge *xbridge = ctx->xbridge;
 
1837
    struct flow *flow = &ctx->xin->flow;
 
1838
 
 
1839
    /* Drop frames for reserved multicast addresses
 
1840
     * only if forward_bpdu option is absent. */
 
1841
    if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
 
1842
        xlate_report(ctx, "packet has reserved destination MAC, dropping");
 
1843
        return false;
 
1844
    }
 
1845
 
 
1846
    if (in_xbundle->bond) {
 
1847
        struct mac_entry *mac;
 
1848
 
 
1849
        switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
 
1850
                                         flow->dl_dst)) {
 
1851
        case BV_ACCEPT:
 
1852
            break;
 
1853
 
 
1854
        case BV_DROP:
 
1855
            xlate_report(ctx, "bonding refused admissibility, dropping");
 
1856
            return false;
 
1857
 
 
1858
        case BV_DROP_IF_MOVED:
 
1859
            ovs_rwlock_rdlock(&xbridge->ml->rwlock);
 
1860
            mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
 
1861
            if (mac
 
1862
                && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
 
1863
                && (!is_gratuitous_arp(flow, &ctx->xout->wc)
 
1864
                    || mac_entry_is_grat_arp_locked(mac))) {
 
1865
                ovs_rwlock_unlock(&xbridge->ml->rwlock);
 
1866
                xlate_report(ctx, "SLB bond thinks this packet looped back, "
 
1867
                             "dropping");
 
1868
                return false;
 
1869
            }
 
1870
            ovs_rwlock_unlock(&xbridge->ml->rwlock);
 
1871
            break;
 
1872
        }
 
1873
    }
 
1874
 
 
1875
    return true;
 
1876
}
 
1877
 
1348
1878
/* Checks whether a MAC learning update is necessary for MAC learning table
1349
1879
 * 'ml' given that a packet matching 'flow' was received  on 'in_xbundle' in
1350
1880
 * 'vlan'.
1459
1989
    }
1460
1990
}
1461
1991
 
1462
 
/* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1463
 
 * dropped.  Returns true if they may be forwarded, false if they should be
1464
 
 * dropped.
1465
 
 *
1466
 
 * 'in_port' must be the xport that corresponds to flow->in_port.
1467
 
 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1468
 
 *
1469
 
 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1470
 
 * returned by input_vid_to_vlan().  It must be a valid VLAN for 'in_port', as
1471
 
 * checked by input_vid_is_valid().
1472
 
 *
1473
 
 * May also add tags to '*tags', although the current implementation only does
1474
 
 * so in one special case.
1475
 
 */
1476
 
static bool
1477
 
is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1478
 
              uint16_t vlan)
1479
 
{
1480
 
    struct xbundle *in_xbundle = in_port->xbundle;
1481
 
    const struct xbridge *xbridge = ctx->xbridge;
1482
 
    struct flow *flow = &ctx->xin->flow;
1483
 
 
1484
 
    /* Drop frames for reserved multicast addresses
1485
 
     * only if forward_bpdu option is absent. */
1486
 
    if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1487
 
        xlate_report(ctx, "packet has reserved destination MAC, dropping");
1488
 
        return false;
1489
 
    }
1490
 
 
1491
 
    if (in_xbundle->bond) {
1492
 
        struct mac_entry *mac;
1493
 
 
1494
 
        switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1495
 
                                         flow->dl_dst)) {
1496
 
        case BV_ACCEPT:
1497
 
            break;
1498
 
 
1499
 
        case BV_DROP:
1500
 
            xlate_report(ctx, "bonding refused admissibility, dropping");
1501
 
            return false;
1502
 
 
1503
 
        case BV_DROP_IF_MOVED:
1504
 
            ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1505
 
            mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1506
 
            if (mac
1507
 
                && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1508
 
                && (!is_gratuitous_arp(flow, &ctx->xout->wc)
1509
 
                    || mac_entry_is_grat_arp_locked(mac))) {
1510
 
                ovs_rwlock_unlock(&xbridge->ml->rwlock);
1511
 
                xlate_report(ctx, "SLB bond thinks this packet looped back, "
1512
 
                             "dropping");
1513
 
                return false;
1514
 
            }
1515
 
            ovs_rwlock_unlock(&xbridge->ml->rwlock);
1516
 
            break;
1517
 
        }
1518
 
    }
1519
 
 
1520
 
    return true;
 
1992
/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
 
1993
 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
 
1994
static void
 
1995
update_mcast_snooping_table__(const struct xbridge *xbridge,
 
1996
                              const struct flow *flow,
 
1997
                              struct mcast_snooping *ms,
 
1998
                              ovs_be32 ip4, int vlan,
 
1999
                              struct xbundle *in_xbundle,
 
2000
                              const struct dp_packet *packet)
 
2001
    OVS_REQ_WRLOCK(ms->rwlock)
 
2002
{
 
2003
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
 
2004
    int count;
 
2005
 
 
2006
    switch (ntohs(flow->tp_src)) {
 
2007
    case IGMP_HOST_MEMBERSHIP_REPORT:
 
2008
    case IGMPV2_HOST_MEMBERSHIP_REPORT:
 
2009
        if (mcast_snooping_add_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
 
2010
            VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
 
2011
                        IP_FMT" is on port %s in VLAN %d",
 
2012
                        xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
 
2013
        }
 
2014
        break;
 
2015
    case IGMP_HOST_LEAVE_MESSAGE:
 
2016
        if (mcast_snooping_leave_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
 
2017
            VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
 
2018
                        IP_FMT" is on port %s in VLAN %d",
 
2019
                        xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
 
2020
        }
 
2021
        break;
 
2022
    case IGMP_HOST_MEMBERSHIP_QUERY:
 
2023
        if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
 
2024
            in_xbundle->ofbundle)) {
 
2025
            VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
 
2026
                        IP_FMT" is on port %s in VLAN %d",
 
2027
                        xbridge->name, IP_ARGS(flow->nw_src),
 
2028
                        in_xbundle->name, vlan);
 
2029
        }
 
2030
        break;
 
2031
    case IGMPV3_HOST_MEMBERSHIP_REPORT:
 
2032
        if ((count = mcast_snooping_add_report(ms, packet, vlan,
 
2033
                                               in_xbundle->ofbundle))) {
 
2034
            VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
 
2035
                        "addresses on port %s in VLAN %d",
 
2036
                        xbridge->name, count, in_xbundle->name, vlan);
 
2037
        }
 
2038
        break;
 
2039
    }
 
2040
}
 
2041
 
 
2042
/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
 
2043
 * was received on 'in_xbundle' in 'vlan'. */
 
2044
static void
 
2045
update_mcast_snooping_table(const struct xbridge *xbridge,
 
2046
                            const struct flow *flow, int vlan,
 
2047
                            struct xbundle *in_xbundle,
 
2048
                            const struct dp_packet *packet)
 
2049
{
 
2050
    struct mcast_snooping *ms = xbridge->ms;
 
2051
    struct xlate_cfg *xcfg;
 
2052
    struct xbundle *mcast_xbundle;
 
2053
    struct mcast_port_bundle *fport;
 
2054
 
 
2055
    /* Don't learn the OFPP_NONE port. */
 
2056
    if (in_xbundle == &ofpp_none_bundle) {
 
2057
        return;
 
2058
    }
 
2059
 
 
2060
    /* Don't learn from flood ports */
 
2061
    mcast_xbundle = NULL;
 
2062
    ovs_rwlock_wrlock(&ms->rwlock);
 
2063
    xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
2064
    LIST_FOR_EACH(fport, node, &ms->fport_list) {
 
2065
        mcast_xbundle = xbundle_lookup(xcfg, fport->port);
 
2066
        if (mcast_xbundle == in_xbundle) {
 
2067
            break;
 
2068
        }
 
2069
    }
 
2070
 
 
2071
    if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
 
2072
        update_mcast_snooping_table__(xbridge, flow, ms, flow->igmp_group_ip4,
 
2073
                                      vlan, in_xbundle, packet);
 
2074
    }
 
2075
    ovs_rwlock_unlock(&ms->rwlock);
 
2076
}
 
2077
 
 
2078
/* send the packet to ports having the multicast group learned */
 
2079
static void
 
2080
xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
 
2081
                              struct mcast_snooping *ms OVS_UNUSED,
 
2082
                              struct mcast_group *grp,
 
2083
                              struct xbundle *in_xbundle, uint16_t vlan)
 
2084
    OVS_REQ_RDLOCK(ms->rwlock)
 
2085
{
 
2086
    struct xlate_cfg *xcfg;
 
2087
    struct mcast_group_bundle *b;
 
2088
    struct xbundle *mcast_xbundle;
 
2089
 
 
2090
    xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
2091
    LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
 
2092
        mcast_xbundle = xbundle_lookup(xcfg, b->port);
 
2093
        if (mcast_xbundle && mcast_xbundle != in_xbundle) {
 
2094
            xlate_report(ctx, "forwarding to mcast group port");
 
2095
            output_normal(ctx, mcast_xbundle, vlan);
 
2096
        } else if (!mcast_xbundle) {
 
2097
            xlate_report(ctx, "mcast group port is unknown, dropping");
 
2098
        } else {
 
2099
            xlate_report(ctx, "mcast group port is input port, dropping");
 
2100
        }
 
2101
    }
 
2102
}
 
2103
 
 
2104
/* send the packet to ports connected to multicast routers */
 
2105
static void
 
2106
xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
 
2107
                                 struct mcast_snooping *ms,
 
2108
                                 struct xbundle *in_xbundle, uint16_t vlan)
 
2109
    OVS_REQ_RDLOCK(ms->rwlock)
 
2110
{
 
2111
    struct xlate_cfg *xcfg;
 
2112
    struct mcast_mrouter_bundle *mrouter;
 
2113
    struct xbundle *mcast_xbundle;
 
2114
 
 
2115
    xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
2116
    LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
 
2117
        mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
 
2118
        if (mcast_xbundle && mcast_xbundle != in_xbundle) {
 
2119
            xlate_report(ctx, "forwarding to mcast router port");
 
2120
            output_normal(ctx, mcast_xbundle, vlan);
 
2121
        } else if (!mcast_xbundle) {
 
2122
            xlate_report(ctx, "mcast router port is unknown, dropping");
 
2123
        } else {
 
2124
            xlate_report(ctx, "mcast router port is input port, dropping");
 
2125
        }
 
2126
    }
 
2127
}
 
2128
 
 
2129
/* send the packet to ports flagged to be flooded */
 
2130
static void
 
2131
xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
 
2132
                               struct mcast_snooping *ms,
 
2133
                               struct xbundle *in_xbundle, uint16_t vlan)
 
2134
    OVS_REQ_RDLOCK(ms->rwlock)
 
2135
{
 
2136
    struct xlate_cfg *xcfg;
 
2137
    struct mcast_port_bundle *fport;
 
2138
    struct xbundle *mcast_xbundle;
 
2139
 
 
2140
    xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
2141
    LIST_FOR_EACH(fport, node, &ms->fport_list) {
 
2142
        mcast_xbundle = xbundle_lookup(xcfg, fport->port);
 
2143
        if (mcast_xbundle && mcast_xbundle != in_xbundle) {
 
2144
            xlate_report(ctx, "forwarding to mcast flood port");
 
2145
            output_normal(ctx, mcast_xbundle, vlan);
 
2146
        } else if (!mcast_xbundle) {
 
2147
            xlate_report(ctx, "mcast flood port is unknown, dropping");
 
2148
        } else {
 
2149
            xlate_report(ctx, "mcast flood port is input port, dropping");
 
2150
        }
 
2151
    }
 
2152
}
 
2153
 
 
2154
/* forward the Reports to configured ports */
 
2155
static void
 
2156
xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
 
2157
                               struct mcast_snooping *ms,
 
2158
                               struct xbundle *in_xbundle, uint16_t vlan)
 
2159
    OVS_REQ_RDLOCK(ms->rwlock)
 
2160
{
 
2161
    struct xlate_cfg *xcfg;
 
2162
    struct mcast_port_bundle *rport;
 
2163
    struct xbundle *mcast_xbundle;
 
2164
 
 
2165
    xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
2166
    LIST_FOR_EACH(rport, node, &ms->rport_list) {
 
2167
        mcast_xbundle = xbundle_lookup(xcfg, rport->port);
 
2168
        if (mcast_xbundle && mcast_xbundle != in_xbundle) {
 
2169
            xlate_report(ctx, "forwarding Report to mcast flagged port");
 
2170
            output_normal(ctx, mcast_xbundle, vlan);
 
2171
        } else if (!mcast_xbundle) {
 
2172
            xlate_report(ctx, "mcast port is unknown, dropping the Report");
 
2173
        } else {
 
2174
            xlate_report(ctx, "mcast port is input port, dropping the Report");
 
2175
        }
 
2176
    }
 
2177
}
 
2178
 
 
2179
static void
 
2180
xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
 
2181
                   uint16_t vlan)
 
2182
{
 
2183
    struct xbundle *xbundle;
 
2184
 
 
2185
    LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
 
2186
        if (xbundle != in_xbundle
 
2187
            && xbundle_includes_vlan(xbundle, vlan)
 
2188
            && xbundle->floodable
 
2189
            && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
 
2190
            output_normal(ctx, xbundle, vlan);
 
2191
        }
 
2192
    }
 
2193
    ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1521
2194
}
1522
2195
 
1523
2196
static void
1598
2271
    }
1599
2272
 
1600
2273
    /* Determine output bundle. */
1601
 
    ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1602
 
    mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1603
 
    mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
1604
 
    ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1605
 
 
1606
 
    if (mac_port) {
1607
 
        struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1608
 
        if (mac_xbundle && mac_xbundle != in_xbundle) {
1609
 
            xlate_report(ctx, "forwarding to learned port");
1610
 
            output_normal(ctx, mac_xbundle, vlan);
1611
 
        } else if (!mac_xbundle) {
1612
 
            xlate_report(ctx, "learned port is unknown, dropping");
1613
 
        } else {
1614
 
            xlate_report(ctx, "learned port is input port, dropping");
1615
 
        }
 
2274
    if (mcast_snooping_enabled(ctx->xbridge->ms)
 
2275
        && !eth_addr_is_broadcast(flow->dl_dst)
 
2276
        && eth_addr_is_multicast(flow->dl_dst)
 
2277
        && flow->dl_type == htons(ETH_TYPE_IP)) {
 
2278
        struct mcast_snooping *ms = ctx->xbridge->ms;
 
2279
        struct mcast_group *grp;
 
2280
 
 
2281
        if (flow->nw_proto == IPPROTO_IGMP) {
 
2282
            if (mcast_snooping_is_membership(flow->tp_src) ||
 
2283
                mcast_snooping_is_query(flow->tp_src)) {
 
2284
                if (ctx->xin->may_learn) {
 
2285
                    update_mcast_snooping_table(ctx->xbridge, flow, vlan,
 
2286
                                                in_xbundle, ctx->xin->packet);
 
2287
                }
 
2288
                /*
 
2289
                 * IGMP packets need to take the slow path, in order to be
 
2290
                 * processed for mdb updates. That will prevent expires
 
2291
                 * firing off even after hosts have sent reports.
 
2292
                 */
 
2293
                ctx->xout->slow |= SLOW_ACTION;
 
2294
            }
 
2295
 
 
2296
            if (mcast_snooping_is_membership(flow->tp_src)) {
 
2297
                ovs_rwlock_rdlock(&ms->rwlock);
 
2298
                xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
 
2299
                /* RFC4541: section 2.1.1, item 1: A snooping switch should
 
2300
                 * forward IGMP Membership Reports only to those ports where
 
2301
                 * multicast routers are attached.  Alternatively stated: a
 
2302
                 * snooping switch should not forward IGMP Membership Reports
 
2303
                 * to ports on which only hosts are attached.
 
2304
                 * An administrative control may be provided to override this
 
2305
                 * restriction, allowing the report messages to be flooded to
 
2306
                 * other ports. */
 
2307
                xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
 
2308
                ovs_rwlock_unlock(&ms->rwlock);
 
2309
            } else {
 
2310
                xlate_report(ctx, "multicast traffic, flooding");
 
2311
                xlate_normal_flood(ctx, in_xbundle, vlan);
 
2312
            }
 
2313
            return;
 
2314
        } else {
 
2315
            if (ip_is_local_multicast(flow->nw_dst)) {
 
2316
                /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
 
2317
                 * address in the 224.0.0.x range which are not IGMP must
 
2318
                 * be forwarded on all ports */
 
2319
                xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
 
2320
                xlate_normal_flood(ctx, in_xbundle, vlan);
 
2321
                return;
 
2322
            }
 
2323
        }
 
2324
 
 
2325
        /* forwarding to group base ports */
 
2326
        ovs_rwlock_rdlock(&ms->rwlock);
 
2327
        grp = mcast_snooping_lookup(ms, flow->nw_dst, vlan);
 
2328
        if (grp) {
 
2329
            xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
 
2330
            xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
 
2331
            xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
 
2332
        } else {
 
2333
            if (mcast_snooping_flood_unreg(ms)) {
 
2334
                xlate_report(ctx, "unregistered multicast, flooding");
 
2335
                xlate_normal_flood(ctx, in_xbundle, vlan);
 
2336
            } else {
 
2337
                xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
 
2338
                xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
 
2339
            }
 
2340
        }
 
2341
        ovs_rwlock_unlock(&ms->rwlock);
1616
2342
    } else {
1617
 
        struct xbundle *xbundle;
 
2343
        ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
 
2344
        mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
 
2345
        mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
 
2346
        ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1618
2347
 
1619
 
        xlate_report(ctx, "no learned MAC for destination, flooding");
1620
 
        LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1621
 
            if (xbundle != in_xbundle
1622
 
                && xbundle_includes_vlan(xbundle, vlan)
1623
 
                && xbundle->floodable
1624
 
                && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1625
 
                output_normal(ctx, xbundle, vlan);
 
2348
        if (mac_port) {
 
2349
            struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
2350
            struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
 
2351
            if (mac_xbundle && mac_xbundle != in_xbundle) {
 
2352
                xlate_report(ctx, "forwarding to learned port");
 
2353
                output_normal(ctx, mac_xbundle, vlan);
 
2354
            } else if (!mac_xbundle) {
 
2355
                xlate_report(ctx, "learned port is unknown, dropping");
 
2356
            } else {
 
2357
                xlate_report(ctx, "learned port is input port, dropping");
1626
2358
            }
 
2359
        } else {
 
2360
            xlate_report(ctx, "no learned MAC for destination, flooding");
 
2361
            xlate_normal_flood(ctx, in_xbundle, vlan);
1627
2362
        }
1628
 
        ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1629
2363
    }
1630
2364
}
1631
2365
 
1639
2373
                      const struct flow *flow,
1640
2374
                      const uint32_t probability,
1641
2375
                      const union user_action_cookie *cookie,
1642
 
                      const size_t cookie_size)
 
2376
                      const size_t cookie_size,
 
2377
                      const odp_port_t tunnel_out_port)
1643
2378
{
1644
2379
    size_t sample_offset, actions_offset;
1645
2380
    odp_port_t odp_port;
1656
2391
    pid = dpif_port_get_pid(xbridge->dpif, odp_port,
1657
2392
                            flow_hash_5tuple(flow, 0));
1658
2393
    cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
1659
 
                                             odp_actions);
 
2394
                                             tunnel_out_port, odp_actions);
1660
2395
 
1661
2396
    nl_msg_end_nested(odp_actions, actions_offset);
1662
2397
    nl_msg_end_nested(odp_actions, sample_offset);
1714
2449
                         odp_port == ODPP_NONE ? 0 : 1, &cookie);
1715
2450
 
1716
2451
    return compose_sample_action(xbridge, odp_actions, flow,  probability,
1717
 
                                 &cookie, sizeof cookie.sflow);
 
2452
                                 &cookie, sizeof cookie.sflow, ODPP_NONE);
1718
2453
}
1719
2454
 
1720
2455
static void
1730
2465
}
1731
2466
 
1732
2467
static void
1733
 
compose_ipfix_cookie(union user_action_cookie *cookie)
 
2468
compose_ipfix_cookie(union user_action_cookie *cookie,
 
2469
                     odp_port_t output_odp_port)
1734
2470
{
1735
2471
    cookie->type = USER_ACTION_COOKIE_IPFIX;
 
2472
    cookie->ipfix.output_odp_port = output_odp_port;
1736
2473
}
1737
2474
 
1738
2475
/* Compose SAMPLE action for IPFIX bridge sampling. */
1739
2476
static void
1740
2477
compose_ipfix_action(const struct xbridge *xbridge,
1741
2478
                     struct ofpbuf *odp_actions,
1742
 
                     const struct flow *flow)
 
2479
                     const struct flow *flow,
 
2480
                     odp_port_t output_odp_port)
1743
2481
{
1744
2482
    uint32_t probability;
1745
2483
    union user_action_cookie cookie;
 
2484
    odp_port_t tunnel_out_port = ODPP_NONE;
1746
2485
 
1747
2486
    if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1748
2487
        return;
1749
2488
    }
1750
2489
 
 
2490
    /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
 
2491
     * number. */
 
2492
    if (output_odp_port == ODPP_NONE &&
 
2493
        !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
 
2494
        return;
 
2495
    }
 
2496
 
 
2497
    /* For output case, output_odp_port is valid*/
 
2498
    if (output_odp_port != ODPP_NONE) {
 
2499
        if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
 
2500
            return;
 
2501
        }
 
2502
        /* If tunnel sampling is enabled, put an additional option attribute:
 
2503
         * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
 
2504
         */
 
2505
        if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
 
2506
            dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
 
2507
           tunnel_out_port = output_odp_port;
 
2508
        }
 
2509
    }
 
2510
 
1751
2511
    probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1752
 
    compose_ipfix_cookie(&cookie);
 
2512
    compose_ipfix_cookie(&cookie, output_odp_port);
1753
2513
 
1754
2514
    compose_sample_action(xbridge, odp_actions, flow,  probability,
1755
 
                          &cookie, sizeof cookie.ipfix);
 
2515
                          &cookie, sizeof cookie.ipfix, tunnel_out_port);
1756
2516
}
1757
2517
 
1758
2518
/* SAMPLE action for sFlow must be first action in any given list of
1762
2522
add_sflow_action(struct xlate_ctx *ctx)
1763
2523
{
1764
2524
    ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1765
 
                                                   &ctx->xout->odp_actions,
 
2525
                                                   ctx->xout->odp_actions,
1766
2526
                                                   &ctx->xin->flow, ODPP_NONE);
1767
2527
    ctx->sflow_odp_port = 0;
1768
2528
    ctx->sflow_n_outputs = 0;
1773
2533
static void
1774
2534
add_ipfix_action(struct xlate_ctx *ctx)
1775
2535
{
1776
 
    compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1777
 
                         &ctx->xin->flow);
 
2536
    compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
 
2537
                         &ctx->xin->flow, ODPP_NONE);
 
2538
}
 
2539
 
 
2540
static void
 
2541
add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
 
2542
{
 
2543
    compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
 
2544
                         &ctx->xin->flow, port);
1778
2545
}
1779
2546
 
1780
2547
/* Fix SAMPLE action according to data collected while composing ODP actions.
1790
2557
        return;
1791
2558
    }
1792
2559
 
1793
 
    cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
 
2560
    cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
1794
2561
                       sizeof cookie->sflow);
1795
2562
    ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1796
2563
 
1800
2567
 
1801
2568
static enum slow_path_reason
1802
2569
process_special(struct xlate_ctx *ctx, const struct flow *flow,
1803
 
                const struct xport *xport, const struct ofpbuf *packet)
 
2570
                const struct xport *xport, const struct dp_packet *packet)
1804
2571
{
1805
2572
    struct flow_wildcards *wc = &ctx->xout->wc;
1806
2573
    const struct xbridge *xbridge = ctx->xbridge;
1827
2594
            lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1828
2595
        }
1829
2596
        return SLOW_LACP;
1830
 
    } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
 
2597
    } else if ((xbridge->stp || xbridge->rstp) &&
 
2598
               stp_should_process_flow(flow, wc)) {
1831
2599
        if (packet) {
1832
 
            stp_process_packet(xport, packet);
 
2600
            xbridge->stp
 
2601
                ? stp_process_packet(xport, packet)
 
2602
                : rstp_process_packet(xport, packet);
1833
2603
        }
1834
2604
        return SLOW_STP;
 
2605
    } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
 
2606
        if (packet) {
 
2607
            lldp_process_packet(xport->lldp, packet);
 
2608
        }
 
2609
        return SLOW_LLDP;
1835
2610
    } else {
1836
2611
        return 0;
1837
2612
    }
1838
2613
}
1839
2614
 
 
2615
static int
 
2616
tnl_route_lookup_flow(const struct flow *oflow,
 
2617
                      ovs_be32 *ip, struct xport **out_port)
 
2618
{
 
2619
    char out_dev[IFNAMSIZ];
 
2620
    struct xbridge *xbridge;
 
2621
    struct xlate_cfg *xcfg;
 
2622
    ovs_be32 gw;
 
2623
 
 
2624
    if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
 
2625
        return -ENOENT;
 
2626
    }
 
2627
 
 
2628
    if (gw) {
 
2629
        *ip = gw;
 
2630
    } else {
 
2631
        *ip = oflow->tunnel.ip_dst;
 
2632
    }
 
2633
 
 
2634
    xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
2635
    ovs_assert(xcfg);
 
2636
 
 
2637
    HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
 
2638
        if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
 
2639
            struct xport *port;
 
2640
 
 
2641
            HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
 
2642
                if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
 
2643
                    *out_port = port;
 
2644
                    return 0;
 
2645
                }
 
2646
            }
 
2647
        }
 
2648
    }
 
2649
    return -ENOENT;
 
2650
}
 
2651
 
 
2652
static int
 
2653
xlate_flood_packet(struct xbridge *xbridge, struct dp_packet *packet)
 
2654
{
 
2655
    struct ofpact_output output;
 
2656
    struct flow flow;
 
2657
 
 
2658
    ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
 
2659
    /* Use OFPP_NONE as the in_port to avoid special packet processing. */
 
2660
    flow_extract(packet, &flow);
 
2661
    flow.in_port.ofp_port = OFPP_NONE;
 
2662
    output.port = OFPP_FLOOD;
 
2663
    output.max_len = 0;
 
2664
 
 
2665
    return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
 
2666
                                        &output.ofpact, sizeof output,
 
2667
                                        packet);
 
2668
}
 
2669
 
 
2670
static void
 
2671
tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
 
2672
                     ovs_be32 ip_src, ovs_be32 ip_dst)
 
2673
{
 
2674
    struct xbridge *xbridge = out_dev->xbridge;
 
2675
    struct dp_packet packet;
 
2676
 
 
2677
    dp_packet_init(&packet, 0);
 
2678
    compose_arp(&packet, eth_src, ip_src, ip_dst);
 
2679
 
 
2680
    xlate_flood_packet(xbridge, &packet);
 
2681
    dp_packet_uninit(&packet);
 
2682
}
 
2683
 
 
2684
static int
 
2685
build_tunnel_send(const struct xlate_ctx *ctx, const struct xport *xport,
 
2686
                  const struct flow *flow, odp_port_t tunnel_odp_port)
 
2687
{
 
2688
    struct ovs_action_push_tnl tnl_push_data;
 
2689
    struct xport *out_dev = NULL;
 
2690
    ovs_be32 s_ip, d_ip = 0;
 
2691
    uint8_t smac[ETH_ADDR_LEN];
 
2692
    uint8_t dmac[ETH_ADDR_LEN];
 
2693
    int err;
 
2694
 
 
2695
    err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
 
2696
    if (err) {
 
2697
        return err;
 
2698
    }
 
2699
 
 
2700
    /* Use mac addr of bridge port of the peer. */
 
2701
    err = netdev_get_etheraddr(out_dev->netdev, smac);
 
2702
    if (err) {
 
2703
        return err;
 
2704
    }
 
2705
 
 
2706
    err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
 
2707
    if (err) {
 
2708
        return err;
 
2709
    }
 
2710
 
 
2711
    err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
 
2712
    if (err) {
 
2713
        tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
 
2714
        return err;
 
2715
    }
 
2716
    if (ctx->xin->xcache) {
 
2717
        struct xc_entry *entry;
 
2718
 
 
2719
        entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
 
2720
        ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
 
2721
                    sizeof entry->u.tnl_arp_cache.br_name);
 
2722
        entry->u.tnl_arp_cache.d_ip = d_ip;
 
2723
    }
 
2724
    err = tnl_port_build_header(xport->ofport, flow,
 
2725
                                dmac, smac, s_ip, &tnl_push_data);
 
2726
    if (err) {
 
2727
        return err;
 
2728
    }
 
2729
    tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
 
2730
    tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
 
2731
    odp_put_tnl_push_action(ctx->xout->odp_actions, &tnl_push_data);
 
2732
    return 0;
 
2733
}
 
2734
 
1840
2735
static void
1841
2736
compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1842
 
                        bool check_stp)
 
2737
                        const struct xlate_bond_recirc *xr, bool check_stp)
1843
2738
{
1844
2739
    const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1845
2740
    struct flow_wildcards *wc = &ctx->xout->wc;
1846
2741
    struct flow *flow = &ctx->xin->flow;
 
2742
    struct flow_tnl flow_tnl;
1847
2743
    ovs_be16 flow_vlan_tci;
1848
2744
    uint32_t flow_pkt_mark;
1849
2745
    uint8_t flow_nw_tos;
1850
2746
    odp_port_t out_port, odp_port;
 
2747
    bool tnl_push_pop_send = false;
1851
2748
    uint8_t dscp;
1852
2749
 
1853
2750
    /* If 'struct flow' gets additional metadata, we'll need to zero it out
1854
2751
     * before traversing a patch port. */
1855
 
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 26);
 
2752
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 31);
 
2753
    memset(&flow_tnl, 0, sizeof flow_tnl);
1856
2754
 
1857
2755
    if (!xport) {
1858
2756
        xlate_report(ctx, "Nonexistent output port");
1862
2760
        return;
1863
2761
    } else if (check_stp) {
1864
2762
        if (is_stp(&ctx->base_flow)) {
1865
 
            if (!xport_stp_should_forward_bpdu(xport)) {
1866
 
                xlate_report(ctx, "STP not in listening state, "
1867
 
                             "skipping bpdu output");
 
2763
            if (!xport_stp_should_forward_bpdu(xport) &&
 
2764
                !xport_rstp_should_manage_bpdu(xport)) {
 
2765
                if (ctx->xbridge->stp != NULL) {
 
2766
                    xlate_report(ctx, "STP not in listening state, "
 
2767
                            "skipping bpdu output");
 
2768
                } else if (ctx->xbridge->rstp != NULL) {
 
2769
                    xlate_report(ctx, "RSTP not managing BPDU in this state, "
 
2770
                            "skipping bpdu output");
 
2771
                }
1868
2772
                return;
1869
2773
            }
1870
 
        } else if (!xport_stp_forward_state(xport)) {
1871
 
            xlate_report(ctx, "STP not in forwarding state, "
1872
 
                         "skipping output");
 
2774
        } else if (!xport_stp_forward_state(xport) ||
 
2775
                   !xport_rstp_forward_state(xport)) {
 
2776
            if (ctx->xbridge->stp != NULL) {
 
2777
                xlate_report(ctx, "STP not in forwarding state, "
 
2778
                        "skipping output");
 
2779
            } else if (ctx->xbridge->rstp != NULL) {
 
2780
                xlate_report(ctx, "RSTP not in forwarding state, "
 
2781
                        "skipping output");
 
2782
            }
1873
2783
            return;
1874
2784
        }
1875
2785
    }
1882
2792
    if (xport->peer) {
1883
2793
        const struct xport *peer = xport->peer;
1884
2794
        struct flow old_flow = ctx->xin->flow;
 
2795
        bool old_was_mpls = ctx->was_mpls;
 
2796
        cls_version_t old_version = ctx->tables_version;
1885
2797
        enum slow_path_reason special;
1886
 
        uint8_t table_id = rule_dpif_lookup_get_init_table_id(&ctx->xin->flow);
 
2798
        struct ofpbuf old_stack = ctx->stack;
 
2799
        union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
 
2800
        struct ofpbuf old_action_set = ctx->action_set;
 
2801
        uint64_t actset_stub[1024 / 8];
1887
2802
 
 
2803
        ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
 
2804
        ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
1888
2805
        ctx->xbridge = peer->xbridge;
1889
2806
        flow->in_port.ofp_port = peer->ofp_port;
1890
2807
        flow->metadata = htonll(0);
1891
2808
        memset(&flow->tunnel, 0, sizeof flow->tunnel);
1892
2809
        memset(flow->regs, 0, sizeof flow->regs);
 
2810
        flow->actset_output = OFPP_UNSET;
 
2811
 
 
2812
        /* The bridge is now known so obtain its table version. */
 
2813
        ctx->tables_version
 
2814
            = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
1893
2815
 
1894
2816
        special = process_special(ctx, &ctx->xin->flow, peer,
1895
2817
                                  ctx->xin->packet);
1896
2818
        if (special) {
1897
2819
            ctx->xout->slow |= special;
1898
2820
        } else if (may_receive(peer, ctx)) {
1899
 
            if (xport_stp_forward_state(peer)) {
1900
 
                xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
1901
 
                                   true, true);
 
2821
            if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
 
2822
                xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
 
2823
                if (ctx->action_set.size) {
 
2824
                    /* Translate action set only if not dropping the packet and
 
2825
                     * not recirculating. */
 
2826
                    if (!exit_recirculates(ctx)) {
 
2827
                        xlate_action_set(ctx);
 
2828
                    }
 
2829
                }
 
2830
                /* Check if need to recirculate. */
 
2831
                if (exit_recirculates(ctx)) {
 
2832
                    compose_recirculate_action(ctx);
 
2833
                }
1902
2834
            } else {
1903
 
                /* Forwarding is disabled by STP.  Let OFPP_NORMAL and the
1904
 
                 * learning action look at the packet, then drop it. */
 
2835
                /* Forwarding is disabled by STP and RSTP.  Let OFPP_NORMAL and
 
2836
                 * the learning action look at the packet, then drop it. */
1905
2837
                struct flow old_base_flow = ctx->base_flow;
1906
 
                size_t old_size = ofpbuf_size(&ctx->xout->odp_actions);
 
2838
                size_t old_size = ctx->xout->odp_actions->size;
1907
2839
                mirror_mask_t old_mirrors = ctx->xout->mirrors;
1908
 
                xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
1909
 
                                   true, true);
 
2840
 
 
2841
                xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
1910
2842
                ctx->xout->mirrors = old_mirrors;
1911
2843
                ctx->base_flow = old_base_flow;
1912
 
                ofpbuf_set_size(&ctx->xout->odp_actions, old_size);
 
2844
                ctx->xout->odp_actions->size = old_size;
 
2845
 
 
2846
                /* Undo changes that may have been done for recirculation. */
 
2847
                if (exit_recirculates(ctx)) {
 
2848
                    ctx->action_set.size = ctx->recirc_action_offset;
 
2849
                    ctx->recirc_action_offset = -1;
 
2850
                    ctx->last_unroll_offset = -1;
 
2851
                }
1913
2852
            }
1914
2853
        }
1915
2854
 
1916
2855
        ctx->xin->flow = old_flow;
1917
2856
        ctx->xbridge = xport->xbridge;
 
2857
        ofpbuf_uninit(&ctx->action_set);
 
2858
        ctx->action_set = old_action_set;
 
2859
        ofpbuf_uninit(&ctx->stack);
 
2860
        ctx->stack = old_stack;
 
2861
 
 
2862
        /* Restore calling bridge's lookup version. */
 
2863
        ctx->tables_version = old_version;
 
2864
 
 
2865
        /* The peer bridge popping MPLS should have no effect on the original
 
2866
         * bridge. */
 
2867
        ctx->was_mpls = old_was_mpls;
 
2868
 
 
2869
        /* The fact that the peer bridge exits (for any reason) does not mean
 
2870
         * that the original bridge should exit.  Specifically, if the peer
 
2871
         * bridge recirculates (which typically modifies the packet), the
 
2872
         * original bridge must continue processing with the original, not the
 
2873
         * recirculated packet! */
 
2874
        ctx->exit = false;
1918
2875
 
1919
2876
        if (ctx->xin->resubmit_stats) {
1920
2877
            netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1931
2888
            entry->u.dev.rx = netdev_ref(peer->netdev);
1932
2889
            entry->u.dev.bfd = bfd_ref(peer->bfd);
1933
2890
        }
1934
 
 
1935
2891
        return;
1936
2892
    }
1937
2893
 
1939
2895
    flow_pkt_mark = flow->pkt_mark;
1940
2896
    flow_nw_tos = flow->nw_tos;
1941
2897
 
1942
 
    if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1943
 
        wc->masks.nw_tos |= IP_DSCP_MASK;
1944
 
        flow->nw_tos &= ~IP_DSCP_MASK;
1945
 
        flow->nw_tos |= dscp;
 
2898
    if (count_skb_priorities(xport)) {
 
2899
        memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
 
2900
        if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
 
2901
            wc->masks.nw_tos |= IP_DSCP_MASK;
 
2902
            flow->nw_tos &= ~IP_DSCP_MASK;
 
2903
            flow->nw_tos |= dscp;
 
2904
        }
1946
2905
    }
1947
2906
 
1948
2907
    if (xport->is_tunnel) {
1950
2909
          * the Logical (tunnel) Port are not visible for any further
1951
2910
          * matches, while explicit set actions on tunnel metadata are.
1952
2911
          */
1953
 
        struct flow_tnl flow_tnl = flow->tunnel;
 
2912
        flow_tnl = flow->tunnel;
1954
2913
        odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1955
2914
        if (odp_port == ODPP_NONE) {
1956
2915
            xlate_report(ctx, "Tunneling decided against output");
1970
2929
            entry->u.dev.tx = netdev_ref(xport->netdev);
1971
2930
        }
1972
2931
        out_port = odp_port;
1973
 
        commit_odp_tunnel_action(flow, &ctx->base_flow,
1974
 
                                 &ctx->xout->odp_actions);
1975
 
        flow->tunnel = flow_tnl; /* Restore tunnel metadata */
 
2932
        if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
 
2933
            tnl_push_pop_send = true;
 
2934
        } else {
 
2935
            commit_odp_tunnel_action(flow, &ctx->base_flow,
 
2936
                                     ctx->xout->odp_actions);
 
2937
            flow->tunnel = flow_tnl; /* Restore tunnel metadata */
 
2938
        }
1976
2939
    } else {
1977
2940
        odp_port = xport->odp_port;
1978
2941
        out_port = odp_port;
1990
2953
    }
1991
2954
 
1992
2955
    if (out_port != ODPP_NONE) {
 
2956
        bool use_masked = ctx->xbridge->support.masked_set_action;
 
2957
 
1993
2958
        ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1994
 
                                              &ctx->xout->odp_actions,
1995
 
                                              &ctx->xout->wc);
 
2959
                                              ctx->xout->odp_actions,
 
2960
                                              wc, use_masked);
1996
2961
 
1997
 
        if (ctx->use_recirc) {
 
2962
        if (xr) {
1998
2963
            struct ovs_action_hash *act_hash;
1999
 
            struct xlate_recirc *xr = &ctx->recirc;
2000
2964
 
2001
2965
            /* Hash action. */
2002
 
            act_hash = nl_msg_put_unspec_uninit(&ctx->xout->odp_actions,
 
2966
            act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
2003
2967
                                                OVS_ACTION_ATTR_HASH,
2004
2968
                                                sizeof *act_hash);
2005
2969
            act_hash->hash_alg = xr->hash_alg;
2006
2970
            act_hash->hash_basis = xr->hash_basis;
2007
2971
 
2008
2972
            /* Recirc action. */
2009
 
            nl_msg_put_u32(&ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
 
2973
            nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
2010
2974
                           xr->recirc_id);
2011
2975
        } else {
2012
 
            nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
2013
 
                                out_port);
 
2976
 
 
2977
            if (tnl_push_pop_send) {
 
2978
                build_tunnel_send(ctx, xport, flow, odp_port);
 
2979
                flow->tunnel = flow_tnl; /* Restore tunnel metadata */
 
2980
            } else {
 
2981
                odp_port_t odp_tnl_port = ODPP_NONE;
 
2982
 
 
2983
                /* XXX: Write better Filter for tunnel port. We can use inport
 
2984
                * int tunnel-port flow to avoid these checks completely. */
 
2985
                if (ofp_port == OFPP_LOCAL &&
 
2986
                    ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
 
2987
 
 
2988
                    odp_tnl_port = tnl_port_map_lookup(flow, wc);
 
2989
                }
 
2990
 
 
2991
                if (odp_tnl_port != ODPP_NONE) {
 
2992
                    nl_msg_put_odp_port(ctx->xout->odp_actions,
 
2993
                                        OVS_ACTION_ATTR_TUNNEL_POP,
 
2994
                                        odp_tnl_port);
 
2995
                } else {
 
2996
                    /* Tunnel push-pop action is not compatible with
 
2997
                     * IPFIX action. */
 
2998
                    add_ipfix_output_action(ctx, out_port);
 
2999
                    nl_msg_put_odp_port(ctx->xout->odp_actions,
 
3000
                                        OVS_ACTION_ATTR_OUTPUT,
 
3001
                                        out_port);
 
3002
               }
 
3003
           }
2014
3004
        }
2015
3005
 
2016
3006
        ctx->sflow_odp_port = odp_port;
2026
3016
}
2027
3017
 
2028
3018
static void
2029
 
compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
 
3019
compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
 
3020
                      const struct xlate_bond_recirc *xr)
2030
3021
{
2031
 
    compose_output_action__(ctx, ofp_port, true);
 
3022
    compose_output_action__(ctx, ofp_port, xr, true);
2032
3023
}
2033
3024
 
2034
3025
static void
2035
3026
xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
2036
3027
{
2037
3028
    struct rule_dpif *old_rule = ctx->rule;
 
3029
    ovs_be64 old_cookie = ctx->rule_cookie;
2038
3030
    const struct rule_actions *actions;
2039
3031
 
2040
3032
    if (ctx->xin->resubmit_stats) {
2044
3036
    ctx->resubmits++;
2045
3037
    ctx->recurse++;
2046
3038
    ctx->rule = rule;
 
3039
    ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
2047
3040
    actions = rule_dpif_get_actions(rule);
2048
3041
    do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
 
3042
    ctx->rule_cookie = old_cookie;
2049
3043
    ctx->rule = old_rule;
2050
3044
    ctx->recurse--;
2051
3045
}
2060
3054
                    MAX_RESUBMIT_RECURSION);
2061
3055
    } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
2062
3056
        VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
2063
 
    } else if (ofpbuf_size(&ctx->xout->odp_actions) > UINT16_MAX) {
 
3057
    } else if (ctx->xout->odp_actions->size > UINT16_MAX) {
2064
3058
        VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
2065
 
    } else if (ofpbuf_size(&ctx->stack) >= 65536) {
 
3059
    } else if (ctx->stack.size >= 65536) {
2066
3060
        VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2067
3061
    } else {
2068
3062
        return true;
2075
3069
xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2076
3070
                   bool may_packet_in, bool honor_table_miss)
2077
3071
{
 
3072
    /* Check if we need to recirculate before matching in a table. */
 
3073
    if (ctx->was_mpls) {
 
3074
        ctx_trigger_recirculation(ctx);
 
3075
        return;
 
3076
    }
2078
3077
    if (xlate_resubmit_resource_check(ctx)) {
2079
 
        ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
2080
 
        bool skip_wildcards = ctx->xin->skip_wildcards;
 
3078
        struct flow_wildcards *wc;
2081
3079
        uint8_t old_table_id = ctx->table_id;
2082
3080
        struct rule_dpif *rule;
2083
 
        enum rule_dpif_lookup_verdict verdict;
2084
 
        enum ofputil_port_config config = 0;
2085
3081
 
2086
3082
        ctx->table_id = table_id;
2087
 
 
2088
 
        /* Look up a flow with 'in_port' as the input port.  Then restore the
2089
 
         * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2090
 
         * have surprising behavior). */
2091
 
        ctx->xin->flow.in_port.ofp_port = in_port;
2092
 
        verdict = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2093
 
                                              &ctx->xin->flow,
2094
 
                                              !skip_wildcards
2095
 
                                              ? &ctx->xout->wc : NULL,
2096
 
                                              honor_table_miss,
2097
 
                                              &ctx->table_id, &rule,
2098
 
                                              ctx->xin->xcache != NULL);
2099
 
        ctx->xin->flow.in_port.ofp_port = old_in_port;
2100
 
 
2101
 
        if (ctx->xin->resubmit_hook) {
2102
 
            ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
2103
 
        }
2104
 
 
2105
 
        switch (verdict) {
2106
 
        case RULE_DPIF_LOOKUP_VERDICT_MATCH:
2107
 
           goto match;
2108
 
        case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER:
2109
 
            if (may_packet_in) {
2110
 
                struct xport *xport;
2111
 
 
2112
 
                xport = get_ofp_port(ctx->xbridge,
2113
 
                                     ctx->xin->flow.in_port.ofp_port);
2114
 
                config = xport ? xport->config : 0;
2115
 
                break;
2116
 
            }
2117
 
            /* Fall through to drop */
2118
 
        case RULE_DPIF_LOOKUP_VERDICT_DROP:
2119
 
            config = OFPUTIL_PC_NO_PACKET_IN;
2120
 
            break;
2121
 
        case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
2122
 
            if (!ofproto_dpif_wants_packet_in_on_miss(ctx->xbridge->ofproto)) {
2123
 
                config = OFPUTIL_PC_NO_PACKET_IN;
2124
 
            }
2125
 
            break;
2126
 
        default:
2127
 
            OVS_NOT_REACHED();
2128
 
        }
2129
 
 
2130
 
        choose_miss_rule(config, ctx->xbridge->miss_rule,
2131
 
                         ctx->xbridge->no_packet_in_rule, &rule,
2132
 
                         ctx->xin->xcache != NULL);
2133
 
 
2134
 
match:
 
3083
        wc = (ctx->xin->skip_wildcards) ? NULL : &ctx->xout->wc;
 
3084
 
 
3085
        rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
 
3086
                                           ctx->tables_version,
 
3087
                                           &ctx->xin->flow, wc,
 
3088
                                           ctx->xin->xcache != NULL,
 
3089
                                           ctx->xin->resubmit_stats,
 
3090
                                           &ctx->table_id, in_port,
 
3091
                                           may_packet_in, honor_table_miss);
 
3092
 
 
3093
        if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
 
3094
            ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
 
3095
        }
 
3096
 
2135
3097
        if (rule) {
2136
3098
            /* Fill in the cache entry here instead of xlate_recursively
2137
3099
             * to make the reference counting more explicit.  We take a
2154
3116
}
2155
3117
 
2156
3118
static void
2157
 
xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
 
3119
xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
 
3120
                  struct ofputil_bucket *bucket)
 
3121
{
 
3122
    if (ctx->xin->resubmit_stats) {
 
3123
        group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
 
3124
    }
 
3125
    if (ctx->xin->xcache) {
 
3126
        struct xc_entry *entry;
 
3127
 
 
3128
        entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
 
3129
        entry->u.group.group = group_dpif_ref(group);
 
3130
        entry->u.group.bucket = bucket;
 
3131
    }
 
3132
}
 
3133
 
 
3134
static void
 
3135
xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
2158
3136
{
2159
3137
    uint64_t action_list_stub[1024 / 8];
2160
3138
    struct ofpbuf action_list, action_set;
2161
3139
    struct flow old_flow = ctx->xin->flow;
 
3140
    bool old_was_mpls = ctx->was_mpls;
2162
3141
 
2163
3142
    ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
2164
3143
    ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2165
3144
 
2166
3145
    ofpacts_execute_action_set(&action_list, &action_set);
2167
3146
    ctx->recurse++;
2168
 
    do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
 
3147
    do_xlate_actions(action_list.data, action_list.size, ctx);
2169
3148
    ctx->recurse--;
2170
3149
 
2171
3150
    ofpbuf_uninit(&action_set);
2172
3151
    ofpbuf_uninit(&action_list);
2173
3152
 
 
3153
    /* Check if need to recirculate. */
 
3154
    if (exit_recirculates(ctx)) {
 
3155
        compose_recirculate_action(ctx);
 
3156
    }
 
3157
 
2174
3158
    /* Roll back flow to previous state.
2175
3159
     * This is equivalent to cloning the packet for each bucket.
2176
3160
     *
2180
3164
     *
2181
3165
     * Note that group buckets are action sets, hence they cannot modify the
2182
3166
     * main action set.  Also any stack actions are ignored when executing an
2183
 
     * action set, so group buckets cannot change the stack either. */
 
3167
     * action set, so group buckets cannot change the stack either.
 
3168
     * However, we do allow resubmit actions in group buckets, which could
 
3169
     * break the above assumptions.  It is up to the controller to not mess up
 
3170
     * with the action_set and stack in the tables resubmitted to from
 
3171
     * group buckets. */
2184
3172
    ctx->xin->flow = old_flow;
2185
3173
 
 
3174
    /* The group bucket popping MPLS should have no effect after bucket
 
3175
     * execution. */
 
3176
    ctx->was_mpls = old_was_mpls;
 
3177
 
2186
3178
    /* The fact that the group bucket exits (for any reason) does not mean that
2187
3179
     * the translation after the group action should exit.  Specifically, if
2188
3180
     * the group bucket recirculates (which typically modifies the packet), the
2194
3186
static void
2195
3187
xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
2196
3188
{
2197
 
    const struct ofputil_bucket *bucket;
2198
 
    const struct list *buckets;
 
3189
    struct ofputil_bucket *bucket;
 
3190
    const struct ovs_list *buckets;
2199
3191
 
2200
3192
    group_dpif_get_buckets(group, &buckets);
2201
3193
 
2202
3194
    LIST_FOR_EACH (bucket, list_node, buckets) {
2203
3195
        xlate_group_bucket(ctx, bucket);
2204
3196
    }
 
3197
    xlate_group_stats(ctx, group, NULL);
2205
3198
}
2206
3199
 
2207
3200
static void
2208
3201
xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
2209
3202
{
2210
 
    const struct ofputil_bucket *bucket;
 
3203
    struct ofputil_bucket *bucket;
2211
3204
 
2212
3205
    bucket = group_first_live_bucket(ctx, group, 0);
2213
3206
    if (bucket) {
2214
3207
        xlate_group_bucket(ctx, bucket);
 
3208
        xlate_group_stats(ctx, group, bucket);
 
3209
    }
 
3210
}
 
3211
 
 
3212
static void
 
3213
xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
 
3214
{
 
3215
    struct flow_wildcards *wc = &ctx->xout->wc;
 
3216
    struct ofputil_bucket *bucket;
 
3217
    uint32_t basis;
 
3218
 
 
3219
    basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
 
3220
    flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
 
3221
    bucket = group_best_live_bucket(ctx, group, basis);
 
3222
    if (bucket) {
 
3223
        xlate_group_bucket(ctx, bucket);
 
3224
        xlate_group_stats(ctx, group, bucket);
 
3225
    }
 
3226
}
 
3227
 
 
3228
static void
 
3229
xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
 
3230
{
 
3231
    struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
 
3232
    struct flow_wildcards *wc = &ctx->xout->wc;
 
3233
    const struct field_array *fields;
 
3234
    struct ofputil_bucket *bucket;
 
3235
    uint32_t basis;
 
3236
    int i;
 
3237
 
 
3238
    fields = group_dpif_get_fields(group);
 
3239
    basis = hash_uint64(group_dpif_get_selection_method_param(group));
 
3240
 
 
3241
    /* Determine which fields to hash */
 
3242
    for (i = 0; i < MFF_N_IDS; i++) {
 
3243
        if (bitmap_is_set(fields->used.bm, i)) {
 
3244
            const struct mf_field *mf;
 
3245
 
 
3246
            /* If the field is already present in 'hash_fields' then
 
3247
             * this loop has already checked that it and its pre-requisites
 
3248
             * are present in the flow and its pre-requisites have
 
3249
             * already been added to 'hash_fields'. There is nothing more
 
3250
             * to do here and as an optimisation the loop can continue. */
 
3251
            if (bitmap_is_set(hash_fields.bm, i)) {
 
3252
                continue;
 
3253
            }
 
3254
 
 
3255
            mf = mf_from_id(i);
 
3256
 
 
3257
            /* Only hash a field if it and its pre-requisites are present
 
3258
             * in the flow. */
 
3259
            if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
 
3260
                continue;
 
3261
            }
 
3262
 
 
3263
            /* Hash both the field and its pre-requisites */
 
3264
            mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
 
3265
        }
 
3266
    }
 
3267
 
 
3268
    /* Hash the fields */
 
3269
    for (i = 0; i < MFF_N_IDS; i++) {
 
3270
        if (bitmap_is_set(hash_fields.bm, i)) {
 
3271
            const struct mf_field *mf = mf_from_id(i);
 
3272
            union mf_value value;
 
3273
            int j;
 
3274
 
 
3275
            mf_get_value(mf, &ctx->xin->flow, &value);
 
3276
            /* This seems inefficient but so does apply_mask() */
 
3277
            for (j = 0; j < mf->n_bytes; j++) {
 
3278
                ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
 
3279
            }
 
3280
            basis = hash_bytes(&value, mf->n_bytes, basis);
 
3281
 
 
3282
            mf_mask_field(mf, &wc->masks);
 
3283
        }
 
3284
    }
 
3285
 
 
3286
    bucket = group_best_live_bucket(ctx, group, basis);
 
3287
    if (bucket) {
 
3288
        xlate_group_bucket(ctx, bucket);
 
3289
        xlate_group_stats(ctx, group, bucket);
2215
3290
    }
2216
3291
}
2217
3292
 
2218
3293
static void
2219
3294
xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
2220
3295
{
2221
 
    struct flow_wildcards *wc = &ctx->xout->wc;
2222
 
    const struct ofputil_bucket *bucket;
2223
 
    uint32_t basis;
 
3296
    const char *selection_method = group_dpif_get_selection_method(group);
2224
3297
 
2225
 
    basis = hash_mac(ctx->xin->flow.dl_dst, 0, 0);
2226
 
    bucket = group_best_live_bucket(ctx, group, basis);
2227
 
    if (bucket) {
2228
 
        memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2229
 
        xlate_group_bucket(ctx, bucket);
 
3298
    if (selection_method[0] == '\0') {
 
3299
        xlate_default_select_group(ctx, group);
 
3300
    } else if (!strcasecmp("hash", selection_method)) {
 
3301
        xlate_hash_fields_select_group(ctx, group);
 
3302
    } else {
 
3303
        /* Parsing of groups should ensure this never happens */
 
3304
        OVS_NOT_REACHED();
2230
3305
    }
2231
3306
}
2232
3307
 
2249
3324
    default:
2250
3325
        OVS_NOT_REACHED();
2251
3326
    }
2252
 
    group_dpif_release(group);
 
3327
    group_dpif_unref(group);
2253
3328
 
2254
3329
    ctx->in_group = false;
2255
3330
}
2337
3412
        }
2338
3413
 
2339
3414
        if (all) {
2340
 
            compose_output_action__(ctx, xport->ofp_port, false);
 
3415
            compose_output_action__(ctx, xport->ofp_port, NULL, false);
2341
3416
        } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2342
 
            compose_output_action(ctx, xport->ofp_port);
 
3417
            compose_output_action(ctx, xport->ofp_port, NULL);
2343
3418
        }
2344
3419
    }
2345
3420
 
2352
3427
                          uint16_t controller_id)
2353
3428
{
2354
3429
    struct ofproto_packet_in *pin;
2355
 
    struct ofpbuf *packet;
2356
 
    struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
 
3430
    struct dp_packet *packet;
 
3431
    bool use_masked;
2357
3432
 
2358
3433
    ctx->xout->slow |= SLOW_CONTROLLER;
2359
3434
    if (!ctx->xin->packet) {
2360
3435
        return;
2361
3436
    }
2362
3437
 
2363
 
    packet = ofpbuf_clone(ctx->xin->packet);
 
3438
    packet = dp_packet_clone(ctx->xin->packet);
2364
3439
 
 
3440
    use_masked = ctx->xbridge->support.masked_set_action;
2365
3441
    ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2366
 
                                          &ctx->xout->odp_actions,
2367
 
                                          &ctx->xout->wc);
 
3442
                                          ctx->xout->odp_actions,
 
3443
                                          &ctx->xout->wc, use_masked);
2368
3444
 
2369
 
    odp_execute_actions(NULL, packet, false, &md,
2370
 
                        ofpbuf_data(&ctx->xout->odp_actions),
2371
 
                        ofpbuf_size(&ctx->xout->odp_actions), NULL);
 
3445
    odp_execute_actions(NULL, &packet, 1, false,
 
3446
                        ctx->xout->odp_actions->data,
 
3447
                        ctx->xout->odp_actions->size, NULL);
2372
3448
 
2373
3449
    pin = xmalloc(sizeof *pin);
2374
 
    pin->up.packet_len = ofpbuf_size(packet);
2375
 
    pin->up.packet = ofpbuf_steal_data(packet);
 
3450
    pin->up.packet_len = dp_packet_size(packet);
 
3451
    pin->up.packet = dp_packet_steal_data(packet);
2376
3452
    pin->up.reason = reason;
2377
3453
    pin->up.table_id = ctx->table_id;
2378
 
    pin->up.cookie = (ctx->rule
2379
 
                      ? rule_dpif_get_flow_cookie(ctx->rule)
2380
 
                      : OVS_BE64_MAX);
 
3454
    pin->up.cookie = ctx->rule_cookie;
2381
3455
 
2382
 
    flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
 
3456
    flow_get_metadata(&ctx->xin->flow, &pin->up.flow_metadata);
2383
3457
 
2384
3458
    pin->controller_id = controller_id;
2385
3459
    pin->send_len = len;
2400
3474
        }
2401
3475
    }
2402
3476
    ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2403
 
    ofpbuf_delete(packet);
 
3477
    dp_packet_delete(packet);
 
3478
}
 
3479
 
 
3480
/* Called only when ctx->recirc_action_offset is set. */
 
3481
static void
 
3482
compose_recirculate_action(struct xlate_ctx *ctx)
 
3483
{
 
3484
    struct recirc_metadata md;
 
3485
    bool use_masked;
 
3486
    uint32_t id;
 
3487
 
 
3488
    use_masked = ctx->xbridge->support.masked_set_action;
 
3489
    ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
 
3490
                                          ctx->xout->odp_actions,
 
3491
                                          &ctx->xout->wc, use_masked);
 
3492
 
 
3493
    recirc_metadata_from_flow(&md, &ctx->xin->flow);
 
3494
 
 
3495
    ovs_assert(ctx->recirc_action_offset >= 0);
 
3496
 
 
3497
    /* Only allocate recirculation ID if we have a packet. */
 
3498
    if (ctx->xin->packet) {
 
3499
        /* Allocate a unique recirc id for the given metadata state in the
 
3500
         * flow.  The life-cycle of this recirc id is managed by associating it
 
3501
         * with the udpif key ('ukey') created for each new datapath flow. */
 
3502
        id = recirc_alloc_id_ctx(ctx->xbridge->ofproto, 0, &md, &ctx->stack,
 
3503
                                 ctx->recirc_action_offset,
 
3504
                                 ctx->action_set.size, ctx->action_set.data);
 
3505
        if (!id) {
 
3506
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
 
3507
            VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
 
3508
            return;
 
3509
        }
 
3510
        xlate_out_add_recirc(ctx->xout, id);
 
3511
    } else {
 
3512
        /* Look up an existing recirc id for the given metadata state in the
 
3513
         * flow.  No new reference is taken, as the ID is RCU protected and is
 
3514
         * only required temporarily for verification. */
 
3515
        id = recirc_find_id(ctx->xbridge->ofproto, 0, &md, &ctx->stack,
 
3516
                            ctx->recirc_action_offset,
 
3517
                            ctx->action_set.size, ctx->action_set.data);
 
3518
        /* We let zero 'id' to be used in the RECIRC action below, which will
 
3519
         * fail all revalidations as zero is not a valid recirculation ID. */
 
3520
    }
 
3521
 
 
3522
    nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
 
3523
 
 
3524
    /* Undo changes done by recirculation. */
 
3525
    ctx->action_set.size = ctx->recirc_action_offset;
 
3526
    ctx->recirc_action_offset = -1;
 
3527
    ctx->last_unroll_offset = -1;
2404
3528
}
2405
3529
 
2406
3530
static void
2414
3538
 
2415
3539
    n = flow_count_mpls_labels(flow, wc);
2416
3540
    if (!n) {
 
3541
        bool use_masked = ctx->xbridge->support.masked_set_action;
 
3542
 
2417
3543
        ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2418
 
                                              &ctx->xout->odp_actions,
2419
 
                                              &ctx->xout->wc);
 
3544
                                              ctx->xout->odp_actions,
 
3545
                                              &ctx->xout->wc, use_masked);
2420
3546
    } else if (n >= FLOW_MAX_MPLS_LABELS) {
2421
3547
        if (ctx->xin->packet != NULL) {
2422
3548
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2427
3553
        }
2428
3554
        ctx->exit = true;
2429
3555
        return;
2430
 
    } else if (n >= ctx->xbridge->max_mpls_depth) {
2431
 
        COVERAGE_INC(xlate_actions_mpls_overflow);
2432
 
        ctx->xout->slow |= SLOW_ACTION;
2433
3556
    }
2434
3557
 
2435
3558
    flow_push_mpls(flow, n, mpls->ethertype, wc);
2442
3565
    struct flow *flow = &ctx->xin->flow;
2443
3566
    int n = flow_count_mpls_labels(flow, wc);
2444
3567
 
2445
 
    if (!flow_pop_mpls(flow, n, eth_type, wc) && n >= FLOW_MAX_MPLS_LABELS) {
 
3568
    if (flow_pop_mpls(flow, n, eth_type, wc)) {
 
3569
        if (ctx->xbridge->support.recirc) {
 
3570
            ctx->was_mpls = true;
 
3571
        }
 
3572
    } else if (n >= FLOW_MAX_MPLS_LABELS) {
2446
3573
        if (ctx->xin->packet != NULL) {
2447
3574
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2448
3575
            VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2451
3578
                         ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2452
3579
        }
2453
3580
        ctx->exit = true;
2454
 
        ofpbuf_clear(&ctx->xout->odp_actions);
 
3581
        ofpbuf_clear(ctx->xout->odp_actions);
2455
3582
    }
2456
3583
}
2457
3584
 
2512
3639
compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2513
3640
{
2514
3641
    struct flow *flow = &ctx->xin->flow;
2515
 
    uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
2516
3642
    struct flow_wildcards *wc = &ctx->xout->wc;
2517
3643
 
2518
 
    memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2519
3644
    if (eth_type_mpls(flow->dl_type)) {
 
3645
        uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
 
3646
 
 
3647
        wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
2520
3648
        if (ttl > 1) {
2521
3649
            ttl--;
2522
3650
            set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
2523
3651
            return false;
2524
3652
        } else {
2525
3653
            execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2526
 
 
2527
 
            /* Stop processing for current table. */
2528
 
            return true;
2529
3654
        }
2530
 
    } else {
2531
 
        return true;
2532
3655
    }
 
3656
 
 
3657
    /* Stop processing for current table. */
 
3658
    return true;
2533
3659
}
2534
3660
 
2535
3661
static void
2542
3668
 
2543
3669
    switch (port) {
2544
3670
    case OFPP_IN_PORT:
2545
 
        compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
 
3671
        compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
2546
3672
        break;
2547
3673
    case OFPP_TABLE:
2548
3674
        xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2558
3684
        flood_packets(ctx, true);
2559
3685
        break;
2560
3686
    case OFPP_CONTROLLER:
2561
 
        execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
 
3687
        execute_controller_action(ctx, max_len,
 
3688
                                  (ctx->in_group ? OFPR_GROUP
 
3689
                                   : ctx->in_action_set ? OFPR_ACTION_SET
 
3690
                                   : OFPR_ACTION),
 
3691
                                  0);
2562
3692
        break;
2563
3693
    case OFPP_NONE:
2564
3694
        break;
2565
3695
    case OFPP_LOCAL:
2566
3696
    default:
2567
3697
        if (port != ctx->xin->flow.in_port.ofp_port) {
2568
 
            compose_output_action(ctx, port);
 
3698
            compose_output_action(ctx, port, NULL);
2569
3699
        } else {
2570
3700
            xlate_report(ctx, "skipping output to input port");
2571
3701
        }
2624
3754
    /* Add datapath actions. */
2625
3755
    flow_priority = ctx->xin->flow.skb_priority;
2626
3756
    ctx->xin->flow.skb_priority = priority;
2627
 
    compose_output_action(ctx, ofp_port);
 
3757
    compose_output_action(ctx, ofp_port, NULL);
2628
3758
    ctx->xin->flow.skb_priority = flow_priority;
2629
3759
 
2630
3760
    /* Update NetFlow output port. */
2756
3886
xlate_sample_action(struct xlate_ctx *ctx,
2757
3887
                    const struct ofpact_sample *os)
2758
3888
{
2759
 
  union user_action_cookie cookie;
2760
 
  /* Scale the probability from 16-bit to 32-bit while representing
2761
 
   * the same percentage. */
2762
 
  uint32_t probability = (os->probability << 16) | os->probability;
2763
 
 
2764
 
  if (!ctx->xbridge->variable_length_userdata) {
2765
 
      static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2766
 
 
2767
 
      VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
2768
 
                  "lacks support (needs Linux 3.10+ or kernel module from "
2769
 
                  "OVS 1.11+)");
2770
 
      return;
2771
 
  }
2772
 
 
2773
 
  ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2774
 
                                        &ctx->xout->odp_actions,
2775
 
                                        &ctx->xout->wc);
2776
 
 
2777
 
  compose_flow_sample_cookie(os->probability, os->collector_set_id,
2778
 
                             os->obs_domain_id, os->obs_point_id, &cookie);
2779
 
  compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2780
 
                        probability, &cookie, sizeof cookie.flow_sample);
 
3889
    union user_action_cookie cookie;
 
3890
    /* Scale the probability from 16-bit to 32-bit while representing
 
3891
     * the same percentage. */
 
3892
    uint32_t probability = (os->probability << 16) | os->probability;
 
3893
    bool use_masked;
 
3894
 
 
3895
    if (!ctx->xbridge->support.variable_length_userdata) {
 
3896
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
 
3897
 
 
3898
        VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
 
3899
                    "lacks support (needs Linux 3.10+ or kernel module from "
 
3900
                    "OVS 1.11+)");
 
3901
        return;
 
3902
    }
 
3903
 
 
3904
    use_masked = ctx->xbridge->support.masked_set_action;
 
3905
    ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
 
3906
                                          ctx->xout->odp_actions,
 
3907
                                          &ctx->xout->wc, use_masked);
 
3908
 
 
3909
    compose_flow_sample_cookie(os->probability, os->collector_set_id,
 
3910
                               os->obs_domain_id, os->obs_point_id, &cookie);
 
3911
    compose_sample_action(ctx->xbridge, ctx->xout->odp_actions,
 
3912
                          &ctx->xin->flow, probability, &cookie,
 
3913
                          sizeof cookie.flow_sample, ODPP_NONE);
2781
3914
}
2782
3915
 
2783
3916
static bool
2793
3926
     * disabled.  If just learning is enabled, we need to have
2794
3927
     * OFPP_NORMAL and the learning action have a look at the packet
2795
3928
     * before we can drop it. */
2796
 
    if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
 
3929
    if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
 
3930
        (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
2797
3931
        return false;
2798
3932
    }
2799
3933
 
2803
3937
static void
2804
3938
xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2805
3939
{
2806
 
    struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2807
 
    ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
 
3940
    const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
 
3941
    size_t on_len = ofpact_nest_get_action_len(on);
 
3942
    const struct ofpact *inner;
 
3943
 
 
3944
    /* Maintain actset_output depending on the contents of the action set:
 
3945
     *
 
3946
     *   - OFPP_UNSET, if there is no "output" action.
 
3947
     *
 
3948
     *   - The output port, if there is an "output" action and no "group"
 
3949
     *     action.
 
3950
     *
 
3951
     *   - OFPP_UNSET, if there is a "group" action.
 
3952
     */
 
3953
    if (!ctx->action_set_has_group) {
 
3954
        OFPACT_FOR_EACH (inner, on->actions, on_len) {
 
3955
            if (inner->type == OFPACT_OUTPUT) {
 
3956
                ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
 
3957
            } else if (inner->type == OFPACT_GROUP) {
 
3958
                ctx->xin->flow.actset_output = OFPP_UNSET;
 
3959
                ctx->action_set_has_group = true;
 
3960
                break;
 
3961
            }
 
3962
        }
 
3963
    }
 
3964
 
 
3965
    ofpbuf_put(&ctx->action_set, on->actions, on_len);
2808
3966
    ofpact_pad(&ctx->action_set);
2809
3967
}
2810
3968
 
2814
3972
    uint64_t action_list_stub[1024 / 64];
2815
3973
    struct ofpbuf action_list;
2816
3974
 
 
3975
    ctx->in_action_set = true;
2817
3976
    ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2818
3977
    ofpacts_execute_action_set(&action_list, &ctx->action_set);
2819
 
    do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
 
3978
    /* Clear the action set, as it is not needed any more. */
 
3979
    ofpbuf_clear(&ctx->action_set);
 
3980
    do_xlate_actions(action_list.data, action_list.size, ctx);
 
3981
    ctx->in_action_set = false;
2820
3982
    ofpbuf_uninit(&action_list);
2821
3983
}
2822
3984
 
2823
3985
static void
 
3986
recirc_put_unroll_xlate(struct xlate_ctx *ctx)
 
3987
{
 
3988
    struct ofpact_unroll_xlate *unroll;
 
3989
 
 
3990
    unroll = ctx->last_unroll_offset < 0
 
3991
        ? NULL
 
3992
        : ALIGNED_CAST(struct ofpact_unroll_xlate *,
 
3993
                       (char *)ctx->action_set.data + ctx->last_unroll_offset);
 
3994
 
 
3995
    /* Restore the table_id and rule cookie for a potential PACKET
 
3996
     * IN if needed. */
 
3997
    if (!unroll ||
 
3998
        (ctx->table_id != unroll->rule_table_id
 
3999
         || ctx->rule_cookie != unroll->rule_cookie)) {
 
4000
 
 
4001
        ctx->last_unroll_offset = ctx->action_set.size;
 
4002
        unroll = ofpact_put_UNROLL_XLATE(&ctx->action_set);
 
4003
        unroll->rule_table_id = ctx->table_id;
 
4004
        unroll->rule_cookie = ctx->rule_cookie;
 
4005
    }
 
4006
}
 
4007
 
 
4008
 
 
4009
/* Copy remaining actions to the action_set to be executed after recirculation.
 
4010
 * UNROLL_XLATE action is inserted, if not already done so, before actions that
 
4011
 * may generate PACKET_INs from the current table and without matching another
 
4012
 * rule. */
 
4013
static void
 
4014
recirc_unroll_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
 
4015
                      struct xlate_ctx *ctx)
 
4016
{
 
4017
    const struct ofpact *a;
 
4018
 
 
4019
    OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
 
4020
        switch (a->type) {
 
4021
            /* May generate PACKET INs. */
 
4022
        case OFPACT_OUTPUT_REG:
 
4023
        case OFPACT_GROUP:
 
4024
        case OFPACT_OUTPUT:
 
4025
        case OFPACT_CONTROLLER:
 
4026
        case OFPACT_DEC_MPLS_TTL:
 
4027
        case OFPACT_DEC_TTL:
 
4028
            recirc_put_unroll_xlate(ctx);
 
4029
            break;
 
4030
 
 
4031
            /* These may not generate PACKET INs. */
 
4032
        case OFPACT_SET_TUNNEL:
 
4033
        case OFPACT_REG_MOVE:
 
4034
        case OFPACT_SET_FIELD:
 
4035
        case OFPACT_STACK_PUSH:
 
4036
        case OFPACT_STACK_POP:
 
4037
        case OFPACT_LEARN:
 
4038
        case OFPACT_WRITE_METADATA:
 
4039
        case OFPACT_RESUBMIT:        /* May indirectly generate PACKET INs, */
 
4040
        case OFPACT_GOTO_TABLE:      /* but from a different table and rule. */
 
4041
        case OFPACT_ENQUEUE:
 
4042
        case OFPACT_SET_VLAN_VID:
 
4043
        case OFPACT_SET_VLAN_PCP:
 
4044
        case OFPACT_STRIP_VLAN:
 
4045
        case OFPACT_PUSH_VLAN:
 
4046
        case OFPACT_SET_ETH_SRC:
 
4047
        case OFPACT_SET_ETH_DST:
 
4048
        case OFPACT_SET_IPV4_SRC:
 
4049
        case OFPACT_SET_IPV4_DST:
 
4050
        case OFPACT_SET_IP_DSCP:
 
4051
        case OFPACT_SET_IP_ECN:
 
4052
        case OFPACT_SET_IP_TTL:
 
4053
        case OFPACT_SET_L4_SRC_PORT:
 
4054
        case OFPACT_SET_L4_DST_PORT:
 
4055
        case OFPACT_SET_QUEUE:
 
4056
        case OFPACT_POP_QUEUE:
 
4057
        case OFPACT_PUSH_MPLS:
 
4058
        case OFPACT_POP_MPLS:
 
4059
        case OFPACT_SET_MPLS_LABEL:
 
4060
        case OFPACT_SET_MPLS_TC:
 
4061
        case OFPACT_SET_MPLS_TTL:
 
4062
        case OFPACT_MULTIPATH:
 
4063
        case OFPACT_BUNDLE:
 
4064
        case OFPACT_EXIT:
 
4065
        case OFPACT_UNROLL_XLATE:
 
4066
        case OFPACT_FIN_TIMEOUT:
 
4067
        case OFPACT_CLEAR_ACTIONS:
 
4068
        case OFPACT_WRITE_ACTIONS:
 
4069
        case OFPACT_METER:
 
4070
        case OFPACT_SAMPLE:
 
4071
            break;
 
4072
 
 
4073
            /* These need not be copied for restoration. */
 
4074
        case OFPACT_NOTE:
 
4075
        case OFPACT_CONJUNCTION:
 
4076
            continue;
 
4077
        }
 
4078
        /* Copy the action over. */
 
4079
        ofpbuf_put(&ctx->action_set, a, OFPACT_ALIGN(a->len));
 
4080
    }
 
4081
}
 
4082
 
 
4083
#define CHECK_MPLS_RECIRCULATION()      \
 
4084
    if (ctx->was_mpls) {                \
 
4085
        ctx_trigger_recirculation(ctx); \
 
4086
        break;                          \
 
4087
    }
 
4088
#define CHECK_MPLS_RECIRCULATION_IF(COND) \
 
4089
    if (COND) {                           \
 
4090
        CHECK_MPLS_RECIRCULATION();       \
 
4091
    }
 
4092
 
 
4093
static void
2824
4094
do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2825
4095
                 struct xlate_ctx *ctx)
2826
4096
{
2828
4098
    struct flow *flow = &ctx->xin->flow;
2829
4099
    const struct ofpact *a;
2830
4100
 
 
4101
    if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
 
4102
        tnl_arp_snoop(flow, wc, ctx->xbridge->name);
 
4103
    }
2831
4104
    /* dl_type already in the mask, not set below. */
2832
4105
 
2833
4106
    OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2837
4110
        const struct mf_field *mf;
2838
4111
 
2839
4112
        if (ctx->exit) {
 
4113
            /* Check if need to store the remaining actions for later
 
4114
             * execution. */
 
4115
            if (exit_recirculates(ctx)) {
 
4116
                recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
 
4117
                                                      ((uint8_t *)a -
 
4118
                                                       (uint8_t *)ofpacts)),
 
4119
                                      ctx);
 
4120
            }
2840
4121
            break;
2841
4122
        }
2842
4123
 
2848
4129
 
2849
4130
        case OFPACT_GROUP:
2850
4131
            if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
 
4132
                /* Group could not be found. */
2851
4133
                return;
2852
4134
            }
2853
4135
            break;
2860
4142
            break;
2861
4143
 
2862
4144
        case OFPACT_ENQUEUE:
 
4145
            memset(&wc->masks.skb_priority, 0xff,
 
4146
                   sizeof wc->masks.skb_priority);
2863
4147
            xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2864
4148
            break;
2865
4149
 
2905
4189
            break;
2906
4190
 
2907
4191
        case OFPACT_SET_IPV4_SRC:
 
4192
            CHECK_MPLS_RECIRCULATION();
2908
4193
            if (flow->dl_type == htons(ETH_TYPE_IP)) {
2909
4194
                memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2910
4195
                flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2912
4197
            break;
2913
4198
 
2914
4199
        case OFPACT_SET_IPV4_DST:
 
4200
            CHECK_MPLS_RECIRCULATION();
2915
4201
            if (flow->dl_type == htons(ETH_TYPE_IP)) {
2916
4202
                memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2917
4203
                flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2919
4205
            break;
2920
4206
 
2921
4207
        case OFPACT_SET_IP_DSCP:
 
4208
            CHECK_MPLS_RECIRCULATION();
2922
4209
            if (is_ip_any(flow)) {
2923
4210
                wc->masks.nw_tos |= IP_DSCP_MASK;
2924
4211
                flow->nw_tos &= ~IP_DSCP_MASK;
2927
4214
            break;
2928
4215
 
2929
4216
        case OFPACT_SET_IP_ECN:
 
4217
            CHECK_MPLS_RECIRCULATION();
2930
4218
            if (is_ip_any(flow)) {
2931
4219
                wc->masks.nw_tos |= IP_ECN_MASK;
2932
4220
                flow->nw_tos &= ~IP_ECN_MASK;
2935
4223
            break;
2936
4224
 
2937
4225
        case OFPACT_SET_IP_TTL:
 
4226
            CHECK_MPLS_RECIRCULATION();
2938
4227
            if (is_ip_any(flow)) {
2939
4228
                wc->masks.nw_ttl = 0xff;
2940
4229
                flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2942
4231
            break;
2943
4232
 
2944
4233
        case OFPACT_SET_L4_SRC_PORT:
2945
 
            if (is_ip_any(flow)) {
 
4234
            CHECK_MPLS_RECIRCULATION();
 
4235
            if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2946
4236
                memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2947
4237
                memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2948
4238
                flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2950
4240
            break;
2951
4241
 
2952
4242
        case OFPACT_SET_L4_DST_PORT:
2953
 
            if (is_ip_any(flow)) {
 
4243
            CHECK_MPLS_RECIRCULATION();
 
4244
            if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2954
4245
                memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2955
4246
                memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2956
4247
                flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2966
4257
            break;
2967
4258
 
2968
4259
        case OFPACT_SET_QUEUE:
 
4260
            memset(&wc->masks.skb_priority, 0xff,
 
4261
                   sizeof wc->masks.skb_priority);
2969
4262
            xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2970
4263
            break;
2971
4264
 
2972
4265
        case OFPACT_POP_QUEUE:
 
4266
            memset(&wc->masks.skb_priority, 0xff,
 
4267
                   sizeof wc->masks.skb_priority);
2973
4268
            flow->skb_priority = ctx->orig_skb_priority;
2974
4269
            break;
2975
4270
 
2976
4271
        case OFPACT_REG_MOVE:
 
4272
            CHECK_MPLS_RECIRCULATION_IF(
 
4273
                mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
 
4274
                mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
2977
4275
            nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2978
4276
            break;
2979
4277
 
2980
 
        case OFPACT_REG_LOAD:
2981
 
            nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
2982
 
            break;
2983
 
 
2984
4278
        case OFPACT_SET_FIELD:
 
4279
            CHECK_MPLS_RECIRCULATION_IF(
 
4280
                mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
2985
4281
            set_field = ofpact_get_SET_FIELD(a);
2986
4282
            mf = set_field->field;
2987
4283
 
2997
4293
                       && !eth_type_mpls(flow->dl_type)) {
2998
4294
                break;
2999
4295
            }
3000
 
 
 
4296
            /* A flow may wildcard nw_frag.  Do nothing if setting a trasport
 
4297
             * header field on a packet that does not have them. */
3001
4298
            mf_mask_field_and_prereqs(mf, &wc->masks);
3002
 
            mf_set_flow_value(mf, &set_field->value, flow);
 
4299
            if (mf_are_prereqs_ok(mf, flow)) {
 
4300
                mf_set_flow_value_masked(mf, &set_field->value,
 
4301
                                         &set_field->mask, flow);
 
4302
            }
3003
4303
            break;
3004
4304
 
3005
4305
        case OFPACT_STACK_PUSH:
 
4306
            CHECK_MPLS_RECIRCULATION_IF(
 
4307
                mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
3006
4308
            nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
3007
4309
                                   &ctx->stack);
3008
4310
            break;
3009
4311
 
3010
4312
        case OFPACT_STACK_POP:
 
4313
            CHECK_MPLS_RECIRCULATION_IF(
 
4314
                mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
3011
4315
            nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
3012
4316
                                  &ctx->stack);
3013
4317
            break;
3014
4318
 
3015
4319
        case OFPACT_PUSH_MPLS:
 
4320
            /* Recirculate if it is an IP packet with a zero ttl.  This may
 
4321
             * indicate that the packet was previously MPLS and an MPLS pop
 
4322
             * action converted it to IP. In this case recirculating should
 
4323
             * reveal the IP TTL which is used as the basis for a new MPLS
 
4324
             * LSE. */
 
4325
            CHECK_MPLS_RECIRCULATION_IF(
 
4326
                !flow_count_mpls_labels(flow, wc)
 
4327
                && flow->nw_ttl == 0
 
4328
                && is_ip_any(flow));
3016
4329
            compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
3017
4330
            break;
3018
4331
 
3019
4332
        case OFPACT_POP_MPLS:
 
4333
            CHECK_MPLS_RECIRCULATION();
3020
4334
            compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
3021
4335
            break;
3022
4336
 
3023
4337
        case OFPACT_SET_MPLS_LABEL:
 
4338
            CHECK_MPLS_RECIRCULATION();
3024
4339
            compose_set_mpls_label_action(
3025
4340
                ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
3026
 
        break;
 
4341
            break;
3027
4342
 
3028
4343
        case OFPACT_SET_MPLS_TC:
 
4344
            CHECK_MPLS_RECIRCULATION();
3029
4345
            compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
3030
4346
            break;
3031
4347
 
3032
4348
        case OFPACT_SET_MPLS_TTL:
 
4349
            CHECK_MPLS_RECIRCULATION();
3033
4350
            compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
3034
4351
            break;
3035
4352
 
3036
4353
        case OFPACT_DEC_MPLS_TTL:
 
4354
            CHECK_MPLS_RECIRCULATION();
3037
4355
            if (compose_dec_mpls_ttl_action(ctx)) {
3038
4356
                return;
3039
4357
            }
3040
4358
            break;
3041
4359
 
3042
4360
        case OFPACT_DEC_TTL:
 
4361
            CHECK_MPLS_RECIRCULATION();
3043
4362
            wc->masks.nw_ttl = 0xff;
3044
4363
            if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
3045
4364
                return;
3051
4370
            break;
3052
4371
 
3053
4372
        case OFPACT_MULTIPATH:
 
4373
            CHECK_MPLS_RECIRCULATION();
3054
4374
            multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
3055
4375
            break;
3056
4376
 
3057
4377
        case OFPACT_BUNDLE:
 
4378
            CHECK_MPLS_RECIRCULATION();
3058
4379
            xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
3059
4380
            break;
3060
4381
 
3063
4384
            break;
3064
4385
 
3065
4386
        case OFPACT_LEARN:
 
4387
            CHECK_MPLS_RECIRCULATION();
3066
4388
            xlate_learn_action(ctx, ofpact_get_LEARN(a));
3067
4389
            break;
3068
4390
 
 
4391
        case OFPACT_CONJUNCTION: {
 
4392
            /* A flow with a "conjunction" action represents part of a special
 
4393
             * kind of "set membership match".  Such a flow should not actually
 
4394
             * get executed, but it could via, say, a "packet-out", even though
 
4395
             * that wouldn't be useful.  Log it to help debugging. */
 
4396
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
 
4397
            VLOG_INFO_RL(&rl, "executing no-op conjunction action");
 
4398
            break;
 
4399
        }
 
4400
 
3069
4401
        case OFPACT_EXIT:
3070
4402
            ctx->exit = true;
3071
4403
            break;
3072
4404
 
 
4405
        case OFPACT_UNROLL_XLATE: {
 
4406
            struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
 
4407
 
 
4408
            /* Restore translation context data that was stored earlier. */
 
4409
            ctx->table_id = unroll->rule_table_id;
 
4410
            ctx->rule_cookie = unroll->rule_cookie;
 
4411
            break;
 
4412
        }
3073
4413
        case OFPACT_FIN_TIMEOUT:
 
4414
            CHECK_MPLS_RECIRCULATION();
3074
4415
            memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3075
4416
            ctx->xout->has_fin_timeout = true;
3076
4417
            xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
3078
4419
 
3079
4420
        case OFPACT_CLEAR_ACTIONS:
3080
4421
            ofpbuf_clear(&ctx->action_set);
 
4422
            ctx->xin->flow.actset_output = OFPP_UNSET;
 
4423
            ctx->action_set_has_group = false;
3081
4424
            break;
3082
4425
 
3083
4426
        case OFPACT_WRITE_ACTIONS:
3097
4440
        case OFPACT_GOTO_TABLE: {
3098
4441
            struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
3099
4442
 
3100
 
            ovs_assert(ctx->table_id < ogt->table_id);
 
4443
            /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
 
4444
             * than ogt->table_id. This is to allow goto_table actions that
 
4445
             * triggered recirculation: ctx->table_id will be TBL_INTERNAL
 
4446
             * after recirculation. */
 
4447
            ovs_assert(ctx->table_id == TBL_INTERNAL
 
4448
                       || ctx->table_id < ogt->table_id);
3101
4449
            xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3102
4450
                               ogt->table_id, true, true);
3103
4451
            break;
3107
4455
            xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
3108
4456
            break;
3109
4457
        }
 
4458
 
 
4459
        /* Check if need to store this and the remaining actions for later
 
4460
         * execution. */
 
4461
        if (ctx->exit && ctx_first_recirculation_action(ctx)) {
 
4462
            recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
 
4463
                                                  ((uint8_t *)a -
 
4464
                                                   (uint8_t *)ofpacts)),
 
4465
                                  ctx);
 
4466
            break;
 
4467
        }
3110
4468
    }
3111
4469
}
3112
4470
 
3113
4471
void
3114
4472
xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
3115
 
              const struct flow *flow, struct rule_dpif *rule,
3116
 
              uint16_t tcp_flags, const struct ofpbuf *packet)
 
4473
              const struct flow *flow, ofp_port_t in_port,
 
4474
              struct rule_dpif *rule, uint16_t tcp_flags,
 
4475
              const struct dp_packet *packet)
3117
4476
{
3118
4477
    xin->ofproto = ofproto;
3119
4478
    xin->flow = *flow;
 
4479
    xin->flow.in_port.ofp_port = in_port;
 
4480
    xin->flow.actset_output = OFPP_UNSET;
3120
4481
    xin->packet = packet;
3121
4482
    xin->may_learn = packet != NULL;
3122
4483
    xin->rule = rule;
3128
4489
    xin->report_hook = NULL;
3129
4490
    xin->resubmit_stats = NULL;
3130
4491
    xin->skip_wildcards = false;
 
4492
    xin->odp_actions = NULL;
 
4493
 
 
4494
    /* Do recirc lookup. */
 
4495
    xin->recirc = flow->recirc_id
 
4496
        ? recirc_id_node_find(flow->recirc_id)
 
4497
        : NULL;
3131
4498
}
3132
4499
 
3133
4500
void
3134
4501
xlate_out_uninit(struct xlate_out *xout)
3135
4502
{
3136
4503
    if (xout) {
3137
 
        ofpbuf_uninit(&xout->odp_actions);
 
4504
        if (xout->odp_actions == &xout->odp_actions_buf) {
 
4505
            ofpbuf_uninit(xout->odp_actions);
 
4506
        }
 
4507
        xlate_out_free_recircs(xout);
3138
4508
    }
3139
4509
}
3140
4510
 
3149
4519
    xlate_out_uninit(&xout);
3150
4520
}
3151
4521
 
3152
 
static void
3153
 
xlate_report(struct xlate_ctx *ctx, const char *s)
3154
 
{
3155
 
    if (ctx->xin->report_hook) {
3156
 
        ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
3157
 
    }
3158
 
}
3159
 
 
3160
4522
void
3161
4523
xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
3162
4524
{
3168
4530
    dst->nf_output_iface = src->nf_output_iface;
3169
4531
    dst->mirrors = src->mirrors;
3170
4532
 
3171
 
    ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
 
4533
    dst->odp_actions = &dst->odp_actions_buf;
 
4534
    ofpbuf_use_stub(dst->odp_actions, dst->odp_actions_stub,
3172
4535
                    sizeof dst->odp_actions_stub);
3173
 
    ofpbuf_put(&dst->odp_actions, ofpbuf_data(&src->odp_actions),
3174
 
               ofpbuf_size(&src->odp_actions));
 
4536
    ofpbuf_put(dst->odp_actions, src->odp_actions->data, src->odp_actions->size);
3175
4537
}
3176
4538
 
3177
4539
static struct skb_priority_to_dscp *
3198
4560
    return pdscp != NULL;
3199
4561
}
3200
4562
 
 
4563
static size_t
 
4564
count_skb_priorities(const struct xport *xport)
 
4565
{
 
4566
    return hmap_count(&xport->skb_priorities);
 
4567
}
 
4568
 
3201
4569
static void
3202
4570
clear_skb_priorities(struct xport *xport)
3203
4571
{
3216
4584
    const struct nlattr *a;
3217
4585
    unsigned int left;
3218
4586
 
3219
 
    NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(&ctx->xout->odp_actions),
3220
 
                             ofpbuf_size(&ctx->xout->odp_actions)) {
 
4587
    NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions->data,
 
4588
                             ctx->xout->odp_actions->size) {
3221
4589
        if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
3222
4590
            && nl_attr_get_odp_port(a) == local_odp_port) {
3223
4591
            return true;
3226
4594
    return false;
3227
4595
}
3228
4596
 
3229
 
/* Thread safe call to xlate_actions__(). */
3230
 
void
3231
 
xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3232
 
    OVS_EXCLUDED(xlate_rwlock)
3233
 
{
3234
 
    fat_rwlock_rdlock(&xlate_rwlock);
3235
 
    xlate_actions__(xin, xout);
3236
 
    fat_rwlock_unlock(&xlate_rwlock);
3237
 
}
3238
 
 
 
4597
#if defined(__linux__)
3239
4598
/* Returns the maximum number of packets that the Linux kernel is willing to
3240
4599
 * queue up internally to certain kinds of software-implemented ports, or the
3241
4600
 * default (and rarely modified) value if it cannot be determined. */
3280
4639
    size_t left;
3281
4640
    int n = 0;
3282
4641
 
3283
 
    NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(odp_actions),
3284
 
                             ofpbuf_size(odp_actions)) {
 
4642
    NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
3285
4643
        if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
3286
4644
            n++;
3287
4645
        }
3288
4646
    }
3289
4647
    return n;
3290
4648
}
 
4649
#endif /* defined(__linux__) */
3291
4650
 
3292
4651
/* Returns true if 'odp_actions' contains more output actions than the datapath
3293
4652
 * can reliably handle in one go.  On Linux, this is the value of the
3295
4654
 * packets that the kernel is willing to queue up for processing while the
3296
4655
 * datapath is processing a set of actions. */
3297
4656
static bool
3298
 
too_many_output_actions(const struct ofpbuf *odp_actions)
 
4657
too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
3299
4658
{
3300
4659
#ifdef __linux__
3301
 
    return (ofpbuf_size(odp_actions) / NL_A_U32_SIZE > netdev_max_backlog()
 
4660
    return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
3302
4661
            && count_output_actions(odp_actions) > netdev_max_backlog());
3303
4662
#else
3304
4663
    /* OSes other than Linux might have similar limits, but we don't know how
3307
4666
#endif
3308
4667
}
3309
4668
 
3310
 
/* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3311
 
 * into datapath actions in 'odp_actions', using 'ctx'.
3312
 
 *
 
4669
/* Translates the flow, actions, or rule in 'xin' into datapath actions in
 
4670
 * 'xout'.
3313
4671
 * The caller must take responsibility for eventually freeing 'xout', with
3314
4672
 * xlate_out_uninit(). */
3315
 
static void
3316
 
xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
3317
 
    OVS_REQ_RDLOCK(xlate_rwlock)
 
4673
void
 
4674
xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3318
4675
{
3319
 
    struct flow_wildcards *wc = &xout->wc;
 
4676
    struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
4677
    struct flow_wildcards *wc = NULL;
3320
4678
    struct flow *flow = &xin->flow;
3321
4679
    struct rule_dpif *rule = NULL;
3322
4680
 
3323
 
    const struct rule_actions *actions = NULL;
3324
4681
    enum slow_path_reason special;
3325
4682
    const struct ofpact *ofpacts;
 
4683
    struct xbridge *xbridge;
3326
4684
    struct xport *in_port;
3327
4685
    struct flow orig_flow;
3328
4686
    struct xlate_ctx ctx;
3361
4719
    ctx.xout->has_fin_timeout = false;
3362
4720
    ctx.xout->nf_output_iface = NF_OUT_DROP;
3363
4721
    ctx.xout->mirrors = 0;
3364
 
    ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3365
 
                    sizeof ctx.xout->odp_actions_stub);
3366
 
    ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3367
 
 
3368
 
    ctx.xbridge = xbridge_lookup(xin->ofproto);
3369
 
    if (!ctx.xbridge) {
 
4722
    ctx.xout->n_recircs = 0;
 
4723
 
 
4724
    xout->odp_actions = xin->odp_actions;
 
4725
    if (!xout->odp_actions) {
 
4726
        xout->odp_actions = &xout->odp_actions_buf;
 
4727
        ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
 
4728
                        sizeof xout->odp_actions_stub);
 
4729
    }
 
4730
    ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
 
4731
 
 
4732
    xbridge = xbridge_lookup(xcfg, xin->ofproto);
 
4733
    if (!xbridge) {
3370
4734
        return;
3371
4735
    }
3372
 
 
 
4736
    /* 'ctx.xbridge' may be changed by action processing, whereas 'xbridge'
 
4737
     * will remain set on the original input bridge. */
 
4738
    ctx.xbridge = xbridge;
3373
4739
    ctx.rule = xin->rule;
3374
4740
 
3375
4741
    ctx.base_flow = *flow;
3376
4742
    memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3377
4743
    ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3378
4744
 
3379
 
    flow_wildcards_init_catchall(wc);
3380
 
    memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3381
 
    memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3382
 
    memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3383
 
    if (is_ip_any(flow)) {
3384
 
        wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
 
4745
    if (!xin->skip_wildcards) {
 
4746
        wc = &xout->wc;
 
4747
        flow_wildcards_init_catchall(wc);
 
4748
        memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
 
4749
        memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
 
4750
        if (is_ip_any(flow)) {
 
4751
            wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
 
4752
        }
 
4753
        if (xbridge->support.recirc) {
 
4754
            /* Always exactly match recirc_id when datapath supports
 
4755
             * recirculation.  */
 
4756
            wc->masks.recirc_id = UINT32_MAX;
 
4757
        }
 
4758
        if (xbridge->netflow) {
 
4759
            netflow_mask_wc(flow, wc);
 
4760
        }
3385
4761
    }
3386
4762
    is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
3387
4763
 
3388
4764
    tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3389
 
    if (ctx.xbridge->netflow) {
3390
 
        netflow_mask_wc(flow, wc);
3391
 
    }
3392
4765
 
3393
4766
    ctx.recurse = 0;
3394
4767
    ctx.resubmits = 0;
3395
4768
    ctx.in_group = false;
 
4769
    ctx.in_action_set = false;
3396
4770
    ctx.orig_skb_priority = flow->skb_priority;
3397
4771
    ctx.table_id = 0;
 
4772
    ctx.rule_cookie = OVS_BE64_MAX;
3398
4773
    ctx.exit = false;
3399
 
    ctx.use_recirc = false;
 
4774
    ctx.was_mpls = false;
 
4775
    ctx.recirc_action_offset = -1;
 
4776
    ctx.last_unroll_offset = -1;
 
4777
 
 
4778
    ctx.action_set_has_group = false;
 
4779
    ofpbuf_use_stub(&ctx.action_set,
 
4780
                    ctx.action_set_stub, sizeof ctx.action_set_stub);
 
4781
 
 
4782
    ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
 
4783
 
 
4784
    /* The in_port of the original packet before recirculation. */
 
4785
    in_port = get_ofp_port(xbridge, flow->in_port.ofp_port);
 
4786
 
 
4787
    if (xin->recirc) {
 
4788
        const struct recirc_id_node *recirc = xin->recirc;
 
4789
 
 
4790
        if (xin->ofpacts_len > 0 || ctx.rule) {
 
4791
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
 
4792
 
 
4793
            VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!",
 
4794
                         xin->ofpacts_len > 0
 
4795
                         ? "actions"
 
4796
                         : "rule");
 
4797
            return;
 
4798
        }
 
4799
 
 
4800
        /* Set the bridge for post-recirculation processing if needed. */
 
4801
        if (ctx.xbridge->ofproto != recirc->ofproto) {
 
4802
            struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
 
4803
            const struct xbridge *new_bridge = xbridge_lookup(xcfg,
 
4804
                                                              recirc->ofproto);
 
4805
 
 
4806
            if (OVS_UNLIKELY(!new_bridge)) {
 
4807
                /* Drop the packet if the bridge cannot be found. */
 
4808
                static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
 
4809
                VLOG_WARN_RL(&rl, "Recirculation bridge no longer exists.");
 
4810
                return;
 
4811
            }
 
4812
            ctx.xbridge = new_bridge;
 
4813
        }
 
4814
 
 
4815
        /* Set the post-recirculation table id.  Note: A table lookup is done
 
4816
         * only if there are no post-recirculation actions. */
 
4817
        ctx.table_id = recirc->table_id;
 
4818
 
 
4819
        /* Restore pipeline metadata. May change flow's in_port and other
 
4820
         * metadata to the values that existed when recirculation was
 
4821
         * triggered. */
 
4822
        recirc_metadata_to_flow(&recirc->metadata, flow);
 
4823
 
 
4824
        /* Restore stack, if any. */
 
4825
        if (recirc->stack) {
 
4826
            ofpbuf_put(&ctx.stack, recirc->stack->data, recirc->stack->size);
 
4827
        }
 
4828
 
 
4829
        /* Restore action set, if any. */
 
4830
        if (recirc->action_set_len) {
 
4831
            const struct ofpact *a;
 
4832
 
 
4833
            ofpbuf_put(&ctx.action_set, recirc->ofpacts,
 
4834
                       recirc->action_set_len);
 
4835
 
 
4836
            OFPACT_FOR_EACH(a, recirc->ofpacts, recirc->action_set_len) {
 
4837
                if (a->type == OFPACT_GROUP) {
 
4838
                    ctx.action_set_has_group = true;
 
4839
                    break;
 
4840
                }
 
4841
            }
 
4842
        }
 
4843
 
 
4844
        /* Restore recirculation actions.  If there are no actions, processing
 
4845
         * will start with a lookup in the table set above. */
 
4846
        if (recirc->ofpacts_len > recirc->action_set_len) {
 
4847
            xin->ofpacts_len = recirc->ofpacts_len - recirc->action_set_len;
 
4848
            xin->ofpacts = recirc->ofpacts +
 
4849
                recirc->action_set_len / sizeof *recirc->ofpacts;
 
4850
        }
 
4851
    } else if (OVS_UNLIKELY(flow->recirc_id)) {
 
4852
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
 
4853
 
 
4854
        VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
 
4855
                     flow->recirc_id);
 
4856
        return;
 
4857
    }
 
4858
    /* The bridge is now known so obtain its table version. */
 
4859
    ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
3400
4860
 
3401
4861
    if (!xin->ofpacts && !ctx.rule) {
3402
 
        ctx.table_id = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3403
 
                                        !xin->skip_wildcards ? wc : NULL,
3404
 
                                        &rule, ctx.xin->xcache != NULL);
 
4862
        rule = rule_dpif_lookup_from_table(ctx.xbridge->ofproto,
 
4863
                                           ctx.tables_version, flow, wc,
 
4864
                                           ctx.xin->xcache != NULL,
 
4865
                                           ctx.xin->resubmit_stats,
 
4866
                                           &ctx.table_id,
 
4867
                                           flow->in_port.ofp_port, true, true);
3405
4868
        if (ctx.xin->resubmit_stats) {
3406
4869
            rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3407
4870
        }
3412
4875
            entry->u.rule = rule;
3413
4876
        }
3414
4877
        ctx.rule = rule;
 
4878
 
 
4879
        if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
 
4880
            ctx.xin->resubmit_hook(ctx.xin, rule, 0);
 
4881
        }
3415
4882
    }
3416
4883
    xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3417
4884
 
3419
4886
        ofpacts = xin->ofpacts;
3420
4887
        ofpacts_len = xin->ofpacts_len;
3421
4888
    } else if (ctx.rule) {
3422
 
        actions = rule_dpif_get_actions(ctx.rule);
 
4889
        const struct rule_actions *actions = rule_dpif_get_actions(ctx.rule);
 
4890
 
3423
4891
        ofpacts = actions->ofpacts;
3424
4892
        ofpacts_len = actions->ofpacts_len;
 
4893
 
 
4894
        ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
3425
4895
    } else {
3426
4896
        OVS_NOT_REACHED();
3427
4897
    }
3428
4898
 
3429
 
    ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3430
 
    ofpbuf_use_stub(&ctx.action_set,
3431
 
                    ctx.action_set_stub, sizeof ctx.action_set_stub);
3432
 
 
3433
 
    if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
 
4899
    if (mbridge_has_mirrors(xbridge->mbridge)) {
3434
4900
        /* Do this conditionally because the copy is expensive enough that it
3435
4901
         * shows up in profiles. */
3436
4902
        orig_flow = *flow;
3437
4903
    }
3438
4904
 
3439
 
    if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3440
 
        switch (ctx.xbridge->frag) {
3441
 
        case OFPC_FRAG_NORMAL:
3442
 
            /* We must pretend that transport ports are unavailable. */
3443
 
            flow->tp_src = ctx.base_flow.tp_src = htons(0);
3444
 
            flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3445
 
            break;
3446
 
 
3447
 
        case OFPC_FRAG_DROP:
3448
 
            return;
3449
 
 
3450
 
        case OFPC_FRAG_REASM:
3451
 
            OVS_NOT_REACHED();
3452
 
 
3453
 
        case OFPC_FRAG_NX_MATCH:
3454
 
            /* Nothing to do. */
3455
 
            break;
3456
 
 
3457
 
        case OFPC_INVALID_TTL_TO_CONTROLLER:
3458
 
            OVS_NOT_REACHED();
3459
 
        }
3460
 
    }
3461
 
 
3462
 
    in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3463
 
    if (in_port && in_port->is_tunnel) {
 
4905
    /* Tunnel stats only for non-recirculated packets. */
 
4906
    if (!xin->recirc && in_port && in_port->is_tunnel) {
3464
4907
        if (ctx.xin->resubmit_stats) {
3465
4908
            netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3466
4909
            if (in_port->bfd) {
3476
4919
        }
3477
4920
    }
3478
4921
 
3479
 
    special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3480
 
    if (special) {
 
4922
    /* Do not perform special processing on recirculated packets,
 
4923
     * as recirculated packets are not really received by the bridge. */
 
4924
    if (!xin->recirc &&
 
4925
        (special = process_special(&ctx, flow, in_port, ctx.xin->packet))) {
3481
4926
        ctx.xout->slow |= special;
3482
4927
    } else {
3483
4928
        size_t sample_actions_len;
3484
4929
 
3485
4930
        if (flow->in_port.ofp_port
3486
 
            != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
 
4931
            != vsp_realdev_to_vlandev(xbridge->ofproto,
3487
4932
                                      flow->in_port.ofp_port,
3488
4933
                                      flow->vlan_tci)) {
3489
4934
            ctx.base_flow.vlan_tci = 0;
3490
4935
        }
3491
4936
 
3492
 
        add_sflow_action(&ctx);
3493
 
        add_ipfix_action(&ctx);
3494
 
        sample_actions_len = ofpbuf_size(&ctx.xout->odp_actions);
 
4937
        /* Sampling is done only for packets really received by the bridge. */
 
4938
        if (!xin->recirc) {
 
4939
            add_sflow_action(&ctx);
 
4940
            add_ipfix_action(&ctx);
 
4941
            sample_actions_len = ctx.xout->odp_actions->size;
 
4942
        } else {
 
4943
            sample_actions_len = 0;
 
4944
        }
3495
4945
 
3496
4946
        if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3497
4947
            do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3498
4948
 
3499
4949
            /* We've let OFPP_NORMAL and the learning action look at the
3500
4950
             * packet, so drop it now if forwarding is disabled. */
3501
 
            if (in_port && !xport_stp_forward_state(in_port)) {
3502
 
                ofpbuf_set_size(&ctx.xout->odp_actions, sample_actions_len);
3503
 
            }
3504
 
        }
3505
 
 
3506
 
        if (ofpbuf_size(&ctx.action_set)) {
3507
 
            xlate_action_set(&ctx);
3508
 
        }
3509
 
 
3510
 
        if (ctx.xbridge->has_in_band
 
4951
            if (in_port && (!xport_stp_forward_state(in_port) ||
 
4952
                            !xport_rstp_forward_state(in_port))) {
 
4953
                /* Drop all actions added by do_xlate_actions() above. */
 
4954
                ctx.xout->odp_actions->size = sample_actions_len;
 
4955
 
 
4956
                /* Undo changes that may have been done for recirculation. */
 
4957
                if (exit_recirculates(&ctx)) {
 
4958
                    ctx.action_set.size = ctx.recirc_action_offset;
 
4959
                    ctx.recirc_action_offset = -1;
 
4960
                    ctx.last_unroll_offset = -1;
 
4961
                }
 
4962
            } else if (ctx.action_set.size) {
 
4963
                /* Translate action set only if not dropping the packet and
 
4964
                 * not recirculating. */
 
4965
                if (!exit_recirculates(&ctx)) {
 
4966
                    xlate_action_set(&ctx);
 
4967
                }
 
4968
            }
 
4969
            /* Check if need to recirculate. */
 
4970
            if (exit_recirculates(&ctx)) {
 
4971
                compose_recirculate_action(&ctx);
 
4972
            }
 
4973
        }
 
4974
 
 
4975
        /* Output only fully processed packets. */
 
4976
        if (!exit_recirculates(&ctx)
 
4977
            && xbridge->has_in_band
3511
4978
            && in_band_must_output_to_local_port(flow)
3512
4979
            && !actions_output_to_local_port(&ctx)) {
3513
 
            compose_output_action(&ctx, OFPP_LOCAL);
3514
 
        }
3515
 
 
3516
 
        fix_sflow_action(&ctx);
3517
 
 
3518
 
        if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
 
4980
            compose_output_action(&ctx, OFPP_LOCAL, NULL);
 
4981
        }
 
4982
 
 
4983
        if (!xin->recirc) {
 
4984
            fix_sflow_action(&ctx);
 
4985
        }
 
4986
        /* Only mirror fully processed packets. */
 
4987
        if (!exit_recirculates(&ctx)
 
4988
            && mbridge_has_mirrors(xbridge->mbridge)) {
3519
4989
            add_mirror_actions(&ctx, &orig_flow);
3520
4990
        }
3521
4991
    }
3522
4992
 
3523
 
    if (nl_attr_oversized(ofpbuf_size(&ctx.xout->odp_actions))) {
 
4993
    if (nl_attr_oversized(ctx.xout->odp_actions->size)) {
3524
4994
        /* These datapath actions are too big for a Netlink attribute, so we
3525
4995
         * can't hand them to the kernel directly.  dpif_execute() can execute
3526
4996
         * them one by one with help, so just mark the result as SLOW_ACTION to
3527
4997
         * prevent the flow from being installed. */
3528
4998
        COVERAGE_INC(xlate_actions_oversize);
3529
4999
        ctx.xout->slow |= SLOW_ACTION;
3530
 
    } else if (too_many_output_actions(&ctx.xout->odp_actions)) {
 
5000
    } else if (too_many_output_actions(ctx.xout->odp_actions)) {
3531
5001
        COVERAGE_INC(xlate_actions_too_many_output);
3532
5002
        ctx.xout->slow |= SLOW_ACTION;
3533
5003
    }
3534
5004
 
3535
 
    if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
 
5005
    /* Update mirror stats only for packets really received by the bridge. */
 
5006
    if (!xin->recirc && mbridge_has_mirrors(xbridge->mbridge)) {
3536
5007
        if (ctx.xin->resubmit_stats) {
3537
 
            mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
 
5008
            mirror_update_stats(xbridge->mbridge, xout->mirrors,
3538
5009
                                ctx.xin->resubmit_stats->n_packets,
3539
5010
                                ctx.xin->resubmit_stats->n_bytes);
3540
5011
        }
3542
5013
            struct xc_entry *entry;
3543
5014
 
3544
5015
            entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
3545
 
            entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
 
5016
            entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
3546
5017
            entry->u.mirror.mirrors = xout->mirrors;
3547
5018
        }
3548
5019
    }
3549
5020
 
3550
 
    if (ctx.xbridge->netflow) {
 
5021
    /* Do netflow only for packets really received by the bridge. */
 
5022
    if (!xin->recirc && xbridge->netflow) {
3551
5023
        /* Only update netflow if we don't have controller flow.  We don't
3552
5024
         * report NetFlow expiration messages for such facets because they
3553
5025
         * are just part of the control logic for the network, not real
3556
5028
            || ofpacts->type != OFPACT_CONTROLLER
3557
5029
            || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3558
5030
            if (ctx.xin->resubmit_stats) {
3559
 
                netflow_flow_update(ctx.xbridge->netflow, flow,
 
5031
                netflow_flow_update(xbridge->netflow, flow,
3560
5032
                                    xout->nf_output_iface,
3561
5033
                                    ctx.xin->resubmit_stats);
3562
5034
            }
3564
5036
                struct xc_entry *entry;
3565
5037
 
3566
5038
                entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
3567
 
                entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
 
5039
                entry->u.nf.netflow = netflow_ref(xbridge->netflow);
3568
5040
                entry->u.nf.flow = xmemdup(flow, sizeof *flow);
3569
5041
                entry->u.nf.iface = xout->nf_output_iface;
3570
5042
            }
3574
5046
    ofpbuf_uninit(&ctx.stack);
3575
5047
    ofpbuf_uninit(&ctx.action_set);
3576
5048
 
3577
 
 
3578
 
    /* Clear the metadata and register wildcard masks, because we won't
3579
 
     * use non-header fields as part of the cache. */
3580
 
    flow_wildcards_clear_non_packet_fields(wc);
3581
 
 
3582
 
    /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields.  struct flow uses
3583
 
     * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
3584
 
     * these fields.  The datapath interface, on the other hand, represents
3585
 
     * them with just 8 bits each.  This means that if the high 8 bits of the
3586
 
     * masks for these fields somehow become set, then they will get chopped
3587
 
     * off by a round trip through the datapath, and revalidation will spot
3588
 
     * that as an inconsistency and delete the flow.  Avoid the problem here by
3589
 
     * making sure that only the low 8 bits of either field can be unwildcarded
3590
 
     * for ICMP.
3591
 
     */
3592
 
    if (is_icmp) {
3593
 
        wc->masks.tp_src &= htons(UINT8_MAX);
3594
 
        wc->masks.tp_dst &= htons(UINT8_MAX);
3595
 
    }
3596
 
    /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
3597
 
    if (wc->masks.vlan_tci) {
3598
 
        wc->masks.vlan_tci |= htons(VLAN_CFI);
 
5049
    if (wc) {
 
5050
        /* Clear the metadata and register wildcard masks, because we won't
 
5051
         * use non-header fields as part of the cache. */
 
5052
        flow_wildcards_clear_non_packet_fields(wc);
 
5053
 
 
5054
        /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields.  struct flow
 
5055
         * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
 
5056
         * represent these fields.  The datapath interface, on the other hand,
 
5057
         * represents them with just 8 bits each.  This means that if the high
 
5058
         * 8 bits of the masks for these fields somehow become set, then they
 
5059
         * will get chopped off by a round trip through the datapath, and
 
5060
         * revalidation will spot that as an inconsistency and delete the flow.
 
5061
         * Avoid the problem here by making sure that only the low 8 bits of
 
5062
         * either field can be unwildcarded for ICMP.
 
5063
         */
 
5064
        if (is_icmp) {
 
5065
            wc->masks.tp_src &= htons(UINT8_MAX);
 
5066
            wc->masks.tp_dst &= htons(UINT8_MAX);
 
5067
        }
 
5068
        /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
 
5069
        if (wc->masks.vlan_tci) {
 
5070
            wc->masks.vlan_tci |= htons(VLAN_CFI);
 
5071
        }
3599
5072
    }
3600
5073
}
3601
5074
 
3603
5076
 * May modify 'packet'.
3604
5077
 * Returns 0 if successful, otherwise a positive errno value. */
3605
5078
int
3606
 
xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
 
5079
xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
3607
5080
{
 
5081
    struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3608
5082
    struct xport *xport;
3609
5083
    struct ofpact_output output;
3610
5084
    struct flow flow;
3611
5085
 
3612
5086
    ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3613
5087
    /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3614
 
    flow_extract(packet, NULL, &flow);
 
5088
    flow_extract(packet, &flow);
3615
5089
    flow.in_port.ofp_port = OFPP_NONE;
3616
5090
 
3617
 
    fat_rwlock_rdlock(&xlate_rwlock);
3618
 
    xport = xport_lookup(ofport);
 
5091
    xport = xport_lookup(xcfg, ofport);
3619
5092
    if (!xport) {
3620
 
        fat_rwlock_unlock(&xlate_rwlock);
3621
5093
        return EINVAL;
3622
5094
    }
3623
5095
    output.port = xport->ofp_port;
3624
5096
    output.max_len = 0;
3625
 
    fat_rwlock_unlock(&xlate_rwlock);
3626
5097
 
3627
5098
    return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3628
5099
                                        &output.ofpact, sizeof output,
3666
5137
static void
3667
5138
xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
3668
5139
{
 
5140
    struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3669
5141
    struct xbridge *xbridge;
3670
5142
    struct xbundle *xbundle;
3671
5143
    struct flow_wildcards wc;
3672
5144
 
3673
 
    xbridge = xbridge_lookup(ofproto);
 
5145
    xbridge = xbridge_lookup(xcfg, ofproto);
3674
5146
    if (!xbridge) {
3675
5147
        return;
3676
5148
    }
3686
5158
 
3687
5159
/* Push stats and perform side effects of flow translation. */
3688
5160
void
3689
 
xlate_push_stats(struct xlate_cache *xcache, bool may_learn,
 
5161
xlate_push_stats(struct xlate_cache *xcache,
3690
5162
                 const struct dpif_flow_stats *stats)
3691
5163
{
3692
5164
    struct xc_entry *entry;
3693
5165
    struct ofpbuf entries = xcache->entries;
 
5166
    uint8_t dmac[ETH_ADDR_LEN];
 
5167
 
 
5168
    if (!stats->n_packets) {
 
5169
        return;
 
5170
    }
3694
5171
 
3695
5172
    XC_ENTRY_FOR_EACH (entry, entries, xcache) {
3696
5173
        switch (entry->type) {
3714
5191
                                stats->n_packets, stats->n_bytes);
3715
5192
            break;
3716
5193
        case XC_LEARN:
3717
 
            if (may_learn) {
3718
 
                ofproto_dpif_flow_mod(entry->u.learn.ofproto,
3719
 
                                      entry->u.learn.fm);
3720
 
            }
 
5194
            ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
3721
5195
            break;
3722
5196
        case XC_NORMAL:
3723
5197
            xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
3727
5201
            xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
3728
5202
                                entry->u.fin.idle, entry->u.fin.hard);
3729
5203
            break;
 
5204
        case XC_GROUP:
 
5205
            group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
 
5206
                                    stats);
 
5207
            break;
 
5208
        case XC_TNL_ARP:
 
5209
            /* Lookup arp to avoid arp timeout. */
 
5210
            tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
 
5211
            break;
3730
5212
        default:
3731
5213
            OVS_NOT_REACHED();
3732
5214
        }
3794
5276
            /* 'u.fin.rule' is always already held as a XC_RULE, which
3795
5277
             * has already released it's reference above. */
3796
5278
            break;
 
5279
        case XC_GROUP:
 
5280
            group_dpif_unref(entry->u.group.group);
 
5281
            break;
 
5282
        case XC_TNL_ARP:
 
5283
            break;
3797
5284
        default:
3798
5285
            OVS_NOT_REACHED();
3799
5286
        }