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 "dpif-netdev.h"
28 #include "netdev-provider.h"
30 #include "ofp-print.h"
33 #include "poll-loop.h"
37 #include "socket-util.h"
40 VLOG_DEFINE_THIS_MODULE(netdev_tunnel);
42 struct netdev_tunnel {
45 /* Protects all members below. */
46 struct ovs_mutex mutex;
48 uint8_t hwaddr[ETH_ADDR_LEN];
49 struct netdev_stats stats;
50 enum netdev_flags flags;
52 struct sockaddr_storage local_addr;
53 struct sockaddr_storage remote_addr;
55 bool valid_remote_port;
57 unsigned int change_seq;
60 struct netdev_rxq_tunnel {
65 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
67 static struct ovs_mutex tunnel_netdevs_mutex = OVS_MUTEX_INITIALIZER;
68 static struct shash tunnel_netdevs OVS_GUARDED_BY(tunnel_netdevs_mutex)
69 = SHASH_INITIALIZER(&tunnel_netdevs);
71 static int netdev_tunnel_construct(struct netdev *netdevp_);
72 static void netdev_tunnel_update_seq(struct netdev_tunnel *);
75 is_netdev_tunnel_class(const struct netdev_class *class)
77 return class->construct == netdev_tunnel_construct;
80 static struct netdev_tunnel *
81 netdev_tunnel_cast(const struct netdev *netdev)
83 ovs_assert(is_netdev_tunnel_class(netdev_get_class(netdev)));
84 return CONTAINER_OF(netdev, struct netdev_tunnel, up);
87 static struct netdev_rxq_tunnel *
88 netdev_rxq_tunnel_cast(const struct netdev_rxq *rx)
90 ovs_assert(is_netdev_tunnel_class(netdev_get_class(rx->netdev)));
91 return CONTAINER_OF(rx, struct netdev_rxq_tunnel, up);
94 static struct netdev *
95 netdev_tunnel_alloc(void)
97 struct netdev_tunnel *netdev = xzalloc(sizeof *netdev);
102 netdev_tunnel_construct(struct netdev *netdev_)
104 static atomic_uint next_n = ATOMIC_VAR_INIT(0);
105 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
108 atomic_add(&next_n, 1, &n);
110 ovs_mutex_init(&netdev->mutex);
111 netdev->hwaddr[0] = 0xfe;
112 netdev->hwaddr[1] = 0xff;
113 netdev->hwaddr[2] = 0xff;
114 netdev->hwaddr[3] = n >> 16;
115 netdev->hwaddr[4] = n >> 8;
116 netdev->hwaddr[5] = n;
118 netdev->change_seq = 1;
119 memset(&netdev->remote_addr, 0, sizeof(netdev->remote_addr));
120 netdev->valid_remote_ip = false;
121 netdev->valid_remote_port = false;
122 netdev->connected = false;
125 netdev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0,
126 &netdev->local_addr, 0);
127 if (netdev->sockfd < 0) {
128 return netdev->sockfd;
132 shash_add(&tunnel_netdevs, netdev_get_name(netdev_), netdev);
136 VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d",
137 netdev_get_name(netdev_), netdev->sockfd, ss_get_port(&netdev->local_addr));
144 netdev_tunnel_destruct(struct netdev *netdev_)
146 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
148 ovs_mutex_lock(&tunnel_netdevs_mutex);
150 if (netdev->sockfd != -1)
151 close(netdev->sockfd);
153 shash_find_and_delete(&tunnel_netdevs,
154 netdev_get_name(netdev_));
156 ovs_mutex_destroy(&netdev->mutex);
157 ovs_mutex_unlock(&tunnel_netdevs_mutex);
161 netdev_tunnel_dealloc(struct netdev *netdev_)
163 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
168 netdev_tunnel_get_config(const struct netdev *dev_, struct smap *args)
170 struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
172 ovs_mutex_lock(&netdev->mutex);
173 if (netdev->valid_remote_ip) {
174 const struct sockaddr_in *sin =
175 ALIGNED_CAST(const struct sockaddr_in *, &netdev->remote_addr);
176 smap_add_format(args, "remote_ip", IP_FMT,
177 IP_ARGS(sin->sin_addr.s_addr));
179 if (netdev->valid_remote_port)
180 smap_add_format(args, "remote_port", "%"PRIu16,
181 ss_get_port(&netdev->remote_addr));
182 ovs_mutex_unlock(&netdev->mutex);
187 netdev_tunnel_connect(struct netdev_tunnel *dev)
188 OVS_REQUIRES(dev->mutex)
191 struct sockaddr_in *sin =
192 ALIGNED_CAST(struct sockaddr_in *, &dev->remote_addr);
195 if (!dev->valid_remote_ip || !dev->valid_remote_port)
197 if (connect(dev->sockfd, (struct sockaddr*) sin, sizeof(*sin)) < 0) {
198 VLOG_DBG("%s: connect returned %s", netdev_get_name(&dev->up),
199 ovs_strerror(errno));
202 dev->connected = true;
203 netdev_tunnel_update_seq(dev);
204 VLOG_DBG("%s: connected to (%s, %d)", netdev_get_name(&dev->up),
205 inet_ntop(AF_INET, &sin->sin_addr.s_addr, buf, 1024),
206 ss_get_port(&dev->remote_addr));
211 netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args)
213 struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
214 struct shash_node *node;
216 struct sockaddr_in *sin =
217 ALIGNED_CAST(struct sockaddr_in *, &netdev->remote_addr);
219 ovs_mutex_lock(&netdev->mutex);
220 VLOG_DBG("tunnel_set_config(%s)", netdev_get_name(dev_));
221 SMAP_FOR_EACH(node, args) {
222 VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
223 if (!strcmp(node->name, "remote_ip")) {
225 if (lookup_ip(node->data, &addr)) {
226 VLOG_WARN("%s: bad 'remote_ip'", node->name);
228 sin->sin_family = AF_INET;
229 sin->sin_addr = addr;
230 netdev->valid_remote_ip = true;
232 } else if (!strcmp(node->name, "remote_port")) {
233 sin->sin_port = htons(atoi(node->data));
234 netdev->valid_remote_port = true;
236 VLOG_WARN("%s: unknown argument '%s'",
237 netdev_get_name(dev_), node->name);
240 error = netdev_tunnel_connect(netdev);
241 ovs_mutex_unlock(&netdev->mutex);
245 static struct netdev_rxq *
246 netdev_tunnel_rxq_alloc(void)
248 struct netdev_rxq_tunnel *rx = xzalloc(sizeof *rx);
253 netdev_tunnel_rxq_construct(struct netdev_rxq *rx_)
255 struct netdev_rxq_tunnel *rx = netdev_rxq_tunnel_cast(rx_);
256 struct netdev *netdev_ = rx->up.netdev;
257 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
259 ovs_mutex_lock(&netdev->mutex);
260 rx->fd = netdev->sockfd;
261 ovs_mutex_unlock(&netdev->mutex);
266 netdev_tunnel_rxq_destruct(struct netdev_rxq *rx_ OVS_UNUSED)
271 netdev_tunnel_rxq_dealloc(struct netdev_rxq *rx_)
273 struct netdev_rxq_tunnel *rx = netdev_rxq_tunnel_cast(rx_);
279 netdev_tunnel_rxq_recv(struct netdev_rxq *rx_, struct ofpbuf **packet, int *c)
281 struct netdev_rxq_tunnel *rx = netdev_rxq_tunnel_cast(rx_);
282 struct netdev_tunnel *netdev =
283 netdev_tunnel_cast(rx_->netdev);
284 struct ofpbuf *buffer = NULL;
288 if (!netdev->connected)
290 buffer = ofpbuf_new_with_headroom(VLAN_ETH_HEADER_LEN + ETH_PAYLOAD_MAX,
292 size = ofpbuf_tailroom(buffer);
296 retval = recv(rx->fd, buffer->data, size, MSG_TRUNC);
297 VLOG_DBG("%s: recv(%"PRIxPTR", %"PRIuSIZE", MSG_TRUNC) = %"PRIdSIZE,
298 netdev_rxq_get_name(rx_), (uintptr_t)buffer->data, size, retval);
300 netdev->stats.rx_packets++;
301 netdev->stats.rx_bytes += retval;
302 if (retval <= size) {
303 buffer->size += retval;
306 netdev->stats.rx_errors++;
307 netdev->stats.rx_length_errors++;
311 } else if (errno != EINTR) {
312 if (errno != EAGAIN) {
313 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
314 netdev_rxq_get_name(rx_), ovs_strerror(errno));
315 netdev->stats.rx_errors++;
323 ofpbuf_delete(buffer);
325 dp_packet_pad(buffer);
334 netdev_tunnel_rxq_wait(struct netdev_rxq *rx_)
336 struct netdev_rxq_tunnel *rx =
337 netdev_rxq_tunnel_cast(rx_);
339 poll_fd_wait(rx->fd, POLLIN);
344 netdev_tunnel_send(struct netdev *netdev_, struct ofpbuf *pkt, bool may_steal)
346 const void *buffer = pkt->data;
347 size_t size = pkt->size;
348 struct netdev_tunnel *dev =
349 netdev_tunnel_cast(netdev_);
351 if (!dev->connected) {
357 retval = send(dev->sockfd, buffer, size, 0);
358 VLOG_DBG("%s: send(%"PRIxPTR", %"PRIuSIZE") = %"PRIdSIZE,
359 netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
361 dev->stats.tx_packets++;
362 dev->stats.tx_bytes += retval;
363 if (retval != size) {
364 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIdSIZE" bytes of "
365 "%"PRIuSIZE") on %s", retval, size, netdev_get_name(netdev_));
366 dev->stats.tx_errors++;
369 } else if (errno != EINTR) {
370 if (errno != EAGAIN) {
371 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
372 netdev_get_name(netdev_), ovs_strerror(errno));
373 dev->stats.tx_errors++;
388 netdev_tunnel_send_wait(struct netdev *netdev_)
390 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
391 if (dev->sockfd >= 0) {
392 poll_fd_wait(dev->sockfd, POLLOUT);
397 netdev_tunnel_rxq_drain(struct netdev_rxq *rx_)
399 struct netdev_tunnel *netdev =
400 netdev_tunnel_cast(rx_->netdev);
401 struct netdev_rxq_tunnel *rx =
402 netdev_rxq_tunnel_cast(rx_);
406 if (!netdev->connected)
409 error = recv(rx->fd, buffer, 128, MSG_TRUNC);
411 if (error == -EAGAIN)
413 else if (error != -EMSGSIZE)
421 netdev_tunnel_set_etheraddr(struct netdev *netdev,
422 const uint8_t mac[ETH_ADDR_LEN])
424 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
426 ovs_mutex_lock(&dev->mutex);
427 if (!eth_addr_equals(dev->hwaddr, mac)) {
428 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
429 netdev_tunnel_update_seq(dev);
431 ovs_mutex_unlock(&dev->mutex);
437 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
438 uint8_t mac[ETH_ADDR_LEN])
440 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
442 ovs_mutex_lock(&dev->mutex);
443 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
444 ovs_mutex_unlock(&dev->mutex);
450 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
452 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
454 ovs_mutex_lock(&dev->mutex);
456 ovs_mutex_unlock(&dev->mutex);
461 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
463 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
465 ovs_mutex_lock(&dev->mutex);
467 ovs_mutex_unlock(&dev->mutex);
472 netdev_tunnel_update_flags(struct netdev *dev_,
473 enum netdev_flags off, enum netdev_flags on,
474 enum netdev_flags *old_flagsp)
476 struct netdev_tunnel *netdev =
477 netdev_tunnel_cast(dev_);
480 ovs_mutex_lock(&netdev->mutex);
481 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
486 // XXX should we actually do something with these flags?
487 *old_flagsp = netdev->flags;
489 netdev->flags &= ~off;
490 if (*old_flagsp != netdev->flags) {
491 netdev_tunnel_update_seq(netdev);
495 ovs_mutex_unlock(&netdev->mutex);
500 /* Helper functions. */
503 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
504 OVS_REQUIRES(dev->mutex)
507 if (!dev->change_seq) {
513 netdev_tunnel_get_port(struct unixctl_conn *conn,
514 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
516 struct netdev_tunnel *tunnel_dev;
519 ovs_mutex_lock(&tunnel_netdevs_mutex);
520 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
522 unixctl_command_reply_error(conn, "no such tunnel netdev");
526 ovs_mutex_lock(&tunnel_dev->mutex);
527 sprintf(buf, "%d", ss_get_port(&tunnel_dev->local_addr));
528 ovs_mutex_unlock(&tunnel_dev->mutex);
530 unixctl_command_reply(conn, buf);
532 ovs_mutex_unlock(&tunnel_netdevs_mutex);
536 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
537 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
539 struct netdev_tunnel *tunnel_dev;
542 ovs_mutex_lock(&tunnel_netdevs_mutex);
543 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
545 unixctl_command_reply_error(conn, "no such tunnel netdev");
549 ovs_mutex_lock(&tunnel_dev->mutex);
550 sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
551 ovs_mutex_unlock(&tunnel_dev->mutex);
552 unixctl_command_reply(conn, buf);
554 ovs_mutex_unlock(&tunnel_netdevs_mutex);
558 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
559 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
561 struct netdev_tunnel *tunnel_dev;
564 ovs_mutex_lock(&tunnel_netdevs_mutex);
565 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
567 unixctl_command_reply_error(conn, "no such tunnel netdev");
571 sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
572 unixctl_command_reply(conn, buf);
574 ovs_mutex_unlock(&tunnel_netdevs_mutex);
579 netdev_tunnel_init(void)
581 unixctl_command_register("netdev-tunnel/get-port", "NAME",
582 1, 1, netdev_tunnel_get_port, NULL);
583 unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
584 1, 1, netdev_tunnel_get_tx_bytes, NULL);
585 unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
586 1, 1, netdev_tunnel_get_rx_bytes, NULL);
591 netdev_tunnel_run(void)
596 netdev_tunnel_wait(void)
600 const struct netdev_class netdev_tunnel_class = {
607 netdev_tunnel_construct,
608 netdev_tunnel_destruct,
609 netdev_tunnel_dealloc,
610 netdev_tunnel_get_config,
611 netdev_tunnel_set_config,
612 NULL, /* get_tunnel_config */
615 netdev_tunnel_send_wait,
617 netdev_tunnel_set_etheraddr,
618 netdev_tunnel_get_etheraddr,
621 NULL, /* get_ifindex */
622 NULL, /* get_carrier */
623 NULL, /* get_carrier_resets */
624 NULL, /* get_miimon */
625 netdev_tunnel_get_stats,
626 netdev_tunnel_set_stats,
628 NULL, /* get_features */
629 NULL, /* set_advertisements */
631 NULL, /* set_policing */
632 NULL, /* get_qos_types */
633 NULL, /* get_qos_capabilities */
636 NULL, /* get_queue */
637 NULL, /* set_queue */
638 NULL, /* delete_queue */
639 NULL, /* get_queue_stats */
640 NULL, /* queue_dump_start */
641 NULL, /* queue_dump_next */
642 NULL, /* queue_dump_done */
643 NULL, /* dump_queue_stats */
648 NULL, /* add_router */
649 NULL, /* get_next_hop */
650 NULL, /* get_status */
651 NULL, /* arp_lookup */
653 netdev_tunnel_update_flags,
655 netdev_tunnel_rxq_alloc,
656 netdev_tunnel_rxq_construct,
657 netdev_tunnel_rxq_destruct,
658 netdev_tunnel_rxq_dealloc,
659 netdev_tunnel_rxq_recv,
660 netdev_tunnel_rxq_wait,
661 netdev_tunnel_rxq_drain,