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