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>
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/gre.h"
55 #include "poll-loop.h"
56 #include "rtnetlink.h"
57 #include "socket-util.h"
61 #ifndef GRE_IOCTL_ONLY
62 #include <linux/if_link.h>
65 #define THIS_MODULE VLM_netdev_linux
68 /* These were introduced in Linux 2.6.14, so they might be missing if we have
70 #ifndef ADVERTISED_Pause
71 #define ADVERTISED_Pause (1 << 13)
73 #ifndef ADVERTISED_Asym_Pause
74 #define ADVERTISED_Asym_Pause (1 << 14)
78 int tap_fd; /* File descriptor for TAP device. */
81 /* Provider-specific netdev object. Netdev objects are devices that are
82 * created by the netdev library through a netdev_create() call. */
83 struct netdev_obj_linux {
84 struct netdev_obj netdev_obj;
93 /* File descriptors. For ordinary network devices, the two fds below are
94 * the same; for tap devices, they differ. */
95 int netdev_fd; /* Network device. */
96 int tap_fd; /* TAP character device, if any, otherwise the
99 struct netdev_linux_cache *cache;
103 VALID_IFINDEX = 1 << 0,
104 VALID_ETHERADDR = 1 << 1,
108 VALID_CARRIER = 1 << 5,
109 VALID_IS_INTERNAL = 1 << 6
112 /* Cached network device information. */
113 struct netdev_linux_cache {
114 struct shash_node *shash_node;
119 uint8_t etheraddr[ETH_ADDR_LEN];
120 struct in_addr address, netmask;
127 static struct shash cache_map = SHASH_INITIALIZER(&cache_map);
128 static struct rtnetlink_notifier netdev_linux_cache_notifier;
130 /* An AF_INET socket (used for ioctl operations). */
131 static int af_inet_sock = -1;
146 struct nl_sock *nl_sock;
152 struct netdev_linux_notifier {
153 struct netdev_notifier notifier;
157 static struct shash netdev_linux_notifiers =
158 SHASH_INITIALIZER(&netdev_linux_notifiers);
159 static struct rtnetlink_notifier netdev_linux_poll_notifier;
161 /* This is set pretty low because we probably won't learn anything from the
162 * additional log messages. */
163 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
165 static int netdev_linux_do_ethtool(struct netdev *, struct ethtool_cmd *,
166 int cmd, const char *cmd_name);
167 static int netdev_linux_do_ioctl(const struct netdev *, struct ifreq *,
168 int cmd, const char *cmd_name);
169 static int netdev_linux_get_ipv4(const struct netdev *, struct in_addr *,
170 int cmd, const char *cmd_name);
171 static int get_flags(const struct netdev *, int *flagsp);
172 static int set_flags(struct netdev *, int flags);
173 static int do_get_ifindex(const char *netdev_name);
174 static int get_ifindex(const struct netdev *, int *ifindexp);
175 static int do_set_addr(struct netdev *netdev,
176 int ioctl_nr, const char *ioctl_name,
177 struct in_addr addr);
178 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
179 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
180 const uint8_t[ETH_ADDR_LEN]);
181 static int get_stats_via_netlink(int ifindex, struct netdev_stats *stats);
182 static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats);
184 static struct netdev_obj_linux *
185 netdev_obj_linux_cast(const struct netdev_obj *netdev_obj)
187 /* xxx Gross hack! Until we get the class 'type' issue resolved. */
189 netdev_obj_assert_class(netdev_obj, &netdev_linux_class);
191 return CONTAINER_OF(netdev_obj, struct netdev_obj_linux, netdev_obj);
194 static struct netdev_linux *
195 netdev_linux_cast(const struct netdev *netdev)
197 netdev_assert_class(netdev, &netdev_linux_class);
198 return CONTAINER_OF(netdev, struct netdev_linux, netdev);
202 netdev_linux_init(void)
204 static int status = -1;
206 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
207 status = af_inet_sock >= 0 ? 0 : errno;
209 VLOG_ERR("failed to create inet socket: %s", strerror(status));
216 netdev_linux_run(void)
218 rtnetlink_notifier_run();
222 netdev_linux_wait(void)
224 rtnetlink_notifier_wait();
228 netdev_linux_cache_cb(const struct rtnetlink_change *change,
231 struct netdev_linux_cache *cache;
233 cache = shash_find_data(&cache_map, change->ifname);
238 struct shash_node *node;
239 SHASH_FOR_EACH (node, &cache_map) {
246 /* The arguments are marked as unused to prevent warnings on platforms where
247 * the Netlink interface isn't supported. */
249 setup_gre_netlink(const char *name UNUSED, struct gre_config *config UNUSED,
252 #ifdef GRE_IOCTL_ONLY
256 struct ofpbuf request, *reply;
257 unsigned int nl_flags;
258 struct ifinfomsg ifinfomsg;
259 struct nlattr *linkinfo_hdr;
260 struct nlattr *info_data_hdr;
263 uint8_t pmtudisc = 0;
265 if (!gre_descriptors.nl_sock) {
266 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0,
267 &gre_descriptors.nl_sock);
269 VLOG_WARN("couldn't create netlink socket: %s\n", strerror(error));
270 gre_descriptors.nl_sock = NULL;
275 ofpbuf_init(&request, 0);
277 nl_flags = NLM_F_REQUEST;
279 nl_flags |= NLM_F_CREATE|NLM_F_EXCL;
282 /* We over-reserve space, because we do some pointer arithmetic
283 * and don't want the buffer address shifting under us. */
284 nl_msg_put_nlmsghdr(&request, gre_descriptors.nl_sock, 2048, RTM_NEWLINK,
287 memset(&ifinfomsg, 0, sizeof ifinfomsg);
288 ifinfomsg.ifi_family = AF_UNSPEC;
289 nl_msg_put(&request, &ifinfomsg, sizeof ifinfomsg);
291 linkinfo_hdr = ofpbuf_tail(&request);
292 nl_msg_put_unspec(&request, IFLA_LINKINFO, NULL, 0);
294 nl_msg_put_unspec(&request, IFLA_INFO_KIND, "gretap", 6);
296 info_data_hdr = ofpbuf_tail(&request);
297 nl_msg_put_unspec(&request, IFLA_INFO_DATA, NULL, 0);
300 if (config->have_in_key) {
303 if (config->have_out_key) {
307 if (config->in_csum) {
310 if (config->out_csum) {
315 nl_msg_put_u32(&request, IFLA_GRE_IKEY, config->in_key);
316 nl_msg_put_u32(&request, IFLA_GRE_OKEY, config->out_key);
317 nl_msg_put_u16(&request, IFLA_GRE_IFLAGS, iflags);
318 nl_msg_put_u16(&request, IFLA_GRE_OFLAGS, oflags);
319 nl_msg_put_u32(&request, IFLA_GRE_LOCAL, config->local_ip);
320 nl_msg_put_u32(&request, IFLA_GRE_REMOTE, config->remote_ip);
321 nl_msg_put_u8(&request, IFLA_GRE_PMTUDISC, pmtudisc);
322 nl_msg_put_u8(&request, IFLA_GRE_TTL, 0);
323 nl_msg_put_u8(&request, IFLA_GRE_TOS, 0);
325 info_data_hdr->nla_len = (char *)ofpbuf_tail(&request)
326 - (char *)info_data_hdr;
327 linkinfo_hdr->nla_len = (char *)ofpbuf_tail(&request)
328 - (char *)linkinfo_hdr;
330 nl_msg_put_string(&request, IFLA_IFNAME, name);
332 error = nl_sock_transact(gre_descriptors.nl_sock, &request, &reply);
333 ofpbuf_uninit(&request);
335 VLOG_WARN("couldn't transact netlink socket: %s\n", strerror(error));
338 ofpbuf_delete(reply);
346 setup_gre_ioctl(const char *name, struct gre_config *config, bool create)
348 struct ip_tunnel_parm p;
351 memset(&p, 0, sizeof p);
353 strncpy(p.name, name, IFNAMSIZ);
357 p.iph.protocol = IPPROTO_GRE;
358 p.iph.saddr = config->local_ip;
359 p.iph.daddr = config->remote_ip;
361 if (config->have_in_key) {
362 p.i_flags |= GRE_KEY;
363 p.i_key = config->in_key;
365 if (config->have_out_key) {
366 p.o_flags |= GRE_KEY;
367 p.o_key = config->out_key;
370 if (config->in_csum) {
371 p.i_flags |= GRE_CSUM;
373 if (config->out_csum) {
374 p.o_flags |= GRE_CSUM;
377 strncpy(ifr.ifr_name, create ? GRE_IOCTL_DEVICE : name, IFNAMSIZ);
378 ifr.ifr_ifru.ifru_data = (void *)&p;
380 if (!gre_descriptors.ioctl_fd) {
381 gre_descriptors.ioctl_fd = socket(AF_INET, SOCK_DGRAM, 0);
382 if (gre_descriptors.ioctl_fd < 0) {
383 VLOG_WARN("couldn't create gre ioctl socket: %s\n", strerror(errno));
384 gre_descriptors.ioctl_fd = 0;
389 if (ioctl(gre_descriptors.ioctl_fd, create ? SIOCADDGRETAP : SIOCCHGGRETAP,
391 VLOG_WARN("couldn't do gre ioctl: %s\n", strerror(errno));
399 setup_gre(const char *name, const struct shash *args, bool create)
402 struct in_addr in_addr;
403 struct shash_node *node;
404 struct gre_config config;
406 memset(&config, 0, sizeof config);
407 config.in_csum = true;
408 config.out_csum = true;
410 SHASH_FOR_EACH (node, args) {
411 if (!strcmp(node->name, "remote_ip")) {
412 if (lookup_ip(node->data, &in_addr)) {
413 VLOG_WARN("bad 'remote_ip' for gre device %s ", name);
415 config.remote_ip = in_addr.s_addr;
417 } else if (!strcmp(node->name, "local_ip")) {
418 if (lookup_ip(node->data, &in_addr)) {
419 VLOG_WARN("bad 'local_ip' for gre device %s ", name);
421 config.local_ip = in_addr.s_addr;
423 } else if (!strcmp(node->name, "key")) {
424 config.have_in_key = true;
425 config.have_out_key = true;
426 config.in_key = htonl(atoi(node->data));
427 config.out_key = htonl(atoi(node->data));
428 } else if (!strcmp(node->name, "in_key")) {
429 config.have_in_key = true;
430 config.in_key = htonl(atoi(node->data));
431 } else if (!strcmp(node->name, "out_key")) {
432 config.have_out_key = true;
433 config.out_key = htonl(atoi(node->data));
434 } else if (!strcmp(node->name, "csum")) {
435 if (!strcmp(node->data, "false")) {
436 config.in_csum = false;
437 config.out_csum = false;
440 VLOG_WARN("unknown gre argument '%s'", node->name);
444 if (!config.remote_ip) {
445 VLOG_WARN("gre type requires valid 'remote_ip' argument");
450 if (!gre_descriptors.use_ioctl) {
451 error = setup_gre_netlink(name, &config, create);
452 if (error == EOPNOTSUPP) {
453 gre_descriptors.use_ioctl = true;
456 if (gre_descriptors.use_ioctl) {
457 error = setup_gre_ioctl(name, &config, create);
465 if_up(const char *name)
469 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
470 ifr.ifr_flags = IFF_UP;
472 if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) {
473 VLOG_DBG_RL(&rl, "%s: failed to bring device up: %s",
474 name, strerror(errno));
481 /* Creates the netdev object of 'type' with 'name'. */
483 netdev_linux_create_system(const char *name, const char *type UNUSED,
484 const struct shash *args, bool created)
486 struct netdev_obj_linux *netdev_obj;
488 if (!shash_is_empty(args)) {
489 VLOG_WARN("arguments for system devices should be empty");
492 netdev_obj = xcalloc(1, sizeof *netdev_obj);
493 netdev_obj_init(&netdev_obj->netdev_obj, name, &netdev_linux_class,
500 netdev_linux_create_tap(const char *name, const char *type UNUSED,
501 const struct shash *args, bool created)
503 struct netdev_obj_linux *netdev_obj;
504 struct tap_state *state;
505 static const char tap_dev[] = "/dev/net/tun";
509 if (!shash_is_empty(args)) {
510 VLOG_WARN("arguments for TAP devices should be empty");
513 netdev_obj = xcalloc(1, sizeof *netdev_obj);
514 netdev_obj_init(&netdev_obj->netdev_obj, name, &netdev_tap_class, created);
516 state = &netdev_obj->state.tap;
518 /* Open tap device. */
519 state->tap_fd = open(tap_dev, O_RDWR);
520 if (state->tap_fd < 0) {
522 VLOG_WARN("opening \"%s\" failed: %s", tap_dev, strerror(error));
526 /* Create tap device. */
527 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
528 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
529 if (ioctl(state->tap_fd, TUNSETIFF, &ifr) == -1) {
530 VLOG_WARN("%s: creating tap device failed: %s", name,
536 /* Make non-blocking. */
537 error = set_nonblocking(state->tap_fd);
550 netdev_destroy(name);
555 netdev_linux_create_gre(const char *name, const char *type UNUSED,
556 const struct shash *args, bool created)
558 struct netdev_obj_linux *netdev_obj;
561 netdev_obj = xcalloc(1, sizeof *netdev_obj);
562 netdev_obj_init(&netdev_obj->netdev_obj, name, &netdev_gre_class, created);
564 error = setup_gre(name, args, true);
577 netdev_destroy(name);
582 netdev_linux_reconfigure_gre(struct netdev_obj *netdev_obj_,
583 const struct shash *args)
585 struct netdev_obj_linux *netdev_obj = netdev_obj_linux_cast(netdev_obj_);
586 const char *name = netdev_obj_get_name(&netdev_obj->netdev_obj);
588 return setup_gre(name, args, false);
591 /* The arguments are marked as unused to prevent warnings on platforms where
592 * the Netlink interface isn't supported. */
594 destroy_gre_netlink(struct netdev_obj_linux *netdev_obj UNUSED)
596 #ifdef GRE_IOCTL_ONLY
599 const char *name = netdev_obj_get_name(&netdev_obj->netdev_obj);
601 struct ofpbuf request, *reply;
602 struct ifinfomsg ifinfomsg;
605 ofpbuf_init(&request, 0);
607 nl_msg_put_nlmsghdr(&request, gre_descriptors.nl_sock, 0, RTM_DELLINK,
610 memset(&ifinfomsg, 0, sizeof ifinfomsg);
611 ifinfomsg.ifi_family = AF_UNSPEC;
612 nl_msg_put(&request, &ifinfomsg, sizeof ifinfomsg);
614 ifindex = do_get_ifindex(name);
615 nl_msg_put_u32(&request, IFLA_LINK, ifindex);
617 nl_msg_put_string(&request, IFLA_IFNAME, name);
619 error = nl_sock_transact(gre_descriptors.nl_sock, &request, &reply);
620 ofpbuf_uninit(&request);
622 VLOG_WARN("couldn't transact netlink socket: %s\n", strerror(error));
625 ofpbuf_delete(reply);
633 destroy_gre_ioctl(struct netdev_obj_linux *netdev_obj)
635 const char *name = netdev_obj_get_name(&netdev_obj->netdev_obj);
636 struct ip_tunnel_parm p;
639 memset(&p, 0, sizeof p);
640 strncpy(p.name, name, IFNAMSIZ);
642 strncpy(ifr.ifr_name, name, IFNAMSIZ);
643 ifr.ifr_ifru.ifru_data = (void *)&p;
645 if (ioctl(gre_descriptors.ioctl_fd, SIOCDELGRETAP, &ifr) < 0) {
646 VLOG_WARN("couldn't do gre ioctl: %s\n", strerror(errno));
654 destroy_tap(struct netdev_obj_linux *netdev_obj)
656 struct tap_state *state = &netdev_obj->state.tap;
657 if (state->tap_fd >= 0) {
658 close(state->tap_fd);
662 /* Destroys the netdev object 'netdev_obj_'. */
664 netdev_linux_destroy(struct netdev_obj *netdev_obj_)
666 struct netdev_obj_linux *netdev_obj = netdev_obj_linux_cast(netdev_obj_);
667 const char *type = netdev_obj_get_type(netdev_obj_);
669 if (!strcmp(type, "tap")) {
670 destroy_tap(netdev_obj);
671 } else if (!strcmp(type, "gre")) {
672 if (gre_descriptors.use_ioctl) {
673 destroy_gre_ioctl(netdev_obj);
675 destroy_gre_netlink(netdev_obj);
682 netdev_linux_open(const char *name, int ethertype, struct netdev **netdevp)
684 struct netdev_linux *netdev;
685 enum netdev_flags flags;
688 /* Allocate network device. */
689 netdev = xzalloc(sizeof *netdev);
690 netdev_init(&netdev->netdev, name, &netdev_linux_class);
691 netdev->netdev_fd = -1;
693 netdev->cache = shash_find_data(&cache_map, name);
694 if (!netdev->cache) {
695 if (shash_is_empty(&cache_map)) {
696 int error = rtnetlink_notifier_register(
697 &netdev_linux_cache_notifier, netdev_linux_cache_cb, NULL);
699 netdev_close(&netdev->netdev);
703 netdev->cache = xmalloc(sizeof *netdev->cache);
704 netdev->cache->shash_node = shash_add(&cache_map, name,
706 netdev->cache->valid = 0;
707 netdev->cache->ref_cnt = 0;
709 netdev->cache->ref_cnt++;
711 if (!strcmp(netdev_get_type(&netdev->netdev), "tap")) {
712 static const char tap_dev[] = "/dev/net/tun";
715 /* Open tap device. */
716 netdev->tap_fd = open(tap_dev, O_RDWR);
717 if (netdev->tap_fd < 0) {
719 VLOG_WARN("opening \"%s\" failed: %s", tap_dev, strerror(error));
723 /* Create tap device. */
724 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
725 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
726 if (ioctl(netdev->tap_fd, TUNSETIFF, &ifr) == -1) {
727 VLOG_WARN("%s: creating tap device failed: %s", name,
733 /* Make non-blocking. */
734 error = set_nonblocking(netdev->tap_fd);
740 error = netdev_get_flags(&netdev->netdev, &flags);
741 if (error == ENODEV) {
745 if (netdev->tap_fd >= 0 || ethertype != NETDEV_ETH_TYPE_NONE) {
746 struct sockaddr_ll sll;
750 /* Create file descriptor. */
751 protocol = (ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
752 : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
754 netdev->netdev_fd = socket(PF_PACKET, SOCK_RAW, htons(protocol));
755 if (netdev->netdev_fd < 0) {
759 if (netdev->tap_fd < 0) {
760 netdev->tap_fd = netdev->netdev_fd;
763 /* Set non-blocking mode. */
764 error = set_nonblocking(netdev->netdev_fd);
769 /* Get ethernet device index. */
770 error = get_ifindex(&netdev->netdev, &ifindex);
775 /* Bind to specific ethernet device. */
776 memset(&sll, 0, sizeof sll);
777 sll.sll_family = AF_PACKET;
778 sll.sll_ifindex = ifindex;
779 if (bind(netdev->netdev_fd,
780 (struct sockaddr *) &sll, sizeof sll) < 0) {
782 VLOG_ERR("bind to %s failed: %s", name, strerror(error));
786 /* Between the socket() and bind() calls above, the socket receives all
787 * packets of the requested type on all system interfaces. We do not
788 * want to receive that data, but there is no way to avoid it. So we
789 * must now drain out the receive queue. */
790 error = drain_rcvbuf(netdev->netdev_fd);
796 *netdevp = &netdev->netdev;
800 netdev_close(&netdev->netdev);
804 /* Closes and destroys 'netdev'. */
806 netdev_linux_close(struct netdev *netdev_)
808 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
810 if (netdev->cache && !--netdev->cache->ref_cnt) {
811 shash_delete(&cache_map, netdev->cache->shash_node);
814 if (shash_is_empty(&cache_map)) {
815 rtnetlink_notifier_unregister(&netdev_linux_cache_notifier);
818 if (netdev->netdev_fd >= 0) {
819 close(netdev->netdev_fd);
821 if (netdev->tap_fd >= 0 && netdev->netdev_fd != netdev->tap_fd) {
822 close(netdev->tap_fd);
827 /* Initializes 'svec' with a list of the names of all known network devices. */
829 netdev_linux_enumerate(struct svec *svec)
831 struct if_nameindex *names;
833 names = if_nameindex();
837 for (i = 0; names[i].if_name != NULL; i++) {
838 svec_add(svec, names[i].if_name);
840 if_freenameindex(names);
843 VLOG_WARN("could not obtain list of network device names: %s",
850 netdev_linux_recv(struct netdev *netdev_, void *data, size_t size)
852 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
854 if (netdev->tap_fd < 0) {
855 /* Device was opened with NETDEV_ETH_TYPE_NONE. */
860 ssize_t retval = read(netdev->tap_fd, data, size);
863 } else if (errno != EINTR) {
864 if (errno != EAGAIN) {
865 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
866 strerror(errno), netdev_get_name(netdev_));
873 /* Registers with the poll loop to wake up from the next call to poll_block()
874 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
876 netdev_linux_recv_wait(struct netdev *netdev_)
878 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
879 if (netdev->tap_fd >= 0) {
880 poll_fd_wait(netdev->tap_fd, POLLIN);
884 /* Discards all packets waiting to be received from 'netdev'. */
886 netdev_linux_drain(struct netdev *netdev_)
888 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
889 if (netdev->tap_fd < 0 && netdev->netdev_fd < 0) {
891 } else if (netdev->tap_fd != netdev->netdev_fd) {
893 int error = netdev_linux_do_ioctl(netdev_, &ifr,
894 SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
898 drain_fd(netdev->tap_fd, ifr.ifr_qlen);
901 return drain_rcvbuf(netdev->netdev_fd);
905 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
906 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
907 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
908 * the packet is too big or too small to transmit on the device.
910 * The caller retains ownership of 'buffer' in all cases.
912 * The kernel maintains a packet transmission queue, so the caller is not
913 * expected to do additional queuing of packets. */
915 netdev_linux_send(struct netdev *netdev_, const void *data, size_t size)
917 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
919 /* XXX should support sending even if 'ethertype' was NETDEV_ETH_TYPE_NONE.
921 if (netdev->tap_fd < 0) {
926 ssize_t retval = write(netdev->tap_fd, data, size);
928 /* The Linux AF_PACKET implementation never blocks waiting for room
929 * for packets, instead returning ENOBUFS. Translate this into
930 * EAGAIN for the caller. */
931 if (errno == ENOBUFS) {
933 } else if (errno == EINTR) {
935 } else if (errno != EAGAIN) {
936 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
937 netdev_get_name(netdev_), strerror(errno));
940 } else if (retval != size) {
941 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
942 "%zu) on %s", retval, size, netdev_get_name(netdev_));
950 /* Registers with the poll loop to wake up from the next call to poll_block()
951 * when the packet transmission queue has sufficient room to transmit a packet
952 * with netdev_send().
954 * The kernel maintains a packet transmission queue, so the client is not
955 * expected to do additional queuing of packets. Thus, this function is
956 * unlikely to ever be used. It is included for completeness. */
958 netdev_linux_send_wait(struct netdev *netdev_)
960 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
961 if (netdev->tap_fd < 0 && netdev->netdev_fd < 0) {
963 } else if (netdev->tap_fd == netdev->netdev_fd) {
964 poll_fd_wait(netdev->tap_fd, POLLOUT);
966 /* TAP device always accepts packets.*/
967 poll_immediate_wake();
971 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
972 * otherwise a positive errno value. */
974 netdev_linux_set_etheraddr(struct netdev *netdev_,
975 const uint8_t mac[ETH_ADDR_LEN])
977 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
980 if (!(netdev->cache->valid & VALID_ETHERADDR)
981 || !eth_addr_equals(netdev->cache->etheraddr, mac)) {
982 error = set_etheraddr(netdev_get_name(netdev_), ARPHRD_ETHER, mac);
984 netdev->cache->valid |= VALID_ETHERADDR;
985 memcpy(netdev->cache->etheraddr, mac, ETH_ADDR_LEN);
993 /* Returns a pointer to 'netdev''s MAC address. The caller must not modify or
994 * free the returned buffer. */
996 netdev_linux_get_etheraddr(const struct netdev *netdev_,
997 uint8_t mac[ETH_ADDR_LEN])
999 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1000 if (!(netdev->cache->valid & VALID_ETHERADDR)) {
1001 int error = get_etheraddr(netdev_get_name(netdev_),
1002 netdev->cache->etheraddr);
1006 netdev->cache->valid |= VALID_ETHERADDR;
1008 memcpy(mac, netdev->cache->etheraddr, ETH_ADDR_LEN);
1012 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
1013 * in bytes, not including the hardware header; thus, this is typically 1500
1014 * bytes for Ethernet devices. */
1016 netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup)
1018 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1019 if (!(netdev->cache->valid & VALID_MTU)) {
1023 error = netdev_linux_do_ioctl(netdev_, &ifr, SIOCGIFMTU, "SIOCGIFMTU");
1027 netdev->cache->mtu = ifr.ifr_mtu;
1028 netdev->cache->valid |= VALID_MTU;
1030 *mtup = netdev->cache->mtu;
1034 /* Returns the ifindex of 'netdev', if successful, as a positive number.
1035 * On failure, returns a negative errno value. */
1037 netdev_linux_get_ifindex(const struct netdev *netdev)
1041 error = get_ifindex(netdev, &ifindex);
1042 return error ? -error : ifindex;
1046 netdev_linux_get_carrier(const struct netdev *netdev_, bool *carrier)
1048 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1053 if (!(netdev->cache->valid & VALID_CARRIER)) {
1057 fn = xasprintf("/sys/class/net/%s/carrier", netdev_get_name(netdev_));
1058 fd = open(fn, O_RDONLY);
1061 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(error));
1065 retval = read(fd, line, sizeof line);
1068 if (error == EINVAL) {
1069 /* This is the normal return value when we try to check carrier
1070 * if the network device is not up. */
1072 VLOG_WARN_RL(&rl, "%s: read failed: %s", fn, strerror(error));
1075 } else if (retval == 0) {
1077 VLOG_WARN_RL(&rl, "%s: unexpected end of file", fn);
1081 if (line[0] != '0' && line[0] != '1') {
1083 VLOG_WARN_RL(&rl, "%s: value is %c (expected 0 or 1)",
1087 netdev->cache->carrier = line[0] != '0';
1088 netdev->cache->valid |= VALID_CARRIER;
1090 *carrier = netdev->cache->carrier;
1101 /* Check whether we can we use RTM_GETLINK to get network device statistics.
1102 * In pre-2.6.19 kernels, this was only available if wireless extensions were
1105 check_for_working_netlink_stats(void)
1107 /* Decide on the netdev_get_stats() implementation to use. Netlink is
1108 * preferable, so if that works, we'll use it. */
1109 int ifindex = do_get_ifindex("lo");
1111 VLOG_WARN("failed to get ifindex for lo, "
1112 "obtaining netdev stats from proc");
1115 struct netdev_stats stats;
1116 int error = get_stats_via_netlink(ifindex, &stats);
1118 VLOG_DBG("obtaining netdev stats via rtnetlink");
1121 VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
1122 "via proc (you are probably running a pre-2.6.19 "
1123 "kernel)", strerror(error));
1129 /* Retrieves current device stats for 'netdev'.
1131 * XXX All of the members of struct netdev_stats are 64 bits wide, but on
1132 * 32-bit architectures the Linux network stats are only 32 bits. */
1134 netdev_linux_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
1136 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1137 static int use_netlink_stats = -1;
1139 struct netdev_stats raw_stats;
1140 struct netdev_stats *collect_stats = stats;
1142 COVERAGE_INC(netdev_get_stats);
1144 if (!(netdev->cache->valid & VALID_IS_INTERNAL)) {
1145 netdev->cache->is_internal = (netdev->tap_fd != -1);
1147 if (!netdev->cache->is_internal) {
1148 struct ethtool_drvinfo drvinfo;
1150 memset(&drvinfo, 0, sizeof drvinfo);
1151 error = netdev_linux_do_ethtool(&netdev->netdev,
1152 (struct ethtool_cmd *)&drvinfo,
1154 "ETHTOOL_GDRVINFO");
1157 netdev->cache->is_internal = !strcmp(drvinfo.driver,
1162 netdev->cache->valid |= VALID_IS_INTERNAL;
1165 if (netdev->cache->is_internal) {
1166 collect_stats = &raw_stats;
1169 if (use_netlink_stats < 0) {
1170 use_netlink_stats = check_for_working_netlink_stats();
1172 if (use_netlink_stats) {
1175 error = get_ifindex(&netdev->netdev, &ifindex);
1177 error = get_stats_via_netlink(ifindex, collect_stats);
1180 error = get_stats_via_proc(netdev->netdev.name, collect_stats);
1183 /* If this port is an internal port then the transmit and receive stats
1184 * will appear to be swapped relative to the other ports since we are the
1185 * one sending the data, not a remote computer. For consistency, we swap
1186 * them back here. */
1187 if (netdev->cache->is_internal) {
1188 stats->rx_packets = raw_stats.tx_packets;
1189 stats->tx_packets = raw_stats.rx_packets;
1190 stats->rx_bytes = raw_stats.tx_bytes;
1191 stats->tx_bytes = raw_stats.rx_bytes;
1192 stats->rx_errors = raw_stats.tx_errors;
1193 stats->tx_errors = raw_stats.rx_errors;
1194 stats->rx_dropped = raw_stats.tx_dropped;
1195 stats->tx_dropped = raw_stats.rx_dropped;
1196 stats->multicast = raw_stats.multicast;
1197 stats->collisions = raw_stats.collisions;
1198 stats->rx_length_errors = 0;
1199 stats->rx_over_errors = 0;
1200 stats->rx_crc_errors = 0;
1201 stats->rx_frame_errors = 0;
1202 stats->rx_fifo_errors = 0;
1203 stats->rx_missed_errors = 0;
1204 stats->tx_aborted_errors = 0;
1205 stats->tx_carrier_errors = 0;
1206 stats->tx_fifo_errors = 0;
1207 stats->tx_heartbeat_errors = 0;
1208 stats->tx_window_errors = 0;
1214 /* Stores the features supported by 'netdev' into each of '*current',
1215 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1216 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1217 * successful, otherwise a positive errno value. */
1219 netdev_linux_get_features(struct netdev *netdev,
1220 uint32_t *current, uint32_t *advertised,
1221 uint32_t *supported, uint32_t *peer)
1223 struct ethtool_cmd ecmd;
1226 memset(&ecmd, 0, sizeof ecmd);
1227 error = netdev_linux_do_ethtool(netdev, &ecmd,
1228 ETHTOOL_GSET, "ETHTOOL_GSET");
1233 /* Supported features. */
1235 if (ecmd.supported & SUPPORTED_10baseT_Half) {
1236 *supported |= OFPPF_10MB_HD;
1238 if (ecmd.supported & SUPPORTED_10baseT_Full) {
1239 *supported |= OFPPF_10MB_FD;
1241 if (ecmd.supported & SUPPORTED_100baseT_Half) {
1242 *supported |= OFPPF_100MB_HD;
1244 if (ecmd.supported & SUPPORTED_100baseT_Full) {
1245 *supported |= OFPPF_100MB_FD;
1247 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
1248 *supported |= OFPPF_1GB_HD;
1250 if (ecmd.supported & SUPPORTED_1000baseT_Full) {
1251 *supported |= OFPPF_1GB_FD;
1253 if (ecmd.supported & SUPPORTED_10000baseT_Full) {
1254 *supported |= OFPPF_10GB_FD;
1256 if (ecmd.supported & SUPPORTED_TP) {
1257 *supported |= OFPPF_COPPER;
1259 if (ecmd.supported & SUPPORTED_FIBRE) {
1260 *supported |= OFPPF_FIBER;
1262 if (ecmd.supported & SUPPORTED_Autoneg) {
1263 *supported |= OFPPF_AUTONEG;
1265 if (ecmd.supported & SUPPORTED_Pause) {
1266 *supported |= OFPPF_PAUSE;
1268 if (ecmd.supported & SUPPORTED_Asym_Pause) {
1269 *supported |= OFPPF_PAUSE_ASYM;
1272 /* Advertised features. */
1274 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
1275 *advertised |= OFPPF_10MB_HD;
1277 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
1278 *advertised |= OFPPF_10MB_FD;
1280 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
1281 *advertised |= OFPPF_100MB_HD;
1283 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
1284 *advertised |= OFPPF_100MB_FD;
1286 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
1287 *advertised |= OFPPF_1GB_HD;
1289 if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
1290 *advertised |= OFPPF_1GB_FD;
1292 if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
1293 *advertised |= OFPPF_10GB_FD;
1295 if (ecmd.advertising & ADVERTISED_TP) {
1296 *advertised |= OFPPF_COPPER;
1298 if (ecmd.advertising & ADVERTISED_FIBRE) {
1299 *advertised |= OFPPF_FIBER;
1301 if (ecmd.advertising & ADVERTISED_Autoneg) {
1302 *advertised |= OFPPF_AUTONEG;
1304 if (ecmd.advertising & ADVERTISED_Pause) {
1305 *advertised |= OFPPF_PAUSE;
1307 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
1308 *advertised |= OFPPF_PAUSE_ASYM;
1311 /* Current settings. */
1312 if (ecmd.speed == SPEED_10) {
1313 *current = ecmd.duplex ? OFPPF_10MB_FD : OFPPF_10MB_HD;
1314 } else if (ecmd.speed == SPEED_100) {
1315 *current = ecmd.duplex ? OFPPF_100MB_FD : OFPPF_100MB_HD;
1316 } else if (ecmd.speed == SPEED_1000) {
1317 *current = ecmd.duplex ? OFPPF_1GB_FD : OFPPF_1GB_HD;
1318 } else if (ecmd.speed == SPEED_10000) {
1319 *current = OFPPF_10GB_FD;
1324 if (ecmd.port == PORT_TP) {
1325 *current |= OFPPF_COPPER;
1326 } else if (ecmd.port == PORT_FIBRE) {
1327 *current |= OFPPF_FIBER;
1331 *current |= OFPPF_AUTONEG;
1334 /* Peer advertisements. */
1335 *peer = 0; /* XXX */
1340 /* Set the features advertised by 'netdev' to 'advertise'. */
1342 netdev_linux_set_advertisements(struct netdev *netdev, uint32_t advertise)
1344 struct ethtool_cmd ecmd;
1347 memset(&ecmd, 0, sizeof ecmd);
1348 error = netdev_linux_do_ethtool(netdev, &ecmd,
1349 ETHTOOL_GSET, "ETHTOOL_GSET");
1354 ecmd.advertising = 0;
1355 if (advertise & OFPPF_10MB_HD) {
1356 ecmd.advertising |= ADVERTISED_10baseT_Half;
1358 if (advertise & OFPPF_10MB_FD) {
1359 ecmd.advertising |= ADVERTISED_10baseT_Full;
1361 if (advertise & OFPPF_100MB_HD) {
1362 ecmd.advertising |= ADVERTISED_100baseT_Half;
1364 if (advertise & OFPPF_100MB_FD) {
1365 ecmd.advertising |= ADVERTISED_100baseT_Full;
1367 if (advertise & OFPPF_1GB_HD) {
1368 ecmd.advertising |= ADVERTISED_1000baseT_Half;
1370 if (advertise & OFPPF_1GB_FD) {
1371 ecmd.advertising |= ADVERTISED_1000baseT_Full;
1373 if (advertise & OFPPF_10GB_FD) {
1374 ecmd.advertising |= ADVERTISED_10000baseT_Full;
1376 if (advertise & OFPPF_COPPER) {
1377 ecmd.advertising |= ADVERTISED_TP;
1379 if (advertise & OFPPF_FIBER) {
1380 ecmd.advertising |= ADVERTISED_FIBRE;
1382 if (advertise & OFPPF_AUTONEG) {
1383 ecmd.advertising |= ADVERTISED_Autoneg;
1385 if (advertise & OFPPF_PAUSE) {
1386 ecmd.advertising |= ADVERTISED_Pause;
1388 if (advertise & OFPPF_PAUSE_ASYM) {
1389 ecmd.advertising |= ADVERTISED_Asym_Pause;
1391 return netdev_linux_do_ethtool(netdev, &ecmd,
1392 ETHTOOL_SSET, "ETHTOOL_SSET");
1395 /* If 'netdev_name' is the name of a VLAN network device (e.g. one created with
1396 * vconfig(8)), sets '*vlan_vid' to the VLAN VID associated with that device
1397 * and returns 0. Otherwise returns a errno value (specifically ENOENT if
1398 * 'netdev_name' is the name of a network device that is not a VLAN device) and
1399 * sets '*vlan_vid' to -1. */
1401 netdev_linux_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
1403 const char *netdev_name = netdev_get_name(netdev);
1404 struct ds line = DS_EMPTY_INITIALIZER;
1405 FILE *stream = NULL;
1409 COVERAGE_INC(netdev_get_vlan_vid);
1410 fn = xasprintf("/proc/net/vlan/%s", netdev_name);
1411 stream = fopen(fn, "r");
1417 if (ds_get_line(&line, stream)) {
1418 if (ferror(stream)) {
1420 VLOG_ERR_RL(&rl, "error reading \"%s\": %s", fn, strerror(errno));
1423 VLOG_ERR_RL(&rl, "unexpected end of file reading \"%s\"", fn);
1428 if (!sscanf(ds_cstr(&line), "%*s VID: %d", vlan_vid)) {
1430 VLOG_ERR_RL(&rl, "parse error reading \"%s\" line 1: \"%s\"",
1431 fn, ds_cstr(&line));
1449 #define POLICE_ADD_CMD "/sbin/tc qdisc add dev %s handle ffff: ingress"
1450 #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"
1451 /* We redirect stderr to /dev/null because we often want to remove all
1452 * traffic control configuration on a port so its in a known state. If
1453 * this done when there is no such configuration, tc complains, so we just
1456 #define POLICE_DEL_CMD "/sbin/tc qdisc del dev %s handle ffff: ingress 2>/dev/null"
1458 /* Attempts to set input rate limiting (policing) policy. */
1460 netdev_linux_set_policing(struct netdev *netdev,
1461 uint32_t kbits_rate, uint32_t kbits_burst)
1463 const char *netdev_name = netdev_get_name(netdev);
1466 COVERAGE_INC(netdev_set_policing);
1469 /* Default to 10 kilobits if not specified. */
1473 /* xxx This should be more careful about only adding if it
1474 * xxx actually exists, as opposed to always deleting it. */
1475 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
1476 if (system(command) == -1) {
1477 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
1480 snprintf(command, sizeof(command), POLICE_ADD_CMD, netdev_name);
1481 if (system(command) != 0) {
1482 VLOG_WARN_RL(&rl, "%s: problem adding policing", netdev_name);
1486 snprintf(command, sizeof(command), POLICE_CONFIG_CMD, netdev_name,
1487 kbits_rate, kbits_burst);
1488 if (system(command) != 0) {
1489 VLOG_WARN_RL(&rl, "%s: problem configuring policing",
1494 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
1495 if (system(command) == -1) {
1496 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
1504 netdev_linux_get_in4(const struct netdev *netdev_,
1505 struct in_addr *address, struct in_addr *netmask)
1507 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1508 if (!(netdev->cache->valid & VALID_IN4)) {
1511 error = netdev_linux_get_ipv4(netdev_, &netdev->cache->address,
1512 SIOCGIFADDR, "SIOCGIFADDR");
1517 error = netdev_linux_get_ipv4(netdev_, &netdev->cache->netmask,
1518 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1523 netdev->cache->valid |= VALID_IN4;
1525 *address = netdev->cache->address;
1526 *netmask = netdev->cache->netmask;
1527 return address->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1531 netdev_linux_set_in4(struct netdev *netdev_, struct in_addr address,
1532 struct in_addr netmask)
1534 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1537 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", address);
1539 netdev->cache->valid |= VALID_IN4;
1540 netdev->cache->address = address;
1541 netdev->cache->netmask = netmask;
1542 if (address.s_addr != INADDR_ANY) {
1543 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1544 "SIOCSIFNETMASK", netmask);
1551 parse_if_inet6_line(const char *line,
1552 struct in6_addr *in6, char ifname[16 + 1])
1554 uint8_t *s6 = in6->s6_addr;
1555 #define X8 "%2"SCNx8
1557 " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
1558 "%*x %*x %*x %*x %16s\n",
1559 &s6[0], &s6[1], &s6[2], &s6[3],
1560 &s6[4], &s6[5], &s6[6], &s6[7],
1561 &s6[8], &s6[9], &s6[10], &s6[11],
1562 &s6[12], &s6[13], &s6[14], &s6[15],
1566 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
1567 * 'in6' is non-null) and returns true. Otherwise, returns false. */
1569 netdev_linux_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1571 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1572 if (!(netdev->cache->valid & VALID_IN6)) {
1576 netdev->cache->in6 = in6addr_any;
1578 file = fopen("/proc/net/if_inet6", "r");
1580 const char *name = netdev_get_name(netdev_);
1581 while (fgets(line, sizeof line, file)) {
1582 struct in6_addr in6;
1583 char ifname[16 + 1];
1584 if (parse_if_inet6_line(line, &in6, ifname)
1585 && !strcmp(name, ifname))
1587 netdev->cache->in6 = in6;
1593 netdev->cache->valid |= VALID_IN6;
1595 *in6 = netdev->cache->in6;
1600 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1602 struct sockaddr_in sin;
1603 memset(&sin, 0, sizeof sin);
1604 sin.sin_family = AF_INET;
1605 sin.sin_addr = addr;
1608 memset(sa, 0, sizeof *sa);
1609 memcpy(sa, &sin, sizeof sin);
1613 do_set_addr(struct netdev *netdev,
1614 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1617 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1618 make_in4_sockaddr(&ifr.ifr_addr, addr);
1619 return netdev_linux_do_ioctl(netdev, &ifr, ioctl_nr, ioctl_name);
1622 /* Adds 'router' as a default IP gateway. */
1624 netdev_linux_add_router(struct netdev *netdev UNUSED, struct in_addr router)
1626 struct in_addr any = { INADDR_ANY };
1630 memset(&rt, 0, sizeof rt);
1631 make_in4_sockaddr(&rt.rt_dst, any);
1632 make_in4_sockaddr(&rt.rt_gateway, router);
1633 make_in4_sockaddr(&rt.rt_genmask, any);
1634 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1635 COVERAGE_INC(netdev_add_router);
1636 error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
1638 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
1644 netdev_linux_get_next_hop(const struct in_addr *host, struct in_addr *next_hop,
1647 static const char fn[] = "/proc/net/route";
1652 *netdev_name = NULL;
1653 stream = fopen(fn, "r");
1654 if (stream == NULL) {
1655 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1660 while (fgets(line, sizeof line, stream)) {
1663 uint32_t dest, gateway, mask;
1664 int refcnt, metric, mtu;
1665 unsigned int flags, use, window, irtt;
1668 "%16s %"SCNx32" %"SCNx32" %04X %d %u %d %"SCNx32
1670 iface, &dest, &gateway, &flags, &refcnt,
1671 &use, &metric, &mask, &mtu, &window, &irtt) != 11) {
1673 VLOG_WARN_RL(&rl, "%s: could not parse line %d: %s",
1677 if (!(flags & RTF_UP)) {
1678 /* Skip routes that aren't up. */
1682 /* The output of 'dest', 'mask', and 'gateway' were given in
1683 * network byte order, so we don't need need any endian
1684 * conversions here. */
1685 if ((dest & mask) == (host->s_addr & mask)) {
1687 /* The host is directly reachable. */
1688 next_hop->s_addr = 0;
1690 /* To reach the host, we must go through a gateway. */
1691 next_hop->s_addr = gateway;
1693 *netdev_name = xstrdup(iface);
1704 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1705 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1706 * returns 0. Otherwise, it returns a positive errno value; in particular,
1707 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
1709 netdev_linux_arp_lookup(const struct netdev *netdev,
1710 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
1713 struct sockaddr_in *pa;
1716 memset(&r, 0, sizeof r);
1717 pa = (struct sockaddr_in *) &r.arp_pa;
1718 pa->sin_family = AF_INET;
1719 pa->sin_addr.s_addr = ip;
1721 r.arp_ha.sa_family = ARPHRD_ETHER;
1723 strncpy(r.arp_dev, netdev->name, sizeof r.arp_dev);
1724 COVERAGE_INC(netdev_arp_lookup);
1725 retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
1727 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
1728 } else if (retval != ENXIO) {
1729 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
1730 netdev->name, IP_ARGS(&ip), strerror(retval));
1736 nd_to_iff_flags(enum netdev_flags nd)
1739 if (nd & NETDEV_UP) {
1742 if (nd & NETDEV_PROMISC) {
1749 iff_to_nd_flags(int iff)
1751 enum netdev_flags nd = 0;
1755 if (iff & IFF_PROMISC) {
1756 nd |= NETDEV_PROMISC;
1762 netdev_linux_update_flags(struct netdev *netdev, enum netdev_flags off,
1763 enum netdev_flags on, enum netdev_flags *old_flagsp)
1765 int old_flags, new_flags;
1768 error = get_flags(netdev, &old_flags);
1770 *old_flagsp = iff_to_nd_flags(old_flags);
1771 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1772 if (new_flags != old_flags) {
1773 error = set_flags(netdev, new_flags);
1780 poll_notify(struct list *list)
1782 struct netdev_linux_notifier *notifier;
1783 LIST_FOR_EACH (notifier, struct netdev_linux_notifier, node, list) {
1784 struct netdev_notifier *n = ¬ifier->notifier;
1790 netdev_linux_poll_cb(const struct rtnetlink_change *change,
1794 struct list *list = shash_find_data(&netdev_linux_notifiers,
1800 struct shash_node *node;
1801 SHASH_FOR_EACH (node, &netdev_linux_notifiers) {
1802 poll_notify(node->data);
1808 netdev_linux_poll_add(struct netdev *netdev,
1809 void (*cb)(struct netdev_notifier *), void *aux,
1810 struct netdev_notifier **notifierp)
1812 const char *netdev_name = netdev_get_name(netdev);
1813 struct netdev_linux_notifier *notifier;
1816 if (shash_is_empty(&netdev_linux_notifiers)) {
1817 int error = rtnetlink_notifier_register(&netdev_linux_poll_notifier,
1818 netdev_linux_poll_cb, NULL);
1824 list = shash_find_data(&netdev_linux_notifiers, netdev_name);
1826 list = xmalloc(sizeof *list);
1828 shash_add(&netdev_linux_notifiers, netdev_name, list);
1831 notifier = xmalloc(sizeof *notifier);
1832 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
1833 list_push_back(list, ¬ifier->node);
1834 *notifierp = ¬ifier->notifier;
1839 netdev_linux_poll_remove(struct netdev_notifier *notifier_)
1841 struct netdev_linux_notifier *notifier =
1842 CONTAINER_OF(notifier_, struct netdev_linux_notifier, notifier);
1845 /* Remove 'notifier' from its list. */
1846 list = list_remove(¬ifier->node);
1847 if (list_is_empty(list)) {
1848 /* The list is now empty. Remove it from the hash and free it. */
1849 const char *netdev_name = netdev_get_name(notifier->notifier.netdev);
1850 shash_delete(&netdev_linux_notifiers,
1851 shash_find(&netdev_linux_notifiers, netdev_name));
1856 /* If that was the last notifier, unregister. */
1857 if (shash_is_empty(&netdev_linux_notifiers)) {
1858 rtnetlink_notifier_unregister(&netdev_linux_poll_notifier);
1862 const struct netdev_class netdev_linux_class = {
1863 "system", /* type */
1869 netdev_linux_create_system,
1870 netdev_linux_destroy,
1871 NULL, /* reconfigure */
1876 netdev_linux_enumerate,
1879 netdev_linux_recv_wait,
1883 netdev_linux_send_wait,
1885 netdev_linux_set_etheraddr,
1886 netdev_linux_get_etheraddr,
1887 netdev_linux_get_mtu,
1888 netdev_linux_get_ifindex,
1889 netdev_linux_get_carrier,
1890 netdev_linux_get_stats,
1892 netdev_linux_get_features,
1893 netdev_linux_set_advertisements,
1894 netdev_linux_get_vlan_vid,
1895 netdev_linux_set_policing,
1897 netdev_linux_get_in4,
1898 netdev_linux_set_in4,
1899 netdev_linux_get_in6,
1900 netdev_linux_add_router,
1901 netdev_linux_get_next_hop,
1902 netdev_linux_arp_lookup,
1904 netdev_linux_update_flags,
1906 netdev_linux_poll_add,
1907 netdev_linux_poll_remove,
1910 const struct netdev_class netdev_tap_class = {
1917 netdev_linux_create_tap,
1918 netdev_linux_destroy,
1919 NULL, /* reconfigure */
1924 netdev_linux_enumerate,
1927 netdev_linux_recv_wait,
1931 netdev_linux_send_wait,
1933 netdev_linux_set_etheraddr,
1934 netdev_linux_get_etheraddr,
1935 netdev_linux_get_mtu,
1936 netdev_linux_get_ifindex,
1937 netdev_linux_get_carrier,
1938 netdev_linux_get_stats,
1940 netdev_linux_get_features,
1941 netdev_linux_set_advertisements,
1942 netdev_linux_get_vlan_vid,
1943 netdev_linux_set_policing,
1945 netdev_linux_get_in4,
1946 netdev_linux_set_in4,
1947 netdev_linux_get_in6,
1948 netdev_linux_add_router,
1949 netdev_linux_get_next_hop,
1950 netdev_linux_arp_lookup,
1952 netdev_linux_update_flags,
1954 netdev_linux_poll_add,
1955 netdev_linux_poll_remove,
1958 const struct netdev_class netdev_gre_class = {
1965 netdev_linux_create_gre,
1966 netdev_linux_destroy,
1967 netdev_linux_reconfigure_gre,
1972 netdev_linux_enumerate,
1975 netdev_linux_recv_wait,
1979 netdev_linux_send_wait,
1981 netdev_linux_set_etheraddr,
1982 netdev_linux_get_etheraddr,
1983 netdev_linux_get_mtu,
1984 netdev_linux_get_ifindex,
1985 netdev_linux_get_carrier,
1986 netdev_linux_get_stats,
1988 netdev_linux_get_features,
1989 netdev_linux_set_advertisements,
1990 netdev_linux_get_vlan_vid,
1991 netdev_linux_set_policing,
1993 netdev_linux_get_in4,
1994 netdev_linux_set_in4,
1995 netdev_linux_get_in6,
1996 netdev_linux_add_router,
1997 netdev_linux_get_next_hop,
1998 netdev_linux_arp_lookup,
2000 netdev_linux_update_flags,
2002 netdev_linux_poll_add,
2003 netdev_linux_poll_remove,
2007 get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
2009 /* Policy for RTNLGRP_LINK messages.
2011 * There are *many* more fields in these messages, but currently we only
2012 * care about these fields. */
2013 static const struct nl_policy rtnlgrp_link_policy[] = {
2014 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
2015 [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
2016 .min_len = sizeof(struct rtnl_link_stats) },
2020 static struct nl_sock *rtnl_sock;
2021 struct ofpbuf request;
2022 struct ofpbuf *reply;
2023 struct ifinfomsg *ifi;
2024 const struct rtnl_link_stats *rtnl_stats;
2025 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
2029 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0, &rtnl_sock);
2031 VLOG_ERR_RL(&rl, "failed to create rtnetlink socket: %s",
2037 ofpbuf_init(&request, 0);
2038 nl_msg_put_nlmsghdr(&request, rtnl_sock, sizeof *ifi,
2039 RTM_GETLINK, NLM_F_REQUEST);
2040 ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
2041 ifi->ifi_family = PF_UNSPEC;
2042 ifi->ifi_index = ifindex;
2043 error = nl_sock_transact(rtnl_sock, &request, &reply);
2044 ofpbuf_uninit(&request);
2049 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
2050 rtnlgrp_link_policy,
2051 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
2052 ofpbuf_delete(reply);
2056 if (!attrs[IFLA_STATS]) {
2057 VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
2058 ofpbuf_delete(reply);
2062 rtnl_stats = nl_attr_get(attrs[IFLA_STATS]);
2063 stats->rx_packets = rtnl_stats->rx_packets;
2064 stats->tx_packets = rtnl_stats->tx_packets;
2065 stats->rx_bytes = rtnl_stats->rx_bytes;
2066 stats->tx_bytes = rtnl_stats->tx_bytes;
2067 stats->rx_errors = rtnl_stats->rx_errors;
2068 stats->tx_errors = rtnl_stats->tx_errors;
2069 stats->rx_dropped = rtnl_stats->rx_dropped;
2070 stats->tx_dropped = rtnl_stats->tx_dropped;
2071 stats->multicast = rtnl_stats->multicast;
2072 stats->collisions = rtnl_stats->collisions;
2073 stats->rx_length_errors = rtnl_stats->rx_length_errors;
2074 stats->rx_over_errors = rtnl_stats->rx_over_errors;
2075 stats->rx_crc_errors = rtnl_stats->rx_crc_errors;
2076 stats->rx_frame_errors = rtnl_stats->rx_frame_errors;
2077 stats->rx_fifo_errors = rtnl_stats->rx_fifo_errors;
2078 stats->rx_missed_errors = rtnl_stats->rx_missed_errors;
2079 stats->tx_aborted_errors = rtnl_stats->tx_aborted_errors;
2080 stats->tx_carrier_errors = rtnl_stats->tx_carrier_errors;
2081 stats->tx_fifo_errors = rtnl_stats->tx_fifo_errors;
2082 stats->tx_heartbeat_errors = rtnl_stats->tx_heartbeat_errors;
2083 stats->tx_window_errors = rtnl_stats->tx_window_errors;
2085 ofpbuf_delete(reply);
2091 get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats)
2093 static const char fn[] = "/proc/net/dev";
2098 stream = fopen(fn, "r");
2100 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
2105 while (fgets(line, sizeof line, stream)) {
2108 #define X64 "%"SCNu64
2111 X64 X64 X64 X64 X64 X64 X64 "%*u"
2112 X64 X64 X64 X64 X64 X64 X64 "%*u",
2118 &stats->rx_fifo_errors,
2119 &stats->rx_frame_errors,
2125 &stats->tx_fifo_errors,
2127 &stats->tx_carrier_errors) != 15) {
2128 VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln);
2129 } else if (!strcmp(devname, netdev_name)) {
2130 stats->rx_length_errors = UINT64_MAX;
2131 stats->rx_over_errors = UINT64_MAX;
2132 stats->rx_crc_errors = UINT64_MAX;
2133 stats->rx_missed_errors = UINT64_MAX;
2134 stats->tx_aborted_errors = UINT64_MAX;
2135 stats->tx_heartbeat_errors = UINT64_MAX;
2136 stats->tx_window_errors = UINT64_MAX;
2142 VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name);
2148 get_flags(const struct netdev *netdev, int *flags)
2153 error = netdev_linux_do_ioctl(netdev, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
2154 *flags = ifr.ifr_flags;
2159 set_flags(struct netdev *netdev, int flags)
2163 ifr.ifr_flags = flags;
2164 return netdev_linux_do_ioctl(netdev, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
2168 do_get_ifindex(const char *netdev_name)
2172 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
2173 COVERAGE_INC(netdev_get_ifindex);
2174 if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) {
2175 VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
2176 netdev_name, strerror(errno));
2179 return ifr.ifr_ifindex;
2183 get_ifindex(const struct netdev *netdev_, int *ifindexp)
2185 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2187 if (!(netdev->cache->valid & VALID_IFINDEX)) {
2188 int ifindex = do_get_ifindex(netdev_get_name(netdev_));
2192 netdev->cache->valid |= VALID_IFINDEX;
2193 netdev->cache->ifindex = ifindex;
2195 *ifindexp = netdev->cache->ifindex;
2200 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
2205 memset(&ifr, 0, sizeof ifr);
2206 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
2207 COVERAGE_INC(netdev_get_hwaddr);
2208 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
2209 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
2210 netdev_name, strerror(errno));
2213 hwaddr_family = ifr.ifr_hwaddr.sa_family;
2214 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
2215 VLOG_WARN("%s device has unknown hardware address family %d",
2216 netdev_name, hwaddr_family);
2218 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
2223 set_etheraddr(const char *netdev_name, int hwaddr_family,
2224 const uint8_t mac[ETH_ADDR_LEN])
2228 memset(&ifr, 0, sizeof ifr);
2229 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
2230 ifr.ifr_hwaddr.sa_family = hwaddr_family;
2231 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
2232 COVERAGE_INC(netdev_set_hwaddr);
2233 if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) {
2234 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
2235 netdev_name, strerror(errno));
2242 netdev_linux_do_ethtool(struct netdev *netdev, struct ethtool_cmd *ecmd,
2243 int cmd, const char *cmd_name)
2247 memset(&ifr, 0, sizeof ifr);
2248 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
2249 ifr.ifr_data = (caddr_t) ecmd;
2252 COVERAGE_INC(netdev_ethtool);
2253 if (ioctl(af_inet_sock, SIOCETHTOOL, &ifr) == 0) {
2256 if (errno != EOPNOTSUPP) {
2257 VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
2258 "failed: %s", cmd_name, netdev->name,
2261 /* The device doesn't support this operation. That's pretty
2262 * common, so there's no point in logging anything. */
2269 netdev_linux_do_ioctl(const struct netdev *netdev, struct ifreq *ifr,
2270 int cmd, const char *cmd_name)
2272 strncpy(ifr->ifr_name, netdev_get_name(netdev), sizeof ifr->ifr_name);
2273 if (ioctl(af_inet_sock, cmd, ifr) == -1) {
2274 VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s",
2275 netdev_get_name(netdev), cmd_name, strerror(errno));
2282 netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip,
2283 int cmd, const char *cmd_name)
2288 ifr.ifr_addr.sa_family = AF_INET;
2289 error = netdev_linux_do_ioctl(netdev, &ifr, cmd, cmd_name);
2291 const struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
2292 *ip = sin->sin_addr;