unsigned int change_seq;
};
-static const struct netdev_rx_class netdev_rx_pltap_class;
-
static struct list sync_list;
struct netdev_rx_pltap {
int fd;
};
-static int af_inet_sock = -1;
-
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
static struct shash pltap_netdevs = SHASH_INITIALIZER(&pltap_netdevs);
-static int netdev_pltap_create(const struct netdev_class *, const char *,
- struct netdev **);
+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 bool
is_netdev_pltap_class(const struct netdev_class *class)
{
- return class->create == netdev_pltap_create;
+ return class->construct == netdev_pltap_construct;
}
static struct netdev_pltap *
static struct netdev_rx_pltap*
netdev_rx_pltap_cast(const struct netdev_rx *rx)
{
- netdev_rx_assert_class(rx, &netdev_rx_pltap_class);
+ ovs_assert(is_netdev_pltap_class(netdev_get_class(rx->netdev)));
return CONTAINER_OF(rx, struct netdev_rx_pltap, up);
}
dev->sync_flags_needed = false;
}
+static struct netdev *
+netdev_pltap_alloc(void)
+{
+ struct netdev_pltap *netdev = xzalloc(sizeof *netdev);
+ return &netdev->up;
+}
+
static int
-netdev_pltap_create(const struct netdev_class *class OVS_UNUSED, const char *name,
- struct netdev **netdevp)
+netdev_pltap_construct(struct netdev *netdev_)
{
- struct netdev_pltap *netdev;
+ struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
int error;
- netdev = xzalloc(sizeof *netdev);
-
netdev->real_name = xzalloc(IFNAMSIZ + 1);
memset(&netdev->local_addr, 0, sizeof(netdev->local_addr));
netdev->valid_local_ip = false;
netdev->valid_local_netmask = false;
netdev->flags = 0;
netdev->sync_flags_needed = false;
+ netdev->change_seq = 1;
list_init(&netdev->sync_list);
netdev->fd = tun_alloc(IFF_TAP, netdev->real_name);
if (netdev->fd < 0) {
error = errno;
- VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s", name, strerror(error));
- goto cleanup;
+ VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s",
+ netdev_get_name(netdev_), ovs_strerror(error));
+ return error;
}
VLOG_DBG("real_name = %s", netdev->real_name);
/* Make non-blocking. */
error = set_nonblocking(netdev->fd);
if (error) {
- goto cleanup;
+ return error;
}
- netdev_init(&netdev->up, name, &netdev_pltap_class);
- shash_add(&pltap_netdevs, name, netdev);
- *netdevp = &netdev->up;
+ shash_add(&pltap_netdevs, netdev_get_name(netdev_), netdev);
return 0;
-
-cleanup:
- free(netdev);
- return error;
}
static void
-netdev_pltap_destroy(struct netdev *netdev_)
+netdev_pltap_destruct(struct netdev *netdev_)
{
struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
shash_find_and_delete(&pltap_netdevs,
netdev_get_name(netdev_));
+}
+
+static void
+netdev_pltap_dealloc(struct netdev *netdev_)
+{
+ struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
free(netdev);
}
static int netdev_pltap_up(struct netdev_pltap *dev);
+static struct netdev_rx *
+netdev_pltap_rx_alloc(void)
+{
+ struct netdev_rx_pltap *rx = xzalloc(sizeof *rx);
+ return &rx->up;
+}
+
static int
-netdev_pltap_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
+netdev_pltap_rx_construct(struct netdev_rx *rx_)
{
+ struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
+ struct netdev *netdev_ = rx->up.netdev;
struct netdev_pltap *netdev =
netdev_pltap_cast(netdev_);
- struct netdev_rx_pltap *rx;
- int err;
+ int error;
- rx = xmalloc(sizeof *rx);
- netdev_rx_init(&rx->up, netdev_, &netdev_rx_pltap_class);
rx->fd = netdev->fd;
- *rxp = &rx->up;
if (!netdev_pltap_finalized(netdev))
return 0;
- err = netdev_pltap_up(netdev);
- if (err) {
- free(rx);
- *rxp = NULL;
- return err;
+ error = netdev_pltap_up(netdev);
+ if (error) {
+ return error;
}
return 0;
}
static void
-netdev_rx_pltap_destroy(struct netdev_rx *rx_)
+netdev_pltap_rx_destruct(struct netdev_rx *rx_ OVS_UNUSED)
+{
+}
+
+static void
+netdev_pltap_rx_dealloc(struct netdev_rx *rx_)
{
struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
+
free(rx);
}
ofd = open(ofname, O_RDONLY | O_NONBLOCK);
if (ofd < 0) {
- VLOG_ERR("Cannot open %s: %s", ofname, strerror(errno));
+ VLOG_ERR("Cannot open %s: %s", ofname, ovs_strerror(errno));
error = errno;
goto cleanup;
}
ifd = open(ifname, O_WRONLY | O_NONBLOCK);
if (ifd < 0) {
- VLOG_ERR("Cannot open %s: %s", ifname, strerror(errno));
+ VLOG_ERR("Cannot open %s: %s", ifname, ovs_strerror(errno));
error = errno;
goto cleanup;
}
if (select(maxfd + 1, &readset, &writeset, &errorset, NULL) < 0) {
if (errno == EINTR)
continue;
- VLOG_ERR("selec error: %s", strerror(errno));
+ VLOG_ERR("selec error: %s", ovs_strerror(errno));
error = errno;
goto cleanup;
}
ssize_t n = write(ifd, msg + bytes_written, bytes_to_write);
if (n < 0) {
if (errno != EAGAIN && errno != EINTR) {
- VLOG_ERR("write on %s: %s", ifname, strerror(errno));
+ VLOG_ERR("write on %s: %s", ifname, ovs_strerror(errno));
error = errno;
goto cleanup;
}
ssize_t n = read(ofd, reply + bytes_read, bytes_to_read);
if (n < 0) {
if (errno != EAGAIN && errno != EINTR) {
- VLOG_ERR("read on %s: %s", ofname, strerror(errno));
+ VLOG_ERR("read on %s: %s", ofname, ovs_strerror(errno));
error = errno;
goto cleanup;
}
}
static int
-netdev_rx_pltap_recv(struct netdev_rx *rx_, void *buffer, size_t size)
+netdev_pltap_rx_recv(struct netdev_rx *rx_, void *buffer, size_t size)
{
struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
- char prefix[4];
+ struct tun_pi pi;
struct iovec iov[2] = {
- { .iov_base = prefix, .iov_len = 4 },
+ { .iov_base = &pi, .iov_len = sizeof(pi) },
{ .iov_base = buffer, .iov_len = size }
};
for (;;) {
} else if (errno != EINTR) {
if (errno != EAGAIN) {
VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
- netdev_rx_get_name(rx_), strerror(errno));
+ netdev_rx_get_name(rx_), ovs_strerror(errno));
}
return -errno;
}
}
static void
-netdev_rx_pltap_wait(struct netdev_rx *rx_)
+netdev_pltap_rx_wait(struct netdev_rx *rx_)
{
struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
struct netdev_pltap *netdev =
{
struct netdev_pltap *dev =
netdev_pltap_cast(netdev_);
- char prefix[4] = { 0, 0, 8, 6 };
+ struct tun_pi pi = { 0, 0x86 };
struct iovec iov[2] = {
- { .iov_base = prefix, .iov_len = 4 },
+ { .iov_base = &pi, .iov_len = sizeof(pi) },
{ .iov_base = (char*) buffer, .iov_len = size }
};
if (dev->fd < 0)
} else if (errno != EINTR) {
if (errno != EAGAIN) {
VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
- netdev_get_name(netdev_), strerror(errno));
+ netdev_get_name(netdev_), ovs_strerror(errno));
}
return errno;
}
}
static int
-netdev_rx_pltap_drain(struct netdev_rx *rx_)
+netdev_pltap_rx_drain(struct netdev_rx *rx_)
{
struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
char buffer[128];
{
struct ifreq ifr;
int hwaddr_family;
+ int error;
memset(&ifr, 0, sizeof ifr);
ovs_strzcpy(ifr.ifr_name, dev->real_name, sizeof ifr.ifr_name);
- if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
- /* ENODEV probably means that a vif disappeared asynchronously and
- * hasn't been removed from the database yet, so reduce the log level
- * to INFO for that case. */
- VLOG(errno == ENODEV ? VLL_INFO : VLL_ERR,
- "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
- dev->real_name, strerror(errno));
+ error = af_inet_ifreq_ioctl(dev->real_name, &ifr,
+ SIOCGIFHWADDR, "SIOCGIFHWADDR");
+ if (error) {
return errno;
}
hwaddr_family = ifr.ifr_hwaddr.sa_family;
get_flags(struct netdev_pltap *dev, enum netdev_flags *flags)
{
struct ifreq ifr;
+ int error;
- memset(&ifr, 0, sizeof ifr);
- ovs_strzcpy(ifr.ifr_name, dev->real_name, sizeof ifr.ifr_name);
- if (ioctl(af_inet_sock, SIOCGIFFLAGS, &ifr) < 0)
- return errno;
+ error = af_inet_ifreq_ioctl(dev->real_name, &ifr,
+ SIOCGIFFLAGS, "SIOCGIFFLAGS");
+ if (error) {
+ return error;
+ }
*flags = 0;
if (ifr.ifr_flags & IFF_UP)
*flags |= NETDEV_UP;
netdev_pltap_init(void)
{
list_init(&sync_list);
- af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
- if (af_inet_sock < 0) {
- VLOG_ERR("failed to create inet socket: %s", strerror(errno));
- }
unixctl_command_register("netdev-pltap/get-tapname", "port",
1, 1, netdev_pltap_get_real_name, NULL);
return 0;
netdev_pltap_run,
netdev_pltap_wait,
- netdev_pltap_create,
- netdev_pltap_destroy,
+ netdev_pltap_alloc,
+ netdev_pltap_construct,
+ netdev_pltap_destruct,
+ netdev_pltap_dealloc,
netdev_pltap_get_config,
netdev_pltap_set_config,
- NULL, /* get_tunnel_config */
-
- netdev_pltap_rx_open,
+ NULL, /* get_tunnel_config */
netdev_pltap_send,
netdev_pltap_send_wait,
netdev_pltap_set_etheraddr,
netdev_pltap_get_etheraddr,
- NULL, /* get_mtu */
- NULL, /* set_mtu */
+ NULL, /* get_mtu */
+ NULL, /* set_mtu */
NULL, /* get_ifindex */
- NULL, /* get_carrier */
+ NULL, /* get_carrier */
NULL, /* get_carrier_resets */
NULL, /* get_miimon */
netdev_pltap_get_stats,
netdev_pltap_update_flags,
- netdev_pltap_change_seq
-};
-
-static const struct netdev_rx_class netdev_rx_pltap_class = {
- netdev_rx_pltap_destroy,
- netdev_rx_pltap_recv,
- netdev_rx_pltap_wait,
- netdev_rx_pltap_drain,
+ 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,
};