]> git.proxmox.com Git - mirror_ovs.git/blob - lib/netdev-bsd.c
netdev: Add function for configuring tx and rx queues.
[mirror_ovs.git] / lib / netdev-bsd.c
1 /*
2 * Copyright (c) 2011, 2013, 2014 Gaetano Catalli.
3 * Copyright (c) 2013, 2014 YAMAMOTO Takashi.
4 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18 #include <config.h>
19
20 #include "netdev-provider.h"
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <sys/sockio.h>
29 #include <ifaddrs.h>
30 #include <pcap/pcap.h>
31 #include <net/if.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>
38 #endif
39 #include <poll.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sys/sysctl.h>
43 #if defined(__NetBSD__)
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/if_inarp.h>
47 #endif
48
49 #include "rtbsd.h"
50 #include "coverage.h"
51 #include "dpif-netdev.h"
52 #include "dynamic-string.h"
53 #include "fatal-signal.h"
54 #include "ofpbuf.h"
55 #include "openflow/openflow.h"
56 #include "ovs-thread.h"
57 #include "packet-dpif.h"
58 #include "packets.h"
59 #include "poll-loop.h"
60 #include "shash.h"
61 #include "socket-util.h"
62 #include "svec.h"
63 #include "util.h"
64 #include "vlog.h"
65
66 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
67
68 \f
69 struct netdev_rxq_bsd {
70 struct netdev_rxq up;
71
72 /* Packet capture descriptor for a system network device.
73 * For a tap device this is NULL. */
74 pcap_t *pcap_handle;
75
76 /* Selectable file descriptor for the network device.
77 * This descriptor will be used for polling operations. */
78 int fd;
79 };
80
81 struct netdev_bsd {
82 struct netdev up;
83
84 /* Never changes after initialization. */
85 char *kernel_name;
86
87 /* Protects all members below. */
88 struct ovs_mutex mutex;
89
90 unsigned int cache_valid;
91
92 int ifindex;
93 uint8_t etheraddr[ETH_ADDR_LEN];
94 struct in_addr in4;
95 struct in_addr netmask;
96 struct in6_addr in6;
97 int mtu;
98 int carrier;
99
100 int tap_fd; /* TAP character device, if any, otherwise -1. */
101
102 /* Used for sending packets on non-tap devices. */
103 pcap_t *pcap;
104 int fd;
105 };
106
107
108 enum {
109 VALID_IFINDEX = 1 << 0,
110 VALID_ETHERADDR = 1 << 1,
111 VALID_IN4 = 1 << 2,
112 VALID_IN6 = 1 << 3,
113 VALID_MTU = 1 << 4,
114 VALID_CARRIER = 1 << 5
115 };
116
117 #define PCAP_SNAPLEN 2048
118
119
120 /*
121 * Notifier used to invalidate device informations in case of status change.
122 *
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()'.
126 *
127 * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
128 * invalidate cached information about the device.
129 */
130 static struct rtbsd_notifier netdev_bsd_cache_notifier;
131 static int cache_notifier_refcount;
132
133 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
134
135 static void destroy_tap(int fd, const char *name);
136 static int get_flags(const struct netdev *, int *flagsp);
137 static int set_flags(const char *, int flags);
138 static int do_set_addr(struct netdev *netdev,
139 unsigned long ioctl_nr, const char *ioctl_name,
140 struct in_addr addr);
141 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
142 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
143 int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
144 static int get_ifindex(const struct netdev *, int *ifindexp);
145
146 static int ifr_get_flags(const struct ifreq *);
147 static void ifr_set_flags(struct ifreq *, int flags);
148
149 #ifdef __NetBSD__
150 static int af_link_ioctl(unsigned long command, const void *arg);
151 #endif
152
153 static void netdev_bsd_run(void);
154 static int netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup);
155
156 static bool
157 is_netdev_bsd_class(const struct netdev_class *netdev_class)
158 {
159 return netdev_class->run == netdev_bsd_run;
160 }
161
162 static struct netdev_bsd *
163 netdev_bsd_cast(const struct netdev *netdev)
164 {
165 ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev)));
166 return CONTAINER_OF(netdev, struct netdev_bsd, up);
167 }
168
169 static struct netdev_rxq_bsd *
170 netdev_rxq_bsd_cast(const struct netdev_rxq *rxq)
171 {
172 ovs_assert(is_netdev_bsd_class(netdev_get_class(rxq->netdev)));
173 return CONTAINER_OF(rxq, struct netdev_rxq_bsd, up);
174 }
175
176 static const char *
177 netdev_get_kernel_name(const struct netdev *netdev)
178 {
179 return netdev_bsd_cast(netdev)->kernel_name;
180 }
181
182 /*
183 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
184 * interface status changes, and eventually calls all the user callbacks.
185 */
186 static void
187 netdev_bsd_run(void)
188 {
189 rtbsd_notifier_run();
190 }
191
192 /*
193 * Arranges for poll_block() to wake up if the "run" member function needs to
194 * be called.
195 */
196 static void
197 netdev_bsd_wait(void)
198 {
199 rtbsd_notifier_wait();
200 }
201
202 /* Invalidate cache in case of interface status change. */
203 static void
204 netdev_bsd_cache_cb(const struct rtbsd_change *change,
205 void *aux OVS_UNUSED)
206 {
207 struct netdev_bsd *dev;
208
209 if (change) {
210 struct netdev *base_dev = netdev_from_name(change->if_name);
211
212 if (base_dev) {
213 const struct netdev_class *netdev_class =
214 netdev_get_class(base_dev);
215
216 if (is_netdev_bsd_class(netdev_class)) {
217 dev = netdev_bsd_cast(base_dev);
218 dev->cache_valid = 0;
219 netdev_change_seq_changed(base_dev);
220 }
221 netdev_close(base_dev);
222 }
223 } else {
224 /*
225 * XXX the API is lacking, we should be able to iterate on the list of
226 * netdevs without having to store the info in a temp shash.
227 */
228 struct shash device_shash;
229 struct shash_node *node;
230
231 shash_init(&device_shash);
232 netdev_get_devices(&netdev_bsd_class, &device_shash);
233 SHASH_FOR_EACH (node, &device_shash) {
234 struct netdev *netdev = node->data;
235 dev = netdev_bsd_cast(netdev);
236 dev->cache_valid = 0;
237 netdev_change_seq_changed(netdev);
238 netdev_close(netdev);
239 }
240 shash_destroy(&device_shash);
241 }
242 }
243
244 static int
245 cache_notifier_ref(void)
246 {
247 int ret = 0;
248
249 if (!cache_notifier_refcount) {
250 ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
251 netdev_bsd_cache_cb, NULL);
252 if (ret) {
253 return ret;
254 }
255 }
256 cache_notifier_refcount++;
257 return 0;
258 }
259
260 static int
261 cache_notifier_unref(void)
262 {
263 cache_notifier_refcount--;
264 if (cache_notifier_refcount == 0) {
265 rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
266 }
267 return 0;
268 }
269
270 static struct netdev *
271 netdev_bsd_alloc(void)
272 {
273 struct netdev_bsd *netdev = xzalloc(sizeof *netdev);
274 return &netdev->up;
275 }
276
277 static int
278 netdev_bsd_construct_system(struct netdev *netdev_)
279 {
280 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
281 enum netdev_flags flags;
282 int error;
283
284 error = cache_notifier_ref();
285 if (error) {
286 return error;
287 }
288
289 ovs_mutex_init(&netdev->mutex);
290 netdev->tap_fd = -1;
291 netdev->kernel_name = xstrdup(netdev_->name);
292
293 /* Verify that the netdev really exists by attempting to read its flags */
294 error = netdev_get_flags(netdev_, &flags);
295 if (error == ENXIO) {
296 free(netdev->kernel_name);
297 cache_notifier_unref();
298 return error;
299 }
300
301 return 0;
302 }
303
304 static int
305 netdev_bsd_construct_tap(struct netdev *netdev_)
306 {
307 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
308 const char *name = netdev_->name;
309 int error = 0;
310 struct ifreq ifr;
311 char *kernel_name = NULL;
312
313 error = cache_notifier_ref();
314 if (error) {
315 goto error;
316 }
317
318 memset(&ifr, 0, sizeof(ifr));
319
320 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
321 * to retrieve the name of the tap device. */
322 ovs_mutex_init(&netdev->mutex);
323 netdev->tap_fd = open("/dev/tap", O_RDWR);
324 if (netdev->tap_fd < 0) {
325 error = errno;
326 VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error));
327 goto error_unref_notifier;
328 }
329
330 /* Retrieve tap name (e.g. tap0) */
331 if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
332 /* XXX Need to destroy the device? */
333 error = errno;
334 close(netdev->tap_fd);
335 goto error_unref_notifier;
336 }
337
338 /* Change the name of the tap device */
339 #if defined(SIOCSIFNAME)
340 ifr.ifr_data = (void *)name;
341 error = af_inet_ioctl(SIOCSIFNAME, &ifr);
342 if (error) {
343 destroy_tap(netdev->tap_fd, ifr.ifr_name);
344 goto error_unref_notifier;
345 }
346 kernel_name = xstrdup(name);
347 #else
348 /*
349 * NetBSD doesn't support inteface renaming.
350 */
351 VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name);
352 kernel_name = xstrdup(ifr.ifr_name);
353 #endif
354
355 /* set non-blocking. */
356 error = set_nonblocking(netdev->tap_fd);
357 if (error) {
358 destroy_tap(netdev->tap_fd, kernel_name);
359 goto error_unref_notifier;
360 }
361
362 /* Turn device UP */
363 ifr_set_flags(&ifr, IFF_UP);
364 strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
365 error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
366 if (error) {
367 destroy_tap(netdev->tap_fd, kernel_name);
368 goto error_unref_notifier;
369 }
370
371 netdev->kernel_name = kernel_name;
372
373 return 0;
374
375 error_unref_notifier:
376 ovs_mutex_destroy(&netdev->mutex);
377 cache_notifier_unref();
378 error:
379 free(kernel_name);
380 return error;
381 }
382
383 static void
384 netdev_bsd_destruct(struct netdev *netdev_)
385 {
386 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
387
388 cache_notifier_unref();
389
390 if (netdev->tap_fd >= 0) {
391 destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_));
392 }
393 if (netdev->pcap) {
394 pcap_close(netdev->pcap);
395 }
396 free(netdev->kernel_name);
397 ovs_mutex_destroy(&netdev->mutex);
398 }
399
400 static void
401 netdev_bsd_dealloc(struct netdev *netdev_)
402 {
403 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
404
405 free(netdev);
406 }
407
408 static int
409 netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
410 {
411 char errbuf[PCAP_ERRBUF_SIZE];
412 pcap_t *pcap = NULL;
413 int one = 1;
414 int error;
415 int fd;
416
417 /* Open the pcap device. The device is opened in non-promiscuous mode
418 * because the interface flags are manually set by the caller. */
419 errbuf[0] = '\0';
420 pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
421 if (!pcap) {
422 VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
423 error = EIO;
424 goto error;
425 }
426 if (errbuf[0] != '\0') {
427 VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
428 }
429
430 /* Get the underlying fd. */
431 fd = pcap_get_selectable_fd(pcap);
432 if (fd == -1) {
433 VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
434 error = errno;
435 goto error;
436 }
437
438 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
439 * on the file descriptor returned by pcap_get_selectable_fd to achieve
440 * a real non-blocking behaviour.*/
441 error = pcap_setnonblock(pcap, 1, errbuf);
442 if (error == -1) {
443 error = errno;
444 goto error;
445 }
446
447 /* This call assure that reads return immediately upon packet
448 * reception. Otherwise, a read will block until either the kernel
449 * buffer becomes full or a timeout occurs. */
450 if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
451 VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
452 name, ovs_strerror(errno));
453 error = errno;
454 goto error;
455 }
456
457 /* Capture only incoming packets. */
458 error = pcap_setdirection(pcap, PCAP_D_IN);
459 if (error == -1) {
460 error = errno;
461 goto error;
462 }
463
464 *pcapp = pcap;
465 *fdp = fd;
466 return 0;
467
468 error:
469 if (pcap) {
470 pcap_close(pcap);
471 }
472 *pcapp = NULL;
473 *fdp = -1;
474 return error;
475 }
476
477 static struct netdev_rxq *
478 netdev_bsd_rxq_alloc(void)
479 {
480 struct netdev_rxq_bsd *rxq = xzalloc(sizeof *rxq);
481 return &rxq->up;
482 }
483
484 static int
485 netdev_bsd_rxq_construct(struct netdev_rxq *rxq_)
486 {
487 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
488 struct netdev *netdev_ = rxq->up.netdev;
489 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
490 int error;
491
492 if (!strcmp(netdev_get_type(netdev_), "tap")) {
493 rxq->pcap_handle = NULL;
494 rxq->fd = netdev->tap_fd;
495 error = 0;
496 } else {
497 ovs_mutex_lock(&netdev->mutex);
498 error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
499 &rxq->pcap_handle, &rxq->fd);
500 ovs_mutex_unlock(&netdev->mutex);
501 }
502
503 return error;
504 }
505
506 static void
507 netdev_bsd_rxq_destruct(struct netdev_rxq *rxq_)
508 {
509 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
510
511 if (rxq->pcap_handle) {
512 pcap_close(rxq->pcap_handle);
513 }
514 }
515
516 static void
517 netdev_bsd_rxq_dealloc(struct netdev_rxq *rxq_)
518 {
519 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
520
521 free(rxq);
522 }
523
524 /* The recv callback of the netdev class returns the number of bytes of the
525 * received packet.
526 *
527 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
528 * not make difference between a missing packet on the capture interface and
529 * an error during the file capture. We can use the pcap_dispatch() function
530 * instead, which is able to distinguish between errors and null packet.
531 *
532 * To make pcap_dispatch() returns the number of bytes read from the interface
533 * we need to define the following callback and argument.
534 */
535 struct pcap_arg {
536 void *data;
537 int size;
538 int retval;
539 };
540
541 /*
542 * This callback will be executed on every captured packet.
543 *
544 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
545 * pcap returns a truncated packet and we follow this behavior.
546 *
547 * The argument args->retval is the packet size in bytes.
548 */
549 static void
550 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
551 {
552 struct pcap_arg *args = ALIGNED_CAST(struct pcap_arg *, args_);
553
554 if (args->size < hdr->len) {
555 VLOG_WARN_RL(&rl, "packet truncated");
556 args->retval = args->size;
557 } else {
558 args->retval = hdr->len;
559 }
560
561 /* copy the packet to our buffer */
562 memcpy(args->data, packet, args->retval);
563 }
564
565 /*
566 * This function attempts to receive a packet from the specified network
567 * device. It is assumed that the network device is a system device or a tap
568 * device opened as a system one. In this case the read operation is performed
569 * from rxq->pcap.
570 */
571 static int
572 netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
573 {
574 struct pcap_arg arg;
575 int ret;
576
577 /* prepare the pcap argument to store the packet */
578 arg.size = ofpbuf_tailroom(buffer);
579 arg.data = ofpbuf_data(buffer);
580
581 for (;;) {
582 ret = pcap_dispatch(rxq->pcap_handle, 1, proc_pkt, (u_char *) &arg);
583
584 if (ret > 0) {
585 ofpbuf_set_size(buffer, ofpbuf_size(buffer) + arg.retval);
586 return 0;
587 }
588 if (ret == -1) {
589 if (errno == EINTR) {
590 continue;
591 }
592 }
593
594 return EAGAIN;
595 }
596 }
597
598 /*
599 * This function attempts to receive a packet from the specified network
600 * device. It is assumed that the network device is a tap device and
601 * 'rxq->fd' is initialized with the tap file descriptor.
602 */
603 static int
604 netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
605 {
606 size_t size = ofpbuf_tailroom(buffer);
607
608 for (;;) {
609 ssize_t retval = read(rxq->fd, ofpbuf_data(buffer), size);
610 if (retval >= 0) {
611 ofpbuf_set_size(buffer, ofpbuf_size(buffer) + retval);
612 return 0;
613 } else if (errno != EINTR) {
614 if (errno != EAGAIN) {
615 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
616 ovs_strerror(errno), netdev_rxq_get_name(&rxq->up));
617 }
618 return errno;
619 }
620 }
621 }
622
623 static int
624 netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **packets,
625 int *c)
626 {
627 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
628 struct netdev *netdev = rxq->up.netdev;
629 struct dpif_packet *packet;
630 struct ofpbuf *buffer;
631 ssize_t retval;
632 int mtu;
633
634 if (netdev_bsd_get_mtu(netdev, &mtu)) {
635 mtu = ETH_PAYLOAD_MAX;
636 }
637
638 packet = dpif_packet_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu,
639 DP_NETDEV_HEADROOM);
640 buffer = &packet->ofpbuf;
641
642 retval = (rxq->pcap_handle
643 ? netdev_rxq_bsd_recv_pcap(rxq, buffer)
644 : netdev_rxq_bsd_recv_tap(rxq, buffer));
645
646 if (retval) {
647 dpif_packet_delete(packet);
648 } else {
649 dp_packet_pad(buffer);
650 dpif_packet_set_dp_hash(packet, 0);
651 packets[0] = packet;
652 *c = 1;
653 }
654 return retval;
655 }
656
657 /*
658 * Registers with the poll loop to wake up from the next call to poll_block()
659 * when a packet is ready to be received with netdev_rxq_recv() on 'rxq'.
660 */
661 static void
662 netdev_bsd_rxq_wait(struct netdev_rxq *rxq_)
663 {
664 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
665
666 poll_fd_wait(rxq->fd, POLLIN);
667 }
668
669 /* Discards all packets waiting to be received from 'rxq'. */
670 static int
671 netdev_bsd_rxq_drain(struct netdev_rxq *rxq_)
672 {
673 struct ifreq ifr;
674 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
675
676 strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rxq_get_netdev(rxq_)));
677 if (ioctl(rxq->fd, BIOCFLUSH, &ifr) == -1) {
678 VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
679 netdev_rxq_get_name(rxq_), ovs_strerror(errno));
680 return errno;
681 }
682 return 0;
683 }
684
685 /*
686 * Send a packet on the specified network device. The device could be either a
687 * system or a tap device.
688 */
689 static int
690 netdev_bsd_send(struct netdev *netdev_, int qid OVS_UNUSED,
691 struct dpif_packet **pkts, int cnt, bool may_steal)
692 {
693 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
694 const char *name = netdev_get_name(netdev_);
695 int error;
696 int i;
697
698 ovs_mutex_lock(&dev->mutex);
699 if (dev->tap_fd < 0 && !dev->pcap) {
700 error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
701 } else {
702 error = 0;
703 }
704
705 for (i = 0; i < cnt; i++) {
706 const void *data = ofpbuf_data(&pkts[i]->ofpbuf);
707 size_t size = ofpbuf_size(&pkts[i]->ofpbuf);
708
709 while (!error) {
710 ssize_t retval;
711 if (dev->tap_fd >= 0) {
712 retval = write(dev->tap_fd, data, size);
713 } else {
714 retval = pcap_inject(dev->pcap, data, size);
715 }
716 if (retval < 0) {
717 if (errno == EINTR) {
718 continue;
719 } else {
720 error = errno;
721 if (error != EAGAIN) {
722 VLOG_WARN_RL(&rl, "error sending Ethernet packet on"
723 " %s: %s", name, ovs_strerror(error));
724 }
725 }
726 } else if (retval != size) {
727 VLOG_WARN_RL(&rl, "sent partial Ethernet packet "
728 "(%"PRIuSIZE" bytes of "
729 "%"PRIuSIZE") on %s", retval, size, name);
730 error = EMSGSIZE;
731 } else {
732 break;
733 }
734 }
735 }
736
737 ovs_mutex_unlock(&dev->mutex);
738 if (may_steal) {
739 for (i = 0; i < cnt; i++) {
740 dpif_packet_delete(pkts[i]);
741 }
742 }
743
744 return error;
745 }
746
747 /*
748 * Registers with the poll loop to wake up from the next call to poll_block()
749 * when the packet transmission queue has sufficient room to transmit a packet
750 * with netdev_send().
751 */
752 static void
753 netdev_bsd_send_wait(struct netdev *netdev_, int qid OVS_UNUSED)
754 {
755 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
756
757 ovs_mutex_lock(&dev->mutex);
758 if (dev->tap_fd >= 0) {
759 /* TAP device always accepts packets. */
760 poll_immediate_wake();
761 } else if (dev->pcap) {
762 poll_fd_wait(dev->fd, POLLOUT);
763 } else {
764 /* We haven't even tried to send a packet yet. */
765 poll_immediate_wake();
766 }
767 ovs_mutex_unlock(&dev->mutex);
768 }
769
770 /*
771 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
772 * otherwise a positive errno value.
773 */
774 static int
775 netdev_bsd_set_etheraddr(struct netdev *netdev_,
776 const uint8_t mac[ETH_ADDR_LEN])
777 {
778 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
779 int error = 0;
780
781 ovs_mutex_lock(&netdev->mutex);
782 if (!(netdev->cache_valid & VALID_ETHERADDR)
783 || !eth_addr_equals(netdev->etheraddr, mac)) {
784 error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
785 ETH_ADDR_LEN, mac);
786 if (!error) {
787 netdev->cache_valid |= VALID_ETHERADDR;
788 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
789 netdev_change_seq_changed(netdev_);
790 }
791 }
792 ovs_mutex_unlock(&netdev->mutex);
793
794 return error;
795 }
796
797 /*
798 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
799 * free the returned buffer.
800 */
801 static int
802 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
803 uint8_t mac[ETH_ADDR_LEN])
804 {
805 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
806 int error = 0;
807
808 ovs_mutex_lock(&netdev->mutex);
809 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
810 error = get_etheraddr(netdev_get_kernel_name(netdev_),
811 netdev->etheraddr);
812 if (!error) {
813 netdev->cache_valid |= VALID_ETHERADDR;
814 }
815 }
816 if (!error) {
817 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
818 }
819 ovs_mutex_unlock(&netdev->mutex);
820
821 return error;
822 }
823
824 /*
825 * Returns the maximum size of transmitted (and received) packets on 'netdev',
826 * in bytes, not including the hardware header; thus, this is typically 1500
827 * bytes for Ethernet devices.
828 */
829 static int
830 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
831 {
832 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
833 int error = 0;
834
835 ovs_mutex_lock(&netdev->mutex);
836 if (!(netdev->cache_valid & VALID_MTU)) {
837 struct ifreq ifr;
838
839 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
840 SIOCGIFMTU, "SIOCGIFMTU");
841 if (!error) {
842 netdev->mtu = ifr.ifr_mtu;
843 netdev->cache_valid |= VALID_MTU;
844 }
845 }
846 if (!error) {
847 *mtup = netdev->mtu;
848 }
849 ovs_mutex_unlock(&netdev->mutex);
850
851 return 0;
852 }
853
854 static int
855 netdev_bsd_get_ifindex(const struct netdev *netdev_)
856 {
857 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
858 int ifindex, error;
859
860 ovs_mutex_lock(&netdev->mutex);
861 error = get_ifindex(netdev_, &ifindex);
862 ovs_mutex_unlock(&netdev->mutex);
863
864 return error ? -error : ifindex;
865 }
866
867 static int
868 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
869 {
870 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
871 int error = 0;
872
873 ovs_mutex_lock(&netdev->mutex);
874 if (!(netdev->cache_valid & VALID_CARRIER)) {
875 struct ifmediareq ifmr;
876
877 memset(&ifmr, 0, sizeof(ifmr));
878 strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
879 sizeof ifmr.ifm_name);
880
881 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
882 if (!error) {
883 netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
884 netdev->cache_valid |= VALID_CARRIER;
885
886 /* If the interface doesn't report whether the media is active,
887 * just assume it is active. */
888 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
889 netdev->carrier = true;
890 }
891 } else {
892 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
893 netdev_get_name(netdev_), ovs_strerror(error));
894 }
895 }
896 if (!error) {
897 *carrier = netdev->carrier;
898 }
899 ovs_mutex_unlock(&netdev->mutex);
900
901 return error;
902 }
903
904 static void
905 convert_stats_system(struct netdev_stats *stats, const struct if_data *ifd)
906 {
907 /*
908 * note: UINT64_MAX means unsupported
909 */
910 stats->rx_packets = ifd->ifi_ipackets;
911 stats->tx_packets = ifd->ifi_opackets;
912 stats->rx_bytes = ifd->ifi_obytes;
913 stats->tx_bytes = ifd->ifi_ibytes;
914 stats->rx_errors = ifd->ifi_ierrors;
915 stats->tx_errors = ifd->ifi_oerrors;
916 stats->rx_dropped = ifd->ifi_iqdrops;
917 stats->tx_dropped = UINT64_MAX;
918 stats->multicast = ifd->ifi_imcasts;
919 stats->collisions = ifd->ifi_collisions;
920 stats->rx_length_errors = UINT64_MAX;
921 stats->rx_over_errors = UINT64_MAX;
922 stats->rx_crc_errors = UINT64_MAX;
923 stats->rx_frame_errors = UINT64_MAX;
924 stats->rx_fifo_errors = UINT64_MAX;
925 stats->rx_missed_errors = UINT64_MAX;
926 stats->tx_aborted_errors = UINT64_MAX;
927 stats->tx_carrier_errors = UINT64_MAX;
928 stats->tx_fifo_errors = UINT64_MAX;
929 stats->tx_heartbeat_errors = UINT64_MAX;
930 stats->tx_window_errors = UINT64_MAX;
931 }
932
933 static void
934 convert_stats_tap(struct netdev_stats *stats, const struct if_data *ifd)
935 {
936 /*
937 * Similar to convert_stats_system but swapping rxq and tx
938 * because 'ifd' is stats for the network interface side of the
939 * tap device and what the caller wants is one for the character
940 * device side.
941 *
942 * note: UINT64_MAX means unsupported
943 */
944 stats->rx_packets = ifd->ifi_opackets;
945 stats->tx_packets = ifd->ifi_ipackets;
946 stats->rx_bytes = ifd->ifi_ibytes;
947 stats->tx_bytes = ifd->ifi_obytes;
948 stats->rx_errors = ifd->ifi_oerrors;
949 stats->tx_errors = ifd->ifi_ierrors;
950 stats->rx_dropped = UINT64_MAX;
951 stats->tx_dropped = ifd->ifi_iqdrops;
952 stats->multicast = ifd->ifi_omcasts;
953 stats->collisions = UINT64_MAX;
954 stats->rx_length_errors = UINT64_MAX;
955 stats->rx_over_errors = UINT64_MAX;
956 stats->rx_crc_errors = UINT64_MAX;
957 stats->rx_frame_errors = UINT64_MAX;
958 stats->rx_fifo_errors = UINT64_MAX;
959 stats->rx_missed_errors = UINT64_MAX;
960 stats->tx_aborted_errors = UINT64_MAX;
961 stats->tx_carrier_errors = UINT64_MAX;
962 stats->tx_fifo_errors = UINT64_MAX;
963 stats->tx_heartbeat_errors = UINT64_MAX;
964 stats->tx_window_errors = UINT64_MAX;
965 }
966
967 static void
968 convert_stats(const struct netdev *netdev, struct netdev_stats *stats,
969 const struct if_data *ifd)
970 {
971 if (netdev_bsd_cast(netdev)->tap_fd == -1) {
972 convert_stats_system(stats, ifd);
973 } else {
974 convert_stats_tap(stats, ifd);
975 }
976 }
977
978 /* Retrieves current device stats for 'netdev'. */
979 static int
980 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
981 {
982 #if defined(__FreeBSD__)
983 int if_count, i;
984 int mib[6];
985 size_t len;
986 struct ifmibdata ifmd;
987
988
989 mib[0] = CTL_NET;
990 mib[1] = PF_LINK;
991 mib[2] = NETLINK_GENERIC;
992 mib[3] = IFMIB_SYSTEM;
993 mib[4] = IFMIB_IFCOUNT;
994
995 len = sizeof(if_count);
996
997 if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
998 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
999 netdev_get_name(netdev_), ovs_strerror(errno));
1000 return errno;
1001 }
1002
1003 mib[5] = IFDATA_GENERAL;
1004 mib[3] = IFMIB_IFDATA;
1005 len = sizeof(ifmd);
1006 for (i = 1; i <= if_count; i++) {
1007 mib[4] = i; //row
1008 if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
1009 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
1010 netdev_get_name(netdev_), ovs_strerror(errno));
1011 return errno;
1012 } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
1013 convert_stats(netdev_, stats, &ifmd.ifmd_data);
1014 break;
1015 }
1016 }
1017
1018 return 0;
1019 #elif defined(__NetBSD__)
1020 struct ifdatareq ifdr;
1021 int error;
1022
1023 memset(&ifdr, 0, sizeof(ifdr));
1024 strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
1025 sizeof(ifdr.ifdr_name));
1026 error = af_link_ioctl(SIOCGIFDATA, &ifdr);
1027 if (!error) {
1028 convert_stats(netdev_, stats, &ifdr.ifdr_data);
1029 }
1030 return error;
1031 #else
1032 #error not implemented
1033 #endif
1034 }
1035
1036 static uint32_t
1037 netdev_bsd_parse_media(int media)
1038 {
1039 uint32_t supported = 0;
1040 bool half_duplex = media & IFM_HDX ? true : false;
1041
1042 switch (IFM_SUBTYPE(media)) {
1043 case IFM_10_2:
1044 case IFM_10_5:
1045 case IFM_10_STP:
1046 case IFM_10_T:
1047 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
1048 supported |= NETDEV_F_COPPER;
1049 break;
1050
1051 case IFM_10_FL:
1052 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
1053 supported |= NETDEV_F_FIBER;
1054 break;
1055
1056 case IFM_100_T2:
1057 case IFM_100_T4:
1058 case IFM_100_TX:
1059 case IFM_100_VG:
1060 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
1061 supported |= NETDEV_F_COPPER;
1062 break;
1063
1064 case IFM_100_FX:
1065 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
1066 supported |= NETDEV_F_FIBER;
1067 break;
1068
1069 case IFM_1000_CX:
1070 case IFM_1000_T:
1071 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
1072 supported |= NETDEV_F_COPPER;
1073 break;
1074
1075 case IFM_1000_LX:
1076 case IFM_1000_SX:
1077 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
1078 supported |= NETDEV_F_FIBER;
1079 break;
1080
1081 case IFM_10G_CX4:
1082 supported |= NETDEV_F_10GB_FD;
1083 supported |= NETDEV_F_COPPER;
1084 break;
1085
1086 case IFM_10G_LR:
1087 case IFM_10G_SR:
1088 supported |= NETDEV_F_10GB_FD;
1089 supported |= NETDEV_F_FIBER;
1090 break;
1091
1092 default:
1093 return 0;
1094 }
1095
1096 if (IFM_SUBTYPE(media) == IFM_AUTO) {
1097 supported |= NETDEV_F_AUTONEG;
1098 }
1099 /*
1100 if (media & IFM_ETH_FMASK) {
1101 supported |= NETDEV_F_PAUSE;
1102 }
1103 */
1104
1105 return supported;
1106 }
1107
1108 /*
1109 * Stores the features supported by 'netdev' into each of '*current',
1110 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1111 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1112 * successful, otherwise a positive errno value. On failure, all of the
1113 * passed-in values are set to 0.
1114 */
1115 static int
1116 netdev_bsd_get_features(const struct netdev *netdev,
1117 enum netdev_features *current, uint32_t *advertised,
1118 enum netdev_features *supported, uint32_t *peer)
1119 {
1120 struct ifmediareq ifmr;
1121 int *media_list;
1122 int i;
1123 int error;
1124
1125
1126 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1127
1128 memset(&ifmr, 0, sizeof(ifmr));
1129 strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1130
1131 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
1132 * number of supported modes, and a second with a buffer to retrieve
1133 * them. */
1134 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1135 if (error) {
1136 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1137 netdev_get_name(netdev), ovs_strerror(error));
1138 return error;
1139 }
1140
1141 media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1142 ifmr.ifm_ulist = media_list;
1143
1144 if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
1145 VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1146 netdev_get_name(netdev));
1147 error = EINVAL;
1148 goto cleanup;
1149 }
1150
1151 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1152 if (error) {
1153 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1154 netdev_get_name(netdev), ovs_strerror(error));
1155 goto cleanup;
1156 }
1157
1158 /* Current settings. */
1159 *current = netdev_bsd_parse_media(ifmr.ifm_active);
1160
1161 /* Advertised features. */
1162 *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1163
1164 /* Supported features. */
1165 *supported = 0;
1166 for (i = 0; i < ifmr.ifm_count; i++) {
1167 *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1168 }
1169
1170 /* Peer advertisements. */
1171 *peer = 0; /* XXX */
1172
1173 error = 0;
1174 cleanup:
1175 free(media_list);
1176 return error;
1177 }
1178
1179 /*
1180 * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
1181 * '*netmask' to its netmask and returns true. Otherwise, returns false.
1182 */
1183 static int
1184 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1185 struct in_addr *netmask)
1186 {
1187 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1188 int error = 0;
1189
1190 ovs_mutex_lock(&netdev->mutex);
1191 if (!(netdev->cache_valid & VALID_IN4)) {
1192 struct ifreq ifr;
1193
1194 ifr.ifr_addr.sa_family = AF_INET;
1195 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1196 SIOCGIFADDR, "SIOCGIFADDR");
1197 if (!error) {
1198 const struct sockaddr_in *sin;
1199
1200 sin = ALIGNED_CAST(struct sockaddr_in *, &ifr.ifr_addr);
1201 netdev->in4 = sin->sin_addr;
1202 netdev->cache_valid |= VALID_IN4;
1203 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1204 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1205 if (!error) {
1206 *netmask = sin->sin_addr;
1207 }
1208 }
1209 }
1210 if (!error) {
1211 *in4 = netdev->in4;
1212 *netmask = netdev->netmask;
1213 }
1214 ovs_mutex_unlock(&netdev->mutex);
1215
1216 return error ? error : in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1217 }
1218
1219 /*
1220 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1221 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1222 * positive errno value.
1223 */
1224 static int
1225 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1226 struct in_addr mask)
1227 {
1228 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1229 int error;
1230
1231 ovs_mutex_lock(&netdev->mutex);
1232 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1233 if (!error) {
1234 if (addr.s_addr != INADDR_ANY) {
1235 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1236 "SIOCSIFNETMASK", mask);
1237 if (!error) {
1238 netdev->cache_valid |= VALID_IN4;
1239 netdev->in4 = addr;
1240 netdev->netmask = mask;
1241 }
1242 }
1243 netdev_change_seq_changed(netdev_);
1244 }
1245 ovs_mutex_unlock(&netdev->mutex);
1246
1247 return error;
1248 }
1249
1250 static int
1251 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1252 {
1253 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1254 if (!(netdev->cache_valid & VALID_IN6)) {
1255 struct ifaddrs *ifa, *head;
1256 struct sockaddr_in6 *sin6;
1257 const char *netdev_name = netdev_get_name(netdev_);
1258
1259 if (getifaddrs(&head) != 0) {
1260 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1261 ovs_strerror(errno));
1262 return errno;
1263 }
1264
1265 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1266 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1267 !strcmp(ifa->ifa_name, netdev_name)) {
1268 sin6 = ALIGNED_CAST(struct sockaddr_in6 *, ifa->ifa_addr);
1269 if (sin6) {
1270 memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
1271 netdev->cache_valid |= VALID_IN6;
1272 *in6 = netdev->in6;
1273 freeifaddrs(head);
1274 return 0;
1275 }
1276 }
1277 }
1278 return EADDRNOTAVAIL;
1279 }
1280 *in6 = netdev->in6;
1281 return 0;
1282 }
1283
1284 #if defined(__NetBSD__)
1285 static char *
1286 netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name)
1287 {
1288 char *ovs_name = NULL;
1289 struct shash device_shash;
1290 struct shash_node *node;
1291
1292 shash_init(&device_shash);
1293 netdev_get_devices(&netdev_tap_class, &device_shash);
1294 SHASH_FOR_EACH(node, &device_shash) {
1295 struct netdev *netdev = node->data;
1296 struct netdev_bsd * const dev = netdev_bsd_cast(netdev);
1297
1298 if (!strcmp(dev->kernel_name, kernel_name)) {
1299 free(ovs_name);
1300 ovs_name = xstrdup(netdev_get_name(&dev->up));
1301 }
1302 netdev_close(netdev);
1303 }
1304 shash_destroy(&device_shash);
1305
1306 return ovs_name ? ovs_name : xstrdup(kernel_name);
1307 }
1308 #endif
1309
1310 static int
1311 netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
1312 struct in_addr *next_hop OVS_UNUSED,
1313 char **netdev_name OVS_UNUSED)
1314 {
1315 #if defined(__NetBSD__)
1316 static int seq = 0;
1317 struct sockaddr_in sin;
1318 struct sockaddr_dl sdl;
1319 int s;
1320 int i;
1321 struct {
1322 struct rt_msghdr h;
1323 char space[512];
1324 } buf;
1325 struct rt_msghdr *rtm = &buf.h;
1326 const pid_t pid = getpid();
1327 char *cp;
1328 ssize_t ssz;
1329 bool gateway = false;
1330 char *ifname = NULL;
1331 int saved_errno;
1332
1333 memset(next_hop, 0, sizeof(*next_hop));
1334 *netdev_name = NULL;
1335
1336 memset(&sin, 0, sizeof(sin));
1337 sin.sin_len = sizeof(sin);
1338 sin.sin_family = AF_INET;
1339 sin.sin_port = 0;
1340 sin.sin_addr = *host;
1341
1342 memset(&sdl, 0, sizeof(sdl));
1343 sdl.sdl_len = sizeof(sdl);
1344 sdl.sdl_family = AF_LINK;
1345
1346 s = socket(PF_ROUTE, SOCK_RAW, 0);
1347 memset(&buf, 0, sizeof(buf));
1348 rtm->rtm_flags = RTF_HOST|RTF_UP;
1349 rtm->rtm_version = RTM_VERSION;
1350 rtm->rtm_addrs = RTA_DST|RTA_IFP;
1351 cp = (void *)&buf.space;
1352 memcpy(cp, &sin, sizeof(sin));
1353 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin);
1354 memcpy(cp, &sdl, sizeof(sdl));
1355 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl);
1356 rtm->rtm_msglen = cp - (char *)(void *)rtm;
1357 rtm->rtm_seq = ++seq;
1358 rtm->rtm_type = RTM_GET;
1359 rtm->rtm_pid = pid;
1360 write(s, rtm, rtm->rtm_msglen);
1361 memset(&buf, 0, sizeof(buf));
1362 do {
1363 ssz = read(s, &buf, sizeof(buf));
1364 } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
1365 saved_errno = errno;
1366 close(s);
1367 if (ssz <= 0) {
1368 if (ssz < 0) {
1369 return saved_errno;
1370 }
1371 return EPIPE; /* XXX */
1372 }
1373 cp = (void *)&buf.space;
1374 for (i = 1; i; i <<= 1) {
1375 if ((rtm->rtm_addrs & i) != 0) {
1376 const struct sockaddr *sa = (const void *)cp;
1377
1378 if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) {
1379 const struct sockaddr_in * const sin =
1380 ALIGNED_CAST(const struct sockaddr_in *, sa);
1381
1382 *next_hop = sin->sin_addr;
1383 gateway = true;
1384 }
1385 if ((i == RTA_IFP) && sa->sa_family == AF_LINK) {
1386 const struct sockaddr_dl * const sdl =
1387 ALIGNED_CAST(const struct sockaddr_dl *, sa);
1388 char *kernel_name;
1389
1390 kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen);
1391 ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name);
1392 free(kernel_name);
1393 }
1394 RT_ADVANCE(cp, sa);
1395 }
1396 }
1397 if (ifname == NULL) {
1398 return ENXIO;
1399 }
1400 if (!gateway) {
1401 *next_hop = *host;
1402 }
1403 *netdev_name = ifname;
1404 VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s",
1405 IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name);
1406 return 0;
1407 #else
1408 return EOPNOTSUPP;
1409 #endif
1410 }
1411
1412 static int
1413 netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED,
1414 ovs_be32 ip OVS_UNUSED,
1415 uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1416 {
1417 #if defined(__NetBSD__)
1418 const struct rt_msghdr *rtm;
1419 size_t needed;
1420 char *buf;
1421 const char *cp;
1422 const char *ep;
1423 int mib[6];
1424 int error;
1425
1426 buf = NULL;
1427 mib[0] = CTL_NET;
1428 mib[1] = PF_ROUTE;
1429 mib[2] = 0;
1430 mib[3] = AF_INET;
1431 mib[4] = NET_RT_FLAGS;
1432 mib[5] = RTF_LLINFO;
1433 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
1434 error = errno;
1435 goto error;
1436 }
1437 buf = xmalloc(needed);
1438 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
1439 error = errno;
1440 goto error;
1441 }
1442 ep = buf + needed;
1443 for (cp = buf; cp < ep; cp += rtm->rtm_msglen) {
1444 const struct sockaddr_inarp *sina;
1445 const struct sockaddr_dl *sdl;
1446
1447 rtm = (const void *)cp;
1448 sina = (const void *)(rtm + 1);
1449 if (ip != sina->sin_addr.s_addr) {
1450 continue;
1451 }
1452 sdl = (const void *)
1453 ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len));
1454 if (sdl->sdl_alen == ETH_ADDR_LEN) {
1455 memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN);
1456 error = 0;
1457 goto error;
1458 }
1459 }
1460 error = ENXIO;
1461 error:
1462 free(buf);
1463 return error;
1464 #else
1465 return EOPNOTSUPP;
1466 #endif
1467 }
1468
1469 static void
1470 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1471 {
1472 struct sockaddr_in sin;
1473 memset(&sin, 0, sizeof sin);
1474 sin.sin_family = AF_INET;
1475 sin.sin_addr = addr;
1476 sin.sin_port = 0;
1477
1478 memset(sa, 0, sizeof *sa);
1479 memcpy(sa, &sin, sizeof sin);
1480 }
1481
1482 static int
1483 do_set_addr(struct netdev *netdev,
1484 unsigned long ioctl_nr, const char *ioctl_name,
1485 struct in_addr addr)
1486 {
1487 struct ifreq ifr;
1488 make_in4_sockaddr(&ifr.ifr_addr, addr);
1489 return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
1490 ioctl_name);
1491 }
1492
1493 static int
1494 nd_to_iff_flags(enum netdev_flags nd)
1495 {
1496 int iff = 0;
1497 if (nd & NETDEV_UP) {
1498 iff |= IFF_UP;
1499 }
1500 if (nd & NETDEV_PROMISC) {
1501 iff |= IFF_PROMISC;
1502 #if defined(IFF_PPROMISC)
1503 iff |= IFF_PPROMISC;
1504 #endif
1505 }
1506 if (nd & NETDEV_LOOPBACK) {
1507 iff |= IFF_LOOPBACK;
1508 }
1509 return iff;
1510 }
1511
1512 static int
1513 iff_to_nd_flags(int iff)
1514 {
1515 enum netdev_flags nd = 0;
1516 if (iff & IFF_UP) {
1517 nd |= NETDEV_UP;
1518 }
1519 if (iff & IFF_PROMISC) {
1520 nd |= NETDEV_PROMISC;
1521 }
1522 if (iff & IFF_LOOPBACK) {
1523 nd |= NETDEV_LOOPBACK;
1524 }
1525 return nd;
1526 }
1527
1528 static int
1529 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1530 enum netdev_flags on, enum netdev_flags *old_flagsp)
1531 {
1532 int old_flags, new_flags;
1533 int error;
1534
1535 error = get_flags(netdev_, &old_flags);
1536 if (!error) {
1537 *old_flagsp = iff_to_nd_flags(old_flags);
1538 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1539 if (new_flags != old_flags) {
1540 error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
1541 netdev_change_seq_changed(netdev_);
1542 }
1543 }
1544 return error;
1545 }
1546
1547 /* Linux has also different GET_STATS, SET_STATS,
1548 * GET_STATUS)
1549 */
1550 #define NETDEV_BSD_CLASS(NAME, CONSTRUCT, \
1551 GET_FEATURES) \
1552 { \
1553 NAME, \
1554 \
1555 NULL, /* init */ \
1556 netdev_bsd_run, \
1557 netdev_bsd_wait, \
1558 netdev_bsd_alloc, \
1559 CONSTRUCT, \
1560 netdev_bsd_destruct, \
1561 netdev_bsd_dealloc, \
1562 NULL, /* get_config */ \
1563 NULL, /* set_config */ \
1564 NULL, /* get_tunnel_config */ \
1565 NULL, /* get_numa_id */ \
1566 NULL, /* set_multiq */ \
1567 \
1568 netdev_bsd_send, \
1569 netdev_bsd_send_wait, \
1570 \
1571 netdev_bsd_set_etheraddr, \
1572 netdev_bsd_get_etheraddr, \
1573 netdev_bsd_get_mtu, \
1574 NULL, /* set_mtu */ \
1575 netdev_bsd_get_ifindex, \
1576 netdev_bsd_get_carrier, \
1577 NULL, /* get_carrier_resets */ \
1578 NULL, /* set_miimon_interval */ \
1579 netdev_bsd_get_stats, \
1580 \
1581 GET_FEATURES, \
1582 NULL, /* set_advertisement */ \
1583 NULL, /* set_policing */ \
1584 NULL, /* get_qos_type */ \
1585 NULL, /* get_qos_capabilities */ \
1586 NULL, /* get_qos */ \
1587 NULL, /* set_qos */ \
1588 NULL, /* get_queue */ \
1589 NULL, /* set_queue */ \
1590 NULL, /* delete_queue */ \
1591 NULL, /* get_queue_stats */ \
1592 NULL, /* queue_dump_start */ \
1593 NULL, /* queue_dump_next */ \
1594 NULL, /* queue_dump_done */ \
1595 NULL, /* dump_queue_stats */ \
1596 \
1597 netdev_bsd_get_in4, \
1598 netdev_bsd_set_in4, \
1599 netdev_bsd_get_in6, \
1600 NULL, /* add_router */ \
1601 netdev_bsd_get_next_hop, \
1602 NULL, /* get_status */ \
1603 netdev_bsd_arp_lookup, /* arp_lookup */ \
1604 \
1605 netdev_bsd_update_flags, \
1606 \
1607 netdev_bsd_rxq_alloc, \
1608 netdev_bsd_rxq_construct, \
1609 netdev_bsd_rxq_destruct, \
1610 netdev_bsd_rxq_dealloc, \
1611 netdev_bsd_rxq_recv, \
1612 netdev_bsd_rxq_wait, \
1613 netdev_bsd_rxq_drain, \
1614 }
1615
1616 const struct netdev_class netdev_bsd_class =
1617 NETDEV_BSD_CLASS(
1618 "system",
1619 netdev_bsd_construct_system,
1620 netdev_bsd_get_features);
1621
1622 const struct netdev_class netdev_tap_class =
1623 NETDEV_BSD_CLASS(
1624 "tap",
1625 netdev_bsd_construct_tap,
1626 netdev_bsd_get_features);
1627 \f
1628
1629 static void
1630 destroy_tap(int fd, const char *name)
1631 {
1632 struct ifreq ifr;
1633
1634 close(fd);
1635 strcpy(ifr.ifr_name, name);
1636 /* XXX What to do if this call fails? */
1637 af_inet_ioctl(SIOCIFDESTROY, &ifr);
1638 }
1639
1640 static int
1641 get_flags(const struct netdev *netdev, int *flags)
1642 {
1643 struct ifreq ifr;
1644 int error;
1645
1646 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr,
1647 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1648
1649 *flags = ifr_get_flags(&ifr);
1650
1651 return error;
1652 }
1653
1654 static int
1655 set_flags(const char *name, int flags)
1656 {
1657 struct ifreq ifr;
1658
1659 ifr_set_flags(&ifr, flags);
1660
1661 return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1662 }
1663
1664 static int
1665 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1666 {
1667 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1668 *ifindexp = 0;
1669 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1670 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1671 if (ifindex <= 0) {
1672 return errno;
1673 }
1674 netdev->cache_valid |= VALID_IFINDEX;
1675 netdev->ifindex = ifindex;
1676 }
1677 *ifindexp = netdev->ifindex;
1678 return 0;
1679 }
1680
1681 static int
1682 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1683 {
1684 struct ifaddrs *head;
1685 struct ifaddrs *ifa;
1686 struct sockaddr_dl *sdl;
1687
1688 if (getifaddrs(&head) != 0) {
1689 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1690 ovs_strerror(errno));
1691 return errno;
1692 }
1693
1694 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1695 if (ifa->ifa_addr->sa_family == AF_LINK) {
1696 if (!strcmp(ifa->ifa_name, netdev_name)) {
1697 sdl = ALIGNED_CAST(struct sockaddr_dl *, ifa->ifa_addr);
1698 if (sdl) {
1699 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1700 freeifaddrs(head);
1701 return 0;
1702 }
1703 }
1704 }
1705 }
1706
1707 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1708 freeifaddrs(head);
1709 return ENODEV;
1710 }
1711
1712 static int
1713 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
1714 int hwaddr_len OVS_UNUSED,
1715 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1716 {
1717 #if defined(__FreeBSD__)
1718 struct ifreq ifr;
1719 int error;
1720
1721 memset(&ifr, 0, sizeof ifr);
1722 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1723 ifr.ifr_addr.sa_family = hwaddr_family;
1724 ifr.ifr_addr.sa_len = hwaddr_len;
1725 memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1726 error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
1727 if (error) {
1728 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1729 netdev_name, ovs_strerror(error));
1730 return error;
1731 }
1732 return 0;
1733 #elif defined(__NetBSD__)
1734 struct if_laddrreq req;
1735 struct sockaddr_dl *sdl;
1736 struct sockaddr_storage oldaddr;
1737 int error;
1738
1739 /*
1740 * get the old address, add new one, and then remove old one.
1741 */
1742
1743 if (hwaddr_len != ETH_ADDR_LEN) {
1744 /* just to be safe about sockaddr storage size */
1745 return EOPNOTSUPP;
1746 }
1747 memset(&req, 0, sizeof(req));
1748 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1749 req.addr.ss_len = sizeof(req.addr);
1750 req.addr.ss_family = hwaddr_family;
1751 sdl = (struct sockaddr_dl *)&req.addr;
1752 sdl->sdl_alen = hwaddr_len;
1753
1754 error = af_link_ioctl(SIOCGLIFADDR, &req);
1755 if (error) {
1756 return error;
1757 }
1758 if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], mac, hwaddr_len)) {
1759 return 0;
1760 }
1761 oldaddr = req.addr;
1762
1763 memset(&req, 0, sizeof(req));
1764 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1765 req.flags = IFLR_ACTIVE;
1766 sdl = (struct sockaddr_dl *)&req.addr;
1767 sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
1768 sdl->sdl_alen = hwaddr_len;
1769 sdl->sdl_family = hwaddr_family;
1770 memcpy(sdl->sdl_data, mac, hwaddr_len);
1771 error = af_link_ioctl(SIOCALIFADDR, &req);
1772 if (error) {
1773 return error;
1774 }
1775
1776 memset(&req, 0, sizeof(req));
1777 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1778 req.addr = oldaddr;
1779 return af_link_ioctl(SIOCDLIFADDR, &req);
1780 #else
1781 #error not implemented
1782 #endif
1783 }
1784
1785 static int
1786 ifr_get_flags(const struct ifreq *ifr)
1787 {
1788 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1789 return (ifr->ifr_flagshigh << 16) | ifr->ifr_flags;
1790 #else
1791 return ifr->ifr_flags;
1792 #endif
1793 }
1794
1795 static void
1796 ifr_set_flags(struct ifreq *ifr, int flags)
1797 {
1798 ifr->ifr_flags = flags;
1799 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1800 ifr->ifr_flagshigh = flags >> 16;
1801 #endif
1802 }
1803
1804 #if defined(__NetBSD__)
1805 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
1806 * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
1807 int
1808 af_link_ioctl(unsigned long command, const void *arg)
1809 {
1810 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1811 static int sock;
1812
1813 if (ovsthread_once_start(&once)) {
1814 sock = socket(AF_LINK, SOCK_DGRAM, 0);
1815 if (sock < 0) {
1816 sock = -errno;
1817 VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno));
1818 }
1819 ovsthread_once_done(&once);
1820 }
1821
1822 return (sock < 0 ? -sock
1823 : ioctl(sock, command, arg) == -1 ? errno
1824 : 0);
1825 }
1826 #endif