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 const 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,
38 kgn_data_t kgnilnd_data;
41 kgnilnd_thread_start(int(*fn)(void *arg), void *arg, char *name, int id)
43 struct task_struct *thrd;
45 thrd = kthread_run(fn, arg, "%s_%02d", name, id);
49 atomic_inc(&kgnilnd_data.kgn_nthreads);
53 /* bind scheduler threads to cpus */
55 kgnilnd_start_sd_threads(void)
59 struct task_struct *task;
61 for_each_online_cpu(cpu) {
62 /* don't bind to cpu 0 - all interrupts are processed here */
66 task = kthread_create(kgnilnd_scheduler, (void *)((long)i),
67 "%s_%02d", "kgnilnd_sd", i);
69 kthread_bind(task, cpu);
70 wake_up_process(task);
72 CERROR("Can't spawn gnilnd scheduler[%d] %ld\n", i,
76 atomic_inc(&kgnilnd_data.kgn_nthreads);
78 if (++i >= *kgnilnd_tunables.kgn_sched_threads) {
86 /* needs write_lock on kgn_peer_conn_lock */
88 kgnilnd_close_stale_conns_locked(kgn_peer_t *peer, kgn_conn_t *newconn)
91 struct list_head *ctmp, *cnxt;
95 loopback = peer->gnp_nid == peer->gnp_net->gnn_ni->ni_nid;
97 list_for_each_safe(ctmp, cnxt, &peer->gnp_conns) {
98 conn = list_entry(ctmp, kgn_conn_t, gnc_list);
100 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
106 if (conn->gnc_device != newconn->gnc_device)
109 /* This is a two connection loopback - one talking to the other */
111 newconn->gnc_my_connstamp == conn->gnc_peer_connstamp &&
112 newconn->gnc_peer_connstamp == conn->gnc_my_connstamp) {
113 CDEBUG(D_NET, "skipping prune of %p, "
114 "loopback and matching stamps"
115 " connstamp %llu(%llu)"
116 " peerstamp %llu(%llu)\n",
117 conn, newconn->gnc_my_connstamp,
118 conn->gnc_peer_connstamp,
119 newconn->gnc_peer_connstamp,
120 conn->gnc_my_connstamp);
124 if (conn->gnc_peerstamp != newconn->gnc_peerstamp) {
125 LASSERTF(conn->gnc_peerstamp < newconn->gnc_peerstamp,
126 "conn 0x%p peerstamp %llu >= "
127 "newconn 0x%p peerstamp %llu\n",
128 conn, conn->gnc_peerstamp,
129 newconn, newconn->gnc_peerstamp);
131 CDEBUG(D_NET, "Closing stale conn nid: %s "
132 " peerstamp:%#llx(%#llx)\n",
133 libcfs_nid2str(peer->gnp_nid),
134 conn->gnc_peerstamp, newconn->gnc_peerstamp);
137 LASSERTF(conn->gnc_peer_connstamp < newconn->gnc_peer_connstamp,
138 "conn 0x%p peer_connstamp %llu >= "
139 "newconn 0x%p peer_connstamp %llu\n",
140 conn, conn->gnc_peer_connstamp,
141 newconn, newconn->gnc_peer_connstamp);
143 CDEBUG(D_NET, "Closing stale conn nid: %s"
144 " connstamp:%llu(%llu)\n",
145 libcfs_nid2str(peer->gnp_nid),
146 conn->gnc_peer_connstamp, newconn->gnc_peer_connstamp);
150 kgnilnd_close_conn_locked(conn, -ESTALE);
154 CWARN("Closed %d stale conns to %s\n", count, libcfs_nid2str(peer->gnp_nid));
161 kgnilnd_conn_isdup_locked(kgn_peer_t *peer, kgn_conn_t *newconn)
164 struct list_head *tmp;
168 loopback = peer->gnp_nid == peer->gnp_net->gnn_ni->ni_nid;
170 list_for_each(tmp, &peer->gnp_conns) {
171 conn = list_entry(tmp, kgn_conn_t, gnc_list);
172 CDEBUG(D_NET, "checking conn 0x%p for peer %s"
173 " lo %d new %llu existing %llu"
174 " new peer %llu existing peer %llu"
175 " new dev %p existing dev %p\n",
176 conn, libcfs_nid2str(peer->gnp_nid),
178 newconn->gnc_peerstamp, conn->gnc_peerstamp,
179 newconn->gnc_peer_connstamp, conn->gnc_peer_connstamp,
180 newconn->gnc_device, conn->gnc_device);
182 /* conn is in the process of closing */
183 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
186 /* 'newconn' is from an earlier version of 'peer'!!! */
187 if (newconn->gnc_peerstamp < conn->gnc_peerstamp)
190 /* 'conn' is from an earlier version of 'peer': it will be
191 * removed when we cull stale conns later on... */
192 if (newconn->gnc_peerstamp > conn->gnc_peerstamp)
195 /* Different devices are OK */
196 if (conn->gnc_device != newconn->gnc_device)
199 /* It's me connecting to myself */
201 newconn->gnc_my_connstamp == conn->gnc_peer_connstamp &&
202 newconn->gnc_peer_connstamp == conn->gnc_my_connstamp)
205 /* 'newconn' is an earlier connection from 'peer'!!! */
206 if (newconn->gnc_peer_connstamp < conn->gnc_peer_connstamp)
209 /* 'conn' is an earlier connection from 'peer': it will be
210 * removed when we cull stale conns later on... */
211 if (newconn->gnc_peer_connstamp > conn->gnc_peer_connstamp)
214 /* 'newconn' has the SAME connection stamp; 'peer' isn't
215 * playing the game... */
223 kgnilnd_create_conn(kgn_conn_t **connp, kgn_device_t *dev)
229 LASSERT (!in_interrupt());
230 atomic_inc(&kgnilnd_data.kgn_nconns);
232 /* divide by 2 to allow for complete reset and immediate reconnect */
233 if (atomic_read(&kgnilnd_data.kgn_nconns) >= GNILND_MAX_CQID/2) {
234 CERROR("Too many conn are live: %d > %d\n",
235 atomic_read(&kgnilnd_data.kgn_nconns), GNILND_MAX_CQID/2);
236 atomic_dec(&kgnilnd_data.kgn_nconns);
240 LIBCFS_ALLOC(conn, sizeof(*conn));
242 atomic_dec(&kgnilnd_data.kgn_nconns);
246 conn->gnc_tx_ref_table =
247 kgnilnd_vzalloc(GNILND_MAX_MSG_ID * sizeof(void *));
248 if (conn->gnc_tx_ref_table == NULL) {
249 CERROR("Can't allocate conn tx_ref_table\n");
250 GOTO(failed, rc = -ENOMEM);
253 mutex_init(&conn->gnc_smsg_mutex);
254 mutex_init(&conn->gnc_rdma_mutex);
255 atomic_set(&conn->gnc_refcount, 1);
256 atomic_set(&conn->gnc_reaper_noop, 0);
257 atomic_set(&conn->gnc_sched_noop, 0);
258 atomic_set(&conn->gnc_tx_in_use, 0);
259 INIT_LIST_HEAD(&conn->gnc_list);
260 INIT_LIST_HEAD(&conn->gnc_hashlist);
261 INIT_LIST_HEAD(&conn->gnc_schedlist);
262 INIT_LIST_HEAD(&conn->gnc_fmaq);
263 INIT_LIST_HEAD(&conn->gnc_mdd_list);
264 INIT_LIST_HEAD(&conn->gnc_delaylist);
265 spin_lock_init(&conn->gnc_list_lock);
266 spin_lock_init(&conn->gnc_tx_lock);
267 conn->gnc_magic = GNILND_CONN_MAGIC;
269 /* set tx id to nearly the end to make sure we find wrapping
271 conn->gnc_next_tx = (int) GNILND_MAX_MSG_ID - 10;
273 /* if this fails, we have conflicts and MAX_TX is too large */
274 BUILD_BUG_ON(GNILND_MAX_MSG_ID >= GNILND_MSGID_CLOSE);
276 /* get a new unique CQ id for this conn */
277 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
278 conn->gnc_my_connstamp = kgnilnd_data.kgn_connstamp++;
279 conn->gnc_cqid = kgnilnd_get_cqid_locked();
280 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
282 if (conn->gnc_cqid == 0) {
283 CERROR("Could not allocate unique CQ ID for conn 0x%p\n", conn);
284 GOTO(failed, rc = -E2BIG);
287 CDEBUG(D_NET, "alloc cqid %u for conn 0x%p\n",
288 conn->gnc_cqid, conn);
290 /* need to be set before gnc_ephandle to allow kgnilnd_destroy_conn_ep to
292 conn->gnc_device = dev;
294 conn->gnc_timeout = max(*kgnilnd_tunables.kgn_timeout,
296 kgnilnd_update_reaper_timeout(conn->gnc_timeout);
298 /* this is the ep_handle for doing SMSG & BTE */
299 mutex_lock(&dev->gnd_cq_mutex);
300 rrc = kgnilnd_ep_create(dev->gnd_handle, dev->gnd_snd_fma_cqh,
301 &conn->gnc_ephandle);
302 mutex_unlock(&dev->gnd_cq_mutex);
303 if (rrc != GNI_RC_SUCCESS)
304 GOTO(failed, rc = -ENETDOWN);
306 CDEBUG(D_NET, "created conn 0x%p ep_hndl 0x%p\n",
307 conn, conn->gnc_ephandle);
309 /* add ref for EP canceling */
310 kgnilnd_conn_addref(conn);
311 atomic_inc(&dev->gnd_neps);
317 atomic_dec(&kgnilnd_data.kgn_nconns);
318 kgnilnd_vfree(conn->gnc_tx_ref_table,
319 GNILND_MAX_MSG_ID * sizeof(void *));
320 LIBCFS_FREE(conn, sizeof(*conn));
324 /* needs to be called with kgn_peer_conn_lock held (read or write) */
326 kgnilnd_find_conn_locked(kgn_peer_t *peer)
328 kgn_conn_t *conn = NULL;
330 /* if we are in reset, this conn is going to die soon */
331 if (unlikely(kgnilnd_data.kgn_in_reset)) {
335 /* just return the first ESTABLISHED connection */
336 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
337 /* kgnilnd_finish_connect doesn't put connections on the
338 * peer list until they are actually established */
339 LASSERTF(conn->gnc_state >= GNILND_CONN_ESTABLISHED,
340 "found conn %p state %s on peer %p (%s)\n",
341 conn, kgnilnd_conn_state2str(conn), peer,
342 libcfs_nid2str(peer->gnp_nid));
343 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
351 /* needs write_lock on kgn_peer_conn_lock held */
353 kgnilnd_find_or_create_conn_locked(kgn_peer_t *peer) {
355 kgn_device_t *dev = peer->gnp_net->gnn_dev;
358 conn = kgnilnd_find_conn_locked(peer);
364 /* if the peer was previously connecting, check if we should
365 * trigger another connection attempt yet. */
366 if (time_before(jiffies, peer->gnp_reconnect_time)) {
370 /* This check prevents us from creating a new connection to a peer while we are
371 * still in the process of closing an existing connection to the peer.
373 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
374 if (conn->gnc_ephandle != NULL) {
375 CDEBUG(D_NET, "Not connecting non-null ephandle found peer 0x%p->%s\n", peer,
376 libcfs_nid2str(peer->gnp_nid));
381 if (peer->gnp_connecting != GNILND_PEER_IDLE) {
382 /* if we are not connecting, fire up a new connection */
383 /* or if we are anything but IDLE DONT start a new connection */
387 CDEBUG(D_NET, "starting connect to %s\n",
388 libcfs_nid2str(peer->gnp_nid));
389 peer->gnp_connecting = GNILND_PEER_CONNECT;
390 kgnilnd_peer_addref(peer); /* extra ref for connd */
392 spin_lock(&dev->gnd_connd_lock);
393 list_add_tail(&peer->gnp_connd_list, &dev->gnd_connd_peers);
394 spin_unlock(&dev->gnd_connd_lock);
396 kgnilnd_schedule_dgram(dev);
397 CDEBUG(D_NETTRACE, "scheduling new connect\n");
402 /* Caller is responsible for deciding if/when to call this */
404 kgnilnd_destroy_conn_ep(kgn_conn_t *conn)
407 gni_ep_handle_t tmp_ep;
409 /* only if we actually initialized it,
410 * then set NULL to tell kgnilnd_destroy_conn to leave it alone */
412 tmp_ep = xchg(&conn->gnc_ephandle, NULL);
413 if (tmp_ep != NULL) {
414 /* we never re-use the EP, so unbind is not needed */
415 mutex_lock(&conn->gnc_device->gnd_cq_mutex);
416 rrc = kgnilnd_ep_destroy(tmp_ep);
418 mutex_unlock(&conn->gnc_device->gnd_cq_mutex);
420 /* if this fails, it could hork up kgni smsg retransmit and others
421 * since we could free the SMSG mbox memory, etc. */
422 LASSERTF(rrc == GNI_RC_SUCCESS, "rrc %d conn 0x%p ep 0x%p\n",
423 rrc, conn, conn->gnc_ephandle);
425 atomic_dec(&conn->gnc_device->gnd_neps);
427 /* clear out count added in kgnilnd_close_conn_locked
428 * conn will have a peer once it hits finish_connect, where it
429 * is the first spot we'll mark it ESTABLISHED as well */
430 if (conn->gnc_peer) {
431 kgnilnd_admin_decref(conn->gnc_peer->gnp_dirty_eps);
434 /* drop ref for EP */
435 kgnilnd_conn_decref(conn);
440 kgnilnd_destroy_conn(kgn_conn_t *conn)
442 LASSERTF(!in_interrupt() &&
443 !conn->gnc_scheduled &&
444 !conn->gnc_in_purgatory &&
445 conn->gnc_ephandle == NULL &&
446 list_empty(&conn->gnc_list) &&
447 list_empty(&conn->gnc_hashlist) &&
448 list_empty(&conn->gnc_schedlist) &&
449 list_empty(&conn->gnc_mdd_list) &&
450 list_empty(&conn->gnc_delaylist) &&
451 conn->gnc_magic == GNILND_CONN_MAGIC,
452 "conn 0x%p->%s IRQ %d sched %d purg %d ep 0x%p Mg %d lists %d/%d/%d/%d/%d\n",
453 conn, conn->gnc_peer ? libcfs_nid2str(conn->gnc_peer->gnp_nid)
455 !!in_interrupt(), conn->gnc_scheduled,
456 conn->gnc_in_purgatory,
459 list_empty(&conn->gnc_list),
460 list_empty(&conn->gnc_hashlist),
461 list_empty(&conn->gnc_schedlist),
462 list_empty(&conn->gnc_mdd_list),
463 list_empty(&conn->gnc_delaylist));
465 /* Tripping these is especially bad, as it means we have items on the
466 * lists that didn't keep their refcount on the connection - or
467 * somebody evil released their own */
468 LASSERTF(list_empty(&conn->gnc_fmaq) &&
469 atomic_read(&conn->gnc_nlive_fma) == 0 &&
470 atomic_read(&conn->gnc_nlive_rdma) == 0,
471 "conn 0x%p fmaq %d@0x%p nfma %d nrdma %d\n",
472 conn, kgnilnd_count_list(&conn->gnc_fmaq), &conn->gnc_fmaq,
473 atomic_read(&conn->gnc_nlive_fma), atomic_read(&conn->gnc_nlive_rdma));
475 CDEBUG(D_NET, "destroying conn %p ephandle %p error %d\n",
476 conn, conn->gnc_ephandle, conn->gnc_error);
478 /* We are freeing this memory remove the magic value from the connection */
481 /* if there is an FMA blk left here, we'll tear it down */
482 if (conn->gnc_fma_blk) {
483 if (conn->gnc_peer) {
484 kgn_mbox_info_t *mbox;
485 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
486 mbox->mbx_prev_nid = conn->gnc_peer->gnp_nid;
488 kgnilnd_release_mbox(conn, 0);
491 if (conn->gnc_peer != NULL)
492 kgnilnd_peer_decref(conn->gnc_peer);
494 if (conn->gnc_tx_ref_table != NULL) {
495 kgnilnd_vfree(conn->gnc_tx_ref_table,
496 GNILND_MAX_MSG_ID * sizeof(void *));
499 LIBCFS_FREE(conn, sizeof(*conn));
500 atomic_dec(&kgnilnd_data.kgn_nconns);
503 /* peer_alive and peer_notify done in the style of the o2iblnd */
505 kgnilnd_peer_alive(kgn_peer_t *peer)
507 time64_t now = ktime_get_seconds();
509 set_mb(peer->gnp_last_alive, now);
513 kgnilnd_peer_notify(kgn_peer_t *peer, int error, int alive)
524 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_DONT_NOTIFY))
527 /* Tell LNet we are giving ups on this peer - but only
528 * if it isn't already reconnected or trying to reconnect */
529 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
531 /* use kgnilnd_find_conn_locked to avoid any conns in the process of being nuked
533 * don't tell LNet if we are in reset - we assume that everyone will be able to
534 * reconnect just fine
536 conn = kgnilnd_find_conn_locked(peer);
538 CDEBUG(D_NETTRACE, "peer 0x%p->%s ting %d conn 0x%p, rst %d error %d\n",
539 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting, conn,
540 kgnilnd_data.kgn_in_reset, error);
542 if (((peer->gnp_connecting == GNILND_PEER_IDLE) &&
544 (!kgnilnd_data.kgn_in_reset) &&
545 (!kgnilnd_conn_clean_errno(error))) || alive) {
549 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
552 /* short circuit if we dont need to notify Lnet */
556 rc = down_read_trylock(&kgnilnd_data.kgn_net_rw_sem);
559 /* dont do this if this fails since LNET is in shutdown or something else
562 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
563 list_for_each_entry(net , &kgnilnd_data.kgn_nets[i], gnn_list) {
564 /* if gnn_shutdown set for any net shutdown is in progress just return */
565 if (net->gnn_shutdown) {
566 up_read(&kgnilnd_data.kgn_net_rw_sem);
574 /* shutdown in progress most likely */
575 up_read(&kgnilnd_data.kgn_net_rw_sem);
579 CFS_ALLOC_PTR_ARRAY(nets, nnets);
582 up_read(&kgnilnd_data.kgn_net_rw_sem);
583 CERROR("Failed to allocate nets[%d]\n", nnets);
588 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
589 list_for_each_entry(net, &kgnilnd_data.kgn_nets[i], gnn_list) {
591 kgnilnd_net_addref(net);
595 up_read(&kgnilnd_data.kgn_net_rw_sem);
597 for (i = 0; i < nnets; i++) {
602 peer_nid = kgnilnd_lnd2lnetnid(net->gnn_ni->ni_nid,
605 CDEBUG(D_NET, "peer 0x%p->%s last_alive %lld (%llds ago)\n",
606 peer, libcfs_nid2str(peer_nid), peer->gnp_last_alive,
607 ktime_get_seconds() - peer->gnp_last_alive);
609 lnet_notify(net->gnn_ni, peer_nid, alive, true,
610 peer->gnp_last_alive);
612 kgnilnd_net_decref(net);
615 CFS_FREE_PTR_ARRAY(nets, nnets);
619 /* need write_lock on kgn_peer_conn_lock */
621 kgnilnd_close_conn_locked(kgn_conn_t *conn, int error)
623 kgn_peer_t *peer = conn->gnc_peer;
626 LASSERT(!in_interrupt());
628 /* store error for tx completion */
629 conn->gnc_error = error;
630 peer->gnp_last_errno = error;
632 /* use real error from peer if possible */
633 if (error == -ECONNRESET) {
634 error = conn->gnc_peer_error;
637 /* if we NETERROR, make sure it is rate limited */
638 if (!kgnilnd_conn_clean_errno(error) &&
639 peer->gnp_state != GNILND_PEER_DOWN) {
640 CNETERR("closing conn to %s: error %d\n",
641 libcfs_nid2str(peer->gnp_nid), error);
643 CDEBUG(D_NET, "closing conn to %s: error %d\n",
644 libcfs_nid2str(peer->gnp_nid), error);
647 LASSERTF(conn->gnc_state == GNILND_CONN_ESTABLISHED,
648 "conn %p to %s with bogus state %s\n", conn,
649 libcfs_nid2str(conn->gnc_peer->gnp_nid),
650 kgnilnd_conn_state2str(conn));
651 LASSERT(!list_empty(&conn->gnc_hashlist));
652 LASSERT(!list_empty(&conn->gnc_list));
655 /* mark peer count here so any place the EP gets destroyed will
656 * open up the peer count so that a new ESTABLISHED conn is then free
657 * to send new messages -- sending before the previous EPs are destroyed
658 * could end up with messages on the network for the old conn _after_
659 * the new conn and break the mbox safety protocol */
660 kgnilnd_admin_addref(conn->gnc_peer->gnp_dirty_eps);
662 /* Remove from conn hash table: no new callbacks */
663 list_del_init(&conn->gnc_hashlist);
664 kgnilnd_data.kgn_conn_version++;
665 kgnilnd_conn_decref(conn);
667 /* if we are in reset, go right to CLOSED as there is no scheduler
668 * thread to move from CLOSING to CLOSED */
669 if (unlikely(kgnilnd_data.kgn_in_reset)) {
670 conn->gnc_state = GNILND_CONN_CLOSED;
672 conn->gnc_state = GNILND_CONN_CLOSING;
675 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_RDMA_CQ_ERROR)) {
676 msleep_interruptible(MSEC_PER_SEC);
679 /* leave on peer->gnp_conns to make sure we don't let the reaper
680 * or others try to unlink this peer until the conn is fully
681 * processed for closing */
683 if (kgnilnd_check_purgatory_conn(conn)) {
684 kgnilnd_add_purgatory_locked(conn, conn->gnc_peer);
687 /* Reset RX timeout to ensure we wait for an incoming CLOSE
688 * for the full timeout. If we get a CLOSE we know the
689 * peer has stopped all RDMA. Otherwise if we wait for
690 * the full timeout we can also be sure all RDMA has stopped. */
691 conn->gnc_last_rx = conn->gnc_last_rx_cq = jiffies;
694 /* schedule sending CLOSE - if we are in quiesce, this adds to
695 * gnd_ready_conns and allows us to find it in quiesce processing */
696 kgnilnd_schedule_conn(conn);
702 kgnilnd_close_conn(kgn_conn_t *conn, int error)
704 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
705 /* need to check the state here - this call is racy and we don't
706 * know the state until after the lock is grabbed */
707 if (conn->gnc_state == GNILND_CONN_ESTABLISHED) {
708 kgnilnd_close_conn_locked(conn, error);
710 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
714 kgnilnd_complete_closed_conn(kgn_conn_t *conn)
724 /* Dump log on cksum error - wait until complete phase to let
725 * RX of error happen */
726 if (*kgnilnd_tunables.kgn_checksum_dump &&
727 (conn != NULL && conn->gnc_peer_error == -ENOKEY)) {
728 libcfs_debug_dumplog();
731 /* _CLOSED set in kgnilnd_process_fmaq once we decide to
732 * send the CLOSE or not */
733 LASSERTF(conn->gnc_state == GNILND_CONN_CLOSED,
734 "conn 0x%p->%s with bad state %s\n",
735 conn, conn->gnc_peer ?
736 libcfs_nid2str(conn->gnc_peer->gnp_nid) :
738 kgnilnd_conn_state2str(conn));
740 LASSERT(list_empty(&conn->gnc_hashlist));
741 /* We shouldnt be on the delay list, the conn can
742 * get added to this list during a retransmit, and retransmits
743 * only occur within scheduler threads.
745 LASSERT(list_empty(&conn->gnc_delaylist));
747 /* we've sent the close, start nuking */
748 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_SCHEDULE_COMPLETE))
749 kgnilnd_schedule_conn(conn);
751 if (conn->gnc_scheduled != GNILND_CONN_PROCESS) {
752 CDEBUG(D_NETERROR, "Error someone scheduled us after we were "
753 "done, Attempting to recover conn 0x%p "
754 "scheduled %d function: %s line: %d\n", conn,
755 conn->gnc_scheduled, conn->gnc_sched_caller,
756 conn->gnc_sched_line);
760 /* we don't use lists to track things that we can get out of the
763 /* need to hold locks for tx_list_state, sampling it is too racy:
764 * - the lock actually protects tx != NULL, but we can't take the proper
765 * lock until we check tx_list_state, which would be too late and
766 * we could have the TX change under us.
767 * gnd_rdmaq_lock and gnd_lock and not used together, so taking both
769 spin_lock(&conn->gnc_device->gnd_rdmaq_lock);
770 spin_lock(&conn->gnc_device->gnd_lock);
772 for (nrdma = 0; nrdma < GNILND_MAX_MSG_ID; nrdma++) {
773 tx = conn->gnc_tx_ref_table[nrdma];
776 /* only print the first error and if not CLOSE, we often don't see
777 * CQ events for that by the time we get here... and really don't care */
778 if (nlive || tx->tx_msg.gnm_type == GNILND_MSG_CLOSE)
779 tx->tx_state |= GNILND_TX_QUIET_ERROR;
781 GNIDBG_TX(D_NET, tx, "cleaning up on close, nlive %d", nlive);
783 /* don't worry about gnc_lock here as nobody else should be
784 * touching this conn */
785 kgnilnd_tx_del_state_locked(tx, NULL, conn, GNILND_TX_ALLOCD);
786 list_add_tail(&tx->tx_list, &sinners);
789 spin_unlock(&conn->gnc_device->gnd_lock);
790 spin_unlock(&conn->gnc_device->gnd_rdmaq_lock);
792 /* nobody should have marked this as needing scheduling after
793 * we called close - so only ref should be us handling it */
794 if (conn->gnc_scheduled != GNILND_CONN_PROCESS) {
795 CDEBUG(D_NETERROR, "Error someone scheduled us after we were "
796 "done, Attempting to recover conn 0x%p "
797 "scheduled %d function %s line: %d\n", conn,
798 conn->gnc_scheduled, conn->gnc_sched_caller,
799 conn->gnc_sched_line);
801 /* now reset a few to actual counters... */
802 nrdma = atomic_read(&conn->gnc_nlive_rdma);
803 nq_rdma = atomic_read(&conn->gnc_nq_rdma);
805 if (!list_empty(&sinners)) {
806 list_for_each_entry_safe(tx, txn, &sinners, tx_list) {
807 /* clear tx_list to make tx_add_list_locked happy */
808 list_del_init(&tx->tx_list);
809 /* The error codes determine if we hold onto the MDD */
810 kgnilnd_tx_done(tx, conn->gnc_error);
814 logmsg = (nlive + nrdma + nq_rdma);
817 int level = conn->gnc_peer->gnp_state == GNILND_PEER_UP ?
819 CDEBUG(level, "Closed conn 0x%p->%s (errno %d,"
820 " peer errno %d): canceled %d TX, %d/%d RDMA\n",
821 conn, libcfs_nid2str(conn->gnc_peer->gnp_nid),
822 conn->gnc_error, conn->gnc_peer_error,
823 nlive, nq_rdma, nrdma);
826 kgnilnd_destroy_conn_ep(conn);
828 /* Bug 765042 - race this with completing a new conn to same peer - we need
829 * finish_connect to detach purgatory before we can do it ourselves here */
830 CFS_RACE(CFS_FAIL_GNI_FINISH_PURG);
832 /* now it is safe to remove from peer list - anyone looking at
833 * gnp_conns now is free to unlink if not on purgatory */
834 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
836 conn->gnc_state = GNILND_CONN_DONE;
838 /* Decrement counter if we are marked by del_conn_or_peers for closing
840 if (conn->gnc_needs_closing)
841 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_conns);
843 /* Remove from peer's list of valid connections if its not in purgatory */
844 if (!conn->gnc_in_purgatory) {
845 list_del_init(&conn->gnc_list);
846 /* Lose peers reference on the conn */
847 kgnilnd_conn_decref(conn);
850 /* NB - only unlinking if we set pending in del_peer_locked from admin or
852 if (kgnilnd_peer_active(conn->gnc_peer) &&
853 conn->gnc_peer->gnp_pending_unlink &&
854 kgnilnd_can_unlink_peer_locked(conn->gnc_peer)) {
855 kgnilnd_unlink_peer_locked(conn->gnc_peer);
858 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
860 /* I'm telling Mommy! - use peer_error if they initiated close */
861 kgnilnd_peer_notify(conn->gnc_peer,
862 conn->gnc_error == -ECONNRESET ?
863 conn->gnc_peer_error : conn->gnc_error, 0);
869 kgnilnd_set_conn_params(kgn_dgram_t *dgram)
871 kgn_conn_t *conn = dgram->gndg_conn;
872 kgn_connreq_t *connreq = &dgram->gndg_conn_in;
873 kgn_gniparams_t *rem_param = &connreq->gncr_gnparams;
876 gni_smsg_attr_t *remote = &connreq->gncr_gnparams.gnpr_smsg_attr;
878 /* set timeout vals in conn early so we can use them for the NAK */
880 /* use max of the requested and our timeout, peer will do the same */
881 conn->gnc_timeout = max(conn->gnc_timeout, connreq->gncr_timeout);
883 /* only ep_bind really mucks around with the CQ */
884 /* only ep bind if we are not connecting to ourself and the dstnid is not a wildcard. this check
885 * is necessary as you can only bind an ep once and we must make sure we dont bind when already bound.
887 if (connreq->gncr_dstnid != LNET_NID_ANY && dgram->gndg_conn_out.gncr_dstnid != connreq->gncr_srcnid) {
888 mutex_lock(&conn->gnc_device->gnd_cq_mutex);
889 rrc = kgnilnd_ep_bind(conn->gnc_ephandle,
890 connreq->gncr_gnparams.gnpr_host_id,
892 mutex_unlock(&conn->gnc_device->gnd_cq_mutex);
893 if (rrc != GNI_RC_SUCCESS) {
899 rrc = kgnilnd_ep_set_eventdata(conn->gnc_ephandle, conn->gnc_cqid,
900 connreq->gncr_gnparams.gnpr_cqid);
901 if (rrc != GNI_RC_SUCCESS) {
906 /* Initialize SMSG */
907 rrc = kgnilnd_smsg_init(conn->gnc_ephandle, &conn->gnpr_smsg_attr,
908 &connreq->gncr_gnparams.gnpr_smsg_attr);
909 if (unlikely(rrc == GNI_RC_INVALID_PARAM)) {
910 gni_smsg_attr_t *local = &conn->gnpr_smsg_attr;
911 /* help folks figure out if there is a tunable off, etc. */
912 LCONSOLE_ERROR("SMSG attribute mismatch. Data from local/remote:"
913 " type %d/%d msg_maxsize %u/%u"
914 " mbox_maxcredit %u/%u. Please check kgni"
915 " logs for further data\n",
916 local->msg_type, remote->msg_type,
917 local->msg_maxsize, remote->msg_maxsize,
918 local->mbox_maxcredit, remote->mbox_maxcredit);
920 if (rrc != GNI_RC_SUCCESS) {
925 /* log this for help in debuggin SMSG buffer re-use */
926 CDEBUG(D_NET, "conn %p src %s dst %s smsg %p acquired"
927 " local cqid %u SMSG %p->%u hndl %#llx.%#llx"
928 " remote cqid %u SMSG %p->%u hndl %#llx.%#llx\n",
929 conn, libcfs_nid2str(connreq->gncr_srcnid),
930 libcfs_nid2str(connreq->gncr_dstnid),
931 &conn->gnpr_smsg_attr,
933 conn->gnpr_smsg_attr.msg_buffer,
934 conn->gnpr_smsg_attr.mbox_offset,
935 conn->gnpr_smsg_attr.mem_hndl.qword1,
936 conn->gnpr_smsg_attr.mem_hndl.qword2,
937 rem_param->gnpr_cqid,
938 rem_param->gnpr_smsg_attr.msg_buffer,
939 rem_param->gnpr_smsg_attr.mbox_offset,
940 rem_param->gnpr_smsg_attr.mem_hndl.qword1,
941 rem_param->gnpr_smsg_attr.mem_hndl.qword2);
943 conn->gnc_peerstamp = connreq->gncr_peerstamp;
944 conn->gnc_peer_connstamp = connreq->gncr_connstamp;
945 conn->remote_mbox_addr = (void *)((char *)remote->msg_buffer + remote->mbox_offset);
947 /* We update the reaper timeout once we have a valid conn and timeout */
948 kgnilnd_update_reaper_timeout(GNILND_TO2KA(conn->gnc_timeout));
953 rrc = kgnilnd_ep_unbind(conn->gnc_ephandle);
954 /* not sure I can just let this fly */
955 LASSERTF(rrc == GNI_RC_SUCCESS,
956 "bad rc from gni_ep_unbind trying to cleanup: %d\n", rrc);
959 LASSERTF(rc != 0, "SOFTWARE BUG: rc == 0\n");
960 CERROR("Error setting connection params from %s: %d\n",
961 libcfs_nid2str(connreq->gncr_srcnid), rc);
965 /* needs down_read on kgn_net_rw_sem held from before this call until
966 * after the write_lock on kgn_peer_conn_lock - this ensures we stay sane
967 * with kgnilnd_shutdown - it'll get the sem and set shutdown, then get the
968 * kgn_peer_conn_lock to start del_peer'ing. If we hold the sem until after
969 * kgn_peer_conn_lock is held, we guarantee that nobody calls
970 * kgnilnd_add_peer_locked without checking gnn_shutdown */
972 kgnilnd_create_peer_safe(kgn_peer_t **peerp,
980 LASSERT(nid != LNET_NID_ANY);
982 /* We dont pass the net around in the dgram anymore so here is where we find it
983 * this will work unless its in shutdown or the nid has a net that is invalid.
984 * Either way error code needs to be returned in that case.
986 * If the net passed in is not NULL then we can use it, this alleviates looking it
987 * when the calling function has access to the data.
990 rc = kgnilnd_find_net(nid, &net);
994 /* find net adds a reference on the net if we are not using
995 * it we must do it manually so the net references are
996 * correct when tearing down the net
998 kgnilnd_net_addref(net);
1001 LIBCFS_ALLOC(peer, sizeof(*peer));
1003 kgnilnd_net_decref(net);
1006 peer->gnp_nid = nid;
1007 peer->gnp_state = node_state;
1009 /* translate from nid to nic addr & store */
1010 rc = kgnilnd_nid_to_nicaddrs(LNET_NIDADDR(nid), 1, &peer->gnp_host_id);
1012 kgnilnd_net_decref(net);
1013 LIBCFS_FREE(peer, sizeof(*peer));
1016 CDEBUG(D_NET, "peer 0x%p->%s -> NIC 0x%x\n", peer,
1017 libcfs_nid2str(nid), peer->gnp_host_id);
1019 atomic_set(&peer->gnp_refcount, 1); /* 1 ref for caller */
1020 atomic_set(&peer->gnp_dirty_eps, 0);
1022 INIT_LIST_HEAD(&peer->gnp_list);
1023 INIT_LIST_HEAD(&peer->gnp_connd_list);
1024 INIT_LIST_HEAD(&peer->gnp_conns);
1025 INIT_LIST_HEAD(&peer->gnp_tx_queue);
1027 /* the first reconnect should happen immediately, so we leave
1028 * gnp_reconnect_interval set to 0 */
1030 LASSERTF(net != NULL, "peer 0x%p->%s with NULL net\n",
1031 peer, libcfs_nid2str(nid));
1033 /* must have kgn_net_rw_sem held for this... */
1034 if (net->gnn_shutdown) {
1035 /* shutdown has started already */
1036 kgnilnd_net_decref(net);
1037 LIBCFS_FREE(peer, sizeof(*peer));
1041 peer->gnp_net = net;
1043 atomic_inc(&kgnilnd_data.kgn_npeers);
1050 kgnilnd_destroy_peer(kgn_peer_t *peer)
1052 CDEBUG(D_NET, "peer %s %p deleted\n",
1053 libcfs_nid2str(peer->gnp_nid), peer);
1054 LASSERTF(atomic_read(&peer->gnp_refcount) == 0,
1055 "peer 0x%p->%s refs %d\n",
1056 peer, libcfs_nid2str(peer->gnp_nid),
1057 atomic_read(&peer->gnp_refcount));
1058 LASSERTF(atomic_read(&peer->gnp_dirty_eps) == 0,
1059 "peer 0x%p->%s dirty eps %d\n",
1060 peer, libcfs_nid2str(peer->gnp_nid),
1061 atomic_read(&peer->gnp_dirty_eps));
1062 LASSERTF(peer->gnp_net != NULL, "peer %p (%s) with NULL net\n",
1063 peer, libcfs_nid2str(peer->gnp_nid));
1064 LASSERTF(!kgnilnd_peer_active(peer),
1066 peer, libcfs_nid2str(peer->gnp_nid));
1067 LASSERTF(peer->gnp_connecting == GNILND_PEER_IDLE || peer->gnp_connecting == GNILND_PEER_KILL,
1068 "peer 0x%p->%s, connecting %d\n",
1069 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting);
1070 LASSERTF(list_empty(&peer->gnp_conns),
1072 peer, libcfs_nid2str(peer->gnp_nid));
1073 LASSERTF(list_empty(&peer->gnp_tx_queue),
1075 peer, libcfs_nid2str(peer->gnp_nid));
1076 LASSERTF(list_empty(&peer->gnp_connd_list),
1078 peer, libcfs_nid2str(peer->gnp_nid));
1080 /* NB a peer's connections keep a reference on their peer until
1081 * they are destroyed, so we can be assured that _all_ state to do
1082 * with this peer has been cleaned up when its refcount drops to
1085 atomic_dec(&kgnilnd_data.kgn_npeers);
1086 kgnilnd_net_decref(peer->gnp_net);
1088 LIBCFS_FREE(peer, sizeof(*peer));
1091 /* the conn might not have made it all the way through to a connected
1092 * state - but we need to purgatory any conn that a remote peer might
1093 * have seen through a posted dgram as well */
1095 kgnilnd_add_purgatory_locked(kgn_conn_t *conn, kgn_peer_t *peer)
1097 kgn_mbox_info_t *mbox = NULL;
1100 /* NB - the caller should own conn by removing him from the
1101 * scheduler thread when finishing the close */
1103 LASSERTF(peer != NULL, "conn %p with NULL peer\n", conn);
1105 /* If this is still true, need to add the calls to unlink back in and
1106 * figure out how to close the hole on loopback conns */
1107 LASSERTF(kgnilnd_peer_active(peer), "can't use inactive peer %s (%p)"
1108 " we'll never recover the resources\n",
1109 libcfs_nid2str(peer->gnp_nid), peer);
1111 CDEBUG(D_NET, "conn %p peer %p dev %p\n", conn, peer,
1114 LASSERTF(conn->gnc_in_purgatory == 0,
1115 "Conn already in purgatory\n");
1116 conn->gnc_in_purgatory = 1;
1118 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
1119 mbox->mbx_prev_purg_nid = peer->gnp_nid;
1120 mbox->mbx_add_purgatory = jiffies;
1121 kgnilnd_release_mbox(conn, 1);
1123 LASSERTF(list_empty(&conn->gnc_mdd_list),
1124 "conn 0x%p->%s with active purgatory hold MDD %d\n",
1125 conn, libcfs_nid2str(peer->gnp_nid),
1126 kgnilnd_count_list(&conn->gnc_mdd_list));
1131 /* Instead of detaching everything from purgatory here we just mark the conn as needing
1132 * detach, when the reaper checks the conn the next time it will detach it.
1133 * Calling function requires write_lock held on kgn_peer_conn_lock
1136 kgnilnd_mark_for_detach_purgatory_all_locked(kgn_peer_t *peer) {
1139 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
1140 if (conn->gnc_in_purgatory && !conn->gnc_needs_detach) {
1141 conn->gnc_needs_detach = 1;
1142 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_detach);
1147 /* Calling function needs a write_lock held on kgn_peer_conn_lock */
1149 kgnilnd_detach_purgatory_locked(kgn_conn_t *conn, struct list_head *conn_list)
1151 kgn_mbox_info_t *mbox = NULL;
1153 /* if needed, add the conn purgatory data to the list passed in */
1154 if (conn->gnc_in_purgatory) {
1155 CDEBUG(D_NET, "peer %p->%s purg_conn %p@%s mdd_list #tx %d\n",
1156 conn->gnc_peer, libcfs_nid2str(conn->gnc_peer->gnp_nid),
1157 conn, kgnilnd_conn_state2str(conn),
1158 kgnilnd_count_list(&conn->gnc_mdd_list));
1160 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
1161 mbox->mbx_detach_of_purgatory = jiffies;
1163 /* conn->gnc_list is the entry point on peer->gnp_conns, so detaching it
1164 * here removes it from the list of 'valid' peer connections.
1165 * We put the current conn onto a list of conns to call kgnilnd_release_purgatory_locked()
1166 * and as such the caller of kgnilnd_detach_purgatory_locked() now owns that conn, since its not
1167 * on the peer's conn_list anymore.
1170 list_del_init(&conn->gnc_list);
1172 /* NB - only unlinking if we set pending in del_peer_locked from admin or
1174 if (kgnilnd_peer_active(conn->gnc_peer) &&
1175 conn->gnc_peer->gnp_pending_unlink &&
1176 kgnilnd_can_unlink_peer_locked(conn->gnc_peer)) {
1177 kgnilnd_unlink_peer_locked(conn->gnc_peer);
1179 /* The reaper will not call detach unless the conn is fully through kgnilnd_complete_closed_conn.
1180 * If the conn is not in a DONE state somehow we are attempting to detach even though
1181 * the conn has not been fully cleaned up. If we detach while the conn is still closing
1182 * we will end up with an orphaned connection that has valid ep_handle, that is not on a
1186 LASSERTF(conn->gnc_state == GNILND_CONN_DONE, "Conn in invalid state %p@%s \n",
1187 conn, kgnilnd_conn_state2str(conn));
1189 /* move from peer to the delayed release list */
1190 list_add_tail(&conn->gnc_list, conn_list);
1195 kgnilnd_release_purgatory_list(struct list_head *conn_list)
1198 kgn_conn_t *conn, *connN;
1199 kgn_mdd_purgatory_t *gmp, *gmpN;
1201 list_for_each_entry_safe(conn, connN, conn_list, gnc_list) {
1202 dev = conn->gnc_device;
1204 kgnilnd_release_mbox(conn, -1);
1205 conn->gnc_in_purgatory = 0;
1207 list_del_init(&conn->gnc_list);
1209 /* gnc_needs_detach is set in kgnilnd_del_conn_or_peer. It is used to keep track
1210 * of conns that have been marked for detach by kgnilnd_del_conn_or_peer.
1211 * The function uses kgn_npending_detach to verify the conn has
1212 * actually been detached.
1215 if (conn->gnc_needs_detach)
1216 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_detach);
1218 /* if this guy is really dead (we are doing release from reaper),
1219 * make sure we tell LNet - if this is from other context,
1220 * the checks in the function will prevent an errant
1222 kgnilnd_peer_notify(conn->gnc_peer, conn->gnc_error, 0);
1224 list_for_each_entry_safe(gmp, gmpN, &conn->gnc_mdd_list,
1227 "dev %p releasing held mdd %#llx.%#llx\n",
1228 conn->gnc_device, gmp->gmp_map_key.qword1,
1229 gmp->gmp_map_key.qword2);
1231 atomic_dec(&dev->gnd_n_mdd_held);
1232 kgnilnd_mem_mdd_release(conn->gnc_device->gnd_handle,
1234 /* ignoring the return code - if kgni/ghal can't find it
1235 * it must be released already */
1237 list_del_init(&gmp->gmp_list);
1238 LIBCFS_FREE(gmp, sizeof(*gmp));
1240 /* lose conn ref for purgatory */
1241 kgnilnd_conn_decref(conn);
1245 /* needs write_lock on kgnilnd_data.kgn_peer_conn_lock held */
1247 kgnilnd_peer_increase_reconnect_locked(kgn_peer_t *peer)
1251 current_to = peer->gnp_reconnect_interval;
1253 /* we'll try to reconnect fast the first time, then back-off */
1254 if (current_to == 0) {
1255 peer->gnp_reconnect_time = jiffies - 1;
1256 current_to = *kgnilnd_tunables.kgn_min_reconnect_interval;
1258 peer->gnp_reconnect_time = jiffies + cfs_time_seconds(current_to);
1259 /* add 50% of min timeout & retry */
1260 current_to += *kgnilnd_tunables.kgn_min_reconnect_interval / 2;
1263 current_to = min(current_to,
1264 *kgnilnd_tunables.kgn_max_reconnect_interval);
1266 peer->gnp_reconnect_interval = current_to;
1267 CDEBUG(D_NET, "peer %s can reconnect at %lu interval %lu\n",
1268 libcfs_nid2str(peer->gnp_nid), peer->gnp_reconnect_time,
1269 peer->gnp_reconnect_interval);
1272 /* needs kgnilnd_data.kgn_peer_conn_lock held */
1274 kgnilnd_find_peer_locked(lnet_nid_t nid)
1276 struct list_head *peer_list = kgnilnd_nid2peerlist(nid);
1279 /* Chopping nid down to only NIDADDR using LNET_NIDADDR so we only
1280 * have a single peer per device instead of a peer per nid/net combo.
1283 list_for_each_entry(peer, peer_list, gnp_list) {
1284 if (LNET_NIDADDR(nid) != LNET_NIDADDR(peer->gnp_nid))
1287 CDEBUG(D_NET, "got peer [%p] -> %s c %d (%d)\n",
1288 peer, libcfs_nid2str(nid),
1289 peer->gnp_connecting,
1290 atomic_read(&peer->gnp_refcount));
1296 /* need write_lock on kgn_peer_conn_lock */
1298 kgnilnd_unlink_peer_locked(kgn_peer_t *peer)
1300 LASSERTF(list_empty(&peer->gnp_conns),
1302 peer, libcfs_nid2str(peer->gnp_nid));
1303 LASSERTF(list_empty(&peer->gnp_tx_queue),
1305 peer, libcfs_nid2str(peer->gnp_nid));
1306 LASSERTF(kgnilnd_peer_active(peer),
1308 peer, libcfs_nid2str(peer->gnp_nid));
1309 CDEBUG(D_NET, "unlinking peer 0x%p->%s\n",
1310 peer, libcfs_nid2str(peer->gnp_nid));
1312 list_del_init(&peer->gnp_list);
1313 kgnilnd_data.kgn_peer_version++;
1314 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_unlink);
1315 /* lose peerlist's ref */
1316 kgnilnd_peer_decref(peer);
1320 kgnilnd_get_peer_info(int index,
1321 kgn_peer_t **found_peer,
1322 lnet_nid_t *id, __u32 *nic_addr,
1323 int *refcount, int *connecting)
1325 struct list_head *ptmp;
1330 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1332 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
1334 list_for_each(ptmp, &kgnilnd_data.kgn_peers[i]) {
1335 peer = list_entry(ptmp, kgn_peer_t, gnp_list);
1340 CDEBUG(D_NET, "found peer %p (%s) at index %d\n",
1341 peer, libcfs_nid2str(peer->gnp_nid), index);
1344 *id = peer->gnp_nid;
1345 *nic_addr = peer->gnp_host_id;
1346 *refcount = atomic_read(&peer->gnp_refcount);
1347 *connecting = peer->gnp_connecting;
1354 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1356 CDEBUG(D_NET, "no gni peer at index %d\n", index);
1360 /* requires write_lock on kgn_peer_conn_lock held */
1362 kgnilnd_add_peer_locked(lnet_nid_t nid, kgn_peer_t *new_stub_peer, kgn_peer_t **peerp)
1364 kgn_peer_t *peer, *peer2;
1366 LASSERTF(new_stub_peer != NULL, "bad stub peer for nid %s\n",
1367 libcfs_nid2str(nid));
1369 peer2 = kgnilnd_find_peer_locked(nid);
1370 if (peer2 != NULL) {
1371 /* A peer was created during the lock transition, so drop
1372 * the new one we created */
1373 kgnilnd_peer_decref(new_stub_peer);
1376 peer = new_stub_peer;
1377 /* peer table takes existing ref on peer */
1379 LASSERTF(!kgnilnd_peer_active(peer),
1380 "peer 0x%p->%s already in peer table\n",
1381 peer, libcfs_nid2str(peer->gnp_nid));
1382 list_add_tail(&peer->gnp_list,
1383 kgnilnd_nid2peerlist(nid));
1384 kgnilnd_data.kgn_peer_version++;
1387 LASSERTF(peer->gnp_net != NULL, "peer 0x%p->%s with NULL net\n",
1388 peer, libcfs_nid2str(peer->gnp_nid));
1393 kgnilnd_add_peer(kgn_net_t *net, lnet_nid_t nid, kgn_peer_t **peerp)
1400 if (nid == LNET_NID_ANY)
1403 node_state = kgnilnd_get_node_state(LNET_NIDADDR(nid));
1405 /* NB - this will not block during normal operations -
1406 * the only writer of this is in the startup/shutdown path. */
1407 rc = down_read_trylock(&kgnilnd_data.kgn_net_rw_sem);
1412 rc = kgnilnd_create_peer_safe(&peer, nid, net, node_state);
1414 up_read(&kgnilnd_data.kgn_net_rw_sem);
1418 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1419 up_read(&kgnilnd_data.kgn_net_rw_sem);
1421 kgnilnd_add_peer_locked(nid, peer, peerp);
1423 CDEBUG(D_NET, "peer 0x%p->%s connecting %d\n",
1424 peerp, libcfs_nid2str((*peerp)->gnp_nid),
1425 (*peerp)->gnp_connecting);
1427 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1431 /* needs write_lock on kgn_peer_conn_lock */
1433 kgnilnd_cancel_peer_connect_locked(kgn_peer_t *peer, struct list_head *zombies)
1437 /* we do care about state of gnp_connecting - we could be between
1438 * reconnect attempts, so try to find the dgram and cancel the TX
1439 * anyways. If we are in the process of posting DONT do anything;
1440 * once it fails or succeeds we can nuke the connect attempt.
1441 * We have no idea where in kgnilnd_post_dgram we are so we cant
1442 * attempt to cancel until the function is done.
1445 /* make sure peer isn't in process of connecting or waiting for connect*/
1446 spin_lock(&peer->gnp_net->gnn_dev->gnd_connd_lock);
1447 if (!(list_empty(&peer->gnp_connd_list))) {
1448 list_del_init(&peer->gnp_connd_list);
1449 /* remove connd ref */
1450 kgnilnd_peer_decref(peer);
1452 spin_unlock(&peer->gnp_net->gnn_dev->gnd_connd_lock);
1454 if (peer->gnp_connecting == GNILND_PEER_POSTING || peer->gnp_connecting == GNILND_PEER_NEEDS_DEATH) {
1455 peer->gnp_connecting = GNILND_PEER_NEEDS_DEATH;
1456 /* We are in process of posting right now the xchg set it up for us to
1457 * cancel the connect so we are finished for now */
1459 /* no need for exchange we have the peer lock and its ready for us to nuke */
1460 LASSERTF(peer->gnp_connecting != GNILND_PEER_POSTING,
1461 "Peer in invalid state 0x%p->%s, connecting %d\n",
1462 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting);
1463 peer->gnp_connecting = GNILND_PEER_IDLE;
1464 set_mb(peer->gnp_last_dgram_errno, -ETIMEDOUT);
1465 kgnilnd_find_and_cancel_dgram(peer->gnp_net->gnn_dev,
1469 /* The least we can do is nuke the tx's no matter what.... */
1470 list_for_each_entry_safe(tx, txn, &peer->gnp_tx_queue, tx_list) {
1471 kgnilnd_tx_del_state_locked(tx, peer, NULL,
1473 list_add_tail(&tx->tx_list, zombies);
1477 /* needs write_lock on kgn_peer_conn_lock */
1479 kgnilnd_del_peer_locked(kgn_peer_t *peer, int error)
1481 /* this peer could be passive and only held for purgatory,
1482 * take a ref to ensure it doesn't disappear in this function */
1483 kgnilnd_peer_addref(peer);
1485 CFS_RACE(CFS_FAIL_GNI_FIND_TARGET);
1487 /* if purgatory release cleared it out, don't try again */
1488 if (kgnilnd_peer_active(peer)) {
1489 /* always do this to allow kgnilnd_start_connect and
1490 * kgnilnd_finish_connect to catch this before they
1491 * wrap up their operations */
1492 if (kgnilnd_can_unlink_peer_locked(peer)) {
1493 /* already released purgatory, so only active
1495 kgnilnd_unlink_peer_locked(peer);
1497 kgnilnd_close_peer_conns_locked(peer, error);
1498 /* peer unlinks itself when last conn is closed */
1502 /* we are done, release back to the wild */
1503 kgnilnd_peer_decref(peer);
1507 kgnilnd_del_conn_or_peer(kgn_net_t *net, lnet_nid_t nid, int command,
1511 LIST_HEAD (zombies);
1512 struct list_head *ptmp, *pnxt;
1519 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1521 if (nid != LNET_NID_ANY)
1522 lo = hi = kgnilnd_nid2peerlist(nid) - kgnilnd_data.kgn_peers;
1525 hi = *kgnilnd_tunables.kgn_peer_hash_size - 1;
1526 /* wildcards always succeed */
1530 for (i = lo; i <= hi; i++) {
1531 list_for_each_safe(ptmp, pnxt, &kgnilnd_data.kgn_peers[i]) {
1532 peer = list_entry(ptmp, kgn_peer_t, gnp_list);
1534 LASSERTF(peer->gnp_net != NULL,
1535 "peer %p (%s) with NULL net\n",
1536 peer, libcfs_nid2str(peer->gnp_nid));
1538 if (net != NULL && peer->gnp_net != net)
1541 if (!(nid == LNET_NID_ANY || LNET_NIDADDR(peer->gnp_nid) == LNET_NIDADDR(nid)))
1544 /* In both cases, we want to stop any in-flight
1545 * connect attempts */
1546 kgnilnd_cancel_peer_connect_locked(peer, &zombies);
1549 case GNILND_DEL_CONN:
1550 kgnilnd_close_peer_conns_locked(peer, error);
1552 case GNILND_DEL_PEER:
1553 peer->gnp_pending_unlink = 1;
1554 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_unlink);
1555 kgnilnd_mark_for_detach_purgatory_all_locked(peer);
1556 kgnilnd_del_peer_locked(peer, error);
1558 case GNILND_CLEAR_PURGATORY:
1559 /* Mark everything ready for detach reaper will cleanup
1560 * once we release the kgn_peer_conn_lock
1562 kgnilnd_mark_for_detach_purgatory_all_locked(peer);
1563 peer->gnp_last_errno = -EISCONN;
1564 /* clear reconnect so he can reconnect soon */
1565 peer->gnp_reconnect_time = 0;
1566 peer->gnp_reconnect_interval = 0;
1569 CERROR("bad command %d\n", command);
1572 /* we matched something */
1577 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1580 kgnilnd_txlist_done(&zombies, error);
1582 /* This function does not return until the commands it initiated have completed,
1583 * since they have to work there way through the other threads. In the case of shutdown
1584 * threads are not woken up until after this call is initiated so we cannot wait, we just
1585 * need to return. The same applies for stack reset we shouldnt wait as the reset thread
1589 CFS_RACE(CFS_FAIL_GNI_RACE_RESET);
1591 if (error == -ENOTRECOVERABLE || error == -ESHUTDOWN) {
1595 wait_var_event_warning(&kgnilnd_data,
1596 !atomic_read(&kgnilnd_data.kgn_npending_conns) &&
1597 !atomic_read(&kgnilnd_data.kgn_npending_detach) &&
1598 !atomic_read(&kgnilnd_data.kgn_npending_unlink),
1599 "Waiting on %d peers %d closes %d detaches\n",
1600 atomic_read(&kgnilnd_data.kgn_npending_unlink),
1601 atomic_read(&kgnilnd_data.kgn_npending_conns),
1602 atomic_read(&kgnilnd_data.kgn_npending_detach));
1608 kgnilnd_get_conn_by_idx(int index)
1611 struct list_head *ptmp;
1613 struct list_head *ctmp;
1617 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
1618 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1619 list_for_each(ptmp, &kgnilnd_data.kgn_peers[i]) {
1621 peer = list_entry(ptmp, kgn_peer_t, gnp_list);
1623 list_for_each(ctmp, &peer->gnp_conns) {
1624 conn = list_entry(ctmp, kgn_conn_t, gnc_list);
1626 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
1632 CDEBUG(D_NET, "++conn[%p] -> %s (%d)\n", conn,
1633 libcfs_nid2str(conn->gnc_peer->gnp_nid),
1634 atomic_read(&conn->gnc_refcount));
1635 kgnilnd_conn_addref(conn);
1636 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1640 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1647 kgnilnd_get_conn_info(kgn_peer_t *peer,
1648 int *device_id, __u64 *peerstamp,
1649 int *tx_seq, int *rx_seq,
1650 int *fmaq_len, int *nfma, int *nrdma)
1655 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1657 conn = kgnilnd_find_conn_locked(peer);
1663 *device_id = conn->gnc_device->gnd_host_id;
1664 *peerstamp = conn->gnc_peerstamp;
1665 *tx_seq = atomic_read(&conn->gnc_tx_seq);
1666 *rx_seq = atomic_read(&conn->gnc_rx_seq);
1667 *fmaq_len = kgnilnd_count_list(&conn->gnc_fmaq);
1668 *nfma = atomic_read(&conn->gnc_nlive_fma);
1669 *nrdma = atomic_read(&conn->gnc_nlive_rdma);
1671 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1675 /* needs write_lock on kgn_peer_conn_lock */
1677 kgnilnd_close_peer_conns_locked(kgn_peer_t *peer, int why)
1680 struct list_head *ctmp, *cnxt;
1683 list_for_each_safe(ctmp, cnxt, &peer->gnp_conns) {
1684 conn = list_entry(ctmp, kgn_conn_t, gnc_list);
1686 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
1690 /* we mark gnc_needs closing and increment kgn_npending_conns so that
1691 * kgnilnd_del_conn_or_peer can wait on the other threads closing
1692 * and cleaning up the connection.
1694 if (!conn->gnc_needs_closing) {
1695 conn->gnc_needs_closing = 1;
1696 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_conns);
1698 kgnilnd_close_conn_locked(conn, why);
1704 kgnilnd_report_node_state(lnet_nid_t nid, int down)
1707 kgn_peer_t *peer, *new_peer;
1710 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1711 peer = kgnilnd_find_peer_locked(nid);
1718 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1720 /* Don't add a peer for node up events */
1721 if (down == GNILND_PEER_UP)
1724 /* find any valid net - we don't care which one... */
1725 down_read(&kgnilnd_data.kgn_net_rw_sem);
1726 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
1727 list_for_each_entry(net, &kgnilnd_data.kgn_nets[i],
1737 up_read(&kgnilnd_data.kgn_net_rw_sem);
1740 CNETERR("Could not find a net for nid %lld\n", nid);
1744 /* The nid passed in does not yet contain the net portion.
1745 * Let's build it up now
1747 nid = LNET_MKNID(LNET_NIDNET(net->gnn_ni->ni_nid), nid);
1748 rc = kgnilnd_add_peer(net, nid, &new_peer);
1751 CNETERR("Could not add peer for nid %lld, rc %d\n",
1756 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1757 peer = kgnilnd_find_peer_locked(nid);
1760 CNETERR("Could not find peer for nid %lld\n", nid);
1761 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1766 peer->gnp_state = down;
1768 if (down == GNILND_PEER_DOWN) {
1771 peer->gnp_down_event_time = jiffies;
1772 kgnilnd_cancel_peer_connect_locked(peer, &zombies);
1773 conn = kgnilnd_find_conn_locked(peer);
1776 kgnilnd_close_conn_locked(conn, -ENETRESET);
1779 peer->gnp_up_event_time = jiffies;
1782 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1784 if (down == GNILND_PEER_DOWN) {
1785 /* using ENETRESET so we don't get messages from
1788 kgnilnd_txlist_done(&zombies, -ENETRESET);
1789 kgnilnd_peer_notify(peer, -ECONNRESET, 0);
1790 LCONSOLE_INFO("Received down event for nid %d\n",
1798 kgnilnd_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
1800 struct libcfs_ioctl_data *data = arg;
1801 kgn_net_t *net = ni->ni_data;
1804 LASSERT(ni == net->gnn_ni);
1807 case IOC_LIBCFS_GET_PEER: {
1809 kgn_peer_t *peer = NULL;
1811 __u64 peerstamp = 0;
1812 int peer_refcount = 0, peer_connecting = 0;
1814 int tx_seq = 0, rx_seq = 0;
1815 int fmaq_len = 0, nfma = 0, nrdma = 0;
1817 rc = kgnilnd_get_peer_info(data->ioc_count, &peer,
1818 &nid, &nic_addr, &peer_refcount,
1824 /* LNET_MKNID is used to mask from lnet the multiplexing/demultiplexing of connections and peers
1825 * LNET assumes a conn and peer per net, the LNET_MKNID/LNET_NIDADDR allows us to let Lnet see what it
1826 * wants to see instead of the underlying network that is being used to send the data
1828 data->ioc_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), LNET_NIDADDR(nid));
1829 data->ioc_flags = peer_connecting;
1830 data->ioc_count = peer_refcount;
1832 rc = kgnilnd_get_conn_info(peer, &device_id, &peerstamp,
1833 &tx_seq, &rx_seq, &fmaq_len,
1836 /* This is allowable - a persistent peer could not
1837 * have a connection */
1839 /* flag to indicate we are not connected -
1840 * need to print as such */
1841 data->ioc_flags |= (1<<16);
1845 data->ioc_net = device_id;
1846 data->ioc_u64[0] = peerstamp;
1847 data->ioc_u32[0] = fmaq_len;
1848 data->ioc_u32[1] = nfma;
1849 data->ioc_u32[2] = tx_seq;
1850 data->ioc_u32[3] = rx_seq;
1851 data->ioc_u32[4] = nrdma;
1855 case IOC_LIBCFS_ADD_PEER: {
1856 /* just dummy value to allow using common interface */
1858 rc = kgnilnd_add_peer(net, data->ioc_nid, &peer);
1861 case IOC_LIBCFS_DEL_PEER: {
1862 /* NULL is passed in so it affects all peers in existence without regard to network
1863 * as the peer may not exist on the network LNET believes it to be on.
1865 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1866 GNILND_DEL_PEER, -EUCLEAN);
1869 case IOC_LIBCFS_GET_CONN: {
1870 kgn_conn_t *conn = kgnilnd_get_conn_by_idx(data->ioc_count);
1876 /* LNET_MKNID is used to build the correct address based on what LNET wants to see instead of
1877 * the generic connection that is used to send the data
1879 data->ioc_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), LNET_NIDADDR(conn->gnc_peer->gnp_nid));
1880 data->ioc_u32[0] = conn->gnc_device->gnd_id;
1881 kgnilnd_conn_decref(conn);
1885 case IOC_LIBCFS_CLOSE_CONNECTION: {
1886 /* use error = -ENETRESET to indicate it was lctl disconnect */
1887 /* NULL is passed in so it affects all the nets as the connection is virtual
1888 * and may not exist on the network LNET believes it to be on.
1890 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1891 GNILND_DEL_CONN, -ENETRESET);
1894 case IOC_LIBCFS_PUSH_CONNECTION: {
1895 /* we use this to flush purgatory */
1896 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1897 GNILND_CLEAR_PURGATORY, -EUCLEAN);
1900 case IOC_LIBCFS_REGISTER_MYNID: {
1901 /* Ignore if this is a noop */
1902 if (data->ioc_nid == ni->ni_nid) {
1905 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
1906 libcfs_nid2str(data->ioc_nid),
1907 libcfs_nid2str(ni->ni_nid));
1918 kgnilnd_dev_init(kgn_device_t *dev)
1922 unsigned int cq_size;
1925 /* size of these CQs should be able to accommodate the outgoing
1926 * RDMA and SMSG transactions. Since we really don't know what we
1927 * really need here, we'll take credits * 2 * 3 to allow a bunch.
1928 * We need to dig into this more with the performance work. */
1929 cq_size = *kgnilnd_tunables.kgn_credits * 2 * 3;
1931 rrc = kgnilnd_cdm_create(dev->gnd_id, *kgnilnd_tunables.kgn_ptag,
1932 *kgnilnd_tunables.kgn_pkey, 0,
1934 if (rrc != GNI_RC_SUCCESS) {
1935 CERROR("Can't create CDM %d (%d)\n", dev->gnd_id, rrc);
1936 GOTO(failed, rc = -ENODEV);
1939 rrc = kgnilnd_cdm_attach(dev->gnd_domain, dev->gnd_id,
1940 &dev->gnd_host_id, &dev->gnd_handle);
1941 if (rrc != GNI_RC_SUCCESS) {
1942 CERROR("Can't attach CDM to device %d (%d)\n",
1944 GOTO(failed, rc = -ENODEV);
1947 /* a bit gross, but not much we can do - Aries Sim doesn't have
1948 * hardcoded NIC/NID that we can use */
1949 rc = kgnilnd_setup_nic_translation(dev->gnd_host_id);
1951 GOTO(failed, rc = -ENODEV);
1953 /* only dev 0 gets the errors - no need to reset the stack twice
1954 * - this works because we have a single PTAG, if we had more
1955 * then we'd need to have multiple handlers */
1956 if (dev->gnd_id == 0) {
1957 rrc = kgnilnd_subscribe_errors(dev->gnd_handle,
1958 GNI_ERRMASK_CRITICAL |
1959 GNI_ERRMASK_UNKNOWN_TRANSACTION,
1960 0, NULL, kgnilnd_critical_error,
1961 &dev->gnd_err_handle);
1962 if (rrc != GNI_RC_SUCCESS) {
1963 CERROR("Can't subscribe for errors on device %d: rc %d\n",
1965 GOTO(failed, rc = -ENODEV);
1968 rc = kgnilnd_set_quiesce_callback(dev->gnd_handle,
1969 kgnilnd_quiesce_end_callback);
1970 if (rc != GNI_RC_SUCCESS) {
1971 CERROR("Can't subscribe for quiesce callback on device %d: rc %d\n",
1973 GOTO(failed, rc = -ENODEV);
1977 rc = kgnilnd_nicaddr_to_nid(dev->gnd_host_id, &dev->gnd_nid);
1979 /* log messages during startup */
1980 if (kgnilnd_data.kgn_init < GNILND_INIT_ALL) {
1981 CERROR("couldn't translate host_id 0x%x to nid. rc %d\n",
1982 dev->gnd_host_id, rc);
1984 GOTO(failed, rc = -ESRCH);
1986 CDEBUG(D_NET, "NIC %x -> NID %d\n", dev->gnd_host_id, dev->gnd_nid);
1988 rrc = kgnilnd_cq_create(dev->gnd_handle, *kgnilnd_tunables.kgn_credits,
1989 0, kgnilnd_device_callback,
1990 dev->gnd_id, &dev->gnd_snd_rdma_cqh);
1991 if (rrc != GNI_RC_SUCCESS) {
1992 CERROR("Can't create rdma send cq size %u for device "
1993 "%d (%d)\n", cq_size, dev->gnd_id, rrc);
1994 GOTO(failed, rc = -EINVAL);
1997 rrc = kgnilnd_cq_create(dev->gnd_handle, cq_size,
1998 0, kgnilnd_device_callback, dev->gnd_id,
1999 &dev->gnd_snd_fma_cqh);
2000 if (rrc != GNI_RC_SUCCESS) {
2001 CERROR("Can't create fma send cq size %u for device %d (%d)\n",
2002 cq_size, dev->gnd_id, rrc);
2003 GOTO(failed, rc = -EINVAL);
2006 /* This one we size differently - overflows are possible and it needs to be
2007 * sized based on machine size */
2008 rrc = kgnilnd_cq_create(dev->gnd_handle,
2009 *kgnilnd_tunables.kgn_fma_cq_size,
2010 0, kgnilnd_device_callback, dev->gnd_id,
2011 &dev->gnd_rcv_fma_cqh);
2012 if (rrc != GNI_RC_SUCCESS) {
2013 CERROR("Can't create fma cq size %d for device %d (%d)\n",
2014 *kgnilnd_tunables.kgn_fma_cq_size, dev->gnd_id, rrc);
2015 GOTO(failed, rc = -EINVAL);
2018 rrc = kgnilnd_register_smdd_buf(dev);
2019 if (rrc != GNI_RC_SUCCESS) {
2020 GOTO(failed, rc = -EINVAL);
2026 kgnilnd_dev_fini(dev);
2031 kgnilnd_dev_fini(kgn_device_t *dev)
2036 /* At quiesce or rest time, need to loop through and clear gnd_ready_conns ?*/
2037 LASSERTF(list_empty(&dev->gnd_ready_conns) &&
2038 list_empty(&dev->gnd_map_tx) &&
2039 list_empty(&dev->gnd_rdmaq) &&
2040 list_empty(&dev->gnd_delay_conns),
2041 "dev 0x%p ready_conns %d@0x%p delay_conns %d@0x%p"
2042 "map_tx %d@0x%p rdmaq %d@0x%p\n",
2043 dev, kgnilnd_count_list(&dev->gnd_ready_conns), &dev->gnd_ready_conns,
2044 kgnilnd_count_list(&dev->gnd_delay_conns), &dev->gnd_delay_conns,
2045 kgnilnd_count_list(&dev->gnd_map_tx), &dev->gnd_map_tx,
2046 kgnilnd_count_list(&dev->gnd_rdmaq), &dev->gnd_rdmaq);
2048 /* These should follow from tearing down all connections */
2049 LASSERTF(dev->gnd_map_nphys == 0 && dev->gnd_map_physnop == 0,
2050 "%d physical mappings of %d pages still mapped\n",
2051 dev->gnd_map_nphys, dev->gnd_map_physnop);
2053 LASSERTF(atomic_read(&dev->gnd_n_mdd) == 0 &&
2054 atomic_read(&dev->gnd_n_mdd_held) == 0 &&
2055 atomic64_read(&dev->gnd_nbytes_map) == 0,
2056 "%d SMSG mappings of %lld bytes still mapped or held %d\n",
2057 atomic_read(&dev->gnd_n_mdd),
2058 (u64)atomic64_read(&dev->gnd_nbytes_map),
2059 atomic_read(&dev->gnd_n_mdd_held));
2061 LASSERT(list_empty(&dev->gnd_map_list));
2063 /* What other assertions needed to ensure all connections torn down ? */
2065 /* check all counters == 0 (EP, MDD, etc) */
2067 /* if we are resetting due to quiese (stack reset), don't check
2069 LASSERTF(kgnilnd_data.kgn_quiesce_trigger ||
2070 atomic_read(&kgnilnd_data.kgn_nthreads) == 0,
2071 "tried to shutdown with threads active\n");
2073 if (dev->gnd_smdd_hold_buf) {
2074 rrc = kgnilnd_deregister_smdd_buf(dev);
2075 LASSERTF(rrc == GNI_RC_SUCCESS,
2076 "bad rc from deregistion of sMDD buffer: %d\n", rrc);
2077 dev->gnd_smdd_hold_buf = NULL;
2080 if (dev->gnd_rcv_fma_cqh) {
2081 rrc = kgnilnd_cq_destroy(dev->gnd_rcv_fma_cqh);
2082 LASSERTF(rrc == GNI_RC_SUCCESS,
2083 "bad rc from gni_cq_destroy on rcv_fma_cqh: %d\n", rrc);
2084 dev->gnd_rcv_fma_cqh = NULL;
2087 if (dev->gnd_snd_rdma_cqh) {
2088 rrc = kgnilnd_cq_destroy(dev->gnd_snd_rdma_cqh);
2089 LASSERTF(rrc == GNI_RC_SUCCESS,
2090 "bad rc from gni_cq_destroy on send_rdma_cqh: %d\n", rrc);
2091 dev->gnd_snd_rdma_cqh = NULL;
2094 if (dev->gnd_snd_fma_cqh) {
2095 rrc = kgnilnd_cq_destroy(dev->gnd_snd_fma_cqh);
2096 LASSERTF(rrc == GNI_RC_SUCCESS,
2097 "bad rc from gni_cq_destroy on snd_fma_cqh: %d\n", rrc);
2098 dev->gnd_snd_fma_cqh = NULL;
2101 if (dev->gnd_err_handle) {
2102 rrc = kgnilnd_release_errors(dev->gnd_err_handle);
2103 LASSERTF(rrc == GNI_RC_SUCCESS,
2104 "bad rc from gni_release_errors: %d\n", rrc);
2105 dev->gnd_err_handle = NULL;
2108 if (dev->gnd_domain) {
2109 rrc = kgnilnd_cdm_destroy(dev->gnd_domain);
2110 LASSERTF(rrc == GNI_RC_SUCCESS,
2111 "bad rc from gni_cdm_destroy: %d\n", rrc);
2112 dev->gnd_domain = NULL;
2118 int kgnilnd_base_startup(void)
2120 struct timespec64 ts;
2121 long long pkmem = libcfs_kmem_read();
2125 struct task_struct *thrd;
2127 #if defined(CONFIG_CRAY_XT) && !defined(CONFIG_CRAY_COMPUTE)
2128 /* limit how much memory can be allocated for fma blocks in
2129 * instances where many nodes need to reconnects at the same time */
2132 kgnilnd_data.free_pages_limit = si.totalram/4;
2137 LASSERTF(kgnilnd_data.kgn_init == GNILND_INIT_NOTHING,
2138 "init %d\n", kgnilnd_data.kgn_init);
2140 /* zero pointers, flags etc */
2141 memset(&kgnilnd_data, 0, sizeof(kgnilnd_data));
2142 kgnilnd_check_kgni_version();
2144 /* CAVEAT EMPTOR: Every 'Fma' message includes the sender's NID and
2145 * a unique (for all time) connstamp so we can uniquely identify
2146 * the sender. The connstamp is an incrementing counter
2147 * initialised with seconds + microseconds at startup time. So we
2148 * rely on NOT creating connections more frequently on average than
2149 * 1MHz to ensure we don't use old connstamps when we reboot. */
2150 ktime_get_ts64(&ts);
2151 kgnilnd_data.kgn_connstamp =
2152 kgnilnd_data.kgn_peerstamp =
2153 (ts.tv_sec * 1000000) + (ts.tv_nsec / 100);
2155 init_rwsem(&kgnilnd_data.kgn_net_rw_sem);
2157 for (i = 0; i < GNILND_MAXDEVS; i++) {
2158 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2161 INIT_LIST_HEAD(&dev->gnd_ready_conns);
2162 INIT_LIST_HEAD(&dev->gnd_delay_conns);
2163 INIT_LIST_HEAD(&dev->gnd_map_tx);
2164 INIT_LIST_HEAD(&dev->gnd_fma_buffs);
2165 mutex_init(&dev->gnd_cq_mutex);
2166 mutex_init(&dev->gnd_fmablk_mutex);
2167 spin_lock_init(&dev->gnd_fmablk_lock);
2168 init_waitqueue_head(&dev->gnd_waitq);
2169 init_waitqueue_head(&dev->gnd_dgram_waitq);
2170 init_waitqueue_head(&dev->gnd_dgping_waitq);
2171 spin_lock_init(&dev->gnd_lock);
2172 INIT_LIST_HEAD(&dev->gnd_map_list);
2173 spin_lock_init(&dev->gnd_map_lock);
2174 atomic_set(&dev->gnd_nfmablk, 0);
2175 atomic_set(&dev->gnd_fmablk_vers, 1);
2176 atomic_set(&dev->gnd_neps, 0);
2177 atomic_set(&dev->gnd_canceled_dgrams, 0);
2178 INIT_LIST_HEAD(&dev->gnd_connd_peers);
2179 spin_lock_init(&dev->gnd_connd_lock);
2180 spin_lock_init(&dev->gnd_dgram_lock);
2181 spin_lock_init(&dev->gnd_rdmaq_lock);
2182 INIT_LIST_HEAD(&dev->gnd_rdmaq);
2183 init_rwsem(&dev->gnd_conn_sem);
2185 /* alloc & setup nid based dgram table */
2186 CFS_ALLOC_PTR_ARRAY(dev->gnd_dgrams,
2187 *kgnilnd_tunables.kgn_peer_hash_size);
2189 if (dev->gnd_dgrams == NULL)
2190 GOTO(failed, rc = -ENOMEM);
2192 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2193 INIT_LIST_HEAD(&dev->gnd_dgrams[i]);
2195 atomic_set(&dev->gnd_ndgrams, 0);
2196 atomic_set(&dev->gnd_nwcdgrams, 0);
2197 /* setup timer for RDMAQ processing */
2198 cfs_timer_setup(&dev->gnd_rdmaq_timer,
2199 kgnilnd_schedule_device_timer,
2200 (unsigned long)dev, 0);
2202 /* setup timer for mapping processing */
2203 cfs_timer_setup(&dev->gnd_map_timer,
2204 kgnilnd_schedule_device_timer,
2205 (unsigned long)dev, 0);
2209 /* CQID 0 isn't allowed, set to MAX_MSG_ID - 1 to check for conflicts early */
2210 kgnilnd_data.kgn_next_cqid = GNILND_MAX_MSG_ID - 1;
2211 kgnilnd_data.kgn_new_min_timeout = *kgnilnd_tunables.kgn_timeout;
2212 init_waitqueue_head(&kgnilnd_data.kgn_reaper_waitq);
2213 init_waitqueue_head(&kgnilnd_data.kgn_ruhroh_waitq);
2214 spin_lock_init(&kgnilnd_data.kgn_reaper_lock);
2216 mutex_init(&kgnilnd_data.kgn_quiesce_mutex);
2217 atomic_set(&kgnilnd_data.kgn_nquiesce, 0);
2218 atomic_set(&kgnilnd_data.kgn_npending_conns, 0);
2219 atomic_set(&kgnilnd_data.kgn_npending_unlink, 0);
2220 atomic_set(&kgnilnd_data.kgn_npending_detach, 0);
2221 atomic_set(&kgnilnd_data.kgn_rev_offset, 0);
2222 atomic_set(&kgnilnd_data.kgn_rev_length, 0);
2223 atomic_set(&kgnilnd_data.kgn_rev_copy_buff, 0);
2225 /* OK to call kgnilnd_api_shutdown() to cleanup now */
2226 kgnilnd_data.kgn_init = GNILND_INIT_DATA;
2227 if (!try_module_get(THIS_MODULE))
2228 GOTO(failed, rc = -ENOENT);
2230 rwlock_init(&kgnilnd_data.kgn_peer_conn_lock);
2232 CFS_ALLOC_PTR_ARRAY(kgnilnd_data.kgn_peers,
2233 *kgnilnd_tunables.kgn_peer_hash_size);
2235 if (kgnilnd_data.kgn_peers == NULL)
2236 GOTO(failed, rc = -ENOMEM);
2238 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2239 INIT_LIST_HEAD(&kgnilnd_data.kgn_peers[i]);
2242 CFS_ALLOC_PTR_ARRAY(kgnilnd_data.kgn_conns,
2243 *kgnilnd_tunables.kgn_peer_hash_size);
2245 if (kgnilnd_data.kgn_conns == NULL)
2246 GOTO(failed, rc = -ENOMEM);
2248 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2249 INIT_LIST_HEAD(&kgnilnd_data.kgn_conns[i]);
2252 CFS_ALLOC_PTR_ARRAY(kgnilnd_data.kgn_nets,
2253 *kgnilnd_tunables.kgn_net_hash_size);
2255 if (kgnilnd_data.kgn_nets == NULL)
2256 GOTO(failed, rc = -ENOMEM);
2258 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
2259 INIT_LIST_HEAD(&kgnilnd_data.kgn_nets[i]);
2262 kgnilnd_data.kgn_mbox_cache =
2263 kmem_cache_create("kgn_mbox_block", GNILND_MBOX_SIZE, 0,
2264 SLAB_HWCACHE_ALIGN, NULL);
2265 if (kgnilnd_data.kgn_mbox_cache == NULL) {
2266 CERROR("Can't create slab for physical mbox blocks\n");
2267 GOTO(failed, rc = -ENOMEM);
2270 kgnilnd_data.kgn_rx_cache =
2271 kmem_cache_create("kgn_rx_t", sizeof(kgn_rx_t), 0, 0, NULL);
2272 if (kgnilnd_data.kgn_rx_cache == NULL) {
2273 CERROR("Can't create slab for kgn_rx_t descriptors\n");
2274 GOTO(failed, rc = -ENOMEM);
2277 kgnilnd_data.kgn_tx_cache =
2278 kmem_cache_create("kgn_tx_t", sizeof(kgn_tx_t), 0, 0, NULL);
2279 if (kgnilnd_data.kgn_tx_cache == NULL) {
2280 CERROR("Can't create slab for kgn_tx_t\n");
2281 GOTO(failed, rc = -ENOMEM);
2284 kgnilnd_data.kgn_tx_phys_cache =
2285 kmem_cache_create("kgn_tx_phys",
2286 LNET_MAX_IOV * sizeof(gni_mem_segment_t),
2288 if (kgnilnd_data.kgn_tx_phys_cache == NULL) {
2289 CERROR("Can't create slab for kgn_tx_phys\n");
2290 GOTO(failed, rc = -ENOMEM);
2293 kgnilnd_data.kgn_dgram_cache =
2294 kmem_cache_create("kgn_dgram_t", sizeof(kgn_dgram_t), 0, 0, NULL);
2295 if (kgnilnd_data.kgn_dgram_cache == NULL) {
2296 CERROR("Can't create slab for outgoing datagrams\n");
2297 GOTO(failed, rc = -ENOMEM);
2300 /* allocate a MAX_IOV array of page pointers for each cpu */
2301 kgnilnd_data.kgn_cksum_map_pages = kmalloc(num_possible_cpus() * sizeof (struct page *),
2303 if (kgnilnd_data.kgn_cksum_map_pages == NULL) {
2304 CERROR("Can't allocate vmap cksum pages\n");
2305 GOTO(failed, rc = -ENOMEM);
2307 kgnilnd_data.kgn_cksum_npages = num_possible_cpus();
2308 memset(kgnilnd_data.kgn_cksum_map_pages, 0,
2309 kgnilnd_data.kgn_cksum_npages * sizeof (struct page *));
2311 for (i = 0; i < kgnilnd_data.kgn_cksum_npages; i++) {
2312 kgnilnd_data.kgn_cksum_map_pages[i] = kmalloc(LNET_MAX_IOV * sizeof (struct page *),
2314 if (kgnilnd_data.kgn_cksum_map_pages[i] == NULL) {
2315 CERROR("Can't allocate vmap cksum pages for cpu %d\n", i);
2316 GOTO(failed, rc = -ENOMEM);
2320 LASSERT(kgnilnd_data.kgn_ndevs == 0);
2322 /* Use all available GNI devices */
2323 for (i = 0; i < GNILND_MAXDEVS; i++) {
2324 dev = &kgnilnd_data.kgn_devices[kgnilnd_data.kgn_ndevs];
2326 rc = kgnilnd_dev_init(dev);
2328 /* Increment here so base_shutdown cleans it up */
2329 kgnilnd_data.kgn_ndevs++;
2331 rc = kgnilnd_allocate_phys_fmablk(dev);
2337 if (kgnilnd_data.kgn_ndevs == 0) {
2338 CERROR("Can't initialise any GNI devices\n");
2339 GOTO(failed, rc = -ENODEV);
2342 rc = kgnilnd_thread_start(kgnilnd_reaper, NULL, "kgnilnd_rpr", 0);
2344 CERROR("Can't spawn gnilnd reaper: %d\n", rc);
2348 rc = kgnilnd_start_rca_thread();
2350 CERROR("Can't spawn gnilnd rca: %d\n", rc);
2355 * Start ruhroh thread. We can't use kgnilnd_thread_start() because
2356 * we don't want this thread included in kgnilnd_data.kgn_nthreads
2357 * count. This thread controls quiesce, so it mustn't
2360 thrd = kthread_run(kgnilnd_ruhroh_thread, NULL, "%s_%02d", "kgnilnd_rr", 0);
2363 CERROR("Can't spawn gnilnd ruhroh thread: %d\n", rc);
2367 /* threads will load balance across devs as they are available */
2368 if (*kgnilnd_tunables.kgn_thread_affinity) {
2369 rc = kgnilnd_start_sd_threads();
2373 for (i = 0; i < *kgnilnd_tunables.kgn_sched_threads; i++) {
2374 rc = kgnilnd_thread_start(kgnilnd_scheduler,
2378 CERROR("Can't spawn gnilnd scheduler[%d]: %d\n",
2385 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2386 dev = &kgnilnd_data.kgn_devices[i];
2387 rc = kgnilnd_thread_start(kgnilnd_dgram_mover, dev,
2388 "kgnilnd_dg", dev->gnd_id);
2390 CERROR("Can't spawn gnilnd dgram_mover[%d]: %d\n",
2395 rc = kgnilnd_thread_start(kgnilnd_dgram_waitq, dev,
2396 "kgnilnd_dgn", dev->gnd_id);
2398 CERROR("Can't spawn gnilnd dgram_waitq[%d]: %d\n",
2403 rc = kgnilnd_setup_wildcard_dgram(dev);
2406 CERROR("Can't create wildcard dgrams[%d]: %d\n",
2412 /* flag everything initialised */
2413 kgnilnd_data.kgn_init = GNILND_INIT_ALL;
2414 /*****************************************************/
2416 CDEBUG(D_MALLOC, "initial kmem %lld\n", pkmem);
2420 kgnilnd_base_shutdown();
2421 kgnilnd_data.kgn_init = GNILND_INIT_NOTHING;
2426 kgnilnd_base_shutdown(void)
2431 while (CFS_FAIL_TIMEOUT(CFS_FAIL_GNI_PAUSE_SHUTDOWN, 1)) {};
2433 kgnilnd_data.kgn_wc_kill = 1;
2435 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2436 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2437 kgnilnd_cancel_wc_dgrams(dev);
2438 kgnilnd_cancel_dgrams(dev);
2439 kgnilnd_del_conn_or_peer(NULL, LNET_NID_ANY, GNILND_DEL_PEER, -ESHUTDOWN);
2440 kgnilnd_wait_for_canceled_dgrams(dev);
2443 /* We need to verify there are no conns left before we let the threads
2444 * shut down otherwise we could clean up the peers but still have
2445 * some outstanding conns due to orphaned datagram conns that are
2449 while (atomic_read(&kgnilnd_data.kgn_nconns) != 0) {
2452 for(j = 0; j < kgnilnd_data.kgn_ndevs; ++j) {
2453 kgn_device_t *dev = &kgnilnd_data.kgn_devices[j];
2454 kgnilnd_schedule_device(dev);
2457 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2458 "Waiting for conns to be cleaned up %d\n",atomic_read(&kgnilnd_data.kgn_nconns));
2459 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2461 /* Peer state all cleaned up BEFORE setting shutdown, so threads don't
2462 * have to worry about shutdown races. NB connections may be created
2463 * while there are still active connds, but these will be temporary
2464 * since peer creation always fails after the listener has started to
2466 * all peers should have been cleared out on the nets */
2467 LASSERTF(atomic_read(&kgnilnd_data.kgn_npeers) == 0,
2468 "peers left %d\n", atomic_read(&kgnilnd_data.kgn_npeers));
2470 /* Wait for the ruhroh thread to shut down. */
2471 kgnilnd_data.kgn_ruhroh_shutdown = 1;
2472 wake_up(&kgnilnd_data.kgn_ruhroh_waitq);
2474 while (kgnilnd_data.kgn_ruhroh_running != 0) {
2476 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2477 "Waiting for ruhroh thread to terminate\n");
2478 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2481 /* Flag threads to terminate */
2482 kgnilnd_data.kgn_shutdown = 1;
2484 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2485 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2487 /* should clear all the MDDs */
2488 kgnilnd_unmap_fma_blocks(dev);
2490 kgnilnd_schedule_device(dev);
2491 wake_up_all(&dev->gnd_dgram_waitq);
2492 wake_up_all(&dev->gnd_dgping_waitq);
2493 LASSERT(list_empty(&dev->gnd_connd_peers));
2496 spin_lock(&kgnilnd_data.kgn_reaper_lock);
2497 wake_up_all(&kgnilnd_data.kgn_reaper_waitq);
2498 spin_unlock(&kgnilnd_data.kgn_reaper_lock);
2500 if (atomic_read(&kgnilnd_data.kgn_nthreads))
2501 kgnilnd_wakeup_rca_thread();
2503 /* Wait for threads to exit */
2505 while (atomic_read(&kgnilnd_data.kgn_nthreads) != 0) {
2507 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2508 "Waiting for %d threads to terminate\n",
2509 atomic_read(&kgnilnd_data.kgn_nthreads));
2510 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2513 LASSERTF(atomic_read(&kgnilnd_data.kgn_npeers) == 0,
2514 "peers left %d\n", atomic_read(&kgnilnd_data.kgn_npeers));
2516 if (kgnilnd_data.kgn_peers != NULL) {
2517 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
2518 LASSERT(list_empty(&kgnilnd_data.kgn_peers[i]));
2520 CFS_FREE_PTR_ARRAY(kgnilnd_data.kgn_peers,
2521 *kgnilnd_tunables.kgn_peer_hash_size);
2524 down_write(&kgnilnd_data.kgn_net_rw_sem);
2525 if (kgnilnd_data.kgn_nets != NULL) {
2526 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++)
2527 LASSERT(list_empty(&kgnilnd_data.kgn_nets[i]));
2529 CFS_FREE_PTR_ARRAY(kgnilnd_data.kgn_nets,
2530 *kgnilnd_tunables.kgn_net_hash_size);
2532 up_write(&kgnilnd_data.kgn_net_rw_sem);
2534 LASSERTF(atomic_read(&kgnilnd_data.kgn_nconns) == 0,
2535 "conns left %d\n", atomic_read(&kgnilnd_data.kgn_nconns));
2537 if (kgnilnd_data.kgn_conns != NULL) {
2538 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
2539 LASSERT(list_empty(&kgnilnd_data.kgn_conns[i]));
2541 CFS_FREE_PTR_ARRAY(kgnilnd_data.kgn_conns,
2542 *kgnilnd_tunables.kgn_peer_hash_size);
2545 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2546 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2547 kgnilnd_dev_fini(dev);
2549 LASSERTF(atomic_read(&dev->gnd_ndgrams) == 0,
2550 "dgrams left %d\n", atomic_read(&dev->gnd_ndgrams));
2552 if (dev->gnd_dgrams != NULL) {
2553 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size;
2555 LASSERT(list_empty(&dev->gnd_dgrams[i]));
2557 CFS_FREE_PTR_ARRAY(dev->gnd_dgrams,
2558 *kgnilnd_tunables.kgn_peer_hash_size);
2561 kgnilnd_free_phys_fmablk(dev);
2564 if (kgnilnd_data.kgn_mbox_cache != NULL)
2565 kmem_cache_destroy(kgnilnd_data.kgn_mbox_cache);
2567 if (kgnilnd_data.kgn_rx_cache != NULL)
2568 kmem_cache_destroy(kgnilnd_data.kgn_rx_cache);
2570 if (kgnilnd_data.kgn_tx_cache != NULL)
2571 kmem_cache_destroy(kgnilnd_data.kgn_tx_cache);
2573 if (kgnilnd_data.kgn_tx_phys_cache != NULL)
2574 kmem_cache_destroy(kgnilnd_data.kgn_tx_phys_cache);
2576 if (kgnilnd_data.kgn_dgram_cache != NULL)
2577 kmem_cache_destroy(kgnilnd_data.kgn_dgram_cache);
2579 if (kgnilnd_data.kgn_cksum_map_pages != NULL) {
2580 for (i = 0; i < kgnilnd_data.kgn_cksum_npages; i++) {
2581 if (kgnilnd_data.kgn_cksum_map_pages[i] != NULL) {
2582 kfree(kgnilnd_data.kgn_cksum_map_pages[i]);
2585 kfree(kgnilnd_data.kgn_cksum_map_pages);
2588 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %lld\n",
2589 libcfs_kmem_read());
2591 kgnilnd_data.kgn_init = GNILND_INIT_NOTHING;
2592 module_put(THIS_MODULE);
2598 kgnilnd_startup(struct lnet_ni *ni)
2604 LASSERTF(ni->ni_net->net_lnd == &the_kgnilnd,
2605 "bad LND 0x%p != the_kgnilnd @ 0x%p\n",
2606 ni->ni_net->net_lnd, &the_kgnilnd);
2608 if (kgnilnd_data.kgn_init == GNILND_INIT_NOTHING) {
2609 rc = kgnilnd_base_startup();
2614 /* Serialize with shutdown. */
2615 mutex_lock(&kgnilnd_data.kgn_quiesce_mutex);
2617 LIBCFS_ALLOC(net, sizeof(*net));
2619 CERROR("could not allocate net for new interface instance\n");
2620 /* no need to cleanup the CDM... */
2621 GOTO(failed, rc = -ENOMEM);
2623 INIT_LIST_HEAD(&net->gnn_list);
2626 if (!ni->ni_net->net_tunables_set) {
2627 ni->ni_net->net_tunables.lct_max_tx_credits =
2628 *kgnilnd_tunables.kgn_credits;
2629 ni->ni_net->net_tunables.lct_peer_tx_credits =
2630 *kgnilnd_tunables.kgn_peer_credits;
2633 if (*kgnilnd_tunables.kgn_peer_health) {
2636 /* give this a bit of leeway - we don't have a hard timeout
2637 * as we only check timeouts periodically - see comment in kgnilnd_reaper */
2638 fudge = (GNILND_TO2KA(*kgnilnd_tunables.kgn_timeout) / GNILND_REAPER_NCHECKS);
2639 timeout = *kgnilnd_tunables.kgn_timeout + fudge;
2641 if (*kgnilnd_tunables.kgn_peer_timeout >= timeout) {
2642 ni->ni_net->net_tunables.lct_peer_timeout =
2643 *kgnilnd_tunables.kgn_peer_timeout;
2644 } else if (*kgnilnd_tunables.kgn_peer_timeout > -1) {
2645 LCONSOLE_ERROR("Peer_timeout is set to %d but needs to be >= %d\n",
2646 *kgnilnd_tunables.kgn_peer_timeout,
2649 LIBCFS_FREE(net, sizeof(*net));
2650 GOTO(failed, rc = -EINVAL);
2652 ni->ni_net->net_tunables.lct_peer_timeout = timeout;
2654 LCONSOLE_INFO("Enabling LNet peer health for gnilnd, timeout %ds\n",
2655 ni->ni_net->net_tunables.lct_peer_timeout);
2658 atomic_set(&net->gnn_refcount, 1);
2660 /* if we have multiple devices, spread the nets around */
2661 net->gnn_netnum = LNET_NETNUM(LNET_NIDNET(ni->ni_nid));
2663 devno = LNET_NIDNET(ni->ni_nid) % GNILND_MAXDEVS;
2664 net->gnn_dev = &kgnilnd_data.kgn_devices[devno];
2666 /* allocate a 'dummy' cdm for datagram use. We can only have a single
2667 * datagram between a nid:inst_id and nid2:inst_id. The fake cdm
2668 * give us additional inst_id to use, allowing the datagrams to flow
2669 * like rivers of honey and beer */
2671 /* the instance id for the cdm is the NETNUM offset by MAXDEVS -
2672 * ensuring we'll have a unique id */
2675 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), net->gnn_dev->gnd_nid);
2676 CDEBUG(D_NET, "adding net %p nid=%s on dev %d \n",
2677 net, libcfs_nid2str(ni->ni_nid), net->gnn_dev->gnd_id);
2678 /* until the gnn_list is set, we need to cleanup ourselves as
2679 * kgnilnd_shutdown is just gonna get confused */
2681 down_write(&kgnilnd_data.kgn_net_rw_sem);
2682 list_add_tail(&net->gnn_list, kgnilnd_netnum2netlist(net->gnn_netnum));
2683 up_write(&kgnilnd_data.kgn_net_rw_sem);
2685 /* we need a separate thread to call probe_wait_by_id until
2686 * we get a function callback notifier from kgni */
2687 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2690 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2691 kgnilnd_shutdown(ni);
2696 kgnilnd_shutdown(struct lnet_ni *ni)
2698 kgn_net_t *net = ni->ni_data;
2703 CFS_RACE(CFS_FAIL_GNI_SR_DOWN_RACE);
2705 LASSERTF(kgnilnd_data.kgn_init == GNILND_INIT_ALL,
2706 "init %d\n", kgnilnd_data.kgn_init);
2708 /* Serialize with startup. */
2709 mutex_lock(&kgnilnd_data.kgn_quiesce_mutex);
2710 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %lld\n",
2711 libcfs_kmem_read());
2714 CERROR("got NULL net for ni %p\n", ni);
2715 GOTO(out, rc = -EINVAL);
2718 LASSERTF(ni == net->gnn_ni,
2719 "ni %p gnn_ni %p\n", net, net->gnn_ni);
2723 LASSERT(!net->gnn_shutdown);
2724 LASSERTF(atomic_read(&net->gnn_refcount) != 0,
2725 "net %p refcount %d\n",
2726 net, atomic_read(&net->gnn_refcount));
2728 if (!list_empty(&net->gnn_list)) {
2729 /* serialize with peer creation */
2730 down_write(&kgnilnd_data.kgn_net_rw_sem);
2731 net->gnn_shutdown = 1;
2732 up_write(&kgnilnd_data.kgn_net_rw_sem);
2734 kgnilnd_cancel_net_dgrams(net);
2736 kgnilnd_del_conn_or_peer(net, LNET_NID_ANY, GNILND_DEL_PEER, -ESHUTDOWN);
2738 /* if we are quiesced, need to wake up - we need those threads
2739 * alive to release peers, etc */
2740 if (GNILND_IS_QUIESCED) {
2741 set_mb(kgnilnd_data.kgn_quiesce_trigger, GNILND_QUIESCE_IDLE);
2742 kgnilnd_quiesce_wait("shutdown");
2745 kgnilnd_wait_for_canceled_dgrams(net->gnn_dev);
2747 /* We wait until the nets ref's are 1, we will release final ref which is ours
2748 * this allows us to make sure everything else is done before we free the
2752 while (atomic_read(&net->gnn_refcount) != 1) {
2754 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2755 "Waiting for %d references to clear on net %d\n",
2756 atomic_read(&net->gnn_refcount),
2758 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2761 /* release ref from kgnilnd_startup */
2762 kgnilnd_net_decref(net);
2763 /* serialize with reaper and conn_task looping */
2764 down_write(&kgnilnd_data.kgn_net_rw_sem);
2765 list_del_init(&net->gnn_list);
2766 up_write(&kgnilnd_data.kgn_net_rw_sem);
2770 /* not locking, this can't race with writers */
2771 LASSERTF(atomic_read(&net->gnn_refcount) == 0,
2772 "net %p refcount %d\n",
2773 net, atomic_read(&net->gnn_refcount));
2774 LIBCFS_FREE(net, sizeof(*net));
2777 down_read(&kgnilnd_data.kgn_net_rw_sem);
2778 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
2779 if (!list_empty(&kgnilnd_data.kgn_nets[i])) {
2780 up_read(&kgnilnd_data.kgn_net_rw_sem);
2784 if (i == *kgnilnd_tunables.kgn_net_hash_size - 1) {
2785 up_read(&kgnilnd_data.kgn_net_rw_sem);
2786 kgnilnd_base_shutdown();
2789 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %lld\n",
2790 libcfs_kmem_read());
2792 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2796 static void __exit kgnilnd_exit(void)
2798 lnet_unregister_lnd(&the_kgnilnd);
2799 kgnilnd_proc_fini();
2800 kgnilnd_remove_sysctl();
2803 static int __init kgnilnd_init(void)
2807 rc = kgnilnd_tunables_init();
2811 LCONSOLE_INFO("Lustre: kgnilnd build version: "LUSTRE_VERSION_STRING"\n");
2813 kgnilnd_insert_sysctl();
2814 kgnilnd_proc_init();
2816 lnet_register_lnd(&the_kgnilnd);
2821 MODULE_AUTHOR("Cray, Inc. <nic@cray.com>");
2822 MODULE_DESCRIPTION("Gemini LNet Network Driver");
2823 MODULE_VERSION(LUSTRE_VERSION_STRING);
2824 MODULE_LICENSE("GPL");
2826 module_init(kgnilnd_init);
2827 module_exit(kgnilnd_exit);