fix showmacs target
[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     ovs_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     ovs_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 = 0;
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] = 0xfe;
99     netdev_dev->hwaddr[1] = 0xff;
100     netdev_dev->hwaddr[2] = 0xff;
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_tunnel *netdev;
151
152     netdev = xmalloc(sizeof *netdev);
153     netdev_init(&netdev->netdev, netdev_dev_);
154
155     *netdevp = &netdev->netdev;
156     return 0;
157 }
158
159 static void
160 netdev_tunnel_close(struct netdev *netdev_)
161 {
162     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
163     free(netdev);
164 }
165
166 static int
167 netdev_tunnel_get_config(struct netdev_dev *dev_, struct smap *args)
168 {
169     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
170
171     if (netdev_dev->valid_remote_ip)
172         smap_add_format(args, "remote_ip", IP_FMT,
173                 IP_ARGS(netdev_dev->remote_addr.sin_addr.s_addr));
174     if (netdev_dev->valid_remote_port)
175         smap_add_format(args, "remote_port", "%"PRIu16,
176                 ntohs(netdev_dev->remote_addr.sin_port));
177     return 0;
178 }
179
180 static int
181 netdev_tunnel_connect(struct netdev_dev_tunnel *dev)
182 {
183     if (dev->sockfd < 0)
184         return EBADF;
185     if (!dev->valid_remote_ip || !dev->valid_remote_port)
186         return 0;
187     dev->remote_addr.sin_family = AF_INET;
188     if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
189         return errno;
190     }
191     dev->connected = true;
192     netdev_tunnel_update_seq(dev);
193     VLOG_DBG("%s: connected to (%s, %d)", netdev_dev_get_name(&dev->netdev_dev),
194         inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
195     return 0;
196 }
197
198 static int
199 netdev_tunnel_set_config(struct netdev_dev *dev_, const struct smap *args)
200 {
201     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
202     struct shash_node *node;
203
204     VLOG_DBG("tunnel_set_config(%s)", netdev_dev_get_name(dev_));
205     SMAP_FOR_EACH(node, args) {
206         VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
207         if (!strcmp(node->name, "remote_ip")) {
208             struct in_addr addr;
209             if (lookup_ip(node->data, &addr)) {
210                 VLOG_WARN("%s: bad 'remote_ip'", node->name);
211             } else {
212                 netdev_dev->remote_addr.sin_addr = addr;
213                 netdev_dev->valid_remote_ip = true;
214             }
215         } else if (!strcmp(node->name, "remote_port")) {
216             netdev_dev->remote_addr.sin_port = htons(atoi(node->data));
217             netdev_dev->valid_remote_port = true;
218         } else {
219             VLOG_WARN("%s: unknown argument '%s'", 
220                 netdev_dev_get_name(dev_), node->name);
221         }
222     }
223     return netdev_tunnel_connect(netdev_dev);        
224 }
225
226 static int
227 netdev_tunnel_listen(struct netdev *netdev_ OVS_UNUSED)
228 {
229     return 0;
230 }
231
232 static int
233 netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
234 {
235     struct netdev_dev_tunnel *dev = 
236         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
237     if (!dev->connected)
238         return -EAGAIN;
239     for (;;) {
240         ssize_t retval;
241         retval = recv(dev->sockfd, buffer, size, MSG_TRUNC);
242         VLOG_DBG("%s: recv(%"PRIxPTR", %zu, MSG_TRUNC) = %zd",
243                  netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
244         if (retval >= 0) {
245             dev->stats.rx_packets++;
246             dev->stats.rx_bytes += retval;
247             if (retval <= size) {
248                 return retval;
249             } else {
250                 dev->stats.rx_errors++;
251                 dev->stats.rx_length_errors++;
252                 return -EMSGSIZE;
253             }
254         } else if (errno != EINTR) {
255             if (errno != EAGAIN) {
256                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
257                     netdev_get_name(netdev_), strerror(errno));
258                 dev->stats.rx_errors++;
259             }
260             return -errno;
261         }
262     }
263 }
264
265 static void
266 netdev_tunnel_recv_wait(struct netdev *netdev_)
267 {
268     struct netdev_dev_tunnel *dev = 
269         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
270     if (dev->sockfd >= 0) {
271         poll_fd_wait(dev->sockfd, POLLIN);
272     }
273 }
274
275 static int
276 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
277 {
278     struct netdev_dev_tunnel *dev = 
279         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
280     if (!dev->connected)
281         return EAGAIN;
282     for (;;) {
283         ssize_t retval;
284         retval = send(dev->sockfd, buffer, size, 0);
285         VLOG_DBG("%s: send(%"PRIxPTR", %zu) = %zd",
286                  netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
287         if (retval >= 0) {
288             dev->stats.tx_packets++;
289             dev->stats.tx_bytes += retval;
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 OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
428 {
429     struct netdev_dev_tunnel *tunnel_dev;
430     char buf[6];
431
432     tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
433     if (!tunnel_dev) {
434         unixctl_command_reply_error(conn, "no such tunnel netdev");
435         return;
436     }
437
438     sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
439     unixctl_command_reply(conn, buf);
440 }
441
442 static void
443 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
444                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
445 {
446     struct netdev_dev_tunnel *tunnel_dev;
447     char buf[128];
448
449     tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
450     if (!tunnel_dev) {
451         unixctl_command_reply_error(conn, "no such tunnel netdev");
452         return;
453     }
454
455     sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
456     unixctl_command_reply(conn, buf);
457 }
458
459 static void
460 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
461                      int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
462 {
463     struct netdev_dev_tunnel *tunnel_dev;
464     char buf[128];
465
466     tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
467     if (!tunnel_dev) {
468         unixctl_command_reply_error(conn, "no such tunnel netdev");
469         return;
470     }
471
472     sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
473     unixctl_command_reply(conn, buf);
474 }
475
476
477 static int
478 netdev_tunnel_init(void)
479 {
480     unixctl_command_register("netdev-tunnel/get-port", "NAME",
481                              1, 1, netdev_tunnel_get_port, NULL);
482     unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
483                              1, 1, netdev_tunnel_get_tx_bytes, NULL);
484     unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
485                              1, 1, netdev_tunnel_get_rx_bytes, NULL);
486     return 0;
487 }
488
489 const struct netdev_class netdev_tunnel_class = {
490     "tunnel",
491     netdev_tunnel_init,         /* init */
492     NULL,                       /* run */
493     NULL,                       /* wait */
494
495     netdev_tunnel_create,
496     netdev_tunnel_destroy,
497     netdev_tunnel_get_config,
498     netdev_tunnel_set_config, 
499     NULL,                       /* get_tunnel_config */
500
501     netdev_tunnel_open,
502     netdev_tunnel_close,
503
504     netdev_tunnel_listen,
505     netdev_tunnel_recv,
506     netdev_tunnel_recv_wait,
507     netdev_tunnel_drain,
508
509     netdev_tunnel_send, 
510     netdev_tunnel_send_wait,  
511
512     netdev_tunnel_set_etheraddr,
513     netdev_tunnel_get_etheraddr,
514     NULL,                       /* get_mtu */
515     NULL,                       /* set_mtu */
516     NULL,                       /* get_ifindex */
517     NULL,                       /* get_carrier */
518     NULL,                       /* get_carrier_resets */
519     NULL,                       /* get_miimon */
520     netdev_tunnel_get_stats,
521     netdev_tunnel_set_stats,
522
523     NULL,                       /* get_features */
524     NULL,                       /* set_advertisements */
525
526     NULL,                       /* set_policing */
527     NULL,                       /* get_qos_types */
528     NULL,                       /* get_qos_capabilities */
529     NULL,                       /* get_qos */
530     NULL,                       /* set_qos */
531     NULL,                       /* get_queue */
532     NULL,                       /* set_queue */
533     NULL,                       /* delete_queue */
534     NULL,                       /* get_queue_stats */
535     NULL,                       /* dump_queues */
536     NULL,                       /* dump_queue_stats */
537
538     NULL,                       /* get_in4 */
539     NULL,                       /* set_in4 */
540     NULL,                       /* get_in6 */
541     NULL,                       /* add_router */
542     NULL,                       /* get_next_hop */
543     NULL,                       /* get_drv_info */
544     NULL,                       /* arp_lookup */
545
546     netdev_tunnel_update_flags,
547
548     netdev_tunnel_change_seq
549 };