2 * Copyright (c) 2011, 2013 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>
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_rxq_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_rxq_bsd *
171 netdev_rxq_bsd_cast(const struct netdev_rxq *rxq)
173 ovs_assert(is_netdev_bsd_class(netdev_get_class(rxq->netdev)));
174 return CONTAINER_OF(rxq, struct netdev_rxq_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_rxq *
479 netdev_bsd_rxq_alloc(void)
481 struct netdev_rxq_bsd *rxq = xzalloc(sizeof *rxq);
486 netdev_bsd_rxq_construct(struct netdev_rxq *rxq_)
488 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
489 struct netdev *netdev_ = rxq->up.netdev;
490 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
493 if (!strcmp(netdev_get_type(netdev_), "tap")) {
494 rxq->pcap_handle = NULL;
495 rxq->fd = netdev->tap_fd;
498 ovs_mutex_lock(&netdev->mutex);
499 error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
500 &rxq->pcap_handle, &rxq->fd);
501 ovs_mutex_unlock(&netdev->mutex);
508 netdev_bsd_rxq_destruct(struct netdev_rxq *rxq_)
510 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
512 if (rxq->pcap_handle) {
513 pcap_close(rxq->pcap_handle);
518 netdev_bsd_rxq_dealloc(struct netdev_rxq *rxq_)
520 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
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_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, 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(rxq->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 * 'rxq->fd' is initialized with the tap file descriptor.
605 netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
607 size_t size = ofpbuf_tailroom(buffer);
610 ssize_t retval = read(rxq->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_rxq_get_name(&rxq->up));
625 netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct ofpbuf **packet, int *c)
627 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
628 struct netdev *netdev = rxq->up.netdev;
629 struct ofpbuf *buffer;
633 if (netdev_bsd_get_mtu(netdev, &mtu)) {
634 mtu = ETH_PAYLOAD_MAX;
637 buffer = ofpbuf_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu, DP_NETDEV_HEADROOM);
639 retval = (rxq->pcap_handle
640 ? netdev_rxq_bsd_recv_pcap(rxq, buffer)
641 : netdev_rxq_bsd_recv_tap(rxq, 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_rxq_recv() on 'rxq'.
658 netdev_bsd_rxq_wait(struct netdev_rxq *rxq_)
660 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
662 poll_fd_wait(rxq->fd, POLLIN);
665 /* Discards all packets waiting to be received from 'rxq'. */
667 netdev_bsd_rxq_drain(struct netdev_rxq *rxq_)
670 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
672 strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rxq_get_netdev(rxq_)));
673 if (ioctl(rxq->fd, BIOCFLUSH, &ifr) == -1) {
674 VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
675 netdev_rxq_get_name(rxq_), 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_, struct ofpbuf *pkt, bool may_steal)
688 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
689 const char *name = netdev_get_name(netdev_);
690 const void *data = pkt->data;
691 size_t size = pkt->size;
694 ovs_mutex_lock(&dev->mutex);
695 if (dev->tap_fd < 0 && !dev->pcap) {
696 error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
703 if (dev->tap_fd >= 0) {
704 retval = write(dev->tap_fd, data, size);
706 retval = pcap_inject(dev->pcap, data, size);
709 if (errno == EINTR) {
713 if (error != EAGAIN) {
714 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: "
715 "%s", name, ovs_strerror(error));
718 } else if (retval != size) {
719 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIuSIZE"d bytes of "
720 "%"PRIuSIZE") on %s", retval, size, name);
727 ovs_mutex_unlock(&dev->mutex);
736 * Registers with the poll loop to wake up from the next call to poll_block()
737 * when the packet transmission queue has sufficient room to transmit a packet
738 * with netdev_send().
741 netdev_bsd_send_wait(struct netdev *netdev_)
743 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
745 ovs_mutex_lock(&dev->mutex);
746 if (dev->tap_fd >= 0) {
747 /* TAP device always accepts packets. */
748 poll_immediate_wake();
749 } else if (dev->pcap) {
750 poll_fd_wait(dev->fd, POLLOUT);
752 /* We haven't even tried to send a packet yet. */
753 poll_immediate_wake();
755 ovs_mutex_unlock(&dev->mutex);
759 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
760 * otherwise a positive errno value.
763 netdev_bsd_set_etheraddr(struct netdev *netdev_,
764 const uint8_t mac[ETH_ADDR_LEN])
766 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
769 ovs_mutex_lock(&netdev->mutex);
770 if (!(netdev->cache_valid & VALID_ETHERADDR)
771 || !eth_addr_equals(netdev->etheraddr, mac)) {
772 error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
775 netdev->cache_valid |= VALID_ETHERADDR;
776 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
777 seq_change(connectivity_seq_get());
780 ovs_mutex_unlock(&netdev->mutex);
786 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
787 * free the returned buffer.
790 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
791 uint8_t mac[ETH_ADDR_LEN])
793 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
796 ovs_mutex_lock(&netdev->mutex);
797 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
798 error = get_etheraddr(netdev_get_kernel_name(netdev_),
801 netdev->cache_valid |= VALID_ETHERADDR;
805 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
807 ovs_mutex_unlock(&netdev->mutex);
813 * Returns the maximum size of transmitted (and received) packets on 'netdev',
814 * in bytes, not including the hardware header; thus, this is typically 1500
815 * bytes for Ethernet devices.
818 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
820 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
823 ovs_mutex_lock(&netdev->mutex);
824 if (!(netdev->cache_valid & VALID_MTU)) {
827 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
828 SIOCGIFMTU, "SIOCGIFMTU");
830 netdev->mtu = ifr.ifr_mtu;
831 netdev->cache_valid |= VALID_MTU;
837 ovs_mutex_unlock(&netdev->mutex);
843 netdev_bsd_get_ifindex(const struct netdev *netdev_)
845 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
848 ovs_mutex_lock(&netdev->mutex);
849 error = get_ifindex(netdev_, &ifindex);
850 ovs_mutex_unlock(&netdev->mutex);
852 return error ? -error : ifindex;
856 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
858 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
861 ovs_mutex_lock(&netdev->mutex);
862 if (!(netdev->cache_valid & VALID_CARRIER)) {
863 struct ifmediareq ifmr;
865 memset(&ifmr, 0, sizeof(ifmr));
866 strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
867 sizeof ifmr.ifm_name);
869 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
871 netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
872 netdev->cache_valid |= VALID_CARRIER;
874 /* If the interface doesn't report whether the media is active,
875 * just assume it is active. */
876 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
877 netdev->carrier = true;
880 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
881 netdev_get_name(netdev_), ovs_strerror(error));
885 *carrier = netdev->carrier;
887 ovs_mutex_unlock(&netdev->mutex);
893 convert_stats_system(struct netdev_stats *stats, const struct if_data *ifd)
896 * note: UINT64_MAX means unsupported
898 stats->rx_packets = ifd->ifi_ipackets;
899 stats->tx_packets = ifd->ifi_opackets;
900 stats->rx_bytes = ifd->ifi_obytes;
901 stats->tx_bytes = ifd->ifi_ibytes;
902 stats->rx_errors = ifd->ifi_ierrors;
903 stats->tx_errors = ifd->ifi_oerrors;
904 stats->rx_dropped = ifd->ifi_iqdrops;
905 stats->tx_dropped = UINT64_MAX;
906 stats->multicast = ifd->ifi_imcasts;
907 stats->collisions = ifd->ifi_collisions;
908 stats->rx_length_errors = UINT64_MAX;
909 stats->rx_over_errors = UINT64_MAX;
910 stats->rx_crc_errors = UINT64_MAX;
911 stats->rx_frame_errors = UINT64_MAX;
912 stats->rx_fifo_errors = UINT64_MAX;
913 stats->rx_missed_errors = UINT64_MAX;
914 stats->tx_aborted_errors = UINT64_MAX;
915 stats->tx_carrier_errors = UINT64_MAX;
916 stats->tx_fifo_errors = UINT64_MAX;
917 stats->tx_heartbeat_errors = UINT64_MAX;
918 stats->tx_window_errors = UINT64_MAX;
922 convert_stats_tap(struct netdev_stats *stats, const struct if_data *ifd)
925 * Similar to convert_stats_system but swapping rxq and tx
926 * because 'ifd' is stats for the network interface side of the
927 * tap device and what the caller wants is one for the character
930 * note: UINT64_MAX means unsupported
932 stats->rx_packets = ifd->ifi_opackets;
933 stats->tx_packets = ifd->ifi_ipackets;
934 stats->rx_bytes = ifd->ifi_ibytes;
935 stats->tx_bytes = ifd->ifi_obytes;
936 stats->rx_errors = ifd->ifi_oerrors;
937 stats->tx_errors = ifd->ifi_ierrors;
938 stats->rx_dropped = UINT64_MAX;
939 stats->tx_dropped = ifd->ifi_iqdrops;
940 stats->multicast = ifd->ifi_omcasts;
941 stats->collisions = UINT64_MAX;
942 stats->rx_length_errors = UINT64_MAX;
943 stats->rx_over_errors = UINT64_MAX;
944 stats->rx_crc_errors = UINT64_MAX;
945 stats->rx_frame_errors = UINT64_MAX;
946 stats->rx_fifo_errors = UINT64_MAX;
947 stats->rx_missed_errors = UINT64_MAX;
948 stats->tx_aborted_errors = UINT64_MAX;
949 stats->tx_carrier_errors = UINT64_MAX;
950 stats->tx_fifo_errors = UINT64_MAX;
951 stats->tx_heartbeat_errors = UINT64_MAX;
952 stats->tx_window_errors = UINT64_MAX;
956 convert_stats(const struct netdev *netdev, struct netdev_stats *stats,
957 const struct if_data *ifd)
959 if (netdev_bsd_cast(netdev)->tap_fd == -1) {
960 convert_stats_system(stats, ifd);
962 convert_stats_tap(stats, ifd);
966 /* Retrieves current device stats for 'netdev'. */
968 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
970 #if defined(__FreeBSD__)
974 struct ifmibdata ifmd;
979 mib[2] = NETLINK_GENERIC;
980 mib[3] = IFMIB_SYSTEM;
981 mib[4] = IFMIB_IFCOUNT;
983 len = sizeof(if_count);
985 if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
986 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
987 netdev_get_name(netdev_), ovs_strerror(errno));
991 mib[5] = IFDATA_GENERAL;
992 mib[3] = IFMIB_IFDATA;
994 for (i = 1; i <= if_count; i++) {
996 if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
997 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
998 netdev_get_name(netdev_), ovs_strerror(errno));
1000 } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
1001 convert_stats(netdev, stats, &ifdr.ifdr_data);
1007 #elif defined(__NetBSD__)
1008 struct ifdatareq ifdr;
1011 memset(&ifdr, 0, sizeof(ifdr));
1012 strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
1013 sizeof(ifdr.ifdr_name));
1014 error = af_link_ioctl(SIOCGIFDATA, &ifdr);
1016 convert_stats(netdev_, stats, &ifdr.ifdr_data);
1020 #error not implemented
1025 netdev_bsd_parse_media(int media)
1027 uint32_t supported = 0;
1028 bool half_duplex = media & IFM_HDX ? true : false;
1030 switch (IFM_SUBTYPE(media)) {
1035 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
1036 supported |= NETDEV_F_COPPER;
1040 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
1041 supported |= NETDEV_F_FIBER;
1048 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
1049 supported |= NETDEV_F_COPPER;
1053 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
1054 supported |= NETDEV_F_FIBER;
1059 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
1060 supported |= NETDEV_F_COPPER;
1065 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
1066 supported |= NETDEV_F_FIBER;
1070 supported |= NETDEV_F_10GB_FD;
1071 supported |= NETDEV_F_COPPER;
1076 supported |= NETDEV_F_10GB_FD;
1077 supported |= NETDEV_F_FIBER;
1084 if (IFM_SUBTYPE(media) == IFM_AUTO) {
1085 supported |= NETDEV_F_AUTONEG;
1088 if (media & IFM_ETH_FMASK) {
1089 supported |= NETDEV_F_PAUSE;
1097 * Stores the features supported by 'netdev' into each of '*current',
1098 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1099 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1100 * successful, otherwise a positive errno value. On failure, all of the
1101 * passed-in values are set to 0.
1104 netdev_bsd_get_features(const struct netdev *netdev,
1105 enum netdev_features *current, uint32_t *advertised,
1106 enum netdev_features *supported, uint32_t *peer)
1108 struct ifmediareq ifmr;
1114 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1116 memset(&ifmr, 0, sizeof(ifmr));
1117 strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1119 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
1120 * number of supported modes, and a second with a buffer to retrieve
1122 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1124 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1125 netdev_get_name(netdev), ovs_strerror(error));
1129 media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1130 ifmr.ifm_ulist = media_list;
1132 if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
1133 VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1134 netdev_get_name(netdev));
1139 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1141 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1142 netdev_get_name(netdev), ovs_strerror(error));
1146 /* Current settings. */
1147 *current = netdev_bsd_parse_media(ifmr.ifm_active);
1149 /* Advertised features. */
1150 *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1152 /* Supported features. */
1154 for (i = 0; i < ifmr.ifm_count; i++) {
1155 *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1158 /* Peer advertisements. */
1159 *peer = 0; /* XXX */
1168 * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
1169 * '*netmask' to its netmask and returns true. Otherwise, returns false.
1172 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1173 struct in_addr *netmask)
1175 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1178 ovs_mutex_lock(&netdev->mutex);
1179 if (!(netdev->cache_valid & VALID_IN4)) {
1182 ifr.ifr_addr.sa_family = AF_INET;
1183 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1184 SIOCGIFADDR, "SIOCGIFADDR");
1186 const struct sockaddr_in *sin;
1188 sin = (struct sockaddr_in *) &ifr.ifr_addr;
1189 netdev->in4 = sin->sin_addr;
1190 netdev->cache_valid |= VALID_IN4;
1191 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1192 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1194 *netmask = sin->sin_addr;
1200 *netmask = netdev->netmask;
1202 ovs_mutex_unlock(&netdev->mutex);
1204 return error ? error : in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1208 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1209 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1210 * positive errno value.
1213 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1214 struct in_addr mask)
1216 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1219 ovs_mutex_lock(&netdev->mutex);
1220 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1222 if (addr.s_addr != INADDR_ANY) {
1223 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1224 "SIOCSIFNETMASK", mask);
1226 netdev->cache_valid |= VALID_IN4;
1228 netdev->netmask = mask;
1231 seq_change(connectivity_seq_get());
1233 ovs_mutex_unlock(&netdev->mutex);
1239 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1241 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1242 if (!(netdev->cache_valid & VALID_IN6)) {
1243 struct ifaddrs *ifa, *head;
1244 struct sockaddr_in6 *sin6;
1245 const char *netdev_name = netdev_get_name(netdev_);
1247 if (getifaddrs(&head) != 0) {
1248 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1249 ovs_strerror(errno));
1253 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1254 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1255 !strcmp(ifa->ifa_name, netdev_name)) {
1256 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1258 memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
1259 netdev->cache_valid |= VALID_IN6;
1266 return EADDRNOTAVAIL;
1272 #if defined(__NetBSD__)
1274 netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name)
1276 char *ovs_name = NULL;
1277 struct shash device_shash;
1278 struct shash_node *node;
1280 shash_init(&device_shash);
1281 netdev_get_devices(&netdev_tap_class, &device_shash);
1282 SHASH_FOR_EACH(node, &device_shash) {
1283 struct netdev *netdev = node->data;
1284 struct netdev_bsd * const dev = netdev_bsd_cast(netdev);
1286 if (!strcmp(dev->kernel_name, kernel_name)) {
1288 ovs_name = xstrdup(netdev_get_name(&dev->up));
1290 netdev_close(netdev);
1292 shash_destroy(&device_shash);
1294 return ovs_name ? ovs_name : xstrdup(kernel_name);
1299 netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
1300 struct in_addr *next_hop OVS_UNUSED,
1301 char **netdev_name OVS_UNUSED)
1303 #if defined(__NetBSD__)
1305 struct sockaddr_in sin;
1306 struct sockaddr_dl sdl;
1313 struct rt_msghdr *rtm = &buf.h;
1314 const pid_t pid = getpid();
1317 bool gateway = false;
1318 char *ifname = NULL;
1321 memset(next_hop, 0, sizeof(*next_hop));
1322 *netdev_name = NULL;
1324 memset(&sin, 0, sizeof(sin));
1325 sin.sin_len = sizeof(sin);
1326 sin.sin_family = AF_INET;
1328 sin.sin_addr = *host;
1330 memset(&sdl, 0, sizeof(sdl));
1331 sdl.sdl_len = sizeof(sdl);
1332 sdl.sdl_family = AF_LINK;
1334 s = socket(PF_ROUTE, SOCK_RAW, 0);
1335 memset(&buf, 0, sizeof(buf));
1336 rtm->rtm_flags = RTF_HOST|RTF_UP;
1337 rtm->rtm_version = RTM_VERSION;
1338 rtm->rtm_addrs = RTA_DST|RTA_IFP;
1339 cp = (void *)&buf.space;
1340 memcpy(cp, &sin, sizeof(sin));
1341 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin);
1342 memcpy(cp, &sdl, sizeof(sdl));
1343 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl);
1344 rtm->rtm_msglen = cp - (char *)(void *)rtm;
1345 rtm->rtm_seq = ++seq;
1346 rtm->rtm_type = RTM_GET;
1348 write(s, rtm, rtm->rtm_msglen);
1349 memset(&buf, 0, sizeof(buf));
1351 ssz = read(s, &buf, sizeof(buf));
1352 } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
1353 saved_errno = errno;
1359 return EPIPE; /* XXX */
1361 cp = (void *)&buf.space;
1362 for (i = 1; i; i <<= 1) {
1363 if ((rtm->rtm_addrs & i) != 0) {
1364 const struct sockaddr *sa = (const void *)cp;
1366 if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) {
1367 const struct sockaddr_in * const sin =
1368 (const struct sockaddr_in *)sa;
1370 *next_hop = sin->sin_addr;
1373 if ((i == RTA_IFP) && sa->sa_family == AF_LINK) {
1374 const struct sockaddr_dl * const sdl =
1375 (const struct sockaddr_dl *)sa;
1378 kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen);
1379 ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name);
1385 if (ifname == NULL) {
1391 *netdev_name = ifname;
1392 VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s",
1393 IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name);
1401 netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED,
1402 ovs_be32 ip OVS_UNUSED,
1403 uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1405 #if defined(__NetBSD__)
1406 const struct rt_msghdr *rtm;
1419 mib[4] = NET_RT_FLAGS;
1420 mib[5] = RTF_LLINFO;
1421 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
1425 buf = xmalloc(needed);
1426 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
1431 for (cp = buf; cp < ep; cp += rtm->rtm_msglen) {
1432 const struct sockaddr_inarp *sina;
1433 const struct sockaddr_dl *sdl;
1435 rtm = (const void *)cp;
1436 sina = (const void *)(rtm + 1);
1437 if (ip != sina->sin_addr.s_addr) {
1440 sdl = (const void *)
1441 ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len));
1442 if (sdl->sdl_alen == ETH_ADDR_LEN) {
1443 memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN);
1458 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1460 struct sockaddr_in sin;
1461 memset(&sin, 0, sizeof sin);
1462 sin.sin_family = AF_INET;
1463 sin.sin_addr = addr;
1466 memset(sa, 0, sizeof *sa);
1467 memcpy(sa, &sin, sizeof sin);
1471 do_set_addr(struct netdev *netdev,
1472 unsigned long ioctl_nr, const char *ioctl_name,
1473 struct in_addr addr)
1476 make_in4_sockaddr(&ifr.ifr_addr, addr);
1477 return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
1482 nd_to_iff_flags(enum netdev_flags nd)
1485 if (nd & NETDEV_UP) {
1488 if (nd & NETDEV_PROMISC) {
1490 #if defined(IFF_PPROMISC)
1491 iff |= IFF_PPROMISC;
1494 if (nd & NETDEV_LOOPBACK) {
1495 iff |= IFF_LOOPBACK;
1501 iff_to_nd_flags(int iff)
1503 enum netdev_flags nd = 0;
1507 if (iff & IFF_PROMISC) {
1508 nd |= NETDEV_PROMISC;
1510 if (iff & IFF_LOOPBACK) {
1511 nd |= NETDEV_LOOPBACK;
1517 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1518 enum netdev_flags on, enum netdev_flags *old_flagsp)
1520 int old_flags, new_flags;
1523 error = get_flags(netdev_, &old_flags);
1525 *old_flagsp = iff_to_nd_flags(old_flags);
1526 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1527 if (new_flags != old_flags) {
1528 error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
1529 seq_change(connectivity_seq_get());
1535 /* Linux has also different GET_STATS, SET_STATS,
1538 #define NETDEV_BSD_CLASS(NAME, CONSTRUCT, \
1548 netdev_bsd_destruct, \
1549 netdev_bsd_dealloc, \
1550 NULL, /* get_config */ \
1551 NULL, /* set_config */ \
1552 NULL, /* get_tunnel_config */ \
1555 netdev_bsd_send_wait, \
1557 netdev_bsd_set_etheraddr, \
1558 netdev_bsd_get_etheraddr, \
1559 netdev_bsd_get_mtu, \
1560 NULL, /* set_mtu */ \
1561 netdev_bsd_get_ifindex, \
1562 netdev_bsd_get_carrier, \
1563 NULL, /* get_carrier_resets */ \
1564 NULL, /* set_miimon_interval */ \
1565 netdev_bsd_get_stats, \
1566 NULL, /* set_stats */ \
1569 NULL, /* set_advertisement */ \
1570 NULL, /* set_policing */ \
1571 NULL, /* get_qos_type */ \
1572 NULL, /* get_qos_capabilities */ \
1573 NULL, /* get_qos */ \
1574 NULL, /* set_qos */ \
1575 NULL, /* get_queue */ \
1576 NULL, /* set_queue */ \
1577 NULL, /* delete_queue */ \
1578 NULL, /* get_queue_stats */ \
1579 NULL, /* queue_dump_start */ \
1580 NULL, /* queue_dump_next */ \
1581 NULL, /* queue_dump_done */ \
1582 NULL, /* dump_queue_stats */ \
1584 netdev_bsd_get_in4, \
1585 netdev_bsd_set_in4, \
1586 netdev_bsd_get_in6, \
1587 NULL, /* add_router */ \
1588 netdev_bsd_get_next_hop, \
1589 NULL, /* get_status */ \
1590 netdev_bsd_arp_lookup, /* arp_lookup */ \
1592 netdev_bsd_update_flags, \
1594 netdev_bsd_rxq_alloc, \
1595 netdev_bsd_rxq_construct, \
1596 netdev_bsd_rxq_destruct, \
1597 netdev_bsd_rxq_dealloc, \
1598 netdev_bsd_rxq_recv, \
1599 netdev_bsd_rxq_wait, \
1600 netdev_bsd_rxq_drain, \
1603 const struct netdev_class netdev_bsd_class =
1606 netdev_bsd_construct_system,
1607 netdev_bsd_get_features);
1609 const struct netdev_class netdev_tap_class =
1612 netdev_bsd_construct_tap,
1613 netdev_bsd_get_features);
1617 destroy_tap(int fd, const char *name)
1622 strcpy(ifr.ifr_name, name);
1623 /* XXX What to do if this call fails? */
1624 af_inet_ioctl(SIOCIFDESTROY, &ifr);
1628 get_flags(const struct netdev *netdev, int *flags)
1633 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr,
1634 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1636 *flags = ifr_get_flags(&ifr);
1642 set_flags(const char *name, int flags)
1646 ifr_set_flags(&ifr, flags);
1648 return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1652 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1654 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1656 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1657 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1661 netdev->cache_valid |= VALID_IFINDEX;
1662 netdev->ifindex = ifindex;
1664 *ifindexp = netdev->ifindex;
1669 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1671 struct ifaddrs *head;
1672 struct ifaddrs *ifa;
1673 struct sockaddr_dl *sdl;
1675 if (getifaddrs(&head) != 0) {
1676 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1677 ovs_strerror(errno));
1681 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1682 if (ifa->ifa_addr->sa_family == AF_LINK) {
1683 if (!strcmp(ifa->ifa_name, netdev_name)) {
1684 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1686 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1694 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1700 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
1701 int hwaddr_len OVS_UNUSED,
1702 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1704 #if defined(__FreeBSD__)
1708 memset(&ifr, 0, sizeof ifr);
1709 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1710 ifr.ifr_addr.sa_family = hwaddr_family;
1711 ifr.ifr_addr.sa_len = hwaddr_len;
1712 memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1713 error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
1715 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1716 netdev_name, ovs_strerror(error));
1720 #elif defined(__NetBSD__)
1721 struct if_laddrreq req;
1722 struct sockaddr_dl *sdl;
1723 struct sockaddr_storage oldaddr;
1727 * get the old address, add new one, and then remove old one.
1730 if (hwaddr_len != ETH_ADDR_LEN) {
1731 /* just to be safe about sockaddr storage size */
1734 memset(&req, 0, sizeof(req));
1735 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1736 req.addr.ss_len = sizeof(req.addr);
1737 req.addr.ss_family = hwaddr_family;
1738 sdl = (struct sockaddr_dl *)&req.addr;
1739 sdl->sdl_alen = hwaddr_len;
1741 error = af_link_ioctl(SIOCGLIFADDR, &req);
1745 if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], mac, hwaddr_len)) {
1750 memset(&req, 0, sizeof(req));
1751 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1752 req.flags = IFLR_ACTIVE;
1753 sdl = (struct sockaddr_dl *)&req.addr;
1754 sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
1755 sdl->sdl_alen = hwaddr_len;
1756 sdl->sdl_family = hwaddr_family;
1757 memcpy(sdl->sdl_data, mac, hwaddr_len);
1758 error = af_link_ioctl(SIOCALIFADDR, &req);
1763 memset(&req, 0, sizeof(req));
1764 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1766 return af_link_ioctl(SIOCDLIFADDR, &req);
1768 #error not implemented
1773 ifr_get_flags(const struct ifreq *ifr)
1775 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1776 return (ifr->ifr_flagshigh << 16) | ifr->ifr_flags;
1778 return ifr->ifr_flags;
1783 ifr_set_flags(struct ifreq *ifr, int flags)
1785 ifr->ifr_flags = flags;
1786 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1787 ifr->ifr_flagshigh = flags >> 16;
1791 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
1792 * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
1794 af_link_ioctl(unsigned long command, const void *arg)
1796 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1799 if (ovsthread_once_start(&once)) {
1800 sock = socket(AF_LINK, SOCK_DGRAM, 0);
1803 VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno));
1805 ovsthread_once_done(&once);
1808 return (sock < 0 ? -sock
1809 : ioctl(sock, command, arg) == -1 ? errno