Coverage Report

Created: 2026-03-02 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/netdev-provider.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2016 Nicira, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at:
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#ifndef NETDEV_PROVIDER_H
18
#define NETDEV_PROVIDER_H 1
19
20
/* Generic interface to network devices. */
21
22
#include "connectivity.h"
23
#include "netdev.h"
24
#include "openvswitch/list.h"
25
#include "ovs-numa.h"
26
#include "ovs-rcu.h"
27
#include "packets.h"
28
#include "seq.h"
29
#include "openvswitch/shash.h"
30
#include "smap.h"
31
32
#ifdef  __cplusplus
33
extern "C" {
34
#endif
35
36
struct netdev_tnl_build_header_params;
37
0
#define NETDEV_NUMA_UNSPEC OVS_NUMA_UNSPEC
38
39
enum netdev_ol_flags {
40
    NETDEV_TX_OFFLOAD_IPV4_CKSUM = 1 << 0,
41
    NETDEV_TX_OFFLOAD_TCP_CKSUM = 1 << 1,
42
    NETDEV_TX_OFFLOAD_UDP_CKSUM = 1 << 2,
43
    NETDEV_TX_OFFLOAD_SCTP_CKSUM = 1 << 3,
44
    NETDEV_TX_OFFLOAD_TCP_TSO = 1 << 4,
45
    NETDEV_TX_VXLAN_TNL_TSO = 1 << 5,
46
    NETDEV_TX_GENEVE_TNL_TSO = 1 << 6,
47
    NETDEV_TX_OFFLOAD_OUTER_IP_CKSUM = 1 << 7,
48
    NETDEV_TX_OFFLOAD_OUTER_UDP_CKSUM = 1 << 8,
49
    NETDEV_TX_GRE_TNL_TSO = 1 << 9,
50
};
51
52
/* A network device (e.g. an Ethernet device).
53
 *
54
 * Network device implementations may read these members but should not modify
55
 * them. */
56
struct netdev {
57
    /* The following do not change during the lifetime of a struct netdev. */
58
    char *name;                         /* Name of network device. */
59
    const struct netdev_class *netdev_class; /* Functions to control
60
                                                this device. */
61
62
    /* If this is 'true' the user did not specify a netdev_class when
63
     * opening this device, and therefore got assigned to the "system" class */
64
    bool auto_classified;
65
66
    /* This bitmask of the offloading features enabled by the netdev. */
67
    uint64_t ol_flags;
68
69
    /* If this is 'true', the user explicitly specified an MTU for this
70
     * netdev.  Otherwise, Open vSwitch is allowed to override it. */
71
    bool mtu_user_config;
72
73
    int ref_cnt;                        /* Times this devices was opened. */
74
75
    /* A sequence number which indicates changes in one of 'netdev''s
76
     * properties.   It must be nonzero so that users have a value which
77
     * they may use as a reset when tracking 'netdev'.
78
     *
79
     * Minimally, the sequence number is required to change whenever
80
     * 'netdev''s flags, features, ethernet address, or carrier changes. */
81
    atomic_uint64_t change_seq;
82
83
    /* A netdev provider might be unable to change some of the device's
84
     * parameter (n_rxq, mtu) when the device is in use.  In this case
85
     * the provider can notify the upper layer by calling
86
     * netdev_request_reconfigure().  The upper layer will react by stopping
87
     * the operations on the device and calling netdev_reconfigure() to allow
88
     * the configuration changes.  'last_reconfigure_seq' remembers the value
89
     * of 'reconfigure_seq' when the last reconfiguration happened. */
90
    struct seq *reconfigure_seq;
91
    uint64_t last_reconfigure_seq;
92
93
    /* The core netdev code initializes these at netdev construction and only
94
     * provide read-only access to its client.  Netdev implementations may
95
     * modify them. */
96
    int n_txq;
97
    int n_rxq;
98
    struct shash_node *node;            /* Pointer to element in global map. */
99
    struct ovs_list saved_flags_list; /* Contains "struct netdev_saved_flags". */
100
101
    /* The type of dpif this netdev is associated with.  This is set once
102
     * when the netdev is added to a specific dpif. */
103
    const char *dpif_type;
104
105
    /* Data used for controlling flow offloading via dpif-offload. */
106
    OVSRCU_TYPE(const struct dpif_offload *) dpif_offload;
107
    struct netdev_hw_info hw_info;  /* Offload-capable netdev info. */
108
};
109
110
static inline void
111
netdev_change_seq_changed(const struct netdev *netdev_)
112
0
{
113
0
    uint64_t change_seq;
114
0
    struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
115
0
    seq_change(connectivity_seq_get());
116
117
0
    atomic_read_relaxed(&netdev->change_seq, &change_seq);
118
0
    change_seq++;
119
0
    if (OVS_UNLIKELY(!change_seq)) {
120
0
        change_seq++;
121
0
    }
122
0
    atomic_store_explicit(&netdev->change_seq, change_seq,
123
0
                          memory_order_release);
124
0
}
Unexecuted instantiation: dp-packet.c:netdev_change_seq_changed
Unexecuted instantiation: dp-packet-gso.c:netdev_change_seq_changed
Unexecuted instantiation: flow.c:netdev_change_seq_changed
Unexecuted instantiation: netdev.c:netdev_change_seq_changed
Unexecuted instantiation: netdev-linux.c:netdev_change_seq_changed
Unexecuted instantiation: dpif-offload.c:netdev_change_seq_changed
Unexecuted instantiation: dpif-offload-dummy.c:netdev_change_seq_changed
Unexecuted instantiation: dpif.c:netdev_change_seq_changed
Unexecuted instantiation: netdev-dummy.c:netdev_change_seq_changed
Unexecuted instantiation: netdev-vport.c:netdev_change_seq_changed
Unexecuted instantiation: netdev-native-tnl.c:netdev_change_seq_changed
Unexecuted instantiation: dpif-netlink.c:netdev_change_seq_changed
Unexecuted instantiation: dpif-offload-tc.c:netdev_change_seq_changed
Unexecuted instantiation: dpif-offload-tc-netdev.c:netdev_change_seq_changed
Unexecuted instantiation: dpif-netdev.c:netdev_change_seq_changed
125
126
static inline void
127
netdev_request_reconfigure(struct netdev *netdev)
128
0
{
129
0
    seq_change(netdev->reconfigure_seq);
130
0
}
Unexecuted instantiation: dp-packet.c:netdev_request_reconfigure
Unexecuted instantiation: dp-packet-gso.c:netdev_request_reconfigure
Unexecuted instantiation: flow.c:netdev_request_reconfigure
Unexecuted instantiation: netdev.c:netdev_request_reconfigure
Unexecuted instantiation: netdev-linux.c:netdev_request_reconfigure
Unexecuted instantiation: dpif-offload.c:netdev_request_reconfigure
Unexecuted instantiation: dpif-offload-dummy.c:netdev_request_reconfigure
Unexecuted instantiation: dpif.c:netdev_request_reconfigure
Unexecuted instantiation: netdev-dummy.c:netdev_request_reconfigure
Unexecuted instantiation: netdev-vport.c:netdev_request_reconfigure
Unexecuted instantiation: netdev-native-tnl.c:netdev_request_reconfigure
Unexecuted instantiation: dpif-netlink.c:netdev_request_reconfigure
Unexecuted instantiation: dpif-offload-tc.c:netdev_request_reconfigure
Unexecuted instantiation: dpif-offload-tc-netdev.c:netdev_request_reconfigure
Unexecuted instantiation: dpif-netdev.c:netdev_request_reconfigure
131
132
const char *netdev_get_type(const struct netdev *);
133
const struct netdev_class *netdev_get_class(const struct netdev *);
134
const char *netdev_get_name(const struct netdev *);
135
struct netdev *netdev_from_name(const char *name);
136
void netdev_get_devices(const struct netdev_class *,
137
                        struct shash *device_list);
138
struct netdev **netdev_get_vports(size_t *size);
139
140
/* A data structure for capturing packets received by a network device.
141
 *
142
 * Network device implementations may read these members but should not modify
143
 * them.
144
 *
145
 * None of these members change during the lifetime of a struct netdev_rxq. */
146
struct netdev_rxq {
147
    struct netdev *netdev;      /* Owns a reference to the netdev. */
148
    int queue_id;
149
};
150
151
struct netdev *netdev_rxq_get_netdev(const struct netdev_rxq *);
152
153
154
/* Network device class structure, to be defined by each implementation of a
155
 * network device.
156
 *
157
 * These functions return 0 if successful or a positive errno value on failure,
158
 * except where otherwise noted.
159
 *
160
 *
161
 * Data Structures
162
 * ===============
163
 *
164
 * These functions work primarily with two different kinds of data structures:
165
 *
166
 *   - "struct netdev", which represents a network device.
167
 *
168
 *   - "struct netdev_rxq", which represents a handle for capturing packets
169
 *     received on a network device
170
 *
171
 * Each of these data structures contains all of the implementation-independent
172
 * generic state for the respective concept, called the "base" state.  None of
173
 * them contains any extra space for implementations to use.  Instead, each
174
 * implementation is expected to declare its own data structure that contains
175
 * an instance of the generic data structure plus additional
176
 * implementation-specific members, called the "derived" state.  The
177
 * implementation can use casts or (preferably) the CONTAINER_OF macro to
178
 * obtain access to derived state given only a pointer to the embedded generic
179
 * data structure.
180
 *
181
 *
182
 * Life Cycle
183
 * ==========
184
 *
185
 * Four stylized functions accompany each of these data structures:
186
 *
187
 *            "alloc"          "construct"        "destruct"       "dealloc"
188
 *            ------------   ----------------  ---------------  --------------
189
 * netdev      ->alloc        ->construct        ->destruct        ->dealloc
190
 * netdev_rxq  ->rxq_alloc    ->rxq_construct    ->rxq_destruct    ->rxq_dealloc
191
 *
192
 * Any instance of a given data structure goes through the following life
193
 * cycle:
194
 *
195
 *   1. The client calls the "alloc" function to obtain raw memory.  If "alloc"
196
 *      fails, skip all the other steps.
197
 *
198
 *   2. The client initializes all of the data structure's base state.  If this
199
 *      fails, skip to step 7.
200
 *
201
 *   3. The client calls the "construct" function.  The implementation
202
 *      initializes derived state.  It may refer to the already-initialized
203
 *      base state.  If "construct" fails, skip to step 6.
204
 *
205
 *   4. The data structure is now initialized and in use.
206
 *
207
 *   5. When the data structure is no longer needed, the client calls the
208
 *      "destruct" function.  The implementation uninitializes derived state.
209
 *      The base state has not been uninitialized yet, so the implementation
210
 *      may still refer to it.
211
 *
212
 *   6. The client uninitializes all of the data structure's base state.
213
 *
214
 *   7. The client calls the "dealloc" to free the raw memory.  The
215
 *      implementation must not refer to base or derived state in the data
216
 *      structure, because it has already been uninitialized.
217
 *
218
 * If netdev support multi-queue IO then netdev->construct should set initialize
219
 * netdev->n_rxq to number of queues.
220
 *
221
 * Each "alloc" function allocates and returns a new instance of the respective
222
 * data structure.  The "alloc" function is not given any information about the
223
 * use of the new data structure, so it cannot perform much initialization.
224
 * Its purpose is just to ensure that the new data structure has enough room
225
 * for base and derived state.  It may return a null pointer if memory is not
226
 * available, in which case none of the other functions is called.
227
 *
228
 * Each "construct" function initializes derived state in its respective data
229
 * structure.  When "construct" is called, all of the base state has already
230
 * been initialized, so the "construct" function may refer to it.  The
231
 * "construct" function is allowed to fail, in which case the client calls the
232
 * "dealloc" function (but not the "destruct" function).
233
 *
234
 * Each "destruct" function uninitializes and frees derived state in its
235
 * respective data structure.  When "destruct" is called, the base state has
236
 * not yet been uninitialized, so the "destruct" function may refer to it.  The
237
 * "destruct" function is not allowed to fail.
238
 *
239
 * Each "dealloc" function frees raw memory that was allocated by the
240
 * "alloc" function.  The memory's base and derived members might not have ever
241
 * been initialized (but if "construct" returned successfully, then it has been
242
 * "destruct"ed already).  The "dealloc" function is not allowed to fail.
243
 *
244
 *
245
 * Device Change Notification
246
 * ==========================
247
 *
248
 * Minimally, implementations are required to report changes to netdev flags,
249
 * features, ethernet address or carrier through connectivity_seq. Changes to
250
 * other properties are allowed to cause notification through this interface,
251
 * although implementations should try to avoid this. connectivity_seq_get()
252
 * can be used to acquire a reference to the struct seq. The interface is
253
 * described in detail in seq.h. */
254
struct netdev_class {
255
    /* Type of netdevs in this class, e.g. "system", "tap", "gre", etc.
256
     *
257
     * One of the providers should supply a "system" type, since this is
258
     * the type assumed if no type is specified when opening a netdev.
259
     * The "system" type corresponds to an existing network device on
260
     * the system. */
261
    const char *type;
262
263
    /* If 'true' then this netdev should be polled by PMD threads. */
264
    bool is_pmd;
265
266
/* ## ------------------- ## */
267
/* ## Top-Level Functions ## */
268
/* ## ------------------- ## */
269
270
    /* Called when the netdev provider is registered, typically at program
271
     * startup.  Returning an error from this function will prevent any network
272
     * device in this class from being opened.
273
     *
274
     * This function may be set to null if a network device class needs no
275
     * initialization at registration time. */
276
    int (*init)(void);
277
278
    /* Performs periodic work needed by netdevs of this class.  May be null if
279
     * no periodic work is necessary.
280
     *
281
     * 'netdev_class' points to the class.  It is useful in case the same
282
     * function is used to implement different classes. */
283
    void (*run)(const struct netdev_class *netdev_class);
284
285
    /* Arranges for poll_block() to wake up if the "run" member function needs
286
     * to be called.  Implementations are additionally required to wake
287
     * whenever something changes in any of its netdevs which would cause their
288
     * ->change_seq() function to change its result.  May be null if nothing is
289
     * needed here.
290
     *
291
     * 'netdev_class' points to the class.  It is useful in case the same
292
     * function is used to implement different classes. */
293
    void (*wait)(const struct netdev_class *netdev_class);
294
295
/* ## ---------------- ## */
296
/* ## netdev Functions ## */
297
/* ## ---------------- ## */
298
299
    /* Life-cycle functions for a netdev.  See the large comment above on
300
     * struct netdev_class. */
301
    struct netdev *(*alloc)(void);
302
    int (*construct)(struct netdev *);
303
    void (*destruct)(struct netdev *);
304
    void (*dealloc)(struct netdev *);
305
306
    /* Fetches the device 'netdev''s configuration, storing it in 'args'.
307
     * The caller owns 'args' and pre-initializes it to an empty smap.
308
     *
309
     * If this netdev class does not have any configuration options, this may
310
     * be a null pointer. */
311
    int (*get_config)(const struct netdev *netdev, struct smap *args);
312
313
    /* Changes the device 'netdev''s configuration to 'args'.
314
     *
315
     * If this netdev class does not support configuration, this may be a null
316
     * pointer.
317
     *
318
     * If the return value is not zero (meaning that an error occurred),
319
     * the provider can allocate a string with an error message in '*errp'.
320
     * The caller has to call free on it. */
321
    int (*set_config)(struct netdev *netdev, const struct smap *args,
322
                      char **errp);
323
324
    /* Returns the tunnel configuration of 'netdev'.  If 'netdev' is
325
     * not a tunnel, returns null.
326
     *
327
     * If this function would always return null, it may be null instead. */
328
    const struct netdev_tunnel_config *
329
        (*get_tunnel_config)(const struct netdev *netdev);
330
331
    /* Build Tunnel header.  Ethernet and ip header parameters are passed to
332
     * tunnel implementation to build entire outer header for given flow. */
333
    int (*build_header)(const struct netdev *, struct ovs_action_push_tnl *data,
334
                        const struct netdev_tnl_build_header_params *params);
335
336
    /* build_header() can not build entire header for all packets for given
337
     * flow.  Push header is called for packet to build header specific to
338
     * a packet on actual transmit.  It uses partial header build by
339
     * build_header() which is passed as data. */
340
    void (*push_header)(const struct netdev *,
341
                        struct dp_packet *packet,
342
                        const struct ovs_action_push_tnl *data);
343
344
    /* Pop tunnel header from packet, build tunnel metadata and resize packet
345
     * for further processing.
346
     * Returns NULL in case of error or tunnel implementation queued packet for further
347
     * processing. */
348
    struct dp_packet * (*pop_header)(struct dp_packet *packet);
349
350
    /* Returns the id of the numa node the 'netdev' is on.  If there is no
351
     * such info, returns NETDEV_NUMA_UNSPEC. */
352
    int (*get_numa_id)(const struct netdev *netdev);
353
354
    /* Configures the number of tx queues of 'netdev'. Returns 0 if successful,
355
     * otherwise a positive errno value.
356
     *
357
     * 'n_txq' specifies the exact number of transmission queues to create.
358
     *
359
     * The caller will call netdev_reconfigure() (if necessary) before using
360
     * netdev_send() on any of the newly configured queues, giving the provider
361
     * a chance to adjust its settings.
362
     *
363
     * On error, the tx queue configuration is unchanged. */
364
    int (*set_tx_multiq)(struct netdev *netdev, unsigned int n_txq);
365
366
    /* Sends buffers on 'netdev'.
367
     * Returns 0 if successful (for every buffer), otherwise a positive errno
368
     * value.  Returns EAGAIN without blocking if one or more packets cannot be
369
     * queued immediately. Returns EMSGSIZE if a partial packet was transmitted
370
     * or if a packet is too big or too small to transmit on the device.
371
     *
372
     * If the function returns a non-zero value, some of the packets might have
373
     * been sent anyway.
374
     *
375
     * The caller transfers ownership of all the packets to the network
376
     * device, regardless of success.
377
     *
378
     * If 'concurrent_txq' is true, the caller may perform concurrent calls
379
     * to netdev_send() with the same 'qid'. The netdev provider is responsible
380
     * for making sure that these concurrent calls do not create a race
381
     * condition by using locking or other synchronization if required.
382
     *
383
     * The network device is expected to maintain one or more packet
384
     * transmission queues, so that the caller does not ordinarily have to
385
     * do additional queuing of packets.  'qid' specifies the queue to use
386
     * and can be ignored if the implementation does not support multiple
387
     * queues.
388
     *
389
     * May return EOPNOTSUPP if a network device does not implement packet
390
     * transmission through this interface.  This function may be set to null
391
     * if it would always return EOPNOTSUPP anyhow.  (This will prevent the
392
     * network device from being usefully used by the netdev-based "userspace
393
     * datapath".  It will also prevent the OVS implementation of bonding from
394
     * working properly over 'netdev'.) */
395
    int (*send)(struct netdev *netdev, int qid, struct dp_packet_batch *batch,
396
                bool concurrent_txq);
397
398
    /* Registers with the poll loop to wake up from the next call to
399
     * poll_block() when the packet transmission queue for 'netdev' has
400
     * sufficient room to transmit a packet with netdev_send().
401
     *
402
     * The network device is expected to maintain one or more packet
403
     * transmission queues, so that the caller does not ordinarily have to
404
     * do additional queuing of packets.  'qid' specifies the queue to use
405
     * and can be ignored if the implementation does not support multiple
406
     * queues.
407
     *
408
     * May be null if not needed, such as for a network device that does not
409
     * implement packet transmission through the 'send' member function. */
410
    void (*send_wait)(struct netdev *netdev, int qid);
411
412
    /* Sets 'netdev''s Ethernet address to 'mac' */
413
    int (*set_etheraddr)(struct netdev *netdev, const struct eth_addr mac);
414
415
    /* Retrieves 'netdev''s Ethernet address into 'mac'.
416
     *
417
     * This address will be advertised as 'netdev''s MAC address through the
418
     * OpenFlow protocol, among other uses. */
419
    int (*get_etheraddr)(const struct netdev *netdev, struct eth_addr *mac);
420
421
    /* Retrieves 'netdev''s MTU into '*mtup'.
422
     *
423
     * The MTU is the maximum size of transmitted (and received) packets, in
424
     * bytes, not including the hardware header; thus, this is typically 1500
425
     * bytes for Ethernet devices.
426
     *
427
     * If 'netdev' does not have an MTU (e.g. as some tunnels do not), then
428
     * this function should return EOPNOTSUPP.  This function may be set to
429
     * null if it would always return EOPNOTSUPP. */
430
    int (*get_mtu)(const struct netdev *netdev, int *mtup);
431
432
    /* Sets 'netdev''s MTU to 'mtu'.
433
     *
434
     * If 'netdev' does not have an MTU (e.g. as some tunnels do not), then
435
     * this function should return EOPNOTSUPP.  This function may be set to
436
     * null if it would always return EOPNOTSUPP. */
437
    int (*set_mtu)(struct netdev *netdev, int mtu);
438
439
    /* Returns the ifindex of 'netdev', if successful, as a positive number.
440
     * On failure, returns a negative errno value.
441
     *
442
     * The desired semantics of the ifindex value are a combination of those
443
     * specified by POSIX for if_nametoindex() and by SNMP for ifIndex.  An
444
     * ifindex value should be unique within a host and remain stable at least
445
     * until reboot.  SNMP says an ifindex "ranges between 1 and the value of
446
     * ifNumber" but many systems do not follow this rule anyhow.
447
     *
448
     * This function may be set to null if it would always return -EOPNOTSUPP.
449
     */
450
    int (*get_ifindex)(const struct netdev *netdev);
451
452
    /* Sets 'carrier' to true if carrier is active (link light is on) on
453
     * 'netdev'.
454
     *
455
     * May be null if device does not provide carrier status (will be always
456
     * up as long as device is up).
457
     */
458
    int (*get_carrier)(const struct netdev *netdev, bool *carrier);
459
460
    /* Returns the number of times 'netdev''s carrier has changed since being
461
     * initialized.
462
     *
463
     * If null, callers will assume the number of carrier resets is zero. */
464
    long long int (*get_carrier_resets)(const struct netdev *netdev);
465
466
    /* Forces ->get_carrier() to poll 'netdev''s MII registers for link status
467
     * instead of checking 'netdev''s carrier.  'netdev''s MII registers will
468
     * be polled once every 'interval' milliseconds.  If 'netdev' does not
469
     * support MII, another method may be used as a fallback.  If 'interval' is
470
     * less than or equal to zero, reverts ->get_carrier() to its normal
471
     * behavior.
472
     *
473
     * Most network devices won't support this feature and will set this
474
     * function pointer to NULL, which is equivalent to returning EOPNOTSUPP.
475
     */
476
    int (*set_miimon_interval)(struct netdev *netdev, long long int interval);
477
478
    /* Retrieves current device stats for 'netdev' into 'stats'.
479
     *
480
     * A network device that supports some statistics but not others, it should
481
     * set the values of the unsupported statistics to all-1-bits
482
     * (UINT64_MAX). */
483
    int (*get_stats)(const struct netdev *netdev, struct netdev_stats *);
484
485
    /* Retrieves current device custom stats for 'netdev' into 'custom_stats'.
486
     *
487
     * A network device should return only available statistics (if any).
488
     * If there are not statistics available, empty array should be
489
     * returned.
490
     *
491
     * The caller initializes 'custom_stats' before calling this function.
492
     * The caller takes ownership over allocated array of counters inside
493
     * structure netdev_custom_stats.
494
     * */
495
    int (*get_custom_stats)(const struct netdev *netdev,
496
                            struct netdev_custom_stats *custom_stats);
497
498
    /* Stores the features supported by 'netdev' into each of '*current',
499
     * '*advertised', '*supported', and '*peer'.  Each value is a bitmap of
500
     * NETDEV_F_* bits.
501
     *
502
     * This function may be set to null if it would always return EOPNOTSUPP.
503
     */
504
    int (*get_features)(const struct netdev *netdev,
505
                        enum netdev_features *current,
506
                        enum netdev_features *advertised,
507
                        enum netdev_features *supported,
508
                        enum netdev_features *peer);
509
510
    /* Stores the current and maximum supported link speed by 'netdev' into
511
     * each of '*current' and '*max'. Each value represents the speed in Mbps.
512
     * If any of the speeds is unknown, a zero value must be stored.
513
     *
514
     * This function may be set to null if it would always return EOPNOTSUPP.
515
     */
516
    int (*get_speed)(const struct netdev *netdev, uint32_t *current,
517
                     uint32_t *max);
518
519
    /* Stores the current duplex status of 'netdev' into '*full_duplex'.
520
     * 'true' means full duplex, 'false' means half duplex.
521
     *
522
     * This function may be set to null if it would always return EOPNOTSUPP.
523
     */
524
    int (*get_duplex)(const struct netdev *netdev, bool *full_duplex);
525
526
    /* Set the features advertised by 'netdev' to 'advertise', which is a
527
     * set of NETDEV_F_* bits.
528
     *
529
     * This function may be set to null for a network device that does not
530
     * support configuring advertisements. */
531
    int (*set_advertisements)(struct netdev *netdev,
532
                              enum netdev_features advertise);
533
534
    /* Returns 'netdev''s configured packet_type mode.
535
     *
536
     * This function may be set to null if it would always return
537
     * NETDEV_PT_LEGACY_L2. */
538
    enum netdev_pt_mode (*get_pt_mode)(const struct netdev *netdev);
539
540
    /* Attempts to set input rate limiting (policing) policy, such that:
541
     * - up to 'kbits_rate' kbps of traffic is accepted, with a maximum
542
     *   accumulative burst size of 'kbits' kb; and
543
     * - up to 'kpkts' kpps of traffic is accepted, with a maximum
544
     *   accumulative burst size of 'kpkts' kilo packets.
545
     *
546
     * This function may be set to null if policing is not supported. */
547
    int (*set_policing)(struct netdev *netdev, unsigned int kbits_rate,
548
                        unsigned int kbits_burst, unsigned int kpkts_rate,
549
                        unsigned int kpkts_burst);
550
551
    /* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves
552
     * it empty if 'netdev' does not support QoS.  Any names added to 'types'
553
     * should be documented as valid for the "type" column in the "QoS" table
554
     * in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
555
     *
556
     * Every network device must support disabling QoS with a type of "", but
557
     * this function must not add "" to 'types'.
558
     *
559
     * The caller is responsible for initializing 'types' (e.g. with
560
     * sset_init()) before calling this function.  The caller retains ownership
561
     * of 'types'.
562
     *
563
     * May be NULL if 'netdev' does not support QoS at all. */
564
    int (*get_qos_types)(const struct netdev *netdev, struct sset *types);
565
566
    /* Queries 'netdev' for its capabilities regarding the specified 'type' of
567
     * QoS.  On success, initializes 'caps' with the QoS capabilities.
568
     *
569
     * Should return EOPNOTSUPP if 'netdev' does not support 'type'.  May be
570
     * NULL if 'netdev' does not support QoS at all. */
571
    int (*get_qos_capabilities)(const struct netdev *netdev,
572
                                const char *type,
573
                                struct netdev_qos_capabilities *caps);
574
575
    /* Queries 'netdev' about its currently configured form of QoS.  If
576
     * successful, stores the name of the current form of QoS into '*typep'
577
     * and any details of configuration as string key-value pairs in
578
     * 'details'.
579
     *
580
     * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
581
     *
582
     * The caller initializes 'details' before calling this function.  The
583
     * caller takes ownership of the string key-values pairs added to
584
     * 'details'.
585
     *
586
     * The netdev retains ownership of '*typep'.
587
     *
588
     * '*typep' will be one of the types returned by netdev_get_qos_types() for
589
     * 'netdev'.  The contents of 'details' should be documented as valid for
590
     * '*typep' in the "other_config" column in the "QoS" table in
591
     * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
592
     *
593
     * May be NULL if 'netdev' does not support QoS at all. */
594
    int (*get_qos)(const struct netdev *netdev,
595
                   const char **typep, struct smap *details);
596
597
    /* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to
598
     * 'type' with details of configuration from 'details'.
599
     *
600
     * On error, the previous QoS configuration is retained.
601
     *
602
     * When this function changes the type of QoS (not just 'details'), this
603
     * also resets all queue configuration for 'netdev' to their defaults
604
     * (which depend on the specific type of QoS).  Otherwise, the queue
605
     * configuration for 'netdev' is unchanged.
606
     *
607
     * 'type' should be "" (to disable QoS) or one of the types returned by
608
     * netdev_get_qos_types() for 'netdev'.  The contents of 'details' should
609
     * be documented as valid for the given 'type' in the "other_config" column
610
     * in the "QoS" table in vswitchd/vswitch.xml (which is built as
611
     * ovs-vswitchd.conf.db(8)).
612
     *
613
     * May be NULL if 'netdev' does not support QoS at all. */
614
    int (*set_qos)(struct netdev *netdev,
615
                   const char *type, const struct smap *details);
616
617
    /* Queries 'netdev' for information about the queue numbered 'queue_id'.
618
     * If successful, adds that information as string key-value pairs to
619
     * 'details'.  Returns 0 if successful, otherwise a positive errno value.
620
     *
621
     * Should return EINVAL if 'queue_id' is greater than or equal to the
622
     * number of supported queues (as reported in the 'n_queues' member of
623
     * struct netdev_qos_capabilities by 'get_qos_capabilities').
624
     *
625
     * The caller initializes 'details' before calling this function.  The
626
     * caller takes ownership of the string key-values pairs added to
627
     * 'details'.
628
     *
629
     * The returned contents of 'details' should be documented as valid for the
630
     * given 'type' in the "other_config" column in the "Queue" table in
631
     * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
632
     */
633
    int (*get_queue)(const struct netdev *netdev,
634
                     unsigned int queue_id, struct smap *details);
635
636
    /* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
637
     * string pairs in 'details'.  The contents of 'details' should be
638
     * documented as valid for the given 'type' in the "other_config" column in
639
     * the "Queue" table in vswitchd/vswitch.xml (which is built as
640
     * ovs-vswitchd.conf.db(8)).  Returns 0 if successful, otherwise a positive
641
     * errno value.  On failure, the given queue's configuration should be
642
     * unmodified.
643
     *
644
     * Should return EINVAL if 'queue_id' is greater than or equal to the
645
     * number of supported queues (as reported in the 'n_queues' member of
646
     * struct netdev_qos_capabilities by 'get_qos_capabilities'), or if
647
     * 'details' is invalid for the type of queue.
648
     *
649
     * This function does not modify 'details', and the caller retains
650
     * ownership of it.
651
     *
652
     * May be NULL if 'netdev' does not support QoS at all. */
653
    int (*set_queue)(struct netdev *netdev,
654
                     unsigned int queue_id, const struct smap *details);
655
656
    /* Attempts to delete the queue numbered 'queue_id' from 'netdev'.
657
     *
658
     * Should return EINVAL if 'queue_id' is greater than or equal to the
659
     * number of supported queues (as reported in the 'n_queues' member of
660
     * struct netdev_qos_capabilities by 'get_qos_capabilities').  Should
661
     * return EOPNOTSUPP if 'queue_id' is valid but may not be deleted (e.g. if
662
     * 'netdev' has a fixed set of queues with the current QoS mode).
663
     *
664
     * May be NULL if 'netdev' does not support QoS at all, or if all of its
665
     * QoS modes have fixed sets of queues. */
666
    int (*delete_queue)(struct netdev *netdev, unsigned int queue_id);
667
668
    /* Obtains statistics about 'queue_id' on 'netdev'.  Fills 'stats' with the
669
     * queue's statistics.  May set individual members of 'stats' to all-1-bits
670
     * if the statistic is unavailable.
671
     *
672
     * May be NULL if 'netdev' does not support QoS at all. */
673
    int (*get_queue_stats)(const struct netdev *netdev, unsigned int queue_id,
674
                           struct netdev_queue_stats *stats);
675
676
    /* Attempts to begin dumping the queues in 'netdev'.  On success, returns 0
677
     * and initializes '*statep' with any data needed for iteration.  On
678
     * failure, returns a positive errno value.
679
     *
680
     * May be NULL if 'netdev' does not support QoS at all. */
681
    int (*queue_dump_start)(const struct netdev *netdev, void **statep);
682
683
    /* Attempts to retrieve another queue from 'netdev' for 'state', which was
684
     * initialized by a successful call to the 'queue_dump_start' function for
685
     * 'netdev'.  On success, stores a queue ID into '*queue_id' and fills
686
     * 'details' with the configuration of the queue with that ID.  Returns EOF
687
     * if the last queue has been dumped, or a positive errno value on error.
688
     * This function will not be called again once it returns nonzero once for
689
     * a given iteration (but the 'queue_dump_done' function will be called
690
     * afterward).
691
     *
692
     * The caller initializes and clears 'details' before calling this
693
     * function.  The caller takes ownership of the string key-values pairs
694
     * added to 'details'.
695
     *
696
     * The returned contents of 'details' should be documented as valid for the
697
     * given 'type' in the "other_config" column in the "Queue" table in
698
     * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
699
     *
700
     * May be NULL if 'netdev' does not support QoS at all. */
701
    int (*queue_dump_next)(const struct netdev *netdev, void *state,
702
                           unsigned int *queue_id, struct smap *details);
703
704
    /* Releases resources from 'netdev' for 'state', which was initialized by a
705
     * successful call to the 'queue_dump_start' function for 'netdev'.
706
     *
707
     * May be NULL if 'netdev' does not support QoS at all. */
708
    int (*queue_dump_done)(const struct netdev *netdev, void *state);
709
710
    /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's
711
     * ID, its statistics, and the 'aux' specified by the caller.  The order of
712
     * iteration is unspecified, but (when successful) each queue must be
713
     * visited exactly once.
714
     *
715
     * 'cb' will not modify or free the statistics passed in. */
716
    int (*dump_queue_stats)(const struct netdev *netdev,
717
                            void (*cb)(unsigned int queue_id,
718
                                       struct netdev_queue_stats *,
719
                                       void *aux),
720
                            void *aux);
721
722
    /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
723
     * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.
724
     *
725
     * This function may be set to null if it would always return EOPNOTSUPP
726
     * anyhow. */
727
    int (*set_in4)(struct netdev *netdev, struct in_addr addr,
728
                   struct in_addr mask);
729
730
    /* Returns all assigned IP address to  'netdev' and returns 0.
731
     * API allocates array of address and masks and set it to
732
     * '*addr' and '*mask'.
733
     * Otherwise, returns a positive errno value and sets '*addr', '*mask
734
     * and '*n_addr' to NULL.
735
     *
736
     * The following error values have well-defined meanings:
737
     *
738
     *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
739
     *
740
     *   - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
741
     *
742
     * 'addr' may be null, in which case the address itself is not reported. */
743
    int (*get_addr_list)(const struct netdev *netdev, struct in6_addr **in,
744
                         struct in6_addr **mask, int *n_in6);
745
746
    /* Adds 'router' as a default IP gateway for the TCP/IP stack that
747
     * corresponds to 'netdev'.
748
     *
749
     * This function may be set to null if it would always return EOPNOTSUPP
750
     * anyhow. */
751
    int (*add_router)(struct netdev *netdev, struct in_addr router);
752
753
    /* Looks up the next hop for 'host' in the host's routing table.  If
754
     * successful, stores the next hop gateway's address (0 if 'host' is on a
755
     * directly connected network) in '*next_hop' and a copy of the name of the
756
     * device to reach 'host' in '*netdev_name', and returns 0.  The caller is
757
     * responsible for freeing '*netdev_name' (by calling free()).
758
     *
759
     * This function may be set to null if it would always return EOPNOTSUPP
760
     * anyhow. */
761
    int (*get_next_hop)(const struct in_addr *host, struct in_addr *next_hop,
762
                        char **netdev_name);
763
764
    /* Retrieves driver information of the device.
765
     *
766
     * Populates 'smap' with key-value pairs representing the status of the
767
     * device.  'smap' is a set of key-value string pairs representing netdev
768
     * type specific information.  For more information see
769
     * ovs-vswitchd.conf.db(5).
770
     *
771
     * The caller is responsible for destroying 'smap' and its data.
772
     *
773
     * This function may be set to null if it would always return EOPNOTSUPP
774
     * anyhow. */
775
    int (*get_status)(const struct netdev *netdev, struct smap *smap);
776
777
    /* Looks up the ARP table entry for 'ip' on 'netdev' and stores the
778
     * corresponding MAC address in 'mac'.  A return value of ENXIO, in
779
     * particular, indicates that there is no ARP table entry for 'ip' on
780
     * 'netdev'.
781
     *
782
     * This function may be set to null if it would always return EOPNOTSUPP
783
     * anyhow. */
784
    int (*arp_lookup)(const struct netdev *netdev, ovs_be32 ip,
785
                      struct eth_addr *mac);
786
787
    /* Retrieves the current set of flags on 'netdev' into '*old_flags'.  Then,
788
     * turns off the flags that are set to 1 in 'off' and turns on the flags
789
     * that are set to 1 in 'on'.  (No bit will be set to 1 in both 'off' and
790
     * 'on'; that is, off & on == 0.)
791
     *
792
     * This function may be invoked from a signal handler.  Therefore, it
793
     * should not do anything that is not signal-safe (such as logging). */
794
    int (*update_flags)(struct netdev *netdev, enum netdev_flags off,
795
                        enum netdev_flags on, enum netdev_flags *old_flags);
796
797
    /* If the provider called netdev_request_reconfigure(), the upper layer
798
     * will eventually call this.  The provider can update the device
799
     * configuration knowing that the upper layer will not call rxq_recv() or
800
     * send() until this function returns.
801
     *
802
     * On error, the configuration is indeterminant and the device cannot be
803
     * used to send and receive packets until a successful configuration is
804
     * applied. */
805
    int (*reconfigure)(struct netdev *netdev);
806
/* ## -------------------- ## */
807
/* ## netdev_rxq Functions ## */
808
/* ## -------------------- ## */
809
810
/* If a particular netdev class does not support receiving packets, all these
811
 * function pointers must be NULL. */
812
813
    /* Life-cycle functions for a netdev_rxq.  See the large comment above on
814
     * struct netdev_class. */
815
    struct netdev_rxq *(*rxq_alloc)(void);
816
    int (*rxq_construct)(struct netdev_rxq *);
817
    void (*rxq_destruct)(struct netdev_rxq *);
818
    void (*rxq_dealloc)(struct netdev_rxq *);
819
820
    /* Retrieves the current state of rx queue.  'false' means that queue won't
821
     * get traffic in a short term and could be not polled.
822
     *
823
     * This function may be set to null if it would always return 'true'
824
     * anyhow. */
825
    bool (*rxq_enabled)(struct netdev_rxq *);
826
827
    /* Attempts to receive a batch of packets from 'rx'.  In 'batch', the
828
     * caller supplies 'packets' as the pointer to the beginning of an array
829
     * of NETDEV_MAX_BURST pointers to dp_packet.  If successful, the
830
     * implementation stores pointers to up to NETDEV_MAX_BURST dp_packets into
831
     * the array, transferring ownership of the packets to the caller, stores
832
     * the number of received packets into 'count', and returns 0.
833
     *
834
     * The implementation does not necessarily initialize any non-data members
835
     * of 'packets' in 'batch'.  That is, the caller must initialize layer
836
     * pointers and metadata itself, if desired, e.g. with pkt_metadata_init()
837
     * and miniflow_extract().
838
     *
839
     * Implementations should allocate buffers with DP_NETDEV_HEADROOM bytes of
840
     * headroom.
841
     *
842
     * If the caller provides a non-NULL qfill pointer, the implementation
843
     * should return the number (zero or more) of remaining packets in the
844
     * queue after the reception the current batch, if it supports that,
845
     * or -ENOTSUP otherwise.
846
     *
847
     * Returns EAGAIN immediately if no packet is ready to be received or
848
     * another positive errno value if an error was encountered. */
849
    int (*rxq_recv)(struct netdev_rxq *rx, struct dp_packet_batch *batch,
850
                    int *qfill);
851
852
    /* Registers with the poll loop to wake up from the next call to
853
     * poll_block() when a packet is ready to be received with
854
     * netdev_rxq_recv() on 'rx'. */
855
    void (*rxq_wait)(struct netdev_rxq *rx);
856
857
    /* Discards all packets waiting to be received from 'rx'. */
858
    int (*rxq_drain)(struct netdev_rxq *rx);
859
860
    /* Get a block_id from the netdev.
861
     * Returns the block_id or 0 if none exists for netdev. */
862
    uint32_t (*get_block_id)(struct netdev *);
863
};
864
865
int netdev_register_provider(const struct netdev_class *);
866
int netdev_unregister_provider(const char *type);
867
868
#if defined(__FreeBSD__) || defined(__NetBSD__)
869
extern const struct netdev_class netdev_bsd_class;
870
#elif defined(_WIN32)
871
extern const struct netdev_class netdev_windows_class;
872
#else
873
extern const struct netdev_class netdev_linux_class;
874
#endif
875
extern const struct netdev_class netdev_internal_class;
876
extern const struct netdev_class netdev_tap_class;
877
878
#ifdef HAVE_AF_XDP
879
extern const struct netdev_class netdev_afxdp_class;
880
extern const struct netdev_class netdev_afxdp_nonpmd_class;
881
#endif
882
#ifdef  __cplusplus
883
}
884
#endif
885
886
#endif /* netdev.h */