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