1 /*
2 * Copyright (C) 2009 Citrix Ltd.
3 * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; version 2.1 only. with the special
8 * exception on linking described in file LICENSE.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 */
15
16 #ifndef LIBXL_UTILS_H
17 #define LIBXL_UTILS_H
18
19 #include "libxl.h"
20
21 #ifndef LIBXL_HAVE_NONCONST_LIBXL_BASENAME_RETURN_VALUE
22 const
23 #endif
24 char *libxl_basename(const char *name); /* returns string from strdup */
25
26 unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsigned int smp_cpus);
27 /* deprecated; see LIBXL_HAVE_DOMAIN_NEED_MEMORY_CONFIG in libxl.h */
28 int libxl_name_to_domid(libxl_ctx *ctx, const char *name, uint32_t *domid);
29 int libxl_domain_qualifier_to_domid(libxl_ctx *ctx, const char *name, uint32_t *domid);
30 char *libxl_domid_to_name(libxl_ctx *ctx, uint32_t domid);
31 int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,
32 uint32_t *poolid_r,
33 int *was_name_r);
34 int libxl_name_to_cpupoolid(libxl_ctx *ctx, const char *name, uint32_t *poolid);
35 char *libxl_cpupoolid_to_name(libxl_ctx *ctx, uint32_t poolid);
36 int libxl_cpupoolid_is_valid(libxl_ctx *ctx, uint32_t poolid);
37 int libxl_get_stubdom_id(libxl_ctx *ctx, int guest_domid);
38 int libxl_is_stubdom(libxl_ctx *ctx, uint32_t domid, uint32_t *target_domid);
39 int libxl_create_logfile(libxl_ctx *ctx, const char *name, char **full_name);
40 int libxl_string_to_backend(libxl_ctx *ctx, char *s, libxl_disk_backend *backend);
41
42 int libxl_read_file_contents(libxl_ctx *ctx, const char *filename,
43 void **data_r, int *datalen_r);
44 /* Reads the contents of the plain file filename into a mallocd
45 * buffer. Returns 0 or errno. Any errors other than ENOENT are logged.
46 * If the file is empty, *data_r and *datalen_r are set to 0.
47 * On error, *data_r and *datalen_r are unchanged.
48 * data_r and/or datalen_r may be 0.
49 */
50
51 int libxl_read_exactly(libxl_ctx *ctx, int fd, void *data, ssize_t sz,
52 const char *filename, const char *what);
53 int libxl_write_exactly(libxl_ctx *ctx, int fd, const void *data,
54 ssize_t sz, const char *filename, const char *what);
55 /* Returns 0 or errno. If file is truncated on reading, returns
56 * EPROTO and you have no way to tell how much was read. Errors are
57 * logged using filename (which is only used for logging) and what
58 * (which may be 0). */
59
60 int libxl_pipe(libxl_ctx *ctx, int pipes[2]);
61 /* Just like pipe(2), but log errors. */
62
63 void libxl_report_child_exitstatus(libxl_ctx *ctx, xentoollog_level,
64 const char *what, pid_t pid, int status);
65 /* treats all exit statuses as errors; if that's not what you want,
66 * check status yourself first */
67
68 int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
69 const char *mac, libxl_device_nic *nic);
70 int libxl_devid_to_device_nic(libxl_ctx *ctx, uint32_t domid, int devid,
71 libxl_device_nic *nic);
72
73 int libxl_vdev_to_device_disk(libxl_ctx *ctx, uint32_t domid, const char *vdev,
74 libxl_device_disk *disk);
75
76 int libxl_uuid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
77 libxl_uuid *uuid, libxl_device_vtpm *vtpm);
78 int libxl_devid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
79 int devid, libxl_device_vtpm *vtpm);
80 int libxl_devid_to_device_usbctrl(libxl_ctx *ctx, uint32_t domid,
81 int devid, libxl_device_usbctrl *usbctrl);
82
83 int libxl_devid_to_device_vkb(libxl_ctx *ctx, uint32_t domid,
84 int devid, libxl_device_vkb *vkb);
85
86 int libxl_devid_to_device_vdispl(libxl_ctx *ctx, uint32_t domid,
87 int devid, libxl_device_vdispl *vdispl);
88
89 int libxl_devid_to_device_vsnd(libxl_ctx *ctx, uint32_t domid,
90 int devid, libxl_device_vsnd *vsnd);
91
92 int libxl_ctrlport_to_device_usbdev(libxl_ctx *ctx, uint32_t domid,
93 int ctrl, int port,
94 libxl_device_usbdev *usbdev);
95
96 int libxl_bitmap_alloc(libxl_ctx *ctx, libxl_bitmap *bitmap, int n_bits);
97 /* Allocated bimap is from malloc, libxl_bitmap_dispose() to be
98 * called by the application when done. */
99 void libxl_bitmap_copy_alloc(libxl_ctx *ctx, libxl_bitmap *dptr,
100 const libxl_bitmap *sptr);
101 void libxl_bitmap_copy(libxl_ctx *ctx, libxl_bitmap *dptr,
102 const libxl_bitmap *sptr);
103 int libxl_bitmap_is_full(const libxl_bitmap *bitmap);
104 int libxl_bitmap_is_empty(const libxl_bitmap *bitmap);
105 int libxl_bitmap_test(const libxl_bitmap *bitmap, int bit);
106 void libxl_bitmap_set(libxl_bitmap *bitmap, int bit);
107 void libxl_bitmap_reset(libxl_bitmap *bitmap, int bit);
108 int libxl_bitmap_count_set(const libxl_bitmap *bitmap);
109 int libxl_bitmap_or(libxl_ctx *ctx, libxl_bitmap *or_map,
110 const libxl_bitmap *map1,
111 const libxl_bitmap *map2);
112 int libxl_bitmap_and(libxl_ctx *ctx, libxl_bitmap *and_map,
113 const libxl_bitmap *map1,
114 const libxl_bitmap *map2);
115 char *libxl_bitmap_to_hex_string(libxl_ctx *ctx, const libxl_bitmap *bitmap);
libxl_bitmap_set_any(libxl_bitmap * bitmap)116 static inline void libxl_bitmap_set_any(libxl_bitmap *bitmap)
117 {
118 memset(bitmap->map, -1, bitmap->size);
119 }
libxl_bitmap_set_none(libxl_bitmap * bitmap)120 static inline void libxl_bitmap_set_none(libxl_bitmap *bitmap)
121 {
122 memset(bitmap->map, 0, bitmap->size);
123 }
libxl_bitmap_cpu_valid(libxl_bitmap * bitmap,int bit)124 static inline int libxl_bitmap_cpu_valid(libxl_bitmap *bitmap, int bit)
125 {
126 return bit >= 0 && bit < (bitmap->size * 8);
127 }
128 #define libxl_for_each_bit(var, map) for (var = 0; var < (map).size * 8; var++)
129 #define libxl_for_each_set_bit(v, m) for (v = 0; v < (m).size * 8; v++) \
130 if (libxl_bitmap_test(&(m), v))
131
132 /*
133 * Compares two bitmaps bit by bit, up to nr_bits or, if nr_bits is 0, up
134 * to the size of the largest bitmap. If sizes does not match, bits past the
135 * of a bitmap are considered as being 0, which matches with the semantic and
136 * implementation of libxl_bitmap_test I think().
137 *
138 * So, basically, [0,1,0] and [0,1] are considered equal, while [0,1,1] and
139 * [0,1] are different.
140 */
libxl_bitmap_equal(const libxl_bitmap * ba,const libxl_bitmap * bb,int nr_bits)141 static inline int libxl_bitmap_equal(const libxl_bitmap *ba,
142 const libxl_bitmap *bb,
143 int nr_bits)
144 {
145 int i;
146
147 if (nr_bits == 0)
148 nr_bits = ba->size > bb->size ? ba->size * 8 : bb->size * 8;
149
150 for (i = 0; i < nr_bits; i++) {
151 if (libxl_bitmap_test(ba, i) != libxl_bitmap_test(bb, i))
152 return 0;
153 }
154 return 1;
155 }
156
157 int libxl_cpu_bitmap_alloc(libxl_ctx *ctx, libxl_bitmap *cpumap, int max_cpus);
158 int libxl_node_bitmap_alloc(libxl_ctx *ctx, libxl_bitmap *nodemap,
159 int max_nodes);
160 int libxl_socket_bitmap_alloc(libxl_ctx *ctx, libxl_bitmap *socketmap,
161 int max_sockets);
162 /* Fill socketmap with the CPU topology information on the system. */
163 int libxl_get_online_socketmap(libxl_ctx *ctx, libxl_bitmap *socketmap);
164
165 /* Populate cpumap with the cpus spanned by the nodes in nodemap */
166 int libxl_nodemap_to_cpumap(libxl_ctx *ctx,
167 const libxl_bitmap *nodemap,
168 libxl_bitmap *cpumap);
169 /* Populate cpumap with the cpus spanned by node */
170 int libxl_node_to_cpumap(libxl_ctx *ctx, int node,
171 libxl_bitmap *cpumap);
172 /* Populate nodemap with the nodes of the cpus in cpumap */
173 int libxl_cpumap_to_nodemap(libxl_ctx *ctx,
174 const libxl_bitmap *cpumap,
175 libxl_bitmap *nodemap);
176
libxl__sizekb_to_mb(uint32_t s)177 static inline uint32_t libxl__sizekb_to_mb(uint32_t s) {
178 return (s + 1023) / 1024;
179 }
180
181 void libxl_string_copy(libxl_ctx *ctx, char **dst, char * const*src);
182
183
184 #define LIBXL_FILLZERO(object) (memset(&(object), 0, sizeof((object))))
185
186 #endif
187
188 /*
189 * Local variables:
190 * mode: C
191 * c-basic-offset: 4
192 * indent-tabs-mode: nil
193 * End:
194 */
195