Whamcloud - gitweb
* Fix for 6357: handle RapidArray memory registration failures.
[fs/lustre-release.git] / lnet / klnds / ralnd / ralnd.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004 Cluster File Systems, Inc.
5  *   Author: Eric Barton <eric@bartonsoftware.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #ifndef EXPORT_SYMTAB
25 # define EXPORT_SYMTAB
26 #endif
27
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/mm.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/errno.h>
35 #include <linux/smp_lock.h>
36 #include <linux/unistd.h>
37 #include <linux/uio.h>
38
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <asm/io.h>
42
43 #include <linux/init.h>
44 #include <linux/fs.h>
45 #include <linux/file.h>
46 #include <linux/stat.h>
47 #include <linux/list.h>
48 #include <linux/kmod.h>
49 #include <linux/sysctl.h>
50
51 #include <net/sock.h>
52 #include <linux/in.h>
53
54 #define DEBUG_SUBSYSTEM S_NAL
55
56 #include <libcfs/kp30.h>
57 #include <portals/p30.h>
58 #include <portals/lib-p30.h>
59 #include <portals/nal.h>
60
61 #include <rapl.h>
62
63 #define RANAL_MAXDEVS       2                   /* max # devices RapidArray supports */
64
65 #define RANAL_N_CONND       4                   /* # connection daemons */
66
67 #define RANAL_MIN_RECONNECT_INTERVAL 1          /* first failed connection retry (seconds)... */
68 #define RANAL_MAX_RECONNECT_INTERVAL 60         /* ...exponentially increasing to this */
69
70 #define RANAL_FMA_MAX_PREFIX      232           /* max size of FMA "Prefix" */
71 #define RANAL_FMA_MAX_DATA        ((7<<10)-256) /* Max FMA MSG is 7K including prefix */
72
73 #define RANAL_PEER_HASH_SIZE  101               /* # peer lists */
74 #define RANAL_CONN_HASH_SIZE  101               /* # conn lists */
75
76 #define RANAL_NTX             64                /* # tx descs */
77 #define RANAL_NTX_NBLK        256               /* # reserved tx descs */
78
79 #define RANAL_FMA_CQ_SIZE     8192              /* # entries in receive CQ
80                                                  * (overflow is a performance hit) */
81
82 #define RANAL_RESCHED         100               /* # scheduler loops before reschedule */
83
84 #define RANAL_MIN_TIMEOUT     5                 /* minimum timeout interval (seconds) */
85 #define RANAL_TIMEOUT2KEEPALIVE(t) (((t)+1)/2)  /* timeout -> keepalive interval */
86
87 /* default vals for runtime tunables */
88 #define RANAL_TIMEOUT           30              /* comms timeout (seconds) */
89 #define RANAL_LISTENER_TIMEOUT   5              /* listener timeout (seconds) */
90 #define RANAL_BACKLOG          127              /* listener's backlog */
91 #define RANAL_PORT             988              /* listener's port */
92 #define RANAL_MAX_IMMEDIATE    (2<<10)          /* immediate payload breakpoint */
93
94 typedef struct
95 {
96         int               kra_timeout;          /* comms timeout (seconds) */
97         int               kra_listener_timeout; /* max time the listener can block */
98         int               kra_backlog;          /* listener's backlog */
99         int               kra_port;             /* listener's TCP/IP port */
100         int               kra_max_immediate;    /* immediate payload breakpoint */
101
102         struct ctl_table_header *kra_sysctl;    /* sysctl interface */
103 } kra_tunables_t;
104
105 typedef struct
106 {
107         RAP_PVOID               rad_handle;     /* device handle */
108         RAP_PVOID               rad_fma_cqh;    /* FMA completion queue handle */
109         RAP_PVOID               rad_rdma_cqh;   /* rdma completion queue handle */
110         int                     rad_id;         /* device id */
111         int                     rad_idx;        /* index in kra_devices */
112         int                     rad_ready;      /* set by device callback */
113         struct list_head        rad_ready_conns;/* connections ready to tx/rx */
114         struct list_head        rad_new_conns;  /* new connections to complete */
115         wait_queue_head_t       rad_waitq;      /* scheduler waits here */
116         spinlock_t              rad_lock;       /* serialise */
117         void                   *rad_scheduler;  /* scheduling thread */
118         unsigned int            rad_nphysmap;   /* # phys mappings */
119         unsigned int            rad_nppphysmap; /* # phys pages mapped */
120         unsigned int            rad_nvirtmap;   /* # virt mappings */
121         unsigned long           rad_nobvirtmap; /* # virt bytes mapped */
122 } kra_device_t;
123
124 typedef struct
125 {
126         int               kra_init;             /* initialisation state */
127         int               kra_shutdown;         /* shut down? */
128         atomic_t          kra_nthreads;         /* # live threads */
129
130         struct semaphore  kra_nid_mutex;        /* serialise NID/listener ops */
131         struct semaphore  kra_listener_signal;  /* block for listener startup/shutdown */
132         struct socket    *kra_listener_sock;    /* listener's socket */
133         int               kra_listener_shutdown; /* ask listener to close */
134
135         kra_device_t      kra_devices[RANAL_MAXDEVS]; /* device/ptag/cq etc */
136         int               kra_ndevs;            /* # devices */
137
138         rwlock_t          kra_global_lock;      /* stabilize peer/conn ops */
139
140         struct list_head *kra_peers;            /* hash table of all my known peers */
141         int               kra_peer_hash_size;   /* size of kra_peers */
142         atomic_t          kra_npeers;           /* # peers extant */
143
144         struct list_head *kra_conns;            /* conns hashed by cqid */
145         int               kra_conn_hash_size;   /* size of kra_conns */
146         __u64             kra_peerstamp;        /* when I started up */
147         __u64             kra_connstamp;        /* conn stamp generator */
148         int               kra_next_cqid;        /* cqid generator */
149         atomic_t          kra_nconns;           /* # connections extant */
150
151         long              kra_new_min_timeout;  /* minimum timeout on any new conn */
152         wait_queue_head_t kra_reaper_waitq;     /* reaper sleeps here */
153         spinlock_t        kra_reaper_lock;      /* serialise */
154
155         struct list_head  kra_connd_peers;      /* peers waiting for a connection */
156         struct list_head  kra_connd_acceptq;    /* accepted sockets to handshake */
157         wait_queue_head_t kra_connd_waitq;      /* connection daemons sleep here */
158         spinlock_t        kra_connd_lock;       /* serialise */
159
160         struct list_head  kra_idle_txs;         /* idle tx descriptors */
161         struct list_head  kra_idle_nblk_txs;    /* idle reserved tx descriptors */
162         __u64             kra_next_tx_cookie;   /* RDMA completion cookie */
163         wait_queue_head_t kra_idle_tx_waitq;    /* block here for tx descriptor */
164         spinlock_t        kra_tx_lock;          /* serialise */
165 } kra_data_t;
166
167 #define RANAL_INIT_NOTHING         0
168 #define RANAL_INIT_DATA            1
169 #define RANAL_INIT_LIB             2
170 #define RANAL_INIT_ALL             3
171
172 typedef struct kra_acceptsock                   /* accepted socket queued for connd */
173 {
174         struct list_head     ras_list;          /* queue for attention */
175         struct socket       *ras_sock;          /* the accepted socket */
176 } kra_acceptsock_t;
177
178 /************************************************************************
179  * Wire message structs.  These are sent in sender's byte order
180  * (i.e. receiver checks magic and flips if required).
181  */
182
183 typedef struct kra_connreq                      /* connection request/response */
184 {                                               /* (sent via socket) */
185         __u32             racr_magic;           /* I'm an ranal connreq */
186         __u16             racr_version;         /* this is my version number */
187         __u16             racr_devid;           /* sender's device ID */
188         __u64             racr_srcnid;          /* sender's NID */
189         __u64             racr_dstnid;          /* who sender expects to listen */
190         __u64             racr_peerstamp;       /* sender's instance stamp */
191         __u64             racr_connstamp;       /* sender's connection stamp */
192         __u32             racr_timeout;         /* sender's timeout */
193         RAP_RI_PARAMETERS racr_riparams;        /* sender's endpoint info */
194 } kra_connreq_t;
195
196 typedef struct
197 {
198         RAP_MEM_KEY       rard_key;
199         RAP_PVOID64       rard_addr;
200         RAP_UINT32        rard_nob;
201 } kra_rdma_desc_t;
202
203 typedef struct
204 {
205         ptl_hdr_t         raim_hdr;             /* portals header */
206         /* Portals payload is in FMA "Message Data" */
207 } kra_immediate_msg_t;
208
209 typedef struct
210 {
211         ptl_hdr_t         raprm_hdr;            /* portals header */
212         __u64             raprm_cookie;         /* opaque completion cookie */
213 } kra_putreq_msg_t;
214
215 typedef struct
216 {
217         __u64             rapam_src_cookie;     /* reflected completion cookie */
218         __u64             rapam_dst_cookie;     /* opaque completion cookie */
219         kra_rdma_desc_t   rapam_desc;           /* sender's sink buffer */
220 } kra_putack_msg_t;
221
222 typedef struct
223 {
224         ptl_hdr_t         ragm_hdr;             /* portals header */
225         __u64             ragm_cookie;          /* opaque completion cookie */
226         kra_rdma_desc_t   ragm_desc;            /* sender's sink buffer */
227 } kra_get_msg_t;
228
229 typedef struct
230 {
231         __u64             racm_cookie;          /* reflected completion cookie */
232 } kra_completion_msg_t;
233
234 typedef struct                                  /* NB must fit in FMA "Prefix" */
235 {
236         __u32             ram_magic;            /* I'm an ranal message */
237         __u16             ram_version;          /* this is my version number */
238         __u16             ram_type;             /* msg type */
239         __u64             ram_srcnid;           /* sender's NID */
240         __u64             ram_connstamp;        /* sender's connection stamp */
241         union {
242                 kra_immediate_msg_t   immediate;
243                 kra_putreq_msg_t      putreq;
244                 kra_putack_msg_t      putack;
245                 kra_get_msg_t         get;
246                 kra_completion_msg_t  completion;
247         }                    ram_u;
248         __u32             ram_seq;              /* incrementing sequence number */
249 } kra_msg_t;
250
251 #define RANAL_MSG_MAGIC       0x0be91b92        /* unique magic */
252 #define RANAL_MSG_VERSION              1        /* current protocol version */
253
254 #define RANAL_MSG_FENCE             0x80        /* fence RDMA */
255
256 #define RANAL_MSG_NONE              0x00        /* illegal message */
257 #define RANAL_MSG_NOOP              0x01        /* empty ram_u (keepalive) */
258 #define RANAL_MSG_IMMEDIATE         0x02        /* ram_u.immediate */
259 #define RANAL_MSG_PUT_REQ           0x03        /* ram_u.putreq (src->sink) */
260 #define RANAL_MSG_PUT_NAK           0x04        /* ram_u.completion (no PUT match: sink->src) */
261 #define RANAL_MSG_PUT_ACK           0x05        /* ram_u.putack (PUT matched: sink->src) */
262 #define RANAL_MSG_PUT_DONE          0x86        /* ram_u.completion (src->sink) */
263 #define RANAL_MSG_GET_REQ           0x07        /* ram_u.get (sink->src) */
264 #define RANAL_MSG_GET_NAK           0x08        /* ram_u.completion (no GET match: src->sink) */
265 #define RANAL_MSG_GET_DONE          0x89        /* ram_u.completion (src->sink) */
266 #define RANAL_MSG_CLOSE             0x8a        /* empty ram_u */
267
268 /***********************************************************************/
269
270 typedef struct kra_tx                           /* message descriptor */
271 {
272         struct list_head          tx_list;      /* queue on idle_txs/rac_sendq/rac_waitq */
273         struct kra_conn          *tx_conn;      /* owning conn */
274         lib_msg_t                *tx_libmsg[2]; /* lib msgs to finalize on completion */
275         unsigned long             tx_qtime;     /* when tx started to wait for something (jiffies) */
276         int                       tx_isnblk;    /* I'm reserved for non-blocking sends */
277         int                       tx_nob;       /* # bytes of payload */
278         int                       tx_buftype;   /* payload buffer type */
279         void                     *tx_buffer;    /* source/sink buffer */
280         int                       tx_phys_offset; /* first page offset (if phys) */
281         int                       tx_phys_npages; /* # physical pages */
282         RAP_PHYS_REGION          *tx_phys;      /* page descriptors */
283         RAP_MEM_KEY               tx_map_key;   /* mapping key */
284         RAP_RDMA_DESCRIPTOR       tx_rdma_desc; /* rdma descriptor */
285         __u64                     tx_cookie;    /* identify this tx to peer */
286         kra_msg_t                 tx_msg;       /* FMA message buffer */
287 } kra_tx_t;
288
289 #define RANAL_BUF_NONE           0              /* buffer type not set */
290 #define RANAL_BUF_IMMEDIATE      1              /* immediate data */
291 #define RANAL_BUF_PHYS_UNMAPPED  2              /* physical: not mapped yet */
292 #define RANAL_BUF_PHYS_MAPPED    3              /* physical: mapped already */
293 #define RANAL_BUF_VIRT_UNMAPPED  4              /* virtual: not mapped yet */
294 #define RANAL_BUF_VIRT_MAPPED    5              /* virtual: mapped already */
295
296 typedef struct kra_conn
297 {
298         struct kra_peer    *rac_peer;           /* owning peer */
299         struct list_head    rac_list;           /* stash on peer's conn list */
300         struct list_head    rac_hashlist;       /* stash in connection hash table */
301         struct list_head    rac_schedlist;      /* schedule (on rad_???_conns) for attention */
302         struct list_head    rac_fmaq;           /* txs queued for FMA */
303         struct list_head    rac_rdmaq;          /* txs awaiting RDMA completion */
304         struct list_head    rac_replyq;         /* txs awaiting replies */
305         __u64               rac_peerstamp;      /* peer's unique stamp */
306         __u64               rac_peer_connstamp; /* peer's unique connection stamp */
307         __u64               rac_my_connstamp;   /* my unique connection stamp */
308         unsigned long       rac_last_tx;        /* when I last sent an FMA message (jiffies) */
309         unsigned long       rac_last_rx;        /* when I last received an FMA messages (jiffies) */
310         long                rac_keepalive;      /* keepalive interval (seconds) */
311         long                rac_timeout;        /* infer peer death if no rx for this many seconds */
312         __u32               rac_cqid;           /* my completion callback id (non-unique) */
313         __u32               rac_tx_seq;         /* tx msg sequence number */
314         __u32               rac_rx_seq;         /* rx msg sequence number */
315         atomic_t            rac_refcount;       /* # users */
316         unsigned int        rac_close_sent;     /* I've sent CLOSE */
317         unsigned int        rac_close_recvd;    /* I've received CLOSE */
318         unsigned int        rac_state;          /* connection state */
319         unsigned int        rac_scheduled;      /* being attented to */
320         spinlock_t          rac_lock;           /* serialise */
321         kra_device_t       *rac_device;         /* which device */
322         RAP_PVOID           rac_rihandle;       /* RA endpoint */
323         kra_msg_t          *rac_rxmsg;          /* incoming message (FMA prefix) */
324         kra_msg_t           rac_msg;            /* keepalive/CLOSE message buffer */
325 } kra_conn_t;
326
327 #define RANAL_CONN_ESTABLISHED     0
328 #define RANAL_CONN_CLOSING         1
329 #define RANAL_CONN_CLOSED          2
330
331 typedef struct kra_peer
332 {
333         struct list_head    rap_list;           /* stash on global peer list */
334         struct list_head    rap_connd_list;     /* schedule on kra_connd_peers */
335         struct list_head    rap_conns;          /* all active connections */
336         struct list_head    rap_tx_queue;       /* msgs waiting for a conn */
337         ptl_nid_t           rap_nid;            /* who's on the other end(s) */
338         __u32               rap_ip;             /* IP address of peer */
339         int                 rap_port;           /* port on which peer listens */
340         atomic_t            rap_refcount;       /* # users */
341         int                 rap_persistence;    /* "known" peer refs */
342         int                 rap_connecting;     /* connection forming */
343         unsigned long       rap_reconnect_time; /* CURRENT_SECONDS when reconnect OK */
344         unsigned long       rap_reconnect_interval; /* exponential backoff */
345 } kra_peer_t;
346
347 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
348 # define sk_allocation  allocation
349 # define sk_data_ready  data_ready
350 # define sk_write_space write_space
351 # define sk_user_data   user_data
352 # define sk_prot        prot
353 # define sk_sndbuf      sndbuf
354 # define sk_socket      socket
355 # define sk_wmem_queued wmem_queued
356 # define sk_err         err
357 # define sk_sleep       sleep
358 #endif
359
360 extern lib_nal_t       kranal_lib;
361 extern kra_data_t      kranal_data;
362 extern kra_tunables_t  kranal_tunables;
363
364 extern void kranal_destroy_peer(kra_peer_t *peer);
365 extern void kranal_destroy_conn(kra_conn_t *conn);
366
367 static inline void
368 kranal_peer_addref(kra_peer_t *peer)
369 {
370         CDEBUG(D_NET, "%p->"LPX64"\n", peer, peer->rap_nid);
371         LASSERT(atomic_read(&peer->rap_refcount) > 0);
372         atomic_inc(&peer->rap_refcount);
373 }
374
375 static inline void
376 kranal_peer_decref(kra_peer_t *peer)
377 {
378         CDEBUG(D_NET, "%p->"LPX64"\n", peer, peer->rap_nid);
379         LASSERT(atomic_read(&peer->rap_refcount) > 0);
380         if (atomic_dec_and_test(&peer->rap_refcount))
381                 kranal_destroy_peer(peer);
382 }
383
384 static inline struct list_head *
385 kranal_nid2peerlist (ptl_nid_t nid)
386 {
387         unsigned int hash = ((unsigned int)nid) % kranal_data.kra_peer_hash_size;
388
389         return (&kranal_data.kra_peers[hash]);
390 }
391
392 static inline int
393 kranal_peer_active(kra_peer_t *peer)
394 {
395         /* Am I in the peer hash table? */
396         return (!list_empty(&peer->rap_list));
397 }
398
399 static inline void
400 kranal_conn_addref(kra_conn_t *conn)
401 {
402         CDEBUG(D_NET, "%p->"LPX64"\n", conn, conn->rac_peer->rap_nid);
403         LASSERT(atomic_read(&conn->rac_refcount) > 0);
404         atomic_inc(&conn->rac_refcount);
405 }
406
407 static inline void
408 kranal_conn_decref(kra_conn_t *conn)
409 {
410         CDEBUG(D_NET, "%p->"LPX64"\n", conn, conn->rac_peer->rap_nid);
411         LASSERT(atomic_read(&conn->rac_refcount) > 0);
412         if (atomic_dec_and_test(&conn->rac_refcount))
413                 kranal_destroy_conn(conn);
414 }
415
416 static inline struct list_head *
417 kranal_cqid2connlist (__u32 cqid)
418 {
419         unsigned int hash = cqid % kranal_data.kra_conn_hash_size;
420
421         return (&kranal_data.kra_conns [hash]);
422 }
423
424 static inline kra_conn_t *
425 kranal_cqid2conn_locked (__u32 cqid)
426 {
427         struct list_head *conns = kranal_cqid2connlist(cqid);
428         struct list_head *tmp;
429         kra_conn_t       *conn;
430
431         list_for_each(tmp, conns) {
432                 conn = list_entry(tmp, kra_conn_t, rac_hashlist);
433
434                 if (conn->rac_cqid == cqid)
435                         return conn;
436         }
437
438         return NULL;
439 }
440
441 static inline int
442 kranal_tx_mapped (kra_tx_t *tx)
443 {
444         return (tx->tx_buftype == RANAL_BUF_VIRT_MAPPED ||
445                 tx->tx_buftype == RANAL_BUF_PHYS_MAPPED);
446 }
447
448 static inline __u64
449 kranal_page2phys (struct page *p)
450 {
451         return page_to_phys(p);
452 }
453
454 extern void kranal_free_acceptsock (kra_acceptsock_t *ras);
455 extern int kranal_listener_procint (ctl_table *table,
456                                     int write, struct file *filp,
457                                     void *buffer, size_t *lenp);
458 extern void kranal_update_reaper_timeout (long timeout);
459 extern void kranal_tx_done (kra_tx_t *tx, int completion);
460 extern void kranal_unlink_peer_locked (kra_peer_t *peer);
461 extern void kranal_schedule_conn (kra_conn_t *conn);
462 extern kra_peer_t *kranal_create_peer (ptl_nid_t nid);
463 extern kra_peer_t *kranal_find_peer_locked (ptl_nid_t nid);
464 extern void kranal_post_fma (kra_conn_t *conn, kra_tx_t *tx);
465 extern int kranal_del_peer (ptl_nid_t nid, int single_share);
466 extern void kranal_device_callback (RAP_INT32 devid, RAP_PVOID arg);
467 extern int kranal_thread_start (int(*fn)(void *arg), void *arg);
468 extern int kranal_connd (void *arg);
469 extern int kranal_reaper (void *arg);
470 extern int kranal_scheduler (void *arg);
471 extern void kranal_close_conn_locked (kra_conn_t *conn, int error);
472 extern void kranal_terminate_conn_locked (kra_conn_t *conn);
473 extern void kranal_connect (kra_peer_t *peer);
474 extern int kranal_conn_handshake (struct socket *sock, kra_peer_t *peer);
475 extern void kranal_pause(int ticks);