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