NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
apsh_containers_get_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #include <doca_apsh.h>
27 #include <doca_log.h>
28 
29 #include "apsh_common.h"
30 
31 DOCA_LOG_REGISTER(CONTAINERS_GET);
32 
33 /*
34  * Calls the DOCA APSH API function that matches this sample name and prints the result
35  *
36  * @dma_device_name [in]: IBDEV Name of the device to use for DMA
37  * @pci_vuid [in]: VUID of the device exposed to the target system
38  * @os_type [in]: Indicates the OS type of the target system
39  * @pid [in]: PID of the target process
40  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
41  */
42 doca_error_t containers_get(const char *dma_device_name,
43  const char *pci_vuid,
44  enum doca_apsh_system_os os_type,
45  const char *mem_region,
46  const char *os_symbols)
47 {
49  int i;
50  struct doca_apsh_ctx *apsh_ctx;
51  struct doca_apsh_system *sys;
52  struct doca_apsh_process **processes;
53  int num_containers, num_processes;
54  struct doca_apsh_container **containers_list;
55 
56  /* Init */
57  result = init_doca_apsh(dma_device_name, &apsh_ctx);
58  if (result != DOCA_SUCCESS) {
59  DOCA_LOG_ERR("Failed to init the DOCA APSH lib");
60  return result;
61  }
62  DOCA_LOG_INFO("DOCA APSH lib context init successful");
63 
64  result = init_doca_apsh_system(apsh_ctx, os_type, os_symbols, mem_region, pci_vuid, &sys);
65  if (result != DOCA_SUCCESS) {
66  DOCA_LOG_ERR("Failed to init the system context");
67  return result;
68  }
69  DOCA_LOG_INFO("DOCA APSH system context created");
70 
71  result = doca_apsh_containers_get(sys, &containers_list, &num_containers);
72  if (result != DOCA_SUCCESS) {
73  DOCA_LOG_ERR("Failed to read containers info from host");
74  cleanup_doca_apsh(apsh_ctx, sys);
75  return result;
76  }
77 
78  DOCA_LOG_INFO("Successfully performed %s. Host system contains %d containers", __func__, num_containers);
79 
80  for (i = 0; i < num_containers; ++i) {
81  const char *container_id = doca_apsh_container_info_get(containers_list[i], DOCA_APSH_CONTAINER_ID);
82 
83  DOCA_LOG_INFO("\tContainer %d - id: %s", i, container_id);
84 
85  result = doca_apsh_container_processes_get(containers_list[i], &processes, &num_processes);
86  if (result != DOCA_SUCCESS) {
87  DOCA_LOG_ERR("Failed to read containers info from host");
88  doca_apsh_containers_free(containers_list);
89  cleanup_doca_apsh(apsh_ctx, sys);
90  return result;
91  }
92  DOCA_LOG_INFO("First 5 (or less) processes of container:");
93  for (int j = 0; j < num_processes && j < 5; j++)
94  DOCA_LOG_INFO("\t\tProcess %d - name: %s, pid: %u, pid_ns %u, mnt_ns %u, net_ns %u",
95  j,
101  doca_apsh_processes_free(processes);
102  }
103 
104  /* Cleanup */
105  doca_apsh_containers_free(containers_list);
106  cleanup_doca_apsh(apsh_ctx, sys);
107  return DOCA_SUCCESS;
108 }
int32_t result
doca_error_t cleanup_doca_apsh(struct doca_apsh_ctx *ctx, struct doca_apsh_system *system)
Definition: apsh_common.c:384
doca_error_t init_doca_apsh_system(struct doca_apsh_ctx *ctx, enum doca_apsh_system_os os_type, const char *os_symbols, const char *mem_region, const char *pci_vuid, struct doca_apsh_system **system)
Definition: apsh_common.c:325
doca_error_t init_doca_apsh(const char *dma_device_name, struct doca_apsh_ctx **ctx)
Definition: apsh_common.c:287
doca_error_t containers_get(const char *dma_device_name, const char *pci_vuid, enum doca_apsh_system_os os_type, const char *mem_region, const char *os_symbols)
DOCA_LOG_REGISTER(CONTAINERS_GET)
doca_apsh_system_os
system os types
@ DOCA_APSH_CONTAINER_ID
@ DOCA_APSH_PROCESS_COMM
@ DOCA_APSH_PROCESS_PID
@ DOCA_APSH_PROCESS_LINUX_NS_MNT
@ DOCA_APSH_PROCESS_LINUX_NS_PID
@ DOCA_APSH_PROCESS_LINUX_NS_NET
#define doca_apsh_container_info_get(container, attr)
Get attribute value for a container.
Definition: doca_apsh.h:1727
DOCA_EXPERIMENTAL doca_error_t doca_apsh_container_processes_get(struct doca_apsh_container *container, struct doca_apsh_process ***processes, int *processes_size)
Get array of current processes running on the container.
DOCA_EXPERIMENTAL doca_error_t doca_apsh_containers_get(struct doca_apsh_system *system, struct doca_apsh_container ***containers, int *containers_size)
Get array of current containers running on the system.
#define doca_apsh_process_info_get(process, attr)
Get attribute value for a process.
Definition: doca_apsh.h:618
DOCA_EXPERIMENTAL void doca_apsh_containers_free(struct doca_apsh_container **containers)
Destroys a container context.
DOCA_EXPERIMENTAL void doca_apsh_processes_free(struct doca_apsh_process **processes)
Destroys a process context.
enum doca_error doca_error_t
DOCA API return codes.
@ DOCA_SUCCESS
Definition: doca_error.h:38
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486