2 * Copyright (c) 2012 Giuseppe Lettieri
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.
19 #include <sys/types.h>
22 #include <arpa/inet.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
26 #include <net/if_arp.h>
27 #include <linux/if_tun.h>
28 #include <netinet/in.h>
33 #include "netdev-provider.h"
35 #include "ofp-print.h"
38 #include "poll-loop.h"
42 #include "socket-util.h"
46 VLOG_DEFINE_THIS_MODULE(netdev_pltap);
48 struct netdev_dev_pltap {
49 struct netdev_dev netdev_dev;
52 struct netdev_stats stats;
53 enum netdev_flags flags;
55 struct sockaddr_in local_addr;
58 bool valid_local_netmask;
60 unsigned int change_seq;
67 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
69 static struct shash pltap_netdev_devs = SHASH_INITIALIZER(&pltap_netdev_devs);
71 static int netdev_pltap_create(const struct netdev_class *, const char *,
72 struct netdev_dev **);
74 static struct shash pltap_creating = SHASH_INITIALIZER(&pltap_creating);
76 static void netdev_pltap_update_seq(struct netdev_dev_pltap *);
79 is_pltap_class(const struct netdev_class *class)
81 return class->create == netdev_pltap_create;
84 static struct netdev_dev_pltap *
85 netdev_dev_pltap_cast(const struct netdev_dev *netdev_dev)
87 assert(is_pltap_class(netdev_dev_get_class(netdev_dev)));
88 return CONTAINER_OF(netdev_dev, struct netdev_dev_pltap, netdev_dev);
91 static struct netdev_pltap *
92 netdev_pltap_cast(const struct netdev *netdev)
94 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
95 assert(is_pltap_class(netdev_dev_get_class(netdev_dev)));
96 return CONTAINER_OF(netdev, struct netdev_pltap, netdev);
100 netdev_pltap_create(const struct netdev_class *class OVS_UNUSED, const char *name,
101 struct netdev_dev **netdev_devp)
103 struct netdev_dev_pltap *netdev_dev;
106 netdev_dev = xzalloc(sizeof *netdev_dev);
108 netdev_dev->real_name = xzalloc(IFNAMSIZ + 1);
109 netdev_dev->error = NULL;
110 memset(&netdev_dev->local_addr, 0, sizeof(netdev_dev->local_addr));
111 netdev_dev->valid_local_ip = false;
112 netdev_dev->valid_local_netmask = false;
113 netdev_dev->finalized = false;
116 /* Open tap device. */
117 netdev_dev->fd = tun_alloc(IFF_TAP, netdev_dev->real_name);
118 if (netdev_dev->fd < 0) {
120 VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s", name, strerror(error));
123 VLOG_DBG("real_name = %s", netdev_dev->real_name);
125 /* Make non-blocking. */
126 error = set_nonblocking(netdev_dev->fd);
131 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_pltap_class);
132 shash_add(&pltap_netdev_devs, name, netdev_dev);
133 *netdev_devp = &netdev_dev->netdev_dev;
142 netdev_pltap_destroy(struct netdev_dev *netdev_dev_)
144 struct netdev_dev_pltap *netdev_dev = netdev_dev_pltap_cast(netdev_dev_);
146 if (netdev_dev->fd != -1)
147 close(netdev_dev->fd);
149 shash_find_and_delete(&pltap_netdev_devs,
150 netdev_dev_get_name(netdev_dev_));
155 netdev_pltap_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
157 struct netdev_pltap *netdev;
159 netdev = xmalloc(sizeof *netdev);
160 netdev_init(&netdev->netdev, netdev_dev_);
162 *netdevp = &netdev->netdev;
167 netdev_pltap_close(struct netdev *netdev_)
169 struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
174 netdev_pltap_create_finalize(struct netdev_dev_pltap *dev)
176 int ifd = -1, ofd = -1, maxfd;
177 size_t bytes_to_write, bytes_to_read = 1024,
178 bytes_written = 0, bytes_read = 0;
180 char *msg = NULL, *reply = NULL;
184 if (!dev->valid_local_ip || !dev->valid_local_netmask)
187 ofd = open("/vsys/vif_up.out", O_RDONLY | O_NONBLOCK);
189 VLOG_ERR("Cannot open vif_up.out: %s", strerror(errno));
193 ifd = open("/vsys/vif_up.in", O_WRONLY | O_NONBLOCK);
195 VLOG_ERR("Cannot open vif_up.in: %s", strerror(errno));
199 maxfd = (ifd < ofd) ? ofd : ifd;
201 msg = xasprintf("%s\n"IP_FMT"\n%d\n",
203 IP_ARGS(&dev->local_addr.sin_addr),
205 reply = (char*)xmalloc(bytes_to_read);
206 if (!msg || !reply) {
207 VLOG_ERR("Out of memory");
211 bytes_to_write = strlen(msg);
212 while (bytes_to_write || bytes_to_read) {
213 fd_set readset, writeset, errorset;
218 if (bytes_to_write) {
219 FD_SET(ifd, &writeset);
220 FD_SET(ifd, &errorset);
222 FD_SET(ofd, &readset);
223 FD_SET(ofd, &errorset);
224 if (select(maxfd + 1, &readset, &writeset, &errorset, NULL) < 0) {
227 VLOG_ERR("selec error: %s", strerror(errno));
231 if (FD_ISSET(ifd, &errorset) || FD_ISSET(ofd, &errorset)) {
232 VLOG_ERR("error condition on ifd or ofd");
235 if (FD_ISSET(ifd, &writeset)) {
236 ssize_t n = write(ifd, msg + bytes_written, bytes_to_write);
238 if (errno != EAGAIN && errno != EINTR) {
239 VLOG_ERR("write on vif_up.in: %s", strerror(errno));
246 if (bytes_to_write == 0)
250 if (FD_ISSET(ofd, &readset)) {
251 ssize_t n = read(ofd, reply + bytes_read, bytes_to_read);
253 if (errno != EAGAIN && errno != EINTR) {
254 VLOG_ERR("read on vif_up.out: %s", strerror(errno));
267 reply[bytes_read] = '\0';
268 VLOG_ERR("vif_up returned: %s", reply);
274 dev->finalized = true;
277 netdev_pltap_update_seq(dev);
288 netdev_pltap_get_config(struct netdev_dev *dev_, struct smap *args)
290 struct netdev_dev_pltap *netdev_dev = netdev_dev_pltap_cast(dev_);
292 if (netdev_dev->valid_local_ip)
293 smap_add_format(args, "local_ip", IP_FMT,
294 IP_ARGS(&netdev_dev->local_addr.sin_addr));
295 if (netdev_dev->valid_local_netmask)
296 smap_add_format(args, "local_netmask", "%"PRIu32,
297 ntohs(netdev_dev->local_netmask));
298 return netdev_pltap_create_finalize(netdev_dev);
302 netdev_pltap_set_config(struct netdev_dev *dev_, const struct smap *args)
304 struct netdev_dev_pltap *netdev_dev = netdev_dev_pltap_cast(dev_);
305 struct shash_node *node;
307 VLOG_DBG("pltap_set_config(%s)", netdev_dev_get_name(dev_));
308 SMAP_FOR_EACH(node, args) {
309 VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
310 if (!strcmp(node->name, "local_ip")) {
312 if (lookup_ip(node->data, &addr)) {
313 VLOG_WARN("%s: bad 'local_ip'", node->name);
315 netdev_dev->local_addr.sin_addr = addr;
316 netdev_dev->valid_local_ip = true;
318 } else if (!strcmp(node->name, "local_netmask")) {
319 netdev_dev->local_netmask = atoi(node->data);
320 // XXX check valididy
321 netdev_dev->valid_local_netmask = true;
323 VLOG_WARN("%s: unknown argument '%s'",
324 netdev_dev_get_name(dev_), node->name);
327 return netdev_pltap_create_finalize(netdev_dev);
331 netdev_pltap_listen(struct netdev *netdev_ OVS_UNUSED)
337 netdev_pltap_recv(struct netdev *netdev_, void *buffer, size_t size)
339 struct netdev_dev_pltap *dev =
340 netdev_dev_pltap_cast(netdev_get_dev(netdev_));
342 struct iovec iov[2] = {
343 { .iov_base = prefix, .iov_len = 4 },
344 { .iov_base = buffer, .iov_len = size }
350 retval = readv(dev->fd, iov, 2);
352 if (retval <= size) {
357 } else if (errno != EINTR) {
358 if (errno != EAGAIN) {
359 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
360 netdev_get_name(netdev_), strerror(errno));
368 netdev_pltap_recv_wait(struct netdev *netdev_)
370 struct netdev_dev_pltap *dev =
371 netdev_dev_pltap_cast(netdev_get_dev(netdev_));
372 if (dev->finalized && dev->fd >= 0) {
373 poll_fd_wait(dev->fd, POLLIN);
378 netdev_pltap_send(struct netdev *netdev_, const void *buffer, size_t size)
380 struct netdev_dev_pltap *dev =
381 netdev_dev_pltap_cast(netdev_get_dev(netdev_));
382 char prefix[4] = { 0, 0, 8, 6 };
383 struct iovec iov[2] = {
384 { .iov_base = prefix, .iov_len = 4 },
385 { .iov_base = buffer, .iov_len = size }
387 if (dev->fd < 0 || !dev->finalized)
391 retval = writev(dev->fd, iov, 2);
393 if (retval != size + 4) {
394 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of %zu) on %s",
395 retval, size + 4, netdev_get_name(netdev_));
398 } else if (errno != EINTR) {
399 if (errno != EAGAIN) {
400 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
401 netdev_get_name(netdev_), strerror(errno));
409 netdev_pltap_send_wait(struct netdev *netdev_)
411 struct netdev_dev_pltap *dev =
412 netdev_dev_pltap_cast(netdev_get_dev(netdev_));
413 if (dev->finalized && dev->fd >= 0) {
414 poll_fd_wait(dev->fd, POLLOUT);
419 netdev_pltap_drain(struct netdev *netdev_)
421 struct netdev_dev_pltap *dev =
422 netdev_dev_pltap_cast(netdev_get_dev(netdev_));
426 if (dev->fd < 0 || !dev->finalized)
429 error = recv(dev->fd, buffer, 128, MSG_TRUNC);
431 if (error == -EAGAIN)
433 else if (error != -EMSGSIZE)
441 netdev_pltap_set_etheraddr(struct netdev *netdevi OVS_UNUSED,
442 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
447 // XXX from netdev-linux.c
449 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
455 /* Create AF_INET socket. */
456 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
457 if (af_inet_sock < 0) {
458 VLOG_ERR("failed to create inet socket: %s", strerror(errno));
461 memset(&ifr, 0, sizeof ifr);
462 ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
463 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
464 /* ENODEV probably means that a vif disappeared asynchronously and
465 * hasn't been removed from the database yet, so reduce the log level
466 * to INFO for that case. */
467 VLOG(errno == ENODEV ? VLL_INFO : VLL_ERR,
468 "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
469 netdev_name, strerror(errno));
473 hwaddr_family = ifr.ifr_hwaddr.sa_family;
474 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
475 VLOG_WARN("%s device has unknown hardware address family %d",
476 netdev_name, hwaddr_family);
478 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
484 netdev_pltap_get_etheraddr(const struct netdev *netdev,
485 uint8_t mac[ETH_ADDR_LEN])
487 struct netdev_dev_pltap *dev =
488 netdev_dev_pltap_cast(netdev_get_dev(netdev));
489 if (dev->fd < 0 || !dev->finalized)
491 return get_etheraddr(dev->real_name, mac);
495 // XXX can we read stats in planetlab?
497 netdev_pltap_get_stats(const struct netdev *netdev OVS_UNUSED, struct netdev_stats *stats OVS_UNUSED)
503 netdev_pltap_set_stats(struct netdev *netdev OVS_UNUSED, const struct netdev_stats *stats OVS_UNUSED)
509 netdev_pltap_update_flags(struct netdev *netdev,
510 enum netdev_flags off, enum netdev_flags on,
511 enum netdev_flags *old_flagsp)
513 struct netdev_dev_pltap *dev =
514 netdev_dev_pltap_cast(netdev_get_dev(netdev));
516 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
520 // XXX should we actually do something with these flags?
521 *old_flagsp = dev->flags;
524 if (*old_flagsp != dev->flags) {
525 netdev_pltap_update_seq(dev);
531 netdev_pltap_change_seq(const struct netdev *netdev)
533 return netdev_dev_pltap_cast(netdev_get_dev(netdev))->change_seq;
536 /* Helper functions. */
539 netdev_pltap_update_seq(struct netdev_dev_pltap *dev)
542 if (!dev->change_seq) {
548 netdev_pltap_get_real_name(struct unixctl_conn *conn,
549 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
551 struct netdev_dev_pltap *pltap_dev;
553 pltap_dev = shash_find_data(&pltap_netdev_devs, argv[1]);
555 unixctl_command_reply_error(conn, "no such pltap netdev");
558 if (pltap_dev->error) {
559 unixctl_command_reply_error(conn, pltap_dev->error);
563 unixctl_command_reply(conn, pltap_dev->real_name);
567 netdev_pltap_init(void)
569 unixctl_command_register("netdev-pltap/get-tapname", "port",
570 1, 1, netdev_pltap_get_real_name, NULL);
574 const struct netdev_class netdev_pltap_class = {
581 netdev_pltap_destroy,
582 netdev_pltap_get_config,
583 netdev_pltap_set_config,
590 netdev_pltap_recv_wait,
594 netdev_pltap_send_wait,
596 netdev_pltap_set_etheraddr,
597 netdev_pltap_get_etheraddr,
600 NULL, /* get_ifindex */
601 NULL, /* get_carrier */
602 NULL, /* get_carrier_resets */
603 NULL, /* get_miimon */
604 netdev_pltap_get_stats,
605 netdev_pltap_set_stats,
607 NULL, /* get_features */
608 NULL, /* set_advertisements */
610 NULL, /* set_policing */
611 NULL, /* get_qos_types */
612 NULL, /* get_qos_capabilities */
615 NULL, /* get_queue */
616 NULL, /* set_queue */
617 NULL, /* delete_queue */
618 NULL, /* get_queue_stats */
619 NULL, /* dump_queues */
620 NULL, /* dump_queue_stats */
625 NULL, /* add_router */
626 NULL, /* get_next_hop */
627 NULL, /* get_drv_info */
628 NULL, /* arp_lookup */
630 netdev_pltap_update_flags,
632 netdev_pltap_change_seq