NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
apsh_process_netscan_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(PROCESS_NETSCAN_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 process_netscan_get(const char *dma_device_name,
43  const char *pci_vuid,
44  enum doca_apsh_system_os os_type,
46  const char *mem_region,
47  const char *os_symbols)
48 {
50  int i, nb_processes;
51  struct doca_apsh_ctx *apsh_ctx;
52  struct doca_apsh_system *sys;
53  struct doca_apsh_process *proc, **processes;
54  int num_connections;
55  struct doca_apsh_netscan **connections;
56 
57  /* Init */
58  result = init_doca_apsh(dma_device_name, &apsh_ctx);
59  if (result != DOCA_SUCCESS) {
60  DOCA_LOG_ERR("Failed to init the DOCA APSH lib");
61  return result;
62  }
63  DOCA_LOG_INFO("DOCA APSH lib context init successful");
64 
65  result = init_doca_apsh_system(apsh_ctx, os_type, os_symbols, mem_region, pci_vuid, &sys);
66  if (result != DOCA_SUCCESS) {
67  DOCA_LOG_ERR("Failed to init the system context");
68  return result;
69  }
70  DOCA_LOG_INFO("DOCA APSH system context created");
71 
72  result = process_get(pid, sys, &nb_processes, &processes, &proc);
73  if (result != DOCA_SUCCESS) {
75  DOCA_LOG_ERR("Process pid %d not found", pid);
76  else
77  DOCA_LOG_ERR("DOCA APSH encountered an error: %s", doca_error_get_descr(result));
78  cleanup_doca_apsh(apsh_ctx, sys);
79  return result;
80  }
81  DOCA_LOG_INFO("Process with PID %u found", pid);
83 
84  result = doca_apsh_process_netscan_get(proc, &connections, &num_connections);
85  if (result != DOCA_SUCCESS) {
86  DOCA_LOG_ERR("Failed to read connections info from host");
87  doca_apsh_processes_free(processes);
88  cleanup_doca_apsh(apsh_ctx, sys);
89  return result;
90  }
91  DOCA_LOG_INFO("Successfully performed %s. Host proc(%d) contains %d connections",
92  __func__,
93  pid,
94  num_connections);
95 
96  /* Print some attributes of the connections */
97  DOCA_LOG_INFO("Connections for process %u:", pid);
98  for (i = 0; i < num_connections; ++i) {
100  "\tConnection %d - Protocol: %s, local address: %s:%lu, local address: %s:%lu, state %s",
101  i,
108  if (os_type == DOCA_APSH_SYSTEM_LINUX) {
109  DOCA_LOG_INFO("\tConnection %d - FD: %u, family: %s, type: %s, filter %s, namespace: %u",
110  i,
115  doca_apsh_netscan_info_get(connections[i],
117  }
118  if (os_type == DOCA_APSH_SYSTEM_LINUX &&
119  !strcmp(doca_apsh_netscan_info_get(connections[i], DOCA_APSH_NETSCAN_PROTOCOL), "TCP")) {
121  "\tConnection %d - TCP connection - Bytes sent: %lu, Bytes acked: %lu, Bytes received: %lu, Segs in: %u, Segs out: %u, Data segs in: %u, Data segs out: %u",
122  i,
130  }
131  }
132 
133  /* Cleanup */
134  doca_apsh_netscan_free(connections);
135  doca_apsh_processes_free(processes);
136  cleanup_doca_apsh(apsh_ctx, sys);
137  return DOCA_SUCCESS;
138 }
int32_t pid
int32_t result
doca_error_t process_get(DOCA_APSH_PROCESS_PID_TYPE pid, struct doca_apsh_system *sys, int *nb_procs, struct doca_apsh_process ***processes, struct doca_apsh_process **process)
Definition: apsh_common.c:397
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 process_netscan_get(const char *dma_device_name, const char *pci_vuid, enum doca_apsh_system_os os_type, DOCA_APSH_PROCESS_PID_TYPE pid, const char *mem_region, const char *os_symbols)
DOCA_LOG_REGISTER(PROCESS_NETSCAN_GET)
uint32_t DOCA_APSH_PROCESS_PID_TYPE
process pid type
doca_apsh_system_os
system os types
@ DOCA_APSH_NETSCAN_LINUX_TCP_DATA_SEGS_IN
@ DOCA_APSH_NETSCAN_LINUX_TCP_BYTES_ACKED
@ DOCA_APSH_NETSCAN_REMOTE_PORT
@ DOCA_APSH_NETSCAN_STATE
@ DOCA_APSH_NETSCAN_LINUX_TCP_BYTES_SENT
@ DOCA_APSH_NETSCAN_LINUX_TCP_DATA_SEGS_OUT
@ DOCA_APSH_NETSCAN_LINUX_FD
@ DOCA_APSH_NETSCAN_LINUX_TCP_BYTES_RECEIVED
@ DOCA_APSH_NETSCAN_LINUX_FILTER
@ DOCA_APSH_NETSCAN_LINUX_TCP_SEGS_IN
@ DOCA_APSH_NETSCAN_PROTOCOL
@ DOCA_APSH_NETSCAN_REMOTE_ADDR
@ DOCA_APSH_NETSCAN_LOCAL_PORT
@ DOCA_APSH_NETSCAN_LINUX_TYPE
@ DOCA_APSH_NETSCAN_LINUX_NET_NAMESPACE
@ DOCA_APSH_NETSCAN_LINUX_FAMILY
@ DOCA_APSH_NETSCAN_LINUX_TCP_SEGS_OUT
@ DOCA_APSH_NETSCAN_LOCAL_ADDR
@ DOCA_APSH_SYSTEM_LINUX
@ DOCA_APSH_PROCESS_COMM
DOCA_EXPERIMENTAL void doca_apsh_netscan_free(struct doca_apsh_netscan **connections)
Destroys a netscan context.
DOCA_EXPERIMENTAL doca_error_t doca_apsh_process_netscan_get(struct doca_apsh_process *process, struct doca_apsh_netscan ***connections, int *connections_size)
Get array of current connections for a specified process.
#define doca_apsh_netscan_info_get(connection, attr)
Get attribute value for a connection.
Definition: doca_apsh.h:1432
#define doca_apsh_process_info_get(process, attr)
Get attribute value for a process.
Definition: doca_apsh.h:618
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_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_NOT_FOUND
Definition: doca_error.h:54
@ 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