Whamcloud - gitweb
b=12302
[fs/lustre-release.git] / lnet / include / lnet / lib-types.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * p30/lib-types.h
5  *
6  * Types used by the library side routines that do not need to be
7  * exposed to the user application
8  */
9
10 #ifndef __LNET_LIB_TYPES_H__
11 #define __LNET_LIB_TYPES_H__
12
13 #if defined(__linux__)
14 #include <lnet/linux/lib-types.h>
15 #elif defined(__APPLE__)
16 #include <lnet/darwin/lib-types.h>
17 #elif defined(__WINNT__)
18 #include <lnet/winnt/lib-types.h>
19 #else
20 #error Unsupported Operating System
21 #endif
22
23 #include <libcfs/libcfs.h>
24 #include <libcfs/list.h>
25 #include <lnet/types.h>
26
27 #define WIRE_ATTR       __attribute__((packed))
28
29 /* The wire handle's interface cookie only matches one network interface in
30  * one epoch (i.e. new cookie when the interface restarts or the node
31  * reboots).  The object cookie only matches one object on that interface
32  * during that object's lifetime (i.e. no cookie re-use). */
33 typedef struct {
34         __u64 wh_interface_cookie;
35         __u64 wh_object_cookie;
36 } WIRE_ATTR lnet_handle_wire_t;
37
38 /* byte-flip insensitive! */
39 #define LNET_WIRE_HANDLE_NONE \
40 ((const lnet_handle_wire_t) {.wh_interface_cookie = -1, .wh_object_cookie = -1})
41
42 typedef enum {
43         LNET_MSG_ACK = 0,
44         LNET_MSG_PUT,
45         LNET_MSG_GET,
46         LNET_MSG_REPLY,
47         LNET_MSG_HELLO,
48 } lnet_msg_type_t;
49
50 /* The variant fields of the portals message header are aligned on an 8
51  * byte boundary in the message header.  Note that all types used in these
52  * wire structs MUST be fixed size and the smaller types are placed at the
53  * end. */
54 typedef struct lnet_ack {
55         lnet_handle_wire_t  dst_wmd;
56         __u64               match_bits;
57         __u32               mlength;
58 } WIRE_ATTR lnet_ack_t;
59
60 typedef struct lnet_put {
61         lnet_handle_wire_t  ack_wmd;
62         __u64               match_bits;
63         __u64               hdr_data;
64         __u32               ptl_index;
65         __u32               offset;
66 } WIRE_ATTR lnet_put_t;
67
68 typedef struct lnet_get {
69         lnet_handle_wire_t  return_wmd;
70         __u64               match_bits;
71         __u32               ptl_index;
72         __u32               src_offset;
73         __u32               sink_length;
74 } WIRE_ATTR lnet_get_t;
75
76 typedef struct lnet_reply {
77         lnet_handle_wire_t  dst_wmd;
78 } WIRE_ATTR lnet_reply_t;
79
80 typedef struct lnet_hello {
81         __u64              incarnation;
82         __u32              type;
83 } WIRE_ATTR lnet_hello_t;
84
85 typedef struct {
86         lnet_nid_t          dest_nid;
87         lnet_nid_t          src_nid;
88         lnet_pid_t          dest_pid;
89         lnet_pid_t          src_pid;
90         __u32               type;               /* lnet_msg_type_t */
91         __u32               payload_length;     /* payload data to follow */
92         /*<------__u64 aligned------->*/
93         union {
94                 lnet_ack_t   ack;
95                 lnet_put_t   put;
96                 lnet_get_t   get;
97                 lnet_reply_t reply;
98                 lnet_hello_t hello;
99         } msg;
100 } WIRE_ATTR lnet_hdr_t;
101
102 /* A HELLO message contains a magic number and protocol version
103  * code in the header's dest_nid, the peer's NID in the src_nid, and
104  * LNET_MSG_HELLO in the type field.  All other common fields are zero
105  * (including payload_size; i.e. no payload).  
106  * This is for use by byte-stream LNDs (e.g. TCP/IP) to check the peer is
107  * running the same protocol and to find out its NID. These LNDs should
108  * exchange HELLO messages when a connection is first established.  Individual
109  * LNDs can put whatever else they fancy in lnet_hdr_t::msg.
110  */
111 typedef struct {
112         __u32   magic;                          /* LNET_PROTO_TCP_MAGIC */
113         __u16   version_major;                  /* increment on incompatible change */
114         __u16   version_minor;                  /* increment on compatible change */
115 } WIRE_ATTR lnet_magicversion_t;
116
117 /* PROTO MAGIC for LNDs */
118 #define LNET_PROTO_IB_MAGIC                 0x0be91b91
119 #define LNET_PROTO_OPENIB_MAGIC             LNET_PROTO_IB_MAGIC
120 #define LNET_PROTO_IIB_MAGIC                LNET_PROTO_IB_MAGIC
121 #define LNET_PROTO_VIB_MAGIC                LNET_PROTO_IB_MAGIC
122 #define LNET_PROTO_RA_MAGIC                 0x0be91b92
123 #define LNET_PROTO_QSW_MAGIC                0x0be91b93
124 #define LNET_PROTO_TCP_MAGIC                0xeebc0ded
125 #define LNET_PROTO_PTL_MAGIC                0x50746C4E /* 'PtlN' unique magic */
126 #define LNET_PROTO_GM_MAGIC                 0x6d797269 /* 'myri'! */
127 #define LNET_PROTO_MX_MAGIC                 0x4d583130 /* 'MX10'! */
128 #define LNET_PROTO_ACCEPTOR_MAGIC           0xacce7100
129 #define LNET_PROTO_PING_MAGIC               0x70696E67 /* 'ping' */
130
131 /* Placeholder for a future "unified" protocol across all LNDs */
132 /* Current LNDs that receive a request with this magic will respond with a
133  * "stub" reply using their current protocol */
134 #define LNET_PROTO_MAGIC                    0x45726963 /* ! */
135
136
137 #define LNET_PROTO_TCP_VERSION_MAJOR        1
138 #define LNET_PROTO_TCP_VERSION_MINOR        0
139
140 /* Acceptor connection request */
141 typedef struct {
142         __u32       acr_magic;                  /* PTL_ACCEPTOR_PROTO_MAGIC */
143         __u32       acr_version;                /* protocol version */
144         __u64       acr_nid;                    /* target NID */
145 } WIRE_ATTR lnet_acceptor_connreq_t;
146
147 #define LNET_PROTO_ACCEPTOR_VERSION       1
148
149 /* forward refs */
150 struct lnet_libmd;
151
152 typedef struct lnet_msg {
153         struct list_head    msg_activelist;
154         struct list_head    msg_list;           /* Q for credits/MD */
155
156         lnet_process_id_t   msg_target;
157         __u32               msg_type;
158
159         unsigned int        msg_target_is_router:1; /* sending to a router */
160         unsigned int        msg_routing:1;      /* being forwarded */
161         unsigned int        msg_ack:1;          /* ack on finalize (PUT) */
162         unsigned int        msg_sending:1;      /* outgoing message */
163         unsigned int        msg_receiving:1;    /* being received */
164         unsigned int        msg_delayed:1;      /* had to Q for buffer or tx credit */
165         unsigned int        msg_txcredit:1;     /* taken an NI send credit */
166         unsigned int        msg_peertxcredit:1; /* taken a peer send credit */
167         unsigned int        msg_rtrcredit:1;    /* taken a globel router credit */
168         unsigned int        msg_peerrtrcredit:1; /* taken a peer router credit */
169         unsigned int        msg_onactivelist:1; /* on the activelist */
170
171         struct lnet_peer   *msg_txpeer;         /* peer I'm sending to */
172         struct lnet_peer   *msg_rxpeer;         /* peer I received from */
173
174         void               *msg_private;
175         struct lnet_libmd  *msg_md;
176
177         unsigned int        msg_len;
178         unsigned int        msg_wanted;
179         unsigned int        msg_offset;
180         unsigned int        msg_niov;
181         struct iovec       *msg_iov;
182         lnet_kiov_t        *msg_kiov;
183
184         lnet_event_t        msg_ev;
185         lnet_hdr_t          msg_hdr;
186 } lnet_msg_t;
187
188
189 typedef struct lnet_libhandle {
190         struct list_head  lh_hash_chain;
191         __u64             lh_cookie;
192 } lnet_libhandle_t;
193
194 #define lh_entry(ptr, type, member) \
195         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
196
197 typedef struct lnet_eq {
198         struct list_head  eq_list;
199         lnet_libhandle_t  eq_lh;
200         lnet_seq_t        eq_enq_seq;
201         lnet_seq_t        eq_deq_seq;
202         unsigned int      eq_size;
203         lnet_event_t     *eq_events;
204         int               eq_refcount;
205         lnet_eq_handler_t eq_callback;
206 } lnet_eq_t;
207
208 typedef struct lnet_me {
209         struct list_head   me_list;
210         lnet_libhandle_t   me_lh;
211         lnet_process_id_t  me_match_id;
212         unsigned int       me_portal;
213         __u64              me_match_bits;
214         __u64              me_ignore_bits;
215         lnet_unlink_t      me_unlink;
216         struct lnet_libmd *me_md;
217 } lnet_me_t;
218
219 typedef struct lnet_libmd {
220         struct list_head  md_list;
221         lnet_libhandle_t  md_lh;
222         lnet_me_t        *md_me;
223         char             *md_start;
224         unsigned int      md_offset;
225         unsigned int      md_length;
226         unsigned int      md_max_size;
227         int               md_threshold;
228         int               md_refcount;
229         unsigned int      md_options;
230         unsigned int      md_flags;
231         void             *md_user_ptr;
232         lnet_eq_t        *md_eq;
233         void             *md_addrkey;
234         unsigned int      md_niov;                /* # frags */
235         union {
236                 struct iovec  iov[LNET_MAX_IOV];
237                 lnet_kiov_t   kiov[LNET_MAX_IOV];
238         } md_iov;
239 } lnet_libmd_t;
240
241 #define LNET_MD_FLAG_ZOMBIE           (1 << 0)
242 #define LNET_MD_FLAG_AUTO_UNLINK      (1 << 1)
243
244 #ifdef LNET_USE_LIB_FREELIST
245 typedef struct
246 {
247         void              *fl_objs;             /* single contiguous array of objects */
248         int                fl_nobjs;            /* the number of them */
249         int                fl_objsize;          /* the size (including overhead) of each of them */
250         struct list_head   fl_list;             /* where they are enqueued */
251 } lnet_freelist_t;
252
253 typedef struct
254 {
255         struct list_head   fo_list;             /* enqueue on fl_list */
256         void              *fo_contents;         /* aligned contents */
257 } lnet_freeobj_t;
258 #endif
259
260 typedef struct {
261         /* info about peers we are trying to fail */
262         struct list_head   tp_list;             /* ln_test_peers */
263         lnet_nid_t         tp_nid;              /* matching nid */
264         unsigned int       tp_threshold;        /* # failures to simulate */
265 } lnet_test_peer_t;
266
267 #define LNET_COOKIE_TYPE_MD    1
268 #define LNET_COOKIE_TYPE_ME    2
269 #define LNET_COOKIE_TYPE_EQ    3
270 #define LNET_COOKIE_TYPES      4
271 /* LNET_COOKIE_TYPES must be a power of 2, so the cookie type can be
272  * extracted by masking with (LNET_COOKIE_TYPES - 1) */
273
274 struct lnet_ni;                                  /* forward ref */
275
276 typedef struct lnet_lnd
277 {
278         /* fields managed by portals */
279         struct list_head  lnd_list;             /* stash in the LND table */
280         int               lnd_refcount;         /* # active instances */
281
282         /* fields initialised by the LND */
283         unsigned int      lnd_type;
284         
285         int  (*lnd_startup) (struct lnet_ni *ni);
286         void (*lnd_shutdown) (struct lnet_ni *ni);
287         int  (*lnd_ctl)(struct lnet_ni *ni, unsigned int cmd, void *arg);
288
289         /* In data movement APIs below, payload buffers are described as a set
290          * of 'niov' fragments which are...
291          * EITHER 
292          *    in virtual memory (struct iovec *iov != NULL)
293          * OR
294          *    in pages (kernel only: plt_kiov_t *kiov != NULL).
295          * The LND may NOT overwrite these fragment descriptors.
296          * An 'offset' and may specify a byte offset within the set of
297          * fragments to start from 
298          */
299
300         /* Start sending a preformatted message.  'private' is NULL for PUT and
301          * GET messages; otherwise this is a response to an incoming message
302          * and 'private' is the 'private' passed to lnet_parse().  Return
303          * non-zero for immediate failure, otherwise complete later with
304          * lnet_finalize() */
305         int (*lnd_send)(struct lnet_ni *ni, void *private, lnet_msg_t *msg);
306
307         /* Start receiving 'mlen' bytes of payload data, skipping the following
308          * 'rlen' - 'mlen' bytes. 'private' is the 'private' passed to
309          * lnet_parse().  Return non-zero for immedaite failure, otherwise
310          * complete later with lnet_finalize().  This also gives back a receive
311          * credit if the LND does flow control. */
312         int (*lnd_recv)(struct lnet_ni *ni, void *private, lnet_msg_t *msg,
313                         int delayed, unsigned int niov, 
314                         struct iovec *iov, lnet_kiov_t *kiov,
315                         unsigned int offset, unsigned int mlen, unsigned int rlen);
316
317         /* lnet_parse() has had to delay processing of this message
318          * (e.g. waiting for a forwarding buffer or send credits).  Give the
319          * LND a chance to free urgently needed resources.  If called, return 0
320          * for success and do NOT give back a receive credit; that has to wait
321          * until lnd_recv() gets called.  On failure return < 0 and
322          * release resources; lnd_recv() will not be called. */
323         int (*lnd_eager_recv)(struct lnet_ni *ni, void *private, lnet_msg_t *msg,
324                               void **new_privatep);
325
326         /* notification of peer health */
327         void (*lnd_notify)(struct lnet_ni *ni, lnet_nid_t peer, int alive);
328
329 #ifdef __KERNEL__
330         /* accept a new connection */
331         int (*lnd_accept)(struct lnet_ni *ni, cfs_socket_t *sock);
332 #else
333         /* wait for something to happen */
334         void (*lnd_wait)(struct lnet_ni *ni, int milliseconds);
335
336         /* ensure non-RDMA messages can be received outside liblustre */
337         int (*lnd_setasync)(struct lnet_ni *ni, lnet_process_id_t id, int nasync);
338
339 #ifdef HAVE_LIBPTHREAD
340         int (*lnd_accept)(struct lnet_ni *ni, int sock);
341 #endif
342 #endif
343 } lnd_t;
344
345 #define LNET_MAX_INTERFACES   16
346
347 typedef struct lnet_ni {
348         struct list_head  ni_list;              /* chain on ln_nis */
349         struct list_head  ni_txq;               /* messages waiting for tx credits */
350         int               ni_maxtxcredits;      /* # tx credits  */
351         int               ni_txcredits;         /* # tx credits free */
352         int               ni_mintxcredits;      /* lowest it's been */
353         int               ni_peertxcredits;     /* # per-peer send credits */
354         lnet_nid_t        ni_nid;               /* interface's NID */
355         void             *ni_data;              /* instance-specific data */
356         lnd_t            *ni_lnd;               /* procedural interface */
357         int               ni_refcount;          /* reference count */
358         char             *ni_interfaces[LNET_MAX_INTERFACES]; /* equivalent interfaces to use */
359 } lnet_ni_t;
360
361 typedef struct lnet_peer {
362         struct list_head  lp_hashlist;          /* chain on peer hash */
363         struct list_head  lp_txq;               /* messages blocking for tx credits */
364         struct list_head  lp_rtrq;              /* messages blocking for router credits */
365         struct list_head  lp_rtr_list;          /* chain on router list */
366         int               lp_txcredits;         /* # tx credits available */
367         int               lp_mintxcredits;      /* low water mark */
368         int               lp_rtrcredits;        /* # router credits */
369         int               lp_minrtrcredits;     /* low water mark */
370         unsigned int      lp_alive:1;           /* alive/dead? */
371         unsigned int      lp_notify:1;          /* notification outstanding? */
372         unsigned int      lp_notifylnd:1;       /* outstanding notification for LND? */
373         unsigned int      lp_notifying:1;       /* some thread is handling notification */
374         unsigned int      lp_ping_notsent;      /* SEND event outstanding from ping */
375         int               lp_alive_count;       /* # times router went dead<->alive */
376         long              lp_txqnob;            /* bytes queued for sending */
377         time_t            lp_timestamp;         /* time of last aliveness news */
378         time_t            lp_ping_timestamp;    /* time of last ping attempt */
379         time_t            lp_ping_deadline;     /* != 0 if ping reply expected */
380         lnet_ni_t        *lp_ni;                /* interface peer is on */
381         lnet_nid_t        lp_nid;               /* peer's NID */
382         int               lp_refcount;          /* # refs */
383         int               lp_rtr_refcount;      /* # refs from lnet_route_t::lr_gateway */
384 } lnet_peer_t;
385
386 typedef struct {
387         struct list_head  lr_list;              /* chain on net */
388         lnet_peer_t      *lr_gateway;           /* router node */
389 } lnet_route_t;
390
391 typedef struct {
392         struct list_head        lrn_list;       /* chain on ln_remote_nets */
393         struct list_head        lrn_routes;     /* routes to me */
394         __u32                   lrn_net;        /* my net number */
395         unsigned int            lrn_hops;       /* how far I am */
396 } lnet_remotenet_t;
397
398 typedef struct {
399         struct list_head  rbp_bufs;             /* my free buffer pool */
400         struct list_head  rbp_msgs;             /* messages blocking for a buffer */
401         int               rbp_npages;           /* # pages in each buffer */
402         int               rbp_nbuffers;         /* # buffers */
403         int               rbp_credits;          /* # free buffers / blocked messages */
404         int               rbp_mincredits;       /* low water mark */
405 } lnet_rtrbufpool_t;
406
407 typedef struct {
408         struct list_head   rb_list;             /* chain on rbp_bufs */
409         lnet_rtrbufpool_t *rb_pool;             /* owning pool */
410         lnet_kiov_t        rb_kiov[0];          /* the buffer space */
411 } lnet_rtrbuf_t;
412
413 typedef struct {
414         __u32        msgs_alloc;
415         __u32        msgs_max;
416         __u32        errors;
417         __u32        send_count;
418         __u32        recv_count;
419         __u32        route_count;
420         __u32        drop_count;
421         __u64        send_length;
422         __u64        recv_length;
423         __u64        route_length;
424         __u64        drop_length;
425 } lnet_counters_t;
426
427 #define LNET_PEER_HASHSIZE   503                /* prime! */
428
429 #define LNET_NRBPOOLS         3                 /* # different router buffer pools */
430
431 #define LNET_PROTO_PING_MATCHBITS     0x8000000000000000LL
432 #define LNET_PROTO_PING_VERSION       1
433 typedef struct {
434         __u32          pi_magic;
435         __u32          pi_version;
436         lnet_pid_t     pi_pid;
437         __u32          pi_nnids;
438         lnet_nid_t     pi_nid[0];
439 } WIRE_ATTR lnet_ping_info_t;
440
441 /* Options for lnet_portal_t::ptl_options */
442 #define LNET_PTL_LAZY               (1 << 0)
443 typedef struct {
444         struct list_head ptl_ml;  /* match list */
445         struct list_head ptl_msgq; /* messages blocking for MD */
446         __u64            ptl_msgq_version;  /* validity stamp */
447         unsigned int     ptl_options;
448 } lnet_portal_t;
449
450 /* Router Checker */
451 /*                               < 0 == startup error */
452 #define LNET_RC_STATE_SHUTDOWN     0            /* not started */
453 #define LNET_RC_STATE_RUNNING      1            /* started up OK */
454 #define LNET_RC_STATE_STOPTHREAD   2            /* telling thread to stop */
455 #define LNET_RC_STATE_UNLINKING    3            /* unlinking RC MD */
456 #define LNET_RC_STATE_UNLINKED     4            /* RC's MD has been unlinked */
457
458 typedef struct
459 {
460         /* Stuff initialised at LNetInit() */
461         int                ln_init;             /* LNetInit() called? */
462         int                ln_refcount;         /* LNetNIInit/LNetNIFini counter */
463         int                ln_niinit_self;      /* Have I called LNetNIInit myself? */
464
465         int                ln_ptlcompat;        /* do I support talking to portals? */
466         
467         struct list_head   ln_lnds;             /* registered LNDs */
468
469 #ifdef __KERNEL__
470         spinlock_t         ln_lock;
471         cfs_waitq_t        ln_waitq;
472         struct semaphore   ln_api_mutex;
473         struct semaphore   ln_lnd_mutex;
474 #else
475 # ifndef HAVE_LIBPTHREAD
476         int                ln_lock;
477         int                ln_api_mutex;
478         int                ln_lnd_mutex;
479 # else
480         pthread_cond_t     ln_cond;
481         pthread_mutex_t    ln_lock;
482         pthread_mutex_t    ln_api_mutex;
483         pthread_mutex_t    ln_lnd_mutex;
484 # endif
485 #endif
486
487         /* Stuff initialised at LNetNIInit() */
488
489         int                ln_shutdown;         /* shutdown in progress */
490         int                ln_nportals;         /* # portals */
491         lnet_portal_t     *ln_portals;          /* the vector of portals */
492
493         lnet_pid_t         ln_pid;              /* requested pid */
494
495         struct list_head   ln_nis;              /* LND instances */
496         lnet_ni_t         *ln_loni;             /* the loopback NI */
497         lnet_ni_t         *ln_eqwaitni;         /* NI to wait for events in */
498         struct list_head   ln_zombie_nis;       /* dying LND instances */
499         int                ln_nzombie_nis;      /* # of NIs to wait for */
500
501         struct list_head   ln_remote_nets;      /* remote networks with routes to them */
502         __u64              ln_remote_nets_version; /* validity stamp */
503
504         struct list_head   ln_routers;          /* list of all known routers */
505         __u64              ln_routers_version;  /* validity stamp */
506
507         struct list_head  *ln_peer_hash;        /* NID->peer hash */
508         int                ln_npeers;           /* # peers extant */
509         int                ln_peertable_version; /* /proc validity stamp */
510         
511         int                ln_routing;          /* am I a router? */
512         lnet_rtrbufpool_t  ln_rtrpools[LNET_NRBPOOLS]; /* router buffer pools */
513         
514         int                ln_lh_hash_size;     /* size of lib handle hash table */
515         struct list_head  *ln_lh_hash_table;    /* all extant lib handles, this interface */
516         __u64              ln_next_object_cookie; /* cookie generator */
517         __u64              ln_interface_cookie; /* uniquely identifies this ni in this epoch */
518
519         char              *ln_network_tokens;   /* space for network names */
520         int                ln_network_tokens_nob;
521
522         int                ln_testprotocompat;  /* test protocol compatibility flags */
523
524         struct list_head   ln_finalizeq;        /* msgs waiting to complete finalizing */
525 #ifdef __KERNEL__
526         void             **ln_finalizers;       /* threads doing finalization */
527         int                ln_nfinalizers;      /* max # threads finalizing */
528 #else
529         int                ln_finalizing;
530 #endif
531         struct list_head   ln_test_peers;       /* failure simulation */
532
533         lnet_handle_md_t   ln_ping_target_md;
534         lnet_handle_eq_t   ln_ping_target_eq;
535         lnet_ping_info_t  *ln_ping_info;
536
537 #ifdef __KERNEL__
538         int                ln_rc_state;         /* router checker startup/shutdown state */
539         struct semaphore   ln_rc_signal;        /* serialise startup/shutdown */
540         lnet_handle_eq_t   ln_rc_eqh;           /* router checker's event queue */
541 #endif
542         
543 #ifdef LNET_USE_LIB_FREELIST
544         lnet_freelist_t    ln_free_mes;
545         lnet_freelist_t    ln_free_msgs;
546         lnet_freelist_t    ln_free_mds;
547         lnet_freelist_t    ln_free_eqs;
548 #endif
549         struct list_head   ln_active_msgs;
550         struct list_head   ln_active_mds;
551         struct list_head   ln_active_eqs;
552
553         lnet_counters_t    ln_counters;
554
555 #ifndef __KERNEL__
556         /* Temporary workaround to allow uOSS and test programs force
557          * server mode in userspace. The only place where we use it is
558          * lnet_prepare(). The only way to turn this flag on is to
559          * call lnet_server_mode() */
560
561         int                ln_server_mode_flag;
562 #endif        
563 } lnet_t;
564
565 #endif