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 #include <linux/module.h>
40 #include <linux/kernel.h>
42 #include <linux/string.h>
43 #include <linux/stat.h>
44 #include <linux/errno.h>
45 #include <linux/unistd.h>
46 #include <linux/uio.h>
48 #include <asm/system.h>
49 #include <asm/uaccess.h>
52 #include <linux/init.h>
54 #include <linux/file.h>
55 #include <linux/stat.h>
56 #include <linux/list.h>
57 #include <linux/kmod.h>
58 #include <linux/sysctl.h>
59 #include <linux/random.h>
65 #define DEBUG_SUBSYSTEM S_LND
67 #include <libcfs/libcfs.h>
68 #include <lnet/lnet.h>
69 #include <lnet/lib-lnet.h>
70 #include <lnet/lnet-sysctl.h>
71 #include <portals/p30.h>
72 #include <lnet/ptllnd.h> /* Depends on portals/p30.h */
75 * Define this to enable console debug logging
78 //#define PJK_DEBUGGING
81 # define PTLLND_N_SCHED cfs_num_online_cpus() /* # schedulers */
83 # define PTLLND_N_SCHED 1 /* # schedulers */
86 #define PTLLND_CREDIT_HIGHWATER ((*kptllnd_tunables.kptl_peertxcredits)-1)
87 /* when eagerly to return credits */
91 int *kptl_ntx; /* # tx descs to pre-allocate */
92 int *kptl_max_nodes; /* max # nodes all talking to me */
93 int *kptl_max_procs_per_node; /* max # processes per node */
94 int *kptl_checksum; /* checksum kptl_msg_t? */
95 int *kptl_timeout; /* comms timeout (seconds) */
96 int *kptl_portal; /* portal number */
97 int *kptl_pid; /* portals PID (self + kernel peers) */
98 int *kptl_rxb_npages; /* number of pages for rx buffer */
99 int *kptl_rxb_nspare; /* number of spare rx buffers */
100 int *kptl_credits; /* number of credits */
101 int *kptl_peertxcredits; /* number of peer tx credits */
102 int *kptl_peerrtrcredits; /* number of peer router credits */
103 int *kptl_max_msg_size; /* max immd message size*/
104 int *kptl_peer_hash_table_size; /* # slots in peer hash table */
105 int *kptl_reschedule_loops; /* scheduler yield loops */
106 int *kptl_ack_puts; /* make portals ack PUTs */
108 int *kptl_simulation_bitmap;/* simulation bitmap */
111 #if defined(CONFIG_SYSCTL) && !CFS_SYSFS_MODULE_PARM
112 cfs_sysctl_table_header_t *kptl_sysctl; /* sysctl interface */
116 #include "lnet/ptllnd_wire.h"
118 /***********************************************************************/
120 typedef struct kptl_data kptl_data_t;
121 typedef struct kptl_net kptl_net_t;
122 typedef struct kptl_rx_buffer kptl_rx_buffer_t;
123 typedef struct kptl_peer kptl_peer_t;
129 #define PTLLND_EVENTARG_TYPE_MSG 0x1
130 #define PTLLND_EVENTARG_TYPE_RDMA 0x2
131 #define PTLLND_EVENTARG_TYPE_BUF 0x3
133 typedef struct kptl_rx /* receive message */
135 cfs_list_t rx_list; /* queue for attention */
136 kptl_rx_buffer_t *rx_rxb; /* the rx buffer pointer */
137 kptl_msg_t *rx_msg; /* received message */
138 int rx_nob; /* received message size */
139 unsigned long rx_treceived; /* time received */
140 ptl_process_id_t rx_initiator; /* sender's address */
141 kptl_peer_t *rx_peer; /* pointer to peer */
142 char rx_space[0]; /* copy of incoming request */
145 #define PTLLND_POSTRX_DONT_POST 0 /* don't post */
146 #define PTLLND_POSTRX_NO_CREDIT 1 /* post: no credits */
147 #define PTLLND_POSTRX_PEER_CREDIT 2 /* post: give peer back 1 credit */
149 typedef struct kptl_rx_buffer_pool
151 cfs_spinlock_t rxbp_lock;
152 cfs_list_t rxbp_list; /* all allocated buffers */
153 int rxbp_count; /* # allocated buffers */
154 int rxbp_reserved; /* # requests to buffer */
155 int rxbp_shutdown; /* shutdown flag */
156 } kptl_rx_buffer_pool_t;
158 struct kptl_rx_buffer
160 kptl_rx_buffer_pool_t *rxb_pool;
161 cfs_list_t rxb_list; /* for the rxb_pool list */
162 cfs_list_t rxb_repost_list;/* for the kptl_sched_rxbq list */
163 int rxb_posted:1; /* on the net */
164 int rxb_idle:1; /* all done */
165 kptl_eventarg_t rxb_eventarg; /* event->md.user_ptr */
166 int rxb_refcount; /* reference count */
167 ptl_handle_md_t rxb_mdh; /* the portals memory descriptor (MD) handle */
168 char *rxb_buffer; /* the buffer */
174 TX_TYPE_RESERVED = 0,
175 TX_TYPE_SMALL_MESSAGE = 1,
176 TX_TYPE_PUT_REQUEST = 2,
177 TX_TYPE_GET_REQUEST = 3,
178 TX_TYPE_PUT_RESPONSE = 4,
179 TX_TYPE_GET_RESPONSE = 5,
183 #ifdef _USING_LUSTRE_PORTALS_
184 struct iovec iov[PTL_MD_MAX_IOV];
185 lnet_kiov_t kiov[PTL_MD_MAX_IOV];
187 ptl_md_iovec_t iov[PTL_MD_MAX_IOV];
191 typedef struct kptl_tx /* transmit message */
193 cfs_list_t tx_list; /* queue on idle_txs etc */
194 cfs_atomic_t tx_refcount; /* reference count*/
195 enum kptl_tx_type tx_type; /* small msg/{put,get}{req,resp} */
196 int tx_active:1; /* queued on the peer */
197 int tx_idle:1; /* on the free list */
198 int tx_acked:1; /* portals ACK wanted (for debug only) */
199 kptl_eventarg_t tx_msg_eventarg; /* event->md.user_ptr */
200 kptl_eventarg_t tx_rdma_eventarg; /* event->md.user_ptr */
201 int tx_status; /* the status of this tx descriptor */
202 ptl_handle_md_t tx_rdma_mdh; /* RDMA buffer */
203 ptl_handle_md_t tx_msg_mdh; /* the portals MD handle for the initial message */
204 lnet_msg_t *tx_lnet_msg; /* LNET message to finalize */
205 lnet_msg_t *tx_lnet_replymsg; /* LNET reply message to finalize */
206 kptl_msg_t *tx_msg; /* the message data */
207 kptl_peer_t *tx_peer; /* the peer this is waiting on */
208 unsigned long tx_deadline; /* deadline */
209 unsigned long tx_tposted; /* time posted */
210 ptl_md_t tx_rdma_md; /* rdma descriptor */
211 kptl_fragvec_t *tx_frags; /* buffer fragments */
214 enum kptllnd_peer_state
216 PEER_STATE_UNINITIALIZED = 0,
217 PEER_STATE_ALLOCATED = 1,
218 PEER_STATE_WAITING_HELLO = 2,
219 PEER_STATE_ACTIVE = 3,
220 PEER_STATE_CLOSING = 4,
221 PEER_STATE_ZOMBIE = 5,
226 cfs_list_t peer_list;
227 cfs_atomic_t peer_refcount; /* The current references */
228 enum kptllnd_peer_state peer_state;
229 cfs_spinlock_t peer_lock; /* serialize */
230 cfs_list_t peer_noops; /* PTLLND_MSG_TYPE_NOOP txs */
231 cfs_list_t peer_sendq; /* txs waiting for mh handles */
232 cfs_list_t peer_activeq; /* txs awaiting completion */
233 lnet_process_id_t peer_id; /* Peer's LNET id */
234 ptl_process_id_t peer_ptlid; /* Peer's portals id */
235 __u64 peer_incarnation; /* peer's incarnation */
236 __u64 peer_myincarnation; /* my incarnation at HELLO */
237 int peer_sent_hello; /* have I sent HELLO? */
238 int peer_credits; /* number of send credits */
239 int peer_outstanding_credits;/* number of peer credits to return */
240 int peer_sent_credits; /* #msg buffers posted for peer */
241 int peer_max_msg_size; /* peer's rx buffer size */
242 int peer_error; /* errno on closing this peer */
243 int peer_retry_noop; /* need to retry returning credits */
244 int peer_check_stamp; /* watchdog check stamp */
245 cfs_time_t peer_last_alive; /* when (in jiffies) I was last alive */
246 __u64 peer_next_matchbits; /* Next value to register RDMA from peer */
247 __u64 peer_last_matchbits_seen; /* last matchbits used to RDMA to peer */
252 int kptl_init; /* initialisation state */
253 volatile int kptl_shutdown; /* shut down? */
254 cfs_atomic_t kptl_nthreads; /* # live threads */
255 ptl_handle_ni_t kptl_nih; /* network inteface handle */
256 ptl_process_id_t kptl_portals_id; /* Portals ID of interface */
257 __u64 kptl_incarnation; /* which one am I */
258 ptl_handle_eq_t kptl_eqh; /* Event Queue (EQ) */
260 cfs_rwlock_t kptl_net_rw_lock; /* serialise... */
261 cfs_list_t kptl_nets; /* kptl_net instances */
263 cfs_spinlock_t kptl_sched_lock; /* serialise... */
264 cfs_waitq_t kptl_sched_waitq; /* schedulers sleep here */
265 cfs_list_t kptl_sched_txq; /* tx requiring attention */
266 cfs_list_t kptl_sched_rxq; /* rx requiring attention */
267 cfs_list_t kptl_sched_rxbq; /* rxb requiring reposting */
269 cfs_waitq_t kptl_watchdog_waitq; /* watchdog sleeps here */
271 kptl_rx_buffer_pool_t kptl_rx_buffer_pool; /* rx buffer pool */
272 cfs_mem_cache_t* kptl_rx_cache; /* rx descripter cache */
274 cfs_atomic_t kptl_ntx; /* # tx descs allocated */
275 cfs_spinlock_t kptl_tx_lock; /* serialise idle tx list*/
276 cfs_list_t kptl_idle_txs; /* idle tx descriptors */
278 cfs_rwlock_t kptl_peer_rw_lock; /* lock for peer table */
279 cfs_list_t *kptl_peers; /* hash table of all my known peers */
280 cfs_list_t kptl_closing_peers; /* peers being closed */
281 cfs_list_t kptl_zombie_peers; /* peers waiting for refs to drain */
282 int kptl_peer_hash_size; /* size of kptl_peers */
283 int kptl_npeers; /* # peers extant */
284 int kptl_n_active_peers; /* # active peers */
285 int kptl_expected_peers; /* # peers I can buffer HELLOs from */
287 kptl_msg_t *kptl_nak_msg; /* common NAK message */
288 cfs_spinlock_t kptl_ptlid2str_lock; /* serialise str ops */
293 cfs_list_t net_list; /* chain on kptl_data:: kptl_nets */
295 cfs_atomic_t net_refcount; /* # current references */
296 int net_shutdown; /* lnd_shutdown called */
301 PTLLND_INIT_NOTHING = 0,
306 extern kptl_tunables_t kptllnd_tunables;
307 extern kptl_data_t kptllnd_data;
309 static inline lnet_nid_t
310 kptllnd_ptl2lnetnid(lnet_nid_t ni_nid, ptl_nid_t ptl_nid)
312 #ifdef _USING_LUSTRE_PORTALS_
313 return LNET_MKNID(LNET_NIDNET(ni_nid), LNET_NIDADDR(ptl_nid));
315 return LNET_MKNID(LNET_NIDNET(ni_nid), ptl_nid);
319 static inline ptl_nid_t
320 kptllnd_lnet2ptlnid(lnet_nid_t lnet_nid)
322 #ifdef _USING_LUSTRE_PORTALS_
323 return LNET_MKNID(LNET_NIDNET(kptllnd_data.kptl_portals_id.nid),
324 LNET_NIDADDR(lnet_nid));
326 return LNET_NIDADDR(lnet_nid);
330 int kptllnd_startup(lnet_ni_t *ni);
331 void kptllnd_shutdown(lnet_ni_t *ni);
332 int kptllnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
333 void kptllnd_query (struct lnet_ni *ni, lnet_nid_t nid, cfs_time_t *when);
334 int kptllnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
335 int kptllnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg,
336 int delayed, unsigned int niov,
337 struct iovec *iov, lnet_kiov_t *kiov,
338 unsigned int offset, unsigned int mlen, unsigned int rlen);
339 int kptllnd_eager_recv(struct lnet_ni *ni, void *private,
340 lnet_msg_t *msg, void **new_privatep);
341 void kptllnd_eq_callback(ptl_event_t *evp);
342 int kptllnd_scheduler(void *arg);
343 int kptllnd_watchdog(void *arg);
344 int kptllnd_thread_start(int (*fn)(void *arg), void *arg);
345 int kptllnd_tunables_init(void);
346 void kptllnd_tunables_fini(void);
348 const char *kptllnd_evtype2str(int evtype);
349 const char *kptllnd_msgtype2str(int msgtype);
350 const char *kptllnd_errtype2str(int errtype);
353 kptllnd_eventarg2obj (kptl_eventarg_t *eva)
355 switch (eva->eva_type) {
358 case PTLLND_EVENTARG_TYPE_BUF:
359 return cfs_list_entry(eva, kptl_rx_buffer_t, rxb_eventarg);
360 case PTLLND_EVENTARG_TYPE_RDMA:
361 return cfs_list_entry(eva, kptl_tx_t, tx_rdma_eventarg);
362 case PTLLND_EVENTARG_TYPE_MSG:
363 return cfs_list_entry(eva, kptl_tx_t, tx_msg_eventarg);
368 * RX BUFFER SUPPORT FUNCTIONS
370 void kptllnd_rx_buffer_pool_init(kptl_rx_buffer_pool_t *rxbp);
371 void kptllnd_rx_buffer_pool_fini(kptl_rx_buffer_pool_t *rxbp);
372 int kptllnd_rx_buffer_pool_reserve(kptl_rx_buffer_pool_t *rxbp, int count);
373 void kptllnd_rx_buffer_pool_unreserve(kptl_rx_buffer_pool_t *rxbp, int count);
374 void kptllnd_rx_buffer_callback(ptl_event_t *ev);
375 void kptllnd_rx_buffer_post(kptl_rx_buffer_t *rxb);
378 kptllnd_rx_buffer_size(void)
380 return PAGE_SIZE * (*kptllnd_tunables.kptl_rxb_npages);
384 kptllnd_rx_buffer_addref(kptl_rx_buffer_t *rxb)
388 cfs_spin_lock_irqsave(&rxb->rxb_pool->rxbp_lock, flags);
390 cfs_spin_unlock_irqrestore(&rxb->rxb_pool->rxbp_lock, flags);
394 kptllnd_rx_buffer_decref_locked(kptl_rx_buffer_t *rxb)
396 if (--(rxb->rxb_refcount) == 0) {
397 cfs_spin_lock(&kptllnd_data.kptl_sched_lock);
399 cfs_list_add_tail(&rxb->rxb_repost_list,
400 &kptllnd_data.kptl_sched_rxbq);
401 cfs_waitq_signal(&kptllnd_data.kptl_sched_waitq);
403 cfs_spin_unlock(&kptllnd_data.kptl_sched_lock);
408 kptllnd_rx_buffer_decref(kptl_rx_buffer_t *rxb)
413 cfs_spin_lock_irqsave(&rxb->rxb_pool->rxbp_lock, flags);
414 count = --(rxb->rxb_refcount);
415 cfs_spin_unlock_irqrestore(&rxb->rxb_pool->rxbp_lock, flags);
418 kptllnd_rx_buffer_post(rxb);
422 * RX SUPPORT FUNCTIONS
424 void kptllnd_rx_parse(kptl_rx_t *rx);
425 void kptllnd_rx_done(kptl_rx_t *rx, int post_credit);
428 * PEER SUPPORT FUNCTIONS
430 int kptllnd_get_peer_info(int index,
431 lnet_process_id_t *id,
432 int *state, int *sent_hello,
433 int *refcount, __u64 *incarnation,
434 __u64 *next_matchbits, __u64 *last_matchbits_seen,
435 int *nsendq, int *nactiveq,
436 int *credits, int *outstanding_credits);
437 void kptllnd_peer_destroy(kptl_peer_t *peer);
438 int kptllnd_peer_del(lnet_process_id_t id);
439 void kptllnd_peer_close_locked(kptl_peer_t *peer, int why);
440 void kptllnd_peer_close(kptl_peer_t *peer, int why);
441 void kptllnd_handle_closing_peers(void);
442 int kptllnd_peer_connect(kptl_tx_t *tx, lnet_nid_t nid);
443 void kptllnd_peer_check_sends(kptl_peer_t *peer);
444 void kptllnd_peer_check_bucket(int idx, int stamp);
445 void kptllnd_tx_launch(kptl_peer_t *peer, kptl_tx_t *tx, int nfrag);
446 int kptllnd_find_target(kptl_net_t *net, lnet_process_id_t target,
447 kptl_peer_t **peerp);
448 kptl_peer_t *kptllnd_peer_handle_hello(kptl_net_t *net,
449 ptl_process_id_t initiator,
451 kptl_peer_t *kptllnd_id2peer_locked(lnet_process_id_t id);
452 void kptllnd_peer_alive(kptl_peer_t *peer);
455 kptllnd_peer_addref (kptl_peer_t *peer)
457 cfs_atomic_inc(&peer->peer_refcount);
461 kptllnd_peer_decref (kptl_peer_t *peer)
463 if (cfs_atomic_dec_and_test(&peer->peer_refcount))
464 kptllnd_peer_destroy(peer);
468 kptllnd_net_addref (kptl_net_t *net)
470 LASSERT (cfs_atomic_read(&net->net_refcount) > 0);
471 cfs_atomic_inc(&net->net_refcount);
475 kptllnd_net_decref (kptl_net_t *net)
477 LASSERT (cfs_atomic_read(&net->net_refcount) > 0);
478 cfs_atomic_dec(&net->net_refcount);
482 kptllnd_set_tx_peer(kptl_tx_t *tx, kptl_peer_t *peer)
484 LASSERT (tx->tx_peer == NULL);
486 kptllnd_peer_addref(peer);
490 static inline cfs_list_t *
491 kptllnd_nid2peerlist(lnet_nid_t nid)
493 /* Only one copy of peer state for all logical peers, so the net part
494 * of NIDs is ignored; e.g. A@ptl0 and A@ptl2 share peer state */
495 unsigned int hash = ((unsigned int)LNET_NIDADDR(nid)) %
496 kptllnd_data.kptl_peer_hash_size;
498 return &kptllnd_data.kptl_peers[hash];
501 static inline kptl_peer_t *
502 kptllnd_id2peer(lnet_process_id_t id)
507 cfs_read_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
508 peer = kptllnd_id2peer_locked(id);
509 cfs_read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
515 kptllnd_reserve_buffers(int n)
517 return kptllnd_rx_buffer_pool_reserve(&kptllnd_data.kptl_rx_buffer_pool,
522 kptllnd_peer_reserve_buffers(void)
524 return kptllnd_reserve_buffers(*kptllnd_tunables.kptl_peertxcredits);
528 kptllnd_peer_unreserve_buffers(void)
530 kptllnd_rx_buffer_pool_unreserve(&kptllnd_data.kptl_rx_buffer_pool,
531 *kptllnd_tunables.kptl_peertxcredits);
535 * TX SUPPORT FUNCTIONS
537 int kptllnd_setup_tx_descs(void);
538 void kptllnd_cleanup_tx_descs(void);
539 void kptllnd_tx_fini(kptl_tx_t *tx);
540 void kptllnd_cancel_txlist(cfs_list_t *peerq, cfs_list_t *txs);
541 void kptllnd_restart_txs(kptl_net_t *net, lnet_process_id_t id,
542 cfs_list_t *restarts);
543 kptl_tx_t *kptllnd_get_idle_tx(enum kptl_tx_type purpose);
544 void kptllnd_tx_callback(ptl_event_t *ev);
545 const char *kptllnd_tx_typestr(int type);
548 kptllnd_tx_addref(kptl_tx_t *tx)
550 cfs_atomic_inc(&tx->tx_refcount);
554 kptllnd_tx_decref(kptl_tx_t *tx)
556 LASSERT (!cfs_in_interrupt()); /* Thread context only */
558 if (cfs_atomic_dec_and_test(&tx->tx_refcount))
563 * MESSAGE SUPPORT FUNCTIONS
565 void kptllnd_init_msg(kptl_msg_t *msg, int type,
566 lnet_process_id_t target, int body_nob);
567 void kptllnd_msg_pack(kptl_msg_t *msg, kptl_peer_t *peer);
568 int kptllnd_msg_unpack(kptl_msg_t *msg, int nob);
571 * MISC SUPPORT FUNCTIONS
573 void kptllnd_init_rdma_md(kptl_tx_t *tx, unsigned int niov,
574 struct iovec *iov, lnet_kiov_t *kiov,
575 unsigned int offset, unsigned int nob);
576 char *kptllnd_ptlid2str(ptl_process_id_t id);
578 void kptllnd_init_ptltrace(void);
579 void kptllnd_dump_ptltrace(void);
582 #define SIMULATION_FAIL_TX_PUT_ALLOC 0 /* 0x00000001 */
583 #define SIMULATION_FAIL_TX_GET_ALLOC 1 /* 0x00000002 */
584 #define SIMULATION_FAIL_TX 2 /* 0x00000004 */
585 #define SIMULATION_FAIL_RX_ALLOC 3 /* 0x00000008 */
587 #define IS_SIMULATION_ENABLED(x) \
588 (((*kptllnd_tunables.kptl_simulation_bitmap) & 1<< SIMULATION_##x) != 0)
590 #define IS_SIMULATION_ENABLED(x) 0