Whamcloud - gitweb
30b1f8547315b263dd7ec7823ad5f8013e967cbf
[fs/lustre-release.git] / lnet / include / lnet / lib-types.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/include/lnet/lib-types.h
37  *
38  * Types used by the library side routines that do not need to be
39  * exposed to the user application
40  */
41
42 #ifndef __LNET_LIB_TYPES_H__
43 #define __LNET_LIB_TYPES_H__
44
45 #if defined(__linux__)
46 #include <lnet/linux/lib-types.h>
47 #elif defined(__APPLE__)
48 #include <lnet/darwin/lib-types.h>
49 #elif defined(__WINNT__)
50 #include <lnet/winnt/lib-types.h>
51 #include <libcfs/libcfs_pack.h>
52 #include <libcfs/libcfs_unpack.h>
53 #else
54 #error Unsupported Operating System
55 #endif
56
57 #include <libcfs/libcfs.h>
58 #include <libcfs/list.h>
59 #include <lnet/types.h>
60
61 #define WIRE_ATTR       __attribute__((packed))
62
63 /* Packed version of lnet_process_id_t to transfer via network */
64 typedef struct {
65         lnet_nid_t nid;
66         lnet_pid_t pid;   /* node id / process id */
67 } WIRE_ATTR lnet_process_id_packed_t;
68
69 /* The wire handle's interface cookie only matches one network interface in
70  * one epoch (i.e. new cookie when the interface restarts or the node
71  * reboots).  The object cookie only matches one object on that interface
72  * during that object's lifetime (i.e. no cookie re-use). */
73 typedef struct {
74         __u64 wh_interface_cookie;
75         __u64 wh_object_cookie;
76 } WIRE_ATTR lnet_handle_wire_t;
77
78 typedef enum {
79         LNET_MSG_ACK = 0,
80         LNET_MSG_PUT,
81         LNET_MSG_GET,
82         LNET_MSG_REPLY,
83         LNET_MSG_HELLO,
84 } lnet_msg_type_t;
85
86 /* The variant fields of the portals message header are aligned on an 8
87  * byte boundary in the message header.  Note that all types used in these
88  * wire structs MUST be fixed size and the smaller types are placed at the
89  * end. */
90 typedef struct lnet_ack {
91         lnet_handle_wire_t  dst_wmd;
92         __u64               match_bits;
93         __u32               mlength;
94 } WIRE_ATTR lnet_ack_t;
95
96 typedef struct lnet_put {
97         lnet_handle_wire_t  ack_wmd;
98         __u64               match_bits;
99         __u64               hdr_data;
100         __u32               ptl_index;
101         __u32               offset;
102 } WIRE_ATTR lnet_put_t;
103
104 typedef struct lnet_get {
105         lnet_handle_wire_t  return_wmd;
106         __u64               match_bits;
107         __u32               ptl_index;
108         __u32               src_offset;
109         __u32               sink_length;
110 } WIRE_ATTR lnet_get_t;
111
112 typedef struct lnet_reply {
113         lnet_handle_wire_t  dst_wmd;
114 } WIRE_ATTR lnet_reply_t;
115
116 typedef struct lnet_hello {
117         __u64              incarnation;
118         __u32              type;
119 } WIRE_ATTR lnet_hello_t;
120
121 typedef struct {
122         lnet_nid_t          dest_nid;
123         lnet_nid_t          src_nid;
124         lnet_pid_t          dest_pid;
125         lnet_pid_t          src_pid;
126         __u32               type;               /* lnet_msg_type_t */
127         __u32               payload_length;     /* payload data to follow */
128         /*<------__u64 aligned------->*/
129         union {
130                 lnet_ack_t   ack;
131                 lnet_put_t   put;
132                 lnet_get_t   get;
133                 lnet_reply_t reply;
134                 lnet_hello_t hello;
135         } msg;
136 } WIRE_ATTR lnet_hdr_t;
137
138 /* A HELLO message contains a magic number and protocol version
139  * code in the header's dest_nid, the peer's NID in the src_nid, and
140  * LNET_MSG_HELLO in the type field.  All other common fields are zero
141  * (including payload_size; i.e. no payload).
142  * This is for use by byte-stream LNDs (e.g. TCP/IP) to check the peer is
143  * running the same protocol and to find out its NID. These LNDs should
144  * exchange HELLO messages when a connection is first established.  Individual
145  * LNDs can put whatever else they fancy in lnet_hdr_t::msg.
146  */
147 typedef struct {
148         __u32   magic;                          /* LNET_PROTO_TCP_MAGIC */
149         __u16   version_major;                  /* increment on incompatible change */
150         __u16   version_minor;                  /* increment on compatible change */
151 } WIRE_ATTR lnet_magicversion_t;
152
153 /* PROTO MAGIC for LNDs */
154 #define LNET_PROTO_IB_MAGIC                 0x0be91b91
155 #define LNET_PROTO_RA_MAGIC                 0x0be91b92
156 #define LNET_PROTO_QSW_MAGIC                0x0be91b93
157 #define LNET_PROTO_GNI_MAGIC                0xb00fbabe /* ask Kim */
158 #define LNET_PROTO_TCP_MAGIC                0xeebc0ded
159 #define LNET_PROTO_PTL_MAGIC                0x50746C4E /* 'PtlN' unique magic */
160 #define LNET_PROTO_MX_MAGIC                 0x4d583130 /* 'MX10'! */
161 #define LNET_PROTO_ACCEPTOR_MAGIC           0xacce7100
162 #define LNET_PROTO_PING_MAGIC               0x70696E67 /* 'ping' */
163
164 /* Placeholder for a future "unified" protocol across all LNDs */
165 /* Current LNDs that receive a request with this magic will respond with a
166  * "stub" reply using their current protocol */
167 #define LNET_PROTO_MAGIC                    0x45726963 /* ! */
168
169
170 #define LNET_PROTO_TCP_VERSION_MAJOR        1
171 #define LNET_PROTO_TCP_VERSION_MINOR        0
172
173 /* Acceptor connection request */
174 typedef struct {
175         __u32       acr_magic;                  /* PTL_ACCEPTOR_PROTO_MAGIC */
176         __u32       acr_version;                /* protocol version */
177         __u64       acr_nid;                    /* target NID */
178 } WIRE_ATTR lnet_acceptor_connreq_t;
179
180 #define LNET_PROTO_ACCEPTOR_VERSION       1
181
182 /* forward refs */
183 struct lnet_libmd;
184
185 typedef struct lnet_msg {
186         cfs_list_t            msg_activelist;
187         cfs_list_t            msg_list;           /* Q for credits/MD */
188
189         lnet_process_id_t     msg_target;
190         /* where is it from, it's only for building event */
191         lnet_nid_t              msg_from;
192         __u32                   msg_type;
193
194         /* commited for sending */
195         unsigned int            msg_tx_committed:1;
196         /* CPT # this message committed for sending */
197         unsigned int            msg_tx_cpt:15;
198         /* commited for receiving */
199         unsigned int            msg_rx_committed:1;
200         /* CPT # this message committed for receiving */
201         unsigned int            msg_rx_cpt:15;
202         /* queued for tx credit */
203         unsigned int            msg_tx_delayed:1;
204         /* queued for RX buffer */
205         unsigned int            msg_rx_delayed:1;
206         /* ready for pending on RX delay list */
207         unsigned int            msg_rx_ready_delay:1;
208
209         unsigned int          msg_vmflush:1;      /* VM trying to free memory */
210         unsigned int          msg_target_is_router:1; /* sending to a router */
211         unsigned int          msg_routing:1;      /* being forwarded */
212         unsigned int          msg_ack:1;          /* ack on finalize (PUT) */
213         unsigned int          msg_sending:1;      /* outgoing message */
214         unsigned int          msg_receiving:1;    /* being received */
215         unsigned int          msg_txcredit:1;     /* taken an NI send credit */
216         unsigned int          msg_peertxcredit:1; /* taken a peer send credit */
217         unsigned int          msg_rtrcredit:1;    /* taken a globel router credit */
218         unsigned int          msg_peerrtrcredit:1; /* taken a peer router credit */
219         unsigned int          msg_onactivelist:1; /* on the activelist */
220
221         struct lnet_peer     *msg_txpeer;         /* peer I'm sending to */
222         struct lnet_peer     *msg_rxpeer;         /* peer I received from */
223
224         void                 *msg_private;
225         struct lnet_libmd    *msg_md;
226
227         unsigned int          msg_len;
228         unsigned int          msg_wanted;
229         unsigned int          msg_offset;
230         unsigned int          msg_niov;
231         struct iovec         *msg_iov;
232         lnet_kiov_t          *msg_kiov;
233
234         lnet_event_t          msg_ev;
235         lnet_hdr_t            msg_hdr;
236 } lnet_msg_t;
237
238
239 typedef struct lnet_libhandle {
240         cfs_list_t            lh_hash_chain;
241         __u64                 lh_cookie;
242 } lnet_libhandle_t;
243
244 #define lh_entry(ptr, type, member) \
245         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
246
247 typedef struct lnet_eq {
248         cfs_list_t              eq_list;
249         lnet_libhandle_t        eq_lh;
250         lnet_seq_t              eq_enq_seq;
251         lnet_seq_t              eq_deq_seq;
252         unsigned int            eq_size;
253         lnet_eq_handler_t       eq_callback;
254         lnet_event_t            *eq_events;
255         int                     **eq_refs;      /* percpt refcount for EQ */
256 } lnet_eq_t;
257
258 typedef struct lnet_me {
259         cfs_list_t             me_list;
260         lnet_libhandle_t       me_lh;
261         lnet_process_id_t      me_match_id;
262         unsigned int           me_portal;
263         unsigned int           me_pos;          /* hash offset in mt_hash */
264         __u64                  me_match_bits;
265         __u64                  me_ignore_bits;
266         lnet_unlink_t          me_unlink;
267         struct lnet_libmd     *me_md;
268 } lnet_me_t;
269
270 typedef struct lnet_libmd {
271         cfs_list_t            md_list;
272         lnet_libhandle_t      md_lh;
273         lnet_me_t            *md_me;
274         char                 *md_start;
275         unsigned int          md_offset;
276         unsigned int          md_length;
277         unsigned int          md_max_size;
278         int                   md_threshold;
279         int                   md_refcount;
280         unsigned int          md_options;
281         unsigned int          md_flags;
282         void                 *md_user_ptr;
283         lnet_eq_t            *md_eq;
284         unsigned int          md_niov;                /* # frags */
285         union {
286                 struct iovec  iov[LNET_MAX_IOV];
287                 lnet_kiov_t   kiov[LNET_MAX_IOV];
288         } md_iov;
289 } lnet_libmd_t;
290
291 #define LNET_MD_FLAG_ZOMBIE      (1 << 0)
292 #define LNET_MD_FLAG_AUTO_UNLINK (1 << 1)
293 #define LNET_MD_FLAG_ABORTED     (1 << 2)
294
295 #ifdef LNET_USE_LIB_FREELIST
296 typedef struct
297 {
298         void                  *fl_objs;          /* single contiguous array of objects */
299         int                    fl_nobjs;         /* the number of them */
300         int                    fl_objsize;       /* the size (including overhead) of each of them */
301         cfs_list_t             fl_list;          /* where they are enqueued */
302 } lnet_freelist_t;
303
304 typedef struct
305 {
306         cfs_list_t             fo_list;             /* enqueue on fl_list */
307         void                  *fo_contents;         /* aligned contents */
308 } lnet_freeobj_t;
309 #endif
310
311 typedef struct {
312         /* info about peers we are trying to fail */
313         cfs_list_t             tp_list;             /* ln_test_peers */
314         lnet_nid_t             tp_nid;              /* matching nid */
315         unsigned int           tp_threshold;        /* # failures to simulate */
316 } lnet_test_peer_t;
317
318 #define LNET_COOKIE_TYPE_MD    1
319 #define LNET_COOKIE_TYPE_ME    2
320 #define LNET_COOKIE_TYPE_EQ    3
321 #define LNET_COOKIE_TYPE_BITS  2
322 #define LNET_COOKIE_MASK        ((1ULL << LNET_COOKIE_TYPE_BITS) - 1ULL)
323
324 struct lnet_ni;                                  /* forward ref */
325
326 typedef struct lnet_lnd
327 {
328         /* fields managed by portals */
329         cfs_list_t            lnd_list;             /* stash in the LND table */
330         int                   lnd_refcount;         /* # active instances */
331
332         /* fields initialised by the LND */
333         unsigned int          lnd_type;
334
335         int  (*lnd_startup) (struct lnet_ni *ni);
336         void (*lnd_shutdown) (struct lnet_ni *ni);
337         int  (*lnd_ctl)(struct lnet_ni *ni, unsigned int cmd, void *arg);
338
339         /* In data movement APIs below, payload buffers are described as a set
340          * of 'niov' fragments which are...
341          * EITHER
342          *    in virtual memory (struct iovec *iov != NULL)
343          * OR
344          *    in pages (kernel only: plt_kiov_t *kiov != NULL).
345          * The LND may NOT overwrite these fragment descriptors.
346          * An 'offset' and may specify a byte offset within the set of
347          * fragments to start from
348          */
349
350         /* Start sending a preformatted message.  'private' is NULL for PUT and
351          * GET messages; otherwise this is a response to an incoming message
352          * and 'private' is the 'private' passed to lnet_parse().  Return
353          * non-zero for immediate failure, otherwise complete later with
354          * lnet_finalize() */
355         int (*lnd_send)(struct lnet_ni *ni, void *private, lnet_msg_t *msg);
356
357         /* Start receiving 'mlen' bytes of payload data, skipping the following
358          * 'rlen' - 'mlen' bytes. 'private' is the 'private' passed to
359          * lnet_parse().  Return non-zero for immedaite failure, otherwise
360          * complete later with lnet_finalize().  This also gives back a receive
361          * credit if the LND does flow control. */
362         int (*lnd_recv)(struct lnet_ni *ni, void *private, lnet_msg_t *msg,
363                         int delayed, unsigned int niov,
364                         struct iovec *iov, lnet_kiov_t *kiov,
365                         unsigned int offset, unsigned int mlen, unsigned int rlen);
366
367         /* lnet_parse() has had to delay processing of this message
368          * (e.g. waiting for a forwarding buffer or send credits).  Give the
369          * LND a chance to free urgently needed resources.  If called, return 0
370          * for success and do NOT give back a receive credit; that has to wait
371          * until lnd_recv() gets called.  On failure return < 0 and
372          * release resources; lnd_recv() will not be called. */
373         int (*lnd_eager_recv)(struct lnet_ni *ni, void *private, lnet_msg_t *msg,
374                               void **new_privatep);
375
376         /* notification of peer health */
377         void (*lnd_notify)(struct lnet_ni *ni, lnet_nid_t peer, int alive);
378
379         /* query of peer aliveness */
380         void (*lnd_query)(struct lnet_ni *ni, lnet_nid_t peer, cfs_time_t *when);
381
382 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
383         /* accept a new connection */
384         int (*lnd_accept)(struct lnet_ni *ni, cfs_socket_t *sock);
385 #endif
386
387 #ifndef __KERNEL__
388         /* wait for something to happen */
389         void (*lnd_wait)(struct lnet_ni *ni, int milliseconds);
390
391         /* ensure non-RDMA messages can be received outside liblustre */
392         int (*lnd_setasync)(struct lnet_ni *ni, lnet_process_id_t id, int nasync);
393 #endif
394 } lnd_t;
395
396 #define LNET_NI_STATUS_UP      0x15aac0de
397 #define LNET_NI_STATUS_DOWN    0xdeadface
398 #define LNET_NI_STATUS_INVALID 0x00000000
399 typedef struct {
400         lnet_nid_t ns_nid;
401         __u32      ns_status;
402         __u32      ns_unused;
403 } WIRE_ATTR lnet_ni_status_t;
404
405 struct lnet_tx_queue {
406         int                     tq_credits;     /* # tx credits free */
407         int                     tq_credits_min; /* lowest it's been */
408         int                     tq_credits_max; /* total # tx credits */
409         cfs_list_t              tq_delayed;     /* delayed TXs */
410 };
411
412 #define LNET_MAX_INTERFACES   16
413
414 typedef struct lnet_ni {
415 #ifdef __KERNEL__
416         spinlock_t              ni_lock;
417 #else
418 # ifndef HAVE_LIBPTHREAD
419         int                     ni_lock;
420 # else
421         pthread_mutex_t         ni_lock;
422 # endif
423 #endif
424         cfs_list_t              ni_list;        /* chain on ln_nis */
425         cfs_list_t              ni_cptlist;     /* chain on ln_nis_cpt */
426         int                     ni_maxtxcredits; /* # tx credits  */
427         /* # per-peer send credits */
428         int                     ni_peertxcredits;
429         /* # per-peer router buffer credits */
430         int                     ni_peerrtrcredits;
431         /* seconds to consider peer dead */
432         int                     ni_peertimeout;
433         int                     ni_ncpts;       /* number of CPTs */
434         __u32                   *ni_cpts;       /* bond NI on some CPTs */
435         lnet_nid_t              ni_nid;         /* interface's NID */
436         void                    *ni_data;       /* instance-specific data */
437         lnd_t                   *ni_lnd;        /* procedural interface */
438         struct lnet_tx_queue    **ni_tx_queues; /* percpt TX queues */
439         int                     **ni_refs;      /* percpt reference count */
440         long                    ni_last_alive;  /* when I was last alive */
441         lnet_ni_status_t        *ni_status;     /* my health status */
442         /* equivalent interfaces to use */
443         char                    *ni_interfaces[LNET_MAX_INTERFACES];
444 } lnet_ni_t;
445
446 #define LNET_PROTO_PING_MATCHBITS       0x8000000000000000LL
447
448 /* NB: value of these features equal to LNET_PROTO_PING_VERSION_x
449  * of old LNet, so there shouldn't be any compatibility issue */
450 #define LNET_PING_FEAT_INVAL            (0)             /* no feature */
451 #define LNET_PING_FEAT_BASE             (1 << 0)        /* just a ping */
452 #define LNET_PING_FEAT_NI_STATUS        (1 << 1)        /* return NI status */
453
454 #define LNET_PING_FEAT_MASK             (LNET_PING_FEAT_BASE | \
455                                          LNET_PING_FEAT_NI_STATUS)
456
457 typedef struct {
458         __u32                   pi_magic;
459         __u32                   pi_features;
460         lnet_pid_t              pi_pid;
461         __u32                   pi_nnis;
462         lnet_ni_status_t        pi_ni[0];
463 } WIRE_ATTR lnet_ping_info_t;
464
465 /* router checker data, per router */
466 #define LNET_MAX_RTR_NIS   16
467 #define LNET_PINGINFO_SIZE offsetof(lnet_ping_info_t, pi_ni[LNET_MAX_RTR_NIS])
468 typedef struct {
469         /* chain on the_lnet.ln_zombie_rcd or ln_deathrow_rcd */
470         cfs_list_t              rcd_list;
471         lnet_handle_md_t        rcd_mdh;        /* ping buffer MD */
472         struct lnet_peer        *rcd_gateway;   /* reference to gateway */
473         lnet_ping_info_t        *rcd_pinginfo;  /* ping buffer */
474 } lnet_rc_data_t;
475
476 typedef struct lnet_peer {
477         cfs_list_t        lp_hashlist;          /* chain on peer hash */
478         cfs_list_t        lp_txq;               /* messages blocking for tx credits */
479         cfs_list_t        lp_rtrq;              /* messages blocking for router credits */
480         cfs_list_t        lp_rtr_list;          /* chain on router list */
481         int               lp_txcredits;         /* # tx credits available */
482         int               lp_mintxcredits;      /* low water mark */
483         int               lp_rtrcredits;        /* # router credits */
484         int               lp_minrtrcredits;     /* low water mark */
485         unsigned int      lp_alive:1;           /* alive/dead? */
486         unsigned int      lp_notify:1;          /* notification outstanding? */
487         unsigned int      lp_notifylnd:1;       /* outstanding notification for LND? */
488         unsigned int      lp_notifying:1;       /* some thread is handling notification */
489         unsigned int      lp_ping_notsent;      /* SEND event outstanding from ping */
490         int               lp_alive_count;       /* # times router went dead<->alive */
491         long              lp_txqnob;            /* bytes queued for sending */
492         cfs_time_t        lp_timestamp;         /* time of last aliveness news */
493         cfs_time_t        lp_ping_timestamp;    /* time of last ping attempt */
494         cfs_time_t        lp_ping_deadline;     /* != 0 if ping reply expected */
495         cfs_time_t        lp_last_alive;        /* when I was last alive */
496         cfs_time_t        lp_last_query;        /* when lp_ni was queried last time */
497         lnet_ni_t        *lp_ni;                /* interface peer is on */
498         lnet_nid_t        lp_nid;               /* peer's NID */
499         int               lp_refcount;          /* # refs */
500         int                     lp_cpt;         /* CPT this peer attached on */
501         /* # refs from lnet_route_t::lr_gateway */
502         int                     lp_rtr_refcount;
503         /* returned RC ping features */
504         unsigned int            lp_ping_feats;
505         cfs_list_t              lp_routes;      /* routers on this peer */
506         lnet_rc_data_t          *lp_rcd;        /* router checker state */
507 } lnet_peer_t;
508
509 /* peer hash size */
510 #define LNET_PEER_HASH_BITS     9
511 #define LNET_PEER_HASH_SIZE     (1 << LNET_PEER_HASH_BITS)
512
513 /* peer hash table */
514 struct lnet_peer_table {
515         int                     pt_version;     /* /proc validity stamp */
516         int                     pt_number;      /* # peers extant */
517         cfs_list_t              pt_deathrow;    /* zombie peers */
518         cfs_list_t              *pt_hash;       /* NID->peer hash */
519 };
520
521 /* peer aliveness is enabled only on routers for peers in a network where the
522  * lnet_ni_t::ni_peertimeout has been set to a positive value */
523 #define lnet_peer_aliveness_enabled(lp) (the_lnet.ln_routing != 0 && \
524                                          (lp)->lp_ni->ni_peertimeout > 0)
525
526 typedef struct {
527         cfs_list_t              lr_list;        /* chain on net */
528         cfs_list_t              lr_gwlist;      /* chain on gateway */
529         lnet_peer_t             *lr_gateway;    /* router node */
530         __u32                   lr_net;         /* remote network number */
531         int                     lr_seq;         /* sequence for round-robin */
532         unsigned int            lr_downis;      /* number of down NIs */
533         unsigned int            lr_hops;        /* how far I am */
534         unsigned int            lr_priority;    /* route priority */
535 } lnet_route_t;
536
537 #define LNET_REMOTE_NETS_HASH_DEFAULT   (1U << 7)
538 #define LNET_REMOTE_NETS_HASH_MAX       (1U << 16)
539 #define LNET_REMOTE_NETS_HASH_SIZE      (1 << the_lnet.ln_remote_nets_hbits)
540
541 typedef struct {
542         cfs_list_t              lrn_list;       /* chain on ln_remote_nets_hash */
543         cfs_list_t              lrn_routes;     /* routes to me */
544         __u32                   lrn_net;        /* my net number */
545 } lnet_remotenet_t;
546
547 typedef struct {
548         cfs_list_t rbp_bufs;             /* my free buffer pool */
549         cfs_list_t rbp_msgs;             /* messages blocking for a buffer */
550         int        rbp_npages;           /* # pages in each buffer */
551         int        rbp_nbuffers;         /* # buffers */
552         int        rbp_credits;          /* # free buffers / blocked messages */
553         int        rbp_mincredits;       /* low water mark */
554 } lnet_rtrbufpool_t;
555
556 typedef struct {
557         cfs_list_t             rb_list;             /* chain on rbp_bufs */
558         lnet_rtrbufpool_t     *rb_pool;             /* owning pool */
559         lnet_kiov_t            rb_kiov[0];          /* the buffer space */
560 } lnet_rtrbuf_t;
561
562 typedef struct {
563         __u32        msgs_alloc;
564         __u32        msgs_max;
565         __u32        errors;
566         __u32        send_count;
567         __u32        recv_count;
568         __u32        route_count;
569         __u32        drop_count;
570         __u64        send_length;
571         __u64        recv_length;
572         __u64        route_length;
573         __u64        drop_length;
574 } WIRE_ATTR lnet_counters_t;
575
576 #define LNET_PEER_HASHSIZE   503                /* prime! */
577
578 #define LNET_NRBPOOLS         3                 /* # different router buffer pools */
579
580 enum {
581         /* Didn't match anything */
582         LNET_MATCHMD_NONE       = (1 << 0),
583         /* Matched OK */
584         LNET_MATCHMD_OK         = (1 << 1),
585         /* Must be discarded */
586         LNET_MATCHMD_DROP       = (1 << 2),
587         /* match and buffer is exhausted */
588         LNET_MATCHMD_EXHAUSTED  = (1 << 3),
589         /* match or drop */
590         LNET_MATCHMD_FINISH     = (LNET_MATCHMD_OK | LNET_MATCHMD_DROP),
591 };
592
593 /* Options for lnet_portal_t::ptl_options */
594 #define LNET_PTL_LAZY               (1 << 0)
595 #define LNET_PTL_MATCH_UNIQUE       (1 << 1)    /* unique match, for RDMA */
596 #define LNET_PTL_MATCH_WILDCARD     (1 << 2)    /* wildcard match, request portal */
597
598 /* parameter for matching operations (GET, PUT) */
599 struct lnet_match_info {
600         __u64                   mi_mbits;
601         lnet_process_id_t       mi_id;
602         unsigned int            mi_opc;
603         unsigned int            mi_portal;
604         unsigned int            mi_rlength;
605         unsigned int            mi_roffset;
606 };
607
608 /* ME hash of RDMA portal */
609 #define LNET_MT_HASH_BITS               8
610 #define LNET_MT_HASH_SIZE               (1 << LNET_MT_HASH_BITS)
611 #define LNET_MT_HASH_MASK               (LNET_MT_HASH_SIZE - 1)
612 /* we allocate (LNET_MT_HASH_SIZE + 1) entries for lnet_match_table::mt_hash,
613  * the last entry is reserved for MEs with ignore-bits */
614 #define LNET_MT_HASH_IGNORE             LNET_MT_HASH_SIZE
615 /* __u64 has 2^6 bits, so need 2^(LNET_MT_HASH_BITS - LNET_MT_BITS_U64) which
616  * is 4 __u64s as bit-map, and add an extra __u64 (only use one bit) for the
617  * ME-list with ignore-bits, which is mtable::mt_hash[LNET_MT_HASH_IGNORE] */
618 #define LNET_MT_BITS_U64                6       /* 2^6 bits */
619 #define LNET_MT_EXHAUSTED_BITS          (LNET_MT_HASH_BITS - LNET_MT_BITS_U64)
620 #define LNET_MT_EXHAUSTED_BMAP          ((1 << LNET_MT_EXHAUSTED_BITS) + 1)
621
622 /* portal match table */
623 struct lnet_match_table {
624         /* reserved for upcoming patches, CPU partition ID */
625         unsigned int            mt_cpt;
626         unsigned int            mt_portal;      /* portal index */
627         /* match table is set as "enabled" if there's non-exhausted MD
628          * attached on mt_mhash, it's only valide for wildcard portal */
629         unsigned int            mt_enabled;
630         /* bitmap to flag whether MEs on mt_hash are exhausted or not */
631         __u64                   mt_exhausted[LNET_MT_EXHAUSTED_BMAP];
632         cfs_list_t              *mt_mhash;      /* matching hash */
633 };
634
635 /* these are only useful for wildcard portal */
636 /* Turn off message rotor for wildcard portals */
637 #define LNET_PTL_ROTOR_OFF      0
638 /* round-robin dispatch all PUT messages for wildcard portals */
639 #define LNET_PTL_ROTOR_ON       1
640 /* round-robin dispatch routed PUT message for wildcard portals */
641 #define LNET_PTL_ROTOR_RR_RT    2
642 /* dispatch routed PUT message by hashing source NID for wildcard portals */
643 #define LNET_PTL_ROTOR_HASH_RT  3
644
645 typedef struct lnet_portal {
646 #ifdef __KERNEL__
647         spinlock_t              ptl_lock;
648 #else
649 # ifndef HAVE_LIBPTHREAD
650         int                     ptl_lock;
651 # else
652         pthread_mutex_t         ptl_lock;
653 # endif
654 #endif
655         unsigned int            ptl_index;      /* portal ID, reserved */
656         /* flags on this portal: lazy, unique... */
657         unsigned int            ptl_options;
658         /* list of messags which are stealing buffer */
659         cfs_list_t              ptl_msg_stealing;
660         /* messages blocking for MD */
661         cfs_list_t              ptl_msg_delayed;
662         /* Match table for each CPT */
663         struct lnet_match_table **ptl_mtables;
664         /* spread rotor of incoming "PUT" */
665         int                     ptl_rotor;
666         /* # active entries for this portal */
667         int                     ptl_mt_nmaps;
668         /* array of active entries' cpu-partition-id */
669         int                     ptl_mt_maps[0];
670 } lnet_portal_t;
671
672 #define LNET_LH_HASH_BITS       12
673 #define LNET_LH_HASH_SIZE       (1ULL << LNET_LH_HASH_BITS)
674 #define LNET_LH_HASH_MASK       (LNET_LH_HASH_SIZE - 1)
675
676 /* resource container (ME, MD, EQ) */
677 struct lnet_res_container {
678         unsigned int            rec_type;       /* container type */
679         __u64                   rec_lh_cookie;  /* cookie generator */
680         cfs_list_t              rec_active;     /* active resource list */
681         cfs_list_t              *rec_lh_hash;   /* handle hash */
682 #ifdef LNET_USE_LIB_FREELIST
683         lnet_freelist_t         rec_freelist;   /* freelist for resources */
684 #endif
685 };
686
687 /* message container */
688 struct lnet_msg_container {
689         int                     msc_init;       /* initialized or not */
690         /* max # threads finalizing */
691         int                     msc_nfinalizers;
692         /* msgs waiting to complete finalizing */
693         cfs_list_t              msc_finalizing;
694         cfs_list_t              msc_active;     /* active message list */
695         /* threads doing finalization */
696         void                    **msc_finalizers;
697 #ifdef LNET_USE_LIB_FREELIST
698         lnet_freelist_t         msc_freelist;   /* freelist for messages */
699 #endif
700 };
701
702 /* Router Checker states */
703 #define LNET_RC_STATE_SHUTDOWN          0       /* not started */
704 #define LNET_RC_STATE_RUNNING           1       /* started up OK */
705 #define LNET_RC_STATE_STOPPING          2       /* telling thread to stop */
706
707 typedef struct
708 {
709         /* CPU partition table of LNet */
710         struct cfs_cpt_table            *ln_cpt_table;
711         /* number of CPTs in ln_cpt_table */
712         unsigned int                    ln_cpt_number;
713         unsigned int                    ln_cpt_bits;
714
715         /* protect LNet resources (ME/MD/EQ) */
716         struct cfs_percpt_lock          *ln_res_lock;
717         /* # portals */
718         int                             ln_nportals;
719         /* the vector of portals */
720         lnet_portal_t                   **ln_portals;
721         /* percpt ME containers */
722         struct lnet_res_container       **ln_me_containers;
723         /* percpt MD container */
724         struct lnet_res_container       **ln_md_containers;
725
726         /* Event Queue container */
727         struct lnet_res_container       ln_eq_container;
728 #ifdef __KERNEL__
729         wait_queue_head_t                       ln_eq_waitq;
730         spinlock_t                      ln_eq_wait_lock;
731 #else
732 # ifndef HAVE_LIBPTHREAD
733         int                             ln_eq_wait_lock;
734 # else
735         pthread_cond_t                  ln_eq_cond;
736         pthread_mutex_t                 ln_eq_wait_lock;
737 # endif
738 #endif
739         unsigned int                    ln_remote_nets_hbits;
740
741         /* protect NI, peer table, credits, routers, rtrbuf... */
742         struct cfs_percpt_lock          *ln_net_lock;
743         /* percpt message containers for active/finalizing/freed message */
744         struct lnet_msg_container       **ln_msg_containers;
745         lnet_counters_t                 **ln_counters;
746         struct lnet_peer_table          **ln_peer_tables;
747         /* failure simulation */
748         cfs_list_t                      ln_test_peers;
749
750         cfs_list_t                      ln_nis;         /* LND instances */
751         /* NIs bond on specific CPT(s) */
752         cfs_list_t                      ln_nis_cpt;
753         /* dying LND instances */
754         cfs_list_t                      ln_nis_zombie;
755         lnet_ni_t                       *ln_loni;       /* the loopback NI */
756         /* NI to wait for events in */
757         lnet_ni_t                       *ln_eq_waitni;
758
759         /* remote networks with routes to them */
760         cfs_list_t                      *ln_remote_nets_hash;
761         /* validity stamp */
762         __u64                           ln_remote_nets_version;
763         /* list of all known routers */
764         cfs_list_t                      ln_routers;
765         /* validity stamp */
766         __u64                           ln_routers_version;
767         /* percpt router buffer pools */
768         lnet_rtrbufpool_t               **ln_rtrpools;
769
770         lnet_handle_md_t                ln_ping_target_md;
771         lnet_handle_eq_t                ln_ping_target_eq;
772         lnet_ping_info_t                *ln_ping_info;
773
774         /* router checker startup/shutdown state */
775         int                             ln_rc_state;
776         /* router checker's event queue */
777         lnet_handle_eq_t                ln_rc_eqh;
778         /* rcd still pending on net */
779         cfs_list_t                      ln_rcd_deathrow;
780         /* rcd ready for free */
781         cfs_list_t                      ln_rcd_zombie;
782 #ifdef __KERNEL__
783         /* serialise startup/shutdown */
784         struct semaphore                ln_rc_signal;
785
786         struct mutex                    ln_api_mutex;
787         struct mutex                    ln_lnd_mutex;
788 #else
789 # ifndef HAVE_LIBPTHREAD
790         int                             ln_api_mutex;
791         int                             ln_lnd_mutex;
792 # else
793         pthread_mutex_t                 ln_api_mutex;
794         pthread_mutex_t                 ln_lnd_mutex;
795 # endif
796 #endif
797         int                             ln_init;        /* LNetInit() called? */
798         /* Have I called LNetNIInit myself? */
799         int                             ln_niinit_self;
800         /* LNetNIInit/LNetNIFini counter */
801         int                             ln_refcount;
802         /* shutdown in progress */
803         int                             ln_shutdown;
804
805         int                             ln_routing;     /* am I a router? */
806         lnet_pid_t                      ln_pid;         /* requested pid */
807         /* uniquely identifies this ni in this epoch */
808         __u64                           ln_interface_cookie;
809         /* registered LNDs */
810         cfs_list_t                      ln_lnds;
811
812         /* space for network names */
813         char                            *ln_network_tokens;
814         int                             ln_network_tokens_nob;
815         /* test protocol compatibility flags */
816         int                             ln_testprotocompat;
817
818 #ifndef __KERNEL__
819         /* Temporary workaround to allow uOSS and test programs force
820          * server mode in userspace. The only place where we use it is
821          * lnet_prepare(). The only way to turn this flag on is to
822          * call lnet_server_mode() */
823         int                             ln_server_mode_flag;
824 #endif
825 } lnet_t;
826
827 #endif