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
91 /* Cached network device information. */
92 struct netdev_linux_cache {
93 struct shash_node *shash_node;
98 uint8_t etheraddr[ETH_ADDR_LEN];
105 static struct shash cache_map = SHASH_INITIALIZER(&cache_map);
106 static struct rtnetlink_notifier netdev_linux_cache_notifier;
108 /* An AF_INET socket (used for ioctl operations). */
109 static int af_inet_sock = -1;
111 struct netdev_linux_notifier {
112 struct netdev_notifier notifier;
116 static struct shash netdev_linux_notifiers =
117 SHASH_INITIALIZER(&netdev_linux_notifiers);
118 static struct rtnetlink_notifier netdev_linux_poll_notifier;
120 /* This is set pretty low because we probably won't learn anything from the
121 * additional log messages. */
122 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
124 static int netdev_linux_do_ethtool(struct netdev *, struct ethtool_cmd *,
125 int cmd, const char *cmd_name);
126 static int netdev_linux_do_ioctl(const struct netdev *, struct ifreq *,
127 int cmd, const char *cmd_name);
128 static int get_flags(const struct netdev *, int *flagsp);
129 static int set_flags(struct netdev *, int flags);
130 static int do_get_ifindex(const char *netdev_name);
131 static int get_ifindex(const struct netdev *, int *ifindexp);
132 static int do_set_addr(struct netdev *netdev,
133 int ioctl_nr, const char *ioctl_name,
134 struct in_addr addr);
135 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
136 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
137 const uint8_t[ETH_ADDR_LEN]);
138 static int get_stats_via_netlink(int ifindex, struct netdev_stats *stats);
139 static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats);
141 static struct netdev_linux *
142 netdev_linux_cast(const struct netdev *netdev)
144 netdev_assert_class(netdev, &netdev_linux_class);
145 return CONTAINER_OF(netdev, struct netdev_linux, netdev);
149 netdev_linux_init(void)
151 static int status = -1;
153 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
154 status = af_inet_sock >= 0 ? 0 : errno;
156 VLOG_ERR("failed to create inet socket: %s", strerror(status));
163 netdev_linux_run(void)
165 rtnetlink_notifier_run();
169 netdev_linux_wait(void)
171 rtnetlink_notifier_wait();
175 netdev_linux_cache_cb(const struct rtnetlink_change *change,
178 struct netdev_linux_cache *cache;
180 cache = shash_find_data(&cache_map, change->ifname);
185 struct shash_node *node;
186 SHASH_FOR_EACH (node, &cache_map) {
194 netdev_linux_open(const char *name, char *suffix, int ethertype,
195 struct netdev **netdevp)
197 struct netdev_linux *netdev;
198 enum netdev_flags flags;
201 /* Allocate network device. */
202 netdev = xcalloc(1, sizeof *netdev);
203 netdev_init(&netdev->netdev, suffix, &netdev_linux_class);
204 netdev->netdev_fd = -1;
206 netdev->cache = shash_find_data(&cache_map, suffix);
207 if (!netdev->cache) {
208 if (shash_is_empty(&cache_map)) {
209 int error = rtnetlink_notifier_register(
210 &netdev_linux_cache_notifier, netdev_linux_cache_cb, NULL);
212 netdev_close(&netdev->netdev);
216 netdev->cache = xmalloc(sizeof *netdev->cache);
217 netdev->cache->shash_node = shash_add(&cache_map, suffix,
219 netdev->cache->valid = 0;
220 netdev->cache->ref_cnt = 0;
222 netdev->cache->ref_cnt++;
224 if (!strncmp(name, "tap:", 4)) {
225 static const char tap_dev[] = "/dev/net/tun";
228 /* Open tap device. */
229 netdev->tap_fd = open(tap_dev, O_RDWR);
230 if (netdev->tap_fd < 0) {
232 VLOG_WARN("opening \"%s\" failed: %s", tap_dev, strerror(error));
236 /* Create tap device. */
237 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
238 error = netdev_linux_do_ioctl(&netdev->netdev, &ifr,
239 TUNSETIFF, "TUNSETIFF");
244 /* Make non-blocking. */
245 error = set_nonblocking(netdev->tap_fd);
251 error = netdev_get_flags(&netdev->netdev, &flags);
252 if (error == ENODEV) {
256 if (netdev->tap_fd >= 0 || ethertype != NETDEV_ETH_TYPE_NONE) {
257 struct sockaddr_ll sll;
261 /* Create file descriptor. */
262 protocol = (ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
263 : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
265 netdev->netdev_fd = socket(PF_PACKET, SOCK_RAW, htons(protocol));
266 if (netdev->netdev_fd < 0) {
270 if (netdev->tap_fd < 0) {
271 netdev->tap_fd = netdev->netdev_fd;
274 /* Set non-blocking mode. */
275 error = set_nonblocking(netdev->netdev_fd);
280 /* Get ethernet device index. */
281 error = get_ifindex(&netdev->netdev, &ifindex);
286 /* Bind to specific ethernet device. */
287 memset(&sll, 0, sizeof sll);
288 sll.sll_family = AF_PACKET;
289 sll.sll_ifindex = ifindex;
290 if (bind(netdev->netdev_fd,
291 (struct sockaddr *) &sll, sizeof sll) < 0) {
293 VLOG_ERR("bind to %s failed: %s", suffix, strerror(error));
297 /* Between the socket() and bind() calls above, the socket receives all
298 * packets of the requested type on all system interfaces. We do not
299 * want to receive that data, but there is no way to avoid it. So we
300 * must now drain out the receive queue. */
301 error = drain_rcvbuf(netdev->netdev_fd);
307 *netdevp = &netdev->netdev;
311 netdev_close(&netdev->netdev);
315 /* Closes and destroys 'netdev'. */
317 netdev_linux_close(struct netdev *netdev_)
319 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
321 if (netdev->cache && !--netdev->cache->ref_cnt) {
322 shash_delete(&cache_map, netdev->cache->shash_node);
325 if (shash_is_empty(&cache_map)) {
326 rtnetlink_notifier_unregister(&netdev_linux_cache_notifier);
329 if (netdev->netdev_fd >= 0) {
330 close(netdev->netdev_fd);
332 if (netdev->tap_fd >= 0 && netdev->netdev_fd != netdev->tap_fd) {
333 close(netdev->tap_fd);
338 /* Initializes 'svec' with a list of the names of all known network devices. */
340 netdev_linux_enumerate(struct svec *svec)
342 struct if_nameindex *names;
344 names = if_nameindex();
348 for (i = 0; names[i].if_name != NULL; i++) {
349 svec_add(svec, names[i].if_name);
351 if_freenameindex(names);
354 VLOG_WARN("could not obtain list of network device names: %s",
361 netdev_linux_recv(struct netdev *netdev_, void *data, size_t size)
363 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
365 if (netdev->tap_fd < 0) {
366 /* Device was opened with NETDEV_ETH_TYPE_NONE. */
371 ssize_t retval = read(netdev->tap_fd, data, size);
374 } else if (errno != EINTR) {
375 if (errno != EAGAIN) {
376 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
377 strerror(errno), netdev_get_name(netdev_));
384 /* Registers with the poll loop to wake up from the next call to poll_block()
385 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
387 netdev_linux_recv_wait(struct netdev *netdev_)
389 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
390 if (netdev->tap_fd >= 0) {
391 poll_fd_wait(netdev->tap_fd, POLLIN);
395 /* Discards all packets waiting to be received from 'netdev'. */
397 netdev_linux_drain(struct netdev *netdev_)
399 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
400 if (netdev->tap_fd < 0 && netdev->netdev_fd < 0) {
402 } else if (netdev->tap_fd != netdev->netdev_fd) {
404 int error = netdev_linux_do_ioctl(netdev_, &ifr,
405 SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
409 drain_fd(netdev->tap_fd, ifr.ifr_qlen);
412 return drain_rcvbuf(netdev->netdev_fd);
416 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
417 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
418 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
419 * the packet is too big or too small to transmit on the device.
421 * The caller retains ownership of 'buffer' in all cases.
423 * The kernel maintains a packet transmission queue, so the caller is not
424 * expected to do additional queuing of packets. */
426 netdev_linux_send(struct netdev *netdev_, const void *data, size_t size)
428 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
430 /* XXX should support sending even if 'ethertype' was NETDEV_ETH_TYPE_NONE.
432 if (netdev->tap_fd < 0) {
437 ssize_t retval = write(netdev->tap_fd, data, size);
439 /* The Linux AF_PACKET implementation never blocks waiting for room
440 * for packets, instead returning ENOBUFS. Translate this into
441 * EAGAIN for the caller. */
442 if (errno == ENOBUFS) {
444 } else if (errno == EINTR) {
446 } else if (errno != EAGAIN) {
447 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
448 netdev_get_name(netdev_), strerror(errno));
451 } else if (retval != size) {
452 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
453 "%zu) on %s", retval, size, netdev_get_name(netdev_));
461 /* Registers with the poll loop to wake up from the next call to poll_block()
462 * when the packet transmission queue has sufficient room to transmit a packet
463 * with netdev_send().
465 * The kernel maintains a packet transmission queue, so the client is not
466 * expected to do additional queuing of packets. Thus, this function is
467 * unlikely to ever be used. It is included for completeness. */
469 netdev_linux_send_wait(struct netdev *netdev_)
471 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
472 if (netdev->tap_fd < 0 && netdev->netdev_fd < 0) {
474 } else if (netdev->tap_fd == netdev->netdev_fd) {
475 poll_fd_wait(netdev->tap_fd, POLLOUT);
477 /* TAP device always accepts packets.*/
478 poll_immediate_wake();
482 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
483 * otherwise a positive errno value. */
485 netdev_linux_set_etheraddr(struct netdev *netdev_,
486 const uint8_t mac[ETH_ADDR_LEN])
488 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
489 int error = set_etheraddr(netdev_get_name(netdev_), ARPHRD_ETHER, mac);
491 memcpy(netdev->cache->etheraddr, mac, ETH_ADDR_LEN);
496 /* Returns a pointer to 'netdev''s MAC address. The caller must not modify or
497 * free the returned buffer. */
499 netdev_linux_get_etheraddr(const struct netdev *netdev_,
500 uint8_t mac[ETH_ADDR_LEN])
502 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
503 if (!(netdev->cache->valid & VALID_ETHERADDR)) {
504 int error = get_etheraddr(netdev_get_name(netdev_),
505 netdev->cache->etheraddr);
509 netdev->cache->valid |= VALID_ETHERADDR;
511 memcpy(mac, netdev->cache->etheraddr, ETH_ADDR_LEN);
515 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
516 * in bytes, not including the hardware header; thus, this is typically 1500
517 * bytes for Ethernet devices. */
519 netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup)
521 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
522 if (!(netdev->cache->valid & VALID_MTU)) {
526 error = netdev_linux_do_ioctl(netdev_, &ifr, SIOCGIFMTU, "SIOCGIFMTU");
530 netdev->cache->mtu = ifr.ifr_mtu;
531 netdev->cache->valid |= VALID_MTU;
533 *mtup = netdev->cache->mtu;
538 netdev_linux_get_carrier(const struct netdev *netdev_, bool *carrier)
540 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
545 if (!(netdev->cache->valid & VALID_CARRIER)) {
549 fn = xasprintf("/sys/class/net/%s/carrier", netdev_get_name(netdev_));
550 fd = open(fn, O_RDONLY);
553 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(error));
557 retval = read(fd, line, sizeof line);
560 if (error == EINVAL) {
561 /* This is the normal return value when we try to check carrier
562 * if the network device is not up. */
564 VLOG_WARN_RL(&rl, "%s: read failed: %s", fn, strerror(error));
567 } else if (retval == 0) {
569 VLOG_WARN_RL(&rl, "%s: unexpected end of file", fn);
573 if (line[0] != '0' && line[0] != '1') {
575 VLOG_WARN_RL(&rl, "%s: value is %c (expected 0 or 1)",
579 netdev->cache->carrier = line[0] != '0';
580 netdev->cache->valid |= VALID_CARRIER;
582 *carrier = netdev->cache->carrier;
593 /* Check whether we can we use RTM_GETLINK to get network device statistics.
594 * In pre-2.6.19 kernels, this was only available if wireless extensions were
597 check_for_working_netlink_stats(void)
599 /* Decide on the netdev_get_stats() implementation to use. Netlink is
600 * preferable, so if that works, we'll use it. */
601 int ifindex = do_get_ifindex("lo");
603 VLOG_WARN("failed to get ifindex for lo, "
604 "obtaining netdev stats from proc");
607 struct netdev_stats stats;
608 int error = get_stats_via_netlink(ifindex, &stats);
610 VLOG_DBG("obtaining netdev stats via rtnetlink");
613 VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
614 "via proc (you are probably running a pre-2.6.19 "
615 "kernel)", strerror(error));
621 /* Retrieves current device stats for 'netdev'.
623 * XXX All of the members of struct netdev_stats are 64 bits wide, but on
624 * 32-bit architectures the Linux network stats are only 32 bits. */
626 netdev_linux_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
628 static int use_netlink_stats = -1;
631 COVERAGE_INC(netdev_get_stats);
632 if (use_netlink_stats < 0) {
633 use_netlink_stats = check_for_working_netlink_stats();
635 if (use_netlink_stats) {
638 error = get_ifindex(netdev, &ifindex);
640 error = get_stats_via_netlink(ifindex, stats);
643 error = get_stats_via_proc(netdev->name, stats);
648 /* Stores the features supported by 'netdev' into each of '*current',
649 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
650 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
651 * successful, otherwise a positive errno value. On failure, all of the
652 * passed-in values are set to 0. */
654 netdev_linux_get_features(struct netdev *netdev,
655 uint32_t *current, uint32_t *advertised,
656 uint32_t *supported, uint32_t *peer)
658 struct ethtool_cmd ecmd;
661 memset(&ecmd, 0, sizeof ecmd);
662 error = netdev_linux_do_ethtool(netdev, &ecmd,
663 ETHTOOL_GSET, "ETHTOOL_GSET");
668 /* Supported features. */
670 if (ecmd.supported & SUPPORTED_10baseT_Half) {
671 *supported |= OFPPF_10MB_HD;
673 if (ecmd.supported & SUPPORTED_10baseT_Full) {
674 *supported |= OFPPF_10MB_FD;
676 if (ecmd.supported & SUPPORTED_100baseT_Half) {
677 *supported |= OFPPF_100MB_HD;
679 if (ecmd.supported & SUPPORTED_100baseT_Full) {
680 *supported |= OFPPF_100MB_FD;
682 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
683 *supported |= OFPPF_1GB_HD;
685 if (ecmd.supported & SUPPORTED_1000baseT_Full) {
686 *supported |= OFPPF_1GB_FD;
688 if (ecmd.supported & SUPPORTED_10000baseT_Full) {
689 *supported |= OFPPF_10GB_FD;
691 if (ecmd.supported & SUPPORTED_TP) {
692 *supported |= OFPPF_COPPER;
694 if (ecmd.supported & SUPPORTED_FIBRE) {
695 *supported |= OFPPF_FIBER;
697 if (ecmd.supported & SUPPORTED_Autoneg) {
698 *supported |= OFPPF_AUTONEG;
700 if (ecmd.supported & SUPPORTED_Pause) {
701 *supported |= OFPPF_PAUSE;
703 if (ecmd.supported & SUPPORTED_Asym_Pause) {
704 *supported |= OFPPF_PAUSE_ASYM;
707 /* Advertised features. */
709 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
710 *advertised |= OFPPF_10MB_HD;
712 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
713 *advertised |= OFPPF_10MB_FD;
715 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
716 *advertised |= OFPPF_100MB_HD;
718 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
719 *advertised |= OFPPF_100MB_FD;
721 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
722 *advertised |= OFPPF_1GB_HD;
724 if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
725 *advertised |= OFPPF_1GB_FD;
727 if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
728 *advertised |= OFPPF_10GB_FD;
730 if (ecmd.advertising & ADVERTISED_TP) {
731 *advertised |= OFPPF_COPPER;
733 if (ecmd.advertising & ADVERTISED_FIBRE) {
734 *advertised |= OFPPF_FIBER;
736 if (ecmd.advertising & ADVERTISED_Autoneg) {
737 *advertised |= OFPPF_AUTONEG;
739 if (ecmd.advertising & ADVERTISED_Pause) {
740 *advertised |= OFPPF_PAUSE;
742 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
743 *advertised |= OFPPF_PAUSE_ASYM;
746 /* Current settings. */
747 if (ecmd.speed == SPEED_10) {
748 *current = ecmd.duplex ? OFPPF_10MB_FD : OFPPF_10MB_HD;
749 } else if (ecmd.speed == SPEED_100) {
750 *current = ecmd.duplex ? OFPPF_100MB_FD : OFPPF_100MB_HD;
751 } else if (ecmd.speed == SPEED_1000) {
752 *current = ecmd.duplex ? OFPPF_1GB_FD : OFPPF_1GB_HD;
753 } else if (ecmd.speed == SPEED_10000) {
754 *current = OFPPF_10GB_FD;
759 if (ecmd.port == PORT_TP) {
760 *current |= OFPPF_COPPER;
761 } else if (ecmd.port == PORT_FIBRE) {
762 *current |= OFPPF_FIBER;
766 *current |= OFPPF_AUTONEG;
769 /* Peer advertisements. */
775 /* Set the features advertised by 'netdev' to 'advertise'. */
777 netdev_linux_set_advertisements(struct netdev *netdev, uint32_t advertise)
779 struct ethtool_cmd ecmd;
782 memset(&ecmd, 0, sizeof ecmd);
783 error = netdev_linux_do_ethtool(netdev, &ecmd,
784 ETHTOOL_GSET, "ETHTOOL_GSET");
789 ecmd.advertising = 0;
790 if (advertise & OFPPF_10MB_HD) {
791 ecmd.advertising |= ADVERTISED_10baseT_Half;
793 if (advertise & OFPPF_10MB_FD) {
794 ecmd.advertising |= ADVERTISED_10baseT_Full;
796 if (advertise & OFPPF_100MB_HD) {
797 ecmd.advertising |= ADVERTISED_100baseT_Half;
799 if (advertise & OFPPF_100MB_FD) {
800 ecmd.advertising |= ADVERTISED_100baseT_Full;
802 if (advertise & OFPPF_1GB_HD) {
803 ecmd.advertising |= ADVERTISED_1000baseT_Half;
805 if (advertise & OFPPF_1GB_FD) {
806 ecmd.advertising |= ADVERTISED_1000baseT_Full;
808 if (advertise & OFPPF_10GB_FD) {
809 ecmd.advertising |= ADVERTISED_10000baseT_Full;
811 if (advertise & OFPPF_COPPER) {
812 ecmd.advertising |= ADVERTISED_TP;
814 if (advertise & OFPPF_FIBER) {
815 ecmd.advertising |= ADVERTISED_FIBRE;
817 if (advertise & OFPPF_AUTONEG) {
818 ecmd.advertising |= ADVERTISED_Autoneg;
820 if (advertise & OFPPF_PAUSE) {
821 ecmd.advertising |= ADVERTISED_Pause;
823 if (advertise & OFPPF_PAUSE_ASYM) {
824 ecmd.advertising |= ADVERTISED_Asym_Pause;
826 return netdev_linux_do_ethtool(netdev, &ecmd,
827 ETHTOOL_SSET, "ETHTOOL_SSET");
830 /* If 'netdev_name' is the name of a VLAN network device (e.g. one created with
831 * vconfig(8)), sets '*vlan_vid' to the VLAN VID associated with that device
832 * and returns 0. Otherwise returns a errno value (specifically ENOENT if
833 * 'netdev_name' is the name of a network device that is not a VLAN device) and
834 * sets '*vlan_vid' to -1. */
836 netdev_linux_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
838 const char *netdev_name = netdev_get_name(netdev);
839 struct ds line = DS_EMPTY_INITIALIZER;
844 COVERAGE_INC(netdev_get_vlan_vid);
845 fn = xasprintf("/proc/net/vlan/%s", netdev_name);
846 stream = fopen(fn, "r");
852 if (ds_get_line(&line, stream)) {
853 if (ferror(stream)) {
855 VLOG_ERR_RL(&rl, "error reading \"%s\": %s", fn, strerror(errno));
858 VLOG_ERR_RL(&rl, "unexpected end of file reading \"%s\"", fn);
863 if (!sscanf(ds_cstr(&line), "%*s VID: %d", vlan_vid)) {
865 VLOG_ERR_RL(&rl, "parse error reading \"%s\" line 1: \"%s\"",
884 #define POLICE_ADD_CMD "/sbin/tc qdisc add dev %s handle ffff: ingress"
885 #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"
886 /* We redirect stderr to /dev/null because we often want to remove all
887 * traffic control configuration on a port so its in a known state. If
888 * this done when there is no such configuration, tc complains, so we just
891 #define POLICE_DEL_CMD "/sbin/tc qdisc del dev %s handle ffff: ingress 2>/dev/null"
893 /* Attempts to set input rate limiting (policing) policy. */
895 netdev_linux_set_policing(struct netdev *netdev,
896 uint32_t kbits_rate, uint32_t kbits_burst)
898 const char *netdev_name = netdev_get_name(netdev);
901 COVERAGE_INC(netdev_set_policing);
904 /* Default to 10 kilobits if not specified. */
908 /* xxx This should be more careful about only adding if it
909 * xxx actually exists, as opposed to always deleting it. */
910 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
911 if (system(command) == -1) {
912 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
915 snprintf(command, sizeof(command), POLICE_ADD_CMD, netdev_name);
916 if (system(command) != 0) {
917 VLOG_WARN_RL(&rl, "%s: problem adding policing", netdev_name);
921 snprintf(command, sizeof(command), POLICE_CONFIG_CMD, netdev_name,
922 kbits_rate, kbits_burst);
923 if (system(command) != 0) {
924 VLOG_WARN_RL(&rl, "%s: problem configuring policing",
929 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
930 if (system(command) == -1) {
931 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
938 /* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
939 * 'in4' is non-null) and returns true. Otherwise, returns false. */
941 netdev_linux_get_in4(const struct netdev *netdev_, struct in_addr *in4)
943 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
944 if (!(netdev->cache->valid & VALID_IN4)) {
945 const struct sockaddr_in *sin;
949 ifr.ifr_addr.sa_family = AF_INET;
950 error = netdev_linux_do_ioctl(netdev_, &ifr,
951 SIOCGIFADDR, "SIOCGIFADDR");
956 sin = (struct sockaddr_in *) &ifr.ifr_addr;
957 netdev->cache->in4 = sin->sin_addr;
958 netdev->cache->valid |= VALID_IN4;
960 *in4 = netdev->cache->in4;
961 return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
964 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
965 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
966 * positive errno value. */
968 netdev_linux_set_in4(struct netdev *netdev_, struct in_addr addr,
971 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
974 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
976 netdev->cache->valid |= VALID_IN4;
977 netdev->cache->in4 = addr;
978 if (addr.s_addr != INADDR_ANY) {
979 error = do_set_addr(netdev_, SIOCSIFNETMASK,
980 "SIOCSIFNETMASK", mask);
987 parse_if_inet6_line(const char *line,
988 struct in6_addr *in6, char ifname[16 + 1])
990 uint8_t *s6 = in6->s6_addr;
993 " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
994 "%*x %*x %*x %*x %16s\n",
995 &s6[0], &s6[1], &s6[2], &s6[3],
996 &s6[4], &s6[5], &s6[6], &s6[7],
997 &s6[8], &s6[9], &s6[10], &s6[11],
998 &s6[12], &s6[13], &s6[14], &s6[15],
1002 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
1003 * 'in6' is non-null) and returns true. Otherwise, returns false. */
1005 netdev_linux_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1007 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1008 if (!(netdev->cache->valid & VALID_IN6)) {
1012 netdev->cache->in6 = in6addr_any;
1014 file = fopen("/proc/net/if_inet6", "r");
1016 const char *name = netdev_get_name(netdev_);
1017 while (fgets(line, sizeof line, file)) {
1018 struct in6_addr in6;
1019 char ifname[16 + 1];
1020 if (parse_if_inet6_line(line, &in6, ifname)
1021 && !strcmp(name, ifname))
1023 netdev->cache->in6 = in6;
1029 netdev->cache->valid |= VALID_IN6;
1031 *in6 = netdev->cache->in6;
1036 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1038 struct sockaddr_in sin;
1039 memset(&sin, 0, sizeof sin);
1040 sin.sin_family = AF_INET;
1041 sin.sin_addr = addr;
1044 memset(sa, 0, sizeof *sa);
1045 memcpy(sa, &sin, sizeof sin);
1049 do_set_addr(struct netdev *netdev,
1050 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1053 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1054 make_in4_sockaddr(&ifr.ifr_addr, addr);
1055 return netdev_linux_do_ioctl(netdev, &ifr, ioctl_nr, ioctl_name);
1058 /* Adds 'router' as a default IP gateway. */
1060 netdev_linux_add_router(struct netdev *netdev UNUSED, struct in_addr router)
1062 struct in_addr any = { INADDR_ANY };
1066 memset(&rt, 0, sizeof rt);
1067 make_in4_sockaddr(&rt.rt_dst, any);
1068 make_in4_sockaddr(&rt.rt_gateway, router);
1069 make_in4_sockaddr(&rt.rt_genmask, any);
1070 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1071 COVERAGE_INC(netdev_add_router);
1072 error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
1074 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
1079 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1080 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1081 * returns 0. Otherwise, it returns a positive errno value; in particular,
1082 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
1084 netdev_linux_arp_lookup(const struct netdev *netdev,
1085 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
1088 struct sockaddr_in *pa;
1091 memset(&r, 0, sizeof r);
1092 pa = (struct sockaddr_in *) &r.arp_pa;
1093 pa->sin_family = AF_INET;
1094 pa->sin_addr.s_addr = ip;
1096 r.arp_ha.sa_family = ARPHRD_ETHER;
1098 strncpy(r.arp_dev, netdev->name, sizeof r.arp_dev);
1099 COVERAGE_INC(netdev_arp_lookup);
1100 retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
1102 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
1103 } else if (retval != ENXIO) {
1104 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
1105 netdev->name, IP_ARGS(&ip), strerror(retval));
1111 nd_to_iff_flags(enum netdev_flags nd)
1114 if (nd & NETDEV_UP) {
1117 if (nd & NETDEV_PROMISC) {
1124 iff_to_nd_flags(int iff)
1126 enum netdev_flags nd = 0;
1130 if (iff & IFF_PROMISC) {
1131 nd |= NETDEV_PROMISC;
1137 netdev_linux_update_flags(struct netdev *netdev, enum netdev_flags off,
1138 enum netdev_flags on, enum netdev_flags *old_flagsp)
1140 int old_flags, new_flags;
1143 error = get_flags(netdev, &old_flags);
1145 *old_flagsp = iff_to_nd_flags(old_flags);
1146 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1147 if (new_flags != old_flags) {
1148 error = set_flags(netdev, new_flags);
1155 poll_notify(struct list *list)
1157 struct netdev_linux_notifier *notifier;
1158 LIST_FOR_EACH (notifier, struct netdev_linux_notifier, node, list) {
1159 struct netdev_notifier *n = ¬ifier->notifier;
1165 netdev_linux_poll_cb(const struct rtnetlink_change *change,
1169 struct list *list = shash_find_data(&netdev_linux_notifiers,
1175 struct shash_node *node;
1176 SHASH_FOR_EACH (node, &netdev_linux_notifiers) {
1177 poll_notify(node->data);
1183 netdev_linux_poll_add(struct netdev *netdev,
1184 void (*cb)(struct netdev_notifier *), void *aux,
1185 struct netdev_notifier **notifierp)
1187 const char *netdev_name = netdev_get_name(netdev);
1188 struct netdev_linux_notifier *notifier;
1191 if (shash_is_empty(&netdev_linux_notifiers)) {
1192 int error = rtnetlink_notifier_register(&netdev_linux_poll_notifier,
1193 netdev_linux_poll_cb, NULL);
1199 list = shash_find_data(&netdev_linux_notifiers, netdev_name);
1201 list = xmalloc(sizeof *list);
1203 shash_add(&netdev_linux_notifiers, netdev_name, list);
1206 notifier = xmalloc(sizeof *notifier);
1207 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
1208 list_push_back(list, ¬ifier->node);
1209 *notifierp = ¬ifier->notifier;
1214 netdev_linux_poll_remove(struct netdev_notifier *notifier_)
1216 struct netdev_linux_notifier *notifier =
1217 CONTAINER_OF(notifier_, struct netdev_linux_notifier, notifier);
1220 /* Remove 'notifier' from its list. */
1221 list = list_remove(¬ifier->node);
1222 if (list_is_empty(list)) {
1223 /* The list is now empty. Remove it from the hash and free it. */
1224 const char *netdev_name = netdev_get_name(notifier->notifier.netdev);
1225 shash_delete(&netdev_linux_notifiers,
1226 shash_find(&netdev_linux_notifiers, netdev_name));
1231 /* If that was the last notifier, unregister. */
1232 if (shash_is_empty(&netdev_linux_notifiers)) {
1233 rtnetlink_notifier_unregister(&netdev_linux_poll_notifier);
1237 const struct netdev_class netdev_linux_class = {
1248 netdev_linux_enumerate,
1251 netdev_linux_recv_wait,
1255 netdev_linux_send_wait,
1257 netdev_linux_set_etheraddr,
1258 netdev_linux_get_etheraddr,
1259 netdev_linux_get_mtu,
1260 netdev_linux_get_carrier,
1261 netdev_linux_get_stats,
1263 netdev_linux_get_features,
1264 netdev_linux_set_advertisements,
1265 netdev_linux_get_vlan_vid,
1266 netdev_linux_set_policing,
1268 netdev_linux_get_in4,
1269 netdev_linux_set_in4,
1270 netdev_linux_get_in6,
1271 netdev_linux_add_router,
1272 netdev_linux_arp_lookup,
1274 netdev_linux_update_flags,
1276 netdev_linux_poll_add,
1277 netdev_linux_poll_remove,
1280 const struct netdev_class netdev_tap_class = {
1291 netdev_linux_enumerate,
1294 netdev_linux_recv_wait,
1298 netdev_linux_send_wait,
1300 netdev_linux_set_etheraddr,
1301 netdev_linux_get_etheraddr,
1302 netdev_linux_get_mtu,
1303 netdev_linux_get_carrier,
1304 netdev_linux_get_stats,
1306 netdev_linux_get_features,
1307 netdev_linux_set_advertisements,
1308 netdev_linux_get_vlan_vid,
1309 netdev_linux_set_policing,
1311 netdev_linux_get_in4,
1312 netdev_linux_set_in4,
1313 netdev_linux_get_in6,
1314 netdev_linux_add_router,
1315 netdev_linux_arp_lookup,
1317 netdev_linux_update_flags,
1319 netdev_linux_poll_add,
1320 netdev_linux_poll_remove,
1324 get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
1326 /* Policy for RTNLGRP_LINK messages.
1328 * There are *many* more fields in these messages, but currently we only
1329 * care about these fields. */
1330 static const struct nl_policy rtnlgrp_link_policy[] = {
1331 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
1332 [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
1333 .min_len = sizeof(struct rtnl_link_stats) },
1337 static struct nl_sock *rtnl_sock;
1338 struct ofpbuf request;
1339 struct ofpbuf *reply;
1340 struct ifinfomsg *ifi;
1341 const struct rtnl_link_stats *rtnl_stats;
1342 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1346 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0, &rtnl_sock);
1348 VLOG_ERR_RL(&rl, "failed to create rtnetlink socket: %s",
1354 ofpbuf_init(&request, 0);
1355 nl_msg_put_nlmsghdr(&request, rtnl_sock, sizeof *ifi,
1356 RTM_GETLINK, NLM_F_REQUEST);
1357 ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
1358 ifi->ifi_family = PF_UNSPEC;
1359 ifi->ifi_index = ifindex;
1360 error = nl_sock_transact(rtnl_sock, &request, &reply);
1361 ofpbuf_uninit(&request);
1366 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1367 rtnlgrp_link_policy,
1368 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1369 ofpbuf_delete(reply);
1373 if (!attrs[IFLA_STATS]) {
1374 VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
1378 rtnl_stats = nl_attr_get(attrs[IFLA_STATS]);
1379 stats->rx_packets = rtnl_stats->rx_packets;
1380 stats->tx_packets = rtnl_stats->tx_packets;
1381 stats->rx_bytes = rtnl_stats->rx_bytes;
1382 stats->tx_bytes = rtnl_stats->tx_bytes;
1383 stats->rx_errors = rtnl_stats->rx_errors;
1384 stats->tx_errors = rtnl_stats->tx_errors;
1385 stats->rx_dropped = rtnl_stats->rx_dropped;
1386 stats->tx_dropped = rtnl_stats->tx_dropped;
1387 stats->multicast = rtnl_stats->multicast;
1388 stats->collisions = rtnl_stats->collisions;
1389 stats->rx_length_errors = rtnl_stats->rx_length_errors;
1390 stats->rx_over_errors = rtnl_stats->rx_over_errors;
1391 stats->rx_crc_errors = rtnl_stats->rx_crc_errors;
1392 stats->rx_frame_errors = rtnl_stats->rx_frame_errors;
1393 stats->rx_fifo_errors = rtnl_stats->rx_fifo_errors;
1394 stats->rx_missed_errors = rtnl_stats->rx_missed_errors;
1395 stats->tx_aborted_errors = rtnl_stats->tx_aborted_errors;
1396 stats->tx_carrier_errors = rtnl_stats->tx_carrier_errors;
1397 stats->tx_fifo_errors = rtnl_stats->tx_fifo_errors;
1398 stats->tx_heartbeat_errors = rtnl_stats->tx_heartbeat_errors;
1399 stats->tx_window_errors = rtnl_stats->tx_window_errors;
1405 get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats)
1407 static const char fn[] = "/proc/net/dev";
1412 stream = fopen(fn, "r");
1414 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1419 while (fgets(line, sizeof line, stream)) {
1422 #define X64 "%"SCNu64
1425 X64 X64 X64 X64 X64 X64 X64 "%*u"
1426 X64 X64 X64 X64 X64 X64 X64 "%*u",
1432 &stats->rx_fifo_errors,
1433 &stats->rx_frame_errors,
1439 &stats->tx_fifo_errors,
1441 &stats->tx_carrier_errors) != 15) {
1442 VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln);
1443 } else if (!strcmp(devname, netdev_name)) {
1444 stats->rx_length_errors = UINT64_MAX;
1445 stats->rx_over_errors = UINT64_MAX;
1446 stats->rx_crc_errors = UINT64_MAX;
1447 stats->rx_missed_errors = UINT64_MAX;
1448 stats->tx_aborted_errors = UINT64_MAX;
1449 stats->tx_heartbeat_errors = UINT64_MAX;
1450 stats->tx_window_errors = UINT64_MAX;
1456 VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name);
1462 get_flags(const struct netdev *netdev, int *flags)
1467 error = netdev_linux_do_ioctl(netdev, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
1468 *flags = ifr.ifr_flags;
1473 set_flags(struct netdev *netdev, int flags)
1477 ifr.ifr_flags = flags;
1478 return netdev_linux_do_ioctl(netdev, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1482 do_get_ifindex(const char *netdev_name)
1486 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1487 COVERAGE_INC(netdev_get_ifindex);
1488 if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) {
1489 VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
1490 netdev_name, strerror(errno));
1493 return ifr.ifr_ifindex;
1497 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1499 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1501 if (!(netdev->cache->valid & VALID_IFINDEX)) {
1502 int ifindex = do_get_ifindex(netdev_get_name(netdev_));
1506 netdev->cache->valid |= VALID_IFINDEX;
1507 netdev->cache->ifindex = ifindex;
1509 *ifindexp = netdev->cache->ifindex;
1514 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1519 memset(&ifr, 0, sizeof ifr);
1520 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1521 COVERAGE_INC(netdev_get_hwaddr);
1522 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
1523 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
1524 netdev_name, strerror(errno));
1527 hwaddr_family = ifr.ifr_hwaddr.sa_family;
1528 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
1529 VLOG_WARN("%s device has unknown hardware address family %d",
1530 netdev_name, hwaddr_family);
1532 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
1537 set_etheraddr(const char *netdev_name, int hwaddr_family,
1538 const uint8_t mac[ETH_ADDR_LEN])
1542 memset(&ifr, 0, sizeof ifr);
1543 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1544 ifr.ifr_hwaddr.sa_family = hwaddr_family;
1545 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
1546 COVERAGE_INC(netdev_set_hwaddr);
1547 if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) {
1548 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
1549 netdev_name, strerror(errno));
1556 netdev_linux_do_ethtool(struct netdev *netdev, struct ethtool_cmd *ecmd,
1557 int cmd, const char *cmd_name)
1561 memset(&ifr, 0, sizeof ifr);
1562 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1563 ifr.ifr_data = (caddr_t) ecmd;
1566 COVERAGE_INC(netdev_ethtool);
1567 if (ioctl(af_inet_sock, SIOCETHTOOL, &ifr) == 0) {
1570 if (errno != EOPNOTSUPP) {
1571 VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
1572 "failed: %s", cmd_name, netdev->name,
1575 /* The device doesn't support this operation. That's pretty
1576 * common, so there's no point in logging anything. */
1583 netdev_linux_do_ioctl(const struct netdev *netdev, struct ifreq *ifr,
1584 int cmd, const char *cmd_name)
1586 strncpy(ifr->ifr_name, netdev_get_name(netdev), sizeof ifr->ifr_name);
1587 if (ioctl(af_inet_sock, cmd, ifr) == -1) {
1588 VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s",
1589 netdev_get_name(netdev), cmd_name, strerror(errno));