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"
51 #include "openflow/openflow.h"
53 #include "poll-loop.h"
54 #include "socket-util.h"
57 /* linux/if.h defines IFF_LOWER_UP, net/if.h doesn't.
58 * net/if.h defines if_nameindex(), linux/if.h doesn't.
59 * We can't include both headers, so define IFF_LOWER_UP ourselves. */
61 #define IFF_LOWER_UP 0x10000
64 /* These were introduced in Linux 2.6.14, so they might be missing if we have
66 #ifndef ADVERTISED_Pause
67 #define ADVERTISED_Pause (1 << 13)
69 #ifndef ADVERTISED_Asym_Pause
70 #define ADVERTISED_Asym_Pause (1 << 14)
73 #define THIS_MODULE VLM_netdev
80 /* File descriptors. For ordinary network devices, the two fds below are
81 * the same; for tap devices, they differ. */
82 int netdev_fd; /* Network device. */
83 int tap_fd; /* TAP character device, if any, otherwise the
86 /* Cached network device information. */
87 int ifindex; /* -1 if not known. */
88 uint8_t etheraddr[ETH_ADDR_LEN];
95 int save_flags; /* Initial device flags. */
96 int changed_flags; /* Flags that we changed. */
99 /* Policy for RTNLGRP_LINK messages.
101 * There are *many* more fields in these messages, but currently we only care
102 * about interface names. */
103 static const struct nl_policy rtnlgrp_link_policy[] = {
104 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
105 [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
106 .min_len = sizeof(struct rtnl_link_stats) },
109 /* All open network devices. */
110 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
112 /* An AF_INET socket (used for ioctl operations). */
113 static int af_inet_sock = -1;
115 /* NETLINK_ROUTE socket. */
116 static struct nl_sock *rtnl_sock;
118 /* Can we use RTM_GETLINK to get network device statistics? (In pre-2.6.19
119 * kernels, this was only available if wireless extensions were enabled.) */
120 static bool use_netlink_stats;
122 /* This is set pretty low because we probably won't learn anything from the
123 * additional log messages. */
124 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
126 static void init_netdev(void);
127 static int do_open_netdev(const char *name, int ethertype, int tap_fd,
128 struct netdev **netdev_);
129 static int restore_flags(struct netdev *netdev);
130 static int get_flags(const char *netdev_name, int *flagsp);
131 static int set_flags(const char *netdev_name, int flags);
132 static int do_get_ifindex(const char *netdev_name);
133 static int get_ifindex(const struct netdev *, int *ifindexp);
134 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN],
135 int *hwaddr_familyp);
136 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
137 const uint8_t[ETH_ADDR_LEN]);
139 /* Obtains the IPv6 address for 'name' into 'in6'. */
141 get_ipv6_address(const char *name, struct in6_addr *in6)
146 file = fopen("/proc/net/if_inet6", "r");
148 /* This most likely indicates that the host doesn't have IPv6 support,
149 * so it's not really a failure condition.*/
154 while (fgets(line, sizeof line, file)) {
155 uint8_t *s6 = in6->s6_addr;
159 if (sscanf(line, " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
160 "%*x %*x %*x %*x %16s\n",
161 &s6[0], &s6[1], &s6[2], &s6[3],
162 &s6[4], &s6[5], &s6[6], &s6[7],
163 &s6[8], &s6[9], &s6[10], &s6[11],
164 &s6[12], &s6[13], &s6[14], &s6[15],
166 && !strcmp(name, ifname))
178 do_ethtool(struct netdev *netdev, struct ethtool_cmd *ecmd,
179 int cmd, const char *cmd_name)
183 memset(&ifr, 0, sizeof ifr);
184 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
185 ifr.ifr_data = (caddr_t) ecmd;
188 COVERAGE_INC(netdev_ethtool);
189 if (ioctl(netdev->netdev_fd, SIOCETHTOOL, &ifr) == 0) {
192 if (errno != EOPNOTSUPP) {
193 VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
194 "failed: %s", cmd_name, netdev->name,
197 /* The device doesn't support this operation. That's pretty
198 * common, so there's no point in logging anything. */
205 do_get_features(struct netdev *netdev,
206 uint32_t *current, uint32_t *advertised,
207 uint32_t *supported, uint32_t *peer)
209 struct ethtool_cmd ecmd;
217 memset(&ecmd, 0, sizeof ecmd);
218 error = do_ethtool(netdev, &ecmd, ETHTOOL_GSET, "ETHTOOL_GSET");
223 if (ecmd.supported & SUPPORTED_10baseT_Half) {
224 *supported |= OFPPF_10MB_HD;
226 if (ecmd.supported & SUPPORTED_10baseT_Full) {
227 *supported |= OFPPF_10MB_FD;
229 if (ecmd.supported & SUPPORTED_100baseT_Half) {
230 *supported |= OFPPF_100MB_HD;
232 if (ecmd.supported & SUPPORTED_100baseT_Full) {
233 *supported |= OFPPF_100MB_FD;
235 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
236 *supported |= OFPPF_1GB_HD;
238 if (ecmd.supported & SUPPORTED_1000baseT_Full) {
239 *supported |= OFPPF_1GB_FD;
241 if (ecmd.supported & SUPPORTED_10000baseT_Full) {
242 *supported |= OFPPF_10GB_FD;
244 if (ecmd.supported & SUPPORTED_TP) {
245 *supported |= OFPPF_COPPER;
247 if (ecmd.supported & SUPPORTED_FIBRE) {
248 *supported |= OFPPF_FIBER;
250 if (ecmd.supported & SUPPORTED_Autoneg) {
251 *supported |= OFPPF_AUTONEG;
253 if (ecmd.supported & SUPPORTED_Pause) {
254 *supported |= OFPPF_PAUSE;
256 if (ecmd.supported & SUPPORTED_Asym_Pause) {
257 *supported |= OFPPF_PAUSE_ASYM;
260 /* Set the advertised features */
261 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
262 *advertised |= OFPPF_10MB_HD;
264 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
265 *advertised |= OFPPF_10MB_FD;
267 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
268 *advertised |= OFPPF_100MB_HD;
270 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
271 *advertised |= OFPPF_100MB_FD;
273 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
274 *advertised |= OFPPF_1GB_HD;
276 if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
277 *advertised |= OFPPF_1GB_FD;
279 if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
280 *advertised |= OFPPF_10GB_FD;
282 if (ecmd.advertising & ADVERTISED_TP) {
283 *advertised |= OFPPF_COPPER;
285 if (ecmd.advertising & ADVERTISED_FIBRE) {
286 *advertised |= OFPPF_FIBER;
288 if (ecmd.advertising & ADVERTISED_Autoneg) {
289 *advertised |= OFPPF_AUTONEG;
291 if (ecmd.advertising & ADVERTISED_Pause) {
292 *advertised |= OFPPF_PAUSE;
294 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
295 *advertised |= OFPPF_PAUSE_ASYM;
298 /* Set the current features */
299 if (ecmd.speed == SPEED_10) {
300 *current = (ecmd.duplex) ? OFPPF_10MB_FD : OFPPF_10MB_HD;
302 else if (ecmd.speed == SPEED_100) {
303 *current = (ecmd.duplex) ? OFPPF_100MB_FD : OFPPF_100MB_HD;
305 else if (ecmd.speed == SPEED_1000) {
306 *current = (ecmd.duplex) ? OFPPF_1GB_FD : OFPPF_1GB_HD;
308 else if (ecmd.speed == SPEED_10000) {
309 *current = OFPPF_10GB_FD;
312 if (ecmd.port == PORT_TP) {
313 *current |= OFPPF_COPPER;
315 else if (ecmd.port == PORT_FIBRE) {
316 *current |= OFPPF_FIBER;
320 *current |= OFPPF_AUTONEG;
325 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
326 * successful, otherwise a positive errno value. On success, sets '*netdevp'
327 * to the new network device, otherwise to null.
329 * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
330 * capture frames of that type received on the device. It may also be one of
331 * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
334 netdev_open(const char *name, int ethertype, struct netdev **netdevp)
336 if (!strncmp(name, "tap:", 4)) {
337 return netdev_open_tap(name + 4, netdevp);
339 return do_open_netdev(name, ethertype, -1, netdevp);
343 /* Opens a TAP virtual network device. If 'name' is a nonnull, non-empty
344 * string, attempts to assign that name to the TAP device (failing if the name
345 * is already in use); otherwise, a name is automatically assigned. Returns
346 * zero if successful, otherwise a positive errno value. On success, sets
347 * '*netdevp' to the new network device, otherwise to null. */
349 netdev_open_tap(const char *name, struct netdev **netdevp)
351 static const char tap_dev[] = "/dev/net/tun";
356 tap_fd = open(tap_dev, O_RDWR);
358 ovs_error(errno, "opening \"%s\" failed", tap_dev);
362 memset(&ifr, 0, sizeof ifr);
363 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
365 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
367 if (ioctl(tap_fd, TUNSETIFF, &ifr) < 0) {
369 ovs_error(error, "ioctl(TUNSETIFF) on \"%s\" failed", tap_dev);
374 error = set_nonblocking(tap_fd);
376 ovs_error(error, "set_nonblocking on \"%s\" failed", tap_dev);
381 error = do_open_netdev(ifr.ifr_name, NETDEV_ETH_TYPE_NONE, tap_fd,
390 do_open_netdev(const char *name, int ethertype, int tap_fd,
391 struct netdev **netdev_)
394 struct sockaddr_ll sll;
397 uint8_t etheraddr[ETH_ADDR_LEN];
403 struct netdev *netdev;
407 COVERAGE_INC(netdev_open);
409 /* Create raw socket. */
410 netdev_fd = socket(PF_PACKET, SOCK_RAW,
411 htons(ethertype == NETDEV_ETH_TYPE_NONE ? 0
412 : ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
413 : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
419 if (ethertype != NETDEV_ETH_TYPE_NONE) {
420 /* Set non-blocking mode. */
421 error = set_nonblocking(netdev_fd);
423 goto error_already_set;
426 /* Get ethernet device index. */
427 ifindex = do_get_ifindex(name);
432 /* Bind to specific ethernet device. */
433 memset(&sll, 0, sizeof sll);
434 sll.sll_family = AF_PACKET;
435 sll.sll_ifindex = ifindex;
436 if (bind(netdev_fd, (struct sockaddr *) &sll, sizeof sll) < 0) {
437 VLOG_ERR("bind to %s failed: %s", name, strerror(errno));
441 /* Between the socket() and bind() calls above, the socket receives all
442 * packets of the requested type on all system interfaces. We do not
443 * want to receive that data, but there is no way to avoid it. So we
444 * must now drain out the receive queue. */
445 error = drain_rcvbuf(netdev_fd);
447 goto error_already_set;
451 /* Get MAC address. */
452 error = get_etheraddr(name, etheraddr, &hwaddr_family);
454 goto error_already_set;
458 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
459 if (ioctl(netdev_fd, SIOCGIFMTU, &ifr) < 0) {
460 VLOG_ERR("ioctl(SIOCGIFMTU) on %s device failed: %s",
461 name, strerror(errno));
466 /* Get TX queue length. */
467 if (ioctl(netdev_fd, SIOCGIFTXQLEN, &ifr) < 0) {
468 VLOG_ERR("ioctl(SIOCGIFTXQLEN) on %s device failed: %s",
469 name, strerror(errno));
472 txqlen = ifr.ifr_qlen;
474 get_ipv6_address(name, &in6);
476 /* Allocate network device. */
477 netdev = xmalloc(sizeof *netdev);
478 netdev->name = xstrdup(name);
479 netdev->ifindex = ifindex;
480 netdev->txqlen = txqlen;
481 netdev->hwaddr_family = hwaddr_family;
482 netdev->netdev_fd = netdev_fd;
483 netdev->tap_fd = tap_fd < 0 ? netdev_fd : tap_fd;
484 memcpy(netdev->etheraddr, etheraddr, sizeof etheraddr);
488 /* Save flags to restore at close or exit. */
489 error = get_flags(netdev->name, &netdev->save_flags);
491 goto error_already_set;
493 netdev->changed_flags = 0;
494 fatal_signal_block();
495 list_push_back(&netdev_list, &netdev->node);
496 fatal_signal_unblock();
512 /* Closes and destroys 'netdev'. */
514 netdev_close(struct netdev *netdev)
517 /* Bring down interface and drop promiscuous mode, if we brought up
518 * the interface or enabled promiscuous mode. */
520 fatal_signal_block();
521 error = restore_flags(netdev);
522 list_remove(&netdev->node);
523 fatal_signal_unblock();
525 VLOG_WARN("failed to restore network device flags on %s: %s",
526 netdev->name, strerror(error));
531 close(netdev->netdev_fd);
532 if (netdev->netdev_fd != netdev->tap_fd) {
533 close(netdev->tap_fd);
539 /* Pads 'buffer' out with zero-bytes to the minimum valid length of an
540 * Ethernet packet, if necessary. */
542 pad_to_minimum_length(struct ofpbuf *buffer)
544 if (buffer->size < ETH_TOTAL_MIN) {
545 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
549 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
550 * must have initialized with sufficient room for the packet. The space
551 * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
552 * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
553 * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
554 * need not be included.)
556 * If a packet is successfully retrieved, returns 0. In this case 'buffer' is
557 * guaranteed to contain at least ETH_TOTAL_MIN bytes. Otherwise, returns a
558 * positive errno value. Returns EAGAIN immediately if no packet is ready to
562 netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
566 assert(buffer->size == 0);
567 assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
569 n_bytes = read(netdev->tap_fd,
570 ofpbuf_tail(buffer), ofpbuf_tailroom(buffer));
571 } while (n_bytes < 0 && errno == EINTR);
573 if (errno != EAGAIN) {
574 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
575 netdev->name, strerror(errno));
579 COVERAGE_INC(netdev_received);
580 buffer->size += n_bytes;
582 /* When the kernel internally sends out an Ethernet frame on an
583 * interface, it gives us a copy *before* padding the frame to the
584 * minimum length. Thus, when it sends out something like an ARP
585 * request, we see a too-short frame. So pad it out to the minimum
587 pad_to_minimum_length(buffer);
592 /* Registers with the poll loop to wake up from the next call to poll_block()
593 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
595 netdev_recv_wait(struct netdev *netdev)
597 poll_fd_wait(netdev->tap_fd, POLLIN);
600 /* Discards all packets waiting to be received from 'netdev'. */
602 netdev_drain(struct netdev *netdev)
604 if (netdev->tap_fd != netdev->netdev_fd) {
605 drain_fd(netdev->tap_fd, netdev->txqlen);
608 return drain_rcvbuf(netdev->netdev_fd);
612 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
613 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
614 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
615 * the packet is too big or too small to transmit on the device.
617 * The caller retains ownership of 'buffer' in all cases.
619 * The kernel maintains a packet transmission queue, so the caller is not
620 * expected to do additional queuing of packets. */
622 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
627 n_bytes = write(netdev->tap_fd, buffer->data, buffer->size);
628 } while (n_bytes < 0 && errno == EINTR);
631 /* The Linux AF_PACKET implementation never blocks waiting for room
632 * for packets, instead returning ENOBUFS. Translate this into EAGAIN
634 if (errno == ENOBUFS) {
636 } else if (errno != EAGAIN) {
637 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
638 netdev->name, strerror(errno));
641 } else if (n_bytes != buffer->size) {
643 "send partial Ethernet packet (%d bytes of %zu) on %s",
644 (int) n_bytes, buffer->size, netdev->name);
647 COVERAGE_INC(netdev_sent);
652 /* Registers with the poll loop to wake up from the next call to poll_block()
653 * when the packet transmission queue has sufficient room to transmit a packet
654 * with netdev_send().
656 * The kernel maintains a packet transmission queue, so the client is not
657 * expected to do additional queuing of packets. Thus, this function is
658 * unlikely to ever be used. It is included for completeness. */
660 netdev_send_wait(struct netdev *netdev)
662 if (netdev->tap_fd == netdev->netdev_fd) {
663 poll_fd_wait(netdev->tap_fd, POLLOUT);
665 /* TAP device always accepts packets.*/
666 poll_immediate_wake();
670 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
671 * otherwise a positive errno value. */
673 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
675 int error = set_etheraddr(netdev->name, netdev->hwaddr_family, mac);
677 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
683 netdev_nodev_set_etheraddr(const char *name, const uint8_t mac[ETH_ADDR_LEN])
686 return set_etheraddr(name, ARPHRD_ETHER, mac);
689 /* Returns a pointer to 'netdev''s MAC address. The caller must not modify or
690 * free the returned buffer. */
692 netdev_get_etheraddr(const struct netdev *netdev)
694 return netdev->etheraddr;
697 /* Returns the name of the network device that 'netdev' represents,
698 * e.g. "eth0". The caller must not modify or free the returned string. */
700 netdev_get_name(const struct netdev *netdev)
705 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
706 * in bytes, not including the hardware header; thus, this is typically 1500
707 * bytes for Ethernet devices. */
709 netdev_get_mtu(const struct netdev *netdev)
714 /* Stores the features supported by 'netdev' into each of '*current',
715 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
716 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
717 * successful, otherwise a positive errno value. On failure, all of the
718 * passed-in values are set to 0. */
720 netdev_get_features(struct netdev *netdev,
721 uint32_t *current, uint32_t *advertised,
722 uint32_t *supported, uint32_t *peer)
725 return do_get_features(netdev,
726 current ? current : &dummy[0],
727 advertised ? advertised : &dummy[1],
728 supported ? supported : &dummy[2],
729 peer ? peer : &dummy[3]);
733 netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
735 struct ethtool_cmd ecmd;
738 memset(&ecmd, 0, sizeof ecmd);
739 error = do_ethtool(netdev, &ecmd, ETHTOOL_GSET, "ETHTOOL_GSET");
744 ecmd.advertising = 0;
745 if (advertise & OFPPF_10MB_HD) {
746 ecmd.advertising |= ADVERTISED_10baseT_Half;
748 if (advertise & OFPPF_10MB_FD) {
749 ecmd.advertising |= ADVERTISED_10baseT_Full;
751 if (advertise & OFPPF_100MB_HD) {
752 ecmd.advertising |= ADVERTISED_100baseT_Half;
754 if (advertise & OFPPF_100MB_FD) {
755 ecmd.advertising |= ADVERTISED_100baseT_Full;
757 if (advertise & OFPPF_1GB_HD) {
758 ecmd.advertising |= ADVERTISED_1000baseT_Half;
760 if (advertise & OFPPF_1GB_FD) {
761 ecmd.advertising |= ADVERTISED_1000baseT_Full;
763 if (advertise & OFPPF_10GB_FD) {
764 ecmd.advertising |= ADVERTISED_10000baseT_Full;
766 if (advertise & OFPPF_COPPER) {
767 ecmd.advertising |= ADVERTISED_TP;
769 if (advertise & OFPPF_FIBER) {
770 ecmd.advertising |= ADVERTISED_FIBRE;
772 if (advertise & OFPPF_AUTONEG) {
773 ecmd.advertising |= ADVERTISED_Autoneg;
775 if (advertise & OFPPF_PAUSE) {
776 ecmd.advertising |= ADVERTISED_Pause;
778 if (advertise & OFPPF_PAUSE_ASYM) {
779 ecmd.advertising |= ADVERTISED_Asym_Pause;
781 return do_ethtool(netdev, &ecmd, ETHTOOL_SSET, "ETHTOOL_SSET");
784 /* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address
785 * and '*mask' to the netmask (if they are non-null) and returns true.
786 * Otherwise, returns false. */
788 netdev_nodev_get_in4(const char *netdev_name, struct in_addr *in4,
789 struct in_addr *mask)
792 struct in_addr ip = { INADDR_ANY };
796 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
797 ifr.ifr_addr.sa_family = AF_INET;
798 COVERAGE_INC(netdev_get_in4);
799 if (ioctl(af_inet_sock, SIOCGIFADDR, &ifr) == 0) {
800 struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
803 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFADDR) failed: %s",
804 netdev_name, strerror(errno));
811 if (ioctl(af_inet_sock, SIOCGIFNETMASK, &ifr) == 0) {
812 struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
813 *mask = sin->sin_addr;
815 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFNETMASK) failed: %s",
816 netdev_name, strerror(errno));
820 return ip.s_addr != INADDR_ANY;
824 netdev_get_in4(const struct netdev *netdev, struct in_addr *in4, struct
827 return netdev_nodev_get_in4(netdev->name, in4, mask);
831 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
833 struct sockaddr_in sin;
834 memset(&sin, 0, sizeof sin);
835 sin.sin_family = AF_INET;
839 memset(sa, 0, sizeof *sa);
840 memcpy(sa, &sin, sizeof sin);
844 do_set_addr(struct netdev *netdev, int sock,
845 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
850 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
851 make_in4_sockaddr(&ifr.ifr_addr, addr);
852 COVERAGE_INC(netdev_set_in4);
853 error = ioctl(sock, ioctl_nr, &ifr) < 0 ? errno : 0;
855 VLOG_WARN("ioctl(%s): %s", ioctl_name, strerror(error));
860 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
861 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
862 * positive errno value. */
864 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
868 error = do_set_addr(netdev, af_inet_sock,
869 SIOCSIFADDR, "SIOCSIFADDR", addr);
870 if (!error && addr.s_addr != INADDR_ANY) {
871 error = do_set_addr(netdev, af_inet_sock,
872 SIOCSIFNETMASK, "SIOCSIFNETMASK", mask);
877 /* Adds 'router' as a default IP gateway. */
879 netdev_add_router(struct in_addr router)
881 struct in_addr any = { INADDR_ANY };
885 memset(&rt, 0, sizeof rt);
886 make_in4_sockaddr(&rt.rt_dst, any);
887 make_in4_sockaddr(&rt.rt_gateway, router);
888 make_in4_sockaddr(&rt.rt_genmask, any);
889 rt.rt_flags = RTF_UP | RTF_GATEWAY;
890 COVERAGE_INC(netdev_add_router);
891 error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
893 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
898 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
899 * 'in6' is non-null) and returns true. Otherwise, returns false. */
901 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
906 return memcmp(&netdev->in6, &in6addr_any, sizeof netdev->in6) != 0;
909 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
910 * Returns 0 if successful, otherwise a positive errno value. On failure,
911 * stores 0 into '*flagsp'. */
913 netdev_get_flags(const struct netdev *netdev, enum netdev_flags *flagsp)
915 return netdev_nodev_get_flags(netdev->name, flagsp);
919 nd_to_iff_flags(enum netdev_flags nd)
922 if (nd & NETDEV_UP) {
925 if (nd & NETDEV_PROMISC) {
931 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
932 * 'on'. If 'permanent' is true, the changes will persist; otherwise, they
933 * will be reverted when 'netdev' is closed or the program exits. Returns 0 if
934 * successful, otherwise a positive errno value. */
936 do_update_flags(struct netdev *netdev, enum netdev_flags off,
937 enum netdev_flags on, bool permanent)
939 int old_flags, new_flags;
942 error = get_flags(netdev->name, &old_flags);
947 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
949 netdev->changed_flags |= new_flags ^ old_flags;
951 if (new_flags != old_flags) {
952 error = set_flags(netdev->name, new_flags);
957 /* Sets the flags for 'netdev' to 'flags'.
958 * If 'permanent' is true, the changes will persist; otherwise, they
959 * will be reverted when 'netdev' is closed or the program exits.
960 * Returns 0 if successful, otherwise a positive errno value. */
962 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
965 return do_update_flags(netdev, -1, flags, permanent);
968 /* Turns on the specified 'flags' on 'netdev'.
969 * If 'permanent' is true, the changes will persist; otherwise, they
970 * will be reverted when 'netdev' is closed or the program exits.
971 * Returns 0 if successful, otherwise a positive errno value. */
973 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
976 return do_update_flags(netdev, 0, flags, permanent);
979 /* Turns off the specified 'flags' on 'netdev'.
980 * If 'permanent' is true, the changes will persist; otherwise, they
981 * will be reverted when 'netdev' is closed or the program exits.
982 * Returns 0 if successful, otherwise a positive errno value. */
984 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
987 return do_update_flags(netdev, flags, 0, permanent);
990 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
991 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
992 * returns 0. Otherwise, it returns a positive errno value; in particular,
993 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
995 netdev_nodev_arp_lookup(const char *netdev_name, uint32_t ip,
996 uint8_t mac[ETH_ADDR_LEN])
999 struct sockaddr_in *pa;
1004 memset(&r, 0, sizeof r);
1005 pa = (struct sockaddr_in *) &r.arp_pa;
1006 pa->sin_family = AF_INET;
1007 pa->sin_addr.s_addr = ip;
1009 r.arp_ha.sa_family = ARPHRD_ETHER;
1011 strncpy(r.arp_dev, netdev_name, sizeof r.arp_dev);
1012 COVERAGE_INC(netdev_arp_lookup);
1013 retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
1015 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
1016 } else if (retval != ENXIO) {
1017 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
1018 netdev_name, IP_ARGS(&ip), strerror(retval));
1024 netdev_arp_lookup(const struct netdev *netdev, uint32_t ip,
1025 uint8_t mac[ETH_ADDR_LEN])
1027 return netdev_nodev_arp_lookup(netdev->name, ip, mac);
1031 get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
1033 struct ofpbuf request;
1034 struct ofpbuf *reply;
1035 struct ifinfomsg *ifi;
1036 const struct rtnl_link_stats *rtnl_stats;
1037 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1040 ofpbuf_init(&request, 0);
1041 nl_msg_put_nlmsghdr(&request, rtnl_sock, sizeof *ifi,
1042 RTM_GETLINK, NLM_F_REQUEST);
1043 ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
1044 ifi->ifi_family = PF_UNSPEC;
1045 ifi->ifi_index = ifindex;
1046 error = nl_sock_transact(rtnl_sock, &request, &reply);
1047 ofpbuf_uninit(&request);
1052 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1053 rtnlgrp_link_policy,
1054 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1055 ofpbuf_delete(reply);
1059 if (!attrs[IFLA_STATS]) {
1060 VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
1061 ofpbuf_delete(reply);
1065 rtnl_stats = nl_attr_get(attrs[IFLA_STATS]);
1066 stats->rx_packets = rtnl_stats->rx_packets;
1067 stats->tx_packets = rtnl_stats->tx_packets;
1068 stats->rx_bytes = rtnl_stats->rx_bytes;
1069 stats->tx_bytes = rtnl_stats->tx_bytes;
1070 stats->rx_errors = rtnl_stats->rx_errors;
1071 stats->tx_errors = rtnl_stats->tx_errors;
1072 stats->rx_dropped = rtnl_stats->rx_dropped;
1073 stats->tx_dropped = rtnl_stats->tx_dropped;
1074 stats->multicast = rtnl_stats->multicast;
1075 stats->collisions = rtnl_stats->collisions;
1076 stats->rx_length_errors = rtnl_stats->rx_length_errors;
1077 stats->rx_over_errors = rtnl_stats->rx_over_errors;
1078 stats->rx_crc_errors = rtnl_stats->rx_crc_errors;
1079 stats->rx_frame_errors = rtnl_stats->rx_frame_errors;
1080 stats->rx_fifo_errors = rtnl_stats->rx_fifo_errors;
1081 stats->rx_missed_errors = rtnl_stats->rx_missed_errors;
1082 stats->tx_aborted_errors = rtnl_stats->tx_aborted_errors;
1083 stats->tx_carrier_errors = rtnl_stats->tx_carrier_errors;
1084 stats->tx_fifo_errors = rtnl_stats->tx_fifo_errors;
1085 stats->tx_heartbeat_errors = rtnl_stats->tx_heartbeat_errors;
1086 stats->tx_window_errors = rtnl_stats->tx_window_errors;
1088 ofpbuf_delete(reply);
1094 get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats)
1096 static const char fn[] = "/proc/net/dev";
1101 stream = fopen(fn, "r");
1103 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1108 while (fgets(line, sizeof line, stream)) {
1111 #define X64 "%"SCNu64
1114 X64 X64 X64 X64 X64 X64 X64 "%*u"
1115 X64 X64 X64 X64 X64 X64 X64 "%*u",
1121 &stats->rx_fifo_errors,
1122 &stats->rx_frame_errors,
1128 &stats->tx_fifo_errors,
1130 &stats->tx_carrier_errors) != 15) {
1131 VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln);
1132 } else if (!strcmp(devname, netdev_name)) {
1133 stats->rx_length_errors = UINT64_MAX;
1134 stats->rx_over_errors = UINT64_MAX;
1135 stats->rx_crc_errors = UINT64_MAX;
1136 stats->rx_missed_errors = UINT64_MAX;
1137 stats->tx_aborted_errors = UINT64_MAX;
1138 stats->tx_heartbeat_errors = UINT64_MAX;
1139 stats->tx_window_errors = UINT64_MAX;
1145 VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name);
1151 netdev_get_carrier(const struct netdev *netdev, bool *carrier)
1153 return netdev_nodev_get_carrier(netdev->name, carrier);
1157 netdev_nodev_get_carrier(const char *netdev_name, bool *carrier)
1167 fn = xasprintf("/sys/class/net/%s/carrier", netdev_name);
1168 fd = open(fn, O_RDONLY);
1171 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(error));
1175 retval = read(fd, line, sizeof line);
1178 if (error == EINVAL) {
1179 /* This is the normal return value when we try to check carrier if
1180 * the network device is not up. */
1182 VLOG_WARN_RL(&rl, "%s: read failed: %s", fn, strerror(error));
1185 } else if (retval == 0) {
1187 VLOG_WARN_RL(&rl, "%s: unexpected end of file", fn);
1191 if (line[0] != '0' && line[0] != '1') {
1193 VLOG_WARN_RL(&rl, "%s: value is %c (expected 0 or 1)", fn, line[0]);
1196 *carrier = line[0] != '0';
1207 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1211 COVERAGE_INC(netdev_get_stats);
1212 if (use_netlink_stats) {
1215 error = get_ifindex(netdev, &ifindex);
1217 error = get_stats_via_netlink(ifindex, stats);
1220 error = get_stats_via_proc(netdev->name, stats);
1224 memset(stats, 0xff, sizeof *stats);
1229 #define POLICE_ADD_CMD "/sbin/tc qdisc add dev %s handle ffff: ingress"
1230 #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"
1231 /* We redirect stderr to /dev/null because we often want to remove all
1232 * traffic control configuration on a port so its in a known state. If
1233 * this done when there is no such configuration, tc complains, so we just
1236 #define POLICE_DEL_CMD "/sbin/tc qdisc del dev %s handle ffff: ingress 2>/dev/null"
1238 /* Attempts to set input rate limiting (policing) policy. */
1240 netdev_nodev_set_policing(const char *netdev_name, uint32_t kbits_rate,
1241 uint32_t kbits_burst)
1247 COVERAGE_INC(netdev_set_policing);
1250 /* Default to 10 kilobits if not specified. */
1254 /* xxx This should be more careful about only adding if it
1255 * xxx actually exists, as opposed to always deleting it. */
1256 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
1257 if (system(command) == -1) {
1258 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
1261 snprintf(command, sizeof(command), POLICE_ADD_CMD, netdev_name);
1262 if (system(command) != 0) {
1263 VLOG_WARN_RL(&rl, "%s: problem adding policing", netdev_name);
1267 snprintf(command, sizeof(command), POLICE_CONFIG_CMD, netdev_name,
1268 kbits_rate, kbits_burst);
1269 if (system(command) != 0) {
1270 VLOG_WARN_RL(&rl, "%s: problem configuring policing",
1275 snprintf(command, sizeof(command), POLICE_DEL_CMD, netdev_name);
1276 if (system(command) == -1) {
1277 VLOG_WARN_RL(&rl, "%s: problem removing policing", netdev_name);
1285 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1286 uint32_t kbits_burst)
1288 return netdev_nodev_set_policing(netdev->name, kbits_rate, kbits_burst);
1291 /* Initializes 'svec' with a list of the names of all known network devices. */
1293 netdev_enumerate(struct svec *svec)
1295 struct if_nameindex *names;
1298 names = if_nameindex();
1302 for (i = 0; names[i].if_name != NULL; i++) {
1303 svec_add(svec, names[i].if_name);
1305 if_freenameindex(names);
1307 VLOG_WARN("could not obtain list of network device names: %s",
1312 /* Attempts to locate a device based on its IPv4 address. The caller
1313 * may provide a hint as to the device by setting 'netdev_name' to a
1314 * likely device name. This string must be malloc'd, since if it is
1315 * not correct then it will be freed. If there is no hint, then
1316 * 'netdev_name' must be the NULL pointer.
1318 * If the device is found, the return value will be true and 'netdev_name'
1319 * contains the device's name as a string, which the caller is responsible
1320 * for freeing. If the device is not found, the return value is false. */
1322 netdev_find_dev_by_in4(const struct in_addr *in4, char **netdev_name)
1325 struct in_addr dev_in4;
1326 struct svec dev_list;
1328 /* Check the hint first. */
1329 if (*netdev_name && (netdev_nodev_get_in4(*netdev_name, &dev_in4, NULL))
1330 && (dev_in4.s_addr == in4->s_addr)) {
1335 *netdev_name = NULL;
1336 netdev_enumerate(&dev_list);
1338 for (i=0; i<dev_list.n; i++) {
1339 if ((netdev_nodev_get_in4(dev_list.names[i], &dev_in4, NULL))
1340 && (dev_in4.s_addr == in4->s_addr)) {
1341 *netdev_name = xstrdup(dev_list.names[i]);
1342 svec_destroy(&dev_list);
1347 svec_destroy(&dev_list);
1351 /* Looks up the next hop for 'ip'. If the next hop can be found, the
1352 * address is stored in 'next_hop'. If a gateway is not required to
1353 * reach 'ip', zero is stored in 'next_hop'. In either case, zero is
1354 * returned and a copy of the name of the device to reach 'ip' is stored
1355 * in 'netdev_name', which the caller is responsible for freeing. If a
1356 * route could not be determined, a positive errno is returned. */
1358 netdev_get_next_hop(const struct in_addr *host, struct in_addr *next_hop,
1361 static const char fn[] = "/proc/net/route";
1366 *netdev_name = NULL;
1367 stream = fopen(fn, "r");
1368 if (stream == NULL) {
1369 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1374 while (fgets(line, sizeof line, stream)) {
1377 uint32_t dest, gateway, mask;
1378 int refcnt, metric, mtu;
1379 unsigned int flags, use, window, irtt;
1382 "%16s %"SCNx32" %"SCNx32" %04X %d %u %d %"SCNx32
1384 iface, &dest, &gateway, &flags, &refcnt,
1385 &use, &metric, &mask, &mtu, &window, &irtt) != 11) {
1387 VLOG_WARN_RL(&rl, "%s: could not parse line %d: %s",
1391 if (!(flags & RTF_UP)) {
1392 /* Skip routes that aren't up. */
1396 /* The output of 'dest', 'mask', and 'gateway' were given in
1397 * network byte order, so we don't need need any endian
1398 * conversions here. */
1399 if ((dest & mask) == (host->s_addr & mask)) {
1401 /* The host is directly reachable. */
1402 next_hop->s_addr = 0;
1404 /* To reach the host, we must go through a gateway. */
1405 next_hop->s_addr = gateway;
1407 *netdev_name = xstrdup(iface);
1418 /* Obtains the current flags for the network device named 'netdev_name' and
1419 * stores them into '*flagsp'. Returns 0 if successful, otherwise a positive
1420 * errno value. On error, stores 0 into '*flagsp'.
1422 * If only device flags are needed, this is more efficient than calling
1423 * netdev_open(), netdev_get_flags(), netdev_close(). */
1425 netdev_nodev_get_flags(const char *netdev_name, enum netdev_flags *flagsp)
1432 error = get_flags(netdev_name, &flags);
1437 if (flags & IFF_UP) {
1438 *flagsp |= NETDEV_UP;
1440 if (flags & IFF_PROMISC) {
1441 *flagsp |= NETDEV_PROMISC;
1447 netdev_nodev_get_etheraddr(const char *netdev_name, uint8_t mac[6])
1451 return get_etheraddr(netdev_name, mac, NULL);
1454 /* If 'netdev_name' is the name of a VLAN network device (e.g. one created with
1455 * vconfig(8)), sets '*vlan_vid' to the VLAN VID associated with that device
1456 * and returns 0. Otherwise returns a errno value (specifically ENOENT if
1457 * 'netdev_name' is the name of a network device that is not a VLAN device) and
1458 * sets '*vlan_vid' to -1. */
1460 netdev_get_vlan_vid(const char *netdev_name, int *vlan_vid)
1462 struct ds line = DS_EMPTY_INITIALIZER;
1463 FILE *stream = NULL;
1467 COVERAGE_INC(netdev_get_vlan_vid);
1468 fn = xasprintf("/proc/net/vlan/%s", netdev_name);
1469 stream = fopen(fn, "r");
1475 if (ds_get_line(&line, stream)) {
1476 if (ferror(stream)) {
1478 VLOG_ERR_RL(&rl, "error reading \"%s\": %s", fn, strerror(errno));
1481 VLOG_ERR_RL(&rl, "unexpected end of file reading \"%s\"", fn);
1486 if (!sscanf(ds_cstr(&line), "%*s VID: %d", vlan_vid)) {
1488 VLOG_ERR_RL(&rl, "parse error reading \"%s\" line 1: \"%s\"",
1489 fn, ds_cstr(&line));
1507 static void restore_all_flags(void *aux);
1509 /* Set up a signal hook to restore network device flags on program
1521 fatal_signal_add_hook(restore_all_flags, NULL, true);
1523 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
1524 if (af_inet_sock < 0) {
1525 ovs_fatal(errno, "socket(AF_INET)");
1528 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0, &rtnl_sock);
1530 ovs_fatal(error, "socket(AF_NETLINK, NETLINK_ROUTE)");
1533 /* Decide on the netdev_get_stats() implementation to use. Netlink is
1534 * preferable, so if that works, we'll use it. */
1535 ifindex = do_get_ifindex("lo");
1537 VLOG_WARN("failed to get ifindex for lo, "
1538 "obtaining netdev stats from proc");
1539 use_netlink_stats = false;
1541 struct netdev_stats stats;
1542 error = get_stats_via_netlink(ifindex, &stats);
1544 VLOG_DBG("obtaining netdev stats via rtnetlink");
1545 use_netlink_stats = true;
1547 VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
1548 "via proc (you are probably running a pre-2.6.19 "
1549 "kernel)", strerror(error));
1550 use_netlink_stats = false;
1556 /* Restore the network device flags on 'netdev' to those that were active
1557 * before we changed them. Returns 0 if successful, otherwise a positive
1560 * To avoid reentry, the caller must ensure that fatal signals are blocked. */
1562 restore_flags(struct netdev *netdev)
1567 /* Get current flags. */
1568 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1569 COVERAGE_INC(netdev_get_flags);
1570 if (ioctl(netdev->netdev_fd, SIOCGIFFLAGS, &ifr) < 0) {
1574 /* Restore flags that we might have changed, if necessary. */
1575 restore_flags = netdev->changed_flags & (IFF_PROMISC | IFF_UP);
1576 if ((ifr.ifr_flags ^ netdev->save_flags) & restore_flags) {
1577 ifr.ifr_flags &= ~restore_flags;
1578 ifr.ifr_flags |= netdev->save_flags & restore_flags;
1579 COVERAGE_INC(netdev_set_flags);
1580 if (ioctl(netdev->netdev_fd, SIOCSIFFLAGS, &ifr) < 0) {
1588 /* Retores all the flags on all network devices that we modified. Called from
1589 * a signal handler, so it does not attempt to report error conditions. */
1591 restore_all_flags(void *aux UNUSED)
1593 struct netdev *netdev;
1594 LIST_FOR_EACH (netdev, struct netdev, node, &netdev_list) {
1595 restore_flags(netdev);
1600 get_flags(const char *netdev_name, int *flags)
1603 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1604 COVERAGE_INC(netdev_get_flags);
1605 if (ioctl(af_inet_sock, SIOCGIFFLAGS, &ifr) < 0) {
1606 VLOG_ERR("ioctl(SIOCGIFFLAGS) on %s device failed: %s",
1607 netdev_name, strerror(errno));
1610 *flags = ifr.ifr_flags;
1615 set_flags(const char *netdev_name, int flags)
1618 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1619 ifr.ifr_flags = flags;
1620 COVERAGE_INC(netdev_set_flags);
1621 if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) < 0) {
1622 VLOG_ERR("ioctl(SIOCSIFFLAGS) on %s device failed: %s",
1623 netdev_name, strerror(errno));
1630 do_get_ifindex(const char *netdev_name)
1634 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1635 COVERAGE_INC(netdev_get_ifindex);
1636 if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) {
1637 VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
1638 netdev_name, strerror(errno));
1641 return ifr.ifr_ifindex;
1645 get_ifindex(const struct netdev *netdev, int *ifindexp)
1648 if (netdev->ifindex < 0) {
1649 int ifindex = do_get_ifindex(netdev->name);
1653 ((struct netdev *) netdev)->ifindex = ifindex;
1655 *ifindexp = netdev->ifindex;
1660 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN],
1661 int *hwaddr_familyp)
1665 memset(&ifr, 0, sizeof ifr);
1666 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1667 COVERAGE_INC(netdev_get_hwaddr);
1668 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
1669 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
1670 netdev_name, strerror(errno));
1673 if (hwaddr_familyp) {
1674 int hwaddr_family = ifr.ifr_hwaddr.sa_family;
1675 *hwaddr_familyp = hwaddr_family;
1676 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
1677 VLOG_WARN("%s device has unknown hardware address family %d",
1678 netdev_name, hwaddr_family);
1681 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
1686 set_etheraddr(const char *netdev_name, int hwaddr_family,
1687 const uint8_t mac[ETH_ADDR_LEN])
1691 memset(&ifr, 0, sizeof ifr);
1692 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1693 ifr.ifr_hwaddr.sa_family = hwaddr_family;
1694 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
1695 COVERAGE_INC(netdev_set_hwaddr);
1696 if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) {
1697 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
1698 netdev_name, strerror(errno));