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.h
38 * Author: PJ Kirner <pjkirner@clusterfs.com>
42 # define EXPORT_SYMTAB
44 #ifndef AUTOCONF_INCLUDED
45 #include <linux/config.h>
47 #include <linux/module.h>
48 #include <linux/kernel.h>
50 #include <linux/string.h>
51 #include <linux/stat.h>
52 #include <linux/errno.h>
53 #include <linux/smp_lock.h>
54 #include <linux/unistd.h>
55 #include <linux/uio.h>
57 #include <asm/system.h>
58 #include <asm/uaccess.h>
61 #include <linux/init.h>
63 #include <linux/file.h>
64 #include <linux/stat.h>
65 #include <linux/list.h>
66 #include <linux/kmod.h>
67 #include <linux/sysctl.h>
68 #include <linux/random.h>
74 #define DEBUG_SUBSYSTEM S_LND
76 #include <libcfs/libcfs.h>
77 #include <lnet/lnet.h>
78 #include <lnet/lib-lnet.h>
79 #include <portals/p30.h>
81 #include <portals/ptltrace.h>
83 #include <lnet/ptllnd.h> /* Depends on portals/p30.h */
86 * Define this to enable console debug logging
89 //#define PJK_DEBUGGING
92 # define PTLLND_N_SCHED num_online_cpus() /* # schedulers */
94 # define PTLLND_N_SCHED 1 /* # schedulers */
97 #define PTLLND_CREDIT_HIGHWATER ((*kptllnd_tunables.kptl_peercredits)-1)
98 /* when eagerly to return credits */
102 int *kptl_ntx; /* # tx descs to pre-allocate */
103 int *kptl_max_nodes; /* max # nodes all talking to me */
104 int *kptl_max_procs_per_node; /* max # processes per node */
105 int *kptl_checksum; /* checksum kptl_msg_t? */
106 int *kptl_timeout; /* comms timeout (seconds) */
107 int *kptl_portal; /* portal number */
108 int *kptl_pid; /* portals PID (self + kernel peers) */
109 int *kptl_rxb_npages; /* number of pages for rx buffer */
110 int *kptl_rxb_nspare; /* number of spare rx buffers */
111 int *kptl_credits; /* number of credits */
112 int *kptl_peercredits; /* number of credits */
113 int *kptl_max_msg_size; /* max immd message size*/
114 int *kptl_peer_hash_table_size; /* # slots in peer hash table */
115 int *kptl_reschedule_loops; /* scheduler yield loops */
116 int *kptl_ack_puts; /* make portals ack PUTs */
118 int *kptl_ptltrace_on_timeout; /* dump pltrace on timeout? */
119 char **kptl_ptltrace_basename; /* ptltrace dump file basename */
122 int *kptl_simulation_bitmap;/* simulation bitmap */
125 #if defined(CONFIG_SYSCTL) && !CFS_SYSFS_MODULE_PARM
126 cfs_sysctl_table_header_t *kptl_sysctl; /* sysctl interface */
130 #include "lnet/ptllnd_wire.h"
132 /***********************************************************************/
134 typedef struct kptl_data kptl_data_t;
135 typedef struct kptl_rx_buffer kptl_rx_buffer_t;
136 typedef struct kptl_peer kptl_peer_t;
142 #define PTLLND_EVENTARG_TYPE_MSG 0x1
143 #define PTLLND_EVENTARG_TYPE_RDMA 0x2
144 #define PTLLND_EVENTARG_TYPE_BUF 0x3
146 typedef struct kptl_rx /* receive message */
148 struct list_head rx_list; /* queue for attention */
149 kptl_rx_buffer_t *rx_rxb; /* the rx buffer pointer */
150 kptl_msg_t *rx_msg; /* received message */
151 int rx_nob; /* received message size */
152 unsigned long rx_treceived; /* time received */
153 ptl_process_id_t rx_initiator; /* sender's address */
155 ptl_uid_t rx_uid; /* sender's uid */
157 kptl_peer_t *rx_peer; /* pointer to peer */
158 char rx_space[0]; /* copy of incoming request */
161 #define PTLLND_POSTRX_DONT_POST 0 /* don't post */
162 #define PTLLND_POSTRX_NO_CREDIT 1 /* post: no credits */
163 #define PTLLND_POSTRX_PEER_CREDIT 2 /* post: give peer back 1 credit */
165 typedef struct kptl_rx_buffer_pool
167 spinlock_t rxbp_lock;
168 struct list_head rxbp_list; /* all allocated buffers */
169 int rxbp_count; /* # allocated buffers */
170 int rxbp_reserved; /* # requests to buffer */
171 int rxbp_shutdown; /* shutdown flag */
172 } kptl_rx_buffer_pool_t;
174 struct kptl_rx_buffer
176 kptl_rx_buffer_pool_t *rxb_pool;
177 struct list_head rxb_list; /* for the rxb_pool list */
178 struct list_head rxb_repost_list;/* for the kptl_sched_rxbq list */
179 int rxb_posted:1; /* on the net */
180 int rxb_idle:1; /* all done */
181 kptl_eventarg_t rxb_eventarg; /* event->md.user_ptr */
182 int rxb_refcount; /* reference count */
183 ptl_handle_md_t rxb_mdh; /* the portals memory descriptor (MD) handle */
184 char *rxb_buffer; /* the buffer */
190 TX_TYPE_RESERVED = 0,
191 TX_TYPE_SMALL_MESSAGE = 1,
192 TX_TYPE_PUT_REQUEST = 2,
193 TX_TYPE_GET_REQUEST = 3,
194 TX_TYPE_PUT_RESPONSE = 4,
195 TX_TYPE_GET_RESPONSE = 5,
199 #ifdef _USING_LUSTRE_PORTALS_
200 struct iovec iov[PTL_MD_MAX_IOV];
201 lnet_kiov_t kiov[PTL_MD_MAX_IOV];
203 ptl_md_iovec_t iov[PTL_MD_MAX_IOV];
207 typedef struct kptl_tx /* transmit message */
209 struct list_head tx_list; /* queue on idle_txs etc */
210 atomic_t tx_refcount; /* reference count*/
211 enum kptl_tx_type tx_type; /* small msg/{put,get}{req,resp} */
212 int tx_active:1; /* queued on the peer */
213 int tx_idle:1; /* on the free list */
214 int tx_acked:1; /* portals ACK wanted (for debug only) */
215 kptl_eventarg_t tx_msg_eventarg; /* event->md.user_ptr */
216 kptl_eventarg_t tx_rdma_eventarg; /* event->md.user_ptr */
217 int tx_status; /* the status of this tx descriptor */
218 ptl_handle_md_t tx_rdma_mdh; /* RDMA buffer */
219 ptl_handle_md_t tx_msg_mdh; /* the portals MD handle for the initial message */
220 lnet_msg_t *tx_lnet_msg; /* LNET message to finalize */
221 lnet_msg_t *tx_lnet_replymsg; /* LNET reply message to finalize */
222 kptl_msg_t *tx_msg; /* the message data */
223 kptl_peer_t *tx_peer; /* the peer this is waiting on */
224 unsigned long tx_deadline; /* deadline */
225 unsigned long tx_tposted; /* time posted */
226 ptl_md_t tx_rdma_md; /* rdma descriptor */
227 kptl_fragvec_t *tx_frags; /* buffer fragments */
230 enum kptllnd_peer_state
232 PEER_STATE_UNINITIALIZED = 0,
233 PEER_STATE_ALLOCATED = 1,
234 PEER_STATE_WAITING_HELLO = 2,
235 PEER_STATE_ACTIVE = 3,
236 PEER_STATE_CLOSING = 4,
237 PEER_STATE_ZOMBIE = 5,
242 struct list_head peer_list;
243 atomic_t peer_refcount; /* The current refrences */
244 enum kptllnd_peer_state peer_state;
245 spinlock_t peer_lock; /* serialize */
246 struct list_head peer_noops; /* PTLLND_MSG_TYPE_NOOP txs */
247 struct list_head peer_sendq; /* txs waiting for mh handles */
248 struct list_head peer_activeq; /* txs awaiting completion */
249 lnet_process_id_t peer_id; /* Peer's LNET id */
250 ptl_process_id_t peer_ptlid; /* Peer's portals id */
251 __u64 peer_incarnation; /* peer's incarnation */
252 __u64 peer_myincarnation; /* my incarnation at HELLO */
253 int peer_sent_hello; /* have I sent HELLO? */
254 int peer_credits; /* number of send credits */
255 int peer_outstanding_credits;/* number of peer credits to return */
256 int peer_sent_credits; /* #msg buffers posted for peer */
257 int peer_max_msg_size; /* peer's rx buffer size */
258 int peer_error; /* errno on closing this peer */
259 int peer_retry_noop; /* need to retry returning credits */
260 int peer_check_stamp; /* watchdog check stamp */
261 cfs_time_t peer_last_alive; /* when (in jiffies) I was last alive */
262 __u64 peer_next_matchbits; /* Next value to register RDMA from peer */
263 __u64 peer_last_matchbits_seen; /* last matchbits used to RDMA to peer */
268 int kptl_init; /* initialisation state */
269 volatile int kptl_shutdown; /* shut down? */
270 atomic_t kptl_nthreads; /* # live threads */
271 lnet_ni_t *kptl_ni; /* _the_ LND instance */
272 ptl_handle_ni_t kptl_nih; /* network inteface handle */
273 ptl_process_id_t kptl_portals_id; /* Portals ID of interface */
274 __u64 kptl_incarnation; /* which one am I */
275 ptl_handle_eq_t kptl_eqh; /* Event Queue (EQ) */
277 spinlock_t kptl_sched_lock; /* serialise... */
278 wait_queue_head_t kptl_sched_waitq; /* schedulers sleep here */
279 struct list_head kptl_sched_txq; /* tx requiring attention */
280 struct list_head kptl_sched_rxq; /* rx requiring attention */
281 struct list_head kptl_sched_rxbq; /* rxb requiring reposting */
283 wait_queue_head_t kptl_watchdog_waitq; /* watchdog sleeps here */
285 kptl_rx_buffer_pool_t kptl_rx_buffer_pool; /* rx buffer pool */
286 cfs_mem_cache_t* kptl_rx_cache; /* rx descripter cache */
288 atomic_t kptl_ntx; /* # tx descs allocated */
289 spinlock_t kptl_tx_lock; /* serialise idle tx list*/
290 struct list_head kptl_idle_txs; /* idle tx descriptors */
292 rwlock_t kptl_peer_rw_lock; /* lock for peer table */
293 struct list_head *kptl_peers; /* hash table of all my known peers */
294 struct list_head kptl_closing_peers; /* peers being closed */
295 struct list_head kptl_zombie_peers; /* peers waiting for refs to drain */
296 int kptl_peer_hash_size; /* size of kptl_peers */
297 int kptl_npeers; /* # peers extant */
298 int kptl_n_active_peers; /* # active peers */
299 int kptl_expected_peers; /* # peers I can buffer HELLOs from */
301 kptl_msg_t *kptl_nak_msg; /* common NAK message */
302 spinlock_t kptl_ptlid2str_lock; /* serialise str ops */
307 PTLLND_INIT_NOTHING = 0,
312 extern kptl_tunables_t kptllnd_tunables;
313 extern kptl_data_t kptllnd_data;
315 static inline lnet_nid_t
316 kptllnd_ptl2lnetnid(ptl_nid_t ptl_nid)
318 #ifdef _USING_LUSTRE_PORTALS_
319 return LNET_MKNID(LNET_NIDNET(kptllnd_data.kptl_ni->ni_nid),
320 LNET_NIDADDR(ptl_nid));
322 return LNET_MKNID(LNET_NIDNET(kptllnd_data.kptl_ni->ni_nid),
327 static inline ptl_nid_t
328 kptllnd_lnet2ptlnid(lnet_nid_t lnet_nid)
330 #ifdef _USING_LUSTRE_PORTALS_
331 return LNET_MKNID(LNET_NIDNET(kptllnd_data.kptl_portals_id.nid),
332 LNET_NIDADDR(lnet_nid));
334 return LNET_NIDADDR(lnet_nid);
338 int kptllnd_startup(lnet_ni_t *ni);
339 void kptllnd_shutdown(lnet_ni_t *ni);
340 int kptllnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
341 int kptllnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
342 int kptllnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg,
343 int delayed, unsigned int niov,
344 struct iovec *iov, lnet_kiov_t *kiov,
345 unsigned int offset, unsigned int mlen, unsigned int rlen);
346 int kptllnd_eager_recv(struct lnet_ni *ni, void *private,
347 lnet_msg_t *msg, void **new_privatep);
348 void kptllnd_eq_callback(ptl_event_t *evp);
349 int kptllnd_scheduler(void *arg);
350 int kptllnd_watchdog(void *arg);
351 int kptllnd_thread_start(int (*fn)(void *arg), void *arg);
352 int kptllnd_tunables_init(void);
353 void kptllnd_tunables_fini(void);
355 const char *kptllnd_evtype2str(int evtype);
356 const char *kptllnd_msgtype2str(int msgtype);
357 const char *kptllnd_errtype2str(int errtype);
360 kptllnd_eventarg2obj (kptl_eventarg_t *eva)
362 switch (eva->eva_type) {
365 case PTLLND_EVENTARG_TYPE_BUF:
366 return list_entry(eva, kptl_rx_buffer_t, rxb_eventarg);
367 case PTLLND_EVENTARG_TYPE_RDMA:
368 return list_entry(eva, kptl_tx_t, tx_rdma_eventarg);
369 case PTLLND_EVENTARG_TYPE_MSG:
370 return list_entry(eva, kptl_tx_t, tx_msg_eventarg);
375 * RX BUFFER SUPPORT FUNCTIONS
377 void kptllnd_rx_buffer_pool_init(kptl_rx_buffer_pool_t *rxbp);
378 void kptllnd_rx_buffer_pool_fini(kptl_rx_buffer_pool_t *rxbp);
379 int kptllnd_rx_buffer_pool_reserve(kptl_rx_buffer_pool_t *rxbp, int count);
380 void kptllnd_rx_buffer_pool_unreserve(kptl_rx_buffer_pool_t *rxbp, int count);
381 void kptllnd_rx_buffer_callback(ptl_event_t *ev);
382 void kptllnd_rx_buffer_post(kptl_rx_buffer_t *rxb);
385 kptllnd_rx_buffer_size(void)
387 return PAGE_SIZE * (*kptllnd_tunables.kptl_rxb_npages);
391 kptllnd_rx_buffer_addref(kptl_rx_buffer_t *rxb)
395 spin_lock_irqsave(&rxb->rxb_pool->rxbp_lock, flags);
397 spin_unlock_irqrestore(&rxb->rxb_pool->rxbp_lock, flags);
401 kptllnd_rx_buffer_decref_locked(kptl_rx_buffer_t *rxb)
403 if (--(rxb->rxb_refcount) == 0) {
404 spin_lock(&kptllnd_data.kptl_sched_lock);
406 list_add_tail(&rxb->rxb_repost_list,
407 &kptllnd_data.kptl_sched_rxbq);
408 wake_up(&kptllnd_data.kptl_sched_waitq);
410 spin_unlock(&kptllnd_data.kptl_sched_lock);
415 kptllnd_rx_buffer_decref(kptl_rx_buffer_t *rxb)
420 spin_lock_irqsave(&rxb->rxb_pool->rxbp_lock, flags);
421 count = --(rxb->rxb_refcount);
422 spin_unlock_irqrestore(&rxb->rxb_pool->rxbp_lock, flags);
425 kptllnd_rx_buffer_post(rxb);
429 * RX SUPPORT FUNCTIONS
431 void kptllnd_rx_parse(kptl_rx_t *rx);
432 void kptllnd_rx_done(kptl_rx_t *rx, int post_credit);
435 * PEER SUPPORT FUNCTIONS
437 int kptllnd_get_peer_info(int index,
438 lnet_process_id_t *id,
439 int *state, int *sent_hello,
440 int *refcount, __u64 *incarnation,
441 __u64 *next_matchbits, __u64 *last_matchbits_seen,
442 int *nsendq, int *nactiveq,
443 int *credits, int *outstanding_credits);
444 void kptllnd_peer_destroy(kptl_peer_t *peer);
445 int kptllnd_peer_del(lnet_process_id_t id);
446 void kptllnd_peer_close_locked(kptl_peer_t *peer, int why);
447 void kptllnd_peer_close(kptl_peer_t *peer, int why);
448 void kptllnd_handle_closing_peers(void);
449 int kptllnd_peer_connect(kptl_tx_t *tx, lnet_nid_t nid);
450 void kptllnd_peer_check_sends(kptl_peer_t *peer);
451 void kptllnd_peer_check_bucket(int idx, int stamp);
452 void kptllnd_tx_launch(kptl_peer_t *peer, kptl_tx_t *tx, int nfrag);
453 int kptllnd_find_target(kptl_peer_t **peerp, lnet_process_id_t target);
454 kptl_peer_t *kptllnd_peer_handle_hello(ptl_process_id_t initiator,
456 kptl_peer_t *kptllnd_id2peer_locked(lnet_process_id_t id);
457 void kptllnd_peer_alive(kptl_peer_t *peer);
460 kptllnd_peer_addref (kptl_peer_t *peer)
462 atomic_inc(&peer->peer_refcount);
466 kptllnd_peer_decref (kptl_peer_t *peer)
468 if (atomic_dec_and_test(&peer->peer_refcount))
469 kptllnd_peer_destroy(peer);
473 kptllnd_set_tx_peer(kptl_tx_t *tx, kptl_peer_t *peer)
475 LASSERT (tx->tx_peer == NULL);
477 kptllnd_peer_addref(peer);
481 static inline struct list_head *
482 kptllnd_nid2peerlist(lnet_nid_t nid)
484 unsigned int hash = ((unsigned int)nid) %
485 kptllnd_data.kptl_peer_hash_size;
487 return &kptllnd_data.kptl_peers[hash];
490 static inline kptl_peer_t *
491 kptllnd_id2peer(lnet_process_id_t id)
496 read_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
497 peer = kptllnd_id2peer_locked(id);
498 read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
504 kptllnd_reserve_buffers(int n)
506 return kptllnd_rx_buffer_pool_reserve(&kptllnd_data.kptl_rx_buffer_pool,
511 kptllnd_peer_reserve_buffers(void)
513 return kptllnd_reserve_buffers(*kptllnd_tunables.kptl_peercredits);
517 kptllnd_peer_unreserve_buffers(void)
519 kptllnd_rx_buffer_pool_unreserve(&kptllnd_data.kptl_rx_buffer_pool,
520 *kptllnd_tunables.kptl_peercredits);
524 * TX SUPPORT FUNCTIONS
526 int kptllnd_setup_tx_descs(void);
527 void kptllnd_cleanup_tx_descs(void);
528 void kptllnd_tx_fini(kptl_tx_t *tx);
529 kptl_tx_t *kptllnd_get_idle_tx(enum kptl_tx_type purpose);
530 void kptllnd_tx_callback(ptl_event_t *ev);
531 const char *kptllnd_tx_typestr(int type);
534 kptllnd_tx_addref(kptl_tx_t *tx)
536 atomic_inc(&tx->tx_refcount);
540 kptllnd_tx_decref(kptl_tx_t *tx)
542 LASSERT (!in_interrupt()); /* Thread context only */
544 if (atomic_dec_and_test(&tx->tx_refcount))
549 * MESSAGE SUPPORT FUNCTIONS
551 void kptllnd_init_msg(kptl_msg_t *msg, int type, int body_nob);
552 void kptllnd_msg_pack(kptl_msg_t *msg, kptl_peer_t *peer);
553 int kptllnd_msg_unpack(kptl_msg_t *msg, int nob);
556 * MISC SUPPORT FUNCTIONS
558 void kptllnd_init_rdma_md(kptl_tx_t *tx, unsigned int niov,
559 struct iovec *iov, lnet_kiov_t *kiov,
560 unsigned int offset, unsigned int nob);
561 char *kptllnd_ptlid2str(ptl_process_id_t id);
563 void kptllnd_init_ptltrace(void);
564 void kptllnd_dump_ptltrace(void);
567 #define SIMULATION_FAIL_TX_PUT_ALLOC 0 /* 0x00000001 */
568 #define SIMULATION_FAIL_TX_GET_ALLOC 1 /* 0x00000002 */
569 #define SIMULATION_FAIL_TX 2 /* 0x00000004 */
570 #define SIMULATION_FAIL_RX_ALLOC 3 /* 0x00000008 */
572 #define IS_SIMULATION_ENABLED(x) \
573 (((*kptllnd_tunables.kptl_simulation_bitmap) & 1<< SIMULATION_##x) != 0)
575 #define IS_SIMULATION_ENABLED(x) 0