Coverage Report

Created: 2025-12-18 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/netdev.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017 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 "netdev.h"
19
20
#include <errno.h>
21
#include <inttypes.h>
22
#include <sys/types.h>
23
#include <netinet/in.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <unistd.h>
27
28
#ifndef _WIN32
29
#include <ifaddrs.h>
30
#include <net/if.h>
31
#include <sys/ioctl.h>
32
#endif
33
34
#include "cmap.h"
35
#include "coverage.h"
36
#include "dpif.h"
37
#include "dp-packet.h"
38
#include "dp-packet-gso.h"
39
#include "openvswitch/dynamic-string.h"
40
#include "fatal-signal.h"
41
#include "hash.h"
42
#include "openvswitch/list.h"
43
#include "netdev-offload-provider.h"
44
#include "netdev-provider.h"
45
#include "netdev-vport.h"
46
#include "odp-netlink.h"
47
#include "openvswitch/json.h"
48
#include "openflow/openflow.h"
49
#include "packets.h"
50
#include "openvswitch/ofp-print.h"
51
#include "openvswitch/poll-loop.h"
52
#include "seq.h"
53
#include "openvswitch/shash.h"
54
#include "smap.h"
55
#include "socket-util.h"
56
#include "sset.h"
57
#include "svec.h"
58
#include "openvswitch/vlog.h"
59
#include "flow.h"
60
#include "userspace-tso.h"
61
#include "util.h"
62
#ifdef __linux__
63
#include "tc.h"
64
#endif
65
66
VLOG_DEFINE_THIS_MODULE(netdev);
67
68
COVERAGE_DEFINE(netdev_received);
69
COVERAGE_DEFINE(netdev_sent);
70
COVERAGE_DEFINE(netdev_add_router);
71
COVERAGE_DEFINE(netdev_get_stats);
72
COVERAGE_DEFINE(netdev_push_header_drops);
73
COVERAGE_DEFINE(netdev_soft_seg_good);
74
COVERAGE_DEFINE(netdev_soft_seg_drops);
75
76
struct netdev_saved_flags {
77
    struct netdev *netdev;
78
    struct ovs_list node;           /* In struct netdev's saved_flags_list. */
79
    enum netdev_flags saved_flags;
80
    enum netdev_flags saved_values;
81
};
82
83
/* Protects 'netdev_shash' and the mutable members of struct netdev. */
84
static struct ovs_mutex netdev_mutex = OVS_MUTEX_INITIALIZER;
85
86
/* All created network devices. */
87
static struct shash netdev_shash OVS_GUARDED_BY(netdev_mutex)
88
    = SHASH_INITIALIZER(&netdev_shash);
89
90
/* Mutual exclusion of */
91
static struct ovs_mutex netdev_class_mutex OVS_ACQ_BEFORE(netdev_mutex)
92
    = OVS_MUTEX_INITIALIZER;
93
94
/* Contains 'struct netdev_registered_class'es. */
95
static struct cmap netdev_classes = CMAP_INITIALIZER;
96
97
struct netdev_registered_class {
98
    struct cmap_node cmap_node; /* In 'netdev_classes', by class->type. */
99
    const struct netdev_class *class;
100
101
    /* Number of references: one for the class itself and one for every
102
     * instance of the class. */
103
    struct ovs_refcount refcnt;
104
};
105
106
/* This is set pretty low because we probably won't learn anything from the
107
 * additional log messages. */
108
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
109
110
static void restore_all_flags(void *aux OVS_UNUSED);
111
void update_device_args(struct netdev *, const struct shash *args);
112
#ifdef HAVE_AF_XDP
113
void signal_remove_xdp(struct netdev *netdev);
114
#endif
115
116
int
117
netdev_n_txq(const struct netdev *netdev)
118
0
{
119
0
    return netdev->n_txq;
120
0
}
121
122
int
123
netdev_n_rxq(const struct netdev *netdev)
124
0
{
125
0
    return netdev->n_rxq;
126
0
}
127
128
bool
129
netdev_is_pmd(const struct netdev *netdev)
130
0
{
131
0
    return netdev->netdev_class->is_pmd;
132
0
}
133
134
bool
135
netdev_has_tunnel_push_pop(const struct netdev *netdev)
136
0
{
137
0
    return netdev->netdev_class->push_header
138
0
           && netdev->netdev_class->pop_header;
139
0
}
140
141
static void
142
netdev_initialize(void)
143
    OVS_EXCLUDED(netdev_mutex)
144
0
{
145
0
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
146
147
0
    if (ovsthread_once_start(&once)) {
148
0
        fatal_signal_add_hook(restore_all_flags, NULL, NULL, true);
149
150
0
        netdev_vport_patch_register();
151
152
0
#ifdef __linux__
153
0
        netdev_register_provider(&netdev_linux_class);
154
0
        netdev_register_provider(&netdev_internal_class);
155
0
        netdev_register_provider(&netdev_tap_class);
156
0
        netdev_vport_tunnel_register();
157
158
0
        netdev_register_flow_api_provider(&netdev_offload_tc);
159
#ifdef HAVE_AF_XDP
160
        netdev_register_provider(&netdev_afxdp_class);
161
        netdev_register_provider(&netdev_afxdp_nonpmd_class);
162
#endif
163
0
#endif
164
#if defined(__FreeBSD__) || defined(__NetBSD__)
165
        netdev_register_provider(&netdev_tap_class);
166
        netdev_register_provider(&netdev_bsd_class);
167
#endif
168
#ifdef _WIN32
169
        netdev_register_provider(&netdev_windows_class);
170
        netdev_register_provider(&netdev_internal_class);
171
        netdev_vport_tunnel_register();
172
#endif
173
0
        ovsthread_once_done(&once);
174
0
    }
175
0
}
176
177
/* Performs periodic work needed by all the various kinds of netdevs.
178
 *
179
 * If your program opens any netdevs, it must call this function within its
180
 * main poll loop. */
181
void
182
netdev_run(void)
183
    OVS_EXCLUDED(netdev_mutex)
184
0
{
185
0
    netdev_initialize();
186
187
0
    struct netdev_registered_class *rc;
188
0
    CMAP_FOR_EACH (rc, cmap_node, &netdev_classes) {
189
0
        if (rc->class->run) {
190
0
            rc->class->run(rc->class);
191
0
        }
192
0
    }
193
0
}
194
195
/* Arranges for poll_block() to wake up when netdev_run() needs to be called.
196
 *
197
 * If your program opens any netdevs, it must call this function within its
198
 * main poll loop. */
199
void
200
netdev_wait(void)
201
    OVS_EXCLUDED(netdev_mutex)
202
0
{
203
0
    netdev_initialize();
204
205
0
    struct netdev_registered_class *rc;
206
0
    CMAP_FOR_EACH (rc, cmap_node, &netdev_classes) {
207
0
        if (rc->class->wait) {
208
0
            rc->class->wait(rc->class);
209
0
        }
210
0
    }
211
0
}
212
213
static struct netdev_registered_class *
214
netdev_lookup_class(const char *type)
215
0
{
216
0
    struct netdev_registered_class *rc;
217
0
    CMAP_FOR_EACH_WITH_HASH (rc, cmap_node, hash_string(type, 0),
218
0
                             &netdev_classes) {
219
0
        if (!strcmp(type, rc->class->type)) {
220
0
            return rc;
221
0
        }
222
0
    }
223
0
    return NULL;
224
0
}
225
226
/* Initializes and registers a new netdev provider.  After successful
227
 * registration, new netdevs of that type can be opened using netdev_open(). */
228
int
229
netdev_register_provider(const struct netdev_class *new_class)
230
    OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
231
0
{
232
0
    int error;
233
234
0
    ovs_mutex_lock(&netdev_class_mutex);
235
0
    if (netdev_lookup_class(new_class->type)) {
236
0
        VLOG_WARN("attempted to register duplicate netdev provider: %s",
237
0
                   new_class->type);
238
0
        error = EEXIST;
239
0
    } else {
240
0
        error = new_class->init ? new_class->init() : 0;
241
0
        if (!error) {
242
0
            struct netdev_registered_class *rc;
243
244
0
            rc = xmalloc(sizeof *rc);
245
0
            cmap_insert(&netdev_classes, &rc->cmap_node,
246
0
                        hash_string(new_class->type, 0));
247
0
            rc->class = new_class;
248
0
            ovs_refcount_init(&rc->refcnt);
249
0
        } else {
250
0
            VLOG_ERR("failed to initialize %s network device class: %s",
251
0
                     new_class->type, ovs_strerror(error));
252
0
        }
253
0
    }
254
0
    ovs_mutex_unlock(&netdev_class_mutex);
255
256
0
    return error;
257
0
}
258
259
/* Unregisters a netdev provider.  'type' must have been previously registered
260
 * and not currently be in use by any netdevs.  After unregistration new
261
 * netdevs of that type cannot be opened using netdev_open().  (However, the
262
 * provider may still be accessible from other threads until the next RCU grace
263
 * period, so the caller must not free or re-register the same netdev_class
264
 * until that has passed.) */
265
int
266
netdev_unregister_provider(const char *type)
267
    OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
268
0
{
269
0
    struct netdev_registered_class *rc;
270
0
    int error;
271
272
0
    netdev_initialize();
273
274
0
    ovs_mutex_lock(&netdev_class_mutex);
275
0
    rc = netdev_lookup_class(type);
276
0
    if (!rc) {
277
0
        VLOG_WARN("attempted to unregister a netdev provider that is not "
278
0
                  "registered: %s", type);
279
0
        error = EAFNOSUPPORT;
280
0
    } else if (ovs_refcount_unref(&rc->refcnt) != 1) {
281
0
        ovs_refcount_ref(&rc->refcnt);
282
0
        VLOG_WARN("attempted to unregister in use netdev provider: %s",
283
0
                  type);
284
0
        error = EBUSY;
285
0
    } else  {
286
0
        cmap_remove(&netdev_classes, &rc->cmap_node,
287
0
                    hash_string(rc->class->type, 0));
288
0
        ovsrcu_postpone(free, rc);
289
0
        error = 0;
290
0
    }
291
0
    ovs_mutex_unlock(&netdev_class_mutex);
292
293
0
    return error;
294
0
}
295
296
/* Clears 'types' and enumerates the types of all currently registered netdev
297
 * providers into it.  The caller must first initialize the sset. */
298
void
299
netdev_enumerate_types(struct sset *types)
300
    OVS_EXCLUDED(netdev_mutex)
301
0
{
302
0
    netdev_initialize();
303
0
    sset_clear(types);
304
305
0
    struct netdev_registered_class *rc;
306
0
    CMAP_FOR_EACH (rc, cmap_node, &netdev_classes) {
307
0
        sset_add(types, rc->class->type);
308
0
    }
309
0
}
310
311
static const char *
312
netdev_vport_type_from_name(const char *name)
313
0
{
314
0
    struct netdev_registered_class *rc;
315
0
    const char *type;
316
0
    CMAP_FOR_EACH (rc, cmap_node, &netdev_classes) {
317
0
        const char *dpif_port = netdev_vport_class_get_dpif_port(rc->class);
318
0
        if (dpif_port && !strncmp(name, dpif_port, strlen(dpif_port))) {
319
0
            type = rc->class->type;
320
0
            return type;
321
0
        }
322
0
    }
323
0
    return NULL;
324
0
}
325
326
/* Check that the network device name is not the same as any of the registered
327
 * vport providers' dpif_port name (dpif_port is NULL if the vport provider
328
 * does not define it) or the datapath internal port name (e.g. ovs-system).
329
 *
330
 * Returns true if there is a name conflict, false otherwise. */
331
bool
332
netdev_is_reserved_name(const char *name)
333
    OVS_EXCLUDED(netdev_mutex)
334
0
{
335
0
    netdev_initialize();
336
337
0
    struct netdev_registered_class *rc;
338
0
    CMAP_FOR_EACH (rc, cmap_node, &netdev_classes) {
339
0
        const char *dpif_port = netdev_vport_class_get_dpif_port(rc->class);
340
0
        if (dpif_port && !strncmp(name, dpif_port, strlen(dpif_port))) {
341
0
            return true;
342
0
        }
343
0
    }
344
345
0
    if (!strncmp(name, "ovs-", 4)) {
346
0
        struct sset types;
347
0
        const char *type;
348
349
0
        sset_init(&types);
350
0
        dp_enumerate_types(&types);
351
0
        SSET_FOR_EACH (type, &types) {
352
0
            if (!strcmp(name+4, type)) {
353
0
                sset_destroy(&types);
354
0
                return true;
355
0
            }
356
0
        }
357
0
        sset_destroy(&types);
358
0
    }
359
360
0
    return false;
361
0
}
362
363
/* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
364
 * (e.g. "system") and returns zero if successful, otherwise a positive errno
365
 * value.  On success, sets '*netdevp' to the new network device, otherwise to
366
 * null.
367
 *
368
 * Some network devices may need to be configured (with netdev_set_config())
369
 * before they can be used.
370
 *
371
 * Before opening rxqs or sending packets, '*netdevp' may need to be
372
 * reconfigured (with netdev_is_reconf_required() and netdev_reconfigure()).
373
 * */
374
int
375
netdev_open(const char *name, const char *type, struct netdev **netdevp)
376
    OVS_EXCLUDED(netdev_mutex)
377
0
{
378
0
    struct netdev *netdev;
379
0
    int error = 0;
380
381
0
    if (!name[0]) {
382
        /* Reject empty names.  This saves the providers having to do this.  At
383
         * least one screwed this up: the netdev-linux "tap" implementation
384
         * passed the name directly to the Linux TUNSETIFF call, which treats
385
         * an empty string as a request to generate a unique name. */
386
0
        return EINVAL;
387
0
    }
388
389
0
    netdev_initialize();
390
391
0
    ovs_mutex_lock(&netdev_mutex);
392
0
    netdev = shash_find_data(&netdev_shash, name);
393
394
0
    if (netdev && type && type[0]) {
395
0
        if (strcmp(type, netdev->netdev_class->type)) {
396
397
0
            if (netdev->auto_classified) {
398
                /* If this device was first created without a classification
399
                 * type, for example due to routing or tunneling code, and they
400
                 * keep a reference, a "classified" call to open will fail.
401
                 * In this case we remove the classless device, and re-add it
402
                 * below. We remove the netdev from the shash, and change the
403
                 * sequence, so owners of the old classless device can
404
                 * release/cleanup. */
405
0
                if (netdev->node) {
406
0
                    shash_delete(&netdev_shash, netdev->node);
407
0
                    netdev->node = NULL;
408
0
                    netdev_change_seq_changed(netdev);
409
0
                }
410
411
0
                netdev = NULL;
412
0
            } else {
413
0
                error = EEXIST;
414
0
            }
415
0
        } else if (netdev->auto_classified) {
416
            /* If netdev reopened with type "system", clear auto_classified. */
417
0
            netdev->auto_classified = false;
418
0
        }
419
0
    }
420
421
0
    if (!netdev) {
422
0
        struct netdev_registered_class *rc;
423
424
0
        rc = netdev_lookup_class(type && type[0] ? type : "system");
425
0
        if (rc && ovs_refcount_try_ref_rcu(&rc->refcnt)) {
426
0
            netdev = rc->class->alloc();
427
0
            if (netdev) {
428
0
                memset(netdev, 0, sizeof *netdev);
429
0
                netdev->netdev_class = rc->class;
430
0
                netdev->auto_classified = type && type[0] ? false : true;
431
0
                netdev->name = xstrdup(name);
432
0
                netdev->change_seq = 1;
433
0
                netdev->reconfigure_seq = seq_create();
434
0
                netdev->last_reconfigure_seq =
435
0
                    seq_read(netdev->reconfigure_seq);
436
0
                ovsrcu_set(&netdev->flow_api, NULL);
437
0
                netdev->hw_info.oor = false;
438
0
                atomic_init(&netdev->hw_info.miss_api_supported, false);
439
0
                netdev->node = shash_add(&netdev_shash, name, netdev);
440
441
                /* By default enable one tx and rx queue per netdev. */
442
0
                netdev->n_txq = netdev->netdev_class->send ? 1 : 0;
443
0
                netdev->n_rxq = netdev->netdev_class->rxq_alloc ? 1 : 0;
444
445
0
                ovs_list_init(&netdev->saved_flags_list);
446
447
0
                error = rc->class->construct(netdev);
448
0
                if (!error) {
449
0
                    netdev_change_seq_changed(netdev);
450
0
                } else {
451
0
                    ovs_refcount_unref(&rc->refcnt);
452
0
                    seq_destroy(netdev->reconfigure_seq);
453
0
                    free(netdev->name);
454
0
                    ovs_assert(ovs_list_is_empty(&netdev->saved_flags_list));
455
0
                    shash_delete(&netdev_shash, netdev->node);
456
0
                    rc->class->dealloc(netdev);
457
0
                }
458
0
            } else {
459
0
                error = ENOMEM;
460
0
            }
461
0
        } else {
462
0
            VLOG_WARN("could not create netdev %s of unknown type %s",
463
0
                      name, type);
464
0
            error = EAFNOSUPPORT;
465
0
        }
466
0
    }
467
468
0
    if (!error) {
469
0
        netdev->ref_cnt++;
470
0
        *netdevp = netdev;
471
0
    } else {
472
0
        *netdevp = NULL;
473
0
    }
474
0
    ovs_mutex_unlock(&netdev_mutex);
475
476
0
    return error;
477
0
}
478
479
/* Returns a reference to 'netdev_' for the caller to own. Returns null if
480
 * 'netdev_' is null. */
481
struct netdev *
482
netdev_ref(const struct netdev *netdev_)
483
    OVS_EXCLUDED(netdev_mutex)
484
0
{
485
0
    struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
486
487
0
    if (netdev) {
488
0
        ovs_mutex_lock(&netdev_mutex);
489
0
        ovs_assert(netdev->ref_cnt > 0);
490
0
        netdev->ref_cnt++;
491
0
        ovs_mutex_unlock(&netdev_mutex);
492
0
    }
493
0
    return netdev;
494
0
}
495
496
/* Reconfigures the device 'netdev' with 'args'.  'args' may be empty
497
 * or NULL if none are needed. */
498
int
499
netdev_set_config(struct netdev *netdev, const struct smap *args, char **errp)
500
    OVS_EXCLUDED(netdev_mutex)
501
0
{
502
0
    if (netdev->netdev_class->set_config) {
503
0
        const struct smap no_args = SMAP_INITIALIZER(&no_args);
504
0
        char *verbose_error = NULL;
505
0
        int error;
506
507
0
        error = netdev->netdev_class->set_config(netdev,
508
0
                                                 args ? args : &no_args,
509
0
                                                 &verbose_error);
510
0
        if (error) {
511
0
            VLOG_WARN_BUF(verbose_error ? NULL : errp,
512
0
                          "%s: could not set configuration (%s)",
513
0
                          netdev_get_name(netdev), ovs_strerror(error));
514
0
            if (verbose_error) {
515
0
                if (errp) {
516
0
                    *errp = verbose_error;
517
0
                } else {
518
0
                    free(verbose_error);
519
0
                }
520
0
            }
521
0
        }
522
0
        return error;
523
0
    } else if (args && !smap_is_empty(args)) {
524
0
        VLOG_WARN_BUF(errp, "%s: arguments provided to device that is not configurable",
525
0
                      netdev_get_name(netdev));
526
0
    }
527
0
    return 0;
528
0
}
529
530
/* Returns the current configuration for 'netdev' in 'args'.  The caller must
531
 * have already initialized 'args' with smap_init().  Returns 0 on success, in
532
 * which case 'args' will be filled with 'netdev''s configuration.  On failure
533
 * returns a positive errno value, in which case 'args' will be empty.
534
 *
535
 * The caller owns 'args' and its contents and must eventually free them with
536
 * smap_destroy(). */
537
int
538
netdev_get_config(const struct netdev *netdev, struct smap *args)
539
    OVS_EXCLUDED(netdev_mutex)
540
0
{
541
0
    int error;
542
543
0
    smap_clear(args);
544
0
    if (netdev->netdev_class->get_config) {
545
0
        error = netdev->netdev_class->get_config(netdev, args);
546
0
        if (error) {
547
0
            smap_clear(args);
548
0
        }
549
0
    } else {
550
0
        error = 0;
551
0
    }
552
553
0
    return error;
554
0
}
555
556
const struct netdev_tunnel_config *
557
netdev_get_tunnel_config(const struct netdev *netdev)
558
    OVS_EXCLUDED(netdev_mutex)
559
0
{
560
0
    if (netdev->netdev_class->get_tunnel_config) {
561
0
        return netdev->netdev_class->get_tunnel_config(netdev);
562
0
    } else {
563
0
        return NULL;
564
0
    }
565
0
}
566
567
/* Returns the id of the numa node the 'netdev' is on.  If the function
568
 * is not implemented, returns NETDEV_NUMA_UNSPEC. */
569
int
570
netdev_get_numa_id(const struct netdev *netdev)
571
0
{
572
0
    if (netdev->netdev_class->get_numa_id) {
573
0
        return netdev->netdev_class->get_numa_id(netdev);
574
0
    } else {
575
0
        return NETDEV_NUMA_UNSPEC;
576
0
    }
577
0
}
578
579
static void
580
netdev_unref(struct netdev *dev)
581
    OVS_RELEASES(netdev_mutex)
582
0
{
583
0
    ovs_assert(dev->ref_cnt);
584
0
    if (!--dev->ref_cnt) {
585
0
        const struct netdev_class *class = dev->netdev_class;
586
0
        struct netdev_registered_class *rc;
587
588
0
        netdev_uninit_flow_api(dev);
589
590
0
        dev->netdev_class->destruct(dev);
591
592
0
        if (dev->node) {
593
0
            shash_delete(&netdev_shash, dev->node);
594
0
        }
595
0
        free(dev->name);
596
0
        seq_destroy(dev->reconfigure_seq);
597
0
        dev->netdev_class->dealloc(dev);
598
0
        ovs_mutex_unlock(&netdev_mutex);
599
600
0
        rc = netdev_lookup_class(class->type);
601
0
        ovs_refcount_unref(&rc->refcnt);
602
0
    } else {
603
0
        ovs_mutex_unlock(&netdev_mutex);
604
0
    }
605
0
}
606
607
/* Closes and destroys 'netdev'. */
608
void
609
netdev_close(struct netdev *netdev)
610
    OVS_EXCLUDED(netdev_mutex)
611
0
{
612
0
    if (netdev) {
613
0
        ovs_mutex_lock(&netdev_mutex);
614
0
        netdev_unref(netdev);
615
0
    }
616
0
}
617
618
/* Removes 'netdev' from the global shash and unrefs 'netdev'.
619
 *
620
 * This allows handler and revalidator threads to still retain references
621
 * to this netdev while the main thread changes interface configuration.
622
 *
623
 * This function should only be called by the main thread when closing
624
 * netdevs during user configuration changes. Otherwise, netdev_close should be
625
 * used to close netdevs. */
626
void
627
netdev_remove(struct netdev *netdev)
628
0
{
629
0
    if (netdev) {
630
0
        ovs_mutex_lock(&netdev_mutex);
631
0
        if (netdev->node) {
632
0
            shash_delete(&netdev_shash, netdev->node);
633
0
            netdev->node = NULL;
634
0
            netdev_change_seq_changed(netdev);
635
0
        }
636
0
        netdev_unref(netdev);
637
0
    }
638
0
}
639
640
/* Parses 'netdev_name_', which is of the form [type@]name into its component
641
 * pieces.  'name' and 'type' must be freed by the caller. */
642
void
643
netdev_parse_name(const char *netdev_name_, char **name, char **type)
644
0
{
645
0
    char *netdev_name = xstrdup(netdev_name_);
646
0
    char *separator;
647
648
0
    separator = strchr(netdev_name, '@');
649
0
    if (separator) {
650
0
        *separator = '\0';
651
0
        *type = netdev_name;
652
0
        *name = xstrdup(separator + 1);
653
0
    } else {
654
0
        *name = netdev_name;
655
0
        *type = xstrdup("system");
656
0
    }
657
0
}
658
659
/* Attempts to open a netdev_rxq handle for obtaining packets received on
660
 * 'netdev'.  On success, returns 0 and stores a nonnull 'netdev_rxq *' into
661
 * '*rxp'.  On failure, returns a positive errno value and stores NULL into
662
 * '*rxp'.
663
 *
664
 * Some kinds of network devices might not support receiving packets.  This
665
 * function returns EOPNOTSUPP in that case.*/
666
int
667
netdev_rxq_open(struct netdev *netdev, struct netdev_rxq **rxp, int id)
668
    OVS_EXCLUDED(netdev_mutex)
669
0
{
670
0
    int error;
671
672
0
    if (netdev->netdev_class->rxq_alloc && id < netdev->n_rxq) {
673
0
        struct netdev_rxq *rx = netdev->netdev_class->rxq_alloc();
674
0
        if (rx) {
675
0
            rx->netdev = netdev;
676
0
            rx->queue_id = id;
677
0
            error = netdev->netdev_class->rxq_construct(rx);
678
0
            if (!error) {
679
0
                netdev_ref(netdev);
680
0
                *rxp = rx;
681
0
                return 0;
682
0
            }
683
0
            netdev->netdev_class->rxq_dealloc(rx);
684
0
        } else {
685
0
            error = ENOMEM;
686
0
        }
687
0
    } else {
688
0
        error = EOPNOTSUPP;
689
0
    }
690
691
0
    *rxp = NULL;
692
0
    return error;
693
0
}
694
695
/* Closes 'rx'. */
696
void
697
netdev_rxq_close(struct netdev_rxq *rx)
698
    OVS_EXCLUDED(netdev_mutex)
699
0
{
700
0
    if (rx) {
701
0
        struct netdev *netdev = rx->netdev;
702
0
        netdev->netdev_class->rxq_destruct(rx);
703
0
        netdev->netdev_class->rxq_dealloc(rx);
704
0
        netdev_close(netdev);
705
0
    }
706
0
}
707
708
bool netdev_rxq_enabled(struct netdev_rxq *rx)
709
0
{
710
0
    bool enabled = true;
711
712
0
    if (rx->netdev->netdev_class->rxq_enabled) {
713
0
        enabled = rx->netdev->netdev_class->rxq_enabled(rx);
714
0
    }
715
0
    return enabled;
716
0
}
717
718
/* Attempts to receive a batch of packets from 'rx'.  'batch' should point to
719
 * the beginning of an array of NETDEV_MAX_BURST pointers to dp_packet.  If
720
 * successful, this function stores pointers to up to NETDEV_MAX_BURST
721
 * dp_packets into the array, transferring ownership of the packets to the
722
 * caller, stores the number of received packets in 'batch->count', and returns
723
 * 0.
724
 *
725
 * The implementation does not necessarily initialize any non-data members of
726
 * 'batch'.  That is, the caller must initialize layer pointers and metadata
727
 * itself, if desired, e.g. with pkt_metadata_init() and miniflow_extract().
728
 *
729
 * Returns EAGAIN immediately if no packet is ready to be received or another
730
 * positive errno value if an error was encountered. */
731
int
732
netdev_rxq_recv(struct netdev_rxq *rx, struct dp_packet_batch *batch,
733
                int *qfill)
734
0
{
735
0
    int retval;
736
737
0
    retval = rx->netdev->netdev_class->rxq_recv(rx, batch, qfill);
738
0
    if (!retval) {
739
0
        COVERAGE_INC(netdev_received);
740
0
    } else {
741
0
        batch->count = 0;
742
0
    }
743
0
    return retval;
744
0
}
745
746
/* Arranges for poll_block() to wake up when a packet is ready to be received
747
 * on 'rx'. */
748
void
749
netdev_rxq_wait(struct netdev_rxq *rx)
750
0
{
751
0
    rx->netdev->netdev_class->rxq_wait(rx);
752
0
}
753
754
/* Discards any packets ready to be received on 'rx'. */
755
int
756
netdev_rxq_drain(struct netdev_rxq *rx)
757
0
{
758
0
    return (rx->netdev->netdev_class->rxq_drain
759
0
            ? rx->netdev->netdev_class->rxq_drain(rx)
760
0
            : 0);
761
0
}
762
763
/* Configures the number of tx queues of 'netdev'. Returns 0 if successful,
764
 * otherwise a positive errno value.
765
 *
766
 * 'n_txq' specifies the exact number of transmission queues to create.
767
 *
768
 * The change might not effective immediately.  The caller must check if a
769
 * reconfiguration is required with netdev_is_reconf_required() and eventually
770
 * call netdev_reconfigure() before using the new queues.
771
 *
772
 * On error, the tx queue configuration is unchanged */
773
int
774
netdev_set_tx_multiq(struct netdev *netdev, unsigned int n_txq)
775
0
{
776
0
    int error;
777
778
0
    error = (netdev->netdev_class->set_tx_multiq
779
0
             ? netdev->netdev_class->set_tx_multiq(netdev, MAX(n_txq, 1))
780
0
             : EOPNOTSUPP);
781
782
0
    if (error && error != EOPNOTSUPP) {
783
0
        VLOG_DBG_RL(&rl, "failed to set tx queue for network device %s:"
784
0
                    "%s", netdev_get_name(netdev), ovs_strerror(error));
785
0
    }
786
787
0
    return error;
788
0
}
789
790
enum netdev_pt_mode
791
netdev_get_pt_mode(const struct netdev *netdev)
792
0
{
793
0
    return (netdev->netdev_class->get_pt_mode
794
0
            ? netdev->netdev_class->get_pt_mode(netdev)
795
0
            : NETDEV_PT_LEGACY_L2);
796
0
}
797
798
/* Attempts to segment GSO flagged packets and send them as multiple bundles.
799
 * This function is only used if at least one packet in the current batch is
800
 * flagged for TSO and the netdev does not support this.
801
 *
802
 * The return value is 0 if all batches sent successfully, and an error code
803
 * from netdev_class->send() if at least one batch failed to send. */
804
static int
805
netdev_send_tso(struct netdev *netdev, int qid,
806
                struct dp_packet_batch *batch, bool concurrent_txq)
807
0
{
808
0
    struct dp_packet_batch *batches;
809
0
    struct dp_packet *packet;
810
0
    int retval = 0;
811
0
    int n_packets;
812
0
    int n_batches;
813
0
    int error;
814
815
    /* Calculate the total number of packets in the batch after
816
     * the segmentation. */
817
0
    n_packets = 0;
818
0
    DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
819
0
        if (dp_packet_get_tso_segsz(packet)) {
820
0
            n_packets += dp_packet_gso_nr_segs(packet);
821
0
        } else {
822
0
            n_packets++;
823
0
        }
824
0
    }
825
826
0
    if (!n_packets) {
827
0
        return 0;
828
0
    }
829
830
    /* Allocate enough batches to store all the packets in order. */
831
0
    n_batches = DIV_ROUND_UP(n_packets, NETDEV_MAX_BURST);
832
0
    batches = xmalloc(n_batches * sizeof *batches);
833
834
0
    struct dp_packet_batch *curr_batch = batches;
835
0
    struct dp_packet_batch *last_batch = &batches[n_batches - 1];
836
0
    for (curr_batch = batches; curr_batch <= last_batch; curr_batch++) {
837
0
        dp_packet_batch_init(curr_batch);
838
0
    }
839
840
    /* Do the packet segmentation if TSO is flagged. */
841
0
    size_t size = dp_packet_batch_size(batch);
842
0
    size_t k;
843
0
    curr_batch = batches;
844
0
    DP_PACKET_BATCH_REFILL_FOR_EACH (k, size, packet, batch) {
845
0
        if (dp_packet_get_tso_segsz(packet)) {
846
0
            if (dp_packet_gso(packet, &curr_batch)) {
847
0
                COVERAGE_INC(netdev_soft_seg_good);
848
0
            } else {
849
0
                COVERAGE_INC(netdev_soft_seg_drops);
850
0
            }
851
0
            dp_packet_delete(packet);
852
0
        } else {
853
0
            if (dp_packet_batch_is_full(curr_batch)) {
854
0
                curr_batch++;
855
0
            }
856
857
0
            dp_packet_batch_add(curr_batch, packet);
858
0
        }
859
0
    }
860
861
0
    for (curr_batch = batches; curr_batch <= last_batch; curr_batch++) {
862
0
        DP_PACKET_BATCH_FOR_EACH (i, packet, curr_batch) {
863
0
            dp_packet_ol_send_prepare(packet, netdev->ol_flags);
864
0
        }
865
866
0
        error = netdev->netdev_class->send(netdev, qid, curr_batch,
867
0
                                           concurrent_txq);
868
0
        if (!error) {
869
0
            COVERAGE_INC(netdev_sent);
870
0
        } else {
871
0
            retval = error;
872
0
        }
873
0
    }
874
0
    free(batches);
875
0
    return retval;
876
0
}
877
878
/* Sends 'batch' on 'netdev'.  Returns 0 if successful (for every packet),
879
 * otherwise a positive errno value.  Returns EAGAIN without blocking if
880
 * at least one the packets cannot be queued immediately.  Returns EMSGSIZE
881
 * if a partial packet was transmitted or if a packet is too big or too small
882
 * to transmit on the device.
883
 *
884
 * The caller must make sure that 'netdev' supports sending by making sure that
885
 * 'netdev_n_txq(netdev)' returns >= 1.
886
 *
887
 * If the function returns a non-zero value, some of the packets might have
888
 * been sent anyway.
889
 *
890
 * The caller transfers ownership of all the packets to the network device,
891
 * regardless of success.
892
 *
893
 * If 'concurrent_txq' is true, the caller may perform concurrent calls
894
 * to netdev_send() with the same 'qid'. The netdev provider is responsible
895
 * for making sure that these concurrent calls do not create a race condition
896
 * by using locking or other synchronization if required.
897
 *
898
 * The network device is expected to maintain one or more packet
899
 * transmission queues, so that the caller does not ordinarily have to
900
 * do additional queuing of packets.  'qid' specifies the queue to use
901
 * and can be ignored if the implementation does not support multiple
902
 * queues. */
903
int
904
netdev_send(struct netdev *netdev, int qid, struct dp_packet_batch *batch,
905
            bool concurrent_txq)
906
0
{
907
0
    const uint64_t netdev_flags = netdev->ol_flags;
908
0
    struct dp_packet *packet;
909
0
    int error;
910
911
0
    if (userspace_tso_enabled()) {
912
0
        if (!(netdev_flags & NETDEV_TX_OFFLOAD_TCP_TSO)) {
913
0
            DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
914
0
                if (dp_packet_get_tso_segsz(packet)) {
915
0
                    return netdev_send_tso(netdev, qid, batch, concurrent_txq);
916
0
                }
917
0
            }
918
0
        } else if (!(netdev_flags & (NETDEV_TX_VXLAN_TNL_TSO |
919
0
                                     NETDEV_TX_GRE_TNL_TSO |
920
0
                                     NETDEV_TX_GENEVE_TNL_TSO))) {
921
0
            DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
922
0
                if (dp_packet_get_tso_segsz(packet)
923
0
                    && dp_packet_tunnel(packet)) {
924
0
                    return netdev_send_tso(netdev, qid, batch, concurrent_txq);
925
0
                }
926
0
            }
927
0
        } else if (!(netdev_flags & NETDEV_TX_OFFLOAD_OUTER_UDP_CKSUM)) {
928
0
            DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
929
0
                if (dp_packet_get_tso_segsz(packet)
930
0
                    && (dp_packet_tunnel_vxlan(packet)
931
0
                        || dp_packet_tunnel_geneve(packet))
932
0
                    && dp_packet_l4_checksum_partial(packet)) {
933
0
                    return netdev_send_tso(netdev, qid, batch, concurrent_txq);
934
0
                }
935
0
            }
936
0
        }
937
0
    }
938
939
0
    DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
940
0
        dp_packet_ol_send_prepare(packet, netdev_flags);
941
0
    }
942
943
0
    error = netdev->netdev_class->send(netdev, qid, batch, concurrent_txq);
944
0
    if (!error) {
945
0
        COVERAGE_INC(netdev_sent);
946
0
    }
947
0
    return error;
948
0
}
949
950
/* Pop tunnel header, build tunnel metadata and resize 'batch->packets'
951
 * for further processing.
952
 *
953
 * The caller must make sure that 'netdev' support this operation by checking
954
 * that netdev_has_tunnel_push_pop() returns true. */
955
void
956
netdev_pop_header(struct netdev *netdev, struct dp_packet_batch *batch)
957
0
{
958
0
    struct dp_packet *packet;
959
0
    size_t i, size = dp_packet_batch_size(batch);
960
961
0
    DP_PACKET_BATCH_REFILL_FOR_EACH (i, size, packet, batch) {
962
0
        packet = netdev->netdev_class->pop_header(packet);
963
0
        if (packet) {
964
            /* Reset the offload flags if present, to avoid wrong
965
             * interpretation in the further packet processing when
966
             * recirculated.*/
967
0
            dp_packet_reset_offload(packet);
968
0
            pkt_metadata_init_conn(&packet->md);
969
0
            dp_packet_batch_refill(batch, packet, i);
970
0
        }
971
0
    }
972
0
}
973
974
void
975
netdev_init_tnl_build_header_params(struct netdev_tnl_build_header_params *params,
976
                                    const struct flow *tnl_flow,
977
                                    const struct in6_addr *src,
978
                                    struct eth_addr dmac,
979
                                    struct eth_addr smac)
980
0
{
981
0
    params->flow = tnl_flow;
982
0
    params->dmac = dmac;
983
0
    params->smac = smac;
984
0
    params->s_ip = src;
985
0
    params->is_ipv6 = !IN6_IS_ADDR_V4MAPPED(src);
986
0
}
987
988
int netdev_build_header(const struct netdev *netdev,
989
                        struct ovs_action_push_tnl *data,
990
                        const struct netdev_tnl_build_header_params *params)
991
0
{
992
0
    if (netdev->netdev_class->build_header) {
993
0
        return netdev->netdev_class->build_header(netdev, data, params);
994
0
    }
995
0
    return EOPNOTSUPP;
996
0
}
997
998
/* Push tunnel header (reading from tunnel metadata) and resize
999
 * 'batch->packets' for further processing.
1000
 *
1001
 * The caller must make sure that 'netdev' support this operation by checking
1002
 * that netdev_has_tunnel_push_pop() returns true. */
1003
int
1004
netdev_push_header(const struct netdev *netdev,
1005
                   struct dp_packet_batch *batch,
1006
                   const struct ovs_action_push_tnl *data)
1007
0
{
1008
0
    struct dp_packet *packet;
1009
0
    size_t i, size = dp_packet_batch_size(batch);
1010
1011
0
    DP_PACKET_BATCH_REFILL_FOR_EACH (i, size, packet, batch) {
1012
0
        if (OVS_UNLIKELY(data->tnl_type != OVS_VPORT_TYPE_GENEVE &&
1013
0
                         data->tnl_type != OVS_VPORT_TYPE_VXLAN &&
1014
0
                         data->tnl_type != OVS_VPORT_TYPE_GRE &&
1015
0
                         data->tnl_type != OVS_VPORT_TYPE_IP6GRE &&
1016
0
                         dp_packet_get_tso_segsz(packet))) {
1017
0
            COVERAGE_INC(netdev_push_header_drops);
1018
0
            dp_packet_delete(packet);
1019
0
            VLOG_WARN_RL(&rl, "%s: Tunneling packets with TSO is not "
1020
0
                              "supported for %s tunnels: packet dropped",
1021
0
                         netdev_get_name(netdev), netdev_get_type(netdev));
1022
0
        } else {
1023
0
            if (data->tnl_type != OVS_VPORT_TYPE_GENEVE &&
1024
0
                data->tnl_type != OVS_VPORT_TYPE_VXLAN &&
1025
0
                data->tnl_type != OVS_VPORT_TYPE_GRE &&
1026
0
                data->tnl_type != OVS_VPORT_TYPE_IP6GRE) {
1027
0
                dp_packet_ol_send_prepare(packet, 0);
1028
0
            } else if (dp_packet_tunnel(packet)) {
1029
0
                if (dp_packet_get_tso_segsz(packet)) {
1030
0
                    COVERAGE_INC(netdev_push_header_drops);
1031
0
                    dp_packet_delete(packet);
1032
0
                    VLOG_WARN_RL(&rl, "%s: Tunneling packets with TSO is not "
1033
0
                                      "supported with multiple levels of "
1034
0
                                      "VXLAN, GENEVE, or GRE encapsulation.",
1035
0
                                 netdev_get_name(netdev));
1036
0
                    continue;
1037
0
                }
1038
0
                dp_packet_ol_send_prepare(packet, 0);
1039
0
            }
1040
0
            netdev->netdev_class->push_header(netdev, packet, data);
1041
1042
0
            pkt_metadata_init(&packet->md, data->out_port);
1043
0
            dp_packet_batch_refill(batch, packet, i);
1044
0
        }
1045
0
    }
1046
1047
0
    return 0;
1048
0
}
1049
1050
/* Registers with the poll loop to wake up from the next call to poll_block()
1051
 * when the packet transmission queue has sufficient room to transmit a packet
1052
 * with netdev_send().
1053
 *
1054
 * The network device is expected to maintain one or more packet
1055
 * transmission queues, so that the caller does not ordinarily have to
1056
 * do additional queuing of packets.  'qid' specifies the queue to use
1057
 * and can be ignored if the implementation does not support multiple
1058
 * queues. */
1059
void
1060
netdev_send_wait(struct netdev *netdev, int qid)
1061
0
{
1062
0
    if (netdev->netdev_class->send_wait) {
1063
0
        netdev->netdev_class->send_wait(netdev, qid);
1064
0
    }
1065
0
}
1066
1067
/* Attempts to set 'netdev''s MAC address to 'mac'.  Returns 0 if successful,
1068
 * otherwise a positive errno value. */
1069
int
1070
netdev_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
1071
0
{
1072
0
    return netdev->netdev_class->set_etheraddr(netdev, mac);
1073
0
}
1074
1075
/* Retrieves 'netdev''s MAC address.  If successful, returns 0 and copies the
1076
 * the MAC address into 'mac'.  On failure, returns a positive errno value and
1077
 * clears 'mac' to all-zeros. */
1078
int
1079
netdev_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
1080
0
{
1081
0
    int error;
1082
1083
0
    error = netdev->netdev_class->get_etheraddr(netdev, mac);
1084
0
    if (error) {
1085
0
        memset(mac, 0, sizeof *mac);
1086
0
    }
1087
0
    return error;
1088
0
}
1089
1090
/* Returns the name of the network device that 'netdev' represents,
1091
 * e.g. "eth0".  The caller must not modify or free the returned string. */
1092
const char *
1093
netdev_get_name(const struct netdev *netdev)
1094
0
{
1095
0
    return netdev->name;
1096
0
}
1097
1098
/* Retrieves the MTU of 'netdev'.  The MTU is the maximum size of transmitted
1099
 * (and received) packets, in bytes, not including the hardware header; thus,
1100
 * this is typically 1500 bytes for Ethernet devices.
1101
 *
1102
 * If successful, returns 0 and stores the MTU size in '*mtup'.  Returns
1103
 * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
1104
 * On other failure, returns a positive errno value.  On failure, sets '*mtup'
1105
 * to 0. */
1106
int
1107
netdev_get_mtu(const struct netdev *netdev, int *mtup)
1108
0
{
1109
0
    const struct netdev_class *class = netdev->netdev_class;
1110
0
    int error;
1111
1112
0
    error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP;
1113
0
    if (error) {
1114
0
        *mtup = 0;
1115
0
        if (error != EOPNOTSUPP) {
1116
0
            VLOG_DBG_RL(&rl, "failed to retrieve MTU for network device %s: "
1117
0
                         "%s", netdev_get_name(netdev), ovs_strerror(error));
1118
0
        }
1119
0
    }
1120
0
    return error;
1121
0
}
1122
1123
/* Sets the MTU of 'netdev'.  The MTU is the maximum size of transmitted
1124
 * (and received) packets, in bytes.
1125
 *
1126
 * If successful, returns 0.  Returns EOPNOTSUPP if 'netdev' does not have an
1127
 * MTU (as e.g. some tunnels do not).  On other failure, returns a positive
1128
 * errno value. */
1129
int
1130
netdev_set_mtu(struct netdev *netdev, int mtu)
1131
0
{
1132
0
    const struct netdev_class *class = netdev->netdev_class;
1133
0
    int error;
1134
1135
0
    error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP;
1136
0
    if (error && error != EOPNOTSUPP) {
1137
0
        VLOG_WARN_RL(&rl, "failed to set MTU for network device %s: %s",
1138
0
                     netdev_get_name(netdev), ovs_strerror(error));
1139
0
    }
1140
1141
0
    return error;
1142
0
}
1143
1144
/* If 'user_config' is true, the user wants to control 'netdev''s MTU and we
1145
 * should not override it.  If 'user_config' is false, we may adjust
1146
 * 'netdev''s MTU (e.g., if 'netdev' is internal). */
1147
void
1148
netdev_mtu_user_config(struct netdev *netdev, bool user_config)
1149
0
{
1150
0
    if (netdev->mtu_user_config != user_config) {
1151
0
        netdev_change_seq_changed(netdev);
1152
0
        netdev->mtu_user_config = user_config;
1153
0
    }
1154
0
}
1155
1156
/* Returns 'true' if the user explicitly specified an MTU value for 'netdev'.
1157
 * Otherwise, returns 'false', in which case we are allowed to adjust the
1158
 * device MTU. */
1159
bool
1160
netdev_mtu_is_user_config(struct netdev *netdev)
1161
0
{
1162
0
    return netdev->mtu_user_config;
1163
0
}
1164
1165
/* Returns the ifindex of 'netdev', if successful, as a positive number.  On
1166
 * failure, returns a negative errno value.
1167
 *
1168
 * The desired semantics of the ifindex value are a combination of those
1169
 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex.  An ifindex
1170
 * value should be unique within a host and remain stable at least until
1171
 * reboot.  SNMP says an ifindex "ranges between 1 and the value of ifNumber"
1172
 * but many systems do not follow this rule anyhow.
1173
 *
1174
 * Some network devices may not implement support for this function.  In such
1175
 * cases this function will always return -EOPNOTSUPP.
1176
 */
1177
int
1178
netdev_get_ifindex(const struct netdev *netdev)
1179
0
{
1180
0
    int (*get_ifindex)(const struct netdev *);
1181
1182
0
    get_ifindex = netdev->netdev_class->get_ifindex;
1183
1184
0
    return get_ifindex ? get_ifindex(netdev) : -EOPNOTSUPP;
1185
0
}
1186
1187
/* Stores the features supported by 'netdev' into each of '*current',
1188
 * '*advertised', '*supported', and '*peer' that are non-null.  Each value is a
1189
 * bitmap of "enum ofp_port_features" bits, in host byte order.  Returns 0 if
1190
 * successful, otherwise a positive errno value.  On failure, all of the
1191
 * passed-in values are set to 0.
1192
 *
1193
 * Some network devices may not implement support for this function.  In such
1194
 * cases this function will always return EOPNOTSUPP. */
1195
int
1196
netdev_get_features(const struct netdev *netdev,
1197
                    enum netdev_features *current,
1198
                    enum netdev_features *advertised,
1199
                    enum netdev_features *supported,
1200
                    enum netdev_features *peer)
1201
0
{
1202
0
    int (*get_features)(const struct netdev *netdev,
1203
0
                        enum netdev_features *current,
1204
0
                        enum netdev_features *advertised,
1205
0
                        enum netdev_features *supported,
1206
0
                        enum netdev_features *peer);
1207
0
    enum netdev_features dummy[4];
1208
0
    int error;
1209
1210
0
    if (!current) {
1211
0
        current = &dummy[0];
1212
0
    }
1213
0
    if (!advertised) {
1214
0
        advertised = &dummy[1];
1215
0
    }
1216
0
    if (!supported) {
1217
0
        supported = &dummy[2];
1218
0
    }
1219
0
    if (!peer) {
1220
0
        peer = &dummy[3];
1221
0
    }
1222
1223
0
    get_features = netdev->netdev_class->get_features;
1224
0
    error = get_features
1225
0
                    ? get_features(netdev, current, advertised, supported,
1226
0
                                   peer)
1227
0
                    : EOPNOTSUPP;
1228
0
    if (error) {
1229
0
        *current = *advertised = *supported = *peer = 0;
1230
0
    }
1231
0
    return error;
1232
0
}
1233
1234
int
1235
netdev_get_speed(const struct netdev *netdev, uint32_t *current, uint32_t *max)
1236
0
{
1237
0
    uint32_t current_dummy, max_dummy;
1238
0
    int error;
1239
1240
0
    if (!current) {
1241
0
        current = &current_dummy;
1242
0
    }
1243
0
    if (!max) {
1244
0
        max = &max_dummy;
1245
0
    }
1246
1247
0
    error = netdev->netdev_class->get_speed
1248
0
            ? netdev->netdev_class->get_speed(netdev, current, max)
1249
0
            : EOPNOTSUPP;
1250
1251
0
    if (error == EOPNOTSUPP) {
1252
0
        enum netdev_features current_f, supported_f;
1253
1254
0
        error = netdev_get_features(netdev, &current_f, NULL,
1255
0
                                    &supported_f, NULL);
1256
0
        *current = netdev_features_to_bps(current_f, 0) / 1000000;
1257
0
        *max = netdev_features_to_bps(supported_f, 0) / 1000000;
1258
0
    } else if (error) {
1259
0
        *current = *max = 0;
1260
0
    }
1261
0
    return error;
1262
0
}
1263
1264
/* Returns the maximum speed of a network connection that has the NETDEV_F_*
1265
 * bits in 'features', in bits per second.  If no bits that indicate a speed
1266
 * are set in 'features', returns 'default_bps'. */
1267
uint64_t
1268
netdev_features_to_bps(enum netdev_features features,
1269
                       uint64_t default_bps)
1270
27.4k
{
1271
27.4k
    enum {
1272
27.4k
        F_1000000MB = NETDEV_F_1TB_FD,
1273
27.4k
        F_100000MB = NETDEV_F_100GB_FD,
1274
27.4k
        F_40000MB = NETDEV_F_40GB_FD,
1275
27.4k
        F_10000MB = NETDEV_F_10GB_FD,
1276
27.4k
        F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
1277
27.4k
        F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
1278
27.4k
        F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
1279
27.4k
    };
1280
1281
27.4k
    return (  features & F_1000000MB ? UINT64_C(1000000000000)
1282
27.4k
            : features & F_100000MB  ? UINT64_C(100000000000)
1283
27.4k
            : features & F_40000MB   ? UINT64_C(40000000000)
1284
27.4k
            : features & F_10000MB   ? UINT64_C(10000000000)
1285
27.4k
            : features & F_1000MB    ? UINT64_C(1000000000)
1286
16.8k
            : features & F_100MB     ? UINT64_C(100000000)
1287
13.6k
            : features & F_10MB      ? UINT64_C(10000000)
1288
8.11k
                                     : default_bps);
1289
27.4k
}
1290
1291
/* Stores the duplex capability of 'netdev' into 'full_duplex'.
1292
 *
1293
 * Some network devices may not implement support for this function.
1294
 * In such cases this function will always return EOPNOTSUPP. */
1295
int
1296
netdev_get_duplex(const struct netdev *netdev, bool *full_duplex)
1297
0
{
1298
0
    int error;
1299
1300
0
    *full_duplex = false;
1301
0
    error = netdev->netdev_class->get_duplex
1302
0
            ? netdev->netdev_class->get_duplex(netdev, full_duplex)
1303
0
            : EOPNOTSUPP;
1304
1305
0
    if (error == EOPNOTSUPP) {
1306
0
        enum netdev_features current;
1307
1308
0
        error = netdev_get_features(netdev, &current, NULL, NULL, NULL);
1309
0
        if (!error && (current & NETDEV_F_OTHER)) {
1310
0
             error = EOPNOTSUPP;
1311
0
        }
1312
0
        if (!error) {
1313
0
            *full_duplex = (current & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD
1314
0
                                        | NETDEV_F_1GB_FD | NETDEV_F_10GB_FD
1315
0
                                        | NETDEV_F_40GB_FD | NETDEV_F_100GB_FD
1316
0
                                        | NETDEV_F_1TB_FD)) != 0;
1317
0
        }
1318
0
    }
1319
0
    return error;
1320
0
}
1321
1322
/* Set the features advertised by 'netdev' to 'advertise'.  Returns 0 if
1323
 * successful, otherwise a positive errno value. */
1324
int
1325
netdev_set_advertisements(struct netdev *netdev,
1326
                          enum netdev_features advertise)
1327
0
{
1328
0
    return (netdev->netdev_class->set_advertisements
1329
0
            ? netdev->netdev_class->set_advertisements(
1330
0
                    netdev, advertise)
1331
0
            : EOPNOTSUPP);
1332
0
}
1333
1334
static const char *
1335
netdev_feature_to_name(uint32_t bit)
1336
324k
{
1337
324k
    enum netdev_features f = bit;
1338
1339
324k
    switch (f) {
1340
27.6k
    case NETDEV_F_10MB_HD:    return "10MB-HD";
1341
26.1k
    case NETDEV_F_10MB_FD:    return "10MB-FD";
1342
26.4k
    case NETDEV_F_100MB_HD:   return "100MB-HD";
1343
32.7k
    case NETDEV_F_100MB_FD:   return "100MB-FD";
1344
25.0k
    case NETDEV_F_1GB_HD:     return "1GB-HD";
1345
26.6k
    case NETDEV_F_1GB_FD:     return "1GB-FD";
1346
25.5k
    case NETDEV_F_10GB_FD:    return "10GB-FD";
1347
1.71k
    case NETDEV_F_40GB_FD:    return "40GB-FD";
1348
1.72k
    case NETDEV_F_100GB_FD:   return "100GB-FD";
1349
1.81k
    case NETDEV_F_1TB_FD:     return "1TB-FD";
1350
1.61k
    case NETDEV_F_OTHER:      return "OTHER";
1351
22.1k
    case NETDEV_F_COPPER:     return "COPPER";
1352
28.6k
    case NETDEV_F_FIBER:      return "FIBER";
1353
25.2k
    case NETDEV_F_AUTONEG:    return "AUTO_NEG";
1354
25.1k
    case NETDEV_F_PAUSE:      return "AUTO_PAUSE";
1355
25.9k
    case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
1356
324k
    }
1357
1358
0
    return NULL;
1359
324k
}
1360
1361
void
1362
netdev_features_format(struct ds *s, enum netdev_features features)
1363
50.6k
{
1364
50.6k
    ofp_print_bit_names(s, features, netdev_feature_to_name, ' ');
1365
50.6k
    ds_put_char(s, '\n');
1366
50.6k
}
1367
1368
/* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
1369
 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.  Returns a
1370
 * positive errno value. */
1371
int
1372
netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
1373
0
{
1374
0
    return (netdev->netdev_class->set_in4
1375
0
            ? netdev->netdev_class->set_in4(netdev, addr, mask)
1376
0
            : EOPNOTSUPP);
1377
0
}
1378
1379
static int
1380
netdev_get_addresses_by_name(const char *device_name,
1381
                             struct in6_addr **addrsp, int *n_addrsp)
1382
0
{
1383
0
    struct netdev *netdev;
1384
0
    int error = netdev_open(device_name, NULL, &netdev);
1385
0
    if (error) {
1386
0
        *addrsp = NULL;
1387
0
        *n_addrsp = 0;
1388
0
        return error;
1389
0
    }
1390
1391
0
    struct in6_addr *masks;
1392
0
    error = netdev_get_addr_list(netdev, addrsp, &masks, n_addrsp);
1393
0
    netdev_close(netdev);
1394
0
    free(masks);
1395
0
    return error;
1396
0
}
1397
1398
/* Obtains an IPv4 address from 'device_name' and save the address in '*in4'.
1399
 * Returns 0 if successful, otherwise a positive errno value. */
1400
int
1401
netdev_get_in4_by_name(const char *device_name, struct in_addr *in4)
1402
0
{
1403
0
    struct in6_addr *addrs;
1404
0
    int n;
1405
0
    int error = netdev_get_addresses_by_name(device_name, &addrs, &n);
1406
1407
0
    in4->s_addr = 0;
1408
0
    if (!error) {
1409
0
        error = ENOENT;
1410
0
        for (int i = 0; i < n; i++) {
1411
0
            if (IN6_IS_ADDR_V4MAPPED(&addrs[i])) {
1412
0
                in4->s_addr = in6_addr_get_mapped_ipv4(&addrs[i]);
1413
0
                error = 0;
1414
0
                break;
1415
0
            }
1416
0
        }
1417
0
    }
1418
0
    free(addrs);
1419
1420
0
    return error;
1421
0
}
1422
1423
/* Obtains an IPv4 or IPv6 address from 'device_name' and save the address in
1424
 * '*in6', representing IPv4 addresses as v6-mapped.  Returns 0 if successful,
1425
 * otherwise a positive errno value. */
1426
int
1427
netdev_get_ip_by_name(const char *device_name, struct in6_addr *in6)
1428
0
{
1429
0
    struct in6_addr *addrs;
1430
0
    int n;
1431
0
    int error = netdev_get_addresses_by_name(device_name, &addrs, &n);
1432
1433
0
    *in6 = in6addr_any;
1434
0
    if (!error) {
1435
0
        error = ENOENT;
1436
0
        for (int i = 0; i < n; i++) {
1437
0
            if (!in6_is_lla(&addrs[i])) {
1438
0
                *in6 = addrs[i];
1439
0
                error = 0;
1440
0
                break;
1441
0
            }
1442
0
        }
1443
0
    }
1444
0
    free(addrs);
1445
1446
0
    return error;
1447
0
}
1448
1449
/* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
1450
 * to 'netdev'. */
1451
int
1452
netdev_add_router(struct netdev *netdev, struct in_addr router)
1453
0
{
1454
0
    COVERAGE_INC(netdev_add_router);
1455
0
    return (netdev->netdev_class->add_router
1456
0
            ? netdev->netdev_class->add_router(netdev, router)
1457
0
            : EOPNOTSUPP);
1458
0
}
1459
1460
/* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
1461
 * 'netdev'.  If a route cannot not be determined, sets '*next_hop' to 0,
1462
 * '*netdev_name' to null, and returns a positive errno value.  Otherwise, if a
1463
 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
1464
 * a directly connected network) in '*next_hop' and a copy of the name of the
1465
 * device to reach 'host' in '*netdev_name', and returns 0.  The caller is
1466
 * responsible for freeing '*netdev_name' (by calling free()). */
1467
int
1468
netdev_get_next_hop(const struct netdev *netdev,
1469
                    const struct in_addr *host, struct in_addr *next_hop,
1470
                    char **netdev_name)
1471
0
{
1472
0
    int error = (netdev->netdev_class->get_next_hop
1473
0
                 ? netdev->netdev_class->get_next_hop(
1474
0
                        host, next_hop, netdev_name)
1475
0
                 : EOPNOTSUPP);
1476
0
    if (error) {
1477
0
        next_hop->s_addr = 0;
1478
0
        *netdev_name = NULL;
1479
0
    }
1480
0
    return error;
1481
0
}
1482
1483
/* Populates 'smap' with status information.
1484
 *
1485
 * Populates 'smap' with 'netdev' specific status information.  This
1486
 * information may be used to populate the status column of the Interface table
1487
 * as defined in ovs-vswitchd.conf.db(5). */
1488
int
1489
netdev_get_status(const struct netdev *netdev, struct smap *smap)
1490
0
{
1491
0
    int err = EOPNOTSUPP;
1492
1493
    /* Set offload status only if relevant. */
1494
0
    if (netdev_get_dpif_type(netdev) &&
1495
0
        strcmp(netdev_get_dpif_type(netdev), "system")) {
1496
1497
0
#define OL_ADD_STAT(name, bit) \
1498
0
        smap_add(smap, "tx_" name "_offload", \
1499
0
                 netdev->ol_flags & bit ? "true" : "false");
1500
1501
0
        OL_ADD_STAT("ip_csum", NETDEV_TX_OFFLOAD_IPV4_CKSUM);
1502
0
        OL_ADD_STAT("tcp_csum", NETDEV_TX_OFFLOAD_TCP_CKSUM);
1503
0
        OL_ADD_STAT("udp_csum", NETDEV_TX_OFFLOAD_UDP_CKSUM);
1504
0
        OL_ADD_STAT("sctp_csum", NETDEV_TX_OFFLOAD_SCTP_CKSUM);
1505
0
        OL_ADD_STAT("tcp_seg", NETDEV_TX_OFFLOAD_TCP_TSO);
1506
0
        OL_ADD_STAT("vxlan_tso", NETDEV_TX_VXLAN_TNL_TSO);
1507
0
        OL_ADD_STAT("gre_tso", NETDEV_TX_GRE_TNL_TSO);
1508
0
        OL_ADD_STAT("geneve_tso", NETDEV_TX_GENEVE_TNL_TSO);
1509
0
        OL_ADD_STAT("out_ip_csum", NETDEV_TX_OFFLOAD_OUTER_IP_CKSUM);
1510
0
        OL_ADD_STAT("out_udp_csum", NETDEV_TX_OFFLOAD_OUTER_UDP_CKSUM);
1511
0
#undef OL_ADD_STAT
1512
1513
0
        err = 0;
1514
0
    }
1515
1516
0
    if (!netdev->netdev_class->get_status) {
1517
0
        return err;
1518
0
    }
1519
1520
0
    return netdev->netdev_class->get_status(netdev, smap);
1521
0
}
1522
1523
/* Returns all assigned IP address to  'netdev' and returns 0.
1524
 * API allocates array of address and masks and set it to
1525
 * '*addr' and '*mask'.
1526
 * Otherwise, returns a positive errno value and sets '*addr', '*mask
1527
 * and '*n_addr' to NULL.
1528
 *
1529
 * The following error values have well-defined meanings:
1530
 *
1531
 *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
1532
 *
1533
 *   - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
1534
 *
1535
 * 'addr' may be null, in which case the address itself is not reported. */
1536
int
1537
netdev_get_addr_list(const struct netdev *netdev, struct in6_addr **addr,
1538
                     struct in6_addr **mask, int *n_addr)
1539
0
{
1540
0
    int error;
1541
1542
0
    error = (netdev->netdev_class->get_addr_list
1543
0
             ? netdev->netdev_class->get_addr_list(netdev, addr, mask, n_addr): EOPNOTSUPP);
1544
0
    if (error && addr) {
1545
0
        *addr = NULL;
1546
0
        *mask = NULL;
1547
0
        *n_addr = 0;
1548
0
    }
1549
1550
0
    return error;
1551
0
}
1552
1553
/* On 'netdev', turns off the flags in 'off' and then turns on the flags in
1554
 * 'on'.  Returns 0 if successful, otherwise a positive errno value. */
1555
static int
1556
do_update_flags(struct netdev *netdev, enum netdev_flags off,
1557
                enum netdev_flags on, enum netdev_flags *old_flagsp,
1558
                struct netdev_saved_flags **sfp)
1559
    OVS_EXCLUDED(netdev_mutex)
1560
0
{
1561
0
    struct netdev_saved_flags *sf = NULL;
1562
0
    enum netdev_flags old_flags;
1563
0
    int error;
1564
1565
0
    error = netdev->netdev_class->update_flags(netdev, off & ~on, on,
1566
0
                                               &old_flags);
1567
0
    if (error) {
1568
0
        VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
1569
0
                     off || on ? "set" : "get", netdev_get_name(netdev),
1570
0
                     ovs_strerror(error));
1571
0
        old_flags = 0;
1572
0
    } else if ((off || on) && sfp) {
1573
0
        enum netdev_flags new_flags = (old_flags & ~off) | on;
1574
0
        enum netdev_flags changed_flags = old_flags ^ new_flags;
1575
0
        if (changed_flags) {
1576
0
            ovs_mutex_lock(&netdev_mutex);
1577
0
            *sfp = sf = xmalloc(sizeof *sf);
1578
0
            sf->netdev = netdev;
1579
0
            ovs_list_push_front(&netdev->saved_flags_list, &sf->node);
1580
0
            sf->saved_flags = changed_flags;
1581
0
            sf->saved_values = changed_flags & new_flags;
1582
1583
0
            netdev->ref_cnt++;
1584
0
            ovs_mutex_unlock(&netdev_mutex);
1585
0
        }
1586
0
    }
1587
1588
0
    if (old_flagsp) {
1589
0
        *old_flagsp = old_flags;
1590
0
    }
1591
0
    if (sfp) {
1592
0
        *sfp = sf;
1593
0
    }
1594
1595
0
    return error;
1596
0
}
1597
1598
/* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
1599
 * Returns 0 if successful, otherwise a positive errno value.  On failure,
1600
 * stores 0 into '*flagsp'. */
1601
int
1602
netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
1603
0
{
1604
0
    struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
1605
0
    return do_update_flags(netdev, 0, 0, flagsp, NULL);
1606
0
}
1607
1608
/* Sets the flags for 'netdev' to 'flags'.
1609
 * Returns 0 if successful, otherwise a positive errno value. */
1610
int
1611
netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
1612
                 struct netdev_saved_flags **sfp)
1613
0
{
1614
0
    return do_update_flags(netdev, -1, flags, NULL, sfp);
1615
0
}
1616
1617
/* Turns on the specified 'flags' on 'netdev':
1618
 *
1619
 *    - On success, returns 0.  If 'sfp' is nonnull, sets '*sfp' to a newly
1620
 *      allocated 'struct netdev_saved_flags *' that may be passed to
1621
 *      netdev_restore_flags() to restore the original values of 'flags' on
1622
 *      'netdev' (this will happen automatically at program termination if
1623
 *      netdev_restore_flags() is never called) , or to NULL if no flags were
1624
 *      actually changed.
1625
 *
1626
 *    - On failure, returns a positive errno value.  If 'sfp' is nonnull, sets
1627
 *      '*sfp' to NULL. */
1628
int
1629
netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
1630
                     struct netdev_saved_flags **sfp)
1631
0
{
1632
0
    return do_update_flags(netdev, 0, flags, NULL, sfp);
1633
0
}
1634
1635
/* Turns off the specified 'flags' on 'netdev'.  See netdev_turn_flags_on() for
1636
 * details of the interface. */
1637
int
1638
netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
1639
                      struct netdev_saved_flags **sfp)
1640
0
{
1641
0
    return do_update_flags(netdev, flags, 0, NULL, sfp);
1642
0
}
1643
1644
/* Restores the flags that were saved in 'sf', and destroys 'sf'.
1645
 * Does nothing if 'sf' is NULL. */
1646
void
1647
netdev_restore_flags(struct netdev_saved_flags *sf)
1648
    OVS_EXCLUDED(netdev_mutex)
1649
0
{
1650
0
    if (sf) {
1651
0
        struct netdev *netdev = sf->netdev;
1652
0
        enum netdev_flags old_flags;
1653
1654
0
        netdev->netdev_class->update_flags(netdev,
1655
0
                                           sf->saved_flags & sf->saved_values,
1656
0
                                           sf->saved_flags & ~sf->saved_values,
1657
0
                                           &old_flags);
1658
1659
0
        ovs_mutex_lock(&netdev_mutex);
1660
0
        ovs_list_remove(&sf->node);
1661
0
        free(sf);
1662
0
        netdev_unref(netdev);
1663
0
    }
1664
0
}
1665
1666
/* Looks up the ARP table entry for 'ip' on 'netdev'.  If one exists and can be
1667
 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1668
 * returns 0.  Otherwise, it returns a positive errno value; in particular,
1669
 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
1670
int
1671
netdev_arp_lookup(const struct netdev *netdev,
1672
                  ovs_be32 ip, struct eth_addr *mac)
1673
0
{
1674
0
    int error = (netdev->netdev_class->arp_lookup
1675
0
                 ? netdev->netdev_class->arp_lookup(netdev, ip, mac)
1676
0
                 : EOPNOTSUPP);
1677
0
    if (error) {
1678
0
        *mac = eth_addr_zero;
1679
0
    }
1680
0
    return error;
1681
0
}
1682
1683
/* Returns true if carrier is active (link light is on) on 'netdev'. */
1684
bool
1685
netdev_get_carrier(const struct netdev *netdev)
1686
0
{
1687
0
    int error;
1688
0
    enum netdev_flags flags;
1689
0
    bool carrier;
1690
1691
0
    netdev_get_flags(netdev, &flags);
1692
0
    if (!(flags & NETDEV_UP)) {
1693
0
        return false;
1694
0
    }
1695
1696
0
    if (!netdev->netdev_class->get_carrier) {
1697
0
        return true;
1698
0
    }
1699
1700
0
    error = netdev->netdev_class->get_carrier(netdev, &carrier);
1701
0
    if (error) {
1702
0
        VLOG_DBG("%s: failed to get network device carrier status, assuming "
1703
0
                 "down: %s", netdev_get_name(netdev), ovs_strerror(error));
1704
0
        carrier = false;
1705
0
    }
1706
1707
0
    return carrier;
1708
0
}
1709
1710
/* Returns the number of times 'netdev''s carrier has changed. */
1711
long long int
1712
netdev_get_carrier_resets(const struct netdev *netdev)
1713
0
{
1714
0
    return (netdev->netdev_class->get_carrier_resets
1715
0
            ? netdev->netdev_class->get_carrier_resets(netdev)
1716
0
            : 0);
1717
0
}
1718
1719
/* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
1720
 * link status instead of checking 'netdev''s carrier.  'netdev''s MII
1721
 * registers will be polled once ever 'interval' milliseconds.  If 'netdev'
1722
 * does not support MII, another method may be used as a fallback.  If
1723
 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
1724
 * its normal behavior.
1725
 *
1726
 * Returns 0 if successful, otherwise a positive errno value. */
1727
int
1728
netdev_set_miimon_interval(struct netdev *netdev, long long int interval)
1729
0
{
1730
0
    return (netdev->netdev_class->set_miimon_interval
1731
0
            ? netdev->netdev_class->set_miimon_interval(netdev, interval)
1732
0
            : EOPNOTSUPP);
1733
0
}
1734
1735
/* Retrieves current device stats for 'netdev'. */
1736
int
1737
netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1738
0
{
1739
0
    int error;
1740
1741
    /* Statistics are initialized before passing it to particular device
1742
     * implementation so all values are filtered out by default. */
1743
0
    memset(stats, 0xFF, sizeof *stats);
1744
1745
0
    COVERAGE_INC(netdev_get_stats);
1746
0
    error = (netdev->netdev_class->get_stats
1747
0
             ? netdev->netdev_class->get_stats(netdev, stats)
1748
0
             : EOPNOTSUPP);
1749
0
    if (error) {
1750
        /* In case of error all statistics are filtered out */
1751
0
        memset(stats, 0xff, sizeof *stats);
1752
0
    }
1753
0
    return error;
1754
0
}
1755
1756
/* Retrieves current device custom stats for 'netdev'. */
1757
int
1758
netdev_get_custom_stats(const struct netdev *netdev,
1759
                        struct netdev_custom_stats *custom_stats)
1760
0
{
1761
0
    int error;
1762
0
    memset(custom_stats, 0, sizeof *custom_stats);
1763
0
    error = (netdev->netdev_class->get_custom_stats
1764
0
             ? netdev->netdev_class->get_custom_stats(netdev, custom_stats)
1765
0
             : EOPNOTSUPP);
1766
1767
0
    return error;
1768
0
}
1769
1770
/* Attempts to set input rate limiting (policing) policy, such that:
1771
 * - up to 'kbits_rate' kbps of traffic is accepted, with a maximum
1772
 *   accumulative burst size of 'kbits' kb; and
1773
 * - up to 'kpkts' kpps of traffic is accepted, with a maximum
1774
 *   accumulative burst size of 'kpkts' kilo packets.
1775
 */
1776
int
1777
netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1778
                    uint32_t kbits_burst, uint32_t kpkts_rate,
1779
                    uint32_t kpkts_burst)
1780
0
{
1781
0
    return (netdev->netdev_class->set_policing
1782
0
            ? netdev->netdev_class->set_policing(netdev,
1783
0
                    kbits_rate, kbits_burst, kpkts_rate, kpkts_burst)
1784
0
            : EOPNOTSUPP);
1785
0
}
1786
1787
/* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1788
 * empty if 'netdev' does not support QoS.  Any names added to 'types' should
1789
 * be documented as valid for the "type" column in the "QoS" table in
1790
 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1791
 *
1792
 * Every network device supports disabling QoS with a type of "", but this type
1793
 * will not be added to 'types'.
1794
 *
1795
 * The caller must initialize 'types' (e.g. with sset_init()) before calling
1796
 * this function.  The caller is responsible for destroying 'types' (e.g. with
1797
 * sset_destroy()) when it is no longer needed.
1798
 *
1799
 * Returns 0 if successful, otherwise a positive errno value. */
1800
int
1801
netdev_get_qos_types(const struct netdev *netdev, struct sset *types)
1802
0
{
1803
0
    const struct netdev_class *class = netdev->netdev_class;
1804
0
    return (class->get_qos_types
1805
0
            ? class->get_qos_types(netdev, types)
1806
0
            : 0);
1807
0
}
1808
1809
/* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1810
 * which should be "" or one of the types returned by netdev_get_qos_types()
1811
 * for 'netdev'.  Returns 0 if successful, otherwise a positive errno value.
1812
 * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1813
 * 'caps' to all zeros. */
1814
int
1815
netdev_get_qos_capabilities(const struct netdev *netdev, const char *type,
1816
                            struct netdev_qos_capabilities *caps)
1817
0
{
1818
0
    const struct netdev_class *class = netdev->netdev_class;
1819
1820
0
    if (*type) {
1821
0
        int retval = (class->get_qos_capabilities
1822
0
                      ? class->get_qos_capabilities(netdev, type, caps)
1823
0
                      : EOPNOTSUPP);
1824
0
        if (retval) {
1825
0
            memset(caps, 0, sizeof *caps);
1826
0
        }
1827
0
        return retval;
1828
0
    } else {
1829
        /* Every netdev supports turning off QoS. */
1830
0
        memset(caps, 0, sizeof *caps);
1831
0
        return 0;
1832
0
    }
1833
0
}
1834
1835
/* Obtains the number of queues supported by 'netdev' for the specified 'type'
1836
 * of QoS.  Returns 0 if successful, otherwise a positive errno value.  Stores
1837
 * the number of queues (zero on failure) in '*n_queuesp'.
1838
 *
1839
 * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1840
int
1841
netdev_get_n_queues(const struct netdev *netdev,
1842
                    const char *type, unsigned int *n_queuesp)
1843
0
{
1844
0
    struct netdev_qos_capabilities caps;
1845
0
    int retval;
1846
1847
0
    retval = netdev_get_qos_capabilities(netdev, type, &caps);
1848
0
    *n_queuesp = caps.n_queues;
1849
0
    return retval;
1850
0
}
1851
1852
/* Queries 'netdev' about its currently configured form of QoS.  If successful,
1853
 * stores the name of the current form of QoS into '*typep', stores any details
1854
 * of configuration as string key-value pairs in 'details', and returns 0.  On
1855
 * failure, sets '*typep' to NULL and returns a positive errno value.
1856
 *
1857
 * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1858
 *
1859
 * The caller must initialize 'details' as an empty smap (e.g. with
1860
 * smap_init()) before calling this function.  The caller must free 'details'
1861
 * when it is no longer needed (e.g. with smap_destroy()).
1862
 *
1863
 * The caller must not modify or free '*typep'.
1864
 *
1865
 * '*typep' will be one of the types returned by netdev_get_qos_types() for
1866
 * 'netdev'.  The contents of 'details' should be documented as valid for
1867
 * '*typep' in the "other_config" column in the "QoS" table in
1868
 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1869
int
1870
netdev_get_qos(const struct netdev *netdev,
1871
               const char **typep, struct smap *details)
1872
0
{
1873
0
    const struct netdev_class *class = netdev->netdev_class;
1874
0
    int retval;
1875
1876
0
    if (class->get_qos) {
1877
0
        retval = class->get_qos(netdev, typep, details);
1878
0
        if (retval) {
1879
0
            *typep = NULL;
1880
0
            smap_clear(details);
1881
0
        }
1882
0
        return retval;
1883
0
    } else {
1884
        /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1885
0
        *typep = "";
1886
0
        return 0;
1887
0
    }
1888
0
}
1889
1890
/* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1891
 * with details of configuration from 'details'.  Returns 0 if successful,
1892
 * otherwise a positive errno value.  On error, the previous QoS configuration
1893
 * is retained.
1894
 *
1895
 * When this function changes the type of QoS (not just 'details'), this also
1896
 * resets all queue configuration for 'netdev' to their defaults (which depend
1897
 * on the specific type of QoS).  Otherwise, the queue configuration for
1898
 * 'netdev' is unchanged.
1899
 *
1900
 * 'type' should be "" (to disable QoS) or one of the types returned by
1901
 * netdev_get_qos_types() for 'netdev'.  The contents of 'details' should be
1902
 * documented as valid for the given 'type' in the "other_config" column in the
1903
 * "QoS" table in vswitchd/vswitch.xml (which is built as
1904
 * ovs-vswitchd.conf.db(8)).
1905
 *
1906
 * NULL may be specified for 'details' if there are no configuration
1907
 * details. */
1908
int
1909
netdev_set_qos(struct netdev *netdev,
1910
               const char *type, const struct smap *details)
1911
0
{
1912
0
    const struct netdev_class *class = netdev->netdev_class;
1913
1914
0
    if (!type) {
1915
0
        type = "";
1916
0
    }
1917
1918
0
    if (class->set_qos) {
1919
0
        if (!details) {
1920
0
            static const struct smap empty = SMAP_INITIALIZER(&empty);
1921
0
            details = &empty;
1922
0
        }
1923
0
        return class->set_qos(netdev, type, details);
1924
0
    } else {
1925
0
        return *type ? EOPNOTSUPP : 0;
1926
0
    }
1927
0
}
1928
1929
/* Queries 'netdev' for information about the queue numbered 'queue_id'.  If
1930
 * successful, adds that information as string key-value pairs to 'details'.
1931
 * Returns 0 if successful, otherwise a positive errno value.
1932
 *
1933
 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1934
 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1935
 *
1936
 * The returned contents of 'details' should be documented as valid for the
1937
 * given 'type' in the "other_config" column in the "Queue" table in
1938
 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1939
 *
1940
 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1941
 * this function.  The caller must free 'details' when it is no longer needed
1942
 * (e.g. with smap_destroy()). */
1943
int
1944
netdev_get_queue(const struct netdev *netdev,
1945
                 unsigned int queue_id, struct smap *details)
1946
0
{
1947
0
    const struct netdev_class *class = netdev->netdev_class;
1948
0
    int retval;
1949
1950
0
    retval = (class->get_queue
1951
0
              ? class->get_queue(netdev, queue_id, details)
1952
0
              : EOPNOTSUPP);
1953
0
    if (retval) {
1954
0
        smap_clear(details);
1955
0
    }
1956
0
    return retval;
1957
0
}
1958
1959
/* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1960
 * string pairs in 'details'.  The contents of 'details' should be documented
1961
 * as valid for the given 'type' in the "other_config" column in the "Queue"
1962
 * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1963
 * Returns 0 if successful, otherwise a positive errno value.  On failure, the
1964
 * given queue's configuration should be unmodified.
1965
 *
1966
 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1967
 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1968
 *
1969
 * This function does not modify 'details', and the caller retains ownership of
1970
 * it. */
1971
int
1972
netdev_set_queue(struct netdev *netdev,
1973
                 unsigned int queue_id, const struct smap *details)
1974
0
{
1975
0
    const struct netdev_class *class = netdev->netdev_class;
1976
0
    return (class->set_queue
1977
0
            ? class->set_queue(netdev, queue_id, details)
1978
0
            : EOPNOTSUPP);
1979
0
}
1980
1981
/* Attempts to delete the queue numbered 'queue_id' from 'netdev'.  Some kinds
1982
 * of QoS may have a fixed set of queues, in which case attempts to delete them
1983
 * will fail with EOPNOTSUPP.
1984
 *
1985
 * Returns 0 if successful, otherwise a positive errno value.  On failure, the
1986
 * given queue will be unmodified.
1987
 *
1988
 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1989
 * the current form of QoS (e.g. as returned by
1990
 * netdev_get_n_queues(netdev)). */
1991
int
1992
netdev_delete_queue(struct netdev *netdev, unsigned int queue_id)
1993
0
{
1994
0
    const struct netdev_class *class = netdev->netdev_class;
1995
0
    return (class->delete_queue
1996
0
            ? class->delete_queue(netdev, queue_id)
1997
0
            : EOPNOTSUPP);
1998
0
}
1999
2000
/* Obtains statistics about 'queue_id' on 'netdev'.  On success, returns 0 and
2001
 * fills 'stats' with the queue's statistics; individual members of 'stats' may
2002
 * be set to all-1-bits if the statistic is unavailable.  On failure, returns a
2003
 * positive errno value and fills 'stats' with values indicating unsupported
2004
 * statistics. */
2005
int
2006
netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
2007
                       struct netdev_queue_stats *stats)
2008
0
{
2009
0
    const struct netdev_class *class = netdev->netdev_class;
2010
0
    int retval;
2011
2012
0
    retval = (class->get_queue_stats
2013
0
              ? class->get_queue_stats(netdev, queue_id, stats)
2014
0
              : EOPNOTSUPP);
2015
0
    if (retval) {
2016
0
        stats->tx_bytes = UINT64_MAX;
2017
0
        stats->tx_packets = UINT64_MAX;
2018
0
        stats->tx_errors = UINT64_MAX;
2019
0
        stats->created = LLONG_MIN;
2020
0
    }
2021
0
    return retval;
2022
0
}
2023
2024
/* Initializes 'dump' to begin dumping the queues in a netdev.
2025
 *
2026
 * This function provides no status indication.  An error status for the entire
2027
 * dump operation is provided when it is completed by calling
2028
 * netdev_queue_dump_done().
2029
 */
2030
void
2031
netdev_queue_dump_start(struct netdev_queue_dump *dump,
2032
                        const struct netdev *netdev)
2033
0
{
2034
0
    dump->netdev = netdev_ref(netdev);
2035
0
    if (netdev->netdev_class->queue_dump_start) {
2036
0
        dump->error = netdev->netdev_class->queue_dump_start(netdev,
2037
0
                                                             &dump->state);
2038
0
    } else {
2039
0
        dump->error = EOPNOTSUPP;
2040
0
    }
2041
0
}
2042
2043
/* Attempts to retrieve another queue from 'dump', which must have been
2044
 * initialized with netdev_queue_dump_start().  On success, stores a new queue
2045
 * ID into '*queue_id', fills 'details' with configuration details for the
2046
 * queue, and returns true.  On failure, returns false.
2047
 *
2048
 * Queues are not necessarily dumped in increasing order of queue ID (or any
2049
 * other predictable order).
2050
 *
2051
 * Failure might indicate an actual error or merely that the last queue has
2052
 * been dumped.  An error status for the entire dump operation is provided when
2053
 * it is completed by calling netdev_queue_dump_done().
2054
 *
2055
 * The returned contents of 'details' should be documented as valid for the
2056
 * given 'type' in the "other_config" column in the "Queue" table in
2057
 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
2058
 *
2059
 * The caller must initialize 'details' (e.g. with smap_init()) before calling
2060
 * this function.  This function will clear and replace its contents.  The
2061
 * caller must free 'details' when it is no longer needed (e.g. with
2062
 * smap_destroy()). */
2063
bool
2064
netdev_queue_dump_next(struct netdev_queue_dump *dump,
2065
                       unsigned int *queue_id, struct smap *details)
2066
0
{
2067
0
    smap_clear(details);
2068
2069
0
    const struct netdev *netdev = dump->netdev;
2070
0
    if (dump->error) {
2071
0
        return false;
2072
0
    }
2073
2074
0
    dump->error = netdev->netdev_class->queue_dump_next(netdev, dump->state,
2075
0
                                                        queue_id, details);
2076
2077
0
    if (dump->error) {
2078
0
        netdev->netdev_class->queue_dump_done(netdev, dump->state);
2079
0
        return false;
2080
0
    }
2081
0
    return true;
2082
0
}
2083
2084
/* Completes queue table dump operation 'dump', which must have been
2085
 * initialized with netdev_queue_dump_start().  Returns 0 if the dump operation
2086
 * was error-free, otherwise a positive errno value describing the problem. */
2087
int
2088
netdev_queue_dump_done(struct netdev_queue_dump *dump)
2089
0
{
2090
0
    const struct netdev *netdev = dump->netdev;
2091
0
    if (!dump->error && netdev->netdev_class->queue_dump_done) {
2092
0
        dump->error = netdev->netdev_class->queue_dump_done(netdev,
2093
0
                                                            dump->state);
2094
0
    }
2095
0
    netdev_close(dump->netdev);
2096
0
    return dump->error == EOF ? 0 : dump->error;
2097
0
}
2098
2099
/* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
2100
 * its statistics, and the 'aux' specified by the caller.  The order of
2101
 * iteration is unspecified, but (when successful) each queue is visited
2102
 * exactly once.
2103
 *
2104
 * Calling this function may be more efficient than calling
2105
 * netdev_get_queue_stats() for every queue.
2106
 *
2107
 * 'cb' must not modify or free the statistics passed in.
2108
 *
2109
 * Returns 0 if successful, otherwise a positive errno value.  On error, some
2110
 * configured queues may not have been included in the iteration. */
2111
int
2112
netdev_dump_queue_stats(const struct netdev *netdev,
2113
                        netdev_dump_queue_stats_cb *cb, void *aux)
2114
0
{
2115
0
    const struct netdev_class *class = netdev->netdev_class;
2116
0
    return (class->dump_queue_stats
2117
0
            ? class->dump_queue_stats(netdev, cb, aux)
2118
0
            : EOPNOTSUPP);
2119
0
}
2120
2121

2122
/* Returns the class type of 'netdev'.
2123
 *
2124
 * The caller must not free the returned value. */
2125
const char *
2126
netdev_get_type(const struct netdev *netdev)
2127
0
{
2128
0
    return netdev->netdev_class->type;
2129
0
}
2130
2131
/* Returns the class associated with 'netdev'. */
2132
const struct netdev_class *
2133
netdev_get_class(const struct netdev *netdev)
2134
0
{
2135
0
    return netdev->netdev_class;
2136
0
}
2137
2138
/* Set the type of 'dpif' this 'netdev' belongs to. */
2139
void
2140
netdev_set_dpif_type(struct netdev *netdev, const char *type)
2141
0
{
2142
0
    netdev->dpif_type = type;
2143
0
}
2144
2145
/* Returns the type of 'dpif' this 'netdev' belongs to.
2146
 *
2147
 * The caller must not free the returned value. */
2148
const char *
2149
netdev_get_dpif_type(const struct netdev *netdev)
2150
0
{
2151
0
    return netdev->dpif_type;
2152
0
}
2153
2154
/* Returns the netdev with 'name' or NULL if there is none.
2155
 *
2156
 * The caller must free the returned netdev with netdev_close(). */
2157
struct netdev *
2158
netdev_from_name(const char *name)
2159
    OVS_EXCLUDED(netdev_mutex)
2160
0
{
2161
0
    struct netdev *netdev;
2162
2163
0
    ovs_mutex_lock(&netdev_mutex);
2164
0
    netdev = shash_find_data(&netdev_shash, name);
2165
0
    if (netdev) {
2166
0
        netdev->ref_cnt++;
2167
0
    }
2168
0
    ovs_mutex_unlock(&netdev_mutex);
2169
2170
0
    return netdev;
2171
0
}
2172
2173
/* Fills 'device_list' with devices that match 'netdev_class'.
2174
 *
2175
 * The caller is responsible for initializing and destroying 'device_list' and
2176
 * must close each device on the list. */
2177
void
2178
netdev_get_devices(const struct netdev_class *netdev_class,
2179
                   struct shash *device_list)
2180
    OVS_EXCLUDED(netdev_mutex)
2181
0
{
2182
0
    struct shash_node *node;
2183
2184
0
    ovs_mutex_lock(&netdev_mutex);
2185
0
    SHASH_FOR_EACH (node, &netdev_shash) {
2186
0
        struct netdev *dev = node->data;
2187
2188
0
        if (dev->netdev_class == netdev_class) {
2189
0
            dev->ref_cnt++;
2190
0
            shash_add(device_list, node->name, node->data);
2191
0
        }
2192
0
    }
2193
0
    ovs_mutex_unlock(&netdev_mutex);
2194
0
}
2195
2196
/* Extracts pointers to all 'netdev-vports' into an array 'vports'
2197
 * and returns it.  Stores the size of the array into '*size'.
2198
 *
2199
 * The caller is responsible for freeing 'vports' and must close
2200
 * each 'netdev-vport' in the list. */
2201
struct netdev **
2202
netdev_get_vports(size_t *size)
2203
    OVS_EXCLUDED(netdev_mutex)
2204
0
{
2205
0
    struct netdev **vports;
2206
0
    struct shash_node *node;
2207
0
    size_t n = 0;
2208
2209
0
    if (!size) {
2210
0
        return NULL;
2211
0
    }
2212
2213
    /* Explicitly allocates big enough chunk of memory. */
2214
0
    ovs_mutex_lock(&netdev_mutex);
2215
0
    vports = xmalloc(shash_count(&netdev_shash) * sizeof *vports);
2216
0
    SHASH_FOR_EACH (node, &netdev_shash) {
2217
0
        struct netdev *dev = node->data;
2218
2219
0
        if (netdev_vport_is_vport_class(dev->netdev_class)) {
2220
0
            dev->ref_cnt++;
2221
0
            vports[n] = dev;
2222
0
            n++;
2223
0
        }
2224
0
    }
2225
0
    ovs_mutex_unlock(&netdev_mutex);
2226
0
    *size = n;
2227
2228
0
    return vports;
2229
0
}
2230
2231
const char *
2232
netdev_get_type_from_name(const char *name)
2233
0
{
2234
0
    struct netdev *dev;
2235
0
    const char *type;
2236
0
    type = netdev_vport_type_from_name(name);
2237
0
    if (type == NULL) {
2238
0
        dev = netdev_from_name(name);
2239
0
        type = dev ? netdev_get_type(dev) : NULL;
2240
0
        netdev_close(dev);
2241
0
    }
2242
0
    return type;
2243
0
}
2244

2245
struct netdev *
2246
netdev_rxq_get_netdev(const struct netdev_rxq *rx)
2247
0
{
2248
0
    ovs_assert(rx->netdev->ref_cnt > 0);
2249
0
    return rx->netdev;
2250
0
}
2251
2252
const char *
2253
netdev_rxq_get_name(const struct netdev_rxq *rx)
2254
0
{
2255
0
    return netdev_get_name(netdev_rxq_get_netdev(rx));
2256
0
}
2257
2258
int
2259
netdev_rxq_get_queue_id(const struct netdev_rxq *rx)
2260
0
{
2261
0
    return rx->queue_id;
2262
0
}
2263
2264
static void
2265
restore_all_flags(void *aux OVS_UNUSED)
2266
0
{
2267
0
    struct shash_node *node;
2268
2269
0
    SHASH_FOR_EACH (node, &netdev_shash) {
2270
0
        struct netdev *netdev = node->data;
2271
0
        const struct netdev_saved_flags *sf;
2272
0
        enum netdev_flags saved_values;
2273
0
        enum netdev_flags saved_flags;
2274
2275
0
        saved_values = saved_flags = 0;
2276
0
        LIST_FOR_EACH (sf, node, &netdev->saved_flags_list) {
2277
0
            saved_flags |= sf->saved_flags;
2278
0
            saved_values &= ~sf->saved_flags;
2279
0
            saved_values |= sf->saved_flags & sf->saved_values;
2280
0
        }
2281
0
        if (saved_flags) {
2282
0
            enum netdev_flags old_flags;
2283
2284
0
            netdev->netdev_class->update_flags(netdev,
2285
0
                                               saved_flags & saved_values,
2286
0
                                               saved_flags & ~saved_values,
2287
0
                                               &old_flags);
2288
0
        }
2289
#ifdef HAVE_AF_XDP
2290
        if (netdev->netdev_class == &netdev_afxdp_class) {
2291
            signal_remove_xdp(netdev);
2292
        }
2293
#endif
2294
0
    }
2295
0
}
2296
2297
uint64_t
2298
netdev_get_change_seq(const struct netdev *netdev)
2299
0
{
2300
0
    uint64_t change_seq;
2301
2302
0
    atomic_read_explicit(&CONST_CAST(struct netdev *, netdev)->change_seq,
2303
0
                        &change_seq, memory_order_acquire);
2304
2305
0
    return change_seq;
2306
0
}
2307
2308
#ifndef _WIN32
2309
/* This implementation is shared by Linux and BSD. */
2310
2311
static struct ifaddrs *if_addr_list;
2312
static struct ovs_mutex if_addr_list_lock = OVS_MUTEX_INITIALIZER;
2313
2314
void
2315
netdev_get_addrs_list_flush(void)
2316
0
{
2317
0
    ovs_mutex_lock(&if_addr_list_lock);
2318
0
    if (if_addr_list) {
2319
0
        freeifaddrs(if_addr_list);
2320
0
        if_addr_list = NULL;
2321
0
    }
2322
0
    ovs_mutex_unlock(&if_addr_list_lock);
2323
0
}
2324
2325
int
2326
netdev_get_addrs(const char dev[], struct in6_addr **paddr,
2327
                 struct in6_addr **pmask, int *n_in)
2328
0
{
2329
0
    struct in6_addr *addr_array, *mask_array;
2330
0
    const struct ifaddrs *ifa;
2331
0
    int cnt = 0, i = 0;
2332
0
    int retries = 3;
2333
2334
0
    ovs_mutex_lock(&if_addr_list_lock);
2335
0
    if (!if_addr_list) {
2336
0
        int err;
2337
2338
0
retry:
2339
0
        err = getifaddrs(&if_addr_list);
2340
0
        if (err) {
2341
0
            ovs_mutex_unlock(&if_addr_list_lock);
2342
0
            return -err;
2343
0
        }
2344
0
        retries--;
2345
0
    }
2346
2347
0
    for (ifa = if_addr_list; ifa; ifa = ifa->ifa_next) {
2348
0
        if (!ifa->ifa_name) {
2349
0
            if (retries) {
2350
                /* Older versions of glibc have a bug on race condition with
2351
                 * address addition which may cause one of the returned
2352
                 * ifa_name values to be NULL. In such case, we know that we've
2353
                 * got an inconsistent dump. Retry but beware of an endless
2354
                 * loop. From glibc 2.28 and beyond, this workaround is not
2355
                 * needed and should be eventually removed. */
2356
0
                freeifaddrs(if_addr_list);
2357
0
                goto retry;
2358
0
            } else {
2359
0
                VLOG_WARN("Proceeding with an inconsistent dump of "
2360
0
                          "interfaces from the kernel. Some may be missing");
2361
0
            }
2362
0
        }
2363
0
        if (ifa->ifa_addr && ifa->ifa_name && ifa->ifa_netmask) {
2364
0
            int family;
2365
2366
0
            family = ifa->ifa_addr->sa_family;
2367
0
            if (family == AF_INET || family == AF_INET6) {
2368
0
                if (!strncmp(ifa->ifa_name, dev, IFNAMSIZ)) {
2369
0
                    cnt++;
2370
0
                }
2371
0
            }
2372
0
        }
2373
0
    }
2374
2375
0
    if (!cnt) {
2376
0
        ovs_mutex_unlock(&if_addr_list_lock);
2377
0
        return EADDRNOTAVAIL;
2378
0
    }
2379
0
    addr_array = xzalloc(sizeof *addr_array * cnt);
2380
0
    mask_array = xzalloc(sizeof *mask_array * cnt);
2381
0
    for (ifa = if_addr_list; ifa; ifa = ifa->ifa_next) {
2382
0
        if (ifa->ifa_name
2383
0
            && ifa->ifa_addr
2384
0
            && ifa->ifa_netmask
2385
0
            && !strncmp(ifa->ifa_name, dev, IFNAMSIZ)
2386
0
            && sa_is_ip(ifa->ifa_addr)) {
2387
0
            addr_array[i] = sa_get_address(ifa->ifa_addr);
2388
0
            mask_array[i] = sa_get_address(ifa->ifa_netmask);
2389
0
            i++;
2390
0
        }
2391
0
    }
2392
0
    ovs_mutex_unlock(&if_addr_list_lock);
2393
0
    if (paddr) {
2394
0
        *n_in = cnt;
2395
0
        *paddr = addr_array;
2396
0
        *pmask = mask_array;
2397
0
    } else {
2398
0
        free(addr_array);
2399
0
        free(mask_array);
2400
0
    }
2401
0
    return 0;
2402
0
}
2403
#endif
2404
2405
void
2406
netdev_wait_reconf_required(struct netdev *netdev)
2407
0
{
2408
0
    seq_wait(netdev->reconfigure_seq, netdev->last_reconfigure_seq);
2409
0
}
2410
2411
bool
2412
netdev_is_reconf_required(struct netdev *netdev)
2413
0
{
2414
0
    return seq_read(netdev->reconfigure_seq) != netdev->last_reconfigure_seq;
2415
0
}
2416
2417
/* Give a chance to 'netdev' to reconfigure some of its parameters.
2418
 *
2419
 * If a module uses netdev_send() and netdev_rxq_recv(), it must call this
2420
 * function when netdev_is_reconf_required() returns true.
2421
 *
2422
 * Return 0 if successful, otherwise a positive errno value.  If the
2423
 * reconfiguration fails the netdev will not be able to send or receive
2424
 * packets.
2425
 *
2426
 * When this function is called, no call to netdev_rxq_recv() or netdev_send()
2427
 * must be issued. */
2428
int
2429
netdev_reconfigure(struct netdev *netdev)
2430
0
{
2431
0
    const struct netdev_class *class = netdev->netdev_class;
2432
2433
0
    netdev->last_reconfigure_seq = seq_read(netdev->reconfigure_seq);
2434
2435
0
    return (class->reconfigure
2436
0
            ? class->reconfigure(netdev)
2437
0
            : EOPNOTSUPP);
2438
0
}
2439
2440
void
2441
netdev_free_custom_stats_counters(struct netdev_custom_stats *custom_stats)
2442
50.4k
{
2443
50.4k
    if (custom_stats) {
2444
50.4k
        if (custom_stats->counters) {
2445
12.9k
            free(custom_stats->counters);
2446
            custom_stats->counters = NULL;
2447
12.9k
            custom_stats->size = 0;
2448
12.9k
        }
2449
50.4k
    }
2450
50.4k
}