Coverage Report

Created: 2023-03-26 07:42

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