2 * Copyright (c) 2011, 2013, 2014 Gaetano Catalli.
3 * Copyright (c) 2013, 2014 YAMAMOTO Takashi.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #if !defined(__MACH__)
21 #include "netdev-provider.h"
25 #include <sys/types.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <sys/sockio.h>
32 #include <pcap/pcap.h>
34 #include <net/if_dl.h>
35 #include <net/if_media.h>
36 #include <net/if_tap.h>
37 #include <netinet/in.h>
38 #ifdef HAVE_NET_IF_MIB_H
39 #include <net/if_mib.h>
44 #include <sys/sysctl.h>
45 #if defined(__NetBSD__)
46 #include <net/route.h>
47 #include <netinet/if_inarp.h>
52 #include "dp-packet.h"
53 #include "dpif-netdev.h"
54 #include "openvswitch/dynamic-string.h"
55 #include "fatal-signal.h"
56 #include "openflow/openflow.h"
57 #include "ovs-thread.h"
59 #include "poll-loop.h"
60 #include "openvswitch/shash.h"
61 #include "socket-util.h"
64 #include "openvswitch/vlog.h"
66 VLOG_DEFINE_THIS_MODULE(netdev_bsd
);
69 struct netdev_rxq_bsd
{
72 /* Packet capture descriptor for a system network device.
73 * For a tap device this is NULL. */
76 /* Selectable file descriptor for the network device.
77 * This descriptor will be used for polling operations. */
84 /* Never changes after initialization. */
87 /* Protects all members below. */
88 struct ovs_mutex mutex
;
90 unsigned int cache_valid
;
93 struct eth_addr etheraddr
;
97 int tap_fd
; /* TAP character device, if any, otherwise -1. */
99 /* Used for sending packets on non-tap devices. */
106 VALID_IFINDEX
= 1 << 0,
107 VALID_ETHERADDR
= 1 << 1,
110 VALID_CARRIER
= 1 << 4
113 #define PCAP_SNAPLEN 2048
117 * Notifier used to invalidate device informations in case of status change.
119 * It will be registered with a 'rtbsd_notifier_register()' when the first
120 * device will be created with the call of either 'netdev_bsd_tap_create()' or
121 * 'netdev_bsd_system_create()'.
123 * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
124 * invalidate cached information about the device.
126 static struct rtbsd_notifier netdev_bsd_cache_notifier
;
127 static int cache_notifier_refcount
;
129 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
131 static void destroy_tap(int fd
, const char *name
);
132 static int get_flags(const struct netdev
*, int *flagsp
);
133 static int set_flags(const char *, int flags
);
134 static int do_set_addr(struct netdev
*netdev
,
135 unsigned long ioctl_nr
, const char *ioctl_name
,
136 struct in_addr addr
);
137 static int get_etheraddr(const char *netdev_name
, struct eth_addr
*ea
);
138 static int set_etheraddr(const char *netdev_name
, int hwaddr_family
,
139 int hwaddr_len
, const struct eth_addr
);
140 static int get_ifindex(const struct netdev
*, int *ifindexp
);
142 static int ifr_get_flags(const struct ifreq
*);
143 static void ifr_set_flags(struct ifreq
*, int flags
);
146 static int af_link_ioctl(unsigned long command
, const void *arg
);
149 static void netdev_bsd_run(void);
150 static int netdev_bsd_get_mtu(const struct netdev
*netdev_
, int *mtup
);
153 is_netdev_bsd_class(const struct netdev_class
*netdev_class
)
155 return netdev_class
->run
== netdev_bsd_run
;
158 static struct netdev_bsd
*
159 netdev_bsd_cast(const struct netdev
*netdev
)
161 ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev
)));
162 return CONTAINER_OF(netdev
, struct netdev_bsd
, up
);
165 static struct netdev_rxq_bsd
*
166 netdev_rxq_bsd_cast(const struct netdev_rxq
*rxq
)
168 ovs_assert(is_netdev_bsd_class(netdev_get_class(rxq
->netdev
)));
169 return CONTAINER_OF(rxq
, struct netdev_rxq_bsd
, up
);
173 netdev_get_kernel_name(const struct netdev
*netdev
)
175 return netdev_bsd_cast(netdev
)->kernel_name
;
179 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
180 * interface status changes, and eventually calls all the user callbacks.
185 rtbsd_notifier_run();
189 * Arranges for poll_block() to wake up if the "run" member function needs to
193 netdev_bsd_wait(void)
195 rtbsd_notifier_wait();
198 /* Invalidate cache in case of interface status change. */
200 netdev_bsd_cache_cb(const struct rtbsd_change
*change
,
201 void *aux OVS_UNUSED
)
203 struct netdev_bsd
*dev
;
206 struct netdev
*base_dev
= netdev_from_name(change
->if_name
);
209 const struct netdev_class
*netdev_class
=
210 netdev_get_class(base_dev
);
212 if (is_netdev_bsd_class(netdev_class
)) {
213 dev
= netdev_bsd_cast(base_dev
);
214 dev
->cache_valid
= 0;
215 netdev_change_seq_changed(base_dev
);
217 netdev_close(base_dev
);
221 * XXX the API is lacking, we should be able to iterate on the list of
222 * netdevs without having to store the info in a temp shash.
224 struct shash device_shash
;
225 struct shash_node
*node
;
227 shash_init(&device_shash
);
228 netdev_get_devices(&netdev_bsd_class
, &device_shash
);
229 SHASH_FOR_EACH (node
, &device_shash
) {
230 struct netdev
*netdev
= node
->data
;
231 dev
= netdev_bsd_cast(netdev
);
232 dev
->cache_valid
= 0;
233 netdev_change_seq_changed(netdev
);
234 netdev_close(netdev
);
236 shash_destroy(&device_shash
);
241 cache_notifier_ref(void)
245 if (!cache_notifier_refcount
) {
246 ret
= rtbsd_notifier_register(&netdev_bsd_cache_notifier
,
247 netdev_bsd_cache_cb
, NULL
);
252 cache_notifier_refcount
++;
257 cache_notifier_unref(void)
259 cache_notifier_refcount
--;
260 if (cache_notifier_refcount
== 0) {
261 rtbsd_notifier_unregister(&netdev_bsd_cache_notifier
);
266 static struct netdev
*
267 netdev_bsd_alloc(void)
269 struct netdev_bsd
*netdev
= xzalloc(sizeof *netdev
);
274 netdev_bsd_construct_system(struct netdev
*netdev_
)
276 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
277 enum netdev_flags flags
;
280 error
= cache_notifier_ref();
285 ovs_mutex_init(&netdev
->mutex
);
287 netdev
->kernel_name
= xstrdup(netdev_
->name
);
289 /* Verify that the netdev really exists by attempting to read its flags */
290 error
= netdev_get_flags(netdev_
, &flags
);
291 if (error
== ENXIO
) {
292 free(netdev
->kernel_name
);
293 cache_notifier_unref();
294 ovs_mutex_destroy(&netdev
->mutex
);
302 netdev_bsd_construct_tap(struct netdev
*netdev_
)
304 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
305 const char *name
= netdev_
->name
;
308 char *kernel_name
= NULL
;
310 error
= cache_notifier_ref();
315 memset(&ifr
, 0, sizeof(ifr
));
317 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
318 * to retrieve the name of the tap device. */
319 ovs_mutex_init(&netdev
->mutex
);
320 netdev
->tap_fd
= open("/dev/tap", O_RDWR
);
321 if (netdev
->tap_fd
< 0) {
323 VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error
));
324 goto error_unref_notifier
;
327 /* Retrieve tap name (e.g. tap0) */
328 if (ioctl(netdev
->tap_fd
, TAPGIFNAME
, &ifr
) == -1) {
329 /* XXX Need to destroy the device? */
331 close(netdev
->tap_fd
);
332 goto error_unref_notifier
;
335 /* Change the name of the tap device */
336 #if defined(SIOCSIFNAME)
337 ifr
.ifr_data
= (void *)name
;
338 error
= af_inet_ioctl(SIOCSIFNAME
, &ifr
);
340 destroy_tap(netdev
->tap_fd
, ifr
.ifr_name
);
341 goto error_unref_notifier
;
343 kernel_name
= xstrdup(name
);
346 * NetBSD doesn't support inteface renaming.
348 VLOG_INFO("tap %s is created for bridge %s", ifr
.ifr_name
, name
);
349 kernel_name
= xstrdup(ifr
.ifr_name
);
352 /* set non-blocking. */
353 error
= set_nonblocking(netdev
->tap_fd
);
355 destroy_tap(netdev
->tap_fd
, kernel_name
);
356 goto error_unref_notifier
;
360 ifr_set_flags(&ifr
, IFF_UP
);
361 ovs_strlcpy(ifr
.ifr_name
, kernel_name
, sizeof ifr
.ifr_name
);
362 error
= af_inet_ioctl(SIOCSIFFLAGS
, &ifr
);
364 destroy_tap(netdev
->tap_fd
, kernel_name
);
365 goto error_unref_notifier
;
368 netdev
->kernel_name
= kernel_name
;
372 error_unref_notifier
:
373 ovs_mutex_destroy(&netdev
->mutex
);
374 cache_notifier_unref();
381 netdev_bsd_destruct(struct netdev
*netdev_
)
383 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
385 cache_notifier_unref();
387 if (netdev
->tap_fd
>= 0) {
388 destroy_tap(netdev
->tap_fd
, netdev_get_kernel_name(netdev_
));
391 pcap_close(netdev
->pcap
);
393 free(netdev
->kernel_name
);
394 ovs_mutex_destroy(&netdev
->mutex
);
398 netdev_bsd_dealloc(struct netdev
*netdev_
)
400 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
406 netdev_bsd_open_pcap(const char *name
, pcap_t
**pcapp
, int *fdp
)
408 char errbuf
[PCAP_ERRBUF_SIZE
];
414 /* Open the pcap device. The device is opened in non-promiscuous mode
415 * because the interface flags are manually set by the caller. */
417 pcap
= pcap_open_live(name
, PCAP_SNAPLEN
, 0, 1000, errbuf
);
419 VLOG_ERR_RL(&rl
, "%s: pcap_open_live failed: %s", name
, errbuf
);
423 if (errbuf
[0] != '\0') {
424 VLOG_WARN_RL(&rl
, "%s: pcap_open_live: %s", name
, errbuf
);
427 /* Get the underlying fd. */
428 fd
= pcap_get_selectable_fd(pcap
);
430 VLOG_WARN_RL(&rl
, "%s: no selectable file descriptor", name
);
435 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
436 * on the file descriptor returned by pcap_get_selectable_fd to achieve
437 * a real non-blocking behaviour.*/
438 error
= pcap_setnonblock(pcap
, 1, errbuf
);
444 /* This call assure that reads return immediately upon packet
445 * reception. Otherwise, a read will block until either the kernel
446 * buffer becomes full or a timeout occurs. */
447 if (ioctl(fd
, BIOCIMMEDIATE
, &one
) < 0 ) {
448 VLOG_ERR_RL(&rl
, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
449 name
, ovs_strerror(errno
));
454 /* Capture only incoming packets. */
455 error
= pcap_setdirection(pcap
, PCAP_D_IN
);
474 static struct netdev_rxq
*
475 netdev_bsd_rxq_alloc(void)
477 struct netdev_rxq_bsd
*rxq
= xzalloc(sizeof *rxq
);
482 netdev_bsd_rxq_construct(struct netdev_rxq
*rxq_
)
484 struct netdev_rxq_bsd
*rxq
= netdev_rxq_bsd_cast(rxq_
);
485 struct netdev
*netdev_
= rxq
->up
.netdev
;
486 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
489 if (!strcmp(netdev_get_type(netdev_
), "tap")) {
490 rxq
->pcap_handle
= NULL
;
491 rxq
->fd
= netdev
->tap_fd
;
494 ovs_mutex_lock(&netdev
->mutex
);
495 error
= netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_
),
496 &rxq
->pcap_handle
, &rxq
->fd
);
497 ovs_mutex_unlock(&netdev
->mutex
);
504 netdev_bsd_rxq_destruct(struct netdev_rxq
*rxq_
)
506 struct netdev_rxq_bsd
*rxq
= netdev_rxq_bsd_cast(rxq_
);
508 if (rxq
->pcap_handle
) {
509 pcap_close(rxq
->pcap_handle
);
514 netdev_bsd_rxq_dealloc(struct netdev_rxq
*rxq_
)
516 struct netdev_rxq_bsd
*rxq
= netdev_rxq_bsd_cast(rxq_
);
521 /* The recv callback of the netdev class returns the number of bytes of the
524 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
525 * not make difference between a missing packet on the capture interface and
526 * an error during the file capture. We can use the pcap_dispatch() function
527 * instead, which is able to distinguish between errors and null packet.
529 * To make pcap_dispatch() returns the number of bytes read from the interface
530 * we need to define the following callback and argument.
539 * This callback will be executed on every captured packet.
541 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
542 * pcap returns a truncated packet and we follow this behavior.
544 * The argument args->retval is the packet size in bytes.
547 proc_pkt(u_char
*args_
, const struct pcap_pkthdr
*hdr
, const u_char
*packet
)
549 struct pcap_arg
*args
= ALIGNED_CAST(struct pcap_arg
*, args_
);
551 if (args
->size
< hdr
->len
) {
552 VLOG_WARN_RL(&rl
, "packet truncated");
553 args
->retval
= args
->size
;
555 args
->retval
= hdr
->len
;
558 /* copy the packet to our buffer */
559 memcpy(args
->data
, packet
, args
->retval
);
563 * This function attempts to receive a packet from the specified network
564 * device. It is assumed that the network device is a system device or a tap
565 * device opened as a system one. In this case the read operation is performed
569 netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd
*rxq
, struct dp_packet
*buffer
)
574 /* prepare the pcap argument to store the packet */
575 arg
.size
= dp_packet_tailroom(buffer
);
576 arg
.data
= dp_packet_data(buffer
);
579 ret
= pcap_dispatch(rxq
->pcap_handle
, 1, proc_pkt
, (u_char
*) &arg
);
582 dp_packet_set_size(buffer
, dp_packet_size(buffer
) + arg
.retval
);
586 if (errno
== EINTR
) {
596 * This function attempts to receive a packet from the specified network
597 * device. It is assumed that the network device is a tap device and
598 * 'rxq->fd' is initialized with the tap file descriptor.
601 netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd
*rxq
, struct dp_packet
*buffer
)
603 size_t size
= dp_packet_tailroom(buffer
);
606 ssize_t retval
= read(rxq
->fd
, dp_packet_data(buffer
), size
);
608 dp_packet_set_size(buffer
, dp_packet_size(buffer
) + retval
);
610 } else if (errno
!= EINTR
) {
611 if (errno
!= EAGAIN
) {
612 VLOG_WARN_RL(&rl
, "error receiving Ethernet packet on %s: %s",
613 ovs_strerror(errno
), netdev_rxq_get_name(&rxq
->up
));
621 netdev_bsd_rxq_recv(struct netdev_rxq
*rxq_
, struct dp_packet_batch
*batch
)
623 struct netdev_rxq_bsd
*rxq
= netdev_rxq_bsd_cast(rxq_
);
624 struct netdev
*netdev
= rxq
->up
.netdev
;
625 struct dp_packet
*packet
;
629 if (netdev_bsd_get_mtu(netdev
, &mtu
)) {
630 mtu
= ETH_PAYLOAD_MAX
;
633 packet
= dp_packet_new_with_headroom(VLAN_ETH_HEADER_LEN
+ mtu
,
635 retval
= (rxq
->pcap_handle
636 ? netdev_rxq_bsd_recv_pcap(rxq
, packet
)
637 : netdev_rxq_bsd_recv_tap(rxq
, packet
));
640 dp_packet_delete(packet
);
642 dp_packet_pad(packet
);
643 batch
->packets
[0] = packet
;
650 * Registers with the poll loop to wake up from the next call to poll_block()
651 * when a packet is ready to be received with netdev_rxq_recv() on 'rxq'.
654 netdev_bsd_rxq_wait(struct netdev_rxq
*rxq_
)
656 struct netdev_rxq_bsd
*rxq
= netdev_rxq_bsd_cast(rxq_
);
658 poll_fd_wait(rxq
->fd
, POLLIN
);
661 /* Discards all packets waiting to be received from 'rxq'. */
663 netdev_bsd_rxq_drain(struct netdev_rxq
*rxq_
)
666 struct netdev_rxq_bsd
*rxq
= netdev_rxq_bsd_cast(rxq_
);
668 strcpy(ifr
.ifr_name
, netdev_get_kernel_name(netdev_rxq_get_netdev(rxq_
)));
669 if (ioctl(rxq
->fd
, BIOCFLUSH
, &ifr
) == -1) {
670 VLOG_DBG_RL(&rl
, "%s: ioctl(BIOCFLUSH) failed: %s",
671 netdev_rxq_get_name(rxq_
), ovs_strerror(errno
));
678 * Send a packet on the specified network device. The device could be either a
679 * system or a tap device.
682 netdev_bsd_send(struct netdev
*netdev_
, int qid OVS_UNUSED
,
683 struct dp_packet_batch
*batch
, bool may_steal
)
685 struct netdev_bsd
*dev
= netdev_bsd_cast(netdev_
);
686 const char *name
= netdev_get_name(netdev_
);
690 ovs_mutex_lock(&dev
->mutex
);
691 if (dev
->tap_fd
< 0 && !dev
->pcap
) {
692 error
= netdev_bsd_open_pcap(name
, &dev
->pcap
, &dev
->fd
);
697 for (i
= 0; i
< batch
->count
; i
++) {
698 const void *data
= dp_packet_data(batch
->packets
[i
]);
699 size_t size
= dp_packet_size(batch
->packets
[i
]);
701 /* Truncate the packet if it is configured. */
702 size
-= dp_packet_get_cutlen(batch
->packets
[i
]);
706 if (dev
->tap_fd
>= 0) {
707 retval
= write(dev
->tap_fd
, data
, size
);
709 retval
= pcap_inject(dev
->pcap
, data
, size
);
712 if (errno
== EINTR
) {
716 if (error
!= EAGAIN
) {
717 VLOG_WARN_RL(&rl
, "error sending Ethernet packet on"
718 " %s: %s", name
, ovs_strerror(error
));
721 } else if (retval
!= size
) {
722 VLOG_WARN_RL(&rl
, "sent partial Ethernet packet "
723 "(%"PRIuSIZE
" bytes of "
724 "%"PRIuSIZE
") on %s", retval
, size
, name
);
732 ovs_mutex_unlock(&dev
->mutex
);
733 dp_packet_delete_batch(batch
, may_steal
);
739 * Registers with the poll loop to wake up from the next call to poll_block()
740 * when the packet transmission queue has sufficient room to transmit a packet
741 * with netdev_send().
744 netdev_bsd_send_wait(struct netdev
*netdev_
, int qid OVS_UNUSED
)
746 struct netdev_bsd
*dev
= netdev_bsd_cast(netdev_
);
748 ovs_mutex_lock(&dev
->mutex
);
749 if (dev
->tap_fd
>= 0) {
750 /* TAP device always accepts packets. */
751 poll_immediate_wake();
752 } else if (dev
->pcap
) {
753 poll_fd_wait(dev
->fd
, POLLOUT
);
755 /* We haven't even tried to send a packet yet. */
756 poll_immediate_wake();
758 ovs_mutex_unlock(&dev
->mutex
);
762 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
763 * otherwise a positive errno value.
766 netdev_bsd_set_etheraddr(struct netdev
*netdev_
,
767 const struct eth_addr mac
)
769 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
772 ovs_mutex_lock(&netdev
->mutex
);
773 if (!(netdev
->cache_valid
& VALID_ETHERADDR
)
774 || !eth_addr_equals(netdev
->etheraddr
, mac
)) {
775 error
= set_etheraddr(netdev_get_kernel_name(netdev_
), AF_LINK
,
778 netdev
->cache_valid
|= VALID_ETHERADDR
;
779 netdev
->etheraddr
= mac
;
780 netdev_change_seq_changed(netdev_
);
783 ovs_mutex_unlock(&netdev
->mutex
);
789 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
790 * free the returned buffer.
793 netdev_bsd_get_etheraddr(const struct netdev
*netdev_
, struct eth_addr
*mac
)
795 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
798 ovs_mutex_lock(&netdev
->mutex
);
799 if (!(netdev
->cache_valid
& VALID_ETHERADDR
)) {
800 error
= get_etheraddr(netdev_get_kernel_name(netdev_
),
803 netdev
->cache_valid
|= VALID_ETHERADDR
;
807 *mac
= netdev
->etheraddr
;
809 ovs_mutex_unlock(&netdev
->mutex
);
815 * Returns the maximum size of transmitted (and received) packets on 'netdev',
816 * in bytes, not including the hardware header; thus, this is typically 1500
817 * bytes for Ethernet devices.
820 netdev_bsd_get_mtu(const struct netdev
*netdev_
, int *mtup
)
822 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
825 ovs_mutex_lock(&netdev
->mutex
);
826 if (!(netdev
->cache_valid
& VALID_MTU
)) {
829 error
= af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_
), &ifr
,
830 SIOCGIFMTU
, "SIOCGIFMTU");
832 netdev
->mtu
= ifr
.ifr_mtu
;
833 netdev
->cache_valid
|= VALID_MTU
;
839 ovs_mutex_unlock(&netdev
->mutex
);
845 netdev_bsd_get_ifindex(const struct netdev
*netdev_
)
847 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
850 ovs_mutex_lock(&netdev
->mutex
);
851 error
= get_ifindex(netdev_
, &ifindex
);
852 ovs_mutex_unlock(&netdev
->mutex
);
854 return error
? -error
: ifindex
;
858 netdev_bsd_get_carrier(const struct netdev
*netdev_
, bool *carrier
)
860 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
863 ovs_mutex_lock(&netdev
->mutex
);
864 if (!(netdev
->cache_valid
& VALID_CARRIER
)) {
865 struct ifmediareq ifmr
;
867 memset(&ifmr
, 0, sizeof(ifmr
));
868 ovs_strlcpy(ifmr
.ifm_name
, netdev_get_kernel_name(netdev_
),
869 sizeof ifmr
.ifm_name
);
871 error
= af_inet_ioctl(SIOCGIFMEDIA
, &ifmr
);
873 netdev
->carrier
= (ifmr
.ifm_status
& IFM_ACTIVE
) == IFM_ACTIVE
;
874 netdev
->cache_valid
|= VALID_CARRIER
;
876 /* If the interface doesn't report whether the media is active,
877 * just assume it is active. */
878 if ((ifmr
.ifm_status
& IFM_AVALID
) == 0) {
879 netdev
->carrier
= true;
882 VLOG_DBG_RL(&rl
, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
883 netdev_get_name(netdev_
), ovs_strerror(error
));
887 *carrier
= netdev
->carrier
;
889 ovs_mutex_unlock(&netdev
->mutex
);
895 convert_stats_system(struct netdev_stats
*stats
, const struct if_data
*ifd
)
898 * note: UINT64_MAX means unsupported
900 stats
->rx_packets
= ifd
->ifi_ipackets
;
901 stats
->tx_packets
= ifd
->ifi_opackets
;
902 stats
->rx_bytes
= ifd
->ifi_obytes
;
903 stats
->tx_bytes
= ifd
->ifi_ibytes
;
904 stats
->rx_errors
= ifd
->ifi_ierrors
;
905 stats
->tx_errors
= ifd
->ifi_oerrors
;
906 stats
->rx_dropped
= ifd
->ifi_iqdrops
;
907 stats
->tx_dropped
= UINT64_MAX
;
908 stats
->multicast
= ifd
->ifi_imcasts
;
909 stats
->collisions
= ifd
->ifi_collisions
;
910 stats
->rx_length_errors
= UINT64_MAX
;
911 stats
->rx_over_errors
= UINT64_MAX
;
912 stats
->rx_crc_errors
= UINT64_MAX
;
913 stats
->rx_frame_errors
= UINT64_MAX
;
914 stats
->rx_fifo_errors
= UINT64_MAX
;
915 stats
->rx_missed_errors
= UINT64_MAX
;
916 stats
->tx_aborted_errors
= UINT64_MAX
;
917 stats
->tx_carrier_errors
= UINT64_MAX
;
918 stats
->tx_fifo_errors
= UINT64_MAX
;
919 stats
->tx_heartbeat_errors
= UINT64_MAX
;
920 stats
->tx_window_errors
= UINT64_MAX
;
924 convert_stats_tap(struct netdev_stats
*stats
, const struct if_data
*ifd
)
927 * Similar to convert_stats_system but swapping rxq and tx
928 * because 'ifd' is stats for the network interface side of the
929 * tap device and what the caller wants is one for the character
932 * note: UINT64_MAX means unsupported
934 stats
->rx_packets
= ifd
->ifi_opackets
;
935 stats
->tx_packets
= ifd
->ifi_ipackets
;
936 stats
->rx_bytes
= ifd
->ifi_ibytes
;
937 stats
->tx_bytes
= ifd
->ifi_obytes
;
938 stats
->rx_errors
= ifd
->ifi_oerrors
;
939 stats
->tx_errors
= ifd
->ifi_ierrors
;
940 stats
->rx_dropped
= UINT64_MAX
;
941 stats
->tx_dropped
= ifd
->ifi_iqdrops
;
942 stats
->multicast
= ifd
->ifi_omcasts
;
943 stats
->collisions
= UINT64_MAX
;
944 stats
->rx_length_errors
= UINT64_MAX
;
945 stats
->rx_over_errors
= UINT64_MAX
;
946 stats
->rx_crc_errors
= UINT64_MAX
;
947 stats
->rx_frame_errors
= UINT64_MAX
;
948 stats
->rx_fifo_errors
= UINT64_MAX
;
949 stats
->rx_missed_errors
= UINT64_MAX
;
950 stats
->tx_aborted_errors
= UINT64_MAX
;
951 stats
->tx_carrier_errors
= UINT64_MAX
;
952 stats
->tx_fifo_errors
= UINT64_MAX
;
953 stats
->tx_heartbeat_errors
= UINT64_MAX
;
954 stats
->tx_window_errors
= UINT64_MAX
;
958 convert_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
,
959 const struct if_data
*ifd
)
961 if (netdev_bsd_cast(netdev
)->tap_fd
== -1) {
962 convert_stats_system(stats
, ifd
);
964 convert_stats_tap(stats
, ifd
);
968 /* Retrieves current device stats for 'netdev'. */
970 netdev_bsd_get_stats(const struct netdev
*netdev_
, struct netdev_stats
*stats
)
972 #if defined(__FreeBSD__)
976 struct ifmibdata ifmd
;
981 mib
[2] = NETLINK_GENERIC
;
982 mib
[3] = IFMIB_SYSTEM
;
983 mib
[4] = IFMIB_IFCOUNT
;
985 len
= sizeof(if_count
);
987 if (sysctl(mib
, 5, &if_count
, &len
, (void *)0, 0) == -1) {
988 VLOG_DBG_RL(&rl
, "%s: sysctl failed: %s",
989 netdev_get_name(netdev_
), ovs_strerror(errno
));
993 mib
[5] = IFDATA_GENERAL
;
994 mib
[3] = IFMIB_IFDATA
;
996 for (i
= 1; i
<= if_count
; i
++) {
998 if (sysctl(mib
, 6, &ifmd
, &len
, (void *)0, 0) == -1) {
999 VLOG_DBG_RL(&rl
, "%s: sysctl failed: %s",
1000 netdev_get_name(netdev_
), ovs_strerror(errno
));
1002 } else if (!strcmp(ifmd
.ifmd_name
, netdev_get_name(netdev_
))) {
1003 convert_stats(netdev_
, stats
, &ifmd
.ifmd_data
);
1009 #elif defined(__NetBSD__)
1010 struct ifdatareq ifdr
;
1013 memset(&ifdr
, 0, sizeof(ifdr
));
1014 ovs_strlcpy(ifdr
.ifdr_name
, netdev_get_kernel_name(netdev_
),
1015 sizeof(ifdr
.ifdr_name
));
1016 error
= af_link_ioctl(SIOCGIFDATA
, &ifdr
);
1018 convert_stats(netdev_
, stats
, &ifdr
.ifdr_data
);
1022 #error not implemented
1027 netdev_bsd_parse_media(int media
)
1029 uint32_t supported
= 0;
1030 bool half_duplex
= media
& IFM_HDX
? true : false;
1032 switch (IFM_SUBTYPE(media
)) {
1037 supported
|= half_duplex
? NETDEV_F_10MB_HD
: NETDEV_F_10MB_FD
;
1038 supported
|= NETDEV_F_COPPER
;
1042 supported
|= half_duplex
? NETDEV_F_10MB_HD
: NETDEV_F_10MB_FD
;
1043 supported
|= NETDEV_F_FIBER
;
1050 supported
|= half_duplex
? NETDEV_F_100MB_HD
: NETDEV_F_100MB_FD
;
1051 supported
|= NETDEV_F_COPPER
;
1055 supported
|= half_duplex
? NETDEV_F_100MB_HD
: NETDEV_F_100MB_FD
;
1056 supported
|= NETDEV_F_FIBER
;
1061 supported
|= half_duplex
? NETDEV_F_1GB_HD
: NETDEV_F_1GB_FD
;
1062 supported
|= NETDEV_F_COPPER
;
1067 supported
|= half_duplex
? NETDEV_F_1GB_HD
: NETDEV_F_1GB_FD
;
1068 supported
|= NETDEV_F_FIBER
;
1072 supported
|= NETDEV_F_10GB_FD
;
1073 supported
|= NETDEV_F_COPPER
;
1078 supported
|= NETDEV_F_10GB_FD
;
1079 supported
|= NETDEV_F_FIBER
;
1086 if (IFM_SUBTYPE(media
) == IFM_AUTO
) {
1087 supported
|= NETDEV_F_AUTONEG
;
1090 if (media & IFM_ETH_FMASK) {
1091 supported |= NETDEV_F_PAUSE;
1099 * Stores the features supported by 'netdev' into each of '*current',
1100 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1101 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1102 * successful, otherwise a positive errno value. On failure, all of the
1103 * passed-in values are set to 0.
1106 netdev_bsd_get_features(const struct netdev
*netdev
,
1107 enum netdev_features
*current
, uint32_t *advertised
,
1108 enum netdev_features
*supported
, uint32_t *peer
)
1110 struct ifmediareq ifmr
;
1116 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1118 memset(&ifmr
, 0, sizeof(ifmr
));
1119 ovs_strlcpy(ifmr
.ifm_name
, netdev_get_name(netdev
), sizeof ifmr
.ifm_name
);
1121 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
1122 * number of supported modes, and a second with a buffer to retrieve
1124 error
= af_inet_ioctl(SIOCGIFMEDIA
, &ifmr
);
1126 VLOG_DBG_RL(&rl
, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1127 netdev_get_name(netdev
), ovs_strerror(error
));
1131 media_list
= xcalloc(ifmr
.ifm_count
, sizeof(int));
1132 ifmr
.ifm_ulist
= media_list
;
1134 if (IFM_TYPE(ifmr
.ifm_current
) != IFM_ETHER
) {
1135 VLOG_DBG_RL(&rl
, "%s: doesn't appear to be ethernet",
1136 netdev_get_name(netdev
));
1141 error
= af_inet_ioctl(SIOCGIFMEDIA
, &ifmr
);
1143 VLOG_DBG_RL(&rl
, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1144 netdev_get_name(netdev
), ovs_strerror(error
));
1148 /* Current settings. */
1149 *current
= netdev_bsd_parse_media(ifmr
.ifm_active
);
1151 /* Advertised features. */
1152 *advertised
= netdev_bsd_parse_media(ifmr
.ifm_current
);
1154 /* Supported features. */
1156 for (i
= 0; i
< ifmr
.ifm_count
; i
++) {
1157 *supported
|= netdev_bsd_parse_media(ifmr
.ifm_ulist
[i
]);
1160 /* Peer advertisements. */
1161 *peer
= 0; /* XXX */
1170 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1171 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1172 * positive errno value.
1175 netdev_bsd_set_in4(struct netdev
*netdev_
, struct in_addr addr
,
1176 struct in_addr mask
)
1178 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
1181 ovs_mutex_lock(&netdev
->mutex
);
1182 error
= do_set_addr(netdev_
, SIOCSIFADDR
, "SIOCSIFADDR", addr
);
1184 if (addr
.s_addr
!= INADDR_ANY
) {
1185 error
= do_set_addr(netdev_
, SIOCSIFNETMASK
,
1186 "SIOCSIFNETMASK", mask
);
1188 netdev_change_seq_changed(netdev_
);
1190 ovs_mutex_unlock(&netdev
->mutex
);
1196 netdev_bsd_get_addr_list(const struct netdev
*netdev_
,
1197 struct in6_addr
**addr
, struct in6_addr
**mask
, int *n_cnt
)
1199 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
1202 if (!(netdev
->cache_valid
& VALID_IN
)) {
1203 netdev_get_addrs_list_flush();
1205 error
= netdev_get_addrs(netdev_get_name(netdev_
), addr
, mask
, n_cnt
);
1207 netdev
->cache_valid
|= VALID_IN
;
1212 #if defined(__NetBSD__)
1214 netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name
)
1216 char *ovs_name
= NULL
;
1217 struct shash device_shash
;
1218 struct shash_node
*node
;
1220 shash_init(&device_shash
);
1221 netdev_get_devices(&netdev_tap_class
, &device_shash
);
1222 SHASH_FOR_EACH(node
, &device_shash
) {
1223 struct netdev
*netdev
= node
->data
;
1224 struct netdev_bsd
* const dev
= netdev_bsd_cast(netdev
);
1226 if (!strcmp(dev
->kernel_name
, kernel_name
)) {
1228 ovs_name
= xstrdup(netdev_get_name(&dev
->up
));
1230 netdev_close(netdev
);
1232 shash_destroy(&device_shash
);
1234 return ovs_name
? ovs_name
: xstrdup(kernel_name
);
1239 netdev_bsd_get_next_hop(const struct in_addr
*host OVS_UNUSED
,
1240 struct in_addr
*next_hop OVS_UNUSED
,
1241 char **netdev_name OVS_UNUSED
)
1243 #if defined(__NetBSD__)
1245 struct sockaddr_in sin
;
1246 struct sockaddr_dl sdl
;
1253 struct rt_msghdr
*rtm
= &buf
.h
;
1254 const pid_t pid
= getpid();
1257 bool gateway
= false;
1258 char *ifname
= NULL
;
1261 memset(next_hop
, 0, sizeof(*next_hop
));
1262 *netdev_name
= NULL
;
1264 memset(&sin
, 0, sizeof(sin
));
1265 sin
.sin_len
= sizeof(sin
);
1266 sin
.sin_family
= AF_INET
;
1268 sin
.sin_addr
= *host
;
1270 memset(&sdl
, 0, sizeof(sdl
));
1271 sdl
.sdl_len
= sizeof(sdl
);
1272 sdl
.sdl_family
= AF_LINK
;
1274 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
1275 memset(&buf
, 0, sizeof(buf
));
1276 rtm
->rtm_flags
= RTF_HOST
|RTF_UP
;
1277 rtm
->rtm_version
= RTM_VERSION
;
1278 rtm
->rtm_addrs
= RTA_DST
|RTA_IFP
;
1279 cp
= (void *)&buf
.space
;
1280 memcpy(cp
, &sin
, sizeof(sin
));
1281 RT_ADVANCE(cp
, (struct sockaddr
*)(void *)&sin
);
1282 memcpy(cp
, &sdl
, sizeof(sdl
));
1283 RT_ADVANCE(cp
, (struct sockaddr
*)(void *)&sdl
);
1284 rtm
->rtm_msglen
= cp
- (char *)(void *)rtm
;
1285 rtm
->rtm_seq
= ++seq
;
1286 rtm
->rtm_type
= RTM_GET
;
1288 write(s
, rtm
, rtm
->rtm_msglen
);
1289 memset(&buf
, 0, sizeof(buf
));
1291 ssz
= read(s
, &buf
, sizeof(buf
));
1292 } while (ssz
> 0 && (rtm
->rtm_seq
!= seq
|| rtm
->rtm_pid
!= pid
));
1293 saved_errno
= errno
;
1299 return EPIPE
; /* XXX */
1301 cp
= (void *)&buf
.space
;
1302 for (i
= 1; i
; i
<<= 1) {
1303 if ((rtm
->rtm_addrs
& i
) != 0) {
1304 const struct sockaddr
*sa
= (const void *)cp
;
1306 if ((i
== RTA_GATEWAY
) && sa
->sa_family
== AF_INET
) {
1307 const struct sockaddr_in
* const sin
=
1308 ALIGNED_CAST(const struct sockaddr_in
*, sa
);
1310 *next_hop
= sin
->sin_addr
;
1313 if ((i
== RTA_IFP
) && sa
->sa_family
== AF_LINK
) {
1314 const struct sockaddr_dl
* const sdl
=
1315 ALIGNED_CAST(const struct sockaddr_dl
*, sa
);
1318 kernel_name
= xmemdup0(sdl
->sdl_data
, sdl
->sdl_nlen
);
1319 ifname
= netdev_bsd_kernel_name_to_ovs_name(kernel_name
);
1325 if (ifname
== NULL
) {
1331 *netdev_name
= ifname
;
1332 VLOG_DBG("host " IP_FMT
" next-hop " IP_FMT
" if %s",
1333 IP_ARGS(host
->s_addr
), IP_ARGS(next_hop
->s_addr
), *netdev_name
);
1341 netdev_bsd_arp_lookup(const struct netdev
*netdev OVS_UNUSED
,
1342 ovs_be32 ip OVS_UNUSED
,
1343 struct eth_addr
*mac OVS_UNUSED
)
1345 #if defined(__NetBSD__)
1346 const struct rt_msghdr
*rtm
;
1359 mib
[4] = NET_RT_FLAGS
;
1360 mib
[5] = RTF_LLINFO
;
1361 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) == -1) {
1365 buf
= xmalloc(needed
);
1366 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) == -1) {
1371 for (cp
= buf
; cp
< ep
; cp
+= rtm
->rtm_msglen
) {
1372 const struct sockaddr_inarp
*sina
;
1373 const struct sockaddr_dl
*sdl
;
1375 rtm
= (const void *)cp
;
1376 sina
= (const void *)(rtm
+ 1);
1377 if (ip
!= sina
->sin_addr
.s_addr
) {
1380 sdl
= (const void *)
1381 ((const char *)(const void *)sina
+ RT_ROUNDUP(sina
->sin_len
));
1382 if (sdl
->sdl_alen
== ETH_ADDR_LEN
) {
1383 memcpy(mac
, &sdl
->sdl_data
[sdl
->sdl_nlen
], ETH_ADDR_LEN
);
1398 make_in4_sockaddr(struct sockaddr
*sa
, struct in_addr addr
)
1400 struct sockaddr_in sin
;
1401 memset(&sin
, 0, sizeof sin
);
1402 sin
.sin_family
= AF_INET
;
1403 sin
.sin_addr
= addr
;
1406 memset(sa
, 0, sizeof *sa
);
1407 memcpy(sa
, &sin
, sizeof sin
);
1411 do_set_addr(struct netdev
*netdev
,
1412 unsigned long ioctl_nr
, const char *ioctl_name
,
1413 struct in_addr addr
)
1416 make_in4_sockaddr(&ifr
.ifr_addr
, addr
);
1417 return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev
), &ifr
, ioctl_nr
,
1422 nd_to_iff_flags(enum netdev_flags nd
)
1425 if (nd
& NETDEV_UP
) {
1428 if (nd
& NETDEV_PROMISC
) {
1430 #if defined(IFF_PPROMISC)
1431 iff
|= IFF_PPROMISC
;
1434 if (nd
& NETDEV_LOOPBACK
) {
1435 iff
|= IFF_LOOPBACK
;
1441 iff_to_nd_flags(int iff
)
1443 enum netdev_flags nd
= 0;
1447 if (iff
& IFF_PROMISC
) {
1448 nd
|= NETDEV_PROMISC
;
1450 if (iff
& IFF_LOOPBACK
) {
1451 nd
|= NETDEV_LOOPBACK
;
1457 netdev_bsd_update_flags(struct netdev
*netdev_
, enum netdev_flags off
,
1458 enum netdev_flags on
, enum netdev_flags
*old_flagsp
)
1460 int old_flags
, new_flags
;
1463 error
= get_flags(netdev_
, &old_flags
);
1465 *old_flagsp
= iff_to_nd_flags(old_flags
);
1466 new_flags
= (old_flags
& ~nd_to_iff_flags(off
)) | nd_to_iff_flags(on
);
1467 if (new_flags
!= old_flags
) {
1468 error
= set_flags(netdev_get_kernel_name(netdev_
), new_flags
);
1469 netdev_change_seq_changed(netdev_
);
1475 /* Linux has also different GET_STATS, SET_STATS,
1478 #define NETDEV_BSD_CLASS(NAME, CONSTRUCT, \
1482 false, /* is_pmd */ \
1489 netdev_bsd_destruct, \
1490 netdev_bsd_dealloc, \
1491 NULL, /* get_config */ \
1492 NULL, /* set_config */ \
1493 NULL, /* get_tunnel_config */ \
1494 NULL, /* build header */ \
1495 NULL, /* push header */ \
1496 NULL, /* pop header */ \
1497 NULL, /* get_numa_id */ \
1498 NULL, /* set_tx_multiq */ \
1501 netdev_bsd_send_wait, \
1503 netdev_bsd_set_etheraddr, \
1504 netdev_bsd_get_etheraddr, \
1505 netdev_bsd_get_mtu, \
1506 NULL, /* set_mtu */ \
1507 netdev_bsd_get_ifindex, \
1508 netdev_bsd_get_carrier, \
1509 NULL, /* get_carrier_resets */ \
1510 NULL, /* set_miimon_interval */ \
1511 netdev_bsd_get_stats, \
1514 NULL, /* set_advertisement */ \
1515 NULL, /* set_policing */ \
1516 NULL, /* get_qos_type */ \
1517 NULL, /* get_qos_capabilities */ \
1518 NULL, /* get_qos */ \
1519 NULL, /* set_qos */ \
1520 NULL, /* get_queue */ \
1521 NULL, /* set_queue */ \
1522 NULL, /* delete_queue */ \
1523 NULL, /* get_queue_stats */ \
1524 NULL, /* queue_dump_start */ \
1525 NULL, /* queue_dump_next */ \
1526 NULL, /* queue_dump_done */ \
1527 NULL, /* dump_queue_stats */ \
1529 netdev_bsd_set_in4, \
1530 netdev_bsd_get_addr_list, \
1531 NULL, /* add_router */ \
1532 netdev_bsd_get_next_hop, \
1533 NULL, /* get_status */ \
1534 netdev_bsd_arp_lookup, /* arp_lookup */ \
1536 netdev_bsd_update_flags, \
1537 NULL, /* reconfigure */ \
1539 netdev_bsd_rxq_alloc, \
1540 netdev_bsd_rxq_construct, \
1541 netdev_bsd_rxq_destruct, \
1542 netdev_bsd_rxq_dealloc, \
1543 netdev_bsd_rxq_recv, \
1544 netdev_bsd_rxq_wait, \
1545 netdev_bsd_rxq_drain, \
1548 const struct netdev_class netdev_bsd_class
=
1551 netdev_bsd_construct_system
,
1552 netdev_bsd_get_features
);
1554 const struct netdev_class netdev_tap_class
=
1557 netdev_bsd_construct_tap
,
1558 netdev_bsd_get_features
);
1562 destroy_tap(int fd
, const char *name
)
1567 strcpy(ifr
.ifr_name
, name
);
1568 /* XXX What to do if this call fails? */
1569 af_inet_ioctl(SIOCIFDESTROY
, &ifr
);
1573 get_flags(const struct netdev
*netdev
, int *flags
)
1578 error
= af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev
), &ifr
,
1579 SIOCGIFFLAGS
, "SIOCGIFFLAGS");
1581 *flags
= ifr_get_flags(&ifr
);
1587 set_flags(const char *name
, int flags
)
1591 ifr_set_flags(&ifr
, flags
);
1593 return af_inet_ifreq_ioctl(name
, &ifr
, SIOCSIFFLAGS
, "SIOCSIFFLAGS");
1597 get_ifindex(const struct netdev
*netdev_
, int *ifindexp
)
1599 struct netdev_bsd
*netdev
= netdev_bsd_cast(netdev_
);
1601 if (!(netdev
->cache_valid
& VALID_IFINDEX
)) {
1602 int ifindex
= if_nametoindex(netdev_get_name(netdev_
));
1606 netdev
->cache_valid
|= VALID_IFINDEX
;
1607 netdev
->ifindex
= ifindex
;
1609 *ifindexp
= netdev
->ifindex
;
1614 get_etheraddr(const char *netdev_name
, struct eth_addr
*ea
)
1616 struct ifaddrs
*head
;
1617 struct ifaddrs
*ifa
;
1618 struct sockaddr_dl
*sdl
;
1620 if (getifaddrs(&head
) != 0) {
1621 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name
,
1622 ovs_strerror(errno
));
1626 for (ifa
= head
; ifa
; ifa
= ifa
->ifa_next
) {
1627 if (ifa
->ifa_addr
->sa_family
== AF_LINK
) {
1628 if (!strcmp(ifa
->ifa_name
, netdev_name
)) {
1629 sdl
= ALIGNED_CAST(struct sockaddr_dl
*, ifa
->ifa_addr
);
1631 memcpy(ea
, LLADDR(sdl
), sdl
->sdl_alen
);
1639 VLOG_ERR("could not find ethernet address for %s device", netdev_name
);
1645 set_etheraddr(const char *netdev_name OVS_UNUSED
, int hwaddr_family OVS_UNUSED
,
1646 int hwaddr_len OVS_UNUSED
,
1647 const struct eth_addr mac OVS_UNUSED
)
1649 #if defined(__FreeBSD__)
1653 memset(&ifr
, 0, sizeof ifr
);
1654 ovs_strlcpy(ifr
.ifr_name
, netdev_name
, sizeof ifr
.ifr_name
);
1655 ifr
.ifr_addr
.sa_family
= hwaddr_family
;
1656 ifr
.ifr_addr
.sa_len
= hwaddr_len
;
1657 memcpy(ifr
.ifr_addr
.sa_data
, &mac
, hwaddr_len
);
1658 error
= af_inet_ioctl(SIOCSIFLLADDR
, &ifr
);
1660 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1661 netdev_name
, ovs_strerror(error
));
1665 #elif defined(__NetBSD__)
1666 struct if_laddrreq req
;
1667 struct sockaddr_dl
*sdl
;
1668 struct sockaddr_storage oldaddr
;
1672 * get the old address, add new one, and then remove old one.
1675 if (hwaddr_len
!= ETH_ADDR_LEN
) {
1676 /* just to be safe about sockaddr storage size */
1679 memset(&req
, 0, sizeof(req
));
1680 ovs_strlcpy(req
.iflr_name
, netdev_name
, sizeof(req
.iflr_name
));
1681 req
.addr
.ss_len
= sizeof(req
.addr
);
1682 req
.addr
.ss_family
= hwaddr_family
;
1683 sdl
= (struct sockaddr_dl
*)&req
.addr
;
1684 sdl
->sdl_alen
= hwaddr_len
;
1686 error
= af_link_ioctl(SIOCGLIFADDR
, &req
);
1690 if (!memcmp(&sdl
->sdl_data
[sdl
->sdl_nlen
], &mac
, hwaddr_len
)) {
1695 memset(&req
, 0, sizeof(req
));
1696 ovs_strlcpy(req
.iflr_name
, netdev_name
, sizeof(req
.iflr_name
));
1697 req
.flags
= IFLR_ACTIVE
;
1698 sdl
= (struct sockaddr_dl
*)&req
.addr
;
1699 sdl
->sdl_len
= offsetof(struct sockaddr_dl
, sdl_data
) + hwaddr_len
;
1700 sdl
->sdl_alen
= hwaddr_len
;
1701 sdl
->sdl_family
= hwaddr_family
;
1702 memcpy(sdl
->sdl_data
, &mac
, hwaddr_len
);
1703 error
= af_link_ioctl(SIOCALIFADDR
, &req
);
1708 memset(&req
, 0, sizeof(req
));
1709 ovs_strlcpy(req
.iflr_name
, netdev_name
, sizeof(req
.iflr_name
));
1711 return af_link_ioctl(SIOCDLIFADDR
, &req
);
1713 #error not implemented
1718 ifr_get_flags(const struct ifreq
*ifr
)
1720 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1721 return (ifr
->ifr_flagshigh
<< 16) | (ifr
->ifr_flags
& 0xffff);
1723 return ifr
->ifr_flags
;
1728 ifr_set_flags(struct ifreq
*ifr
, int flags
)
1730 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1731 ifr
->ifr_flags
= flags
& 0xffff;
1732 ifr
->ifr_flagshigh
= flags
>> 16;
1734 ifr
->ifr_flags
= flags
;
1738 #if defined(__NetBSD__)
1739 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
1740 * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
1742 af_link_ioctl(unsigned long command
, const void *arg
)
1744 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
1747 if (ovsthread_once_start(&once
)) {
1748 sock
= socket(AF_LINK
, SOCK_DGRAM
, 0);
1751 VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno
));
1753 ovsthread_once_done(&once
);
1756 return (sock
< 0 ? -sock
1757 : ioctl(sock
, command
, arg
) == -1 ? errno
1761 #endif /* !defined(__MACH__) */