Merge branch 'master' of ssh://git.onelab.eu/git/sliver-openvswitch
[sliver-openvswitch.git] / lib / netdev-tunnel.c
1 /*
2  * Copyright (c) 2010, 2011, 2012 Nicira Networks.
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 <unistd.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <errno.h>
24
25 #include "flow.h"
26 #include "list.h"
27 #include "netdev-provider.h"
28 #include "odp-util.h"
29 #include "ofp-print.h"
30 #include "ofpbuf.h"
31 #include "packets.h"
32 #include "poll-loop.h"
33 #include "shash.h"
34 #include "sset.h"
35 #include "unixctl.h"
36 #include "socket-util.h"
37 #include "vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(netdev_tunnel);
40
41 struct netdev_tunnel {
42     struct netdev up;
43
44     /* Protects all members below. */
45     struct ovs_mutex mutex;
46
47     uint8_t hwaddr[ETH_ADDR_LEN];
48     struct netdev_stats stats;
49     enum netdev_flags flags;
50     int sockfd;
51     struct sockaddr_storage local_addr;
52     struct sockaddr_storage remote_addr;
53     bool valid_remote_ip;
54     bool valid_remote_port;
55     bool connected;
56     unsigned int change_seq;
57 };
58
59 struct netdev_rx_tunnel {
60     struct netdev_rx up;
61     int fd;
62 };
63
64 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
65
66 static struct ovs_mutex tunnel_netdevs_mutex = OVS_MUTEX_INITIALIZER;
67 static struct shash tunnel_netdevs OVS_GUARDED_BY(tunnel_netdevs_mutex)
68     = SHASH_INITIALIZER(&tunnel_netdevs);
69
70 static int netdev_tunnel_construct(struct netdev *netdevp_);
71 static void netdev_tunnel_update_seq(struct netdev_tunnel *);
72
73 static bool
74 is_netdev_tunnel_class(const struct netdev_class *class)
75 {
76     return class->construct == netdev_tunnel_construct;
77 }
78
79 static struct netdev_tunnel *
80 netdev_tunnel_cast(const struct netdev *netdev)
81 {
82     ovs_assert(is_netdev_tunnel_class(netdev_get_class(netdev)));
83     return CONTAINER_OF(netdev, struct netdev_tunnel, up);
84 }
85
86 static struct netdev_rx_tunnel *
87 netdev_rx_tunnel_cast(const struct netdev_rx *rx)
88 {
89     ovs_assert(is_netdev_tunnel_class(netdev_get_class(rx->netdev)));
90     return CONTAINER_OF(rx, struct netdev_rx_tunnel, up);
91 }
92
93 static struct netdev *
94 netdev_tunnel_alloc(void)
95 {
96     struct netdev_tunnel *netdev = xzalloc(sizeof *netdev);
97     return &netdev->up;
98 }
99
100 static int
101 netdev_tunnel_construct(struct netdev *netdev_)
102 {
103     static atomic_uint next_n = ATOMIC_VAR_INIT(0);
104     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
105     unsigned int n;
106
107     atomic_add(&next_n, 1, &n);
108
109     ovs_mutex_init(&netdev->mutex);
110     netdev->hwaddr[0] = 0xfe;
111     netdev->hwaddr[1] = 0xff;
112     netdev->hwaddr[2] = 0xff;
113     netdev->hwaddr[3] = n >> 16;
114     netdev->hwaddr[4] = n >> 8;
115     netdev->hwaddr[5] = n;
116     netdev->flags = 0;
117     netdev->change_seq = 1;
118     memset(&netdev->remote_addr, 0, sizeof(netdev->remote_addr));
119     netdev->valid_remote_ip = false;
120     netdev->valid_remote_port = false;
121     netdev->connected = false;
122
123
124     netdev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0,
125         &netdev->local_addr, 0);
126     if (netdev->sockfd < 0) {
127         return netdev->sockfd;
128     }
129
130
131     shash_add(&tunnel_netdevs, netdev_get_name(netdev_), netdev);
132
133     n++;
134
135     VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d",
136         netdev_get_name(netdev_), netdev->sockfd, ss_get_port(&netdev->local_addr));
137
138     return 0;
139
140 }
141
142 static void
143 netdev_tunnel_destruct(struct netdev *netdev_)
144 {
145     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
146
147     ovs_mutex_lock(&tunnel_netdevs_mutex);
148
149     if (netdev->sockfd != -1)
150         close(netdev->sockfd);
151
152     shash_find_and_delete(&tunnel_netdevs,
153                           netdev_get_name(netdev_));
154
155     ovs_mutex_destroy(&netdev->mutex);
156     ovs_mutex_unlock(&tunnel_netdevs_mutex);
157 }
158
159 static void
160 netdev_tunnel_dealloc(struct netdev *netdev_)
161 {
162     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
163     free(netdev);
164 }
165
166 static int
167 netdev_tunnel_get_config(const struct netdev *dev_, struct smap *args)
168 {
169     struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
170
171     ovs_mutex_lock(&netdev->mutex);
172     if (netdev->valid_remote_ip) {
173         const struct sockaddr_in *sin =
174             ALIGNED_CAST(const struct sockaddr_in *, &netdev->remote_addr);
175         smap_add_format(args, "remote_ip", IP_FMT,
176                 IP_ARGS(sin->sin_addr.s_addr));
177     }
178     if (netdev->valid_remote_port)
179         smap_add_format(args, "remote_port", "%"PRIu16,
180                 ss_get_port(&netdev->remote_addr));
181     ovs_mutex_unlock(&netdev->mutex);
182     return 0;
183 }
184
185 static int
186 netdev_tunnel_connect(struct netdev_tunnel *dev)
187     OVS_REQUIRES(dev->mutex)
188 {
189     char buf[1024];
190     struct sockaddr_in *sin =
191         ALIGNED_CAST(struct sockaddr_in *, &dev->remote_addr);
192     if (dev->sockfd < 0)
193         return EBADF;
194     if (!dev->valid_remote_ip || !dev->valid_remote_port)
195         return 0;
196     if (connect(dev->sockfd, (struct sockaddr*) sin, sizeof(*sin)) < 0) {
197         VLOG_DBG("%s: connect returned %s", netdev_get_name(&dev->up),
198             ovs_strerror(errno));
199         return errno;
200     }
201     dev->connected = true;
202     netdev_tunnel_update_seq(dev);
203     VLOG_DBG("%s: connected to (%s, %d)", netdev_get_name(&dev->up),
204         inet_ntop(AF_INET, &sin->sin_addr.s_addr, buf, 1024),
205         ss_get_port(&dev->remote_addr));
206     return 0;
207 }
208
209 static int
210 netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args)
211 {
212     struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
213     struct shash_node *node;
214     int error;
215     struct sockaddr_in *sin =
216         ALIGNED_CAST(struct sockaddr_in *, &netdev->remote_addr);
217
218     ovs_mutex_lock(&netdev->mutex);
219     VLOG_DBG("tunnel_set_config(%s)", netdev_get_name(dev_));
220     SMAP_FOR_EACH(node, args) {
221         VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
222         if (!strcmp(node->name, "remote_ip")) {
223             struct in_addr addr;
224             if (lookup_ip(node->data, &addr)) {
225                 VLOG_WARN("%s: bad 'remote_ip'", node->name);
226             } else {
227         sin->sin_family = AF_INET;
228                 sin->sin_addr = addr;
229                 netdev->valid_remote_ip = true;
230             }
231         } else if (!strcmp(node->name, "remote_port")) {
232             sin->sin_port = htons(atoi(node->data));
233             netdev->valid_remote_port = true;
234         } else {
235             VLOG_WARN("%s: unknown argument '%s'", 
236                 netdev_get_name(dev_), node->name);
237         }
238     }
239     error = netdev_tunnel_connect(netdev);        
240     ovs_mutex_unlock(&netdev->mutex);
241     return error;
242 }
243
244 static struct netdev_rx *
245 netdev_tunnel_rx_alloc(void)
246 {
247     struct netdev_rx_tunnel *rx = xzalloc(sizeof *rx);
248     return &rx->up;
249 }
250
251 static int
252 netdev_tunnel_rx_construct(struct netdev_rx *rx_)
253 {   
254     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
255     struct netdev *netdev_ = rx->up.netdev;
256     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
257
258     ovs_mutex_lock(&netdev->mutex);
259     rx->fd = netdev->sockfd;
260     ovs_mutex_unlock(&netdev->mutex);
261     return 0;
262 }
263
264 static void
265 netdev_tunnel_rx_destruct(struct netdev_rx *rx_ OVS_UNUSED)
266 {
267 }
268
269 static void
270 netdev_tunnel_rx_dealloc(struct netdev_rx *rx_)
271 {
272     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
273
274     free(rx);
275 }
276
277 static int
278 netdev_tunnel_rx_recv(struct netdev_rx *rx_, struct ofpbuf *buffer)
279 {
280     size_t size = ofpbuf_tailroom(buffer);
281     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
282     struct netdev_tunnel *netdev =
283         netdev_tunnel_cast(rx_->netdev);
284     if (!netdev->connected)
285         return EAGAIN;
286     for (;;) {
287         ssize_t retval;
288         retval = recv(rx->fd, buffer->data, size, MSG_TRUNC);
289             VLOG_DBG("%s: recv(%"PRIxPTR", %"PRIuSIZE", MSG_TRUNC) = %"PRIdSIZE,
290                     netdev_rx_get_name(rx_), (uintptr_t)buffer->data, size, retval);
291         if (retval >= 0) {
292                 netdev->stats.rx_packets++;
293                 netdev->stats.rx_bytes += retval;
294             if (retval <= size) {
295                 buffer->size += retval;
296                     return 0;
297             } else {
298                 netdev->stats.rx_errors++;
299                 netdev->stats.rx_length_errors++;
300                 return EMSGSIZE;
301             }
302         } else if (errno != EINTR) {
303             if (errno != EAGAIN) {
304                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
305                     netdev_rx_get_name(rx_), ovs_strerror(errno));
306                     netdev->stats.rx_errors++;
307             }
308             return errno;
309         }
310     }
311 }
312
313 static void
314 netdev_tunnel_rx_wait(struct netdev_rx *rx_)
315 {
316     struct netdev_rx_tunnel *rx = 
317         netdev_rx_tunnel_cast(rx_);
318     if (rx->fd >= 0) {
319         poll_fd_wait(rx->fd, POLLIN);
320     }
321 }
322
323 static int
324 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
325 {
326     struct netdev_tunnel *dev = 
327         netdev_tunnel_cast(netdev_);
328     if (!dev->connected)
329         return EAGAIN;
330     for (;;) {
331         ssize_t retval;
332         retval = send(dev->sockfd, buffer, size, 0);
333         VLOG_DBG("%s: send(%"PRIxPTR", %"PRIuSIZE") = %"PRIdSIZE,
334                  netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
335         if (retval >= 0) {
336             dev->stats.tx_packets++;
337             dev->stats.tx_bytes += retval;
338             if (retval != size) {
339                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIdSIZE" bytes of "
340                              "%"PRIuSIZE") on %s", retval, size, netdev_get_name(netdev_));
341                 dev->stats.tx_errors++;
342             }
343             return 0;
344         } else if (errno != EINTR) {
345             if (errno != EAGAIN) {
346                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
347                     netdev_get_name(netdev_), ovs_strerror(errno));
348                 dev->stats.tx_errors++;
349             }
350             return errno;
351         }
352     }
353 }
354
355 static void
356 netdev_tunnel_send_wait(struct netdev *netdev_)
357 {
358     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
359     if (dev->sockfd >= 0) {
360         poll_fd_wait(dev->sockfd, POLLOUT);
361     }
362 }
363
364 static int
365 netdev_tunnel_rx_drain(struct netdev_rx *rx_)
366 {
367     struct netdev_tunnel *netdev =
368         netdev_tunnel_cast(rx_->netdev);
369     struct netdev_rx_tunnel *rx = 
370         netdev_rx_tunnel_cast(rx_);
371     char buffer[128];
372     int error;
373
374     if (!netdev->connected)
375         return 0;
376     for (;;) {
377         error = recv(rx->fd, buffer, 128, MSG_TRUNC);
378         if (error) {
379             if (error == -EAGAIN)
380                 break;
381             else if (error != -EMSGSIZE)
382                 return error;
383         }
384     }
385     return 0;
386 }
387
388 static int
389 netdev_tunnel_set_etheraddr(struct netdev *netdev,
390                            const uint8_t mac[ETH_ADDR_LEN])
391 {
392     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
393
394     ovs_mutex_lock(&dev->mutex);
395     if (!eth_addr_equals(dev->hwaddr, mac)) {
396         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
397         netdev_tunnel_update_seq(dev);
398     }
399     ovs_mutex_unlock(&dev->mutex);
400
401     return 0;
402 }
403
404 static int
405 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
406                            uint8_t mac[ETH_ADDR_LEN])
407 {
408     const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
409
410     ovs_mutex_lock(&dev->mutex);
411     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
412     ovs_mutex_unlock(&dev->mutex);
413     return 0;
414 }
415
416
417 static int
418 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
419 {
420     const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
421
422     ovs_mutex_lock(&dev->mutex);
423     *stats = dev->stats;
424     ovs_mutex_unlock(&dev->mutex);
425     return 0;
426 }
427
428 static int
429 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
430 {
431     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
432
433     ovs_mutex_lock(&dev->mutex);
434     dev->stats = *stats;
435     ovs_mutex_unlock(&dev->mutex);
436     return 0;
437 }
438
439 static int
440 netdev_tunnel_update_flags(struct netdev *dev_,
441                           enum netdev_flags off, enum netdev_flags on,
442                           enum netdev_flags *old_flagsp)
443 {
444     struct netdev_tunnel *netdev =
445         netdev_tunnel_cast(dev_);
446     int error = 0;
447
448     ovs_mutex_lock(&netdev->mutex);
449     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
450         error = EINVAL;
451         goto out;
452     }
453
454     // XXX should we actually do something with these flags?
455     *old_flagsp = netdev->flags;
456     netdev->flags |= on;
457     netdev->flags &= ~off;
458     if (*old_flagsp != netdev->flags) {
459         netdev_tunnel_update_seq(netdev);
460     }
461
462 out:
463     ovs_mutex_unlock(&netdev->mutex);
464     return error;
465 }
466
467 \f
468 /* Helper functions. */
469
470 static void
471 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
472     OVS_REQUIRES(dev->mutex)
473 {
474     dev->change_seq++;
475     if (!dev->change_seq) {
476         dev->change_seq++;
477     }
478 }
479
480 static void
481 netdev_tunnel_get_port(struct unixctl_conn *conn,
482                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
483 {
484     struct netdev_tunnel *tunnel_dev;
485     char buf[6];
486
487     ovs_mutex_lock(&tunnel_netdevs_mutex);
488     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
489     if (!tunnel_dev) {
490         unixctl_command_reply_error(conn, "no such tunnel netdev");
491         goto out;
492     }
493
494     ovs_mutex_lock(&tunnel_dev->mutex);
495     sprintf(buf, "%d", ss_get_port(&tunnel_dev->local_addr));
496     ovs_mutex_unlock(&tunnel_dev->mutex);
497
498     unixctl_command_reply(conn, buf);
499 out:
500     ovs_mutex_unlock(&tunnel_netdevs_mutex);
501 }
502
503 static void
504 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
505                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
506 {
507     struct netdev_tunnel *tunnel_dev;
508     char buf[128];
509
510     ovs_mutex_lock(&tunnel_netdevs_mutex);
511     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
512     if (!tunnel_dev) {
513         unixctl_command_reply_error(conn, "no such tunnel netdev");
514         goto out;
515     }
516
517     ovs_mutex_lock(&tunnel_dev->mutex);
518     sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
519     ovs_mutex_unlock(&tunnel_dev->mutex);
520     unixctl_command_reply(conn, buf);
521 out:
522     ovs_mutex_unlock(&tunnel_netdevs_mutex);
523 }
524
525 static void
526 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
527                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
528 {
529     struct netdev_tunnel *tunnel_dev;
530     char buf[128];
531
532     ovs_mutex_lock(&tunnel_netdevs_mutex);
533     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
534     if (!tunnel_dev) {
535         unixctl_command_reply_error(conn, "no such tunnel netdev");
536         goto out;
537     }
538
539     sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
540     unixctl_command_reply(conn, buf);
541 out:
542     ovs_mutex_unlock(&tunnel_netdevs_mutex);
543 }
544
545
546 static int
547 netdev_tunnel_init(void)
548 {
549     unixctl_command_register("netdev-tunnel/get-port", "NAME",
550                              1, 1, netdev_tunnel_get_port, NULL);
551     unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
552                              1, 1, netdev_tunnel_get_tx_bytes, NULL);
553     unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
554                              1, 1, netdev_tunnel_get_rx_bytes, NULL);
555     return 0;
556 }
557
558 static void
559 netdev_tunnel_run(void)
560 {
561 }
562
563 static void
564 netdev_tunnel_wait(void)
565 {
566 }
567
568 const struct netdev_class netdev_tunnel_class = {
569     "tunnel",
570     netdev_tunnel_init,    
571     netdev_tunnel_run,      
572     netdev_tunnel_wait,   
573
574     netdev_tunnel_alloc,
575     netdev_tunnel_construct,
576     netdev_tunnel_destruct,
577     netdev_tunnel_dealloc,
578     netdev_tunnel_get_config,
579     netdev_tunnel_set_config, 
580     NULL,                                   /* get_tunnel_config */
581
582     netdev_tunnel_send, 
583     netdev_tunnel_send_wait,  
584
585     netdev_tunnel_set_etheraddr,
586     netdev_tunnel_get_etheraddr,
587     NULL,                                   /* get_mtu */
588     NULL,                                   /* set_mtu */
589     NULL,                       /* get_ifindex */
590     NULL,                                   /* get_carrier */
591     NULL,                       /* get_carrier_resets */
592     NULL,                       /* get_miimon */
593     netdev_tunnel_get_stats,
594     netdev_tunnel_set_stats,
595
596     NULL,                       /* get_features */
597     NULL,                       /* set_advertisements */
598
599     NULL,                       /* set_policing */
600     NULL,                       /* get_qos_types */
601     NULL,                       /* get_qos_capabilities */
602     NULL,                       /* get_qos */
603     NULL,                       /* set_qos */
604     NULL,                       /* get_queue */
605     NULL,                       /* set_queue */
606     NULL,                       /* delete_queue */
607     NULL,                       /* get_queue_stats */
608     NULL,                       /* queue_dump_start */
609     NULL,                       /* queue_dump_next */
610     NULL,                       /* queue_dump_done */
611     NULL,                       /* dump_queue_stats */
612
613     NULL,                       /* get_in4 */
614     NULL,                       /* set_in4 */
615     NULL,                       /* get_in6 */
616     NULL,                       /* add_router */
617     NULL,                       /* get_next_hop */
618     NULL,                       /* get_status */
619     NULL,                       /* arp_lookup */
620
621     netdev_tunnel_update_flags,
622
623     netdev_tunnel_rx_alloc,
624     netdev_tunnel_rx_construct,
625     netdev_tunnel_rx_destruct,
626     netdev_tunnel_rx_dealloc,
627     netdev_tunnel_rx_recv,
628     netdev_tunnel_rx_wait,
629     netdev_tunnel_rx_drain,
630 };