X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fnetdev-bsd.c;h=0fb0057a23dbcdbec291bcaa9a1bf4a5fbbdbb23;hb=bff7eeb648a03598c57ac9a8288132379640cb2c;hp=9eb502e6b13c1fbecc5beff093162b7b7f586c8a;hpb=796223f5bc3a4896e6398733c798390158479400;p=sliver-openvswitch.git diff --git a/lib/netdev-bsd.c b/lib/netdev-bsd.c index 9eb502e6b..0fb0057a2 100644 --- a/lib/netdev-bsd.c +++ b/lib/netdev-bsd.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2011 Gaetano Catalli. + * Copyright (c) 2013 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,11 +33,16 @@ #include #include #include +#ifdef HAVE_NET_IF_MIB_H #include +#endif #include #include #include #include +#if defined(__NetBSD__) +#include +#endif #include "rtbsd.h" #include "coverage.h" @@ -55,33 +61,6 @@ 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; @@ -96,8 +75,8 @@ struct netdev_rx_bsd { static const struct netdev_rx_class netdev_rx_bsd_class; -struct netdev_dev_bsd { - struct netdev_dev netdev_dev; +struct netdev_bsd { + struct netdev up; unsigned int cache_valid; unsigned int change_seq; @@ -113,6 +92,8 @@ struct netdev_dev_bsd { /* Used for sending packets on non-tap devices. */ pcap_t *pcap; int fd; + + char *kernel_name; }; @@ -128,6 +109,11 @@ enum { /* An AF_INET socket (used for ioctl operations). */ static int af_inet_sock = -1; +#if defined(__NetBSD__) +/* AF_LINK socket used for netdev_bsd_get_stats and set_etheraddr */ +static int af_link_sock = -1; +#endif /* defined(__NetBSD__) */ + #define PCAP_SNAPLEN 2048 @@ -149,7 +135,7 @@ 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, @@ -159,6 +145,9 @@ 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 ifr_get_flags(const struct ifreq *); +static void ifr_set_flags(struct ifreq *, int flags); + static int netdev_bsd_init(void); static bool @@ -170,16 +159,8 @@ is_netdev_bsd_class(const struct netdev_class *netdev_class) 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); -} - -static struct netdev_dev_bsd * -netdev_dev_bsd_cast(const struct netdev_dev *netdev_dev) -{ - 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(netdev))); + return CONTAINER_OF(netdev, struct netdev_bsd, up); } static struct netdev_rx_bsd * @@ -189,6 +170,12 @@ netdev_rx_bsd_cast(const struct netdev_rx *rx) return CONTAINER_OF(rx, struct netdev_rx_bsd, up); } +static const char * +netdev_get_kernel_name(const struct netdev *netdev) +{ + return netdev_bsd_cast(netdev)->kernel_name; +} + /* Initialize the AF_INET socket used for ioctl operations */ static int netdev_bsd_init(void) @@ -201,11 +188,21 @@ netdev_bsd_init(void) 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; } +#if defined(__NetBSD__) + af_link_sock = socket(AF_LINK, SOCK_DGRAM, 0); + status = af_link_sock >= 0 ? 0 : errno; + if (status) { + VLOG_ERR("failed to create link socket: %s", strerror(status)); + close(af_inet_sock); + af_inet_sock = -1; + } +#endif /* defined(__NetBSD__) */ + return status; } @@ -230,7 +227,7 @@ netdev_bsd_wait(void) } static void -netdev_dev_bsd_changed(struct netdev_dev_bsd *dev) +netdev_bsd_changed(struct netdev_bsd *dev) { dev->change_seq++; if (!dev->change_seq) { @@ -243,19 +240,19 @@ 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_bsd_changed(dev); } } } else { @@ -267,11 +264,11 @@ 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; dev->cache_valid = 0; - netdev_dev_bsd_changed(dev); + netdev_bsd_changed(dev); } shash_destroy(&device_shash); } @@ -303,12 +300,13 @@ cache_notifier_unref(void) return 0; } -/* Allocate a netdev_dev_bsd structure */ +/* Allocate a netdev_bsd structure */ static int netdev_bsd_create_system(const struct netdev_class *class, const char *name, - struct netdev_dev **netdev_devp) + struct netdev **netdevp) { - struct netdev_dev_bsd *netdev_dev; + struct netdev_bsd *netdev; + enum netdev_flags flags; int error; error = cache_notifier_ref(); @@ -316,25 +314,36 @@ 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; + netdev = xzalloc(sizeof *netdev); + netdev->change_seq = 1; + netdev_init(&netdev->up, name, class); + netdev->tap_fd = -1; + netdev->kernel_name = xstrdup(name); + /* Verify that the netdev really exists by attempting to read its flags */ + error = netdev_get_flags(&netdev->up, &flags); + if (error == ENXIO) { + netdev_uninit(&netdev->up, false); + free(netdev); + cache_notifier_unref(); + return error; + } + + *netdevp = &netdev->up; return 0; } /* - * Allocate a netdev_dev_bsd structure with 'tap' class. + * Allocate a netdev_bsd structure with 'tap' class. */ static int netdev_bsd_create_tap(const struct netdev_class *class, const char *name, - struct netdev_dev **netdev_devp) + struct netdev **netdevp) { - struct netdev_dev_bsd *netdev_dev = NULL; + struct netdev_bsd *netdev = NULL; int error = 0; struct ifreq ifr; + char *kernel_name = NULL; error = cache_notifier_ref(); if (error) { @@ -342,116 +351,90 @@ netdev_bsd_create_tap(const struct netdev_class *class, const char *name, } /* allocate the device structure and set the internal flag */ - netdev_dev = xzalloc(sizeof *netdev_dev); + netdev = xzalloc(sizeof *netdev); 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) { + netdev->tap_fd = open("/dev/tap", O_RDWR); + netdev->change_seq = 1; + if (netdev->tap_fd < 0) { error = errno; VLOG_WARN("opening \"/dev/tap\" failed: %s", strerror(error)); goto error_undef_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; } /* 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); + destroy_tap(netdev->tap_fd, ifr.ifr_name); goto error_undef_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); + destroy_tap(netdev->tap_fd, kernel_name); goto error_undef_notifier; } /* Turn device UP */ - ifr.ifr_flags = (uint16_t)IFF_UP; - ifr.ifr_flagshigh = 0; - strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); + ifr_set_flags(&ifr, IFF_UP); + strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name); if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) { error = errno; - destroy_tap(netdev_dev->tap_fd, name); + destroy_tap(netdev->tap_fd, kernel_name); goto error_undef_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_init(&netdev->up, name, class); + netdev->kernel_name = kernel_name; + *netdevp = &netdev->up; return 0; error_undef_notifier: cache_notifier_unref(); error: - free(netdev_dev); + free(netdev); + free(kernel_name); return error; } static void -netdev_bsd_destroy(struct netdev_dev *netdev_dev_) +netdev_bsd_destroy(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_dev->pcap) { - pcap_close(netdev_dev->pcap); + if (netdev->tap_fd >= 0) { + destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_)); } - 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; + if (netdev->pcap) { + pcap_close(netdev->pcap); } - - *netdevp = &netdev->netdev; - return 0; - -error: - netdev_uninit(&netdev->netdev, true); - return error; -} - - - -/* Close a 'netdev'. */ -static void -netdev_bsd_close(struct netdev *netdev_) -{ - struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); - + free(netdev->kernel_name); free(netdev); } @@ -527,8 +510,7 @@ error: static int netdev_bsd_rx_open(struct netdev *netdev_, struct netdev_rx **rxp) { - struct netdev_dev_bsd *netdev_dev = - netdev_dev_bsd_cast(netdev_get_dev(netdev_)); + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); struct netdev_rx_bsd *rx; pcap_t *pcap; @@ -536,18 +518,19 @@ netdev_bsd_rx_open(struct netdev *netdev_, struct netdev_rx **rxp) if (!strcmp(netdev_get_type(netdev_), "tap")) { pcap = NULL; - fd = netdev_dev->tap_fd; + fd = netdev->tap_fd; } else { - int error = netdev_bsd_open_pcap(netdev_get_name(netdev_), &pcap, &fd); + int error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_), + &pcap, &fd); if (error) { return error; } - netdev_dev_bsd_changed(netdev_dev); + netdev_bsd_changed(netdev); } rx = xmalloc(sizeof *rx); - netdev_rx_init(&rx->up, netdev_get_dev(netdev_), &netdev_rx_bsd_class); + netdev_rx_init(&rx->up, netdev_, &netdev_rx_bsd_class); rx->pcap_handle = pcap; rx->fd = fd; @@ -691,7 +674,7 @@ netdev_rx_bsd_drain(struct netdev_rx *rx_) struct ifreq ifr; struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_); - strcpy(ifr.ifr_name, netdev_rx_get_name(rx_)); + strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rx_get_netdev(rx_))); if (ioctl(rx->fd, BIOCFLUSH, &ifr) == -1) { VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s", netdev_rx_get_name(rx_), strerror(errno)); @@ -707,7 +690,7 @@ netdev_rx_bsd_drain(struct netdev_rx *rx_) static int netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size) { - 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_); if (dev->tap_fd < 0 && !dev->pcap) { @@ -750,7 +733,7 @@ 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_); if (dev->tap_fd >= 0) { /* TAP device always accepts packets. */ @@ -771,18 +754,17 @@ 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_)); + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); int error; - 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); + 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_bsd_changed(netdev); } } else { error = 0; @@ -798,18 +780,17 @@ 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_)); + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); - if (!(netdev_dev->cache_valid & VALID_ETHERADDR)) { - int error = get_etheraddr(netdev_get_name(netdev_), - netdev_dev->etheraddr); + if (!(netdev->cache_valid & VALID_ETHERADDR)) { + int error = get_etheraddr(netdev_get_kernel_name(netdev_), + netdev->etheraddr); if (error) { return error; } - netdev_dev->cache_valid |= VALID_ETHERADDR; + netdev->cache_valid |= VALID_ETHERADDR; } - memcpy(mac, netdev_dev->etheraddr, ETH_ADDR_LEN); + memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN); return 0; } @@ -822,23 +803,22 @@ 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_); - if (!(netdev_dev->cache_valid & VALID_MTU)) { + if (!(netdev->cache_valid & VALID_MTU)) { struct ifreq ifr; int error; - error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr, SIOCGIFMTU, - "SIOCGIFMTU"); + error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr, + SIOCGIFMTU, "SIOCGIFMTU"); if (error) { return error; } - netdev_dev->mtu = ifr.ifr_mtu; - netdev_dev->cache_valid |= VALID_MTU; + netdev->mtu = ifr.ifr_mtu; + netdev->cache_valid |= VALID_MTU; } - *mtup = netdev_dev->mtu; + *mtup = netdev->mtu; return 0; } @@ -854,14 +834,14 @@ netdev_bsd_get_ifindex(const struct netdev *netdev) 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_); - if (!(netdev_dev->cache_valid & VALID_CARRIER)) { + 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); if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) { VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s", @@ -869,24 +849,54 @@ netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier) return errno; } - netdev_dev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE; - netdev_dev->cache_valid |= VALID_CARRIER; + netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE; + netdev->cache_valid |= VALID_CARRIER; /* 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; + netdev->carrier = true; } } - *carrier = netdev_dev->carrier; + *carrier = netdev->carrier; return 0; } +static void +convert_stats(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; +} + /* 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; @@ -917,34 +927,30 @@ netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats) netdev_get_name(netdev_), 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(stats, &ifmd.ifmd_data); break; } } return 0; +#elif defined(__NetBSD__) + struct ifdatareq ifdr; + int saved_errno; + int ret; + + memset(&ifdr, 0, sizeof(ifdr)); + strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_), + sizeof(ifdr.ifdr_name)); + ret = ioctl(af_link_sock, SIOCGIFDATA, &ifdr); + saved_errno = errno; + if (ret == -1) { + return saved_errno; + } + convert_stats(stats, &ifdr.ifdr_data); + return 0; +#else +#error not implemented +#endif } static uint32_t @@ -1097,32 +1103,31 @@ 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_); - if (!(netdev_dev->cache_valid & VALID_IN4)) { + if (!(netdev->cache_valid & VALID_IN4)) { const struct sockaddr_in *sin; struct ifreq ifr; int error; ifr.ifr_addr.sa_family = AF_INET; - error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr, + error = netdev_bsd_do_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, + netdev->in4 = sin->sin_addr; + netdev->cache_valid |= VALID_IN4; + error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr, SIOCGIFNETMASK, "SIOCGIFNETMASK"); if (error) { return error; } *netmask = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr; } - *in4 = netdev_dev->in4; + *in4 = netdev->in4; return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0; } @@ -1136,19 +1141,18 @@ 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; error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr); if (!error) { - netdev_dev->cache_valid |= VALID_IN4; - netdev_dev->in4 = addr; + netdev->cache_valid |= VALID_IN4; + netdev->in4 = addr; if (addr.s_addr != INADDR_ANY) { error = do_set_addr(netdev_, SIOCSIFNETMASK, "SIOCSIFNETMASK", mask); } - netdev_dev_bsd_changed(netdev_dev); + netdev_bsd_changed(netdev); } return error; } @@ -1156,9 +1160,8 @@ netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr, 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_); @@ -1174,9 +1177,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,10 +1187,154 @@ 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 struct netdev * +find_netdev_by_kernel_name(const char *kernel_name) +{ + 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_bsd * const dev = node->data; + + if (!strcmp(dev->kernel_name, kernel_name)) { + shash_destroy(&device_shash); + return &dev->up; + } + } + shash_destroy(&device_shash); + return NULL; +} + +static const char * +netdev_bsd_convert_kernel_name_to_ovs_name(const char *kernel_name) +{ + const struct netdev * const netdev = + find_netdev_by_kernel_name(kernel_name); + + if (netdev == NULL) { + return NULL; + } + return netdev_get_name(netdev); +} +#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; + const size_t nlen = sdl->sdl_nlen; + char * const kernel_name = xmalloc(nlen + 1); + const char *name; + + memcpy(kernel_name, sdl->sdl_data, nlen); + kernel_name[nlen] = 0; + name = netdev_bsd_convert_kernel_name_to_ovs_name(kernel_name); + if (name == NULL) { + ifname = xstrdup(kernel_name); + } else { + ifname = xstrdup(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 void make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr) { @@ -1207,7 +1354,8 @@ do_set_addr(struct netdev *netdev, { struct ifreq ifr; make_in4_sockaddr(&ifr.ifr_addr, addr); - return netdev_bsd_do_ioctl(netdev, &ifr, ioctl_nr, ioctl_name); + return netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr, + ioctl_name); } static int @@ -1219,7 +1367,9 @@ nd_to_iff_flags(enum netdev_flags nd) } if (nd & NETDEV_PROMISC) { iff |= IFF_PROMISC; +#if defined(IFF_PPROMISC) iff |= IFF_PPROMISC; +#endif } return iff; } @@ -1238,21 +1388,20 @@ iff_to_nd_flags(int iff) } 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; + struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); 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_bsd_changed(netdev); } } return error; @@ -1261,7 +1410,7 @@ netdev_bsd_update_flags(struct netdev_dev *dev_, enum netdev_flags off, static unsigned int netdev_bsd_change_seq(const struct netdev *netdev) { - return netdev_dev_bsd_cast(netdev_get_dev(netdev))->change_seq; + return netdev_bsd_cast(netdev)->change_seq; } @@ -1276,8 +1425,6 @@ const struct netdev_class netdev_bsd_class = { NULL, /* get_config */ NULL, /* set_config */ NULL, /* get_tunnel_config */ - netdev_bsd_open_system, - netdev_bsd_close, netdev_bsd_rx_open, @@ -1313,7 +1460,7 @@ const struct netdev_class netdev_bsd_class = { netdev_bsd_set_in4, netdev_bsd_get_in6, NULL, /* add_router */ - NULL, /* get_next_hop */ + netdev_bsd_get_next_hop, NULL, /* get_status */ NULL, /* arp_lookup */ @@ -1333,8 +1480,6 @@ const struct netdev_class netdev_tap_class = { NULL, /* get_config */ NULL, /* set_config */ NULL, /* get_tunnel_config */ - netdev_bsd_open_system, - netdev_bsd_close, netdev_bsd_rx_open, @@ -1370,7 +1515,7 @@ const struct netdev_class netdev_tap_class = { netdev_bsd_set_in4, netdev_bsd_get_in6, NULL, /* add_router */ - NULL, /* get_next_hop */ + netdev_bsd_get_next_hop, NULL, /* get_status */ NULL, /* arp_lookup */ @@ -1399,15 +1544,15 @@ destroy_tap(int fd, const char *name) } 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 = netdev_bsd_do_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,8 +1562,7 @@ 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"); } @@ -1426,18 +1570,17 @@ set_flags(const char *name, int flags) 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; } @@ -1473,9 +1616,11 @@ 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; memset(&ifr, 0, sizeof ifr); @@ -1489,6 +1634,59 @@ set_etheraddr(const char *netdev_name, int hwaddr_family, return errno; } return 0; +#elif defined(__NetBSD__) + struct if_laddrreq req; + struct sockaddr_dl *sdl; + struct sockaddr_storage oldaddr; + int ret; + + /* + * 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; + ret = ioctl(af_link_sock, SIOCGLIFADDR, &req); + if (ret == -1) { + return errno; + } + 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); + ret = ioctl(af_link_sock, SIOCALIFADDR, &req); + if (ret == -1) { + return errno; + } + + memset(&req, 0, sizeof(req)); + strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name)); + req.addr = oldaddr; + ret = ioctl(af_link_sock, SIOCDLIFADDR, &req); + if (ret == -1) { + return errno; + } + return 0; +#else +#error not implemented +#endif } static int @@ -1503,3 +1701,22 @@ netdev_bsd_do_ioctl(const char *name, struct ifreq *ifr, unsigned long cmd, } return 0; } + +static int +ifr_get_flags(const struct ifreq *ifr) +{ +#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 +}