2 * Copyright (C) 2012 Cray, Inc.
4 * Copyright (c) 2013, 2017, Intel Corporation.
6 * Author: Nic Henke <nic@cray.com>
7 * Author: James Shimek <jshimek@cray.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /* Primary entry points from LNET. There are no guarantees against reentrance. */
28 struct lnet_lnd the_kgnilnd = {
30 .lnd_startup = kgnilnd_startup,
31 .lnd_shutdown = kgnilnd_shutdown,
32 .lnd_ctl = kgnilnd_ctl,
33 .lnd_send = kgnilnd_send,
34 .lnd_recv = kgnilnd_recv,
35 .lnd_eager_recv = kgnilnd_eager_recv,
36 .lnd_query = kgnilnd_query,
39 kgn_data_t kgnilnd_data;
42 kgnilnd_thread_start(int(*fn)(void *arg), void *arg, char *name, int id)
44 struct task_struct *thrd;
46 thrd = kthread_run(fn, arg, "%s_%02d", name, id);
50 atomic_inc(&kgnilnd_data.kgn_nthreads);
54 /* bind scheduler threads to cpus */
56 kgnilnd_start_sd_threads(void)
60 struct task_struct *task;
62 for_each_online_cpu(cpu) {
63 /* don't bind to cpu 0 - all interrupts are processed here */
67 task = kthread_create(kgnilnd_scheduler, (void *)((long)i),
68 "%s_%02d", "kgnilnd_sd", i);
70 kthread_bind(task, cpu);
71 wake_up_process(task);
73 CERROR("Can't spawn gnilnd scheduler[%d] %ld\n", i,
77 atomic_inc(&kgnilnd_data.kgn_nthreads);
79 if (++i >= *kgnilnd_tunables.kgn_sched_threads) {
87 /* needs write_lock on kgn_peer_conn_lock */
89 kgnilnd_close_stale_conns_locked(kgn_peer_t *peer, kgn_conn_t *newconn)
92 struct list_head *ctmp, *cnxt;
96 loopback = peer->gnp_nid == peer->gnp_net->gnn_ni->ni_nid;
98 list_for_each_safe(ctmp, cnxt, &peer->gnp_conns) {
99 conn = list_entry(ctmp, kgn_conn_t, gnc_list);
101 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
107 if (conn->gnc_device != newconn->gnc_device)
110 /* This is a two connection loopback - one talking to the other */
112 newconn->gnc_my_connstamp == conn->gnc_peer_connstamp &&
113 newconn->gnc_peer_connstamp == conn->gnc_my_connstamp) {
114 CDEBUG(D_NET, "skipping prune of %p, "
115 "loopback and matching stamps"
116 " connstamp %llu(%llu)"
117 " peerstamp %llu(%llu)\n",
118 conn, newconn->gnc_my_connstamp,
119 conn->gnc_peer_connstamp,
120 newconn->gnc_peer_connstamp,
121 conn->gnc_my_connstamp);
125 if (conn->gnc_peerstamp != newconn->gnc_peerstamp) {
126 LASSERTF(conn->gnc_peerstamp < newconn->gnc_peerstamp,
127 "conn 0x%p peerstamp %llu >= "
128 "newconn 0x%p peerstamp %llu\n",
129 conn, conn->gnc_peerstamp,
130 newconn, newconn->gnc_peerstamp);
132 CDEBUG(D_NET, "Closing stale conn nid: %s "
133 " peerstamp:%#llx(%#llx)\n",
134 libcfs_nid2str(peer->gnp_nid),
135 conn->gnc_peerstamp, newconn->gnc_peerstamp);
138 LASSERTF(conn->gnc_peer_connstamp < newconn->gnc_peer_connstamp,
139 "conn 0x%p peer_connstamp %llu >= "
140 "newconn 0x%p peer_connstamp %llu\n",
141 conn, conn->gnc_peer_connstamp,
142 newconn, newconn->gnc_peer_connstamp);
144 CDEBUG(D_NET, "Closing stale conn nid: %s"
145 " connstamp:%llu(%llu)\n",
146 libcfs_nid2str(peer->gnp_nid),
147 conn->gnc_peer_connstamp, newconn->gnc_peer_connstamp);
151 kgnilnd_close_conn_locked(conn, -ESTALE);
155 CWARN("Closed %d stale conns to %s\n", count, libcfs_nid2str(peer->gnp_nid));
162 kgnilnd_conn_isdup_locked(kgn_peer_t *peer, kgn_conn_t *newconn)
165 struct list_head *tmp;
169 loopback = peer->gnp_nid == peer->gnp_net->gnn_ni->ni_nid;
171 list_for_each(tmp, &peer->gnp_conns) {
172 conn = list_entry(tmp, kgn_conn_t, gnc_list);
173 CDEBUG(D_NET, "checking conn 0x%p for peer %s"
174 " lo %d new %llu existing %llu"
175 " new peer %llu existing peer %llu"
176 " new dev %p existing dev %p\n",
177 conn, libcfs_nid2str(peer->gnp_nid),
179 newconn->gnc_peerstamp, conn->gnc_peerstamp,
180 newconn->gnc_peer_connstamp, conn->gnc_peer_connstamp,
181 newconn->gnc_device, conn->gnc_device);
183 /* conn is in the process of closing */
184 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
187 /* 'newconn' is from an earlier version of 'peer'!!! */
188 if (newconn->gnc_peerstamp < conn->gnc_peerstamp)
191 /* 'conn' is from an earlier version of 'peer': it will be
192 * removed when we cull stale conns later on... */
193 if (newconn->gnc_peerstamp > conn->gnc_peerstamp)
196 /* Different devices are OK */
197 if (conn->gnc_device != newconn->gnc_device)
200 /* It's me connecting to myself */
202 newconn->gnc_my_connstamp == conn->gnc_peer_connstamp &&
203 newconn->gnc_peer_connstamp == conn->gnc_my_connstamp)
206 /* 'newconn' is an earlier connection from 'peer'!!! */
207 if (newconn->gnc_peer_connstamp < conn->gnc_peer_connstamp)
210 /* 'conn' is an earlier connection from 'peer': it will be
211 * removed when we cull stale conns later on... */
212 if (newconn->gnc_peer_connstamp > conn->gnc_peer_connstamp)
215 /* 'newconn' has the SAME connection stamp; 'peer' isn't
216 * playing the game... */
224 kgnilnd_create_conn(kgn_conn_t **connp, kgn_device_t *dev)
230 LASSERT (!in_interrupt());
231 atomic_inc(&kgnilnd_data.kgn_nconns);
233 /* divide by 2 to allow for complete reset and immediate reconnect */
234 if (atomic_read(&kgnilnd_data.kgn_nconns) >= GNILND_MAX_CQID/2) {
235 CERROR("Too many conn are live: %d > %d\n",
236 atomic_read(&kgnilnd_data.kgn_nconns), GNILND_MAX_CQID/2);
237 atomic_dec(&kgnilnd_data.kgn_nconns);
241 LIBCFS_ALLOC(conn, sizeof(*conn));
243 atomic_dec(&kgnilnd_data.kgn_nconns);
247 conn->gnc_tx_ref_table =
248 kgnilnd_vzalloc(GNILND_MAX_MSG_ID * sizeof(void *));
249 if (conn->gnc_tx_ref_table == NULL) {
250 CERROR("Can't allocate conn tx_ref_table\n");
251 GOTO(failed, rc = -ENOMEM);
254 mutex_init(&conn->gnc_smsg_mutex);
255 mutex_init(&conn->gnc_rdma_mutex);
256 atomic_set(&conn->gnc_refcount, 1);
257 atomic_set(&conn->gnc_reaper_noop, 0);
258 atomic_set(&conn->gnc_sched_noop, 0);
259 atomic_set(&conn->gnc_tx_in_use, 0);
260 INIT_LIST_HEAD(&conn->gnc_list);
261 INIT_LIST_HEAD(&conn->gnc_hashlist);
262 INIT_LIST_HEAD(&conn->gnc_schedlist);
263 INIT_LIST_HEAD(&conn->gnc_fmaq);
264 INIT_LIST_HEAD(&conn->gnc_mdd_list);
265 INIT_LIST_HEAD(&conn->gnc_delaylist);
266 spin_lock_init(&conn->gnc_list_lock);
267 spin_lock_init(&conn->gnc_tx_lock);
268 conn->gnc_magic = GNILND_CONN_MAGIC;
270 /* set tx id to nearly the end to make sure we find wrapping
272 conn->gnc_next_tx = (int) GNILND_MAX_MSG_ID - 10;
274 /* if this fails, we have conflicts and MAX_TX is too large */
275 CLASSERT(GNILND_MAX_MSG_ID < GNILND_MSGID_CLOSE);
277 /* get a new unique CQ id for this conn */
278 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
279 conn->gnc_my_connstamp = kgnilnd_data.kgn_connstamp++;
280 conn->gnc_cqid = kgnilnd_get_cqid_locked();
281 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
283 if (conn->gnc_cqid == 0) {
284 CERROR("Could not allocate unique CQ ID for conn 0x%p\n", conn);
285 GOTO(failed, rc = -E2BIG);
288 CDEBUG(D_NET, "alloc cqid %u for conn 0x%p\n",
289 conn->gnc_cqid, conn);
291 /* need to be set before gnc_ephandle to allow kgnilnd_destroy_conn_ep to
293 conn->gnc_device = dev;
295 conn->gnc_timeout = MAX(*kgnilnd_tunables.kgn_timeout,
297 kgnilnd_update_reaper_timeout(conn->gnc_timeout);
299 /* this is the ep_handle for doing SMSG & BTE */
300 mutex_lock(&dev->gnd_cq_mutex);
301 rrc = kgnilnd_ep_create(dev->gnd_handle, dev->gnd_snd_fma_cqh,
302 &conn->gnc_ephandle);
303 mutex_unlock(&dev->gnd_cq_mutex);
304 if (rrc != GNI_RC_SUCCESS)
305 GOTO(failed, rc = -ENETDOWN);
307 CDEBUG(D_NET, "created conn 0x%p ep_hndl 0x%p\n",
308 conn, conn->gnc_ephandle);
310 /* add ref for EP canceling */
311 kgnilnd_conn_addref(conn);
312 atomic_inc(&dev->gnd_neps);
318 atomic_dec(&kgnilnd_data.kgn_nconns);
319 kgnilnd_vfree(conn->gnc_tx_ref_table,
320 GNILND_MAX_MSG_ID * sizeof(void *));
321 LIBCFS_FREE(conn, sizeof(*conn));
325 /* needs to be called with kgn_peer_conn_lock held (read or write) */
327 kgnilnd_find_conn_locked(kgn_peer_t *peer)
329 kgn_conn_t *conn = NULL;
331 /* if we are in reset, this conn is going to die soon */
332 if (unlikely(kgnilnd_data.kgn_in_reset)) {
336 /* just return the first ESTABLISHED connection */
337 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
338 /* kgnilnd_finish_connect doesn't put connections on the
339 * peer list until they are actually established */
340 LASSERTF(conn->gnc_state >= GNILND_CONN_ESTABLISHED,
341 "found conn %p state %s on peer %p (%s)\n",
342 conn, kgnilnd_conn_state2str(conn), peer,
343 libcfs_nid2str(peer->gnp_nid));
344 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
352 /* needs write_lock on kgn_peer_conn_lock held */
354 kgnilnd_find_or_create_conn_locked(kgn_peer_t *peer) {
356 kgn_device_t *dev = peer->gnp_net->gnn_dev;
359 conn = kgnilnd_find_conn_locked(peer);
365 /* if the peer was previously connecting, check if we should
366 * trigger another connection attempt yet. */
367 if (time_before(jiffies, peer->gnp_reconnect_time)) {
371 /* This check prevents us from creating a new connection to a peer while we are
372 * still in the process of closing an existing connection to the peer.
374 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
375 if (conn->gnc_ephandle != NULL) {
376 CDEBUG(D_NET, "Not connecting non-null ephandle found peer 0x%p->%s\n", peer,
377 libcfs_nid2str(peer->gnp_nid));
382 if (peer->gnp_connecting != GNILND_PEER_IDLE) {
383 /* if we are not connecting, fire up a new connection */
384 /* or if we are anything but IDLE DONT start a new connection */
388 CDEBUG(D_NET, "starting connect to %s\n",
389 libcfs_nid2str(peer->gnp_nid));
390 peer->gnp_connecting = GNILND_PEER_CONNECT;
391 kgnilnd_peer_addref(peer); /* extra ref for connd */
393 spin_lock(&dev->gnd_connd_lock);
394 list_add_tail(&peer->gnp_connd_list, &dev->gnd_connd_peers);
395 spin_unlock(&dev->gnd_connd_lock);
397 kgnilnd_schedule_dgram(dev);
398 CDEBUG(D_NETTRACE, "scheduling new connect\n");
403 /* Caller is responsible for deciding if/when to call this */
405 kgnilnd_destroy_conn_ep(kgn_conn_t *conn)
408 gni_ep_handle_t tmp_ep;
410 /* only if we actually initialized it,
411 * then set NULL to tell kgnilnd_destroy_conn to leave it alone */
413 tmp_ep = xchg(&conn->gnc_ephandle, NULL);
414 if (tmp_ep != NULL) {
415 /* we never re-use the EP, so unbind is not needed */
416 mutex_lock(&conn->gnc_device->gnd_cq_mutex);
417 rrc = kgnilnd_ep_destroy(tmp_ep);
419 mutex_unlock(&conn->gnc_device->gnd_cq_mutex);
421 /* if this fails, it could hork up kgni smsg retransmit and others
422 * since we could free the SMSG mbox memory, etc. */
423 LASSERTF(rrc == GNI_RC_SUCCESS, "rrc %d conn 0x%p ep 0x%p\n",
424 rrc, conn, conn->gnc_ephandle);
426 atomic_dec(&conn->gnc_device->gnd_neps);
428 /* clear out count added in kgnilnd_close_conn_locked
429 * conn will have a peer once it hits finish_connect, where it
430 * is the first spot we'll mark it ESTABLISHED as well */
431 if (conn->gnc_peer) {
432 kgnilnd_admin_decref(conn->gnc_peer->gnp_dirty_eps);
435 /* drop ref for EP */
436 kgnilnd_conn_decref(conn);
441 kgnilnd_destroy_conn(kgn_conn_t *conn)
443 LASSERTF(!in_interrupt() &&
444 !conn->gnc_scheduled &&
445 !conn->gnc_in_purgatory &&
446 conn->gnc_ephandle == NULL &&
447 list_empty(&conn->gnc_list) &&
448 list_empty(&conn->gnc_hashlist) &&
449 list_empty(&conn->gnc_schedlist) &&
450 list_empty(&conn->gnc_mdd_list) &&
451 list_empty(&conn->gnc_delaylist) &&
452 conn->gnc_magic == GNILND_CONN_MAGIC,
453 "conn 0x%p->%s IRQ %d sched %d purg %d ep 0x%p Mg %d lists %d/%d/%d/%d/%d\n",
454 conn, conn->gnc_peer ? libcfs_nid2str(conn->gnc_peer->gnp_nid)
456 !!in_interrupt(), conn->gnc_scheduled,
457 conn->gnc_in_purgatory,
460 list_empty(&conn->gnc_list),
461 list_empty(&conn->gnc_hashlist),
462 list_empty(&conn->gnc_schedlist),
463 list_empty(&conn->gnc_mdd_list),
464 list_empty(&conn->gnc_delaylist));
466 /* Tripping these is especially bad, as it means we have items on the
467 * lists that didn't keep their refcount on the connection - or
468 * somebody evil released their own */
469 LASSERTF(list_empty(&conn->gnc_fmaq) &&
470 atomic_read(&conn->gnc_nlive_fma) == 0 &&
471 atomic_read(&conn->gnc_nlive_rdma) == 0,
472 "conn 0x%p fmaq %d@0x%p nfma %d nrdma %d\n",
473 conn, kgnilnd_count_list(&conn->gnc_fmaq), &conn->gnc_fmaq,
474 atomic_read(&conn->gnc_nlive_fma), atomic_read(&conn->gnc_nlive_rdma));
476 CDEBUG(D_NET, "destroying conn %p ephandle %p error %d\n",
477 conn, conn->gnc_ephandle, conn->gnc_error);
479 /* We are freeing this memory remove the magic value from the connection */
482 /* if there is an FMA blk left here, we'll tear it down */
483 if (conn->gnc_fma_blk) {
484 if (conn->gnc_peer) {
485 kgn_mbox_info_t *mbox;
486 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
487 mbox->mbx_prev_nid = conn->gnc_peer->gnp_nid;
489 kgnilnd_release_mbox(conn, 0);
492 if (conn->gnc_peer != NULL)
493 kgnilnd_peer_decref(conn->gnc_peer);
495 if (conn->gnc_tx_ref_table != NULL) {
496 kgnilnd_vfree(conn->gnc_tx_ref_table,
497 GNILND_MAX_MSG_ID * sizeof(void *));
500 LIBCFS_FREE(conn, sizeof(*conn));
501 atomic_dec(&kgnilnd_data.kgn_nconns);
504 /* peer_alive and peer_notify done in the style of the o2iblnd */
506 kgnilnd_peer_alive(kgn_peer_t *peer)
508 time64_t now = ktime_get_seconds();
510 set_mb(peer->gnp_last_alive, now);
514 kgnilnd_peer_notify(kgn_peer_t *peer, int error, int alive)
525 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_DONT_NOTIFY))
528 /* Tell LNet we are giving ups on this peer - but only
529 * if it isn't already reconnected or trying to reconnect */
530 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
532 /* use kgnilnd_find_conn_locked to avoid any conns in the process of being nuked
534 * don't tell LNet if we are in reset - we assume that everyone will be able to
535 * reconnect just fine
537 conn = kgnilnd_find_conn_locked(peer);
539 CDEBUG(D_NETTRACE, "peer 0x%p->%s ting %d conn 0x%p, rst %d error %d\n",
540 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting, conn,
541 kgnilnd_data.kgn_in_reset, error);
543 if (((peer->gnp_connecting == GNILND_PEER_IDLE) &&
545 (!kgnilnd_data.kgn_in_reset) &&
546 (!kgnilnd_conn_clean_errno(error))) || alive) {
550 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
553 /* short circuit if we dont need to notify Lnet */
557 rc = down_read_trylock(&kgnilnd_data.kgn_net_rw_sem);
560 /* dont do this if this fails since LNET is in shutdown or something else
563 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
564 list_for_each_entry(net , &kgnilnd_data.kgn_nets[i], gnn_list) {
565 /* if gnn_shutdown set for any net shutdown is in progress just return */
566 if (net->gnn_shutdown) {
567 up_read(&kgnilnd_data.kgn_net_rw_sem);
575 /* shutdown in progress most likely */
576 up_read(&kgnilnd_data.kgn_net_rw_sem);
580 LIBCFS_ALLOC(nets, nnets * sizeof(*nets));
583 up_read(&kgnilnd_data.kgn_net_rw_sem);
584 CERROR("Failed to allocate nets[%d]\n", nnets);
589 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
590 list_for_each_entry(net, &kgnilnd_data.kgn_nets[i], gnn_list) {
592 kgnilnd_net_addref(net);
596 up_read(&kgnilnd_data.kgn_net_rw_sem);
598 for (i = 0; i < nnets; i++) {
603 peer_nid = kgnilnd_lnd2lnetnid(net->gnn_ni->ni_nid,
606 CDEBUG(D_NET, "peer 0x%p->%s last_alive %lld (%llds ago)\n",
607 peer, libcfs_nid2str(peer_nid), peer->gnp_last_alive,
608 ktime_get_seconds() - peer->gnp_last_alive);
610 lnet_notify(net->gnn_ni, peer_nid, alive,
611 peer->gnp_last_alive);
613 kgnilnd_net_decref(net);
616 LIBCFS_FREE(nets, nnets * sizeof(*nets));
620 /* need write_lock on kgn_peer_conn_lock */
622 kgnilnd_close_conn_locked(kgn_conn_t *conn, int error)
624 kgn_peer_t *peer = conn->gnc_peer;
627 LASSERT(!in_interrupt());
629 /* store error for tx completion */
630 conn->gnc_error = error;
631 peer->gnp_last_errno = error;
633 /* use real error from peer if possible */
634 if (error == -ECONNRESET) {
635 error = conn->gnc_peer_error;
638 /* if we NETERROR, make sure it is rate limited */
639 if (!kgnilnd_conn_clean_errno(error) &&
640 peer->gnp_state != GNILND_PEER_DOWN) {
641 CNETERR("closing conn to %s: error %d\n",
642 libcfs_nid2str(peer->gnp_nid), error);
644 CDEBUG(D_NET, "closing conn to %s: error %d\n",
645 libcfs_nid2str(peer->gnp_nid), error);
648 LASSERTF(conn->gnc_state == GNILND_CONN_ESTABLISHED,
649 "conn %p to %s with bogus state %s\n", conn,
650 libcfs_nid2str(conn->gnc_peer->gnp_nid),
651 kgnilnd_conn_state2str(conn));
652 LASSERT(!list_empty(&conn->gnc_hashlist));
653 LASSERT(!list_empty(&conn->gnc_list));
656 /* mark peer count here so any place the EP gets destroyed will
657 * open up the peer count so that a new ESTABLISHED conn is then free
658 * to send new messages -- sending before the previous EPs are destroyed
659 * could end up with messages on the network for the old conn _after_
660 * the new conn and break the mbox safety protocol */
661 kgnilnd_admin_addref(conn->gnc_peer->gnp_dirty_eps);
663 /* Remove from conn hash table: no new callbacks */
664 list_del_init(&conn->gnc_hashlist);
665 kgnilnd_data.kgn_conn_version++;
666 kgnilnd_conn_decref(conn);
668 /* if we are in reset, go right to CLOSED as there is no scheduler
669 * thread to move from CLOSING to CLOSED */
670 if (unlikely(kgnilnd_data.kgn_in_reset)) {
671 conn->gnc_state = GNILND_CONN_CLOSED;
673 conn->gnc_state = GNILND_CONN_CLOSING;
676 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_RDMA_CQ_ERROR)) {
677 msleep_interruptible(MSEC_PER_SEC);
680 /* leave on peer->gnp_conns to make sure we don't let the reaper
681 * or others try to unlink this peer until the conn is fully
682 * processed for closing */
684 if (kgnilnd_check_purgatory_conn(conn)) {
685 kgnilnd_add_purgatory_locked(conn, conn->gnc_peer);
688 /* Reset RX timeout to ensure we wait for an incoming CLOSE
689 * for the full timeout. If we get a CLOSE we know the
690 * peer has stopped all RDMA. Otherwise if we wait for
691 * the full timeout we can also be sure all RDMA has stopped. */
692 conn->gnc_last_rx = conn->gnc_last_rx_cq = jiffies;
695 /* schedule sending CLOSE - if we are in quiesce, this adds to
696 * gnd_ready_conns and allows us to find it in quiesce processing */
697 kgnilnd_schedule_conn(conn);
703 kgnilnd_close_conn(kgn_conn_t *conn, int error)
705 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
706 /* need to check the state here - this call is racy and we don't
707 * know the state until after the lock is grabbed */
708 if (conn->gnc_state == GNILND_CONN_ESTABLISHED) {
709 kgnilnd_close_conn_locked(conn, error);
711 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
715 kgnilnd_complete_closed_conn(kgn_conn_t *conn)
725 /* Dump log on cksum error - wait until complete phase to let
726 * RX of error happen */
727 if (*kgnilnd_tunables.kgn_checksum_dump &&
728 (conn != NULL && conn->gnc_peer_error == -ENOKEY)) {
729 libcfs_debug_dumplog();
732 /* _CLOSED set in kgnilnd_process_fmaq once we decide to
733 * send the CLOSE or not */
734 LASSERTF(conn->gnc_state == GNILND_CONN_CLOSED,
735 "conn 0x%p->%s with bad state %s\n",
736 conn, conn->gnc_peer ?
737 libcfs_nid2str(conn->gnc_peer->gnp_nid) :
739 kgnilnd_conn_state2str(conn));
741 LASSERT(list_empty(&conn->gnc_hashlist));
742 /* We shouldnt be on the delay list, the conn can
743 * get added to this list during a retransmit, and retransmits
744 * only occur within scheduler threads.
746 LASSERT(list_empty(&conn->gnc_delaylist));
748 /* we've sent the close, start nuking */
749 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_SCHEDULE_COMPLETE))
750 kgnilnd_schedule_conn(conn);
752 if (conn->gnc_scheduled != GNILND_CONN_PROCESS) {
753 CDEBUG(D_NETERROR, "Error someone scheduled us after we were "
754 "done, Attempting to recover conn 0x%p "
755 "scheduled %d function: %s line: %d\n", conn,
756 conn->gnc_scheduled, conn->gnc_sched_caller,
757 conn->gnc_sched_line);
761 /* we don't use lists to track things that we can get out of the
764 /* need to hold locks for tx_list_state, sampling it is too racy:
765 * - the lock actually protects tx != NULL, but we can't take the proper
766 * lock until we check tx_list_state, which would be too late and
767 * we could have the TX change under us.
768 * gnd_rdmaq_lock and gnd_lock and not used together, so taking both
770 spin_lock(&conn->gnc_device->gnd_rdmaq_lock);
771 spin_lock(&conn->gnc_device->gnd_lock);
773 for (nrdma = 0; nrdma < GNILND_MAX_MSG_ID; nrdma++) {
774 tx = conn->gnc_tx_ref_table[nrdma];
777 /* only print the first error and if not CLOSE, we often don't see
778 * CQ events for that by the time we get here... and really don't care */
779 if (nlive || tx->tx_msg.gnm_type == GNILND_MSG_CLOSE)
780 tx->tx_state |= GNILND_TX_QUIET_ERROR;
782 GNIDBG_TX(D_NET, tx, "cleaning up on close, nlive %d", nlive);
784 /* don't worry about gnc_lock here as nobody else should be
785 * touching this conn */
786 kgnilnd_tx_del_state_locked(tx, NULL, conn, GNILND_TX_ALLOCD);
787 list_add_tail(&tx->tx_list, &sinners);
790 spin_unlock(&conn->gnc_device->gnd_lock);
791 spin_unlock(&conn->gnc_device->gnd_rdmaq_lock);
793 /* nobody should have marked this as needing scheduling after
794 * we called close - so only ref should be us handling it */
795 if (conn->gnc_scheduled != GNILND_CONN_PROCESS) {
796 CDEBUG(D_NETERROR, "Error someone scheduled us after we were "
797 "done, Attempting to recover conn 0x%p "
798 "scheduled %d function %s line: %d\n", conn,
799 conn->gnc_scheduled, conn->gnc_sched_caller,
800 conn->gnc_sched_line);
802 /* now reset a few to actual counters... */
803 nrdma = atomic_read(&conn->gnc_nlive_rdma);
804 nq_rdma = atomic_read(&conn->gnc_nq_rdma);
806 if (!list_empty(&sinners)) {
807 list_for_each_entry_safe(tx, txn, &sinners, tx_list) {
808 /* clear tx_list to make tx_add_list_locked happy */
809 list_del_init(&tx->tx_list);
810 /* The error codes determine if we hold onto the MDD */
811 kgnilnd_tx_done(tx, conn->gnc_error);
815 logmsg = (nlive + nrdma + nq_rdma);
818 int level = conn->gnc_peer->gnp_state == GNILND_PEER_UP ?
820 CDEBUG(level, "Closed conn 0x%p->%s (errno %d,"
821 " peer errno %d): canceled %d TX, %d/%d RDMA\n",
822 conn, libcfs_nid2str(conn->gnc_peer->gnp_nid),
823 conn->gnc_error, conn->gnc_peer_error,
824 nlive, nq_rdma, nrdma);
827 kgnilnd_destroy_conn_ep(conn);
829 /* Bug 765042 - race this with completing a new conn to same peer - we need
830 * finish_connect to detach purgatory before we can do it ourselves here */
831 CFS_RACE(CFS_FAIL_GNI_FINISH_PURG);
833 /* now it is safe to remove from peer list - anyone looking at
834 * gnp_conns now is free to unlink if not on purgatory */
835 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
837 conn->gnc_state = GNILND_CONN_DONE;
839 /* Decrement counter if we are marked by del_conn_or_peers for closing
841 if (conn->gnc_needs_closing)
842 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_conns);
844 /* Remove from peer's list of valid connections if its not in purgatory */
845 if (!conn->gnc_in_purgatory) {
846 list_del_init(&conn->gnc_list);
847 /* Lose peers reference on the conn */
848 kgnilnd_conn_decref(conn);
851 /* NB - only unlinking if we set pending in del_peer_locked from admin or
853 if (kgnilnd_peer_active(conn->gnc_peer) &&
854 conn->gnc_peer->gnp_pending_unlink &&
855 kgnilnd_can_unlink_peer_locked(conn->gnc_peer)) {
856 kgnilnd_unlink_peer_locked(conn->gnc_peer);
859 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
861 /* I'm telling Mommy! - use peer_error if they initiated close */
862 kgnilnd_peer_notify(conn->gnc_peer,
863 conn->gnc_error == -ECONNRESET ?
864 conn->gnc_peer_error : conn->gnc_error, 0);
870 kgnilnd_set_conn_params(kgn_dgram_t *dgram)
872 kgn_conn_t *conn = dgram->gndg_conn;
873 kgn_connreq_t *connreq = &dgram->gndg_conn_in;
874 kgn_gniparams_t *rem_param = &connreq->gncr_gnparams;
877 gni_smsg_attr_t *remote = &connreq->gncr_gnparams.gnpr_smsg_attr;
879 /* set timeout vals in conn early so we can use them for the NAK */
881 /* use max of the requested and our timeout, peer will do the same */
882 conn->gnc_timeout = MAX(conn->gnc_timeout, connreq->gncr_timeout);
884 /* only ep_bind really mucks around with the CQ */
885 /* only ep bind if we are not connecting to ourself and the dstnid is not a wildcard. this check
886 * is necessary as you can only bind an ep once and we must make sure we dont bind when already bound.
888 if (connreq->gncr_dstnid != LNET_NID_ANY && dgram->gndg_conn_out.gncr_dstnid != connreq->gncr_srcnid) {
889 mutex_lock(&conn->gnc_device->gnd_cq_mutex);
890 rrc = kgnilnd_ep_bind(conn->gnc_ephandle,
891 connreq->gncr_gnparams.gnpr_host_id,
893 mutex_unlock(&conn->gnc_device->gnd_cq_mutex);
894 if (rrc != GNI_RC_SUCCESS) {
900 rrc = kgnilnd_ep_set_eventdata(conn->gnc_ephandle, conn->gnc_cqid,
901 connreq->gncr_gnparams.gnpr_cqid);
902 if (rrc != GNI_RC_SUCCESS) {
907 /* Initialize SMSG */
908 rrc = kgnilnd_smsg_init(conn->gnc_ephandle, &conn->gnpr_smsg_attr,
909 &connreq->gncr_gnparams.gnpr_smsg_attr);
910 if (unlikely(rrc == GNI_RC_INVALID_PARAM)) {
911 gni_smsg_attr_t *local = &conn->gnpr_smsg_attr;
912 /* help folks figure out if there is a tunable off, etc. */
913 LCONSOLE_ERROR("SMSG attribute mismatch. Data from local/remote:"
914 " type %d/%d msg_maxsize %u/%u"
915 " mbox_maxcredit %u/%u. Please check kgni"
916 " logs for further data\n",
917 local->msg_type, remote->msg_type,
918 local->msg_maxsize, remote->msg_maxsize,
919 local->mbox_maxcredit, remote->mbox_maxcredit);
921 if (rrc != GNI_RC_SUCCESS) {
926 /* log this for help in debuggin SMSG buffer re-use */
927 CDEBUG(D_NET, "conn %p src %s dst %s smsg %p acquired"
928 " local cqid %u SMSG %p->%u hndl %#llx.%#llx"
929 " remote cqid %u SMSG %p->%u hndl %#llx.%#llx\n",
930 conn, libcfs_nid2str(connreq->gncr_srcnid),
931 libcfs_nid2str(connreq->gncr_dstnid),
932 &conn->gnpr_smsg_attr,
934 conn->gnpr_smsg_attr.msg_buffer,
935 conn->gnpr_smsg_attr.mbox_offset,
936 conn->gnpr_smsg_attr.mem_hndl.qword1,
937 conn->gnpr_smsg_attr.mem_hndl.qword2,
938 rem_param->gnpr_cqid,
939 rem_param->gnpr_smsg_attr.msg_buffer,
940 rem_param->gnpr_smsg_attr.mbox_offset,
941 rem_param->gnpr_smsg_attr.mem_hndl.qword1,
942 rem_param->gnpr_smsg_attr.mem_hndl.qword2);
944 conn->gnc_peerstamp = connreq->gncr_peerstamp;
945 conn->gnc_peer_connstamp = connreq->gncr_connstamp;
946 conn->remote_mbox_addr = (void *)((char *)remote->msg_buffer + remote->mbox_offset);
948 /* We update the reaper timeout once we have a valid conn and timeout */
949 kgnilnd_update_reaper_timeout(GNILND_TO2KA(conn->gnc_timeout));
954 rrc = kgnilnd_ep_unbind(conn->gnc_ephandle);
955 /* not sure I can just let this fly */
956 LASSERTF(rrc == GNI_RC_SUCCESS,
957 "bad rc from gni_ep_unbind trying to cleanup: %d\n", rrc);
960 LASSERTF(rc != 0, "SOFTWARE BUG: rc == 0\n");
961 CERROR("Error setting connection params from %s: %d\n",
962 libcfs_nid2str(connreq->gncr_srcnid), rc);
966 /* needs down_read on kgn_net_rw_sem held from before this call until
967 * after the write_lock on kgn_peer_conn_lock - this ensures we stay sane
968 * with kgnilnd_shutdown - it'll get the sem and set shutdown, then get the
969 * kgn_peer_conn_lock to start del_peer'ing. If we hold the sem until after
970 * kgn_peer_conn_lock is held, we guarantee that nobody calls
971 * kgnilnd_add_peer_locked without checking gnn_shutdown */
973 kgnilnd_create_peer_safe(kgn_peer_t **peerp,
981 LASSERT(nid != LNET_NID_ANY);
983 /* We dont pass the net around in the dgram anymore so here is where we find it
984 * this will work unless its in shutdown or the nid has a net that is invalid.
985 * Either way error code needs to be returned in that case.
987 * If the net passed in is not NULL then we can use it, this alleviates looking it
988 * when the calling function has access to the data.
991 rc = kgnilnd_find_net(nid, &net);
995 /* find net adds a reference on the net if we are not using
996 * it we must do it manually so the net references are
997 * correct when tearing down the net
999 kgnilnd_net_addref(net);
1002 LIBCFS_ALLOC(peer, sizeof(*peer));
1004 kgnilnd_net_decref(net);
1007 peer->gnp_nid = nid;
1008 peer->gnp_state = node_state;
1010 /* translate from nid to nic addr & store */
1011 rc = kgnilnd_nid_to_nicaddrs(LNET_NIDADDR(nid), 1, &peer->gnp_host_id);
1013 kgnilnd_net_decref(net);
1014 LIBCFS_FREE(peer, sizeof(*peer));
1017 CDEBUG(D_NET, "peer 0x%p->%s -> NIC 0x%x\n", peer,
1018 libcfs_nid2str(nid), peer->gnp_host_id);
1020 atomic_set(&peer->gnp_refcount, 1); /* 1 ref for caller */
1021 atomic_set(&peer->gnp_dirty_eps, 0);
1023 INIT_LIST_HEAD(&peer->gnp_list);
1024 INIT_LIST_HEAD(&peer->gnp_connd_list);
1025 INIT_LIST_HEAD(&peer->gnp_conns);
1026 INIT_LIST_HEAD(&peer->gnp_tx_queue);
1028 /* the first reconnect should happen immediately, so we leave
1029 * gnp_reconnect_interval set to 0 */
1031 LASSERTF(net != NULL, "peer 0x%p->%s with NULL net\n",
1032 peer, libcfs_nid2str(nid));
1034 /* must have kgn_net_rw_sem held for this... */
1035 if (net->gnn_shutdown) {
1036 /* shutdown has started already */
1037 kgnilnd_net_decref(net);
1038 LIBCFS_FREE(peer, sizeof(*peer));
1042 peer->gnp_net = net;
1044 atomic_inc(&kgnilnd_data.kgn_npeers);
1051 kgnilnd_destroy_peer(kgn_peer_t *peer)
1053 CDEBUG(D_NET, "peer %s %p deleted\n",
1054 libcfs_nid2str(peer->gnp_nid), peer);
1055 LASSERTF(atomic_read(&peer->gnp_refcount) == 0,
1056 "peer 0x%p->%s refs %d\n",
1057 peer, libcfs_nid2str(peer->gnp_nid),
1058 atomic_read(&peer->gnp_refcount));
1059 LASSERTF(atomic_read(&peer->gnp_dirty_eps) == 0,
1060 "peer 0x%p->%s dirty eps %d\n",
1061 peer, libcfs_nid2str(peer->gnp_nid),
1062 atomic_read(&peer->gnp_dirty_eps));
1063 LASSERTF(peer->gnp_net != NULL, "peer %p (%s) with NULL net\n",
1064 peer, libcfs_nid2str(peer->gnp_nid));
1065 LASSERTF(!kgnilnd_peer_active(peer),
1067 peer, libcfs_nid2str(peer->gnp_nid));
1068 LASSERTF(peer->gnp_connecting == GNILND_PEER_IDLE || peer->gnp_connecting == GNILND_PEER_KILL,
1069 "peer 0x%p->%s, connecting %d\n",
1070 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting);
1071 LASSERTF(list_empty(&peer->gnp_conns),
1073 peer, libcfs_nid2str(peer->gnp_nid));
1074 LASSERTF(list_empty(&peer->gnp_tx_queue),
1076 peer, libcfs_nid2str(peer->gnp_nid));
1077 LASSERTF(list_empty(&peer->gnp_connd_list),
1079 peer, libcfs_nid2str(peer->gnp_nid));
1081 /* NB a peer's connections keep a reference on their peer until
1082 * they are destroyed, so we can be assured that _all_ state to do
1083 * with this peer has been cleaned up when its refcount drops to
1086 atomic_dec(&kgnilnd_data.kgn_npeers);
1087 kgnilnd_net_decref(peer->gnp_net);
1089 LIBCFS_FREE(peer, sizeof(*peer));
1092 /* the conn might not have made it all the way through to a connected
1093 * state - but we need to purgatory any conn that a remote peer might
1094 * have seen through a posted dgram as well */
1096 kgnilnd_add_purgatory_locked(kgn_conn_t *conn, kgn_peer_t *peer)
1098 kgn_mbox_info_t *mbox = NULL;
1101 /* NB - the caller should own conn by removing him from the
1102 * scheduler thread when finishing the close */
1104 LASSERTF(peer != NULL, "conn %p with NULL peer\n", conn);
1106 /* If this is still true, need to add the calls to unlink back in and
1107 * figure out how to close the hole on loopback conns */
1108 LASSERTF(kgnilnd_peer_active(peer), "can't use inactive peer %s (%p)"
1109 " we'll never recover the resources\n",
1110 libcfs_nid2str(peer->gnp_nid), peer);
1112 CDEBUG(D_NET, "conn %p peer %p dev %p\n", conn, peer,
1115 LASSERTF(conn->gnc_in_purgatory == 0,
1116 "Conn already in purgatory\n");
1117 conn->gnc_in_purgatory = 1;
1119 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
1120 mbox->mbx_prev_purg_nid = peer->gnp_nid;
1121 mbox->mbx_add_purgatory = jiffies;
1122 kgnilnd_release_mbox(conn, 1);
1124 LASSERTF(list_empty(&conn->gnc_mdd_list),
1125 "conn 0x%p->%s with active purgatory hold MDD %d\n",
1126 conn, libcfs_nid2str(peer->gnp_nid),
1127 kgnilnd_count_list(&conn->gnc_mdd_list));
1132 /* Instead of detaching everything from purgatory here we just mark the conn as needing
1133 * detach, when the reaper checks the conn the next time it will detach it.
1134 * Calling function requires write_lock held on kgn_peer_conn_lock
1137 kgnilnd_mark_for_detach_purgatory_all_locked(kgn_peer_t *peer) {
1140 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
1141 if (conn->gnc_in_purgatory && !conn->gnc_needs_detach) {
1142 conn->gnc_needs_detach = 1;
1143 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_detach);
1148 /* Calling function needs a write_lock held on kgn_peer_conn_lock */
1150 kgnilnd_detach_purgatory_locked(kgn_conn_t *conn, struct list_head *conn_list)
1152 kgn_mbox_info_t *mbox = NULL;
1154 /* if needed, add the conn purgatory data to the list passed in */
1155 if (conn->gnc_in_purgatory) {
1156 CDEBUG(D_NET, "peer %p->%s purg_conn %p@%s mdd_list #tx %d\n",
1157 conn->gnc_peer, libcfs_nid2str(conn->gnc_peer->gnp_nid),
1158 conn, kgnilnd_conn_state2str(conn),
1159 kgnilnd_count_list(&conn->gnc_mdd_list));
1161 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
1162 mbox->mbx_detach_of_purgatory = jiffies;
1164 /* conn->gnc_list is the entry point on peer->gnp_conns, so detaching it
1165 * here removes it from the list of 'valid' peer connections.
1166 * We put the current conn onto a list of conns to call kgnilnd_release_purgatory_locked()
1167 * and as such the caller of kgnilnd_detach_purgatory_locked() now owns that conn, since its not
1168 * on the peer's conn_list anymore.
1171 list_del_init(&conn->gnc_list);
1173 /* NB - only unlinking if we set pending in del_peer_locked from admin or
1175 if (kgnilnd_peer_active(conn->gnc_peer) &&
1176 conn->gnc_peer->gnp_pending_unlink &&
1177 kgnilnd_can_unlink_peer_locked(conn->gnc_peer)) {
1178 kgnilnd_unlink_peer_locked(conn->gnc_peer);
1180 /* The reaper will not call detach unless the conn is fully through kgnilnd_complete_closed_conn.
1181 * If the conn is not in a DONE state somehow we are attempting to detach even though
1182 * the conn has not been fully cleaned up. If we detach while the conn is still closing
1183 * we will end up with an orphaned connection that has valid ep_handle, that is not on a
1187 LASSERTF(conn->gnc_state == GNILND_CONN_DONE, "Conn in invalid state %p@%s \n",
1188 conn, kgnilnd_conn_state2str(conn));
1190 /* move from peer to the delayed release list */
1191 list_add_tail(&conn->gnc_list, conn_list);
1196 kgnilnd_release_purgatory_list(struct list_head *conn_list)
1199 kgn_conn_t *conn, *connN;
1200 kgn_mdd_purgatory_t *gmp, *gmpN;
1202 list_for_each_entry_safe(conn, connN, conn_list, gnc_list) {
1203 dev = conn->gnc_device;
1205 kgnilnd_release_mbox(conn, -1);
1206 conn->gnc_in_purgatory = 0;
1208 list_del_init(&conn->gnc_list);
1210 /* gnc_needs_detach is set in kgnilnd_del_conn_or_peer. It is used to keep track
1211 * of conns that have been marked for detach by kgnilnd_del_conn_or_peer.
1212 * The function uses kgn_npending_detach to verify the conn has
1213 * actually been detached.
1216 if (conn->gnc_needs_detach)
1217 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_detach);
1219 /* if this guy is really dead (we are doing release from reaper),
1220 * make sure we tell LNet - if this is from other context,
1221 * the checks in the function will prevent an errant
1223 kgnilnd_peer_notify(conn->gnc_peer, conn->gnc_error, 0);
1225 list_for_each_entry_safe(gmp, gmpN, &conn->gnc_mdd_list,
1228 "dev %p releasing held mdd %#llx.%#llx\n",
1229 conn->gnc_device, gmp->gmp_map_key.qword1,
1230 gmp->gmp_map_key.qword2);
1232 atomic_dec(&dev->gnd_n_mdd_held);
1233 kgnilnd_mem_mdd_release(conn->gnc_device->gnd_handle,
1235 /* ignoring the return code - if kgni/ghal can't find it
1236 * it must be released already */
1238 list_del_init(&gmp->gmp_list);
1239 LIBCFS_FREE(gmp, sizeof(*gmp));
1241 /* lose conn ref for purgatory */
1242 kgnilnd_conn_decref(conn);
1246 /* needs write_lock on kgnilnd_data.kgn_peer_conn_lock held */
1248 kgnilnd_peer_increase_reconnect_locked(kgn_peer_t *peer)
1252 current_to = peer->gnp_reconnect_interval;
1254 /* we'll try to reconnect fast the first time, then back-off */
1255 if (current_to == 0) {
1256 peer->gnp_reconnect_time = jiffies - 1;
1257 current_to = *kgnilnd_tunables.kgn_min_reconnect_interval;
1259 peer->gnp_reconnect_time = jiffies + cfs_time_seconds(current_to);
1260 /* add 50% of min timeout & retry */
1261 current_to += *kgnilnd_tunables.kgn_min_reconnect_interval / 2;
1264 current_to = MIN(current_to,
1265 *kgnilnd_tunables.kgn_max_reconnect_interval);
1267 peer->gnp_reconnect_interval = current_to;
1268 CDEBUG(D_NET, "peer %s can reconnect at %lu interval %lu\n",
1269 libcfs_nid2str(peer->gnp_nid), peer->gnp_reconnect_time,
1270 peer->gnp_reconnect_interval);
1273 /* needs kgnilnd_data.kgn_peer_conn_lock held */
1275 kgnilnd_find_peer_locked(lnet_nid_t nid)
1277 struct list_head *peer_list = kgnilnd_nid2peerlist(nid);
1280 /* Chopping nid down to only NIDADDR using LNET_NIDADDR so we only
1281 * have a single peer per device instead of a peer per nid/net combo.
1284 list_for_each_entry(peer, peer_list, gnp_list) {
1285 if (LNET_NIDADDR(nid) != LNET_NIDADDR(peer->gnp_nid))
1288 CDEBUG(D_NET, "got peer [%p] -> %s c %d (%d)\n",
1289 peer, libcfs_nid2str(nid),
1290 peer->gnp_connecting,
1291 atomic_read(&peer->gnp_refcount));
1297 /* need write_lock on kgn_peer_conn_lock */
1299 kgnilnd_unlink_peer_locked(kgn_peer_t *peer)
1301 LASSERTF(list_empty(&peer->gnp_conns),
1303 peer, libcfs_nid2str(peer->gnp_nid));
1304 LASSERTF(list_empty(&peer->gnp_tx_queue),
1306 peer, libcfs_nid2str(peer->gnp_nid));
1307 LASSERTF(kgnilnd_peer_active(peer),
1309 peer, libcfs_nid2str(peer->gnp_nid));
1310 CDEBUG(D_NET, "unlinking peer 0x%p->%s\n",
1311 peer, libcfs_nid2str(peer->gnp_nid));
1313 list_del_init(&peer->gnp_list);
1314 kgnilnd_data.kgn_peer_version++;
1315 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_unlink);
1316 /* lose peerlist's ref */
1317 kgnilnd_peer_decref(peer);
1321 kgnilnd_get_peer_info(int index,
1322 kgn_peer_t **found_peer,
1323 lnet_nid_t *id, __u32 *nic_addr,
1324 int *refcount, int *connecting)
1326 struct list_head *ptmp;
1331 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1333 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
1335 list_for_each(ptmp, &kgnilnd_data.kgn_peers[i]) {
1336 peer = list_entry(ptmp, kgn_peer_t, gnp_list);
1341 CDEBUG(D_NET, "found peer %p (%s) at index %d\n",
1342 peer, libcfs_nid2str(peer->gnp_nid), index);
1345 *id = peer->gnp_nid;
1346 *nic_addr = peer->gnp_host_id;
1347 *refcount = atomic_read(&peer->gnp_refcount);
1348 *connecting = peer->gnp_connecting;
1355 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1357 CDEBUG(D_NET, "no gni peer at index %d\n", index);
1361 /* requires write_lock on kgn_peer_conn_lock held */
1363 kgnilnd_add_peer_locked(lnet_nid_t nid, kgn_peer_t *new_stub_peer, kgn_peer_t **peerp)
1365 kgn_peer_t *peer, *peer2;
1367 LASSERTF(new_stub_peer != NULL, "bad stub peer for nid %s\n",
1368 libcfs_nid2str(nid));
1370 peer2 = kgnilnd_find_peer_locked(nid);
1371 if (peer2 != NULL) {
1372 /* A peer was created during the lock transition, so drop
1373 * the new one we created */
1374 kgnilnd_peer_decref(new_stub_peer);
1377 peer = new_stub_peer;
1378 /* peer table takes existing ref on peer */
1380 LASSERTF(!kgnilnd_peer_active(peer),
1381 "peer 0x%p->%s already in peer table\n",
1382 peer, libcfs_nid2str(peer->gnp_nid));
1383 list_add_tail(&peer->gnp_list,
1384 kgnilnd_nid2peerlist(nid));
1385 kgnilnd_data.kgn_peer_version++;
1388 LASSERTF(peer->gnp_net != NULL, "peer 0x%p->%s with NULL net\n",
1389 peer, libcfs_nid2str(peer->gnp_nid));
1394 kgnilnd_add_peer(kgn_net_t *net, lnet_nid_t nid, kgn_peer_t **peerp)
1401 if (nid == LNET_NID_ANY)
1404 node_state = kgnilnd_get_node_state(LNET_NIDADDR(nid));
1406 /* NB - this will not block during normal operations -
1407 * the only writer of this is in the startup/shutdown path. */
1408 rc = down_read_trylock(&kgnilnd_data.kgn_net_rw_sem);
1413 rc = kgnilnd_create_peer_safe(&peer, nid, net, node_state);
1415 up_read(&kgnilnd_data.kgn_net_rw_sem);
1419 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1420 up_read(&kgnilnd_data.kgn_net_rw_sem);
1422 kgnilnd_add_peer_locked(nid, peer, peerp);
1424 CDEBUG(D_NET, "peer 0x%p->%s connecting %d\n",
1425 peerp, libcfs_nid2str((*peerp)->gnp_nid),
1426 (*peerp)->gnp_connecting);
1428 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1432 /* needs write_lock on kgn_peer_conn_lock */
1434 kgnilnd_cancel_peer_connect_locked(kgn_peer_t *peer, struct list_head *zombies)
1438 /* we do care about state of gnp_connecting - we could be between
1439 * reconnect attempts, so try to find the dgram and cancel the TX
1440 * anyways. If we are in the process of posting DONT do anything;
1441 * once it fails or succeeds we can nuke the connect attempt.
1442 * We have no idea where in kgnilnd_post_dgram we are so we cant
1443 * attempt to cancel until the function is done.
1446 /* make sure peer isn't in process of connecting or waiting for connect*/
1447 spin_lock(&peer->gnp_net->gnn_dev->gnd_connd_lock);
1448 if (!(list_empty(&peer->gnp_connd_list))) {
1449 list_del_init(&peer->gnp_connd_list);
1450 /* remove connd ref */
1451 kgnilnd_peer_decref(peer);
1453 spin_unlock(&peer->gnp_net->gnn_dev->gnd_connd_lock);
1455 if (peer->gnp_connecting == GNILND_PEER_POSTING || peer->gnp_connecting == GNILND_PEER_NEEDS_DEATH) {
1456 peer->gnp_connecting = GNILND_PEER_NEEDS_DEATH;
1457 /* We are in process of posting right now the xchg set it up for us to
1458 * cancel the connect so we are finished for now */
1460 /* no need for exchange we have the peer lock and its ready for us to nuke */
1461 LASSERTF(peer->gnp_connecting != GNILND_PEER_POSTING,
1462 "Peer in invalid state 0x%p->%s, connecting %d\n",
1463 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting);
1464 peer->gnp_connecting = GNILND_PEER_IDLE;
1465 set_mb(peer->gnp_last_dgram_errno, -ETIMEDOUT);
1466 kgnilnd_find_and_cancel_dgram(peer->gnp_net->gnn_dev,
1470 /* The least we can do is nuke the tx's no matter what.... */
1471 list_for_each_entry_safe(tx, txn, &peer->gnp_tx_queue, tx_list) {
1472 kgnilnd_tx_del_state_locked(tx, peer, NULL,
1474 list_add_tail(&tx->tx_list, zombies);
1478 /* needs write_lock on kgn_peer_conn_lock */
1480 kgnilnd_del_peer_locked(kgn_peer_t *peer, int error)
1482 /* this peer could be passive and only held for purgatory,
1483 * take a ref to ensure it doesn't disappear in this function */
1484 kgnilnd_peer_addref(peer);
1486 CFS_RACE(CFS_FAIL_GNI_FIND_TARGET);
1488 /* if purgatory release cleared it out, don't try again */
1489 if (kgnilnd_peer_active(peer)) {
1490 /* always do this to allow kgnilnd_start_connect and
1491 * kgnilnd_finish_connect to catch this before they
1492 * wrap up their operations */
1493 if (kgnilnd_can_unlink_peer_locked(peer)) {
1494 /* already released purgatory, so only active
1496 kgnilnd_unlink_peer_locked(peer);
1498 kgnilnd_close_peer_conns_locked(peer, error);
1499 /* peer unlinks itself when last conn is closed */
1503 /* we are done, release back to the wild */
1504 kgnilnd_peer_decref(peer);
1508 kgnilnd_del_conn_or_peer(kgn_net_t *net, lnet_nid_t nid, int command,
1512 LIST_HEAD (zombies);
1513 struct list_head *ptmp, *pnxt;
1520 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1522 if (nid != LNET_NID_ANY)
1523 lo = hi = kgnilnd_nid2peerlist(nid) - kgnilnd_data.kgn_peers;
1526 hi = *kgnilnd_tunables.kgn_peer_hash_size - 1;
1527 /* wildcards always succeed */
1531 for (i = lo; i <= hi; i++) {
1532 list_for_each_safe(ptmp, pnxt, &kgnilnd_data.kgn_peers[i]) {
1533 peer = list_entry(ptmp, kgn_peer_t, gnp_list);
1535 LASSERTF(peer->gnp_net != NULL,
1536 "peer %p (%s) with NULL net\n",
1537 peer, libcfs_nid2str(peer->gnp_nid));
1539 if (net != NULL && peer->gnp_net != net)
1542 if (!(nid == LNET_NID_ANY || LNET_NIDADDR(peer->gnp_nid) == LNET_NIDADDR(nid)))
1545 /* In both cases, we want to stop any in-flight
1546 * connect attempts */
1547 kgnilnd_cancel_peer_connect_locked(peer, &zombies);
1550 case GNILND_DEL_CONN:
1551 kgnilnd_close_peer_conns_locked(peer, error);
1553 case GNILND_DEL_PEER:
1554 peer->gnp_pending_unlink = 1;
1555 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_unlink);
1556 kgnilnd_mark_for_detach_purgatory_all_locked(peer);
1557 kgnilnd_del_peer_locked(peer, error);
1559 case GNILND_CLEAR_PURGATORY:
1560 /* Mark everything ready for detach reaper will cleanup
1561 * once we release the kgn_peer_conn_lock
1563 kgnilnd_mark_for_detach_purgatory_all_locked(peer);
1564 peer->gnp_last_errno = -EISCONN;
1565 /* clear reconnect so he can reconnect soon */
1566 peer->gnp_reconnect_time = 0;
1567 peer->gnp_reconnect_interval = 0;
1570 CERROR("bad command %d\n", command);
1573 /* we matched something */
1578 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1581 kgnilnd_txlist_done(&zombies, error);
1583 /* This function does not return until the commands it initiated have completed,
1584 * since they have to work there way through the other threads. In the case of shutdown
1585 * threads are not woken up until after this call is initiated so we cannot wait, we just
1586 * need to return. The same applies for stack reset we shouldnt wait as the reset thread
1590 CFS_RACE(CFS_FAIL_GNI_RACE_RESET);
1592 if (error == -ENOTRECOVERABLE || error == -ESHUTDOWN) {
1597 while (atomic_read(&kgnilnd_data.kgn_npending_conns) ||
1598 atomic_read(&kgnilnd_data.kgn_npending_detach) ||
1599 atomic_read(&kgnilnd_data.kgn_npending_unlink)) {
1601 set_current_state(TASK_UNINTERRUPTIBLE);
1602 schedule_timeout(cfs_time_seconds(1));
1605 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, "Waiting on %d peers %d closes %d detaches\n",
1606 atomic_read(&kgnilnd_data.kgn_npending_unlink),
1607 atomic_read(&kgnilnd_data.kgn_npending_conns),
1608 atomic_read(&kgnilnd_data.kgn_npending_detach));
1615 kgnilnd_get_conn_by_idx(int index)
1618 struct list_head *ptmp;
1620 struct list_head *ctmp;
1624 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
1625 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1626 list_for_each(ptmp, &kgnilnd_data.kgn_peers[i]) {
1628 peer = list_entry(ptmp, kgn_peer_t, gnp_list);
1630 list_for_each(ctmp, &peer->gnp_conns) {
1631 conn = list_entry(ctmp, kgn_conn_t, gnc_list);
1633 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
1639 CDEBUG(D_NET, "++conn[%p] -> %s (%d)\n", conn,
1640 libcfs_nid2str(conn->gnc_peer->gnp_nid),
1641 atomic_read(&conn->gnc_refcount));
1642 kgnilnd_conn_addref(conn);
1643 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1647 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1654 kgnilnd_get_conn_info(kgn_peer_t *peer,
1655 int *device_id, __u64 *peerstamp,
1656 int *tx_seq, int *rx_seq,
1657 int *fmaq_len, int *nfma, int *nrdma)
1662 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1664 conn = kgnilnd_find_conn_locked(peer);
1670 *device_id = conn->gnc_device->gnd_host_id;
1671 *peerstamp = conn->gnc_peerstamp;
1672 *tx_seq = atomic_read(&conn->gnc_tx_seq);
1673 *rx_seq = atomic_read(&conn->gnc_rx_seq);
1674 *fmaq_len = kgnilnd_count_list(&conn->gnc_fmaq);
1675 *nfma = atomic_read(&conn->gnc_nlive_fma);
1676 *nrdma = atomic_read(&conn->gnc_nlive_rdma);
1678 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1682 /* needs write_lock on kgn_peer_conn_lock */
1684 kgnilnd_close_peer_conns_locked(kgn_peer_t *peer, int why)
1687 struct list_head *ctmp, *cnxt;
1690 list_for_each_safe(ctmp, cnxt, &peer->gnp_conns) {
1691 conn = list_entry(ctmp, kgn_conn_t, gnc_list);
1693 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
1697 /* we mark gnc_needs closing and increment kgn_npending_conns so that
1698 * kgnilnd_del_conn_or_peer can wait on the other threads closing
1699 * and cleaning up the connection.
1701 if (!conn->gnc_needs_closing) {
1702 conn->gnc_needs_closing = 1;
1703 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_conns);
1705 kgnilnd_close_conn_locked(conn, why);
1711 kgnilnd_report_node_state(lnet_nid_t nid, int down)
1714 kgn_peer_t *peer, *new_peer;
1717 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1718 peer = kgnilnd_find_peer_locked(nid);
1725 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1727 /* Don't add a peer for node up events */
1728 if (down == GNILND_PEER_UP)
1731 /* find any valid net - we don't care which one... */
1732 down_read(&kgnilnd_data.kgn_net_rw_sem);
1733 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
1734 list_for_each_entry(net, &kgnilnd_data.kgn_nets[i],
1744 up_read(&kgnilnd_data.kgn_net_rw_sem);
1747 CNETERR("Could not find a net for nid %lld\n", nid);
1751 /* The nid passed in does not yet contain the net portion.
1752 * Let's build it up now
1754 nid = LNET_MKNID(LNET_NIDNET(net->gnn_ni->ni_nid), nid);
1755 rc = kgnilnd_add_peer(net, nid, &new_peer);
1758 CNETERR("Could not add peer for nid %lld, rc %d\n",
1763 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1764 peer = kgnilnd_find_peer_locked(nid);
1767 CNETERR("Could not find peer for nid %lld\n", nid);
1768 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1773 peer->gnp_state = down;
1775 if (down == GNILND_PEER_DOWN) {
1778 peer->gnp_down_event_time = jiffies;
1779 kgnilnd_cancel_peer_connect_locked(peer, &zombies);
1780 conn = kgnilnd_find_conn_locked(peer);
1783 kgnilnd_close_conn_locked(conn, -ENETRESET);
1786 peer->gnp_up_event_time = jiffies;
1789 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1791 if (down == GNILND_PEER_DOWN) {
1792 /* using ENETRESET so we don't get messages from
1795 kgnilnd_txlist_done(&zombies, -ENETRESET);
1796 kgnilnd_peer_notify(peer, -ECONNRESET, 0);
1797 LCONSOLE_INFO("Received down event for nid %d\n",
1805 kgnilnd_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
1807 struct libcfs_ioctl_data *data = arg;
1808 kgn_net_t *net = ni->ni_data;
1811 LASSERT(ni == net->gnn_ni);
1814 case IOC_LIBCFS_GET_PEER: {
1816 kgn_peer_t *peer = NULL;
1818 __u64 peerstamp = 0;
1819 int peer_refcount = 0, peer_connecting = 0;
1821 int tx_seq = 0, rx_seq = 0;
1822 int fmaq_len = 0, nfma = 0, nrdma = 0;
1824 rc = kgnilnd_get_peer_info(data->ioc_count, &peer,
1825 &nid, &nic_addr, &peer_refcount,
1831 /* LNET_MKNID is used to mask from lnet the multiplexing/demultiplexing of connections and peers
1832 * LNET assumes a conn and peer per net, the LNET_MKNID/LNET_NIDADDR allows us to let Lnet see what it
1833 * wants to see instead of the underlying network that is being used to send the data
1835 data->ioc_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), LNET_NIDADDR(nid));
1836 data->ioc_flags = peer_connecting;
1837 data->ioc_count = peer_refcount;
1839 rc = kgnilnd_get_conn_info(peer, &device_id, &peerstamp,
1840 &tx_seq, &rx_seq, &fmaq_len,
1843 /* This is allowable - a persistent peer could not
1844 * have a connection */
1846 /* flag to indicate we are not connected -
1847 * need to print as such */
1848 data->ioc_flags |= (1<<16);
1852 data->ioc_net = device_id;
1853 data->ioc_u64[0] = peerstamp;
1854 data->ioc_u32[0] = fmaq_len;
1855 data->ioc_u32[1] = nfma;
1856 data->ioc_u32[2] = tx_seq;
1857 data->ioc_u32[3] = rx_seq;
1858 data->ioc_u32[4] = nrdma;
1862 case IOC_LIBCFS_ADD_PEER: {
1863 /* just dummy value to allow using common interface */
1865 rc = kgnilnd_add_peer(net, data->ioc_nid, &peer);
1868 case IOC_LIBCFS_DEL_PEER: {
1869 /* NULL is passed in so it affects all peers in existence without regard to network
1870 * as the peer may not exist on the network LNET believes it to be on.
1872 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1873 GNILND_DEL_PEER, -EUCLEAN);
1876 case IOC_LIBCFS_GET_CONN: {
1877 kgn_conn_t *conn = kgnilnd_get_conn_by_idx(data->ioc_count);
1883 /* LNET_MKNID is used to build the correct address based on what LNET wants to see instead of
1884 * the generic connection that is used to send the data
1886 data->ioc_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), LNET_NIDADDR(conn->gnc_peer->gnp_nid));
1887 data->ioc_u32[0] = conn->gnc_device->gnd_id;
1888 kgnilnd_conn_decref(conn);
1892 case IOC_LIBCFS_CLOSE_CONNECTION: {
1893 /* use error = -ENETRESET to indicate it was lctl disconnect */
1894 /* NULL is passed in so it affects all the nets as the connection is virtual
1895 * and may not exist on the network LNET believes it to be on.
1897 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1898 GNILND_DEL_CONN, -ENETRESET);
1901 case IOC_LIBCFS_PUSH_CONNECTION: {
1902 /* we use this to flush purgatory */
1903 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1904 GNILND_CLEAR_PURGATORY, -EUCLEAN);
1907 case IOC_LIBCFS_REGISTER_MYNID: {
1908 /* Ignore if this is a noop */
1909 if (data->ioc_nid == ni->ni_nid) {
1912 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
1913 libcfs_nid2str(data->ioc_nid),
1914 libcfs_nid2str(ni->ni_nid));
1925 kgnilnd_query(struct lnet_ni *ni, lnet_nid_t nid, time64_t *when)
1927 kgn_net_t *net = ni->ni_data;
1929 kgn_peer_t *peer = NULL;
1930 kgn_conn_t *conn = NULL;
1931 struct lnet_process_id id = {
1933 .pid = LNET_PID_LUSTRE,
1937 /* I expect to find him, so only take a read lock */
1938 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1939 peer = kgnilnd_find_peer_locked(nid);
1941 /* LIE if in a quiesce - we will update the timeouts after,
1942 * but we don't want sends failing during it */
1943 if (kgnilnd_data.kgn_quiesce_trigger) {
1944 *when = ktime_get_seconds();
1945 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1949 /* Update to best guess, might refine on later checks */
1950 *when = peer->gnp_last_alive;
1952 /* we have a peer, how about a conn? */
1953 conn = kgnilnd_find_conn_locked(peer);
1956 /* if there is no conn, check peer last errno to see if clean disconnect
1957 * - if it was, we lie to LNet because we believe a TX would complete
1959 if (kgnilnd_conn_clean_errno(peer->gnp_last_errno)) {
1960 *when = ktime_get_seconds();
1962 /* we still want to fire a TX and new conn in this case */
1964 /* gnp_last_alive is valid, run for the hills */
1965 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1969 /* if we get here, either we have no peer or no conn for him, so fire off
1970 * new TX to trigger conn setup */
1971 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1973 /* if we couldn't find him, we'll fire up a TX and get connected -
1974 * if we don't do this, after ni_peer_timeout, LNet will declare him dead.
1975 * So really we treat kgnilnd_query as a bit of a 'connect now' type
1976 * event because it'll only do this when it wants to send
1978 * Use a real TX for this to get the proper gnp_tx_queue behavior, etc
1979 * normally we'd use kgnilnd_send_ctlmsg for this, but we don't really
1980 * care that this goes out quickly since we already know we need a new conn
1982 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_NOOP_SEND))
1985 tx = kgnilnd_new_tx_msg(GNILND_MSG_NOOP, ni->ni_nid);
1987 kgnilnd_launch_tx(tx, net, &id);
1990 CDEBUG(D_NETTRACE, "peer 0x%p->%s when %lld\n", peer,
1991 libcfs_nid2str(nid), *when);
1996 kgnilnd_dev_init(kgn_device_t *dev)
2000 unsigned int cq_size;
2003 /* size of these CQs should be able to accommodate the outgoing
2004 * RDMA and SMSG transactions. Since we really don't know what we
2005 * really need here, we'll take credits * 2 * 3 to allow a bunch.
2006 * We need to dig into this more with the performance work. */
2007 cq_size = *kgnilnd_tunables.kgn_credits * 2 * 3;
2009 rrc = kgnilnd_cdm_create(dev->gnd_id, *kgnilnd_tunables.kgn_ptag,
2010 *kgnilnd_tunables.kgn_pkey, 0,
2012 if (rrc != GNI_RC_SUCCESS) {
2013 CERROR("Can't create CDM %d (%d)\n", dev->gnd_id, rrc);
2014 GOTO(failed, rc = -ENODEV);
2017 rrc = kgnilnd_cdm_attach(dev->gnd_domain, dev->gnd_id,
2018 &dev->gnd_host_id, &dev->gnd_handle);
2019 if (rrc != GNI_RC_SUCCESS) {
2020 CERROR("Can't attach CDM to device %d (%d)\n",
2022 GOTO(failed, rc = -ENODEV);
2025 /* a bit gross, but not much we can do - Aries Sim doesn't have
2026 * hardcoded NIC/NID that we can use */
2027 rc = kgnilnd_setup_nic_translation(dev->gnd_host_id);
2029 GOTO(failed, rc = -ENODEV);
2031 /* only dev 0 gets the errors - no need to reset the stack twice
2032 * - this works because we have a single PTAG, if we had more
2033 * then we'd need to have multiple handlers */
2034 if (dev->gnd_id == 0) {
2035 rrc = kgnilnd_subscribe_errors(dev->gnd_handle,
2036 GNI_ERRMASK_CRITICAL |
2037 GNI_ERRMASK_UNKNOWN_TRANSACTION,
2038 0, NULL, kgnilnd_critical_error,
2039 &dev->gnd_err_handle);
2040 if (rrc != GNI_RC_SUCCESS) {
2041 CERROR("Can't subscribe for errors on device %d: rc %d\n",
2043 GOTO(failed, rc = -ENODEV);
2046 rc = kgnilnd_set_quiesce_callback(dev->gnd_handle,
2047 kgnilnd_quiesce_end_callback);
2048 if (rc != GNI_RC_SUCCESS) {
2049 CERROR("Can't subscribe for quiesce callback on device %d: rc %d\n",
2051 GOTO(failed, rc = -ENODEV);
2055 rc = kgnilnd_nicaddr_to_nid(dev->gnd_host_id, &dev->gnd_nid);
2057 /* log messages during startup */
2058 if (kgnilnd_data.kgn_init < GNILND_INIT_ALL) {
2059 CERROR("couldn't translate host_id 0x%x to nid. rc %d\n",
2060 dev->gnd_host_id, rc);
2062 GOTO(failed, rc = -ESRCH);
2064 CDEBUG(D_NET, "NIC %x -> NID %d\n", dev->gnd_host_id, dev->gnd_nid);
2066 rrc = kgnilnd_cq_create(dev->gnd_handle, *kgnilnd_tunables.kgn_credits,
2067 0, kgnilnd_device_callback,
2068 dev->gnd_id, &dev->gnd_snd_rdma_cqh);
2069 if (rrc != GNI_RC_SUCCESS) {
2070 CERROR("Can't create rdma send cq size %u for device "
2071 "%d (%d)\n", cq_size, dev->gnd_id, rrc);
2072 GOTO(failed, rc = -EINVAL);
2075 rrc = kgnilnd_cq_create(dev->gnd_handle, cq_size,
2076 0, kgnilnd_device_callback, dev->gnd_id,
2077 &dev->gnd_snd_fma_cqh);
2078 if (rrc != GNI_RC_SUCCESS) {
2079 CERROR("Can't create fma send cq size %u for device %d (%d)\n",
2080 cq_size, dev->gnd_id, rrc);
2081 GOTO(failed, rc = -EINVAL);
2084 /* This one we size differently - overflows are possible and it needs to be
2085 * sized based on machine size */
2086 rrc = kgnilnd_cq_create(dev->gnd_handle,
2087 *kgnilnd_tunables.kgn_fma_cq_size,
2088 0, kgnilnd_device_callback, dev->gnd_id,
2089 &dev->gnd_rcv_fma_cqh);
2090 if (rrc != GNI_RC_SUCCESS) {
2091 CERROR("Can't create fma cq size %d for device %d (%d)\n",
2092 *kgnilnd_tunables.kgn_fma_cq_size, dev->gnd_id, rrc);
2093 GOTO(failed, rc = -EINVAL);
2096 rrc = kgnilnd_register_smdd_buf(dev);
2097 if (rrc != GNI_RC_SUCCESS) {
2098 GOTO(failed, rc = -EINVAL);
2104 kgnilnd_dev_fini(dev);
2109 kgnilnd_dev_fini(kgn_device_t *dev)
2114 /* At quiesce or rest time, need to loop through and clear gnd_ready_conns ?*/
2115 LASSERTF(list_empty(&dev->gnd_ready_conns) &&
2116 list_empty(&dev->gnd_map_tx) &&
2117 list_empty(&dev->gnd_rdmaq) &&
2118 list_empty(&dev->gnd_delay_conns),
2119 "dev 0x%p ready_conns %d@0x%p delay_conns %d@0x%p"
2120 "map_tx %d@0x%p rdmaq %d@0x%p\n",
2121 dev, kgnilnd_count_list(&dev->gnd_ready_conns), &dev->gnd_ready_conns,
2122 kgnilnd_count_list(&dev->gnd_delay_conns), &dev->gnd_delay_conns,
2123 kgnilnd_count_list(&dev->gnd_map_tx), &dev->gnd_map_tx,
2124 kgnilnd_count_list(&dev->gnd_rdmaq), &dev->gnd_rdmaq);
2126 /* These should follow from tearing down all connections */
2127 LASSERTF(dev->gnd_map_nphys == 0 && dev->gnd_map_physnop == 0,
2128 "%d physical mappings of %d pages still mapped\n",
2129 dev->gnd_map_nphys, dev->gnd_map_physnop);
2131 LASSERTF(dev->gnd_map_nvirt == 0 && dev->gnd_map_virtnob == 0,
2132 "%d virtual mappings of %llu bytes still mapped\n",
2133 dev->gnd_map_nvirt, dev->gnd_map_virtnob);
2135 LASSERTF(atomic_read(&dev->gnd_n_mdd) == 0 &&
2136 atomic_read(&dev->gnd_n_mdd_held) == 0 &&
2137 atomic64_read(&dev->gnd_nbytes_map) == 0,
2138 "%d SMSG mappings of %ld bytes still mapped or held %d\n",
2139 atomic_read(&dev->gnd_n_mdd),
2140 atomic64_read(&dev->gnd_nbytes_map), atomic_read(&dev->gnd_n_mdd_held));
2142 LASSERT(list_empty(&dev->gnd_map_list));
2144 /* What other assertions needed to ensure all connections torn down ? */
2146 /* check all counters == 0 (EP, MDD, etc) */
2148 /* if we are resetting due to quiese (stack reset), don't check
2150 LASSERTF(kgnilnd_data.kgn_quiesce_trigger ||
2151 atomic_read(&kgnilnd_data.kgn_nthreads) == 0,
2152 "tried to shutdown with threads active\n");
2154 if (dev->gnd_smdd_hold_buf) {
2155 rrc = kgnilnd_deregister_smdd_buf(dev);
2156 LASSERTF(rrc == GNI_RC_SUCCESS,
2157 "bad rc from deregistion of sMDD buffer: %d\n", rrc);
2158 dev->gnd_smdd_hold_buf = NULL;
2161 if (dev->gnd_rcv_fma_cqh) {
2162 rrc = kgnilnd_cq_destroy(dev->gnd_rcv_fma_cqh);
2163 LASSERTF(rrc == GNI_RC_SUCCESS,
2164 "bad rc from gni_cq_destroy on rcv_fma_cqh: %d\n", rrc);
2165 dev->gnd_rcv_fma_cqh = NULL;
2168 if (dev->gnd_snd_rdma_cqh) {
2169 rrc = kgnilnd_cq_destroy(dev->gnd_snd_rdma_cqh);
2170 LASSERTF(rrc == GNI_RC_SUCCESS,
2171 "bad rc from gni_cq_destroy on send_rdma_cqh: %d\n", rrc);
2172 dev->gnd_snd_rdma_cqh = NULL;
2175 if (dev->gnd_snd_fma_cqh) {
2176 rrc = kgnilnd_cq_destroy(dev->gnd_snd_fma_cqh);
2177 LASSERTF(rrc == GNI_RC_SUCCESS,
2178 "bad rc from gni_cq_destroy on snd_fma_cqh: %d\n", rrc);
2179 dev->gnd_snd_fma_cqh = NULL;
2182 if (dev->gnd_err_handle) {
2183 rrc = kgnilnd_release_errors(dev->gnd_err_handle);
2184 LASSERTF(rrc == GNI_RC_SUCCESS,
2185 "bad rc from gni_release_errors: %d\n", rrc);
2186 dev->gnd_err_handle = NULL;
2189 if (dev->gnd_domain) {
2190 rrc = kgnilnd_cdm_destroy(dev->gnd_domain);
2191 LASSERTF(rrc == GNI_RC_SUCCESS,
2192 "bad rc from gni_cdm_destroy: %d\n", rrc);
2193 dev->gnd_domain = NULL;
2199 int kgnilnd_base_startup(void)
2202 int pkmem = atomic_read(&libcfs_kmemory);
2206 struct task_struct *thrd;
2208 #if defined(CONFIG_CRAY_XT) && !defined(CONFIG_CRAY_COMPUTE)
2209 /* limit how much memory can be allocated for fma blocks in
2210 * instances where many nodes need to reconnects at the same time */
2213 kgnilnd_data.free_pages_limit = si.totalram/4;
2218 LASSERTF(kgnilnd_data.kgn_init == GNILND_INIT_NOTHING,
2219 "init %d\n", kgnilnd_data.kgn_init);
2221 /* zero pointers, flags etc */
2222 memset(&kgnilnd_data, 0, sizeof(kgnilnd_data));
2223 kgnilnd_check_kgni_version();
2225 /* CAVEAT EMPTOR: Every 'Fma' message includes the sender's NID and
2226 * a unique (for all time) connstamp so we can uniquely identify
2227 * the sender. The connstamp is an incrementing counter
2228 * initialised with seconds + microseconds at startup time. So we
2229 * rely on NOT creating connections more frequently on average than
2230 * 1MHz to ensure we don't use old connstamps when we reboot. */
2231 do_gettimeofday(&tv);
2232 kgnilnd_data.kgn_connstamp =
2233 kgnilnd_data.kgn_peerstamp =
2234 (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
2236 init_rwsem(&kgnilnd_data.kgn_net_rw_sem);
2238 for (i = 0; i < GNILND_MAXDEVS; i++) {
2239 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2242 INIT_LIST_HEAD(&dev->gnd_ready_conns);
2243 INIT_LIST_HEAD(&dev->gnd_delay_conns);
2244 INIT_LIST_HEAD(&dev->gnd_map_tx);
2245 INIT_LIST_HEAD(&dev->gnd_fma_buffs);
2246 mutex_init(&dev->gnd_cq_mutex);
2247 mutex_init(&dev->gnd_fmablk_mutex);
2248 spin_lock_init(&dev->gnd_fmablk_lock);
2249 init_waitqueue_head(&dev->gnd_waitq);
2250 init_waitqueue_head(&dev->gnd_dgram_waitq);
2251 init_waitqueue_head(&dev->gnd_dgping_waitq);
2252 spin_lock_init(&dev->gnd_lock);
2253 INIT_LIST_HEAD(&dev->gnd_map_list);
2254 spin_lock_init(&dev->gnd_map_lock);
2255 atomic_set(&dev->gnd_nfmablk, 0);
2256 atomic_set(&dev->gnd_fmablk_vers, 1);
2257 atomic_set(&dev->gnd_neps, 0);
2258 atomic_set(&dev->gnd_canceled_dgrams, 0);
2259 INIT_LIST_HEAD(&dev->gnd_connd_peers);
2260 spin_lock_init(&dev->gnd_connd_lock);
2261 spin_lock_init(&dev->gnd_dgram_lock);
2262 spin_lock_init(&dev->gnd_rdmaq_lock);
2263 INIT_LIST_HEAD(&dev->gnd_rdmaq);
2264 init_rwsem(&dev->gnd_conn_sem);
2266 /* alloc & setup nid based dgram table */
2267 LIBCFS_ALLOC(dev->gnd_dgrams,
2268 sizeof(struct list_head) * *kgnilnd_tunables.kgn_peer_hash_size);
2270 if (dev->gnd_dgrams == NULL)
2271 GOTO(failed, rc = -ENOMEM);
2273 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2274 INIT_LIST_HEAD(&dev->gnd_dgrams[i]);
2276 atomic_set(&dev->gnd_ndgrams, 0);
2277 atomic_set(&dev->gnd_nwcdgrams, 0);
2278 /* setup timer for RDMAQ processing */
2279 setup_timer(&dev->gnd_rdmaq_timer, kgnilnd_schedule_device_timer,
2280 (unsigned long)dev);
2282 /* setup timer for mapping processing */
2283 setup_timer(&dev->gnd_map_timer, kgnilnd_schedule_device_timer,
2284 (unsigned long)dev);
2288 /* CQID 0 isn't allowed, set to MAX_MSG_ID - 1 to check for conflicts early */
2289 kgnilnd_data.kgn_next_cqid = GNILND_MAX_MSG_ID - 1;
2290 kgnilnd_data.kgn_new_min_timeout = *kgnilnd_tunables.kgn_timeout;
2291 init_waitqueue_head(&kgnilnd_data.kgn_reaper_waitq);
2292 init_waitqueue_head(&kgnilnd_data.kgn_ruhroh_waitq);
2293 spin_lock_init(&kgnilnd_data.kgn_reaper_lock);
2295 mutex_init(&kgnilnd_data.kgn_quiesce_mutex);
2296 atomic_set(&kgnilnd_data.kgn_nquiesce, 0);
2297 atomic_set(&kgnilnd_data.kgn_npending_conns, 0);
2298 atomic_set(&kgnilnd_data.kgn_npending_unlink, 0);
2299 atomic_set(&kgnilnd_data.kgn_npending_detach, 0);
2300 atomic_set(&kgnilnd_data.kgn_rev_offset, 0);
2301 atomic_set(&kgnilnd_data.kgn_rev_length, 0);
2302 atomic_set(&kgnilnd_data.kgn_rev_copy_buff, 0);
2304 /* OK to call kgnilnd_api_shutdown() to cleanup now */
2305 kgnilnd_data.kgn_init = GNILND_INIT_DATA;
2306 try_module_get(THIS_MODULE);
2308 rwlock_init(&kgnilnd_data.kgn_peer_conn_lock);
2310 LIBCFS_ALLOC(kgnilnd_data.kgn_peers,
2311 sizeof(struct list_head) * *kgnilnd_tunables.kgn_peer_hash_size);
2313 if (kgnilnd_data.kgn_peers == NULL)
2314 GOTO(failed, rc = -ENOMEM);
2316 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2317 INIT_LIST_HEAD(&kgnilnd_data.kgn_peers[i]);
2320 LIBCFS_ALLOC(kgnilnd_data.kgn_conns,
2321 sizeof(struct list_head) * *kgnilnd_tunables.kgn_peer_hash_size);
2323 if (kgnilnd_data.kgn_conns == NULL)
2324 GOTO(failed, rc = -ENOMEM);
2326 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2327 INIT_LIST_HEAD(&kgnilnd_data.kgn_conns[i]);
2330 LIBCFS_ALLOC(kgnilnd_data.kgn_nets,
2331 sizeof(struct list_head) * *kgnilnd_tunables.kgn_net_hash_size);
2333 if (kgnilnd_data.kgn_nets == NULL)
2334 GOTO(failed, rc = -ENOMEM);
2336 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
2337 INIT_LIST_HEAD(&kgnilnd_data.kgn_nets[i]);
2340 kgnilnd_data.kgn_mbox_cache =
2341 kmem_cache_create("kgn_mbox_block", GNILND_MBOX_SIZE, 0,
2342 SLAB_HWCACHE_ALIGN, NULL);
2343 if (kgnilnd_data.kgn_mbox_cache == NULL) {
2344 CERROR("Can't create slab for physical mbox blocks\n");
2345 GOTO(failed, rc = -ENOMEM);
2348 kgnilnd_data.kgn_rx_cache =
2349 kmem_cache_create("kgn_rx_t", sizeof(kgn_rx_t), 0, 0, NULL);
2350 if (kgnilnd_data.kgn_rx_cache == NULL) {
2351 CERROR("Can't create slab for kgn_rx_t descriptors\n");
2352 GOTO(failed, rc = -ENOMEM);
2355 kgnilnd_data.kgn_tx_cache =
2356 kmem_cache_create("kgn_tx_t", sizeof(kgn_tx_t), 0, 0, NULL);
2357 if (kgnilnd_data.kgn_tx_cache == NULL) {
2358 CERROR("Can't create slab for kgn_tx_t\n");
2359 GOTO(failed, rc = -ENOMEM);
2362 kgnilnd_data.kgn_tx_phys_cache =
2363 kmem_cache_create("kgn_tx_phys",
2364 LNET_MAX_IOV * sizeof(gni_mem_segment_t),
2366 if (kgnilnd_data.kgn_tx_phys_cache == NULL) {
2367 CERROR("Can't create slab for kgn_tx_phys\n");
2368 GOTO(failed, rc = -ENOMEM);
2371 kgnilnd_data.kgn_dgram_cache =
2372 kmem_cache_create("kgn_dgram_t", sizeof(kgn_dgram_t), 0, 0, NULL);
2373 if (kgnilnd_data.kgn_dgram_cache == NULL) {
2374 CERROR("Can't create slab for outgoing datagrams\n");
2375 GOTO(failed, rc = -ENOMEM);
2378 /* allocate a MAX_IOV array of page pointers for each cpu */
2379 kgnilnd_data.kgn_cksum_map_pages = kmalloc(num_possible_cpus() * sizeof (struct page *),
2381 if (kgnilnd_data.kgn_cksum_map_pages == NULL) {
2382 CERROR("Can't allocate vmap cksum pages\n");
2383 GOTO(failed, rc = -ENOMEM);
2385 kgnilnd_data.kgn_cksum_npages = num_possible_cpus();
2386 memset(kgnilnd_data.kgn_cksum_map_pages, 0,
2387 kgnilnd_data.kgn_cksum_npages * sizeof (struct page *));
2389 for (i = 0; i < kgnilnd_data.kgn_cksum_npages; i++) {
2390 kgnilnd_data.kgn_cksum_map_pages[i] = kmalloc(LNET_MAX_IOV * sizeof (struct page *),
2392 if (kgnilnd_data.kgn_cksum_map_pages[i] == NULL) {
2393 CERROR("Can't allocate vmap cksum pages for cpu %d\n", i);
2394 GOTO(failed, rc = -ENOMEM);
2398 LASSERT(kgnilnd_data.kgn_ndevs == 0);
2400 /* Use all available GNI devices */
2401 for (i = 0; i < GNILND_MAXDEVS; i++) {
2402 dev = &kgnilnd_data.kgn_devices[kgnilnd_data.kgn_ndevs];
2404 rc = kgnilnd_dev_init(dev);
2406 /* Increment here so base_shutdown cleans it up */
2407 kgnilnd_data.kgn_ndevs++;
2409 rc = kgnilnd_allocate_phys_fmablk(dev);
2415 if (kgnilnd_data.kgn_ndevs == 0) {
2416 CERROR("Can't initialise any GNI devices\n");
2417 GOTO(failed, rc = -ENODEV);
2420 rc = kgnilnd_thread_start(kgnilnd_reaper, NULL, "kgnilnd_rpr", 0);
2422 CERROR("Can't spawn gnilnd reaper: %d\n", rc);
2426 rc = kgnilnd_start_rca_thread();
2428 CERROR("Can't spawn gnilnd rca: %d\n", rc);
2433 * Start ruhroh thread. We can't use kgnilnd_thread_start() because
2434 * we don't want this thread included in kgnilnd_data.kgn_nthreads
2435 * count. This thread controls quiesce, so it mustn't
2438 thrd = kthread_run(kgnilnd_ruhroh_thread, NULL, "%s_%02d", "kgnilnd_rr", 0);
2441 CERROR("Can't spawn gnilnd ruhroh thread: %d\n", rc);
2445 /* threads will load balance across devs as they are available */
2446 if (*kgnilnd_tunables.kgn_thread_affinity) {
2447 rc = kgnilnd_start_sd_threads();
2451 for (i = 0; i < *kgnilnd_tunables.kgn_sched_threads; i++) {
2452 rc = kgnilnd_thread_start(kgnilnd_scheduler,
2456 CERROR("Can't spawn gnilnd scheduler[%d]: %d\n",
2463 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2464 dev = &kgnilnd_data.kgn_devices[i];
2465 rc = kgnilnd_thread_start(kgnilnd_dgram_mover, dev,
2466 "kgnilnd_dg", dev->gnd_id);
2468 CERROR("Can't spawn gnilnd dgram_mover[%d]: %d\n",
2473 rc = kgnilnd_thread_start(kgnilnd_dgram_waitq, dev,
2474 "kgnilnd_dgn", dev->gnd_id);
2476 CERROR("Can't spawn gnilnd dgram_waitq[%d]: %d\n",
2481 rc = kgnilnd_setup_wildcard_dgram(dev);
2484 CERROR("Can't create wildcard dgrams[%d]: %d\n",
2490 /* flag everything initialised */
2491 kgnilnd_data.kgn_init = GNILND_INIT_ALL;
2492 /*****************************************************/
2494 CDEBUG(D_MALLOC, "initial kmem %d\n", pkmem);
2498 kgnilnd_base_shutdown();
2499 kgnilnd_data.kgn_init = GNILND_INIT_NOTHING;
2504 kgnilnd_base_shutdown(void)
2509 while (CFS_FAIL_TIMEOUT(CFS_FAIL_GNI_PAUSE_SHUTDOWN, 1)) {};
2511 kgnilnd_data.kgn_wc_kill = 1;
2513 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2514 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2515 kgnilnd_cancel_wc_dgrams(dev);
2516 kgnilnd_cancel_dgrams(dev);
2517 kgnilnd_del_conn_or_peer(NULL, LNET_NID_ANY, GNILND_DEL_PEER, -ESHUTDOWN);
2518 kgnilnd_wait_for_canceled_dgrams(dev);
2521 /* We need to verify there are no conns left before we let the threads
2522 * shut down otherwise we could clean up the peers but still have
2523 * some outstanding conns due to orphaned datagram conns that are
2527 while (atomic_read(&kgnilnd_data.kgn_nconns) != 0) {
2530 for(j = 0; j < kgnilnd_data.kgn_ndevs; ++j) {
2531 kgn_device_t *dev = &kgnilnd_data.kgn_devices[j];
2532 kgnilnd_schedule_device(dev);
2535 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2536 "Waiting for conns to be cleaned up %d\n",atomic_read(&kgnilnd_data.kgn_nconns));
2537 set_current_state(TASK_UNINTERRUPTIBLE);
2538 schedule_timeout(cfs_time_seconds(1));
2540 /* Peer state all cleaned up BEFORE setting shutdown, so threads don't
2541 * have to worry about shutdown races. NB connections may be created
2542 * while there are still active connds, but these will be temporary
2543 * since peer creation always fails after the listener has started to
2545 * all peers should have been cleared out on the nets */
2546 LASSERTF(atomic_read(&kgnilnd_data.kgn_npeers) == 0,
2547 "peers left %d\n", atomic_read(&kgnilnd_data.kgn_npeers));
2549 /* Wait for the ruhroh thread to shut down. */
2550 kgnilnd_data.kgn_ruhroh_shutdown = 1;
2551 wake_up(&kgnilnd_data.kgn_ruhroh_waitq);
2553 while (kgnilnd_data.kgn_ruhroh_running != 0) {
2555 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2556 "Waiting for ruhroh thread to terminate\n");
2557 set_current_state(TASK_UNINTERRUPTIBLE);
2558 schedule_timeout(cfs_time_seconds(1));
2561 /* Flag threads to terminate */
2562 kgnilnd_data.kgn_shutdown = 1;
2564 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2565 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2567 /* should clear all the MDDs */
2568 kgnilnd_unmap_fma_blocks(dev);
2570 kgnilnd_schedule_device(dev);
2571 wake_up_all(&dev->gnd_dgram_waitq);
2572 wake_up_all(&dev->gnd_dgping_waitq);
2573 LASSERT(list_empty(&dev->gnd_connd_peers));
2576 spin_lock(&kgnilnd_data.kgn_reaper_lock);
2577 wake_up_all(&kgnilnd_data.kgn_reaper_waitq);
2578 spin_unlock(&kgnilnd_data.kgn_reaper_lock);
2580 if (atomic_read(&kgnilnd_data.kgn_nthreads))
2581 kgnilnd_wakeup_rca_thread();
2583 /* Wait for threads to exit */
2585 while (atomic_read(&kgnilnd_data.kgn_nthreads) != 0) {
2587 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2588 "Waiting for %d threads to terminate\n",
2589 atomic_read(&kgnilnd_data.kgn_nthreads));
2590 set_current_state(TASK_UNINTERRUPTIBLE);
2591 schedule_timeout(cfs_time_seconds(1));
2594 LASSERTF(atomic_read(&kgnilnd_data.kgn_npeers) == 0,
2595 "peers left %d\n", atomic_read(&kgnilnd_data.kgn_npeers));
2597 if (kgnilnd_data.kgn_peers != NULL) {
2598 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
2599 LASSERT(list_empty(&kgnilnd_data.kgn_peers[i]));
2601 LIBCFS_FREE(kgnilnd_data.kgn_peers,
2602 sizeof (struct list_head) *
2603 *kgnilnd_tunables.kgn_peer_hash_size);
2606 down_write(&kgnilnd_data.kgn_net_rw_sem);
2607 if (kgnilnd_data.kgn_nets != NULL) {
2608 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++)
2609 LASSERT(list_empty(&kgnilnd_data.kgn_nets[i]));
2611 LIBCFS_FREE(kgnilnd_data.kgn_nets,
2612 sizeof (struct list_head) *
2613 *kgnilnd_tunables.kgn_net_hash_size);
2615 up_write(&kgnilnd_data.kgn_net_rw_sem);
2617 LASSERTF(atomic_read(&kgnilnd_data.kgn_nconns) == 0,
2618 "conns left %d\n", atomic_read(&kgnilnd_data.kgn_nconns));
2620 if (kgnilnd_data.kgn_conns != NULL) {
2621 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
2622 LASSERT(list_empty(&kgnilnd_data.kgn_conns[i]));
2624 LIBCFS_FREE(kgnilnd_data.kgn_conns,
2625 sizeof (struct list_head) *
2626 *kgnilnd_tunables.kgn_peer_hash_size);
2629 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2630 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2631 kgnilnd_dev_fini(dev);
2633 LASSERTF(atomic_read(&dev->gnd_ndgrams) == 0,
2634 "dgrams left %d\n", atomic_read(&dev->gnd_ndgrams));
2636 if (dev->gnd_dgrams != NULL) {
2637 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
2638 LASSERT(list_empty(&dev->gnd_dgrams[i]));
2640 LIBCFS_FREE(dev->gnd_dgrams,
2641 sizeof (struct list_head) *
2642 *kgnilnd_tunables.kgn_peer_hash_size);
2645 kgnilnd_free_phys_fmablk(dev);
2648 if (kgnilnd_data.kgn_mbox_cache != NULL)
2649 kmem_cache_destroy(kgnilnd_data.kgn_mbox_cache);
2651 if (kgnilnd_data.kgn_rx_cache != NULL)
2652 kmem_cache_destroy(kgnilnd_data.kgn_rx_cache);
2654 if (kgnilnd_data.kgn_tx_cache != NULL)
2655 kmem_cache_destroy(kgnilnd_data.kgn_tx_cache);
2657 if (kgnilnd_data.kgn_tx_phys_cache != NULL)
2658 kmem_cache_destroy(kgnilnd_data.kgn_tx_phys_cache);
2660 if (kgnilnd_data.kgn_dgram_cache != NULL)
2661 kmem_cache_destroy(kgnilnd_data.kgn_dgram_cache);
2663 if (kgnilnd_data.kgn_cksum_map_pages != NULL) {
2664 for (i = 0; i < kgnilnd_data.kgn_cksum_npages; i++) {
2665 if (kgnilnd_data.kgn_cksum_map_pages[i] != NULL) {
2666 kfree(kgnilnd_data.kgn_cksum_map_pages[i]);
2669 kfree(kgnilnd_data.kgn_cksum_map_pages);
2672 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2673 atomic_read(&libcfs_kmemory));
2675 kgnilnd_data.kgn_init = GNILND_INIT_NOTHING;
2676 module_put(THIS_MODULE);
2682 kgnilnd_startup(struct lnet_ni *ni)
2688 LASSERTF(ni->ni_net->net_lnd == &the_kgnilnd,
2689 "bad LND 0x%p != the_kgnilnd @ 0x%p\n",
2690 ni->ni_net->net_lnd, &the_kgnilnd);
2692 if (kgnilnd_data.kgn_init == GNILND_INIT_NOTHING) {
2693 rc = kgnilnd_base_startup();
2698 /* Serialize with shutdown. */
2699 mutex_lock(&kgnilnd_data.kgn_quiesce_mutex);
2701 LIBCFS_ALLOC(net, sizeof(*net));
2703 CERROR("could not allocate net for new interface instance\n");
2704 /* no need to cleanup the CDM... */
2705 GOTO(failed, rc = -ENOMEM);
2707 INIT_LIST_HEAD(&net->gnn_list);
2710 if (!ni->ni_net->net_tunables_set) {
2711 ni->ni_net->net_tunables.lct_max_tx_credits =
2712 *kgnilnd_tunables.kgn_credits;
2713 ni->ni_net->net_tunables.lct_peer_tx_credits =
2714 *kgnilnd_tunables.kgn_peer_credits;
2717 if (*kgnilnd_tunables.kgn_peer_health) {
2720 /* give this a bit of leeway - we don't have a hard timeout
2721 * as we only check timeouts periodically - see comment in kgnilnd_reaper */
2722 fudge = (GNILND_TO2KA(*kgnilnd_tunables.kgn_timeout) / GNILND_REAPER_NCHECKS);
2723 timeout = *kgnilnd_tunables.kgn_timeout + fudge;
2725 if (*kgnilnd_tunables.kgn_peer_timeout >= timeout) {
2726 ni->ni_net->net_tunables.lct_peer_timeout =
2727 *kgnilnd_tunables.kgn_peer_timeout;
2728 } else if (*kgnilnd_tunables.kgn_peer_timeout > -1) {
2729 LCONSOLE_ERROR("Peer_timeout is set to %d but needs to be >= %d\n",
2730 *kgnilnd_tunables.kgn_peer_timeout,
2733 LIBCFS_FREE(net, sizeof(*net));
2734 GOTO(failed, rc = -EINVAL);
2736 ni->ni_net->net_tunables.lct_peer_timeout = timeout;
2738 LCONSOLE_INFO("Enabling LNet peer health for gnilnd, timeout %ds\n",
2739 ni->ni_net->net_tunables.lct_peer_timeout);
2742 atomic_set(&net->gnn_refcount, 1);
2744 /* if we have multiple devices, spread the nets around */
2745 net->gnn_netnum = LNET_NETNUM(LNET_NIDNET(ni->ni_nid));
2747 devno = LNET_NIDNET(ni->ni_nid) % GNILND_MAXDEVS;
2748 net->gnn_dev = &kgnilnd_data.kgn_devices[devno];
2750 /* allocate a 'dummy' cdm for datagram use. We can only have a single
2751 * datagram between a nid:inst_id and nid2:inst_id. The fake cdm
2752 * give us additional inst_id to use, allowing the datagrams to flow
2753 * like rivers of honey and beer */
2755 /* the instance id for the cdm is the NETNUM offset by MAXDEVS -
2756 * ensuring we'll have a unique id */
2759 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), net->gnn_dev->gnd_nid);
2760 CDEBUG(D_NET, "adding net %p nid=%s on dev %d \n",
2761 net, libcfs_nid2str(ni->ni_nid), net->gnn_dev->gnd_id);
2762 /* until the gnn_list is set, we need to cleanup ourselves as
2763 * kgnilnd_shutdown is just gonna get confused */
2765 down_write(&kgnilnd_data.kgn_net_rw_sem);
2766 list_add_tail(&net->gnn_list, kgnilnd_netnum2netlist(net->gnn_netnum));
2767 up_write(&kgnilnd_data.kgn_net_rw_sem);
2769 /* we need a separate thread to call probe_wait_by_id until
2770 * we get a function callback notifier from kgni */
2771 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2774 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2775 kgnilnd_shutdown(ni);
2780 kgnilnd_shutdown(struct lnet_ni *ni)
2782 kgn_net_t *net = ni->ni_data;
2787 CFS_RACE(CFS_FAIL_GNI_SR_DOWN_RACE);
2789 LASSERTF(kgnilnd_data.kgn_init == GNILND_INIT_ALL,
2790 "init %d\n", kgnilnd_data.kgn_init);
2792 /* Serialize with startup. */
2793 mutex_lock(&kgnilnd_data.kgn_quiesce_mutex);
2794 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2795 atomic_read(&libcfs_kmemory));
2798 CERROR("got NULL net for ni %p\n", ni);
2799 GOTO(out, rc = -EINVAL);
2802 LASSERTF(ni == net->gnn_ni,
2803 "ni %p gnn_ni %p\n", net, net->gnn_ni);
2807 LASSERT(!net->gnn_shutdown);
2808 LASSERTF(atomic_read(&net->gnn_refcount) != 0,
2809 "net %p refcount %d\n",
2810 net, atomic_read(&net->gnn_refcount));
2812 if (!list_empty(&net->gnn_list)) {
2813 /* serialize with peer creation */
2814 down_write(&kgnilnd_data.kgn_net_rw_sem);
2815 net->gnn_shutdown = 1;
2816 up_write(&kgnilnd_data.kgn_net_rw_sem);
2818 kgnilnd_cancel_net_dgrams(net);
2820 kgnilnd_del_conn_or_peer(net, LNET_NID_ANY, GNILND_DEL_PEER, -ESHUTDOWN);
2822 /* if we are quiesced, need to wake up - we need those threads
2823 * alive to release peers, etc */
2824 if (GNILND_IS_QUIESCED) {
2825 set_mb(kgnilnd_data.kgn_quiesce_trigger, GNILND_QUIESCE_IDLE);
2826 kgnilnd_quiesce_wait("shutdown");
2829 kgnilnd_wait_for_canceled_dgrams(net->gnn_dev);
2831 /* We wait until the nets ref's are 1, we will release final ref which is ours
2832 * this allows us to make sure everything else is done before we free the
2836 while (atomic_read(&net->gnn_refcount) != 1) {
2838 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2839 "Waiting for %d references to clear on net %d\n",
2840 atomic_read(&net->gnn_refcount),
2842 set_current_state(TASK_UNINTERRUPTIBLE);
2843 schedule_timeout(cfs_time_seconds(1));
2846 /* release ref from kgnilnd_startup */
2847 kgnilnd_net_decref(net);
2848 /* serialize with reaper and conn_task looping */
2849 down_write(&kgnilnd_data.kgn_net_rw_sem);
2850 list_del_init(&net->gnn_list);
2851 up_write(&kgnilnd_data.kgn_net_rw_sem);
2855 /* not locking, this can't race with writers */
2856 LASSERTF(atomic_read(&net->gnn_refcount) == 0,
2857 "net %p refcount %d\n",
2858 net, atomic_read(&net->gnn_refcount));
2859 LIBCFS_FREE(net, sizeof(*net));
2862 down_read(&kgnilnd_data.kgn_net_rw_sem);
2863 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
2864 if (!list_empty(&kgnilnd_data.kgn_nets[i])) {
2865 up_read(&kgnilnd_data.kgn_net_rw_sem);
2869 if (i == *kgnilnd_tunables.kgn_net_hash_size - 1) {
2870 up_read(&kgnilnd_data.kgn_net_rw_sem);
2871 kgnilnd_base_shutdown();
2874 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2875 atomic_read(&libcfs_kmemory));
2877 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2881 static void __exit kgnilnd_exit(void)
2883 lnet_unregister_lnd(&the_kgnilnd);
2884 kgnilnd_proc_fini();
2885 kgnilnd_remove_sysctl();
2888 static int __init kgnilnd_init(void)
2892 rc = kgnilnd_tunables_init();
2896 LCONSOLE_INFO("Lustre: kgnilnd build version: "LUSTRE_VERSION_STRING"\n");
2898 kgnilnd_insert_sysctl();
2899 kgnilnd_proc_init();
2901 lnet_register_lnd(&the_kgnilnd);
2906 MODULE_AUTHOR("Cray, Inc. <nic@cray.com>");
2907 MODULE_DESCRIPTION("Gemini LNet Network Driver");
2908 MODULE_VERSION(LUSTRE_VERSION_STRING);
2909 MODULE_LICENSE("GPL");
2911 module_init(kgnilnd_init);
2912 module_exit(kgnilnd_exit);