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];
45 struct netdev_stats stats;
46 enum netdev_flags flags;
48 struct sockaddr_in local_addr;
49 struct sockaddr_in remote_addr;
52 unsigned int change_seq;
55 struct netdev_tunnel {
60 static struct shash tunnel_netdev_devs = SHASH_INITIALIZER(&tunnel_netdev_devs);
62 static int netdev_tunnel_create(const struct netdev_class *, const char *,
63 struct netdev_dev **);
64 static void netdev_tunnel_poll_notify(const struct netdev *);
67 is_tunnel_class(const struct netdev_class *class)
69 return class->create == netdev_tunnel_create;
72 static struct netdev_dev_tunnel *
73 netdev_dev_tunnel_cast(const struct netdev_dev *netdev_dev)
75 assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
76 return CONTAINER_OF(netdev_dev, struct netdev_dev_tunnel, netdev_dev);
79 static struct netdev_tunnel *
80 netdev_tunnel_cast(const struct netdev *netdev)
82 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
83 assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
84 return CONTAINER_OF(netdev, struct netdev_tunnel, netdev);
88 netdev_tunnel_create(const struct netdev_class *class, const char *name,
89 struct netdev_dev **netdev_devp)
91 static unsigned int n = 0xaa550000;
92 struct netdev_dev_tunnel *netdev_dev;
95 netdev_dev = xzalloc(sizeof *netdev_dev);
96 netdev_dev_init(&netdev_dev->netdev_dev, name, class);
97 netdev_dev->hwaddr[0] = 0x55;
98 netdev_dev->hwaddr[1] = 0xaa;
99 netdev_dev->hwaddr[2] = n >> 24;
100 netdev_dev->hwaddr[3] = n >> 16;
101 netdev_dev->hwaddr[4] = n >> 8;
102 netdev_dev->hwaddr[5] = n;
103 netdev_dev->mtu = 1500;
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->got_remote_ip = false;
108 netdev_dev->connected = false;
111 netdev_dev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev_dev->local_addr, 0);
112 if (netdev_dev->sockfd < 0) {
113 error = netdev_dev->sockfd;
118 shash_add(&tunnel_netdev_devs, name, netdev_dev);
122 *netdev_devp = &netdev_dev->netdev_dev;
124 VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", name, netdev_dev->sockfd, netdev_dev->local_addr.sin_port);
134 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
136 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
138 if (netdev_dev->sockfd != -1)
139 close(netdev_dev->sockfd);
141 shash_find_and_delete(&tunnel_netdev_devs,
142 netdev_dev_get_name(netdev_dev_));
147 netdev_tunnel_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
149 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
150 struct netdev_tunnel *netdev;
152 netdev = xmalloc(sizeof *netdev);
153 netdev_init(&netdev->netdev, netdev_dev_);
154 netdev->listening = false;
156 *netdevp = &netdev->netdev;
161 netdev_tunnel_close(struct netdev *netdev_)
163 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
168 netdev_tunnel_get_config(struct netdev_dev *dev_, struct shash *args)
174 netdev_tunnel_connect(struct netdev_dev_tunnel *dev)
178 if (!dev->got_remote_ip || !dev->remote_addr.sin_port)
180 dev->remote_addr.sin_family = AF_INET;
181 if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
184 dev->connected = true;
185 VLOG_DBG("%s: connected to (%s, %d)", netdev_dev_get_name(&dev->netdev_dev),
186 inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
191 netdev_tunnel_set_config(struct netdev_dev *dev_, const struct shash *args)
193 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
194 struct shash_node *node;
197 VLOG_DBG("tunnel_set_config(%s)", netdev_dev_get_name(dev_));
198 SHASH_FOR_EACH(node, args) {
199 VLOG_DBG("arg: %s->%s", node->name, node->data);
201 data = shash_find_data(args, "remote_ip");
204 int error = lookup_ip(data, &addr);
207 netdev_dev->remote_addr.sin_addr = addr;
208 netdev_dev->got_remote_ip = true;
209 return netdev_tunnel_connect(netdev_dev);
215 netdev_tunnel_listen(struct netdev *netdev_)
217 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
218 VLOG_DBG("tunnel_listen(%s)", netdev_get_name(netdev_));
219 netdev->listening = true;
224 netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
226 struct netdev_dev_tunnel *dev =
227 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
232 retval = recv(dev->sockfd, buffer, size, MSG_TRUNC);
233 VLOG_DBG("%s: recv(%x, %d, MSG_TRUNC) = %d",
234 netdev_get_name(netdev_), buffer, size, retval);
236 return retval <= size ? retval : -EMSGSIZE;
237 } else if (errno != EINTR) {
238 if (errno != EAGAIN) {
239 VLOG_WARN("error receiveing Ethernet packet on %s: %s",
240 strerror(errno), netdev_get_name(netdev_));
248 netdev_tunnel_recv_wait(struct netdev *netdev_)
250 struct netdev_dev_tunnel *dev =
251 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
252 if (dev->sockfd >= 0) {
253 poll_fd_wait(dev->sockfd, POLLIN);
258 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
260 struct netdev_dev_tunnel *dev =
261 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
266 retval = send(dev->sockfd, buffer, size, 0);
267 VLOG_DBG("%s: send(%x, %d) = %d", netdev_get_name(netdev_), buffer, size, retval);
270 } else if (errno != EINTR) {
271 if (errno != EAGAIN) {
272 VLOG_WARN("error sending Ethernet packet on %s: %s",
273 strerror(errno), netdev_get_name(netdev_));
281 netdev_tunnel_send_wait(struct netdev *netdev_)
283 struct netdev_dev_tunnel *dev =
284 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
285 if (dev->sockfd >= 0) {
286 poll_fd_wait(dev->sockfd, POLLOUT);
291 netdev_tunnel_drain(struct netdev *netdev_)
293 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
298 netdev_tunnel_set_etheraddr(struct netdev *netdev,
299 const uint8_t mac[ETH_ADDR_LEN])
301 struct netdev_dev_tunnel *dev =
302 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
304 if (!eth_addr_equals(dev->hwaddr, mac)) {
305 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
306 netdev_tunnel_poll_notify(netdev);
313 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
314 uint8_t mac[ETH_ADDR_LEN])
316 const struct netdev_dev_tunnel *dev =
317 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
319 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
324 netdev_tunnel_get_mtu(const struct netdev *netdev, int *mtup)
326 const struct netdev_dev_tunnel *dev =
327 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
334 netdev_tunnel_set_mtu(const struct netdev *netdev, int mtu)
336 struct netdev_dev_tunnel *dev =
337 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
344 netdev_tunnel_get_carrier(const struct netdev *netdev, bool *carrier)
346 struct netdev_dev_tunnel *dev =
347 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
348 VLOG_DBG("tunnel_get_carrier(%s)", netdev_get_name(netdev));
349 *carrier = dev->connected;
354 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
356 const struct netdev_dev_tunnel *dev =
357 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
364 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
366 struct netdev_dev_tunnel *dev =
367 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
374 netdev_tunnel_update_flags(struct netdev *netdev,
375 enum netdev_flags off, enum netdev_flags on,
376 enum netdev_flags *old_flagsp)
378 struct netdev_dev_tunnel *dev =
379 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
381 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
385 *old_flagsp = dev->flags;
388 if (*old_flagsp != dev->flags) {
389 netdev_tunnel_poll_notify(netdev);
395 netdev_tunnel_change_seq(const struct netdev *netdev)
397 return netdev_dev_tunnel_cast(netdev_get_dev(netdev))->change_seq;
400 /* Helper functions. */
403 netdev_tunnel_poll_notify(const struct netdev *netdev)
405 struct netdev_dev_tunnel *dev =
406 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
409 if (!dev->change_seq) {
414 #define LPORT ((void*)1)
415 #define RPORT ((void*)2)
418 netdev_tunnel_get_port(struct unixctl_conn *conn,
419 int argc, const char *argv[], void *aux OVS_UNUSED)
421 struct netdev_dev_tunnel *tunnel_dev;
425 tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
427 unixctl_command_reply_error(conn, "no such tunnel netdev");
431 port = (aux == LPORT ? tunnel_dev->local_addr.sin_port
432 : tunnel_dev->remote_addr.sin_port);
434 sprintf(buf, "%d", ntohs(port));
435 unixctl_command_reply(conn, buf);
439 netdev_tunnel_set_rport(struct unixctl_conn *conn,
440 int argc, const char *argv[], void *aux OVS_UNUSED)
442 struct netdev_dev_tunnel *tunnel_dev;
445 tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
447 unixctl_command_reply_error(conn, "no such tunnel netdev");
451 tunnel_dev->remote_addr.sin_port = htons(atoi(argv[2]));
453 error = netdev_tunnel_connect(tunnel_dev);
455 unixctl_command_reply_error(conn, strerror(errno));
457 unixctl_command_reply(conn, "ok");
462 netdev_tunnel_init(void)
464 unixctl_command_register("netdev-tunnel/get-lport", "NAME",
465 1, 1, netdev_tunnel_get_port, LPORT);
466 unixctl_command_register("netdev-tunnel/get-rport", "NAME",
467 1, 1, netdev_tunnel_get_port, RPORT);
468 unixctl_command_register("netdev-tunnel/set-rport", "NAME PORT",
469 2, 2, netdev_tunnel_set_rport, 0);
473 const struct netdev_class netdev_tunnel_class = {
475 netdev_tunnel_init, /* init */
479 netdev_tunnel_create,
480 netdev_tunnel_destroy,
481 netdev_tunnel_get_config,
482 netdev_tunnel_set_config,
487 netdev_tunnel_listen,
489 netdev_tunnel_recv_wait,
493 netdev_tunnel_send_wait,
495 netdev_tunnel_set_etheraddr,
496 netdev_tunnel_get_etheraddr,
497 netdev_tunnel_get_mtu,
498 netdev_tunnel_set_mtu,
499 NULL, /* get_ifindex */
500 netdev_tunnel_get_carrier, /* get_carrier */
501 NULL, /* get_carrier_resets */
502 NULL, /* get_miimon */
503 netdev_tunnel_get_stats,
504 netdev_tunnel_set_stats,
506 NULL, /* get_features */
507 NULL, /* set_advertisements */
509 NULL, /* set_policing */
510 NULL, /* get_qos_types */
511 NULL, /* get_qos_capabilities */
514 NULL, /* get_queue */
515 NULL, /* set_queue */
516 NULL, /* delete_queue */
517 NULL, /* get_queue_stats */
518 NULL, /* dump_queues */
519 NULL, /* dump_queue_stats */
524 NULL, /* add_router */
525 NULL, /* get_next_hop */
526 NULL, /* get_drv_info */
527 NULL, /* arp_lookup */
529 netdev_tunnel_update_flags,
531 netdev_tunnel_change_seq