comply with thread-safety checks
[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_tunnel {
42     struct netdev up;
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_rx_tunnel {
56     struct netdev_rx up;
57     int fd;
58 };
59
60 static const struct netdev_rx_class netdev_rx_tunnel_class;
61
62 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
63
64 static struct shash tunnel_netdevs = SHASH_INITIALIZER(&tunnel_netdevs);
65
66 static int netdev_tunnel_create(const struct netdev_class *, const char *,
67                                struct netdev **);
68 static void netdev_tunnel_update_seq(struct netdev_tunnel *);
69
70 static bool
71 is_netdev_tunnel_class(const struct netdev_class *class)
72 {
73     return class->create == netdev_tunnel_create;
74 }
75
76 static struct netdev_tunnel *
77 netdev_tunnel_cast(const struct netdev *netdev)
78 {
79     ovs_assert(is_netdev_tunnel_class(netdev_get_class(netdev)));
80     return CONTAINER_OF(netdev, struct netdev_tunnel, up);
81 }
82
83 static struct netdev_rx_tunnel *
84 netdev_rx_tunnel_cast(const struct netdev_rx *rx)
85 {
86     netdev_rx_assert_class(rx, &netdev_rx_tunnel_class);
87     return CONTAINER_OF(rx, struct netdev_rx_tunnel, up);
88 }
89
90 static int
91 netdev_tunnel_create(const struct netdev_class *class, const char *name,
92                     struct netdev **netdevp)
93 {
94     static unsigned int n = 0;
95     struct netdev_tunnel *netdev;
96     int error;
97
98     netdev = xzalloc(sizeof *netdev);
99     netdev_init(&netdev->up, name, class);
100     netdev->hwaddr[0] = 0xfe;
101     netdev->hwaddr[1] = 0xff;
102     netdev->hwaddr[2] = 0xff;
103     netdev->hwaddr[3] = n >> 16;
104     netdev->hwaddr[4] = n >> 8;
105     netdev->hwaddr[5] = n;
106     netdev->flags = 0;
107     netdev->change_seq = 1;
108     memset(&netdev->remote_addr, 0, sizeof(netdev->remote_addr));
109     netdev->valid_remote_ip = false;
110     netdev->valid_remote_port = false;
111     netdev->connected = false;
112
113
114     netdev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev->local_addr, 0);
115     if (netdev->sockfd < 0) {
116         error = netdev->sockfd;
117         goto error;
118     }
119
120
121     shash_add(&tunnel_netdevs, name, netdev);
122
123     n++;
124
125     *netdevp = &netdev->up;
126
127     VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", name, netdev->sockfd, netdev->local_addr.sin_port);
128
129     return 0;
130
131 error:
132     free(netdev);
133     return error;
134 }
135
136 static void
137 netdev_tunnel_destroy(struct netdev *netdev_)
138 {
139     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
140
141     if (netdev->sockfd != -1)
142         close(netdev->sockfd);
143
144     shash_find_and_delete(&tunnel_netdevs,
145                           netdev_get_name(netdev_));
146     free(netdev);
147 }
148
149 static int
150 netdev_tunnel_get_config(const struct netdev *dev_, struct smap *args)
151 {
152     struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
153
154     if (netdev->valid_remote_ip)
155         smap_add_format(args, "remote_ip", IP_FMT,
156                 IP_ARGS(netdev->remote_addr.sin_addr.s_addr));
157     if (netdev->valid_remote_port)
158         smap_add_format(args, "remote_port", "%"PRIu16,
159                 ntohs(netdev->remote_addr.sin_port));
160     return 0;
161 }
162
163 static int
164 netdev_tunnel_connect(struct netdev_tunnel *dev)
165 {
166     char buf[1024];
167     if (dev->sockfd < 0)
168         return EBADF;
169     if (!dev->valid_remote_ip || !dev->valid_remote_port)
170         return 0;
171     dev->remote_addr.sin_family = AF_INET;
172     if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
173         return errno;
174     }
175     dev->connected = true;
176     netdev_tunnel_update_seq(dev);
177     VLOG_DBG("%s: connected to (%s, %d)", netdev_get_name(&dev->up),
178         inet_ntop(AF_INET, &dev->remote_addr.sin_addr, buf, 1024), ntohs(dev->remote_addr.sin_port));
179     return 0;
180 }
181
182 static int
183 netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args)
184 {
185     struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
186     struct shash_node *node;
187
188     VLOG_DBG("tunnel_set_config(%s)", netdev_get_name(dev_));
189     SMAP_FOR_EACH(node, args) {
190         VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
191         if (!strcmp(node->name, "remote_ip")) {
192             struct in_addr addr;
193             if (lookup_ip(node->data, &addr)) {
194                 VLOG_WARN("%s: bad 'remote_ip'", node->name);
195             } else {
196                 netdev->remote_addr.sin_addr = addr;
197                 netdev->valid_remote_ip = true;
198             }
199         } else if (!strcmp(node->name, "remote_port")) {
200             netdev->remote_addr.sin_port = htons(atoi(node->data));
201             netdev->valid_remote_port = true;
202         } else {
203             VLOG_WARN("%s: unknown argument '%s'", 
204                 netdev_get_name(dev_), node->name);
205         }
206     }
207     return netdev_tunnel_connect(netdev);        
208 }
209
210 static int
211 netdev_tunnel_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
212 {   
213     struct netdev_tunnel *netdev =
214         netdev_tunnel_cast(netdev_);
215     struct netdev_rx_tunnel *rx;
216     rx = xmalloc(sizeof *rx);
217     netdev_rx_init(&rx->up, netdev_, &netdev_rx_tunnel_class);
218     rx->fd = netdev->sockfd;
219     *rxp = &rx->up;
220     return 0;
221 }
222
223 static void
224 netdev_rx_tunnel_destroy(struct netdev_rx *rx_)
225 {
226     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
227     free(rx);
228 }
229
230 static int
231 netdev_rx_tunnel_recv(struct netdev_rx *rx_, void *buffer, size_t size)
232 {
233     struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
234     struct netdev_tunnel *netdev =
235         netdev_tunnel_cast(rx_->netdev);
236     if (!netdev->connected)
237         return -EAGAIN;
238     for (;;) {
239         ssize_t retval;
240         retval = recv(rx->fd, buffer, size, MSG_TRUNC);
241             VLOG_DBG("%s: recv(%"PRIxPTR", %zu, MSG_TRUNC) = %zd",
242                     netdev_rx_get_name(rx_), (uintptr_t)buffer, size, retval);
243         if (retval >= 0) {
244             netdev->stats.rx_packets++;
245             netdev->stats.rx_bytes += retval;
246             if (retval <= size) {
247                     return retval;
248             } else {
249                 netdev->stats.rx_errors++;
250                 netdev->stats.rx_length_errors++;
251                 return -EMSGSIZE;
252             }
253         } else if (errno != EINTR) {
254             if (errno != EAGAIN) {
255                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
256                     netdev_rx_get_name(rx_), ovs_strerror(errno));
257                     netdev->stats.rx_errors++;
258             }
259             return -errno;
260         }
261     }
262 }
263
264 static void
265 netdev_rx_tunnel_wait(struct netdev_rx *rx_)
266 {
267     struct netdev_rx_tunnel *rx = 
268         netdev_rx_tunnel_cast(rx_);
269     if (rx->fd >= 0) {
270         poll_fd_wait(rx->fd, POLLIN);
271     }
272 }
273
274 static int
275 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
276 {
277     struct netdev_tunnel *dev = 
278         netdev_tunnel_cast(netdev_);
279     if (!dev->connected)
280         return EAGAIN;
281     for (;;) {
282         ssize_t retval;
283         retval = send(dev->sockfd, buffer, size, 0);
284         VLOG_DBG("%s: send(%"PRIxPTR", %zu) = %zd",
285                  netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
286         if (retval >= 0) {
287             dev->stats.tx_packets++;
288             dev->stats.tx_bytes += retval;
289             if (retval != size) {
290                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
291                              "%zu) on %s", retval, size, netdev_get_name(netdev_));
292                 dev->stats.tx_errors++;
293             }
294             return 0;
295         } else if (errno != EINTR) {
296             if (errno != EAGAIN) {
297                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
298                     netdev_get_name(netdev_), ovs_strerror(errno));
299                 dev->stats.tx_errors++;
300             }
301             return errno;
302         }
303     }
304 }
305
306 static void
307 netdev_tunnel_send_wait(struct netdev *netdev_)
308 {
309     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
310     if (dev->sockfd >= 0) {
311         poll_fd_wait(dev->sockfd, POLLOUT);
312     }
313 }
314
315 static int
316 netdev_rx_tunnel_drain(struct netdev_rx *rx_)
317 {
318     struct netdev_tunnel *netdev =
319         netdev_tunnel_cast(rx_->netdev);
320     struct netdev_rx_tunnel *rx = 
321         netdev_rx_tunnel_cast(rx_);
322     char buffer[128];
323     int error;
324
325     if (!netdev->connected)
326         return 0;
327     for (;;) {
328         error = recv(rx->fd, buffer, 128, MSG_TRUNC);
329         if (error) {
330             if (error == -EAGAIN)
331                 break;
332             else if (error != -EMSGSIZE)
333                 return error;
334         }
335     }
336     return 0;
337 }
338
339 static int
340 netdev_tunnel_set_etheraddr(struct netdev *netdev,
341                            const uint8_t mac[ETH_ADDR_LEN])
342 {
343     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
344
345     if (!eth_addr_equals(dev->hwaddr, mac)) {
346         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
347         netdev_tunnel_update_seq(dev);
348     }
349
350     return 0;
351 }
352
353 static int
354 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
355                            uint8_t mac[ETH_ADDR_LEN])
356 {
357     const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
358
359     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
360     return 0;
361 }
362
363
364 static int
365 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
366 {
367     const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
368
369     *stats = dev->stats;
370     return 0;
371 }
372
373 static int
374 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
375 {
376     struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
377
378     dev->stats = *stats;
379     return 0;
380 }
381
382 static int
383 netdev_tunnel_update_flags(struct netdev *dev_,
384                           enum netdev_flags off, enum netdev_flags on,
385                           enum netdev_flags *old_flagsp)
386 {
387     struct netdev_tunnel *netdev =
388         netdev_tunnel_cast(dev_);
389
390     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
391         return EINVAL;
392     }
393
394     // XXX should we actually do something with these flags?
395     *old_flagsp = netdev->flags;
396     netdev->flags |= on;
397     netdev->flags &= ~off;
398     if (*old_flagsp != netdev->flags) {
399         netdev_tunnel_update_seq(netdev);
400     }
401     return 0;
402 }
403
404 static unsigned int
405 netdev_tunnel_change_seq(const struct netdev *netdev)
406 {
407     return netdev_tunnel_cast(netdev)->change_seq;
408 }
409 \f
410 /* Helper functions. */
411
412 static void
413 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
414 {
415     dev->change_seq++;
416     if (!dev->change_seq) {
417         dev->change_seq++;
418     }
419 }
420
421 static void
422 netdev_tunnel_get_port(struct unixctl_conn *conn,
423                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
424 {
425     struct netdev_tunnel *tunnel_dev;
426     char buf[6];
427
428     tunnel_dev = shash_find_data(&tunnel_netdevs, 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 static void
439 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
440                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
441 {
442     struct netdev_tunnel *tunnel_dev;
443     char buf[128];
444
445     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
446     if (!tunnel_dev) {
447         unixctl_command_reply_error(conn, "no such tunnel netdev");
448         return;
449     }
450
451     sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
452     unixctl_command_reply(conn, buf);
453 }
454
455 static void
456 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
457                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
458 {
459     struct netdev_tunnel *tunnel_dev;
460     char buf[128];
461
462     tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
463     if (!tunnel_dev) {
464         unixctl_command_reply_error(conn, "no such tunnel netdev");
465         return;
466     }
467
468     sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
469     unixctl_command_reply(conn, buf);
470 }
471
472
473 static int
474 netdev_tunnel_init(void)
475 {
476     unixctl_command_register("netdev-tunnel/get-port", "NAME",
477                              1, 1, netdev_tunnel_get_port, NULL);
478     unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
479                              1, 1, netdev_tunnel_get_tx_bytes, NULL);
480     unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
481                              1, 1, netdev_tunnel_get_rx_bytes, NULL);
482     return 0;
483 }
484
485 const struct netdev_class netdev_tunnel_class = {
486     "tunnel",
487     netdev_tunnel_init,         /* init */
488     NULL,                       /* run */
489     NULL,                       /* wait */
490
491     netdev_tunnel_create,
492     netdev_tunnel_destroy,
493     netdev_tunnel_get_config,
494     netdev_tunnel_set_config, 
495     NULL,                                   /* get_tunnel_config */
496
497     netdev_tunnel_rx_open,
498
499     netdev_tunnel_send, 
500     netdev_tunnel_send_wait,  
501
502     netdev_tunnel_set_etheraddr,
503     netdev_tunnel_get_etheraddr,
504     NULL,                                   /* get_mtu */
505     NULL,                                   /* set_mtu */
506     NULL,                       /* get_ifindex */
507     NULL,                                   /* get_carrier */
508     NULL,                       /* get_carrier_resets */
509     NULL,                       /* get_miimon */
510     netdev_tunnel_get_stats,
511     netdev_tunnel_set_stats,
512
513     NULL,                       /* get_features */
514     NULL,                       /* set_advertisements */
515
516     NULL,                       /* set_policing */
517     NULL,                       /* get_qos_types */
518     NULL,                       /* get_qos_capabilities */
519     NULL,                       /* get_qos */
520     NULL,                       /* set_qos */
521     NULL,                       /* get_queue */
522     NULL,                       /* set_queue */
523     NULL,                       /* delete_queue */
524     NULL,                       /* get_queue_stats */
525     NULL,                       /* dump_queues */
526     NULL,                       /* dump_queue_stats */
527
528     NULL,                       /* get_in4 */
529     NULL,                       /* set_in4 */
530     NULL,                       /* get_in6 */
531     NULL,                       /* add_router */
532     NULL,                       /* get_next_hop */
533     NULL,                       /* get_drv_info */
534     NULL,                       /* arp_lookup */
535
536     netdev_tunnel_update_flags,
537
538     netdev_tunnel_change_seq
539 };
540
541
542 static const struct netdev_rx_class netdev_rx_tunnel_class = {
543     netdev_rx_tunnel_destroy,
544     netdev_rx_tunnel_recv,
545     netdev_rx_tunnel_wait,
546     netdev_rx_tunnel_drain,
547 };
548