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