aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 1 | /* |
Amit Shah | d4443cb | 2011-07-20 13:37:01 +0530 | [diff] [blame] | 2 | * Virtio Balloon Device |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright IBM, Corp. 2008 |
Amit Shah | d4443cb | 2011-07-20 13:37:01 +0530 | [diff] [blame] | 5 | * Copyright (C) 2011 Red Hat, Inc. |
| 6 | * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com> |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 7 | * |
| 8 | * Authors: |
| 9 | * Anthony Liguori <aliguori@us.ibm.com> |
| 10 | * |
| 11 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 12 | * the COPYING file in the top-level directory. |
| 13 | * |
| 14 | */ |
| 15 | |
Peter Maydell | 9b8bfe2 | 2016-01-26 18:17:07 +0000 | [diff] [blame] | 16 | #include "qemu/osdep.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 17 | #include "qemu/iov.h" |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 18 | #include "qemu/timer.h" |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 19 | #include "qemu-common.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 20 | #include "hw/virtio/virtio.h" |
Philippe Mathieu-Daudé | 2070aae | 2017-10-17 13:44:13 -0300 | [diff] [blame] | 21 | #include "hw/mem/pc-dimm.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 22 | #include "sysemu/balloon.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 23 | #include "hw/virtio/virtio-balloon.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 24 | #include "sysemu/kvm.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 25 | #include "exec/address-spaces.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Markus Armbruster | 112ed24 | 2018-02-26 17:13:27 -0600 | [diff] [blame] | 27 | #include "qapi/qapi-events-misc.h" |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 28 | #include "qapi/visitor.h" |
zhanghailiang | 6adfdc5 | 2014-11-17 13:11:10 +0800 | [diff] [blame] | 29 | #include "trace.h" |
Alistair Francis | 2ab4b13 | 2017-09-11 12:52:50 -0700 | [diff] [blame] | 30 | #include "qemu/error-report.h" |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 31 | |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 32 | #include "hw/virtio/virtio-bus.h" |
Rusty Russell | 8609d2a | 2014-06-24 19:43:22 +0200 | [diff] [blame] | 33 | #include "hw/virtio/virtio-access.h" |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 34 | |
Thomas Huth | 01310e2 | 2016-04-14 10:50:07 +0200 | [diff] [blame] | 35 | #define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT) |
| 36 | |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 37 | static void balloon_page(void *addr, int deflate) |
| 38 | { |
Dr. David Alan Gilbert | 371ff5a | 2015-11-05 18:11:23 +0000 | [diff] [blame] | 39 | if (!qemu_balloon_is_inhibited() && (!kvm_enabled() || |
| 40 | kvm_has_sync_mmu())) { |
Thomas Huth | 01310e2 | 2016-04-14 10:50:07 +0200 | [diff] [blame] | 41 | qemu_madvise(addr, BALLOON_PAGE_SIZE, |
Andreas Färber | e78815a | 2010-09-25 11:26:05 +0000 | [diff] [blame] | 42 | deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED); |
Dr. David Alan Gilbert | 371ff5a | 2015-11-05 18:11:23 +0000 | [diff] [blame] | 43 | } |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 46 | static const char *balloon_stat_names[] = { |
| 47 | [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in", |
| 48 | [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out", |
| 49 | [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults", |
| 50 | [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults", |
| 51 | [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory", |
| 52 | [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory", |
Denis V. Lunev | a0d0648 | 2016-02-24 10:50:48 +0300 | [diff] [blame] | 53 | [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory", |
Tomáš Golembiovský | bf1e714 | 2017-12-05 13:14:46 +0100 | [diff] [blame] | 54 | [VIRTIO_BALLOON_S_CACHES] = "stat-disk-caches", |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 55 | [VIRTIO_BALLOON_S_NR] = NULL |
| 56 | }; |
| 57 | |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 58 | /* |
| 59 | * reset_stats - Mark all items in the stats array as unset |
| 60 | * |
Stefan Weil | 52f3502 | 2013-07-24 19:48:56 +0200 | [diff] [blame] | 61 | * This function needs to be called at device initialization and before |
| 62 | * updating to a set of newly-generated stats. This will ensure that no |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 63 | * stale values stick around in case the guest reports a subset of the supported |
| 64 | * statistics. |
| 65 | */ |
| 66 | static inline void reset_stats(VirtIOBalloon *dev) |
| 67 | { |
| 68 | int i; |
| 69 | for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1); |
| 70 | } |
| 71 | |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 72 | static bool balloon_stats_supported(const VirtIOBalloon *s) |
| 73 | { |
KONRAD Frederic | c96cace | 2013-03-27 10:49:14 +0100 | [diff] [blame] | 74 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
Cornelia Huck | 95129d6 | 2015-08-17 11:48:29 +0200 | [diff] [blame] | 75 | return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ); |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static bool balloon_stats_enabled(const VirtIOBalloon *s) |
| 79 | { |
| 80 | return s->stats_poll_interval > 0; |
| 81 | } |
| 82 | |
| 83 | static void balloon_stats_destroy_timer(VirtIOBalloon *s) |
| 84 | { |
| 85 | if (balloon_stats_enabled(s)) { |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 86 | timer_del(s->stats_timer); |
| 87 | timer_free(s->stats_timer); |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 88 | s->stats_timer = NULL; |
| 89 | s->stats_poll_interval = 0; |
| 90 | } |
| 91 | } |
| 92 | |
Luiz Capitulino | 1f9296b | 2014-09-15 12:00:11 -0400 | [diff] [blame] | 93 | static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs) |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 94 | { |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 95 | timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000); |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void balloon_stats_poll_cb(void *opaque) |
| 99 | { |
| 100 | VirtIOBalloon *s = opaque; |
KONRAD Frederic | c96cace | 2013-03-27 10:49:14 +0100 | [diff] [blame] | 101 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 102 | |
Ladi Prosek | 4eae2a6 | 2016-03-01 12:14:03 +0100 | [diff] [blame] | 103 | if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) { |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 104 | /* re-schedule */ |
| 105 | balloon_stats_change_timer(s, s->stats_poll_interval); |
| 106 | return; |
| 107 | } |
| 108 | |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 109 | virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset); |
KONRAD Frederic | c96cace | 2013-03-27 10:49:14 +0100 | [diff] [blame] | 110 | virtio_notify(vdev, s->svq); |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 111 | g_free(s->stats_vq_elem); |
| 112 | s->stats_vq_elem = NULL; |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 113 | } |
| 114 | |
Eric Blake | d7bce99 | 2016-01-29 06:48:55 -0700 | [diff] [blame] | 115 | static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name, |
| 116 | void *opaque, Error **errp) |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 117 | { |
Markus Armbruster | 2ddb16a | 2014-05-07 09:53:52 +0200 | [diff] [blame] | 118 | Error *err = NULL; |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 119 | VirtIOBalloon *s = opaque; |
| 120 | int i; |
| 121 | |
Eric Blake | 337283d | 2016-01-29 06:48:57 -0700 | [diff] [blame] | 122 | visit_start_struct(v, name, NULL, 0, &err); |
Markus Armbruster | 2ddb16a | 2014-05-07 09:53:52 +0200 | [diff] [blame] | 123 | if (err) { |
| 124 | goto out; |
| 125 | } |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 126 | visit_type_int(v, "last-update", &s->stats_last_update, &err); |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 127 | if (err) { |
| 128 | goto out_end; |
| 129 | } |
Markus Armbruster | 2ddb16a | 2014-05-07 09:53:52 +0200 | [diff] [blame] | 130 | |
Eric Blake | 337283d | 2016-01-29 06:48:57 -0700 | [diff] [blame] | 131 | visit_start_struct(v, "stats", NULL, 0, &err); |
Markus Armbruster | 2ddb16a | 2014-05-07 09:53:52 +0200 | [diff] [blame] | 132 | if (err) { |
| 133 | goto out_end; |
| 134 | } |
Eric Blake | 9dbb8fa | 2016-01-29 06:48:45 -0700 | [diff] [blame] | 135 | for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) { |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 136 | visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err); |
Eric Blake | 9dbb8fa | 2016-01-29 06:48:45 -0700 | [diff] [blame] | 137 | if (err) { |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 138 | goto out_nested; |
Eric Blake | 9dbb8fa | 2016-01-29 06:48:45 -0700 | [diff] [blame] | 139 | } |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 140 | } |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 141 | visit_check_struct(v, &err); |
| 142 | out_nested: |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 143 | visit_end_struct(v, NULL); |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 144 | |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 145 | if (!err) { |
| 146 | visit_check_struct(v, &err); |
| 147 | } |
Markus Armbruster | 2ddb16a | 2014-05-07 09:53:52 +0200 | [diff] [blame] | 148 | out_end: |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 149 | visit_end_struct(v, NULL); |
Markus Armbruster | 2ddb16a | 2014-05-07 09:53:52 +0200 | [diff] [blame] | 150 | out: |
| 151 | error_propagate(errp, err); |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 152 | } |
| 153 | |
Eric Blake | 4fa4549 | 2016-01-29 06:48:53 -0700 | [diff] [blame] | 154 | static void balloon_stats_get_poll_interval(Object *obj, Visitor *v, |
Eric Blake | d7bce99 | 2016-01-29 06:48:55 -0700 | [diff] [blame] | 155 | const char *name, void *opaque, |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 156 | Error **errp) |
| 157 | { |
| 158 | VirtIOBalloon *s = opaque; |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 159 | visit_type_int(v, name, &s->stats_poll_interval, errp); |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 160 | } |
| 161 | |
Eric Blake | 4fa4549 | 2016-01-29 06:48:53 -0700 | [diff] [blame] | 162 | static void balloon_stats_set_poll_interval(Object *obj, Visitor *v, |
Eric Blake | d7bce99 | 2016-01-29 06:48:55 -0700 | [diff] [blame] | 163 | const char *name, void *opaque, |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 164 | Error **errp) |
| 165 | { |
| 166 | VirtIOBalloon *s = opaque; |
Markus Armbruster | 65cd906 | 2014-04-25 12:44:22 +0200 | [diff] [blame] | 167 | Error *local_err = NULL; |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 168 | int64_t value; |
| 169 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 170 | visit_type_int(v, name, &value, &local_err); |
Markus Armbruster | 65cd906 | 2014-04-25 12:44:22 +0200 | [diff] [blame] | 171 | if (local_err) { |
| 172 | error_propagate(errp, local_err); |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
| 176 | if (value < 0) { |
| 177 | error_setg(errp, "timer value must be greater than zero"); |
| 178 | return; |
| 179 | } |
| 180 | |
Markus Armbruster | 22644cd | 2014-10-01 18:43:44 +0200 | [diff] [blame] | 181 | if (value > UINT32_MAX) { |
Luiz Capitulino | 1f9296b | 2014-09-15 12:00:11 -0400 | [diff] [blame] | 182 | error_setg(errp, "timer value is too big"); |
| 183 | return; |
| 184 | } |
| 185 | |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 186 | if (value == s->stats_poll_interval) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (value == 0) { |
| 191 | /* timer=0 disables the timer */ |
| 192 | balloon_stats_destroy_timer(s); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if (balloon_stats_enabled(s)) { |
| 197 | /* timer interval change */ |
| 198 | s->stats_poll_interval = value; |
| 199 | balloon_stats_change_timer(s, value); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | /* create a new timer */ |
| 204 | g_assert(s->stats_timer == NULL); |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 205 | s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s); |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 206 | s->stats_poll_interval = value; |
| 207 | balloon_stats_change_timer(s, 0); |
| 208 | } |
| 209 | |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 210 | static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
| 211 | { |
KONRAD Frederic | c96cace | 2013-03-27 10:49:14 +0100 | [diff] [blame] | 212 | VirtIOBalloon *s = VIRTIO_BALLOON(vdev); |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 213 | VirtQueueElement *elem; |
Avi Kivity | b7c28c7 | 2011-12-19 13:18:13 +0200 | [diff] [blame] | 214 | MemoryRegionSection section; |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 215 | |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 216 | for (;;) { |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 217 | size_t offset = 0; |
| 218 | uint32_t pfn; |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 219 | elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); |
| 220 | if (!elem) { |
| 221 | return; |
| 222 | } |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 223 | |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 224 | while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 225 | ram_addr_t pa; |
| 226 | ram_addr_t addr; |
Rusty Russell | 8609d2a | 2014-06-24 19:43:22 +0200 | [diff] [blame] | 227 | int p = virtio_ldl_p(vdev, &pfn); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 228 | |
Rusty Russell | 8609d2a | 2014-06-24 19:43:22 +0200 | [diff] [blame] | 229 | pa = (ram_addr_t) p << VIRTIO_BALLOON_PFN_SHIFT; |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 230 | offset += 4; |
| 231 | |
Avi Kivity | b7c28c7 | 2011-12-19 13:18:13 +0200 | [diff] [blame] | 232 | /* FIXME: remove get_system_memory(), but how? */ |
| 233 | section = memory_region_find(get_system_memory(), pa, 1); |
Dr. David Alan Gilbert | f2fd57d | 2016-12-16 11:41:55 +0000 | [diff] [blame] | 234 | if (!int128_nz(section.size) || |
| 235 | !memory_region_is_ram(section.mr) || |
| 236 | memory_region_is_rom(section.mr) || |
| 237 | memory_region_is_romd(section.mr)) { |
| 238 | trace_virtio_balloon_bad_addr(pa); |
Tiwei Bie | b86107a | 2018-01-25 15:12:43 +0800 | [diff] [blame] | 239 | memory_region_unref(section.mr); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 240 | continue; |
Dr. David Alan Gilbert | f2fd57d | 2016-12-16 11:41:55 +0000 | [diff] [blame] | 241 | } |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 242 | |
zhanghailiang | 6adfdc5 | 2014-11-17 13:11:10 +0800 | [diff] [blame] | 243 | trace_virtio_balloon_handle_output(memory_region_name(section.mr), |
| 244 | pa); |
Avi Kivity | b7c28c7 | 2011-12-19 13:18:13 +0200 | [diff] [blame] | 245 | /* Using memory_region_get_ram_ptr is bending the rules a bit, but |
pbrook | 5c130f6 | 2009-04-10 14:29:45 +0000 | [diff] [blame] | 246 | should be OK because we only want a single page. */ |
Avi Kivity | b7c28c7 | 2011-12-19 13:18:13 +0200 | [diff] [blame] | 247 | addr = section.offset_within_region; |
| 248 | balloon_page(memory_region_get_ram_ptr(section.mr) + addr, |
| 249 | !!(vq == s->dvq)); |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 250 | memory_region_unref(section.mr); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 253 | virtqueue_push(vq, elem, offset); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 254 | virtio_notify(vdev, vq); |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 255 | g_free(elem); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 259 | static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) |
| 260 | { |
KONRAD Frederic | c96cace | 2013-03-27 10:49:14 +0100 | [diff] [blame] | 261 | VirtIOBalloon *s = VIRTIO_BALLOON(vdev); |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 262 | VirtQueueElement *elem; |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 263 | VirtIOBalloonStat stat; |
| 264 | size_t offset = 0; |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 265 | qemu_timeval tv; |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 266 | |
Ladi Prosek | 4eae2a6 | 2016-03-01 12:14:03 +0100 | [diff] [blame] | 267 | elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 268 | if (!elem) { |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 269 | goto out; |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 270 | } |
| 271 | |
Ladi Prosek | 4eae2a6 | 2016-03-01 12:14:03 +0100 | [diff] [blame] | 272 | if (s->stats_vq_elem != NULL) { |
| 273 | /* This should never happen if the driver follows the spec. */ |
| 274 | virtqueue_push(vq, s->stats_vq_elem, 0); |
| 275 | virtio_notify(vdev, vq); |
| 276 | g_free(s->stats_vq_elem); |
| 277 | } |
| 278 | |
| 279 | s->stats_vq_elem = elem; |
| 280 | |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 281 | /* Initialize the stats to get rid of any stale values. This is only |
| 282 | * needed to handle the case where a guest supports fewer stats than it |
| 283 | * used to (ie. it has booted into an old kernel). |
| 284 | */ |
| 285 | reset_stats(s); |
| 286 | |
Michael Tokarev | dcf6f5e | 2012-03-11 18:05:12 +0400 | [diff] [blame] | 287 | while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat)) |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 288 | == sizeof(stat)) { |
Rusty Russell | 8609d2a | 2014-06-24 19:43:22 +0200 | [diff] [blame] | 289 | uint16_t tag = virtio_tswap16(vdev, stat.tag); |
| 290 | uint64_t val = virtio_tswap64(vdev, stat.val); |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 291 | |
| 292 | offset += sizeof(stat); |
| 293 | if (tag < VIRTIO_BALLOON_S_NR) |
| 294 | s->stats[tag] = val; |
| 295 | } |
| 296 | s->stats_vq_offset = offset; |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 297 | |
| 298 | if (qemu_gettimeofday(&tv) < 0) { |
Alistair Francis | 2ab4b13 | 2017-09-11 12:52:50 -0700 | [diff] [blame] | 299 | warn_report("%s: failed to get time of day", __func__); |
Luiz Capitulino | 7e6ccd9 | 2012-12-01 00:14:57 -0200 | [diff] [blame] | 300 | goto out; |
| 301 | } |
| 302 | |
| 303 | s->stats_last_update = tv.tv_sec; |
| 304 | |
| 305 | out: |
| 306 | if (balloon_stats_enabled(s)) { |
| 307 | balloon_stats_change_timer(s, s->stats_poll_interval); |
| 308 | } |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 309 | } |
| 310 | |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 311 | static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data) |
| 312 | { |
KONRAD Frederic | c96cace | 2013-03-27 10:49:14 +0100 | [diff] [blame] | 313 | VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 314 | struct virtio_balloon_config config; |
| 315 | |
| 316 | config.num_pages = cpu_to_le32(dev->num_pages); |
| 317 | config.actual = cpu_to_le32(dev->actual); |
| 318 | |
zhanghailiang | 6adfdc5 | 2014-11-17 13:11:10 +0800 | [diff] [blame] | 319 | trace_virtio_balloon_get_config(config.num_pages, config.actual); |
Luiz Capitulino | e6baf61 | 2014-01-09 09:58:16 -0500 | [diff] [blame] | 320 | memcpy(config_data, &config, sizeof(struct virtio_balloon_config)); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Vladimir Sementsov-Ogievskiy | 2b75f84 | 2016-02-10 11:49:26 +0300 | [diff] [blame] | 323 | static int build_dimm_list(Object *obj, void *opaque) |
| 324 | { |
| 325 | GSList **list = opaque; |
| 326 | |
| 327 | if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { |
| 328 | DeviceState *dev = DEVICE(obj); |
| 329 | if (dev->realized) { /* only realized DIMMs matter */ |
| 330 | *list = g_slist_prepend(*list, dev); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | object_child_foreach(obj, build_dimm_list, opaque); |
| 335 | return 0; |
| 336 | } |
| 337 | |
Vladimir Sementsov-Ogievskiy | 39de998 | 2016-02-10 11:49:22 +0300 | [diff] [blame] | 338 | static ram_addr_t get_current_ram_size(void) |
| 339 | { |
Vladimir Sementsov-Ogievskiy | e8dc06d | 2016-02-10 11:49:25 +0300 | [diff] [blame] | 340 | GSList *list = NULL, *item; |
Vladimir Sementsov-Ogievskiy | 39de998 | 2016-02-10 11:49:22 +0300 | [diff] [blame] | 341 | ram_addr_t size = ram_size; |
| 342 | |
Vladimir Sementsov-Ogievskiy | 2b75f84 | 2016-02-10 11:49:26 +0300 | [diff] [blame] | 343 | build_dimm_list(qdev_get_machine(), &list); |
Vladimir Sementsov-Ogievskiy | e8dc06d | 2016-02-10 11:49:25 +0300 | [diff] [blame] | 344 | for (item = list; item; item = g_slist_next(item)) { |
| 345 | Object *obj = OBJECT(item->data); |
Vladimir Sementsov-Ogievskiy | 2b75f84 | 2016-02-10 11:49:26 +0300 | [diff] [blame] | 346 | if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) { |
| 347 | size += object_property_get_int(obj, PC_DIMM_SIZE_PROP, |
| 348 | &error_abort); |
| 349 | } |
Vladimir Sementsov-Ogievskiy | 39de998 | 2016-02-10 11:49:22 +0300 | [diff] [blame] | 350 | } |
Vladimir Sementsov-Ogievskiy | e8dc06d | 2016-02-10 11:49:25 +0300 | [diff] [blame] | 351 | g_slist_free(list); |
Vladimir Sementsov-Ogievskiy | 39de998 | 2016-02-10 11:49:22 +0300 | [diff] [blame] | 352 | |
| 353 | return size; |
| 354 | } |
| 355 | |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 356 | static void virtio_balloon_set_config(VirtIODevice *vdev, |
| 357 | const uint8_t *config_data) |
| 358 | { |
KONRAD Frederic | c96cace | 2013-03-27 10:49:14 +0100 | [diff] [blame] | 359 | VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 360 | struct virtio_balloon_config config; |
Daniel P. Berrange | 973603a | 2012-06-14 18:12:56 +0100 | [diff] [blame] | 361 | uint32_t oldactual = dev->actual; |
zhanghailiang | 463756d | 2014-11-17 13:11:09 +0800 | [diff] [blame] | 362 | ram_addr_t vm_ram_size = get_current_ram_size(); |
| 363 | |
Luiz Capitulino | e6baf61 | 2014-01-09 09:58:16 -0500 | [diff] [blame] | 364 | memcpy(&config, config_data, sizeof(struct virtio_balloon_config)); |
Alexey Kardashevskiy | e54f177 | 2011-04-07 13:02:04 +1000 | [diff] [blame] | 365 | dev->actual = le32_to_cpu(config.actual); |
Daniel P. Berrange | 973603a | 2012-06-14 18:12:56 +0100 | [diff] [blame] | 366 | if (dev->actual != oldactual) { |
zhanghailiang | 463756d | 2014-11-17 13:11:09 +0800 | [diff] [blame] | 367 | qapi_event_send_balloon_change(vm_ram_size - |
Wenchao Xia | aef9d31 | 2014-06-18 08:43:51 +0200 | [diff] [blame] | 368 | ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT), |
| 369 | &error_abort); |
Daniel P. Berrange | 973603a | 2012-06-14 18:12:56 +0100 | [diff] [blame] | 370 | } |
zhanghailiang | 6adfdc5 | 2014-11-17 13:11:10 +0800 | [diff] [blame] | 371 | trace_virtio_balloon_set_config(dev->actual, oldactual); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Jason Wang | 9d5b731 | 2015-07-27 17:49:19 +0800 | [diff] [blame] | 374 | static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f, |
| 375 | Error **errp) |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 376 | { |
Denis V. Lunev | e381625 | 2015-06-15 13:52:52 +0300 | [diff] [blame] | 377 | VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); |
| 378 | f |= dev->host_features; |
Cornelia Huck | 40de55a | 2015-06-04 12:34:32 +0200 | [diff] [blame] | 379 | virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ); |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 380 | return f; |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Luiz Capitulino | 96637bc | 2011-10-21 11:41:37 -0200 | [diff] [blame] | 383 | static void virtio_balloon_stat(void *opaque, BalloonInfo *info) |
Amit Shah | dce911c | 2011-07-20 13:19:07 +0530 | [diff] [blame] | 384 | { |
| 385 | VirtIOBalloon *dev = opaque; |
zhanghailiang | 463756d | 2014-11-17 13:11:09 +0800 | [diff] [blame] | 386 | info->actual = get_current_ram_size() - ((uint64_t) dev->actual << |
| 387 | VIRTIO_BALLOON_PFN_SHIFT); |
Amit Shah | dce911c | 2011-07-20 13:19:07 +0530 | [diff] [blame] | 388 | } |
| 389 | |
Amit Shah | 30fb2ca | 2011-07-20 13:30:56 +0530 | [diff] [blame] | 390 | static void virtio_balloon_to_target(void *opaque, ram_addr_t target) |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 391 | { |
KONRAD Frederic | c96cace | 2013-03-27 10:49:14 +0100 | [diff] [blame] | 392 | VirtIOBalloon *dev = VIRTIO_BALLOON(opaque); |
| 393 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
zhanghailiang | 463756d | 2014-11-17 13:11:09 +0800 | [diff] [blame] | 394 | ram_addr_t vm_ram_size = get_current_ram_size(); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 395 | |
zhanghailiang | 463756d | 2014-11-17 13:11:09 +0800 | [diff] [blame] | 396 | if (target > vm_ram_size) { |
| 397 | target = vm_ram_size; |
Amit Shah | dce911c | 2011-07-20 13:19:07 +0530 | [diff] [blame] | 398 | } |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 399 | if (target) { |
zhanghailiang | 463756d | 2014-11-17 13:11:09 +0800 | [diff] [blame] | 400 | dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT; |
KONRAD Frederic | c96cace | 2013-03-27 10:49:14 +0100 | [diff] [blame] | 401 | virtio_notify_config(vdev); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 402 | } |
zhanghailiang | 6adfdc5 | 2014-11-17 13:11:10 +0800 | [diff] [blame] | 403 | trace_virtio_balloon_to_target(target, dev->num_pages); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Dr. David Alan Gilbert | 019518a | 2016-10-27 18:36:37 +0100 | [diff] [blame] | 406 | static int virtio_balloon_post_load_device(void *opaque, int version_id) |
Greg Kurz | 9ea2511 | 2014-06-24 19:20:08 +0200 | [diff] [blame] | 407 | { |
Dr. David Alan Gilbert | 019518a | 2016-10-27 18:36:37 +0100 | [diff] [blame] | 408 | VirtIOBalloon *s = VIRTIO_BALLOON(opaque); |
Pavel Butsykin | fecb48f | 2016-03-29 17:00:49 +0300 | [diff] [blame] | 409 | |
| 410 | if (balloon_stats_enabled(s)) { |
| 411 | balloon_stats_change_timer(s, s->stats_poll_interval); |
| 412 | } |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Dr. David Alan Gilbert | 019518a | 2016-10-27 18:36:37 +0100 | [diff] [blame] | 416 | static const VMStateDescription vmstate_virtio_balloon_device = { |
| 417 | .name = "virtio-balloon-device", |
| 418 | .version_id = 1, |
| 419 | .minimum_version_id = 1, |
| 420 | .post_load = virtio_balloon_post_load_device, |
| 421 | .fields = (VMStateField[]) { |
| 422 | VMSTATE_UINT32(num_pages, VirtIOBalloon), |
| 423 | VMSTATE_UINT32(actual, VirtIOBalloon), |
| 424 | VMSTATE_END_OF_LIST() |
| 425 | }, |
| 426 | }; |
| 427 | |
Andreas Färber | 74def47 | 2013-07-30 02:51:37 +0200 | [diff] [blame] | 428 | static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 429 | { |
Andreas Färber | 74def47 | 2013-07-30 02:51:37 +0200 | [diff] [blame] | 430 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
Andreas Färber | a546fb1 | 2013-07-30 05:33:58 +0200 | [diff] [blame] | 431 | VirtIOBalloon *s = VIRTIO_BALLOON(dev); |
Amit Shah | f76f665 | 2011-07-27 12:29:33 +0530 | [diff] [blame] | 432 | int ret; |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 433 | |
Luiz Capitulino | e6baf61 | 2014-01-09 09:58:16 -0500 | [diff] [blame] | 434 | virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON, |
| 435 | sizeof(struct virtio_balloon_config)); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 436 | |
Amit Shah | f76f665 | 2011-07-27 12:29:33 +0530 | [diff] [blame] | 437 | ret = qemu_add_balloon_handler(virtio_balloon_to_target, |
| 438 | virtio_balloon_stat, s); |
KONRAD Frederic | 5c7d096 | 2013-03-27 10:49:13 +0100 | [diff] [blame] | 439 | |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 440 | if (ret < 0) { |
Luiz Capitulino | 46abb81 | 2015-03-31 13:00:26 -0400 | [diff] [blame] | 441 | error_setg(errp, "Only one balloon device is supported"); |
Andreas Färber | a546fb1 | 2013-07-30 05:33:58 +0200 | [diff] [blame] | 442 | virtio_cleanup(vdev); |
Andreas Färber | 74def47 | 2013-07-30 02:51:37 +0200 | [diff] [blame] | 443 | return; |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 444 | } |
Amit Shah | f76f665 | 2011-07-27 12:29:33 +0530 | [diff] [blame] | 445 | |
KONRAD Frederic | 5c7d096 | 2013-03-27 10:49:13 +0100 | [diff] [blame] | 446 | s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); |
| 447 | s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); |
| 448 | s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats); |
aliguori | bd32208 | 2008-12-04 20:33:06 +0000 | [diff] [blame] | 449 | |
Ján Tomko | 38dbd48 | 2014-05-21 11:03:47 +0200 | [diff] [blame] | 450 | reset_stats(s); |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Andreas Färber | 306ec6c | 2013-07-30 03:50:44 +0200 | [diff] [blame] | 453 | static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp) |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 454 | { |
Andreas Färber | 306ec6c | 2013-07-30 03:50:44 +0200 | [diff] [blame] | 455 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 456 | VirtIOBalloon *s = VIRTIO_BALLOON(dev); |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 457 | |
| 458 | balloon_stats_destroy_timer(s); |
| 459 | qemu_remove_balloon_handler(s); |
KONRAD Frederic | 6a1a8cc | 2013-04-24 10:21:22 +0200 | [diff] [blame] | 460 | virtio_cleanup(vdev); |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Ladi Prosek | 4eae2a6 | 2016-03-01 12:14:03 +0100 | [diff] [blame] | 463 | static void virtio_balloon_device_reset(VirtIODevice *vdev) |
| 464 | { |
| 465 | VirtIOBalloon *s = VIRTIO_BALLOON(vdev); |
| 466 | |
| 467 | if (s->stats_vq_elem != NULL) { |
Ladi Prosek | 27e57ef | 2016-11-03 09:55:49 +0100 | [diff] [blame] | 468 | virtqueue_unpop(s->svq, s->stats_vq_elem, 0); |
Ladi Prosek | 4eae2a6 | 2016-03-01 12:14:03 +0100 | [diff] [blame] | 469 | g_free(s->stats_vq_elem); |
| 470 | s->stats_vq_elem = NULL; |
| 471 | } |
| 472 | } |
| 473 | |
Ladi Prosek | 4a1e48b | 2016-09-07 17:20:49 +0200 | [diff] [blame] | 474 | static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status) |
| 475 | { |
| 476 | VirtIOBalloon *s = VIRTIO_BALLOON(vdev); |
| 477 | |
| 478 | if (!s->stats_vq_elem && vdev->vm_running && |
| 479 | (status & VIRTIO_CONFIG_S_DRIVER_OK) && virtqueue_rewind(s->svq, 1)) { |
| 480 | /* poll stats queue for the element we have discarded when the VM |
| 481 | * was stopped */ |
| 482 | virtio_balloon_receive_stats(vdev, s->svq); |
| 483 | } |
| 484 | } |
| 485 | |
Shannon Zhao | 1190044 | 2015-05-11 17:34:05 +0800 | [diff] [blame] | 486 | static void virtio_balloon_instance_init(Object *obj) |
| 487 | { |
| 488 | VirtIOBalloon *s = VIRTIO_BALLOON(obj); |
| 489 | |
| 490 | object_property_add(obj, "guest-stats", "guest statistics", |
| 491 | balloon_stats_get_all, NULL, NULL, s, NULL); |
| 492 | |
| 493 | object_property_add(obj, "guest-stats-polling-interval", "int", |
| 494 | balloon_stats_get_poll_interval, |
| 495 | balloon_stats_set_poll_interval, |
| 496 | NULL, s, NULL); |
| 497 | } |
| 498 | |
Halil Pasic | c5dc16b | 2016-10-06 14:55:47 +0200 | [diff] [blame] | 499 | static const VMStateDescription vmstate_virtio_balloon = { |
| 500 | .name = "virtio-balloon", |
| 501 | .minimum_version_id = 1, |
| 502 | .version_id = 1, |
| 503 | .fields = (VMStateField[]) { |
| 504 | VMSTATE_VIRTIO_DEVICE, |
| 505 | VMSTATE_END_OF_LIST() |
| 506 | }, |
| 507 | }; |
Dr. David Alan Gilbert | 7f1ca9b | 2016-07-14 18:22:49 +0100 | [diff] [blame] | 508 | |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 509 | static Property virtio_balloon_properties[] = { |
Denis V. Lunev | e381625 | 2015-06-15 13:52:52 +0300 | [diff] [blame] | 510 | DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features, |
| 511 | VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false), |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 512 | DEFINE_PROP_END_OF_LIST(), |
| 513 | }; |
| 514 | |
| 515 | static void virtio_balloon_class_init(ObjectClass *klass, void *data) |
| 516 | { |
| 517 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 518 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); |
Andreas Färber | 74def47 | 2013-07-30 02:51:37 +0200 | [diff] [blame] | 519 | |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 520 | dc->props = virtio_balloon_properties; |
Dr. David Alan Gilbert | 7f1ca9b | 2016-07-14 18:22:49 +0100 | [diff] [blame] | 521 | dc->vmsd = &vmstate_virtio_balloon; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 522 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
Andreas Färber | 74def47 | 2013-07-30 02:51:37 +0200 | [diff] [blame] | 523 | vdc->realize = virtio_balloon_device_realize; |
Andreas Färber | 306ec6c | 2013-07-30 03:50:44 +0200 | [diff] [blame] | 524 | vdc->unrealize = virtio_balloon_device_unrealize; |
Ladi Prosek | 4eae2a6 | 2016-03-01 12:14:03 +0100 | [diff] [blame] | 525 | vdc->reset = virtio_balloon_device_reset; |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 526 | vdc->get_config = virtio_balloon_get_config; |
| 527 | vdc->set_config = virtio_balloon_set_config; |
| 528 | vdc->get_features = virtio_balloon_get_features; |
Ladi Prosek | 4a1e48b | 2016-09-07 17:20:49 +0200 | [diff] [blame] | 529 | vdc->set_status = virtio_balloon_set_status; |
Dr. David Alan Gilbert | 019518a | 2016-10-27 18:36:37 +0100 | [diff] [blame] | 530 | vdc->vmsd = &vmstate_virtio_balloon_device; |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static const TypeInfo virtio_balloon_info = { |
| 534 | .name = TYPE_VIRTIO_BALLOON, |
| 535 | .parent = TYPE_VIRTIO_DEVICE, |
| 536 | .instance_size = sizeof(VirtIOBalloon), |
Shannon Zhao | 1190044 | 2015-05-11 17:34:05 +0800 | [diff] [blame] | 537 | .instance_init = virtio_balloon_instance_init, |
KONRAD Frederic | 1ab461b | 2013-03-27 10:49:10 +0100 | [diff] [blame] | 538 | .class_init = virtio_balloon_class_init, |
| 539 | }; |
| 540 | |
| 541 | static void virtio_register_types(void) |
| 542 | { |
| 543 | type_register_static(&virtio_balloon_info); |
| 544 | } |
| 545 | |
| 546 | type_init(virtio_register_types) |