root / balloon.c @ 8cfd0495
History | View | Annotate | Download (3.7 kB)
1 | 28c28973 | Paolo Bonzini | /*
|
---|---|---|---|
2 | 73428a8e | Amit Shah | * Generic Balloon handlers and management
|
3 | 28c28973 | Paolo Bonzini | *
|
4 | 28c28973 | Paolo Bonzini | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 73428a8e | Amit Shah | * Copyright (C) 2011 Red Hat, Inc.
|
6 | 73428a8e | Amit Shah | * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
|
7 | 28c28973 | Paolo Bonzini | *
|
8 | 28c28973 | Paolo Bonzini | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 | 28c28973 | Paolo Bonzini | * of this software and associated documentation files (the "Software"), to deal
|
10 | 28c28973 | Paolo Bonzini | * in the Software without restriction, including without limitation the rights
|
11 | 28c28973 | Paolo Bonzini | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 | 28c28973 | Paolo Bonzini | * copies of the Software, and to permit persons to whom the Software is
|
13 | 28c28973 | Paolo Bonzini | * furnished to do so, subject to the following conditions:
|
14 | 28c28973 | Paolo Bonzini | *
|
15 | 28c28973 | Paolo Bonzini | * The above copyright notice and this permission notice shall be included in
|
16 | 28c28973 | Paolo Bonzini | * all copies or substantial portions of the Software.
|
17 | 28c28973 | Paolo Bonzini | *
|
18 | 28c28973 | Paolo Bonzini | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 | 28c28973 | Paolo Bonzini | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 | 28c28973 | Paolo Bonzini | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
21 | 28c28973 | Paolo Bonzini | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 | 28c28973 | Paolo Bonzini | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 | 28c28973 | Paolo Bonzini | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24 | 28c28973 | Paolo Bonzini | * THE SOFTWARE.
|
25 | 28c28973 | Paolo Bonzini | */
|
26 | 28c28973 | Paolo Bonzini | |
27 | 83c9089e | Paolo Bonzini | #include "monitor/monitor.h" |
28 | 022c62cb | Paolo Bonzini | #include "exec/cpu-common.h" |
29 | 9c17d615 | Paolo Bonzini | #include "sysemu/kvm.h" |
30 | 9c17d615 | Paolo Bonzini | #include "sysemu/balloon.h" |
31 | 62dd89de | Prerna Saxena | #include "trace.h" |
32 | 96637bcd | Luiz Capitulino | #include "qmp-commands.h" |
33 | 7b1b5d19 | Paolo Bonzini | #include "qapi/qmp/qjson.h" |
34 | 28c28973 | Paolo Bonzini | |
35 | 0a2a30d5 | Amit Shah | static QEMUBalloonEvent *balloon_event_fn;
|
36 | 30fb2ca6 | Amit Shah | static QEMUBalloonStatus *balloon_stat_fn;
|
37 | 0a2a30d5 | Amit Shah | static void *balloon_opaque; |
38 | 28c28973 | Paolo Bonzini | |
39 | 6c6ec182 | Amit Shah | int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
|
40 | 6c6ec182 | Amit Shah | QEMUBalloonStatus *stat_func, void *opaque)
|
41 | 28c28973 | Paolo Bonzini | { |
42 | 6c6ec182 | Amit Shah | if (balloon_event_fn || balloon_stat_fn || balloon_opaque) {
|
43 | 6c6ec182 | Amit Shah | /* We're already registered one balloon handler. How many can
|
44 | 6c6ec182 | Amit Shah | * a guest really have?
|
45 | 6c6ec182 | Amit Shah | */
|
46 | 6c6ec182 | Amit Shah | error_report("Another balloon device already registered");
|
47 | 6c6ec182 | Amit Shah | return -1; |
48 | 6c6ec182 | Amit Shah | } |
49 | 30fb2ca6 | Amit Shah | balloon_event_fn = event_func; |
50 | 30fb2ca6 | Amit Shah | balloon_stat_fn = stat_func; |
51 | 0a2a30d5 | Amit Shah | balloon_opaque = opaque; |
52 | 6c6ec182 | Amit Shah | return 0; |
53 | 28c28973 | Paolo Bonzini | } |
54 | 28c28973 | Paolo Bonzini | |
55 | 8a7d552c | Amit Shah | void qemu_remove_balloon_handler(void *opaque) |
56 | 8a7d552c | Amit Shah | { |
57 | 8a7d552c | Amit Shah | if (balloon_opaque != opaque) {
|
58 | 8a7d552c | Amit Shah | return;
|
59 | 8a7d552c | Amit Shah | } |
60 | 8a7d552c | Amit Shah | balloon_event_fn = NULL;
|
61 | 8a7d552c | Amit Shah | balloon_stat_fn = NULL;
|
62 | 8a7d552c | Amit Shah | balloon_opaque = NULL;
|
63 | 8a7d552c | Amit Shah | } |
64 | 8a7d552c | Amit Shah | |
65 | 30fb2ca6 | Amit Shah | static int qemu_balloon(ram_addr_t target) |
66 | 28c28973 | Paolo Bonzini | { |
67 | 182b9203 | Amit Shah | if (!balloon_event_fn) {
|
68 | 28c28973 | Paolo Bonzini | return 0; |
69 | 28c28973 | Paolo Bonzini | } |
70 | 182b9203 | Amit Shah | trace_balloon_event(balloon_opaque, target); |
71 | 30fb2ca6 | Amit Shah | balloon_event_fn(balloon_opaque, target); |
72 | 182b9203 | Amit Shah | return 1; |
73 | 28c28973 | Paolo Bonzini | } |
74 | 28c28973 | Paolo Bonzini | |
75 | 96637bcd | Luiz Capitulino | static int qemu_balloon_status(BalloonInfo *info) |
76 | 28c28973 | Paolo Bonzini | { |
77 | 30fb2ca6 | Amit Shah | if (!balloon_stat_fn) {
|
78 | 28c28973 | Paolo Bonzini | return 0; |
79 | 28c28973 | Paolo Bonzini | } |
80 | 96637bcd | Luiz Capitulino | balloon_stat_fn(balloon_opaque, info); |
81 | 182b9203 | Amit Shah | return 1; |
82 | 28c28973 | Paolo Bonzini | } |
83 | 28c28973 | Paolo Bonzini | |
84 | 973603a8 | Daniel P. Berrange | void qemu_balloon_changed(int64_t actual)
|
85 | 973603a8 | Daniel P. Berrange | { |
86 | 973603a8 | Daniel P. Berrange | QObject *data; |
87 | 973603a8 | Daniel P. Berrange | |
88 | 973603a8 | Daniel P. Berrange | data = qobject_from_jsonf("{ 'actual': %" PRId64 " }", |
89 | 973603a8 | Daniel P. Berrange | actual); |
90 | 973603a8 | Daniel P. Berrange | |
91 | 973603a8 | Daniel P. Berrange | monitor_protocol_event(QEVENT_BALLOON_CHANGE, data); |
92 | 973603a8 | Daniel P. Berrange | |
93 | 973603a8 | Daniel P. Berrange | qobject_decref(data); |
94 | 973603a8 | Daniel P. Berrange | } |
95 | 973603a8 | Daniel P. Berrange | |
96 | 973603a8 | Daniel P. Berrange | |
97 | 96637bcd | Luiz Capitulino | BalloonInfo *qmp_query_balloon(Error **errp) |
98 | 28c28973 | Paolo Bonzini | { |
99 | 96637bcd | Luiz Capitulino | BalloonInfo *info; |
100 | 28c28973 | Paolo Bonzini | |
101 | 28c28973 | Paolo Bonzini | if (kvm_enabled() && !kvm_has_sync_mmu()) {
|
102 | 96637bcd | Luiz Capitulino | error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon"); |
103 | 96637bcd | Luiz Capitulino | return NULL; |
104 | 28c28973 | Paolo Bonzini | } |
105 | 28c28973 | Paolo Bonzini | |
106 | 96637bcd | Luiz Capitulino | info = g_malloc0(sizeof(*info));
|
107 | 96637bcd | Luiz Capitulino | |
108 | 96637bcd | Luiz Capitulino | if (qemu_balloon_status(info) == 0) { |
109 | 96637bcd | Luiz Capitulino | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon");
|
110 | 96637bcd | Luiz Capitulino | qapi_free_BalloonInfo(info); |
111 | 96637bcd | Luiz Capitulino | return NULL; |
112 | 28c28973 | Paolo Bonzini | } |
113 | 28c28973 | Paolo Bonzini | |
114 | 96637bcd | Luiz Capitulino | return info;
|
115 | 28c28973 | Paolo Bonzini | } |
116 | 28c28973 | Paolo Bonzini | |
117 | d72f3264 | Luiz Capitulino | void qmp_balloon(int64_t value, Error **errp)
|
118 | 28c28973 | Paolo Bonzini | { |
119 | 28c28973 | Paolo Bonzini | if (kvm_enabled() && !kvm_has_sync_mmu()) {
|
120 | d72f3264 | Luiz Capitulino | error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon"); |
121 | d72f3264 | Luiz Capitulino | return;
|
122 | 28c28973 | Paolo Bonzini | } |
123 | 28c28973 | Paolo Bonzini | |
124 | d72f3264 | Luiz Capitulino | if (value <= 0) { |
125 | b3c83a22 | Luiz Capitulino | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "target", "a size"); |
126 | d72f3264 | Luiz Capitulino | return;
|
127 | 514e73ec | Amit Shah | } |
128 | d72f3264 | Luiz Capitulino | |
129 | d72f3264 | Luiz Capitulino | if (qemu_balloon(value) == 0) { |
130 | d72f3264 | Luiz Capitulino | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon");
|
131 | 28c28973 | Paolo Bonzini | } |
132 | 28c28973 | Paolo Bonzini | } |