4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/klnds/ralnd/ralnd.h
38 * Author: Eric Barton <eric@bartonsoftware.com>
41 #include <linux/module.h>
42 #include <linux/kernel.h>
44 #include <linux/string.h>
45 #include <linux/stat.h>
46 #include <linux/errno.h>
47 #include <linux/unistd.h>
48 #include <linux/uio.h>
50 #include <asm/uaccess.h>
53 #include <linux/init.h>
55 #include <linux/file.h>
56 #include <linux/stat.h>
57 #include <linux/list.h>
58 #include <linux/kmod.h>
59 #include <linux/sysctl.h>
64 #define DEBUG_SUBSYSTEM S_LND
66 #include <libcfs/libcfs.h>
67 #include <lnet/lnet.h>
68 #include <lnet/lib-lnet.h>
69 #include <lnet/lnet-sysctl.h>
73 /* tunables determined at compile time */
74 #define RANAL_RESCHED 100 /* # scheduler loops before reschedule */
76 #define RANAL_PEER_HASH_SIZE 101 /* # peer lists */
77 #define RANAL_CONN_HASH_SIZE 101 /* # conn lists */
79 #define RANAL_MIN_TIMEOUT 5 /* minimum timeout interval (seconds) */
80 #define RANAL_TIMEOUT2KEEPALIVE(t) (((t)+1)/2) /* timeout -> keepalive interval */
83 #define RANAL_MAXDEVS 2 /* max # devices RapidArray supports */
84 #define RANAL_FMA_MAX_PREFIX 232 /* max bytes in FMA "Prefix" we can use */
85 #define RANAL_FMA_MAX_DATA ((7<<10)-256) /* Max FMA MSG is 7K including prefix */
90 int *kra_n_connd; /* # connection daemons */
91 int *kra_min_reconnect_interval; /* first failed connection retry... */
92 int *kra_max_reconnect_interval; /* ...exponentially increasing to this */
93 int *kra_ntx; /* # tx descs */
94 int *kra_credits; /* # concurrent sends */
95 int *kra_peercredits; /* # concurrent sends to 1 peer */
96 int *kra_fma_cq_size; /* # entries in receive CQ */
97 int *kra_timeout; /* comms timeout (seconds) */
98 int *kra_max_immediate; /* immediate payload breakpoint */
100 #if CONFIG_SYSCTL && !CFS_SYSFS_MODULE_PARM
101 cfs_sysctl_table_header_t *kra_sysctl; /* sysctl interface */
107 RAP_PVOID rad_handle; /* device handle */
108 RAP_PVOID rad_fma_cqh; /* FMA completion queue handle */
109 RAP_PVOID rad_rdma_cqh; /* rdma completion queue handle */
110 int rad_id; /* device id */
111 int rad_idx; /* index in kra_devices */
112 int rad_ready; /* set by device callback */
113 cfs_list_t rad_ready_conns;/* connections ready to tx/rx */
114 cfs_list_t rad_new_conns; /* new connections to complete */
115 cfs_waitq_t rad_waitq; /* scheduler waits here */
116 spinlock_t rad_lock; /* serialise */
117 void *rad_scheduler; /* scheduling thread */
118 unsigned int rad_nphysmap; /* # phys mappings */
119 unsigned int rad_nppphysmap;/* # phys pages mapped */
120 unsigned int rad_nvirtmap; /* # virt mappings */
121 unsigned long rad_nobvirtmap;/* # virt bytes mapped */
126 int kra_init; /* initialisation state */
127 int kra_shutdown; /* shut down? */
128 cfs_atomic_t kra_nthreads; /* # live threads */
129 lnet_ni_t *kra_ni; /* _the_ nal instance */
131 kra_device_t kra_devices[RANAL_MAXDEVS]; /* device/ptag/cq */
132 int kra_ndevs; /* # devices */
134 rwlock_t kra_global_lock; /* stabilize peer/conn ops */
136 cfs_list_t *kra_peers; /* hash table of all my known peers */
137 int kra_peer_hash_size; /* size of kra_peers */
138 cfs_atomic_t kra_npeers; /* # peers extant */
139 int kra_nonewpeers; /* prevent new peers */
141 cfs_list_t *kra_conns; /* conns hashed by cqid */
142 int kra_conn_hash_size; /* size of kra_conns */
143 __u64 kra_peerstamp; /* when I started up */
144 __u64 kra_connstamp; /* conn stamp generator */
145 int kra_next_cqid; /* cqid generator */
146 cfs_atomic_t kra_nconns; /* # connections extant */
148 long kra_new_min_timeout; /* minimum timeout on any new conn */
149 cfs_waitq_t kra_reaper_waitq; /* reaper sleeps here */
150 spinlock_t kra_reaper_lock; /* serialise */
152 cfs_list_t kra_connd_peers; /* peers waiting for a connection */
153 cfs_list_t kra_connd_acceptq; /* accepted sockets to handshake */
154 cfs_waitq_t kra_connd_waitq; /* connection daemons sleep here */
155 spinlock_t kra_connd_lock; /* serialise */
157 cfs_list_t kra_idle_txs; /* idle tx descriptors */
158 __u64 kra_next_tx_cookie; /* RDMA completion cookie */
159 spinlock_t kra_tx_lock; /* serialise */
162 #define RANAL_INIT_NOTHING 0
163 #define RANAL_INIT_DATA 1
164 #define RANAL_INIT_ALL 2
166 typedef struct kra_acceptsock /* accepted socket queued for connd */
168 cfs_list_t ras_list; /* queue for attention */
169 struct socket *ras_sock; /* the accepted socket */
172 /************************************************************************
173 * Wire message structs. These are sent in sender's byte order
174 * (i.e. receiver checks magic and flips if required).
177 typedef struct kra_connreq /* connection request/response */
178 { /* (sent via socket) */
179 __u32 racr_magic; /* I'm an ranal connreq */
180 __u16 racr_version; /* this is my version number */
181 __u16 racr_devid; /* sender's device ID */
182 __u64 racr_srcnid; /* sender's NID */
183 __u64 racr_dstnid; /* who sender expects to listen */
184 __u64 racr_peerstamp; /* sender's instance stamp */
185 __u64 racr_connstamp; /* sender's connection stamp */
186 __u32 racr_timeout; /* sender's timeout */
187 RAP_RI_PARAMETERS racr_riparams; /* sender's endpoint info */
192 RAP_MEM_KEY rard_key;
193 RAP_PVOID64 rard_addr;
199 lnet_hdr_t raim_hdr; /* portals header */
200 /* Portals payload is in FMA "Message Data" */
201 } kra_immediate_msg_t;
205 lnet_hdr_t raprm_hdr; /* portals header */
206 __u64 raprm_cookie; /* opaque completion cookie */
211 __u64 rapam_src_cookie; /* reflected completion cookie */
212 __u64 rapam_dst_cookie; /* opaque completion cookie */
213 kra_rdma_desc_t rapam_desc; /* sender's sink buffer */
218 lnet_hdr_t ragm_hdr; /* portals header */
219 __u64 ragm_cookie; /* opaque completion cookie */
220 kra_rdma_desc_t ragm_desc; /* sender's sink buffer */
225 __u64 racm_cookie; /* reflected completion cookie */
226 } kra_completion_msg_t;
228 typedef struct /* NB must fit in FMA "Prefix" */
230 __u32 ram_magic; /* I'm an ranal message */
231 __u16 ram_version; /* this is my version number */
232 __u16 ram_type; /* msg type */
233 __u64 ram_srcnid; /* sender's NID */
234 __u64 ram_connstamp; /* sender's connection stamp */
236 kra_immediate_msg_t immediate;
237 kra_putreq_msg_t putreq;
238 kra_putack_msg_t putack;
240 kra_completion_msg_t completion;
242 __u32 ram_seq; /* incrementing sequence number */
245 #define RANAL_MSG_MAGIC LNET_PROTO_RA_MAGIC /* unique magic */
246 #define RANAL_MSG_VERSION 1 /* current protocol version */
248 #define RANAL_MSG_FENCE 0x80 /* fence RDMA */
250 #define RANAL_MSG_NONE 0x00 /* illegal message */
251 #define RANAL_MSG_NOOP 0x01 /* empty ram_u (keepalive) */
252 #define RANAL_MSG_IMMEDIATE 0x02 /* ram_u.immediate */
253 #define RANAL_MSG_PUT_REQ 0x03 /* ram_u.putreq (src->sink) */
254 #define RANAL_MSG_PUT_NAK 0x04 /* ram_u.completion (no PUT match: sink->src) */
255 #define RANAL_MSG_PUT_ACK 0x05 /* ram_u.putack (PUT matched: sink->src) */
256 #define RANAL_MSG_PUT_DONE 0x86 /* ram_u.completion (src->sink) */
257 #define RANAL_MSG_GET_REQ 0x07 /* ram_u.get (sink->src) */
258 #define RANAL_MSG_GET_NAK 0x08 /* ram_u.completion (no GET match: src->sink) */
259 #define RANAL_MSG_GET_DONE 0x89 /* ram_u.completion (src->sink) */
260 #define RANAL_MSG_CLOSE 0x8a /* empty ram_u */
262 /***********************************************************************/
264 typedef struct kra_tx /* message descriptor */
266 cfs_list_t tx_list; /* queue on idle_txs/rac_sendq/rac_waitq */
267 struct kra_conn *tx_conn; /* owning conn */
268 lnet_msg_t *tx_lntmsg[2]; /* ptl msgs to finalize on completion */
269 unsigned long tx_qtime; /* when tx started to wait for something (jiffies) */
270 int tx_nob; /* # bytes of payload */
271 int tx_buftype; /* payload buffer type */
272 void *tx_buffer; /* source/sink buffer */
273 int tx_phys_offset; /* first page offset (if phys) */
274 int tx_phys_npages; /* # physical pages */
275 RAP_PHYS_REGION *tx_phys; /* page descriptors */
276 RAP_MEM_KEY tx_map_key; /* mapping key */
277 RAP_RDMA_DESCRIPTOR tx_rdma_desc; /* rdma descriptor */
278 __u64 tx_cookie; /* identify this tx to peer */
279 kra_msg_t tx_msg; /* FMA message buffer */
282 #define RANAL_BUF_NONE 0 /* buffer type not set */
283 #define RANAL_BUF_IMMEDIATE 1 /* immediate data */
284 #define RANAL_BUF_PHYS_UNMAPPED 2 /* physical: not mapped yet */
285 #define RANAL_BUF_PHYS_MAPPED 3 /* physical: mapped already */
286 #define RANAL_BUF_VIRT_UNMAPPED 4 /* virtual: not mapped yet */
287 #define RANAL_BUF_VIRT_MAPPED 5 /* virtual: mapped already */
289 typedef struct kra_conn
291 struct kra_peer *rac_peer; /* owning peer */
292 cfs_list_t rac_list; /* stash on peer's conn list */
293 cfs_list_t rac_hashlist; /* stash in connection hash table */
294 cfs_list_t rac_schedlist; /* schedule (on rad_???_conns) for attention */
295 cfs_list_t rac_fmaq; /* txs queued for FMA */
296 cfs_list_t rac_rdmaq; /* txs awaiting RDMA completion */
297 cfs_list_t rac_replyq; /* txs awaiting replies */
298 __u64 rac_peerstamp; /* peer's unique stamp */
299 __u64 rac_peer_connstamp;/* peer's unique connection stamp */
300 __u64 rac_my_connstamp; /* my unique connection stamp */
301 unsigned long rac_last_tx; /* when I last sent an FMA message (jiffies) */
302 unsigned long rac_last_rx; /* when I last received an FMA messages (jiffies) */
303 long rac_keepalive; /* keepalive interval (seconds) */
304 long rac_timeout; /* infer peer death if no rx for this many seconds */
305 __u32 rac_cqid; /* my completion callback id (non-unique) */
306 __u32 rac_tx_seq; /* tx msg sequence number */
307 __u32 rac_rx_seq; /* rx msg sequence number */
308 cfs_atomic_t rac_refcount; /* # users */
309 unsigned int rac_close_sent; /* I've sent CLOSE */
310 unsigned int rac_close_recvd; /* I've received CLOSE */
311 unsigned int rac_state; /* connection state */
312 unsigned int rac_scheduled; /* being attented to */
313 spinlock_t rac_lock; /* serialise */
314 kra_device_t *rac_device; /* which device */
315 RAP_PVOID rac_rihandle; /* RA endpoint */
316 kra_msg_t *rac_rxmsg; /* incoming message (FMA prefix) */
317 kra_msg_t rac_msg; /* keepalive/CLOSE message buffer */
320 #define RANAL_CONN_ESTABLISHED 0
321 #define RANAL_CONN_CLOSING 1
322 #define RANAL_CONN_CLOSED 2
324 typedef struct kra_peer {
325 cfs_list_t rap_list; /* stash on global peer list */
326 cfs_list_t rap_connd_list; /* schedule on kra_connd_peers */
327 cfs_list_t rap_conns; /* all active connections */
328 cfs_list_t rap_tx_queue; /* msgs waiting for a conn */
329 lnet_nid_t rap_nid; /* who's on the other end(s) */
330 __u32 rap_ip; /* IP address of peer */
331 int rap_port; /* port on which peer listens */
332 cfs_atomic_t rap_refcount; /* # users */
333 int rap_persistence; /* "known" peer refs */
334 int rap_connecting; /* connection forming */
335 unsigned long rap_reconnect_time; /* get_seconds() when reconnect OK */
336 unsigned long rap_reconnect_interval; /* exponential backoff */
339 extern kra_data_t kranal_data;
340 extern kra_tunables_t kranal_tunables;
342 extern void kranal_destroy_peer(kra_peer_t *peer);
343 extern void kranal_destroy_conn(kra_conn_t *conn);
346 kranal_peer_addref(kra_peer_t *peer)
348 CDEBUG(D_NET, "%p->%s\n", peer, libcfs_nid2str(peer->rap_nid));
349 LASSERT(cfs_atomic_read(&peer->rap_refcount) > 0);
350 cfs_atomic_inc(&peer->rap_refcount);
354 kranal_peer_decref(kra_peer_t *peer)
356 CDEBUG(D_NET, "%p->%s\n", peer, libcfs_nid2str(peer->rap_nid));
357 LASSERT(cfs_atomic_read(&peer->rap_refcount) > 0);
358 if (cfs_atomic_dec_and_test(&peer->rap_refcount))
359 kranal_destroy_peer(peer);
362 static inline cfs_list_t *
363 kranal_nid2peerlist (lnet_nid_t nid)
365 unsigned int hash = ((unsigned int)nid) % kranal_data.kra_peer_hash_size;
367 return (&kranal_data.kra_peers[hash]);
371 kranal_peer_active(kra_peer_t *peer)
373 /* Am I in the peer hash table? */
374 return (!cfs_list_empty(&peer->rap_list));
378 kranal_conn_addref(kra_conn_t *conn)
380 CDEBUG(D_NET, "%p->%s\n", conn,
381 libcfs_nid2str(conn->rac_peer->rap_nid));
382 LASSERT(cfs_atomic_read(&conn->rac_refcount) > 0);
383 cfs_atomic_inc(&conn->rac_refcount);
387 kranal_conn_decref(kra_conn_t *conn)
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 if (cfs_atomic_dec_and_test(&conn->rac_refcount))
393 kranal_destroy_conn(conn);
396 static inline cfs_list_t *
397 kranal_cqid2connlist (__u32 cqid)
399 unsigned int hash = cqid % kranal_data.kra_conn_hash_size;
401 return (&kranal_data.kra_conns [hash]);
404 static inline kra_conn_t *
405 kranal_cqid2conn_locked (__u32 cqid)
407 cfs_list_t *conns = kranal_cqid2connlist(cqid);
411 cfs_list_for_each(tmp, conns) {
412 conn = cfs_list_entry(tmp, kra_conn_t, rac_hashlist);
414 if (conn->rac_cqid == cqid)
422 kranal_tx_mapped (kra_tx_t *tx)
424 return (tx->tx_buftype == RANAL_BUF_VIRT_MAPPED ||
425 tx->tx_buftype == RANAL_BUF_PHYS_MAPPED);
428 int kranal_startup (lnet_ni_t *ni);
429 void kranal_shutdown (lnet_ni_t *ni);
430 int kranal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
431 int kranal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
432 int kranal_eager_recv(lnet_ni_t *ni, void *private,
433 lnet_msg_t *lntmsg, void **new_private);
434 int kranal_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg,
435 int delayed, unsigned int niov,
436 struct iovec *iov, lnet_kiov_t *kiov,
437 unsigned int offset, unsigned int mlen, unsigned int rlen);
438 int kranal_accept(lnet_ni_t *ni, struct socket *sock);
440 extern void kranal_free_acceptsock (kra_acceptsock_t *ras);
441 extern int kranal_listener_procint (cfs_sysctl_table_t *table,
442 int write, struct file *filp,
443 void *buffer, size_t *lenp);
444 extern void kranal_update_reaper_timeout (long timeout);
445 extern void kranal_tx_done (kra_tx_t *tx, int completion);
446 extern void kranal_unlink_peer_locked (kra_peer_t *peer);
447 extern void kranal_schedule_conn (kra_conn_t *conn);
448 extern int kranal_create_peer (kra_peer_t **peerp, lnet_nid_t nid);
449 extern int kranal_add_persistent_peer (lnet_nid_t nid, __u32 ip, int port);
450 extern kra_peer_t *kranal_find_peer_locked (lnet_nid_t nid);
451 extern void kranal_post_fma (kra_conn_t *conn, kra_tx_t *tx);
452 extern int kranal_del_peer (lnet_nid_t nid);
453 extern void kranal_device_callback (RAP_INT32 devid, RAP_PVOID arg);
454 extern int kranal_thread_start(int(*fn)(void *arg), void *arg, char *name);
455 extern int kranal_connd (void *arg);
456 extern int kranal_reaper (void *arg);
457 extern int kranal_scheduler (void *arg);
458 extern void kranal_close_conn_locked (kra_conn_t *conn, int error);
459 extern void kranal_close_conn (kra_conn_t *conn, int error);
460 extern void kranal_terminate_conn_locked (kra_conn_t *conn);
461 extern void kranal_connect (kra_peer_t *peer);
462 extern int kranal_conn_handshake (struct socket *sock, kra_peer_t *peer);
463 extern int kranal_tunables_init(void);
464 extern void kranal_tunables_fini(void);
465 extern void kranal_init_msg(kra_msg_t *msg, int type);