New functions for getting and setting network device flags.
[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
63 #define THIS_MODULE VLM_netdev
64 #include "vlog.h"
65
66 struct netdev {
67     struct list node;
68     char *name;
69     int fd;
70     uint8_t etheraddr[ETH_ADDR_LEN];
71     int speed;
72     int mtu;
73     uint32_t features;
74     struct in_addr in4;
75     struct in6_addr in6;
76     int save_flags;
77 };
78
79 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
80
81 static void init_netdev(void);
82 static int restore_flags(struct netdev *netdev);
83 static int get_flags(const struct netdev *, int *flagsp);
84 static int set_flags(struct netdev *, int flags);
85
86 /* Obtains the IPv4 address for 'name' into 'in4'.  Returns true if
87  * successful. */
88 static bool
89 get_ipv4_address(const char *name, struct in_addr *in4)
90 {
91     int sock;
92     struct ifreq ifr;
93
94     sock = socket(AF_INET, SOCK_DGRAM, 0);
95     if (sock < 0) {
96         VLOG_WARN("socket(AF_INET): %s", strerror(errno));
97         return false;
98     }
99
100     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
101     ifr.ifr_addr.sa_family = AF_INET;
102     if (ioctl(sock, SIOCGIFADDR, &ifr) == 0) {
103         struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
104         *in4 = sin->sin_addr;
105     } else {
106         in4->s_addr = INADDR_ANY;
107     }
108
109     close(sock);
110     return true;
111 }
112
113 /* Obtains the IPv6 address for 'name' into 'in6'. */
114 static void
115 get_ipv6_address(const char *name, struct in6_addr *in6)
116 {
117     FILE *file;
118     char line[128];
119
120     file = fopen("/proc/net/if_inet6", "r");
121     if (file == NULL) {
122         /* This most likely indicates that the host doesn't have IPv6 support,
123          * so it's not really a failure condition.*/
124         *in6 = in6addr_any;
125         return;
126     }
127
128     while (fgets(line, sizeof line, file)) {
129         uint8_t *s6 = in6->s6_addr;
130         char ifname[16 + 1];
131
132 #define X8 "%2"SCNx8
133         if (sscanf(line, " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
134                    "%*x %*x %*x %*x %16s\n",
135                    &s6[0], &s6[1], &s6[2], &s6[3],
136                    &s6[4], &s6[5], &s6[6], &s6[7],
137                    &s6[8], &s6[9], &s6[10], &s6[11],
138                    &s6[12], &s6[13], &s6[14], &s6[15],
139                    ifname) == 17
140             && !strcmp(name, ifname))
141         {
142             return;
143         }
144     }
145     *in6 = in6addr_any;
146
147     fclose(file);
148 }
149
150 static void
151 do_ethtool(struct netdev *netdev) 
152 {
153     struct ifreq ifr;
154     struct ethtool_cmd ecmd;
155
156     netdev->speed = 0;
157     netdev->features = 0;
158
159     memset(&ifr, 0, sizeof ifr);
160     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
161     ifr.ifr_data = (caddr_t) &ecmd;
162
163     memset(&ecmd, 0, sizeof ecmd);
164     ecmd.cmd = ETHTOOL_GSET;
165     if (ioctl(netdev->fd, SIOCETHTOOL, &ifr) == 0) {
166         if (ecmd.supported & SUPPORTED_10baseT_Half) {
167             netdev->features |= OFPPF_10MB_HD;
168         }
169         if (ecmd.supported & SUPPORTED_10baseT_Full) {
170             netdev->features |= OFPPF_10MB_FD;
171         }
172         if (ecmd.supported & SUPPORTED_100baseT_Half)  {
173             netdev->features |= OFPPF_100MB_HD;
174         }
175         if (ecmd.supported & SUPPORTED_100baseT_Full) {
176             netdev->features |= OFPPF_100MB_FD;
177         }
178         if (ecmd.supported & SUPPORTED_1000baseT_Half) {
179             netdev->features |= OFPPF_1GB_HD;
180         }
181         if (ecmd.supported & SUPPORTED_1000baseT_Full) {
182             netdev->features |= OFPPF_1GB_FD;
183         }
184         /* 10Gbps half-duplex doesn't exist... */
185         if (ecmd.supported & SUPPORTED_10000baseT_Full) {
186             netdev->features |= OFPPF_10GB_FD;
187         }
188
189         switch (ecmd.speed) {
190         case SPEED_10:
191             netdev->speed = 10;
192             break;
193
194         case SPEED_100:
195             netdev->speed = 100;
196             break;
197
198         case SPEED_1000:
199             netdev->speed = 1000;
200             break;
201
202         case SPEED_2500:
203             netdev->speed = 2500;
204             break;
205
206         case SPEED_10000:
207             netdev->speed = 10000;
208             break;
209         }
210     } else {
211         VLOG_DBG("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
212     }
213 }
214
215 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
216  * successful, otherwise a positive errno value.  On success, sets '*netdev'
217  * to the new network device, otherwise to null. */
218 int
219 netdev_open(const char *name, struct netdev **netdev_)
220 {
221     int fd;
222     struct sockaddr sa;
223     struct ifreq ifr;
224     unsigned int ifindex;
225     socklen_t rcvbuf_len;
226     size_t rcvbuf;
227     uint8_t etheraddr[ETH_ADDR_LEN];
228     struct in_addr in4;
229     struct in6_addr in6;
230     int mtu;
231     int error;
232     struct netdev *netdev;
233
234     *netdev_ = NULL;
235     init_netdev();
236
237     /* Create raw socket.
238      *
239      * We have to use SOCK_PACKET, despite its deprecation, because only
240      * SOCK_PACKET lets us set the hardware source address of outgoing
241      * packets. */
242     fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL));
243     if (fd < 0) {
244         return errno;
245     }
246
247     /* Bind to specific ethernet device. */
248     memset(&sa, 0, sizeof sa);
249     sa.sa_family = AF_UNSPEC;
250     strncpy((char *) sa.sa_data, name, sizeof sa.sa_data);
251     if (bind(fd, &sa, sizeof sa) < 0) {
252         VLOG_ERR("bind to %s failed: %s", name, strerror(errno));
253         goto error;
254     }
255
256     /* Between the socket() and bind() calls above, the socket receives all
257      * packets on all system interfaces.  We do not want to receive that
258      * data, but there is no way to avoid it.  So we must now drain out the
259      * receive queue.  There is no way to know how long the receive queue is,
260      * but we know that the total number of bytes queued does not exceed the
261      * receive buffer size, so we pull packets until none are left or we've
262      * read that many bytes. */
263     rcvbuf_len = sizeof rcvbuf;
264     if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &rcvbuf_len) < 0) {
265         VLOG_ERR("getsockopt(SO_RCVBUF) on %s device failed: %s",
266                  name, strerror(errno));
267         goto error;
268     }
269     while (rcvbuf > 0) {
270         char buffer;
271         ssize_t n_bytes = recv(fd, &buffer, 1, MSG_TRUNC | MSG_DONTWAIT);
272         if (n_bytes <= 0) {
273             break;
274         }
275         rcvbuf -= n_bytes;
276     }
277
278     /* Get ethernet device index. */
279     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
280     if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
281         VLOG_ERR("ioctl(SIOCGIFINDEX) on %s device failed: %s",
282                  name, strerror(errno));
283         goto error;
284     }
285     ifindex = ifr.ifr_ifindex;
286
287     /* Get MAC address. */
288     if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
289         VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
290                  name, strerror(errno));
291         goto error;
292     }
293     if (ifr.ifr_hwaddr.sa_family != AF_UNSPEC
294         && ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
295         VLOG_WARN("%s device has unknown hardware address family %d",
296                   name, (int) ifr.ifr_hwaddr.sa_family);
297     }
298     memcpy(etheraddr, ifr.ifr_hwaddr.sa_data, sizeof etheraddr);
299
300     /* Get MTU. */
301     if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
302         VLOG_ERR("ioctl(SIOCGIFMTU) on %s device failed: %s",
303                  name, strerror(errno));
304         goto error;
305     }
306     mtu = ifr.ifr_mtu;
307
308     if (!get_ipv4_address(name, &in4)) {
309         goto error;
310     }
311     get_ipv6_address(name, &in6);
312
313     /* Allocate network device. */
314     netdev = xmalloc(sizeof *netdev);
315     netdev->name = xstrdup(name);
316     netdev->fd = fd;
317     memcpy(netdev->etheraddr, etheraddr, sizeof etheraddr);
318     netdev->mtu = mtu;
319     netdev->in4 = in4;
320     netdev->in6 = in6;
321
322     /* Get speed, features. */
323     do_ethtool(netdev);
324
325     /* Save flags to restore at close or exit. */
326     error = get_flags(netdev, &netdev->save_flags);
327     if (error) {
328         goto preset_error;
329     }
330     fatal_signal_block();
331     list_push_back(&netdev_list, &netdev->node);
332     fatal_signal_unblock();
333
334     /* Success! */
335     *netdev_ = netdev;
336     return 0;
337
338 error:
339     error = errno;
340 preset_error:
341     close(fd);
342     return error;
343 }
344
345 /* Closes and destroys 'netdev'. */
346 void
347 netdev_close(struct netdev *netdev)
348 {
349     if (netdev) {
350         /* Bring down interface and drop promiscuous mode, if we brought up
351          * the interface or enabled promiscuous mode. */
352         int error;
353         fatal_signal_block();
354         error = restore_flags(netdev);
355         list_remove(&netdev->node);
356         fatal_signal_unblock();
357         if (error) {
358             VLOG_WARN("failed to restore network device flags on %s: %s",
359                       netdev->name, strerror(error));
360         }
361
362         /* Free. */
363         free(netdev->name);
364         close(netdev->fd);
365         free(netdev);
366     }
367 }
368
369 /* Pads 'buffer' out with zero-bytes to the minimum valid length of an
370  * Ethernet packet, if necessary.  */
371 static void
372 pad_to_minimum_length(struct buffer *buffer)
373 {
374     if (buffer->size < ETH_TOTAL_MIN) {
375         size_t shortage = ETH_TOTAL_MIN - buffer->size;
376         memset(buffer_put_uninit(buffer, shortage), 0, shortage);
377     }
378 }
379
380 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
381  * must have initialized with sufficient room for the packet.  The space
382  * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
383  * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
384  * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
385  * need not be included.)
386  *
387  * If a packet is successfully retrieved, returns 0.  In this case 'buffer' is
388  * guaranteed to contain at least ETH_TOTAL_MIN bytes.  Otherwise, returns a
389  * positive errno value.  Returns EAGAIN immediately if no packet is ready to
390  * be returned.
391  */
392 int
393 netdev_recv(struct netdev *netdev, struct buffer *buffer)
394 {
395     ssize_t n_bytes;
396
397     assert(buffer->size == 0);
398     assert(buffer_tailroom(buffer) >= ETH_TOTAL_MIN);
399     do {
400         n_bytes = recv(netdev->fd,
401                        buffer_tail(buffer), buffer_tailroom(buffer),
402                        MSG_DONTWAIT);
403     } while (n_bytes < 0 && errno == EINTR);
404     if (n_bytes < 0) {
405         if (errno != EAGAIN) {
406             VLOG_WARN("error receiving Ethernet packet on %s: %s",
407                       strerror(errno), netdev->name);
408         }
409         return errno;
410     } else {
411         buffer->size += n_bytes;
412
413         /* When the kernel internally sends out an Ethernet frame on an
414          * interface, it gives us a copy *before* padding the frame to the
415          * minimum length.  Thus, when it sends out something like an ARP
416          * request, we see a too-short frame.  So pad it out to the minimum
417          * length. */
418         pad_to_minimum_length(buffer);
419         return 0;
420     }
421 }
422
423 /* Registers with the poll loop to wake up from the next call to poll_block()
424  * when a packet is ready to be received with netdev_recv() on 'netdev'. */
425 void
426 netdev_recv_wait(struct netdev *netdev)
427 {
428     poll_fd_wait(netdev->fd, POLLIN);
429 }
430
431 /* Sends 'buffer' on 'netdev'.  Returns 0 if successful, otherwise a positive
432  * errno value.  Returns EAGAIN without blocking if the packet cannot be queued
433  * immediately.  Returns EMSGSIZE if a partial packet was transmitted or if
434  * the packet is too big to transmit on the device.
435  *
436  * The kernel maintains a packet transmission queue, so the caller is not
437  * expected to do additional queuing of packets. */
438 int
439 netdev_send(struct netdev *netdev, struct buffer *buffer)
440 {
441     ssize_t n_bytes;
442     const struct eth_header *eh;
443     struct sockaddr_pkt spkt;
444
445     /* Ensure packet is long enough.  (Although all incoming packets are at
446      * least ETH_TOTAL_MIN bytes long, we could have trimmed some data off a
447      * minimum-size packet, e.g. by dropping a vlan header.)
448      *
449      * The kernel does not require this, but it ensures that we always access
450      * valid memory in grabbing the sockaddr below. */
451     pad_to_minimum_length(buffer);
452
453     /* Construct packet sockaddr, which SOCK_PACKET requires. */
454     spkt.spkt_family = AF_PACKET;
455     strncpy((char *) spkt.spkt_device, netdev->name, sizeof spkt.spkt_device);
456     eh = buffer_at_assert(buffer, 0, sizeof *eh);
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 \f
604 static void restore_all_flags(void *aux);
605
606 /* Set up a signal hook to restore network device flags on program
607  * termination.  */
608 static void
609 init_netdev(void)
610 {
611     static bool inited;
612     if (!inited) {
613         inited = true;
614         fatal_signal_add_hook(restore_all_flags, NULL);
615     }
616 }
617
618 /* Restore the network device flags on 'netdev' to those that were active
619  * before we changed them.  Returns 0 if successful, otherwise a positive
620  * errno value.
621  *
622  * To avoid reentry, the caller must ensure that fatal signals are blocked. */
623 static int
624 restore_flags(struct netdev *netdev)
625 {
626     struct ifreq ifr;
627
628     /* Get current flags. */
629     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
630     if (ioctl(netdev->fd, SIOCGIFFLAGS, &ifr) < 0) {
631         return errno;
632     }
633
634     /* Restore flags that we might have changed, if necessary. */
635     if ((ifr.ifr_flags ^ netdev->save_flags) & (IFF_PROMISC | IFF_UP)) {
636         ifr.ifr_flags &= ~(IFF_PROMISC | IFF_UP);
637         ifr.ifr_flags |= netdev->save_flags & (IFF_PROMISC | IFF_UP);
638         if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) {
639             return errno;
640         }
641     }
642
643     return 0;
644 }
645
646 /* Retores all the flags on all network devices that we modified.  Called from
647  * a signal handler, so it does not attempt to report error conditions. */
648 static void
649 restore_all_flags(void *aux UNUSED)
650 {
651     struct netdev *netdev;
652     LIST_FOR_EACH (netdev, struct netdev, node, &netdev_list) {
653         restore_flags(netdev);
654     }
655 }
656
657 static int
658 get_flags(const struct netdev *netdev, int *flags)
659 {
660     struct ifreq ifr;
661     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
662     if (ioctl(netdev->fd, SIOCGIFFLAGS, &ifr) < 0) {
663         VLOG_ERR("ioctl(SIOCGIFFLAGS) on %s device failed: %s",
664                  netdev->name, strerror(errno));
665         return errno;
666     }
667     *flags = ifr.ifr_flags;
668     return 0;
669 }
670
671 static int
672 set_flags(struct netdev *netdev, int flags)
673 {
674     struct ifreq ifr;
675     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
676     ifr.ifr_flags = flags;
677     if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) {
678         VLOG_ERR("ioctl(SIOCSIFFLAGS) on %s device failed: %s",
679                  netdev->name, strerror(errno));
680         return errno;
681     }
682     return 0;
683 }