2 * Copyright (c) 2009 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.
19 #include "netdev-linux.h"
24 #include <arpa/inet.h>
26 #include <linux/if_tun.h>
27 #include <linux/types.h>
28 #include <linux/ethtool.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
31 #include <linux/version.h>
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <netpacket/packet.h>
36 #include <net/ethernet.h>
38 #include <net/if_arp.h>
39 #include <net/if_packet.h>
40 #include <net/route.h>
41 #include <netinet/in.h>
48 #include "dynamic-string.h"
49 #include "fatal-signal.h"
50 #include "netdev-provider.h"
53 #include "openflow/openflow.h"
55 #include "poll-loop.h"
56 #include "socket-util.h"
60 #define THIS_MODULE VLM_netdev_linux
63 /* These were introduced in Linux 2.6.14, so they might be missing if we have
65 #ifndef ADVERTISED_Pause
66 #define ADVERTISED_Pause (1 << 13)
68 #ifndef ADVERTISED_Asym_Pause
69 #define ADVERTISED_Asym_Pause (1 << 14)
75 /* File descriptors. For ordinary network devices, the two fds below are
76 * the same; for tap devices, they differ. */
77 int netdev_fd; /* Network device. */
78 int tap_fd; /* TAP character device, if any, otherwise the
81 struct netdev_linux_cache *cache;
85 VALID_IFINDEX = 1 << 0,
86 VALID_ETHERADDR = 1 << 1,
90 VALID_CARRIER = 1 << 5
93 /* Cached network device information. */
94 struct netdev_linux_cache {
95 struct shash_node *shash_node;
100 uint8_t etheraddr[ETH_ADDR_LEN];
107 static struct shash cache_map = SHASH_INITIALIZER(&cache_map);
108 static struct rtnetlink_notifier netdev_linux_cache_notifier;
110 /* Policy for RTNLGRP_LINK messages.
112 * There are *many* more fields in these messages, but currently we only care
113 * about interface names. */
114 static const struct nl_policy rtnlgrp_link_policy[] = {
115 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
116 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
117 [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
118 .min_len = sizeof(struct rtnl_link_stats) },
121 /* An AF_INET socket (used for ioctl operations). */
122 static int af_inet_sock = -1;
124 struct netdev_linux_notifier {
125 struct netdev_notifier notifier;
129 static struct shash netdev_linux_notifiers =
130 SHASH_INITIALIZER(&netdev_linux_notifiers);
131 static struct rtnetlink_notifier netdev_linux_poll_notifier;
133 /* This is set pretty low because we probably won't learn anything from the
134 * additional log messages. */
135 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
137 static int netdev_linux_do_ethtool(struct netdev *, struct ethtool_cmd *,
138 int cmd, const char *cmd_name);
139 static int netdev_linux_do_ioctl(const struct netdev *, struct ifreq *,
140 int cmd, const char *cmd_name);
141 static int get_flags(const struct netdev *, int *flagsp);
142 static int set_flags(struct netdev *, int flags);
143 static int do_get_ifindex(const char *netdev_name);
144 static int get_ifindex(const struct netdev *, int *ifindexp);
145 static int do_set_addr(struct netdev *netdev,
146 int ioctl_nr, const char *ioctl_name,
147 struct in_addr addr);
148 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
149 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
150 const uint8_t[ETH_ADDR_LEN]);
151 static int get_stats_via_netlink(int ifindex, struct netdev_stats *stats);
152 static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats);
154 static struct netdev_linux *
155 netdev_linux_cast(const struct netdev *netdev)
157 netdev_assert_class(netdev, &netdev_linux_class);
158 return CONTAINER_OF(netdev, struct netdev_linux, netdev);
162 netdev_linux_init(void)
164 static int status = -1;
166 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
167 status = af_inet_sock >= 0 ? 0 : errno;
169 VLOG_ERR("failed to create inet socket: %s", strerror(status));
176 netdev_linux_run(void)
178 rtnetlink_notifier_run();
182 netdev_linux_wait(void)
184 rtnetlink_notifier_wait();
188 netdev_linux_cache_cb(const struct rtnetlink_change *change,
191 struct netdev_linux_cache *cache;
193 cache = shash_find_data(&cache_map, change->ifname);
198 struct shash_node *node;
199 SHASH_FOR_EACH (node, &cache_map) {
207 netdev_linux_open(const char *name, char *suffix, int ethertype,
208 struct netdev **netdevp)
210 struct netdev_linux *netdev;
211 enum netdev_flags flags;
214 /* Allocate network device. */
215 netdev = xcalloc(1, sizeof *netdev);
216 netdev_init(&netdev->netdev, suffix, &netdev_linux_class);
217 netdev->netdev_fd = -1;
219 netdev->cache = shash_find_data(&cache_map, suffix);
220 if (!netdev->cache) {
221 if (shash_is_empty(&cache_map)) {
222 int error = rtnetlink_notifier_register(
223 &netdev_linux_cache_notifier, netdev_linux_cache_cb, NULL);
225 netdev_close(&netdev->netdev);
229 netdev->cache = xmalloc(sizeof *netdev->cache);
230 netdev->cache->shash_node = shash_add(&cache_map, suffix,
232 netdev->cache->valid = 0;
233 netdev->cache->ref_cnt = 0;
235 netdev->cache->ref_cnt++;
237 if (!strncmp(name, "tap:", 4)) {
238 static const char tap_dev[] = "/dev/net/tun";
241 /* Open tap device. */
242 netdev->tap_fd = open(tap_dev, O_RDWR);
243 if (netdev->tap_fd < 0) {
245 VLOG_WARN("opening \"%s\" failed: %s", tap_dev, strerror(error));
249 /* Create tap device. */
250 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
251 error = netdev_linux_do_ioctl(&netdev->netdev, &ifr,
252 TUNSETIFF, "TUNSETIFF");
257 /* Make non-blocking. */
258 error = set_nonblocking(netdev->tap_fd);
264 error = netdev_get_flags(&netdev->netdev, &flags);
265 if (error == ENODEV) {
269 if (netdev->tap_fd >= 0 || ethertype != NETDEV_ETH_TYPE_NONE) {
270 struct sockaddr_ll sll;
274 /* Create file descriptor. */
275 protocol = (ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
276 : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
278 netdev->netdev_fd = socket(PF_PACKET, SOCK_RAW, htons(protocol));
279 if (netdev->netdev_fd < 0) {
283 if (netdev->tap_fd < 0) {
284 netdev->tap_fd = netdev->netdev_fd;
287 /* Set non-blocking mode. */
288 error = set_nonblocking(netdev->netdev_fd);
293 /* Get ethernet device index. */
294 error = get_ifindex(&netdev->netdev, &ifindex);
299 /* Bind to specific ethernet device. */
300 memset(&sll, 0, sizeof sll);
301 sll.sll_family = AF_PACKET;
302 sll.sll_ifindex = ifindex;
303 if (bind(netdev->netdev_fd,
304 (struct sockaddr *) &sll, sizeof sll) < 0) {
306 VLOG_ERR("bind to %s failed: %s", suffix, strerror(error));
310 /* Between the socket() and bind() calls above, the socket receives all
311 * packets of the requested type on all system interfaces. We do not
312 * want to receive that data, but there is no way to avoid it. So we
313 * must now drain out the receive queue. */
314 error = drain_rcvbuf(netdev->netdev_fd);
320 *netdevp = &netdev->netdev;
324 netdev_close(&netdev->netdev);
328 /* Closes and destroys 'netdev'. */
330 netdev_linux_close(struct netdev *netdev_)
332 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
334 if (netdev->cache && !--netdev->cache->ref_cnt) {
335 shash_delete(&cache_map, netdev->cache->shash_node);
338 if (shash_is_empty(&cache_map)) {
339 rtnetlink_notifier_unregister(&netdev_linux_cache_notifier);
342 if (netdev->netdev_fd >= 0) {
343 close(netdev->netdev_fd);
345 if (netdev->tap_fd >= 0 && netdev->netdev_fd != netdev->tap_fd) {
346 close(netdev->tap_fd);
351 /* Initializes 'svec' with a list of the names of all known network devices. */
353 netdev_linux_enumerate(struct svec *svec)
355 struct if_nameindex *names;
357 names = if_nameindex();
361 for (i = 0; names[i].if_name != NULL; i++) {
362 svec_add(svec, names[i].if_name);
364 if_freenameindex(names);
367 VLOG_WARN("could not obtain list of network device names: %s",
374 netdev_linux_recv(struct netdev *netdev_, void *data, size_t size)
376 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
378 if (netdev->tap_fd < 0) {
379 /* Device was opened with NETDEV_ETH_TYPE_NONE. */
384 ssize_t retval = read(netdev->tap_fd, data, size);
387 } else if (errno != EINTR) {
388 if (errno != EAGAIN) {
389 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
390 strerror(errno), netdev_get_name(netdev_));
397 /* Registers with the poll loop to wake up from the next call to poll_block()
398 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
400 netdev_linux_recv_wait(struct netdev *netdev_)
402 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
403 if (netdev->tap_fd >= 0) {
404 poll_fd_wait(netdev->tap_fd, POLLIN);
408 /* Discards all packets waiting to be received from 'netdev'. */
410 netdev_linux_drain(struct netdev *netdev_)
412 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
413 if (netdev->tap_fd < 0 && netdev->netdev_fd < 0) {
415 } else if (netdev->tap_fd != netdev->netdev_fd) {
417 int error = netdev_linux_do_ioctl(netdev_, &ifr,
418 SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
422 drain_fd(netdev->tap_fd, ifr.ifr_qlen);
425 return drain_rcvbuf(netdev->netdev_fd);
429 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
430 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
431 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
432 * the packet is too big or too small to transmit on the device.
434 * The caller retains ownership of 'buffer' in all cases.
436 * The kernel maintains a packet transmission queue, so the caller is not
437 * expected to do additional queuing of packets. */
439 netdev_linux_send(struct netdev *netdev_, const void *data, size_t size)
441 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
443 /* XXX should support sending even if 'ethertype' was NETDEV_ETH_TYPE_NONE.
445 if (netdev->tap_fd < 0) {
450 ssize_t retval = write(netdev->tap_fd, data, size);
452 /* The Linux AF_PACKET implementation never blocks waiting for room
453 * for packets, instead returning ENOBUFS. Translate this into
454 * EAGAIN for the caller. */
455 if (errno == ENOBUFS) {
457 } else if (errno == EINTR) {
459 } else if (errno != EAGAIN) {
460 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
461 netdev_get_name(netdev_), strerror(errno));
464 } else if (retval != size) {
465 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
466 "%zu) on %s", retval, size, netdev_get_name(netdev_));
474 /* Registers with the poll loop to wake up from the next call to poll_block()
475 * when the packet transmission queue has sufficient room to transmit a packet
476 * with netdev_send().
478 * The kernel maintains a packet transmission queue, so the client is not
479 * expected to do additional queuing of packets. Thus, this function is
480 * unlikely to ever be used. It is included for completeness. */
482 netdev_linux_send_wait(struct netdev *netdev_)
484 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
485 if (netdev->tap_fd < 0 && netdev->netdev_fd < 0) {
487 } else if (netdev->tap_fd == netdev->netdev_fd) {
488 poll_fd_wait(netdev->tap_fd, POLLOUT);
490 /* TAP device always accepts packets.*/
491 poll_immediate_wake();
495 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
496 * otherwise a positive errno value. */
498 netdev_linux_set_etheraddr(struct netdev *netdev_,
499 const uint8_t mac[ETH_ADDR_LEN])
501 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
502 int error = set_etheraddr(netdev_get_name(netdev_), ARPHRD_ETHER, mac);
504 memcpy(netdev->cache->etheraddr, mac, ETH_ADDR_LEN);
509 /* Returns a pointer to 'netdev''s MAC address. The caller must not modify or
510 * free the returned buffer. */
512 netdev_linux_get_etheraddr(const struct netdev *netdev_,
513 uint8_t mac[ETH_ADDR_LEN])
515 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
516 if (!(netdev->cache->valid & VALID_ETHERADDR)) {
517 int error = get_etheraddr(netdev_get_name(netdev_),
518 netdev->cache->etheraddr);
522 netdev->cache->valid |= VALID_ETHERADDR;
524 memcpy(mac, netdev->cache->etheraddr, ETH_ADDR_LEN);
528 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
529 * in bytes, not including the hardware header; thus, this is typically 1500
530 * bytes for Ethernet devices. */
532 netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup)
534 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
535 if (!(netdev->cache->valid & VALID_MTU)) {
539 error = netdev_linux_do_ioctl(netdev_, &ifr, SIOCGIFMTU, "SIOCGIFMTU");
543 netdev->cache->mtu = ifr.ifr_mtu;
544 netdev->cache->valid |= VALID_MTU;
546 *mtup = netdev->cache->mtu;
551 netdev_linux_get_carrier(const struct netdev *netdev_, bool *carrier)
553 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
558 if (!(netdev->cache->valid & VALID_CARRIER)) {
562 fn = xasprintf("/sys/class/net/%s/carrier", netdev_get_name(netdev_));
563 fd = open(fn, O_RDONLY);
566 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(error));
570 retval = read(fd, line, sizeof line);
573 if (error == EINVAL) {
574 /* This is the normal return value when we try to check carrier
575 * if the network device is not up. */
577 VLOG_WARN_RL(&rl, "%s: read failed: %s", fn, strerror(error));
580 } else if (retval == 0) {
582 VLOG_WARN_RL(&rl, "%s: unexpected end of file", fn);
586 if (line[0] != '0' && line[0] != '1') {
588 VLOG_WARN_RL(&rl, "%s: value is %c (expected 0 or 1)",
592 netdev->cache->carrier = line[0] != '0';
593 netdev->cache->valid |= VALID_CARRIER;
595 *carrier = netdev->cache->carrier;
606 /* Check whether we can we use RTM_GETLINK to get network device statistics.
607 * In pre-2.6.19 kernels, this was only available if wireless extensions were
610 check_for_working_netlink_stats(void)
612 /* Decide on the netdev_get_stats() implementation to use. Netlink is
613 * preferable, so if that works, we'll use it. */
614 int ifindex = do_get_ifindex("lo");
616 VLOG_WARN("failed to get ifindex for lo, "
617 "obtaining netdev stats from proc");
620 struct netdev_stats stats;
621 int error = get_stats_via_netlink(ifindex, &stats);
623 VLOG_DBG("obtaining netdev stats via rtnetlink");
626 VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
627 "via proc (you are probably running a pre-2.6.19 "
628 "kernel)", strerror(error));
634 /* Retrieves current device stats for 'netdev'.
636 * XXX All of the members of struct netdev_stats are 64 bits wide, but on
637 * 32-bit architectures the Linux network stats are only 32 bits. */
639 netdev_linux_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
641 static int use_netlink_stats = -1;
644 COVERAGE_INC(netdev_get_stats);
645 if (use_netlink_stats < 0) {
646 use_netlink_stats = check_for_working_netlink_stats();
648 if (use_netlink_stats) {
651 error = get_ifindex(netdev, &ifindex);
653 error = get_stats_via_netlink(ifindex, stats);
656 error = get_stats_via_proc(netdev->name, stats);
661 /* Stores the features supported by 'netdev' into each of '*current',
662 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
663 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
664 * successful, otherwise a positive errno value. On failure, all of the
665 * passed-in values are set to 0. */
667 netdev_linux_get_features(struct netdev *netdev,
668 uint32_t *current, uint32_t *advertised,
669 uint32_t *supported, uint32_t *peer)
671 struct ethtool_cmd ecmd;
674 memset(&ecmd, 0, sizeof ecmd);
675 error = netdev_linux_do_ethtool(netdev, &ecmd,
676 ETHTOOL_GSET, "ETHTOOL_GSET");
681 /* Supported features. */
683 if (ecmd.supported & SUPPORTED_10baseT_Half) {
684 *supported |= OFPPF_10MB_HD;
686 if (ecmd.supported & SUPPORTED_10baseT_Full) {
687 *supported |= OFPPF_10MB_FD;
689 if (ecmd.supported & SUPPORTED_100baseT_Half) {
690 *supported |= OFPPF_100MB_HD;
692 if (ecmd.supported & SUPPORTED_100baseT_Full) {
693 *supported |= OFPPF_100MB_FD;
695 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
696 *supported |= OFPPF_1GB_HD;
698 if (ecmd.supported & SUPPORTED_1000baseT_Full) {
699 *supported |= OFPPF_1GB_FD;
701 if (ecmd.supported & SUPPORTED_10000baseT_Full) {
702 *supported |= OFPPF_10GB_FD;
704 if (ecmd.supported & SUPPORTED_TP) {
705 *supported |= OFPPF_COPPER;
707 if (ecmd.supported & SUPPORTED_FIBRE) {
708 *supported |= OFPPF_FIBER;
710 if (ecmd.supported & SUPPORTED_Autoneg) {
711 *supported |= OFPPF_AUTONEG;
713 if (ecmd.supported & SUPPORTED_Pause) {
714 *supported |= OFPPF_PAUSE;
716 if (ecmd.supported & SUPPORTED_Asym_Pause) {
717 *supported |= OFPPF_PAUSE_ASYM;
720 /* Advertised features. */
722 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
723 *advertised |= OFPPF_10MB_HD;
725 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
726 *advertised |= OFPPF_10MB_FD;
728 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
729 *advertised |= OFPPF_100MB_HD;
731 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
732 *advertised |= OFPPF_100MB_FD;
734 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
735 *advertised |= OFPPF_1GB_HD;
737 if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
738 *advertised |= OFPPF_1GB_FD;
740 if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
741 *advertised |= OFPPF_10GB_FD;
743 if (ecmd.advertising & ADVERTISED_TP) {
744 *advertised |= OFPPF_COPPER;
746 if (ecmd.advertising & ADVERTISED_FIBRE) {
747 *advertised |= OFPPF_FIBER;
749 if (ecmd.advertising & ADVERTISED_Autoneg) {
750 *advertised |= OFPPF_AUTONEG;
752 if (ecmd.advertising & ADVERTISED_Pause) {
753 *advertised |= OFPPF_PAUSE;
755 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
756 *advertised |= OFPPF_PAUSE_ASYM;
759 /* Current settings. */
760 if (ecmd.speed == SPEED_10) {
761 *current = ecmd.duplex ? OFPPF_10MB_FD : OFPPF_10MB_HD;
762 } else if (ecmd.speed == SPEED_100) {
763 *current = ecmd.duplex ? OFPPF_100MB_FD : OFPPF_100MB_HD;
764 } else if (ecmd.speed == SPEED_1000) {
765 *current = ecmd.duplex ? OFPPF_1GB_FD : OFPPF_1GB_HD;
766 } else if (ecmd.speed == SPEED_10000) {
767 *current = OFPPF_10GB_FD;
772 if (ecmd.port == PORT_TP) {
773 *current |= OFPPF_COPPER;
774 } else if (ecmd.port == PORT_FIBRE) {
775 *current |= OFPPF_FIBER;
779 *current |= OFPPF_AUTONEG;
782 /* Peer advertisements. */
788 /* Set the features advertised by 'netdev' to 'advertise'. */
790 netdev_linux_set_advertisements(struct netdev *netdev, uint32_t advertise)
792 struct ethtool_cmd ecmd;
795 memset(&ecmd, 0, sizeof ecmd);
796 error = netdev_linux_do_ethtool(netdev, &ecmd,
797 ETHTOOL_GSET, "ETHTOOL_GSET");
802 ecmd.advertising = 0;
803 if (advertise & OFPPF_10MB_HD) {
804 ecmd.advertising |= ADVERTISED_10baseT_Half;
806 if (advertise & OFPPF_10MB_FD) {
807 ecmd.advertising |= ADVERTISED_10baseT_Full;
809 if (advertise & OFPPF_100MB_HD) {
810 ecmd.advertising |= ADVERTISED_100baseT_Half;
812 if (advertise & OFPPF_100MB_FD) {
813 ecmd.advertising |= ADVERTISED_100baseT_Full;
815 if (advertise & OFPPF_1GB_HD) {
816 ecmd.advertising |= ADVERTISED_1000baseT_Half;
818 if (advertise & OFPPF_1GB_FD) {
819 ecmd.advertising |= ADVERTISED_1000baseT_Full;
821 if (advertise & OFPPF_10GB_FD) {
822 ecmd.advertising |= ADVERTISED_10000baseT_Full;
824 if (advertise & OFPPF_COPPER) {
825 ecmd.advertising |= ADVERTISED_TP;
827 if (advertise & OFPPF_FIBER) {
828 ecmd.advertising |= ADVERTISED_FIBRE;
830 if (advertise & OFPPF_AUTONEG) {
831 ecmd.advertising |= ADVERTISED_Autoneg;
833 if (advertise & OFPPF_PAUSE) {
834 ecmd.advertising |= ADVERTISED_Pause;
836 if (advertise & OFPPF_PAUSE_ASYM) {
837 ecmd.advertising |= ADVERTISED_Asym_Pause;
839 return netdev_linux_do_ethtool(netdev, &ecmd,
840 ETHTOOL_SSET, "ETHTOOL_SSET");
843 /* If 'netdev_name' is the name of a VLAN network device (e.g. one created with
844 * vconfig(8)), sets '*vlan_vid' to the VLAN VID associated with that device
845 * and returns 0. Otherwise returns a errno value (specifically ENOENT if
846 * 'netdev_name' is the name of a network device that is not a VLAN device) and
847 * sets '*vlan_vid' to -1. */
849 netdev_linux_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
851 const char *netdev_name = netdev_get_name(netdev);
852 struct ds line = DS_EMPTY_INITIALIZER;
857 COVERAGE_INC(netdev_get_vlan_vid);
858 fn = xasprintf("/proc/net/vlan/%s", netdev_name);
859 stream = fopen(fn, "r");
865 if (ds_get_line(&line, stream)) {
866 if (ferror(stream)) {
868 VLOG_ERR_RL(&rl, "error reading \"%s\": %s", fn, strerror(errno));
871 VLOG_ERR_RL(&rl, "unexpected end of file reading \"%s\"", fn);
876 if (!sscanf(ds_cstr(&line), "%*s VID: %d", vlan_vid)) {
878 VLOG_ERR_RL(&rl, "parse error reading \"%s\" line 1: \"%s\"",
897 #define POLICE_ADD_CMD "/sbin/tc qdisc add dev %s handle ffff: ingress"
898 #define POLICE_CONFIG_CMD "/sbin/tc filter add dev %s parent ffff: protocol ip prio 50 u32 match ip src 0.0.0.0/0 police rate %dkbit burst %dk mtu 65535 drop flowid :1"
899 /* We redirect stderr to /dev/null because we often want to remove all
900 * traffic control configuration on a port so its in a known state. If
901 * this done when there is no such configuration, tc complains, so we just
904 #define POLICE_DEL_CMD "/sbin/tc qdisc del dev %s handle ffff: ingress 2>/dev/null"
906 /* Attempts to set input rate limiting (policing) policy. */
908 netdev_linux_set_policing(struct netdev *netdev,
909 uint32_t kbits_rate, uint32_t kbits_burst)
911 const char *netdev_name = netdev_get_name(netdev);
914 COVERAGE_INC(netdev_set_policing);
917 /* Default to 10 kilobits if not specified. */
921 /* xxx This should be more careful about only adding if it
922 * xxx actually exists, as opposed to always deleting it. */
923 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
924 if (system(command) == -1) {
925 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
928 snprintf(command, sizeof(command), POLICE_ADD_CMD, netdev_name);
929 if (system(command) != 0) {
930 VLOG_WARN_RL(&rl, "%s: problem adding policing", netdev_name);
934 snprintf(command, sizeof(command), POLICE_CONFIG_CMD, netdev_name,
935 kbits_rate, kbits_burst);
936 if (system(command) != 0) {
937 VLOG_WARN_RL(&rl, "%s: problem configuring policing",
942 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
943 if (system(command) == -1) {
944 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
951 /* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
952 * 'in4' is non-null) and returns true. Otherwise, returns false. */
954 netdev_linux_get_in4(const struct netdev *netdev_, struct in_addr *in4)
956 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
957 if (!(netdev->cache->valid & VALID_IN4)) {
958 const struct sockaddr_in *sin;
962 ifr.ifr_addr.sa_family = AF_INET;
963 error = netdev_linux_do_ioctl(netdev_, &ifr,
964 SIOCGIFADDR, "SIOCGIFADDR");
969 sin = (struct sockaddr_in *) &ifr.ifr_addr;
970 netdev->cache->in4 = sin->sin_addr;
971 netdev->cache->valid |= VALID_IN4;
973 *in4 = netdev->cache->in4;
974 return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
977 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
978 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
979 * positive errno value. */
981 netdev_linux_set_in4(struct netdev *netdev_, struct in_addr addr,
984 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
987 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
989 netdev->cache->valid |= VALID_IN4;
990 netdev->cache->in4 = addr;
991 if (addr.s_addr != INADDR_ANY) {
992 error = do_set_addr(netdev_, SIOCSIFNETMASK,
993 "SIOCSIFNETMASK", mask);
1000 parse_if_inet6_line(const char *line,
1001 struct in6_addr *in6, char ifname[16 + 1])
1003 uint8_t *s6 = in6->s6_addr;
1004 #define X8 "%2"SCNx8
1006 " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
1007 "%*x %*x %*x %*x %16s\n",
1008 &s6[0], &s6[1], &s6[2], &s6[3],
1009 &s6[4], &s6[5], &s6[6], &s6[7],
1010 &s6[8], &s6[9], &s6[10], &s6[11],
1011 &s6[12], &s6[13], &s6[14], &s6[15],
1015 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
1016 * 'in6' is non-null) and returns true. Otherwise, returns false. */
1018 netdev_linux_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1020 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1021 if (!(netdev->cache->valid & VALID_IN6)) {
1025 netdev->cache->in6 = in6addr_any;
1027 file = fopen("/proc/net/if_inet6", "r");
1029 const char *name = netdev_get_name(netdev_);
1030 while (fgets(line, sizeof line, file)) {
1031 struct in6_addr in6;
1032 char ifname[16 + 1];
1033 if (parse_if_inet6_line(line, &in6, ifname)
1034 && !strcmp(name, ifname))
1036 netdev->cache->in6 = in6;
1042 netdev->cache->valid |= VALID_IN6;
1044 *in6 = netdev->cache->in6;
1049 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1051 struct sockaddr_in sin;
1052 memset(&sin, 0, sizeof sin);
1053 sin.sin_family = AF_INET;
1054 sin.sin_addr = addr;
1057 memset(sa, 0, sizeof *sa);
1058 memcpy(sa, &sin, sizeof sin);
1062 do_set_addr(struct netdev *netdev,
1063 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1066 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1067 make_in4_sockaddr(&ifr.ifr_addr, addr);
1068 return netdev_linux_do_ioctl(netdev, &ifr, ioctl_nr, ioctl_name);
1071 /* Adds 'router' as a default IP gateway. */
1073 netdev_linux_add_router(struct netdev *netdev UNUSED, struct in_addr router)
1075 struct in_addr any = { INADDR_ANY };
1079 memset(&rt, 0, sizeof rt);
1080 make_in4_sockaddr(&rt.rt_dst, any);
1081 make_in4_sockaddr(&rt.rt_gateway, router);
1082 make_in4_sockaddr(&rt.rt_genmask, any);
1083 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1084 COVERAGE_INC(netdev_add_router);
1085 error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
1087 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
1092 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1093 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1094 * returns 0. Otherwise, it returns a positive errno value; in particular,
1095 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
1097 netdev_linux_arp_lookup(const struct netdev *netdev,
1098 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
1101 struct sockaddr_in *pa;
1104 memset(&r, 0, sizeof r);
1105 pa = (struct sockaddr_in *) &r.arp_pa;
1106 pa->sin_family = AF_INET;
1107 pa->sin_addr.s_addr = ip;
1109 r.arp_ha.sa_family = ARPHRD_ETHER;
1111 strncpy(r.arp_dev, netdev->name, sizeof r.arp_dev);
1112 COVERAGE_INC(netdev_arp_lookup);
1113 retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
1115 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
1116 } else if (retval != ENXIO) {
1117 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
1118 netdev->name, IP_ARGS(&ip), strerror(retval));
1124 nd_to_iff_flags(enum netdev_flags nd)
1127 if (nd & NETDEV_UP) {
1130 if (nd & NETDEV_PROMISC) {
1137 iff_to_nd_flags(int iff)
1139 enum netdev_flags nd = 0;
1143 if (iff & IFF_PROMISC) {
1144 nd |= NETDEV_PROMISC;
1150 netdev_linux_update_flags(struct netdev *netdev, enum netdev_flags off,
1151 enum netdev_flags on, enum netdev_flags *old_flagsp)
1153 int old_flags, new_flags;
1156 error = get_flags(netdev, &old_flags);
1158 *old_flagsp = iff_to_nd_flags(old_flags);
1159 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1160 if (new_flags != old_flags) {
1161 error = set_flags(netdev, new_flags);
1168 poll_notify(struct list *list)
1170 struct netdev_linux_notifier *notifier;
1171 LIST_FOR_EACH (notifier, struct netdev_linux_notifier, node, list) {
1172 struct netdev_notifier *n = ¬ifier->notifier;
1178 netdev_linux_poll_cb(const struct rtnetlink_change *change,
1182 struct list *list = shash_find_data(&netdev_linux_notifiers,
1188 struct shash_node *node;
1189 SHASH_FOR_EACH (node, &netdev_linux_notifiers) {
1190 poll_notify(node->data);
1196 netdev_linux_poll_add(struct netdev *netdev,
1197 void (*cb)(struct netdev_notifier *), void *aux,
1198 struct netdev_notifier **notifierp)
1200 const char *netdev_name = netdev_get_name(netdev);
1201 struct netdev_linux_notifier *notifier;
1204 if (shash_is_empty(&netdev_linux_notifiers)) {
1205 int error = rtnetlink_notifier_register(&netdev_linux_poll_notifier,
1206 netdev_linux_poll_cb, NULL);
1212 list = shash_find_data(&netdev_linux_notifiers, netdev_name);
1214 list = xmalloc(sizeof *list);
1216 shash_add(&netdev_linux_notifiers, netdev_name, list);
1219 notifier = xmalloc(sizeof *notifier);
1220 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
1221 list_push_back(list, ¬ifier->node);
1222 *notifierp = ¬ifier->notifier;
1227 netdev_linux_poll_remove(struct netdev_notifier *notifier_)
1229 struct netdev_linux_notifier *notifier =
1230 CONTAINER_OF(notifier_, struct netdev_linux_notifier, notifier);
1233 /* Remove 'notifier' from its list. */
1234 list = list_remove(¬ifier->node);
1235 if (list_is_empty(list)) {
1236 /* The list is now empty. Remove it from the hash and free it. */
1237 const char *netdev_name = netdev_get_name(notifier->notifier.netdev);
1238 shash_delete(&netdev_linux_notifiers,
1239 shash_find(&netdev_linux_notifiers, netdev_name));
1244 /* If that was the last notifier, unregister. */
1245 if (shash_is_empty(&netdev_linux_notifiers)) {
1246 rtnetlink_notifier_unregister(&netdev_linux_poll_notifier);
1250 const struct netdev_class netdev_linux_class = {
1261 netdev_linux_enumerate,
1264 netdev_linux_recv_wait,
1268 netdev_linux_send_wait,
1270 netdev_linux_set_etheraddr,
1271 netdev_linux_get_etheraddr,
1272 netdev_linux_get_mtu,
1273 netdev_linux_get_carrier,
1274 netdev_linux_get_stats,
1276 netdev_linux_get_features,
1277 netdev_linux_set_advertisements,
1278 netdev_linux_get_vlan_vid,
1279 netdev_linux_set_policing,
1281 netdev_linux_get_in4,
1282 netdev_linux_set_in4,
1283 netdev_linux_get_in6,
1284 netdev_linux_add_router,
1285 netdev_linux_arp_lookup,
1287 netdev_linux_update_flags,
1289 netdev_linux_poll_add,
1290 netdev_linux_poll_remove,
1293 const struct netdev_class netdev_tap_class = {
1304 netdev_linux_enumerate,
1307 netdev_linux_recv_wait,
1311 netdev_linux_send_wait,
1313 netdev_linux_set_etheraddr,
1314 netdev_linux_get_etheraddr,
1315 netdev_linux_get_mtu,
1316 netdev_linux_get_carrier,
1317 netdev_linux_get_stats,
1319 netdev_linux_get_features,
1320 netdev_linux_set_advertisements,
1321 netdev_linux_get_vlan_vid,
1322 netdev_linux_set_policing,
1324 netdev_linux_get_in4,
1325 netdev_linux_set_in4,
1326 netdev_linux_get_in6,
1327 netdev_linux_add_router,
1328 netdev_linux_arp_lookup,
1330 netdev_linux_update_flags,
1332 netdev_linux_poll_add,
1333 netdev_linux_poll_remove,
1337 get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
1339 static struct nl_sock *rtnl_sock;
1340 struct ofpbuf request;
1341 struct ofpbuf *reply;
1342 struct ifinfomsg *ifi;
1343 const struct rtnl_link_stats *rtnl_stats;
1344 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1348 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0, &rtnl_sock);
1350 VLOG_ERR_RL(&rl, "failed to create rtnetlink socket: %s",
1356 ofpbuf_init(&request, 0);
1357 nl_msg_put_nlmsghdr(&request, rtnl_sock, sizeof *ifi,
1358 RTM_GETLINK, NLM_F_REQUEST);
1359 ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
1360 ifi->ifi_family = PF_UNSPEC;
1361 ifi->ifi_index = ifindex;
1362 error = nl_sock_transact(rtnl_sock, &request, &reply);
1363 ofpbuf_uninit(&request);
1368 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1369 rtnlgrp_link_policy,
1370 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1371 ofpbuf_delete(reply);
1375 if (!attrs[IFLA_STATS]) {
1376 VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
1380 rtnl_stats = nl_attr_get(attrs[IFLA_STATS]);
1381 stats->rx_packets = rtnl_stats->rx_packets;
1382 stats->tx_packets = rtnl_stats->tx_packets;
1383 stats->rx_bytes = rtnl_stats->rx_bytes;
1384 stats->tx_bytes = rtnl_stats->tx_bytes;
1385 stats->rx_errors = rtnl_stats->rx_errors;
1386 stats->tx_errors = rtnl_stats->tx_errors;
1387 stats->rx_dropped = rtnl_stats->rx_dropped;
1388 stats->tx_dropped = rtnl_stats->tx_dropped;
1389 stats->multicast = rtnl_stats->multicast;
1390 stats->collisions = rtnl_stats->collisions;
1391 stats->rx_length_errors = rtnl_stats->rx_length_errors;
1392 stats->rx_over_errors = rtnl_stats->rx_over_errors;
1393 stats->rx_crc_errors = rtnl_stats->rx_crc_errors;
1394 stats->rx_frame_errors = rtnl_stats->rx_frame_errors;
1395 stats->rx_fifo_errors = rtnl_stats->rx_fifo_errors;
1396 stats->rx_missed_errors = rtnl_stats->rx_missed_errors;
1397 stats->tx_aborted_errors = rtnl_stats->tx_aborted_errors;
1398 stats->tx_carrier_errors = rtnl_stats->tx_carrier_errors;
1399 stats->tx_fifo_errors = rtnl_stats->tx_fifo_errors;
1400 stats->tx_heartbeat_errors = rtnl_stats->tx_heartbeat_errors;
1401 stats->tx_window_errors = rtnl_stats->tx_window_errors;
1407 get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats)
1409 static const char fn[] = "/proc/net/dev";
1414 stream = fopen(fn, "r");
1416 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1421 while (fgets(line, sizeof line, stream)) {
1424 #define X64 "%"SCNu64
1427 X64 X64 X64 X64 X64 X64 X64 "%*u"
1428 X64 X64 X64 X64 X64 X64 X64 "%*u",
1434 &stats->rx_fifo_errors,
1435 &stats->rx_frame_errors,
1441 &stats->tx_fifo_errors,
1443 &stats->tx_carrier_errors) != 15) {
1444 VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln);
1445 } else if (!strcmp(devname, netdev_name)) {
1446 stats->rx_length_errors = UINT64_MAX;
1447 stats->rx_over_errors = UINT64_MAX;
1448 stats->rx_crc_errors = UINT64_MAX;
1449 stats->rx_missed_errors = UINT64_MAX;
1450 stats->tx_aborted_errors = UINT64_MAX;
1451 stats->tx_heartbeat_errors = UINT64_MAX;
1452 stats->tx_window_errors = UINT64_MAX;
1458 VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name);
1464 get_flags(const struct netdev *netdev, int *flags)
1469 error = netdev_linux_do_ioctl(netdev, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
1470 *flags = ifr.ifr_flags;
1475 set_flags(struct netdev *netdev, int flags)
1479 ifr.ifr_flags = flags;
1480 return netdev_linux_do_ioctl(netdev, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1484 do_get_ifindex(const char *netdev_name)
1488 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1489 COVERAGE_INC(netdev_get_ifindex);
1490 if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) {
1491 VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
1492 netdev_name, strerror(errno));
1495 return ifr.ifr_ifindex;
1499 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1501 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1503 if (!(netdev->cache->valid & VALID_IFINDEX)) {
1504 int ifindex = do_get_ifindex(netdev_get_name(netdev_));
1508 netdev->cache->valid |= VALID_IFINDEX;
1509 netdev->cache->ifindex = ifindex;
1511 *ifindexp = netdev->cache->ifindex;
1516 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1521 memset(&ifr, 0, sizeof ifr);
1522 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1523 COVERAGE_INC(netdev_get_hwaddr);
1524 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
1525 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
1526 netdev_name, strerror(errno));
1529 hwaddr_family = ifr.ifr_hwaddr.sa_family;
1530 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
1531 VLOG_WARN("%s device has unknown hardware address family %d",
1532 netdev_name, hwaddr_family);
1534 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
1539 set_etheraddr(const char *netdev_name, int hwaddr_family,
1540 const uint8_t mac[ETH_ADDR_LEN])
1544 memset(&ifr, 0, sizeof ifr);
1545 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1546 ifr.ifr_hwaddr.sa_family = hwaddr_family;
1547 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
1548 COVERAGE_INC(netdev_set_hwaddr);
1549 if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) {
1550 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
1551 netdev_name, strerror(errno));
1558 netdev_linux_do_ethtool(struct netdev *netdev, struct ethtool_cmd *ecmd,
1559 int cmd, const char *cmd_name)
1563 memset(&ifr, 0, sizeof ifr);
1564 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1565 ifr.ifr_data = (caddr_t) ecmd;
1568 COVERAGE_INC(netdev_ethtool);
1569 if (ioctl(af_inet_sock, SIOCETHTOOL, &ifr) == 0) {
1572 if (errno != EOPNOTSUPP) {
1573 VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
1574 "failed: %s", cmd_name, netdev->name,
1577 /* The device doesn't support this operation. That's pretty
1578 * common, so there's no point in logging anything. */
1585 netdev_linux_do_ioctl(const struct netdev *netdev, struct ifreq *ifr,
1586 int cmd, const char *cmd_name)
1588 strncpy(ifr->ifr_name, netdev_get_name(netdev), sizeof ifr->ifr_name);
1589 if (ioctl(af_inet_sock, cmd, ifr) == -1) {
1590 VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s",
1591 netdev_get_name(netdev), cmd_name, strerror(errno));
1597 /* rtnetlink socket. */
1598 static struct nl_sock *notify_sock;
1600 /* All registered notifiers. */
1601 static struct list all_notifiers = LIST_INITIALIZER(&all_notifiers);
1603 static void rtnetlink_report_change(const struct nlmsghdr *,
1604 const struct ifinfomsg *,
1605 struct nlattr *attrs[]);
1606 static void rtnetlink_report_notify_error(void);
1608 /* Registers 'cb' to be called with auxiliary data 'aux' with network device
1609 * change notifications. The notifier is stored in 'notifier', which the
1610 * caller must not modify or free.
1612 * This is probably not the function that you want. You should probably be
1613 * using dpif_port_poll() or netdev_monitor_create(), which unlike this
1614 * function are not Linux-specific.
1616 * Returns 0 if successful, otherwise a positive errno value. */
1618 rtnetlink_notifier_register(struct rtnetlink_notifier *notifier,
1619 rtnetlink_notify_func *cb, void *aux)
1622 int error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0,
1625 VLOG_WARN("could not create rtnetlink socket: %s",
1630 /* Catch up on notification work so that the new notifier won't
1631 * receive any stale notifications. */
1632 rtnetlink_notifier_run();
1635 list_push_back(&all_notifiers, ¬ifier->node);
1637 notifier->aux = aux;
1641 /* Cancels notification on 'notifier', which must have previously been
1642 * registered with lxnetdev_notifier_register(). */
1644 rtnetlink_notifier_unregister(struct rtnetlink_notifier *notifier)
1646 list_remove(¬ifier->node);
1647 if (list_is_empty(&all_notifiers)) {
1648 nl_sock_destroy(notify_sock);
1653 /* Calls all of the registered notifiers, passing along any as-yet-unreported
1654 * netdev change events. */
1656 rtnetlink_notifier_run(void)
1658 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1665 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1669 error = nl_sock_recv(notify_sock, &buf, false);
1671 if (nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1672 rtnlgrp_link_policy,
1673 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1674 struct ifinfomsg *ifinfo;
1676 ifinfo = (void *) ((char *) buf->data + NLMSG_HDRLEN);
1677 rtnetlink_report_change(buf->data, ifinfo, attrs);
1679 VLOG_WARN_RL(&rl, "received bad rtnl message");
1680 rtnetlink_report_notify_error();
1683 } else if (error == EAGAIN) {
1686 if (error == ENOBUFS) {
1687 VLOG_WARN_RL(&rl, "rtnetlink receive buffer overflowed");
1689 VLOG_WARN_RL(&rl, "error reading rtnetlink socket: %s",
1692 rtnetlink_report_notify_error();
1697 /* Causes poll_block() to wake up when network device change notifications are
1700 rtnetlink_notifier_wait(void)
1703 nl_sock_wait(notify_sock, POLLIN);
1708 rtnetlink_report_change(const struct nlmsghdr *nlmsg,
1709 const struct ifinfomsg *ifinfo,
1710 struct nlattr *attrs[])
1712 struct rtnetlink_notifier *notifier;
1713 struct rtnetlink_change change;
1715 COVERAGE_INC(rtnetlink_changed);
1717 change.nlmsg_type = nlmsg->nlmsg_type;
1718 change.ifi_index = ifinfo->ifi_index;
1719 change.ifname = nl_attr_get_string(attrs[IFLA_IFNAME]);
1720 change.master_ifindex = (attrs[IFLA_MASTER]
1721 ? nl_attr_get_u32(attrs[IFLA_MASTER]) : 0);
1723 LIST_FOR_EACH (notifier, struct rtnetlink_notifier, node,
1725 notifier->cb(&change, notifier->aux);
1730 rtnetlink_report_notify_error(void)
1732 struct rtnetlink_notifier *notifier;
1734 LIST_FOR_EACH (notifier, struct rtnetlink_notifier, node,
1736 notifier->cb(NULL, notifier->aux);