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)
176 netdev_tunnel_connect(struct netdev_dev_tunnel *dev)
180 if (!dev->valid_remote_ip || !dev->valid_remote_port)
182 dev->remote_addr.sin_family = AF_INET;
183 if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
186 dev->connected = true;
187 VLOG_DBG("%s: connected to (%s, %d)", netdev_dev_get_name(&dev->netdev_dev),
188 inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
193 netdev_tunnel_set_config(struct netdev_dev *dev_, const struct shash *args)
195 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
196 struct shash_node *node;
199 VLOG_DBG("tunnel_set_config(%s)", netdev_dev_get_name(dev_));
200 SHASH_FOR_EACH(node, args) {
201 VLOG_DBG("arg: %s->%s", node->name, node->data);
203 data = shash_find_data(args, "remote_ip");
206 int error = lookup_ip(data, &addr);
209 netdev_dev->remote_addr.sin_addr = addr;
210 netdev_dev->valid_remote_ip = true;
212 data = shash_find_data(args, "remote_port");
214 netdev_dev->remote_addr.sin_port = htons(atoi(data));
215 netdev_dev->valid_remote_port = true;
217 return netdev_tunnel_connect(netdev_dev);
221 netdev_tunnel_listen(struct netdev *netdev_)
223 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
224 VLOG_DBG("tunnel_listen(%s)", netdev_get_name(netdev_));
225 netdev->listening = true;
230 netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
232 struct netdev_dev_tunnel *dev =
233 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
238 retval = recv(dev->sockfd, buffer, size, MSG_TRUNC);
239 VLOG_DBG("%s: recv(%x, %d, MSG_TRUNC) = %d",
240 netdev_get_name(netdev_), buffer, size, retval);
242 return retval <= size ? retval : -EMSGSIZE;
243 } else if (errno != EINTR) {
244 if (errno != EAGAIN) {
245 VLOG_WARN("error receiveing Ethernet packet on %s: %s",
246 strerror(errno), netdev_get_name(netdev_));
254 netdev_tunnel_recv_wait(struct netdev *netdev_)
256 struct netdev_dev_tunnel *dev =
257 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
258 if (dev->sockfd >= 0) {
259 poll_fd_wait(dev->sockfd, POLLIN);
264 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
266 struct netdev_dev_tunnel *dev =
267 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
272 retval = send(dev->sockfd, buffer, size, 0);
273 VLOG_DBG("%s: send(%x, %d) = %d", netdev_get_name(netdev_), buffer, size, retval);
276 } else if (errno != EINTR) {
277 if (errno != EAGAIN) {
278 VLOG_WARN("error sending Ethernet packet on %s: %s",
279 strerror(errno), netdev_get_name(netdev_));
287 netdev_tunnel_send_wait(struct netdev *netdev_)
289 struct netdev_dev_tunnel *dev =
290 netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
291 if (dev->sockfd >= 0) {
292 poll_fd_wait(dev->sockfd, POLLOUT);
297 netdev_tunnel_drain(struct netdev *netdev_)
299 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
304 netdev_tunnel_set_etheraddr(struct netdev *netdev,
305 const uint8_t mac[ETH_ADDR_LEN])
307 struct netdev_dev_tunnel *dev =
308 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
310 if (!eth_addr_equals(dev->hwaddr, mac)) {
311 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
312 netdev_tunnel_poll_notify(netdev);
319 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
320 uint8_t mac[ETH_ADDR_LEN])
322 const struct netdev_dev_tunnel *dev =
323 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
325 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
330 netdev_tunnel_get_mtu(const struct netdev *netdev, int *mtup)
332 const struct netdev_dev_tunnel *dev =
333 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
340 netdev_tunnel_set_mtu(const struct netdev *netdev, int mtu)
342 struct netdev_dev_tunnel *dev =
343 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
350 netdev_tunnel_get_carrier(const struct netdev *netdev, bool *carrier)
352 struct netdev_dev_tunnel *dev =
353 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
354 VLOG_DBG("tunnel_get_carrier(%s)", netdev_get_name(netdev));
355 *carrier = dev->connected;
360 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
362 const struct netdev_dev_tunnel *dev =
363 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
370 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
372 struct netdev_dev_tunnel *dev =
373 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
380 netdev_tunnel_update_flags(struct netdev *netdev,
381 enum netdev_flags off, enum netdev_flags on,
382 enum netdev_flags *old_flagsp)
384 struct netdev_dev_tunnel *dev =
385 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
387 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
391 *old_flagsp = dev->flags;
394 if (*old_flagsp != dev->flags) {
395 netdev_tunnel_poll_notify(netdev);
401 netdev_tunnel_change_seq(const struct netdev *netdev)
403 return netdev_dev_tunnel_cast(netdev_get_dev(netdev))->change_seq;
406 /* Helper functions. */
409 netdev_tunnel_poll_notify(const struct netdev *netdev)
411 struct netdev_dev_tunnel *dev =
412 netdev_dev_tunnel_cast(netdev_get_dev(netdev));
415 if (!dev->change_seq) {
421 netdev_tunnel_get_port(struct unixctl_conn *conn,
422 int argc, const char *argv[], void *aux OVS_UNUSED)
424 struct netdev_dev_tunnel *tunnel_dev;
428 tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
430 unixctl_command_reply_error(conn, "no such tunnel netdev");
434 sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
435 unixctl_command_reply(conn, buf);
440 netdev_tunnel_init(void)
442 unixctl_command_register("netdev-tunnel/get-port", "NAME",
443 1, 1, netdev_tunnel_get_port, NULL);
447 const struct netdev_class netdev_tunnel_class = {
449 netdev_tunnel_init, /* init */
453 netdev_tunnel_create,
454 netdev_tunnel_destroy,
455 netdev_tunnel_get_config,
456 netdev_tunnel_set_config,
461 netdev_tunnel_listen,
463 netdev_tunnel_recv_wait,
467 netdev_tunnel_send_wait,
469 netdev_tunnel_set_etheraddr,
470 netdev_tunnel_get_etheraddr,
471 netdev_tunnel_get_mtu,
472 netdev_tunnel_set_mtu,
473 NULL, /* get_ifindex */
474 netdev_tunnel_get_carrier, /* get_carrier */
475 NULL, /* get_carrier_resets */
476 NULL, /* get_miimon */
477 netdev_tunnel_get_stats,
478 netdev_tunnel_set_stats,
480 NULL, /* get_features */
481 NULL, /* set_advertisements */
483 NULL, /* set_policing */
484 NULL, /* get_qos_types */
485 NULL, /* get_qos_capabilities */
488 NULL, /* get_queue */
489 NULL, /* set_queue */
490 NULL, /* delete_queue */
491 NULL, /* get_queue_stats */
492 NULL, /* dump_queues */
493 NULL, /* dump_queue_stats */
498 NULL, /* add_router */
499 NULL, /* get_next_hop */
500 NULL, /* get_drv_info */
501 NULL, /* arp_lookup */
503 netdev_tunnel_update_flags,
505 netdev_tunnel_change_seq