GVM User Suite
User tools for the GVM open source project.
nvidia-api.cpp
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 <iostream>
20 
21 #include <unistd.h>
22 
23 #include <gpu/nvidia/resman/api.h>
25 #include <gpu/nvidia/device.h>
26 
27 #include <utils/colors.h>
28 #include <utils/device.h>
29 
30 using std::cout;
31 
184 {
185  int nvctl_fd = nv_open_dev(255);
186 
187  bool ret = rm_version_check(nvctl_fd, false, RM_VERSION);
188 
189  if (nvctl_fd != -1)
190  close(nvctl_fd);
191 
192  return ret;
193 }
194 
196 {
197  int nvctl_fd = nv_open_dev(255);
198 
199  bool ret = rm_version_check(nvctl_fd, false, NULL);
200 
201  if (nvctl_fd != -1)
202  close(nvctl_fd);
203 
204  return ret;
205 }
206 
208 {
209  int nvctl_fd = nv_open_dev(255);
210 
211  bool ret = rm_version_check(nvctl_fd, true, NULL);
212 
213  if (nvctl_fd != -1)
214  close(nvctl_fd);
215 
216  return ret;
217 }
218 
220 {
221  int ctlfd = nv_open_dev(255);
222 
223  struct NvResource *ptr = rm_alloc_res(ctlfd, NULL, 0, 0, NULL);
224 
225  bool ret = ptr != NULL;
226 
227  if (ret)
228  free(ptr);
229 
230  close(ctlfd);
231 
232  return ret;
233 }
234 
236 {
237  int ctlfd = -1;
238 
239  struct NvResource *ptr = rm_alloc_res(ctlfd, NULL, 0, 0, NULL);
240 
241  bool ret = ptr == NULL;
242 
243  if (!ret)
244  free(ptr);
245 
246  return ret;
247 }
248 
250 {
251  int ctlfd = STDOUT_FILENO;
252 
253  struct NvResource *ptr = rm_alloc_res(ctlfd, NULL, 0, 0, NULL);
254 
255  bool ret = ptr == NULL;
256 
257  if (!ret)
258  free(ptr);
259 
260  return ret;
261 }
262 
264 {
265  int ctlfd = nv_open_dev(255);
266 
267  struct NvResource *ptr = rm_alloc_res(ctlfd, NULL, 0, 0xA080, NULL);
268 
269  bool ret = ptr == NULL;
270 
271  if (!ret)
272  free(ptr);
273 
274  close(ctlfd);
275 
276  return ret;
277 }
278 
280 {
281  int ctlfd = nv_open_dev(255);
282 
283  struct NvResource *ptr = rm_alloc_res(ctlfd, NULL, 0, 0, NULL);
284 
285  bool ret = rm_free_res(ctlfd, ptr);
286 
287  if (ptr != NULL)
288  free(ptr);
289 
290  close(ctlfd);
291 
292  return ret;
293 }
294 
296 {
297  int ctlfd = nv_open_dev(255);
298 
299  struct NvResource *ptr = rm_alloc_res(ctlfd, NULL, 0, 0, NULL);
300 
301  bool ret = !rm_free_res(-1, ptr);
302 
303  if (ptr != NULL)
304  free(ptr);
305 
306  close(ctlfd);
307 
308  return ret;
309 }
310 
312 {
313  int ctlfd = nv_open_dev(255);
314 
315  struct NvResource *ptr = rm_alloc_res(ctlfd, NULL, 0, 0, NULL);
316 
317  bool ret = rm_free_res(ctlfd, ptr) && !rm_free_res(ctlfd, ptr);
318 
319  if (ptr != NULL)
320  free(ptr);
321 
322  close(ctlfd);
323 
324  return ret;
325 }
326 
328 {
329  int ctlfd = nv_open_dev(255);
330 
331  struct Nv0000CtrlGpuGetProbedIdsParams probed_ids = {};
332 
333  struct NvResource *ptr = rm_alloc_res(ctlfd, NULL, 0, 0, NULL);
334 
335  bool ret = false;
336 
337  if (ptr != NULL) {
338  ret = RM_CTRL(ctlfd, ptr, NV0000_GET_PROBED_IDS, probed_ids) != NULL && probed_ids.gpu_ids[0] != 0xFFFFFFFF;
339  rm_free_res(ctlfd, ptr);
340  free(ptr);
341  }
342 
343  close(ctlfd);
344  return ret;
345 }
346 
347 int main()
348 {
349  const uint32_t NUM_TESTS = 11;
350 
351  const std::string test_names[] = {
352  "RM Version Check Ok",
353  "RM Version Check Incorrect",
354  "RM Version Check Ignore",
355  "RM Alloc Root Ok",
356  "RM Alloc Root Fail (Invalid FD)",
357  "RM Alloc Root Fail (Bad FD)",
358  "RM Alloc Root Fail (Bad Argument)",
359  "RM Free Root Ok",
360  "RM Free Root Fail (Invalid FD)",
361  "RM Free Root Fail (Double Deallocate)",
362  "Get Probed Ids"
363  };
364  const std::string test_details[] = {
365  "Ensure your version check is correctly: " RM_VERSION,
366  "Invalid version is reported correctly, please check your driver version.",
367  "Ignored version is reported incorrectly, please check your driver version.",
368  "RM Alloc Root failed, check driver version.",
369  "RM Alloc Root succeeded, SHOULD NOT BE POSSIBLE.",
370  "RM Alloc Root succeeded, SHOULD NOT BE POSSIBLE.",
371  "RM Alloc Root succeeded, SHOULD NOT BE POSSIBLE.",
372  "RM Free Root failed, check driver version.",
373  "RM Free Root succeded, SHOULD NOT BE POSSIBLE.",
374  "RM Free Root succeded, SHOULD NOT BE POSSIBLE.",
375  "No GPU Ids were found."
376  };
377 
378  bool (*tests[])(void) = {
386  free_root_ok,
390  };
391 
392  uint32_t failures = 0;
393 
394  for (uint32_t i = 0; i < NUM_TESTS; ++i) {
395  bool test_result = tests[i]() == 1;
396 
397  if (test_result)
398  cout << GREEN_COLOR << test_names[i]
399  << " succeeded" RESET_COLOR "\n";
400  else {
401  ++failures;
402  cout << RED_COLOR << test_names[i]
403  << ": " << test_details[i]
404  << RESET_COLOR "\n";
405  }
406  }
407 
408  if (failures == 0)
409  cout << GREEN_COLOR << "All tests succeeded" RESET_COLOR "\n";
410  else
411  cout << RED_COLOR << failures << "/"
412  << NUM_TESTS << " tests failed" RESET_COLOR "\n";
413 }
uint8_t rm_version_check(int ctl_fd, uint8_t ignore_version, const char *version)
Version check for the RM API.
Definition: api.c:39
#define RM_CTRL(fd, res, cmd, data)
Controls a RM Resource.
Definition: api.h:133
struct NvResource * rm_alloc_res(int fd, struct NvResource *parent, uint32_t object, uint32_t rm_class, void *data)
Allocates a Node for a resource.
Definition: api.c:65
uint8_t rm_free_res(int fd, struct NvResource *object)
Frees a Node for a Resource.
Definition: api.c:124
#define RED_COLOR
Red color.
Definition: colors.h:30
#define GREEN_COLOR
Green color.
Definition: colors.h:33
#define RESET_COLOR
Reset color.
Definition: colors.h:51
int nv_open_dev(uint16_t minor)
Opens a traditional NVIDIA device.
Definition: device.c:65
#define NV0000_GET_PROBED_IDS
Command for rm control res to get a list of all probed ids.
Definition: nv0000.h:29
bool alloc_root_bad_fd()
Definition: nvidia-api.cpp:249
bool free_root_invalid_fd()
Definition: nvidia-api.cpp:295
bool version_check_bad()
Definition: nvidia-api.cpp:195
bool free_root_ok()
Definition: nvidia-api.cpp:279
bool get_probed_ids()
Definition: nvidia-api.cpp:327
bool alloc_root_bad_arg()
Definition: nvidia-api.cpp:263
bool alloc_root_invalid_fd()
Definition: nvidia-api.cpp:235
bool version_check_ignore()
Definition: nvidia-api.cpp:207
bool version_check_ok()
Definition: nvidia-api.cpp:183
bool alloc_root_ok()
Definition: nvidia-api.cpp:219
bool free_root_double_deallocate()
Definition: nvidia-api.cpp:311
int main()
Definition: nvidia-api.cpp:347
Gets a list of all probed Ids.
Definition: nv0000.h:35
uint32_t gpu_ids[32]
GPU IDs available.
Definition: nv0000.h:36
Resource for managing NVIDIA kernel module objects.
Definition: resources.h:39
#define free(x)
Definition: toml.c:52