Whamcloud - gitweb
* bug 11659 fix - finer-grained peerstamps for ptllnd connection
[fs/lustre-release.git] / lnet / klnds / ptllnd / ptllnd.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2005 Cluster File Systems, Inc. All rights reserved.
5  *   Author: PJ Kirner <pjkirner@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   This file is confidential source code owned by Cluster File Systems.
11  *   No viewing, modification, compilation, redistribution, or any other
12  *   form of use is permitted except through a signed license agreement.
13  *
14  *   If you have not signed such an agreement, then you have no rights to
15  *   this file.  Please destroy it immediately and contact CFS.
16  *
17  */
18
19 #ifndef EXPORT_SYMTAB
20 # define EXPORT_SYMTAB
21 #endif
22
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/errno.h>
30 #include <linux/smp_lock.h>
31 #include <linux/unistd.h>
32 #include <linux/uio.h>
33
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <asm/io.h>
37
38 #include <linux/init.h>
39 #include <linux/fs.h>
40 #include <linux/file.h>
41 #include <linux/stat.h>
42 #include <linux/list.h>
43 #include <linux/kmod.h>
44 #include <linux/sysctl.h>
45 #include <linux/random.h>
46
47 #include <net/sock.h>
48 #include <linux/in.h>
49
50
51 #define DEBUG_SUBSYSTEM S_LND
52
53 #include <libcfs/kp30.h>
54 #include <lnet/lnet.h>
55 #include <lnet/lib-lnet.h>
56 #include <portals/p30.h>
57 #ifdef CRAY_XT3
58 #include <portals/ptltrace.h>
59 #endif
60 #include <lnet/ptllnd.h>        /* Depends on portals/p30.h */
61
62 /*
63  * Define this to enable console debug logging
64  * and simulation
65  */
66 //#define PJK_DEBUGGING
67
68 #ifdef CONFIG_SMP
69 # define PTLLND_N_SCHED         num_online_cpus()   /* # schedulers */
70 #else
71 # define PTLLND_N_SCHED         1                   /* # schedulers */
72 #endif
73
74 #define PTLLND_CREDIT_HIGHWATER ((*kptllnd_tunables.kptl_peercredits)-1)
75   /* when eagerly to return credits */
76
77 typedef struct
78 {
79         int             *kptl_ntx;              /* # tx descs to pre-allocate */
80         int             *kptl_max_nodes;        /* max # nodes all talking to me */
81         int             *kptl_max_procs_per_node; /* max # processes per node */
82         int             *kptl_checksum;         /* checksum kptl_msg_t? */
83         int             *kptl_timeout;          /* comms timeout (seconds) */
84         int             *kptl_portal;           /* portal number */
85         int             *kptl_pid;              /* portals PID (self + kernel peers) */
86         int             *kptl_rxb_npages;       /* number of pages for rx buffer */
87         int             *kptl_rxb_nspare;       /* number of spare rx buffers */
88         int             *kptl_credits;          /* number of credits */
89         int             *kptl_peercredits;      /* number of credits */
90         int             *kptl_max_msg_size;     /* max immd message size*/
91         int             *kptl_peer_hash_table_size; /* # slots in peer hash table */
92         int             *kptl_reschedule_loops; /* scheduler yield loops */
93 #ifdef CRAY_XT3
94         int             *kptl_ptltrace_on_timeout; /* dump pltrace on timeout? */
95         char           **kptl_ptltrace_basename;  /* ptltrace dump file basename */
96 #endif
97 #ifdef PJK_DEBUGGING
98         int             *kptl_simulation_bitmap;/* simulation bitmap */
99 #endif
100
101 #if CONFIG_SYSCTL && !CFS_SYSFS_MODULE_PARM
102         struct ctl_table_header *kptl_sysctl;    /* sysctl interface */
103 #endif
104 } kptl_tunables_t;
105
106 #include "lnet/ptllnd_wire.h"
107
108 /***********************************************************************/
109
110 typedef struct kptl_data kptl_data_t;
111 typedef struct kptl_rx_buffer kptl_rx_buffer_t;
112 typedef struct kptl_peer kptl_peer_t;
113
114 typedef struct {
115         char      eva_type;
116 } kptl_eventarg_t;
117
118 #define PTLLND_EVENTARG_TYPE_MSG    0x1
119 #define PTLLND_EVENTARG_TYPE_RDMA   0x2
120 #define PTLLND_EVENTARG_TYPE_BUF    0x3
121
122 typedef struct kptl_rx                          /* receive message */
123 {
124         struct list_head        rx_list;        /* queue for attention */
125         kptl_rx_buffer_t       *rx_rxb;         /* the rx buffer pointer */
126         kptl_msg_t             *rx_msg;         /* received message */
127         int                     rx_nob;         /* received message size */
128         ptl_process_id_t        rx_initiator;   /* sender's address */
129 #ifdef CRAY_XT3
130         ptl_uid_t               rx_uid;         /* sender's uid */
131 #endif
132         kptl_peer_t            *rx_peer;        /* pointer to peer */
133         char                    rx_space[0];    /* copy of incoming request */
134 } kptl_rx_t;
135
136 typedef struct kptl_rx_buffer_pool
137 {
138         spinlock_t              rxbp_lock;
139         struct list_head        rxbp_list;      /* all allocated buffers */
140         int                     rxbp_count;     /* # allocated buffers */
141         int                     rxbp_reserved;  /* # requests to buffer */
142         int                     rxbp_shutdown;  /* shutdown flag */
143 } kptl_rx_buffer_pool_t;
144
145 struct kptl_rx_buffer
146 {
147         kptl_rx_buffer_pool_t  *rxb_pool;
148         struct list_head        rxb_list;       /* for the rxb_pool list */
149         struct list_head        rxb_repost_list;/* for the kptl_sched_rxbq list */
150         int                     rxb_posted:1;   /* on the net */
151         int                     rxb_idle:1;     /* all done */
152         kptl_eventarg_t         rxb_eventarg;   /* event->md.user_ptr */
153         int                     rxb_refcount;   /* reference count */
154         ptl_handle_md_t         rxb_mdh;        /* the portals memory descriptor (MD) handle */
155         char                   *rxb_buffer;     /* the buffer */
156
157 };
158
159 enum kptl_tx_type
160 {
161         TX_TYPE_RESERVED                = 0,
162         TX_TYPE_SMALL_MESSAGE           = 1,
163         TX_TYPE_PUT_REQUEST             = 2,
164         TX_TYPE_GET_REQUEST             = 3,
165         TX_TYPE_PUT_RESPONSE            = 4,
166         TX_TYPE_GET_RESPONSE            = 5,
167 };
168
169 typedef union {
170 #ifdef _USING_LUSTRE_PORTALS_
171         struct iovec iov[PTL_MD_MAX_IOV];
172         lnet_kiov_t kiov[PTL_MD_MAX_IOV];
173 #else
174         ptl_md_iovec_t iov[PTL_MD_MAX_IOV];
175 #endif
176 } kptl_fragvec_t;
177
178 typedef struct kptl_tx                           /* transmit message */
179 {
180         struct list_head        tx_list;      /* queue on idle_txs etc */
181         atomic_t                tx_refcount;  /* reference count*/
182         enum kptl_tx_type       tx_type;      /* small msg/{put,get}{req,resp} */
183         int                     tx_active:1;  /* queued on the peer */
184         int                     tx_idle:1;    /* on the free list */
185         kptl_eventarg_t         tx_msg_eventarg; /* event->md.user_ptr */
186         kptl_eventarg_t         tx_rdma_eventarg; /* event->md.user_ptr */
187         int                     tx_status;    /* the status of this tx descriptor */
188         ptl_handle_md_t         tx_rdma_mdh;  /* RDMA buffer */
189         ptl_handle_md_t         tx_msg_mdh;   /* the portals MD handle for the initial message */
190         lnet_msg_t             *tx_lnet_msg;  /* LNET message to finalize */
191         lnet_msg_t             *tx_lnet_replymsg; /* LNET reply message to finalize */
192         kptl_msg_t             *tx_msg;       /* the message data */
193         kptl_peer_t            *tx_peer;      /* the peer this is waiting on */
194         unsigned long           tx_deadline;  /* deadline */
195         ptl_md_t                tx_rdma_md;   /* rdma buffer */
196         kptl_fragvec_t         *tx_rdma_frags; /* buffer fragments */
197 } kptl_tx_t;
198
199 enum kptllnd_peer_state
200 {
201         PEER_STATE_UNINITIALIZED        = 0,
202         PEER_STATE_ALLOCATED            = 1,
203         PEER_STATE_WAITING_HELLO        = 2,
204         PEER_STATE_ACTIVE               = 3,
205         PEER_STATE_CLOSING              = 4,
206         PEER_STATE_ZOMBIE               = 5,
207 };
208
209 struct kptl_peer
210 {
211         struct list_head        peer_list;
212         atomic_t                peer_refcount;          /* The current refrences */
213         enum kptllnd_peer_state peer_state;
214         spinlock_t              peer_lock;              /* serialize */
215         struct list_head        peer_sendq;             /* txs waiting for mh handles */
216         struct list_head        peer_activeq;           /* txs awaiting completion */
217         lnet_process_id_t       peer_id;                /* Peer's LNET id */
218         ptl_process_id_t        peer_ptlid;             /* Peer's portals id */
219         __u64                   peer_incarnation;       /* peer's incarnation */
220         __u64                   peer_myincarnation;     /* my incarnation at HELLO */
221         int                     peer_sent_hello;        /* have I sent HELLO? */
222         int                     peer_credits;           /* number of send credits */
223         int                     peer_outstanding_credits;/* number of peer credits to return */
224         int                     peer_active_rxs;        /* # rx-es being handled */
225         int                     peer_error;             /* errno on closing this peer */
226         cfs_time_t              peer_last_alive;        /* when (in jiffies) I was last alive */
227         __u64                   peer_next_matchbits;    /* Next value to register RDMA from peer */
228         __u64                   peer_last_matchbits_seen; /* last matchbits used to RDMA to peer */
229 };
230
231 struct kptl_data
232 {
233         int                     kptl_init;             /* initialisation state */
234         volatile int            kptl_shutdown;         /* shut down? */
235         atomic_t                kptl_nthreads;         /* # live threads */
236         lnet_ni_t              *kptl_ni;               /* _the_ LND instance */
237         ptl_handle_ni_t         kptl_nih;              /* network inteface handle */
238         ptl_process_id_t        kptl_portals_id;       /* Portals ID of interface */
239         __u64                   kptl_incarnation;      /* which one am I */
240         ptl_handle_eq_t         kptl_eqh;              /* Event Queue (EQ) */
241
242         spinlock_t              kptl_sched_lock;       /* serialise... */
243         wait_queue_head_t       kptl_sched_waitq;      /* schedulers sleep here */
244         struct list_head        kptl_sched_txq;        /* tx requiring attention */
245         struct list_head        kptl_sched_rxq;        /* rx requiring attention */
246         struct list_head        kptl_sched_rxbq;       /* rxb requiring reposting */
247
248         wait_queue_head_t       kptl_watchdog_waitq;   /* watchdog sleeps here */
249
250         kptl_rx_buffer_pool_t   kptl_rx_buffer_pool;   /* rx buffer pool */
251         cfs_mem_cache_t*        kptl_rx_cache;         /* rx descripter cache */
252
253         atomic_t                kptl_ntx;              /* # tx descs allocated */
254         spinlock_t              kptl_tx_lock;          /* serialise idle tx list*/
255         struct list_head        kptl_idle_txs;         /* idle tx descriptors */
256
257         rwlock_t                kptl_peer_rw_lock;     /* lock for peer table */
258         struct list_head       *kptl_peers;            /* hash table of all my known peers */
259         struct list_head        kptl_closing_peers;    /* peers being closed */
260         struct list_head        kptl_zombie_peers;     /* peers waiting for refs to drain */
261         int                     kptl_peer_hash_size;   /* size of kptl_peers */
262         int                     kptl_npeers;           /* # peers extant */
263         int                     kptl_n_active_peers;   /* # active peers */
264         int                     kptl_expected_peers;   /* # peers I can buffer HELLOs from */
265
266         kptl_msg_t             *kptl_nak_msg;          /* common NAK message */
267         spinlock_t              kptl_ptlid2str_lock;   /* serialise str ops */
268 };
269
270 enum 
271 {
272         PTLLND_INIT_NOTHING = 0,
273         PTLLND_INIT_DATA,
274         PTLLND_INIT_ALL,
275 };
276
277 extern kptl_tunables_t  kptllnd_tunables;
278 extern kptl_data_t      kptllnd_data;
279
280 static inline lnet_nid_t 
281 kptllnd_ptl2lnetnid(ptl_nid_t ptl_nid)
282 {
283 #ifdef _USING_LUSTRE_PORTALS_
284         return LNET_MKNID(LNET_NIDNET(kptllnd_data.kptl_ni->ni_nid), 
285                           LNET_NIDADDR(ptl_nid));
286 #else
287         return LNET_MKNID(LNET_NIDNET(kptllnd_data.kptl_ni->ni_nid), 
288                           ptl_nid);
289 #endif
290 }
291
292 static inline ptl_nid_t 
293 kptllnd_lnet2ptlnid(lnet_nid_t lnet_nid)
294 {
295 #ifdef _USING_LUSTRE_PORTALS_
296         return LNET_MKNID(LNET_NIDNET(kptllnd_data.kptl_portals_id.nid),
297                           LNET_NIDADDR(lnet_nid));
298 #else
299         return LNET_NIDADDR(lnet_nid);
300 #endif
301 }
302
303 int  kptllnd_startup(lnet_ni_t *ni);
304 void kptllnd_shutdown(lnet_ni_t *ni);
305 int  kptllnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
306 int  kptllnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
307 int  kptllnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg,
308                   int delayed, unsigned int niov, 
309                   struct iovec *iov, lnet_kiov_t *kiov,
310                   unsigned int offset, unsigned int mlen, unsigned int rlen);
311 int  kptllnd_eager_recv(struct lnet_ni *ni, void *private, 
312                         lnet_msg_t *msg, void **new_privatep);
313 void kptllnd_eq_callback(ptl_event_t *evp);
314 int  kptllnd_scheduler(void *arg);
315 int  kptllnd_watchdog(void *arg);
316 int  kptllnd_thread_start(int (*fn)(void *arg), void *arg);
317 int  kptllnd_tunables_init(void);
318 void kptllnd_tunables_fini(void);
319
320 const char *kptllnd_evtype2str(int evtype);
321 const char *kptllnd_msgtype2str(int msgtype);
322
323 static inline void *
324 kptllnd_eventarg2obj (kptl_eventarg_t *eva)
325 {
326         switch (eva->eva_type) {
327         default:
328                 LBUG();
329         case PTLLND_EVENTARG_TYPE_BUF:
330                 return list_entry(eva, kptl_rx_buffer_t, rxb_eventarg);
331         case PTLLND_EVENTARG_TYPE_RDMA:
332                 return list_entry(eva, kptl_tx_t, tx_rdma_eventarg);
333         case PTLLND_EVENTARG_TYPE_MSG:
334                 return list_entry(eva, kptl_tx_t, tx_msg_eventarg);
335         }
336 }
337
338 /*
339  * RX BUFFER SUPPORT FUNCTIONS
340  */
341 void kptllnd_rx_buffer_pool_init(kptl_rx_buffer_pool_t *rxbp);
342 void kptllnd_rx_buffer_pool_fini(kptl_rx_buffer_pool_t *rxbp);
343 int  kptllnd_rx_buffer_pool_reserve(kptl_rx_buffer_pool_t *rxbp, int count);
344 void kptllnd_rx_buffer_pool_unreserve(kptl_rx_buffer_pool_t *rxbp, int count);
345 void kptllnd_rx_buffer_callback(ptl_event_t *ev);
346 void kptllnd_rx_buffer_post(kptl_rx_buffer_t *rxb);
347
348 static inline int
349 kptllnd_rx_buffer_size(void)
350 {
351         return PAGE_SIZE * (*kptllnd_tunables.kptl_rxb_npages);
352 }
353
354 static inline void
355 kptllnd_rx_buffer_addref(kptl_rx_buffer_t *rxb)
356 {
357         unsigned long flags;
358         
359         spin_lock_irqsave(&rxb->rxb_pool->rxbp_lock, flags);
360         rxb->rxb_refcount++;
361         spin_unlock_irqrestore(&rxb->rxb_pool->rxbp_lock, flags);
362 }
363
364 static inline void
365 kptllnd_rx_buffer_decref_locked(kptl_rx_buffer_t *rxb)
366 {
367         if (--(rxb->rxb_refcount) == 0) {
368                 spin_lock(&kptllnd_data.kptl_sched_lock);
369         
370                 list_add_tail(&rxb->rxb_repost_list,
371                               &kptllnd_data.kptl_sched_rxbq);
372                 wake_up(&kptllnd_data.kptl_sched_waitq);
373
374                 spin_unlock(&kptllnd_data.kptl_sched_lock);
375         }
376 }
377
378 static inline void
379 kptllnd_rx_buffer_decref(kptl_rx_buffer_t *rxb)
380 {
381         unsigned long flags;
382         int           count;
383         
384         spin_lock_irqsave(&rxb->rxb_pool->rxbp_lock, flags);
385         count = --(rxb->rxb_refcount);
386         spin_unlock_irqrestore(&rxb->rxb_pool->rxbp_lock, flags);
387
388         if (count == 0)
389                 kptllnd_rx_buffer_post(rxb);
390 }
391
392 /*
393  * RX SUPPORT FUNCTIONS
394  */
395 void kptllnd_rx_done(kptl_rx_t *rx);
396 void kptllnd_rx_parse(kptl_rx_t *rx);
397
398 /*
399  * PEER SUPPORT FUNCTIONS
400  */
401 int kptllnd_get_peer_info(int index,
402                           lnet_process_id_t *id, 
403                           int *state, int *sent_hello,
404                           int *refcount, __u64 *incarnation,
405                           __u64 *next_matchbits, __u64 *last_matchbits_seen,
406                           int *nsendq, int *nactiveq,
407                           int *credits, int *outstanding_credits);
408 void kptllnd_peer_destroy(kptl_peer_t *peer);
409 int  kptllnd_peer_del(lnet_process_id_t id);
410 void kptllnd_peer_close_locked(kptl_peer_t *peer, int why);
411 void kptllnd_peer_close(kptl_peer_t *peer, int why);
412 void kptllnd_handle_closing_peers(void);
413 int  kptllnd_peer_connect(kptl_tx_t *tx, lnet_nid_t nid);
414 void kptllnd_peer_check_sends(kptl_peer_t *peer);
415 void kptllnd_peer_check_bucket(int idx);
416 void kptllnd_tx_launch(kptl_tx_t *tx, lnet_process_id_t target);
417 kptl_peer_t *kptllnd_peer_handle_hello(ptl_process_id_t initiator,
418                                        kptl_msg_t *msg);
419 kptl_peer_t *kptllnd_id2peer_locked(lnet_process_id_t id);
420 void kptllnd_peer_alive(kptl_peer_t *peer);
421
422 static inline void
423 kptllnd_peer_addref (kptl_peer_t *peer)
424 {
425         atomic_inc(&peer->peer_refcount);
426 }
427
428 static inline void
429 kptllnd_peer_decref (kptl_peer_t *peer)
430 {
431         if (atomic_dec_and_test(&peer->peer_refcount))
432                 kptllnd_peer_destroy(peer);
433 }
434
435 static inline void
436 kptllnd_set_tx_peer(kptl_tx_t *tx, kptl_peer_t *peer) 
437 {
438         LASSERT (tx->tx_peer == NULL);
439         
440         kptllnd_peer_addref(peer);
441         tx->tx_peer = peer;
442 }
443
444 static inline struct list_head *
445 kptllnd_nid2peerlist(lnet_nid_t nid)
446 {
447         unsigned int hash = ((unsigned int)nid) %
448                             kptllnd_data.kptl_peer_hash_size;
449
450         return &kptllnd_data.kptl_peers[hash];
451 }
452
453 static inline kptl_peer_t *
454 kptllnd_id2peer(lnet_process_id_t id)
455 {
456         kptl_peer_t   *peer;
457         unsigned long  flags;
458
459         read_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
460         peer = kptllnd_id2peer_locked(id);
461         read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
462
463         return peer;
464 }
465
466 static inline int
467 kptllnd_reserve_buffers(int n)
468 {
469         return kptllnd_rx_buffer_pool_reserve(&kptllnd_data.kptl_rx_buffer_pool,
470                                               n);
471 }
472
473 static inline int
474 kptllnd_peer_reserve_buffers(void)
475 {
476         return kptllnd_reserve_buffers(*kptllnd_tunables.kptl_peercredits);
477 }
478
479 static inline void
480 kptllnd_peer_unreserve_buffers(void)
481 {
482         kptllnd_rx_buffer_pool_unreserve(&kptllnd_data.kptl_rx_buffer_pool,
483                                          *kptllnd_tunables.kptl_peercredits);
484 }
485
486 /*
487  * TX SUPPORT FUNCTIONS
488  */
489 int  kptllnd_setup_tx_descs(void);
490 void kptllnd_cleanup_tx_descs(void);
491 void kptllnd_tx_fini(kptl_tx_t *tx);
492 kptl_tx_t *kptllnd_get_idle_tx(enum kptl_tx_type purpose);
493 void kptllnd_tx_callback(ptl_event_t *ev);
494 const char *kptllnd_tx_typestr(int type);
495
496 static inline void
497 kptllnd_tx_addref(kptl_tx_t *tx)
498 {
499         atomic_inc(&tx->tx_refcount);
500 }
501
502 static inline void 
503 kptllnd_tx_decref(kptl_tx_t *tx)
504 {
505         LASSERT (!in_interrupt());        /* Thread context only */
506
507         if (atomic_dec_and_test(&tx->tx_refcount))
508                 kptllnd_tx_fini(tx);
509 }
510
511 /*
512  * MESSAGE SUPPORT FUNCTIONS
513  */
514 void kptllnd_init_msg(kptl_msg_t *msg, int type, int body_nob);
515 void kptllnd_msg_pack(kptl_msg_t *msg, kptl_peer_t *peer);
516 int  kptllnd_msg_unpack(kptl_msg_t *msg, int nob);
517
518 /*
519  * MISC SUPPORT FUNCTIONS
520  */
521 void kptllnd_init_rdma_md(kptl_tx_t *tx, unsigned int niov,
522                           struct iovec *iov, lnet_kiov_t *kiov,
523                           unsigned int offset, unsigned int nob);
524 char *kptllnd_ptlid2str(ptl_process_id_t id);
525
526 void kptllnd_init_ptltrace(void);
527 void kptllnd_dump_ptltrace(void);
528
529 #ifdef PJK_DEBUGGING
530 #define SIMULATION_FAIL_TX_PUT_ALLOC   0       /* 0x00000001 */
531 #define SIMULATION_FAIL_TX_GET_ALLOC   1       /* 0x00000002 */
532 #define SIMULATION_FAIL_TX             2       /* 0x00000004 */
533 #define SIMULATION_FAIL_RX_ALLOC       3       /* 0x00000008 */
534
535 #define IS_SIMULATION_ENABLED(x) \
536         (((*kptllnd_tunables.kptl_simulation_bitmap) & 1<< SIMULATION_##x) != 0)
537 #else
538 #define IS_SIMULATION_ENABLED(x)       0
539 #endif
540