better change_seq
[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     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_tunnel {
56     struct netdev netdev;
57 } ;
58
59 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
60
61 static struct shash tunnel_netdev_devs = SHASH_INITIALIZER(&tunnel_netdev_devs);
62
63 static int netdev_tunnel_create(const struct netdev_class *, const char *,
64                                struct netdev_dev **);
65 static void netdev_tunnel_update_seq(struct netdev_dev_tunnel *);
66
67 static bool
68 is_tunnel_class(const struct netdev_class *class)
69 {
70     return class->create == netdev_tunnel_create;
71 }
72
73 static struct netdev_dev_tunnel *
74 netdev_dev_tunnel_cast(const struct netdev_dev *netdev_dev)
75 {
76     assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
77     return CONTAINER_OF(netdev_dev, struct netdev_dev_tunnel, netdev_dev);
78 }
79
80 static struct netdev_tunnel *
81 netdev_tunnel_cast(const struct netdev *netdev)
82 {
83     struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
84     assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
85     return CONTAINER_OF(netdev, struct netdev_tunnel, netdev);
86 }
87
88 static int
89 netdev_tunnel_create(const struct netdev_class *class, const char *name,
90                     struct netdev_dev **netdev_devp)
91 {
92     static unsigned int n = 0xaa550000;
93     struct netdev_dev_tunnel *netdev_dev;
94     int error;
95
96     netdev_dev = xzalloc(sizeof *netdev_dev);
97     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
98     netdev_dev->hwaddr[0] = 0x55;
99     netdev_dev->hwaddr[1] = 0xaa;
100     netdev_dev->hwaddr[2] = n >> 24;
101     netdev_dev->hwaddr[3] = n >> 16;
102     netdev_dev->hwaddr[4] = n >> 8;
103     netdev_dev->hwaddr[5] = n;
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     netdev_tunnel_update_seq(dev);
194     VLOG_DBG("%s: connected to (%s, %d)", netdev_dev_get_name(&dev->netdev_dev),
195         inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
196     return 0;
197 }
198
199 static int
200 netdev_tunnel_set_config(struct netdev_dev *dev_, const struct shash *args)
201 {
202     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
203     struct shash_node *node;
204
205     VLOG_DBG("tunnel_set_config(%s)", netdev_dev_get_name(dev_));
206     SHASH_FOR_EACH(node, args) {
207         VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
208         if (!strcmp(node->name, "remote_ip")) {
209             struct in_addr addr;
210             if (lookup_ip(node->data, &addr)) {
211                 VLOG_WARN("%s: bad 'remote_ip'", node->name);
212             } else {
213                 netdev_dev->remote_addr.sin_addr = addr;
214                 netdev_dev->valid_remote_ip = true;
215             }
216         } else if (!strcmp(node->name, "remote_port")) {
217             netdev_dev->remote_addr.sin_port = htons(atoi(node->data));
218             netdev_dev->valid_remote_port = true;
219         } else {
220             VLOG_WARN("%s: unknown argument '%s'", 
221                 netdev_dev_get_name(dev_), node->name);
222         }
223     }
224     return netdev_tunnel_connect(netdev_dev);        
225 }
226
227 static int
228 netdev_tunnel_listen(struct netdev *netdev_)
229 {
230     return 0;
231 }
232
233 static int
234 netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
235 {
236     struct netdev_dev_tunnel *dev = 
237         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
238     if (!dev->connected)
239         return -EAGAIN;
240     for (;;) {
241         ssize_t retval;
242         retval = recv(dev->sockfd, buffer, size, MSG_TRUNC);
243         VLOG_DBG("%s: recv(%x, %d, MSG_TRUNC) = %d",
244                         netdev_get_name(netdev_), buffer, size, retval);
245         if (retval >= 0) {
246             return retval <= size ? retval : -EMSGSIZE;
247         } else if (errno != EINTR) {
248             if (errno != EAGAIN) {
249                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
250                     netdev_get_name(netdev_), strerror(errno));
251             }
252             return -errno;
253         }
254     }
255 }
256
257 static void
258 netdev_tunnel_recv_wait(struct netdev *netdev_)
259 {
260     struct netdev_dev_tunnel *dev = 
261         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
262     if (dev->sockfd >= 0) {
263         poll_fd_wait(dev->sockfd, POLLIN);
264     }
265 }
266
267 static int
268 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
269 {
270     struct netdev_dev_tunnel *dev = 
271         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
272     if (!dev->connected)
273         return EAGAIN;
274     for (;;) {
275         ssize_t retval;
276         retval = send(dev->sockfd, buffer, size, 0);
277         VLOG_DBG("%s: send(%x, %d) = %d", netdev_get_name(netdev_), buffer, size, retval);
278         if (retval >= 0) {
279             if (retval != size) {
280                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
281                              "%zu) on %s", retval, size, netdev_get_name(netdev_));
282             }
283             return 0;
284         } else if (errno != EINTR) {
285             if (errno != EAGAIN) {
286                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
287                     netdev_get_name(netdev_), strerror(errno));
288             }
289             return errno;
290         }
291     }
292 }
293
294 static void
295 netdev_tunnel_send_wait(struct netdev *netdev_)
296 {
297     struct netdev_dev_tunnel *dev = 
298         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
299     if (dev->sockfd >= 0) {
300         poll_fd_wait(dev->sockfd, POLLOUT);
301     }
302 }
303
304 static int
305 netdev_tunnel_drain(struct netdev *netdev_)
306 {
307     struct netdev_dev_tunnel *dev = 
308         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
309     char buffer[128];
310     int error;
311
312     if (!dev->connected)
313         return 0;
314     for (;;) {
315         error = recv(dev->sockfd, buffer, 128, MSG_TRUNC);
316         if (error) {
317             if (error == -EAGAIN)
318                 break;
319             else if (error != -EMSGSIZE)
320                 return error;
321         }
322     }
323     return 0;
324 }
325
326 static int
327 netdev_tunnel_set_etheraddr(struct netdev *netdev,
328                            const uint8_t mac[ETH_ADDR_LEN])
329 {
330     struct netdev_dev_tunnel *dev =
331         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
332
333     if (!eth_addr_equals(dev->hwaddr, mac)) {
334         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
335         netdev_tunnel_update_seq(dev);
336     }
337
338     return 0;
339 }
340
341 static int
342 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
343                            uint8_t mac[ETH_ADDR_LEN])
344 {
345     const struct netdev_dev_tunnel *dev =
346         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
347
348     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
349     return 0;
350 }
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     // XXX should we actually do something with this flags?
386     *old_flagsp = dev->flags;
387     dev->flags |= on;
388     dev->flags &= ~off;
389     if (*old_flagsp != dev->flags) {
390         netdev_tunnel_update_seq(dev);
391     }
392     return 0;
393 }
394
395 static unsigned int
396 netdev_tunnel_change_seq(const struct netdev *netdev)
397 {
398     return netdev_dev_tunnel_cast(netdev_get_dev(netdev))->change_seq;
399 }
400 \f
401 /* Helper functions. */
402
403 static void
404 netdev_tunnel_update_seq(struct netdev_dev_tunnel *dev)
405 {
406     dev->change_seq++;
407     if (!dev->change_seq) {
408         dev->change_seq++;
409     }
410 }
411
412 static void
413 netdev_tunnel_get_port(struct unixctl_conn *conn,
414                      int argc, const char *argv[], void *aux OVS_UNUSED)
415 {
416     struct netdev_dev_tunnel *tunnel_dev;
417     uint16_t port;
418     char buf[6];
419
420     tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
421     if (!tunnel_dev) {
422         unixctl_command_reply_error(conn, "no such tunnel netdev");
423         return;
424     }
425
426     sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
427     unixctl_command_reply(conn, buf);
428 }
429
430
431 static int
432 netdev_tunnel_init(void)
433 {
434     unixctl_command_register("netdev-tunnel/get-port", "NAME",
435                              1, 1, netdev_tunnel_get_port, NULL);
436     return 0;
437 }
438
439 const struct netdev_class netdev_tunnel_class = {
440     "tunnel",
441     netdev_tunnel_init,         /* init */
442     NULL,                       /* run */
443     NULL,                       /* wait */
444
445     netdev_tunnel_create,
446     netdev_tunnel_destroy,
447     netdev_tunnel_get_config,
448     netdev_tunnel_set_config, 
449
450     netdev_tunnel_open,
451     netdev_tunnel_close,
452
453     netdev_tunnel_listen,
454     netdev_tunnel_recv,
455     netdev_tunnel_recv_wait,
456     netdev_tunnel_drain,
457
458     netdev_tunnel_send, 
459     netdev_tunnel_send_wait,  
460
461     netdev_tunnel_set_etheraddr,
462     netdev_tunnel_get_etheraddr,
463     NULL,                       /* get_mtu */
464     NULL,                       /* set_mtu */
465     NULL,                       /* get_ifindex */
466     NULL,                       /* get_carrier */
467     NULL,                       /* get_carrier_resets */
468     NULL,                       /* get_miimon */
469     netdev_tunnel_get_stats,
470     netdev_tunnel_set_stats,
471
472     NULL,                       /* get_features */
473     NULL,                       /* set_advertisements */
474
475     NULL,                       /* set_policing */
476     NULL,                       /* get_qos_types */
477     NULL,                       /* get_qos_capabilities */
478     NULL,                       /* get_qos */
479     NULL,                       /* set_qos */
480     NULL,                       /* get_queue */
481     NULL,                       /* set_queue */
482     NULL,                       /* delete_queue */
483     NULL,                       /* get_queue_stats */
484     NULL,                       /* dump_queues */
485     NULL,                       /* dump_queue_stats */
486
487     NULL,                       /* get_in4 */
488     NULL,                       /* set_in4 */
489     NULL,                       /* get_in6 */
490     NULL,                       /* add_router */
491     NULL,                       /* get_next_hop */
492     NULL,                       /* get_drv_info */
493     NULL,                       /* arp_lookup */
494
495     netdev_tunnel_update_flags,
496
497     netdev_tunnel_change_seq
498 };