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