Coverage Report

Created: 2025-07-11 06:11

/src/openvswitch/lib/netdev.c
Line
Count
Source (jump to first uncovered line)
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
0
{
1271
0
    enum {
1272
0
        F_1000000MB = NETDEV_F_1TB_FD,
1273
0
        F_100000MB = NETDEV_F_100GB_FD,
1274
0
        F_40000MB = NETDEV_F_40GB_FD,
1275
0
        F_10000MB = NETDEV_F_10GB_FD,
1276
0
        F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
1277
0
        F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
1278
0
        F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
1279
0
    };
1280
1281
0
    return (  features & F_1000000MB ? UINT64_C(1000000000000)
1282
0
            : features & F_100000MB  ? UINT64_C(100000000000)
1283
0
            : features & F_40000MB   ? UINT64_C(40000000000)
1284
0
            : features & F_10000MB   ? UINT64_C(10000000000)
1285
0
            : features & F_1000MB    ? UINT64_C(1000000000)
1286
0
            : features & F_100MB     ? UINT64_C(100000000)
1287
0
            : features & F_10MB      ? UINT64_C(10000000)
1288
0
                                     : default_bps);
1289
0
}
1290
1291
/* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
1292
 * are set in 'features', otherwise false. */
1293
bool
1294
netdev_features_is_full_duplex(enum netdev_features features)
1295
0
{
1296
0
    return (features & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD | NETDEV_F_1GB_FD
1297
0
                        | NETDEV_F_10GB_FD | NETDEV_F_40GB_FD
1298
0
                        | NETDEV_F_100GB_FD | NETDEV_F_1TB_FD)) != 0;
1299
0
}
1300
1301
/* Set the features advertised by 'netdev' to 'advertise'.  Returns 0 if
1302
 * successful, otherwise a positive errno value. */
1303
int
1304
netdev_set_advertisements(struct netdev *netdev,
1305
                          enum netdev_features advertise)
1306
0
{
1307
0
    return (netdev->netdev_class->set_advertisements
1308
0
            ? netdev->netdev_class->set_advertisements(
1309
0
                    netdev, advertise)
1310
0
            : EOPNOTSUPP);
1311
0
}
1312
1313
static const char *
1314
netdev_feature_to_name(uint32_t bit)
1315
0
{
1316
0
    enum netdev_features f = bit;
1317
1318
0
    switch (f) {
1319
0
    case NETDEV_F_10MB_HD:    return "10MB-HD";
1320
0
    case NETDEV_F_10MB_FD:    return "10MB-FD";
1321
0
    case NETDEV_F_100MB_HD:   return "100MB-HD";
1322
0
    case NETDEV_F_100MB_FD:   return "100MB-FD";
1323
0
    case NETDEV_F_1GB_HD:     return "1GB-HD";
1324
0
    case NETDEV_F_1GB_FD:     return "1GB-FD";
1325
0
    case NETDEV_F_10GB_FD:    return "10GB-FD";
1326
0
    case NETDEV_F_40GB_FD:    return "40GB-FD";
1327
0
    case NETDEV_F_100GB_FD:   return "100GB-FD";
1328
0
    case NETDEV_F_1TB_FD:     return "1TB-FD";
1329
0
    case NETDEV_F_OTHER:      return "OTHER";
1330
0
    case NETDEV_F_COPPER:     return "COPPER";
1331
0
    case NETDEV_F_FIBER:      return "FIBER";
1332
0
    case NETDEV_F_AUTONEG:    return "AUTO_NEG";
1333
0
    case NETDEV_F_PAUSE:      return "AUTO_PAUSE";
1334
0
    case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
1335
0
    }
1336
1337
0
    return NULL;
1338
0
}
1339
1340
void
1341
netdev_features_format(struct ds *s, enum netdev_features features)
1342
0
{
1343
0
    ofp_print_bit_names(s, features, netdev_feature_to_name, ' ');
1344
0
    ds_put_char(s, '\n');
1345
0
}
1346
1347
/* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
1348
 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.  Returns a
1349
 * positive errno value. */
1350
int
1351
netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
1352
0
{
1353
0
    return (netdev->netdev_class->set_in4
1354
0
            ? netdev->netdev_class->set_in4(netdev, addr, mask)
1355
0
            : EOPNOTSUPP);
1356
0
}
1357
1358
static int
1359
netdev_get_addresses_by_name(const char *device_name,
1360
                             struct in6_addr **addrsp, int *n_addrsp)
1361
0
{
1362
0
    struct netdev *netdev;
1363
0
    int error = netdev_open(device_name, NULL, &netdev);
1364
0
    if (error) {
1365
0
        *addrsp = NULL;
1366
0
        *n_addrsp = 0;
1367
0
        return error;
1368
0
    }
1369
1370
0
    struct in6_addr *masks;
1371
0
    error = netdev_get_addr_list(netdev, addrsp, &masks, n_addrsp);
1372
0
    netdev_close(netdev);
1373
0
    free(masks);
1374
0
    return error;
1375
0
}
1376
1377
/* Obtains an IPv4 address from 'device_name' and save the address in '*in4'.
1378
 * Returns 0 if successful, otherwise a positive errno value. */
1379
int
1380
netdev_get_in4_by_name(const char *device_name, struct in_addr *in4)
1381
0
{
1382
0
    struct in6_addr *addrs;
1383
0
    int n;
1384
0
    int error = netdev_get_addresses_by_name(device_name, &addrs, &n);
1385
1386
0
    in4->s_addr = 0;
1387
0
    if (!error) {
1388
0
        error = ENOENT;
1389
0
        for (int i = 0; i < n; i++) {
1390
0
            if (IN6_IS_ADDR_V4MAPPED(&addrs[i])) {
1391
0
                in4->s_addr = in6_addr_get_mapped_ipv4(&addrs[i]);
1392
0
                error = 0;
1393
0
                break;
1394
0
            }
1395
0
        }
1396
0
    }
1397
0
    free(addrs);
1398
1399
0
    return error;
1400
0
}
1401
1402
/* Obtains an IPv4 or IPv6 address from 'device_name' and save the address in
1403
 * '*in6', representing IPv4 addresses as v6-mapped.  Returns 0 if successful,
1404
 * otherwise a positive errno value. */
1405
int
1406
netdev_get_ip_by_name(const char *device_name, struct in6_addr *in6)
1407
0
{
1408
0
    struct in6_addr *addrs;
1409
0
    int n;
1410
0
    int error = netdev_get_addresses_by_name(device_name, &addrs, &n);
1411
1412
0
    *in6 = in6addr_any;
1413
0
    if (!error) {
1414
0
        error = ENOENT;
1415
0
        for (int i = 0; i < n; i++) {
1416
0
            if (!in6_is_lla(&addrs[i])) {
1417
0
                *in6 = addrs[i];
1418
0
                error = 0;
1419
0
                break;
1420
0
            }
1421
0
        }
1422
0
    }
1423
0
    free(addrs);
1424
1425
0
    return error;
1426
0
}
1427
1428
/* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
1429
 * to 'netdev'. */
1430
int
1431
netdev_add_router(struct netdev *netdev, struct in_addr router)
1432
0
{
1433
0
    COVERAGE_INC(netdev_add_router);
1434
0
    return (netdev->netdev_class->add_router
1435
0
            ? netdev->netdev_class->add_router(netdev, router)
1436
0
            : EOPNOTSUPP);
1437
0
}
1438
1439
/* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
1440
 * 'netdev'.  If a route cannot not be determined, sets '*next_hop' to 0,
1441
 * '*netdev_name' to null, and returns a positive errno value.  Otherwise, if a
1442
 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
1443
 * a directly connected network) in '*next_hop' and a copy of the name of the
1444
 * device to reach 'host' in '*netdev_name', and returns 0.  The caller is
1445
 * responsible for freeing '*netdev_name' (by calling free()). */
1446
int
1447
netdev_get_next_hop(const struct netdev *netdev,
1448
                    const struct in_addr *host, struct in_addr *next_hop,
1449
                    char **netdev_name)
1450
0
{
1451
0
    int error = (netdev->netdev_class->get_next_hop
1452
0
                 ? netdev->netdev_class->get_next_hop(
1453
0
                        host, next_hop, netdev_name)
1454
0
                 : EOPNOTSUPP);
1455
0
    if (error) {
1456
0
        next_hop->s_addr = 0;
1457
0
        *netdev_name = NULL;
1458
0
    }
1459
0
    return error;
1460
0
}
1461
1462
/* Populates 'smap' with status information.
1463
 *
1464
 * Populates 'smap' with 'netdev' specific status information.  This
1465
 * information may be used to populate the status column of the Interface table
1466
 * as defined in ovs-vswitchd.conf.db(5). */
1467
int
1468
netdev_get_status(const struct netdev *netdev, struct smap *smap)
1469
0
{
1470
0
    int err = EOPNOTSUPP;
1471
1472
    /* Set offload status only if relevant. */
1473
0
    if (netdev_get_dpif_type(netdev) &&
1474
0
        strcmp(netdev_get_dpif_type(netdev), "system")) {
1475
1476
0
#define OL_ADD_STAT(name, bit) \
1477
0
        smap_add(smap, "tx_" name "_offload", \
1478
0
                 netdev->ol_flags & bit ? "true" : "false");
1479
1480
0
        OL_ADD_STAT("ip_csum", NETDEV_TX_OFFLOAD_IPV4_CKSUM);
1481
0
        OL_ADD_STAT("tcp_csum", NETDEV_TX_OFFLOAD_TCP_CKSUM);
1482
0
        OL_ADD_STAT("udp_csum", NETDEV_TX_OFFLOAD_UDP_CKSUM);
1483
0
        OL_ADD_STAT("sctp_csum", NETDEV_TX_OFFLOAD_SCTP_CKSUM);
1484
0
        OL_ADD_STAT("tcp_seg", NETDEV_TX_OFFLOAD_TCP_TSO);
1485
0
        OL_ADD_STAT("vxlan_tso", NETDEV_TX_VXLAN_TNL_TSO);
1486
0
        OL_ADD_STAT("gre_tso", NETDEV_TX_GRE_TNL_TSO);
1487
0
        OL_ADD_STAT("geneve_tso", NETDEV_TX_GENEVE_TNL_TSO);
1488
0
        OL_ADD_STAT("out_ip_csum", NETDEV_TX_OFFLOAD_OUTER_IP_CKSUM);
1489
0
        OL_ADD_STAT("out_udp_csum", NETDEV_TX_OFFLOAD_OUTER_UDP_CKSUM);
1490
0
#undef OL_ADD_STAT
1491
1492
0
        err = 0;
1493
0
    }
1494
1495
0
    if (!netdev->netdev_class->get_status) {
1496
0
        return err;
1497
0
    }
1498
1499
0
    return netdev->netdev_class->get_status(netdev, smap);
1500
0
}
1501
1502
/* Returns all assigned IP address to  'netdev' and returns 0.
1503
 * API allocates array of address and masks and set it to
1504
 * '*addr' and '*mask'.
1505
 * Otherwise, returns a positive errno value and sets '*addr', '*mask
1506
 * and '*n_addr' to NULL.
1507
 *
1508
 * The following error values have well-defined meanings:
1509
 *
1510
 *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
1511
 *
1512
 *   - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
1513
 *
1514
 * 'addr' may be null, in which case the address itself is not reported. */
1515
int
1516
netdev_get_addr_list(const struct netdev *netdev, struct in6_addr **addr,
1517
                     struct in6_addr **mask, int *n_addr)
1518
0
{
1519
0
    int error;
1520
1521
0
    error = (netdev->netdev_class->get_addr_list
1522
0
             ? netdev->netdev_class->get_addr_list(netdev, addr, mask, n_addr): EOPNOTSUPP);
1523
0
    if (error && addr) {
1524
0
        *addr = NULL;
1525
0
        *mask = NULL;
1526
0
        *n_addr = 0;
1527
0
    }
1528
1529
0
    return error;
1530
0
}
1531
1532
/* On 'netdev', turns off the flags in 'off' and then turns on the flags in
1533
 * 'on'.  Returns 0 if successful, otherwise a positive errno value. */
1534
static int
1535
do_update_flags(struct netdev *netdev, enum netdev_flags off,
1536
                enum netdev_flags on, enum netdev_flags *old_flagsp,
1537
                struct netdev_saved_flags **sfp)
1538
    OVS_EXCLUDED(netdev_mutex)
1539
0
{
1540
0
    struct netdev_saved_flags *sf = NULL;
1541
0
    enum netdev_flags old_flags;
1542
0
    int error;
1543
1544
0
    error = netdev->netdev_class->update_flags(netdev, off & ~on, on,
1545
0
                                               &old_flags);
1546
0
    if (error) {
1547
0
        VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
1548
0
                     off || on ? "set" : "get", netdev_get_name(netdev),
1549
0
                     ovs_strerror(error));
1550
0
        old_flags = 0;
1551
0
    } else if ((off || on) && sfp) {
1552
0
        enum netdev_flags new_flags = (old_flags & ~off) | on;
1553
0
        enum netdev_flags changed_flags = old_flags ^ new_flags;
1554
0
        if (changed_flags) {
1555
0
            ovs_mutex_lock(&netdev_mutex);
1556
0
            *sfp = sf = xmalloc(sizeof *sf);
1557
0
            sf->netdev = netdev;
1558
0
            ovs_list_push_front(&netdev->saved_flags_list, &sf->node);
1559
0
            sf->saved_flags = changed_flags;
1560
0
            sf->saved_values = changed_flags & new_flags;
1561
1562
0
            netdev->ref_cnt++;
1563
0
            ovs_mutex_unlock(&netdev_mutex);
1564
0
        }
1565
0
    }
1566
1567
0
    if (old_flagsp) {
1568
0
        *old_flagsp = old_flags;
1569
0
    }
1570
0
    if (sfp) {
1571
0
        *sfp = sf;
1572
0
    }
1573
1574
0
    return error;
1575
0
}
1576
1577
/* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
1578
 * Returns 0 if successful, otherwise a positive errno value.  On failure,
1579
 * stores 0 into '*flagsp'. */
1580
int
1581
netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
1582
0
{
1583
0
    struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
1584
0
    return do_update_flags(netdev, 0, 0, flagsp, NULL);
1585
0
}
1586
1587
/* Sets the flags for 'netdev' to 'flags'.
1588
 * Returns 0 if successful, otherwise a positive errno value. */
1589
int
1590
netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
1591
                 struct netdev_saved_flags **sfp)
1592
0
{
1593
0
    return do_update_flags(netdev, -1, flags, NULL, sfp);
1594
0
}
1595
1596
/* Turns on the specified 'flags' on 'netdev':
1597
 *
1598
 *    - On success, returns 0.  If 'sfp' is nonnull, sets '*sfp' to a newly
1599
 *      allocated 'struct netdev_saved_flags *' that may be passed to
1600
 *      netdev_restore_flags() to restore the original values of 'flags' on
1601
 *      'netdev' (this will happen automatically at program termination if
1602
 *      netdev_restore_flags() is never called) , or to NULL if no flags were
1603
 *      actually changed.
1604
 *
1605
 *    - On failure, returns a positive errno value.  If 'sfp' is nonnull, sets
1606
 *      '*sfp' to NULL. */
1607
int
1608
netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
1609
                     struct netdev_saved_flags **sfp)
1610
0
{
1611
0
    return do_update_flags(netdev, 0, flags, NULL, sfp);
1612
0
}
1613
1614
/* Turns off the specified 'flags' on 'netdev'.  See netdev_turn_flags_on() for
1615
 * details of the interface. */
1616
int
1617
netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
1618
                      struct netdev_saved_flags **sfp)
1619
0
{
1620
0
    return do_update_flags(netdev, flags, 0, NULL, sfp);
1621
0
}
1622
1623
/* Restores the flags that were saved in 'sf', and destroys 'sf'.
1624
 * Does nothing if 'sf' is NULL. */
1625
void
1626
netdev_restore_flags(struct netdev_saved_flags *sf)
1627
    OVS_EXCLUDED(netdev_mutex)
1628
0
{
1629
0
    if (sf) {
1630
0
        struct netdev *netdev = sf->netdev;
1631
0
        enum netdev_flags old_flags;
1632
1633
0
        netdev->netdev_class->update_flags(netdev,
1634
0
                                           sf->saved_flags & sf->saved_values,
1635
0
                                           sf->saved_flags & ~sf->saved_values,
1636
0
                                           &old_flags);
1637
1638
0
        ovs_mutex_lock(&netdev_mutex);
1639
0
        ovs_list_remove(&sf->node);
1640
0
        free(sf);
1641
0
        netdev_unref(netdev);
1642
0
    }
1643
0
}
1644
1645
/* Looks up the ARP table entry for 'ip' on 'netdev'.  If one exists and can be
1646
 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1647
 * returns 0.  Otherwise, it returns a positive errno value; in particular,
1648
 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
1649
int
1650
netdev_arp_lookup(const struct netdev *netdev,
1651
                  ovs_be32 ip, struct eth_addr *mac)
1652
0
{
1653
0
    int error = (netdev->netdev_class->arp_lookup
1654
0
                 ? netdev->netdev_class->arp_lookup(netdev, ip, mac)
1655
0
                 : EOPNOTSUPP);
1656
0
    if (error) {
1657
0
        *mac = eth_addr_zero;
1658
0
    }
1659
0
    return error;
1660
0
}
1661
1662
/* Returns true if carrier is active (link light is on) on 'netdev'. */
1663
bool
1664
netdev_get_carrier(const struct netdev *netdev)
1665
0
{
1666
0
    int error;
1667
0
    enum netdev_flags flags;
1668
0
    bool carrier;
1669
1670
0
    netdev_get_flags(netdev, &flags);
1671
0
    if (!(flags & NETDEV_UP)) {
1672
0
        return false;
1673
0
    }
1674
1675
0
    if (!netdev->netdev_class->get_carrier) {
1676
0
        return true;
1677
0
    }
1678
1679
0
    error = netdev->netdev_class->get_carrier(netdev, &carrier);
1680
0
    if (error) {
1681
0
        VLOG_DBG("%s: failed to get network device carrier status, assuming "
1682
0
                 "down: %s", netdev_get_name(netdev), ovs_strerror(error));
1683
0
        carrier = false;
1684
0
    }
1685
1686
0
    return carrier;
1687
0
}
1688
1689
/* Returns the number of times 'netdev''s carrier has changed. */
1690
long long int
1691
netdev_get_carrier_resets(const struct netdev *netdev)
1692
0
{
1693
0
    return (netdev->netdev_class->get_carrier_resets
1694
0
            ? netdev->netdev_class->get_carrier_resets(netdev)
1695
0
            : 0);
1696
0
}
1697
1698
/* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
1699
 * link status instead of checking 'netdev''s carrier.  'netdev''s MII
1700
 * registers will be polled once ever 'interval' milliseconds.  If 'netdev'
1701
 * does not support MII, another method may be used as a fallback.  If
1702
 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
1703
 * its normal behavior.
1704
 *
1705
 * Returns 0 if successful, otherwise a positive errno value. */
1706
int
1707
netdev_set_miimon_interval(struct netdev *netdev, long long int interval)
1708
0
{
1709
0
    return (netdev->netdev_class->set_miimon_interval
1710
0
            ? netdev->netdev_class->set_miimon_interval(netdev, interval)
1711
0
            : EOPNOTSUPP);
1712
0
}
1713
1714
/* Retrieves current device stats for 'netdev'. */
1715
int
1716
netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1717
0
{
1718
0
    int error;
1719
1720
    /* Statistics are initialized before passing it to particular device
1721
     * implementation so all values are filtered out by default. */
1722
0
    memset(stats, 0xFF, sizeof *stats);
1723
1724
0
    COVERAGE_INC(netdev_get_stats);
1725
0
    error = (netdev->netdev_class->get_stats
1726
0
             ? netdev->netdev_class->get_stats(netdev, stats)
1727
0
             : EOPNOTSUPP);
1728
0
    if (error) {
1729
        /* In case of error all statistics are filtered out */
1730
0
        memset(stats, 0xff, sizeof *stats);
1731
0
    }
1732
0
    return error;
1733
0
}
1734
1735
/* Retrieves current device custom stats for 'netdev'. */
1736
int
1737
netdev_get_custom_stats(const struct netdev *netdev,
1738
                        struct netdev_custom_stats *custom_stats)
1739
0
{
1740
0
    int error;
1741
0
    memset(custom_stats, 0, sizeof *custom_stats);
1742
0
    error = (netdev->netdev_class->get_custom_stats
1743
0
             ? netdev->netdev_class->get_custom_stats(netdev, custom_stats)
1744
0
             : EOPNOTSUPP);
1745
1746
0
    return error;
1747
0
}
1748
1749
/* Attempts to set input rate limiting (policing) policy, such that:
1750
 * - up to 'kbits_rate' kbps of traffic is accepted, with a maximum
1751
 *   accumulative burst size of 'kbits' kb; and
1752
 * - up to 'kpkts' kpps of traffic is accepted, with a maximum
1753
 *   accumulative burst size of 'kpkts' kilo packets.
1754
 */
1755
int
1756
netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1757
                    uint32_t kbits_burst, uint32_t kpkts_rate,
1758
                    uint32_t kpkts_burst)
1759
0
{
1760
0
    return (netdev->netdev_class->set_policing
1761
0
            ? netdev->netdev_class->set_policing(netdev,
1762
0
                    kbits_rate, kbits_burst, kpkts_rate, kpkts_burst)
1763
0
            : EOPNOTSUPP);
1764
0
}
1765
1766
/* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1767
 * empty if 'netdev' does not support QoS.  Any names added to 'types' should
1768
 * be documented as valid for the "type" column in the "QoS" table in
1769
 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1770
 *
1771
 * Every network device supports disabling QoS with a type of "", but this type
1772
 * will not be added to 'types'.
1773
 *
1774
 * The caller must initialize 'types' (e.g. with sset_init()) before calling
1775
 * this function.  The caller is responsible for destroying 'types' (e.g. with
1776
 * sset_destroy()) when it is no longer needed.
1777
 *
1778
 * Returns 0 if successful, otherwise a positive errno value. */
1779
int
1780
netdev_get_qos_types(const struct netdev *netdev, struct sset *types)
1781
0
{
1782
0
    const struct netdev_class *class = netdev->netdev_class;
1783
0
    return (class->get_qos_types
1784
0
            ? class->get_qos_types(netdev, types)
1785
0
            : 0);
1786
0
}
1787
1788
/* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1789
 * which should be "" or one of the types returned by netdev_get_qos_types()
1790
 * for 'netdev'.  Returns 0 if successful, otherwise a positive errno value.
1791
 * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1792
 * 'caps' to all zeros. */
1793
int
1794
netdev_get_qos_capabilities(const struct netdev *netdev, const char *type,
1795
                            struct netdev_qos_capabilities *caps)
1796
0
{
1797
0
    const struct netdev_class *class = netdev->netdev_class;
1798
1799
0
    if (*type) {
1800
0
        int retval = (class->get_qos_capabilities
1801
0
                      ? class->get_qos_capabilities(netdev, type, caps)
1802
0
                      : EOPNOTSUPP);
1803
0
        if (retval) {
1804
0
            memset(caps, 0, sizeof *caps);
1805
0
        }
1806
0
        return retval;
1807
0
    } else {
1808
        /* Every netdev supports turning off QoS. */
1809
0
        memset(caps, 0, sizeof *caps);
1810
0
        return 0;
1811
0
    }
1812
0
}
1813
1814
/* Obtains the number of queues supported by 'netdev' for the specified 'type'
1815
 * of QoS.  Returns 0 if successful, otherwise a positive errno value.  Stores
1816
 * the number of queues (zero on failure) in '*n_queuesp'.
1817
 *
1818
 * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1819
int
1820
netdev_get_n_queues(const struct netdev *netdev,
1821
                    const char *type, unsigned int *n_queuesp)
1822
0
{
1823
0
    struct netdev_qos_capabilities caps;
1824
0
    int retval;
1825
1826
0
    retval = netdev_get_qos_capabilities(netdev, type, &caps);
1827
0
    *n_queuesp = caps.n_queues;
1828
0
    return retval;
1829
0
}
1830
1831
/* Queries 'netdev' about its currently configured form of QoS.  If successful,
1832
 * stores the name of the current form of QoS into '*typep', stores any details
1833
 * of configuration as string key-value pairs in 'details', and returns 0.  On
1834
 * failure, sets '*typep' to NULL and returns a positive errno value.
1835
 *
1836
 * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1837
 *
1838
 * The caller must initialize 'details' as an empty smap (e.g. with
1839
 * smap_init()) before calling this function.  The caller must free 'details'
1840
 * when it is no longer needed (e.g. with smap_destroy()).
1841
 *
1842
 * The caller must not modify or free '*typep'.
1843
 *
1844
 * '*typep' will be one of the types returned by netdev_get_qos_types() for
1845
 * 'netdev'.  The contents of 'details' should be documented as valid for
1846
 * '*typep' in the "other_config" column in the "QoS" table in
1847
 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1848
int
1849
netdev_get_qos(const struct netdev *netdev,
1850
               const char **typep, struct smap *details)
1851
0
{
1852
0
    const struct netdev_class *class = netdev->netdev_class;
1853
0
    int retval;
1854
1855
0
    if (class->get_qos) {
1856
0
        retval = class->get_qos(netdev, typep, details);
1857
0
        if (retval) {
1858
0
            *typep = NULL;
1859
0
            smap_clear(details);
1860
0
        }
1861
0
        return retval;
1862
0
    } else {
1863
        /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1864
0
        *typep = "";
1865
0
        return 0;
1866
0
    }
1867
0
}
1868
1869
/* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1870
 * with details of configuration from 'details'.  Returns 0 if successful,
1871
 * otherwise a positive errno value.  On error, the previous QoS configuration
1872
 * is retained.
1873
 *
1874
 * When this function changes the type of QoS (not just 'details'), this also
1875
 * resets all queue configuration for 'netdev' to their defaults (which depend
1876
 * on the specific type of QoS).  Otherwise, the queue configuration for
1877
 * 'netdev' is unchanged.
1878
 *
1879
 * 'type' should be "" (to disable QoS) or one of the types returned by
1880
 * netdev_get_qos_types() for 'netdev'.  The contents of 'details' should be
1881
 * documented as valid for the given 'type' in the "other_config" column in the
1882
 * "QoS" table in vswitchd/vswitch.xml (which is built as
1883
 * ovs-vswitchd.conf.db(8)).
1884
 *
1885
 * NULL may be specified for 'details' if there are no configuration
1886
 * details. */
1887
int
1888
netdev_set_qos(struct netdev *netdev,
1889
               const char *type, const struct smap *details)
1890
0
{
1891
0
    const struct netdev_class *class = netdev->netdev_class;
1892
1893
0
    if (!type) {
1894
0
        type = "";
1895
0
    }
1896
1897
0
    if (class->set_qos) {
1898
0
        if (!details) {
1899
0
            static const struct smap empty = SMAP_INITIALIZER(&empty);
1900
0
            details = &empty;
1901
0
        }
1902
0
        return class->set_qos(netdev, type, details);
1903
0
    } else {
1904
0
        return *type ? EOPNOTSUPP : 0;
1905
0
    }
1906
0
}
1907
1908
/* Queries 'netdev' for information about the queue numbered 'queue_id'.  If
1909
 * successful, adds that information as string key-value pairs to 'details'.
1910
 * Returns 0 if successful, otherwise a positive errno value.
1911
 *
1912
 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1913
 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1914
 *
1915
 * The returned contents of 'details' should be documented as valid for the
1916
 * given 'type' in the "other_config" column in the "Queue" table in
1917
 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1918
 *
1919
 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1920
 * this function.  The caller must free 'details' when it is no longer needed
1921
 * (e.g. with smap_destroy()). */
1922
int
1923
netdev_get_queue(const struct netdev *netdev,
1924
                 unsigned int queue_id, struct smap *details)
1925
0
{
1926
0
    const struct netdev_class *class = netdev->netdev_class;
1927
0
    int retval;
1928
1929
0
    retval = (class->get_queue
1930
0
              ? class->get_queue(netdev, queue_id, details)
1931
0
              : EOPNOTSUPP);
1932
0
    if (retval) {
1933
0
        smap_clear(details);
1934
0
    }
1935
0
    return retval;
1936
0
}
1937
1938
/* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1939
 * string pairs in 'details'.  The contents of 'details' should be documented
1940
 * as valid for the given 'type' in the "other_config" column in the "Queue"
1941
 * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1942
 * Returns 0 if successful, otherwise a positive errno value.  On failure, the
1943
 * given queue's configuration should be unmodified.
1944
 *
1945
 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1946
 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1947
 *
1948
 * This function does not modify 'details', and the caller retains ownership of
1949
 * it. */
1950
int
1951
netdev_set_queue(struct netdev *netdev,
1952
                 unsigned int queue_id, const struct smap *details)
1953
0
{
1954
0
    const struct netdev_class *class = netdev->netdev_class;
1955
0
    return (class->set_queue
1956
0
            ? class->set_queue(netdev, queue_id, details)
1957
0
            : EOPNOTSUPP);
1958
0
}
1959
1960
/* Attempts to delete the queue numbered 'queue_id' from 'netdev'.  Some kinds
1961
 * of QoS may have a fixed set of queues, in which case attempts to delete them
1962
 * will fail with EOPNOTSUPP.
1963
 *
1964
 * Returns 0 if successful, otherwise a positive errno value.  On failure, the
1965
 * given queue will be unmodified.
1966
 *
1967
 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1968
 * the current form of QoS (e.g. as returned by
1969
 * netdev_get_n_queues(netdev)). */
1970
int
1971
netdev_delete_queue(struct netdev *netdev, unsigned int queue_id)
1972
0
{
1973
0
    const struct netdev_class *class = netdev->netdev_class;
1974
0
    return (class->delete_queue
1975
0
            ? class->delete_queue(netdev, queue_id)
1976
0
            : EOPNOTSUPP);
1977
0
}
1978
1979
/* Obtains statistics about 'queue_id' on 'netdev'.  On success, returns 0 and
1980
 * fills 'stats' with the queue's statistics; individual members of 'stats' may
1981
 * be set to all-1-bits if the statistic is unavailable.  On failure, returns a
1982
 * positive errno value and fills 'stats' with values indicating unsupported
1983
 * statistics. */
1984
int
1985
netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
1986
                       struct netdev_queue_stats *stats)
1987
0
{
1988
0
    const struct netdev_class *class = netdev->netdev_class;
1989
0
    int retval;
1990
1991
0
    retval = (class->get_queue_stats
1992
0
              ? class->get_queue_stats(netdev, queue_id, stats)
1993
0
              : EOPNOTSUPP);
1994
0
    if (retval) {
1995
0
        stats->tx_bytes = UINT64_MAX;
1996
0
        stats->tx_packets = UINT64_MAX;
1997
0
        stats->tx_errors = UINT64_MAX;
1998
0
        stats->created = LLONG_MIN;
1999
0
    }
2000
0
    return retval;
2001
0
}
2002
2003
/* Initializes 'dump' to begin dumping the queues in a netdev.
2004
 *
2005
 * This function provides no status indication.  An error status for the entire
2006
 * dump operation is provided when it is completed by calling
2007
 * netdev_queue_dump_done().
2008
 */
2009
void
2010
netdev_queue_dump_start(struct netdev_queue_dump *dump,
2011
                        const struct netdev *netdev)
2012
0
{
2013
0
    dump->netdev = netdev_ref(netdev);
2014
0
    if (netdev->netdev_class->queue_dump_start) {
2015
0
        dump->error = netdev->netdev_class->queue_dump_start(netdev,
2016
0
                                                             &dump->state);
2017
0
    } else {
2018
0
        dump->error = EOPNOTSUPP;
2019
0
    }
2020
0
}
2021
2022
/* Attempts to retrieve another queue from 'dump', which must have been
2023
 * initialized with netdev_queue_dump_start().  On success, stores a new queue
2024
 * ID into '*queue_id', fills 'details' with configuration details for the
2025
 * queue, and returns true.  On failure, returns false.
2026
 *
2027
 * Queues are not necessarily dumped in increasing order of queue ID (or any
2028
 * other predictable order).
2029
 *
2030
 * Failure might indicate an actual error or merely that the last queue has
2031
 * been dumped.  An error status for the entire dump operation is provided when
2032
 * it is completed by calling netdev_queue_dump_done().
2033
 *
2034
 * The returned contents of 'details' should be documented as valid for the
2035
 * given 'type' in the "other_config" column in the "Queue" table in
2036
 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
2037
 *
2038
 * The caller must initialize 'details' (e.g. with smap_init()) before calling
2039
 * this function.  This function will clear and replace its contents.  The
2040
 * caller must free 'details' when it is no longer needed (e.g. with
2041
 * smap_destroy()). */
2042
bool
2043
netdev_queue_dump_next(struct netdev_queue_dump *dump,
2044
                       unsigned int *queue_id, struct smap *details)
2045
0
{
2046
0
    smap_clear(details);
2047
2048
0
    const struct netdev *netdev = dump->netdev;
2049
0
    if (dump->error) {
2050
0
        return false;
2051
0
    }
2052
2053
0
    dump->error = netdev->netdev_class->queue_dump_next(netdev, dump->state,
2054
0
                                                        queue_id, details);
2055
2056
0
    if (dump->error) {
2057
0
        netdev->netdev_class->queue_dump_done(netdev, dump->state);
2058
0
        return false;
2059
0
    }
2060
0
    return true;
2061
0
}
2062
2063
/* Completes queue table dump operation 'dump', which must have been
2064
 * initialized with netdev_queue_dump_start().  Returns 0 if the dump operation
2065
 * was error-free, otherwise a positive errno value describing the problem. */
2066
int
2067
netdev_queue_dump_done(struct netdev_queue_dump *dump)
2068
0
{
2069
0
    const struct netdev *netdev = dump->netdev;
2070
0
    if (!dump->error && netdev->netdev_class->queue_dump_done) {
2071
0
        dump->error = netdev->netdev_class->queue_dump_done(netdev,
2072
0
                                                            dump->state);
2073
0
    }
2074
0
    netdev_close(dump->netdev);
2075
0
    return dump->error == EOF ? 0 : dump->error;
2076
0
}
2077
2078
/* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
2079
 * its statistics, and the 'aux' specified by the caller.  The order of
2080
 * iteration is unspecified, but (when successful) each queue is visited
2081
 * exactly once.
2082
 *
2083
 * Calling this function may be more efficient than calling
2084
 * netdev_get_queue_stats() for every queue.
2085
 *
2086
 * 'cb' must not modify or free the statistics passed in.
2087
 *
2088
 * Returns 0 if successful, otherwise a positive errno value.  On error, some
2089
 * configured queues may not have been included in the iteration. */
2090
int
2091
netdev_dump_queue_stats(const struct netdev *netdev,
2092
                        netdev_dump_queue_stats_cb *cb, void *aux)
2093
0
{
2094
0
    const struct netdev_class *class = netdev->netdev_class;
2095
0
    return (class->dump_queue_stats
2096
0
            ? class->dump_queue_stats(netdev, cb, aux)
2097
0
            : EOPNOTSUPP);
2098
0
}
2099
2100

2101
/* Returns the class type of 'netdev'.
2102
 *
2103
 * The caller must not free the returned value. */
2104
const char *
2105
netdev_get_type(const struct netdev *netdev)
2106
0
{
2107
0
    return netdev->netdev_class->type;
2108
0
}
2109
2110
/* Returns the class associated with 'netdev'. */
2111
const struct netdev_class *
2112
netdev_get_class(const struct netdev *netdev)
2113
0
{
2114
0
    return netdev->netdev_class;
2115
0
}
2116
2117
/* Set the type of 'dpif' this 'netdev' belongs to. */
2118
void
2119
netdev_set_dpif_type(struct netdev *netdev, const char *type)
2120
0
{
2121
0
    netdev->dpif_type = type;
2122
0
}
2123
2124
/* Returns the type of 'dpif' this 'netdev' belongs to.
2125
 *
2126
 * The caller must not free the returned value. */
2127
const char *
2128
netdev_get_dpif_type(const struct netdev *netdev)
2129
0
{
2130
0
    return netdev->dpif_type;
2131
0
}
2132
2133
/* Returns the netdev with 'name' or NULL if there is none.
2134
 *
2135
 * The caller must free the returned netdev with netdev_close(). */
2136
struct netdev *
2137
netdev_from_name(const char *name)
2138
    OVS_EXCLUDED(netdev_mutex)
2139
0
{
2140
0
    struct netdev *netdev;
2141
2142
0
    ovs_mutex_lock(&netdev_mutex);
2143
0
    netdev = shash_find_data(&netdev_shash, name);
2144
0
    if (netdev) {
2145
0
        netdev->ref_cnt++;
2146
0
    }
2147
0
    ovs_mutex_unlock(&netdev_mutex);
2148
2149
0
    return netdev;
2150
0
}
2151
2152
/* Fills 'device_list' with devices that match 'netdev_class'.
2153
 *
2154
 * The caller is responsible for initializing and destroying 'device_list' and
2155
 * must close each device on the list. */
2156
void
2157
netdev_get_devices(const struct netdev_class *netdev_class,
2158
                   struct shash *device_list)
2159
    OVS_EXCLUDED(netdev_mutex)
2160
0
{
2161
0
    struct shash_node *node;
2162
2163
0
    ovs_mutex_lock(&netdev_mutex);
2164
0
    SHASH_FOR_EACH (node, &netdev_shash) {
2165
0
        struct netdev *dev = node->data;
2166
2167
0
        if (dev->netdev_class == netdev_class) {
2168
0
            dev->ref_cnt++;
2169
0
            shash_add(device_list, node->name, node->data);
2170
0
        }
2171
0
    }
2172
0
    ovs_mutex_unlock(&netdev_mutex);
2173
0
}
2174
2175
/* Extracts pointers to all 'netdev-vports' into an array 'vports'
2176
 * and returns it.  Stores the size of the array into '*size'.
2177
 *
2178
 * The caller is responsible for freeing 'vports' and must close
2179
 * each 'netdev-vport' in the list. */
2180
struct netdev **
2181
netdev_get_vports(size_t *size)
2182
    OVS_EXCLUDED(netdev_mutex)
2183
0
{
2184
0
    struct netdev **vports;
2185
0
    struct shash_node *node;
2186
0
    size_t n = 0;
2187
2188
0
    if (!size) {
2189
0
        return NULL;
2190
0
    }
2191
2192
    /* Explicitly allocates big enough chunk of memory. */
2193
0
    ovs_mutex_lock(&netdev_mutex);
2194
0
    vports = xmalloc(shash_count(&netdev_shash) * sizeof *vports);
2195
0
    SHASH_FOR_EACH (node, &netdev_shash) {
2196
0
        struct netdev *dev = node->data;
2197
2198
0
        if (netdev_vport_is_vport_class(dev->netdev_class)) {
2199
0
            dev->ref_cnt++;
2200
0
            vports[n] = dev;
2201
0
            n++;
2202
0
        }
2203
0
    }
2204
0
    ovs_mutex_unlock(&netdev_mutex);
2205
0
    *size = n;
2206
2207
0
    return vports;
2208
0
}
2209
2210
const char *
2211
netdev_get_type_from_name(const char *name)
2212
0
{
2213
0
    struct netdev *dev;
2214
0
    const char *type;
2215
0
    type = netdev_vport_type_from_name(name);
2216
0
    if (type == NULL) {
2217
0
        dev = netdev_from_name(name);
2218
0
        type = dev ? netdev_get_type(dev) : NULL;
2219
0
        netdev_close(dev);
2220
0
    }
2221
0
    return type;
2222
0
}
2223

2224
struct netdev *
2225
netdev_rxq_get_netdev(const struct netdev_rxq *rx)
2226
0
{
2227
0
    ovs_assert(rx->netdev->ref_cnt > 0);
2228
0
    return rx->netdev;
2229
0
}
2230
2231
const char *
2232
netdev_rxq_get_name(const struct netdev_rxq *rx)
2233
0
{
2234
0
    return netdev_get_name(netdev_rxq_get_netdev(rx));
2235
0
}
2236
2237
int
2238
netdev_rxq_get_queue_id(const struct netdev_rxq *rx)
2239
0
{
2240
0
    return rx->queue_id;
2241
0
}
2242
2243
static void
2244
restore_all_flags(void *aux OVS_UNUSED)
2245
0
{
2246
0
    struct shash_node *node;
2247
2248
0
    SHASH_FOR_EACH (node, &netdev_shash) {
2249
0
        struct netdev *netdev = node->data;
2250
0
        const struct netdev_saved_flags *sf;
2251
0
        enum netdev_flags saved_values;
2252
0
        enum netdev_flags saved_flags;
2253
2254
0
        saved_values = saved_flags = 0;
2255
0
        LIST_FOR_EACH (sf, node, &netdev->saved_flags_list) {
2256
0
            saved_flags |= sf->saved_flags;
2257
0
            saved_values &= ~sf->saved_flags;
2258
0
            saved_values |= sf->saved_flags & sf->saved_values;
2259
0
        }
2260
0
        if (saved_flags) {
2261
0
            enum netdev_flags old_flags;
2262
2263
0
            netdev->netdev_class->update_flags(netdev,
2264
0
                                               saved_flags & saved_values,
2265
0
                                               saved_flags & ~saved_values,
2266
0
                                               &old_flags);
2267
0
        }
2268
#ifdef HAVE_AF_XDP
2269
        if (netdev->netdev_class == &netdev_afxdp_class) {
2270
            signal_remove_xdp(netdev);
2271
        }
2272
#endif
2273
0
    }
2274
0
}
2275
2276
uint64_t
2277
netdev_get_change_seq(const struct netdev *netdev)
2278
0
{
2279
0
    uint64_t change_seq;
2280
2281
0
    atomic_read_explicit(&CONST_CAST(struct netdev *, netdev)->change_seq,
2282
0
                        &change_seq, memory_order_acquire);
2283
2284
0
    return change_seq;
2285
0
}
2286
2287
#ifndef _WIN32
2288
/* This implementation is shared by Linux and BSD. */
2289
2290
static struct ifaddrs *if_addr_list;
2291
static struct ovs_mutex if_addr_list_lock = OVS_MUTEX_INITIALIZER;
2292
2293
void
2294
netdev_get_addrs_list_flush(void)
2295
0
{
2296
0
    ovs_mutex_lock(&if_addr_list_lock);
2297
0
    if (if_addr_list) {
2298
0
        freeifaddrs(if_addr_list);
2299
0
        if_addr_list = NULL;
2300
0
    }
2301
0
    ovs_mutex_unlock(&if_addr_list_lock);
2302
0
}
2303
2304
int
2305
netdev_get_addrs(const char dev[], struct in6_addr **paddr,
2306
                 struct in6_addr **pmask, int *n_in)
2307
0
{
2308
0
    struct in6_addr *addr_array, *mask_array;
2309
0
    const struct ifaddrs *ifa;
2310
0
    int cnt = 0, i = 0;
2311
0
    int retries = 3;
2312
2313
0
    ovs_mutex_lock(&if_addr_list_lock);
2314
0
    if (!if_addr_list) {
2315
0
        int err;
2316
2317
0
retry:
2318
0
        err = getifaddrs(&if_addr_list);
2319
0
        if (err) {
2320
0
            ovs_mutex_unlock(&if_addr_list_lock);
2321
0
            return -err;
2322
0
        }
2323
0
        retries--;
2324
0
    }
2325
2326
0
    for (ifa = if_addr_list; ifa; ifa = ifa->ifa_next) {
2327
0
        if (!ifa->ifa_name) {
2328
0
            if (retries) {
2329
                /* Older versions of glibc have a bug on race condition with
2330
                 * address addition which may cause one of the returned
2331
                 * ifa_name values to be NULL. In such case, we know that we've
2332
                 * got an inconsistent dump. Retry but beware of an endless
2333
                 * loop. From glibc 2.28 and beyond, this workaround is not
2334
                 * needed and should be eventually removed. */
2335
0
                freeifaddrs(if_addr_list);
2336
0
                goto retry;
2337
0
            } else {
2338
0
                VLOG_WARN("Proceeding with an inconsistent dump of "
2339
0
                          "interfaces from the kernel. Some may be missing");
2340
0
            }
2341
0
        }
2342
0
        if (ifa->ifa_addr && ifa->ifa_name && ifa->ifa_netmask) {
2343
0
            int family;
2344
2345
0
            family = ifa->ifa_addr->sa_family;
2346
0
            if (family == AF_INET || family == AF_INET6) {
2347
0
                if (!strncmp(ifa->ifa_name, dev, IFNAMSIZ)) {
2348
0
                    cnt++;
2349
0
                }
2350
0
            }
2351
0
        }
2352
0
    }
2353
2354
0
    if (!cnt) {
2355
0
        ovs_mutex_unlock(&if_addr_list_lock);
2356
0
        return EADDRNOTAVAIL;
2357
0
    }
2358
0
    addr_array = xzalloc(sizeof *addr_array * cnt);
2359
0
    mask_array = xzalloc(sizeof *mask_array * cnt);
2360
0
    for (ifa = if_addr_list; ifa; ifa = ifa->ifa_next) {
2361
0
        if (ifa->ifa_name
2362
0
            && ifa->ifa_addr
2363
0
            && ifa->ifa_netmask
2364
0
            && !strncmp(ifa->ifa_name, dev, IFNAMSIZ)
2365
0
            && sa_is_ip(ifa->ifa_addr)) {
2366
0
            addr_array[i] = sa_get_address(ifa->ifa_addr);
2367
0
            mask_array[i] = sa_get_address(ifa->ifa_netmask);
2368
0
            i++;
2369
0
        }
2370
0
    }
2371
0
    ovs_mutex_unlock(&if_addr_list_lock);
2372
0
    if (paddr) {
2373
0
        *n_in = cnt;
2374
0
        *paddr = addr_array;
2375
0
        *pmask = mask_array;
2376
0
    } else {
2377
0
        free(addr_array);
2378
0
        free(mask_array);
2379
0
    }
2380
0
    return 0;
2381
0
}
2382
#endif
2383
2384
void
2385
netdev_wait_reconf_required(struct netdev *netdev)
2386
0
{
2387
0
    seq_wait(netdev->reconfigure_seq, netdev->last_reconfigure_seq);
2388
0
}
2389
2390
bool
2391
netdev_is_reconf_required(struct netdev *netdev)
2392
0
{
2393
0
    return seq_read(netdev->reconfigure_seq) != netdev->last_reconfigure_seq;
2394
0
}
2395
2396
/* Give a chance to 'netdev' to reconfigure some of its parameters.
2397
 *
2398
 * If a module uses netdev_send() and netdev_rxq_recv(), it must call this
2399
 * function when netdev_is_reconf_required() returns true.
2400
 *
2401
 * Return 0 if successful, otherwise a positive errno value.  If the
2402
 * reconfiguration fails the netdev will not be able to send or receive
2403
 * packets.
2404
 *
2405
 * When this function is called, no call to netdev_rxq_recv() or netdev_send()
2406
 * must be issued. */
2407
int
2408
netdev_reconfigure(struct netdev *netdev)
2409
0
{
2410
0
    const struct netdev_class *class = netdev->netdev_class;
2411
2412
0
    netdev->last_reconfigure_seq = seq_read(netdev->reconfigure_seq);
2413
2414
0
    return (class->reconfigure
2415
0
            ? class->reconfigure(netdev)
2416
0
            : EOPNOTSUPP);
2417
0
}
2418
2419
void
2420
netdev_free_custom_stats_counters(struct netdev_custom_stats *custom_stats)
2421
0
{
2422
0
    if (custom_stats) {
2423
0
        if (custom_stats->counters) {
2424
0
            free(custom_stats->counters);
2425
0
            custom_stats->counters = NULL;
2426
0
            custom_stats->size = 0;
2427
0
        }
2428
0
    }
2429
0
}