2 * Copyright (c) 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <arpa/inet.h>
23 #include <linux/if_tun.h>
25 #include <linux/types.h>
26 #include <linux/ethtool.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sockios.h>
29 #include <linux/version.h>
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <netpacket/packet.h>
34 #include <net/ethernet.h>
36 #include <linux/if_tunnel.h>
37 #include <net/if_arp.h>
38 #include <net/if_packet.h>
39 #include <net/route.h>
40 #include <netinet/in.h>
47 #include "dynamic-string.h"
48 #include "fatal-signal.h"
49 #include "netdev-provider.h"
52 #include "openflow/openflow.h"
53 #include "openvswitch/internal_dev.h"
54 #include "openvswitch/gre.h"
56 #include "poll-loop.h"
57 #include "rtnetlink.h"
58 #include "socket-util.h"
62 #define THIS_MODULE VLM_netdev_linux
65 /* These were introduced in Linux 2.6.14, so they might be missing if we have
67 #ifndef ADVERTISED_Pause
68 #define ADVERTISED_Pause (1 << 13)
70 #ifndef ADVERTISED_Asym_Pause
71 #define ADVERTISED_Asym_Pause (1 << 14)
74 static struct rtnetlink_notifier netdev_linux_cache_notifier;
75 static int cache_notifier_refcount;
78 VALID_IFINDEX = 1 << 0,
79 VALID_ETHERADDR = 1 << 1,
83 VALID_CARRIER = 1 << 5,
84 VALID_IS_PSEUDO = 1 << 6, /* Represents is_internal and is_tap. */
85 VALID_POLICING = 1 << 7
96 struct netdev_dev_linux {
97 struct netdev_dev netdev_dev;
99 struct shash_node *shash_node;
100 unsigned int cache_valid;
102 /* The following are figured out "on demand" only. They are only valid
103 * when the corresponding VALID_* bit in 'cache_valid' is set. */
105 uint8_t etheraddr[ETH_ADDR_LEN];
106 struct in_addr address, netmask;
110 bool is_internal; /* Is this an openvswitch internal device? */
111 bool is_tap; /* Is this a tuntap device? */
112 uint32_t kbits_rate; /* Policing data. */
113 uint32_t kbits_burst;
116 struct tap_state tap;
117 struct patch_state patch;
121 struct netdev_linux {
122 struct netdev netdev;
126 /* An AF_INET socket (used for ioctl operations). */
127 static int af_inet_sock = -1;
129 struct netdev_linux_notifier {
130 struct netdev_notifier notifier;
134 static struct shash netdev_linux_notifiers =
135 SHASH_INITIALIZER(&netdev_linux_notifiers);
136 static struct rtnetlink_notifier netdev_linux_poll_notifier;
138 /* This is set pretty low because we probably won't learn anything from the
139 * additional log messages. */
140 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
142 static int netdev_linux_init(void);
144 static int netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *,
145 int cmd, const char *cmd_name);
146 static int netdev_linux_do_ioctl(const char *name, struct ifreq *, int cmd,
147 const char *cmd_name);
148 static int netdev_linux_get_ipv4(const struct netdev *, struct in_addr *,
149 int cmd, const char *cmd_name);
150 static int get_flags(const struct netdev *, int *flagsp);
151 static int set_flags(struct netdev *, int flags);
152 static int do_get_ifindex(const char *netdev_name);
153 static int get_ifindex(const struct netdev *, int *ifindexp);
154 static int do_set_addr(struct netdev *netdev,
155 int ioctl_nr, const char *ioctl_name,
156 struct in_addr addr);
157 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
158 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
159 const uint8_t[ETH_ADDR_LEN]);
160 static int get_stats_via_netlink(int ifindex, struct netdev_stats *stats);
161 static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats);
162 static int get_rtnl_sock(struct nl_sock **);
165 is_netdev_linux_class(const struct netdev_class *netdev_class)
167 return netdev_class->init == netdev_linux_init;
170 static struct netdev_dev_linux *
171 netdev_dev_linux_cast(const struct netdev_dev *netdev_dev)
173 const struct netdev_class *netdev_class = netdev_dev_get_class(netdev_dev);
174 assert(is_netdev_linux_class(netdev_class));
176 return CONTAINER_OF(netdev_dev, struct netdev_dev_linux, netdev_dev);
179 static struct netdev_linux *
180 netdev_linux_cast(const struct netdev *netdev)
182 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
183 const struct netdev_class *netdev_class = netdev_dev_get_class(netdev_dev);
184 assert(is_netdev_linux_class(netdev_class));
186 return CONTAINER_OF(netdev, struct netdev_linux, netdev);
190 netdev_linux_init(void)
192 static int status = -1;
194 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
195 status = af_inet_sock >= 0 ? 0 : errno;
197 VLOG_ERR("failed to create inet socket: %s", strerror(status));
204 netdev_linux_run(void)
206 rtnetlink_notifier_run();
210 netdev_linux_wait(void)
212 rtnetlink_notifier_wait();
216 netdev_linux_cache_cb(const struct rtnetlink_change *change,
217 void *aux OVS_UNUSED)
219 struct netdev_dev_linux *dev;
221 struct netdev_dev *base_dev = netdev_dev_from_name(change->ifname);
223 const struct netdev_class *netdev_class =
224 netdev_dev_get_class(base_dev);
226 if (is_netdev_linux_class(netdev_class)) {
227 dev = netdev_dev_linux_cast(base_dev);
228 dev->cache_valid = 0;
232 struct shash device_shash;
233 struct shash_node *node;
235 shash_init(&device_shash);
236 netdev_dev_get_devices(&netdev_linux_class, &device_shash);
237 SHASH_FOR_EACH (node, &device_shash) {
239 dev->cache_valid = 0;
241 shash_destroy(&device_shash);
246 if_up(const char *name)
250 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
251 ifr.ifr_flags = IFF_UP;
253 if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) {
254 VLOG_DBG_RL(&rl, "%s: failed to bring device up: %s",
255 name, strerror(errno));
262 /* A veth may be created using the 'command' "+<name>,<peer>". A veth may
263 * be destroyed by using the 'command' "-<name>", where <name> can be
264 * either side of the device.
267 modify_veth(const char *format, ...)
273 veth_file = fopen("/sys/class/net/veth_pairs", "w");
275 VLOG_WARN_RL(&rl, "could not open veth device. Are you running a "
276 "supported XenServer with the kernel module loaded?");
279 setvbuf(veth_file, NULL, _IONBF, 0);
281 va_start(args, format);
282 retval = vfprintf(veth_file, format, args);
287 VLOG_WARN_RL(&rl, "could not destroy patch: %s", strerror(errno));
295 create_patch(const char *name, const char *peer)
298 struct netdev_dev *peer_nd;
301 /* Only create the veth if the peer didn't already do it. */
302 peer_nd = netdev_dev_from_name(peer);
304 if (!strcmp("patch", netdev_dev_get_type(peer_nd))) {
305 struct netdev_dev_linux *ndl = netdev_dev_linux_cast(peer_nd);
306 if (!strcmp(name, ndl->state.patch.peer)) {
309 VLOG_WARN_RL(&rl, "peer '%s' already paired with '%s'",
310 peer, ndl->state.patch.peer);
314 VLOG_WARN_RL(&rl, "peer '%s' exists and is not a patch", peer);
319 retval = modify_veth("+%s,%s", name, peer);
324 retval = if_up(name);
329 retval = if_up(peer);
338 setup_patch(const char *name, const struct shash *args, char **peer_)
342 peer = shash_find_data(args, "peer");
344 VLOG_WARN("patch type requires valid 'peer' argument");
348 if (shash_count(args) > 1) {
349 VLOG_WARN("patch type takes only a 'peer' argument");
353 if (strlen(peer) >= IFNAMSIZ) {
354 VLOG_WARN_RL(&rl, "patch 'peer' arg too long");
358 *peer_ = xstrdup(peer);
359 return create_patch(name, peer);
362 /* Creates the netdev device of 'type' with 'name'. */
364 netdev_linux_create_system(const char *name, const char *type OVS_UNUSED,
365 const struct shash *args, struct netdev_dev **netdev_devp)
367 struct netdev_dev_linux *netdev_dev;
370 if (!shash_is_empty(args)) {
371 VLOG_WARN("%s: arguments for system devices should be empty", name);
374 if (!cache_notifier_refcount) {
375 error = rtnetlink_notifier_register(&netdev_linux_cache_notifier,
376 netdev_linux_cache_cb, NULL);
381 cache_notifier_refcount++;
383 netdev_dev = xzalloc(sizeof *netdev_dev);
384 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_linux_class);
386 *netdev_devp = &netdev_dev->netdev_dev;
390 /* For most types of netdevs we open the device for each call of
391 * netdev_open(). However, this is not the case with tap devices,
392 * since it is only possible to open the device once. In this
393 * situation we share a single file descriptor, and consequently
394 * buffers, across all readers. Therefore once data is read it will
395 * be unavailable to other reads for tap devices. */
397 netdev_linux_create_tap(const char *name, const char *type OVS_UNUSED,
398 const struct shash *args, struct netdev_dev **netdev_devp)
400 struct netdev_dev_linux *netdev_dev;
401 struct tap_state *state;
402 static const char tap_dev[] = "/dev/net/tun";
406 if (!shash_is_empty(args)) {
407 VLOG_WARN("%s: arguments for TAP devices should be empty", name);
410 netdev_dev = xzalloc(sizeof *netdev_dev);
411 state = &netdev_dev->state.tap;
413 /* Open tap device. */
414 state->fd = open(tap_dev, O_RDWR);
417 VLOG_WARN("opening \"%s\" failed: %s", tap_dev, strerror(error));
421 /* Create tap device. */
422 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
423 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
424 if (ioctl(state->fd, TUNSETIFF, &ifr) == -1) {
425 VLOG_WARN("%s: creating tap device failed: %s", name,
431 /* Make non-blocking. */
432 error = set_nonblocking(state->fd);
437 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_tap_class);
438 *netdev_devp = &netdev_dev->netdev_dev;
447 netdev_linux_create_patch(const char *name, const char *type OVS_UNUSED,
448 const struct shash *args, struct netdev_dev **netdev_devp)
450 struct netdev_dev_linux *netdev_dev;
454 error = setup_patch(name, args, &peer);
460 netdev_dev = xzalloc(sizeof *netdev_dev);
461 netdev_dev->state.patch.peer = peer;
462 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_patch_class);
463 *netdev_devp = &netdev_dev->netdev_dev;
469 destroy_tap(struct netdev_dev_linux *netdev_dev)
471 struct tap_state *state = &netdev_dev->state.tap;
473 if (state->fd >= 0) {
479 destroy_patch(struct netdev_dev_linux *netdev_dev)
481 const char *name = netdev_dev_get_name(&netdev_dev->netdev_dev);
482 struct patch_state *state = &netdev_dev->state.patch;
484 /* Only destroy veth if 'peer' doesn't exist as an existing netdev. */
485 if (!netdev_dev_from_name(state->peer)) {
486 modify_veth("-%s", name);
491 /* Destroys the netdev device 'netdev_dev_'. */
493 netdev_linux_destroy(struct netdev_dev *netdev_dev_)
495 struct netdev_dev_linux *netdev_dev = netdev_dev_linux_cast(netdev_dev_);
496 const char *type = netdev_dev_get_type(netdev_dev_);
498 if (!strcmp(type, "system")) {
499 cache_notifier_refcount--;
501 if (!cache_notifier_refcount) {
502 rtnetlink_notifier_unregister(&netdev_linux_cache_notifier);
504 } else if (!strcmp(type, "tap")) {
505 destroy_tap(netdev_dev);
506 } else if (!strcmp(type, "patch")) {
507 destroy_patch(netdev_dev);
514 netdev_linux_open(struct netdev_dev *netdev_dev_, int ethertype,
515 struct netdev **netdevp)
517 struct netdev_dev_linux *netdev_dev = netdev_dev_linux_cast(netdev_dev_);
518 struct netdev_linux *netdev;
519 enum netdev_flags flags;
522 /* Allocate network device. */
523 netdev = xzalloc(sizeof *netdev);
525 netdev_init(&netdev->netdev, netdev_dev_);
527 error = netdev_get_flags(&netdev->netdev, &flags);
528 if (error == ENODEV) {
532 if (!strcmp(netdev_dev_get_type(netdev_dev_), "tap")) {
533 netdev->fd = netdev_dev->state.tap.fd;
534 } else if (ethertype != NETDEV_ETH_TYPE_NONE) {
535 struct sockaddr_ll sll;
539 /* Create file descriptor. */
540 protocol = (ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
541 : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
543 netdev->fd = socket(PF_PACKET, SOCK_RAW, htons(protocol));
544 if (netdev->fd < 0) {
549 /* Set non-blocking mode. */
550 error = set_nonblocking(netdev->fd);
555 /* Get ethernet device index. */
556 error = get_ifindex(&netdev->netdev, &ifindex);
561 /* Bind to specific ethernet device. */
562 memset(&sll, 0, sizeof sll);
563 sll.sll_family = AF_PACKET;
564 sll.sll_ifindex = ifindex;
566 (struct sockaddr *) &sll, sizeof sll) < 0) {
568 VLOG_ERR("bind to %s failed: %s", netdev_dev_get_name(netdev_dev_),
573 /* Between the socket() and bind() calls above, the socket receives all
574 * packets of the requested type on all system interfaces. We do not
575 * want to receive that data, but there is no way to avoid it. So we
576 * must now drain out the receive queue. */
577 error = drain_rcvbuf(netdev->fd);
583 *netdevp = &netdev->netdev;
587 netdev_uninit(&netdev->netdev, true);
591 /* Closes and destroys 'netdev'. */
593 netdev_linux_close(struct netdev *netdev_)
595 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
597 if (netdev->fd > 0 && strcmp(netdev_get_type(netdev_), "tap")) {
603 /* Initializes 'svec' with a list of the names of all known network devices. */
605 netdev_linux_enumerate(struct svec *svec)
607 struct if_nameindex *names;
609 names = if_nameindex();
613 for (i = 0; names[i].if_name != NULL; i++) {
614 svec_add(svec, names[i].if_name);
616 if_freenameindex(names);
619 VLOG_WARN("could not obtain list of network device names: %s",
626 netdev_linux_recv(struct netdev *netdev_, void *data, size_t size)
628 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
630 if (netdev->fd < 0) {
631 /* Device was opened with NETDEV_ETH_TYPE_NONE. */
636 ssize_t retval = read(netdev->fd, data, size);
639 } else if (errno != EINTR) {
640 if (errno != EAGAIN) {
641 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
642 strerror(errno), netdev_get_name(netdev_));
649 /* Registers with the poll loop to wake up from the next call to poll_block()
650 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
652 netdev_linux_recv_wait(struct netdev *netdev_)
654 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
655 if (netdev->fd >= 0) {
656 poll_fd_wait(netdev->fd, POLLIN);
660 /* Discards all packets waiting to be received from 'netdev'. */
662 netdev_linux_drain(struct netdev *netdev_)
664 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
665 if (netdev->fd < 0) {
667 } else if (!strcmp(netdev_get_type(netdev_), "tap")) {
669 int error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr,
670 SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
674 drain_fd(netdev->fd, ifr.ifr_qlen);
677 return drain_rcvbuf(netdev->fd);
681 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
682 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
683 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
684 * the packet is too big or too small to transmit on the device.
686 * The caller retains ownership of 'buffer' in all cases.
688 * The kernel maintains a packet transmission queue, so the caller is not
689 * expected to do additional queuing of packets. */
691 netdev_linux_send(struct netdev *netdev_, const void *data, size_t size)
693 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
695 /* XXX should support sending even if 'ethertype' was NETDEV_ETH_TYPE_NONE.
697 if (netdev->fd < 0) {
702 ssize_t retval = write(netdev->fd, data, size);
704 /* The Linux AF_PACKET implementation never blocks waiting for room
705 * for packets, instead returning ENOBUFS. Translate this into
706 * EAGAIN for the caller. */
707 if (errno == ENOBUFS) {
709 } else if (errno == EINTR) {
711 } else if (errno != EAGAIN) {
712 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
713 netdev_get_name(netdev_), strerror(errno));
716 } else if (retval != size) {
717 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
718 "%zu) on %s", retval, size, netdev_get_name(netdev_));
726 /* Registers with the poll loop to wake up from the next call to poll_block()
727 * when the packet transmission queue has sufficient room to transmit a packet
728 * with netdev_send().
730 * The kernel maintains a packet transmission queue, so the client is not
731 * expected to do additional queuing of packets. Thus, this function is
732 * unlikely to ever be used. It is included for completeness. */
734 netdev_linux_send_wait(struct netdev *netdev_)
736 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
737 if (netdev->fd < 0) {
739 } else if (strcmp(netdev_get_type(netdev_), "tap")) {
740 poll_fd_wait(netdev->fd, POLLOUT);
742 /* TAP device always accepts packets.*/
743 poll_immediate_wake();
747 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
748 * otherwise a positive errno value. */
750 netdev_linux_set_etheraddr(struct netdev *netdev_,
751 const uint8_t mac[ETH_ADDR_LEN])
753 struct netdev_dev_linux *netdev_dev =
754 netdev_dev_linux_cast(netdev_get_dev(netdev_));
757 if (!(netdev_dev->cache_valid & VALID_ETHERADDR)
758 || !eth_addr_equals(netdev_dev->etheraddr, mac)) {
759 error = set_etheraddr(netdev_get_name(netdev_), ARPHRD_ETHER, mac);
761 netdev_dev->cache_valid |= VALID_ETHERADDR;
762 memcpy(netdev_dev->etheraddr, mac, ETH_ADDR_LEN);
770 /* Returns a pointer to 'netdev''s MAC address. The caller must not modify or
771 * free the returned buffer. */
773 netdev_linux_get_etheraddr(const struct netdev *netdev_,
774 uint8_t mac[ETH_ADDR_LEN])
776 struct netdev_dev_linux *netdev_dev =
777 netdev_dev_linux_cast(netdev_get_dev(netdev_));
778 if (!(netdev_dev->cache_valid & VALID_ETHERADDR)) {
779 int error = get_etheraddr(netdev_get_name(netdev_),
780 netdev_dev->etheraddr);
784 netdev_dev->cache_valid |= VALID_ETHERADDR;
786 memcpy(mac, netdev_dev->etheraddr, ETH_ADDR_LEN);
790 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
791 * in bytes, not including the hardware header; thus, this is typically 1500
792 * bytes for Ethernet devices. */
794 netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup)
796 struct netdev_dev_linux *netdev_dev =
797 netdev_dev_linux_cast(netdev_get_dev(netdev_));
798 if (!(netdev_dev->cache_valid & VALID_MTU)) {
802 error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr,
803 SIOCGIFMTU, "SIOCGIFMTU");
807 netdev_dev->mtu = ifr.ifr_mtu;
808 netdev_dev->cache_valid |= VALID_MTU;
810 *mtup = netdev_dev->mtu;
814 /* Returns the ifindex of 'netdev', if successful, as a positive number.
815 * On failure, returns a negative errno value. */
817 netdev_linux_get_ifindex(const struct netdev *netdev)
821 error = get_ifindex(netdev, &ifindex);
822 return error ? -error : ifindex;
826 netdev_linux_get_carrier(const struct netdev *netdev_, bool *carrier)
828 struct netdev_dev_linux *netdev_dev =
829 netdev_dev_linux_cast(netdev_get_dev(netdev_));
834 if (!(netdev_dev->cache_valid & VALID_CARRIER)) {
838 fn = xasprintf("/sys/class/net/%s/carrier",
839 netdev_get_name(netdev_));
840 fd = open(fn, O_RDONLY);
843 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(error));
847 retval = read(fd, line, sizeof line);
850 if (error == EINVAL) {
851 /* This is the normal return value when we try to check carrier
852 * if the network device is not up. */
854 VLOG_WARN_RL(&rl, "%s: read failed: %s", fn, strerror(error));
857 } else if (retval == 0) {
859 VLOG_WARN_RL(&rl, "%s: unexpected end of file", fn);
863 if (line[0] != '0' && line[0] != '1') {
865 VLOG_WARN_RL(&rl, "%s: value is %c (expected 0 or 1)",
869 netdev_dev->carrier = line[0] != '0';
870 netdev_dev->cache_valid |= VALID_CARRIER;
872 *carrier = netdev_dev->carrier;
883 /* Check whether we can we use RTM_GETLINK to get network device statistics.
884 * In pre-2.6.19 kernels, this was only available if wireless extensions were
887 check_for_working_netlink_stats(void)
889 /* Decide on the netdev_get_stats() implementation to use. Netlink is
890 * preferable, so if that works, we'll use it. */
891 int ifindex = do_get_ifindex("lo");
893 VLOG_WARN("failed to get ifindex for lo, "
894 "obtaining netdev stats from proc");
897 struct netdev_stats stats;
898 int error = get_stats_via_netlink(ifindex, &stats);
900 VLOG_DBG("obtaining netdev stats via rtnetlink");
903 VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
904 "via proc (you are probably running a pre-2.6.19 "
905 "kernel)", strerror(error));
911 /* Brings the 'is_internal' and 'is_tap' members of 'netdev_dev' up-to-date. */
913 netdev_linux_update_is_pseudo(struct netdev_dev_linux *netdev_dev)
915 if (!(netdev_dev->cache_valid & VALID_IS_PSEUDO)) {
916 const char *name = netdev_dev_get_name(&netdev_dev->netdev_dev);
917 const char *type = netdev_dev_get_type(&netdev_dev->netdev_dev);
919 netdev_dev->is_tap = !strcmp(type, "tap");
920 netdev_dev->is_internal = false;
921 if (!netdev_dev->is_tap) {
922 struct ethtool_drvinfo drvinfo;
925 memset(&drvinfo, 0, sizeof drvinfo);
926 error = netdev_linux_do_ethtool(name,
927 (struct ethtool_cmd *)&drvinfo,
931 if (!error && !strcmp(drvinfo.driver, "openvswitch")) {
932 netdev_dev->is_internal = true;
936 netdev_dev->cache_valid |= VALID_IS_PSEUDO;
940 /* Retrieves current device stats for 'netdev'.
942 * XXX All of the members of struct netdev_stats are 64 bits wide, but on
943 * 32-bit architectures the Linux network stats are only 32 bits. */
945 netdev_linux_get_stats(const struct netdev *netdev_,
946 struct netdev_stats *stats)
948 struct netdev_dev_linux *netdev_dev =
949 netdev_dev_linux_cast(netdev_get_dev(netdev_));
950 static int use_netlink_stats = -1;
952 struct netdev_stats raw_stats;
953 struct netdev_stats *collect_stats = stats;
955 COVERAGE_INC(netdev_get_stats);
957 netdev_linux_update_is_pseudo(netdev_dev);
958 if (netdev_dev->is_internal) {
959 collect_stats = &raw_stats;
962 if (use_netlink_stats < 0) {
963 use_netlink_stats = check_for_working_netlink_stats();
965 if (use_netlink_stats) {
968 error = get_ifindex(netdev_, &ifindex);
970 error = get_stats_via_netlink(ifindex, collect_stats);
973 error = get_stats_via_proc(netdev_get_name(netdev_), collect_stats);
976 /* If this port is an internal port then the transmit and receive stats
977 * will appear to be swapped relative to the other ports since we are the
978 * one sending the data, not a remote computer. For consistency, we swap
980 if (!error && (netdev_dev->is_internal || netdev_dev->is_tap)) {
981 stats->rx_packets = raw_stats.tx_packets;
982 stats->tx_packets = raw_stats.rx_packets;
983 stats->rx_bytes = raw_stats.tx_bytes;
984 stats->tx_bytes = raw_stats.rx_bytes;
985 stats->rx_errors = raw_stats.tx_errors;
986 stats->tx_errors = raw_stats.rx_errors;
987 stats->rx_dropped = raw_stats.tx_dropped;
988 stats->tx_dropped = raw_stats.rx_dropped;
989 stats->multicast = raw_stats.multicast;
990 stats->collisions = raw_stats.collisions;
991 stats->rx_length_errors = 0;
992 stats->rx_over_errors = 0;
993 stats->rx_crc_errors = 0;
994 stats->rx_frame_errors = 0;
995 stats->rx_fifo_errors = 0;
996 stats->rx_missed_errors = 0;
997 stats->tx_aborted_errors = 0;
998 stats->tx_carrier_errors = 0;
999 stats->tx_fifo_errors = 0;
1000 stats->tx_heartbeat_errors = 0;
1001 stats->tx_window_errors = 0;
1008 netdev_linux_set_stats(struct netdev *netdev,
1009 const struct netdev_stats *stats)
1011 struct netdev_dev_linux *netdev_dev =
1012 netdev_dev_linux_cast(netdev_get_dev(netdev));
1013 struct internal_dev_stats dp_dev_stats;
1016 /* We must reject this call if 'netdev' is not an Open vSwitch internal
1017 * port, because the ioctl that we are about to execute is in the "device
1018 * private ioctls" range, which means that executing it on a device that
1019 * is not the type we expect could do any random thing.
1021 * (Amusingly, these ioctl numbers are commented "THESE IOCTLS ARE
1022 * _DEPRECATED_ AND WILL DISAPPEAR IN 2.5.X" in linux/sockios.h. I guess
1023 * DaveM is a little behind on that.) */
1024 netdev_linux_update_is_pseudo(netdev_dev);
1025 if (!netdev_dev->is_internal) {
1029 /* This actually only sets the *offset* that the dp_dev applies, but in our
1030 * usage for fake bond devices the dp_dev never has any traffic of it own
1031 * so it has the same effect. */
1032 dp_dev_stats.rx_packets = stats->rx_packets;
1033 dp_dev_stats.rx_bytes = stats->rx_bytes;
1034 dp_dev_stats.tx_packets = stats->tx_packets;
1035 dp_dev_stats.tx_bytes = stats->tx_bytes;
1036 ifr.ifr_data = (void *) &dp_dev_stats;
1037 return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr,
1038 INTERNAL_DEV_SET_STATS,
1039 "INTERNAL_DEV_SET_STATS");
1042 /* Stores the features supported by 'netdev' into each of '*current',
1043 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1044 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1045 * successful, otherwise a positive errno value. */
1047 netdev_linux_get_features(struct netdev *netdev,
1048 uint32_t *current, uint32_t *advertised,
1049 uint32_t *supported, uint32_t *peer)
1051 struct ethtool_cmd ecmd;
1054 memset(&ecmd, 0, sizeof ecmd);
1055 error = netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd,
1056 ETHTOOL_GSET, "ETHTOOL_GSET");
1061 /* Supported features. */
1063 if (ecmd.supported & SUPPORTED_10baseT_Half) {
1064 *supported |= OFPPF_10MB_HD;
1066 if (ecmd.supported & SUPPORTED_10baseT_Full) {
1067 *supported |= OFPPF_10MB_FD;
1069 if (ecmd.supported & SUPPORTED_100baseT_Half) {
1070 *supported |= OFPPF_100MB_HD;
1072 if (ecmd.supported & SUPPORTED_100baseT_Full) {
1073 *supported |= OFPPF_100MB_FD;
1075 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
1076 *supported |= OFPPF_1GB_HD;
1078 if (ecmd.supported & SUPPORTED_1000baseT_Full) {
1079 *supported |= OFPPF_1GB_FD;
1081 if (ecmd.supported & SUPPORTED_10000baseT_Full) {
1082 *supported |= OFPPF_10GB_FD;
1084 if (ecmd.supported & SUPPORTED_TP) {
1085 *supported |= OFPPF_COPPER;
1087 if (ecmd.supported & SUPPORTED_FIBRE) {
1088 *supported |= OFPPF_FIBER;
1090 if (ecmd.supported & SUPPORTED_Autoneg) {
1091 *supported |= OFPPF_AUTONEG;
1093 if (ecmd.supported & SUPPORTED_Pause) {
1094 *supported |= OFPPF_PAUSE;
1096 if (ecmd.supported & SUPPORTED_Asym_Pause) {
1097 *supported |= OFPPF_PAUSE_ASYM;
1100 /* Advertised features. */
1102 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
1103 *advertised |= OFPPF_10MB_HD;
1105 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
1106 *advertised |= OFPPF_10MB_FD;
1108 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
1109 *advertised |= OFPPF_100MB_HD;
1111 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
1112 *advertised |= OFPPF_100MB_FD;
1114 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
1115 *advertised |= OFPPF_1GB_HD;
1117 if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
1118 *advertised |= OFPPF_1GB_FD;
1120 if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
1121 *advertised |= OFPPF_10GB_FD;
1123 if (ecmd.advertising & ADVERTISED_TP) {
1124 *advertised |= OFPPF_COPPER;
1126 if (ecmd.advertising & ADVERTISED_FIBRE) {
1127 *advertised |= OFPPF_FIBER;
1129 if (ecmd.advertising & ADVERTISED_Autoneg) {
1130 *advertised |= OFPPF_AUTONEG;
1132 if (ecmd.advertising & ADVERTISED_Pause) {
1133 *advertised |= OFPPF_PAUSE;
1135 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
1136 *advertised |= OFPPF_PAUSE_ASYM;
1139 /* Current settings. */
1140 if (ecmd.speed == SPEED_10) {
1141 *current = ecmd.duplex ? OFPPF_10MB_FD : OFPPF_10MB_HD;
1142 } else if (ecmd.speed == SPEED_100) {
1143 *current = ecmd.duplex ? OFPPF_100MB_FD : OFPPF_100MB_HD;
1144 } else if (ecmd.speed == SPEED_1000) {
1145 *current = ecmd.duplex ? OFPPF_1GB_FD : OFPPF_1GB_HD;
1146 } else if (ecmd.speed == SPEED_10000) {
1147 *current = OFPPF_10GB_FD;
1152 if (ecmd.port == PORT_TP) {
1153 *current |= OFPPF_COPPER;
1154 } else if (ecmd.port == PORT_FIBRE) {
1155 *current |= OFPPF_FIBER;
1159 *current |= OFPPF_AUTONEG;
1162 /* Peer advertisements. */
1163 *peer = 0; /* XXX */
1168 /* Set the features advertised by 'netdev' to 'advertise'. */
1170 netdev_linux_set_advertisements(struct netdev *netdev, uint32_t advertise)
1172 struct ethtool_cmd ecmd;
1175 memset(&ecmd, 0, sizeof ecmd);
1176 error = netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd,
1177 ETHTOOL_GSET, "ETHTOOL_GSET");
1182 ecmd.advertising = 0;
1183 if (advertise & OFPPF_10MB_HD) {
1184 ecmd.advertising |= ADVERTISED_10baseT_Half;
1186 if (advertise & OFPPF_10MB_FD) {
1187 ecmd.advertising |= ADVERTISED_10baseT_Full;
1189 if (advertise & OFPPF_100MB_HD) {
1190 ecmd.advertising |= ADVERTISED_100baseT_Half;
1192 if (advertise & OFPPF_100MB_FD) {
1193 ecmd.advertising |= ADVERTISED_100baseT_Full;
1195 if (advertise & OFPPF_1GB_HD) {
1196 ecmd.advertising |= ADVERTISED_1000baseT_Half;
1198 if (advertise & OFPPF_1GB_FD) {
1199 ecmd.advertising |= ADVERTISED_1000baseT_Full;
1201 if (advertise & OFPPF_10GB_FD) {
1202 ecmd.advertising |= ADVERTISED_10000baseT_Full;
1204 if (advertise & OFPPF_COPPER) {
1205 ecmd.advertising |= ADVERTISED_TP;
1207 if (advertise & OFPPF_FIBER) {
1208 ecmd.advertising |= ADVERTISED_FIBRE;
1210 if (advertise & OFPPF_AUTONEG) {
1211 ecmd.advertising |= ADVERTISED_Autoneg;
1213 if (advertise & OFPPF_PAUSE) {
1214 ecmd.advertising |= ADVERTISED_Pause;
1216 if (advertise & OFPPF_PAUSE_ASYM) {
1217 ecmd.advertising |= ADVERTISED_Asym_Pause;
1219 return netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd,
1220 ETHTOOL_SSET, "ETHTOOL_SSET");
1223 /* If 'netdev_name' is the name of a VLAN network device (e.g. one created with
1224 * vconfig(8)), sets '*vlan_vid' to the VLAN VID associated with that device
1225 * and returns 0. Otherwise returns a errno value (specifically ENOENT if
1226 * 'netdev_name' is the name of a network device that is not a VLAN device) and
1227 * sets '*vlan_vid' to -1. */
1229 netdev_linux_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
1231 const char *netdev_name = netdev_get_name(netdev);
1232 struct ds line = DS_EMPTY_INITIALIZER;
1233 FILE *stream = NULL;
1237 COVERAGE_INC(netdev_get_vlan_vid);
1238 fn = xasprintf("/proc/net/vlan/%s", netdev_name);
1239 stream = fopen(fn, "r");
1245 if (ds_get_line(&line, stream)) {
1246 if (ferror(stream)) {
1248 VLOG_ERR_RL(&rl, "error reading \"%s\": %s", fn, strerror(errno));
1251 VLOG_ERR_RL(&rl, "unexpected end of file reading \"%s\"", fn);
1256 if (!sscanf(ds_cstr(&line), "%*s VID: %d", vlan_vid)) {
1258 VLOG_ERR_RL(&rl, "parse error reading \"%s\" line 1: \"%s\"",
1259 fn, ds_cstr(&line));
1277 #define POLICE_ADD_CMD "/sbin/tc qdisc add dev %s handle ffff: ingress"
1278 #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"
1279 /* We redirect stderr to /dev/null because we often want to remove all
1280 * traffic control configuration on a port so its in a known state. If
1281 * this done when there is no such configuration, tc complains, so we just
1284 #define POLICE_DEL_CMD "/sbin/tc qdisc del dev %s handle ffff: ingress 2>/dev/null"
1286 /* Remove ingress policing from 'netdev'. Returns 0 if successful, otherwise a
1287 * positive errno value. */
1289 netdev_linux_remove_policing(struct netdev *netdev)
1291 struct netdev_dev_linux *netdev_dev =
1292 netdev_dev_linux_cast(netdev_get_dev(netdev));
1293 const char *netdev_name = netdev_get_name(netdev);
1296 /* xxx This should be more careful about only adding if it
1297 * xxx actually exists, as opposed to always deleting it. */
1298 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
1299 if (system(command) == -1) {
1300 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
1303 netdev_dev->kbits_rate = 0;
1304 netdev_dev->kbits_burst = 0;
1305 netdev_dev->cache_valid |= VALID_POLICING;
1309 /* Attempts to set input rate limiting (policing) policy. */
1311 netdev_linux_set_policing(struct netdev *netdev,
1312 uint32_t kbits_rate, uint32_t kbits_burst)
1314 struct netdev_dev_linux *netdev_dev =
1315 netdev_dev_linux_cast(netdev_get_dev(netdev));
1316 const char *netdev_name = netdev_get_name(netdev);
1319 COVERAGE_INC(netdev_set_policing);
1321 kbits_burst = (!kbits_rate ? 0 /* Force to 0 if no rate specified. */
1322 : !kbits_burst ? 1000 /* Default to 1000 kbits if 0. */
1323 : kbits_burst); /* Stick with user-specified value. */
1325 if (netdev_dev->cache_valid & VALID_POLICING
1326 && netdev_dev->kbits_rate == kbits_rate
1327 && netdev_dev->kbits_burst == kbits_burst) {
1328 /* Assume that settings haven't changed since we last set them. */
1332 netdev_linux_remove_policing(netdev);
1334 snprintf(command, sizeof(command), POLICE_ADD_CMD, netdev_name);
1335 if (system(command) != 0) {
1336 VLOG_WARN_RL(&rl, "%s: problem adding policing", netdev_name);
1340 snprintf(command, sizeof(command), POLICE_CONFIG_CMD, netdev_name,
1341 kbits_rate, kbits_burst);
1342 if (system(command) != 0) {
1343 VLOG_WARN_RL(&rl, "%s: problem configuring policing",
1348 netdev_dev->kbits_rate = kbits_rate;
1349 netdev_dev->kbits_burst = kbits_burst;
1350 netdev_dev->cache_valid |= VALID_POLICING;
1357 netdev_linux_get_in4(const struct netdev *netdev_,
1358 struct in_addr *address, struct in_addr *netmask)
1360 struct netdev_dev_linux *netdev_dev =
1361 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1363 if (!(netdev_dev->cache_valid & VALID_IN4)) {
1366 error = netdev_linux_get_ipv4(netdev_, &netdev_dev->address,
1367 SIOCGIFADDR, "SIOCGIFADDR");
1372 error = netdev_linux_get_ipv4(netdev_, &netdev_dev->netmask,
1373 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1378 netdev_dev->cache_valid |= VALID_IN4;
1380 *address = netdev_dev->address;
1381 *netmask = netdev_dev->netmask;
1382 return address->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1386 netdev_linux_set_in4(struct netdev *netdev_, struct in_addr address,
1387 struct in_addr netmask)
1389 struct netdev_dev_linux *netdev_dev =
1390 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1393 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", address);
1395 netdev_dev->cache_valid |= VALID_IN4;
1396 netdev_dev->address = address;
1397 netdev_dev->netmask = netmask;
1398 if (address.s_addr != INADDR_ANY) {
1399 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1400 "SIOCSIFNETMASK", netmask);
1407 parse_if_inet6_line(const char *line,
1408 struct in6_addr *in6, char ifname[16 + 1])
1410 uint8_t *s6 = in6->s6_addr;
1411 #define X8 "%2"SCNx8
1413 " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
1414 "%*x %*x %*x %*x %16s\n",
1415 &s6[0], &s6[1], &s6[2], &s6[3],
1416 &s6[4], &s6[5], &s6[6], &s6[7],
1417 &s6[8], &s6[9], &s6[10], &s6[11],
1418 &s6[12], &s6[13], &s6[14], &s6[15],
1422 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
1423 * 'in6' is non-null) and returns true. Otherwise, returns false. */
1425 netdev_linux_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1427 struct netdev_dev_linux *netdev_dev =
1428 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1429 if (!(netdev_dev->cache_valid & VALID_IN6)) {
1433 netdev_dev->in6 = in6addr_any;
1435 file = fopen("/proc/net/if_inet6", "r");
1437 const char *name = netdev_get_name(netdev_);
1438 while (fgets(line, sizeof line, file)) {
1439 struct in6_addr in6;
1440 char ifname[16 + 1];
1441 if (parse_if_inet6_line(line, &in6, ifname)
1442 && !strcmp(name, ifname))
1444 netdev_dev->in6 = in6;
1450 netdev_dev->cache_valid |= VALID_IN6;
1452 *in6 = netdev_dev->in6;
1457 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1459 struct sockaddr_in sin;
1460 memset(&sin, 0, sizeof sin);
1461 sin.sin_family = AF_INET;
1462 sin.sin_addr = addr;
1465 memset(sa, 0, sizeof *sa);
1466 memcpy(sa, &sin, sizeof sin);
1470 do_set_addr(struct netdev *netdev,
1471 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1474 strncpy(ifr.ifr_name, netdev_get_name(netdev), sizeof ifr.ifr_name);
1475 make_in4_sockaddr(&ifr.ifr_addr, addr);
1477 return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, ioctl_nr,
1481 /* Adds 'router' as a default IP gateway. */
1483 netdev_linux_add_router(struct netdev *netdev OVS_UNUSED, struct in_addr router)
1485 struct in_addr any = { INADDR_ANY };
1489 memset(&rt, 0, sizeof rt);
1490 make_in4_sockaddr(&rt.rt_dst, any);
1491 make_in4_sockaddr(&rt.rt_gateway, router);
1492 make_in4_sockaddr(&rt.rt_genmask, any);
1493 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1494 COVERAGE_INC(netdev_add_router);
1495 error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
1497 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
1503 netdev_linux_get_next_hop(const struct in_addr *host, struct in_addr *next_hop,
1506 static const char fn[] = "/proc/net/route";
1511 *netdev_name = NULL;
1512 stream = fopen(fn, "r");
1513 if (stream == NULL) {
1514 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1519 while (fgets(line, sizeof line, stream)) {
1522 uint32_t dest, gateway, mask;
1523 int refcnt, metric, mtu;
1524 unsigned int flags, use, window, irtt;
1527 "%16s %"SCNx32" %"SCNx32" %04X %d %u %d %"SCNx32
1529 iface, &dest, &gateway, &flags, &refcnt,
1530 &use, &metric, &mask, &mtu, &window, &irtt) != 11) {
1532 VLOG_WARN_RL(&rl, "%s: could not parse line %d: %s",
1536 if (!(flags & RTF_UP)) {
1537 /* Skip routes that aren't up. */
1541 /* The output of 'dest', 'mask', and 'gateway' were given in
1542 * network byte order, so we don't need need any endian
1543 * conversions here. */
1544 if ((dest & mask) == (host->s_addr & mask)) {
1546 /* The host is directly reachable. */
1547 next_hop->s_addr = 0;
1549 /* To reach the host, we must go through a gateway. */
1550 next_hop->s_addr = gateway;
1552 *netdev_name = xstrdup(iface);
1563 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1564 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1565 * returns 0. Otherwise, it returns a positive errno value; in particular,
1566 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
1568 netdev_linux_arp_lookup(const struct netdev *netdev,
1569 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
1572 struct sockaddr_in sin;
1575 memset(&r, 0, sizeof r);
1576 sin.sin_family = AF_INET;
1577 sin.sin_addr.s_addr = ip;
1579 memcpy(&r.arp_pa, &sin, sizeof sin);
1580 r.arp_ha.sa_family = ARPHRD_ETHER;
1582 strncpy(r.arp_dev, netdev_get_name(netdev), sizeof r.arp_dev);
1583 COVERAGE_INC(netdev_arp_lookup);
1584 retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
1586 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
1587 } else if (retval != ENXIO) {
1588 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
1589 netdev_get_name(netdev), IP_ARGS(&ip), strerror(retval));
1595 nd_to_iff_flags(enum netdev_flags nd)
1598 if (nd & NETDEV_UP) {
1601 if (nd & NETDEV_PROMISC) {
1608 iff_to_nd_flags(int iff)
1610 enum netdev_flags nd = 0;
1614 if (iff & IFF_PROMISC) {
1615 nd |= NETDEV_PROMISC;
1621 netdev_linux_update_flags(struct netdev *netdev, enum netdev_flags off,
1622 enum netdev_flags on, enum netdev_flags *old_flagsp)
1624 int old_flags, new_flags;
1627 error = get_flags(netdev, &old_flags);
1629 *old_flagsp = iff_to_nd_flags(old_flags);
1630 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1631 if (new_flags != old_flags) {
1632 error = set_flags(netdev, new_flags);
1639 poll_notify(struct list *list)
1641 struct netdev_linux_notifier *notifier;
1642 LIST_FOR_EACH (notifier, struct netdev_linux_notifier, node, list) {
1643 struct netdev_notifier *n = ¬ifier->notifier;
1649 netdev_linux_poll_cb(const struct rtnetlink_change *change,
1650 void *aux OVS_UNUSED)
1653 struct list *list = shash_find_data(&netdev_linux_notifiers,
1659 struct shash_node *node;
1660 SHASH_FOR_EACH (node, &netdev_linux_notifiers) {
1661 poll_notify(node->data);
1667 netdev_linux_poll_add(struct netdev *netdev,
1668 void (*cb)(struct netdev_notifier *), void *aux,
1669 struct netdev_notifier **notifierp)
1671 const char *netdev_name = netdev_get_name(netdev);
1672 struct netdev_linux_notifier *notifier;
1675 if (shash_is_empty(&netdev_linux_notifiers)) {
1676 int error = rtnetlink_notifier_register(&netdev_linux_poll_notifier,
1677 netdev_linux_poll_cb, NULL);
1683 list = shash_find_data(&netdev_linux_notifiers, netdev_name);
1685 list = xmalloc(sizeof *list);
1687 shash_add(&netdev_linux_notifiers, netdev_name, list);
1690 notifier = xmalloc(sizeof *notifier);
1691 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
1692 list_push_back(list, ¬ifier->node);
1693 *notifierp = ¬ifier->notifier;
1698 netdev_linux_poll_remove(struct netdev_notifier *notifier_)
1700 struct netdev_linux_notifier *notifier =
1701 CONTAINER_OF(notifier_, struct netdev_linux_notifier, notifier);
1704 /* Remove 'notifier' from its list. */
1705 list = list_remove(¬ifier->node);
1706 if (list_is_empty(list)) {
1707 /* The list is now empty. Remove it from the hash and free it. */
1708 const char *netdev_name = netdev_get_name(notifier->notifier.netdev);
1709 shash_delete(&netdev_linux_notifiers,
1710 shash_find(&netdev_linux_notifiers, netdev_name));
1715 /* If that was the last notifier, unregister. */
1716 if (shash_is_empty(&netdev_linux_notifiers)) {
1717 rtnetlink_notifier_unregister(&netdev_linux_poll_notifier);
1721 const struct netdev_class netdev_linux_class = {
1728 netdev_linux_create_system,
1729 netdev_linux_destroy,
1730 NULL, /* reconfigure */
1735 netdev_linux_enumerate,
1738 netdev_linux_recv_wait,
1742 netdev_linux_send_wait,
1744 netdev_linux_set_etheraddr,
1745 netdev_linux_get_etheraddr,
1746 netdev_linux_get_mtu,
1747 netdev_linux_get_ifindex,
1748 netdev_linux_get_carrier,
1749 netdev_linux_get_stats,
1750 netdev_linux_set_stats,
1752 netdev_linux_get_features,
1753 netdev_linux_set_advertisements,
1754 netdev_linux_get_vlan_vid,
1755 netdev_linux_set_policing,
1757 netdev_linux_get_in4,
1758 netdev_linux_set_in4,
1759 netdev_linux_get_in6,
1760 netdev_linux_add_router,
1761 netdev_linux_get_next_hop,
1762 netdev_linux_arp_lookup,
1764 netdev_linux_update_flags,
1766 netdev_linux_poll_add,
1767 netdev_linux_poll_remove,
1770 const struct netdev_class netdev_tap_class = {
1777 netdev_linux_create_tap,
1778 netdev_linux_destroy,
1779 NULL, /* reconfigure */
1784 NULL, /* enumerate */
1787 netdev_linux_recv_wait,
1791 netdev_linux_send_wait,
1793 netdev_linux_set_etheraddr,
1794 netdev_linux_get_etheraddr,
1795 netdev_linux_get_mtu,
1796 netdev_linux_get_ifindex,
1797 netdev_linux_get_carrier,
1798 netdev_linux_get_stats,
1799 NULL, /* set_stats */
1801 netdev_linux_get_features,
1802 netdev_linux_set_advertisements,
1803 netdev_linux_get_vlan_vid,
1804 netdev_linux_set_policing,
1806 netdev_linux_get_in4,
1807 netdev_linux_set_in4,
1808 netdev_linux_get_in6,
1809 netdev_linux_add_router,
1810 netdev_linux_get_next_hop,
1811 netdev_linux_arp_lookup,
1813 netdev_linux_update_flags,
1815 netdev_linux_poll_add,
1816 netdev_linux_poll_remove,
1819 const struct netdev_class netdev_patch_class = {
1826 netdev_linux_create_patch,
1827 netdev_linux_destroy,
1828 NULL, /* reconfigure */
1833 NULL, /* enumerate */
1836 netdev_linux_recv_wait,
1840 netdev_linux_send_wait,
1842 netdev_linux_set_etheraddr,
1843 netdev_linux_get_etheraddr,
1844 netdev_linux_get_mtu,
1845 netdev_linux_get_ifindex,
1846 netdev_linux_get_carrier,
1847 netdev_linux_get_stats,
1848 NULL, /* set_stats */
1850 netdev_linux_get_features,
1851 netdev_linux_set_advertisements,
1852 netdev_linux_get_vlan_vid,
1853 netdev_linux_set_policing,
1855 netdev_linux_get_in4,
1856 netdev_linux_set_in4,
1857 netdev_linux_get_in6,
1858 netdev_linux_add_router,
1859 netdev_linux_get_next_hop,
1860 netdev_linux_arp_lookup,
1862 netdev_linux_update_flags,
1864 netdev_linux_poll_add,
1865 netdev_linux_poll_remove,
1870 get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
1872 /* Policy for RTNLGRP_LINK messages.
1874 * There are *many* more fields in these messages, but currently we only
1875 * care about these fields. */
1876 static const struct nl_policy rtnlgrp_link_policy[] = {
1877 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
1878 [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
1879 .min_len = sizeof(struct rtnl_link_stats) },
1882 struct nl_sock *rtnl_sock;
1883 struct ofpbuf request;
1884 struct ofpbuf *reply;
1885 struct ifinfomsg *ifi;
1886 const struct rtnl_link_stats *rtnl_stats;
1887 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1890 error = get_rtnl_sock(&rtnl_sock);
1895 ofpbuf_init(&request, 0);
1896 nl_msg_put_nlmsghdr(&request, rtnl_sock, sizeof *ifi,
1897 RTM_GETLINK, NLM_F_REQUEST);
1898 ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
1899 ifi->ifi_family = PF_UNSPEC;
1900 ifi->ifi_index = ifindex;
1901 error = nl_sock_transact(rtnl_sock, &request, &reply);
1902 ofpbuf_uninit(&request);
1907 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1908 rtnlgrp_link_policy,
1909 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1910 ofpbuf_delete(reply);
1914 if (!attrs[IFLA_STATS]) {
1915 VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
1916 ofpbuf_delete(reply);
1920 rtnl_stats = nl_attr_get(attrs[IFLA_STATS]);
1921 stats->rx_packets = rtnl_stats->rx_packets;
1922 stats->tx_packets = rtnl_stats->tx_packets;
1923 stats->rx_bytes = rtnl_stats->rx_bytes;
1924 stats->tx_bytes = rtnl_stats->tx_bytes;
1925 stats->rx_errors = rtnl_stats->rx_errors;
1926 stats->tx_errors = rtnl_stats->tx_errors;
1927 stats->rx_dropped = rtnl_stats->rx_dropped;
1928 stats->tx_dropped = rtnl_stats->tx_dropped;
1929 stats->multicast = rtnl_stats->multicast;
1930 stats->collisions = rtnl_stats->collisions;
1931 stats->rx_length_errors = rtnl_stats->rx_length_errors;
1932 stats->rx_over_errors = rtnl_stats->rx_over_errors;
1933 stats->rx_crc_errors = rtnl_stats->rx_crc_errors;
1934 stats->rx_frame_errors = rtnl_stats->rx_frame_errors;
1935 stats->rx_fifo_errors = rtnl_stats->rx_fifo_errors;
1936 stats->rx_missed_errors = rtnl_stats->rx_missed_errors;
1937 stats->tx_aborted_errors = rtnl_stats->tx_aborted_errors;
1938 stats->tx_carrier_errors = rtnl_stats->tx_carrier_errors;
1939 stats->tx_fifo_errors = rtnl_stats->tx_fifo_errors;
1940 stats->tx_heartbeat_errors = rtnl_stats->tx_heartbeat_errors;
1941 stats->tx_window_errors = rtnl_stats->tx_window_errors;
1943 ofpbuf_delete(reply);
1949 get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats)
1951 static const char fn[] = "/proc/net/dev";
1956 stream = fopen(fn, "r");
1958 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1963 while (fgets(line, sizeof line, stream)) {
1966 #define X64 "%"SCNu64
1969 X64 X64 X64 X64 X64 X64 X64 "%*u"
1970 X64 X64 X64 X64 X64 X64 X64 "%*u",
1976 &stats->rx_fifo_errors,
1977 &stats->rx_frame_errors,
1983 &stats->tx_fifo_errors,
1985 &stats->tx_carrier_errors) != 15) {
1986 VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln);
1987 } else if (!strcmp(devname, netdev_name)) {
1988 stats->rx_length_errors = UINT64_MAX;
1989 stats->rx_over_errors = UINT64_MAX;
1990 stats->rx_crc_errors = UINT64_MAX;
1991 stats->rx_missed_errors = UINT64_MAX;
1992 stats->tx_aborted_errors = UINT64_MAX;
1993 stats->tx_heartbeat_errors = UINT64_MAX;
1994 stats->tx_window_errors = UINT64_MAX;
2000 VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name);
2006 get_flags(const struct netdev *netdev, int *flags)
2011 error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, SIOCGIFFLAGS,
2013 *flags = ifr.ifr_flags;
2018 set_flags(struct netdev *netdev, int flags)
2022 ifr.ifr_flags = flags;
2023 return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, SIOCSIFFLAGS,
2028 do_get_ifindex(const char *netdev_name)
2032 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
2033 COVERAGE_INC(netdev_get_ifindex);
2034 if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) {
2035 VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
2036 netdev_name, strerror(errno));
2039 return ifr.ifr_ifindex;
2043 get_ifindex(const struct netdev *netdev_, int *ifindexp)
2045 struct netdev_dev_linux *netdev_dev =
2046 netdev_dev_linux_cast(netdev_get_dev(netdev_));
2048 if (!(netdev_dev->cache_valid & VALID_IFINDEX)) {
2049 int ifindex = do_get_ifindex(netdev_get_name(netdev_));
2053 netdev_dev->cache_valid |= VALID_IFINDEX;
2054 netdev_dev->ifindex = ifindex;
2056 *ifindexp = netdev_dev->ifindex;
2061 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
2066 memset(&ifr, 0, sizeof ifr);
2067 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
2068 COVERAGE_INC(netdev_get_hwaddr);
2069 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
2070 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
2071 netdev_name, strerror(errno));
2074 hwaddr_family = ifr.ifr_hwaddr.sa_family;
2075 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
2076 VLOG_WARN("%s device has unknown hardware address family %d",
2077 netdev_name, hwaddr_family);
2079 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
2084 set_etheraddr(const char *netdev_name, int hwaddr_family,
2085 const uint8_t mac[ETH_ADDR_LEN])
2089 memset(&ifr, 0, sizeof ifr);
2090 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
2091 ifr.ifr_hwaddr.sa_family = hwaddr_family;
2092 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
2093 COVERAGE_INC(netdev_set_hwaddr);
2094 if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) {
2095 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
2096 netdev_name, strerror(errno));
2103 netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *ecmd,
2104 int cmd, const char *cmd_name)
2108 memset(&ifr, 0, sizeof ifr);
2109 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
2110 ifr.ifr_data = (caddr_t) ecmd;
2113 COVERAGE_INC(netdev_ethtool);
2114 if (ioctl(af_inet_sock, SIOCETHTOOL, &ifr) == 0) {
2117 if (errno != EOPNOTSUPP) {
2118 VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
2119 "failed: %s", cmd_name, name, strerror(errno));
2121 /* The device doesn't support this operation. That's pretty
2122 * common, so there's no point in logging anything. */
2129 netdev_linux_do_ioctl(const char *name, struct ifreq *ifr, int cmd,
2130 const char *cmd_name)
2132 strncpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
2133 if (ioctl(af_inet_sock, cmd, ifr) == -1) {
2134 VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
2142 netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip,
2143 int cmd, const char *cmd_name)
2148 ifr.ifr_addr.sa_family = AF_INET;
2149 error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name);
2151 const struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
2152 *ip = sin->sin_addr;
2157 /* Obtains a Netlink routing socket that is not subscribed to any multicast
2158 * groups. Returns 0 if successful, otherwise a positive errno value. Stores
2159 * the socket in '*rtnl_sockp' if successful, otherwise a null pointer. */
2161 get_rtnl_sock(struct nl_sock **rtnl_sockp)
2163 static struct nl_sock *sock;
2167 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0, &sock);
2169 VLOG_ERR_RL(&rl, "failed to create rtnetlink socket: %s",