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