2021-05-22 13:47:49 +00:00
|
|
|
Since /sys is unavailable in build environments, the list of available
|
|
|
|
TCP network interfaces cannot be obtained via /sys/class/net. This patch
|
|
|
|
provides alternative code that uses the SIOCGIFCONF ioctl to get the
|
|
|
|
names of the available TCP network interfaces.
|
|
|
|
|
2023-10-16 07:09:52 +00:00
|
|
|
Initially submitted at <https://github.com/openucx/ucx/pull/4462>.
|
|
|
|
|
2021-05-22 13:47:49 +00:00
|
|
|
diff --git a/src/uct/tcp/tcp_iface.c b/src/uct/tcp/tcp_iface.c
|
2023-10-16 07:09:52 +00:00
|
|
|
index 6a6cd34fa..af32bb2e9 100644
|
2021-05-22 13:47:49 +00:00
|
|
|
--- a/src/uct/tcp/tcp_iface.c
|
|
|
|
+++ b/src/uct/tcp/tcp_iface.c
|
2023-10-16 07:09:52 +00:00
|
|
|
@@ -18,6 +18,8 @@
|
2021-05-22 13:47:49 +00:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <dirent.h>
|
2023-10-16 07:09:52 +00:00
|
|
|
#include <float.h>
|
2021-05-22 13:47:49 +00:00
|
|
|
+#include <net/if.h>
|
|
|
|
+#include <sys/ioctl.h>
|
|
|
|
|
2023-10-16 07:09:52 +00:00
|
|
|
#define UCT_TCP_IFACE_NETDEV_DIR "/sys/class/net"
|
2021-05-22 13:47:49 +00:00
|
|
|
|
2023-10-16 07:09:52 +00:00
|
|
|
@@ -875,6 +877,85 @@ static UCS_CLASS_DEFINE_NEW_FUNC(uct_tcp_iface_t, uct_iface_t, uct_md_h,
|
2021-05-22 13:47:49 +00:00
|
|
|
uct_worker_h, const uct_iface_params_t*,
|
|
|
|
const uct_iface_config_t*);
|
|
|
|
|
|
|
|
+/* Fetch information about available network devices through an ioctl. */
|
2023-10-16 07:09:52 +00:00
|
|
|
+static ucs_status_t uct_tcp_query_devices_ioctl(uct_md_h md,
|
|
|
|
+ uct_tl_device_resource_t **devices_p,
|
|
|
|
+ unsigned *num_devices_p)
|
2021-05-22 13:47:49 +00:00
|
|
|
+{
|
|
|
|
+ int sock, err, i;
|
2023-10-16 07:09:52 +00:00
|
|
|
+ uct_tl_device_resource_t *devices, *tmp;
|
|
|
|
+ unsigned num_devices;
|
2021-05-22 13:47:49 +00:00
|
|
|
+ ucs_status_t status;
|
|
|
|
+ struct ifconf conf;
|
|
|
|
+
|
2023-10-16 07:09:52 +00:00
|
|
|
+ conf.ifc_len = 0;
|
|
|
|
+ conf.ifc_req = NULL;
|
|
|
|
+
|
|
|
|
+ status = ucs_socket_create(AF_INET, SOCK_STREAM, &sock);
|
|
|
|
+ if (status != UCS_OK) {
|
|
|
|
+ goto out;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ err = ioctl(sock, SIOCGIFCONF, &conf);
|
|
|
|
+ if (err < 0) {
|
|
|
|
+ ucs_error("ioctl(SIOCGIFCONF) failed: %m");
|
|
|
|
+ status = UCS_ERR_IO_ERROR;
|
|
|
|
+ goto out;
|
|
|
|
+ }
|
2021-05-22 13:47:49 +00:00
|
|
|
+
|
2023-10-16 07:09:52 +00:00
|
|
|
+ conf.ifc_req = ucs_calloc(1, conf.ifc_len, "ifreq");
|
|
|
|
+ if (conf.ifc_req == NULL) {
|
|
|
|
+ ucs_error("memory alocation failed");
|
|
|
|
+ status = UCS_ERR_NO_MEMORY;
|
|
|
|
+ goto out;
|
2021-05-22 13:47:49 +00:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ err = ioctl(sock, SIOCGIFCONF, &conf);
|
|
|
|
+ if (err < 0) {
|
2023-10-16 07:09:52 +00:00
|
|
|
+ ucs_error("ioctl(SIOCGIFCONF) failed: %m");
|
|
|
|
+ status = UCS_ERR_IO_ERROR;
|
|
|
|
+ goto out_free;
|
2021-05-22 13:47:49 +00:00
|
|
|
+ }
|
|
|
|
+
|
2023-10-16 07:09:52 +00:00
|
|
|
+ devices = NULL;
|
|
|
|
+ num_devices = 0;
|
|
|
|
+ for (i = 0; i < (conf.ifc_len / sizeof(struct ifreq)); i++) {
|
|
|
|
+ const char *name = conf.ifc_req[i].ifr_name;
|
|
|
|
+ sa_family_t family = conf.ifc_req[i].ifr_addr.sa_family;
|
2021-05-22 13:47:49 +00:00
|
|
|
+
|
2023-10-16 07:09:52 +00:00
|
|
|
+ if (!ucs_netif_is_active(name, family)) {
|
2021-05-22 13:47:49 +00:00
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
2023-10-16 07:09:52 +00:00
|
|
|
+ tmp = ucs_realloc(devices, sizeof(*devices) * (num_devices + 1),
|
|
|
|
+ "tcp devices");
|
2021-05-22 13:47:49 +00:00
|
|
|
+ if (tmp == NULL) {
|
2023-10-16 07:09:52 +00:00
|
|
|
+ ucs_free(devices);
|
2021-05-22 13:47:49 +00:00
|
|
|
+ status = UCS_ERR_NO_MEMORY;
|
2023-10-16 07:09:52 +00:00
|
|
|
+ goto out_free;
|
2021-05-22 13:47:49 +00:00
|
|
|
+ }
|
2023-10-16 07:09:52 +00:00
|
|
|
+ devices = tmp;
|
2021-05-22 13:47:49 +00:00
|
|
|
+
|
2023-10-16 07:09:52 +00:00
|
|
|
+ ucs_snprintf_zero(devices[num_devices].name,
|
|
|
|
+ sizeof(devices[num_devices].name),
|
2021-05-22 13:47:49 +00:00
|
|
|
+ "%s", name);
|
2023-10-16 07:09:52 +00:00
|
|
|
+ devices[num_devices].type = UCT_DEVICE_TYPE_NET;
|
|
|
|
+ ++num_devices;
|
2021-05-22 13:47:49 +00:00
|
|
|
+ }
|
|
|
|
+
|
2023-10-16 07:09:52 +00:00
|
|
|
+ *num_devices_p = num_devices;
|
|
|
|
+ *devices_p = devices;
|
|
|
|
+ status = UCS_OK;
|
2021-05-22 13:47:49 +00:00
|
|
|
+
|
2023-10-16 07:09:52 +00:00
|
|
|
+out_free:
|
|
|
|
+ ucs_free(conf.ifc_req);
|
2021-05-22 13:47:49 +00:00
|
|
|
+out:
|
2023-10-16 07:09:52 +00:00
|
|
|
+ if (sock >= 0) {
|
|
|
|
+ close(sock);
|
|
|
|
+ }
|
2021-05-22 13:47:49 +00:00
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
ucs_status_t uct_tcp_query_devices(uct_md_h md,
|
|
|
|
uct_tl_device_resource_t **devices_p,
|
|
|
|
unsigned *num_devices_p)
|
2023-10-16 07:09:52 +00:00
|
|
|
@@ -893,9 +974,9 @@ ucs_status_t uct_tcp_query_devices(uct_md_h md,
|
2021-05-22 13:47:49 +00:00
|
|
|
|
2023-03-27 09:34:50 +00:00
|
|
|
dir = opendir(UCT_TCP_IFACE_NETDEV_DIR);
|
2021-05-22 13:47:49 +00:00
|
|
|
if (dir == NULL) {
|
2023-03-27 09:34:50 +00:00
|
|
|
- ucs_error("opendir(%s) failed: %m", UCT_TCP_IFACE_NETDEV_DIR);
|
2021-05-22 13:47:49 +00:00
|
|
|
- status = UCS_ERR_IO_ERROR;
|
|
|
|
- goto out;
|
2023-10-16 07:09:52 +00:00
|
|
|
+ /* When /sys is unavailable, as can be the case in a container,
|
|
|
|
+ * resort to a good old 'ioctl'. */
|
|
|
|
+ return uct_tcp_query_devices_ioctl(md, devices_p, num_devices_p);
|
2021-05-22 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
devices = NULL;
|
2023-10-16 07:09:52 +00:00
|
|
|
@@ -963,7 +1044,6 @@ ucs_status_t uct_tcp_query_devices(uct_md_h md,
|
2021-05-22 13:47:49 +00:00
|
|
|
|
|
|
|
out_closedir:
|
|
|
|
closedir(dir);
|
|
|
|
-out:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|