2 * Copyright (c) 2011, 2013, 2014 Gaetano Catalli.
3 * Copyright (c) 2013, 2014 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>
51 #include "dpif-netdev.h"
52 #include "dynamic-string.h"
53 #include "fatal-signal.h"
55 #include "openflow/openflow.h"
56 #include "ovs-thread.h"
57 #include "packet-dpif.h"
59 #include "poll-loop.h"
61 #include "socket-util.h"
66 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
69 struct netdev_rxq_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);
154 static int netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup);
157 is_netdev_bsd_class(const struct netdev_class *netdev_class)
159 return netdev_class->run == netdev_bsd_run;
162 static struct netdev_bsd *
163 netdev_bsd_cast(const struct netdev *netdev)
165 ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev)));
166 return CONTAINER_OF(netdev, struct netdev_bsd, up);
169 static struct netdev_rxq_bsd *
170 netdev_rxq_bsd_cast(const struct netdev_rxq *rxq)
172 ovs_assert(is_netdev_bsd_class(netdev_get_class(rxq->netdev)));
173 return CONTAINER_OF(rxq, struct netdev_rxq_bsd, up);
177 netdev_get_kernel_name(const struct netdev *netdev)
179 return netdev_bsd_cast(netdev)->kernel_name;
183 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
184 * interface status changes, and eventually calls all the user callbacks.
189 rtbsd_notifier_run();
193 * Arranges for poll_block() to wake up if the "run" member function needs to
197 netdev_bsd_wait(void)
199 rtbsd_notifier_wait();
202 /* Invalidate cache in case of interface status change. */
204 netdev_bsd_cache_cb(const struct rtbsd_change *change,
205 void *aux OVS_UNUSED)
207 struct netdev_bsd *dev;
210 struct netdev *base_dev = netdev_from_name(change->if_name);
213 const struct netdev_class *netdev_class =
214 netdev_get_class(base_dev);
216 if (is_netdev_bsd_class(netdev_class)) {
217 dev = netdev_bsd_cast(base_dev);
218 dev->cache_valid = 0;
219 netdev_change_seq_changed(base_dev);
221 netdev_close(base_dev);
225 * XXX the API is lacking, we should be able to iterate on the list of
226 * netdevs without having to store the info in a temp shash.
228 struct shash device_shash;
229 struct shash_node *node;
231 shash_init(&device_shash);
232 netdev_get_devices(&netdev_bsd_class, &device_shash);
233 SHASH_FOR_EACH (node, &device_shash) {
234 struct netdev *netdev = node->data;
235 dev = netdev_bsd_cast(netdev);
236 dev->cache_valid = 0;
237 netdev_change_seq_changed(netdev);
238 netdev_close(netdev);
240 shash_destroy(&device_shash);
245 cache_notifier_ref(void)
249 if (!cache_notifier_refcount) {
250 ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
251 netdev_bsd_cache_cb, NULL);
256 cache_notifier_refcount++;
261 cache_notifier_unref(void)
263 cache_notifier_refcount--;
264 if (cache_notifier_refcount == 0) {
265 rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
270 static struct netdev *
271 netdev_bsd_alloc(void)
273 struct netdev_bsd *netdev = xzalloc(sizeof *netdev);
278 netdev_bsd_construct_system(struct netdev *netdev_)
280 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
281 enum netdev_flags flags;
284 error = cache_notifier_ref();
289 ovs_mutex_init(&netdev->mutex);
291 netdev->kernel_name = xstrdup(netdev_->name);
293 /* Verify that the netdev really exists by attempting to read its flags */
294 error = netdev_get_flags(netdev_, &flags);
295 if (error == ENXIO) {
296 free(netdev->kernel_name);
297 cache_notifier_unref();
305 netdev_bsd_construct_tap(struct netdev *netdev_)
307 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
308 const char *name = netdev_->name;
311 char *kernel_name = NULL;
313 error = cache_notifier_ref();
318 memset(&ifr, 0, sizeof(ifr));
320 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
321 * to retrieve the name of the tap device. */
322 ovs_mutex_init(&netdev->mutex);
323 netdev->tap_fd = open("/dev/tap", O_RDWR);
324 if (netdev->tap_fd < 0) {
326 VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error));
327 goto error_unref_notifier;
330 /* Retrieve tap name (e.g. tap0) */
331 if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
332 /* XXX Need to destroy the device? */
334 close(netdev->tap_fd);
335 goto error_unref_notifier;
338 /* Change the name of the tap device */
339 #if defined(SIOCSIFNAME)
340 ifr.ifr_data = (void *)name;
341 error = af_inet_ioctl(SIOCSIFNAME, &ifr);
343 destroy_tap(netdev->tap_fd, ifr.ifr_name);
344 goto error_unref_notifier;
346 kernel_name = xstrdup(name);
349 * NetBSD doesn't support inteface renaming.
351 VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name);
352 kernel_name = xstrdup(ifr.ifr_name);
355 /* set non-blocking. */
356 error = set_nonblocking(netdev->tap_fd);
358 destroy_tap(netdev->tap_fd, kernel_name);
359 goto error_unref_notifier;
363 ifr_set_flags(&ifr, IFF_UP);
364 strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
365 error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
367 destroy_tap(netdev->tap_fd, kernel_name);
368 goto error_unref_notifier;
371 netdev->kernel_name = kernel_name;
375 error_unref_notifier:
376 ovs_mutex_destroy(&netdev->mutex);
377 cache_notifier_unref();
384 netdev_bsd_destruct(struct netdev *netdev_)
386 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
388 cache_notifier_unref();
390 if (netdev->tap_fd >= 0) {
391 destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_));
394 pcap_close(netdev->pcap);
396 free(netdev->kernel_name);
397 ovs_mutex_destroy(&netdev->mutex);
401 netdev_bsd_dealloc(struct netdev *netdev_)
403 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
409 netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
411 char errbuf[PCAP_ERRBUF_SIZE];
417 /* Open the pcap device. The device is opened in non-promiscuous mode
418 * because the interface flags are manually set by the caller. */
420 pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
422 VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
426 if (errbuf[0] != '\0') {
427 VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
430 /* Get the underlying fd. */
431 fd = pcap_get_selectable_fd(pcap);
433 VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
438 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
439 * on the file descriptor returned by pcap_get_selectable_fd to achieve
440 * a real non-blocking behaviour.*/
441 error = pcap_setnonblock(pcap, 1, errbuf);
447 /* This call assure that reads return immediately upon packet
448 * reception. Otherwise, a read will block until either the kernel
449 * buffer becomes full or a timeout occurs. */
450 if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
451 VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
452 name, ovs_strerror(errno));
457 /* Capture only incoming packets. */
458 error = pcap_setdirection(pcap, PCAP_D_IN);
477 static struct netdev_rxq *
478 netdev_bsd_rxq_alloc(void)
480 struct netdev_rxq_bsd *rxq = xzalloc(sizeof *rxq);
485 netdev_bsd_rxq_construct(struct netdev_rxq *rxq_)
487 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
488 struct netdev *netdev_ = rxq->up.netdev;
489 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
492 if (!strcmp(netdev_get_type(netdev_), "tap")) {
493 rxq->pcap_handle = NULL;
494 rxq->fd = netdev->tap_fd;
497 ovs_mutex_lock(&netdev->mutex);
498 error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
499 &rxq->pcap_handle, &rxq->fd);
500 ovs_mutex_unlock(&netdev->mutex);
507 netdev_bsd_rxq_destruct(struct netdev_rxq *rxq_)
509 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
511 if (rxq->pcap_handle) {
512 pcap_close(rxq->pcap_handle);
517 netdev_bsd_rxq_dealloc(struct netdev_rxq *rxq_)
519 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
524 /* The recv callback of the netdev class returns the number of bytes of the
527 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
528 * not make difference between a missing packet on the capture interface and
529 * an error during the file capture. We can use the pcap_dispatch() function
530 * instead, which is able to distinguish between errors and null packet.
532 * To make pcap_dispatch() returns the number of bytes read from the interface
533 * we need to define the following callback and argument.
542 * This callback will be executed on every captured packet.
544 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
545 * pcap returns a truncated packet and we follow this behavior.
547 * The argument args->retval is the packet size in bytes.
550 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
552 struct pcap_arg *args = ALIGNED_CAST(struct pcap_arg *, args_);
554 if (args->size < hdr->len) {
555 VLOG_WARN_RL(&rl, "packet truncated");
556 args->retval = args->size;
558 args->retval = hdr->len;
561 /* copy the packet to our buffer */
562 memcpy(args->data, packet, args->retval);
566 * This function attempts to receive a packet from the specified network
567 * device. It is assumed that the network device is a system device or a tap
568 * device opened as a system one. In this case the read operation is performed
572 netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
577 /* prepare the pcap argument to store the packet */
578 arg.size = ofpbuf_tailroom(buffer);
579 arg.data = ofpbuf_data(buffer);
582 ret = pcap_dispatch(rxq->pcap_handle, 1, proc_pkt, (u_char *) &arg);
585 ofpbuf_set_size(buffer, ofpbuf_size(buffer) + arg.retval);
589 if (errno == EINTR) {
599 * This function attempts to receive a packet from the specified network
600 * device. It is assumed that the network device is a tap device and
601 * 'rxq->fd' is initialized with the tap file descriptor.
604 netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
606 size_t size = ofpbuf_tailroom(buffer);
609 ssize_t retval = read(rxq->fd, ofpbuf_data(buffer), size);
611 ofpbuf_set_size(buffer, ofpbuf_size(buffer) + retval);
613 } else if (errno != EINTR) {
614 if (errno != EAGAIN) {
615 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
616 ovs_strerror(errno), netdev_rxq_get_name(&rxq->up));
624 netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **packets,
627 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
628 struct netdev *netdev = rxq->up.netdev;
629 struct dpif_packet *packet;
630 struct ofpbuf *buffer;
634 if (netdev_bsd_get_mtu(netdev, &mtu)) {
635 mtu = ETH_PAYLOAD_MAX;
638 packet = dpif_packet_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu,
640 buffer = &packet->ofpbuf;
642 retval = (rxq->pcap_handle
643 ? netdev_rxq_bsd_recv_pcap(rxq, buffer)
644 : netdev_rxq_bsd_recv_tap(rxq, buffer));
647 dpif_packet_delete(packet);
649 dp_packet_pad(buffer);
657 * Registers with the poll loop to wake up from the next call to poll_block()
658 * when a packet is ready to be received with netdev_rxq_recv() on 'rxq'.
661 netdev_bsd_rxq_wait(struct netdev_rxq *rxq_)
663 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
665 poll_fd_wait(rxq->fd, POLLIN);
668 /* Discards all packets waiting to be received from 'rxq'. */
670 netdev_bsd_rxq_drain(struct netdev_rxq *rxq_)
673 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
675 strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rxq_get_netdev(rxq_)));
676 if (ioctl(rxq->fd, BIOCFLUSH, &ifr) == -1) {
677 VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
678 netdev_rxq_get_name(rxq_), ovs_strerror(errno));
685 * Send a packet on the specified network device. The device could be either a
686 * system or a tap device.
689 netdev_bsd_send(struct netdev *netdev_, struct dpif_packet **pkts, int cnt,
692 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
693 const char *name = netdev_get_name(netdev_);
697 ovs_mutex_lock(&dev->mutex);
698 if (dev->tap_fd < 0 && !dev->pcap) {
699 error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
704 for (i = 0; i < cnt; i++) {
705 const void *data = ofpbuf_data(&pkts[i]->ofpbuf);
706 size_t size = ofpbuf_size(&pkts[i]->ofpbuf);
710 if (dev->tap_fd >= 0) {
711 retval = write(dev->tap_fd, data, size);
713 retval = pcap_inject(dev->pcap, data, size);
716 if (errno == EINTR) {
720 if (error != EAGAIN) {
721 VLOG_WARN_RL(&rl, "error sending Ethernet packet on"
722 " %s: %s", name, ovs_strerror(error));
725 } else if (retval != size) {
726 VLOG_WARN_RL(&rl, "sent partial Ethernet packet "
727 "(%"PRIuSIZE" bytes of "
728 "%"PRIuSIZE") on %s", retval, size, name);
736 ovs_mutex_unlock(&dev->mutex);
738 for (i = 0; i < cnt; i++) {
739 dpif_packet_delete(pkts[i]);
747 * Registers with the poll loop to wake up from the next call to poll_block()
748 * when the packet transmission queue has sufficient room to transmit a packet
749 * with netdev_send().
752 netdev_bsd_send_wait(struct netdev *netdev_)
754 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
756 ovs_mutex_lock(&dev->mutex);
757 if (dev->tap_fd >= 0) {
758 /* TAP device always accepts packets. */
759 poll_immediate_wake();
760 } else if (dev->pcap) {
761 poll_fd_wait(dev->fd, POLLOUT);
763 /* We haven't even tried to send a packet yet. */
764 poll_immediate_wake();
766 ovs_mutex_unlock(&dev->mutex);
770 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
771 * otherwise a positive errno value.
774 netdev_bsd_set_etheraddr(struct netdev *netdev_,
775 const uint8_t mac[ETH_ADDR_LEN])
777 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
780 ovs_mutex_lock(&netdev->mutex);
781 if (!(netdev->cache_valid & VALID_ETHERADDR)
782 || !eth_addr_equals(netdev->etheraddr, mac)) {
783 error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
786 netdev->cache_valid |= VALID_ETHERADDR;
787 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
788 netdev_change_seq_changed(netdev_);
791 ovs_mutex_unlock(&netdev->mutex);
797 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
798 * free the returned buffer.
801 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
802 uint8_t mac[ETH_ADDR_LEN])
804 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
807 ovs_mutex_lock(&netdev->mutex);
808 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
809 error = get_etheraddr(netdev_get_kernel_name(netdev_),
812 netdev->cache_valid |= VALID_ETHERADDR;
816 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
818 ovs_mutex_unlock(&netdev->mutex);
824 * Returns the maximum size of transmitted (and received) packets on 'netdev',
825 * in bytes, not including the hardware header; thus, this is typically 1500
826 * bytes for Ethernet devices.
829 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
831 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
834 ovs_mutex_lock(&netdev->mutex);
835 if (!(netdev->cache_valid & VALID_MTU)) {
838 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
839 SIOCGIFMTU, "SIOCGIFMTU");
841 netdev->mtu = ifr.ifr_mtu;
842 netdev->cache_valid |= VALID_MTU;
848 ovs_mutex_unlock(&netdev->mutex);
854 netdev_bsd_get_ifindex(const struct netdev *netdev_)
856 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
859 ovs_mutex_lock(&netdev->mutex);
860 error = get_ifindex(netdev_, &ifindex);
861 ovs_mutex_unlock(&netdev->mutex);
863 return error ? -error : ifindex;
867 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
869 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
872 ovs_mutex_lock(&netdev->mutex);
873 if (!(netdev->cache_valid & VALID_CARRIER)) {
874 struct ifmediareq ifmr;
876 memset(&ifmr, 0, sizeof(ifmr));
877 strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
878 sizeof ifmr.ifm_name);
880 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
882 netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
883 netdev->cache_valid |= VALID_CARRIER;
885 /* If the interface doesn't report whether the media is active,
886 * just assume it is active. */
887 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
888 netdev->carrier = true;
891 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
892 netdev_get_name(netdev_), ovs_strerror(error));
896 *carrier = netdev->carrier;
898 ovs_mutex_unlock(&netdev->mutex);
904 convert_stats_system(struct netdev_stats *stats, const struct if_data *ifd)
907 * note: UINT64_MAX means unsupported
909 stats->rx_packets = ifd->ifi_ipackets;
910 stats->tx_packets = ifd->ifi_opackets;
911 stats->rx_bytes = ifd->ifi_obytes;
912 stats->tx_bytes = ifd->ifi_ibytes;
913 stats->rx_errors = ifd->ifi_ierrors;
914 stats->tx_errors = ifd->ifi_oerrors;
915 stats->rx_dropped = ifd->ifi_iqdrops;
916 stats->tx_dropped = UINT64_MAX;
917 stats->multicast = ifd->ifi_imcasts;
918 stats->collisions = ifd->ifi_collisions;
919 stats->rx_length_errors = UINT64_MAX;
920 stats->rx_over_errors = UINT64_MAX;
921 stats->rx_crc_errors = UINT64_MAX;
922 stats->rx_frame_errors = UINT64_MAX;
923 stats->rx_fifo_errors = UINT64_MAX;
924 stats->rx_missed_errors = UINT64_MAX;
925 stats->tx_aborted_errors = UINT64_MAX;
926 stats->tx_carrier_errors = UINT64_MAX;
927 stats->tx_fifo_errors = UINT64_MAX;
928 stats->tx_heartbeat_errors = UINT64_MAX;
929 stats->tx_window_errors = UINT64_MAX;
933 convert_stats_tap(struct netdev_stats *stats, const struct if_data *ifd)
936 * Similar to convert_stats_system but swapping rxq and tx
937 * because 'ifd' is stats for the network interface side of the
938 * tap device and what the caller wants is one for the character
941 * note: UINT64_MAX means unsupported
943 stats->rx_packets = ifd->ifi_opackets;
944 stats->tx_packets = ifd->ifi_ipackets;
945 stats->rx_bytes = ifd->ifi_ibytes;
946 stats->tx_bytes = ifd->ifi_obytes;
947 stats->rx_errors = ifd->ifi_oerrors;
948 stats->tx_errors = ifd->ifi_ierrors;
949 stats->rx_dropped = UINT64_MAX;
950 stats->tx_dropped = ifd->ifi_iqdrops;
951 stats->multicast = ifd->ifi_omcasts;
952 stats->collisions = UINT64_MAX;
953 stats->rx_length_errors = UINT64_MAX;
954 stats->rx_over_errors = UINT64_MAX;
955 stats->rx_crc_errors = UINT64_MAX;
956 stats->rx_frame_errors = UINT64_MAX;
957 stats->rx_fifo_errors = UINT64_MAX;
958 stats->rx_missed_errors = UINT64_MAX;
959 stats->tx_aborted_errors = UINT64_MAX;
960 stats->tx_carrier_errors = UINT64_MAX;
961 stats->tx_fifo_errors = UINT64_MAX;
962 stats->tx_heartbeat_errors = UINT64_MAX;
963 stats->tx_window_errors = UINT64_MAX;
967 convert_stats(const struct netdev *netdev, struct netdev_stats *stats,
968 const struct if_data *ifd)
970 if (netdev_bsd_cast(netdev)->tap_fd == -1) {
971 convert_stats_system(stats, ifd);
973 convert_stats_tap(stats, ifd);
977 /* Retrieves current device stats for 'netdev'. */
979 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
981 #if defined(__FreeBSD__)
985 struct ifmibdata ifmd;
990 mib[2] = NETLINK_GENERIC;
991 mib[3] = IFMIB_SYSTEM;
992 mib[4] = IFMIB_IFCOUNT;
994 len = sizeof(if_count);
996 if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
997 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
998 netdev_get_name(netdev_), ovs_strerror(errno));
1002 mib[5] = IFDATA_GENERAL;
1003 mib[3] = IFMIB_IFDATA;
1005 for (i = 1; i <= if_count; i++) {
1007 if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
1008 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
1009 netdev_get_name(netdev_), ovs_strerror(errno));
1011 } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
1012 convert_stats(netdev_, stats, &ifmd.ifmd_data);
1018 #elif defined(__NetBSD__)
1019 struct ifdatareq ifdr;
1022 memset(&ifdr, 0, sizeof(ifdr));
1023 strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
1024 sizeof(ifdr.ifdr_name));
1025 error = af_link_ioctl(SIOCGIFDATA, &ifdr);
1027 convert_stats(netdev_, stats, &ifdr.ifdr_data);
1031 #error not implemented
1036 netdev_bsd_parse_media(int media)
1038 uint32_t supported = 0;
1039 bool half_duplex = media & IFM_HDX ? true : false;
1041 switch (IFM_SUBTYPE(media)) {
1046 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
1047 supported |= NETDEV_F_COPPER;
1051 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
1052 supported |= NETDEV_F_FIBER;
1059 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
1060 supported |= NETDEV_F_COPPER;
1064 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
1065 supported |= NETDEV_F_FIBER;
1070 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
1071 supported |= NETDEV_F_COPPER;
1076 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
1077 supported |= NETDEV_F_FIBER;
1081 supported |= NETDEV_F_10GB_FD;
1082 supported |= NETDEV_F_COPPER;
1087 supported |= NETDEV_F_10GB_FD;
1088 supported |= NETDEV_F_FIBER;
1095 if (IFM_SUBTYPE(media) == IFM_AUTO) {
1096 supported |= NETDEV_F_AUTONEG;
1099 if (media & IFM_ETH_FMASK) {
1100 supported |= NETDEV_F_PAUSE;
1108 * Stores the features supported by 'netdev' into each of '*current',
1109 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1110 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1111 * successful, otherwise a positive errno value. On failure, all of the
1112 * passed-in values are set to 0.
1115 netdev_bsd_get_features(const struct netdev *netdev,
1116 enum netdev_features *current, uint32_t *advertised,
1117 enum netdev_features *supported, uint32_t *peer)
1119 struct ifmediareq ifmr;
1125 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1127 memset(&ifmr, 0, sizeof(ifmr));
1128 strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1130 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
1131 * number of supported modes, and a second with a buffer to retrieve
1133 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1135 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1136 netdev_get_name(netdev), ovs_strerror(error));
1140 media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1141 ifmr.ifm_ulist = media_list;
1143 if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
1144 VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1145 netdev_get_name(netdev));
1150 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1152 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1153 netdev_get_name(netdev), ovs_strerror(error));
1157 /* Current settings. */
1158 *current = netdev_bsd_parse_media(ifmr.ifm_active);
1160 /* Advertised features. */
1161 *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1163 /* Supported features. */
1165 for (i = 0; i < ifmr.ifm_count; i++) {
1166 *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1169 /* Peer advertisements. */
1170 *peer = 0; /* XXX */
1179 * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
1180 * '*netmask' to its netmask and returns true. Otherwise, returns false.
1183 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1184 struct in_addr *netmask)
1186 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1189 ovs_mutex_lock(&netdev->mutex);
1190 if (!(netdev->cache_valid & VALID_IN4)) {
1193 ifr.ifr_addr.sa_family = AF_INET;
1194 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1195 SIOCGIFADDR, "SIOCGIFADDR");
1197 const struct sockaddr_in *sin;
1199 sin = ALIGNED_CAST(struct sockaddr_in *, &ifr.ifr_addr);
1200 netdev->in4 = sin->sin_addr;
1201 netdev->cache_valid |= VALID_IN4;
1202 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1203 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1205 *netmask = sin->sin_addr;
1211 *netmask = netdev->netmask;
1213 ovs_mutex_unlock(&netdev->mutex);
1215 return error ? error : in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1219 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1220 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1221 * positive errno value.
1224 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1225 struct in_addr mask)
1227 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1230 ovs_mutex_lock(&netdev->mutex);
1231 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1233 if (addr.s_addr != INADDR_ANY) {
1234 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1235 "SIOCSIFNETMASK", mask);
1237 netdev->cache_valid |= VALID_IN4;
1239 netdev->netmask = mask;
1242 netdev_change_seq_changed(netdev_);
1244 ovs_mutex_unlock(&netdev->mutex);
1250 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1252 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1253 if (!(netdev->cache_valid & VALID_IN6)) {
1254 struct ifaddrs *ifa, *head;
1255 struct sockaddr_in6 *sin6;
1256 const char *netdev_name = netdev_get_name(netdev_);
1258 if (getifaddrs(&head) != 0) {
1259 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1260 ovs_strerror(errno));
1264 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1265 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1266 !strcmp(ifa->ifa_name, netdev_name)) {
1267 sin6 = ALIGNED_CAST(struct sockaddr_in6 *, ifa->ifa_addr);
1269 memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
1270 netdev->cache_valid |= VALID_IN6;
1277 return EADDRNOTAVAIL;
1283 #if defined(__NetBSD__)
1285 netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name)
1287 char *ovs_name = NULL;
1288 struct shash device_shash;
1289 struct shash_node *node;
1291 shash_init(&device_shash);
1292 netdev_get_devices(&netdev_tap_class, &device_shash);
1293 SHASH_FOR_EACH(node, &device_shash) {
1294 struct netdev *netdev = node->data;
1295 struct netdev_bsd * const dev = netdev_bsd_cast(netdev);
1297 if (!strcmp(dev->kernel_name, kernel_name)) {
1299 ovs_name = xstrdup(netdev_get_name(&dev->up));
1301 netdev_close(netdev);
1303 shash_destroy(&device_shash);
1305 return ovs_name ? ovs_name : xstrdup(kernel_name);
1310 netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
1311 struct in_addr *next_hop OVS_UNUSED,
1312 char **netdev_name OVS_UNUSED)
1314 #if defined(__NetBSD__)
1316 struct sockaddr_in sin;
1317 struct sockaddr_dl sdl;
1324 struct rt_msghdr *rtm = &buf.h;
1325 const pid_t pid = getpid();
1328 bool gateway = false;
1329 char *ifname = NULL;
1332 memset(next_hop, 0, sizeof(*next_hop));
1333 *netdev_name = NULL;
1335 memset(&sin, 0, sizeof(sin));
1336 sin.sin_len = sizeof(sin);
1337 sin.sin_family = AF_INET;
1339 sin.sin_addr = *host;
1341 memset(&sdl, 0, sizeof(sdl));
1342 sdl.sdl_len = sizeof(sdl);
1343 sdl.sdl_family = AF_LINK;
1345 s = socket(PF_ROUTE, SOCK_RAW, 0);
1346 memset(&buf, 0, sizeof(buf));
1347 rtm->rtm_flags = RTF_HOST|RTF_UP;
1348 rtm->rtm_version = RTM_VERSION;
1349 rtm->rtm_addrs = RTA_DST|RTA_IFP;
1350 cp = (void *)&buf.space;
1351 memcpy(cp, &sin, sizeof(sin));
1352 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin);
1353 memcpy(cp, &sdl, sizeof(sdl));
1354 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl);
1355 rtm->rtm_msglen = cp - (char *)(void *)rtm;
1356 rtm->rtm_seq = ++seq;
1357 rtm->rtm_type = RTM_GET;
1359 write(s, rtm, rtm->rtm_msglen);
1360 memset(&buf, 0, sizeof(buf));
1362 ssz = read(s, &buf, sizeof(buf));
1363 } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
1364 saved_errno = errno;
1370 return EPIPE; /* XXX */
1372 cp = (void *)&buf.space;
1373 for (i = 1; i; i <<= 1) {
1374 if ((rtm->rtm_addrs & i) != 0) {
1375 const struct sockaddr *sa = (const void *)cp;
1377 if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) {
1378 const struct sockaddr_in * const sin =
1379 ALIGNED_CAST(const struct sockaddr_in *, sa);
1381 *next_hop = sin->sin_addr;
1384 if ((i == RTA_IFP) && sa->sa_family == AF_LINK) {
1385 const struct sockaddr_dl * const sdl =
1386 ALIGNED_CAST(const struct sockaddr_dl *, sa);
1389 kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen);
1390 ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name);
1396 if (ifname == NULL) {
1402 *netdev_name = ifname;
1403 VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s",
1404 IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name);
1412 netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED,
1413 ovs_be32 ip OVS_UNUSED,
1414 uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1416 #if defined(__NetBSD__)
1417 const struct rt_msghdr *rtm;
1430 mib[4] = NET_RT_FLAGS;
1431 mib[5] = RTF_LLINFO;
1432 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
1436 buf = xmalloc(needed);
1437 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
1442 for (cp = buf; cp < ep; cp += rtm->rtm_msglen) {
1443 const struct sockaddr_inarp *sina;
1444 const struct sockaddr_dl *sdl;
1446 rtm = (const void *)cp;
1447 sina = (const void *)(rtm + 1);
1448 if (ip != sina->sin_addr.s_addr) {
1451 sdl = (const void *)
1452 ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len));
1453 if (sdl->sdl_alen == ETH_ADDR_LEN) {
1454 memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN);
1469 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1471 struct sockaddr_in sin;
1472 memset(&sin, 0, sizeof sin);
1473 sin.sin_family = AF_INET;
1474 sin.sin_addr = addr;
1477 memset(sa, 0, sizeof *sa);
1478 memcpy(sa, &sin, sizeof sin);
1482 do_set_addr(struct netdev *netdev,
1483 unsigned long ioctl_nr, const char *ioctl_name,
1484 struct in_addr addr)
1487 make_in4_sockaddr(&ifr.ifr_addr, addr);
1488 return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
1493 nd_to_iff_flags(enum netdev_flags nd)
1496 if (nd & NETDEV_UP) {
1499 if (nd & NETDEV_PROMISC) {
1501 #if defined(IFF_PPROMISC)
1502 iff |= IFF_PPROMISC;
1505 if (nd & NETDEV_LOOPBACK) {
1506 iff |= IFF_LOOPBACK;
1512 iff_to_nd_flags(int iff)
1514 enum netdev_flags nd = 0;
1518 if (iff & IFF_PROMISC) {
1519 nd |= NETDEV_PROMISC;
1521 if (iff & IFF_LOOPBACK) {
1522 nd |= NETDEV_LOOPBACK;
1528 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1529 enum netdev_flags on, enum netdev_flags *old_flagsp)
1531 int old_flags, new_flags;
1534 error = get_flags(netdev_, &old_flags);
1536 *old_flagsp = iff_to_nd_flags(old_flags);
1537 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1538 if (new_flags != old_flags) {
1539 error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
1540 netdev_change_seq_changed(netdev_);
1546 /* Linux has also different GET_STATS, SET_STATS,
1549 #define NETDEV_BSD_CLASS(NAME, CONSTRUCT, \
1559 netdev_bsd_destruct, \
1560 netdev_bsd_dealloc, \
1561 NULL, /* get_config */ \
1562 NULL, /* set_config */ \
1563 NULL, /* get_tunnel_config */ \
1566 netdev_bsd_send_wait, \
1568 netdev_bsd_set_etheraddr, \
1569 netdev_bsd_get_etheraddr, \
1570 netdev_bsd_get_mtu, \
1571 NULL, /* set_mtu */ \
1572 netdev_bsd_get_ifindex, \
1573 netdev_bsd_get_carrier, \
1574 NULL, /* get_carrier_resets */ \
1575 NULL, /* set_miimon_interval */ \
1576 netdev_bsd_get_stats, \
1577 NULL, /* set_stats */ \
1580 NULL, /* set_advertisement */ \
1581 NULL, /* set_policing */ \
1582 NULL, /* get_qos_type */ \
1583 NULL, /* get_qos_capabilities */ \
1584 NULL, /* get_qos */ \
1585 NULL, /* set_qos */ \
1586 NULL, /* get_queue */ \
1587 NULL, /* set_queue */ \
1588 NULL, /* delete_queue */ \
1589 NULL, /* get_queue_stats */ \
1590 NULL, /* queue_dump_start */ \
1591 NULL, /* queue_dump_next */ \
1592 NULL, /* queue_dump_done */ \
1593 NULL, /* dump_queue_stats */ \
1595 netdev_bsd_get_in4, \
1596 netdev_bsd_set_in4, \
1597 netdev_bsd_get_in6, \
1598 NULL, /* add_router */ \
1599 netdev_bsd_get_next_hop, \
1600 NULL, /* get_status */ \
1601 netdev_bsd_arp_lookup, /* arp_lookup */ \
1603 netdev_bsd_update_flags, \
1605 netdev_bsd_rxq_alloc, \
1606 netdev_bsd_rxq_construct, \
1607 netdev_bsd_rxq_destruct, \
1608 netdev_bsd_rxq_dealloc, \
1609 netdev_bsd_rxq_recv, \
1610 netdev_bsd_rxq_wait, \
1611 netdev_bsd_rxq_drain, \
1614 const struct netdev_class netdev_bsd_class =
1617 netdev_bsd_construct_system,
1618 netdev_bsd_get_features);
1620 const struct netdev_class netdev_tap_class =
1623 netdev_bsd_construct_tap,
1624 netdev_bsd_get_features);
1628 destroy_tap(int fd, const char *name)
1633 strcpy(ifr.ifr_name, name);
1634 /* XXX What to do if this call fails? */
1635 af_inet_ioctl(SIOCIFDESTROY, &ifr);
1639 get_flags(const struct netdev *netdev, int *flags)
1644 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr,
1645 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1647 *flags = ifr_get_flags(&ifr);
1653 set_flags(const char *name, int flags)
1657 ifr_set_flags(&ifr, flags);
1659 return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1663 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1665 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1667 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1668 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1672 netdev->cache_valid |= VALID_IFINDEX;
1673 netdev->ifindex = ifindex;
1675 *ifindexp = netdev->ifindex;
1680 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1682 struct ifaddrs *head;
1683 struct ifaddrs *ifa;
1684 struct sockaddr_dl *sdl;
1686 if (getifaddrs(&head) != 0) {
1687 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1688 ovs_strerror(errno));
1692 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1693 if (ifa->ifa_addr->sa_family == AF_LINK) {
1694 if (!strcmp(ifa->ifa_name, netdev_name)) {
1695 sdl = ALIGNED_CAST(struct sockaddr_dl *, ifa->ifa_addr);
1697 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1705 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1711 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
1712 int hwaddr_len OVS_UNUSED,
1713 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1715 #if defined(__FreeBSD__)
1719 memset(&ifr, 0, sizeof ifr);
1720 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1721 ifr.ifr_addr.sa_family = hwaddr_family;
1722 ifr.ifr_addr.sa_len = hwaddr_len;
1723 memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1724 error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
1726 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1727 netdev_name, ovs_strerror(error));
1731 #elif defined(__NetBSD__)
1732 struct if_laddrreq req;
1733 struct sockaddr_dl *sdl;
1734 struct sockaddr_storage oldaddr;
1738 * get the old address, add new one, and then remove old one.
1741 if (hwaddr_len != ETH_ADDR_LEN) {
1742 /* just to be safe about sockaddr storage size */
1745 memset(&req, 0, sizeof(req));
1746 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1747 req.addr.ss_len = sizeof(req.addr);
1748 req.addr.ss_family = hwaddr_family;
1749 sdl = (struct sockaddr_dl *)&req.addr;
1750 sdl->sdl_alen = hwaddr_len;
1752 error = af_link_ioctl(SIOCGLIFADDR, &req);
1756 if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], mac, hwaddr_len)) {
1761 memset(&req, 0, sizeof(req));
1762 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1763 req.flags = IFLR_ACTIVE;
1764 sdl = (struct sockaddr_dl *)&req.addr;
1765 sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
1766 sdl->sdl_alen = hwaddr_len;
1767 sdl->sdl_family = hwaddr_family;
1768 memcpy(sdl->sdl_data, mac, hwaddr_len);
1769 error = af_link_ioctl(SIOCALIFADDR, &req);
1774 memset(&req, 0, sizeof(req));
1775 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1777 return af_link_ioctl(SIOCDLIFADDR, &req);
1779 #error not implemented
1784 ifr_get_flags(const struct ifreq *ifr)
1786 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1787 return (ifr->ifr_flagshigh << 16) | ifr->ifr_flags;
1789 return ifr->ifr_flags;
1794 ifr_set_flags(struct ifreq *ifr, int flags)
1796 ifr->ifr_flags = flags;
1797 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1798 ifr->ifr_flagshigh = flags >> 16;
1802 #if defined(__NetBSD__)
1803 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
1804 * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
1806 af_link_ioctl(unsigned long command, const void *arg)
1808 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1811 if (ovsthread_once_start(&once)) {
1812 sock = socket(AF_LINK, SOCK_DGRAM, 0);
1815 VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno));
1817 ovsthread_once_done(&once);
1820 return (sock < 0 ? -sock
1821 : ioctl(sock, command, arg) == -1 ? errno