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,
125 (struct sockaddr_storage *)&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, netdev->local_addr.sin_port);
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 smap_add_format(args, "remote_ip", IP_FMT,
174 IP_ARGS(netdev->remote_addr.sin_addr.s_addr));
175 if (netdev->valid_remote_port)
176 smap_add_format(args, "remote_port", "%"PRIu16,
177 ntohs(netdev->remote_addr.sin_port));
178 ovs_mutex_unlock(&netdev->mutex);
183 netdev_tunnel_connect(struct netdev_tunnel *dev)
184 OVS_REQUIRES(dev->mutex)
189 if (!dev->valid_remote_ip || !dev->valid_remote_port)
191 dev->remote_addr.sin_family = AF_INET;
192 if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
195 dev->connected = true;
196 netdev_tunnel_update_seq(dev);
197 VLOG_DBG("%s: connected to (%s, %d)", netdev_get_name(&dev->up),
198 inet_ntop(AF_INET, &dev->remote_addr.sin_addr, buf, 1024), ntohs(dev->remote_addr.sin_port));
203 netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args)
205 struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
206 struct shash_node *node;
209 ovs_mutex_lock(&netdev->mutex);
210 VLOG_DBG("tunnel_set_config(%s)", netdev_get_name(dev_));
211 SMAP_FOR_EACH(node, args) {
212 VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
213 if (!strcmp(node->name, "remote_ip")) {
215 if (lookup_ip(node->data, &addr)) {
216 VLOG_WARN("%s: bad 'remote_ip'", node->name);
218 netdev->remote_addr.sin_addr = addr;
219 netdev->valid_remote_ip = true;
221 } else if (!strcmp(node->name, "remote_port")) {
222 netdev->remote_addr.sin_port = htons(atoi(node->data));
223 netdev->valid_remote_port = true;
225 VLOG_WARN("%s: unknown argument '%s'",
226 netdev_get_name(dev_), node->name);
229 error = netdev_tunnel_connect(netdev);
230 ovs_mutex_unlock(&netdev->mutex);
234 static struct netdev_rx *
235 netdev_tunnel_rx_alloc(void)
237 struct netdev_rx_tunnel *rx = xzalloc(sizeof *rx);
242 netdev_tunnel_rx_construct(struct netdev_rx *rx_)
244 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
245 struct netdev *netdev_ = rx->up.netdev;
246 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
248 ovs_mutex_lock(&netdev->mutex);
249 rx->fd = netdev->sockfd;
250 ovs_mutex_unlock(&netdev->mutex);
255 netdev_tunnel_rx_destruct(struct netdev_rx *rx_ OVS_UNUSED)
260 netdev_tunnel_rx_dealloc(struct netdev_rx *rx_)
262 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
268 netdev_tunnel_rx_recv(struct netdev_rx *rx_, struct ofpbuf *buffer)
270 size_t size = ofpbuf_tailroom(buffer);
271 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
272 struct netdev_tunnel *netdev =
273 netdev_tunnel_cast(rx_->netdev);
274 if (!netdev->connected)
278 retval = recv(rx->fd, buffer->data, size, MSG_TRUNC);
279 VLOG_DBG("%s: recv(%"PRIxPTR", %"PRIuSIZE", MSG_TRUNC) = %"PRIdSIZE,
280 netdev_rx_get_name(rx_), (uintptr_t)buffer->data, size, retval);
282 netdev->stats.rx_packets++;
283 netdev->stats.rx_bytes += retval;
284 if (retval <= size) {
285 buffer->size += retval;
288 netdev->stats.rx_errors++;
289 netdev->stats.rx_length_errors++;
292 } else if (errno != EINTR) {
293 if (errno != EAGAIN) {
294 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
295 netdev_rx_get_name(rx_), ovs_strerror(errno));
296 netdev->stats.rx_errors++;
304 netdev_tunnel_rx_wait(struct netdev_rx *rx_)
306 struct netdev_rx_tunnel *rx =
307 netdev_rx_tunnel_cast(rx_);
309 poll_fd_wait(rx->fd, POLLIN);
314 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
316 struct netdev_tunnel *dev =
317 netdev_tunnel_cast(netdev_);
322 retval = send(dev->sockfd, buffer, size, 0);
323 VLOG_DBG("%s: send(%"PRIxPTR", %"PRIuSIZE") = %"PRIdSIZE,
324 netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
326 dev->stats.tx_packets++;
327 dev->stats.tx_bytes += retval;
328 if (retval != size) {
329 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIdSIZE" bytes of "
330 "%"PRIuSIZE") on %s", retval, size, netdev_get_name(netdev_));
331 dev->stats.tx_errors++;
334 } else if (errno != EINTR) {
335 if (errno != EAGAIN) {
336 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
337 netdev_get_name(netdev_), ovs_strerror(errno));
338 dev->stats.tx_errors++;
346 netdev_tunnel_send_wait(struct netdev *netdev_)
348 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
349 if (dev->sockfd >= 0) {
350 poll_fd_wait(dev->sockfd, POLLOUT);
355 netdev_tunnel_rx_drain(struct netdev_rx *rx_)
357 struct netdev_tunnel *netdev =
358 netdev_tunnel_cast(rx_->netdev);
359 struct netdev_rx_tunnel *rx =
360 netdev_rx_tunnel_cast(rx_);
364 if (!netdev->connected)
367 error = recv(rx->fd, buffer, 128, MSG_TRUNC);
369 if (error == -EAGAIN)
371 else if (error != -EMSGSIZE)
379 netdev_tunnel_set_etheraddr(struct netdev *netdev,
380 const uint8_t mac[ETH_ADDR_LEN])
382 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
384 ovs_mutex_lock(&dev->mutex);
385 if (!eth_addr_equals(dev->hwaddr, mac)) {
386 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
387 netdev_tunnel_update_seq(dev);
389 ovs_mutex_unlock(&dev->mutex);
395 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
396 uint8_t mac[ETH_ADDR_LEN])
398 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
400 ovs_mutex_lock(&dev->mutex);
401 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
402 ovs_mutex_unlock(&dev->mutex);
408 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
410 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
412 ovs_mutex_lock(&dev->mutex);
414 ovs_mutex_unlock(&dev->mutex);
419 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
421 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
423 ovs_mutex_lock(&dev->mutex);
425 ovs_mutex_unlock(&dev->mutex);
430 netdev_tunnel_update_flags(struct netdev *dev_,
431 enum netdev_flags off, enum netdev_flags on,
432 enum netdev_flags *old_flagsp)
434 struct netdev_tunnel *netdev =
435 netdev_tunnel_cast(dev_);
438 ovs_mutex_lock(&netdev->mutex);
439 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
444 // XXX should we actually do something with these flags?
445 *old_flagsp = netdev->flags;
447 netdev->flags &= ~off;
448 if (*old_flagsp != netdev->flags) {
449 netdev_tunnel_update_seq(netdev);
453 ovs_mutex_unlock(&netdev->mutex);
458 /* Helper functions. */
461 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
462 OVS_REQUIRES(dev->mutex)
465 if (!dev->change_seq) {
471 netdev_tunnel_get_port(struct unixctl_conn *conn,
472 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
474 struct netdev_tunnel *tunnel_dev;
477 ovs_mutex_lock(&tunnel_netdevs_mutex);
478 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
480 unixctl_command_reply_error(conn, "no such tunnel netdev");
484 ovs_mutex_lock(&tunnel_dev->mutex);
485 sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
486 ovs_mutex_unlock(&tunnel_dev->mutex);
488 unixctl_command_reply(conn, buf);
490 ovs_mutex_unlock(&tunnel_netdevs_mutex);
494 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
495 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
497 struct netdev_tunnel *tunnel_dev;
500 ovs_mutex_lock(&tunnel_netdevs_mutex);
501 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
503 unixctl_command_reply_error(conn, "no such tunnel netdev");
507 ovs_mutex_lock(&tunnel_dev->mutex);
508 sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
509 ovs_mutex_unlock(&tunnel_dev->mutex);
510 unixctl_command_reply(conn, buf);
512 ovs_mutex_unlock(&tunnel_netdevs_mutex);
516 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
517 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
519 struct netdev_tunnel *tunnel_dev;
522 ovs_mutex_lock(&tunnel_netdevs_mutex);
523 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
525 unixctl_command_reply_error(conn, "no such tunnel netdev");
529 sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
530 unixctl_command_reply(conn, buf);
532 ovs_mutex_unlock(&tunnel_netdevs_mutex);
537 netdev_tunnel_init(void)
539 unixctl_command_register("netdev-tunnel/get-port", "NAME",
540 1, 1, netdev_tunnel_get_port, NULL);
541 unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
542 1, 1, netdev_tunnel_get_tx_bytes, NULL);
543 unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
544 1, 1, netdev_tunnel_get_rx_bytes, NULL);
549 netdev_tunnel_run(void)
554 netdev_tunnel_wait(void)
558 const struct netdev_class netdev_tunnel_class = {
565 netdev_tunnel_construct,
566 netdev_tunnel_destruct,
567 netdev_tunnel_dealloc,
568 netdev_tunnel_get_config,
569 netdev_tunnel_set_config,
570 NULL, /* get_tunnel_config */
573 netdev_tunnel_send_wait,
575 netdev_tunnel_set_etheraddr,
576 netdev_tunnel_get_etheraddr,
579 NULL, /* get_ifindex */
580 NULL, /* get_carrier */
581 NULL, /* get_carrier_resets */
582 NULL, /* get_miimon */
583 netdev_tunnel_get_stats,
584 netdev_tunnel_set_stats,
586 NULL, /* get_features */
587 NULL, /* set_advertisements */
589 NULL, /* set_policing */
590 NULL, /* get_qos_types */
591 NULL, /* get_qos_capabilities */
594 NULL, /* get_queue */
595 NULL, /* set_queue */
596 NULL, /* delete_queue */
597 NULL, /* get_queue_stats */
598 NULL, /* queue_dump_start */
599 NULL, /* queue_dump_next */
600 NULL, /* queue_dump_done */
601 NULL, /* dump_queue_stats */
606 NULL, /* add_router */
607 NULL, /* get_next_hop */
608 NULL, /* get_status */
609 NULL, /* arp_lookup */
611 netdev_tunnel_update_flags,
613 netdev_tunnel_rx_alloc,
614 netdev_tunnel_rx_construct,
615 netdev_tunnel_rx_destruct,
616 netdev_tunnel_rx_dealloc,
617 netdev_tunnel_rx_recv,
618 netdev_tunnel_rx_wait,
619 netdev_tunnel_rx_drain,