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