2 * Copyright (c) 2011, 2013 Gaetano Catalli.
3 * Copyright (c) 2013 YAMAMOTO Takashi.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "netdev-provider.h"
24 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <sys/sockio.h>
30 #include <pcap/pcap.h>
32 #include <net/if_dl.h>
33 #include <net/if_media.h>
34 #include <net/if_tap.h>
35 #include <netinet/in.h>
36 #ifdef HAVE_NET_IF_MIB_H
37 #include <net/if_mib.h>
42 #include <sys/sysctl.h>
43 #if defined(__NetBSD__)
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/if_inarp.h>
50 #include "connectivity.h"
52 #include "dpif-netdev.h"
53 #include "dynamic-string.h"
54 #include "fatal-signal.h"
56 #include "openflow/openflow.h"
57 #include "ovs-thread.h"
59 #include "poll-loop.h"
62 #include "socket-util.h"
67 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
70 struct netdev_rx_bsd {
73 /* Packet capture descriptor for a system network device.
74 * For a tap device this is NULL. */
77 /* Selectable file descriptor for the network device.
78 * This descriptor will be used for polling operations. */
85 /* Never changes after initialization. */
88 /* Protects all members below. */
89 struct ovs_mutex mutex;
91 unsigned int cache_valid;
94 uint8_t etheraddr[ETH_ADDR_LEN];
96 struct in_addr netmask;
101 int tap_fd; /* TAP character device, if any, otherwise -1. */
103 /* Used for sending packets on non-tap devices. */
110 VALID_IFINDEX = 1 << 0,
111 VALID_ETHERADDR = 1 << 1,
115 VALID_CARRIER = 1 << 5
118 #define PCAP_SNAPLEN 2048
122 * Notifier used to invalidate device informations in case of status change.
124 * It will be registered with a 'rtbsd_notifier_register()' when the first
125 * device will be created with the call of either 'netdev_bsd_tap_create()' or
126 * 'netdev_bsd_system_create()'.
128 * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
129 * invalidate cached information about the device.
131 static struct rtbsd_notifier netdev_bsd_cache_notifier;
132 static int cache_notifier_refcount;
134 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
136 static void destroy_tap(int fd, const char *name);
137 static int get_flags(const struct netdev *, int *flagsp);
138 static int set_flags(const char *, int flags);
139 static int do_set_addr(struct netdev *netdev,
140 unsigned long ioctl_nr, const char *ioctl_name,
141 struct in_addr addr);
142 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
143 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
144 int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
145 static int get_ifindex(const struct netdev *, int *ifindexp);
147 static int ifr_get_flags(const struct ifreq *);
148 static void ifr_set_flags(struct ifreq *, int flags);
151 static int af_link_ioctl(unsigned long command, const void *arg);
154 static void netdev_bsd_run(void);
155 static int netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup);
158 is_netdev_bsd_class(const struct netdev_class *netdev_class)
160 return netdev_class->run == netdev_bsd_run;
163 static struct netdev_bsd *
164 netdev_bsd_cast(const struct netdev *netdev)
166 ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev)));
167 return CONTAINER_OF(netdev, struct netdev_bsd, up);
170 static struct netdev_rx_bsd *
171 netdev_rx_bsd_cast(const struct netdev_rx *rx)
173 ovs_assert(is_netdev_bsd_class(netdev_get_class(rx->netdev)));
174 return CONTAINER_OF(rx, struct netdev_rx_bsd, up);
178 netdev_get_kernel_name(const struct netdev *netdev)
180 return netdev_bsd_cast(netdev)->kernel_name;
184 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
185 * interface status changes, and eventually calls all the user callbacks.
190 rtbsd_notifier_run();
194 * Arranges for poll_block() to wake up if the "run" member function needs to
198 netdev_bsd_wait(void)
200 rtbsd_notifier_wait();
203 /* Invalidate cache in case of interface status change. */
205 netdev_bsd_cache_cb(const struct rtbsd_change *change,
206 void *aux OVS_UNUSED)
208 struct netdev_bsd *dev;
211 struct netdev *base_dev = netdev_from_name(change->if_name);
214 const struct netdev_class *netdev_class =
215 netdev_get_class(base_dev);
217 if (is_netdev_bsd_class(netdev_class)) {
218 dev = netdev_bsd_cast(base_dev);
219 dev->cache_valid = 0;
220 seq_change(connectivity_seq_get());
222 netdev_close(base_dev);
226 * XXX the API is lacking, we should be able to iterate on the list of
227 * netdevs without having to store the info in a temp shash.
229 struct shash device_shash;
230 struct shash_node *node;
232 shash_init(&device_shash);
233 netdev_get_devices(&netdev_bsd_class, &device_shash);
234 SHASH_FOR_EACH (node, &device_shash) {
235 struct netdev *netdev = node->data;
236 dev = netdev_bsd_cast(netdev);
237 dev->cache_valid = 0;
238 seq_change(connectivity_seq_get());
239 netdev_close(netdev);
241 shash_destroy(&device_shash);
246 cache_notifier_ref(void)
250 if (!cache_notifier_refcount) {
251 ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
252 netdev_bsd_cache_cb, NULL);
257 cache_notifier_refcount++;
262 cache_notifier_unref(void)
264 cache_notifier_refcount--;
265 if (cache_notifier_refcount == 0) {
266 rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
271 static struct netdev *
272 netdev_bsd_alloc(void)
274 struct netdev_bsd *netdev = xzalloc(sizeof *netdev);
279 netdev_bsd_construct_system(struct netdev *netdev_)
281 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
282 enum netdev_flags flags;
285 error = cache_notifier_ref();
290 ovs_mutex_init(&netdev->mutex);
292 netdev->kernel_name = xstrdup(netdev_->name);
294 /* Verify that the netdev really exists by attempting to read its flags */
295 error = netdev_get_flags(netdev_, &flags);
296 if (error == ENXIO) {
297 free(netdev->kernel_name);
298 cache_notifier_unref();
306 netdev_bsd_construct_tap(struct netdev *netdev_)
308 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
309 const char *name = netdev_->name;
312 char *kernel_name = NULL;
314 error = cache_notifier_ref();
319 memset(&ifr, 0, sizeof(ifr));
321 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
322 * to retrieve the name of the tap device. */
323 ovs_mutex_init(&netdev->mutex);
324 netdev->tap_fd = open("/dev/tap", O_RDWR);
325 if (netdev->tap_fd < 0) {
327 VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error));
328 goto error_unref_notifier;
331 /* Retrieve tap name (e.g. tap0) */
332 if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
333 /* XXX Need to destroy the device? */
335 close(netdev->tap_fd);
336 goto error_unref_notifier;
339 /* Change the name of the tap device */
340 #if defined(SIOCSIFNAME)
341 ifr.ifr_data = (void *)name;
342 error = af_inet_ioctl(SIOCSIFNAME, &ifr);
344 destroy_tap(netdev->tap_fd, ifr.ifr_name);
345 goto error_unref_notifier;
347 kernel_name = xstrdup(name);
350 * NetBSD doesn't support inteface renaming.
352 VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name);
353 kernel_name = xstrdup(ifr.ifr_name);
356 /* set non-blocking. */
357 error = set_nonblocking(netdev->tap_fd);
359 destroy_tap(netdev->tap_fd, kernel_name);
360 goto error_unref_notifier;
364 ifr_set_flags(&ifr, IFF_UP);
365 strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
366 error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
368 destroy_tap(netdev->tap_fd, kernel_name);
369 goto error_unref_notifier;
372 netdev->kernel_name = kernel_name;
376 error_unref_notifier:
377 ovs_mutex_destroy(&netdev->mutex);
378 cache_notifier_unref();
385 netdev_bsd_destruct(struct netdev *netdev_)
387 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
389 cache_notifier_unref();
391 if (netdev->tap_fd >= 0) {
392 destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_));
395 pcap_close(netdev->pcap);
397 free(netdev->kernel_name);
398 ovs_mutex_destroy(&netdev->mutex);
402 netdev_bsd_dealloc(struct netdev *netdev_)
404 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
410 netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
412 char errbuf[PCAP_ERRBUF_SIZE];
418 /* Open the pcap device. The device is opened in non-promiscuous mode
419 * because the interface flags are manually set by the caller. */
421 pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
423 VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
427 if (errbuf[0] != '\0') {
428 VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
431 /* Get the underlying fd. */
432 fd = pcap_get_selectable_fd(pcap);
434 VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
439 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
440 * on the file descriptor returned by pcap_get_selectable_fd to achieve
441 * a real non-blocking behaviour.*/
442 error = pcap_setnonblock(pcap, 1, errbuf);
448 /* This call assure that reads return immediately upon packet
449 * reception. Otherwise, a read will block until either the kernel
450 * buffer becomes full or a timeout occurs. */
451 if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
452 VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
453 name, ovs_strerror(errno));
458 /* Capture only incoming packets. */
459 error = pcap_setdirection(pcap, PCAP_D_IN);
478 static struct netdev_rx *
479 netdev_bsd_rx_alloc(void)
481 struct netdev_rx_bsd *rx = xzalloc(sizeof *rx);
486 netdev_bsd_rx_construct(struct netdev_rx *rx_)
488 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
489 struct netdev *netdev_ = rx->up.netdev;
490 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
493 if (!strcmp(netdev_get_type(netdev_), "tap")) {
494 rx->pcap_handle = NULL;
495 rx->fd = netdev->tap_fd;
498 ovs_mutex_lock(&netdev->mutex);
499 error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
500 &rx->pcap_handle, &rx->fd);
501 ovs_mutex_unlock(&netdev->mutex);
508 netdev_bsd_rx_destruct(struct netdev_rx *rx_)
510 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
512 if (rx->pcap_handle) {
513 pcap_close(rx->pcap_handle);
518 netdev_bsd_rx_dealloc(struct netdev_rx *rx_)
520 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
525 /* The recv callback of the netdev class returns the number of bytes of the
528 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
529 * not make difference between a missing packet on the capture interface and
530 * an error during the file capture. We can use the pcap_dispatch() function
531 * instead, which is able to distinguish between errors and null packet.
533 * To make pcap_dispatch() returns the number of bytes read from the interface
534 * we need to define the following callback and argument.
543 * This callback will be executed on every captured packet.
545 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
546 * pcap returns a truncated packet and we follow this behavior.
548 * The argument args->retval is the packet size in bytes.
551 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
553 struct pcap_arg *args = (struct pcap_arg *)args_;
555 if (args->size < hdr->len) {
556 VLOG_WARN_RL(&rl, "packet truncated");
557 args->retval = args->size;
559 args->retval = hdr->len;
562 /* copy the packet to our buffer */
563 memcpy(args->data, packet, args->retval);
567 * This function attempts to receive a packet from the specified network
568 * device. It is assumed that the network device is a system device or a tap
569 * device opened as a system one. In this case the read operation is performed
573 netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, struct ofpbuf *buffer)
578 /* prepare the pcap argument to store the packet */
579 arg.size = ofpbuf_tailroom(buffer);
580 arg.data = buffer->data;
583 ret = pcap_dispatch(rx->pcap_handle, 1, proc_pkt, (u_char *) &arg);
586 buffer->size += arg.retval;
590 if (errno == EINTR) {
600 * This function attempts to receive a packet from the specified network
601 * device. It is assumed that the network device is a tap device and
602 * 'rx->fd' is initialized with the tap file descriptor.
605 netdev_rx_bsd_recv_tap(struct netdev_rx_bsd *rx, struct ofpbuf *buffer)
607 size_t size = ofpbuf_tailroom(buffer);
610 ssize_t retval = read(rx->fd, buffer->data, size);
612 buffer->size += retval;
614 } else if (errno != EINTR) {
615 if (errno != EAGAIN) {
616 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
617 ovs_strerror(errno), netdev_rx_get_name(&rx->up));
625 netdev_bsd_rx_recv(struct netdev_rx *rx_, struct ofpbuf **packet, int *c)
627 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
628 struct netdev *netdev = rx->up.netdev;
629 struct ofpbuf *buffer;
633 if (netdev_bsd_get_mtu(netdev_bsd_cast(netdev), &mtu)) {
634 mtu = ETH_PAYLOAD_MAX;
637 buffer = ofpbuf_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu, DP_NETDEV_HEADROOM);
639 retval = (rx->pcap_handle
640 ? netdev_rx_bsd_recv_pcap(rx, buffer)
641 : netdev_rx_bsd_recv_tap(rx, buffer));
644 ofpbuf_delete(buffer);
646 dp_packet_pad(buffer);
654 * Registers with the poll loop to wake up from the next call to poll_block()
655 * when a packet is ready to be received with netdev_rx_recv() on 'rx'.
658 netdev_bsd_rx_wait(struct netdev_rx *rx_)
660 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
662 poll_fd_wait(rx->fd, POLLIN);
665 /* Discards all packets waiting to be received from 'rx'. */
667 netdev_bsd_rx_drain(struct netdev_rx *rx_)
670 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
672 strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rx_get_netdev(rx_)));
673 if (ioctl(rx->fd, BIOCFLUSH, &ifr) == -1) {
674 VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
675 netdev_rx_get_name(rx_), ovs_strerror(errno));
682 * Send a packet on the specified network device. The device could be either a
683 * system or a tap device.
686 netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
688 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
689 const char *name = netdev_get_name(netdev_);
692 ovs_mutex_lock(&dev->mutex);
693 if (dev->tap_fd < 0 && !dev->pcap) {
694 error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
701 if (dev->tap_fd >= 0) {
702 retval = write(dev->tap_fd, data, size);
704 retval = pcap_inject(dev->pcap, data, size);
707 if (errno == EINTR) {
711 if (error != EAGAIN) {
712 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: "
713 "%s", name, ovs_strerror(error));
716 } else if (retval != size) {
717 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIuSIZE"d bytes of "
718 "%"PRIuSIZE") on %s", retval, size, name);
725 ovs_mutex_unlock(&dev->mutex);
730 * Registers with the poll loop to wake up from the next call to poll_block()
731 * when the packet transmission queue has sufficient room to transmit a packet
732 * with netdev_send().
735 netdev_bsd_send_wait(struct netdev *netdev_)
737 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
739 ovs_mutex_lock(&dev->mutex);
740 if (dev->tap_fd >= 0) {
741 /* TAP device always accepts packets. */
742 poll_immediate_wake();
743 } else if (dev->pcap) {
744 poll_fd_wait(dev->fd, POLLOUT);
746 /* We haven't even tried to send a packet yet. */
747 poll_immediate_wake();
749 ovs_mutex_unlock(&dev->mutex);
753 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
754 * otherwise a positive errno value.
757 netdev_bsd_set_etheraddr(struct netdev *netdev_,
758 const uint8_t mac[ETH_ADDR_LEN])
760 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
763 ovs_mutex_lock(&netdev->mutex);
764 if (!(netdev->cache_valid & VALID_ETHERADDR)
765 || !eth_addr_equals(netdev->etheraddr, mac)) {
766 error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
769 netdev->cache_valid |= VALID_ETHERADDR;
770 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
771 seq_change(connectivity_seq_get());
774 ovs_mutex_unlock(&netdev->mutex);
780 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
781 * free the returned buffer.
784 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
785 uint8_t mac[ETH_ADDR_LEN])
787 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
790 ovs_mutex_lock(&netdev->mutex);
791 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
792 error = get_etheraddr(netdev_get_kernel_name(netdev_),
795 netdev->cache_valid |= VALID_ETHERADDR;
799 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
801 ovs_mutex_unlock(&netdev->mutex);
807 * Returns the maximum size of transmitted (and received) packets on 'netdev',
808 * in bytes, not including the hardware header; thus, this is typically 1500
809 * bytes for Ethernet devices.
812 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
814 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
817 ovs_mutex_lock(&netdev->mutex);
818 if (!(netdev->cache_valid & VALID_MTU)) {
821 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
822 SIOCGIFMTU, "SIOCGIFMTU");
824 netdev->mtu = ifr.ifr_mtu;
825 netdev->cache_valid |= VALID_MTU;
831 ovs_mutex_unlock(&netdev->mutex);
837 netdev_bsd_get_ifindex(const struct netdev *netdev_)
839 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
842 ovs_mutex_lock(&netdev->mutex);
843 error = get_ifindex(netdev_, &ifindex);
844 ovs_mutex_unlock(&netdev->mutex);
846 return error ? -error : ifindex;
850 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
852 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
855 ovs_mutex_lock(&netdev->mutex);
856 if (!(netdev->cache_valid & VALID_CARRIER)) {
857 struct ifmediareq ifmr;
859 memset(&ifmr, 0, sizeof(ifmr));
860 strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
861 sizeof ifmr.ifm_name);
863 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
865 netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
866 netdev->cache_valid |= VALID_CARRIER;
868 /* If the interface doesn't report whether the media is active,
869 * just assume it is active. */
870 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
871 netdev->carrier = true;
874 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
875 netdev_get_name(netdev_), ovs_strerror(error));
879 *carrier = netdev->carrier;
881 ovs_mutex_unlock(&netdev->mutex);
887 convert_stats_system(struct netdev_stats *stats, const struct if_data *ifd)
890 * note: UINT64_MAX means unsupported
892 stats->rx_packets = ifd->ifi_ipackets;
893 stats->tx_packets = ifd->ifi_opackets;
894 stats->rx_bytes = ifd->ifi_obytes;
895 stats->tx_bytes = ifd->ifi_ibytes;
896 stats->rx_errors = ifd->ifi_ierrors;
897 stats->tx_errors = ifd->ifi_oerrors;
898 stats->rx_dropped = ifd->ifi_iqdrops;
899 stats->tx_dropped = UINT64_MAX;
900 stats->multicast = ifd->ifi_imcasts;
901 stats->collisions = ifd->ifi_collisions;
902 stats->rx_length_errors = UINT64_MAX;
903 stats->rx_over_errors = UINT64_MAX;
904 stats->rx_crc_errors = UINT64_MAX;
905 stats->rx_frame_errors = UINT64_MAX;
906 stats->rx_fifo_errors = UINT64_MAX;
907 stats->rx_missed_errors = UINT64_MAX;
908 stats->tx_aborted_errors = UINT64_MAX;
909 stats->tx_carrier_errors = UINT64_MAX;
910 stats->tx_fifo_errors = UINT64_MAX;
911 stats->tx_heartbeat_errors = UINT64_MAX;
912 stats->tx_window_errors = UINT64_MAX;
916 convert_stats_tap(struct netdev_stats *stats, const struct if_data *ifd)
919 * Similar to convert_stats_system but swapping rx and tx
920 * because 'ifd' is stats for the network interface side of the
921 * tap device and what the caller wants is one for the character
924 * note: UINT64_MAX means unsupported
926 stats->rx_packets = ifd->ifi_opackets;
927 stats->tx_packets = ifd->ifi_ipackets;
928 stats->rx_bytes = ifd->ifi_ibytes;
929 stats->tx_bytes = ifd->ifi_obytes;
930 stats->rx_errors = ifd->ifi_oerrors;
931 stats->tx_errors = ifd->ifi_ierrors;
932 stats->rx_dropped = UINT64_MAX;
933 stats->tx_dropped = ifd->ifi_iqdrops;
934 stats->multicast = ifd->ifi_omcasts;
935 stats->collisions = UINT64_MAX;
936 stats->rx_length_errors = UINT64_MAX;
937 stats->rx_over_errors = UINT64_MAX;
938 stats->rx_crc_errors = UINT64_MAX;
939 stats->rx_frame_errors = UINT64_MAX;
940 stats->rx_fifo_errors = UINT64_MAX;
941 stats->rx_missed_errors = UINT64_MAX;
942 stats->tx_aborted_errors = UINT64_MAX;
943 stats->tx_carrier_errors = UINT64_MAX;
944 stats->tx_fifo_errors = UINT64_MAX;
945 stats->tx_heartbeat_errors = UINT64_MAX;
946 stats->tx_window_errors = UINT64_MAX;
950 convert_stats(const struct netdev *netdev, struct netdev_stats *stats,
951 const struct if_data *ifd)
953 if (netdev_bsd_cast(netdev)->tap_fd == -1) {
954 convert_stats_system(stats, ifd);
956 convert_stats_tap(stats, ifd);
960 /* Retrieves current device stats for 'netdev'. */
962 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
964 #if defined(__FreeBSD__)
968 struct ifmibdata ifmd;
973 mib[2] = NETLINK_GENERIC;
974 mib[3] = IFMIB_SYSTEM;
975 mib[4] = IFMIB_IFCOUNT;
977 len = sizeof(if_count);
979 if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
980 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
981 netdev_get_name(netdev_), ovs_strerror(errno));
985 mib[5] = IFDATA_GENERAL;
986 mib[3] = IFMIB_IFDATA;
988 for (i = 1; i <= if_count; i++) {
990 if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
991 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
992 netdev_get_name(netdev_), ovs_strerror(errno));
994 } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
995 convert_stats(netdev, stats, &ifdr.ifdr_data);
1001 #elif defined(__NetBSD__)
1002 struct ifdatareq ifdr;
1005 memset(&ifdr, 0, sizeof(ifdr));
1006 strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
1007 sizeof(ifdr.ifdr_name));
1008 error = af_link_ioctl(SIOCGIFDATA, &ifdr);
1010 convert_stats(netdev_, stats, &ifdr.ifdr_data);
1014 #error not implemented
1019 netdev_bsd_parse_media(int media)
1021 uint32_t supported = 0;
1022 bool half_duplex = media & IFM_HDX ? true : false;
1024 switch (IFM_SUBTYPE(media)) {
1029 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
1030 supported |= NETDEV_F_COPPER;
1034 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
1035 supported |= NETDEV_F_FIBER;
1042 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
1043 supported |= NETDEV_F_COPPER;
1047 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
1048 supported |= NETDEV_F_FIBER;
1053 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
1054 supported |= NETDEV_F_COPPER;
1059 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
1060 supported |= NETDEV_F_FIBER;
1064 supported |= NETDEV_F_10GB_FD;
1065 supported |= NETDEV_F_COPPER;
1070 supported |= NETDEV_F_10GB_FD;
1071 supported |= NETDEV_F_FIBER;
1078 if (IFM_SUBTYPE(media) == IFM_AUTO) {
1079 supported |= NETDEV_F_AUTONEG;
1082 if (media & IFM_ETH_FMASK) {
1083 supported |= NETDEV_F_PAUSE;
1091 * Stores the features supported by 'netdev' into each of '*current',
1092 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1093 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1094 * successful, otherwise a positive errno value. On failure, all of the
1095 * passed-in values are set to 0.
1098 netdev_bsd_get_features(const struct netdev *netdev,
1099 enum netdev_features *current, uint32_t *advertised,
1100 enum netdev_features *supported, uint32_t *peer)
1102 struct ifmediareq ifmr;
1108 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1110 memset(&ifmr, 0, sizeof(ifmr));
1111 strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1113 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
1114 * number of supported modes, and a second with a buffer to retrieve
1116 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1118 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1119 netdev_get_name(netdev), ovs_strerror(error));
1123 media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1124 ifmr.ifm_ulist = media_list;
1126 if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
1127 VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1128 netdev_get_name(netdev));
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 /* Current settings. */
1141 *current = netdev_bsd_parse_media(ifmr.ifm_active);
1143 /* Advertised features. */
1144 *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1146 /* Supported features. */
1148 for (i = 0; i < ifmr.ifm_count; i++) {
1149 *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1152 /* Peer advertisements. */
1153 *peer = 0; /* XXX */
1162 * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
1163 * '*netmask' to its netmask and returns true. Otherwise, returns false.
1166 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1167 struct in_addr *netmask)
1169 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1172 ovs_mutex_lock(&netdev->mutex);
1173 if (!(netdev->cache_valid & VALID_IN4)) {
1176 ifr.ifr_addr.sa_family = AF_INET;
1177 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1178 SIOCGIFADDR, "SIOCGIFADDR");
1180 const struct sockaddr_in *sin;
1182 sin = (struct sockaddr_in *) &ifr.ifr_addr;
1183 netdev->in4 = sin->sin_addr;
1184 netdev->cache_valid |= VALID_IN4;
1185 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1186 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1188 *netmask = sin->sin_addr;
1194 *netmask = netdev->netmask;
1196 ovs_mutex_unlock(&netdev->mutex);
1198 return error ? error : in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1202 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1203 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1204 * positive errno value.
1207 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1208 struct in_addr mask)
1210 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1213 ovs_mutex_lock(&netdev->mutex);
1214 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1216 if (addr.s_addr != INADDR_ANY) {
1217 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1218 "SIOCSIFNETMASK", mask);
1220 netdev->cache_valid |= VALID_IN4;
1222 netdev->netmask = mask;
1225 seq_change(connectivity_seq_get());
1227 ovs_mutex_unlock(&netdev->mutex);
1233 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1235 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1236 if (!(netdev->cache_valid & VALID_IN6)) {
1237 struct ifaddrs *ifa, *head;
1238 struct sockaddr_in6 *sin6;
1239 const char *netdev_name = netdev_get_name(netdev_);
1241 if (getifaddrs(&head) != 0) {
1242 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1243 ovs_strerror(errno));
1247 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1248 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1249 !strcmp(ifa->ifa_name, netdev_name)) {
1250 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1252 memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
1253 netdev->cache_valid |= VALID_IN6;
1260 return EADDRNOTAVAIL;
1266 #if defined(__NetBSD__)
1268 netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name)
1270 char *ovs_name = NULL;
1271 struct shash device_shash;
1272 struct shash_node *node;
1274 shash_init(&device_shash);
1275 netdev_get_devices(&netdev_tap_class, &device_shash);
1276 SHASH_FOR_EACH(node, &device_shash) {
1277 struct netdev *netdev = node->data;
1278 struct netdev_bsd * const dev = netdev_bsd_cast(netdev);
1280 if (!strcmp(dev->kernel_name, kernel_name)) {
1282 ovs_name = xstrdup(netdev_get_name(&dev->up));
1284 netdev_close(netdev);
1286 shash_destroy(&device_shash);
1288 return ovs_name ? ovs_name : xstrdup(kernel_name);
1293 netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
1294 struct in_addr *next_hop OVS_UNUSED,
1295 char **netdev_name OVS_UNUSED)
1297 #if defined(__NetBSD__)
1299 struct sockaddr_in sin;
1300 struct sockaddr_dl sdl;
1307 struct rt_msghdr *rtm = &buf.h;
1308 const pid_t pid = getpid();
1311 bool gateway = false;
1312 char *ifname = NULL;
1315 memset(next_hop, 0, sizeof(*next_hop));
1316 *netdev_name = NULL;
1318 memset(&sin, 0, sizeof(sin));
1319 sin.sin_len = sizeof(sin);
1320 sin.sin_family = AF_INET;
1322 sin.sin_addr = *host;
1324 memset(&sdl, 0, sizeof(sdl));
1325 sdl.sdl_len = sizeof(sdl);
1326 sdl.sdl_family = AF_LINK;
1328 s = socket(PF_ROUTE, SOCK_RAW, 0);
1329 memset(&buf, 0, sizeof(buf));
1330 rtm->rtm_flags = RTF_HOST|RTF_UP;
1331 rtm->rtm_version = RTM_VERSION;
1332 rtm->rtm_addrs = RTA_DST|RTA_IFP;
1333 cp = (void *)&buf.space;
1334 memcpy(cp, &sin, sizeof(sin));
1335 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin);
1336 memcpy(cp, &sdl, sizeof(sdl));
1337 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl);
1338 rtm->rtm_msglen = cp - (char *)(void *)rtm;
1339 rtm->rtm_seq = ++seq;
1340 rtm->rtm_type = RTM_GET;
1342 write(s, rtm, rtm->rtm_msglen);
1343 memset(&buf, 0, sizeof(buf));
1345 ssz = read(s, &buf, sizeof(buf));
1346 } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
1347 saved_errno = errno;
1353 return EPIPE; /* XXX */
1355 cp = (void *)&buf.space;
1356 for (i = 1; i; i <<= 1) {
1357 if ((rtm->rtm_addrs & i) != 0) {
1358 const struct sockaddr *sa = (const void *)cp;
1360 if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) {
1361 const struct sockaddr_in * const sin =
1362 (const struct sockaddr_in *)sa;
1364 *next_hop = sin->sin_addr;
1367 if ((i == RTA_IFP) && sa->sa_family == AF_LINK) {
1368 const struct sockaddr_dl * const sdl =
1369 (const struct sockaddr_dl *)sa;
1372 kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen);
1373 ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name);
1379 if (ifname == NULL) {
1385 *netdev_name = ifname;
1386 VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s",
1387 IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name);
1395 netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED,
1396 ovs_be32 ip OVS_UNUSED,
1397 uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1399 #if defined(__NetBSD__)
1400 const struct rt_msghdr *rtm;
1413 mib[4] = NET_RT_FLAGS;
1414 mib[5] = RTF_LLINFO;
1415 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
1419 buf = xmalloc(needed);
1420 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
1425 for (cp = buf; cp < ep; cp += rtm->rtm_msglen) {
1426 const struct sockaddr_inarp *sina;
1427 const struct sockaddr_dl *sdl;
1429 rtm = (const void *)cp;
1430 sina = (const void *)(rtm + 1);
1431 if (ip != sina->sin_addr.s_addr) {
1434 sdl = (const void *)
1435 ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len));
1436 if (sdl->sdl_alen == ETH_ADDR_LEN) {
1437 memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN);
1452 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1454 struct sockaddr_in sin;
1455 memset(&sin, 0, sizeof sin);
1456 sin.sin_family = AF_INET;
1457 sin.sin_addr = addr;
1460 memset(sa, 0, sizeof *sa);
1461 memcpy(sa, &sin, sizeof sin);
1465 do_set_addr(struct netdev *netdev,
1466 unsigned long ioctl_nr, const char *ioctl_name,
1467 struct in_addr addr)
1470 make_in4_sockaddr(&ifr.ifr_addr, addr);
1471 return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
1476 nd_to_iff_flags(enum netdev_flags nd)
1479 if (nd & NETDEV_UP) {
1482 if (nd & NETDEV_PROMISC) {
1484 #if defined(IFF_PPROMISC)
1485 iff |= IFF_PPROMISC;
1488 if (nd & NETDEV_LOOPBACK) {
1489 iff |= IFF_LOOPBACK;
1495 iff_to_nd_flags(int iff)
1497 enum netdev_flags nd = 0;
1501 if (iff & IFF_PROMISC) {
1502 nd |= NETDEV_PROMISC;
1504 if (iff & IFF_LOOPBACK) {
1505 nd |= NETDEV_LOOPBACK;
1511 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1512 enum netdev_flags on, enum netdev_flags *old_flagsp)
1514 int old_flags, new_flags;
1517 error = get_flags(netdev_, &old_flags);
1519 *old_flagsp = iff_to_nd_flags(old_flags);
1520 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1521 if (new_flags != old_flags) {
1522 error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
1523 seq_change(connectivity_seq_get());
1529 /* Linux has also different GET_STATS, SET_STATS,
1532 #define NETDEV_BSD_CLASS(NAME, CONSTRUCT, \
1542 netdev_bsd_destruct, \
1543 netdev_bsd_dealloc, \
1544 NULL, /* get_config */ \
1545 NULL, /* set_config */ \
1546 NULL, /* get_tunnel_config */ \
1549 netdev_bsd_send_wait, \
1551 netdev_bsd_set_etheraddr, \
1552 netdev_bsd_get_etheraddr, \
1553 netdev_bsd_get_mtu, \
1554 NULL, /* set_mtu */ \
1555 netdev_bsd_get_ifindex, \
1556 netdev_bsd_get_carrier, \
1557 NULL, /* get_carrier_resets */ \
1558 NULL, /* set_miimon_interval */ \
1559 netdev_bsd_get_stats, \
1560 NULL, /* set_stats */ \
1563 NULL, /* set_advertisement */ \
1564 NULL, /* set_policing */ \
1565 NULL, /* get_qos_type */ \
1566 NULL, /* get_qos_capabilities */ \
1567 NULL, /* get_qos */ \
1568 NULL, /* set_qos */ \
1569 NULL, /* get_queue */ \
1570 NULL, /* set_queue */ \
1571 NULL, /* delete_queue */ \
1572 NULL, /* get_queue_stats */ \
1573 NULL, /* queue_dump_start */ \
1574 NULL, /* queue_dump_next */ \
1575 NULL, /* queue_dump_done */ \
1576 NULL, /* dump_queue_stats */ \
1578 netdev_bsd_get_in4, \
1579 netdev_bsd_set_in4, \
1580 netdev_bsd_get_in6, \
1581 NULL, /* add_router */ \
1582 netdev_bsd_get_next_hop, \
1583 NULL, /* get_status */ \
1584 netdev_bsd_arp_lookup, /* arp_lookup */ \
1586 netdev_bsd_update_flags, \
1588 netdev_bsd_rx_alloc, \
1589 netdev_bsd_rx_construct, \
1590 netdev_bsd_rx_destruct, \
1591 netdev_bsd_rx_dealloc, \
1592 netdev_bsd_rx_recv, \
1593 netdev_bsd_rx_wait, \
1594 netdev_bsd_rx_drain, \
1597 const struct netdev_class netdev_bsd_class =
1600 netdev_bsd_construct_system,
1601 netdev_bsd_get_features);
1603 const struct netdev_class netdev_tap_class =
1606 netdev_bsd_construct_tap,
1607 netdev_bsd_get_features);
1611 destroy_tap(int fd, const char *name)
1616 strcpy(ifr.ifr_name, name);
1617 /* XXX What to do if this call fails? */
1618 af_inet_ioctl(SIOCIFDESTROY, &ifr);
1622 get_flags(const struct netdev *netdev, int *flags)
1627 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr,
1628 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1630 *flags = ifr_get_flags(&ifr);
1636 set_flags(const char *name, int flags)
1640 ifr_set_flags(&ifr, flags);
1642 return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1646 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1648 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1650 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1651 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1655 netdev->cache_valid |= VALID_IFINDEX;
1656 netdev->ifindex = ifindex;
1658 *ifindexp = netdev->ifindex;
1663 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1665 struct ifaddrs *head;
1666 struct ifaddrs *ifa;
1667 struct sockaddr_dl *sdl;
1669 if (getifaddrs(&head) != 0) {
1670 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1671 ovs_strerror(errno));
1675 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1676 if (ifa->ifa_addr->sa_family == AF_LINK) {
1677 if (!strcmp(ifa->ifa_name, netdev_name)) {
1678 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1680 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1688 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1694 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
1695 int hwaddr_len OVS_UNUSED,
1696 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1698 #if defined(__FreeBSD__)
1702 memset(&ifr, 0, sizeof ifr);
1703 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1704 ifr.ifr_addr.sa_family = hwaddr_family;
1705 ifr.ifr_addr.sa_len = hwaddr_len;
1706 memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1707 error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
1709 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1710 netdev_name, ovs_strerror(error));
1714 #elif defined(__NetBSD__)
1715 struct if_laddrreq req;
1716 struct sockaddr_dl *sdl;
1717 struct sockaddr_storage oldaddr;
1721 * get the old address, add new one, and then remove old one.
1724 if (hwaddr_len != ETH_ADDR_LEN) {
1725 /* just to be safe about sockaddr storage size */
1728 memset(&req, 0, sizeof(req));
1729 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1730 req.addr.ss_len = sizeof(req.addr);
1731 req.addr.ss_family = hwaddr_family;
1732 sdl = (struct sockaddr_dl *)&req.addr;
1733 sdl->sdl_alen = hwaddr_len;
1735 error = af_link_ioctl(SIOCGLIFADDR, &req);
1739 if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], mac, hwaddr_len)) {
1744 memset(&req, 0, sizeof(req));
1745 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1746 req.flags = IFLR_ACTIVE;
1747 sdl = (struct sockaddr_dl *)&req.addr;
1748 sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
1749 sdl->sdl_alen = hwaddr_len;
1750 sdl->sdl_family = hwaddr_family;
1751 memcpy(sdl->sdl_data, mac, hwaddr_len);
1752 error = af_link_ioctl(SIOCALIFADDR, &req);
1757 memset(&req, 0, sizeof(req));
1758 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1760 return af_link_ioctl(SIOCDLIFADDR, &req);
1762 #error not implemented
1767 ifr_get_flags(const struct ifreq *ifr)
1769 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1770 return (ifr->ifr_flagshigh << 16) | ifr->ifr_flags;
1772 return ifr->ifr_flags;
1777 ifr_set_flags(struct ifreq *ifr, int flags)
1779 ifr->ifr_flags = flags;
1780 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1781 ifr->ifr_flagshigh = flags >> 16;
1785 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
1786 * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
1788 af_link_ioctl(unsigned long command, const void *arg)
1790 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1793 if (ovsthread_once_start(&once)) {
1794 sock = socket(AF_LINK, SOCK_DGRAM, 0);
1797 VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno));
1799 ovsthread_once_done(&once);
1802 return (sock < 0 ? -sock
1803 : ioctl(sock, command, arg) == -1 ? errno