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