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