1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/klnds/ptllnd/ptllnd.c
38 * Author: PJ Kirner <pjkirner@clusterfs.com>
45 .lnd_startup = kptllnd_startup,
46 .lnd_shutdown = kptllnd_shutdown,
47 .lnd_ctl = kptllnd_ctl,
48 .lnd_send = kptllnd_send,
49 .lnd_recv = kptllnd_recv,
50 .lnd_eager_recv = kptllnd_eager_recv,
53 kptl_data_t kptllnd_data;
56 kptllnd_ptlid2str(ptl_process_id_t id)
58 static char strs[64][32];
64 spin_lock_irqsave(&kptllnd_data.kptl_ptlid2str_lock, flags);
66 if (idx >= sizeof(strs)/sizeof(strs[0]))
68 spin_unlock_irqrestore(&kptllnd_data.kptl_ptlid2str_lock, flags);
70 snprintf(str, sizeof(strs[0]), FMT_PTLID, id.pid, id.nid);
75 kptllnd_assert_wire_constants (void)
77 /* Wire protocol assertions generated by 'wirecheck'
78 * running on Linux fedora 2.6.11-co-0.6.4 #1 Mon Jun 19 05:36:13 UTC 2006 i686 i686 i386 GNU
79 * with gcc version 4.1.1 20060525 (Red Hat 4.1.1-1) */
83 CLASSERT (PTL_RESERVED_MATCHBITS == 0x100);
84 CLASSERT (LNET_MSG_MATCHBITS == 0);
85 CLASSERT (PTLLND_MSG_MAGIC == 0x50746C4E);
86 CLASSERT (PTLLND_MSG_VERSION == 0x04);
87 CLASSERT (PTLLND_RDMA_OK == 0x00);
88 CLASSERT (PTLLND_RDMA_FAIL == 0x01);
89 CLASSERT (PTLLND_MSG_TYPE_INVALID == 0x00);
90 CLASSERT (PTLLND_MSG_TYPE_PUT == 0x01);
91 CLASSERT (PTLLND_MSG_TYPE_GET == 0x02);
92 CLASSERT (PTLLND_MSG_TYPE_IMMEDIATE == 0x03);
93 CLASSERT (PTLLND_MSG_TYPE_NOOP == 0x04);
94 CLASSERT (PTLLND_MSG_TYPE_HELLO == 0x05);
95 CLASSERT (PTLLND_MSG_TYPE_NAK == 0x06);
97 /* Checks for struct kptl_msg_t */
98 CLASSERT ((int)sizeof(kptl_msg_t) == 136);
99 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_magic) == 0);
100 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_magic) == 4);
101 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_version) == 4);
102 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_version) == 2);
103 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_type) == 6);
104 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_type) == 1);
105 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_credits) == 7);
106 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_credits) == 1);
107 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_nob) == 8);
108 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_nob) == 4);
109 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_cksum) == 12);
110 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_cksum) == 4);
111 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_srcnid) == 16);
112 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_srcnid) == 8);
113 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_srcstamp) == 24);
114 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_srcstamp) == 8);
115 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_dstnid) == 32);
116 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_dstnid) == 8);
117 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_dststamp) == 40);
118 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_dststamp) == 8);
119 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_srcpid) == 48);
120 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_srcpid) == 4);
121 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_dstpid) == 52);
122 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_dstpid) == 4);
123 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_u.immediate) == 56);
124 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_u.immediate) == 72);
125 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_u.rdma) == 56);
126 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_u.rdma) == 80);
127 CLASSERT ((int)offsetof(kptl_msg_t, ptlm_u.hello) == 56);
128 CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_u.hello) == 12);
130 /* Checks for struct kptl_immediate_msg_t */
131 CLASSERT ((int)sizeof(kptl_immediate_msg_t) == 72);
132 CLASSERT ((int)offsetof(kptl_immediate_msg_t, kptlim_hdr) == 0);
133 CLASSERT ((int)sizeof(((kptl_immediate_msg_t *)0)->kptlim_hdr) == 72);
134 CLASSERT ((int)offsetof(kptl_immediate_msg_t, kptlim_payload[13]) == 85);
135 CLASSERT ((int)sizeof(((kptl_immediate_msg_t *)0)->kptlim_payload[13]) == 1);
137 /* Checks for struct kptl_rdma_msg_t */
138 CLASSERT ((int)sizeof(kptl_rdma_msg_t) == 80);
139 CLASSERT ((int)offsetof(kptl_rdma_msg_t, kptlrm_hdr) == 0);
140 CLASSERT ((int)sizeof(((kptl_rdma_msg_t *)0)->kptlrm_hdr) == 72);
141 CLASSERT ((int)offsetof(kptl_rdma_msg_t, kptlrm_matchbits) == 72);
142 CLASSERT ((int)sizeof(((kptl_rdma_msg_t *)0)->kptlrm_matchbits) == 8);
144 /* Checks for struct kptl_hello_msg_t */
145 CLASSERT ((int)sizeof(kptl_hello_msg_t) == 12);
146 CLASSERT ((int)offsetof(kptl_hello_msg_t, kptlhm_matchbits) == 0);
147 CLASSERT ((int)sizeof(((kptl_hello_msg_t *)0)->kptlhm_matchbits) == 8);
148 CLASSERT ((int)offsetof(kptl_hello_msg_t, kptlhm_max_msg_size) == 8);
149 CLASSERT ((int)sizeof(((kptl_hello_msg_t *)0)->kptlhm_max_msg_size) == 4);
152 const char *kptllnd_evtype2str(int type)
154 #define DO_TYPE(x) case x: return #x;
157 DO_TYPE(PTL_EVENT_GET_START);
158 DO_TYPE(PTL_EVENT_GET_END);
159 DO_TYPE(PTL_EVENT_PUT_START);
160 DO_TYPE(PTL_EVENT_PUT_END);
161 DO_TYPE(PTL_EVENT_REPLY_START);
162 DO_TYPE(PTL_EVENT_REPLY_END);
163 DO_TYPE(PTL_EVENT_ACK);
164 DO_TYPE(PTL_EVENT_SEND_START);
165 DO_TYPE(PTL_EVENT_SEND_END);
166 DO_TYPE(PTL_EVENT_UNLINK);
168 return "<unknown event type>";
173 const char *kptllnd_msgtype2str(int type)
175 #define DO_TYPE(x) case x: return #x;
178 DO_TYPE(PTLLND_MSG_TYPE_INVALID);
179 DO_TYPE(PTLLND_MSG_TYPE_PUT);
180 DO_TYPE(PTLLND_MSG_TYPE_GET);
181 DO_TYPE(PTLLND_MSG_TYPE_IMMEDIATE);
182 DO_TYPE(PTLLND_MSG_TYPE_HELLO);
183 DO_TYPE(PTLLND_MSG_TYPE_NOOP);
184 DO_TYPE(PTLLND_MSG_TYPE_NAK);
186 return "<unknown msg type>";
191 const char *kptllnd_errtype2str(int type)
193 #define DO_TYPE(x) case x: return #x;
198 DO_TYPE(PTL_NO_SPACE);
199 DO_TYPE(PTL_ME_IN_USE);
200 DO_TYPE(PTL_NAL_FAILED);
201 DO_TYPE(PTL_NO_INIT);
202 DO_TYPE(PTL_IFACE_DUP);
203 DO_TYPE(PTL_IFACE_INVALID);
204 DO_TYPE(PTL_HANDLE_INVALID);
205 DO_TYPE(PTL_MD_INVALID);
206 DO_TYPE(PTL_ME_INVALID);
207 DO_TYPE(PTL_PROCESS_INVALID);
208 DO_TYPE(PTL_PT_INDEX_INVALID);
209 DO_TYPE(PTL_SR_INDEX_INVALID);
210 DO_TYPE(PTL_EQ_INVALID);
211 DO_TYPE(PTL_EQ_DROPPED);
212 DO_TYPE(PTL_EQ_EMPTY);
213 DO_TYPE(PTL_MD_NO_UPDATE);
215 DO_TYPE(PTL_AC_INDEX_INVALID);
216 DO_TYPE(PTL_MD_ILLEGAL);
217 DO_TYPE(PTL_ME_LIST_TOO_LONG);
218 DO_TYPE(PTL_MD_IN_USE);
219 DO_TYPE(PTL_NI_INVALID);
220 DO_TYPE(PTL_PID_INVALID);
221 DO_TYPE(PTL_PT_FULL);
222 DO_TYPE(PTL_VAL_FAILED);
223 DO_TYPE(PTL_NOT_IMPLEMENTED);
225 DO_TYPE(PTL_EQ_IN_USE);
226 DO_TYPE(PTL_PID_IN_USE);
227 DO_TYPE(PTL_INV_EQ_SIZE);
230 return "<unknown event type>";
236 kptllnd_cksum (void *ptr, int nob)
242 sum = ((sum << 1) | (sum >> 31)) + *c++;
244 /* ensure I don't return 0 (== no checksum) */
245 return (sum == 0) ? 1 : sum;
249 kptllnd_init_msg(kptl_msg_t *msg, int type, int body_nob)
251 msg->ptlm_type = type;
252 msg->ptlm_nob = (offsetof(kptl_msg_t, ptlm_u) + body_nob + 7) & ~7;
254 LASSERT(msg->ptlm_nob <= *kptllnd_tunables.kptl_max_msg_size);
258 kptllnd_msg_pack(kptl_msg_t *msg, kptl_peer_t *peer)
260 msg->ptlm_magic = PTLLND_MSG_MAGIC;
261 msg->ptlm_version = PTLLND_MSG_VERSION;
262 /* msg->ptlm_type Filled in kptllnd_init_msg() */
263 msg->ptlm_credits = peer->peer_outstanding_credits;
264 /* msg->ptlm_nob Filled in kptllnd_init_msg() */
266 msg->ptlm_srcnid = kptllnd_data.kptl_ni->ni_nid;
267 msg->ptlm_srcstamp = peer->peer_myincarnation;
268 msg->ptlm_dstnid = peer->peer_id.nid;
269 msg->ptlm_dststamp = peer->peer_incarnation;
270 msg->ptlm_srcpid = the_lnet.ln_pid;
271 msg->ptlm_dstpid = peer->peer_id.pid;
273 if (*kptllnd_tunables.kptl_checksum) {
274 /* NB ptlm_cksum zero while computing cksum */
275 msg->ptlm_cksum = kptllnd_cksum(msg,
276 offsetof(kptl_msg_t, ptlm_u));
281 kptllnd_msg_unpack(kptl_msg_t *msg, int nob)
283 const int hdr_size = offsetof(kptl_msg_t, ptlm_u);
288 /* 6 bytes are enough to have received magic + version */
290 CERROR("Very Short message: %d\n", nob);
295 * Determine if we need to flip
297 if (msg->ptlm_magic == PTLLND_MSG_MAGIC) {
299 } else if (msg->ptlm_magic == __swab32(PTLLND_MSG_MAGIC)) {
302 CERROR("Bad magic: %08x\n", msg->ptlm_magic);
306 msg_version = flip ? __swab16(msg->ptlm_version) : msg->ptlm_version;
308 if (msg_version != PTLLND_MSG_VERSION) {
309 CERROR("Bad version: got %04x expected %04x\n",
310 (__u32)msg_version, PTLLND_MSG_VERSION);
314 if (nob < hdr_size) {
315 CERROR("Short message: got %d, wanted at least %d\n",
320 /* checksum must be computed with
321 * 1) ptlm_cksum zero and
322 * 2) BEFORE anything gets modified/flipped
324 msg_cksum = flip ? __swab32(msg->ptlm_cksum) : msg->ptlm_cksum;
326 if (msg_cksum != 0 &&
327 msg_cksum != kptllnd_cksum(msg, hdr_size)) {
328 CERROR("Bad checksum\n");
332 msg->ptlm_version = msg_version;
333 msg->ptlm_cksum = msg_cksum;
336 /* These two are 1 byte long so we don't swap them
337 But check this assumtion*/
338 CLASSERT (sizeof(msg->ptlm_type) == 1);
339 CLASSERT (sizeof(msg->ptlm_credits) == 1);
340 /* src & dst stamps are opaque cookies */
341 __swab32s(&msg->ptlm_nob);
342 __swab64s(&msg->ptlm_srcnid);
343 __swab64s(&msg->ptlm_dstnid);
344 __swab32s(&msg->ptlm_srcpid);
345 __swab32s(&msg->ptlm_dstpid);
348 if (msg->ptlm_nob != nob) {
349 CERROR("msg_nob corrupt: got 0x%08x, wanted %08x\n",
354 switch(msg->ptlm_type)
356 case PTLLND_MSG_TYPE_PUT:
357 case PTLLND_MSG_TYPE_GET:
358 if (nob < hdr_size + sizeof(kptl_rdma_msg_t)) {
359 CERROR("Short rdma request: got %d, want %d\n",
360 nob, hdr_size + (int)sizeof(kptl_rdma_msg_t));
365 __swab64s(&msg->ptlm_u.rdma.kptlrm_matchbits);
367 if (msg->ptlm_u.rdma.kptlrm_matchbits < PTL_RESERVED_MATCHBITS) {
368 CERROR("Bad matchbits "LPX64"\n",
369 msg->ptlm_u.rdma.kptlrm_matchbits);
374 case PTLLND_MSG_TYPE_IMMEDIATE:
375 if (nob < offsetof(kptl_msg_t,
376 ptlm_u.immediate.kptlim_payload)) {
377 CERROR("Short immediate: got %d, want %d\n", nob,
378 (int)offsetof(kptl_msg_t,
379 ptlm_u.immediate.kptlim_payload));
385 case PTLLND_MSG_TYPE_NOOP:
386 case PTLLND_MSG_TYPE_NAK:
390 case PTLLND_MSG_TYPE_HELLO:
391 if (nob < hdr_size + sizeof(kptl_hello_msg_t)) {
392 CERROR("Short hello: got %d want %d\n",
393 nob, hdr_size + (int)sizeof(kptl_hello_msg_t));
397 __swab64s(&msg->ptlm_u.hello.kptlhm_matchbits);
398 __swab32s(&msg->ptlm_u.hello.kptlhm_max_msg_size);
403 CERROR("Bad message type: 0x%02x\n", (__u32)msg->ptlm_type);
411 kptllnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
413 struct libcfs_ioctl_data *data = arg;
416 CDEBUG(D_NET, ">>> kptllnd_ctl cmd=%u arg=%p\n", cmd, arg);
419 * Validate that the context block is actually
420 * pointing to this interface
422 LASSERT (ni == kptllnd_data.kptl_ni);
425 case IOC_LIBCFS_DEL_PEER: {
426 lnet_process_id_t id;
428 id.nid = data->ioc_nid;
429 id.pid = data->ioc_u32[1];
431 rc = kptllnd_peer_del(id);
435 case IOC_LIBCFS_GET_PEER: {
436 lnet_process_id_t id = {.nid = LNET_NID_ANY,
437 .pid = LNET_PID_ANY};
438 __u64 incarnation = 0;
439 __u64 next_matchbits = 0;
440 __u64 last_matchbits_seen = 0;
447 int outstanding_credits = 0;
449 rc = kptllnd_get_peer_info(data->ioc_count, &id,
451 &refcount, &incarnation,
452 &next_matchbits, &last_matchbits_seen,
454 &credits, &outstanding_credits);
456 data->ioc_nid = id.nid;
457 data->ioc_net = state;
458 data->ioc_flags = sent_hello;
459 data->ioc_count = refcount;
460 data->ioc_u64[0] = incarnation;
461 data->ioc_u32[0] = (__u32)next_matchbits;
462 data->ioc_u32[1] = (__u32)(next_matchbits >> 32);
463 data->ioc_u32[2] = (__u32)last_matchbits_seen;
464 data->ioc_u32[3] = (__u32)(last_matchbits_seen >> 32);
465 data->ioc_u32[4] = id.pid;
466 data->ioc_u32[5] = (nsendq << 16) | nactiveq;
467 data->ioc_u32[6] = (credits << 16) | outstanding_credits;
475 CDEBUG(D_NET, "<<< kptllnd_ctl rc=%d\n", rc);
480 kptllnd_startup (lnet_ni_t *ni)
488 LASSERT (ni->ni_lnd == &kptllnd_lnd);
490 if (kptllnd_data.kptl_init != PTLLND_INIT_NOTHING) {
491 CERROR("Only 1 instance supported\n");
495 if (*kptllnd_tunables.kptl_max_procs_per_node < 1) {
496 CERROR("max_procs_per_node must be >= 1\n");
500 if (*kptllnd_tunables.kptl_peercredits > PTLLND_MSG_MAX_CREDITS) {
501 CERROR("peercredits must be <= %d\n", PTLLND_MSG_MAX_CREDITS);
505 *kptllnd_tunables.kptl_max_msg_size &= ~7;
506 if (*kptllnd_tunables.kptl_max_msg_size < PTLLND_MIN_BUFFER_SIZE)
507 *kptllnd_tunables.kptl_max_msg_size = PTLLND_MIN_BUFFER_SIZE;
509 CLASSERT ((PTLLND_MIN_BUFFER_SIZE & 7) == 0);
510 CLASSERT (sizeof(kptl_msg_t) <= PTLLND_MIN_BUFFER_SIZE);
513 * zero pointers, flags etc
514 * put everything into a known state.
516 memset (&kptllnd_data, 0, sizeof (kptllnd_data));
517 kptllnd_data.kptl_eqh = PTL_INVALID_HANDLE;
518 kptllnd_data.kptl_nih = PTL_INVALID_HANDLE;
521 * Setup the sched locks/lists/waitq
523 spin_lock_init(&kptllnd_data.kptl_sched_lock);
524 init_waitqueue_head(&kptllnd_data.kptl_sched_waitq);
525 INIT_LIST_HEAD(&kptllnd_data.kptl_sched_txq);
526 INIT_LIST_HEAD(&kptllnd_data.kptl_sched_rxq);
527 INIT_LIST_HEAD(&kptllnd_data.kptl_sched_rxbq);
529 /* init kptl_ptlid2str_lock before any call to kptllnd_ptlid2str */
530 spin_lock_init(&kptllnd_data.kptl_ptlid2str_lock);
533 * Setup the tx locks/lists
535 spin_lock_init(&kptllnd_data.kptl_tx_lock);
536 INIT_LIST_HEAD(&kptllnd_data.kptl_idle_txs);
537 atomic_set(&kptllnd_data.kptl_ntx, 0);
540 * Uptick the module reference count
545 * Setup pointers between the ni and context data block
547 kptllnd_data.kptl_ni = ni;
548 ni->ni_data = &kptllnd_data;
553 ni->ni_maxtxcredits = *kptllnd_tunables.kptl_credits;
554 ni->ni_peertxcredits = *kptllnd_tunables.kptl_peercredits;
556 kptllnd_data.kptl_expected_peers =
557 *kptllnd_tunables.kptl_max_nodes *
558 *kptllnd_tunables.kptl_max_procs_per_node;
561 * Initialize the Network interface instance
562 * We use the default because we don't have any
563 * way to choose a better interface.
564 * Requested and actual limits are ignored.
567 #ifdef _USING_LUSTRE_PORTALS_
572 *kptllnd_tunables.kptl_pid, NULL, NULL,
573 &kptllnd_data.kptl_nih);
576 * Note: PTL_IFACE_DUP simply means that the requested
577 * interface was already inited and that we're sharing it.
580 if (ptl_rc != PTL_OK && ptl_rc != PTL_IFACE_DUP) {
581 CERROR ("PtlNIInit: error %s(%d)\n",
582 kptllnd_errtype2str(ptl_rc), ptl_rc);
587 /* NB eq size irrelevant if using a callback */
588 ptl_rc = PtlEQAlloc(kptllnd_data.kptl_nih,
590 kptllnd_eq_callback, /* handler callback */
591 &kptllnd_data.kptl_eqh); /* output handle */
592 if (ptl_rc != PTL_OK) {
593 CERROR("PtlEQAlloc failed %s(%d)\n",
594 kptllnd_errtype2str(ptl_rc), ptl_rc);
600 * Fetch the lower NID
602 ptl_rc = PtlGetId(kptllnd_data.kptl_nih,
603 &kptllnd_data.kptl_portals_id);
604 if (ptl_rc != PTL_OK) {
605 CERROR ("PtlGetID: error %s(%d)\n",
606 kptllnd_errtype2str(ptl_rc), ptl_rc);
611 if (kptllnd_data.kptl_portals_id.pid != *kptllnd_tunables.kptl_pid) {
612 /* The kernel ptllnd must have the expected PID */
613 CERROR("Unexpected PID: %u (%u expected)\n",
614 kptllnd_data.kptl_portals_id.pid,
615 *kptllnd_tunables.kptl_pid);
620 ni->ni_nid = kptllnd_ptl2lnetnid(kptllnd_data.kptl_portals_id.nid);
622 CDEBUG(D_NET, "ptl id=%s, lnet id=%s\n",
623 kptllnd_ptlid2str(kptllnd_data.kptl_portals_id),
624 libcfs_nid2str(ni->ni_nid));
626 /* Initialized the incarnation - it must be for-all-time unique, even
627 * accounting for the fact that we increment it when we disconnect a
628 * peer that's using it */
629 do_gettimeofday(&tv);
630 kptllnd_data.kptl_incarnation = (((__u64)tv.tv_sec) * 1000000) +
632 CDEBUG(D_NET, "Incarnation="LPX64"\n", kptllnd_data.kptl_incarnation);
635 * Allocate and setup the peer hash table
637 rwlock_init(&kptllnd_data.kptl_peer_rw_lock);
638 init_waitqueue_head(&kptllnd_data.kptl_watchdog_waitq);
639 INIT_LIST_HEAD(&kptllnd_data.kptl_closing_peers);
640 INIT_LIST_HEAD(&kptllnd_data.kptl_zombie_peers);
642 kptllnd_data.kptl_peer_hash_size =
643 *kptllnd_tunables.kptl_peer_hash_table_size;
644 LIBCFS_ALLOC(kptllnd_data.kptl_peers,
645 (kptllnd_data.kptl_peer_hash_size *
646 sizeof(struct list_head)));
647 if (kptllnd_data.kptl_peers == NULL) {
648 CERROR("Failed to allocate space for peer hash table size=%d\n",
649 kptllnd_data.kptl_peer_hash_size);
653 for (i = 0; i < kptllnd_data.kptl_peer_hash_size; i++)
654 INIT_LIST_HEAD(&kptllnd_data.kptl_peers[i]);
656 LIBCFS_ALLOC(kptllnd_data.kptl_nak_msg, offsetof(kptl_msg_t, ptlm_u));
657 if (kptllnd_data.kptl_nak_msg == NULL) {
658 CERROR("Can't allocate NAK msg\n");
662 memset(kptllnd_data.kptl_nak_msg, 0, offsetof(kptl_msg_t, ptlm_u));
663 kptllnd_init_msg(kptllnd_data.kptl_nak_msg, PTLLND_MSG_TYPE_NAK, 0);
664 kptllnd_data.kptl_nak_msg->ptlm_magic = PTLLND_MSG_MAGIC;
665 kptllnd_data.kptl_nak_msg->ptlm_version = PTLLND_MSG_VERSION;
666 kptllnd_data.kptl_nak_msg->ptlm_srcpid = the_lnet.ln_pid;
667 kptllnd_data.kptl_nak_msg->ptlm_srcnid = ni->ni_nid;
668 kptllnd_data.kptl_nak_msg->ptlm_srcstamp = kptllnd_data.kptl_incarnation;
669 kptllnd_data.kptl_nak_msg->ptlm_dstpid = LNET_PID_ANY;
670 kptllnd_data.kptl_nak_msg->ptlm_dstnid = LNET_NID_ANY;
672 kptllnd_rx_buffer_pool_init(&kptllnd_data.kptl_rx_buffer_pool);
674 kptllnd_data.kptl_rx_cache =
675 cfs_mem_cache_create("ptllnd_rx",
677 *kptllnd_tunables.kptl_max_msg_size,
680 if (kptllnd_data.kptl_rx_cache == NULL) {
681 CERROR("Can't create slab for RX descriptors\n");
686 /* lists/ptrs/locks initialised */
687 kptllnd_data.kptl_init = PTLLND_INIT_DATA;
689 /*****************************************************/
691 rc = kptllnd_setup_tx_descs();
693 CERROR("Can't pre-allocate %d TX descriptors: %d\n",
694 *kptllnd_tunables.kptl_ntx, rc);
698 /* Start the scheduler threads for handling incoming requests. No need
699 * to advance the state because this will be automatically cleaned up
700 * now that PTLNAT_INIT_DATA state has been entered */
701 CDEBUG(D_NET, "starting %d scheduler threads\n", PTLLND_N_SCHED);
702 for (i = 0; i < PTLLND_N_SCHED; i++) {
703 rc = kptllnd_thread_start(kptllnd_scheduler, (void *)((long)i));
705 CERROR("Can't spawn scheduler[%d]: %d\n", i, rc);
710 rc = kptllnd_thread_start(kptllnd_watchdog, NULL);
712 CERROR("Can't spawn watchdog: %d\n", rc);
716 /* Ensure that 'rxb_nspare' buffers can be off the net (being emptied)
717 * and we will still have enough buffers posted for all our peers */
718 spares = *kptllnd_tunables.kptl_rxb_nspare *
719 ((*kptllnd_tunables.kptl_rxb_npages * PAGE_SIZE)/
720 *kptllnd_tunables.kptl_max_msg_size);
722 /* reserve and post the buffers */
723 rc = kptllnd_rx_buffer_pool_reserve(&kptllnd_data.kptl_rx_buffer_pool,
724 kptllnd_data.kptl_expected_peers +
727 CERROR("Can't reserve RX Buffer pool: %d\n", rc);
731 /* flag everything initialised */
732 kptllnd_data.kptl_init = PTLLND_INIT_ALL;
734 /*****************************************************/
736 if (*kptllnd_tunables.kptl_checksum)
737 CWARN("Checksumming enabled\n");
739 CDEBUG(D_NET, "<<< kptllnd_startup SUCCESS\n");
743 CDEBUG(D_NET, "kptllnd_startup failed rc=%d\n", rc);
744 kptllnd_shutdown(ni);
749 kptllnd_shutdown (lnet_ni_t *ni)
753 lnet_process_id_t process_id;
756 CDEBUG(D_MALLOC, "before LND cleanup: kmem %d\n",
757 atomic_read (&libcfs_kmemory));
759 LASSERT (ni == kptllnd_data.kptl_ni);
761 switch (kptllnd_data.kptl_init) {
765 case PTLLND_INIT_ALL:
766 case PTLLND_INIT_DATA:
768 kptllnd_rx_buffer_pool_fini(&kptllnd_data.kptl_rx_buffer_pool);
769 LASSERT (list_empty(&kptllnd_data.kptl_sched_rxq));
770 LASSERT (list_empty(&kptllnd_data.kptl_sched_rxbq));
772 /* Hold peertable lock to interleave cleanly with peer birth/death */
773 write_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
775 LASSERT (kptllnd_data.kptl_shutdown == 0);
776 kptllnd_data.kptl_shutdown = 1; /* phase 1 == destroy peers */
778 /* no new peers possible now */
779 write_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock,
782 /* nuke all existing peers */
783 process_id.nid = LNET_NID_ANY;
784 process_id.pid = LNET_PID_ANY;
785 kptllnd_peer_del(process_id);
787 read_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
789 LASSERT (kptllnd_data.kptl_n_active_peers == 0);
792 while (kptllnd_data.kptl_npeers != 0) {
794 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
795 "Waiting for %d peers to terminate\n",
796 kptllnd_data.kptl_npeers);
798 read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock,
801 cfs_pause(cfs_time_seconds(1));
803 read_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock,
807 LASSERT(list_empty(&kptllnd_data.kptl_closing_peers));
808 LASSERT(list_empty(&kptllnd_data.kptl_zombie_peers));
809 LASSERT (kptllnd_data.kptl_peers != NULL);
810 for (i = 0; i < kptllnd_data.kptl_peer_hash_size; i++)
811 LASSERT (list_empty (&kptllnd_data.kptl_peers[i]));
813 read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
814 CDEBUG(D_NET, "All peers deleted\n");
816 /* Shutdown phase 2: kill the daemons... */
817 kptllnd_data.kptl_shutdown = 2;
821 while (atomic_read (&kptllnd_data.kptl_nthreads) != 0) {
822 /* Wake up all threads*/
823 wake_up_all(&kptllnd_data.kptl_sched_waitq);
824 wake_up_all(&kptllnd_data.kptl_watchdog_waitq);
827 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
828 "Waiting for %d threads to terminate\n",
829 atomic_read(&kptllnd_data.kptl_nthreads));
830 cfs_pause(cfs_time_seconds(1));
833 CDEBUG(D_NET, "All Threads stopped\n");
834 LASSERT(list_empty(&kptllnd_data.kptl_sched_txq));
836 kptllnd_cleanup_tx_descs();
838 /* Nothing here now, but libcfs might soon require
839 * us to explicitly destroy wait queues and semaphores
840 * that would be done here */
844 case PTLLND_INIT_NOTHING:
845 CDEBUG(D_NET, "PTLLND_INIT_NOTHING\n");
849 if (!PtlHandleIsEqual(kptllnd_data.kptl_eqh, PTL_INVALID_HANDLE)) {
850 prc = PtlEQFree(kptllnd_data.kptl_eqh);
852 CERROR("Error %s(%d) freeing portals EQ\n",
853 kptllnd_errtype2str(prc), prc);
856 if (!PtlHandleIsEqual(kptllnd_data.kptl_nih, PTL_INVALID_HANDLE)) {
857 prc = PtlNIFini(kptllnd_data.kptl_nih);
859 CERROR("Error %s(%d) finalizing portals NI\n",
860 kptllnd_errtype2str(prc), prc);
863 LASSERT (atomic_read(&kptllnd_data.kptl_ntx) == 0);
864 LASSERT (list_empty(&kptllnd_data.kptl_idle_txs));
866 if (kptllnd_data.kptl_rx_cache != NULL)
867 cfs_mem_cache_destroy(kptllnd_data.kptl_rx_cache);
869 if (kptllnd_data.kptl_peers != NULL)
870 LIBCFS_FREE (kptllnd_data.kptl_peers,
871 sizeof (struct list_head) *
872 kptllnd_data.kptl_peer_hash_size);
874 if (kptllnd_data.kptl_nak_msg != NULL)
875 LIBCFS_FREE (kptllnd_data.kptl_nak_msg,
876 offsetof(kptl_msg_t, ptlm_u));
878 memset(&kptllnd_data, 0, sizeof(kptllnd_data));
880 CDEBUG(D_MALLOC, "after LND cleanup: kmem %d\n",
881 atomic_read (&libcfs_kmemory));
887 kptllnd_module_init (void)
891 kptllnd_assert_wire_constants();
893 rc = kptllnd_tunables_init();
897 kptllnd_init_ptltrace();
899 lnet_register_lnd(&kptllnd_lnd);
905 kptllnd_module_fini (void)
907 lnet_unregister_lnd(&kptllnd_lnd);
908 kptllnd_tunables_fini();
911 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
912 MODULE_DESCRIPTION("Kernel Portals LND v1.00");
913 MODULE_LICENSE("GPL");
915 module_init(kptllnd_module_init);
916 module_exit(kptllnd_module_fini);