gnu: datefudge: Update to 1.23.
[jackhill/guix/guix.git] / gnu / packages / patches / ucx-tcp-iface-ioctl.patch
1 Since /sys is unavailable in build environments, the list of available
2 TCP network interfaces cannot be obtained via /sys/class/net. This patch
3 provides alternative code that uses the SIOCGIFCONF ioctl to get the
4 names of the available TCP network interfaces.
5
6 diff --git a/src/uct/tcp/tcp_iface.c b/src/uct/tcp/tcp_iface.c
7 index 81ad459..10024a6 100644
8 --- a/src/uct/tcp/tcp_iface.c
9 +++ b/src/uct/tcp/tcp_iface.c
10 @@ -12,6 +12,8 @@
11 #include <sys/poll.h>
12 #include <netinet/tcp.h>
13 #include <dirent.h>
14 +#include <net/if.h>
15 +#include <sys/ioctl.h>
16
17 static ucs_config_field_t uct_tcp_iface_config_table[] = {
18 {"", "MAX_SHORT=8k", NULL,
19 @@ -483,6 +485,70 @@ static UCS_CLASS_DEFINE_NEW_FUNC(uct_tcp_iface_t, uct_iface_t, uct_md_h,
20 uct_worker_h, const uct_iface_params_t*,
21 const uct_iface_config_t*);
22
23 +/* Fetch information about available network devices through an ioctl. */
24 +static ucs_status_t query_devices_ioctl(uct_md_h md,
25 + uct_tl_resource_desc_t **resource_p,
26 + unsigned *num_resources_p)
27 +{
28 + int sock, err, i;
29 + uct_tl_resource_desc_t *resources, *tmp;
30 + unsigned num_resources;
31 + ucs_status_t status;
32 + struct ifconf conf;
33 + struct ifreq reqs[10];
34 +
35 + conf.ifc_len = sizeof reqs;
36 + conf.ifc_req = reqs;
37 +
38 + sock = socket(SOCK_STREAM, AF_INET, 0);
39 + if (sock < 0) {
40 + ucs_error("socket(2) failed: %m");
41 + status = UCS_ERR_IO_ERROR;
42 + goto out;
43 + }
44 +
45 + err = ioctl(sock, SIOCGIFCONF, &conf);
46 + if (err < 0) {
47 + ucs_error("SIOCGIFCONF ioctl failed: %m");
48 + status = UCS_ERR_IO_ERROR;
49 + goto out;
50 + }
51 +
52 + resources = NULL;
53 + num_resources = 0;
54 + for (i = 0; i < conf.ifc_len / sizeof(struct ifreq); i++) {
55 + const char *name = reqs[i].ifr_name;
56 +
57 + if (!ucs_netif_is_active(name)) {
58 + continue;
59 + }
60 +
61 + tmp = ucs_realloc(resources, sizeof(*resources) * (num_resources + 1),
62 + "tcp resources");
63 + if (tmp == NULL) {
64 + ucs_free(resources);
65 + status = UCS_ERR_NO_MEMORY;
66 + goto out;
67 + }
68 + resources = tmp;
69 +
70 + ucs_snprintf_zero(resources[i].tl_name, sizeof(resources[i].tl_name),
71 + "%s", UCT_TCP_NAME);
72 + ucs_snprintf_zero(resources[i].dev_name, sizeof(resources[i].dev_name),
73 + "%s", name);
74 + resources[i].dev_type = UCT_DEVICE_TYPE_NET;
75 + ++num_resources;
76 + }
77 +
78 + *num_resources_p = num_resources;
79 + *resource_p = resources;
80 + status = UCS_OK;
81 +
82 +out:
83 + if (sock >= 0) close(sock);
84 + return status;
85 +}
86 +
87 static ucs_status_t uct_tcp_query_tl_resources(uct_md_h md,
88 uct_tl_resource_desc_t **resource_p,
89 unsigned *num_resources_p)
90 @@ -496,9 +562,9 @@ static ucs_status_t uct_tcp_query_tl_resources(uct_md_h md,
91
92 dir = opendir(netdev_dir);
93 if (dir == NULL) {
94 - ucs_error("opendir(%s) failed: %m", netdev_dir);
95 - status = UCS_ERR_IO_ERROR;
96 - goto out;
97 + /* When /sys is unavailable, as can be the case in a container,
98 + * resort to a good old 'ioctl'. */
99 + return query_devices_ioctl(md, resource_p, num_resources_p);
100 }
101
102 resources = NULL;
103 @@ -543,6 +609,5 @@ static ucs_status_t uct_tcp_query_tl_resources(uct_md_h md,
104
105 out_closedir:
106 closedir(dir);
107 -out:
108 return status;
109 }