SlideShare a Scribd company logo
1 of 59
Download to read offline
VIRTIO
                          Yi-Man Ma & Chang-Jung Lin




                                                       2011 / 01 / 05
Thursday, January 5, 12                                                 1
Agenda

                          Introduction
                          Architecture
                          VirtQueue & Vring
                          Five APIs
                          Adding a new Virtio Device / Driver

Thursday, January 5, 12                                         2
INTRODUCTION




Thursday, January 5, 12                  3
ct ion
             du
       In tro



               Create a shortcut to deal with IO Request
                          Coalesced IO Request by VirtQueue




Thursday, January 5, 12                                       4
ct ion
             du
       In tro

                                          Device emulation in full virtualization
                                            and paravirtualization environments

                                Guest operating system                     Guest operating system
                                                                                               Para-drivers
                                                       Traps                                    Interfaces
                              Hypervisor                                 Hypervisor
                          (Full virtualization)   Device emulation   (Full virtualization)   Device emulation



                                          Hardware                                   Hardware




Thursday, January 5, 12                                                                                         5
ARCHITECTURE




Thursday, January 5, 12                  6
tu re
                 hit ec
      A rc
                                  GUEST1      GUEST2


                                  Transport   Transport

                                                              Other
                                   QEMU          QEMU
                                                            Processes


                                           KVM
                                                                Host

                                                 Hardware




Thursday, January 5, 12                                                 7
tu re
                 hit ec
      A rc

                                      Virtio Driver
                                                            Guest
                                  Virtio PCI Controller


                                          Vring           Transport

                                  Virtio PCI Controller
                                                            QEMU
                                      Virtio Device



Thursday, January 5, 12                                               8
VIRTQUEUE & VRING




Thursday, January 5, 12                       9
ue ue
          Vi rtQ




                          Each Device have it’s own virtqueues.
                          Queue Requests




Thursday, January 5, 12                                           10
V ri ng




                Each virtqueue have it’s own vring.
                Memory Mapped between QEMU & Guest
                Actual memory layout for queue



Thursday, January 5, 12                               11
ue ng
                ue ri
          Vi rtQ V

      Find virtqueue              Virtio Driver
                                                        Guest
                              Virtio PCI Controller
                          Alloc Vring
                                        Vring         Transport

                              Virtio PCI Controller
       Vring GPA                                        QEMU
                                  Virtio Device



Thursday, January 5, 12                                           12
ue ng
                ue ri
          Vi rtQ V

                                             Virtio Driver


                          Virt queue Write                   Virt queue Read



                              Vring                             Vring




                                             Virtio Device



Thursday, January 5, 12                                                        13
ue ng
                ue ri
          Vi rtQ V

                          Virtio Driver


                           Virt queue Write   Virt queue Read



                               Vring             Vring




                          Virtio Device



Thursday, January 5, 12                                         14
ue ng
                ue ri
          Vi rtQ V

                                    Virtio Driver
                          Add Buf

                                     Virt queue Write                Virt queue Read



                                         Vring           In    Out      Vring
                                                        data




                                    Virtio Device



Thursday, January 5, 12                                                                15
ue ng
                ue ri
          Vi rtQ V

                          Kick   Virtio Driver


                                  Virt queue Write                Virt queue Read



                                      Vring           In    Out      Vring
                                                     data




                                 Virtio Device



Thursday, January 5, 12                                                             16
ue ng
                ue ri
          Vi rtQ V

                          Virtio Driver


                           Virt queue Write                 Virt queue Read



                               Vring           In     Out      Vring
                                              data




                                                POP

                          Virtio Device



Thursday, January 5, 12                                                       17
ue ng
                ue ri
          Vi rtQ V

                          Virtio Driver


                           Virt queue Write               Virt queue Read



                               Vring          In   Out       Vring
                                                   data




                                                   Push

                          Virtio Device



Thursday, January 5, 12                                                     18
ue ng
                ue ri
          Vi rtQ V

                          Virtio Driver                   Get Buf

                           Virt queue Write                 Virt queue Read


                                                   data

                               Vring          In   Out         Vring




                          Virtio Device



Thursday, January 5, 12                                                       19
FIVE APIS




Thursday, January 5, 12               20
APIs
         F ive

                             add_buf
                             kick
                             get_buf
                             disable_cb
                             enable_cb




Thursday, January 5, 12                   21
Is et buf
                      AP g
         F ive add &



                                add_buf
                                    expose buffer to other end
                                get_buf
                                    get the next used buffer




Thursday, January 5, 12                                          22
Is
                      AP ick
         F ive            k




                              kick
                               update after add_buf
                               notify QEMU to deal with the data




Thursday, January 5, 12                                            23
Is ative
                      AP el
         F ive            C Br



                          disable_cb
                           disable callbacks
                           not necessarily synchronous
                           unreliable and only useful as an optimization
                          enable_cb
                           restart callbacks after disable_cb


Thursday, January 5, 12                                                    24
NEW DEVICE / DRIVER




Thursday, January 5, 12                         25
river
            D
        New


                              include/linux/virtio_ids.h
                              include/linux/virtio.h
                              include/linux/virtio_[device].h
                              drivers/virtio/virtio_[device].c
                              drivers/virtio/virtio_pci.c




Thursday, January 5, 12                                          26
ev ice
           D
       New



                              hw/virtio-[device].c
                              hw/virtio-[device].h
                              hw/pci.h
                              hw/virtio_pci.c




Thursday, January 5, 12                              27
tu re
                 hit ec
      A rc

                                      Virtio Driver
                                                            Guest
                                  Virtio PCI Controller


                                         Vring            Transport

                                  Virtio PCI Controller
                                                            QEMU
                                     Virtio Device



Thursday, January 5, 12                                               28
ev iceeid
            D devic                      Virtio PCI Controller
       New add




                           Modify qemu-src/hw/pci.h
                          #define PCI_DEVICE_ID_VIRTIO_[device]   0x1004




Thursday, January 5, 12                                                   29
icde r
                          ev le
            Dal han                  Virtio PCI Controller
       Newiniti

                Modify qemu-src/hw/virtio-pci.c
           static int virtio_[device]_init_pci(PCIDevice *pci_dev)
           {
              VirtIOPCIProxy *proxy =
                             DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
              VirtIODevice *vdev;
              vdev = virtio_[device]_init(&pci_dev->qdev);
              if (!vdev) {
                  return -1;
              }
              virtio_init_pci(proxy, vdev);
              return 0;
           }

Thursday, January 5, 12                                                     30
ev  ce
                             idler
            Dt han                   Virtio PCI Controller
       New exi


                Modify qemu-src/hw/virtio-pci.c
           static int virtio_[device]_exit_pci (PCIDevice *pci_dev)
           {
              VirtIOPCIProxy *proxy =
                             DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

                 virtio_pci_stop_ioeventfd(proxy);
                 virtio_[device]_exit(proxy->vdev);

                 return virtio_exit_pci(pci_dev);
           }




Thursday, January 5, 12                                                     31
ieciefo
                          ev n
           D evic
         wdd d
                                      Virtio PCI Controller
       Nea

                Modify qemu-src/hw/virtio-pci.c
           static PCIDeviceInfo virtio_info[] = {
              {
           
     .qdev.name = "virtio-[device]-pci",
                
.qdev.alias = "virtio-[device]",
                
.init           = virtio_[device]_init_pci,
                 
 exit
                 .               = virtio_[device]_exit_pci,
                 
 device_id = PCI_DEVICE_ID_VIRTIO_[device],
                 .
                
.qdev.props = (Property[]) {
                           DEFINE_VIRTIO_COMMON_FEATURES(
                                             VirtIOPCIProxy, host_features),
                     
      DEFINE_PROP_END_OF_LIST(),
                 
,
                 }
                 ...... (Skip)


Thursday, January 5, 12                                                        32
tu re
                 hit ec
      A rc

                                      Virtio Driver
                                                            Guest
                                  Virtio PCI Controller


                                         Vring            Transport

                                  Virtio PCI Controller
                                                            QEMU
                                     Virtio Device



Thursday, January 5, 12                                               33
ev ice
           D                                Virtio Device
       New




                          Add virtio-[device].c, virtio[device].h
                          Put them under the directory qemu-src/hw/




Thursday, January 5, 12                                               34
ivcce].h
                          ev i e
            D-[de                      Virtio Device
        ewirtio
       N v


           #ifndef _QEMU_VIRTIO_[device]_H

           #include "virtio.h“

           #define _QEMU_VIRTIO_[device]_H
           #define VIRTIO_ID_[device] [num] //ID for your device

           VirtIODevice *virtio_[device]_init(DeviceState *dev);
           void virtio_[device]_exit(VirtIODevice *vdev);

           #endif




Thursday, January 5, 12                                            35
ivcce].c
                          ev i e
            D-[de                         Virtio Device
        ewirtio
       N v
           #include          <qemu-common.h>
           #include         "qemu-error.h"
           #include         "virtio-[device].h"
           #include         "virtio.h"
           typedef struct VirtIO[device]
           {
               VirtIODevice vdev;
               VirtQueue *vq[num];
               DeviceState *qdev;
               int status;
           } VirtIO[device];
           struct virtio_[device]_config {
              int config;
           };

Thursday, January 5, 12                                   36
ictere
                          evuc u
            D str                    Virtio Device
        ewirtio
       N v

           struct VirtIODevice
           {
              ...........
              uint32_t (*get_features)(VirtIODevice *vdev, uint32_t
           requested_features);
              uint32_t (*bad_features)(VirtIODevice *vdev);
              void (*set_features)(VirtIODevice *vdev, uint32_t val);
              void (*get_config)(VirtIODevice *vdev, uint8_t *config);
              void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
              void (*reset)(VirtIODevice *vdev);
              void (*set_status)(VirtIODevice *vdev, uint8_t val);
              .......
           };



Thursday, January 5, 12                                                      37
iece nt
                          evl me
           Dimp                       Virtio Device
        eweed
       N n


           static VirtIO[device] *to_virtio_[device](VirtIODevice *vdev)
                  //Get device defined by yourself
           static uint32_t virtio_[device]_ get_features (VirtIODevice *vdev,
           uint32_t features);
           static uint32_t virtio_[device]_ bad_features (VirtIODevice
           *vdev);
           static void virtio_[device]_ set_features (VirtIODevice *vdev,
           uint32_t val);
           static void virtio_[device]_ get_config (VirtIODevice *vdev,
           uint8_t *config);
           static void virtio_[device]_ set_config (VirtIODevice *vdev, const
           uint8_t *config);


Thursday, January 5, 12                                                         38
iece nt
                          evl me
           Dimp                       Virtio Device
        eweed
       N n

           static void virtio_[device]_ reset (VirtIODevice *vdev);
           static void virtio_[device]_ set_status (VirtIODevice *vdev,
           uint8_t val);

           VirtIODevice *virtio_[device]_init(DeviceState *dev);
                     //Initialize your device
           void virtio_[device]_exit (VirtIODevice *vdev);

           static void virtio_[device]_save(QEMUFile *f, void *opaque);
           static int virtio_[device]_load(QEMUFile *f, void *opaque, int
           version_id);

           static void virtio_[device]_handle_out(VirtIODevice *vdev,
           VirtQueue *vq);


Thursday, January 5, 12                                                     39
itcees
                          ev ur
            Dt fea                   Virtio Device
        ew bou
       N a
           static uint32_t virtio_[device]_ get_features (VirtIODevice *vdev,
           uint32_t features)
           {
              VirtIO[device] *n = to_virtio_[device](vdev);
              return features;
           }
           static uint32_t virtio_[device]_ bad_features (VirtIODevice
           *vdev){
              uint32_t features = 0;
              return features;
           }
           static void virtio_[device]_ set_features (VirtIODevice *vdev,
           uint32_t val){
              VirtIOMy *n = to_virtio_[device](vdev);
           }


Thursday, January 5, 12                                                         40
e
                            icfig
           Dut c          evon       Virtio Device
       Newabo

           static void virtio_[device]_ get_config (VirtIODevice *vdev,
           uint8_t *config)
           {
              VirtIO[device] *n = to_virtio_[device](vdev);
              struct virtio_[device]_config cfg;
              stw_p(&cfg.config, n->status);
              memcpy(config, &cfg, sizeof(cfg));
           }
           static void virtio_[device]_ set_config (VirtIODevice *vdev, const
           uint8_t *config)
           {
              VirtIO[de vice] *n = to_virtio_[device](vdev);
              struct virtio_[device]_config cfg;
           }


Thursday, January 5, 12                                                        41
icee
                          eves t
           D& r
         w set
                                      Virtio Device
       Ne


           static void virtio_[device]_ reset (VirtIODevice *vdev)
           {
           }

           static void virtio_[device]_ set_status (VirtIODevice *vdev,
           uint8_t val)
           {
              VirtIO[device] *n = to_virtio_my(vdev);
           }




Thursday, January 5, 12                                                   42
e
                            icice
            Dial de       ev v        Virtio Device
       New init
           VirtIODevice *virtio_[device]_init(DeviceState *dev) {
               VirtIO[device] *s;

              s = (VirtIO[device]*)virtio_common_init("virtio-[device]",
           VIRTIO_ID_[device], sizeof(struct virtio_[device]_config),
           sizeof(VirtIO[device]));
              s->vdev.get_config = virtio_[device]get_config;
              s->vdev.set_config = virtio_[device]_set_config;
              s->vdev.get_features = virtio_[device]_get_features;
              s->vdev.set_features = virtio_[device]_set_features;
              s->vdev.bad_features = virtio_[device]_bad_features;
              s->vdev.reset = virtio_[device]_reset;
              s->vdev.set_status = virtio_[device]_set_status;

           .....


Thursday, January 5, 12                                                    43
e
                            icice
            Dial de       ev v       Virtio Device
       New init

           .....
             s->vdev.set_status = virtio_[device]_set_status;
               s->vq[0] = virtio_add_queue(&s->vdev, 1024,
           virtio_[device]_handle_out1);
               s->vq[1] = virtio_add_queue(&s->vdev, 1024,
           virtio_[device]_handle_out2);
               s->qdev = dev;
               s->status = 99;
               register_savevm(dev, "virtio-[device]”, 0, 2,
           virtio_[device]_save, virtio_[device]_load, s);

                 return &s->vdev;
           }



Thursday, January 5, 12                                         44
ivcce
                          ev i e
           Dit de                    Virtio Device
       New ex




           void virtio_[device]_exit (VirtIODevice *vdev)
           {
             VirtIO[device] *s = to_virtio_[device](vdev);
             unregister_savevm(s->qdev, "virtio-[device]“ , s);
           }




Thursday, January 5, 12                                           45
e iloae
                           v cd
           De &                       Virtio Device
       New sav


           static void virtio_[device]_save(QEMUFile *f, void *opaque)
           {
             VirtIO[device] *n = opaque;
             virtio_save(&n->vdev, f);
           }
           static int virtio_[device]_load(QEMUFile *f, void *opaque, int
           version_id)
           {
              VirtIO[device] *n = opaque;
              virtio_load(&n->vdev, f);
              return 0;
           }



Thursday, January 5, 12                                                     46
icler
                          ev d e
           Da han                    Virtio Device
       Newdat
           static void virtio_[device]_handle_out(VirtIODevice *vdev,
           VirtQueue *vq)
           {
               VirtIO[device] *n = to_virtio_my(vdev);
               VirtQueueElement elem1, elem2;
               int K;
               memset(&elem1, 0, sizeof(elem1));
               memset(&elem2, 0, sizeof(elem2));
               virtqueue_pop(vq, &elem1);
               if(memcmp(&elem1, &elem2, sizeof(elem1)))
                      printf("something happened!!!n");
               K = *((int*)elem1.out_sg[0].iov_base) + 1;
               memcpy(elem1.in_sg[0].iov_base, &K, sizeof(int));
               virtqueue_push(vq, &elem1, sizeof(int));
           }


Thursday, January 5, 12                                                 47
tu re
                 hit ec
      A rc

                                      Virtio Driver
                                                            Guest
                                  Virtio PCI Controller


                                         Vring            Transport

                                  Virtio PCI Controller
                                                            QEMU
                                     Virtio Device



Thursday, January 5, 12                                               48
r
                             eid
           Devic          rive       Virtio Driver
        New d


                Modify kernel-src/include/linux/virtio-ids.h

           #define VIRTIO_ID_[device]        number


                                           Should be the same number as
                                                  defined in qemu




Thursday, January 5, 12                                                   49
river
            D                               Virtio Driver
        New
           #include         "linux/virtio.h“
           #include        "linux/virtio_ids.h“
           #include        "linux/virtio_pci.h“
           #include        "virtio.h"
           static struct virtio_device_id id_table[] = {
                 {VIRTIO_ID_[device], VIRTIO_DEV_ANY_ID}, {0}
           };
           struct virtio_[device]{
                 int size;
                 void* data;
                 void* mapbuf;
                 struct semaphore sem;
                 struct cdev cdev;
                 struct virtio_device *vdev;
                 struct virtqueue *vq[2];
           } my_cdev;

Thursday, January 5, 12                                         50
ecrure
                          riv u t
            De str                     Virtio Driver
         ewefin
        N d

           static struct virtio_driver virtio_[device]_driver = {
                 .feature_table = features,
                 .feature_table_size = ARRAY_SIZE(features),
                 .driver.name = KBUILD_MODNAME,
                 .driver.owner = THIS_MODULE,
                 .id_table = id_table,
                 .probe = my_probe,
                 .remove = __devexit_p(my_remove),
           };




Thursday, January 5, 12                                             51
river
            D                          Virtio Driver
        New



                Register & unregister driver - two APIs
                     register_virtio_driver(&virtio_[device]_driver);
                     unregister_virtio_driver(&virtio_[device]_driver);




Thursday, January 5, 12                                                   52
tu re
                 hit ec
      A rc

                                      Virtio Driver
                                                            Guest
                                  Virtio PCI Controller


                                         Vring            Transport

                                  Virtio PCI Controller
                                                            QEMU
                                     Virtio Device



Thursday, January 5, 12                                               53
ati on
               nviec side
            uri r                         Vring
         omm d
        C
                Declare necessary parameter
           char * buffer1, buffer2;
           int* A[2] = {buffer1, buffer2};
           struct scatterlist sg[2]; /*0 for output, 1 for input*/
           struct virtqueue *vq[2];
           const char *names[2] = { “[Q1]“ , “[Q2]“ };
           vq_callback_t *cb[2] = {NULL, NULL};


                Find virtqueue
           vdev->config->find_vqs(vdev, 2, vq, cb, names);
           my_cdev.vq[0] = vq[0];
           my_cdev.vq[1] = vq[1];



Thursday, January 5, 12                                              54
ati on
               nviec side
            uri r                       Vring
         omm d
        C

                Send data to qemu
           sg_init_table(sg, ARRAY_SIZE(sg));
           sg_set_buf(&sg[0], buffer1, [count]);
           sg_set_buf(&sg[1], buffer2, [count]);
           virtqueue_add_buf(myc->vq[0], sg, /*out*/1, /*in*/1, A);
           virtqueue_kick(myc->vq[0]);


                Get data from qemu
           virtqueue_get_buf(myc->vq[0], &len);




Thursday, January 5, 12                                               55
ati on
               niicc side
            uev e                      Vring
         omm d
        C

                Set virtqueue handler @init function
           s->vq[0] = virtio_add_queue(&s->vdev, 1024,
           virtio_[device]_handle_out1);
           s->vq[1] = virtio_add_queue(&s->vdev, 1024,
           virtio_[device]_handle_out2);


                Declare variable for data @virtqueue handler
           VirtQueueElement elem1, elem2;
           memset(&elem1, 0, sizeof(elem1));
           memset(&elem2, 0, sizeof(elem2));



Thursday, January 5, 12                                        56
ati on
               niicc side
            uev e                     Vring
         omm d
        C

                VirtQueueElement
           typedef struct VirtQueueElement
           {
              unsigned int index;
              unsigned int out_num;
              unsigned int in_num;
              target_phys_addr_t in_addr[VIRTQUEUE_MAX_SIZE];
              target_phys_addr_t out_addr[VIRTQUEUE_MAX_SIZE];
              struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
              struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
           } VirtQueueElement;



Thursday, January 5, 12                                          57
ati on
               niicc side
            uev e                        Vring
         omm d
        C
                Pop
            virtqueue_pop(vq, &elem1);

                Check if data exist
            if(memcmp(&elem1, &elem2, sizeof(elem1)))
              do something;

                Put data to elem
            memcpy(elem1.in_sg[0].iov_base, &data, sizeof(data));

                Push data back to guest
           elem1.in_sg[0].iov_len = sizeof(data);
           virtqueue_push(vq, &elem1, sizeof(data));

Thursday, January 5, 12                                             58
en ce
           R    e fer

                Virtio: An I/O virtualization framework for Linux
                     http://www.ibm.com/developerworks/linux/library/l-virtio/
                     index.html?ca=dgr-lnxw97Viriodth-
                     LX&S_TACT=105AGX59&S_CMP=grlnxw97

                virtio: Towards a De-Facto Standard For Virtual IO Devices
                     http://www.tdeig.ch/kvm/pasche/32_virtio_Russel.pdf

                QEMU & Kernel Source Code
                     http://kernel.org/
                     http://wiki.qemu.org/

Thursday, January 5, 12                                                          59

More Related Content

What's hot

eBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux KerneleBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux KernelThomas Graf
 
I/O仮想化最前線〜ネットワークI/Oを中心に〜
I/O仮想化最前線〜ネットワークI/Oを中心に〜I/O仮想化最前線〜ネットワークI/Oを中心に〜
I/O仮想化最前線〜ネットワークI/Oを中心に〜Ryousei Takano
 
Intel DPDK Step by Step instructions
Intel DPDK Step by Step instructionsIntel DPDK Step by Step instructions
Intel DPDK Step by Step instructionsHisaki Ohara
 
OVS VXLAN Network Accelaration on OpenStack (VXLAN offload and DPDK) - OpenSt...
OVS VXLAN Network Accelaration on OpenStack (VXLAN offload and DPDK) - OpenSt...OVS VXLAN Network Accelaration on OpenStack (VXLAN offload and DPDK) - OpenSt...
OVS VXLAN Network Accelaration on OpenStack (VXLAN offload and DPDK) - OpenSt...VirtualTech Japan Inc.
 
エンジニアなら知っておきたい「仮想マシン」のしくみ v1.1 (hbstudy 17)
エンジニアなら知っておきたい「仮想マシン」のしくみ v1.1 (hbstudy 17)エンジニアなら知っておきたい「仮想マシン」のしくみ v1.1 (hbstudy 17)
エンジニアなら知っておきたい「仮想マシン」のしくみ v1.1 (hbstudy 17)Takeshi HASEGAWA
 
Achieving the ultimate performance with KVM
Achieving the ultimate performance with KVM Achieving the ultimate performance with KVM
Achieving the ultimate performance with KVM ShapeBlue
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking ExplainedThomas Graf
 
Understanding Open vSwitch
Understanding Open vSwitch Understanding Open vSwitch
Understanding Open vSwitch YongKi Kim
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux KernelAdrian Huang
 
Linux KVM のコードを追いかけてみよう
Linux KVM のコードを追いかけてみようLinux KVM のコードを追いかけてみよう
Linux KVM のコードを追いかけてみようTsuyoshi OZAWA
 
Fun with Network Interfaces
Fun with Network InterfacesFun with Network Interfaces
Fun with Network InterfacesKernel TLV
 
Kernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at NetflixKernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at NetflixBrendan Gregg
 
ARM LinuxのMMUはわかりにくい
ARM LinuxのMMUはわかりにくいARM LinuxのMMUはわかりにくい
ARM LinuxのMMUはわかりにくいwata2ki
 
DPDK: Multi Architecture High Performance Packet Processing
DPDK: Multi Architecture High Performance Packet ProcessingDPDK: Multi Architecture High Performance Packet Processing
DPDK: Multi Architecture High Performance Packet ProcessingMichelle Holley
 
twlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsotwlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsoViller Hsiao
 

What's hot (20)

eBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux KerneleBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux Kernel
 
I/O仮想化最前線〜ネットワークI/Oを中心に〜
I/O仮想化最前線〜ネットワークI/Oを中心に〜I/O仮想化最前線〜ネットワークI/Oを中心に〜
I/O仮想化最前線〜ネットワークI/Oを中心に〜
 
Linux : PSCI
Linux : PSCILinux : PSCI
Linux : PSCI
 
Intel DPDK Step by Step instructions
Intel DPDK Step by Step instructionsIntel DPDK Step by Step instructions
Intel DPDK Step by Step instructions
 
OVS VXLAN Network Accelaration on OpenStack (VXLAN offload and DPDK) - OpenSt...
OVS VXLAN Network Accelaration on OpenStack (VXLAN offload and DPDK) - OpenSt...OVS VXLAN Network Accelaration on OpenStack (VXLAN offload and DPDK) - OpenSt...
OVS VXLAN Network Accelaration on OpenStack (VXLAN offload and DPDK) - OpenSt...
 
エンジニアなら知っておきたい「仮想マシン」のしくみ v1.1 (hbstudy 17)
エンジニアなら知っておきたい「仮想マシン」のしくみ v1.1 (hbstudy 17)エンジニアなら知っておきたい「仮想マシン」のしくみ v1.1 (hbstudy 17)
エンジニアなら知っておきたい「仮想マシン」のしくみ v1.1 (hbstudy 17)
 
Achieving the ultimate performance with KVM
Achieving the ultimate performance with KVM Achieving the ultimate performance with KVM
Achieving the ultimate performance with KVM
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking Explained
 
Understanding Open vSwitch
Understanding Open vSwitch Understanding Open vSwitch
Understanding Open vSwitch
 
Embedded Hypervisor for ARM
Embedded Hypervisor for ARMEmbedded Hypervisor for ARM
Embedded Hypervisor for ARM
 
淺談探索 Linux 系統設計之道
淺談探索 Linux 系統設計之道 淺談探索 Linux 系統設計之道
淺談探索 Linux 系統設計之道
 
Hands-on ethernet driver
Hands-on ethernet driverHands-on ethernet driver
Hands-on ethernet driver
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
 
Linux KVM のコードを追いかけてみよう
Linux KVM のコードを追いかけてみようLinux KVM のコードを追いかけてみよう
Linux KVM のコードを追いかけてみよう
 
DPDK In Depth
DPDK In DepthDPDK In Depth
DPDK In Depth
 
Fun with Network Interfaces
Fun with Network InterfacesFun with Network Interfaces
Fun with Network Interfaces
 
Kernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at NetflixKernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at Netflix
 
ARM LinuxのMMUはわかりにくい
ARM LinuxのMMUはわかりにくいARM LinuxのMMUはわかりにくい
ARM LinuxのMMUはわかりにくい
 
DPDK: Multi Architecture High Performance Packet Processing
DPDK: Multi Architecture High Performance Packet ProcessingDPDK: Multi Architecture High Performance Packet Processing
DPDK: Multi Architecture High Performance Packet Processing
 
twlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsotwlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdso
 

Recently uploaded

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

05.2 virtio introduction

  • 1. VIRTIO Yi-Man Ma & Chang-Jung Lin 2011 / 01 / 05 Thursday, January 5, 12 1
  • 2. Agenda Introduction Architecture VirtQueue & Vring Five APIs Adding a new Virtio Device / Driver Thursday, January 5, 12 2
  • 4. ct ion du In tro Create a shortcut to deal with IO Request Coalesced IO Request by VirtQueue Thursday, January 5, 12 4
  • 5. ct ion du In tro Device emulation in full virtualization and paravirtualization environments Guest operating system Guest operating system Para-drivers Traps Interfaces Hypervisor Hypervisor (Full virtualization) Device emulation (Full virtualization) Device emulation Hardware Hardware Thursday, January 5, 12 5
  • 7. tu re hit ec A rc GUEST1 GUEST2 Transport Transport Other QEMU QEMU Processes KVM Host Hardware Thursday, January 5, 12 7
  • 8. tu re hit ec A rc Virtio Driver Guest Virtio PCI Controller Vring Transport Virtio PCI Controller QEMU Virtio Device Thursday, January 5, 12 8
  • 9. VIRTQUEUE & VRING Thursday, January 5, 12 9
  • 10. ue ue Vi rtQ Each Device have it’s own virtqueues. Queue Requests Thursday, January 5, 12 10
  • 11. V ri ng Each virtqueue have it’s own vring. Memory Mapped between QEMU & Guest Actual memory layout for queue Thursday, January 5, 12 11
  • 12. ue ng ue ri Vi rtQ V Find virtqueue Virtio Driver Guest Virtio PCI Controller Alloc Vring Vring Transport Virtio PCI Controller Vring GPA QEMU Virtio Device Thursday, January 5, 12 12
  • 13. ue ng ue ri Vi rtQ V Virtio Driver Virt queue Write Virt queue Read Vring Vring Virtio Device Thursday, January 5, 12 13
  • 14. ue ng ue ri Vi rtQ V Virtio Driver Virt queue Write Virt queue Read Vring Vring Virtio Device Thursday, January 5, 12 14
  • 15. ue ng ue ri Vi rtQ V Virtio Driver Add Buf Virt queue Write Virt queue Read Vring In Out Vring data Virtio Device Thursday, January 5, 12 15
  • 16. ue ng ue ri Vi rtQ V Kick Virtio Driver Virt queue Write Virt queue Read Vring In Out Vring data Virtio Device Thursday, January 5, 12 16
  • 17. ue ng ue ri Vi rtQ V Virtio Driver Virt queue Write Virt queue Read Vring In Out Vring data POP Virtio Device Thursday, January 5, 12 17
  • 18. ue ng ue ri Vi rtQ V Virtio Driver Virt queue Write Virt queue Read Vring In Out Vring data Push Virtio Device Thursday, January 5, 12 18
  • 19. ue ng ue ri Vi rtQ V Virtio Driver Get Buf Virt queue Write Virt queue Read data Vring In Out Vring Virtio Device Thursday, January 5, 12 19
  • 21. APIs F ive add_buf kick get_buf disable_cb enable_cb Thursday, January 5, 12 21
  • 22. Is et buf AP g F ive add & add_buf expose buffer to other end get_buf get the next used buffer Thursday, January 5, 12 22
  • 23. Is AP ick F ive k kick update after add_buf notify QEMU to deal with the data Thursday, January 5, 12 23
  • 24. Is ative AP el F ive C Br disable_cb disable callbacks not necessarily synchronous unreliable and only useful as an optimization enable_cb restart callbacks after disable_cb Thursday, January 5, 12 24
  • 25. NEW DEVICE / DRIVER Thursday, January 5, 12 25
  • 26. river D New include/linux/virtio_ids.h include/linux/virtio.h include/linux/virtio_[device].h drivers/virtio/virtio_[device].c drivers/virtio/virtio_pci.c Thursday, January 5, 12 26
  • 27. ev ice D New hw/virtio-[device].c hw/virtio-[device].h hw/pci.h hw/virtio_pci.c Thursday, January 5, 12 27
  • 28. tu re hit ec A rc Virtio Driver Guest Virtio PCI Controller Vring Transport Virtio PCI Controller QEMU Virtio Device Thursday, January 5, 12 28
  • 29. ev iceeid D devic Virtio PCI Controller New add Modify qemu-src/hw/pci.h #define PCI_DEVICE_ID_VIRTIO_[device] 0x1004 Thursday, January 5, 12 29
  • 30. icde r ev le Dal han Virtio PCI Controller Newiniti Modify qemu-src/hw/virtio-pci.c static int virtio_[device]_init_pci(PCIDevice *pci_dev) { VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); VirtIODevice *vdev; vdev = virtio_[device]_init(&pci_dev->qdev); if (!vdev) { return -1; } virtio_init_pci(proxy, vdev); return 0; } Thursday, January 5, 12 30
  • 31. ev ce idler Dt han Virtio PCI Controller New exi Modify qemu-src/hw/virtio-pci.c static int virtio_[device]_exit_pci (PCIDevice *pci_dev) { VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); virtio_pci_stop_ioeventfd(proxy); virtio_[device]_exit(proxy->vdev); return virtio_exit_pci(pci_dev); } Thursday, January 5, 12 31
  • 32. ieciefo ev n D evic wdd d Virtio PCI Controller Nea Modify qemu-src/hw/virtio-pci.c static PCIDeviceInfo virtio_info[] = { { .qdev.name = "virtio-[device]-pci", .qdev.alias = "virtio-[device]", .init = virtio_[device]_init_pci, exit . = virtio_[device]_exit_pci, device_id = PCI_DEVICE_ID_VIRTIO_[device], . .qdev.props = (Property[]) { DEFINE_VIRTIO_COMMON_FEATURES( VirtIOPCIProxy, host_features), DEFINE_PROP_END_OF_LIST(), , } ...... (Skip) Thursday, January 5, 12 32
  • 33. tu re hit ec A rc Virtio Driver Guest Virtio PCI Controller Vring Transport Virtio PCI Controller QEMU Virtio Device Thursday, January 5, 12 33
  • 34. ev ice D Virtio Device New Add virtio-[device].c, virtio[device].h Put them under the directory qemu-src/hw/ Thursday, January 5, 12 34
  • 35. ivcce].h ev i e D-[de Virtio Device ewirtio N v #ifndef _QEMU_VIRTIO_[device]_H #include "virtio.h“ #define _QEMU_VIRTIO_[device]_H #define VIRTIO_ID_[device] [num] //ID for your device VirtIODevice *virtio_[device]_init(DeviceState *dev); void virtio_[device]_exit(VirtIODevice *vdev); #endif Thursday, January 5, 12 35
  • 36. ivcce].c ev i e D-[de Virtio Device ewirtio N v #include <qemu-common.h> #include "qemu-error.h" #include "virtio-[device].h" #include "virtio.h" typedef struct VirtIO[device] { VirtIODevice vdev; VirtQueue *vq[num]; DeviceState *qdev; int status; } VirtIO[device]; struct virtio_[device]_config { int config; }; Thursday, January 5, 12 36
  • 37. ictere evuc u D str Virtio Device ewirtio N v struct VirtIODevice { ........... uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features); uint32_t (*bad_features)(VirtIODevice *vdev); void (*set_features)(VirtIODevice *vdev, uint32_t val); void (*get_config)(VirtIODevice *vdev, uint8_t *config); void (*set_config)(VirtIODevice *vdev, const uint8_t *config); void (*reset)(VirtIODevice *vdev); void (*set_status)(VirtIODevice *vdev, uint8_t val); ....... }; Thursday, January 5, 12 37
  • 38. iece nt evl me Dimp Virtio Device eweed N n static VirtIO[device] *to_virtio_[device](VirtIODevice *vdev) //Get device defined by yourself static uint32_t virtio_[device]_ get_features (VirtIODevice *vdev, uint32_t features); static uint32_t virtio_[device]_ bad_features (VirtIODevice *vdev); static void virtio_[device]_ set_features (VirtIODevice *vdev, uint32_t val); static void virtio_[device]_ get_config (VirtIODevice *vdev, uint8_t *config); static void virtio_[device]_ set_config (VirtIODevice *vdev, const uint8_t *config); Thursday, January 5, 12 38
  • 39. iece nt evl me Dimp Virtio Device eweed N n static void virtio_[device]_ reset (VirtIODevice *vdev); static void virtio_[device]_ set_status (VirtIODevice *vdev, uint8_t val); VirtIODevice *virtio_[device]_init(DeviceState *dev); //Initialize your device void virtio_[device]_exit (VirtIODevice *vdev); static void virtio_[device]_save(QEMUFile *f, void *opaque); static int virtio_[device]_load(QEMUFile *f, void *opaque, int version_id); static void virtio_[device]_handle_out(VirtIODevice *vdev, VirtQueue *vq); Thursday, January 5, 12 39
  • 40. itcees ev ur Dt fea Virtio Device ew bou N a static uint32_t virtio_[device]_ get_features (VirtIODevice *vdev, uint32_t features) { VirtIO[device] *n = to_virtio_[device](vdev); return features; } static uint32_t virtio_[device]_ bad_features (VirtIODevice *vdev){ uint32_t features = 0; return features; } static void virtio_[device]_ set_features (VirtIODevice *vdev, uint32_t val){ VirtIOMy *n = to_virtio_[device](vdev); } Thursday, January 5, 12 40
  • 41. e icfig Dut c evon Virtio Device Newabo static void virtio_[device]_ get_config (VirtIODevice *vdev, uint8_t *config) { VirtIO[device] *n = to_virtio_[device](vdev); struct virtio_[device]_config cfg; stw_p(&cfg.config, n->status); memcpy(config, &cfg, sizeof(cfg)); } static void virtio_[device]_ set_config (VirtIODevice *vdev, const uint8_t *config) { VirtIO[de vice] *n = to_virtio_[device](vdev); struct virtio_[device]_config cfg; } Thursday, January 5, 12 41
  • 42. icee eves t D& r w set Virtio Device Ne static void virtio_[device]_ reset (VirtIODevice *vdev) { } static void virtio_[device]_ set_status (VirtIODevice *vdev, uint8_t val) { VirtIO[device] *n = to_virtio_my(vdev); } Thursday, January 5, 12 42
  • 43. e icice Dial de ev v Virtio Device New init VirtIODevice *virtio_[device]_init(DeviceState *dev) { VirtIO[device] *s; s = (VirtIO[device]*)virtio_common_init("virtio-[device]", VIRTIO_ID_[device], sizeof(struct virtio_[device]_config), sizeof(VirtIO[device])); s->vdev.get_config = virtio_[device]get_config; s->vdev.set_config = virtio_[device]_set_config; s->vdev.get_features = virtio_[device]_get_features; s->vdev.set_features = virtio_[device]_set_features; s->vdev.bad_features = virtio_[device]_bad_features; s->vdev.reset = virtio_[device]_reset; s->vdev.set_status = virtio_[device]_set_status; ..... Thursday, January 5, 12 43
  • 44. e icice Dial de ev v Virtio Device New init ..... s->vdev.set_status = virtio_[device]_set_status; s->vq[0] = virtio_add_queue(&s->vdev, 1024, virtio_[device]_handle_out1); s->vq[1] = virtio_add_queue(&s->vdev, 1024, virtio_[device]_handle_out2); s->qdev = dev; s->status = 99; register_savevm(dev, "virtio-[device]”, 0, 2, virtio_[device]_save, virtio_[device]_load, s); return &s->vdev; } Thursday, January 5, 12 44
  • 45. ivcce ev i e Dit de Virtio Device New ex void virtio_[device]_exit (VirtIODevice *vdev) { VirtIO[device] *s = to_virtio_[device](vdev); unregister_savevm(s->qdev, "virtio-[device]“ , s); } Thursday, January 5, 12 45
  • 46. e iloae v cd De & Virtio Device New sav static void virtio_[device]_save(QEMUFile *f, void *opaque) { VirtIO[device] *n = opaque; virtio_save(&n->vdev, f); } static int virtio_[device]_load(QEMUFile *f, void *opaque, int version_id) { VirtIO[device] *n = opaque; virtio_load(&n->vdev, f); return 0; } Thursday, January 5, 12 46
  • 47. icler ev d e Da han Virtio Device Newdat static void virtio_[device]_handle_out(VirtIODevice *vdev, VirtQueue *vq) { VirtIO[device] *n = to_virtio_my(vdev); VirtQueueElement elem1, elem2; int K; memset(&elem1, 0, sizeof(elem1)); memset(&elem2, 0, sizeof(elem2)); virtqueue_pop(vq, &elem1); if(memcmp(&elem1, &elem2, sizeof(elem1))) printf("something happened!!!n"); K = *((int*)elem1.out_sg[0].iov_base) + 1; memcpy(elem1.in_sg[0].iov_base, &K, sizeof(int)); virtqueue_push(vq, &elem1, sizeof(int)); } Thursday, January 5, 12 47
  • 48. tu re hit ec A rc Virtio Driver Guest Virtio PCI Controller Vring Transport Virtio PCI Controller QEMU Virtio Device Thursday, January 5, 12 48
  • 49. r eid Devic rive Virtio Driver New d Modify kernel-src/include/linux/virtio-ids.h #define VIRTIO_ID_[device] number Should be the same number as defined in qemu Thursday, January 5, 12 49
  • 50. river D Virtio Driver New #include "linux/virtio.h“ #include "linux/virtio_ids.h“ #include "linux/virtio_pci.h“ #include "virtio.h" static struct virtio_device_id id_table[] = { {VIRTIO_ID_[device], VIRTIO_DEV_ANY_ID}, {0} }; struct virtio_[device]{ int size; void* data; void* mapbuf; struct semaphore sem; struct cdev cdev; struct virtio_device *vdev; struct virtqueue *vq[2]; } my_cdev; Thursday, January 5, 12 50
  • 51. ecrure riv u t De str Virtio Driver ewefin N d static struct virtio_driver virtio_[device]_driver = { .feature_table = features, .feature_table_size = ARRAY_SIZE(features), .driver.name = KBUILD_MODNAME, .driver.owner = THIS_MODULE, .id_table = id_table, .probe = my_probe, .remove = __devexit_p(my_remove), }; Thursday, January 5, 12 51
  • 52. river D Virtio Driver New Register & unregister driver - two APIs register_virtio_driver(&virtio_[device]_driver); unregister_virtio_driver(&virtio_[device]_driver); Thursday, January 5, 12 52
  • 53. tu re hit ec A rc Virtio Driver Guest Virtio PCI Controller Vring Transport Virtio PCI Controller QEMU Virtio Device Thursday, January 5, 12 53
  • 54. ati on nviec side uri r Vring omm d C Declare necessary parameter char * buffer1, buffer2; int* A[2] = {buffer1, buffer2}; struct scatterlist sg[2]; /*0 for output, 1 for input*/ struct virtqueue *vq[2]; const char *names[2] = { “[Q1]“ , “[Q2]“ }; vq_callback_t *cb[2] = {NULL, NULL}; Find virtqueue vdev->config->find_vqs(vdev, 2, vq, cb, names); my_cdev.vq[0] = vq[0]; my_cdev.vq[1] = vq[1]; Thursday, January 5, 12 54
  • 55. ati on nviec side uri r Vring omm d C Send data to qemu sg_init_table(sg, ARRAY_SIZE(sg)); sg_set_buf(&sg[0], buffer1, [count]); sg_set_buf(&sg[1], buffer2, [count]); virtqueue_add_buf(myc->vq[0], sg, /*out*/1, /*in*/1, A); virtqueue_kick(myc->vq[0]); Get data from qemu virtqueue_get_buf(myc->vq[0], &len); Thursday, January 5, 12 55
  • 56. ati on niicc side uev e Vring omm d C Set virtqueue handler @init function s->vq[0] = virtio_add_queue(&s->vdev, 1024, virtio_[device]_handle_out1); s->vq[1] = virtio_add_queue(&s->vdev, 1024, virtio_[device]_handle_out2); Declare variable for data @virtqueue handler VirtQueueElement elem1, elem2; memset(&elem1, 0, sizeof(elem1)); memset(&elem2, 0, sizeof(elem2)); Thursday, January 5, 12 56
  • 57. ati on niicc side uev e Vring omm d C VirtQueueElement typedef struct VirtQueueElement { unsigned int index; unsigned int out_num; unsigned int in_num; target_phys_addr_t in_addr[VIRTQUEUE_MAX_SIZE]; target_phys_addr_t out_addr[VIRTQUEUE_MAX_SIZE]; struct iovec in_sg[VIRTQUEUE_MAX_SIZE]; struct iovec out_sg[VIRTQUEUE_MAX_SIZE]; } VirtQueueElement; Thursday, January 5, 12 57
  • 58. ati on niicc side uev e Vring omm d C Pop virtqueue_pop(vq, &elem1); Check if data exist if(memcmp(&elem1, &elem2, sizeof(elem1))) do something; Put data to elem memcpy(elem1.in_sg[0].iov_base, &data, sizeof(data)); Push data back to guest elem1.in_sg[0].iov_len = sizeof(data); virtqueue_push(vq, &elem1, sizeof(data)); Thursday, January 5, 12 58
  • 59. en ce R e fer Virtio: An I/O virtualization framework for Linux http://www.ibm.com/developerworks/linux/library/l-virtio/ index.html?ca=dgr-lnxw97Viriodth- LX&S_TACT=105AGX59&S_CMP=grlnxw97 virtio: Towards a De-Facto Standard For Virtual IO Devices http://www.tdeig.ch/kvm/pasche/32_virtio_Russel.pdf QEMU & Kernel Source Code http://kernel.org/ http://wiki.qemu.org/ Thursday, January 5, 12 59