Coverage Report

Created: 2026-05-30 06:26

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