NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
apsh_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 <inttypes.h>
27 
28 #include <doca_apsh.h>
29 #include <doca_log.h>
30 
31 #include "apsh_common.h"
32 
33 DOCA_LOG_REGISTER(NETSCAN_GET);
34 #define BUFFER_SIZE 2048
35 
36 void collect_interface_data(struct doca_apsh_netscan *connection, char *buffer)
37 {
38  int offset = 0;
39  char *interface_name = doca_apsh_netscan_info_get(connection, DOCA_APSH_NETSCAN_LINUX_INTERFACE_NAME);
40  if (interface_name == NULL) {
41  buffer[0] = '-';
42  buffer[1] = '\0';
43  return;
44  }
45  offset += snprintf(buffer + offset,
46  BUFFER_SIZE - offset,
47  "Interface name: %s",
49 
50  uint32_t ipv4_arr_size =
52  if (ipv4_arr_size > 0) {
53  offset += snprintf(buffer + offset, BUFFER_SIZE - offset, ", IPV4: ");
54  char **ipv4_addr_arr =
56  // prints only first 5 IPV4 addresses
57  for (uint32_t j = 0; j < ipv4_arr_size && j < 5; j++) {
58  offset += snprintf(buffer + offset, BUFFER_SIZE - offset, "%s ", ipv4_addr_arr[j]);
59  }
60  }
61  uint32_t ipv6_arr_size =
63  if (ipv6_arr_size > 0) {
64  offset += snprintf(buffer + offset, BUFFER_SIZE - offset, ", IPV6: ");
65  char **ipv6_addr_arr =
67  // prints only first 5 IPV6 addresses
68  for (uint32_t j = 0; j < ipv6_arr_size && j < 5; j++) {
69  offset += snprintf(buffer + offset, BUFFER_SIZE - offset, "%s ", ipv6_addr_arr[j]);
70  }
71  }
73  if (mac_arr_size > 0) {
74  offset += snprintf(buffer + offset, BUFFER_SIZE - offset, ", MAC address: ");
76  // prints only first 5 MAC addresses
77  for (uint32_t j = 0; j < mac_arr_size && j < 5; j++) {
78  offset += snprintf(buffer + offset, BUFFER_SIZE - offset, "%s ", mac_addr_arr[j]);
79  }
80  }
81 }
82 /*
83  * Calls the DOCA APSH API function that matches this sample name and prints the result
84  *
85  * @dma_device_name [in]: IBDEV Name of the device to use for DMA
86  * @pci_vuid [in]: VUID of the device exposed to the target system
87  * @os_type [in]: Indicates the OS type of the target system
88  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
89  */
90 doca_error_t netscan_get(const char *dma_device_name,
91  const char *pci_vuid,
92  enum doca_apsh_system_os os_type,
93  const char *mem_region,
94  const char *os_symbols)
95 {
97  int num_connections, i;
98  struct doca_apsh_ctx *apsh_ctx;
99  struct doca_apsh_system *sys;
100  struct doca_apsh_netscan **connections;
101 
102  /* Init */
103  result = init_doca_apsh(dma_device_name, &apsh_ctx);
104  if (result != DOCA_SUCCESS) {
105  DOCA_LOG_ERR("Failed to init the DOCA APSH lib");
106  return result;
107  }
108  DOCA_LOG_INFO("DOCA APSH lib context init successful");
109 
110  result = init_doca_apsh_system(apsh_ctx, os_type, os_symbols, mem_region, pci_vuid, &sys);
111  if (result != DOCA_SUCCESS) {
112  DOCA_LOG_ERR("Failed to init the system context");
113  return result;
114  }
115  DOCA_LOG_INFO("DOCA APSH system context created");
116 
117  result = doca_apsh_netscan_get(sys, &connections, &num_connections);
118  if (result != DOCA_SUCCESS) {
119  DOCA_LOG_ERR("Failed to create the connection list");
120  cleanup_doca_apsh(apsh_ctx, sys);
121  return result;
122  }
123  DOCA_LOG_INFO("Successfully performed %s. Host system contains %d connections", __func__, num_connections);
124 
125  /* Print some attributes of the connections */
126  DOCA_LOG_INFO("Connections of system:");
127  for (i = 0; i < num_connections; ++i) {
129  "\tConnection %d - PID: %u, COMM: %s, Protocol: %s, local address: %s:%lu, local address: %s:%lu, state %s",
130  i,
139  if (os_type == DOCA_APSH_SYSTEM_LINUX) {
140  char interfaces_data_buffer[BUFFER_SIZE];
141  collect_interface_data(connections[i], interfaces_data_buffer);
142  DOCA_LOG_INFO("\tConnection %d - FD: %u, family: %s, type: %s, filter %s, Interface data: %s",
143  i,
148  interfaces_data_buffer);
149  }
150  if (os_type == DOCA_APSH_SYSTEM_LINUX &&
151  !strcmp(doca_apsh_netscan_info_get(connections[i], DOCA_APSH_NETSCAN_PROTOCOL), "TCP")) {
153  "\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",
154  i,
162  }
163  }
164 
165  /* Cleanup */
166  doca_apsh_netscan_free(connections);
167  cleanup_doca_apsh(apsh_ctx, sys);
168  return DOCA_SUCCESS;
169 }
#define NULL
Definition: __stddef_null.h:26
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_LOG_REGISTER(NETSCAN_GET)
#define BUFFER_SIZE
doca_error_t netscan_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)
void collect_interface_data(struct doca_apsh_netscan *connection, char *buffer)
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_INTERFACE_IPV4_ARR_SIZE
@ DOCA_APSH_NETSCAN_LINUX_FD
@ DOCA_APSH_NETSCAN_LINUX_TCP_BYTES_RECEIVED
@ DOCA_APSH_NETSCAN_LINUX_INTERFACE_MAC_ARR_SIZE
@ DOCA_APSH_NETSCAN_LINUX_FILTER
@ DOCA_APSH_NETSCAN_LINUX_INTERFACE_NAME
@ DOCA_APSH_NETSCAN_LINUX_INTERFACE_IPV4_ARR
@ DOCA_APSH_NETSCAN_LINUX_INTERFACE_MAC_ARR
@ DOCA_APSH_NETSCAN_LINUX_INTERFACE_IPV6_ARR
@ DOCA_APSH_NETSCAN_LINUX_TCP_SEGS_IN
@ DOCA_APSH_NETSCAN_LINUX_INTERFACE_IPV6_ARR_SIZE
@ DOCA_APSH_NETSCAN_COMM
@ DOCA_APSH_NETSCAN_PROTOCOL
@ DOCA_APSH_NETSCAN_REMOTE_ADDR
@ DOCA_APSH_NETSCAN_LOCAL_PORT
@ DOCA_APSH_NETSCAN_LINUX_TYPE
@ DOCA_APSH_NETSCAN_LINUX_FAMILY
@ DOCA_APSH_NETSCAN_LINUX_TCP_SEGS_OUT
@ DOCA_APSH_NETSCAN_PID
@ DOCA_APSH_NETSCAN_LOCAL_ADDR
@ DOCA_APSH_SYSTEM_LINUX
DOCA_EXPERIMENTAL void doca_apsh_netscan_free(struct doca_apsh_netscan **connections)
Destroys a netscan context.
#define doca_apsh_netscan_info_get(connection, attr)
Get attribute value for a connection.
Definition: doca_apsh.h:1432
DOCA_EXPERIMENTAL doca_error_t doca_apsh_netscan_get(struct doca_apsh_system *system, struct doca_apsh_netscan ***connections, int *connections_size)
Get array of current connections.
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