#include "flow.h"
#include "list.h"
+#include "dpif-netdev.h"
#include "netdev-provider.h"
#include "odp-util.h"
#include "ofp-print.h"
VLOG_DEFINE_THIS_MODULE(netdev_pltap);
+/* Protects 'sync_list'. */
+static struct ovs_mutex sync_list_mutex = OVS_MUTEX_INITIALIZER;
+
+static struct list sync_list OVS_GUARDED_BY(sync_list_mutex)
+ = LIST_INITIALIZER(&sync_list);
+
struct netdev_pltap {
struct netdev up;
+
+ /* In sync_list. */
+ struct list sync_list OVS_GUARDED_BY(sync_list_mutex);
+
+ /* Protects all members below. */
+ struct ovs_mutex mutex OVS_ACQ_AFTER(sync_list_mutex);
+
char *real_name;
struct netdev_stats stats;
enum netdev_flags new_flags;
bool valid_local_ip;
bool valid_local_netmask;
bool sync_flags_needed;
- struct list sync_list;
unsigned int change_seq;
};
-static struct list sync_list;
-struct netdev_rx_pltap {
- struct netdev_rx up;
+struct netdev_rxq_pltap {
+ struct netdev_rxq up;
int fd;
};
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
-static struct shash pltap_netdevs = SHASH_INITIALIZER(&pltap_netdevs);
+/* Protects 'pltap_netdevs' */
+static struct ovs_mutex pltap_netdevs_mutex = OVS_MUTEX_INITIALIZER;
+static struct shash pltap_netdevs OVS_GUARDED_BY(pltap_netdevs_mutex)
+ = SHASH_INITIALIZER(&pltap_netdevs);
static int netdev_pltap_construct(struct netdev *netdev_);
-static void netdev_pltap_update_seq(struct netdev_pltap *);
-static int get_flags(struct netdev_pltap *dev, enum netdev_flags *flags);
+static void netdev_pltap_update_seq(struct netdev_pltap *)
+ OVS_REQUIRES(dev->mutex);
+static int get_flags(struct netdev_pltap *dev, enum netdev_flags *flags)
+ OVS_REQUIRES(dev->mutex);
static bool
netdev_pltap_finalized(struct netdev_pltap *dev)
+ OVS_REQUIRES(dev->mutex)
{
return dev->valid_local_ip && dev->valid_local_netmask;
}
return CONTAINER_OF(netdev, struct netdev_pltap, up);
}
-static struct netdev_rx_pltap*
-netdev_rx_pltap_cast(const struct netdev_rx *rx)
+static struct netdev_rxq_pltap*
+netdev_rxq_pltap_cast(const struct netdev_rxq *rx)
{
ovs_assert(is_netdev_pltap_class(netdev_get_class(rx->netdev)));
- return CONTAINER_OF(rx, struct netdev_rx_pltap, up);
+ return CONTAINER_OF(rx, struct netdev_rxq_pltap, up);
}
static void sync_needed(struct netdev_pltap *dev)
+ OVS_REQUIRES(dev->mutex, sync_list_mutex)
{
if (dev->sync_flags_needed)
return;
dev->sync_flags_needed = true;
list_insert(&sync_list, &dev->sync_list);
-
}
static void sync_done(struct netdev_pltap *dev)
+ OVS_REQUIRES(dev->mutex, sync_list_mutex)
{
if (!dev->sync_flags_needed)
return;
struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
int error;
+ ovs_mutex_init(&netdev->mutex);
netdev->real_name = xzalloc(IFNAMSIZ + 1);
memset(&netdev->local_addr, 0, sizeof(netdev->local_addr));
netdev->valid_local_ip = false;
netdev->flags = 0;
netdev->sync_flags_needed = false;
netdev->change_seq = 1;
- list_init(&netdev->sync_list);
/* Open tap device. */
return error;
}
+ ovs_mutex_lock(&pltap_netdevs_mutex);
shash_add(&pltap_netdevs, netdev_get_name(netdev_), netdev);
+ ovs_mutex_unlock(&pltap_netdevs_mutex);
return 0;
}
{
struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
+ ovs_mutex_lock(&pltap_netdevs_mutex);
if (netdev->fd != -1)
close(netdev->fd);
- sync_done(netdev);
+ if (netdev->sync_flags_needed) {
+ ovs_mutex_lock(&sync_list_mutex);
+ (void) list_remove(&netdev->sync_list);
+ ovs_mutex_unlock(&sync_list_mutex);
+ }
shash_find_and_delete(&pltap_netdevs,
netdev_get_name(netdev_));
+ ovs_mutex_unlock(&pltap_netdevs_mutex);
+ ovs_mutex_destroy(&netdev->mutex);
}
static void
free(netdev);
}
-static int netdev_pltap_up(struct netdev_pltap *dev);
+static int netdev_pltap_up(struct netdev_pltap *dev) OVS_REQUIRES(dev->mutex);
-static struct netdev_rx *
-netdev_pltap_rx_alloc(void)
+static struct netdev_rxq *
+netdev_pltap_rxq_alloc(void)
{
- struct netdev_rx_pltap *rx = xzalloc(sizeof *rx);
+ struct netdev_rxq_pltap *rx = xzalloc(sizeof *rx);
return &rx->up;
}
static int
-netdev_pltap_rx_construct(struct netdev_rx *rx_)
+netdev_pltap_rxq_construct(struct netdev_rxq *rx_)
{
- struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
+ struct netdev_rxq_pltap *rx = netdev_rxq_pltap_cast(rx_);
struct netdev *netdev_ = rx->up.netdev;
struct netdev_pltap *netdev =
netdev_pltap_cast(netdev_);
- int error;
+ int error = 0;
+ ovs_mutex_lock(&netdev->mutex);
rx->fd = netdev->fd;
if (!netdev_pltap_finalized(netdev))
- return 0;
+ goto out;
error = netdev_pltap_up(netdev);
if (error) {
- return error;
+ goto out;
}
- return 0;
+out:
+ ovs_mutex_unlock(&netdev->mutex);
+ return error;
}
static void
-netdev_pltap_rx_destruct(struct netdev_rx *rx_ OVS_UNUSED)
+netdev_pltap_rxq_destruct(struct netdev_rxq *rx_ OVS_UNUSED)
{
}
static void
-netdev_pltap_rx_dealloc(struct netdev_rx *rx_)
+netdev_pltap_rxq_dealloc(struct netdev_rxq *rx_)
{
- struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
+ struct netdev_rxq_pltap *rx = netdev_rxq_pltap_cast(rx_);
free(rx);
}
static int
netdev_pltap_up(struct netdev_pltap *dev)
+ OVS_REQUIRES(dev->mutex)
{
if (!netdev_pltap_finalized(dev)) {
return 0;
static int
netdev_pltap_down(struct netdev_pltap *dev)
+ OVS_REQUIRES(dev->mutex)
{
if (!netdev_pltap_finalized(dev)) {
return 0;
static int
netdev_pltap_promisc(struct netdev_pltap *dev, bool promisc)
+ OVS_REQUIRES(dev-mutex)
{
if (!netdev_pltap_finalized(dev)) {
return 0;
static void
netdev_pltap_sync_flags(struct netdev_pltap *dev)
+ OVS_REQUIRES(sync_list_mutex)
{
+ ovs_mutex_lock(&dev->mutex);
+
if (dev->fd < 0 || !netdev_pltap_finalized(dev)) {
- sync_done(dev);
- return;
+ goto out;
}
VLOG_DBG("sync_flags(%s): current: %s %s target: %s %s",
}
netdev_pltap_update_seq(dev);
+
+out:
sync_done(dev);
+ ovs_mutex_unlock(&dev->mutex);
}
{
struct netdev_pltap *netdev = netdev_pltap_cast(dev_);
+ ovs_mutex_lock(&netdev->mutex);
if (netdev->valid_local_ip)
smap_add_format(args, "local_ip", IP_FMT,
IP_ARGS(netdev->local_addr.sin_addr.s_addr));
if (netdev->valid_local_netmask)
smap_add_format(args, "local_netmask", "%"PRIu32,
ntohs(netdev->local_netmask));
+ ovs_mutex_unlock(&netdev->mutex);
return 0;
}
struct netdev_pltap *netdev = netdev_pltap_cast(dev_);
struct shash_node *node;
+ ovs_mutex_lock(&sync_list_mutex);
+ ovs_mutex_lock(&netdev->mutex);
VLOG_DBG("pltap_set_config(%s)", netdev_get_name(dev_));
SMAP_FOR_EACH(node, args) {
VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
netdev->new_flags |= NETDEV_UP;
sync_needed(netdev);
}
+ ovs_mutex_unlock(&netdev->mutex);
+ ovs_mutex_unlock(&sync_list_mutex);
return 0;
}
static int
-netdev_pltap_rx_recv(struct netdev_rx *rx_, void *buffer, size_t size)
+netdev_pltap_rxq_recv(struct netdev_rxq *rx_, struct ofpbuf **packet, int *c)
{
- struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
+ struct netdev_rxq_pltap *rx = netdev_rxq_pltap_cast(rx_);
struct tun_pi pi;
struct iovec iov[2] = {
{ .iov_base = &pi, .iov_len = sizeof(pi) },
- { .iov_base = buffer, .iov_len = size }
};
+ struct ofpbuf *buffer = NULL;
+ size_t size;
+ int error = 0;
+
+ buffer = ofpbuf_new_with_headroom(VLAN_ETH_HEADER_LEN + ETH_PAYLOAD_MAX,
+ DP_NETDEV_HEADROOM);
+ size = ofpbuf_tailroom(buffer);
+ iov[1].iov_base = ofpbuf_data(buffer);
+ iov[1].iov_len = size;
for (;;) {
ssize_t retval;
retval = readv(rx->fd, iov, 2);
if (retval >= 0) {
if (retval <= size) {
- return retval;
+ ofpbuf_set_size(buffer, ofpbuf_size(buffer) + retval);
+ goto out;
} else {
- return -EMSGSIZE;
+ error = EMSGSIZE;
+ goto out;
}
} else if (errno != EINTR) {
if (errno != EAGAIN) {
VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
- netdev_rx_get_name(rx_), ovs_strerror(errno));
+ netdev_rxq_get_name(rx_), ovs_strerror(errno));
}
- return -errno;
+ error = errno;
+ goto out;
}
}
+out:
+ if (error) {
+ ofpbuf_delete(buffer);
+ } else {
+ dp_packet_pad(buffer);
+ packet[0] = buffer;
+ *c = 1;
+ }
+
+ return error;
}
static void
-netdev_pltap_rx_wait(struct netdev_rx *rx_)
+netdev_pltap_rxq_wait(struct netdev_rxq *rx_)
{
- struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
+ struct netdev_rxq_pltap *rx = netdev_rxq_pltap_cast(rx_);
struct netdev_pltap *netdev =
netdev_pltap_cast(rx->up.netdev);
if (rx->fd >= 0 && netdev_pltap_finalized(netdev)) {
}
static int
-netdev_pltap_send(struct netdev *netdev_, const void *buffer, size_t size)
+netdev_pltap_send(struct netdev *netdev_, struct ofpbuf *pkt, bool may_steal)
{
+ const void *buffer = ofpbuf_data(pkt);
+ size_t size = ofpbuf_size(pkt);
struct netdev_pltap *dev =
- netdev_pltap_cast(netdev_);
+ netdev_pltap_cast(netdev_);
+ int error = 0;
struct tun_pi pi = { 0, 0x86 };
struct iovec iov[2] = {
{ .iov_base = &pi, .iov_len = sizeof(pi) },
- { .iov_base = (char*) buffer, .iov_len = size }
+ { .iov_base = (char*) buffer, .iov_len = size }
};
- if (dev->fd < 0)
- return EAGAIN;
+ if (dev->fd < 0) {
+ error = EAGAIN;
+ goto out;
+ }
for (;;) {
ssize_t retval;
retval = writev(dev->fd, iov, 2);
if (retval >= 0) {
- if (retval != size + 4) {
- VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of %zu) on %s",
- retval, size + 4, netdev_get_name(netdev_));
- }
- return 0;
+ if (retval != size + sizeof(pi)) {
+ VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIdSIZE" bytes of %"PRIuSIZE") on %s",
+ retval, size + sizeof(pi), netdev_get_name(netdev_));
+ }
+ goto out;
} else if (errno != EINTR) {
if (errno != EAGAIN) {
VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
- netdev_get_name(netdev_), ovs_strerror(errno));
+ netdev_get_name(netdev_), ovs_strerror(errno));
}
- return errno;
+ error = errno;
+ goto out;
}
}
+out:
+ if (may_steal) {
+ ofpbuf_delete(pkt);
+ }
+ return error;
}
static void
}
static int
-netdev_pltap_rx_drain(struct netdev_rx *rx_)
+netdev_pltap_rxq_drain(struct netdev_rxq *rx_)
{
- struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
+ struct netdev_rxq_pltap *rx = netdev_rxq_pltap_cast(rx_);
char buffer[128];
int error;
// XXX from netdev-linux.c
static int
get_etheraddr(struct netdev_pltap *dev, uint8_t ea[ETH_ADDR_LEN])
+ OVS_REQUIRES(dev->mutex)
{
struct ifreq ifr;
int hwaddr_family;
error = af_inet_ifreq_ioctl(dev->real_name, &ifr,
SIOCGIFHWADDR, "SIOCGIFHWADDR");
if (error) {
- return errno;
+ return error;
}
hwaddr_family = ifr.ifr_hwaddr.sa_family;
if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
static int
get_flags(struct netdev_pltap *dev, enum netdev_flags *flags)
+ OVS_REQUIRES(dev->mutex)
{
struct ifreq ifr;
int error;
{
struct netdev_pltap *dev =
netdev_pltap_cast(netdev);
- if (dev->fd < 0)
- return EAGAIN;
- return get_etheraddr(dev, mac);
+ int error = 0;
+
+ ovs_mutex_lock(&dev->mutex);
+ if (dev->fd < 0) {
+ error = EAGAIN;
+ goto out;
+ }
+ error = get_etheraddr(dev, mac);
+
+out:
+ ovs_mutex_unlock(&dev->mutex);
+ return error;
}
netdev_pltap_cast(dev_);
int error = 0;
+ ovs_mutex_lock(&sync_list_mutex);
+ ovs_mutex_lock(&netdev->mutex);
if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
- return EINVAL;
+ error = EINVAL;
+ goto out;
}
if (netdev_pltap_finalized(netdev)) {
sync_needed(netdev);
}
+out:
+ ovs_mutex_unlock(&netdev->mutex);
+ ovs_mutex_unlock(&sync_list_mutex);
return error;
}
static unsigned int
netdev_pltap_change_seq(const struct netdev *netdev)
{
- return netdev_pltap_cast(netdev)->change_seq;
+ struct netdev_pltap *dev =
+ netdev_pltap_cast(netdev);
+ unsigned int change_seq;
+
+ ovs_mutex_lock(&dev->mutex);
+ change_seq = dev->change_seq;
+ ovs_mutex_unlock(&dev->mutex);
+
+ return change_seq;
}
\f
/* Helper functions. */
static void
netdev_pltap_update_seq(struct netdev_pltap *dev)
+ OVS_REQUIRES(dev->mutex)
{
dev->change_seq++;
if (!dev->change_seq) {
{
struct netdev_pltap *pltap_dev;
+ ovs_mutex_lock(&pltap_netdevs_mutex);
pltap_dev = shash_find_data(&pltap_netdevs, argv[1]);
if (!pltap_dev) {
unixctl_command_reply_error(conn, "no such pltap netdev");
- return;
+ goto out;
}
if (pltap_dev->fd < 0) {
unixctl_command_reply_error(conn, "no real device attached");
- return;
+ goto out;
}
unixctl_command_reply(conn, pltap_dev->real_name);
+
+out:
+ ovs_mutex_unlock(&pltap_netdevs_mutex);
}
static int
netdev_pltap_init(void)
{
- list_init(&sync_list);
unixctl_command_register("netdev-pltap/get-tapname", "port",
1, 1, netdev_pltap_get_real_name, NULL);
return 0;
netdev_pltap_run(void)
{
struct netdev_pltap *iter, *next;
+ ovs_mutex_lock(&sync_list_mutex);
LIST_FOR_EACH_SAFE(iter, next, sync_list, &sync_list) {
netdev_pltap_sync_flags(iter);
}
+ ovs_mutex_unlock(&sync_list_mutex);
}
static void
netdev_pltap_wait(void)
{
+ ovs_mutex_lock(&sync_list_mutex);
if (!list_is_empty(&sync_list)) {
VLOG_DBG("netdev_pltap: scheduling sync");
poll_immediate_wake();
}
+ ovs_mutex_unlock(&sync_list_mutex);
}
const struct netdev_class netdev_pltap_class = {
NULL, /* set_queue */
NULL, /* delete_queue */
NULL, /* get_queue_stats */
- NULL, /* dump_queues */
+ NULL, /* queue_dump_start */
+ NULL, /* queue_dump_next */
+ NULL, /* queue_dump_done */
NULL, /* dump_queue_stats */
NULL, /* get_in4 */
netdev_pltap_update_flags,
- netdev_pltap_change_seq,
-
- netdev_pltap_rx_alloc,
- netdev_pltap_rx_construct,
- netdev_pltap_rx_destruct,
- netdev_pltap_rx_dealloc,
- netdev_pltap_rx_recv,
- netdev_pltap_rx_wait,
- netdev_pltap_rx_drain,
+ netdev_pltap_rxq_alloc,
+ netdev_pltap_rxq_construct,
+ netdev_pltap_rxq_destruct,
+ netdev_pltap_rxq_dealloc,
+ netdev_pltap_rxq_recv,
+ netdev_pltap_rxq_wait,
+ netdev_pltap_rxq_drain,
};