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