unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 2a0e4ce13899d21ad6ea9886a0184964e6104b76 3978 bytes (raw)
name: packages/patches/ucx-tcp-iface-ioctl.patch 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 
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.

Initially submitted at <https://github.com/openucx/ucx/pull/4462>.

diff --git a/src/uct/tcp/tcp_iface.c b/src/uct/tcp/tcp_iface.c
index 6a6cd34fa..af32bb2e9 100644
--- a/src/uct/tcp/tcp_iface.c
+++ b/src/uct/tcp/tcp_iface.c
@@ -18,6 +18,8 @@
 #include <netinet/tcp.h>
 #include <dirent.h>
 #include <float.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
 
 #define UCT_TCP_IFACE_NETDEV_DIR "/sys/class/net"
 
@@ -875,6 +877,85 @@ static UCS_CLASS_DEFINE_NEW_FUNC(uct_tcp_iface_t, uct_iface_t, uct_md_h,
                                  uct_worker_h, const uct_iface_params_t*,
                                  const uct_iface_config_t*);
 
+/* Fetch information about available network devices through an ioctl.  */
+static ucs_status_t uct_tcp_query_devices_ioctl(uct_md_h md,
+                                                uct_tl_device_resource_t **devices_p,
+                                                unsigned *num_devices_p)
+{
+    int sock, err, i;
+    uct_tl_device_resource_t *devices, *tmp;
+    unsigned num_devices;
+    ucs_status_t status;
+    struct ifconf conf;
+
+    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;
+    }
+
+    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;
+    }
+
+    err = ioctl(sock, SIOCGIFCONF, &conf);
+    if (err < 0) {
+        ucs_error("ioctl(SIOCGIFCONF) failed: %m");
+        status = UCS_ERR_IO_ERROR;
+        goto out_free;
+    }
+
+    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;
+
+        if (!ucs_netif_is_active(name, family)) {
+            continue;
+        }
+
+        tmp = ucs_realloc(devices, sizeof(*devices) * (num_devices + 1),
+                          "tcp devices");
+        if (tmp == NULL) {
+            ucs_free(devices);
+            status = UCS_ERR_NO_MEMORY;
+            goto out_free;
+        }
+        devices = tmp;
+
+        ucs_snprintf_zero(devices[num_devices].name,
+                          sizeof(devices[num_devices].name),
+                          "%s", name);
+        devices[num_devices].type = UCT_DEVICE_TYPE_NET;
+        ++num_devices;
+    }
+
+    *num_devices_p = num_devices;
+    *devices_p     = devices;
+    status         = UCS_OK;
+
+out_free:
+    ucs_free(conf.ifc_req);
+out:
+    if (sock >= 0) {
+        close(sock);
+    }
+    return status;
+}
+
 ucs_status_t uct_tcp_query_devices(uct_md_h md,
                                    uct_tl_device_resource_t **devices_p,
                                    unsigned *num_devices_p)
@@ -893,9 +974,9 @@ ucs_status_t uct_tcp_query_devices(uct_md_h md,
 
     dir = opendir(UCT_TCP_IFACE_NETDEV_DIR);
     if (dir == NULL) {
-        ucs_error("opendir(%s) failed: %m", UCT_TCP_IFACE_NETDEV_DIR);
-        status = UCS_ERR_IO_ERROR;
-        goto out;
+        /* 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);
     }
 
     devices     = NULL;
@@ -963,7 +1044,6 @@ ucs_status_t uct_tcp_query_devices(uct_md_h md,
 
 out_closedir:
     closedir(dir);
-out:
     return status;
 }
 

debug log:

solving 2a0e4ce13899d21ad6ea9886a0184964e6104b76 ...
found 2a0e4ce13899d21ad6ea9886a0184964e6104b76 in https://git.savannah.gnu.org/cgit/guix.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).