Whamcloud - gitweb
2227c6a9a65187a6e0d6c4b98ad326151fc25f91
[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 #endif
336 } lnd_t;
337
338 #define LNET_MAX_INTERFACES   16
339
340 typedef struct lnet_ni {
341         struct list_head  ni_list;              /* chain on ln_nis */
342         struct list_head  ni_txq;               /* messages waiting for tx credits */
343         int               ni_maxtxcredits;      /* # tx credits  */
344         int               ni_txcredits;         /* # tx credits free */
345         int               ni_mintxcredits;      /* lowest it's been */
346         int               ni_peertxcredits;     /* # per-peer send credits */
347         lnet_nid_t        ni_nid;               /* interface's NID */
348         void             *ni_data;              /* instance-specific data */
349         lnd_t            *ni_lnd;               /* procedural interface */
350         int               ni_refcount;          /* reference count */
351         char             *ni_interfaces[LNET_MAX_INTERFACES]; /* equivalent interfaces to use */
352 } lnet_ni_t;
353
354 typedef struct lnet_peer {
355         struct list_head  lp_hashlist;          /* chain on peer hash */
356         struct list_head  lp_txq;               /* messages blocking for tx credits */
357         struct list_head  lp_rtrq;              /* messages blocking for router credits */
358         struct list_head  lp_rtr_list;          /* chain on router list */
359         int               lp_txcredits;         /* # tx credits available */
360         int               lp_mintxcredits;      /* low water mark */
361         int               lp_rtrcredits;        /* # router credits */
362         int               lp_minrtrcredits;     /* low water mark */
363         unsigned int      lp_alive:1;           /* alive/dead? */
364         unsigned int      lp_notify:1;          /* notification outstanding? */
365         unsigned int      lp_notifylnd:1;       /* outstanding notification for LND? */
366         unsigned int      lp_notifying:1;       /* some thread is handling notification */
367         unsigned int      lp_ping_notsent;      /* SEND event outstanding from ping */
368         int               lp_alive_count;       /* # times router went dead<->alive */
369         long              lp_txqnob;            /* bytes queued for sending */
370         time_t            lp_timestamp;         /* time of last aliveness news */
371         time_t            lp_ping_timestamp;    /* time of last ping attempt */
372         time_t            lp_ping_deadline;     /* != 0 if ping reply expected */
373         lnet_ni_t        *lp_ni;                /* interface peer is on */
374         lnet_nid_t        lp_nid;               /* peer's NID */
375         int               lp_refcount;          /* # refs */
376         int               lp_rtr_refcount;      /* # refs from lnet_route_t::lr_gateway */
377 } lnet_peer_t;
378
379 typedef struct {
380         struct list_head  lr_list;              /* chain on net */
381         lnet_peer_t      *lr_gateway;           /* router node */
382 } lnet_route_t;
383
384 typedef struct {
385         struct list_head        lrn_list;       /* chain on ln_remote_nets */
386         struct list_head        lrn_routes;     /* routes to me */
387         __u32                   lrn_net;        /* my net number */
388         unsigned int            lrn_hops;       /* how far I am */
389 } lnet_remotenet_t;
390
391 typedef struct {
392         struct list_head  rbp_bufs;             /* my free buffer pool */
393         struct list_head  rbp_msgs;             /* messages blocking for a buffer */
394         int               rbp_npages;           /* # pages in each buffer */
395         int               rbp_nbuffers;         /* # buffers */
396         int               rbp_credits;          /* # free buffers / blocked messages */
397         int               rbp_mincredits;       /* low water mark */
398 } lnet_rtrbufpool_t;
399
400 typedef struct {
401         struct list_head   rb_list;             /* chain on rbp_bufs */
402         lnet_rtrbufpool_t *rb_pool;             /* owning pool */
403         lnet_kiov_t        rb_kiov[0];          /* the buffer space */
404 } lnet_rtrbuf_t;
405
406 typedef struct {
407         __u32        msgs_alloc;
408         __u32        msgs_max;
409         __u32        errors;
410         __u32        send_count;
411         __u32        recv_count;
412         __u32        route_count;
413         __u32        drop_count;
414         __u64        send_length;
415         __u64        recv_length;
416         __u64        route_length;
417         __u64        drop_length;
418 } lnet_counters_t;
419
420 #define LNET_PEER_HASHSIZE   503                /* prime! */
421
422 #define LNET_NRBPOOLS         3                 /* # different router buffer pools */
423
424 #define LNET_PROTO_PING_MATCHBITS     0x8000000000000000LL
425 #define LNET_PROTO_PING_VERSION       1
426 typedef struct {
427         __u32          pi_magic;
428         __u32          pi_version;
429         lnet_pid_t     pi_pid;
430         __u32          pi_nnids;
431         lnet_nid_t     pi_nid[0];
432 } WIRE_ATTR lnet_ping_info_t;
433
434 /* Options for lnet_portal_t::ptl_options */
435 #define LNET_PTL_LAZY               (1 << 0)
436 typedef struct {
437         struct list_head ptl_ml;  /* match list */
438         struct list_head ptl_msgq; /* messages blocking for MD */
439         __u64            ptl_msgq_version;  /* validity stamp */
440         unsigned int     ptl_options;
441 } lnet_portal_t;
442
443 /* Router Checker */
444 /*                               < 0 == startup error */
445 #define LNET_RC_STATE_SHUTDOWN     0            /* not started */
446 #define LNET_RC_STATE_RUNNING      1            /* started up OK */
447 #define LNET_RC_STATE_STOPTHREAD   2            /* telling thread to stop */
448 #define LNET_RC_STATE_UNLINKING    3            /* unlinking RC MD */
449 #define LNET_RC_STATE_UNLINKED     4            /* RC's MD has been unlinked */
450
451 typedef struct
452 {
453         /* Stuff initialised at LNetInit() */
454         int                ln_init;             /* LNetInit() called? */
455         int                ln_refcount;         /* LNetNIInit/LNetNIFini counter */
456         int                ln_niinit_self;      /* Have I called LNetNIInit myself? */
457
458         int                ln_ptlcompat;        /* do I support talking to portals? */
459         
460         struct list_head   ln_lnds;             /* registered LNDs */
461
462 #ifdef __KERNEL__
463         spinlock_t         ln_lock;
464         cfs_waitq_t        ln_waitq;
465         struct semaphore   ln_api_mutex;
466         struct semaphore   ln_lnd_mutex;
467 #else
468 # if !HAVE_LIBPTHREAD
469         int                ln_lock;
470         int                ln_api_mutex;
471         int                ln_lnd_mutex;
472 # else
473         pthread_cond_t     ln_cond;
474         pthread_mutex_t    ln_lock;
475         pthread_mutex_t    ln_api_mutex;
476         pthread_mutex_t    ln_lnd_mutex;
477 # endif
478 #endif
479
480         /* Stuff initialised at LNetNIInit() */
481
482         int                ln_shutdown;         /* shutdown in progress */
483         int                ln_nportals;         /* # portals */
484         lnet_portal_t     *ln_portals;          /* the vector of portals */
485
486         lnet_pid_t         ln_pid;              /* requested pid */
487
488         struct list_head   ln_nis;              /* LND instances */
489         lnet_ni_t         *ln_loni;             /* the loopback NI */
490         lnet_ni_t         *ln_eqwaitni;         /* NI to wait for events in */
491         struct list_head   ln_zombie_nis;       /* dying LND instances */
492         int                ln_nzombie_nis;      /* # of NIs to wait for */
493
494         struct list_head   ln_remote_nets;      /* remote networks with routes to them */
495         __u64              ln_remote_nets_version; /* validity stamp */
496
497         struct list_head   ln_routers;          /* list of all known routers */
498         __u64              ln_routers_version;  /* validity stamp */
499
500         struct list_head  *ln_peer_hash;        /* NID->peer hash */
501         int                ln_npeers;           /* # peers extant */
502         int                ln_peertable_version; /* /proc validity stamp */
503         
504         int                ln_routing;          /* am I a router? */
505         lnet_rtrbufpool_t  ln_rtrpools[LNET_NRBPOOLS]; /* router buffer pools */
506         
507         int                ln_lh_hash_size;     /* size of lib handle hash table */
508         struct list_head  *ln_lh_hash_table;    /* all extant lib handles, this interface */
509         __u64              ln_next_object_cookie; /* cookie generator */
510         __u64              ln_interface_cookie; /* uniquely identifies this ni in this epoch */
511
512         char              *ln_network_tokens;   /* space for network names */
513         int                ln_network_tokens_nob;
514
515         int                ln_testprotocompat;  /* test protocol compatibility flags */
516
517         struct list_head   ln_finalizeq;        /* msgs waiting to complete finalizing */
518 #ifdef __KERNEL__
519         void             **ln_finalizers;       /* threads doing finalization */
520         int                ln_nfinalizers;      /* max # threads finalizing */
521 #else
522         int                ln_finalizing;
523 #endif
524         struct list_head   ln_test_peers;       /* failure simulation */
525
526         lnet_handle_md_t   ln_ping_target_md;
527         lnet_handle_eq_t   ln_ping_target_eq;
528         lnet_ping_info_t  *ln_ping_info;
529
530 #ifdef __KERNEL__
531         int                ln_rc_state;         /* router checker startup/shutdown state */
532         struct semaphore   ln_rc_signal;        /* serialise startup/shutdown */
533         lnet_handle_eq_t   ln_rc_eqh;           /* router checker's event queue */
534 #endif
535         
536 #ifdef LNET_USE_LIB_FREELIST
537         lnet_freelist_t    ln_free_mes;
538         lnet_freelist_t    ln_free_msgs;
539         lnet_freelist_t    ln_free_mds;
540         lnet_freelist_t    ln_free_eqs;
541 #endif
542         struct list_head   ln_active_msgs;
543         struct list_head   ln_active_mds;
544         struct list_head   ln_active_eqs;
545
546         lnet_counters_t    ln_counters;
547 } lnet_t;
548
549 #endif