netdev: New function netdev_drain().
[sliver-openvswitch.git] / lib / netdev.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include "netdev.h"
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <arpa/inet.h>
39 #include <inttypes.h>
40 #include <linux/types.h>
41 #include <linux/ethtool.h>
42 #include <linux/sockios.h>
43 #include <sys/types.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <netpacket/packet.h>
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_arp.h>
50 #include <net/if_packet.h>
51 #include <netinet/in.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "list.h"
57 #include "fatal-signal.h"
58 #include "buffer.h"
59 #include "openflow.h"
60 #include "packets.h"
61 #include "poll-loop.h"
62 #include "socket-util.h"
63
64 #define THIS_MODULE VLM_netdev
65 #include "vlog.h"
66
67 struct netdev {
68     struct list node;
69     char *name;
70     int fd;
71     uint8_t etheraddr[ETH_ADDR_LEN];
72     int speed;
73     int mtu;
74     uint32_t features;
75     struct in_addr in4;
76     struct in6_addr in6;
77     int save_flags;
78 };
79
80 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
81
82 /* An AF_INET socket (used for ioctl operations). */
83 static int af_inet_sock = -1;
84
85 static void init_netdev(void);
86 static int restore_flags(struct netdev *netdev);
87 static int get_flags(const struct netdev *, int *flagsp);
88 static int set_flags(struct netdev *, int flags);
89
90 /* Obtains the IPv4 address for 'name' into 'in4'.  Returns true if
91  * successful. */
92 static bool
93 get_ipv4_address(const char *name, struct in_addr *in4)
94 {
95     struct ifreq ifr;
96
97     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
98     ifr.ifr_addr.sa_family = AF_INET;
99     if (ioctl(af_inet_sock, SIOCGIFADDR, &ifr) == 0) {
100         struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
101         *in4 = sin->sin_addr;
102     } else {
103         in4->s_addr = INADDR_ANY;
104     }
105
106     return true;
107 }
108
109 /* Obtains the IPv6 address for 'name' into 'in6'. */
110 static void
111 get_ipv6_address(const char *name, struct in6_addr *in6)
112 {
113     FILE *file;
114     char line[128];
115
116     file = fopen("/proc/net/if_inet6", "r");
117     if (file == NULL) {
118         /* This most likely indicates that the host doesn't have IPv6 support,
119          * so it's not really a failure condition.*/
120         *in6 = in6addr_any;
121         return;
122     }
123
124     while (fgets(line, sizeof line, file)) {
125         uint8_t *s6 = in6->s6_addr;
126         char ifname[16 + 1];
127
128 #define X8 "%2"SCNx8
129         if (sscanf(line, " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
130                    "%*x %*x %*x %*x %16s\n",
131                    &s6[0], &s6[1], &s6[2], &s6[3],
132                    &s6[4], &s6[5], &s6[6], &s6[7],
133                    &s6[8], &s6[9], &s6[10], &s6[11],
134                    &s6[12], &s6[13], &s6[14], &s6[15],
135                    ifname) == 17
136             && !strcmp(name, ifname))
137         {
138             return;
139         }
140     }
141     *in6 = in6addr_any;
142
143     fclose(file);
144 }
145
146 static void
147 do_ethtool(struct netdev *netdev) 
148 {
149     struct ifreq ifr;
150     struct ethtool_cmd ecmd;
151
152     netdev->speed = 0;
153     netdev->features = 0;
154
155     memset(&ifr, 0, sizeof ifr);
156     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
157     ifr.ifr_data = (caddr_t) &ecmd;
158
159     memset(&ecmd, 0, sizeof ecmd);
160     ecmd.cmd = ETHTOOL_GSET;
161     if (ioctl(netdev->fd, SIOCETHTOOL, &ifr) == 0) {
162         if (ecmd.supported & SUPPORTED_10baseT_Half) {
163             netdev->features |= OFPPF_10MB_HD;
164         }
165         if (ecmd.supported & SUPPORTED_10baseT_Full) {
166             netdev->features |= OFPPF_10MB_FD;
167         }
168         if (ecmd.supported & SUPPORTED_100baseT_Half)  {
169             netdev->features |= OFPPF_100MB_HD;
170         }
171         if (ecmd.supported & SUPPORTED_100baseT_Full) {
172             netdev->features |= OFPPF_100MB_FD;
173         }
174         if (ecmd.supported & SUPPORTED_1000baseT_Half) {
175             netdev->features |= OFPPF_1GB_HD;
176         }
177         if (ecmd.supported & SUPPORTED_1000baseT_Full) {
178             netdev->features |= OFPPF_1GB_FD;
179         }
180         /* 10Gbps half-duplex doesn't exist... */
181         if (ecmd.supported & SUPPORTED_10000baseT_Full) {
182             netdev->features |= OFPPF_10GB_FD;
183         }
184
185         switch (ecmd.speed) {
186         case SPEED_10:
187             netdev->speed = 10;
188             break;
189
190         case SPEED_100:
191             netdev->speed = 100;
192             break;
193
194         case SPEED_1000:
195             netdev->speed = 1000;
196             break;
197
198         case SPEED_2500:
199             netdev->speed = 2500;
200             break;
201
202         case SPEED_10000:
203             netdev->speed = 10000;
204             break;
205         }
206     } else {
207         VLOG_DBG("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
208     }
209 }
210
211 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
212  * successful, otherwise a positive errno value.  On success, sets '*netdev'
213  * to the new network device, otherwise to null.
214  *
215  * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
216  * capture frames of that type received on the device.  It may also be one of
217  * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
218  * categories. */
219 int
220 netdev_open(const char *name, int ethertype, struct netdev **netdev_)
221 {
222     int fd;
223     struct sockaddr sa;
224     struct ifreq ifr;
225     unsigned int ifindex;
226     uint8_t etheraddr[ETH_ADDR_LEN];
227     struct in_addr in4;
228     struct in6_addr in6;
229     int mtu;
230     int error;
231     struct netdev *netdev;
232
233     *netdev_ = NULL;
234     init_netdev();
235
236     /* Create raw socket.
237      *
238      * We have to use SOCK_PACKET, despite its deprecation, because only
239      * SOCK_PACKET lets us set the hardware source address of outgoing
240      * packets. */
241     fd = socket(PF_PACKET, SOCK_PACKET,
242                 htons(ethertype == NETDEV_ETH_TYPE_NONE ? 0
243                       : ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
244                       : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
245                       : ethertype));
246     if (fd < 0) {
247         return errno;
248     }
249
250     /* Bind to specific ethernet device. */
251     memset(&sa, 0, sizeof sa);
252     sa.sa_family = AF_UNSPEC;
253     strncpy((char *) sa.sa_data, name, sizeof sa.sa_data);
254     if (bind(fd, &sa, sizeof sa) < 0) {
255         VLOG_ERR("bind to %s failed: %s", name, strerror(errno));
256         goto error;
257     }
258
259     if (ethertype != NETDEV_ETH_TYPE_NONE) {
260         /* Between the socket() and bind() calls above, the socket receives all
261          * packets of the requested type on all system interfaces.  We do not
262          * want to receive that data, but there is no way to avoid it.  So we
263          * must now drain out the receive queue. */
264         error = drain_rcvbuf(fd);
265         if (error) {
266             goto error;
267         }
268     }
269
270     /* Get ethernet device index. */
271     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
272     if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
273         VLOG_ERR("ioctl(SIOCGIFINDEX) on %s device failed: %s",
274                  name, strerror(errno));
275         goto error;
276     }
277     ifindex = ifr.ifr_ifindex;
278
279     /* Get MAC address. */
280     if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
281         VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
282                  name, strerror(errno));
283         goto error;
284     }
285     if (ifr.ifr_hwaddr.sa_family != AF_UNSPEC
286         && ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
287         VLOG_WARN("%s device has unknown hardware address family %d",
288                   name, (int) ifr.ifr_hwaddr.sa_family);
289     }
290     memcpy(etheraddr, ifr.ifr_hwaddr.sa_data, sizeof etheraddr);
291
292     /* Get MTU. */
293     if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
294         VLOG_ERR("ioctl(SIOCGIFMTU) on %s device failed: %s",
295                  name, strerror(errno));
296         goto error;
297     }
298     mtu = ifr.ifr_mtu;
299
300     if (!get_ipv4_address(name, &in4)) {
301         goto error;
302     }
303     get_ipv6_address(name, &in6);
304
305     /* Allocate network device. */
306     netdev = xmalloc(sizeof *netdev);
307     netdev->name = xstrdup(name);
308     netdev->fd = fd;
309     memcpy(netdev->etheraddr, etheraddr, sizeof etheraddr);
310     netdev->mtu = mtu;
311     netdev->in4 = in4;
312     netdev->in6 = in6;
313
314     /* Get speed, features. */
315     do_ethtool(netdev);
316
317     /* Save flags to restore at close or exit. */
318     error = get_flags(netdev, &netdev->save_flags);
319     if (error) {
320         goto preset_error;
321     }
322     fatal_signal_block();
323     list_push_back(&netdev_list, &netdev->node);
324     fatal_signal_unblock();
325
326     /* Success! */
327     *netdev_ = netdev;
328     return 0;
329
330 error:
331     error = errno;
332 preset_error:
333     close(fd);
334     return error;
335 }
336
337 /* Closes and destroys 'netdev'. */
338 void
339 netdev_close(struct netdev *netdev)
340 {
341     if (netdev) {
342         /* Bring down interface and drop promiscuous mode, if we brought up
343          * the interface or enabled promiscuous mode. */
344         int error;
345         fatal_signal_block();
346         error = restore_flags(netdev);
347         list_remove(&netdev->node);
348         fatal_signal_unblock();
349         if (error) {
350             VLOG_WARN("failed to restore network device flags on %s: %s",
351                       netdev->name, strerror(error));
352         }
353
354         /* Free. */
355         free(netdev->name);
356         close(netdev->fd);
357         free(netdev);
358     }
359 }
360
361 /* Pads 'buffer' out with zero-bytes to the minimum valid length of an
362  * Ethernet packet, if necessary.  */
363 static void
364 pad_to_minimum_length(struct buffer *buffer)
365 {
366     if (buffer->size < ETH_TOTAL_MIN) {
367         size_t shortage = ETH_TOTAL_MIN - buffer->size;
368         memset(buffer_put_uninit(buffer, shortage), 0, shortage);
369     }
370 }
371
372 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
373  * must have initialized with sufficient room for the packet.  The space
374  * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
375  * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
376  * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
377  * need not be included.)
378  *
379  * If a packet is successfully retrieved, returns 0.  In this case 'buffer' is
380  * guaranteed to contain at least ETH_TOTAL_MIN bytes.  Otherwise, returns a
381  * positive errno value.  Returns EAGAIN immediately if no packet is ready to
382  * be returned.
383  */
384 int
385 netdev_recv(struct netdev *netdev, struct buffer *buffer)
386 {
387     ssize_t n_bytes;
388
389     assert(buffer->size == 0);
390     assert(buffer_tailroom(buffer) >= ETH_TOTAL_MIN);
391     do {
392         n_bytes = recv(netdev->fd,
393                        buffer_tail(buffer), buffer_tailroom(buffer),
394                        MSG_DONTWAIT);
395     } while (n_bytes < 0 && errno == EINTR);
396     if (n_bytes < 0) {
397         if (errno != EAGAIN) {
398             VLOG_WARN("error receiving Ethernet packet on %s: %s",
399                       strerror(errno), netdev->name);
400         }
401         return errno;
402     } else {
403         buffer->size += n_bytes;
404
405         /* When the kernel internally sends out an Ethernet frame on an
406          * interface, it gives us a copy *before* padding the frame to the
407          * minimum length.  Thus, when it sends out something like an ARP
408          * request, we see a too-short frame.  So pad it out to the minimum
409          * length. */
410         pad_to_minimum_length(buffer);
411         return 0;
412     }
413 }
414
415 /* Registers with the poll loop to wake up from the next call to poll_block()
416  * when a packet is ready to be received with netdev_recv() on 'netdev'. */
417 void
418 netdev_recv_wait(struct netdev *netdev)
419 {
420     poll_fd_wait(netdev->fd, POLLIN);
421 }
422
423 /* Discards all packets waiting to be received from 'netdev'. */
424 void
425 netdev_drain(struct netdev *netdev)
426 {
427     drain_rcvbuf(netdev->fd);
428 }
429
430 /* Sends 'buffer' on 'netdev'.  Returns 0 if successful, otherwise a positive
431  * errno value.  Returns EAGAIN without blocking if the packet cannot be queued
432  * immediately.  Returns EMSGSIZE if a partial packet was transmitted or if
433  * the packet is too big or too small to transmit on the device.
434  *
435  * The caller retains ownership of 'buffer' in all cases.
436  *
437  * The kernel maintains a packet transmission queue, so the caller is not
438  * expected to do additional queuing of packets. */
439 int
440 netdev_send(struct netdev *netdev, const struct buffer *buffer)
441 {
442     ssize_t n_bytes;
443     const struct eth_header *eh;
444     struct sockaddr_pkt spkt;
445
446     /* Pull out the Ethernet header. */
447     if (buffer->size < ETH_HEADER_LEN) {
448         VLOG_WARN("cannot send %zu-byte frame on %s",
449                   buffer->size, netdev->name);
450         return EMSGSIZE;
451     }
452     eh = buffer_at_assert(buffer, 0, sizeof *eh);
453
454     /* Construct packet sockaddr, which SOCK_PACKET requires. */
455     spkt.spkt_family = AF_PACKET;
456     strncpy((char *) spkt.spkt_device, netdev->name, sizeof spkt.spkt_device);
457     spkt.spkt_protocol = eh->eth_type;
458
459     do {
460         n_bytes = sendto(netdev->fd, buffer->data, buffer->size, 0,
461                          (const struct sockaddr *) &spkt, sizeof spkt);
462     } while (n_bytes < 0 && errno == EINTR);
463
464     if (n_bytes < 0) {
465         /* The Linux AF_PACKET implementation never blocks waiting for room
466          * for packets, instead returning ENOBUFS.  Translate this into EAGAIN
467          * for the caller. */
468         if (errno == ENOBUFS) {
469             return EAGAIN;
470         } else if (errno != EAGAIN) {
471             VLOG_WARN("error sending Ethernet packet on %s: %s",
472                       netdev->name, strerror(errno));
473         }
474         return errno;
475     } else if (n_bytes != buffer->size) {
476         VLOG_WARN("send partial Ethernet packet (%d bytes of %zu) on %s",
477                   (int) n_bytes, buffer->size, netdev->name);
478         return EMSGSIZE;
479     } else {
480         return 0;
481     }
482 }
483
484 /* Registers with the poll loop to wake up from the next call to poll_block()
485  * when the packet transmission queue has sufficient room to transmit a packet
486  * with netdev_send().
487  *
488  * The kernel maintains a packet transmission queue, so the client is not
489  * expected to do additional queuing of packets.  Thus, this function is
490  * unlikely to ever be used.  It is included for completeness. */
491 void
492 netdev_send_wait(struct netdev *netdev)
493 {
494     poll_fd_wait(netdev->fd, POLLOUT);
495 }
496
497 /* Returns a pointer to 'netdev''s MAC address.  The caller must not modify or
498  * free the returned buffer. */
499 const uint8_t *
500 netdev_get_etheraddr(const struct netdev *netdev)
501 {
502     return netdev->etheraddr;
503 }
504
505 /* Returns the name of the network device that 'netdev' represents,
506  * e.g. "eth0".  The caller must not modify or free the returned string. */
507 const char *
508 netdev_get_name(const struct netdev *netdev)
509 {
510     return netdev->name;
511 }
512
513 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
514  * in bytes, not including the hardware header; thus, this is typically 1500
515  * bytes for Ethernet devices. */
516 int
517 netdev_get_mtu(const struct netdev *netdev) 
518 {
519     return netdev->mtu;
520 }
521
522 /* Returns the current speed of the network device that 'netdev' represents, in
523  * megabits per second, or 0 if the speed is unknown. */
524 int
525 netdev_get_speed(const struct netdev *netdev) 
526 {
527     return netdev->speed;
528 }
529
530 /* Returns the features supported by 'netdev', as a bitmap of bits from enum
531  * ofp_phy_port, in host byte order. */
532 uint32_t
533 netdev_get_features(const struct netdev *netdev) 
534 {
535     return netdev->features;
536 }
537
538 /* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
539  * returns true.  Otherwise, returns false. */
540 bool
541 netdev_get_in4(const struct netdev *netdev, struct in_addr *in4)
542 {
543     *in4 = netdev->in4;
544     return in4->s_addr != INADDR_ANY;
545 }
546
547 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
548  * returns true.  Otherwise, returns false. */
549 bool
550 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
551 {
552     *in6 = netdev->in6;
553     return memcmp(in6, &in6addr_any, sizeof *in6) != 0;
554 }
555
556 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
557  * Returns 0 if successful, otherwise a positive errno value. */
558 int
559 netdev_get_flags(const struct netdev *netdev, enum netdev_flags *flagsp)
560 {
561     int error, flags;
562
563     error = get_flags(netdev, &flags);
564     if (error) {
565         return error;
566     }
567
568     *flagsp = 0;
569     if (flags & IFF_UP) {
570         *flagsp |= NETDEV_UP;
571     }
572     if (flags & IFF_PROMISC) {
573         *flagsp |= NETDEV_PROMISC;
574     }
575     return 0;
576 }
577
578 /* Sets the flags for 'netdev' to 'nd_flags'.
579  * Returns 0 if successful, otherwise a positive errno value. */
580 int
581 netdev_set_flags(struct netdev *netdev, enum netdev_flags nd_flags)
582 {
583     int old_flags, new_flags;
584     int error;
585
586     error = get_flags(netdev, &old_flags);
587     if (error) {
588         return error;
589     }
590
591     new_flags = old_flags & ~(IFF_UP | IFF_PROMISC);
592     if (nd_flags & NETDEV_UP) {
593         new_flags |= IFF_UP;
594     }
595     if (nd_flags & NETDEV_PROMISC) {
596         new_flags |= IFF_PROMISC;
597     }
598     if (new_flags != old_flags) {
599         error = set_flags(netdev, new_flags);
600     }
601     return error;
602 }
603
604 /* Looks up the ARP table entry for 'ip' on 'netdev'.  If one exists and can be
605  * successfully retrieved, it stores the corresponding MAC address in 'mac' and
606  * returns 0.  Otherwise, it returns a positive errno value; in particular,
607  * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
608 int
609 netdev_arp_lookup(const struct netdev *netdev,
610                   uint32_t ip, uint8_t mac[ETH_ADDR_LEN]) 
611 {
612     struct arpreq r;
613     struct sockaddr_in *pa;
614     int retval;
615
616     memset(&r, 0, sizeof r);
617     pa = (struct sockaddr_in *) &r.arp_pa;
618     pa->sin_family = AF_INET;
619     pa->sin_addr.s_addr = ip;
620     pa->sin_port = 0;
621     r.arp_ha.sa_family = ARPHRD_ETHER;
622     r.arp_flags = 0;
623     strncpy(r.arp_dev, netdev->name, sizeof r.arp_dev);
624     retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
625     if (!retval) {
626         memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
627     } else if (retval != ENXIO) {
628         VLOG_WARN("%s: could not look up ARP entry for "IP_FMT": %s",
629                   netdev->name, IP_ARGS(&ip), strerror(retval));
630     }
631     return retval;
632 }
633 \f
634 static void restore_all_flags(void *aux);
635
636 /* Set up a signal hook to restore network device flags on program
637  * termination.  */
638 static void
639 init_netdev(void)
640 {
641     static bool inited;
642     if (!inited) {
643         inited = true;
644         fatal_signal_add_hook(restore_all_flags, NULL);
645         af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
646         if (af_inet_sock < 0) {
647             fatal(errno, "socket(AF_INET)");
648         }
649     }
650 }
651
652 /* Restore the network device flags on 'netdev' to those that were active
653  * before we changed them.  Returns 0 if successful, otherwise a positive
654  * errno value.
655  *
656  * To avoid reentry, the caller must ensure that fatal signals are blocked. */
657 static int
658 restore_flags(struct netdev *netdev)
659 {
660     struct ifreq ifr;
661
662     /* Get current flags. */
663     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
664     if (ioctl(netdev->fd, SIOCGIFFLAGS, &ifr) < 0) {
665         return errno;
666     }
667
668     /* Restore flags that we might have changed, if necessary. */
669     if ((ifr.ifr_flags ^ netdev->save_flags) & (IFF_PROMISC | IFF_UP)) {
670         ifr.ifr_flags &= ~(IFF_PROMISC | IFF_UP);
671         ifr.ifr_flags |= netdev->save_flags & (IFF_PROMISC | IFF_UP);
672         if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) {
673             return errno;
674         }
675     }
676
677     return 0;
678 }
679
680 /* Retores all the flags on all network devices that we modified.  Called from
681  * a signal handler, so it does not attempt to report error conditions. */
682 static void
683 restore_all_flags(void *aux UNUSED)
684 {
685     struct netdev *netdev;
686     LIST_FOR_EACH (netdev, struct netdev, node, &netdev_list) {
687         restore_flags(netdev);
688     }
689 }
690
691 static int
692 get_flags(const struct netdev *netdev, int *flags)
693 {
694     struct ifreq ifr;
695     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
696     if (ioctl(netdev->fd, SIOCGIFFLAGS, &ifr) < 0) {
697         VLOG_ERR("ioctl(SIOCGIFFLAGS) on %s device failed: %s",
698                  netdev->name, strerror(errno));
699         return errno;
700     }
701     *flags = ifr.ifr_flags;
702     return 0;
703 }
704
705 static int
706 set_flags(struct netdev *netdev, int flags)
707 {
708     struct ifreq ifr;
709     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
710     ifr.ifr_flags = flags;
711     if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) {
712         VLOG_ERR("ioctl(SIOCSIFFLAGS) on %s device failed: %s",
713                  netdev->name, strerror(errno));
714         return errno;
715     }
716     return 0;
717 }