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