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)
168 if (!dev->valid_remote_ip || !dev->valid_remote_port)
170 dev->remote_addr.sin_family = AF_INET;
171 if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
174 dev->connected = true;
175 netdev_tunnel_update_seq(dev);
176 VLOG_DBG("%s: connected to (%s, %d)", netdev_get_name(&dev->up),
177 inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
182 netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args)
184 struct netdev_tunnel *netdev = netdev_tunnel_cast(dev_);
185 struct shash_node *node;
187 VLOG_DBG("tunnel_set_config(%s)", netdev_get_name(dev_));
188 SMAP_FOR_EACH(node, args) {
189 VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
190 if (!strcmp(node->name, "remote_ip")) {
192 if (lookup_ip(node->data, &addr)) {
193 VLOG_WARN("%s: bad 'remote_ip'", node->name);
195 netdev->remote_addr.sin_addr = addr;
196 netdev->valid_remote_ip = true;
198 } else if (!strcmp(node->name, "remote_port")) {
199 netdev->remote_addr.sin_port = htons(atoi(node->data));
200 netdev->valid_remote_port = true;
202 VLOG_WARN("%s: unknown argument '%s'",
203 netdev_get_name(dev_), node->name);
206 return netdev_tunnel_connect(netdev);
210 netdev_tunnel_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
212 struct netdev_tunnel *netdev =
213 netdev_tunnel_cast(netdev_);
214 struct netdev_rx_tunnel *rx;
215 rx = xmalloc(sizeof *rx);
216 netdev_rx_init(&rx->up, netdev_, &netdev_rx_tunnel_class);
217 rx->fd = netdev->sockfd;
223 netdev_rx_tunnel_destroy(struct netdev_rx *rx_)
225 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
230 netdev_rx_tunnel_recv(struct netdev_rx *rx_, void *buffer, size_t size)
232 struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
233 struct netdev_tunnel *netdev =
234 netdev_tunnel_cast(rx_->netdev);
235 if (!netdev->connected)
239 retval = recv(rx->fd, buffer, size, MSG_TRUNC);
240 VLOG_DBG("%s: recv(%"PRIxPTR", %zu, MSG_TRUNC) = %zd",
241 netdev_rx_get_name(rx_), (uintptr_t)buffer, size, retval);
243 netdev->stats.rx_packets++;
244 netdev->stats.rx_bytes += retval;
245 if (retval <= size) {
248 netdev->stats.rx_errors++;
249 netdev->stats.rx_length_errors++;
252 } else if (errno != EINTR) {
253 if (errno != EAGAIN) {
254 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
255 netdev_rx_get_name(rx_), ovs_strerror(errno));
256 netdev->stats.rx_errors++;
264 netdev_rx_tunnel_wait(struct netdev_rx *rx_)
266 struct netdev_rx_tunnel *rx =
267 netdev_rx_tunnel_cast(rx_);
269 poll_fd_wait(rx->fd, POLLIN);
274 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
276 struct netdev_tunnel *dev =
277 netdev_tunnel_cast(netdev_);
282 retval = send(dev->sockfd, buffer, size, 0);
283 VLOG_DBG("%s: send(%"PRIxPTR", %zu) = %zd",
284 netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
286 dev->stats.tx_packets++;
287 dev->stats.tx_bytes += retval;
288 if (retval != size) {
289 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
290 "%zu) on %s", retval, size, netdev_get_name(netdev_));
291 dev->stats.tx_errors++;
294 } else if (errno != EINTR) {
295 if (errno != EAGAIN) {
296 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
297 netdev_get_name(netdev_), ovs_strerror(errno));
298 dev->stats.tx_errors++;
306 netdev_tunnel_send_wait(struct netdev *netdev_)
308 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev_);
309 if (dev->sockfd >= 0) {
310 poll_fd_wait(dev->sockfd, POLLOUT);
315 netdev_rx_tunnel_drain(struct netdev_rx *rx_)
317 struct netdev_tunnel *netdev =
318 netdev_tunnel_cast(rx_->netdev);
319 struct netdev_rx_tunnel *rx =
320 netdev_rx_tunnel_cast(rx_);
324 if (!netdev->connected)
327 error = recv(rx->fd, buffer, 128, MSG_TRUNC);
329 if (error == -EAGAIN)
331 else if (error != -EMSGSIZE)
339 netdev_tunnel_set_etheraddr(struct netdev *netdev,
340 const uint8_t mac[ETH_ADDR_LEN])
342 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
344 if (!eth_addr_equals(dev->hwaddr, mac)) {
345 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
346 netdev_tunnel_update_seq(dev);
353 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
354 uint8_t mac[ETH_ADDR_LEN])
356 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
358 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
364 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
366 const struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
373 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
375 struct netdev_tunnel *dev = netdev_tunnel_cast(netdev);
382 netdev_tunnel_update_flags(struct netdev *dev_,
383 enum netdev_flags off, enum netdev_flags on,
384 enum netdev_flags *old_flagsp)
386 struct netdev_tunnel *netdev =
387 netdev_tunnel_cast(dev_);
389 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
393 // XXX should we actually do something with these flags?
394 *old_flagsp = netdev->flags;
396 netdev->flags &= ~off;
397 if (*old_flagsp != netdev->flags) {
398 netdev_tunnel_update_seq(netdev);
404 netdev_tunnel_change_seq(const struct netdev *netdev)
406 return netdev_tunnel_cast(netdev)->change_seq;
409 /* Helper functions. */
412 netdev_tunnel_update_seq(struct netdev_tunnel *dev)
415 if (!dev->change_seq) {
421 netdev_tunnel_get_port(struct unixctl_conn *conn,
422 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
424 struct netdev_tunnel *tunnel_dev;
427 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
429 unixctl_command_reply_error(conn, "no such tunnel netdev");
433 sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
434 unixctl_command_reply(conn, buf);
438 netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
439 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
441 struct netdev_tunnel *tunnel_dev;
444 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
446 unixctl_command_reply_error(conn, "no such tunnel netdev");
450 sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
451 unixctl_command_reply(conn, buf);
455 netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
456 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
458 struct netdev_tunnel *tunnel_dev;
461 tunnel_dev = shash_find_data(&tunnel_netdevs, argv[1]);
463 unixctl_command_reply_error(conn, "no such tunnel netdev");
467 sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
468 unixctl_command_reply(conn, buf);
473 netdev_tunnel_init(void)
475 unixctl_command_register("netdev-tunnel/get-port", "NAME",
476 1, 1, netdev_tunnel_get_port, NULL);
477 unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
478 1, 1, netdev_tunnel_get_tx_bytes, NULL);
479 unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
480 1, 1, netdev_tunnel_get_rx_bytes, NULL);
484 const struct netdev_class netdev_tunnel_class = {
486 netdev_tunnel_init, /* init */
490 netdev_tunnel_create,
491 netdev_tunnel_destroy,
492 netdev_tunnel_get_config,
493 netdev_tunnel_set_config,
494 NULL, /* get_tunnel_config */
496 netdev_tunnel_rx_open,
499 netdev_tunnel_send_wait,
501 netdev_tunnel_set_etheraddr,
502 netdev_tunnel_get_etheraddr,
505 NULL, /* get_ifindex */
506 NULL, /* get_carrier */
507 NULL, /* get_carrier_resets */
508 NULL, /* get_miimon */
509 netdev_tunnel_get_stats,
510 netdev_tunnel_set_stats,
512 NULL, /* get_features */
513 NULL, /* set_advertisements */
515 NULL, /* set_policing */
516 NULL, /* get_qos_types */
517 NULL, /* get_qos_capabilities */
520 NULL, /* get_queue */
521 NULL, /* set_queue */
522 NULL, /* delete_queue */
523 NULL, /* get_queue_stats */
524 NULL, /* dump_queues */
525 NULL, /* dump_queue_stats */
530 NULL, /* add_router */
531 NULL, /* get_next_hop */
532 NULL, /* get_drv_info */
533 NULL, /* arp_lookup */
535 netdev_tunnel_update_flags,
537 netdev_tunnel_change_seq
541 static const struct netdev_rx_class netdev_rx_tunnel_class = {
542 netdev_rx_tunnel_destroy,
543 netdev_rx_tunnel_recv,
544 netdev_rx_tunnel_wait,
545 netdev_rx_tunnel_drain,