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;
289 if (!netdev->connected)
291 buffer = ofpbuf_new_with_headroom(VLAN_ETH_HEADER_LEN + ETH_PAYLOAD_MAX,
293 data = ofpbuf_data(buffer);
294 size = ofpbuf_tailroom(buffer);
298 retval = recv(rx->fd, data, size, MSG_TRUNC);
299 VLOG_DBG("%s: recv(%"PRIxPTR", %"PRIuSIZE", MSG_TRUNC) = %"PRIdSIZE,
300 netdev_rxq_get_name(rx_), (uintptr_t)data, size, retval);
302 netdev->stats.rx_packets++;
303 netdev->stats.rx_bytes += retval;
304 if (retval <= size) {
305 ofpbuf_set_size(buffer, ofpbuf_size(buffer) + retval);
308 netdev->stats.rx_errors++;
309 netdev->stats.rx_length_errors++;
313 } else if (errno != EINTR) {
314 if (errno != EAGAIN) {
315 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
316 netdev_rxq_get_name(rx_), ovs_strerror(errno));
317 netdev->stats.rx_errors++;
325 ofpbuf_delete(buffer);
327 dp_packet_pad(buffer);
336 netdev_tunnel_rxq_wait(struct netdev_rxq *rx_)
338 struct netdev_rxq_tunnel *rx =
339 netdev_rxq_tunnel_cast(rx_);
341 poll_fd_wait(rx->fd, POLLIN);
346 netdev_tunnel_send(struct netdev *netdev_, struct ofpbuf *pkt, bool may_steal)
348 const void *buffer = ofpbuf_data(pkt);
349 size_t size = ofpbuf_size(pkt);
350 struct netdev_tunnel *dev =
351 netdev_tunnel_cast(netdev_);
353 if (!dev->connected) {
359 retval = send(dev->sockfd, buffer, size, 0);
360 VLOG_DBG("%s: send(%"PRIxPTR", %"PRIuSIZE") = %"PRIdSIZE,
361 netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
363 dev->stats.tx_packets++;
364 dev->stats.tx_bytes += retval;
365 if (retval != size) {
366 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIdSIZE" bytes of "
367 "%"PRIuSIZE") on %s", retval, size, netdev_get_name(netdev_));
368 dev->stats.tx_errors++;
371 } else if (errno != EINTR) {
372 if (errno != EAGAIN) {
373 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
374 netdev_get_name(netdev_), ovs_strerror(errno));
375 dev->stats.tx_errors++;
390 netdev_tunnel_send_wait(struct netdev *netdev_)
392 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
393 if (dev->sockfd >= 0) {
394 poll_fd_wait(dev->sockfd, POLLOUT);
399 netdev_tunnel_rxq_drain(struct netdev_rxq *rx_)
401 struct netdev_tunnel *netdev =
402 netdev_tunnel_cast(rx_->netdev);
403 struct netdev_rxq_tunnel *rx =
404 netdev_rxq_tunnel_cast(rx_);
408 if (!netdev->connected)
411 error = recv(rx->fd, buffer, 128, MSG_TRUNC);
413 if (error == -EAGAIN)
415 else if (error != -EMSGSIZE)
423 netdev_tunnel_set_etheraddr(struct netdev *netdev,
424 const uint8_t mac[ETH_ADDR_LEN])
426 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
428 ovs_mutex_lock(&dev->mutex);
429 if (!eth_addr_equals(dev->hwaddr, mac)) {
430 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
431 netdev_tunnel_update_seq(dev);
433 ovs_mutex_unlock(&dev->mutex);
439 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
440 uint8_t mac[ETH_ADDR_LEN])
442 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
444 ovs_mutex_lock(&dev->mutex);
445 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
446 ovs_mutex_unlock(&dev->mutex);
452 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
454 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
456 ovs_mutex_lock(&dev->mutex);
458 ovs_mutex_unlock(&dev->mutex);
463 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
465 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
467 ovs_mutex_lock(&dev->mutex);
469 ovs_mutex_unlock(&dev->mutex);
474 netdev_tunnel_update_flags(struct netdev *dev_,
475 enum netdev_flags off, enum netdev_flags on,
476 enum netdev_flags *old_flagsp)
478 struct netdev_tunnel *netdev =
479 netdev_tunnel_cast(dev_);
482 ovs_mutex_lock(&netdev->mutex);
483 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
488 // XXX should we actually do something with these flags?
489 *old_flagsp = netdev->flags;
491 netdev->flags &= ~off;
492 if (*old_flagsp != netdev->flags) {
493 netdev_tunnel_update_seq(netdev);
497 ovs_mutex_unlock(&netdev->mutex);
502 /* Helper functions. */
505 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
506 OVS_REQUIRES(dev->mutex)
509 if (!dev->change_seq) {
515 netdev_tunnel_get_port(struct unixctl_conn *conn,
516 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
518 struct netdev_tunnel *tunnel_dev;
521 ovs_mutex_lock(&tunnel_netdevs_mutex);
522 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
524 unixctl_command_reply_error(conn, "no such tunnel netdev");
528 ovs_mutex_lock(&tunnel_dev->mutex);
529 sprintf(buf, "%d", ss_get_port(&tunnel_dev->local_addr));
530 ovs_mutex_unlock(&tunnel_dev->mutex);
532 unixctl_command_reply(conn, buf);
534 ovs_mutex_unlock(&tunnel_netdevs_mutex);
538 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
539 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
541 struct netdev_tunnel *tunnel_dev;
544 ovs_mutex_lock(&tunnel_netdevs_mutex);
545 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
547 unixctl_command_reply_error(conn, "no such tunnel netdev");
551 ovs_mutex_lock(&tunnel_dev->mutex);
552 sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
553 ovs_mutex_unlock(&tunnel_dev->mutex);
554 unixctl_command_reply(conn, buf);
556 ovs_mutex_unlock(&tunnel_netdevs_mutex);
560 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
561 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
563 struct netdev_tunnel *tunnel_dev;
566 ovs_mutex_lock(&tunnel_netdevs_mutex);
567 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
569 unixctl_command_reply_error(conn, "no such tunnel netdev");
573 sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
574 unixctl_command_reply(conn, buf);
576 ovs_mutex_unlock(&tunnel_netdevs_mutex);
581 netdev_tunnel_init(void)
583 unixctl_command_register("netdev-tunnel/get-port", "NAME",
584 1, 1, netdev_tunnel_get_port, NULL);
585 unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
586 1, 1, netdev_tunnel_get_tx_bytes, NULL);
587 unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
588 1, 1, netdev_tunnel_get_rx_bytes, NULL);
593 netdev_tunnel_run(void)
598 netdev_tunnel_wait(void)
602 const struct netdev_class netdev_tunnel_class = {
609 netdev_tunnel_construct,
610 netdev_tunnel_destruct,
611 netdev_tunnel_dealloc,
612 netdev_tunnel_get_config,
613 netdev_tunnel_set_config,
614 NULL, /* get_tunnel_config */
617 netdev_tunnel_send_wait,
619 netdev_tunnel_set_etheraddr,
620 netdev_tunnel_get_etheraddr,
623 NULL, /* get_ifindex */
624 NULL, /* get_carrier */
625 NULL, /* get_carrier_resets */
626 NULL, /* get_miimon */
627 netdev_tunnel_get_stats,
628 netdev_tunnel_set_stats,
630 NULL, /* get_features */
631 NULL, /* set_advertisements */
633 NULL, /* set_policing */
634 NULL, /* get_qos_types */
635 NULL, /* get_qos_capabilities */
638 NULL, /* get_queue */
639 NULL, /* set_queue */
640 NULL, /* delete_queue */
641 NULL, /* get_queue_stats */
642 NULL, /* queue_dump_start */
643 NULL, /* queue_dump_next */
644 NULL, /* queue_dump_done */
645 NULL, /* dump_queue_stats */
650 NULL, /* add_router */
651 NULL, /* get_next_hop */
652 NULL, /* get_status */
653 NULL, /* arp_lookup */
655 netdev_tunnel_update_flags,
657 netdev_tunnel_rxq_alloc,
658 netdev_tunnel_rxq_construct,
659 netdev_tunnel_rxq_destruct,
660 netdev_tunnel_rxq_dealloc,
661 netdev_tunnel_rxq_recv,
662 netdev_tunnel_rxq_wait,
663 netdev_tunnel_rxq_drain,