linux/drivers/staging/lustre/lnet/lnet/acceptor.c
<<
>>
Prefs
   1/*
   2 * GPL HEADER START
   3 *
   4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 only,
   8 * as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful, but
  11 * WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13 * General Public License version 2 for more details (a copy is included
  14 * in the LICENSE file that accompanied this code).
  15 *
  16 * You should have received a copy of the GNU General Public License
  17 * version 2 along with this program; If not, see
  18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  19 *
  20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  21 * CA 95054 USA or visit www.sun.com if you need additional information or
  22 * have any questions.
  23 *
  24 * GPL HEADER END
  25 */
  26/*
  27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  28 * Use is subject to license terms.
  29 *
  30 * Copyright (c) 2011, 2012, Intel Corporation.
  31 */
  32/*
  33 * This file is part of Lustre, http://www.lustre.org/
  34 * Lustre is a trademark of Sun Microsystems, Inc.
  35 */
  36
  37#define DEBUG_SUBSYSTEM S_LNET
  38#include "../../include/linux/lnet/lib-lnet.h"
  39
  40
  41static int   accept_port    = 988;
  42static int   accept_backlog = 127;
  43static int   accept_timeout = 5;
  44
  45static struct {
  46        int                     pta_shutdown;
  47        struct socket           *pta_sock;
  48        struct completion       pta_signal;
  49} lnet_acceptor_state;
  50
  51int
  52lnet_acceptor_port(void)
  53{
  54        return accept_port;
  55}
  56EXPORT_SYMBOL(lnet_acceptor_port);
  57
  58static inline int
  59lnet_accept_magic(__u32 magic, __u32 constant)
  60{
  61        return (magic == constant ||
  62                magic == __swab32(constant));
  63}
  64
  65static char *accept = "secure";
  66
  67module_param(accept, charp, 0444);
  68MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
  69module_param(accept_port, int, 0444);
  70MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
  71module_param(accept_backlog, int, 0444);
  72MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
  73module_param(accept_timeout, int, 0644);
  74MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
  75
  76static char *accept_type;
  77
  78static int
  79lnet_acceptor_get_tunables(void)
  80{
  81        /* Userland acceptor uses 'accept_type' instead of 'accept', due to
  82         * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
  83         * for compatibility. Hence the trick. */
  84        accept_type = accept;
  85        return 0;
  86}
  87
  88int
  89lnet_acceptor_timeout(void)
  90{
  91        return accept_timeout;
  92}
  93EXPORT_SYMBOL(lnet_acceptor_timeout);
  94
  95void
  96lnet_connect_console_error(int rc, lnet_nid_t peer_nid,
  97                           __u32 peer_ip, int peer_port)
  98{
  99        switch (rc) {
 100        /* "normal" errors */
 101        case -ECONNREFUSED:
 102                CNETERR("Connection to %s at host %pI4h on port %d was refused: check that Lustre is running on that node.\n",
 103                        libcfs_nid2str(peer_nid),
 104                        &peer_ip, peer_port);
 105                break;
 106        case -EHOSTUNREACH:
 107        case -ENETUNREACH:
 108                CNETERR("Connection to %s at host %pI4h was unreachable: the network or that node may be down, or Lustre may be misconfigured.\n",
 109                        libcfs_nid2str(peer_nid), &peer_ip);
 110                break;
 111        case -ETIMEDOUT:
 112                CNETERR("Connection to %s at host %pI4h on port %d took too long: that node may be hung or experiencing high load.\n",
 113                        libcfs_nid2str(peer_nid),
 114                        &peer_ip, peer_port);
 115                break;
 116        case -ECONNRESET:
 117                LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h on port %d was reset: is it running a compatible version of Lustre and is %s one of its NIDs?\n",
 118                                   libcfs_nid2str(peer_nid),
 119                                   &peer_ip, peer_port,
 120                                   libcfs_nid2str(peer_nid));
 121                break;
 122        case -EPROTO:
 123                LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at host %pI4h on port %d: is it running a compatible version of Lustre?\n",
 124                                   libcfs_nid2str(peer_nid),
 125                                   &peer_ip, peer_port);
 126                break;
 127        case -EADDRINUSE:
 128                LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to connect to %s at host %pI4h on port %d\n",
 129                                   libcfs_nid2str(peer_nid),
 130                                   &peer_ip, peer_port);
 131                break;
 132        default:
 133                LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s at host %pI4h on port %d\n",
 134                                   rc, libcfs_nid2str(peer_nid),
 135                                   &peer_ip, peer_port);
 136                break;
 137        }
 138}
 139EXPORT_SYMBOL(lnet_connect_console_error);
 140
 141int
 142lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
 143            __u32 local_ip, __u32 peer_ip, int peer_port)
 144{
 145        lnet_acceptor_connreq_t cr;
 146        struct socket      *sock;
 147        int                  rc;
 148        int                  port;
 149        int                  fatal;
 150
 151        CLASSERT(sizeof(cr) <= 16);         /* not too big to be on the stack */
 152
 153        for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
 154             port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
 155             --port) {
 156                /* Iterate through reserved ports. */
 157
 158                rc = libcfs_sock_connect(&sock, &fatal,
 159                                         local_ip, port,
 160                                         peer_ip, peer_port);
 161                if (rc != 0) {
 162                        if (fatal)
 163                                goto failed;
 164                        continue;
 165                }
 166
 167                CLASSERT(LNET_PROTO_ACCEPTOR_VERSION == 1);
 168
 169                cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
 170                cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
 171                cr.acr_nid     = peer_nid;
 172
 173                if (the_lnet.ln_testprotocompat != 0) {
 174                        /* single-shot proto check */
 175                        lnet_net_lock(LNET_LOCK_EX);
 176                        if ((the_lnet.ln_testprotocompat & 4) != 0) {
 177                                cr.acr_version++;
 178                                the_lnet.ln_testprotocompat &= ~4;
 179                        }
 180                        if ((the_lnet.ln_testprotocompat & 8) != 0) {
 181                                cr.acr_magic = LNET_PROTO_MAGIC;
 182                                the_lnet.ln_testprotocompat &= ~8;
 183                        }
 184                        lnet_net_unlock(LNET_LOCK_EX);
 185                }
 186
 187                rc = libcfs_sock_write(sock, &cr, sizeof(cr),
 188                                       accept_timeout);
 189                if (rc != 0)
 190                        goto failed_sock;
 191
 192                *sockp = sock;
 193                return 0;
 194        }
 195
 196        rc = -EADDRINUSE;
 197        goto failed;
 198
 199 failed_sock:
 200        libcfs_sock_release(sock);
 201 failed:
 202        lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
 203        return rc;
 204}
 205EXPORT_SYMBOL(lnet_connect);
 206
 207
 208/* Below is the code common for both kernel and MT user-space */
 209
 210static int
 211lnet_accept(struct socket *sock, __u32 magic)
 212{
 213        lnet_acceptor_connreq_t cr;
 214        __u32              peer_ip;
 215        int                  peer_port;
 216        int                  rc;
 217        int                  flip;
 218        lnet_ni_t             *ni;
 219        char               *str;
 220
 221        LASSERT(sizeof(cr) <= 16);           /* not too big for the stack */
 222
 223        rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
 224        LASSERT(rc == 0);                     /* we succeeded before */
 225
 226        if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
 227
 228                if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
 229                        /* future version compatibility!
 230                         * When LNET unifies protocols over all LNDs, the first
 231                         * thing sent will be a version query.  I send back
 232                         * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
 233
 234                        memset(&cr, 0, sizeof(cr));
 235                        cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
 236                        cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
 237                        rc = libcfs_sock_write(sock, &cr, sizeof(cr),
 238                                               accept_timeout);
 239
 240                        if (rc != 0)
 241                                CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
 242                                       &peer_ip, rc);
 243                        return -EPROTO;
 244                }
 245
 246                if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
 247                        str = "'old' socknal/tcpnal";
 248                else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
 249                        str = "'old' ranal";
 250                else
 251                        str = "unrecognised";
 252
 253                LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h magic %08x: %s acceptor protocol\n",
 254                                   &peer_ip, magic, str);
 255                return -EPROTO;
 256        }
 257
 258        flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
 259
 260        rc = libcfs_sock_read(sock, &cr.acr_version,
 261                              sizeof(cr.acr_version),
 262                              accept_timeout);
 263        if (rc != 0) {
 264                CERROR("Error %d reading connection request version from %pI4h\n",
 265                        rc, &peer_ip);
 266                return -EIO;
 267        }
 268
 269        if (flip)
 270                __swab32s(&cr.acr_version);
 271
 272        if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
 273                /* future version compatibility!
 274                 * An acceptor-specific protocol rev will first send a version
 275                 * query.  I send back my current version to tell her I'm
 276                 * "old". */
 277                int peer_version = cr.acr_version;
 278
 279                memset(&cr, 0, sizeof(cr));
 280                cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
 281                cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
 282
 283                rc = libcfs_sock_write(sock, &cr, sizeof(cr),
 284                                       accept_timeout);
 285
 286                if (rc != 0)
 287                        CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
 288                               peer_version, &peer_ip, rc);
 289                return -EPROTO;
 290        }
 291
 292        rc = libcfs_sock_read(sock, &cr.acr_nid,
 293                              sizeof(cr) -
 294                              offsetof(lnet_acceptor_connreq_t, acr_nid),
 295                              accept_timeout);
 296        if (rc != 0) {
 297                CERROR("Error %d reading connection request from %pI4h\n",
 298                        rc, &peer_ip);
 299                return -EIO;
 300        }
 301
 302        if (flip)
 303                __swab64s(&cr.acr_nid);
 304
 305        ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
 306        if (ni == NULL ||              /* no matching net */
 307            ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
 308                if (ni != NULL)
 309                        lnet_ni_decref(ni);
 310                LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h for %s: No matching NI\n",
 311                                   &peer_ip, libcfs_nid2str(cr.acr_nid));
 312                return -EPERM;
 313        }
 314
 315        if (ni->ni_lnd->lnd_accept == NULL) {
 316                /* This catches a request for the loopback LND */
 317                lnet_ni_decref(ni);
 318                LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h for %s: NI doesn not accept IP connections\n",
 319                                  &peer_ip, libcfs_nid2str(cr.acr_nid));
 320                return -EPERM;
 321        }
 322
 323        CDEBUG(D_NET, "Accept %s from %pI4h\n",
 324               libcfs_nid2str(cr.acr_nid), &peer_ip);
 325
 326        rc = ni->ni_lnd->lnd_accept(ni, sock);
 327
 328        lnet_ni_decref(ni);
 329        return rc;
 330}
 331
 332static int
 333lnet_acceptor(void *arg)
 334{
 335        struct socket *newsock;
 336        int         rc;
 337        __u32     magic;
 338        __u32     peer_ip;
 339        int         peer_port;
 340        int         secure = (int)((long_ptr_t)arg);
 341
 342        LASSERT(lnet_acceptor_state.pta_sock == NULL);
 343
 344        cfs_block_allsigs();
 345
 346        rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
 347                                0, accept_port, accept_backlog);
 348        if (rc != 0) {
 349                if (rc == -EADDRINUSE)
 350                        LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port %d: port already in use\n",
 351                                           accept_port);
 352                else
 353                        LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port %d: unexpected error %d\n",
 354                                           accept_port, rc);
 355
 356                lnet_acceptor_state.pta_sock = NULL;
 357        } else {
 358                LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
 359        }
 360
 361        /* set init status and unblock parent */
 362        lnet_acceptor_state.pta_shutdown = rc;
 363        complete(&lnet_acceptor_state.pta_signal);
 364
 365        if (rc != 0)
 366                return rc;
 367
 368        while (!lnet_acceptor_state.pta_shutdown) {
 369
 370                rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
 371                if (rc != 0) {
 372                        if (rc != -EAGAIN) {
 373                                CWARN("Accept error %d: pausing...\n", rc);
 374                                set_current_state(TASK_UNINTERRUPTIBLE);
 375                                schedule_timeout(cfs_time_seconds(1));
 376                        }
 377                        continue;
 378                }
 379
 380                /* maybe we're waken up with libcfs_sock_abort_accept() */
 381                if (lnet_acceptor_state.pta_shutdown) {
 382                        libcfs_sock_release(newsock);
 383                        break;
 384                }
 385
 386                rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
 387                if (rc != 0) {
 388                        CERROR("Can't determine new connection's address\n");
 389                        goto failed;
 390                }
 391
 392                if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
 393                        CERROR("Refusing connection from %pI4h: insecure port %d\n",
 394                               &peer_ip, peer_port);
 395                        goto failed;
 396                }
 397
 398                rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
 399                                      accept_timeout);
 400                if (rc != 0) {
 401                        CERROR("Error %d reading connection request from %pI4h\n",
 402                                rc, &peer_ip);
 403                        goto failed;
 404                }
 405
 406                rc = lnet_accept(newsock, magic);
 407                if (rc != 0)
 408                        goto failed;
 409
 410                continue;
 411
 412failed:
 413                libcfs_sock_release(newsock);
 414        }
 415
 416        libcfs_sock_release(lnet_acceptor_state.pta_sock);
 417        lnet_acceptor_state.pta_sock = NULL;
 418
 419        CDEBUG(D_NET, "Acceptor stopping\n");
 420
 421        /* unblock lnet_acceptor_stop() */
 422        complete(&lnet_acceptor_state.pta_signal);
 423        return 0;
 424}
 425
 426static inline int
 427accept2secure(const char *acc, long *sec)
 428{
 429        if (!strcmp(acc, "secure")) {
 430                *sec = 1;
 431                return 1;
 432        } else if (!strcmp(acc, "all")) {
 433                *sec = 0;
 434                return 1;
 435        } else if (!strcmp(acc, "none")) {
 436                return 0;
 437        }
 438
 439        LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
 440                           acc);
 441        return -EINVAL;
 442}
 443
 444int
 445lnet_acceptor_start(void)
 446{
 447        int  rc;
 448        long rc2;
 449        long secure;
 450
 451        LASSERT(lnet_acceptor_state.pta_sock == NULL);
 452
 453        rc = lnet_acceptor_get_tunables();
 454        if (rc != 0)
 455                return rc;
 456
 457
 458        init_completion(&lnet_acceptor_state.pta_signal);
 459        rc = accept2secure(accept_type, &secure);
 460        if (rc <= 0)
 461                return rc;
 462
 463        if (lnet_count_acceptor_nis() == 0)  /* not required */
 464                return 0;
 465
 466        rc2 = PTR_ERR(kthread_run(lnet_acceptor,
 467                                  (void *)(ulong_ptr_t)secure,
 468                                  "acceptor_%03ld", secure));
 469        if (IS_ERR_VALUE(rc2)) {
 470                CERROR("Can't start acceptor thread: %ld\n", rc2);
 471
 472                return -ESRCH;
 473        }
 474
 475        /* wait for acceptor to startup */
 476        wait_for_completion(&lnet_acceptor_state.pta_signal);
 477
 478        if (!lnet_acceptor_state.pta_shutdown) {
 479                /* started OK */
 480                LASSERT(lnet_acceptor_state.pta_sock != NULL);
 481                return 0;
 482        }
 483
 484        LASSERT(lnet_acceptor_state.pta_sock == NULL);
 485
 486        return -ENETDOWN;
 487}
 488
 489void
 490lnet_acceptor_stop(void)
 491{
 492        if (lnet_acceptor_state.pta_sock == NULL) /* not running */
 493                return;
 494
 495        lnet_acceptor_state.pta_shutdown = 1;
 496        libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
 497
 498        /* block until acceptor signals exit */
 499        wait_for_completion(&lnet_acceptor_state.pta_signal);
 500}
 501