1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2005 Cluster File Systems, Inc. All rights reserved.
5 * Author: PJ Kirner <pjkirner@clusterfs.com>
6 * E Barton <eeb@bartonsoftware.com>
8 * This file is part of the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
11 * This file is confidential source code owned by Cluster File Systems.
12 * No viewing, modification, compilation, redistribution, or any other
13 * form of use is permitted except through a signed license agreement.
15 * If you have not signed such an agreement, then you have no rights to
16 * this file. Please destroy it immediately and contact CFS.
21 #include <libcfs/list.h>
24 kptllnd_count_queue(struct list_head *q)
37 kptllnd_get_peer_info(int index,
38 lnet_process_id_t *id,
39 int *state, int *sent_hello,
40 int *refcount, __u64 *incarnation,
41 __u64 *next_matchbits, __u64 *last_matchbits_seen,
42 int *nsendq, int *nactiveq,
43 int *credits, int *outstanding_credits)
45 rwlock_t *g_lock = &kptllnd_data.kptl_peer_rw_lock;
47 struct list_head *ptmp;
52 read_lock_irqsave(g_lock, flags);
54 for (i = 0; i < kptllnd_data.kptl_peer_hash_size; i++) {
56 list_for_each (ptmp, &kptllnd_data.kptl_peers[i]) {
57 peer = list_entry(ptmp, kptl_peer_t, peer_list);
63 *state = peer->peer_state;
64 *sent_hello = peer->peer_sent_hello;
65 *refcount = atomic_read(&peer->peer_refcount);
66 *incarnation = peer->peer_incarnation;
68 spin_lock(&peer->peer_lock);
70 *next_matchbits = peer->peer_next_matchbits;
71 *last_matchbits_seen = peer->peer_last_matchbits_seen;
72 *credits = peer->peer_credits;
73 *outstanding_credits = peer->peer_outstanding_credits;
75 *nsendq = kptllnd_count_queue(&peer->peer_sendq);
76 *nactiveq = kptllnd_count_queue(&peer->peer_activeq);
78 spin_unlock(&peer->peer_lock);
86 read_unlock_irqrestore(g_lock, flags);
91 kptllnd_peer_add_peertable_locked (kptl_peer_t *peer)
93 LASSERT (kptllnd_data.kptl_n_active_peers <
94 kptllnd_data.kptl_expected_peers);
96 LASSERT (peer->peer_state == PEER_STATE_WAITING_HELLO ||
97 peer->peer_state == PEER_STATE_ACTIVE);
99 kptllnd_data.kptl_n_active_peers++;
100 atomic_inc(&peer->peer_refcount); /* +1 ref for the list */
102 /* NB add to HEAD of peer list for MRU order!
103 * (see kptllnd_cull_peertable) */
104 list_add(&peer->peer_list, kptllnd_nid2peerlist(peer->peer_id.nid));
108 kptllnd_cull_peertable_locked (lnet_process_id_t pid)
110 /* I'm about to add a new peer with this portals ID to the peer table,
111 * so (a) this peer should not exist already and (b) I want to leave at
112 * most (max_procs_per_nid - 1) peers with this NID in the table. */
113 struct list_head *peers = kptllnd_nid2peerlist(pid.nid);
114 int cull_count = *kptllnd_tunables.kptl_max_procs_per_node;
116 struct list_head *tmp;
117 struct list_head *nxt;
121 list_for_each_safe (tmp, nxt, peers) {
122 /* NB I rely on kptllnd_peer_add_peertable_locked to add peers
124 peer = list_entry(tmp, kptl_peer_t, peer_list);
126 if (peer->peer_id.nid != pid.nid)
129 LASSERT (peer->peer_id.pid != pid.pid);
133 if (count < cull_count) /* recent (don't cull) */
136 CDEBUG(D_NET, "Cull %s(%s)\n",
137 libcfs_id2str(peer->peer_id),
138 kptllnd_ptlid2str(peer->peer_ptlid));
140 kptllnd_peer_close_locked(peer, 0);
145 kptllnd_peer_allocate (lnet_process_id_t lpid, ptl_process_id_t ppid)
150 LIBCFS_ALLOC(peer, sizeof (*peer));
152 CERROR("Can't create peer %s (%s)\n",
154 kptllnd_ptlid2str(ppid));
158 memset(peer, 0, sizeof(*peer)); /* zero flags etc */
160 INIT_LIST_HEAD (&peer->peer_sendq);
161 INIT_LIST_HEAD (&peer->peer_activeq);
162 spin_lock_init (&peer->peer_lock);
164 peer->peer_state = PEER_STATE_ALLOCATED;
165 peer->peer_error = 0;
166 peer->peer_last_alive = cfs_time_current();
167 peer->peer_id = lpid;
168 peer->peer_ptlid = ppid;
169 peer->peer_credits = 1; /* enough for HELLO */
170 peer->peer_next_matchbits = PTL_RESERVED_MATCHBITS;
171 peer->peer_outstanding_credits = *kptllnd_tunables.kptl_peercredits - 1;
172 peer->peer_sent_credits = 1; /* HELLO credit is implicit */
173 peer->peer_max_msg_size = PTLLND_MIN_BUFFER_SIZE; /* until we know better */
175 atomic_set(&peer->peer_refcount, 1); /* 1 ref for caller */
177 write_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
179 peer->peer_myincarnation = kptllnd_data.kptl_incarnation;
181 /* Only increase # peers under lock, to guarantee we dont grow it
183 if (kptllnd_data.kptl_shutdown) {
184 write_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock,
186 LIBCFS_FREE(peer, sizeof(*peer));
190 kptllnd_data.kptl_npeers++;
191 write_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
197 kptllnd_peer_destroy (kptl_peer_t *peer)
201 CDEBUG(D_NET, "Peer=%p\n", peer);
203 LASSERT (!in_interrupt());
204 LASSERT (atomic_read(&peer->peer_refcount) == 0);
205 LASSERT (peer->peer_state == PEER_STATE_ALLOCATED ||
206 peer->peer_state == PEER_STATE_ZOMBIE);
207 LASSERT (list_empty(&peer->peer_sendq));
208 LASSERT (list_empty(&peer->peer_activeq));
210 write_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
212 if (peer->peer_state == PEER_STATE_ZOMBIE)
213 list_del(&peer->peer_list);
215 kptllnd_data.kptl_npeers--;
217 write_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
219 LIBCFS_FREE (peer, sizeof (*peer));
223 kptllnd_cancel_txlist (struct list_head *peerq, struct list_head *txs)
225 struct list_head *tmp;
226 struct list_head *nxt;
229 list_for_each_safe (tmp, nxt, peerq) {
230 tx = list_entry(tmp, kptl_tx_t, tx_list);
232 list_del(&tx->tx_list);
233 list_add_tail(&tx->tx_list, txs);
235 tx->tx_status = -EIO;
241 kptllnd_peer_cancel_txs(kptl_peer_t *peer, struct list_head *txs)
245 spin_lock_irqsave(&peer->peer_lock, flags);
247 kptllnd_cancel_txlist(&peer->peer_sendq, txs);
248 kptllnd_cancel_txlist(&peer->peer_activeq, txs);
250 spin_unlock_irqrestore(&peer->peer_lock, flags);
254 kptllnd_peer_alive (kptl_peer_t *peer)
256 /* This is racy, but everyone's only writing cfs_time_current() */
257 peer->peer_last_alive = cfs_time_current();
262 kptllnd_peer_notify (kptl_peer_t *peer)
265 time_t last_alive = 0;
268 spin_lock_irqsave(&peer->peer_lock, flags);
270 if (peer->peer_error != 0) {
271 error = peer->peer_error;
272 peer->peer_error = 0;
274 last_alive = cfs_time_current_sec() -
275 cfs_duration_sec(cfs_time_current() -
276 peer->peer_last_alive);
279 spin_unlock_irqrestore(&peer->peer_lock, flags);
282 lnet_notify (kptllnd_data.kptl_ni, peer->peer_id.nid, 0,
287 kptllnd_handle_closing_peers ()
290 struct list_head txs;
292 struct list_head *tmp;
293 struct list_head *nxt;
297 /* Check with a read lock first to avoid blocking anyone */
299 read_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
300 idle = list_empty(&kptllnd_data.kptl_closing_peers) &&
301 list_empty(&kptllnd_data.kptl_zombie_peers);
302 read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
307 INIT_LIST_HEAD(&txs);
309 write_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
311 /* Cancel txs on all zombie peers. NB anyone dropping the last peer
312 * ref removes it from this list, so I musn't drop the lock while
314 list_for_each (tmp, &kptllnd_data.kptl_zombie_peers) {
315 peer = list_entry (tmp, kptl_peer_t, peer_list);
317 LASSERT (peer->peer_state == PEER_STATE_ZOMBIE);
319 kptllnd_peer_cancel_txs(peer, &txs);
322 /* Notify LNET and cancel txs on closing (i.e. newly closed) peers. NB
323 * I'm the only one removing from this list, but peers can be added on
324 * the end any time I drop the lock. */
326 list_for_each_safe (tmp, nxt, &kptllnd_data.kptl_closing_peers) {
327 peer = list_entry (tmp, kptl_peer_t, peer_list);
329 LASSERT (peer->peer_state == PEER_STATE_CLOSING);
331 list_del(&peer->peer_list);
332 list_add_tail(&peer->peer_list,
333 &kptllnd_data.kptl_zombie_peers);
334 peer->peer_state = PEER_STATE_ZOMBIE;
336 write_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
338 kptllnd_peer_notify(peer);
339 kptllnd_peer_cancel_txs(peer, &txs);
340 kptllnd_peer_decref(peer);
342 write_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
345 write_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
347 /* Drop peer's ref on all cancelled txs. This will get
348 * kptllnd_tx_fini() to abort outstanding comms if necessary. */
350 list_for_each_safe (tmp, nxt, &txs) {
351 tx = list_entry(tmp, kptl_tx_t, tx_list);
352 list_del(&tx->tx_list);
353 kptllnd_tx_decref(tx);
358 kptllnd_peer_close_locked(kptl_peer_t *peer, int why)
360 switch (peer->peer_state) {
364 case PEER_STATE_WAITING_HELLO:
365 case PEER_STATE_ACTIVE:
366 /* Ensure new peers see a new incarnation of me */
367 LASSERT(peer->peer_myincarnation <= kptllnd_data.kptl_incarnation);
368 if (peer->peer_myincarnation == kptllnd_data.kptl_incarnation)
369 kptllnd_data.kptl_incarnation++;
371 /* Removing from peer table */
372 kptllnd_data.kptl_n_active_peers--;
373 LASSERT (kptllnd_data.kptl_n_active_peers >= 0);
375 list_del(&peer->peer_list);
376 kptllnd_peer_unreserve_buffers();
378 peer->peer_error = why; /* stash 'why' only on first close */
379 peer->peer_state = PEER_STATE_CLOSING;
381 /* Schedule for immediate attention, taking peer table's ref */
382 list_add_tail(&peer->peer_list,
383 &kptllnd_data.kptl_closing_peers);
384 wake_up(&kptllnd_data.kptl_watchdog_waitq);
387 case PEER_STATE_ZOMBIE:
388 case PEER_STATE_CLOSING:
394 kptllnd_peer_close(kptl_peer_t *peer, int why)
398 write_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
399 kptllnd_peer_close_locked(peer, why);
400 write_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
404 kptllnd_peer_del(lnet_process_id_t id)
406 struct list_head *ptmp;
407 struct list_head *pnxt;
416 * Find the single bucket we are supposed to look at or if nid is a
417 * wildcard (LNET_NID_ANY) then look at all of the buckets
419 if (id.nid != LNET_NID_ANY) {
420 struct list_head *l = kptllnd_nid2peerlist(id.nid);
422 lo = hi = l - kptllnd_data.kptl_peers;
424 if (id.pid != LNET_PID_ANY)
428 hi = kptllnd_data.kptl_peer_hash_size - 1;
432 read_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
434 for (i = lo; i <= hi; i++) {
435 list_for_each_safe (ptmp, pnxt, &kptllnd_data.kptl_peers[i]) {
436 peer = list_entry (ptmp, kptl_peer_t, peer_list);
438 if (!(id.nid == LNET_NID_ANY ||
439 (peer->peer_id.nid == id.nid &&
440 (id.pid == LNET_PID_ANY ||
441 peer->peer_id.pid == id.pid))))
444 kptllnd_peer_addref(peer); /* 1 ref for me... */
446 read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock,
449 kptllnd_peer_close(peer, 0);
450 kptllnd_peer_decref(peer); /* ...until here */
452 rc = 0; /* matched something */
454 /* start again now I've dropped the lock */
459 read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
465 kptllnd_post_tx(kptl_peer_t *peer, kptl_tx_t *tx, int nfrag)
467 /* CAVEAT EMPTOR: I take over caller's ref on 'tx' */
468 ptl_handle_md_t msg_mdh;
473 LASSERT (!tx->tx_idle);
474 LASSERT (!tx->tx_active);
475 LASSERT (PtlHandleIsEqual(tx->tx_msg_mdh, PTL_INVALID_HANDLE));
476 LASSERT (PtlHandleIsEqual(tx->tx_rdma_mdh, PTL_INVALID_HANDLE));
477 LASSERT (tx->tx_type == TX_TYPE_SMALL_MESSAGE ||
478 tx->tx_type == TX_TYPE_PUT_REQUEST ||
479 tx->tx_type == TX_TYPE_GET_REQUEST);
481 kptllnd_set_tx_peer(tx, peer);
483 memset(&md, 0, sizeof(md));
485 md.threshold = tx->tx_acked ? 2 : 1; /* SEND END + ACK? */
486 md.options = PTL_MD_OP_PUT |
487 PTL_MD_LUSTRE_COMPLETION_SEMANTICS |
488 PTL_MD_EVENT_START_DISABLE;
489 md.user_ptr = &tx->tx_msg_eventarg;
490 md.eq_handle = kptllnd_data.kptl_eqh;
493 md.start = tx->tx_msg;
494 md.length = tx->tx_msg->ptlm_nob;
497 LASSERT (tx->tx_frags->iov[0].iov_base == (void *)tx->tx_msg);
499 md.start = tx->tx_frags;
501 md.options |= PTL_MD_IOVEC;
504 prc = PtlMDBind(kptllnd_data.kptl_nih, md, PTL_UNLINK, &msg_mdh);
506 CERROR("PtlMDBind(%s) failed: %d\n",
507 libcfs_id2str(peer->peer_id), prc);
508 tx->tx_status = -EIO;
509 kptllnd_tx_decref(tx);
513 spin_lock_irqsave(&peer->peer_lock, flags);
515 tx->tx_deadline = jiffies + (*kptllnd_tunables.kptl_timeout * HZ);
517 tx->tx_msg_mdh = msg_mdh;
519 /* Ensure HELLO is sent first */
520 if (tx->tx_msg->ptlm_type == PTLLND_MSG_TYPE_HELLO)
521 list_add(&tx->tx_list, &peer->peer_sendq);
523 list_add_tail(&tx->tx_list, &peer->peer_sendq);
525 spin_unlock_irqrestore(&peer->peer_lock, flags);
529 kptllnd_peer_check_sends (kptl_peer_t *peer)
536 LASSERT(!in_interrupt());
538 spin_lock_irqsave(&peer->peer_lock, flags);
540 peer->peer_retry_noop = 0;
542 if (list_empty(&peer->peer_sendq) &&
543 peer->peer_outstanding_credits >= PTLLND_CREDIT_HIGHWATER &&
544 peer->peer_credits != 0) {
546 /* post a NOOP to return credits */
547 spin_unlock_irqrestore(&peer->peer_lock, flags);
549 tx = kptllnd_get_idle_tx(TX_TYPE_SMALL_MESSAGE);
551 CERROR("Can't return credits to %s: can't allocate descriptor\n",
552 libcfs_id2str(peer->peer_id));
554 kptllnd_init_msg(tx->tx_msg, PTLLND_MSG_TYPE_NOOP, 0);
555 kptllnd_post_tx(peer, tx, 0);
558 spin_lock_irqsave(&peer->peer_lock, flags);
559 peer->peer_retry_noop = (tx == NULL);
562 while (!list_empty(&peer->peer_sendq)) {
563 tx = list_entry (peer->peer_sendq.next, kptl_tx_t, tx_list);
565 LASSERT (tx->tx_active);
566 LASSERT (!PtlHandleIsEqual(tx->tx_msg_mdh, PTL_INVALID_HANDLE));
567 LASSERT (PtlHandleIsEqual(tx->tx_rdma_mdh, PTL_INVALID_HANDLE));
569 LASSERT (peer->peer_outstanding_credits >= 0);
570 LASSERT (peer->peer_sent_credits >= 0);
571 LASSERT (peer->peer_sent_credits +
572 peer->peer_outstanding_credits <=
573 *kptllnd_tunables.kptl_peercredits);
574 LASSERT (peer->peer_credits >= 0);
576 /* Ensure HELLO is sent first */
577 if (!peer->peer_sent_hello) {
578 if (tx->tx_msg->ptlm_type != PTLLND_MSG_TYPE_HELLO)
580 peer->peer_sent_hello = 1;
583 if (peer->peer_credits == 0) {
584 CDEBUG(D_NETTRACE, "%s[%d/%d+%d]: no credits for %p\n",
585 libcfs_id2str(peer->peer_id),
587 peer->peer_outstanding_credits,
588 peer->peer_sent_credits, tx);
592 /* Don't use the last credit unless I've got credits to
594 if (peer->peer_credits == 1 &&
595 peer->peer_outstanding_credits == 0) {
596 CDEBUG(D_NETTRACE, "%s[%d/%d+%d]: "
597 "not using last credit for %p\n",
598 libcfs_id2str(peer->peer_id),
600 peer->peer_outstanding_credits,
601 peer->peer_sent_credits, tx);
605 list_del(&tx->tx_list);
607 /* Discard any NOOP I queued if I'm not at the high-water mark
608 * any more or more messages have been queued */
609 if (tx->tx_msg->ptlm_type == PTLLND_MSG_TYPE_NOOP &&
610 (!list_empty(&peer->peer_sendq) ||
611 peer->peer_outstanding_credits < PTLLND_CREDIT_HIGHWATER)) {
615 spin_unlock_irqrestore(&peer->peer_lock, flags);
617 CDEBUG(D_NET, "%s: redundant noop\n",
618 libcfs_id2str(peer->peer_id));
619 kptllnd_tx_decref(tx);
621 spin_lock_irqsave(&peer->peer_lock, flags);
625 /* fill last-minute msg fields */
626 kptllnd_msg_pack(tx->tx_msg, peer);
628 if (tx->tx_type == TX_TYPE_PUT_REQUEST ||
629 tx->tx_type == TX_TYPE_GET_REQUEST) {
630 /* peer_next_matchbits must be known good */
631 LASSERT (peer->peer_state >= PEER_STATE_ACTIVE);
632 /* Assume 64-bit matchbits can't wrap */
633 LASSERT (peer->peer_next_matchbits >= PTL_RESERVED_MATCHBITS);
634 tx->tx_msg->ptlm_u.rdma.kptlrm_matchbits =
635 peer->peer_next_matchbits++;
638 peer->peer_sent_credits += peer->peer_outstanding_credits;
639 peer->peer_outstanding_credits = 0;
640 peer->peer_credits--;
642 CDEBUG(D_NETTRACE, "%s[%d/%d+%d]: %s tx=%p nob=%d cred=%d\n",
643 libcfs_id2str(peer->peer_id), peer->peer_credits,
644 peer->peer_outstanding_credits, peer->peer_sent_credits,
645 kptllnd_msgtype2str(tx->tx_msg->ptlm_type),
646 tx, tx->tx_msg->ptlm_nob,
647 tx->tx_msg->ptlm_credits);
649 list_add_tail(&tx->tx_list, &peer->peer_activeq);
651 kptllnd_tx_addref(tx); /* 1 ref for me... */
653 spin_unlock_irqrestore(&peer->peer_lock, flags);
655 if (tx->tx_type == TX_TYPE_PUT_REQUEST ||
656 tx->tx_type == TX_TYPE_GET_REQUEST) {
657 /* Post bulk now we have safe matchbits */
658 rc = PtlMEAttach(kptllnd_data.kptl_nih,
659 *kptllnd_tunables.kptl_portal,
661 tx->tx_msg->ptlm_u.rdma.kptlrm_matchbits,
667 CERROR("PtlMEAttach(%s) failed: %d\n",
668 libcfs_id2str(peer->peer_id), rc);
672 rc = PtlMDAttach(meh, tx->tx_rdma_md, PTL_UNLINK,
675 CERROR("PtlMDAttach(%s) failed: %d\n",
676 libcfs_id2str(tx->tx_peer->peer_id), rc);
677 rc = PtlMEUnlink(meh);
678 LASSERT(rc == PTL_OK);
679 tx->tx_rdma_mdh = PTL_INVALID_HANDLE;
682 /* I'm not racing with the event callback here. It's a
683 * bug if there's an event on the MD I just attached
684 * before I actually send the RDMA request message -
685 * probably matchbits re-used in error. */
688 tx->tx_tposted = jiffies; /* going on the wire */
690 rc = PtlPut (tx->tx_msg_mdh,
691 tx->tx_acked ? PTL_ACK_REQ : PTL_NOACK_REQ,
693 *kptllnd_tunables.kptl_portal,
697 0); /* header data */
699 CERROR("PtlPut %s error %d\n",
700 libcfs_id2str(peer->peer_id), rc);
704 kptllnd_tx_decref(tx); /* drop my ref */
706 spin_lock_irqsave(&peer->peer_lock, flags);
709 spin_unlock_irqrestore(&peer->peer_lock, flags);
713 /* Nuke everything (including tx we were trying) */
714 kptllnd_peer_close(peer, -EIO);
715 kptllnd_tx_decref(tx);
719 kptllnd_find_timed_out_tx(kptl_peer_t *peer)
722 struct list_head *tmp;
724 list_for_each(tmp, &peer->peer_sendq) {
725 tx = list_entry(peer->peer_sendq.next, kptl_tx_t, tx_list);
727 if (time_after_eq(jiffies, tx->tx_deadline)) {
728 kptllnd_tx_addref(tx);
733 list_for_each(tmp, &peer->peer_activeq) {
734 tx = list_entry(peer->peer_activeq.next, kptl_tx_t, tx_list);
736 if (time_after_eq(jiffies, tx->tx_deadline)) {
737 kptllnd_tx_addref(tx);
747 kptllnd_peer_check_bucket (int idx, int stamp)
749 struct list_head *peers = &kptllnd_data.kptl_peers[idx];
750 struct list_head *ptmp;
758 CDEBUG(D_NET, "Bucket=%d, stamp=%d\n", idx, stamp);
761 /* NB. Shared lock while I just look */
762 read_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
764 list_for_each (ptmp, peers) {
765 peer = list_entry (ptmp, kptl_peer_t, peer_list);
767 CDEBUG(D_NET, "Peer=%s Credits=%d Outstanding=%d Send=%d\n",
768 libcfs_id2str(peer->peer_id), peer->peer_credits,
769 peer->peer_outstanding_credits, peer->peer_sent_credits);
771 spin_lock(&peer->peer_lock);
773 if (peer->peer_check_stamp == stamp) {
774 /* checked already this pass */
775 spin_unlock(&peer->peer_lock);
779 peer->peer_check_stamp = stamp;
780 tx = kptllnd_find_timed_out_tx(peer);
781 check_sends = peer->peer_retry_noop;
783 spin_unlock(&peer->peer_lock);
785 if (tx == NULL && !check_sends)
788 kptllnd_peer_addref(peer); /* 1 ref for me... */
790 read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
792 if (tx == NULL) { /* nothing timed out */
793 kptllnd_peer_check_sends(peer);
794 kptllnd_peer_decref(peer); /* ...until here or... */
796 /* rescan after dropping the lock */
800 spin_lock_irqsave(&peer->peer_lock, flags);
801 nsend = kptllnd_count_queue(&peer->peer_sendq);
802 nactive = kptllnd_count_queue(&peer->peer_activeq);
803 spin_unlock_irqrestore(&peer->peer_lock, flags);
805 LCONSOLE_ERROR("Timing out %s: %s\n",
806 libcfs_id2str(peer->peer_id),
807 (tx->tx_tposted == 0) ?
808 "no free peer buffers" : "please check Portals");
810 CERROR("%s timed out: cred %d outstanding %d, sent %d, "
811 "sendq %d, activeq %d Tx %p %s (%s%s%s) status %d "
812 "%sposted %lu T/O %ds\n",
813 libcfs_id2str(peer->peer_id), peer->peer_credits,
814 peer->peer_outstanding_credits, peer->peer_sent_credits,
815 nsend, nactive, tx, kptllnd_tx_typestr(tx->tx_type),
816 tx->tx_active ? "A" : "",
817 PtlHandleIsEqual(tx->tx_msg_mdh, PTL_INVALID_HANDLE) ?
819 PtlHandleIsEqual(tx->tx_rdma_mdh, PTL_INVALID_HANDLE) ?
822 (tx->tx_tposted == 0) ? "not " : "",
823 (tx->tx_tposted == 0) ? 0UL : (jiffies - tx->tx_tposted),
824 *kptllnd_tunables.kptl_timeout);
826 kptllnd_dump_ptltrace();
828 kptllnd_tx_decref(tx);
830 kptllnd_peer_close(peer, -ETIMEDOUT);
831 kptllnd_peer_decref(peer); /* ...until here */
833 /* start again now I've dropped the lock */
837 read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
841 kptllnd_id2peer_locked (lnet_process_id_t id)
843 struct list_head *peers = kptllnd_nid2peerlist(id.nid);
844 struct list_head *tmp;
847 list_for_each (tmp, peers) {
849 peer = list_entry (tmp, kptl_peer_t, peer_list);
851 LASSERT(peer->peer_state == PEER_STATE_WAITING_HELLO ||
852 peer->peer_state == PEER_STATE_ACTIVE);
854 if (peer->peer_id.nid != id.nid ||
855 peer->peer_id.pid != id.pid)
858 kptllnd_peer_addref(peer);
860 CDEBUG(D_NET, "%s -> %s (%d)\n",
862 kptllnd_ptlid2str(peer->peer_ptlid),
863 atomic_read (&peer->peer_refcount));
871 kptllnd_peertable_overflow_msg(char *str, lnet_process_id_t id)
873 LCONSOLE_ERROR("%s %s overflows the peer table[%d]: "
874 "messages may be dropped\n",
875 str, libcfs_id2str(id),
876 kptllnd_data.kptl_n_active_peers);
877 LCONSOLE_ERROR("Please correct by increasing "
878 "'max_nodes' or 'max_procs_per_node'\n");
882 kptllnd_get_last_seen_matchbits_locked(lnet_process_id_t lpid)
885 struct list_head *tmp;
887 /* Find the last matchbits I saw this new peer using. Note..
888 A. This peer cannot be in the peer table - she's new!
889 B. If I can't find the peer in the closing/zombie peers, all
890 matchbits are safe because all refs to the (old) peer have gone
891 so all txs have completed so there's no risk of matchbit
895 LASSERT(kptllnd_id2peer_locked(lpid) == NULL);
897 /* peer's last matchbits can't change after it comes out of the peer
898 * table, so first match is fine */
900 list_for_each (tmp, &kptllnd_data.kptl_closing_peers) {
901 peer = list_entry (tmp, kptl_peer_t, peer_list);
903 if (peer->peer_id.nid == lpid.nid &&
904 peer->peer_id.pid == lpid.pid)
905 return peer->peer_last_matchbits_seen;
908 list_for_each (tmp, &kptllnd_data.kptl_zombie_peers) {
909 peer = list_entry (tmp, kptl_peer_t, peer_list);
911 if (peer->peer_id.nid == lpid.nid &&
912 peer->peer_id.pid == lpid.pid)
913 return peer->peer_last_matchbits_seen;
916 return PTL_RESERVED_MATCHBITS;
920 kptllnd_peer_handle_hello (ptl_process_id_t initiator,
923 rwlock_t *g_lock = &kptllnd_data.kptl_peer_rw_lock;
925 kptl_peer_t *new_peer;
926 lnet_process_id_t lpid;
930 __u64 safe_matchbits;
931 __u64 last_matchbits_seen;
933 lpid.nid = msg->ptlm_srcnid;
934 lpid.pid = msg->ptlm_srcpid;
936 CDEBUG(D_NET, "hello from %s(%s)\n",
937 libcfs_id2str(lpid), kptllnd_ptlid2str(initiator));
939 if (initiator.pid != kptllnd_data.kptl_portals_id.pid &&
940 (msg->ptlm_srcpid & LNET_PID_USERFLAG) == 0) {
941 /* If the peer's PID isn't _the_ ptllnd kernel pid, she must be
942 * userspace. Refuse the connection if she hasn't set the
943 * correct flag in her PID... */
944 CERROR("Userflag not set in hello from %s (%s)\n",
945 libcfs_id2str(lpid), kptllnd_ptlid2str(initiator));
949 /* kptlhm_matchbits are the highest matchbits my peer may have used to
950 * RDMA to me. I ensure I never register buffers for RDMA that could
951 * match any she used */
952 safe_matchbits = msg->ptlm_u.hello.kptlhm_matchbits + 1;
954 if (safe_matchbits < PTL_RESERVED_MATCHBITS) {
955 CERROR("Illegal matchbits "LPX64" in HELLO from %s\n",
956 safe_matchbits, libcfs_id2str(lpid));
960 if (msg->ptlm_u.hello.kptlhm_max_msg_size < PTLLND_MIN_BUFFER_SIZE) {
961 CERROR("%s: max message size %d < MIN %d",
963 msg->ptlm_u.hello.kptlhm_max_msg_size,
964 *kptllnd_tunables.kptl_max_msg_size);
968 if (msg->ptlm_credits <= 1) {
969 CERROR("Need more than 1+%d credits from %s\n",
970 msg->ptlm_credits, libcfs_id2str(lpid));
974 write_lock_irqsave(g_lock, flags);
976 peer = kptllnd_id2peer_locked(lpid);
978 if (peer->peer_state == PEER_STATE_WAITING_HELLO) {
979 /* Completing HELLO handshake */
980 LASSERT(peer->peer_incarnation == 0);
982 if (msg->ptlm_dststamp != 0 &&
983 msg->ptlm_dststamp != peer->peer_myincarnation) {
984 write_unlock_irqrestore(g_lock, flags);
986 CERROR("Ignoring HELLO from %s: unexpected "
987 "dststamp "LPX64" ("LPX64" wanted)\n",
990 peer->peer_myincarnation);
991 kptllnd_peer_decref(peer);
995 /* Concurrent initiation or response to my HELLO */
996 peer->peer_state = PEER_STATE_ACTIVE;
997 peer->peer_incarnation = msg->ptlm_srcstamp;
998 peer->peer_next_matchbits = safe_matchbits;
999 peer->peer_max_msg_size =
1000 msg->ptlm_u.hello.kptlhm_max_msg_size;
1002 write_unlock_irqrestore(g_lock, flags);
1006 if (msg->ptlm_dststamp != 0 &&
1007 msg->ptlm_dststamp <= peer->peer_myincarnation) {
1008 write_unlock_irqrestore(g_lock, flags);
1010 CERROR("Ignoring stale HELLO from %s: "
1011 "dststamp "LPX64" (current "LPX64")\n",
1012 libcfs_id2str(lpid),
1014 peer->peer_myincarnation);
1015 kptllnd_peer_decref(peer);
1019 /* Brand new connection attempt: remove old incarnation */
1020 kptllnd_peer_close_locked(peer, 0);
1023 kptllnd_cull_peertable_locked(lpid);
1025 write_unlock_irqrestore(g_lock, flags);
1028 CDEBUG(D_NET, "Peer %s (%s) reconnecting:"
1029 " stamp "LPX64"("LPX64")\n",
1030 libcfs_id2str(lpid), kptllnd_ptlid2str(initiator),
1031 msg->ptlm_srcstamp, peer->peer_incarnation);
1033 kptllnd_peer_decref(peer);
1036 hello_tx = kptllnd_get_idle_tx(TX_TYPE_SMALL_MESSAGE);
1037 if (hello_tx == NULL) {
1038 CERROR("Unable to allocate HELLO message for %s\n",
1039 libcfs_id2str(lpid));
1043 kptllnd_init_msg(hello_tx->tx_msg, PTLLND_MSG_TYPE_HELLO,
1044 sizeof(kptl_hello_msg_t));
1046 new_peer = kptllnd_peer_allocate(lpid, initiator);
1047 if (new_peer == NULL) {
1048 kptllnd_tx_decref(hello_tx);
1052 rc = kptllnd_peer_reserve_buffers();
1054 kptllnd_peer_decref(new_peer);
1055 kptllnd_tx_decref(hello_tx);
1057 CERROR("Failed to reserve buffers for %s\n",
1058 libcfs_id2str(lpid));
1062 write_lock_irqsave(g_lock, flags);
1064 peer = kptllnd_id2peer_locked(lpid);
1066 if (peer->peer_state == PEER_STATE_WAITING_HELLO) {
1067 /* An outgoing message instantiated 'peer' for me */
1068 LASSERT(peer->peer_incarnation == 0);
1070 peer->peer_state = PEER_STATE_ACTIVE;
1071 peer->peer_incarnation = msg->ptlm_srcstamp;
1072 peer->peer_next_matchbits = safe_matchbits;
1073 peer->peer_max_msg_size =
1074 msg->ptlm_u.hello.kptlhm_max_msg_size;
1076 write_unlock_irqrestore(g_lock, flags);
1078 CWARN("Outgoing instantiated peer %s\n",
1079 libcfs_id2str(lpid));
1081 LASSERT (peer->peer_state == PEER_STATE_ACTIVE);
1083 write_unlock_irqrestore(g_lock, flags);
1085 /* WOW! Somehow this peer completed the HELLO
1086 * handshake while I slept. I guess I could have slept
1087 * while it rebooted and sent a new HELLO, so I'll fail
1089 CWARN("Wow! peer %s\n", libcfs_id2str(lpid));
1090 kptllnd_peer_decref(peer);
1094 kptllnd_peer_unreserve_buffers();
1095 kptllnd_peer_decref(new_peer);
1096 kptllnd_tx_decref(hello_tx);
1100 if (kptllnd_data.kptl_n_active_peers ==
1101 kptllnd_data.kptl_expected_peers) {
1102 /* peer table full */
1103 write_unlock_irqrestore(g_lock, flags);
1105 kptllnd_peertable_overflow_msg("Connection from ", lpid);
1107 rc = kptllnd_reserve_buffers(1); /* HELLO headroom */
1109 CERROR("Refusing connection from %s\n",
1110 libcfs_id2str(lpid));
1111 kptllnd_peer_unreserve_buffers();
1112 kptllnd_peer_decref(new_peer);
1113 kptllnd_tx_decref(hello_tx);
1117 write_lock_irqsave(g_lock, flags);
1118 kptllnd_data.kptl_expected_peers++;
1122 last_matchbits_seen = kptllnd_get_last_seen_matchbits_locked(lpid);
1124 hello_tx->tx_msg->ptlm_u.hello.kptlhm_matchbits = last_matchbits_seen;
1125 hello_tx->tx_msg->ptlm_u.hello.kptlhm_max_msg_size =
1126 *kptllnd_tunables.kptl_max_msg_size;
1128 new_peer->peer_state = PEER_STATE_ACTIVE;
1129 new_peer->peer_incarnation = msg->ptlm_srcstamp;
1130 new_peer->peer_next_matchbits = safe_matchbits;
1131 new_peer->peer_last_matchbits_seen = last_matchbits_seen;
1132 new_peer->peer_max_msg_size = msg->ptlm_u.hello.kptlhm_max_msg_size;
1134 kptllnd_peer_add_peertable_locked(new_peer);
1136 write_unlock_irqrestore(g_lock, flags);
1138 /* NB someone else could get in now and post a message before I post
1139 * the HELLO, but post_tx/check_sends take care of that! */
1141 CDEBUG(D_NETTRACE, "%s: post response hello %p\n",
1142 libcfs_id2str(new_peer->peer_id), hello_tx);
1144 kptllnd_post_tx(new_peer, hello_tx, 0);
1145 kptllnd_peer_check_sends(new_peer);
1151 kptllnd_tx_launch(kptl_peer_t *peer, kptl_tx_t *tx, int nfrag)
1153 kptllnd_post_tx(peer, tx, nfrag);
1154 kptllnd_peer_check_sends(peer);
1158 kptllnd_find_target(kptl_peer_t **peerp, lnet_process_id_t target)
1160 rwlock_t *g_lock = &kptllnd_data.kptl_peer_rw_lock;
1161 ptl_process_id_t ptl_id;
1162 kptl_peer_t *new_peer;
1163 kptl_tx_t *hello_tx;
1164 unsigned long flags;
1166 __u64 last_matchbits_seen;
1168 /* I expect to find the peer, so I only take a read lock... */
1169 read_lock_irqsave(g_lock, flags);
1170 *peerp = kptllnd_id2peer_locked(target);
1171 read_unlock_irqrestore(g_lock, flags);
1176 if ((target.pid & LNET_PID_USERFLAG) != 0) {
1177 CWARN("Refusing to create a new connection to %s "
1178 "(non-kernel peer)\n", libcfs_id2str(target));
1179 return -EHOSTUNREACH;
1182 /* The new peer is a kernel ptllnd, and kernel ptllnds all have
1183 * the same portals PID */
1184 ptl_id.nid = kptllnd_lnet2ptlnid(target.nid);
1185 ptl_id.pid = kptllnd_data.kptl_portals_id.pid;
1187 hello_tx = kptllnd_get_idle_tx(TX_TYPE_SMALL_MESSAGE);
1188 if (hello_tx == NULL) {
1189 CERROR("Unable to allocate connect message for %s\n",
1190 libcfs_id2str(target));
1194 kptllnd_init_msg(hello_tx->tx_msg, PTLLND_MSG_TYPE_HELLO,
1195 sizeof(kptl_hello_msg_t));
1197 new_peer = kptllnd_peer_allocate(target, ptl_id);
1198 if (new_peer == NULL) {
1203 rc = kptllnd_peer_reserve_buffers();
1207 write_lock_irqsave(g_lock, flags);
1209 *peerp = kptllnd_id2peer_locked(target);
1210 if (*peerp != NULL) {
1211 write_unlock_irqrestore(g_lock, flags);
1215 kptllnd_cull_peertable_locked(target);
1217 if (kptllnd_data.kptl_n_active_peers ==
1218 kptllnd_data.kptl_expected_peers) {
1219 /* peer table full */
1220 write_unlock_irqrestore(g_lock, flags);
1222 kptllnd_peertable_overflow_msg("Connection to ", target);
1224 rc = kptllnd_reserve_buffers(1); /* HELLO headroom */
1226 CERROR("Can't create connection to %s\n",
1227 libcfs_id2str(target));
1231 write_lock_irqsave(g_lock, flags);
1232 kptllnd_data.kptl_expected_peers++;
1236 last_matchbits_seen = kptllnd_get_last_seen_matchbits_locked(target);
1238 hello_tx->tx_msg->ptlm_u.hello.kptlhm_matchbits = last_matchbits_seen;
1239 hello_tx->tx_msg->ptlm_u.hello.kptlhm_max_msg_size =
1240 *kptllnd_tunables.kptl_max_msg_size;
1242 new_peer->peer_state = PEER_STATE_WAITING_HELLO;
1243 new_peer->peer_last_matchbits_seen = last_matchbits_seen;
1245 kptllnd_peer_add_peertable_locked(new_peer);
1247 write_unlock_irqrestore(g_lock, flags);
1249 /* NB someone else could get in now and post a message before I post
1250 * the HELLO, but post_tx/check_sends take care of that! */
1252 CDEBUG(D_NETTRACE, "%s: post initial hello %p\n",
1253 libcfs_id2str(new_peer->peer_id), hello_tx);
1255 kptllnd_post_tx(new_peer, hello_tx, 0);
1256 kptllnd_peer_check_sends(new_peer);
1262 kptllnd_peer_unreserve_buffers();
1264 kptllnd_peer_decref(new_peer);
1266 kptllnd_tx_decref(hello_tx);