updated pltap&tunnel to new netdev interface
[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_, void *buffer, size_t size)
268 {
269     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
270     struct netdev_tunnel *netdev =
271         netdev_tunnel_cast(rx_->netdev);
272     if (!netdev->connected)
273         return -EAGAIN;
274     for (;;) {
275         ssize_t retval;
276         retval = recv(rx->fd, buffer, size, MSG_TRUNC);
277             VLOG_DBG("%s: recv(%"PRIxPTR", %zu, MSG_TRUNC) = %zd",
278                     netdev_rx_get_name(rx_), (uintptr_t)buffer, size, retval);
279         if (retval >= 0) {
280             netdev->stats.rx_packets++;
281             netdev->stats.rx_bytes += retval;
282             if (retval <= size) {
283                     return retval;
284             } else {
285                 netdev->stats.rx_errors++;
286                 netdev->stats.rx_length_errors++;
287                 return -EMSGSIZE;
288             }
289         } else if (errno != EINTR) {
290             if (errno != EAGAIN) {
291                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
292                     netdev_rx_get_name(rx_), ovs_strerror(errno));
293                     netdev->stats.rx_errors++;
294             }
295             return -errno;
296         }
297     }
298 }
299
300 static void
301 netdev_tunnel_rx_wait(struct netdev_rx *rx_)
302 {
303     struct netdev_rx_tunnel *rx = 
304         netdev_rx_tunnel_cast(rx_);
305     if (rx->fd >= 0) {
306         poll_fd_wait(rx->fd, POLLIN);
307     }
308 }
309
310 static int
311 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
312 {
313     struct netdev_tunnel *dev = 
314         netdev_tunnel_cast(netdev_);
315     if (!dev->connected)
316         return EAGAIN;
317     for (;;) {
318         ssize_t retval;
319         retval = send(dev->sockfd, buffer, size, 0);
320         VLOG_DBG("%s: send(%"PRIxPTR", %zu) = %zd",
321                  netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
322         if (retval >= 0) {
323             dev->stats.tx_packets++;
324             dev->stats.tx_bytes += retval;
325             if (retval != size) {
326                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
327                              "%zu) on %s", retval, size, netdev_get_name(netdev_));
328                 dev->stats.tx_errors++;
329             }
330             return 0;
331         } else if (errno != EINTR) {
332             if (errno != EAGAIN) {
333                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
334                     netdev_get_name(netdev_), ovs_strerror(errno));
335                 dev->stats.tx_errors++;
336             }
337             return errno;
338         }
339     }
340 }
341
342 static void
343 netdev_tunnel_send_wait(struct netdev *netdev_)
344 {
345     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
346     if (dev->sockfd >= 0) {
347         poll_fd_wait(dev->sockfd, POLLOUT);
348     }
349 }
350
351 static int
352 netdev_tunnel_rx_drain(struct netdev_rx *rx_)
353 {
354     struct netdev_tunnel *netdev =
355         netdev_tunnel_cast(rx_->netdev);
356     struct netdev_rx_tunnel *rx = 
357         netdev_rx_tunnel_cast(rx_);
358     char buffer[128];
359     int error;
360
361     if (!netdev->connected)
362         return 0;
363     for (;;) {
364         error = recv(rx->fd, buffer, 128, MSG_TRUNC);
365         if (error) {
366             if (error == -EAGAIN)
367                 break;
368             else if (error != -EMSGSIZE)
369                 return error;
370         }
371     }
372     return 0;
373 }
374
375 static int
376 netdev_tunnel_set_etheraddr(struct netdev *netdev,
377                            const uint8_t mac[ETH_ADDR_LEN])
378 {
379     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
380
381     ovs_mutex_lock(&dev->mutex);
382     if (!eth_addr_equals(dev->hwaddr, mac)) {
383         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
384         netdev_tunnel_update_seq(dev);
385     }
386     ovs_mutex_unlock(&dev->mutex);
387
388     return 0;
389 }
390
391 static int
392 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
393                            uint8_t mac[ETH_ADDR_LEN])
394 {
395     const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
396
397     ovs_mutex_lock(&dev->mutex);
398     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
399     ovs_mutex_unlock(&dev->mutex);
400     return 0;
401 }
402
403
404 static int
405 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
406 {
407     const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
408
409     ovs_mutex_lock(&dev->mutex);
410     *stats = dev->stats;
411     ovs_mutex_unlock(&dev->mutex);
412     return 0;
413 }
414
415 static int
416 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
417 {
418     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
419
420     ovs_mutex_lock(&dev->mutex);
421     dev->stats = *stats;
422     ovs_mutex_unlock(&dev->mutex);
423     return 0;
424 }
425
426 static int
427 netdev_tunnel_update_flags(struct netdev *dev_,
428                           enum netdev_flags off, enum netdev_flags on,
429                           enum netdev_flags *old_flagsp)
430 {
431     struct netdev_tunnel *netdev =
432         netdev_tunnel_cast(dev_);
433     int error = 0;
434
435     ovs_mutex_lock(&netdev->mutex);
436     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
437         error = EINVAL;
438         goto out;
439     }
440
441     // XXX should we actually do something with these flags?
442     *old_flagsp = netdev->flags;
443     netdev->flags |= on;
444     netdev->flags &= ~off;
445     if (*old_flagsp != netdev->flags) {
446         netdev_tunnel_update_seq(netdev);
447     }
448
449 out:
450     ovs_mutex_unlock(&netdev->mutex);
451     return error;
452 }
453
454 static unsigned int
455 netdev_tunnel_change_seq(const struct netdev *netdev_)
456 {
457     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
458     unsigned int change_seq;
459
460
461     ovs_mutex_lock(&netdev->mutex);
462     change_seq = netdev->change_seq;
463     ovs_mutex_unlock(&netdev->mutex);
464     return change_seq;
465 }
466 \f
467 /* Helper functions. */
468
469 static void
470 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
471     OVS_REQUIRES(dev->mutex)
472 {
473     dev->change_seq++;
474     if (!dev->change_seq) {
475         dev->change_seq++;
476     }
477 }
478
479 static void
480 netdev_tunnel_get_port(struct unixctl_conn *conn,
481                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
482 {
483     struct netdev_tunnel *tunnel_dev;
484     char buf[6];
485
486     ovs_mutex_lock(&tunnel_netdevs_mutex);
487     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
488     if (!tunnel_dev) {
489         unixctl_command_reply_error(conn, "no such tunnel netdev");
490         goto out;
491     }
492
493     ovs_mutex_lock(&tunnel_dev->mutex);
494     sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
495     ovs_mutex_unlock(&tunnel_dev->mutex);
496
497     unixctl_command_reply(conn, buf);
498 out:
499     ovs_mutex_unlock(&tunnel_netdevs_mutex);
500 }
501
502 static void
503 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
504                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
505 {
506     struct netdev_tunnel *tunnel_dev;
507     char buf[128];
508
509     ovs_mutex_lock(&tunnel_netdevs_mutex);
510     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
511     if (!tunnel_dev) {
512         unixctl_command_reply_error(conn, "no such tunnel netdev");
513         goto out;
514     }
515
516     ovs_mutex_lock(&tunnel_dev->mutex);
517     sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
518     ovs_mutex_unlock(&tunnel_dev->mutex);
519     unixctl_command_reply(conn, buf);
520 out:
521     ovs_mutex_unlock(&tunnel_netdevs_mutex);
522 }
523
524 static void
525 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
526                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
527 {
528     struct netdev_tunnel *tunnel_dev;
529     char buf[128];
530
531     ovs_mutex_lock(&tunnel_netdevs_mutex);
532     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
533     if (!tunnel_dev) {
534         unixctl_command_reply_error(conn, "no such tunnel netdev");
535         goto out;
536     }
537
538     sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
539     unixctl_command_reply(conn, buf);
540 out:
541     ovs_mutex_unlock(&tunnel_netdevs_mutex);
542 }
543
544
545 static int
546 netdev_tunnel_init(void)
547 {
548     unixctl_command_register("netdev-tunnel/get-port", "NAME",
549                              1, 1, netdev_tunnel_get_port, NULL);
550     unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
551                              1, 1, netdev_tunnel_get_tx_bytes, NULL);
552     unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
553                              1, 1, netdev_tunnel_get_rx_bytes, NULL);
554     return 0;
555 }
556
557 static void
558 netdev_tunnel_run(void)
559 {
560 }
561
562 static void
563 netdev_tunnel_wait(void)
564 {
565 }
566
567 const struct netdev_class netdev_tunnel_class = {
568     "tunnel",
569     netdev_tunnel_init,    
570     netdev_tunnel_run,      
571     netdev_tunnel_wait,   
572
573     netdev_tunnel_alloc,
574     netdev_tunnel_construct,
575     netdev_tunnel_destruct,
576     netdev_tunnel_dealloc,
577     netdev_tunnel_get_config,
578     netdev_tunnel_set_config, 
579     NULL,                                   /* get_tunnel_config */
580
581     netdev_tunnel_send, 
582     netdev_tunnel_send_wait,  
583
584     netdev_tunnel_set_etheraddr,
585     netdev_tunnel_get_etheraddr,
586     NULL,                                   /* get_mtu */
587     NULL,                                   /* set_mtu */
588     NULL,                       /* get_ifindex */
589     NULL,                                   /* get_carrier */
590     NULL,                       /* get_carrier_resets */
591     NULL,                       /* get_miimon */
592     netdev_tunnel_get_stats,
593     netdev_tunnel_set_stats,
594
595     NULL,                       /* get_features */
596     NULL,                       /* set_advertisements */
597
598     NULL,                       /* set_policing */
599     NULL,                       /* get_qos_types */
600     NULL,                       /* get_qos_capabilities */
601     NULL,                       /* get_qos */
602     NULL,                       /* set_qos */
603     NULL,                       /* get_queue */
604     NULL,                       /* set_queue */
605     NULL,                       /* delete_queue */
606     NULL,                       /* get_queue_stats */
607     NULL,                       /* queue_dump_start */
608     NULL,                       /* queue_dump_next */
609     NULL,                       /* queue_dump_done */
610     NULL,                       /* dump_queue_stats */
611
612     NULL,                       /* get_in4 */
613     NULL,                       /* set_in4 */
614     NULL,                       /* get_in6 */
615     NULL,                       /* add_router */
616     NULL,                       /* get_next_hop */
617     NULL,                       /* get_drv_info */
618     NULL,                       /* arp_lookup */
619
620     netdev_tunnel_update_flags,
621
622     netdev_tunnel_change_seq,
623
624     netdev_tunnel_rx_alloc,
625     netdev_tunnel_rx_construct,
626     netdev_tunnel_rx_destruct,
627     netdev_tunnel_rx_dealloc,
628     netdev_tunnel_rx_recv,
629     netdev_tunnel_rx_wait,
630     netdev_tunnel_rx_drain,
631 };