Whamcloud - gitweb
LU-10157 lnet: restore an maximal fragments count
[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, 2017, 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/semaphore.h>
48 #include <linux/types.h>
49
50 #include <uapi/linux/lnet/lnet-dlc.h>
51 #include <uapi/linux/lnet/lnetctl.h>
52
53 /* Max payload size */
54 #define LNET_MAX_PAYLOAD        LNET_MTU
55
56 /** limit on the number of fragments in discontiguous MDs */
57 #define LNET_MAX_IOV    256
58
59 /*
60  * This is the maximum health value.
61  * All local and peer NIs created have their health default to this value.
62  */
63 #define LNET_MAX_HEALTH_VALUE 1000
64
65 /* forward refs */
66 struct lnet_libmd;
67
68 enum lnet_msg_hstatus {
69         LNET_MSG_STATUS_OK = 0,
70         LNET_MSG_STATUS_LOCAL_INTERRUPT,
71         LNET_MSG_STATUS_LOCAL_DROPPED,
72         LNET_MSG_STATUS_LOCAL_ABORTED,
73         LNET_MSG_STATUS_LOCAL_NO_ROUTE,
74         LNET_MSG_STATUS_LOCAL_ERROR,
75         LNET_MSG_STATUS_LOCAL_TIMEOUT,
76         LNET_MSG_STATUS_REMOTE_ERROR,
77         LNET_MSG_STATUS_REMOTE_DROPPED,
78         LNET_MSG_STATUS_REMOTE_TIMEOUT,
79         LNET_MSG_STATUS_NETWORK_TIMEOUT,
80         LNET_MSG_STATUS_END,
81 };
82
83 struct lnet_rsp_tracker {
84         /* chain on the waiting list */
85         struct list_head rspt_on_list;
86         /* cpt to lock */
87         int rspt_cpt;
88         /* nid of next hop */
89         lnet_nid_t rspt_next_hop_nid;
90         /* deadline of the REPLY/ACK */
91         ktime_t rspt_deadline;
92         /* parent MD */
93         struct lnet_handle_md rspt_mdh;
94 };
95
96 struct lnet_msg {
97         struct list_head        msg_activelist;
98         struct list_head        msg_list;       /* Q for credits/MD */
99
100         struct lnet_process_id  msg_target;
101         /* Primary NID of the source. */
102         lnet_nid_t              msg_initiator;
103         /* where is it from, it's only for building event */
104         lnet_nid_t              msg_from;
105         __u32                   msg_type;
106
107         /*
108          * hold parameters in case message is with held due
109          * to discovery
110          */
111         lnet_nid_t              msg_src_nid_param;
112         lnet_nid_t              msg_rtr_nid_param;
113
114         /*
115          * Deadline for the message after which it will be finalized if it
116          * has not completed.
117          */
118         ktime_t                 msg_deadline;
119
120         /* The message health status. */
121         enum lnet_msg_hstatus   msg_health_status;
122         /* This is a recovery message */
123         bool                    msg_recovery;
124         /* the number of times a transmission has been retried */
125         int                     msg_retry_count;
126         /* flag to indicate that we do not want to resend this message */
127         bool                    msg_no_resend;
128
129         /* committed for sending */
130         unsigned int            msg_tx_committed:1;
131         /* CPT # this message committed for sending */
132         unsigned int            msg_tx_cpt:15;
133         /* committed for receiving */
134         unsigned int            msg_rx_committed:1;
135         /* CPT # this message committed for receiving */
136         unsigned int            msg_rx_cpt:15;
137         /* queued for tx credit */
138         unsigned int            msg_tx_delayed:1;
139         /* queued for RX buffer */
140         unsigned int            msg_rx_delayed:1;
141         /* ready for pending on RX delay list */
142         unsigned int            msg_rx_ready_delay:1;
143
144         unsigned int          msg_vmflush:1;      /* VM trying to free memory */
145         unsigned int          msg_target_is_router:1; /* sending to a router */
146         unsigned int          msg_routing:1;      /* being forwarded */
147         unsigned int          msg_ack:1;          /* ack on finalize (PUT) */
148         unsigned int          msg_sending:1;      /* outgoing message */
149         unsigned int          msg_receiving:1;    /* being received */
150         unsigned int          msg_txcredit:1;     /* taken an NI send credit */
151         unsigned int          msg_peertxcredit:1; /* taken a peer send credit */
152         unsigned int          msg_rtrcredit:1;    /* taken a globel router credit */
153         unsigned int          msg_peerrtrcredit:1; /* taken a peer router credit */
154         unsigned int          msg_onactivelist:1; /* on the activelist */
155         unsigned int          msg_rdma_get:1;
156
157         struct lnet_peer_ni  *msg_txpeer;         /* peer I'm sending to */
158         struct lnet_peer_ni  *msg_rxpeer;         /* peer I received from */
159
160         void                 *msg_private;
161         struct lnet_libmd    *msg_md;
162         /* the NI the message was sent or received over */
163         struct lnet_ni       *msg_txni;
164         struct lnet_ni       *msg_rxni;
165
166         unsigned int          msg_len;
167         unsigned int          msg_wanted;
168         unsigned int          msg_offset;
169         unsigned int          msg_niov;
170         struct kvec          *msg_iov;
171         lnet_kiov_t          *msg_kiov;
172
173         struct lnet_event       msg_ev;
174         struct lnet_hdr         msg_hdr;
175 };
176
177 struct lnet_libhandle {
178         struct list_head        lh_hash_chain;
179         __u64                   lh_cookie;
180 };
181
182 #define lh_entry(ptr, type, member) \
183         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
184
185 struct lnet_eq {
186         struct list_head        eq_list;
187         struct lnet_libhandle   eq_lh;
188         unsigned long           eq_enq_seq;
189         unsigned long           eq_deq_seq;
190         unsigned int            eq_size;
191         lnet_eq_handler_t       eq_callback;
192         struct lnet_event       *eq_events;
193         int                     **eq_refs;      /* percpt refcount for EQ */
194 };
195
196 struct lnet_me {
197         struct list_head        me_list;
198         struct lnet_libhandle   me_lh;
199         struct lnet_process_id  me_match_id;
200         unsigned int            me_portal;
201         unsigned int            me_pos;         /* hash offset in mt_hash */
202         __u64                   me_match_bits;
203         __u64                   me_ignore_bits;
204         enum lnet_unlink        me_unlink;
205         struct lnet_libmd      *me_md;
206 };
207
208 struct lnet_libmd {
209         struct list_head         md_list;
210         struct lnet_libhandle    md_lh;
211         struct lnet_me          *md_me;
212         char                    *md_start;
213         unsigned int             md_offset;
214         unsigned int             md_length;
215         unsigned int             md_max_size;
216         int                      md_threshold;
217         int                      md_refcount;
218         unsigned int             md_options;
219         unsigned int             md_flags;
220         unsigned int             md_niov;       /* # frags at end of struct */
221         void                    *md_user_ptr;
222         struct lnet_rsp_tracker *md_rspt_ptr;
223         struct lnet_eq          *md_eq;
224         struct lnet_handle_md    md_bulk_handle;
225         union {
226                 struct kvec      iov[LNET_MAX_IOV];
227                 lnet_kiov_t      kiov[LNET_MAX_IOV];
228         } md_iov;
229 };
230
231 #define LNET_MD_FLAG_ZOMBIE      (1 << 0)
232 #define LNET_MD_FLAG_AUTO_UNLINK (1 << 1)
233 #define LNET_MD_FLAG_ABORTED     (1 << 2)
234
235 struct lnet_test_peer {
236         /* info about peers we are trying to fail */
237         struct list_head        tp_list;        /* ln_test_peers */
238         lnet_nid_t              tp_nid;         /* matching nid */
239         unsigned int            tp_threshold;   /* # failures to simulate */
240 };
241
242 #define LNET_COOKIE_TYPE_MD    1
243 #define LNET_COOKIE_TYPE_ME    2
244 #define LNET_COOKIE_TYPE_EQ    3
245 #define LNET_COOKIE_TYPE_BITS  2
246 #define LNET_COOKIE_MASK        ((1ULL << LNET_COOKIE_TYPE_BITS) - 1ULL)
247
248 struct lnet_ni;                                  /* forward ref */
249 struct socket;
250
251 struct lnet_lnd {
252         /* fields managed by portals */
253         struct list_head        lnd_list;       /* stash in the LND table */
254         int                     lnd_refcount;   /* # active instances */
255
256         /* fields initialized by the LND */
257         __u32                   lnd_type;
258
259         int  (*lnd_startup)(struct lnet_ni *ni);
260         void (*lnd_shutdown)(struct lnet_ni *ni);
261         int  (*lnd_ctl)(struct lnet_ni *ni, unsigned int cmd, void *arg);
262
263         /* In data movement APIs below, payload buffers are described as a set
264          * of 'niov' fragments which are...
265          * EITHER
266          *    in virtual memory (struct kvec *iov != NULL)
267          * OR
268          *    in pages (kernel only: plt_kiov_t *kiov != NULL).
269          * The LND may NOT overwrite these fragment descriptors.
270          * An 'offset' and may specify a byte offset within the set of
271          * fragments to start from
272          */
273
274         /* Start sending a preformatted message.  'private' is NULL for PUT and
275          * GET messages; otherwise this is a response to an incoming message
276          * and 'private' is the 'private' passed to lnet_parse().  Return
277          * non-zero for immediate failure, otherwise complete later with
278          * lnet_finalize() */
279         int (*lnd_send)(struct lnet_ni *ni, void *private,
280                         struct lnet_msg *msg);
281
282         /* Start receiving 'mlen' bytes of payload data, skipping the following
283          * 'rlen' - 'mlen' bytes. 'private' is the 'private' passed to
284          * lnet_parse().  Return non-zero for immedaite failure, otherwise
285          * complete later with lnet_finalize().  This also gives back a receive
286          * credit if the LND does flow control. */
287         int (*lnd_recv)(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
288                         int delayed, unsigned int niov,
289                         struct kvec *iov, lnet_kiov_t *kiov,
290                         unsigned int offset, unsigned int mlen, unsigned int rlen);
291
292         /* lnet_parse() has had to delay processing of this message
293          * (e.g. waiting for a forwarding buffer or send credits).  Give the
294          * LND a chance to free urgently needed resources.  If called, return 0
295          * for success and do NOT give back a receive credit; that has to wait
296          * until lnd_recv() gets called.  On failure return < 0 and
297          * release resources; lnd_recv() will not be called. */
298         int (*lnd_eager_recv)(struct lnet_ni *ni, void *private,
299                               struct lnet_msg *msg, void **new_privatep);
300
301         /* notification of peer health */
302         void (*lnd_notify)(struct lnet_ni *ni, lnet_nid_t peer, int alive);
303
304         /* query of peer aliveness */
305         void (*lnd_query)(struct lnet_ni *ni, lnet_nid_t peer, time64_t *when);
306
307         /* accept a new connection */
308         int (*lnd_accept)(struct lnet_ni *ni, struct socket *sock);
309 };
310
311 struct lnet_tx_queue {
312         int                     tq_credits;     /* # tx credits free */
313         int                     tq_credits_min; /* lowest it's been */
314         int                     tq_credits_max; /* total # tx credits */
315         struct list_head        tq_delayed;     /* delayed TXs */
316 };
317
318 enum lnet_net_state {
319         /* set when net block is allocated */
320         LNET_NET_STATE_INIT = 0,
321         /* set when NIs in net are started successfully */
322         LNET_NET_STATE_ACTIVE,
323         /* set if all NIs in net are in FAILED state */
324         LNET_NET_STATE_INACTIVE,
325         /* set when shutting down a NET */
326         LNET_NET_STATE_DELETING
327 };
328
329 enum lnet_ni_state {
330         /* initial state when NI is created */
331         LNET_NI_STATE_INIT = 0,
332         /* set when NI is brought up */
333         LNET_NI_STATE_ACTIVE,
334         /* set when NI is being shutdown */
335         LNET_NI_STATE_DELETING,
336 };
337
338 #define LNET_NI_RECOVERY_PENDING        BIT(0)
339 #define LNET_NI_RECOVERY_FAILED         BIT(1)
340
341 enum lnet_stats_type {
342         LNET_STATS_TYPE_SEND = 0,
343         LNET_STATS_TYPE_RECV,
344         LNET_STATS_TYPE_DROP
345 };
346
347 struct lnet_comm_count {
348         atomic_t co_get_count;
349         atomic_t co_put_count;
350         atomic_t co_reply_count;
351         atomic_t co_ack_count;
352         atomic_t co_hello_count;
353 };
354
355 struct lnet_element_stats {
356         struct lnet_comm_count el_send_stats;
357         struct lnet_comm_count el_recv_stats;
358         struct lnet_comm_count el_drop_stats;
359 };
360
361 struct lnet_health_local_stats {
362         atomic_t hlt_local_interrupt;
363         atomic_t hlt_local_dropped;
364         atomic_t hlt_local_aborted;
365         atomic_t hlt_local_no_route;
366         atomic_t hlt_local_timeout;
367         atomic_t hlt_local_error;
368 };
369
370 struct lnet_health_remote_stats {
371         atomic_t hlt_remote_dropped;
372         atomic_t hlt_remote_timeout;
373         atomic_t hlt_remote_error;
374         atomic_t hlt_network_timeout;
375 };
376
377 struct lnet_net {
378         /* chain on the ln_nets */
379         struct list_head        net_list;
380
381         /* net ID, which is composed of
382          * (net_type << 16) | net_num.
383          * net_type can be one of the enumerated types defined in
384          * lnet/include/lnet/nidstr.h */
385         __u32                   net_id;
386
387         /* priority of the network */
388         __u32                   net_prio;
389
390         /* total number of CPTs in the array */
391         __u32                   net_ncpts;
392
393         /* cumulative CPTs of all NIs in this net */
394         __u32                   *net_cpts;
395
396         /* network tunables */
397         struct lnet_ioctl_config_lnd_cmn_tunables net_tunables;
398
399         /*
400          * boolean to indicate that the tunables have been set and
401          * shouldn't be reset
402          */
403         bool                    net_tunables_set;
404
405         /* procedural interface */
406         struct lnet_lnd         *net_lnd;
407
408         /* list of NIs on this net */
409         struct list_head        net_ni_list;
410
411         /* list of NIs being added, but not started yet */
412         struct list_head        net_ni_added;
413
414         /* dying LND instances */
415         struct list_head        net_ni_zombie;
416
417         /* network state */
418         enum lnet_net_state     net_state;
419 };
420
421 struct lnet_ni {
422         /* chain on the lnet_net structure */
423         struct list_head        ni_netlist;
424
425         /* chain on the recovery queue */
426         struct list_head        ni_recovery;
427
428         /* MD handle for recovery ping */
429         struct lnet_handle_md   ni_ping_mdh;
430
431         spinlock_t              ni_lock;
432
433         /* number of CPTs */
434         int                     ni_ncpts;
435
436         /* bond NI on some CPTs */
437         __u32                   *ni_cpts;
438
439         /* interface's NID */
440         lnet_nid_t              ni_nid;
441
442         /* instance-specific data */
443         void                    *ni_data;
444
445         /* per ni credits */
446         atomic_t                ni_tx_credits;
447
448         /* percpt TX queues */
449         struct lnet_tx_queue    **ni_tx_queues;
450
451         /* percpt reference count */
452         int                     **ni_refs;
453
454         /* when I was last alive */
455         time64_t                ni_last_alive;
456
457         /* pointer to parent network */
458         struct lnet_net         *ni_net;
459
460         /* my health status */
461         struct lnet_ni_status   *ni_status;
462
463         /* NI FSM. Protected by lnet_ni_lock() */
464         enum lnet_ni_state      ni_state;
465
466         /* Recovery state. Protected by lnet_ni_lock() */
467         __u32                   ni_recovery_state;
468
469         /* per NI LND tunables */
470         struct lnet_lnd_tunables ni_lnd_tunables;
471
472         /* lnd tunables set explicitly */
473         bool ni_lnd_tunables_set;
474
475         /* NI statistics */
476         struct lnet_element_stats ni_stats;
477         struct lnet_health_local_stats ni_hstats;
478
479         /* physical device CPT */
480         int                     ni_dev_cpt;
481
482         /* sequence number used to round robin over nis within a net */
483         __u32                   ni_seq;
484
485         /*
486          * health value
487          *      initialized to LNET_MAX_HEALTH_VALUE
488          * Value is decremented every time we fail to send a message over
489          * this NI because of a NI specific failure.
490          * Value is incremented if we successfully send a message.
491          */
492         atomic_t                ni_healthv;
493
494         /*
495          * Set to 1 by the LND when it receives an event telling it the device
496          * has gone into a fatal state. Set to 0 when the LND receives an
497          * even telling it the device is back online.
498          */
499         atomic_t                ni_fatal_error_on;
500
501         /*
502          * equivalent interfaces to use
503          * This is an array because socklnd bonding can still be configured
504          */
505         char                    *ni_interfaces[LNET_INTERFACES_NUM];
506         struct net              *ni_net_ns;     /* original net namespace */
507 };
508
509 #define LNET_PROTO_PING_MATCHBITS       0x8000000000000000LL
510
511 /*
512  * Descriptor of a ping info buffer: keep a separate indicator of the
513  * size and a reference count. The type is used both as a source and
514  * sink of data, so we need to keep some information outside of the
515  * area that may be overwritten by network data.
516  */
517 struct lnet_ping_buffer {
518         int                     pb_nnis;
519         atomic_t                pb_refcnt;
520         struct lnet_ping_info   pb_info;
521 };
522
523 #define LNET_PING_BUFFER_SIZE(NNIDS) \
524         offsetof(struct lnet_ping_buffer, pb_info.pi_ni[NNIDS])
525 #define LNET_PING_BUFFER_LONI(PBUF)     ((PBUF)->pb_info.pi_ni[0].ns_nid)
526 #define LNET_PING_BUFFER_SEQNO(PBUF)    ((PBUF)->pb_info.pi_ni[0].ns_status)
527
528 #define LNET_PING_INFO_TO_BUFFER(PINFO) \
529         container_of((PINFO), struct lnet_ping_buffer, pb_info)
530
531 /* router checker data, per router */
532 struct lnet_rc_data {
533         /* chain on the_lnet.ln_zombie_rcd or ln_deathrow_rcd */
534         struct list_head        rcd_list;
535         struct lnet_handle_md   rcd_mdh;        /* ping buffer MD */
536         struct lnet_peer_ni     *rcd_gateway;   /* reference to gateway */
537         struct lnet_ping_buffer *rcd_pingbuffer;/* ping buffer */
538         int                     rcd_nnis;       /* desired size of buffer */
539 };
540
541 struct lnet_peer_ni {
542         /* chain on lpn_peer_nis */
543         struct list_head        lpni_peer_nis;
544         /* chain on remote peer list */
545         struct list_head        lpni_on_remote_peer_ni_list;
546         /* chain on recovery queue */
547         struct list_head        lpni_recovery;
548         /* chain on peer hash */
549         struct list_head        lpni_hashlist;
550         /* messages blocking for tx credits */
551         struct list_head        lpni_txq;
552         /* messages blocking for router credits */
553         struct list_head        lpni_rtrq;
554         /* chain on router list */
555         struct list_head        lpni_rtr_list;
556         /* pointer to peer net I'm part of */
557         struct lnet_peer_net    *lpni_peer_net;
558         /* statistics kept on each peer NI */
559         struct lnet_element_stats lpni_stats;
560         struct lnet_health_remote_stats lpni_hstats;
561         /* spin lock protecting credits and lpni_txq / lpni_rtrq */
562         spinlock_t              lpni_lock;
563         /* # tx credits available */
564         int                     lpni_txcredits;
565         /* low water mark */
566         int                     lpni_mintxcredits;
567         /* # router credits */
568         int                     lpni_rtrcredits;
569         /* low water mark */
570         int                     lpni_minrtrcredits;
571         /* bytes queued for sending */
572         long                    lpni_txqnob;
573         /* alive/dead? */
574         bool                    lpni_alive;
575         /* notification outstanding? */
576         bool                    lpni_notify;
577         /* outstanding notification for LND? */
578         bool                    lpni_notifylnd;
579         /* some thread is handling notification */
580         bool                    lpni_notifying;
581         /* SEND event outstanding from ping */
582         bool                    lpni_ping_notsent;
583         /* # times router went dead<->alive. Protected with lpni_lock */
584         int                     lpni_alive_count;
585         /* time of last aliveness news */
586         time64_t                lpni_timestamp;
587         /* time of last ping attempt */
588         time64_t                lpni_ping_timestamp;
589         /* != 0 if ping reply expected */
590         time64_t                lpni_ping_deadline;
591         /* when I was last alive */
592         time64_t                lpni_last_alive;
593         /* when lpni_ni was queried last time */
594         time64_t                lpni_last_query;
595         /* network peer is on */
596         struct lnet_net         *lpni_net;
597         /* peer's NID */
598         lnet_nid_t              lpni_nid;
599         /* # refs */
600         atomic_t                lpni_refcount;
601         /* health value for the peer */
602         atomic_t                lpni_healthv;
603         /* recovery ping mdh */
604         struct lnet_handle_md   lpni_recovery_ping_mdh;
605         /* CPT this peer attached on */
606         int                     lpni_cpt;
607         /* state flags -- protected by lpni_lock */
608         unsigned                lpni_state;
609         /* # refs from lnet_route_t::lr_gateway */
610         int                     lpni_rtr_refcount;
611         /* sequence number used to round robin over peer nis within a net */
612         __u32                   lpni_seq;
613         /* sequence number used to round robin over gateways */
614         __u32                   lpni_gw_seq;
615         /* health flag */
616         bool                    lpni_healthy;
617         /* returned RC ping features. Protected with lpni_lock */
618         unsigned int            lpni_ping_feats;
619         /* routes on this peer */
620         struct list_head        lpni_routes;
621         /* preferred local nids: if only one, use lpni_pref.nid */
622         union lpni_pref {
623                 lnet_nid_t      nid;
624                 lnet_nid_t      *nids;
625         } lpni_pref;
626         /* number of preferred NIDs in lnpi_pref_nids */
627         __u32                   lpni_pref_nnids;
628         /* router checker state */
629         struct lnet_rc_data     *lpni_rcd;
630 };
631
632 /* Preferred path added due to traffic on non-MR peer_ni */
633 #define LNET_PEER_NI_NON_MR_PREF        (1 << 0)
634 /* peer is being recovered. */
635 #define LNET_PEER_NI_RECOVERY_PENDING   (1 << 1)
636 /* recovery ping failed */
637 #define LNET_PEER_NI_RECOVERY_FAILED    (1 << 2)
638 /* peer is being deleted */
639 #define LNET_PEER_NI_DELETING           (1 << 3)
640
641 struct lnet_peer {
642         /* chain on pt_peer_list */
643         struct list_head        lp_peer_list;
644
645         /* list of peer nets */
646         struct list_head        lp_peer_nets;
647
648         /* list of messages pending discovery*/
649         struct list_head        lp_dc_pendq;
650
651         /* primary NID of the peer */
652         lnet_nid_t              lp_primary_nid;
653
654         /* source NID to use during discovery */
655         lnet_nid_t              lp_disc_src_nid;
656
657         /* CPT of peer_table */
658         int                     lp_cpt;
659
660         /* number of NIDs on this peer */
661         int                     lp_nnis;
662
663         /* reference count */
664         atomic_t                lp_refcount;
665
666         /* lock protecting peer state flags */
667         spinlock_t              lp_lock;
668
669         /* peer state flags */
670         unsigned                lp_state;
671
672         /* buffer for data pushed by peer */
673         struct lnet_ping_buffer *lp_data;
674
675         /* MD handle for ping in progress */
676         struct lnet_handle_md   lp_ping_mdh;
677
678         /* MD handle for push in progress */
679         struct lnet_handle_md   lp_push_mdh;
680
681         /* number of NIDs for sizing push data */
682         int                     lp_data_nnis;
683
684         /* NI config sequence number of peer */
685         __u32                   lp_peer_seqno;
686
687         /* Local NI config sequence number acked by peer */
688         __u32                   lp_node_seqno;
689
690         /* Local NI config sequence number sent to peer */
691         __u32                   lp_node_seqno_sent;
692
693         /* Ping error encountered during discovery. */
694         int                     lp_ping_error;
695
696         /* Push error encountered during discovery. */
697         int                     lp_push_error;
698
699         /* Error encountered during discovery. */
700         int                     lp_dc_error;
701
702         /* time it was put on the ln_dc_working queue */
703         time64_t                lp_last_queued;
704
705         /* link on discovery-related lists */
706         struct list_head        lp_dc_list;
707
708         /* tasks waiting on discovery of this peer */
709         wait_queue_head_t       lp_dc_waitq;
710 };
711
712 /*
713  * The status flags in lp_state. Their semantics have chosen so that
714  * lp_state can be zero-initialized.
715  *
716  * A peer is marked MULTI_RAIL in two cases: it was configured using DLC
717  * as multi-rail aware, or the LNET_PING_FEAT_MULTI_RAIL bit was set.
718  *
719  * A peer is marked NO_DISCOVERY if the LNET_PING_FEAT_DISCOVERY bit was
720  * NOT set when the peer was pinged by discovery.
721  */
722 #define LNET_PEER_MULTI_RAIL    (1 << 0)        /* Multi-rail aware */
723 #define LNET_PEER_NO_DISCOVERY  (1 << 1)        /* Peer disabled discovery */
724 /*
725  * A peer is marked CONFIGURED if it was configured by DLC.
726  *
727  * In addition, a peer is marked DISCOVERED if it has fully passed
728  * through Peer Discovery.
729  *
730  * When Peer Discovery is disabled, the discovery thread will mark
731  * peers REDISCOVER to indicate that they should be re-examined if
732  * discovery is (re)enabled on the node.
733  *
734  * A peer that was created as the result of inbound traffic will not
735  * be marked at all.
736  */
737 #define LNET_PEER_CONFIGURED    (1 << 2)        /* Configured via DLC */
738 #define LNET_PEER_DISCOVERED    (1 << 3)        /* Peer was discovered */
739 #define LNET_PEER_REDISCOVER    (1 << 4)        /* Discovery was disabled */
740 /*
741  * A peer is marked DISCOVERING when discovery is in progress.
742  * The other flags below correspond to stages of discovery.
743  */
744 #define LNET_PEER_DISCOVERING   (1 << 5)        /* Discovering */
745 #define LNET_PEER_DATA_PRESENT  (1 << 6)        /* Remote peer data present */
746 #define LNET_PEER_NIDS_UPTODATE (1 << 7)        /* Remote peer info uptodate */
747 #define LNET_PEER_PING_SENT     (1 << 8)        /* Waiting for REPLY to Ping */
748 #define LNET_PEER_PUSH_SENT     (1 << 9)        /* Waiting for ACK of Push */
749 #define LNET_PEER_PING_FAILED   (1 << 10)       /* Ping send failure */
750 #define LNET_PEER_PUSH_FAILED   (1 << 11)       /* Push send failure */
751 /*
752  * A ping can be forced as a way to fix up state, or as a manual
753  * intervention by an admin.
754  * A push can be forced in circumstances that would normally not
755  * allow for one to happen.
756  */
757 #define LNET_PEER_FORCE_PING    (1 << 12)       /* Forced Ping */
758 #define LNET_PEER_FORCE_PUSH    (1 << 13)       /* Forced Push */
759
760 struct lnet_peer_net {
761         /* chain on lp_peer_nets */
762         struct list_head        lpn_peer_nets;
763
764         /* list of peer_nis on this network */
765         struct list_head        lpn_peer_nis;
766
767         /* pointer to the peer I'm part of */
768         struct lnet_peer        *lpn_peer;
769
770         /* Net ID */
771         __u32                   lpn_net_id;
772
773         /* reference count */
774         atomic_t                lpn_refcount;
775 };
776
777 /* peer hash size */
778 #define LNET_PEER_HASH_BITS     9
779 #define LNET_PEER_HASH_SIZE     (1 << LNET_PEER_HASH_BITS)
780
781 /*
782  * peer hash table - one per CPT
783  *
784  * protected by lnet_net_lock/EX for update
785  *    pt_version
786  *    pt_number
787  *    pt_hash[...]
788  *    pt_peer_list
789  *    pt_peers
790  * protected by pt_zombie_lock:
791  *    pt_zombie_list
792  *    pt_zombies
793  *
794  * pt_zombie lock nests inside lnet_net_lock
795  */
796 struct lnet_peer_table {
797         int                     pt_version;     /* /proc validity stamp */
798         int                     pt_number;      /* # peers_ni extant */
799         struct list_head        *pt_hash;       /* NID->peer hash */
800         struct list_head        pt_peer_list;   /* peers */
801         int                     pt_peers;       /* # peers */
802         struct list_head        pt_zombie_list; /* zombie peer_ni */
803         int                     pt_zombies;     /* # zombie peers_ni */
804         spinlock_t              pt_zombie_lock; /* protect list and count */
805 };
806
807 /* peer aliveness is enabled only on routers for peers in a network where the
808  * struct lnet_ni::ni_peertimeout has been set to a positive value
809  */
810 #define lnet_peer_aliveness_enabled(lp) (the_lnet.ln_routing != 0 && \
811                                         ((lp)->lpni_net) && \
812                                         (lp)->lpni_net->net_tunables.lct_peer_timeout > 0)
813
814 struct lnet_route {
815         struct list_head        lr_list;        /* chain on net */
816         struct list_head        lr_gwlist;      /* chain on gateway */
817         struct lnet_peer_ni     *lr_gateway;    /* router node */
818         __u32                   lr_net;         /* remote network number */
819         int                     lr_seq;         /* sequence for round-robin */
820         unsigned int            lr_downis;      /* number of down NIs */
821         __u32                   lr_hops;        /* how far I am */
822         unsigned int            lr_priority;    /* route priority */
823 };
824
825 #define LNET_REMOTE_NETS_HASH_DEFAULT   (1U << 7)
826 #define LNET_REMOTE_NETS_HASH_MAX       (1U << 16)
827 #define LNET_REMOTE_NETS_HASH_SIZE      (1 << the_lnet.ln_remote_nets_hbits)
828
829 struct lnet_remotenet {
830         /* chain on ln_remote_nets_hash */
831         struct list_head        lrn_list;
832         /* routes to me */
833         struct list_head        lrn_routes;
834         /* my net number */
835         __u32                   lrn_net;
836 };
837
838 /** lnet message has credit and can be submitted to lnd for send/receive */
839 #define LNET_CREDIT_OK          0
840 /** lnet message is waiting for credit */
841 #define LNET_CREDIT_WAIT        1
842 /** lnet message is waiting for discovery */
843 #define LNET_DC_WAIT            2
844
845 struct lnet_rtrbufpool {
846         /* my free buffer pool */
847         struct list_head        rbp_bufs;
848         /* messages blocking for a buffer */
849         struct list_head        rbp_msgs;
850         /* # pages in each buffer */
851         int                     rbp_npages;
852         /* requested number of buffers */
853         int                     rbp_req_nbuffers;
854         /* # buffers actually allocated */
855         int                     rbp_nbuffers;
856         /* # free buffers / blocked messages */
857         int                     rbp_credits;
858         /* low water mark */
859         int                     rbp_mincredits;
860 };
861
862 struct lnet_rtrbuf {
863         struct list_head         rb_list;       /* chain on rbp_bufs */
864         struct lnet_rtrbufpool  *rb_pool;       /* owning pool */
865         lnet_kiov_t              rb_kiov[0];    /* the buffer space */
866 };
867
868 #define LNET_PEER_HASHSIZE   503                /* prime! */
869
870 enum lnet_match_flags {
871         /* Didn't match anything */
872         LNET_MATCHMD_NONE       = (1 << 0),
873         /* Matched OK */
874         LNET_MATCHMD_OK         = (1 << 1),
875         /* Must be discarded */
876         LNET_MATCHMD_DROP       = (1 << 2),
877         /* match and buffer is exhausted */
878         LNET_MATCHMD_EXHAUSTED  = (1 << 3),
879         /* match or drop */
880         LNET_MATCHMD_FINISH     = (LNET_MATCHMD_OK | LNET_MATCHMD_DROP),
881 };
882
883 /* Options for struct lnet_portal::ptl_options */
884 #define LNET_PTL_LAZY               (1 << 0)
885 #define LNET_PTL_MATCH_UNIQUE       (1 << 1)    /* unique match, for RDMA */
886 #define LNET_PTL_MATCH_WILDCARD     (1 << 2)    /* wildcard match, request portal */
887
888 /* parameter for matching operations (GET, PUT) */
889 struct lnet_match_info {
890         __u64                   mi_mbits;
891         struct lnet_process_id  mi_id;
892         unsigned int            mi_cpt;
893         unsigned int            mi_opc;
894         unsigned int            mi_portal;
895         unsigned int            mi_rlength;
896         unsigned int            mi_roffset;
897 };
898
899 /* ME hash of RDMA portal */
900 #define LNET_MT_HASH_BITS               8
901 #define LNET_MT_HASH_SIZE               (1 << LNET_MT_HASH_BITS)
902 #define LNET_MT_HASH_MASK               (LNET_MT_HASH_SIZE - 1)
903 /* we allocate (LNET_MT_HASH_SIZE + 1) entries for lnet_match_table::mt_hash,
904  * the last entry is reserved for MEs with ignore-bits */
905 #define LNET_MT_HASH_IGNORE             LNET_MT_HASH_SIZE
906 /* __u64 has 2^6 bits, so need 2^(LNET_MT_HASH_BITS - LNET_MT_BITS_U64) which
907  * is 4 __u64s as bit-map, and add an extra __u64 (only use one bit) for the
908  * ME-list with ignore-bits, which is mtable::mt_hash[LNET_MT_HASH_IGNORE] */
909 #define LNET_MT_BITS_U64                6       /* 2^6 bits */
910 #define LNET_MT_EXHAUSTED_BITS          (LNET_MT_HASH_BITS - LNET_MT_BITS_U64)
911 #define LNET_MT_EXHAUSTED_BMAP          ((1 << LNET_MT_EXHAUSTED_BITS) + 1)
912
913 /* portal match table */
914 struct lnet_match_table {
915         /* reserved for upcoming patches, CPU partition ID */
916         unsigned int            mt_cpt;
917         unsigned int            mt_portal;      /* portal index */
918         /* match table is set as "enabled" if there's non-exhausted MD
919          * attached on mt_mhash, it's only valid for wildcard portal */
920         unsigned int            mt_enabled;
921         /* bitmap to flag whether MEs on mt_hash are exhausted or not */
922         __u64                   mt_exhausted[LNET_MT_EXHAUSTED_BMAP];
923         struct list_head        *mt_mhash;      /* matching hash */
924 };
925
926 /* these are only useful for wildcard portal */
927 /* Turn off message rotor for wildcard portals */
928 #define LNET_PTL_ROTOR_OFF      0
929 /* round-robin dispatch all PUT messages for wildcard portals */
930 #define LNET_PTL_ROTOR_ON       1
931 /* round-robin dispatch routed PUT message for wildcard portals */
932 #define LNET_PTL_ROTOR_RR_RT    2
933 /* dispatch routed PUT message by hashing source NID for wildcard portals */
934 #define LNET_PTL_ROTOR_HASH_RT  3
935
936 struct lnet_portal {
937         spinlock_t              ptl_lock;
938         unsigned int            ptl_index;      /* portal ID, reserved */
939         /* flags on this portal: lazy, unique... */
940         unsigned int            ptl_options;
941         /* list of messages which are stealing buffer */
942         struct list_head        ptl_msg_stealing;
943         /* messages blocking for MD */
944         struct list_head        ptl_msg_delayed;
945         /* Match table for each CPT */
946         struct lnet_match_table **ptl_mtables;
947         /* spread rotor of incoming "PUT" */
948         unsigned int            ptl_rotor;
949         /* # active entries for this portal */
950         int                     ptl_mt_nmaps;
951         /* array of active entries' cpu-partition-id */
952         int                     ptl_mt_maps[0];
953 };
954
955 #define LNET_LH_HASH_BITS       12
956 #define LNET_LH_HASH_SIZE       (1ULL << LNET_LH_HASH_BITS)
957 #define LNET_LH_HASH_MASK       (LNET_LH_HASH_SIZE - 1)
958
959 /* resource container (ME, MD, EQ) */
960 struct lnet_res_container {
961         unsigned int            rec_type;       /* container type */
962         __u64                   rec_lh_cookie;  /* cookie generator */
963         struct list_head        rec_active;     /* active resource list */
964         struct list_head        *rec_lh_hash;   /* handle hash */
965 };
966
967 /* message container */
968 struct lnet_msg_container {
969         int                     msc_init;       /* initialized or not */
970         /* max # threads finalizing */
971         int                     msc_nfinalizers;
972         /* msgs waiting to complete finalizing */
973         struct list_head        msc_finalizing;
974         /* msgs waiting to be resent */
975         struct list_head        msc_resending;
976         struct list_head        msc_active;     /* active message list */
977         /* threads doing finalization */
978         void                    **msc_finalizers;
979         /* threads doing resends */
980         void                    **msc_resenders;
981 };
982
983 /* Peer Discovery states */
984 #define LNET_DC_STATE_SHUTDOWN          0       /* not started */
985 #define LNET_DC_STATE_RUNNING           1       /* started up OK */
986 #define LNET_DC_STATE_STOPPING          2       /* telling thread to stop */
987
988 /* Router Checker states */
989 #define LNET_MT_STATE_SHUTDOWN          0       /* not started */
990 #define LNET_MT_STATE_RUNNING           1       /* started up OK */
991 #define LNET_MT_STATE_STOPPING          2       /* telling thread to stop */
992
993 /* LNet states */
994 #define LNET_STATE_SHUTDOWN             0       /* not started */
995 #define LNET_STATE_RUNNING              1       /* started up OK */
996 #define LNET_STATE_STOPPING             2       /* telling thread to stop */
997
998 struct lnet {
999         /* CPU partition table of LNet */
1000         struct cfs_cpt_table            *ln_cpt_table;
1001         /* number of CPTs in ln_cpt_table */
1002         unsigned int                    ln_cpt_number;
1003         unsigned int                    ln_cpt_bits;
1004
1005         /* protect LNet resources (ME/MD/EQ) */
1006         struct cfs_percpt_lock          *ln_res_lock;
1007         /* # portals */
1008         int                             ln_nportals;
1009         /* the vector of portals */
1010         struct lnet_portal              **ln_portals;
1011         /* percpt ME containers */
1012         struct lnet_res_container       **ln_me_containers;
1013         /* percpt MD container */
1014         struct lnet_res_container       **ln_md_containers;
1015
1016         /* Event Queue container */
1017         struct lnet_res_container       ln_eq_container;
1018         wait_queue_head_t               ln_eq_waitq;
1019         spinlock_t                      ln_eq_wait_lock;
1020
1021         unsigned int                    ln_remote_nets_hbits;
1022
1023         /* protect NI, peer table, credits, routers, rtrbuf... */
1024         struct cfs_percpt_lock          *ln_net_lock;
1025         /* percpt message containers for active/finalizing/freed message */
1026         struct lnet_msg_container       **ln_msg_containers;
1027         struct lnet_counters            **ln_counters;
1028         struct lnet_peer_table          **ln_peer_tables;
1029         /* list of peer nis not on a local network */
1030         struct list_head                ln_remote_peer_ni_list;
1031         /* failure simulation */
1032         struct list_head                ln_test_peers;
1033         struct list_head                ln_drop_rules;
1034         struct list_head                ln_delay_rules;
1035         /* LND instances */
1036         struct list_head                ln_nets;
1037         /* the loopback NI */
1038         struct lnet_ni                  *ln_loni;
1039         /* network zombie list */
1040         struct list_head                ln_net_zombie;
1041         /* resend messages list */
1042         struct list_head                ln_msg_resend;
1043         /* spin lock to protect the msg resend list */
1044         spinlock_t                      ln_msg_resend_lock;
1045
1046         /* remote networks with routes to them */
1047         struct list_head                *ln_remote_nets_hash;
1048         /* validity stamp */
1049         __u64                           ln_remote_nets_version;
1050         /* list of all known routers */
1051         struct list_head                ln_routers;
1052         /* validity stamp */
1053         __u64                           ln_routers_version;
1054         /* percpt router buffer pools */
1055         struct lnet_rtrbufpool          **ln_rtrpools;
1056
1057         /*
1058          * Ping target / Push source
1059          *
1060          * The ping target and push source share a single buffer. The
1061          * ln_ping_target is protected against concurrent updates by
1062          * ln_api_mutex.
1063          */
1064         struct lnet_handle_md           ln_ping_target_md;
1065         struct lnet_handle_eq           ln_ping_target_eq;
1066         struct lnet_ping_buffer         *ln_ping_target;
1067         atomic_t                        ln_ping_target_seqno;
1068
1069         /*
1070          * Push Target
1071          *
1072          * ln_push_nnis contains the desired size of the push target.
1073          * The lnet_net_lock is used to handle update races. The old
1074          * buffer may linger a while after it has been unlinked, in
1075          * which case the event handler cleans up.
1076          */
1077         struct lnet_handle_eq           ln_push_target_eq;
1078         struct lnet_handle_md           ln_push_target_md;
1079         struct lnet_ping_buffer         *ln_push_target;
1080         int                             ln_push_target_nnis;
1081
1082         /* discovery event queue handle */
1083         struct lnet_handle_eq           ln_dc_eqh;
1084         /* discovery requests */
1085         struct list_head                ln_dc_request;
1086         /* discovery working list */
1087         struct list_head                ln_dc_working;
1088         /* discovery expired list */
1089         struct list_head                ln_dc_expired;
1090         /* discovery thread wait queue */
1091         wait_queue_head_t               ln_dc_waitq;
1092         /* discovery startup/shutdown state */
1093         int                             ln_dc_state;
1094
1095         /* monitor thread startup/shutdown state */
1096         int                             ln_mt_state;
1097         /* router checker's event queue */
1098         struct lnet_handle_eq           ln_rc_eqh;
1099         /* rcd still pending on net */
1100         struct list_head                ln_rcd_deathrow;
1101         /* rcd ready for free */
1102         struct list_head                ln_rcd_zombie;
1103         /* serialise startup/shutdown */
1104         struct semaphore                ln_mt_signal;
1105
1106         struct mutex                    ln_api_mutex;
1107         struct mutex                    ln_lnd_mutex;
1108         /* Have I called LNetNIInit myself? */
1109         int                             ln_niinit_self;
1110         /* LNetNIInit/LNetNIFini counter */
1111         int                             ln_refcount;
1112         /* SHUTDOWN/RUNNING/STOPPING */
1113         int                             ln_state;
1114
1115         int                             ln_routing;     /* am I a router? */
1116         lnet_pid_t                      ln_pid;         /* requested pid */
1117         /* uniquely identifies this ni in this epoch */
1118         __u64                           ln_interface_cookie;
1119         /* registered LNDs */
1120         struct list_head                ln_lnds;
1121
1122         /* test protocol compatibility flags */
1123         int                             ln_testprotocompat;
1124
1125         /* 0 - load the NIs from the mod params
1126          * 1 - do not load the NIs from the mod params
1127          * Reverse logic to ensure that other calls to LNetNIInit
1128          * need no change
1129          */
1130         bool                            ln_nis_from_mod_params;
1131
1132         /*
1133          * waitq for the monitor thread. The monitor thread takes care of
1134          * checking routes, timedout messages and resending messages.
1135          */
1136         wait_queue_head_t               ln_mt_waitq;
1137
1138         /* per-cpt resend queues */
1139         struct list_head                **ln_mt_resendqs;
1140         /* local NIs to recover */
1141         struct list_head                ln_mt_localNIRecovq;
1142         /* local NIs to recover */
1143         struct list_head                ln_mt_peerNIRecovq;
1144         /*
1145          * An array of queues for GET/PUT waiting for REPLY/ACK respectively.
1146          * There are CPT number of queues. Since response trackers will be
1147          * added on the fast path we can't afford to grab the exclusive
1148          * net lock to protect these queues. The CPT will be calculated
1149          * based on the mdh cookie.
1150          */
1151         struct list_head                **ln_mt_rstq;
1152         /*
1153          * A response tracker becomes a zombie when the associated MD is queued
1154          * for unlink before the response tracker is detached from the MD. An
1155          * entry on a zombie list can be freed when either the remaining
1156          * operations on the MD complete or when LNet has shut down.
1157          */
1158         struct list_head                **ln_mt_zombie_rstqs;
1159         /* recovery eq handler */
1160         struct lnet_handle_eq           ln_mt_eqh;
1161
1162 };
1163
1164 #endif