GVM User Suite
User tools for the GVM open source project.
configs.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 2666680 Ontario Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  */
19 #include <utils/configs.h>
20 
21 #include <toml.h>
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 
29 static inline void failed_parse(const char* s)
30 {
31  printf("Missing required field in request config '%s', exiting program\n", s);
32  exit(1);
33 }
34 
35 struct GpuConfigs get_configs(const char* name)
36 {
37  FILE *fp;
38  char error_buffer[1024];
39  struct GpuConfigs ret = {};
40 
41  fp = fopen(name, "r");
42  if (fp == NULL)
43  return ret;
44 
45  toml_table_t* base = toml_parse_file(fp, error_buffer, sizeof(error_buffer));
46  fclose(fp);
47 
48  if (base == NULL) {
49  printf("Error parsing config: %s\n", error_buffer);
50  return ret;
51  }
52 
53  toml_array_t* config_array = toml_array_in(base, "config");
54 
55  ret.config_size = toml_array_nelem(config_array);
56  ret.configs = calloc(ret.config_size, sizeof(struct GpuConfig));
57 
58  for (size_t i = 0; i < ret.config_size; ++i) {
59  toml_table_t* config = toml_table_at(config_array, i);
60 
61  toml_array_t* gpu_array = toml_array_in(config, "gpu_config");
62  ret.configs[i].gpu_size = toml_array_nelem(gpu_array);
63  int max_gpu_size = ret.configs[i].gpu_size * sizeof(struct Gpu);
64  ret.configs[i].gpus = malloc(max_gpu_size);
65  memset(ret.configs[i].gpus, 0xFF, max_gpu_size);
66 
67  for (size_t j = 0; j < ret.configs[i].gpu_size; ++j) {
68  toml_table_t* gpu = toml_table_at(gpu_array, j);
69  toml_datum_t info = {};
70 
71  info = toml_int_in(gpu, "domain");
72  if (info.ok)
73  ret.configs[i].gpus[j].domain = info.u.i;
74 
75  info = toml_int_in(gpu, "bus");
76  if (info.ok)
77  ret.configs[i].gpus[j].bus = info.u.i;
78 
79  info = toml_int_in(gpu, "slot");
80  if (info.ok)
81  ret.configs[i].gpus[j].slot = info.u.i;
82 
83  info = toml_int_in(gpu, "function");
84  if (info.ok)
85  ret.configs[i].gpus[j].function = info.u.i;
86 
87  info = toml_int_in(gpu, "vendor_id");
88  if (info.ok)
89  ret.configs[i].gpus[j].vendor_id = info.u.i;
90 
91  info = toml_int_in(gpu, "device_id");
92  if (info.ok)
93  ret.configs[i].gpus[j].device_id = info.u.i;
94 
95  info = toml_int_in(gpu, "sub_vendor_id");
96  if (info.ok)
97  ret.configs[i].gpus[j].sub_vendor_id = info.u.i;
98 
99  info = toml_int_in(gpu, "sub_device_id");
100  if (info.ok)
101  ret.configs[i].gpus[j].sub_device_id = info.u.i;
102 
103  info = toml_int_in(gpu, "identifier");
104  if (info.ok)
105  ret.configs[i].gpus[j].identifier = info.u.i;
106  }
107 
108  toml_array_t* req_array = toml_array_in(config, "request");
109  ret.configs[i].mdev_size = toml_array_nelem(req_array);
110  ret.configs[i].requests = calloc(ret.configs[i].mdev_size, sizeof(struct MDevRequest));
111 
112  for (size_t j = 0; j < ret.configs[i].mdev_size; ++j) {
113  toml_table_t* mdev = toml_table_at(req_array, j);
114  toml_table_t* display = toml_table_in(mdev, "display");
115  toml_datum_t info = {};
116 
117  info = toml_int_in(mdev, "num");
118  if (!info.ok)
119  failed_parse("num");
120 
121  ret.configs[i].requests[j].num = info.u.i;
122 
123  info = toml_int_in(mdev, "fb_len");
124  if (!info.ok)
125  failed_parse("fb_len");
126 
127  ret.configs[i].requests[j].fb_len = info.u.i;
128 
129  info = toml_int_in(mdev, "fb_res");
130  if (!info.ok)
131  failed_parse("fb_res");
132 
133  ret.configs[i].requests[j].fb_res = info.u.i;
134 
135  info = toml_int_in(mdev, "max_instances");
136  if (!info.ok)
137  failed_parse("max_instances");
138 
139  ret.configs[i].requests[j].max_inst = info.u.i;
140 
141  info = toml_int_in(mdev, "v_dev_id");
142  ret.configs[i].requests[j].v_dev_id =
143  info.ok ? ((unsigned) info.u.i) : 0xFFFFFFFFFFFFFFFF;
144 
145  info = toml_int_in(mdev, "p_dev_id");
146  ret.configs[i].requests[j].p_dev_id =
147  info.ok ? ((unsigned) info.u.i) : 0xFFFFFFFFFFFFFFFF;
148 
149  info = toml_bool_in(mdev, "ecc_support");
150  ret.configs[i].requests[j].ecc_support =
151  info.ok ? info.u.i != 0 : 0;
152 
153  info = toml_bool_in(mdev, "multi_mdev");
154  ret.configs[i].requests[j].multi_mdev =
155  info.ok ? info.u.i != 0 : 0;
156 
157  info = toml_int_in(mdev, "map_vid_size");
158  ret.configs[i].requests[j].map_vid_size =
159  info.ok ? info.u.i : 24;
160 
161  info = toml_int_in(mdev, "enc_cap");
162  ret.configs[i].requests[j].enc_cap =
163  info.ok ? info.u.i : 100;
164 
165  info = toml_int_in(mdev, "bar1_len");
166  ret.configs[i].requests[j].bar1_len =
167  info.ok ? info.u.i : 0x100;
168 
169  info = toml_string_in(mdev, "name");
170  ret.configs[i].requests[j].name =
171  info.ok ? info.u.s : "GVM GPU";
172 
173  info = toml_string_in(mdev, "class");
174  ret.configs[i].requests[j].gpu_class =
175  info.ok ? info.u.s : "Compute";
176 
177  ret.configs[i].requests[j].disp = calloc(1, sizeof(struct VirtDisplay));
178 
179  info.ok = 0;
180  if (display)
181  info = toml_int_in(display, "num_heads");
182  ret.configs[i].requests[j].disp->num_heads =
183  info.ok ? info.u.i : 1;
184 
185  if (display)
186  info = toml_int_in(display, "max_res_x");
187  ret.configs[i].requests[j].disp->max_res_x =
188  info.ok ? info.u.i : 1024;
189 
190  if (display)
191  info = toml_int_in(display, "max_res_y");
192  ret.configs[i].requests[j].disp->max_res_y =
193  info.ok ? info.u.i : 1024;
194 
195  if (display)
196  info = toml_int_in(display, "frl_config");
197  ret.configs[i].requests[j].disp->num_heads =
198  info.ok ? info.u.i : 120;
199 
200  if (display)
201  info = toml_int_in(display, "frl_enable");
202  ret.configs[i].requests[j].disp->num_heads =
203  info.ok ? info.u.i : 0;
204  }
205  }
206 
207  toml_free(base);
208 
209  return ret;
210 }
struct GpuConfigs get_configs(const char *name)
Parses a config file.
Definition: configs.c:35
static void failed_parse(const char *s)
Definition: configs.c:29
Structure to handle GPU Configurations.
Definition: configs.h:34
struct MDevRequest * requests
List of requested mdevs.
Definition: configs.h:37
size_t mdev_size
How many requested mdevs are in the list of mdevs.
Definition: configs.h:38
size_t gpu_size
How many gpus are in the list of gpus.
Definition: configs.h:36
struct Gpu * gpus
List of gpus.
Definition: configs.h:35
Structure to provide a list of GPU configurations.
Definition: configs.h:45
size_t config_size
Size of the configuration list.
Definition: configs.h:47
struct GpuConfig * configs
List of configurations.
Definition: configs.h:46
GPU Base Structure.
Definition: mdev.h:33
uint32_t identifier
Identifier for the GPU.
Definition: mdev.h:42
uint32_t device_id
Device id for the PCI device.
Definition: mdev.h:39
uint32_t slot
Slot for the PCI device.
Definition: mdev.h:36
uint32_t sub_vendor_id
Sub vendor id for the PCI device.
Definition: mdev.h:40
uint32_t bus
Bus for the PCI device.
Definition: mdev.h:35
uint32_t sub_device_id
Sub device id for the PCI device.
Definition: mdev.h:41
uint32_t vendor_id
Vendor id for the PCI device.
Definition: mdev.h:38
uint32_t function
Function for the PCI device.
Definition: mdev.h:37
uint32_t domain
Domain for the PCI device.
Definition: mdev.h:34
Mediated Device Request Structure.
Definition: mdev.h:62
uint32_t map_vid_size
Mappable video size (IN MEGABYTES).
Definition: mdev.h:74
const char * name
Name of the GPU.
Definition: mdev.h:66
uint8_t ecc_support
If the Mediated GPU has ECC supported.
Definition: mdev.h:70
uint32_t bar1_len
Bar 1 Length.
Definition: mdev.h:77
uint8_t multi_mdev
If multiple mdevs supported.
Definition: mdev.h:71
uint32_t max_inst
Max number of mediated GPUs.
Definition: mdev.h:68
uint64_t v_dev_id
Virtual device id.
Definition: mdev.h:64
uint32_t fb_res
Frame buffer reserved (IN MEGABYTES).
Definition: mdev.h:73
uint64_t p_dev_id
Physical device id.
Definition: mdev.h:65
uint32_t fb_len
Frame buffer length (IN MEGABYTES).
Definition: mdev.h:72
uint32_t num
Number of the mdev.
Definition: mdev.h:63
uint32_t enc_cap
Definition: mdev.h:75
const char * gpu_class
GPU Class structure.
Definition: mdev.h:67
struct VirtDisplay * disp
Virtual display structure.
Definition: mdev.h:69
Virtual Display.
Definition: mdev.h:49
uint32_t max_res_y
Max resolution Y.
Definition: mdev.h:52
uint32_t max_res_x
Max resolution X.
Definition: mdev.h:51
uint32_t num_heads
Number of monitor heads.
Definition: mdev.h:50
union toml_datum_t::@1 u
char * s
Definition: toml.h:86
int ok
Definition: toml.h:83
int64_t i
Definition: toml.h:88
#define malloc(x)
Definition: toml.c:51
#define calloc(x, y)
Definition: toml.c:53
TOML_EXTERN toml_datum_t toml_int_in(const toml_table_t *arr, const char *key)
Definition: toml.c:2327
TOML_EXTERN void toml_free(toml_table_t *tab)
Definition: toml.c:1578
TOML_EXTERN toml_table_t * toml_parse_file(FILE *fp, char *errbuf, int errbufsz)
Definition: toml.c:1475
TOML_EXTERN toml_table_t * toml_table_at(const toml_array_t *arr, int idx)
Definition: toml.c:1955
TOML_EXTERN toml_datum_t toml_string_in(const toml_table_t *arr, const char *key)
Definition: toml.c:2310
TOML_EXTERN int toml_array_nelem(const toml_array_t *arr)
Definition: toml.c:1935
TOML_EXTERN toml_table_t * toml_table_in(const toml_table_t *tab, const char *key)
Definition: toml.c:1910
TOML_EXTERN toml_array_t * toml_array_in(const toml_table_t *tab, const char *key)
Definition: toml.c:1901
TOML_EXTERN toml_datum_t toml_bool_in(const toml_table_t *arr, const char *key)
Definition: toml.c:2320