Whamcloud - gitweb
b4c3f610c193aa715cf96a25871237aaf57caba9
[fs/lustre-release.git] / lnet / klnds / ralnd / ralnd.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lnet/klnds/ralnd/ralnd.h
35  *
36  * Author: Eric Barton <eric@bartonsoftware.com>
37  */
38
39 #ifndef EXPORT_SYMTAB
40 # define EXPORT_SYMTAB
41 #endif
42 #ifndef AUTOCONF_INCLUDED
43 #include <linux/config.h>
44 #endif
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/mm.h>
48 #include <linux/string.h>
49 #include <linux/stat.h>
50 #include <linux/errno.h>
51 #include <linux/smp_lock.h>
52 #include <linux/unistd.h>
53 #include <linux/uio.h>
54
55 #include <asm/system.h>
56 #include <asm/uaccess.h>
57 #include <asm/io.h>
58
59 #include <linux/init.h>
60 #include <linux/fs.h>
61 #include <linux/file.h>
62 #include <linux/stat.h>
63 #include <linux/list.h>
64 #include <linux/kmod.h>
65 #include <linux/sysctl.h>
66
67 #include <net/sock.h>
68 #include <linux/in.h>
69
70 #define DEBUG_SUBSYSTEM S_LND
71
72 #include <libcfs/libcfs.h>
73 #include <lnet/lnet.h>
74 #include <lnet/lib-lnet.h>
75 #include <lnet/lnet-sysctl.h>
76
77 #include <rapl.h>
78
79 /* tunables determined at compile time */
80 #define RANAL_RESCHED             100           /* # scheduler loops before reschedule */
81
82 #define RANAL_PEER_HASH_SIZE      101           /* # peer lists */
83 #define RANAL_CONN_HASH_SIZE      101           /* # conn lists */
84
85 #define RANAL_MIN_TIMEOUT         5             /* minimum timeout interval (seconds) */
86 #define RANAL_TIMEOUT2KEEPALIVE(t) (((t)+1)/2)  /* timeout -> keepalive interval */
87
88 /* fixed constants */
89 #define RANAL_MAXDEVS             2             /* max # devices RapidArray supports */
90 #define RANAL_FMA_MAX_PREFIX      232           /* max bytes in FMA "Prefix" we can use */
91 #define RANAL_FMA_MAX_DATA        ((7<<10)-256) /* Max FMA MSG is 7K including prefix */
92
93
94 typedef struct
95 {
96         int              *kra_n_connd;          /* # connection daemons */
97         int              *kra_min_reconnect_interval; /* first failed connection retry... */
98         int              *kra_max_reconnect_interval; /* ...exponentially increasing to this */
99         int              *kra_ntx;              /* # tx descs */
100         int              *kra_credits;          /* # concurrent sends */
101         int              *kra_peercredits;      /* # concurrent sends to 1 peer */
102         int              *kra_fma_cq_size;      /* # entries in receive CQ */
103         int              *kra_timeout;          /* comms timeout (seconds) */
104         int              *kra_max_immediate;    /* immediate payload breakpoint */
105
106 #if CONFIG_SYSCTL && !CFS_SYSFS_MODULE_PARM
107         cfs_sysctl_table_header_t *kra_sysctl;  /* sysctl interface */
108 #endif
109 } kra_tunables_t;
110
111 typedef struct
112 {
113         RAP_PVOID              rad_handle;    /* device handle */
114         RAP_PVOID              rad_fma_cqh;   /* FMA completion queue handle */
115         RAP_PVOID              rad_rdma_cqh;  /* rdma completion queue handle */
116         int                    rad_id;        /* device id */
117         int                    rad_idx;       /* index in kra_devices */
118         int                    rad_ready;     /* set by device callback */
119         cfs_list_t             rad_ready_conns;/* connections ready to tx/rx */
120         cfs_list_t             rad_new_conns; /* new connections to complete */
121         cfs_waitq_t            rad_waitq;     /* scheduler waits here */
122         cfs_spinlock_t         rad_lock;      /* serialise */
123         void                  *rad_scheduler; /* scheduling thread */
124         unsigned int           rad_nphysmap;  /* # phys mappings */
125         unsigned int           rad_nppphysmap;/* # phys pages mapped */
126         unsigned int           rad_nvirtmap;  /* # virt mappings */
127         unsigned long          rad_nobvirtmap;/* # virt bytes mapped */
128 } kra_device_t;
129
130 typedef struct
131 {
132         int               kra_init;            /* initialisation state */
133         int               kra_shutdown;        /* shut down? */
134         cfs_atomic_t      kra_nthreads;        /* # live threads */
135         lnet_ni_t        *kra_ni;              /* _the_ nal instance */
136
137         kra_device_t      kra_devices[RANAL_MAXDEVS]; /* device/ptag/cq */
138         int               kra_ndevs;           /* # devices */
139
140         cfs_rwlock_t      kra_global_lock;     /* stabilize peer/conn ops */
141
142         cfs_list_t       *kra_peers;           /* hash table of all my known peers */
143         int               kra_peer_hash_size;  /* size of kra_peers */
144         cfs_atomic_t      kra_npeers;          /* # peers extant */
145         int               kra_nonewpeers;      /* prevent new peers */
146
147         cfs_list_t       *kra_conns;           /* conns hashed by cqid */
148         int               kra_conn_hash_size;  /* size of kra_conns */
149         __u64             kra_peerstamp;       /* when I started up */
150         __u64             kra_connstamp;       /* conn stamp generator */
151         int               kra_next_cqid;       /* cqid generator */
152         cfs_atomic_t      kra_nconns;          /* # connections extant */
153
154         long              kra_new_min_timeout; /* minimum timeout on any new conn */
155         cfs_waitq_t       kra_reaper_waitq;    /* reaper sleeps here */
156         cfs_spinlock_t    kra_reaper_lock;     /* serialise */
157
158         cfs_list_t        kra_connd_peers;     /* peers waiting for a connection */
159         cfs_list_t        kra_connd_acceptq;   /* accepted sockets to handshake */
160         cfs_waitq_t       kra_connd_waitq;     /* connection daemons sleep here */
161         cfs_spinlock_t    kra_connd_lock;      /* serialise */
162
163         cfs_list_t        kra_idle_txs;        /* idle tx descriptors */
164         __u64             kra_next_tx_cookie;  /* RDMA completion cookie */
165         cfs_spinlock_t    kra_tx_lock;         /* serialise */
166 } kra_data_t;
167
168 #define RANAL_INIT_NOTHING         0
169 #define RANAL_INIT_DATA            1
170 #define RANAL_INIT_ALL             2
171
172 typedef struct kra_acceptsock             /* accepted socket queued for connd */
173 {
174         cfs_list_t           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         lnet_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         lnet_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         lnet_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     LNET_PROTO_RA_MAGIC /* 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         cfs_list_t            tx_list;      /* queue on idle_txs/rac_sendq/rac_waitq */
273         struct kra_conn      *tx_conn;      /* owning conn */
274         lnet_msg_t           *tx_lntmsg[2]; /* ptl msgs to finalize on completion */
275         unsigned long         tx_qtime;     /* when tx started to wait for something (jiffies) */
276         int                   tx_nob;       /* # bytes of payload */
277         int                   tx_buftype;   /* payload buffer type */
278         void                 *tx_buffer;    /* source/sink buffer */
279         int                   tx_phys_offset; /* first page offset (if phys) */
280         int                   tx_phys_npages; /* # physical pages */
281         RAP_PHYS_REGION      *tx_phys;      /* page descriptors */
282         RAP_MEM_KEY           tx_map_key;   /* mapping key */
283         RAP_RDMA_DESCRIPTOR   tx_rdma_desc; /* rdma descriptor */
284         __u64                 tx_cookie;    /* identify this tx to peer */
285         kra_msg_t             tx_msg;       /* FMA message buffer */
286 } kra_tx_t;
287
288 #define RANAL_BUF_NONE           0              /* buffer type not set */
289 #define RANAL_BUF_IMMEDIATE      1              /* immediate data */
290 #define RANAL_BUF_PHYS_UNMAPPED  2              /* physical: not mapped yet */
291 #define RANAL_BUF_PHYS_MAPPED    3              /* physical: mapped already */
292 #define RANAL_BUF_VIRT_UNMAPPED  4              /* virtual: not mapped yet */
293 #define RANAL_BUF_VIRT_MAPPED    5              /* virtual: mapped already */
294
295 typedef struct kra_conn
296 {
297         struct kra_peer    *rac_peer;           /* owning peer */
298         cfs_list_t          rac_list;          /* stash on peer's conn list */
299         cfs_list_t          rac_hashlist;      /* stash in connection hash table */
300         cfs_list_t          rac_schedlist;     /* schedule (on rad_???_conns) for attention */
301         cfs_list_t          rac_fmaq;          /* txs queued for FMA */
302         cfs_list_t          rac_rdmaq;         /* txs awaiting RDMA completion */
303         cfs_list_t          rac_replyq;        /* txs awaiting replies */
304         __u64               rac_peerstamp;     /* peer's unique stamp */
305         __u64               rac_peer_connstamp;/* peer's unique connection stamp */
306         __u64               rac_my_connstamp;  /* my unique connection stamp */
307         unsigned long       rac_last_tx;       /* when I last sent an FMA message (jiffies) */
308         unsigned long       rac_last_rx;       /* when I last received an FMA messages (jiffies) */
309         long                rac_keepalive;     /* keepalive interval (seconds) */
310         long                rac_timeout;       /* infer peer death if no rx for this many seconds */
311         __u32               rac_cqid;          /* my completion callback id (non-unique) */
312         __u32               rac_tx_seq;        /* tx msg sequence number */
313         __u32               rac_rx_seq;        /* rx msg sequence number */
314         cfs_atomic_t        rac_refcount;      /* # users */
315         unsigned int        rac_close_sent;    /* I've sent CLOSE */
316         unsigned int        rac_close_recvd;   /* I've received CLOSE */
317         unsigned int        rac_state;         /* connection state */
318         unsigned int        rac_scheduled;     /* being attented to */
319         cfs_spinlock_t      rac_lock;          /* serialise */
320         kra_device_t       *rac_device;        /* which device */
321         RAP_PVOID           rac_rihandle;      /* RA endpoint */
322         kra_msg_t          *rac_rxmsg;         /* incoming message (FMA prefix) */
323         kra_msg_t           rac_msg;           /* keepalive/CLOSE message buffer */
324 } kra_conn_t;
325
326 #define RANAL_CONN_ESTABLISHED     0
327 #define RANAL_CONN_CLOSING         1
328 #define RANAL_CONN_CLOSED          2
329
330 typedef struct kra_peer
331 {
332         cfs_list_t          rap_list;         /* stash on global peer list */
333         cfs_list_t          rap_connd_list;   /* schedule on kra_connd_peers */
334         cfs_list_t          rap_conns;        /* all active connections */
335         cfs_list_t          rap_tx_queue;     /* msgs waiting for a conn */
336         lnet_nid_t          rap_nid;          /* who's on the other end(s) */
337         __u32               rap_ip;           /* IP address of peer */
338         int                 rap_port;         /* port on which peer listens */
339         cfs_atomic_t        rap_refcount;     /* # users */
340         int                 rap_persistence;  /* "known" peer refs */
341         int                 rap_connecting;   /* connection forming */
342         unsigned long       rap_reconnect_time; /* CURRENT_SECONDS when reconnect OK */
343         unsigned long       rap_reconnect_interval; /* exponential backoff */
344 } kra_peer_t;
345
346 extern kra_data_t      kranal_data;
347 extern kra_tunables_t  kranal_tunables;
348
349 extern void kranal_destroy_peer(kra_peer_t *peer);
350 extern void kranal_destroy_conn(kra_conn_t *conn);
351
352 static inline void
353 kranal_peer_addref(kra_peer_t *peer)
354 {
355         CDEBUG(D_NET, "%p->%s\n", peer, libcfs_nid2str(peer->rap_nid));
356         LASSERT(cfs_atomic_read(&peer->rap_refcount) > 0);
357         cfs_atomic_inc(&peer->rap_refcount);
358 }
359
360 static inline void
361 kranal_peer_decref(kra_peer_t *peer)
362 {
363         CDEBUG(D_NET, "%p->%s\n", peer, libcfs_nid2str(peer->rap_nid));
364         LASSERT(cfs_atomic_read(&peer->rap_refcount) > 0);
365         if (cfs_atomic_dec_and_test(&peer->rap_refcount))
366                 kranal_destroy_peer(peer);
367 }
368
369 static inline cfs_list_t *
370 kranal_nid2peerlist (lnet_nid_t nid)
371 {
372         unsigned int hash = ((unsigned int)nid) % kranal_data.kra_peer_hash_size;
373
374         return (&kranal_data.kra_peers[hash]);
375 }
376
377 static inline int
378 kranal_peer_active(kra_peer_t *peer)
379 {
380         /* Am I in the peer hash table? */
381         return (!cfs_list_empty(&peer->rap_list));
382 }
383
384 static inline void
385 kranal_conn_addref(kra_conn_t *conn)
386 {
387         CDEBUG(D_NET, "%p->%s\n", conn, 
388                libcfs_nid2str(conn->rac_peer->rap_nid));
389         LASSERT(cfs_atomic_read(&conn->rac_refcount) > 0);
390         cfs_atomic_inc(&conn->rac_refcount);
391 }
392
393 static inline void
394 kranal_conn_decref(kra_conn_t *conn)
395 {
396         CDEBUG(D_NET, "%p->%s\n", conn,
397                libcfs_nid2str(conn->rac_peer->rap_nid));
398         LASSERT(cfs_atomic_read(&conn->rac_refcount) > 0);
399         if (cfs_atomic_dec_and_test(&conn->rac_refcount))
400                 kranal_destroy_conn(conn);
401 }
402
403 static inline cfs_list_t *
404 kranal_cqid2connlist (__u32 cqid)
405 {
406         unsigned int hash = cqid % kranal_data.kra_conn_hash_size;
407
408         return (&kranal_data.kra_conns [hash]);
409 }
410
411 static inline kra_conn_t *
412 kranal_cqid2conn_locked (__u32 cqid)
413 {
414         cfs_list_t       *conns = kranal_cqid2connlist(cqid);
415         cfs_list_t       *tmp;
416         kra_conn_t       *conn;
417
418         cfs_list_for_each(tmp, conns) {
419                 conn = cfs_list_entry(tmp, kra_conn_t, rac_hashlist);
420
421                 if (conn->rac_cqid == cqid)
422                         return conn;
423         }
424
425         return NULL;
426 }
427
428 static inline int
429 kranal_tx_mapped (kra_tx_t *tx)
430 {
431         return (tx->tx_buftype == RANAL_BUF_VIRT_MAPPED ||
432                 tx->tx_buftype == RANAL_BUF_PHYS_MAPPED);
433 }
434
435 int kranal_startup (lnet_ni_t *ni);
436 void kranal_shutdown (lnet_ni_t *ni);
437 int kranal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
438 int kranal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
439 int kranal_eager_recv(lnet_ni_t *ni, void *private,
440                       lnet_msg_t *lntmsg, void **new_private);
441 int kranal_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg,
442                 int delayed, unsigned int niov,
443                 struct iovec *iov, lnet_kiov_t *kiov,
444                 unsigned int offset, unsigned int mlen, unsigned int rlen);
445 int kranal_accept(lnet_ni_t *ni, struct socket *sock);
446
447 extern void kranal_free_acceptsock (kra_acceptsock_t *ras);
448 extern int kranal_listener_procint (cfs_sysctl_table_t *table,
449                                     int write, struct file *filp,
450                                     void *buffer, size_t *lenp);
451 extern void kranal_update_reaper_timeout (long timeout);
452 extern void kranal_tx_done (kra_tx_t *tx, int completion);
453 extern void kranal_unlink_peer_locked (kra_peer_t *peer);
454 extern void kranal_schedule_conn (kra_conn_t *conn);
455 extern int kranal_create_peer (kra_peer_t **peerp, lnet_nid_t nid);
456 extern int kranal_add_persistent_peer (lnet_nid_t nid, __u32 ip, int port);
457 extern kra_peer_t *kranal_find_peer_locked (lnet_nid_t nid);
458 extern void kranal_post_fma (kra_conn_t *conn, kra_tx_t *tx);
459 extern int kranal_del_peer (lnet_nid_t nid);
460 extern void kranal_device_callback (RAP_INT32 devid, RAP_PVOID arg);
461 extern int kranal_thread_start (int(*fn)(void *arg), void *arg);
462 extern int kranal_connd (void *arg);
463 extern int kranal_reaper (void *arg);
464 extern int kranal_scheduler (void *arg);
465 extern void kranal_close_conn_locked (kra_conn_t *conn, int error);
466 extern void kranal_close_conn (kra_conn_t *conn, int error);
467 extern void kranal_terminate_conn_locked (kra_conn_t *conn);
468 extern void kranal_connect (kra_peer_t *peer);
469 extern int kranal_conn_handshake (struct socket *sock, kra_peer_t *peer);
470 extern int kranal_tunables_init(void);
471 extern void kranal_tunables_fini(void);
472 extern void kranal_init_msg(kra_msg_t *msg, int type);