summaryrefslogtreecommitdiffstats
path: root/rpc/rpc-transport/rdma/src/name.c
blob: 3718cd7648e29baa8a1b8c4eb6d5687cf93d7f85 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>

#ifdef CLIENT_PORT_CEILING
#undef CLIENT_PORT_CEILING
#endif

#define CLIENT_PORT_CEILING 1024

#ifndef AF_INET_SDP
#define AF_INET_SDP 27
#endif

#include "rpc-transport.h"
#include "rdma.h"

int32_t
gf_resolve_ip6 (const char *hostname,
                uint16_t port,
                int family,
                void **dnscache,
                struct addrinfo **addr_info);

static int32_t
af_inet_bind_to_port_lt_ceiling (int fd, struct sockaddr *sockaddr,
                                 socklen_t sockaddr_len, int ceiling)
{
        int32_t ret = -1;
        /*  struct sockaddr_in sin = {0, }; */
        uint16_t port = ceiling - 1;

        while (port)
        {
                switch (sockaddr->sa_family)
                {
                case AF_INET6:
                        ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons (port);
                        break;

                case AF_INET_SDP:
                case AF_INET:
                        ((struct sockaddr_in *)sockaddr)->sin_port = htons (port);
                        break;
                }

                ret = bind (fd, sockaddr, sockaddr_len);

                if (ret == 0)
                        break;

                if (ret == -1 && errno == EACCES)
                        break;

                port--;
        }

        return ret;
}

static int32_t
af_unix_client_bind (rpc_transport_t *this,
                     struct sockaddr *sockaddr,
                     socklen_t sockaddr_len,
                     int sock)
{
        data_t *path_data = NULL;
        struct sockaddr_un *addr = NULL;
        int32_t ret = -1;

        path_data = dict_get (this->options,
                              "transport.rdma.bind-path");
        if (path_data) {
                char *path = data_to_str (path_data);
                if (!path || strlen (path) > UNIX_PATH_MAX) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "transport.rdma.bind-path not specified "
                                "for unix socket, letting connect to assign "
                                "default value");
                        goto err;
                }

                addr = (struct sockaddr_un *) sockaddr;
                strcpy (addr->sun_path, path);
                ret = bind (sock, (struct sockaddr *)addr, sockaddr_len);
                if (ret == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "cannot bind to unix-domain socket %d (%s)",
                                sock, strerror (errno));
                        goto err;
                }
        }

err:
        return ret;
}

static int32_t
client_fill_address_family (rpc_transport_t *this, struct sockaddr *sockaddr)
{
        data_t *address_family_data = NULL;

        address_family_data = dict_get (this->options,
                                        "transport.address-family");
        if (!address_family_data) {
                data_t *remote_host_data = NULL, *connect_path_data = NULL;
                remote_host_data = dict_get (this->options, "remote-host");
                connect_path_data = dict_get (this->options,
                                              "transport.rdma.connect-path");

                if (!(remote_host_data || connect_path_data) ||
                    (remote_host_data && connect_path_data)) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "address-family not specified and not able to "
                                "determine the same from other options "
                                "(remote-host:%s and connect-path:%s)",
                                data_to_str (remote_host_data),
                                data_to_str (connect_path_data));
                        return -1;
                }

                if (remote_host_data) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "address-family not specified, guessing it "
                                "to be inet/inet6");
                        sockaddr->sa_family = AF_UNSPEC;
                } else {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "address-family not specified, guessing it "
                                "to be unix");
                        sockaddr->sa_family = AF_UNIX;
                }

        } else {
                char *address_family = data_to_str (address_family_data);
                if (!strcasecmp (address_family, "unix")) {
                        sockaddr->sa_family = AF_UNIX;
                } else if (!strcasecmp (address_family, "inet")) {
                        sockaddr->sa_family = AF_INET;
                } else if (!strcasecmp (address_family, "inet6")) {
                        sockaddr->sa_family = AF_INET6;
                } else if (!strcasecmp (address_family, "inet-sdp")) {
                        sockaddr->sa_family = AF_INET_SDP;
                } else {
                        gf_log (this->name, GF_LOG_ERROR,
                                "unknown address-family (%s) specified",
                                address_family);
                        sockaddr->sa_family = AF_UNSPEC;
                        return -1;
                }
        }

        return 0;
}

static int32_t
af_inet_client_get_remote_sockaddr (rpc_transport_t *this,
                                    struct sockaddr *sockaddr,
                                    socklen_t *sockaddr_len,
                                    int16_t remote_port)
{
        dict_t *options = this->options;
        data_t *remote_host_data = NULL;
        data_t *remote_port_data = NULL;
        char *remote_host = NULL;
        struct addrinfo *addr_info = NULL;
        int32_t ret = 0;

        remote_host_data = dict_get (options, "remote-host");
        if (remote_host_data == NULL)
        {
                gf_log (this->name, GF_LOG_ERROR,
                        "option remote-host missing in volume %s",
                        this->name);
                ret = -1;
                goto err;
        }

        remote_host = data_to_str (remote_host_data);
        if (remote_host == NULL)
        {
                gf_log (this->name, GF_LOG_ERROR,
                        "option remote-host has data NULL in volume %s",
                        this->name);
                ret = -1;
                goto err;
        }

        if (remote_port == 0) {
                remote_port_data = dict_get (options, "remote-port");
                if (remote_port_data == NULL)
                {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "option remote-port missing in volume %s. "
                                "Defaulting to %d",
                                this->name, GF_DEFAULT_RDMA_LISTEN_PORT);

                        remote_port = GF_DEFAULT_RDMA_LISTEN_PORT;
                }
                else
                {
                        remote_port = data_to_uint16 (remote_port_data);
                }
        }

        if (remote_port == -1)
        {
                gf_log (this->name, GF_LOG_ERROR,
                        "option remote-port has invalid port in volume %s",
                        this->name);
                ret = -1;
                goto err;
        }

        /* TODO: gf_resolve is a blocking call. kick in some
           non blocking dns techniques */
        ret = gf_resolve_ip6 (remote_host, remote_port,
                              sockaddr->sa_family,
                              &this->dnscache, &addr_info);
        if (ret == -1) {
                gf_log (this->name, GF_LOG_ERROR,
                        "DNS resolution failed on host %s", remote_host);
                goto err;
        }

        memcpy (sockaddr, addr_info->ai_addr, addr_info->ai_addrlen);
        *sockaddr_len = addr_info->ai_addrlen;

err:
        return ret;
}

static int32_t
af_unix_client_get_remote_sockaddr (rpc_transport_t *this,
                                    struct sockaddr *sockaddr,
                                    socklen_t *sockaddr_len)
{
        struct sockaddr_un *sockaddr_un = NULL;
        char *connect_path = NULL;
        data_t *connect_path_data = NULL;
        int32_t ret = 0;

        connect_path_data = dict_get (this->options,
                                      "transport.rdma.connect-path");
        if (!connect_path_data) {
                gf_log (this->name, GF_LOG_ERROR,
                        "option transport.rdma.connect-path not "
                        "specified for address-family unix");
                ret = -1;
                goto err;
        }

        connect_path = data_to_str (connect_path_data);
        if (!connect_path) {
                gf_log (this->name, GF_LOG_ERROR,
                        "connect-path is null-string");
                ret = -1;
                goto err;
        }

        if (strlen (connect_path) > UNIX_PATH_MAX) {
                gf_log (this->name, GF_LOG_ERROR,
                        "connect-path value length %"GF_PRI_SIZET" > "
                        "%d octets", strlen (connect_path), UNIX_PATH_MAX);
                ret = -1;
                goto err;
        }

        gf_log (this->name,
                GF_LOG_DEBUG,
                "using connect-path %s", connect_path);
        sockaddr_un = (struct sockaddr_un *)sockaddr;
        strcpy (sockaddr_un->sun_path, connect_path);
        *sockaddr_len = sizeof (struct sockaddr_un);

err:
        return ret;
}

static int32_t
af_unix_server_get_local_sockaddr (rpc_transport_t *this,
                                   struct sockaddr *addr,
                                   socklen_t *addr_len)
{
        data_t *listen_path_data = NULL;
        char *listen_path = NULL;
        int32_t ret = 0;
        struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr;


        listen_path_data = dict_get (this->options,
                                     "transport.rdma.listen-path");
        if (!listen_path_data) {
                gf_log (this->name, GF_LOG_ERROR,
                        "missing option listen-path");
                ret = -1;
                goto err;
        }

        listen_path = data_to_str (listen_path_data);

#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
#endif

        if (strlen (listen_path) > UNIX_PATH_MAX) {
                gf_log (this->name, GF_LOG_ERROR,
                        "option listen-path has value length %"GF_PRI_SIZET" > %d",
                        strlen (listen_path), UNIX_PATH_MAX);
                ret = -1;
                goto err;
        }

        sunaddr->sun_family = AF_UNIX;
        strcpy (sunaddr->sun_path, listen_path);
        *addr_len = sizeof (struct sockaddr_un);

err:
        return ret;
}

static int32_t
af_inet_server_get_local_sockaddr (rpc_transport_t *this,
                                   struct sockaddr *addr,
                                   socklen_t *addr_len)
{
        struct addrinfo hints, *res = 0;
        data_t *listen_port_data = NULL, *listen_host_data = NULL;
        uint16_t listen_port = -1;
        char service[NI_MAXSERV], *listen_host = NULL;
        dict_t *options = NULL;
        int32_t ret = 0;

        options = this->options;

        listen_port_data = dict_get (options, "transport.rdma.listen-port");
        listen_host_data = dict_get (options,
                                     "transport.rdma.bind-address");

        if (listen_port_data) {
                listen_port = data_to_uint16 (listen_port_data);
        } else {
                if (addr->sa_family == AF_INET6) {
                        struct sockaddr_in6 *in = (struct sockaddr_in6 *) addr;
                        in->sin6_addr = in6addr_any;
                        in->sin6_port = htons(listen_port);
                        *addr_len = sizeof(struct sockaddr_in6);
                        goto out;
                } else if (addr->sa_family == AF_INET) {
                        struct sockaddr_in *in = (struct sockaddr_in *) addr;
                        in->sin_addr.s_addr = htonl(INADDR_ANY);
                        in->sin_port = htons(listen_port);
                        *addr_len = sizeof(struct sockaddr_in);
                        goto out;
                }
        }

        if (listen_port == (uint16_t) -1)
                listen_port = GF_DEFAULT_RDMA_LISTEN_PORT;


        if (listen_host_data) {
                listen_host = data_to_str (listen_host_data);
        }

        memset (service, 0, sizeof (service));
        sprintf (service, "%d", listen_port);

        memset (&hints, 0, sizeof (hints));
        hints.ai_family = addr->sa_family;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags    = AI_ADDRCONFIG | AI_PASSIVE;

        ret = getaddrinfo(listen_host, service, &hints, &res);
        if (ret != 0) {
                gf_log (this->name, GF_LOG_ERROR,
                        "getaddrinfo failed for host %s, service %s (%s)",
                        listen_host, service, gai_strerror (ret));
                ret = -1;
                goto out;
        }

        memcpy (addr, res->ai_addr, res->ai_addrlen);
        *addr_len = res->ai_addrlen;

        freeaddrinfo (res);

out:
        return ret;
}

int32_t
gf_rdma_client_bind (rpc_transport_t *this,
                     struct sockaddr *sockaddr,
                     socklen_t *sockaddr_len,
                     int sock)
{
        int ret = 0;

        *sockaddr_len = sizeof (struct sockaddr_in6);
        switch (sockaddr->sa_family)
        {
        case AF_INET_SDP:
        case AF_INET:
                *sockaddr_len = sizeof (struct sockaddr_in);

        case AF_INET6:
                ret = af_inet_bind_to_port_lt_ceiling (sock, sockaddr,
                                                       *sockaddr_len,
                                                       CLIENT_PORT_CEILING);
                if (ret == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "cannot bind inet socket (%d) to port "
                                "less than %d (%s)",
                                sock, CLIENT_PORT_CEILING, strerror (errno));
                        ret = 0;
                }
                break;

        case AF_UNIX:
                *sockaddr_len = sizeof (struct sockaddr_un);
                ret = af_unix_client_bind (this, (struct sockaddr *)sockaddr,
                                           *sockaddr_len, sock);
                break;

        default:
                gf_log (this->name, GF_LOG_ERROR,
                        "unknown address family %d", sockaddr->sa_family);
                ret = -1;
                break;
        }

        return ret;
}

int32_t
gf_rdma_client_get_remote_sockaddr (rpc_transport_t *this,
                                    struct sockaddr *sockaddr,
                                    socklen_t *sockaddr_len,
                                    int16_t remote_port)
{
        int32_t ret = 0;
        char is_inet_sdp = 0;

        ret = client_fill_address_family (this, sockaddr);
        if (ret) {
                ret = -1;
                goto err;
        }

        switch (sockaddr->sa_family)
        {
        case AF_INET_SDP:
                sockaddr->sa_family = AF_INET;
                is_inet_sdp = 1;

        case AF_INET:
        case AF_INET6:
        case AF_UNSPEC:
                ret = af_inet_client_get_remote_sockaddr (this,
                                                          sockaddr,
                                                          sockaddr_len,
                                                          remote_port);

                if (is_inet_sdp) {
                        sockaddr->sa_family = AF_INET_SDP;
                }

                break;

        case AF_UNIX:
                ret = af_unix_client_get_remote_sockaddr (this,
                                                          sockaddr,
                                                          sockaddr_len);
                break;

        default:
                gf_log (this->name, GF_LOG_ERROR,
                        "unknown address-family %d", sockaddr->sa_family);
                ret = -1;
        }

err:
        return ret;
}

int32_t
gf_rdma_server_get_local_sockaddr (rpc_transport_t *this,
                                   struct sockaddr *addr,
                                   socklen_t *addr_len)
{
        data_t *address_family_data = NULL;
        int32_t ret = 0;
        char is_inet_sdp = 0;

        address_family_data = dict_get (this->options,
                                        "transport.address-family");
        if (address_family_data) {
                char *address_family = NULL;
                address_family = data_to_str (address_family_data);

                if (!strcasecmp (address_family, "inet")) {
                        addr->sa_family = AF_INET;
                } else if (!strcasecmp (address_family, "inet6")) {
                        addr->sa_family = AF_INET6;
                } else if (!strcasecmp (address_family, "inet-sdp")) {
                        addr->sa_family = AF_INET_SDP;
                } else if (!strcasecmp (address_family, "unix")) {
                        addr->sa_family = AF_UNIX;
                } else {
                        gf_log (this->name, GF_LOG_ERROR,
                                "unknown address family (%s) specified",
                                address_family);
                        addr->sa_family = AF_UNSPEC;
                        ret = -1;
                        goto err;
                }
        } else {
                gf_log (this->name, GF_LOG_DEBUG,
                        "option address-family not specified, defaulting "
                        "to inet");
                addr->sa_family = AF_INET;
        }

        switch (addr->sa_family)
        {
        case AF_INET_SDP:
                is_inet_sdp = 1;
                addr->sa_family = AF_INET;

        case AF_INET:
        case AF_INET6:
        case AF_UNSPEC:
                ret = af_inet_server_get_local_sockaddr (this, addr, addr_len);
                if (is_inet_sdp && !ret) {
                        addr->sa_family = AF_INET_SDP;
                }
                break;

        case AF_UNIX:
                ret = af_unix_server_get_local_sockaddr (this, addr, addr_len);
                break;
        }

err:
        return ret;
}

int32_t
fill_inet6_inet_identifiers (rpc_transport_t *this, struct sockaddr_storage *addr,
                             int32_t addr_len, char *identifier)
{
        int32_t ret = 0, tmpaddr_len = 0;
        char service[NI_MAXSERV], host[NI_MAXHOST];
        union gf_sock_union sock_union;

        memset (&sock_union, 0, sizeof (sock_union));
        sock_union.storage = *addr;
        tmpaddr_len = addr_len;

        if (sock_union.sa.sa_family == AF_INET6) {
                int32_t one_to_four, four_to_eight, twelve_to_sixteen;
                int16_t eight_to_ten, ten_to_twelve;

                one_to_four = four_to_eight = twelve_to_sixteen = 0;
                eight_to_ten = ten_to_twelve = 0;

                one_to_four = sock_union.sin6.sin6_addr.s6_addr32[0];
                four_to_eight = sock_union.sin6.sin6_addr.s6_addr32[1];
#ifdef GF_SOLARIS_HOST_OS
                eight_to_ten = S6_ADDR16(sock_union.sin6.sin6_addr)[4];
#else
                eight_to_ten = sock_union.sin6.sin6_addr.s6_addr16[4];
#endif

#ifdef GF_SOLARIS_HOST_OS
                ten_to_twelve = S6_ADDR16(sock_union.sin6.sin6_addr)[5];
#else
                ten_to_twelve = sock_union.sin6.sin6_addr.s6_addr16[5];
#endif
                twelve_to_sixteen = sock_union.sin6.sin6_addr.s6_addr32[3];

                /* ipv4 mapped ipv6 address has
                   bits 0-80: 0
                   bits 80-96: 0xffff
                   bits 96-128: ipv4 address
                */

                if (one_to_four == 0 &&
                    four_to_eight == 0 &&
                    eight_to_ten == 0 &&
                    ten_to_twelve == -1) {
                        struct sockaddr_in *in_ptr = &sock_union.sin;
                        memset (&sock_union, 0, sizeof (sock_union));

                        in_ptr->sin_family = AF_INET;
                        in_ptr->sin_port = ((struct sockaddr_in6 *)addr)->sin6_port;
                        in_ptr->sin_addr.s_addr = twelve_to_sixteen;
                        tmpaddr_len = sizeof (*in_ptr);
                }
        }

        ret = getnameinfo (&sock_union.sa,
                           tmpaddr_len,
                           host, sizeof (host),
                           service, sizeof (service),
                           NI_NUMERICHOST | NI_NUMERICSERV);
        if (ret != 0) {
                gf_log (this->name,
                        GF_LOG_ERROR,
                        "getnameinfo failed (%s)", gai_strerror (ret));
        }

        sprintf (identifier, "%s:%s", host, service);

        return ret;
}

int32_t
gf_rdma_get_transport_identifiers (rpc_transport_t *this)
{
        int32_t ret = 0;
        char is_inet_sdp = 0;

        switch (((struct sockaddr *) &this->myinfo.sockaddr)->sa_family)
        {
        case AF_INET_SDP:
                is_inet_sdp = 1;
                ((struct sockaddr *) &this->peerinfo.sockaddr)->sa_family = ((struct sockaddr *) &this->myinfo.sockaddr)->sa_family = AF_INET;

        case AF_INET:
        case AF_INET6:
        {
                ret = fill_inet6_inet_identifiers (this,
                                                   &this->myinfo.sockaddr,
                                                   this->myinfo.sockaddr_len,
                                                   this->myinfo.identifier);
                if (ret == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "can't fill inet/inet6 identifier for server");
                        goto err;
                }

                ret = fill_inet6_inet_identifiers (this,
                                                   &this->peerinfo.sockaddr,
                                                   this->peerinfo.sockaddr_len,
                                                   this->peerinfo.identifier);
                if (ret == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "can't fill inet/inet6 identifier for client");
                        goto err;
                }

                if (is_inet_sdp) {
                        ((struct sockaddr *) &this->peerinfo.sockaddr)->sa_family = ((struct sockaddr *) &this->myinfo.sockaddr)->sa_family = AF_INET_SDP;
                }
        }
        break;

        case AF_UNIX:
        {
                struct sockaddr_un *sunaddr = NULL;

                sunaddr = (struct sockaddr_un *) &this->myinfo.sockaddr;
                strcpy (this->myinfo.identifier, sunaddr->sun_path);

                sunaddr = (struct sockaddr_un *) &this->peerinfo.sockaddr;
                strcpy (this->peerinfo.identifier, sunaddr->sun_path);
        }
        break;

        default:
                gf_log (this->name, GF_LOG_ERROR,
                        "unknown address family (%d)",
                        ((struct sockaddr *) &this->myinfo.sockaddr)->sa_family);
                ret = -1;
                break;
        }

err:
        return ret;
}