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