Whamcloud - gitweb
b=16098
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/klnds/ralnd/ralnd.h
37  *
38  * Author: Eric Barton <eric@bartonsoftware.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44 #ifndef AUTOCONF_INCLUDED
45 #include <linux/config.h>
46 #endif
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/mm.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>
56
57 #include <asm/system.h>
58 #include <asm/uaccess.h>
59 #include <asm/io.h>
60
61 #include <linux/init.h>
62 #include <linux/fs.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
69 #include <net/sock.h>
70 #include <linux/in.h>
71
72 #define DEBUG_SUBSYSTEM S_LND
73
74 #include <libcfs/libcfs.h>
75 #include <lnet/lnet.h>
76 #include <lnet/lib-lnet.h>
77
78 #include <rapl.h>
79
80 /* tunables determined at compile time */
81 #define RANAL_RESCHED             100           /* # scheduler loops before reschedule */
82
83 #define RANAL_PEER_HASH_SIZE      101           /* # peer lists */
84 #define RANAL_CONN_HASH_SIZE      101           /* # conn lists */
85
86 #define RANAL_MIN_TIMEOUT         5             /* minimum timeout interval (seconds) */
87 #define RANAL_TIMEOUT2KEEPALIVE(t) (((t)+1)/2)  /* timeout -> keepalive interval */
88
89 /* fixed constants */
90 #define RANAL_MAXDEVS             2             /* max # devices RapidArray supports */
91 #define RANAL_FMA_MAX_PREFIX      232           /* max bytes in FMA "Prefix" we can use */
92 #define RANAL_FMA_MAX_DATA        ((7<<10)-256) /* Max FMA MSG is 7K including prefix */
93
94
95 typedef struct
96 {
97         int              *kra_n_connd;          /* # connection daemons */
98         int              *kra_min_reconnect_interval; /* first failed connection retry... */
99         int              *kra_max_reconnect_interval; /* ...exponentially increasing to this */
100         int              *kra_ntx;              /* # tx descs */
101         int              *kra_credits;          /* # concurrent sends */
102         int              *kra_peercredits;      /* # concurrent sends to 1 peer */
103         int              *kra_fma_cq_size;      /* # entries in receive CQ */
104         int              *kra_timeout;          /* comms timeout (seconds) */
105         int              *kra_max_immediate;    /* immediate payload breakpoint */
106
107 #if CONFIG_SYSCTL && !CFS_SYSFS_MODULE_PARM
108         cfs_sysctl_table_header_t *kra_sysctl;  /* sysctl interface */
109 #endif
110 } kra_tunables_t;
111
112 typedef struct
113 {
114         RAP_PVOID               rad_handle;     /* device handle */
115         RAP_PVOID               rad_fma_cqh;    /* FMA completion queue handle */
116         RAP_PVOID               rad_rdma_cqh;   /* rdma completion queue handle */
117         int                     rad_id;         /* device id */
118         int                     rad_idx;        /* index in kra_devices */
119         int                     rad_ready;      /* set by device callback */
120         struct list_head        rad_ready_conns;/* connections ready to tx/rx */
121         struct list_head        rad_new_conns;  /* new connections to complete */
122         wait_queue_head_t       rad_waitq;      /* scheduler waits here */
123         spinlock_t              rad_lock;       /* serialise */
124         void                   *rad_scheduler;  /* scheduling thread */
125         unsigned int            rad_nphysmap;   /* # phys mappings */
126         unsigned int            rad_nppphysmap; /* # phys pages mapped */
127         unsigned int            rad_nvirtmap;   /* # virt mappings */
128         unsigned long           rad_nobvirtmap; /* # virt bytes mapped */
129 } kra_device_t;
130
131 typedef struct
132 {
133         int               kra_init;             /* initialisation state */
134         int               kra_shutdown;         /* shut down? */
135         atomic_t          kra_nthreads;         /* # live threads */
136         lnet_ni_t        *kra_ni;               /* _the_ nal instance */
137         
138         kra_device_t      kra_devices[RANAL_MAXDEVS]; /* device/ptag/cq etc */
139         int               kra_ndevs;            /* # devices */
140
141         rwlock_t          kra_global_lock;      /* stabilize peer/conn ops */
142
143         struct list_head *kra_peers;            /* hash table of all my known peers */
144         int               kra_peer_hash_size;   /* size of kra_peers */
145         atomic_t          kra_npeers;           /* # peers extant */
146         int               kra_nonewpeers;       /* prevent new peers */
147
148         struct list_head *kra_conns;            /* conns hashed by cqid */
149         int               kra_conn_hash_size;   /* size of kra_conns */
150         __u64             kra_peerstamp;        /* when I started up */
151         __u64             kra_connstamp;        /* conn stamp generator */
152         int               kra_next_cqid;        /* cqid generator */
153         atomic_t          kra_nconns;           /* # connections extant */
154
155         long              kra_new_min_timeout;  /* minimum timeout on any new conn */
156         wait_queue_head_t kra_reaper_waitq;     /* reaper sleeps here */
157         spinlock_t        kra_reaper_lock;      /* serialise */
158
159         struct list_head  kra_connd_peers;      /* peers waiting for a connection */
160         struct list_head  kra_connd_acceptq;    /* accepted sockets to handshake */
161         wait_queue_head_t kra_connd_waitq;      /* connection daemons sleep here */
162         spinlock_t        kra_connd_lock;       /* serialise */
163
164         struct list_head  kra_idle_txs;         /* idle tx descriptors */
165         __u64             kra_next_tx_cookie;   /* RDMA completion cookie */
166         spinlock_t        kra_tx_lock;          /* serialise */
167 } kra_data_t;
168
169 #define RANAL_INIT_NOTHING         0
170 #define RANAL_INIT_DATA            1
171 #define RANAL_INIT_ALL             2
172
173 typedef struct kra_acceptsock                   /* accepted socket queued for connd */
174 {
175         struct list_head     ras_list;          /* queue for attention */
176         struct socket       *ras_sock;          /* the accepted socket */
177 } kra_acceptsock_t;
178
179 /************************************************************************
180  * Wire message structs.  These are sent in sender's byte order
181  * (i.e. receiver checks magic and flips if required).
182  */
183
184 typedef struct kra_connreq                      /* connection request/response */
185 {                                               /* (sent via socket) */
186         __u32             racr_magic;           /* I'm an ranal connreq */
187         __u16             racr_version;         /* this is my version number */
188         __u16             racr_devid;           /* sender's device ID */
189         __u64             racr_srcnid;          /* sender's NID */
190         __u64             racr_dstnid;          /* who sender expects to listen */
191         __u64             racr_peerstamp;       /* sender's instance stamp */
192         __u64             racr_connstamp;       /* sender's connection stamp */
193         __u32             racr_timeout;         /* sender's timeout */
194         RAP_RI_PARAMETERS racr_riparams;        /* sender's endpoint info */
195 } kra_connreq_t;
196
197 typedef struct
198 {
199         RAP_MEM_KEY       rard_key;
200         RAP_PVOID64       rard_addr;
201         RAP_UINT32        rard_nob;
202 } kra_rdma_desc_t;
203
204 typedef struct
205 {
206         lnet_hdr_t        raim_hdr;             /* portals header */
207         /* Portals payload is in FMA "Message Data" */
208 } kra_immediate_msg_t;
209
210 typedef struct
211 {
212         lnet_hdr_t        raprm_hdr;            /* portals header */
213         __u64             raprm_cookie;         /* opaque completion cookie */
214 } kra_putreq_msg_t;
215
216 typedef struct
217 {
218         __u64             rapam_src_cookie;     /* reflected completion cookie */
219         __u64             rapam_dst_cookie;     /* opaque completion cookie */
220         kra_rdma_desc_t   rapam_desc;           /* sender's sink buffer */
221 } kra_putack_msg_t;
222
223 typedef struct
224 {
225         lnet_hdr_t        ragm_hdr;             /* portals header */
226         __u64             ragm_cookie;          /* opaque completion cookie */
227         kra_rdma_desc_t   ragm_desc;            /* sender's sink buffer */
228 } kra_get_msg_t;
229
230 typedef struct
231 {
232         __u64             racm_cookie;          /* reflected completion cookie */
233 } kra_completion_msg_t;
234
235 typedef struct                                  /* NB must fit in FMA "Prefix" */
236 {
237         __u32             ram_magic;            /* I'm an ranal message */
238         __u16             ram_version;          /* this is my version number */
239         __u16             ram_type;             /* msg type */
240         __u64             ram_srcnid;           /* sender's NID */
241         __u64             ram_connstamp;        /* sender's connection stamp */
242         union {
243                 kra_immediate_msg_t   immediate;
244                 kra_putreq_msg_t      putreq;
245                 kra_putack_msg_t      putack;
246                 kra_get_msg_t         get;
247                 kra_completion_msg_t  completion;
248         }                    ram_u;
249         __u32             ram_seq;              /* incrementing sequence number */
250 } kra_msg_t;
251
252 #define RANAL_MSG_MAGIC     LNET_PROTO_RA_MAGIC /* unique magic */
253 #define RANAL_MSG_VERSION              1        /* current protocol version */
254
255 #define RANAL_MSG_FENCE             0x80        /* fence RDMA */
256
257 #define RANAL_MSG_NONE              0x00        /* illegal message */
258 #define RANAL_MSG_NOOP              0x01        /* empty ram_u (keepalive) */
259 #define RANAL_MSG_IMMEDIATE         0x02        /* ram_u.immediate */
260 #define RANAL_MSG_PUT_REQ           0x03        /* ram_u.putreq (src->sink) */
261 #define RANAL_MSG_PUT_NAK           0x04        /* ram_u.completion (no PUT match: sink->src) */
262 #define RANAL_MSG_PUT_ACK           0x05        /* ram_u.putack (PUT matched: sink->src) */
263 #define RANAL_MSG_PUT_DONE          0x86        /* ram_u.completion (src->sink) */
264 #define RANAL_MSG_GET_REQ           0x07        /* ram_u.get (sink->src) */
265 #define RANAL_MSG_GET_NAK           0x08        /* ram_u.completion (no GET match: src->sink) */
266 #define RANAL_MSG_GET_DONE          0x89        /* ram_u.completion (src->sink) */
267 #define RANAL_MSG_CLOSE             0x8a        /* empty ram_u */
268
269 /***********************************************************************/
270
271 typedef struct kra_tx                           /* message descriptor */
272 {
273         struct list_head          tx_list;      /* queue on idle_txs/rac_sendq/rac_waitq */
274         struct kra_conn          *tx_conn;      /* owning conn */
275         lnet_msg_t               *tx_lntmsg[2]; /* ptl msgs to finalize on completion */
276         unsigned long             tx_qtime;     /* when tx started to wait for something (jiffies) */
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         lnet_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 extern kra_data_t      kranal_data;
348 extern kra_tunables_t  kranal_tunables;
349
350 extern void kranal_destroy_peer(kra_peer_t *peer);
351 extern void kranal_destroy_conn(kra_conn_t *conn);
352
353 static inline void
354 kranal_peer_addref(kra_peer_t *peer)
355 {
356         CDEBUG(D_NET, "%p->%s\n", peer, libcfs_nid2str(peer->rap_nid));
357         LASSERT(atomic_read(&peer->rap_refcount) > 0);
358         atomic_inc(&peer->rap_refcount);
359 }
360
361 static inline void
362 kranal_peer_decref(kra_peer_t *peer)
363 {
364         CDEBUG(D_NET, "%p->%s\n", peer, libcfs_nid2str(peer->rap_nid));
365         LASSERT(atomic_read(&peer->rap_refcount) > 0);
366         if (atomic_dec_and_test(&peer->rap_refcount))
367                 kranal_destroy_peer(peer);
368 }
369
370 static inline struct list_head *
371 kranal_nid2peerlist (lnet_nid_t nid)
372 {
373         unsigned int hash = ((unsigned int)nid) % kranal_data.kra_peer_hash_size;
374
375         return (&kranal_data.kra_peers[hash]);
376 }
377
378 static inline int
379 kranal_peer_active(kra_peer_t *peer)
380 {
381         /* Am I in the peer hash table? */
382         return (!list_empty(&peer->rap_list));
383 }
384
385 static inline void
386 kranal_conn_addref(kra_conn_t *conn)
387 {
388         CDEBUG(D_NET, "%p->%s\n", conn, 
389                libcfs_nid2str(conn->rac_peer->rap_nid));
390         LASSERT(atomic_read(&conn->rac_refcount) > 0);
391         atomic_inc(&conn->rac_refcount);
392 }
393
394 static inline void
395 kranal_conn_decref(kra_conn_t *conn)
396 {
397         CDEBUG(D_NET, "%p->%s\n", conn,
398                libcfs_nid2str(conn->rac_peer->rap_nid));
399         LASSERT(atomic_read(&conn->rac_refcount) > 0);
400         if (atomic_dec_and_test(&conn->rac_refcount))
401                 kranal_destroy_conn(conn);
402 }
403
404 static inline struct list_head *
405 kranal_cqid2connlist (__u32 cqid)
406 {
407         unsigned int hash = cqid % kranal_data.kra_conn_hash_size;
408
409         return (&kranal_data.kra_conns [hash]);
410 }
411
412 static inline kra_conn_t *
413 kranal_cqid2conn_locked (__u32 cqid)
414 {
415         struct list_head *conns = kranal_cqid2connlist(cqid);
416         struct list_head *tmp;
417         kra_conn_t       *conn;
418
419         list_for_each(tmp, conns) {
420                 conn = list_entry(tmp, kra_conn_t, rac_hashlist);
421
422                 if (conn->rac_cqid == cqid)
423                         return conn;
424         }
425
426         return NULL;
427 }
428
429 static inline int
430 kranal_tx_mapped (kra_tx_t *tx)
431 {
432         return (tx->tx_buftype == RANAL_BUF_VIRT_MAPPED ||
433                 tx->tx_buftype == RANAL_BUF_PHYS_MAPPED);
434 }
435
436 int kranal_startup (lnet_ni_t *ni);
437 void kranal_shutdown (lnet_ni_t *ni);
438 int kranal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
439 int kranal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
440 int kranal_eager_recv(lnet_ni_t *ni, void *private, 
441                         lnet_msg_t *lntmsg, void **new_private);
442 int kranal_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, 
443                 int delayed, unsigned int niov, 
444                 struct iovec *iov, lnet_kiov_t *kiov,
445                 unsigned int offset, unsigned int mlen, unsigned int rlen);
446 int kranal_accept(lnet_ni_t *ni, struct socket *sock);
447
448 extern void kranal_free_acceptsock (kra_acceptsock_t *ras);
449 extern int kranal_listener_procint (cfs_sysctl_table_t *table,
450                                     int write, struct file *filp,
451                                     void *buffer, size_t *lenp);
452 extern void kranal_update_reaper_timeout (long timeout);
453 extern void kranal_tx_done (kra_tx_t *tx, int completion);
454 extern void kranal_unlink_peer_locked (kra_peer_t *peer);
455 extern void kranal_schedule_conn (kra_conn_t *conn);
456 extern int kranal_create_peer (kra_peer_t **peerp, lnet_nid_t nid);
457 extern int kranal_add_persistent_peer (lnet_nid_t nid, __u32 ip, int port);
458 extern kra_peer_t *kranal_find_peer_locked (lnet_nid_t nid);
459 extern void kranal_post_fma (kra_conn_t *conn, kra_tx_t *tx);
460 extern int kranal_del_peer (lnet_nid_t nid);
461 extern void kranal_device_callback (RAP_INT32 devid, RAP_PVOID arg);
462 extern int kranal_thread_start (int(*fn)(void *arg), void *arg);
463 extern int kranal_connd (void *arg);
464 extern int kranal_reaper (void *arg);
465 extern int kranal_scheduler (void *arg);
466 extern void kranal_close_conn_locked (kra_conn_t *conn, int error);
467 extern void kranal_close_conn (kra_conn_t *conn, int error);
468 extern void kranal_terminate_conn_locked (kra_conn_t *conn);
469 extern void kranal_connect (kra_peer_t *peer);
470 extern int kranal_conn_handshake (struct socket *sock, kra_peer_t *peer);
471 extern int kranal_tunables_init(void);
472 extern void kranal_tunables_fini(void);
473 extern void kranal_init_msg(kra_msg_t *msg, int type);