ec0f71f54769bb2aabb08b2ea481517b464a68c3
[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_in local_addr;
52     struct sockaddr_in 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, &netdev->local_addr, 0);
125     if (netdev->sockfd < 0) {
126         return netdev->sockfd;
127     }
128
129
130     shash_add(&tunnel_netdevs, netdev_get_name(netdev_), netdev);
131
132     n++;
133
134     VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d",
135         netdev_get_name(netdev_), netdev->sockfd, netdev->local_addr.sin_port);
136
137     return 0;
138
139 }
140
141 static void
142 netdev_tunnel_destruct(struct netdev *netdev_)
143 {
144     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
145
146     ovs_mutex_lock(&tunnel_netdevs_mutex);
147
148     if (netdev->sockfd != -1)
149         close(netdev->sockfd);
150
151     shash_find_and_delete(&tunnel_netdevs,
152                           netdev_get_name(netdev_));
153
154     ovs_mutex_destroy(&netdev->mutex);
155     ovs_mutex_unlock(&tunnel_netdevs_mutex);
156 }
157
158 static void
159 netdev_tunnel_dealloc(struct netdev *netdev_)
160 {
161     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
162     free(netdev);
163 }
164
165 static int
166 netdev_tunnel_get_config(const struct netdev *dev_, struct smap *args)
167 {
168     struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
169
170     ovs_mutex_lock(&netdev->mutex);
171     if (netdev->valid_remote_ip)
172         smap_add_format(args, "remote_ip", IP_FMT,
173                 IP_ARGS(netdev->remote_addr.sin_addr.s_addr));
174     if (netdev->valid_remote_port)
175         smap_add_format(args, "remote_port", "%"PRIu16,
176                 ntohs(netdev->remote_addr.sin_port));
177     ovs_mutex_unlock(&netdev->mutex);
178     return 0;
179 }
180
181 static int
182 netdev_tunnel_connect(struct netdev_tunnel *dev)
183     OVS_REQUIRES(dev->mutex)
184 {
185     char buf[1024];
186     if (dev->sockfd < 0)
187         return EBADF;
188     if (!dev->valid_remote_ip || !dev->valid_remote_port)
189         return 0;
190     dev->remote_addr.sin_family = AF_INET;
191     if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
192         return errno;
193     }
194     dev->connected = true;
195     netdev_tunnel_update_seq(dev);
196     VLOG_DBG("%s: connected to (%s, %d)", netdev_get_name(&dev->up),
197         inet_ntop(AF_INET, &dev->remote_addr.sin_addr, buf, 1024), ntohs(dev->remote_addr.sin_port));
198     return 0;
199 }
200
201 static int
202 netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args)
203 {
204     struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
205     struct shash_node *node;
206     int error;
207
208     ovs_mutex_lock(&netdev->mutex);
209     VLOG_DBG("tunnel_set_config(%s)", netdev_get_name(dev_));
210     SMAP_FOR_EACH(node, args) {
211         VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
212         if (!strcmp(node->name, "remote_ip")) {
213             struct in_addr addr;
214             if (lookup_ip(node->data, &addr)) {
215                 VLOG_WARN("%s: bad 'remote_ip'", node->name);
216             } else {
217                 netdev->remote_addr.sin_addr = addr;
218                 netdev->valid_remote_ip = true;
219             }
220         } else if (!strcmp(node->name, "remote_port")) {
221             netdev->remote_addr.sin_port = htons(atoi(node->data));
222             netdev->valid_remote_port = true;
223         } else {
224             VLOG_WARN("%s: unknown argument '%s'", 
225                 netdev_get_name(dev_), node->name);
226         }
227     }
228     error = netdev_tunnel_connect(netdev);        
229     ovs_mutex_unlock(&netdev->mutex);
230     return error;
231 }
232
233 static struct netdev_rx *
234 netdev_tunnel_rx_alloc(void)
235 {
236     struct netdev_rx_tunnel *rx = xzalloc(sizeof *rx);
237     return &rx->up;
238 }
239
240 static int
241 netdev_tunnel_rx_construct(struct netdev_rx *rx_)
242 {   
243     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
244     struct netdev *netdev_ = rx->up.netdev;
245     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
246
247     ovs_mutex_lock(&netdev->mutex);
248     rx->fd = netdev->sockfd;
249     ovs_mutex_unlock(&netdev->mutex);
250     return 0;
251 }
252
253 static void
254 netdev_tunnel_rx_destruct(struct netdev_rx *rx_ OVS_UNUSED)
255 {
256 }
257
258 static void
259 netdev_tunnel_rx_dealloc(struct netdev_rx *rx_)
260 {
261     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
262
263     free(rx);
264 }
265
266 static int
267 netdev_tunnel_rx_recv(struct netdev_rx *rx_, struct ofpbuf *buffer)
268 {
269     size_t size = ofpbuf_tailroom(buffer);
270     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
271     struct netdev_tunnel *netdev =
272         netdev_tunnel_cast(rx_->netdev);
273     if (!netdev->connected)
274         return EAGAIN;
275     for (;;) {
276         ssize_t retval;
277         retval = recv(rx->fd, buffer->data, size, MSG_TRUNC);
278             VLOG_DBG("%s: recv(%"PRIxPTR", %"PRIuSIZE", MSG_TRUNC) = %"PRIdSIZE,
279                     netdev_rx_get_name(rx_), (uintptr_t)buffer->data, size, retval);
280         if (retval >= 0) {
281                 netdev->stats.rx_packets++;
282                 netdev->stats.rx_bytes += retval;
283             if (retval <= size) {
284                 buffer->size += retval;
285                     return 0;
286             } else {
287                 netdev->stats.rx_errors++;
288                 netdev->stats.rx_length_errors++;
289                 return EMSGSIZE;
290             }
291         } else if (errno != EINTR) {
292             if (errno != EAGAIN) {
293                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
294                     netdev_rx_get_name(rx_), ovs_strerror(errno));
295                     netdev->stats.rx_errors++;
296             }
297             return errno;
298         }
299     }
300 }
301
302 static void
303 netdev_tunnel_rx_wait(struct netdev_rx *rx_)
304 {
305     struct netdev_rx_tunnel *rx = 
306         netdev_rx_tunnel_cast(rx_);
307     if (rx->fd >= 0) {
308         poll_fd_wait(rx->fd, POLLIN);
309     }
310 }
311
312 static int
313 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
314 {
315     struct netdev_tunnel *dev = 
316         netdev_tunnel_cast(netdev_);
317     if (!dev->connected)
318         return EAGAIN;
319     for (;;) {
320         ssize_t retval;
321         retval = send(dev->sockfd, buffer, size, 0);
322         VLOG_DBG("%s: send(%"PRIxPTR", %"PRIuSIZE") = %"PRIdSIZE,
323                  netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
324         if (retval >= 0) {
325             dev->stats.tx_packets++;
326             dev->stats.tx_bytes += retval;
327             if (retval != size) {
328                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIdSIZE" bytes of "
329                              "%"PRIuSIZE") on %s", retval, size, netdev_get_name(netdev_));
330                 dev->stats.tx_errors++;
331             }
332             return 0;
333         } else if (errno != EINTR) {
334             if (errno != EAGAIN) {
335                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
336                     netdev_get_name(netdev_), ovs_strerror(errno));
337                 dev->stats.tx_errors++;
338             }
339             return errno;
340         }
341     }
342 }
343
344 static void
345 netdev_tunnel_send_wait(struct netdev *netdev_)
346 {
347     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
348     if (dev->sockfd >= 0) {
349         poll_fd_wait(dev->sockfd, POLLOUT);
350     }
351 }
352
353 static int
354 netdev_tunnel_rx_drain(struct netdev_rx *rx_)
355 {
356     struct netdev_tunnel *netdev =
357         netdev_tunnel_cast(rx_->netdev);
358     struct netdev_rx_tunnel *rx = 
359         netdev_rx_tunnel_cast(rx_);
360     char buffer[128];
361     int error;
362
363     if (!netdev->connected)
364         return 0;
365     for (;;) {
366         error = recv(rx->fd, buffer, 128, MSG_TRUNC);
367         if (error) {
368             if (error == -EAGAIN)
369                 break;
370             else if (error != -EMSGSIZE)
371                 return error;
372         }
373     }
374     return 0;
375 }
376
377 static int
378 netdev_tunnel_set_etheraddr(struct netdev *netdev,
379                            const uint8_t mac[ETH_ADDR_LEN])
380 {
381     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
382
383     ovs_mutex_lock(&dev->mutex);
384     if (!eth_addr_equals(dev->hwaddr, mac)) {
385         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
386         netdev_tunnel_update_seq(dev);
387     }
388     ovs_mutex_unlock(&dev->mutex);
389
390     return 0;
391 }
392
393 static int
394 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
395                            uint8_t mac[ETH_ADDR_LEN])
396 {
397     const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
398
399     ovs_mutex_lock(&dev->mutex);
400     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
401     ovs_mutex_unlock(&dev->mutex);
402     return 0;
403 }
404
405
406 static int
407 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
408 {
409     const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
410
411     ovs_mutex_lock(&dev->mutex);
412     *stats = dev->stats;
413     ovs_mutex_unlock(&dev->mutex);
414     return 0;
415 }
416
417 static int
418 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
419 {
420     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
421
422     ovs_mutex_lock(&dev->mutex);
423     dev->stats = *stats;
424     ovs_mutex_unlock(&dev->mutex);
425     return 0;
426 }
427
428 static int
429 netdev_tunnel_update_flags(struct netdev *dev_,
430                           enum netdev_flags off, enum netdev_flags on,
431                           enum netdev_flags *old_flagsp)
432 {
433     struct netdev_tunnel *netdev =
434         netdev_tunnel_cast(dev_);
435     int error = 0;
436
437     ovs_mutex_lock(&netdev->mutex);
438     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
439         error = EINVAL;
440         goto out;
441     }
442
443     // XXX should we actually do something with these flags?
444     *old_flagsp = netdev->flags;
445     netdev->flags |= on;
446     netdev->flags &= ~off;
447     if (*old_flagsp != netdev->flags) {
448         netdev_tunnel_update_seq(netdev);
449     }
450
451 out:
452     ovs_mutex_unlock(&netdev->mutex);
453     return error;
454 }
455
456 \f
457 /* Helper functions. */
458
459 static void
460 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
461     OVS_REQUIRES(dev->mutex)
462 {
463     dev->change_seq++;
464     if (!dev->change_seq) {
465         dev->change_seq++;
466     }
467 }
468
469 static void
470 netdev_tunnel_get_port(struct unixctl_conn *conn,
471                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
472 {
473     struct netdev_tunnel *tunnel_dev;
474     char buf[6];
475
476     ovs_mutex_lock(&tunnel_netdevs_mutex);
477     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
478     if (!tunnel_dev) {
479         unixctl_command_reply_error(conn, "no such tunnel netdev");
480         goto out;
481     }
482
483     ovs_mutex_lock(&tunnel_dev->mutex);
484     sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
485     ovs_mutex_unlock(&tunnel_dev->mutex);
486
487     unixctl_command_reply(conn, buf);
488 out:
489     ovs_mutex_unlock(&tunnel_netdevs_mutex);
490 }
491
492 static void
493 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
494                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
495 {
496     struct netdev_tunnel *tunnel_dev;
497     char buf[128];
498
499     ovs_mutex_lock(&tunnel_netdevs_mutex);
500     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
501     if (!tunnel_dev) {
502         unixctl_command_reply_error(conn, "no such tunnel netdev");
503         goto out;
504     }
505
506     ovs_mutex_lock(&tunnel_dev->mutex);
507     sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
508     ovs_mutex_unlock(&tunnel_dev->mutex);
509     unixctl_command_reply(conn, buf);
510 out:
511     ovs_mutex_unlock(&tunnel_netdevs_mutex);
512 }
513
514 static void
515 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
516                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
517 {
518     struct netdev_tunnel *tunnel_dev;
519     char buf[128];
520
521     ovs_mutex_lock(&tunnel_netdevs_mutex);
522     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
523     if (!tunnel_dev) {
524         unixctl_command_reply_error(conn, "no such tunnel netdev");
525         goto out;
526     }
527
528     sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
529     unixctl_command_reply(conn, buf);
530 out:
531     ovs_mutex_unlock(&tunnel_netdevs_mutex);
532 }
533
534
535 static int
536 netdev_tunnel_init(void)
537 {
538     unixctl_command_register("netdev-tunnel/get-port", "NAME",
539                              1, 1, netdev_tunnel_get_port, NULL);
540     unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
541                              1, 1, netdev_tunnel_get_tx_bytes, NULL);
542     unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
543                              1, 1, netdev_tunnel_get_rx_bytes, NULL);
544     return 0;
545 }
546
547 static void
548 netdev_tunnel_run(void)
549 {
550 }
551
552 static void
553 netdev_tunnel_wait(void)
554 {
555 }
556
557 const struct netdev_class netdev_tunnel_class = {
558     "tunnel",
559     netdev_tunnel_init,    
560     netdev_tunnel_run,      
561     netdev_tunnel_wait,   
562
563     netdev_tunnel_alloc,
564     netdev_tunnel_construct,
565     netdev_tunnel_destruct,
566     netdev_tunnel_dealloc,
567     netdev_tunnel_get_config,
568     netdev_tunnel_set_config, 
569     NULL,                                   /* get_tunnel_config */
570
571     netdev_tunnel_send, 
572     netdev_tunnel_send_wait,  
573
574     netdev_tunnel_set_etheraddr,
575     netdev_tunnel_get_etheraddr,
576     NULL,                                   /* get_mtu */
577     NULL,                                   /* set_mtu */
578     NULL,                       /* get_ifindex */
579     NULL,                                   /* get_carrier */
580     NULL,                       /* get_carrier_resets */
581     NULL,                       /* get_miimon */
582     netdev_tunnel_get_stats,
583     netdev_tunnel_set_stats,
584
585     NULL,                       /* get_features */
586     NULL,                       /* set_advertisements */
587
588     NULL,                       /* set_policing */
589     NULL,                       /* get_qos_types */
590     NULL,                       /* get_qos_capabilities */
591     NULL,                       /* get_qos */
592     NULL,                       /* set_qos */
593     NULL,                       /* get_queue */
594     NULL,                       /* set_queue */
595     NULL,                       /* delete_queue */
596     NULL,                       /* get_queue_stats */
597     NULL,                       /* queue_dump_start */
598     NULL,                       /* queue_dump_next */
599     NULL,                       /* queue_dump_done */
600     NULL,                       /* dump_queue_stats */
601
602     NULL,                       /* get_in4 */
603     NULL,                       /* set_in4 */
604     NULL,                       /* get_in6 */
605     NULL,                       /* add_router */
606     NULL,                       /* get_next_hop */
607     NULL,                       /* get_status */
608     NULL,                       /* arp_lookup */
609
610     netdev_tunnel_update_flags,
611
612     netdev_tunnel_rx_alloc,
613     netdev_tunnel_rx_construct,
614     netdev_tunnel_rx_destruct,
615     netdev_tunnel_rx_dealloc,
616     netdev_tunnel_rx_recv,
617     netdev_tunnel_rx_wait,
618     netdev_tunnel_rx_drain,
619 };