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