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;
51 bool valid_remote_port;
53 unsigned int change_seq;
56 struct netdev_tunnel {
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_poll_notify(const struct netdev *);
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 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 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 = 0xaa550000;
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] = 0x55;
99 netdev_dev->hwaddr[1] = 0xaa;
100 netdev_dev->hwaddr[2] = n >> 24;
101 netdev_dev->hwaddr[3] = n >> 16;
102 netdev_dev->hwaddr[4] = n >> 8;
103 netdev_dev->hwaddr[5] = n;
104 netdev_dev->mtu = 1500;
105 netdev_dev->flags = 0;
106 netdev_dev->change_seq = 1;
107 memset(&netdev_dev->remote_addr, 0, sizeof(netdev_dev->remote_addr));
108 netdev_dev->valid_remote_ip = false;
109 netdev_dev->valid_remote_port = false;
110 netdev_dev->connected = false;
113 netdev_dev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev_dev->local_addr, 0);
114 if (netdev_dev->sockfd < 0) {
115 error = netdev_dev->sockfd;
120 shash_add(&tunnel_netdev_devs, name, netdev_dev);
124 *netdev_devp = &netdev_dev->netdev_dev;
126 VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", name, netdev_dev->sockfd, netdev_dev->local_addr.sin_port);
136 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
138 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
140 if (netdev_dev->sockfd != -1)
141 close(netdev_dev->sockfd);
143 shash_find_and_delete(&tunnel_netdev_devs,
144 netdev_dev_get_name(netdev_dev_));
149 netdev_tunnel_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
151 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
152 struct netdev_tunnel *netdev;
154 netdev = xmalloc(sizeof *netdev);
155 netdev_init(&netdev->netdev, netdev_dev_);
156 netdev->listening = false;
158 *netdevp = &netdev->netdev;
163 netdev_tunnel_close(struct netdev *netdev_)
165 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
170 netdev_tunnel_get_config(struct netdev_dev *dev_, struct shash *args)
172 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
174 if (netdev_dev->valid_remote_ip)
175 shash_add(args, "remote_ip",
176 xasprintf(IP_FMT, IP_ARGS(&netdev_dev->remote_addr.sin_addr)));
177 if (netdev_dev->valid_remote_port)
178 shash_add(args, "remote_port",
179 xasprintf("%"PRIu16, ntohs(netdev_dev->remote_addr.sin_port)));
184 netdev_tunnel_connect(struct netdev_dev_tunnel *dev)
188 if (!dev->valid_remote_ip || !dev->valid_remote_port)
190 dev->remote_addr.sin_family = AF_INET;
191 if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
194 dev->connected = true;
195 VLOG_DBG("%s: connected to (%s, %d)", netdev_dev_get_name(&dev->netdev_dev),
196 inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
201 netdev_tunnel_set_config(struct netdev_dev *dev_, const struct shash *args)
203 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
204 struct shash_node *node;
206 VLOG_DBG("tunnel_set_config(%s)", netdev_dev_get_name(dev_));
207 SHASH_FOR_EACH(node, args) {
208 VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
209 if (!strcmp(node->name, "remote_ip")) {
211 if (lookup_ip(node->data, &addr)) {
212 VLOG_WARN("%s: bad 'remote_ip'", node->name);
214 netdev_dev->remote_addr.sin_addr = addr;
215 netdev_dev->valid_remote_ip = true;
217 } else if (!strcmp(node->name, "remote_port")) {
218 netdev_dev->remote_addr.sin_port = htons(atoi(node->data));
219 netdev_dev->valid_remote_port = true;
221 VLOG_WARN("%s: unknown argument '%s'",
222 netdev_dev_get_name(dev_), node->name);
225 return netdev_tunnel_connect(netdev_dev);
229 netdev_tunnel_listen(struct netdev *netdev_)
231 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
232 VLOG_DBG("tunnel_listen(%s)", netdev_get_name(netdev_));
233 netdev->listening = true;
238 netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
240 struct netdev_dev_tunnel *dev =
241 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
246 retval = recv(dev->sockfd, buffer, size, MSG_TRUNC);
247 VLOG_DBG("%s: recv(%x, %d, MSG_TRUNC) = %d",
248 netdev_get_name(netdev_), buffer, size, retval);
250 return retval <= size ? retval : -EMSGSIZE;
251 } else if (errno != EINTR) {
252 if (errno != EAGAIN) {
253 VLOG_WARN("error receiveing Ethernet packet on %s: %s",
254 strerror(errno), netdev_get_name(netdev_));
262 netdev_tunnel_recv_wait(struct netdev *netdev_)
264 struct netdev_dev_tunnel *dev =
265 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
266 if (dev->sockfd >= 0) {
267 poll_fd_wait(dev->sockfd, POLLIN);
272 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
274 struct netdev_dev_tunnel *dev =
275 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
280 retval = send(dev->sockfd, buffer, size, 0);
281 VLOG_DBG("%s: send(%x, %d) = %d", netdev_get_name(netdev_), buffer, size, retval);
284 } else if (errno != EINTR) {
285 if (errno != EAGAIN) {
286 VLOG_WARN("error sending Ethernet packet on %s: %s",
287 strerror(errno), netdev_get_name(netdev_));
295 netdev_tunnel_send_wait(struct netdev *netdev_)
297 struct netdev_dev_tunnel *dev =
298 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
299 if (dev->sockfd >= 0) {
300 poll_fd_wait(dev->sockfd, POLLOUT);
305 netdev_tunnel_drain(struct netdev *netdev_)
307 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
312 netdev_tunnel_set_etheraddr(struct netdev *netdev,
313 const uint8_t mac[ETH_ADDR_LEN])
315 struct netdev_dev_tunnel *dev =
316 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
318 if (!eth_addr_equals(dev->hwaddr, mac)) {
319 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
320 netdev_tunnel_poll_notify(netdev);
327 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
328 uint8_t mac[ETH_ADDR_LEN])
330 const struct netdev_dev_tunnel *dev =
331 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
333 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
338 netdev_tunnel_get_mtu(const struct netdev *netdev, int *mtup)
340 const struct netdev_dev_tunnel *dev =
341 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
348 netdev_tunnel_set_mtu(const struct netdev *netdev, int mtu)
350 struct netdev_dev_tunnel *dev =
351 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
358 netdev_tunnel_get_carrier(const struct netdev *netdev, bool *carrier)
360 struct netdev_dev_tunnel *dev =
361 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
362 VLOG_DBG("tunnel_get_carrier(%s)", netdev_get_name(netdev));
363 *carrier = dev->connected;
368 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
370 const struct netdev_dev_tunnel *dev =
371 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
378 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
380 struct netdev_dev_tunnel *dev =
381 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
388 netdev_tunnel_update_flags(struct netdev *netdev,
389 enum netdev_flags off, enum netdev_flags on,
390 enum netdev_flags *old_flagsp)
392 struct netdev_dev_tunnel *dev =
393 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
395 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
399 *old_flagsp = dev->flags;
402 if (*old_flagsp != dev->flags) {
403 netdev_tunnel_poll_notify(netdev);
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_poll_notify(const struct netdev *netdev)
419 struct netdev_dev_tunnel *dev =
420 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
423 if (!dev->change_seq) {
429 netdev_tunnel_get_port(struct unixctl_conn *conn,
430 int argc, const char *argv[], void *aux OVS_UNUSED)
432 struct netdev_dev_tunnel *tunnel_dev;
436 tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
438 unixctl_command_reply_error(conn, "no such tunnel netdev");
442 sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
443 unixctl_command_reply(conn, buf);
448 netdev_tunnel_init(void)
450 unixctl_command_register("netdev-tunnel/get-port", "NAME",
451 1, 1, netdev_tunnel_get_port, NULL);
455 const struct netdev_class netdev_tunnel_class = {
457 netdev_tunnel_init, /* init */
461 netdev_tunnel_create,
462 netdev_tunnel_destroy,
463 netdev_tunnel_get_config,
464 netdev_tunnel_set_config,
469 netdev_tunnel_listen,
471 netdev_tunnel_recv_wait,
475 netdev_tunnel_send_wait,
477 netdev_tunnel_set_etheraddr,
478 netdev_tunnel_get_etheraddr,
479 netdev_tunnel_get_mtu,
480 netdev_tunnel_set_mtu,
481 NULL, /* get_ifindex */
482 netdev_tunnel_get_carrier, /* get_carrier */
483 NULL, /* get_carrier_resets */
484 NULL, /* get_miimon */
485 netdev_tunnel_get_stats,
486 netdev_tunnel_set_stats,
488 NULL, /* get_features */
489 NULL, /* set_advertisements */
491 NULL, /* set_policing */
492 NULL, /* get_qos_types */
493 NULL, /* get_qos_capabilities */
496 NULL, /* get_queue */
497 NULL, /* set_queue */
498 NULL, /* delete_queue */
499 NULL, /* get_queue_stats */
500 NULL, /* dump_queues */
501 NULL, /* dump_queue_stats */
506 NULL, /* add_router */
507 NULL, /* get_next_hop */
508 NULL, /* get_drv_info */
509 NULL, /* arp_lookup */
511 netdev_tunnel_update_flags,
513 netdev_tunnel_change_seq