All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anthony Liguori <anthony@codemonkey.ws>
To: Blue Swirl <blauwirbel@gmail.com>
Cc: Chris Wright <chrisw@redhat.com>, Gleb Natapov <gleb@redhat.com>,
	kvm@vger.kernel.org, qemu-devel@nongnu.org,
	Markus Armbruster <armbru@redhat.com>,
	Avi Kivity <avi@redhat.com>
Subject: Re: [Qemu-devel] KVM call minutes for Feb 8
Date: Mon, 14 Feb 2011 14:53:13 -0600	[thread overview]
Message-ID: <4D599639.2030508@codemonkey.ws> (raw)
In-Reply-To: <AANLkTi=k9Q-5Qfe9JvzR1q3mKWHkR0nAZpsNCkJdPTu0@mail.gmail.com>

On 02/14/2011 11:31 AM, Blue Swirl wrote:
> I don't understand. The caller just does
> if (isa_serial_init()) {
>    error();
> }
> or
> if (serial_init()) {
>    error();
> }
>
> If you mean inside isa_serial_init() vs. serial_init(), that may be
> true since isa_serial_init has to check for qdev failures, but the to
> the caller both should be identical.
>    

The problem with qdev is there's too much boiler plate code which makes 
it hard to give examples :-)  Here's precisely what I'm talking about:

static int serial_isa_initfn(ISADevice *dev)
{
     static int index;
     ISASerialState *isa = DO_UPCAST(ISASerialState, dev, dev);
     SerialState *s = &isa->state;

     if (isa->index == -1)
         isa->index = index;
     if (isa->index >= MAX_SERIAL_PORTS)
         return -1;
     if (isa->iobase == -1)
         isa->iobase = isa_serial_io[isa->index];
     if (isa->isairq == -1)
         isa->isairq = isa_serial_irq[isa->index];
     index++;

     s->baudbase = 115200;
     isa_init_irq(dev, &s->irq, isa->isairq);
     serial_init_core(s);
     qdev_set_legacy_instance_id(&dev->qdev, isa->iobase, 3);

     register_ioport_write(isa->iobase, 8, 1, serial_ioport_write, s);
     register_ioport_read(isa->iobase, 8, 1, serial_ioport_read, s);
     isa_init_ioport_range(dev, isa->iobase, 8);
     return 0;
}

SerialState *serial_init(int base, qemu_irq irq, int baudbase,
                          CharDriverState *chr)
{
     SerialState *s;

     s = qemu_mallocz(sizeof(SerialState));

     s->irq = irq;
     s->baudbase = baudbase;
     s->chr = chr;
     serial_init_core(s);

     vmstate_register(NULL, base, &vmstate_serial, s);

     register_ioport_write(base, 8, 1, serial_ioport_write, s);
     register_ioport_read(base, 8, 1, serial_ioport_read, s);
     return s;
}

static ISADeviceInfo serial_isa_info = {
     .qdev.name  = "isa-serial",
     .qdev.size  = sizeof(ISASerialState),
     .qdev.vmsd  = &vmstate_isa_serial,
     .init       = serial_isa_initfn,
     .qdev.props = (Property[]) {
         DEFINE_PROP_UINT32("index", ISASerialState, index,   -1),
         DEFINE_PROP_HEX32("iobase", ISASerialState, iobase,  -1),
         DEFINE_PROP_UINT32("irq",   ISASerialState, isairq,  -1),
         DEFINE_PROP_CHR("chardev",  ISASerialState, state.chr),
         DEFINE_PROP_END_OF_LIST(),
     },
};

static void serial_register_devices(void)
{
     isa_qdev_register(&serial_isa_info);
}

device_init(serial_register_devices)


To create a device, I need to do:

{
      ISADevice *dev;

      dev = isa_create("isa-serial");
      if (dev == NULL) {
           return error;
      }
      if (qdev_set_uint32(&dev->qdev, "index", index)) {
           goto err;
      }
      if (qdev_set_uint32(&dev->qdev, "iobase", iobase)) {
           goto err;
      }
      if (qdev_set_uint32(&dev->qdev, "irq", irq)) {
          goto err;
      }
      if (qdev_set_chr(&dev->qdev, "chardev", chr)) {
          goto err;
      }
      if (qdev_init(&dev->qdev)) {
          goto err;
      }
      return 0;
err:
      qdev_destroy(&dev->qdev);
      return -1;
}

This is simply not a reasonable API to use to create devices.  There are 
two ways we can make this more managable.  The first is gobject-style 
vararg constructor coupled with a type safe wrapper.  So...

ISASerialDevice *isa_serial_device_new(uint32_t index, uint32_t iobase, 
uint32_t irq, CharDriverState *chr)
{
       return isa_device_create_va("isa-seral", "index", index, 
"iobase", iobase, "irq", irq, "chardev", chr, NULL);
}

Now this can be used in a reasonable fashion.  However, we can do even 
better if we change the way qdev is done.   Consider the following:

SerialState *serial_init(int base, qemu_irq irq, int baudbase,
                          CharDriverState *chr)
{
     SerialState *s;

     s = qemu_mallocz(sizeof(SerialState));

     s->irq = irq;
     s->baudbase = baudbase;
     s->chr = chr;
     serial_init_core(s);

     vmstate_register(NULL, base, &vmstate_serial, s);

     register_ioport_write(base, 8, 1, serial_ioport_write, s);
     register_ioport_read(base, 8, 1, serial_ioport_read, s);
     return s;
}

ISASerialDevice *isa_serial_new(uint32_t index, uint32_t iobase, 
uint32_t irq, CharDriverState *chr)
{
     static int index;
     ISADevice *dev = isa_create("isa-serial");
     ISASerialState *isa = DO_UPCAST(ISASerialState, dev, dev);
     SerialState *s = &isa->state;

     if (index == -1)
         index = index;
     if (index >= MAX_SERIAL_PORTS)
         return NULL;
     if (iobase == -1)
         iobase = isa_serial_io[index];
     if (isairq == -1)
         isairq = isa_serial_irq[index];
     index++;

     s->baudbase = 115200;
     isa_init_irq(dev, &s->irq, isairq);
     serial_init_core(s);
     qdev_set_legacy_instance_id(&dev->qdev, isa->iobase, 3);

     register_ioport_write(isa->iobase, 8, 1, serial_ioport_write, s);
     register_ioport_read(isa->iobase, 8, 1, serial_ioport_read, s);
     isa_init_ioport_range(dev, isa->iobase, 8);
     return isa;
}

static ISADevice *isa_seral_init_qdev(QemuOpts *opts)
{
       uint32_t index = qemu_opt_get_uint32(opts, "index", -1);
       uint32_t irq = qemu_opt_get_uint32(opts, "irq", -1);
       uint32_t iobase = qemu_opt_get_uint32(opts, "iobase", -1);
       CharDriverState *chr = qemu_opt_get_chr(opts, "chardev");

       return isa_serial_new(index, irq, iobase, chr);
}

static void serial_register_devices(void)
{
     isa_qdev_register("isa-serial", isa_serial_init_qdev);
}

device_init(serial_register_devices)

The advantage of this model is that we can totally ignore the factory 
interface for devices that we don't care about exposing to the user.  So 
the isa_seral_init_qdev part is totally optional.

Regards,

Anthony Liguori

WARNING: multiple messages have this Message-ID (diff)
From: Anthony Liguori <anthony@codemonkey.ws>
To: Blue Swirl <blauwirbel@gmail.com>
Cc: Chris Wright <chrisw@redhat.com>, Gleb Natapov <gleb@redhat.com>,
	kvm@vger.kernel.org, Markus Armbruster <armbru@redhat.com>,
	qemu-devel@nongnu.org, Avi Kivity <avi@redhat.com>
Subject: Re: [Qemu-devel] KVM call minutes for Feb 8
Date: Mon, 14 Feb 2011 14:53:13 -0600	[thread overview]
Message-ID: <4D599639.2030508@codemonkey.ws> (raw)
In-Reply-To: <AANLkTi=k9Q-5Qfe9JvzR1q3mKWHkR0nAZpsNCkJdPTu0@mail.gmail.com>

On 02/14/2011 11:31 AM, Blue Swirl wrote:
> I don't understand. The caller just does
> if (isa_serial_init()) {
>    error();
> }
> or
> if (serial_init()) {
>    error();
> }
>
> If you mean inside isa_serial_init() vs. serial_init(), that may be
> true since isa_serial_init has to check for qdev failures, but the to
> the caller both should be identical.
>    

The problem with qdev is there's too much boiler plate code which makes 
it hard to give examples :-)  Here's precisely what I'm talking about:

static int serial_isa_initfn(ISADevice *dev)
{
     static int index;
     ISASerialState *isa = DO_UPCAST(ISASerialState, dev, dev);
     SerialState *s = &isa->state;

     if (isa->index == -1)
         isa->index = index;
     if (isa->index >= MAX_SERIAL_PORTS)
         return -1;
     if (isa->iobase == -1)
         isa->iobase = isa_serial_io[isa->index];
     if (isa->isairq == -1)
         isa->isairq = isa_serial_irq[isa->index];
     index++;

     s->baudbase = 115200;
     isa_init_irq(dev, &s->irq, isa->isairq);
     serial_init_core(s);
     qdev_set_legacy_instance_id(&dev->qdev, isa->iobase, 3);

     register_ioport_write(isa->iobase, 8, 1, serial_ioport_write, s);
     register_ioport_read(isa->iobase, 8, 1, serial_ioport_read, s);
     isa_init_ioport_range(dev, isa->iobase, 8);
     return 0;
}

SerialState *serial_init(int base, qemu_irq irq, int baudbase,
                          CharDriverState *chr)
{
     SerialState *s;

     s = qemu_mallocz(sizeof(SerialState));

     s->irq = irq;
     s->baudbase = baudbase;
     s->chr = chr;
     serial_init_core(s);

     vmstate_register(NULL, base, &vmstate_serial, s);

     register_ioport_write(base, 8, 1, serial_ioport_write, s);
     register_ioport_read(base, 8, 1, serial_ioport_read, s);
     return s;
}

static ISADeviceInfo serial_isa_info = {
     .qdev.name  = "isa-serial",
     .qdev.size  = sizeof(ISASerialState),
     .qdev.vmsd  = &vmstate_isa_serial,
     .init       = serial_isa_initfn,
     .qdev.props = (Property[]) {
         DEFINE_PROP_UINT32("index", ISASerialState, index,   -1),
         DEFINE_PROP_HEX32("iobase", ISASerialState, iobase,  -1),
         DEFINE_PROP_UINT32("irq",   ISASerialState, isairq,  -1),
         DEFINE_PROP_CHR("chardev",  ISASerialState, state.chr),
         DEFINE_PROP_END_OF_LIST(),
     },
};

static void serial_register_devices(void)
{
     isa_qdev_register(&serial_isa_info);
}

device_init(serial_register_devices)


To create a device, I need to do:

{
      ISADevice *dev;

      dev = isa_create("isa-serial");
      if (dev == NULL) {
           return error;
      }
      if (qdev_set_uint32(&dev->qdev, "index", index)) {
           goto err;
      }
      if (qdev_set_uint32(&dev->qdev, "iobase", iobase)) {
           goto err;
      }
      if (qdev_set_uint32(&dev->qdev, "irq", irq)) {
          goto err;
      }
      if (qdev_set_chr(&dev->qdev, "chardev", chr)) {
          goto err;
      }
      if (qdev_init(&dev->qdev)) {
          goto err;
      }
      return 0;
err:
      qdev_destroy(&dev->qdev);
      return -1;
}

This is simply not a reasonable API to use to create devices.  There are 
two ways we can make this more managable.  The first is gobject-style 
vararg constructor coupled with a type safe wrapper.  So...

ISASerialDevice *isa_serial_device_new(uint32_t index, uint32_t iobase, 
uint32_t irq, CharDriverState *chr)
{
       return isa_device_create_va("isa-seral", "index", index, 
"iobase", iobase, "irq", irq, "chardev", chr, NULL);
}

Now this can be used in a reasonable fashion.  However, we can do even 
better if we change the way qdev is done.   Consider the following:

SerialState *serial_init(int base, qemu_irq irq, int baudbase,
                          CharDriverState *chr)
{
     SerialState *s;

     s = qemu_mallocz(sizeof(SerialState));

     s->irq = irq;
     s->baudbase = baudbase;
     s->chr = chr;
     serial_init_core(s);

     vmstate_register(NULL, base, &vmstate_serial, s);

     register_ioport_write(base, 8, 1, serial_ioport_write, s);
     register_ioport_read(base, 8, 1, serial_ioport_read, s);
     return s;
}

ISASerialDevice *isa_serial_new(uint32_t index, uint32_t iobase, 
uint32_t irq, CharDriverState *chr)
{
     static int index;
     ISADevice *dev = isa_create("isa-serial");
     ISASerialState *isa = DO_UPCAST(ISASerialState, dev, dev);
     SerialState *s = &isa->state;

     if (index == -1)
         index = index;
     if (index >= MAX_SERIAL_PORTS)
         return NULL;
     if (iobase == -1)
         iobase = isa_serial_io[index];
     if (isairq == -1)
         isairq = isa_serial_irq[index];
     index++;

     s->baudbase = 115200;
     isa_init_irq(dev, &s->irq, isairq);
     serial_init_core(s);
     qdev_set_legacy_instance_id(&dev->qdev, isa->iobase, 3);

     register_ioport_write(isa->iobase, 8, 1, serial_ioport_write, s);
     register_ioport_read(isa->iobase, 8, 1, serial_ioport_read, s);
     isa_init_ioport_range(dev, isa->iobase, 8);
     return isa;
}

static ISADevice *isa_seral_init_qdev(QemuOpts *opts)
{
       uint32_t index = qemu_opt_get_uint32(opts, "index", -1);
       uint32_t irq = qemu_opt_get_uint32(opts, "irq", -1);
       uint32_t iobase = qemu_opt_get_uint32(opts, "iobase", -1);
       CharDriverState *chr = qemu_opt_get_chr(opts, "chardev");

       return isa_serial_new(index, irq, iobase, chr);
}

static void serial_register_devices(void)
{
     isa_qdev_register("isa-serial", isa_serial_init_qdev);
}

device_init(serial_register_devices)

The advantage of this model is that we can totally ignore the factory 
interface for devices that we don't care about exposing to the user.  So 
the isa_seral_init_qdev part is totally optional.

Regards,

Anthony Liguori

  reply	other threads:[~2011-02-14 20:53 UTC|newest]

Thread overview: 118+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-08 15:55 KVM call minutes for Feb 8 Chris Wright
2011-02-08 15:55 ` [Qemu-devel] " Chris Wright
2011-02-08 16:14 ` Stefan Hajnoczi
2011-02-08 16:14   ` [Qemu-devel] " Stefan Hajnoczi
2011-02-08 16:39 ` [Qemu-devel] " Anthony Liguori
2011-02-08 16:39   ` Anthony Liguori
2011-02-08 17:13 ` Markus Armbruster
2011-02-08 17:13   ` Markus Armbruster
2011-02-08 19:02   ` Peter Maydell
2011-02-08 21:11     ` Anthony Liguori
2011-02-08 21:11       ` Anthony Liguori
2011-02-09  8:11     ` Markus Armbruster
2011-02-09  8:20       ` Peter Maydell
2011-02-09  9:02         ` Markus Armbruster
2011-02-08 19:30   ` Alexander Graf
2011-02-08 19:30   ` Aurelien Jarno
2011-02-09  8:23     ` Markus Armbruster
2011-02-09 10:43     ` Anthony Liguori
2011-02-09 10:43       ` Anthony Liguori
2011-02-09 17:38       ` Blue Swirl
2011-02-09 17:38         ` Blue Swirl
2011-02-08 21:12   ` Anthony Liguori
2011-02-09  8:01     ` Markus Armbruster
2011-02-09 10:31       ` Anthony Liguori
2011-02-09 12:28         ` Markus Armbruster
2011-02-09 14:44           ` Anthony Liguori
2011-02-09 17:48             ` Blue Swirl
2011-02-09 17:48               ` Blue Swirl
2011-02-09 19:53               ` Anthony Liguori
2011-02-09 19:59               ` Anthony Liguori
2011-02-09 20:15                 ` Blue Swirl
2011-02-10  7:47                   ` Anthony Liguori
2011-02-10  8:16                     ` Peter Maydell
2011-02-10  8:36                       ` Anthony Liguori
2011-02-10  9:04                         ` Peter Maydell
2011-02-10 10:13                           ` Anthony Liguori
2011-02-10 10:38                             ` Peter Maydell
2011-02-10 11:24                               ` Gleb Natapov
2011-02-10 11:24                                 ` Gleb Natapov
2011-02-10 12:23                               ` Anthony Liguori
2011-02-10 13:06                                 ` Peter Maydell
2011-02-10 19:17                       ` Scott Wood
2011-02-10 19:17                         ` Scott Wood
2011-02-10 19:22                         ` Peter Maydell
2011-02-10 19:22                           ` Peter Maydell
2011-02-10 19:29                           ` Scott Wood
2011-02-10 19:29                             ` Scott Wood
2011-02-10  9:07                     ` Gleb Natapov
2011-02-10 10:00                       ` Anthony Liguori
2011-02-10 10:10                         ` Gleb Natapov
2011-02-10 10:19                           ` Anthony Liguori
2011-02-10 10:49                             ` Gleb Natapov
2011-02-10 12:47                               ` Anthony Liguori
2011-02-10 13:12                                 ` Gleb Natapov
2011-02-10 10:25                       ` Avi Kivity
2011-02-10 10:25                         ` Avi Kivity
2011-02-10 11:13                         ` Gleb Natapov
2011-02-10 11:13                           ` Gleb Natapov
2011-02-10 12:51                           ` Anthony Liguori
2011-02-10 12:51                             ` Anthony Liguori
2011-02-10 13:00                             ` Avi Kivity
2011-02-10 13:00                               ` Avi Kivity
2011-02-10 13:29                               ` Gleb Natapov
2011-02-10 13:29                                 ` Gleb Natapov
2011-02-10 14:00                               ` Anthony Liguori
2011-02-10 14:00                                 ` Anthony Liguori
2011-02-10 13:27                             ` Gleb Natapov
2011-02-10 13:27                               ` Gleb Natapov
2011-02-10 14:04                               ` Anthony Liguori
2011-02-10 14:20                                 ` Gleb Natapov
2011-02-10 16:05                                   ` Anthony Liguori
2011-02-11 18:14                                     ` Blue Swirl
2011-02-11 18:14                                       ` Blue Swirl
2011-02-13  9:24                                       ` Gleb Natapov
2011-02-13  9:24                                         ` Gleb Natapov
2011-02-13 15:31                                       ` Anthony Liguori
2011-02-13 15:31                                         ` Anthony Liguori
2011-02-13 19:37                                         ` Blue Swirl
2011-02-13 19:37                                           ` Blue Swirl
2011-02-13 19:57                                           ` Anthony Liguori
2011-02-13 19:57                                             ` Anthony Liguori
2011-02-13 21:00                                             ` Blue Swirl
2011-02-13 21:00                                               ` Blue Swirl
2011-02-13 22:42                                               ` Anthony Liguori
2011-02-13 22:42                                                 ` Anthony Liguori
2011-02-14 17:31                                                 ` Blue Swirl
2011-02-14 17:31                                                   ` Blue Swirl
2011-02-14 20:53                                                   ` Anthony Liguori [this message]
2011-02-14 20:53                                                     ` Anthony Liguori
2011-02-14 21:25                                                     ` Blue Swirl
2011-02-14 21:25                                                       ` Blue Swirl
2011-02-14 21:47                                                       ` Anthony Liguori
2011-02-14 21:47                                                         ` Anthony Liguori
2011-02-15 17:11                                                         ` Blue Swirl
2011-02-15 17:11                                                           ` Blue Swirl
2011-02-15 23:07                                                           ` Anthony Liguori
2011-02-15 23:07                                                             ` Anthony Liguori
2011-02-16  9:52                                                             ` Gleb Natapov
2011-02-16  9:52                                                               ` Gleb Natapov
2011-02-14  9:44                                             ` Paolo Bonzini
2011-02-14  9:44                                               ` Paolo Bonzini
2011-02-10 10:29                     ` Avi Kivity
2011-02-13 15:38                       ` Anthony Liguori
2011-02-13 15:38                         ` Anthony Liguori
2011-02-13 15:56                         ` Avi Kivity
2011-02-13 16:56                           ` Anthony Liguori
2011-02-13 18:08                             ` Gleb Natapov
2011-02-13 18:08                               ` Gleb Natapov
2011-02-13 19:38                               ` Anthony Liguori
2011-02-14 10:23                                 ` Gleb Natapov
2011-02-13 21:24                             ` Peter Maydell
2011-02-13 21:24                               ` Peter Maydell
2011-02-13 22:43                               ` Anthony Liguori
2011-02-13 22:43                                 ` Anthony Liguori
2011-02-13 23:35                                 ` Peter Maydell
2011-02-13 15:39                       ` Anthony Liguori
2011-02-13 15:39                         ` Anthony Liguori
2011-02-11 17:54                     ` Blue Swirl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4D599639.2030508@codemonkey.ws \
    --to=anthony@codemonkey.ws \
    --cc=armbru@redhat.com \
    --cc=avi@redhat.com \
    --cc=blauwirbel@gmail.com \
    --cc=chrisw@redhat.com \
    --cc=gleb@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.