X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fnetdev-bsd.c;h=ef1e649761087e25a7f50ac60185287ee1954749;hb=HEAD;hp=9eb502e6b13c1fbecc5beff093162b7b7f586c8a;hpb=796223f5bc3a4896e6398733c798390158479400;p=sliver-openvswitch.git diff --git a/lib/netdev-bsd.c b/lib/netdev-bsd.c index 9eb502e6b..ef1e64976 100644 --- a/lib/netdev-bsd.c +++ b/lib/netdev-bsd.c @@ -1,5 +1,6 @@ /* - * Copyright (c) 2011 Gaetano Catalli. + * Copyright (c) 2011, 2013 Gaetano Catalli. + * Copyright (c) 2013, 2014 YAMAMOTO Takashi. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,22 +33,31 @@ #include #include #include +#ifdef HAVE_NET_IF_MIB_H #include +#endif #include #include #include #include +#if defined(__NetBSD__) +#include +#include +#include +#endif #include "rtbsd.h" #include "coverage.h" +#include "dpif-netdev.h" #include "dynamic-string.h" #include "fatal-signal.h" #include "ofpbuf.h" #include "openflow/openflow.h" +#include "ovs-thread.h" #include "packets.h" #include "poll-loop.h" -#include "socket-util.h" #include "shash.h" +#include "socket-util.h" #include "svec.h" #include "util.h" #include "vlog.h" @@ -55,35 +65,8 @@ VLOG_DEFINE_THIS_MODULE(netdev_bsd); -/* - * This file implements objects to access interfaces. - * Externally, interfaces are represented by three structures: - * + struct netdev_dev, representing a network device, - * containing e.g. name and a refcount; - * We can have private variables by embedding the - * struct netdev_dev into our own structure - * (e.g. netdev_dev_bsd) - * - * + struct netdev, representing an instance of an open netdev_dev. - * The structure contains a pointer to the 'struct netdev' - * representing the device. - * - * + struct netdev_rx, which represents a netdev open to capture received - * packets. Again, private information such as file descriptor etc. are - * stored in our own struct netdev_rx_bsd which includes a struct - * netdev_rx. - * - * 'struct netdev', 'struct netdev_dev', and 'struct netdev_rx' are referenced - * in containers which hold pointers to the data structures. We can reach our - * own struct netdev_XXX_bsd by putting a struct netdev_XXX within our own - * struct, and using CONTAINER_OF to access the parent structure. - */ -struct netdev_bsd { - struct netdev netdev; -}; - -struct netdev_rx_bsd { - struct netdev_rx up; +struct netdev_rxq_bsd { + struct netdev_rxq up; /* Packet capture descriptor for a system network device. * For a tap device this is NULL. */ @@ -94,16 +77,21 @@ struct netdev_rx_bsd { int fd; }; -static const struct netdev_rx_class netdev_rx_bsd_class; +struct netdev_bsd { + struct netdev up; + + /* Never changes after initialization. */ + char *kernel_name; + + /* Protects all members below. */ + struct ovs_mutex mutex; -struct netdev_dev_bsd { - struct netdev_dev netdev_dev; unsigned int cache_valid; - unsigned int change_seq; int ifindex; uint8_t etheraddr[ETH_ADDR_LEN]; struct in_addr in4; + struct in_addr netmask; struct in6_addr in6; int mtu; int carrier; @@ -125,9 +113,6 @@ enum { VALID_CARRIER = 1 << 5 }; -/* An AF_INET socket (used for ioctl operations). */ -static int af_inet_sock = -1; - #define PCAP_SNAPLEN 2048 @@ -146,67 +131,51 @@ static int cache_notifier_refcount; static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); -static int netdev_bsd_do_ioctl(const char *, struct ifreq *, unsigned long cmd, - const char *cmd_name); static void destroy_tap(int fd, const char *name); -static int get_flags(const struct netdev_dev *, int *flagsp); +static int get_flags(const struct netdev *, int *flagsp); static int set_flags(const char *, int flags); static int do_set_addr(struct netdev *netdev, - int ioctl_nr, const char *ioctl_name, + unsigned long ioctl_nr, const char *ioctl_name, struct in_addr addr); static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]); static int set_etheraddr(const char *netdev_name, int hwaddr_family, int hwaddr_len, const uint8_t[ETH_ADDR_LEN]); static int get_ifindex(const struct netdev *, int *ifindexp); -static int netdev_bsd_init(void); +static int ifr_get_flags(const struct ifreq *); +static void ifr_set_flags(struct ifreq *, int flags); + +#ifdef __NetBSD__ +static int af_link_ioctl(unsigned long command, const void *arg); +#endif + +static void netdev_bsd_run(void); +static int netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup); static bool is_netdev_bsd_class(const struct netdev_class *netdev_class) { - return netdev_class->init == netdev_bsd_init; + return netdev_class->run == netdev_bsd_run; } static struct netdev_bsd * netdev_bsd_cast(const struct netdev *netdev) { - ovs_assert(is_netdev_bsd_class(netdev_dev_get_class( - netdev_get_dev(netdev)))); - return CONTAINER_OF(netdev, struct netdev_bsd, netdev); + ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev))); + return CONTAINER_OF(netdev, struct netdev_bsd, up); } -static struct netdev_dev_bsd * -netdev_dev_bsd_cast(const struct netdev_dev *netdev_dev) +static struct netdev_rxq_bsd * +netdev_rxq_bsd_cast(const struct netdev_rxq *rxq) { - ovs_assert(is_netdev_bsd_class(netdev_dev_get_class(netdev_dev))); - return CONTAINER_OF(netdev_dev, struct netdev_dev_bsd, netdev_dev); + ovs_assert(is_netdev_bsd_class(netdev_get_class(rxq->netdev))); + return CONTAINER_OF(rxq, struct netdev_rxq_bsd, up); } -static struct netdev_rx_bsd * -netdev_rx_bsd_cast(const struct netdev_rx *rx) +static const char * +netdev_get_kernel_name(const struct netdev *netdev) { - netdev_rx_assert_class(rx, &netdev_rx_bsd_class); - return CONTAINER_OF(rx, struct netdev_rx_bsd, up); -} - -/* Initialize the AF_INET socket used for ioctl operations */ -static int -netdev_bsd_init(void) -{ - static int status = -1; - - if (status >= 0) { /* already initialized */ - return status; - } - - af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0); - status = af_inet_sock >= 0 ? 0 : errno; - - if (status) { - VLOG_ERR("failed to create inet socket: %s", strerror(status)); - } - - return status; + return netdev_bsd_cast(netdev)->kernel_name; } /* @@ -229,34 +198,26 @@ netdev_bsd_wait(void) rtbsd_notifier_wait(); } -static void -netdev_dev_bsd_changed(struct netdev_dev_bsd *dev) -{ - dev->change_seq++; - if (!dev->change_seq) { - dev->change_seq++; - } -} - /* Invalidate cache in case of interface status change. */ static void netdev_bsd_cache_cb(const struct rtbsd_change *change, void *aux OVS_UNUSED) { - struct netdev_dev_bsd *dev; + struct netdev_bsd *dev; if (change) { - struct netdev_dev *base_dev = netdev_dev_from_name(change->if_name); + struct netdev *base_dev = netdev_from_name(change->if_name); if (base_dev) { const struct netdev_class *netdev_class = - netdev_dev_get_class(base_dev); + netdev_get_class(base_dev); if (is_netdev_bsd_class(netdev_class)) { - dev = netdev_dev_bsd_cast(base_dev); + dev = netdev_bsd_cast(base_dev); dev->cache_valid = 0; - netdev_dev_bsd_changed(dev); + netdev_change_seq_changed(base_dev); } + netdev_close(base_dev); } } else { /* @@ -267,11 +228,13 @@ netdev_bsd_cache_cb(const struct rtbsd_change *change, struct shash_node *node; shash_init(&device_shash); - netdev_dev_get_devices(&netdev_bsd_class, &device_shash); + netdev_get_devices(&netdev_bsd_class, &device_shash); SHASH_FOR_EACH (node, &device_shash) { - dev = node->data; + struct netdev *netdev = node->data; + dev = netdev_bsd_cast(netdev); dev->cache_valid = 0; - netdev_dev_bsd_changed(dev); + netdev_change_seq_changed(netdev); + netdev_close(netdev); } shash_destroy(&device_shash); } @@ -303,12 +266,18 @@ cache_notifier_unref(void) return 0; } -/* Allocate a netdev_dev_bsd structure */ +static struct netdev * +netdev_bsd_alloc(void) +{ + struct netdev_bsd *netdev = xzalloc(sizeof *netdev); + return &netdev->up; +} + static int -netdev_bsd_create_system(const struct netdev_class *class, const char *name, - struct netdev_dev **netdev_devp) +netdev_bsd_construct_system(struct netdev *netdev_) { - struct netdev_dev_bsd *netdev_dev; + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); + enum netdev_flags flags; int error; error = cache_notifier_ref(); @@ -316,139 +285,119 @@ netdev_bsd_create_system(const struct netdev_class *class, const char *name, return error; } - netdev_dev = xzalloc(sizeof *netdev_dev); - netdev_dev->change_seq = 1; - netdev_dev_init(&netdev_dev->netdev_dev, name, class); - netdev_dev->tap_fd = -1; - *netdev_devp = &netdev_dev->netdev_dev; + ovs_mutex_init(&netdev->mutex); + netdev->tap_fd = -1; + netdev->kernel_name = xstrdup(netdev_->name); + + /* Verify that the netdev really exists by attempting to read its flags */ + error = netdev_get_flags(netdev_, &flags); + if (error == ENXIO) { + free(netdev->kernel_name); + cache_notifier_unref(); + return error; + } return 0; } -/* - * Allocate a netdev_dev_bsd structure with 'tap' class. - */ static int -netdev_bsd_create_tap(const struct netdev_class *class, const char *name, - struct netdev_dev **netdev_devp) +netdev_bsd_construct_tap(struct netdev *netdev_) { - struct netdev_dev_bsd *netdev_dev = NULL; + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); + const char *name = netdev_->name; int error = 0; struct ifreq ifr; + char *kernel_name = NULL; error = cache_notifier_ref(); if (error) { goto error; } - /* allocate the device structure and set the internal flag */ - netdev_dev = xzalloc(sizeof *netdev_dev); - memset(&ifr, 0, sizeof(ifr)); /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used * to retrieve the name of the tap device. */ - netdev_dev->tap_fd = open("/dev/tap", O_RDWR); - netdev_dev->change_seq = 1; - if (netdev_dev->tap_fd < 0) { + ovs_mutex_init(&netdev->mutex); + netdev->tap_fd = open("/dev/tap", O_RDWR); + if (netdev->tap_fd < 0) { error = errno; - VLOG_WARN("opening \"/dev/tap\" failed: %s", strerror(error)); - goto error_undef_notifier; + VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error)); + goto error_unref_notifier; } /* Retrieve tap name (e.g. tap0) */ - if (ioctl(netdev_dev->tap_fd, TAPGIFNAME, &ifr) == -1) { + if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) { /* XXX Need to destroy the device? */ error = errno; - goto error_undef_notifier; + close(netdev->tap_fd); + goto error_unref_notifier; } /* Change the name of the tap device */ +#if defined(SIOCSIFNAME) ifr.ifr_data = (void *)name; - if (ioctl(af_inet_sock, SIOCSIFNAME, &ifr) == -1) { - error = errno; - destroy_tap(netdev_dev->tap_fd, ifr.ifr_name); - goto error_undef_notifier; + error = af_inet_ioctl(SIOCSIFNAME, &ifr); + if (error) { + destroy_tap(netdev->tap_fd, ifr.ifr_name); + goto error_unref_notifier; } + kernel_name = xstrdup(name); +#else + /* + * NetBSD doesn't support inteface renaming. + */ + VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name); + kernel_name = xstrdup(ifr.ifr_name); +#endif /* set non-blocking. */ - error = set_nonblocking(netdev_dev->tap_fd); + error = set_nonblocking(netdev->tap_fd); if (error) { - destroy_tap(netdev_dev->tap_fd, name); - goto error_undef_notifier; + destroy_tap(netdev->tap_fd, kernel_name); + goto error_unref_notifier; } /* Turn device UP */ - ifr.ifr_flags = (uint16_t)IFF_UP; - ifr.ifr_flagshigh = 0; - strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); - if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) { - error = errno; - destroy_tap(netdev_dev->tap_fd, name); - goto error_undef_notifier; + ifr_set_flags(&ifr, IFF_UP); + strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name); + error = af_inet_ioctl(SIOCSIFFLAGS, &ifr); + if (error) { + destroy_tap(netdev->tap_fd, kernel_name); + goto error_unref_notifier; } - /* initialize the device structure and - * link the structure to its netdev */ - netdev_dev_init(&netdev_dev->netdev_dev, name, class); - *netdev_devp = &netdev_dev->netdev_dev; + netdev->kernel_name = kernel_name; return 0; -error_undef_notifier: +error_unref_notifier: + ovs_mutex_destroy(&netdev->mutex); cache_notifier_unref(); error: - free(netdev_dev); + free(kernel_name); return error; } static void -netdev_bsd_destroy(struct netdev_dev *netdev_dev_) +netdev_bsd_destruct(struct netdev *netdev_) { - struct netdev_dev_bsd *netdev_dev = netdev_dev_bsd_cast(netdev_dev_); + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); cache_notifier_unref(); - if (netdev_dev->tap_fd >= 0) { - destroy_tap(netdev_dev->tap_fd, netdev_dev_get_name(netdev_dev_)); + if (netdev->tap_fd >= 0) { + destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_)); } - if (netdev_dev->pcap) { - pcap_close(netdev_dev->pcap); + if (netdev->pcap) { + pcap_close(netdev->pcap); } - free(netdev_dev); -} - - -static int -netdev_bsd_open_system(struct netdev_dev *netdev_dev_, struct netdev **netdevp) -{ - struct netdev_bsd *netdev; - int error; - enum netdev_flags flags; - - /* Allocate network device. */ - netdev = xcalloc(1, sizeof *netdev); - netdev_init(&netdev->netdev, netdev_dev_); - - /* Verify that the netdev really exists by attempting to read its flags */ - error = netdev_get_flags(&netdev->netdev, &flags); - if (error == ENXIO) { - goto error; - } - - *netdevp = &netdev->netdev; - return 0; - -error: - netdev_uninit(&netdev->netdev, true); - return error; + free(netdev->kernel_name); + ovs_mutex_destroy(&netdev->mutex); } - - -/* Close a 'netdev'. */ static void -netdev_bsd_close(struct netdev *netdev_) +netdev_bsd_dealloc(struct netdev *netdev_) { struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); @@ -499,7 +448,7 @@ netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp) * buffer becomes full or a timeout occurs. */ if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) { VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s", - name, strerror(errno)); + name, ovs_strerror(errno)); error = errno; goto error; } @@ -524,46 +473,51 @@ error: return error; } -static int -netdev_bsd_rx_open(struct netdev *netdev_, struct netdev_rx **rxp) +static struct netdev_rxq * +netdev_bsd_rxq_alloc(void) { - struct netdev_dev_bsd *netdev_dev = - netdev_dev_bsd_cast(netdev_get_dev(netdev_)); + struct netdev_rxq_bsd *rxq = xzalloc(sizeof *rxq); + return &rxq->up; +} - struct netdev_rx_bsd *rx; - pcap_t *pcap; - int fd; +static int +netdev_bsd_rxq_construct(struct netdev_rxq *rxq_) +{ + struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); + struct netdev *netdev_ = rxq->up.netdev; + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); + int error; if (!strcmp(netdev_get_type(netdev_), "tap")) { - pcap = NULL; - fd = netdev_dev->tap_fd; + rxq->pcap_handle = NULL; + rxq->fd = netdev->tap_fd; + error = 0; } else { - int error = netdev_bsd_open_pcap(netdev_get_name(netdev_), &pcap, &fd); - if (error) { - return error; - } - - netdev_dev_bsd_changed(netdev_dev); + ovs_mutex_lock(&netdev->mutex); + error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_), + &rxq->pcap_handle, &rxq->fd); + ovs_mutex_unlock(&netdev->mutex); } - rx = xmalloc(sizeof *rx); - netdev_rx_init(&rx->up, netdev_get_dev(netdev_), &netdev_rx_bsd_class); - rx->pcap_handle = pcap; - rx->fd = fd; - - *rxp = &rx->up; - return 0; + return error; } static void -netdev_rx_bsd_destroy(struct netdev_rx *rx_) +netdev_bsd_rxq_destruct(struct netdev_rxq *rxq_) { - struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_); + struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); - if (rx->pcap_handle) { - pcap_close(rx->pcap_handle); + if (rxq->pcap_handle) { + pcap_close(rxq->pcap_handle); } - free(rx); +} + +static void +netdev_bsd_rxq_dealloc(struct netdev_rxq *rxq_) +{ + struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); + + free(rxq); } /* The recv callback of the netdev class returns the number of bytes of the @@ -611,23 +565,24 @@ proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet) * This function attempts to receive a packet from the specified network * device. It is assumed that the network device is a system device or a tap * device opened as a system one. In this case the read operation is performed - * from rx->pcap. + * from rxq->pcap. */ static int -netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, void *data, size_t size) +netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer) { struct pcap_arg arg; int ret; /* prepare the pcap argument to store the packet */ - arg.size = size; - arg.data = data; + arg.size = ofpbuf_tailroom(buffer); + arg.data = ofpbuf_data(buffer); for (;;) { - ret = pcap_dispatch(rx->pcap_handle, 1, proc_pkt, (u_char *) &arg); + ret = pcap_dispatch(rxq->pcap_handle, 1, proc_pkt, (u_char *) &arg); if (ret > 0) { - return arg.retval; /* arg.retval < 0 is handled in the caller */ + ofpbuf_set_size(buffer, ofpbuf_size(buffer) + arg.retval); + return 0; } if (ret == -1) { if (errno == EINTR) { @@ -635,66 +590,87 @@ netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, void *data, size_t size) } } - return -EAGAIN; + return EAGAIN; } } /* * This function attempts to receive a packet from the specified network * device. It is assumed that the network device is a tap device and - * 'rx->fd' is initialized with the tap file descriptor. + * 'rxq->fd' is initialized with the tap file descriptor. */ static int -netdev_rx_bsd_recv_tap(struct netdev_rx_bsd *rx, void *data, size_t size) +netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer) { + size_t size = ofpbuf_tailroom(buffer); + for (;;) { - ssize_t retval = read(rx->fd, data, size); + ssize_t retval = read(rxq->fd, ofpbuf_data(buffer), size); if (retval >= 0) { - return retval; + ofpbuf_set_size(buffer, ofpbuf_size(buffer) + retval); + return 0; } else if (errno != EINTR) { if (errno != EAGAIN) { VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s", - strerror(errno), netdev_rx_get_name(&rx->up)); + ovs_strerror(errno), netdev_rxq_get_name(&rxq->up)); } - return -errno; + return errno; } } } - static int -netdev_rx_bsd_recv(struct netdev_rx *rx_, void *data, size_t size) +netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct ofpbuf **packet, int *c) { - struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_); + struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); + struct netdev *netdev = rxq->up.netdev; + struct ofpbuf *buffer; + ssize_t retval; + int mtu; + + if (netdev_bsd_get_mtu(netdev, &mtu)) { + mtu = ETH_PAYLOAD_MAX; + } + + buffer = ofpbuf_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu, DP_NETDEV_HEADROOM); - return (rx->pcap_handle - ? netdev_rx_bsd_recv_pcap(rx, data, size) - : netdev_rx_bsd_recv_tap(rx, data, size)); + retval = (rxq->pcap_handle + ? netdev_rxq_bsd_recv_pcap(rxq, buffer) + : netdev_rxq_bsd_recv_tap(rxq, buffer)); + + if (retval) { + ofpbuf_delete(buffer); + } else { + dp_packet_pad(buffer); + packet[0] = buffer; + *c = 1; + } + return retval; } /* * Registers with the poll loop to wake up from the next call to poll_block() - * when a packet is ready to be received with netdev_rx_recv() on 'rx'. + * when a packet is ready to be received with netdev_rxq_recv() on 'rxq'. */ static void -netdev_rx_bsd_wait(struct netdev_rx *rx_) +netdev_bsd_rxq_wait(struct netdev_rxq *rxq_) { - struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_); + struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); - poll_fd_wait(rx->fd, POLLIN); + poll_fd_wait(rxq->fd, POLLIN); } -/* Discards all packets waiting to be received from 'rx'. */ +/* Discards all packets waiting to be received from 'rxq'. */ static int -netdev_rx_bsd_drain(struct netdev_rx *rx_) +netdev_bsd_rxq_drain(struct netdev_rxq *rxq_) { struct ifreq ifr; - struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_); + struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); - strcpy(ifr.ifr_name, netdev_rx_get_name(rx_)); - if (ioctl(rx->fd, BIOCFLUSH, &ifr) == -1) { + strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rxq_get_netdev(rxq_))); + if (ioctl(rxq->fd, BIOCFLUSH, &ifr) == -1) { VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s", - netdev_rx_get_name(rx_), strerror(errno)); + netdev_rxq_get_name(rxq_), ovs_strerror(errno)); return errno; } return 0; @@ -705,19 +681,22 @@ netdev_rx_bsd_drain(struct netdev_rx *rx_) * system or a tap device. */ static int -netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size) +netdev_bsd_send(struct netdev *netdev_, struct ofpbuf *pkt, bool may_steal) { - struct netdev_dev_bsd *dev = netdev_dev_bsd_cast(netdev_get_dev(netdev_)); + struct netdev_bsd *dev = netdev_bsd_cast(netdev_); const char *name = netdev_get_name(netdev_); + const void *data = ofpbuf_data(pkt); + size_t size = ofpbuf_size(pkt); + int error; + ovs_mutex_lock(&dev->mutex); if (dev->tap_fd < 0 && !dev->pcap) { - int error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd); - if (error) { - return error; - } + error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd); + } else { + error = 0; } - for (;;) { + while (!error) { ssize_t retval; if (dev->tap_fd >= 0) { retval = write(dev->tap_fd, data, size); @@ -727,19 +706,28 @@ netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size) if (retval < 0) { if (errno == EINTR) { continue; - } else if (errno != EAGAIN) { - VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s", - name, strerror(errno)); + } else { + error = errno; + if (error != EAGAIN) { + VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: " + "%s", name, ovs_strerror(error)); + } } - return errno; } else if (retval != size) { - VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of " - "%zu) on %s", retval, size, name); - return EMSGSIZE; + VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIuSIZE"d bytes of " + "%"PRIuSIZE") on %s", retval, size, name); + error = EMSGSIZE; } else { - return 0; + break; } } + + ovs_mutex_unlock(&dev->mutex); + if (may_steal) { + ofpbuf_delete(pkt); + } + + return error; } /* @@ -750,8 +738,9 @@ netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size) static void netdev_bsd_send_wait(struct netdev *netdev_) { - struct netdev_dev_bsd *dev = netdev_dev_bsd_cast(netdev_get_dev(netdev_)); + struct netdev_bsd *dev = netdev_bsd_cast(netdev_); + ovs_mutex_lock(&dev->mutex); if (dev->tap_fd >= 0) { /* TAP device always accepts packets. */ poll_immediate_wake(); @@ -761,6 +750,7 @@ netdev_bsd_send_wait(struct netdev *netdev_) /* We haven't even tried to send a packet yet. */ poll_immediate_wake(); } + ovs_mutex_unlock(&dev->mutex); } /* @@ -771,22 +761,22 @@ static int netdev_bsd_set_etheraddr(struct netdev *netdev_, const uint8_t mac[ETH_ADDR_LEN]) { - struct netdev_dev_bsd *netdev_dev = - netdev_dev_bsd_cast(netdev_get_dev(netdev_)); - int error; + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); + int error = 0; - if (!(netdev_dev->cache_valid & VALID_ETHERADDR) - || !eth_addr_equals(netdev_dev->etheraddr, mac)) { - error = set_etheraddr(netdev_get_name(netdev_), AF_LINK, ETH_ADDR_LEN, - mac); + ovs_mutex_lock(&netdev->mutex); + if (!(netdev->cache_valid & VALID_ETHERADDR) + || !eth_addr_equals(netdev->etheraddr, mac)) { + error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK, + ETH_ADDR_LEN, mac); if (!error) { - netdev_dev->cache_valid |= VALID_ETHERADDR; - memcpy(netdev_dev->etheraddr, mac, ETH_ADDR_LEN); - netdev_dev_bsd_changed(netdev_dev); + netdev->cache_valid |= VALID_ETHERADDR; + memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN); + netdev_change_seq_changed(netdev_); } - } else { - error = 0; } + ovs_mutex_unlock(&netdev->mutex); + return error; } @@ -798,20 +788,23 @@ static int netdev_bsd_get_etheraddr(const struct netdev *netdev_, uint8_t mac[ETH_ADDR_LEN]) { - struct netdev_dev_bsd *netdev_dev = - netdev_dev_bsd_cast(netdev_get_dev(netdev_)); - - if (!(netdev_dev->cache_valid & VALID_ETHERADDR)) { - int error = get_etheraddr(netdev_get_name(netdev_), - netdev_dev->etheraddr); - if (error) { - return error; + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); + int error = 0; + + ovs_mutex_lock(&netdev->mutex); + if (!(netdev->cache_valid & VALID_ETHERADDR)) { + error = get_etheraddr(netdev_get_kernel_name(netdev_), + netdev->etheraddr); + if (!error) { + netdev->cache_valid |= VALID_ETHERADDR; } - netdev_dev->cache_valid |= VALID_ETHERADDR; } - memcpy(mac, netdev_dev->etheraddr, ETH_ADDR_LEN); + if (!error) { + memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN); + } + ovs_mutex_unlock(&netdev->mutex); - return 0; + return error; } /* @@ -822,71 +815,157 @@ netdev_bsd_get_etheraddr(const struct netdev *netdev_, static int netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup) { - struct netdev_dev_bsd *netdev_dev = - netdev_dev_bsd_cast(netdev_get_dev(netdev_)); + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); + int error = 0; - if (!(netdev_dev->cache_valid & VALID_MTU)) { + ovs_mutex_lock(&netdev->mutex); + if (!(netdev->cache_valid & VALID_MTU)) { struct ifreq ifr; - int error; - error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr, SIOCGIFMTU, - "SIOCGIFMTU"); - if (error) { - return error; + error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr, + SIOCGIFMTU, "SIOCGIFMTU"); + if (!error) { + netdev->mtu = ifr.ifr_mtu; + netdev->cache_valid |= VALID_MTU; } - netdev_dev->mtu = ifr.ifr_mtu; - netdev_dev->cache_valid |= VALID_MTU; } + if (!error) { + *mtup = netdev->mtu; + } + ovs_mutex_unlock(&netdev->mutex); - *mtup = netdev_dev->mtu; return 0; } static int -netdev_bsd_get_ifindex(const struct netdev *netdev) +netdev_bsd_get_ifindex(const struct netdev *netdev_) { + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); int ifindex, error; - error = get_ifindex(netdev, &ifindex); + ovs_mutex_lock(&netdev->mutex); + error = get_ifindex(netdev_, &ifindex); + ovs_mutex_unlock(&netdev->mutex); + return error ? -error : ifindex; } static int netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier) { - struct netdev_dev_bsd *netdev_dev = - netdev_dev_bsd_cast(netdev_get_dev(netdev_)); + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); + int error = 0; - if (!(netdev_dev->cache_valid & VALID_CARRIER)) { + ovs_mutex_lock(&netdev->mutex); + if (!(netdev->cache_valid & VALID_CARRIER)) { struct ifmediareq ifmr; memset(&ifmr, 0, sizeof(ifmr)); - strncpy(ifmr.ifm_name, netdev_get_name(netdev_), sizeof ifmr.ifm_name); + strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_), + sizeof ifmr.ifm_name); + + error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr); + if (!error) { + netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE; + netdev->cache_valid |= VALID_CARRIER; - if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) { + /* If the interface doesn't report whether the media is active, + * just assume it is active. */ + if ((ifmr.ifm_status & IFM_AVALID) == 0) { + netdev->carrier = true; + } + } else { VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s", - netdev_get_name(netdev_), strerror(errno)); - return errno; + netdev_get_name(netdev_), ovs_strerror(error)); } + } + if (!error) { + *carrier = netdev->carrier; + } + ovs_mutex_unlock(&netdev->mutex); - netdev_dev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE; - netdev_dev->cache_valid |= VALID_CARRIER; + return error; +} - /* If the interface doesn't report whether the media is active, - * just assume it is active. */ - if ((ifmr.ifm_status & IFM_AVALID) == 0) { - netdev_dev->carrier = true; - } - } - *carrier = netdev_dev->carrier; +static void +convert_stats_system(struct netdev_stats *stats, const struct if_data *ifd) +{ + /* + * note: UINT64_MAX means unsupported + */ + stats->rx_packets = ifd->ifi_ipackets; + stats->tx_packets = ifd->ifi_opackets; + stats->rx_bytes = ifd->ifi_obytes; + stats->tx_bytes = ifd->ifi_ibytes; + stats->rx_errors = ifd->ifi_ierrors; + stats->tx_errors = ifd->ifi_oerrors; + stats->rx_dropped = ifd->ifi_iqdrops; + stats->tx_dropped = UINT64_MAX; + stats->multicast = ifd->ifi_imcasts; + stats->collisions = ifd->ifi_collisions; + stats->rx_length_errors = UINT64_MAX; + stats->rx_over_errors = UINT64_MAX; + stats->rx_crc_errors = UINT64_MAX; + stats->rx_frame_errors = UINT64_MAX; + stats->rx_fifo_errors = UINT64_MAX; + stats->rx_missed_errors = UINT64_MAX; + stats->tx_aborted_errors = UINT64_MAX; + stats->tx_carrier_errors = UINT64_MAX; + stats->tx_fifo_errors = UINT64_MAX; + stats->tx_heartbeat_errors = UINT64_MAX; + stats->tx_window_errors = UINT64_MAX; +} - return 0; +static void +convert_stats_tap(struct netdev_stats *stats, const struct if_data *ifd) +{ + /* + * Similar to convert_stats_system but swapping rxq and tx + * because 'ifd' is stats for the network interface side of the + * tap device and what the caller wants is one for the character + * device side. + * + * note: UINT64_MAX means unsupported + */ + stats->rx_packets = ifd->ifi_opackets; + stats->tx_packets = ifd->ifi_ipackets; + stats->rx_bytes = ifd->ifi_ibytes; + stats->tx_bytes = ifd->ifi_obytes; + stats->rx_errors = ifd->ifi_oerrors; + stats->tx_errors = ifd->ifi_ierrors; + stats->rx_dropped = UINT64_MAX; + stats->tx_dropped = ifd->ifi_iqdrops; + stats->multicast = ifd->ifi_omcasts; + stats->collisions = UINT64_MAX; + stats->rx_length_errors = UINT64_MAX; + stats->rx_over_errors = UINT64_MAX; + stats->rx_crc_errors = UINT64_MAX; + stats->rx_frame_errors = UINT64_MAX; + stats->rx_fifo_errors = UINT64_MAX; + stats->rx_missed_errors = UINT64_MAX; + stats->tx_aborted_errors = UINT64_MAX; + stats->tx_carrier_errors = UINT64_MAX; + stats->tx_fifo_errors = UINT64_MAX; + stats->tx_heartbeat_errors = UINT64_MAX; + stats->tx_window_errors = UINT64_MAX; +} + +static void +convert_stats(const struct netdev *netdev, struct netdev_stats *stats, + const struct if_data *ifd) +{ + if (netdev_bsd_cast(netdev)->tap_fd == -1) { + convert_stats_system(stats, ifd); + } else { + convert_stats_tap(stats, ifd); + } } /* Retrieves current device stats for 'netdev'. */ static int netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats) { +#if defined(__FreeBSD__) int if_count, i; int mib[6]; size_t len; @@ -903,7 +982,7 @@ netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats) if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) { VLOG_DBG_RL(&rl, "%s: sysctl failed: %s", - netdev_get_name(netdev_), strerror(errno)); + netdev_get_name(netdev_), ovs_strerror(errno)); return errno; } @@ -914,37 +993,30 @@ netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats) mib[4] = i; //row if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) { VLOG_DBG_RL(&rl, "%s: sysctl failed: %s", - netdev_get_name(netdev_), strerror(errno)); + netdev_get_name(netdev_), ovs_strerror(errno)); return errno; } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) { - stats->rx_packets = ifmd.ifmd_data.ifi_ipackets; - stats->tx_packets = ifmd.ifmd_data.ifi_opackets; - stats->rx_bytes = ifmd.ifmd_data.ifi_ibytes; - stats->tx_bytes = ifmd.ifmd_data.ifi_obytes; - stats->rx_errors = ifmd.ifmd_data.ifi_ierrors; - stats->tx_errors = ifmd.ifmd_data.ifi_oerrors; - stats->rx_dropped = ifmd.ifmd_data.ifi_iqdrops; - stats->tx_dropped = UINT64_MAX; - stats->multicast = ifmd.ifmd_data.ifi_imcasts; - stats->collisions = ifmd.ifmd_data.ifi_collisions; - - stats->rx_length_errors = UINT64_MAX; - stats->rx_over_errors = UINT64_MAX; - stats->rx_crc_errors = UINT64_MAX; - stats->rx_frame_errors = UINT64_MAX; - stats->rx_fifo_errors = UINT64_MAX; - stats->rx_missed_errors = UINT64_MAX; - - stats->tx_aborted_errors = UINT64_MAX; - stats->tx_carrier_errors = UINT64_MAX; - stats->tx_fifo_errors = UINT64_MAX; - stats->tx_heartbeat_errors = UINT64_MAX; - stats->tx_window_errors = UINT64_MAX; + convert_stats(netdev, stats, &ifdr.ifdr_data); break; } } return 0; +#elif defined(__NetBSD__) + struct ifdatareq ifdr; + int error; + + memset(&ifdr, 0, sizeof(ifdr)); + strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_), + sizeof(ifdr.ifdr_name)); + error = af_link_ioctl(SIOCGIFDATA, &ifdr); + if (!error) { + convert_stats(netdev_, stats, &ifdr.ifdr_data); + } + return error; +#else +#error not implemented +#endif } static uint32_t @@ -1045,10 +1117,11 @@ netdev_bsd_get_features(const struct netdev *netdev, /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the * number of supported modes, and a second with a buffer to retrieve * them. */ - if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) { + error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr); + if (error) { VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s", - netdev_get_name(netdev), strerror(errno)); - return errno; + netdev_get_name(netdev), ovs_strerror(error)); + return error; } media_list = xcalloc(ifmr.ifm_count, sizeof(int)); @@ -1061,10 +1134,10 @@ netdev_bsd_get_features(const struct netdev *netdev, goto cleanup; } - if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) { + error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr); + if (error) { VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s", - netdev_get_name(netdev), strerror(errno)); - error = errno; + netdev_get_name(netdev), ovs_strerror(error)); goto cleanup; } @@ -1090,41 +1163,43 @@ cleanup: } /* - * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if - * 'in4' is non-null) and returns true. Otherwise, returns false. + * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and + * '*netmask' to its netmask and returns true. Otherwise, returns false. */ static int netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4, struct in_addr *netmask) { - struct netdev_dev_bsd *netdev_dev = - netdev_dev_bsd_cast(netdev_get_dev(netdev_)); + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); + int error = 0; - if (!(netdev_dev->cache_valid & VALID_IN4)) { - const struct sockaddr_in *sin; + ovs_mutex_lock(&netdev->mutex); + if (!(netdev->cache_valid & VALID_IN4)) { struct ifreq ifr; - int error; ifr.ifr_addr.sa_family = AF_INET; - error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr, + error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr, SIOCGIFADDR, "SIOCGIFADDR"); - if (error) { - return error; - } - - sin = (struct sockaddr_in *) &ifr.ifr_addr; - netdev_dev->in4 = sin->sin_addr; - netdev_dev->cache_valid |= VALID_IN4; - error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr, - SIOCGIFNETMASK, "SIOCGIFNETMASK"); - if (error) { - return error; + if (!error) { + const struct sockaddr_in *sin; + + sin = (struct sockaddr_in *) &ifr.ifr_addr; + netdev->in4 = sin->sin_addr; + netdev->cache_valid |= VALID_IN4; + error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr, + SIOCGIFNETMASK, "SIOCGIFNETMASK"); + if (!error) { + *netmask = sin->sin_addr; + } } - *netmask = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr; } - *in4 = netdev_dev->in4; + if (!error) { + *in4 = netdev->in4; + *netmask = netdev->netmask; + } + ovs_mutex_unlock(&netdev->mutex); - return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0; + return error ? error : in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0; } /* @@ -1136,36 +1211,40 @@ static int netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr, struct in_addr mask) { - struct netdev_dev_bsd *netdev_dev = - netdev_dev_bsd_cast(netdev_get_dev(netdev_)); + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); int error; + ovs_mutex_lock(&netdev->mutex); error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr); if (!error) { - netdev_dev->cache_valid |= VALID_IN4; - netdev_dev->in4 = addr; if (addr.s_addr != INADDR_ANY) { error = do_set_addr(netdev_, SIOCSIFNETMASK, "SIOCSIFNETMASK", mask); + if (!error) { + netdev->cache_valid |= VALID_IN4; + netdev->in4 = addr; + netdev->netmask = mask; + } } - netdev_dev_bsd_changed(netdev_dev); + netdev_change_seq_changed(netdev_); } + ovs_mutex_unlock(&netdev->mutex); + return error; } static int netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6) { - struct netdev_dev_bsd *netdev_dev = - netdev_dev_bsd_cast(netdev_get_dev(netdev_)); - if (!(netdev_dev->cache_valid & VALID_IN6)) { + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); + if (!(netdev->cache_valid & VALID_IN6)) { struct ifaddrs *ifa, *head; struct sockaddr_in6 *sin6; const char *netdev_name = netdev_get_name(netdev_); if (getifaddrs(&head) != 0) { VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name, - strerror(errno)); + ovs_strerror(errno)); return errno; } @@ -1174,9 +1253,9 @@ netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6) !strcmp(ifa->ifa_name, netdev_name)) { sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; if (sin6) { - memcpy(&netdev_dev->in6, &sin6->sin6_addr, sin6->sin6_len); - netdev_dev->cache_valid |= VALID_IN6; - *in6 = netdev_dev->in6; + memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len); + netdev->cache_valid |= VALID_IN6; + *in6 = netdev->in6; freeifaddrs(head); return 0; } @@ -1184,8 +1263,193 @@ netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6) } return EADDRNOTAVAIL; } - *in6 = netdev_dev->in6; + *in6 = netdev->in6; + return 0; +} + +#if defined(__NetBSD__) +static char * +netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name) +{ + char *ovs_name = NULL; + struct shash device_shash; + struct shash_node *node; + + shash_init(&device_shash); + netdev_get_devices(&netdev_tap_class, &device_shash); + SHASH_FOR_EACH(node, &device_shash) { + struct netdev *netdev = node->data; + struct netdev_bsd * const dev = netdev_bsd_cast(netdev); + + if (!strcmp(dev->kernel_name, kernel_name)) { + free(ovs_name); + ovs_name = xstrdup(netdev_get_name(&dev->up)); + } + netdev_close(netdev); + } + shash_destroy(&device_shash); + + return ovs_name ? ovs_name : xstrdup(kernel_name); +} +#endif + +static int +netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED, + struct in_addr *next_hop OVS_UNUSED, + char **netdev_name OVS_UNUSED) +{ +#if defined(__NetBSD__) + static int seq = 0; + struct sockaddr_in sin; + struct sockaddr_dl sdl; + int s; + int i; + struct { + struct rt_msghdr h; + char space[512]; + } buf; + struct rt_msghdr *rtm = &buf.h; + const pid_t pid = getpid(); + char *cp; + ssize_t ssz; + bool gateway = false; + char *ifname = NULL; + int saved_errno; + + memset(next_hop, 0, sizeof(*next_hop)); + *netdev_name = NULL; + + memset(&sin, 0, sizeof(sin)); + sin.sin_len = sizeof(sin); + sin.sin_family = AF_INET; + sin.sin_port = 0; + sin.sin_addr = *host; + + memset(&sdl, 0, sizeof(sdl)); + sdl.sdl_len = sizeof(sdl); + sdl.sdl_family = AF_LINK; + + s = socket(PF_ROUTE, SOCK_RAW, 0); + memset(&buf, 0, sizeof(buf)); + rtm->rtm_flags = RTF_HOST|RTF_UP; + rtm->rtm_version = RTM_VERSION; + rtm->rtm_addrs = RTA_DST|RTA_IFP; + cp = (void *)&buf.space; + memcpy(cp, &sin, sizeof(sin)); + RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin); + memcpy(cp, &sdl, sizeof(sdl)); + RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl); + rtm->rtm_msglen = cp - (char *)(void *)rtm; + rtm->rtm_seq = ++seq; + rtm->rtm_type = RTM_GET; + rtm->rtm_pid = pid; + write(s, rtm, rtm->rtm_msglen); + memset(&buf, 0, sizeof(buf)); + do { + ssz = read(s, &buf, sizeof(buf)); + } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); + saved_errno = errno; + close(s); + if (ssz <= 0) { + if (ssz < 0) { + return saved_errno; + } + return EPIPE; /* XXX */ + } + cp = (void *)&buf.space; + for (i = 1; i; i <<= 1) { + if ((rtm->rtm_addrs & i) != 0) { + const struct sockaddr *sa = (const void *)cp; + + if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) { + const struct sockaddr_in * const sin = + (const struct sockaddr_in *)sa; + + *next_hop = sin->sin_addr; + gateway = true; + } + if ((i == RTA_IFP) && sa->sa_family == AF_LINK) { + const struct sockaddr_dl * const sdl = + (const struct sockaddr_dl *)sa; + char *kernel_name; + + kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen); + ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name); + free(kernel_name); + } + RT_ADVANCE(cp, sa); + } + } + if (ifname == NULL) { + return ENXIO; + } + if (!gateway) { + *next_hop = *host; + } + *netdev_name = ifname; + VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s", + IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name); return 0; +#else + return EOPNOTSUPP; +#endif +} + +static int +netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED, + ovs_be32 ip OVS_UNUSED, + uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED) +{ +#if defined(__NetBSD__) + const struct rt_msghdr *rtm; + size_t needed; + char *buf; + const char *cp; + const char *ep; + int mib[6]; + int error; + + buf = NULL; + mib[0] = CTL_NET; + mib[1] = PF_ROUTE; + mib[2] = 0; + mib[3] = AF_INET; + mib[4] = NET_RT_FLAGS; + mib[5] = RTF_LLINFO; + if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) { + error = errno; + goto error; + } + buf = xmalloc(needed); + if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) { + error = errno; + goto error; + } + ep = buf + needed; + for (cp = buf; cp < ep; cp += rtm->rtm_msglen) { + const struct sockaddr_inarp *sina; + const struct sockaddr_dl *sdl; + + rtm = (const void *)cp; + sina = (const void *)(rtm + 1); + if (ip != sina->sin_addr.s_addr) { + continue; + } + sdl = (const void *) + ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len)); + if (sdl->sdl_alen == ETH_ADDR_LEN) { + memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN); + error = 0; + goto error; + } + } + error = ENXIO; +error: + free(buf); + return error; +#else + return EOPNOTSUPP; +#endif } static void @@ -1203,11 +1467,13 @@ make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr) static int do_set_addr(struct netdev *netdev, - int ioctl_nr, const char *ioctl_name, struct in_addr addr) + unsigned long ioctl_nr, const char *ioctl_name, + struct in_addr addr) { struct ifreq ifr; make_in4_sockaddr(&ifr.ifr_addr, addr); - return netdev_bsd_do_ioctl(netdev, &ifr, ioctl_nr, ioctl_name); + return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr, + ioctl_name); } static int @@ -1219,7 +1485,12 @@ nd_to_iff_flags(enum netdev_flags nd) } if (nd & NETDEV_PROMISC) { iff |= IFF_PROMISC; +#if defined(IFF_PPROMISC) iff |= IFF_PPROMISC; +#endif + } + if (nd & NETDEV_LOOPBACK) { + iff |= IFF_LOOPBACK; } return iff; } @@ -1234,157 +1505,110 @@ iff_to_nd_flags(int iff) if (iff & IFF_PROMISC) { nd |= NETDEV_PROMISC; } + if (iff & IFF_LOOPBACK) { + nd |= NETDEV_LOOPBACK; + } return nd; } static int -netdev_bsd_update_flags(struct netdev_dev *dev_, enum netdev_flags off, +netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off, enum netdev_flags on, enum netdev_flags *old_flagsp) { - struct netdev_dev_bsd *netdev_dev; int old_flags, new_flags; int error; - netdev_dev = netdev_dev_bsd_cast(dev_); - error = get_flags(dev_, &old_flags); + error = get_flags(netdev_, &old_flags); if (!error) { *old_flagsp = iff_to_nd_flags(old_flags); new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on); if (new_flags != old_flags) { - error = set_flags(netdev_dev_get_name(dev_), new_flags); - netdev_dev_bsd_changed(netdev_dev); + error = set_flags(netdev_get_kernel_name(netdev_), new_flags); + netdev_change_seq_changed(netdev_); } } return error; } -static unsigned int -netdev_bsd_change_seq(const struct netdev *netdev) -{ - return netdev_dev_bsd_cast(netdev_get_dev(netdev))->change_seq; +/* Linux has also different GET_STATS, SET_STATS, + * GET_STATUS) + */ +#define NETDEV_BSD_CLASS(NAME, CONSTRUCT, \ + GET_FEATURES) \ +{ \ + NAME, \ + \ + NULL, /* init */ \ + netdev_bsd_run, \ + netdev_bsd_wait, \ + netdev_bsd_alloc, \ + CONSTRUCT, \ + netdev_bsd_destruct, \ + netdev_bsd_dealloc, \ + NULL, /* get_config */ \ + NULL, /* set_config */ \ + NULL, /* get_tunnel_config */ \ + \ + netdev_bsd_send, \ + netdev_bsd_send_wait, \ + \ + netdev_bsd_set_etheraddr, \ + netdev_bsd_get_etheraddr, \ + netdev_bsd_get_mtu, \ + NULL, /* set_mtu */ \ + netdev_bsd_get_ifindex, \ + netdev_bsd_get_carrier, \ + NULL, /* get_carrier_resets */ \ + NULL, /* set_miimon_interval */ \ + netdev_bsd_get_stats, \ + NULL, /* set_stats */ \ + \ + GET_FEATURES, \ + NULL, /* set_advertisement */ \ + NULL, /* set_policing */ \ + NULL, /* get_qos_type */ \ + NULL, /* get_qos_capabilities */ \ + NULL, /* get_qos */ \ + NULL, /* set_qos */ \ + NULL, /* get_queue */ \ + NULL, /* set_queue */ \ + NULL, /* delete_queue */ \ + NULL, /* get_queue_stats */ \ + NULL, /* queue_dump_start */ \ + NULL, /* queue_dump_next */ \ + NULL, /* queue_dump_done */ \ + NULL, /* dump_queue_stats */ \ + \ + netdev_bsd_get_in4, \ + netdev_bsd_set_in4, \ + netdev_bsd_get_in6, \ + NULL, /* add_router */ \ + netdev_bsd_get_next_hop, \ + NULL, /* get_status */ \ + netdev_bsd_arp_lookup, /* arp_lookup */ \ + \ + netdev_bsd_update_flags, \ + \ + netdev_bsd_rxq_alloc, \ + netdev_bsd_rxq_construct, \ + netdev_bsd_rxq_destruct, \ + netdev_bsd_rxq_dealloc, \ + netdev_bsd_rxq_recv, \ + netdev_bsd_rxq_wait, \ + netdev_bsd_rxq_drain, \ } - -const struct netdev_class netdev_bsd_class = { - "system", - - netdev_bsd_init, - netdev_bsd_run, - netdev_bsd_wait, - netdev_bsd_create_system, - netdev_bsd_destroy, - NULL, /* get_config */ - NULL, /* set_config */ - NULL, /* get_tunnel_config */ - netdev_bsd_open_system, - netdev_bsd_close, - - netdev_bsd_rx_open, - - netdev_bsd_send, - netdev_bsd_send_wait, - - netdev_bsd_set_etheraddr, - netdev_bsd_get_etheraddr, - netdev_bsd_get_mtu, - NULL, /* set_mtu */ - netdev_bsd_get_ifindex, - netdev_bsd_get_carrier, - NULL, /* get_carrier_resets */ - NULL, /* set_miimon_interval */ - netdev_bsd_get_stats, - NULL, /* set_stats */ - - netdev_bsd_get_features, - NULL, /* set_advertisement */ - NULL, /* set_policing */ - NULL, /* get_qos_type */ - NULL, /* get_qos_capabilities */ - NULL, /* get_qos */ - NULL, /* set_qos */ - NULL, /* get_queue */ - NULL, /* set_queue */ - NULL, /* delete_queue */ - NULL, /* get_queue_stats */ - NULL, /* dump_queue */ - NULL, /* dump_queue_stats */ - - netdev_bsd_get_in4, - netdev_bsd_set_in4, - netdev_bsd_get_in6, - NULL, /* add_router */ - NULL, /* get_next_hop */ - NULL, /* get_status */ - NULL, /* arp_lookup */ - - netdev_bsd_update_flags, - - netdev_bsd_change_seq -}; - -const struct netdev_class netdev_tap_class = { - "tap", - - netdev_bsd_init, - netdev_bsd_run, - netdev_bsd_wait, - netdev_bsd_create_tap, - netdev_bsd_destroy, - NULL, /* get_config */ - NULL, /* set_config */ - NULL, /* get_tunnel_config */ - netdev_bsd_open_system, - netdev_bsd_close, - - netdev_bsd_rx_open, - - netdev_bsd_send, - netdev_bsd_send_wait, - - netdev_bsd_set_etheraddr, - netdev_bsd_get_etheraddr, - netdev_bsd_get_mtu, - NULL, /* set_mtu */ - netdev_bsd_get_ifindex, - netdev_bsd_get_carrier, - NULL, /* get_carrier_resets */ - NULL, /* set_miimon_interval */ - netdev_bsd_get_stats, - NULL, /* set_stats */ - - netdev_bsd_get_features, - NULL, /* set_advertisement */ - NULL, /* set_policing */ - NULL, /* get_qos_type */ - NULL, /* get_qos_capabilities */ - NULL, /* get_qos */ - NULL, /* set_qos */ - NULL, /* get_queue */ - NULL, /* set_queue */ - NULL, /* delete_queue */ - NULL, /* get_queue_stats */ - NULL, /* dump_queue */ - NULL, /* dump_queue_stats */ - - netdev_bsd_get_in4, - netdev_bsd_set_in4, - netdev_bsd_get_in6, - NULL, /* add_router */ - NULL, /* get_next_hop */ - NULL, /* get_status */ - NULL, /* arp_lookup */ - - netdev_bsd_update_flags, - - netdev_bsd_change_seq -}; - -static const struct netdev_rx_class netdev_rx_bsd_class = { - netdev_rx_bsd_destroy, - netdev_rx_bsd_recv, - netdev_rx_bsd_wait, - netdev_rx_bsd_drain, -}; +const struct netdev_class netdev_bsd_class = + NETDEV_BSD_CLASS( + "system", + netdev_bsd_construct_system, + netdev_bsd_get_features); + +const struct netdev_class netdev_tap_class = + NETDEV_BSD_CLASS( + "tap", + netdev_bsd_construct_tap, + netdev_bsd_get_features); static void @@ -1395,19 +1619,19 @@ destroy_tap(int fd, const char *name) close(fd); strcpy(ifr.ifr_name, name); /* XXX What to do if this call fails? */ - ioctl(af_inet_sock, SIOCIFDESTROY, &ifr); + af_inet_ioctl(SIOCIFDESTROY, &ifr); } static int -get_flags(const struct netdev_dev *dev, int *flags) +get_flags(const struct netdev *netdev, int *flags) { struct ifreq ifr; int error; - error = netdev_bsd_do_ioctl(dev->name, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS"); + error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, + SIOCGIFFLAGS, "SIOCGIFFLAGS"); - *flags = 0xFFFF0000 & (ifr.ifr_flagshigh << 16); - *flags |= 0x0000FFFF & ifr.ifr_flags; + *flags = ifr_get_flags(&ifr); return error; } @@ -1417,27 +1641,25 @@ set_flags(const char *name, int flags) { struct ifreq ifr; - ifr.ifr_flags = 0x0000FFFF & flags; - ifr.ifr_flagshigh = (0xFFFF0000 & flags) >> 16; + ifr_set_flags(&ifr, flags); - return netdev_bsd_do_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS"); + return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS"); } static int get_ifindex(const struct netdev *netdev_, int *ifindexp) { - struct netdev_dev_bsd *netdev_dev = - netdev_dev_bsd_cast(netdev_get_dev(netdev_)); + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); *ifindexp = 0; - if (!(netdev_dev->cache_valid & VALID_IFINDEX)) { + if (!(netdev->cache_valid & VALID_IFINDEX)) { int ifindex = if_nametoindex(netdev_get_name(netdev_)); if (ifindex <= 0) { return errno; } - netdev_dev->cache_valid |= VALID_IFINDEX; - netdev_dev->ifindex = ifindex; + netdev->cache_valid |= VALID_IFINDEX; + netdev->ifindex = ifindex; } - *ifindexp = netdev_dev->ifindex; + *ifindexp = netdev->ifindex; return 0; } @@ -1450,7 +1672,7 @@ get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]) if (getifaddrs(&head) != 0) { VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name, - strerror(errno)); + ovs_strerror(errno)); return errno; } @@ -1473,33 +1695,115 @@ get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]) } static int -set_etheraddr(const char *netdev_name, int hwaddr_family, - int hwaddr_len, const uint8_t mac[ETH_ADDR_LEN]) +set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED, + int hwaddr_len OVS_UNUSED, + const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED) { +#if defined(__FreeBSD__) struct ifreq ifr; + int error; memset(&ifr, 0, sizeof ifr); strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name); ifr.ifr_addr.sa_family = hwaddr_family; ifr.ifr_addr.sa_len = hwaddr_len; memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len); - if (ioctl(af_inet_sock, SIOCSIFLLADDR, &ifr) < 0) { + error = af_inet_ioctl(SIOCSIFLLADDR, &ifr); + if (error) { VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s", - netdev_name, strerror(errno)); - return errno; + netdev_name, ovs_strerror(error)); + return error; } return 0; +#elif defined(__NetBSD__) + struct if_laddrreq req; + struct sockaddr_dl *sdl; + struct sockaddr_storage oldaddr; + int error; + + /* + * get the old address, add new one, and then remove old one. + */ + + if (hwaddr_len != ETH_ADDR_LEN) { + /* just to be safe about sockaddr storage size */ + return EOPNOTSUPP; + } + memset(&req, 0, sizeof(req)); + strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name)); + req.addr.ss_len = sizeof(req.addr); + req.addr.ss_family = hwaddr_family; + sdl = (struct sockaddr_dl *)&req.addr; + sdl->sdl_alen = hwaddr_len; + + error = af_link_ioctl(SIOCGLIFADDR, &req); + if (error) { + return error; + } + if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], mac, hwaddr_len)) { + return 0; + } + oldaddr = req.addr; + + memset(&req, 0, sizeof(req)); + strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name)); + req.flags = IFLR_ACTIVE; + sdl = (struct sockaddr_dl *)&req.addr; + sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len; + sdl->sdl_alen = hwaddr_len; + sdl->sdl_family = hwaddr_family; + memcpy(sdl->sdl_data, mac, hwaddr_len); + error = af_link_ioctl(SIOCALIFADDR, &req); + if (error) { + return error; + } + + memset(&req, 0, sizeof(req)); + strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name)); + req.addr = oldaddr; + return af_link_ioctl(SIOCDLIFADDR, &req); +#else +#error not implemented +#endif } static int -netdev_bsd_do_ioctl(const char *name, struct ifreq *ifr, unsigned long cmd, - const char *cmd_name) +ifr_get_flags(const struct ifreq *ifr) { - strncpy(ifr->ifr_name, name, sizeof ifr->ifr_name); - if (ioctl(af_inet_sock, cmd, ifr) == -1) { - VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name, - strerror(errno)); - return errno; +#ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH + return (ifr->ifr_flagshigh << 16) | ifr->ifr_flags; +#else + return ifr->ifr_flags; +#endif +} + +static void +ifr_set_flags(struct ifreq *ifr, int flags) +{ + ifr->ifr_flags = flags; +#ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH + ifr->ifr_flagshigh = flags >> 16; +#endif +} + +/* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and + * 'arg'. Returns 0 if successful, otherwise a positive errno value. */ +int +af_link_ioctl(unsigned long command, const void *arg) +{ + static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; + static int sock; + + if (ovsthread_once_start(&once)) { + sock = socket(AF_LINK, SOCK_DGRAM, 0); + if (sock < 0) { + sock = -errno; + VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno)); + } + ovsthread_once_done(&once); } - return 0; + + return (sock < 0 ? -sock + : ioctl(sock, command, arg) == -1 ? errno + : 0); }