4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lnet/klnds/ptllnd/ptllnd.h
36 * Author: PJ Kirner <pjkirner@clusterfs.com>
39 #ifndef AUTOCONF_INCLUDED
40 #include <linux/config.h>
42 #include <linux/module.h>
43 #include <linux/kernel.h>
45 #include <linux/string.h>
46 #include <linux/stat.h>
47 #include <linux/errno.h>
48 #include <linux/smp_lock.h>
49 #include <linux/unistd.h>
50 #include <linux/uio.h>
52 #include <asm/system.h>
53 #include <asm/uaccess.h>
56 #include <linux/init.h>
58 #include <linux/file.h>
59 #include <linux/stat.h>
60 #include <linux/list.h>
61 #include <linux/kmod.h>
62 #include <linux/sysctl.h>
63 #include <linux/random.h>
69 #define DEBUG_SUBSYSTEM S_LND
71 #include <libcfs/libcfs.h>
72 #include <lnet/lnet.h>
73 #include <lnet/lib-lnet.h>
74 #include <lnet/lnet-sysctl.h>
75 #include <portals/p30.h>
77 #include <portals/ptltrace.h>
79 #include <lnet/ptllnd.h> /* Depends on portals/p30.h */
82 * Define this to enable console debug logging
85 //#define PJK_DEBUGGING
88 # define PTLLND_N_SCHED cfs_num_online_cpus() /* # schedulers */
90 # define PTLLND_N_SCHED 1 /* # schedulers */
93 #define PTLLND_CREDIT_HIGHWATER ((*kptllnd_tunables.kptl_peertxcredits)-1)
94 /* when eagerly to return credits */
98 int *kptl_ntx; /* # tx descs to pre-allocate */
99 int *kptl_max_nodes; /* max # nodes all talking to me */
100 int *kptl_max_procs_per_node; /* max # processes per node */
101 int *kptl_checksum; /* checksum kptl_msg_t? */
102 int *kptl_timeout; /* comms timeout (seconds) */
103 int *kptl_portal; /* portal number */
104 int *kptl_pid; /* portals PID (self + kernel peers) */
105 int *kptl_rxb_npages; /* number of pages for rx buffer */
106 int *kptl_rxb_nspare; /* number of spare rx buffers */
107 int *kptl_credits; /* number of credits */
108 int *kptl_peertxcredits; /* number of peer tx credits */
109 int *kptl_peerrtrcredits; /* number of peer router credits */
110 int *kptl_max_msg_size; /* max immd message size*/
111 int *kptl_peer_hash_table_size; /* # slots in peer hash table */
112 int *kptl_reschedule_loops; /* scheduler yield loops */
113 int *kptl_ack_puts; /* make portals ack PUTs */
115 int *kptl_ptltrace_on_timeout; /* dump pltrace on timeout? */
116 int *kptl_ptltrace_on_fail; /* dump pltrace on PTL_NAL_FAILED? */
117 char **kptl_ptltrace_basename; /* ptltrace dump file basename */
120 int *kptl_simulation_bitmap;/* simulation bitmap */
123 #if defined(CONFIG_SYSCTL) && !CFS_SYSFS_MODULE_PARM
124 cfs_sysctl_table_header_t *kptl_sysctl; /* sysctl interface */
128 #include "lnet/ptllnd_wire.h"
130 /***********************************************************************/
132 typedef struct kptl_data kptl_data_t;
133 typedef struct kptl_net kptl_net_t;
134 typedef struct kptl_rx_buffer kptl_rx_buffer_t;
135 typedef struct kptl_peer kptl_peer_t;
141 #define PTLLND_EVENTARG_TYPE_MSG 0x1
142 #define PTLLND_EVENTARG_TYPE_RDMA 0x2
143 #define PTLLND_EVENTARG_TYPE_BUF 0x3
145 typedef struct kptl_rx /* receive message */
147 cfs_list_t rx_list; /* queue for attention */
148 kptl_rx_buffer_t *rx_rxb; /* the rx buffer pointer */
149 kptl_msg_t *rx_msg; /* received message */
150 int rx_nob; /* received message size */
151 unsigned long rx_treceived; /* time received */
152 ptl_process_id_t rx_initiator; /* sender's address */
154 ptl_uid_t rx_uid; /* sender's uid */
156 kptl_peer_t *rx_peer; /* pointer to peer */
157 char rx_space[0]; /* copy of incoming request */
160 #define PTLLND_POSTRX_DONT_POST 0 /* don't post */
161 #define PTLLND_POSTRX_NO_CREDIT 1 /* post: no credits */
162 #define PTLLND_POSTRX_PEER_CREDIT 2 /* post: give peer back 1 credit */
164 typedef struct kptl_rx_buffer_pool
166 cfs_spinlock_t rxbp_lock;
167 cfs_list_t rxbp_list; /* all allocated buffers */
168 int rxbp_count; /* # allocated buffers */
169 int rxbp_reserved; /* # requests to buffer */
170 int rxbp_shutdown; /* shutdown flag */
171 } kptl_rx_buffer_pool_t;
173 struct kptl_rx_buffer
175 kptl_rx_buffer_pool_t *rxb_pool;
176 cfs_list_t rxb_list; /* for the rxb_pool list */
177 cfs_list_t rxb_repost_list;/* for the kptl_sched_rxbq list */
178 int rxb_posted:1; /* on the net */
179 int rxb_idle:1; /* all done */
180 kptl_eventarg_t rxb_eventarg; /* event->md.user_ptr */
181 int rxb_refcount; /* reference count */
182 ptl_handle_md_t rxb_mdh; /* the portals memory descriptor (MD) handle */
183 char *rxb_buffer; /* the buffer */
189 TX_TYPE_RESERVED = 0,
190 TX_TYPE_SMALL_MESSAGE = 1,
191 TX_TYPE_PUT_REQUEST = 2,
192 TX_TYPE_GET_REQUEST = 3,
193 TX_TYPE_PUT_RESPONSE = 4,
194 TX_TYPE_GET_RESPONSE = 5,
198 #ifdef _USING_LUSTRE_PORTALS_
199 struct iovec iov[PTL_MD_MAX_IOV];
200 lnet_kiov_t kiov[PTL_MD_MAX_IOV];
202 ptl_md_iovec_t iov[PTL_MD_MAX_IOV];
206 typedef struct kptl_tx /* transmit message */
208 cfs_list_t tx_list; /* queue on idle_txs etc */
209 cfs_atomic_t tx_refcount; /* reference count*/
210 enum kptl_tx_type tx_type; /* small msg/{put,get}{req,resp} */
211 int tx_active:1; /* queued on the peer */
212 int tx_idle:1; /* on the free list */
213 int tx_acked:1; /* portals ACK wanted (for debug only) */
214 kptl_eventarg_t tx_msg_eventarg; /* event->md.user_ptr */
215 kptl_eventarg_t tx_rdma_eventarg; /* event->md.user_ptr */
216 int tx_status; /* the status of this tx descriptor */
217 ptl_handle_md_t tx_rdma_mdh; /* RDMA buffer */
218 ptl_handle_md_t tx_msg_mdh; /* the portals MD handle for the initial message */
219 lnet_msg_t *tx_lnet_msg; /* LNET message to finalize */
220 lnet_msg_t *tx_lnet_replymsg; /* LNET reply message to finalize */
221 kptl_msg_t *tx_msg; /* the message data */
222 kptl_peer_t *tx_peer; /* the peer this is waiting on */
223 unsigned long tx_deadline; /* deadline */
224 unsigned long tx_tposted; /* time posted */
225 ptl_md_t tx_rdma_md; /* rdma descriptor */
226 kptl_fragvec_t *tx_frags; /* buffer fragments */
229 enum kptllnd_peer_state
231 PEER_STATE_UNINITIALIZED = 0,
232 PEER_STATE_ALLOCATED = 1,
233 PEER_STATE_WAITING_HELLO = 2,
234 PEER_STATE_ACTIVE = 3,
235 PEER_STATE_CLOSING = 4,
236 PEER_STATE_ZOMBIE = 5,
241 cfs_list_t peer_list;
242 cfs_atomic_t peer_refcount; /* The current references */
243 enum kptllnd_peer_state peer_state;
244 cfs_spinlock_t peer_lock; /* serialize */
245 cfs_list_t peer_noops; /* PTLLND_MSG_TYPE_NOOP txs */
246 cfs_list_t peer_sendq; /* txs waiting for mh handles */
247 cfs_list_t peer_activeq; /* txs awaiting completion */
248 lnet_process_id_t peer_id; /* Peer's LNET id */
249 ptl_process_id_t peer_ptlid; /* Peer's portals id */
250 __u64 peer_incarnation; /* peer's incarnation */
251 __u64 peer_myincarnation; /* my incarnation at HELLO */
252 int peer_sent_hello; /* have I sent HELLO? */
253 int peer_credits; /* number of send credits */
254 int peer_outstanding_credits;/* number of peer credits to return */
255 int peer_sent_credits; /* #msg buffers posted for peer */
256 int peer_max_msg_size; /* peer's rx buffer size */
257 int peer_error; /* errno on closing this peer */
258 int peer_retry_noop; /* need to retry returning credits */
259 int peer_check_stamp; /* watchdog check stamp */
260 cfs_time_t peer_last_alive; /* when (in jiffies) I was last alive */
261 __u64 peer_next_matchbits; /* Next value to register RDMA from peer */
262 __u64 peer_last_matchbits_seen; /* last matchbits used to RDMA to peer */
267 int kptl_init; /* initialisation state */
268 volatile int kptl_shutdown; /* shut down? */
269 cfs_atomic_t kptl_nthreads; /* # live threads */
270 ptl_handle_ni_t kptl_nih; /* network inteface handle */
271 ptl_process_id_t kptl_portals_id; /* Portals ID of interface */
272 __u64 kptl_incarnation; /* which one am I */
273 ptl_handle_eq_t kptl_eqh; /* Event Queue (EQ) */
275 cfs_rwlock_t kptl_net_rw_lock; /* serialise... */
276 cfs_list_t kptl_nets; /* kptl_net instances */
278 cfs_spinlock_t kptl_sched_lock; /* serialise... */
279 cfs_waitq_t kptl_sched_waitq; /* schedulers sleep here */
280 cfs_list_t kptl_sched_txq; /* tx requiring attention */
281 cfs_list_t kptl_sched_rxq; /* rx requiring attention */
282 cfs_list_t kptl_sched_rxbq; /* rxb requiring reposting */
284 cfs_waitq_t kptl_watchdog_waitq; /* watchdog sleeps here */
285 cfs_atomic_t kptl_needs_ptltrace; /* watchdog thread to dump ptltrace */
287 kptl_rx_buffer_pool_t kptl_rx_buffer_pool; /* rx buffer pool */
288 cfs_mem_cache_t* kptl_rx_cache; /* rx descripter cache */
290 cfs_atomic_t kptl_ntx; /* # tx descs allocated */
291 cfs_spinlock_t kptl_tx_lock; /* serialise idle tx list*/
292 cfs_list_t kptl_idle_txs; /* idle tx descriptors */
294 cfs_rwlock_t kptl_peer_rw_lock; /* lock for peer table */
295 cfs_list_t *kptl_peers; /* hash table of all my known peers */
296 cfs_list_t kptl_closing_peers; /* peers being closed */
297 cfs_list_t kptl_zombie_peers; /* peers waiting for refs to drain */
298 int kptl_peer_hash_size; /* size of kptl_peers */
299 int kptl_npeers; /* # peers extant */
300 int kptl_n_active_peers; /* # active peers */
301 int kptl_expected_peers; /* # peers I can buffer HELLOs from */
303 kptl_msg_t *kptl_nak_msg; /* common NAK message */
304 cfs_spinlock_t kptl_ptlid2str_lock; /* serialise str ops */
309 cfs_list_t net_list; /* chain on kptl_data:: kptl_nets */
311 cfs_atomic_t net_refcount; /* # current references */
312 int net_shutdown; /* lnd_shutdown called */
317 PTLLND_INIT_NOTHING = 0,
322 extern kptl_tunables_t kptllnd_tunables;
323 extern kptl_data_t kptllnd_data;
325 static inline lnet_nid_t
326 kptllnd_ptl2lnetnid(lnet_nid_t ni_nid, ptl_nid_t ptl_nid)
328 #ifdef _USING_LUSTRE_PORTALS_
329 return LNET_MKNID(LNET_NIDNET(ni_nid), LNET_NIDADDR(ptl_nid));
331 return LNET_MKNID(LNET_NIDNET(ni_nid), ptl_nid);
335 static inline ptl_nid_t
336 kptllnd_lnet2ptlnid(lnet_nid_t lnet_nid)
338 #ifdef _USING_LUSTRE_PORTALS_
339 return LNET_MKNID(LNET_NIDNET(kptllnd_data.kptl_portals_id.nid),
340 LNET_NIDADDR(lnet_nid));
342 return LNET_NIDADDR(lnet_nid);
347 kptllnd_schedule_ptltrace_dump (void)
350 if (*kptllnd_tunables.kptl_ptltrace_on_fail) {
351 cfs_atomic_inc(&kptllnd_data.kptl_needs_ptltrace);
352 cfs_waitq_signal(&kptllnd_data.kptl_watchdog_waitq);
357 int kptllnd_startup(lnet_ni_t *ni);
358 void kptllnd_shutdown(lnet_ni_t *ni);
359 int kptllnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
360 void kptllnd_query (struct lnet_ni *ni, lnet_nid_t nid, cfs_time_t *when);
361 int kptllnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
362 int kptllnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg,
363 int delayed, unsigned int niov,
364 struct iovec *iov, lnet_kiov_t *kiov,
365 unsigned int offset, unsigned int mlen, unsigned int rlen);
366 int kptllnd_eager_recv(struct lnet_ni *ni, void *private,
367 lnet_msg_t *msg, void **new_privatep);
368 void kptllnd_eq_callback(ptl_event_t *evp);
369 int kptllnd_scheduler(void *arg);
370 int kptllnd_watchdog(void *arg);
371 int kptllnd_thread_start(int (*fn)(void *arg), void *arg);
372 int kptllnd_tunables_init(void);
373 void kptllnd_tunables_fini(void);
375 const char *kptllnd_evtype2str(int evtype);
376 const char *kptllnd_msgtype2str(int msgtype);
377 const char *kptllnd_errtype2str(int errtype);
380 kptllnd_eventarg2obj (kptl_eventarg_t *eva)
382 switch (eva->eva_type) {
385 case PTLLND_EVENTARG_TYPE_BUF:
386 return cfs_list_entry(eva, kptl_rx_buffer_t, rxb_eventarg);
387 case PTLLND_EVENTARG_TYPE_RDMA:
388 return cfs_list_entry(eva, kptl_tx_t, tx_rdma_eventarg);
389 case PTLLND_EVENTARG_TYPE_MSG:
390 return cfs_list_entry(eva, kptl_tx_t, tx_msg_eventarg);
395 * RX BUFFER SUPPORT FUNCTIONS
397 void kptllnd_rx_buffer_pool_init(kptl_rx_buffer_pool_t *rxbp);
398 void kptllnd_rx_buffer_pool_fini(kptl_rx_buffer_pool_t *rxbp);
399 int kptllnd_rx_buffer_pool_reserve(kptl_rx_buffer_pool_t *rxbp, int count);
400 void kptllnd_rx_buffer_pool_unreserve(kptl_rx_buffer_pool_t *rxbp, int count);
401 void kptllnd_rx_buffer_callback(ptl_event_t *ev);
402 void kptllnd_rx_buffer_post(kptl_rx_buffer_t *rxb);
405 kptllnd_rx_buffer_size(void)
407 return PAGE_SIZE * (*kptllnd_tunables.kptl_rxb_npages);
411 kptllnd_rx_buffer_addref(kptl_rx_buffer_t *rxb)
415 cfs_spin_lock_irqsave(&rxb->rxb_pool->rxbp_lock, flags);
417 cfs_spin_unlock_irqrestore(&rxb->rxb_pool->rxbp_lock, flags);
421 kptllnd_rx_buffer_decref_locked(kptl_rx_buffer_t *rxb)
423 if (--(rxb->rxb_refcount) == 0) {
424 cfs_spin_lock(&kptllnd_data.kptl_sched_lock);
426 cfs_list_add_tail(&rxb->rxb_repost_list,
427 &kptllnd_data.kptl_sched_rxbq);
428 cfs_waitq_signal(&kptllnd_data.kptl_sched_waitq);
430 cfs_spin_unlock(&kptllnd_data.kptl_sched_lock);
435 kptllnd_rx_buffer_decref(kptl_rx_buffer_t *rxb)
440 cfs_spin_lock_irqsave(&rxb->rxb_pool->rxbp_lock, flags);
441 count = --(rxb->rxb_refcount);
442 cfs_spin_unlock_irqrestore(&rxb->rxb_pool->rxbp_lock, flags);
445 kptllnd_rx_buffer_post(rxb);
449 * RX SUPPORT FUNCTIONS
451 void kptllnd_rx_parse(kptl_rx_t *rx);
452 void kptllnd_rx_done(kptl_rx_t *rx, int post_credit);
455 * PEER SUPPORT FUNCTIONS
457 int kptllnd_get_peer_info(int index,
458 lnet_process_id_t *id,
459 int *state, int *sent_hello,
460 int *refcount, __u64 *incarnation,
461 __u64 *next_matchbits, __u64 *last_matchbits_seen,
462 int *nsendq, int *nactiveq,
463 int *credits, int *outstanding_credits);
464 void kptllnd_peer_destroy(kptl_peer_t *peer);
465 int kptllnd_peer_del(lnet_process_id_t id);
466 void kptllnd_peer_close_locked(kptl_peer_t *peer, int why);
467 void kptllnd_peer_close(kptl_peer_t *peer, int why);
468 void kptllnd_handle_closing_peers(void);
469 int kptllnd_peer_connect(kptl_tx_t *tx, lnet_nid_t nid);
470 void kptllnd_peer_check_sends(kptl_peer_t *peer);
471 void kptllnd_peer_check_bucket(int idx, int stamp);
472 void kptllnd_tx_launch(kptl_peer_t *peer, kptl_tx_t *tx, int nfrag);
473 int kptllnd_find_target(kptl_net_t *net, lnet_process_id_t target,
474 kptl_peer_t **peerp);
475 kptl_peer_t *kptllnd_peer_handle_hello(kptl_net_t *net,
476 ptl_process_id_t initiator,
478 kptl_peer_t *kptllnd_id2peer_locked(lnet_process_id_t id);
479 void kptllnd_peer_alive(kptl_peer_t *peer);
482 kptllnd_peer_addref (kptl_peer_t *peer)
484 cfs_atomic_inc(&peer->peer_refcount);
488 kptllnd_peer_decref (kptl_peer_t *peer)
490 if (cfs_atomic_dec_and_test(&peer->peer_refcount))
491 kptllnd_peer_destroy(peer);
495 kptllnd_net_addref (kptl_net_t *net)
497 LASSERT (cfs_atomic_read(&net->net_refcount) > 0);
498 cfs_atomic_inc(&net->net_refcount);
502 kptllnd_net_decref (kptl_net_t *net)
504 LASSERT (cfs_atomic_read(&net->net_refcount) > 0);
505 cfs_atomic_dec(&net->net_refcount);
509 kptllnd_set_tx_peer(kptl_tx_t *tx, kptl_peer_t *peer)
511 LASSERT (tx->tx_peer == NULL);
513 kptllnd_peer_addref(peer);
517 static inline cfs_list_t *
518 kptllnd_nid2peerlist(lnet_nid_t nid)
520 /* Only one copy of peer state for all logical peers, so the net part
521 * of NIDs is ignored; e.g. A@ptl0 and A@ptl2 share peer state */
522 unsigned int hash = ((unsigned int)LNET_NIDADDR(nid)) %
523 kptllnd_data.kptl_peer_hash_size;
525 return &kptllnd_data.kptl_peers[hash];
528 static inline kptl_peer_t *
529 kptllnd_id2peer(lnet_process_id_t id)
534 cfs_read_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
535 peer = kptllnd_id2peer_locked(id);
536 cfs_read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
542 kptllnd_reserve_buffers(int n)
544 return kptllnd_rx_buffer_pool_reserve(&kptllnd_data.kptl_rx_buffer_pool,
549 kptllnd_peer_reserve_buffers(void)
551 return kptllnd_reserve_buffers(*kptllnd_tunables.kptl_peertxcredits);
555 kptllnd_peer_unreserve_buffers(void)
557 kptllnd_rx_buffer_pool_unreserve(&kptllnd_data.kptl_rx_buffer_pool,
558 *kptllnd_tunables.kptl_peertxcredits);
562 * TX SUPPORT FUNCTIONS
564 int kptllnd_setup_tx_descs(void);
565 void kptllnd_cleanup_tx_descs(void);
566 void kptllnd_tx_fini(kptl_tx_t *tx);
567 void kptllnd_cancel_txlist(cfs_list_t *peerq, cfs_list_t *txs);
568 void kptllnd_restart_txs(kptl_net_t *net, lnet_process_id_t id,
569 cfs_list_t *restarts);
570 kptl_tx_t *kptllnd_get_idle_tx(enum kptl_tx_type purpose);
571 void kptllnd_tx_callback(ptl_event_t *ev);
572 const char *kptllnd_tx_typestr(int type);
575 kptllnd_tx_addref(kptl_tx_t *tx)
577 cfs_atomic_inc(&tx->tx_refcount);
581 kptllnd_tx_decref(kptl_tx_t *tx)
583 LASSERT (!cfs_in_interrupt()); /* Thread context only */
585 if (cfs_atomic_dec_and_test(&tx->tx_refcount))
590 * MESSAGE SUPPORT FUNCTIONS
592 void kptllnd_init_msg(kptl_msg_t *msg, int type,
593 lnet_process_id_t target, int body_nob);
594 void kptllnd_msg_pack(kptl_msg_t *msg, kptl_peer_t *peer);
595 int kptllnd_msg_unpack(kptl_msg_t *msg, int nob);
598 * MISC SUPPORT FUNCTIONS
600 void kptllnd_init_rdma_md(kptl_tx_t *tx, unsigned int niov,
601 struct iovec *iov, lnet_kiov_t *kiov,
602 unsigned int offset, unsigned int nob);
603 char *kptllnd_ptlid2str(ptl_process_id_t id);
605 void kptllnd_init_ptltrace(void);
606 void kptllnd_dump_ptltrace(void);
609 #define SIMULATION_FAIL_TX_PUT_ALLOC 0 /* 0x00000001 */
610 #define SIMULATION_FAIL_TX_GET_ALLOC 1 /* 0x00000002 */
611 #define SIMULATION_FAIL_TX 2 /* 0x00000004 */
612 #define SIMULATION_FAIL_RX_ALLOC 3 /* 0x00000008 */
614 #define IS_SIMULATION_ENABLED(x) \
615 (((*kptllnd_tunables.kptl_simulation_bitmap) & 1<< SIMULATION_##x) != 0)
617 #define IS_SIMULATION_ENABLED(x) 0