restored minimal functionality
[sliver-openvswitch.git] / lib / netdev-bsd.c
1 /*
2  * Copyright (c) 2011 Gaetano Catalli. 
3  * 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * 
7  *    1. Redistributions of source code must retain the above copyright notice,
8  *       this list of conditions and the following disclaimer.
9  * 
10  *    2. Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  * 
14  * THIS SOFTWARE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <config.h>
20 #include <assert.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include <sys/time.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
27 #include <sys/sockio.h>
28 #include <ifaddrs.h>
29 #include <pcap/pcap.h>
30 #include <net/if.h>
31 #include <net/if_dl.h>
32 #include <net/if_media.h>
33 #include <net/if_tap.h>
34 #include <netinet/in.h>
35 #include <net/if_mib.h>
36 #include <poll.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/sysctl.h>
40
41 #include "rtbsd.h"
42 #include "coverage.h"
43 #include "dynamic-string.h"
44 #include "fatal-signal.h"
45 #include "netdev-provider.h"
46 #include "ofpbuf.h"
47 #include "openflow/openflow.h"
48 #include "packets.h"
49 #include "poll-loop.h"
50 #include "socket-util.h"
51 #include "shash.h"
52 #include "svec.h"
53 #include "vlog.h"
54
55 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
56
57 \f
58 /*
59  * This file implements objects to access interfaces.
60  * Externally, interfaces are represented by two structures:
61  *   + struct netdev_dev, representing a network device,
62  *     containing e.g. name and a refcount;
63  *     We can have private variables by embedding the
64  *     struct netdev_dev into our own structure
65  *     (e.g. netdev_dev_bsd)
66  *
67  *   + struct netdev, representing an instance of an open netdev_dev.
68  *     The structure contains a pointer to the 'struct netdev'
69  *     representing the device. Again, private information
70  *     such as file descriptor etc. are stored in our
71  *     own struct netdev_bsd which includes a struct netdev.
72  *
73  * Both 'struct netdev' and 'struct netdev_dev' are referenced
74  * in containers which hold pointers to the data structures.
75  * We can reach our own struct netdev_XXX_bsd by putting a
76  * struct netdev_XXX within our own struct, and using CONTAINER_OF
77  * to access the parent structure.
78  */
79 struct netdev_bsd {
80     struct netdev netdev;
81
82     int netdev_fd;   /* Selectable file descriptor for the network device.
83                         This descriptor will be used for polling operations */
84     
85     pcap_t *pcap_handle;  /* Packet capture descriptor for a system network
86                              device */
87 };
88
89 struct netdev_dev_bsd {
90     struct netdev_dev netdev_dev;
91     unsigned int cache_valid;
92     unsigned int change_seq;
93
94     int ifindex;
95     uint8_t etheraddr[ETH_ADDR_LEN];
96     struct in_addr in4;
97     struct in6_addr in6;
98     int mtu;
99     int carrier;
100     
101     bool tap_opened;
102     int tap_fd;         /* TAP character device, if any */
103 };
104
105
106 enum {
107     VALID_IFINDEX = 1 << 0,
108     VALID_ETHERADDR = 1 << 1,
109     VALID_IN4 = 1 << 2,
110     VALID_IN6 = 1 << 3,
111     VALID_MTU = 1 << 4,
112     VALID_CARRIER = 1 << 5
113 };
114
115 /* An AF_INET socket (used for ioctl operations). */
116 static int af_inet_sock = -1;
117
118 #define PCAP_SNAPLEN 1024
119
120
121 /*
122  * Notifier used to invalidate device informations in case of status change.
123  *
124  * It will be registered with a 'rtbsd_notifier_register()' when the first
125  * device will be created with the call of either 'netdev_bsd_tap_create()' or
126  * 'netdev_bsd_system_create()'.
127  *
128  * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
129  * invalidate cached information about the device.
130  */
131 static struct rtbsd_notifier netdev_bsd_cache_notifier;
132 static int cache_notifier_refcount;
133
134 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
135
136 static int netdev_bsd_do_ioctl(const struct netdev *, struct ifreq *,
137                                  unsigned long cmd, const char *cmd_name);
138 static void destroy_tap(int fd, const char *name);
139 static int get_flags(const struct netdev *, int *flagsp);
140 static int set_flags(struct netdev *, int flags);
141 static int do_set_addr(struct netdev *netdev,
142                        int ioctl_nr, const char *ioctl_name,
143                        struct in_addr addr);
144 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
145 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
146                          int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
147 static int get_ifindex(const struct netdev *, int *ifindexp);
148
149 static int netdev_bsd_init(void);
150
151 static bool
152 is_netdev_bsd_class(const struct netdev_class *netdev_class)
153 {
154     return netdev_class->init == netdev_bsd_init;
155 }
156
157 static struct netdev_bsd *
158 netdev_bsd_cast(const struct netdev *netdev)
159 {
160     assert(is_netdev_bsd_class(netdev_dev_get_class(netdev_get_dev(netdev))));
161     return CONTAINER_OF(netdev, struct netdev_bsd, netdev);
162 }
163
164 static struct netdev_dev_bsd *
165 netdev_dev_bsd_cast(const struct netdev_dev *netdev_dev)
166 {
167     assert(is_netdev_bsd_class(netdev_dev_get_class(netdev_dev)));
168     return CONTAINER_OF(netdev_dev, struct netdev_dev_bsd, netdev_dev);
169 }
170
171 /* Initialize the AF_INET socket used for ioctl operations */
172 static int
173 netdev_bsd_init(void)
174 {
175     static int status = -1;
176     
177     if (status >= 0) {  /* already initialized */
178         return status;
179     }
180
181     af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
182     status = af_inet_sock >= 0 ? 0 : errno;
183
184     if (status) {
185         VLOG_ERR("failed to create inet socket: %s", strerror(status));
186     }
187         
188     return status;
189 }
190
191 /* 
192  * Perform periodic work needed by netdev. In BSD netdevs it checks for any
193  * interface status changes, and eventually calls all the user callbacks.
194  */
195 static void
196 netdev_bsd_run(void)
197 {
198     rtbsd_notifier_run();
199 }
200
201 /* 
202  * Arranges for poll_block() to wake up if the "run" member function needs to
203  * be called. 
204  */
205 static void
206 netdev_bsd_wait(void)
207 {
208     rtbsd_notifier_wait();
209 }
210
211 /* Invalidate cache in case of interface status change. */
212 static void
213 netdev_bsd_cache_cb(const struct rtbsd_change *change,
214                       void *aux OVS_UNUSED)
215 {
216     struct netdev_dev_bsd *dev;
217     
218     if (change) {
219         struct netdev_dev *base_dev = netdev_dev_from_name(change->if_name);
220
221         if (base_dev) {
222             const struct netdev_class *netdev_class =
223                                                 netdev_dev_get_class(base_dev);
224
225             if (is_netdev_bsd_class(netdev_class)) {
226                 dev = netdev_dev_bsd_cast(base_dev);
227                 dev->cache_valid = 0;
228             }
229         }
230     } else {
231         /*
232          * XXX the API is lacking, we should be able to iterate on the list of
233          * netdevs without having to store the info in a temp shash.
234          */
235         struct shash device_shash;
236         struct shash_node *node;
237
238         shash_init(&device_shash);
239         netdev_dev_get_devices(&netdev_bsd_class, &device_shash);
240         SHASH_FOR_EACH (node, &device_shash) {
241             dev = node->data;
242             dev->cache_valid = 0;
243         }
244         shash_destroy(&device_shash);
245     }
246 }
247
248 static int
249 cache_notifier_ref(void)
250 {
251     int ret = 0;
252
253     if (!cache_notifier_refcount) {
254         ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
255                                                 netdev_bsd_cache_cb, NULL);
256         if (ret) {
257             return ret;
258         }
259     }
260     cache_notifier_refcount++;
261     return 0;
262 }
263
264 static int
265 cache_notifier_unref(void)
266 {
267     cache_notifier_refcount--;
268     if (cache_notifier_refcount == 0) {
269         rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
270     }
271     return 0;
272 }
273     
274 /* Allocate a netdev_dev_bsd structure */
275 static int
276 netdev_bsd_create_system(const struct netdev_class *class, const char *name,
277                   struct netdev_dev **netdev_devp)
278 {
279     struct netdev_dev_bsd *netdev_dev;
280     int error;
281
282     error = cache_notifier_ref();
283     if (error) {
284         return error;
285     }
286
287     netdev_dev = xzalloc(sizeof *netdev_dev);
288     netdev_dev->change_seq = 1;
289     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
290     *netdev_devp = &netdev_dev->netdev_dev;
291
292     return 0;
293 }
294
295 /* 
296  * Allocate a netdev_dev_bsd structure with 'tap' class.
297  */
298 static int
299 netdev_bsd_create_tap(const struct netdev_class *class, const char *name,
300                   struct netdev_dev **netdev_devp)
301 {
302     struct netdev_dev_bsd *netdev_dev = NULL;
303     int error = 0;
304     struct ifreq ifr;
305
306     error = cache_notifier_ref();
307     if (error) {
308         goto error;
309     }
310
311     /* allocate the device structure and set the internal flag */
312     netdev_dev = xzalloc(sizeof *netdev_dev);
313     
314     memset(&ifr, 0, sizeof(ifr));
315
316     /* Create a tap device by opening /dev/tap.  The TAPGIFNAME ioctl is used
317      * to retrieve the name of the tap device. */
318     netdev_dev->tap_fd = open("/dev/tap", O_RDWR);
319     if (netdev_dev->tap_fd < 0) {
320         error = errno;
321         VLOG_WARN("opening \"/dev/tap\" failed: %s", strerror(error));
322         goto error_undef_notifier;
323     }
324     
325     /* Retrieve tap name (e.g. tap0) */
326     if (ioctl(netdev_dev->tap_fd, TAPGIFNAME, &ifr) == -1) {
327         /* XXX Need to destroy the device? */
328         error = errno;
329         goto error_undef_notifier;
330     }
331     
332     /* Change the name of the tap device */
333     ifr.ifr_data = (void *)name;
334     if (ioctl(af_inet_sock, SIOCSIFNAME, &ifr) == -1) {
335         error = errno;
336         destroy_tap(netdev_dev->tap_fd, ifr.ifr_name);
337         goto error_undef_notifier;
338     }
339
340     /* set non-blocking. */
341     error = set_nonblocking(netdev_dev->tap_fd);
342     if (error) {
343         destroy_tap(netdev_dev->tap_fd, name);
344         goto error_undef_notifier;
345     }
346  
347     /* Turn device UP */
348     ifr.ifr_flags = (uint16_t)IFF_UP;
349     ifr.ifr_flagshigh = 0;
350     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
351     if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) {
352         error = errno;
353         destroy_tap(netdev_dev->tap_fd, name);
354         goto error_undef_notifier;
355     }
356
357     /* initialize the device structure and
358      * link the structure to its netdev */
359     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
360     *netdev_devp = &netdev_dev->netdev_dev;
361
362     return 0;
363
364 error_undef_notifier:
365     cache_notifier_unref();
366 error:
367     free(netdev_dev);
368     return error;
369 }
370
371 static void
372 netdev_bsd_destroy(struct netdev_dev *netdev_dev_)
373 {
374     struct netdev_dev_bsd *netdev_dev = netdev_dev_bsd_cast(netdev_dev_);
375
376     cache_notifier_unref();
377
378     if (netdev_dev->tap_fd >= 0 && 
379             !strcmp(netdev_dev_get_type(netdev_dev_), "tap")) {
380         destroy_tap(netdev_dev->tap_fd, netdev_dev_get_name(netdev_dev_));
381     }
382     free(netdev_dev);
383 }
384
385
386 static int
387 netdev_bsd_open_system(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
388 {
389     struct netdev_dev_bsd *netdev_dev = netdev_dev_bsd_cast(netdev_dev_);
390     struct netdev_bsd *netdev;
391     int error;
392     enum netdev_flags flags;
393     
394     /* Allocate network device. */
395     netdev = xcalloc(1, sizeof *netdev);
396     netdev->netdev_fd = -1;
397     netdev_init(&netdev->netdev, netdev_dev_);
398
399     /* Verify that the netdev really exists by attempting to read its flags */
400     error = netdev_get_flags(&netdev->netdev, &flags);
401     if (error == ENXIO) {
402         goto error;
403     }
404
405     /* The first user that opens a tap port(from dpif_create_and_open()) will
406      * receive the file descriptor associated with the tap device. Instead, the
407      * following users will open the tap device as a normal 'system' device. */
408     if (!strcmp(netdev_dev_get_type(netdev_dev_), "tap") &&
409             !netdev_dev->tap_opened) {
410         netdev_dev->tap_opened = true;
411         netdev->netdev_fd = netdev_dev->tap_fd;
412     } 
413
414     *netdevp = &netdev->netdev;
415     return 0;
416
417 error:
418     netdev_uninit(&netdev->netdev, true);
419     return error;
420 }
421
422
423
424 /* Close a 'netdev'. */
425 static void
426 netdev_bsd_close(struct netdev *netdev_)
427 {
428     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
429
430     if (netdev->netdev_fd >= 0 && strcmp(netdev_get_type(netdev_), "tap")) {
431         pcap_close(netdev->pcap_handle);
432     }
433
434     free(netdev);
435 }
436
437 static int
438 netdev_bsd_listen(struct netdev *netdev_)
439 {
440     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
441     char errbuf[PCAP_ERRBUF_SIZE];
442     int error;
443     int fd;
444     int one = 1;
445
446     if (netdev->netdev_fd >= 0) {
447         return 0;
448     }
449     
450     /* open the pcap device. The device is opened in non-promiscuous mode
451      * because the interface flags are manually set by the caller. */
452     netdev->pcap_handle = pcap_open_live(netdev_get_name(netdev_), PCAP_SNAPLEN,
453                                      0, 1000, errbuf);
454     if (netdev->pcap_handle == NULL) {
455         error = errno;
456         goto error;
457     }
458     
459     /* initialize netdev->netdev_fd */
460     fd = pcap_get_selectable_fd(netdev->pcap_handle);
461     if (fd == -1) {
462         error = errno;
463         goto error;
464     }
465     
466     /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
467      * on the file descriptor returned by pcap_get_selectable_fd to achieve
468      * a real non-blocking behaviour.*/
469     error = pcap_setnonblock(netdev->pcap_handle, 1, errbuf);
470     if (error == -1) {
471         error = errno;
472         goto error;
473     }
474     
475     /* This call assure that reads return immediately upon packet reception.
476      * Otherwise, a read will block until either the kernel buffer becomes
477      * full or a timeout occurs. */
478     if(ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
479         VLOG_ERR("ioctl(BIOCIMMEDIATE) on %s device failed: %s",
480              netdev_get_name(netdev_), strerror(errno));
481         error = errno;
482         goto error;
483     }
484     
485     /* Capture only incoming packets */
486     error = pcap_setdirection(netdev->pcap_handle, PCAP_D_IN);
487     if (error == -1) {
488         error = errno;
489         goto error;
490     }
491
492     netdev->netdev_fd = fd;
493     return 0;
494
495 error:
496     if (fd >= 0) {
497         close(netdev->netdev_fd);
498     }
499     return error;
500 }
501
502
503 /* The recv callback of the netdev class returns the number of bytes of the
504  * received packet.
505  *
506  * This can be done by the pcap_next() function. Unfortunately pcap_next() does
507  * not make difference between a missing packet on the capture interface and
508  * an error during the file capture.  We can use the pcap_dispatch() function 
509  * instead, which is able to distinguish between errors and null packet.
510  *
511  * To make pcap_dispatch() returns the number of bytes read from the interface
512  * we need to define the following callback and argument. 
513  */
514 struct pcap_arg {
515     void *data;
516     int size;
517     int retval;
518 };
519
520 /*
521  * This callback will be executed on every captured packet.
522  * 
523  * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
524  * pcap returns a truncated packet and we follow this behavior.
525  *
526  * The argument args->retval is the packet size in bytes.
527  */
528 static void 
529 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
530 {
531     struct pcap_arg *args = (struct pcap_arg *)args_;
532
533     if (args->size < hdr->len) {
534         printf("%s Warning: Packet truncated'n", __func__);
535         args->retval = args->size;
536     } else {
537         args->retval = hdr->len;
538     }
539
540     /* copy the packet to our buffer */
541     memcpy(args->data, packet, args->retval);
542 }
543
544 /*
545  * This function attempts to receive a packet from the specified network
546  * device. It is assumed that the network device is a system device or a tap
547  * device opened as a system one. In this case the read operation is performed
548  * on the 'netdev' pcap descriptor.
549  */
550 static int
551 netdev_bsd_recv_system(struct netdev_bsd *netdev, void *data, size_t size)
552 {
553     struct pcap_arg arg;
554     int ret;
555
556     if (netdev->netdev_fd < 0) {
557         /* Device was opened with NETDEV_ETH_TYPE_NONE. */
558         return -EAGAIN;
559     }
560     
561     /* prepare the pcap argument to store the packet */
562     arg.size = size;
563     arg.data = data;
564
565     for (;;) {
566         ret = pcap_dispatch(netdev->pcap_handle, 1, proc_pkt, (u_char *)&arg);
567
568         if (ret > 0) {
569             return arg.retval;  /* arg.retval < 0 is handled in the caller */
570         }
571         if (ret == -1) {
572             if (errno == EINTR) {
573                  continue;
574             }
575         }
576
577         return -EAGAIN;
578     }
579 }
580
581 /*
582  * This function attempts to receive a packet from the specified network
583  * device. It is assumed that the network device is a tap device and the
584  * 'netdev_fd' member of the 'netdev' structure is initialized with the tap
585  * file descriptor.
586  */
587 static int
588 netdev_bsd_recv_tap(struct netdev_bsd *netdev, void *data, size_t size)
589 {
590     if (netdev->netdev_fd < 0) {
591         /* Device was opened with NETDEV_ETH_TYPE_NONE. */
592         return -EAGAIN;
593     }
594     
595     for (;;) {
596         ssize_t retval = read(netdev->netdev_fd, data, size);
597         if (retval >= 0) {
598             return retval;
599         } else if (errno != EINTR) {
600             if (errno != EAGAIN) {
601                 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
602                              strerror(errno), netdev->netdev.netdev_dev->name);
603             }
604             return -errno;
605         }
606     }
607 }
608
609
610 /*
611  * According with the nature of the device a different function must be called.
612  * If the device is the bridge local port the 'netdev_bsd_recv_tap' function
613  * must be called, otherwise the 'netdev_bsd_recv_system' function is called.
614  *
615  * type!="tap"                                        --->  system device.
616  * type=="tap" && netdev_fd == tap_fd                 --->  internal tap device
617  * type=="tap" && netdev_fd != tap_fd                 --->  internal tap device
618  *                                                          opened as a system
619  *                                                          device. 
620  */
621 static int
622 netdev_bsd_recv(struct netdev *netdev_, void* data, size_t size)
623 {
624     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
625     struct netdev_dev_bsd * netdev_dev =
626         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
627
628     if (!strcmp(netdev_get_type(netdev_), "tap") && 
629             netdev->netdev_fd == netdev_dev->tap_fd) {
630         return netdev_bsd_recv_tap(netdev, data, size);
631     } else {
632         return netdev_bsd_recv_system(netdev, data, size);
633     }
634 }
635
636
637 /* 
638  * Registers with the poll loop to wake up from the next call to poll_block()
639  * when a packet is ready to be received with netdev_recv() on 'netdev'.
640  */
641 static void
642 netdev_bsd_recv_wait(struct netdev *netdev_)
643 {
644     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
645
646     if (netdev->netdev_fd >= 0) {
647         poll_fd_wait(netdev->netdev_fd, POLLIN);
648     }
649 }
650
651 /* Discards all packets waiting to be received from 'netdev'. */
652 static int
653 netdev_bsd_drain(struct netdev *netdev_)
654 {
655     struct ifreq ifr;
656     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
657
658     strcpy(ifr.ifr_name, netdev_get_name(netdev_));
659     if (ioctl(netdev->netdev_fd, BIOCFLUSH, &ifr) == -1) {
660         VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
661                     netdev_get_name(netdev_), strerror(errno));
662         return errno;
663     }
664     return 0;
665 }
666
667 /* 
668  * Send a packet on the specified network device. The device could be either a
669  * system or a tap device.
670  */
671 static int
672 netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
673 {
674     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
675     struct netdev_dev_bsd * netdev_dev =
676         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
677
678     /* XXX should support sending even if 'ethertype' was NETDEV_ETH_TYPE_NONE.
679      */
680     if (netdev->netdev_fd < 0) {
681         return EPIPE;
682     }
683
684     for (;;) {
685         ssize_t retval;
686         if (!strcmp(netdev_get_type(netdev_), "tap") && 
687                 netdev_dev->tap_fd == netdev->netdev_fd) {
688             retval = write(netdev->netdev_fd, data, size);
689         } else {
690             retval = pcap_inject(netdev->pcap_handle, data, size);
691         }
692         if (retval < 0) {
693             if (errno == EINTR) {
694                 continue;
695             } else if (errno != EAGAIN) {
696                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
697                              netdev_get_name(netdev_), strerror(errno));
698             }
699             return errno;
700         } else if (retval != size) {
701             VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
702                          "%zu) on %s", retval, size,
703                          netdev_get_name(netdev_));
704            return EMSGSIZE;
705         } else {
706             return 0;
707         }
708     }
709 }
710
711 /*
712  * Registers with the poll loop to wake up from the next call to poll_block()
713  * when the packet transmission queue has sufficient room to transmit a packet
714  * with netdev_send().
715  */
716 static void
717 netdev_bsd_send_wait(struct netdev *netdev_)
718 {
719     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
720
721     if (netdev->netdev_fd < 0) { /* Nothing to do. */
722         return;
723     }
724
725     if (strcmp(netdev_get_type(netdev_), "tap")) {
726         poll_fd_wait(netdev->netdev_fd, POLLOUT);
727     } else {
728         /* TAP device always accepts packets. */
729         poll_immediate_wake();
730     }
731 }
732
733 /*
734  * Attempts to set 'netdev''s MAC address to 'mac'.  Returns 0 if successful,
735  * otherwise a positive errno value.
736  */
737 static int
738 netdev_bsd_set_etheraddr(struct netdev *netdev_,
739                            const uint8_t mac[ETH_ADDR_LEN])
740 {
741     struct netdev_dev_bsd *netdev_dev =
742                                 netdev_dev_bsd_cast(netdev_get_dev(netdev_));
743     int error;
744     
745     if (!(netdev_dev->cache_valid & VALID_ETHERADDR)
746         || !eth_addr_equals(netdev_dev->etheraddr, mac)) {
747         error = set_etheraddr(netdev_get_name(netdev_), AF_LINK, ETH_ADDR_LEN,
748                               mac);
749         if (!error) {
750             netdev_dev->cache_valid |= VALID_ETHERADDR;
751             memcpy(netdev_dev->etheraddr, mac, ETH_ADDR_LEN);
752         }
753     } else {
754         error = 0;
755     }
756     return error;
757 }
758
759 /*
760  * Returns a pointer to 'netdev''s MAC address.  The caller must not modify or
761  * free the returned buffer.
762  */
763 static int
764 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
765                            uint8_t mac[ETH_ADDR_LEN])
766 {
767     struct netdev_dev_bsd *netdev_dev =
768         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
769     
770     if (!(netdev_dev->cache_valid & VALID_ETHERADDR)) {
771         int error = get_etheraddr(netdev_get_name(netdev_), 
772                                   netdev_dev->etheraddr);
773         if (error) {
774             return error;
775         }
776         netdev_dev->cache_valid |= VALID_ETHERADDR;
777     }
778     memcpy(mac, netdev_dev->etheraddr, ETH_ADDR_LEN);
779     
780     return 0;
781 }
782
783 /*
784  * Returns the maximum size of transmitted (and received) packets on 'netdev',
785  * in bytes, not including the hardware header; thus, this is typically 1500
786  * bytes for Ethernet devices.
787  */
788 static int
789 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
790 {
791     struct netdev_dev_bsd *netdev_dev = 
792         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
793     
794     if (!(netdev_dev->cache_valid & VALID_MTU)) {
795         struct ifreq ifr;
796         int error;
797
798         error = netdev_bsd_do_ioctl(netdev_, &ifr, SIOCGIFMTU, "SIOCGIFMTU");
799         if (error) {
800             return error;
801         }
802         netdev_dev->mtu = ifr.ifr_mtu;
803         netdev_dev->cache_valid |= VALID_MTU;
804     }
805
806     *mtup = netdev_dev->mtu;
807     return 0;
808 }
809
810 static int
811 netdev_bsd_get_ifindex(const struct netdev *netdev)
812 {
813     int ifindex, error;
814     
815     error = get_ifindex(netdev, &ifindex);
816     return error ? -error : ifindex;
817 }
818
819 static int
820 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
821 {
822     struct netdev_dev_bsd *netdev_dev =
823         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
824     
825     if (!(netdev_dev->cache_valid & VALID_CARRIER)) {
826         struct ifmediareq ifmr;
827
828         memset(&ifmr, 0, sizeof(ifmr));
829         strncpy(ifmr.ifm_name, netdev_get_name(netdev_), sizeof ifmr.ifm_name);
830
831         if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
832             VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
833                         netdev_get_name(netdev_), strerror(errno));
834             return errno;
835         }
836
837         netdev_dev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
838         netdev_dev->cache_valid |= VALID_CARRIER;
839
840         /* If the interface doesn't report whether the media is active,
841          * just assume it is active. */
842         if ((ifmr.ifm_status & IFM_AVALID) == 0) {
843             netdev_dev->carrier = true;
844         }
845     }
846     *carrier = netdev_dev->carrier;
847     
848     return 0;
849 }
850
851 /* Retrieves current device stats for 'netdev'. */
852 static int
853 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
854 {
855     int if_count, i;
856     int mib[6];
857     size_t len;
858     struct ifmibdata ifmd;
859
860     //COVERAGE_INC(netdev_get_stats);
861
862     mib[0] = CTL_NET;
863     mib[1] = PF_LINK;
864     mib[2] = NETLINK_GENERIC;
865     mib[3] = IFMIB_SYSTEM;
866     mib[4] = IFMIB_IFCOUNT;
867
868     len = sizeof(if_count);
869
870     if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
871         VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
872                     netdev_get_name(netdev_), strerror(errno));
873         return errno;
874     }
875     
876     mib[5] = IFDATA_GENERAL;
877     mib[3] = IFMIB_IFDATA;
878     len = sizeof(ifmd);
879     for (i = 1; i <= if_count; i++) {
880         mib[4] = i; //row
881         if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
882             VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
883                         netdev_get_name(netdev_), strerror(errno));
884             return errno;
885         } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
886             stats->rx_packets = ifmd.ifmd_data.ifi_ipackets;        
887             stats->tx_packets = ifmd.ifmd_data.ifi_opackets;
888             stats->rx_bytes = ifmd.ifmd_data.ifi_ibytes;          
889             stats->tx_bytes = ifmd.ifmd_data.ifi_obytes;          
890             stats->rx_errors = ifmd.ifmd_data.ifi_ierrors;         
891             stats->tx_errors = ifmd.ifmd_data.ifi_oerrors;       
892             stats->rx_dropped = ifmd.ifmd_data.ifi_iqdrops;        
893             stats->tx_dropped = 0;        
894             stats->multicast = ifmd.ifmd_data.ifi_imcasts;         
895             stats->collisions = ifmd.ifmd_data.ifi_collisions;
896
897             stats->rx_length_errors = 0;
898             stats->rx_over_errors = 0;   
899             stats->rx_crc_errors = 0;  
900             stats->rx_frame_errors = 0;
901             stats->rx_fifo_errors = 0;    
902             stats->rx_missed_errors = 0;  
903
904             stats->tx_aborted_errors = 0;
905             stats->tx_carrier_errors = 0;
906             stats->tx_fifo_errors = 0;
907             stats->tx_heartbeat_errors = 0;
908             stats->tx_window_errors = 0;
909             break;
910         }
911     }
912
913     return 0;
914 }
915
916 static uint32_t
917 netdev_bsd_parse_media(int media)
918 {
919     uint32_t supported = 0;
920     bool half_duplex = media & IFM_HDX ? true : false;
921
922     switch (IFM_SUBTYPE(media)) {
923     case IFM_10_2:
924     case IFM_10_5:
925     case IFM_10_STP:
926     case IFM_10_T:
927         supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
928         supported |= NETDEV_F_COPPER;
929         break;
930
931     case IFM_10_FL:
932         supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
933         supported |= NETDEV_F_FIBER;
934         break;
935
936     case IFM_100_T2:
937     case IFM_100_T4:
938     case IFM_100_TX:
939     case IFM_100_VG:
940         supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
941         supported |= NETDEV_F_COPPER;
942         break;
943
944     case IFM_100_FX:
945         supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
946         supported |= NETDEV_F_FIBER;
947         break;
948
949     case IFM_1000_CX:
950     case IFM_1000_T:
951         supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
952         supported |= NETDEV_F_COPPER;
953         break;
954
955     case IFM_1000_LX:
956     case IFM_1000_SX:
957         supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
958         supported |= NETDEV_F_FIBER;
959         break;
960
961     case IFM_10G_CX4:
962         supported |= NETDEV_F_10GB_FD;
963         supported |= NETDEV_F_COPPER;
964         break;
965
966     case IFM_10G_LR:
967     case IFM_10G_SR:
968         supported |= NETDEV_F_10GB_FD;
969         supported |= NETDEV_F_FIBER;
970         break;
971
972     default:
973         return 0;
974     }
975
976     if (IFM_SUBTYPE(media) == IFM_AUTO) {
977         supported |= NETDEV_F_AUTONEG;
978     }
979     /*
980     if (media & IFM_ETH_FMASK) {
981         supported |= NETDEV_F_PAUSE;
982     }
983     */
984
985     return supported;
986 }
987
988 /* 
989  * Stores the features supported by 'netdev' into each of '*current',
990  * '*advertised', '*supported', and '*peer' that are non-null.  Each value is a
991  * bitmap of "enum ofp_port_features" bits, in host byte order.  Returns 0 if
992  * successful, otherwise a positive errno value.  On failure, all of the
993  * passed-in values are set to 0.
994  */
995 static int
996 netdev_bsd_get_features(const struct netdev *netdev,
997                           enum netdev_features *current, uint32_t *advertised,
998                           enum netdev_features *supported, uint32_t *peer)
999 {
1000     struct ifmediareq ifmr;
1001     int *media_list;
1002     int i;
1003     int error;
1004
1005
1006     /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1007
1008     memset(&ifmr, 0, sizeof(ifmr));
1009     strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1010
1011     /* We make two SIOCGIFMEDIA ioctl calls.  The first to determine the
1012      * number of supported modes, and a second with a buffer to retrieve
1013      * them. */
1014     if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
1015         VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1016                     netdev_get_name(netdev), strerror(errno));
1017         return errno;
1018     }
1019
1020     media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1021     ifmr.ifm_ulist = media_list;
1022
1023     if (!IFM_TYPE(ifmr.ifm_current) & IFM_ETHER) {
1024         VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1025                     netdev_get_name(netdev));
1026         error = EINVAL;
1027         goto cleanup;
1028     }
1029
1030     if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
1031         VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1032                     netdev_get_name(netdev), strerror(errno));
1033         error = errno;
1034         goto cleanup;
1035     }
1036
1037     /* Current settings. */
1038     *current = netdev_bsd_parse_media(ifmr.ifm_active);
1039
1040     /* Advertised features. */
1041     *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1042
1043     /* Supported features. */
1044     *supported = 0;
1045     for (i = 0; i < ifmr.ifm_count; i++) {
1046         *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1047     }
1048
1049     /* Peer advertisements. */
1050     *peer = 0;                  /* XXX */
1051
1052     error = 0;
1053 cleanup:
1054     free(media_list);
1055     return error;
1056 }
1057
1058 /*
1059  * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
1060  * 'in4' is non-null) and returns true.  Otherwise, returns false.
1061  */
1062 static int
1063 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4, 
1064                    struct in_addr *netmask)
1065 {
1066     struct netdev_dev_bsd *netdev_dev =
1067         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
1068
1069     if (!(netdev_dev->cache_valid & VALID_IN4)) {
1070         const struct sockaddr_in *sin;
1071         struct ifreq ifr;
1072         int error;
1073
1074         ifr.ifr_addr.sa_family = AF_INET;
1075         error = netdev_bsd_do_ioctl(netdev_, &ifr,
1076                                       SIOCGIFADDR, "SIOCGIFADDR");
1077         if (error) {
1078             return error;
1079         }
1080
1081         sin = (struct sockaddr_in *) &ifr.ifr_addr;
1082         netdev_dev->in4 = sin->sin_addr;
1083         netdev_dev->cache_valid |= VALID_IN4;
1084         error = netdev_bsd_do_ioctl(netdev_, &ifr,
1085                                       SIOCGIFNETMASK, "SIOCGIFNETMASK");
1086         if (error) {
1087             return error;
1088         }
1089         *netmask = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr;
1090     }
1091     *in4 = netdev_dev->in4;
1092
1093     return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1094 }
1095
1096 /*
1097  * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
1098  * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.  Returns a
1099  * positive errno value.
1100  */
1101 static int
1102 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1103                      struct in_addr mask)
1104 {
1105     struct netdev_dev_bsd *netdev_dev =
1106         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
1107     int error;
1108     
1109     error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1110     if (!error) {
1111         netdev_dev->cache_valid |= VALID_IN4;
1112         netdev_dev->in4 = addr;
1113         if (addr.s_addr != INADDR_ANY) {
1114             error = do_set_addr(netdev_, SIOCSIFNETMASK,
1115                                 "SIOCSIFNETMASK", mask);
1116         }
1117     }
1118     return error;
1119 }
1120
1121 static int
1122 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1123 {
1124     struct netdev_dev_bsd *netdev_dev =
1125                                 netdev_dev_bsd_cast(netdev_get_dev(netdev_));
1126     if (!(netdev_dev->cache_valid & VALID_IN6)) {
1127         struct ifaddrs *ifa, *head;
1128         struct sockaddr_in6 *sin6;
1129         const char *netdev_name = netdev_get_name(netdev_);
1130
1131         if (getifaddrs(&head) != 0) {
1132             VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name, 
1133                     strerror(errno));
1134             return errno;
1135         }
1136
1137         for (ifa = head; ifa; ifa = ifa->ifa_next) {
1138             if (ifa->ifa_addr->sa_family == AF_INET6 && 
1139                     !strcmp(ifa->ifa_name, netdev_name)) {
1140                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1141                 if (sin6) {
1142                     memcpy(&netdev_dev->in6, &sin6->sin6_addr, sin6->sin6_len);
1143                     netdev_dev->cache_valid |= VALID_IN6;
1144                     *in6 = netdev_dev->in6;
1145                     freeifaddrs(head);
1146                     return 0;
1147                 } 
1148             }
1149         }
1150         return EADDRNOTAVAIL;
1151     }
1152     *in6 = netdev_dev->in6;
1153     return 0;
1154 }
1155
1156 static void
1157 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1158 {
1159     struct sockaddr_in sin;
1160     memset(&sin, 0, sizeof sin);
1161     sin.sin_family = AF_INET;
1162     sin.sin_addr = addr;
1163     sin.sin_port = 0;
1164
1165     memset(sa, 0, sizeof *sa);
1166     memcpy(sa, &sin, sizeof sin);
1167 }
1168
1169 static int
1170 do_set_addr(struct netdev *netdev,
1171             int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1172 {
1173     struct ifreq ifr;
1174     make_in4_sockaddr(&ifr.ifr_addr, addr);
1175     return netdev_bsd_do_ioctl(netdev, &ifr, ioctl_nr, ioctl_name);
1176 }
1177
1178 static int
1179 nd_to_iff_flags(enum netdev_flags nd)
1180 {
1181     int iff = 0;
1182     if (nd & NETDEV_UP) {
1183         iff |= IFF_UP;
1184     }
1185     if (nd & NETDEV_PROMISC) {
1186         iff |= IFF_PROMISC;
1187         iff |= IFF_PPROMISC;
1188     }
1189     return iff;
1190 }
1191
1192 static int
1193 iff_to_nd_flags(int iff)
1194 {
1195     enum netdev_flags nd = 0;
1196     if (iff & IFF_UP) {
1197         nd |= NETDEV_UP;
1198     }
1199     if (iff & IFF_PROMISC) { 
1200         nd |= NETDEV_PROMISC;
1201     }
1202     return nd;
1203 }
1204
1205 static int
1206 netdev_bsd_update_flags(struct netdev *netdev, enum netdev_flags off,
1207                           enum netdev_flags on, enum netdev_flags *old_flagsp)
1208 {
1209     int old_flags, new_flags;
1210     int error;
1211
1212     error = get_flags(netdev, &old_flags);
1213     if (!error) {
1214         *old_flagsp = iff_to_nd_flags(old_flags);
1215         new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1216         if (new_flags != old_flags) {
1217             error = set_flags(netdev, new_flags);
1218         }
1219     }
1220     return error;
1221 }
1222
1223 static unsigned int
1224 netdev_bsd_change_seq(const struct netdev *netdev)
1225 {
1226     return netdev_dev_bsd_cast(netdev_get_dev(netdev))->change_seq;
1227 }
1228
1229
1230 const struct netdev_class netdev_bsd_class = {
1231     "system",
1232
1233     netdev_bsd_init,
1234     netdev_bsd_run,
1235     netdev_bsd_wait,
1236     netdev_bsd_create_system,
1237     netdev_bsd_destroy,
1238     NULL, /* get_config */
1239     NULL, /* set_config */
1240     netdev_bsd_open_system,
1241     netdev_bsd_close,
1242
1243     netdev_bsd_listen,
1244
1245     netdev_bsd_recv,
1246     netdev_bsd_recv_wait,
1247     netdev_bsd_drain,
1248
1249     netdev_bsd_send,
1250     netdev_bsd_send_wait,
1251
1252     netdev_bsd_set_etheraddr,
1253     netdev_bsd_get_etheraddr,
1254     netdev_bsd_get_mtu,
1255     NULL, /* set_mtu */
1256     netdev_bsd_get_ifindex,
1257     netdev_bsd_get_carrier,
1258     NULL, /* get_carrier_resets */
1259     NULL, /* set_miimon_interval */
1260     netdev_bsd_get_stats,
1261     NULL, /* set_stats */
1262
1263     netdev_bsd_get_features,
1264     NULL, /* set_advertisement */
1265     NULL, /* set_policing */
1266     NULL, /* get_qos_type */
1267     NULL, /* get_qos_capabilities */
1268     NULL, /* get_qos */
1269     NULL, /* set_qos */
1270     NULL, /* get_queue */
1271     NULL, /* set_queue */
1272     NULL, /* delete_queue */
1273     NULL, /* get_queue_stats */
1274     NULL, /* dump_queue */
1275     NULL, /* dump_queue_stats */
1276
1277     netdev_bsd_get_in4,
1278     netdev_bsd_set_in4,
1279     netdev_bsd_get_in6,
1280     NULL, /* add_router */
1281     NULL, /* get_next_hop */
1282     NULL, /* get_drv_info */
1283     NULL, /* arp_lookup */
1284
1285     netdev_bsd_update_flags,
1286
1287     netdev_bsd_change_seq
1288 };
1289
1290 const struct netdev_class netdev_tap_class = {
1291     "tap",  
1292
1293     netdev_bsd_init,
1294     netdev_bsd_run,
1295     netdev_bsd_wait,
1296     netdev_bsd_create_tap,
1297     netdev_bsd_destroy,
1298     NULL, /* get_config */
1299     NULL, /* set_config */
1300     netdev_bsd_open_system,
1301     netdev_bsd_close,
1302
1303     netdev_bsd_listen,
1304
1305     netdev_bsd_recv,
1306     netdev_bsd_recv_wait,
1307     netdev_bsd_drain,
1308
1309     netdev_bsd_send,
1310     netdev_bsd_send_wait,
1311
1312     netdev_bsd_set_etheraddr,
1313     netdev_bsd_get_etheraddr,
1314     netdev_bsd_get_mtu,
1315     NULL, /* set_mtu */
1316     netdev_bsd_get_ifindex,
1317     netdev_bsd_get_carrier,
1318     NULL, /* get_carrier_resets */
1319     NULL, /* set_miimon_interval */
1320     netdev_bsd_get_stats,
1321     NULL, /* set_stats */
1322
1323     netdev_bsd_get_features,
1324     NULL, /* set_advertisement */
1325     NULL, /* set_policing */
1326     NULL, /* get_qos_type */
1327     NULL, /* get_qos_capabilities */
1328     NULL, /* get_qos */
1329     NULL, /* set_qos */
1330     NULL, /* get_queue */
1331     NULL, /* set_queue */
1332     NULL, /* delete_queue */
1333     NULL, /* get_queue_stats */
1334     NULL, /* dump_queue */
1335     NULL, /* dump_queue_stats */
1336
1337     netdev_bsd_get_in4,
1338     netdev_bsd_set_in4,
1339     netdev_bsd_get_in6,
1340     NULL, /* add_router */
1341     NULL, /* get_next_hop */
1342     NULL, /* get_drv_info */
1343     NULL, /* arp_lookup */
1344
1345     netdev_bsd_update_flags,
1346
1347     netdev_bsd_change_seq
1348 }; 
1349 \f
1350
1351 static void 
1352 destroy_tap(int fd, const char *name)
1353 {
1354     struct ifreq ifr;
1355
1356     close(fd);
1357     strcpy(ifr.ifr_name, name);
1358     /* XXX What to do if this call fails? */
1359     ioctl(af_inet_sock, SIOCIFDESTROY, &ifr);
1360 }
1361
1362 static int
1363 get_flags(const struct netdev *netdev, int *flags)
1364 {
1365     struct ifreq ifr;
1366     int error;
1367     
1368     error = netdev_bsd_do_ioctl(netdev, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
1369
1370     *flags = 0xFFFF0000 & (ifr.ifr_flagshigh << 16);
1371     *flags |= 0x0000FFFF & ifr.ifr_flags;
1372
1373     return error;
1374 }
1375
1376 static int
1377 set_flags(struct netdev *netdev, int flags)
1378 {
1379     struct ifreq ifr;
1380
1381     ifr.ifr_flags = 0x0000FFFF & flags;
1382     ifr.ifr_flagshigh = (0xFFFF0000 & flags) >> 16;
1383
1384     return netdev_bsd_do_ioctl(netdev, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1385 }
1386
1387 static int
1388 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1389 {
1390     struct netdev_dev_bsd *netdev_dev =
1391                                 netdev_dev_bsd_cast(netdev_get_dev(netdev_));
1392     *ifindexp = 0;
1393     if (!(netdev_dev->cache_valid & VALID_IFINDEX)) {
1394         int ifindex = if_nametoindex(netdev_get_name(netdev_));
1395         if (ifindex <= 0) {
1396             return errno;
1397         }
1398         netdev_dev->cache_valid |= VALID_IFINDEX;
1399         netdev_dev->ifindex = ifindex;
1400     }
1401     *ifindexp = netdev_dev->ifindex;
1402     return 0;
1403 }
1404
1405 static int
1406 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1407 {
1408     struct ifaddrs *head;
1409     struct ifaddrs *ifa;
1410     struct sockaddr_dl *sdl;
1411
1412     if (getifaddrs(&head) != 0) {
1413         VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name, 
1414                 strerror(errno));
1415         return errno;
1416     }
1417
1418     for (ifa = head; ifa; ifa = ifa->ifa_next) {
1419         if (ifa->ifa_addr->sa_family == AF_LINK) {
1420             if (!strcmp(ifa->ifa_name, netdev_name)) {
1421                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1422                 if (sdl) {
1423                     memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1424                     freeifaddrs(head);
1425                     return 0;
1426                 }
1427             }
1428         }
1429     }
1430
1431     VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1432     freeifaddrs(head);
1433     return ENODEV;
1434 }
1435
1436 static int
1437 set_etheraddr(const char *netdev_name, int hwaddr_family,
1438               int hwaddr_len, const uint8_t mac[ETH_ADDR_LEN])
1439 {
1440     struct ifreq ifr;
1441     
1442     memset(&ifr, 0, sizeof ifr);
1443     strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1444     ifr.ifr_addr.sa_family = hwaddr_family;
1445     ifr.ifr_addr.sa_len = hwaddr_len;
1446     memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1447     //COVERAGE_INC(netdev_set_hwaddr);
1448     if (ioctl(af_inet_sock, SIOCSIFLLADDR, &ifr) < 0) {
1449         VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1450                  netdev_name, strerror(errno));
1451         return errno;
1452     }
1453     return 0;
1454 }
1455
1456 static int
1457 netdev_bsd_do_ioctl(const struct netdev *netdev, struct ifreq *ifr,
1458                     unsigned long cmd, const char *cmd_name)
1459 {
1460     strncpy(ifr->ifr_name, netdev_get_name(netdev), sizeof ifr->ifr_name);
1461     if (ioctl(af_inet_sock, cmd, ifr) == -1) {
1462         VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s",
1463                     netdev_get_name(netdev), cmd_name, strerror(errno));
1464         return errno;
1465     }
1466     return 0;
1467 }