c98d0698501562e3cb612e0786f7556f0b003632
[sliver-openvswitch.git] / lib / netdev-pltap.c
1 /*
2  * Copyright (c) 2012 Giuseppe Lettieri
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 <sys/types.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <arpa/inet.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <net/if.h>
26 #include <net/if_arp.h>
27 #include <linux/if_tun.h>
28 #include <netinet/in.h>
29 #include <errno.h>
30
31 #include "flow.h"
32 #include "list.h"
33 #include "netdev-provider.h"
34 #include "odp-util.h"
35 #include "ofp-print.h"
36 #include "ofpbuf.h"
37 #include "packets.h"
38 #include "poll-loop.h"
39 #include "shash.h"
40 #include "sset.h"
41 #include "unixctl.h"
42 #include "socket-util.h"
43 #include "vlog.h"
44 #include "tunalloc.h"
45
46 VLOG_DEFINE_THIS_MODULE(netdev_pltap);
47
48 /* Protects 'sync_list'. */
49 static struct ovs_mutex sync_list_mutex = OVS_MUTEX_INITIALIZER;
50
51 static struct list sync_list OVS_GUARDED_BY(sync_list_mutex)
52     = LIST_INITIALIZER(&sync_list);
53
54 struct netdev_pltap {
55     struct netdev up;
56
57     /* In sync_list. */
58     struct list sync_list OVS_GUARDED_BY(sync_list_mutex);
59
60     /* Protects all members below. */
61     struct ovs_mutex mutex OVS_ACQ_AFTER(sync_list_mutex);
62
63     char *real_name;
64     struct netdev_stats stats;
65     enum netdev_flags new_flags;
66     enum netdev_flags flags;
67     int fd;
68     struct sockaddr_in local_addr;
69     int local_netmask;
70     bool valid_local_ip;
71     bool valid_local_netmask;
72     bool sync_flags_needed;
73     unsigned int change_seq;
74 };
75
76
77 struct netdev_rx_pltap {
78     struct netdev_rx up;    
79     int fd;
80 };
81
82 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
83
84 /* Protects 'pltap_netdevs' */
85 static struct ovs_mutex pltap_netdevs_mutex = OVS_MUTEX_INITIALIZER;
86 static struct shash pltap_netdevs OVS_GUARDED_BY(pltap_netdevs_mutex)
87     = SHASH_INITIALIZER(&pltap_netdevs);
88
89 static int netdev_pltap_construct(struct netdev *netdev_);
90
91 static void netdev_pltap_update_seq(struct netdev_pltap *) 
92     OVS_REQUIRES(dev->mutex);
93 static int get_flags(struct netdev_pltap *dev, enum netdev_flags *flags)
94     OVS_REQUIRES(dev->mutex);
95
96 static bool
97 netdev_pltap_finalized(struct netdev_pltap *dev)
98     OVS_REQUIRES(dev->mutex)
99 {
100     return dev->valid_local_ip && dev->valid_local_netmask;
101 }
102
103 static bool
104 is_netdev_pltap_class(const struct netdev_class *class)
105 {
106     return class->construct == netdev_pltap_construct;
107 }
108
109 static struct netdev_pltap *
110 netdev_pltap_cast(const struct netdev *netdev)
111 {
112     ovs_assert(is_netdev_pltap_class(netdev_get_class(netdev)));
113     return CONTAINER_OF(netdev, struct netdev_pltap, up);
114 }
115
116 static struct netdev_rx_pltap*
117 netdev_rx_pltap_cast(const struct netdev_rx *rx)
118 {
119     ovs_assert(is_netdev_pltap_class(netdev_get_class(rx->netdev)));
120     return CONTAINER_OF(rx, struct netdev_rx_pltap, up);
121 }
122
123 static void sync_needed(struct netdev_pltap *dev)
124     OVS_REQUIRES(dev->mutex, sync_list_mutex)
125 {
126     if (dev->sync_flags_needed)
127         return;
128
129     dev->sync_flags_needed = true;
130     list_insert(&sync_list, &dev->sync_list);
131 }
132
133 static void sync_done(struct netdev_pltap *dev)
134     OVS_REQUIRES(dev->mutex, sync_list_mutex)
135 {
136     if (!dev->sync_flags_needed)
137         return;
138
139     (void) list_remove(&dev->sync_list);
140     dev->sync_flags_needed = false;
141 }
142
143 static struct netdev *
144 netdev_pltap_alloc(void)
145 {
146     struct netdev_pltap *netdev = xzalloc(sizeof *netdev);
147     return &netdev->up;
148 }
149
150 static int
151 netdev_pltap_construct(struct netdev *netdev_)
152 {
153     struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
154     int error;
155
156     ovs_mutex_init(&netdev->mutex);
157     netdev->real_name = xzalloc(IFNAMSIZ + 1);
158     memset(&netdev->local_addr, 0, sizeof(netdev->local_addr));
159     netdev->valid_local_ip = false;
160     netdev->valid_local_netmask = false;
161     netdev->flags = 0;
162     netdev->sync_flags_needed = false;
163     netdev->change_seq = 1;
164
165
166     /* Open tap device. */
167     netdev->fd = tun_alloc(IFF_TAP, netdev->real_name);
168     if (netdev->fd < 0) {
169         error = errno;
170         VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s",
171             netdev_get_name(netdev_), ovs_strerror(error));
172         return error;
173     }
174     VLOG_DBG("real_name = %s", netdev->real_name);
175
176     /* Make non-blocking. */
177     error = set_nonblocking(netdev->fd);
178     if (error) {
179         return error;
180     }
181
182     ovs_mutex_lock(&pltap_netdevs_mutex);
183     shash_add(&pltap_netdevs, netdev_get_name(netdev_), netdev);
184     ovs_mutex_unlock(&pltap_netdevs_mutex);
185     return 0;
186 }
187
188 static void
189 netdev_pltap_destruct(struct netdev *netdev_)
190 {
191     struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
192
193     ovs_mutex_lock(&pltap_netdevs_mutex);
194     if (netdev->fd != -1)
195         close(netdev->fd);
196
197     if (netdev->sync_flags_needed) {
198         ovs_mutex_lock(&sync_list_mutex);
199         (void) list_remove(&netdev->sync_list);
200         ovs_mutex_unlock(&sync_list_mutex);
201     }
202
203     shash_find_and_delete(&pltap_netdevs,
204                           netdev_get_name(netdev_));
205     ovs_mutex_unlock(&pltap_netdevs_mutex);
206     ovs_mutex_destroy(&netdev->mutex);
207 }
208
209 static void
210 netdev_pltap_dealloc(struct netdev *netdev_)
211 {
212     struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
213     free(netdev);
214 }
215
216 static int netdev_pltap_up(struct netdev_pltap *dev) OVS_REQUIRES(dev->mutex);
217
218 static struct netdev_rx *
219 netdev_pltap_rx_alloc(void)
220 {
221     struct netdev_rx_pltap *rx = xzalloc(sizeof *rx);
222     return &rx->up;
223 }
224
225 static int
226 netdev_pltap_rx_construct(struct netdev_rx *rx_)
227 {
228     struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
229     struct netdev *netdev_ = rx->up.netdev;
230     struct netdev_pltap *netdev =
231         netdev_pltap_cast(netdev_);
232     int error = 0;
233
234     ovs_mutex_lock(&netdev->mutex);
235     rx->fd = netdev->fd;
236     if (!netdev_pltap_finalized(netdev))
237         goto out;
238     error = netdev_pltap_up(netdev);
239     if (error) {
240         goto out;
241     }
242 out:
243     ovs_mutex_unlock(&netdev->mutex);
244     return error;
245 }
246
247 static void
248 netdev_pltap_rx_destruct(struct netdev_rx *rx_ OVS_UNUSED)
249 {
250 }
251
252 static void
253 netdev_pltap_rx_dealloc(struct netdev_rx *rx_)
254 {
255     struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
256
257     free(rx);
258 }
259
260 static int vsys_transaction(const char *script,
261         const char **preply, char *format, ...)
262 {
263     char *msg = NULL, *reply = NULL;
264     const size_t reply_size = 1024;
265     int ifd = -1, ofd = -1, maxfd;
266     size_t bytes_to_write, bytes_to_read,
267            bytes_written = 0, bytes_read = 0;
268     int error = 0;
269     char *ofname = NULL, *ifname = NULL;
270     va_list args;
271
272     va_start(args, format);
273     msg = xvasprintf(format, args);
274     va_end(args);
275     reply = (char*)xmalloc(reply_size);
276     if (!msg || !reply) {
277         VLOG_ERR("Out of memory");
278         error = ENOMEM;
279         goto cleanup;
280     }
281
282     ofname = xasprintf("/vsys/%s.out", script);
283     ifname = xasprintf("/vsys/%s.in", script);
284     if (!ofname || !ifname) {
285         VLOG_ERR("Out of memory");
286         error = ENOMEM;
287         goto cleanup;
288     }
289
290     ofd = open(ofname, O_RDONLY | O_NONBLOCK);
291     if (ofd < 0) {
292         VLOG_ERR("Cannot open %s: %s", ofname, ovs_strerror(errno));
293         error = errno;
294         goto cleanup;
295     }
296     ifd = open(ifname, O_WRONLY | O_NONBLOCK);
297     if (ifd < 0) {
298         VLOG_ERR("Cannot open %s: %s", ifname, ovs_strerror(errno));
299         error = errno;
300         goto cleanup;
301     }
302     maxfd = (ifd < ofd) ? ofd : ifd;
303
304     bytes_to_write = strlen(msg);
305     bytes_to_read = reply_size;
306     while (bytes_to_write || bytes_to_read) {
307         fd_set readset, writeset, errorset;
308
309         FD_ZERO(&readset);
310         FD_ZERO(&writeset);
311         FD_ZERO(&errorset);
312         if (bytes_to_write) {
313             FD_SET(ifd, &writeset);
314             FD_SET(ifd, &errorset);
315         }
316         FD_SET(ofd, &readset);
317         FD_SET(ofd, &errorset);
318         if (select(maxfd + 1, &readset, &writeset, &errorset, NULL) < 0) {
319             if (errno == EINTR)
320                 continue;
321             VLOG_ERR("selec error: %s", ovs_strerror(errno));
322             error = errno;
323             goto cleanup;
324         }
325         if (FD_ISSET(ifd, &errorset) || FD_ISSET(ofd, &errorset)) {
326             VLOG_ERR("error condition on ifd or ofd");
327             goto cleanup;
328         }
329         if (FD_ISSET(ifd, &writeset)) {
330             ssize_t n = write(ifd, msg + bytes_written, bytes_to_write);    
331             if (n < 0) {
332                 if (errno != EAGAIN && errno != EINTR) {
333                     VLOG_ERR("write on %s: %s", ifname, ovs_strerror(errno));
334                     error = errno;
335                     goto cleanup;
336                 }
337             } else {
338                 bytes_written += n;
339                 bytes_to_write -= n;
340                 if (bytes_to_write == 0)
341                     close(ifd);
342             }
343         }
344         if (FD_ISSET(ofd, &readset)) {
345             ssize_t n = read(ofd, reply + bytes_read, bytes_to_read);    
346             if (n < 0) {
347                 if (errno != EAGAIN && errno != EINTR) {
348                     VLOG_ERR("read on %s: %s", ofname, ovs_strerror(errno));
349                     error = errno;
350                     goto cleanup;
351                 }
352             } else if (n == 0) {
353                 bytes_to_read = 0;
354             } else {
355                 bytes_read += n;
356                 bytes_to_read -= n;
357             }
358         }
359     }
360     if (bytes_read) {
361         reply[bytes_read] = '\0';
362         if (preply) {
363                 *preply = reply;
364                 reply = NULL; /* prevent freeing the reply msg */
365         } else {
366                 VLOG_ERR("%s returned: %s", script, reply);
367         }
368         error = EAGAIN;
369         goto cleanup;
370     }
371
372 cleanup:
373     free(msg);
374     free(reply);
375     free(ofname);
376     free(ifname);
377     close(ifd);
378     close(ofd);
379     return error;
380 }
381
382 static int
383 netdev_pltap_up(struct netdev_pltap *dev)
384     OVS_REQUIRES(dev->mutex)
385 {
386     if (!netdev_pltap_finalized(dev)) {
387         return 0;
388     }
389     
390     return vsys_transaction("vif_up", NULL, "%s\n"IP_FMT"\n%d\n",
391                dev->real_name,
392                IP_ARGS(dev->local_addr.sin_addr.s_addr),
393                dev->local_netmask);
394 }
395
396 static int
397 netdev_pltap_down(struct netdev_pltap *dev)
398     OVS_REQUIRES(dev->mutex)
399 {
400     if (!netdev_pltap_finalized(dev)) {
401         return 0;
402     }
403     
404     return vsys_transaction("vif_down", NULL, "%s\n", dev->real_name);
405 }
406
407 static int
408 netdev_pltap_promisc(struct netdev_pltap *dev, bool promisc)
409     OVS_REQUIRES(dev-mutex)
410 {
411     if (!netdev_pltap_finalized(dev)) {
412         return 0;
413     }
414
415     return vsys_transaction("promisc", NULL, "%s\n%s",
416                dev->real_name,
417                (promisc ? "" : "-\n"));
418 }
419
420 static void
421 netdev_pltap_sync_flags(struct netdev_pltap *dev)
422     OVS_REQUIRES(sync_list_mutex)
423 {
424
425     ovs_mutex_lock(&dev->mutex);
426
427     if (dev->fd < 0 || !netdev_pltap_finalized(dev)) {
428         goto out;
429     }
430     
431     VLOG_DBG("sync_flags(%s): current: %s %s  target: %s %s",
432         dev->real_name,
433         (dev->flags & NETDEV_UP ? "UP" : "-"),
434         (dev->flags & NETDEV_PROMISC ? "PROMISC" : "-"),
435         (dev->new_flags & NETDEV_UP ? "UP" : "-"),
436         (dev->new_flags & NETDEV_PROMISC ? "PROMISC" : "-"));
437
438     if ((dev->new_flags & NETDEV_UP) && !(dev->flags & NETDEV_UP)) {
439         (void) netdev_pltap_up(dev);
440     } else if (!(dev->new_flags & NETDEV_UP) && (dev->flags & NETDEV_UP)) {
441         (void) netdev_pltap_down(dev);
442     }
443
444     if ((dev->new_flags & NETDEV_PROMISC) ^ (dev->flags & NETDEV_PROMISC)) {
445         (void) netdev_pltap_promisc(dev, dev->new_flags & NETDEV_PROMISC);
446     }
447
448     netdev_pltap_update_seq(dev);
449
450 out:
451     sync_done(dev);
452     ovs_mutex_unlock(&dev->mutex);
453 }
454
455
456 static int
457 netdev_pltap_get_config(const struct netdev *dev_, struct smap *args)
458 {
459     struct netdev_pltap *netdev = netdev_pltap_cast(dev_);
460
461     ovs_mutex_lock(&netdev->mutex);
462     if (netdev->valid_local_ip)
463         smap_add_format(args, "local_ip", IP_FMT,
464             IP_ARGS(netdev->local_addr.sin_addr.s_addr));
465     if (netdev->valid_local_netmask)
466         smap_add_format(args, "local_netmask", "%"PRIu32,
467             ntohs(netdev->local_netmask));
468     ovs_mutex_unlock(&netdev->mutex);
469     return 0;
470 }
471
472 static int
473 netdev_pltap_set_config(struct netdev *dev_, const struct smap *args)
474 {
475     struct netdev_pltap *netdev = netdev_pltap_cast(dev_);
476     struct shash_node *node;
477
478     ovs_mutex_lock(&sync_list_mutex);
479     ovs_mutex_lock(&netdev->mutex);
480     VLOG_DBG("pltap_set_config(%s)", netdev_get_name(dev_));
481     SMAP_FOR_EACH(node, args) {
482         VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
483         if (!strcmp(node->name, "local_ip")) {
484             struct in_addr addr;
485             if (lookup_ip(node->data, &addr)) {
486                 VLOG_WARN("%s: bad 'local_ip'", node->name);
487             } else {
488                 netdev->local_addr.sin_addr = addr;
489                 netdev->valid_local_ip = true;
490             }
491         } else if (!strcmp(node->name, "local_netmask")) {
492             netdev->local_netmask = atoi(node->data);
493             // XXX check valididy
494             netdev->valid_local_netmask = true;
495         } else {
496             VLOG_WARN("%s: unknown argument '%s'", 
497                 netdev_get_name(dev_), node->name);
498         }
499     }
500     if (netdev_pltap_finalized(netdev)) {
501         netdev->new_flags |= NETDEV_UP;
502         sync_needed(netdev);
503     }
504     ovs_mutex_unlock(&netdev->mutex);
505     ovs_mutex_unlock(&sync_list_mutex);
506     return 0;
507 }
508
509 static int
510 netdev_pltap_rx_recv(struct netdev_rx *rx_, void *buffer, size_t size)
511 {
512     struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
513     struct tun_pi pi;
514     struct iovec iov[2] = {
515         { .iov_base = &pi, .iov_len = sizeof(pi) },
516         { .iov_base = buffer, .iov_len = size }
517     };
518     for (;;) {
519         ssize_t retval;
520         retval = readv(rx->fd, iov, 2);
521         if (retval >= 0) {
522             if (retval <= size) {
523                 return retval;
524             } else {
525                 return -EMSGSIZE;
526             }
527         } else if (errno != EINTR) {
528             if (errno != EAGAIN) {
529                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
530                     netdev_rx_get_name(rx_), ovs_strerror(errno));
531             }
532             return -errno;
533         }
534     }
535 }
536
537 static void
538 netdev_pltap_rx_wait(struct netdev_rx *rx_)
539 {
540     struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
541     struct netdev_pltap *netdev =
542         netdev_pltap_cast(rx->up.netdev);
543     if (rx->fd >= 0 && netdev_pltap_finalized(netdev)) {
544         poll_fd_wait(rx->fd, POLLIN);
545     }
546 }
547
548 static int
549 netdev_pltap_send(struct netdev *netdev_, const void *buffer, size_t size)
550 {
551     struct netdev_pltap *dev = 
552         netdev_pltap_cast(netdev_);
553     struct tun_pi pi = { 0, 0x86 };
554     struct iovec iov[2] = {
555         { .iov_base = &pi, .iov_len = sizeof(pi) },
556         { .iov_base = (char*) buffer, .iov_len = size }
557     };
558     if (dev->fd < 0)
559         return EAGAIN;
560     for (;;) {
561         ssize_t retval;
562         retval = writev(dev->fd, iov, 2);
563         if (retval >= 0) {
564             if (retval != size + 4) {
565                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of %zu) on %s",
566                              retval, size + 4, netdev_get_name(netdev_));
567             }
568             return 0;
569         } else if (errno != EINTR) {
570             if (errno != EAGAIN) {
571                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
572                     netdev_get_name(netdev_), ovs_strerror(errno));
573             }
574             return errno;
575         }
576     }
577 }
578
579 static void
580 netdev_pltap_send_wait(struct netdev *netdev_)
581 {
582     struct netdev_pltap *dev = 
583         netdev_pltap_cast(netdev_);
584     if (dev->fd >= 0 && netdev_pltap_finalized(dev)) {
585         poll_fd_wait(dev->fd, POLLOUT);
586     }
587 }
588
589 static int
590 netdev_pltap_rx_drain(struct netdev_rx *rx_)
591 {
592     struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
593     char buffer[128];
594     int error;
595
596     if (rx->fd < 0)
597         return EAGAIN;
598     for (;;) {
599         error = recv(rx->fd, buffer, 128, MSG_TRUNC);
600         if (error) {
601             if (error == -EAGAIN)
602                 break;
603             else if (error != -EMSGSIZE)
604                 return error;
605         }
606     }
607     return 0;
608 }
609
610 static int
611 netdev_pltap_set_etheraddr(struct netdev *netdevi OVS_UNUSED,
612                            const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
613 {
614     return ENOTSUP;
615 }
616
617
618 // XXX from netdev-linux.c
619 static int
620 get_etheraddr(struct netdev_pltap *dev, uint8_t ea[ETH_ADDR_LEN])
621     OVS_REQUIRES(dev->mutex)
622 {
623     struct ifreq ifr;
624     int hwaddr_family;
625     int error;
626
627     memset(&ifr, 0, sizeof ifr);
628     ovs_strzcpy(ifr.ifr_name, dev->real_name, sizeof ifr.ifr_name);
629     error = af_inet_ifreq_ioctl(dev->real_name, &ifr,
630         SIOCGIFHWADDR, "SIOCGIFHWADDR");
631     if (error) {
632         return error;
633     }
634     hwaddr_family = ifr.ifr_hwaddr.sa_family;
635     if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
636         VLOG_WARN("%s device has unknown hardware address family %d",
637                   dev->real_name, hwaddr_family);
638     }
639     memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
640     return 0;
641 }
642
643 static int
644 get_flags(struct netdev_pltap *dev, enum netdev_flags *flags)
645     OVS_REQUIRES(dev->mutex)
646 {
647     struct ifreq ifr;
648     int error;
649
650     error = af_inet_ifreq_ioctl(dev->real_name, &ifr,
651         SIOCGIFFLAGS, "SIOCGIFFLAGS");
652     if (error) {
653         return error;
654     }
655     *flags = 0;
656     if (ifr.ifr_flags & IFF_UP)
657         *flags |= NETDEV_UP;
658     if (ifr.ifr_flags & IFF_PROMISC)
659         *flags |= NETDEV_PROMISC;
660     return 0;
661 }
662
663 static int
664 netdev_pltap_get_etheraddr(const struct netdev *netdev,
665                            uint8_t mac[ETH_ADDR_LEN])
666 {
667     struct netdev_pltap *dev = 
668         netdev_pltap_cast(netdev);
669     int error = 0;
670
671     ovs_mutex_lock(&dev->mutex);
672     if (dev->fd < 0) {
673         error = EAGAIN;
674         goto out;
675     }
676     error = get_etheraddr(dev, mac);
677
678 out:
679     ovs_mutex_unlock(&dev->mutex);
680     return error;
681 }
682
683
684 // XXX can we read stats in planetlab?
685 static int
686 netdev_pltap_get_stats(const struct netdev *netdev OVS_UNUSED, struct netdev_stats *stats OVS_UNUSED)
687 {
688     return ENOTSUP;
689 }
690
691 static int
692 netdev_pltap_set_stats(struct netdev *netdev OVS_UNUSED, const struct netdev_stats *stats OVS_UNUSED)
693 {
694     return ENOTSUP;
695 }
696
697
698 static int
699 netdev_pltap_update_flags(struct netdev *dev_,
700                           enum netdev_flags off, enum netdev_flags on,
701                           enum netdev_flags *old_flagsp)
702 {
703     struct netdev_pltap *netdev =
704         netdev_pltap_cast(dev_);
705     int error = 0;
706
707     ovs_mutex_lock(&sync_list_mutex);
708     ovs_mutex_lock(&netdev->mutex);
709     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
710         error = EINVAL;
711         goto out;
712     }
713
714     if (netdev_pltap_finalized(netdev)) {
715         error = get_flags(netdev, &netdev->flags);
716     }
717     *old_flagsp = netdev->flags;
718     netdev->new_flags |= on;
719     netdev->new_flags &= ~off;
720     if (netdev->flags != netdev->new_flags) {
721         /* we cannot sync here, since we may be in a signal handler */
722         sync_needed(netdev);
723     }
724
725 out:
726     ovs_mutex_unlock(&netdev->mutex);
727     ovs_mutex_unlock(&sync_list_mutex);
728     return error;
729 }
730
731 static unsigned int
732 netdev_pltap_change_seq(const struct netdev *netdev)
733 {
734     struct netdev_pltap *dev =
735         netdev_pltap_cast(netdev);
736     unsigned int change_seq;
737
738     ovs_mutex_lock(&dev->mutex);
739     change_seq = dev->change_seq;
740     ovs_mutex_unlock(&dev->mutex);
741
742     return change_seq;
743 }
744 \f
745 /* Helper functions. */
746
747 static void
748 netdev_pltap_update_seq(struct netdev_pltap *dev)
749     OVS_REQUIRES(dev->mutex)
750 {
751     dev->change_seq++;
752     if (!dev->change_seq) {
753         dev->change_seq++;
754     }
755 }
756
757 static void
758 netdev_pltap_get_real_name(struct unixctl_conn *conn,
759                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
760 {
761     struct netdev_pltap *pltap_dev;
762
763     ovs_mutex_lock(&pltap_netdevs_mutex);
764     pltap_dev = shash_find_data(&pltap_netdevs, argv[1]);
765     if (!pltap_dev) {
766         unixctl_command_reply_error(conn, "no such pltap netdev");
767         goto out;
768     }
769     if (pltap_dev->fd < 0) {
770         unixctl_command_reply_error(conn, "no real device attached");
771         goto out;       
772     }
773
774     unixctl_command_reply(conn, pltap_dev->real_name);
775
776 out:
777     ovs_mutex_unlock(&pltap_netdevs_mutex);
778 }
779
780 static int
781 netdev_pltap_init(void)
782 {
783     unixctl_command_register("netdev-pltap/get-tapname", "port",
784                              1, 1, netdev_pltap_get_real_name, NULL);
785     return 0;
786 }
787
788 static void
789 netdev_pltap_run(void)
790 {
791     struct netdev_pltap *iter, *next;
792     ovs_mutex_lock(&sync_list_mutex);
793     LIST_FOR_EACH_SAFE(iter, next, sync_list, &sync_list) {
794         netdev_pltap_sync_flags(iter);
795     }
796     ovs_mutex_unlock(&sync_list_mutex);
797 }
798
799 static void
800 netdev_pltap_wait(void)
801 {
802     ovs_mutex_lock(&sync_list_mutex);
803     if (!list_is_empty(&sync_list)) {
804         VLOG_DBG("netdev_pltap: scheduling sync");
805         poll_immediate_wake();
806     }
807     ovs_mutex_unlock(&sync_list_mutex);
808 }
809
810 const struct netdev_class netdev_pltap_class = {
811     "pltap",
812     netdev_pltap_init,
813     netdev_pltap_run,  
814     netdev_pltap_wait,            
815
816     netdev_pltap_alloc,
817     netdev_pltap_construct,
818     netdev_pltap_destruct,
819     netdev_pltap_dealloc,
820     netdev_pltap_get_config,
821     netdev_pltap_set_config, 
822     NULL,                                   /* get_tunnel_config */
823
824     netdev_pltap_send, 
825     netdev_pltap_send_wait,  
826
827     netdev_pltap_set_etheraddr,
828     netdev_pltap_get_etheraddr,
829     NULL,                                   /* get_mtu */
830     NULL,                                   /* set_mtu */
831     NULL,                       /* get_ifindex */
832     NULL,                                   /* get_carrier */
833     NULL,                       /* get_carrier_resets */
834     NULL,                       /* get_miimon */
835     netdev_pltap_get_stats,
836     netdev_pltap_set_stats,
837
838     NULL,                       /* get_features */
839     NULL,                       /* set_advertisements */
840
841     NULL,                       /* set_policing */
842     NULL,                       /* get_qos_types */
843     NULL,                       /* get_qos_capabilities */
844     NULL,                       /* get_qos */
845     NULL,                       /* set_qos */
846     NULL,                       /* get_queue */
847     NULL,                       /* set_queue */
848     NULL,                       /* delete_queue */
849     NULL,                       /* get_queue_stats */
850     NULL,                       /* dump_queues */
851     NULL,                       /* dump_queue_stats */
852
853     NULL,                       /* get_in4 */
854     NULL,                       /* set_in4 */
855     NULL,                       /* get_in6 */
856     NULL,                       /* add_router */
857     NULL,                       /* get_next_hop */
858     NULL,                       /* get_drv_info */
859     NULL,                       /* arp_lookup */
860
861     netdev_pltap_update_flags,
862
863     netdev_pltap_change_seq,
864
865     netdev_pltap_rx_alloc,
866     netdev_pltap_rx_construct,
867     netdev_pltap_rx_destruct,
868     netdev_pltap_rx_dealloc,
869     netdev_pltap_rx_recv,
870     netdev_pltap_rx_wait,
871     netdev_pltap_rx_drain,
872 };