NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
apsh_interfaces_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(INTERFACES_GET);
34 #define BUFFER_SIZE 2048
35 
36 /*
37  * Calls the DOCA APSH API function that matches this sample name and prints the result
38  *
39  * @dma_device_name [in]: IBDEV Name of the device to use for DMA
40  * @pci_vuid [in]: VUID of the device exposed to the target system
41  * @os_type [in]: Indicates the OS type of the target system
42  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
43  */
44 doca_error_t interfaces_get(const char *dma_device_name,
45  const char *pci_vuid,
46  enum doca_apsh_system_os os_type,
47  const char *mem_region,
48  const char *os_symbols)
49 {
51  int num_interfaces, i;
52  struct doca_apsh_ctx *apsh_ctx;
53  struct doca_apsh_system *sys;
54  struct doca_apsh_interface **interfaces;
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_interfaces_get(sys, &interfaces, &num_interfaces);
72  if (result != DOCA_SUCCESS) {
73  DOCA_LOG_ERR("Failed to create the interfaces list");
74  cleanup_doca_apsh(apsh_ctx, sys);
75  return result;
76  }
77  DOCA_LOG_INFO("Successfully performed %s. Host system contains %d interfaces", __func__, num_interfaces);
78  DOCA_LOG_INFO("Interfaces of system:");
79  for (i = 0; i < num_interfaces; i++) {
80  char buffer[BUFFER_SIZE];
81  int offset = 0;
82  offset += snprintf(buffer + offset,
83  BUFFER_SIZE - offset,
84  "Interface number %d\nname: %s\n",
85  i,
87  uint32_t ipv4_arr_size =
89  if (ipv4_arr_size > 0) {
90  offset += snprintf(buffer + offset, BUFFER_SIZE - offset, "IPV4:\n");
91  char **ipv4_addr_arr =
93  unsigned char *ipv4_prefixlen_arr =
94  doca_apsh_interface_info_get(interfaces[i],
96  // prints only first 5 IPV4 addresses
97  for (uint32_t j = 0; j < ipv4_arr_size && j < 5; j++) {
98  offset += snprintf(buffer + offset,
99  BUFFER_SIZE - offset,
100  "%s, prefixlen: %u\n",
101  ipv4_addr_arr[j],
102  ipv4_prefixlen_arr[j]);
103  }
104  }
105  uint32_t ipv6_arr_size =
107  if (ipv6_arr_size > 0) {
108  offset += snprintf(buffer + offset, BUFFER_SIZE - offset, "IPV6:\n");
109  char **ipv6_addr_arr =
111  uint32_t *ipv6_prefixlen_arr =
112  doca_apsh_interface_info_get(interfaces[i],
114  // prints only first 5 IPV6 addresses
115  for (uint32_t j = 0; j < ipv6_arr_size && j < 5; j++) {
116  offset += snprintf(buffer + offset,
117  BUFFER_SIZE - offset,
118  "%s, prefixlen: %u\n",
119  ipv6_addr_arr[j],
120  ipv6_prefixlen_arr[j]);
121  }
122  }
123  uint32_t mac_arr_size =
125  if (mac_arr_size > 0) {
126  offset += snprintf(buffer + offset, BUFFER_SIZE - offset, "MAC address:\n");
127  char **mac_addr_arr =
129  // prints only first 5 MAC addresses
130  for (uint32_t j = 0; j < mac_arr_size && j < 5; j++) {
131  offset += snprintf(buffer + offset, BUFFER_SIZE - offset, "%s\n", mac_addr_arr[j]);
132  }
133  }
134  DOCA_LOG_INFO("%s", buffer);
135  }
136  doca_apsh_interfaces_free(interfaces);
137  cleanup_doca_apsh(apsh_ctx, sys);
138  return DOCA_SUCCESS;
139 }
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(INTERFACES_GET)
#define BUFFER_SIZE
doca_error_t interfaces_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_apsh_system_os
system os types
@ DOCA_APSH_LINUX_INTERFACE_IPV6_PREFIX_LEN_ARR
@ DOCA_APSH_LINUX_INTERFACE_IPV4_ARR
@ DOCA_APSH_LINUX_INTERFACE_IPV6_ARR
@ DOCA_APSH_LINUX_INTERFACE_MAC_ARR_SIZE
@ DOCA_APSH_LINUX_INTERFACE_MAC_ARR
@ DOCA_APSH_LINUX_INTERFACE_IPV4_ARR_SIZE
@ DOCA_APSH_LINUX_INTERFACE_NAME
@ DOCA_APSH_LINUX_INTERFACE_IPV6_ARR_SIZE
@ DOCA_APSH_LINUX_INTERFACE_IPV4_PREFIX_LEN_ARR
DOCA_EXPERIMENTAL doca_error_t doca_apsh_interfaces_get(struct doca_apsh_system *system, struct doca_apsh_interface ***interfaces, int *interfaces_size)
Get array of all interfaces.
DOCA_EXPERIMENTAL void doca_apsh_interfaces_free(struct doca_apsh_interface **interfaces)
Destroys an interfaces data array.
#define doca_apsh_interface_info_get(interface, attr)
Get attribute value for an interface.
Definition: doca_apsh.h:1505
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