Coverage Report

Created: 2026-01-17 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/dpif.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at:
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <config.h>
18
#include "dpif-provider.h"
19
20
#include <ctype.h>
21
#include <errno.h>
22
#include <inttypes.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "coverage.h"
27
#include "dp-packet.h"
28
#include "dpctl.h"
29
#include "dpif-netdev.h"
30
#include "dpif-offload.h"
31
#include "dpif-offload-provider.h"
32
#include "flow.h"
33
#include "netdev-provider.h"
34
#include "netdev.h"
35
#include "netlink.h"
36
#include "odp-execute.h"
37
#include "odp-util.h"
38
#include "packets.h"
39
#include "route-table.h"
40
#include "seq.h"
41
#include "sset.h"
42
#include "timeval.h"
43
#include "tnl-neigh-cache.h"
44
#include "tnl-ports.h"
45
#include "util.h"
46
#include "uuid.h"
47
#include "valgrind.h"
48
#include "openvswitch/dynamic-string.h"
49
#include "openvswitch/ofp-errors.h"
50
#include "openvswitch/ofp-print.h"
51
#include "openvswitch/ofpbuf.h"
52
#include "openvswitch/poll-loop.h"
53
#include "openvswitch/shash.h"
54
#include "openvswitch/usdt-probes.h"
55
#include "openvswitch/vlog.h"
56
57
VLOG_DEFINE_THIS_MODULE(dpif);
58
59
COVERAGE_DEFINE(dpif_destroy);
60
COVERAGE_DEFINE(dpif_execute);
61
COVERAGE_DEFINE(dpif_execute_error);
62
COVERAGE_DEFINE(dpif_execute_with_help);
63
COVERAGE_DEFINE(dpif_flow_del);
64
COVERAGE_DEFINE(dpif_flow_del_error);
65
COVERAGE_DEFINE(dpif_flow_flush);
66
COVERAGE_DEFINE(dpif_flow_get);
67
COVERAGE_DEFINE(dpif_flow_get_error);
68
COVERAGE_DEFINE(dpif_flow_put);
69
COVERAGE_DEFINE(dpif_flow_put_error);
70
COVERAGE_DEFINE(dpif_meter_del);
71
COVERAGE_DEFINE(dpif_meter_get);
72
COVERAGE_DEFINE(dpif_meter_set);
73
COVERAGE_DEFINE(dpif_port_add);
74
COVERAGE_DEFINE(dpif_port_del);
75
COVERAGE_DEFINE(dpif_purge);
76
77
static const struct dpif_class *base_dpif_classes[] = {
78
#if defined(__linux__) || defined(_WIN32)
79
    &dpif_netlink_class,
80
#endif
81
    &dpif_netdev_class,
82
};
83
84
struct registered_dpif_class {
85
    const struct dpif_class *dpif_class;
86
    int refcount;
87
};
88
static struct shash dpif_classes = SHASH_INITIALIZER(&dpif_classes);
89
static struct sset dpif_disallowed = SSET_INITIALIZER(&dpif_disallowed);
90
91
/* Protects 'dpif_classes', including the refcount, and 'dpif_disallowed'. */
92
static struct ovs_mutex dpif_mutex = OVS_MUTEX_INITIALIZER;
93
94
/* Rate limit for individual messages going to or from the datapath, output at
95
 * DBG level.  This is very high because, if these are enabled, it is because
96
 * we really need to see them. */
97
static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
98
99
/* Not really much point in logging many dpif errors. */
100
static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
101
102
static void log_operation(const struct dpif *, const char *operation,
103
                          int error);
104
static bool should_log_flow_message(const struct vlog_module *module,
105
                                    int error);
106
107
/* Incremented whenever tnl route, arp, etc changes. */
108
struct seq *tnl_conf_seq;
109
110
static bool
111
dpif_is_tap_port(const char *type)
112
0
{
113
0
    return !strcmp(type, "tap");
114
0
}
115
116
static void
117
dp_initialize(void)
118
0
{
119
0
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
120
121
0
    if (ovsthread_once_start(&once)) {
122
0
        int i;
123
124
0
        tnl_conf_seq = seq_create();
125
0
        dpctl_unixctl_register();
126
0
        tnl_port_map_init();
127
0
        tnl_neigh_cache_init();
128
0
        route_table_init();
129
0
        dpif_offload_module_init();
130
131
0
        for (i = 0; i < ARRAY_SIZE(base_dpif_classes); i++) {
132
0
            dp_register_provider(base_dpif_classes[i]);
133
0
        }
134
135
0
        ovsthread_once_done(&once);
136
0
    }
137
0
}
138
139
static int
140
dp_register_provider__(const struct dpif_class *new_class)
141
0
{
142
0
    struct registered_dpif_class *registered_class;
143
0
    int error;
144
145
0
    if (sset_contains(&dpif_disallowed, new_class->type)) {
146
0
        VLOG_DBG("attempted to register disallowed provider: %s",
147
0
                 new_class->type);
148
0
        return EINVAL;
149
0
    }
150
151
0
    if (shash_find(&dpif_classes, new_class->type)) {
152
0
        VLOG_WARN("attempted to register duplicate datapath provider: %s",
153
0
                  new_class->type);
154
0
        return EEXIST;
155
0
    }
156
157
0
    error = new_class->init ? new_class->init() : 0;
158
0
    if (error) {
159
0
        VLOG_WARN("failed to initialize %s datapath class: %s",
160
0
                  new_class->type, ovs_strerror(error));
161
0
        return error;
162
0
    }
163
164
0
    registered_class = xmalloc(sizeof *registered_class);
165
0
    registered_class->dpif_class = new_class;
166
0
    registered_class->refcount = 0;
167
168
0
    shash_add(&dpif_classes, new_class->type, registered_class);
169
170
0
    return 0;
171
0
}
172
173
/* Registers a new datapath provider.  After successful registration, new
174
 * datapaths of that type can be opened using dpif_open(). */
175
int
176
dp_register_provider(const struct dpif_class *new_class)
177
0
{
178
0
    int error;
179
180
0
    ovs_mutex_lock(&dpif_mutex);
181
0
    error = dp_register_provider__(new_class);
182
0
    ovs_mutex_unlock(&dpif_mutex);
183
184
0
    return error;
185
0
}
186
187
/* Unregisters a datapath provider.  'type' must have been previously
188
 * registered and not currently be in use by any dpifs.  After unregistration
189
 * new datapaths of that type cannot be opened using dpif_open(). */
190
static int
191
dp_unregister_provider__(const char *type)
192
0
{
193
0
    struct shash_node *node;
194
0
    struct registered_dpif_class *registered_class;
195
196
0
    node = shash_find(&dpif_classes, type);
197
0
    if (!node) {
198
0
        return EAFNOSUPPORT;
199
0
    }
200
201
0
    registered_class = node->data;
202
0
    if (registered_class->refcount) {
203
0
        VLOG_WARN("attempted to unregister in use datapath provider: %s", type);
204
0
        return EBUSY;
205
0
    }
206
207
0
    shash_delete(&dpif_classes, node);
208
0
    free(registered_class);
209
210
0
    return 0;
211
0
}
212
213
/* Unregisters a datapath provider.  'type' must have been previously
214
 * registered and not currently be in use by any dpifs.  After unregistration
215
 * new datapaths of that type cannot be opened using dpif_open(). */
216
int
217
dp_unregister_provider(const char *type)
218
0
{
219
0
    int error;
220
221
0
    dp_initialize();
222
223
0
    ovs_mutex_lock(&dpif_mutex);
224
0
    error = dp_unregister_provider__(type);
225
0
    ovs_mutex_unlock(&dpif_mutex);
226
227
0
    return error;
228
0
}
229
230
/* Disallows a provider.  Causes future calls of dp_register_provider() with
231
 * a dpif_class which implements 'type' to fail. */
232
void
233
dp_disallow_provider(const char *type)
234
0
{
235
0
    ovs_mutex_lock(&dpif_mutex);
236
0
    sset_add(&dpif_disallowed, type);
237
0
    ovs_mutex_unlock(&dpif_mutex);
238
0
}
239
240
/* Adds the types of all currently registered datapath providers to 'types'.
241
 * The caller must first initialize the sset. */
242
void
243
dp_enumerate_types(struct sset *types)
244
0
{
245
0
    struct shash_node *node;
246
247
0
    dp_initialize();
248
249
0
    ovs_mutex_lock(&dpif_mutex);
250
0
    SHASH_FOR_EACH(node, &dpif_classes) {
251
0
        const struct registered_dpif_class *registered_class = node->data;
252
0
        sset_add(types, registered_class->dpif_class->type);
253
0
    }
254
0
    ovs_mutex_unlock(&dpif_mutex);
255
0
}
256
257
static void
258
dp_class_unref(struct registered_dpif_class *rc)
259
0
{
260
0
    ovs_mutex_lock(&dpif_mutex);
261
0
    ovs_assert(rc->refcount);
262
0
    rc->refcount--;
263
0
    ovs_mutex_unlock(&dpif_mutex);
264
0
}
265
266
static struct registered_dpif_class *
267
dp_class_lookup(const char *type)
268
0
{
269
0
    struct registered_dpif_class *rc;
270
271
0
    ovs_mutex_lock(&dpif_mutex);
272
0
    rc = shash_find_data(&dpif_classes, type);
273
0
    if (rc) {
274
0
        rc->refcount++;
275
0
    }
276
0
    ovs_mutex_unlock(&dpif_mutex);
277
278
0
    return rc;
279
0
}
280
281
/* Clears 'names' and enumerates the names of all known created datapaths with
282
 * the given 'type'.  The caller must first initialize the sset.  Returns 0 if
283
 * successful, otherwise a positive errno value.
284
 *
285
 * Some kinds of datapaths might not be practically enumerable.  This is not
286
 * considered an error. */
287
int
288
dp_enumerate_names(const char *type, struct sset *names)
289
0
{
290
0
    struct registered_dpif_class *registered_class;
291
0
    const struct dpif_class *dpif_class;
292
0
    int error;
293
294
0
    dp_initialize();
295
0
    sset_clear(names);
296
297
0
    registered_class = dp_class_lookup(type);
298
0
    if (!registered_class) {
299
0
        VLOG_WARN("could not enumerate unknown type: %s", type);
300
0
        return EAFNOSUPPORT;
301
0
    }
302
303
0
    dpif_class = registered_class->dpif_class;
304
0
    error = (dpif_class->enumerate
305
0
             ? dpif_class->enumerate(names, dpif_class)
306
0
             : 0);
307
0
    if (error) {
308
0
        VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type,
309
0
                   ovs_strerror(error));
310
0
    }
311
0
    dp_class_unref(registered_class);
312
313
0
    return error;
314
0
}
315
316
/* Parses 'datapath_name_', which is of the form [type@]name into its
317
 * component pieces.  'name' and 'type' must be freed by the caller.
318
 *
319
 * The returned 'type' is normalized, as if by dpif_normalize_type(). */
320
void
321
dp_parse_name(const char *datapath_name_, char **name, char **type)
322
0
{
323
0
    char *datapath_name = xstrdup(datapath_name_);
324
0
    char *separator;
325
326
0
    separator = strchr(datapath_name, '@');
327
0
    if (separator) {
328
0
        *separator = '\0';
329
0
        *type = datapath_name;
330
0
        *name = xstrdup(dpif_normalize_type(separator + 1));
331
0
    } else {
332
0
        *name = datapath_name;
333
0
        *type = xstrdup(dpif_normalize_type(NULL));
334
0
    }
335
0
}
336
337
static int
338
do_open(const char *name, const char *type, bool create, struct dpif **dpifp)
339
0
{
340
0
    struct dpif *dpif = NULL;
341
0
    int error;
342
0
    struct registered_dpif_class *registered_class;
343
344
0
    dp_initialize();
345
346
0
    type = dpif_normalize_type(type);
347
0
    registered_class = dp_class_lookup(type);
348
0
    if (!registered_class) {
349
0
        VLOG_WARN("could not create datapath %s of unknown type %s", name,
350
0
                  type);
351
0
        error = EAFNOSUPPORT;
352
0
        goto exit;
353
0
    }
354
355
0
    error = registered_class->dpif_class->open(registered_class->dpif_class,
356
0
                                               name, create, &dpif);
357
0
    if (!error) {
358
0
        const char *dpif_type_str = dpif_normalize_type(dpif_type(dpif));
359
0
        struct dpif_port_dump port_dump;
360
0
        struct dpif_port dpif_port;
361
362
0
        ovs_assert(dpif->dpif_class == registered_class->dpif_class);
363
364
        /* Only if a new provider was attached should we try to add already
365
         * existing ports. */
366
0
        if (dpif_attach_offload_providers(dpif) == 0) {
367
0
            DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
368
0
                struct netdev *netdev;
369
0
                int err;
370
371
0
                if (dpif_is_tap_port(dpif_port.type)) {
372
0
                    continue;
373
0
                }
374
375
0
                err = netdev_open(dpif_port.name, dpif_port.type, &netdev);
376
0
                if (err) {
377
0
                    VLOG_WARN("could not open netdev %s type %s: %s",
378
0
                              dpif_port.name, dpif_port.type,
379
0
                              ovs_strerror(err));
380
0
                    continue;
381
0
                }
382
383
0
                netdev_set_dpif_type(netdev, dpif_type_str);
384
0
                dpif_offload_port_add(dpif, netdev, dpif_port.port_no);
385
0
                netdev_close(netdev);
386
0
            }
387
0
        }
388
0
    } else {
389
0
        dp_class_unref(registered_class);
390
0
    }
391
392
0
exit:
393
0
    *dpifp = error ? NULL : dpif;
394
0
    return error;
395
0
}
396
397
/* Tries to open an existing datapath named 'name' and type 'type'.  Will fail
398
 * if no datapath with 'name' and 'type' exists.  'type' may be either NULL or
399
 * the empty string to specify the default system type.  Returns 0 if
400
 * successful, otherwise a positive errno value.  On success stores a pointer
401
 * to the datapath in '*dpifp', otherwise a null pointer. */
402
int
403
dpif_open(const char *name, const char *type, struct dpif **dpifp)
404
0
{
405
0
    return do_open(name, type, false, dpifp);
406
0
}
407
408
/* Tries to create and open a new datapath with the given 'name' and 'type'.
409
 * 'type' may be either NULL or the empty string to specify the default system
410
 * type.  Will fail if a datapath with 'name' and 'type' already exists.
411
 * Returns 0 if successful, otherwise a positive errno value.  On success
412
 * stores a pointer to the datapath in '*dpifp', otherwise a null pointer. */
413
int
414
dpif_create(const char *name, const char *type, struct dpif **dpifp)
415
0
{
416
0
    return do_open(name, type, true, dpifp);
417
0
}
418
419
/* Tries to open a datapath with the given 'name' and 'type', creating it if it
420
 * does not exist.  'type' may be either NULL or the empty string to specify
421
 * the default system type.  Returns 0 if successful, otherwise a positive
422
 * errno value. On success stores a pointer to the datapath in '*dpifp',
423
 * otherwise a null pointer. */
424
int
425
dpif_create_and_open(const char *name, const char *type, struct dpif **dpifp)
426
0
{
427
0
    int error;
428
429
0
    error = dpif_create(name, type, dpifp);
430
0
    if (error == EEXIST || error == EBUSY) {
431
0
        error = dpif_open(name, type, dpifp);
432
0
        if (error) {
433
0
            VLOG_WARN("datapath %s already exists but cannot be opened: %s",
434
0
                      name, ovs_strerror(error));
435
0
        }
436
0
    } else if (error) {
437
0
        VLOG_WARN("failed to create datapath %s: %s",
438
0
                  name, ovs_strerror(error));
439
0
    }
440
0
    return error;
441
0
}
442
443
/* Closes and frees the connection to 'dpif'.  Does not destroy the datapath
444
 * itself; call dpif_delete() first, instead, if that is desirable. */
445
void
446
dpif_close(struct dpif *dpif)
447
0
{
448
0
    if (dpif) {
449
0
        struct registered_dpif_class *rc;
450
451
0
        rc = shash_find_data(&dpif_classes, dpif->dpif_class->type);
452
453
0
        dpif_detach_offload_providers(dpif);
454
0
        dpif_uninit(dpif, true);
455
0
        dp_class_unref(rc);
456
0
    }
457
0
}
458
459
/* Performs periodic work needed by 'dpif'. */
460
bool
461
dpif_run(struct dpif *dpif)
462
0
{
463
0
    if (dpif->dpif_class->run) {
464
0
        return dpif->dpif_class->run(dpif);
465
0
    }
466
0
    return false;
467
0
}
468
469
/* Arranges for poll_block() to wake up when dp_run() needs to be called for
470
 * 'dpif'. */
471
void
472
dpif_wait(struct dpif *dpif)
473
0
{
474
0
    if (dpif->dpif_class->wait) {
475
0
        dpif->dpif_class->wait(dpif);
476
0
    }
477
0
}
478
479
/* Returns the name of datapath 'dpif' prefixed with the type
480
 * (for use in log messages). */
481
const char *
482
dpif_name(const struct dpif *dpif)
483
0
{
484
0
    return dpif->full_name;
485
0
}
486
487
/* Returns the name of datapath 'dpif' without the type
488
 * (for use in device names). */
489
const char *
490
dpif_base_name(const struct dpif *dpif)
491
0
{
492
0
    return dpif->base_name;
493
0
}
494
495
/* Returns the type of datapath 'dpif'. */
496
const char *
497
dpif_type(const struct dpif *dpif)
498
0
{
499
0
    return dpif->dpif_class->type;
500
0
}
501
502
/* Checks if datapath 'dpif' requires cleanup. */
503
bool
504
dpif_cleanup_required(const struct dpif *dpif)
505
0
{
506
0
    return dpif->dpif_class->cleanup_required;
507
0
}
508
509
/* Returns the fully spelled out name for the given datapath 'type'.
510
 *
511
 * Normalized type string can be compared with strcmp().  Unnormalized type
512
 * string might be the same even if they have different spellings. */
513
const char *
514
dpif_normalize_type(const char *type)
515
0
{
516
0
    return type && type[0] ? type : "system";
517
0
}
518
519
/* Destroys the datapath that 'dpif' is connected to, first removing all of its
520
 * ports.  After calling this function, it does not make sense to pass 'dpif'
521
 * to any functions other than dpif_name() or dpif_close(). */
522
int
523
dpif_delete(struct dpif *dpif)
524
0
{
525
0
    int error;
526
527
0
    COVERAGE_INC(dpif_destroy);
528
529
0
    error = dpif->dpif_class->destroy(dpif);
530
0
    log_operation(dpif, "delete", error);
531
0
    return error;
532
0
}
533
534
/* Retrieves statistics for 'dpif' into 'stats'.  Returns 0 if successful,
535
 * otherwise a positive errno value. */
536
int
537
dpif_get_dp_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
538
0
{
539
0
    int error = dpif->dpif_class->get_stats(dpif, stats);
540
0
    if (error) {
541
0
        memset(stats, 0, sizeof *stats);
542
0
    }
543
0
    log_operation(dpif, "get_stats", error);
544
0
    return error;
545
0
}
546
547
int
548
dpif_set_features(struct dpif *dpif, uint32_t new_features)
549
0
{
550
0
    int error = dpif->dpif_class->set_features
551
0
                    ? dpif->dpif_class->set_features(dpif, new_features)
552
0
                    : EOPNOTSUPP;
553
554
0
    log_operation(dpif, "set_features", error);
555
0
    return error;
556
0
}
557
558
uint32_t
559
dpif_get_features(struct dpif *dpif)
560
0
{
561
0
    return dpif->dpif_class->get_features
562
0
               ? dpif->dpif_class->get_features(dpif)
563
0
               : 0;
564
0
}
565
566
const char *
567
dpif_port_open_type(const char *datapath_type, const char *port_type)
568
0
{
569
0
    struct registered_dpif_class *rc;
570
571
0
    datapath_type = dpif_normalize_type(datapath_type);
572
573
0
    ovs_mutex_lock(&dpif_mutex);
574
0
    rc = shash_find_data(&dpif_classes, datapath_type);
575
0
    if (rc && rc->dpif_class->port_open_type) {
576
0
        port_type = rc->dpif_class->port_open_type(rc->dpif_class, port_type);
577
0
    }
578
0
    ovs_mutex_unlock(&dpif_mutex);
579
580
0
    return port_type;
581
0
}
582
583
/* Attempts to add 'netdev' as a port on 'dpif'.  If 'port_nop' is
584
 * non-null and its value is not ODPP_NONE, then attempts to use the
585
 * value as the port number.
586
 *
587
 * If successful, returns 0 and sets '*port_nop' to the new port's port
588
 * number (if 'port_nop' is non-null).  On failure, returns a positive
589
 * errno value and sets '*port_nop' to ODPP_NONE (if 'port_nop' is
590
 * non-null). */
591
int
592
dpif_port_add(struct dpif *dpif, struct netdev *netdev, odp_port_t *port_nop)
593
0
{
594
0
    const char *netdev_name = netdev_get_name(netdev);
595
0
    odp_port_t port_no = ODPP_NONE;
596
0
    int error;
597
598
0
    COVERAGE_INC(dpif_port_add);
599
600
0
    if (port_nop) {
601
0
        port_no = *port_nop;
602
0
    }
603
604
0
    error = dpif->dpif_class->port_add(dpif, netdev, &port_no);
605
0
    if (!error) {
606
0
        VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu32,
607
0
                    dpif_name(dpif), netdev_name, port_no);
608
609
0
        if (!dpif_is_tap_port(netdev_get_type(netdev))) {
610
0
            const char *dpif_type_str = dpif_normalize_type(dpif_type(dpif));
611
612
0
            netdev_set_dpif_type(netdev, dpif_type_str);
613
0
            dpif_offload_port_add(dpif, netdev, port_no);
614
0
        }
615
0
    } else {
616
0
        VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
617
0
                     dpif_name(dpif), netdev_name, ovs_strerror(error));
618
0
        port_no = ODPP_NONE;
619
0
    }
620
0
    if (port_nop) {
621
0
        *port_nop = port_no;
622
0
    }
623
0
    return error;
624
0
}
625
626
/* Attempts to remove 'dpif''s port number 'port_no'.  Returns 0 if successful,
627
 * otherwise a positive errno value. */
628
int
629
dpif_port_del(struct dpif *dpif, odp_port_t port_no, bool local_delete)
630
0
{
631
0
    int error = 0;
632
633
0
    COVERAGE_INC(dpif_port_del);
634
635
0
    if (!local_delete) {
636
0
        error = dpif->dpif_class->port_del(dpif, port_no);
637
0
        if (!error) {
638
0
            VLOG_DBG_RL(&dpmsg_rl, "%s: port_del(%"PRIu32")",
639
0
                        dpif_name(dpif), port_no);
640
0
        } else {
641
0
            log_operation(dpif, "port_del", error);
642
0
        }
643
0
    }
644
645
0
    dpif_offload_port_del(dpif, port_no);
646
0
    return error;
647
0
}
648
649
/* Makes a deep copy of 'src' into 'dst'. */
650
void
651
dpif_port_clone(struct dpif_port *dst, const struct dpif_port *src)
652
0
{
653
0
    dst->name = xstrdup(src->name);
654
0
    dst->type = xstrdup(src->type);
655
0
    dst->port_no = src->port_no;
656
0
}
657
658
/* Frees memory allocated to members of 'dpif_port'.
659
 *
660
 * Do not call this function on a dpif_port obtained from
661
 * dpif_port_dump_next(): that function retains ownership of the data in the
662
 * dpif_port. */
663
void
664
dpif_port_destroy(struct dpif_port *dpif_port)
665
0
{
666
0
    free(dpif_port->name);
667
0
    free(dpif_port->type);
668
0
}
669
670
/* Checks if port named 'devname' exists in 'dpif'.  If so, returns
671
 * true; otherwise, returns false. */
672
bool
673
dpif_port_exists(const struct dpif *dpif, const char *devname)
674
0
{
675
0
    int error = dpif->dpif_class->port_query_by_name(dpif, devname, NULL);
676
0
    if (error != 0 && error != ENODEV) {
677
0
        VLOG_WARN_RL(&error_rl, "%s: failed to query port %s: %s",
678
0
                     dpif_name(dpif), devname, ovs_strerror(error));
679
0
    }
680
681
0
    return !error;
682
0
}
683
684
/* Refreshes configuration of 'dpif's port. */
685
int
686
dpif_port_set_config(struct dpif *dpif, odp_port_t port_no,
687
                     const struct smap *cfg)
688
0
{
689
0
    int error = 0;
690
691
0
    if (dpif->dpif_class->port_set_config) {
692
0
        error = dpif->dpif_class->port_set_config(dpif, port_no, cfg);
693
0
        if (error) {
694
0
            log_operation(dpif, "port_set_config", error);
695
0
        }
696
0
        dpif_offload_port_set_config(dpif, port_no, cfg);
697
0
    }
698
699
0
    return error;
700
0
}
701
702
/* Looks up port number 'port_no' in 'dpif'.  On success, returns 0 and
703
 * initializes '*port' appropriately; on failure, returns a positive errno
704
 * value.
705
 *
706
 * Retuns ENODEV if the port doesn't exist.  Will not log a warning in this
707
 * case unless 'warn_if_not_found' is true.
708
 *
709
 * The caller owns the data in 'port' and must free it with
710
 * dpif_port_destroy() when it is no longer needed. */
711
int
712
dpif_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
713
                          struct dpif_port *port, bool warn_if_not_found)
714
0
{
715
0
    int error = dpif->dpif_class->port_query_by_number(dpif, port_no, port);
716
0
    if (!error) {
717
0
        VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu32" is device %s",
718
0
                    dpif_name(dpif), port_no, port->name);
719
0
    } else {
720
0
        memset(port, 0, sizeof *port);
721
0
        if (error == ENODEV && !warn_if_not_found) {
722
0
            VLOG_DBG_RL(&dpmsg_rl, "%s: failed to query port %"PRIu32": %s",
723
0
                        dpif_name(dpif), port_no, ovs_strerror(error));
724
0
        } else {
725
0
            VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu32": %s",
726
0
                         dpif_name(dpif), port_no, ovs_strerror(error));
727
0
        }
728
0
    }
729
0
    return error;
730
0
}
731
732
/* Looks up port named 'devname' in 'dpif'.  On success, returns 0 and
733
 * initializes '*port' appropriately; on failure, returns a positive errno
734
 * value.
735
 *
736
 * Retuns ENODEV if the port doesn't exist.
737
 *
738
 * The caller owns the data in 'port' and must free it with
739
 * dpif_port_destroy() when it is no longer needed. */
740
int
741
dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
742
                        struct dpif_port *port)
743
0
{
744
0
    int error = dpif->dpif_class->port_query_by_name(dpif, devname, port);
745
0
    if (!error) {
746
0
        VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu32,
747
0
                    dpif_name(dpif), devname, port->port_no);
748
0
    } else {
749
0
        memset(port, 0, sizeof *port);
750
751
        /* For ENODEV we use DBG level because the caller is probably
752
         * interested in whether 'dpif' actually has a port 'devname', so that
753
         * it's not an issue worth logging if it doesn't.  Other errors are
754
         * uncommon and more likely to indicate a real problem. */
755
0
        VLOG_RL(&error_rl, error == ENODEV ? VLL_DBG : VLL_WARN,
756
0
                "%s: failed to query port %s: %s",
757
0
                dpif_name(dpif), devname, ovs_strerror(error));
758
0
    }
759
0
    return error;
760
0
}
761
762
/* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE
763
 * actions as the OVS_USERSPACE_ATTR_PID attribute's value, for use in
764
 * flows whose packets arrived on port 'port_no'.
765
 *
766
 * A 'port_no' of ODPP_NONE is a special case: it returns a reserved PID, not
767
 * allocated to any port, that the client may use for special purposes.
768
 *
769
 * The return value is only meaningful when DPIF_UC_ACTION has been enabled in
770
 * the 'dpif''s listen mask.  It is allowed to change when DPIF_UC_ACTION is
771
 * disabled and then re-enabled, so a client that does that must be prepared to
772
 * update all of the flows that it installed that contain
773
 * OVS_ACTION_ATTR_USERSPACE actions. */
774
uint32_t
775
dpif_port_get_pid(const struct dpif *dpif, odp_port_t port_no)
776
0
{
777
0
    return (dpif->dpif_class->port_get_pid
778
0
            ? (dpif->dpif_class->port_get_pid)(dpif, port_no)
779
0
            : 0);
780
0
}
781
782
/* Looks up port number 'port_no' in 'dpif'.  On success, returns 0 and copies
783
 * the port's name into the 'name_size' bytes in 'name', ensuring that the
784
 * result is null-terminated.  On failure, returns a positive errno value and
785
 * makes 'name' the empty string. */
786
int
787
dpif_port_get_name(struct dpif *dpif, odp_port_t port_no,
788
                   char *name, size_t name_size)
789
0
{
790
0
    struct dpif_port port;
791
0
    int error;
792
793
0
    ovs_assert(name_size > 0);
794
795
0
    error = dpif_port_query_by_number(dpif, port_no, &port, true);
796
0
    if (!error) {
797
0
        ovs_strlcpy(name, port.name, name_size);
798
0
        dpif_port_destroy(&port);
799
0
    } else {
800
0
        *name = '\0';
801
0
    }
802
0
    return error;
803
0
}
804
805
/* Initializes 'dump' to begin dumping the ports in a dpif.
806
 *
807
 * This function provides no status indication.  An error status for the entire
808
 * dump operation is provided when it is completed by calling
809
 * dpif_port_dump_done().
810
 */
811
void
812
dpif_port_dump_start(struct dpif_port_dump *dump, const struct dpif *dpif)
813
0
{
814
0
    dump->dpif = dpif;
815
0
    dump->error = dpif->dpif_class->port_dump_start(dpif, &dump->state);
816
0
    log_operation(dpif, "port_dump_start", dump->error);
817
0
}
818
819
/* Attempts to retrieve another port from 'dump', which must have been
820
 * initialized with dpif_port_dump_start().  On success, stores a new dpif_port
821
 * into 'port' and returns true.  On failure, returns false.
822
 *
823
 * Failure might indicate an actual error or merely that the last port has been
824
 * dumped.  An error status for the entire dump operation is provided when it
825
 * is completed by calling dpif_port_dump_done().
826
 *
827
 * The dpif owns the data stored in 'port'.  It will remain valid until at
828
 * least the next time 'dump' is passed to dpif_port_dump_next() or
829
 * dpif_port_dump_done(). */
830
bool
831
dpif_port_dump_next(struct dpif_port_dump *dump, struct dpif_port *port)
832
0
{
833
0
    const struct dpif *dpif = dump->dpif;
834
835
0
    if (dump->error) {
836
0
        return false;
837
0
    }
838
839
0
    dump->error = dpif->dpif_class->port_dump_next(dpif, dump->state, port);
840
0
    if (dump->error == EOF) {
841
0
        VLOG_DBG_RL(&dpmsg_rl, "%s: dumped all ports", dpif_name(dpif));
842
0
    } else {
843
0
        log_operation(dpif, "port_dump_next", dump->error);
844
0
    }
845
846
0
    if (dump->error) {
847
0
        dpif->dpif_class->port_dump_done(dpif, dump->state);
848
0
        return false;
849
0
    }
850
0
    return true;
851
0
}
852
853
/* Completes port table dump operation 'dump', which must have been initialized
854
 * with dpif_port_dump_start().  Returns 0 if the dump operation was
855
 * error-free, otherwise a positive errno value describing the problem. */
856
int
857
dpif_port_dump_done(struct dpif_port_dump *dump)
858
0
{
859
0
    const struct dpif *dpif = dump->dpif;
860
0
    if (!dump->error) {
861
0
        dump->error = dpif->dpif_class->port_dump_done(dpif, dump->state);
862
0
        log_operation(dpif, "port_dump_done", dump->error);
863
0
    }
864
0
    return dump->error == EOF ? 0 : dump->error;
865
0
}
866
867
/* Polls for changes in the set of ports in 'dpif'.  If the set of ports in
868
 * 'dpif' has changed, this function does one of the following:
869
 *
870
 * - Stores the name of the device that was added to or deleted from 'dpif' in
871
 *   '*devnamep' and returns 0.  The caller is responsible for freeing
872
 *   '*devnamep' (with free()) when it no longer needs it.
873
 *
874
 * - Returns ENOBUFS and sets '*devnamep' to NULL.
875
 *
876
 * This function may also return 'false positives', where it returns 0 and
877
 * '*devnamep' names a device that was not actually added or deleted or it
878
 * returns ENOBUFS without any change.
879
 *
880
 * Returns EAGAIN if the set of ports in 'dpif' has not changed.  May also
881
 * return other positive errno values to indicate that something has gone
882
 * wrong. */
883
int
884
dpif_port_poll(const struct dpif *dpif, char **devnamep)
885
0
{
886
0
    int error = dpif->dpif_class->port_poll(dpif, devnamep);
887
0
    if (error) {
888
0
        *devnamep = NULL;
889
0
    }
890
0
    return error;
891
0
}
892
893
/* Arranges for the poll loop to wake up when port_poll(dpif) will return a
894
 * value other than EAGAIN. */
895
void
896
dpif_port_poll_wait(const struct dpif *dpif)
897
0
{
898
0
    dpif->dpif_class->port_poll_wait(dpif);
899
0
}
900
901
/* Extracts the flow stats for a packet.  The 'flow' and 'packet'
902
 * arguments must have been initialized through a call to flow_extract().
903
 * 'used' is stored into stats->used. */
904
void
905
dpif_flow_stats_extract(const struct flow *flow, const struct dp_packet *packet,
906
                        long long int used, struct dpif_flow_stats *stats)
907
0
{
908
0
    stats->tcp_flags = ntohs(flow->tcp_flags);
909
0
    stats->n_bytes = dp_packet_size(packet);
910
0
    stats->n_packets = 1;
911
0
    stats->used = used;
912
0
}
913
914
/* Appends a human-readable representation of 'stats' to 's'. */
915
void
916
dpif_flow_stats_format(const struct dpif_flow_stats *stats, struct ds *s)
917
0
{
918
0
    ds_put_format(s, "packets:%"PRIu64", bytes:%"PRIu64", used:",
919
0
                  stats->n_packets, stats->n_bytes);
920
0
    if (stats->used) {
921
0
        ds_put_format(s, "%.3fs", (time_msec() - stats->used) / 1000.0);
922
0
    } else {
923
0
        ds_put_format(s, "never");
924
0
    }
925
0
    if (stats->tcp_flags) {
926
0
        ds_put_cstr(s, ", flags:");
927
0
        packet_format_tcp_flags(s, stats->tcp_flags);
928
0
    }
929
0
}
930
931
/* Deletes all flows from 'dpif'.  Returns 0 if successful, otherwise a
932
 * positive errno value.  */
933
int
934
dpif_flow_flush(struct dpif *dpif)
935
0
{
936
0
    int error;
937
938
0
    COVERAGE_INC(dpif_flow_flush);
939
940
0
    error = dpif->dpif_class->flow_flush(dpif);
941
0
    log_operation(dpif, "flow_flush", error);
942
0
    dpif_offload_flow_flush(dpif);
943
0
    return error;
944
0
}
945
946
/* Attempts to install 'key' into the datapath, fetches it, then deletes it.
947
 * Returns true if the datapath supported installing 'flow', false otherwise.
948
 */
949
bool
950
dpif_probe_feature(struct dpif *dpif, const char *name,
951
                   const struct ofpbuf *key, const struct ofpbuf *actions,
952
                   const ovs_u128 *ufid)
953
0
{
954
0
    struct dpif_flow flow;
955
0
    struct ofpbuf reply;
956
0
    uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
957
0
    bool enable_feature = false;
958
0
    int error;
959
0
    const struct nlattr *nl_actions = actions ? actions->data : NULL;
960
0
    const size_t nl_actions_size = actions ? actions->size : 0;
961
962
    /* Use DPIF_FP_MODIFY to cover the case where ovs-vswitchd is killed (and
963
     * restarted) at just the right time such that feature probes from the
964
     * previous run are still present in the datapath. */
965
0
    error = dpif_flow_put(dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY | DPIF_FP_PROBE,
966
0
                          key->data, key->size, NULL, 0,
967
0
                          nl_actions, nl_actions_size,
968
0
                          ufid, NON_PMD_CORE_ID, NULL);
969
0
    if (error) {
970
0
        if (error != EINVAL && error != EOVERFLOW) {
971
0
            VLOG_WARN("%s: %s flow probe failed (%s)",
972
0
                      dpif_name(dpif), name, ovs_strerror(error));
973
0
        }
974
0
        return false;
975
0
    }
976
977
0
    ofpbuf_use_stack(&reply, &stub, sizeof stub);
978
0
    error = dpif_flow_get(dpif, key->data, key->size, ufid,
979
0
                          NON_PMD_CORE_ID, &reply, &flow);
980
0
    if (!error
981
0
        && (!ufid || (flow.ufid_present
982
0
                      && ovs_u128_equals(*ufid, flow.ufid)))) {
983
0
        enable_feature = true;
984
0
    }
985
986
0
    error = dpif_flow_del(dpif, key->data, key->size, ufid,
987
0
                          NON_PMD_CORE_ID, NULL);
988
0
    if (error) {
989
0
        VLOG_WARN("%s: failed to delete %s feature probe flow",
990
0
                  dpif_name(dpif), name);
991
0
    }
992
993
0
    return enable_feature;
994
0
}
995
996
/* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_GET. */
997
int
998
dpif_flow_get(struct dpif *dpif,
999
              const struct nlattr *key, size_t key_len, const ovs_u128 *ufid,
1000
              const unsigned pmd_id, struct ofpbuf *buf, struct dpif_flow *flow)
1001
0
{
1002
0
    struct dpif_op *opp;
1003
0
    struct dpif_op op;
1004
1005
0
    op.type = DPIF_OP_FLOW_GET;
1006
0
    op.flow_get.key = key;
1007
0
    op.flow_get.key_len = key_len;
1008
0
    op.flow_get.ufid = ufid;
1009
0
    op.flow_get.pmd_id = pmd_id;
1010
0
    op.flow_get.buffer = buf;
1011
1012
0
    memset(flow, 0, sizeof *flow);
1013
0
    op.flow_get.flow = flow;
1014
0
    op.flow_get.flow->key = key;
1015
0
    op.flow_get.flow->key_len = key_len;
1016
1017
0
    opp = &op;
1018
0
    dpif_operate(dpif, &opp, 1, DPIF_OFFLOAD_AUTO);
1019
1020
0
    return op.error;
1021
0
}
1022
1023
/* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_PUT. */
1024
int
1025
dpif_flow_put(struct dpif *dpif, enum dpif_flow_put_flags flags,
1026
              const struct nlattr *key, size_t key_len,
1027
              const struct nlattr *mask, size_t mask_len,
1028
              const struct nlattr *actions, size_t actions_len,
1029
              const ovs_u128 *ufid, const unsigned pmd_id,
1030
              struct dpif_flow_stats *stats)
1031
0
{
1032
0
    struct dpif_op *opp;
1033
0
    struct dpif_op op;
1034
1035
0
    op.type = DPIF_OP_FLOW_PUT;
1036
0
    op.flow_put.flags = flags;
1037
0
    op.flow_put.key = key;
1038
0
    op.flow_put.key_len = key_len;
1039
0
    op.flow_put.mask = mask;
1040
0
    op.flow_put.mask_len = mask_len;
1041
0
    op.flow_put.actions = actions;
1042
0
    op.flow_put.actions_len = actions_len;
1043
0
    op.flow_put.ufid = ufid;
1044
0
    op.flow_put.pmd_id = pmd_id;
1045
0
    op.flow_put.stats = stats;
1046
1047
0
    opp = &op;
1048
0
    dpif_operate(dpif, &opp, 1, DPIF_OFFLOAD_AUTO);
1049
1050
0
    return op.error;
1051
0
}
1052
1053
/* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_DEL. */
1054
int
1055
dpif_flow_del(struct dpif *dpif,
1056
              const struct nlattr *key, size_t key_len, const ovs_u128 *ufid,
1057
              const unsigned pmd_id, struct dpif_flow_stats *stats)
1058
0
{
1059
0
    struct dpif_op *opp;
1060
0
    struct dpif_op op;
1061
1062
0
    op.type = DPIF_OP_FLOW_DEL;
1063
0
    op.flow_del.key = key;
1064
0
    op.flow_del.key_len = key_len;
1065
0
    op.flow_del.ufid = ufid;
1066
0
    op.flow_del.pmd_id = pmd_id;
1067
0
    op.flow_del.stats = stats;
1068
0
    op.flow_del.terse = false;
1069
1070
0
    opp = &op;
1071
0
    dpif_operate(dpif, &opp, 1, DPIF_OFFLOAD_AUTO);
1072
1073
0
    return op.error;
1074
0
}
1075
1076
/* Creates and returns a new 'struct dpif_flow_dump' for iterating through the
1077
 * flows in 'dpif'. If 'terse' is true, then only UFID and statistics will
1078
 * be returned in the dump. Otherwise, all fields will be returned.
1079
 *
1080
 * This function always successfully returns a dpif_flow_dump.  Error
1081
 * reporting is deferred to dpif_flow_dump_destroy(). */
1082
struct dpif_flow_dump *
1083
dpif_flow_dump_create(const struct dpif *dpif, bool terse,
1084
                      struct dpif_flow_dump_types *types)
1085
0
{
1086
0
    return dpif->dpif_class->flow_dump_create(dpif, terse, types);
1087
0
}
1088
1089
/* Destroys 'dump', which must have been created with dpif_flow_dump_create().
1090
 * All dpif_flow_dump_thread structures previously created for 'dump' must
1091
 * previously have been destroyed.
1092
 *
1093
 * Returns 0 if the dump operation was error-free, otherwise a positive errno
1094
 * value describing the problem. */
1095
int
1096
dpif_flow_dump_destroy(struct dpif_flow_dump *dump)
1097
0
{
1098
0
    const struct dpif *dpif = dump->dpif;
1099
0
    int error;
1100
0
    int offload_error;
1101
1102
0
    offload_error = dpif_offload_flow_dump_destroy(dump);
1103
0
    error = dpif->dpif_class->flow_dump_destroy(dump);
1104
1105
0
    if (!error || error == EOF) {
1106
0
        error = offload_error;
1107
0
    }
1108
0
    log_operation(dpif, "flow_dump_destroy", error);
1109
0
    return error == EOF ? 0 : error;
1110
0
}
1111
1112
/* Returns new thread-local state for use with dpif_flow_dump_next(). */
1113
struct dpif_flow_dump_thread *
1114
dpif_flow_dump_thread_create(struct dpif_flow_dump *dump)
1115
0
{
1116
0
    return dump->dpif->dpif_class->flow_dump_thread_create(dump);
1117
0
}
1118
1119
/* Releases 'thread'. */
1120
void
1121
dpif_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread)
1122
0
{
1123
0
    dpif_offload_flow_dump_thread_destroy(thread);
1124
0
    thread->dump->dpif->dpif_class->flow_dump_thread_destroy(thread);
1125
0
}
1126
1127
/* Attempts to retrieve up to 'max_flows' more flows from 'thread'.  Returns 0
1128
 * if and only if no flows remained to be retrieved, otherwise a positive
1129
 * number reflecting the number of elements in 'flows[]' that were updated.
1130
 * The number of flows returned might be less than 'max_flows' because
1131
 * fewer than 'max_flows' remained, because this particular datapath does not
1132
 * benefit from batching, or because an error occurred partway through
1133
 * retrieval.  Thus, the caller should continue calling until a 0 return value,
1134
 * even if intermediate return values are less than 'max_flows'.
1135
 *
1136
 * No error status is immediately provided.  An error status for the entire
1137
 * dump operation is provided when it is completed by calling
1138
 * dpif_flow_dump_destroy().
1139
 *
1140
 * All of the data stored into 'flows' is owned by the datapath, not by the
1141
 * caller, and the caller must not modify or free it.  The datapath guarantees
1142
 * that it remains accessible and unchanged until the first of:
1143
 *  - The next call to dpif_flow_dump_next() for 'thread', or
1144
 *  - The next rcu quiescent period. */
1145
int
1146
dpif_flow_dump_next(struct dpif_flow_dump_thread *thread,
1147
                    struct dpif_flow *flows, int max_flows)
1148
0
{
1149
0
    struct dpif *dpif = thread->dump->dpif;
1150
0
    int n = 0;
1151
1152
0
    ovs_assert(max_flows > 0);
1153
1154
0
    if (!thread->offload_dump_done) {
1155
0
        n = dpif_offload_flow_dump_next(thread, flows, max_flows);
1156
0
    }
1157
0
    if (n == 0) {
1158
0
        n = dpif->dpif_class->flow_dump_next(thread, flows, max_flows);
1159
0
    }
1160
1161
0
    if (n > 0) {
1162
0
        struct dpif_flow *f;
1163
1164
0
        for (f = flows; f < &flows[n]
1165
0
             && should_log_flow_message(&this_module, 0); f++) {
1166
0
            log_flow_message(dpif, 0, &this_module, "flow_dump",
1167
0
                             f->key, f->key_len, f->mask, f->mask_len,
1168
0
                             &f->ufid, &f->stats, f->actions, f->actions_len);
1169
0
        }
1170
0
    } else {
1171
0
        VLOG_DBG_RL(&dpmsg_rl, "%s: dumped all flows", dpif_name(dpif));
1172
0
    }
1173
0
    return n;
1174
0
}
1175
1176
struct dpif_execute_helper_aux {
1177
    struct dpif *dpif;
1178
    int error;
1179
    struct ofpbuf meter_actions;
1180
    struct dpif_execute orig_execute;
1181
};
1182
1183
/* This is called for actions that need the context of the datapath to be
1184
 * meaningful. */
1185
static void
1186
dpif_execute_helper_cb(void *aux_, struct dp_packet_batch *packets_,
1187
                       const struct nlattr *action, bool should_steal)
1188
0
{
1189
0
    struct dpif_execute_helper_aux *aux = aux_;
1190
0
    int type = nl_attr_type(action);
1191
0
    struct dp_packet *packet = packets_->packets[0];
1192
1193
0
    ovs_assert(dp_packet_batch_size(packets_) == 1);
1194
1195
0
    switch ((enum ovs_action_attr)type) {
1196
0
    case OVS_ACTION_ATTR_METER:
1197
        /* XXX: This code collects meter actions since the last action
1198
         * execution via the datapath to be executed right before the
1199
         * current action that needs to be executed by the datapath.
1200
         * This is only an approximation, but better than nothing.
1201
         * Fundamentally, we should have a mechanism by which the
1202
         * datapath could return the result of the meter action so that
1203
         * we could execute them at the right order. */
1204
0
        ofpbuf_put(&aux->meter_actions, action, NLA_ALIGN(action->nla_len));
1205
0
        break;
1206
1207
0
    case OVS_ACTION_ATTR_CT:
1208
0
    case OVS_ACTION_ATTR_OUTPUT:
1209
0
    case OVS_ACTION_ATTR_LB_OUTPUT:
1210
0
    case OVS_ACTION_ATTR_TUNNEL_PUSH:
1211
0
    case OVS_ACTION_ATTR_TUNNEL_POP:
1212
0
    case OVS_ACTION_ATTR_USERSPACE:
1213
0
    case OVS_ACTION_ATTR_PSAMPLE:
1214
0
    case OVS_ACTION_ATTR_SAMPLE:
1215
0
    case OVS_ACTION_ATTR_RECIRC: {
1216
0
        struct dpif_execute execute = aux->orig_execute;
1217
0
        struct pkt_metadata *md = &packet->md;
1218
1219
0
        if (flow_tnl_dst_is_set(&md->tunnel) || aux->meter_actions.size) {
1220
0
            struct ofpbuf *execute_actions = &aux->meter_actions;
1221
1222
            /* The Linux kernel datapath throws away the tunnel information
1223
             * that we supply as metadata.  We have to use a "set" action to
1224
             * supply it. */
1225
0
            if (flow_tnl_dst_is_set(&md->tunnel)) {
1226
0
                odp_put_tunnel_action(&md->tunnel, execute_actions, NULL);
1227
0
            }
1228
0
            ofpbuf_put(execute_actions, action, NLA_ALIGN(action->nla_len));
1229
1230
0
            execute.actions = execute_actions->data;
1231
0
            execute.actions_len = execute_actions->size;
1232
0
        } else {
1233
0
            execute.actions = action;
1234
0
            execute.actions_len = NLA_ALIGN(action->nla_len);
1235
0
        }
1236
1237
0
        struct dp_packet *clone = NULL;
1238
0
        uint32_t cutlen = dp_packet_get_cutlen(packet);
1239
0
        if (cutlen && (type == OVS_ACTION_ATTR_OUTPUT
1240
0
                        || type == OVS_ACTION_ATTR_LB_OUTPUT
1241
0
                        || type == OVS_ACTION_ATTR_TUNNEL_PUSH
1242
0
                        || type == OVS_ACTION_ATTR_TUNNEL_POP
1243
0
                        || type == OVS_ACTION_ATTR_USERSPACE)) {
1244
0
            dp_packet_reset_cutlen(packet);
1245
0
            if (!should_steal) {
1246
0
                packet = clone = dp_packet_clone(packet);
1247
0
            }
1248
0
            dp_packet_set_size(packet, dp_packet_size(packet) - cutlen);
1249
0
        }
1250
1251
0
        execute.packet = packet;
1252
0
        execute.needs_help = false;
1253
1254
0
        aux->error = dpif_execute(aux->dpif, &execute);
1255
0
        log_execute_message(aux->dpif, &this_module, &execute,
1256
0
                            true, aux->error);
1257
1258
0
        dp_packet_delete(clone);
1259
1260
        /* Clear the 'aux->meter_actions' ofpbuf as it could have been
1261
         * used for sending the additional meter and/or tunnel actions. */
1262
0
        ofpbuf_clear(&aux->meter_actions);
1263
0
        break;
1264
0
    }
1265
1266
0
    case OVS_ACTION_ATTR_HASH:
1267
0
    case OVS_ACTION_ATTR_PUSH_VLAN:
1268
0
    case OVS_ACTION_ATTR_POP_VLAN:
1269
0
    case OVS_ACTION_ATTR_PUSH_MPLS:
1270
0
    case OVS_ACTION_ATTR_POP_MPLS:
1271
0
    case OVS_ACTION_ATTR_SET:
1272
0
    case OVS_ACTION_ATTR_SET_MASKED:
1273
0
    case OVS_ACTION_ATTR_TRUNC:
1274
0
    case OVS_ACTION_ATTR_PUSH_ETH:
1275
0
    case OVS_ACTION_ATTR_POP_ETH:
1276
0
    case OVS_ACTION_ATTR_CLONE:
1277
0
    case OVS_ACTION_ATTR_PUSH_NSH:
1278
0
    case OVS_ACTION_ATTR_POP_NSH:
1279
0
    case OVS_ACTION_ATTR_CT_CLEAR:
1280
0
    case OVS_ACTION_ATTR_UNSPEC:
1281
0
    case OVS_ACTION_ATTR_CHECK_PKT_LEN:
1282
0
    case OVS_ACTION_ATTR_DROP:
1283
0
    case OVS_ACTION_ATTR_ADD_MPLS:
1284
0
    case OVS_ACTION_ATTR_DEC_TTL:
1285
0
    case __OVS_ACTION_ATTR_MAX:
1286
0
        OVS_NOT_REACHED();
1287
0
    }
1288
0
    dp_packet_delete_batch(packets_, should_steal);
1289
0
}
1290
1291
/* Executes 'execute' by performing most of the actions in userspace and
1292
 * passing the fully constructed packets to 'dpif' for output and userspace
1293
 * actions.
1294
 *
1295
 * This helps with actions that a given 'dpif' doesn't implement directly. */
1296
static int
1297
dpif_execute_with_help(struct dpif *dpif, struct dpif_execute *execute)
1298
0
{
1299
0
    struct dpif_execute_helper_aux aux = {
1300
0
        .dpif = dpif,
1301
0
        .error = 0,
1302
0
        .orig_execute = *execute,
1303
0
    };
1304
0
    struct dp_packet_batch pb;
1305
1306
0
    COVERAGE_INC(dpif_execute_with_help);
1307
1308
0
    ofpbuf_init(&aux.meter_actions, 0);
1309
0
    dp_packet_batch_init_packet(&pb, execute->packet);
1310
0
    odp_execute_actions(&aux, &pb, false, execute->actions,
1311
0
                        execute->actions_len, dpif_execute_helper_cb);
1312
0
    ofpbuf_uninit(&aux.meter_actions);
1313
0
    return aux.error;
1314
0
}
1315
1316
/* Returns true if the datapath needs help executing 'execute'. */
1317
static bool
1318
dpif_execute_needs_help(const struct dpif_execute *execute)
1319
0
{
1320
0
    return execute->needs_help || nl_attr_oversized(execute->actions_len);
1321
0
}
1322
1323
/* A dpif_operate() wrapper for performing a single DPIF_OP_EXECUTE. */
1324
int
1325
dpif_execute(struct dpif *dpif, struct dpif_execute *execute)
1326
0
{
1327
0
    if (execute->actions_len) {
1328
0
        struct dpif_op *opp;
1329
0
        struct dpif_op op;
1330
1331
0
        op.type = DPIF_OP_EXECUTE;
1332
0
        op.execute = *execute;
1333
1334
0
        opp = &op;
1335
0
        dpif_operate(dpif, &opp, 1, DPIF_OFFLOAD_AUTO);
1336
1337
0
        return op.error;
1338
0
    } else {
1339
0
        return 0;
1340
0
    }
1341
0
}
1342
1343
/* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order in
1344
 * which they are specified.  Places each operation's results in the "output"
1345
 * members documented in comments, and 0 in the 'error' member on success or a
1346
 * positive errno on failure.
1347
 */
1348
void
1349
dpif_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops,
1350
             enum dpif_offload_type offload_type)
1351
0
{
1352
0
    if (offload_type == DPIF_OFFLOAD_ALWAYS && !dpif_offload_enabled()) {
1353
0
        size_t i;
1354
0
        for (i = 0; i < n_ops; i++) {
1355
0
            struct dpif_op *op = ops[i];
1356
0
            op->error = EINVAL;
1357
0
        }
1358
0
        return;
1359
0
    }
1360
1361
0
    while (n_ops > 0) {
1362
0
        size_t chunk;
1363
1364
        /* Count 'chunk', the number of ops that can be executed without
1365
         * needing any help.  Ops that need help should be rare, so we
1366
         * expect this to ordinarily be 'n_ops', that is, all the ops. */
1367
0
        for (chunk = 0; chunk < n_ops; chunk++) {
1368
0
            struct dpif_op *op = ops[chunk];
1369
1370
0
            if (op->type == DPIF_OP_EXECUTE
1371
0
                && dpif_execute_needs_help(&op->execute)) {
1372
0
                break;
1373
0
            }
1374
0
        }
1375
1376
0
        if (chunk) {
1377
            /* Execute a chunk full of ops that the dpif provider can
1378
             * handle itself, without help. */
1379
0
            size_t i;
1380
1381
            /* See if we can handle some flow add/del/get through the
1382
             * dpif-offload provider. */
1383
0
            if (offload_type != DPIF_OFFLOAD_NEVER && dpif_offload_enabled()) {
1384
0
                struct dpif_op **ops_copy;
1385
0
                size_t ops_left;
1386
1387
0
                ops_copy = xmemdup(ops, sizeof(*ops_copy) * chunk);
1388
1389
0
                ops_left = dpif_offload_operate(dpif, ops_copy, chunk,
1390
0
                                                offload_type);
1391
0
                if (ops_left) {
1392
0
                    dpif->dpif_class->operate(dpif, ops_copy, ops_left);
1393
0
                }
1394
0
                free(ops_copy);
1395
0
            } else {
1396
0
                dpif->dpif_class->operate(dpif, ops, chunk);
1397
0
            }
1398
1399
0
            for (i = 0; i < chunk; i++) {
1400
0
                struct dpif_op *op = ops[i];
1401
0
                int error = op->error;
1402
1403
0
                switch (op->type) {
1404
0
                case DPIF_OP_FLOW_PUT: {
1405
0
                    struct dpif_flow_put *put = &op->flow_put;
1406
1407
0
                    COVERAGE_INC(dpif_flow_put);
1408
0
                    log_flow_put_message(dpif, &this_module, put, error);
1409
0
                    if (error) {
1410
0
                        COVERAGE_INC(dpif_flow_put_error);
1411
0
                        if (put->stats) {
1412
0
                            memset(put->stats, 0, sizeof *put->stats);
1413
0
                        }
1414
0
                    }
1415
0
                    break;
1416
0
                }
1417
1418
0
                case DPIF_OP_FLOW_GET: {
1419
0
                    struct dpif_flow_get *get = &op->flow_get;
1420
1421
0
                    COVERAGE_INC(dpif_flow_get);
1422
0
                    if (error) {
1423
0
                        COVERAGE_INC(dpif_flow_get_error);
1424
0
                        memset(get->flow, 0, sizeof *get->flow);
1425
0
                    }
1426
0
                    log_flow_get_message(dpif, &this_module, get, error);
1427
0
                    break;
1428
0
                }
1429
1430
0
                case DPIF_OP_FLOW_DEL: {
1431
0
                    struct dpif_flow_del *del = &op->flow_del;
1432
1433
0
                    COVERAGE_INC(dpif_flow_del);
1434
0
                    log_flow_del_message(dpif, &this_module, del, error);
1435
0
                    if (error) {
1436
0
                        COVERAGE_INC(dpif_flow_del_error);
1437
0
                        if (del->stats) {
1438
0
                            memset(del->stats, 0, sizeof *del->stats);
1439
0
                        }
1440
0
                    }
1441
0
                    break;
1442
0
                }
1443
1444
0
                case DPIF_OP_EXECUTE:
1445
0
                    COVERAGE_INC(dpif_execute);
1446
0
                    log_execute_message(dpif, &this_module, &op->execute,
1447
0
                                        false, error);
1448
0
                    if (error) {
1449
0
                        COVERAGE_INC(dpif_execute_error);
1450
0
                    }
1451
0
                    break;
1452
0
                }
1453
0
            }
1454
1455
0
            ops += chunk;
1456
0
            n_ops -= chunk;
1457
0
        } else {
1458
            /* Help the dpif provider to execute one op. */
1459
0
            struct dpif_op *op = ops[0];
1460
1461
0
            COVERAGE_INC(dpif_execute);
1462
0
            op->error = dpif_execute_with_help(dpif, &op->execute);
1463
0
            ops++;
1464
0
            n_ops--;
1465
0
        }
1466
0
    }
1467
0
}
1468
1469
/* Returns a string that represents 'type', for use in log messages. */
1470
const char *
1471
dpif_upcall_type_to_string(enum dpif_upcall_type type)
1472
0
{
1473
0
    switch (type) {
1474
0
    case DPIF_UC_MISS: return "miss";
1475
0
    case DPIF_UC_ACTION: return "action";
1476
0
    case DPIF_N_UC_TYPES: default: return "<unknown>";
1477
0
    }
1478
0
}
1479
1480
/* Enables or disables receiving packets with dpif_recv() on 'dpif'.  Returns 0
1481
 * if successful, otherwise a positive errno value.
1482
 *
1483
 * Turning packet receive off and then back on may change the Netlink PID
1484
 * assignments returned by dpif_port_get_pid().  If the client does this, it
1485
 * must update all of the flows that have OVS_ACTION_ATTR_USERSPACE actions
1486
 * using the new PID assignment. */
1487
int
1488
dpif_recv_set(struct dpif *dpif, bool enable)
1489
0
{
1490
0
    int error = 0;
1491
1492
0
    if (dpif->dpif_class->recv_set) {
1493
0
        error = dpif->dpif_class->recv_set(dpif, enable);
1494
0
        log_operation(dpif, "recv_set", error);
1495
0
    }
1496
0
    return error;
1497
0
}
1498
1499
/* Refreshes the poll loops and Netlink sockets associated to each port,
1500
 * when the number of upcall handlers (upcall receiving thread) is changed
1501
 * to 'n_handlers' and receiving packets for 'dpif' is enabled by
1502
 * recv_set().
1503
 *
1504
 * Since multiple upcall handlers can read upcalls simultaneously from
1505
 * 'dpif', each port can have multiple Netlink sockets, one per upcall
1506
 * handler.  So, handlers_set() is responsible for the following tasks:
1507
 *
1508
 *    When receiving upcall is enabled, extends or creates the
1509
 *    configuration to support:
1510
 *
1511
 *        - 'n_handlers' Netlink sockets for each port.
1512
 *
1513
 *        - 'n_handlers' poll loops, one for each upcall handler.
1514
 *
1515
 *        - registering the Netlink sockets for the same upcall handler to
1516
 *          the corresponding poll loop.
1517
 *
1518
 * Returns 0 if successful, otherwise a positive errno value. */
1519
int
1520
dpif_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1521
0
{
1522
0
    int error = 0;
1523
1524
0
    if (dpif->dpif_class->handlers_set) {
1525
0
        error = dpif->dpif_class->handlers_set(dpif, n_handlers);
1526
0
        log_operation(dpif, "handlers_set", error);
1527
0
    }
1528
0
    return error;
1529
0
}
1530
1531
/* Checks if a certain number of handlers are required.
1532
 *
1533
 * If a certain number of handlers are required, returns 'true' and sets
1534
 * 'n_handlers' to that number of handler threads.
1535
 *
1536
 * If not, returns 'false'
1537
 */
1538
bool
1539
dpif_number_handlers_required(struct dpif *dpif, uint32_t *n_handlers)
1540
0
{
1541
0
    if (dpif->dpif_class->number_handlers_required) {
1542
0
        return dpif->dpif_class->number_handlers_required(dpif, n_handlers);
1543
0
    }
1544
0
    return false;
1545
0
}
1546
1547
void
1548
dpif_register_dp_purge_cb(struct dpif *dpif, dp_purge_callback *cb, void *aux)
1549
0
{
1550
0
    if (dpif->dpif_class->register_dp_purge_cb) {
1551
0
        dpif->dpif_class->register_dp_purge_cb(dpif, cb, aux);
1552
0
    }
1553
0
}
1554
1555
void
1556
dpif_register_upcall_cb(struct dpif *dpif, upcall_callback *cb, void *aux)
1557
0
{
1558
0
    if (dpif->dpif_class->register_upcall_cb) {
1559
0
        dpif->dpif_class->register_upcall_cb(dpif, cb, aux);
1560
0
    }
1561
0
}
1562
1563
void
1564
dpif_enable_upcall(struct dpif *dpif)
1565
0
{
1566
0
    if (dpif->dpif_class->enable_upcall) {
1567
0
        dpif->dpif_class->enable_upcall(dpif);
1568
0
    }
1569
0
}
1570
1571
void
1572
dpif_disable_upcall(struct dpif *dpif)
1573
0
{
1574
0
    if (dpif->dpif_class->disable_upcall) {
1575
0
        dpif->dpif_class->disable_upcall(dpif);
1576
0
    }
1577
0
}
1578
1579
void
1580
dpif_print_packet(struct dpif *dpif, struct dpif_upcall *upcall)
1581
0
{
1582
0
    if (!VLOG_DROP_DBG(&dpmsg_rl)) {
1583
0
        struct ds flow;
1584
0
        char *packet;
1585
1586
0
        packet = ofp_dp_packet_to_string(&upcall->packet);
1587
1588
0
        ds_init(&flow);
1589
0
        odp_flow_key_format(upcall->key, upcall->key_len, &flow);
1590
1591
0
        VLOG_DBG("%s: %s upcall:\n%s\n%s",
1592
0
                 dpif_name(dpif), dpif_upcall_type_to_string(upcall->type),
1593
0
                 ds_cstr(&flow), packet);
1594
1595
0
        ds_destroy(&flow);
1596
0
        free(packet);
1597
0
    }
1598
0
}
1599
1600
/* Pass custom configuration to the datapath implementation.  Some of the
1601
 * changes can be postponed until dpif_run() is called. */
1602
int
1603
dpif_set_config(struct dpif *dpif, const struct smap *cfg)
1604
0
{
1605
0
    int error = 0;
1606
1607
0
    if (dpif->dpif_class->set_config) {
1608
0
        error = dpif->dpif_class->set_config(dpif, cfg);
1609
0
        if (error) {
1610
0
            log_operation(dpif, "set_config", error);
1611
0
        }
1612
0
        dpif_offload_set_config(dpif, cfg);
1613
0
    }
1614
1615
0
    return error;
1616
0
}
1617
1618
/* Polls for an upcall from 'dpif' for an upcall handler.  Since there can
1619
 * be multiple poll loops, 'handler_id' is needed as index to identify the
1620
 * corresponding poll loop.  If successful, stores the upcall into '*upcall',
1621
 * using 'buf' for storage.  Should only be called if 'recv_set' has been used
1622
 * to enable receiving packets from 'dpif'.
1623
 *
1624
 * 'upcall->key' and 'upcall->userdata' point into data in the caller-provided
1625
 * 'buf', so their memory cannot be freed separately from 'buf'.
1626
 *
1627
 * The caller owns the data of 'upcall->packet' and may modify it.  If
1628
 * packet's headroom is exhausted as it is manipulated, 'upcall->packet'
1629
 * will be reallocated.  This requires the data of 'upcall->packet' to be
1630
 * released with ofpbuf_uninit() before 'upcall' is destroyed.  However,
1631
 * when an error is returned, the 'upcall->packet' may be uninitialized
1632
 * and should not be released.
1633
 *
1634
 * Returns 0 if successful, otherwise a positive errno value.  Returns EAGAIN
1635
 * if no upcall is immediately available. */
1636
int
1637
dpif_recv(struct dpif *dpif, uint32_t handler_id, struct dpif_upcall *upcall,
1638
          struct ofpbuf *buf)
1639
0
{
1640
0
    int error = EAGAIN;
1641
1642
0
    if (dpif->dpif_class->recv) {
1643
0
        error = dpif->dpif_class->recv(dpif, handler_id, upcall, buf);
1644
0
        if (!error) {
1645
0
            OVS_USDT_PROBE(dpif_recv, recv_upcall, dpif->full_name,
1646
0
                           upcall->type,
1647
0
                           dp_packet_data(&upcall->packet),
1648
0
                           dp_packet_size(&upcall->packet),
1649
0
                           upcall->key, upcall->key_len);
1650
1651
0
            dpif_print_packet(dpif, upcall);
1652
0
        } else if (error != EAGAIN) {
1653
0
            log_operation(dpif, "recv", error);
1654
0
        }
1655
0
    }
1656
0
    return error;
1657
0
}
1658
1659
/* Discards all messages that would otherwise be received by dpif_recv() on
1660
 * 'dpif'. */
1661
void
1662
dpif_recv_purge(struct dpif *dpif)
1663
0
{
1664
0
    COVERAGE_INC(dpif_purge);
1665
0
    if (dpif->dpif_class->recv_purge) {
1666
0
        dpif->dpif_class->recv_purge(dpif);
1667
0
    }
1668
0
}
1669
1670
/* Arranges for the poll loop for an upcall handler to wake up when 'dpif'
1671
 * 'dpif' has a message queued to be received with the recv member
1672
 * function.  Since there can be multiple poll loops, 'handler_id' is
1673
 * needed as index to identify the corresponding poll loop. */
1674
void
1675
dpif_recv_wait(struct dpif *dpif, uint32_t handler_id)
1676
0
{
1677
0
    if (dpif->dpif_class->recv_wait) {
1678
0
        dpif->dpif_class->recv_wait(dpif, handler_id);
1679
0
    }
1680
0
}
1681
1682
/*
1683
 * Return the datapath version. Caller is responsible for freeing
1684
 * the string.
1685
 */
1686
char *
1687
dpif_get_dp_version(const struct dpif *dpif)
1688
0
{
1689
0
    char *version = NULL;
1690
1691
0
    if (dpif->dpif_class->get_datapath_version) {
1692
0
        version = dpif->dpif_class->get_datapath_version();
1693
0
    }
1694
1695
0
    return version;
1696
0
}
1697
1698
/* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
1699
 * and '*engine_id', respectively. */
1700
void
1701
dpif_get_netflow_ids(const struct dpif *dpif,
1702
                     uint8_t *engine_type, uint8_t *engine_id)
1703
0
{
1704
0
    *engine_type = dpif->netflow_engine_type;
1705
0
    *engine_id = dpif->netflow_engine_id;
1706
0
}
1707
1708
/* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a priority
1709
 * value used for setting packet priority.
1710
 * On success, returns 0 and stores the priority into '*priority'.
1711
 * On failure, returns a positive errno value and stores 0 into '*priority'. */
1712
int
1713
dpif_queue_to_priority(const struct dpif *dpif, uint32_t queue_id,
1714
                       uint32_t *priority)
1715
0
{
1716
0
    int error = (dpif->dpif_class->queue_to_priority
1717
0
                 ? dpif->dpif_class->queue_to_priority(dpif, queue_id,
1718
0
                                                       priority)
1719
0
                 : EOPNOTSUPP);
1720
0
    if (error) {
1721
0
        *priority = 0;
1722
0
    }
1723
0
    log_operation(dpif, "queue_to_priority", error);
1724
0
    return error;
1725
0
}
1726

1727
void
1728
dpif_init(struct dpif *dpif, const struct dpif_class *dpif_class,
1729
          const char *name,
1730
          uint8_t netflow_engine_type, uint8_t netflow_engine_id)
1731
0
{
1732
0
    dpif->dpif_class = dpif_class;
1733
0
    dpif->base_name = xstrdup(name);
1734
0
    dpif->full_name = xasprintf("%s@%s", dpif_class->type, name);
1735
0
    dpif->netflow_engine_type = netflow_engine_type;
1736
0
    dpif->netflow_engine_id = netflow_engine_id;
1737
0
    ovsrcu_set(&dpif->offload_provider_collection, NULL);
1738
0
}
1739
1740
/* Undoes the results of initialization.
1741
 *
1742
 * Normally this function only needs to be called from dpif_close().
1743
 * However, it may be called by providers due to an error on opening
1744
 * that occurs after initialization.  It this case dpif_close() would
1745
 * never be called. */
1746
void
1747
dpif_uninit(struct dpif *dpif, bool close)
1748
0
{
1749
0
    char *base_name = dpif->base_name;
1750
0
    char *full_name = dpif->full_name;
1751
1752
0
    if (close) {
1753
0
        dpif->dpif_class->close(dpif);
1754
0
    }
1755
1756
0
    free(base_name);
1757
0
    free(full_name);
1758
0
}
1759

1760
static void
1761
log_operation(const struct dpif *dpif, const char *operation, int error)
1762
0
{
1763
0
    if (!error) {
1764
0
        VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", dpif_name(dpif), operation);
1765
0
    } else if (ofperr_is_valid(error)) {
1766
0
        VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1767
0
                     dpif_name(dpif), operation, ofperr_get_name(error));
1768
0
    } else {
1769
0
        VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1770
0
                     dpif_name(dpif), operation, ovs_strerror(error));
1771
0
    }
1772
0
}
1773
1774
static enum vlog_level
1775
flow_message_log_level(int error)
1776
0
{
1777
    /* If flows arrive in a batch, userspace may push down multiple
1778
     * unique flow definitions that overlap when wildcards are applied.
1779
     * Kernels that support flow wildcarding will reject these flows as
1780
     * duplicates (EEXIST), so lower the log level to debug for these
1781
     * types of messages. */
1782
0
    return (error && error != EEXIST) ? VLL_WARN : VLL_DBG;
1783
0
}
1784
1785
static bool
1786
should_log_flow_message(const struct vlog_module *module, int error)
1787
0
{
1788
0
    return !vlog_should_drop(module, flow_message_log_level(error),
1789
0
                             error ? &error_rl : &dpmsg_rl);
1790
0
}
1791
1792
void
1793
log_flow_message(const struct dpif *dpif, int error,
1794
                 const struct vlog_module *module,
1795
                 const char *operation,
1796
                 const struct nlattr *key, size_t key_len,
1797
                 const struct nlattr *mask, size_t mask_len,
1798
                 const ovs_u128 *ufid, const struct dpif_flow_stats *stats,
1799
                 const struct nlattr *actions, size_t actions_len)
1800
0
{
1801
0
    struct ds ds = DS_EMPTY_INITIALIZER;
1802
0
    ds_put_format(&ds, "%s: ", dpif_name(dpif));
1803
0
    if (error) {
1804
0
        ds_put_cstr(&ds, "failed to ");
1805
0
    }
1806
0
    ds_put_format(&ds, "%s ", operation);
1807
0
    if (error) {
1808
0
        ds_put_format(&ds, "(%s) ", ovs_strerror(error));
1809
0
    }
1810
0
    if (ufid) {
1811
0
        odp_format_ufid(ufid, &ds);
1812
0
        ds_put_cstr(&ds, " ");
1813
0
    }
1814
0
    odp_flow_format(key, key_len, mask, mask_len, NULL, &ds, true, true);
1815
0
    if (stats) {
1816
0
        ds_put_cstr(&ds, ", ");
1817
0
        dpif_flow_stats_format(stats, &ds);
1818
0
    }
1819
0
    if (actions) {
1820
0
        ds_put_cstr(&ds, ", actions:");
1821
0
        format_odp_actions(&ds, actions, actions_len, NULL);
1822
0
    }
1823
0
    vlog(module, flow_message_log_level(error), "%s", ds_cstr(&ds));
1824
0
    ds_destroy(&ds);
1825
0
}
1826
1827
void
1828
log_flow_put_message(const struct dpif *dpif,
1829
                     const struct vlog_module *module,
1830
                     const struct dpif_flow_put *put,
1831
                     int error)
1832
0
{
1833
0
    if (should_log_flow_message(module, error)
1834
0
        && !(put->flags & DPIF_FP_PROBE)) {
1835
0
        struct ds s;
1836
1837
0
        ds_init(&s);
1838
0
        ds_put_cstr(&s, "put");
1839
0
        if (put->flags & DPIF_FP_CREATE) {
1840
0
            ds_put_cstr(&s, "[create]");
1841
0
        }
1842
0
        if (put->flags & DPIF_FP_MODIFY) {
1843
0
            ds_put_cstr(&s, "[modify]");
1844
0
        }
1845
0
        if (put->flags & DPIF_FP_ZERO_STATS) {
1846
0
            ds_put_cstr(&s, "[zero]");
1847
0
        }
1848
0
        log_flow_message(dpif, error, module, ds_cstr(&s),
1849
0
                         put->key, put->key_len, put->mask, put->mask_len,
1850
0
                         put->ufid, put->stats, put->actions,
1851
0
                         put->actions_len);
1852
0
        ds_destroy(&s);
1853
0
    }
1854
0
}
1855
1856
void
1857
log_flow_del_message(const struct dpif *dpif,
1858
                     const struct vlog_module *module,
1859
                     const struct dpif_flow_del *del,
1860
                     int error)
1861
0
{
1862
0
    if (should_log_flow_message(module, error)) {
1863
0
        log_flow_message(dpif, error, module, "flow_del",
1864
0
                         del->key, del->key_len,
1865
0
                         NULL, 0, del->ufid, !error ? del->stats : NULL,
1866
0
                         NULL, 0);
1867
0
    }
1868
0
}
1869
1870
/* Logs that 'execute' was executed on 'dpif' and completed with errno 'error'
1871
 * (0 for success).  'subexecute' should be true if the execution is a result
1872
 * of breaking down a larger execution that needed help, false otherwise.
1873
 *
1874
 *
1875
 * XXX In theory, the log message could be deceptive because this function is
1876
 * called after the dpif_provider's '->execute' function, which is allowed to
1877
 * modify execute->packet and execute->md.  In practice, though:
1878
 *
1879
 *     - dpif-netlink doesn't modify execute->packet or execute->md.
1880
 *
1881
 *     - dpif-netdev does modify them but it is less likely to have problems
1882
 *       because it is built into ovs-vswitchd and cannot have version skew,
1883
 *       etc.
1884
 *
1885
 * It would still be better to avoid the potential problem.  I don't know of a
1886
 * good way to do that, though, that isn't expensive. */
1887
void
1888
log_execute_message(const struct dpif *dpif,
1889
                    const struct vlog_module *module,
1890
                    const struct dpif_execute *execute,
1891
                    bool subexecute, int error)
1892
0
{
1893
0
    if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))
1894
0
        && !execute->probe) {
1895
0
        struct ds ds = DS_EMPTY_INITIALIZER;
1896
0
        char *packet;
1897
0
        uint64_t stub[1024 / 8];
1898
0
        struct ofpbuf md = OFPBUF_STUB_INITIALIZER(stub);
1899
1900
0
        packet = ofp_packet_to_string(dp_packet_data(execute->packet),
1901
0
                                      dp_packet_size(execute->packet),
1902
0
                                      execute->packet->packet_type);
1903
0
        odp_key_from_dp_packet(&md, execute->packet);
1904
0
        ds_put_format(&ds, "%s: %sexecute ",
1905
0
                      dpif_name(dpif),
1906
0
                      (subexecute ? "sub-"
1907
0
                       : dpif_execute_needs_help(execute) ? "super-"
1908
0
                       : ""));
1909
0
        format_odp_actions(&ds, execute->actions, execute->actions_len, NULL);
1910
0
        if (error) {
1911
0
            ds_put_format(&ds, " failed (%s)", ovs_strerror(error));
1912
0
        }
1913
0
        ds_put_format(&ds, " on packet %s", packet);
1914
0
        ds_put_format(&ds, " with metadata ");
1915
0
        odp_flow_format(md.data, md.size, NULL, 0, NULL, &ds, true, false);
1916
0
        ds_put_format(&ds, " mtu %d", execute->mtu);
1917
0
        vlog(module, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
1918
0
        ds_destroy(&ds);
1919
0
        free(packet);
1920
0
        ofpbuf_uninit(&md);
1921
0
    }
1922
0
}
1923
1924
void
1925
log_flow_get_message(const struct dpif *dpif,
1926
                     const struct vlog_module *module,
1927
                     const struct dpif_flow_get *get,
1928
                     int error)
1929
0
{
1930
0
    if (should_log_flow_message(module, error)) {
1931
0
        log_flow_message(dpif, error, module, "flow_get",
1932
0
                         get->key, get->key_len,
1933
0
                         get->flow->mask, get->flow->mask_len,
1934
0
                         get->ufid, &get->flow->stats,
1935
0
                         get->flow->actions, get->flow->actions_len);
1936
0
    }
1937
0
}
1938
1939
bool
1940
dpif_supports_tnl_push_pop(const struct dpif *dpif)
1941
0
{
1942
0
    return dpif_is_netdev(dpif);
1943
0
}
1944
1945
bool
1946
dpif_may_support_explicit_drop_action(const struct dpif *dpif)
1947
0
{
1948
    /* TC does not support offloading this action. */
1949
0
    return dpif_is_netdev(dpif) || !dpif_offload_enabled();
1950
0
}
1951
1952
bool
1953
dpif_supports_lb_output_action(const struct dpif *dpif)
1954
0
{
1955
    /*
1956
     * Balance-tcp optimization is currently supported in netdev
1957
     * datapath only.
1958
     */
1959
0
    return dpif_is_netdev(dpif);
1960
0
}
1961
1962
bool
1963
dpif_may_support_psample(const struct dpif *dpif)
1964
0
{
1965
    /* Userspace datapath does not support this action. */
1966
0
    return !dpif_is_netdev(dpif);
1967
0
}
1968
1969
/* Meters */
1970
void
1971
dpif_meter_get_features(const struct dpif *dpif,
1972
                        struct ofputil_meter_features *features)
1973
0
{
1974
0
    memset(features, 0, sizeof *features);
1975
0
    if (dpif->dpif_class->meter_get_features) {
1976
0
        dpif->dpif_class->meter_get_features(dpif, features);
1977
0
    }
1978
0
}
1979
1980
/* Adds or modifies the meter in 'dpif' with the given 'meter_id' and
1981
 * the configuration in 'config'.
1982
 *
1983
 * The meter id specified through 'config->meter_id' is ignored. */
1984
int
1985
dpif_meter_set(struct dpif *dpif, ofproto_meter_id meter_id,
1986
               struct ofputil_meter_config *config)
1987
0
{
1988
0
    COVERAGE_INC(dpif_meter_set);
1989
1990
0
    if (!(config->flags & (OFPMF13_KBPS | OFPMF13_PKTPS))) {
1991
0
        return EBADF; /* Rate unit type not set. */
1992
0
    }
1993
1994
0
    if ((config->flags & OFPMF13_KBPS) && (config->flags & OFPMF13_PKTPS)) {
1995
0
        return EBADF; /* Both rate units may not be set. */
1996
0
    }
1997
1998
0
    if (config->n_bands == 0) {
1999
0
        return EINVAL;
2000
0
    }
2001
2002
0
    for (size_t i = 0; i < config->n_bands; i++) {
2003
0
        if (config->bands[i].rate == 0) {
2004
0
            return EDOM; /* Rate must be non-zero */
2005
0
        }
2006
0
    }
2007
2008
0
    int error = dpif->dpif_class->meter_set(dpif, meter_id, config);
2009
0
    if (!error) {
2010
0
        VLOG_DBG_RL(&dpmsg_rl, "%s: DPIF meter %"PRIu32" set",
2011
0
                    dpif_name(dpif), meter_id.uint32);
2012
0
        dpif_offload_meter_set(dpif, meter_id, config);
2013
0
    } else {
2014
0
        VLOG_WARN_RL(&error_rl, "%s: failed to set DPIF meter %"PRIu32": %s",
2015
0
                     dpif_name(dpif), meter_id.uint32, ovs_strerror(error));
2016
0
    }
2017
0
    return error;
2018
0
}
2019
2020
int
2021
dpif_meter_get(const struct dpif *dpif, ofproto_meter_id meter_id,
2022
               struct ofputil_meter_stats *stats, uint16_t n_bands)
2023
0
{
2024
0
    int error;
2025
2026
0
    COVERAGE_INC(dpif_meter_get);
2027
2028
0
    error = dpif->dpif_class->meter_get(dpif, meter_id, stats, n_bands);
2029
0
    if (!error) {
2030
0
        VLOG_DBG_RL(&dpmsg_rl, "%s: DPIF meter %"PRIu32" get stats",
2031
0
                    dpif_name(dpif), meter_id.uint32);
2032
0
        dpif_offload_meter_get(dpif, meter_id, stats);
2033
0
    } else {
2034
0
        VLOG_WARN_RL(&error_rl,
2035
0
                     "%s: failed to get DPIF meter %"PRIu32" stats: %s",
2036
0
                     dpif_name(dpif), meter_id.uint32, ovs_strerror(error));
2037
0
        stats->packet_in_count = ~0;
2038
0
        stats->byte_in_count = ~0;
2039
0
        stats->n_bands = 0;
2040
0
    }
2041
0
    return error;
2042
0
}
2043
2044
int
2045
dpif_meter_del(struct dpif *dpif, ofproto_meter_id meter_id,
2046
               struct ofputil_meter_stats *stats, uint16_t n_bands)
2047
0
{
2048
0
    int error;
2049
2050
0
    COVERAGE_INC(dpif_meter_del);
2051
2052
0
    error = dpif->dpif_class->meter_del(dpif, meter_id, stats, n_bands);
2053
0
    if (!error) {
2054
0
        VLOG_DBG_RL(&dpmsg_rl, "%s: DPIF meter %"PRIu32" deleted",
2055
0
                    dpif_name(dpif), meter_id.uint32);
2056
0
        dpif_offload_meter_del(dpif, meter_id, stats);
2057
0
    } else {
2058
0
        VLOG_WARN_RL(&error_rl,
2059
0
                     "%s: failed to delete DPIF meter %"PRIu32": %s",
2060
0
                     dpif_name(dpif), meter_id.uint32, ovs_strerror(error));
2061
0
        if (stats) {
2062
0
            stats->packet_in_count = ~0;
2063
0
            stats->byte_in_count = ~0;
2064
0
            stats->n_bands = 0;
2065
0
        }
2066
0
    }
2067
0
    return error;
2068
0
}
2069
2070
int
2071
dpif_bond_add(struct dpif *dpif, uint32_t bond_id, odp_port_t *member_map)
2072
0
{
2073
0
    return dpif->dpif_class->bond_add
2074
0
           ? dpif->dpif_class->bond_add(dpif, bond_id, member_map)
2075
0
           : EOPNOTSUPP;
2076
0
}
2077
2078
int
2079
dpif_bond_del(struct dpif *dpif, uint32_t bond_id)
2080
0
{
2081
0
    return dpif->dpif_class->bond_del
2082
0
           ? dpif->dpif_class->bond_del(dpif, bond_id)
2083
0
           : EOPNOTSUPP;
2084
0
}
2085
2086
int
2087
dpif_bond_stats_get(struct dpif *dpif, uint32_t bond_id,
2088
                    uint64_t *n_bytes)
2089
0
{
2090
0
    memset(n_bytes, 0, BOND_BUCKETS * sizeof *n_bytes);
2091
2092
0
    return dpif->dpif_class->bond_stats_get
2093
0
           ? dpif->dpif_class->bond_stats_get(dpif, bond_id, n_bytes)
2094
0
           : EOPNOTSUPP;
2095
0
}
2096
2097
int
2098
dpif_cache_get_supported_levels(struct dpif *dpif, uint32_t *levels)
2099
0
{
2100
0
    return dpif->dpif_class->cache_get_supported_levels
2101
0
           ? dpif->dpif_class->cache_get_supported_levels(dpif, levels)
2102
0
           : EOPNOTSUPP;
2103
0
}
2104
2105
int
2106
dpif_cache_get_name(struct dpif *dpif, uint32_t level, const char **name)
2107
0
{
2108
0
    return dpif->dpif_class->cache_get_name
2109
0
           ? dpif->dpif_class->cache_get_name(dpif, level, name)
2110
0
           : EOPNOTSUPP;
2111
0
}
2112
2113
int
2114
dpif_cache_get_size(struct dpif *dpif, uint32_t level, uint32_t *size)
2115
0
{
2116
0
    return dpif->dpif_class->cache_get_size
2117
0
           ? dpif->dpif_class->cache_get_size(dpif, level, size)
2118
0
           : EOPNOTSUPP;
2119
0
}
2120
2121
int
2122
dpif_cache_set_size(struct dpif *dpif, uint32_t level, uint32_t size)
2123
0
{
2124
0
    return dpif->dpif_class->cache_set_size
2125
0
           ? dpif->dpif_class->cache_set_size(dpif, level, size)
2126
           : EOPNOTSUPP;
2127
0
}