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