2 * Copyright (c) 2011, 2013 Gaetano Catalli.
3 * Copyright (c) 2013 YAMAMOTO Takashi.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "netdev-provider.h"
24 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <sys/sockio.h>
30 #include <pcap/pcap.h>
32 #include <net/if_dl.h>
33 #include <net/if_media.h>
34 #include <net/if_tap.h>
35 #include <netinet/in.h>
36 #ifdef HAVE_NET_IF_MIB_H
37 #include <net/if_mib.h>
42 #include <sys/sysctl.h>
43 #if defined(__NetBSD__)
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/if_inarp.h>
50 #include "connectivity.h"
52 #include "dynamic-string.h"
53 #include "fatal-signal.h"
55 #include "openflow/openflow.h"
56 #include "ovs-thread.h"
58 #include "poll-loop.h"
61 #include "socket-util.h"
66 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
69 struct netdev_rx_bsd {
72 /* Packet capture descriptor for a system network device.
73 * For a tap device this is NULL. */
76 /* Selectable file descriptor for the network device.
77 * This descriptor will be used for polling operations. */
84 /* Never changes after initialization. */
87 /* Protects all members below. */
88 struct ovs_mutex mutex;
90 unsigned int cache_valid;
93 uint8_t etheraddr[ETH_ADDR_LEN];
95 struct in_addr netmask;
100 int tap_fd; /* TAP character device, if any, otherwise -1. */
102 /* Used for sending packets on non-tap devices. */
109 VALID_IFINDEX = 1 << 0,
110 VALID_ETHERADDR = 1 << 1,
114 VALID_CARRIER = 1 << 5
117 #define PCAP_SNAPLEN 2048
121 * Notifier used to invalidate device informations in case of status change.
123 * It will be registered with a 'rtbsd_notifier_register()' when the first
124 * device will be created with the call of either 'netdev_bsd_tap_create()' or
125 * 'netdev_bsd_system_create()'.
127 * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
128 * invalidate cached information about the device.
130 static struct rtbsd_notifier netdev_bsd_cache_notifier;
131 static int cache_notifier_refcount;
133 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
135 static void destroy_tap(int fd, const char *name);
136 static int get_flags(const struct netdev *, int *flagsp);
137 static int set_flags(const char *, int flags);
138 static int do_set_addr(struct netdev *netdev,
139 unsigned long ioctl_nr, const char *ioctl_name,
140 struct in_addr addr);
141 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
142 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
143 int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
144 static int get_ifindex(const struct netdev *, int *ifindexp);
146 static int ifr_get_flags(const struct ifreq *);
147 static void ifr_set_flags(struct ifreq *, int flags);
150 static int af_link_ioctl(unsigned long command, const void *arg);
153 static void netdev_bsd_run(void);
156 is_netdev_bsd_class(const struct netdev_class *netdev_class)
158 return netdev_class->run == netdev_bsd_run;
161 static struct netdev_bsd *
162 netdev_bsd_cast(const struct netdev *netdev)
164 ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev)));
165 return CONTAINER_OF(netdev, struct netdev_bsd, up);
168 static struct netdev_rx_bsd *
169 netdev_rx_bsd_cast(const struct netdev_rx *rx)
171 ovs_assert(is_netdev_bsd_class(netdev_get_class(rx->netdev)));
172 return CONTAINER_OF(rx, struct netdev_rx_bsd, up);
176 netdev_get_kernel_name(const struct netdev *netdev)
178 return netdev_bsd_cast(netdev)->kernel_name;
182 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
183 * interface status changes, and eventually calls all the user callbacks.
188 rtbsd_notifier_run();
192 * Arranges for poll_block() to wake up if the "run" member function needs to
196 netdev_bsd_wait(void)
198 rtbsd_notifier_wait();
201 /* Invalidate cache in case of interface status change. */
203 netdev_bsd_cache_cb(const struct rtbsd_change *change,
204 void *aux OVS_UNUSED)
206 struct netdev_bsd *dev;
209 struct netdev *base_dev = netdev_from_name(change->if_name);
212 const struct netdev_class *netdev_class =
213 netdev_get_class(base_dev);
215 if (is_netdev_bsd_class(netdev_class)) {
216 dev = netdev_bsd_cast(base_dev);
217 dev->cache_valid = 0;
218 seq_change(connectivity_seq_get());
220 netdev_close(base_dev);
224 * XXX the API is lacking, we should be able to iterate on the list of
225 * netdevs without having to store the info in a temp shash.
227 struct shash device_shash;
228 struct shash_node *node;
230 shash_init(&device_shash);
231 netdev_get_devices(&netdev_bsd_class, &device_shash);
232 SHASH_FOR_EACH (node, &device_shash) {
233 struct netdev *netdev = node->data;
234 dev = netdev_bsd_cast(netdev);
235 dev->cache_valid = 0;
236 seq_change(connectivity_seq_get());
237 netdev_close(netdev);
239 shash_destroy(&device_shash);
244 cache_notifier_ref(void)
248 if (!cache_notifier_refcount) {
249 ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
250 netdev_bsd_cache_cb, NULL);
255 cache_notifier_refcount++;
260 cache_notifier_unref(void)
262 cache_notifier_refcount--;
263 if (cache_notifier_refcount == 0) {
264 rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
269 static struct netdev *
270 netdev_bsd_alloc(void)
272 struct netdev_bsd *netdev = xzalloc(sizeof *netdev);
277 netdev_bsd_construct_system(struct netdev *netdev_)
279 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
280 enum netdev_flags flags;
283 error = cache_notifier_ref();
288 ovs_mutex_init(&netdev->mutex);
290 netdev->kernel_name = xstrdup(netdev_->name);
292 /* Verify that the netdev really exists by attempting to read its flags */
293 error = netdev_get_flags(netdev_, &flags);
294 if (error == ENXIO) {
295 free(netdev->kernel_name);
296 cache_notifier_unref();
304 netdev_bsd_construct_tap(struct netdev *netdev_)
306 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
307 const char *name = netdev_->name;
310 char *kernel_name = NULL;
312 error = cache_notifier_ref();
317 memset(&ifr, 0, sizeof(ifr));
319 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
320 * to retrieve the name of the tap device. */
321 ovs_mutex_init(&netdev->mutex);
322 netdev->tap_fd = open("/dev/tap", O_RDWR);
323 if (netdev->tap_fd < 0) {
325 VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error));
326 goto error_unref_notifier;
329 /* Retrieve tap name (e.g. tap0) */
330 if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
331 /* XXX Need to destroy the device? */
333 close(netdev->tap_fd);
334 goto error_unref_notifier;
337 /* Change the name of the tap device */
338 #if defined(SIOCSIFNAME)
339 ifr.ifr_data = (void *)name;
340 error = af_inet_ioctl(SIOCSIFNAME, &ifr);
342 destroy_tap(netdev->tap_fd, ifr.ifr_name);
343 goto error_unref_notifier;
345 kernel_name = xstrdup(name);
348 * NetBSD doesn't support inteface renaming.
350 VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name);
351 kernel_name = xstrdup(ifr.ifr_name);
354 /* set non-blocking. */
355 error = set_nonblocking(netdev->tap_fd);
357 destroy_tap(netdev->tap_fd, kernel_name);
358 goto error_unref_notifier;
362 ifr_set_flags(&ifr, IFF_UP);
363 strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
364 error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
366 destroy_tap(netdev->tap_fd, kernel_name);
367 goto error_unref_notifier;
370 netdev->kernel_name = kernel_name;
374 error_unref_notifier:
375 ovs_mutex_destroy(&netdev->mutex);
376 cache_notifier_unref();
383 netdev_bsd_destruct(struct netdev *netdev_)
385 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
387 cache_notifier_unref();
389 if (netdev->tap_fd >= 0) {
390 destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_));
393 pcap_close(netdev->pcap);
395 free(netdev->kernel_name);
396 ovs_mutex_destroy(&netdev->mutex);
400 netdev_bsd_dealloc(struct netdev *netdev_)
402 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
408 netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
410 char errbuf[PCAP_ERRBUF_SIZE];
416 /* Open the pcap device. The device is opened in non-promiscuous mode
417 * because the interface flags are manually set by the caller. */
419 pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
421 VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
425 if (errbuf[0] != '\0') {
426 VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
429 /* Get the underlying fd. */
430 fd = pcap_get_selectable_fd(pcap);
432 VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
437 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
438 * on the file descriptor returned by pcap_get_selectable_fd to achieve
439 * a real non-blocking behaviour.*/
440 error = pcap_setnonblock(pcap, 1, errbuf);
446 /* This call assure that reads return immediately upon packet
447 * reception. Otherwise, a read will block until either the kernel
448 * buffer becomes full or a timeout occurs. */
449 if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
450 VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
451 name, ovs_strerror(errno));
456 /* Capture only incoming packets. */
457 error = pcap_setdirection(pcap, PCAP_D_IN);
476 static struct netdev_rx *
477 netdev_bsd_rx_alloc(void)
479 struct netdev_rx_bsd *rx = xzalloc(sizeof *rx);
484 netdev_bsd_rx_construct(struct netdev_rx *rx_)
486 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
487 struct netdev *netdev_ = rx->up.netdev;
488 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
491 if (!strcmp(netdev_get_type(netdev_), "tap")) {
492 rx->pcap_handle = NULL;
493 rx->fd = netdev->tap_fd;
496 ovs_mutex_lock(&netdev->mutex);
497 error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
498 &rx->pcap_handle, &rx->fd);
499 ovs_mutex_unlock(&netdev->mutex);
506 netdev_bsd_rx_destruct(struct netdev_rx *rx_)
508 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
510 if (rx->pcap_handle) {
511 pcap_close(rx->pcap_handle);
516 netdev_bsd_rx_dealloc(struct netdev_rx *rx_)
518 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
523 /* The recv callback of the netdev class returns the number of bytes of the
526 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
527 * not make difference between a missing packet on the capture interface and
528 * an error during the file capture. We can use the pcap_dispatch() function
529 * instead, which is able to distinguish between errors and null packet.
531 * To make pcap_dispatch() returns the number of bytes read from the interface
532 * we need to define the following callback and argument.
541 * This callback will be executed on every captured packet.
543 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
544 * pcap returns a truncated packet and we follow this behavior.
546 * The argument args->retval is the packet size in bytes.
549 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
551 struct pcap_arg *args = (struct pcap_arg *)args_;
553 if (args->size < hdr->len) {
554 VLOG_WARN_RL(&rl, "packet truncated");
555 args->retval = args->size;
557 args->retval = hdr->len;
560 /* copy the packet to our buffer */
561 memcpy(args->data, packet, args->retval);
565 * This function attempts to receive a packet from the specified network
566 * device. It is assumed that the network device is a system device or a tap
567 * device opened as a system one. In this case the read operation is performed
571 netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, struct ofpbuf *buffer)
576 /* prepare the pcap argument to store the packet */
577 arg.size = ofpbuf_tailroom(buffer);
578 arg.data = buffer->data;
581 ret = pcap_dispatch(rx->pcap_handle, 1, proc_pkt, (u_char *) &arg);
584 buffer->size += arg.retval;
588 if (errno == EINTR) {
598 * This function attempts to receive a packet from the specified network
599 * device. It is assumed that the network device is a tap device and
600 * 'rx->fd' is initialized with the tap file descriptor.
603 netdev_rx_bsd_recv_tap(struct netdev_rx_bsd *rx, struct ofpbuf *buffer)
605 size_t size = ofpbuf_tailroom(buffer);
608 ssize_t retval = read(rx->fd, buffer->data, size);
610 buffer->size += retval;
612 } else if (errno != EINTR) {
613 if (errno != EAGAIN) {
614 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
615 ovs_strerror(errno), netdev_rx_get_name(&rx->up));
623 netdev_bsd_rx_recv(struct netdev_rx *rx_, struct ofpbuf *buffer)
625 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
627 return (rx->pcap_handle
628 ? netdev_rx_bsd_recv_pcap(rx, buffer)
629 : netdev_rx_bsd_recv_tap(rx, buffer));
633 * Registers with the poll loop to wake up from the next call to poll_block()
634 * when a packet is ready to be received with netdev_rx_recv() on 'rx'.
637 netdev_bsd_rx_wait(struct netdev_rx *rx_)
639 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
641 poll_fd_wait(rx->fd, POLLIN);
644 /* Discards all packets waiting to be received from 'rx'. */
646 netdev_bsd_rx_drain(struct netdev_rx *rx_)
649 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
651 strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rx_get_netdev(rx_)));
652 if (ioctl(rx->fd, BIOCFLUSH, &ifr) == -1) {
653 VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
654 netdev_rx_get_name(rx_), ovs_strerror(errno));
661 * Send a packet on the specified network device. The device could be either a
662 * system or a tap device.
665 netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
667 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
668 const char *name = netdev_get_name(netdev_);
671 ovs_mutex_lock(&dev->mutex);
672 if (dev->tap_fd < 0 && !dev->pcap) {
673 error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
680 if (dev->tap_fd >= 0) {
681 retval = write(dev->tap_fd, data, size);
683 retval = pcap_inject(dev->pcap, data, size);
686 if (errno == EINTR) {
690 if (error != EAGAIN) {
691 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: "
692 "%s", name, ovs_strerror(error));
695 } else if (retval != size) {
696 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIuSIZE"d bytes of "
697 "%"PRIuSIZE") on %s", retval, size, name);
704 ovs_mutex_unlock(&dev->mutex);
709 * Registers with the poll loop to wake up from the next call to poll_block()
710 * when the packet transmission queue has sufficient room to transmit a packet
711 * with netdev_send().
714 netdev_bsd_send_wait(struct netdev *netdev_)
716 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
718 ovs_mutex_lock(&dev->mutex);
719 if (dev->tap_fd >= 0) {
720 /* TAP device always accepts packets. */
721 poll_immediate_wake();
722 } else if (dev->pcap) {
723 poll_fd_wait(dev->fd, POLLOUT);
725 /* We haven't even tried to send a packet yet. */
726 poll_immediate_wake();
728 ovs_mutex_unlock(&dev->mutex);
732 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
733 * otherwise a positive errno value.
736 netdev_bsd_set_etheraddr(struct netdev *netdev_,
737 const uint8_t mac[ETH_ADDR_LEN])
739 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
742 ovs_mutex_lock(&netdev->mutex);
743 if (!(netdev->cache_valid & VALID_ETHERADDR)
744 || !eth_addr_equals(netdev->etheraddr, mac)) {
745 error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
748 netdev->cache_valid |= VALID_ETHERADDR;
749 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
750 seq_change(connectivity_seq_get());
753 ovs_mutex_unlock(&netdev->mutex);
759 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
760 * free the returned buffer.
763 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
764 uint8_t mac[ETH_ADDR_LEN])
766 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
769 ovs_mutex_lock(&netdev->mutex);
770 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
771 error = get_etheraddr(netdev_get_kernel_name(netdev_),
774 netdev->cache_valid |= VALID_ETHERADDR;
778 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
780 ovs_mutex_unlock(&netdev->mutex);
786 * Returns the maximum size of transmitted (and received) packets on 'netdev',
787 * in bytes, not including the hardware header; thus, this is typically 1500
788 * bytes for Ethernet devices.
791 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
793 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
796 ovs_mutex_lock(&netdev->mutex);
797 if (!(netdev->cache_valid & VALID_MTU)) {
800 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
801 SIOCGIFMTU, "SIOCGIFMTU");
803 netdev->mtu = ifr.ifr_mtu;
804 netdev->cache_valid |= VALID_MTU;
810 ovs_mutex_unlock(&netdev->mutex);
816 netdev_bsd_get_ifindex(const struct netdev *netdev_)
818 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
821 ovs_mutex_lock(&netdev->mutex);
822 error = get_ifindex(netdev_, &ifindex);
823 ovs_mutex_unlock(&netdev->mutex);
825 return error ? -error : ifindex;
829 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
831 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
834 ovs_mutex_lock(&netdev->mutex);
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 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
844 netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
845 netdev->cache_valid |= VALID_CARRIER;
847 /* If the interface doesn't report whether the media is active,
848 * just assume it is active. */
849 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
850 netdev->carrier = true;
853 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
854 netdev_get_name(netdev_), ovs_strerror(error));
858 *carrier = netdev->carrier;
860 ovs_mutex_unlock(&netdev->mutex);
866 convert_stats(struct netdev_stats *stats, const struct if_data *ifd)
869 * note: UINT64_MAX means unsupported
871 stats->rx_packets = ifd->ifi_ipackets;
872 stats->tx_packets = ifd->ifi_opackets;
873 stats->rx_bytes = ifd->ifi_obytes;
874 stats->tx_bytes = ifd->ifi_ibytes;
875 stats->rx_errors = ifd->ifi_ierrors;
876 stats->tx_errors = ifd->ifi_oerrors;
877 stats->rx_dropped = ifd->ifi_iqdrops;
878 stats->tx_dropped = UINT64_MAX;
879 stats->multicast = ifd->ifi_imcasts;
880 stats->collisions = ifd->ifi_collisions;
881 stats->rx_length_errors = UINT64_MAX;
882 stats->rx_over_errors = UINT64_MAX;
883 stats->rx_crc_errors = UINT64_MAX;
884 stats->rx_frame_errors = UINT64_MAX;
885 stats->rx_fifo_errors = UINT64_MAX;
886 stats->rx_missed_errors = UINT64_MAX;
887 stats->tx_aborted_errors = UINT64_MAX;
888 stats->tx_carrier_errors = UINT64_MAX;
889 stats->tx_fifo_errors = UINT64_MAX;
890 stats->tx_heartbeat_errors = UINT64_MAX;
891 stats->tx_window_errors = UINT64_MAX;
894 /* Retrieves current device stats for 'netdev'. */
896 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
898 #if defined(__FreeBSD__)
902 struct ifmibdata ifmd;
907 mib[2] = NETLINK_GENERIC;
908 mib[3] = IFMIB_SYSTEM;
909 mib[4] = IFMIB_IFCOUNT;
911 len = sizeof(if_count);
913 if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
914 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
915 netdev_get_name(netdev_), ovs_strerror(errno));
919 mib[5] = IFDATA_GENERAL;
920 mib[3] = IFMIB_IFDATA;
922 for (i = 1; i <= if_count; i++) {
924 if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
925 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
926 netdev_get_name(netdev_), ovs_strerror(errno));
928 } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
929 convert_stats(stats, &ifmd.ifmd_data);
935 #elif defined(__NetBSD__)
936 struct ifdatareq ifdr;
939 memset(&ifdr, 0, sizeof(ifdr));
940 strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
941 sizeof(ifdr.ifdr_name));
942 error = af_link_ioctl(SIOCGIFDATA, &ifdr);
944 convert_stats(stats, &ifdr.ifdr_data);
948 #error not implemented
953 netdev_bsd_parse_media(int media)
955 uint32_t supported = 0;
956 bool half_duplex = media & IFM_HDX ? true : false;
958 switch (IFM_SUBTYPE(media)) {
963 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
964 supported |= NETDEV_F_COPPER;
968 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
969 supported |= NETDEV_F_FIBER;
976 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
977 supported |= NETDEV_F_COPPER;
981 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
982 supported |= NETDEV_F_FIBER;
987 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
988 supported |= NETDEV_F_COPPER;
993 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
994 supported |= NETDEV_F_FIBER;
998 supported |= NETDEV_F_10GB_FD;
999 supported |= NETDEV_F_COPPER;
1004 supported |= NETDEV_F_10GB_FD;
1005 supported |= NETDEV_F_FIBER;
1012 if (IFM_SUBTYPE(media) == IFM_AUTO) {
1013 supported |= NETDEV_F_AUTONEG;
1016 if (media & IFM_ETH_FMASK) {
1017 supported |= NETDEV_F_PAUSE;
1025 * Stores the features supported by 'netdev' into each of '*current',
1026 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1027 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1028 * successful, otherwise a positive errno value. On failure, all of the
1029 * passed-in values are set to 0.
1032 netdev_bsd_get_features(const struct netdev *netdev,
1033 enum netdev_features *current, uint32_t *advertised,
1034 enum netdev_features *supported, uint32_t *peer)
1036 struct ifmediareq ifmr;
1042 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1044 memset(&ifmr, 0, sizeof(ifmr));
1045 strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1047 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
1048 * number of supported modes, and a second with a buffer to retrieve
1050 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1052 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1053 netdev_get_name(netdev), ovs_strerror(error));
1057 media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1058 ifmr.ifm_ulist = media_list;
1060 if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
1061 VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1062 netdev_get_name(netdev));
1067 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1069 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1070 netdev_get_name(netdev), ovs_strerror(error));
1074 /* Current settings. */
1075 *current = netdev_bsd_parse_media(ifmr.ifm_active);
1077 /* Advertised features. */
1078 *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1080 /* Supported features. */
1082 for (i = 0; i < ifmr.ifm_count; i++) {
1083 *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1086 /* Peer advertisements. */
1087 *peer = 0; /* XXX */
1096 * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
1097 * '*netmask' to its netmask and returns true. Otherwise, returns false.
1100 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1101 struct in_addr *netmask)
1103 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1106 ovs_mutex_lock(&netdev->mutex);
1107 if (!(netdev->cache_valid & VALID_IN4)) {
1110 ifr.ifr_addr.sa_family = AF_INET;
1111 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1112 SIOCGIFADDR, "SIOCGIFADDR");
1114 const struct sockaddr_in *sin;
1116 sin = (struct sockaddr_in *) &ifr.ifr_addr;
1117 netdev->in4 = sin->sin_addr;
1118 netdev->cache_valid |= VALID_IN4;
1119 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1120 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1122 *netmask = sin->sin_addr;
1128 *netmask = netdev->netmask;
1130 ovs_mutex_unlock(&netdev->mutex);
1132 return error ? error : in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1136 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1137 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1138 * positive errno value.
1141 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1142 struct in_addr mask)
1144 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1147 ovs_mutex_lock(&netdev->mutex);
1148 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1150 if (addr.s_addr != INADDR_ANY) {
1151 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1152 "SIOCSIFNETMASK", mask);
1154 netdev->cache_valid |= VALID_IN4;
1156 netdev->netmask = mask;
1159 seq_change(connectivity_seq_get());
1161 ovs_mutex_unlock(&netdev->mutex);
1167 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1169 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1170 if (!(netdev->cache_valid & VALID_IN6)) {
1171 struct ifaddrs *ifa, *head;
1172 struct sockaddr_in6 *sin6;
1173 const char *netdev_name = netdev_get_name(netdev_);
1175 if (getifaddrs(&head) != 0) {
1176 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1177 ovs_strerror(errno));
1181 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1182 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1183 !strcmp(ifa->ifa_name, netdev_name)) {
1184 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1186 memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
1187 netdev->cache_valid |= VALID_IN6;
1194 return EADDRNOTAVAIL;
1200 #if defined(__NetBSD__)
1202 netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name)
1204 char *ovs_name = NULL;
1205 struct shash device_shash;
1206 struct shash_node *node;
1208 shash_init(&device_shash);
1209 netdev_get_devices(&netdev_tap_class, &device_shash);
1210 SHASH_FOR_EACH(node, &device_shash) {
1211 struct netdev *netdev = node->data;
1212 struct netdev_bsd * const dev = netdev_bsd_cast(netdev);
1214 if (!strcmp(dev->kernel_name, kernel_name)) {
1216 ovs_name = xstrdup(netdev_get_name(&dev->up));
1218 netdev_close(netdev);
1220 shash_destroy(&device_shash);
1222 return ovs_name ? ovs_name : xstrdup(kernel_name);
1227 netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
1228 struct in_addr *next_hop OVS_UNUSED,
1229 char **netdev_name OVS_UNUSED)
1231 #if defined(__NetBSD__)
1233 struct sockaddr_in sin;
1234 struct sockaddr_dl sdl;
1241 struct rt_msghdr *rtm = &buf.h;
1242 const pid_t pid = getpid();
1245 bool gateway = false;
1246 char *ifname = NULL;
1249 memset(next_hop, 0, sizeof(*next_hop));
1250 *netdev_name = NULL;
1252 memset(&sin, 0, sizeof(sin));
1253 sin.sin_len = sizeof(sin);
1254 sin.sin_family = AF_INET;
1256 sin.sin_addr = *host;
1258 memset(&sdl, 0, sizeof(sdl));
1259 sdl.sdl_len = sizeof(sdl);
1260 sdl.sdl_family = AF_LINK;
1262 s = socket(PF_ROUTE, SOCK_RAW, 0);
1263 memset(&buf, 0, sizeof(buf));
1264 rtm->rtm_flags = RTF_HOST|RTF_UP;
1265 rtm->rtm_version = RTM_VERSION;
1266 rtm->rtm_addrs = RTA_DST|RTA_IFP;
1267 cp = (void *)&buf.space;
1268 memcpy(cp, &sin, sizeof(sin));
1269 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin);
1270 memcpy(cp, &sdl, sizeof(sdl));
1271 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl);
1272 rtm->rtm_msglen = cp - (char *)(void *)rtm;
1273 rtm->rtm_seq = ++seq;
1274 rtm->rtm_type = RTM_GET;
1276 write(s, rtm, rtm->rtm_msglen);
1277 memset(&buf, 0, sizeof(buf));
1279 ssz = read(s, &buf, sizeof(buf));
1280 } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
1281 saved_errno = errno;
1287 return EPIPE; /* XXX */
1289 cp = (void *)&buf.space;
1290 for (i = 1; i; i <<= 1) {
1291 if ((rtm->rtm_addrs & i) != 0) {
1292 const struct sockaddr *sa = (const void *)cp;
1294 if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) {
1295 const struct sockaddr_in * const sin =
1296 (const struct sockaddr_in *)sa;
1298 *next_hop = sin->sin_addr;
1301 if ((i == RTA_IFP) && sa->sa_family == AF_LINK) {
1302 const struct sockaddr_dl * const sdl =
1303 (const struct sockaddr_dl *)sa;
1306 kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen);
1307 ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name);
1313 if (ifname == NULL) {
1319 *netdev_name = ifname;
1320 VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s",
1321 IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name);
1329 netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED,
1330 ovs_be32 ip OVS_UNUSED,
1331 uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1333 #if defined(__NetBSD__)
1334 const struct rt_msghdr *rtm;
1347 mib[4] = NET_RT_FLAGS;
1348 mib[5] = RTF_LLINFO;
1349 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
1353 buf = xmalloc(needed);
1354 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
1359 for (cp = buf; cp < ep; cp += rtm->rtm_msglen) {
1360 const struct sockaddr_inarp *sina;
1361 const struct sockaddr_dl *sdl;
1363 rtm = (const void *)cp;
1364 sina = (const void *)(rtm + 1);
1365 if (ip != sina->sin_addr.s_addr) {
1368 sdl = (const void *)
1369 ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len));
1370 if (sdl->sdl_alen == ETH_ADDR_LEN) {
1371 memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN);
1386 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1388 struct sockaddr_in sin;
1389 memset(&sin, 0, sizeof sin);
1390 sin.sin_family = AF_INET;
1391 sin.sin_addr = addr;
1394 memset(sa, 0, sizeof *sa);
1395 memcpy(sa, &sin, sizeof sin);
1399 do_set_addr(struct netdev *netdev,
1400 unsigned long ioctl_nr, const char *ioctl_name,
1401 struct in_addr addr)
1404 make_in4_sockaddr(&ifr.ifr_addr, addr);
1405 return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
1410 nd_to_iff_flags(enum netdev_flags nd)
1413 if (nd & NETDEV_UP) {
1416 if (nd & NETDEV_PROMISC) {
1418 #if defined(IFF_PPROMISC)
1419 iff |= IFF_PPROMISC;
1422 if (nd & NETDEV_LOOPBACK) {
1423 iff |= IFF_LOOPBACK;
1429 iff_to_nd_flags(int iff)
1431 enum netdev_flags nd = 0;
1435 if (iff & IFF_PROMISC) {
1436 nd |= NETDEV_PROMISC;
1438 if (iff & IFF_LOOPBACK) {
1439 nd |= NETDEV_LOOPBACK;
1445 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1446 enum netdev_flags on, enum netdev_flags *old_flagsp)
1448 int old_flags, new_flags;
1451 error = get_flags(netdev_, &old_flags);
1453 *old_flagsp = iff_to_nd_flags(old_flags);
1454 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1455 if (new_flags != old_flags) {
1456 error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
1457 seq_change(connectivity_seq_get());
1463 /* Linux has also different GET_STATS, SET_STATS,
1466 #define NETDEV_BSD_CLASS(NAME, CONSTRUCT, \
1476 netdev_bsd_destruct, \
1477 netdev_bsd_dealloc, \
1478 NULL, /* get_config */ \
1479 NULL, /* set_config */ \
1480 NULL, /* get_tunnel_config */ \
1483 netdev_bsd_send_wait, \
1485 netdev_bsd_set_etheraddr, \
1486 netdev_bsd_get_etheraddr, \
1487 netdev_bsd_get_mtu, \
1488 NULL, /* set_mtu */ \
1489 netdev_bsd_get_ifindex, \
1490 netdev_bsd_get_carrier, \
1491 NULL, /* get_carrier_resets */ \
1492 NULL, /* set_miimon_interval */ \
1493 netdev_bsd_get_stats, \
1494 NULL, /* set_stats */ \
1497 NULL, /* set_advertisement */ \
1498 NULL, /* set_policing */ \
1499 NULL, /* get_qos_type */ \
1500 NULL, /* get_qos_capabilities */ \
1501 NULL, /* get_qos */ \
1502 NULL, /* set_qos */ \
1503 NULL, /* get_queue */ \
1504 NULL, /* set_queue */ \
1505 NULL, /* delete_queue */ \
1506 NULL, /* get_queue_stats */ \
1507 NULL, /* queue_dump_start */ \
1508 NULL, /* queue_dump_next */ \
1509 NULL, /* queue_dump_done */ \
1510 NULL, /* dump_queue_stats */ \
1512 netdev_bsd_get_in4, \
1513 netdev_bsd_set_in4, \
1514 netdev_bsd_get_in6, \
1515 NULL, /* add_router */ \
1516 netdev_bsd_get_next_hop, \
1517 NULL, /* get_status */ \
1518 netdev_bsd_arp_lookup, /* arp_lookup */ \
1520 netdev_bsd_update_flags, \
1522 netdev_bsd_rx_alloc, \
1523 netdev_bsd_rx_construct, \
1524 netdev_bsd_rx_destruct, \
1525 netdev_bsd_rx_dealloc, \
1526 netdev_bsd_rx_recv, \
1527 netdev_bsd_rx_wait, \
1528 netdev_bsd_rx_drain, \
1531 const struct netdev_class netdev_bsd_class =
1534 netdev_bsd_construct_system,
1535 netdev_bsd_get_features);
1537 const struct netdev_class netdev_tap_class =
1540 netdev_bsd_construct_tap,
1541 netdev_bsd_get_features);
1545 destroy_tap(int fd, const char *name)
1550 strcpy(ifr.ifr_name, name);
1551 /* XXX What to do if this call fails? */
1552 af_inet_ioctl(SIOCIFDESTROY, &ifr);
1556 get_flags(const struct netdev *netdev, int *flags)
1561 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr,
1562 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1564 *flags = ifr_get_flags(&ifr);
1570 set_flags(const char *name, int flags)
1574 ifr_set_flags(&ifr, flags);
1576 return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1580 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1582 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1584 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1585 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1589 netdev->cache_valid |= VALID_IFINDEX;
1590 netdev->ifindex = ifindex;
1592 *ifindexp = netdev->ifindex;
1597 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1599 struct ifaddrs *head;
1600 struct ifaddrs *ifa;
1601 struct sockaddr_dl *sdl;
1603 if (getifaddrs(&head) != 0) {
1604 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1605 ovs_strerror(errno));
1609 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1610 if (ifa->ifa_addr->sa_family == AF_LINK) {
1611 if (!strcmp(ifa->ifa_name, netdev_name)) {
1612 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1614 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1622 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1628 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
1629 int hwaddr_len OVS_UNUSED,
1630 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1632 #if defined(__FreeBSD__)
1636 memset(&ifr, 0, sizeof ifr);
1637 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1638 ifr.ifr_addr.sa_family = hwaddr_family;
1639 ifr.ifr_addr.sa_len = hwaddr_len;
1640 memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1641 error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
1643 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1644 netdev_name, ovs_strerror(error));
1648 #elif defined(__NetBSD__)
1649 struct if_laddrreq req;
1650 struct sockaddr_dl *sdl;
1651 struct sockaddr_storage oldaddr;
1655 * get the old address, add new one, and then remove old one.
1658 if (hwaddr_len != ETH_ADDR_LEN) {
1659 /* just to be safe about sockaddr storage size */
1662 memset(&req, 0, sizeof(req));
1663 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1664 req.addr.ss_len = sizeof(req.addr);
1665 req.addr.ss_family = hwaddr_family;
1666 sdl = (struct sockaddr_dl *)&req.addr;
1667 sdl->sdl_alen = hwaddr_len;
1669 error = af_link_ioctl(SIOCGLIFADDR, &req);
1673 if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], mac, hwaddr_len)) {
1678 memset(&req, 0, sizeof(req));
1679 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1680 req.flags = IFLR_ACTIVE;
1681 sdl = (struct sockaddr_dl *)&req.addr;
1682 sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
1683 sdl->sdl_alen = hwaddr_len;
1684 sdl->sdl_family = hwaddr_family;
1685 memcpy(sdl->sdl_data, mac, hwaddr_len);
1686 error = af_link_ioctl(SIOCALIFADDR, &req);
1691 memset(&req, 0, sizeof(req));
1692 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1694 return af_link_ioctl(SIOCDLIFADDR, &req);
1696 #error not implemented
1701 ifr_get_flags(const struct ifreq *ifr)
1703 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1704 return (ifr->ifr_flagshigh << 16) | ifr->ifr_flags;
1706 return ifr->ifr_flags;
1711 ifr_set_flags(struct ifreq *ifr, int flags)
1713 ifr->ifr_flags = flags;
1714 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1715 ifr->ifr_flagshigh = flags >> 16;
1719 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
1720 * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
1722 af_link_ioctl(unsigned long command, const void *arg)
1724 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1727 if (ovsthread_once_start(&once)) {
1728 sock = socket(AF_LINK, SOCK_DGRAM, 0);
1731 VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno));
1733 ovsthread_once_done(&once);
1736 return (sock < 0 ? -sock
1737 : ioctl(sock, command, arg) == -1 ? errno