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 {
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_rx_tunnel {
60 static const struct netdev_rx_class netdev_rx_tunnel_class;
62 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
64 static struct shash tunnel_netdevs = SHASH_INITIALIZER(&tunnel_netdevs);
66 static int netdev_tunnel_create(const struct netdev_class *, const char *,
68 static void netdev_tunnel_update_seq(struct netdev_tunnel *);
71 is_netdev_tunnel_class(const struct netdev_class *class)
73 return class->create == netdev_tunnel_create;
76 static struct netdev_tunnel *
77 netdev_tunnel_cast(const struct netdev *netdev)
79 ovs_assert(is_netdev_tunnel_class(netdev_get_class(netdev)));
80 return CONTAINER_OF(netdev, struct netdev_tunnel, up);
83 static struct netdev_rx_tunnel *
84 netdev_rx_tunnel_cast(const struct netdev_rx *rx)
86 netdev_rx_assert_class(rx, &netdev_rx_tunnel_class);
87 return CONTAINER_OF(rx, struct netdev_rx_tunnel, up);
91 netdev_tunnel_create(const struct netdev_class *class, const char *name,
92 struct netdev **netdevp)
94 static unsigned int n = 0;
95 struct netdev_tunnel *netdev;
98 netdev = xzalloc(sizeof *netdev);
99 netdev_init(&netdev->up, name, class);
100 netdev->hwaddr[0] = 0xfe;
101 netdev->hwaddr[1] = 0xff;
102 netdev->hwaddr[2] = 0xff;
103 netdev->hwaddr[3] = n >> 16;
104 netdev->hwaddr[4] = n >> 8;
105 netdev->hwaddr[5] = n;
107 netdev->change_seq = 1;
108 memset(&netdev->remote_addr, 0, sizeof(netdev->remote_addr));
109 netdev->valid_remote_ip = false;
110 netdev->valid_remote_port = false;
111 netdev->connected = false;
114 netdev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev->local_addr, 0);
115 if (netdev->sockfd < 0) {
116 error = netdev->sockfd;
121 shash_add(&tunnel_netdevs, name, netdev);
125 *netdevp = &netdev->up;
127 VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", name, netdev->sockfd, netdev->local_addr.sin_port);
137 netdev_tunnel_destroy(struct netdev *netdev_)
139 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
141 if (netdev->sockfd != -1)
142 close(netdev->sockfd);
144 shash_find_and_delete(&tunnel_netdevs,
145 netdev_get_name(netdev_));
150 netdev_tunnel_get_config(const struct netdev *dev_, struct smap *args)
152 struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
154 if (netdev->valid_remote_ip)
155 smap_add_format(args, "remote_ip", IP_FMT,
156 IP_ARGS(netdev->remote_addr.sin_addr.s_addr));
157 if (netdev->valid_remote_port)
158 smap_add_format(args, "remote_port", "%"PRIu16,
159 ntohs(netdev->remote_addr.sin_port));
164 netdev_tunnel_connect(struct netdev_tunnel *dev)
169 if (!dev->valid_remote_ip || !dev->valid_remote_port)
171 dev->remote_addr.sin_family = AF_INET;
172 if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
175 dev->connected = true;
176 netdev_tunnel_update_seq(dev);
177 VLOG_DBG("%s: connected to (%s, %d)", netdev_get_name(&dev->up),
178 inet_ntop(AF_INET, &dev->remote_addr.sin_addr, buf, 1024), ntohs(dev->remote_addr.sin_port));
183 netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args)
185 struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
186 struct shash_node *node;
188 VLOG_DBG("tunnel_set_config(%s)", netdev_get_name(dev_));
189 SMAP_FOR_EACH(node, args) {
190 VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
191 if (!strcmp(node->name, "remote_ip")) {
193 if (lookup_ip(node->data, &addr)) {
194 VLOG_WARN("%s: bad 'remote_ip'", node->name);
196 netdev->remote_addr.sin_addr = addr;
197 netdev->valid_remote_ip = true;
199 } else if (!strcmp(node->name, "remote_port")) {
200 netdev->remote_addr.sin_port = htons(atoi(node->data));
201 netdev->valid_remote_port = true;
203 VLOG_WARN("%s: unknown argument '%s'",
204 netdev_get_name(dev_), node->name);
207 return netdev_tunnel_connect(netdev);
211 netdev_tunnel_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
213 struct netdev_tunnel *netdev =
214 netdev_tunnel_cast(netdev_);
215 struct netdev_rx_tunnel *rx;
216 rx = xmalloc(sizeof *rx);
217 netdev_rx_init(&rx->up, netdev_, &netdev_rx_tunnel_class);
218 rx->fd = netdev->sockfd;
224 netdev_rx_tunnel_destroy(struct netdev_rx *rx_)
226 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
231 netdev_rx_tunnel_recv(struct netdev_rx *rx_, void *buffer, size_t size)
233 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
234 struct netdev_tunnel *netdev =
235 netdev_tunnel_cast(rx_->netdev);
236 if (!netdev->connected)
240 retval = recv(rx->fd, buffer, size, MSG_TRUNC);
241 VLOG_DBG("%s: recv(%"PRIxPTR", %zu, MSG_TRUNC) = %zd",
242 netdev_rx_get_name(rx_), (uintptr_t)buffer, size, retval);
244 netdev->stats.rx_packets++;
245 netdev->stats.rx_bytes += retval;
246 if (retval <= size) {
249 netdev->stats.rx_errors++;
250 netdev->stats.rx_length_errors++;
253 } else if (errno != EINTR) {
254 if (errno != EAGAIN) {
255 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
256 netdev_rx_get_name(rx_), ovs_strerror(errno));
257 netdev->stats.rx_errors++;
265 netdev_rx_tunnel_wait(struct netdev_rx *rx_)
267 struct netdev_rx_tunnel *rx =
268 netdev_rx_tunnel_cast(rx_);
270 poll_fd_wait(rx->fd, POLLIN);
275 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
277 struct netdev_tunnel *dev =
278 netdev_tunnel_cast(netdev_);
283 retval = send(dev->sockfd, buffer, size, 0);
284 VLOG_DBG("%s: send(%"PRIxPTR", %zu) = %zd",
285 netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
287 dev->stats.tx_packets++;
288 dev->stats.tx_bytes += retval;
289 if (retval != size) {
290 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
291 "%zu) on %s", retval, size, netdev_get_name(netdev_));
292 dev->stats.tx_errors++;
295 } else if (errno != EINTR) {
296 if (errno != EAGAIN) {
297 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
298 netdev_get_name(netdev_), ovs_strerror(errno));
299 dev->stats.tx_errors++;
307 netdev_tunnel_send_wait(struct netdev *netdev_)
309 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
310 if (dev->sockfd >= 0) {
311 poll_fd_wait(dev->sockfd, POLLOUT);
316 netdev_rx_tunnel_drain(struct netdev_rx *rx_)
318 struct netdev_tunnel *netdev =
319 netdev_tunnel_cast(rx_->netdev);
320 struct netdev_rx_tunnel *rx =
321 netdev_rx_tunnel_cast(rx_);
325 if (!netdev->connected)
328 error = recv(rx->fd, 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_tunnel *dev = netdev_tunnel_cast(netdev);
345 if (!eth_addr_equals(dev->hwaddr, mac)) {
346 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
347 netdev_tunnel_update_seq(dev);
354 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
355 uint8_t mac[ETH_ADDR_LEN])
357 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
359 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
365 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
367 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
374 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
376 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
383 netdev_tunnel_update_flags(struct netdev *dev_,
384 enum netdev_flags off, enum netdev_flags on,
385 enum netdev_flags *old_flagsp)
387 struct netdev_tunnel *netdev =
388 netdev_tunnel_cast(dev_);
390 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
394 // XXX should we actually do something with these flags?
395 *old_flagsp = netdev->flags;
397 netdev->flags &= ~off;
398 if (*old_flagsp != netdev->flags) {
399 netdev_tunnel_update_seq(netdev);
405 netdev_tunnel_change_seq(const struct netdev *netdev)
407 return netdev_tunnel_cast(netdev)->change_seq;
410 /* Helper functions. */
413 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
416 if (!dev->change_seq) {
422 netdev_tunnel_get_port(struct unixctl_conn *conn,
423 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
425 struct netdev_tunnel *tunnel_dev;
428 tunnel_dev = shash_find_data(&tunnel_netdevs, 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);
439 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
440 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
442 struct netdev_tunnel *tunnel_dev;
445 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
447 unixctl_command_reply_error(conn, "no such tunnel netdev");
451 sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
452 unixctl_command_reply(conn, buf);
456 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
457 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
459 struct netdev_tunnel *tunnel_dev;
462 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
464 unixctl_command_reply_error(conn, "no such tunnel netdev");
468 sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
469 unixctl_command_reply(conn, buf);
474 netdev_tunnel_init(void)
476 unixctl_command_register("netdev-tunnel/get-port", "NAME",
477 1, 1, netdev_tunnel_get_port, NULL);
478 unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
479 1, 1, netdev_tunnel_get_tx_bytes, NULL);
480 unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
481 1, 1, netdev_tunnel_get_rx_bytes, NULL);
485 const struct netdev_class netdev_tunnel_class = {
487 netdev_tunnel_init, /* init */
491 netdev_tunnel_create,
492 netdev_tunnel_destroy,
493 netdev_tunnel_get_config,
494 netdev_tunnel_set_config,
495 NULL, /* get_tunnel_config */
497 netdev_tunnel_rx_open,
500 netdev_tunnel_send_wait,
502 netdev_tunnel_set_etheraddr,
503 netdev_tunnel_get_etheraddr,
506 NULL, /* get_ifindex */
507 NULL, /* get_carrier */
508 NULL, /* get_carrier_resets */
509 NULL, /* get_miimon */
510 netdev_tunnel_get_stats,
511 netdev_tunnel_set_stats,
513 NULL, /* get_features */
514 NULL, /* set_advertisements */
516 NULL, /* set_policing */
517 NULL, /* get_qos_types */
518 NULL, /* get_qos_capabilities */
521 NULL, /* get_queue */
522 NULL, /* set_queue */
523 NULL, /* delete_queue */
524 NULL, /* get_queue_stats */
525 NULL, /* dump_queues */
526 NULL, /* dump_queue_stats */
531 NULL, /* add_router */
532 NULL, /* get_next_hop */
533 NULL, /* get_drv_info */
534 NULL, /* arp_lookup */
536 netdev_tunnel_update_flags,
538 netdev_tunnel_change_seq
542 static const struct netdev_rx_class netdev_rx_tunnel_class = {
543 netdev_rx_tunnel_destroy,
544 netdev_rx_tunnel_recv,
545 netdev_rx_tunnel_wait,
546 netdev_rx_tunnel_drain,