add/skip packet information on tap send/recv
[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 netdev_dev;
50     char *real_name;
51     char *error;
52     struct netdev_stats stats;
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 finalized;
60     unsigned int change_seq;
61 };
62
63 struct netdev_pltap {
64     struct netdev netdev;
65 } ;
66
67 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
68
69 static struct shash pltap_netdev_devs = SHASH_INITIALIZER(&pltap_netdev_devs);
70
71 static int netdev_pltap_create(const struct netdev_class *, const char *,
72                                struct netdev_dev **);
73
74 static struct shash pltap_creating = SHASH_INITIALIZER(&pltap_creating);
75
76 static void netdev_pltap_update_seq(struct netdev_dev_pltap *);
77
78 static bool
79 is_pltap_class(const struct netdev_class *class)
80 {
81     return class->create == netdev_pltap_create;
82 }
83
84 static struct netdev_dev_pltap *
85 netdev_dev_pltap_cast(const struct netdev_dev *netdev_dev)
86 {
87     assert(is_pltap_class(netdev_dev_get_class(netdev_dev)));
88     return CONTAINER_OF(netdev_dev, struct netdev_dev_pltap, netdev_dev);
89 }
90
91 static struct netdev_pltap *
92 netdev_pltap_cast(const struct netdev *netdev)
93 {
94     struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
95     assert(is_pltap_class(netdev_dev_get_class(netdev_dev)));
96     return CONTAINER_OF(netdev, struct netdev_pltap, netdev);
97 }
98
99 static int
100 netdev_pltap_create(const struct netdev_class *class OVS_UNUSED, const char *name,
101                     struct netdev_dev **netdev_devp)
102 {
103     struct netdev_dev_pltap *netdev_dev;
104     int error;
105
106     netdev_dev = xzalloc(sizeof *netdev_dev);
107
108     netdev_dev->real_name = xzalloc(IFNAMSIZ + 1);
109     netdev_dev->error = NULL;
110     memset(&netdev_dev->local_addr, 0, sizeof(netdev_dev->local_addr));
111     netdev_dev->valid_local_ip = false;
112     netdev_dev->valid_local_netmask = false;
113     netdev_dev->finalized = false;
114
115
116     /* Open tap device. */
117     netdev_dev->fd = tun_alloc(IFF_TAP, netdev_dev->real_name);
118     if (netdev_dev->fd < 0) {
119         error = errno;
120         VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s", name, strerror(error));
121         goto cleanup;
122     }
123     VLOG_DBG("real_name = %s", netdev_dev->real_name);
124
125     /* Make non-blocking. */
126     error = set_nonblocking(netdev_dev->fd);
127     if (error) {
128         goto cleanup;
129     }
130
131     netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_pltap_class);
132     shash_add(&pltap_netdev_devs, name, netdev_dev);
133     *netdev_devp = &netdev_dev->netdev_dev;
134     return 0;
135
136 cleanup:
137     free(netdev_dev);
138     return error;
139 }
140
141 static void
142 netdev_pltap_destroy(struct netdev_dev *netdev_dev_)
143 {
144     struct netdev_dev_pltap *netdev_dev = netdev_dev_pltap_cast(netdev_dev_);
145
146     if (netdev_dev->fd != -1)
147         close(netdev_dev->fd);
148
149     shash_find_and_delete(&pltap_netdev_devs,
150                           netdev_dev_get_name(netdev_dev_));
151     free(netdev_dev);
152 }
153
154 static int
155 netdev_pltap_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
156 {
157     struct netdev_pltap *netdev;
158
159     netdev = xmalloc(sizeof *netdev);
160     netdev_init(&netdev->netdev, netdev_dev_);
161
162     *netdevp = &netdev->netdev;
163     return 0;
164 }
165
166 static void
167 netdev_pltap_close(struct netdev *netdev_)
168 {
169     struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
170     free(netdev);
171 }
172
173 static int
174 netdev_pltap_create_finalize(struct netdev_dev_pltap *dev)
175 {
176     int ifd = -1, ofd = -1, maxfd;
177     size_t bytes_to_write, bytes_to_read = 1024,
178            bytes_written = 0, bytes_read = 0;
179     int error = 0;
180     char *msg = NULL, *reply = NULL;
181
182     if (dev->finalized)
183         return 0;
184     if (!dev->valid_local_ip || !dev->valid_local_netmask)
185         return 0;
186     
187     ofd = open("/vsys/vif_up.out", O_RDONLY | O_NONBLOCK);
188     if (ofd < 0) {
189         VLOG_ERR("Cannot open vif_up.out: %s", strerror(errno));
190         error = errno;
191         goto cleanup;
192     }
193     ifd = open("/vsys/vif_up.in", O_WRONLY | O_NONBLOCK);
194     if (ifd < 0) {
195         VLOG_ERR("Cannot open vif_up.in: %s", strerror(errno));
196         error = errno;
197         goto cleanup;
198     }
199     maxfd = (ifd < ofd) ? ofd : ifd;
200
201     msg = xasprintf("%s\n"IP_FMT"\n%d\n",
202        dev->real_name,
203        IP_ARGS(&dev->local_addr.sin_addr),
204        dev->local_netmask);
205     reply = (char*)xmalloc(bytes_to_read);
206     if (!msg || !reply) {
207         VLOG_ERR("Out of memory");
208         error = ENOMEM;
209         goto cleanup;
210     }
211     bytes_to_write = strlen(msg);
212     while (bytes_to_write || bytes_to_read) {
213         fd_set readset, writeset, errorset;
214
215         FD_ZERO(&readset);
216         FD_ZERO(&writeset);
217         FD_ZERO(&errorset);
218         if (bytes_to_write) {
219             FD_SET(ifd, &writeset);
220             FD_SET(ifd, &errorset);
221         }
222         FD_SET(ofd, &readset);
223         FD_SET(ofd, &errorset);
224         if (select(maxfd + 1, &readset, &writeset, &errorset, NULL) < 0) {
225             if (errno == EINTR)
226                 continue;
227             VLOG_ERR("selec error: %s", strerror(errno));
228             error = errno;
229             goto cleanup;
230         }
231         if (FD_ISSET(ifd, &errorset) || FD_ISSET(ofd, &errorset)) {
232             VLOG_ERR("error condition on ifd or ofd");
233             goto cleanup;
234         }
235         if (FD_ISSET(ifd, &writeset)) {
236             ssize_t n = write(ifd, msg + bytes_written, bytes_to_write);    
237             if (n < 0) {
238                 if (errno != EAGAIN && errno != EINTR) {
239                     VLOG_ERR("write on vif_up.in: %s", strerror(errno));
240                     error = errno;
241                     goto cleanup;
242                 }
243             } else {
244                 bytes_written += n;
245                 bytes_to_write -= n;
246                 if (bytes_to_write == 0)
247                     close(ifd);
248             }
249         }
250         if (FD_ISSET(ofd, &readset)) {
251             ssize_t n = read(ofd, reply + bytes_read, bytes_to_read);    
252             if (n < 0) {
253                 if (errno != EAGAIN && errno != EINTR) {
254                     VLOG_ERR("read on vif_up.out: %s", strerror(errno));
255                     error = errno;
256                     goto cleanup;
257                 }
258             } else if (n == 0) {
259                 bytes_to_read = 0;
260             } else {
261                 bytes_read += n;
262                 bytes_to_read -= n;
263             }
264         }
265     }
266     if (bytes_read) {
267         reply[bytes_read] = '\0';
268         VLOG_ERR("vif_up returned: %s", reply);
269         dev->error = reply;
270         reply = NULL;
271         error = EAGAIN;
272         goto cleanup;
273     }
274     dev->finalized = true;
275     free(dev->error);
276     dev->error = NULL;
277     netdev_pltap_update_seq(dev);
278
279 cleanup:
280     free(msg);
281     free(reply);
282     close(ifd);
283     close(ofd);
284     return error;
285 }
286
287 static int
288 netdev_pltap_get_config(struct netdev_dev *dev_, struct smap *args)
289 {
290     struct netdev_dev_pltap *netdev_dev = netdev_dev_pltap_cast(dev_);
291
292     if (netdev_dev->valid_local_ip)
293         smap_add_format(args, "local_ip", IP_FMT,
294             IP_ARGS(&netdev_dev->local_addr.sin_addr));
295     if (netdev_dev->valid_local_netmask)
296         smap_add_format(args, "local_netmask", "%"PRIu32,
297             ntohs(netdev_dev->local_netmask));
298     return netdev_pltap_create_finalize(netdev_dev);
299 }
300
301 static int
302 netdev_pltap_set_config(struct netdev_dev *dev_, const struct smap *args)
303 {
304     struct netdev_dev_pltap *netdev_dev = netdev_dev_pltap_cast(dev_);
305     struct shash_node *node;
306
307     VLOG_DBG("pltap_set_config(%s)", netdev_dev_get_name(dev_));
308     SMAP_FOR_EACH(node, args) {
309         VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
310         if (!strcmp(node->name, "local_ip")) {
311             struct in_addr addr;
312             if (lookup_ip(node->data, &addr)) {
313                 VLOG_WARN("%s: bad 'local_ip'", node->name);
314             } else {
315                 netdev_dev->local_addr.sin_addr = addr;
316                 netdev_dev->valid_local_ip = true;
317             }
318         } else if (!strcmp(node->name, "local_netmask")) {
319             netdev_dev->local_netmask = atoi(node->data);
320             // XXX check valididy
321             netdev_dev->valid_local_netmask = true;
322         } else {
323             VLOG_WARN("%s: unknown argument '%s'", 
324                 netdev_dev_get_name(dev_), node->name);
325         }
326     }
327     return netdev_pltap_create_finalize(netdev_dev);        
328 }
329
330 static int
331 netdev_pltap_listen(struct netdev *netdev_ OVS_UNUSED)
332 {
333     return 0;
334 }
335
336 static int
337 netdev_pltap_recv(struct netdev *netdev_, void *buffer, size_t size)
338 {
339     struct netdev_dev_pltap *dev = 
340         netdev_dev_pltap_cast(netdev_get_dev(netdev_));
341     char prefix[4];
342     struct iovec iov[2] = {
343         { .iov_base = prefix, .iov_len = 4 },
344         { .iov_base = buffer, .iov_len = size }
345     };
346     if (!dev->finalized)
347         return -EAGAIN;
348     for (;;) {
349         ssize_t retval;
350         retval = readv(dev->fd, iov, 2);
351         if (retval >= 0) {
352             if (retval <= size) {
353                 return retval;
354             } else {
355                 return -EMSGSIZE;
356             }
357         } else if (errno != EINTR) {
358             if (errno != EAGAIN) {
359                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
360                     netdev_get_name(netdev_), strerror(errno));
361             }
362             return -errno;
363         }
364     }
365 }
366
367 static void
368 netdev_pltap_recv_wait(struct netdev *netdev_)
369 {
370     struct netdev_dev_pltap *dev = 
371         netdev_dev_pltap_cast(netdev_get_dev(netdev_));
372     if (dev->finalized && dev->fd >= 0) {
373         poll_fd_wait(dev->fd, POLLIN);
374     }
375 }
376
377 static int
378 netdev_pltap_send(struct netdev *netdev_, const void *buffer, size_t size)
379 {
380     struct netdev_dev_pltap *dev = 
381         netdev_dev_pltap_cast(netdev_get_dev(netdev_));
382     char prefix[4] = { 0, 0, 8, 6 };
383     struct iovec iov[2] = {
384         { .iov_base = prefix, .iov_len = 4 },
385         { .iov_base = buffer, .iov_len = size }
386     };
387     if (dev->fd < 0 || !dev->finalized)
388         return EAGAIN;
389     for (;;) {
390         ssize_t retval;
391         retval = writev(dev->fd, iov, 2);
392         if (retval >= 0) {
393             if (retval != size + 4) {
394                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of %zu) on %s",
395                              retval, size + 4, netdev_get_name(netdev_));
396             }
397             return 0;
398         } else if (errno != EINTR) {
399             if (errno != EAGAIN) {
400                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
401                     netdev_get_name(netdev_), strerror(errno));
402             }
403             return errno;
404         }
405     }
406 }
407
408 static void
409 netdev_pltap_send_wait(struct netdev *netdev_)
410 {
411     struct netdev_dev_pltap *dev = 
412         netdev_dev_pltap_cast(netdev_get_dev(netdev_));
413     if (dev->finalized && dev->fd >= 0) {
414         poll_fd_wait(dev->fd, POLLOUT);
415     }
416 }
417
418 static int
419 netdev_pltap_drain(struct netdev *netdev_)
420 {
421     struct netdev_dev_pltap *dev = 
422         netdev_dev_pltap_cast(netdev_get_dev(netdev_));
423     char buffer[128];
424     int error;
425
426     if (dev->fd < 0 || !dev->finalized)
427         return 0;
428     for (;;) {
429         error = recv(dev->fd, buffer, 128, MSG_TRUNC);
430         if (error) {
431             if (error == -EAGAIN)
432                 break;
433             else if (error != -EMSGSIZE)
434                 return error;
435         }
436     }
437     return 0;
438 }
439
440 static int
441 netdev_pltap_set_etheraddr(struct netdev *netdevi OVS_UNUSED,
442                            const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
443 {
444     return ENOTSUP;
445 }
446
447 // XXX from netdev-linux.c
448 static int
449 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
450 {
451     struct ifreq ifr;
452     int hwaddr_family;
453     int af_inet_sock;
454
455     /* Create AF_INET socket. */
456     af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
457     if (af_inet_sock < 0) {
458         VLOG_ERR("failed to create inet socket: %s", strerror(errno));
459     }
460
461     memset(&ifr, 0, sizeof ifr);
462     ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
463     if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
464         /* ENODEV probably means that a vif disappeared asynchronously and
465          * hasn't been removed from the database yet, so reduce the log level
466          * to INFO for that case. */
467         VLOG(errno == ENODEV ? VLL_INFO : VLL_ERR,
468              "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
469              netdev_name, strerror(errno));
470         return errno;
471     }
472     hwaddr_family = ifr.ifr_hwaddr.sa_family;
473     if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
474         VLOG_WARN("%s device has unknown hardware address family %d",
475                   netdev_name, hwaddr_family);
476     }
477     memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
478     return 0;
479 }
480
481 static int
482 netdev_pltap_get_etheraddr(const struct netdev *netdev,
483                            uint8_t mac[ETH_ADDR_LEN])
484 {
485     struct netdev_dev_pltap *dev = 
486         netdev_dev_pltap_cast(netdev_get_dev(netdev));
487     if (dev->fd < 0 || !dev->finalized)
488         return EAGAIN;
489     return get_etheraddr(dev->real_name, mac);
490 }
491
492
493 // XXX can we read stats in planetlab?
494 static int
495 netdev_pltap_get_stats(const struct netdev *netdev OVS_UNUSED, struct netdev_stats *stats OVS_UNUSED)
496 {
497     return ENOTSUP;
498 }
499
500 static int
501 netdev_pltap_set_stats(struct netdev *netdev OVS_UNUSED, const struct netdev_stats *stats OVS_UNUSED)
502 {
503     return ENOTSUP;
504 }
505
506 static int
507 netdev_pltap_update_flags(struct netdev *netdev,
508                           enum netdev_flags off, enum netdev_flags on,
509                           enum netdev_flags *old_flagsp)
510 {
511     struct netdev_dev_pltap *dev =
512         netdev_dev_pltap_cast(netdev_get_dev(netdev));
513
514     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
515         return EINVAL;
516     }
517
518     // XXX should we actually do something with these flags?
519     *old_flagsp = dev->flags;
520     dev->flags |= on;
521     dev->flags &= ~off;
522     if (*old_flagsp != dev->flags) {
523         netdev_pltap_update_seq(dev);
524     }
525     return 0;
526 }
527
528 static unsigned int
529 netdev_pltap_change_seq(const struct netdev *netdev)
530 {
531     return netdev_dev_pltap_cast(netdev_get_dev(netdev))->change_seq;
532 }
533 \f
534 /* Helper functions. */
535
536 static void
537 netdev_pltap_update_seq(struct netdev_dev_pltap *dev)
538 {
539     dev->change_seq++;
540     if (!dev->change_seq) {
541         dev->change_seq++;
542     }
543 }
544
545 static void
546 netdev_pltap_get_real_name(struct unixctl_conn *conn,
547                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
548 {
549     struct netdev_dev_pltap *pltap_dev;
550
551     pltap_dev = shash_find_data(&pltap_netdev_devs, argv[1]);
552     if (!pltap_dev) {
553         unixctl_command_reply_error(conn, "no such pltap netdev");
554         return;
555     }
556     if (pltap_dev->error) {
557         unixctl_command_reply_error(conn, pltap_dev->error);
558         return;
559     }
560
561     unixctl_command_reply(conn, pltap_dev->real_name);
562 }
563
564 static int
565 netdev_pltap_init(void)
566 {
567     unixctl_command_register("netdev-pltap/get-tapname", "port",
568                              1, 1, netdev_pltap_get_real_name, NULL);
569     return 0;
570 }
571
572 const struct netdev_class netdev_pltap_class = {
573     "pltap",
574     netdev_pltap_init,
575     NULL,  
576     NULL,            
577
578     netdev_pltap_create,
579     netdev_pltap_destroy,
580     netdev_pltap_get_config,
581     netdev_pltap_set_config, 
582
583     netdev_pltap_open,
584     netdev_pltap_close,
585
586     netdev_pltap_listen,
587     netdev_pltap_recv,
588     netdev_pltap_recv_wait,
589     netdev_pltap_drain,
590
591     netdev_pltap_send, 
592     netdev_pltap_send_wait,  
593
594     netdev_pltap_set_etheraddr,
595     netdev_pltap_get_etheraddr,
596     NULL,                       /* get_mtu */
597     NULL,                       /* set_mtu */
598     NULL,                       /* get_ifindex */
599     NULL,                       /* get_carrier */
600     NULL,                       /* get_carrier_resets */
601     NULL,                       /* get_miimon */
602     netdev_pltap_get_stats,
603     netdev_pltap_set_stats,
604
605     NULL,                       /* get_features */
606     NULL,                       /* set_advertisements */
607
608     NULL,                       /* set_policing */
609     NULL,                       /* get_qos_types */
610     NULL,                       /* get_qos_capabilities */
611     NULL,                       /* get_qos */
612     NULL,                       /* set_qos */
613     NULL,                       /* get_queue */
614     NULL,                       /* set_queue */
615     NULL,                       /* delete_queue */
616     NULL,                       /* get_queue_stats */
617     NULL,                       /* dump_queues */
618     NULL,                       /* dump_queue_stats */
619
620     NULL,                       /* get_in4 */
621     NULL,                       /* set_in4 */
622     NULL,                       /* get_in6 */
623     NULL,                       /* add_router */
624     NULL,                       /* get_next_hop */
625     NULL,                       /* get_drv_info */
626     NULL,                       /* arp_lookup */
627
628     netdev_pltap_update_flags,
629
630     netdev_pltap_change_seq
631 };