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/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)
77 static struct rtnetlink_notifier netdev_linux_cache_notifier;
78 static int cache_notifier_refcount;
81 VALID_IFINDEX = 1 << 0,
82 VALID_ETHERADDR = 1 << 1,
86 VALID_CARRIER = 1 << 5,
87 VALID_IS_INTERNAL = 1 << 6
98 struct netdev_dev_linux {
99 struct netdev_dev netdev_dev;
101 struct shash_node *shash_node;
102 unsigned int cache_valid;
105 uint8_t etheraddr[ETH_ADDR_LEN];
106 struct in_addr address, netmask;
113 struct tap_state tap;
114 struct patch_state patch;
118 struct netdev_linux {
119 struct netdev netdev;
123 /* An AF_INET socket (used for ioctl operations). */
124 static int af_inet_sock = -1;
141 struct nl_sock *nl_sock;
147 struct netdev_linux_notifier {
148 struct netdev_notifier notifier;
152 static struct shash netdev_linux_notifiers =
153 SHASH_INITIALIZER(&netdev_linux_notifiers);
154 static struct rtnetlink_notifier netdev_linux_poll_notifier;
156 /* This is set pretty low because we probably won't learn anything from the
157 * additional log messages. */
158 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
160 static int if_up(const char *name);
161 static int destroy_gre(const char *name);
162 static int netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *,
163 int cmd, const char *cmd_name);
164 static int netdev_linux_do_ioctl(const char *name, struct ifreq *, int cmd,
165 const char *cmd_name);
166 static int netdev_linux_get_ipv4(const struct netdev *, struct in_addr *,
167 int cmd, const char *cmd_name);
168 static int get_flags(const struct netdev *, int *flagsp);
169 static int set_flags(struct netdev *, int flags);
170 static int do_get_ifindex(const char *netdev_name);
171 static int get_ifindex(const struct netdev *, int *ifindexp);
172 static int do_set_addr(struct netdev *netdev,
173 int ioctl_nr, const char *ioctl_name,
174 struct in_addr addr);
175 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
176 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
177 const uint8_t[ETH_ADDR_LEN]);
178 static int get_stats_via_netlink(int ifindex, struct netdev_stats *stats);
179 static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats);
181 static struct netdev_dev_linux *
182 netdev_dev_linux_cast(const struct netdev_dev *netdev_dev)
184 const char *type = netdev_dev_get_type(netdev_dev);
185 assert(!strcmp(type, "system") || !strcmp(type, "tap")
186 || !strcmp(type, "gre") || !strcmp(type, "patch"));
187 return CONTAINER_OF(netdev_dev, struct netdev_dev_linux, netdev_dev);
190 static struct netdev_linux *
191 netdev_linux_cast(const struct netdev *netdev)
193 const char *type = netdev_get_type(netdev);
194 assert(!strcmp(type, "system") || !strcmp(type, "tap")
195 || !strcmp(type, "gre") || !strcmp(type, "patch"));
196 return CONTAINER_OF(netdev, struct netdev_linux, netdev);
200 netdev_linux_init(void)
202 static int status = -1;
204 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
205 status = af_inet_sock >= 0 ? 0 : errno;
207 VLOG_ERR("failed to create inet socket: %s", strerror(status));
214 netdev_linux_run(void)
216 rtnetlink_notifier_run();
220 netdev_linux_wait(void)
222 rtnetlink_notifier_wait();
226 netdev_linux_cache_cb(const struct rtnetlink_change *change,
227 void *aux OVS_UNUSED)
229 struct netdev_dev_linux *dev;
231 struct netdev_dev *base_dev = netdev_dev_from_name(change->ifname);
233 dev = netdev_dev_linux_cast(base_dev);
234 dev->cache_valid = 0;
237 struct shash device_shash;
238 struct shash_node *node;
240 shash_init(&device_shash);
241 netdev_dev_get_devices(&netdev_linux_class, &device_shash);
242 SHASH_FOR_EACH (node, &device_shash) {
244 dev->cache_valid = 0;
246 shash_destroy(&device_shash);
250 /* The arguments are marked as unused to prevent warnings on platforms where
251 * the Netlink interface isn't supported. */
253 setup_gre_netlink(const char *name OVS_UNUSED,
254 struct gre_config *config OVS_UNUSED, bool create OVS_UNUSED)
256 #ifdef GRE_IOCTL_ONLY
260 struct ofpbuf request, *reply;
261 unsigned int nl_flags;
262 struct ifinfomsg ifinfomsg;
263 struct nlattr *linkinfo_hdr;
264 struct nlattr *info_data_hdr;
268 VLOG_DBG("%s: attempting to create gre device using netlink", name);
270 if (!gre_descriptors.nl_sock) {
271 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0,
272 &gre_descriptors.nl_sock);
274 VLOG_WARN("couldn't create netlink socket: %s", strerror(error));
279 ofpbuf_init(&request, 0);
281 nl_flags = NLM_F_REQUEST;
283 nl_flags |= NLM_F_CREATE|NLM_F_EXCL;
286 /* We over-reserve space, because we do some pointer arithmetic
287 * and don't want the buffer address shifting under us. */
288 nl_msg_put_nlmsghdr(&request, gre_descriptors.nl_sock, 2048, RTM_NEWLINK,
291 memset(&ifinfomsg, 0, sizeof ifinfomsg);
292 ifinfomsg.ifi_family = AF_UNSPEC;
293 nl_msg_put(&request, &ifinfomsg, sizeof ifinfomsg);
295 linkinfo_hdr = ofpbuf_tail(&request);
296 nl_msg_put_unspec(&request, IFLA_LINKINFO, NULL, 0);
298 nl_msg_put_unspec(&request, IFLA_INFO_KIND, "gretap", 6);
300 info_data_hdr = ofpbuf_tail(&request);
301 nl_msg_put_unspec(&request, IFLA_INFO_DATA, NULL, 0);
304 if (config->have_in_key) {
307 if (config->have_out_key) {
311 if (config->in_csum) {
314 if (config->out_csum) {
319 nl_msg_put_u32(&request, IFLA_GRE_IKEY, config->in_key);
320 nl_msg_put_u32(&request, IFLA_GRE_OKEY, config->out_key);
321 nl_msg_put_u16(&request, IFLA_GRE_IFLAGS, iflags);
322 nl_msg_put_u16(&request, IFLA_GRE_OFLAGS, oflags);
323 nl_msg_put_u32(&request, IFLA_GRE_LOCAL, config->local_ip);
324 nl_msg_put_u32(&request, IFLA_GRE_REMOTE, config->remote_ip);
325 nl_msg_put_u8(&request, IFLA_GRE_PMTUDISC, config->pmtud);
326 nl_msg_put_u8(&request, IFLA_GRE_TTL, IPDEFTTL);
327 nl_msg_put_u8(&request, IFLA_GRE_TOS, config->tos);
329 info_data_hdr->nla_len = (char *)ofpbuf_tail(&request)
330 - (char *)info_data_hdr;
331 linkinfo_hdr->nla_len = (char *)ofpbuf_tail(&request)
332 - (char *)linkinfo_hdr;
334 nl_msg_put_string(&request, IFLA_IFNAME, name);
336 error = nl_sock_transact(gre_descriptors.nl_sock, &request, &reply);
337 ofpbuf_uninit(&request);
339 VLOG_WARN("couldn't transact netlink socket: %s", strerror(error));
342 ofpbuf_delete(reply);
350 setup_gre_ioctl(const char *name, struct gre_config *config, bool create)
352 struct ip_tunnel_parm p;
355 VLOG_DBG("%s: attempting to create gre device using ioctl", name);
357 memset(&p, 0, sizeof p);
359 strncpy(p.name, name, IFNAMSIZ);
363 p.iph.protocol = IPPROTO_GRE;
364 p.iph.saddr = config->local_ip;
365 p.iph.daddr = config->remote_ip;
366 p.iph.ttl = IPDEFTTL;
367 p.iph.tos = config->tos;
369 if (config->have_in_key) {
370 p.i_flags |= GRE_KEY;
371 p.i_key = config->in_key;
373 if (config->have_out_key) {
374 p.o_flags |= GRE_KEY;
375 p.o_key = config->out_key;
378 if (config->in_csum) {
379 p.i_flags |= GRE_CSUM;
381 if (config->out_csum) {
382 p.o_flags |= GRE_CSUM;
386 p.iph.frag_off = htons(IP_DONT_FRAGMENT);
389 strncpy(ifr.ifr_name, create ? GRE_IOCTL_DEVICE : name, IFNAMSIZ);
390 ifr.ifr_ifru.ifru_data = (void *)&p;
392 if (!gre_descriptors.ioctl_fd) {
393 gre_descriptors.ioctl_fd = socket(AF_INET, SOCK_DGRAM, 0);
394 if (gre_descriptors.ioctl_fd < 0) {
395 VLOG_WARN("couldn't create gre ioctl socket: %s", strerror(errno));
396 gre_descriptors.ioctl_fd = 0;
401 if (ioctl(gre_descriptors.ioctl_fd, create ? SIOCADDGRETAP : SIOCCHGGRETAP,
403 VLOG_WARN("couldn't do gre ioctl: %s", strerror(errno));
410 /* The arguments are marked as unused to prevent warnings on platforms where
411 * the Netlink interface isn't supported. */
413 check_gre_device_netlink(const char *name OVS_UNUSED)
415 #ifdef GRE_IOCTL_ONLY
418 static const struct nl_policy getlink_policy[] = {
419 [IFLA_LINKINFO] = { .type = NL_A_NESTED, .optional = false },
422 static const struct nl_policy linkinfo_policy[] = {
423 [IFLA_INFO_KIND] = { .type = NL_A_STRING, .optional = false },
428 struct ofpbuf request, *reply;
429 struct ifinfomsg ifinfomsg;
430 struct nlattr *getlink_attrs[ARRAY_SIZE(getlink_policy)];
431 struct nlattr *linkinfo_attrs[ARRAY_SIZE(linkinfo_policy)];
432 struct ofpbuf linkinfo;
433 const char *device_kind;
435 ofpbuf_init(&request, 0);
437 nl_msg_put_nlmsghdr(&request, gre_descriptors.nl_sock,
438 NLMSG_LENGTH(sizeof ifinfomsg), RTM_GETLINK,
441 memset(&ifinfomsg, 0, sizeof ifinfomsg);
442 ifinfomsg.ifi_family = AF_UNSPEC;
443 ifinfomsg.ifi_index = do_get_ifindex(name);
444 nl_msg_put(&request, &ifinfomsg, sizeof ifinfomsg);
446 error = nl_sock_transact(gre_descriptors.nl_sock, &request, &reply);
447 ofpbuf_uninit(&request);
449 VLOG_WARN("couldn't transact netlink socket: %s", strerror(error));
453 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
454 getlink_policy, getlink_attrs,
455 ARRAY_SIZE(getlink_policy))) {
456 VLOG_WARN("received bad rtnl message (getlink policy)");
460 linkinfo.data = (void *)nl_attr_get(getlink_attrs[IFLA_LINKINFO]);
461 linkinfo.size = nl_attr_get_size(getlink_attrs[IFLA_LINKINFO]);
462 if (!nl_policy_parse(&linkinfo, 0, linkinfo_policy,
463 linkinfo_attrs, ARRAY_SIZE(linkinfo_policy))) {
464 VLOG_WARN("received bad rtnl message (linkinfo policy)");
468 device_kind = nl_attr_get_string(linkinfo_attrs[IFLA_INFO_KIND]);
469 ret = !strcmp(device_kind, "gretap");
472 ofpbuf_delete(reply);
478 check_gre_device_ioctl(const char *name)
480 struct ethtool_drvinfo drvinfo;
483 memset(&drvinfo, 0, sizeof drvinfo);
484 error = netdev_linux_do_ethtool(name, (struct ethtool_cmd *)&drvinfo,
485 ETHTOOL_GDRVINFO, "ETHTOOL_GDRVINFO");
487 return !error && !strcmp(drvinfo.driver, "ip_gre")
488 && !strcmp(drvinfo.bus_info, "gretap");
492 setup_gre(const char *name, const struct shash *args, bool create)
495 struct in_addr in_addr;
496 struct shash_node *node;
497 struct gre_config config;
499 memset(&config, 0, sizeof config);
500 config.in_csum = true;
501 config.out_csum = true;
504 SHASH_FOR_EACH (node, args) {
505 if (!strcmp(node->name, "remote_ip")) {
506 if (lookup_ip(node->data, &in_addr)) {
507 VLOG_WARN("bad 'remote_ip' for gre device %s ", name);
509 config.remote_ip = in_addr.s_addr;
511 } else if (!strcmp(node->name, "local_ip")) {
512 if (lookup_ip(node->data, &in_addr)) {
513 VLOG_WARN("bad 'local_ip' for gre device %s ", name);
515 config.local_ip = in_addr.s_addr;
517 } else if (!strcmp(node->name, "key")) {
518 config.have_in_key = true;
519 config.have_out_key = true;
520 config.in_key = htonl(atoi(node->data));
521 config.out_key = htonl(atoi(node->data));
522 } else if (!strcmp(node->name, "in_key")) {
523 config.have_in_key = true;
524 config.in_key = htonl(atoi(node->data));
525 } else if (!strcmp(node->name, "out_key")) {
526 config.have_out_key = true;
527 config.out_key = htonl(atoi(node->data));
528 } else if (!strcmp(node->name, "tos")) {
529 config.tos = atoi(node->data);
530 } else if (!strcmp(node->name, "csum")) {
531 if (!strcmp(node->data, "false")) {
532 config.in_csum = false;
533 config.out_csum = false;
535 } else if (!strcmp(node->name, "pmtud")) {
536 if (!strcmp(node->data, "false")) {
537 config.pmtud = false;
540 VLOG_WARN("unknown gre argument '%s'", node->name);
544 if (!config.remote_ip) {
545 VLOG_WARN("gre type requires valid 'remote_ip' argument");
550 if (!gre_descriptors.use_ioctl) {
551 error = setup_gre_netlink(name, &config, create);
552 if (error == EOPNOTSUPP) {
553 gre_descriptors.use_ioctl = true;
556 if (gre_descriptors.use_ioctl) {
557 error = setup_gre_ioctl(name, &config, create);
560 if (create && error == EEXIST) {
563 if (gre_descriptors.use_ioctl) {
564 gre_device = check_gre_device_ioctl(name);
566 gre_device = check_gre_device_netlink(name);
573 VLOG_WARN("replacing existing gre device %s", name);
574 error = destroy_gre(name);
579 if (gre_descriptors.use_ioctl) {
580 error = setup_gre_ioctl(name, &config, create);
582 error = setup_gre_netlink(name, &config, create);
590 /* A veth may be created using the 'command' "+<name>,<peer>". A veth may
591 * be destroyed by using the 'command' "-<name>", where <name> can be
592 * either side of the device.
595 modify_veth(const char *format, ...)
601 veth_file = fopen("/sys/class/net/veth_pairs", "w");
603 VLOG_WARN_RL(&rl, "could not open veth device. Are you running a "
604 "supported XenServer with the kernel module loaded?");
607 setvbuf(veth_file, NULL, _IONBF, 0);
609 va_start(args, format);
610 retval = vfprintf(veth_file, format, args);
615 VLOG_WARN_RL(&rl, "could not destroy patch: %s", strerror(errno));
623 create_patch(const char *name, const char *peer)
626 struct netdev_dev *peer_nd;
629 /* Only create the veth if the peer didn't already do it. */
630 peer_nd = netdev_dev_from_name(peer);
632 if (!strcmp("patch", netdev_dev_get_type(peer_nd))) {
633 struct netdev_dev_linux *ndl = netdev_dev_linux_cast(peer_nd);
634 if (!strcmp(name, ndl->state.patch.peer)) {
637 VLOG_WARN_RL(&rl, "peer '%s' already paired with '%s'",
638 peer, ndl->state.patch.peer);
642 VLOG_WARN_RL(&rl, "peer '%s' exists and is not a patch", peer);
647 retval = modify_veth("+%s,%s", name, peer);
652 retval = if_up(name);
657 retval = if_up(peer);
666 setup_patch(const char *name, const struct shash *args, char **peer_)
670 peer = shash_find_data(args, "peer");
672 VLOG_WARN("patch type requires valid 'peer' argument");
676 if (shash_count(args) > 1) {
677 VLOG_WARN("patch type takes only a 'peer' argument");
681 if (strlen(peer) >= IFNAMSIZ) {
682 VLOG_WARN_RL(&rl, "patch 'peer' arg too long");
686 *peer_ = xstrdup(peer);
687 return create_patch(name, peer);
690 /* Creates the netdev device of 'type' with 'name'. */
692 netdev_linux_create_system(const char *name, const char *type OVS_UNUSED,
693 const struct shash *args, struct netdev_dev **netdev_devp)
695 struct netdev_dev_linux *netdev_dev;
698 if (!shash_is_empty(args)) {
699 VLOG_WARN("%s: arguments for system devices should be empty", name);
702 if (!cache_notifier_refcount) {
703 error = rtnetlink_notifier_register(&netdev_linux_cache_notifier,
704 netdev_linux_cache_cb, NULL);
709 cache_notifier_refcount++;
711 netdev_dev = xzalloc(sizeof *netdev_dev);
712 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_linux_class);
714 *netdev_devp = &netdev_dev->netdev_dev;
718 /* For most types of netdevs we open the device for each call of
719 * netdev_open(). However, this is not the case with tap devices,
720 * since it is only possible to open the device once. In this
721 * situation we share a single file descriptor, and consequently
722 * buffers, across all readers. Therefore once data is read it will
723 * be unavailable to other reads for tap devices. */
725 netdev_linux_create_tap(const char *name, const char *type OVS_UNUSED,
726 const struct shash *args, struct netdev_dev **netdev_devp)
728 struct netdev_dev_linux *netdev_dev;
729 struct tap_state *state;
730 static const char tap_dev[] = "/dev/net/tun";
734 if (!shash_is_empty(args)) {
735 VLOG_WARN("%s: arguments for TAP devices should be empty", name);
738 netdev_dev = xzalloc(sizeof *netdev_dev);
739 state = &netdev_dev->state.tap;
741 /* Open tap device. */
742 state->fd = open(tap_dev, O_RDWR);
745 VLOG_WARN("opening \"%s\" failed: %s", tap_dev, strerror(error));
749 /* Create tap device. */
750 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
751 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
752 if (ioctl(state->fd, TUNSETIFF, &ifr) == -1) {
753 VLOG_WARN("%s: creating tap device failed: %s", name,
759 /* Make non-blocking. */
760 error = set_nonblocking(state->fd);
765 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_tap_class);
766 *netdev_devp = &netdev_dev->netdev_dev;
775 if_up(const char *name)
779 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
780 ifr.ifr_flags = IFF_UP;
782 if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) {
783 VLOG_DBG_RL(&rl, "%s: failed to bring device up: %s",
784 name, strerror(errno));
792 netdev_linux_create_gre(const char *name, const char *type OVS_UNUSED,
793 const struct shash *args, struct netdev_dev **netdev_devp)
795 struct netdev_dev_linux *netdev_dev;
798 netdev_dev = xzalloc(sizeof *netdev_dev);
800 error = setup_gre(name, args, true);
810 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
811 *netdev_devp = &netdev_dev->netdev_dev;
820 netdev_linux_create_patch(const char *name, const char *type OVS_UNUSED,
821 const struct shash *args, struct netdev_dev **netdev_devp)
823 struct netdev_dev_linux *netdev_dev;
827 error = setup_patch(name, args, &peer);
833 netdev_dev = xzalloc(sizeof *netdev_dev);
834 netdev_dev->state.patch.peer = peer;
835 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_patch_class);
836 *netdev_devp = &netdev_dev->netdev_dev;
842 netdev_linux_reconfigure_gre(struct netdev_dev *netdev_dev_,
843 const struct shash *args)
845 const char *name = netdev_dev_get_name(netdev_dev_);
847 return setup_gre(name, args, false);
850 /* The arguments are marked as unused to prevent warnings on platforms where
851 * the Netlink interface isn't supported. */
853 destroy_gre_netlink(const char *name OVS_UNUSED)
855 #ifdef GRE_IOCTL_ONLY
859 struct ofpbuf request, *reply;
860 struct ifinfomsg ifinfomsg;
863 ofpbuf_init(&request, 0);
865 nl_msg_put_nlmsghdr(&request, gre_descriptors.nl_sock, 0, RTM_DELLINK,
868 memset(&ifinfomsg, 0, sizeof ifinfomsg);
869 ifinfomsg.ifi_family = AF_UNSPEC;
870 nl_msg_put(&request, &ifinfomsg, sizeof ifinfomsg);
872 ifindex = do_get_ifindex(name);
873 nl_msg_put_u32(&request, IFLA_LINK, ifindex);
875 nl_msg_put_string(&request, IFLA_IFNAME, name);
877 error = nl_sock_transact(gre_descriptors.nl_sock, &request, &reply);
878 ofpbuf_uninit(&request);
880 VLOG_WARN("couldn't transact netlink socket: %s", strerror(error));
883 ofpbuf_delete(reply);
891 destroy_gre_ioctl(const char *name)
893 struct ip_tunnel_parm p;
896 memset(&p, 0, sizeof p);
897 strncpy(p.name, name, IFNAMSIZ);
899 strncpy(ifr.ifr_name, name, IFNAMSIZ);
900 ifr.ifr_ifru.ifru_data = (void *)&p;
902 if (ioctl(gre_descriptors.ioctl_fd, SIOCDELGRETAP, &ifr) < 0) {
903 VLOG_WARN("couldn't do gre ioctl: %s\n", strerror(errno));
911 destroy_tap(struct netdev_dev_linux *netdev_dev)
913 struct tap_state *state = &netdev_dev->state.tap;
915 if (state->fd >= 0) {
921 destroy_gre(const char *name)
923 if (gre_descriptors.use_ioctl) {
924 return destroy_gre_ioctl(name);
926 return destroy_gre_netlink(name);
931 destroy_patch(struct netdev_dev_linux *netdev_dev)
933 const char *name = netdev_dev_get_name(&netdev_dev->netdev_dev);
934 struct patch_state *state = &netdev_dev->state.patch;
936 /* Only destroy veth if 'peer' doesn't exist as an existing netdev. */
937 if (!netdev_dev_from_name(state->peer)) {
938 modify_veth("-%s", name);
943 /* Destroys the netdev device 'netdev_dev_'. */
945 netdev_linux_destroy(struct netdev_dev *netdev_dev_)
947 struct netdev_dev_linux *netdev_dev = netdev_dev_linux_cast(netdev_dev_);
948 const char *type = netdev_dev_get_type(netdev_dev_);
950 if (!strcmp(type, "system")) {
951 cache_notifier_refcount--;
953 if (!cache_notifier_refcount) {
954 rtnetlink_notifier_unregister(&netdev_linux_cache_notifier);
956 } else if (!strcmp(type, "tap")) {
957 destroy_tap(netdev_dev);
958 } else if (!strcmp(type, "gre")) {
959 destroy_gre(netdev_dev_get_name(&netdev_dev->netdev_dev));
960 } else if (!strcmp(type, "patch")) {
961 destroy_patch(netdev_dev);
968 netdev_linux_open(struct netdev_dev *netdev_dev_, int ethertype,
969 struct netdev **netdevp)
971 struct netdev_dev_linux *netdev_dev = netdev_dev_linux_cast(netdev_dev_);
972 struct netdev_linux *netdev;
973 enum netdev_flags flags;
976 /* Allocate network device. */
977 netdev = xzalloc(sizeof *netdev);
979 netdev_init(&netdev->netdev, netdev_dev_);
981 error = netdev_get_flags(&netdev->netdev, &flags);
982 if (error == ENODEV) {
986 if (!strcmp(netdev_dev_get_type(netdev_dev_), "tap")) {
987 netdev->fd = netdev_dev->state.tap.fd;
988 } else if (ethertype != NETDEV_ETH_TYPE_NONE) {
989 struct sockaddr_ll sll;
993 /* Create file descriptor. */
994 protocol = (ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
995 : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
997 netdev->fd = socket(PF_PACKET, SOCK_RAW, htons(protocol));
998 if (netdev->fd < 0) {
1003 /* Set non-blocking mode. */
1004 error = set_nonblocking(netdev->fd);
1009 /* Get ethernet device index. */
1010 error = get_ifindex(&netdev->netdev, &ifindex);
1015 /* Bind to specific ethernet device. */
1016 memset(&sll, 0, sizeof sll);
1017 sll.sll_family = AF_PACKET;
1018 sll.sll_ifindex = ifindex;
1019 if (bind(netdev->fd,
1020 (struct sockaddr *) &sll, sizeof sll) < 0) {
1022 VLOG_ERR("bind to %s failed: %s", netdev_dev_get_name(netdev_dev_),
1027 /* Between the socket() and bind() calls above, the socket receives all
1028 * packets of the requested type on all system interfaces. We do not
1029 * want to receive that data, but there is no way to avoid it. So we
1030 * must now drain out the receive queue. */
1031 error = drain_rcvbuf(netdev->fd);
1037 *netdevp = &netdev->netdev;
1041 netdev_uninit(&netdev->netdev, true);
1045 /* Closes and destroys 'netdev'. */
1047 netdev_linux_close(struct netdev *netdev_)
1049 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1051 if (netdev->fd > 0 && strcmp(netdev_get_type(netdev_), "tap")) {
1057 /* Initializes 'svec' with a list of the names of all known network devices. */
1059 netdev_linux_enumerate(struct svec *svec)
1061 struct if_nameindex *names;
1063 names = if_nameindex();
1067 for (i = 0; names[i].if_name != NULL; i++) {
1068 svec_add(svec, names[i].if_name);
1070 if_freenameindex(names);
1073 VLOG_WARN("could not obtain list of network device names: %s",
1080 netdev_linux_recv(struct netdev *netdev_, void *data, size_t size)
1082 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1084 if (netdev->fd < 0) {
1085 /* Device was opened with NETDEV_ETH_TYPE_NONE. */
1090 ssize_t retval = read(netdev->fd, data, size);
1093 } else if (errno != EINTR) {
1094 if (errno != EAGAIN) {
1095 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
1096 strerror(errno), netdev_get_name(netdev_));
1103 /* Registers with the poll loop to wake up from the next call to poll_block()
1104 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
1106 netdev_linux_recv_wait(struct netdev *netdev_)
1108 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1109 if (netdev->fd >= 0) {
1110 poll_fd_wait(netdev->fd, POLLIN);
1114 /* Discards all packets waiting to be received from 'netdev'. */
1116 netdev_linux_drain(struct netdev *netdev_)
1118 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1119 if (netdev->fd < 0) {
1121 } else if (!strcmp(netdev_get_type(netdev_), "tap")) {
1123 int error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr,
1124 SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
1128 drain_fd(netdev->fd, ifr.ifr_qlen);
1131 return drain_rcvbuf(netdev->fd);
1135 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
1136 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
1137 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
1138 * the packet is too big or too small to transmit on the device.
1140 * The caller retains ownership of 'buffer' in all cases.
1142 * The kernel maintains a packet transmission queue, so the caller is not
1143 * expected to do additional queuing of packets. */
1145 netdev_linux_send(struct netdev *netdev_, const void *data, size_t size)
1147 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1149 /* XXX should support sending even if 'ethertype' was NETDEV_ETH_TYPE_NONE.
1151 if (netdev->fd < 0) {
1156 ssize_t retval = write(netdev->fd, data, size);
1158 /* The Linux AF_PACKET implementation never blocks waiting for room
1159 * for packets, instead returning ENOBUFS. Translate this into
1160 * EAGAIN for the caller. */
1161 if (errno == ENOBUFS) {
1163 } else if (errno == EINTR) {
1165 } else if (errno != EAGAIN) {
1166 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
1167 netdev_get_name(netdev_), strerror(errno));
1170 } else if (retval != size) {
1171 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
1172 "%zu) on %s", retval, size, netdev_get_name(netdev_));
1180 /* Registers with the poll loop to wake up from the next call to poll_block()
1181 * when the packet transmission queue has sufficient room to transmit a packet
1182 * with netdev_send().
1184 * The kernel maintains a packet transmission queue, so the client is not
1185 * expected to do additional queuing of packets. Thus, this function is
1186 * unlikely to ever be used. It is included for completeness. */
1188 netdev_linux_send_wait(struct netdev *netdev_)
1190 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1191 if (netdev->fd < 0) {
1192 /* Nothing to do. */
1193 } else if (strcmp(netdev_get_type(netdev_), "tap")) {
1194 poll_fd_wait(netdev->fd, POLLOUT);
1196 /* TAP device always accepts packets.*/
1197 poll_immediate_wake();
1201 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
1202 * otherwise a positive errno value. */
1204 netdev_linux_set_etheraddr(struct netdev *netdev_,
1205 const uint8_t mac[ETH_ADDR_LEN])
1207 struct netdev_dev_linux *netdev_dev =
1208 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1211 if (!(netdev_dev->cache_valid & VALID_ETHERADDR)
1212 || !eth_addr_equals(netdev_dev->etheraddr, mac)) {
1213 error = set_etheraddr(netdev_get_name(netdev_), ARPHRD_ETHER, mac);
1215 netdev_dev->cache_valid |= VALID_ETHERADDR;
1216 memcpy(netdev_dev->etheraddr, mac, ETH_ADDR_LEN);
1224 /* Returns a pointer to 'netdev''s MAC address. The caller must not modify or
1225 * free the returned buffer. */
1227 netdev_linux_get_etheraddr(const struct netdev *netdev_,
1228 uint8_t mac[ETH_ADDR_LEN])
1230 struct netdev_dev_linux *netdev_dev =
1231 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1232 if (!(netdev_dev->cache_valid & VALID_ETHERADDR)) {
1233 int error = get_etheraddr(netdev_get_name(netdev_),
1234 netdev_dev->etheraddr);
1238 netdev_dev->cache_valid |= VALID_ETHERADDR;
1240 memcpy(mac, netdev_dev->etheraddr, ETH_ADDR_LEN);
1244 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
1245 * in bytes, not including the hardware header; thus, this is typically 1500
1246 * bytes for Ethernet devices. */
1248 netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup)
1250 struct netdev_dev_linux *netdev_dev =
1251 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1252 if (!(netdev_dev->cache_valid & VALID_MTU)) {
1256 error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr,
1257 SIOCGIFMTU, "SIOCGIFMTU");
1261 netdev_dev->mtu = ifr.ifr_mtu;
1262 netdev_dev->cache_valid |= VALID_MTU;
1264 *mtup = netdev_dev->mtu;
1268 /* Returns the ifindex of 'netdev', if successful, as a positive number.
1269 * On failure, returns a negative errno value. */
1271 netdev_linux_get_ifindex(const struct netdev *netdev)
1275 error = get_ifindex(netdev, &ifindex);
1276 return error ? -error : ifindex;
1280 netdev_linux_get_carrier(const struct netdev *netdev_, bool *carrier)
1282 struct netdev_dev_linux *netdev_dev =
1283 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1288 if (!(netdev_dev->cache_valid & VALID_CARRIER)) {
1292 fn = xasprintf("/sys/class/net/%s/carrier",
1293 netdev_get_name(netdev_));
1294 fd = open(fn, O_RDONLY);
1297 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(error));
1301 retval = read(fd, line, sizeof line);
1304 if (error == EINVAL) {
1305 /* This is the normal return value when we try to check carrier
1306 * if the network device is not up. */
1308 VLOG_WARN_RL(&rl, "%s: read failed: %s", fn, strerror(error));
1311 } else if (retval == 0) {
1313 VLOG_WARN_RL(&rl, "%s: unexpected end of file", fn);
1317 if (line[0] != '0' && line[0] != '1') {
1319 VLOG_WARN_RL(&rl, "%s: value is %c (expected 0 or 1)",
1323 netdev_dev->carrier = line[0] != '0';
1324 netdev_dev->cache_valid |= VALID_CARRIER;
1326 *carrier = netdev_dev->carrier;
1337 /* Check whether we can we use RTM_GETLINK to get network device statistics.
1338 * In pre-2.6.19 kernels, this was only available if wireless extensions were
1341 check_for_working_netlink_stats(void)
1343 /* Decide on the netdev_get_stats() implementation to use. Netlink is
1344 * preferable, so if that works, we'll use it. */
1345 int ifindex = do_get_ifindex("lo");
1347 VLOG_WARN("failed to get ifindex for lo, "
1348 "obtaining netdev stats from proc");
1351 struct netdev_stats stats;
1352 int error = get_stats_via_netlink(ifindex, &stats);
1354 VLOG_DBG("obtaining netdev stats via rtnetlink");
1357 VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
1358 "via proc (you are probably running a pre-2.6.19 "
1359 "kernel)", strerror(error));
1365 /* Retrieves current device stats for 'netdev'.
1367 * XXX All of the members of struct netdev_stats are 64 bits wide, but on
1368 * 32-bit architectures the Linux network stats are only 32 bits. */
1370 netdev_linux_get_stats(const struct netdev *netdev_,
1371 struct netdev_stats *stats)
1373 struct netdev_dev_linux *netdev_dev =
1374 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1375 static int use_netlink_stats = -1;
1377 struct netdev_stats raw_stats;
1378 struct netdev_stats *collect_stats = stats;
1380 COVERAGE_INC(netdev_get_stats);
1382 if (!(netdev_dev->cache_valid & VALID_IS_INTERNAL)) {
1383 netdev_dev->is_internal = !strcmp(netdev_get_type(netdev_), "tap");
1384 if (!netdev_dev->is_internal) {
1385 struct ethtool_drvinfo drvinfo;
1387 memset(&drvinfo, 0, sizeof drvinfo);
1388 error = netdev_linux_do_ethtool(netdev_get_name(netdev_),
1389 (struct ethtool_cmd *)&drvinfo,
1391 "ETHTOOL_GDRVINFO");
1394 netdev_dev->is_internal = !strcmp(drvinfo.driver,
1399 netdev_dev->cache_valid |= VALID_IS_INTERNAL;
1402 if (netdev_dev->is_internal) {
1403 collect_stats = &raw_stats;
1406 if (use_netlink_stats < 0) {
1407 use_netlink_stats = check_for_working_netlink_stats();
1409 if (use_netlink_stats) {
1412 error = get_ifindex(netdev_, &ifindex);
1414 error = get_stats_via_netlink(ifindex, collect_stats);
1417 error = get_stats_via_proc(netdev_get_name(netdev_), collect_stats);
1420 /* If this port is an internal port then the transmit and receive stats
1421 * will appear to be swapped relative to the other ports since we are the
1422 * one sending the data, not a remote computer. For consistency, we swap
1423 * them back here. */
1424 if (!error && netdev_dev->is_internal) {
1425 stats->rx_packets = raw_stats.tx_packets;
1426 stats->tx_packets = raw_stats.rx_packets;
1427 stats->rx_bytes = raw_stats.tx_bytes;
1428 stats->tx_bytes = raw_stats.rx_bytes;
1429 stats->rx_errors = raw_stats.tx_errors;
1430 stats->tx_errors = raw_stats.rx_errors;
1431 stats->rx_dropped = raw_stats.tx_dropped;
1432 stats->tx_dropped = raw_stats.rx_dropped;
1433 stats->multicast = raw_stats.multicast;
1434 stats->collisions = raw_stats.collisions;
1435 stats->rx_length_errors = 0;
1436 stats->rx_over_errors = 0;
1437 stats->rx_crc_errors = 0;
1438 stats->rx_frame_errors = 0;
1439 stats->rx_fifo_errors = 0;
1440 stats->rx_missed_errors = 0;
1441 stats->tx_aborted_errors = 0;
1442 stats->tx_carrier_errors = 0;
1443 stats->tx_fifo_errors = 0;
1444 stats->tx_heartbeat_errors = 0;
1445 stats->tx_window_errors = 0;
1451 /* Stores the features supported by 'netdev' into each of '*current',
1452 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1453 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1454 * successful, otherwise a positive errno value. */
1456 netdev_linux_get_features(struct netdev *netdev,
1457 uint32_t *current, uint32_t *advertised,
1458 uint32_t *supported, uint32_t *peer)
1460 struct ethtool_cmd ecmd;
1463 memset(&ecmd, 0, sizeof ecmd);
1464 error = netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd,
1465 ETHTOOL_GSET, "ETHTOOL_GSET");
1470 /* Supported features. */
1472 if (ecmd.supported & SUPPORTED_10baseT_Half) {
1473 *supported |= OFPPF_10MB_HD;
1475 if (ecmd.supported & SUPPORTED_10baseT_Full) {
1476 *supported |= OFPPF_10MB_FD;
1478 if (ecmd.supported & SUPPORTED_100baseT_Half) {
1479 *supported |= OFPPF_100MB_HD;
1481 if (ecmd.supported & SUPPORTED_100baseT_Full) {
1482 *supported |= OFPPF_100MB_FD;
1484 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
1485 *supported |= OFPPF_1GB_HD;
1487 if (ecmd.supported & SUPPORTED_1000baseT_Full) {
1488 *supported |= OFPPF_1GB_FD;
1490 if (ecmd.supported & SUPPORTED_10000baseT_Full) {
1491 *supported |= OFPPF_10GB_FD;
1493 if (ecmd.supported & SUPPORTED_TP) {
1494 *supported |= OFPPF_COPPER;
1496 if (ecmd.supported & SUPPORTED_FIBRE) {
1497 *supported |= OFPPF_FIBER;
1499 if (ecmd.supported & SUPPORTED_Autoneg) {
1500 *supported |= OFPPF_AUTONEG;
1502 if (ecmd.supported & SUPPORTED_Pause) {
1503 *supported |= OFPPF_PAUSE;
1505 if (ecmd.supported & SUPPORTED_Asym_Pause) {
1506 *supported |= OFPPF_PAUSE_ASYM;
1509 /* Advertised features. */
1511 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
1512 *advertised |= OFPPF_10MB_HD;
1514 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
1515 *advertised |= OFPPF_10MB_FD;
1517 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
1518 *advertised |= OFPPF_100MB_HD;
1520 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
1521 *advertised |= OFPPF_100MB_FD;
1523 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
1524 *advertised |= OFPPF_1GB_HD;
1526 if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
1527 *advertised |= OFPPF_1GB_FD;
1529 if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
1530 *advertised |= OFPPF_10GB_FD;
1532 if (ecmd.advertising & ADVERTISED_TP) {
1533 *advertised |= OFPPF_COPPER;
1535 if (ecmd.advertising & ADVERTISED_FIBRE) {
1536 *advertised |= OFPPF_FIBER;
1538 if (ecmd.advertising & ADVERTISED_Autoneg) {
1539 *advertised |= OFPPF_AUTONEG;
1541 if (ecmd.advertising & ADVERTISED_Pause) {
1542 *advertised |= OFPPF_PAUSE;
1544 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
1545 *advertised |= OFPPF_PAUSE_ASYM;
1548 /* Current settings. */
1549 if (ecmd.speed == SPEED_10) {
1550 *current = ecmd.duplex ? OFPPF_10MB_FD : OFPPF_10MB_HD;
1551 } else if (ecmd.speed == SPEED_100) {
1552 *current = ecmd.duplex ? OFPPF_100MB_FD : OFPPF_100MB_HD;
1553 } else if (ecmd.speed == SPEED_1000) {
1554 *current = ecmd.duplex ? OFPPF_1GB_FD : OFPPF_1GB_HD;
1555 } else if (ecmd.speed == SPEED_10000) {
1556 *current = OFPPF_10GB_FD;
1561 if (ecmd.port == PORT_TP) {
1562 *current |= OFPPF_COPPER;
1563 } else if (ecmd.port == PORT_FIBRE) {
1564 *current |= OFPPF_FIBER;
1568 *current |= OFPPF_AUTONEG;
1571 /* Peer advertisements. */
1572 *peer = 0; /* XXX */
1577 /* Set the features advertised by 'netdev' to 'advertise'. */
1579 netdev_linux_set_advertisements(struct netdev *netdev, uint32_t advertise)
1581 struct ethtool_cmd ecmd;
1584 memset(&ecmd, 0, sizeof ecmd);
1585 error = netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd,
1586 ETHTOOL_GSET, "ETHTOOL_GSET");
1591 ecmd.advertising = 0;
1592 if (advertise & OFPPF_10MB_HD) {
1593 ecmd.advertising |= ADVERTISED_10baseT_Half;
1595 if (advertise & OFPPF_10MB_FD) {
1596 ecmd.advertising |= ADVERTISED_10baseT_Full;
1598 if (advertise & OFPPF_100MB_HD) {
1599 ecmd.advertising |= ADVERTISED_100baseT_Half;
1601 if (advertise & OFPPF_100MB_FD) {
1602 ecmd.advertising |= ADVERTISED_100baseT_Full;
1604 if (advertise & OFPPF_1GB_HD) {
1605 ecmd.advertising |= ADVERTISED_1000baseT_Half;
1607 if (advertise & OFPPF_1GB_FD) {
1608 ecmd.advertising |= ADVERTISED_1000baseT_Full;
1610 if (advertise & OFPPF_10GB_FD) {
1611 ecmd.advertising |= ADVERTISED_10000baseT_Full;
1613 if (advertise & OFPPF_COPPER) {
1614 ecmd.advertising |= ADVERTISED_TP;
1616 if (advertise & OFPPF_FIBER) {
1617 ecmd.advertising |= ADVERTISED_FIBRE;
1619 if (advertise & OFPPF_AUTONEG) {
1620 ecmd.advertising |= ADVERTISED_Autoneg;
1622 if (advertise & OFPPF_PAUSE) {
1623 ecmd.advertising |= ADVERTISED_Pause;
1625 if (advertise & OFPPF_PAUSE_ASYM) {
1626 ecmd.advertising |= ADVERTISED_Asym_Pause;
1628 return netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd,
1629 ETHTOOL_SSET, "ETHTOOL_SSET");
1632 /* If 'netdev_name' is the name of a VLAN network device (e.g. one created with
1633 * vconfig(8)), sets '*vlan_vid' to the VLAN VID associated with that device
1634 * and returns 0. Otherwise returns a errno value (specifically ENOENT if
1635 * 'netdev_name' is the name of a network device that is not a VLAN device) and
1636 * sets '*vlan_vid' to -1. */
1638 netdev_linux_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
1640 const char *netdev_name = netdev_get_name(netdev);
1641 struct ds line = DS_EMPTY_INITIALIZER;
1642 FILE *stream = NULL;
1646 COVERAGE_INC(netdev_get_vlan_vid);
1647 fn = xasprintf("/proc/net/vlan/%s", netdev_name);
1648 stream = fopen(fn, "r");
1654 if (ds_get_line(&line, stream)) {
1655 if (ferror(stream)) {
1657 VLOG_ERR_RL(&rl, "error reading \"%s\": %s", fn, strerror(errno));
1660 VLOG_ERR_RL(&rl, "unexpected end of file reading \"%s\"", fn);
1665 if (!sscanf(ds_cstr(&line), "%*s VID: %d", vlan_vid)) {
1667 VLOG_ERR_RL(&rl, "parse error reading \"%s\" line 1: \"%s\"",
1668 fn, ds_cstr(&line));
1686 #define POLICE_ADD_CMD "/sbin/tc qdisc add dev %s handle ffff: ingress"
1687 #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"
1688 /* We redirect stderr to /dev/null because we often want to remove all
1689 * traffic control configuration on a port so its in a known state. If
1690 * this done when there is no such configuration, tc complains, so we just
1693 #define POLICE_DEL_CMD "/sbin/tc qdisc del dev %s handle ffff: ingress 2>/dev/null"
1695 /* Attempts to set input rate limiting (policing) policy. */
1697 netdev_linux_set_policing(struct netdev *netdev,
1698 uint32_t kbits_rate, uint32_t kbits_burst)
1700 const char *netdev_name = netdev_get_name(netdev);
1703 COVERAGE_INC(netdev_set_policing);
1706 /* Default to 1000 kilobits if not specified. */
1710 /* xxx This should be more careful about only adding if it
1711 * xxx actually exists, as opposed to always deleting it. */
1712 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
1713 if (system(command) == -1) {
1714 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
1717 snprintf(command, sizeof(command), POLICE_ADD_CMD, netdev_name);
1718 if (system(command) != 0) {
1719 VLOG_WARN_RL(&rl, "%s: problem adding policing", netdev_name);
1723 snprintf(command, sizeof(command), POLICE_CONFIG_CMD, netdev_name,
1724 kbits_rate, kbits_burst);
1725 if (system(command) != 0) {
1726 VLOG_WARN_RL(&rl, "%s: problem configuring policing",
1731 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
1732 if (system(command) == -1) {
1733 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
1741 netdev_linux_get_in4(const struct netdev *netdev_,
1742 struct in_addr *address, struct in_addr *netmask)
1744 struct netdev_dev_linux *netdev_dev =
1745 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1747 if (!(netdev_dev->cache_valid & VALID_IN4)) {
1750 error = netdev_linux_get_ipv4(netdev_, &netdev_dev->address,
1751 SIOCGIFADDR, "SIOCGIFADDR");
1756 error = netdev_linux_get_ipv4(netdev_, &netdev_dev->netmask,
1757 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1762 netdev_dev->cache_valid |= VALID_IN4;
1764 *address = netdev_dev->address;
1765 *netmask = netdev_dev->netmask;
1766 return address->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1770 netdev_linux_set_in4(struct netdev *netdev_, struct in_addr address,
1771 struct in_addr netmask)
1773 struct netdev_dev_linux *netdev_dev =
1774 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1777 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", address);
1779 netdev_dev->cache_valid |= VALID_IN4;
1780 netdev_dev->address = address;
1781 netdev_dev->netmask = netmask;
1782 if (address.s_addr != INADDR_ANY) {
1783 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1784 "SIOCSIFNETMASK", netmask);
1791 parse_if_inet6_line(const char *line,
1792 struct in6_addr *in6, char ifname[16 + 1])
1794 uint8_t *s6 = in6->s6_addr;
1795 #define X8 "%2"SCNx8
1797 " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
1798 "%*x %*x %*x %*x %16s\n",
1799 &s6[0], &s6[1], &s6[2], &s6[3],
1800 &s6[4], &s6[5], &s6[6], &s6[7],
1801 &s6[8], &s6[9], &s6[10], &s6[11],
1802 &s6[12], &s6[13], &s6[14], &s6[15],
1806 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
1807 * 'in6' is non-null) and returns true. Otherwise, returns false. */
1809 netdev_linux_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1811 struct netdev_dev_linux *netdev_dev =
1812 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1813 if (!(netdev_dev->cache_valid & VALID_IN6)) {
1817 netdev_dev->in6 = in6addr_any;
1819 file = fopen("/proc/net/if_inet6", "r");
1821 const char *name = netdev_get_name(netdev_);
1822 while (fgets(line, sizeof line, file)) {
1823 struct in6_addr in6;
1824 char ifname[16 + 1];
1825 if (parse_if_inet6_line(line, &in6, ifname)
1826 && !strcmp(name, ifname))
1828 netdev_dev->in6 = in6;
1834 netdev_dev->cache_valid |= VALID_IN6;
1836 *in6 = netdev_dev->in6;
1841 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1843 struct sockaddr_in sin;
1844 memset(&sin, 0, sizeof sin);
1845 sin.sin_family = AF_INET;
1846 sin.sin_addr = addr;
1849 memset(sa, 0, sizeof *sa);
1850 memcpy(sa, &sin, sizeof sin);
1854 do_set_addr(struct netdev *netdev,
1855 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1858 strncpy(ifr.ifr_name, netdev_get_name(netdev), sizeof ifr.ifr_name);
1859 make_in4_sockaddr(&ifr.ifr_addr, addr);
1861 return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, ioctl_nr,
1865 /* Adds 'router' as a default IP gateway. */
1867 netdev_linux_add_router(struct netdev *netdev OVS_UNUSED, struct in_addr router)
1869 struct in_addr any = { INADDR_ANY };
1873 memset(&rt, 0, sizeof rt);
1874 make_in4_sockaddr(&rt.rt_dst, any);
1875 make_in4_sockaddr(&rt.rt_gateway, router);
1876 make_in4_sockaddr(&rt.rt_genmask, any);
1877 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1878 COVERAGE_INC(netdev_add_router);
1879 error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
1881 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
1887 netdev_linux_get_next_hop(const struct in_addr *host, struct in_addr *next_hop,
1890 static const char fn[] = "/proc/net/route";
1895 *netdev_name = NULL;
1896 stream = fopen(fn, "r");
1897 if (stream == NULL) {
1898 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1903 while (fgets(line, sizeof line, stream)) {
1906 uint32_t dest, gateway, mask;
1907 int refcnt, metric, mtu;
1908 unsigned int flags, use, window, irtt;
1911 "%16s %"SCNx32" %"SCNx32" %04X %d %u %d %"SCNx32
1913 iface, &dest, &gateway, &flags, &refcnt,
1914 &use, &metric, &mask, &mtu, &window, &irtt) != 11) {
1916 VLOG_WARN_RL(&rl, "%s: could not parse line %d: %s",
1920 if (!(flags & RTF_UP)) {
1921 /* Skip routes that aren't up. */
1925 /* The output of 'dest', 'mask', and 'gateway' were given in
1926 * network byte order, so we don't need need any endian
1927 * conversions here. */
1928 if ((dest & mask) == (host->s_addr & mask)) {
1930 /* The host is directly reachable. */
1931 next_hop->s_addr = 0;
1933 /* To reach the host, we must go through a gateway. */
1934 next_hop->s_addr = gateway;
1936 *netdev_name = xstrdup(iface);
1947 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1948 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1949 * returns 0. Otherwise, it returns a positive errno value; in particular,
1950 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
1952 netdev_linux_arp_lookup(const struct netdev *netdev,
1953 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
1956 struct sockaddr_in sin;
1959 memset(&r, 0, sizeof r);
1960 sin.sin_family = AF_INET;
1961 sin.sin_addr.s_addr = ip;
1963 memcpy(&r.arp_pa, &sin, sizeof sin);
1964 r.arp_ha.sa_family = ARPHRD_ETHER;
1966 strncpy(r.arp_dev, netdev_get_name(netdev), sizeof r.arp_dev);
1967 COVERAGE_INC(netdev_arp_lookup);
1968 retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
1970 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
1971 } else if (retval != ENXIO) {
1972 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
1973 netdev_get_name(netdev), IP_ARGS(&ip), strerror(retval));
1979 nd_to_iff_flags(enum netdev_flags nd)
1982 if (nd & NETDEV_UP) {
1985 if (nd & NETDEV_PROMISC) {
1992 iff_to_nd_flags(int iff)
1994 enum netdev_flags nd = 0;
1998 if (iff & IFF_PROMISC) {
1999 nd |= NETDEV_PROMISC;
2005 netdev_linux_update_flags(struct netdev *netdev, enum netdev_flags off,
2006 enum netdev_flags on, enum netdev_flags *old_flagsp)
2008 int old_flags, new_flags;
2011 error = get_flags(netdev, &old_flags);
2013 *old_flagsp = iff_to_nd_flags(old_flags);
2014 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
2015 if (new_flags != old_flags) {
2016 error = set_flags(netdev, new_flags);
2023 poll_notify(struct list *list)
2025 struct netdev_linux_notifier *notifier;
2026 LIST_FOR_EACH (notifier, struct netdev_linux_notifier, node, list) {
2027 struct netdev_notifier *n = ¬ifier->notifier;
2033 netdev_linux_poll_cb(const struct rtnetlink_change *change,
2034 void *aux OVS_UNUSED)
2037 struct list *list = shash_find_data(&netdev_linux_notifiers,
2043 struct shash_node *node;
2044 SHASH_FOR_EACH (node, &netdev_linux_notifiers) {
2045 poll_notify(node->data);
2051 netdev_linux_poll_add(struct netdev *netdev,
2052 void (*cb)(struct netdev_notifier *), void *aux,
2053 struct netdev_notifier **notifierp)
2055 const char *netdev_name = netdev_get_name(netdev);
2056 struct netdev_linux_notifier *notifier;
2059 if (shash_is_empty(&netdev_linux_notifiers)) {
2060 int error = rtnetlink_notifier_register(&netdev_linux_poll_notifier,
2061 netdev_linux_poll_cb, NULL);
2067 list = shash_find_data(&netdev_linux_notifiers, netdev_name);
2069 list = xmalloc(sizeof *list);
2071 shash_add(&netdev_linux_notifiers, netdev_name, list);
2074 notifier = xmalloc(sizeof *notifier);
2075 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
2076 list_push_back(list, ¬ifier->node);
2077 *notifierp = ¬ifier->notifier;
2082 netdev_linux_poll_remove(struct netdev_notifier *notifier_)
2084 struct netdev_linux_notifier *notifier =
2085 CONTAINER_OF(notifier_, struct netdev_linux_notifier, notifier);
2088 /* Remove 'notifier' from its list. */
2089 list = list_remove(¬ifier->node);
2090 if (list_is_empty(list)) {
2091 /* The list is now empty. Remove it from the hash and free it. */
2092 const char *netdev_name = netdev_get_name(notifier->notifier.netdev);
2093 shash_delete(&netdev_linux_notifiers,
2094 shash_find(&netdev_linux_notifiers, netdev_name));
2099 /* If that was the last notifier, unregister. */
2100 if (shash_is_empty(&netdev_linux_notifiers)) {
2101 rtnetlink_notifier_unregister(&netdev_linux_poll_notifier);
2105 const struct netdev_class netdev_linux_class = {
2112 netdev_linux_create_system,
2113 netdev_linux_destroy,
2114 NULL, /* reconfigure */
2119 netdev_linux_enumerate,
2122 netdev_linux_recv_wait,
2126 netdev_linux_send_wait,
2128 netdev_linux_set_etheraddr,
2129 netdev_linux_get_etheraddr,
2130 netdev_linux_get_mtu,
2131 netdev_linux_get_ifindex,
2132 netdev_linux_get_carrier,
2133 netdev_linux_get_stats,
2135 netdev_linux_get_features,
2136 netdev_linux_set_advertisements,
2137 netdev_linux_get_vlan_vid,
2138 netdev_linux_set_policing,
2140 netdev_linux_get_in4,
2141 netdev_linux_set_in4,
2142 netdev_linux_get_in6,
2143 netdev_linux_add_router,
2144 netdev_linux_get_next_hop,
2145 netdev_linux_arp_lookup,
2147 netdev_linux_update_flags,
2149 netdev_linux_poll_add,
2150 netdev_linux_poll_remove,
2153 const struct netdev_class netdev_tap_class = {
2160 netdev_linux_create_tap,
2161 netdev_linux_destroy,
2162 NULL, /* reconfigure */
2167 NULL, /* enumerate */
2170 netdev_linux_recv_wait,
2174 netdev_linux_send_wait,
2176 netdev_linux_set_etheraddr,
2177 netdev_linux_get_etheraddr,
2178 netdev_linux_get_mtu,
2179 netdev_linux_get_ifindex,
2180 netdev_linux_get_carrier,
2181 netdev_linux_get_stats,
2183 netdev_linux_get_features,
2184 netdev_linux_set_advertisements,
2185 netdev_linux_get_vlan_vid,
2186 netdev_linux_set_policing,
2188 netdev_linux_get_in4,
2189 netdev_linux_set_in4,
2190 netdev_linux_get_in6,
2191 netdev_linux_add_router,
2192 netdev_linux_get_next_hop,
2193 netdev_linux_arp_lookup,
2195 netdev_linux_update_flags,
2197 netdev_linux_poll_add,
2198 netdev_linux_poll_remove,
2201 const struct netdev_class netdev_gre_class = {
2208 netdev_linux_create_gre,
2209 netdev_linux_destroy,
2210 netdev_linux_reconfigure_gre,
2215 NULL, /* enumerate */
2218 netdev_linux_recv_wait,
2222 netdev_linux_send_wait,
2224 netdev_linux_set_etheraddr,
2225 netdev_linux_get_etheraddr,
2226 netdev_linux_get_mtu,
2227 netdev_linux_get_ifindex,
2228 netdev_linux_get_carrier,
2229 netdev_linux_get_stats,
2231 netdev_linux_get_features,
2232 netdev_linux_set_advertisements,
2233 netdev_linux_get_vlan_vid,
2234 netdev_linux_set_policing,
2236 netdev_linux_get_in4,
2237 netdev_linux_set_in4,
2238 netdev_linux_get_in6,
2239 netdev_linux_add_router,
2240 netdev_linux_get_next_hop,
2241 netdev_linux_arp_lookup,
2243 netdev_linux_update_flags,
2245 netdev_linux_poll_add,
2246 netdev_linux_poll_remove,
2249 const struct netdev_class netdev_patch_class = {
2256 netdev_linux_create_patch,
2257 netdev_linux_destroy,
2258 NULL, /* reconfigure */
2263 NULL, /* enumerate */
2266 netdev_linux_recv_wait,
2270 netdev_linux_send_wait,
2272 netdev_linux_set_etheraddr,
2273 netdev_linux_get_etheraddr,
2274 netdev_linux_get_mtu,
2275 netdev_linux_get_ifindex,
2276 netdev_linux_get_carrier,
2277 netdev_linux_get_stats,
2279 netdev_linux_get_features,
2280 netdev_linux_set_advertisements,
2281 netdev_linux_get_vlan_vid,
2282 netdev_linux_set_policing,
2284 netdev_linux_get_in4,
2285 netdev_linux_set_in4,
2286 netdev_linux_get_in6,
2287 netdev_linux_add_router,
2288 netdev_linux_get_next_hop,
2289 netdev_linux_arp_lookup,
2291 netdev_linux_update_flags,
2293 netdev_linux_poll_add,
2294 netdev_linux_poll_remove,
2298 get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
2300 /* Policy for RTNLGRP_LINK messages.
2302 * There are *many* more fields in these messages, but currently we only
2303 * care about these fields. */
2304 static const struct nl_policy rtnlgrp_link_policy[] = {
2305 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
2306 [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
2307 .min_len = sizeof(struct rtnl_link_stats) },
2311 static struct nl_sock *rtnl_sock;
2312 struct ofpbuf request;
2313 struct ofpbuf *reply;
2314 struct ifinfomsg *ifi;
2315 const struct rtnl_link_stats *rtnl_stats;
2316 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
2320 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0, &rtnl_sock);
2322 VLOG_ERR_RL(&rl, "failed to create rtnetlink socket: %s",
2328 ofpbuf_init(&request, 0);
2329 nl_msg_put_nlmsghdr(&request, rtnl_sock, sizeof *ifi,
2330 RTM_GETLINK, NLM_F_REQUEST);
2331 ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
2332 ifi->ifi_family = PF_UNSPEC;
2333 ifi->ifi_index = ifindex;
2334 error = nl_sock_transact(rtnl_sock, &request, &reply);
2335 ofpbuf_uninit(&request);
2340 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
2341 rtnlgrp_link_policy,
2342 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
2343 ofpbuf_delete(reply);
2347 if (!attrs[IFLA_STATS]) {
2348 VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
2349 ofpbuf_delete(reply);
2353 rtnl_stats = nl_attr_get(attrs[IFLA_STATS]);
2354 stats->rx_packets = rtnl_stats->rx_packets;
2355 stats->tx_packets = rtnl_stats->tx_packets;
2356 stats->rx_bytes = rtnl_stats->rx_bytes;
2357 stats->tx_bytes = rtnl_stats->tx_bytes;
2358 stats->rx_errors = rtnl_stats->rx_errors;
2359 stats->tx_errors = rtnl_stats->tx_errors;
2360 stats->rx_dropped = rtnl_stats->rx_dropped;
2361 stats->tx_dropped = rtnl_stats->tx_dropped;
2362 stats->multicast = rtnl_stats->multicast;
2363 stats->collisions = rtnl_stats->collisions;
2364 stats->rx_length_errors = rtnl_stats->rx_length_errors;
2365 stats->rx_over_errors = rtnl_stats->rx_over_errors;
2366 stats->rx_crc_errors = rtnl_stats->rx_crc_errors;
2367 stats->rx_frame_errors = rtnl_stats->rx_frame_errors;
2368 stats->rx_fifo_errors = rtnl_stats->rx_fifo_errors;
2369 stats->rx_missed_errors = rtnl_stats->rx_missed_errors;
2370 stats->tx_aborted_errors = rtnl_stats->tx_aborted_errors;
2371 stats->tx_carrier_errors = rtnl_stats->tx_carrier_errors;
2372 stats->tx_fifo_errors = rtnl_stats->tx_fifo_errors;
2373 stats->tx_heartbeat_errors = rtnl_stats->tx_heartbeat_errors;
2374 stats->tx_window_errors = rtnl_stats->tx_window_errors;
2376 ofpbuf_delete(reply);
2382 get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats)
2384 static const char fn[] = "/proc/net/dev";
2389 stream = fopen(fn, "r");
2391 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
2396 while (fgets(line, sizeof line, stream)) {
2399 #define X64 "%"SCNu64
2402 X64 X64 X64 X64 X64 X64 X64 "%*u"
2403 X64 X64 X64 X64 X64 X64 X64 "%*u",
2409 &stats->rx_fifo_errors,
2410 &stats->rx_frame_errors,
2416 &stats->tx_fifo_errors,
2418 &stats->tx_carrier_errors) != 15) {
2419 VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln);
2420 } else if (!strcmp(devname, netdev_name)) {
2421 stats->rx_length_errors = UINT64_MAX;
2422 stats->rx_over_errors = UINT64_MAX;
2423 stats->rx_crc_errors = UINT64_MAX;
2424 stats->rx_missed_errors = UINT64_MAX;
2425 stats->tx_aborted_errors = UINT64_MAX;
2426 stats->tx_heartbeat_errors = UINT64_MAX;
2427 stats->tx_window_errors = UINT64_MAX;
2433 VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name);
2439 get_flags(const struct netdev *netdev, int *flags)
2444 error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, SIOCGIFFLAGS,
2446 *flags = ifr.ifr_flags;
2451 set_flags(struct netdev *netdev, int flags)
2455 ifr.ifr_flags = flags;
2456 return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, SIOCSIFFLAGS,
2461 do_get_ifindex(const char *netdev_name)
2465 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
2466 COVERAGE_INC(netdev_get_ifindex);
2467 if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) {
2468 VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
2469 netdev_name, strerror(errno));
2472 return ifr.ifr_ifindex;
2476 get_ifindex(const struct netdev *netdev_, int *ifindexp)
2478 struct netdev_dev_linux *netdev_dev =
2479 netdev_dev_linux_cast(netdev_get_dev(netdev_));
2481 if (!(netdev_dev->cache_valid & VALID_IFINDEX)) {
2482 int ifindex = do_get_ifindex(netdev_get_name(netdev_));
2486 netdev_dev->cache_valid |= VALID_IFINDEX;
2487 netdev_dev->ifindex = ifindex;
2489 *ifindexp = netdev_dev->ifindex;
2494 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
2499 memset(&ifr, 0, sizeof ifr);
2500 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
2501 COVERAGE_INC(netdev_get_hwaddr);
2502 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
2503 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
2504 netdev_name, strerror(errno));
2507 hwaddr_family = ifr.ifr_hwaddr.sa_family;
2508 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
2509 VLOG_WARN("%s device has unknown hardware address family %d",
2510 netdev_name, hwaddr_family);
2512 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
2517 set_etheraddr(const char *netdev_name, int hwaddr_family,
2518 const uint8_t mac[ETH_ADDR_LEN])
2522 memset(&ifr, 0, sizeof ifr);
2523 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
2524 ifr.ifr_hwaddr.sa_family = hwaddr_family;
2525 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
2526 COVERAGE_INC(netdev_set_hwaddr);
2527 if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) {
2528 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
2529 netdev_name, strerror(errno));
2536 netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *ecmd,
2537 int cmd, const char *cmd_name)
2541 memset(&ifr, 0, sizeof ifr);
2542 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
2543 ifr.ifr_data = (caddr_t) ecmd;
2546 COVERAGE_INC(netdev_ethtool);
2547 if (ioctl(af_inet_sock, SIOCETHTOOL, &ifr) == 0) {
2550 if (errno != EOPNOTSUPP) {
2551 VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
2552 "failed: %s", cmd_name, name, strerror(errno));
2554 /* The device doesn't support this operation. That's pretty
2555 * common, so there's no point in logging anything. */
2562 netdev_linux_do_ioctl(const char *name, struct ifreq *ifr, int cmd,
2563 const char *cmd_name)
2565 strncpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
2566 if (ioctl(af_inet_sock, cmd, ifr) == -1) {
2567 VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
2575 netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip,
2576 int cmd, const char *cmd_name)
2581 ifr.ifr_addr.sa_family = AF_INET;
2582 error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name);
2584 const struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
2585 *ip = sin->sin_addr;