1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
32 * Copyright (C) 2006 Myricom, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lnet/klnds/mxlnd/mxlnd.h
40 * Author: Eric Barton <eric@bartonsoftware.com>
41 * Author: Scott Atchley <atchley at myri.com>
47 #ifndef AUTOCONF_INCLUDED
48 #include <linux/config.h>
50 #include <linux/module.h> /* module */
51 #include <linux/kernel.h> /* module */
53 #include <linux/string.h>
54 #include <linux/stat.h>
55 #include <linux/errno.h>
56 #include <linux/smp_lock.h>
57 #include <linux/unistd.h>
58 #include <linux/uio.h>
61 #include <asm/system.h>
62 #include <asm/uaccess.h>
65 #include <linux/init.h> /* module */
67 #include <linux/file.h>
68 #include <linux/stat.h>
69 #include <linux/list.h>
70 #include <linux/kmod.h>
71 #include <linux/sysctl.h>
72 #include <linux/random.h>
73 #include <linux/utsname.h>
78 #include <linux/netdevice.h> /* these are needed for ARP */
79 #include <linux/if_arp.h>
81 #include <linux/inetdevice.h>
83 #define DEBUG_SUBSYSTEM S_LND
85 #include "libcfs/libcfs.h"
86 #include "lnet/lnet.h"
87 #include "lnet/lib-lnet.h"
88 #include <lnet/lnet-sysctl.h>
91 #include "mx_extensions.h"
92 #include "myriexpress.h"
94 #if LNET_MAX_IOV > MX_MAX_SEGMENTS
95 #error LNET_MAX_IOV is greater then MX_MAX_SEGMENTS
98 /* Using MX's 64 match bits
99 * We are using the match bits to specify message type and the cookie. The
100 * highest four bits (60-63) are reserved for message type. Below we specify
101 * the types. MXLND_MASK_ICON_REQ and MXLND_MASK_ICON_ACK are used for
102 * mx_iconnect(). We reserve the remaining combinations for future use. The
103 * next 8 bits (52-59) are reserved for returning a status code for failed
104 * GET_DATA (payload) messages. The last 52 bits are used for cookies. That
105 * should allow unique cookies for 4 KB messages at 10 Gbps line rate without
106 * rollover for about 8 years. That should be enough. */
109 #define MXLND_MASK_ICON_REQ (0xBLL << 60) /* it is a mx_iconnect() completion */
110 #define MXLND_MASK_CONN_REQ (0xCLL << 60) /* CONN_REQ msg */
111 #define MXLND_MASK_ICON_ACK (0x9LL << 60) /* it is a mx_iconnect() completion */
112 #define MXLND_MASK_CONN_ACK (0xALL << 60) /* CONN_ACK msg*/
113 #define MXLND_MASK_EAGER (0xELL << 60) /* EAGER msg */
114 #define MXLND_MASK_NOOP (0x1LL << 60) /* NOOP msg */
115 #define MXLND_MASK_PUT_REQ (0x2LL << 60) /* PUT_REQ msg */
116 #define MXLND_MASK_PUT_ACK (0x3LL << 60) /* PUT_ACK msg */
117 #define MXLND_MASK_PUT_DATA (0x4LL << 60) /* PUT_DATA msg */
118 #define MXLND_MASK_GET_REQ (0x5LL << 60) /* GET_REQ msg */
119 #define MXLND_MASK_GET_DATA (0x6LL << 60) /* GET_DATA msg */
120 //#define MXLND_MASK_NAK (0x7LL << 60) /* NAK msg */
122 #define MXLND_MAX_COOKIE ((1LL << 52) - 1) /* when to roll-over the cookie value */
123 #define MXLND_NCOMPLETIONS (MXLND_N_SCHED + 2) /* max threads for completion array */
125 /* defaults for configurable parameters */
126 #define MXLND_N_SCHED 1 /* # schedulers (mx_wait_any() threads) */
127 #define MXLND_MX_BOARD 0 /* Use the first MX NIC if more than 1 avail */
128 #define MXLND_MX_EP_ID 3 /* MX endpoint ID */
129 #define MXLND_COMM_TIMEOUT (20 * HZ) /* timeout for send/recv (jiffies) */
130 #define MXLND_WAIT_TIMEOUT HZ /* timeout for wait (jiffies) */
131 #define MXLND_POLLING 1000 /* poll iterations before blocking */
132 #define MXLND_MAX_PEERS 1024 /* number of nodes talking to me */
133 #define MXLND_EAGER_NUM MXLND_MAX_PEERS /* number of pre-posted receives */
134 #define MXLND_EAGER_SIZE PAGE_SIZE /* pre-posted eager message size */
135 #define MXLND_MSG_QUEUE_DEPTH 8 /* msg queue depth */
136 #define MXLND_CREDIT_HIGHWATER (MXLND_MSG_QUEUE_DEPTH - 2)
137 /* when to send a noop to return credits */
138 #define MXLND_NTX 256 /* # of kmx_tx - total sends in flight
139 1/2 are reserved for connect messages */
141 #define MXLND_HASH_BITS 6 /* the number of bits to hash over */
142 #define MXLND_HASH_SIZE (1<<MXLND_HASH_BITS)
143 /* number of peer lists for lookup.
144 we hash over the last N bits of
145 the IP address converted to an int. */
146 #define MXLND_HASH_MASK (MXLND_HASH_SIZE - 1)
147 /* ensure we use only the last N bits */
149 /* debugging features */
150 #define MXLND_CKSUM 0 /* checksum kmx_msg_t */
151 #define MXLND_DEBUG 0 /* turn on printk()s */
153 extern inline void mxlnd_noop(char *s, ...);
155 #define MXLND_PRINT printk
157 #define MXLND_PRINT mxlnd_noop
160 /* provide wrappers around LIBCFS_ALLOC/FREE to keep MXLND specific
161 * memory usage stats that include pages */
163 #define MXLND_ALLOC(x, size) \
165 spin_lock(&kmxlnd_data.kmx_global_lock); \
166 kmxlnd_data.kmx_mem_used += size; \
167 spin_unlock(&kmxlnd_data.kmx_global_lock); \
168 LIBCFS_ALLOC(x, size); \
170 spin_lock(&kmxlnd_data.kmx_global_lock); \
171 kmxlnd_data.kmx_mem_used -= size; \
172 spin_unlock(&kmxlnd_data.kmx_global_lock); \
176 #define MXLND_FREE(x, size) \
178 spin_lock(&kmxlnd_data.kmx_global_lock); \
179 kmxlnd_data.kmx_mem_used -= size; \
180 spin_unlock(&kmxlnd_data.kmx_global_lock); \
181 LIBCFS_FREE(x, size); \
185 typedef struct kmx_tunables {
186 int *kmx_n_waitd; /* # completion threads */
187 int *kmx_max_peers; /* max # of potential peers */
188 int *kmx_cksum; /* checksum small msgs? */
189 int *kmx_ntx; /* total # of tx (1/2 for LNET 1/2 for CONN_REQ */
190 int *kmx_credits; /* concurrent sends to 1 peer */
191 int *kmx_board; /* MX board (NIC) number */
192 int *kmx_ep_id; /* MX endpoint number */
193 int *kmx_polling; /* if 0, block. if > 0, poll this many
194 iterations before blocking */
195 char **kmx_hosts; /* Location of hosts file, if used */
198 /* structure to hold IP-to-hostname resolution data */
200 struct kmx_peer *mxh_peer; /* pointer to matching peer */
201 u32 mxh_addr; /* IP address as int */
202 char *mxh_hostname; /* peer's hostname */
203 u32 mxh_board; /* peer's board rank */
204 u32 mxh_ep_id; /* peer's MX endpoint ID */
205 struct list_head mxh_list; /* position on kmx_hosts */
206 spinlock_t mxh_lock; /* lock */
209 /* global interface state */
210 typedef struct kmx_data
212 int kmx_init; /* initialization state */
213 int kmx_shutdown; /* shutting down? */
214 atomic_t kmx_nthreads; /* number of threads */
215 struct completion *kmx_completions; /* array of completion structs */
216 lnet_ni_t *kmx_ni; /* the LND instance */
217 u64 kmx_incarnation; /* my incarnation value - unused */
218 long kmx_mem_used; /* memory used */
219 struct kmx_host *kmx_localhost; /* pointer to my kmx_host info */
220 mx_endpoint_t kmx_endpt; /* the MX endpoint */
222 spinlock_t kmx_global_lock; /* global lock */
224 struct list_head kmx_conn_req; /* list of connection requests */
225 spinlock_t kmx_conn_lock; /* connection list lock */
226 struct semaphore kmx_conn_sem; /* semaphore for connection request list */
228 struct list_head kmx_hosts; /* host lookup info */
229 spinlock_t kmx_hosts_lock; /* hosts list lock */
231 struct list_head kmx_peers[MXLND_HASH_SIZE];
232 /* list of all known peers */
233 rwlock_t kmx_peers_lock; /* peer list rw lock */
234 atomic_t kmx_npeers; /* number of peers */
236 struct list_head kmx_txs; /* all tx descriptors */
237 struct list_head kmx_tx_idle; /* list of idle tx */
238 spinlock_t kmx_tx_idle_lock; /* lock for idle tx list */
239 s32 kmx_tx_used; /* txs in use */
240 u64 kmx_tx_next_cookie; /* unique id for tx */
241 struct list_head kmx_tx_queue; /* generic send queue */
242 spinlock_t kmx_tx_queue_lock; /* lock for generic sends */
243 struct semaphore kmx_tx_queue_sem; /* semaphore for tx queue */
245 struct list_head kmx_rxs; /* all rx descriptors */
246 spinlock_t kmx_rxs_lock; /* lock for rxs list */
247 struct list_head kmx_rx_idle; /* list of idle tx */
248 spinlock_t kmx_rx_idle_lock; /* lock for idle rx list */
251 #define MXLND_INIT_NOTHING 0 /* in the beginning, there was nothing... */
252 #define MXLND_INIT_DATA 1 /* main data structures created */
253 #define MXLND_INIT_TXS 2 /* tx descriptors created */
254 #define MXLND_INIT_RXS 3 /* initial rx descriptors created */
255 #define MXLND_INIT_MX 4 /* initiate MX library, open endpoint, get NIC id */
256 #define MXLND_INIT_THREADS 5 /* waitd, timeoutd, tx_queued threads */
257 #define MXLND_INIT_ALL 6 /* startup completed */
259 #include "mxlnd_wire.h"
266 /* The life cycle of a request */
268 MXLND_CTX_INIT = 0, /* just created */
269 MXLND_CTX_IDLE = 1, /* available for use */
270 MXLND_CTX_PREP = 2, /* getting ready for send/recv */
271 MXLND_CTX_PENDING = 3, /* mx_isend() or mx_irecv() called */
272 MXLND_CTX_COMPLETED = 4, /* cleaning up after completion or timeout */
273 MXLND_CTX_CANCELED = 5, /* timed out but still in ctx list */
276 /* Context Structure - generic tx/rx descriptor
277 * It represents the context (or state) of each send or receive request.
278 * In other LNDs, they have separate TX and RX descriptors and this replaces both.
280 * We will keep the these on the global kmx_rxs and kmx_txs lists for cleanup
281 * during shutdown(). We will move them between the rx/tx idle lists and the
282 * pending list which is monitored by mxlnd_timeoutd().
285 enum kmx_req_type mxc_type; /* TX or RX */
286 u64 mxc_incarnation; /* store the peer's incarnation here
287 to verify before changing flow
288 control credits after completion */
289 unsigned long mxc_deadline; /* request time out in absolute jiffies */
290 enum kmx_req_state mxc_state; /* what is the state of the request? */
291 struct list_head mxc_global_list; /* place on kmx_rxs or kmx_txs */
292 struct list_head mxc_list; /* place on rx/tx idle list, tx q, peer tx */
293 struct list_head mxc_rx_list; /* place on mxp_rx_posted list */
294 spinlock_t mxc_lock; /* lock */
296 lnet_nid_t mxc_nid; /* dst's NID if peer is not known */
297 struct kmx_peer *mxc_peer; /* owning peer */
298 struct kmx_conn *mxc_conn; /* owning conn */
299 struct kmx_msg *mxc_msg; /* msg hdr mapped to mxc_page */
300 struct page *mxc_page; /* buffer for eager msgs */
301 lnet_msg_t *mxc_lntmsg[2]; /* lnet msgs to finalize */
303 u8 mxc_msg_type; /* what type of message is this? */
304 u64 mxc_cookie; /* completion cookie */
305 u64 mxc_match; /* MX match info */
306 mx_ksegment_t mxc_seg; /* local MX ksegment for non-DATA */
307 mx_ksegment_t *mxc_seg_list; /* MX ksegment array for DATA */
308 int mxc_nseg; /* number of segments */
309 unsigned long mxc_pin_type; /* MX_PIN_KERNEL or MX_PIN_PHYSICAL */
310 u32 mxc_nob; /* number of bytes sent/received */
311 mx_request_t mxc_mxreq; /* MX request */
312 mx_status_t mxc_status; /* MX status */
313 s64 mxc_get; /* # of times returned from idle list */
314 s64 mxc_put; /* # of times returned from idle list */
317 #define MXLND_CONN_DISCONNECT -2 /* conn is being destroyed - do not add txs */
318 #define MXLND_CONN_FAIL -1 /* connect failed (bad handshake, unavail, etc.) */
319 #define MXLND_CONN_INIT 0 /* in the beginning, there was nothing... */
320 #define MXLND_CONN_REQ 1 /* a connection request message is needed */
321 #define MXLND_CONN_ACK 2 /* a connection ack is needed */
322 #define MXLND_CONN_WAIT 3 /* waiting for req or ack to complete */
323 #define MXLND_CONN_READY 4 /* ready to send */
325 /* connection state - queues for queued and pending msgs */
328 u64 mxk_incarnation; /* connections's incarnation value */
329 atomic_t mxk_refcount; /* reference counting */
331 struct kmx_peer *mxk_peer; /* owning peer */
332 mx_endpoint_addr_t mxk_epa; /* peer's endpoint address */
334 struct list_head mxk_list; /* for placing on mxp_conns */
335 spinlock_t mxk_lock; /* lock */
336 unsigned long mxk_timeout; /* expiration of oldest pending tx/rx */
337 unsigned long mxk_last_tx; /* when last tx completed with success */
338 unsigned long mxk_last_rx; /* when last rx completed */
340 int mxk_credits; /* # of my credits for sending to peer */
341 int mxk_outstanding; /* # of credits to return */
343 int mxk_status; /* can we send messages? MXLND_CONN_* */
344 struct list_head mxk_tx_credit_queue; /* send queue for peer */
345 struct list_head mxk_tx_free_queue; /* send queue for peer */
346 int mxk_ntx_msgs; /* # of msgs on tx queues */
347 int mxk_ntx_data ; /* # of DATA on tx queues */
348 int mxk_ntx_posted; /* # of tx msgs in flight */
349 int mxk_data_posted; /* # of tx data payloads in flight */
351 struct list_head mxk_pending; /* in flight rxs and txs */
357 lnet_nid_t mxp_nid; /* peer's LNET NID */
358 u64 mxp_incarnation; /* peer's incarnation value */
359 atomic_t mxp_refcount; /* reference counts */
361 struct kmx_host *mxp_host; /* peer lookup info */
362 u64 mxp_nic_id; /* remote's MX nic_id for mx_connect() */
364 struct list_head mxp_peers; /* for placing on kmx_peers */
365 spinlock_t mxp_lock; /* lock */
367 struct list_head mxp_conns; /* list of connections */
368 struct kmx_conn *mxp_conn; /* current connection */
370 unsigned long mxp_reconnect_time; /* when to retry connect */
371 int mxp_incompatible; /* incorrect conn_req values */
374 extern kmx_data_t kmxlnd_data;
375 extern kmx_tunables_t kmxlnd_tunables;
377 /* required for the LNET API */
378 int mxlnd_startup(lnet_ni_t *ni);
379 void mxlnd_shutdown(lnet_ni_t *ni);
380 int mxlnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
381 int mxlnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
382 int mxlnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
383 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
384 unsigned int offset, unsigned int mlen, unsigned int rlen);
387 extern void mxlnd_thread_stop(long id);
388 extern int mxlnd_ctx_alloc(struct kmx_ctx **ctxp, enum kmx_req_type type);
389 extern void mxlnd_ctx_free(struct kmx_ctx *ctx);
390 extern void mxlnd_ctx_init(struct kmx_ctx *ctx);
391 extern lnet_nid_t mxlnd_nic_id2nid(lnet_ni_t *ni, u64 nic_id);
392 extern u64 mxlnd_nid2nic_id(lnet_nid_t nid);
395 void mxlnd_eager_recv(void *context, uint64_t match_value, uint32_t length);
396 extern mx_unexp_handler_action_t mxlnd_unexpected_recv(void *context,
397 mx_endpoint_addr_t source, uint64_t match_value, uint32_t length,
398 void *data_if_available);
399 extern void mxlnd_peer_free(struct kmx_peer *peer);
400 extern void mxlnd_conn_free(struct kmx_conn *conn);
401 extern void mxlnd_sleep(unsigned long timeout);
402 extern int mxlnd_tx_queued(void *arg);
403 extern void mxlnd_handle_rx_completion(struct kmx_ctx *rx);
404 extern int mxlnd_check_sends(struct kmx_peer *peer);
405 extern int mxlnd_tx_peer_queued(void *arg);
406 extern int mxlnd_request_waitd(void *arg);
407 extern int mxlnd_unex_recvd(void *arg);
408 extern int mxlnd_timeoutd(void *arg);
409 extern int mxlnd_connd(void *arg);
411 #define mxlnd_peer_addref(peer) \
413 LASSERT(peer != NULL); \
414 LASSERT(atomic_read(&(peer)->mxp_refcount) > 0); \
415 atomic_inc(&(peer)->mxp_refcount); \
419 #define mxlnd_peer_decref(peer) \
421 LASSERT(atomic_read(&(peer)->mxp_refcount) > 0); \
422 if (atomic_dec_and_test(&(peer)->mxp_refcount)) \
423 mxlnd_peer_free(peer); \
426 #define mxlnd_conn_addref(conn) \
428 LASSERT(conn != NULL); \
429 LASSERT(atomic_read(&(conn)->mxk_refcount) > 0); \
430 atomic_inc(&(conn)->mxk_refcount); \
434 #define mxlnd_conn_decref(conn) \
436 LASSERT(atomic_read(&(conn)->mxk_refcount) > 0); \
437 if (atomic_dec_and_test(&(conn)->mxk_refcount)) \
438 mxlnd_conn_free(conn); \