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.
28 kgnilnd_nl_get(int cmd, struct sk_buff *msg, int type, void *data)
30 struct lnet_ni *ni = data;
35 if (cmd != LNET_CMD_NETS || type != LNET_NET_LOCAL_NI_ATTR_LND_TUNABLES)
38 nla_put_u32(msg, LNET_NET_GNILND_TUNABLES_ATTR_LND_TIMEOUT,
44 kgnilnd_nl_set(int cmd, struct nlattr *attr, int type, void *data)
46 struct lnet_ni *ni = data;
48 if (cmd != LNET_CMD_NETS)
54 if (nla_type(attr) != LN_SCALAR_ATTR_INT_VALUE)
57 if (type == LNET_NET_GNILND_TUNABLES_ATTR_LND_TIMEOUT) {
58 s64 timeout = nla_get_s64(attr);
60 ni->ni_lnd_tunables.lnd_tun_u.lnd_gni.lnd_timeout = timeout;
66 /* Primary entry points from LNET. There are no guarantees against reentrance. */
67 const struct lnet_lnd the_kgnilnd = {
69 .lnd_startup = kgnilnd_startup,
70 .lnd_shutdown = kgnilnd_shutdown,
71 .lnd_ctl = kgnilnd_ctl,
72 .lnd_send = kgnilnd_send,
73 .lnd_recv = kgnilnd_recv,
74 .lnd_eager_recv = kgnilnd_eager_recv,
75 .lnd_nl_get = kgnilnd_nl_get,
76 .lnd_nl_set = kgnilnd_nl_set,
79 kgn_data_t kgnilnd_data;
82 kgnilnd_thread_start(int(*fn)(void *arg), void *arg, char *name, int id)
84 struct task_struct *thrd;
86 thrd = kthread_run(fn, arg, "%s_%02d", name, id);
90 atomic_inc(&kgnilnd_data.kgn_nthreads);
94 /* bind scheduler threads to cpus */
96 kgnilnd_start_sd_threads(void)
100 struct task_struct *task;
102 for_each_online_cpu(cpu) {
103 /* don't bind to cpu 0 - all interrupts are processed here */
107 task = kthread_create(kgnilnd_scheduler, (void *)((long)i),
108 "%s_%02d", "kgnilnd_sd", i);
110 kthread_bind(task, cpu);
111 wake_up_process(task);
113 CERROR("Can't spawn gnilnd scheduler[%d] %ld\n", i,
115 return PTR_ERR(task);
117 atomic_inc(&kgnilnd_data.kgn_nthreads);
119 if (++i >= *kgnilnd_tunables.kgn_sched_threads) {
127 /* needs write_lock on kgn_peer_conn_lock */
129 kgnilnd_close_stale_conns_locked(kgn_peer_t *peer, kgn_conn_t *newconn)
131 kgn_conn_t *conn, *cnxt;
135 loopback = (peer->gnp_nid ==
136 lnet_nid_to_nid4(&peer->gnp_net->gnn_ni->ni_nid));
138 list_for_each_entry_safe(conn, cnxt, &peer->gnp_conns, gnc_list) {
139 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
145 if (conn->gnc_device != newconn->gnc_device)
148 /* This is a two connection loopback - one talking to the other */
150 newconn->gnc_my_connstamp == conn->gnc_peer_connstamp &&
151 newconn->gnc_peer_connstamp == conn->gnc_my_connstamp) {
152 CDEBUG(D_NET, "skipping prune of %p, "
153 "loopback and matching stamps"
154 " connstamp %llu(%llu)"
155 " peerstamp %llu(%llu)\n",
156 conn, newconn->gnc_my_connstamp,
157 conn->gnc_peer_connstamp,
158 newconn->gnc_peer_connstamp,
159 conn->gnc_my_connstamp);
163 if (conn->gnc_peerstamp != newconn->gnc_peerstamp) {
164 LASSERTF(conn->gnc_peerstamp < newconn->gnc_peerstamp,
165 "conn 0x%p peerstamp %llu >= "
166 "newconn 0x%p peerstamp %llu\n",
167 conn, conn->gnc_peerstamp,
168 newconn, newconn->gnc_peerstamp);
170 CDEBUG(D_NET, "Closing stale conn nid: %s "
171 " peerstamp:%#llx(%#llx)\n",
172 libcfs_nid2str(peer->gnp_nid),
173 conn->gnc_peerstamp, newconn->gnc_peerstamp);
176 LASSERTF(conn->gnc_peer_connstamp < newconn->gnc_peer_connstamp,
177 "conn 0x%p peer_connstamp %llu >= "
178 "newconn 0x%p peer_connstamp %llu\n",
179 conn, conn->gnc_peer_connstamp,
180 newconn, newconn->gnc_peer_connstamp);
182 CDEBUG(D_NET, "Closing stale conn nid: %s"
183 " connstamp:%llu(%llu)\n",
184 libcfs_nid2str(peer->gnp_nid),
185 conn->gnc_peer_connstamp, newconn->gnc_peer_connstamp);
189 kgnilnd_close_conn_locked(conn, -ESTALE);
193 CWARN("Closed %d stale conns to %s\n", count, libcfs_nid2str(peer->gnp_nid));
200 kgnilnd_conn_isdup_locked(kgn_peer_t *peer, kgn_conn_t *newconn)
206 loopback = (peer->gnp_nid ==
207 lnet_nid_to_nid4(&peer->gnp_net->gnn_ni->ni_nid));
209 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
210 CDEBUG(D_NET, "checking conn 0x%p for peer %s"
211 " lo %d new %llu existing %llu"
212 " new peer %llu existing peer %llu"
213 " new dev %p existing dev %p\n",
214 conn, libcfs_nid2str(peer->gnp_nid),
216 newconn->gnc_peerstamp, conn->gnc_peerstamp,
217 newconn->gnc_peer_connstamp, conn->gnc_peer_connstamp,
218 newconn->gnc_device, conn->gnc_device);
220 /* conn is in the process of closing */
221 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
224 /* 'newconn' is from an earlier version of 'peer'!!! */
225 if (newconn->gnc_peerstamp < conn->gnc_peerstamp)
228 /* 'conn' is from an earlier version of 'peer': it will be
229 * removed when we cull stale conns later on... */
230 if (newconn->gnc_peerstamp > conn->gnc_peerstamp)
233 /* Different devices are OK */
234 if (conn->gnc_device != newconn->gnc_device)
237 /* It's me connecting to myself */
239 newconn->gnc_my_connstamp == conn->gnc_peer_connstamp &&
240 newconn->gnc_peer_connstamp == conn->gnc_my_connstamp)
243 /* 'newconn' is an earlier connection from 'peer'!!! */
244 if (newconn->gnc_peer_connstamp < conn->gnc_peer_connstamp)
247 /* 'conn' is an earlier connection from 'peer': it will be
248 * removed when we cull stale conns later on... */
249 if (newconn->gnc_peer_connstamp > conn->gnc_peer_connstamp)
252 /* 'newconn' has the SAME connection stamp; 'peer' isn't
253 * playing the game... */
261 kgnilnd_create_conn(kgn_conn_t **connp, kgn_device_t *dev)
267 LASSERT (!in_interrupt());
268 atomic_inc(&kgnilnd_data.kgn_nconns);
270 /* divide by 2 to allow for complete reset and immediate reconnect */
271 if (atomic_read(&kgnilnd_data.kgn_nconns) >= GNILND_MAX_CQID/2) {
272 CERROR("Too many conn are live: %d > %d\n",
273 atomic_read(&kgnilnd_data.kgn_nconns), GNILND_MAX_CQID/2);
274 atomic_dec(&kgnilnd_data.kgn_nconns);
278 LIBCFS_ALLOC(conn, sizeof(*conn));
280 atomic_dec(&kgnilnd_data.kgn_nconns);
284 conn->gnc_tx_ref_table =
285 kgnilnd_vzalloc(GNILND_MAX_MSG_ID * sizeof(void *));
286 if (conn->gnc_tx_ref_table == NULL) {
287 CERROR("Can't allocate conn tx_ref_table\n");
288 GOTO(failed, rc = -ENOMEM);
291 mutex_init(&conn->gnc_smsg_mutex);
292 mutex_init(&conn->gnc_rdma_mutex);
293 atomic_set(&conn->gnc_refcount, 1);
294 atomic_set(&conn->gnc_reaper_noop, 0);
295 atomic_set(&conn->gnc_sched_noop, 0);
296 atomic_set(&conn->gnc_tx_in_use, 0);
297 INIT_LIST_HEAD(&conn->gnc_list);
298 INIT_LIST_HEAD(&conn->gnc_hashlist);
299 INIT_LIST_HEAD(&conn->gnc_schedlist);
300 INIT_LIST_HEAD(&conn->gnc_fmaq);
301 INIT_LIST_HEAD(&conn->gnc_mdd_list);
302 INIT_LIST_HEAD(&conn->gnc_delaylist);
303 spin_lock_init(&conn->gnc_list_lock);
304 spin_lock_init(&conn->gnc_tx_lock);
305 conn->gnc_magic = GNILND_CONN_MAGIC;
307 /* set tx id to nearly the end to make sure we find wrapping
309 conn->gnc_next_tx = (int) GNILND_MAX_MSG_ID - 10;
311 /* if this fails, we have conflicts and MAX_TX is too large */
312 BUILD_BUG_ON(GNILND_MAX_MSG_ID >= GNILND_MSGID_CLOSE);
314 /* get a new unique CQ id for this conn */
315 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
316 conn->gnc_my_connstamp = kgnilnd_data.kgn_connstamp++;
317 conn->gnc_cqid = kgnilnd_get_cqid_locked();
318 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
320 if (conn->gnc_cqid == 0) {
321 CERROR("Could not allocate unique CQ ID for conn 0x%p\n", conn);
322 GOTO(failed, rc = -E2BIG);
325 CDEBUG(D_NET, "alloc cqid %u for conn 0x%p\n",
326 conn->gnc_cqid, conn);
328 /* need to be set before gnc_ephandle to allow kgnilnd_destroy_conn_ep to
330 conn->gnc_device = dev;
332 conn->gnc_timeout = max(*kgnilnd_tunables.kgn_timeout,
334 kgnilnd_update_reaper_timeout(conn->gnc_timeout);
336 /* this is the ep_handle for doing SMSG & BTE */
337 mutex_lock(&dev->gnd_cq_mutex);
338 rrc = kgnilnd_ep_create(dev->gnd_handle, dev->gnd_snd_fma_cqh,
339 &conn->gnc_ephandle);
340 mutex_unlock(&dev->gnd_cq_mutex);
341 if (rrc != GNI_RC_SUCCESS)
342 GOTO(failed, rc = -ENETDOWN);
344 CDEBUG(D_NET, "created conn 0x%p ep_hndl 0x%p\n",
345 conn, conn->gnc_ephandle);
347 /* add ref for EP canceling */
348 kgnilnd_conn_addref(conn);
349 atomic_inc(&dev->gnd_neps);
355 atomic_dec(&kgnilnd_data.kgn_nconns);
356 kgnilnd_vfree(conn->gnc_tx_ref_table,
357 GNILND_MAX_MSG_ID * sizeof(void *));
358 LIBCFS_FREE(conn, sizeof(*conn));
362 /* needs to be called with kgn_peer_conn_lock held (read or write) */
364 kgnilnd_find_conn_locked(kgn_peer_t *peer)
366 kgn_conn_t *conn = NULL;
368 /* if we are in reset, this conn is going to die soon */
369 if (unlikely(kgnilnd_data.kgn_in_reset)) {
373 /* just return the first ESTABLISHED connection */
374 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
375 /* kgnilnd_finish_connect doesn't put connections on the
376 * peer list until they are actually established */
377 LASSERTF(conn->gnc_state >= GNILND_CONN_ESTABLISHED,
378 "found conn %px state %s on peer %px (%s)\n",
379 conn, kgnilnd_conn_state2str(conn), peer,
380 libcfs_nid2str(peer->gnp_nid));
381 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
389 /* needs write_lock on kgn_peer_conn_lock held */
391 kgnilnd_find_or_create_conn_locked(kgn_peer_t *peer) {
393 kgn_device_t *dev = peer->gnp_net->gnn_dev;
396 conn = kgnilnd_find_conn_locked(peer);
402 /* if the peer was previously connecting, check if we should
403 * trigger another connection attempt yet. */
404 if (time_before(jiffies, peer->gnp_reconnect_time)) {
408 /* This check prevents us from creating a new connection to a peer while we are
409 * still in the process of closing an existing connection to the peer.
411 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
412 if (conn->gnc_ephandle != NULL) {
413 CDEBUG(D_NET, "Not connecting non-null ephandle found peer 0x%p->%s\n", peer,
414 libcfs_nid2str(peer->gnp_nid));
419 if (peer->gnp_connecting != GNILND_PEER_IDLE) {
420 /* if we are not connecting, fire up a new connection */
421 /* or if we are anything but IDLE DONT start a new connection */
425 CDEBUG(D_NET, "starting connect to %s\n",
426 libcfs_nid2str(peer->gnp_nid));
427 peer->gnp_connecting = GNILND_PEER_CONNECT;
428 kgnilnd_peer_addref(peer); /* extra ref for connd */
430 spin_lock(&dev->gnd_connd_lock);
431 list_add_tail(&peer->gnp_connd_list, &dev->gnd_connd_peers);
432 spin_unlock(&dev->gnd_connd_lock);
434 kgnilnd_schedule_dgram(dev);
435 CDEBUG(D_NETTRACE, "scheduling new connect\n");
440 /* Caller is responsible for deciding if/when to call this */
442 kgnilnd_destroy_conn_ep(kgn_conn_t *conn)
445 gni_ep_handle_t tmp_ep;
447 /* only if we actually initialized it,
448 * then set NULL to tell kgnilnd_destroy_conn to leave it alone */
450 tmp_ep = xchg(&conn->gnc_ephandle, NULL);
451 if (tmp_ep != NULL) {
452 /* we never re-use the EP, so unbind is not needed */
453 mutex_lock(&conn->gnc_device->gnd_cq_mutex);
454 rrc = kgnilnd_ep_destroy(tmp_ep);
456 mutex_unlock(&conn->gnc_device->gnd_cq_mutex);
458 /* if this fails, it could hork up kgni smsg retransmit and others
459 * since we could free the SMSG mbox memory, etc. */
460 LASSERTF(rrc == GNI_RC_SUCCESS, "rrc %d conn 0x%p ep 0x%p\n",
461 rrc, conn, conn->gnc_ephandle);
463 atomic_dec(&conn->gnc_device->gnd_neps);
465 /* clear out count added in kgnilnd_close_conn_locked
466 * conn will have a peer once it hits finish_connect, where it
467 * is the first spot we'll mark it ESTABLISHED as well */
468 if (conn->gnc_peer) {
469 kgnilnd_admin_decref(conn->gnc_peer->gnp_dirty_eps);
472 /* drop ref for EP */
473 kgnilnd_conn_decref(conn);
478 kgnilnd_destroy_conn(kgn_conn_t *conn)
480 LASSERTF(!in_interrupt() &&
481 !conn->gnc_scheduled &&
482 !conn->gnc_in_purgatory &&
483 conn->gnc_ephandle == NULL &&
484 list_empty(&conn->gnc_list) &&
485 list_empty(&conn->gnc_hashlist) &&
486 list_empty(&conn->gnc_schedlist) &&
487 list_empty(&conn->gnc_mdd_list) &&
488 list_empty(&conn->gnc_delaylist) &&
489 conn->gnc_magic == GNILND_CONN_MAGIC,
490 "conn 0x%p->%s IRQ %d sched %d purg %d ep 0x%p Mg %d lists %d/%d/%d/%d/%d\n",
491 conn, conn->gnc_peer ? libcfs_nid2str(conn->gnc_peer->gnp_nid)
493 !!in_interrupt(), conn->gnc_scheduled,
494 conn->gnc_in_purgatory,
497 list_empty(&conn->gnc_list),
498 list_empty(&conn->gnc_hashlist),
499 list_empty(&conn->gnc_schedlist),
500 list_empty(&conn->gnc_mdd_list),
501 list_empty(&conn->gnc_delaylist));
503 /* Tripping these is especially bad, as it means we have items on the
504 * lists that didn't keep their refcount on the connection - or
505 * somebody evil released their own */
506 LASSERTF(list_empty(&conn->gnc_fmaq) &&
507 atomic_read(&conn->gnc_nlive_fma) == 0 &&
508 atomic_read(&conn->gnc_nlive_rdma) == 0,
509 "conn 0x%p fmaq %d@0x%p nfma %d nrdma %d\n",
510 conn, kgnilnd_count_list(&conn->gnc_fmaq), &conn->gnc_fmaq,
511 atomic_read(&conn->gnc_nlive_fma), atomic_read(&conn->gnc_nlive_rdma));
513 CDEBUG(D_NET, "destroying conn %p ephandle %p error %d\n",
514 conn, conn->gnc_ephandle, conn->gnc_error);
516 /* We are freeing this memory remove the magic value from the connection */
519 /* if there is an FMA blk left here, we'll tear it down */
520 if (conn->gnc_fma_blk) {
521 if (conn->gnc_peer) {
522 kgn_mbox_info_t *mbox;
523 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
524 mbox->mbx_prev_nid = conn->gnc_peer->gnp_nid;
526 kgnilnd_release_mbox(conn, 0);
529 if (conn->gnc_peer != NULL)
530 kgnilnd_peer_decref(conn->gnc_peer);
532 if (conn->gnc_tx_ref_table != NULL) {
533 kgnilnd_vfree(conn->gnc_tx_ref_table,
534 GNILND_MAX_MSG_ID * sizeof(void *));
537 LIBCFS_FREE(conn, sizeof(*conn));
538 atomic_dec(&kgnilnd_data.kgn_nconns);
541 /* peer_alive and peer_notify done in the style of the o2iblnd */
543 kgnilnd_peer_alive(kgn_peer_t *peer)
545 time64_t now = ktime_get_seconds();
547 set_mb(peer->gnp_last_alive, now);
551 kgnilnd_peer_notify(kgn_peer_t *peer, int error, int alive)
562 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_DONT_NOTIFY))
565 /* Tell LNet we are giving ups on this peer - but only
566 * if it isn't already reconnected or trying to reconnect */
567 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
569 /* use kgnilnd_find_conn_locked to avoid any conns in the process of being nuked
571 * don't tell LNet if we are in reset - we assume that everyone will be able to
572 * reconnect just fine
574 conn = kgnilnd_find_conn_locked(peer);
576 CDEBUG(D_NETTRACE, "peer 0x%p->%s ting %d conn 0x%p, rst %d error %d\n",
577 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting, conn,
578 kgnilnd_data.kgn_in_reset, error);
580 if (((peer->gnp_connecting == GNILND_PEER_IDLE) &&
582 (!kgnilnd_data.kgn_in_reset) &&
583 (!kgnilnd_conn_clean_errno(error))) || alive) {
587 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
590 /* short circuit if we dont need to notify Lnet */
594 rc = down_read_trylock(&kgnilnd_data.kgn_net_rw_sem);
597 /* dont do this if this fails since LNET is in shutdown or something else
600 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
601 list_for_each_entry(net, &kgnilnd_data.kgn_nets[i], gnn_list) {
602 /* if gnn_shutdown set for any net shutdown is in progress just return */
603 if (net->gnn_shutdown) {
604 up_read(&kgnilnd_data.kgn_net_rw_sem);
612 /* shutdown in progress most likely */
613 up_read(&kgnilnd_data.kgn_net_rw_sem);
617 CFS_ALLOC_PTR_ARRAY(nets, nnets);
620 up_read(&kgnilnd_data.kgn_net_rw_sem);
621 CERROR("Failed to allocate nets[%d]\n", nnets);
626 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
627 list_for_each_entry(net, &kgnilnd_data.kgn_nets[i], gnn_list) {
629 kgnilnd_net_addref(net);
633 up_read(&kgnilnd_data.kgn_net_rw_sem);
635 for (i = 0; i < nnets; i++) {
636 struct lnet_nid peer_nid;
640 lnet_nid4_to_nid(kgnilnd_lnd2lnetnid(
641 lnet_nid_to_nid4(&net->gnn_ni->ni_nid),
645 CDEBUG(D_NET, "peer 0x%p->%s last_alive %lld (%llds ago)\n",
646 peer, libcfs_nidstr(&peer_nid),
647 peer->gnp_last_alive,
648 ktime_get_seconds() - peer->gnp_last_alive);
650 lnet_notify(net->gnn_ni, &peer_nid, alive, true,
651 peer->gnp_last_alive);
653 kgnilnd_net_decref(net);
656 CFS_FREE_PTR_ARRAY(nets, nnets);
660 /* need write_lock on kgn_peer_conn_lock */
662 kgnilnd_close_conn_locked(kgn_conn_t *conn, int error)
664 kgn_peer_t *peer = conn->gnc_peer;
667 LASSERT(!in_interrupt());
669 /* store error for tx completion */
670 conn->gnc_error = error;
671 peer->gnp_last_errno = error;
673 /* use real error from peer if possible */
674 if (error == -ECONNRESET) {
675 error = conn->gnc_peer_error;
678 /* if we NETERROR, make sure it is rate limited */
679 if (!kgnilnd_conn_clean_errno(error) &&
680 peer->gnp_state != GNILND_PEER_DOWN) {
681 CNETERR("closing conn to %s: error %d\n",
682 libcfs_nid2str(peer->gnp_nid), error);
684 CDEBUG(D_NET, "closing conn to %s: error %d\n",
685 libcfs_nid2str(peer->gnp_nid), error);
688 LASSERTF(conn->gnc_state == GNILND_CONN_ESTABLISHED,
689 "conn %px to %s with bogus state %s\n", conn,
690 libcfs_nid2str(conn->gnc_peer->gnp_nid),
691 kgnilnd_conn_state2str(conn));
692 LASSERT(!list_empty(&conn->gnc_hashlist));
693 LASSERT(!list_empty(&conn->gnc_list));
696 /* mark peer count here so any place the EP gets destroyed will
697 * open up the peer count so that a new ESTABLISHED conn is then free
698 * to send new messages -- sending before the previous EPs are destroyed
699 * could end up with messages on the network for the old conn _after_
700 * the new conn and break the mbox safety protocol */
701 kgnilnd_admin_addref(conn->gnc_peer->gnp_dirty_eps);
703 /* Remove from conn hash table: no new callbacks */
704 list_del_init(&conn->gnc_hashlist);
705 kgnilnd_data.kgn_conn_version++;
706 kgnilnd_conn_decref(conn);
708 /* if we are in reset, go right to CLOSED as there is no scheduler
709 * thread to move from CLOSING to CLOSED */
710 if (unlikely(kgnilnd_data.kgn_in_reset)) {
711 conn->gnc_state = GNILND_CONN_CLOSED;
713 conn->gnc_state = GNILND_CONN_CLOSING;
716 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_RDMA_CQ_ERROR)) {
717 msleep_interruptible(MSEC_PER_SEC);
720 /* leave on peer->gnp_conns to make sure we don't let the reaper
721 * or others try to unlink this peer until the conn is fully
722 * processed for closing */
724 if (kgnilnd_check_purgatory_conn(conn)) {
725 kgnilnd_add_purgatory_locked(conn, conn->gnc_peer);
728 /* Reset RX timeout to ensure we wait for an incoming CLOSE
729 * for the full timeout. If we get a CLOSE we know the
730 * peer has stopped all RDMA. Otherwise if we wait for
731 * the full timeout we can also be sure all RDMA has stopped. */
732 conn->gnc_last_rx = conn->gnc_last_rx_cq = jiffies;
735 /* schedule sending CLOSE - if we are in quiesce, this adds to
736 * gnd_ready_conns and allows us to find it in quiesce processing */
737 kgnilnd_schedule_conn(conn);
743 kgnilnd_close_conn(kgn_conn_t *conn, int error)
745 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
746 /* need to check the state here - this call is racy and we don't
747 * know the state until after the lock is grabbed */
748 if (conn->gnc_state == GNILND_CONN_ESTABLISHED) {
749 kgnilnd_close_conn_locked(conn, error);
751 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
755 kgnilnd_complete_closed_conn(kgn_conn_t *conn)
765 /* Dump log on cksum error - wait until complete phase to let
766 * RX of error happen */
767 if (*kgnilnd_tunables.kgn_checksum_dump &&
768 (conn != NULL && conn->gnc_peer_error == -ENOKEY)) {
769 libcfs_debug_dumplog();
772 /* _CLOSED set in kgnilnd_process_fmaq once we decide to
773 * send the CLOSE or not */
774 LASSERTF(conn->gnc_state == GNILND_CONN_CLOSED,
775 "conn 0x%p->%s with bad state %s\n",
776 conn, conn->gnc_peer ?
777 libcfs_nid2str(conn->gnc_peer->gnp_nid) :
779 kgnilnd_conn_state2str(conn));
781 LASSERT(list_empty(&conn->gnc_hashlist));
782 /* We shouldnt be on the delay list, the conn can
783 * get added to this list during a retransmit, and retransmits
784 * only occur within scheduler threads.
786 LASSERT(list_empty(&conn->gnc_delaylist));
788 /* we've sent the close, start nuking */
789 if (CFS_FAIL_CHECK(CFS_FAIL_GNI_SCHEDULE_COMPLETE))
790 kgnilnd_schedule_conn(conn);
792 if (conn->gnc_scheduled != GNILND_CONN_PROCESS) {
793 CDEBUG(D_NETERROR, "Error someone scheduled us after we were "
794 "done, Attempting to recover conn 0x%p "
795 "scheduled %d function: %s line: %d\n", conn,
796 conn->gnc_scheduled, conn->gnc_sched_caller,
797 conn->gnc_sched_line);
801 /* we don't use lists to track things that we can get out of the
804 /* need to hold locks for tx_list_state, sampling it is too racy:
805 * - the lock actually protects tx != NULL, but we can't take the proper
806 * lock until we check tx_list_state, which would be too late and
807 * we could have the TX change under us.
808 * gnd_rdmaq_lock and gnd_lock and not used together, so taking both
810 spin_lock(&conn->gnc_device->gnd_rdmaq_lock);
811 spin_lock(&conn->gnc_device->gnd_lock);
813 for (nrdma = 0; nrdma < GNILND_MAX_MSG_ID; nrdma++) {
814 tx = conn->gnc_tx_ref_table[nrdma];
817 /* only print the first error and if not CLOSE, we often don't see
818 * CQ events for that by the time we get here... and really don't care */
819 if (nlive || tx->tx_msg.gnm_type == GNILND_MSG_CLOSE)
820 tx->tx_state |= GNILND_TX_QUIET_ERROR;
822 GNIDBG_TX(D_NET, tx, "cleaning up on close, nlive %d", nlive);
824 /* don't worry about gnc_lock here as nobody else should be
825 * touching this conn */
826 kgnilnd_tx_del_state_locked(tx, NULL, conn, GNILND_TX_ALLOCD);
827 list_add_tail(&tx->tx_list, &sinners);
830 spin_unlock(&conn->gnc_device->gnd_lock);
831 spin_unlock(&conn->gnc_device->gnd_rdmaq_lock);
833 /* nobody should have marked this as needing scheduling after
834 * we called close - so only ref should be us handling it */
835 if (conn->gnc_scheduled != GNILND_CONN_PROCESS) {
836 CDEBUG(D_NETERROR, "Error someone scheduled us after we were "
837 "done, Attempting to recover conn 0x%p "
838 "scheduled %d function %s line: %d\n", conn,
839 conn->gnc_scheduled, conn->gnc_sched_caller,
840 conn->gnc_sched_line);
842 /* now reset a few to actual counters... */
843 nrdma = atomic_read(&conn->gnc_nlive_rdma);
844 nq_rdma = atomic_read(&conn->gnc_nq_rdma);
846 if (!list_empty(&sinners)) {
847 list_for_each_entry_safe(tx, txn, &sinners, tx_list) {
848 /* clear tx_list to make tx_add_list_locked happy */
849 list_del_init(&tx->tx_list);
850 /* The error codes determine if we hold onto the MDD */
851 kgnilnd_tx_done(tx, conn->gnc_error);
855 logmsg = (nlive + nrdma + nq_rdma);
858 int level = conn->gnc_peer->gnp_state == GNILND_PEER_UP ?
860 CDEBUG(level, "Closed conn 0x%p->%s (errno %d,"
861 " peer errno %d): canceled %d TX, %d/%d RDMA\n",
862 conn, libcfs_nid2str(conn->gnc_peer->gnp_nid),
863 conn->gnc_error, conn->gnc_peer_error,
864 nlive, nq_rdma, nrdma);
867 kgnilnd_destroy_conn_ep(conn);
869 /* Bug 765042 - race this with completing a new conn to same peer - we need
870 * finish_connect to detach purgatory before we can do it ourselves here */
871 CFS_RACE(CFS_FAIL_GNI_FINISH_PURG);
873 /* now it is safe to remove from peer list - anyone looking at
874 * gnp_conns now is free to unlink if not on purgatory */
875 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
877 conn->gnc_state = GNILND_CONN_DONE;
879 /* Decrement counter if we are marked by del_conn_or_peers for closing
881 if (conn->gnc_needs_closing)
882 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_conns);
884 /* Remove from peer's list of valid connections if its not in purgatory */
885 if (!conn->gnc_in_purgatory) {
886 list_del_init(&conn->gnc_list);
887 /* Lose peers reference on the conn */
888 kgnilnd_conn_decref(conn);
891 /* NB - only unlinking if we set pending in del_peer_locked from admin or
893 if (kgnilnd_peer_active(conn->gnc_peer) &&
894 conn->gnc_peer->gnp_pending_unlink &&
895 kgnilnd_can_unlink_peer_locked(conn->gnc_peer)) {
896 kgnilnd_unlink_peer_locked(conn->gnc_peer);
899 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
901 /* I'm telling Mommy! - use peer_error if they initiated close */
902 kgnilnd_peer_notify(conn->gnc_peer,
903 conn->gnc_error == -ECONNRESET ?
904 conn->gnc_peer_error : conn->gnc_error, 0);
910 kgnilnd_set_conn_params(kgn_dgram_t *dgram)
912 kgn_conn_t *conn = dgram->gndg_conn;
913 kgn_connreq_t *connreq = &dgram->gndg_conn_in;
914 kgn_gniparams_t *rem_param = &connreq->gncr_gnparams;
917 gni_smsg_attr_t *remote = &connreq->gncr_gnparams.gnpr_smsg_attr;
919 /* set timeout vals in conn early so we can use them for the NAK */
921 /* use max of the requested and our timeout, peer will do the same */
922 conn->gnc_timeout = max(conn->gnc_timeout, connreq->gncr_timeout);
924 /* only ep_bind really mucks around with the CQ */
925 /* only ep bind if we are not connecting to ourself and the dstnid is not a wildcard. this check
926 * is necessary as you can only bind an ep once and we must make sure we dont bind when already bound.
928 if (connreq->gncr_dstnid != LNET_NID_ANY && dgram->gndg_conn_out.gncr_dstnid != connreq->gncr_srcnid) {
929 mutex_lock(&conn->gnc_device->gnd_cq_mutex);
930 rrc = kgnilnd_ep_bind(conn->gnc_ephandle,
931 connreq->gncr_gnparams.gnpr_host_id,
933 mutex_unlock(&conn->gnc_device->gnd_cq_mutex);
934 if (rrc != GNI_RC_SUCCESS) {
940 rrc = kgnilnd_ep_set_eventdata(conn->gnc_ephandle, conn->gnc_cqid,
941 connreq->gncr_gnparams.gnpr_cqid);
942 if (rrc != GNI_RC_SUCCESS) {
947 /* Initialize SMSG */
948 rrc = kgnilnd_smsg_init(conn->gnc_ephandle, &conn->gnpr_smsg_attr,
949 &connreq->gncr_gnparams.gnpr_smsg_attr);
950 if (unlikely(rrc == GNI_RC_INVALID_PARAM)) {
951 gni_smsg_attr_t *local = &conn->gnpr_smsg_attr;
952 /* help folks figure out if there is a tunable off, etc. */
953 LCONSOLE_ERROR("SMSG attribute mismatch. Data from local/remote:"
954 " type %d/%d msg_maxsize %u/%u"
955 " mbox_maxcredit %u/%u. Please check kgni"
956 " logs for further data\n",
957 local->msg_type, remote->msg_type,
958 local->msg_maxsize, remote->msg_maxsize,
959 local->mbox_maxcredit, remote->mbox_maxcredit);
961 if (rrc != GNI_RC_SUCCESS) {
966 /* log this for help in debuggin SMSG buffer re-use */
967 CDEBUG(D_NET, "conn %p src %s dst %s smsg %p acquired"
968 " local cqid %u SMSG %p->%u hndl %#llx.%#llx"
969 " remote cqid %u SMSG %p->%u hndl %#llx.%#llx\n",
970 conn, libcfs_nid2str(connreq->gncr_srcnid),
971 libcfs_nid2str(connreq->gncr_dstnid),
972 &conn->gnpr_smsg_attr,
974 conn->gnpr_smsg_attr.msg_buffer,
975 conn->gnpr_smsg_attr.mbox_offset,
976 conn->gnpr_smsg_attr.mem_hndl.qword1,
977 conn->gnpr_smsg_attr.mem_hndl.qword2,
978 rem_param->gnpr_cqid,
979 rem_param->gnpr_smsg_attr.msg_buffer,
980 rem_param->gnpr_smsg_attr.mbox_offset,
981 rem_param->gnpr_smsg_attr.mem_hndl.qword1,
982 rem_param->gnpr_smsg_attr.mem_hndl.qword2);
984 conn->gnc_peerstamp = connreq->gncr_peerstamp;
985 conn->gnc_peer_connstamp = connreq->gncr_connstamp;
986 conn->remote_mbox_addr = (void *)((char *)remote->msg_buffer + remote->mbox_offset);
988 /* We update the reaper timeout once we have a valid conn and timeout */
989 kgnilnd_update_reaper_timeout(GNILND_TO2KA(conn->gnc_timeout));
994 rrc = kgnilnd_ep_unbind(conn->gnc_ephandle);
995 /* not sure I can just let this fly */
996 LASSERTF(rrc == GNI_RC_SUCCESS,
997 "bad rc from gni_ep_unbind trying to cleanup: %d\n", rrc);
1000 LASSERTF(rc != 0, "SOFTWARE BUG: rc == 0\n");
1001 CERROR("Error setting connection params from %s: %d\n",
1002 libcfs_nid2str(connreq->gncr_srcnid), rc);
1006 /* needs down_read on kgn_net_rw_sem held from before this call until
1007 * after the write_lock on kgn_peer_conn_lock - this ensures we stay sane
1008 * with kgnilnd_shutdown - it'll get the sem and set shutdown, then get the
1009 * kgn_peer_conn_lock to start del_peer'ing. If we hold the sem until after
1010 * kgn_peer_conn_lock is held, we guarantee that nobody calls
1011 * kgnilnd_add_peer_locked without checking gnn_shutdown */
1013 kgnilnd_create_peer_safe(kgn_peer_t **peerp,
1021 LASSERT(nid != LNET_NID_ANY);
1023 /* We dont pass the net around in the dgram anymore so here is where we find it
1024 * this will work unless its in shutdown or the nid has a net that is invalid.
1025 * Either way error code needs to be returned in that case.
1027 * If the net passed in is not NULL then we can use it, this alleviates looking it
1028 * when the calling function has access to the data.
1031 rc = kgnilnd_find_net(nid, &net);
1035 /* find net adds a reference on the net if we are not using
1036 * it we must do it manually so the net references are
1037 * correct when tearing down the net
1039 kgnilnd_net_addref(net);
1042 LIBCFS_ALLOC(peer, sizeof(*peer));
1044 kgnilnd_net_decref(net);
1047 peer->gnp_nid = nid;
1048 peer->gnp_state = node_state;
1050 /* translate from nid to nic addr & store */
1051 rc = kgnilnd_nid_to_nicaddrs(LNET_NIDADDR(nid), 1, &peer->gnp_host_id);
1053 kgnilnd_net_decref(net);
1054 LIBCFS_FREE(peer, sizeof(*peer));
1057 CDEBUG(D_NET, "peer 0x%p->%s -> NIC 0x%x\n", peer,
1058 libcfs_nid2str(nid), peer->gnp_host_id);
1060 atomic_set(&peer->gnp_refcount, 1); /* 1 ref for caller */
1061 atomic_set(&peer->gnp_dirty_eps, 0);
1063 INIT_LIST_HEAD(&peer->gnp_list);
1064 INIT_LIST_HEAD(&peer->gnp_connd_list);
1065 INIT_LIST_HEAD(&peer->gnp_conns);
1066 INIT_LIST_HEAD(&peer->gnp_tx_queue);
1068 /* the first reconnect should happen immediately, so we leave
1069 * gnp_reconnect_interval set to 0 */
1071 LASSERTF(net != NULL, "peer 0x%p->%s with NULL net\n",
1072 peer, libcfs_nid2str(nid));
1074 /* must have kgn_net_rw_sem held for this... */
1075 if (net->gnn_shutdown) {
1076 /* shutdown has started already */
1077 kgnilnd_net_decref(net);
1078 LIBCFS_FREE(peer, sizeof(*peer));
1082 peer->gnp_net = net;
1084 atomic_inc(&kgnilnd_data.kgn_npeers);
1091 kgnilnd_destroy_peer(kgn_peer_t *peer)
1093 CDEBUG(D_NET, "peer %s %p deleted\n",
1094 libcfs_nid2str(peer->gnp_nid), peer);
1095 LASSERTF(atomic_read(&peer->gnp_refcount) == 0,
1096 "peer 0x%p->%s refs %d\n",
1097 peer, libcfs_nid2str(peer->gnp_nid),
1098 atomic_read(&peer->gnp_refcount));
1099 LASSERTF(atomic_read(&peer->gnp_dirty_eps) == 0,
1100 "peer 0x%p->%s dirty eps %d\n",
1101 peer, libcfs_nid2str(peer->gnp_nid),
1102 atomic_read(&peer->gnp_dirty_eps));
1103 LASSERTF(peer->gnp_net != NULL, "peer %px (%s) with NULL net\n",
1104 peer, libcfs_nid2str(peer->gnp_nid));
1105 LASSERTF(!kgnilnd_peer_active(peer),
1107 peer, libcfs_nid2str(peer->gnp_nid));
1108 LASSERTF(peer->gnp_connecting == GNILND_PEER_IDLE || peer->gnp_connecting == GNILND_PEER_KILL,
1109 "peer 0x%p->%s, connecting %d\n",
1110 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting);
1111 LASSERTF(list_empty(&peer->gnp_conns),
1113 peer, libcfs_nid2str(peer->gnp_nid));
1114 LASSERTF(list_empty(&peer->gnp_tx_queue),
1116 peer, libcfs_nid2str(peer->gnp_nid));
1117 LASSERTF(list_empty(&peer->gnp_connd_list),
1119 peer, libcfs_nid2str(peer->gnp_nid));
1121 /* NB a peer's connections keep a reference on their peer until
1122 * they are destroyed, so we can be assured that _all_ state to do
1123 * with this peer has been cleaned up when its refcount drops to
1126 atomic_dec(&kgnilnd_data.kgn_npeers);
1127 kgnilnd_net_decref(peer->gnp_net);
1129 LIBCFS_FREE(peer, sizeof(*peer));
1132 /* the conn might not have made it all the way through to a connected
1133 * state - but we need to purgatory any conn that a remote peer might
1134 * have seen through a posted dgram as well */
1136 kgnilnd_add_purgatory_locked(kgn_conn_t *conn, kgn_peer_t *peer)
1138 kgn_mbox_info_t *mbox = NULL;
1141 /* NB - the caller should own conn by removing him from the
1142 * scheduler thread when finishing the close */
1144 LASSERTF(peer != NULL, "conn %px with NULL peer\n", conn);
1146 /* If this is still true, need to add the calls to unlink back in and
1147 * figure out how to close the hole on loopback conns */
1148 LASSERTF(kgnilnd_peer_active(peer),
1149 "can't use inactive peer %s (%px) we'll never recover the resources\n",
1150 libcfs_nid2str(peer->gnp_nid), peer);
1152 CDEBUG(D_NET, "conn %p peer %p dev %p\n", conn, peer,
1155 LASSERTF(conn->gnc_in_purgatory == 0,
1156 "Conn already in purgatory\n");
1157 conn->gnc_in_purgatory = 1;
1159 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
1160 mbox->mbx_prev_purg_nid = peer->gnp_nid;
1161 mbox->mbx_add_purgatory = jiffies;
1162 kgnilnd_release_mbox(conn, 1);
1164 LASSERTF(list_empty(&conn->gnc_mdd_list),
1165 "conn 0x%p->%s with active purgatory hold MDD %d\n",
1166 conn, libcfs_nid2str(peer->gnp_nid),
1167 kgnilnd_count_list(&conn->gnc_mdd_list));
1172 /* Instead of detaching everything from purgatory here we just mark the conn as needing
1173 * detach, when the reaper checks the conn the next time it will detach it.
1174 * Calling function requires write_lock held on kgn_peer_conn_lock
1177 kgnilnd_mark_for_detach_purgatory_all_locked(kgn_peer_t *peer) {
1180 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
1181 if (conn->gnc_in_purgatory && !conn->gnc_needs_detach) {
1182 conn->gnc_needs_detach = 1;
1183 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_detach);
1188 /* Calling function needs a write_lock held on kgn_peer_conn_lock */
1190 kgnilnd_detach_purgatory_locked(kgn_conn_t *conn, struct list_head *conn_list)
1192 kgn_mbox_info_t *mbox = NULL;
1194 /* if needed, add the conn purgatory data to the list passed in */
1195 if (conn->gnc_in_purgatory) {
1196 CDEBUG(D_NET, "peer %p->%s purg_conn %p@%s mdd_list #tx %d\n",
1197 conn->gnc_peer, libcfs_nid2str(conn->gnc_peer->gnp_nid),
1198 conn, kgnilnd_conn_state2str(conn),
1199 kgnilnd_count_list(&conn->gnc_mdd_list));
1201 mbox = &conn->gnc_fma_blk->gnm_mbox_info[conn->gnc_mbox_id];
1202 mbox->mbx_detach_of_purgatory = jiffies;
1204 /* conn->gnc_list is the entry point on peer->gnp_conns, so detaching it
1205 * here removes it from the list of 'valid' peer connections.
1206 * We put the current conn onto a list of conns to call kgnilnd_release_purgatory_locked()
1207 * and as such the caller of kgnilnd_detach_purgatory_locked() now owns that conn, since its not
1208 * on the peer's conn_list anymore.
1211 list_del_init(&conn->gnc_list);
1213 /* NB - only unlinking if we set pending in del_peer_locked from admin or
1215 if (kgnilnd_peer_active(conn->gnc_peer) &&
1216 conn->gnc_peer->gnp_pending_unlink &&
1217 kgnilnd_can_unlink_peer_locked(conn->gnc_peer)) {
1218 kgnilnd_unlink_peer_locked(conn->gnc_peer);
1220 /* The reaper will not call detach unless the conn is fully through kgnilnd_complete_closed_conn.
1221 * If the conn is not in a DONE state somehow we are attempting to detach even though
1222 * the conn has not been fully cleaned up. If we detach while the conn is still closing
1223 * we will end up with an orphaned connection that has valid ep_handle, that is not on a
1227 LASSERTF(conn->gnc_state == GNILND_CONN_DONE,
1228 "Conn in invalid state %px@%s\n",
1229 conn, kgnilnd_conn_state2str(conn));
1231 /* move from peer to the delayed release list */
1232 list_add_tail(&conn->gnc_list, conn_list);
1237 kgnilnd_release_purgatory_list(struct list_head *conn_list)
1240 kgn_conn_t *conn, *connN;
1241 kgn_mdd_purgatory_t *gmp, *gmpN;
1243 list_for_each_entry_safe(conn, connN, conn_list, gnc_list) {
1244 dev = conn->gnc_device;
1246 kgnilnd_release_mbox(conn, -1);
1247 conn->gnc_in_purgatory = 0;
1249 list_del_init(&conn->gnc_list);
1251 /* gnc_needs_detach is set in kgnilnd_del_conn_or_peer. It is used to keep track
1252 * of conns that have been marked for detach by kgnilnd_del_conn_or_peer.
1253 * The function uses kgn_npending_detach to verify the conn has
1254 * actually been detached.
1257 if (conn->gnc_needs_detach)
1258 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_detach);
1260 /* if this guy is really dead (we are doing release from reaper),
1261 * make sure we tell LNet - if this is from other context,
1262 * the checks in the function will prevent an errant
1264 kgnilnd_peer_notify(conn->gnc_peer, conn->gnc_error, 0);
1266 list_for_each_entry_safe(gmp, gmpN, &conn->gnc_mdd_list,
1269 "dev %p releasing held mdd %#llx.%#llx\n",
1270 conn->gnc_device, gmp->gmp_map_key.qword1,
1271 gmp->gmp_map_key.qword2);
1273 atomic_dec(&dev->gnd_n_mdd_held);
1274 kgnilnd_mem_mdd_release(conn->gnc_device->gnd_handle,
1276 /* ignoring the return code - if kgni/ghal can't find it
1277 * it must be released already */
1279 list_del_init(&gmp->gmp_list);
1280 LIBCFS_FREE(gmp, sizeof(*gmp));
1282 /* lose conn ref for purgatory */
1283 kgnilnd_conn_decref(conn);
1287 /* needs write_lock on kgnilnd_data.kgn_peer_conn_lock held */
1289 kgnilnd_peer_increase_reconnect_locked(kgn_peer_t *peer)
1293 current_to = peer->gnp_reconnect_interval;
1295 /* we'll try to reconnect fast the first time, then back-off */
1296 if (current_to == 0) {
1297 peer->gnp_reconnect_time = jiffies - 1;
1298 current_to = *kgnilnd_tunables.kgn_min_reconnect_interval;
1300 peer->gnp_reconnect_time = jiffies + cfs_time_seconds(current_to);
1301 /* add 50% of min timeout & retry */
1302 current_to += *kgnilnd_tunables.kgn_min_reconnect_interval / 2;
1305 current_to = min(current_to,
1306 *kgnilnd_tunables.kgn_max_reconnect_interval);
1308 peer->gnp_reconnect_interval = current_to;
1309 CDEBUG(D_NET, "peer %s can reconnect at %lu interval %lu\n",
1310 libcfs_nid2str(peer->gnp_nid), peer->gnp_reconnect_time,
1311 peer->gnp_reconnect_interval);
1314 /* needs kgnilnd_data.kgn_peer_conn_lock held */
1316 kgnilnd_find_peer_locked(lnet_nid_t nid)
1318 struct list_head *peer_list = kgnilnd_nid2peerlist(nid);
1321 /* Chopping nid down to only NIDADDR using LNET_NIDADDR so we only
1322 * have a single peer per device instead of a peer per nid/net combo.
1325 list_for_each_entry(peer, peer_list, gnp_list) {
1326 if (LNET_NIDADDR(nid) != LNET_NIDADDR(peer->gnp_nid))
1329 CDEBUG(D_NET, "got peer [%p] -> %s c %d (%d)\n",
1330 peer, libcfs_nid2str(nid),
1331 peer->gnp_connecting,
1332 atomic_read(&peer->gnp_refcount));
1338 /* need write_lock on kgn_peer_conn_lock */
1340 kgnilnd_unlink_peer_locked(kgn_peer_t *peer)
1342 LASSERTF(list_empty(&peer->gnp_conns),
1344 peer, libcfs_nid2str(peer->gnp_nid));
1345 LASSERTF(list_empty(&peer->gnp_tx_queue),
1347 peer, libcfs_nid2str(peer->gnp_nid));
1348 LASSERTF(kgnilnd_peer_active(peer),
1350 peer, libcfs_nid2str(peer->gnp_nid));
1351 CDEBUG(D_NET, "unlinking peer 0x%p->%s\n",
1352 peer, libcfs_nid2str(peer->gnp_nid));
1354 list_del_init(&peer->gnp_list);
1355 kgnilnd_data.kgn_peer_version++;
1356 kgnilnd_admin_decref(kgnilnd_data.kgn_npending_unlink);
1357 /* lose peerlist's ref */
1358 kgnilnd_peer_decref(peer);
1362 kgnilnd_get_peer_info(int index,
1363 kgn_peer_t **found_peer,
1364 lnet_nid_t *id, __u32 *nic_addr,
1365 int *refcount, int *connecting)
1371 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1373 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
1374 list_for_each_entry(peer, &kgnilnd_data.kgn_peers[i], gnp_list) {
1378 CDEBUG(D_NET, "found peer %p (%s) at index %d\n",
1379 peer, libcfs_nid2str(peer->gnp_nid), index);
1382 *id = peer->gnp_nid;
1383 *nic_addr = peer->gnp_host_id;
1384 *refcount = atomic_read(&peer->gnp_refcount);
1385 *connecting = peer->gnp_connecting;
1392 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1394 CDEBUG(D_NET, "no gni peer at index %d\n", index);
1398 /* requires write_lock on kgn_peer_conn_lock held */
1400 kgnilnd_add_peer_locked(lnet_nid_t nid, kgn_peer_t *new_stub_peer, kgn_peer_t **peerp)
1402 kgn_peer_t *peer, *peer2;
1404 LASSERTF(new_stub_peer != NULL, "bad stub peer for nid %s\n",
1405 libcfs_nid2str(nid));
1407 peer2 = kgnilnd_find_peer_locked(nid);
1408 if (peer2 != NULL) {
1409 /* A peer was created during the lock transition, so drop
1410 * the new one we created */
1411 kgnilnd_peer_decref(new_stub_peer);
1414 peer = new_stub_peer;
1415 /* peer table takes existing ref on peer */
1417 LASSERTF(!kgnilnd_peer_active(peer),
1418 "peer 0x%p->%s already in peer table\n",
1419 peer, libcfs_nid2str(peer->gnp_nid));
1420 list_add_tail(&peer->gnp_list,
1421 kgnilnd_nid2peerlist(nid));
1422 kgnilnd_data.kgn_peer_version++;
1425 LASSERTF(peer->gnp_net != NULL, "peer 0x%p->%s with NULL net\n",
1426 peer, libcfs_nid2str(peer->gnp_nid));
1431 kgnilnd_add_peer(kgn_net_t *net, lnet_nid_t nid, kgn_peer_t **peerp)
1438 if (nid == LNET_NID_ANY)
1441 node_state = kgnilnd_get_node_state(LNET_NIDADDR(nid));
1443 /* NB - this will not block during normal operations -
1444 * the only writer of this is in the startup/shutdown path. */
1445 rc = down_read_trylock(&kgnilnd_data.kgn_net_rw_sem);
1450 rc = kgnilnd_create_peer_safe(&peer, nid, net, node_state);
1452 up_read(&kgnilnd_data.kgn_net_rw_sem);
1456 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1457 up_read(&kgnilnd_data.kgn_net_rw_sem);
1459 kgnilnd_add_peer_locked(nid, peer, peerp);
1461 CDEBUG(D_NET, "peer 0x%p->%s connecting %d\n",
1462 peerp, libcfs_nid2str((*peerp)->gnp_nid),
1463 (*peerp)->gnp_connecting);
1465 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1469 /* needs write_lock on kgn_peer_conn_lock */
1471 kgnilnd_cancel_peer_connect_locked(kgn_peer_t *peer, struct list_head *zombies)
1475 /* we do care about state of gnp_connecting - we could be between
1476 * reconnect attempts, so try to find the dgram and cancel the TX
1477 * anyways. If we are in the process of posting DONT do anything;
1478 * once it fails or succeeds we can nuke the connect attempt.
1479 * We have no idea where in kgnilnd_post_dgram we are so we cant
1480 * attempt to cancel until the function is done.
1483 /* make sure peer isn't in process of connecting or waiting for connect*/
1484 spin_lock(&peer->gnp_net->gnn_dev->gnd_connd_lock);
1485 if (!(list_empty(&peer->gnp_connd_list))) {
1486 list_del_init(&peer->gnp_connd_list);
1487 /* remove connd ref */
1488 kgnilnd_peer_decref(peer);
1490 spin_unlock(&peer->gnp_net->gnn_dev->gnd_connd_lock);
1492 if (peer->gnp_connecting == GNILND_PEER_POSTING || peer->gnp_connecting == GNILND_PEER_NEEDS_DEATH) {
1493 peer->gnp_connecting = GNILND_PEER_NEEDS_DEATH;
1494 /* We are in process of posting right now the xchg set it up for us to
1495 * cancel the connect so we are finished for now */
1497 /* no need for exchange we have the peer lock and its ready for us to nuke */
1498 LASSERTF(peer->gnp_connecting != GNILND_PEER_POSTING,
1499 "Peer in invalid state 0x%p->%s, connecting %d\n",
1500 peer, libcfs_nid2str(peer->gnp_nid), peer->gnp_connecting);
1501 peer->gnp_connecting = GNILND_PEER_IDLE;
1502 set_mb(peer->gnp_last_dgram_errno, -ETIMEDOUT);
1503 kgnilnd_find_and_cancel_dgram(peer->gnp_net->gnn_dev,
1507 /* The least we can do is nuke the tx's no matter what.... */
1508 list_for_each_entry_safe(tx, txn, &peer->gnp_tx_queue, tx_list) {
1509 kgnilnd_tx_del_state_locked(tx, peer, NULL,
1511 list_add_tail(&tx->tx_list, zombies);
1515 /* needs write_lock on kgn_peer_conn_lock */
1517 kgnilnd_del_peer_locked(kgn_peer_t *peer, int error)
1519 /* this peer could be passive and only held for purgatory,
1520 * take a ref to ensure it doesn't disappear in this function */
1521 kgnilnd_peer_addref(peer);
1523 CFS_RACE(CFS_FAIL_GNI_FIND_TARGET);
1525 /* if purgatory release cleared it out, don't try again */
1526 if (kgnilnd_peer_active(peer)) {
1527 /* always do this to allow kgnilnd_start_connect and
1528 * kgnilnd_finish_connect to catch this before they
1529 * wrap up their operations */
1530 if (kgnilnd_can_unlink_peer_locked(peer)) {
1531 /* already released purgatory, so only active
1533 kgnilnd_unlink_peer_locked(peer);
1535 kgnilnd_close_peer_conns_locked(peer, error);
1536 /* peer unlinks itself when last conn is closed */
1540 /* we are done, release back to the wild */
1541 kgnilnd_peer_decref(peer);
1545 kgnilnd_del_conn_or_peer(kgn_net_t *net, lnet_nid_t nid, int command,
1549 LIST_HEAD (zombies);
1550 kgn_peer_t *peer, *pnxt;
1556 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1558 if (nid != LNET_NID_ANY)
1559 lo = hi = kgnilnd_nid2peerlist(nid) - kgnilnd_data.kgn_peers;
1562 hi = *kgnilnd_tunables.kgn_peer_hash_size - 1;
1563 /* wildcards always succeed */
1567 for (i = lo; i <= hi; i++) {
1568 list_for_each_entry_safe(peer, pnxt, &kgnilnd_data.kgn_peers[i],
1570 LASSERTF(peer->gnp_net != NULL,
1571 "peer %px (%s) with NULL net\n",
1572 peer, libcfs_nid2str(peer->gnp_nid));
1574 if (net != NULL && peer->gnp_net != net)
1577 if (!(nid == LNET_NID_ANY || LNET_NIDADDR(peer->gnp_nid) == LNET_NIDADDR(nid)))
1580 /* In both cases, we want to stop any in-flight
1581 * connect attempts */
1582 kgnilnd_cancel_peer_connect_locked(peer, &zombies);
1585 case GNILND_DEL_CONN:
1586 kgnilnd_close_peer_conns_locked(peer, error);
1588 case GNILND_DEL_PEER:
1589 peer->gnp_pending_unlink = 1;
1590 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_unlink);
1591 kgnilnd_mark_for_detach_purgatory_all_locked(peer);
1592 kgnilnd_del_peer_locked(peer, error);
1594 case GNILND_CLEAR_PURGATORY:
1595 /* Mark everything ready for detach reaper will cleanup
1596 * once we release the kgn_peer_conn_lock
1598 kgnilnd_mark_for_detach_purgatory_all_locked(peer);
1599 peer->gnp_last_errno = -EISCONN;
1600 /* clear reconnect so he can reconnect soon */
1601 peer->gnp_reconnect_time = 0;
1602 peer->gnp_reconnect_interval = 0;
1605 CERROR("bad command %d\n", command);
1608 /* we matched something */
1613 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1616 kgnilnd_txlist_done(&zombies, error);
1618 /* This function does not return until the commands it initiated have completed,
1619 * since they have to work there way through the other threads. In the case of shutdown
1620 * threads are not woken up until after this call is initiated so we cannot wait, we just
1621 * need to return. The same applies for stack reset we shouldnt wait as the reset thread
1625 CFS_RACE(CFS_FAIL_GNI_RACE_RESET);
1627 if (error == -ENOTRECOVERABLE || error == -ESHUTDOWN) {
1631 wait_var_event_warning(&kgnilnd_data,
1632 !atomic_read(&kgnilnd_data.kgn_npending_conns) &&
1633 !atomic_read(&kgnilnd_data.kgn_npending_detach) &&
1634 !atomic_read(&kgnilnd_data.kgn_npending_unlink),
1635 "Waiting on %d peers %d closes %d detaches\n",
1636 atomic_read(&kgnilnd_data.kgn_npending_unlink),
1637 atomic_read(&kgnilnd_data.kgn_npending_conns),
1638 atomic_read(&kgnilnd_data.kgn_npending_detach));
1644 kgnilnd_get_conn_by_idx(int index)
1651 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
1652 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1653 list_for_each_entry(peer, &kgnilnd_data.kgn_peers[i], gnp_list) {
1654 list_for_each_entry(conn, &peer->gnp_conns, gnc_list) {
1655 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
1661 CDEBUG(D_NET, "++conn[%p] -> %s (%d)\n", conn,
1662 libcfs_nid2str(conn->gnc_peer->gnp_nid),
1663 atomic_read(&conn->gnc_refcount));
1664 kgnilnd_conn_addref(conn);
1665 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1669 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1676 kgnilnd_get_conn_info(kgn_peer_t *peer,
1677 int *device_id, __u64 *peerstamp,
1678 int *tx_seq, int *rx_seq,
1679 int *fmaq_len, int *nfma, int *nrdma)
1684 read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1686 conn = kgnilnd_find_conn_locked(peer);
1692 *device_id = conn->gnc_device->gnd_host_id;
1693 *peerstamp = conn->gnc_peerstamp;
1694 *tx_seq = atomic_read(&conn->gnc_tx_seq);
1695 *rx_seq = atomic_read(&conn->gnc_rx_seq);
1696 *fmaq_len = kgnilnd_count_list(&conn->gnc_fmaq);
1697 *nfma = atomic_read(&conn->gnc_nlive_fma);
1698 *nrdma = atomic_read(&conn->gnc_nlive_rdma);
1700 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1704 /* needs write_lock on kgn_peer_conn_lock */
1706 kgnilnd_close_peer_conns_locked(kgn_peer_t *peer, int why)
1709 struct list_head *ctmp, *cnxt;
1712 list_for_each_safe(ctmp, cnxt, &peer->gnp_conns) {
1713 conn = list_entry(ctmp, kgn_conn_t, gnc_list);
1715 if (conn->gnc_state != GNILND_CONN_ESTABLISHED)
1719 /* we mark gnc_needs closing and increment kgn_npending_conns so that
1720 * kgnilnd_del_conn_or_peer can wait on the other threads closing
1721 * and cleaning up the connection.
1723 if (!conn->gnc_needs_closing) {
1724 conn->gnc_needs_closing = 1;
1725 kgnilnd_admin_addref(kgnilnd_data.kgn_npending_conns);
1727 kgnilnd_close_conn_locked(conn, why);
1733 kgnilnd_report_node_state(lnet_nid_t nid, int down)
1736 kgn_peer_t *peer, *new_peer;
1739 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1740 peer = kgnilnd_find_peer_locked(nid);
1747 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1749 /* Don't add a peer for node up events */
1750 if (down == GNILND_PEER_UP)
1753 /* find any valid net - we don't care which one... */
1754 down_read(&kgnilnd_data.kgn_net_rw_sem);
1755 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
1756 list_for_each_entry(net, &kgnilnd_data.kgn_nets[i],
1766 up_read(&kgnilnd_data.kgn_net_rw_sem);
1769 CNETERR("Could not find a net for nid %lld\n", nid);
1773 /* The nid passed in does not yet contain the net portion.
1774 * Let's build it up now
1776 nid = LNET_MKNID(LNET_NID_NET(&net->gnn_ni->ni_nid), nid);
1777 rc = kgnilnd_add_peer(net, nid, &new_peer);
1780 CNETERR("Could not add peer for nid %lld, rc %d\n",
1785 write_lock(&kgnilnd_data.kgn_peer_conn_lock);
1786 peer = kgnilnd_find_peer_locked(nid);
1789 CNETERR("Could not find peer for nid %lld\n", nid);
1790 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1795 peer->gnp_state = down;
1797 if (down == GNILND_PEER_DOWN) {
1800 peer->gnp_down_event_time = jiffies;
1801 kgnilnd_cancel_peer_connect_locked(peer, &zombies);
1802 conn = kgnilnd_find_conn_locked(peer);
1805 kgnilnd_close_conn_locked(conn, -ENETRESET);
1808 peer->gnp_up_event_time = jiffies;
1811 write_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1813 if (down == GNILND_PEER_DOWN) {
1814 /* using ENETRESET so we don't get messages from
1817 kgnilnd_txlist_done(&zombies, -ENETRESET);
1818 kgnilnd_peer_notify(peer, -ECONNRESET, 0);
1819 LCONSOLE_INFO("Received down event for nid %d\n",
1827 kgnilnd_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
1829 struct libcfs_ioctl_data *data = arg;
1830 kgn_net_t *net = ni->ni_data;
1833 LASSERT(ni == net->gnn_ni);
1836 case IOC_LIBCFS_GET_PEER: {
1838 kgn_peer_t *peer = NULL;
1840 __u64 peerstamp = 0;
1841 int peer_refcount = 0, peer_connecting = 0;
1843 int tx_seq = 0, rx_seq = 0;
1844 int fmaq_len = 0, nfma = 0, nrdma = 0;
1846 rc = kgnilnd_get_peer_info(data->ioc_count, &peer,
1847 &nid, &nic_addr, &peer_refcount,
1853 /* LNET_MKNID is used to mask from lnet the multiplexing/demultiplexing of connections and peers
1854 * LNET assumes a conn and peer per net, the LNET_MKNID/LNET_NIDADDR allows us to let Lnet see what it
1855 * wants to see instead of the underlying network that is being used to send the data
1857 data->ioc_nid = LNET_MKNID(LNET_NID_NET(&ni->ni_nid),
1859 data->ioc_flags = peer_connecting;
1860 data->ioc_count = peer_refcount;
1862 rc = kgnilnd_get_conn_info(peer, &device_id, &peerstamp,
1863 &tx_seq, &rx_seq, &fmaq_len,
1866 /* This is allowable - a persistent peer could not
1867 * have a connection */
1869 /* flag to indicate we are not connected -
1870 * need to print as such */
1871 data->ioc_flags |= (1<<16);
1875 data->ioc_net = device_id;
1876 data->ioc_u64[0] = peerstamp;
1877 data->ioc_u32[0] = fmaq_len;
1878 data->ioc_u32[1] = nfma;
1879 data->ioc_u32[2] = tx_seq;
1880 data->ioc_u32[3] = rx_seq;
1881 data->ioc_u32[4] = nrdma;
1885 case IOC_LIBCFS_ADD_PEER: {
1886 /* just dummy value to allow using common interface */
1888 rc = kgnilnd_add_peer(net, data->ioc_nid, &peer);
1891 case IOC_LIBCFS_DEL_PEER: {
1892 /* NULL is passed in so it affects all peers in existence without regard to network
1893 * as the peer may not exist on the network LNET believes it to be on.
1895 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1896 GNILND_DEL_PEER, -EUCLEAN);
1899 case IOC_LIBCFS_GET_CONN: {
1900 kgn_conn_t *conn = kgnilnd_get_conn_by_idx(data->ioc_count);
1906 /* LNET_MKNID is used to build the correct address based on what LNET wants to see instead of
1907 * the generic connection that is used to send the data
1909 data->ioc_nid = LNET_MKNID(LNET_NID_NET(&ni->ni_nid),
1910 LNET_NIDADDR(conn->gnc_peer->gnp_nid));
1911 data->ioc_u32[0] = conn->gnc_device->gnd_id;
1912 kgnilnd_conn_decref(conn);
1916 case IOC_LIBCFS_CLOSE_CONNECTION: {
1917 /* use error = -ENETRESET to indicate it was lctl disconnect */
1918 /* NULL is passed in so it affects all the nets as the connection is virtual
1919 * and may not exist on the network LNET believes it to be on.
1921 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1922 GNILND_DEL_CONN, -ENETRESET);
1925 case IOC_LIBCFS_PUSH_CONNECTION: {
1926 /* we use this to flush purgatory */
1927 rc = kgnilnd_del_conn_or_peer(NULL, data->ioc_nid,
1928 GNILND_CLEAR_PURGATORY, -EUCLEAN);
1931 case IOC_LIBCFS_REGISTER_MYNID: {
1932 /* Ignore if this is a noop */
1933 if (data->ioc_nid == lnet_nid_to_nid4(&ni->ni_nid)) {
1936 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
1937 libcfs_nid2str(data->ioc_nid),
1938 libcfs_nidstr(&ni->ni_nid));
1949 kgnilnd_dev_init(kgn_device_t *dev)
1953 unsigned int cq_size;
1956 /* size of these CQs should be able to accommodate the outgoing
1957 * RDMA and SMSG transactions. Since we really don't know what we
1958 * really need here, we'll take credits * 2 * 3 to allow a bunch.
1959 * We need to dig into this more with the performance work. */
1960 cq_size = *kgnilnd_tunables.kgn_credits * 2 * 3;
1962 rrc = kgnilnd_cdm_create(dev->gnd_id, *kgnilnd_tunables.kgn_ptag,
1963 *kgnilnd_tunables.kgn_pkey, 0,
1965 if (rrc != GNI_RC_SUCCESS) {
1966 CERROR("Can't create CDM %d (%d)\n", dev->gnd_id, rrc);
1967 GOTO(failed, rc = -ENODEV);
1970 rrc = kgnilnd_cdm_attach(dev->gnd_domain, dev->gnd_id,
1971 &dev->gnd_host_id, &dev->gnd_handle);
1972 if (rrc != GNI_RC_SUCCESS) {
1973 CERROR("Can't attach CDM to device %d (%d)\n",
1975 GOTO(failed, rc = -ENODEV);
1978 /* a bit gross, but not much we can do - Aries Sim doesn't have
1979 * hardcoded NIC/NID that we can use */
1980 rc = kgnilnd_setup_nic_translation(dev->gnd_host_id);
1982 GOTO(failed, rc = -ENODEV);
1984 /* only dev 0 gets the errors - no need to reset the stack twice
1985 * - this works because we have a single PTAG, if we had more
1986 * then we'd need to have multiple handlers */
1987 if (dev->gnd_id == 0) {
1988 rrc = kgnilnd_subscribe_errors(dev->gnd_handle,
1989 GNI_ERRMASK_CRITICAL |
1990 GNI_ERRMASK_UNKNOWN_TRANSACTION,
1991 0, NULL, kgnilnd_critical_error,
1992 &dev->gnd_err_handle);
1993 if (rrc != GNI_RC_SUCCESS) {
1994 CERROR("Can't subscribe for errors on device %d: rc %d\n",
1996 GOTO(failed, rc = -ENODEV);
1999 rc = kgnilnd_set_quiesce_callback(dev->gnd_handle,
2000 kgnilnd_quiesce_end_callback);
2001 if (rc != GNI_RC_SUCCESS) {
2002 CERROR("Can't subscribe for quiesce callback on device %d: rc %d\n",
2004 GOTO(failed, rc = -ENODEV);
2008 rc = kgnilnd_nicaddr_to_nid(dev->gnd_host_id, &dev->gnd_nid);
2010 /* log messages during startup */
2011 if (kgnilnd_data.kgn_init < GNILND_INIT_ALL) {
2012 CERROR("couldn't translate host_id 0x%x to nid. rc %d\n",
2013 dev->gnd_host_id, rc);
2015 GOTO(failed, rc = -ESRCH);
2017 CDEBUG(D_NET, "NIC %x -> NID %d\n", dev->gnd_host_id, dev->gnd_nid);
2019 rrc = kgnilnd_cq_create(dev->gnd_handle, *kgnilnd_tunables.kgn_credits,
2020 0, kgnilnd_device_callback,
2021 dev->gnd_id, &dev->gnd_snd_rdma_cqh);
2022 if (rrc != GNI_RC_SUCCESS) {
2023 CERROR("Can't create rdma send cq size %u for device "
2024 "%d (%d)\n", cq_size, dev->gnd_id, rrc);
2025 GOTO(failed, rc = -EINVAL);
2028 rrc = kgnilnd_cq_create(dev->gnd_handle, cq_size,
2029 0, kgnilnd_device_callback, dev->gnd_id,
2030 &dev->gnd_snd_fma_cqh);
2031 if (rrc != GNI_RC_SUCCESS) {
2032 CERROR("Can't create fma send cq size %u for device %d (%d)\n",
2033 cq_size, dev->gnd_id, rrc);
2034 GOTO(failed, rc = -EINVAL);
2037 /* This one we size differently - overflows are possible and it needs to be
2038 * sized based on machine size */
2039 rrc = kgnilnd_cq_create(dev->gnd_handle,
2040 *kgnilnd_tunables.kgn_fma_cq_size,
2041 0, kgnilnd_device_callback, dev->gnd_id,
2042 &dev->gnd_rcv_fma_cqh);
2043 if (rrc != GNI_RC_SUCCESS) {
2044 CERROR("Can't create fma cq size %d for device %d (%d)\n",
2045 *kgnilnd_tunables.kgn_fma_cq_size, dev->gnd_id, rrc);
2046 GOTO(failed, rc = -EINVAL);
2049 rrc = kgnilnd_register_smdd_buf(dev);
2050 if (rrc != GNI_RC_SUCCESS) {
2051 GOTO(failed, rc = -EINVAL);
2057 kgnilnd_dev_fini(dev);
2062 kgnilnd_dev_fini(kgn_device_t *dev)
2067 /* At quiesce or rest time, need to loop through and clear gnd_ready_conns ?*/
2068 LASSERTF(list_empty(&dev->gnd_ready_conns) &&
2069 list_empty(&dev->gnd_map_tx) &&
2070 list_empty(&dev->gnd_rdmaq) &&
2071 list_empty(&dev->gnd_delay_conns),
2072 "dev 0x%p ready_conns %d@0x%p delay_conns %d@0x%p"
2073 "map_tx %d@0x%p rdmaq %d@0x%p\n",
2074 dev, kgnilnd_count_list(&dev->gnd_ready_conns), &dev->gnd_ready_conns,
2075 kgnilnd_count_list(&dev->gnd_delay_conns), &dev->gnd_delay_conns,
2076 kgnilnd_count_list(&dev->gnd_map_tx), &dev->gnd_map_tx,
2077 kgnilnd_count_list(&dev->gnd_rdmaq), &dev->gnd_rdmaq);
2079 /* These should follow from tearing down all connections */
2080 LASSERTF(dev->gnd_map_nphys == 0 && dev->gnd_map_physnop == 0,
2081 "%d physical mappings of %d pages still mapped\n",
2082 dev->gnd_map_nphys, dev->gnd_map_physnop);
2084 LASSERTF(atomic_read(&dev->gnd_n_mdd) == 0 &&
2085 atomic_read(&dev->gnd_n_mdd_held) == 0 &&
2086 atomic64_read(&dev->gnd_nbytes_map) == 0,
2087 "%d SMSG mappings of %lld bytes still mapped or held %d\n",
2088 atomic_read(&dev->gnd_n_mdd),
2089 (u64)atomic64_read(&dev->gnd_nbytes_map),
2090 atomic_read(&dev->gnd_n_mdd_held));
2092 LASSERT(list_empty(&dev->gnd_map_list));
2094 /* What other assertions needed to ensure all connections torn down ? */
2096 /* check all counters == 0 (EP, MDD, etc) */
2098 /* if we are resetting due to quiese (stack reset), don't check
2100 LASSERTF(kgnilnd_data.kgn_quiesce_trigger ||
2101 atomic_read(&kgnilnd_data.kgn_nthreads) == 0,
2102 "tried to shutdown with threads active\n");
2104 if (dev->gnd_smdd_hold_buf) {
2105 rrc = kgnilnd_deregister_smdd_buf(dev);
2106 LASSERTF(rrc == GNI_RC_SUCCESS,
2107 "bad rc from deregistion of sMDD buffer: %d\n", rrc);
2108 dev->gnd_smdd_hold_buf = NULL;
2111 if (dev->gnd_rcv_fma_cqh) {
2112 rrc = kgnilnd_cq_destroy(dev->gnd_rcv_fma_cqh);
2113 LASSERTF(rrc == GNI_RC_SUCCESS,
2114 "bad rc from gni_cq_destroy on rcv_fma_cqh: %d\n", rrc);
2115 dev->gnd_rcv_fma_cqh = NULL;
2118 if (dev->gnd_snd_rdma_cqh) {
2119 rrc = kgnilnd_cq_destroy(dev->gnd_snd_rdma_cqh);
2120 LASSERTF(rrc == GNI_RC_SUCCESS,
2121 "bad rc from gni_cq_destroy on send_rdma_cqh: %d\n", rrc);
2122 dev->gnd_snd_rdma_cqh = NULL;
2125 if (dev->gnd_snd_fma_cqh) {
2126 rrc = kgnilnd_cq_destroy(dev->gnd_snd_fma_cqh);
2127 LASSERTF(rrc == GNI_RC_SUCCESS,
2128 "bad rc from gni_cq_destroy on snd_fma_cqh: %d\n", rrc);
2129 dev->gnd_snd_fma_cqh = NULL;
2132 if (dev->gnd_err_handle) {
2133 rrc = kgnilnd_release_errors(dev->gnd_err_handle);
2134 LASSERTF(rrc == GNI_RC_SUCCESS,
2135 "bad rc from gni_release_errors: %d\n", rrc);
2136 dev->gnd_err_handle = NULL;
2139 if (dev->gnd_domain) {
2140 rrc = kgnilnd_cdm_destroy(dev->gnd_domain);
2141 LASSERTF(rrc == GNI_RC_SUCCESS,
2142 "bad rc from gni_cdm_destroy: %d\n", rrc);
2143 dev->gnd_domain = NULL;
2149 int kgnilnd_base_startup(void)
2151 long long pkmem = libcfs_kmem_read();
2155 struct task_struct *thrd;
2157 #if defined(CONFIG_CRAY_XT) && !defined(CONFIG_CRAY_COMPUTE)
2158 /* limit how much memory can be allocated for fma blocks in
2159 * instances where many nodes need to reconnects at the same time */
2162 kgnilnd_data.free_pages_limit = si.totalram/4;
2167 LASSERTF(kgnilnd_data.kgn_init == GNILND_INIT_NOTHING,
2168 "init %d\n", kgnilnd_data.kgn_init);
2170 /* zero pointers, flags etc */
2171 memset(&kgnilnd_data, 0, sizeof(kgnilnd_data));
2172 kgnilnd_check_kgni_version();
2174 /* CAVEAT EMPTOR: Every 'Fma' message includes the sender's NID and
2175 * a unique (for all time) connstamp so we can uniquely identify
2176 * the sender. The connstamp is an incrementing counter
2177 * initialised with seconds + microseconds at startup time. So we
2178 * rely on NOT creating connections more frequently on average than
2179 * 1MHz to ensure we don't use old connstamps when we reboot. */
2180 kgnilnd_data.kgn_connstamp =
2181 kgnilnd_data.kgn_peerstamp =
2182 ktime_get_seconds();
2184 init_rwsem(&kgnilnd_data.kgn_net_rw_sem);
2186 for (i = 0; i < GNILND_MAXDEVS; i++) {
2187 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2190 INIT_LIST_HEAD(&dev->gnd_ready_conns);
2191 INIT_LIST_HEAD(&dev->gnd_delay_conns);
2192 INIT_LIST_HEAD(&dev->gnd_map_tx);
2193 INIT_LIST_HEAD(&dev->gnd_fma_buffs);
2194 mutex_init(&dev->gnd_cq_mutex);
2195 mutex_init(&dev->gnd_fmablk_mutex);
2196 spin_lock_init(&dev->gnd_fmablk_lock);
2197 init_waitqueue_head(&dev->gnd_waitq);
2198 init_waitqueue_head(&dev->gnd_dgram_waitq);
2199 init_waitqueue_head(&dev->gnd_dgping_waitq);
2200 spin_lock_init(&dev->gnd_lock);
2201 INIT_LIST_HEAD(&dev->gnd_map_list);
2202 spin_lock_init(&dev->gnd_map_lock);
2203 atomic_set(&dev->gnd_nfmablk, 0);
2204 atomic_set(&dev->gnd_fmablk_vers, 1);
2205 atomic_set(&dev->gnd_neps, 0);
2206 atomic_set(&dev->gnd_canceled_dgrams, 0);
2207 INIT_LIST_HEAD(&dev->gnd_connd_peers);
2208 spin_lock_init(&dev->gnd_connd_lock);
2209 spin_lock_init(&dev->gnd_dgram_lock);
2210 spin_lock_init(&dev->gnd_rdmaq_lock);
2211 INIT_LIST_HEAD(&dev->gnd_rdmaq);
2212 init_rwsem(&dev->gnd_conn_sem);
2214 /* alloc & setup nid based dgram table */
2215 CFS_ALLOC_PTR_ARRAY(dev->gnd_dgrams,
2216 *kgnilnd_tunables.kgn_peer_hash_size);
2218 if (dev->gnd_dgrams == NULL)
2219 GOTO(failed, rc = -ENOMEM);
2221 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2222 INIT_LIST_HEAD(&dev->gnd_dgrams[i]);
2224 atomic_set(&dev->gnd_ndgrams, 0);
2225 atomic_set(&dev->gnd_nwcdgrams, 0);
2226 /* setup timer for RDMAQ processing */
2227 cfs_timer_setup(&dev->gnd_rdmaq_timer,
2228 kgnilnd_schedule_device_timer,
2229 (unsigned long)dev, 0);
2231 /* setup timer for mapping processing */
2232 cfs_timer_setup(&dev->gnd_map_timer,
2233 kgnilnd_schedule_device_timer,
2234 (unsigned long)dev, 0);
2238 /* CQID 0 isn't allowed, set to MAX_MSG_ID - 1 to check for conflicts early */
2239 kgnilnd_data.kgn_next_cqid = GNILND_MAX_MSG_ID - 1;
2240 kgnilnd_data.kgn_new_min_timeout = *kgnilnd_tunables.kgn_timeout;
2241 init_waitqueue_head(&kgnilnd_data.kgn_reaper_waitq);
2242 init_waitqueue_head(&kgnilnd_data.kgn_ruhroh_waitq);
2243 spin_lock_init(&kgnilnd_data.kgn_reaper_lock);
2245 mutex_init(&kgnilnd_data.kgn_quiesce_mutex);
2246 atomic_set(&kgnilnd_data.kgn_nquiesce, 0);
2247 atomic_set(&kgnilnd_data.kgn_npending_conns, 0);
2248 atomic_set(&kgnilnd_data.kgn_npending_unlink, 0);
2249 atomic_set(&kgnilnd_data.kgn_npending_detach, 0);
2250 atomic_set(&kgnilnd_data.kgn_rev_offset, 0);
2251 atomic_set(&kgnilnd_data.kgn_rev_length, 0);
2252 atomic_set(&kgnilnd_data.kgn_rev_copy_buff, 0);
2254 /* OK to call kgnilnd_api_shutdown() to cleanup now */
2255 kgnilnd_data.kgn_init = GNILND_INIT_DATA;
2256 if (!try_module_get(THIS_MODULE))
2257 GOTO(failed, rc = -ENOENT);
2259 rwlock_init(&kgnilnd_data.kgn_peer_conn_lock);
2261 CFS_ALLOC_PTR_ARRAY(kgnilnd_data.kgn_peers,
2262 *kgnilnd_tunables.kgn_peer_hash_size);
2264 if (kgnilnd_data.kgn_peers == 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_peers[i]);
2271 CFS_ALLOC_PTR_ARRAY(kgnilnd_data.kgn_conns,
2272 *kgnilnd_tunables.kgn_peer_hash_size);
2274 if (kgnilnd_data.kgn_conns == NULL)
2275 GOTO(failed, rc = -ENOMEM);
2277 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++) {
2278 INIT_LIST_HEAD(&kgnilnd_data.kgn_conns[i]);
2281 CFS_ALLOC_PTR_ARRAY(kgnilnd_data.kgn_nets,
2282 *kgnilnd_tunables.kgn_net_hash_size);
2284 if (kgnilnd_data.kgn_nets == NULL)
2285 GOTO(failed, rc = -ENOMEM);
2287 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
2288 INIT_LIST_HEAD(&kgnilnd_data.kgn_nets[i]);
2291 kgnilnd_data.kgn_mbox_cache =
2292 kmem_cache_create("kgn_mbox_block", GNILND_MBOX_SIZE, 0,
2293 SLAB_HWCACHE_ALIGN, NULL);
2294 if (kgnilnd_data.kgn_mbox_cache == NULL) {
2295 CERROR("Can't create slab for physical mbox blocks\n");
2296 GOTO(failed, rc = -ENOMEM);
2299 kgnilnd_data.kgn_rx_cache =
2300 kmem_cache_create("kgn_rx_t", sizeof(kgn_rx_t), 0, 0, NULL);
2301 if (kgnilnd_data.kgn_rx_cache == NULL) {
2302 CERROR("Can't create slab for kgn_rx_t descriptors\n");
2303 GOTO(failed, rc = -ENOMEM);
2306 kgnilnd_data.kgn_tx_cache =
2307 kmem_cache_create("kgn_tx_t", sizeof(kgn_tx_t), 0, 0, NULL);
2308 if (kgnilnd_data.kgn_tx_cache == NULL) {
2309 CERROR("Can't create slab for kgn_tx_t\n");
2310 GOTO(failed, rc = -ENOMEM);
2313 kgnilnd_data.kgn_tx_phys_cache =
2314 kmem_cache_create("kgn_tx_phys",
2315 GNILND_MAX_IOV * sizeof(gni_mem_segment_t),
2317 if (kgnilnd_data.kgn_tx_phys_cache == NULL) {
2318 CERROR("Can't create slab for kgn_tx_phys\n");
2319 GOTO(failed, rc = -ENOMEM);
2322 kgnilnd_data.kgn_dgram_cache =
2323 kmem_cache_create("kgn_dgram_t", sizeof(kgn_dgram_t), 0, 0, NULL);
2324 if (kgnilnd_data.kgn_dgram_cache == NULL) {
2325 CERROR("Can't create slab for outgoing datagrams\n");
2326 GOTO(failed, rc = -ENOMEM);
2329 /* allocate a MAX_IOV array of page pointers for each cpu */
2330 kgnilnd_data.kgn_cksum_map_pages = kmalloc(num_possible_cpus() * sizeof (struct page *),
2332 if (kgnilnd_data.kgn_cksum_map_pages == NULL) {
2333 CERROR("Can't allocate vmap cksum pages\n");
2334 GOTO(failed, rc = -ENOMEM);
2336 kgnilnd_data.kgn_cksum_npages = num_possible_cpus();
2337 memset(kgnilnd_data.kgn_cksum_map_pages, 0,
2338 kgnilnd_data.kgn_cksum_npages * sizeof (struct page *));
2340 for (i = 0; i < kgnilnd_data.kgn_cksum_npages; i++) {
2341 kgnilnd_data.kgn_cksum_map_pages[i] = kmalloc(GNILND_MAX_IOV * sizeof (struct page *),
2343 if (kgnilnd_data.kgn_cksum_map_pages[i] == NULL) {
2344 CERROR("Can't allocate vmap cksum pages for cpu %d\n", i);
2345 GOTO(failed, rc = -ENOMEM);
2349 LASSERT(kgnilnd_data.kgn_ndevs == 0);
2351 /* Use all available GNI devices */
2352 for (i = 0; i < GNILND_MAXDEVS; i++) {
2353 dev = &kgnilnd_data.kgn_devices[kgnilnd_data.kgn_ndevs];
2355 rc = kgnilnd_dev_init(dev);
2357 /* Increment here so base_shutdown cleans it up */
2358 kgnilnd_data.kgn_ndevs++;
2360 rc = kgnilnd_allocate_phys_fmablk(dev);
2366 if (kgnilnd_data.kgn_ndevs == 0) {
2367 CERROR("Can't initialise any GNI devices\n");
2368 GOTO(failed, rc = -ENODEV);
2371 rc = kgnilnd_thread_start(kgnilnd_reaper, NULL, "kgnilnd_rpr", 0);
2373 CERROR("Can't spawn gnilnd reaper: %d\n", rc);
2377 rc = kgnilnd_start_rca_thread();
2379 CERROR("Can't spawn gnilnd rca: %d\n", rc);
2384 * Start ruhroh thread. We can't use kgnilnd_thread_start() because
2385 * we don't want this thread included in kgnilnd_data.kgn_nthreads
2386 * count. This thread controls quiesce, so it mustn't
2389 thrd = kthread_run(kgnilnd_ruhroh_thread, NULL, "%s_%02d", "kgnilnd_rr", 0);
2392 CERROR("Can't spawn gnilnd ruhroh thread: %d\n", rc);
2396 /* threads will load balance across devs as they are available */
2397 if (*kgnilnd_tunables.kgn_thread_affinity) {
2398 rc = kgnilnd_start_sd_threads();
2402 for (i = 0; i < *kgnilnd_tunables.kgn_sched_threads; i++) {
2403 rc = kgnilnd_thread_start(kgnilnd_scheduler,
2407 CERROR("Can't spawn gnilnd scheduler[%d]: %d\n",
2414 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2415 dev = &kgnilnd_data.kgn_devices[i];
2416 rc = kgnilnd_thread_start(kgnilnd_dgram_mover, dev,
2417 "kgnilnd_dg", dev->gnd_id);
2419 CERROR("Can't spawn gnilnd dgram_mover[%d]: %d\n",
2424 rc = kgnilnd_thread_start(kgnilnd_dgram_waitq, dev,
2425 "kgnilnd_dgn", dev->gnd_id);
2427 CERROR("Can't spawn gnilnd dgram_waitq[%d]: %d\n",
2432 rc = kgnilnd_setup_wildcard_dgram(dev);
2435 CERROR("Can't create wildcard dgrams[%d]: %d\n",
2441 /* flag everything initialised */
2442 kgnilnd_data.kgn_init = GNILND_INIT_ALL;
2443 /*****************************************************/
2445 CDEBUG(D_MALLOC, "initial kmem %lld\n", pkmem);
2449 kgnilnd_base_shutdown();
2450 kgnilnd_data.kgn_init = GNILND_INIT_NOTHING;
2455 kgnilnd_base_shutdown(void)
2460 while (CFS_FAIL_TIMEOUT(CFS_FAIL_GNI_PAUSE_SHUTDOWN, 1)) {};
2462 kgnilnd_data.kgn_wc_kill = 1;
2464 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2465 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2466 kgnilnd_cancel_wc_dgrams(dev);
2467 kgnilnd_cancel_dgrams(dev);
2468 kgnilnd_del_conn_or_peer(NULL, LNET_NID_ANY, GNILND_DEL_PEER, -ESHUTDOWN);
2469 kgnilnd_wait_for_canceled_dgrams(dev);
2472 /* We need to verify there are no conns left before we let the threads
2473 * shut down otherwise we could clean up the peers but still have
2474 * some outstanding conns due to orphaned datagram conns that are
2478 while (atomic_read(&kgnilnd_data.kgn_nconns) != 0) {
2481 for(j = 0; j < kgnilnd_data.kgn_ndevs; ++j) {
2482 kgn_device_t *dev = &kgnilnd_data.kgn_devices[j];
2483 kgnilnd_schedule_device(dev);
2486 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2487 "Waiting for conns to be cleaned up %d\n",atomic_read(&kgnilnd_data.kgn_nconns));
2488 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2490 /* Peer state all cleaned up BEFORE setting shutdown, so threads don't
2491 * have to worry about shutdown races. NB connections may be created
2492 * while there are still active connds, but these will be temporary
2493 * since peer creation always fails after the listener has started to
2495 * all peers should have been cleared out on the nets */
2496 LASSERTF(atomic_read(&kgnilnd_data.kgn_npeers) == 0,
2497 "peers left %d\n", atomic_read(&kgnilnd_data.kgn_npeers));
2499 /* Wait for the ruhroh thread to shut down. */
2500 kgnilnd_data.kgn_ruhroh_shutdown = 1;
2501 wake_up(&kgnilnd_data.kgn_ruhroh_waitq);
2503 while (kgnilnd_data.kgn_ruhroh_running != 0) {
2505 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2506 "Waiting for ruhroh thread to terminate\n");
2507 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2510 /* Flag threads to terminate */
2511 kgnilnd_data.kgn_shutdown = 1;
2513 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2514 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2516 /* should clear all the MDDs */
2517 kgnilnd_unmap_fma_blocks(dev);
2519 kgnilnd_schedule_device(dev);
2520 wake_up(&dev->gnd_dgram_waitq);
2521 wake_up(&dev->gnd_dgping_waitq);
2522 LASSERT(list_empty(&dev->gnd_connd_peers));
2525 spin_lock(&kgnilnd_data.kgn_reaper_lock);
2526 wake_up(&kgnilnd_data.kgn_reaper_waitq);
2527 spin_unlock(&kgnilnd_data.kgn_reaper_lock);
2529 if (atomic_read(&kgnilnd_data.kgn_nthreads))
2530 kgnilnd_wakeup_rca_thread();
2532 /* Wait for threads to exit */
2534 while (atomic_read(&kgnilnd_data.kgn_nthreads) != 0) {
2536 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2537 "Waiting for %d threads to terminate\n",
2538 atomic_read(&kgnilnd_data.kgn_nthreads));
2539 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2542 LASSERTF(atomic_read(&kgnilnd_data.kgn_npeers) == 0,
2543 "peers left %d\n", atomic_read(&kgnilnd_data.kgn_npeers));
2545 if (kgnilnd_data.kgn_peers != NULL) {
2546 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
2547 LASSERT(list_empty(&kgnilnd_data.kgn_peers[i]));
2549 CFS_FREE_PTR_ARRAY(kgnilnd_data.kgn_peers,
2550 *kgnilnd_tunables.kgn_peer_hash_size);
2553 down_write(&kgnilnd_data.kgn_net_rw_sem);
2554 if (kgnilnd_data.kgn_nets != NULL) {
2555 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++)
2556 LASSERT(list_empty(&kgnilnd_data.kgn_nets[i]));
2558 CFS_FREE_PTR_ARRAY(kgnilnd_data.kgn_nets,
2559 *kgnilnd_tunables.kgn_net_hash_size);
2561 up_write(&kgnilnd_data.kgn_net_rw_sem);
2563 LASSERTF(atomic_read(&kgnilnd_data.kgn_nconns) == 0,
2564 "conns left %d\n", atomic_read(&kgnilnd_data.kgn_nconns));
2566 if (kgnilnd_data.kgn_conns != NULL) {
2567 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
2568 LASSERT(list_empty(&kgnilnd_data.kgn_conns[i]));
2570 CFS_FREE_PTR_ARRAY(kgnilnd_data.kgn_conns,
2571 *kgnilnd_tunables.kgn_peer_hash_size);
2574 for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
2575 kgn_device_t *dev = &kgnilnd_data.kgn_devices[i];
2576 kgnilnd_dev_fini(dev);
2578 LASSERTF(atomic_read(&dev->gnd_ndgrams) == 0,
2579 "dgrams left %d\n", atomic_read(&dev->gnd_ndgrams));
2581 if (dev->gnd_dgrams != NULL) {
2582 for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size;
2584 LASSERT(list_empty(&dev->gnd_dgrams[i]));
2586 CFS_FREE_PTR_ARRAY(dev->gnd_dgrams,
2587 *kgnilnd_tunables.kgn_peer_hash_size);
2590 kgnilnd_free_phys_fmablk(dev);
2593 if (kgnilnd_data.kgn_mbox_cache != NULL)
2594 kmem_cache_destroy(kgnilnd_data.kgn_mbox_cache);
2596 if (kgnilnd_data.kgn_rx_cache != NULL)
2597 kmem_cache_destroy(kgnilnd_data.kgn_rx_cache);
2599 if (kgnilnd_data.kgn_tx_cache != NULL)
2600 kmem_cache_destroy(kgnilnd_data.kgn_tx_cache);
2602 if (kgnilnd_data.kgn_tx_phys_cache != NULL)
2603 kmem_cache_destroy(kgnilnd_data.kgn_tx_phys_cache);
2605 if (kgnilnd_data.kgn_dgram_cache != NULL)
2606 kmem_cache_destroy(kgnilnd_data.kgn_dgram_cache);
2608 if (kgnilnd_data.kgn_cksum_map_pages != NULL) {
2609 for (i = 0; i < kgnilnd_data.kgn_cksum_npages; i++) {
2610 if (kgnilnd_data.kgn_cksum_map_pages[i] != NULL) {
2611 kfree(kgnilnd_data.kgn_cksum_map_pages[i]);
2614 kfree(kgnilnd_data.kgn_cksum_map_pages);
2617 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %lld\n",
2618 libcfs_kmem_read());
2620 kgnilnd_data.kgn_init = GNILND_INIT_NOTHING;
2621 module_put(THIS_MODULE);
2627 kgnilnd_startup(struct lnet_ni *ni)
2633 LASSERTF(ni->ni_net->net_lnd == &the_kgnilnd,
2634 "bad LND 0x%p != the_kgnilnd @ 0x%p\n",
2635 ni->ni_net->net_lnd, &the_kgnilnd);
2637 if (kgnilnd_data.kgn_init == GNILND_INIT_NOTHING) {
2638 rc = kgnilnd_base_startup();
2643 /* Serialize with shutdown. */
2644 mutex_lock(&kgnilnd_data.kgn_quiesce_mutex);
2646 LIBCFS_ALLOC(net, sizeof(*net));
2648 CERROR("could not allocate net for new interface instance\n");
2649 /* no need to cleanup the CDM... */
2650 GOTO(failed, rc = -ENOMEM);
2652 INIT_LIST_HEAD(&net->gnn_list);
2656 kgnilnd_tunables_setup(ni);
2658 if (!ni->ni_interface) {
2659 rc = lnet_ni_add_interface(ni, "ipogif0");
2661 CWARN("gnilnd failed to allocate ni_interface\n");
2664 if (*kgnilnd_tunables.kgn_peer_health) {
2667 /* give this a bit of leeway - we don't have a hard timeout
2668 * as we only check timeouts periodically - see comment in kgnilnd_reaper */
2669 fudge = (GNILND_TO2KA(*kgnilnd_tunables.kgn_timeout) / GNILND_REAPER_NCHECKS);
2670 timeout = *kgnilnd_tunables.kgn_timeout + fudge;
2672 if (*kgnilnd_tunables.kgn_peer_timeout >= timeout) {
2673 ni->ni_net->net_tunables.lct_peer_timeout =
2674 *kgnilnd_tunables.kgn_peer_timeout;
2675 } else if (*kgnilnd_tunables.kgn_peer_timeout > -1) {
2676 LCONSOLE_ERROR("Peer_timeout is set to %d but needs to be >= %d\n",
2677 *kgnilnd_tunables.kgn_peer_timeout,
2680 LIBCFS_FREE(net, sizeof(*net));
2681 GOTO(failed, rc = -EINVAL);
2683 ni->ni_net->net_tunables.lct_peer_timeout = timeout;
2685 LCONSOLE_INFO("Enabling LNet peer health for gnilnd, timeout %ds\n",
2686 ni->ni_net->net_tunables.lct_peer_timeout);
2689 atomic_set(&net->gnn_refcount, 1);
2691 /* if we have multiple devices, spread the nets around */
2692 net->gnn_netnum = LNET_NETNUM(LNET_NID_NET(&ni->ni_nid));
2694 devno = LNET_NID_NET(&ni->ni_nid) % GNILND_MAXDEVS;
2695 net->gnn_dev = &kgnilnd_data.kgn_devices[devno];
2697 /* allocate a 'dummy' cdm for datagram use. We can only have a single
2698 * datagram between a nid:inst_id and nid2:inst_id. The fake cdm
2699 * give us additional inst_id to use, allowing the datagrams to flow
2700 * like rivers of honey and beer */
2702 /* the instance id for the cdm is the NETNUM offset by MAXDEVS -
2703 * ensuring we'll have a unique id */
2705 ni->ni_nid.nid_addr[0] =
2706 cpu_to_be32(LNET_NIDADDR(net->gnn_dev->gnd_nid));
2707 CDEBUG(D_NET, "adding net %p nid=%s on dev %d \n",
2708 net, libcfs_nidstr(&ni->ni_nid), net->gnn_dev->gnd_id);
2709 /* until the gnn_list is set, we need to cleanup ourselves as
2710 * kgnilnd_shutdown is just gonna get confused */
2712 down_write(&kgnilnd_data.kgn_net_rw_sem);
2713 list_add_tail(&net->gnn_list, kgnilnd_netnum2netlist(net->gnn_netnum));
2714 up_write(&kgnilnd_data.kgn_net_rw_sem);
2716 /* we need a separate thread to call probe_wait_by_id until
2717 * we get a function callback notifier from kgni */
2718 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2721 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2722 kgnilnd_shutdown(ni);
2727 kgnilnd_shutdown(struct lnet_ni *ni)
2729 kgn_net_t *net = ni->ni_data;
2734 CFS_RACE(CFS_FAIL_GNI_SR_DOWN_RACE);
2736 LASSERTF(kgnilnd_data.kgn_init == GNILND_INIT_ALL,
2737 "init %d\n", kgnilnd_data.kgn_init);
2739 /* Serialize with startup. */
2740 mutex_lock(&kgnilnd_data.kgn_quiesce_mutex);
2741 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %lld\n",
2742 libcfs_kmem_read());
2745 CERROR("got NULL net for ni %p\n", ni);
2746 GOTO(out, rc = -EINVAL);
2749 LASSERTF(ni == net->gnn_ni,
2750 "ni %px gnn_ni %px\n", net, net->gnn_ni);
2754 LASSERT(!net->gnn_shutdown);
2755 LASSERTF(atomic_read(&net->gnn_refcount) != 0,
2756 "net %px refcount %d\n",
2757 net, atomic_read(&net->gnn_refcount));
2759 if (!list_empty(&net->gnn_list)) {
2760 /* serialize with peer creation */
2761 down_write(&kgnilnd_data.kgn_net_rw_sem);
2762 net->gnn_shutdown = 1;
2763 up_write(&kgnilnd_data.kgn_net_rw_sem);
2765 kgnilnd_cancel_net_dgrams(net);
2767 kgnilnd_del_conn_or_peer(net, LNET_NID_ANY, GNILND_DEL_PEER, -ESHUTDOWN);
2769 /* if we are quiesced, need to wake up - we need those threads
2770 * alive to release peers, etc */
2771 if (GNILND_IS_QUIESCED) {
2772 set_mb(kgnilnd_data.kgn_quiesce_trigger, GNILND_QUIESCE_IDLE);
2773 kgnilnd_quiesce_wait("shutdown");
2776 kgnilnd_wait_for_canceled_dgrams(net->gnn_dev);
2778 /* We wait until the nets ref's are 1, we will release final ref which is ours
2779 * this allows us to make sure everything else is done before we free the
2783 while (atomic_read(&net->gnn_refcount) != 1) {
2785 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2786 "Waiting for %d references to clear on net %d\n",
2787 atomic_read(&net->gnn_refcount),
2789 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2792 /* release ref from kgnilnd_startup */
2793 kgnilnd_net_decref(net);
2794 /* serialize with reaper and conn_task looping */
2795 down_write(&kgnilnd_data.kgn_net_rw_sem);
2796 list_del_init(&net->gnn_list);
2797 up_write(&kgnilnd_data.kgn_net_rw_sem);
2801 /* not locking, this can't race with writers */
2802 LASSERTF(atomic_read(&net->gnn_refcount) == 0,
2803 "net %px refcount %d\n",
2804 net, atomic_read(&net->gnn_refcount));
2805 LIBCFS_FREE(net, sizeof(*net));
2808 down_read(&kgnilnd_data.kgn_net_rw_sem);
2809 for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++) {
2810 if (!list_empty(&kgnilnd_data.kgn_nets[i])) {
2811 up_read(&kgnilnd_data.kgn_net_rw_sem);
2815 if (i == *kgnilnd_tunables.kgn_net_hash_size - 1) {
2816 up_read(&kgnilnd_data.kgn_net_rw_sem);
2817 kgnilnd_base_shutdown();
2820 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %lld\n",
2821 libcfs_kmem_read());
2823 mutex_unlock(&kgnilnd_data.kgn_quiesce_mutex);
2827 static void __exit kgnilnd_exit(void)
2829 lnet_unregister_lnd(&the_kgnilnd);
2830 kgnilnd_proc_fini();
2831 kgnilnd_remove_sysctl();
2834 static int __init kgnilnd_init(void)
2838 rc = kgnilnd_tunables_init();
2842 LCONSOLE_INFO("Lustre: kgnilnd build version: "LUSTRE_VERSION_STRING"\n");
2844 kgnilnd_insert_sysctl();
2845 kgnilnd_proc_init();
2847 rc = libcfs_setup();
2851 lnet_register_lnd(&the_kgnilnd);
2856 MODULE_AUTHOR("Cray, Inc. <nic@cray.com>");
2857 MODULE_DESCRIPTION("Gemini LNet Network Driver");
2858 MODULE_VERSION(LUSTRE_VERSION_STRING);
2859 MODULE_LICENSE("GPL");
2861 module_init(kgnilnd_init);
2862 module_exit(kgnilnd_exit);