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