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