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 <linux/completion.h>
  39#include "../../include/linux/lnet/lib-lnet.h"
  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 = lnet_sock_connect(&sock, &fatal, local_ip, port, peer_ip,
 159                                       peer_port);
 160                if (rc != 0) {
 161                        if (fatal)
 162                                goto failed;
 163                        continue;
 164                }
 165
 166                CLASSERT(LNET_PROTO_ACCEPTOR_VERSION == 1);
 167
 168                cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
 169                cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
 170                cr.acr_nid     = peer_nid;
 171
 172                if (the_lnet.ln_testprotocompat != 0) {
 173                        /* single-shot proto check */
 174                        lnet_net_lock(LNET_LOCK_EX);
 175                        if ((the_lnet.ln_testprotocompat & 4) != 0) {
 176                                cr.acr_version++;
 177                                the_lnet.ln_testprotocompat &= ~4;
 178                        }
 179                        if ((the_lnet.ln_testprotocompat & 8) != 0) {
 180                                cr.acr_magic = LNET_PROTO_MAGIC;
 181                                the_lnet.ln_testprotocompat &= ~8;
 182                        }
 183                        lnet_net_unlock(LNET_LOCK_EX);
 184                }
 185
 186                rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
 187                if (rc != 0)
 188                        goto failed_sock;
 189
 190                *sockp = sock;
 191                return 0;
 192        }
 193
 194        rc = -EADDRINUSE;
 195        goto failed;
 196
 197 failed_sock:
 198        sock_release(sock);
 199 failed:
 200        lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
 201        return rc;
 202}
 203EXPORT_SYMBOL(lnet_connect);
 204
 205
 206/* Below is the code common for both kernel and MT user-space */
 207
 208static int
 209lnet_accept(struct socket *sock, __u32 magic)
 210{
 211        lnet_acceptor_connreq_t cr;
 212        __u32 peer_ip;
 213        int peer_port;
 214        int rc;
 215        int flip;
 216        lnet_ni_t *ni;
 217        char *str;
 218
 219        LASSERT(sizeof(cr) <= 16);           /* not too big for the stack */
 220
 221        rc = lnet_sock_getaddr(sock, 1, &peer_ip, &peer_port);
 222        LASSERT(rc == 0);                     /* we succeeded before */
 223
 224        if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
 225
 226                if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
 227                        /* future version compatibility!
 228                         * When LNET unifies protocols over all LNDs, the first
 229                         * thing sent will be a version query.  I send back
 230                         * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
 231
 232                        memset(&cr, 0, sizeof(cr));
 233                        cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
 234                        cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
 235                        rc = lnet_sock_write(sock, &cr, sizeof(cr),
 236                                             accept_timeout);
 237
 238                        if (rc != 0)
 239                                CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
 240                                       &peer_ip, rc);
 241                        return -EPROTO;
 242                }
 243
 244                if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
 245                        str = "'old' socknal/tcpnal";
 246                else
 247                        str = "unrecognised";
 248
 249                LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h magic %08x: %s acceptor protocol\n",
 250                                   &peer_ip, magic, str);
 251                return -EPROTO;
 252        }
 253
 254        flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
 255
 256        rc = lnet_sock_read(sock, &cr.acr_version, sizeof(cr.acr_version),
 257                            accept_timeout);
 258        if (rc != 0) {
 259                CERROR("Error %d reading connection request version from %pI4h\n",
 260                        rc, &peer_ip);
 261                return -EIO;
 262        }
 263
 264        if (flip)
 265                __swab32s(&cr.acr_version);
 266
 267        if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
 268                /* future version compatibility!
 269                 * An acceptor-specific protocol rev will first send a version
 270                 * query.  I send back my current version to tell her I'm
 271                 * "old". */
 272                int peer_version = cr.acr_version;
 273
 274                memset(&cr, 0, sizeof(cr));
 275                cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
 276                cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
 277
 278                rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
 279                if (rc != 0)
 280                        CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
 281                               peer_version, &peer_ip, rc);
 282                return -EPROTO;
 283        }
 284
 285        rc = lnet_sock_read(sock, &cr.acr_nid,
 286                            sizeof(cr) -
 287                            offsetof(lnet_acceptor_connreq_t, acr_nid),
 288                            accept_timeout);
 289        if (rc != 0) {
 290                CERROR("Error %d reading connection request from %pI4h\n",
 291                        rc, &peer_ip);
 292                return -EIO;
 293        }
 294
 295        if (flip)
 296                __swab64s(&cr.acr_nid);
 297
 298        ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
 299        if (ni == NULL ||              /* no matching net */
 300            ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
 301                if (ni != NULL)
 302                        lnet_ni_decref(ni);
 303                LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h for %s: No matching NI\n",
 304                                   &peer_ip, libcfs_nid2str(cr.acr_nid));
 305                return -EPERM;
 306        }
 307
 308        if (ni->ni_lnd->lnd_accept == NULL) {
 309                /* This catches a request for the loopback LND */
 310                lnet_ni_decref(ni);
 311                LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h for %s: NI doesn not accept IP connections\n",
 312                                  &peer_ip, libcfs_nid2str(cr.acr_nid));
 313                return -EPERM;
 314        }
 315
 316        CDEBUG(D_NET, "Accept %s from %pI4h\n",
 317               libcfs_nid2str(cr.acr_nid), &peer_ip);
 318
 319        rc = ni->ni_lnd->lnd_accept(ni, sock);
 320
 321        lnet_ni_decref(ni);
 322        return rc;
 323}
 324
 325static int
 326lnet_acceptor(void *arg)
 327{
 328        struct socket *newsock;
 329        int rc;
 330        __u32 magic;
 331        __u32 peer_ip;
 332        int peer_port;
 333        int secure = (int)((long_ptr_t)arg);
 334
 335        LASSERT(lnet_acceptor_state.pta_sock == NULL);
 336
 337        cfs_block_allsigs();
 338
 339        rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock, 0, accept_port,
 340                              accept_backlog);
 341        if (rc != 0) {
 342                if (rc == -EADDRINUSE)
 343                        LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port %d: port already in use\n",
 344                                           accept_port);
 345                else
 346                        LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port %d: unexpected error %d\n",
 347                                           accept_port, rc);
 348
 349                lnet_acceptor_state.pta_sock = NULL;
 350        } else {
 351                LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
 352        }
 353
 354        /* set init status and unblock parent */
 355        lnet_acceptor_state.pta_shutdown = rc;
 356        complete(&lnet_acceptor_state.pta_signal);
 357
 358        if (rc != 0)
 359                return rc;
 360
 361        while (!lnet_acceptor_state.pta_shutdown) {
 362
 363                rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
 364                if (rc != 0) {
 365                        if (rc != -EAGAIN) {
 366                                CWARN("Accept error %d: pausing...\n", rc);
 367                                set_current_state(TASK_UNINTERRUPTIBLE);
 368                                schedule_timeout(cfs_time_seconds(1));
 369                        }
 370                        continue;
 371                }
 372
 373                /* maybe the LNet acceptor thread has been waken */
 374                if (lnet_acceptor_state.pta_shutdown) {
 375                        sock_release(newsock);
 376                        break;
 377                }
 378
 379                rc = lnet_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
 380                if (rc != 0) {
 381                        CERROR("Can't determine new connection's address\n");
 382                        goto failed;
 383                }
 384
 385                if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
 386                        CERROR("Refusing connection from %pI4h: insecure port %d\n",
 387                               &peer_ip, peer_port);
 388                        goto failed;
 389                }
 390
 391                rc = lnet_sock_read(newsock, &magic, sizeof(magic),
 392                                    accept_timeout);
 393                if (rc != 0) {
 394                        CERROR("Error %d reading connection request from %pI4h\n",
 395                                rc, &peer_ip);
 396                        goto failed;
 397                }
 398
 399                rc = lnet_accept(newsock, magic);
 400                if (rc != 0)
 401                        goto failed;
 402
 403                continue;
 404
 405failed:
 406                sock_release(newsock);
 407        }
 408
 409        sock_release(lnet_acceptor_state.pta_sock);
 410        lnet_acceptor_state.pta_sock = NULL;
 411
 412        CDEBUG(D_NET, "Acceptor stopping\n");
 413
 414        /* unblock lnet_acceptor_stop() */
 415        complete(&lnet_acceptor_state.pta_signal);
 416        return 0;
 417}
 418
 419static inline int
 420accept2secure(const char *acc, long *sec)
 421{
 422        if (!strcmp(acc, "secure")) {
 423                *sec = 1;
 424                return 1;
 425        } else if (!strcmp(acc, "all")) {
 426                *sec = 0;
 427                return 1;
 428        } else if (!strcmp(acc, "none")) {
 429                return 0;
 430        }
 431
 432        LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
 433                           acc);
 434        return -EINVAL;
 435}
 436
 437int
 438lnet_acceptor_start(void)
 439{
 440        int rc;
 441        long rc2;
 442        long secure;
 443
 444        LASSERT(lnet_acceptor_state.pta_sock == NULL);
 445
 446        rc = lnet_acceptor_get_tunables();
 447        if (rc != 0)
 448                return rc;
 449
 450
 451        init_completion(&lnet_acceptor_state.pta_signal);
 452        rc = accept2secure(accept_type, &secure);
 453        if (rc <= 0)
 454                return rc;
 455
 456        if (lnet_count_acceptor_nis() == 0)  /* not required */
 457                return 0;
 458
 459        rc2 = PTR_ERR(kthread_run(lnet_acceptor,
 460                                  (void *)(ulong_ptr_t)secure,
 461                                  "acceptor_%03ld", secure));
 462        if (IS_ERR_VALUE(rc2)) {
 463                CERROR("Can't start acceptor thread: %ld\n", rc2);
 464
 465                return -ESRCH;
 466        }
 467
 468        /* wait for acceptor to startup */
 469        wait_for_completion(&lnet_acceptor_state.pta_signal);
 470
 471        if (!lnet_acceptor_state.pta_shutdown) {
 472                /* started OK */
 473                LASSERT(lnet_acceptor_state.pta_sock != NULL);
 474                return 0;
 475        }
 476
 477        LASSERT(lnet_acceptor_state.pta_sock == NULL);
 478
 479        return -ENETDOWN;
 480}
 481
 482void
 483lnet_acceptor_stop(void)
 484{
 485        if (lnet_acceptor_state.pta_sock == NULL) /* not running */
 486                return;
 487
 488        lnet_acceptor_state.pta_shutdown = 1;
 489        wake_up_all(sk_sleep(lnet_acceptor_state.pta_sock->sk));
 490
 491        /* block until acceptor signals exit */
 492        wait_for_completion(&lnet_acceptor_state.pta_signal);
 493}
 494