datapath: Move generic tunnel functions to lisp module.
[sliver-openvswitch.git] / lib / netdev.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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 <netinet/in.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "coverage.h"
28 #include "dpif.h"
29 #include "dynamic-string.h"
30 #include "fatal-signal.h"
31 #include "hash.h"
32 #include "list.h"
33 #include "netdev-provider.h"
34 #include "netdev-vport.h"
35 #include "ofpbuf.h"
36 #include "openflow/openflow.h"
37 #include "packets.h"
38 #include "poll-loop.h"
39 #include "shash.h"
40 #include "smap.h"
41 #include "sset.h"
42 #include "svec.h"
43 #include "vlog.h"
44
45 VLOG_DEFINE_THIS_MODULE(netdev);
46
47 COVERAGE_DEFINE(netdev_received);
48 COVERAGE_DEFINE(netdev_sent);
49 COVERAGE_DEFINE(netdev_add_router);
50 COVERAGE_DEFINE(netdev_get_stats);
51
52 struct netdev_saved_flags {
53     struct netdev *netdev;
54     struct list node;           /* In struct netdev's saved_flags_list. */
55     enum netdev_flags saved_flags;
56     enum netdev_flags saved_values;
57 };
58
59 /* Protects 'netdev_shash' and the mutable members of struct netdev. */
60 static struct ovs_mutex netdev_mutex = OVS_MUTEX_INITIALIZER;
61
62 /* All created network devices. */
63 static struct shash netdev_shash OVS_GUARDED_BY(netdev_mutex)
64     = SHASH_INITIALIZER(&netdev_shash);
65
66 /* Protects 'netdev_classes' against insertions or deletions.
67  *
68  * This is not an rwlock for performance reasons but to allow recursive
69  * acquisition when calling into providers.  For example, netdev_run() calls
70  * into provider 'run' functions, which might reasonably want to call one of
71  * the netdev functions that takes netdev_class_rwlock read-only. */
72 static struct ovs_rwlock netdev_class_rwlock OVS_ACQ_BEFORE(netdev_mutex)
73     = OVS_RWLOCK_INITIALIZER;
74
75 /* Contains 'struct netdev_registered_class'es. */
76 static struct hmap netdev_classes OVS_GUARDED_BY(netdev_class_rwlock)
77     = HMAP_INITIALIZER(&netdev_classes);
78
79 struct netdev_registered_class {
80     struct hmap_node hmap_node; /* In 'netdev_classes', by class->type. */
81     const struct netdev_class *class;
82     atomic_int ref_cnt;         /* Number of 'struct netdev's of this class. */
83 };
84
85 /* This is set pretty low because we probably won't learn anything from the
86  * additional log messages. */
87 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
88
89 static void restore_all_flags(void *aux OVS_UNUSED);
90 void update_device_args(struct netdev *, const struct shash *args);
91
92 static void
93 netdev_initialize(void)
94     OVS_EXCLUDED(netdev_class_rwlock, netdev_mutex)
95 {
96     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
97
98     if (ovsthread_once_start(&once)) {
99         fatal_signal_add_hook(restore_all_flags, NULL, NULL, true);
100         netdev_vport_patch_register();
101
102 #ifdef LINUX_DATAPATH
103         netdev_register_provider(&netdev_linux_class);
104         netdev_register_provider(&netdev_internal_class);
105         netdev_register_provider(&netdev_tap_class);
106         netdev_vport_tunnel_register();
107 #endif
108 #if defined(__FreeBSD__) || defined(__NetBSD__)
109         netdev_register_provider(&netdev_tap_class);
110         netdev_register_provider(&netdev_bsd_class);
111 #endif
112
113         ovsthread_once_done(&once);
114     }
115 }
116
117 /* Performs periodic work needed by all the various kinds of netdevs.
118  *
119  * If your program opens any netdevs, it must call this function within its
120  * main poll loop. */
121 void
122 netdev_run(void)
123     OVS_EXCLUDED(netdev_class_rwlock, netdev_mutex)
124 {
125     struct netdev_registered_class *rc;
126
127     ovs_rwlock_rdlock(&netdev_class_rwlock);
128     HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
129         rc->class->run();
130     }
131     ovs_rwlock_unlock(&netdev_class_rwlock);
132 }
133
134 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
135  *
136  * If your program opens any netdevs, it must call this function within its
137  * main poll loop. */
138 void
139 netdev_wait(void)
140     OVS_EXCLUDED(netdev_class_rwlock, netdev_mutex)
141 {
142     struct netdev_registered_class *rc;
143
144     ovs_rwlock_rdlock(&netdev_class_rwlock);
145     HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
146         rc->class->wait();
147     }
148     ovs_rwlock_unlock(&netdev_class_rwlock);
149 }
150
151 static struct netdev_registered_class *
152 netdev_lookup_class(const char *type)
153     OVS_REQ_RDLOCK(netdev_class_rwlock)
154 {
155     struct netdev_registered_class *rc;
156
157     HMAP_FOR_EACH_WITH_HASH (rc, hmap_node, hash_string(type, 0),
158                              &netdev_classes) {
159         if (!strcmp(type, rc->class->type)) {
160             return rc;
161         }
162     }
163     return NULL;
164 }
165
166 /* Initializes and registers a new netdev provider.  After successful
167  * registration, new netdevs of that type can be opened using netdev_open(). */
168 int
169 netdev_register_provider(const struct netdev_class *new_class)
170     OVS_EXCLUDED(netdev_class_rwlock, netdev_mutex)
171 {
172     int error;
173
174     ovs_rwlock_wrlock(&netdev_class_rwlock);
175     if (netdev_lookup_class(new_class->type)) {
176         VLOG_WARN("attempted to register duplicate netdev provider: %s",
177                    new_class->type);
178         error = EEXIST;
179     } else {
180         error = new_class->init ? new_class->init() : 0;
181         if (!error) {
182             struct netdev_registered_class *rc;
183
184             rc = xmalloc(sizeof *rc);
185             hmap_insert(&netdev_classes, &rc->hmap_node,
186                         hash_string(new_class->type, 0));
187             rc->class = new_class;
188             atomic_init(&rc->ref_cnt, 0);
189         } else {
190             VLOG_ERR("failed to initialize %s network device class: %s",
191                      new_class->type, ovs_strerror(error));
192         }
193     }
194     ovs_rwlock_unlock(&netdev_class_rwlock);
195
196     return error;
197 }
198
199 /* Unregisters a netdev provider.  'type' must have been previously
200  * registered and not currently be in use by any netdevs.  After unregistration
201  * new netdevs of that type cannot be opened using netdev_open(). */
202 int
203 netdev_unregister_provider(const char *type)
204     OVS_EXCLUDED(netdev_class_rwlock, netdev_mutex)
205 {
206     struct netdev_registered_class *rc;
207     int error;
208
209     ovs_rwlock_wrlock(&netdev_class_rwlock);
210     rc = netdev_lookup_class(type);
211     if (!rc) {
212         VLOG_WARN("attempted to unregister a netdev provider that is not "
213                   "registered: %s", type);
214         error = EAFNOSUPPORT;
215     } else {
216         int ref_cnt;
217
218         atomic_read(&rc->ref_cnt, &ref_cnt);
219         if (!ref_cnt) {
220             hmap_remove(&netdev_classes, &rc->hmap_node);
221             free(rc);
222             error = 0;
223         } else {
224             VLOG_WARN("attempted to unregister in use netdev provider: %s",
225                       type);
226             error = EBUSY;
227         }
228     }
229     ovs_rwlock_unlock(&netdev_class_rwlock);
230
231     return error;
232 }
233
234 /* Clears 'types' and enumerates the types of all currently registered netdev
235  * providers into it.  The caller must first initialize the sset. */
236 void
237 netdev_enumerate_types(struct sset *types)
238     OVS_EXCLUDED(netdev_mutex)
239 {
240     struct netdev_registered_class *rc;
241
242     netdev_initialize();
243     sset_clear(types);
244
245     ovs_rwlock_rdlock(&netdev_class_rwlock);
246     HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
247         sset_add(types, rc->class->type);
248     }
249     ovs_rwlock_unlock(&netdev_class_rwlock);
250 }
251
252 /* Check that the network device name is not the same as any of the registered
253  * vport providers' dpif_port name (dpif_port is NULL if the vport provider
254  * does not define it) or the datapath internal port name (e.g. ovs-system).
255  *
256  * Returns true if there is a name conflict, false otherwise. */
257 bool
258 netdev_is_reserved_name(const char *name)
259     OVS_EXCLUDED(netdev_mutex)
260 {
261     struct netdev_registered_class *rc;
262
263     netdev_initialize();
264
265     ovs_rwlock_rdlock(&netdev_class_rwlock);
266     HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
267         const char *dpif_port = netdev_vport_class_get_dpif_port(rc->class);
268         if (dpif_port && !strcmp(dpif_port, name)) {
269             ovs_rwlock_unlock(&netdev_class_rwlock);
270             return true;
271         }
272     }
273     ovs_rwlock_unlock(&netdev_class_rwlock);
274
275     if (!strncmp(name, "ovs-", 4)) {
276         struct sset types;
277         const char *type;
278
279         sset_init(&types);
280         dp_enumerate_types(&types);
281         SSET_FOR_EACH (type, &types) {
282             if (!strcmp(name+4, type)) {
283                 sset_destroy(&types);
284                 return true;
285             }
286         }
287         sset_destroy(&types);
288     }
289
290     return false;
291 }
292
293 /* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
294  * (e.g. "system") and returns zero if successful, otherwise a positive errno
295  * value.  On success, sets '*netdevp' to the new network device, otherwise to
296  * null.
297  *
298  * Some network devices may need to be configured (with netdev_set_config())
299  * before they can be used. */
300 int
301 netdev_open(const char *name, const char *type, struct netdev **netdevp)
302     OVS_EXCLUDED(netdev_mutex)
303 {
304     struct netdev *netdev;
305     int error;
306
307     netdev_initialize();
308
309     ovs_rwlock_rdlock(&netdev_class_rwlock);
310     ovs_mutex_lock(&netdev_mutex);
311     netdev = shash_find_data(&netdev_shash, name);
312     if (!netdev) {
313         struct netdev_registered_class *rc;
314
315         rc = netdev_lookup_class(type && type[0] ? type : "system");
316         if (rc) {
317             netdev = rc->class->alloc();
318             if (netdev) {
319                 memset(netdev, 0, sizeof *netdev);
320                 netdev->netdev_class = rc->class;
321                 netdev->name = xstrdup(name);
322                 netdev->node = shash_add(&netdev_shash, name, netdev);
323                 list_init(&netdev->saved_flags_list);
324
325                 error = rc->class->construct(netdev);
326                 if (!error) {
327                     int old_ref_cnt;
328
329                     atomic_add(&rc->ref_cnt, 1, &old_ref_cnt);
330                 } else {
331                     free(netdev->name);
332                     ovs_assert(list_is_empty(&netdev->saved_flags_list));
333                     shash_delete(&netdev_shash, netdev->node);
334                     rc->class->dealloc(netdev);
335                 }
336             } else {
337                 error = ENOMEM;
338             }
339         } else {
340             VLOG_WARN("could not create netdev %s of unknown type %s",
341                       name, type);
342             error = EAFNOSUPPORT;
343         }
344     } else {
345         error = 0;
346     }
347
348     ovs_mutex_unlock(&netdev_mutex);
349     ovs_rwlock_unlock(&netdev_class_rwlock);
350
351     if (!error) {
352         netdev->ref_cnt++;
353         *netdevp = netdev;
354     } else {
355         *netdevp = NULL;
356     }
357     return error;
358 }
359
360 /* Returns a reference to 'netdev_' for the caller to own. Returns null if
361  * 'netdev_' is null. */
362 struct netdev *
363 netdev_ref(const struct netdev *netdev_)
364     OVS_EXCLUDED(netdev_mutex)
365 {
366     struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
367
368     if (netdev) {
369         ovs_mutex_lock(&netdev_mutex);
370         ovs_assert(netdev->ref_cnt > 0);
371         netdev->ref_cnt++;
372         ovs_mutex_unlock(&netdev_mutex);
373     }
374     return netdev;
375 }
376
377 /* Reconfigures the device 'netdev' with 'args'.  'args' may be empty
378  * or NULL if none are needed. */
379 int
380 netdev_set_config(struct netdev *netdev, const struct smap *args)
381     OVS_EXCLUDED(netdev_mutex)
382 {
383     if (netdev->netdev_class->set_config) {
384         const struct smap no_args = SMAP_INITIALIZER(&no_args);
385         return netdev->netdev_class->set_config(netdev,
386                                                 args ? args : &no_args);
387     } else if (args && !smap_is_empty(args)) {
388         VLOG_WARN("%s: arguments provided to device that is not configurable",
389                   netdev_get_name(netdev));
390     }
391
392     return 0;
393 }
394
395 /* Returns the current configuration for 'netdev' in 'args'.  The caller must
396  * have already initialized 'args' with smap_init().  Returns 0 on success, in
397  * which case 'args' will be filled with 'netdev''s configuration.  On failure
398  * returns a positive errno value, in which case 'args' will be empty.
399  *
400  * The caller owns 'args' and its contents and must eventually free them with
401  * smap_destroy(). */
402 int
403 netdev_get_config(const struct netdev *netdev, struct smap *args)
404     OVS_EXCLUDED(netdev_mutex)
405 {
406     int error;
407
408     smap_clear(args);
409     if (netdev->netdev_class->get_config) {
410         error = netdev->netdev_class->get_config(netdev, args);
411         if (error) {
412             smap_clear(args);
413         }
414     } else {
415         error = 0;
416     }
417
418     return error;
419 }
420
421 const struct netdev_tunnel_config *
422 netdev_get_tunnel_config(const struct netdev *netdev)
423     OVS_EXCLUDED(netdev_mutex)
424 {
425     if (netdev->netdev_class->get_tunnel_config) {
426         return netdev->netdev_class->get_tunnel_config(netdev);
427     } else {
428         return NULL;
429     }
430 }
431
432 static void
433 netdev_unref(struct netdev *dev)
434     OVS_RELEASES(netdev_mutex)
435 {
436     ovs_assert(dev->ref_cnt);
437     if (!--dev->ref_cnt) {
438         const struct netdev_class *class = dev->netdev_class;
439         struct netdev_registered_class *rc;
440         int old_ref_cnt;
441
442         dev->netdev_class->destruct(dev);
443
444         shash_delete(&netdev_shash, dev->node);
445         free(dev->name);
446         dev->netdev_class->dealloc(dev);
447         ovs_mutex_unlock(&netdev_mutex);
448
449         ovs_rwlock_rdlock(&netdev_class_rwlock);
450         rc = netdev_lookup_class(class->type);
451         atomic_sub(&rc->ref_cnt, 1, &old_ref_cnt);
452         ovs_assert(old_ref_cnt > 0);
453         ovs_rwlock_unlock(&netdev_class_rwlock);
454     } else {
455         ovs_mutex_unlock(&netdev_mutex);
456     }
457 }
458
459 /* Closes and destroys 'netdev'. */
460 void
461 netdev_close(struct netdev *netdev)
462     OVS_EXCLUDED(netdev_mutex)
463 {
464     if (netdev) {
465         ovs_mutex_lock(&netdev_mutex);
466         netdev_unref(netdev);
467     }
468 }
469
470 /* Parses 'netdev_name_', which is of the form [type@]name into its component
471  * pieces.  'name' and 'type' must be freed by the caller. */
472 void
473 netdev_parse_name(const char *netdev_name_, char **name, char **type)
474 {
475     char *netdev_name = xstrdup(netdev_name_);
476     char *separator;
477
478     separator = strchr(netdev_name, '@');
479     if (separator) {
480         *separator = '\0';
481         *type = netdev_name;
482         *name = xstrdup(separator + 1);
483     } else {
484         *name = netdev_name;
485         *type = xstrdup("system");
486     }
487 }
488
489 int
490 netdev_rx_open(struct netdev *netdev, struct netdev_rx **rxp)
491     OVS_EXCLUDED(netdev_mutex)
492 {
493     int error;
494
495     if (netdev->netdev_class->rx_alloc) {
496         struct netdev_rx *rx = netdev->netdev_class->rx_alloc();
497         if (rx) {
498             rx->netdev = netdev;
499             error = netdev->netdev_class->rx_construct(rx);
500             if (!error) {
501                 ovs_mutex_lock(&netdev_mutex);
502                 netdev->ref_cnt++;
503                 ovs_mutex_unlock(&netdev_mutex);
504
505                 *rxp = rx;
506                 return 0;
507             }
508             netdev->netdev_class->rx_dealloc(rx);
509         } else {
510             error = ENOMEM;
511         }
512     } else {
513         error = EOPNOTSUPP;
514     }
515
516     *rxp = NULL;
517     return error;
518 }
519
520 void
521 netdev_rx_close(struct netdev_rx *rx)
522     OVS_EXCLUDED(netdev_mutex)
523 {
524     if (rx) {
525         struct netdev *netdev = rx->netdev;
526         netdev->netdev_class->rx_destruct(rx);
527         netdev->netdev_class->rx_dealloc(rx);
528         netdev_close(netdev);
529     }
530 }
531
532 int
533 netdev_rx_recv(struct netdev_rx *rx, struct ofpbuf *buffer)
534 {
535     int retval;
536
537     ovs_assert(buffer->size == 0);
538     ovs_assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
539
540     retval = rx->netdev->netdev_class->rx_recv(rx, buffer->data,
541                                                ofpbuf_tailroom(buffer));
542     if (retval >= 0) {
543         COVERAGE_INC(netdev_received);
544         buffer->size += retval;
545         if (buffer->size < ETH_TOTAL_MIN) {
546             ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
547         }
548         return 0;
549     } else {
550         return -retval;
551     }
552 }
553
554 void
555 netdev_rx_wait(struct netdev_rx *rx)
556 {
557     rx->netdev->netdev_class->rx_wait(rx);
558 }
559
560 int
561 netdev_rx_drain(struct netdev_rx *rx)
562 {
563     return (rx->netdev->netdev_class->rx_drain
564             ? rx->netdev->netdev_class->rx_drain(rx)
565             : 0);
566 }
567
568 /* Sends 'buffer' on 'netdev'.  Returns 0 if successful, otherwise a positive
569  * errno value.  Returns EAGAIN without blocking if the packet cannot be queued
570  * immediately.  Returns EMSGSIZE if a partial packet was transmitted or if
571  * the packet is too big or too small to transmit on the device.
572  *
573  * The caller retains ownership of 'buffer' in all cases.
574  *
575  * The kernel maintains a packet transmission queue, so the caller is not
576  * expected to do additional queuing of packets.
577  *
578  * Some network devices may not implement support for this function.  In such
579  * cases this function will always return EOPNOTSUPP. */
580 int
581 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
582 {
583     int error;
584
585     error = (netdev->netdev_class->send
586              ? netdev->netdev_class->send(netdev, buffer->data, buffer->size)
587              : EOPNOTSUPP);
588     if (!error) {
589         COVERAGE_INC(netdev_sent);
590     }
591     return error;
592 }
593
594 /* Registers with the poll loop to wake up from the next call to poll_block()
595  * when the packet transmission queue has sufficient room to transmit a packet
596  * with netdev_send().
597  *
598  * The kernel maintains a packet transmission queue, so the client is not
599  * expected to do additional queuing of packets.  Thus, this function is
600  * unlikely to ever be used.  It is included for completeness. */
601 void
602 netdev_send_wait(struct netdev *netdev)
603 {
604     if (netdev->netdev_class->send_wait) {
605         netdev->netdev_class->send_wait(netdev);
606     }
607 }
608
609 /* Attempts to set 'netdev''s MAC address to 'mac'.  Returns 0 if successful,
610  * otherwise a positive errno value. */
611 int
612 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
613 {
614     return netdev->netdev_class->set_etheraddr(netdev, mac);
615 }
616
617 /* Retrieves 'netdev''s MAC address.  If successful, returns 0 and copies the
618  * the MAC address into 'mac'.  On failure, returns a positive errno value and
619  * clears 'mac' to all-zeros. */
620 int
621 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
622 {
623     return netdev->netdev_class->get_etheraddr(netdev, mac);
624 }
625
626 /* Returns the name of the network device that 'netdev' represents,
627  * e.g. "eth0".  The caller must not modify or free the returned string. */
628 const char *
629 netdev_get_name(const struct netdev *netdev)
630 {
631     return netdev->name;
632 }
633
634 /* Retrieves the MTU of 'netdev'.  The MTU is the maximum size of transmitted
635  * (and received) packets, in bytes, not including the hardware header; thus,
636  * this is typically 1500 bytes for Ethernet devices.
637  *
638  * If successful, returns 0 and stores the MTU size in '*mtup'.  Returns
639  * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
640  * On other failure, returns a positive errno value.  On failure, sets '*mtup'
641  * to 0. */
642 int
643 netdev_get_mtu(const struct netdev *netdev, int *mtup)
644 {
645     const struct netdev_class *class = netdev->netdev_class;
646     int error;
647
648     error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP;
649     if (error) {
650         *mtup = 0;
651         if (error != EOPNOTSUPP) {
652             VLOG_DBG_RL(&rl, "failed to retrieve MTU for network device %s: "
653                          "%s", netdev_get_name(netdev), ovs_strerror(error));
654         }
655     }
656     return error;
657 }
658
659 /* Sets the MTU of 'netdev'.  The MTU is the maximum size of transmitted
660  * (and received) packets, in bytes.
661  *
662  * If successful, returns 0.  Returns EOPNOTSUPP if 'netdev' does not have an
663  * MTU (as e.g. some tunnels do not).  On other failure, returns a positive
664  * errno value. */
665 int
666 netdev_set_mtu(const struct netdev *netdev, int mtu)
667 {
668     const struct netdev_class *class = netdev->netdev_class;
669     int error;
670
671     error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP;
672     if (error && error != EOPNOTSUPP) {
673         VLOG_DBG_RL(&rl, "failed to set MTU for network device %s: %s",
674                      netdev_get_name(netdev), ovs_strerror(error));
675     }
676
677     return error;
678 }
679
680 /* Returns the ifindex of 'netdev', if successful, as a positive number.  On
681  * failure, returns a negative errno value.
682  *
683  * The desired semantics of the ifindex value are a combination of those
684  * specified by POSIX for if_nametoindex() and by SNMP for ifIndex.  An ifindex
685  * value should be unique within a host and remain stable at least until
686  * reboot.  SNMP says an ifindex "ranges between 1 and the value of ifNumber"
687  * but many systems do not follow this rule anyhow.
688  *
689  * Some network devices may not implement support for this function.  In such
690  * cases this function will always return -EOPNOTSUPP.
691  */
692 int
693 netdev_get_ifindex(const struct netdev *netdev)
694 {
695     int (*get_ifindex)(const struct netdev *);
696
697     get_ifindex = netdev->netdev_class->get_ifindex;
698
699     return get_ifindex ? get_ifindex(netdev) : -EOPNOTSUPP;
700 }
701
702 /* Stores the features supported by 'netdev' into each of '*current',
703  * '*advertised', '*supported', and '*peer' that are non-null.  Each value is a
704  * bitmap of "enum ofp_port_features" bits, in host byte order.  Returns 0 if
705  * successful, otherwise a positive errno value.  On failure, all of the
706  * passed-in values are set to 0.
707  *
708  * Some network devices may not implement support for this function.  In such
709  * cases this function will always return EOPNOTSUPP. */
710 int
711 netdev_get_features(const struct netdev *netdev,
712                     enum netdev_features *current,
713                     enum netdev_features *advertised,
714                     enum netdev_features *supported,
715                     enum netdev_features *peer)
716 {
717     int (*get_features)(const struct netdev *netdev,
718                         enum netdev_features *current,
719                         enum netdev_features *advertised,
720                         enum netdev_features *supported,
721                         enum netdev_features *peer);
722     enum netdev_features dummy[4];
723     int error;
724
725     if (!current) {
726         current = &dummy[0];
727     }
728     if (!advertised) {
729         advertised = &dummy[1];
730     }
731     if (!supported) {
732         supported = &dummy[2];
733     }
734     if (!peer) {
735         peer = &dummy[3];
736     }
737
738     get_features = netdev->netdev_class->get_features;
739     error = get_features
740                     ? get_features(netdev, current, advertised, supported,
741                                    peer)
742                     : EOPNOTSUPP;
743     if (error) {
744         *current = *advertised = *supported = *peer = 0;
745     }
746     return error;
747 }
748
749 /* Returns the maximum speed of a network connection that has the NETDEV_F_*
750  * bits in 'features', in bits per second.  If no bits that indicate a speed
751  * are set in 'features', returns 'default_bps'. */
752 uint64_t
753 netdev_features_to_bps(enum netdev_features features,
754                        uint64_t default_bps)
755 {
756     enum {
757         F_1000000MB = NETDEV_F_1TB_FD,
758         F_100000MB = NETDEV_F_100GB_FD,
759         F_40000MB = NETDEV_F_40GB_FD,
760         F_10000MB = NETDEV_F_10GB_FD,
761         F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
762         F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
763         F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
764     };
765
766     return (  features & F_1000000MB ? UINT64_C(1000000000000)
767             : features & F_100000MB  ? UINT64_C(100000000000)
768             : features & F_40000MB   ? UINT64_C(40000000000)
769             : features & F_10000MB   ? UINT64_C(10000000000)
770             : features & F_1000MB    ? UINT64_C(1000000000)
771             : features & F_100MB     ? UINT64_C(100000000)
772             : features & F_10MB      ? UINT64_C(10000000)
773                                      : default_bps);
774 }
775
776 /* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
777  * are set in 'features', otherwise false. */
778 bool
779 netdev_features_is_full_duplex(enum netdev_features features)
780 {
781     return (features & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD | NETDEV_F_1GB_FD
782                         | NETDEV_F_10GB_FD | NETDEV_F_40GB_FD
783                         | NETDEV_F_100GB_FD | NETDEV_F_1TB_FD)) != 0;
784 }
785
786 /* Set the features advertised by 'netdev' to 'advertise'.  Returns 0 if
787  * successful, otherwise a positive errno value. */
788 int
789 netdev_set_advertisements(struct netdev *netdev,
790                           enum netdev_features advertise)
791 {
792     return (netdev->netdev_class->set_advertisements
793             ? netdev->netdev_class->set_advertisements(
794                     netdev, advertise)
795             : EOPNOTSUPP);
796 }
797
798 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
799  * and '*netmask' to its netmask and returns 0.  Otherwise, returns a positive
800  * errno value and sets '*address' to 0 (INADDR_ANY).
801  *
802  * The following error values have well-defined meanings:
803  *
804  *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
805  *
806  *   - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
807  *
808  * 'address' or 'netmask' or both may be null, in which case the address or
809  * netmask is not reported. */
810 int
811 netdev_get_in4(const struct netdev *netdev,
812                struct in_addr *address_, struct in_addr *netmask_)
813 {
814     struct in_addr address;
815     struct in_addr netmask;
816     int error;
817
818     error = (netdev->netdev_class->get_in4
819              ? netdev->netdev_class->get_in4(netdev,
820                     &address, &netmask)
821              : EOPNOTSUPP);
822     if (address_) {
823         address_->s_addr = error ? 0 : address.s_addr;
824     }
825     if (netmask_) {
826         netmask_->s_addr = error ? 0 : netmask.s_addr;
827     }
828     return error;
829 }
830
831 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
832  * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.  Returns a
833  * positive errno value. */
834 int
835 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
836 {
837     return (netdev->netdev_class->set_in4
838             ? netdev->netdev_class->set_in4(netdev, addr, mask)
839             : EOPNOTSUPP);
840 }
841
842 /* Obtains ad IPv4 address from device name and save the address in
843  * in4.  Returns 0 if successful, otherwise a positive errno value.
844  */
845 int
846 netdev_get_in4_by_name(const char *device_name, struct in_addr *in4)
847 {
848     struct netdev *netdev;
849     int error;
850
851     error = netdev_open(device_name, "system", &netdev);
852     if (error) {
853         in4->s_addr = htonl(0);
854         return error;
855     }
856
857     error = netdev_get_in4(netdev, in4, NULL);
858     netdev_close(netdev);
859     return error;
860 }
861
862 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
863  * to 'netdev'. */
864 int
865 netdev_add_router(struct netdev *netdev, struct in_addr router)
866 {
867     COVERAGE_INC(netdev_add_router);
868     return (netdev->netdev_class->add_router
869             ? netdev->netdev_class->add_router(netdev, router)
870             : EOPNOTSUPP);
871 }
872
873 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
874  * 'netdev'.  If a route cannot not be determined, sets '*next_hop' to 0,
875  * '*netdev_name' to null, and returns a positive errno value.  Otherwise, if a
876  * next hop is found, stores the next hop gateway's address (0 if 'host' is on
877  * a directly connected network) in '*next_hop' and a copy of the name of the
878  * device to reach 'host' in '*netdev_name', and returns 0.  The caller is
879  * responsible for freeing '*netdev_name' (by calling free()). */
880 int
881 netdev_get_next_hop(const struct netdev *netdev,
882                     const struct in_addr *host, struct in_addr *next_hop,
883                     char **netdev_name)
884 {
885     int error = (netdev->netdev_class->get_next_hop
886                  ? netdev->netdev_class->get_next_hop(
887                         host, next_hop, netdev_name)
888                  : EOPNOTSUPP);
889     if (error) {
890         next_hop->s_addr = 0;
891         *netdev_name = NULL;
892     }
893     return error;
894 }
895
896 /* Populates 'smap' with status information.
897  *
898  * Populates 'smap' with 'netdev' specific status information.  This
899  * information may be used to populate the status column of the Interface table
900  * as defined in ovs-vswitchd.conf.db(5). */
901 int
902 netdev_get_status(const struct netdev *netdev, struct smap *smap)
903 {
904     return (netdev->netdev_class->get_status
905             ? netdev->netdev_class->get_status(netdev, smap)
906             : EOPNOTSUPP);
907 }
908
909 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
910  * returns 0.  Otherwise, returns a positive errno value and sets '*in6' to
911  * all-zero-bits (in6addr_any).
912  *
913  * The following error values have well-defined meanings:
914  *
915  *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
916  *
917  *   - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
918  *
919  * 'in6' may be null, in which case the address itself is not reported. */
920 int
921 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
922 {
923     struct in6_addr dummy;
924     int error;
925
926     error = (netdev->netdev_class->get_in6
927              ? netdev->netdev_class->get_in6(netdev,
928                     in6 ? in6 : &dummy)
929              : EOPNOTSUPP);
930     if (error && in6) {
931         memset(in6, 0, sizeof *in6);
932     }
933     return error;
934 }
935
936 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
937  * 'on'.  Returns 0 if successful, otherwise a positive errno value. */
938 static int
939 do_update_flags(struct netdev *netdev, enum netdev_flags off,
940                 enum netdev_flags on, enum netdev_flags *old_flagsp,
941                 struct netdev_saved_flags **sfp)
942     OVS_EXCLUDED(netdev_mutex)
943 {
944     struct netdev_saved_flags *sf = NULL;
945     enum netdev_flags old_flags;
946     int error;
947
948     error = netdev->netdev_class->update_flags(netdev, off & ~on, on,
949                                                &old_flags);
950     if (error) {
951         VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
952                      off || on ? "set" : "get", netdev_get_name(netdev),
953                      ovs_strerror(error));
954         old_flags = 0;
955     } else if ((off || on) && sfp) {
956         enum netdev_flags new_flags = (old_flags & ~off) | on;
957         enum netdev_flags changed_flags = old_flags ^ new_flags;
958         if (changed_flags) {
959             ovs_mutex_lock(&netdev_mutex);
960             *sfp = sf = xmalloc(sizeof *sf);
961             sf->netdev = netdev;
962             list_push_front(&netdev->saved_flags_list, &sf->node);
963             sf->saved_flags = changed_flags;
964             sf->saved_values = changed_flags & new_flags;
965
966             netdev->ref_cnt++;
967             ovs_mutex_unlock(&netdev_mutex);
968         }
969     }
970
971     if (old_flagsp) {
972         *old_flagsp = old_flags;
973     }
974     if (sfp) {
975         *sfp = sf;
976     }
977
978     return error;
979 }
980
981 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
982  * Returns 0 if successful, otherwise a positive errno value.  On failure,
983  * stores 0 into '*flagsp'. */
984 int
985 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
986 {
987     struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
988     return do_update_flags(netdev, 0, 0, flagsp, NULL);
989 }
990
991 /* Sets the flags for 'netdev' to 'flags'.
992  * Returns 0 if successful, otherwise a positive errno value. */
993 int
994 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
995                  struct netdev_saved_flags **sfp)
996 {
997     return do_update_flags(netdev, -1, flags, NULL, sfp);
998 }
999
1000 /* Turns on the specified 'flags' on 'netdev':
1001  *
1002  *    - On success, returns 0.  If 'sfp' is nonnull, sets '*sfp' to a newly
1003  *      allocated 'struct netdev_saved_flags *' that may be passed to
1004  *      netdev_restore_flags() to restore the original values of 'flags' on
1005  *      'netdev' (this will happen automatically at program termination if
1006  *      netdev_restore_flags() is never called) , or to NULL if no flags were
1007  *      actually changed.
1008  *
1009  *    - On failure, returns a positive errno value.  If 'sfp' is nonnull, sets
1010  *      '*sfp' to NULL. */
1011 int
1012 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
1013                      struct netdev_saved_flags **sfp)
1014 {
1015     return do_update_flags(netdev, 0, flags, NULL, sfp);
1016 }
1017
1018 /* Turns off the specified 'flags' on 'netdev'.  See netdev_turn_flags_on() for
1019  * details of the interface. */
1020 int
1021 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
1022                       struct netdev_saved_flags **sfp)
1023 {
1024     return do_update_flags(netdev, flags, 0, NULL, sfp);
1025 }
1026
1027 /* Restores the flags that were saved in 'sf', and destroys 'sf'.
1028  * Does nothing if 'sf' is NULL. */
1029 void
1030 netdev_restore_flags(struct netdev_saved_flags *sf)
1031     OVS_EXCLUDED(netdev_mutex)
1032 {
1033     if (sf) {
1034         struct netdev *netdev = sf->netdev;
1035         enum netdev_flags old_flags;
1036
1037         netdev->netdev_class->update_flags(netdev,
1038                                            sf->saved_flags & sf->saved_values,
1039                                            sf->saved_flags & ~sf->saved_values,
1040                                            &old_flags);
1041
1042         ovs_mutex_lock(&netdev_mutex);
1043         list_remove(&sf->node);
1044         free(sf);
1045         netdev_unref(netdev);
1046     }
1047 }
1048
1049 /* Looks up the ARP table entry for 'ip' on 'netdev'.  If one exists and can be
1050  * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1051  * returns 0.  Otherwise, it returns a positive errno value; in particular,
1052  * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
1053 int
1054 netdev_arp_lookup(const struct netdev *netdev,
1055                   ovs_be32 ip, uint8_t mac[ETH_ADDR_LEN])
1056 {
1057     int error = (netdev->netdev_class->arp_lookup
1058                  ? netdev->netdev_class->arp_lookup(netdev, ip, mac)
1059                  : EOPNOTSUPP);
1060     if (error) {
1061         memset(mac, 0, ETH_ADDR_LEN);
1062     }
1063     return error;
1064 }
1065
1066 /* Returns true if carrier is active (link light is on) on 'netdev'. */
1067 bool
1068 netdev_get_carrier(const struct netdev *netdev)
1069 {
1070     int error;
1071     enum netdev_flags flags;
1072     bool carrier;
1073
1074     netdev_get_flags(netdev, &flags);
1075     if (!(flags & NETDEV_UP)) {
1076         return false;
1077     }
1078
1079     if (!netdev->netdev_class->get_carrier) {
1080         return true;
1081     }
1082
1083     error = netdev->netdev_class->get_carrier(netdev, &carrier);
1084     if (error) {
1085         VLOG_DBG("%s: failed to get network device carrier status, assuming "
1086                  "down: %s", netdev_get_name(netdev), ovs_strerror(error));
1087         carrier = false;
1088     }
1089
1090     return carrier;
1091 }
1092
1093 /* Returns the number of times 'netdev''s carrier has changed. */
1094 long long int
1095 netdev_get_carrier_resets(const struct netdev *netdev)
1096 {
1097     return (netdev->netdev_class->get_carrier_resets
1098             ? netdev->netdev_class->get_carrier_resets(netdev)
1099             : 0);
1100 }
1101
1102 /* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
1103  * link status instead of checking 'netdev''s carrier.  'netdev''s MII
1104  * registers will be polled once ever 'interval' milliseconds.  If 'netdev'
1105  * does not support MII, another method may be used as a fallback.  If
1106  * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
1107  * its normal behavior.
1108  *
1109  * Returns 0 if successful, otherwise a positive errno value. */
1110 int
1111 netdev_set_miimon_interval(struct netdev *netdev, long long int interval)
1112 {
1113     return (netdev->netdev_class->set_miimon_interval
1114             ? netdev->netdev_class->set_miimon_interval(netdev, interval)
1115             : EOPNOTSUPP);
1116 }
1117
1118 /* Retrieves current device stats for 'netdev'. */
1119 int
1120 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1121 {
1122     int error;
1123
1124     COVERAGE_INC(netdev_get_stats);
1125     error = (netdev->netdev_class->get_stats
1126              ? netdev->netdev_class->get_stats(netdev, stats)
1127              : EOPNOTSUPP);
1128     if (error) {
1129         memset(stats, 0xff, sizeof *stats);
1130     }
1131     return error;
1132 }
1133
1134 /* Attempts to change the stats for 'netdev' to those provided in 'stats'.
1135  * Returns 0 if successful, otherwise a positive errno value.
1136  *
1137  * This will probably fail for most network devices.  Some devices might only
1138  * allow setting their stats to 0. */
1139 int
1140 netdev_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
1141 {
1142     return (netdev->netdev_class->set_stats
1143              ? netdev->netdev_class->set_stats(netdev, stats)
1144              : EOPNOTSUPP);
1145 }
1146
1147 /* Attempts to set input rate limiting (policing) policy, such that up to
1148  * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
1149  * size of 'kbits' kb. */
1150 int
1151 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1152                     uint32_t kbits_burst)
1153 {
1154     return (netdev->netdev_class->set_policing
1155             ? netdev->netdev_class->set_policing(netdev,
1156                     kbits_rate, kbits_burst)
1157             : EOPNOTSUPP);
1158 }
1159
1160 /* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1161  * empty if 'netdev' does not support QoS.  Any names added to 'types' should
1162  * be documented as valid for the "type" column in the "QoS" table in
1163  * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1164  *
1165  * Every network device supports disabling QoS with a type of "", but this type
1166  * will not be added to 'types'.
1167  *
1168  * The caller must initialize 'types' (e.g. with sset_init()) before calling
1169  * this function.  The caller is responsible for destroying 'types' (e.g. with
1170  * sset_destroy()) when it is no longer needed.
1171  *
1172  * Returns 0 if successful, otherwise a positive errno value. */
1173 int
1174 netdev_get_qos_types(const struct netdev *netdev, struct sset *types)
1175 {
1176     const struct netdev_class *class = netdev->netdev_class;
1177     return (class->get_qos_types
1178             ? class->get_qos_types(netdev, types)
1179             : 0);
1180 }
1181
1182 /* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1183  * which should be "" or one of the types returned by netdev_get_qos_types()
1184  * for 'netdev'.  Returns 0 if successful, otherwise a positive errno value.
1185  * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1186  * 'caps' to all zeros. */
1187 int
1188 netdev_get_qos_capabilities(const struct netdev *netdev, const char *type,
1189                             struct netdev_qos_capabilities *caps)
1190 {
1191     const struct netdev_class *class = netdev->netdev_class;
1192
1193     if (*type) {
1194         int retval = (class->get_qos_capabilities
1195                       ? class->get_qos_capabilities(netdev, type, caps)
1196                       : EOPNOTSUPP);
1197         if (retval) {
1198             memset(caps, 0, sizeof *caps);
1199         }
1200         return retval;
1201     } else {
1202         /* Every netdev supports turning off QoS. */
1203         memset(caps, 0, sizeof *caps);
1204         return 0;
1205     }
1206 }
1207
1208 /* Obtains the number of queues supported by 'netdev' for the specified 'type'
1209  * of QoS.  Returns 0 if successful, otherwise a positive errno value.  Stores
1210  * the number of queues (zero on failure) in '*n_queuesp'.
1211  *
1212  * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1213 int
1214 netdev_get_n_queues(const struct netdev *netdev,
1215                     const char *type, unsigned int *n_queuesp)
1216 {
1217     struct netdev_qos_capabilities caps;
1218     int retval;
1219
1220     retval = netdev_get_qos_capabilities(netdev, type, &caps);
1221     *n_queuesp = caps.n_queues;
1222     return retval;
1223 }
1224
1225 /* Queries 'netdev' about its currently configured form of QoS.  If successful,
1226  * stores the name of the current form of QoS into '*typep', stores any details
1227  * of configuration as string key-value pairs in 'details', and returns 0.  On
1228  * failure, sets '*typep' to NULL and returns a positive errno value.
1229  *
1230  * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1231  *
1232  * The caller must initialize 'details' as an empty smap (e.g. with
1233  * smap_init()) before calling this function.  The caller must free 'details'
1234  * when it is no longer needed (e.g. with smap_destroy()).
1235  *
1236  * The caller must not modify or free '*typep'.
1237  *
1238  * '*typep' will be one of the types returned by netdev_get_qos_types() for
1239  * 'netdev'.  The contents of 'details' should be documented as valid for
1240  * '*typep' in the "other_config" column in the "QoS" table in
1241  * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1242 int
1243 netdev_get_qos(const struct netdev *netdev,
1244                const char **typep, struct smap *details)
1245 {
1246     const struct netdev_class *class = netdev->netdev_class;
1247     int retval;
1248
1249     if (class->get_qos) {
1250         retval = class->get_qos(netdev, typep, details);
1251         if (retval) {
1252             *typep = NULL;
1253             smap_clear(details);
1254         }
1255         return retval;
1256     } else {
1257         /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1258         *typep = "";
1259         return 0;
1260     }
1261 }
1262
1263 /* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1264  * with details of configuration from 'details'.  Returns 0 if successful,
1265  * otherwise a positive errno value.  On error, the previous QoS configuration
1266  * is retained.
1267  *
1268  * When this function changes the type of QoS (not just 'details'), this also
1269  * resets all queue configuration for 'netdev' to their defaults (which depend
1270  * on the specific type of QoS).  Otherwise, the queue configuration for
1271  * 'netdev' is unchanged.
1272  *
1273  * 'type' should be "" (to disable QoS) or one of the types returned by
1274  * netdev_get_qos_types() for 'netdev'.  The contents of 'details' should be
1275  * documented as valid for the given 'type' in the "other_config" column in the
1276  * "QoS" table in vswitchd/vswitch.xml (which is built as
1277  * ovs-vswitchd.conf.db(8)).
1278  *
1279  * NULL may be specified for 'details' if there are no configuration
1280  * details. */
1281 int
1282 netdev_set_qos(struct netdev *netdev,
1283                const char *type, const struct smap *details)
1284 {
1285     const struct netdev_class *class = netdev->netdev_class;
1286
1287     if (!type) {
1288         type = "";
1289     }
1290
1291     if (class->set_qos) {
1292         if (!details) {
1293             static const struct smap empty = SMAP_INITIALIZER(&empty);
1294             details = &empty;
1295         }
1296         return class->set_qos(netdev, type, details);
1297     } else {
1298         return *type ? EOPNOTSUPP : 0;
1299     }
1300 }
1301
1302 /* Queries 'netdev' for information about the queue numbered 'queue_id'.  If
1303  * successful, adds that information as string key-value pairs to 'details'.
1304  * Returns 0 if successful, otherwise a positive errno value.
1305  *
1306  * 'queue_id' must be less than the number of queues supported by 'netdev' for
1307  * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1308  *
1309  * The returned contents of 'details' should be documented as valid for the
1310  * given 'type' in the "other_config" column in the "Queue" table in
1311  * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1312  *
1313  * The caller must initialize 'details' (e.g. with smap_init()) before calling
1314  * this function.  The caller must free 'details' when it is no longer needed
1315  * (e.g. with smap_destroy()). */
1316 int
1317 netdev_get_queue(const struct netdev *netdev,
1318                  unsigned int queue_id, struct smap *details)
1319 {
1320     const struct netdev_class *class = netdev->netdev_class;
1321     int retval;
1322
1323     retval = (class->get_queue
1324               ? class->get_queue(netdev, queue_id, details)
1325               : EOPNOTSUPP);
1326     if (retval) {
1327         smap_clear(details);
1328     }
1329     return retval;
1330 }
1331
1332 /* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1333  * string pairs in 'details'.  The contents of 'details' should be documented
1334  * as valid for the given 'type' in the "other_config" column in the "Queue"
1335  * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1336  * Returns 0 if successful, otherwise a positive errno value.  On failure, the
1337  * given queue's configuration should be unmodified.
1338  *
1339  * 'queue_id' must be less than the number of queues supported by 'netdev' for
1340  * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1341  *
1342  * This function does not modify 'details', and the caller retains ownership of
1343  * it. */
1344 int
1345 netdev_set_queue(struct netdev *netdev,
1346                  unsigned int queue_id, const struct smap *details)
1347 {
1348     const struct netdev_class *class = netdev->netdev_class;
1349     return (class->set_queue
1350             ? class->set_queue(netdev, queue_id, details)
1351             : EOPNOTSUPP);
1352 }
1353
1354 /* Attempts to delete the queue numbered 'queue_id' from 'netdev'.  Some kinds
1355  * of QoS may have a fixed set of queues, in which case attempts to delete them
1356  * will fail with EOPNOTSUPP.
1357  *
1358  * Returns 0 if successful, otherwise a positive errno value.  On failure, the
1359  * given queue will be unmodified.
1360  *
1361  * 'queue_id' must be less than the number of queues supported by 'netdev' for
1362  * the current form of QoS (e.g. as returned by
1363  * netdev_get_n_queues(netdev)). */
1364 int
1365 netdev_delete_queue(struct netdev *netdev, unsigned int queue_id)
1366 {
1367     const struct netdev_class *class = netdev->netdev_class;
1368     return (class->delete_queue
1369             ? class->delete_queue(netdev, queue_id)
1370             : EOPNOTSUPP);
1371 }
1372
1373 /* Obtains statistics about 'queue_id' on 'netdev'.  On success, returns 0 and
1374  * fills 'stats' with the queue's statistics; individual members of 'stats' may
1375  * be set to all-1-bits if the statistic is unavailable.  On failure, returns a
1376  * positive errno value and fills 'stats' with values indicating unsupported
1377  * statistics. */
1378 int
1379 netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
1380                        struct netdev_queue_stats *stats)
1381 {
1382     const struct netdev_class *class = netdev->netdev_class;
1383     int retval;
1384
1385     retval = (class->get_queue_stats
1386               ? class->get_queue_stats(netdev, queue_id, stats)
1387               : EOPNOTSUPP);
1388     if (retval) {
1389         stats->tx_bytes = UINT64_MAX;
1390         stats->tx_packets = UINT64_MAX;
1391         stats->tx_errors = UINT64_MAX;
1392         stats->created = LLONG_MIN;
1393     }
1394     return retval;
1395 }
1396
1397 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1398  * its configuration, and the 'aux' specified by the caller.  The order of
1399  * iteration is unspecified, but (when successful) each queue is visited
1400  * exactly once.
1401  *
1402  * Calling this function may be more efficient than calling netdev_get_queue()
1403  * for every queue.
1404  *
1405  * 'cb' must not modify or free the 'details' argument passed in.  It may
1406  * delete or modify the queue passed in as its 'queue_id' argument.  It may
1407  * modify but must not delete any other queue within 'netdev'.  'cb' should not
1408  * add new queues because this may cause some queues to be visited twice or not
1409  * at all.
1410  *
1411  * Returns 0 if successful, otherwise a positive errno value.  On error, some
1412  * configured queues may not have been included in the iteration. */
1413 int
1414 netdev_dump_queues(const struct netdev *netdev,
1415                    netdev_dump_queues_cb *cb, void *aux)
1416 {
1417     const struct netdev_class *class = netdev->netdev_class;
1418     return (class->dump_queues
1419             ? class->dump_queues(netdev, cb, aux)
1420             : EOPNOTSUPP);
1421 }
1422
1423 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1424  * its statistics, and the 'aux' specified by the caller.  The order of
1425  * iteration is unspecified, but (when successful) each queue is visited
1426  * exactly once.
1427  *
1428  * Calling this function may be more efficient than calling
1429  * netdev_get_queue_stats() for every queue.
1430  *
1431  * 'cb' must not modify or free the statistics passed in.
1432  *
1433  * Returns 0 if successful, otherwise a positive errno value.  On error, some
1434  * configured queues may not have been included in the iteration. */
1435 int
1436 netdev_dump_queue_stats(const struct netdev *netdev,
1437                         netdev_dump_queue_stats_cb *cb, void *aux)
1438 {
1439     const struct netdev_class *class = netdev->netdev_class;
1440     return (class->dump_queue_stats
1441             ? class->dump_queue_stats(netdev, cb, aux)
1442             : EOPNOTSUPP);
1443 }
1444
1445 /* Returns a sequence number which indicates changes in one of 'netdev''s
1446  * properties.  The returned sequence will be nonzero so that callers have a
1447  * value which they may use as a reset when tracking 'netdev'.
1448  *
1449  * The returned sequence number will change whenever 'netdev''s flags,
1450  * features, ethernet address, or carrier changes.  It may change for other
1451  * reasons as well, or no reason at all. */
1452 unsigned int
1453 netdev_change_seq(const struct netdev *netdev)
1454 {
1455     return netdev->netdev_class->change_seq(netdev);
1456 }
1457 \f
1458 /* Returns the class type of 'netdev'.
1459  *
1460  * The caller must not free the returned value. */
1461 const char *
1462 netdev_get_type(const struct netdev *netdev)
1463 {
1464     return netdev->netdev_class->type;
1465 }
1466
1467 /* Returns the class associated with 'netdev'. */
1468 const struct netdev_class *
1469 netdev_get_class(const struct netdev *netdev)
1470 {
1471     return netdev->netdev_class;
1472 }
1473
1474 /* Returns the netdev with 'name' or NULL if there is none.
1475  *
1476  * The caller must free the returned netdev with netdev_close(). */
1477 struct netdev *
1478 netdev_from_name(const char *name)
1479     OVS_EXCLUDED(netdev_mutex)
1480 {
1481     struct netdev *netdev;
1482
1483     ovs_mutex_lock(&netdev_mutex);
1484     netdev = shash_find_data(&netdev_shash, name);
1485     if (netdev) {
1486         netdev->ref_cnt++;
1487     }
1488     ovs_mutex_unlock(&netdev_mutex);
1489
1490     return netdev;
1491 }
1492
1493 /* Fills 'device_list' with devices that match 'netdev_class'.
1494  *
1495  * The caller is responsible for initializing and destroying 'device_list' and
1496  * must close each device on the list. */
1497 void
1498 netdev_get_devices(const struct netdev_class *netdev_class,
1499                    struct shash *device_list)
1500     OVS_EXCLUDED(netdev_mutex)
1501 {
1502     struct shash_node *node;
1503
1504     ovs_mutex_lock(&netdev_mutex);
1505     SHASH_FOR_EACH (node, &netdev_shash) {
1506         struct netdev *dev = node->data;
1507
1508         if (dev->netdev_class == netdev_class) {
1509             dev->ref_cnt++;
1510             shash_add(device_list, node->name, node->data);
1511         }
1512     }
1513     ovs_mutex_unlock(&netdev_mutex);
1514 }
1515
1516 const char *
1517 netdev_get_type_from_name(const char *name)
1518 {
1519     struct netdev *dev = netdev_from_name(name);
1520     const char *type = dev ? netdev_get_type(dev) : NULL;
1521     netdev_close(dev);
1522     return type;
1523 }
1524 \f
1525 struct netdev *
1526 netdev_rx_get_netdev(const struct netdev_rx *rx)
1527 {
1528     ovs_assert(rx->netdev->ref_cnt > 0);
1529     return rx->netdev;
1530 }
1531
1532 const char *
1533 netdev_rx_get_name(const struct netdev_rx *rx)
1534 {
1535     return netdev_get_name(netdev_rx_get_netdev(rx));
1536 }
1537
1538 static void
1539 restore_all_flags(void *aux OVS_UNUSED)
1540 {
1541     struct shash_node *node;
1542
1543     SHASH_FOR_EACH (node, &netdev_shash) {
1544         struct netdev *netdev = node->data;
1545         const struct netdev_saved_flags *sf;
1546         enum netdev_flags saved_values;
1547         enum netdev_flags saved_flags;
1548
1549         saved_values = saved_flags = 0;
1550         LIST_FOR_EACH (sf, node, &netdev->saved_flags_list) {
1551             saved_flags |= sf->saved_flags;
1552             saved_values &= ~sf->saved_flags;
1553             saved_values |= sf->saved_flags & sf->saved_values;
1554         }
1555         if (saved_flags) {
1556             enum netdev_flags old_flags;
1557
1558             netdev->netdev_class->update_flags(netdev,
1559                                                saved_flags & saved_values,
1560                                                saved_flags & ~saved_values,
1561                                                &old_flags);
1562         }
1563     }
1564 }