better set_config for netdev-tunnel's
[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     bool listening;
59 };
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_poll_notify(const struct netdev *);
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->mtu = 1500;
105     netdev_dev->flags = 0;
106     netdev_dev->change_seq = 1;
107     memset(&netdev_dev->remote_addr, 0, sizeof(netdev_dev->remote_addr));
108     netdev_dev->valid_remote_ip = false;
109     netdev_dev->valid_remote_port = false;
110     netdev_dev->connected = false;
111
112
113     netdev_dev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev_dev->local_addr, 0);
114     if (netdev_dev->sockfd < 0) {
115         error = netdev_dev->sockfd;
116         goto error;
117     }
118
119
120     shash_add(&tunnel_netdev_devs, name, netdev_dev);
121
122     n++;
123
124     *netdev_devp = &netdev_dev->netdev_dev;
125
126     VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", name, netdev_dev->sockfd, netdev_dev->local_addr.sin_port);
127
128     return 0;
129
130 error:
131     free(netdev_dev);
132     return error;
133 }
134
135 static void
136 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
137 {
138     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
139
140     if (netdev_dev->sockfd != -1)
141         close(netdev_dev->sockfd);
142
143     shash_find_and_delete(&tunnel_netdev_devs,
144                           netdev_dev_get_name(netdev_dev_));
145     free(netdev_dev);
146 }
147
148 static int
149 netdev_tunnel_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
150 {
151     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
152     struct netdev_tunnel *netdev;
153
154     netdev = xmalloc(sizeof *netdev);
155     netdev_init(&netdev->netdev, netdev_dev_);
156     netdev->listening = false;
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     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
232     VLOG_DBG("tunnel_listen(%s)", netdev_get_name(netdev_));
233     netdev->listening = true;
234     return 0;
235 }
236
237 static int
238 netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
239 {
240     struct netdev_dev_tunnel *dev = 
241         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
242     if (!dev->connected)
243         return -EAGAIN;
244     for (;;) {
245         ssize_t retval;
246         retval = recv(dev->sockfd, buffer, size, MSG_TRUNC);
247         VLOG_DBG("%s: recv(%x, %d, MSG_TRUNC) = %d",
248                         netdev_get_name(netdev_), buffer, size, retval);
249         if (retval >= 0) {
250             return retval <= size ? retval : -EMSGSIZE;
251         } else if (errno != EINTR) {
252             if (errno != EAGAIN) {
253                 VLOG_WARN("error receiveing Ethernet packet on %s: %s",
254                     strerror(errno), netdev_get_name(netdev_));
255             }
256             return -errno;
257         }
258     }
259 }
260
261 static void
262 netdev_tunnel_recv_wait(struct netdev *netdev_)
263 {
264     struct netdev_dev_tunnel *dev = 
265         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
266     if (dev->sockfd >= 0) {
267         poll_fd_wait(dev->sockfd, POLLIN);
268     }
269 }
270
271 static int
272 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
273 {
274     struct netdev_dev_tunnel *dev = 
275         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
276     if (!dev->connected)
277         return -EAGAIN;
278     for (;;) {
279         ssize_t retval;
280         retval = send(dev->sockfd, buffer, size, 0);
281         VLOG_DBG("%s: send(%x, %d) = %d", netdev_get_name(netdev_), buffer, size, retval);
282         if (retval >= 0) {
283             return retval;
284         } else if (errno != EINTR) {
285             if (errno != EAGAIN) {
286                 VLOG_WARN("error sending Ethernet packet on %s: %s",
287                     strerror(errno), netdev_get_name(netdev_));
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_tunnel *netdev = netdev_tunnel_cast(netdev_);
308     return 0;
309 }
310
311 static int
312 netdev_tunnel_set_etheraddr(struct netdev *netdev,
313                            const uint8_t mac[ETH_ADDR_LEN])
314 {
315     struct netdev_dev_tunnel *dev =
316         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
317
318     if (!eth_addr_equals(dev->hwaddr, mac)) {
319         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
320         netdev_tunnel_poll_notify(netdev);
321     }
322
323     return 0;
324 }
325
326 static int
327 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
328                            uint8_t mac[ETH_ADDR_LEN])
329 {
330     const struct netdev_dev_tunnel *dev =
331         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
332
333     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
334     return 0;
335 }
336
337 static int
338 netdev_tunnel_get_mtu(const struct netdev *netdev, int *mtup)
339 {
340     const struct netdev_dev_tunnel *dev =
341         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
342
343     *mtup = dev->mtu;
344     return 0;
345 }
346
347 static int
348 netdev_tunnel_set_mtu(const struct netdev *netdev, int mtu)
349 {
350     struct netdev_dev_tunnel *dev =
351         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
352
353     dev->mtu = mtu;
354     return 0;
355 }
356
357 static int
358 netdev_tunnel_get_carrier(const struct netdev *netdev, bool *carrier)
359 {
360     struct netdev_dev_tunnel *dev =
361         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
362     VLOG_DBG("tunnel_get_carrier(%s)", netdev_get_name(netdev));
363     *carrier = dev->connected;
364     return 0;
365 }
366
367 static int
368 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
369 {
370     const struct netdev_dev_tunnel *dev =
371         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
372
373     *stats = dev->stats;
374     return 0;
375 }
376
377 static int
378 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
379 {
380     struct netdev_dev_tunnel *dev =
381         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
382
383     dev->stats = *stats;
384     return 0;
385 }
386
387 static int
388 netdev_tunnel_update_flags(struct netdev *netdev,
389                           enum netdev_flags off, enum netdev_flags on,
390                           enum netdev_flags *old_flagsp)
391 {
392     struct netdev_dev_tunnel *dev =
393         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
394
395     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
396         return EINVAL;
397     }
398
399     *old_flagsp = dev->flags;
400     dev->flags |= on;
401     dev->flags &= ~off;
402     if (*old_flagsp != dev->flags) {
403         netdev_tunnel_poll_notify(netdev);
404     }
405     return 0;
406 }
407
408 static unsigned int
409 netdev_tunnel_change_seq(const struct netdev *netdev)
410 {
411     return netdev_dev_tunnel_cast(netdev_get_dev(netdev))->change_seq;
412 }
413 \f
414 /* Helper functions. */
415
416 static void
417 netdev_tunnel_poll_notify(const struct netdev *netdev)
418 {
419     struct netdev_dev_tunnel *dev =
420         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
421
422     dev->change_seq++;
423     if (!dev->change_seq) {
424         dev->change_seq++;
425     }
426 }
427
428 static void
429 netdev_tunnel_get_port(struct unixctl_conn *conn,
430                      int argc, const char *argv[], void *aux OVS_UNUSED)
431 {
432     struct netdev_dev_tunnel *tunnel_dev;
433     uint16_t port;
434     char buf[6];
435
436     tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
437     if (!tunnel_dev) {
438         unixctl_command_reply_error(conn, "no such tunnel netdev");
439         return;
440     }
441
442     sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
443     unixctl_command_reply(conn, buf);
444 }
445
446
447 static int
448 netdev_tunnel_init(void)
449 {
450     unixctl_command_register("netdev-tunnel/get-port", "NAME",
451                              1, 1, netdev_tunnel_get_port, NULL);
452     return 0;
453 }
454
455 const struct netdev_class netdev_tunnel_class = {
456     "tunnel",
457     netdev_tunnel_init,         /* init */
458     NULL,                       /* run */
459     NULL,                       /* wait */
460
461     netdev_tunnel_create,
462     netdev_tunnel_destroy,
463     netdev_tunnel_get_config,
464     netdev_tunnel_set_config, 
465
466     netdev_tunnel_open,
467     netdev_tunnel_close,
468
469     netdev_tunnel_listen,
470     netdev_tunnel_recv,
471     netdev_tunnel_recv_wait,
472     netdev_tunnel_drain,
473
474     netdev_tunnel_send, 
475     netdev_tunnel_send_wait,  
476
477     netdev_tunnel_set_etheraddr,
478     netdev_tunnel_get_etheraddr,
479     netdev_tunnel_get_mtu,
480     netdev_tunnel_set_mtu,
481     NULL,                       /* get_ifindex */
482     netdev_tunnel_get_carrier,  /* get_carrier */
483     NULL,                       /* get_carrier_resets */
484     NULL,                       /* get_miimon */
485     netdev_tunnel_get_stats,
486     netdev_tunnel_set_stats,
487
488     NULL,                       /* get_features */
489     NULL,                       /* set_advertisements */
490
491     NULL,                       /* set_policing */
492     NULL,                       /* get_qos_types */
493     NULL,                       /* get_qos_capabilities */
494     NULL,                       /* get_qos */
495     NULL,                       /* set_qos */
496     NULL,                       /* get_queue */
497     NULL,                       /* set_queue */
498     NULL,                       /* delete_queue */
499     NULL,                       /* get_queue_stats */
500     NULL,                       /* dump_queues */
501     NULL,                       /* dump_queue_stats */
502
503     NULL,                       /* get_in4 */
504     NULL,                       /* set_in4 */
505     NULL,                       /* get_in6 */
506     NULL,                       /* add_router */
507     NULL,                       /* get_next_hop */
508     NULL,                       /* get_drv_info */
509     NULL,                       /* arp_lookup */
510
511     netdev_tunnel_update_flags,
512
513     netdev_tunnel_change_seq
514 };