2 * Copyright (C) 2012 Cray, Inc.
4 * Copyright (c) 2013, 2014, 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. */
34 .lnd_startup = kgnilnd_startup,
35 .lnd_shutdown = kgnilnd_shutdown,
36 .lnd_ctl = kgnilnd_ctl,
37 .lnd_send = kgnilnd_send,
38 .lnd_recv = kgnilnd_recv,
39 .lnd_eager_recv = kgnilnd_eager_recv,
40 .lnd_query = kgnilnd_query,
43 kgn_data_t kgnilnd_data;
45 /* needs write_lock on kgn_peer_conn_lock */
47 kgnilnd_close_stale_conns_locked(kgn_peer_t *peer, kgn_conn_t *newconn)
50 struct list_head *ctmp, *cnxt;
54 loopback = peer->gnp_nid == peer->gnp_net->gnn_ni->ni_nid;
56 list_for_each_safe(ctmp, cnxt, &peer->gnp_conns) {
57 conn = list_entry(ctmp, kgn_conn_t, gnc_list);
59 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
65 if (conn->gnc_device != newconn->gnc_device)
68 /* This is a two connection loopback - one talking to the other */
70 newconn->gnc_my_connstamp == conn->gnc_peer_connstamp &&
71 newconn->gnc_peer_connstamp == conn->gnc_my_connstamp) {
72 CDEBUG(D_NET, "skipping prune of %p, "
73 "loopback and matching stamps"
74 " connstamp "LPU64"("LPU64")"
75 " peerstamp "LPU64"("LPU64")\n",
76 conn, newconn->gnc_my_connstamp,
77 conn->gnc_peer_connstamp,
78 newconn->gnc_peer_connstamp,
79 conn->gnc_my_connstamp);
83 if (conn->gnc_peerstamp != newconn->gnc_peerstamp) {
84 LASSERTF(conn->gnc_peerstamp < newconn->gnc_peerstamp,
85 "conn 0x%p peerstamp "LPU64" >= "
86 "newconn 0x%p peerstamp "LPU64"\n",
87 conn, conn->gnc_peerstamp,
88 newconn, newconn->gnc_peerstamp);
90 CDEBUG(D_NET, "Closing stale conn nid: %s "
91 " peerstamp:"LPX64"("LPX64")\n",
92 libcfs_nid2str(peer->gnp_nid),
93 conn->gnc_peerstamp, newconn->gnc_peerstamp);
96 LASSERTF(conn->gnc_peer_connstamp < newconn->gnc_peer_connstamp,
97 "conn 0x%p peer_connstamp "LPU64" >= "
98 "newconn 0x%p peer_connstamp "LPU64"\n",
99 conn, conn->gnc_peer_connstamp,
100 newconn, newconn->gnc_peer_connstamp);
102 CDEBUG(D_NET, "Closing stale conn nid: %s"
103 " connstamp:"LPU64"("LPU64")\n",
104 libcfs_nid2str(peer->gnp_nid),
105 conn->gnc_peer_connstamp, newconn->gnc_peer_connstamp);
109 kgnilnd_close_conn_locked(conn, -ESTALE);
113 CWARN("Closed %d stale conns to %s\n", count, libcfs_nid2str(peer->gnp_nid));
120 kgnilnd_conn_isdup_locked(kgn_peer_t *peer, kgn_conn_t *newconn)
123 struct list_head *tmp;
127 loopback = peer->gnp_nid == peer->gnp_net->gnn_ni->ni_nid;
129 list_for_each(tmp, &peer->gnp_conns) {
130 conn = list_entry(tmp, kgn_conn_t, gnc_list);
131 CDEBUG(D_NET, "checking conn 0x%p for peer %s"
132 " lo %d new "LPU64" existing "LPU64
133 " new peer "LPU64" existing peer "LPU64
134 " new dev %p existing dev %p\n",
135 conn, libcfs_nid2str(peer->gnp_nid),
137 newconn->gnc_peerstamp, conn->gnc_peerstamp,
138 newconn->gnc_peer_connstamp, conn->gnc_peer_connstamp,
139 newconn->gnc_device, conn->gnc_device);
141 /* conn is in the process of closing */
142 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
145 /* 'newconn' is from an earlier version of 'peer'!!! */
146 if (newconn->gnc_peerstamp < conn->gnc_peerstamp)
149 /* 'conn' is from an earlier version of 'peer': it will be
150 * removed when we cull stale conns later on... */
151 if (newconn->gnc_peerstamp > conn->gnc_peerstamp)
154 /* Different devices are OK */
155 if (conn->gnc_device != newconn->gnc_device)
158 /* It's me connecting to myself */
160 newconn->gnc_my_connstamp == conn->gnc_peer_connstamp &&
161 newconn->gnc_peer_connstamp == conn->gnc_my_connstamp)
164 /* 'newconn' is an earlier connection from 'peer'!!! */
165 if (newconn->gnc_peer_connstamp < conn->gnc_peer_connstamp)
168 /* 'conn' is an earlier connection from 'peer': it will be
169 * removed when we cull stale conns later on... */
170 if (newconn->gnc_peer_connstamp > conn->gnc_peer_connstamp)
173 /* 'newconn' has the SAME connection stamp; 'peer' isn't
174 * playing the game... */
182 kgnilnd_create_conn(kgn_conn_t **connp, kgn_device_t *dev)
188 LASSERT (!in_interrupt());
189 atomic_inc(&kgnilnd_data.kgn_nconns);
191 /* divide by 2 to allow for complete reset and immediate reconnect */
192 if (atomic_read(&kgnilnd_data.kgn_nconns) >= GNILND_MAX_CQID/2) {
193 CERROR("Too many conn are live: %d > %d\n",
194 atomic_read(&kgnilnd_data.kgn_nconns), GNILND_MAX_CQID/2);
195 atomic_dec(&kgnilnd_data.kgn_nconns);
199 LIBCFS_ALLOC(conn, sizeof(*conn));
201 atomic_dec(&kgnilnd_data.kgn_nconns);
205 LIBCFS_ALLOC(conn->gnc_tx_ref_table, GNILND_MAX_MSG_ID * sizeof(void *));
206 if (conn->gnc_tx_ref_table == NULL) {
207 CERROR("Can't allocate conn tx_ref_table\n");
208 GOTO(failed, rc = -ENOMEM);
211 mutex_init(&conn->gnc_smsg_mutex);
212 mutex_init(&conn->gnc_rdma_mutex);
213 atomic_set(&conn->gnc_refcount, 1);
214 atomic_set(&conn->gnc_reaper_noop, 0);
215 atomic_set(&conn->gnc_sched_noop, 0);
216 atomic_set(&conn->gnc_tx_in_use, 0);
217 INIT_LIST_HEAD(&conn->gnc_list);
218 INIT_LIST_HEAD(&conn->gnc_hashlist);
219 INIT_LIST_HEAD(&conn->gnc_schedlist);
220 INIT_LIST_HEAD(&conn->gnc_fmaq);
221 INIT_LIST_HEAD(&conn->gnc_mdd_list);
222 spin_lock_init(&conn->gnc_list_lock);
223 spin_lock_init(&conn->gnc_tx_lock);
224 conn->gnc_magic = GNILND_CONN_MAGIC;
226 /* set tx id to nearly the end to make sure we find wrapping
228 conn->gnc_next_tx = (int) GNILND_MAX_MSG_ID - 10;
230 /* if this fails, we have conflicts and MAX_TX is too large */
231 CLASSERT(GNILND_MAX_MSG_ID < GNILND_MSGID_CLOSE);
233 /* get a new unique CQ id for this conn */
234 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
235 conn->gnc_my_connstamp = kgnilnd_data.kgn_connstamp++;
236 conn->gnc_cqid = kgnilnd_get_cqid_locked();
237 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
239 if (conn->gnc_cqid == 0) {
240 CERROR("Could not allocate unique CQ ID for conn 0x%p\n", conn);
241 GOTO(failed, rc = -E2BIG);
244 CDEBUG(D_NET, "alloc cqid %u for conn 0x%p\n",
245 conn->gnc_cqid, conn);
247 /* need to be set before gnc_ephandle to allow kgnilnd_destroy_conn_ep to
249 conn->gnc_device = dev;
251 conn->gnc_timeout = MAX(*kgnilnd_tunables.kgn_timeout,
253 kgnilnd_update_reaper_timeout(conn->gnc_timeout);
255 /* this is the ep_handle for doing SMSG & BTE */
256 mutex_lock(&dev->gnd_cq_mutex);
257 rrc = kgnilnd_ep_create(dev->gnd_handle, dev->gnd_snd_fma_cqh,
258 &conn->gnc_ephandle);
259 mutex_unlock(&dev->gnd_cq_mutex);
260 if (rrc != GNI_RC_SUCCESS)
261 GOTO(failed, rc = -ENETDOWN);
263 CDEBUG(D_NET, "created conn 0x%p ep_hndl 0x%p\n",
264 conn, conn->gnc_ephandle);
266 /* add ref for EP canceling */
267 kgnilnd_conn_addref(conn);
268 atomic_inc(&dev->gnd_neps);
274 atomic_dec(&kgnilnd_data.kgn_nconns);
275 LIBCFS_FREE(conn->gnc_tx_ref_table, GNILND_MAX_MSG_ID * sizeof(void *));
276 LIBCFS_FREE(conn, sizeof(*conn));
280 /* needs to be called with kgn_peer_conn_lock held (read or write) */
282 kgnilnd_find_conn_locked(kgn_peer_t *peer)
284 kgn_conn_t *conn = NULL;
286 /* if we are in reset, this conn is going to die soon */
287 if (unlikely(kgnilnd_data.kgn_in_reset)) {
291 /* just return the first ESTABLISHED connection */
292 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
293 /* kgnilnd_finish_connect doesn't put connections on the
294 * peer list until they are actually established */
295 LASSERTF(conn->gnc_state >= GNILND_CONN_ESTABLISHED,
296 "found conn %p state %s on peer %p (%s)\n",
297 conn, kgnilnd_conn_state2str(conn), peer,
298 libcfs_nid2str(peer->gnp_nid));
299 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
307 /* needs write_lock on kgn_peer_conn_lock held */
309 kgnilnd_find_or_create_conn_locked(kgn_peer_t *peer) {
311 kgn_device_t *dev = peer->gnp_net->gnn_dev;
314 conn = kgnilnd_find_conn_locked(peer);
320 /* if the peer was previously connecting, check if we should
321 * trigger another connection attempt yet. */
322 if (time_before(jiffies, peer->gnp_reconnect_time)) {
326 /* This check prevents us from creating a new connection to a peer while we are
327 * still in the process of closing an existing connection to the peer.
329 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
330 if (conn->gnc_ephandle != NULL) {
331 CDEBUG(D_NET, "Not connecting non-null ephandle found peer 0x%p->%s\n", peer,
332 libcfs_nid2str(peer->gnp_nid));
337 if (peer->gnp_connecting != GNILND_PEER_IDLE) {
338 /* if we are not connecting, fire up a new connection */
339 /* or if we are anything but IDLE DONT start a new connection */
343 CDEBUG(D_NET, "starting connect to %s\n",
344 libcfs_nid2str(peer->gnp_nid));
345 peer->gnp_connecting = GNILND_PEER_CONNECT;
346 kgnilnd_peer_addref(peer); /* extra ref for connd */
348 spin_lock(&dev->gnd_connd_lock);
349 list_add_tail(&peer->gnp_connd_list, &dev->gnd_connd_peers);
350 spin_unlock(&dev->gnd_connd_lock);
352 kgnilnd_schedule_dgram(dev);
353 CDEBUG(D_NETTRACE, "scheduling new connect\n");
358 /* Caller is responsible for deciding if/when to call this */
360 kgnilnd_destroy_conn_ep(kgn_conn_t *conn)
363 gni_ep_handle_t tmp_ep;
365 /* only if we actually initialized it,
366 * then set NULL to tell kgnilnd_destroy_conn to leave it alone */
368 tmp_ep = xchg(&conn->gnc_ephandle, NULL);
369 if (tmp_ep != NULL) {
370 /* we never re-use the EP, so unbind is not needed */
371 mutex_lock(&conn->gnc_device->gnd_cq_mutex);
372 rrc = kgnilnd_ep_destroy(tmp_ep);
374 mutex_unlock(&conn->gnc_device->gnd_cq_mutex);
376 /* if this fails, it could hork up kgni smsg retransmit and others
377 * since we could free the SMSG mbox memory, etc. */
378 LASSERTF(rrc == GNI_RC_SUCCESS, "rrc %d conn 0x%p ep 0x%p\n",
379 rrc, conn, conn->gnc_ephandle);
381 atomic_dec(&conn->gnc_device->gnd_neps);
383 /* clear out count added in kgnilnd_close_conn_locked
384 * conn will have a peer once it hits finish_connect, where it
385 * is the first spot we'll mark it ESTABLISHED as well */
386 if (conn->gnc_peer) {
387 kgnilnd_admin_decref(conn->gnc_peer->gnp_dirty_eps);
390 /* drop ref for EP */
391 kgnilnd_conn_decref(conn);
396 kgnilnd_destroy_conn(kgn_conn_t *conn)
398 LASSERTF(!in_interrupt() &&
399 !conn->gnc_scheduled &&
400 !conn->gnc_in_purgatory &&
401 conn->gnc_ephandle == NULL &&
402 list_empty(&conn->gnc_list) &&
403 list_empty(&conn->gnc_hashlist) &&
404 list_empty(&conn->gnc_schedlist) &&
405 list_empty(&conn->gnc_mdd_list) &&
406 conn->gnc_magic == GNILND_CONN_MAGIC,
407 "conn 0x%p->%s IRQ %d sched %d purg %d ep 0x%p Mg %d lists %d/%d/%d/%d\n",
408 conn, conn->gnc_peer ? libcfs_nid2str(conn->gnc_peer->gnp_nid)
410 !!in_interrupt(), conn->gnc_scheduled,
411 conn->gnc_in_purgatory,
414 list_empty(&conn->gnc_list),
415 list_empty(&conn->gnc_hashlist),
416 list_empty(&conn->gnc_schedlist),
417 list_empty(&conn->gnc_mdd_list));
419 /* Tripping these is especially bad, as it means we have items on the
420 * lists that didn't keep their refcount on the connection - or
421 * somebody evil released their own */
422 LASSERTF(list_empty(&conn->gnc_fmaq) &&
423 atomic_read(&conn->gnc_nlive_fma) == 0 &&
424 atomic_read(&conn->gnc_nlive_rdma) == 0,
425 "conn 0x%p fmaq %d@0x%p nfma %d nrdma %d\n",
426 conn, kgnilnd_count_list(&conn->gnc_fmaq), &conn->gnc_fmaq,
427 atomic_read(&conn->gnc_nlive_fma), atomic_read(&conn->gnc_nlive_rdma));
429 CDEBUG(D_NET, "destroying conn %p ephandle %p error %d\n",
430 conn, conn->gnc_ephandle, conn->gnc_error);
432 /* We are freeing this memory remove the magic value from the connection */
435 /* if there is an FMA blk left here, we'll tear it down */
436 if (conn->gnc_fma_blk) {
437 if (conn->gnc_peer) {
438 kgn_mbox_info_t *mbox;
439 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
440 mbox->mbx_prev_nid = conn->gnc_peer->gnp_nid;
442 kgnilnd_release_mbox(conn, 0);
445 if (conn->gnc_peer != NULL)
446 kgnilnd_peer_decref(conn->gnc_peer);
448 if (conn->gnc_tx_ref_table != NULL) {
449 LIBCFS_FREE(conn->gnc_tx_ref_table,
450 GNILND_MAX_MSG_ID * sizeof(void *));
453 LIBCFS_FREE(conn, sizeof(*conn));
454 atomic_dec(&kgnilnd_data.kgn_nconns);
457 /* peer_alive and peer_notify done in the style of the o2iblnd */
459 kgnilnd_peer_alive(kgn_peer_t *peer)
461 set_mb(peer->gnp_last_alive, jiffies);
465 kgnilnd_peer_notify(kgn_peer_t *peer, int error, int alive)
476 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_DONT_NOTIFY))
479 /* Tell LNet we are giving ups on this peer - but only
480 * if it isn't already reconnected or trying to reconnect */
481 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
483 /* use kgnilnd_find_conn_locked to avoid any conns in the process of being nuked
485 * don't tell LNet if we are in reset - we assume that everyone will be able to
486 * reconnect just fine
488 conn = kgnilnd_find_conn_locked(peer);
490 CDEBUG(D_NETTRACE, "peer 0x%p->%s ting %d conn 0x%p, rst %d error %d\n",
491 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting, conn,
492 kgnilnd_data.kgn_in_reset, error);
494 if (((peer->gnp_connecting == GNILND_PEER_IDLE) &&
496 (!kgnilnd_data.kgn_in_reset) &&
497 (!kgnilnd_conn_clean_errno(error))) || alive) {
501 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
504 /* short circuit if we dont need to notify Lnet */
508 rc = down_read_trylock(&kgnilnd_data.kgn_net_rw_sem);
511 /* dont do this if this fails since LNET is in shutdown or something else
514 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
515 list_for_each_entry(net , &kgnilnd_data.kgn_nets[i], gnn_list) {
516 /* if gnn_shutdown set for any net shutdown is in progress just return */
517 if (net->gnn_shutdown) {
518 up_read(&kgnilnd_data.kgn_net_rw_sem);
526 /* shutdown in progress most likely */
527 up_read(&kgnilnd_data.kgn_net_rw_sem);
531 LIBCFS_ALLOC(nets, nnets * sizeof(*nets));
534 up_read(&kgnilnd_data.kgn_net_rw_sem);
535 CERROR("Failed to allocate nets[%d]\n", nnets);
540 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
541 list_for_each_entry(net, &kgnilnd_data.kgn_nets[i], gnn_list) {
543 kgnilnd_net_addref(net);
547 up_read(&kgnilnd_data.kgn_net_rw_sem);
549 for (i = 0; i < nnets; i++) {
554 peer_nid = kgnilnd_lnd2lnetnid(net->gnn_ni->ni_nid,
557 CDEBUG(D_NET, "peer 0x%p->%s last_alive %lu (%lus ago)\n",
558 peer, libcfs_nid2str(peer_nid), peer->gnp_last_alive,
559 cfs_duration_sec(jiffies - peer->gnp_last_alive));
561 lnet_notify(net->gnn_ni, peer_nid, alive,
562 peer->gnp_last_alive);
564 kgnilnd_net_decref(net);
567 LIBCFS_FREE(nets, nnets * sizeof(*nets));
571 /* need write_lock on kgn_peer_conn_lock */
573 kgnilnd_close_conn_locked(kgn_conn_t *conn, int error)
575 kgn_peer_t *peer = conn->gnc_peer;
578 LASSERT(!in_interrupt());
580 /* store error for tx completion */
581 conn->gnc_error = error;
582 peer->gnp_last_errno = error;
584 /* use real error from peer if possible */
585 if (error == -ECONNRESET) {
586 error = conn->gnc_peer_error;
589 /* if we NETERROR, make sure it is rate limited */
590 if (!kgnilnd_conn_clean_errno(error) &&
591 peer->gnp_down == GNILND_RCA_NODE_UP) {
592 CNETERR("closing conn to %s: error %d\n",
593 libcfs_nid2str(peer->gnp_nid), error);
595 CDEBUG(D_NET, "closing conn to %s: error %d\n",
596 libcfs_nid2str(peer->gnp_nid), error);
599 LASSERTF(conn->gnc_state == GNILND_CONN_ESTABLISHED,
600 "conn %p to %s with bogus state %s\n", conn,
601 libcfs_nid2str(conn->gnc_peer->gnp_nid),
602 kgnilnd_conn_state2str(conn));
603 LASSERT(!list_empty(&conn->gnc_hashlist));
604 LASSERT(!list_empty(&conn->gnc_list));
607 /* mark peer count here so any place the EP gets destroyed will
608 * open up the peer count so that a new ESTABLISHED conn is then free
609 * to send new messages -- sending before the previous EPs are destroyed
610 * could end up with messages on the network for the old conn _after_
611 * the new conn and break the mbox safety protocol */
612 kgnilnd_admin_addref(conn->gnc_peer->gnp_dirty_eps);
614 /* Remove from conn hash table: no new callbacks */
615 list_del_init(&conn->gnc_hashlist);
616 kgnilnd_data.kgn_conn_version++;
617 kgnilnd_conn_decref(conn);
619 /* if we are in reset, go right to CLOSED as there is no scheduler
620 * thread to move from CLOSING to CLOSED */
621 if (unlikely(kgnilnd_data.kgn_in_reset)) {
622 conn->gnc_state = GNILND_CONN_CLOSED;
624 conn->gnc_state = GNILND_CONN_CLOSING;
627 /* leave on peer->gnp_conns to make sure we don't let the reaper
628 * or others try to unlink this peer until the conn is fully
629 * processed for closing */
631 if (kgnilnd_check_purgatory_conn(conn)) {
632 kgnilnd_add_purgatory_locked(conn, conn->gnc_peer);
635 /* Reset RX timeout to ensure we wait for an incoming CLOSE
636 * for the full timeout. If we get a CLOSE we know the
637 * peer has stopped all RDMA. Otherwise if we wait for
638 * the full timeout we can also be sure all RDMA has stopped. */
639 conn->gnc_last_rx = conn->gnc_last_rx_cq = jiffies;
642 /* schedule sending CLOSE - if we are in quiesce, this adds to
643 * gnd_ready_conns and allows us to find it in quiesce processing */
644 kgnilnd_schedule_conn(conn);
650 kgnilnd_close_conn(kgn_conn_t *conn, int error)
652 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
653 /* need to check the state here - this call is racy and we don't
654 * know the state until after the lock is grabbed */
655 if (conn->gnc_state == GNILND_CONN_ESTABLISHED) {
656 kgnilnd_close_conn_locked(conn, error);
658 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
662 kgnilnd_complete_closed_conn(kgn_conn_t *conn)
672 /* Dump log on cksum error - wait until complete phase to let
673 * RX of error happen */
674 if (*kgnilnd_tunables.kgn_checksum_dump &&
675 (conn != NULL && conn->gnc_peer_error == -ENOKEY)) {
676 libcfs_debug_dumplog();
679 /* _CLOSED set in kgnilnd_process_fmaq once we decide to
680 * send the CLOSE or not */
681 LASSERTF(conn->gnc_state == GNILND_CONN_CLOSED,
682 "conn 0x%p->%s with bad state %s\n",
683 conn, conn->gnc_peer ?
684 libcfs_nid2str(conn->gnc_peer->gnp_nid) :
686 kgnilnd_conn_state2str(conn));
688 LASSERT(list_empty(&conn->gnc_hashlist));
690 /* we've sent the close, start nuking */
691 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_SCHEDULE_COMPLETE))
692 kgnilnd_schedule_conn(conn);
694 if (conn->gnc_scheduled != GNILND_CONN_PROCESS) {
695 CDEBUG(D_NETERROR, "Error someone scheduled us after we were "
696 "done, Attempting to recover conn 0x%p "
697 "scheduled %d function: %s line: %d\n", conn,
698 conn->gnc_scheduled, conn->gnc_sched_caller,
699 conn->gnc_sched_line);
703 /* we don't use lists to track things that we can get out of the
706 /* need to hold locks for tx_list_state, sampling it is too racy:
707 * - the lock actually protects tx != NULL, but we can't take the proper
708 * lock until we check tx_list_state, which would be too late and
709 * we could have the TX change under us.
710 * gnd_rdmaq_lock and gnd_lock and not used together, so taking both
712 spin_lock(&conn->gnc_device->gnd_rdmaq_lock);
713 spin_lock(&conn->gnc_device->gnd_lock);
715 for (nrdma = 0; nrdma < GNILND_MAX_MSG_ID; nrdma++) {
716 tx = conn->gnc_tx_ref_table[nrdma];
719 /* only print the first error and if not CLOSE, we often don't see
720 * CQ events for that by the time we get here... and really don't care */
721 if (nlive || tx->tx_msg.gnm_type == GNILND_MSG_CLOSE)
722 tx->tx_state |= GNILND_TX_QUIET_ERROR;
724 GNIDBG_TX(D_NET, tx, "cleaning up on close, nlive %d", nlive);
726 /* don't worry about gnc_lock here as nobody else should be
727 * touching this conn */
728 kgnilnd_tx_del_state_locked(tx, NULL, conn, GNILND_TX_ALLOCD);
729 list_add_tail(&tx->tx_list, &sinners);
732 spin_unlock(&conn->gnc_device->gnd_lock);
733 spin_unlock(&conn->gnc_device->gnd_rdmaq_lock);
735 /* nobody should have marked this as needing scheduling after
736 * we called close - so only ref should be us handling it */
737 if (conn->gnc_scheduled != GNILND_CONN_PROCESS) {
738 CDEBUG(D_NETERROR, "Error someone scheduled us after we were "
739 "done, Attempting to recover conn 0x%p "
740 "scheduled %d function %s line: %d\n", conn,
741 conn->gnc_scheduled, conn->gnc_sched_caller,
742 conn->gnc_sched_line);
744 /* now reset a few to actual counters... */
745 nrdma = atomic_read(&conn->gnc_nlive_rdma);
746 nq_rdma = atomic_read(&conn->gnc_nq_rdma);
748 if (!list_empty(&sinners)) {
749 list_for_each_entry_safe(tx, txn, &sinners, tx_list) {
750 /* clear tx_list to make tx_add_list_locked happy */
751 list_del_init(&tx->tx_list);
752 /* The error codes determine if we hold onto the MDD */
753 kgnilnd_tx_done(tx, conn->gnc_error);
757 logmsg = (nlive + nrdma + nq_rdma);
760 if (conn->gnc_peer->gnp_down == GNILND_RCA_NODE_UP) {
761 CNETERR("Closed conn 0x%p->%s (errno %d, peer errno %d): "
762 "canceled %d TX, %d/%d RDMA\n",
763 conn, libcfs_nid2str(conn->gnc_peer->gnp_nid),
764 conn->gnc_error, conn->gnc_peer_error,
765 nlive, nq_rdma, nrdma);
767 CDEBUG(D_NET, "Closed conn 0x%p->%s (errno %d,"
768 " peer errno %d): canceled %d TX, %d/%d RDMA\n",
769 conn, libcfs_nid2str(conn->gnc_peer->gnp_nid),
770 conn->gnc_error, conn->gnc_peer_error,
771 nlive, nq_rdma, nrdma);
775 kgnilnd_destroy_conn_ep(conn);
777 /* Bug 765042 - race this with completing a new conn to same peer - we need
778 * finish_connect to detach purgatory before we can do it ourselves here */
779 CFS_RACE(CFS_FAIL_GNI_FINISH_PURG);
781 /* now it is safe to remove from peer list - anyone looking at
782 * gnp_conns now is free to unlink if not on purgatory */
783 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
785 conn->gnc_state = GNILND_CONN_DONE;
787 /* Decrement counter if we are marked by del_conn_or_peers for closing
789 if (conn->gnc_needs_closing)
790 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_conns);
792 /* Remove from peer's list of valid connections if its not in purgatory */
793 if (!conn->gnc_in_purgatory) {
794 list_del_init(&conn->gnc_list);
795 /* Lose peers reference on the conn */
796 kgnilnd_conn_decref(conn);
799 /* NB - only unlinking if we set pending in del_peer_locked from admin or
801 if (kgnilnd_peer_active(conn->gnc_peer) &&
802 conn->gnc_peer->gnp_pending_unlink &&
803 kgnilnd_can_unlink_peer_locked(conn->gnc_peer)) {
804 kgnilnd_unlink_peer_locked(conn->gnc_peer);
807 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
809 /* I'm telling Mommy! - use peer_error if they initiated close */
810 kgnilnd_peer_notify(conn->gnc_peer,
811 conn->gnc_error == -ECONNRESET ?
812 conn->gnc_peer_error : conn->gnc_error, 0);
818 kgnilnd_set_conn_params(kgn_dgram_t *dgram)
820 kgn_conn_t *conn = dgram->gndg_conn;
821 kgn_connreq_t *connreq = &dgram->gndg_conn_in;
822 kgn_gniparams_t *rem_param = &connreq->gncr_gnparams;
825 gni_smsg_attr_t *remote = &connreq->gncr_gnparams.gnpr_smsg_attr;
827 /* set timeout vals in conn early so we can use them for the NAK */
829 /* use max of the requested and our timeout, peer will do the same */
830 conn->gnc_timeout = MAX(conn->gnc_timeout, connreq->gncr_timeout);
832 /* only ep_bind really mucks around with the CQ */
833 /* only ep bind if we are not connecting to ourself and the dstnid is not a wildcard. this check
834 * is necessary as you can only bind an ep once and we must make sure we dont bind when already bound.
836 if (connreq->gncr_dstnid != LNET_NID_ANY && dgram->gndg_conn_out.gncr_dstnid != connreq->gncr_srcnid) {
837 mutex_lock(&conn->gnc_device->gnd_cq_mutex);
838 rrc = kgnilnd_ep_bind(conn->gnc_ephandle,
839 connreq->gncr_gnparams.gnpr_host_id,
841 mutex_unlock(&conn->gnc_device->gnd_cq_mutex);
842 if (rrc != GNI_RC_SUCCESS) {
848 rrc = kgnilnd_ep_set_eventdata(conn->gnc_ephandle, conn->gnc_cqid,
849 connreq->gncr_gnparams.gnpr_cqid);
850 if (rrc != GNI_RC_SUCCESS) {
855 /* Initialize SMSG */
856 rrc = kgnilnd_smsg_init(conn->gnc_ephandle, &conn->gnpr_smsg_attr,
857 &connreq->gncr_gnparams.gnpr_smsg_attr);
858 if (unlikely(rrc == GNI_RC_INVALID_PARAM)) {
859 gni_smsg_attr_t *local = &conn->gnpr_smsg_attr;
860 /* help folks figure out if there is a tunable off, etc. */
861 LCONSOLE_ERROR("SMSG attribute mismatch. Data from local/remote:"
862 " type %d/%d msg_maxsize %u/%u"
863 " mbox_maxcredit %u/%u. Please check kgni"
864 " logs for further data\n",
865 local->msg_type, remote->msg_type,
866 local->msg_maxsize, remote->msg_maxsize,
867 local->mbox_maxcredit, remote->mbox_maxcredit);
869 if (rrc != GNI_RC_SUCCESS) {
874 /* log this for help in debuggin SMSG buffer re-use */
875 CDEBUG(D_NET, "conn %p src %s dst %s smsg %p acquired"
876 " local cqid %u SMSG %p->%u hndl "LPX64"."LPX64
877 " remote cqid %u SMSG %p->%u hndl "LPX64"."LPX64"\n",
878 conn, libcfs_nid2str(connreq->gncr_srcnid),
879 libcfs_nid2str(connreq->gncr_dstnid),
880 &conn->gnpr_smsg_attr,
882 conn->gnpr_smsg_attr.msg_buffer,
883 conn->gnpr_smsg_attr.mbox_offset,
884 conn->gnpr_smsg_attr.mem_hndl.qword1,
885 conn->gnpr_smsg_attr.mem_hndl.qword2,
886 rem_param->gnpr_cqid,
887 rem_param->gnpr_smsg_attr.msg_buffer,
888 rem_param->gnpr_smsg_attr.mbox_offset,
889 rem_param->gnpr_smsg_attr.mem_hndl.qword1,
890 rem_param->gnpr_smsg_attr.mem_hndl.qword2);
892 conn->gnc_peerstamp = connreq->gncr_peerstamp;
893 conn->gnc_peer_connstamp = connreq->gncr_connstamp;
894 conn->remote_mbox_addr = (void *)((char *)remote->msg_buffer + remote->mbox_offset);
896 /* We update the reaper timeout once we have a valid conn and timeout */
897 kgnilnd_update_reaper_timeout(GNILND_TO2KA(conn->gnc_timeout));
902 rrc = kgnilnd_ep_unbind(conn->gnc_ephandle);
903 /* not sure I can just let this fly */
904 LASSERTF(rrc == GNI_RC_SUCCESS,
905 "bad rc from gni_ep_unbind trying to cleanup: %d\n", rrc);
908 LASSERTF(rc != 0, "SOFTWARE BUG: rc == 0\n");
909 CERROR("Error setting connection params from %s: %d\n",
910 libcfs_nid2str(connreq->gncr_srcnid), rc);
914 /* needs down_read on kgn_net_rw_sem held from before this call until
915 * after the write_lock on kgn_peer_conn_lock - this ensures we stay sane
916 * with kgnilnd_shutdown - it'll get the sem and set shutdown, then get the
917 * kgn_peer_conn_lock to start del_peer'ing. If we hold the sem until after
918 * kgn_peer_conn_lock is held, we guarantee that nobody calls
919 * kgnilnd_add_peer_locked without checking gnn_shutdown */
921 kgnilnd_create_peer_safe(kgn_peer_t **peerp,
929 LASSERT(nid != LNET_NID_ANY);
931 /* We dont pass the net around in the dgram anymore so here is where we find it
932 * this will work unless its in shutdown or the nid has a net that is invalid.
933 * Either way error code needs to be returned in that case.
935 * If the net passed in is not NULL then we can use it, this alleviates looking it
936 * when the calling function has access to the data.
939 rc = kgnilnd_find_net(nid, &net);
943 /* find net adds a reference on the net if we are not using
944 * it we must do it manually so the net references are
945 * correct when tearing down the net
947 kgnilnd_net_addref(net);
950 LIBCFS_ALLOC(peer, sizeof(*peer));
952 kgnilnd_net_decref(net);
956 peer->gnp_down = node_state;
958 /* translate from nid to nic addr & store */
959 rc = kgnilnd_nid_to_nicaddrs(LNET_NIDADDR(nid), 1, &peer->gnp_host_id);
961 kgnilnd_net_decref(net);
962 LIBCFS_FREE(peer, sizeof(*peer));
965 CDEBUG(D_NET, "peer 0x%p->%s -> NIC 0x%x\n", peer,
966 libcfs_nid2str(nid), peer->gnp_host_id);
968 atomic_set(&peer->gnp_refcount, 1); /* 1 ref for caller */
969 atomic_set(&peer->gnp_dirty_eps, 0);
971 INIT_LIST_HEAD(&peer->gnp_list);
972 INIT_LIST_HEAD(&peer->gnp_connd_list);
973 INIT_LIST_HEAD(&peer->gnp_conns);
974 INIT_LIST_HEAD(&peer->gnp_tx_queue);
976 /* the first reconnect should happen immediately, so we leave
977 * gnp_reconnect_interval set to 0 */
979 LASSERTF(net != NULL, "peer 0x%p->%s with NULL net\n",
980 peer, libcfs_nid2str(nid));
982 /* must have kgn_net_rw_sem held for this... */
983 if (net->gnn_shutdown) {
984 /* shutdown has started already */
985 kgnilnd_net_decref(net);
986 LIBCFS_FREE(peer, sizeof(*peer));
992 atomic_inc(&kgnilnd_data.kgn_npeers);
999 kgnilnd_destroy_peer(kgn_peer_t *peer)
1001 CDEBUG(D_NET, "peer %s %p deleted\n",
1002 libcfs_nid2str(peer->gnp_nid), peer);
1003 LASSERTF(atomic_read(&peer->gnp_refcount) == 0,
1004 "peer 0x%p->%s refs %d\n",
1005 peer, libcfs_nid2str(peer->gnp_nid),
1006 atomic_read(&peer->gnp_refcount));
1007 LASSERTF(atomic_read(&peer->gnp_dirty_eps) == 0,
1008 "peer 0x%p->%s dirty eps %d\n",
1009 peer, libcfs_nid2str(peer->gnp_nid),
1010 atomic_read(&peer->gnp_dirty_eps));
1011 LASSERTF(peer->gnp_net != NULL, "peer %p (%s) with NULL net\n",
1012 peer, libcfs_nid2str(peer->gnp_nid));
1013 LASSERTF(!kgnilnd_peer_active(peer),
1015 peer, libcfs_nid2str(peer->gnp_nid));
1016 LASSERTF(peer->gnp_connecting == GNILND_PEER_IDLE || peer->gnp_connecting == GNILND_PEER_KILL,
1017 "peer 0x%p->%s, connecting %d\n",
1018 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting);
1019 LASSERTF(list_empty(&peer->gnp_conns),
1021 peer, libcfs_nid2str(peer->gnp_nid));
1022 LASSERTF(list_empty(&peer->gnp_tx_queue),
1024 peer, libcfs_nid2str(peer->gnp_nid));
1025 LASSERTF(list_empty(&peer->gnp_connd_list),
1027 peer, libcfs_nid2str(peer->gnp_nid));
1029 /* NB a peer's connections keep a reference on their peer until
1030 * they are destroyed, so we can be assured that _all_ state to do
1031 * with this peer has been cleaned up when its refcount drops to
1034 atomic_dec(&kgnilnd_data.kgn_npeers);
1035 kgnilnd_net_decref(peer->gnp_net);
1037 LIBCFS_FREE(peer, sizeof(*peer));
1040 /* the conn might not have made it all the way through to a connected
1041 * state - but we need to purgatory any conn that a remote peer might
1042 * have seen through a posted dgram as well */
1044 kgnilnd_add_purgatory_locked(kgn_conn_t *conn, kgn_peer_t *peer)
1046 kgn_mbox_info_t *mbox = NULL;
1049 /* NB - the caller should own conn by removing him from the
1050 * scheduler thread when finishing the close */
1052 LASSERTF(peer != NULL, "conn %p with NULL peer\n", conn);
1054 /* If this is still true, need to add the calls to unlink back in and
1055 * figure out how to close the hole on loopback conns */
1056 LASSERTF(kgnilnd_peer_active(peer), "can't use inactive peer %s (%p)"
1057 " we'll never recover the resources\n",
1058 libcfs_nid2str(peer->gnp_nid), peer);
1060 CDEBUG(D_NET, "conn %p peer %p dev %p\n", conn, peer,
1063 LASSERTF(conn->gnc_in_purgatory == 0,
1064 "Conn already in purgatory\n");
1065 conn->gnc_in_purgatory = 1;
1067 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
1068 mbox->mbx_prev_purg_nid = peer->gnp_nid;
1069 mbox->mbx_add_purgatory = jiffies;
1070 kgnilnd_release_mbox(conn, 1);
1072 LASSERTF(list_empty(&conn->gnc_mdd_list),
1073 "conn 0x%p->%s with active purgatory hold MDD %d\n",
1074 conn, libcfs_nid2str(peer->gnp_nid),
1075 kgnilnd_count_list(&conn->gnc_mdd_list));
1080 /* Instead of detaching everything from purgatory here we just mark the conn as needing
1081 * detach, when the reaper checks the conn the next time it will detach it.
1082 * Calling function requires write_lock held on kgn_peer_conn_lock
1085 kgnilnd_mark_for_detach_purgatory_all_locked(kgn_peer_t *peer) {
1088 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
1089 if (conn->gnc_in_purgatory && !conn->gnc_needs_detach) {
1090 conn->gnc_needs_detach = 1;
1091 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_detach);
1096 /* Calling function needs a write_lock held on kgn_peer_conn_lock */
1098 kgnilnd_detach_purgatory_locked(kgn_conn_t *conn, struct list_head *conn_list)
1100 kgn_mbox_info_t *mbox = NULL;
1102 /* if needed, add the conn purgatory data to the list passed in */
1103 if (conn->gnc_in_purgatory) {
1104 CDEBUG(D_NET, "peer %p->%s purg_conn %p@%s mdd_list #tx %d\n",
1105 conn->gnc_peer, libcfs_nid2str(conn->gnc_peer->gnp_nid),
1106 conn, kgnilnd_conn_state2str(conn),
1107 kgnilnd_count_list(&conn->gnc_mdd_list));
1109 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
1110 mbox->mbx_detach_of_purgatory = jiffies;
1112 /* conn->gnc_list is the entry point on peer->gnp_conns, so detaching it
1113 * here removes it from the list of 'valid' peer connections.
1114 * We put the current conn onto a list of conns to call kgnilnd_release_purgatory_locked()
1115 * and as such the caller of kgnilnd_detach_purgatory_locked() now owns that conn, since its not
1116 * on the peer's conn_list anymore.
1119 list_del_init(&conn->gnc_list);
1121 /* NB - only unlinking if we set pending in del_peer_locked from admin or
1123 if (kgnilnd_peer_active(conn->gnc_peer) &&
1124 conn->gnc_peer->gnp_pending_unlink &&
1125 kgnilnd_can_unlink_peer_locked(conn->gnc_peer)) {
1126 kgnilnd_unlink_peer_locked(conn->gnc_peer);
1128 /* The reaper will not call detach unless the conn is fully through kgnilnd_complete_closed_conn.
1129 * If the conn is not in a DONE state somehow we are attempting to detach even though
1130 * the conn has not been fully cleaned up. If we detach while the conn is still closing
1131 * we will end up with an orphaned connection that has valid ep_handle, that is not on a
1135 LASSERTF(conn->gnc_state == GNILND_CONN_DONE, "Conn in invalid state %p@%s \n",
1136 conn, kgnilnd_conn_state2str(conn));
1138 /* move from peer to the delayed release list */
1139 list_add_tail(&conn->gnc_list, conn_list);
1144 kgnilnd_release_purgatory_list(struct list_head *conn_list)
1147 kgn_conn_t *conn, *connN;
1148 kgn_mdd_purgatory_t *gmp, *gmpN;
1150 list_for_each_entry_safe(conn, connN, conn_list, gnc_list) {
1151 dev = conn->gnc_device;
1153 kgnilnd_release_mbox(conn, -1);
1154 conn->gnc_in_purgatory = 0;
1156 list_del_init(&conn->gnc_list);
1158 /* gnc_needs_detach is set in kgnilnd_del_conn_or_peer. It is used to keep track
1159 * of conns that have been marked for detach by kgnilnd_del_conn_or_peer.
1160 * The function uses kgn_npending_detach to verify the conn has
1161 * actually been detached.
1164 if (conn->gnc_needs_detach)
1165 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_detach);
1167 /* if this guy is really dead (we are doing release from reaper),
1168 * make sure we tell LNet - if this is from other context,
1169 * the checks in the function will prevent an errant
1171 kgnilnd_peer_notify(conn->gnc_peer, conn->gnc_error, 0);
1173 list_for_each_entry_safe(gmp, gmpN, &conn->gnc_mdd_list,
1176 "dev %p releasing held mdd "LPX64"."LPX64"\n",
1177 conn->gnc_device, gmp->gmp_map_key.qword1,
1178 gmp->gmp_map_key.qword2);
1180 atomic_dec(&dev->gnd_n_mdd_held);
1181 kgnilnd_mem_mdd_release(conn->gnc_device->gnd_handle,
1183 /* ignoring the return code - if kgni/ghal can't find it
1184 * it must be released already */
1186 list_del_init(&gmp->gmp_list);
1187 LIBCFS_FREE(gmp, sizeof(*gmp));
1189 /* lose conn ref for purgatory */
1190 kgnilnd_conn_decref(conn);
1194 /* needs write_lock on kgnilnd_data.kgn_peer_conn_lock held */
1196 kgnilnd_peer_increase_reconnect_locked(kgn_peer_t *peer)
1200 current_to = peer->gnp_reconnect_interval;
1202 /* we'll try to reconnect fast the first time, then back-off */
1203 if (current_to == 0) {
1204 peer->gnp_reconnect_time = jiffies - 1;
1205 current_to = *kgnilnd_tunables.kgn_min_reconnect_interval;
1207 peer->gnp_reconnect_time = jiffies + cfs_time_seconds(current_to);
1208 /* add 50% of min timeout & retry */
1209 current_to += *kgnilnd_tunables.kgn_min_reconnect_interval / 2;
1212 current_to = MIN(current_to,
1213 *kgnilnd_tunables.kgn_max_reconnect_interval);
1215 peer->gnp_reconnect_interval = current_to;
1216 CDEBUG(D_NET, "peer %s can reconnect at %lu interval %lu\n",
1217 libcfs_nid2str(peer->gnp_nid), peer->gnp_reconnect_time,
1218 peer->gnp_reconnect_interval);
1221 /* needs kgnilnd_data.kgn_peer_conn_lock held */
1223 kgnilnd_find_peer_locked(lnet_nid_t nid)
1225 struct list_head *peer_list = kgnilnd_nid2peerlist(nid);
1228 /* Chopping nid down to only NIDADDR using LNET_NIDADDR so we only
1229 * have a single peer per device instead of a peer per nid/net combo.
1232 list_for_each_entry(peer, peer_list, gnp_list) {
1233 if (LNET_NIDADDR(nid) != LNET_NIDADDR(peer->gnp_nid))
1236 CDEBUG(D_NET, "got peer [%p] -> %s c %d (%d)\n",
1237 peer, libcfs_nid2str(nid),
1238 peer->gnp_connecting,
1239 atomic_read(&peer->gnp_refcount));
1245 /* need write_lock on kgn_peer_conn_lock */
1247 kgnilnd_unlink_peer_locked(kgn_peer_t *peer)
1249 LASSERTF(list_empty(&peer->gnp_conns),
1251 peer, libcfs_nid2str(peer->gnp_nid));
1252 LASSERTF(list_empty(&peer->gnp_tx_queue),
1254 peer, libcfs_nid2str(peer->gnp_nid));
1255 LASSERTF(kgnilnd_peer_active(peer),
1257 peer, libcfs_nid2str(peer->gnp_nid));
1258 CDEBUG(D_NET, "unlinking peer 0x%p->%s\n",
1259 peer, libcfs_nid2str(peer->gnp_nid));
1261 list_del_init(&peer->gnp_list);
1262 kgnilnd_data.kgn_peer_version++;
1263 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_unlink);
1264 /* lose peerlist's ref */
1265 kgnilnd_peer_decref(peer);
1269 kgnilnd_get_peer_info(int index,
1270 kgn_peer_t **found_peer,
1271 lnet_nid_t *id, __u32 *nic_addr,
1272 int *refcount, int *connecting)
1274 struct list_head *ptmp;
1279 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1281 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
1283 list_for_each(ptmp, &kgnilnd_data.kgn_peers[i]) {
1284 peer = list_entry(ptmp, kgn_peer_t, gnp_list);
1289 CDEBUG(D_NET, "found peer %p (%s) at index %d\n",
1290 peer, libcfs_nid2str(peer->gnp_nid), index);
1293 *id = peer->gnp_nid;
1294 *nic_addr = peer->gnp_host_id;
1295 *refcount = atomic_read(&peer->gnp_refcount);
1296 *connecting = peer->gnp_connecting;
1303 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1305 CDEBUG(D_NET, "no gni peer at index %d\n", index);
1309 /* requires write_lock on kgn_peer_conn_lock held */
1311 kgnilnd_add_peer_locked(lnet_nid_t nid, kgn_peer_t *new_stub_peer, kgn_peer_t **peerp)
1313 kgn_peer_t *peer, *peer2;
1315 LASSERTF(new_stub_peer != NULL, "bad stub peer for nid %s\n",
1316 libcfs_nid2str(nid));
1318 peer2 = kgnilnd_find_peer_locked(nid);
1319 if (peer2 != NULL) {
1320 /* A peer was created during the lock transition, so drop
1321 * the new one we created */
1322 kgnilnd_peer_decref(new_stub_peer);
1325 peer = new_stub_peer;
1326 /* peer table takes existing ref on peer */
1328 LASSERTF(!kgnilnd_peer_active(peer),
1329 "peer 0x%p->%s already in peer table\n",
1330 peer, libcfs_nid2str(peer->gnp_nid));
1331 list_add_tail(&peer->gnp_list,
1332 kgnilnd_nid2peerlist(nid));
1333 kgnilnd_data.kgn_peer_version++;
1336 LASSERTF(peer->gnp_net != NULL, "peer 0x%p->%s with NULL net\n",
1337 peer, libcfs_nid2str(peer->gnp_nid));
1342 kgnilnd_add_peer(kgn_net_t *net, lnet_nid_t nid, kgn_peer_t **peerp)
1349 if (nid == LNET_NID_ANY)
1352 node_state = kgnilnd_get_node_state(LNET_NIDADDR(nid));
1354 /* NB - this will not block during normal operations -
1355 * the only writer of this is in the startup/shutdown path. */
1356 rc = down_read_trylock(&kgnilnd_data.kgn_net_rw_sem);
1361 rc = kgnilnd_create_peer_safe(&peer, nid, net, node_state);
1363 up_read(&kgnilnd_data.kgn_net_rw_sem);
1367 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1368 up_read(&kgnilnd_data.kgn_net_rw_sem);
1370 kgnilnd_add_peer_locked(nid, peer, peerp);
1372 CDEBUG(D_NET, "peer 0x%p->%s connecting %d\n",
1373 peerp, libcfs_nid2str((*peerp)->gnp_nid),
1374 (*peerp)->gnp_connecting);
1376 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1380 /* needs write_lock on kgn_peer_conn_lock */
1382 kgnilnd_cancel_peer_connect_locked(kgn_peer_t *peer, struct list_head *zombies)
1386 /* we do care about state of gnp_connecting - we could be between
1387 * reconnect attempts, so try to find the dgram and cancel the TX
1388 * anyways. If we are in the process of posting DONT do anything;
1389 * once it fails or succeeds we can nuke the connect attempt.
1390 * We have no idea where in kgnilnd_post_dgram we are so we cant
1391 * attempt to cancel until the function is done.
1394 /* make sure peer isn't in process of connecting or waiting for connect*/
1395 spin_lock(&peer->gnp_net->gnn_dev->gnd_connd_lock);
1396 if (!(list_empty(&peer->gnp_connd_list))) {
1397 list_del_init(&peer->gnp_connd_list);
1398 /* remove connd ref */
1399 kgnilnd_peer_decref(peer);
1401 spin_unlock(&peer->gnp_net->gnn_dev->gnd_connd_lock);
1403 if (peer->gnp_connecting == GNILND_PEER_POSTING || peer->gnp_connecting == GNILND_PEER_NEEDS_DEATH) {
1404 peer->gnp_connecting = GNILND_PEER_NEEDS_DEATH;
1405 /* We are in process of posting right now the xchg set it up for us to
1406 * cancel the connect so we are finished for now */
1408 /* no need for exchange we have the peer lock and its ready for us to nuke */
1409 LASSERTF(peer->gnp_connecting != GNILND_PEER_POSTING,
1410 "Peer in invalid state 0x%p->%s, connecting %d\n",
1411 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting);
1412 peer->gnp_connecting = GNILND_PEER_IDLE;
1413 set_mb(peer->gnp_last_dgram_errno, -ETIMEDOUT);
1414 kgnilnd_find_and_cancel_dgram(peer->gnp_net->gnn_dev,
1418 /* The least we can do is nuke the tx's no matter what.... */
1419 list_for_each_entry_safe(tx, txn, &peer->gnp_tx_queue, tx_list) {
1420 kgnilnd_tx_del_state_locked(tx, peer, NULL,
1422 list_add_tail(&tx->tx_list, zombies);
1426 /* needs write_lock on kgn_peer_conn_lock */
1428 kgnilnd_del_peer_locked(kgn_peer_t *peer, int error)
1430 /* this peer could be passive and only held for purgatory,
1431 * take a ref to ensure it doesn't disappear in this function */
1432 kgnilnd_peer_addref(peer);
1434 CFS_RACE(CFS_FAIL_GNI_FIND_TARGET);
1436 /* if purgatory release cleared it out, don't try again */
1437 if (kgnilnd_peer_active(peer)) {
1438 /* always do this to allow kgnilnd_start_connect and
1439 * kgnilnd_finish_connect to catch this before they
1440 * wrap up their operations */
1441 if (kgnilnd_can_unlink_peer_locked(peer)) {
1442 /* already released purgatory, so only active
1444 kgnilnd_unlink_peer_locked(peer);
1446 kgnilnd_close_peer_conns_locked(peer, error);
1447 /* peer unlinks itself when last conn is closed */
1451 /* we are done, release back to the wild */
1452 kgnilnd_peer_decref(peer);
1456 kgnilnd_del_conn_or_peer(kgn_net_t *net, lnet_nid_t nid, int command,
1460 LIST_HEAD (zombies);
1461 struct list_head *ptmp, *pnxt;
1468 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1470 if (nid != LNET_NID_ANY)
1471 lo = hi = kgnilnd_nid2peerlist(nid) - kgnilnd_data.kgn_peers;
1474 hi = *kgnilnd_tunables.kgn_peer_hash_size - 1;
1475 /* wildcards always succeed */
1479 for (i = lo; i <= hi; i++) {
1480 list_for_each_safe(ptmp, pnxt, &kgnilnd_data.kgn_peers[i]) {
1481 peer = list_entry(ptmp, kgn_peer_t, gnp_list);
1483 LASSERTF(peer->gnp_net != NULL,
1484 "peer %p (%s) with NULL net\n",
1485 peer, libcfs_nid2str(peer->gnp_nid));
1487 if (net != NULL && peer->gnp_net != net)
1490 if (!(nid == LNET_NID_ANY || LNET_NIDADDR(peer->gnp_nid) == LNET_NIDADDR(nid)))
1493 /* In both cases, we want to stop any in-flight
1494 * connect attempts */
1495 kgnilnd_cancel_peer_connect_locked(peer, &zombies);
1498 case GNILND_DEL_CONN:
1499 kgnilnd_close_peer_conns_locked(peer, error);
1501 case GNILND_DEL_PEER:
1502 peer->gnp_pending_unlink = 1;
1503 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_unlink);
1504 kgnilnd_mark_for_detach_purgatory_all_locked(peer);
1505 kgnilnd_del_peer_locked(peer, error);
1507 case GNILND_CLEAR_PURGATORY:
1508 /* Mark everything ready for detach reaper will cleanup
1509 * once we release the kgn_peer_conn_lock
1511 kgnilnd_mark_for_detach_purgatory_all_locked(peer);
1512 peer->gnp_last_errno = -EISCONN;
1513 /* clear reconnect so he can reconnect soon */
1514 peer->gnp_reconnect_time = 0;
1515 peer->gnp_reconnect_interval = 0;
1518 CERROR("bad command %d\n", command);
1521 /* we matched something */
1526 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1529 kgnilnd_txlist_done(&zombies, error);
1531 /* This function does not return until the commands it initiated have completed,
1532 * since they have to work there way through the other threads. In the case of shutdown
1533 * threads are not woken up until after this call is initiated so we cannot wait, we just
1534 * need to return. The same applies for stack reset we shouldnt wait as the reset thread
1538 CFS_RACE(CFS_FAIL_GNI_RACE_RESET);
1540 if (error == -ENOTRECOVERABLE || error == -ESHUTDOWN) {
1545 while (atomic_read(&kgnilnd_data.kgn_npending_conns) ||
1546 atomic_read(&kgnilnd_data.kgn_npending_detach) ||
1547 atomic_read(&kgnilnd_data.kgn_npending_unlink)) {
1549 set_current_state(TASK_UNINTERRUPTIBLE);
1550 schedule_timeout(cfs_time_seconds(1));
1553 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, "Waiting on %d peers %d closes %d detaches\n",
1554 atomic_read(&kgnilnd_data.kgn_npending_unlink),
1555 atomic_read(&kgnilnd_data.kgn_npending_conns),
1556 atomic_read(&kgnilnd_data.kgn_npending_detach));
1563 kgnilnd_get_conn_by_idx(int index)
1566 struct list_head *ptmp;
1568 struct list_head *ctmp;
1572 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
1573 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1574 list_for_each(ptmp, &kgnilnd_data.kgn_peers[i]) {
1576 peer = list_entry(ptmp, kgn_peer_t, gnp_list);
1578 list_for_each(ctmp, &peer->gnp_conns) {
1579 conn = list_entry(ctmp, kgn_conn_t, gnc_list);
1581 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
1587 CDEBUG(D_NET, "++conn[%p] -> %s (%d)\n", conn,
1588 libcfs_nid2str(conn->gnc_peer->gnp_nid),
1589 atomic_read(&conn->gnc_refcount));
1590 kgnilnd_conn_addref(conn);
1591 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1595 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1602 kgnilnd_get_conn_info(kgn_peer_t *peer,
1603 int *device_id, __u64 *peerstamp,
1604 int *tx_seq, int *rx_seq,
1605 int *fmaq_len, int *nfma, int *nrdma)
1610 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1612 conn = kgnilnd_find_conn_locked(peer);
1618 *device_id = conn->gnc_device->gnd_host_id;
1619 *peerstamp = conn->gnc_peerstamp;
1620 *tx_seq = atomic_read(&conn->gnc_tx_seq);
1621 *rx_seq = atomic_read(&conn->gnc_rx_seq);
1622 *fmaq_len = kgnilnd_count_list(&conn->gnc_fmaq);
1623 *nfma = atomic_read(&conn->gnc_nlive_fma);
1624 *nrdma = atomic_read(&conn->gnc_nlive_rdma);
1626 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1630 /* needs write_lock on kgn_peer_conn_lock */
1632 kgnilnd_close_peer_conns_locked(kgn_peer_t *peer, int why)
1635 struct list_head *ctmp, *cnxt;
1638 list_for_each_safe(ctmp, cnxt, &peer->gnp_conns) {
1639 conn = list_entry(ctmp, kgn_conn_t, gnc_list);
1641 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
1645 /* we mark gnc_needs closing and increment kgn_npending_conns so that
1646 * kgnilnd_del_conn_or_peer can wait on the other threads closing
1647 * and cleaning up the connection.
1649 if (!conn->gnc_needs_closing) {
1650 conn->gnc_needs_closing = 1;
1651 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_conns);
1653 kgnilnd_close_conn_locked(conn, why);
1659 kgnilnd_report_node_state(lnet_nid_t nid, int down)
1662 kgn_peer_t *peer, *new_peer;
1665 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1666 peer = kgnilnd_find_peer_locked(nid);
1673 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1675 /* Don't add a peer for node up events */
1676 if (down == GNILND_RCA_NODE_UP) {
1680 /* find any valid net - we don't care which one... */
1681 down_read(&kgnilnd_data.kgn_net_rw_sem);
1682 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
1683 list_for_each_entry(net, &kgnilnd_data.kgn_nets[i],
1693 up_read(&kgnilnd_data.kgn_net_rw_sem);
1696 CNETERR("Could not find a net for nid %lld\n", nid);
1700 /* The nid passed in does not yet contain the net portion.
1701 * Let's build it up now
1703 nid = LNET_MKNID(LNET_NIDNET(net->gnn_ni->ni_nid), nid);
1704 rc = kgnilnd_add_peer(net, nid, &new_peer);
1707 CNETERR("Could not add peer for nid %lld, rc %d\n",
1712 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1713 peer = kgnilnd_find_peer_locked(nid);
1716 CNETERR("Could not find peer for nid %lld\n", nid);
1717 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1722 peer->gnp_down = down;
1724 if (down == GNILND_RCA_NODE_DOWN) {
1727 peer->gnp_down_event_time = jiffies;
1728 kgnilnd_cancel_peer_connect_locked(peer, &zombies);
1729 conn = kgnilnd_find_conn_locked(peer);
1732 kgnilnd_close_conn_locked(conn, -ENETRESET);
1735 peer->gnp_up_event_time = jiffies;
1738 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1740 if (down == GNILND_RCA_NODE_DOWN) {
1741 /* using ENETRESET so we don't get messages from
1744 kgnilnd_txlist_done(&zombies, -ENETRESET);
1745 kgnilnd_peer_notify(peer, -ECONNRESET, 0);
1746 LCONSOLE_INFO("Recieved down event for nid %lld\n", nid);
1753 kgnilnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
1755 struct libcfs_ioctl_data *data = arg;
1756 kgn_net_t *net = ni->ni_data;
1759 LASSERT(ni == net->gnn_ni);
1762 case IOC_LIBCFS_GET_PEER: {
1764 kgn_peer_t *peer = NULL;
1766 __u64 peerstamp = 0;
1767 int peer_refcount = 0, peer_connecting = 0;
1769 int tx_seq = 0, rx_seq = 0;
1770 int fmaq_len = 0, nfma = 0, nrdma = 0;
1772 rc = kgnilnd_get_peer_info(data->ioc_count, &peer,
1773 &nid, &nic_addr, &peer_refcount,
1779 /* LNET_MKNID is used to mask from lnet the multiplexing/demultiplexing of connections and peers
1780 * LNET assumes a conn and peer per net, the LNET_MKNID/LNET_NIDADDR allows us to let Lnet see what it
1781 * wants to see instead of the underlying network that is being used to send the data
1783 data->ioc_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), LNET_NIDADDR(nid));
1784 data->ioc_flags = peer_connecting;
1785 data->ioc_count = peer_refcount;
1787 rc = kgnilnd_get_conn_info(peer, &device_id, &peerstamp,
1788 &tx_seq, &rx_seq, &fmaq_len,
1791 /* This is allowable - a persistent peer could not
1792 * have a connection */
1794 /* flag to indicate we are not connected -
1795 * need to print as such */
1796 data->ioc_flags |= (1<<16);
1800 data->ioc_net = device_id;
1801 data->ioc_u64[0] = peerstamp;
1802 data->ioc_u32[0] = fmaq_len;
1803 data->ioc_u32[1] = nfma;
1804 data->ioc_u32[2] = tx_seq;
1805 data->ioc_u32[3] = rx_seq;
1806 data->ioc_u32[4] = nrdma;
1810 case IOC_LIBCFS_ADD_PEER: {
1811 /* just dummy value to allow using common interface */
1813 rc = kgnilnd_add_peer(net, data->ioc_nid, &peer);
1816 case IOC_LIBCFS_DEL_PEER: {
1817 /* NULL is passed in so it affects all peers in existence without regard to network
1818 * as the peer may not exist on the network LNET believes it to be on.
1820 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1821 GNILND_DEL_PEER, -EUCLEAN);
1824 case IOC_LIBCFS_GET_CONN: {
1825 kgn_conn_t *conn = kgnilnd_get_conn_by_idx(data->ioc_count);
1831 /* LNET_MKNID is used to build the correct address based on what LNET wants to see instead of
1832 * the generic connection that is used to send the data
1834 data->ioc_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), LNET_NIDADDR(conn->gnc_peer->gnp_nid));
1835 data->ioc_u32[0] = conn->gnc_device->gnd_id;
1836 kgnilnd_conn_decref(conn);
1840 case IOC_LIBCFS_CLOSE_CONNECTION: {
1841 /* use error = -ENETRESET to indicate it was lctl disconnect */
1842 /* NULL is passed in so it affects all the nets as the connection is virtual
1843 * and may not exist on the network LNET believes it to be on.
1845 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1846 GNILND_DEL_CONN, -ENETRESET);
1849 case IOC_LIBCFS_PUSH_CONNECTION: {
1850 /* we use this to flush purgatory */
1851 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1852 GNILND_CLEAR_PURGATORY, -EUCLEAN);
1855 case IOC_LIBCFS_REGISTER_MYNID: {
1856 /* Ignore if this is a noop */
1857 if (data->ioc_nid == ni->ni_nid) {
1860 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
1861 libcfs_nid2str(data->ioc_nid),
1862 libcfs_nid2str(ni->ni_nid));
1873 kgnilnd_query(lnet_ni_t *ni, lnet_nid_t nid, cfs_time_t *when)
1875 kgn_net_t *net = ni->ni_data;
1877 kgn_peer_t *peer = NULL;
1878 kgn_conn_t *conn = NULL;
1879 lnet_process_id_t id = {
1881 .pid = LNET_PID_LUSTRE,
1885 /* I expect to find him, so only take a read lock */
1886 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1887 peer = kgnilnd_find_peer_locked(nid);
1889 /* LIE if in a quiesce - we will update the timeouts after,
1890 * but we don't want sends failing during it */
1891 if (kgnilnd_data.kgn_quiesce_trigger) {
1893 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1897 /* Update to best guess, might refine on later checks */
1898 *when = peer->gnp_last_alive;
1900 /* we have a peer, how about a conn? */
1901 conn = kgnilnd_find_conn_locked(peer);
1904 /* if there is no conn, check peer last errno to see if clean disconnect
1905 * - if it was, we lie to LNet because we believe a TX would complete
1907 if (kgnilnd_conn_clean_errno(peer->gnp_last_errno)) {
1910 /* we still want to fire a TX and new conn in this case */
1912 /* gnp_last_alive is valid, run for the hills */
1913 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1917 /* if we get here, either we have no peer or no conn for him, so fire off
1918 * new TX to trigger conn setup */
1919 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1921 /* if we couldn't find him, we'll fire up a TX and get connected -
1922 * if we don't do this, after ni_peer_timeout, LNet will declare him dead.
1923 * So really we treat kgnilnd_query as a bit of a 'connect now' type
1924 * event because it'll only do this when it wants to send
1926 * Use a real TX for this to get the proper gnp_tx_queue behavior, etc
1927 * normally we'd use kgnilnd_send_ctlmsg for this, but we don't really
1928 * care that this goes out quickly since we already know we need a new conn
1930 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_NOOP_SEND))
1933 tx = kgnilnd_new_tx_msg(GNILND_MSG_NOOP, ni->ni_nid);
1935 kgnilnd_launch_tx(tx, net, &id);
1938 CDEBUG(D_NETTRACE, "peer 0x%p->%s when %lu\n", peer,
1939 libcfs_nid2str(nid), *when);
1944 kgnilnd_dev_init(kgn_device_t *dev)
1948 unsigned int cq_size;
1951 /* size of these CQs should be able to accommodate the outgoing
1952 * RDMA and SMSG transactions. Since we really don't know what we
1953 * really need here, we'll take credits * 2 * 3 to allow a bunch.
1954 * We need to dig into this more with the performance work. */
1955 cq_size = *kgnilnd_tunables.kgn_credits * 2 * 3;
1957 rrc = kgnilnd_cdm_create(dev->gnd_id, *kgnilnd_tunables.kgn_ptag,
1960 if (rrc != GNI_RC_SUCCESS) {
1961 CERROR("Can't create CDM %d (%d)\n", dev->gnd_id, rrc);
1962 GOTO(failed, rc = -ENODEV);
1965 rrc = kgnilnd_cdm_attach(dev->gnd_domain, dev->gnd_id,
1966 &dev->gnd_host_id, &dev->gnd_handle);
1967 if (rrc != GNI_RC_SUCCESS) {
1968 CERROR("Can't attach CDM to device %d (%d)\n",
1970 GOTO(failed, rc = -ENODEV);
1973 /* a bit gross, but not much we can do - Aries Sim doesn't have
1974 * hardcoded NIC/NID that we can use */
1975 rc = kgnilnd_setup_nic_translation(dev->gnd_host_id);
1977 GOTO(failed, rc = -ENODEV);
1979 /* only dev 0 gets the errors - no need to reset the stack twice
1980 * - this works because we have a single PTAG, if we had more
1981 * then we'd need to have multiple handlers */
1982 if (dev->gnd_id == 0) {
1983 rrc = kgnilnd_subscribe_errors(dev->gnd_handle,
1984 GNI_ERRMASK_CRITICAL |
1985 GNI_ERRMASK_UNKNOWN_TRANSACTION,
1986 0, NULL, kgnilnd_critical_error,
1987 &dev->gnd_err_handle);
1988 if (rrc != GNI_RC_SUCCESS) {
1989 CERROR("Can't subscribe for errors on device %d: rc %d\n",
1991 GOTO(failed, rc = -ENODEV);
1994 rc = kgnilnd_set_quiesce_callback(dev->gnd_handle,
1995 kgnilnd_quiesce_end_callback);
1996 if (rc != GNI_RC_SUCCESS) {
1997 CERROR("Can't subscribe for quiesce callback on device %d: rc %d\n",
1999 GOTO(failed, rc = -ENODEV);
2003 rrc = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_IP, &kgnilnd_data.kgn_sock);
2005 CERROR("sock_create returned %d\n", rrc);
2009 rc = kgnilnd_nicaddr_to_nid(dev->gnd_host_id, &dev->gnd_nid);
2011 /* log messages during startup */
2012 if (kgnilnd_data.kgn_init < GNILND_INIT_ALL) {
2013 CERROR("couldn't translate host_id 0x%x to nid. rc %d\n",
2014 dev->gnd_host_id, rc);
2016 GOTO(failed, rc = -ESRCH);
2018 CDEBUG(D_NET, "NIC %x -> NID %d\n", dev->gnd_host_id, dev->gnd_nid);
2020 rrc = kgnilnd_cq_create(dev->gnd_handle, *kgnilnd_tunables.kgn_credits,
2021 0, kgnilnd_device_callback,
2022 dev->gnd_id, &dev->gnd_snd_rdma_cqh);
2023 if (rrc != GNI_RC_SUCCESS) {
2024 CERROR("Can't create rdma send cq size %u for device "
2025 "%d (%d)\n", cq_size, dev->gnd_id, rrc);
2026 GOTO(failed, rc = -EINVAL);
2029 rrc = kgnilnd_cq_create(dev->gnd_handle, cq_size,
2030 0, kgnilnd_device_callback, dev->gnd_id,
2031 &dev->gnd_snd_fma_cqh);
2032 if (rrc != GNI_RC_SUCCESS) {
2033 CERROR("Can't create fma send cq size %u for device %d (%d)\n",
2034 cq_size, dev->gnd_id, rrc);
2035 GOTO(failed, rc = -EINVAL);
2038 /* This one we size differently - overflows are possible and it needs to be
2039 * sized based on machine size */
2040 rrc = kgnilnd_cq_create(dev->gnd_handle,
2041 *kgnilnd_tunables.kgn_fma_cq_size,
2042 0, kgnilnd_device_callback, dev->gnd_id,
2043 &dev->gnd_rcv_fma_cqh);
2044 if (rrc != GNI_RC_SUCCESS) {
2045 CERROR("Can't create fma cq size %d for device %d (%d)\n",
2046 *kgnilnd_tunables.kgn_fma_cq_size, dev->gnd_id, rrc);
2047 GOTO(failed, rc = -EINVAL);
2053 kgnilnd_dev_fini(dev);
2058 kgnilnd_dev_fini(kgn_device_t *dev)
2063 /* At quiesce or rest time, need to loop through and clear gnd_ready_conns ?*/
2064 LASSERTF(list_empty(&dev->gnd_ready_conns) &&
2065 list_empty(&dev->gnd_map_tx) &&
2066 list_empty(&dev->gnd_rdmaq),
2067 "dev 0x%p ready_conns %d@0x%p map_tx %d@0x%p rdmaq %d@0x%p\n",
2068 dev, kgnilnd_count_list(&dev->gnd_ready_conns), &dev->gnd_ready_conns,
2069 kgnilnd_count_list(&dev->gnd_map_tx), &dev->gnd_map_tx,
2070 kgnilnd_count_list(&dev->gnd_rdmaq), &dev->gnd_rdmaq);
2072 /* These should follow from tearing down all connections */
2073 LASSERTF(dev->gnd_map_nphys == 0 && dev->gnd_map_physnop == 0,
2074 "%d physical mappings of %d pages still mapped\n",
2075 dev->gnd_map_nphys, dev->gnd_map_physnop);
2077 LASSERTF(dev->gnd_map_nvirt == 0 && dev->gnd_map_virtnob == 0,
2078 "%d virtual mappings of "LPU64" bytes still mapped\n",
2079 dev->gnd_map_nvirt, dev->gnd_map_virtnob);
2081 LASSERTF(atomic_read(&dev->gnd_n_mdd) == 0 &&
2082 atomic_read(&dev->gnd_n_mdd_held) == 0 &&
2083 atomic64_read(&dev->gnd_nbytes_map) == 0,
2084 "%d SMSG mappings of %ld bytes still mapped or held %d\n",
2085 atomic_read(&dev->gnd_n_mdd),
2086 atomic64_read(&dev->gnd_nbytes_map), atomic_read(&dev->gnd_n_mdd_held));
2088 LASSERT(list_empty(&dev->gnd_map_list));
2090 /* What other assertions needed to ensure all connections torn down ? */
2092 /* check all counters == 0 (EP, MDD, etc) */
2094 /* if we are resetting due to quiese (stack reset), don't check
2096 LASSERTF(kgnilnd_data.kgn_quiesce_trigger ||
2097 atomic_read(&kgnilnd_data.kgn_nthreads) == 0,
2098 "tried to shutdown with threads active\n");
2100 if (dev->gnd_rcv_fma_cqh) {
2101 rrc = kgnilnd_cq_destroy(dev->gnd_rcv_fma_cqh);
2102 LASSERTF(rrc == GNI_RC_SUCCESS,
2103 "bad rc from gni_cq_destroy on rcv_fma_cqh: %d\n", rrc);
2104 dev->gnd_rcv_fma_cqh = NULL;
2107 if (dev->gnd_snd_rdma_cqh) {
2108 rrc = kgnilnd_cq_destroy(dev->gnd_snd_rdma_cqh);
2109 LASSERTF(rrc == GNI_RC_SUCCESS,
2110 "bad rc from gni_cq_destroy on send_rdma_cqh: %d\n", rrc);
2111 dev->gnd_snd_rdma_cqh = NULL;
2114 if (dev->gnd_snd_fma_cqh) {
2115 rrc = kgnilnd_cq_destroy(dev->gnd_snd_fma_cqh);
2116 LASSERTF(rrc == GNI_RC_SUCCESS,
2117 "bad rc from gni_cq_destroy on snd_fma_cqh: %d\n", rrc);
2118 dev->gnd_snd_fma_cqh = NULL;
2121 if (dev->gnd_err_handle) {
2122 rrc = kgnilnd_release_errors(dev->gnd_err_handle);
2123 LASSERTF(rrc == GNI_RC_SUCCESS,
2124 "bad rc from gni_release_errors: %d\n", rrc);
2125 dev->gnd_err_handle = NULL;
2128 if (dev->gnd_domain) {
2129 rrc = kgnilnd_cdm_destroy(dev->gnd_domain);
2130 LASSERTF(rrc == GNI_RC_SUCCESS,
2131 "bad rc from gni_cdm_destroy: %d\n", rrc);
2132 dev->gnd_domain = NULL;
2135 if (kgnilnd_data.kgn_sock)
2136 sock_release(kgnilnd_data.kgn_sock);
2141 int kgnilnd_base_startup(void)
2144 int pkmem = atomic_read(&libcfs_kmemory);
2148 struct task_struct *thrd;
2150 #if defined(CONFIG_CRAY_XT) && !defined(CONFIG_CRAY_COMPUTE)
2151 /* limit how much memory can be allocated for fma blocks in
2152 * instances where many nodes need to reconnects at the same time */
2155 kgnilnd_data.free_pages_limit = si.totalram/4;
2160 LASSERTF(kgnilnd_data.kgn_init == GNILND_INIT_NOTHING,
2161 "init %d\n", kgnilnd_data.kgn_init);
2163 /* zero pointers, flags etc */
2164 memset(&kgnilnd_data, 0, sizeof(kgnilnd_data));
2165 kgnilnd_check_kgni_version();
2167 /* CAVEAT EMPTOR: Every 'Fma' message includes the sender's NID and
2168 * a unique (for all time) connstamp so we can uniquely identify
2169 * the sender. The connstamp is an incrementing counter
2170 * initialised with seconds + microseconds at startup time. So we
2171 * rely on NOT creating connections more frequently on average than
2172 * 1MHz to ensure we don't use old connstamps when we reboot. */
2173 do_gettimeofday(&tv);
2174 kgnilnd_data.kgn_connstamp =
2175 kgnilnd_data.kgn_peerstamp =
2176 (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
2178 init_rwsem(&kgnilnd_data.kgn_net_rw_sem);
2180 for (i = 0; i < GNILND_MAXDEVS; i++) {
2181 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2184 INIT_LIST_HEAD(&dev->gnd_ready_conns);
2185 INIT_LIST_HEAD(&dev->gnd_map_tx);
2186 INIT_LIST_HEAD(&dev->gnd_fma_buffs);
2187 mutex_init(&dev->gnd_cq_mutex);
2188 mutex_init(&dev->gnd_fmablk_mutex);
2189 spin_lock_init(&dev->gnd_fmablk_lock);
2190 init_waitqueue_head(&dev->gnd_waitq);
2191 init_waitqueue_head(&dev->gnd_dgram_waitq);
2192 init_waitqueue_head(&dev->gnd_dgping_waitq);
2193 spin_lock_init(&dev->gnd_lock);
2194 INIT_LIST_HEAD(&dev->gnd_map_list);
2195 spin_lock_init(&dev->gnd_map_lock);
2196 atomic_set(&dev->gnd_nfmablk, 0);
2197 atomic_set(&dev->gnd_fmablk_vers, 1);
2198 atomic_set(&dev->gnd_neps, 0);
2199 atomic_set(&dev->gnd_canceled_dgrams, 0);
2200 INIT_LIST_HEAD(&dev->gnd_connd_peers);
2201 spin_lock_init(&dev->gnd_connd_lock);
2202 spin_lock_init(&dev->gnd_dgram_lock);
2203 spin_lock_init(&dev->gnd_rdmaq_lock);
2204 INIT_LIST_HEAD(&dev->gnd_rdmaq);
2205 init_rwsem(&dev->gnd_conn_sem);
2207 /* alloc & setup nid based dgram table */
2208 LIBCFS_ALLOC(dev->gnd_dgrams,
2209 sizeof(struct list_head) * *kgnilnd_tunables.kgn_peer_hash_size);
2211 if (dev->gnd_dgrams == NULL)
2212 GOTO(failed, rc = -ENOMEM);
2214 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2215 INIT_LIST_HEAD(&dev->gnd_dgrams[i]);
2217 atomic_set(&dev->gnd_ndgrams, 0);
2218 atomic_set(&dev->gnd_nwcdgrams, 0);
2219 /* setup timer for RDMAQ processing */
2220 setup_timer(&dev->gnd_rdmaq_timer, kgnilnd_schedule_device_timer,
2221 (unsigned long)dev);
2223 /* setup timer for mapping processing */
2224 setup_timer(&dev->gnd_map_timer, kgnilnd_schedule_device_timer,
2225 (unsigned long)dev);
2229 /* CQID 0 isn't allowed, set to MAX_MSG_ID - 1 to check for conflicts early */
2230 kgnilnd_data.kgn_next_cqid = GNILND_MAX_MSG_ID - 1;
2231 kgnilnd_data.kgn_new_min_timeout = *kgnilnd_tunables.kgn_timeout;
2232 init_waitqueue_head(&kgnilnd_data.kgn_reaper_waitq);
2233 init_waitqueue_head(&kgnilnd_data.kgn_ruhroh_waitq);
2234 spin_lock_init(&kgnilnd_data.kgn_reaper_lock);
2236 mutex_init(&kgnilnd_data.kgn_quiesce_mutex);
2237 atomic_set(&kgnilnd_data.kgn_nquiesce, 0);
2238 atomic_set(&kgnilnd_data.kgn_npending_conns, 0);
2239 atomic_set(&kgnilnd_data.kgn_npending_unlink, 0);
2240 atomic_set(&kgnilnd_data.kgn_npending_detach, 0);
2241 atomic_set(&kgnilnd_data.kgn_rev_offset, 0);
2242 atomic_set(&kgnilnd_data.kgn_rev_length, 0);
2243 atomic_set(&kgnilnd_data.kgn_rev_copy_buff, 0);
2245 /* OK to call kgnilnd_api_shutdown() to cleanup now */
2246 kgnilnd_data.kgn_init = GNILND_INIT_DATA;
2247 try_module_get(THIS_MODULE);
2249 rwlock_init(&kgnilnd_data.kgn_peer_conn_lock);
2251 LIBCFS_ALLOC(kgnilnd_data.kgn_peers,
2252 sizeof(struct list_head) * *kgnilnd_tunables.kgn_peer_hash_size);
2254 if (kgnilnd_data.kgn_peers == NULL)
2255 GOTO(failed, rc = -ENOMEM);
2257 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2258 INIT_LIST_HEAD(&kgnilnd_data.kgn_peers[i]);
2261 LIBCFS_ALLOC(kgnilnd_data.kgn_conns,
2262 sizeof(struct list_head) * *kgnilnd_tunables.kgn_peer_hash_size);
2264 if (kgnilnd_data.kgn_conns == NULL)
2265 GOTO(failed, rc = -ENOMEM);
2267 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2268 INIT_LIST_HEAD(&kgnilnd_data.kgn_conns[i]);
2271 LIBCFS_ALLOC(kgnilnd_data.kgn_nets,
2272 sizeof(struct list_head) * *kgnilnd_tunables.kgn_net_hash_size);
2274 if (kgnilnd_data.kgn_nets == NULL)
2275 GOTO(failed, rc = -ENOMEM);
2277 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
2278 INIT_LIST_HEAD(&kgnilnd_data.kgn_nets[i]);
2281 kgnilnd_data.kgn_mbox_cache =
2282 kmem_cache_create("kgn_mbox_block", KMALLOC_MAX_SIZE, 0,
2283 SLAB_HWCACHE_ALIGN, NULL);
2284 if (kgnilnd_data.kgn_mbox_cache == NULL) {
2285 CERROR("Can't create slab for physical mbox blocks\n");
2286 GOTO(failed, rc = -ENOMEM);
2289 kgnilnd_data.kgn_rx_cache =
2290 kmem_cache_create("kgn_rx_t", sizeof(kgn_rx_t), 0, 0, NULL);
2291 if (kgnilnd_data.kgn_rx_cache == NULL) {
2292 CERROR("Can't create slab for kgn_rx_t descriptors\n");
2293 GOTO(failed, rc = -ENOMEM);
2296 kgnilnd_data.kgn_tx_cache =
2297 kmem_cache_create("kgn_tx_t", sizeof(kgn_tx_t), 0, 0, NULL);
2298 if (kgnilnd_data.kgn_tx_cache == NULL) {
2299 CERROR("Can't create slab for kgn_tx_t\n");
2300 GOTO(failed, rc = -ENOMEM);
2303 kgnilnd_data.kgn_tx_phys_cache =
2304 kmem_cache_create("kgn_tx_phys",
2305 LNET_MAX_IOV * sizeof(gni_mem_segment_t),
2307 if (kgnilnd_data.kgn_tx_phys_cache == NULL) {
2308 CERROR("Can't create slab for kgn_tx_phys\n");
2309 GOTO(failed, rc = -ENOMEM);
2312 kgnilnd_data.kgn_dgram_cache =
2313 kmem_cache_create("kgn_dgram_t", sizeof(kgn_dgram_t), 0, 0, NULL);
2314 if (kgnilnd_data.kgn_dgram_cache == NULL) {
2315 CERROR("Can't create slab for outgoing datagrams\n");
2316 GOTO(failed, rc = -ENOMEM);
2319 /* allocate a MAX_IOV array of page pointers for each cpu */
2320 kgnilnd_data.kgn_cksum_map_pages = kmalloc(num_possible_cpus() * sizeof (struct page *),
2322 if (kgnilnd_data.kgn_cksum_map_pages == NULL) {
2323 CERROR("Can't allocate vmap cksum pages\n");
2324 GOTO(failed, rc = -ENOMEM);
2326 kgnilnd_data.kgn_cksum_npages = num_possible_cpus();
2327 memset(kgnilnd_data.kgn_cksum_map_pages, 0,
2328 kgnilnd_data.kgn_cksum_npages * sizeof (struct page *));
2330 for (i = 0; i < kgnilnd_data.kgn_cksum_npages; i++) {
2331 kgnilnd_data.kgn_cksum_map_pages[i] = kmalloc(LNET_MAX_IOV * sizeof (struct page *),
2333 if (kgnilnd_data.kgn_cksum_map_pages[i] == NULL) {
2334 CERROR("Can't allocate vmap cksum pages for cpu %d\n", i);
2335 GOTO(failed, rc = -ENOMEM);
2339 LASSERT(kgnilnd_data.kgn_ndevs == 0);
2341 /* Use all available GNI devices */
2342 for (i = 0; i < GNILND_MAXDEVS; i++) {
2343 dev = &kgnilnd_data.kgn_devices[kgnilnd_data.kgn_ndevs];
2345 rc = kgnilnd_dev_init(dev);
2347 /* Increment here so base_shutdown cleans it up */
2348 kgnilnd_data.kgn_ndevs++;
2350 rc = kgnilnd_allocate_phys_fmablk(dev);
2356 if (kgnilnd_data.kgn_ndevs == 0) {
2357 CERROR("Can't initialise any GNI devices\n");
2358 GOTO(failed, rc = -ENODEV);
2361 rc = kgnilnd_thread_start(kgnilnd_reaper, NULL, "kgnilnd_rpr", 0);
2363 CERROR("Can't spawn gnilnd reaper: %d\n", rc);
2367 rc = kgnilnd_start_rca_thread();
2369 CERROR("Can't spawn gnilnd rca: %d\n", rc);
2374 * Start ruhroh thread. We can't use kgnilnd_thread_start() because
2375 * we don't want this thread included in kgnilnd_data.kgn_nthreads
2376 * count. This thread controls quiesce, so it mustn't
2379 thrd = kthread_run(kgnilnd_ruhroh_thread, NULL, "%s_%02d", "kgnilnd_rr", 0);
2382 CERROR("Can't spawn gnilnd ruhroh thread: %d\n", rc);
2386 /* threads will load balance across devs as they are available */
2387 for (i = 0; i < *kgnilnd_tunables.kgn_sched_threads; i++) {
2388 rc = kgnilnd_thread_start(kgnilnd_scheduler, (void *)((long)i),
2391 CERROR("Can't spawn gnilnd scheduler[%d]: %d\n",
2397 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2398 dev = &kgnilnd_data.kgn_devices[i];
2399 rc = kgnilnd_thread_start(kgnilnd_dgram_mover, dev,
2400 "kgnilnd_dg", dev->gnd_id);
2402 CERROR("Can't spawn gnilnd dgram_mover[%d]: %d\n",
2407 rc = kgnilnd_thread_start(kgnilnd_dgram_waitq, dev,
2408 "kgnilnd_dgn", dev->gnd_id);
2410 CERROR("Can't spawn gnilnd dgram_waitq[%d]: %d\n",
2415 rc = kgnilnd_setup_wildcard_dgram(dev);
2418 CERROR("Can't create wildcard dgrams[%d]: %d\n",
2426 /* flag everything initialised */
2427 kgnilnd_data.kgn_init = GNILND_INIT_ALL;
2428 /*****************************************************/
2430 CDEBUG(D_MALLOC, "initial kmem %d\n", pkmem);
2434 kgnilnd_base_shutdown();
2435 kgnilnd_data.kgn_init = GNILND_INIT_NOTHING;
2440 kgnilnd_base_shutdown(void)
2445 while (CFS_FAIL_TIMEOUT(CFS_FAIL_GNI_PAUSE_SHUTDOWN, 1)) {};
2447 kgnilnd_data.kgn_wc_kill = 1;
2449 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2450 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2451 kgnilnd_cancel_wc_dgrams(dev);
2452 kgnilnd_cancel_dgrams(dev);
2453 kgnilnd_del_conn_or_peer(NULL, LNET_NID_ANY, GNILND_DEL_PEER, -ESHUTDOWN);
2454 kgnilnd_wait_for_canceled_dgrams(dev);
2457 /* We need to verify there are no conns left before we let the threads
2458 * shut down otherwise we could clean up the peers but still have
2459 * some outstanding conns due to orphaned datagram conns that are
2463 while (atomic_read(&kgnilnd_data.kgn_nconns) != 0) {
2466 for(j = 0; j < kgnilnd_data.kgn_ndevs; ++j) {
2467 kgn_device_t *dev = &kgnilnd_data.kgn_devices[j];
2468 kgnilnd_schedule_device(dev);
2471 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2472 "Waiting for conns to be cleaned up %d\n",atomic_read(&kgnilnd_data.kgn_nconns));
2473 set_current_state(TASK_UNINTERRUPTIBLE);
2474 schedule_timeout(cfs_time_seconds(1));
2476 /* Peer state all cleaned up BEFORE setting shutdown, so threads don't
2477 * have to worry about shutdown races. NB connections may be created
2478 * while there are still active connds, but these will be temporary
2479 * since peer creation always fails after the listener has started to
2481 * all peers should have been cleared out on the nets */
2482 LASSERTF(atomic_read(&kgnilnd_data.kgn_npeers) == 0,
2483 "peers left %d\n", atomic_read(&kgnilnd_data.kgn_npeers));
2485 /* Wait for the ruhroh thread to shut down. */
2486 kgnilnd_data.kgn_ruhroh_shutdown = 1;
2487 wake_up(&kgnilnd_data.kgn_ruhroh_waitq);
2489 while (kgnilnd_data.kgn_ruhroh_running != 0) {
2491 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2492 "Waiting for ruhroh thread to terminate\n");
2493 set_current_state(TASK_UNINTERRUPTIBLE);
2494 schedule_timeout(cfs_time_seconds(1));
2497 /* Flag threads to terminate */
2498 kgnilnd_data.kgn_shutdown = 1;
2500 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2501 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2503 /* should clear all the MDDs */
2504 kgnilnd_unmap_fma_blocks(dev);
2506 kgnilnd_schedule_device(dev);
2507 wake_up_all(&dev->gnd_dgram_waitq);
2508 wake_up_all(&dev->gnd_dgping_waitq);
2509 LASSERT(list_empty(&dev->gnd_connd_peers));
2512 spin_lock(&kgnilnd_data.kgn_reaper_lock);
2513 wake_up_all(&kgnilnd_data.kgn_reaper_waitq);
2514 spin_unlock(&kgnilnd_data.kgn_reaper_lock);
2516 if (atomic_read(&kgnilnd_data.kgn_nthreads))
2517 kgnilnd_wakeup_rca_thread();
2519 /* Wait for threads to exit */
2521 while (atomic_read(&kgnilnd_data.kgn_nthreads) != 0) {
2523 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2524 "Waiting for %d threads to terminate\n",
2525 atomic_read(&kgnilnd_data.kgn_nthreads));
2526 set_current_state(TASK_UNINTERRUPTIBLE);
2527 schedule_timeout(cfs_time_seconds(1));
2530 LASSERTF(atomic_read(&kgnilnd_data.kgn_npeers) == 0,
2531 "peers left %d\n", atomic_read(&kgnilnd_data.kgn_npeers));
2533 if (kgnilnd_data.kgn_peers != NULL) {
2534 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
2535 LASSERT(list_empty(&kgnilnd_data.kgn_peers[i]));
2537 LIBCFS_FREE(kgnilnd_data.kgn_peers,
2538 sizeof (struct list_head) *
2539 *kgnilnd_tunables.kgn_peer_hash_size);
2542 down_write(&kgnilnd_data.kgn_net_rw_sem);
2543 if (kgnilnd_data.kgn_nets != NULL) {
2544 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++)
2545 LASSERT(list_empty(&kgnilnd_data.kgn_nets[i]));
2547 LIBCFS_FREE(kgnilnd_data.kgn_nets,
2548 sizeof (struct list_head) *
2549 *kgnilnd_tunables.kgn_net_hash_size);
2551 up_write(&kgnilnd_data.kgn_net_rw_sem);
2553 LASSERTF(atomic_read(&kgnilnd_data.kgn_nconns) == 0,
2554 "conns left %d\n", atomic_read(&kgnilnd_data.kgn_nconns));
2556 if (kgnilnd_data.kgn_conns != NULL) {
2557 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
2558 LASSERT(list_empty(&kgnilnd_data.kgn_conns[i]));
2560 LIBCFS_FREE(kgnilnd_data.kgn_conns,
2561 sizeof (struct list_head) *
2562 *kgnilnd_tunables.kgn_peer_hash_size);
2565 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2566 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2567 kgnilnd_dev_fini(dev);
2569 LASSERTF(atomic_read(&dev->gnd_ndgrams) == 0,
2570 "dgrams left %d\n", atomic_read(&dev->gnd_ndgrams));
2572 if (dev->gnd_dgrams != NULL) {
2573 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
2574 LASSERT(list_empty(&dev->gnd_dgrams[i]));
2576 LIBCFS_FREE(dev->gnd_dgrams,
2577 sizeof (struct list_head) *
2578 *kgnilnd_tunables.kgn_peer_hash_size);
2581 kgnilnd_free_phys_fmablk(dev);
2584 if (kgnilnd_data.kgn_mbox_cache != NULL)
2585 kmem_cache_destroy(kgnilnd_data.kgn_mbox_cache);
2587 if (kgnilnd_data.kgn_rx_cache != NULL)
2588 kmem_cache_destroy(kgnilnd_data.kgn_rx_cache);
2590 if (kgnilnd_data.kgn_tx_cache != NULL)
2591 kmem_cache_destroy(kgnilnd_data.kgn_tx_cache);
2593 if (kgnilnd_data.kgn_tx_phys_cache != NULL)
2594 kmem_cache_destroy(kgnilnd_data.kgn_tx_phys_cache);
2596 if (kgnilnd_data.kgn_dgram_cache != NULL)
2597 kmem_cache_destroy(kgnilnd_data.kgn_dgram_cache);
2599 if (kgnilnd_data.kgn_cksum_map_pages != NULL) {
2600 for (i = 0; i < kgnilnd_data.kgn_cksum_npages; i++) {
2601 if (kgnilnd_data.kgn_cksum_map_pages[i] != NULL) {
2602 kfree(kgnilnd_data.kgn_cksum_map_pages[i]);
2605 kfree(kgnilnd_data.kgn_cksum_map_pages);
2608 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2609 atomic_read(&libcfs_kmemory));
2611 kgnilnd_data.kgn_init = GNILND_INIT_NOTHING;
2612 module_put(THIS_MODULE);
2618 kgnilnd_startup(lnet_ni_t *ni)
2624 LASSERTF(ni->ni_lnd == &the_kgnilnd,
2625 "bad LND 0x%p != the_kgnilnd @ 0x%p\n",
2626 ni->ni_lnd, &the_kgnilnd);
2628 if (kgnilnd_data.kgn_init == GNILND_INIT_NOTHING) {
2629 rc = kgnilnd_base_startup();
2634 /* Serialize with shutdown. */
2635 mutex_lock(&kgnilnd_data.kgn_quiesce_mutex);
2637 LIBCFS_ALLOC(net, sizeof(*net));
2639 CERROR("could not allocate net for new interface instance\n");
2640 /* no need to cleanup the CDM... */
2641 GOTO(failed, rc = -ENOMEM);
2643 INIT_LIST_HEAD(&net->gnn_list);
2646 ni->ni_maxtxcredits = *kgnilnd_tunables.kgn_credits;
2647 ni->ni_peertxcredits = *kgnilnd_tunables.kgn_peer_credits;
2649 if (*kgnilnd_tunables.kgn_peer_health) {
2652 /* give this a bit of leeway - we don't have a hard timeout
2653 * as we only check timeouts periodically - see comment in kgnilnd_reaper */
2654 fudge = (GNILND_TO2KA(*kgnilnd_tunables.kgn_timeout) / GNILND_REAPER_NCHECKS);
2655 timeout = *kgnilnd_tunables.kgn_timeout + fudge;
2657 if (*kgnilnd_tunables.kgn_peer_timeout >= timeout)
2658 ni->ni_peertimeout = *kgnilnd_tunables.kgn_peer_timeout;
2659 else if (*kgnilnd_tunables.kgn_peer_timeout > -1) {
2660 LCONSOLE_ERROR("Peer_timeout is set to %d but needs to be >= %d\n",
2661 *kgnilnd_tunables.kgn_peer_timeout,
2664 LIBCFS_FREE(net, sizeof(*net));
2665 GOTO(failed, rc = -EINVAL);
2667 ni->ni_peertimeout = timeout;
2669 LCONSOLE_INFO("Enabling LNet peer health for gnilnd, timeout %ds\n",
2670 ni->ni_peertimeout);
2673 atomic_set(&net->gnn_refcount, 1);
2675 /* if we have multiple devices, spread the nets around */
2676 net->gnn_netnum = LNET_NETNUM(LNET_NIDNET(ni->ni_nid));
2678 devno = LNET_NIDNET(ni->ni_nid) % GNILND_MAXDEVS;
2679 net->gnn_dev = &kgnilnd_data.kgn_devices[devno];
2681 /* allocate a 'dummy' cdm for datagram use. We can only have a single
2682 * datagram between a nid:inst_id and nid2:inst_id. The fake cdm
2683 * give us additional inst_id to use, allowing the datagrams to flow
2684 * like rivers of honey and beer */
2686 /* the instance id for the cdm is the NETNUM offset by MAXDEVS -
2687 * ensuring we'll have a unique id */
2690 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), net->gnn_dev->gnd_nid);
2691 CDEBUG(D_NET, "adding net %p nid=%s on dev %d \n",
2692 net, libcfs_nid2str(ni->ni_nid), net->gnn_dev->gnd_id);
2693 /* until the gnn_list is set, we need to cleanup ourselves as
2694 * kgnilnd_shutdown is just gonna get confused */
2696 down_write(&kgnilnd_data.kgn_net_rw_sem);
2697 list_add_tail(&net->gnn_list, kgnilnd_netnum2netlist(net->gnn_netnum));
2698 up_write(&kgnilnd_data.kgn_net_rw_sem);
2700 /* we need a separate thread to call probe_wait_by_id until
2701 * we get a function callback notifier from kgni */
2702 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2705 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2706 kgnilnd_shutdown(ni);
2711 kgnilnd_shutdown(lnet_ni_t *ni)
2713 kgn_net_t *net = ni->ni_data;
2718 CFS_RACE(CFS_FAIL_GNI_SR_DOWN_RACE);
2720 LASSERTF(kgnilnd_data.kgn_init == GNILND_INIT_ALL,
2721 "init %d\n", kgnilnd_data.kgn_init);
2723 /* Serialize with startup. */
2724 mutex_lock(&kgnilnd_data.kgn_quiesce_mutex);
2725 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2726 atomic_read(&libcfs_kmemory));
2729 CERROR("got NULL net for ni %p\n", ni);
2730 GOTO(out, rc = -EINVAL);
2733 LASSERTF(ni == net->gnn_ni,
2734 "ni %p gnn_ni %p\n", net, net->gnn_ni);
2738 LASSERT(!net->gnn_shutdown);
2739 LASSERTF(atomic_read(&net->gnn_refcount) != 0,
2740 "net %p refcount %d\n",
2741 net, atomic_read(&net->gnn_refcount));
2743 if (!list_empty(&net->gnn_list)) {
2744 /* serialize with peer creation */
2745 down_write(&kgnilnd_data.kgn_net_rw_sem);
2746 net->gnn_shutdown = 1;
2747 up_write(&kgnilnd_data.kgn_net_rw_sem);
2749 kgnilnd_cancel_net_dgrams(net);
2751 kgnilnd_del_conn_or_peer(net, LNET_NID_ANY, GNILND_DEL_PEER, -ESHUTDOWN);
2753 /* if we are quiesced, need to wake up - we need those threads
2754 * alive to release peers, etc */
2755 if (GNILND_IS_QUIESCED) {
2756 set_mb(kgnilnd_data.kgn_quiesce_trigger, GNILND_QUIESCE_IDLE);
2757 kgnilnd_quiesce_wait("shutdown");
2760 kgnilnd_wait_for_canceled_dgrams(net->gnn_dev);
2762 /* We wait until the nets ref's are 1, we will release final ref which is ours
2763 * this allows us to make sure everything else is done before we free the
2767 while (atomic_read(&net->gnn_refcount) != 1) {
2769 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2770 "Waiting for %d references to clear on net %d\n",
2771 atomic_read(&net->gnn_refcount),
2773 set_current_state(TASK_UNINTERRUPTIBLE);
2774 schedule_timeout(cfs_time_seconds(1));
2777 /* release ref from kgnilnd_startup */
2778 kgnilnd_net_decref(net);
2779 /* serialize with reaper and conn_task looping */
2780 down_write(&kgnilnd_data.kgn_net_rw_sem);
2781 list_del_init(&net->gnn_list);
2782 up_write(&kgnilnd_data.kgn_net_rw_sem);
2786 /* not locking, this can't race with writers */
2787 LASSERTF(atomic_read(&net->gnn_refcount) == 0,
2788 "net %p refcount %d\n",
2789 net, atomic_read(&net->gnn_refcount));
2790 LIBCFS_FREE(net, sizeof(*net));
2793 down_read(&kgnilnd_data.kgn_net_rw_sem);
2794 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
2795 if (!list_empty(&kgnilnd_data.kgn_nets[i])) {
2796 up_read(&kgnilnd_data.kgn_net_rw_sem);
2800 if (i == *kgnilnd_tunables.kgn_net_hash_size - 1) {
2801 up_read(&kgnilnd_data.kgn_net_rw_sem);
2802 kgnilnd_base_shutdown();
2805 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2806 atomic_read(&libcfs_kmemory));
2808 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2813 kgnilnd_module_fini(void)
2815 lnet_unregister_lnd(&the_kgnilnd);
2816 kgnilnd_proc_fini();
2817 kgnilnd_remove_sysctl();
2818 kgnilnd_tunables_fini();
2822 kgnilnd_module_init(void)
2826 rc = kgnilnd_tunables_init();
2830 printk(KERN_INFO "Lustre: kgnilnd build version: "KGNILND_BUILD_REV"\n");
2832 kgnilnd_insert_sysctl();
2833 kgnilnd_proc_init();
2835 lnet_register_lnd(&the_kgnilnd);
2840 MODULE_AUTHOR("Cray, Inc. <nic@cray.com>");
2841 MODULE_DESCRIPTION("Kernel Gemini LND v"KGNILND_BUILD_REV);
2842 MODULE_LICENSE("GPL");
2844 module_init(kgnilnd_module_init);
2845 module_exit(kgnilnd_module_fini);