#include <inttypes.h>
#include <net/if.h>
#include <linux/ethtool.h>
+#include <linux/rtnetlink.h>
#include <linux/sockios.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "dpif-provider.h"
+#include "netdev-linux.h"
#include "ofpbuf.h"
#include "poll-loop.h"
+#include "svec.h"
#include "util.h"
#include "vlog.h"
struct dpif_linux {
struct dpif dpif;
int fd;
+
+ /* Used by dpif_linux_get_all_names(). */
+ char *local_ifname;
+ int minor;
+
+ /* Change notification. */
+ int local_ifindex; /* Ifindex of local port. */
+ struct svec changed_ports; /* Ports that have changed. */
+ struct linux_netdev_notifier port_notifier;
};
static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
static int do_ioctl(const struct dpif *, int cmd, const void *arg);
static int lookup_minor(const char *name, int *minor);
+static int finish_open(struct dpif *, const char *local_ifname);
static int create_minor(const char *name, int minor, struct dpif **dpifp);
static int open_minor(int minor, struct dpif **dpifp);
static int make_openvswitch_device(int minor, char **fnp);
+static void dpif_linux_port_changed(const struct linux_netdev_change *,
+ void *dpif);
static struct dpif_linux *
dpif_linux_cast(const struct dpif *dpif)
return CONTAINER_OF(dpif, struct dpif_linux, dpif);
}
+static void
+dpif_linux_run(void)
+{
+ linux_netdev_notifier_run();
+}
+
+static void
+dpif_linux_wait(void)
+{
+ linux_netdev_notifier_wait();
+}
+
+static int
+dpif_linux_enumerate(struct svec *all_dps)
+{
+ int error;
+ int i;
+
+ error = 0;
+ for (i = 0; i < ODP_MAX; i++) {
+ struct dpif *dpif;
+ char devname[16];
+ int retval;
+
+ sprintf(devname, "dp%d", i);
+ retval = dpif_open(devname, &dpif);
+ if (!retval) {
+ svec_add(all_dps, devname);
+ dpif_close(dpif);
+ } else if (retval != ENODEV && !error) {
+ error = retval;
+ }
+ }
+ return error;
+}
+
static int
dpif_linux_open(const char *name UNUSED, char *suffix, bool create,
struct dpif **dpifp)
}
} else {
struct dpif_linux *dpif;
- int listen_mask;
+ struct odp_port port;
int error;
if (minor < 0) {
}
dpif = dpif_linux_cast(*dpifp);
- /* We can open the device, but that doesn't mean that it's been
- * created. If it hasn't been, then any command other than
- * ODP_DP_CREATE will return ENODEV. Try something innocuous. */
- listen_mask = 0; /* Make Valgrind happy. */
- error = do_ioctl(*dpifp, ODP_GET_LISTEN_MASK, &listen_mask);
- if (error) {
+ /* We need the local port's ifindex for the poll function. Start by
+ * getting the local port's name. */
+ memset(&port, 0, sizeof port);
+ port.port = ODPP_LOCAL;
+ if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
+ error = errno;
if (error != ENODEV) {
VLOG_WARN("%s: probe returned unexpected error: %s",
dpif_name(*dpifp), strerror(error));
}
dpif_close(*dpifp);
+ return error;
}
- return error;
+
+ /* Then use that to finish up opening. */
+ return finish_open(&dpif->dpif, port.devname);
}
}
dpif_linux_close(struct dpif *dpif_)
{
struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+ linux_netdev_notifier_unregister(&dpif->port_notifier);
+ svec_destroy(&dpif->changed_ports);
+ free(dpif->local_ifname);
close(dpif->fd);
free(dpif);
}
+static int
+dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
+{
+ struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+
+ svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
+ svec_add(all_names, dpif->local_ifname);
+ return 0;
+}
+
static int
dpif_linux_delete(struct dpif *dpif_)
{
return error ? -error : pv.n_ports;
}
+static int
+dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
+{
+ struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+ int error;
+
+ error = linux_netdev_notifier_get_error(&dpif->port_notifier);
+ if (!error) {
+ if (!dpif->changed_ports.n) {
+ return EAGAIN;
+ }
+ *devnamep = dpif->changed_ports.names[--dpif->changed_ports.n];
+ } else {
+ svec_clear(&dpif->changed_ports);
+ }
+ return error;
+}
+
+static void
+dpif_linux_port_poll_wait(const struct dpif *dpif_)
+{
+ struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+ if (dpif->changed_ports.n
+ || linux_netdev_notifier_peek_error(&dpif->port_notifier)) {
+ poll_immediate_wake();
+ } else {
+ linux_netdev_notifier_wait();
+ }
+}
+
static int
dpif_linux_port_group_get(const struct dpif *dpif_, int group,
uint16_t ports[], int n)
const struct dpif_class dpif_linux_class = {
"", /* This is the default class. */
"linux",
- NULL, /* run */
- NULL, /* wait */
+ dpif_linux_run,
+ dpif_linux_wait,
+ dpif_linux_enumerate,
dpif_linux_open,
dpif_linux_close,
+ dpif_linux_get_all_names,
dpif_linux_delete,
dpif_linux_get_stats,
dpif_linux_get_drop_frags,
dpif_linux_port_query_by_number,
dpif_linux_port_query_by_name,
dpif_linux_port_list,
+ dpif_linux_port_poll,
+ dpif_linux_port_poll_wait,
dpif_linux_port_group_get,
dpif_linux_port_group_set,
dpif_linux_flow_get,
}
static int
-lookup_minor(const char *name, int *minor)
+lookup_minor(const char *name, int *minorp)
{
struct ethtool_drvinfo drvinfo;
+ int minor, port_no;
struct ifreq ifr;
int error;
int sock;
goto error_close_sock;
}
- if (!isdigit(drvinfo.bus_info[0])) {
- VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
- name);
+ if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
+ VLOG_WARN("%s ethtool bus_info has unexpected format", name);
error = EPROTOTYPE;
goto error_close_sock;
+ } else if (port_no != ODPP_LOCAL) {
+ /* This is an Open vSwitch device but not the local port. We
+ * intentionally support only using the name of the local port as the
+ * name of a datapath; otherwise, it would be too difficult to
+ * enumerate all the names of a datapath. */
+ error = EOPNOTSUPP;
+ goto error_close_sock;
}
- *minor = atoi(drvinfo.bus_info);
+ *minorp = minor;
close(sock);
return 0;
return default_major;
}
+static int
+finish_open(struct dpif *dpif_, const char *local_ifname)
+{
+ struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+ dpif->local_ifname = strdup(local_ifname);
+ dpif->local_ifindex = if_nametoindex(local_ifname);
+ if (!dpif->local_ifindex) {
+ int error = errno;
+ dpif_close(dpif_);
+ VLOG_WARN("could not get ifindex of %s device: %s",
+ local_ifname, strerror(errno));
+ return error;
+ }
+ return 0;
+}
+
static int
create_minor(const char *name, int minor, struct dpif **dpifp)
{
int error = open_minor(minor, dpifp);
if (!error) {
error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
- if (error) {
+ if (!error) {
+ error = finish_open(*dpifp, name);
+ } else {
dpif_close(*dpifp);
}
}
fd = open(fn, O_RDONLY | O_NONBLOCK);
if (fd >= 0) {
- struct dpif_linux *dpif;
- char *name;
-
- name = xasprintf("dp%d", minor);
-
- dpif = xmalloc(sizeof *dpif);
- dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
- dpif->fd = fd;
- *dpifp = &dpif->dpif;
-
- free(name);
+ struct dpif_linux *dpif = xmalloc(sizeof *dpif);
+ error = linux_netdev_notifier_register(&dpif->port_notifier,
+ dpif_linux_port_changed, dpif);
+ if (!error) {
+ char *name;
+
+ name = xasprintf("dp%d", minor);
+ dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
+ free(name);
+
+ dpif->fd = fd;
+ dpif->local_ifname = NULL;
+ dpif->minor = minor;
+ dpif->local_ifindex = 0;
+ svec_init(&dpif->changed_ports);
+ *dpifp = &dpif->dpif;
+ } else {
+ free(dpif);
+ }
} else {
error = errno;
VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
return error;
}
+
+static void
+dpif_linux_port_changed(const struct linux_netdev_change *change, void *dpif_)
+{
+ struct dpif_linux *dpif = dpif_;
+
+ if (change->master_ifindex == dpif->local_ifindex
+ && (change->nlmsg_type == RTM_NEWLINK
+ || change->nlmsg_type == RTM_DELLINK))
+ {
+ /* Our datapath changed, either adding a new port or deleting an
+ * existing one. */
+ if (!svec_contains(&dpif->changed_ports, change->ifname)) {
+ svec_add(&dpif->changed_ports, change->ifname);
+ svec_sort(&dpif->changed_ports);
+ }
+ }
+}