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_, struct ofpbuf *buffer)
269 size_t size = ofpbuf_tailroom(buffer);
270 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
271 struct netdev_tunnel *netdev =
272 netdev_tunnel_cast(rx_->netdev);
273 if (!netdev->connected)
277 retval = recv(rx->fd, buffer->data, size, MSG_TRUNC);
278 VLOG_DBG("%s: recv(%"PRIxPTR", %"PRIuSIZE", MSG_TRUNC) = %"PRIdSIZE,
279 netdev_rx_get_name(rx_), (uintptr_t)buffer->data, size, retval);
281 netdev->stats.rx_packets++;
282 netdev->stats.rx_bytes += retval;
283 if (retval <= size) {
286 netdev->stats.rx_errors++;
287 netdev->stats.rx_length_errors++;
290 } else if (errno != EINTR) {
291 if (errno != EAGAIN) {
292 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
293 netdev_rx_get_name(rx_), ovs_strerror(errno));
294 netdev->stats.rx_errors++;
302 netdev_tunnel_rx_wait(struct netdev_rx *rx_)
304 struct netdev_rx_tunnel *rx =
305 netdev_rx_tunnel_cast(rx_);
307 poll_fd_wait(rx->fd, POLLIN);
312 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
314 struct netdev_tunnel *dev =
315 netdev_tunnel_cast(netdev_);
320 retval = send(dev->sockfd, buffer, size, 0);
321 VLOG_DBG("%s: send(%"PRIxPTR", %"PRIuSIZE") = %"PRIdSIZE,
322 netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
324 dev->stats.tx_packets++;
325 dev->stats.tx_bytes += retval;
326 if (retval != size) {
327 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIdSIZE" bytes of "
328 "%"PRIuSIZE") on %s", retval, size, netdev_get_name(netdev_));
329 dev->stats.tx_errors++;
332 } else if (errno != EINTR) {
333 if (errno != EAGAIN) {
334 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
335 netdev_get_name(netdev_), ovs_strerror(errno));
336 dev->stats.tx_errors++;
344 netdev_tunnel_send_wait(struct netdev *netdev_)
346 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
347 if (dev->sockfd >= 0) {
348 poll_fd_wait(dev->sockfd, POLLOUT);
353 netdev_tunnel_rx_drain(struct netdev_rx *rx_)
355 struct netdev_tunnel *netdev =
356 netdev_tunnel_cast(rx_->netdev);
357 struct netdev_rx_tunnel *rx =
358 netdev_rx_tunnel_cast(rx_);
362 if (!netdev->connected)
365 error = recv(rx->fd, buffer, 128, MSG_TRUNC);
367 if (error == -EAGAIN)
369 else if (error != -EMSGSIZE)
377 netdev_tunnel_set_etheraddr(struct netdev *netdev,
378 const uint8_t mac[ETH_ADDR_LEN])
380 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
382 ovs_mutex_lock(&dev->mutex);
383 if (!eth_addr_equals(dev->hwaddr, mac)) {
384 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
385 netdev_tunnel_update_seq(dev);
387 ovs_mutex_unlock(&dev->mutex);
393 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
394 uint8_t mac[ETH_ADDR_LEN])
396 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
398 ovs_mutex_lock(&dev->mutex);
399 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
400 ovs_mutex_unlock(&dev->mutex);
406 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
408 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
410 ovs_mutex_lock(&dev->mutex);
412 ovs_mutex_unlock(&dev->mutex);
417 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
419 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
421 ovs_mutex_lock(&dev->mutex);
423 ovs_mutex_unlock(&dev->mutex);
428 netdev_tunnel_update_flags(struct netdev *dev_,
429 enum netdev_flags off, enum netdev_flags on,
430 enum netdev_flags *old_flagsp)
432 struct netdev_tunnel *netdev =
433 netdev_tunnel_cast(dev_);
436 ovs_mutex_lock(&netdev->mutex);
437 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
442 // XXX should we actually do something with these flags?
443 *old_flagsp = netdev->flags;
445 netdev->flags &= ~off;
446 if (*old_flagsp != netdev->flags) {
447 netdev_tunnel_update_seq(netdev);
451 ovs_mutex_unlock(&netdev->mutex);
456 netdev_tunnel_change_seq(const struct netdev *netdev_)
458 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
459 unsigned int change_seq;
462 ovs_mutex_lock(&netdev->mutex);
463 change_seq = netdev->change_seq;
464 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", ntohs(tunnel_dev->local_addr.sin_port));
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,