blob: e1cd5fac4c292ae1bf504926ee77ddc7dffde810 [file] [log] [blame]
Paolo Bonzini28c28972010-04-01 19:57:12 +02001/*
Amit Shah73428a82011-07-20 13:35:30 +05302 * Generic Balloon handlers and management
Paolo Bonzini28c28972010-04-01 19:57:12 +02003 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
Amit Shah73428a82011-07-20 13:35:30 +05305 * Copyright (C) 2011 Red Hat, Inc.
6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
Paolo Bonzini28c28972010-04-01 19:57:12 +02007 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paolo Bonzini28c28972010-04-01 19:57:12 +020027#include "monitor.h"
Paolo Bonzini28c28972010-04-01 19:57:12 +020028#include "cpu-common.h"
29#include "kvm.h"
30#include "balloon.h"
Prerna Saxena62dd89d2010-08-11 17:16:03 +053031#include "trace.h"
Luiz Capitulino96637bc2011-10-21 11:41:37 -020032#include "qmp-commands.h"
Paolo Bonzini28c28972010-04-01 19:57:12 +020033
Amit Shah0a2a30d2011-07-20 13:08:46 +053034static QEMUBalloonEvent *balloon_event_fn;
Amit Shah30fb2ca2011-07-20 13:30:56 +053035static QEMUBalloonStatus *balloon_stat_fn;
Amit Shah0a2a30d2011-07-20 13:08:46 +053036static void *balloon_opaque;
Paolo Bonzini28c28972010-04-01 19:57:12 +020037
Amit Shah6c6ec182011-07-27 12:28:19 +053038int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
39 QEMUBalloonStatus *stat_func, void *opaque)
Paolo Bonzini28c28972010-04-01 19:57:12 +020040{
Amit Shah6c6ec182011-07-27 12:28:19 +053041 if (balloon_event_fn || balloon_stat_fn || balloon_opaque) {
42 /* We're already registered one balloon handler. How many can
43 * a guest really have?
44 */
45 error_report("Another balloon device already registered");
46 return -1;
47 }
Amit Shah30fb2ca2011-07-20 13:30:56 +053048 balloon_event_fn = event_func;
49 balloon_stat_fn = stat_func;
Amit Shah0a2a30d2011-07-20 13:08:46 +053050 balloon_opaque = opaque;
Amit Shah6c6ec182011-07-27 12:28:19 +053051 return 0;
Paolo Bonzini28c28972010-04-01 19:57:12 +020052}
53
Amit Shah8a7d5522011-09-09 14:30:39 +053054void qemu_remove_balloon_handler(void *opaque)
55{
56 if (balloon_opaque != opaque) {
57 return;
58 }
59 balloon_event_fn = NULL;
60 balloon_stat_fn = NULL;
61 balloon_opaque = NULL;
62}
63
Amit Shah30fb2ca2011-07-20 13:30:56 +053064static int qemu_balloon(ram_addr_t target)
Paolo Bonzini28c28972010-04-01 19:57:12 +020065{
Amit Shah182b9202011-07-20 13:14:12 +053066 if (!balloon_event_fn) {
Paolo Bonzini28c28972010-04-01 19:57:12 +020067 return 0;
68 }
Amit Shah182b9202011-07-20 13:14:12 +053069 trace_balloon_event(balloon_opaque, target);
Amit Shah30fb2ca2011-07-20 13:30:56 +053070 balloon_event_fn(balloon_opaque, target);
Amit Shah182b9202011-07-20 13:14:12 +053071 return 1;
Paolo Bonzini28c28972010-04-01 19:57:12 +020072}
73
Luiz Capitulino96637bc2011-10-21 11:41:37 -020074static int qemu_balloon_status(BalloonInfo *info)
Paolo Bonzini28c28972010-04-01 19:57:12 +020075{
Amit Shah30fb2ca2011-07-20 13:30:56 +053076 if (!balloon_stat_fn) {
Paolo Bonzini28c28972010-04-01 19:57:12 +020077 return 0;
78 }
Luiz Capitulino96637bc2011-10-21 11:41:37 -020079 balloon_stat_fn(balloon_opaque, info);
Amit Shah182b9202011-07-20 13:14:12 +053080 return 1;
Paolo Bonzini28c28972010-04-01 19:57:12 +020081}
82
Luiz Capitulino96637bc2011-10-21 11:41:37 -020083BalloonInfo *qmp_query_balloon(Error **errp)
Paolo Bonzini28c28972010-04-01 19:57:12 +020084{
Luiz Capitulino96637bc2011-10-21 11:41:37 -020085 BalloonInfo *info;
Paolo Bonzini28c28972010-04-01 19:57:12 +020086
87 if (kvm_enabled() && !kvm_has_sync_mmu()) {
Luiz Capitulino96637bc2011-10-21 11:41:37 -020088 error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
89 return NULL;
Paolo Bonzini28c28972010-04-01 19:57:12 +020090 }
91
Luiz Capitulino96637bc2011-10-21 11:41:37 -020092 info = g_malloc0(sizeof(*info));
93
94 if (qemu_balloon_status(info) == 0) {
95 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon");
96 qapi_free_BalloonInfo(info);
97 return NULL;
Paolo Bonzini28c28972010-04-01 19:57:12 +020098 }
99
Luiz Capitulino96637bc2011-10-21 11:41:37 -0200100 return info;
Paolo Bonzini28c28972010-04-01 19:57:12 +0200101}
102
103/**
104 * do_balloon(): Request VM to change its memory allocation
105 */
106int do_balloon(Monitor *mon, const QDict *params,
107 MonitorCompletion cb, void *opaque)
108{
Amit Shah514e73e2011-07-27 16:50:54 +0530109 int64_t target;
Paolo Bonzini28c28972010-04-01 19:57:12 +0200110 int ret;
111
112 if (kvm_enabled() && !kvm_has_sync_mmu()) {
113 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
114 return -1;
115 }
116
Amit Shah514e73e2011-07-27 16:50:54 +0530117 target = qdict_get_int(params, "value");
118 if (target <= 0) {
119 qerror_report(QERR_INVALID_PARAMETER_VALUE, "target", "a size");
120 return -1;
121 }
122 ret = qemu_balloon(target);
Paolo Bonzini28c28972010-04-01 19:57:12 +0200123 if (ret == 0) {
124 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
125 return -1;
126 }
127
128 cb(opaque, NULL);
129 return 0;
130}