2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-19 14:37:21 +00:00

poll-loop: Port to Windows.

Use WaitForMultipleObjects for polling on windows.  This works on all kinds
of objects, e.g. sockets, files, especially ioctl calls to the kernel.
poll_fd_wait_event() is used if events need to be passed to pollfds.  latch
is signaled with event, to be waited/polled by WaitForMultipleObjects() as
well.  Changed array of fds to hmap to check for duplicate fds.

Signed-off-by: Linda Sun <lsun@vmware.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
This commit is contained in:
Linda Sun
2014-01-09 16:26:12 -08:00
committed by Ben Pfaff
parent d103f479c6
commit 4ca828d713
9 changed files with 231 additions and 42 deletions

View File

@@ -55,4 +55,8 @@ typedef uint32_t __bitwise__ __be32;
typedef uint64_t __bitwise__ __be64; typedef uint64_t __bitwise__ __be64;
#endif /* no <linux/types.h> */ #endif /* no <linux/types.h> */
#ifndef _WIN32
typedef __u32 HANDLE;
#endif
#endif /* <linux/types.h> */ #endif /* <linux/types.h> */

View File

@@ -81,7 +81,6 @@ lib_libopenvswitch_la_SOURCES = \
lib/jsonrpc.h \ lib/jsonrpc.h \
lib/lacp.c \ lib/lacp.c \
lib/lacp.h \ lib/lacp.h \
lib/latch.c \
lib/latch.h \ lib/latch.h \
lib/learn.c \ lib/learn.c \
lib/learn.h \ lib/learn.h \
@@ -237,6 +236,12 @@ lib_libopenvswitch_la_SOURCES = \
lib/vswitch-idl.h \ lib/vswitch-idl.h \
lib/vtep-idl.c \ lib/vtep-idl.c \
lib/vtep-idl.h lib/vtep-idl.h
if WIN32
lib_libopenvswitch_la_SOURCES += lib/latch-windows.c
else
lib_libopenvswitch_la_SOURCES += lib/latch.c
endif
EXTRA_DIST += \ EXTRA_DIST += \
lib/stdio.h.in \ lib/stdio.h.in \
lib/string.h.in lib/string.h.in

83
lib/latch-windows.c Normal file
View File

@@ -0,0 +1,83 @@
/*
* Copyright (c) 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "latch.h"
#include <errno.h>
#include <poll.h>
#include <unistd.h>
#include "poll-loop.h"
#include "socket-util.h"
/* Initializes 'latch' as initially unset. */
void
latch_init(struct latch *latch)
{
latch->is_set = FALSE;
latch->wevent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
/* Destroys 'latch'. */
void
latch_destroy(struct latch *latch)
{
latch->is_set = FALSE;
CloseHandle(latch->wevent);
}
/* Resets 'latch' to the unset state. Returns true if 'latch' was previously
* set, false otherwise. */
bool
latch_poll(struct latch *latch)
{
bool is_set;
is_set = latch->is_set;
latch->is_set = FALSE;
ResetEvent(latch->wevent);
return is_set;
}
/* Sets 'latch'.
*
* Calls are not additive: a single latch_poll() clears out any number of
* latch_set(). */
void
latch_set(struct latch *latch)
{
latch->is_set = TRUE;
SetEvent(latch->wevent);
}
/* Returns true if 'latch' is set, false otherwise. Does not reset 'latch'
* to the unset state. */
bool
latch_is_set(const struct latch *latch)
{
return latch->is_set;
}
/* Causes the next poll_block() to wake up when 'latch' is set.
*
* ('where' is used in debug logging. Commonly one would use latch_wait() to
* automatically provide the caller's source file and line number for
* 'where'.) */
void
latch_wait_at(const struct latch *latch, const char *where)
{
poll_fd_wait_at(0, latch->wevent, POLLIN, where);
}

View File

@@ -83,5 +83,5 @@ latch_is_set(const struct latch *latch)
void void
latch_wait_at(const struct latch *latch, const char *where) latch_wait_at(const struct latch *latch, const char *where)
{ {
poll_fd_wait_at(latch->fds[0], POLLIN, where); poll_fd_wait_at(latch->fds[0], 0, POLLIN, where);
} }

View File

@@ -26,7 +26,12 @@
#include "util.h" #include "util.h"
struct latch { struct latch {
#ifndef _WIN32
int fds[2]; int fds[2];
#else
HANDLE wevent;
bool is_set;
#endif
}; };
void latch_init(struct latch *); void latch_init(struct latch *);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -30,18 +30,24 @@
#include "socket-util.h" #include "socket-util.h"
#include "timeval.h" #include "timeval.h"
#include "vlog.h" #include "vlog.h"
#include "hmap.h"
#include "hash.h"
VLOG_DEFINE_THIS_MODULE(poll_loop); VLOG_DEFINE_THIS_MODULE(poll_loop);
COVERAGE_DEFINE(poll_fd_wait); COVERAGE_DEFINE(poll_fd_wait);
COVERAGE_DEFINE(poll_zero_timeout); COVERAGE_DEFINE(poll_zero_timeout);
struct poll_node {
struct hmap_node hmap_node;
struct pollfd pollfd; /* Events to pass to time_poll(). */
HANDLE wevent; /* Events for WaitForMultipleObjects(). */
const char *where; /* Where poll_node was created. */
};
struct poll_loop { struct poll_loop {
/* All active poll waiters. */ /* All active poll waiters. */
struct pollfd *pollfds; /* Events to pass to poll(). */ struct hmap poll_nodes;
const char **where; /* Where each pollfd was created. */
size_t n_waiters; /* Number of elems in 'where' and 'pollfds'. */
size_t allocated_waiters; /* Allocated elems in 'where' and 'pollfds'. */
/* Time at which to wake up the next call to poll_block(), LLONG_MIN to /* Time at which to wake up the next call to poll_block(), LLONG_MIN to
* wake up immediately, or LLONG_MAX to wait forever. */ * wake up immediately, or LLONG_MAX to wait forever. */
@@ -51,35 +57,68 @@ struct poll_loop {
static struct poll_loop *poll_loop(void); static struct poll_loop *poll_loop(void);
/* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN /* Look up the node with same fd and wevent. */
* or POLLOUT or POLLIN | POLLOUT). The following call to poll_block() will static struct poll_node *
* wake up when 'fd' becomes ready for one or more of the requested events. find_poll_node(struct poll_loop *loop, int fd, uint32_t wevent)
{
struct poll_node *node;
HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_2words(fd, wevent),
&loop->poll_nodes) {
if (node->pollfd.fd == fd && node->wevent == wevent) {
return node;
}
}
return NULL;
}
/* On Unix based systems:
* *
* The event registration is one-shot: only the following call to poll_block() * Registers 'fd' as waiting for the specified 'events' (which should be
* is affected. The event will need to be re-registered after poll_block() is * POLLIN or POLLOUT or POLLIN | POLLOUT). The following call to
* called if it is to persist. * poll_block() will wake up when 'fd' becomes ready for one or more of the
* requested events. the 'fd's are given to poll() function later.
*
* On Windows system:
*
* Register 'wevent' handle for the specified 'events'. These wevents are
* given to the handleMultipleObjects() to be polled. The event
* registration is one-shot: only the following call to poll_block() is
* affected. The event will need to be re-registered after poll_block() is
* called if it is to persist.
* *
* ('where' is used in debug logging. Commonly one would use poll_fd_wait() to * ('where' is used in debug logging. Commonly one would use poll_fd_wait() to
* automatically provide the caller's source file and line number for * automatically provide the caller's source file and line number for
* 'where'.) */ * 'where'.) */
void void
poll_fd_wait_at(int fd, short int events, const char *where) poll_fd_wait_at(int fd, HANDLE wevent, short int events, const char *where)
{ {
struct poll_loop *loop = poll_loop(); struct poll_loop *loop = poll_loop();
struct poll_node *node;
COVERAGE_INC(poll_fd_wait); COVERAGE_INC(poll_fd_wait);
if (loop->n_waiters >= loop->allocated_waiters) {
loop->where = x2nrealloc(loop->where, &loop->allocated_waiters,
sizeof *loop->where);
loop->pollfds = xrealloc(loop->pollfds,
(loop->allocated_waiters
* sizeof *loop->pollfds));
}
loop->where[loop->n_waiters] = where; #ifdef _WIN32
loop->pollfds[loop->n_waiters].fd = fd; /* Null event cannot be polled. */
loop->pollfds[loop->n_waiters].events = events; if (wevent == 0) {
loop->n_waiters++; VLOG_ERR("No event to wait fd %d", fd);
return;
}
#endif
/* Check for duplicate. If found, "or" the event. */
node = find_poll_node(loop, fd, wevent);
if (node) {
node->pollfd.events |= events;
} else {
node = xzalloc(sizeof *node);
hmap_insert(&loop->poll_nodes, &node->hmap_node,
hash_2words(fd, wevent));
node->pollfd.fd = fd;
node->pollfd.events = events;
node->wevent = wevent;
node->where = where;
}
} }
/* Causes the following call to poll_block() to block for no more than 'msec' /* Causes the following call to poll_block() to block for no more than 'msec'
@@ -208,6 +247,17 @@ log_wakeup(const char *where, const struct pollfd *pollfd, int timeout)
ds_destroy(&s); ds_destroy(&s);
} }
static void
free_poll_nodes(struct poll_loop *loop)
{
struct poll_node *node, *next;
HMAP_FOR_EACH_SAFE (node, next, hmap_node, &loop->poll_nodes) {
hmap_remove(&loop->poll_nodes, &node->hmap_node);
free(node);
}
}
/* Blocks until one or more of the events registered with poll_fd_wait() /* Blocks until one or more of the events registered with poll_fd_wait()
* occurs, or until the minimum duration registered with poll_timer_wait() * occurs, or until the minimum duration registered with poll_timer_wait()
* elapses, or not at all if poll_immediate_wake() has been called. */ * elapses, or not at all if poll_immediate_wake() has been called. */
@@ -215,8 +265,12 @@ void
poll_block(void) poll_block(void)
{ {
struct poll_loop *loop = poll_loop(); struct poll_loop *loop = poll_loop();
struct poll_node *node;
struct pollfd *pollfds;
HANDLE *wevents = NULL;
int elapsed; int elapsed;
int retval; int retval;
int i;
/* Register fatal signal events before actually doing any real work for /* Register fatal signal events before actually doing any real work for
* poll_block. */ * poll_block. */
@@ -227,7 +281,23 @@ poll_block(void)
} }
timewarp_wait(); timewarp_wait();
retval = time_poll(loop->pollfds, loop->n_waiters, pollfds = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *pollfds);
#ifdef _WIN32
wevents = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *wevents);
#endif
/* Populate with all the fds and events. */
i = 0;
HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) {
pollfds[i] = node->pollfd;
#ifdef _WIN32
wevents[i] = node->wevent;
#endif
i++;
}
retval = time_poll(pollfds, hmap_count(&loop->poll_nodes), wevents,
loop->timeout_when, &elapsed); loop->timeout_when, &elapsed);
if (retval < 0) { if (retval < 0) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
@@ -235,18 +305,20 @@ poll_block(void)
} else if (!retval) { } else if (!retval) {
log_wakeup(loop->timeout_where, NULL, elapsed); log_wakeup(loop->timeout_where, NULL, elapsed);
} else if (get_cpu_usage() > 50 || VLOG_IS_DBG_ENABLED()) { } else if (get_cpu_usage() > 50 || VLOG_IS_DBG_ENABLED()) {
size_t i; i = 0;
HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) {
for (i = 0; i < loop->n_waiters; i++) { if (pollfds[i].revents) {
if (loop->pollfds[i].revents) { log_wakeup(node->where, &pollfds[i], 0);
log_wakeup(loop->where[i], &loop->pollfds[i], 0);
} }
i++;
} }
} }
free_poll_nodes(loop);
loop->timeout_when = LLONG_MAX; loop->timeout_when = LLONG_MAX;
loop->timeout_where = NULL; loop->timeout_where = NULL;
loop->n_waiters = 0; free(pollfds);
free(wevents);
/* Handle any pending signals before doing anything else. */ /* Handle any pending signals before doing anything else. */
fatal_signal_run(); fatal_signal_run();
@@ -259,8 +331,8 @@ free_poll_loop(void *loop_)
{ {
struct poll_loop *loop = loop_; struct poll_loop *loop = loop_;
free(loop->pollfds); free_poll_nodes(loop);
free(loop->where); hmap_destroy(&loop->poll_nodes);
free(loop); free(loop);
} }
@@ -279,6 +351,7 @@ poll_loop(void)
loop = pthread_getspecific(key); loop = pthread_getspecific(key);
if (!loop) { if (!loop) {
loop = xzalloc(sizeof *loop); loop = xzalloc(sizeof *loop);
hmap_init(&loop->poll_nodes);
xpthread_setspecific(key, loop); xpthread_setspecific(key, loop);
} }
return loop; return loop;

View File

@@ -50,8 +50,12 @@ extern "C" {
* caller to supply a location explicitly, which is useful if the caller's own * caller to supply a location explicitly, which is useful if the caller's own
* caller would be more useful in log output. See timer_wait_at() for an * caller would be more useful in log output. See timer_wait_at() for an
* example. */ * example. */
void poll_fd_wait_at(int fd, short int events, const char *where); void poll_fd_wait_at(int fd, HANDLE wevent, short int events, const char *where);
#define poll_fd_wait(fd, events) poll_fd_wait_at(fd, events, SOURCE_LOCATOR) #ifndef _WIN32
#define poll_fd_wait(fd, events) poll_fd_wait_at(fd, 0, events, SOURCE_LOCATOR)
#else
#define poll_fd_wait_event(fd, wevent, events) poll_fd_wait_at(fd, wevent, events, SOURCE_LOCATOR)
#endif
void poll_timer_wait_at(long long int msec, const char *where); void poll_timer_wait_at(long long int msec, const char *where);
#define poll_timer_wait(msec) poll_timer_wait_at(msec, SOURCE_LOCATOR) #define poll_timer_wait(msec) poll_timer_wait_at(msec, SOURCE_LOCATOR)

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -232,12 +232,12 @@ time_alarm(unsigned int secs)
* *
* Stores the number of milliseconds elapsed during poll in '*elapsed'. */ * Stores the number of milliseconds elapsed during poll in '*elapsed'. */
int int
time_poll(struct pollfd *pollfds, int n_pollfds, long long int timeout_when, time_poll(struct pollfd *pollfds, int n_pollfds, HANDLE *handles OVS_UNUSED,
int *elapsed) long long int timeout_when, int *elapsed)
{ {
long long int *last_wakeup = last_wakeup_get(); long long int *last_wakeup = last_wakeup_get();
long long int start; long long int start;
int retval; int retval = 0;
time_init(); time_init();
coverage_clear(); coverage_clear();
@@ -261,10 +261,25 @@ time_poll(struct pollfd *pollfds, int n_pollfds, long long int timeout_when,
time_left = timeout_when - now; time_left = timeout_when - now;
} }
#ifndef _WIN32
retval = poll(pollfds, n_pollfds, time_left); retval = poll(pollfds, n_pollfds, time_left);
if (retval < 0) { if (retval < 0) {
retval = -errno; retval = -errno;
} }
#else
if (n_pollfds > MAXIMUM_WAIT_OBJECTS) {
VLOG_ERR("Cannot handle more than maximum wait objects\n");
} else if (n_pollfds != 0) {
retval = WaitForMultipleObjects(n_pollfds, handles, FALSE,
time_left);
}
if (retval < 0) {
/* XXX This will be replace by a win error to errno
conversion function */
retval = -WSAGetLastError();
retval = -EINVAL;
}
#endif
if (deadline <= time_msec()) { if (deadline <= time_msec()) {
fatal_signal_handler(SIGALRM); fatal_signal_handler(SIGALRM);

View File

@@ -52,8 +52,8 @@ long long int time_wall_msec(void);
void time_timespec(struct timespec *); void time_timespec(struct timespec *);
void time_wall_timespec(struct timespec *); void time_wall_timespec(struct timespec *);
void time_alarm(unsigned int secs); void time_alarm(unsigned int secs);
int time_poll(struct pollfd *, int n_pollfds, long long int timeout_when, int time_poll(struct pollfd *, int n_pollfds, HANDLE *handles,
int *elapsed); long long int timeout_when, int *elapsed);
long long int timespec_to_msec(const struct timespec *); long long int timespec_to_msec(const struct timespec *);
long long int timeval_to_msec(const struct timeval *); long long int timeval_to_msec(const struct timeval *);