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 #ifdef HAVE_NET_IF_MIB_H
36 #include <net/if_mib.h>
41 #include <sys/sysctl.h>
45 #include "dynamic-string.h"
46 #include "fatal-signal.h"
48 #include "openflow/openflow.h"
50 #include "poll-loop.h"
51 #include "socket-util.h"
57 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
60 struct netdev_rx_bsd {
63 /* Packet capture descriptor for a system network device.
64 * For a tap device this is NULL. */
67 /* Selectable file descriptor for the network device.
68 * This descriptor will be used for polling operations. */
72 static const struct netdev_rx_class netdev_rx_bsd_class;
76 unsigned int cache_valid;
77 unsigned int change_seq;
80 uint8_t etheraddr[ETH_ADDR_LEN];
86 int tap_fd; /* TAP character device, if any, otherwise -1. */
88 /* Used for sending packets on non-tap devices. */
97 VALID_IFINDEX = 1 << 0,
98 VALID_ETHERADDR = 1 << 1,
102 VALID_CARRIER = 1 << 5
105 /* An AF_INET socket (used for ioctl operations). */
106 static int af_inet_sock = -1;
108 #if defined(__NetBSD__)
109 /* AF_LINK socket used for netdev_bsd_get_stats and set_etheraddr */
110 static int af_link_sock = -1;
111 #endif /* defined(__NetBSD__) */
113 #define PCAP_SNAPLEN 2048
117 * Notifier used to invalidate device informations in case of status change.
119 * It will be registered with a 'rtbsd_notifier_register()' when the first
120 * device will be created with the call of either 'netdev_bsd_tap_create()' or
121 * 'netdev_bsd_system_create()'.
123 * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
124 * invalidate cached information about the device.
126 static struct rtbsd_notifier netdev_bsd_cache_notifier;
127 static int cache_notifier_refcount;
129 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
131 static int netdev_bsd_do_ioctl(const char *, struct ifreq *, unsigned long cmd,
132 const char *cmd_name);
133 static void destroy_tap(int fd, const char *name);
134 static int get_flags(const struct netdev *, int *flagsp);
135 static int set_flags(const char *, int flags);
136 static int do_set_addr(struct netdev *netdev,
137 int ioctl_nr, const char *ioctl_name,
138 struct in_addr addr);
139 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
140 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
141 int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
142 static int get_ifindex(const struct netdev *, int *ifindexp);
144 static int ifr_get_flags(const struct ifreq *);
145 static void ifr_set_flags(struct ifreq *, int flags);
147 static int netdev_bsd_init(void);
150 is_netdev_bsd_class(const struct netdev_class *netdev_class)
152 return netdev_class->init == netdev_bsd_init;
155 static struct netdev_bsd *
156 netdev_bsd_cast(const struct netdev *netdev)
158 ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev)));
159 return CONTAINER_OF(netdev, struct netdev_bsd, up);
162 static struct netdev_rx_bsd *
163 netdev_rx_bsd_cast(const struct netdev_rx *rx)
165 netdev_rx_assert_class(rx, &netdev_rx_bsd_class);
166 return CONTAINER_OF(rx, struct netdev_rx_bsd, up);
170 netdev_get_kernel_name(const struct netdev *netdev)
172 return netdev_bsd_cast(netdev)->kernel_name;
175 /* Initialize the AF_INET socket used for ioctl operations */
177 netdev_bsd_init(void)
179 static int status = -1;
181 if (status >= 0) { /* already initialized */
185 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
186 status = af_inet_sock >= 0 ? 0 : errno;
188 VLOG_ERR("failed to create inet socket: %s", strerror(status));
192 #if defined(__NetBSD__)
193 af_link_sock = socket(AF_LINK, SOCK_DGRAM, 0);
194 status = af_link_sock >= 0 ? 0 : errno;
196 VLOG_ERR("failed to create link socket: %s", strerror(status));
200 #endif /* defined(__NetBSD__) */
206 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
207 * interface status changes, and eventually calls all the user callbacks.
212 rtbsd_notifier_run();
216 * Arranges for poll_block() to wake up if the "run" member function needs to
220 netdev_bsd_wait(void)
222 rtbsd_notifier_wait();
226 netdev_bsd_changed(struct netdev_bsd *dev)
229 if (!dev->change_seq) {
234 /* Invalidate cache in case of interface status change. */
236 netdev_bsd_cache_cb(const struct rtbsd_change *change,
237 void *aux OVS_UNUSED)
239 struct netdev_bsd *dev;
242 struct netdev *base_dev = netdev_from_name(change->if_name);
245 const struct netdev_class *netdev_class =
246 netdev_get_class(base_dev);
248 if (is_netdev_bsd_class(netdev_class)) {
249 dev = netdev_bsd_cast(base_dev);
250 dev->cache_valid = 0;
251 netdev_bsd_changed(dev);
256 * XXX the API is lacking, we should be able to iterate on the list of
257 * netdevs without having to store the info in a temp shash.
259 struct shash device_shash;
260 struct shash_node *node;
262 shash_init(&device_shash);
263 netdev_get_devices(&netdev_bsd_class, &device_shash);
264 SHASH_FOR_EACH (node, &device_shash) {
266 dev->cache_valid = 0;
267 netdev_bsd_changed(dev);
269 shash_destroy(&device_shash);
274 cache_notifier_ref(void)
278 if (!cache_notifier_refcount) {
279 ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
280 netdev_bsd_cache_cb, NULL);
285 cache_notifier_refcount++;
290 cache_notifier_unref(void)
292 cache_notifier_refcount--;
293 if (cache_notifier_refcount == 0) {
294 rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
299 /* Allocate a netdev_bsd structure */
301 netdev_bsd_create_system(const struct netdev_class *class, const char *name,
302 struct netdev **netdevp)
304 struct netdev_bsd *netdev;
305 enum netdev_flags flags;
308 error = cache_notifier_ref();
313 netdev = xzalloc(sizeof *netdev);
314 netdev->change_seq = 1;
315 netdev_init(&netdev->up, name, class);
317 netdev->kernel_name = xstrdup(name);
319 /* Verify that the netdev really exists by attempting to read its flags */
320 error = netdev_get_flags(&netdev->up, &flags);
321 if (error == ENXIO) {
322 netdev_uninit(&netdev->up, false);
324 cache_notifier_unref();
328 *netdevp = &netdev->up;
333 * Allocate a netdev_bsd structure with 'tap' class.
336 netdev_bsd_create_tap(const struct netdev_class *class, const char *name,
337 struct netdev **netdevp)
339 struct netdev_bsd *netdev = NULL;
342 char *kernel_name = NULL;
344 error = cache_notifier_ref();
349 /* allocate the device structure and set the internal flag */
350 netdev = xzalloc(sizeof *netdev);
352 memset(&ifr, 0, sizeof(ifr));
354 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
355 * to retrieve the name of the tap device. */
356 netdev->tap_fd = open("/dev/tap", O_RDWR);
357 netdev->change_seq = 1;
358 if (netdev->tap_fd < 0) {
360 VLOG_WARN("opening \"/dev/tap\" failed: %s", strerror(error));
361 goto error_undef_notifier;
364 /* Retrieve tap name (e.g. tap0) */
365 if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
366 /* XXX Need to destroy the device? */
368 goto error_undef_notifier;
371 /* Change the name of the tap device */
372 #if defined(SIOCSIFNAME)
373 ifr.ifr_data = (void *)name;
374 if (ioctl(af_inet_sock, SIOCSIFNAME, &ifr) == -1) {
376 destroy_tap(netdev->tap_fd, ifr.ifr_name);
377 goto error_undef_notifier;
379 kernel_name = xstrdup(name);
382 * NetBSD doesn't support inteface renaming.
384 VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name);
385 kernel_name = xstrdup(ifr.ifr_name);
388 /* set non-blocking. */
389 error = set_nonblocking(netdev->tap_fd);
391 destroy_tap(netdev->tap_fd, kernel_name);
392 goto error_undef_notifier;
396 ifr_set_flags(&ifr, IFF_UP);
397 strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
398 if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) {
400 destroy_tap(netdev->tap_fd, kernel_name);
401 goto error_undef_notifier;
404 /* initialize the device structure and
405 * link the structure to its netdev */
406 netdev_init(&netdev->up, name, class);
407 netdev->kernel_name = kernel_name;
408 *netdevp = &netdev->up;
412 error_undef_notifier:
413 cache_notifier_unref();
421 netdev_bsd_destroy(struct netdev *netdev_)
423 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
425 cache_notifier_unref();
427 if (netdev->tap_fd >= 0) {
428 destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_));
431 pcap_close(netdev->pcap);
433 free(netdev->kernel_name);
438 netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
440 char errbuf[PCAP_ERRBUF_SIZE];
446 /* Open the pcap device. The device is opened in non-promiscuous mode
447 * because the interface flags are manually set by the caller. */
449 pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
451 VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
455 if (errbuf[0] != '\0') {
456 VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
459 /* Get the underlying fd. */
460 fd = pcap_get_selectable_fd(pcap);
462 VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
467 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
468 * on the file descriptor returned by pcap_get_selectable_fd to achieve
469 * a real non-blocking behaviour.*/
470 error = pcap_setnonblock(pcap, 1, errbuf);
476 /* This call assure that reads return immediately upon packet
477 * reception. Otherwise, a read will block until either the kernel
478 * buffer becomes full or a timeout occurs. */
479 if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
480 VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
481 name, strerror(errno));
486 /* Capture only incoming packets. */
487 error = pcap_setdirection(pcap, PCAP_D_IN);
507 netdev_bsd_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
509 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
511 struct netdev_rx_bsd *rx;
515 if (!strcmp(netdev_get_type(netdev_), "tap")) {
519 int error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
525 netdev_bsd_changed(netdev);
528 rx = xmalloc(sizeof *rx);
529 netdev_rx_init(&rx->up, netdev_, &netdev_rx_bsd_class);
530 rx->pcap_handle = pcap;
538 netdev_rx_bsd_destroy(struct netdev_rx *rx_)
540 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
542 if (rx->pcap_handle) {
543 pcap_close(rx->pcap_handle);
548 /* The recv callback of the netdev class returns the number of bytes of the
551 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
552 * not make difference between a missing packet on the capture interface and
553 * an error during the file capture. We can use the pcap_dispatch() function
554 * instead, which is able to distinguish between errors and null packet.
556 * To make pcap_dispatch() returns the number of bytes read from the interface
557 * we need to define the following callback and argument.
566 * This callback will be executed on every captured packet.
568 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
569 * pcap returns a truncated packet and we follow this behavior.
571 * The argument args->retval is the packet size in bytes.
574 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
576 struct pcap_arg *args = (struct pcap_arg *)args_;
578 if (args->size < hdr->len) {
579 VLOG_WARN_RL(&rl, "packet truncated");
580 args->retval = args->size;
582 args->retval = hdr->len;
585 /* copy the packet to our buffer */
586 memcpy(args->data, packet, args->retval);
590 * This function attempts to receive a packet from the specified network
591 * device. It is assumed that the network device is a system device or a tap
592 * device opened as a system one. In this case the read operation is performed
596 netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, void *data, size_t size)
601 /* prepare the pcap argument to store the packet */
606 ret = pcap_dispatch(rx->pcap_handle, 1, proc_pkt, (u_char *) &arg);
609 return arg.retval; /* arg.retval < 0 is handled in the caller */
612 if (errno == EINTR) {
622 * This function attempts to receive a packet from the specified network
623 * device. It is assumed that the network device is a tap device and
624 * 'rx->fd' is initialized with the tap file descriptor.
627 netdev_rx_bsd_recv_tap(struct netdev_rx_bsd *rx, void *data, size_t size)
630 ssize_t retval = read(rx->fd, data, size);
633 } else if (errno != EINTR) {
634 if (errno != EAGAIN) {
635 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
636 strerror(errno), netdev_rx_get_name(&rx->up));
645 netdev_rx_bsd_recv(struct netdev_rx *rx_, void *data, size_t size)
647 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
649 return (rx->pcap_handle
650 ? netdev_rx_bsd_recv_pcap(rx, data, size)
651 : netdev_rx_bsd_recv_tap(rx, data, size));
655 * Registers with the poll loop to wake up from the next call to poll_block()
656 * when a packet is ready to be received with netdev_rx_recv() on 'rx'.
659 netdev_rx_bsd_wait(struct netdev_rx *rx_)
661 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
663 poll_fd_wait(rx->fd, POLLIN);
666 /* Discards all packets waiting to be received from 'rx'. */
668 netdev_rx_bsd_drain(struct netdev_rx *rx_)
671 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
673 strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rx_get_netdev(rx_)));
674 if (ioctl(rx->fd, BIOCFLUSH, &ifr) == -1) {
675 VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
676 netdev_rx_get_name(rx_), strerror(errno));
683 * Send a packet on the specified network device. The device could be either a
684 * system or a tap device.
687 netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
689 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
690 const char *name = netdev_get_name(netdev_);
692 if (dev->tap_fd < 0 && !dev->pcap) {
693 int error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
701 if (dev->tap_fd >= 0) {
702 retval = write(dev->tap_fd, data, size);
704 retval = pcap_inject(dev->pcap, data, size);
707 if (errno == EINTR) {
709 } else if (errno != EAGAIN) {
710 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
711 name, strerror(errno));
714 } else if (retval != size) {
715 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
716 "%zu) on %s", retval, size, name);
725 * Registers with the poll loop to wake up from the next call to poll_block()
726 * when the packet transmission queue has sufficient room to transmit a packet
727 * with netdev_send().
730 netdev_bsd_send_wait(struct netdev *netdev_)
732 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
734 if (dev->tap_fd >= 0) {
735 /* TAP device always accepts packets. */
736 poll_immediate_wake();
737 } else if (dev->pcap) {
738 poll_fd_wait(dev->fd, POLLOUT);
740 /* We haven't even tried to send a packet yet. */
741 poll_immediate_wake();
746 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
747 * otherwise a positive errno value.
750 netdev_bsd_set_etheraddr(struct netdev *netdev_,
751 const uint8_t mac[ETH_ADDR_LEN])
753 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
756 if (!(netdev->cache_valid & VALID_ETHERADDR)
757 || !eth_addr_equals(netdev->etheraddr, mac)) {
758 error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
761 netdev->cache_valid |= VALID_ETHERADDR;
762 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
763 netdev_bsd_changed(netdev);
772 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
773 * free the returned buffer.
776 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
777 uint8_t mac[ETH_ADDR_LEN])
779 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
781 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
782 int error = get_etheraddr(netdev_get_kernel_name(netdev_),
787 netdev->cache_valid |= VALID_ETHERADDR;
789 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
795 * Returns the maximum size of transmitted (and received) packets on 'netdev',
796 * in bytes, not including the hardware header; thus, this is typically 1500
797 * bytes for Ethernet devices.
800 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
802 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
804 if (!(netdev->cache_valid & VALID_MTU)) {
808 error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr,
809 SIOCGIFMTU, "SIOCGIFMTU");
813 netdev->mtu = ifr.ifr_mtu;
814 netdev->cache_valid |= VALID_MTU;
822 netdev_bsd_get_ifindex(const struct netdev *netdev)
826 error = get_ifindex(netdev, &ifindex);
827 return error ? -error : ifindex;
831 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
833 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
835 if (!(netdev->cache_valid & VALID_CARRIER)) {
836 struct ifmediareq ifmr;
838 memset(&ifmr, 0, sizeof(ifmr));
839 strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
840 sizeof ifmr.ifm_name);
842 if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
843 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
844 netdev_get_name(netdev_), strerror(errno));
848 netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
849 netdev->cache_valid |= VALID_CARRIER;
851 /* If the interface doesn't report whether the media is active,
852 * just assume it is active. */
853 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
854 netdev->carrier = true;
857 *carrier = netdev->carrier;
862 /* Retrieves current device stats for 'netdev'. */
864 netdev_bsd_get_stats(const struct netdev *netdev_ OVS_UNUSED,
865 struct netdev_stats *stats)
867 #if defined(__FreeBSD__)
871 struct ifmibdata ifmd;
876 mib[2] = NETLINK_GENERIC;
877 mib[3] = IFMIB_SYSTEM;
878 mib[4] = IFMIB_IFCOUNT;
880 len = sizeof(if_count);
882 if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
883 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
884 netdev_get_name(netdev_), strerror(errno));
888 mib[5] = IFDATA_GENERAL;
889 mib[3] = IFMIB_IFDATA;
891 for (i = 1; i <= if_count; i++) {
893 if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
894 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
895 netdev_get_name(netdev_), strerror(errno));
897 } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
898 stats->rx_packets = ifmd.ifmd_data.ifi_ipackets;
899 stats->tx_packets = ifmd.ifmd_data.ifi_opackets;
900 stats->rx_bytes = ifmd.ifmd_data.ifi_ibytes;
901 stats->tx_bytes = ifmd.ifmd_data.ifi_obytes;
902 stats->rx_errors = ifmd.ifmd_data.ifi_ierrors;
903 stats->tx_errors = ifmd.ifmd_data.ifi_oerrors;
904 stats->rx_dropped = ifmd.ifmd_data.ifi_iqdrops;
905 stats->tx_dropped = UINT64_MAX;
906 stats->multicast = ifmd.ifmd_data.ifi_imcasts;
907 stats->collisions = ifmd.ifmd_data.ifi_collisions;
909 stats->rx_length_errors = UINT64_MAX;
910 stats->rx_over_errors = UINT64_MAX;
911 stats->rx_crc_errors = UINT64_MAX;
912 stats->rx_frame_errors = UINT64_MAX;
913 stats->rx_fifo_errors = UINT64_MAX;
914 stats->rx_missed_errors = UINT64_MAX;
916 stats->tx_aborted_errors = UINT64_MAX;
917 stats->tx_carrier_errors = UINT64_MAX;
918 stats->tx_fifo_errors = UINT64_MAX;
919 stats->tx_heartbeat_errors = UINT64_MAX;
920 stats->tx_window_errors = UINT64_MAX;
928 memset(stats, 0, sizeof(*stats));
934 netdev_bsd_parse_media(int media)
936 uint32_t supported = 0;
937 bool half_duplex = media & IFM_HDX ? true : false;
939 switch (IFM_SUBTYPE(media)) {
944 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
945 supported |= NETDEV_F_COPPER;
949 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
950 supported |= NETDEV_F_FIBER;
957 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
958 supported |= NETDEV_F_COPPER;
962 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
963 supported |= NETDEV_F_FIBER;
968 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
969 supported |= NETDEV_F_COPPER;
974 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
975 supported |= NETDEV_F_FIBER;
979 supported |= NETDEV_F_10GB_FD;
980 supported |= NETDEV_F_COPPER;
985 supported |= NETDEV_F_10GB_FD;
986 supported |= NETDEV_F_FIBER;
993 if (IFM_SUBTYPE(media) == IFM_AUTO) {
994 supported |= NETDEV_F_AUTONEG;
997 if (media & IFM_ETH_FMASK) {
998 supported |= NETDEV_F_PAUSE;
1006 * Stores the features supported by 'netdev' into each of '*current',
1007 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1008 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1009 * successful, otherwise a positive errno value. On failure, all of the
1010 * passed-in values are set to 0.
1013 netdev_bsd_get_features(const struct netdev *netdev,
1014 enum netdev_features *current, uint32_t *advertised,
1015 enum netdev_features *supported, uint32_t *peer)
1017 struct ifmediareq ifmr;
1023 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1025 memset(&ifmr, 0, sizeof(ifmr));
1026 strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1028 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
1029 * number of supported modes, and a second with a buffer to retrieve
1031 if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
1032 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1033 netdev_get_name(netdev), strerror(errno));
1037 media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1038 ifmr.ifm_ulist = media_list;
1040 if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
1041 VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1042 netdev_get_name(netdev));
1047 if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
1048 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1049 netdev_get_name(netdev), strerror(errno));
1054 /* Current settings. */
1055 *current = netdev_bsd_parse_media(ifmr.ifm_active);
1057 /* Advertised features. */
1058 *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1060 /* Supported features. */
1062 for (i = 0; i < ifmr.ifm_count; i++) {
1063 *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1066 /* Peer advertisements. */
1067 *peer = 0; /* XXX */
1076 * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
1077 * 'in4' is non-null) and returns true. Otherwise, returns false.
1080 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1081 struct in_addr *netmask)
1083 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1085 if (!(netdev->cache_valid & VALID_IN4)) {
1086 const struct sockaddr_in *sin;
1090 ifr.ifr_addr.sa_family = AF_INET;
1091 error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1092 SIOCGIFADDR, "SIOCGIFADDR");
1097 sin = (struct sockaddr_in *) &ifr.ifr_addr;
1098 netdev->in4 = sin->sin_addr;
1099 netdev->cache_valid |= VALID_IN4;
1100 error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1101 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1105 *netmask = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr;
1109 return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1113 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1114 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1115 * positive errno value.
1118 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1119 struct in_addr mask)
1121 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1124 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1126 netdev->cache_valid |= VALID_IN4;
1128 if (addr.s_addr != INADDR_ANY) {
1129 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1130 "SIOCSIFNETMASK", mask);
1132 netdev_bsd_changed(netdev);
1138 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1140 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1141 if (!(netdev->cache_valid & VALID_IN6)) {
1142 struct ifaddrs *ifa, *head;
1143 struct sockaddr_in6 *sin6;
1144 const char *netdev_name = netdev_get_name(netdev_);
1146 if (getifaddrs(&head) != 0) {
1147 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1152 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1153 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1154 !strcmp(ifa->ifa_name, netdev_name)) {
1155 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1157 memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
1158 netdev->cache_valid |= VALID_IN6;
1165 return EADDRNOTAVAIL;
1172 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1174 struct sockaddr_in sin;
1175 memset(&sin, 0, sizeof sin);
1176 sin.sin_family = AF_INET;
1177 sin.sin_addr = addr;
1180 memset(sa, 0, sizeof *sa);
1181 memcpy(sa, &sin, sizeof sin);
1185 do_set_addr(struct netdev *netdev,
1186 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1189 make_in4_sockaddr(&ifr.ifr_addr, addr);
1190 return netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
1195 nd_to_iff_flags(enum netdev_flags nd)
1198 if (nd & NETDEV_UP) {
1201 if (nd & NETDEV_PROMISC) {
1203 #if defined(IFF_PPROMISC)
1204 iff |= IFF_PPROMISC;
1211 iff_to_nd_flags(int iff)
1213 enum netdev_flags nd = 0;
1217 if (iff & IFF_PROMISC) {
1218 nd |= NETDEV_PROMISC;
1224 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1225 enum netdev_flags on, enum netdev_flags *old_flagsp)
1227 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1228 int old_flags, new_flags;
1231 error = get_flags(netdev_, &old_flags);
1233 *old_flagsp = iff_to_nd_flags(old_flags);
1234 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1235 if (new_flags != old_flags) {
1236 error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
1237 netdev_bsd_changed(netdev);
1244 netdev_bsd_change_seq(const struct netdev *netdev)
1246 return netdev_bsd_cast(netdev)->change_seq;
1250 const struct netdev_class netdev_bsd_class = {
1256 netdev_bsd_create_system,
1258 NULL, /* get_config */
1259 NULL, /* set_config */
1260 NULL, /* get_tunnel_config */
1265 netdev_bsd_send_wait,
1267 netdev_bsd_set_etheraddr,
1268 netdev_bsd_get_etheraddr,
1271 netdev_bsd_get_ifindex,
1272 netdev_bsd_get_carrier,
1273 NULL, /* get_carrier_resets */
1274 NULL, /* set_miimon_interval */
1275 netdev_bsd_get_stats,
1276 NULL, /* set_stats */
1278 netdev_bsd_get_features,
1279 NULL, /* set_advertisement */
1280 NULL, /* set_policing */
1281 NULL, /* get_qos_type */
1282 NULL, /* get_qos_capabilities */
1285 NULL, /* get_queue */
1286 NULL, /* set_queue */
1287 NULL, /* delete_queue */
1288 NULL, /* get_queue_stats */
1289 NULL, /* dump_queue */
1290 NULL, /* dump_queue_stats */
1295 NULL, /* add_router */
1296 NULL, /* get_next_hop */
1297 NULL, /* get_status */
1298 NULL, /* arp_lookup */
1300 netdev_bsd_update_flags,
1302 netdev_bsd_change_seq
1305 const struct netdev_class netdev_tap_class = {
1311 netdev_bsd_create_tap,
1313 NULL, /* get_config */
1314 NULL, /* set_config */
1315 NULL, /* get_tunnel_config */
1320 netdev_bsd_send_wait,
1322 netdev_bsd_set_etheraddr,
1323 netdev_bsd_get_etheraddr,
1326 netdev_bsd_get_ifindex,
1327 netdev_bsd_get_carrier,
1328 NULL, /* get_carrier_resets */
1329 NULL, /* set_miimon_interval */
1330 netdev_bsd_get_stats,
1331 NULL, /* set_stats */
1333 netdev_bsd_get_features,
1334 NULL, /* set_advertisement */
1335 NULL, /* set_policing */
1336 NULL, /* get_qos_type */
1337 NULL, /* get_qos_capabilities */
1340 NULL, /* get_queue */
1341 NULL, /* set_queue */
1342 NULL, /* delete_queue */
1343 NULL, /* get_queue_stats */
1344 NULL, /* dump_queue */
1345 NULL, /* dump_queue_stats */
1350 NULL, /* add_router */
1351 NULL, /* get_next_hop */
1352 NULL, /* get_status */
1353 NULL, /* arp_lookup */
1355 netdev_bsd_update_flags,
1357 netdev_bsd_change_seq
1360 static const struct netdev_rx_class netdev_rx_bsd_class = {
1361 netdev_rx_bsd_destroy,
1364 netdev_rx_bsd_drain,
1369 destroy_tap(int fd, const char *name)
1374 strcpy(ifr.ifr_name, name);
1375 /* XXX What to do if this call fails? */
1376 ioctl(af_inet_sock, SIOCIFDESTROY, &ifr);
1380 get_flags(const struct netdev *netdev, int *flags)
1385 error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev), &ifr,
1386 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1388 *flags = ifr_get_flags(&ifr);
1394 set_flags(const char *name, int flags)
1398 ifr_set_flags(&ifr, flags);
1400 return netdev_bsd_do_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1404 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1406 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1408 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1409 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1413 netdev->cache_valid |= VALID_IFINDEX;
1414 netdev->ifindex = ifindex;
1416 *ifindexp = netdev->ifindex;
1421 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1423 struct ifaddrs *head;
1424 struct ifaddrs *ifa;
1425 struct sockaddr_dl *sdl;
1427 if (getifaddrs(&head) != 0) {
1428 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1433 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1434 if (ifa->ifa_addr->sa_family == AF_LINK) {
1435 if (!strcmp(ifa->ifa_name, netdev_name)) {
1436 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1438 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1446 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1452 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
1453 int hwaddr_len OVS_UNUSED,
1454 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1456 #if defined(__NetBSD__)
1457 return ENOTSUP; /* XXX */
1461 memset(&ifr, 0, sizeof ifr);
1462 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1463 ifr.ifr_addr.sa_family = hwaddr_family;
1464 ifr.ifr_addr.sa_len = hwaddr_len;
1465 memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1466 if (ioctl(af_inet_sock, SIOCSIFLLADDR, &ifr) < 0) {
1467 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1468 netdev_name, strerror(errno));
1476 netdev_bsd_do_ioctl(const char *name, struct ifreq *ifr, unsigned long cmd,
1477 const char *cmd_name)
1479 strncpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
1480 if (ioctl(af_inet_sock, cmd, ifr) == -1) {
1481 VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
1489 ifr_get_flags(const struct ifreq *ifr)
1491 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1492 return (ifr.ifr_flagshigh << 16) | ifr.ifr_flags;
1494 return ifr.ifr_flags;
1499 ifr_set_flags(struct ifreq *ifr, int flags)
1501 ifr->ifr_flags = flags;
1502 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1503 ifr->ifr_flagshigh = flags >> 16;