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>
53 #define DEBUG_SUBSYSTEM S_NAL
55 #include <linux/kp30.h>
56 #include <portals/p30.h>
57 #include <portals/lib-p30.h>
58 #include <portals/nal.h>
62 #define RANAL_MAXDEVS 2 /* max # devices RapidArray supports */
64 #define RANAL_N_CONND 4 /* # connection daemons */
66 #define RANAL_MIN_RECONNECT_INTERVAL 1 /* first failed connection retry (seconds)... */
67 #define RANAL_MAX_RECONNECT_INTERVAL 60 /* ...exponentially increasing to this */
69 #define RANAL_FMA_MAX_PREFIX 232 /* max size of FMA "Prefix" */
70 #define RANAL_FMA_MAX_DATA ((7<<10)-256) /* Max FMA MSG is 7K including prefix */
72 #define RANAL_PEER_HASH_SIZE 101 /* # peer lists */
73 #define RANAL_CONN_HASH_SIZE 101 /* # conn lists */
75 #define RANAL_NTX 64 /* # tx descs */
76 #define RANAL_NTX_NBLK 256 /* # reserved tx descs */
78 #define RANAL_FMA_CQ_SIZE 8192 /* # entries in receive CQ
79 * (overflow is a performance hit) */
81 #define RANAL_RESCHED 100 /* # scheduler loops before reschedule */
83 #define RANAL_MIN_TIMEOUT 5 /* minimum timeout interval (seconds) */
84 #define RANAL_TIMEOUT2KEEPALIVE(t) (((t)+1)/2) /* timeout -> keepalive interval */
86 /* default vals for runtime tunables */
87 #define RANAL_TIMEOUT 30 /* comms timeout (seconds) */
88 #define RANAL_LISTENER_TIMEOUT 5 /* listener timeout (seconds) */
89 #define RANAL_MAX_IMMEDIATE (2<<10) /* immediate payload breakpoint */
93 int kra_timeout; /* comms timeout (seconds) */
94 int kra_listener_timeout; /* max time the listener can block */
95 int kra_backlog; /* listener's backlog */
96 int kra_port; /* listener's TCP/IP port */
97 int kra_max_immediate; /* immediate payload breakpoint */
99 struct ctl_table_header *kra_sysctl; /* sysctl interface */
104 RAP_PVOID rad_handle; /* device handle */
105 RAP_PROTECTION_HANDLE rad_ptag; /* protection tag */
106 RAP_CQ_HANDLE rad_fma_cq; /* FMA (small message) completion queue */
107 RAP_CQ_HANDLE rad_rdma_cq; /* rdma completion queue */
108 int rad_id; /* device id */
109 int rad_idx; /* index in kra_devices */
110 int rad_ready; /* set by device callback */
111 struct list_head rad_connq; /* connections requiring attention */
112 struct list_head rad_zombies; /* connections to free */
113 wait_queue_head_t rad_waitq; /* scheduler waits here */
114 spinlock_t rad_lock; /* serialise */
115 void *rad_scheduler; /* scheduling thread */
120 int kra_init; /* initialisation state */
121 int kra_shutdown; /* shut down? */
122 atomic_t kra_nthreads; /* # live threads */
124 struct semaphore kra_nid_mutex; /* serialise NID/listener ops */
125 struct semaphore kra_listener_signal; /* block for listener startup/shutdown */
126 struct socket *kra_listener_sock; /* listener's socket */
127 int kra_listener_shutdown; /* ask listener to close */
129 kra_device_t kra_devices[RANAL_MAXDEVS]; /* device/ptag/cq etc */
130 int kra_ndevs; /* # devices */
132 rwlock_t kra_global_lock; /* stabilize peer/conn ops */
134 struct list_head *kra_peers; /* hash table of all my known peers */
135 int kra_peer_hash_size; /* size of kra_peers */
136 atomic_t kra_npeers; /* # peers extant */
138 struct list_head *kra_conns; /* conns hashed by cqid */
139 int kra_conn_hash_size; /* size of kra_conns */
140 __u64 kra_peerstamp; /* when I started up */
141 __u64 kra_connstamp; /* conn stamp generator */
142 int kra_next_cqid; /* cqid generator */
143 atomic_t kra_nconns; /* # connections extant */
145 long kra_new_min_timeout; /* minimum timeout on any new conn */
146 wait_queue_head_t kra_reaper_waitq; /* reaper sleeps here */
147 spinlock_t kra_reaper_lock; /* serialise */
149 struct list_head kra_connd_peers; /* peers waiting for a connection */
150 wait_queue_head_t kra_connd_waitq; /* connection daemons sleep here */
151 spinlock_t kra_connd_lock; /* serialise */
153 struct list_head kra_idle_txs; /* idle tx descriptors */
154 struct list_head kra_idle_nblk_txs; /* idle reserved tx descriptors */
155 __u64 kra_next_tx_cookie; /* RDMA completion cookie */
156 wait_queue_head_t kra_idle_tx_waitq; /* block here for tx descriptor */
157 spinlock_t kra_tx_lock; /* serialise */
160 #define RANAL_INIT_NOTHING 0
161 #define RANAL_INIT_DATA 1
162 #define RANAL_INIT_LIB 2
163 #define RANAL_INIT_ALL 3
165 /************************************************************************
166 * Wire message structs. These are sent in sender's byte order
167 * (i.e. receiver checks magic and flips if required).
170 typedef struct kra_connreq /* connection request/response */
171 { /* (sent via socket) */
172 __u32 racr_magic; /* I'm an ranal connreq */
173 __u16 racr_version; /* this is my version number */
174 __u16 racr_devid; /* sender's device ID */
175 __u64 racr_nid; /* sender's NID */
176 __u64 racr_peerstamp; /* sender's instance stamp */
177 __u64 racr_connstamp; /* sender's connection stamp */
178 __u32 racr_timeout; /* sender's timeout */
179 RAP_RI_PARAMETERS racr_riparams; /* sender's endpoint info */
184 RAP_MEM_KEY rard_key;
185 RAP_PVOID64 rard_addr;
191 ptl_hdr_t raim_hdr; /* portals header */
192 /* Portals payload is in FMA "Message Data" */
193 } kra_immediate_msg_t;
197 ptl_hdr_t raprm_hdr; /* portals header */
198 __u64 raprm_cookie; /* opaque completion cookie */
203 __u64 rapam_src_cookie; /* reflected completion cookie */
204 __u64 rapam_dst_cookie; /* opaque completion cookie */
205 kra_rdma_desc_t rapam_desc; /* sender's sink buffer */
210 ptl_hdr_t ragm_hdr; /* portals header */
211 __u64 ragm_cookie; /* opaque completion cookie */
212 kra_rdma_desc_t ragm_desc; /* sender's sink buffer */
217 __u64 racm_cookie; /* reflected completion cookie */
218 } kra_completion_msg_t;
220 typedef struct /* NB must fit in FMA "Prefix" */
222 __u32 ram_magic; /* I'm an ranal message */
223 __u16 ram_version; /* this is my version number */
224 __u16 ram_type; /* msg type */
225 __u64 ram_srcnid; /* sender's NID */
226 __u64 ram_connstamp; /* sender's connection stamp */
228 kra_immediate_msg_t immediate;
229 kra_putreq_msg_t putreq;
230 kra_putack_msg_t putack;
232 kra_completion_msg_t completion;
234 __u32 ram_seq; /* incrementing sequence number */
237 #define RANAL_MSG_MAGIC 0x0be91b92 /* unique magic */
238 #define RANAL_MSG_VERSION 1 /* current protocol version */
240 #define RANAL_MSG_FENCE 0x80 /* fence RDMA */
242 #define RANAL_MSG_NONE 0x00 /* illegal message */
243 #define RANAL_MSG_NOOP 0x01 /* empty ram_u (keepalive) */
244 #define RANAL_MSG_IMMEDIATE 0x02 /* ram_u.immediate */
245 #define RANAL_MSG_PUT_REQ 0x03 /* ram_u.putreq (src->sink) */
246 #define RANAL_MSG_PUT_NAK 0x04 /* ram_u.completion (no PUT match: sink->src) */
247 #define RANAL_MSG_PUT_ACK 0x05 /* ram_u.putack (PUT matched: sink->src) */
248 #define RANAL_MSG_PUT_DONE 0x86 /* ram_u.completion (src->sink) */
249 #define RANAL_MSG_GET_REQ 0x07 /* ram_u.get (sink->src) */
250 #define RANAL_MSG_GET_NAK 0x08 /* ram_u.completion (no GET match: src->sink) */
251 #define RANAL_MSG_GET_DONE 0x89 /* ram_u.completion (src->sink) */
252 #define RANAL_MSG_CLOSE 0x8a /* empty ram_u */
254 /***********************************************************************/
256 typedef struct kra_tx /* message descriptor */
258 struct list_head tx_list; /* queue on idle_txs/rac_sendq/rac_waitq */
259 struct kra_conn *tx_conn; /* owning conn */
260 lib_msg_t *tx_libmsg[2]; /* lib msgs to finalize on completion */
261 unsigned long tx_qtime; /* when tx started to wait for something */
262 int tx_isnblk; /* I'm reserved for non-blocking sends */
263 int tx_nob; /* # bytes of payload */
264 int tx_buftype; /* payload buffer type */
265 void *tx_buffer; /* source/sink buffer */
266 int tx_phys_offset; /* first page offset (if phys) */
267 int tx_phys_npages; /* # physical pages */
268 RAP_PHYS_REGION *tx_phys; /* page descriptors */
269 RAP_MEM_KEY tx_map_key; /* mapping key */
270 RAP_RDMA_DESCRIPTOR tx_rdma_desc; /* rdma descriptor */
271 __u64 tx_cookie; /* identify this tx to peer */
272 kra_msg_t tx_msg; /* FMA message buffer */
275 #define RANAL_BUF_NONE 0 /* buffer type not set */
276 #define RANAL_BUF_IMMEDIATE 1 /* immediate data */
277 #define RANAL_BUF_PHYS_UNMAPPED 2 /* physical: not mapped yet */
278 #define RANAL_BUF_PHYS_MAPPED 3 /* physical: mapped already */
279 #define RANAL_BUF_VIRT_UNMAPPED 4 /* virtual: not mapped yet */
280 #define RANAL_BUF_VIRT_MAPPED 5 /* virtual: mapped already */
282 #define RANAL_TX_IDLE 0x00 /* on freelist */
283 #define RANAL_TX_SIMPLE 0x10 /* about to send a simple message */
284 #define RANAL_TX_PUTI_REQ 0x20 /* PUT initiator about to send PUT_REQ */
285 #define RANAL_TX_PUTI_WAIT_ACK 0x21 /* PUT initiator waiting for PUT_ACK */
286 #define RANAL_TX_PUTI_RDMA 0x22 /* PUT initiator waiting for RDMA to complete */
287 #define RANAL_TX_PUTI_DONE 0x23 /* PUT initiator about to send PUT_DONE */
288 #define RANAL_TX_PUTT_NAK 0x30 /* PUT target about to send PUT_NAK */
289 #define RANAL_TX_PUTT_ACK 0x30 /* PUT target about to send PUT_ACK */
290 #define RANAL_TX_PUTT_WAIT_DONE 0x31 /* PUT target waiting for PUT_DONE */
291 #define RANAL_TX_GETI_REQ 0x40 /* GET initiator about to send GET_REQ */
292 #define RANAL_TX_GETI_WAIT_DONE 0x41 /* GET initiator waiting for GET_DONE */
293 #define RANAL_TX_GETT_NAK 0x50 /* GET target about to send PUT_NAK */
294 #define RANAL_TX_GETT_RDMA 0x51 /* GET target waiting for RDMA to complete */
295 #define RANAL_TX_GETT_DONE 0x52 /* GET target about to send GET_DONE */
297 typedef struct kra_conn
299 struct kra_peer *rac_peer; /* owning peer */
300 struct list_head rac_list; /* stash on peer's conn list */
301 struct list_head rac_hashlist; /* stash in connection hash table */
302 struct list_head rac_schedlist; /* schedule (on rad_connq) for attention */
303 struct list_head rac_fmaq; /* txs queued for FMA */
304 struct list_head rac_rdmaq; /* txs awaiting RDMA completion */
305 struct list_head 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 */
310 unsigned long rac_last_rx; /* when I last received an FMA messages */
311 long rac_keepalive; /* keepalive interval */
312 long rac_timeout; /* infer peer death on (last_rx + timout > now) */
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 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 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 */
328 #define RANAL_CONN_ESTABLISHED 0
329 #define RANAL_CONN_CLOSING 1
330 #define RANAL_CONN_CLOSED 2
332 typedef struct kra_peer
334 struct list_head rap_list; /* stash on global peer list */
335 struct list_head rap_connd_list; /* schedule on kra_connd_peers */
336 struct list_head rap_conns; /* all active connections */
337 struct list_head rap_tx_queue; /* msgs waiting for a conn */
338 ptl_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 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_TIME when reconnect OK */
345 unsigned long rap_reconnect_interval; /* exponential backoff */
348 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
349 # define sk_allocation allocation
350 # define sk_data_ready data_ready
351 # define sk_write_space write_space
352 # define sk_user_data user_data
353 # define sk_prot prot
354 # define sk_sndbuf sndbuf
355 # define sk_socket socket
356 # define sk_wmem_queued wmem_queued
358 # define sk_sleep sleep
361 extern lib_nal_t kranal_lib;
362 extern kra_data_t kranal_data;
363 extern kra_tunables_t kranal_tunables;
365 extern void kranal_destroy_peer(kra_peer_t *peer);
366 extern void kranal_destroy_conn(kra_conn_t *conn);
369 kranal_peer_addref(kra_peer_t *peer)
371 CDEBUG(D_NET, "%p->"LPX64"\n", peer, peer->rap_nid);
372 LASSERT(atomic_read(&peer->rap_refcount) > 0);
373 atomic_inc(&peer->rap_refcount);
377 kranal_peer_decref(kra_peer_t *peer)
379 CDEBUG(D_NET, "%p->"LPX64"\n", peer, peer->rap_nid);
380 LASSERT(atomic_read(&peer->rap_refcount) > 0);
381 if (atomic_dec_and_test(&peer->rap_refcount))
382 kranal_destroy_peer(peer);
385 static inline struct list_head *
386 kranal_nid2peerlist (ptl_nid_t nid)
388 unsigned int hash = ((unsigned int)nid) % kranal_data.kra_peer_hash_size;
390 return (&kranal_data.kra_peers[hash]);
394 kranal_peer_active(kra_peer_t *peer)
396 /* Am I in the peer hash table? */
397 return (!list_empty(&peer->rap_list));
401 kranal_conn_addref(kra_conn_t *conn)
403 CDEBUG(D_NET, "%p->"LPX64"\n", conn, conn->rac_peer->rap_nid);
404 LASSERT(atomic_read(&conn->rac_refcount) > 0);
405 atomic_inc(&conn->rac_refcount);
409 kranal_conn_decref(kra_conn_t *conn)
411 CDEBUG(D_NET, "%p->"LPX64"\n", conn, conn->rac_peer->rap_nid);
412 LASSERT(atomic_read(&conn->rac_refcount) > 0);
413 if (atomic_dec_and_test(&conn->rac_refcount))
414 kranal_destroy_conn(conn);
417 static inline struct list_head *
418 kranal_cqid2connlist (__u32 cqid)
420 unsigned int hash = cqid % kranal_data.kra_conn_hash_size;
422 return (&kranal_data.kra_conns [hash]);
425 static inline kra_conn_t *
426 kranal_cqid2conn_locked (__u32 cqid)
428 struct list_head *conns = kranal_cqid2connlist(cqid);
429 struct list_head *tmp;
432 list_for_each(tmp, conns) {
433 conn = list_entry(tmp, kra_conn_t, rac_hashlist);
435 if (conn->rac_cqid == cqid)
443 kranal_tx_mapped (kra_tx_t *tx)
445 return (tx->tx_buftype == RANAL_BUF_VIRT_MAPPED ||
446 tx->tx_buftype == RANAL_BUF_PHYS_MAPPED);
451 kranal_page2phys (struct page *p)
453 __u64 page_number = p - mem_map;
455 return (page_number << PAGE_SHIFT);
458 # error "no page->phys"
461 extern int kranal_listener_procint(ctl_table *table,
462 int write, struct file *filp,
463 void *buffer, size_t *lenp);
464 extern void kranal_update_reaper_timeout(long timeout);
465 extern void kranal_tx_done (kra_tx_t *tx, int completion);
466 extern void kranal_unlink_peer_locked (kra_peer_t *peer);
467 extern void kranal_schedule_conn(kra_conn_t *conn);
468 extern kra_peer_t *kranal_create_peer (ptl_nid_t nid);
469 extern kra_peer_t *kranal_find_peer_locked (ptl_nid_t nid);
470 extern void kranal_post_fma (kra_conn_t *conn, kra_tx_t *tx);
471 extern int kranal_del_peer (ptl_nid_t nid, int single_share);
472 extern void kranal_device_callback(RAP_INT32 devid);
473 extern int kranal_thread_start (int(*fn)(void *arg), void *arg);
474 extern int kranal_connd (void *arg);
475 extern int kranal_reaper (void *arg);
476 extern int kranal_scheduler (void *arg);
477 extern void kranal_close_conn_locked (kra_conn_t *conn, int error);
478 extern void kranal_terminate_conn_locked (kra_conn_t *conn);
479 extern void kranal_connect (kra_peer_t *peer);