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);
51 struct netdev_stats stats;
52 enum netdev_flags new_flags;
53 enum netdev_flags flags;
55 struct sockaddr_in local_addr;
58 bool valid_local_netmask;
59 bool sync_flags_needed;
60 struct list sync_list;
61 unsigned int change_seq;
64 static const struct netdev_rx_class netdev_rx_pltap_class;
66 static struct list sync_list;
68 struct netdev_rx_pltap {
73 static int af_inet_sock = -1;
75 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
77 static struct shash pltap_netdevs = SHASH_INITIALIZER(&pltap_netdevs);
79 static int netdev_pltap_create(const struct netdev_class *, const char *,
82 static void netdev_pltap_update_seq(struct netdev_pltap *);
83 static int get_flags(struct netdev_pltap *dev, enum netdev_flags *flags);
86 netdev_pltap_finalized(struct netdev_pltap *dev)
88 return dev->valid_local_ip && dev->valid_local_netmask;
92 is_netdev_pltap_class(const struct netdev_class *class)
94 return class->create == netdev_pltap_create;
97 static struct netdev_pltap *
98 netdev_pltap_cast(const struct netdev *netdev)
100 ovs_assert(is_netdev_pltap_class(netdev_get_class(netdev)));
101 return CONTAINER_OF(netdev, struct netdev_pltap, up);
104 static struct netdev_rx_pltap*
105 netdev_rx_pltap_cast(const struct netdev_rx *rx)
107 netdev_rx_assert_class(rx, &netdev_rx_pltap_class);
108 return CONTAINER_OF(rx, struct netdev_rx_pltap, up);
111 static void sync_needed(struct netdev_pltap *dev)
113 if (dev->sync_flags_needed)
116 dev->sync_flags_needed = true;
117 list_insert(&sync_list, &dev->sync_list);
121 static void sync_done(struct netdev_pltap *dev)
123 if (!dev->sync_flags_needed)
126 (void) list_remove(&dev->sync_list);
127 dev->sync_flags_needed = false;
131 netdev_pltap_create(const struct netdev_class *class OVS_UNUSED, const char *name,
132 struct netdev **netdevp)
134 struct netdev_pltap *netdev;
137 netdev = xzalloc(sizeof *netdev);
139 netdev->real_name = xzalloc(IFNAMSIZ + 1);
140 memset(&netdev->local_addr, 0, sizeof(netdev->local_addr));
141 netdev->valid_local_ip = false;
142 netdev->valid_local_netmask = false;
144 netdev->sync_flags_needed = false;
145 list_init(&netdev->sync_list);
148 /* Open tap device. */
149 netdev->fd = tun_alloc(IFF_TAP, netdev->real_name);
150 if (netdev->fd < 0) {
152 VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s", name, ovs_strerror(error));
155 VLOG_DBG("real_name = %s", netdev->real_name);
157 /* Make non-blocking. */
158 error = set_nonblocking(netdev->fd);
163 netdev_init(&netdev->up, name, &netdev_pltap_class);
164 shash_add(&pltap_netdevs, name, netdev);
165 *netdevp = &netdev->up;
174 netdev_pltap_destroy(struct netdev *netdev_)
176 struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
178 if (netdev->fd != -1)
183 shash_find_and_delete(&pltap_netdevs,
184 netdev_get_name(netdev_));
188 static int netdev_pltap_up(struct netdev_pltap *dev);
191 netdev_pltap_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
193 struct netdev_pltap *netdev =
194 netdev_pltap_cast(netdev_);
195 struct netdev_rx_pltap *rx;
198 rx = xmalloc(sizeof *rx);
199 netdev_rx_init(&rx->up, netdev_, &netdev_rx_pltap_class);
202 if (!netdev_pltap_finalized(netdev))
204 err = netdev_pltap_up(netdev);
214 netdev_rx_pltap_destroy(struct netdev_rx *rx_)
216 struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
220 static int vsys_transaction(const char *script,
221 const char **preply, char *format, ...)
223 char *msg = NULL, *reply = NULL;
224 const size_t reply_size = 1024;
225 int ifd = -1, ofd = -1, maxfd;
226 size_t bytes_to_write, bytes_to_read,
227 bytes_written = 0, bytes_read = 0;
229 char *ofname = NULL, *ifname = NULL;
232 va_start(args, format);
233 msg = xvasprintf(format, args);
235 reply = (char*)xmalloc(reply_size);
236 if (!msg || !reply) {
237 VLOG_ERR("Out of memory");
242 ofname = xasprintf("/vsys/%s.out", script);
243 ifname = xasprintf("/vsys/%s.in", script);
244 if (!ofname || !ifname) {
245 VLOG_ERR("Out of memory");
250 ofd = open(ofname, O_RDONLY | O_NONBLOCK);
252 VLOG_ERR("Cannot open %s: %s", ofname, ovs_strerror(errno));
256 ifd = open(ifname, O_WRONLY | O_NONBLOCK);
258 VLOG_ERR("Cannot open %s: %s", ifname, ovs_strerror(errno));
262 maxfd = (ifd < ofd) ? ofd : ifd;
264 bytes_to_write = strlen(msg);
265 bytes_to_read = reply_size;
266 while (bytes_to_write || bytes_to_read) {
267 fd_set readset, writeset, errorset;
272 if (bytes_to_write) {
273 FD_SET(ifd, &writeset);
274 FD_SET(ifd, &errorset);
276 FD_SET(ofd, &readset);
277 FD_SET(ofd, &errorset);
278 if (select(maxfd + 1, &readset, &writeset, &errorset, NULL) < 0) {
281 VLOG_ERR("selec error: %s", ovs_strerror(errno));
285 if (FD_ISSET(ifd, &errorset) || FD_ISSET(ofd, &errorset)) {
286 VLOG_ERR("error condition on ifd or ofd");
289 if (FD_ISSET(ifd, &writeset)) {
290 ssize_t n = write(ifd, msg + bytes_written, bytes_to_write);
292 if (errno != EAGAIN && errno != EINTR) {
293 VLOG_ERR("write on %s: %s", ifname, ovs_strerror(errno));
300 if (bytes_to_write == 0)
304 if (FD_ISSET(ofd, &readset)) {
305 ssize_t n = read(ofd, reply + bytes_read, bytes_to_read);
307 if (errno != EAGAIN && errno != EINTR) {
308 VLOG_ERR("read on %s: %s", ofname, ovs_strerror(errno));
321 reply[bytes_read] = '\0';
324 reply = NULL; /* prevent freeing the reply msg */
326 VLOG_ERR("%s returned: %s", script, reply);
343 netdev_pltap_up(struct netdev_pltap *dev)
345 if (!netdev_pltap_finalized(dev)) {
349 return vsys_transaction("vif_up", NULL, "%s\n"IP_FMT"\n%d\n",
351 IP_ARGS(dev->local_addr.sin_addr.s_addr),
356 netdev_pltap_down(struct netdev_pltap *dev)
358 if (!netdev_pltap_finalized(dev)) {
362 return vsys_transaction("vif_down", NULL, "%s\n", dev->real_name);
366 netdev_pltap_promisc(struct netdev_pltap *dev, bool promisc)
368 if (!netdev_pltap_finalized(dev)) {
372 return vsys_transaction("promisc", NULL, "%s\n%s",
374 (promisc ? "" : "-\n"));
378 netdev_pltap_sync_flags(struct netdev_pltap *dev)
381 if (dev->fd < 0 || !netdev_pltap_finalized(dev)) {
386 VLOG_DBG("sync_flags(%s): current: %s %s target: %s %s",
388 (dev->flags & NETDEV_UP ? "UP" : "-"),
389 (dev->flags & NETDEV_PROMISC ? "PROMISC" : "-"),
390 (dev->new_flags & NETDEV_UP ? "UP" : "-"),
391 (dev->new_flags & NETDEV_PROMISC ? "PROMISC" : "-"));
393 if ((dev->new_flags & NETDEV_UP) && !(dev->flags & NETDEV_UP)) {
394 (void) netdev_pltap_up(dev);
395 } else if (!(dev->new_flags & NETDEV_UP) && (dev->flags & NETDEV_UP)) {
396 (void) netdev_pltap_down(dev);
399 if ((dev->new_flags & NETDEV_PROMISC) ^ (dev->flags & NETDEV_PROMISC)) {
400 (void) netdev_pltap_promisc(dev, dev->new_flags & NETDEV_PROMISC);
403 netdev_pltap_update_seq(dev);
409 netdev_pltap_get_config(const struct netdev *dev_, struct smap *args)
411 struct netdev_pltap *netdev = netdev_pltap_cast(dev_);
413 if (netdev->valid_local_ip)
414 smap_add_format(args, "local_ip", IP_FMT,
415 IP_ARGS(netdev->local_addr.sin_addr.s_addr));
416 if (netdev->valid_local_netmask)
417 smap_add_format(args, "local_netmask", "%"PRIu32,
418 ntohs(netdev->local_netmask));
423 netdev_pltap_set_config(struct netdev *dev_, const struct smap *args)
425 struct netdev_pltap *netdev = netdev_pltap_cast(dev_);
426 struct shash_node *node;
428 VLOG_DBG("pltap_set_config(%s)", netdev_get_name(dev_));
429 SMAP_FOR_EACH(node, args) {
430 VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
431 if (!strcmp(node->name, "local_ip")) {
433 if (lookup_ip(node->data, &addr)) {
434 VLOG_WARN("%s: bad 'local_ip'", node->name);
436 netdev->local_addr.sin_addr = addr;
437 netdev->valid_local_ip = true;
439 } else if (!strcmp(node->name, "local_netmask")) {
440 netdev->local_netmask = atoi(node->data);
441 // XXX check valididy
442 netdev->valid_local_netmask = true;
444 VLOG_WARN("%s: unknown argument '%s'",
445 netdev_get_name(dev_), node->name);
448 if (netdev_pltap_finalized(netdev)) {
449 netdev->new_flags |= NETDEV_UP;
456 netdev_rx_pltap_recv(struct netdev_rx *rx_, void *buffer, size_t size)
458 struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
460 struct iovec iov[2] = {
461 { .iov_base = &pi, .iov_len = sizeof(pi) },
462 { .iov_base = buffer, .iov_len = size }
466 retval = readv(rx->fd, iov, 2);
468 if (retval <= size) {
473 } else if (errno != EINTR) {
474 if (errno != EAGAIN) {
475 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
476 netdev_rx_get_name(rx_), ovs_strerror(errno));
484 netdev_rx_pltap_wait(struct netdev_rx *rx_)
486 struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
487 struct netdev_pltap *netdev =
488 netdev_pltap_cast(rx->up.netdev);
489 if (rx->fd >= 0 && netdev_pltap_finalized(netdev)) {
490 poll_fd_wait(rx->fd, POLLIN);
495 netdev_pltap_send(struct netdev *netdev_, const void *buffer, size_t size)
497 struct netdev_pltap *dev =
498 netdev_pltap_cast(netdev_);
499 struct tun_pi pi = { 0, 0x86 };
500 struct iovec iov[2] = {
501 { .iov_base = &pi, .iov_len = sizeof(pi) },
502 { .iov_base = (char*) buffer, .iov_len = size }
508 retval = writev(dev->fd, iov, 2);
510 if (retval != size + 4) {
511 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of %zu) on %s",
512 retval, size + 4, netdev_get_name(netdev_));
515 } else if (errno != EINTR) {
516 if (errno != EAGAIN) {
517 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
518 netdev_get_name(netdev_), ovs_strerror(errno));
526 netdev_pltap_send_wait(struct netdev *netdev_)
528 struct netdev_pltap *dev =
529 netdev_pltap_cast(netdev_);
530 if (dev->fd >= 0 && netdev_pltap_finalized(dev)) {
531 poll_fd_wait(dev->fd, POLLOUT);
536 netdev_rx_pltap_drain(struct netdev_rx *rx_)
538 struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
545 error = recv(rx->fd, buffer, 128, MSG_TRUNC);
547 if (error == -EAGAIN)
549 else if (error != -EMSGSIZE)
557 netdev_pltap_set_etheraddr(struct netdev *netdevi OVS_UNUSED,
558 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
564 // XXX from netdev-linux.c
566 get_etheraddr(struct netdev_pltap *dev, uint8_t ea[ETH_ADDR_LEN])
571 memset(&ifr, 0, sizeof ifr);
572 ovs_strzcpy(ifr.ifr_name, dev->real_name, sizeof ifr.ifr_name);
573 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
574 /* ENODEV probably means that a vif disappeared asynchronously and
575 * hasn't been removed from the database yet, so reduce the log level
576 * to INFO for that case. */
577 VLOG(errno == ENODEV ? VLL_INFO : VLL_ERR,
578 "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
579 dev->real_name, ovs_strerror(errno));
582 hwaddr_family = ifr.ifr_hwaddr.sa_family;
583 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
584 VLOG_WARN("%s device has unknown hardware address family %d",
585 dev->real_name, hwaddr_family);
587 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
592 get_flags(struct netdev_pltap *dev, enum netdev_flags *flags)
596 memset(&ifr, 0, sizeof ifr);
597 ovs_strzcpy(ifr.ifr_name, dev->real_name, sizeof ifr.ifr_name);
598 if (ioctl(af_inet_sock, SIOCGIFFLAGS, &ifr) < 0)
601 if (ifr.ifr_flags & IFF_UP)
603 if (ifr.ifr_flags & IFF_PROMISC)
604 *flags |= NETDEV_PROMISC;
609 netdev_pltap_get_etheraddr(const struct netdev *netdev,
610 uint8_t mac[ETH_ADDR_LEN])
612 struct netdev_pltap *dev =
613 netdev_pltap_cast(netdev);
616 return get_etheraddr(dev, mac);
620 // XXX can we read stats in planetlab?
622 netdev_pltap_get_stats(const struct netdev *netdev OVS_UNUSED, struct netdev_stats *stats OVS_UNUSED)
628 netdev_pltap_set_stats(struct netdev *netdev OVS_UNUSED, const struct netdev_stats *stats OVS_UNUSED)
635 netdev_pltap_update_flags(struct netdev *dev_,
636 enum netdev_flags off, enum netdev_flags on,
637 enum netdev_flags *old_flagsp)
639 struct netdev_pltap *netdev =
640 netdev_pltap_cast(dev_);
643 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
647 if (netdev_pltap_finalized(netdev)) {
648 error = get_flags(netdev, &netdev->flags);
650 *old_flagsp = netdev->flags;
651 netdev->new_flags |= on;
652 netdev->new_flags &= ~off;
653 if (netdev->flags != netdev->new_flags) {
654 /* we cannot sync here, since we may be in a signal handler */
662 netdev_pltap_change_seq(const struct netdev *netdev)
664 return netdev_pltap_cast(netdev)->change_seq;
667 /* Helper functions. */
670 netdev_pltap_update_seq(struct netdev_pltap *dev)
673 if (!dev->change_seq) {
679 netdev_pltap_get_real_name(struct unixctl_conn *conn,
680 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
682 struct netdev_pltap *pltap_dev;
684 pltap_dev = shash_find_data(&pltap_netdevs, argv[1]);
686 unixctl_command_reply_error(conn, "no such pltap netdev");
689 if (pltap_dev->fd < 0) {
690 unixctl_command_reply_error(conn, "no real device attached");
694 unixctl_command_reply(conn, pltap_dev->real_name);
698 netdev_pltap_init(void)
700 list_init(&sync_list);
701 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
702 if (af_inet_sock < 0) {
703 VLOG_ERR("failed to create inet socket: %s", ovs_strerror(errno));
705 unixctl_command_register("netdev-pltap/get-tapname", "port",
706 1, 1, netdev_pltap_get_real_name, NULL);
711 netdev_pltap_run(void)
713 struct netdev_pltap *iter, *next;
714 LIST_FOR_EACH_SAFE(iter, next, sync_list, &sync_list) {
715 netdev_pltap_sync_flags(iter);
720 netdev_pltap_wait(void)
722 if (!list_is_empty(&sync_list)) {
723 VLOG_DBG("netdev_pltap: scheduling sync");
724 poll_immediate_wake();
728 const struct netdev_class netdev_pltap_class = {
735 netdev_pltap_destroy,
736 netdev_pltap_get_config,
737 netdev_pltap_set_config,
738 NULL, /* get_tunnel_config */
740 netdev_pltap_rx_open,
743 netdev_pltap_send_wait,
745 netdev_pltap_set_etheraddr,
746 netdev_pltap_get_etheraddr,
749 NULL, /* get_ifindex */
750 NULL, /* get_carrier */
751 NULL, /* get_carrier_resets */
752 NULL, /* get_miimon */
753 netdev_pltap_get_stats,
754 netdev_pltap_set_stats,
756 NULL, /* get_features */
757 NULL, /* set_advertisements */
759 NULL, /* set_policing */
760 NULL, /* get_qos_types */
761 NULL, /* get_qos_capabilities */
764 NULL, /* get_queue */
765 NULL, /* set_queue */
766 NULL, /* delete_queue */
767 NULL, /* get_queue_stats */
768 NULL, /* dump_queues */
769 NULL, /* dump_queue_stats */
774 NULL, /* add_router */
775 NULL, /* get_next_hop */
776 NULL, /* get_drv_info */
777 NULL, /* arp_lookup */
779 netdev_pltap_update_flags,
781 netdev_pltap_change_seq
784 static const struct netdev_rx_class netdev_rx_pltap_class = {
785 netdev_rx_pltap_destroy,
786 netdev_rx_pltap_recv,
787 netdev_rx_pltap_wait,
788 netdev_rx_pltap_drain,