2 * Copyright (c) 2011 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>
49 #include "dynamic-string.h"
50 #include "fatal-signal.h"
52 #include "openflow/openflow.h"
53 #include "ovs-thread.h"
55 #include "poll-loop.h"
56 #include "socket-util.h"
62 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
65 struct netdev_rx_bsd {
68 /* Packet capture descriptor for a system network device.
69 * For a tap device this is NULL. */
72 /* Selectable file descriptor for the network device.
73 * This descriptor will be used for polling operations. */
77 static const struct netdev_rx_class netdev_rx_bsd_class;
81 unsigned int cache_valid;
82 unsigned int change_seq;
85 uint8_t etheraddr[ETH_ADDR_LEN];
87 struct in_addr netmask;
92 int tap_fd; /* TAP character device, if any, otherwise -1. */
94 /* Used for sending packets on non-tap devices. */
103 VALID_IFINDEX = 1 << 0,
104 VALID_ETHERADDR = 1 << 1,
108 VALID_CARRIER = 1 << 5
111 #define PCAP_SNAPLEN 2048
115 * Notifier used to invalidate device informations in case of status change.
117 * It will be registered with a 'rtbsd_notifier_register()' when the first
118 * device will be created with the call of either 'netdev_bsd_tap_create()' or
119 * 'netdev_bsd_system_create()'.
121 * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
122 * invalidate cached information about the device.
124 static struct rtbsd_notifier netdev_bsd_cache_notifier;
125 static int cache_notifier_refcount;
127 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
129 static void destroy_tap(int fd, const char *name);
130 static int get_flags(const struct netdev *, int *flagsp);
131 static int set_flags(const char *, int flags);
132 static int do_set_addr(struct netdev *netdev,
133 int ioctl_nr, const char *ioctl_name,
134 struct in_addr addr);
135 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
136 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
137 int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
138 static int get_ifindex(const struct netdev *, int *ifindexp);
140 static int ifr_get_flags(const struct ifreq *);
141 static void ifr_set_flags(struct ifreq *, int flags);
144 static int af_link_ioctl(int command, const void *arg);
147 static void netdev_bsd_run(void);
150 is_netdev_bsd_class(const struct netdev_class *netdev_class)
152 return netdev_class->run == netdev_bsd_run;
155 static struct netdev_bsd *
156 netdev_bsd_cast(const struct netdev *netdev)
158 ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev)));
159 return CONTAINER_OF(netdev, struct netdev_bsd, up);
162 static struct netdev_rx_bsd *
163 netdev_rx_bsd_cast(const struct netdev_rx *rx)
165 netdev_rx_assert_class(rx, &netdev_rx_bsd_class);
166 return CONTAINER_OF(rx, struct netdev_rx_bsd, up);
170 netdev_get_kernel_name(const struct netdev *netdev)
172 return netdev_bsd_cast(netdev)->kernel_name;
176 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
177 * interface status changes, and eventually calls all the user callbacks.
182 rtbsd_notifier_run();
186 * Arranges for poll_block() to wake up if the "run" member function needs to
190 netdev_bsd_wait(void)
192 rtbsd_notifier_wait();
196 netdev_bsd_changed(struct netdev_bsd *dev)
199 if (!dev->change_seq) {
204 /* Invalidate cache in case of interface status change. */
206 netdev_bsd_cache_cb(const struct rtbsd_change *change,
207 void *aux OVS_UNUSED)
209 struct netdev_bsd *dev;
212 struct netdev *base_dev = netdev_from_name(change->if_name);
215 const struct netdev_class *netdev_class =
216 netdev_get_class(base_dev);
218 if (is_netdev_bsd_class(netdev_class)) {
219 dev = netdev_bsd_cast(base_dev);
220 dev->cache_valid = 0;
221 netdev_bsd_changed(dev);
223 netdev_close(base_dev);
227 * XXX the API is lacking, we should be able to iterate on the list of
228 * netdevs without having to store the info in a temp shash.
230 struct shash device_shash;
231 struct shash_node *node;
233 shash_init(&device_shash);
234 netdev_get_devices(&netdev_bsd_class, &device_shash);
235 SHASH_FOR_EACH (node, &device_shash) {
236 struct netdev *netdev = node->data;
237 dev = netdev_bsd_cast(netdev);
238 dev->cache_valid = 0;
239 netdev_bsd_changed(dev);
240 netdev_close(netdev);
242 shash_destroy(&device_shash);
247 cache_notifier_ref(void)
251 if (!cache_notifier_refcount) {
252 ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
253 netdev_bsd_cache_cb, NULL);
258 cache_notifier_refcount++;
263 cache_notifier_unref(void)
265 cache_notifier_refcount--;
266 if (cache_notifier_refcount == 0) {
267 rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
272 /* Allocate a netdev_bsd structure */
274 netdev_bsd_create_system(const struct netdev_class *class, const char *name,
275 struct netdev **netdevp)
277 struct netdev_bsd *netdev;
278 enum netdev_flags flags;
281 error = cache_notifier_ref();
286 netdev = xzalloc(sizeof *netdev);
287 netdev->change_seq = 1;
288 netdev_init(&netdev->up, name, class);
290 netdev->kernel_name = xstrdup(name);
292 /* Verify that the netdev really exists by attempting to read its flags */
293 error = netdev_get_flags(&netdev->up, &flags);
294 if (error == ENXIO) {
295 free(netdev->kernel_name);
296 netdev_uninit(&netdev->up, false);
298 cache_notifier_unref();
302 *netdevp = &netdev->up;
307 * Allocate a netdev_bsd structure with 'tap' class.
310 netdev_bsd_create_tap(const struct netdev_class *class, const char *name,
311 struct netdev **netdevp)
313 struct netdev_bsd *netdev = NULL;
316 char *kernel_name = NULL;
318 error = cache_notifier_ref();
323 /* allocate the device structure and set the internal flag */
324 netdev = xzalloc(sizeof *netdev);
326 memset(&ifr, 0, sizeof(ifr));
328 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
329 * to retrieve the name of the tap device. */
330 netdev->tap_fd = open("/dev/tap", O_RDWR);
331 netdev->change_seq = 1;
332 if (netdev->tap_fd < 0) {
334 VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error));
335 goto error_unref_notifier;
338 /* Retrieve tap name (e.g. tap0) */
339 if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
340 /* XXX Need to destroy the device? */
342 close(netdev->tap_fd);
343 goto error_unref_notifier;
346 /* Change the name of the tap device */
347 #if defined(SIOCSIFNAME)
348 ifr.ifr_data = (void *)name;
349 error = af_inet_ioctl(SIOCSIFNAME, &ifr);
351 destroy_tap(netdev->tap_fd, ifr.ifr_name);
352 goto error_unref_notifier;
354 kernel_name = xstrdup(name);
357 * NetBSD doesn't support inteface renaming.
359 VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name);
360 kernel_name = xstrdup(ifr.ifr_name);
363 /* set non-blocking. */
364 error = set_nonblocking(netdev->tap_fd);
366 destroy_tap(netdev->tap_fd, kernel_name);
367 goto error_unref_notifier;
371 ifr_set_flags(&ifr, IFF_UP);
372 strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
373 error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
375 destroy_tap(netdev->tap_fd, kernel_name);
376 goto error_unref_notifier;
379 /* initialize the device structure and
380 * link the structure to its netdev */
381 netdev_init(&netdev->up, name, class);
382 netdev->kernel_name = kernel_name;
383 *netdevp = &netdev->up;
387 error_unref_notifier:
388 cache_notifier_unref();
396 netdev_bsd_destroy(struct netdev *netdev_)
398 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
400 cache_notifier_unref();
402 if (netdev->tap_fd >= 0) {
403 destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_));
406 pcap_close(netdev->pcap);
408 free(netdev->kernel_name);
413 netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
415 char errbuf[PCAP_ERRBUF_SIZE];
421 /* Open the pcap device. The device is opened in non-promiscuous mode
422 * because the interface flags are manually set by the caller. */
424 pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
426 VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
430 if (errbuf[0] != '\0') {
431 VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
434 /* Get the underlying fd. */
435 fd = pcap_get_selectable_fd(pcap);
437 VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
442 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
443 * on the file descriptor returned by pcap_get_selectable_fd to achieve
444 * a real non-blocking behaviour.*/
445 error = pcap_setnonblock(pcap, 1, errbuf);
451 /* This call assure that reads return immediately upon packet
452 * reception. Otherwise, a read will block until either the kernel
453 * buffer becomes full or a timeout occurs. */
454 if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
455 VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
456 name, ovs_strerror(errno));
461 /* Capture only incoming packets. */
462 error = pcap_setdirection(pcap, PCAP_D_IN);
482 netdev_bsd_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
484 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
486 struct netdev_rx_bsd *rx;
490 if (!strcmp(netdev_get_type(netdev_), "tap")) {
494 int error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
500 netdev_bsd_changed(netdev);
503 rx = xmalloc(sizeof *rx);
504 netdev_rx_init(&rx->up, netdev_, &netdev_rx_bsd_class);
505 rx->pcap_handle = pcap;
513 netdev_rx_bsd_destroy(struct netdev_rx *rx_)
515 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
517 if (rx->pcap_handle) {
518 pcap_close(rx->pcap_handle);
523 /* The recv callback of the netdev class returns the number of bytes of the
526 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
527 * not make difference between a missing packet on the capture interface and
528 * an error during the file capture. We can use the pcap_dispatch() function
529 * instead, which is able to distinguish between errors and null packet.
531 * To make pcap_dispatch() returns the number of bytes read from the interface
532 * we need to define the following callback and argument.
541 * This callback will be executed on every captured packet.
543 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
544 * pcap returns a truncated packet and we follow this behavior.
546 * The argument args->retval is the packet size in bytes.
549 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
551 struct pcap_arg *args = (struct pcap_arg *)args_;
553 if (args->size < hdr->len) {
554 VLOG_WARN_RL(&rl, "packet truncated");
555 args->retval = args->size;
557 args->retval = hdr->len;
560 /* copy the packet to our buffer */
561 memcpy(args->data, packet, args->retval);
565 * This function attempts to receive a packet from the specified network
566 * device. It is assumed that the network device is a system device or a tap
567 * device opened as a system one. In this case the read operation is performed
571 netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, void *data, size_t size)
576 /* prepare the pcap argument to store the packet */
581 ret = pcap_dispatch(rx->pcap_handle, 1, proc_pkt, (u_char *) &arg);
584 return arg.retval; /* arg.retval < 0 is handled in the caller */
587 if (errno == EINTR) {
597 * This function attempts to receive a packet from the specified network
598 * device. It is assumed that the network device is a tap device and
599 * 'rx->fd' is initialized with the tap file descriptor.
602 netdev_rx_bsd_recv_tap(struct netdev_rx_bsd *rx, void *data, size_t size)
605 ssize_t retval = read(rx->fd, data, size);
608 } else if (errno != EINTR) {
609 if (errno != EAGAIN) {
610 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
611 ovs_strerror(errno), netdev_rx_get_name(&rx->up));
620 netdev_rx_bsd_recv(struct netdev_rx *rx_, void *data, size_t size)
622 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
624 return (rx->pcap_handle
625 ? netdev_rx_bsd_recv_pcap(rx, data, size)
626 : netdev_rx_bsd_recv_tap(rx, data, size));
630 * Registers with the poll loop to wake up from the next call to poll_block()
631 * when a packet is ready to be received with netdev_rx_recv() on 'rx'.
634 netdev_rx_bsd_wait(struct netdev_rx *rx_)
636 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
638 poll_fd_wait(rx->fd, POLLIN);
641 /* Discards all packets waiting to be received from 'rx'. */
643 netdev_rx_bsd_drain(struct netdev_rx *rx_)
646 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
648 strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rx_get_netdev(rx_)));
649 if (ioctl(rx->fd, BIOCFLUSH, &ifr) == -1) {
650 VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
651 netdev_rx_get_name(rx_), ovs_strerror(errno));
658 * Send a packet on the specified network device. The device could be either a
659 * system or a tap device.
662 netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
664 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
665 const char *name = netdev_get_name(netdev_);
667 if (dev->tap_fd < 0 && !dev->pcap) {
668 int error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
676 if (dev->tap_fd >= 0) {
677 retval = write(dev->tap_fd, data, size);
679 retval = pcap_inject(dev->pcap, data, size);
682 if (errno == EINTR) {
684 } else if (errno != EAGAIN) {
685 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
686 name, ovs_strerror(errno));
689 } else if (retval != size) {
690 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
691 "%zu) on %s", retval, size, name);
700 * Registers with the poll loop to wake up from the next call to poll_block()
701 * when the packet transmission queue has sufficient room to transmit a packet
702 * with netdev_send().
705 netdev_bsd_send_wait(struct netdev *netdev_)
707 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
709 if (dev->tap_fd >= 0) {
710 /* TAP device always accepts packets. */
711 poll_immediate_wake();
712 } else if (dev->pcap) {
713 poll_fd_wait(dev->fd, POLLOUT);
715 /* We haven't even tried to send a packet yet. */
716 poll_immediate_wake();
721 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
722 * otherwise a positive errno value.
725 netdev_bsd_set_etheraddr(struct netdev *netdev_,
726 const uint8_t mac[ETH_ADDR_LEN])
728 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
731 if (!(netdev->cache_valid & VALID_ETHERADDR)
732 || !eth_addr_equals(netdev->etheraddr, mac)) {
733 error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
736 netdev->cache_valid |= VALID_ETHERADDR;
737 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
738 netdev_bsd_changed(netdev);
747 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
748 * free the returned buffer.
751 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
752 uint8_t mac[ETH_ADDR_LEN])
754 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
756 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
757 int error = get_etheraddr(netdev_get_kernel_name(netdev_),
762 netdev->cache_valid |= VALID_ETHERADDR;
764 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
770 * Returns the maximum size of transmitted (and received) packets on 'netdev',
771 * in bytes, not including the hardware header; thus, this is typically 1500
772 * bytes for Ethernet devices.
775 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
777 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
779 if (!(netdev->cache_valid & VALID_MTU)) {
783 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
784 SIOCGIFMTU, "SIOCGIFMTU");
788 netdev->mtu = ifr.ifr_mtu;
789 netdev->cache_valid |= VALID_MTU;
797 netdev_bsd_get_ifindex(const struct netdev *netdev)
801 error = get_ifindex(netdev, &ifindex);
802 return error ? -error : ifindex;
806 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
808 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
810 if (!(netdev->cache_valid & VALID_CARRIER)) {
811 struct ifmediareq ifmr;
814 memset(&ifmr, 0, sizeof(ifmr));
815 strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
816 sizeof ifmr.ifm_name);
818 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
820 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
821 netdev_get_name(netdev_), ovs_strerror(error));
825 netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
826 netdev->cache_valid |= VALID_CARRIER;
828 /* If the interface doesn't report whether the media is active,
829 * just assume it is active. */
830 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
831 netdev->carrier = true;
834 *carrier = netdev->carrier;
840 convert_stats(struct netdev_stats *stats, const struct if_data *ifd)
843 * note: UINT64_MAX means unsupported
845 stats->rx_packets = ifd->ifi_ipackets;
846 stats->tx_packets = ifd->ifi_opackets;
847 stats->rx_bytes = ifd->ifi_obytes;
848 stats->tx_bytes = ifd->ifi_ibytes;
849 stats->rx_errors = ifd->ifi_ierrors;
850 stats->tx_errors = ifd->ifi_oerrors;
851 stats->rx_dropped = ifd->ifi_iqdrops;
852 stats->tx_dropped = UINT64_MAX;
853 stats->multicast = ifd->ifi_imcasts;
854 stats->collisions = ifd->ifi_collisions;
855 stats->rx_length_errors = UINT64_MAX;
856 stats->rx_over_errors = UINT64_MAX;
857 stats->rx_crc_errors = UINT64_MAX;
858 stats->rx_frame_errors = UINT64_MAX;
859 stats->rx_fifo_errors = UINT64_MAX;
860 stats->rx_missed_errors = UINT64_MAX;
861 stats->tx_aborted_errors = UINT64_MAX;
862 stats->tx_carrier_errors = UINT64_MAX;
863 stats->tx_fifo_errors = UINT64_MAX;
864 stats->tx_heartbeat_errors = UINT64_MAX;
865 stats->tx_window_errors = UINT64_MAX;
868 /* Retrieves current device stats for 'netdev'. */
870 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
872 #if defined(__FreeBSD__)
876 struct ifmibdata ifmd;
881 mib[2] = NETLINK_GENERIC;
882 mib[3] = IFMIB_SYSTEM;
883 mib[4] = IFMIB_IFCOUNT;
885 len = sizeof(if_count);
887 if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
888 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
889 netdev_get_name(netdev_), ovs_strerror(errno));
893 mib[5] = IFDATA_GENERAL;
894 mib[3] = IFMIB_IFDATA;
896 for (i = 1; i <= if_count; i++) {
898 if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
899 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
900 netdev_get_name(netdev_), ovs_strerror(errno));
902 } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
903 convert_stats(stats, &ifmd.ifmd_data);
909 #elif defined(__NetBSD__)
910 struct ifdatareq ifdr;
913 memset(&ifdr, 0, sizeof(ifdr));
914 strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
915 sizeof(ifdr.ifdr_name));
916 error = af_link_ioctl(SIOCGIFDATA, &ifdr);
918 convert_stats(stats, &ifdr.ifdr_data);
922 #error not implemented
927 netdev_bsd_parse_media(int media)
929 uint32_t supported = 0;
930 bool half_duplex = media & IFM_HDX ? true : false;
932 switch (IFM_SUBTYPE(media)) {
937 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
938 supported |= NETDEV_F_COPPER;
942 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
943 supported |= NETDEV_F_FIBER;
950 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
951 supported |= NETDEV_F_COPPER;
955 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
956 supported |= NETDEV_F_FIBER;
961 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
962 supported |= NETDEV_F_COPPER;
967 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
968 supported |= NETDEV_F_FIBER;
972 supported |= NETDEV_F_10GB_FD;
973 supported |= NETDEV_F_COPPER;
978 supported |= NETDEV_F_10GB_FD;
979 supported |= NETDEV_F_FIBER;
986 if (IFM_SUBTYPE(media) == IFM_AUTO) {
987 supported |= NETDEV_F_AUTONEG;
990 if (media & IFM_ETH_FMASK) {
991 supported |= NETDEV_F_PAUSE;
999 * Stores the features supported by 'netdev' into each of '*current',
1000 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1001 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1002 * successful, otherwise a positive errno value. On failure, all of the
1003 * passed-in values are set to 0.
1006 netdev_bsd_get_features(const struct netdev *netdev,
1007 enum netdev_features *current, uint32_t *advertised,
1008 enum netdev_features *supported, uint32_t *peer)
1010 struct ifmediareq ifmr;
1016 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1018 memset(&ifmr, 0, sizeof(ifmr));
1019 strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1021 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
1022 * number of supported modes, and a second with a buffer to retrieve
1024 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1026 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1027 netdev_get_name(netdev), ovs_strerror(error));
1031 media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1032 ifmr.ifm_ulist = media_list;
1034 if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
1035 VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1036 netdev_get_name(netdev));
1041 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1043 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1044 netdev_get_name(netdev), ovs_strerror(error));
1048 /* Current settings. */
1049 *current = netdev_bsd_parse_media(ifmr.ifm_active);
1051 /* Advertised features. */
1052 *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1054 /* Supported features. */
1056 for (i = 0; i < ifmr.ifm_count; i++) {
1057 *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1060 /* Peer advertisements. */
1061 *peer = 0; /* XXX */
1070 * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
1071 * '*netmask' to its netmask and returns true. Otherwise, returns false.
1074 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1075 struct in_addr *netmask)
1077 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1079 if (!(netdev->cache_valid & VALID_IN4)) {
1080 const struct sockaddr_in *sin;
1084 ifr.ifr_addr.sa_family = AF_INET;
1085 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1086 SIOCGIFADDR, "SIOCGIFADDR");
1091 sin = (struct sockaddr_in *) &ifr.ifr_addr;
1092 netdev->in4 = sin->sin_addr;
1093 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1094 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1098 netdev->netmask = sin->sin_addr;
1099 netdev->cache_valid |= VALID_IN4;
1102 *netmask = netdev->netmask;
1104 return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1108 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1109 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1110 * positive errno value.
1113 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1114 struct in_addr mask)
1116 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1119 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1121 if (addr.s_addr != INADDR_ANY) {
1122 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1123 "SIOCSIFNETMASK", mask);
1125 netdev->cache_valid |= VALID_IN4;
1127 netdev->netmask = mask;
1130 netdev_bsd_changed(netdev);
1136 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1138 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1139 if (!(netdev->cache_valid & VALID_IN6)) {
1140 struct ifaddrs *ifa, *head;
1141 struct sockaddr_in6 *sin6;
1142 const char *netdev_name = netdev_get_name(netdev_);
1144 if (getifaddrs(&head) != 0) {
1145 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1146 ovs_strerror(errno));
1150 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1151 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1152 !strcmp(ifa->ifa_name, netdev_name)) {
1153 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1155 memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
1156 netdev->cache_valid |= VALID_IN6;
1163 return EADDRNOTAVAIL;
1169 #if defined(__NetBSD__)
1171 netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name)
1173 char *ovs_name = NULL;
1174 struct shash device_shash;
1175 struct shash_node *node;
1177 shash_init(&device_shash);
1178 netdev_get_devices(&netdev_tap_class, &device_shash);
1179 SHASH_FOR_EACH(node, &device_shash) {
1180 struct netdev *netdev = node->data;
1181 struct netdev_bsd * const dev = netdev_bsd_cast(netdev);
1183 if (!strcmp(dev->kernel_name, kernel_name)) {
1185 ovs_name = xstrdup(netdev_get_name(&dev->up));
1187 netdev_close(netdev);
1189 shash_destroy(&device_shash);
1191 return ovs_name ? ovs_name : xstrdup(kernel_name);
1196 netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
1197 struct in_addr *next_hop OVS_UNUSED,
1198 char **netdev_name OVS_UNUSED)
1200 #if defined(__NetBSD__)
1202 struct sockaddr_in sin;
1203 struct sockaddr_dl sdl;
1210 struct rt_msghdr *rtm = &buf.h;
1211 const pid_t pid = getpid();
1214 bool gateway = false;
1215 char *ifname = NULL;
1218 memset(next_hop, 0, sizeof(*next_hop));
1219 *netdev_name = NULL;
1221 memset(&sin, 0, sizeof(sin));
1222 sin.sin_len = sizeof(sin);
1223 sin.sin_family = AF_INET;
1225 sin.sin_addr = *host;
1227 memset(&sdl, 0, sizeof(sdl));
1228 sdl.sdl_len = sizeof(sdl);
1229 sdl.sdl_family = AF_LINK;
1231 s = socket(PF_ROUTE, SOCK_RAW, 0);
1232 memset(&buf, 0, sizeof(buf));
1233 rtm->rtm_flags = RTF_HOST|RTF_UP;
1234 rtm->rtm_version = RTM_VERSION;
1235 rtm->rtm_addrs = RTA_DST|RTA_IFP;
1236 cp = (void *)&buf.space;
1237 memcpy(cp, &sin, sizeof(sin));
1238 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin);
1239 memcpy(cp, &sdl, sizeof(sdl));
1240 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl);
1241 rtm->rtm_msglen = cp - (char *)(void *)rtm;
1242 rtm->rtm_seq = ++seq;
1243 rtm->rtm_type = RTM_GET;
1245 write(s, rtm, rtm->rtm_msglen);
1246 memset(&buf, 0, sizeof(buf));
1248 ssz = read(s, &buf, sizeof(buf));
1249 } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
1250 saved_errno = errno;
1256 return EPIPE; /* XXX */
1258 cp = (void *)&buf.space;
1259 for (i = 1; i; i <<= 1) {
1260 if ((rtm->rtm_addrs & i) != 0) {
1261 const struct sockaddr *sa = (const void *)cp;
1263 if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) {
1264 const struct sockaddr_in * const sin =
1265 (const struct sockaddr_in *)sa;
1267 *next_hop = sin->sin_addr;
1270 if ((i == RTA_IFP) && sa->sa_family == AF_LINK) {
1271 const struct sockaddr_dl * const sdl =
1272 (const struct sockaddr_dl *)sa;
1275 kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen);
1276 ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name);
1282 if (ifname == NULL) {
1288 *netdev_name = ifname;
1289 VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s",
1290 IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name);
1298 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1300 struct sockaddr_in sin;
1301 memset(&sin, 0, sizeof sin);
1302 sin.sin_family = AF_INET;
1303 sin.sin_addr = addr;
1306 memset(sa, 0, sizeof *sa);
1307 memcpy(sa, &sin, sizeof sin);
1311 do_set_addr(struct netdev *netdev,
1312 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1315 make_in4_sockaddr(&ifr.ifr_addr, addr);
1316 return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
1321 nd_to_iff_flags(enum netdev_flags nd)
1324 if (nd & NETDEV_UP) {
1327 if (nd & NETDEV_PROMISC) {
1329 #if defined(IFF_PPROMISC)
1330 iff |= IFF_PPROMISC;
1337 iff_to_nd_flags(int iff)
1339 enum netdev_flags nd = 0;
1343 if (iff & IFF_PROMISC) {
1344 nd |= NETDEV_PROMISC;
1350 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1351 enum netdev_flags on, enum netdev_flags *old_flagsp)
1353 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1354 int old_flags, new_flags;
1357 error = get_flags(netdev_, &old_flags);
1359 *old_flagsp = iff_to_nd_flags(old_flags);
1360 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1361 if (new_flags != old_flags) {
1362 error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
1363 netdev_bsd_changed(netdev);
1370 netdev_bsd_change_seq(const struct netdev *netdev)
1372 return netdev_bsd_cast(netdev)->change_seq;
1376 const struct netdev_class netdev_bsd_class = {
1382 netdev_bsd_create_system,
1384 NULL, /* get_config */
1385 NULL, /* set_config */
1386 NULL, /* get_tunnel_config */
1391 netdev_bsd_send_wait,
1393 netdev_bsd_set_etheraddr,
1394 netdev_bsd_get_etheraddr,
1397 netdev_bsd_get_ifindex,
1398 netdev_bsd_get_carrier,
1399 NULL, /* get_carrier_resets */
1400 NULL, /* set_miimon_interval */
1401 netdev_bsd_get_stats,
1402 NULL, /* set_stats */
1404 netdev_bsd_get_features,
1405 NULL, /* set_advertisement */
1406 NULL, /* set_policing */
1407 NULL, /* get_qos_type */
1408 NULL, /* get_qos_capabilities */
1411 NULL, /* get_queue */
1412 NULL, /* set_queue */
1413 NULL, /* delete_queue */
1414 NULL, /* get_queue_stats */
1415 NULL, /* dump_queue */
1416 NULL, /* dump_queue_stats */
1421 NULL, /* add_router */
1422 netdev_bsd_get_next_hop,
1423 NULL, /* get_status */
1424 NULL, /* arp_lookup */
1426 netdev_bsd_update_flags,
1428 netdev_bsd_change_seq
1431 const struct netdev_class netdev_tap_class = {
1437 netdev_bsd_create_tap,
1439 NULL, /* get_config */
1440 NULL, /* set_config */
1441 NULL, /* get_tunnel_config */
1446 netdev_bsd_send_wait,
1448 netdev_bsd_set_etheraddr,
1449 netdev_bsd_get_etheraddr,
1452 netdev_bsd_get_ifindex,
1453 netdev_bsd_get_carrier,
1454 NULL, /* get_carrier_resets */
1455 NULL, /* set_miimon_interval */
1456 netdev_bsd_get_stats,
1457 NULL, /* set_stats */
1459 netdev_bsd_get_features,
1460 NULL, /* set_advertisement */
1461 NULL, /* set_policing */
1462 NULL, /* get_qos_type */
1463 NULL, /* get_qos_capabilities */
1466 NULL, /* get_queue */
1467 NULL, /* set_queue */
1468 NULL, /* delete_queue */
1469 NULL, /* get_queue_stats */
1470 NULL, /* dump_queue */
1471 NULL, /* dump_queue_stats */
1476 NULL, /* add_router */
1477 netdev_bsd_get_next_hop,
1478 NULL, /* get_status */
1479 NULL, /* arp_lookup */
1481 netdev_bsd_update_flags,
1483 netdev_bsd_change_seq
1486 static const struct netdev_rx_class netdev_rx_bsd_class = {
1487 netdev_rx_bsd_destroy,
1490 netdev_rx_bsd_drain,
1495 destroy_tap(int fd, const char *name)
1500 strcpy(ifr.ifr_name, name);
1501 /* XXX What to do if this call fails? */
1502 af_inet_ioctl(SIOCIFDESTROY, &ifr);
1506 get_flags(const struct netdev *netdev, int *flags)
1511 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr,
1512 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1514 *flags = ifr_get_flags(&ifr);
1520 set_flags(const char *name, int flags)
1524 ifr_set_flags(&ifr, flags);
1526 return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1530 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1532 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1534 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1535 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1539 netdev->cache_valid |= VALID_IFINDEX;
1540 netdev->ifindex = ifindex;
1542 *ifindexp = netdev->ifindex;
1547 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1549 struct ifaddrs *head;
1550 struct ifaddrs *ifa;
1551 struct sockaddr_dl *sdl;
1553 if (getifaddrs(&head) != 0) {
1554 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1555 ovs_strerror(errno));
1559 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1560 if (ifa->ifa_addr->sa_family == AF_LINK) {
1561 if (!strcmp(ifa->ifa_name, netdev_name)) {
1562 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1564 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1572 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1578 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
1579 int hwaddr_len OVS_UNUSED,
1580 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1582 #if defined(__FreeBSD__)
1586 memset(&ifr, 0, sizeof ifr);
1587 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1588 ifr.ifr_addr.sa_family = hwaddr_family;
1589 ifr.ifr_addr.sa_len = hwaddr_len;
1590 memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1591 error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
1593 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1594 netdev_name, ovs_strerror(error));
1598 #elif defined(__NetBSD__)
1599 struct if_laddrreq req;
1600 struct sockaddr_dl *sdl;
1601 struct sockaddr_storage oldaddr;
1605 * get the old address, add new one, and then remove old one.
1608 if (hwaddr_len != ETH_ADDR_LEN) {
1609 /* just to be safe about sockaddr storage size */
1612 memset(&req, 0, sizeof(req));
1613 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1614 req.addr.ss_len = sizeof(req.addr);
1615 req.addr.ss_family = hwaddr_family;
1616 sdl = (struct sockaddr_dl *)&req.addr;
1617 sdl->sdl_alen = hwaddr_len;
1619 error = af_link_ioctl(SIOCGLIFADDR, &req);
1623 if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], mac, hwaddr_len)) {
1628 memset(&req, 0, sizeof(req));
1629 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1630 req.flags = IFLR_ACTIVE;
1631 sdl = (struct sockaddr_dl *)&req.addr;
1632 sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
1633 sdl->sdl_alen = hwaddr_len;
1634 sdl->sdl_family = hwaddr_family;
1635 memcpy(sdl->sdl_data, mac, hwaddr_len);
1636 error = af_link_ioctl(SIOCALIFADDR, &req);
1641 memset(&req, 0, sizeof(req));
1642 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1644 return af_link_ioctl(SIOCDLIFADDR, &req);
1646 #error not implemented
1651 ifr_get_flags(const struct ifreq *ifr)
1653 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1654 return (ifr->ifr_flagshigh << 16) | ifr->ifr_flags;
1656 return ifr->ifr_flags;
1661 ifr_set_flags(struct ifreq *ifr, int flags)
1663 ifr->ifr_flags = flags;
1664 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1665 ifr->ifr_flagshigh = flags >> 16;
1669 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
1670 * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
1672 af_link_ioctl(int command, const void *arg)
1674 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1677 if (ovsthread_once_start(&once)) {
1678 sock = socket(AF_LINK, SOCK_DGRAM, 0);
1681 VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno));
1683 ovsthread_once_done(&once);
1686 return (sock < 0 ? -sock
1687 : ioctl(sock, command, arg) == -1 ? errno