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.
21 #include <arpa/inet.h>
23 #include <linux/if_tun.h>
24 #include <linux/types.h>
25 #include <linux/ethtool.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/sockios.h>
28 #include <linux/version.h>
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <netpacket/packet.h>
33 #include <net/ethernet.h>
35 #include <net/if_arp.h>
36 #include <net/if_packet.h>
37 #include <net/route.h>
38 #include <netinet/in.h>
45 #include "dynamic-string.h"
46 #include "fatal-signal.h"
47 #include "netdev-provider.h"
50 #include "openflow/openflow.h"
52 #include "poll-loop.h"
53 #include "rtnetlink.h"
54 #include "socket-util.h"
58 #define THIS_MODULE VLM_netdev_linux
61 /* These were introduced in Linux 2.6.14, so they might be missing if we have
63 #ifndef ADVERTISED_Pause
64 #define ADVERTISED_Pause (1 << 13)
66 #ifndef ADVERTISED_Asym_Pause
67 #define ADVERTISED_Asym_Pause (1 << 14)
73 /* File descriptors. For ordinary network devices, the two fds below are
74 * the same; for tap devices, they differ. */
75 int netdev_fd; /* Network device. */
76 int tap_fd; /* TAP character device, if any, otherwise the
79 struct netdev_linux_cache *cache;
83 VALID_IFINDEX = 1 << 0,
84 VALID_ETHERADDR = 1 << 1,
88 VALID_CARRIER = 1 << 5,
89 VALID_IS_INTERNAL = 1 << 6
92 /* Cached network device information. */
93 struct netdev_linux_cache {
94 struct shash_node *shash_node;
99 uint8_t etheraddr[ETH_ADDR_LEN];
100 struct in_addr address, netmask;
107 static struct shash cache_map = SHASH_INITIALIZER(&cache_map);
108 static struct rtnetlink_notifier netdev_linux_cache_notifier;
110 /* An AF_INET socket (used for ioctl operations). */
111 static int af_inet_sock = -1;
113 struct netdev_linux_notifier {
114 struct netdev_notifier notifier;
118 static struct shash netdev_linux_notifiers =
119 SHASH_INITIALIZER(&netdev_linux_notifiers);
120 static struct rtnetlink_notifier netdev_linux_poll_notifier;
122 /* This is set pretty low because we probably won't learn anything from the
123 * additional log messages. */
124 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
126 static int netdev_linux_do_ethtool(struct netdev *, struct ethtool_cmd *,
127 int cmd, const char *cmd_name);
128 static int netdev_linux_do_ioctl(const struct netdev *, struct ifreq *,
129 int cmd, const char *cmd_name);
130 static int netdev_linux_get_ipv4(const struct netdev *, struct in_addr *,
131 int cmd, const char *cmd_name);
132 static int get_flags(const struct netdev *, int *flagsp);
133 static int set_flags(struct netdev *, int flags);
134 static int do_get_ifindex(const char *netdev_name);
135 static int get_ifindex(const struct netdev *, int *ifindexp);
136 static int do_set_addr(struct netdev *netdev,
137 int ioctl_nr, const char *ioctl_name,
138 struct in_addr addr);
139 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
140 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
141 const uint8_t[ETH_ADDR_LEN]);
142 static int get_stats_via_netlink(int ifindex, struct netdev_stats *stats);
143 static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats);
145 static struct netdev_linux *
146 netdev_linux_cast(const struct netdev *netdev)
148 netdev_assert_class(netdev, &netdev_linux_class);
149 return CONTAINER_OF(netdev, struct netdev_linux, netdev);
153 netdev_linux_init(void)
155 static int status = -1;
157 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
158 status = af_inet_sock >= 0 ? 0 : errno;
160 VLOG_ERR("failed to create inet socket: %s", strerror(status));
167 netdev_linux_run(void)
169 rtnetlink_notifier_run();
173 netdev_linux_wait(void)
175 rtnetlink_notifier_wait();
179 netdev_linux_cache_cb(const struct rtnetlink_change *change,
182 struct netdev_linux_cache *cache;
184 cache = shash_find_data(&cache_map, change->ifname);
189 struct shash_node *node;
190 SHASH_FOR_EACH (node, &cache_map) {
198 netdev_linux_open(const char *name, char *suffix, int ethertype,
199 struct netdev **netdevp)
201 struct netdev_linux *netdev;
202 enum netdev_flags flags;
205 /* Allocate network device. */
206 netdev = xcalloc(1, sizeof *netdev);
207 netdev_init(&netdev->netdev, suffix, &netdev_linux_class);
208 netdev->netdev_fd = -1;
210 netdev->cache = shash_find_data(&cache_map, suffix);
211 if (!netdev->cache) {
212 if (shash_is_empty(&cache_map)) {
213 int error = rtnetlink_notifier_register(
214 &netdev_linux_cache_notifier, netdev_linux_cache_cb, NULL);
216 netdev_close(&netdev->netdev);
220 netdev->cache = xmalloc(sizeof *netdev->cache);
221 netdev->cache->shash_node = shash_add(&cache_map, suffix,
223 netdev->cache->valid = 0;
224 netdev->cache->ref_cnt = 0;
226 netdev->cache->ref_cnt++;
228 if (!strncmp(name, "tap:", 4)) {
229 static const char tap_dev[] = "/dev/net/tun";
232 /* Open tap device. */
233 netdev->tap_fd = open(tap_dev, O_RDWR);
234 if (netdev->tap_fd < 0) {
236 VLOG_WARN("opening \"%s\" failed: %s", tap_dev, strerror(error));
240 /* Create tap device. */
241 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
242 strncpy(ifr.ifr_name, suffix, sizeof ifr.ifr_name);
243 if (ioctl(netdev->tap_fd, TUNSETIFF, &ifr) == -1) {
244 VLOG_WARN("%s: creating tap device failed: %s", suffix,
250 /* Make non-blocking. */
251 error = set_nonblocking(netdev->tap_fd);
257 error = netdev_get_flags(&netdev->netdev, &flags);
258 if (error == ENODEV) {
262 if (netdev->tap_fd >= 0 || ethertype != NETDEV_ETH_TYPE_NONE) {
263 struct sockaddr_ll sll;
267 /* Create file descriptor. */
268 protocol = (ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
269 : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
271 netdev->netdev_fd = socket(PF_PACKET, SOCK_RAW, htons(protocol));
272 if (netdev->netdev_fd < 0) {
276 if (netdev->tap_fd < 0) {
277 netdev->tap_fd = netdev->netdev_fd;
280 /* Set non-blocking mode. */
281 error = set_nonblocking(netdev->netdev_fd);
286 /* Get ethernet device index. */
287 error = get_ifindex(&netdev->netdev, &ifindex);
292 /* Bind to specific ethernet device. */
293 memset(&sll, 0, sizeof sll);
294 sll.sll_family = AF_PACKET;
295 sll.sll_ifindex = ifindex;
296 if (bind(netdev->netdev_fd,
297 (struct sockaddr *) &sll, sizeof sll) < 0) {
299 VLOG_ERR("bind to %s failed: %s", suffix, strerror(error));
303 /* Between the socket() and bind() calls above, the socket receives all
304 * packets of the requested type on all system interfaces. We do not
305 * want to receive that data, but there is no way to avoid it. So we
306 * must now drain out the receive queue. */
307 error = drain_rcvbuf(netdev->netdev_fd);
313 *netdevp = &netdev->netdev;
317 netdev_close(&netdev->netdev);
321 /* Closes and destroys 'netdev'. */
323 netdev_linux_close(struct netdev *netdev_)
325 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
327 if (netdev->cache && !--netdev->cache->ref_cnt) {
328 shash_delete(&cache_map, netdev->cache->shash_node);
331 if (shash_is_empty(&cache_map)) {
332 rtnetlink_notifier_unregister(&netdev_linux_cache_notifier);
335 if (netdev->netdev_fd >= 0) {
336 close(netdev->netdev_fd);
338 if (netdev->tap_fd >= 0 && netdev->netdev_fd != netdev->tap_fd) {
339 close(netdev->tap_fd);
344 /* Initializes 'svec' with a list of the names of all known network devices. */
346 netdev_linux_enumerate(struct svec *svec)
348 struct if_nameindex *names;
350 names = if_nameindex();
354 for (i = 0; names[i].if_name != NULL; i++) {
355 svec_add(svec, names[i].if_name);
357 if_freenameindex(names);
360 VLOG_WARN("could not obtain list of network device names: %s",
367 netdev_linux_recv(struct netdev *netdev_, void *data, size_t size)
369 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
371 if (netdev->tap_fd < 0) {
372 /* Device was opened with NETDEV_ETH_TYPE_NONE. */
377 ssize_t retval = read(netdev->tap_fd, data, size);
380 } else if (errno != EINTR) {
381 if (errno != EAGAIN) {
382 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
383 strerror(errno), netdev_get_name(netdev_));
390 /* Registers with the poll loop to wake up from the next call to poll_block()
391 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
393 netdev_linux_recv_wait(struct netdev *netdev_)
395 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
396 if (netdev->tap_fd >= 0) {
397 poll_fd_wait(netdev->tap_fd, POLLIN);
401 /* Discards all packets waiting to be received from 'netdev'. */
403 netdev_linux_drain(struct netdev *netdev_)
405 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
406 if (netdev->tap_fd < 0 && netdev->netdev_fd < 0) {
408 } else if (netdev->tap_fd != netdev->netdev_fd) {
410 int error = netdev_linux_do_ioctl(netdev_, &ifr,
411 SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
415 drain_fd(netdev->tap_fd, ifr.ifr_qlen);
418 return drain_rcvbuf(netdev->netdev_fd);
422 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
423 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
424 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
425 * the packet is too big or too small to transmit on the device.
427 * The caller retains ownership of 'buffer' in all cases.
429 * The kernel maintains a packet transmission queue, so the caller is not
430 * expected to do additional queuing of packets. */
432 netdev_linux_send(struct netdev *netdev_, const void *data, size_t size)
434 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
436 /* XXX should support sending even if 'ethertype' was NETDEV_ETH_TYPE_NONE.
438 if (netdev->tap_fd < 0) {
443 ssize_t retval = write(netdev->tap_fd, data, size);
445 /* The Linux AF_PACKET implementation never blocks waiting for room
446 * for packets, instead returning ENOBUFS. Translate this into
447 * EAGAIN for the caller. */
448 if (errno == ENOBUFS) {
450 } else if (errno == EINTR) {
452 } else if (errno != EAGAIN) {
453 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
454 netdev_get_name(netdev_), strerror(errno));
457 } else if (retval != size) {
458 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
459 "%zu) on %s", retval, size, netdev_get_name(netdev_));
467 /* Registers with the poll loop to wake up from the next call to poll_block()
468 * when the packet transmission queue has sufficient room to transmit a packet
469 * with netdev_send().
471 * The kernel maintains a packet transmission queue, so the client is not
472 * expected to do additional queuing of packets. Thus, this function is
473 * unlikely to ever be used. It is included for completeness. */
475 netdev_linux_send_wait(struct netdev *netdev_)
477 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
478 if (netdev->tap_fd < 0 && netdev->netdev_fd < 0) {
480 } else if (netdev->tap_fd == netdev->netdev_fd) {
481 poll_fd_wait(netdev->tap_fd, POLLOUT);
483 /* TAP device always accepts packets.*/
484 poll_immediate_wake();
488 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
489 * otherwise a positive errno value. */
491 netdev_linux_set_etheraddr(struct netdev *netdev_,
492 const uint8_t mac[ETH_ADDR_LEN])
494 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
497 if (!(netdev->cache->valid & VALID_ETHERADDR)
498 || !eth_addr_equals(netdev->cache->etheraddr, mac)) {
499 error = set_etheraddr(netdev_get_name(netdev_), ARPHRD_ETHER, mac);
501 netdev->cache->valid |= VALID_ETHERADDR;
502 memcpy(netdev->cache->etheraddr, mac, ETH_ADDR_LEN);
510 /* Returns a pointer to 'netdev''s MAC address. The caller must not modify or
511 * free the returned buffer. */
513 netdev_linux_get_etheraddr(const struct netdev *netdev_,
514 uint8_t mac[ETH_ADDR_LEN])
516 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
517 if (!(netdev->cache->valid & VALID_ETHERADDR)) {
518 int error = get_etheraddr(netdev_get_name(netdev_),
519 netdev->cache->etheraddr);
523 netdev->cache->valid |= VALID_ETHERADDR;
525 memcpy(mac, netdev->cache->etheraddr, ETH_ADDR_LEN);
529 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
530 * in bytes, not including the hardware header; thus, this is typically 1500
531 * bytes for Ethernet devices. */
533 netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup)
535 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
536 if (!(netdev->cache->valid & VALID_MTU)) {
540 error = netdev_linux_do_ioctl(netdev_, &ifr, SIOCGIFMTU, "SIOCGIFMTU");
544 netdev->cache->mtu = ifr.ifr_mtu;
545 netdev->cache->valid |= VALID_MTU;
547 *mtup = netdev->cache->mtu;
552 netdev_linux_get_carrier(const struct netdev *netdev_, bool *carrier)
554 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
559 if (!(netdev->cache->valid & VALID_CARRIER)) {
563 fn = xasprintf("/sys/class/net/%s/carrier", netdev_get_name(netdev_));
564 fd = open(fn, O_RDONLY);
567 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(error));
571 retval = read(fd, line, sizeof line);
574 if (error == EINVAL) {
575 /* This is the normal return value when we try to check carrier
576 * if the network device is not up. */
578 VLOG_WARN_RL(&rl, "%s: read failed: %s", fn, strerror(error));
581 } else if (retval == 0) {
583 VLOG_WARN_RL(&rl, "%s: unexpected end of file", fn);
587 if (line[0] != '0' && line[0] != '1') {
589 VLOG_WARN_RL(&rl, "%s: value is %c (expected 0 or 1)",
593 netdev->cache->carrier = line[0] != '0';
594 netdev->cache->valid |= VALID_CARRIER;
596 *carrier = netdev->cache->carrier;
607 /* Check whether we can we use RTM_GETLINK to get network device statistics.
608 * In pre-2.6.19 kernels, this was only available if wireless extensions were
611 check_for_working_netlink_stats(void)
613 /* Decide on the netdev_get_stats() implementation to use. Netlink is
614 * preferable, so if that works, we'll use it. */
615 int ifindex = do_get_ifindex("lo");
617 VLOG_WARN("failed to get ifindex for lo, "
618 "obtaining netdev stats from proc");
621 struct netdev_stats stats;
622 int error = get_stats_via_netlink(ifindex, &stats);
624 VLOG_DBG("obtaining netdev stats via rtnetlink");
627 VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
628 "via proc (you are probably running a pre-2.6.19 "
629 "kernel)", strerror(error));
635 /* Retrieves current device stats for 'netdev'.
637 * XXX All of the members of struct netdev_stats are 64 bits wide, but on
638 * 32-bit architectures the Linux network stats are only 32 bits. */
640 netdev_linux_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
642 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
643 static int use_netlink_stats = -1;
645 struct netdev_stats raw_stats;
646 struct netdev_stats *collect_stats = stats;
648 COVERAGE_INC(netdev_get_stats);
650 if (!(netdev->cache->valid & VALID_IS_INTERNAL)) {
651 netdev->cache->is_internal = (netdev->tap_fd != -1);
653 if (!netdev->cache->is_internal) {
654 struct ethtool_drvinfo drvinfo;
656 memset(&drvinfo, 0, sizeof drvinfo);
657 error = netdev_linux_do_ethtool(&netdev->netdev,
658 (struct ethtool_cmd *)&drvinfo,
663 netdev->cache->is_internal = !strcmp(drvinfo.driver,
668 netdev->cache->valid |= VALID_IS_INTERNAL;
671 if (netdev->cache->is_internal) {
672 collect_stats = &raw_stats;
675 if (use_netlink_stats < 0) {
676 use_netlink_stats = check_for_working_netlink_stats();
678 if (use_netlink_stats) {
681 error = get_ifindex(&netdev->netdev, &ifindex);
683 error = get_stats_via_netlink(ifindex, collect_stats);
686 error = get_stats_via_proc(netdev->netdev.name, collect_stats);
689 /* If this port is an internal port then the transmit and receive stats
690 * will appear to be swapped relative to the other ports since we are the
691 * one sending the data, not a remote computer. For consistency, we swap
693 if (netdev->cache->is_internal) {
694 stats->rx_packets = raw_stats.tx_packets;
695 stats->tx_packets = raw_stats.rx_packets;
696 stats->rx_bytes = raw_stats.tx_bytes;
697 stats->tx_bytes = raw_stats.rx_bytes;
698 stats->rx_errors = raw_stats.tx_errors;
699 stats->tx_errors = raw_stats.rx_errors;
700 stats->rx_dropped = raw_stats.tx_dropped;
701 stats->tx_dropped = raw_stats.rx_dropped;
702 stats->multicast = raw_stats.multicast;
703 stats->collisions = raw_stats.collisions;
704 stats->rx_length_errors = 0;
705 stats->rx_over_errors = 0;
706 stats->rx_crc_errors = 0;
707 stats->rx_frame_errors = 0;
708 stats->rx_fifo_errors = 0;
709 stats->rx_missed_errors = 0;
710 stats->tx_aborted_errors = 0;
711 stats->tx_carrier_errors = 0;
712 stats->tx_fifo_errors = 0;
713 stats->tx_heartbeat_errors = 0;
714 stats->tx_window_errors = 0;
720 /* Stores the features supported by 'netdev' into each of '*current',
721 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
722 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
723 * successful, otherwise a positive errno value. On failure, all of the
724 * passed-in values are set to 0. */
726 netdev_linux_get_features(struct netdev *netdev,
727 uint32_t *current, uint32_t *advertised,
728 uint32_t *supported, uint32_t *peer)
730 struct ethtool_cmd ecmd;
733 memset(&ecmd, 0, sizeof ecmd);
734 error = netdev_linux_do_ethtool(netdev, &ecmd,
735 ETHTOOL_GSET, "ETHTOOL_GSET");
740 /* Supported features. */
742 if (ecmd.supported & SUPPORTED_10baseT_Half) {
743 *supported |= OFPPF_10MB_HD;
745 if (ecmd.supported & SUPPORTED_10baseT_Full) {
746 *supported |= OFPPF_10MB_FD;
748 if (ecmd.supported & SUPPORTED_100baseT_Half) {
749 *supported |= OFPPF_100MB_HD;
751 if (ecmd.supported & SUPPORTED_100baseT_Full) {
752 *supported |= OFPPF_100MB_FD;
754 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
755 *supported |= OFPPF_1GB_HD;
757 if (ecmd.supported & SUPPORTED_1000baseT_Full) {
758 *supported |= OFPPF_1GB_FD;
760 if (ecmd.supported & SUPPORTED_10000baseT_Full) {
761 *supported |= OFPPF_10GB_FD;
763 if (ecmd.supported & SUPPORTED_TP) {
764 *supported |= OFPPF_COPPER;
766 if (ecmd.supported & SUPPORTED_FIBRE) {
767 *supported |= OFPPF_FIBER;
769 if (ecmd.supported & SUPPORTED_Autoneg) {
770 *supported |= OFPPF_AUTONEG;
772 if (ecmd.supported & SUPPORTED_Pause) {
773 *supported |= OFPPF_PAUSE;
775 if (ecmd.supported & SUPPORTED_Asym_Pause) {
776 *supported |= OFPPF_PAUSE_ASYM;
779 /* Advertised features. */
781 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
782 *advertised |= OFPPF_10MB_HD;
784 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
785 *advertised |= OFPPF_10MB_FD;
787 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
788 *advertised |= OFPPF_100MB_HD;
790 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
791 *advertised |= OFPPF_100MB_FD;
793 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
794 *advertised |= OFPPF_1GB_HD;
796 if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
797 *advertised |= OFPPF_1GB_FD;
799 if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
800 *advertised |= OFPPF_10GB_FD;
802 if (ecmd.advertising & ADVERTISED_TP) {
803 *advertised |= OFPPF_COPPER;
805 if (ecmd.advertising & ADVERTISED_FIBRE) {
806 *advertised |= OFPPF_FIBER;
808 if (ecmd.advertising & ADVERTISED_Autoneg) {
809 *advertised |= OFPPF_AUTONEG;
811 if (ecmd.advertising & ADVERTISED_Pause) {
812 *advertised |= OFPPF_PAUSE;
814 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
815 *advertised |= OFPPF_PAUSE_ASYM;
818 /* Current settings. */
819 if (ecmd.speed == SPEED_10) {
820 *current = ecmd.duplex ? OFPPF_10MB_FD : OFPPF_10MB_HD;
821 } else if (ecmd.speed == SPEED_100) {
822 *current = ecmd.duplex ? OFPPF_100MB_FD : OFPPF_100MB_HD;
823 } else if (ecmd.speed == SPEED_1000) {
824 *current = ecmd.duplex ? OFPPF_1GB_FD : OFPPF_1GB_HD;
825 } else if (ecmd.speed == SPEED_10000) {
826 *current = OFPPF_10GB_FD;
831 if (ecmd.port == PORT_TP) {
832 *current |= OFPPF_COPPER;
833 } else if (ecmd.port == PORT_FIBRE) {
834 *current |= OFPPF_FIBER;
838 *current |= OFPPF_AUTONEG;
841 /* Peer advertisements. */
847 /* Set the features advertised by 'netdev' to 'advertise'. */
849 netdev_linux_set_advertisements(struct netdev *netdev, uint32_t advertise)
851 struct ethtool_cmd ecmd;
854 memset(&ecmd, 0, sizeof ecmd);
855 error = netdev_linux_do_ethtool(netdev, &ecmd,
856 ETHTOOL_GSET, "ETHTOOL_GSET");
861 ecmd.advertising = 0;
862 if (advertise & OFPPF_10MB_HD) {
863 ecmd.advertising |= ADVERTISED_10baseT_Half;
865 if (advertise & OFPPF_10MB_FD) {
866 ecmd.advertising |= ADVERTISED_10baseT_Full;
868 if (advertise & OFPPF_100MB_HD) {
869 ecmd.advertising |= ADVERTISED_100baseT_Half;
871 if (advertise & OFPPF_100MB_FD) {
872 ecmd.advertising |= ADVERTISED_100baseT_Full;
874 if (advertise & OFPPF_1GB_HD) {
875 ecmd.advertising |= ADVERTISED_1000baseT_Half;
877 if (advertise & OFPPF_1GB_FD) {
878 ecmd.advertising |= ADVERTISED_1000baseT_Full;
880 if (advertise & OFPPF_10GB_FD) {
881 ecmd.advertising |= ADVERTISED_10000baseT_Full;
883 if (advertise & OFPPF_COPPER) {
884 ecmd.advertising |= ADVERTISED_TP;
886 if (advertise & OFPPF_FIBER) {
887 ecmd.advertising |= ADVERTISED_FIBRE;
889 if (advertise & OFPPF_AUTONEG) {
890 ecmd.advertising |= ADVERTISED_Autoneg;
892 if (advertise & OFPPF_PAUSE) {
893 ecmd.advertising |= ADVERTISED_Pause;
895 if (advertise & OFPPF_PAUSE_ASYM) {
896 ecmd.advertising |= ADVERTISED_Asym_Pause;
898 return netdev_linux_do_ethtool(netdev, &ecmd,
899 ETHTOOL_SSET, "ETHTOOL_SSET");
902 /* If 'netdev_name' is the name of a VLAN network device (e.g. one created with
903 * vconfig(8)), sets '*vlan_vid' to the VLAN VID associated with that device
904 * and returns 0. Otherwise returns a errno value (specifically ENOENT if
905 * 'netdev_name' is the name of a network device that is not a VLAN device) and
906 * sets '*vlan_vid' to -1. */
908 netdev_linux_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
910 const char *netdev_name = netdev_get_name(netdev);
911 struct ds line = DS_EMPTY_INITIALIZER;
916 COVERAGE_INC(netdev_get_vlan_vid);
917 fn = xasprintf("/proc/net/vlan/%s", netdev_name);
918 stream = fopen(fn, "r");
924 if (ds_get_line(&line, stream)) {
925 if (ferror(stream)) {
927 VLOG_ERR_RL(&rl, "error reading \"%s\": %s", fn, strerror(errno));
930 VLOG_ERR_RL(&rl, "unexpected end of file reading \"%s\"", fn);
935 if (!sscanf(ds_cstr(&line), "%*s VID: %d", vlan_vid)) {
937 VLOG_ERR_RL(&rl, "parse error reading \"%s\" line 1: \"%s\"",
956 #define POLICE_ADD_CMD "/sbin/tc qdisc add dev %s handle ffff: ingress"
957 #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"
958 /* We redirect stderr to /dev/null because we often want to remove all
959 * traffic control configuration on a port so its in a known state. If
960 * this done when there is no such configuration, tc complains, so we just
963 #define POLICE_DEL_CMD "/sbin/tc qdisc del dev %s handle ffff: ingress 2>/dev/null"
965 /* Attempts to set input rate limiting (policing) policy. */
967 netdev_linux_set_policing(struct netdev *netdev,
968 uint32_t kbits_rate, uint32_t kbits_burst)
970 const char *netdev_name = netdev_get_name(netdev);
973 COVERAGE_INC(netdev_set_policing);
976 /* Default to 10 kilobits if not specified. */
980 /* xxx This should be more careful about only adding if it
981 * xxx actually exists, as opposed to always deleting it. */
982 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
983 if (system(command) == -1) {
984 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
987 snprintf(command, sizeof(command), POLICE_ADD_CMD, netdev_name);
988 if (system(command) != 0) {
989 VLOG_WARN_RL(&rl, "%s: problem adding policing", netdev_name);
993 snprintf(command, sizeof(command), POLICE_CONFIG_CMD, netdev_name,
994 kbits_rate, kbits_burst);
995 if (system(command) != 0) {
996 VLOG_WARN_RL(&rl, "%s: problem configuring policing",
1001 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
1002 if (system(command) == -1) {
1003 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
1011 netdev_linux_get_in4(const struct netdev *netdev_,
1012 struct in_addr *address, struct in_addr *netmask)
1014 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1015 if (!(netdev->cache->valid & VALID_IN4)) {
1018 error = netdev_linux_get_ipv4(netdev_, &netdev->cache->address,
1019 SIOCGIFADDR, "SIOCGIFADDR");
1024 error = netdev_linux_get_ipv4(netdev_, &netdev->cache->netmask,
1025 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1030 netdev->cache->valid |= VALID_IN4;
1032 *address = netdev->cache->address;
1033 *netmask = netdev->cache->netmask;
1034 return address->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1038 netdev_linux_set_in4(struct netdev *netdev_, struct in_addr address,
1039 struct in_addr netmask)
1041 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1044 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", address);
1046 netdev->cache->valid |= VALID_IN4;
1047 netdev->cache->address = address;
1048 netdev->cache->netmask = netmask;
1049 if (address.s_addr != INADDR_ANY) {
1050 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1051 "SIOCSIFNETMASK", netmask);
1058 parse_if_inet6_line(const char *line,
1059 struct in6_addr *in6, char ifname[16 + 1])
1061 uint8_t *s6 = in6->s6_addr;
1062 #define X8 "%2"SCNx8
1064 " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
1065 "%*x %*x %*x %*x %16s\n",
1066 &s6[0], &s6[1], &s6[2], &s6[3],
1067 &s6[4], &s6[5], &s6[6], &s6[7],
1068 &s6[8], &s6[9], &s6[10], &s6[11],
1069 &s6[12], &s6[13], &s6[14], &s6[15],
1073 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
1074 * 'in6' is non-null) and returns true. Otherwise, returns false. */
1076 netdev_linux_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1078 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1079 if (!(netdev->cache->valid & VALID_IN6)) {
1083 netdev->cache->in6 = in6addr_any;
1085 file = fopen("/proc/net/if_inet6", "r");
1087 const char *name = netdev_get_name(netdev_);
1088 while (fgets(line, sizeof line, file)) {
1089 struct in6_addr in6;
1090 char ifname[16 + 1];
1091 if (parse_if_inet6_line(line, &in6, ifname)
1092 && !strcmp(name, ifname))
1094 netdev->cache->in6 = in6;
1100 netdev->cache->valid |= VALID_IN6;
1102 *in6 = netdev->cache->in6;
1107 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1109 struct sockaddr_in sin;
1110 memset(&sin, 0, sizeof sin);
1111 sin.sin_family = AF_INET;
1112 sin.sin_addr = addr;
1115 memset(sa, 0, sizeof *sa);
1116 memcpy(sa, &sin, sizeof sin);
1120 do_set_addr(struct netdev *netdev,
1121 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1124 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1125 make_in4_sockaddr(&ifr.ifr_addr, addr);
1126 return netdev_linux_do_ioctl(netdev, &ifr, ioctl_nr, ioctl_name);
1129 /* Adds 'router' as a default IP gateway. */
1131 netdev_linux_add_router(struct netdev *netdev UNUSED, struct in_addr router)
1133 struct in_addr any = { INADDR_ANY };
1137 memset(&rt, 0, sizeof rt);
1138 make_in4_sockaddr(&rt.rt_dst, any);
1139 make_in4_sockaddr(&rt.rt_gateway, router);
1140 make_in4_sockaddr(&rt.rt_genmask, any);
1141 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1142 COVERAGE_INC(netdev_add_router);
1143 error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
1145 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
1151 netdev_linux_get_next_hop(const struct in_addr *host, struct in_addr *next_hop,
1154 static const char fn[] = "/proc/net/route";
1159 *netdev_name = NULL;
1160 stream = fopen(fn, "r");
1161 if (stream == NULL) {
1162 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1167 while (fgets(line, sizeof line, stream)) {
1170 uint32_t dest, gateway, mask;
1171 int refcnt, metric, mtu;
1172 unsigned int flags, use, window, irtt;
1175 "%16s %"SCNx32" %"SCNx32" %04X %d %u %d %"SCNx32
1177 iface, &dest, &gateway, &flags, &refcnt,
1178 &use, &metric, &mask, &mtu, &window, &irtt) != 11) {
1180 VLOG_WARN_RL(&rl, "%s: could not parse line %d: %s",
1184 if (!(flags & RTF_UP)) {
1185 /* Skip routes that aren't up. */
1189 /* The output of 'dest', 'mask', and 'gateway' were given in
1190 * network byte order, so we don't need need any endian
1191 * conversions here. */
1192 if ((dest & mask) == (host->s_addr & mask)) {
1194 /* The host is directly reachable. */
1195 next_hop->s_addr = 0;
1197 /* To reach the host, we must go through a gateway. */
1198 next_hop->s_addr = gateway;
1200 *netdev_name = xstrdup(iface);
1211 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1212 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1213 * returns 0. Otherwise, it returns a positive errno value; in particular,
1214 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
1216 netdev_linux_arp_lookup(const struct netdev *netdev,
1217 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
1220 struct sockaddr_in *pa;
1223 memset(&r, 0, sizeof r);
1224 pa = (struct sockaddr_in *) &r.arp_pa;
1225 pa->sin_family = AF_INET;
1226 pa->sin_addr.s_addr = ip;
1228 r.arp_ha.sa_family = ARPHRD_ETHER;
1230 strncpy(r.arp_dev, netdev->name, sizeof r.arp_dev);
1231 COVERAGE_INC(netdev_arp_lookup);
1232 retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
1234 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
1235 } else if (retval != ENXIO) {
1236 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
1237 netdev->name, IP_ARGS(&ip), strerror(retval));
1243 nd_to_iff_flags(enum netdev_flags nd)
1246 if (nd & NETDEV_UP) {
1249 if (nd & NETDEV_PROMISC) {
1256 iff_to_nd_flags(int iff)
1258 enum netdev_flags nd = 0;
1262 if (iff & IFF_PROMISC) {
1263 nd |= NETDEV_PROMISC;
1269 netdev_linux_update_flags(struct netdev *netdev, enum netdev_flags off,
1270 enum netdev_flags on, enum netdev_flags *old_flagsp)
1272 int old_flags, new_flags;
1275 error = get_flags(netdev, &old_flags);
1277 *old_flagsp = iff_to_nd_flags(old_flags);
1278 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1279 if (new_flags != old_flags) {
1280 error = set_flags(netdev, new_flags);
1287 poll_notify(struct list *list)
1289 struct netdev_linux_notifier *notifier;
1290 LIST_FOR_EACH (notifier, struct netdev_linux_notifier, node, list) {
1291 struct netdev_notifier *n = ¬ifier->notifier;
1297 netdev_linux_poll_cb(const struct rtnetlink_change *change,
1301 struct list *list = shash_find_data(&netdev_linux_notifiers,
1307 struct shash_node *node;
1308 SHASH_FOR_EACH (node, &netdev_linux_notifiers) {
1309 poll_notify(node->data);
1315 netdev_linux_poll_add(struct netdev *netdev,
1316 void (*cb)(struct netdev_notifier *), void *aux,
1317 struct netdev_notifier **notifierp)
1319 const char *netdev_name = netdev_get_name(netdev);
1320 struct netdev_linux_notifier *notifier;
1323 if (shash_is_empty(&netdev_linux_notifiers)) {
1324 int error = rtnetlink_notifier_register(&netdev_linux_poll_notifier,
1325 netdev_linux_poll_cb, NULL);
1331 list = shash_find_data(&netdev_linux_notifiers, netdev_name);
1333 list = xmalloc(sizeof *list);
1335 shash_add(&netdev_linux_notifiers, netdev_name, list);
1338 notifier = xmalloc(sizeof *notifier);
1339 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
1340 list_push_back(list, ¬ifier->node);
1341 *notifierp = ¬ifier->notifier;
1346 netdev_linux_poll_remove(struct netdev_notifier *notifier_)
1348 struct netdev_linux_notifier *notifier =
1349 CONTAINER_OF(notifier_, struct netdev_linux_notifier, notifier);
1352 /* Remove 'notifier' from its list. */
1353 list = list_remove(¬ifier->node);
1354 if (list_is_empty(list)) {
1355 /* The list is now empty. Remove it from the hash and free it. */
1356 const char *netdev_name = netdev_get_name(notifier->notifier.netdev);
1357 shash_delete(&netdev_linux_notifiers,
1358 shash_find(&netdev_linux_notifiers, netdev_name));
1363 /* If that was the last notifier, unregister. */
1364 if (shash_is_empty(&netdev_linux_notifiers)) {
1365 rtnetlink_notifier_unregister(&netdev_linux_poll_notifier);
1369 const struct netdev_class netdev_linux_class = {
1380 netdev_linux_enumerate,
1383 netdev_linux_recv_wait,
1387 netdev_linux_send_wait,
1389 netdev_linux_set_etheraddr,
1390 netdev_linux_get_etheraddr,
1391 netdev_linux_get_mtu,
1392 netdev_linux_get_carrier,
1393 netdev_linux_get_stats,
1395 netdev_linux_get_features,
1396 netdev_linux_set_advertisements,
1397 netdev_linux_get_vlan_vid,
1398 netdev_linux_set_policing,
1400 netdev_linux_get_in4,
1401 netdev_linux_set_in4,
1402 netdev_linux_get_in6,
1403 netdev_linux_add_router,
1404 netdev_linux_get_next_hop,
1405 netdev_linux_arp_lookup,
1407 netdev_linux_update_flags,
1409 netdev_linux_poll_add,
1410 netdev_linux_poll_remove,
1413 const struct netdev_class netdev_tap_class = {
1424 netdev_linux_enumerate,
1427 netdev_linux_recv_wait,
1431 netdev_linux_send_wait,
1433 netdev_linux_set_etheraddr,
1434 netdev_linux_get_etheraddr,
1435 netdev_linux_get_mtu,
1436 netdev_linux_get_carrier,
1437 netdev_linux_get_stats,
1439 netdev_linux_get_features,
1440 netdev_linux_set_advertisements,
1441 netdev_linux_get_vlan_vid,
1442 netdev_linux_set_policing,
1444 netdev_linux_get_in4,
1445 netdev_linux_set_in4,
1446 netdev_linux_get_in6,
1447 netdev_linux_add_router,
1448 netdev_linux_get_next_hop,
1449 netdev_linux_arp_lookup,
1451 netdev_linux_update_flags,
1453 netdev_linux_poll_add,
1454 netdev_linux_poll_remove,
1458 get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
1460 /* Policy for RTNLGRP_LINK messages.
1462 * There are *many* more fields in these messages, but currently we only
1463 * care about these fields. */
1464 static const struct nl_policy rtnlgrp_link_policy[] = {
1465 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
1466 [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
1467 .min_len = sizeof(struct rtnl_link_stats) },
1471 static struct nl_sock *rtnl_sock;
1472 struct ofpbuf request;
1473 struct ofpbuf *reply;
1474 struct ifinfomsg *ifi;
1475 const struct rtnl_link_stats *rtnl_stats;
1476 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1480 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0, &rtnl_sock);
1482 VLOG_ERR_RL(&rl, "failed to create rtnetlink socket: %s",
1488 ofpbuf_init(&request, 0);
1489 nl_msg_put_nlmsghdr(&request, rtnl_sock, sizeof *ifi,
1490 RTM_GETLINK, NLM_F_REQUEST);
1491 ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
1492 ifi->ifi_family = PF_UNSPEC;
1493 ifi->ifi_index = ifindex;
1494 error = nl_sock_transact(rtnl_sock, &request, &reply);
1495 ofpbuf_uninit(&request);
1500 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1501 rtnlgrp_link_policy,
1502 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1503 ofpbuf_delete(reply);
1507 if (!attrs[IFLA_STATS]) {
1508 VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
1509 ofpbuf_delete(reply);
1513 rtnl_stats = nl_attr_get(attrs[IFLA_STATS]);
1514 stats->rx_packets = rtnl_stats->rx_packets;
1515 stats->tx_packets = rtnl_stats->tx_packets;
1516 stats->rx_bytes = rtnl_stats->rx_bytes;
1517 stats->tx_bytes = rtnl_stats->tx_bytes;
1518 stats->rx_errors = rtnl_stats->rx_errors;
1519 stats->tx_errors = rtnl_stats->tx_errors;
1520 stats->rx_dropped = rtnl_stats->rx_dropped;
1521 stats->tx_dropped = rtnl_stats->tx_dropped;
1522 stats->multicast = rtnl_stats->multicast;
1523 stats->collisions = rtnl_stats->collisions;
1524 stats->rx_length_errors = rtnl_stats->rx_length_errors;
1525 stats->rx_over_errors = rtnl_stats->rx_over_errors;
1526 stats->rx_crc_errors = rtnl_stats->rx_crc_errors;
1527 stats->rx_frame_errors = rtnl_stats->rx_frame_errors;
1528 stats->rx_fifo_errors = rtnl_stats->rx_fifo_errors;
1529 stats->rx_missed_errors = rtnl_stats->rx_missed_errors;
1530 stats->tx_aborted_errors = rtnl_stats->tx_aborted_errors;
1531 stats->tx_carrier_errors = rtnl_stats->tx_carrier_errors;
1532 stats->tx_fifo_errors = rtnl_stats->tx_fifo_errors;
1533 stats->tx_heartbeat_errors = rtnl_stats->tx_heartbeat_errors;
1534 stats->tx_window_errors = rtnl_stats->tx_window_errors;
1536 ofpbuf_delete(reply);
1542 get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats)
1544 static const char fn[] = "/proc/net/dev";
1549 stream = fopen(fn, "r");
1551 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1556 while (fgets(line, sizeof line, stream)) {
1559 #define X64 "%"SCNu64
1562 X64 X64 X64 X64 X64 X64 X64 "%*u"
1563 X64 X64 X64 X64 X64 X64 X64 "%*u",
1569 &stats->rx_fifo_errors,
1570 &stats->rx_frame_errors,
1576 &stats->tx_fifo_errors,
1578 &stats->tx_carrier_errors) != 15) {
1579 VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln);
1580 } else if (!strcmp(devname, netdev_name)) {
1581 stats->rx_length_errors = UINT64_MAX;
1582 stats->rx_over_errors = UINT64_MAX;
1583 stats->rx_crc_errors = UINT64_MAX;
1584 stats->rx_missed_errors = UINT64_MAX;
1585 stats->tx_aborted_errors = UINT64_MAX;
1586 stats->tx_heartbeat_errors = UINT64_MAX;
1587 stats->tx_window_errors = UINT64_MAX;
1593 VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name);
1599 get_flags(const struct netdev *netdev, int *flags)
1604 error = netdev_linux_do_ioctl(netdev, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
1605 *flags = ifr.ifr_flags;
1610 set_flags(struct netdev *netdev, int flags)
1614 ifr.ifr_flags = flags;
1615 return netdev_linux_do_ioctl(netdev, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1619 do_get_ifindex(const char *netdev_name)
1623 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1624 COVERAGE_INC(netdev_get_ifindex);
1625 if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) {
1626 VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
1627 netdev_name, strerror(errno));
1630 return ifr.ifr_ifindex;
1634 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1636 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1638 if (!(netdev->cache->valid & VALID_IFINDEX)) {
1639 int ifindex = do_get_ifindex(netdev_get_name(netdev_));
1643 netdev->cache->valid |= VALID_IFINDEX;
1644 netdev->cache->ifindex = ifindex;
1646 *ifindexp = netdev->cache->ifindex;
1651 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1656 memset(&ifr, 0, sizeof ifr);
1657 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1658 COVERAGE_INC(netdev_get_hwaddr);
1659 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
1660 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
1661 netdev_name, strerror(errno));
1664 hwaddr_family = ifr.ifr_hwaddr.sa_family;
1665 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
1666 VLOG_WARN("%s device has unknown hardware address family %d",
1667 netdev_name, hwaddr_family);
1669 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
1674 set_etheraddr(const char *netdev_name, int hwaddr_family,
1675 const uint8_t mac[ETH_ADDR_LEN])
1679 memset(&ifr, 0, sizeof ifr);
1680 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1681 ifr.ifr_hwaddr.sa_family = hwaddr_family;
1682 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
1683 COVERAGE_INC(netdev_set_hwaddr);
1684 if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) {
1685 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
1686 netdev_name, strerror(errno));
1693 netdev_linux_do_ethtool(struct netdev *netdev, struct ethtool_cmd *ecmd,
1694 int cmd, const char *cmd_name)
1698 memset(&ifr, 0, sizeof ifr);
1699 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1700 ifr.ifr_data = (caddr_t) ecmd;
1703 COVERAGE_INC(netdev_ethtool);
1704 if (ioctl(af_inet_sock, SIOCETHTOOL, &ifr) == 0) {
1707 if (errno != EOPNOTSUPP) {
1708 VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
1709 "failed: %s", cmd_name, netdev->name,
1712 /* The device doesn't support this operation. That's pretty
1713 * common, so there's no point in logging anything. */
1720 netdev_linux_do_ioctl(const struct netdev *netdev, struct ifreq *ifr,
1721 int cmd, const char *cmd_name)
1723 strncpy(ifr->ifr_name, netdev_get_name(netdev), sizeof ifr->ifr_name);
1724 if (ioctl(af_inet_sock, cmd, ifr) == -1) {
1725 VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s",
1726 netdev_get_name(netdev), cmd_name, strerror(errno));
1733 netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip,
1734 int cmd, const char *cmd_name)
1739 ifr.ifr_addr.sa_family = AF_INET;
1740 error = netdev_linux_do_ioctl(netdev, &ifr, cmd, cmd_name);
1742 const struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
1743 *ip = sin->sin_addr;