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