From mboxrd@z Thu Jan 1 00:00:00 1970 From: Gregory Haskins Subject: Re: [PATCH] virtio: add a virtio-console to the vbus backend Date: Sat, 06 Jun 2009 21:38:18 -0400 Message-ID: <4A2B1A0A.1090906@novell.com> References: <20090605192648.19271.68909.stgit@dev.haskins.net> <20090606220230.GE28614@redhat.com> <4A2B0A7E.1060009@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig2D0F6F7860F9EE3AD6DE7262" Cc: "Michael S. Tsirkin" , kvm@vger.kernel.org, rusty@rustcorp.com.au To: Gregory Haskins Return-path: Received: from victor.provo.novell.com ([137.65.250.26]:39743 "EHLO victor.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752238AbZFGBi1 (ORCPT ); Sat, 6 Jun 2009 21:38:27 -0400 In-Reply-To: <4A2B0A7E.1060009@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig2D0F6F7860F9EE3AD6DE7262 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Gregory Haskins wrote: > Michael S. Tsirkin wrote: > =20 >> On Fri, Jun 05, 2009 at 03:30:07PM -0400, Gregory Haskins wrote: >> =20 >> =20 >>> Hi Michael, >>> FYI: This was a patch I started to put together around the time vbu= s-v3 was >>> released to demonstrate the interface for virtio devices that we ne= eded. I >>> just wanted to get it out to you so you have a working example to l= ook at >>> what it takes to write something like virtio-net against virtio-vbu= s. >>> >>> What I will do next is make a minor v3.1 release of vbus on top of = kvm.git >>> and get this patch cleaned up so we can actually run it. Note that= v3.1 >>> will just a minor port of v3 to kvm.git/HEAD, not the new iosignalf= d/irqfd >>> stuff that I have queued for v4. Thats not ready for prime-time ye= t. >>> >>> ---------------------------- >>> >>> virtio: add a virtio-console to the vbus backend >>> >>> A console probably doesnt need to live in the kernel since it is gene= rally >>> not performance limited. However, its also a very simple example, so= lets >>> make a vbus backend to demonstrate the virtio interfaces. >>> >>> Signed-off-by: Gregory Haskins >>> =20 >>> =20 >> I'm a bit confused: does this live on the guest or host side? >> =20 >> =20 > > Host side. The guest would run the existing virtio-console driver. > > -Greg > =20 >> >> =20 >> =20 >>> --- >>> >>> drivers/vbus/devices/Kconfig | 9=20 >>> drivers/vbus/devices/Makefile | 1=20 >>> drivers/vbus/devices/virtio-console-backend.c | 461 +++++++++++++++= ++++++++++ >>> 3 files changed, 471 insertions(+), 0 deletions(-) >>> create mode 100644 drivers/vbus/devices/virtio-console-backend.c >>> >>> diff --git a/drivers/vbus/devices/Kconfig b/drivers/vbus/devices/Kcon= fig >>> index 64e4731..0239e86 100644 >>> --- a/drivers/vbus/devices/Kconfig >>> +++ b/drivers/vbus/devices/Kconfig >>> @@ -15,3 +15,12 @@ config VBUS_VENETTAP >>> =20 >>> If unsure, say N >>> =20 >>> +config VBUS_VIRTIO_CONSOLE >>> + tristate "Virtual-Bus Backend for virtio-console drivers" >>> + depends on VBUS_DEVICES >>> + select VBUS_VIRTIO_BACKEND >>> + default n >>> + help >>> + Provides a virtual console device within the vbus infrastruc= ture >>> + >>> + If unsure, say N >>> diff --git a/drivers/vbus/devices/Makefile b/drivers/vbus/devices/Mak= efile >>> index 2ea7d2a..fd51cb6 100644 >>> --- a/drivers/vbus/devices/Makefile >>> +++ b/drivers/vbus/devices/Makefile >>> @@ -1 +1,2 @@ >>> obj-$(CONFIG_VBUS_VENETTAP) +=3D venet-tap.o >>> +obj-$(CONFIG_VBUS_VIRTIO_CONSOLE) +=3D virtio-console-backend.o >>> diff --git a/drivers/vbus/devices/virtio-console-backend.c b/drivers/= vbus/devices/virtio-console-backend.c >>> new file mode 100644 >>> index 0000000..bf30016 >>> --- /dev/null >>> +++ b/drivers/vbus/devices/virtio-console-backend.c >>> @@ -0,0 +1,461 @@ >>> +/* >>> + * virtio-console for the vbus backend >>> + * >>> + * Copyright (C) 2009 Novell, Gregory Haskins >>> + * >>> + * This file is free software; you can redistribute it and/or modify= >>> + * it under the terms of version 2 of the GNU General Public License= >>> + * as published by the Free Software Foundation. >>> + * >>> + * This program is distributed in the hope that it will be useful, >>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of >>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >>> + * GNU General Public License for more details. >>> + * >>> + * You should have received a copy of the GNU General Public License= >>> + * along with this program; if not, write to the Free Software Found= ation, >>> + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. >>> + */ >>> + >>> +#include >>> +#include >>> +#include >>> + >>> +#include >>> +#include >>> +#include >>> + >>> +#include >>> +#include >>> =20 ^^^^^^^^^^^^^^^^^^^^^^^ For more details, see these two files in my tree. Here is a direct link via gitweb http://git.kernel.org/?p=3Dlinux/kernel/git/ghaskins/vbus/linux-2.6.git;a= =3Dblob;f=3Dinclude/linux/vbus_device.h;h=3Dc91bce440e2d956a7a90bf4a111b3= dfd6ef16c19;hb=3D6a7f28ebf83fc8b845aefb120121b1bb3c471876 and http://git.kernel.org/?p=3Dlinux/kernel/git/ghaskins/vbus/linux-2.6.git;a= =3Dblob;f=3Dinclude/linux/virtio_vbus.h;h=3D05791bf24b42900e09ae088e90591= 721db9180b6;hb=3D6a7f28ebf83fc8b845aefb120121b1bb3c471876 HTH -Greg >>> +#include >>> + >>> +MODULE_AUTHOR("Gregory Haskins"); >>> +MODULE_LICENSE("GPL"); >>> + >>> +#undef PDEBUG /* undef it, just in case */ >>> +#ifdef VIRTIO_CONSOLE_DEBUG >>> +# define PDEBUG(fmt, args...) printk(KERN_DEBUG "virtio-console: " = fmt, ## args) >>> +#else >>> +# define PDEBUG(fmt, args...) /* not debugging: nothing */ >>> +#endif >>> + >>> +struct _virtio_console { >>> + spinlock_t lock; >>> + char *name; >>> + dev_t devno; >>> + struct cdev cdev; >>> + struct vbus_device dev; >>> + struct virtio_device_interface intf; >>> + struct virtio_connection conn; >>> + struct vbus_memctx *ctx; >>> + struct virtqueue *rx; >>> + struct virtqueue *tx; >>> + int enabled:1; >>> + int connected:1; >>> + int opened:1; >>> + int tty:1; >>> + int link:1; >>> +}; >>> + >>> +static struct _virtio_console * >>> +conn_to_priv(struct virtio_connection *conn) >>> +{ >>> + return container_of(conn, struct _virtio_console, conn); >>> +} >>> + >>> +static struct _virtio_console * >>> +intf_to_priv(struct virtio_device_interface *intf) >>> +{ >>> + return container_of(intf, struct _virtio_console, intf); >>> +} >>> + >>> +static struct _virtio_console * >>> +vdev_to_priv(struct vbus_device *vdev) >>> +{ >>> + return container_of(vdev, struct _virtio_console, dev); >>> +} >>> + >>> +/* >>> + * -------------------------------------------- >>> + * Driver connection >>> + * -------------------------------------------- >>> + */ >>> + >>> +static void >>> +_virtio_console_add_vq(struct virtio_connection *vconn, int index, >>> + struct virtqueue *vq) >>> +{ >>> + struct _virtio_console *priv =3D conn_to_priv(vconn); >>> + >>> + switch (index) { >>> + case 0: >>> + if (!priv->rx) >>> + priv->rx =3D vq; >>> + break; >>> + case 1: >>> + if (!priv->tx) >>> + priv->tx =3D vq; >>> + break; >>> + default: >>> + break; >>> + } >>> +} >>> + >>> +static void >>> +_virtio_console_del_vq(struct virtio_connection *vconn, int index) >>> +{ >>> + struct _virtio_console *priv =3D conn_to_priv(vconn); >>> + >>> + switch (index) { >>> + case 0: >>> + priv->rx =3D NULL; >>> + break; >>> + case 1: >>> + priv->tx =3D NULL; >>> + break; >>> + default: >>> + break; >>> + } >>> +} >>> + >>> +static void >>> +_virtio_console_notify_vq(struct virtio_connection *vconn, int index= ) >>> +{ >>> + >>> +} >>> + >>> +/* >>> + * This is called whenever the driver closes all references to our d= evice >>> + */ >>> +static void >>> +_virtio_console_release(struct virtio_connection *conn) >>> +{ >>> + struct _virtio_console *priv =3D conn_to_priv(conn); >>> + >>> + priv->opened =3D false; >>> + vbus_memctx_put(priv->ctx); >>> +} >>> + >>> +static struct virtio_connection_ops virtio_console_connection_ops =3D= { >>> + .add_vq =3D _virtio_console_add_vq, >>> + .del_vq =3D _virtio_console_del_vq, >>> + .notify_vq =3D _virtio_console_notify_vq, >>> + .release =3D _virtio_console_release, >>> +}; >>> + >>> +/* >>> + * -------------------------------------------- >>> + * Interface >>> + * -------------------------------------------- >>> + */ >>> + >>> +/* >>> + * This is called whenever a driver wants to open our device_interfa= ce >>> + * for communication. The connection is represented by a >>> + * virtio_connection object. It is up to the implementation to deci= de >>> + * if it allows more than one connection at a time. This simple exa= mple >>> + * does not. >>> + */ >>> +static int >>> +virtio_console_intf_open(struct virtio_device_interface *intf, >>> + struct vbus_memctx *ctx, >>> + struct virtio_connection **conn) >>> +{ >>> + struct _virtio_console *priv =3D intf_to_priv(intf); >>> + unsigned long flags; >>> + >>> + PDEBUG("open\n"); >>> + >>> + spin_lock_irqsave(&priv->lock, flags); >>> + >>> + /* >>> + * We only allow one connection to this device >>> + */ >>> + if (priv->opened) { >>> + spin_unlock_irqrestore(&priv->lock, flags); >>> + return -EBUSY; >>> + } >>> + >>> + memset(&priv->conn, 0, sizeof(priv->conn)); >>> + priv->conn.ops =3D &virtio_console_connection_ops; >>> + >>> + priv->opened =3D true; >>> + priv->ctx =3D ctx; >>> + >>> + vbus_memctx_get(ctx); >>> + >>> + spin_unlock_irqrestore(&priv->lock, flags); >>> + >>> + *conn =3D &priv->conn; >>> + >>> + return 0; >>> +} >>> + >>> +static void >>> +virtio_console_intf_release(struct virtio_device_interface *intf) >>> +{ >>> + kobject_put(intf->parent->dev->kobj); /* acquired on bus-connect */= >>> +} >>> + >>> +static struct virtio_device_interface_ops virtio_console_intf_ops =3D= { >>> + .open =3D virtio_console_intf_open, >>> + .release =3D virtio_console_intf_release, >>> +}; >>> + >>> +/* >>> + * -------------------------------------------- >>> + * Bus+Device interaction >>> + * -------------------------------------------- >>> + */ >>> + >>> +/* >>> + * This is called whenever the admin creates a symbolic link between= >>> + * a bus in /config/vbus/buses and our device. It represents a bus >>> + * connection. Your device can chose to allow more than one bus to >>> + * connect, or it can restrict it to one bus. It can also choose to= >>> + * register one or more device_interfaces on each bus that it >>> + * successfully connects to. >>> + * >>> + * This example device only registers a single interface >>> + */ >>> +static int >>> +virtio_console_device_bus_connect(struct vbus_device *dev, struct vb= us *vbus) >>> +{ >>> + struct _virtio_console *priv =3D vdev_to_priv(dev); >>> + struct virtio_device_interface *intf =3D &priv->intf; >>> + >>> + /* We only allow one bus to connect */ >>> + if (priv->connected) >>> + return -EBUSY; >>> + >>> + kobject_get(dev->kobj); /* released when we release the intf */ >>> + >>> + intf->id.vendor =3D 0x1af4; >>> + intf->id.device =3D VIRTIO_ID_CONSOLE; >>> + >>> + intf->ops =3D &virtio_console_intf_ops; >>> + >>> + priv->connected =3D true; >>> + >>> + /* >>> + * Our example only registers one interface. If you need >>> + * more, simply call interface_register() multiple times >>> + */ >>> + return virtio_device_interface_register(dev, vbus, intf); >>> +} >>> + >>> +/* >>> + * This is called whenever the admin removes the symbolic link betwe= en >>> + * a bus in /config/vbus/buses and our device. >>> + */ >>> +static int >>> +virtio_console_device_bus_disconnect(struct vbus_device *dev, struct= vbus *vbus) >>> +{ >>> + struct _virtio_console *priv =3D vdev_to_priv(dev); >>> + struct virtio_device_interface *intf =3D &priv->intf; >>> + >>> + if (!priv->connected) >>> + return -EINVAL; >>> + >>> + virtio_device_interface_unregister(intf); >>> + >>> + priv->connected =3D false; >>> + >>> + return 0; >>> +} >>> + >>> +static void >>> +virtio_console_device_release(struct vbus_device *dev) >>> +{ >>> + struct _virtio_console *priv =3D vdev_to_priv(dev); >>> + >>> + unregister_chrdev_region(priv->devno, 1); >>> + kfree(priv); >>> + >>> + module_put(THIS_MODULE); >>> +} >>> + >>> + >>> +static struct vbus_device_ops virtio_console_device_ops =3D { >>> + .bus_connect =3D virtio_console_device_bus_connect, >>> + .bus_disconnect =3D virtio_console_device_bus_disconnect, >>> + .release =3D virtio_console_device_release, >>> +}; >>> + >>> +#define VIRTIO_CONSOLE_TYPE "virtio-console" >>> + >>> +/* >>> + * -------------------------------------------- >>> + * Character Device >>> + * -------------------------------------------- >>> + */ >>> + >>> +static int >>> +virtio_console_chardev_open(struct inode *inode, struct file *filp) >>> +{ >>> + struct _virtio_console *priv; >>> + >>> + priv =3D container_of(inode->i_cdev, struct _virtio_console, cdev);= >>> + >>> + kobject_get(priv->dev.kobj); /* released when we release the cd */ >>> + filp->private_data =3D priv; >>> + >>> + return 0; >>> +} >>> + >>> +static ssize_t >>> +virtio_console_chardev_read(struct file *filp, char __user *buf, siz= e_t len, >>> + loff_t *ppos) >>> +{ >>> + struct _virtio_console *priv =3D filp->private_data; >>> + >>> + return -EINVAL; >>> +} >>> + >>> +static ssize_t >>> +virtio_console_chardev_write(struct file *filp, const char __user *b= uf, >>> + size_t len, loff_t *ppos) >>> +{ >>> + struct _virtio_console *priv =3D filp->private_data; >>> + >>> + return -EINVAL; >>> +} >>> + >>> +static int >>> +virtio_console_chardev_release(struct inode *inode, struct file *fil= p) >>> +{ >>> + struct _virtio_console *priv =3D filp->private_data; >>> + >>> + kobject_put(priv->dev.kobj); >>> + >>> + return 0; >>> +} >>> + >>> +static const struct file_operations virtio_console_chardev_ops =3D {= >>> + .open =3D virtio_console_chardev_open, >>> + .read =3D virtio_console_chardev_read, >>> + .write =3D virtio_console_chardev_write, >>> + .release =3D virtio_console_chardev_release, >>> +}; >>> + >>> +/* >>> + * -------------------------------------------- >>> + * Attributes >>> + * -------------------------------------------- >>> + */ >>> + >>> +/* >>> + * Interface attributes show up as files under >>> + * /sys/vbus/devices/$devid >>> + */ >>> + >>> +static ssize_t >>> +devno_show(struct vbus_device *dev, struct vbus_device_attribute *at= tr, >>> + char *buf) >>> +{ >>> + struct _virtio_console *priv =3D vdev_to_priv(dev); >>> + >>> + return snprintf(buf, PAGE_SIZE, "%d:%d\n", >>> + MAJOR(priv->devno), MINOR(priv->devno)); >>> +} >>> + >>> +static struct vbus_device_attribute attr_devno =3D >>> + __ATTR_RO(devno); >>> + >>> +static struct attribute *attrs[] =3D { >>> + &attr_devno.attr, >>> + NULL, >>> +}; >>> + >>> +static struct attribute_group virtio_console_attr_group =3D { >>> + .attrs =3D attrs, >>> +}; >>> + >>> +/* >>> + * -------------------------------------------- >>> + * Device >>> + * -------------------------------------------- >>> + */ >>> + >>> +/* >>> + * This is called whenever the admin instantiates our devclass via >>> + * "mkdir /config/vbus/devices/$(inst)/virtio-console" >>> + */ >>> +static int >>> +virtio_console_device_create(struct vbus_devclass *dc, >>> + struct vbus_device **vdev) >>> +{ >>> + struct _virtio_console *priv; >>> + struct vbus_device *_vdev; >>> + int ret; >>> + >>> + priv =3D kzalloc(sizeof(*priv), GFP_KERNEL); >>> + if (!priv) >>> + return -ENOMEM; >>> + >>> + spin_lock_init(&priv->lock); >>> + >>> + /* >>> + * cdev init >>> + */ >>> + ret =3D alloc_chrdev_region(&priv->devno, TTY_MAJOR, >>> + 1, VIRTIO_CONSOLE_TYPE); >>> + if (ret < 0) >>> + goto out_free; >>> + >>> + cdev_init(&priv->cdev, &virtio_console_chardev_ops); >>> + priv->cdev.owner =3D THIS_MODULE; >>> + priv->cdev.ops =3D &virtio_console_chardev_ops; >>> + ret =3D cdev_add(&priv->cdev, priv->devno, 1); >>> + if (ret < 0) >>> + goto out; >>> + >>> + /* >>> + * vbus init >>> + */ >>> + _vdev =3D &priv->dev; >>> + >>> + _vdev->type =3D VIRTIO_CONSOLE_TYPE; >>> + _vdev->ops =3D &virtio_console_device_ops; >>> + _vdev->attrs =3D &virtio_console_attr_group; >>> + >>> + *vdev =3D _vdev; >>> + >>> + /* >>> + * We don't need a try_get because the reference is held by the >>> + * infrastructure during a create() operation. This is released >>> + * when our device is released >>> + */ >>> + __module_get(THIS_MODULE); >>> + >>> + return 0; >>> + >>> +out: >>> + unregister_chrdev_region(priv->devno, 1); >>> +out_free: >>> + kfree(priv); >>> + return ret; >>> +} >>> + >>> +static struct vbus_devclass_ops virtio_console_devclass_ops =3D { >>> + .create =3D virtio_console_device_create, >>> +}; >>> + >>> +static struct vbus_devclass virtio_console_devclass =3D { >>> + .name =3D VIRTIO_CONSOLE_TYPE, >>> + .ops =3D &virtio_console_devclass_ops, >>> + .owner =3D THIS_MODULE, >>> +}; >>> + >>> +static int __init virtio_console_init(void) >>> +{ >>> + return vbus_devclass_register(&virtio_console_devclass); >>> +} >>> + >>> +static void __exit virtio_console_cleanup(void) >>> +{ >>> + vbus_devclass_unregister(&virtio_console_devclass); >>> +} >>> + >>> +module_init(virtio_console_init); >>> +module_exit(virtio_console_cleanup); >>> =20 >>> =20 >> =20 >> =20 > > > =20 --------------enig2D0F6F7860F9EE3AD6DE7262 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.11 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkorGg4ACgkQlOSOBdgZUxns4QCfYI2dqYoOmLJcuij9dY13rcXE Xn8An3vsfS8vG77/iPeJWuMGVOes46GY =9HAA -----END PGP SIGNATURE----- --------------enig2D0F6F7860F9EE3AD6DE7262--