7f266fdc51479d95c6543fca254f565246644714
[cascardo/ovs.git] / lib / netdev-provider.h
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 "list.h"
25 #include "ovs-numa.h"
26 #include "seq.h"
27 #include "shash.h"
28 #include "smap.h"
29
30 #ifdef  __cplusplus
31 extern "C" {
32 #endif
33
34 #define NETDEV_NUMA_UNSPEC OVS_NUMA_UNSPEC
35
36 /* A network device (e.g. an Ethernet device).
37  *
38  * Network device implementations may read these members but should not modify
39  * them. */
40 struct netdev {
41     /* The following do not change during the lifetime of a struct netdev. */
42     char *name;                         /* Name of network device. */
43     const struct netdev_class *netdev_class; /* Functions to control
44                                                 this device. */
45
46     /* A sequence number which indicates changes in one of 'netdev''s
47      * properties.   It must be nonzero so that users have a value which
48      * they may use as a reset when tracking 'netdev'.
49      *
50      * Minimally, the sequence number is required to change whenever
51      * 'netdev''s flags, features, ethernet address, or carrier changes. */
52     uint64_t change_seq;
53
54     /* The following are protected by 'netdev_mutex' (internal to netdev.c). */
55     int n_txq;
56     int n_rxq;
57     int ref_cnt;                        /* Times this devices was opened. */
58     struct shash_node *node;            /* Pointer to element in global map. */
59     struct list saved_flags_list; /* Contains "struct netdev_saved_flags". */
60 };
61
62 static void
63 netdev_change_seq_changed(const struct netdev *netdev_)
64 {
65     struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
66     seq_change(connectivity_seq_get());
67     netdev->change_seq++;
68     if (!netdev->change_seq) {
69         netdev->change_seq++;
70     }
71 }
72
73 const char *netdev_get_type(const struct netdev *);
74 const struct netdev_class *netdev_get_class(const struct netdev *);
75 const char *netdev_get_name(const struct netdev *);
76 struct netdev *netdev_from_name(const char *name);
77 void netdev_get_devices(const struct netdev_class *,
78                         struct shash *device_list);
79 struct netdev **netdev_get_vports(size_t *size);
80
81 /* A data structure for capturing packets received by a network device.
82  *
83  * Network device implementations may read these members but should not modify
84  * them.
85  *
86  * None of these members change during the lifetime of a struct netdev_rxq. */
87 struct netdev_rxq {
88     struct netdev *netdev;      /* Owns a reference to the netdev. */
89     int queue_id;
90 };
91
92 struct netdev *netdev_rxq_get_netdev(const struct netdev_rxq *);
93
94 /* Network device class structure, to be defined by each implementation of a
95  * network device.
96  *
97  * These functions return 0 if successful or a positive errno value on failure,
98  * except where otherwise noted.
99  *
100  *
101  * Data Structures
102  * ===============
103  *
104  * These functions work primarily with two different kinds of data structures:
105  *
106  *   - "struct netdev", which represents a network device.
107  *
108  *   - "struct netdev_rxq", which represents a handle for capturing packets
109  *     received on a network device
110  *
111  * Each of these data structures contains all of the implementation-independent
112  * generic state for the respective concept, called the "base" state.  None of
113  * them contains any extra space for implementations to use.  Instead, each
114  * implementation is expected to declare its own data structure that contains
115  * an instance of the generic data structure plus additional
116  * implementation-specific members, called the "derived" state.  The
117  * implementation can use casts or (preferably) the CONTAINER_OF macro to
118  * obtain access to derived state given only a pointer to the embedded generic
119  * data structure.
120  *
121  *
122  * Life Cycle
123  * ==========
124  *
125  * Four stylized functions accompany each of these data structures:
126  *
127  *            "alloc"          "construct"        "destruct"       "dealloc"
128  *            ------------   ----------------  ---------------  --------------
129  * netdev      ->alloc        ->construct        ->destruct        ->dealloc
130  * netdev_rxq  ->rxq_alloc    ->rxq_construct    ->rxq_destruct    ->rxq_dealloc
131  *
132  * Any instance of a given data structure goes through the following life
133  * cycle:
134  *
135  *   1. The client calls the "alloc" function to obtain raw memory.  If "alloc"
136  *      fails, skip all the other steps.
137  *
138  *   2. The client initializes all of the data structure's base state.  If this
139  *      fails, skip to step 7.
140  *
141  *   3. The client calls the "construct" function.  The implementation
142  *      initializes derived state.  It may refer to the already-initialized
143  *      base state.  If "construct" fails, skip to step 6.
144  *
145  *   4. The data structure is now initialized and in use.
146  *
147  *   5. When the data structure is no longer needed, the client calls the
148  *      "destruct" function.  The implementation uninitializes derived state.
149  *      The base state has not been uninitialized yet, so the implementation
150  *      may still refer to it.
151  *
152  *   6. The client uninitializes all of the data structure's base state.
153  *
154  *   7. The client calls the "dealloc" to free the raw memory.  The
155  *      implementation must not refer to base or derived state in the data
156  *      structure, because it has already been uninitialized.
157  *
158  * If netdev support multi-queue IO then netdev->construct should set initialize
159  * netdev->n_rxq to number of queues.
160  *
161  * Each "alloc" function allocates and returns a new instance of the respective
162  * data structure.  The "alloc" function is not given any information about the
163  * use of the new data structure, so it cannot perform much initialization.
164  * Its purpose is just to ensure that the new data structure has enough room
165  * for base and derived state.  It may return a null pointer if memory is not
166  * available, in which case none of the other functions is called.
167  *
168  * Each "construct" function initializes derived state in its respective data
169  * structure.  When "construct" is called, all of the base state has already
170  * been initialized, so the "construct" function may refer to it.  The
171  * "construct" function is allowed to fail, in which case the client calls the
172  * "dealloc" function (but not the "destruct" function).
173  *
174  * Each "destruct" function uninitializes and frees derived state in its
175  * respective data structure.  When "destruct" is called, the base state has
176  * not yet been uninitialized, so the "destruct" function may refer to it.  The
177  * "destruct" function is not allowed to fail.
178  *
179  * Each "dealloc" function frees raw memory that was allocated by the the
180  * "alloc" function.  The memory's base and derived members might not have ever
181  * been initialized (but if "construct" returned successfully, then it has been
182  * "destruct"ed already).  The "dealloc" function is not allowed to fail.
183  *
184  *
185  * Device Change Notification
186  * ==========================
187  *
188  * Minimally, implementations are required to report changes to netdev flags,
189  * features, ethernet address or carrier through connectivity_seq. Changes to
190  * other properties are allowed to cause notification through this interface,
191  * although implementations should try to avoid this. connectivity_seq_get()
192  * can be used to acquire a reference to the struct seq. The interface is
193  * described in detail in seq.h. */
194 struct netdev_class {
195     /* Type of netdevs in this class, e.g. "system", "tap", "gre", etc.
196      *
197      * One of the providers should supply a "system" type, since this is
198      * the type assumed if no type is specified when opening a netdev.
199      * The "system" type corresponds to an existing network device on
200      * the system. */
201     const char *type;
202
203 /* ## ------------------- ## */
204 /* ## Top-Level Functions ## */
205 /* ## ------------------- ## */
206
207     /* Called when the netdev provider is registered, typically at program
208      * startup.  Returning an error from this function will prevent any network
209      * device in this class from being opened.
210      *
211      * This function may be set to null if a network device class needs no
212      * initialization at registration time. */
213     int (*init)(void);
214
215     /* Performs periodic work needed by netdevs of this class.  May be null if
216      * no periodic work is necessary. */
217     void (*run)(void);
218
219     /* Arranges for poll_block() to wake up if the "run" member function needs
220      * to be called.  Implementations are additionally required to wake
221      * whenever something changes in any of its netdevs which would cause their
222      * ->change_seq() function to change its result.  May be null if nothing is
223      * needed here. */
224     void (*wait)(void);
225
226 /* ## ---------------- ## */
227 /* ## netdev Functions ## */
228 /* ## ---------------- ## */
229
230     /* Life-cycle functions for a netdev.  See the large comment above on
231      * struct netdev_class. */
232     struct netdev *(*alloc)(void);
233     int (*construct)(struct netdev *);
234     void (*destruct)(struct netdev *);
235     void (*dealloc)(struct netdev *);
236
237     /* Fetches the device 'netdev''s configuration, storing it in 'args'.
238      * The caller owns 'args' and pre-initializes it to an empty smap.
239      *
240      * If this netdev class does not have any configuration options, this may
241      * be a null pointer. */
242     int (*get_config)(const struct netdev *netdev, struct smap *args);
243
244     /* Changes the device 'netdev''s configuration to 'args'.
245      *
246      * If this netdev class does not support configuration, this may be a null
247      * pointer. */
248     int (*set_config)(struct netdev *netdev, const struct smap *args);
249
250     /* Returns the tunnel configuration of 'netdev'.  If 'netdev' is
251      * not a tunnel, returns null.
252      *
253      * If this function would always return null, it may be null instead. */
254     const struct netdev_tunnel_config *
255         (*get_tunnel_config)(const struct netdev *netdev);
256
257     /* Returns the id of the numa node the 'netdev' is on.  If there is no
258      * such info, returns NETDEV_NUMA_UNSPEC. */
259     int (*get_numa_id)(const struct netdev *netdev);
260
261     /* Sends buffers on 'netdev'.
262      * Returns 0 if successful (for every buffer), otherwise a positive errno
263      * value.  Returns EAGAIN without blocking if one or more packets cannot be
264      * queued immediately. Returns EMSGSIZE if a partial packet was transmitted
265      * or if a packet is too big or too small to transmit on the device.
266      *
267      * If the function returns a non-zero value, some of the packets might have
268      * been sent anyway.
269      *
270      * To retain ownership of 'buffers' caller can set may_steal to false.
271      *
272      * The network device is expected to maintain one or more packet
273      * transmission queues, so that the caller does not ordinarily have to
274      * do additional queuing of packets.  'qid' specifies the queue to use
275      * and can be ignored if the implementation does not support multiple
276      * queues.
277      *
278      * May return EOPNOTSUPP if a network device does not implement packet
279      * transmission through this interface.  This function may be set to null
280      * if it would always return EOPNOTSUPP anyhow.  (This will prevent the
281      * network device from being usefully used by the netdev-based "userspace
282      * datapath".  It will also prevent the OVS implementation of bonding from
283      * working properly over 'netdev'.) */
284     int (*send)(struct netdev *netdev, int qid, struct dpif_packet **buffers,
285                 int cnt, bool may_steal);
286
287     /* Registers with the poll loop to wake up from the next call to
288      * poll_block() when the packet transmission queue for 'netdev' has
289      * sufficient room to transmit a packet with netdev_send().
290      *
291      * The network device is expected to maintain one or more packet
292      * transmission queues, so that the caller does not ordinarily have to
293      * do additional queuing of packets.  'qid' specifies the queue to use
294      * and can be ignored if the implementation does not support multiple
295      * queues.
296      *
297      * May be null if not needed, such as for a network device that does not
298      * implement packet transmission through the 'send' member function. */
299     void (*send_wait)(struct netdev *netdev, int qid);
300
301     /* Sets 'netdev''s Ethernet address to 'mac' */
302     int (*set_etheraddr)(struct netdev *netdev, const uint8_t mac[6]);
303
304     /* Retrieves 'netdev''s Ethernet address into 'mac'.
305      *
306      * This address will be advertised as 'netdev''s MAC address through the
307      * OpenFlow protocol, among other uses. */
308     int (*get_etheraddr)(const struct netdev *netdev, uint8_t mac[6]);
309
310     /* Retrieves 'netdev''s MTU into '*mtup'.
311      *
312      * The MTU is the maximum size of transmitted (and received) packets, in
313      * bytes, not including the hardware header; thus, this is typically 1500
314      * bytes for Ethernet devices.
315      *
316      * If 'netdev' does not have an MTU (e.g. as some tunnels do not), then
317      * this function should return EOPNOTSUPP.  This function may be set to
318      * null if it would always return EOPNOTSUPP. */
319     int (*get_mtu)(const struct netdev *netdev, int *mtup);
320
321     /* Sets 'netdev''s MTU to 'mtu'.
322      *
323      * If 'netdev' does not have an MTU (e.g. as some tunnels do not), then
324      * this function should return EOPNOTSUPP.  This function may be set to
325      * null if it would always return EOPNOTSUPP. */
326     int (*set_mtu)(const struct netdev *netdev, int mtu);
327
328     /* Returns the ifindex of 'netdev', if successful, as a positive number.
329      * On failure, returns a negative errno value.
330      *
331      * The desired semantics of the ifindex value are a combination of those
332      * specified by POSIX for if_nametoindex() and by SNMP for ifIndex.  An
333      * ifindex value should be unique within a host and remain stable at least
334      * until reboot.  SNMP says an ifindex "ranges between 1 and the value of
335      * ifNumber" but many systems do not follow this rule anyhow.
336      *
337      * This function may be set to null if it would always return -EOPNOTSUPP.
338      */
339     int (*get_ifindex)(const struct netdev *netdev);
340
341     /* Sets 'carrier' to true if carrier is active (link light is on) on
342      * 'netdev'.
343      *
344      * May be null if device does not provide carrier status (will be always
345      * up as long as device is up).
346      */
347     int (*get_carrier)(const struct netdev *netdev, bool *carrier);
348
349     /* Returns the number of times 'netdev''s carrier has changed since being
350      * initialized.
351      *
352      * If null, callers will assume the number of carrier resets is zero. */
353     long long int (*get_carrier_resets)(const struct netdev *netdev);
354
355     /* Forces ->get_carrier() to poll 'netdev''s MII registers for link status
356      * instead of checking 'netdev''s carrier.  'netdev''s MII registers will
357      * be polled once ever 'interval' milliseconds.  If 'netdev' does not
358      * support MII, another method may be used as a fallback.  If 'interval' is
359      * less than or equal to zero, reverts ->get_carrier() to its normal
360      * behavior.
361      *
362      * Most network devices won't support this feature and will set this
363      * function pointer to NULL, which is equivalent to returning EOPNOTSUPP.
364      */
365     int (*set_miimon_interval)(struct netdev *netdev, long long int interval);
366
367     /* Retrieves current device stats for 'netdev' into 'stats'.
368      *
369      * A network device that supports some statistics but not others, it should
370      * set the values of the unsupported statistics to all-1-bits
371      * (UINT64_MAX). */
372     int (*get_stats)(const struct netdev *netdev, struct netdev_stats *);
373
374     /* Stores the features supported by 'netdev' into each of '*current',
375      * '*advertised', '*supported', and '*peer'.  Each value is a bitmap of
376      * NETDEV_F_* bits.
377      *
378      * This function may be set to null if it would always return EOPNOTSUPP.
379      */
380     int (*get_features)(const struct netdev *netdev,
381                         enum netdev_features *current,
382                         enum netdev_features *advertised,
383                         enum netdev_features *supported,
384                         enum netdev_features *peer);
385
386     /* Set the features advertised by 'netdev' to 'advertise', which is a
387      * set of NETDEV_F_* bits.
388      *
389      * This function may be set to null for a network device that does not
390      * support configuring advertisements. */
391     int (*set_advertisements)(struct netdev *netdev,
392                               enum netdev_features advertise);
393
394     /* Attempts to set input rate limiting (policing) policy, such that up to
395      * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative
396      * burst size of 'kbits' kb.
397      *
398      * This function may be set to null if policing is not supported. */
399     int (*set_policing)(struct netdev *netdev, unsigned int kbits_rate,
400                         unsigned int kbits_burst);
401
402     /* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves
403      * it empty if 'netdev' does not support QoS.  Any names added to 'types'
404      * should be documented as valid for the "type" column in the "QoS" table
405      * in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
406      *
407      * Every network device must support disabling QoS with a type of "", but
408      * this function must not add "" to 'types'.
409      *
410      * The caller is responsible for initializing 'types' (e.g. with
411      * sset_init()) before calling this function.  The caller retains ownership
412      * of 'types'.
413      *
414      * May be NULL if 'netdev' does not support QoS at all. */
415     int (*get_qos_types)(const struct netdev *netdev, struct sset *types);
416
417     /* Queries 'netdev' for its capabilities regarding the specified 'type' of
418      * QoS.  On success, initializes 'caps' with the QoS capabilities.
419      *
420      * Should return EOPNOTSUPP if 'netdev' does not support 'type'.  May be
421      * NULL if 'netdev' does not support QoS at all. */
422     int (*get_qos_capabilities)(const struct netdev *netdev,
423                                 const char *type,
424                                 struct netdev_qos_capabilities *caps);
425
426     /* Queries 'netdev' about its currently configured form of QoS.  If
427      * successful, stores the name of the current form of QoS into '*typep'
428      * and any details of configuration as string key-value pairs in
429      * 'details'.
430      *
431      * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
432      *
433      * The caller initializes 'details' before calling this function.  The
434      * caller takes ownership of the string key-values pairs added to
435      * 'details'.
436      *
437      * The netdev retains ownership of '*typep'.
438      *
439      * '*typep' will be one of the types returned by netdev_get_qos_types() for
440      * 'netdev'.  The contents of 'details' should be documented as valid for
441      * '*typep' in the "other_config" column in the "QoS" table in
442      * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
443      *
444      * May be NULL if 'netdev' does not support QoS at all. */
445     int (*get_qos)(const struct netdev *netdev,
446                    const char **typep, struct smap *details);
447
448     /* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to
449      * 'type' with details of configuration from 'details'.
450      *
451      * On error, the previous QoS configuration is retained.
452      *
453      * When this function changes the type of QoS (not just 'details'), this
454      * also resets all queue configuration for 'netdev' to their defaults
455      * (which depend on the specific type of QoS).  Otherwise, the queue
456      * configuration for 'netdev' is unchanged.
457      *
458      * 'type' should be "" (to disable QoS) or one of the types returned by
459      * netdev_get_qos_types() for 'netdev'.  The contents of 'details' should
460      * be documented as valid for the given 'type' in the "other_config" column
461      * in the "QoS" table in vswitchd/vswitch.xml (which is built as
462      * ovs-vswitchd.conf.db(8)).
463      *
464      * May be NULL if 'netdev' does not support QoS at all. */
465     int (*set_qos)(struct netdev *netdev,
466                    const char *type, const struct smap *details);
467
468     /* Queries 'netdev' for information about the queue numbered 'queue_id'.
469      * If successful, adds that information as string key-value pairs to
470      * 'details'.  Returns 0 if successful, otherwise a positive errno value.
471      *
472      * Should return EINVAL if 'queue_id' is greater than or equal to the
473      * number of supported queues (as reported in the 'n_queues' member of
474      * struct netdev_qos_capabilities by 'get_qos_capabilities').
475      *
476      * The caller initializes 'details' before calling this function.  The
477      * caller takes ownership of the string key-values pairs added to
478      * 'details'.
479      *
480      * The returned contents of 'details' should be documented as valid for the
481      * given 'type' in the "other_config" column in the "Queue" table in
482      * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
483      */
484     int (*get_queue)(const struct netdev *netdev,
485                      unsigned int queue_id, struct smap *details);
486
487     /* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
488      * string pairs in 'details'.  The contents of 'details' should be
489      * documented as valid for the given 'type' in the "other_config" column in
490      * the "Queue" table in vswitchd/vswitch.xml (which is built as
491      * ovs-vswitchd.conf.db(8)).  Returns 0 if successful, otherwise a positive
492      * errno value.  On failure, the given queue's configuration should be
493      * unmodified.
494      *
495      * Should return EINVAL if 'queue_id' is greater than or equal to the
496      * number of supported queues (as reported in the 'n_queues' member of
497      * struct netdev_qos_capabilities by 'get_qos_capabilities'), or if
498      * 'details' is invalid for the type of queue.
499      *
500      * This function does not modify 'details', and the caller retains
501      * ownership of it.
502      *
503      * May be NULL if 'netdev' does not support QoS at all. */
504     int (*set_queue)(struct netdev *netdev,
505                      unsigned int queue_id, const struct smap *details);
506
507     /* Attempts to delete the queue numbered 'queue_id' from 'netdev'.
508      *
509      * Should return EINVAL if 'queue_id' is greater than or equal to the
510      * number of supported queues (as reported in the 'n_queues' member of
511      * struct netdev_qos_capabilities by 'get_qos_capabilities').  Should
512      * return EOPNOTSUPP if 'queue_id' is valid but may not be deleted (e.g. if
513      * 'netdev' has a fixed set of queues with the current QoS mode).
514      *
515      * May be NULL if 'netdev' does not support QoS at all, or if all of its
516      * QoS modes have fixed sets of queues. */
517     int (*delete_queue)(struct netdev *netdev, unsigned int queue_id);
518
519     /* Obtains statistics about 'queue_id' on 'netdev'.  Fills 'stats' with the
520      * queue's statistics.  May set individual members of 'stats' to all-1-bits
521      * if the statistic is unavailable.
522      *
523      * May be NULL if 'netdev' does not support QoS at all. */
524     int (*get_queue_stats)(const struct netdev *netdev, unsigned int queue_id,
525                            struct netdev_queue_stats *stats);
526
527     /* Attempts to begin dumping the queues in 'netdev'.  On success, returns 0
528      * and initializes '*statep' with any data needed for iteration.  On
529      * failure, returns a positive errno value.
530      *
531      * May be NULL if 'netdev' does not support QoS at all. */
532     int (*queue_dump_start)(const struct netdev *netdev, void **statep);
533
534     /* Attempts to retrieve another queue from 'netdev' for 'state', which was
535      * initialized by a successful call to the 'queue_dump_start' function for
536      * 'netdev'.  On success, stores a queue ID into '*queue_id' and fills
537      * 'details' with the configuration of the queue with that ID.  Returns EOF
538      * if the last queue has been dumped, or a positive errno value on error.
539      * This function will not be called again once it returns nonzero once for
540      * a given iteration (but the 'queue_dump_done' function will be called
541      * afterward).
542      *
543      * The caller initializes and clears 'details' before calling this
544      * function.  The caller takes ownership of the string key-values pairs
545      * added to 'details'.
546      *
547      * The returned contents of 'details' should be documented as valid for the
548      * given 'type' in the "other_config" column in the "Queue" table in
549      * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
550      *
551      * May be NULL if 'netdev' does not support QoS at all. */
552     int (*queue_dump_next)(const struct netdev *netdev, void *state,
553                            unsigned int *queue_id, struct smap *details);
554
555     /* Releases resources from 'netdev' for 'state', which was initialized by a
556      * successful call to the 'queue_dump_start' function for 'netdev'.
557      *
558      * May be NULL if 'netdev' does not support QoS at all. */
559     int (*queue_dump_done)(const struct netdev *netdev, void *state);
560
561     /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's
562      * ID, its statistics, and the 'aux' specified by the caller.  The order of
563      * iteration is unspecified, but (when successful) each queue must be
564      * visited exactly once.
565      *
566      * 'cb' will not modify or free the statistics passed in. */
567     int (*dump_queue_stats)(const struct netdev *netdev,
568                             void (*cb)(unsigned int queue_id,
569                                        struct netdev_queue_stats *,
570                                        void *aux),
571                             void *aux);
572
573     /* If 'netdev' has an assigned IPv4 address, sets '*address' to that
574      * address and '*netmask' to the associated netmask.
575      *
576      * The following error values have well-defined meanings:
577      *
578      *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
579      *
580      *   - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
581      *
582      * This function may be set to null if it would always return EOPNOTSUPP
583      * anyhow. */
584     int (*get_in4)(const struct netdev *netdev, struct in_addr *address,
585                    struct in_addr *netmask);
586
587     /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
588      * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.
589      *
590      * This function may be set to null if it would always return EOPNOTSUPP
591      * anyhow. */
592     int (*set_in4)(struct netdev *netdev, struct in_addr addr,
593                    struct in_addr mask);
594
595     /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address.
596      *
597      * The following error values have well-defined meanings:
598      *
599      *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
600      *
601      *   - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
602      *
603      * This function may be set to null if it would always return EOPNOTSUPP
604      * anyhow. */
605     int (*get_in6)(const struct netdev *netdev, struct in6_addr *in6);
606
607     /* Adds 'router' as a default IP gateway for the TCP/IP stack that
608      * corresponds to 'netdev'.
609      *
610      * This function may be set to null if it would always return EOPNOTSUPP
611      * anyhow. */
612     int (*add_router)(struct netdev *netdev, struct in_addr router);
613
614     /* Looks up the next hop for 'host' in the host's routing table.  If
615      * successful, stores the next hop gateway's address (0 if 'host' is on a
616      * directly connected network) in '*next_hop' and a copy of the name of the
617      * device to reach 'host' in '*netdev_name', and returns 0.  The caller is
618      * responsible for freeing '*netdev_name' (by calling free()).
619      *
620      * This function may be set to null if it would always return EOPNOTSUPP
621      * anyhow. */
622     int (*get_next_hop)(const struct in_addr *host, struct in_addr *next_hop,
623                         char **netdev_name);
624
625     /* Retrieves driver information of the device.
626      *
627      * Populates 'smap' with key-value pairs representing the status of the
628      * device.  'smap' is a set of key-value string pairs representing netdev
629      * type specific information.  For more information see
630      * ovs-vswitchd.conf.db(5).
631      *
632      * The caller is responsible for destroying 'smap' and its data.
633      *
634      * This function may be set to null if it would always return EOPNOTSUPP
635      * anyhow. */
636     int (*get_status)(const struct netdev *netdev, struct smap *smap);
637
638     /* Looks up the ARP table entry for 'ip' on 'netdev' and stores the
639      * corresponding MAC address in 'mac'.  A return value of ENXIO, in
640      * particular, indicates that there is no ARP table entry for 'ip' on
641      * 'netdev'.
642      *
643      * This function may be set to null if it would always return EOPNOTSUPP
644      * anyhow. */
645     int (*arp_lookup)(const struct netdev *netdev, ovs_be32 ip,
646                       uint8_t mac[6]);
647
648     /* Retrieves the current set of flags on 'netdev' into '*old_flags'.  Then,
649      * turns off the flags that are set to 1 in 'off' and turns on the flags
650      * that are set to 1 in 'on'.  (No bit will be set to 1 in both 'off' and
651      * 'on'; that is, off & on == 0.)
652      *
653      * This function may be invoked from a signal handler.  Therefore, it
654      * should not do anything that is not signal-safe (such as logging). */
655     int (*update_flags)(struct netdev *netdev, enum netdev_flags off,
656                         enum netdev_flags on, enum netdev_flags *old_flags);
657
658 /* ## -------------------- ## */
659 /* ## netdev_rxq Functions ## */
660 /* ## -------------------- ## */
661
662 /* If a particular netdev class does not support receiving packets, all these
663  * function pointers must be NULL. */
664
665     /* Life-cycle functions for a netdev_rxq.  See the large comment above on
666      * struct netdev_class. */
667     struct netdev_rxq *(*rxq_alloc)(void);
668     int (*rxq_construct)(struct netdev_rxq *);
669     void (*rxq_destruct)(struct netdev_rxq *);
670     void (*rxq_dealloc)(struct netdev_rxq *);
671
672     /* Attempts to receive batch of packets from 'rx' and place array of
673      * pointers into '*pkts'. netdev is responsible for allocating buffers.
674      * '*cnt' points to packet count for given batch. Once packets are returned
675      * to caller, netdev should give up ownership of ofpbuf data.
676      *
677      * Implementations should allocate buffer with DP_NETDEV_HEADROOM headroom
678      * and add a VLAN header which is obtained out-of-band to the packet.
679      *
680      * Caller is expected to pass array of size MAX_RX_BATCH.
681      * This function may be set to null if it would always return EOPNOTSUPP
682      * anyhow. */
683     int (*rxq_recv)(struct netdev_rxq *rx, struct dpif_packet **pkts,
684                     int *cnt);
685
686     /* Registers with the poll loop to wake up from the next call to
687      * poll_block() when a packet is ready to be received with netdev_rxq_recv()
688      * on 'rx'. */
689     void (*rxq_wait)(struct netdev_rxq *rx);
690
691     /* Discards all packets waiting to be received from 'rx'. */
692     int (*rxq_drain)(struct netdev_rxq *rx);
693 };
694
695 int netdev_register_provider(const struct netdev_class *);
696 int netdev_unregister_provider(const char *type);
697
698 extern const struct netdev_class netdev_linux_class;
699 extern const struct netdev_class netdev_internal_class;
700 extern const struct netdev_class netdev_tap_class;
701 #if defined(__FreeBSD__) || defined(__NetBSD__)
702 extern const struct netdev_class netdev_bsd_class;
703 #endif
704
705 #ifdef  __cplusplus
706 }
707 #endif
708
709 #endif /* netdev.h */