linux/drivers/staging/lustre/lnet/lnet/peer.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * GPL HEADER START
   4 *
   5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License version 2 only,
   9 * as published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope that it will be useful, but
  12 * WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14 * General Public License version 2 for more details (a copy is included
  15 * in the LICENSE file that accompanied this code).
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * version 2 along with this program; If not, see
  19 * http://www.gnu.org/licenses/gpl-2.0.html
  20 *
  21 * GPL HEADER END
  22 */
  23/*
  24 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  25 * Use is subject to license terms.
  26 *
  27 * Copyright (c) 2012, Intel Corporation.
  28 */
  29/*
  30 * This file is part of Lustre, http://www.lustre.org/
  31 * Lustre is a trademark of Sun Microsystems, Inc.
  32 *
  33 * lnet/lnet/peer.c
  34 */
  35
  36#define DEBUG_SUBSYSTEM S_LNET
  37
  38#include <linux/lnet/lib-lnet.h>
  39#include <uapi/linux/lnet/lnet-dlc.h>
  40
  41int
  42lnet_peer_tables_create(void)
  43{
  44        struct lnet_peer_table *ptable;
  45        struct list_head *hash;
  46        int i;
  47        int j;
  48
  49        the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
  50                                                   sizeof(*ptable));
  51        if (!the_lnet.ln_peer_tables) {
  52                CERROR("Failed to allocate cpu-partition peer tables\n");
  53                return -ENOMEM;
  54        }
  55
  56        cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
  57                INIT_LIST_HEAD(&ptable->pt_deathrow);
  58
  59                hash = kvmalloc_cpt(LNET_PEER_HASH_SIZE * sizeof(*hash),
  60                                    GFP_KERNEL, i);
  61                if (!hash) {
  62                        CERROR("Failed to create peer hash table\n");
  63                        lnet_peer_tables_destroy();
  64                        return -ENOMEM;
  65                }
  66
  67                for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
  68                        INIT_LIST_HEAD(&hash[j]);
  69                ptable->pt_hash = hash; /* sign of initialization */
  70        }
  71
  72        return 0;
  73}
  74
  75void
  76lnet_peer_tables_destroy(void)
  77{
  78        struct lnet_peer_table *ptable;
  79        struct list_head *hash;
  80        int i;
  81        int j;
  82
  83        if (!the_lnet.ln_peer_tables)
  84                return;
  85
  86        cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
  87                hash = ptable->pt_hash;
  88                if (!hash) /* not initialized */
  89                        break;
  90
  91                LASSERT(list_empty(&ptable->pt_deathrow));
  92
  93                ptable->pt_hash = NULL;
  94                for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
  95                        LASSERT(list_empty(&hash[j]));
  96
  97                kvfree(hash);
  98        }
  99
 100        cfs_percpt_free(the_lnet.ln_peer_tables);
 101        the_lnet.ln_peer_tables = NULL;
 102}
 103
 104static void
 105lnet_peer_table_cleanup_locked(struct lnet_ni *ni,
 106                               struct lnet_peer_table *ptable)
 107{
 108        int i;
 109        struct lnet_peer *lp;
 110        struct lnet_peer *tmp;
 111
 112        for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
 113                list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
 114                                         lp_hashlist) {
 115                        if (ni && ni != lp->lp_ni)
 116                                continue;
 117                        list_del_init(&lp->lp_hashlist);
 118                        /* Lose hash table's ref */
 119                        ptable->pt_zombies++;
 120                        lnet_peer_decref_locked(lp);
 121                }
 122        }
 123}
 124
 125static void
 126lnet_peer_table_deathrow_wait_locked(struct lnet_peer_table *ptable,
 127                                     int cpt_locked)
 128{
 129        int i;
 130
 131        for (i = 3; ptable->pt_zombies; i++) {
 132                lnet_net_unlock(cpt_locked);
 133
 134                if (is_power_of_2(i)) {
 135                        CDEBUG(D_WARNING,
 136                               "Waiting for %d zombies on peer table\n",
 137                               ptable->pt_zombies);
 138                }
 139                set_current_state(TASK_UNINTERRUPTIBLE);
 140                schedule_timeout(HZ >> 1);
 141                lnet_net_lock(cpt_locked);
 142        }
 143}
 144
 145static void
 146lnet_peer_table_del_rtrs_locked(struct lnet_ni *ni,
 147                                struct lnet_peer_table *ptable,
 148                                int cpt_locked)
 149{
 150        struct lnet_peer *lp;
 151        struct lnet_peer *tmp;
 152        lnet_nid_t lp_nid;
 153        int i;
 154
 155        for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
 156                list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
 157                                         lp_hashlist) {
 158                        if (ni != lp->lp_ni)
 159                                continue;
 160
 161                        if (!lp->lp_rtr_refcount)
 162                                continue;
 163
 164                        lp_nid = lp->lp_nid;
 165
 166                        lnet_net_unlock(cpt_locked);
 167                        lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lp_nid);
 168                        lnet_net_lock(cpt_locked);
 169                }
 170        }
 171}
 172
 173void
 174lnet_peer_tables_cleanup(struct lnet_ni *ni)
 175{
 176        struct lnet_peer_table *ptable;
 177        struct list_head deathrow;
 178        struct lnet_peer *lp;
 179        struct lnet_peer *temp;
 180        int i;
 181
 182        INIT_LIST_HEAD(&deathrow);
 183
 184        LASSERT(the_lnet.ln_shutdown || ni);
 185        /*
 186         * If just deleting the peers for a NI, get rid of any routes these
 187         * peers are gateways for.
 188         */
 189        cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
 190                lnet_net_lock(i);
 191                lnet_peer_table_del_rtrs_locked(ni, ptable, i);
 192                lnet_net_unlock(i);
 193        }
 194
 195        /*
 196         * Start the process of moving the applicable peers to
 197         * deathrow.
 198         */
 199        cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
 200                lnet_net_lock(i);
 201                lnet_peer_table_cleanup_locked(ni, ptable);
 202                lnet_net_unlock(i);
 203        }
 204
 205        /* Cleanup all entries on deathrow. */
 206        cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
 207                lnet_net_lock(i);
 208                lnet_peer_table_deathrow_wait_locked(ptable, i);
 209                list_splice_init(&ptable->pt_deathrow, &deathrow);
 210                lnet_net_unlock(i);
 211        }
 212
 213        list_for_each_entry_safe(lp, temp, &deathrow, lp_hashlist) {
 214                list_del(&lp->lp_hashlist);
 215                kfree(lp);
 216        }
 217}
 218
 219void
 220lnet_destroy_peer_locked(struct lnet_peer *lp)
 221{
 222        struct lnet_peer_table *ptable;
 223
 224        LASSERT(!lp->lp_refcount);
 225        LASSERT(!lp->lp_rtr_refcount);
 226        LASSERT(list_empty(&lp->lp_txq));
 227        LASSERT(list_empty(&lp->lp_hashlist));
 228        LASSERT(!lp->lp_txqnob);
 229
 230        ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
 231        LASSERT(ptable->pt_number > 0);
 232        ptable->pt_number--;
 233
 234        lnet_ni_decref_locked(lp->lp_ni, lp->lp_cpt);
 235        lp->lp_ni = NULL;
 236
 237        list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
 238        LASSERT(ptable->pt_zombies > 0);
 239        ptable->pt_zombies--;
 240}
 241
 242struct lnet_peer *
 243lnet_find_peer_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
 244{
 245        struct list_head *peers;
 246        struct lnet_peer *lp;
 247
 248        LASSERT(!the_lnet.ln_shutdown);
 249
 250        peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
 251        list_for_each_entry(lp, peers, lp_hashlist) {
 252                if (lp->lp_nid == nid) {
 253                        lnet_peer_addref_locked(lp);
 254                        return lp;
 255                }
 256        }
 257
 258        return NULL;
 259}
 260
 261int
 262lnet_nid2peer_locked(struct lnet_peer **lpp, lnet_nid_t nid, int cpt)
 263{
 264        struct lnet_peer_table *ptable;
 265        struct lnet_peer *lp = NULL;
 266        struct lnet_peer *lp2;
 267        int cpt2;
 268        int rc = 0;
 269
 270        *lpp = NULL;
 271        if (the_lnet.ln_shutdown) /* it's shutting down */
 272                return -ESHUTDOWN;
 273
 274        /* cpt can be LNET_LOCK_EX if it's called from router functions */
 275        cpt2 = cpt != LNET_LOCK_EX ? cpt : lnet_cpt_of_nid_locked(nid);
 276
 277        ptable = the_lnet.ln_peer_tables[cpt2];
 278        lp = lnet_find_peer_locked(ptable, nid);
 279        if (lp) {
 280                *lpp = lp;
 281                return 0;
 282        }
 283
 284        if (!list_empty(&ptable->pt_deathrow)) {
 285                lp = list_entry(ptable->pt_deathrow.next,
 286                                struct lnet_peer, lp_hashlist);
 287                list_del(&lp->lp_hashlist);
 288        }
 289
 290        /*
 291         * take extra refcount in case another thread has shutdown LNet
 292         * and destroyed locks and peer-table before I finish the allocation
 293         */
 294        ptable->pt_number++;
 295        lnet_net_unlock(cpt);
 296
 297        if (lp)
 298                memset(lp, 0, sizeof(*lp));
 299        else
 300                lp = kzalloc_cpt(sizeof(*lp), GFP_NOFS, cpt2);
 301
 302        if (!lp) {
 303                rc = -ENOMEM;
 304                lnet_net_lock(cpt);
 305                goto out;
 306        }
 307
 308        INIT_LIST_HEAD(&lp->lp_txq);
 309        INIT_LIST_HEAD(&lp->lp_rtrq);
 310        INIT_LIST_HEAD(&lp->lp_routes);
 311
 312        lp->lp_notify = 0;
 313        lp->lp_notifylnd = 0;
 314        lp->lp_notifying = 0;
 315        lp->lp_alive_count = 0;
 316        lp->lp_timestamp = 0;
 317        lp->lp_alive = !lnet_peers_start_down(); /* 1 bit!! */
 318        lp->lp_last_alive = cfs_time_current(); /* assumes alive */
 319        lp->lp_last_query = 0; /* haven't asked NI yet */
 320        lp->lp_ping_timestamp = 0;
 321        lp->lp_ping_feats = LNET_PING_FEAT_INVAL;
 322        lp->lp_nid = nid;
 323        lp->lp_cpt = cpt2;
 324        lp->lp_refcount = 2;    /* 1 for caller; 1 for hash */
 325        lp->lp_rtr_refcount = 0;
 326
 327        lnet_net_lock(cpt);
 328
 329        if (the_lnet.ln_shutdown) {
 330                rc = -ESHUTDOWN;
 331                goto out;
 332        }
 333
 334        lp2 = lnet_find_peer_locked(ptable, nid);
 335        if (lp2) {
 336                *lpp = lp2;
 337                goto out;
 338        }
 339
 340        lp->lp_ni = lnet_net2ni_locked(LNET_NIDNET(nid), cpt2);
 341        if (!lp->lp_ni) {
 342                rc = -EHOSTUNREACH;
 343                goto out;
 344        }
 345
 346        lp->lp_txcredits = lp->lp_ni->ni_peertxcredits;
 347        lp->lp_mintxcredits = lp->lp_ni->ni_peertxcredits;
 348        lp->lp_rtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
 349        lp->lp_minrtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
 350
 351        list_add_tail(&lp->lp_hashlist,
 352                      &ptable->pt_hash[lnet_nid2peerhash(nid)]);
 353        ptable->pt_version++;
 354        *lpp = lp;
 355
 356        return 0;
 357out:
 358        if (lp)
 359                list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
 360        ptable->pt_number--;
 361        return rc;
 362}
 363
 364void
 365lnet_debug_peer(lnet_nid_t nid)
 366{
 367        char *aliveness = "NA";
 368        struct lnet_peer *lp;
 369        int rc;
 370        int cpt;
 371
 372        cpt = lnet_cpt_of_nid(nid);
 373        lnet_net_lock(cpt);
 374
 375        rc = lnet_nid2peer_locked(&lp, nid, cpt);
 376        if (rc) {
 377                lnet_net_unlock(cpt);
 378                CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
 379                return;
 380        }
 381
 382        if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
 383                aliveness = lp->lp_alive ? "up" : "down";
 384
 385        CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
 386               libcfs_nid2str(lp->lp_nid), lp->lp_refcount,
 387               aliveness, lp->lp_ni->ni_peertxcredits,
 388               lp->lp_rtrcredits, lp->lp_minrtrcredits,
 389               lp->lp_txcredits, lp->lp_mintxcredits, lp->lp_txqnob);
 390
 391        lnet_peer_decref_locked(lp);
 392
 393        lnet_net_unlock(cpt);
 394}
 395
 396int
 397lnet_get_peer_info(__u32 peer_index, __u64 *nid,
 398                   char aliveness[LNET_MAX_STR_LEN],
 399                   __u32 *cpt_iter, __u32 *refcount,
 400                   __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
 401                   __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
 402                   __u32 *peer_tx_qnob)
 403{
 404        struct lnet_peer_table *peer_table;
 405        struct lnet_peer *lp;
 406        bool found = false;
 407        int lncpt, j;
 408
 409        /* get the number of CPTs */
 410        lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
 411
 412        /*
 413         * if the cpt number to be examined is >= the number of cpts in
 414         * the system then indicate that there are no more cpts to examin
 415         */
 416        if (*cpt_iter >= lncpt)
 417                return -ENOENT;
 418
 419        /* get the current table */
 420        peer_table = the_lnet.ln_peer_tables[*cpt_iter];
 421        /* if the ptable is NULL then there are no more cpts to examine */
 422        if (!peer_table)
 423                return -ENOENT;
 424
 425        lnet_net_lock(*cpt_iter);
 426
 427        for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
 428                struct list_head *peers = &peer_table->pt_hash[j];
 429
 430                list_for_each_entry(lp, peers, lp_hashlist) {
 431                        if (peer_index-- > 0)
 432                                continue;
 433
 434                        snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
 435                        if (lnet_isrouter(lp) ||
 436                            lnet_peer_aliveness_enabled(lp))
 437                                snprintf(aliveness, LNET_MAX_STR_LEN,
 438                                         lp->lp_alive ? "up" : "down");
 439
 440                        *nid = lp->lp_nid;
 441                        *refcount = lp->lp_refcount;
 442                        *ni_peer_tx_credits = lp->lp_ni->ni_peertxcredits;
 443                        *peer_tx_credits = lp->lp_txcredits;
 444                        *peer_rtr_credits = lp->lp_rtrcredits;
 445                        *peer_min_rtr_credits = lp->lp_mintxcredits;
 446                        *peer_tx_qnob = lp->lp_txqnob;
 447
 448                        found = true;
 449                }
 450        }
 451        lnet_net_unlock(*cpt_iter);
 452
 453        *cpt_iter = lncpt;
 454
 455        return found ? 0 : -ENOENT;
 456}
 457