2 * Copyright (c) 2011 Gaetano Catalli.
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.
19 #include "netdev-provider.h"
23 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
27 #include <sys/sockio.h>
29 #include <pcap/pcap.h>
31 #include <net/if_dl.h>
32 #include <net/if_media.h>
33 #include <net/if_tap.h>
34 #include <netinet/in.h>
35 #include <net/if_mib.h>
39 #include <sys/sysctl.h>
43 #include "dynamic-string.h"
44 #include "fatal-signal.h"
46 #include "openflow/openflow.h"
48 #include "poll-loop.h"
49 #include "socket-util.h"
55 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
58 struct netdev_rx_bsd {
61 /* Packet capture descriptor for a system network device.
62 * For a tap device this is NULL. */
65 /* Selectable file descriptor for the network device.
66 * This descriptor will be used for polling operations. */
70 static const struct netdev_rx_class netdev_rx_bsd_class;
74 unsigned int cache_valid;
75 unsigned int change_seq;
78 uint8_t etheraddr[ETH_ADDR_LEN];
84 int tap_fd; /* TAP character device, if any, otherwise -1. */
86 /* Used for sending packets on non-tap devices. */
93 VALID_IFINDEX = 1 << 0,
94 VALID_ETHERADDR = 1 << 1,
98 VALID_CARRIER = 1 << 5
101 /* An AF_INET socket (used for ioctl operations). */
102 static int af_inet_sock = -1;
104 #define PCAP_SNAPLEN 2048
108 * Notifier used to invalidate device informations in case of status change.
110 * It will be registered with a 'rtbsd_notifier_register()' when the first
111 * device will be created with the call of either 'netdev_bsd_tap_create()' or
112 * 'netdev_bsd_system_create()'.
114 * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
115 * invalidate cached information about the device.
117 static struct rtbsd_notifier netdev_bsd_cache_notifier;
118 static int cache_notifier_refcount;
120 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
122 static int netdev_bsd_do_ioctl(const char *, struct ifreq *, unsigned long cmd,
123 const char *cmd_name);
124 static void destroy_tap(int fd, const char *name);
125 static int get_flags(const struct netdev *, int *flagsp);
126 static int set_flags(const char *, int flags);
127 static int do_set_addr(struct netdev *netdev,
128 int ioctl_nr, const char *ioctl_name,
129 struct in_addr addr);
130 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
131 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
132 int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
133 static int get_ifindex(const struct netdev *, int *ifindexp);
135 static int netdev_bsd_init(void);
138 is_netdev_bsd_class(const struct netdev_class *netdev_class)
140 return netdev_class->init == netdev_bsd_init;
143 static struct netdev_bsd *
144 netdev_bsd_cast(const struct netdev *netdev)
146 ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev)));
147 return CONTAINER_OF(netdev, struct netdev_bsd, up);
150 static struct netdev_rx_bsd *
151 netdev_rx_bsd_cast(const struct netdev_rx *rx)
153 netdev_rx_assert_class(rx, &netdev_rx_bsd_class);
154 return CONTAINER_OF(rx, struct netdev_rx_bsd, up);
157 /* Initialize the AF_INET socket used for ioctl operations */
159 netdev_bsd_init(void)
161 static int status = -1;
163 if (status >= 0) { /* already initialized */
167 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
168 status = af_inet_sock >= 0 ? 0 : errno;
171 VLOG_ERR("failed to create inet socket: %s", strerror(status));
178 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
179 * interface status changes, and eventually calls all the user callbacks.
184 rtbsd_notifier_run();
188 * Arranges for poll_block() to wake up if the "run" member function needs to
192 netdev_bsd_wait(void)
194 rtbsd_notifier_wait();
198 netdev_bsd_changed(struct netdev_bsd *dev)
201 if (!dev->change_seq) {
206 /* Invalidate cache in case of interface status change. */
208 netdev_bsd_cache_cb(const struct rtbsd_change *change,
209 void *aux OVS_UNUSED)
211 struct netdev_bsd *dev;
214 struct netdev *base_dev = netdev_from_name(change->if_name);
217 const struct netdev_class *netdev_class =
218 netdev_get_class(base_dev);
220 if (is_netdev_bsd_class(netdev_class)) {
221 dev = netdev_bsd_cast(base_dev);
222 dev->cache_valid = 0;
223 netdev_bsd_changed(dev);
228 * XXX the API is lacking, we should be able to iterate on the list of
229 * netdevs without having to store the info in a temp shash.
231 struct shash device_shash;
232 struct shash_node *node;
234 shash_init(&device_shash);
235 netdev_get_devices(&netdev_bsd_class, &device_shash);
236 SHASH_FOR_EACH (node, &device_shash) {
238 dev->cache_valid = 0;
239 netdev_bsd_changed(dev);
241 shash_destroy(&device_shash);
246 cache_notifier_ref(void)
250 if (!cache_notifier_refcount) {
251 ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
252 netdev_bsd_cache_cb, NULL);
257 cache_notifier_refcount++;
262 cache_notifier_unref(void)
264 cache_notifier_refcount--;
265 if (cache_notifier_refcount == 0) {
266 rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
271 /* Allocate a netdev_bsd structure */
273 netdev_bsd_create_system(const struct netdev_class *class, const char *name,
274 struct netdev **netdevp)
276 struct netdev_bsd *netdev;
277 enum netdev_flags flags;
280 error = cache_notifier_ref();
285 netdev = xzalloc(sizeof *netdev);
286 netdev->change_seq = 1;
287 netdev_init(&netdev->up, name, class);
290 /* Verify that the netdev really exists by attempting to read its flags */
291 error = netdev_get_flags(&netdev->up, &flags);
292 if (error == ENXIO) {
293 netdev_uninit(&netdev->up, false);
295 cache_notifier_unref();
299 *netdevp = &netdev->up;
304 * Allocate a netdev_bsd structure with 'tap' class.
307 netdev_bsd_create_tap(const struct netdev_class *class, const char *name,
308 struct netdev **netdevp)
310 struct netdev_bsd *netdev = NULL;
314 error = cache_notifier_ref();
319 /* allocate the device structure and set the internal flag */
320 netdev = xzalloc(sizeof *netdev);
322 memset(&ifr, 0, sizeof(ifr));
324 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
325 * to retrieve the name of the tap device. */
326 netdev->tap_fd = open("/dev/tap", O_RDWR);
327 netdev->change_seq = 1;
328 if (netdev->tap_fd < 0) {
330 VLOG_WARN("opening \"/dev/tap\" failed: %s", strerror(error));
331 goto error_undef_notifier;
334 /* Retrieve tap name (e.g. tap0) */
335 if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
336 /* XXX Need to destroy the device? */
338 goto error_undef_notifier;
341 /* Change the name of the tap device */
342 ifr.ifr_data = (void *)name;
343 if (ioctl(af_inet_sock, SIOCSIFNAME, &ifr) == -1) {
345 destroy_tap(netdev->tap_fd, ifr.ifr_name);
346 goto error_undef_notifier;
349 /* set non-blocking. */
350 error = set_nonblocking(netdev->tap_fd);
352 destroy_tap(netdev->tap_fd, name);
353 goto error_undef_notifier;
357 ifr.ifr_flags = (uint16_t)IFF_UP;
358 ifr.ifr_flagshigh = 0;
359 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
360 if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) {
362 destroy_tap(netdev->tap_fd, name);
363 goto error_undef_notifier;
366 /* initialize the device structure and
367 * link the structure to its netdev */
368 netdev_init(&netdev->up, name, class);
369 *netdevp = &netdev->up;
373 error_undef_notifier:
374 cache_notifier_unref();
381 netdev_bsd_destroy(struct netdev *netdev_)
383 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
385 cache_notifier_unref();
387 if (netdev->tap_fd >= 0) {
388 destroy_tap(netdev->tap_fd, netdev_get_name(netdev_));
391 pcap_close(netdev->pcap);
397 netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
399 char errbuf[PCAP_ERRBUF_SIZE];
405 /* Open the pcap device. The device is opened in non-promiscuous mode
406 * because the interface flags are manually set by the caller. */
408 pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
410 VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
414 if (errbuf[0] != '\0') {
415 VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
418 /* Get the underlying fd. */
419 fd = pcap_get_selectable_fd(pcap);
421 VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
426 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
427 * on the file descriptor returned by pcap_get_selectable_fd to achieve
428 * a real non-blocking behaviour.*/
429 error = pcap_setnonblock(pcap, 1, errbuf);
435 /* This call assure that reads return immediately upon packet
436 * reception. Otherwise, a read will block until either the kernel
437 * buffer becomes full or a timeout occurs. */
438 if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
439 VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
440 name, strerror(errno));
445 /* Capture only incoming packets. */
446 error = pcap_setdirection(pcap, PCAP_D_IN);
466 netdev_bsd_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
468 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
470 struct netdev_rx_bsd *rx;
474 if (!strcmp(netdev_get_type(netdev_), "tap")) {
478 int error = netdev_bsd_open_pcap(netdev_get_name(netdev_), &pcap, &fd);
483 netdev_bsd_changed(netdev);
486 rx = xmalloc(sizeof *rx);
487 netdev_rx_init(&rx->up, netdev_, &netdev_rx_bsd_class);
488 rx->pcap_handle = pcap;
496 netdev_rx_bsd_destroy(struct netdev_rx *rx_)
498 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
500 if (rx->pcap_handle) {
501 pcap_close(rx->pcap_handle);
506 /* The recv callback of the netdev class returns the number of bytes of the
509 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
510 * not make difference between a missing packet on the capture interface and
511 * an error during the file capture. We can use the pcap_dispatch() function
512 * instead, which is able to distinguish between errors and null packet.
514 * To make pcap_dispatch() returns the number of bytes read from the interface
515 * we need to define the following callback and argument.
524 * This callback will be executed on every captured packet.
526 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
527 * pcap returns a truncated packet and we follow this behavior.
529 * The argument args->retval is the packet size in bytes.
532 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
534 struct pcap_arg *args = (struct pcap_arg *)args_;
536 if (args->size < hdr->len) {
537 VLOG_WARN_RL(&rl, "packet truncated");
538 args->retval = args->size;
540 args->retval = hdr->len;
543 /* copy the packet to our buffer */
544 memcpy(args->data, packet, args->retval);
548 * This function attempts to receive a packet from the specified network
549 * device. It is assumed that the network device is a system device or a tap
550 * device opened as a system one. In this case the read operation is performed
554 netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, void *data, size_t size)
559 /* prepare the pcap argument to store the packet */
564 ret = pcap_dispatch(rx->pcap_handle, 1, proc_pkt, (u_char *) &arg);
567 return arg.retval; /* arg.retval < 0 is handled in the caller */
570 if (errno == EINTR) {
580 * This function attempts to receive a packet from the specified network
581 * device. It is assumed that the network device is a tap device and
582 * 'rx->fd' is initialized with the tap file descriptor.
585 netdev_rx_bsd_recv_tap(struct netdev_rx_bsd *rx, void *data, size_t size)
588 ssize_t retval = read(rx->fd, data, size);
591 } else if (errno != EINTR) {
592 if (errno != EAGAIN) {
593 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
594 strerror(errno), netdev_rx_get_name(&rx->up));
603 netdev_rx_bsd_recv(struct netdev_rx *rx_, void *data, size_t size)
605 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
607 return (rx->pcap_handle
608 ? netdev_rx_bsd_recv_pcap(rx, data, size)
609 : netdev_rx_bsd_recv_tap(rx, data, size));
613 * Registers with the poll loop to wake up from the next call to poll_block()
614 * when a packet is ready to be received with netdev_rx_recv() on 'rx'.
617 netdev_rx_bsd_wait(struct netdev_rx *rx_)
619 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
621 poll_fd_wait(rx->fd, POLLIN);
624 /* Discards all packets waiting to be received from 'rx'. */
626 netdev_rx_bsd_drain(struct netdev_rx *rx_)
629 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
631 strcpy(ifr.ifr_name, netdev_rx_get_name(rx_));
632 if (ioctl(rx->fd, BIOCFLUSH, &ifr) == -1) {
633 VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
634 netdev_rx_get_name(rx_), strerror(errno));
641 * Send a packet on the specified network device. The device could be either a
642 * system or a tap device.
645 netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
647 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
648 const char *name = netdev_get_name(netdev_);
650 if (dev->tap_fd < 0 && !dev->pcap) {
651 int error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
659 if (dev->tap_fd >= 0) {
660 retval = write(dev->tap_fd, data, size);
662 retval = pcap_inject(dev->pcap, data, size);
665 if (errno == EINTR) {
667 } else if (errno != EAGAIN) {
668 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
669 name, strerror(errno));
672 } else if (retval != size) {
673 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
674 "%zu) on %s", retval, size, name);
683 * Registers with the poll loop to wake up from the next call to poll_block()
684 * when the packet transmission queue has sufficient room to transmit a packet
685 * with netdev_send().
688 netdev_bsd_send_wait(struct netdev *netdev_)
690 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
692 if (dev->tap_fd >= 0) {
693 /* TAP device always accepts packets. */
694 poll_immediate_wake();
695 } else if (dev->pcap) {
696 poll_fd_wait(dev->fd, POLLOUT);
698 /* We haven't even tried to send a packet yet. */
699 poll_immediate_wake();
704 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
705 * otherwise a positive errno value.
708 netdev_bsd_set_etheraddr(struct netdev *netdev_,
709 const uint8_t mac[ETH_ADDR_LEN])
711 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
714 if (!(netdev->cache_valid & VALID_ETHERADDR)
715 || !eth_addr_equals(netdev->etheraddr, mac)) {
716 error = set_etheraddr(netdev_get_name(netdev_), AF_LINK, ETH_ADDR_LEN,
719 netdev->cache_valid |= VALID_ETHERADDR;
720 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
721 netdev_bsd_changed(netdev);
730 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
731 * free the returned buffer.
734 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
735 uint8_t mac[ETH_ADDR_LEN])
737 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
739 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
740 int error = get_etheraddr(netdev_get_name(netdev_),
745 netdev->cache_valid |= VALID_ETHERADDR;
747 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
753 * Returns the maximum size of transmitted (and received) packets on 'netdev',
754 * in bytes, not including the hardware header; thus, this is typically 1500
755 * bytes for Ethernet devices.
758 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
760 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
762 if (!(netdev->cache_valid & VALID_MTU)) {
766 error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr, SIOCGIFMTU,
771 netdev->mtu = ifr.ifr_mtu;
772 netdev->cache_valid |= VALID_MTU;
780 netdev_bsd_get_ifindex(const struct netdev *netdev)
784 error = get_ifindex(netdev, &ifindex);
785 return error ? -error : ifindex;
789 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
791 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
793 if (!(netdev->cache_valid & VALID_CARRIER)) {
794 struct ifmediareq ifmr;
796 memset(&ifmr, 0, sizeof(ifmr));
797 strncpy(ifmr.ifm_name, netdev_get_name(netdev_), sizeof ifmr.ifm_name);
799 if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
800 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
801 netdev_get_name(netdev_), strerror(errno));
805 netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
806 netdev->cache_valid |= VALID_CARRIER;
808 /* If the interface doesn't report whether the media is active,
809 * just assume it is active. */
810 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
811 netdev->carrier = true;
814 *carrier = netdev->carrier;
819 /* Retrieves current device stats for 'netdev'. */
821 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
826 struct ifmibdata ifmd;
831 mib[2] = NETLINK_GENERIC;
832 mib[3] = IFMIB_SYSTEM;
833 mib[4] = IFMIB_IFCOUNT;
835 len = sizeof(if_count);
837 if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
838 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
839 netdev_get_name(netdev_), strerror(errno));
843 mib[5] = IFDATA_GENERAL;
844 mib[3] = IFMIB_IFDATA;
846 for (i = 1; i <= if_count; i++) {
848 if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
849 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
850 netdev_get_name(netdev_), strerror(errno));
852 } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
853 stats->rx_packets = ifmd.ifmd_data.ifi_ipackets;
854 stats->tx_packets = ifmd.ifmd_data.ifi_opackets;
855 stats->rx_bytes = ifmd.ifmd_data.ifi_ibytes;
856 stats->tx_bytes = ifmd.ifmd_data.ifi_obytes;
857 stats->rx_errors = ifmd.ifmd_data.ifi_ierrors;
858 stats->tx_errors = ifmd.ifmd_data.ifi_oerrors;
859 stats->rx_dropped = ifmd.ifmd_data.ifi_iqdrops;
860 stats->tx_dropped = UINT64_MAX;
861 stats->multicast = ifmd.ifmd_data.ifi_imcasts;
862 stats->collisions = ifmd.ifmd_data.ifi_collisions;
864 stats->rx_length_errors = UINT64_MAX;
865 stats->rx_over_errors = UINT64_MAX;
866 stats->rx_crc_errors = UINT64_MAX;
867 stats->rx_frame_errors = UINT64_MAX;
868 stats->rx_fifo_errors = UINT64_MAX;
869 stats->rx_missed_errors = UINT64_MAX;
871 stats->tx_aborted_errors = UINT64_MAX;
872 stats->tx_carrier_errors = UINT64_MAX;
873 stats->tx_fifo_errors = UINT64_MAX;
874 stats->tx_heartbeat_errors = UINT64_MAX;
875 stats->tx_window_errors = UINT64_MAX;
884 netdev_bsd_parse_media(int media)
886 uint32_t supported = 0;
887 bool half_duplex = media & IFM_HDX ? true : false;
889 switch (IFM_SUBTYPE(media)) {
894 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
895 supported |= NETDEV_F_COPPER;
899 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
900 supported |= NETDEV_F_FIBER;
907 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
908 supported |= NETDEV_F_COPPER;
912 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
913 supported |= NETDEV_F_FIBER;
918 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
919 supported |= NETDEV_F_COPPER;
924 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
925 supported |= NETDEV_F_FIBER;
929 supported |= NETDEV_F_10GB_FD;
930 supported |= NETDEV_F_COPPER;
935 supported |= NETDEV_F_10GB_FD;
936 supported |= NETDEV_F_FIBER;
943 if (IFM_SUBTYPE(media) == IFM_AUTO) {
944 supported |= NETDEV_F_AUTONEG;
947 if (media & IFM_ETH_FMASK) {
948 supported |= NETDEV_F_PAUSE;
956 * Stores the features supported by 'netdev' into each of '*current',
957 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
958 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
959 * successful, otherwise a positive errno value. On failure, all of the
960 * passed-in values are set to 0.
963 netdev_bsd_get_features(const struct netdev *netdev,
964 enum netdev_features *current, uint32_t *advertised,
965 enum netdev_features *supported, uint32_t *peer)
967 struct ifmediareq ifmr;
973 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
975 memset(&ifmr, 0, sizeof(ifmr));
976 strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
978 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
979 * number of supported modes, and a second with a buffer to retrieve
981 if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
982 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
983 netdev_get_name(netdev), strerror(errno));
987 media_list = xcalloc(ifmr.ifm_count, sizeof(int));
988 ifmr.ifm_ulist = media_list;
990 if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
991 VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
992 netdev_get_name(netdev));
997 if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
998 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
999 netdev_get_name(netdev), strerror(errno));
1004 /* Current settings. */
1005 *current = netdev_bsd_parse_media(ifmr.ifm_active);
1007 /* Advertised features. */
1008 *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1010 /* Supported features. */
1012 for (i = 0; i < ifmr.ifm_count; i++) {
1013 *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1016 /* Peer advertisements. */
1017 *peer = 0; /* XXX */
1026 * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
1027 * 'in4' is non-null) and returns true. Otherwise, returns false.
1030 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1031 struct in_addr *netmask)
1033 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1035 if (!(netdev->cache_valid & VALID_IN4)) {
1036 const struct sockaddr_in *sin;
1040 ifr.ifr_addr.sa_family = AF_INET;
1041 error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr,
1042 SIOCGIFADDR, "SIOCGIFADDR");
1047 sin = (struct sockaddr_in *) &ifr.ifr_addr;
1048 netdev->in4 = sin->sin_addr;
1049 netdev->cache_valid |= VALID_IN4;
1050 error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr,
1051 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1055 *netmask = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr;
1059 return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1063 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1064 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1065 * positive errno value.
1068 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1069 struct in_addr mask)
1071 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1074 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1076 netdev->cache_valid |= VALID_IN4;
1078 if (addr.s_addr != INADDR_ANY) {
1079 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1080 "SIOCSIFNETMASK", mask);
1082 netdev_bsd_changed(netdev);
1088 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1090 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1091 if (!(netdev->cache_valid & VALID_IN6)) {
1092 struct ifaddrs *ifa, *head;
1093 struct sockaddr_in6 *sin6;
1094 const char *netdev_name = netdev_get_name(netdev_);
1096 if (getifaddrs(&head) != 0) {
1097 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1102 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1103 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1104 !strcmp(ifa->ifa_name, netdev_name)) {
1105 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1107 memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
1108 netdev->cache_valid |= VALID_IN6;
1115 return EADDRNOTAVAIL;
1122 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1124 struct sockaddr_in sin;
1125 memset(&sin, 0, sizeof sin);
1126 sin.sin_family = AF_INET;
1127 sin.sin_addr = addr;
1130 memset(sa, 0, sizeof *sa);
1131 memcpy(sa, &sin, sizeof sin);
1135 do_set_addr(struct netdev *netdev,
1136 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1139 make_in4_sockaddr(&ifr.ifr_addr, addr);
1140 return netdev_bsd_do_ioctl(netdev, &ifr, ioctl_nr, ioctl_name);
1144 nd_to_iff_flags(enum netdev_flags nd)
1147 if (nd & NETDEV_UP) {
1150 if (nd & NETDEV_PROMISC) {
1152 iff |= IFF_PPROMISC;
1158 iff_to_nd_flags(int iff)
1160 enum netdev_flags nd = 0;
1164 if (iff & IFF_PROMISC) {
1165 nd |= NETDEV_PROMISC;
1171 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1172 enum netdev_flags on, enum netdev_flags *old_flagsp)
1174 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1175 int old_flags, new_flags;
1178 error = get_flags(netdev_, &old_flags);
1180 *old_flagsp = iff_to_nd_flags(old_flags);
1181 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1182 if (new_flags != old_flags) {
1183 error = set_flags(netdev_get_name(netdev_), new_flags);
1184 netdev_bsd_changed(netdev);
1191 netdev_bsd_change_seq(const struct netdev *netdev)
1193 return netdev_bsd_cast(netdev)->change_seq;
1197 const struct netdev_class netdev_bsd_class = {
1203 netdev_bsd_create_system,
1205 NULL, /* get_config */
1206 NULL, /* set_config */
1207 NULL, /* get_tunnel_config */
1212 netdev_bsd_send_wait,
1214 netdev_bsd_set_etheraddr,
1215 netdev_bsd_get_etheraddr,
1218 netdev_bsd_get_ifindex,
1219 netdev_bsd_get_carrier,
1220 NULL, /* get_carrier_resets */
1221 NULL, /* set_miimon_interval */
1222 netdev_bsd_get_stats,
1223 NULL, /* set_stats */
1225 netdev_bsd_get_features,
1226 NULL, /* set_advertisement */
1227 NULL, /* set_policing */
1228 NULL, /* get_qos_type */
1229 NULL, /* get_qos_capabilities */
1232 NULL, /* get_queue */
1233 NULL, /* set_queue */
1234 NULL, /* delete_queue */
1235 NULL, /* get_queue_stats */
1236 NULL, /* dump_queue */
1237 NULL, /* dump_queue_stats */
1242 NULL, /* add_router */
1243 NULL, /* get_next_hop */
1244 NULL, /* get_status */
1245 NULL, /* arp_lookup */
1247 netdev_bsd_update_flags,
1249 netdev_bsd_change_seq
1252 const struct netdev_class netdev_tap_class = {
1258 netdev_bsd_create_tap,
1260 NULL, /* get_config */
1261 NULL, /* set_config */
1262 NULL, /* get_tunnel_config */
1267 netdev_bsd_send_wait,
1269 netdev_bsd_set_etheraddr,
1270 netdev_bsd_get_etheraddr,
1273 netdev_bsd_get_ifindex,
1274 netdev_bsd_get_carrier,
1275 NULL, /* get_carrier_resets */
1276 NULL, /* set_miimon_interval */
1277 netdev_bsd_get_stats,
1278 NULL, /* set_stats */
1280 netdev_bsd_get_features,
1281 NULL, /* set_advertisement */
1282 NULL, /* set_policing */
1283 NULL, /* get_qos_type */
1284 NULL, /* get_qos_capabilities */
1287 NULL, /* get_queue */
1288 NULL, /* set_queue */
1289 NULL, /* delete_queue */
1290 NULL, /* get_queue_stats */
1291 NULL, /* dump_queue */
1292 NULL, /* dump_queue_stats */
1297 NULL, /* add_router */
1298 NULL, /* get_next_hop */
1299 NULL, /* get_status */
1300 NULL, /* arp_lookup */
1302 netdev_bsd_update_flags,
1304 netdev_bsd_change_seq
1307 static const struct netdev_rx_class netdev_rx_bsd_class = {
1308 netdev_rx_bsd_destroy,
1311 netdev_rx_bsd_drain,
1316 destroy_tap(int fd, const char *name)
1321 strcpy(ifr.ifr_name, name);
1322 /* XXX What to do if this call fails? */
1323 ioctl(af_inet_sock, SIOCIFDESTROY, &ifr);
1327 get_flags(const struct netdev *netdev, int *flags)
1332 error = netdev_bsd_do_ioctl(netdev->name, &ifr,
1333 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1335 *flags = 0xFFFF0000 & (ifr.ifr_flagshigh << 16);
1336 *flags |= 0x0000FFFF & ifr.ifr_flags;
1342 set_flags(const char *name, int flags)
1346 ifr.ifr_flags = 0x0000FFFF & flags;
1347 ifr.ifr_flagshigh = (0xFFFF0000 & flags) >> 16;
1349 return netdev_bsd_do_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1353 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1355 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1357 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1358 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1362 netdev->cache_valid |= VALID_IFINDEX;
1363 netdev->ifindex = ifindex;
1365 *ifindexp = netdev->ifindex;
1370 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1372 struct ifaddrs *head;
1373 struct ifaddrs *ifa;
1374 struct sockaddr_dl *sdl;
1376 if (getifaddrs(&head) != 0) {
1377 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1382 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1383 if (ifa->ifa_addr->sa_family == AF_LINK) {
1384 if (!strcmp(ifa->ifa_name, netdev_name)) {
1385 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1387 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1395 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1401 set_etheraddr(const char *netdev_name, int hwaddr_family,
1402 int hwaddr_len, const uint8_t mac[ETH_ADDR_LEN])
1406 memset(&ifr, 0, sizeof ifr);
1407 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1408 ifr.ifr_addr.sa_family = hwaddr_family;
1409 ifr.ifr_addr.sa_len = hwaddr_len;
1410 memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1411 if (ioctl(af_inet_sock, SIOCSIFLLADDR, &ifr) < 0) {
1412 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1413 netdev_name, strerror(errno));
1420 netdev_bsd_do_ioctl(const char *name, struct ifreq *ifr, unsigned long cmd,
1421 const char *cmd_name)
1423 strncpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
1424 if (ioctl(af_inet_sock, cmd, ifr) == -1) {
1425 VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,