NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
rmax_list_devices_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 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 <stdlib.h>
27 #include <stdbool.h>
28 #include <netinet/in.h>
29 
30 #include <doca_dev.h>
31 #include <doca_log.h>
32 #include <doca_rmax.h>
33 
34 #include "rmax_common.h"
35 
36 DOCA_LOG_REGISTER(RMAX_LIST_DEVICES);
37 
38 /*
39  * Run rmax_list_devices sample
40  *
41  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
42  */
44 {
46  uint32_t nb_devs, i;
47  uint8_t addr[4];
48  bool has_ptp = true;
49  struct doca_devinfo **devinfo;
50  char dev_pci_addr[DOCA_DEVINFO_PCI_ADDR_SIZE];
51 
52  result = doca_rmax_init();
53  if (result != DOCA_SUCCESS) {
54  DOCA_LOG_ERR("Failed to initialize DOCA Rivermax: %s", doca_error_get_descr(result));
55  return result;
56  }
57 
58  result = doca_devinfo_create_list(&devinfo, &nb_devs);
59  if (result != DOCA_SUCCESS) {
60  DOCA_LOG_ERR("Failed to create devices info list: %s", doca_error_get_descr(result));
61  return result;
62  }
63 
64  for (i = 0; i < nb_devs; ++i) {
65  /* get PCI address */
66  result = doca_devinfo_get_pci_addr_str(devinfo[i], dev_pci_addr);
67  if (result != DOCA_SUCCESS) {
68  DOCA_LOG_ERR("Failed to get PCI address: %s", doca_error_get_descr(result));
69  return result;
70  }
71  /* get IP address */
72  result = doca_devinfo_get_ipv4_addr(devinfo[i], addr, 4);
73  if (result != DOCA_SUCCESS) {
74  memset(&addr, 0, sizeof(addr));
75  } else {
76  /* query PTP capability can be queried if IPv4 address is configured */
77  result = doca_rmax_get_ptp_clock_supported(devinfo[i]);
78  if (result != DOCA_SUCCESS) {
80  DOCA_LOG_ERR("Failed to get PTP clock capability: %s",
82  return result;
83  }
84  has_ptp = false;
85  } else
86  has_ptp = true;
87  }
88 
89  DOCA_LOG_INFO("PCI address: %s, IP: %d.%d.%d.%d, PTP: %c",
90  dev_pci_addr,
91  addr[0],
92  addr[1],
93  addr[2],
94  addr[3],
95  (has_ptp) ? 'y' : 'n');
96  }
97 
99  if (result != DOCA_SUCCESS) {
100  DOCA_LOG_ERR("Failed to destroy devices info list: %s", doca_error_get_descr(result));
101  return result;
102  }
103 
104  result = doca_rmax_release();
105  if (result != DOCA_SUCCESS) {
106  DOCA_LOG_ERR("Failed to destroy the DOCA Rivermax: %s", doca_error_get_descr(result));
107  return result;
108  }
109 
110  return result;
111 }
int32_t result
uintptr_t addr
DOCA_STABLE doca_error_t doca_devinfo_create_list(struct doca_devinfo ***dev_list, uint32_t *nb_devs)
Creates list of all available local devices.
DOCA_STABLE doca_error_t doca_devinfo_destroy_list(struct doca_devinfo **dev_list)
Destroy list of local device info structures.
#define DOCA_DEVINFO_PCI_ADDR_SIZE
Buffer size to hold PCI BDF format: "XXXX:XX:XX.X". Including a null terminator.
Definition: doca_dev.h:313
DOCA_STABLE doca_error_t doca_devinfo_get_ipv4_addr(const struct doca_devinfo *devinfo, uint8_t *ipv4_addr, uint32_t size)
Get the IPv4 address of a DOCA devinfo.
DOCA_STABLE doca_error_t doca_devinfo_get_pci_addr_str(const struct doca_devinfo *devinfo, char *pci_addr_str)
Get the PCI address of a DOCA devinfo.
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_SUPPORTED
Definition: doca_error.h:42
@ 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
DOCA_LOG_REGISTER(RMAX_LIST_DEVICES)
doca_error_t rmax_list_devices(void)