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 int libxl_name_to_domid(libxl_ctx *ctx, const char *name, uint32_t *domid);
28 int libxl_domain_qualifier_to_domid(libxl_ctx *ctx, const char *name, uint32_t *domid);
29 char *libxl_domid_to_name(libxl_ctx *ctx, uint32_t domid);
30 int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,
31                                          uint32_t *poolid_r,
32                                          int *was_name_r);
33 int libxl_name_to_cpupoolid(libxl_ctx *ctx, const char *name, uint32_t *poolid);
34 char *libxl_cpupoolid_to_name(libxl_ctx *ctx, uint32_t poolid);
35 int libxl_cpupoolid_is_valid(libxl_ctx *ctx, uint32_t poolid);
36 int libxl_get_stubdom_id(libxl_ctx *ctx, int guest_domid);
37 int libxl_is_stubdom(libxl_ctx *ctx, uint32_t domid, uint32_t *target_domid);
38 int libxl_create_logfile(libxl_ctx *ctx, const char *name, char **full_name);
39 int libxl_string_to_backend(libxl_ctx *ctx, char *s, libxl_disk_backend *backend);
40 
41 int libxl_read_file_contents(libxl_ctx *ctx, const char *filename,
42                              void **data_r, int *datalen_r);
43   /* Reads the contents of the plain file filename into a mallocd
44    * buffer.  Returns 0 or errno.  Any errors other than ENOENT are logged.
45    * If the file is empty, *data_r and *datalen_r are set to 0.
46    * On error, *data_r and *datalen_r are unchanged.
47    * data_r and/or datalen_r may be 0.
48    */
49 
50 int libxl_read_exactly(libxl_ctx *ctx, int fd, void *data, ssize_t sz,
51                        const char *filename, const char *what);
52 int libxl_write_exactly(libxl_ctx *ctx, int fd, const void *data,
53                         ssize_t sz, const char *filename, const char *what);
54   /* Returns 0 or errno.  If file is truncated on reading, returns
55    * EPROTO and you have no way to tell how much was read.  Errors are
56    * logged using filename (which is only used for logging) and what
57    * (which may be 0). */
58 
59 int libxl_pipe(libxl_ctx *ctx, int pipes[2]);
60   /* Just like pipe(2), but log errors. */
61 
62 void libxl_report_child_exitstatus(libxl_ctx *ctx, xentoollog_level,
63                                    const char *what, pid_t pid, int status);
64     /* treats all exit statuses as errors; if that's not what you want,
65      * check status yourself first */
66 
67 int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
68                             const char *mac, libxl_device_nic *nic);
69 int libxl_devid_to_device_nic(libxl_ctx *ctx, uint32_t domid, int devid,
70                               libxl_device_nic *nic);
71 
72 int libxl_vdev_to_device_disk(libxl_ctx *ctx, uint32_t domid, const char *vdev,
73                                libxl_device_disk *disk);
74 
75 int libxl_uuid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
76                                libxl_uuid *uuid, libxl_device_vtpm *vtpm);
77 int libxl_devid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
78                                int devid, libxl_device_vtpm *vtpm);
79 int libxl_devid_to_device_usbctrl(libxl_ctx *ctx, uint32_t domid,
80                                   int devid, libxl_device_usbctrl *usbctrl);
81 
82 int libxl_devid_to_device_vdispl(libxl_ctx *ctx, uint32_t domid,
83                                  int devid, libxl_device_vdispl *vdispl);
84 
85 int libxl_ctrlport_to_device_usbdev(libxl_ctx *ctx, uint32_t domid,
86                                     int ctrl, int port,
87                                     libxl_device_usbdev *usbdev);
88 
89 int libxl_bitmap_alloc(libxl_ctx *ctx, libxl_bitmap *bitmap, int n_bits);
90     /* Allocated bimap is from malloc, libxl_bitmap_dispose() to be
91      * called by the application when done. */
92 void libxl_bitmap_copy_alloc(libxl_ctx *ctx, libxl_bitmap *dptr,
93                              const libxl_bitmap *sptr);
94 void libxl_bitmap_copy(libxl_ctx *ctx, libxl_bitmap *dptr,
95                        const libxl_bitmap *sptr);
96 int libxl_bitmap_is_full(const libxl_bitmap *bitmap);
97 int libxl_bitmap_is_empty(const libxl_bitmap *bitmap);
98 int libxl_bitmap_test(const libxl_bitmap *bitmap, int bit);
99 void libxl_bitmap_set(libxl_bitmap *bitmap, int bit);
100 void libxl_bitmap_reset(libxl_bitmap *bitmap, int bit);
101 int libxl_bitmap_count_set(const libxl_bitmap *bitmap);
102 int libxl_bitmap_or(libxl_ctx *ctx, libxl_bitmap *or_map,
103                     const libxl_bitmap *map1,
104                     const libxl_bitmap *map2);
105 int libxl_bitmap_and(libxl_ctx *ctx, libxl_bitmap *and_map,
106                      const libxl_bitmap *map1,
107                      const libxl_bitmap *map2);
108 char *libxl_bitmap_to_hex_string(libxl_ctx *ctx, const libxl_bitmap *bitmap);
libxl_bitmap_set_any(libxl_bitmap * bitmap)109 static inline void libxl_bitmap_set_any(libxl_bitmap *bitmap)
110 {
111     memset(bitmap->map, -1, bitmap->size);
112 }
libxl_bitmap_set_none(libxl_bitmap * bitmap)113 static inline void libxl_bitmap_set_none(libxl_bitmap *bitmap)
114 {
115     memset(bitmap->map, 0, bitmap->size);
116 }
libxl_bitmap_cpu_valid(libxl_bitmap * bitmap,int bit)117 static inline int libxl_bitmap_cpu_valid(libxl_bitmap *bitmap, int bit)
118 {
119     return bit >= 0 && bit < (bitmap->size * 8);
120 }
121 #define libxl_for_each_bit(var, map) for (var = 0; var < (map).size * 8; var++)
122 #define libxl_for_each_set_bit(v, m) for (v = 0; v < (m).size * 8; v++) \
123                                              if (libxl_bitmap_test(&(m), v))
124 
125 /*
126  * Compares two bitmaps bit by bit, up to nr_bits or, if nr_bits is 0, up
127  * to the size of the largest bitmap. If sizes does not match, bits past the
128  * of a bitmap are considered as being 0, which matches with the semantic and
129  * implementation of libxl_bitmap_test I think().
130  *
131  * So, basically, [0,1,0] and [0,1] are considered equal, while [0,1,1] and
132  * [0,1] are different.
133  */
libxl_bitmap_equal(const libxl_bitmap * ba,const libxl_bitmap * bb,int nr_bits)134 static inline int libxl_bitmap_equal(const libxl_bitmap *ba,
135                                      const libxl_bitmap *bb,
136                                      int nr_bits)
137 {
138     int i;
139 
140     if (nr_bits == 0)
141         nr_bits = ba->size > bb->size ? ba->size * 8 : bb->size * 8;
142 
143     for (i = 0; i < nr_bits; i++) {
144         if (libxl_bitmap_test(ba, i) != libxl_bitmap_test(bb, i))
145             return 0;
146     }
147     return 1;
148 }
149 
150 int libxl_cpu_bitmap_alloc(libxl_ctx *ctx, libxl_bitmap *cpumap, int max_cpus);
151 int libxl_node_bitmap_alloc(libxl_ctx *ctx, libxl_bitmap *nodemap,
152                             int max_nodes);
153 int libxl_socket_bitmap_alloc(libxl_ctx *ctx, libxl_bitmap *socketmap,
154                               int max_sockets);
155 /* Fill socketmap with the CPU topology information on the system. */
156 int libxl_get_online_socketmap(libxl_ctx *ctx, libxl_bitmap *socketmap);
157 
158 /* Populate cpumap with the cpus spanned by the nodes in nodemap */
159 int libxl_nodemap_to_cpumap(libxl_ctx *ctx,
160                             const libxl_bitmap *nodemap,
161                             libxl_bitmap *cpumap);
162 /* Populate cpumap with the cpus spanned by node */
163 int libxl_node_to_cpumap(libxl_ctx *ctx, int node,
164                          libxl_bitmap *cpumap);
165 /* Populate nodemap with the nodes of the cpus in cpumap */
166 int libxl_cpumap_to_nodemap(libxl_ctx *ctx,
167                             const libxl_bitmap *cpumap,
168                             libxl_bitmap *nodemap);
169 
libxl__sizekb_to_mb(uint32_t s)170  static inline uint32_t libxl__sizekb_to_mb(uint32_t s) {
171     return (s + 1023) / 1024;
172 }
173 
174 void libxl_string_copy(libxl_ctx *ctx, char **dst, char * const*src);
175 
176 
177 #define LIBXL_FILLZERO(object) (memset(&(object), 0, sizeof((object))))
178 
179 #endif
180 
181 /*
182  * Local variables:
183  * mode: C
184  * c-basic-offset: 4
185  * indent-tabs-mode: nil
186  * End:
187  */
188