2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <netinet/in.h>
29 #include "dynamic-string.h"
30 #include "fatal-signal.h"
33 #include "netdev-provider.h"
36 #include "poll-loop.h"
40 #define THIS_MODULE VLM_netdev
43 static const struct netdev_class
*base_netdev_classes
[] = {
49 static struct shash netdev_classes
= SHASH_INITIALIZER(&netdev_classes
);
51 /* All created network devices. */
52 static struct shash netdev_dev_shash
= SHASH_INITIALIZER(&netdev_dev_shash
);
54 /* All open network devices. */
55 static struct list netdev_list
= LIST_INITIALIZER(&netdev_list
);
57 /* This is set pretty low because we probably won't learn anything from the
58 * additional log messages. */
59 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
61 static void close_all_netdevs(void *aux UNUSED
);
62 static int restore_flags(struct netdev
*netdev
);
63 void update_device_args(struct netdev_dev
*, const struct shash
*args
);
66 netdev_initialize(void)
68 static int status
= -1;
73 fatal_signal_add_hook(close_all_netdevs
, NULL
, NULL
, true);
76 for (i
= 0; i
< ARRAY_SIZE(base_netdev_classes
); i
++) {
77 netdev_register_provider(base_netdev_classes
[i
]);
82 /* Performs periodic work needed by all the various kinds of netdevs.
84 * If your program opens any netdevs, it must call this function within its
89 struct shash_node
*node
;
90 SHASH_FOR_EACH(node
, &netdev_classes
) {
91 const struct netdev_class
*class = node
->data
;
98 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
100 * If your program opens any netdevs, it must call this function within its
105 struct shash_node
*node
;
106 SHASH_FOR_EACH(node
, &netdev_classes
) {
107 const struct netdev_class
*class = node
->data
;
114 /* Initializes and registers a new netdev provider. After successful
115 * registration, new netdevs of that type can be opened using netdev_open(). */
117 netdev_register_provider(const struct netdev_class
*new_class
)
119 struct netdev_class
*new_provider
;
121 if (shash_find(&netdev_classes
, new_class
->type
)) {
122 VLOG_WARN("attempted to register duplicate netdev provider: %s",
127 if (new_class
->init
) {
128 int error
= new_class
->init();
130 VLOG_ERR("failed to initialize %s network device class: %s",
131 new_class
->type
, strerror(error
));
136 new_provider
= xmalloc(sizeof *new_provider
);
137 memcpy(new_provider
, new_class
, sizeof *new_provider
);
139 shash_add(&netdev_classes
, new_class
->type
, new_provider
);
144 /* Unregisters a netdev provider. 'type' must have been previously
145 * registered and not currently be in use by any netdevs. After unregistration
146 * new netdevs of that type cannot be opened using netdev_open(). */
148 netdev_unregister_provider(const char *type
)
150 struct shash_node
*del_node
, *netdev_dev_node
;
152 del_node
= shash_find(&netdev_classes
, type
);
154 VLOG_WARN("attempted to unregister a netdev provider that is not "
155 "registered: %s", type
);
159 SHASH_FOR_EACH(netdev_dev_node
, &netdev_dev_shash
) {
160 struct netdev_dev
*netdev_dev
= netdev_dev_node
->data
;
161 if (!strcmp(netdev_dev
->class->type
, type
)) {
162 VLOG_WARN("attempted to unregister in use netdev provider: %s",
168 shash_delete(&netdev_classes
, del_node
);
169 free(del_node
->data
);
174 /* Clears 'types' and enumerates the types of all currently registered netdev
175 * providers into it. The caller must first initialize the svec. */
177 netdev_enumerate_types(struct svec
*types
)
179 struct shash_node
*node
;
184 SHASH_FOR_EACH(node
, &netdev_classes
) {
185 const struct netdev_class
*netdev_class
= node
->data
;
186 svec_add(types
, netdev_class
->type
);
190 /* Compares 'args' to those used to those used by 'dev'. Returns true
191 * if the arguments are the same, false otherwise. Does not update the
192 * values stored in 'dev'. */
194 compare_device_args(const struct netdev_dev
*dev
, const struct shash
*args
)
196 const struct shash_node
**new_args
;
200 if (shash_count(args
) != dev
->n_args
) {
204 new_args
= shash_sort(args
);
205 for (i
= 0; i
< dev
->n_args
; i
++) {
206 if (strcmp(dev
->args
[i
].key
, new_args
[i
]->name
) ||
207 strcmp(dev
->args
[i
].value
, new_args
[i
]->data
)) {
219 compare_args(const void *a_
, const void *b_
)
221 const struct arg
*a
= a_
;
222 const struct arg
*b
= b_
;
223 return strcmp(a
->key
, b
->key
);
227 update_device_args(struct netdev_dev
*dev
, const struct shash
*args
)
229 struct shash_node
*node
;
233 for (i
= 0; i
< dev
->n_args
; i
++) {
234 free(dev
->args
[i
].key
);
235 free(dev
->args
[i
].value
);
242 if (!args
|| shash_is_empty(args
)) {
246 dev
->n_args
= shash_count(args
);
247 dev
->args
= xmalloc(dev
->n_args
* sizeof *dev
->args
);
250 SHASH_FOR_EACH(node
, args
) {
251 dev
->args
[i
].key
= xstrdup(node
->name
);
252 dev
->args
[i
].value
= xstrdup(node
->data
);
256 qsort(dev
->args
, dev
->n_args
, sizeof *dev
->args
, compare_args
);
260 create_device(struct netdev_options
*options
, struct netdev_dev
**netdev_devp
)
262 struct netdev_class
*netdev_class
;
264 if (!options
->may_create
) {
265 VLOG_WARN("attempted to create a device that may not be created: %s",
270 if (!options
->type
|| strlen(options
->type
) == 0) {
271 /* Default to system. */
272 options
->type
= "system";
275 netdev_class
= shash_find_data(&netdev_classes
, options
->type
);
277 VLOG_WARN("could not create netdev %s of unknown type %s",
278 options
->name
, options
->type
);
282 return netdev_class
->create(options
->name
, options
->type
, options
->args
,
286 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
287 * successful, otherwise a positive errno value. On success, sets '*netdevp'
288 * to the new network device, otherwise to null.
290 * If this is the first time the device has been opened, then create is called
291 * before opening. The device is created using the given type and arguments.
293 * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
294 * capture frames of that type received on the device. It may also be one of
295 * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
298 * If the 'may_create' flag is set then this is allowed to be the first time
299 * the device is opened (i.e. the refcount will be 1 after this call). It
300 * may be set to false if the device should have already been created.
302 * If the 'may_open' flag is set then the call will succeed even if another
303 * caller has already opened it. It may be to false if the device should not
304 * currently be open. */
307 netdev_open(struct netdev_options
*options
, struct netdev
**netdevp
)
309 struct shash empty_args
= SHASH_INITIALIZER(&empty_args
);
310 struct netdev_dev
*netdev_dev
;
316 if (!options
->args
) {
317 options
->args
= &empty_args
;
320 netdev_dev
= shash_find_data(&netdev_dev_shash
, options
->name
);
323 error
= create_device(options
, &netdev_dev
);
327 update_device_args(netdev_dev
, options
->args
);
329 } else if (options
->may_open
) {
330 if (!shash_is_empty(options
->args
) &&
331 !compare_device_args(netdev_dev
, options
->args
)) {
333 VLOG_WARN("%s: attempted to open already created netdev with "
334 "different arguments", options
->name
);
338 VLOG_WARN("%s: attempted to create a netdev device with bound name",
343 error
= netdev_dev
->class->open(netdev_dev
, options
->ethertype
, netdevp
);
346 netdev_dev
->ref_cnt
++;
348 if (!netdev_dev
->ref_cnt
) {
349 netdev_dev_uninit(netdev_dev
, true);
357 netdev_open_default(const char *name
, struct netdev
**netdevp
)
359 struct netdev_options options
;
361 memset(&options
, 0, sizeof options
);
364 options
.ethertype
= NETDEV_ETH_TYPE_NONE
;
365 options
.may_create
= true;
366 options
.may_open
= true;
368 return netdev_open(&options
, netdevp
);
371 /* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
372 * or NULL if none are needed. */
374 netdev_reconfigure(struct netdev
*netdev
, const struct shash
*args
)
376 struct shash empty_args
= SHASH_INITIALIZER(&empty_args
);
377 struct netdev_dev
*netdev_dev
= netdev_get_dev(netdev
);
383 if (netdev_dev
->class->reconfigure
) {
384 if (!compare_device_args(netdev_dev
, args
)) {
385 update_device_args(netdev_dev
, args
);
386 return netdev_dev
->class->reconfigure(netdev_dev
, args
);
388 } else if (!shash_is_empty(args
)) {
389 VLOG_WARN("%s: arguments provided to device that does not have a "
390 "reconfigure function", netdev_get_name(netdev
));
396 /* Closes and destroys 'netdev'. */
398 netdev_close(struct netdev
*netdev
)
401 struct netdev_dev
*netdev_dev
= netdev_get_dev(netdev
);
403 assert(netdev_dev
->ref_cnt
);
404 netdev_dev
->ref_cnt
--;
405 netdev_uninit(netdev
, true);
407 /* If the reference count for the netdev device is zero, destroy it. */
408 if (!netdev_dev
->ref_cnt
) {
409 netdev_dev_uninit(netdev_dev
, true);
414 /* Returns true if a network device named 'name' exists and may be opened,
415 * otherwise false. */
417 netdev_exists(const char *name
)
419 struct netdev
*netdev
;
422 error
= netdev_open_default(name
, &netdev
);
424 netdev_close(netdev
);
427 if (error
!= ENODEV
) {
428 VLOG_WARN("failed to open network device %s: %s",
429 name
, strerror(error
));
435 /* Clears 'svec' and enumerates the names of all known network devices. */
437 netdev_enumerate(struct svec
*svec
)
439 struct shash_node
*node
;
445 SHASH_FOR_EACH(node
, &netdev_classes
) {
446 const struct netdev_class
*netdev_class
= node
->data
;
447 if (netdev_class
->enumerate
) {
448 int retval
= netdev_class
->enumerate(svec
);
450 VLOG_WARN("failed to enumerate %s network devices: %s",
451 netdev_class
->type
, strerror(retval
));
462 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
463 * must have initialized with sufficient room for the packet. The space
464 * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
465 * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
466 * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
467 * need not be included.)
469 * If a packet is successfully retrieved, returns 0. In this case 'buffer' is
470 * guaranteed to contain at least ETH_TOTAL_MIN bytes. Otherwise, returns a
471 * positive errno value. Returns EAGAIN immediately if no packet is ready to
475 netdev_recv(struct netdev
*netdev
, struct ofpbuf
*buffer
)
479 assert(buffer
->size
== 0);
480 assert(ofpbuf_tailroom(buffer
) >= ETH_TOTAL_MIN
);
482 retval
= netdev_get_dev(netdev
)->class->recv(netdev
, buffer
->data
,
483 ofpbuf_tailroom(buffer
));
485 COVERAGE_INC(netdev_received
);
486 buffer
->size
+= retval
;
487 if (buffer
->size
< ETH_TOTAL_MIN
) {
488 ofpbuf_put_zeros(buffer
, ETH_TOTAL_MIN
- buffer
->size
);
496 /* Registers with the poll loop to wake up from the next call to poll_block()
497 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
499 netdev_recv_wait(struct netdev
*netdev
)
501 netdev_get_dev(netdev
)->class->recv_wait(netdev
);
504 /* Discards all packets waiting to be received from 'netdev'. */
506 netdev_drain(struct netdev
*netdev
)
508 return netdev_get_dev(netdev
)->class->drain(netdev
);
511 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
512 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
513 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
514 * the packet is too big or too small to transmit on the device.
516 * The caller retains ownership of 'buffer' in all cases.
518 * The kernel maintains a packet transmission queue, so the caller is not
519 * expected to do additional queuing of packets. */
521 netdev_send(struct netdev
*netdev
, const struct ofpbuf
*buffer
)
523 int error
= netdev_get_dev(netdev
)->class->send(netdev
, buffer
->data
,
526 COVERAGE_INC(netdev_sent
);
531 /* Registers with the poll loop to wake up from the next call to poll_block()
532 * when the packet transmission queue has sufficient room to transmit a packet
533 * with netdev_send().
535 * The kernel maintains a packet transmission queue, so the client is not
536 * expected to do additional queuing of packets. Thus, this function is
537 * unlikely to ever be used. It is included for completeness. */
539 netdev_send_wait(struct netdev
*netdev
)
541 return netdev_get_dev(netdev
)->class->send_wait(netdev
);
544 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
545 * otherwise a positive errno value. */
547 netdev_set_etheraddr(struct netdev
*netdev
, const uint8_t mac
[ETH_ADDR_LEN
])
549 return netdev_get_dev(netdev
)->class->set_etheraddr(netdev
, mac
);
552 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
553 * the MAC address into 'mac'. On failure, returns a positive errno value and
554 * clears 'mac' to all-zeros. */
556 netdev_get_etheraddr(const struct netdev
*netdev
, uint8_t mac
[ETH_ADDR_LEN
])
558 return netdev_get_dev(netdev
)->class->get_etheraddr(netdev
, mac
);
561 /* Returns the name of the network device that 'netdev' represents,
562 * e.g. "eth0". The caller must not modify or free the returned string. */
564 netdev_get_name(const struct netdev
*netdev
)
566 return netdev_get_dev(netdev
)->name
;
569 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
570 * (and received) packets, in bytes, not including the hardware header; thus,
571 * this is typically 1500 bytes for Ethernet devices.
573 * If successful, returns 0 and stores the MTU size in '*mtup'. On failure,
574 * returns a positive errno value and stores ETH_PAYLOAD_MAX (1500) in
577 netdev_get_mtu(const struct netdev
*netdev
, int *mtup
)
579 int error
= netdev_get_dev(netdev
)->class->get_mtu(netdev
, mtup
);
581 VLOG_WARN_RL(&rl
, "failed to retrieve MTU for network device %s: %s",
582 netdev_get_name(netdev
), strerror(error
));
583 *mtup
= ETH_PAYLOAD_MAX
;
588 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
589 * failure, returns a negative errno value.
591 * The desired semantics of the ifindex value are a combination of those
592 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
593 * value should be unique within a host and remain stable at least until
594 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
595 * but many systems do not follow this rule anyhow.
598 netdev_get_ifindex(const struct netdev
*netdev
)
600 return netdev_get_dev(netdev
)->class->get_ifindex(netdev
);
603 /* Stores the features supported by 'netdev' into each of '*current',
604 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
605 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
606 * successful, otherwise a positive errno value. On failure, all of the
607 * passed-in values are set to 0. */
609 netdev_get_features(struct netdev
*netdev
,
610 uint32_t *current
, uint32_t *advertised
,
611 uint32_t *supported
, uint32_t *peer
)
620 advertised
= &dummy
[1];
623 supported
= &dummy
[2];
629 error
= netdev_get_dev(netdev
)->class->get_features(netdev
, current
,
630 advertised
, supported
,
633 *current
= *advertised
= *supported
= *peer
= 0;
638 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
639 * successful, otherwise a positive errno value. */
641 netdev_set_advertisements(struct netdev
*netdev
, uint32_t advertise
)
643 return (netdev_get_dev(netdev
)->class->set_advertisements
644 ? netdev_get_dev(netdev
)->class->set_advertisements(netdev
,
649 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
650 * and '*netmask' to its netmask and returns 0. Otherwise, returns a positive
651 * errno value and sets '*address' to 0 (INADDR_ANY).
653 * The following error values have well-defined meanings:
655 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
657 * - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
659 * 'address' or 'netmask' or both may be null, in which case the address or netmask
660 * is not reported. */
662 netdev_get_in4(const struct netdev
*netdev
,
663 struct in_addr
*address_
, struct in_addr
*netmask_
)
665 struct in_addr address
;
666 struct in_addr netmask
;
669 error
= (netdev_get_dev(netdev
)->class->get_in4
670 ? netdev_get_dev(netdev
)->class->get_in4(netdev
, &address
,
674 address_
->s_addr
= error
? 0 : address
.s_addr
;
677 netmask_
->s_addr
= error
? 0 : netmask
.s_addr
;
682 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
683 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
684 * positive errno value. */
686 netdev_set_in4(struct netdev
*netdev
, struct in_addr addr
, struct in_addr mask
)
688 return (netdev_get_dev(netdev
)->class->set_in4
689 ? netdev_get_dev(netdev
)->class->set_in4(netdev
, addr
, mask
)
693 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
696 netdev_add_router(struct netdev
*netdev
, struct in_addr router
)
698 COVERAGE_INC(netdev_add_router
);
699 return (netdev_get_dev(netdev
)->class->add_router
700 ? netdev_get_dev(netdev
)->class->add_router(netdev
, router
)
704 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
705 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
706 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
707 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
708 * a directly connected network) in '*next_hop' and a copy of the name of the
709 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
710 * responsible for freeing '*netdev_name' (by calling free()). */
712 netdev_get_next_hop(const struct netdev
*netdev
,
713 const struct in_addr
*host
, struct in_addr
*next_hop
,
716 int error
= (netdev_get_dev(netdev
)->class->get_next_hop
717 ? netdev_get_dev(netdev
)->class->get_next_hop(host
, next_hop
,
721 next_hop
->s_addr
= 0;
727 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
728 * returns 0. Otherwise, returns a positive errno value and sets '*in6' to
729 * all-zero-bits (in6addr_any).
731 * The following error values have well-defined meanings:
733 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
735 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
737 * 'in6' may be null, in which case the address itself is not reported. */
739 netdev_get_in6(const struct netdev
*netdev
, struct in6_addr
*in6
)
741 struct in6_addr dummy
;
744 error
= (netdev_get_dev(netdev
)->class->get_in6
745 ? netdev_get_dev(netdev
)->class->get_in6(netdev
, in6
? in6
749 memset(in6
, 0, sizeof *in6
);
754 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
755 * 'on'. If 'permanent' is true, the changes will persist; otherwise, they
756 * will be reverted when 'netdev' is closed or the program exits. Returns 0 if
757 * successful, otherwise a positive errno value. */
759 do_update_flags(struct netdev
*netdev
, enum netdev_flags off
,
760 enum netdev_flags on
, enum netdev_flags
*old_flagsp
,
763 enum netdev_flags old_flags
;
766 error
= netdev_get_dev(netdev
)->class->update_flags(netdev
, off
& ~on
, on
,
769 VLOG_WARN_RL(&rl
, "failed to %s flags for network device %s: %s",
770 off
|| on
? "set" : "get", netdev_get_name(netdev
),
773 } else if ((off
|| on
) && !permanent
) {
774 enum netdev_flags new_flags
= (old_flags
& ~off
) | on
;
775 enum netdev_flags changed_flags
= old_flags
^ new_flags
;
777 if (!netdev
->changed_flags
) {
778 netdev
->save_flags
= old_flags
;
780 netdev
->changed_flags
|= changed_flags
;
784 *old_flagsp
= old_flags
;
789 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
790 * Returns 0 if successful, otherwise a positive errno value. On failure,
791 * stores 0 into '*flagsp'. */
793 netdev_get_flags(const struct netdev
*netdev_
, enum netdev_flags
*flagsp
)
795 struct netdev
*netdev
= (struct netdev
*) netdev_
;
796 return do_update_flags(netdev
, 0, 0, flagsp
, false);
799 /* Sets the flags for 'netdev' to 'flags'.
800 * If 'permanent' is true, the changes will persist; otherwise, they
801 * will be reverted when 'netdev' is closed or the program exits.
802 * Returns 0 if successful, otherwise a positive errno value. */
804 netdev_set_flags(struct netdev
*netdev
, enum netdev_flags flags
,
807 return do_update_flags(netdev
, -1, flags
, NULL
, permanent
);
810 /* Turns on the specified 'flags' on 'netdev'.
811 * If 'permanent' is true, the changes will persist; otherwise, they
812 * will be reverted when 'netdev' is closed or the program exits.
813 * Returns 0 if successful, otherwise a positive errno value. */
815 netdev_turn_flags_on(struct netdev
*netdev
, enum netdev_flags flags
,
818 return do_update_flags(netdev
, 0, flags
, NULL
, permanent
);
821 /* Turns off the specified 'flags' on 'netdev'.
822 * If 'permanent' is true, the changes will persist; otherwise, they
823 * will be reverted when 'netdev' is closed or the program exits.
824 * Returns 0 if successful, otherwise a positive errno value. */
826 netdev_turn_flags_off(struct netdev
*netdev
, enum netdev_flags flags
,
829 return do_update_flags(netdev
, flags
, 0, NULL
, permanent
);
832 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
833 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
834 * returns 0. Otherwise, it returns a positive errno value; in particular,
835 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
837 netdev_arp_lookup(const struct netdev
*netdev
,
838 uint32_t ip
, uint8_t mac
[ETH_ADDR_LEN
])
840 int error
= (netdev_get_dev(netdev
)->class->arp_lookup
841 ? netdev_get_dev(netdev
)->class->arp_lookup(netdev
, ip
, mac
)
844 memset(mac
, 0, ETH_ADDR_LEN
);
849 /* Sets 'carrier' to true if carrier is active (link light is on) on
852 netdev_get_carrier(const struct netdev
*netdev
, bool *carrier
)
854 int error
= (netdev_get_dev(netdev
)->class->get_carrier
855 ? netdev_get_dev(netdev
)->class->get_carrier(netdev
, carrier
)
863 /* Retrieves current device stats for 'netdev'. */
865 netdev_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
869 COVERAGE_INC(netdev_get_stats
);
870 error
= (netdev_get_dev(netdev
)->class->get_stats
871 ? netdev_get_dev(netdev
)->class->get_stats(netdev
, stats
)
874 memset(stats
, 0xff, sizeof *stats
);
879 /* Attempts to set input rate limiting (policing) policy, such that up to
880 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
881 * size of 'kbits' kb. */
883 netdev_set_policing(struct netdev
*netdev
, uint32_t kbits_rate
,
884 uint32_t kbits_burst
)
886 return (netdev_get_dev(netdev
)->class->set_policing
887 ? netdev_get_dev(netdev
)->class->set_policing(netdev
, kbits_rate
,
892 /* If 'netdev' is a VLAN network device (e.g. one created with vconfig(8)),
893 * sets '*vlan_vid' to the VLAN VID associated with that device and returns 0.
894 * Otherwise returns a errno value (specifically ENOENT if 'netdev_name' is the
895 * name of a network device that is not a VLAN device) and sets '*vlan_vid' to
898 netdev_get_vlan_vid(const struct netdev
*netdev
, int *vlan_vid
)
900 int error
= (netdev_get_dev(netdev
)->class->get_vlan_vid
901 ? netdev_get_dev(netdev
)->class->get_vlan_vid(netdev
, vlan_vid
)
909 /* Returns a network device that has 'in4' as its IP address, if one exists,
910 * otherwise a null pointer. */
912 netdev_find_dev_by_in4(const struct in_addr
*in4
)
914 struct netdev
*netdev
;
915 struct svec dev_list
= SVEC_EMPTY_INITIALIZER
;
918 netdev_enumerate(&dev_list
);
919 for (i
= 0; i
< dev_list
.n
; i
++) {
920 const char *name
= dev_list
.names
[i
];
921 struct in_addr dev_in4
;
923 if (!netdev_open_default(name
, &netdev
)
924 && !netdev_get_in4(netdev
, &dev_in4
, NULL
)
925 && dev_in4
.s_addr
== in4
->s_addr
) {
928 netdev_close(netdev
);
933 svec_destroy(&dev_list
);
937 /* Initializes 'netdev_dev' as a netdev device named 'name' of the
940 * This function adds 'netdev_dev' to a netdev-owned shash, so it is
941 * very important that 'netdev_dev' only be freed after calling
942 * the refcount drops to zero. */
944 netdev_dev_init(struct netdev_dev
*netdev_dev
, const char *name
,
945 const struct netdev_class
*class)
947 assert(!shash_find(&netdev_dev_shash
, name
));
949 memset(netdev_dev
, 0, sizeof *netdev_dev
);
950 netdev_dev
->class = class;
951 netdev_dev
->name
= xstrdup(name
);
952 netdev_dev
->node
= shash_add(&netdev_dev_shash
, name
, netdev_dev
);
955 /* Undoes the results of initialization.
957 * Normally this function does not need to be called as netdev_close has
958 * the same effect when the refcount drops to zero.
959 * However, it may be called by providers due to an error on creation
960 * that occurs after initialization. It this case netdev_close() would
961 * never be called. */
963 netdev_dev_uninit(struct netdev_dev
*netdev_dev
, bool destroy
)
965 char *name
= netdev_dev
->name
;
967 assert(!netdev_dev
->ref_cnt
);
969 shash_delete(&netdev_dev_shash
, netdev_dev
->node
);
970 update_device_args(netdev_dev
, NULL
);
973 netdev_dev
->class->destroy(netdev_dev
);
978 /* Returns the class type of 'netdev_dev'.
980 * The caller must not free the returned value. */
982 netdev_dev_get_type(const struct netdev_dev
*netdev_dev
)
984 return netdev_dev
->class->type
;
987 /* Returns the name of 'netdev_dev'.
989 * The caller must not free the returned value. */
991 netdev_dev_get_name(const struct netdev_dev
*netdev_dev
)
993 return netdev_dev
->name
;
996 /* Returns the netdev_dev with 'name' or NULL if there is none.
998 * The caller must not free the returned value. */
1000 netdev_dev_from_name(const char *name
)
1002 return shash_find_data(&netdev_dev_shash
, name
);
1005 /* Fills 'device_list' with devices that match 'class'.
1007 * The caller is responsible for initializing and destroying 'device_list'
1008 * but the contained netdev_devs must not be freed. */
1010 netdev_dev_get_devices(const struct netdev_class
*class,
1011 struct shash
*device_list
)
1013 struct shash_node
*node
;
1014 SHASH_FOR_EACH (node
, &netdev_dev_shash
) {
1015 struct netdev_dev
*dev
= node
->data
;
1017 if (dev
->class == class) {
1018 shash_add(device_list
, node
->name
, node
->data
);
1023 /* Initializes 'netdev' as a instance of the netdev_dev.
1025 * This function adds 'netdev' to a netdev-owned linked list, so it is very
1026 * important that 'netdev' only be freed after calling netdev_close(). */
1028 netdev_init(struct netdev
*netdev
, struct netdev_dev
*netdev_dev
)
1030 memset(netdev
, 0, sizeof *netdev
);
1031 netdev
->netdev_dev
= netdev_dev
;
1032 list_push_back(&netdev_list
, &netdev
->node
);
1035 /* Undoes the results of initialization.
1037 * Normally this function only needs to be called from netdev_close().
1038 * However, it may be called by providers due to an error on opening
1039 * that occurs after initialization. It this case netdev_close() would
1040 * never be called. */
1042 netdev_uninit(struct netdev
*netdev
, bool close
)
1044 /* Restore flags that we changed, if any. */
1045 int error
= restore_flags(netdev
);
1046 list_remove(&netdev
->node
);
1048 VLOG_WARN("failed to restore network device flags on %s: %s",
1049 netdev_get_name(netdev
), strerror(error
));
1053 netdev_get_dev(netdev
)->class->close(netdev
);
1058 /* Returns the class type of 'netdev'.
1060 * The caller must not free the returned value. */
1062 netdev_get_type(const struct netdev
*netdev
)
1064 return netdev_get_dev(netdev
)->class->type
;
1068 netdev_get_dev(const struct netdev
*netdev
)
1070 return netdev
->netdev_dev
;
1073 /* Initializes 'notifier' as a netdev notifier for 'netdev', for which
1074 * notification will consist of calling 'cb', with auxiliary data 'aux'. */
1076 netdev_notifier_init(struct netdev_notifier
*notifier
, struct netdev
*netdev
,
1077 void (*cb
)(struct netdev_notifier
*), void *aux
)
1079 notifier
->netdev
= netdev
;
1081 notifier
->aux
= aux
;
1084 /* Tracks changes in the status of a set of network devices. */
1085 struct netdev_monitor
{
1086 struct shash polled_netdevs
;
1087 struct shash changed_netdevs
;
1090 /* Creates and returns a new structure for monitor changes in the status of
1091 * network devices. */
1092 struct netdev_monitor
*
1093 netdev_monitor_create(void)
1095 struct netdev_monitor
*monitor
= xmalloc(sizeof *monitor
);
1096 shash_init(&monitor
->polled_netdevs
);
1097 shash_init(&monitor
->changed_netdevs
);
1101 /* Destroys 'monitor'. */
1103 netdev_monitor_destroy(struct netdev_monitor
*monitor
)
1106 struct shash_node
*node
;
1108 SHASH_FOR_EACH (node
, &monitor
->polled_netdevs
) {
1109 struct netdev_notifier
*notifier
= node
->data
;
1110 netdev_get_dev(notifier
->netdev
)->class->poll_remove(notifier
);
1113 shash_destroy(&monitor
->polled_netdevs
);
1114 shash_destroy(&monitor
->changed_netdevs
);
1120 netdev_monitor_cb(struct netdev_notifier
*notifier
)
1122 struct netdev_monitor
*monitor
= notifier
->aux
;
1123 const char *name
= netdev_get_name(notifier
->netdev
);
1124 if (!shash_find(&monitor
->changed_netdevs
, name
)) {
1125 shash_add(&monitor
->changed_netdevs
, name
, NULL
);
1129 /* Attempts to add 'netdev' as a netdev monitored by 'monitor'. Returns 0 if
1130 * successful, otherwise a positive errno value.
1132 * Adding a given 'netdev' to a monitor multiple times is equivalent to adding
1135 netdev_monitor_add(struct netdev_monitor
*monitor
, struct netdev
*netdev
)
1137 const char *netdev_name
= netdev_get_name(netdev
);
1139 if (!shash_find(&monitor
->polled_netdevs
, netdev_name
)
1140 && netdev_get_dev(netdev
)->class->poll_add
)
1142 struct netdev_notifier
*notifier
;
1143 error
= netdev_get_dev(netdev
)->class->poll_add(netdev
,
1145 monitor
, ¬ifier
);
1147 assert(notifier
->netdev
== netdev
);
1148 shash_add(&monitor
->polled_netdevs
, netdev_name
, notifier
);
1154 /* Removes 'netdev' from the set of netdevs monitored by 'monitor'. (This has
1155 * no effect if 'netdev' is not in the set of devices monitored by
1158 netdev_monitor_remove(struct netdev_monitor
*monitor
, struct netdev
*netdev
)
1160 const char *netdev_name
= netdev_get_name(netdev
);
1161 struct shash_node
*node
;
1163 node
= shash_find(&monitor
->polled_netdevs
, netdev_name
);
1165 /* Cancel future notifications. */
1166 struct netdev_notifier
*notifier
= node
->data
;
1167 netdev_get_dev(netdev
)->class->poll_remove(notifier
);
1168 shash_delete(&monitor
->polled_netdevs
, node
);
1170 /* Drop any pending notification. */
1171 node
= shash_find(&monitor
->changed_netdevs
, netdev_name
);
1173 shash_delete(&monitor
->changed_netdevs
, node
);
1178 /* Checks for changes to netdevs in the set monitored by 'monitor'. If any of
1179 * the attributes (Ethernet address, carrier status, speed or peer-advertised
1180 * speed, flags, etc.) of a network device monitored by 'monitor' has changed,
1181 * sets '*devnamep' to the name of a device that has changed and returns 0.
1182 * The caller is responsible for freeing '*devnamep' (with free()).
1184 * If no devices have changed, sets '*devnamep' to NULL and returns EAGAIN.
1187 netdev_monitor_poll(struct netdev_monitor
*monitor
, char **devnamep
)
1189 struct shash_node
*node
= shash_first(&monitor
->changed_netdevs
);
1194 *devnamep
= xstrdup(node
->name
);
1195 shash_delete(&monitor
->changed_netdevs
, node
);
1200 /* Registers with the poll loop to wake up from the next call to poll_block()
1201 * when netdev_monitor_poll(monitor) would indicate that a device has
1204 netdev_monitor_poll_wait(const struct netdev_monitor
*monitor
)
1206 if (!shash_is_empty(&monitor
->changed_netdevs
)) {
1207 poll_immediate_wake();
1209 /* XXX Nothing needed here for netdev_linux, but maybe other netdev
1210 * classes need help. */
1214 /* Restore the network device flags on 'netdev' to those that were active
1215 * before we changed them. Returns 0 if successful, otherwise a positive
1218 * To avoid reentry, the caller must ensure that fatal signals are blocked. */
1220 restore_flags(struct netdev
*netdev
)
1222 if (netdev
->changed_flags
) {
1223 enum netdev_flags restore
= netdev
->save_flags
& netdev
->changed_flags
;
1224 enum netdev_flags old_flags
;
1225 return netdev_get_dev(netdev
)->class->update_flags(netdev
,
1226 netdev
->changed_flags
& ~restore
,
1227 restore
, &old_flags
);
1232 /* Close all netdevs on shutdown so they can do any needed cleanup such as
1233 * destroying devices, restoring flags, etc. */
1235 close_all_netdevs(void *aux UNUSED
)
1237 struct netdev
*netdev
, *next
;
1238 LIST_FOR_EACH_SAFE(netdev
, next
, struct netdev
, node
, &netdev_list
) {
1239 netdev_close(netdev
);