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