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