Debian Codegraph @

prov/verbs/src/windows/addrinfo.c

raw
1 /*
2 * Copyright (c) 2022 Canon Medical Research USA, Inc. All rights reserved.
3 * Copyright (c) 2010-2014 Intel Corporation. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 * $Id: cm.c 3453 2005-09-15 21:43:21Z sean.hefty $
34 */
35
36 /* Adapted for use on Windows after being taken from
37 * URL: https://github.com/linux-rdma/rdma-core.git
38 * commit: 4834dd4a0848a71b98a48c71deb733500d4d5f3a
39 * path: librdmacm/addrinfo.c
40 */
41
42 #include <config.h>
43 #include <asm/types.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netdb.h>
47 #include <unistd.h>
48
49 #include <rdma/rdma_cma.h>
50 #include <infiniband/ib.h>
51
52 static inline int ERR(int err)
53 {
54 errno = err;
55 return -1;
56 }
57
58 __be16 ucma_get_port(struct sockaddr *addr)
59 {
60 switch (addr->sa_family) {
61 case AF_INET:
62 return ((struct sockaddr_in *)addr)->sin_port;
63 case AF_INET6:
64 return ((struct sockaddr_in6 *)addr)->sin6_port;
65 case AF_IB:
66 return htons((uint16_t)ntohll(
67 ((struct sockaddr_ib *)addr)->sib_sid));
68 default:
69 return 0;
70 }
71 }
72
73 static struct rdma_addrinfo nohints;
74
75 static void ucma_convert_to_ai(struct addrinfo *ai,
76 const struct rdma_addrinfo *rai)
77 {
78 memset(ai, 0, sizeof(*ai));
79 if (rai->ai_flags & RAI_PASSIVE)
80 ai->ai_flags = AI_PASSIVE;
81 if (rai->ai_flags & RAI_NUMERICHOST)
82 ai->ai_flags |= AI_NUMERICHOST;
83 if (rai->ai_family != AF_IB)
84 ai->ai_family = rai->ai_family;
85
86 switch (rai->ai_qp_type) {
87 case IBV_QPT_RC:
88 case IBV_QPT_UC:
89 case IBV_QPT_XRC_SEND:
90 case IBV_QPT_XRC_RECV:
91 ai->ai_socktype = SOCK_STREAM;
92 break;
93 case IBV_QPT_UD:
94 ai->ai_socktype = SOCK_DGRAM;
95 break;
96 }
97
98 switch (rai->ai_port_space) {
99 case RDMA_PS_TCP:
100 ai->ai_protocol = IPPROTO_TCP;
101 break;
102 case RDMA_PS_IPOIB:
103 case RDMA_PS_UDP:
104 ai->ai_protocol = IPPROTO_UDP;
105 break;
106 case RDMA_PS_IB:
107 if (ai->ai_socktype == SOCK_STREAM)
108 ai->ai_protocol = IPPROTO_TCP;
109 else if (ai->ai_socktype == SOCK_DGRAM)
110 ai->ai_protocol = IPPROTO_UDP;
111 break;
112 }
113 }
114
115 static int ucma_copy_addr(struct sockaddr **dst, socklen_t *dst_len,
116 struct sockaddr *src, socklen_t src_len)
117 {
118 *dst = malloc(src_len);
119 if (!(*dst))
120 return ERR(ENOMEM);
121
122 memcpy(*dst, src, src_len);
123 *dst_len = src_len;
124 return 0;
125 }
126
127 void ucma_set_sid(enum rdma_port_space ps, struct sockaddr *addr,
128 struct sockaddr_ib *sib)
129 {
130 __be16 port;
131
132 port = addr ? ucma_get_port(addr) : 0;
133 sib->sib_sid = htonll(((uint64_t)ps << 16) + ntohs(port));
134
135 if (ps)
136 sib->sib_sid_mask = htonll(RDMA_IB_IP_PS_MASK);
137 if (port)
138 sib->sib_sid_mask |= htonll(RDMA_IB_IP_PORT_MASK);
139 }
140
141 /*
142 * IPv6 address structure
143 */
144 struct __in6_addr {
145 union {
146 __u8 u6_addr8[16];
147 __be16 u6_addr16[8];
148 __be32 u6_addr32[4];
149 } in6_u;
150 #define s6_addr32 in6_u.u6_addr32
151 };
152
153 struct __sockaddr_in6 {
154 unsigned short int sin6_family; /* AF_INET6 */
155 __be16 sin6_port; /* Transport layer port # */
156 __be32 sin6_flowinfo; /* IPv6 flow information */
157 struct __in6_addr sin6_addr; /* IPv6 address */
158 __u32 sin6_scope_id; /* scope id (new in RFC2553) */
159 };
160
161 static int ucma_convert_in6(int ps, struct sockaddr_ib **dst,
162 socklen_t *dst_len, struct __sockaddr_in6 *src,
163 socklen_t src_len)
164 {
165 *dst = calloc(1, sizeof(struct sockaddr_ib));
166 if (!(*dst))
167 return ERR(ENOMEM);
168
169 (*dst)->sib_family = AF_IB;
170 (*dst)->sib_pkey = htons(0xFFFF);
171 (*dst)->sib_flowinfo = src->sin6_flowinfo;
172 ib_addr_set(&(*dst)->sib_addr, src->sin6_addr.s6_addr32[0],
173 src->sin6_addr.s6_addr32[1], src->sin6_addr.s6_addr32[2],
174 src->sin6_addr.s6_addr32[3]);
175 ucma_set_sid(ps, (struct sockaddr *)src, *dst);
176 (*dst)->sib_scope_id = src->sin6_scope_id;
177
178 *dst_len = sizeof(struct sockaddr_ib);
179 return 0;
180 }
181
182 static int ucma_convert_to_rai(struct rdma_addrinfo *rai,
183 const struct rdma_addrinfo *hints,
184 const struct addrinfo *ai)
185 {
186 int ret;
187
188 if (hints->ai_qp_type) {
189 rai->ai_qp_type = hints->ai_qp_type;
190 } else {
191 switch (ai->ai_socktype) {
192 case SOCK_STREAM:
193 rai->ai_qp_type = IBV_QPT_RC;
194 break;
195 case SOCK_DGRAM:
196 rai->ai_qp_type = IBV_QPT_UD;
197 break;
198 }
199 }
200
201 if (hints->ai_port_space) {
202 rai->ai_port_space = hints->ai_port_space;
203 } else {
204 switch (ai->ai_protocol) {
205 case IPPROTO_TCP:
206 rai->ai_port_space = RDMA_PS_TCP;
207 break;
208 case IPPROTO_UDP:
209 rai->ai_port_space = RDMA_PS_UDP;
210 break;
211 }
212 }
213
214 if ((ai->ai_flags & AI_PASSIVE) ||
215 (hints->ai_flags &
216 RAI_PASSIVE)) { // Windows getaddrinfo doesn't set AI_PASSIVE flag on output.
217 rai->ai_flags = RAI_PASSIVE;
218 if (ai->ai_canonname)
219 rai->ai_src_canonname = strdup(ai->ai_canonname);
220
221 if ((hints->ai_flags & RAI_FAMILY) &&
222 (hints->ai_family == AF_IB) &&
223 (hints->ai_flags & RAI_NUMERICHOST)) {
224 rai->ai_family = AF_IB;
225 ret = ucma_convert_in6(
226 rai->ai_port_space,
227 (struct sockaddr_ib **)&rai->ai_src_addr,
228 &rai->ai_src_len,
229 (struct __sockaddr_in6 *)ai->ai_addr,
230 (socklen_t)ai->ai_addrlen);
231 } else {
232 rai->ai_family = ai->ai_family;
233 ret = ucma_copy_addr(&rai->ai_src_addr,
234 &rai->ai_src_len, ai->ai_addr,
235 (socklen_t)ai->ai_addrlen);
236 }
237 } else {
238 if (ai->ai_canonname)
239 rai->ai_dst_canonname = strdup(ai->ai_canonname);
240
241 if ((hints->ai_flags & RAI_FAMILY) &&
242 (hints->ai_family == AF_IB) &&
243 (hints->ai_flags & RAI_NUMERICHOST)) {
244 rai->ai_family = AF_IB;
245 ret = ucma_convert_in6(
246 rai->ai_port_space,
247 (struct sockaddr_ib **)&rai->ai_dst_addr,
248 &rai->ai_dst_len,
249 (struct __sockaddr_in6 *)ai->ai_addr,
250 (socklen_t)ai->ai_addrlen);
251 } else {
252 rai->ai_family = ai->ai_family;
253 ret = ucma_copy_addr(&rai->ai_dst_addr,
254 &rai->ai_dst_len, ai->ai_addr,
255 (socklen_t)ai->ai_addrlen);
256 }
257 }
258 return ret;
259 }
260
261 static int ucma_getaddrinfo(const char *node, const char *service,
262 const struct rdma_addrinfo *hints,
263 struct rdma_addrinfo *rai)
264 {
265 struct addrinfo ai_hints;
266 struct addrinfo *ai;
267 int ret;
268
269 if (hints != &nohints) {
270 ucma_convert_to_ai(&ai_hints, hints);
271 ret = getaddrinfo(node, service, &ai_hints, &ai);
272 } else {
273 ret = getaddrinfo(node, service, NULL, &ai);
274 }
275 if (ret)
276 return ret;
277
278 ret = ucma_convert_to_rai(rai, hints, ai);
279 freeaddrinfo(ai);
280 return ret;
281 }
282
283 int rdma_getaddrinfo(const char *node, const char *service,
284 const struct rdma_addrinfo *hints,
285 struct rdma_addrinfo **res)
286 {
287 struct rdma_addrinfo *rai;
288 int ret = 0;
289
290 if (!service && !node && !hints)
291 return ERR(EINVAL);
292
293 rai = calloc(1, sizeof(*rai));
294 if (!rai)
295 return ERR(ENOMEM);
296
297 if (!hints)
298 hints = &nohints;
299
300 if (node || service) {
301 ret = ucma_getaddrinfo(node, service, hints, rai);
302 } else {
303 rai->ai_flags = hints->ai_flags;
304 rai->ai_family = hints->ai_family;
305 rai->ai_qp_type = hints->ai_qp_type;
306 rai->ai_port_space = hints->ai_port_space;
307 if (hints->ai_dst_len) {
308 ret = ucma_copy_addr(&rai->ai_dst_addr,
309 &rai->ai_dst_len,
310 hints->ai_dst_addr,
311 hints->ai_dst_len);
312 }
313 }
314 if (ret)
315 goto err;
316
317 if (!rai->ai_src_len && hints->ai_src_len) {
318 ret = ucma_copy_addr(&rai->ai_src_addr, &rai->ai_src_len,
319 hints->ai_src_addr, hints->ai_src_len);
320 if (ret)
321 goto err;
322 }
323
324 *res = rai;
325 return 0;
326
327 err:
328 rdma_freeaddrinfo(rai);
329 return ret;
330 }
331
332 void rdma_freeaddrinfo(struct rdma_addrinfo *res)
333 {
334 struct rdma_addrinfo *rai;
335
336 while (res) {
337 rai = res;
338 res = res->ai_next;
339
340 if (rai->ai_connect)
341 free(rai->ai_connect);
342
343 if (rai->ai_route)
344 free(rai->ai_route);
345
346 if (rai->ai_src_canonname)
347 free(rai->ai_src_canonname);
348
349 if (rai->ai_dst_canonname)
350 free(rai->ai_dst_canonname);
351
352 if (rai->ai_src_addr)
353 free(rai->ai_src_addr);
354
355 if (rai->ai_dst_addr)
356 free(rai->ai_dst_addr);
357
358 free(rai);
359 }
360 }