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