#include <linux/pkt_sched.h>
#include <linux/rtnetlink.h>
#include <linux/sockios.h>
+#include <poll.h>
#include <stdlib.h>
+#include <strings.h>
+#include <sys/epoll.h>
#include <sys/stat.h>
#include <unistd.h>
enum { N_UPCALL_SOCKS = 16 };
BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
+BUILD_ASSERT_DECL(N_UPCALL_SOCKS <= 32); /* We use a 32-bit word as a mask. */
/* This ethtool flag was introduced in Linux 2.6.24, so it might be
* missing if we have old headers. */
/* Upcall messages. */
struct nl_sock *upcall_socks[N_UPCALL_SOCKS];
- int last_read_upcall;
- unsigned int listen_mask;
+ uint32_t ready_mask; /* 1-bit for each sock with unread messages. */
+ int epoll_fd; /* epoll fd that includes the upcall socks. */
/* Change notification. */
struct sset changed_ports; /* Ports that have changed. */
static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
static void dpif_linux_port_changed(const void *vport, void *dpif);
-static uint32_t dpif_linux_port_get_pid__(const struct dpif *,
- uint16_t port_no,
- enum dpif_upcall_type);
+static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint16_t port_no);
static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
struct ofpbuf *);
dpif = xzalloc(sizeof *dpif);
dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
dpif);
+ dpif->epoll_fd = -1;
dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
dp->dp_ifindex, dp->dp_ifindex);
{
int i;
+ if (dpif->epoll_fd >= 0) {
+ close(dpif->epoll_fd);
+ dpif->epoll_fd = -1;
+ }
for (i = 0; i < N_UPCALL_SOCKS; i++) {
nl_sock_destroy(dpif->upcall_socks[i]);
dpif->upcall_socks[i] = NULL;
uint32_t upcall_pid;
request.port_no = dpif_linux_pop_port(dpif);
- upcall_pid = dpif_linux_port_get_pid__(dpif_, request.port_no,
- DPIF_UC_MISS);
+ upcall_pid = dpif_linux_port_get_pid(dpif_, request.port_no);
request.upcall_pid = &upcall_pid;
error = dpif_linux_vport_transact(&request, &reply, &buf);
}
static uint32_t
-dpif_linux_port_get_pid__(const struct dpif *dpif_, uint16_t port_no,
- enum dpif_upcall_type upcall_type)
+dpif_linux_port_get_pid(const struct dpif *dpif_, uint16_t port_no)
{
struct dpif_linux *dpif = dpif_linux_cast(dpif_);
- if (!(dpif->listen_mask & (1u << upcall_type))) {
+ if (dpif->epoll_fd < 0) {
return 0;
} else {
int idx = port_no & (N_UPCALL_SOCKS - 1);
}
}
-static uint32_t
-dpif_linux_port_get_pid(const struct dpif *dpif, uint16_t port_no)
-{
- return dpif_linux_port_get_pid__(dpif, port_no, DPIF_UC_ACTION);
-}
-
static int
dpif_linux_flow_flush(struct dpif *dpif_)
{
}
static void
-dpif_linux_operate(struct dpif *dpif_, union dpif_op **ops, size_t n_ops)
+dpif_linux_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
{
struct dpif_linux *dpif = dpif_linux_cast(dpif_);
struct nl_transaction **txnsp;
txns = xmalloc(n_ops * sizeof *txns);
for (i = 0; i < n_ops; i++) {
struct nl_transaction *txn = &txns[i];
- union dpif_op *op = ops[i];
+ struct dpif_op *op = ops[i];
if (op->type == DPIF_OP_FLOW_PUT) {
- struct dpif_flow_put *put = &op->flow_put;
+ struct dpif_flow_put *put = &op->u.flow_put;
struct dpif_linux_flow request;
dpif_linux_init_flow_put(dpif_, put->flags, put->key, put->key_len,
txn->request = ofpbuf_new(1024);
dpif_linux_flow_to_ofpbuf(&request, txn->request);
} else if (op->type == DPIF_OP_EXECUTE) {
- struct dpif_execute *execute = &op->execute;
+ struct dpif_execute *execute = &op->u.execute;
txn->request = dpif_linux_encode_execute(
dpif->dp_ifindex, execute->key, execute->key_len,
for (i = 0; i < n_ops; i++) {
struct nl_transaction *txn = &txns[i];
- union dpif_op *op = ops[i];
+ struct dpif_op *op = ops[i];
if (op->type == DPIF_OP_FLOW_PUT) {
- struct dpif_flow_put *put = &op->flow_put;
+ struct dpif_flow_put *put = &op->u.flow_put;
int error = txn->error;
if (!error && put->stats) {
dpif_linux_flow_get_stats(&reply, put->stats);
}
}
- put->error = error;
+ op->error = error;
} else if (op->type == DPIF_OP_EXECUTE) {
- struct dpif_execute *execute = &op->execute;
-
- execute->error = txn->error;
+ op->error = txn->error;
} else {
NOT_REACHED();
}
free(txns);
}
-static int
-dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
-{
- struct dpif_linux *dpif = dpif_linux_cast(dpif_);
- *listen_mask = dpif->listen_mask;
- return 0;
-}
-
static void
set_upcall_pids(struct dpif *dpif_)
{
int error;
DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
- uint32_t upcall_pid = dpif_linux_port_get_pid__(dpif_, port.port_no,
- DPIF_UC_MISS);
+ uint32_t upcall_pid = dpif_linux_port_get_pid(dpif_, port.port_no);
struct dpif_linux_vport vport_request;
dpif_linux_vport_init(&vport_request);
}
static int
-dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
+dpif_linux_recv_set(struct dpif *dpif_, bool enable)
{
struct dpif_linux *dpif = dpif_linux_cast(dpif_);
- if (listen_mask == dpif->listen_mask) {
+ if ((dpif->epoll_fd >= 0) == enable) {
return 0;
}
- if (!listen_mask) {
+ if (!enable) {
destroy_upcall_socks(dpif);
- } else if (!dpif->listen_mask) {
+ } else {
int i;
int error;
+ dpif->epoll_fd = epoll_create(N_UPCALL_SOCKS);
+ if (dpif->epoll_fd < 0) {
+ return errno;
+ }
+
for (i = 0; i < N_UPCALL_SOCKS; i++) {
+ struct epoll_event event;
+
error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
if (error) {
destroy_upcall_socks(dpif);
return error;
}
+
+ memset(&event, 0, sizeof event);
+ event.events = EPOLLIN;
+ event.data.u32 = i;
+ if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD,
+ nl_sock_fd(dpif->upcall_socks[i]), &event) < 0) {
+ error = errno;
+ destroy_upcall_socks(dpif);
+ return error;
+ }
}
+
+ dpif->ready_mask = 0;
}
- dpif->listen_mask = listen_mask;
set_upcall_pids(dpif_);
return 0;
dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
{
struct dpif_linux *dpif = dpif_linux_cast(dpif_);
- int i;
int read_tries = 0;
- if (!dpif->listen_mask) {
+ if (dpif->epoll_fd < 0) {
return EAGAIN;
}
- for (i = 0; i < N_UPCALL_SOCKS; i++) {
- struct nl_sock *upcall_sock;
- int dp_ifindex;
+ if (!dpif->ready_mask) {
+ struct epoll_event events[N_UPCALL_SOCKS];
+ int retval;
+ int i;
- dpif->last_read_upcall = (dpif->last_read_upcall + 1) &
- (N_UPCALL_SOCKS - 1);
- upcall_sock = dpif->upcall_socks[dpif->last_read_upcall];
+ do {
+ retval = epoll_wait(dpif->epoll_fd, events, N_UPCALL_SOCKS, 0);
+ } while (retval < 0 && errno == EINTR);
+ if (retval < 0) {
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
+ VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
+ }
+
+ for (i = 0; i < retval; i++) {
+ dpif->ready_mask |= 1u << events[i].data.u32;
+ }
+ }
+ while (dpif->ready_mask) {
+ int indx = ffs(dpif->ready_mask) - 1;
+ struct nl_sock *upcall_sock = dpif->upcall_socks[indx];
+
+ dpif->ready_mask &= ~(1u << indx);
for (;;) {
struct ofpbuf *buf;
+ int dp_ifindex;
int error;
if (++read_tries > 50) {
}
error = parse_odp_packet(buf, upcall, &dp_ifindex);
- if (!error
- && dp_ifindex == dpif->dp_ifindex
- && dpif->listen_mask & (1u << upcall->type)) {
+ if (!error && dp_ifindex == dpif->dp_ifindex) {
return 0;
}
dpif_linux_recv_wait(struct dpif *dpif_)
{
struct dpif_linux *dpif = dpif_linux_cast(dpif_);
- int i;
- if (!dpif->listen_mask) {
+ if (dpif->epoll_fd < 0) {
return;
}
- for (i = 0; i < N_UPCALL_SOCKS; i++) {
- nl_sock_wait(dpif->upcall_socks[i], POLLIN);
- }
+ poll_fd_wait(dpif->epoll_fd, POLLIN);
}
static void
struct dpif_linux *dpif = dpif_linux_cast(dpif_);
int i;
- if (!dpif->listen_mask) {
+ if (dpif->epoll_fd < 0) {
return;
}
dpif_linux_flow_dump_done,
dpif_linux_execute,
dpif_linux_operate,
- dpif_linux_recv_get_mask,
- dpif_linux_recv_set_mask,
+ dpif_linux_recv_set,
dpif_linux_queue_to_priority,
dpif_linux_recv,
dpif_linux_recv_wait,