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

« back to all changes in this revision

Viewing changes to ofproto/ofproto-dpif-upcall.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:
21
21
 
22
22
#include "connmgr.h"
23
23
#include "coverage.h"
 
24
#include "cmap.h"
24
25
#include "dpif.h"
25
26
#include "dynamic-string.h"
26
27
#include "fail-open.h"
37
38
#include "poll-loop.h"
38
39
#include "seq.h"
39
40
#include "unixctl.h"
40
 
#include "vlog.h"
 
41
#include "openvswitch/vlog.h"
41
42
 
42
43
#define MAX_QUEUE_LENGTH 512
43
 
#define FLOW_MISS_MAX_BATCH 50
 
44
#define UPCALL_MAX_BATCH 64
44
45
#define REVALIDATE_MAX_BATCH 50
45
46
 
46
47
VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall);
47
48
 
48
 
COVERAGE_DEFINE(upcall_duplicate_flow);
 
49
COVERAGE_DEFINE(dumped_duplicate_flow);
 
50
COVERAGE_DEFINE(dumped_new_flow);
 
51
COVERAGE_DEFINE(handler_duplicate_upcall);
 
52
COVERAGE_DEFINE(upcall_ukey_contention);
49
53
COVERAGE_DEFINE(revalidate_missed_dp_flow);
50
54
 
51
55
/* A thread that reads upcalls from dpif, forwards each upcall's packet,
56
60
    uint32_t handler_id;               /* Handler id. */
57
61
};
58
62
 
 
63
/* In the absence of a multiple-writer multiple-reader datastructure for
 
64
 * storing ukeys, we use a large number of cmaps, each with its own lock for
 
65
 * writing. */
 
66
#define N_UMAPS 512 /* per udpif. */
 
67
struct umap {
 
68
    struct ovs_mutex mutex;            /* Take for writing to the following. */
 
69
    struct cmap cmap;                  /* Datapath flow keys. */
 
70
};
 
71
 
59
72
/* A thread that processes datapath flows, updates OpenFlow statistics, and
60
73
 * updates or removes them if necessary. */
61
74
struct revalidator {
62
75
    struct udpif *udpif;               /* Parent udpif. */
63
76
    pthread_t thread;                  /* Thread ID. */
64
77
    unsigned int id;                   /* ovsthread_id_self(). */
65
 
    struct hmap *ukeys;                /* Points into udpif->ukeys for this
66
 
                                          revalidator. Used for GC phase. */
67
78
};
68
79
 
69
80
/* An upcall handler for ofproto_dpif.
83
94
 *      them.
84
95
 */
85
96
struct udpif {
86
 
    struct list list_node;             /* In all_udpifs list. */
 
97
    struct ovs_list list_node;         /* In all_udpifs list. */
87
98
 
88
99
    struct dpif *dpif;                 /* Datapath handle. */
89
100
    struct dpif_backer *backer;        /* Opaque dpif_backer pointer. */
90
101
 
91
 
    uint32_t secret;                   /* Random seed for upcall hash. */
92
 
 
93
102
    struct handler *handlers;          /* Upcall handlers. */
94
103
    size_t n_handlers;
95
104
 
100
109
 
101
110
    /* Revalidation. */
102
111
    struct seq *reval_seq;             /* Incremented to force revalidation. */
103
 
    bool need_revalidate;              /* As indicated by 'reval_seq'. */
104
112
    bool reval_exit;                   /* Set by leader on 'exit_latch. */
105
113
    struct ovs_barrier reval_barrier;  /* Barrier used by revalidators. */
106
 
    struct dpif_flow_dump dump;        /* DPIF flow dump state. */
 
114
    struct dpif_flow_dump *dump;       /* DPIF flow dump state. */
107
115
    long long int dump_duration;       /* Duration of the last flow dump. */
108
116
    struct seq *dump_seq;              /* Increments each dump iteration. */
 
117
    atomic_bool enable_ufid;           /* If true, skip dumping flow attrs. */
109
118
 
110
 
    /* There are 'n_revalidators' ukey hmaps. Each revalidator retains a
111
 
     * reference to one of these for garbage collection.
 
119
    /* There are 'N_UMAPS' maps containing 'struct udpif_key' elements.
112
120
     *
113
121
     * During the flow dump phase, revalidators insert into these with a random
114
122
     * distribution. During the garbage collection phase, each revalidator
115
 
     * takes care of garbage collecting one of these hmaps. */
116
 
    struct {
117
 
        struct ovs_mutex mutex;        /* Guards the following. */
118
 
        struct hmap hmap OVS_GUARDED;  /* Datapath flow keys. */
119
 
    } *ukeys;
 
123
     * takes care of garbage collecting a slice of these maps. */
 
124
    struct umap *ukeys;
120
125
 
121
126
    /* Datapath flow statistics. */
122
127
    unsigned int max_n_flows;
126
131
    atomic_uint flow_limit;            /* Datapath flow hard limit. */
127
132
 
128
133
    /* n_flows_mutex prevents multiple threads updating these concurrently. */
129
 
    atomic_ulong n_flows;           /* Number of flows in the datapath. */
 
134
    atomic_uint n_flows;               /* Number of flows in the datapath. */
130
135
    atomic_llong n_flows_timestamp;    /* Last time n_flows was updated. */
131
136
    struct ovs_mutex n_flows_mutex;
 
137
 
 
138
    /* Following fields are accessed and modified only from the main thread. */
 
139
    struct unixctl_conn **conns;       /* Connections waiting on dump_seq. */
 
140
    uint64_t conn_seq;                 /* Corresponds to 'dump_seq' when
 
141
                                          conns[n_conns-1] was stored. */
 
142
    size_t n_conns;                    /* Number of connections waiting. */
132
143
};
133
144
 
134
145
enum upcall_type {
140
151
};
141
152
 
142
153
struct upcall {
143
 
    struct flow_miss *flow_miss;    /* This upcall's flow_miss. */
144
 
 
145
 
    /* Raw upcall plus data for keeping track of the memory backing it. */
146
 
    struct dpif_upcall dpif_upcall; /* As returned by dpif_recv() */
147
 
    struct ofpbuf upcall_buf;       /* Owns some data in 'dpif_upcall'. */
148
 
    uint64_t upcall_stub[512 / 8];  /* Buffer to reduce need for malloc(). */
 
154
    struct ofproto_dpif *ofproto;  /* Parent ofproto. */
 
155
    const struct recirc_id_node *recirc; /* Recirculation context. */
 
156
    bool have_recirc_ref;                /* Reference held on recirc ctx? */
 
157
 
 
158
    /* The flow and packet are only required to be constant when using
 
159
     * dpif-netdev.  If a modification is absolutely necessary, a const cast
 
160
     * may be used with other datapaths. */
 
161
    const struct flow *flow;       /* Parsed representation of the packet. */
 
162
    const ovs_u128 *ufid;          /* Unique identifier for 'flow'. */
 
163
    unsigned pmd_id;               /* Datapath poll mode driver id. */
 
164
    const struct dp_packet *packet;   /* Packet associated with this upcall. */
 
165
    ofp_port_t in_port;            /* OpenFlow in port, or OFPP_NONE. */
 
166
 
 
167
    enum dpif_upcall_type type;    /* Datapath type of the upcall. */
 
168
    const struct nlattr *userdata; /* Userdata for DPIF_UC_ACTION Upcalls. */
 
169
 
 
170
    bool xout_initialized;         /* True if 'xout' must be uninitialized. */
 
171
    struct xlate_out xout;         /* Result of xlate_actions(). */
 
172
    struct ofpbuf put_actions;     /* Actions 'put' in the fastapath. */
 
173
 
 
174
    struct dpif_ipfix *ipfix;      /* IPFIX pointer or NULL. */
 
175
    struct dpif_sflow *sflow;      /* SFlow pointer or NULL. */
 
176
 
 
177
    bool vsp_adjusted;             /* 'packet' and 'flow' were adjusted for
 
178
                                      VLAN splinters if true. */
 
179
 
 
180
    struct udpif_key *ukey;        /* Revalidator flow cache. */
 
181
    bool ukey_persists;            /* Set true to keep 'ukey' beyond the
 
182
                                      lifetime of this upcall. */
 
183
 
 
184
    uint64_t dump_seq;             /* udpif->dump_seq at translation time. */
 
185
    uint64_t reval_seq;            /* udpif->reval_seq at translation time. */
 
186
 
 
187
    /* Not used by the upcall callback interface. */
 
188
    const struct nlattr *key;      /* Datapath flow key. */
 
189
    size_t key_len;                /* Datapath flow key length. */
 
190
    const struct nlattr *out_tun_key;  /* Datapath output tunnel key. */
149
191
};
150
192
 
151
193
/* 'udpif_key's are responsible for tracking the little bit of state udpif
152
194
 * needs to do flow expiration which can't be pulled directly from the
153
 
 * datapath.  They may be created or maintained by any revalidator during
154
 
 * the dump phase, but are owned by a single revalidator, and are destroyed
155
 
 * by that revalidator during the garbage-collection phase.
 
195
 * datapath.  They may be created by any handler or revalidator thread at any
 
196
 * time, and read by any revalidator during the dump phase. They are however
 
197
 * each owned by a single revalidator which takes care of destroying them
 
198
 * during the garbage-collection phase.
156
199
 *
157
 
 * While some elements of a udpif_key are protected by a mutex, the ukey itself
158
 
 * is not.  Therefore it is not safe to destroy a udpif_key except when all
159
 
 * revalidators are in garbage collection phase, or they aren't running. */
 
200
 * The mutex within the ukey protects some members of the ukey. The ukey
 
201
 * itself is protected by RCU and is held within a umap in the parent udpif.
 
202
 * Adding or removing a ukey from a umap is only safe when holding the
 
203
 * corresponding umap lock. */
160
204
struct udpif_key {
161
 
    struct hmap_node hmap_node;     /* In parent revalidator 'ukeys' map. */
 
205
    struct cmap_node cmap_node;     /* In parent revalidator 'ukeys' map. */
162
206
 
163
207
    /* These elements are read only once created, and therefore aren't
164
208
     * protected by a mutex. */
165
209
    const struct nlattr *key;      /* Datapath flow key. */
166
210
    size_t key_len;                /* Length of 'key'. */
 
211
    const struct nlattr *mask;     /* Datapath flow mask. */
 
212
    size_t mask_len;               /* Length of 'mask'. */
 
213
    struct ofpbuf *actions;        /* Datapath flow actions as nlattrs. */
 
214
    ovs_u128 ufid;                 /* Unique flow identifier. */
 
215
    bool ufid_present;             /* True if 'ufid' is in datapath. */
 
216
    uint32_t hash;                 /* Pre-computed hash for 'key'. */
 
217
    unsigned pmd_id;               /* Datapath poll mode driver id. */
167
218
 
168
219
    struct ovs_mutex mutex;                   /* Guards the following. */
169
220
    struct dpif_flow_stats stats OVS_GUARDED; /* Last known stats.*/
170
221
    long long int created OVS_GUARDED;        /* Estimate of creation time. */
171
 
    bool mark OVS_GUARDED;                    /* For mark and sweep garbage
172
 
                                                 collection. */
 
222
    uint64_t dump_seq OVS_GUARDED;            /* Tracks udpif->dump_seq. */
 
223
    uint64_t reval_seq OVS_GUARDED;           /* Tracks udpif->reval_seq. */
173
224
    bool flow_exists OVS_GUARDED;             /* Ensures flows are only deleted
174
225
                                                 once. */
175
226
 
176
227
    struct xlate_cache *xcache OVS_GUARDED;   /* Cache for xlate entries that
177
228
                                               * are affected by this ukey.
178
229
                                               * Used for stats and learning.*/
179
 
    struct odputil_keybuf key_buf;            /* Memory for 'key'. */
 
230
    union {
 
231
        struct odputil_keybuf buf;
 
232
        struct nlattr nla;
 
233
    } keybuf, maskbuf;
 
234
 
 
235
    /* Recirculation IDs with references held by the ukey. */
 
236
    unsigned n_recircs;
 
237
    uint32_t recircs[];   /* 'n_recircs' id's for which references are held. */
180
238
};
181
239
 
182
 
/* Flow miss batching.
183
 
 *
184
 
 * Some dpifs implement operations faster when you hand them off in a batch.
185
 
 * To allow batching, "struct flow_miss" queues the dpif-related work needed
186
 
 * for a given flow.  Each "struct flow_miss" corresponds to sending one or
187
 
 * more packets, plus possibly installing the flow in the dpif. */
188
 
struct flow_miss {
189
 
    struct hmap_node hmap_node;
190
 
    struct ofproto_dpif *ofproto;
191
 
 
192
 
    struct flow flow;
193
 
    const struct nlattr *key;
194
 
    size_t key_len;
195
 
    enum dpif_upcall_type upcall_type;
196
 
    struct dpif_flow_stats stats;
197
 
    odp_port_t odp_in_port;
198
 
 
199
 
    uint64_t slow_path_buf[128 / 8];
200
 
    struct odputil_keybuf mask_buf;
201
 
 
202
 
    struct xlate_out xout;
203
 
 
204
 
    bool put;
 
240
/* Datapath operation with optional ukey attached. */
 
241
struct ukey_op {
 
242
    struct udpif_key *ukey;
 
243
    struct dpif_flow_stats stats; /* Stats for 'op'. */
 
244
    struct dpif_op dop;           /* Flow operation. */
205
245
};
206
246
 
207
247
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
208
 
static struct list all_udpifs = LIST_INITIALIZER(&all_udpifs);
 
248
static struct ovs_list all_udpifs = OVS_LIST_INITIALIZER(&all_udpifs);
209
249
 
210
 
static size_t read_upcalls(struct handler *,
211
 
                           struct upcall upcalls[FLOW_MISS_MAX_BATCH],
212
 
                           struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH],
213
 
                           struct hmap *);
214
 
static void handle_upcalls(struct handler *, struct hmap *, struct upcall *,
215
 
                           size_t n_upcalls);
 
250
static size_t recv_upcalls(struct handler *);
 
251
static int process_upcall(struct udpif *, struct upcall *,
 
252
                          struct ofpbuf *odp_actions);
 
253
static void handle_upcalls(struct udpif *, struct upcall *, size_t n_upcalls);
216
254
static void udpif_stop_threads(struct udpif *);
217
255
static void udpif_start_threads(struct udpif *, size_t n_handlers,
218
256
                                size_t n_revalidators);
228
266
                                             const char *argv[], void *aux);
229
267
static void upcall_unixctl_enable_megaflows(struct unixctl_conn *, int argc,
230
268
                                            const char *argv[], void *aux);
 
269
static void upcall_unixctl_disable_ufid(struct unixctl_conn *, int argc,
 
270
                                              const char *argv[], void *aux);
 
271
static void upcall_unixctl_enable_ufid(struct unixctl_conn *, int argc,
 
272
                                             const char *argv[], void *aux);
231
273
static void upcall_unixctl_set_flow_limit(struct unixctl_conn *conn, int argc,
232
274
                                            const char *argv[], void *aux);
233
 
 
234
 
static struct udpif_key *ukey_create(const struct nlattr *key, size_t key_len,
235
 
                                     long long int used);
236
 
static void ukey_delete(struct revalidator *, struct udpif_key *);
 
275
static void upcall_unixctl_dump_wait(struct unixctl_conn *conn, int argc,
 
276
                                     const char *argv[], void *aux);
 
277
static void upcall_unixctl_purge(struct unixctl_conn *conn, int argc,
 
278
                                 const char *argv[], void *aux);
 
279
 
 
280
static struct udpif_key *ukey_create_from_upcall(struct upcall *);
 
281
static int ukey_create_from_dpif_flow(const struct udpif *,
 
282
                                      const struct dpif_flow *,
 
283
                                      struct udpif_key **);
 
284
static bool ukey_install_start(struct udpif *, struct udpif_key *ukey);
 
285
static bool ukey_install_finish(struct udpif_key *ukey, int error);
 
286
static bool ukey_install(struct udpif *udpif, struct udpif_key *ukey);
 
287
static struct udpif_key *ukey_lookup(struct udpif *udpif,
 
288
                                     const ovs_u128 *ufid);
 
289
static int ukey_acquire(struct udpif *, const struct dpif_flow *,
 
290
                        struct udpif_key **result, int *error);
 
291
static void ukey_delete__(struct udpif_key *);
 
292
static void ukey_delete(struct umap *, struct udpif_key *);
 
293
static enum upcall_type classify_upcall(enum dpif_upcall_type type,
 
294
                                        const struct nlattr *userdata);
 
295
 
 
296
static int upcall_receive(struct upcall *, const struct dpif_backer *,
 
297
                          const struct dp_packet *packet, enum dpif_upcall_type,
 
298
                          const struct nlattr *userdata, const struct flow *,
 
299
                          const ovs_u128 *ufid, const unsigned pmd_id);
 
300
static void upcall_uninit(struct upcall *);
 
301
 
 
302
static upcall_callback upcall_cb;
237
303
 
238
304
static atomic_bool enable_megaflows = ATOMIC_VAR_INIT(true);
 
305
static atomic_bool enable_ufid = ATOMIC_VAR_INIT(true);
239
306
 
240
 
struct udpif *
241
 
udpif_create(struct dpif_backer *backer, struct dpif *dpif)
 
307
void
 
308
udpif_init(void)
242
309
{
243
310
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
244
 
    struct udpif *udpif = xzalloc(sizeof *udpif);
245
 
 
246
311
    if (ovsthread_once_start(&once)) {
247
312
        unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show,
248
313
                                 NULL);
250
315
                                 upcall_unixctl_disable_megaflows, NULL);
251
316
        unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
252
317
                                 upcall_unixctl_enable_megaflows, NULL);
 
318
        unixctl_command_register("upcall/disable-ufid", "", 0, 0,
 
319
                                 upcall_unixctl_disable_ufid, NULL);
 
320
        unixctl_command_register("upcall/enable-ufid", "", 0, 0,
 
321
                                 upcall_unixctl_enable_ufid, NULL);
253
322
        unixctl_command_register("upcall/set-flow-limit", "", 1, 1,
254
323
                                 upcall_unixctl_set_flow_limit, NULL);
 
324
        unixctl_command_register("revalidator/wait", "", 0, 0,
 
325
                                 upcall_unixctl_dump_wait, NULL);
 
326
        unixctl_command_register("revalidator/purge", "", 0, 0,
 
327
                                 upcall_unixctl_purge, NULL);
255
328
        ovsthread_once_done(&once);
256
329
    }
 
330
}
 
331
 
 
332
struct udpif *
 
333
udpif_create(struct dpif_backer *backer, struct dpif *dpif)
 
334
{
 
335
    struct udpif *udpif = xzalloc(sizeof *udpif);
257
336
 
258
337
    udpif->dpif = dpif;
259
338
    udpif->backer = backer;
260
339
    atomic_init(&udpif->flow_limit, MIN(ofproto_flow_limit, 10000));
261
 
    udpif->secret = random_uint32();
262
340
    udpif->reval_seq = seq_create();
263
341
    udpif->dump_seq = seq_create();
264
342
    latch_init(&udpif->exit_latch);
265
343
    list_push_back(&all_udpifs, &udpif->list_node);
 
344
    atomic_init(&udpif->enable_ufid, false);
266
345
    atomic_init(&udpif->n_flows, 0);
267
346
    atomic_init(&udpif->n_flows_timestamp, LLONG_MIN);
268
347
    ovs_mutex_init(&udpif->n_flows_mutex);
 
348
    udpif->ukeys = xmalloc(N_UMAPS * sizeof *udpif->ukeys);
 
349
    for (int i = 0; i < N_UMAPS; i++) {
 
350
        cmap_init(&udpif->ukeys[i].cmap);
 
351
        ovs_mutex_init(&udpif->ukeys[i].mutex);
 
352
    }
 
353
 
 
354
    dpif_register_upcall_cb(dpif, upcall_cb, udpif);
269
355
 
270
356
    return udpif;
271
357
}
272
358
 
273
359
void
 
360
udpif_run(struct udpif *udpif)
 
361
{
 
362
    if (udpif->conns && udpif->conn_seq != seq_read(udpif->dump_seq)) {
 
363
        int i;
 
364
 
 
365
        for (i = 0; i < udpif->n_conns; i++) {
 
366
            unixctl_command_reply(udpif->conns[i], NULL);
 
367
        }
 
368
        free(udpif->conns);
 
369
        udpif->conns = NULL;
 
370
        udpif->n_conns = 0;
 
371
    }
 
372
}
 
373
 
 
374
void
274
375
udpif_destroy(struct udpif *udpif)
275
376
{
276
377
    udpif_stop_threads(udpif);
277
378
 
 
379
    for (int i = 0; i < N_UMAPS; i++) {
 
380
        cmap_destroy(&udpif->ukeys[i].cmap);
 
381
        ovs_mutex_destroy(&udpif->ukeys[i].mutex);
 
382
    }
 
383
    free(udpif->ukeys);
 
384
    udpif->ukeys = NULL;
 
385
 
278
386
    list_remove(&udpif->list_node);
279
387
    latch_destroy(&udpif->exit_latch);
280
388
    seq_destroy(udpif->reval_seq);
303
411
            xpthread_join(udpif->revalidators[i].thread, NULL);
304
412
        }
305
413
 
 
414
        dpif_disable_upcall(udpif->dpif);
 
415
 
306
416
        for (i = 0; i < udpif->n_revalidators; i++) {
307
417
            struct revalidator *revalidator = &udpif->revalidators[i];
308
418
 
309
419
            /* Delete ukeys, and delete all flows from the datapath to prevent
310
420
             * double-counting stats. */
311
421
            revalidator_purge(revalidator);
312
 
 
313
 
            hmap_destroy(&udpif->ukeys[i].hmap);
314
 
            ovs_mutex_destroy(&udpif->ukeys[i].mutex);
315
422
        }
316
423
 
317
424
        latch_poll(&udpif->exit_latch);
325
432
        free(udpif->handlers);
326
433
        udpif->handlers = NULL;
327
434
        udpif->n_handlers = 0;
328
 
 
329
 
        free(udpif->ukeys);
330
 
        udpif->ukeys = NULL;
331
435
    }
332
436
}
333
437
 
339
443
{
340
444
    if (udpif && n_handlers && n_revalidators) {
341
445
        size_t i;
 
446
        bool enable_ufid;
342
447
 
343
448
        udpif->n_handlers = n_handlers;
344
449
        udpif->n_revalidators = n_revalidators;
353
458
                "handler", udpif_upcall_handler, handler);
354
459
        }
355
460
 
 
461
        enable_ufid = ofproto_dpif_get_enable_ufid(udpif->backer);
 
462
        atomic_init(&udpif->enable_ufid, enable_ufid);
 
463
        dpif_enable_upcall(udpif->dpif);
 
464
 
356
465
        ovs_barrier_init(&udpif->reval_barrier, udpif->n_revalidators);
357
466
        udpif->reval_exit = false;
358
467
        udpif->revalidators = xzalloc(udpif->n_revalidators
359
468
                                      * sizeof *udpif->revalidators);
360
 
        udpif->ukeys = xmalloc(sizeof *udpif->ukeys * n_revalidators);
361
469
        for (i = 0; i < udpif->n_revalidators; i++) {
362
470
            struct revalidator *revalidator = &udpif->revalidators[i];
363
471
 
364
472
            revalidator->udpif = udpif;
365
 
            hmap_init(&udpif->ukeys[i].hmap);
366
 
            ovs_mutex_init(&udpif->ukeys[i].mutex);
367
 
            revalidator->ukeys = &udpif->ukeys[i].hmap;
368
473
            revalidator->thread = ovs_thread_create(
369
474
                "revalidator", udpif_revalidator, revalidator);
370
475
        }
447
552
    simap_increase(usage, "handlers", udpif->n_handlers);
448
553
 
449
554
    simap_increase(usage, "revalidators", udpif->n_revalidators);
450
 
    for (i = 0; i < udpif->n_revalidators; i++) {
451
 
        ovs_mutex_lock(&udpif->ukeys[i].mutex);
452
 
        simap_increase(usage, "udpif keys", hmap_count(&udpif->ukeys[i].hmap));
453
 
        ovs_mutex_unlock(&udpif->ukeys[i].mutex);
 
555
    for (i = 0; i < N_UMAPS; i++) {
 
556
        simap_increase(usage, "udpif keys", cmap_count(&udpif->ukeys[i].cmap));
454
557
    }
455
558
}
456
559
 
483
586
    }
484
587
}
485
588
 
 
589
static bool
 
590
udpif_use_ufid(struct udpif *udpif)
 
591
{
 
592
    bool enable;
 
593
 
 
594
    atomic_read_relaxed(&enable_ufid, &enable);
 
595
    return enable && ofproto_dpif_get_enable_ufid(udpif->backer);
 
596
}
 
597
 
486
598
 
487
599
static unsigned long
488
600
udpif_get_n_flows(struct udpif *udpif)
491
603
    unsigned long flow_count;
492
604
 
493
605
    now = time_msec();
494
 
    atomic_read(&udpif->n_flows_timestamp, &time);
 
606
    atomic_read_relaxed(&udpif->n_flows_timestamp, &time);
495
607
    if (time < now - 100 && !ovs_mutex_trylock(&udpif->n_flows_mutex)) {
496
608
        struct dpif_dp_stats stats;
497
609
 
498
 
        atomic_store(&udpif->n_flows_timestamp, now);
 
610
        atomic_store_relaxed(&udpif->n_flows_timestamp, now);
499
611
        dpif_get_dp_stats(udpif->dpif, &stats);
500
612
        flow_count = stats.n_flows;
501
 
        atomic_store(&udpif->n_flows, flow_count);
 
613
        atomic_store_relaxed(&udpif->n_flows, flow_count);
502
614
        ovs_mutex_unlock(&udpif->n_flows_mutex);
503
615
    } else {
504
 
        atomic_read(&udpif->n_flows, &flow_count);
 
616
        atomic_read_relaxed(&udpif->n_flows, &flow_count);
505
617
    }
506
618
    return flow_count;
507
619
}
508
620
 
509
 
/* The upcall handler thread tries to read a batch of FLOW_MISS_MAX_BATCH
 
621
/* The upcall handler thread tries to read a batch of UPCALL_MAX_BATCH
510
622
 * upcalls from dpif, processes the batch and installs corresponding flows
511
623
 * in dpif. */
512
624
static void *
514
626
{
515
627
    struct handler *handler = arg;
516
628
    struct udpif *udpif = handler->udpif;
517
 
    struct hmap misses = HMAP_INITIALIZER(&misses);
518
629
 
519
630
    while (!latch_is_set(&handler->udpif->exit_latch)) {
520
 
        struct upcall upcalls[FLOW_MISS_MAX_BATCH];
521
 
        struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH];
522
 
        struct flow_miss *miss;
523
 
        size_t n_upcalls, i;
524
 
 
525
 
        n_upcalls = read_upcalls(handler, upcalls, miss_buf, &misses);
526
 
        if (!n_upcalls) {
 
631
        if (recv_upcalls(handler)) {
 
632
            poll_immediate_wake();
 
633
        } else {
527
634
            dpif_recv_wait(udpif->dpif, handler->handler_id);
528
635
            latch_wait(&udpif->exit_latch);
529
 
        } else {
530
 
            poll_immediate_wake();
531
 
 
532
 
            handle_upcalls(handler, &misses, upcalls, n_upcalls);
533
 
 
534
 
            HMAP_FOR_EACH (miss, hmap_node, &misses) {
535
 
                xlate_out_uninit(&miss->xout);
536
 
            }
537
 
            hmap_clear(&misses);
538
 
            for (i = 0; i < n_upcalls; i++) {
539
 
                ofpbuf_uninit(&upcalls[i].dpif_upcall.packet);
540
 
                ofpbuf_uninit(&upcalls[i].upcall_buf);
541
 
            }
542
636
        }
543
637
        poll_block();
544
638
    }
545
 
    hmap_destroy(&misses);
546
639
 
547
640
    return NULL;
548
641
}
549
642
 
 
643
static size_t
 
644
recv_upcalls(struct handler *handler)
 
645
{
 
646
    struct udpif *udpif = handler->udpif;
 
647
    uint64_t recv_stubs[UPCALL_MAX_BATCH][512 / 8];
 
648
    struct ofpbuf recv_bufs[UPCALL_MAX_BATCH];
 
649
    struct dpif_upcall dupcalls[UPCALL_MAX_BATCH];
 
650
    struct upcall upcalls[UPCALL_MAX_BATCH];
 
651
    struct flow flows[UPCALL_MAX_BATCH];
 
652
    size_t n_upcalls, i;
 
653
 
 
654
    n_upcalls = 0;
 
655
    while (n_upcalls < UPCALL_MAX_BATCH) {
 
656
        struct ofpbuf *recv_buf = &recv_bufs[n_upcalls];
 
657
        struct dpif_upcall *dupcall = &dupcalls[n_upcalls];
 
658
        struct upcall *upcall = &upcalls[n_upcalls];
 
659
        struct flow *flow = &flows[n_upcalls];
 
660
        int error;
 
661
 
 
662
        ofpbuf_use_stub(recv_buf, recv_stubs[n_upcalls],
 
663
                        sizeof recv_stubs[n_upcalls]);
 
664
        if (dpif_recv(udpif->dpif, handler->handler_id, dupcall, recv_buf)) {
 
665
            ofpbuf_uninit(recv_buf);
 
666
            break;
 
667
        }
 
668
 
 
669
        if (odp_flow_key_to_flow(dupcall->key, dupcall->key_len, flow)
 
670
            == ODP_FIT_ERROR) {
 
671
            goto free_dupcall;
 
672
        }
 
673
 
 
674
        error = upcall_receive(upcall, udpif->backer, &dupcall->packet,
 
675
                               dupcall->type, dupcall->userdata, flow,
 
676
                               &dupcall->ufid, PMD_ID_NULL);
 
677
        if (error) {
 
678
            if (error == ENODEV) {
 
679
                /* Received packet on datapath port for which we couldn't
 
680
                 * associate an ofproto.  This can happen if a port is removed
 
681
                 * while traffic is being received.  Print a rate-limited
 
682
                 * message in case it happens frequently. */
 
683
                dpif_flow_put(udpif->dpif, DPIF_FP_CREATE, dupcall->key,
 
684
                              dupcall->key_len, NULL, 0, NULL, 0,
 
685
                              &dupcall->ufid, PMD_ID_NULL, NULL);
 
686
                VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
 
687
                             "port %"PRIu32, flow->in_port.odp_port);
 
688
            }
 
689
            goto free_dupcall;
 
690
        }
 
691
 
 
692
        upcall->key = dupcall->key;
 
693
        upcall->key_len = dupcall->key_len;
 
694
        upcall->ufid = &dupcall->ufid;
 
695
 
 
696
        upcall->out_tun_key = dupcall->out_tun_key;
 
697
 
 
698
        if (vsp_adjust_flow(upcall->ofproto, flow, &dupcall->packet)) {
 
699
            upcall->vsp_adjusted = true;
 
700
        }
 
701
 
 
702
        pkt_metadata_from_flow(&dupcall->packet.md, flow);
 
703
        flow_extract(&dupcall->packet, flow);
 
704
 
 
705
        error = process_upcall(udpif, upcall, NULL);
 
706
        if (error) {
 
707
            goto cleanup;
 
708
        }
 
709
 
 
710
        n_upcalls++;
 
711
        continue;
 
712
 
 
713
cleanup:
 
714
        upcall_uninit(upcall);
 
715
free_dupcall:
 
716
        dp_packet_uninit(&dupcall->packet);
 
717
        ofpbuf_uninit(recv_buf);
 
718
    }
 
719
 
 
720
    if (n_upcalls) {
 
721
        handle_upcalls(handler->udpif, upcalls, n_upcalls);
 
722
        for (i = 0; i < n_upcalls; i++) {
 
723
            dp_packet_uninit(&dupcalls[i].packet);
 
724
            ofpbuf_uninit(&recv_bufs[i]);
 
725
            upcall_uninit(&upcalls[i]);
 
726
        }
 
727
    }
 
728
 
 
729
    return n_upcalls;
 
730
}
 
731
 
550
732
static void *
551
733
udpif_revalidator(void *arg)
552
734
{
558
740
    /* Used only by the leader. */
559
741
    long long int start_time = 0;
560
742
    uint64_t last_reval_seq = 0;
561
 
    unsigned int flow_limit = 0;
562
743
    size_t n_flows = 0;
563
744
 
564
745
    revalidator->id = ovsthread_id_self();
566
747
        if (leader) {
567
748
            uint64_t reval_seq;
568
749
 
 
750
            recirc_run(); /* Recirculation cleanup. */
 
751
 
569
752
            reval_seq = seq_read(udpif->reval_seq);
570
 
            udpif->need_revalidate = last_reval_seq != reval_seq;
571
753
            last_reval_seq = reval_seq;
572
754
 
573
755
            n_flows = udpif_get_n_flows(udpif);
581
763
 
582
764
            start_time = time_msec();
583
765
            if (!udpif->reval_exit) {
584
 
                dpif_flow_dump_start(&udpif->dump, udpif->dpif);
 
766
                bool terse_dump;
 
767
 
 
768
                terse_dump = udpif_use_ufid(udpif);
 
769
                udpif->dump = dpif_flow_dump_create(udpif->dpif, terse_dump);
585
770
            }
586
771
        }
587
772
 
600
785
        ovs_barrier_block(&udpif->reval_barrier);
601
786
 
602
787
        if (leader) {
 
788
            unsigned int flow_limit;
603
789
            long long int duration;
604
790
 
605
 
            dpif_flow_dump_done(&udpif->dump);
 
791
            atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
 
792
 
 
793
            dpif_flow_dump_destroy(udpif->dump);
606
794
            seq_change(udpif->dump_seq);
607
795
 
608
796
            duration = MAX(time_msec() - start_time, 1);
609
 
            atomic_read(&udpif->flow_limit, &flow_limit);
610
797
            udpif->dump_duration = duration;
611
798
            if (duration > 2000) {
612
799
                flow_limit /= duration / 1000;
617
804
                flow_limit += 1000;
618
805
            }
619
806
            flow_limit = MIN(ofproto_flow_limit, MAX(flow_limit, 1000));
620
 
            atomic_store(&udpif->flow_limit, flow_limit);
 
807
            atomic_store_relaxed(&udpif->flow_limit, flow_limit);
621
808
 
622
809
            if (duration > 2000) {
623
810
                VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
635
822
}
636
823
 
637
824
static enum upcall_type
638
 
classify_upcall(const struct upcall *upcall)
 
825
classify_upcall(enum dpif_upcall_type type, const struct nlattr *userdata)
639
826
{
640
 
    const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
641
827
    union user_action_cookie cookie;
642
828
    size_t userdata_len;
643
829
 
644
830
    /* First look at the upcall type. */
645
 
    switch (dpif_upcall->type) {
 
831
    switch (type) {
646
832
    case DPIF_UC_ACTION:
647
833
        break;
648
834
 
651
837
 
652
838
    case DPIF_N_UC_TYPES:
653
839
    default:
654
 
        VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
655
 
                     dpif_upcall->type);
 
840
        VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, type);
656
841
        return BAD_UPCALL;
657
842
    }
658
843
 
659
844
    /* "action" upcalls need a closer look. */
660
 
    if (!dpif_upcall->userdata) {
 
845
    if (!userdata) {
661
846
        VLOG_WARN_RL(&rl, "action upcall missing cookie");
662
847
        return BAD_UPCALL;
663
848
    }
664
 
    userdata_len = nl_attr_get_size(dpif_upcall->userdata);
 
849
    userdata_len = nl_attr_get_size(userdata);
665
850
    if (userdata_len < sizeof cookie.type
666
851
        || userdata_len > sizeof cookie) {
667
852
        VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
669
854
        return BAD_UPCALL;
670
855
    }
671
856
    memset(&cookie, 0, sizeof cookie);
672
 
    memcpy(&cookie, nl_attr_get(dpif_upcall->userdata), userdata_len);
 
857
    memcpy(&cookie, nl_attr_get(userdata), userdata_len);
673
858
    if (userdata_len == MAX(8, sizeof cookie.sflow)
674
859
        && cookie.type == USER_ACTION_COOKIE_SFLOW) {
675
860
        return SFLOW_UPCALL;
693
878
 * initialized with at least 128 bytes of space. */
694
879
static void
695
880
compose_slow_path(struct udpif *udpif, struct xlate_out *xout,
696
 
                  struct flow *flow, odp_port_t odp_in_port,
 
881
                  const struct flow *flow, odp_port_t odp_in_port,
697
882
                  struct ofpbuf *buf)
698
883
{
699
884
    union user_action_cookie cookie;
708
893
        ? ODPP_NONE
709
894
        : odp_in_port;
710
895
    pid = dpif_port_get_pid(udpif->dpif, port, flow_hash_5tuple(flow, 0));
711
 
    odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, buf);
712
 
}
713
 
 
714
 
static struct flow_miss *
715
 
flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
716
 
               const struct flow *flow, uint32_t hash)
717
 
{
718
 
    struct flow_miss *miss;
719
 
 
720
 
    HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
721
 
        if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
722
 
            return miss;
723
 
        }
724
 
    }
725
 
 
726
 
    return NULL;
727
 
}
728
 
 
729
 
/* Reads and classifies upcalls.  Returns the number of upcalls successfully
730
 
 * read. */
731
 
static size_t
732
 
read_upcalls(struct handler *handler,
733
 
             struct upcall upcalls[FLOW_MISS_MAX_BATCH],
734
 
             struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH],
735
 
             struct hmap *misses)
736
 
{
737
 
    struct udpif *udpif = handler->udpif;
738
 
    size_t i;
739
 
    size_t n_misses = 0;
740
 
    size_t n_upcalls = 0;
741
 
 
742
 
    /*
743
 
     * Try reading FLOW_MISS_MAX_BATCH upcalls from dpif.
744
 
     *
745
 
     * Extract the flow from each upcall.  Construct in 'misses' a hash table
746
 
     * that maps each unique flow to a 'struct flow_miss'.
747
 
     *
748
 
     * Most commonly there is a single packet per flow_miss, but there are
749
 
     * several reasons why there might be more than one, e.g.:
750
 
     *
751
 
     *   - The dpif packet interface does not support TSO (or UFO, etc.), so a
752
 
     *     large packet sent to userspace is split into a sequence of smaller
753
 
     *     ones.
754
 
     *
755
 
     *   - A stream of quickly arriving packets in an established "slow-pathed"
756
 
     *     flow.
757
 
     *
758
 
     *   - Rarely, a stream of quickly arriving packets in a flow not yet
759
 
     *     established.  (This is rare because most protocols do not send
760
 
     *     multiple back-to-back packets before receiving a reply from the
761
 
     *     other end of the connection, which gives OVS a chance to set up a
762
 
     *     datapath flow.)
763
 
     */
764
 
    for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
765
 
        struct upcall *upcall = &upcalls[n_upcalls];
766
 
        struct flow_miss *miss = &miss_buf[n_misses];
767
 
        struct dpif_upcall *dupcall;
768
 
        struct ofpbuf *packet;
769
 
        struct flow_miss *existing_miss;
770
 
        struct ofproto_dpif *ofproto;
771
 
        struct dpif_sflow *sflow;
772
 
        struct dpif_ipfix *ipfix;
773
 
        struct flow flow;
774
 
        enum upcall_type type;
775
 
        odp_port_t odp_in_port;
776
 
        int error;
777
 
 
778
 
        ofpbuf_use_stub(&upcall->upcall_buf, upcall->upcall_stub,
779
 
                        sizeof upcall->upcall_stub);
780
 
        error = dpif_recv(udpif->dpif, handler->handler_id,
781
 
                          &upcall->dpif_upcall, &upcall->upcall_buf);
782
 
        if (error) {
783
 
            ofpbuf_uninit(&upcall->upcall_buf);
784
 
            break;
785
 
        }
786
 
 
787
 
        dupcall = &upcall->dpif_upcall;
788
 
        packet = &dupcall->packet;
789
 
        error = xlate_receive(udpif->backer, packet, dupcall->key,
790
 
                              dupcall->key_len, &flow,
791
 
                              &ofproto, &ipfix, &sflow, NULL, &odp_in_port);
792
 
        if (error) {
793
 
            if (error == ENODEV) {
794
 
                /* Received packet on datapath port for which we couldn't
795
 
                 * associate an ofproto.  This can happen if a port is removed
796
 
                 * while traffic is being received.  Print a rate-limited
797
 
                 * message in case it happens frequently.  Install a drop flow
798
 
                 * so that future packets of the flow are inexpensively dropped
799
 
                 * in the kernel. */
800
 
                VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
801
 
                             "port %"PRIu32, odp_in_port);
802
 
                dpif_flow_put(udpif->dpif, DPIF_FP_CREATE,
803
 
                              dupcall->key, dupcall->key_len, NULL, 0, NULL, 0,
804
 
                              NULL);
805
 
            }
806
 
            goto destroy_upcall;
807
 
        }
808
 
 
809
 
        type = classify_upcall(upcall);
810
 
        if (type == MISS_UPCALL) {
811
 
            uint32_t hash;
812
 
            struct pkt_metadata md = pkt_metadata_from_flow(&flow);
813
 
 
814
 
            flow_extract(packet, &md, &miss->flow);
815
 
            hash = flow_hash(&miss->flow, 0);
816
 
            existing_miss = flow_miss_find(misses, ofproto, &miss->flow,
817
 
                                           hash);
818
 
            if (!existing_miss) {
819
 
                hmap_insert(misses, &miss->hmap_node, hash);
820
 
                miss->ofproto = ofproto;
821
 
                miss->key = dupcall->key;
822
 
                miss->key_len = dupcall->key_len;
823
 
                miss->upcall_type = dupcall->type;
824
 
                miss->stats.n_packets = 0;
825
 
                miss->stats.n_bytes = 0;
826
 
                miss->stats.used = time_msec();
827
 
                miss->stats.tcp_flags = 0;
828
 
                miss->odp_in_port = odp_in_port;
829
 
                miss->put = false;
830
 
                n_misses++;
831
 
            } else {
832
 
                miss = existing_miss;
833
 
            }
834
 
            miss->stats.tcp_flags |= ntohs(miss->flow.tcp_flags);
835
 
            miss->stats.n_bytes += ofpbuf_size(packet);
836
 
            miss->stats.n_packets++;
837
 
 
838
 
            upcall->flow_miss = miss;
839
 
            n_upcalls++;
840
 
            continue;
841
 
        }
842
 
 
843
 
        switch (type) {
844
 
        case SFLOW_UPCALL:
845
 
            if (sflow) {
846
 
                union user_action_cookie cookie;
847
 
 
848
 
                memset(&cookie, 0, sizeof cookie);
849
 
                memcpy(&cookie, nl_attr_get(dupcall->userdata),
850
 
                       sizeof cookie.sflow);
851
 
                dpif_sflow_received(sflow, packet, &flow, odp_in_port,
852
 
                                    &cookie);
853
 
            }
854
 
            break;
855
 
        case IPFIX_UPCALL:
856
 
            if (ipfix) {
857
 
                dpif_ipfix_bridge_sample(ipfix, packet, &flow);
858
 
            }
859
 
            break;
860
 
        case FLOW_SAMPLE_UPCALL:
861
 
            if (ipfix) {
862
 
                union user_action_cookie cookie;
863
 
 
864
 
                memset(&cookie, 0, sizeof cookie);
865
 
                memcpy(&cookie, nl_attr_get(dupcall->userdata),
866
 
                       sizeof cookie.flow_sample);
867
 
 
868
 
                /* The flow reflects exactly the contents of the packet.
869
 
                 * Sample the packet using it. */
870
 
                dpif_ipfix_flow_sample(ipfix, packet, &flow,
871
 
                                       cookie.flow_sample.collector_set_id,
872
 
                                       cookie.flow_sample.probability,
873
 
                                       cookie.flow_sample.obs_domain_id,
874
 
                                       cookie.flow_sample.obs_point_id);
875
 
            }
876
 
            break;
877
 
        case BAD_UPCALL:
878
 
            break;
879
 
        case MISS_UPCALL:
880
 
            OVS_NOT_REACHED();
881
 
        }
882
 
 
883
 
        dpif_ipfix_unref(ipfix);
884
 
        dpif_sflow_unref(sflow);
885
 
 
886
 
destroy_upcall:
887
 
        ofpbuf_uninit(&upcall->dpif_upcall.packet);
888
 
        ofpbuf_uninit(&upcall->upcall_buf);
889
 
    }
890
 
 
891
 
    return n_upcalls;
892
 
}
893
 
 
894
 
static void
895
 
handle_upcalls(struct handler *handler, struct hmap *misses,
896
 
               struct upcall *upcalls, size_t n_upcalls)
897
 
{
898
 
    struct udpif *udpif = handler->udpif;
899
 
    struct dpif_op *opsp[FLOW_MISS_MAX_BATCH * 2];
900
 
    struct dpif_op ops[FLOW_MISS_MAX_BATCH * 2];
901
 
    struct flow_miss *miss;
902
 
    size_t n_ops, i;
903
 
    unsigned int flow_limit;
904
 
    bool fail_open, may_put;
905
 
 
906
 
    atomic_read(&udpif->flow_limit, &flow_limit);
 
896
    odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, ODPP_NONE,
 
897
                             buf);
 
898
}
 
899
 
 
900
/* If there is no error, the upcall must be destroyed with upcall_uninit()
 
901
 * before quiescing, as the referred objects are guaranteed to exist only
 
902
 * until the calling thread quiesces.  Otherwise, do not call upcall_uninit()
 
903
 * since the 'upcall->put_actions' remains uninitialized. */
 
904
static int
 
905
upcall_receive(struct upcall *upcall, const struct dpif_backer *backer,
 
906
               const struct dp_packet *packet, enum dpif_upcall_type type,
 
907
               const struct nlattr *userdata, const struct flow *flow,
 
908
               const ovs_u128 *ufid, const unsigned pmd_id)
 
909
{
 
910
    int error;
 
911
 
 
912
    error = xlate_lookup(backer, flow, &upcall->ofproto, &upcall->ipfix,
 
913
                         &upcall->sflow, NULL, &upcall->in_port);
 
914
    if (error) {
 
915
        return error;
 
916
    }
 
917
 
 
918
    upcall->recirc = NULL;
 
919
    upcall->have_recirc_ref = false;
 
920
    upcall->flow = flow;
 
921
    upcall->packet = packet;
 
922
    upcall->ufid = ufid;
 
923
    upcall->pmd_id = pmd_id;
 
924
    upcall->type = type;
 
925
    upcall->userdata = userdata;
 
926
    ofpbuf_init(&upcall->put_actions, 0);
 
927
 
 
928
    upcall->xout_initialized = false;
 
929
    upcall->vsp_adjusted = false;
 
930
    upcall->ukey_persists = false;
 
931
 
 
932
    upcall->ukey = NULL;
 
933
    upcall->key = NULL;
 
934
    upcall->key_len = 0;
 
935
 
 
936
    upcall->out_tun_key = NULL;
 
937
 
 
938
    return 0;
 
939
}
 
940
 
 
941
static void
 
942
upcall_xlate(struct udpif *udpif, struct upcall *upcall,
 
943
             struct ofpbuf *odp_actions)
 
944
{
 
945
    struct dpif_flow_stats stats;
 
946
    struct xlate_in xin;
 
947
 
 
948
    stats.n_packets = 1;
 
949
    stats.n_bytes = dp_packet_size(upcall->packet);
 
950
    stats.used = time_msec();
 
951
    stats.tcp_flags = ntohs(upcall->flow->tcp_flags);
 
952
 
 
953
    xlate_in_init(&xin, upcall->ofproto, upcall->flow, upcall->in_port, NULL,
 
954
                  stats.tcp_flags, upcall->packet);
 
955
    xin.odp_actions = odp_actions;
 
956
 
 
957
    if (upcall->type == DPIF_UC_MISS) {
 
958
        xin.resubmit_stats = &stats;
 
959
 
 
960
        if (xin.recirc) {
 
961
            /* We may install a datapath flow only if we get a reference to the
 
962
             * recirculation context (otherwise we could have recirculation
 
963
             * upcalls using recirculation ID for which no context can be
 
964
             * found).  We may still execute the flow's actions even if we
 
965
             * don't install the flow. */
 
966
            upcall->recirc = xin.recirc;
 
967
            upcall->have_recirc_ref = recirc_id_node_try_ref_rcu(xin.recirc);
 
968
        }
 
969
    } else {
 
970
        /* For non-miss upcalls, we are either executing actions (one of which
 
971
         * is an userspace action) for an upcall, in which case the stats have
 
972
         * already been taken care of, or there's a flow in the datapath which
 
973
         * this packet was accounted to.  Presumably the revalidators will deal
 
974
         * with pushing its stats eventually. */
 
975
    }
 
976
 
 
977
    upcall->dump_seq = seq_read(udpif->dump_seq);
 
978
    upcall->reval_seq = seq_read(udpif->reval_seq);
 
979
    xlate_actions(&xin, &upcall->xout);
 
980
    upcall->xout_initialized = true;
 
981
 
 
982
    /* Special case for fail-open mode.
 
983
     *
 
984
     * If we are in fail-open mode, but we are connected to a controller too,
 
985
     * then we should send the packet up to the controller in the hope that it
 
986
     * will try to set up a flow and thereby allow us to exit fail-open.
 
987
     *
 
988
     * See the top-level comment in fail-open.c for more information.
 
989
     *
 
990
     * Copy packets before they are modified by execution. */
 
991
    if (upcall->xout.fail_open) {
 
992
        const struct dp_packet *packet = upcall->packet;
 
993
        struct ofproto_packet_in *pin;
 
994
 
 
995
        pin = xmalloc(sizeof *pin);
 
996
        pin->up.packet = xmemdup(dp_packet_data(packet), dp_packet_size(packet));
 
997
        pin->up.packet_len = dp_packet_size(packet);
 
998
        pin->up.reason = OFPR_NO_MATCH;
 
999
        pin->up.table_id = 0;
 
1000
        pin->up.cookie = OVS_BE64_MAX;
 
1001
        flow_get_metadata(upcall->flow, &pin->up.flow_metadata);
 
1002
        pin->send_len = 0; /* Not used for flow table misses. */
 
1003
        pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
 
1004
        ofproto_dpif_send_packet_in(upcall->ofproto, pin);
 
1005
    }
 
1006
 
 
1007
    if (!upcall->xout.slow) {
 
1008
        ofpbuf_use_const(&upcall->put_actions,
 
1009
                         upcall->xout.odp_actions->data,
 
1010
                         upcall->xout.odp_actions->size);
 
1011
    } else {
 
1012
        ofpbuf_init(&upcall->put_actions, 0);
 
1013
        compose_slow_path(udpif, &upcall->xout, upcall->flow,
 
1014
                          upcall->flow->in_port.odp_port,
 
1015
                          &upcall->put_actions);
 
1016
    }
 
1017
 
 
1018
    /* This function is also called for slow-pathed flows.  As we are only
 
1019
     * going to create new datapath flows for actual datapath misses, there is
 
1020
     * no point in creating a ukey otherwise. */
 
1021
    if (upcall->type == DPIF_UC_MISS) {
 
1022
        upcall->ukey = ukey_create_from_upcall(upcall);
 
1023
    }
 
1024
}
 
1025
 
 
1026
static void
 
1027
upcall_uninit(struct upcall *upcall)
 
1028
{
 
1029
    if (upcall) {
 
1030
        if (upcall->xout_initialized) {
 
1031
            xlate_out_uninit(&upcall->xout);
 
1032
        }
 
1033
        ofpbuf_uninit(&upcall->put_actions);
 
1034
        if (upcall->ukey) {
 
1035
            if (!upcall->ukey_persists) {
 
1036
                ukey_delete__(upcall->ukey);
 
1037
            }
 
1038
        } else if (upcall->have_recirc_ref) {
 
1039
            /* The reference was transferred to the ukey if one was created. */
 
1040
            recirc_id_node_unref(upcall->recirc);
 
1041
        }
 
1042
    }
 
1043
}
 
1044
 
 
1045
static int
 
1046
upcall_cb(const struct dp_packet *packet, const struct flow *flow, ovs_u128 *ufid,
 
1047
          unsigned pmd_id, enum dpif_upcall_type type,
 
1048
          const struct nlattr *userdata, struct ofpbuf *actions,
 
1049
          struct flow_wildcards *wc, struct ofpbuf *put_actions, void *aux)
 
1050
{
 
1051
    struct udpif *udpif = aux;
 
1052
    unsigned int flow_limit;
 
1053
    struct upcall upcall;
 
1054
    bool megaflow;
 
1055
    int error;
 
1056
 
 
1057
    atomic_read_relaxed(&enable_megaflows, &megaflow);
 
1058
    atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
 
1059
 
 
1060
    error = upcall_receive(&upcall, udpif->backer, packet, type, userdata,
 
1061
                           flow, ufid, pmd_id);
 
1062
    if (error) {
 
1063
        return error;
 
1064
    }
 
1065
 
 
1066
    error = process_upcall(udpif, &upcall, actions);
 
1067
    if (error) {
 
1068
        goto out;
 
1069
    }
 
1070
 
 
1071
    if (upcall.xout.slow && put_actions) {
 
1072
        ofpbuf_put(put_actions, upcall.put_actions.data,
 
1073
                   upcall.put_actions.size);
 
1074
    }
 
1075
 
 
1076
    if (OVS_LIKELY(wc)) {
 
1077
        if (megaflow) {
 
1078
            /* XXX: This could be avoided with sufficient API changes. */
 
1079
            *wc = upcall.xout.wc;
 
1080
        } else {
 
1081
            flow_wildcards_init_for_packet(wc, flow);
 
1082
        }
 
1083
    }
 
1084
 
 
1085
    if (udpif_get_n_flows(udpif) >= flow_limit) {
 
1086
        error = ENOSPC;
 
1087
        goto out;
 
1088
    }
 
1089
 
 
1090
    /* Prevent miss flow installation if the key has recirculation ID but we
 
1091
     * were not able to get a reference on it. */
 
1092
    if (type == DPIF_UC_MISS && upcall.recirc && !upcall.have_recirc_ref) {
 
1093
        error = ENOSPC;
 
1094
        goto out;
 
1095
    }
 
1096
 
 
1097
    if (upcall.ukey && !ukey_install(udpif, upcall.ukey)) {
 
1098
        error = ENOSPC;
 
1099
    }
 
1100
out:
 
1101
    if (!error) {
 
1102
        upcall.ukey_persists = true;
 
1103
    }
 
1104
    upcall_uninit(&upcall);
 
1105
    return error;
 
1106
}
 
1107
 
 
1108
static int
 
1109
process_upcall(struct udpif *udpif, struct upcall *upcall,
 
1110
               struct ofpbuf *odp_actions)
 
1111
{
 
1112
    const struct nlattr *userdata = upcall->userdata;
 
1113
    const struct dp_packet *packet = upcall->packet;
 
1114
    const struct flow *flow = upcall->flow;
 
1115
 
 
1116
    switch (classify_upcall(upcall->type, userdata)) {
 
1117
    case MISS_UPCALL:
 
1118
        upcall_xlate(udpif, upcall, odp_actions);
 
1119
        return 0;
 
1120
 
 
1121
    case SFLOW_UPCALL:
 
1122
        if (upcall->sflow) {
 
1123
            union user_action_cookie cookie;
 
1124
 
 
1125
            memset(&cookie, 0, sizeof cookie);
 
1126
            memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.sflow);
 
1127
            dpif_sflow_received(upcall->sflow, packet, flow,
 
1128
                                flow->in_port.odp_port, &cookie);
 
1129
        }
 
1130
        break;
 
1131
 
 
1132
    case IPFIX_UPCALL:
 
1133
        if (upcall->ipfix) {
 
1134
            union user_action_cookie cookie;
 
1135
            struct flow_tnl output_tunnel_key;
 
1136
 
 
1137
            memset(&cookie, 0, sizeof cookie);
 
1138
            memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.ipfix);
 
1139
 
 
1140
            if (upcall->out_tun_key) {
 
1141
                memset(&output_tunnel_key, 0, sizeof output_tunnel_key);
 
1142
                odp_tun_key_from_attr(upcall->out_tun_key,
 
1143
                                      &output_tunnel_key);
 
1144
            }
 
1145
            dpif_ipfix_bridge_sample(upcall->ipfix, packet, flow,
 
1146
                                     flow->in_port.odp_port,
 
1147
                                     cookie.ipfix.output_odp_port,
 
1148
                                     upcall->out_tun_key ?
 
1149
                                         &output_tunnel_key : NULL);
 
1150
        }
 
1151
        break;
 
1152
 
 
1153
    case FLOW_SAMPLE_UPCALL:
 
1154
        if (upcall->ipfix) {
 
1155
            union user_action_cookie cookie;
 
1156
 
 
1157
            memset(&cookie, 0, sizeof cookie);
 
1158
            memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.flow_sample);
 
1159
 
 
1160
            /* The flow reflects exactly the contents of the packet.
 
1161
             * Sample the packet using it. */
 
1162
            dpif_ipfix_flow_sample(upcall->ipfix, packet, flow,
 
1163
                                   cookie.flow_sample.collector_set_id,
 
1164
                                   cookie.flow_sample.probability,
 
1165
                                   cookie.flow_sample.obs_domain_id,
 
1166
                                   cookie.flow_sample.obs_point_id);
 
1167
        }
 
1168
        break;
 
1169
 
 
1170
    case BAD_UPCALL:
 
1171
        break;
 
1172
    }
 
1173
 
 
1174
    return EAGAIN;
 
1175
}
 
1176
 
 
1177
static void
 
1178
handle_upcalls(struct udpif *udpif, struct upcall *upcalls,
 
1179
               size_t n_upcalls)
 
1180
{
 
1181
    struct dpif_op *opsp[UPCALL_MAX_BATCH * 2];
 
1182
    struct ukey_op ops[UPCALL_MAX_BATCH * 2];
 
1183
    unsigned int flow_limit;
 
1184
    size_t n_ops, n_opsp, i;
 
1185
    bool may_put;
 
1186
    bool megaflow;
 
1187
 
 
1188
    atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
 
1189
    atomic_read_relaxed(&enable_megaflows, &megaflow);
 
1190
 
907
1191
    may_put = udpif_get_n_flows(udpif) < flow_limit;
908
1192
 
909
 
    /* Initialize each 'struct flow_miss's ->xout.
910
 
     *
911
 
     * We do this per-flow_miss rather than per-packet because, most commonly,
912
 
     * all the packets in a flow can use the same translation.
913
 
     *
914
 
     * We can't do this in the previous loop because we need the TCP flags for
915
 
     * all the packets in each miss. */
916
 
    fail_open = false;
917
 
    HMAP_FOR_EACH (miss, hmap_node, misses) {
918
 
        struct xlate_in xin;
919
 
 
920
 
        xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL,
921
 
                      miss->stats.tcp_flags, NULL);
922
 
        xin.may_learn = true;
923
 
 
924
 
        if (miss->upcall_type == DPIF_UC_MISS) {
925
 
            xin.resubmit_stats = &miss->stats;
926
 
        } else {
927
 
            /* For non-miss upcalls, there's a flow in the datapath which this
928
 
             * packet was accounted to.  Presumably the revalidators will deal
929
 
             * with pushing its stats eventually. */
930
 
        }
931
 
 
932
 
        xlate_actions(&xin, &miss->xout);
933
 
        fail_open = fail_open || miss->xout.fail_open;
934
 
    }
935
 
 
936
 
    /* Now handle the packets individually in order of arrival.  In the common
937
 
     * case each packet of a miss can share the same actions, but slow-pathed
938
 
     * packets need to be translated individually:
 
1193
    /* Handle the packets individually in order of arrival.
939
1194
     *
940
1195
     *   - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
941
1196
     *     processes received packets for these protocols.
948
1203
    n_ops = 0;
949
1204
    for (i = 0; i < n_upcalls; i++) {
950
1205
        struct upcall *upcall = &upcalls[i];
951
 
        struct flow_miss *miss = upcall->flow_miss;
952
 
        struct ofpbuf *packet = &upcall->dpif_upcall.packet;
953
 
        struct dpif_op *op;
954
 
        ovs_be16 flow_vlan_tci;
955
 
 
956
 
        /* Save a copy of flow.vlan_tci in case it is changed to
957
 
         * generate proper mega flow masks for VLAN splinter flows. */
958
 
        flow_vlan_tci = miss->flow.vlan_tci;
959
 
 
960
 
        if (miss->xout.slow) {
961
 
            struct xlate_in xin;
962
 
 
963
 
            xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL, 0, packet);
964
 
            xlate_actions_for_side_effects(&xin);
965
 
        }
966
 
 
967
 
        if (miss->flow.in_port.ofp_port
968
 
            != vsp_realdev_to_vlandev(miss->ofproto,
969
 
                                      miss->flow.in_port.ofp_port,
970
 
                                      miss->flow.vlan_tci)) {
971
 
            /* This packet was received on a VLAN splinter port.  We
972
 
             * added a VLAN to the packet to make the packet resemble
973
 
             * the flow, but the actions were composed assuming that
974
 
             * the packet contained no VLAN.  So, we must remove the
975
 
             * VLAN header from the packet before trying to execute the
976
 
             * actions. */
977
 
            if (ofpbuf_size(&miss->xout.odp_actions)) {
978
 
                eth_pop_vlan(packet);
 
1206
        const struct dp_packet *packet = upcall->packet;
 
1207
        struct ukey_op *op;
 
1208
 
 
1209
        if (upcall->vsp_adjusted) {
 
1210
            /* This packet was received on a VLAN splinter port.  We added a
 
1211
             * VLAN to the packet to make the packet resemble the flow, but the
 
1212
             * actions were composed assuming that the packet contained no
 
1213
             * VLAN.  So, we must remove the VLAN header from the packet before
 
1214
             * trying to execute the actions. */
 
1215
            if (upcall->xout.odp_actions->size) {
 
1216
                eth_pop_vlan(CONST_CAST(struct dp_packet *, upcall->packet));
979
1217
            }
980
1218
 
981
1219
            /* Remove the flow vlan tags inserted by vlan splinter logic
982
1220
             * to ensure megaflow masks generated match the data path flow. */
983
 
            miss->flow.vlan_tci = 0;
 
1221
            CONST_CAST(struct flow *, upcall->flow)->vlan_tci = 0;
984
1222
        }
985
1223
 
986
1224
        /* Do not install a flow into the datapath if:
987
1225
         *
988
1226
         *    - The datapath already has too many flows.
989
1227
         *
990
 
         *    - An earlier iteration of this loop already put the same flow.
991
 
         *
992
1228
         *    - We received this packet via some flow installed in the kernel
993
 
         *      already. */
994
 
        if (may_put
995
 
            && !miss->put
996
 
            && upcall->dpif_upcall.type == DPIF_UC_MISS) {
997
 
            struct ofpbuf mask;
998
 
            bool megaflow;
999
 
 
1000
 
            miss->put = true;
1001
 
 
1002
 
            atomic_read(&enable_megaflows, &megaflow);
1003
 
            ofpbuf_use_stack(&mask, &miss->mask_buf, sizeof miss->mask_buf);
1004
 
            if (megaflow) {
1005
 
                size_t max_mpls;
1006
 
 
1007
 
                max_mpls = ofproto_dpif_get_max_mpls_depth(miss->ofproto);
1008
 
                odp_flow_key_from_mask(&mask, &miss->xout.wc.masks,
1009
 
                                       &miss->flow, UINT32_MAX, max_mpls);
1010
 
            }
1011
 
 
1012
 
            op = &ops[n_ops++];
1013
 
            op->type = DPIF_OP_FLOW_PUT;
1014
 
            op->u.flow_put.flags = DPIF_FP_CREATE;
1015
 
            op->u.flow_put.key = miss->key;
1016
 
            op->u.flow_put.key_len = miss->key_len;
1017
 
            op->u.flow_put.mask = ofpbuf_data(&mask);
1018
 
            op->u.flow_put.mask_len = ofpbuf_size(&mask);
1019
 
            op->u.flow_put.stats = NULL;
1020
 
 
1021
 
            if (!miss->xout.slow) {
1022
 
                op->u.flow_put.actions = ofpbuf_data(&miss->xout.odp_actions);
1023
 
                op->u.flow_put.actions_len = ofpbuf_size(&miss->xout.odp_actions);
1024
 
            } else {
1025
 
                struct ofpbuf buf;
1026
 
 
1027
 
                ofpbuf_use_stack(&buf, miss->slow_path_buf,
1028
 
                                 sizeof miss->slow_path_buf);
1029
 
                compose_slow_path(udpif, &miss->xout, &miss->flow,
1030
 
                                  miss->odp_in_port, &buf);
1031
 
                op->u.flow_put.actions = ofpbuf_data(&buf);
1032
 
                op->u.flow_put.actions_len = ofpbuf_size(&buf);
1033
 
            }
1034
 
        }
1035
 
 
1036
 
        /*
1037
 
         * The 'miss' may be shared by multiple upcalls. Restore
1038
 
         * the saved flow vlan_tci field before processing the next
1039
 
         * upcall. */
1040
 
        miss->flow.vlan_tci = flow_vlan_tci;
1041
 
 
1042
 
        if (ofpbuf_size(&miss->xout.odp_actions)) {
1043
 
 
1044
 
            op = &ops[n_ops++];
1045
 
            op->type = DPIF_OP_EXECUTE;
1046
 
            op->u.execute.packet = packet;
1047
 
            odp_key_to_pkt_metadata(miss->key, miss->key_len,
1048
 
                                    &op->u.execute.md);
1049
 
            op->u.execute.actions = ofpbuf_data(&miss->xout.odp_actions);
1050
 
            op->u.execute.actions_len = ofpbuf_size(&miss->xout.odp_actions);
1051
 
            op->u.execute.needs_help = (miss->xout.slow & SLOW_ACTION) != 0;
1052
 
        }
1053
 
    }
1054
 
 
1055
 
    /* Special case for fail-open mode.
1056
 
     *
1057
 
     * If we are in fail-open mode, but we are connected to a controller too,
1058
 
     * then we should send the packet up to the controller in the hope that it
1059
 
     * will try to set up a flow and thereby allow us to exit fail-open.
1060
 
     *
1061
 
     * See the top-level comment in fail-open.c for more information.
1062
 
     *
1063
 
     * Copy packets before they are modified by execution. */
1064
 
    if (fail_open) {
1065
 
        for (i = 0; i < n_upcalls; i++) {
1066
 
            struct upcall *upcall = &upcalls[i];
1067
 
            struct flow_miss *miss = upcall->flow_miss;
1068
 
            struct ofpbuf *packet = &upcall->dpif_upcall.packet;
1069
 
            struct ofproto_packet_in *pin;
1070
 
 
1071
 
            pin = xmalloc(sizeof *pin);
1072
 
            pin->up.packet = xmemdup(ofpbuf_data(packet), ofpbuf_size(packet));
1073
 
            pin->up.packet_len = ofpbuf_size(packet);
1074
 
            pin->up.reason = OFPR_NO_MATCH;
1075
 
            pin->up.table_id = 0;
1076
 
            pin->up.cookie = OVS_BE64_MAX;
1077
 
            flow_get_metadata(&miss->flow, &pin->up.fmd);
1078
 
            pin->send_len = 0; /* Not used for flow table misses. */
1079
 
            pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
1080
 
            ofproto_dpif_send_packet_in(miss->ofproto, pin);
1081
 
        }
1082
 
    }
1083
 
 
1084
 
    /* Execute batch. */
1085
 
    for (i = 0; i < n_ops; i++) {
1086
 
        opsp[i] = &ops[i];
1087
 
    }
1088
 
    dpif_operate(udpif->dpif, opsp, n_ops);
1089
 
}
1090
 
 
1091
 
/* Must be called with udpif->ukeys[hash % udpif->n_revalidators].mutex. */
 
1229
         *      already.
 
1230
         *
 
1231
         *    - Upcall was a recirculation but we do not have a reference to
 
1232
         *      to the recirculation ID. */
 
1233
        if (may_put && upcall->type == DPIF_UC_MISS &&
 
1234
            (!upcall->recirc || upcall->have_recirc_ref)) {
 
1235
            struct udpif_key *ukey = upcall->ukey;
 
1236
 
 
1237
            upcall->ukey_persists = true;
 
1238
            op = &ops[n_ops++];
 
1239
 
 
1240
            op->ukey = ukey;
 
1241
            op->dop.type = DPIF_OP_FLOW_PUT;
 
1242
            op->dop.u.flow_put.flags = DPIF_FP_CREATE;
 
1243
            op->dop.u.flow_put.key = ukey->key;
 
1244
            op->dop.u.flow_put.key_len = ukey->key_len;
 
1245
            op->dop.u.flow_put.mask = ukey->mask;
 
1246
            op->dop.u.flow_put.mask_len = ukey->mask_len;
 
1247
            op->dop.u.flow_put.ufid = upcall->ufid;
 
1248
            op->dop.u.flow_put.stats = NULL;
 
1249
            op->dop.u.flow_put.actions = ukey->actions->data;
 
1250
            op->dop.u.flow_put.actions_len = ukey->actions->size;
 
1251
        }
 
1252
 
 
1253
        if (upcall->xout.odp_actions->size) {
 
1254
            op = &ops[n_ops++];
 
1255
            op->ukey = NULL;
 
1256
            op->dop.type = DPIF_OP_EXECUTE;
 
1257
            op->dop.u.execute.packet = CONST_CAST(struct dp_packet *, packet);
 
1258
            odp_key_to_pkt_metadata(upcall->key, upcall->key_len,
 
1259
                                    &op->dop.u.execute.packet->md);
 
1260
            op->dop.u.execute.actions = upcall->xout.odp_actions->data;
 
1261
            op->dop.u.execute.actions_len = upcall->xout.odp_actions->size;
 
1262
            op->dop.u.execute.needs_help = (upcall->xout.slow & SLOW_ACTION) != 0;
 
1263
            op->dop.u.execute.probe = false;
 
1264
        }
 
1265
    }
 
1266
 
 
1267
    /* Execute batch.
 
1268
     *
 
1269
     * We install ukeys before installing the flows, locking them for exclusive
 
1270
     * access by this thread for the period of installation. This ensures that
 
1271
     * other threads won't attempt to delete the flows as we are creating them.
 
1272
     */
 
1273
    n_opsp = 0;
 
1274
    for (i = 0; i < n_ops; i++) {
 
1275
        struct udpif_key *ukey = ops[i].ukey;
 
1276
 
 
1277
        if (ukey) {
 
1278
            /* If we can't install the ukey, don't install the flow. */
 
1279
            if (!ukey_install_start(udpif, ukey)) {
 
1280
                ukey_delete__(ukey);
 
1281
                ops[i].ukey = NULL;
 
1282
                continue;
 
1283
            }
 
1284
        }
 
1285
        opsp[n_opsp++] = &ops[i].dop;
 
1286
    }
 
1287
    dpif_operate(udpif->dpif, opsp, n_opsp);
 
1288
    for (i = 0; i < n_ops; i++) {
 
1289
        if (ops[i].ukey) {
 
1290
            ukey_install_finish(ops[i].ukey, ops[i].dop.error);
 
1291
        }
 
1292
    }
 
1293
}
 
1294
 
 
1295
static uint32_t
 
1296
get_ufid_hash(const ovs_u128 *ufid)
 
1297
{
 
1298
    return ufid->u32[0];
 
1299
}
 
1300
 
1092
1301
static struct udpif_key *
1093
 
ukey_lookup__(struct udpif *udpif, const struct nlattr *key, size_t key_len,
1094
 
              uint32_t hash)
 
1302
ukey_lookup(struct udpif *udpif, const ovs_u128 *ufid)
1095
1303
{
1096
1304
    struct udpif_key *ukey;
1097
 
    struct hmap *hmap = &udpif->ukeys[hash % udpif->n_revalidators].hmap;
 
1305
    int idx = get_ufid_hash(ufid) % N_UMAPS;
 
1306
    struct cmap *cmap = &udpif->ukeys[idx].cmap;
1098
1307
 
1099
 
    HMAP_FOR_EACH_WITH_HASH (ukey, hmap_node, hash, hmap) {
1100
 
        if (ukey->key_len == key_len && !memcmp(ukey->key, key, key_len)) {
 
1308
    CMAP_FOR_EACH_WITH_HASH (ukey, cmap_node, get_ufid_hash(ufid), cmap) {
 
1309
        if (ovs_u128_equals(&ukey->ufid, ufid)) {
1101
1310
            return ukey;
1102
1311
        }
1103
1312
    }
1105
1314
}
1106
1315
 
1107
1316
static struct udpif_key *
1108
 
ukey_lookup(struct udpif *udpif, const struct nlattr *key, size_t key_len,
1109
 
            uint32_t hash)
1110
 
{
1111
 
    struct udpif_key *ukey;
1112
 
    uint32_t idx = hash % udpif->n_revalidators;
1113
 
 
1114
 
    ovs_mutex_lock(&udpif->ukeys[idx].mutex);
1115
 
    ukey = ukey_lookup__(udpif, key, key_len, hash);
1116
 
    ovs_mutex_unlock(&udpif->ukeys[idx].mutex);
1117
 
 
1118
 
    return ukey;
1119
 
}
1120
 
 
1121
 
static struct udpif_key *
1122
 
ukey_create(const struct nlattr *key, size_t key_len, long long int used)
1123
 
{
1124
 
    struct udpif_key *ukey = xmalloc(sizeof *ukey);
 
1317
ukey_create__(const struct nlattr *key, size_t key_len,
 
1318
              const struct nlattr *mask, size_t mask_len,
 
1319
              bool ufid_present, const ovs_u128 *ufid,
 
1320
              const unsigned pmd_id, const struct ofpbuf *actions,
 
1321
              uint64_t dump_seq, uint64_t reval_seq, long long int used,
 
1322
              const struct recirc_id_node *key_recirc, struct xlate_out *xout)
 
1323
    OVS_NO_THREAD_SAFETY_ANALYSIS
 
1324
{
 
1325
    unsigned n_recircs = (key_recirc ? 1 : 0) + (xout ? xout->n_recircs : 0);
 
1326
    struct udpif_key *ukey = xmalloc(sizeof *ukey +
 
1327
                                     n_recircs * sizeof *ukey->recircs);
 
1328
 
 
1329
    memcpy(&ukey->keybuf, key, key_len);
 
1330
    ukey->key = &ukey->keybuf.nla;
 
1331
    ukey->key_len = key_len;
 
1332
    memcpy(&ukey->maskbuf, mask, mask_len);
 
1333
    ukey->mask = &ukey->maskbuf.nla;
 
1334
    ukey->mask_len = mask_len;
 
1335
    ukey->ufid_present = ufid_present;
 
1336
    ukey->ufid = *ufid;
 
1337
    ukey->pmd_id = pmd_id;
 
1338
    ukey->hash = get_ufid_hash(&ukey->ufid);
 
1339
    ukey->actions = ofpbuf_clone(actions);
 
1340
 
1125
1341
    ovs_mutex_init(&ukey->mutex);
1126
 
 
1127
 
    ukey->key = (struct nlattr *) &ukey->key_buf;
1128
 
    memcpy(&ukey->key_buf, key, key_len);
1129
 
    ukey->key_len = key_len;
1130
 
 
1131
 
    ovs_mutex_lock(&ukey->mutex);
1132
 
    ukey->mark = false;
1133
 
    ukey->flow_exists = true;
1134
 
    ukey->created = used ? used : time_msec();
 
1342
    ukey->dump_seq = dump_seq;
 
1343
    ukey->reval_seq = reval_seq;
 
1344
    ukey->flow_exists = false;
 
1345
    ukey->created = time_msec();
1135
1346
    memset(&ukey->stats, 0, sizeof ukey->stats);
 
1347
    ukey->stats.used = used;
1136
1348
    ukey->xcache = NULL;
 
1349
 
 
1350
    ukey->n_recircs = n_recircs;
 
1351
    if (key_recirc) {
 
1352
        ukey->recircs[0] = key_recirc->id;
 
1353
    }
 
1354
    if (xout && xout->n_recircs) {
 
1355
        const uint32_t *act_recircs = xlate_out_get_recircs(xout);
 
1356
 
 
1357
        memcpy(ukey->recircs + (key_recirc ? 1 : 0), act_recircs,
 
1358
               xout->n_recircs * sizeof *ukey->recircs);
 
1359
        xlate_out_take_recircs(xout);
 
1360
    }
 
1361
    return ukey;
 
1362
}
 
1363
 
 
1364
static struct udpif_key *
 
1365
ukey_create_from_upcall(struct upcall *upcall)
 
1366
{
 
1367
    struct odputil_keybuf keystub, maskstub;
 
1368
    struct ofpbuf keybuf, maskbuf;
 
1369
    bool recirc, megaflow;
 
1370
 
 
1371
    if (upcall->key_len) {
 
1372
        ofpbuf_use_const(&keybuf, upcall->key, upcall->key_len);
 
1373
    } else {
 
1374
        /* dpif-netdev doesn't provide a netlink-formatted flow key in the
 
1375
         * upcall, so convert the upcall's flow here. */
 
1376
        ofpbuf_use_stack(&keybuf, &keystub, sizeof keystub);
 
1377
        odp_flow_key_from_flow(&keybuf, upcall->flow, &upcall->xout.wc.masks,
 
1378
                               upcall->flow->in_port.odp_port, true);
 
1379
    }
 
1380
 
 
1381
    atomic_read_relaxed(&enable_megaflows, &megaflow);
 
1382
    recirc = ofproto_dpif_get_enable_recirc(upcall->ofproto);
 
1383
    ofpbuf_use_stack(&maskbuf, &maskstub, sizeof maskstub);
 
1384
    if (megaflow) {
 
1385
        size_t max_mpls;
 
1386
 
 
1387
        max_mpls = ofproto_dpif_get_max_mpls_depth(upcall->ofproto);
 
1388
        odp_flow_key_from_mask(&maskbuf, &upcall->xout.wc.masks, upcall->flow,
 
1389
                               UINT32_MAX, max_mpls, recirc);
 
1390
    }
 
1391
 
 
1392
    return ukey_create__(keybuf.data, keybuf.size, maskbuf.data, maskbuf.size,
 
1393
                         true, upcall->ufid, upcall->pmd_id,
 
1394
                         &upcall->put_actions, upcall->dump_seq,
 
1395
                         upcall->reval_seq, 0,
 
1396
                         upcall->have_recirc_ref ? upcall->recirc : NULL,
 
1397
                         &upcall->xout);
 
1398
}
 
1399
 
 
1400
static int
 
1401
ukey_create_from_dpif_flow(const struct udpif *udpif,
 
1402
                           const struct dpif_flow *flow,
 
1403
                           struct udpif_key **ukey)
 
1404
{
 
1405
    struct dpif_flow full_flow;
 
1406
    struct ofpbuf actions;
 
1407
    uint64_t dump_seq, reval_seq;
 
1408
    uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
 
1409
    const struct nlattr *a;
 
1410
    unsigned int left;
 
1411
 
 
1412
    if (!flow->key_len || !flow->actions_len) {
 
1413
        struct ofpbuf buf;
 
1414
        int err;
 
1415
 
 
1416
        /* If the key or actions were not provided by the datapath, fetch the
 
1417
         * full flow. */
 
1418
        ofpbuf_use_stack(&buf, &stub, sizeof stub);
 
1419
        err = dpif_flow_get(udpif->dpif, NULL, 0, &flow->ufid,
 
1420
                            flow->pmd_id, &buf, &full_flow);
 
1421
        if (err) {
 
1422
            return err;
 
1423
        }
 
1424
        flow = &full_flow;
 
1425
    }
 
1426
 
 
1427
    /* Check the flow actions for recirculation action.  As recirculation
 
1428
     * relies on OVS userspace internal state, we need to delete all old
 
1429
     * datapath flows with recirculation upon OVS restart. */
 
1430
    NL_ATTR_FOR_EACH_UNSAFE (a, left, flow->actions, flow->actions_len) {
 
1431
        if (nl_attr_type(a) == OVS_ACTION_ATTR_RECIRC) {
 
1432
            return EINVAL;
 
1433
        }
 
1434
    }
 
1435
 
 
1436
    dump_seq = seq_read(udpif->dump_seq);
 
1437
    reval_seq = seq_read(udpif->reval_seq);
 
1438
    ofpbuf_use_const(&actions, &flow->actions, flow->actions_len);
 
1439
    *ukey = ukey_create__(flow->key, flow->key_len,
 
1440
                          flow->mask, flow->mask_len, flow->ufid_present,
 
1441
                          &flow->ufid, flow->pmd_id, &actions, dump_seq,
 
1442
                          reval_seq, flow->stats.used, NULL, NULL);
 
1443
 
 
1444
    return 0;
 
1445
}
 
1446
 
 
1447
/* Attempts to insert a ukey into the shared ukey maps.
 
1448
 *
 
1449
 * On success, returns true, installs the ukey and returns it in a locked
 
1450
 * state. Otherwise, returns false. */
 
1451
static bool
 
1452
ukey_install_start(struct udpif *udpif, struct udpif_key *new_ukey)
 
1453
    OVS_TRY_LOCK(true, new_ukey->mutex)
 
1454
{
 
1455
    struct umap *umap;
 
1456
    struct udpif_key *old_ukey;
 
1457
    uint32_t idx;
 
1458
    bool locked = false;
 
1459
 
 
1460
    idx = new_ukey->hash % N_UMAPS;
 
1461
    umap = &udpif->ukeys[idx];
 
1462
    ovs_mutex_lock(&umap->mutex);
 
1463
    old_ukey = ukey_lookup(udpif, &new_ukey->ufid);
 
1464
    if (old_ukey) {
 
1465
        /* Uncommon case: A ukey is already installed with the same UFID. */
 
1466
        if (old_ukey->key_len == new_ukey->key_len
 
1467
            && !memcmp(old_ukey->key, new_ukey->key, new_ukey->key_len)) {
 
1468
            COVERAGE_INC(handler_duplicate_upcall);
 
1469
        } else {
 
1470
            struct ds ds = DS_EMPTY_INITIALIZER;
 
1471
 
 
1472
            odp_format_ufid(&old_ukey->ufid, &ds);
 
1473
            ds_put_cstr(&ds, " ");
 
1474
            odp_flow_key_format(old_ukey->key, old_ukey->key_len, &ds);
 
1475
            ds_put_cstr(&ds, "\n");
 
1476
            odp_format_ufid(&new_ukey->ufid, &ds);
 
1477
            ds_put_cstr(&ds, " ");
 
1478
            odp_flow_key_format(new_ukey->key, new_ukey->key_len, &ds);
 
1479
 
 
1480
            VLOG_WARN_RL(&rl, "Conflicting ukey for flows:\n%s", ds_cstr(&ds));
 
1481
            ds_destroy(&ds);
 
1482
        }
 
1483
    } else {
 
1484
        ovs_mutex_lock(&new_ukey->mutex);
 
1485
        cmap_insert(&umap->cmap, &new_ukey->cmap_node, new_ukey->hash);
 
1486
        locked = true;
 
1487
    }
 
1488
    ovs_mutex_unlock(&umap->mutex);
 
1489
 
 
1490
    return locked;
 
1491
}
 
1492
 
 
1493
static void
 
1494
ukey_install_finish__(struct udpif_key *ukey) OVS_REQUIRES(ukey->mutex)
 
1495
{
 
1496
    ukey->flow_exists = true;
 
1497
}
 
1498
 
 
1499
static bool
 
1500
ukey_install_finish(struct udpif_key *ukey, int error)
 
1501
    OVS_RELEASES(ukey->mutex)
 
1502
{
 
1503
    if (!error) {
 
1504
        ukey_install_finish__(ukey);
 
1505
    }
1137
1506
    ovs_mutex_unlock(&ukey->mutex);
1138
1507
 
1139
 
    return ukey;
 
1508
    return !error;
1140
1509
}
1141
1510
 
1142
 
/* Checks for a ukey in 'udpif->ukeys' with the same 'ukey->key' and 'hash',
1143
 
 * and inserts 'ukey' if it does not exist.
1144
 
 *
1145
 
 * Returns true if 'ukey' was inserted into 'udpif->ukeys', false otherwise. */
1146
1511
static bool
1147
 
udpif_insert_ukey(struct udpif *udpif, struct udpif_key *ukey, uint32_t hash)
1148
 
{
1149
 
    struct udpif_key *duplicate;
1150
 
    uint32_t idx = hash % udpif->n_revalidators;
1151
 
    bool ok;
1152
 
 
1153
 
    ovs_mutex_lock(&udpif->ukeys[idx].mutex);
1154
 
    duplicate = ukey_lookup__(udpif, ukey->key, ukey->key_len, hash);
1155
 
    if (duplicate) {
1156
 
        ok = false;
1157
 
    } else {
1158
 
        hmap_insert(&udpif->ukeys[idx].hmap, &ukey->hmap_node, hash);
1159
 
        ok = true;
1160
 
    }
1161
 
    ovs_mutex_unlock(&udpif->ukeys[idx].mutex);
1162
 
 
1163
 
    return ok;
 
1512
ukey_install(struct udpif *udpif, struct udpif_key *ukey)
 
1513
{
 
1514
    /* The usual way to keep 'ukey->flow_exists' in sync with the datapath is
 
1515
     * to call ukey_install_start(), install the corresponding datapath flow,
 
1516
     * then call ukey_install_finish(). The netdev interface using upcall_cb()
 
1517
     * doesn't provide a function to separately finish the flow installation,
 
1518
     * so we perform the operations together here.
 
1519
     *
 
1520
     * This is fine currently, as revalidator threads will only delete this
 
1521
     * ukey during revalidator_sweep() and only if the dump_seq is mismatched.
 
1522
     * It is unlikely for a revalidator thread to advance dump_seq and reach
 
1523
     * the next GC phase between ukey creation and flow installation. */
 
1524
    return ukey_install_start(udpif, ukey) && ukey_install_finish(ukey, 0);
 
1525
}
 
1526
 
 
1527
/* Searches for a ukey in 'udpif->ukeys' that matches 'flow' and attempts to
 
1528
 * lock the ukey. If the ukey does not exist, create it.
 
1529
 *
 
1530
 * Returns 0 on success, setting *result to the matching ukey and returning it
 
1531
 * in a locked state. Otherwise, returns an errno and clears *result. EBUSY
 
1532
 * indicates that another thread is handling this flow. Other errors indicate
 
1533
 * an unexpected condition creating a new ukey.
 
1534
 *
 
1535
 * *error is an output parameter provided to appease the threadsafety analyser,
 
1536
 * and its value matches the return value. */
 
1537
static int
 
1538
ukey_acquire(struct udpif *udpif, const struct dpif_flow *flow,
 
1539
             struct udpif_key **result, int *error)
 
1540
    OVS_TRY_LOCK(0, (*result)->mutex)
 
1541
{
 
1542
    struct udpif_key *ukey;
 
1543
    int retval;
 
1544
 
 
1545
    ukey = ukey_lookup(udpif, &flow->ufid);
 
1546
    if (ukey) {
 
1547
        retval = ovs_mutex_trylock(&ukey->mutex);
 
1548
    } else {
 
1549
        /* Usually we try to avoid installing flows from revalidator threads,
 
1550
         * because locking on a umap may cause handler threads to block.
 
1551
         * However there are certain cases, like when ovs-vswitchd is
 
1552
         * restarted, where it is desirable to handle flows that exist in the
 
1553
         * datapath gracefully (ie, don't just clear the datapath). */
 
1554
        bool install;
 
1555
 
 
1556
        retval = ukey_create_from_dpif_flow(udpif, flow, &ukey);
 
1557
        if (retval) {
 
1558
            goto done;
 
1559
        }
 
1560
        install = ukey_install_start(udpif, ukey);
 
1561
        if (install) {
 
1562
            ukey_install_finish__(ukey);
 
1563
            retval = 0;
 
1564
        } else {
 
1565
            ukey_delete__(ukey);
 
1566
            retval = EBUSY;
 
1567
        }
 
1568
    }
 
1569
 
 
1570
done:
 
1571
    *error = retval;
 
1572
    if (retval) {
 
1573
        *result = NULL;
 
1574
    } else {
 
1575
        *result = ukey;
 
1576
    }
 
1577
    return retval;
1164
1578
}
1165
1579
 
1166
1580
static void
1167
 
ukey_delete(struct revalidator *revalidator, struct udpif_key *ukey)
 
1581
ukey_delete__(struct udpif_key *ukey)
1168
1582
    OVS_NO_THREAD_SAFETY_ANALYSIS
1169
1583
{
1170
 
    if (revalidator) {
1171
 
        hmap_remove(revalidator->ukeys, &ukey->hmap_node);
 
1584
    if (ukey) {
 
1585
        for (int i = 0; i < ukey->n_recircs; i++) {
 
1586
            recirc_free_id(ukey->recircs[i]);
 
1587
        }
 
1588
        xlate_cache_delete(ukey->xcache);
 
1589
        ofpbuf_delete(ukey->actions);
 
1590
        ovs_mutex_destroy(&ukey->mutex);
 
1591
        free(ukey);
1172
1592
    }
1173
 
    xlate_cache_delete(ukey->xcache);
1174
 
    ovs_mutex_destroy(&ukey->mutex);
1175
 
    free(ukey);
 
1593
}
 
1594
 
 
1595
static void
 
1596
ukey_delete(struct umap *umap, struct udpif_key *ukey)
 
1597
    OVS_REQUIRES(umap->mutex)
 
1598
{
 
1599
    cmap_remove(&umap->cmap, &ukey->cmap_node, ukey->hash);
 
1600
    ovsrcu_postpone(ukey_delete__, ukey);
1176
1601
}
1177
1602
 
1178
1603
static bool
1210
1635
 
1211
1636
static bool
1212
1637
revalidate_ukey(struct udpif *udpif, struct udpif_key *ukey,
1213
 
                const struct nlattr *mask, size_t mask_len,
1214
 
                const struct nlattr *actions, size_t actions_len,
1215
 
                const struct dpif_flow_stats *stats)
 
1638
                const struct dpif_flow_stats *stats, uint64_t reval_seq)
1216
1639
    OVS_REQUIRES(ukey->mutex)
1217
1640
{
1218
1641
    uint64_t slow_path_buf[128 / 8];
1222
1645
    struct dpif_flow_stats push;
1223
1646
    struct ofpbuf xout_actions;
1224
1647
    struct flow flow, dp_mask;
1225
 
    uint32_t *dp32, *xout32;
1226
 
    odp_port_t odp_in_port;
 
1648
    uint64_t *dp64, *xout64;
 
1649
    ofp_port_t ofp_in_port;
1227
1650
    struct xlate_in xin;
1228
1651
    long long int last_used;
1229
1652
    int error;
1230
1653
    size_t i;
1231
 
    bool may_learn, ok;
 
1654
    bool ok;
 
1655
    bool need_revalidate;
1232
1656
 
1233
1657
    ok = false;
1234
1658
    xoutp = NULL;
1235
1659
    netflow = NULL;
1236
1660
 
 
1661
    need_revalidate = (ukey->reval_seq != reval_seq);
1237
1662
    last_used = ukey->stats.used;
1238
1663
    push.used = stats->used;
1239
1664
    push.tcp_flags = stats->tcp_flags;
1240
 
    push.n_packets = stats->n_packets > ukey->stats.n_packets
1241
 
        ? stats->n_packets - ukey->stats.n_packets
1242
 
        : 0;
1243
 
    push.n_bytes = stats->n_bytes > ukey->stats.n_bytes
1244
 
        ? stats->n_bytes - ukey->stats.n_bytes
1245
 
        : 0;
 
1665
    push.n_packets = (stats->n_packets > ukey->stats.n_packets
 
1666
                      ? stats->n_packets - ukey->stats.n_packets
 
1667
                      : 0);
 
1668
    push.n_bytes = (stats->n_bytes > ukey->stats.n_bytes
 
1669
                    ? stats->n_bytes - ukey->stats.n_bytes
 
1670
                    : 0);
1246
1671
 
1247
 
    if (udpif->need_revalidate && last_used
 
1672
    if (need_revalidate && last_used
1248
1673
        && !should_revalidate(udpif, push.n_packets, last_used)) {
1249
1674
        ok = false;
1250
1675
        goto exit;
1252
1677
 
1253
1678
    /* We will push the stats, so update the ukey stats cache. */
1254
1679
    ukey->stats = *stats;
1255
 
    if (!push.n_packets && !udpif->need_revalidate) {
1256
 
        ok = true;
1257
 
        goto exit;
1258
 
    }
1259
 
 
1260
 
    may_learn = push.n_packets > 0;
1261
 
    if (ukey->xcache && !udpif->need_revalidate) {
1262
 
        xlate_push_stats(ukey->xcache, may_learn, &push);
1263
 
        ok = true;
1264
 
        goto exit;
1265
 
    }
1266
 
 
1267
 
    error = xlate_receive(udpif->backer, NULL, ukey->key, ukey->key_len, &flow,
1268
 
                          &ofproto, NULL, NULL, &netflow, &odp_in_port);
 
1680
    if (!push.n_packets && !need_revalidate) {
 
1681
        ok = true;
 
1682
        goto exit;
 
1683
    }
 
1684
 
 
1685
    if (ukey->xcache && !need_revalidate) {
 
1686
        xlate_push_stats(ukey->xcache, &push);
 
1687
        ok = true;
 
1688
        goto exit;
 
1689
    }
 
1690
 
 
1691
    if (odp_flow_key_to_flow(ukey->key, ukey->key_len, &flow)
 
1692
        == ODP_FIT_ERROR) {
 
1693
        goto exit;
 
1694
    }
 
1695
 
 
1696
    error = xlate_lookup(udpif->backer, &flow, &ofproto, NULL, NULL, &netflow,
 
1697
                         &ofp_in_port);
1269
1698
    if (error) {
1270
1699
        goto exit;
1271
1700
    }
1272
1701
 
1273
 
    if (udpif->need_revalidate) {
 
1702
    if (need_revalidate) {
1274
1703
        xlate_cache_clear(ukey->xcache);
1275
1704
    }
1276
1705
    if (!ukey->xcache) {
1277
1706
        ukey->xcache = xlate_cache_new();
1278
1707
    }
1279
1708
 
1280
 
    xlate_in_init(&xin, ofproto, &flow, NULL, push.tcp_flags, NULL);
1281
 
    xin.resubmit_stats = push.n_packets ? &push : NULL;
 
1709
    xlate_in_init(&xin, ofproto, &flow, ofp_in_port, NULL, push.tcp_flags,
 
1710
                  NULL);
 
1711
    if (push.n_packets) {
 
1712
        xin.resubmit_stats = &push;
 
1713
        xin.may_learn = true;
 
1714
    }
1282
1715
    xin.xcache = ukey->xcache;
1283
 
    xin.may_learn = may_learn;
1284
 
    xin.skip_wildcards = !udpif->need_revalidate;
 
1716
    xin.skip_wildcards = !need_revalidate;
1285
1717
    xlate_actions(&xin, &xout);
1286
1718
    xoutp = &xout;
1287
1719
 
1288
 
    if (!udpif->need_revalidate) {
 
1720
    if (!need_revalidate) {
1289
1721
        ok = true;
1290
1722
        goto exit;
1291
1723
    }
1292
1724
 
1293
1725
    if (!xout.slow) {
1294
 
        ofpbuf_use_const(&xout_actions, ofpbuf_data(&xout.odp_actions),
1295
 
                         ofpbuf_size(&xout.odp_actions));
 
1726
        ofpbuf_use_const(&xout_actions, xout.odp_actions->data,
 
1727
                         xout.odp_actions->size);
1296
1728
    } else {
1297
1729
        ofpbuf_use_stack(&xout_actions, slow_path_buf, sizeof slow_path_buf);
1298
 
        compose_slow_path(udpif, &xout, &flow, odp_in_port, &xout_actions);
 
1730
        compose_slow_path(udpif, &xout, &flow, flow.in_port.odp_port,
 
1731
                          &xout_actions);
1299
1732
    }
1300
1733
 
1301
 
    if (actions_len != ofpbuf_size(&xout_actions)
1302
 
        || memcmp(ofpbuf_data(&xout_actions), actions, actions_len)) {
 
1734
    if (!ofpbuf_equal(&xout_actions, ukey->actions)) {
1303
1735
        goto exit;
1304
1736
    }
1305
1737
 
1306
 
    if (odp_flow_key_to_mask(mask, mask_len, &dp_mask, &flow)
 
1738
    if (odp_flow_key_to_mask(ukey->mask, ukey->mask_len, &dp_mask, &flow)
1307
1739
        == ODP_FIT_ERROR) {
1308
1740
        goto exit;
1309
1741
    }
1313
1745
     * mask in the kernel is more specific i.e. less wildcarded, than what
1314
1746
     * we've calculated here.  This guarantees we don't catch any packets we
1315
1747
     * shouldn't with the megaflow. */
1316
 
    dp32 = (uint32_t *) &dp_mask;
1317
 
    xout32 = (uint32_t *) &xout.wc.masks;
1318
 
    for (i = 0; i < FLOW_U32S; i++) {
1319
 
        if ((dp32[i] | xout32[i]) != dp32[i]) {
 
1748
    dp64 = (uint64_t *) &dp_mask;
 
1749
    xout64 = (uint64_t *) &xout.wc.masks;
 
1750
    for (i = 0; i < FLOW_U64S; i++) {
 
1751
        if ((dp64[i] | xout64[i]) != dp64[i]) {
1320
1752
            goto exit;
1321
1753
        }
1322
1754
    }
 
1755
 
1323
1756
    ok = true;
1324
1757
 
1325
1758
exit:
1326
 
    if (netflow) {
1327
 
        if (!ok) {
1328
 
            netflow_flow_clear(netflow, &flow);
1329
 
        }
1330
 
        netflow_unref(netflow);
 
1759
    if (ok) {
 
1760
        ukey->reval_seq = reval_seq;
 
1761
    }
 
1762
    if (netflow && !ok) {
 
1763
        netflow_flow_clear(netflow, &flow);
1331
1764
    }
1332
1765
    xlate_out_uninit(xoutp);
1333
1766
    return ok;
1334
1767
}
1335
1768
 
1336
 
struct dump_op {
1337
 
    struct udpif_key *ukey;
1338
 
    struct dpif_flow_stats stats; /* Stats for 'op'. */
1339
 
    struct dpif_op op;            /* Flow del operation. */
1340
 
};
 
1769
static void
 
1770
delete_op_init__(struct udpif *udpif, struct ukey_op *op,
 
1771
                 const struct dpif_flow *flow)
 
1772
{
 
1773
    op->ukey = NULL;
 
1774
    op->dop.type = DPIF_OP_FLOW_DEL;
 
1775
    op->dop.u.flow_del.key = flow->key;
 
1776
    op->dop.u.flow_del.key_len = flow->key_len;
 
1777
    op->dop.u.flow_del.ufid = flow->ufid_present ? &flow->ufid : NULL;
 
1778
    op->dop.u.flow_del.pmd_id = flow->pmd_id;
 
1779
    op->dop.u.flow_del.stats = &op->stats;
 
1780
    op->dop.u.flow_del.terse = udpif_use_ufid(udpif);
 
1781
}
1341
1782
 
1342
1783
static void
1343
 
dump_op_init(struct dump_op *op, const struct nlattr *key, size_t key_len,
1344
 
             struct udpif_key *ukey)
 
1784
delete_op_init(struct udpif *udpif, struct ukey_op *op, struct udpif_key *ukey)
1345
1785
{
1346
1786
    op->ukey = ukey;
1347
 
    op->op.type = DPIF_OP_FLOW_DEL;
1348
 
    op->op.u.flow_del.key = key;
1349
 
    op->op.u.flow_del.key_len = key_len;
1350
 
    op->op.u.flow_del.stats = &op->stats;
 
1787
    op->dop.type = DPIF_OP_FLOW_DEL;
 
1788
    op->dop.u.flow_del.key = ukey->key;
 
1789
    op->dop.u.flow_del.key_len = ukey->key_len;
 
1790
    op->dop.u.flow_del.ufid = ukey->ufid_present ? &ukey->ufid : NULL;
 
1791
    op->dop.u.flow_del.pmd_id = ukey->pmd_id;
 
1792
    op->dop.u.flow_del.stats = &op->stats;
 
1793
    op->dop.u.flow_del.terse = udpif_use_ufid(udpif);
1351
1794
}
1352
1795
 
1353
1796
static void
1354
 
push_dump_ops__(struct udpif *udpif, struct dump_op *ops, size_t n_ops)
 
1797
push_ukey_ops__(struct udpif *udpif, struct ukey_op *ops, size_t n_ops)
1355
1798
{
1356
1799
    struct dpif_op *opsp[REVALIDATE_MAX_BATCH];
1357
1800
    size_t i;
1358
1801
 
1359
1802
    ovs_assert(n_ops <= REVALIDATE_MAX_BATCH);
1360
1803
    for (i = 0; i < n_ops; i++) {
1361
 
        opsp[i] = &ops[i].op;
 
1804
        opsp[i] = &ops[i].dop;
1362
1805
    }
1363
1806
    dpif_operate(udpif->dpif, opsp, n_ops);
1364
1807
 
1365
1808
    for (i = 0; i < n_ops; i++) {
1366
 
        struct dump_op *op = &ops[i];
 
1809
        struct ukey_op *op = &ops[i];
1367
1810
        struct dpif_flow_stats *push, *stats, push_buf;
1368
1811
 
1369
 
        stats = op->op.u.flow_del.stats;
 
1812
        stats = op->dop.u.flow_del.stats;
 
1813
        push = &push_buf;
 
1814
 
1370
1815
        if (op->ukey) {
1371
 
            push = &push_buf;
1372
1816
            ovs_mutex_lock(&op->ukey->mutex);
1373
1817
            push->used = MAX(stats->used, op->ukey->stats.used);
1374
1818
            push->tcp_flags = stats->tcp_flags | op->ukey->stats.tcp_flags;
1380
1824
        }
1381
1825
 
1382
1826
        if (push->n_packets || netflow_exists()) {
 
1827
            const struct nlattr *key = op->dop.u.flow_del.key;
 
1828
            size_t key_len = op->dop.u.flow_del.key_len;
1383
1829
            struct ofproto_dpif *ofproto;
1384
1830
            struct netflow *netflow;
 
1831
            ofp_port_t ofp_in_port;
1385
1832
            struct flow flow;
1386
 
            bool may_learn;
 
1833
            int error;
1387
1834
 
1388
 
            may_learn = push->n_packets > 0;
1389
1835
            if (op->ukey) {
1390
1836
                ovs_mutex_lock(&op->ukey->mutex);
1391
1837
                if (op->ukey->xcache) {
1392
 
                    xlate_push_stats(op->ukey->xcache, may_learn, push);
 
1838
                    xlate_push_stats(op->ukey->xcache, push);
1393
1839
                    ovs_mutex_unlock(&op->ukey->mutex);
1394
1840
                    continue;
1395
1841
                }
1396
1842
                ovs_mutex_unlock(&op->ukey->mutex);
1397
 
            }
1398
 
 
1399
 
            if (!xlate_receive(udpif->backer, NULL, op->op.u.flow_del.key,
1400
 
                               op->op.u.flow_del.key_len, &flow, &ofproto,
1401
 
                               NULL, NULL, &netflow, NULL)) {
 
1843
                key = op->ukey->key;
 
1844
                key_len = op->ukey->key_len;
 
1845
            }
 
1846
 
 
1847
            if (odp_flow_key_to_flow(key, key_len, &flow)
 
1848
                == ODP_FIT_ERROR) {
 
1849
                continue;
 
1850
            }
 
1851
 
 
1852
            error = xlate_lookup(udpif->backer, &flow, &ofproto, NULL, NULL,
 
1853
                                 &netflow, &ofp_in_port);
 
1854
            if (!error) {
1402
1855
                struct xlate_in xin;
1403
1856
 
1404
 
                xlate_in_init(&xin, ofproto, &flow, NULL, push->tcp_flags,
1405
 
                              NULL);
 
1857
                xlate_in_init(&xin, ofproto, &flow, ofp_in_port, NULL,
 
1858
                              push->tcp_flags, NULL);
1406
1859
                xin.resubmit_stats = push->n_packets ? push : NULL;
1407
 
                xin.may_learn = may_learn;
 
1860
                xin.may_learn = push->n_packets > 0;
1408
1861
                xin.skip_wildcards = true;
1409
1862
                xlate_actions_for_side_effects(&xin);
1410
1863
 
1411
1864
                if (netflow) {
1412
1865
                    netflow_flow_clear(netflow, &flow);
1413
 
                    netflow_unref(netflow);
1414
1866
                }
1415
1867
            }
1416
1868
        }
1418
1870
}
1419
1871
 
1420
1872
static void
1421
 
push_dump_ops(struct revalidator *revalidator,
1422
 
              struct dump_op *ops, size_t n_ops)
 
1873
push_ukey_ops(struct udpif *udpif, struct umap *umap,
 
1874
              struct ukey_op *ops, size_t n_ops)
1423
1875
{
1424
1876
    int i;
1425
1877
 
1426
 
    push_dump_ops__(revalidator->udpif, ops, n_ops);
 
1878
    push_ukey_ops__(udpif, ops, n_ops);
 
1879
    ovs_mutex_lock(&umap->mutex);
1427
1880
    for (i = 0; i < n_ops; i++) {
1428
 
        ukey_delete(revalidator, ops[i].ukey);
 
1881
        ukey_delete(umap, ops[i].ukey);
1429
1882
    }
 
1883
    ovs_mutex_unlock(&umap->mutex);
 
1884
}
 
1885
 
 
1886
static void
 
1887
log_unexpected_flow(const struct dpif_flow *flow, int error)
 
1888
{
 
1889
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 60);
 
1890
    struct ds ds = DS_EMPTY_INITIALIZER;
 
1891
 
 
1892
    ds_put_format(&ds, "Failed to acquire udpif_key corresponding to "
 
1893
                  "unexpected flow (%s): ", ovs_strerror(error));
 
1894
    odp_format_ufid(&flow->ufid, &ds);
 
1895
    VLOG_WARN_RL(&rl, "%s", ds_cstr(&ds));
1430
1896
}
1431
1897
 
1432
1898
static void
1433
1899
revalidate(struct revalidator *revalidator)
1434
1900
{
1435
1901
    struct udpif *udpif = revalidator->udpif;
1436
 
 
1437
 
    struct dump_op ops[REVALIDATE_MAX_BATCH];
1438
 
    const struct nlattr *key, *mask, *actions;
1439
 
    size_t key_len, mask_len, actions_len;
1440
 
    const struct dpif_flow_stats *stats;
1441
 
    long long int now;
 
1902
    struct dpif_flow_dump_thread *dump_thread;
 
1903
    uint64_t dump_seq, reval_seq;
1442
1904
    unsigned int flow_limit;
1443
 
    size_t n_ops;
1444
 
    void *state;
1445
 
 
1446
 
    n_ops = 0;
1447
 
    now = time_msec();
1448
 
    atomic_read(&udpif->flow_limit, &flow_limit);
1449
 
 
1450
 
    dpif_flow_dump_state_init(udpif->dpif, &state);
1451
 
    while (dpif_flow_dump_next(&udpif->dump, state, &key, &key_len, &mask,
1452
 
                               &mask_len, &actions, &actions_len, &stats)) {
1453
 
        struct udpif_key *ukey;
1454
 
        bool mark, may_destroy;
1455
 
        long long int used, max_idle;
1456
 
        uint32_t hash;
1457
 
        size_t n_flows;
1458
 
 
1459
 
        hash = hash_bytes(key, key_len, udpif->secret);
1460
 
        ukey = ukey_lookup(udpif, key, key_len, hash);
1461
 
 
1462
 
        used = stats->used;
1463
 
        if (!ukey) {
1464
 
            ukey = ukey_create(key, key_len, used);
1465
 
            if (!udpif_insert_ukey(udpif, ukey, hash)) {
1466
 
                /* The same ukey has already been created. This means that
1467
 
                 * another revalidator is processing this flow
1468
 
                 * concurrently, so don't bother processing it. */
1469
 
                COVERAGE_INC(upcall_duplicate_flow);
1470
 
                ukey_delete(NULL, ukey);
1471
 
                goto next;
1472
 
            }
1473
 
        }
1474
 
 
1475
 
        if (ovs_mutex_trylock(&ukey->mutex)) {
1476
 
            /* The flow has been dumped, and is being handled by another
1477
 
             * revalidator concurrently. This can occasionally occur if the
1478
 
             * datapath is changed in the middle of a flow dump. Rather than
1479
 
             * perform the same work twice, skip the flow this time. */
1480
 
            COVERAGE_INC(upcall_duplicate_flow);
1481
 
            goto next;
1482
 
        }
1483
 
 
1484
 
        if (ukey->mark || !ukey->flow_exists) {
1485
 
            /* The flow has already been dumped and handled by another
1486
 
             * revalidator during this flow dump operation. Skip it. */
1487
 
            COVERAGE_INC(upcall_duplicate_flow);
 
1905
 
 
1906
    dump_seq = seq_read(udpif->dump_seq);
 
1907
    reval_seq = seq_read(udpif->reval_seq);
 
1908
    atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
 
1909
    dump_thread = dpif_flow_dump_thread_create(udpif->dump);
 
1910
    for (;;) {
 
1911
        struct ukey_op ops[REVALIDATE_MAX_BATCH];
 
1912
        int n_ops = 0;
 
1913
 
 
1914
        struct dpif_flow flows[REVALIDATE_MAX_BATCH];
 
1915
        const struct dpif_flow *f;
 
1916
        int n_dumped;
 
1917
 
 
1918
        long long int max_idle;
 
1919
        long long int now;
 
1920
        size_t n_dp_flows;
 
1921
        bool kill_them_all;
 
1922
 
 
1923
        n_dumped = dpif_flow_dump_next(dump_thread, flows, ARRAY_SIZE(flows));
 
1924
        if (!n_dumped) {
 
1925
            break;
 
1926
        }
 
1927
 
 
1928
        now = time_msec();
 
1929
 
 
1930
        /* In normal operation we want to keep flows around until they have
 
1931
         * been idle for 'ofproto_max_idle' milliseconds.  However:
 
1932
         *
 
1933
         *     - If the number of datapath flows climbs above 'flow_limit',
 
1934
         *       drop that down to 100 ms to try to bring the flows down to
 
1935
         *       the limit.
 
1936
         *
 
1937
         *     - If the number of datapath flows climbs above twice
 
1938
         *       'flow_limit', delete all the datapath flows as an emergency
 
1939
         *       measure.  (We reassess this condition for the next batch of
 
1940
         *       datapath flows, so we will recover before all the flows are
 
1941
         *       gone.) */
 
1942
        n_dp_flows = udpif_get_n_flows(udpif);
 
1943
        kill_them_all = n_dp_flows > flow_limit * 2;
 
1944
        max_idle = n_dp_flows > flow_limit ? 100 : ofproto_max_idle;
 
1945
 
 
1946
        for (f = flows; f < &flows[n_dumped]; f++) {
 
1947
            long long int used = f->stats.used;
 
1948
            struct udpif_key *ukey;
 
1949
            bool already_dumped, keep;
 
1950
            int error;
 
1951
 
 
1952
            if (ukey_acquire(udpif, f, &ukey, &error)) {
 
1953
                if (error == EBUSY) {
 
1954
                    /* Another thread is processing this flow, so don't bother
 
1955
                     * processing it.*/
 
1956
                    COVERAGE_INC(upcall_ukey_contention);
 
1957
                } else {
 
1958
                    log_unexpected_flow(f, error);
 
1959
                    if (error != ENOENT) {
 
1960
                        delete_op_init__(udpif, &ops[n_ops++], f);
 
1961
                    }
 
1962
                }
 
1963
                continue;
 
1964
            }
 
1965
 
 
1966
            already_dumped = ukey->dump_seq == dump_seq;
 
1967
            if (already_dumped) {
 
1968
                /* The flow has already been handled during this flow dump
 
1969
                 * operation. Skip it. */
 
1970
                if (ukey->xcache) {
 
1971
                    COVERAGE_INC(dumped_duplicate_flow);
 
1972
                } else {
 
1973
                    COVERAGE_INC(dumped_new_flow);
 
1974
                }
 
1975
                ovs_mutex_unlock(&ukey->mutex);
 
1976
                continue;
 
1977
            }
 
1978
 
 
1979
            if (!used) {
 
1980
                used = ukey->created;
 
1981
            }
 
1982
            if (kill_them_all || (used && used < now - max_idle)) {
 
1983
                keep = false;
 
1984
            } else {
 
1985
                keep = revalidate_ukey(udpif, ukey, &f->stats, reval_seq);
 
1986
            }
 
1987
            ukey->dump_seq = dump_seq;
 
1988
            ukey->flow_exists = keep;
 
1989
 
 
1990
            if (!keep) {
 
1991
                delete_op_init(udpif, &ops[n_ops++], ukey);
 
1992
            }
1488
1993
            ovs_mutex_unlock(&ukey->mutex);
1489
 
            goto next;
1490
 
        }
1491
 
 
1492
 
        if (!used) {
1493
 
            used = ukey->created;
1494
 
        }
1495
 
        n_flows = udpif_get_n_flows(udpif);
1496
 
        max_idle = ofproto_max_idle;
1497
 
        if (n_flows > flow_limit) {
1498
 
            max_idle = 100;
1499
 
        }
1500
 
 
1501
 
        if ((used && used < now - max_idle) || n_flows > flow_limit * 2) {
1502
 
            mark = false;
1503
 
        } else {
1504
 
            mark = revalidate_ukey(udpif, ukey, mask, mask_len, actions,
1505
 
                                   actions_len, stats);
1506
 
        }
1507
 
        ukey->mark = ukey->flow_exists = mark;
1508
 
 
1509
 
        if (!mark) {
1510
 
            dump_op_init(&ops[n_ops++], key, key_len, ukey);
1511
 
        }
1512
 
        ovs_mutex_unlock(&ukey->mutex);
1513
 
 
1514
 
    next:
1515
 
        may_destroy = dpif_flow_dump_next_may_destroy_keys(&udpif->dump,
1516
 
                                                           state);
1517
 
 
1518
 
        /* Only update 'now' immediately before 'buffer' will be updated.
1519
 
         * This gives us the current time relative to the time the datapath
1520
 
         * will write into 'stats'. */
1521
 
        if (may_destroy) {
1522
 
            now = time_msec();
1523
 
        }
1524
 
 
1525
 
        /* Only do a dpif_operate when we've hit our maximum batch, or when our
1526
 
         * memory is about to be clobbered by the next call to
1527
 
         * dpif_flow_dump_next(). */
1528
 
        if (n_ops == REVALIDATE_MAX_BATCH || (n_ops && may_destroy)) {
1529
 
            push_dump_ops__(udpif, ops, n_ops);
1530
 
            n_ops = 0;
1531
 
        }
1532
 
    }
1533
 
 
1534
 
    if (n_ops) {
1535
 
        push_dump_ops__(udpif, ops, n_ops);
1536
 
    }
1537
 
 
1538
 
    dpif_flow_dump_state_uninit(udpif->dpif, state);
 
1994
        }
 
1995
 
 
1996
        if (n_ops) {
 
1997
            push_ukey_ops__(udpif, ops, n_ops);
 
1998
        }
 
1999
        ovsrcu_quiesce();
 
2000
    }
 
2001
    dpif_flow_dump_thread_destroy(dump_thread);
1539
2002
}
1540
2003
 
1541
 
/* Called with exclusive access to 'revalidator' and 'ukey'. */
1542
2004
static bool
1543
 
handle_missed_revalidation(struct revalidator *revalidator,
 
2005
handle_missed_revalidation(struct udpif *udpif, uint64_t reval_seq,
1544
2006
                           struct udpif_key *ukey)
1545
 
    OVS_NO_THREAD_SAFETY_ANALYSIS
1546
2007
{
1547
 
    struct udpif *udpif = revalidator->udpif;
1548
 
    struct nlattr *mask, *actions;
1549
 
    size_t mask_len, actions_len;
1550
2008
    struct dpif_flow_stats stats;
1551
 
    struct ofpbuf *buf;
1552
 
    bool keep = false;
 
2009
    bool keep;
1553
2010
 
1554
2011
    COVERAGE_INC(revalidate_missed_dp_flow);
1555
2012
 
1556
 
    if (!dpif_flow_get(udpif->dpif, ukey->key, ukey->key_len, &buf,
1557
 
                       &mask, &mask_len, &actions, &actions_len, &stats)) {
1558
 
        keep = revalidate_ukey(udpif, ukey, mask, mask_len, actions,
1559
 
                               actions_len, &stats);
1560
 
        ofpbuf_delete(buf);
1561
 
    }
 
2013
    memset(&stats, 0, sizeof stats);
 
2014
    ovs_mutex_lock(&ukey->mutex);
 
2015
    keep = revalidate_ukey(udpif, ukey, &stats, reval_seq);
 
2016
    ovs_mutex_unlock(&ukey->mutex);
1562
2017
 
1563
2018
    return keep;
1564
2019
}
1565
2020
 
1566
2021
static void
1567
2022
revalidator_sweep__(struct revalidator *revalidator, bool purge)
1568
 
    OVS_NO_THREAD_SAFETY_ANALYSIS
1569
2023
{
1570
 
    struct dump_op ops[REVALIDATE_MAX_BATCH];
1571
 
    struct udpif_key *ukey, *next;
1572
 
    size_t n_ops;
1573
 
 
1574
 
    n_ops = 0;
1575
 
 
1576
 
    /* During garbage collection, this revalidator completely owns its ukeys
1577
 
     * map, and therefore doesn't need to do any locking. */
1578
 
    HMAP_FOR_EACH_SAFE (ukey, next, hmap_node, revalidator->ukeys) {
1579
 
        if (ukey->flow_exists) {
1580
 
            bool missed_flow = !ukey->mark;
1581
 
 
1582
 
            ukey->mark = false;
1583
 
            if (purge
1584
 
                || (missed_flow
1585
 
                    && revalidator->udpif->need_revalidate
1586
 
                    && !handle_missed_revalidation(revalidator, ukey))) {
1587
 
                struct dump_op *op = &ops[n_ops++];
1588
 
 
1589
 
                dump_op_init(op, ukey->key, ukey->key_len, ukey);
 
2024
    struct udpif *udpif;
 
2025
    uint64_t dump_seq, reval_seq;
 
2026
    int slice;
 
2027
 
 
2028
    udpif = revalidator->udpif;
 
2029
    dump_seq = seq_read(udpif->dump_seq);
 
2030
    reval_seq = seq_read(udpif->reval_seq);
 
2031
    slice = revalidator - udpif->revalidators;
 
2032
    ovs_assert(slice < udpif->n_revalidators);
 
2033
 
 
2034
    for (int i = slice; i < N_UMAPS; i += udpif->n_revalidators) {
 
2035
        struct ukey_op ops[REVALIDATE_MAX_BATCH];
 
2036
        struct udpif_key *ukey;
 
2037
        struct umap *umap = &udpif->ukeys[i];
 
2038
        size_t n_ops = 0;
 
2039
 
 
2040
        CMAP_FOR_EACH(ukey, cmap_node, &umap->cmap) {
 
2041
            bool flow_exists, seq_mismatch;
 
2042
 
 
2043
            /* Handler threads could be holding a ukey lock while it installs a
 
2044
             * new flow, so don't hang around waiting for access to it. */
 
2045
            if (ovs_mutex_trylock(&ukey->mutex)) {
 
2046
                continue;
 
2047
            }
 
2048
            flow_exists = ukey->flow_exists;
 
2049
            seq_mismatch = (ukey->dump_seq != dump_seq
 
2050
                            && ukey->reval_seq != reval_seq);
 
2051
            ovs_mutex_unlock(&ukey->mutex);
 
2052
 
 
2053
            if (flow_exists
 
2054
                && (purge
 
2055
                    || (seq_mismatch
 
2056
                        && !handle_missed_revalidation(udpif, reval_seq,
 
2057
                                                       ukey)))) {
 
2058
                struct ukey_op *op = &ops[n_ops++];
 
2059
 
 
2060
                delete_op_init(udpif, op, ukey);
1590
2061
                if (n_ops == REVALIDATE_MAX_BATCH) {
1591
 
                    push_dump_ops(revalidator, ops, n_ops);
 
2062
                    push_ukey_ops(udpif, umap, ops, n_ops);
1592
2063
                    n_ops = 0;
1593
2064
                }
 
2065
            } else if (!flow_exists) {
 
2066
                ovs_mutex_lock(&umap->mutex);
 
2067
                ukey_delete(umap, ukey);
 
2068
                ovs_mutex_unlock(&umap->mutex);
1594
2069
            }
1595
 
        } else {
1596
 
            ukey_delete(revalidator, ukey);
1597
2070
        }
1598
 
    }
1599
2071
 
1600
 
    if (n_ops) {
1601
 
        push_dump_ops(revalidator, ops, n_ops);
 
2072
        if (n_ops) {
 
2073
            push_ukey_ops(udpif, umap, ops, n_ops);
 
2074
        }
 
2075
        ovsrcu_quiesce();
1602
2076
    }
1603
2077
}
1604
2078
 
1623
2097
 
1624
2098
    LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1625
2099
        unsigned int flow_limit;
 
2100
        bool ufid_enabled;
1626
2101
        size_t i;
1627
2102
 
1628
 
        atomic_read(&udpif->flow_limit, &flow_limit);
 
2103
        atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
 
2104
        ufid_enabled = udpif_use_ufid(udpif);
1629
2105
 
1630
2106
        ds_put_format(&ds, "%s:\n", dpif_name(udpif->dpif));
1631
2107
        ds_put_format(&ds, "\tflows         : (current %lu)"
1632
2108
            " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif),
1633
2109
            udpif->avg_n_flows, udpif->max_n_flows, flow_limit);
1634
2110
        ds_put_format(&ds, "\tdump duration : %lldms\n", udpif->dump_duration);
1635
 
 
 
2111
        ds_put_format(&ds, "\tufid enabled : ");
 
2112
        if (ufid_enabled) {
 
2113
            ds_put_format(&ds, "true\n");
 
2114
        } else {
 
2115
            ds_put_format(&ds, "false\n");
 
2116
        }
1636
2117
        ds_put_char(&ds, '\n');
 
2118
 
1637
2119
        for (i = 0; i < n_revalidators; i++) {
1638
2120
            struct revalidator *revalidator = &udpif->revalidators[i];
 
2121
            int j, elements = 0;
1639
2122
 
1640
 
            ovs_mutex_lock(&udpif->ukeys[i].mutex);
1641
 
            ds_put_format(&ds, "\t%u: (keys %"PRIuSIZE")\n",
1642
 
                          revalidator->id, hmap_count(&udpif->ukeys[i].hmap));
1643
 
            ovs_mutex_unlock(&udpif->ukeys[i].mutex);
 
2123
            for (j = i; j < N_UMAPS; j += n_revalidators) {
 
2124
                elements += cmap_count(&udpif->ukeys[j].cmap);
 
2125
            }
 
2126
            ds_put_format(&ds, "\t%u: (keys %d)\n", revalidator->id, elements);
1644
2127
        }
1645
2128
    }
1646
2129
 
1658
2141
                                 const char *argv[] OVS_UNUSED,
1659
2142
                                 void *aux OVS_UNUSED)
1660
2143
{
1661
 
    atomic_store(&enable_megaflows, false);
 
2144
    atomic_store_relaxed(&enable_megaflows, false);
1662
2145
    udpif_flush_all_datapaths();
1663
2146
    unixctl_command_reply(conn, "megaflows disabled");
1664
2147
}
1673
2156
                                const char *argv[] OVS_UNUSED,
1674
2157
                                void *aux OVS_UNUSED)
1675
2158
{
1676
 
    atomic_store(&enable_megaflows, true);
 
2159
    atomic_store_relaxed(&enable_megaflows, true);
1677
2160
    udpif_flush_all_datapaths();
1678
2161
    unixctl_command_reply(conn, "megaflows enabled");
1679
2162
}
1680
2163
 
 
2164
/* Disable skipping flow attributes during flow dump.
 
2165
 *
 
2166
 * This command is only needed for advanced debugging, so it's not
 
2167
 * documented in the man page. */
 
2168
static void
 
2169
upcall_unixctl_disable_ufid(struct unixctl_conn *conn, int argc OVS_UNUSED,
 
2170
                           const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
 
2171
{
 
2172
    atomic_store_relaxed(&enable_ufid, false);
 
2173
    unixctl_command_reply(conn, "Datapath dumping tersely using UFID disabled");
 
2174
}
 
2175
 
 
2176
/* Re-enable skipping flow attributes during flow dump.
 
2177
 *
 
2178
 * This command is only needed for advanced debugging, so it's not documented
 
2179
 * in the man page. */
 
2180
static void
 
2181
upcall_unixctl_enable_ufid(struct unixctl_conn *conn, int argc OVS_UNUSED,
 
2182
                          const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
 
2183
{
 
2184
    atomic_store_relaxed(&enable_ufid, true);
 
2185
    unixctl_command_reply(conn, "Datapath dumping tersely using UFID enabled "
 
2186
                                "for supported datapaths");
 
2187
}
 
2188
 
1681
2189
/* Set the flow limit.
1682
2190
 *
1683
2191
 * This command is only needed for advanced debugging, so it's not
1693
2201
    unsigned int flow_limit = atoi(argv[1]);
1694
2202
 
1695
2203
    LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1696
 
        atomic_store(&udpif->flow_limit, flow_limit);
 
2204
        atomic_store_relaxed(&udpif->flow_limit, flow_limit);
1697
2205
    }
1698
2206
    ds_put_format(&ds, "set flow_limit to %u\n", flow_limit);
1699
2207
    unixctl_command_reply(conn, ds_cstr(&ds));
1700
2208
    ds_destroy(&ds);
1701
2209
}
 
2210
 
 
2211
static void
 
2212
upcall_unixctl_dump_wait(struct unixctl_conn *conn,
 
2213
                         int argc OVS_UNUSED,
 
2214
                         const char *argv[] OVS_UNUSED,
 
2215
                         void *aux OVS_UNUSED)
 
2216
{
 
2217
    if (list_is_singleton(&all_udpifs)) {
 
2218
        struct udpif *udpif = NULL;
 
2219
        size_t len;
 
2220
 
 
2221
        udpif = OBJECT_CONTAINING(list_front(&all_udpifs), udpif, list_node);
 
2222
        len = (udpif->n_conns + 1) * sizeof *udpif->conns;
 
2223
        udpif->conn_seq = seq_read(udpif->dump_seq);
 
2224
        udpif->conns = xrealloc(udpif->conns, len);
 
2225
        udpif->conns[udpif->n_conns++] = conn;
 
2226
    } else {
 
2227
        unixctl_command_reply_error(conn, "can't wait on multiple udpifs.");
 
2228
    }
 
2229
}
 
2230
 
 
2231
static void
 
2232
upcall_unixctl_purge(struct unixctl_conn *conn, int argc OVS_UNUSED,
 
2233
                     const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
 
2234
{
 
2235
    struct udpif *udpif;
 
2236
 
 
2237
    LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
 
2238
        int n;
 
2239
 
 
2240
        for (n = 0; n < udpif->n_revalidators; n++) {
 
2241
            revalidator_purge(&udpif->revalidators[n]);
 
2242
        }
 
2243
    }
 
2244
    unixctl_command_reply(conn, "");
 
2245
}