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_storage local_addr;
52 struct sockaddr_storage 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,
125 &netdev->local_addr, 0);
126 if (netdev->sockfd < 0) {
127 return netdev->sockfd;
131 shash_add(&tunnel_netdevs, netdev_get_name(netdev_), netdev);
135 VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d",
136 netdev_get_name(netdev_), netdev->sockfd, ss_get_port(&netdev->local_addr));
143 netdev_tunnel_destruct(struct netdev *netdev_)
145 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
147 ovs_mutex_lock(&tunnel_netdevs_mutex);
149 if (netdev->sockfd != -1)
150 close(netdev->sockfd);
152 shash_find_and_delete(&tunnel_netdevs,
153 netdev_get_name(netdev_));
155 ovs_mutex_destroy(&netdev->mutex);
156 ovs_mutex_unlock(&tunnel_netdevs_mutex);
160 netdev_tunnel_dealloc(struct netdev *netdev_)
162 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
167 netdev_tunnel_get_config(const struct netdev *dev_, struct smap *args)
169 struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
171 ovs_mutex_lock(&netdev->mutex);
172 if (netdev->valid_remote_ip) {
173 const struct sockaddr_in *sin =
174 ALIGNED_CAST(const struct sockaddr_in *, &netdev->remote_addr);
175 smap_add_format(args, "remote_ip", IP_FMT,
176 IP_ARGS(sin->sin_addr.s_addr));
178 if (netdev->valid_remote_port)
179 smap_add_format(args, "remote_port", "%"PRIu16,
180 ss_get_port(&netdev->remote_addr));
181 ovs_mutex_unlock(&netdev->mutex);
186 netdev_tunnel_connect(struct netdev_tunnel *dev)
187 OVS_REQUIRES(dev->mutex)
190 struct sockaddr_in *sin =
191 ALIGNED_CAST(struct sockaddr_in *, &dev->remote_addr);
194 if (!dev->valid_remote_ip || !dev->valid_remote_port)
196 if (connect(dev->sockfd, (struct sockaddr*) sin, sizeof(*sin)) < 0) {
197 VLOG_DBG("%s: connect returned %s", netdev_get_name(&dev->up),
198 ovs_strerror(errno));
201 dev->connected = true;
202 netdev_tunnel_update_seq(dev);
203 VLOG_DBG("%s: connected to (%s, %d)", netdev_get_name(&dev->up),
204 inet_ntop(AF_INET, &sin->sin_addr.s_addr, buf, 1024),
205 ss_get_port(&dev->remote_addr));
210 netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args)
212 struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
213 struct shash_node *node;
215 struct sockaddr_in *sin =
216 ALIGNED_CAST(struct sockaddr_in *, &netdev->remote_addr);
218 ovs_mutex_lock(&netdev->mutex);
219 VLOG_DBG("tunnel_set_config(%s)", netdev_get_name(dev_));
220 SMAP_FOR_EACH(node, args) {
221 VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
222 if (!strcmp(node->name, "remote_ip")) {
224 if (lookup_ip(node->data, &addr)) {
225 VLOG_WARN("%s: bad 'remote_ip'", node->name);
227 sin->sin_family = AF_INET;
228 sin->sin_addr = addr;
229 netdev->valid_remote_ip = true;
231 } else if (!strcmp(node->name, "remote_port")) {
232 sin->sin_port = htons(atoi(node->data));
233 netdev->valid_remote_port = true;
235 VLOG_WARN("%s: unknown argument '%s'",
236 netdev_get_name(dev_), node->name);
239 error = netdev_tunnel_connect(netdev);
240 ovs_mutex_unlock(&netdev->mutex);
244 static struct netdev_rx *
245 netdev_tunnel_rx_alloc(void)
247 struct netdev_rx_tunnel *rx = xzalloc(sizeof *rx);
252 netdev_tunnel_rx_construct(struct netdev_rx *rx_)
254 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
255 struct netdev *netdev_ = rx->up.netdev;
256 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
258 ovs_mutex_lock(&netdev->mutex);
259 rx->fd = netdev->sockfd;
260 ovs_mutex_unlock(&netdev->mutex);
265 netdev_tunnel_rx_destruct(struct netdev_rx *rx_ OVS_UNUSED)
270 netdev_tunnel_rx_dealloc(struct netdev_rx *rx_)
272 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
278 netdev_tunnel_rx_recv(struct netdev_rx *rx_, struct ofpbuf *buffer)
280 size_t size = ofpbuf_tailroom(buffer);
281 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
282 struct netdev_tunnel *netdev =
283 netdev_tunnel_cast(rx_->netdev);
284 if (!netdev->connected)
288 retval = recv(rx->fd, buffer->data, size, MSG_TRUNC);
289 VLOG_DBG("%s: recv(%"PRIxPTR", %"PRIuSIZE", MSG_TRUNC) = %"PRIdSIZE,
290 netdev_rx_get_name(rx_), (uintptr_t)buffer->data, size, retval);
292 netdev->stats.rx_packets++;
293 netdev->stats.rx_bytes += retval;
294 if (retval <= size) {
295 buffer->size += retval;
298 netdev->stats.rx_errors++;
299 netdev->stats.rx_length_errors++;
302 } else if (errno != EINTR) {
303 if (errno != EAGAIN) {
304 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
305 netdev_rx_get_name(rx_), ovs_strerror(errno));
306 netdev->stats.rx_errors++;
314 netdev_tunnel_rx_wait(struct netdev_rx *rx_)
316 struct netdev_rx_tunnel *rx =
317 netdev_rx_tunnel_cast(rx_);
319 poll_fd_wait(rx->fd, POLLIN);
324 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
326 struct netdev_tunnel *dev =
327 netdev_tunnel_cast(netdev_);
332 retval = send(dev->sockfd, buffer, size, 0);
333 VLOG_DBG("%s: send(%"PRIxPTR", %"PRIuSIZE") = %"PRIdSIZE,
334 netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
336 dev->stats.tx_packets++;
337 dev->stats.tx_bytes += retval;
338 if (retval != size) {
339 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIdSIZE" bytes of "
340 "%"PRIuSIZE") on %s", retval, size, netdev_get_name(netdev_));
341 dev->stats.tx_errors++;
344 } else if (errno != EINTR) {
345 if (errno != EAGAIN) {
346 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
347 netdev_get_name(netdev_), ovs_strerror(errno));
348 dev->stats.tx_errors++;
356 netdev_tunnel_send_wait(struct netdev *netdev_)
358 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
359 if (dev->sockfd >= 0) {
360 poll_fd_wait(dev->sockfd, POLLOUT);
365 netdev_tunnel_rx_drain(struct netdev_rx *rx_)
367 struct netdev_tunnel *netdev =
368 netdev_tunnel_cast(rx_->netdev);
369 struct netdev_rx_tunnel *rx =
370 netdev_rx_tunnel_cast(rx_);
374 if (!netdev->connected)
377 error = recv(rx->fd, buffer, 128, MSG_TRUNC);
379 if (error == -EAGAIN)
381 else if (error != -EMSGSIZE)
389 netdev_tunnel_set_etheraddr(struct netdev *netdev,
390 const uint8_t mac[ETH_ADDR_LEN])
392 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
394 ovs_mutex_lock(&dev->mutex);
395 if (!eth_addr_equals(dev->hwaddr, mac)) {
396 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
397 netdev_tunnel_update_seq(dev);
399 ovs_mutex_unlock(&dev->mutex);
405 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
406 uint8_t mac[ETH_ADDR_LEN])
408 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
410 ovs_mutex_lock(&dev->mutex);
411 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
412 ovs_mutex_unlock(&dev->mutex);
418 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
420 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
422 ovs_mutex_lock(&dev->mutex);
424 ovs_mutex_unlock(&dev->mutex);
429 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
431 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
433 ovs_mutex_lock(&dev->mutex);
435 ovs_mutex_unlock(&dev->mutex);
440 netdev_tunnel_update_flags(struct netdev *dev_,
441 enum netdev_flags off, enum netdev_flags on,
442 enum netdev_flags *old_flagsp)
444 struct netdev_tunnel *netdev =
445 netdev_tunnel_cast(dev_);
448 ovs_mutex_lock(&netdev->mutex);
449 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
454 // XXX should we actually do something with these flags?
455 *old_flagsp = netdev->flags;
457 netdev->flags &= ~off;
458 if (*old_flagsp != netdev->flags) {
459 netdev_tunnel_update_seq(netdev);
463 ovs_mutex_unlock(&netdev->mutex);
468 /* Helper functions. */
471 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
472 OVS_REQUIRES(dev->mutex)
475 if (!dev->change_seq) {
481 netdev_tunnel_get_port(struct unixctl_conn *conn,
482 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
484 struct netdev_tunnel *tunnel_dev;
487 ovs_mutex_lock(&tunnel_netdevs_mutex);
488 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
490 unixctl_command_reply_error(conn, "no such tunnel netdev");
494 ovs_mutex_lock(&tunnel_dev->mutex);
495 sprintf(buf, "%d", ss_get_port(&tunnel_dev->local_addr));
496 ovs_mutex_unlock(&tunnel_dev->mutex);
498 unixctl_command_reply(conn, buf);
500 ovs_mutex_unlock(&tunnel_netdevs_mutex);
504 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
505 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
507 struct netdev_tunnel *tunnel_dev;
510 ovs_mutex_lock(&tunnel_netdevs_mutex);
511 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
513 unixctl_command_reply_error(conn, "no such tunnel netdev");
517 ovs_mutex_lock(&tunnel_dev->mutex);
518 sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
519 ovs_mutex_unlock(&tunnel_dev->mutex);
520 unixctl_command_reply(conn, buf);
522 ovs_mutex_unlock(&tunnel_netdevs_mutex);
526 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
527 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
529 struct netdev_tunnel *tunnel_dev;
532 ovs_mutex_lock(&tunnel_netdevs_mutex);
533 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
535 unixctl_command_reply_error(conn, "no such tunnel netdev");
539 sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
540 unixctl_command_reply(conn, buf);
542 ovs_mutex_unlock(&tunnel_netdevs_mutex);
547 netdev_tunnel_init(void)
549 unixctl_command_register("netdev-tunnel/get-port", "NAME",
550 1, 1, netdev_tunnel_get_port, NULL);
551 unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
552 1, 1, netdev_tunnel_get_tx_bytes, NULL);
553 unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
554 1, 1, netdev_tunnel_get_rx_bytes, NULL);
559 netdev_tunnel_run(void)
564 netdev_tunnel_wait(void)
568 const struct netdev_class netdev_tunnel_class = {
575 netdev_tunnel_construct,
576 netdev_tunnel_destruct,
577 netdev_tunnel_dealloc,
578 netdev_tunnel_get_config,
579 netdev_tunnel_set_config,
580 NULL, /* get_tunnel_config */
583 netdev_tunnel_send_wait,
585 netdev_tunnel_set_etheraddr,
586 netdev_tunnel_get_etheraddr,
589 NULL, /* get_ifindex */
590 NULL, /* get_carrier */
591 NULL, /* get_carrier_resets */
592 NULL, /* get_miimon */
593 netdev_tunnel_get_stats,
594 netdev_tunnel_set_stats,
596 NULL, /* get_features */
597 NULL, /* set_advertisements */
599 NULL, /* set_policing */
600 NULL, /* get_qos_types */
601 NULL, /* get_qos_capabilities */
604 NULL, /* get_queue */
605 NULL, /* set_queue */
606 NULL, /* delete_queue */
607 NULL, /* get_queue_stats */
608 NULL, /* queue_dump_start */
609 NULL, /* queue_dump_next */
610 NULL, /* queue_dump_done */
611 NULL, /* dump_queue_stats */
616 NULL, /* add_router */
617 NULL, /* get_next_hop */
618 NULL, /* get_status */
619 NULL, /* arp_lookup */
621 netdev_tunnel_update_flags,
623 netdev_tunnel_rx_alloc,
624 netdev_tunnel_rx_construct,
625 netdev_tunnel_rx_destruct,
626 netdev_tunnel_rx_dealloc,
627 netdev_tunnel_rx_recv,
628 netdev_tunnel_rx_wait,
629 netdev_tunnel_rx_drain,