Whamcloud - gitweb
Branch HEAD
[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 #ifndef AUTOCONF_INCLUDED
28 #include <linux/config.h>
29 #endif
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/mm.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/smp_lock.h>
37 #include <linux/unistd.h>
38 #include <linux/uio.h>
39
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
42 #include <asm/io.h>
43
44 #include <linux/init.h>
45 #include <linux/fs.h>
46 #include <linux/file.h>
47 #include <linux/stat.h>
48 #include <linux/list.h>
49 #include <linux/kmod.h>
50 #include <linux/sysctl.h>
51
52 #include <net/sock.h>
53 #include <linux/in.h>
54
55 #define DEBUG_SUBSYSTEM S_LND
56
57 #include <libcfs/libcfs.h>
58 #include <lnet/lnet.h>
59 #include <lnet/lib-lnet.h>
60
61 #include <rapl.h>
62
63 /* tunables determined at compile time */
64 #define RANAL_RESCHED             100           /* # scheduler loops before reschedule */
65
66 #define RANAL_PEER_HASH_SIZE      101           /* # peer lists */
67 #define RANAL_CONN_HASH_SIZE      101           /* # conn lists */
68
69 #define RANAL_MIN_TIMEOUT         5             /* minimum timeout interval (seconds) */
70 #define RANAL_TIMEOUT2KEEPALIVE(t) (((t)+1)/2)  /* timeout -> keepalive interval */
71
72 /* fixed constants */
73 #define RANAL_MAXDEVS             2             /* max # devices RapidArray supports */
74 #define RANAL_FMA_MAX_PREFIX      232           /* max bytes in FMA "Prefix" we can use */
75 #define RANAL_FMA_MAX_DATA        ((7<<10)-256) /* Max FMA MSG is 7K including prefix */
76
77
78 typedef struct
79 {
80         int              *kra_n_connd;          /* # connection daemons */
81         int              *kra_min_reconnect_interval; /* first failed connection retry... */
82         int              *kra_max_reconnect_interval; /* ...exponentially increasing to this */
83         int              *kra_ntx;              /* # tx descs */
84         int              *kra_credits;          /* # concurrent sends */
85         int              *kra_peercredits;      /* # concurrent sends to 1 peer */
86         int              *kra_fma_cq_size;      /* # entries in receive CQ */
87         int              *kra_timeout;          /* comms timeout (seconds) */
88         int              *kra_max_immediate;    /* immediate payload breakpoint */
89
90 #if CONFIG_SYSCTL && !CFS_SYSFS_MODULE_PARM
91         cfs_sysctl_table_header_t *kra_sysctl;  /* sysctl interface */
92 #endif
93 } kra_tunables_t;
94
95 typedef struct
96 {
97         RAP_PVOID               rad_handle;     /* device handle */
98         RAP_PVOID               rad_fma_cqh;    /* FMA completion queue handle */
99         RAP_PVOID               rad_rdma_cqh;   /* rdma completion queue handle */
100         int                     rad_id;         /* device id */
101         int                     rad_idx;        /* index in kra_devices */
102         int                     rad_ready;      /* set by device callback */
103         struct list_head        rad_ready_conns;/* connections ready to tx/rx */
104         struct list_head        rad_new_conns;  /* new connections to complete */
105         wait_queue_head_t       rad_waitq;      /* scheduler waits here */
106         spinlock_t              rad_lock;       /* serialise */
107         void                   *rad_scheduler;  /* scheduling thread */
108         unsigned int            rad_nphysmap;   /* # phys mappings */
109         unsigned int            rad_nppphysmap; /* # phys pages mapped */
110         unsigned int            rad_nvirtmap;   /* # virt mappings */
111         unsigned long           rad_nobvirtmap; /* # virt bytes mapped */
112 } kra_device_t;
113
114 typedef struct
115 {
116         int               kra_init;             /* initialisation state */
117         int               kra_shutdown;         /* shut down? */
118         atomic_t          kra_nthreads;         /* # live threads */
119         lnet_ni_t        *kra_ni;               /* _the_ nal instance */
120         
121         kra_device_t      kra_devices[RANAL_MAXDEVS]; /* device/ptag/cq etc */
122         int               kra_ndevs;            /* # devices */
123
124         rwlock_t          kra_global_lock;      /* stabilize peer/conn ops */
125
126         struct list_head *kra_peers;            /* hash table of all my known peers */
127         int               kra_peer_hash_size;   /* size of kra_peers */
128         atomic_t          kra_npeers;           /* # peers extant */
129         int               kra_nonewpeers;       /* prevent new peers */
130
131         struct list_head *kra_conns;            /* conns hashed by cqid */
132         int               kra_conn_hash_size;   /* size of kra_conns */
133         __u64             kra_peerstamp;        /* when I started up */
134         __u64             kra_connstamp;        /* conn stamp generator */
135         int               kra_next_cqid;        /* cqid generator */
136         atomic_t          kra_nconns;           /* # connections extant */
137
138         long              kra_new_min_timeout;  /* minimum timeout on any new conn */
139         wait_queue_head_t kra_reaper_waitq;     /* reaper sleeps here */
140         spinlock_t        kra_reaper_lock;      /* serialise */
141
142         struct list_head  kra_connd_peers;      /* peers waiting for a connection */
143         struct list_head  kra_connd_acceptq;    /* accepted sockets to handshake */
144         wait_queue_head_t kra_connd_waitq;      /* connection daemons sleep here */
145         spinlock_t        kra_connd_lock;       /* serialise */
146
147         struct list_head  kra_idle_txs;         /* idle tx descriptors */
148         __u64             kra_next_tx_cookie;   /* RDMA completion cookie */
149         spinlock_t        kra_tx_lock;          /* serialise */
150 } kra_data_t;
151
152 #define RANAL_INIT_NOTHING         0
153 #define RANAL_INIT_DATA            1
154 #define RANAL_INIT_ALL             2
155
156 typedef struct kra_acceptsock                   /* accepted socket queued for connd */
157 {
158         struct list_head     ras_list;          /* queue for attention */
159         struct socket       *ras_sock;          /* the accepted socket */
160 } kra_acceptsock_t;
161
162 /************************************************************************
163  * Wire message structs.  These are sent in sender's byte order
164  * (i.e. receiver checks magic and flips if required).
165  */
166
167 typedef struct kra_connreq                      /* connection request/response */
168 {                                               /* (sent via socket) */
169         __u32             racr_magic;           /* I'm an ranal connreq */
170         __u16             racr_version;         /* this is my version number */
171         __u16             racr_devid;           /* sender's device ID */
172         __u64             racr_srcnid;          /* sender's NID */
173         __u64             racr_dstnid;          /* who sender expects to listen */
174         __u64             racr_peerstamp;       /* sender's instance stamp */
175         __u64             racr_connstamp;       /* sender's connection stamp */
176         __u32             racr_timeout;         /* sender's timeout */
177         RAP_RI_PARAMETERS racr_riparams;        /* sender's endpoint info */
178 } kra_connreq_t;
179
180 typedef struct
181 {
182         RAP_MEM_KEY       rard_key;
183         RAP_PVOID64       rard_addr;
184         RAP_UINT32        rard_nob;
185 } kra_rdma_desc_t;
186
187 typedef struct
188 {
189         lnet_hdr_t        raim_hdr;             /* portals header */
190         /* Portals payload is in FMA "Message Data" */
191 } kra_immediate_msg_t;
192
193 typedef struct
194 {
195         lnet_hdr_t        raprm_hdr;            /* portals header */
196         __u64             raprm_cookie;         /* opaque completion cookie */
197 } kra_putreq_msg_t;
198
199 typedef struct
200 {
201         __u64             rapam_src_cookie;     /* reflected completion cookie */
202         __u64             rapam_dst_cookie;     /* opaque completion cookie */
203         kra_rdma_desc_t   rapam_desc;           /* sender's sink buffer */
204 } kra_putack_msg_t;
205
206 typedef struct
207 {
208         lnet_hdr_t        ragm_hdr;             /* portals header */
209         __u64             ragm_cookie;          /* opaque completion cookie */
210         kra_rdma_desc_t   ragm_desc;            /* sender's sink buffer */
211 } kra_get_msg_t;
212
213 typedef struct
214 {
215         __u64             racm_cookie;          /* reflected completion cookie */
216 } kra_completion_msg_t;
217
218 typedef struct                                  /* NB must fit in FMA "Prefix" */
219 {
220         __u32             ram_magic;            /* I'm an ranal message */
221         __u16             ram_version;          /* this is my version number */
222         __u16             ram_type;             /* msg type */
223         __u64             ram_srcnid;           /* sender's NID */
224         __u64             ram_connstamp;        /* sender's connection stamp */
225         union {
226                 kra_immediate_msg_t   immediate;
227                 kra_putreq_msg_t      putreq;
228                 kra_putack_msg_t      putack;
229                 kra_get_msg_t         get;
230                 kra_completion_msg_t  completion;
231         }                    ram_u;
232         __u32             ram_seq;              /* incrementing sequence number */
233 } kra_msg_t;
234
235 #define RANAL_MSG_MAGIC     LNET_PROTO_RA_MAGIC /* unique magic */
236 #define RANAL_MSG_VERSION              1        /* current protocol version */
237
238 #define RANAL_MSG_FENCE             0x80        /* fence RDMA */
239
240 #define RANAL_MSG_NONE              0x00        /* illegal message */
241 #define RANAL_MSG_NOOP              0x01        /* empty ram_u (keepalive) */
242 #define RANAL_MSG_IMMEDIATE         0x02        /* ram_u.immediate */
243 #define RANAL_MSG_PUT_REQ           0x03        /* ram_u.putreq (src->sink) */
244 #define RANAL_MSG_PUT_NAK           0x04        /* ram_u.completion (no PUT match: sink->src) */
245 #define RANAL_MSG_PUT_ACK           0x05        /* ram_u.putack (PUT matched: sink->src) */
246 #define RANAL_MSG_PUT_DONE          0x86        /* ram_u.completion (src->sink) */
247 #define RANAL_MSG_GET_REQ           0x07        /* ram_u.get (sink->src) */
248 #define RANAL_MSG_GET_NAK           0x08        /* ram_u.completion (no GET match: src->sink) */
249 #define RANAL_MSG_GET_DONE          0x89        /* ram_u.completion (src->sink) */
250 #define RANAL_MSG_CLOSE             0x8a        /* empty ram_u */
251
252 /***********************************************************************/
253
254 typedef struct kra_tx                           /* message descriptor */
255 {
256         struct list_head          tx_list;      /* queue on idle_txs/rac_sendq/rac_waitq */
257         struct kra_conn          *tx_conn;      /* owning conn */
258         lnet_msg_t               *tx_lntmsg[2]; /* ptl msgs to finalize on completion */
259         unsigned long             tx_qtime;     /* when tx started to wait for something (jiffies) */
260         int                       tx_nob;       /* # bytes of payload */
261         int                       tx_buftype;   /* payload buffer type */
262         void                     *tx_buffer;    /* source/sink buffer */
263         int                       tx_phys_offset; /* first page offset (if phys) */
264         int                       tx_phys_npages; /* # physical pages */
265         RAP_PHYS_REGION          *tx_phys;      /* page descriptors */
266         RAP_MEM_KEY               tx_map_key;   /* mapping key */
267         RAP_RDMA_DESCRIPTOR       tx_rdma_desc; /* rdma descriptor */
268         __u64                     tx_cookie;    /* identify this tx to peer */
269         kra_msg_t                 tx_msg;       /* FMA message buffer */
270 } kra_tx_t;
271
272 #define RANAL_BUF_NONE           0              /* buffer type not set */
273 #define RANAL_BUF_IMMEDIATE      1              /* immediate data */
274 #define RANAL_BUF_PHYS_UNMAPPED  2              /* physical: not mapped yet */
275 #define RANAL_BUF_PHYS_MAPPED    3              /* physical: mapped already */
276 #define RANAL_BUF_VIRT_UNMAPPED  4              /* virtual: not mapped yet */
277 #define RANAL_BUF_VIRT_MAPPED    5              /* virtual: mapped already */
278
279 typedef struct kra_conn
280 {
281         struct kra_peer    *rac_peer;           /* owning peer */
282         struct list_head    rac_list;           /* stash on peer's conn list */
283         struct list_head    rac_hashlist;       /* stash in connection hash table */
284         struct list_head    rac_schedlist;      /* schedule (on rad_???_conns) for attention */
285         struct list_head    rac_fmaq;           /* txs queued for FMA */
286         struct list_head    rac_rdmaq;          /* txs awaiting RDMA completion */
287         struct list_head    rac_replyq;         /* txs awaiting replies */
288         __u64               rac_peerstamp;      /* peer's unique stamp */
289         __u64               rac_peer_connstamp; /* peer's unique connection stamp */
290         __u64               rac_my_connstamp;   /* my unique connection stamp */
291         unsigned long       rac_last_tx;        /* when I last sent an FMA message (jiffies) */
292         unsigned long       rac_last_rx;        /* when I last received an FMA messages (jiffies) */
293         long                rac_keepalive;      /* keepalive interval (seconds) */
294         long                rac_timeout;        /* infer peer death if no rx for this many seconds */
295         __u32               rac_cqid;           /* my completion callback id (non-unique) */
296         __u32               rac_tx_seq;         /* tx msg sequence number */
297         __u32               rac_rx_seq;         /* rx msg sequence number */
298         atomic_t            rac_refcount;       /* # users */
299         unsigned int        rac_close_sent;     /* I've sent CLOSE */
300         unsigned int        rac_close_recvd;    /* I've received CLOSE */
301         unsigned int        rac_state;          /* connection state */
302         unsigned int        rac_scheduled;      /* being attented to */
303         spinlock_t          rac_lock;           /* serialise */
304         kra_device_t       *rac_device;         /* which device */
305         RAP_PVOID           rac_rihandle;       /* RA endpoint */
306         kra_msg_t          *rac_rxmsg;          /* incoming message (FMA prefix) */
307         kra_msg_t           rac_msg;            /* keepalive/CLOSE message buffer */
308 } kra_conn_t;
309
310 #define RANAL_CONN_ESTABLISHED     0
311 #define RANAL_CONN_CLOSING         1
312 #define RANAL_CONN_CLOSED          2
313
314 typedef struct kra_peer
315 {
316         struct list_head    rap_list;           /* stash on global peer list */
317         struct list_head    rap_connd_list;     /* schedule on kra_connd_peers */
318         struct list_head    rap_conns;          /* all active connections */
319         struct list_head    rap_tx_queue;       /* msgs waiting for a conn */
320         lnet_nid_t           rap_nid;            /* who's on the other end(s) */
321         __u32               rap_ip;             /* IP address of peer */
322         int                 rap_port;           /* port on which peer listens */
323         atomic_t            rap_refcount;       /* # users */
324         int                 rap_persistence;    /* "known" peer refs */
325         int                 rap_connecting;     /* connection forming */
326         unsigned long       rap_reconnect_time; /* CURRENT_SECONDS when reconnect OK */
327         unsigned long       rap_reconnect_interval; /* exponential backoff */
328 } kra_peer_t;
329
330 extern kra_data_t      kranal_data;
331 extern kra_tunables_t  kranal_tunables;
332
333 extern void kranal_destroy_peer(kra_peer_t *peer);
334 extern void kranal_destroy_conn(kra_conn_t *conn);
335
336 static inline void
337 kranal_peer_addref(kra_peer_t *peer)
338 {
339         CDEBUG(D_NET, "%p->%s\n", peer, libcfs_nid2str(peer->rap_nid));
340         LASSERT(atomic_read(&peer->rap_refcount) > 0);
341         atomic_inc(&peer->rap_refcount);
342 }
343
344 static inline void
345 kranal_peer_decref(kra_peer_t *peer)
346 {
347         CDEBUG(D_NET, "%p->%s\n", peer, libcfs_nid2str(peer->rap_nid));
348         LASSERT(atomic_read(&peer->rap_refcount) > 0);
349         if (atomic_dec_and_test(&peer->rap_refcount))
350                 kranal_destroy_peer(peer);
351 }
352
353 static inline struct list_head *
354 kranal_nid2peerlist (lnet_nid_t nid)
355 {
356         unsigned int hash = ((unsigned int)nid) % kranal_data.kra_peer_hash_size;
357
358         return (&kranal_data.kra_peers[hash]);
359 }
360
361 static inline int
362 kranal_peer_active(kra_peer_t *peer)
363 {
364         /* Am I in the peer hash table? */
365         return (!list_empty(&peer->rap_list));
366 }
367
368 static inline void
369 kranal_conn_addref(kra_conn_t *conn)
370 {
371         CDEBUG(D_NET, "%p->%s\n", conn, 
372                libcfs_nid2str(conn->rac_peer->rap_nid));
373         LASSERT(atomic_read(&conn->rac_refcount) > 0);
374         atomic_inc(&conn->rac_refcount);
375 }
376
377 static inline void
378 kranal_conn_decref(kra_conn_t *conn)
379 {
380         CDEBUG(D_NET, "%p->%s\n", conn,
381                libcfs_nid2str(conn->rac_peer->rap_nid));
382         LASSERT(atomic_read(&conn->rac_refcount) > 0);
383         if (atomic_dec_and_test(&conn->rac_refcount))
384                 kranal_destroy_conn(conn);
385 }
386
387 static inline struct list_head *
388 kranal_cqid2connlist (__u32 cqid)
389 {
390         unsigned int hash = cqid % kranal_data.kra_conn_hash_size;
391
392         return (&kranal_data.kra_conns [hash]);
393 }
394
395 static inline kra_conn_t *
396 kranal_cqid2conn_locked (__u32 cqid)
397 {
398         struct list_head *conns = kranal_cqid2connlist(cqid);
399         struct list_head *tmp;
400         kra_conn_t       *conn;
401
402         list_for_each(tmp, conns) {
403                 conn = list_entry(tmp, kra_conn_t, rac_hashlist);
404
405                 if (conn->rac_cqid == cqid)
406                         return conn;
407         }
408
409         return NULL;
410 }
411
412 static inline int
413 kranal_tx_mapped (kra_tx_t *tx)
414 {
415         return (tx->tx_buftype == RANAL_BUF_VIRT_MAPPED ||
416                 tx->tx_buftype == RANAL_BUF_PHYS_MAPPED);
417 }
418
419 int kranal_startup (lnet_ni_t *ni);
420 void kranal_shutdown (lnet_ni_t *ni);
421 int kranal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
422 int kranal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
423 int kranal_eager_recv(lnet_ni_t *ni, void *private, 
424                         lnet_msg_t *lntmsg, void **new_private);
425 int kranal_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, 
426                 int delayed, unsigned int niov, 
427                 struct iovec *iov, lnet_kiov_t *kiov,
428                 unsigned int offset, unsigned int mlen, unsigned int rlen);
429 int kranal_accept(lnet_ni_t *ni, struct socket *sock);
430
431 extern void kranal_free_acceptsock (kra_acceptsock_t *ras);
432 extern int kranal_listener_procint (cfs_sysctl_table_t *table,
433                                     int write, struct file *filp,
434                                     void *buffer, size_t *lenp);
435 extern void kranal_update_reaper_timeout (long timeout);
436 extern void kranal_tx_done (kra_tx_t *tx, int completion);
437 extern void kranal_unlink_peer_locked (kra_peer_t *peer);
438 extern void kranal_schedule_conn (kra_conn_t *conn);
439 extern int kranal_create_peer (kra_peer_t **peerp, lnet_nid_t nid);
440 extern int kranal_add_persistent_peer (lnet_nid_t nid, __u32 ip, int port);
441 extern kra_peer_t *kranal_find_peer_locked (lnet_nid_t nid);
442 extern void kranal_post_fma (kra_conn_t *conn, kra_tx_t *tx);
443 extern int kranal_del_peer (lnet_nid_t nid);
444 extern void kranal_device_callback (RAP_INT32 devid, RAP_PVOID arg);
445 extern int kranal_thread_start (int(*fn)(void *arg), void *arg);
446 extern int kranal_connd (void *arg);
447 extern int kranal_reaper (void *arg);
448 extern int kranal_scheduler (void *arg);
449 extern void kranal_close_conn_locked (kra_conn_t *conn, int error);
450 extern void kranal_close_conn (kra_conn_t *conn, int error);
451 extern void kranal_terminate_conn_locked (kra_conn_t *conn);
452 extern void kranal_connect (kra_peer_t *peer);
453 extern int kranal_conn_handshake (struct socket *sock, kra_peer_t *peer);
454 extern int kranal_tunables_init(void);
455 extern void kranal_tunables_fini(void);
456 extern void kranal_init_msg(kra_msg_t *msg, int type);