Whamcloud - gitweb
LU-7734 lnet: implement Peer Discovery
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lnet/include/lnet/lib-types.h
33  *
34  * Types used by the library side routines that do not need to be
35  * exposed to the user application
36  */
37
38 #ifndef __LNET_LIB_TYPES_H__
39 #define __LNET_LIB_TYPES_H__
40
41 #ifndef __KERNEL__
42 # error This include is only for kernel use.
43 #endif
44
45 #include <linux/kthread.h>
46 #include <linux/uio.h>
47 #include <linux/types.h>
48
49 #include <lnet/lnetctl.h>
50
51 /* Max payload size */
52 #ifndef CONFIG_LNET_MAX_PAYLOAD
53 # error "CONFIG_LNET_MAX_PAYLOAD must be defined in config.h"
54 #endif
55
56 #define LNET_MAX_PAYLOAD       CONFIG_LNET_MAX_PAYLOAD
57 #if (LNET_MAX_PAYLOAD < LNET_MTU)
58 # error "LNET_MAX_PAYLOAD too small - error in configure --with-max-payload-mb"
59 #elif (LNET_MAX_PAYLOAD > (PAGE_SIZE * LNET_MAX_IOV))
60 # error "LNET_MAX_PAYLOAD too large - error in configure --with-max-payload-mb"
61 #endif
62
63 /* forward refs */
64 struct lnet_libmd;
65
66 typedef struct lnet_msg {
67         struct list_head        msg_activelist;
68         struct list_head        msg_list;       /* Q for credits/MD */
69
70         lnet_process_id_t       msg_target;
71         /* Primary NID of the source. */
72         lnet_nid_t              msg_initiator;
73         /* where is it from, it's only for building event */
74         lnet_nid_t              msg_from;
75         __u32                   msg_type;
76
77         /* committed for sending */
78         unsigned int            msg_tx_committed:1;
79         /* CPT # this message committed for sending */
80         unsigned int            msg_tx_cpt:15;
81         /* committed for receiving */
82         unsigned int            msg_rx_committed:1;
83         /* CPT # this message committed for receiving */
84         unsigned int            msg_rx_cpt:15;
85         /* queued for tx credit */
86         unsigned int            msg_tx_delayed:1;
87         /* queued for RX buffer */
88         unsigned int            msg_rx_delayed:1;
89         /* ready for pending on RX delay list */
90         unsigned int            msg_rx_ready_delay:1;
91
92         unsigned int          msg_vmflush:1;      /* VM trying to free memory */
93         unsigned int          msg_target_is_router:1; /* sending to a router */
94         unsigned int          msg_routing:1;      /* being forwarded */
95         unsigned int          msg_ack:1;          /* ack on finalize (PUT) */
96         unsigned int          msg_sending:1;      /* outgoing message */
97         unsigned int          msg_receiving:1;    /* being received */
98         unsigned int          msg_txcredit:1;     /* taken an NI send credit */
99         unsigned int          msg_peertxcredit:1; /* taken a peer send credit */
100         unsigned int          msg_rtrcredit:1;    /* taken a globel router credit */
101         unsigned int          msg_peerrtrcredit:1; /* taken a peer router credit */
102         unsigned int          msg_onactivelist:1; /* on the activelist */
103         unsigned int          msg_rdma_get:1;
104
105         struct lnet_peer_ni  *msg_txpeer;         /* peer I'm sending to */
106         struct lnet_peer_ni  *msg_rxpeer;         /* peer I received from */
107
108         void                 *msg_private;
109         struct lnet_libmd    *msg_md;
110         /* the NI the message was sent or received over */
111         struct lnet_ni       *msg_txni;
112         struct lnet_ni       *msg_rxni;
113
114         unsigned int          msg_len;
115         unsigned int          msg_wanted;
116         unsigned int          msg_offset;
117         unsigned int          msg_niov;
118         struct kvec          *msg_iov;
119         lnet_kiov_t          *msg_kiov;
120
121         lnet_event_t          msg_ev;
122         lnet_hdr_t            msg_hdr;
123 } lnet_msg_t;
124
125
126 typedef struct lnet_libhandle {
127         struct list_head        lh_hash_chain;
128         __u64                   lh_cookie;
129 } lnet_libhandle_t;
130
131 #define lh_entry(ptr, type, member) \
132         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
133
134 typedef struct lnet_eq {
135         struct list_head        eq_list;
136         lnet_libhandle_t        eq_lh;
137         lnet_seq_t              eq_enq_seq;
138         lnet_seq_t              eq_deq_seq;
139         unsigned int            eq_size;
140         lnet_eq_handler_t       eq_callback;
141         lnet_event_t            *eq_events;
142         int                     **eq_refs;      /* percpt refcount for EQ */
143 } lnet_eq_t;
144
145 typedef struct lnet_me {
146         struct list_head        me_list;
147         lnet_libhandle_t        me_lh;
148         lnet_process_id_t       me_match_id;
149         unsigned int            me_portal;
150         unsigned int            me_pos;         /* hash offset in mt_hash */
151         __u64                   me_match_bits;
152         __u64                   me_ignore_bits;
153         lnet_unlink_t           me_unlink;
154         struct lnet_libmd      *me_md;
155 } lnet_me_t;
156
157 typedef struct lnet_libmd {
158         struct list_head        md_list;
159         lnet_libhandle_t        md_lh;
160         lnet_me_t              *md_me;
161         char                   *md_start;
162         unsigned int            md_offset;
163         unsigned int            md_length;
164         unsigned int            md_max_size;
165         int                     md_threshold;
166         int                     md_refcount;
167         unsigned int            md_options;
168         unsigned int            md_flags;
169         unsigned int            md_niov;        /* # frags at end of struct */
170         void                   *md_user_ptr;
171         lnet_eq_t              *md_eq;
172         lnet_handle_md_t       md_bulk_handle;
173         union {
174                 struct kvec     iov[LNET_MAX_IOV];
175                 lnet_kiov_t     kiov[LNET_MAX_IOV];
176         } md_iov;
177 } lnet_libmd_t;
178
179 #define LNET_MD_FLAG_ZOMBIE      (1 << 0)
180 #define LNET_MD_FLAG_AUTO_UNLINK (1 << 1)
181 #define LNET_MD_FLAG_ABORTED     (1 << 2)
182
183 typedef struct {
184         /* info about peers we are trying to fail */
185         struct list_head        tp_list;        /* ln_test_peers */
186         lnet_nid_t              tp_nid;         /* matching nid */
187         unsigned int            tp_threshold;   /* # failures to simulate */
188 } lnet_test_peer_t;
189
190 #define LNET_COOKIE_TYPE_MD    1
191 #define LNET_COOKIE_TYPE_ME    2
192 #define LNET_COOKIE_TYPE_EQ    3
193 #define LNET_COOKIE_TYPE_BITS  2
194 #define LNET_COOKIE_MASK        ((1ULL << LNET_COOKIE_TYPE_BITS) - 1ULL)
195
196 struct lnet_ni;                                  /* forward ref */
197 struct socket;
198
199 typedef struct lnet_lnd
200 {
201         /* fields managed by portals */
202         struct list_head        lnd_list;       /* stash in the LND table */
203         int                     lnd_refcount;   /* # active instances */
204
205         /* fields initialized by the LND */
206         __u32                   lnd_type;
207
208         int  (*lnd_startup)(struct lnet_ni *ni);
209         void (*lnd_shutdown)(struct lnet_ni *ni);
210         int  (*lnd_ctl)(struct lnet_ni *ni, unsigned int cmd, void *arg);
211
212         /* In data movement APIs below, payload buffers are described as a set
213          * of 'niov' fragments which are...
214          * EITHER
215          *    in virtual memory (struct kvec *iov != NULL)
216          * OR
217          *    in pages (kernel only: plt_kiov_t *kiov != NULL).
218          * The LND may NOT overwrite these fragment descriptors.
219          * An 'offset' and may specify a byte offset within the set of
220          * fragments to start from
221          */
222
223         /* Start sending a preformatted message.  'private' is NULL for PUT and
224          * GET messages; otherwise this is a response to an incoming message
225          * and 'private' is the 'private' passed to lnet_parse().  Return
226          * non-zero for immediate failure, otherwise complete later with
227          * lnet_finalize() */
228         int (*lnd_send)(struct lnet_ni *ni, void *private, lnet_msg_t *msg);
229
230         /* Start receiving 'mlen' bytes of payload data, skipping the following
231          * 'rlen' - 'mlen' bytes. 'private' is the 'private' passed to
232          * lnet_parse().  Return non-zero for immedaite failure, otherwise
233          * complete later with lnet_finalize().  This also gives back a receive
234          * credit if the LND does flow control. */
235         int (*lnd_recv)(struct lnet_ni *ni, void *private, lnet_msg_t *msg,
236                         int delayed, unsigned int niov,
237                         struct kvec *iov, lnet_kiov_t *kiov,
238                         unsigned int offset, unsigned int mlen, unsigned int rlen);
239
240         /* lnet_parse() has had to delay processing of this message
241          * (e.g. waiting for a forwarding buffer or send credits).  Give the
242          * LND a chance to free urgently needed resources.  If called, return 0
243          * for success and do NOT give back a receive credit; that has to wait
244          * until lnd_recv() gets called.  On failure return < 0 and
245          * release resources; lnd_recv() will not be called. */
246         int (*lnd_eager_recv)(struct lnet_ni *ni, void *private, lnet_msg_t *msg,
247                               void **new_privatep);
248
249         /* notification of peer health */
250         void (*lnd_notify)(struct lnet_ni *ni, lnet_nid_t peer, int alive);
251
252         /* query of peer aliveness */
253         void (*lnd_query)(struct lnet_ni *ni, lnet_nid_t peer, cfs_time_t *when);
254
255         /* accept a new connection */
256         int (*lnd_accept)(struct lnet_ni *ni, struct socket *sock);
257 } lnd_t;
258
259 struct lnet_tx_queue {
260         int                     tq_credits;     /* # tx credits free */
261         int                     tq_credits_min; /* lowest it's been */
262         int                     tq_credits_max; /* total # tx credits */
263         struct list_head        tq_delayed;     /* delayed TXs */
264 };
265
266 enum lnet_net_state {
267         /* set when net block is allocated */
268         LNET_NET_STATE_INIT = 0,
269         /* set when NIs in net are started successfully */
270         LNET_NET_STATE_ACTIVE,
271         /* set if all NIs in net are in FAILED state */
272         LNET_NET_STATE_INACTIVE,
273         /* set when shutting down a NET */
274         LNET_NET_STATE_DELETING
275 };
276
277 enum lnet_ni_state {
278         /* set when NI block is allocated */
279         LNET_NI_STATE_INIT = 0,
280         /* set when NI is started successfully */
281         LNET_NI_STATE_ACTIVE,
282         /* set when LND notifies NI failed */
283         LNET_NI_STATE_FAILED,
284         /* set when LND notifies NI degraded */
285         LNET_NI_STATE_DEGRADED,
286         /* set when shuttding down NI */
287         LNET_NI_STATE_DELETING
288 };
289
290 struct lnet_element_stats {
291         atomic_t        send_count;
292         atomic_t        recv_count;
293         atomic_t        drop_count;
294 };
295
296 struct lnet_net {
297         /* chain on the ln_nets */
298         struct list_head        net_list;
299
300         /* net ID, which is composed of
301          * (net_type << 16) | net_num.
302          * net_type can be one of the enumerated types defined in
303          * lnet/include/lnet/nidstr.h */
304         __u32                   net_id;
305
306         /* priority of the network */
307         __u32                   net_prio;
308
309         /* total number of CPTs in the array */
310         __u32                   net_ncpts;
311
312         /* cumulative CPTs of all NIs in this net */
313         __u32                   *net_cpts;
314
315         /* network tunables */
316         struct lnet_ioctl_config_lnd_cmn_tunables net_tunables;
317
318         /*
319          * boolean to indicate that the tunables have been set and
320          * shouldn't be reset
321          */
322         bool                    net_tunables_set;
323
324         /* procedural interface */
325         lnd_t                   *net_lnd;
326
327         /* list of NIs on this net */
328         struct list_head        net_ni_list;
329
330         /* list of NIs being added, but not started yet */
331         struct list_head        net_ni_added;
332
333         /* dying LND instances */
334         struct list_head        net_ni_zombie;
335
336         /* network state */
337         enum lnet_net_state     net_state;
338 };
339
340 typedef struct lnet_ni {
341         /* chain on the lnet_net structure */
342         struct list_head        ni_netlist;
343
344         /* chain on net_ni_cpt */
345         struct list_head        ni_cptlist;
346
347         spinlock_t              ni_lock;
348
349         /* number of CPTs */
350         int                     ni_ncpts;
351
352         /* bond NI on some CPTs */
353         __u32                   *ni_cpts;
354
355         /* interface's NID */
356         lnet_nid_t              ni_nid;
357
358         /* instance-specific data */
359         void                    *ni_data;
360
361         /* per ni credits */
362         atomic_t                ni_tx_credits;
363
364         /* percpt TX queues */
365         struct lnet_tx_queue    **ni_tx_queues;
366
367         /* percpt reference count */
368         int                     **ni_refs;
369
370         /* when I was last alive */
371         long                    ni_last_alive;
372
373         /* pointer to parent network */
374         struct lnet_net         *ni_net;
375
376         /* my health status */
377         lnet_ni_status_t        *ni_status;
378
379         /* NI FSM */
380         enum lnet_ni_state      ni_state;
381
382         /* per NI LND tunables */
383         struct lnet_lnd_tunables ni_lnd_tunables;
384
385         /* lnd tunables set explicitly */
386         bool ni_lnd_tunables_set;
387
388         /* NI statistics */
389         struct lnet_element_stats ni_stats;
390
391         /* physical device CPT */
392         int                     ni_dev_cpt;
393
394         /* sequence number used to round robin over nis within a net */
395         __u32                   ni_seq;
396
397         /*
398          * equivalent interfaces to use
399          * This is an array because socklnd bonding can still be configured
400          */
401         char                    *ni_interfaces[LNET_NUM_INTERFACES];
402         struct net              *ni_net_ns;     /* original net namespace */
403 } lnet_ni_t;
404
405 #define LNET_PROTO_PING_MATCHBITS       0x8000000000000000LL
406
407 /*
408  * Descriptor of a ping info buffer: keep a separate indicator of the
409  * size and a reference count. The type is used both as a source and
410  * sink of data, so we need to keep some information outside of the
411  * area that may be overwritten by network data.
412  */
413 struct lnet_ping_buffer {
414         int                     pb_nnis;
415         atomic_t                pb_refcnt;
416         struct lnet_ping_info   pb_info;
417 };
418
419 #define LNET_PING_BUFFER_SIZE(NNIDS) \
420         offsetof(struct lnet_ping_buffer, pb_info.pi_ni[NNIDS])
421 #define LNET_PING_BUFFER_LONI(PBUF)     ((PBUF)->pb_info.pi_ni[0].ns_nid)
422 #define LNET_PING_BUFFER_SEQNO(PBUF)    ((PBUF)->pb_info.pi_ni[0].ns_status)
423
424 #define LNET_PING_INFO_TO_BUFFER(PINFO) \
425         container_of((PINFO), struct lnet_ping_buffer, pb_info)
426
427 /* router checker data, per router */
428 typedef struct {
429         /* chain on the_lnet.ln_zombie_rcd or ln_deathrow_rcd */
430         struct list_head        rcd_list;
431         lnet_handle_md_t        rcd_mdh;        /* ping buffer MD */
432         struct lnet_peer_ni     *rcd_gateway;   /* reference to gateway */
433         struct lnet_ping_buffer *rcd_pingbuffer;/* ping buffer */
434         int                     rcd_nnis;       /* desired size of buffer */
435 } lnet_rc_data_t;
436
437 struct lnet_peer_ni {
438         /* chain on lpn_peer_nis */
439         struct list_head        lpni_peer_nis;
440         /* chain on remote peer list */
441         struct list_head        lpni_on_remote_peer_ni_list;
442         /* chain on peer hash */
443         struct list_head        lpni_hashlist;
444         /* messages blocking for tx credits */
445         struct list_head        lpni_txq;
446         /* messages blocking for router credits */
447         struct list_head        lpni_rtrq;
448         /* chain on router list */
449         struct list_head        lpni_rtr_list;
450         /* pointer to peer net I'm part of */
451         struct lnet_peer_net    *lpni_peer_net;
452         /* statistics kept on each peer NI */
453         struct lnet_element_stats lpni_stats;
454         /* spin lock protecting credits and lpni_txq / lpni_rtrq */
455         spinlock_t              lpni_lock;
456         /* # tx credits available */
457         int                     lpni_txcredits;
458         /* low water mark */
459         int                     lpni_mintxcredits;
460         /* # router credits */
461         int                     lpni_rtrcredits;
462         /* low water mark */
463         int                     lpni_minrtrcredits;
464         /* bytes queued for sending */
465         long                    lpni_txqnob;
466         /* alive/dead? */
467         bool                    lpni_alive;
468         /* notification outstanding? */
469         bool                    lpni_notify;
470         /* outstanding notification for LND? */
471         bool                    lpni_notifylnd;
472         /* some thread is handling notification */
473         bool                    lpni_notifying;
474         /* SEND event outstanding from ping */
475         bool                    lpni_ping_notsent;
476         /* # times router went dead<->alive. Protected with lpni_lock */
477         int                     lpni_alive_count;
478         /* time of last aliveness news */
479         cfs_time_t              lpni_timestamp;
480         /* time of last ping attempt */
481         cfs_time_t              lpni_ping_timestamp;
482         /* != 0 if ping reply expected */
483         cfs_time_t              lpni_ping_deadline;
484         /* when I was last alive */
485         cfs_time_t              lpni_last_alive;
486         /* when lpni_ni was queried last time */
487         cfs_time_t              lpni_last_query;
488         /* network peer is on */
489         struct lnet_net         *lpni_net;
490         /* peer's NID */
491         lnet_nid_t              lpni_nid;
492         /* # refs */
493         atomic_t                lpni_refcount;
494         /* CPT this peer attached on */
495         int                     lpni_cpt;
496         /* state flags -- protected by lpni_lock */
497         unsigned                lpni_state;
498         /* # refs from lnet_route_t::lr_gateway */
499         int                     lpni_rtr_refcount;
500         /* sequence number used to round robin over peer nis within a net */
501         __u32                   lpni_seq;
502         /* sequence number used to round robin over gateways */
503         __u32                   lpni_gw_seq;
504         /* health flag */
505         bool                    lpni_healthy;
506         /* returned RC ping features. Protected with lpni_lock */
507         unsigned int            lpni_ping_feats;
508         /* routes on this peer */
509         struct list_head        lpni_routes;
510         /* preferred local nids: if only one, use lpni_pref.nid */
511         union lpni_pref {
512                 lnet_nid_t      nid;
513                 lnet_nid_t      *nids;
514         } lpni_pref;
515         /* number of preferred NIDs in lnpi_pref_nids */
516         __u32                   lpni_pref_nnids;
517         /* router checker state */
518         lnet_rc_data_t          *lpni_rcd;
519 };
520
521 /* Preferred path added due to traffic on non-MR peer_ni */
522 #define LNET_PEER_NI_NON_MR_PREF        (1 << 0)
523
524 struct lnet_peer {
525         /* chain on pt_peer_list */
526         struct list_head        lp_peer_list;
527
528         /* list of peer nets */
529         struct list_head        lp_peer_nets;
530
531         /* primary NID of the peer */
532         lnet_nid_t              lp_primary_nid;
533
534         /* CPT of peer_table */
535         int                     lp_cpt;
536
537         /* number of NIDs on this peer */
538         int                     lp_nnis;
539
540         /* reference count */
541         atomic_t                lp_refcount;
542
543         /* lock protecting peer state flags */
544         spinlock_t              lp_lock;
545
546         /* peer state flags */
547         unsigned                lp_state;
548
549         /* buffer for data pushed by peer */
550         struct lnet_ping_buffer *lp_data;
551
552         /* MD handle for ping in progress */
553         lnet_handle_md_t        lp_ping_mdh;
554
555         /* MD handle for push in progress */
556         lnet_handle_md_t        lp_push_mdh;
557
558         /* number of NIDs for sizing push data */
559         int                     lp_data_nnis;
560
561         /* NI config sequence number of peer */
562         __u32                   lp_peer_seqno;
563
564         /* Local NI config sequence number acked by peer */
565         __u32                   lp_node_seqno;
566
567         /* Local NI config sequence number sent to peer */
568         __u32                   lp_node_seqno_sent;
569
570         /* Ping error encountered during discovery. */
571         int                     lp_ping_error;
572
573         /* Push error encountered during discovery. */
574         int                     lp_push_error;
575
576         /* Error encountered during discovery. */
577         int                     lp_dc_error;
578
579         /* link on discovery-related lists */
580         struct list_head        lp_dc_list;
581
582         /* tasks waiting on discovery of this peer */
583         wait_queue_head_t       lp_dc_waitq;
584 };
585
586 #define LNET_PEER_MULTI_RAIL    (1 << 0)        /* Multi-rail aware */
587
588 /*
589  * A peer can be CONFIGURED by DLC, DISCOVERED by peer discovery, or
590  * UNDISCOVERED if discovery was disabled. The UNDISCOVERED state
591  * exists to keep a peer from being requeued to discovery.
592  *
593  * A peer that was created as the result of inbound traffic will not
594  * be marked at all.
595  */
596 #define LNET_PEER_CONFIGURED    (1 << 1)        /* Configured via DLC */
597 #define LNET_PEER_DISCOVERED    (1 << 2)        /* Peer was discovered */
598 #define LNET_PEER_UNDISCOVERED  (1 << 3)        /* Discovery was disabled */
599
600 #define LNET_PEER_QUEUED        (1 << 4)        /* Queued for discovery */
601 #define LNET_PEER_DISCOVERING   (1 << 5)        /* Discovering */
602 #define LNET_PEER_DATA_PRESENT  (1 << 6)        /* Remote peer data present */
603 #define LNET_PEER_NIDS_UPTODATE (1 << 7)        /* Remote peer info uptodate */
604 #define LNET_PEER_PING_SENT     (1 << 8)        /* Waiting for REPLY to Ping */
605 #define LNET_PEER_PUSH_SENT     (1 << 9)        /* Waiting for ACK of Push */
606 #define LNET_PEER_PING_FAILED   (1 << 10)       /* Ping send failure */
607 #define LNET_PEER_PUSH_FAILED   (1 << 11)       /* Push send failure */
608 #define LNET_PEER_PING_REQUIRED (1 << 12)       /* Must ping to fix up state */
609
610 struct lnet_peer_net {
611         /* chain on lp_peer_nets */
612         struct list_head        lpn_peer_nets;
613
614         /* list of peer_nis on this network */
615         struct list_head        lpn_peer_nis;
616
617         /* pointer to the peer I'm part of */
618         struct lnet_peer        *lpn_peer;
619
620         /* Net ID */
621         __u32                   lpn_net_id;
622
623         /* reference count */
624         atomic_t                lpn_refcount;
625 };
626
627 /* peer hash size */
628 #define LNET_PEER_HASH_BITS     9
629 #define LNET_PEER_HASH_SIZE     (1 << LNET_PEER_HASH_BITS)
630
631 /*
632  * peer hash table - one per CPT
633  *
634  * protected by lnet_net_lock/EX for update
635  *    pt_version
636  *    pt_number
637  *    pt_hash[...]
638  *    pt_peer_list
639  *    pt_peers
640  *    pt_peer_nnids
641  * protected by pt_zombie_lock:
642  *    pt_zombie_list
643  *    pt_zombies
644  *
645  * pt_zombie lock nests inside lnet_net_lock
646  */
647 struct lnet_peer_table {
648         int                     pt_version;     /* /proc validity stamp */
649         int                     pt_number;      /* # peers_ni extant */
650         struct list_head        *pt_hash;       /* NID->peer hash */
651         struct list_head        pt_peer_list;   /* peers */
652         int                     pt_peers;       /* # peers */
653         int                     pt_peer_nnids;  /* # NIDS on listed peers */
654         struct list_head        pt_zombie_list; /* zombie peer_ni */
655         int                     pt_zombies;     /* # zombie peers_ni */
656         spinlock_t              pt_zombie_lock; /* protect list and count */
657 };
658
659 /* peer aliveness is enabled only on routers for peers in a network where the
660  * lnet_ni_t::ni_peertimeout has been set to a positive value */
661 #define lnet_peer_aliveness_enabled(lp) (the_lnet.ln_routing != 0 && \
662                                         ((lp)->lpni_net) && \
663                                         (lp)->lpni_net->net_tunables.lct_peer_timeout > 0)
664
665 typedef struct {
666         struct list_head        lr_list;        /* chain on net */
667         struct list_head        lr_gwlist;      /* chain on gateway */
668         struct lnet_peer_ni     *lr_gateway;    /* router node */
669         __u32                   lr_net;         /* remote network number */
670         int                     lr_seq;         /* sequence for round-robin */
671         unsigned int            lr_downis;      /* number of down NIs */
672         __u32                   lr_hops;        /* how far I am */
673         unsigned int            lr_priority;    /* route priority */
674 } lnet_route_t;
675
676 #define LNET_REMOTE_NETS_HASH_DEFAULT   (1U << 7)
677 #define LNET_REMOTE_NETS_HASH_MAX       (1U << 16)
678 #define LNET_REMOTE_NETS_HASH_SIZE      (1 << the_lnet.ln_remote_nets_hbits)
679
680 typedef struct {
681         /* chain on ln_remote_nets_hash */
682         struct list_head        lrn_list;
683         /* routes to me */
684         struct list_head        lrn_routes;
685         /* my net number */
686         __u32                   lrn_net;
687 } lnet_remotenet_t;
688
689 /** lnet message has credit and can be submitted to lnd for send/receive */
690 #define LNET_CREDIT_OK          0
691 /** lnet message is waiting for credit */
692 #define LNET_CREDIT_WAIT        1
693
694 typedef struct {
695         /* my free buffer pool */
696         struct list_head        rbp_bufs;
697         /* messages blocking for a buffer */
698         struct list_head        rbp_msgs;
699         /* # pages in each buffer */
700         int                     rbp_npages;
701         /* requested number of buffers */
702         int                     rbp_req_nbuffers;
703         /* # buffers actually allocated */
704         int                     rbp_nbuffers;
705         /* # free buffers / blocked messages */
706         int                     rbp_credits;
707         /* low water mark */
708         int                     rbp_mincredits;
709 } lnet_rtrbufpool_t;
710
711 typedef struct {
712         struct list_head         rb_list;       /* chain on rbp_bufs */
713         lnet_rtrbufpool_t       *rb_pool;       /* owning pool */
714         lnet_kiov_t              rb_kiov[0];    /* the buffer space */
715 } lnet_rtrbuf_t;
716
717 #define LNET_PEER_HASHSIZE   503                /* prime! */
718
719 enum {
720         /* Didn't match anything */
721         LNET_MATCHMD_NONE       = (1 << 0),
722         /* Matched OK */
723         LNET_MATCHMD_OK         = (1 << 1),
724         /* Must be discarded */
725         LNET_MATCHMD_DROP       = (1 << 2),
726         /* match and buffer is exhausted */
727         LNET_MATCHMD_EXHAUSTED  = (1 << 3),
728         /* match or drop */
729         LNET_MATCHMD_FINISH     = (LNET_MATCHMD_OK | LNET_MATCHMD_DROP),
730 };
731
732 /* Options for lnet_portal_t::ptl_options */
733 #define LNET_PTL_LAZY               (1 << 0)
734 #define LNET_PTL_MATCH_UNIQUE       (1 << 1)    /* unique match, for RDMA */
735 #define LNET_PTL_MATCH_WILDCARD     (1 << 2)    /* wildcard match, request portal */
736
737 /* parameter for matching operations (GET, PUT) */
738 struct lnet_match_info {
739         __u64                   mi_mbits;
740         lnet_process_id_t       mi_id;
741         unsigned int            mi_cpt;
742         unsigned int            mi_opc;
743         unsigned int            mi_portal;
744         unsigned int            mi_rlength;
745         unsigned int            mi_roffset;
746 };
747
748 /* ME hash of RDMA portal */
749 #define LNET_MT_HASH_BITS               8
750 #define LNET_MT_HASH_SIZE               (1 << LNET_MT_HASH_BITS)
751 #define LNET_MT_HASH_MASK               (LNET_MT_HASH_SIZE - 1)
752 /* we allocate (LNET_MT_HASH_SIZE + 1) entries for lnet_match_table::mt_hash,
753  * the last entry is reserved for MEs with ignore-bits */
754 #define LNET_MT_HASH_IGNORE             LNET_MT_HASH_SIZE
755 /* __u64 has 2^6 bits, so need 2^(LNET_MT_HASH_BITS - LNET_MT_BITS_U64) which
756  * is 4 __u64s as bit-map, and add an extra __u64 (only use one bit) for the
757  * ME-list with ignore-bits, which is mtable::mt_hash[LNET_MT_HASH_IGNORE] */
758 #define LNET_MT_BITS_U64                6       /* 2^6 bits */
759 #define LNET_MT_EXHAUSTED_BITS          (LNET_MT_HASH_BITS - LNET_MT_BITS_U64)
760 #define LNET_MT_EXHAUSTED_BMAP          ((1 << LNET_MT_EXHAUSTED_BITS) + 1)
761
762 /* portal match table */
763 struct lnet_match_table {
764         /* reserved for upcoming patches, CPU partition ID */
765         unsigned int            mt_cpt;
766         unsigned int            mt_portal;      /* portal index */
767         /* match table is set as "enabled" if there's non-exhausted MD
768          * attached on mt_mhash, it's only valid for wildcard portal */
769         unsigned int            mt_enabled;
770         /* bitmap to flag whether MEs on mt_hash are exhausted or not */
771         __u64                   mt_exhausted[LNET_MT_EXHAUSTED_BMAP];
772         struct list_head        *mt_mhash;      /* matching hash */
773 };
774
775 /* these are only useful for wildcard portal */
776 /* Turn off message rotor for wildcard portals */
777 #define LNET_PTL_ROTOR_OFF      0
778 /* round-robin dispatch all PUT messages for wildcard portals */
779 #define LNET_PTL_ROTOR_ON       1
780 /* round-robin dispatch routed PUT message for wildcard portals */
781 #define LNET_PTL_ROTOR_RR_RT    2
782 /* dispatch routed PUT message by hashing source NID for wildcard portals */
783 #define LNET_PTL_ROTOR_HASH_RT  3
784
785 typedef struct lnet_portal {
786         spinlock_t              ptl_lock;
787         unsigned int            ptl_index;      /* portal ID, reserved */
788         /* flags on this portal: lazy, unique... */
789         unsigned int            ptl_options;
790         /* list of messages which are stealing buffer */
791         struct list_head        ptl_msg_stealing;
792         /* messages blocking for MD */
793         struct list_head        ptl_msg_delayed;
794         /* Match table for each CPT */
795         struct lnet_match_table **ptl_mtables;
796         /* spread rotor of incoming "PUT" */
797         unsigned int            ptl_rotor;
798         /* # active entries for this portal */
799         int                     ptl_mt_nmaps;
800         /* array of active entries' cpu-partition-id */
801         int                     ptl_mt_maps[0];
802 } lnet_portal_t;
803
804 #define LNET_LH_HASH_BITS       12
805 #define LNET_LH_HASH_SIZE       (1ULL << LNET_LH_HASH_BITS)
806 #define LNET_LH_HASH_MASK       (LNET_LH_HASH_SIZE - 1)
807
808 /* resource container (ME, MD, EQ) */
809 struct lnet_res_container {
810         unsigned int            rec_type;       /* container type */
811         __u64                   rec_lh_cookie;  /* cookie generator */
812         struct list_head        rec_active;     /* active resource list */
813         struct list_head        *rec_lh_hash;   /* handle hash */
814 };
815
816 /* message container */
817 struct lnet_msg_container {
818         int                     msc_init;       /* initialized or not */
819         /* max # threads finalizing */
820         int                     msc_nfinalizers;
821         /* msgs waiting to complete finalizing */
822         struct list_head        msc_finalizing;
823         struct list_head        msc_active;     /* active message list */
824         /* threads doing finalization */
825         void                    **msc_finalizers;
826 };
827
828 /* Peer Discovery states */
829 #define LNET_DC_STATE_SHUTDOWN          0       /* not started */
830 #define LNET_DC_STATE_RUNNING           1       /* started up OK */
831 #define LNET_DC_STATE_STOPPING          2       /* telling thread to stop */
832
833 /* Router Checker states */
834 #define LNET_RC_STATE_SHUTDOWN          0       /* not started */
835 #define LNET_RC_STATE_RUNNING           1       /* started up OK */
836 #define LNET_RC_STATE_STOPPING          2       /* telling thread to stop */
837
838 /* LNet states */
839 #define LNET_STATE_SHUTDOWN             0       /* not started */
840 #define LNET_STATE_RUNNING              1       /* started up OK */
841 #define LNET_STATE_STOPPING             2       /* telling thread to stop */
842
843 typedef struct
844 {
845         /* CPU partition table of LNet */
846         struct cfs_cpt_table            *ln_cpt_table;
847         /* number of CPTs in ln_cpt_table */
848         unsigned int                    ln_cpt_number;
849         unsigned int                    ln_cpt_bits;
850
851         /* protect LNet resources (ME/MD/EQ) */
852         struct cfs_percpt_lock          *ln_res_lock;
853         /* # portals */
854         int                             ln_nportals;
855         /* the vector of portals */
856         lnet_portal_t                   **ln_portals;
857         /* percpt ME containers */
858         struct lnet_res_container       **ln_me_containers;
859         /* percpt MD container */
860         struct lnet_res_container       **ln_md_containers;
861
862         /* Event Queue container */
863         struct lnet_res_container       ln_eq_container;
864         wait_queue_head_t               ln_eq_waitq;
865         spinlock_t                      ln_eq_wait_lock;
866
867         unsigned int                    ln_remote_nets_hbits;
868
869         /* protect NI, peer table, credits, routers, rtrbuf... */
870         struct cfs_percpt_lock          *ln_net_lock;
871         /* percpt message containers for active/finalizing/freed message */
872         struct lnet_msg_container       **ln_msg_containers;
873         lnet_counters_t                 **ln_counters;
874         struct lnet_peer_table          **ln_peer_tables;
875         /* list of peer nis not on a local network */
876         struct list_head                ln_remote_peer_ni_list;
877         /* failure simulation */
878         struct list_head                ln_test_peers;
879         struct list_head                ln_drop_rules;
880         struct list_head                ln_delay_rules;
881         /* LND instances */
882         struct list_head                ln_nets;
883         /* the loopback NI */
884         struct lnet_ni                  *ln_loni;
885         /* network zombie list */
886         struct list_head                ln_net_zombie;
887
888         /* remote networks with routes to them */
889         struct list_head                *ln_remote_nets_hash;
890         /* validity stamp */
891         __u64                           ln_remote_nets_version;
892         /* list of all known routers */
893         struct list_head                ln_routers;
894         /* validity stamp */
895         __u64                           ln_routers_version;
896         /* percpt router buffer pools */
897         lnet_rtrbufpool_t               **ln_rtrpools;
898
899         /*
900          * Ping target / Push source
901          *
902          * The ping target and push source share a single buffer. The
903          * ln_ping_target is protected against concurrent updates by
904          * ln_api_mutex.
905          */
906         lnet_handle_eq_t                ln_ping_target_eq;
907         lnet_handle_md_t                ln_ping_target_md;
908         struct lnet_ping_buffer         *ln_ping_target;
909         atomic_t                        ln_ping_target_seqno;
910
911         /*
912          * Push Target
913          *
914          * ln_push_nnis contains the desired size of the push target.
915          * The lnet_net_lock is used to handle update races. The old
916          * buffer may linger a while after it has been unlinked, in
917          * which case the event handler cleans up.
918          */
919         lnet_handle_eq_t                ln_push_target_eq;
920         lnet_handle_md_t                ln_push_target_md;
921         struct lnet_ping_buffer         *ln_push_target;
922         int                             ln_push_target_nnis;
923
924         /* discovery event queue handle */
925         lnet_handle_eq_t                ln_dc_eqh;
926         /* discovery requests */
927         struct list_head                ln_dc_request;
928         /* discovery working list */
929         struct list_head                ln_dc_working;
930         /* discovery thread wait queue */
931         wait_queue_head_t               ln_dc_waitq;
932         /* discovery startup/shutdown state */
933         int                             ln_dc_state;
934
935         /* router checker startup/shutdown state */
936         int                             ln_rc_state;
937         /* router checker's event queue */
938         lnet_handle_eq_t                ln_rc_eqh;
939         /* rcd still pending on net */
940         struct list_head                ln_rcd_deathrow;
941         /* rcd ready for free */
942         struct list_head                ln_rcd_zombie;
943         /* serialise startup/shutdown */
944         struct semaphore                ln_rc_signal;
945
946         struct mutex                    ln_api_mutex;
947         struct mutex                    ln_lnd_mutex;
948         /* Have I called LNetNIInit myself? */
949         int                             ln_niinit_self;
950         /* LNetNIInit/LNetNIFini counter */
951         int                             ln_refcount;
952         /* SHUTDOWN/RUNNING/STOPPING */
953         int                             ln_state;
954
955         int                             ln_routing;     /* am I a router? */
956         lnet_pid_t                      ln_pid;         /* requested pid */
957         /* uniquely identifies this ni in this epoch */
958         __u64                           ln_interface_cookie;
959         /* registered LNDs */
960         struct list_head                ln_lnds;
961
962         /* test protocol compatibility flags */
963         int                             ln_testprotocompat;
964
965         /* 0 - load the NIs from the mod params
966          * 1 - do not load the NIs from the mod params
967          * Reverse logic to ensure that other calls to LNetNIInit
968          * need no change
969          */
970         bool                            ln_nis_from_mod_params;
971
972         /* waitq for router checker.  As long as there are no routes in
973          * the list, the router checker will sleep on this queue.  when
974          * routes are added the thread will wake up */
975         wait_queue_head_t               ln_rc_waitq;
976 } lnet_t;
977
978 #endif