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 "libxl_osdeps.h"
16 
17 #include "libxl_internal.h"
18 
libxl_tmem_list(libxl_ctx * ctx,uint32_t domid,int use_long)19 char *libxl_tmem_list(libxl_ctx *ctx, uint32_t domid, int use_long)
20 {
21     int r;
22     char _buf[32768];
23     GC_INIT(ctx);
24 
25     r = xc_tmem_control(ctx->xch, -1, XEN_SYSCTL_TMEM_OP_LIST, domid, 32768,
26                         use_long, _buf);
27     if (r < 0) {
28         LOGED(ERROR, domid, "Can not get tmem list");
29         GC_FREE;
30         return NULL;
31     }
32 
33     GC_FREE;
34     return strdup(_buf);
35 }
36 
libxl_tmem_freeze(libxl_ctx * ctx,uint32_t domid)37 int libxl_tmem_freeze(libxl_ctx *ctx, uint32_t domid)
38 {
39     int r, rc;
40     GC_INIT(ctx);
41 
42     r = xc_tmem_control(ctx->xch, -1, XEN_SYSCTL_TMEM_OP_FREEZE, domid, 0, 0,
43                         NULL);
44     if (r < 0) {
45         LOGED(ERROR, domid, "Can not freeze tmem pools");
46         rc = ERROR_FAIL;
47         goto out;
48     }
49 
50     rc = 0;
51 out:
52     GC_FREE;
53     return rc;
54 }
55 
libxl_tmem_thaw(libxl_ctx * ctx,uint32_t domid)56 int libxl_tmem_thaw(libxl_ctx *ctx, uint32_t domid)
57 {
58     int r, rc;
59     GC_INIT(ctx);
60 
61     r = xc_tmem_control(ctx->xch, -1, XEN_SYSCTL_TMEM_OP_THAW, domid, 0, 0,
62                         NULL);
63     if (r < 0) {
64         LOGED(ERROR, domid, "Can not thaw tmem pools");
65         rc = ERROR_FAIL;
66         goto out;
67     }
68 
69     rc = 0;
70 out:
71     GC_FREE;
72     return rc;
73 }
74 
tmem_setop_from_string(char * set_name,uint32_t val,xen_tmem_client_t * info)75 static int32_t tmem_setop_from_string(char *set_name, uint32_t val,
76                                       xen_tmem_client_t *info)
77 {
78     if (!strcmp(set_name, "weight"))
79         info->weight = val;
80     else if (!strcmp(set_name, "compress"))
81         info->flags.u.compress = val;
82     else
83         return -1;
84 
85     return 0;
86 }
87 
libxl_tmem_set(libxl_ctx * ctx,uint32_t domid,char * name,uint32_t set)88 int libxl_tmem_set(libxl_ctx *ctx, uint32_t domid, char* name, uint32_t set)
89 {
90     int r, rc;
91     xen_tmem_client_t info;
92     GC_INIT(ctx);
93 
94     r = xc_tmem_control(ctx->xch, -1 /* pool_id */,
95                         XEN_SYSCTL_TMEM_OP_GET_CLIENT_INFO,
96                         domid, sizeof(info), 0 /* arg */, &info);
97     if (r < 0) {
98         LOGED(ERROR, domid, "Can not get tmem data!");
99         rc = ERROR_FAIL;
100         goto out;
101     }
102     rc = tmem_setop_from_string(name, set, &info);
103     if (rc == -1) {
104         LOGEVD(ERROR, -1, domid, "Invalid set, valid sets are <weight|compress>");
105         rc = ERROR_INVAL;
106         goto out;
107     }
108     r = xc_tmem_control(ctx->xch, -1 /* pool_id */,
109                         XEN_SYSCTL_TMEM_OP_SET_CLIENT_INFO,
110                         domid, sizeof(info), 0 /* arg */, &info);
111     if (r < 0) {
112         LOGED(ERROR, domid, "Can not set tmem %s", name);
113         rc = ERROR_FAIL;
114         goto out;
115     }
116 
117     rc = 0;
118 out:
119     GC_FREE;
120     return rc;
121 }
122 
libxl_tmem_shared_auth(libxl_ctx * ctx,uint32_t domid,char * uuid,int auth)123 int libxl_tmem_shared_auth(libxl_ctx *ctx, uint32_t domid,
124                            char* uuid, int auth)
125 {
126     int r, rc;
127     GC_INIT(ctx);
128 
129     r = xc_tmem_auth(ctx->xch, domid, uuid, auth);
130     if (r < 0) {
131         LOGED(ERROR, domid, "Can not set tmem shared auth");
132         rc = ERROR_FAIL;
133         goto out;
134     }
135 
136     rc = 0;
137 out:
138     GC_FREE;
139     return rc;
140 }
141 
libxl_tmem_freeable(libxl_ctx * ctx)142 int libxl_tmem_freeable(libxl_ctx *ctx)
143 {
144     int r, rc;
145     GC_INIT(ctx);
146 
147     r = xc_tmem_control(ctx->xch, -1, XEN_SYSCTL_TMEM_OP_QUERY_FREEABLE_MB,
148                         -1, 0, 0, 0);
149     if (r < 0) {
150         LOGE(ERROR, "Can not get tmem freeable memory");
151         rc = ERROR_FAIL;
152         goto out;
153     }
154 
155     rc = 0;
156 out:
157     GC_FREE;
158     return rc;
159 }
160 
161 /*
162  * Local variables:
163  * mode: C
164  * c-basic-offset: 4
165  * indent-tabs-mode: nil
166  * End:
167  */
168