1 /*
2  * Copyright 2009-2017 Citrix Ltd and other contributors
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; version 2.1 only. with the special
7  * exception on linking described in file LICENSE.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  */
14 
15 #include <stdlib.h>
16 #include <unistd.h>
17 
18 #include <libxl.h>
19 
20 #include "xl.h"
21 #include "xl_utils.h"
22 
main_tmem_list(int argc,char ** argv)23 int main_tmem_list(int argc, char **argv)
24 {
25     uint32_t domid;
26     const char *dom = NULL;
27     char *buf = NULL;
28     int use_long = 0;
29     int all = 0;
30     int opt;
31 
32     SWITCH_FOREACH_OPT(opt, "al", NULL, "tmem-list", 0) {
33     case 'l':
34         use_long = 1;
35         break;
36     case 'a':
37         all = 1;
38         break;
39     }
40 
41     dom = argv[optind];
42     if (!dom && all == 0) {
43         fprintf(stderr, "You must specify -a or a domain id.\n\n");
44         help("tmem-list");
45         return 1;
46     }
47 
48     if (all)
49         domid = INVALID_DOMID;
50     else
51         domid = find_domain(dom);
52 
53     buf = libxl_tmem_list(ctx, domid, use_long);
54     if (buf == NULL)
55         return EXIT_FAILURE;
56 
57     printf("%s\n", buf);
58     free(buf);
59     return EXIT_SUCCESS;
60 }
61 
main_tmem_freeze(int argc,char ** argv)62 int main_tmem_freeze(int argc, char **argv)
63 {
64     uint32_t domid;
65     const char *dom = NULL;
66     int all = 0;
67     int opt;
68 
69     SWITCH_FOREACH_OPT(opt, "a", NULL, "tmem-freeze", 0) {
70     case 'a':
71         all = 1;
72         break;
73     }
74 
75     dom = argv[optind];
76     if (!dom && all == 0) {
77         fprintf(stderr, "You must specify -a or a domain id.\n\n");
78         help("tmem-freeze");
79         return EXIT_FAILURE;
80     }
81 
82     if (all)
83         domid = INVALID_DOMID;
84     else
85         domid = find_domain(dom);
86 
87     if (libxl_tmem_freeze(ctx, domid) < 0)
88         return EXIT_FAILURE;
89 
90     return EXIT_SUCCESS;
91 }
92 
main_tmem_thaw(int argc,char ** argv)93 int main_tmem_thaw(int argc, char **argv)
94 {
95     uint32_t domid;
96     const char *dom = NULL;
97     int all = 0;
98     int opt;
99 
100     SWITCH_FOREACH_OPT(opt, "a", NULL, "tmem-thaw", 0) {
101     case 'a':
102         all = 1;
103         break;
104     }
105 
106     dom = argv[optind];
107     if (!dom && all == 0) {
108         fprintf(stderr, "You must specify -a or a domain id.\n\n");
109         help("tmem-thaw");
110         return EXIT_FAILURE;
111     }
112 
113     if (all)
114         domid = INVALID_DOMID;
115     else
116         domid = find_domain(dom);
117 
118     if (libxl_tmem_thaw(ctx, domid) < 0)
119         return EXIT_FAILURE;
120 
121     return EXIT_SUCCESS;
122 }
123 
main_tmem_set(int argc,char ** argv)124 int main_tmem_set(int argc, char **argv)
125 {
126     uint32_t domid;
127     const char *dom = NULL;
128     uint32_t weight = 0, cap = 0, compress = 0;
129     int opt_w = 0, opt_c = 0, opt_p = 0;
130     int all = 0;
131     int opt;
132     int rc = 0;
133 
134     SWITCH_FOREACH_OPT(opt, "aw:c:p:", NULL, "tmem-set", 0) {
135     case 'a':
136         all = 1;
137         break;
138     case 'w':
139         weight = strtol(optarg, NULL, 10);
140         opt_w = 1;
141         break;
142     case 'c':
143         cap = strtol(optarg, NULL, 10);
144         opt_c = 1;
145         break;
146     case 'p':
147         compress = strtol(optarg, NULL, 10);
148         opt_p = 1;
149         break;
150     }
151 
152     dom = argv[optind];
153     if (!dom && all == 0) {
154         fprintf(stderr, "You must specify -a or a domain id.\n\n");
155         help("tmem-set");
156         return EXIT_FAILURE;
157     }
158 
159     if (all)
160         domid = INVALID_DOMID;
161     else
162         domid = find_domain(dom);
163 
164     if (!opt_w && !opt_c && !opt_p) {
165         fprintf(stderr, "No set value specified.\n\n");
166         help("tmem-set");
167         return EXIT_FAILURE;
168     }
169 
170     if (opt_w)
171         rc = libxl_tmem_set(ctx, domid, "weight", weight);
172     if (opt_c)
173         rc = libxl_tmem_set(ctx, domid, "cap", cap);
174     if (opt_p)
175         rc = libxl_tmem_set(ctx, domid, "compress", compress);
176 
177     if (rc < 0)
178         return EXIT_FAILURE;
179 
180     return EXIT_SUCCESS;
181 }
182 
main_tmem_shared_auth(int argc,char ** argv)183 int main_tmem_shared_auth(int argc, char **argv)
184 {
185     uint32_t domid;
186     const char *autharg = NULL;
187     char *endptr = NULL;
188     const char *dom = NULL;
189     char *uuid = NULL;
190     int auth = -1;
191     int all = 0;
192     int opt;
193 
194     SWITCH_FOREACH_OPT(opt, "au:A:", NULL, "tmem-shared-auth", 0) {
195     case 'a':
196         all = 1;
197         break;
198     case 'u':
199         uuid = optarg;
200         break;
201     case 'A':
202         autharg = optarg;
203         break;
204     }
205 
206     dom = argv[optind];
207     if (!dom && all == 0) {
208         fprintf(stderr, "You must specify -a or a domain id.\n\n");
209         help("tmem-shared-auth");
210         return EXIT_FAILURE;
211     }
212 
213     if (all)
214         domid = INVALID_DOMID;
215     else
216         domid = find_domain(dom);
217 
218     if (uuid == NULL || autharg == NULL) {
219         fprintf(stderr, "No uuid or auth specified.\n\n");
220         help("tmem-shared-auth");
221         return EXIT_FAILURE;
222     }
223 
224     auth = strtol(autharg, &endptr, 10);
225     if (*endptr != '\0') {
226         fprintf(stderr, "Invalid auth, valid auth are <0|1>.\n\n");
227         return EXIT_FAILURE;
228     }
229 
230     if (libxl_tmem_shared_auth(ctx, domid, uuid, auth) < 0)
231         return EXIT_FAILURE;
232 
233     return EXIT_SUCCESS;
234 }
235 
main_tmem_freeable(int argc,char ** argv)236 int main_tmem_freeable(int argc, char **argv)
237 {
238     int opt;
239     int mb;
240 
241     SWITCH_FOREACH_OPT(opt, "", NULL, "tmem-freeable", 0) {
242         /* No options */
243     }
244 
245     mb = libxl_tmem_freeable(ctx);
246     if (mb == -1)
247         return EXIT_FAILURE;
248 
249     printf("%d\n", mb);
250     return EXIT_SUCCESS;
251 }
252