replaced strerror to ovs_strerror in pltap&tunnel
[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     uint8_t hwaddr[ETH_ADDR_LEN];
44     struct netdev_stats stats;
45     enum netdev_flags flags;
46     int sockfd;
47     struct sockaddr_in local_addr;
48     struct sockaddr_in remote_addr;
49     bool valid_remote_ip;
50     bool valid_remote_port;
51     bool connected;
52     unsigned int change_seq;
53 };
54
55 struct netdev_rx_tunnel {
56     struct netdev_rx up;
57     int fd;
58 };
59
60 static const struct netdev_rx_class netdev_rx_tunnel_class;
61
62 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
63
64 static struct shash tunnel_netdevs = SHASH_INITIALIZER(&tunnel_netdevs);
65
66 static int netdev_tunnel_create(const struct netdev_class *, const char *,
67                                struct netdev **);
68 static void netdev_tunnel_update_seq(struct netdev_tunnel *);
69
70 static bool
71 is_netdev_tunnel_class(const struct netdev_class *class)
72 {
73     return class->create == netdev_tunnel_create;
74 }
75
76 static struct netdev_tunnel *
77 netdev_tunnel_cast(const struct netdev *netdev)
78 {
79     ovs_assert(is_netdev_tunnel_class(netdev_get_class(netdev)));
80     return CONTAINER_OF(netdev, struct netdev_tunnel, up);
81 }
82
83 static struct netdev_rx_tunnel *
84 netdev_rx_tunnel_cast(const struct netdev_rx *rx)
85 {
86     netdev_rx_assert_class(rx, &netdev_rx_tunnel_class);
87     return CONTAINER_OF(rx, struct netdev_rx_tunnel, up);
88 }
89
90 static int
91 netdev_tunnel_create(const struct netdev_class *class, const char *name,
92                     struct netdev **netdevp)
93 {
94     static unsigned int n = 0;
95     struct netdev_tunnel *netdev;
96     int error;
97
98     netdev = xzalloc(sizeof *netdev);
99     netdev_init(&netdev->up, name, class);
100     netdev->hwaddr[0] = 0xfe;
101     netdev->hwaddr[1] = 0xff;
102     netdev->hwaddr[2] = 0xff;
103     netdev->hwaddr[3] = n >> 16;
104     netdev->hwaddr[4] = n >> 8;
105     netdev->hwaddr[5] = n;
106     netdev->flags = 0;
107     netdev->change_seq = 1;
108     memset(&netdev->remote_addr, 0, sizeof(netdev->remote_addr));
109     netdev->valid_remote_ip = false;
110     netdev->valid_remote_port = false;
111     netdev->connected = false;
112
113
114     netdev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev->local_addr, 0);
115     if (netdev->sockfd < 0) {
116         error = netdev->sockfd;
117         goto error;
118     }
119
120
121     shash_add(&tunnel_netdevs, name, netdev);
122
123     n++;
124
125     *netdevp = &netdev->up;
126
127     VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", name, netdev->sockfd, netdev->local_addr.sin_port);
128
129     return 0;
130
131 error:
132     free(netdev);
133     return error;
134 }
135
136 static void
137 netdev_tunnel_destroy(struct netdev *netdev_)
138 {
139     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
140
141     if (netdev->sockfd != -1)
142         close(netdev->sockfd);
143
144     shash_find_and_delete(&tunnel_netdevs,
145                           netdev_get_name(netdev_));
146     free(netdev);
147 }
148
149 static int
150 netdev_tunnel_get_config(const struct netdev *dev_, struct smap *args)
151 {
152     struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
153
154     if (netdev->valid_remote_ip)
155         smap_add_format(args, "remote_ip", IP_FMT,
156                 IP_ARGS(netdev->remote_addr.sin_addr.s_addr));
157     if (netdev->valid_remote_port)
158         smap_add_format(args, "remote_port", "%"PRIu16,
159                 ntohs(netdev->remote_addr.sin_port));
160     return 0;
161 }
162
163 static int
164 netdev_tunnel_connect(struct netdev_tunnel *dev)
165 {
166     if (dev->sockfd < 0)
167         return EBADF;
168     if (!dev->valid_remote_ip || !dev->valid_remote_port)
169         return 0;
170     dev->remote_addr.sin_family = AF_INET;
171     if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
172         return errno;
173     }
174     dev->connected = true;
175     netdev_tunnel_update_seq(dev);
176     VLOG_DBG("%s: connected to (%s, %d)", netdev_get_name(&dev->up),
177         inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
178     return 0;
179 }
180
181 static int
182 netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args)
183 {
184     struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
185     struct shash_node *node;
186
187     VLOG_DBG("tunnel_set_config(%s)", netdev_get_name(dev_));
188     SMAP_FOR_EACH(node, args) {
189         VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
190         if (!strcmp(node->name, "remote_ip")) {
191             struct in_addr addr;
192             if (lookup_ip(node->data, &addr)) {
193                 VLOG_WARN("%s: bad 'remote_ip'", node->name);
194             } else {
195                 netdev->remote_addr.sin_addr = addr;
196                 netdev->valid_remote_ip = true;
197             }
198         } else if (!strcmp(node->name, "remote_port")) {
199             netdev->remote_addr.sin_port = htons(atoi(node->data));
200             netdev->valid_remote_port = true;
201         } else {
202             VLOG_WARN("%s: unknown argument '%s'", 
203                 netdev_get_name(dev_), node->name);
204         }
205     }
206     return netdev_tunnel_connect(netdev);        
207 }
208
209 static int
210 netdev_tunnel_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
211 {   
212     struct netdev_tunnel *netdev =
213         netdev_tunnel_cast(netdev_);
214     struct netdev_rx_tunnel *rx;
215     rx = xmalloc(sizeof *rx);
216     netdev_rx_init(&rx->up, netdev_, &netdev_rx_tunnel_class);
217     rx->fd = netdev->sockfd;
218     *rxp = &rx->up;
219     return 0;
220 }
221
222 static void
223 netdev_rx_tunnel_destroy(struct netdev_rx *rx_)
224 {
225     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
226     free(rx);
227 }
228
229 static int
230 netdev_rx_tunnel_recv(struct netdev_rx *rx_, void *buffer, size_t size)
231 {
232     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
233     struct netdev_tunnel *netdev =
234         netdev_tunnel_cast(rx_->netdev);
235     if (!netdev->connected)
236         return -EAGAIN;
237     for (;;) {
238         ssize_t retval;
239         retval = recv(rx->fd, buffer, size, MSG_TRUNC);
240             VLOG_DBG("%s: recv(%"PRIxPTR", %zu, MSG_TRUNC) = %zd",
241                     netdev_rx_get_name(rx_), (uintptr_t)buffer, size, retval);
242         if (retval >= 0) {
243             netdev->stats.rx_packets++;
244             netdev->stats.rx_bytes += retval;
245             if (retval <= size) {
246                     return retval;
247             } else {
248                 netdev->stats.rx_errors++;
249                 netdev->stats.rx_length_errors++;
250                 return -EMSGSIZE;
251             }
252         } else if (errno != EINTR) {
253             if (errno != EAGAIN) {
254                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
255                     netdev_rx_get_name(rx_), ovs_strerror(errno));
256                     netdev->stats.rx_errors++;
257             }
258             return -errno;
259         }
260     }
261 }
262
263 static void
264 netdev_rx_tunnel_wait(struct netdev_rx *rx_)
265 {
266     struct netdev_rx_tunnel *rx = 
267         netdev_rx_tunnel_cast(rx_);
268     if (rx->fd >= 0) {
269         poll_fd_wait(rx->fd, POLLIN);
270     }
271 }
272
273 static int
274 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
275 {
276     struct netdev_tunnel *dev = 
277         netdev_tunnel_cast(netdev_);
278     if (!dev->connected)
279         return EAGAIN;
280     for (;;) {
281         ssize_t retval;
282         retval = send(dev->sockfd, buffer, size, 0);
283         VLOG_DBG("%s: send(%"PRIxPTR", %zu) = %zd",
284                  netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
285         if (retval >= 0) {
286             dev->stats.tx_packets++;
287             dev->stats.tx_bytes += retval;
288             if (retval != size) {
289                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
290                              "%zu) on %s", retval, size, netdev_get_name(netdev_));
291                 dev->stats.tx_errors++;
292             }
293             return 0;
294         } else if (errno != EINTR) {
295             if (errno != EAGAIN) {
296                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
297                     netdev_get_name(netdev_), ovs_strerror(errno));
298                 dev->stats.tx_errors++;
299             }
300             return errno;
301         }
302     }
303 }
304
305 static void
306 netdev_tunnel_send_wait(struct netdev *netdev_)
307 {
308     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
309     if (dev->sockfd >= 0) {
310         poll_fd_wait(dev->sockfd, POLLOUT);
311     }
312 }
313
314 static int
315 netdev_rx_tunnel_drain(struct netdev_rx *rx_)
316 {
317     struct netdev_tunnel *netdev =
318         netdev_tunnel_cast(rx_->netdev);
319     struct netdev_rx_tunnel *rx = 
320         netdev_rx_tunnel_cast(rx_);
321     char buffer[128];
322     int error;
323
324     if (!netdev->connected)
325         return 0;
326     for (;;) {
327         error = recv(rx->fd, buffer, 128, MSG_TRUNC);
328         if (error) {
329             if (error == -EAGAIN)
330                 break;
331             else if (error != -EMSGSIZE)
332                 return error;
333         }
334     }
335     return 0;
336 }
337
338 static int
339 netdev_tunnel_set_etheraddr(struct netdev *netdev,
340                            const uint8_t mac[ETH_ADDR_LEN])
341 {
342     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
343
344     if (!eth_addr_equals(dev->hwaddr, mac)) {
345         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
346         netdev_tunnel_update_seq(dev);
347     }
348
349     return 0;
350 }
351
352 static int
353 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
354                            uint8_t mac[ETH_ADDR_LEN])
355 {
356     const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
357
358     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
359     return 0;
360 }
361
362
363 static int
364 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
365 {
366     const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
367
368     *stats = dev->stats;
369     return 0;
370 }
371
372 static int
373 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
374 {
375     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
376
377     dev->stats = *stats;
378     return 0;
379 }
380
381 static int
382 netdev_tunnel_update_flags(struct netdev *dev_,
383                           enum netdev_flags off, enum netdev_flags on,
384                           enum netdev_flags *old_flagsp)
385 {
386     struct netdev_tunnel *netdev =
387         netdev_tunnel_cast(dev_);
388
389     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
390         return EINVAL;
391     }
392
393     // XXX should we actually do something with these flags?
394     *old_flagsp = netdev->flags;
395     netdev->flags |= on;
396     netdev->flags &= ~off;
397     if (*old_flagsp != netdev->flags) {
398         netdev_tunnel_update_seq(netdev);
399     }
400     return 0;
401 }
402
403 static unsigned int
404 netdev_tunnel_change_seq(const struct netdev *netdev)
405 {
406     return netdev_tunnel_cast(netdev)->change_seq;
407 }
408 \f
409 /* Helper functions. */
410
411 static void
412 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
413 {
414     dev->change_seq++;
415     if (!dev->change_seq) {
416         dev->change_seq++;
417     }
418 }
419
420 static void
421 netdev_tunnel_get_port(struct unixctl_conn *conn,
422                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
423 {
424     struct netdev_tunnel *tunnel_dev;
425     char buf[6];
426
427     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
428     if (!tunnel_dev) {
429         unixctl_command_reply_error(conn, "no such tunnel netdev");
430         return;
431     }
432
433     sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
434     unixctl_command_reply(conn, buf);
435 }
436
437 static void
438 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
439                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
440 {
441     struct netdev_tunnel *tunnel_dev;
442     char buf[128];
443
444     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
445     if (!tunnel_dev) {
446         unixctl_command_reply_error(conn, "no such tunnel netdev");
447         return;
448     }
449
450     sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
451     unixctl_command_reply(conn, buf);
452 }
453
454 static void
455 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
456                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
457 {
458     struct netdev_tunnel *tunnel_dev;
459     char buf[128];
460
461     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
462     if (!tunnel_dev) {
463         unixctl_command_reply_error(conn, "no such tunnel netdev");
464         return;
465     }
466
467     sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
468     unixctl_command_reply(conn, buf);
469 }
470
471
472 static int
473 netdev_tunnel_init(void)
474 {
475     unixctl_command_register("netdev-tunnel/get-port", "NAME",
476                              1, 1, netdev_tunnel_get_port, NULL);
477     unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
478                              1, 1, netdev_tunnel_get_tx_bytes, NULL);
479     unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
480                              1, 1, netdev_tunnel_get_rx_bytes, NULL);
481     return 0;
482 }
483
484 const struct netdev_class netdev_tunnel_class = {
485     "tunnel",
486     netdev_tunnel_init,         /* init */
487     NULL,                       /* run */
488     NULL,                       /* wait */
489
490     netdev_tunnel_create,
491     netdev_tunnel_destroy,
492     netdev_tunnel_get_config,
493     netdev_tunnel_set_config, 
494     NULL,                                   /* get_tunnel_config */
495
496     netdev_tunnel_rx_open,
497
498     netdev_tunnel_send, 
499     netdev_tunnel_send_wait,  
500
501     netdev_tunnel_set_etheraddr,
502     netdev_tunnel_get_etheraddr,
503     NULL,                                   /* get_mtu */
504     NULL,                                   /* set_mtu */
505     NULL,                       /* get_ifindex */
506     NULL,                                   /* get_carrier */
507     NULL,                       /* get_carrier_resets */
508     NULL,                       /* get_miimon */
509     netdev_tunnel_get_stats,
510     netdev_tunnel_set_stats,
511
512     NULL,                       /* get_features */
513     NULL,                       /* set_advertisements */
514
515     NULL,                       /* set_policing */
516     NULL,                       /* get_qos_types */
517     NULL,                       /* get_qos_capabilities */
518     NULL,                       /* get_qos */
519     NULL,                       /* set_qos */
520     NULL,                       /* get_queue */
521     NULL,                       /* set_queue */
522     NULL,                       /* delete_queue */
523     NULL,                       /* get_queue_stats */
524     NULL,                       /* dump_queues */
525     NULL,                       /* dump_queue_stats */
526
527     NULL,                       /* get_in4 */
528     NULL,                       /* set_in4 */
529     NULL,                       /* get_in6 */
530     NULL,                       /* add_router */
531     NULL,                       /* get_next_hop */
532     NULL,                       /* get_drv_info */
533     NULL,                       /* arp_lookup */
534
535     netdev_tunnel_update_flags,
536
537     netdev_tunnel_change_seq
538 };
539
540
541 static const struct netdev_rx_class netdev_rx_tunnel_class = {
542     netdev_rx_tunnel_destroy,
543     netdev_rx_tunnel_recv,
544     netdev_rx_tunnel_wait,
545     netdev_rx_tunnel_drain,
546 };
547