2 * Copyright (c) 2008, 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.
23 #include <arpa/inet.h>
25 #include <linux/if_tun.h>
26 #include <linux/types.h>
27 #include <linux/ethtool.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/sockios.h>
30 #include <linux/version.h>
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <netpacket/packet.h>
35 #include <net/ethernet.h>
37 #include <net/if_arp.h>
38 #include <net/if_packet.h>
39 #include <net/route.h>
40 #include <netinet/in.h>
46 #include "dynamic-string.h"
47 #include "fatal-signal.h"
49 #include "netdev-linux.h"
52 #include "openflow/openflow.h"
54 #include "poll-loop.h"
56 #include "socket-util.h"
59 /* linux/if.h defines IFF_LOWER_UP, net/if.h doesn't.
60 * net/if.h defines if_nameindex(), linux/if.h doesn't.
61 * We can't include both headers, so define IFF_LOWER_UP ourselves. */
63 #define IFF_LOWER_UP 0x10000
66 /* These were introduced in Linux 2.6.14, so they might be missing if we have
68 #ifndef ADVERTISED_Pause
69 #define ADVERTISED_Pause (1 << 13)
71 #ifndef ADVERTISED_Asym_Pause
72 #define ADVERTISED_Asym_Pause (1 << 14)
75 #define THIS_MODULE VLM_netdev
82 /* File descriptors. For ordinary network devices, the two fds below are
83 * the same; for tap devices, they differ. */
84 int netdev_fd; /* Network device. */
85 int tap_fd; /* TAP character device, if any, otherwise the
88 /* Cached network device information. */
89 int ifindex; /* -1 if not known. */
90 uint8_t etheraddr[ETH_ADDR_LEN];
97 int save_flags; /* Initial device flags. */
98 int changed_flags; /* Flags that we changed. */
101 /* Policy for RTNLGRP_LINK messages.
103 * There are *many* more fields in these messages, but currently we only care
104 * about interface names. */
105 static const struct nl_policy rtnlgrp_link_policy[] = {
106 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
107 [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
108 .min_len = sizeof(struct rtnl_link_stats) },
111 /* All open network devices. */
112 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
114 /* An AF_INET socket (used for ioctl operations). */
115 static int af_inet_sock = -1;
117 /* NETLINK_ROUTE socket. */
118 static struct nl_sock *rtnl_sock;
120 /* Can we use RTM_GETLINK to get network device statistics? (In pre-2.6.19
121 * kernels, this was only available if wireless extensions were enabled.) */
122 static bool use_netlink_stats;
124 /* This is set pretty low because we probably won't learn anything from the
125 * additional log messages. */
126 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
128 static void init_netdev(void);
129 static int do_open_netdev(const char *name, int ethertype, int tap_fd,
130 struct netdev **netdev_);
131 static int restore_flags(struct netdev *netdev);
132 static int get_flags(const char *netdev_name, int *flagsp);
133 static int set_flags(const char *netdev_name, int flags);
134 static int do_get_ifindex(const char *netdev_name);
135 static int get_ifindex(const struct netdev *, int *ifindexp);
136 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN],
137 int *hwaddr_familyp);
138 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
139 const uint8_t[ETH_ADDR_LEN]);
141 /* Obtains the IPv6 address for 'name' into 'in6'. */
143 get_ipv6_address(const char *name, struct in6_addr *in6)
148 file = fopen("/proc/net/if_inet6", "r");
150 /* This most likely indicates that the host doesn't have IPv6 support,
151 * so it's not really a failure condition.*/
156 while (fgets(line, sizeof line, file)) {
157 uint8_t *s6 = in6->s6_addr;
161 if (sscanf(line, " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
162 "%*x %*x %*x %*x %16s\n",
163 &s6[0], &s6[1], &s6[2], &s6[3],
164 &s6[4], &s6[5], &s6[6], &s6[7],
165 &s6[8], &s6[9], &s6[10], &s6[11],
166 &s6[12], &s6[13], &s6[14], &s6[15],
168 && !strcmp(name, ifname))
180 do_ethtool(struct netdev *netdev, struct ethtool_cmd *ecmd,
181 int cmd, const char *cmd_name)
185 memset(&ifr, 0, sizeof ifr);
186 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
187 ifr.ifr_data = (caddr_t) ecmd;
190 COVERAGE_INC(netdev_ethtool);
191 if (ioctl(netdev->netdev_fd, SIOCETHTOOL, &ifr) == 0) {
194 if (errno != EOPNOTSUPP) {
195 VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
196 "failed: %s", cmd_name, netdev->name,
199 /* The device doesn't support this operation. That's pretty
200 * common, so there's no point in logging anything. */
207 do_get_features(struct netdev *netdev,
208 uint32_t *current, uint32_t *advertised,
209 uint32_t *supported, uint32_t *peer)
211 struct ethtool_cmd ecmd;
219 memset(&ecmd, 0, sizeof ecmd);
220 error = do_ethtool(netdev, &ecmd, ETHTOOL_GSET, "ETHTOOL_GSET");
225 if (ecmd.supported & SUPPORTED_10baseT_Half) {
226 *supported |= OFPPF_10MB_HD;
228 if (ecmd.supported & SUPPORTED_10baseT_Full) {
229 *supported |= OFPPF_10MB_FD;
231 if (ecmd.supported & SUPPORTED_100baseT_Half) {
232 *supported |= OFPPF_100MB_HD;
234 if (ecmd.supported & SUPPORTED_100baseT_Full) {
235 *supported |= OFPPF_100MB_FD;
237 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
238 *supported |= OFPPF_1GB_HD;
240 if (ecmd.supported & SUPPORTED_1000baseT_Full) {
241 *supported |= OFPPF_1GB_FD;
243 if (ecmd.supported & SUPPORTED_10000baseT_Full) {
244 *supported |= OFPPF_10GB_FD;
246 if (ecmd.supported & SUPPORTED_TP) {
247 *supported |= OFPPF_COPPER;
249 if (ecmd.supported & SUPPORTED_FIBRE) {
250 *supported |= OFPPF_FIBER;
252 if (ecmd.supported & SUPPORTED_Autoneg) {
253 *supported |= OFPPF_AUTONEG;
255 if (ecmd.supported & SUPPORTED_Pause) {
256 *supported |= OFPPF_PAUSE;
258 if (ecmd.supported & SUPPORTED_Asym_Pause) {
259 *supported |= OFPPF_PAUSE_ASYM;
262 /* Set the advertised features */
263 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
264 *advertised |= OFPPF_10MB_HD;
266 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
267 *advertised |= OFPPF_10MB_FD;
269 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
270 *advertised |= OFPPF_100MB_HD;
272 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
273 *advertised |= OFPPF_100MB_FD;
275 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
276 *advertised |= OFPPF_1GB_HD;
278 if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
279 *advertised |= OFPPF_1GB_FD;
281 if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
282 *advertised |= OFPPF_10GB_FD;
284 if (ecmd.advertising & ADVERTISED_TP) {
285 *advertised |= OFPPF_COPPER;
287 if (ecmd.advertising & ADVERTISED_FIBRE) {
288 *advertised |= OFPPF_FIBER;
290 if (ecmd.advertising & ADVERTISED_Autoneg) {
291 *advertised |= OFPPF_AUTONEG;
293 if (ecmd.advertising & ADVERTISED_Pause) {
294 *advertised |= OFPPF_PAUSE;
296 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
297 *advertised |= OFPPF_PAUSE_ASYM;
300 /* Set the current features */
301 if (ecmd.speed == SPEED_10) {
302 *current = (ecmd.duplex) ? OFPPF_10MB_FD : OFPPF_10MB_HD;
304 else if (ecmd.speed == SPEED_100) {
305 *current = (ecmd.duplex) ? OFPPF_100MB_FD : OFPPF_100MB_HD;
307 else if (ecmd.speed == SPEED_1000) {
308 *current = (ecmd.duplex) ? OFPPF_1GB_FD : OFPPF_1GB_HD;
310 else if (ecmd.speed == SPEED_10000) {
311 *current = OFPPF_10GB_FD;
314 if (ecmd.port == PORT_TP) {
315 *current |= OFPPF_COPPER;
317 else if (ecmd.port == PORT_FIBRE) {
318 *current |= OFPPF_FIBER;
322 *current |= OFPPF_AUTONEG;
327 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
328 * successful, otherwise a positive errno value. On success, sets '*netdevp'
329 * to the new network device, otherwise to null.
331 * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
332 * capture frames of that type received on the device. It may also be one of
333 * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
336 netdev_open(const char *name, int ethertype, struct netdev **netdevp)
338 if (strncmp(name, "tap:", 4)) {
339 return do_open_netdev(name, ethertype, -1, netdevp);
341 static const char tap_dev[] = "/dev/net/tun";
346 tap_fd = open(tap_dev, O_RDWR);
348 ovs_error(errno, "opening \"%s\" failed", tap_dev);
352 memset(&ifr, 0, sizeof ifr);
353 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
355 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
357 if (ioctl(tap_fd, TUNSETIFF, &ifr) < 0) {
359 ovs_error(error, "ioctl(TUNSETIFF) on \"%s\" failed", tap_dev);
364 error = set_nonblocking(tap_fd);
366 ovs_error(error, "set_nonblocking on \"%s\" failed", tap_dev);
371 error = do_open_netdev(ifr.ifr_name, NETDEV_ETH_TYPE_NONE, tap_fd,
382 do_open_netdev(const char *name, int ethertype, int tap_fd,
383 struct netdev **netdev_)
386 struct sockaddr_ll sll;
389 uint8_t etheraddr[ETH_ADDR_LEN];
395 struct netdev *netdev;
399 COVERAGE_INC(netdev_open);
401 /* Create raw socket. */
402 netdev_fd = socket(PF_PACKET, SOCK_RAW,
403 htons(ethertype == NETDEV_ETH_TYPE_NONE ? 0
404 : ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
405 : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
411 if (ethertype != NETDEV_ETH_TYPE_NONE) {
412 /* Set non-blocking mode. */
413 error = set_nonblocking(netdev_fd);
415 goto error_already_set;
418 /* Get ethernet device index. */
419 ifindex = do_get_ifindex(name);
424 /* Bind to specific ethernet device. */
425 memset(&sll, 0, sizeof sll);
426 sll.sll_family = AF_PACKET;
427 sll.sll_ifindex = ifindex;
428 if (bind(netdev_fd, (struct sockaddr *) &sll, sizeof sll) < 0) {
429 VLOG_ERR("bind to %s failed: %s", name, strerror(errno));
433 /* Between the socket() and bind() calls above, the socket receives all
434 * packets of the requested type on all system interfaces. We do not
435 * want to receive that data, but there is no way to avoid it. So we
436 * must now drain out the receive queue. */
437 error = drain_rcvbuf(netdev_fd);
439 goto error_already_set;
443 /* Get MAC address. */
444 error = get_etheraddr(name, etheraddr, &hwaddr_family);
446 goto error_already_set;
450 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
451 if (ioctl(netdev_fd, SIOCGIFMTU, &ifr) < 0) {
452 VLOG_ERR("ioctl(SIOCGIFMTU) on %s device failed: %s",
453 name, strerror(errno));
458 /* Get TX queue length. */
459 if (ioctl(netdev_fd, SIOCGIFTXQLEN, &ifr) < 0) {
460 VLOG_ERR("ioctl(SIOCGIFTXQLEN) on %s device failed: %s",
461 name, strerror(errno));
464 txqlen = ifr.ifr_qlen;
466 get_ipv6_address(name, &in6);
468 /* Allocate network device. */
469 netdev = xmalloc(sizeof *netdev);
470 netdev->name = xstrdup(name);
471 netdev->ifindex = ifindex;
472 netdev->txqlen = txqlen;
473 netdev->hwaddr_family = hwaddr_family;
474 netdev->netdev_fd = netdev_fd;
475 netdev->tap_fd = tap_fd < 0 ? netdev_fd : tap_fd;
476 memcpy(netdev->etheraddr, etheraddr, sizeof etheraddr);
480 /* Save flags to restore at close or exit. */
481 error = get_flags(netdev->name, &netdev->save_flags);
483 goto error_already_set;
485 netdev->changed_flags = 0;
486 fatal_signal_block();
487 list_push_back(&netdev_list, &netdev->node);
488 fatal_signal_unblock();
504 /* Closes and destroys 'netdev'. */
506 netdev_close(struct netdev *netdev)
509 /* Bring down interface and drop promiscuous mode, if we brought up
510 * the interface or enabled promiscuous mode. */
512 fatal_signal_block();
513 error = restore_flags(netdev);
514 list_remove(&netdev->node);
515 fatal_signal_unblock();
517 VLOG_WARN("failed to restore network device flags on %s: %s",
518 netdev->name, strerror(error));
523 close(netdev->netdev_fd);
524 if (netdev->netdev_fd != netdev->tap_fd) {
525 close(netdev->tap_fd);
531 /* Checks whether a network device named 'name' exists and returns true if so,
532 * false otherwise. */
534 netdev_exists(const char *name)
540 filename = xasprintf("/sys/class/net/%s", name);
541 error = stat(filename, &s);
546 /* Pads 'buffer' out with zero-bytes to the minimum valid length of an
547 * Ethernet packet, if necessary. */
549 pad_to_minimum_length(struct ofpbuf *buffer)
551 if (buffer->size < ETH_TOTAL_MIN) {
552 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
556 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
557 * must have initialized with sufficient room for the packet. The space
558 * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
559 * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
560 * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
561 * need not be included.)
563 * If a packet is successfully retrieved, returns 0. In this case 'buffer' is
564 * guaranteed to contain at least ETH_TOTAL_MIN bytes. Otherwise, returns a
565 * positive errno value. Returns EAGAIN immediately if no packet is ready to
569 netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
573 assert(buffer->size == 0);
574 assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
576 n_bytes = read(netdev->tap_fd,
577 ofpbuf_tail(buffer), ofpbuf_tailroom(buffer));
578 } while (n_bytes < 0 && errno == EINTR);
580 if (errno != EAGAIN) {
581 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
582 strerror(errno), netdev->name);
586 COVERAGE_INC(netdev_received);
587 buffer->size += n_bytes;
589 /* When the kernel internally sends out an Ethernet frame on an
590 * interface, it gives us a copy *before* padding the frame to the
591 * minimum length. Thus, when it sends out something like an ARP
592 * request, we see a too-short frame. So pad it out to the minimum
594 pad_to_minimum_length(buffer);
599 /* Registers with the poll loop to wake up from the next call to poll_block()
600 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
602 netdev_recv_wait(struct netdev *netdev)
604 poll_fd_wait(netdev->tap_fd, POLLIN);
607 /* Discards all packets waiting to be received from 'netdev'. */
609 netdev_drain(struct netdev *netdev)
611 if (netdev->tap_fd != netdev->netdev_fd) {
612 drain_fd(netdev->tap_fd, netdev->txqlen);
615 return drain_rcvbuf(netdev->netdev_fd);
619 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
620 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
621 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
622 * the packet is too big or too small to transmit on the device.
624 * The caller retains ownership of 'buffer' in all cases.
626 * The kernel maintains a packet transmission queue, so the caller is not
627 * expected to do additional queuing of packets. */
629 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
634 n_bytes = write(netdev->tap_fd, buffer->data, buffer->size);
635 } while (n_bytes < 0 && errno == EINTR);
638 /* The Linux AF_PACKET implementation never blocks waiting for room
639 * for packets, instead returning ENOBUFS. Translate this into EAGAIN
641 if (errno == ENOBUFS) {
643 } else if (errno != EAGAIN) {
644 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
645 netdev->name, strerror(errno));
648 } else if (n_bytes != buffer->size) {
650 "send partial Ethernet packet (%d bytes of %zu) on %s",
651 (int) n_bytes, buffer->size, netdev->name);
654 COVERAGE_INC(netdev_sent);
659 /* Registers with the poll loop to wake up from the next call to poll_block()
660 * when the packet transmission queue has sufficient room to transmit a packet
661 * with netdev_send().
663 * The kernel maintains a packet transmission queue, so the client is not
664 * expected to do additional queuing of packets. Thus, this function is
665 * unlikely to ever be used. It is included for completeness. */
667 netdev_send_wait(struct netdev *netdev)
669 if (netdev->tap_fd == netdev->netdev_fd) {
670 poll_fd_wait(netdev->tap_fd, POLLOUT);
672 /* TAP device always accepts packets.*/
673 poll_immediate_wake();
677 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
678 * otherwise a positive errno value. */
680 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
682 int error = set_etheraddr(netdev->name, netdev->hwaddr_family, mac);
684 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
689 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
690 * the MAC address into 'mac'. On failure, returns a positive errno value and
691 * clears 'mac' to all-zeros. */
693 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
695 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
699 /* Returns the name of the network device that 'netdev' represents,
700 * e.g. "eth0". The caller must not modify or free the returned string. */
702 netdev_get_name(const struct netdev *netdev)
707 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
708 * (and received) packets, in bytes, not including the hardware header; thus,
709 * this is typically 1500 bytes for Ethernet devices.
711 * If successful, returns 0 and stores the MTU size in '*mtup'. On failure,
712 * returns a positive errno value and stores ETH_PAYLOAD_MAX (1500) in
715 netdev_get_mtu(const struct netdev *netdev, int *mtup)
721 /* Stores the features supported by 'netdev' into each of '*current',
722 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
723 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
724 * successful, otherwise a positive errno value. On failure, all of the
725 * passed-in values are set to 0. */
727 netdev_get_features(struct netdev *netdev,
728 uint32_t *current, uint32_t *advertised,
729 uint32_t *supported, uint32_t *peer)
732 return do_get_features(netdev,
733 current ? current : &dummy[0],
734 advertised ? advertised : &dummy[1],
735 supported ? supported : &dummy[2],
736 peer ? peer : &dummy[3]);
739 /* Set the features advertised by 'netdev' to 'advertise'. */
741 netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
743 struct ethtool_cmd ecmd;
746 memset(&ecmd, 0, sizeof ecmd);
747 error = do_ethtool(netdev, &ecmd, ETHTOOL_GSET, "ETHTOOL_GSET");
752 ecmd.advertising = 0;
753 if (advertise & OFPPF_10MB_HD) {
754 ecmd.advertising |= ADVERTISED_10baseT_Half;
756 if (advertise & OFPPF_10MB_FD) {
757 ecmd.advertising |= ADVERTISED_10baseT_Full;
759 if (advertise & OFPPF_100MB_HD) {
760 ecmd.advertising |= ADVERTISED_100baseT_Half;
762 if (advertise & OFPPF_100MB_FD) {
763 ecmd.advertising |= ADVERTISED_100baseT_Full;
765 if (advertise & OFPPF_1GB_HD) {
766 ecmd.advertising |= ADVERTISED_1000baseT_Half;
768 if (advertise & OFPPF_1GB_FD) {
769 ecmd.advertising |= ADVERTISED_1000baseT_Full;
771 if (advertise & OFPPF_10GB_FD) {
772 ecmd.advertising |= ADVERTISED_10000baseT_Full;
774 if (advertise & OFPPF_COPPER) {
775 ecmd.advertising |= ADVERTISED_TP;
777 if (advertise & OFPPF_FIBER) {
778 ecmd.advertising |= ADVERTISED_FIBRE;
780 if (advertise & OFPPF_AUTONEG) {
781 ecmd.advertising |= ADVERTISED_Autoneg;
783 if (advertise & OFPPF_PAUSE) {
784 ecmd.advertising |= ADVERTISED_Pause;
786 if (advertise & OFPPF_PAUSE_ASYM) {
787 ecmd.advertising |= ADVERTISED_Asym_Pause;
789 return do_ethtool(netdev, &ecmd, ETHTOOL_SSET, "ETHTOOL_SSET");
792 /* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
793 * 'in4' is non-null) and returns 0. Otherwise, returns a positive errno value
794 * and sets '*in4' to INADDR_ANY (0). */
796 netdev_get_in4(const struct netdev *netdev, struct in_addr *in4)
798 const char *netdev_name = netdev_get_name(netdev);
800 struct in_addr ip = { INADDR_ANY };
805 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
806 ifr.ifr_addr.sa_family = AF_INET;
807 COVERAGE_INC(netdev_get_in4);
808 if (ioctl(af_inet_sock, SIOCGIFADDR, &ifr) == 0) {
809 struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
811 error = ip.s_addr != INADDR_ANY ? 0 : EADDRNOTAVAIL;
813 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFADDR) failed: %s",
814 netdev_name, strerror(errno));
824 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
826 struct sockaddr_in sin;
827 memset(&sin, 0, sizeof sin);
828 sin.sin_family = AF_INET;
832 memset(sa, 0, sizeof *sa);
833 memcpy(sa, &sin, sizeof sin);
837 do_set_addr(struct netdev *netdev, int sock,
838 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
843 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
844 make_in4_sockaddr(&ifr.ifr_addr, addr);
845 COVERAGE_INC(netdev_set_in4);
846 error = ioctl(sock, ioctl_nr, &ifr) < 0 ? errno : 0;
848 VLOG_WARN("ioctl(%s): %s", ioctl_name, strerror(error));
853 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
854 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
855 * positive errno value. */
857 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
861 error = do_set_addr(netdev, af_inet_sock,
862 SIOCSIFADDR, "SIOCSIFADDR", addr);
863 if (!error && addr.s_addr != INADDR_ANY) {
864 error = do_set_addr(netdev, af_inet_sock,
865 SIOCSIFNETMASK, "SIOCSIFNETMASK", mask);
870 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
873 netdev_add_router(struct netdev *netdev UNUSED, struct in_addr router)
875 struct in_addr any = { INADDR_ANY };
879 memset(&rt, 0, sizeof rt);
880 make_in4_sockaddr(&rt.rt_dst, any);
881 make_in4_sockaddr(&rt.rt_gateway, router);
882 make_in4_sockaddr(&rt.rt_genmask, any);
883 rt.rt_flags = RTF_UP | RTF_GATEWAY;
884 COVERAGE_INC(netdev_add_router);
885 error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
887 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
892 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
893 * 'in6' is non-null) and returns true. Otherwise, returns false. */
895 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
900 return memcmp(&netdev->in6, &in6addr_any, sizeof netdev->in6) != 0;
903 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
904 * Returns 0 if successful, otherwise a positive errno value. On failure,
905 * stores 0 into '*flagsp'. */
907 netdev_get_flags(const struct netdev *netdev, enum netdev_flags *flagsp)
914 error = get_flags(netdev_get_name(netdev), &flags);
919 if (flags & IFF_UP) {
920 *flagsp |= NETDEV_UP;
922 if (flags & IFF_PROMISC) {
923 *flagsp |= NETDEV_PROMISC;
929 nd_to_iff_flags(enum netdev_flags nd)
932 if (nd & NETDEV_UP) {
935 if (nd & NETDEV_PROMISC) {
941 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
942 * 'on'. If 'permanent' is true, the changes will persist; otherwise, they
943 * will be reverted when 'netdev' is closed or the program exits. Returns 0 if
944 * successful, otherwise a positive errno value. */
946 do_update_flags(struct netdev *netdev, enum netdev_flags off,
947 enum netdev_flags on, bool permanent)
949 int old_flags, new_flags;
952 error = get_flags(netdev->name, &old_flags);
957 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
959 netdev->changed_flags |= new_flags ^ old_flags;
961 if (new_flags != old_flags) {
962 error = set_flags(netdev->name, new_flags);
967 /* Sets the flags for 'netdev' to 'flags'.
968 * If 'permanent' is true, the changes will persist; otherwise, they
969 * will be reverted when 'netdev' is closed or the program exits.
970 * Returns 0 if successful, otherwise a positive errno value. */
972 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
975 return do_update_flags(netdev, -1, flags, permanent);
978 /* Turns on the specified 'flags' on 'netdev'.
979 * If 'permanent' is true, the changes will persist; otherwise, they
980 * will be reverted when 'netdev' is closed or the program exits.
981 * Returns 0 if successful, otherwise a positive errno value. */
983 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
986 return do_update_flags(netdev, 0, flags, permanent);
989 /* Turns off the specified 'flags' on 'netdev'.
990 * If 'permanent' is true, the changes will persist; otherwise, they
991 * will be reverted when 'netdev' is closed or the program exits.
992 * Returns 0 if successful, otherwise a positive errno value. */
994 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
997 return do_update_flags(netdev, flags, 0, permanent);
1000 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1001 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1002 * returns 0. Otherwise, it returns a positive errno value; in particular,
1003 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
1005 netdev_arp_lookup(const struct netdev *netdev, uint32_t ip,
1006 uint8_t mac[ETH_ADDR_LEN])
1008 const char *netdev_name = netdev_get_name(netdev);
1010 struct sockaddr_in *pa;
1015 memset(&r, 0, sizeof r);
1016 pa = (struct sockaddr_in *) &r.arp_pa;
1017 pa->sin_family = AF_INET;
1018 pa->sin_addr.s_addr = ip;
1020 r.arp_ha.sa_family = ARPHRD_ETHER;
1022 strncpy(r.arp_dev, netdev_name, sizeof r.arp_dev);
1023 COVERAGE_INC(netdev_arp_lookup);
1024 retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
1026 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
1027 } else if (retval != ENXIO) {
1028 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
1029 netdev_name, IP_ARGS(&ip), strerror(retval));
1035 get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
1037 struct ofpbuf request;
1038 struct ofpbuf *reply;
1039 struct ifinfomsg *ifi;
1040 const struct rtnl_link_stats *rtnl_stats;
1041 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1044 ofpbuf_init(&request, 0);
1045 nl_msg_put_nlmsghdr(&request, rtnl_sock, sizeof *ifi,
1046 RTM_GETLINK, NLM_F_REQUEST);
1047 ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
1048 ifi->ifi_family = PF_UNSPEC;
1049 ifi->ifi_index = ifindex;
1050 error = nl_sock_transact(rtnl_sock, &request, &reply);
1051 ofpbuf_uninit(&request);
1056 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1057 rtnlgrp_link_policy,
1058 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1059 ofpbuf_delete(reply);
1063 if (!attrs[IFLA_STATS]) {
1064 VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
1068 rtnl_stats = nl_attr_get(attrs[IFLA_STATS]);
1069 stats->rx_packets = rtnl_stats->rx_packets;
1070 stats->tx_packets = rtnl_stats->tx_packets;
1071 stats->rx_bytes = rtnl_stats->rx_bytes;
1072 stats->tx_bytes = rtnl_stats->tx_bytes;
1073 stats->rx_errors = rtnl_stats->rx_errors;
1074 stats->tx_errors = rtnl_stats->tx_errors;
1075 stats->rx_dropped = rtnl_stats->rx_dropped;
1076 stats->tx_dropped = rtnl_stats->tx_dropped;
1077 stats->multicast = rtnl_stats->multicast;
1078 stats->collisions = rtnl_stats->collisions;
1079 stats->rx_length_errors = rtnl_stats->rx_length_errors;
1080 stats->rx_over_errors = rtnl_stats->rx_over_errors;
1081 stats->rx_crc_errors = rtnl_stats->rx_crc_errors;
1082 stats->rx_frame_errors = rtnl_stats->rx_frame_errors;
1083 stats->rx_fifo_errors = rtnl_stats->rx_fifo_errors;
1084 stats->rx_missed_errors = rtnl_stats->rx_missed_errors;
1085 stats->tx_aborted_errors = rtnl_stats->tx_aborted_errors;
1086 stats->tx_carrier_errors = rtnl_stats->tx_carrier_errors;
1087 stats->tx_fifo_errors = rtnl_stats->tx_fifo_errors;
1088 stats->tx_heartbeat_errors = rtnl_stats->tx_heartbeat_errors;
1089 stats->tx_window_errors = rtnl_stats->tx_window_errors;
1095 get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats)
1097 static const char fn[] = "/proc/net/dev";
1102 stream = fopen(fn, "r");
1104 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1109 while (fgets(line, sizeof line, stream)) {
1112 #define X64 "%"SCNu64
1115 X64 X64 X64 X64 X64 X64 X64 "%*u"
1116 X64 X64 X64 X64 X64 X64 X64 "%*u",
1122 &stats->rx_fifo_errors,
1123 &stats->rx_frame_errors,
1129 &stats->tx_fifo_errors,
1131 &stats->tx_carrier_errors) != 15) {
1132 VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln);
1133 } else if (!strcmp(devname, netdev_name)) {
1134 stats->rx_length_errors = UINT64_MAX;
1135 stats->rx_over_errors = UINT64_MAX;
1136 stats->rx_crc_errors = UINT64_MAX;
1137 stats->rx_missed_errors = UINT64_MAX;
1138 stats->tx_aborted_errors = UINT64_MAX;
1139 stats->tx_heartbeat_errors = UINT64_MAX;
1140 stats->tx_window_errors = UINT64_MAX;
1146 VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name);
1151 /* Sets 'carrier' to true if carrier is active (link light is on) on
1154 netdev_get_carrier(const struct netdev *netdev, bool *carrier)
1164 fn = xasprintf("/sys/class/net/%s/carrier", netdev_get_name(netdev));
1165 fd = open(fn, O_RDONLY);
1168 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(error));
1172 retval = read(fd, line, sizeof line);
1175 if (error == EINVAL) {
1176 /* This is the normal return value when we try to check carrier if
1177 * the network device is not up. */
1179 VLOG_WARN_RL(&rl, "%s: read failed: %s", fn, strerror(error));
1182 } else if (retval == 0) {
1184 VLOG_WARN_RL(&rl, "%s: unexpected end of file", fn);
1188 if (line[0] != '0' && line[0] != '1') {
1190 VLOG_WARN_RL(&rl, "%s: value is %c (expected 0 or 1)", fn, line[0]);
1193 *carrier = line[0] != '0';
1203 /* Retrieves current device stats for 'netdev'. */
1205 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1209 COVERAGE_INC(netdev_get_stats);
1210 if (use_netlink_stats) {
1213 error = get_ifindex(netdev, &ifindex);
1215 error = get_stats_via_netlink(ifindex, stats);
1218 error = get_stats_via_proc(netdev->name, stats);
1222 memset(stats, 0xff, sizeof *stats);
1227 #define POLICE_ADD_CMD "/sbin/tc qdisc add dev %s handle ffff: ingress"
1228 #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"
1229 /* We redirect stderr to /dev/null because we often want to remove all
1230 * traffic control configuration on a port so its in a known state. If
1231 * this done when there is no such configuration, tc complains, so we just
1234 #define POLICE_DEL_CMD "/sbin/tc qdisc del dev %s handle ffff: ingress 2>/dev/null"
1236 /* Attempts to set input rate limiting (policing) policy. */
1238 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1239 uint32_t kbits_burst)
1241 const char *netdev_name = netdev_get_name(netdev);
1246 COVERAGE_INC(netdev_set_policing);
1249 /* Default to 10 kilobits if not specified. */
1253 /* xxx This should be more careful about only adding if it
1254 * xxx actually exists, as opposed to always deleting it. */
1255 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
1256 if (system(command) == -1) {
1257 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
1260 snprintf(command, sizeof(command), POLICE_ADD_CMD, netdev_name);
1261 if (system(command) != 0) {
1262 VLOG_WARN_RL(&rl, "%s: problem adding policing", netdev_name);
1266 snprintf(command, sizeof(command), POLICE_CONFIG_CMD, netdev_name,
1267 kbits_rate, kbits_burst);
1268 if (system(command) != 0) {
1269 VLOG_WARN_RL(&rl, "%s: problem configuring policing",
1274 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
1275 if (system(command) == -1) {
1276 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
1283 /* Initializes 'svec' with a list of the names of all known network devices. */
1285 netdev_enumerate(struct svec *svec)
1287 struct if_nameindex *names;
1290 names = if_nameindex();
1294 for (i = 0; names[i].if_name != NULL; i++) {
1295 svec_add(svec, names[i].if_name);
1297 if_freenameindex(names);
1299 VLOG_WARN("could not obtain list of network device names: %s",
1304 /* Returns a network device that has 'in4' as its IP address, if one exists,
1305 * otherwise a null pointer. */
1307 netdev_find_dev_by_in4(const struct in_addr *in4)
1309 struct netdev *netdev;
1310 struct svec dev_list;
1313 netdev_enumerate(&dev_list);
1314 for (i = 0; i < dev_list.n; i++) {
1315 const char *name = dev_list.names[i];
1316 struct in_addr dev_in4;
1318 if (!netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev)
1319 && !netdev_get_in4(netdev, &dev_in4)
1320 && dev_in4.s_addr == in4->s_addr) {
1323 netdev_close(netdev);
1328 svec_destroy(&dev_list);
1332 /* If 'netdev' is a VLAN network device (e.g. one created with vconfig(8)),
1333 * sets '*vlan_vid' to the VLAN VID associated with that device and returns 0.
1334 * Otherwise returns a errno value (specifically ENOENT if 'netdev_name' is the
1335 * name of a network device that is not a VLAN device) and sets '*vlan_vid' to
1338 netdev_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
1340 struct ds line = DS_EMPTY_INITIALIZER;
1341 FILE *stream = NULL;
1345 COVERAGE_INC(netdev_get_vlan_vid);
1346 fn = xasprintf("/proc/net/vlan/%s", netdev_get_name(netdev));
1347 stream = fopen(fn, "r");
1353 if (ds_get_line(&line, stream)) {
1354 if (ferror(stream)) {
1356 VLOG_ERR_RL(&rl, "error reading \"%s\": %s", fn, strerror(errno));
1359 VLOG_ERR_RL(&rl, "unexpected end of file reading \"%s\"", fn);
1364 if (!sscanf(ds_cstr(&line), "%*s VID: %d", vlan_vid)) {
1366 VLOG_ERR_RL(&rl, "parse error reading \"%s\" line 1: \"%s\"",
1367 fn, ds_cstr(&line));
1385 struct netdev_monitor {
1386 struct linux_netdev_notifier notifier;
1387 struct shash polled_netdevs;
1388 struct shash changed_netdevs;
1391 static void netdev_monitor_change(const struct linux_netdev_change *change,
1395 netdev_monitor_create(struct netdev_monitor **monitorp)
1397 struct netdev_monitor *monitor;
1400 monitor = xmalloc(sizeof *monitor);
1401 error = linux_netdev_notifier_register(&monitor->notifier,
1402 netdev_monitor_change, monitor);
1407 shash_init(&monitor->polled_netdevs);
1408 shash_init(&monitor->changed_netdevs);
1409 *monitorp = monitor;
1414 netdev_monitor_destroy(struct netdev_monitor *monitor)
1417 linux_netdev_notifier_unregister(&monitor->notifier);
1418 shash_destroy(&monitor->polled_netdevs);
1424 netdev_monitor_add(struct netdev_monitor *monitor, struct netdev *netdev)
1426 if (!shash_find(&monitor->polled_netdevs, netdev_get_name(netdev))) {
1427 shash_add(&monitor->polled_netdevs, netdev_get_name(netdev), NULL);
1432 netdev_monitor_remove(struct netdev_monitor *monitor, struct netdev *netdev)
1434 struct shash_node *node;
1436 node = shash_find(&monitor->polled_netdevs, netdev_get_name(netdev));
1438 shash_delete(&monitor->polled_netdevs, node);
1439 node = shash_find(&monitor->changed_netdevs, netdev_get_name(netdev));
1441 shash_delete(&monitor->changed_netdevs, node);
1447 netdev_monitor_poll(struct netdev_monitor *monitor, char **devnamep)
1449 int error = linux_netdev_notifier_get_error(&monitor->notifier);
1452 struct shash_node *node = shash_first(&monitor->changed_netdevs);
1456 *devnamep = xstrdup(node->name);
1457 shash_delete(&monitor->changed_netdevs, node);
1459 shash_clear(&monitor->changed_netdevs);
1465 netdev_monitor_poll_wait(const struct netdev_monitor *monitor)
1467 if (!shash_is_empty(&monitor->changed_netdevs)
1468 || linux_netdev_notifier_peek_error(&monitor->notifier)) {
1469 poll_immediate_wake();
1471 linux_netdev_notifier_wait();
1476 netdev_monitor_change(const struct linux_netdev_change *change, void *monitor_)
1478 struct netdev_monitor *monitor = monitor_;
1479 if (shash_find(&monitor->polled_netdevs, change->ifname)
1480 && !shash_find(&monitor->changed_netdevs, change->ifname)) {
1481 shash_add(&monitor->changed_netdevs, change->ifname, NULL);
1485 static void restore_all_flags(void *aux);
1487 /* Set up a signal hook to restore network device flags on program
1499 fatal_signal_add_hook(restore_all_flags, NULL, true);
1501 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
1502 if (af_inet_sock < 0) {
1503 ovs_fatal(errno, "socket(AF_INET)");
1506 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0, &rtnl_sock);
1508 ovs_fatal(error, "socket(AF_NETLINK, NETLINK_ROUTE)");
1511 /* Decide on the netdev_get_stats() implementation to use. Netlink is
1512 * preferable, so if that works, we'll use it. */
1513 ifindex = do_get_ifindex("lo");
1515 VLOG_WARN("failed to get ifindex for lo, "
1516 "obtaining netdev stats from proc");
1517 use_netlink_stats = false;
1519 struct netdev_stats stats;
1520 error = get_stats_via_netlink(ifindex, &stats);
1522 VLOG_DBG("obtaining netdev stats via rtnetlink");
1523 use_netlink_stats = true;
1525 VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
1526 "via proc (you are probably running a pre-2.6.19 "
1527 "kernel)", strerror(error));
1528 use_netlink_stats = false;
1534 /* Restore the network device flags on 'netdev' to those that were active
1535 * before we changed them. Returns 0 if successful, otherwise a positive
1538 * To avoid reentry, the caller must ensure that fatal signals are blocked. */
1540 restore_flags(struct netdev *netdev)
1545 /* Get current flags. */
1546 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1547 COVERAGE_INC(netdev_get_flags);
1548 if (ioctl(netdev->netdev_fd, SIOCGIFFLAGS, &ifr) < 0) {
1552 /* Restore flags that we might have changed, if necessary. */
1553 restore_flags = netdev->changed_flags & (IFF_PROMISC | IFF_UP);
1554 if ((ifr.ifr_flags ^ netdev->save_flags) & restore_flags) {
1555 ifr.ifr_flags &= ~restore_flags;
1556 ifr.ifr_flags |= netdev->save_flags & restore_flags;
1557 COVERAGE_INC(netdev_set_flags);
1558 if (ioctl(netdev->netdev_fd, SIOCSIFFLAGS, &ifr) < 0) {
1566 /* Retores all the flags on all network devices that we modified. Called from
1567 * a signal handler, so it does not attempt to report error conditions. */
1569 restore_all_flags(void *aux UNUSED)
1571 struct netdev *netdev;
1572 LIST_FOR_EACH (netdev, struct netdev, node, &netdev_list) {
1573 restore_flags(netdev);
1578 get_flags(const char *netdev_name, int *flags)
1581 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1582 COVERAGE_INC(netdev_get_flags);
1583 if (ioctl(af_inet_sock, SIOCGIFFLAGS, &ifr) < 0) {
1584 VLOG_ERR("ioctl(SIOCGIFFLAGS) on %s device failed: %s",
1585 netdev_name, strerror(errno));
1588 *flags = ifr.ifr_flags;
1593 set_flags(const char *netdev_name, int flags)
1596 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1597 ifr.ifr_flags = flags;
1598 COVERAGE_INC(netdev_set_flags);
1599 if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) < 0) {
1600 VLOG_ERR("ioctl(SIOCSIFFLAGS) on %s device failed: %s",
1601 netdev_name, strerror(errno));
1608 do_get_ifindex(const char *netdev_name)
1612 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1613 COVERAGE_INC(netdev_get_ifindex);
1614 if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) {
1615 VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
1616 netdev_name, strerror(errno));
1619 return ifr.ifr_ifindex;
1623 get_ifindex(const struct netdev *netdev, int *ifindexp)
1626 if (netdev->ifindex < 0) {
1627 int ifindex = do_get_ifindex(netdev->name);
1631 ((struct netdev *) netdev)->ifindex = ifindex;
1633 *ifindexp = netdev->ifindex;
1638 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN],
1639 int *hwaddr_familyp)
1643 *hwaddr_familyp = 0;
1644 memset(&ifr, 0, sizeof ifr);
1645 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1646 COVERAGE_INC(netdev_get_hwaddr);
1647 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
1648 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
1649 netdev_name, strerror(errno));
1652 if (hwaddr_familyp) {
1653 int hwaddr_family = ifr.ifr_hwaddr.sa_family;
1654 *hwaddr_familyp = hwaddr_family;
1655 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
1656 VLOG_WARN("%s device has unknown hardware address family %d",
1657 netdev_name, hwaddr_family);
1660 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
1665 set_etheraddr(const char *netdev_name, int hwaddr_family,
1666 const uint8_t mac[ETH_ADDR_LEN])
1670 memset(&ifr, 0, sizeof ifr);
1671 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1672 ifr.ifr_hwaddr.sa_family = hwaddr_family;
1673 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
1674 COVERAGE_INC(netdev_set_hwaddr);
1675 if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) {
1676 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
1677 netdev_name, strerror(errno));