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