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_dev_tunnel {
42 struct netdev_dev netdev_dev;
43 uint8_t hwaddr[ETH_ADDR_LEN];
44 struct netdev_stats stats;
45 enum netdev_flags flags;
47 struct sockaddr_in local_addr;
48 struct sockaddr_in remote_addr;
50 bool valid_remote_port;
52 unsigned int change_seq;
55 struct netdev_tunnel {
59 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
61 static struct shash tunnel_netdev_devs = SHASH_INITIALIZER(&tunnel_netdev_devs);
63 static int netdev_tunnel_create(const struct netdev_class *, const char *,
64 struct netdev_dev **);
65 static void netdev_tunnel_update_seq(struct netdev_dev_tunnel *);
68 is_tunnel_class(const struct netdev_class *class)
70 return class->create == netdev_tunnel_create;
73 static struct netdev_dev_tunnel *
74 netdev_dev_tunnel_cast(const struct netdev_dev *netdev_dev)
76 ovs_assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
77 return CONTAINER_OF(netdev_dev, struct netdev_dev_tunnel, netdev_dev);
80 static struct netdev_tunnel *
81 netdev_tunnel_cast(const struct netdev *netdev)
83 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
84 ovs_assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
85 return CONTAINER_OF(netdev, struct netdev_tunnel, netdev);
89 netdev_tunnel_create(const struct netdev_class *class, const char *name,
90 struct netdev_dev **netdev_devp)
92 static unsigned int n = 0;
93 struct netdev_dev_tunnel *netdev_dev;
96 netdev_dev = xzalloc(sizeof *netdev_dev);
97 netdev_dev_init(&netdev_dev->netdev_dev, name, class);
98 netdev_dev->hwaddr[0] = 0xfe;
99 netdev_dev->hwaddr[1] = 0xff;
100 netdev_dev->hwaddr[2] = 0xff;
101 netdev_dev->hwaddr[3] = n >> 16;
102 netdev_dev->hwaddr[4] = n >> 8;
103 netdev_dev->hwaddr[5] = n;
104 netdev_dev->flags = 0;
105 netdev_dev->change_seq = 1;
106 memset(&netdev_dev->remote_addr, 0, sizeof(netdev_dev->remote_addr));
107 netdev_dev->valid_remote_ip = false;
108 netdev_dev->valid_remote_port = false;
109 netdev_dev->connected = false;
112 netdev_dev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev_dev->local_addr, 0);
113 if (netdev_dev->sockfd < 0) {
114 error = netdev_dev->sockfd;
119 shash_add(&tunnel_netdev_devs, name, netdev_dev);
123 *netdev_devp = &netdev_dev->netdev_dev;
125 VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", name, netdev_dev->sockfd, netdev_dev->local_addr.sin_port);
135 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
137 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
139 if (netdev_dev->sockfd != -1)
140 close(netdev_dev->sockfd);
142 shash_find_and_delete(&tunnel_netdev_devs,
143 netdev_dev_get_name(netdev_dev_));
148 netdev_tunnel_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
150 struct netdev_tunnel *netdev;
152 netdev = xmalloc(sizeof *netdev);
153 netdev_init(&netdev->netdev, netdev_dev_);
155 *netdevp = &netdev->netdev;
160 netdev_tunnel_close(struct netdev *netdev_)
162 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
167 netdev_tunnel_get_config(struct netdev_dev *dev_, struct smap *args)
169 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
171 if (netdev_dev->valid_remote_ip)
172 smap_add_format(args, "remote_ip", IP_FMT,
173 IP_ARGS(netdev_dev->remote_addr.sin_addr.s_addr));
174 if (netdev_dev->valid_remote_port)
175 smap_add_format(args, "remote_port", "%"PRIu16,
176 ntohs(netdev_dev->remote_addr.sin_port));
181 netdev_tunnel_connect(struct netdev_dev_tunnel *dev)
185 if (!dev->valid_remote_ip || !dev->valid_remote_port)
187 dev->remote_addr.sin_family = AF_INET;
188 if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
191 dev->connected = true;
192 netdev_tunnel_update_seq(dev);
193 VLOG_DBG("%s: connected to (%s, %d)", netdev_dev_get_name(&dev->netdev_dev),
194 inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
199 netdev_tunnel_set_config(struct netdev_dev *dev_, const struct smap *args)
201 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
202 struct shash_node *node;
204 VLOG_DBG("tunnel_set_config(%s)", netdev_dev_get_name(dev_));
205 SMAP_FOR_EACH(node, args) {
206 VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
207 if (!strcmp(node->name, "remote_ip")) {
209 if (lookup_ip(node->data, &addr)) {
210 VLOG_WARN("%s: bad 'remote_ip'", node->name);
212 netdev_dev->remote_addr.sin_addr = addr;
213 netdev_dev->valid_remote_ip = true;
215 } else if (!strcmp(node->name, "remote_port")) {
216 netdev_dev->remote_addr.sin_port = htons(atoi(node->data));
217 netdev_dev->valid_remote_port = true;
219 VLOG_WARN("%s: unknown argument '%s'",
220 netdev_dev_get_name(dev_), node->name);
223 return netdev_tunnel_connect(netdev_dev);
227 netdev_tunnel_listen(struct netdev *netdev_ OVS_UNUSED)
233 netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
235 struct netdev_dev_tunnel *dev =
236 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
241 retval = recv(dev->sockfd, buffer, size, MSG_TRUNC);
242 VLOG_DBG("%s: recv(%"PRIxPTR", %zu, MSG_TRUNC) = %zd",
243 netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
245 dev->stats.rx_packets++;
246 dev->stats.rx_bytes += retval;
247 if (retval <= size) {
250 dev->stats.rx_errors++;
251 dev->stats.rx_length_errors++;
254 } else if (errno != EINTR) {
255 if (errno != EAGAIN) {
256 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
257 netdev_get_name(netdev_), strerror(errno));
258 dev->stats.rx_errors++;
266 netdev_tunnel_recv_wait(struct netdev *netdev_)
268 struct netdev_dev_tunnel *dev =
269 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
270 if (dev->sockfd >= 0) {
271 poll_fd_wait(dev->sockfd, POLLIN);
276 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
278 struct netdev_dev_tunnel *dev =
279 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
284 retval = send(dev->sockfd, buffer, size, 0);
285 VLOG_DBG("%s: send(%"PRIxPTR", %zu) = %zd",
286 netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
288 dev->stats.tx_packets++;
289 dev->stats.tx_bytes += retval;
290 if (retval != size) {
291 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
292 "%zu) on %s", retval, size, netdev_get_name(netdev_));
293 dev->stats.tx_errors++;
296 } else if (errno != EINTR) {
297 if (errno != EAGAIN) {
298 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
299 netdev_get_name(netdev_), strerror(errno));
300 dev->stats.tx_errors++;
308 netdev_tunnel_send_wait(struct netdev *netdev_)
310 struct netdev_dev_tunnel *dev =
311 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
312 if (dev->sockfd >= 0) {
313 poll_fd_wait(dev->sockfd, POLLOUT);
318 netdev_tunnel_drain(struct netdev *netdev_)
320 struct netdev_dev_tunnel *dev =
321 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
328 error = recv(dev->sockfd, buffer, 128, MSG_TRUNC);
330 if (error == -EAGAIN)
332 else if (error != -EMSGSIZE)
340 netdev_tunnel_set_etheraddr(struct netdev *netdev,
341 const uint8_t mac[ETH_ADDR_LEN])
343 struct netdev_dev_tunnel *dev =
344 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
346 if (!eth_addr_equals(dev->hwaddr, mac)) {
347 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
348 netdev_tunnel_update_seq(dev);
355 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
356 uint8_t mac[ETH_ADDR_LEN])
358 const struct netdev_dev_tunnel *dev =
359 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
361 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
367 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
369 const struct netdev_dev_tunnel *dev =
370 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
377 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
379 struct netdev_dev_tunnel *dev =
380 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
387 netdev_tunnel_update_flags(struct netdev *netdev,
388 enum netdev_flags off, enum netdev_flags on,
389 enum netdev_flags *old_flagsp)
391 struct netdev_dev_tunnel *dev =
392 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
394 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
398 // XXX should we actually do something with this flags?
399 *old_flagsp = dev->flags;
402 if (*old_flagsp != dev->flags) {
403 netdev_tunnel_update_seq(dev);
409 netdev_tunnel_change_seq(const struct netdev *netdev)
411 return netdev_dev_tunnel_cast(netdev_get_dev(netdev))->change_seq;
414 /* Helper functions. */
417 netdev_tunnel_update_seq(struct netdev_dev_tunnel *dev)
420 if (!dev->change_seq) {
426 netdev_tunnel_get_port(struct unixctl_conn *conn,
427 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
429 struct netdev_dev_tunnel *tunnel_dev;
432 tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
434 unixctl_command_reply_error(conn, "no such tunnel netdev");
438 sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
439 unixctl_command_reply(conn, buf);
443 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
444 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
446 struct netdev_dev_tunnel *tunnel_dev;
449 tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
451 unixctl_command_reply_error(conn, "no such tunnel netdev");
455 sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
456 unixctl_command_reply(conn, buf);
460 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
461 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
463 struct netdev_dev_tunnel *tunnel_dev;
466 tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
468 unixctl_command_reply_error(conn, "no such tunnel netdev");
472 sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
473 unixctl_command_reply(conn, buf);
478 netdev_tunnel_init(void)
480 unixctl_command_register("netdev-tunnel/get-port", "NAME",
481 1, 1, netdev_tunnel_get_port, NULL);
482 unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
483 1, 1, netdev_tunnel_get_tx_bytes, NULL);
484 unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
485 1, 1, netdev_tunnel_get_rx_bytes, NULL);
489 const struct netdev_class netdev_tunnel_class = {
491 netdev_tunnel_init, /* init */
495 netdev_tunnel_create,
496 netdev_tunnel_destroy,
497 netdev_tunnel_get_config,
498 netdev_tunnel_set_config,
499 NULL, /* get_tunnel_config */
504 netdev_tunnel_listen,
506 netdev_tunnel_recv_wait,
510 netdev_tunnel_send_wait,
512 netdev_tunnel_set_etheraddr,
513 netdev_tunnel_get_etheraddr,
516 NULL, /* get_ifindex */
517 NULL, /* get_carrier */
518 NULL, /* get_carrier_resets */
519 NULL, /* get_miimon */
520 netdev_tunnel_get_stats,
521 netdev_tunnel_set_stats,
523 NULL, /* get_features */
524 NULL, /* set_advertisements */
526 NULL, /* set_policing */
527 NULL, /* get_qos_types */
528 NULL, /* get_qos_capabilities */
531 NULL, /* get_queue */
532 NULL, /* set_queue */
533 NULL, /* delete_queue */
534 NULL, /* get_queue_stats */
535 NULL, /* dump_queues */
536 NULL, /* dump_queue_stats */
541 NULL, /* add_router */
542 NULL, /* get_next_hop */
543 NULL, /* get_drv_info */
544 NULL, /* arp_lookup */
546 netdev_tunnel_update_flags,
548 netdev_tunnel_change_seq