update rx/tx stats
[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             dev->stats.rx_packets++;
247             dev->stats.rx_bytes += retval;
248             if (retval <= size) {
249                 return retval;
250             } else {
251                 dev->stats.rx_errors++;
252                 dev->stats.rx_length_errors++;
253                 return -EMSGSIZE;
254             }
255         } else if (errno != EINTR) {
256             if (errno != EAGAIN) {
257                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
258                     netdev_get_name(netdev_), strerror(errno));
259                 dev->stats.rx_errors++;
260             }
261             return -errno;
262         }
263     }
264 }
265
266 static void
267 netdev_tunnel_recv_wait(struct netdev *netdev_)
268 {
269     struct netdev_dev_tunnel *dev = 
270         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
271     if (dev->sockfd >= 0) {
272         poll_fd_wait(dev->sockfd, POLLIN);
273     }
274 }
275
276 static int
277 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
278 {
279     struct netdev_dev_tunnel *dev = 
280         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
281     if (!dev->connected)
282         return EAGAIN;
283     for (;;) {
284         ssize_t retval;
285         retval = send(dev->sockfd, buffer, size, 0);
286         VLOG_DBG("%s: send(%x, %d) = %d", netdev_get_name(netdev_), buffer, size, retval);
287         if (retval >= 0) {
288             dev->stats.tx_packets++;
289             dev->stats.tx_bytes++;
290             if (retval != size) {
291                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
292                              "%zu) on %s", retval, size, netdev_get_name(netdev_));
293                 dev->stats.tx_errors++;
294             }
295             return 0;
296         } else if (errno != EINTR) {
297             if (errno != EAGAIN) {
298                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
299                     netdev_get_name(netdev_), strerror(errno));
300                 dev->stats.tx_errors++;
301             }
302             return errno;
303         }
304     }
305 }
306
307 static void
308 netdev_tunnel_send_wait(struct netdev *netdev_)
309 {
310     struct netdev_dev_tunnel *dev = 
311         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
312     if (dev->sockfd >= 0) {
313         poll_fd_wait(dev->sockfd, POLLOUT);
314     }
315 }
316
317 static int
318 netdev_tunnel_drain(struct netdev *netdev_)
319 {
320     struct netdev_dev_tunnel *dev = 
321         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
322     char buffer[128];
323     int error;
324
325     if (!dev->connected)
326         return 0;
327     for (;;) {
328         error = recv(dev->sockfd, 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_dev_tunnel *dev =
344         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
345
346     if (!eth_addr_equals(dev->hwaddr, mac)) {
347         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
348         netdev_tunnel_update_seq(dev);
349     }
350
351     return 0;
352 }
353
354 static int
355 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
356                            uint8_t mac[ETH_ADDR_LEN])
357 {
358     const struct netdev_dev_tunnel *dev =
359         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
360
361     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
362     return 0;
363 }
364
365
366 static int
367 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
368 {
369     const struct netdev_dev_tunnel *dev =
370         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
371
372     *stats = dev->stats;
373     return 0;
374 }
375
376 static int
377 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
378 {
379     struct netdev_dev_tunnel *dev =
380         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
381
382     dev->stats = *stats;
383     return 0;
384 }
385
386 static int
387 netdev_tunnel_update_flags(struct netdev *netdev,
388                           enum netdev_flags off, enum netdev_flags on,
389                           enum netdev_flags *old_flagsp)
390 {
391     struct netdev_dev_tunnel *dev =
392         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
393
394     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
395         return EINVAL;
396     }
397
398     // XXX should we actually do something with this flags?
399     *old_flagsp = dev->flags;
400     dev->flags |= on;
401     dev->flags &= ~off;
402     if (*old_flagsp != dev->flags) {
403         netdev_tunnel_update_seq(dev);
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_update_seq(struct netdev_dev_tunnel *dev)
418 {
419     dev->change_seq++;
420     if (!dev->change_seq) {
421         dev->change_seq++;
422     }
423 }
424
425 static void
426 netdev_tunnel_get_port(struct unixctl_conn *conn,
427                      int argc, const char *argv[], void *aux OVS_UNUSED)
428 {
429     struct netdev_dev_tunnel *tunnel_dev;
430     uint16_t port;
431     char buf[6];
432
433     tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
434     if (!tunnel_dev) {
435         unixctl_command_reply_error(conn, "no such tunnel netdev");
436         return;
437     }
438
439     sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
440     unixctl_command_reply(conn, buf);
441 }
442
443
444 static int
445 netdev_tunnel_init(void)
446 {
447     unixctl_command_register("netdev-tunnel/get-port", "NAME",
448                              1, 1, netdev_tunnel_get_port, NULL);
449     return 0;
450 }
451
452 const struct netdev_class netdev_tunnel_class = {
453     "tunnel",
454     netdev_tunnel_init,         /* init */
455     NULL,                       /* run */
456     NULL,                       /* wait */
457
458     netdev_tunnel_create,
459     netdev_tunnel_destroy,
460     netdev_tunnel_get_config,
461     netdev_tunnel_set_config, 
462
463     netdev_tunnel_open,
464     netdev_tunnel_close,
465
466     netdev_tunnel_listen,
467     netdev_tunnel_recv,
468     netdev_tunnel_recv_wait,
469     netdev_tunnel_drain,
470
471     netdev_tunnel_send, 
472     netdev_tunnel_send_wait,  
473
474     netdev_tunnel_set_etheraddr,
475     netdev_tunnel_get_etheraddr,
476     NULL,                       /* get_mtu */
477     NULL,                       /* set_mtu */
478     NULL,                       /* get_ifindex */
479     NULL,                       /* get_carrier */
480     NULL,                       /* get_carrier_resets */
481     NULL,                       /* get_miimon */
482     netdev_tunnel_get_stats,
483     netdev_tunnel_set_stats,
484
485     NULL,                       /* get_features */
486     NULL,                       /* set_advertisements */
487
488     NULL,                       /* set_policing */
489     NULL,                       /* get_qos_types */
490     NULL,                       /* get_qos_capabilities */
491     NULL,                       /* get_qos */
492     NULL,                       /* set_qos */
493     NULL,                       /* get_queue */
494     NULL,                       /* set_queue */
495     NULL,                       /* delete_queue */
496     NULL,                       /* get_queue_stats */
497     NULL,                       /* dump_queues */
498     NULL,                       /* dump_queue_stats */
499
500     NULL,                       /* get_in4 */
501     NULL,                       /* set_in4 */
502     NULL,                       /* get_in6 */
503     NULL,                       /* add_router */
504     NULL,                       /* get_next_hop */
505     NULL,                       /* get_drv_info */
506     NULL,                       /* arp_lookup */
507
508     netdev_tunnel_update_flags,
509
510     netdev_tunnel_change_seq
511 };