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"
54 #include "poll-loop.h"
55 #include "socket-util.h"
61 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
64 struct netdev_rx_bsd {
67 /* Packet capture descriptor for a system network device.
68 * For a tap device this is NULL. */
71 /* Selectable file descriptor for the network device.
72 * This descriptor will be used for polling operations. */
76 static const struct netdev_rx_class netdev_rx_bsd_class;
80 unsigned int cache_valid;
81 unsigned int change_seq;
84 uint8_t etheraddr[ETH_ADDR_LEN];
90 int tap_fd; /* TAP character device, if any, otherwise -1. */
92 /* Used for sending packets on non-tap devices. */
101 VALID_IFINDEX = 1 << 0,
102 VALID_ETHERADDR = 1 << 1,
106 VALID_CARRIER = 1 << 5
109 /* An AF_INET socket (used for ioctl operations). */
110 static int af_inet_sock = -1;
112 #if defined(__NetBSD__)
113 /* AF_LINK socket used for netdev_bsd_get_stats and set_etheraddr */
114 static int af_link_sock = -1;
115 #endif /* defined(__NetBSD__) */
117 #define PCAP_SNAPLEN 2048
121 * Notifier used to invalidate device informations in case of status change.
123 * It will be registered with a 'rtbsd_notifier_register()' when the first
124 * device will be created with the call of either 'netdev_bsd_tap_create()' or
125 * 'netdev_bsd_system_create()'.
127 * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
128 * invalidate cached information about the device.
130 static struct rtbsd_notifier netdev_bsd_cache_notifier;
131 static int cache_notifier_refcount;
133 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
135 static int netdev_bsd_do_ioctl(const char *, struct ifreq *, unsigned long cmd,
136 const char *cmd_name);
137 static void destroy_tap(int fd, const char *name);
138 static int get_flags(const struct netdev *, int *flagsp);
139 static int set_flags(const char *, int flags);
140 static int do_set_addr(struct netdev *netdev,
141 int ioctl_nr, const char *ioctl_name,
142 struct in_addr addr);
143 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
144 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
145 int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
146 static int get_ifindex(const struct netdev *, int *ifindexp);
148 static int ifr_get_flags(const struct ifreq *);
149 static void ifr_set_flags(struct ifreq *, int flags);
151 static int netdev_bsd_init(void);
154 is_netdev_bsd_class(const struct netdev_class *netdev_class)
156 return netdev_class->init == netdev_bsd_init;
159 static struct netdev_bsd *
160 netdev_bsd_cast(const struct netdev *netdev)
162 ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev)));
163 return CONTAINER_OF(netdev, struct netdev_bsd, up);
166 static struct netdev_rx_bsd *
167 netdev_rx_bsd_cast(const struct netdev_rx *rx)
169 netdev_rx_assert_class(rx, &netdev_rx_bsd_class);
170 return CONTAINER_OF(rx, struct netdev_rx_bsd, up);
174 netdev_get_kernel_name(const struct netdev *netdev)
176 return netdev_bsd_cast(netdev)->kernel_name;
179 /* Initialize the AF_INET socket used for ioctl operations */
181 netdev_bsd_init(void)
183 static int status = -1;
185 if (status >= 0) { /* already initialized */
189 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
190 status = af_inet_sock >= 0 ? 0 : errno;
192 VLOG_ERR("failed to create inet socket: %s", strerror(status));
196 #if defined(__NetBSD__)
197 af_link_sock = socket(AF_LINK, SOCK_DGRAM, 0);
198 status = af_link_sock >= 0 ? 0 : errno;
200 VLOG_ERR("failed to create link socket: %s", strerror(status));
204 #endif /* defined(__NetBSD__) */
210 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
211 * interface status changes, and eventually calls all the user callbacks.
216 rtbsd_notifier_run();
220 * Arranges for poll_block() to wake up if the "run" member function needs to
224 netdev_bsd_wait(void)
226 rtbsd_notifier_wait();
230 netdev_bsd_changed(struct netdev_bsd *dev)
233 if (!dev->change_seq) {
238 /* Invalidate cache in case of interface status change. */
240 netdev_bsd_cache_cb(const struct rtbsd_change *change,
241 void *aux OVS_UNUSED)
243 struct netdev_bsd *dev;
246 struct netdev *base_dev = netdev_from_name(change->if_name);
249 const struct netdev_class *netdev_class =
250 netdev_get_class(base_dev);
252 if (is_netdev_bsd_class(netdev_class)) {
253 dev = netdev_bsd_cast(base_dev);
254 dev->cache_valid = 0;
255 netdev_bsd_changed(dev);
260 * XXX the API is lacking, we should be able to iterate on the list of
261 * netdevs without having to store the info in a temp shash.
263 struct shash device_shash;
264 struct shash_node *node;
266 shash_init(&device_shash);
267 netdev_get_devices(&netdev_bsd_class, &device_shash);
268 SHASH_FOR_EACH (node, &device_shash) {
270 dev->cache_valid = 0;
271 netdev_bsd_changed(dev);
273 shash_destroy(&device_shash);
278 cache_notifier_ref(void)
282 if (!cache_notifier_refcount) {
283 ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
284 netdev_bsd_cache_cb, NULL);
289 cache_notifier_refcount++;
294 cache_notifier_unref(void)
296 cache_notifier_refcount--;
297 if (cache_notifier_refcount == 0) {
298 rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
303 /* Allocate a netdev_bsd structure */
305 netdev_bsd_create_system(const struct netdev_class *class, const char *name,
306 struct netdev **netdevp)
308 struct netdev_bsd *netdev;
309 enum netdev_flags flags;
312 error = cache_notifier_ref();
317 netdev = xzalloc(sizeof *netdev);
318 netdev->change_seq = 1;
319 netdev_init(&netdev->up, name, class);
321 netdev->kernel_name = xstrdup(name);
323 /* Verify that the netdev really exists by attempting to read its flags */
324 error = netdev_get_flags(&netdev->up, &flags);
325 if (error == ENXIO) {
326 netdev_uninit(&netdev->up, false);
328 cache_notifier_unref();
332 *netdevp = &netdev->up;
337 * Allocate a netdev_bsd structure with 'tap' class.
340 netdev_bsd_create_tap(const struct netdev_class *class, const char *name,
341 struct netdev **netdevp)
343 struct netdev_bsd *netdev = NULL;
346 char *kernel_name = NULL;
348 error = cache_notifier_ref();
353 /* allocate the device structure and set the internal flag */
354 netdev = xzalloc(sizeof *netdev);
356 memset(&ifr, 0, sizeof(ifr));
358 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
359 * to retrieve the name of the tap device. */
360 netdev->tap_fd = open("/dev/tap", O_RDWR);
361 netdev->change_seq = 1;
362 if (netdev->tap_fd < 0) {
364 VLOG_WARN("opening \"/dev/tap\" failed: %s", strerror(error));
365 goto error_undef_notifier;
368 /* Retrieve tap name (e.g. tap0) */
369 if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
370 /* XXX Need to destroy the device? */
372 goto error_undef_notifier;
375 /* Change the name of the tap device */
376 #if defined(SIOCSIFNAME)
377 ifr.ifr_data = (void *)name;
378 if (ioctl(af_inet_sock, SIOCSIFNAME, &ifr) == -1) {
380 destroy_tap(netdev->tap_fd, ifr.ifr_name);
381 goto error_undef_notifier;
383 kernel_name = xstrdup(name);
386 * NetBSD doesn't support inteface renaming.
388 VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name);
389 kernel_name = xstrdup(ifr.ifr_name);
392 /* set non-blocking. */
393 error = set_nonblocking(netdev->tap_fd);
395 destroy_tap(netdev->tap_fd, kernel_name);
396 goto error_undef_notifier;
400 ifr_set_flags(&ifr, IFF_UP);
401 strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
402 if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) {
404 destroy_tap(netdev->tap_fd, kernel_name);
405 goto error_undef_notifier;
408 /* initialize the device structure and
409 * link the structure to its netdev */
410 netdev_init(&netdev->up, name, class);
411 netdev->kernel_name = kernel_name;
412 *netdevp = &netdev->up;
416 error_undef_notifier:
417 cache_notifier_unref();
425 netdev_bsd_destroy(struct netdev *netdev_)
427 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
429 cache_notifier_unref();
431 if (netdev->tap_fd >= 0) {
432 destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_));
435 pcap_close(netdev->pcap);
437 free(netdev->kernel_name);
442 netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
444 char errbuf[PCAP_ERRBUF_SIZE];
450 /* Open the pcap device. The device is opened in non-promiscuous mode
451 * because the interface flags are manually set by the caller. */
453 pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
455 VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
459 if (errbuf[0] != '\0') {
460 VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
463 /* Get the underlying fd. */
464 fd = pcap_get_selectable_fd(pcap);
466 VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
471 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
472 * on the file descriptor returned by pcap_get_selectable_fd to achieve
473 * a real non-blocking behaviour.*/
474 error = pcap_setnonblock(pcap, 1, errbuf);
480 /* This call assure that reads return immediately upon packet
481 * reception. Otherwise, a read will block until either the kernel
482 * buffer becomes full or a timeout occurs. */
483 if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
484 VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
485 name, strerror(errno));
490 /* Capture only incoming packets. */
491 error = pcap_setdirection(pcap, PCAP_D_IN);
511 netdev_bsd_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
513 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
515 struct netdev_rx_bsd *rx;
519 if (!strcmp(netdev_get_type(netdev_), "tap")) {
523 int error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
529 netdev_bsd_changed(netdev);
532 rx = xmalloc(sizeof *rx);
533 netdev_rx_init(&rx->up, netdev_, &netdev_rx_bsd_class);
534 rx->pcap_handle = pcap;
542 netdev_rx_bsd_destroy(struct netdev_rx *rx_)
544 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
546 if (rx->pcap_handle) {
547 pcap_close(rx->pcap_handle);
552 /* The recv callback of the netdev class returns the number of bytes of the
555 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
556 * not make difference between a missing packet on the capture interface and
557 * an error during the file capture. We can use the pcap_dispatch() function
558 * instead, which is able to distinguish between errors and null packet.
560 * To make pcap_dispatch() returns the number of bytes read from the interface
561 * we need to define the following callback and argument.
570 * This callback will be executed on every captured packet.
572 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
573 * pcap returns a truncated packet and we follow this behavior.
575 * The argument args->retval is the packet size in bytes.
578 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
580 struct pcap_arg *args = (struct pcap_arg *)args_;
582 if (args->size < hdr->len) {
583 VLOG_WARN_RL(&rl, "packet truncated");
584 args->retval = args->size;
586 args->retval = hdr->len;
589 /* copy the packet to our buffer */
590 memcpy(args->data, packet, args->retval);
594 * This function attempts to receive a packet from the specified network
595 * device. It is assumed that the network device is a system device or a tap
596 * device opened as a system one. In this case the read operation is performed
600 netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, void *data, size_t size)
605 /* prepare the pcap argument to store the packet */
610 ret = pcap_dispatch(rx->pcap_handle, 1, proc_pkt, (u_char *) &arg);
613 return arg.retval; /* arg.retval < 0 is handled in the caller */
616 if (errno == EINTR) {
626 * This function attempts to receive a packet from the specified network
627 * device. It is assumed that the network device is a tap device and
628 * 'rx->fd' is initialized with the tap file descriptor.
631 netdev_rx_bsd_recv_tap(struct netdev_rx_bsd *rx, void *data, size_t size)
634 ssize_t retval = read(rx->fd, data, size);
637 } else if (errno != EINTR) {
638 if (errno != EAGAIN) {
639 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
640 strerror(errno), netdev_rx_get_name(&rx->up));
649 netdev_rx_bsd_recv(struct netdev_rx *rx_, void *data, size_t size)
651 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
653 return (rx->pcap_handle
654 ? netdev_rx_bsd_recv_pcap(rx, data, size)
655 : netdev_rx_bsd_recv_tap(rx, data, size));
659 * Registers with the poll loop to wake up from the next call to poll_block()
660 * when a packet is ready to be received with netdev_rx_recv() on 'rx'.
663 netdev_rx_bsd_wait(struct netdev_rx *rx_)
665 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
667 poll_fd_wait(rx->fd, POLLIN);
670 /* Discards all packets waiting to be received from 'rx'. */
672 netdev_rx_bsd_drain(struct netdev_rx *rx_)
675 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
677 strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rx_get_netdev(rx_)));
678 if (ioctl(rx->fd, BIOCFLUSH, &ifr) == -1) {
679 VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
680 netdev_rx_get_name(rx_), strerror(errno));
687 * Send a packet on the specified network device. The device could be either a
688 * system or a tap device.
691 netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
693 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
694 const char *name = netdev_get_name(netdev_);
696 if (dev->tap_fd < 0 && !dev->pcap) {
697 int error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
705 if (dev->tap_fd >= 0) {
706 retval = write(dev->tap_fd, data, size);
708 retval = pcap_inject(dev->pcap, data, size);
711 if (errno == EINTR) {
713 } else if (errno != EAGAIN) {
714 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
715 name, strerror(errno));
718 } else if (retval != size) {
719 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
720 "%zu) on %s", retval, size, name);
729 * Registers with the poll loop to wake up from the next call to poll_block()
730 * when the packet transmission queue has sufficient room to transmit a packet
731 * with netdev_send().
734 netdev_bsd_send_wait(struct netdev *netdev_)
736 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
738 if (dev->tap_fd >= 0) {
739 /* TAP device always accepts packets. */
740 poll_immediate_wake();
741 } else if (dev->pcap) {
742 poll_fd_wait(dev->fd, POLLOUT);
744 /* We haven't even tried to send a packet yet. */
745 poll_immediate_wake();
750 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
751 * otherwise a positive errno value.
754 netdev_bsd_set_etheraddr(struct netdev *netdev_,
755 const uint8_t mac[ETH_ADDR_LEN])
757 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
760 if (!(netdev->cache_valid & VALID_ETHERADDR)
761 || !eth_addr_equals(netdev->etheraddr, mac)) {
762 error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
765 netdev->cache_valid |= VALID_ETHERADDR;
766 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
767 netdev_bsd_changed(netdev);
776 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
777 * free the returned buffer.
780 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
781 uint8_t mac[ETH_ADDR_LEN])
783 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
785 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
786 int error = get_etheraddr(netdev_get_kernel_name(netdev_),
791 netdev->cache_valid |= VALID_ETHERADDR;
793 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
799 * Returns the maximum size of transmitted (and received) packets on 'netdev',
800 * in bytes, not including the hardware header; thus, this is typically 1500
801 * bytes for Ethernet devices.
804 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
806 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
808 if (!(netdev->cache_valid & VALID_MTU)) {
812 error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr,
813 SIOCGIFMTU, "SIOCGIFMTU");
817 netdev->mtu = ifr.ifr_mtu;
818 netdev->cache_valid |= VALID_MTU;
826 netdev_bsd_get_ifindex(const struct netdev *netdev)
830 error = get_ifindex(netdev, &ifindex);
831 return error ? -error : ifindex;
835 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
837 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
839 if (!(netdev->cache_valid & VALID_CARRIER)) {
840 struct ifmediareq ifmr;
842 memset(&ifmr, 0, sizeof(ifmr));
843 strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
844 sizeof ifmr.ifm_name);
846 if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
847 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
848 netdev_get_name(netdev_), strerror(errno));
852 netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
853 netdev->cache_valid |= VALID_CARRIER;
855 /* If the interface doesn't report whether the media is active,
856 * just assume it is active. */
857 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
858 netdev->carrier = true;
861 *carrier = netdev->carrier;
867 convert_stats(struct netdev_stats *stats, const struct if_data *ifd)
870 * note: UINT64_MAX means unsupported
872 stats->rx_packets = ifd->ifi_ipackets;
873 stats->tx_packets = ifd->ifi_opackets;
874 stats->rx_bytes = ifd->ifi_obytes;
875 stats->tx_bytes = ifd->ifi_ibytes;
876 stats->rx_errors = ifd->ifi_ierrors;
877 stats->tx_errors = ifd->ifi_oerrors;
878 stats->rx_dropped = ifd->ifi_iqdrops;
879 stats->tx_dropped = UINT64_MAX;
880 stats->multicast = ifd->ifi_imcasts;
881 stats->collisions = ifd->ifi_collisions;
882 stats->rx_length_errors = UINT64_MAX;
883 stats->rx_over_errors = UINT64_MAX;
884 stats->rx_crc_errors = UINT64_MAX;
885 stats->rx_frame_errors = UINT64_MAX;
886 stats->rx_fifo_errors = UINT64_MAX;
887 stats->rx_missed_errors = UINT64_MAX;
888 stats->tx_aborted_errors = UINT64_MAX;
889 stats->tx_carrier_errors = UINT64_MAX;
890 stats->tx_fifo_errors = UINT64_MAX;
891 stats->tx_heartbeat_errors = UINT64_MAX;
892 stats->tx_window_errors = UINT64_MAX;
895 /* Retrieves current device stats for 'netdev'. */
897 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
899 #if defined(__FreeBSD__)
903 struct ifmibdata ifmd;
908 mib[2] = NETLINK_GENERIC;
909 mib[3] = IFMIB_SYSTEM;
910 mib[4] = IFMIB_IFCOUNT;
912 len = sizeof(if_count);
914 if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
915 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
916 netdev_get_name(netdev_), strerror(errno));
920 mib[5] = IFDATA_GENERAL;
921 mib[3] = IFMIB_IFDATA;
923 for (i = 1; i <= if_count; i++) {
925 if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
926 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
927 netdev_get_name(netdev_), strerror(errno));
929 } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
930 convert_stats(stats, &ifmd.ifmd_data);
936 #elif defined(__NetBSD__)
937 struct ifdatareq ifdr;
941 memset(&ifdr, 0, sizeof(ifdr));
942 strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
943 sizeof(ifdr.ifdr_name));
944 ret = ioctl(af_link_sock, SIOCGIFDATA, &ifdr);
949 convert_stats(stats, &ifdr.ifdr_data);
952 #error not implemented
957 netdev_bsd_parse_media(int media)
959 uint32_t supported = 0;
960 bool half_duplex = media & IFM_HDX ? true : false;
962 switch (IFM_SUBTYPE(media)) {
967 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
968 supported |= NETDEV_F_COPPER;
972 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
973 supported |= NETDEV_F_FIBER;
980 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
981 supported |= NETDEV_F_COPPER;
985 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
986 supported |= NETDEV_F_FIBER;
991 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
992 supported |= NETDEV_F_COPPER;
997 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
998 supported |= NETDEV_F_FIBER;
1002 supported |= NETDEV_F_10GB_FD;
1003 supported |= NETDEV_F_COPPER;
1008 supported |= NETDEV_F_10GB_FD;
1009 supported |= NETDEV_F_FIBER;
1016 if (IFM_SUBTYPE(media) == IFM_AUTO) {
1017 supported |= NETDEV_F_AUTONEG;
1020 if (media & IFM_ETH_FMASK) {
1021 supported |= NETDEV_F_PAUSE;
1029 * Stores the features supported by 'netdev' into each of '*current',
1030 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1031 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1032 * successful, otherwise a positive errno value. On failure, all of the
1033 * passed-in values are set to 0.
1036 netdev_bsd_get_features(const struct netdev *netdev,
1037 enum netdev_features *current, uint32_t *advertised,
1038 enum netdev_features *supported, uint32_t *peer)
1040 struct ifmediareq ifmr;
1046 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1048 memset(&ifmr, 0, sizeof(ifmr));
1049 strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1051 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
1052 * number of supported modes, and a second with a buffer to retrieve
1054 if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
1055 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1056 netdev_get_name(netdev), strerror(errno));
1060 media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1061 ifmr.ifm_ulist = media_list;
1063 if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
1064 VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1065 netdev_get_name(netdev));
1070 if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
1071 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1072 netdev_get_name(netdev), strerror(errno));
1077 /* Current settings. */
1078 *current = netdev_bsd_parse_media(ifmr.ifm_active);
1080 /* Advertised features. */
1081 *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1083 /* Supported features. */
1085 for (i = 0; i < ifmr.ifm_count; i++) {
1086 *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1089 /* Peer advertisements. */
1090 *peer = 0; /* XXX */
1099 * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
1100 * 'in4' is non-null) and returns true. Otherwise, returns false.
1103 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1104 struct in_addr *netmask)
1106 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1108 if (!(netdev->cache_valid & VALID_IN4)) {
1109 const struct sockaddr_in *sin;
1113 ifr.ifr_addr.sa_family = AF_INET;
1114 error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1115 SIOCGIFADDR, "SIOCGIFADDR");
1120 sin = (struct sockaddr_in *) &ifr.ifr_addr;
1121 netdev->in4 = sin->sin_addr;
1122 netdev->cache_valid |= VALID_IN4;
1123 error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1124 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1128 *netmask = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr;
1132 return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1136 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1137 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1138 * positive errno value.
1141 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1142 struct in_addr mask)
1144 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1147 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1149 netdev->cache_valid |= VALID_IN4;
1151 if (addr.s_addr != INADDR_ANY) {
1152 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1153 "SIOCSIFNETMASK", mask);
1155 netdev_bsd_changed(netdev);
1161 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1163 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1164 if (!(netdev->cache_valid & VALID_IN6)) {
1165 struct ifaddrs *ifa, *head;
1166 struct sockaddr_in6 *sin6;
1167 const char *netdev_name = netdev_get_name(netdev_);
1169 if (getifaddrs(&head) != 0) {
1170 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1175 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1176 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1177 !strcmp(ifa->ifa_name, netdev_name)) {
1178 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1180 memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
1181 netdev->cache_valid |= VALID_IN6;
1188 return EADDRNOTAVAIL;
1194 #if defined(__NetBSD__)
1195 static struct netdev *
1196 find_netdev_by_kernel_name(const char *kernel_name)
1198 struct shash device_shash;
1199 struct shash_node *node;
1201 shash_init(&device_shash);
1202 netdev_get_devices(&netdev_tap_class, &device_shash);
1203 SHASH_FOR_EACH(node, &device_shash) {
1204 struct netdev_bsd * const dev = node->data;
1206 if (!strcmp(dev->kernel_name, kernel_name)) {
1207 shash_destroy(&device_shash);
1211 shash_destroy(&device_shash);
1216 netdev_bsd_convert_kernel_name_to_ovs_name(const char *kernel_name)
1218 const struct netdev * const netdev =
1219 find_netdev_by_kernel_name(kernel_name);
1221 if (netdev == NULL) {
1224 return netdev_get_name(netdev);
1229 netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
1230 struct in_addr *next_hop OVS_UNUSED,
1231 char **netdev_name OVS_UNUSED)
1233 #if defined(__NetBSD__)
1235 struct sockaddr_in sin;
1236 struct sockaddr_dl sdl;
1243 struct rt_msghdr *rtm = &buf.h;
1244 const pid_t pid = getpid();
1247 bool gateway = false;
1248 char *ifname = NULL;
1251 memset(next_hop, 0, sizeof(*next_hop));
1252 *netdev_name = NULL;
1254 memset(&sin, 0, sizeof(sin));
1255 sin.sin_len = sizeof(sin);
1256 sin.sin_family = AF_INET;
1258 sin.sin_addr = *host;
1260 memset(&sdl, 0, sizeof(sdl));
1261 sdl.sdl_len = sizeof(sdl);
1262 sdl.sdl_family = AF_LINK;
1264 s = socket(PF_ROUTE, SOCK_RAW, 0);
1265 memset(&buf, 0, sizeof(buf));
1266 rtm->rtm_flags = RTF_HOST|RTF_UP;
1267 rtm->rtm_version = RTM_VERSION;
1268 rtm->rtm_addrs = RTA_DST|RTA_IFP;
1269 cp = (void *)&buf.space;
1270 memcpy(cp, &sin, sizeof(sin));
1271 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin);
1272 memcpy(cp, &sdl, sizeof(sdl));
1273 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl);
1274 rtm->rtm_msglen = cp - (char *)(void *)rtm;
1275 rtm->rtm_seq = ++seq;
1276 rtm->rtm_type = RTM_GET;
1278 write(s, rtm, rtm->rtm_msglen);
1279 memset(&buf, 0, sizeof(buf));
1281 ssz = read(s, &buf, sizeof(buf));
1282 } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
1283 saved_errno = errno;
1289 return EPIPE; /* XXX */
1291 cp = (void *)&buf.space;
1292 for (i = 1; i; i <<= 1) {
1293 if ((rtm->rtm_addrs & i) != 0) {
1294 const struct sockaddr *sa = (const void *)cp;
1296 if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) {
1297 const struct sockaddr_in * const sin =
1298 (const struct sockaddr_in *)sa;
1300 *next_hop = sin->sin_addr;
1303 if ((i == RTA_IFP) && sa->sa_family == AF_LINK) {
1304 const struct sockaddr_dl * const sdl =
1305 (const struct sockaddr_dl *)sa;
1306 const size_t nlen = sdl->sdl_nlen;
1307 char * const kernel_name = xmalloc(nlen + 1);
1310 memcpy(kernel_name, sdl->sdl_data, nlen);
1311 kernel_name[nlen] = 0;
1312 name = netdev_bsd_convert_kernel_name_to_ovs_name(kernel_name);
1314 ifname = xstrdup(kernel_name);
1316 ifname = xstrdup(name);
1323 if (ifname == NULL) {
1329 *netdev_name = ifname;
1330 VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s",
1331 IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name);
1339 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1341 struct sockaddr_in sin;
1342 memset(&sin, 0, sizeof sin);
1343 sin.sin_family = AF_INET;
1344 sin.sin_addr = addr;
1347 memset(sa, 0, sizeof *sa);
1348 memcpy(sa, &sin, sizeof sin);
1352 do_set_addr(struct netdev *netdev,
1353 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1356 make_in4_sockaddr(&ifr.ifr_addr, addr);
1357 return netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
1362 nd_to_iff_flags(enum netdev_flags nd)
1365 if (nd & NETDEV_UP) {
1368 if (nd & NETDEV_PROMISC) {
1370 #if defined(IFF_PPROMISC)
1371 iff |= IFF_PPROMISC;
1378 iff_to_nd_flags(int iff)
1380 enum netdev_flags nd = 0;
1384 if (iff & IFF_PROMISC) {
1385 nd |= NETDEV_PROMISC;
1391 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1392 enum netdev_flags on, enum netdev_flags *old_flagsp)
1394 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1395 int old_flags, new_flags;
1398 error = get_flags(netdev_, &old_flags);
1400 *old_flagsp = iff_to_nd_flags(old_flags);
1401 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1402 if (new_flags != old_flags) {
1403 error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
1404 netdev_bsd_changed(netdev);
1411 netdev_bsd_change_seq(const struct netdev *netdev)
1413 return netdev_bsd_cast(netdev)->change_seq;
1417 const struct netdev_class netdev_bsd_class = {
1423 netdev_bsd_create_system,
1425 NULL, /* get_config */
1426 NULL, /* set_config */
1427 NULL, /* get_tunnel_config */
1432 netdev_bsd_send_wait,
1434 netdev_bsd_set_etheraddr,
1435 netdev_bsd_get_etheraddr,
1438 netdev_bsd_get_ifindex,
1439 netdev_bsd_get_carrier,
1440 NULL, /* get_carrier_resets */
1441 NULL, /* set_miimon_interval */
1442 netdev_bsd_get_stats,
1443 NULL, /* set_stats */
1445 netdev_bsd_get_features,
1446 NULL, /* set_advertisement */
1447 NULL, /* set_policing */
1448 NULL, /* get_qos_type */
1449 NULL, /* get_qos_capabilities */
1452 NULL, /* get_queue */
1453 NULL, /* set_queue */
1454 NULL, /* delete_queue */
1455 NULL, /* get_queue_stats */
1456 NULL, /* dump_queue */
1457 NULL, /* dump_queue_stats */
1462 NULL, /* add_router */
1463 netdev_bsd_get_next_hop,
1464 NULL, /* get_status */
1465 NULL, /* arp_lookup */
1467 netdev_bsd_update_flags,
1469 netdev_bsd_change_seq
1472 const struct netdev_class netdev_tap_class = {
1478 netdev_bsd_create_tap,
1480 NULL, /* get_config */
1481 NULL, /* set_config */
1482 NULL, /* get_tunnel_config */
1487 netdev_bsd_send_wait,
1489 netdev_bsd_set_etheraddr,
1490 netdev_bsd_get_etheraddr,
1493 netdev_bsd_get_ifindex,
1494 netdev_bsd_get_carrier,
1495 NULL, /* get_carrier_resets */
1496 NULL, /* set_miimon_interval */
1497 netdev_bsd_get_stats,
1498 NULL, /* set_stats */
1500 netdev_bsd_get_features,
1501 NULL, /* set_advertisement */
1502 NULL, /* set_policing */
1503 NULL, /* get_qos_type */
1504 NULL, /* get_qos_capabilities */
1507 NULL, /* get_queue */
1508 NULL, /* set_queue */
1509 NULL, /* delete_queue */
1510 NULL, /* get_queue_stats */
1511 NULL, /* dump_queue */
1512 NULL, /* dump_queue_stats */
1517 NULL, /* add_router */
1518 netdev_bsd_get_next_hop,
1519 NULL, /* get_status */
1520 NULL, /* arp_lookup */
1522 netdev_bsd_update_flags,
1524 netdev_bsd_change_seq
1527 static const struct netdev_rx_class netdev_rx_bsd_class = {
1528 netdev_rx_bsd_destroy,
1531 netdev_rx_bsd_drain,
1536 destroy_tap(int fd, const char *name)
1541 strcpy(ifr.ifr_name, name);
1542 /* XXX What to do if this call fails? */
1543 ioctl(af_inet_sock, SIOCIFDESTROY, &ifr);
1547 get_flags(const struct netdev *netdev, int *flags)
1552 error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev), &ifr,
1553 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1555 *flags = ifr_get_flags(&ifr);
1561 set_flags(const char *name, int flags)
1565 ifr_set_flags(&ifr, flags);
1567 return netdev_bsd_do_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1571 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1573 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1575 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1576 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1580 netdev->cache_valid |= VALID_IFINDEX;
1581 netdev->ifindex = ifindex;
1583 *ifindexp = netdev->ifindex;
1588 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1590 struct ifaddrs *head;
1591 struct ifaddrs *ifa;
1592 struct sockaddr_dl *sdl;
1594 if (getifaddrs(&head) != 0) {
1595 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1600 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1601 if (ifa->ifa_addr->sa_family == AF_LINK) {
1602 if (!strcmp(ifa->ifa_name, netdev_name)) {
1603 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1605 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1613 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1619 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
1620 int hwaddr_len OVS_UNUSED,
1621 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1623 #if defined(__FreeBSD__)
1626 memset(&ifr, 0, sizeof ifr);
1627 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1628 ifr.ifr_addr.sa_family = hwaddr_family;
1629 ifr.ifr_addr.sa_len = hwaddr_len;
1630 memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1631 if (ioctl(af_inet_sock, SIOCSIFLLADDR, &ifr) < 0) {
1632 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1633 netdev_name, strerror(errno));
1637 #elif defined(__NetBSD__)
1638 struct if_laddrreq req;
1639 struct sockaddr_dl *sdl;
1640 struct sockaddr_storage oldaddr;
1644 * get the old address, add new one, and then remove old one.
1647 if (hwaddr_len != ETH_ADDR_LEN) {
1648 /* just to be safe about sockaddr storage size */
1651 memset(&req, 0, sizeof(req));
1652 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1653 req.addr.ss_len = sizeof(req.addr);
1654 req.addr.ss_family = hwaddr_family;
1655 sdl = (struct sockaddr_dl *)&req.addr;
1656 sdl->sdl_alen = hwaddr_len;
1657 ret = ioctl(af_link_sock, SIOCGLIFADDR, &req);
1661 if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], mac, hwaddr_len)) {
1666 memset(&req, 0, sizeof(req));
1667 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1668 req.flags = IFLR_ACTIVE;
1669 sdl = (struct sockaddr_dl *)&req.addr;
1670 sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
1671 sdl->sdl_alen = hwaddr_len;
1672 sdl->sdl_family = hwaddr_family;
1673 memcpy(sdl->sdl_data, mac, hwaddr_len);
1674 ret = ioctl(af_link_sock, SIOCALIFADDR, &req);
1679 memset(&req, 0, sizeof(req));
1680 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1682 ret = ioctl(af_link_sock, SIOCDLIFADDR, &req);
1688 #error not implemented
1693 netdev_bsd_do_ioctl(const char *name, struct ifreq *ifr, unsigned long cmd,
1694 const char *cmd_name)
1696 strncpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
1697 if (ioctl(af_inet_sock, cmd, ifr) == -1) {
1698 VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
1706 ifr_get_flags(const struct ifreq *ifr)
1708 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1709 return (ifr->ifr_flagshigh << 16) | ifr->ifr_flags;
1711 return ifr->ifr_flags;
1716 ifr_set_flags(struct ifreq *ifr, int flags)
1718 ifr->ifr_flags = flags;
1719 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1720 ifr->ifr_flagshigh = flags >> 16;