root / balloon.c @ 8294a64d
History | View | Annotate | Download (3.4 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 | 28c28973 | Paolo Bonzini | #include "monitor.h" |
28 | 28c28973 | Paolo Bonzini | #include "cpu-common.h" |
29 | 28c28973 | Paolo Bonzini | #include "kvm.h" |
30 | 28c28973 | Paolo Bonzini | #include "balloon.h" |
31 | 62dd89de | Prerna Saxena | #include "trace.h" |
32 | 96637bcd | Luiz Capitulino | #include "qmp-commands.h" |
33 | 28c28973 | Paolo Bonzini | |
34 | 0a2a30d5 | Amit Shah | static QEMUBalloonEvent *balloon_event_fn;
|
35 | 30fb2ca6 | Amit Shah | static QEMUBalloonStatus *balloon_stat_fn;
|
36 | 0a2a30d5 | Amit Shah | static void *balloon_opaque; |
37 | 28c28973 | Paolo Bonzini | |
38 | 6c6ec182 | Amit Shah | int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
|
39 | 6c6ec182 | Amit Shah | QEMUBalloonStatus *stat_func, void *opaque)
|
40 | 28c28973 | Paolo Bonzini | { |
41 | 6c6ec182 | Amit Shah | if (balloon_event_fn || balloon_stat_fn || balloon_opaque) {
|
42 | 6c6ec182 | Amit Shah | /* We're already registered one balloon handler. How many can
|
43 | 6c6ec182 | Amit Shah | * a guest really have?
|
44 | 6c6ec182 | Amit Shah | */
|
45 | 6c6ec182 | Amit Shah | error_report("Another balloon device already registered");
|
46 | 6c6ec182 | Amit Shah | return -1; |
47 | 6c6ec182 | Amit Shah | } |
48 | 30fb2ca6 | Amit Shah | balloon_event_fn = event_func; |
49 | 30fb2ca6 | Amit Shah | balloon_stat_fn = stat_func; |
50 | 0a2a30d5 | Amit Shah | balloon_opaque = opaque; |
51 | 6c6ec182 | Amit Shah | return 0; |
52 | 28c28973 | Paolo Bonzini | } |
53 | 28c28973 | Paolo Bonzini | |
54 | 8a7d552c | Amit Shah | void qemu_remove_balloon_handler(void *opaque) |
55 | 8a7d552c | Amit Shah | { |
56 | 8a7d552c | Amit Shah | if (balloon_opaque != opaque) {
|
57 | 8a7d552c | Amit Shah | return;
|
58 | 8a7d552c | Amit Shah | } |
59 | 8a7d552c | Amit Shah | balloon_event_fn = NULL;
|
60 | 8a7d552c | Amit Shah | balloon_stat_fn = NULL;
|
61 | 8a7d552c | Amit Shah | balloon_opaque = NULL;
|
62 | 8a7d552c | Amit Shah | } |
63 | 8a7d552c | Amit Shah | |
64 | 30fb2ca6 | Amit Shah | static int qemu_balloon(ram_addr_t target) |
65 | 28c28973 | Paolo Bonzini | { |
66 | 182b9203 | Amit Shah | if (!balloon_event_fn) {
|
67 | 28c28973 | Paolo Bonzini | return 0; |
68 | 28c28973 | Paolo Bonzini | } |
69 | 182b9203 | Amit Shah | trace_balloon_event(balloon_opaque, target); |
70 | 30fb2ca6 | Amit Shah | balloon_event_fn(balloon_opaque, target); |
71 | 182b9203 | Amit Shah | return 1; |
72 | 28c28973 | Paolo Bonzini | } |
73 | 28c28973 | Paolo Bonzini | |
74 | 96637bcd | Luiz Capitulino | static int qemu_balloon_status(BalloonInfo *info) |
75 | 28c28973 | Paolo Bonzini | { |
76 | 30fb2ca6 | Amit Shah | if (!balloon_stat_fn) {
|
77 | 28c28973 | Paolo Bonzini | return 0; |
78 | 28c28973 | Paolo Bonzini | } |
79 | 96637bcd | Luiz Capitulino | balloon_stat_fn(balloon_opaque, info); |
80 | 182b9203 | Amit Shah | return 1; |
81 | 28c28973 | Paolo Bonzini | } |
82 | 28c28973 | Paolo Bonzini | |
83 | 96637bcd | Luiz Capitulino | BalloonInfo *qmp_query_balloon(Error **errp) |
84 | 28c28973 | Paolo Bonzini | { |
85 | 96637bcd | Luiz Capitulino | BalloonInfo *info; |
86 | 28c28973 | Paolo Bonzini | |
87 | 28c28973 | Paolo Bonzini | if (kvm_enabled() && !kvm_has_sync_mmu()) {
|
88 | 96637bcd | Luiz Capitulino | error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon"); |
89 | 96637bcd | Luiz Capitulino | return NULL; |
90 | 28c28973 | Paolo Bonzini | } |
91 | 28c28973 | Paolo Bonzini | |
92 | 96637bcd | Luiz Capitulino | info = g_malloc0(sizeof(*info));
|
93 | 96637bcd | Luiz Capitulino | |
94 | 96637bcd | Luiz Capitulino | if (qemu_balloon_status(info) == 0) { |
95 | 96637bcd | Luiz Capitulino | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon");
|
96 | 96637bcd | Luiz Capitulino | qapi_free_BalloonInfo(info); |
97 | 96637bcd | Luiz Capitulino | return NULL; |
98 | 28c28973 | Paolo Bonzini | } |
99 | 28c28973 | Paolo Bonzini | |
100 | 96637bcd | Luiz Capitulino | return info;
|
101 | 28c28973 | Paolo Bonzini | } |
102 | 28c28973 | Paolo Bonzini | |
103 | d72f3264 | Luiz Capitulino | void qmp_balloon(int64_t value, Error **errp)
|
104 | 28c28973 | Paolo Bonzini | { |
105 | 28c28973 | Paolo Bonzini | if (kvm_enabled() && !kvm_has_sync_mmu()) {
|
106 | d72f3264 | Luiz Capitulino | error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon"); |
107 | d72f3264 | Luiz Capitulino | return;
|
108 | 28c28973 | Paolo Bonzini | } |
109 | 28c28973 | Paolo Bonzini | |
110 | d72f3264 | Luiz Capitulino | if (value <= 0) { |
111 | b3c83a22 | Luiz Capitulino | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "target", "a size"); |
112 | d72f3264 | Luiz Capitulino | return;
|
113 | 514e73ec | Amit Shah | } |
114 | d72f3264 | Luiz Capitulino | |
115 | d72f3264 | Luiz Capitulino | if (qemu_balloon(value) == 0) { |
116 | d72f3264 | Luiz Capitulino | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon");
|
117 | 28c28973 | Paolo Bonzini | } |
118 | 28c28973 | Paolo Bonzini | } |