22b7674be557a262716b25944c2f16a28908830e
[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     if (!dev->finalized)
342         return -EAGAIN;
343     for (;;) {
344         ssize_t retval;
345         retval = read(dev->fd, buffer, size);
346         VLOG_DBG("%s: read(%"PRIxPTR", %"PRIu64") = %"PRId64,
347                  netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
348         if (retval >= 0) {
349             if (retval <= size) {
350                 return retval;
351             } else {
352                 return -EMSGSIZE;
353             }
354         } else if (errno != EINTR) {
355             if (errno != EAGAIN) {
356                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
357                     netdev_get_name(netdev_), strerror(errno));
358             }
359             return -errno;
360         }
361     }
362 }
363
364 static void
365 netdev_pltap_recv_wait(struct netdev *netdev_)
366 {
367     struct netdev_dev_pltap *dev = 
368         netdev_dev_pltap_cast(netdev_get_dev(netdev_));
369     if (dev->finalized && dev->fd >= 0) {
370         poll_fd_wait(dev->fd, POLLIN);
371     }
372 }
373
374 static int
375 netdev_pltap_send(struct netdev *netdev_, const void *buffer, size_t size)
376 {
377     struct netdev_dev_pltap *dev = 
378         netdev_dev_pltap_cast(netdev_get_dev(netdev_));
379     if (dev->fd < 0 || !dev->finalized)
380         return -EAGAIN;
381     for (;;) {
382         ssize_t retval;
383         retval = write(dev->fd, buffer, size);
384         VLOG_DBG("%s: write(%"PRIxPTR", %"PRIu64") = %"PRId64,
385                  netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
386         if (retval >= 0) {
387             if (retval != size) {
388                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRId64" bytes of "
389                              "%"PRIu64") on %s", retval, size, netdev_get_name(netdev_));
390             }
391             return 0;
392         } else if (errno != EINTR) {
393             if (errno != EAGAIN) {
394                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
395                     netdev_get_name(netdev_), strerror(errno));
396             }
397             return errno;
398         }
399     }
400 }
401
402 static void
403 netdev_pltap_send_wait(struct netdev *netdev_)
404 {
405     struct netdev_dev_pltap *dev = 
406         netdev_dev_pltap_cast(netdev_get_dev(netdev_));
407     if (dev->finalized && dev->fd >= 0) {
408         poll_fd_wait(dev->fd, POLLOUT);
409     }
410 }
411
412 static int
413 netdev_pltap_drain(struct netdev *netdev_)
414 {
415     struct netdev_dev_pltap *dev = 
416         netdev_dev_pltap_cast(netdev_get_dev(netdev_));
417     char buffer[128];
418     int error;
419
420     if (dev->fd < 0 || !dev->finalized)
421         return 0;
422     for (;;) {
423         error = recv(dev->fd, buffer, 128, MSG_TRUNC);
424         if (error) {
425             if (error == -EAGAIN)
426                 break;
427             else if (error != -EMSGSIZE)
428                 return error;
429         }
430     }
431     return 0;
432 }
433
434 static int
435 netdev_pltap_set_etheraddr(struct netdev *netdev,
436                            const uint8_t mac[ETH_ADDR_LEN])
437 {
438     return ENOTSUP;
439 }
440
441 // XXX from netdev-linux.c
442 static int
443 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
444 {
445     struct ifreq ifr;
446     int hwaddr_family;
447     int af_inet_sock;
448
449     /* Create AF_INET socket. */
450     af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
451     if (af_inet_sock < 0) {
452         VLOG_ERR("failed to create inet socket: %s", strerror(errno));
453     }
454
455     memset(&ifr, 0, sizeof ifr);
456     ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
457     if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
458         /* ENODEV probably means that a vif disappeared asynchronously and
459          * hasn't been removed from the database yet, so reduce the log level
460          * to INFO for that case. */
461         VLOG(errno == ENODEV ? VLL_INFO : VLL_ERR,
462              "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
463              netdev_name, strerror(errno));
464         return errno;
465     }
466     hwaddr_family = ifr.ifr_hwaddr.sa_family;
467     if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
468         VLOG_WARN("%s device has unknown hardware address family %d",
469                   netdev_name, hwaddr_family);
470     }
471     memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
472     return 0;
473 }
474
475 static int
476 netdev_pltap_get_etheraddr(const struct netdev *netdev,
477                            uint8_t mac[ETH_ADDR_LEN])
478 {
479     struct netdev_dev_pltap *dev = 
480         netdev_dev_pltap_cast(netdev_get_dev(netdev));
481     if (dev->fd < 0 || !dev->finalized)
482         return EAGAIN;
483     return get_etheraddr(dev->real_name, mac);
484 }
485
486
487 // XXX can we read stats in planetlab?
488 static int
489 netdev_pltap_get_stats(const struct netdev *netdev OVS_UNUSED, struct netdev_stats *stats OVS_UNUSED)
490 {
491     return -ENOTSUP;
492 }
493
494 static int
495 netdev_pltap_set_stats(struct netdev *netdev OVS_UNUSED, const struct netdev_stats *stats OVS_UNUSED)
496 {
497     return -ENOTSUP;
498 }
499
500 static int
501 netdev_pltap_update_flags(struct netdev *netdev,
502                           enum netdev_flags off, enum netdev_flags on,
503                           enum netdev_flags *old_flagsp)
504 {
505     struct netdev_dev_pltap *dev =
506         netdev_dev_pltap_cast(netdev_get_dev(netdev));
507
508     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
509         return EINVAL;
510     }
511
512     // XXX should we actually do something with these flags?
513     *old_flagsp = dev->flags;
514     dev->flags |= on;
515     dev->flags &= ~off;
516     if (*old_flagsp != dev->flags) {
517         netdev_pltap_update_seq(dev);
518     }
519     return 0;
520 }
521
522 static unsigned int
523 netdev_pltap_change_seq(const struct netdev *netdev)
524 {
525     return netdev_dev_pltap_cast(netdev_get_dev(netdev))->change_seq;
526 }
527 \f
528 /* Helper functions. */
529
530 static void
531 netdev_pltap_update_seq(struct netdev_dev_pltap *dev)
532 {
533     dev->change_seq++;
534     if (!dev->change_seq) {
535         dev->change_seq++;
536     }
537 }
538
539 static void
540 netdev_pltap_get_real_name(struct unixctl_conn *conn,
541                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
542 {
543     struct netdev_dev_pltap *pltap_dev;
544
545     pltap_dev = shash_find_data(&pltap_netdev_devs, argv[1]);
546     if (!pltap_dev) {
547         unixctl_command_reply_error(conn, "no such pltap netdev");
548         return;
549     }
550     if (pltap_dev->error) {
551         unixctl_command_reply_error(conn, pltap_dev->error);
552         return;
553     }
554
555     unixctl_command_reply(conn, pltap_dev->real_name);
556 }
557
558 static int
559 netdev_pltap_init(void)
560 {
561     unixctl_command_register("netdev-pltap/get-tapname", "port",
562                              1, 1, netdev_pltap_get_real_name, NULL);
563     return 0;
564 }
565
566 const struct netdev_class netdev_pltap_class = {
567     "pltap",
568     netdev_pltap_init,
569     NULL,  
570     NULL,            
571
572     netdev_pltap_create,
573     netdev_pltap_destroy,
574     netdev_pltap_get_config,
575     netdev_pltap_set_config, 
576
577     netdev_pltap_open,
578     netdev_pltap_close,
579
580     netdev_pltap_listen,
581     netdev_pltap_recv,
582     netdev_pltap_recv_wait,
583     netdev_pltap_drain,
584
585     netdev_pltap_send, 
586     netdev_pltap_send_wait,  
587
588     netdev_pltap_set_etheraddr,
589     netdev_pltap_get_etheraddr,
590     NULL,                       /* get_mtu */
591     NULL,                       /* set_mtu */
592     NULL,                       /* get_ifindex */
593     NULL,                       /* get_carrier */
594     NULL,                       /* get_carrier_resets */
595     NULL,                       /* get_miimon */
596     netdev_pltap_get_stats,
597     netdev_pltap_set_stats,
598
599     NULL,                       /* get_features */
600     NULL,                       /* set_advertisements */
601
602     NULL,                       /* set_policing */
603     NULL,                       /* get_qos_types */
604     NULL,                       /* get_qos_capabilities */
605     NULL,                       /* get_qos */
606     NULL,                       /* set_qos */
607     NULL,                       /* get_queue */
608     NULL,                       /* set_queue */
609     NULL,                       /* delete_queue */
610     NULL,                       /* get_queue_stats */
611     NULL,                       /* dump_queues */
612     NULL,                       /* dump_queue_stats */
613
614     NULL,                       /* get_in4 */
615     NULL,                       /* set_in4 */
616     NULL,                       /* get_in6 */
617     NULL,                       /* add_router */
618     NULL,                       /* get_next_hop */
619     NULL,                       /* get_drv_info */
620     NULL,                       /* arp_lookup */
621
622     netdev_pltap_update_flags,
623
624     netdev_pltap_change_seq
625 };