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