2 * Copyright (c) 2010, 2011, 2012 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
27 #include "netdev-provider.h"
29 #include "ofp-print.h"
32 #include "poll-loop.h"
36 #include "socket-util.h"
39 VLOG_DEFINE_THIS_MODULE(netdev_tunnel);
41 struct netdev_tunnel {
44 /* Protects all members below. */
45 struct ovs_mutex mutex;
47 uint8_t hwaddr[ETH_ADDR_LEN];
48 struct netdev_stats stats;
49 enum netdev_flags flags;
51 struct sockaddr_in local_addr;
52 struct sockaddr_in remote_addr;
54 bool valid_remote_port;
56 unsigned int change_seq;
59 struct netdev_rx_tunnel {
64 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
66 static struct ovs_mutex tunnel_netdevs_mutex = OVS_MUTEX_INITIALIZER;
67 static struct shash tunnel_netdevs OVS_GUARDED_BY(tunnel_netdevs_mutex)
68 = SHASH_INITIALIZER(&tunnel_netdevs);
70 static int netdev_tunnel_construct(struct netdev *netdevp_);
71 static void netdev_tunnel_update_seq(struct netdev_tunnel *);
74 is_netdev_tunnel_class(const struct netdev_class *class)
76 return class->construct == netdev_tunnel_construct;
79 static struct netdev_tunnel *
80 netdev_tunnel_cast(const struct netdev *netdev)
82 ovs_assert(is_netdev_tunnel_class(netdev_get_class(netdev)));
83 return CONTAINER_OF(netdev, struct netdev_tunnel, up);
86 static struct netdev_rx_tunnel *
87 netdev_rx_tunnel_cast(const struct netdev_rx *rx)
89 ovs_assert(is_netdev_tunnel_class(netdev_get_class(rx->netdev)));
90 return CONTAINER_OF(rx, struct netdev_rx_tunnel, up);
93 static struct netdev *
94 netdev_tunnel_alloc(void)
96 struct netdev_tunnel *netdev = xzalloc(sizeof *netdev);
101 netdev_tunnel_construct(struct netdev *netdev_)
103 static atomic_uint next_n = ATOMIC_VAR_INIT(0);
104 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
107 atomic_add(&next_n, 1, &n);
109 ovs_mutex_init(&netdev->mutex);
110 netdev->hwaddr[0] = 0xfe;
111 netdev->hwaddr[1] = 0xff;
112 netdev->hwaddr[2] = 0xff;
113 netdev->hwaddr[3] = n >> 16;
114 netdev->hwaddr[4] = n >> 8;
115 netdev->hwaddr[5] = n;
117 netdev->change_seq = 1;
118 memset(&netdev->remote_addr, 0, sizeof(netdev->remote_addr));
119 netdev->valid_remote_ip = false;
120 netdev->valid_remote_port = false;
121 netdev->connected = false;
124 netdev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev->local_addr, 0);
125 if (netdev->sockfd < 0) {
126 return netdev->sockfd;
130 shash_add(&tunnel_netdevs, netdev_get_name(netdev_), netdev);
134 VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d",
135 netdev_get_name(netdev_), netdev->sockfd, netdev->local_addr.sin_port);
142 netdev_tunnel_destruct(struct netdev *netdev_)
144 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
146 ovs_mutex_lock(&tunnel_netdevs_mutex);
148 if (netdev->sockfd != -1)
149 close(netdev->sockfd);
151 shash_find_and_delete(&tunnel_netdevs,
152 netdev_get_name(netdev_));
154 ovs_mutex_destroy(&netdev->mutex);
155 ovs_mutex_unlock(&tunnel_netdevs_mutex);
159 netdev_tunnel_dealloc(struct netdev *netdev_)
161 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
166 netdev_tunnel_get_config(const struct netdev *dev_, struct smap *args)
168 struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
170 ovs_mutex_lock(&netdev->mutex);
171 if (netdev->valid_remote_ip)
172 smap_add_format(args, "remote_ip", IP_FMT,
173 IP_ARGS(netdev->remote_addr.sin_addr.s_addr));
174 if (netdev->valid_remote_port)
175 smap_add_format(args, "remote_port", "%"PRIu16,
176 ntohs(netdev->remote_addr.sin_port));
177 ovs_mutex_unlock(&netdev->mutex);
182 netdev_tunnel_connect(struct netdev_tunnel *dev)
183 OVS_REQUIRES(dev->mutex)
188 if (!dev->valid_remote_ip || !dev->valid_remote_port)
190 dev->remote_addr.sin_family = AF_INET;
191 if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
194 dev->connected = true;
195 netdev_tunnel_update_seq(dev);
196 VLOG_DBG("%s: connected to (%s, %d)", netdev_get_name(&dev->up),
197 inet_ntop(AF_INET, &dev->remote_addr.sin_addr, buf, 1024), ntohs(dev->remote_addr.sin_port));
202 netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args)
204 struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
205 struct shash_node *node;
208 ovs_mutex_lock(&netdev->mutex);
209 VLOG_DBG("tunnel_set_config(%s)", netdev_get_name(dev_));
210 SMAP_FOR_EACH(node, args) {
211 VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
212 if (!strcmp(node->name, "remote_ip")) {
214 if (lookup_ip(node->data, &addr)) {
215 VLOG_WARN("%s: bad 'remote_ip'", node->name);
217 netdev->remote_addr.sin_addr = addr;
218 netdev->valid_remote_ip = true;
220 } else if (!strcmp(node->name, "remote_port")) {
221 netdev->remote_addr.sin_port = htons(atoi(node->data));
222 netdev->valid_remote_port = true;
224 VLOG_WARN("%s: unknown argument '%s'",
225 netdev_get_name(dev_), node->name);
228 error = netdev_tunnel_connect(netdev);
229 ovs_mutex_unlock(&netdev->mutex);
233 static struct netdev_rx *
234 netdev_tunnel_rx_alloc(void)
236 struct netdev_rx_tunnel *rx = xzalloc(sizeof *rx);
241 netdev_tunnel_rx_construct(struct netdev_rx *rx_)
243 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
244 struct netdev *netdev_ = rx->up.netdev;
245 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
247 ovs_mutex_lock(&netdev->mutex);
248 rx->fd = netdev->sockfd;
249 ovs_mutex_unlock(&netdev->mutex);
254 netdev_tunnel_rx_destruct(struct netdev_rx *rx_ OVS_UNUSED)
259 netdev_tunnel_rx_dealloc(struct netdev_rx *rx_)
261 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
267 netdev_tunnel_rx_recv(struct netdev_rx *rx_, void *buffer, size_t size)
269 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
270 struct netdev_tunnel *netdev =
271 netdev_tunnel_cast(rx_->netdev);
272 if (!netdev->connected)
276 retval = recv(rx->fd, buffer, size, MSG_TRUNC);
277 VLOG_DBG("%s: recv(%"PRIxPTR", %"PRIuSIZE", MSG_TRUNC) = %"PRIdSIZE,
278 netdev_rx_get_name(rx_), (uintptr_t)buffer, size, retval);
280 netdev->stats.rx_packets++;
281 netdev->stats.rx_bytes += retval;
282 if (retval <= size) {
285 netdev->stats.rx_errors++;
286 netdev->stats.rx_length_errors++;
289 } else if (errno != EINTR) {
290 if (errno != EAGAIN) {
291 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
292 netdev_rx_get_name(rx_), ovs_strerror(errno));
293 netdev->stats.rx_errors++;
301 netdev_tunnel_rx_wait(struct netdev_rx *rx_)
303 struct netdev_rx_tunnel *rx =
304 netdev_rx_tunnel_cast(rx_);
306 poll_fd_wait(rx->fd, POLLIN);
311 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
313 struct netdev_tunnel *dev =
314 netdev_tunnel_cast(netdev_);
319 retval = send(dev->sockfd, buffer, size, 0);
320 VLOG_DBG("%s: send(%"PRIxPTR", %"PRIuSIZE") = %"PRIdSIZE,
321 netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
323 dev->stats.tx_packets++;
324 dev->stats.tx_bytes += retval;
325 if (retval != size) {
326 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIdSIZE" bytes of "
327 "%"PRIuSIZE") on %s", retval, size, netdev_get_name(netdev_));
328 dev->stats.tx_errors++;
331 } else if (errno != EINTR) {
332 if (errno != EAGAIN) {
333 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
334 netdev_get_name(netdev_), ovs_strerror(errno));
335 dev->stats.tx_errors++;
343 netdev_tunnel_send_wait(struct netdev *netdev_)
345 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
346 if (dev->sockfd >= 0) {
347 poll_fd_wait(dev->sockfd, POLLOUT);
352 netdev_tunnel_rx_drain(struct netdev_rx *rx_)
354 struct netdev_tunnel *netdev =
355 netdev_tunnel_cast(rx_->netdev);
356 struct netdev_rx_tunnel *rx =
357 netdev_rx_tunnel_cast(rx_);
361 if (!netdev->connected)
364 error = recv(rx->fd, buffer, 128, MSG_TRUNC);
366 if (error == -EAGAIN)
368 else if (error != -EMSGSIZE)
376 netdev_tunnel_set_etheraddr(struct netdev *netdev,
377 const uint8_t mac[ETH_ADDR_LEN])
379 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
381 ovs_mutex_lock(&dev->mutex);
382 if (!eth_addr_equals(dev->hwaddr, mac)) {
383 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
384 netdev_tunnel_update_seq(dev);
386 ovs_mutex_unlock(&dev->mutex);
392 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
393 uint8_t mac[ETH_ADDR_LEN])
395 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
397 ovs_mutex_lock(&dev->mutex);
398 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
399 ovs_mutex_unlock(&dev->mutex);
405 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
407 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
409 ovs_mutex_lock(&dev->mutex);
411 ovs_mutex_unlock(&dev->mutex);
416 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
418 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
420 ovs_mutex_lock(&dev->mutex);
422 ovs_mutex_unlock(&dev->mutex);
427 netdev_tunnel_update_flags(struct netdev *dev_,
428 enum netdev_flags off, enum netdev_flags on,
429 enum netdev_flags *old_flagsp)
431 struct netdev_tunnel *netdev =
432 netdev_tunnel_cast(dev_);
435 ovs_mutex_lock(&netdev->mutex);
436 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
441 // XXX should we actually do something with these flags?
442 *old_flagsp = netdev->flags;
444 netdev->flags &= ~off;
445 if (*old_flagsp != netdev->flags) {
446 netdev_tunnel_update_seq(netdev);
450 ovs_mutex_unlock(&netdev->mutex);
455 netdev_tunnel_change_seq(const struct netdev *netdev_)
457 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
458 unsigned int change_seq;
461 ovs_mutex_lock(&netdev->mutex);
462 change_seq = netdev->change_seq;
463 ovs_mutex_unlock(&netdev->mutex);
467 /* Helper functions. */
470 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
471 OVS_REQUIRES(dev->mutex)
474 if (!dev->change_seq) {
480 netdev_tunnel_get_port(struct unixctl_conn *conn,
481 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
483 struct netdev_tunnel *tunnel_dev;
486 ovs_mutex_lock(&tunnel_netdevs_mutex);
487 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
489 unixctl_command_reply_error(conn, "no such tunnel netdev");
493 ovs_mutex_lock(&tunnel_dev->mutex);
494 sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
495 ovs_mutex_unlock(&tunnel_dev->mutex);
497 unixctl_command_reply(conn, buf);
499 ovs_mutex_unlock(&tunnel_netdevs_mutex);
503 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
504 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
506 struct netdev_tunnel *tunnel_dev;
509 ovs_mutex_lock(&tunnel_netdevs_mutex);
510 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
512 unixctl_command_reply_error(conn, "no such tunnel netdev");
516 ovs_mutex_lock(&tunnel_dev->mutex);
517 sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
518 ovs_mutex_unlock(&tunnel_dev->mutex);
519 unixctl_command_reply(conn, buf);
521 ovs_mutex_unlock(&tunnel_netdevs_mutex);
525 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
526 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
528 struct netdev_tunnel *tunnel_dev;
531 ovs_mutex_lock(&tunnel_netdevs_mutex);
532 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
534 unixctl_command_reply_error(conn, "no such tunnel netdev");
538 sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
539 unixctl_command_reply(conn, buf);
541 ovs_mutex_unlock(&tunnel_netdevs_mutex);
546 netdev_tunnel_init(void)
548 unixctl_command_register("netdev-tunnel/get-port", "NAME",
549 1, 1, netdev_tunnel_get_port, NULL);
550 unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
551 1, 1, netdev_tunnel_get_tx_bytes, NULL);
552 unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
553 1, 1, netdev_tunnel_get_rx_bytes, NULL);
558 netdev_tunnel_run(void)
563 netdev_tunnel_wait(void)
567 const struct netdev_class netdev_tunnel_class = {
574 netdev_tunnel_construct,
575 netdev_tunnel_destruct,
576 netdev_tunnel_dealloc,
577 netdev_tunnel_get_config,
578 netdev_tunnel_set_config,
579 NULL, /* get_tunnel_config */
582 netdev_tunnel_send_wait,
584 netdev_tunnel_set_etheraddr,
585 netdev_tunnel_get_etheraddr,
588 NULL, /* get_ifindex */
589 NULL, /* get_carrier */
590 NULL, /* get_carrier_resets */
591 NULL, /* get_miimon */
592 netdev_tunnel_get_stats,
593 netdev_tunnel_set_stats,
595 NULL, /* get_features */
596 NULL, /* set_advertisements */
598 NULL, /* set_policing */
599 NULL, /* get_qos_types */
600 NULL, /* get_qos_capabilities */
603 NULL, /* get_queue */
604 NULL, /* set_queue */
605 NULL, /* delete_queue */
606 NULL, /* get_queue_stats */
607 NULL, /* queue_dump_start */
608 NULL, /* queue_dump_next */
609 NULL, /* queue_dump_done */
610 NULL, /* dump_queue_stats */
615 NULL, /* add_router */
616 NULL, /* get_next_hop */
617 NULL, /* get_drv_info */
618 NULL, /* arp_lookup */
620 netdev_tunnel_update_flags,
622 netdev_tunnel_change_seq,
624 netdev_tunnel_rx_alloc,
625 netdev_tunnel_rx_construct,
626 netdev_tunnel_rx_destruct,
627 netdev_tunnel_rx_dealloc,
628 netdev_tunnel_rx_recv,
629 netdev_tunnel_rx_wait,
630 netdev_tunnel_rx_drain,