Whamcloud - gitweb
LU-9921 lnet: resolve unsafe list access
[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 enum lnet_stats_type {
298         LNET_STATS_TYPE_SEND = 0,
299         LNET_STATS_TYPE_RECV,
300         LNET_STATS_TYPE_DROP
301 };
302
303 struct lnet_comm_count {
304         atomic_t co_get_count;
305         atomic_t co_put_count;
306         atomic_t co_reply_count;
307         atomic_t co_ack_count;
308         atomic_t co_hello_count;
309 };
310
311 struct lnet_element_stats {
312         struct lnet_comm_count el_send_stats;
313         struct lnet_comm_count el_recv_stats;
314         struct lnet_comm_count el_drop_stats;
315 };
316
317 struct lnet_net {
318         /* chain on the ln_nets */
319         struct list_head        net_list;
320
321         /* net ID, which is composed of
322          * (net_type << 16) | net_num.
323          * net_type can be one of the enumerated types defined in
324          * lnet/include/lnet/nidstr.h */
325         __u32                   net_id;
326
327         /* priority of the network */
328         __u32                   net_prio;
329
330         /* total number of CPTs in the array */
331         __u32                   net_ncpts;
332
333         /* cumulative CPTs of all NIs in this net */
334         __u32                   *net_cpts;
335
336         /* network tunables */
337         struct lnet_ioctl_config_lnd_cmn_tunables net_tunables;
338
339         /*
340          * boolean to indicate that the tunables have been set and
341          * shouldn't be reset
342          */
343         bool                    net_tunables_set;
344
345         /* procedural interface */
346         struct lnet_lnd         *net_lnd;
347
348         /* list of NIs on this net */
349         struct list_head        net_ni_list;
350
351         /* list of NIs being added, but not started yet */
352         struct list_head        net_ni_added;
353
354         /* dying LND instances */
355         struct list_head        net_ni_zombie;
356
357         /* network state */
358         enum lnet_net_state     net_state;
359 };
360
361 typedef struct lnet_ni {
362         /* chain on the lnet_net structure */
363         struct list_head        ni_netlist;
364
365         /* chain on net_ni_cpt */
366         struct list_head        ni_cptlist;
367
368         spinlock_t              ni_lock;
369
370         /* number of CPTs */
371         int                     ni_ncpts;
372
373         /* bond NI on some CPTs */
374         __u32                   *ni_cpts;
375
376         /* interface's NID */
377         lnet_nid_t              ni_nid;
378
379         /* instance-specific data */
380         void                    *ni_data;
381
382         /* per ni credits */
383         atomic_t                ni_tx_credits;
384
385         /* percpt TX queues */
386         struct lnet_tx_queue    **ni_tx_queues;
387
388         /* percpt reference count */
389         int                     **ni_refs;
390
391         /* when I was last alive */
392         long                    ni_last_alive;
393
394         /* pointer to parent network */
395         struct lnet_net         *ni_net;
396
397         /* my health status */
398         struct lnet_ni_status   *ni_status;
399
400         /* NI FSM */
401         enum lnet_ni_state      ni_state;
402
403         /* per NI LND tunables */
404         struct lnet_lnd_tunables ni_lnd_tunables;
405
406         /* lnd tunables set explicitly */
407         bool ni_lnd_tunables_set;
408
409         /* NI statistics */
410         struct lnet_element_stats ni_stats;
411
412         /* physical device CPT */
413         int                     ni_dev_cpt;
414
415         /* sequence number used to round robin over nis within a net */
416         __u32                   ni_seq;
417
418         /*
419          * equivalent interfaces to use
420          * This is an array because socklnd bonding can still be configured
421          */
422         char                    *ni_interfaces[LNET_INTERFACES_NUM];
423         struct net              *ni_net_ns;     /* original net namespace */
424 } lnet_ni_t;
425
426 #define LNET_PROTO_PING_MATCHBITS       0x8000000000000000LL
427
428 /*
429  * Descriptor of a ping info buffer: keep a separate indicator of the
430  * size and a reference count. The type is used both as a source and
431  * sink of data, so we need to keep some information outside of the
432  * area that may be overwritten by network data.
433  */
434 struct lnet_ping_buffer {
435         int                     pb_nnis;
436         atomic_t                pb_refcnt;
437         struct lnet_ping_info   pb_info;
438 };
439
440 #define LNET_PING_BUFFER_SIZE(NNIDS) \
441         offsetof(struct lnet_ping_buffer, pb_info.pi_ni[NNIDS])
442 #define LNET_PING_BUFFER_LONI(PBUF)     ((PBUF)->pb_info.pi_ni[0].ns_nid)
443 #define LNET_PING_BUFFER_SEQNO(PBUF)    ((PBUF)->pb_info.pi_ni[0].ns_status)
444
445 #define LNET_PING_INFO_TO_BUFFER(PINFO) \
446         container_of((PINFO), struct lnet_ping_buffer, pb_info)
447
448 /* router checker data, per router */
449 typedef struct lnet_rc_data {
450         /* chain on the_lnet.ln_zombie_rcd or ln_deathrow_rcd */
451         struct list_head        rcd_list;
452         struct lnet_handle_md   rcd_mdh;        /* ping buffer MD */
453         struct lnet_peer_ni     *rcd_gateway;   /* reference to gateway */
454         struct lnet_ping_buffer *rcd_pingbuffer;/* ping buffer */
455         int                     rcd_nnis;       /* desired size of buffer */
456 } lnet_rc_data_t;
457
458 struct lnet_peer_ni {
459         /* chain on lpn_peer_nis */
460         struct list_head        lpni_peer_nis;
461         /* chain on remote peer list */
462         struct list_head        lpni_on_remote_peer_ni_list;
463         /* chain on peer hash */
464         struct list_head        lpni_hashlist;
465         /* messages blocking for tx credits */
466         struct list_head        lpni_txq;
467         /* messages blocking for router credits */
468         struct list_head        lpni_rtrq;
469         /* chain on router list */
470         struct list_head        lpni_rtr_list;
471         /* pointer to peer net I'm part of */
472         struct lnet_peer_net    *lpni_peer_net;
473         /* statistics kept on each peer NI */
474         struct lnet_element_stats lpni_stats;
475         /* spin lock protecting credits and lpni_txq / lpni_rtrq */
476         spinlock_t              lpni_lock;
477         /* # tx credits available */
478         int                     lpni_txcredits;
479         /* low water mark */
480         int                     lpni_mintxcredits;
481         /* # router credits */
482         int                     lpni_rtrcredits;
483         /* low water mark */
484         int                     lpni_minrtrcredits;
485         /* bytes queued for sending */
486         long                    lpni_txqnob;
487         /* alive/dead? */
488         bool                    lpni_alive;
489         /* notification outstanding? */
490         bool                    lpni_notify;
491         /* outstanding notification for LND? */
492         bool                    lpni_notifylnd;
493         /* some thread is handling notification */
494         bool                    lpni_notifying;
495         /* SEND event outstanding from ping */
496         bool                    lpni_ping_notsent;
497         /* # times router went dead<->alive. Protected with lpni_lock */
498         int                     lpni_alive_count;
499         /* time of last aliveness news */
500         cfs_time_t              lpni_timestamp;
501         /* time of last ping attempt */
502         cfs_time_t              lpni_ping_timestamp;
503         /* != 0 if ping reply expected */
504         cfs_time_t              lpni_ping_deadline;
505         /* when I was last alive */
506         cfs_time_t              lpni_last_alive;
507         /* when lpni_ni was queried last time */
508         cfs_time_t              lpni_last_query;
509         /* network peer is on */
510         struct lnet_net         *lpni_net;
511         /* peer's NID */
512         lnet_nid_t              lpni_nid;
513         /* # refs */
514         atomic_t                lpni_refcount;
515         /* CPT this peer attached on */
516         int                     lpni_cpt;
517         /* state flags -- protected by lpni_lock */
518         unsigned                lpni_state;
519         /* # refs from lnet_route_t::lr_gateway */
520         int                     lpni_rtr_refcount;
521         /* sequence number used to round robin over peer nis within a net */
522         __u32                   lpni_seq;
523         /* sequence number used to round robin over gateways */
524         __u32                   lpni_gw_seq;
525         /* health flag */
526         bool                    lpni_healthy;
527         /* returned RC ping features. Protected with lpni_lock */
528         unsigned int            lpni_ping_feats;
529         /* routes on this peer */
530         struct list_head        lpni_routes;
531         /* preferred local nids: if only one, use lpni_pref.nid */
532         union lpni_pref {
533                 lnet_nid_t      nid;
534                 lnet_nid_t      *nids;
535         } lpni_pref;
536         /* number of preferred NIDs in lnpi_pref_nids */
537         __u32                   lpni_pref_nnids;
538         /* router checker state */
539         struct lnet_rc_data     *lpni_rcd;
540 };
541
542 /* Preferred path added due to traffic on non-MR peer_ni */
543 #define LNET_PEER_NI_NON_MR_PREF        (1 << 0)
544
545 struct lnet_peer {
546         /* chain on pt_peer_list */
547         struct list_head        lp_peer_list;
548
549         /* list of peer nets */
550         struct list_head        lp_peer_nets;
551
552         /* list of messages pending discovery*/
553         struct list_head        lp_dc_pendq;
554
555         /* primary NID of the peer */
556         lnet_nid_t              lp_primary_nid;
557
558         /* CPT of peer_table */
559         int                     lp_cpt;
560
561         /* number of NIDs on this peer */
562         int                     lp_nnis;
563
564         /* reference count */
565         atomic_t                lp_refcount;
566
567         /* lock protecting peer state flags */
568         spinlock_t              lp_lock;
569
570         /* peer state flags */
571         unsigned                lp_state;
572
573         /* buffer for data pushed by peer */
574         struct lnet_ping_buffer *lp_data;
575
576         /* MD handle for ping in progress */
577         lnet_handle_md_t        lp_ping_mdh;
578
579         /* MD handle for push in progress */
580         lnet_handle_md_t        lp_push_mdh;
581
582         /* number of NIDs for sizing push data */
583         int                     lp_data_nnis;
584
585         /* NI config sequence number of peer */
586         __u32                   lp_peer_seqno;
587
588         /* Local NI config sequence number acked by peer */
589         __u32                   lp_node_seqno;
590
591         /* Local NI config sequence number sent to peer */
592         __u32                   lp_node_seqno_sent;
593
594         /* Ping error encountered during discovery. */
595         int                     lp_ping_error;
596
597         /* Push error encountered during discovery. */
598         int                     lp_push_error;
599
600         /* Error encountered during discovery. */
601         int                     lp_dc_error;
602
603         /* time it was put on the ln_dc_working queue */
604         time64_t                lp_last_queued;
605
606         /* link on discovery-related lists */
607         struct list_head        lp_dc_list;
608
609         /* tasks waiting on discovery of this peer */
610         wait_queue_head_t       lp_dc_waitq;
611 };
612
613 /*
614  * The status flags in lp_state. Their semantics have chosen so that
615  * lp_state can be zero-initialized.
616  *
617  * A peer is marked MULTI_RAIL in two cases: it was configured using DLC
618  * as multi-rail aware, or the LNET_PING_FEAT_MULTI_RAIL bit was set.
619  *
620  * A peer is marked NO_DISCOVERY if the LNET_PING_FEAT_DISCOVERY bit was
621  * NOT set when the peer was pinged by discovery.
622  */
623 #define LNET_PEER_MULTI_RAIL    (1 << 0)        /* Multi-rail aware */
624 #define LNET_PEER_NO_DISCOVERY  (1 << 1)        /* Peer disabled discovery */
625 /*
626  * A peer is marked CONFIGURED if it was configured by DLC.
627  *
628  * In addition, a peer is marked DISCOVERED if it has fully passed
629  * through Peer Discovery.
630  *
631  * When Peer Discovery is disabled, the discovery thread will mark
632  * peers REDISCOVER to indicate that they should be re-examined if
633  * discovery is (re)enabled on the node.
634  *
635  * A peer that was created as the result of inbound traffic will not
636  * be marked at all.
637  */
638 #define LNET_PEER_CONFIGURED    (1 << 2)        /* Configured via DLC */
639 #define LNET_PEER_DISCOVERED    (1 << 3)        /* Peer was discovered */
640 #define LNET_PEER_REDISCOVER    (1 << 4)        /* Discovery was disabled */
641 /*
642  * A peer is marked DISCOVERING when discovery is in progress.
643  * The other flags below correspond to stages of discovery.
644  */
645 #define LNET_PEER_DISCOVERING   (1 << 5)        /* Discovering */
646 #define LNET_PEER_DATA_PRESENT  (1 << 6)        /* Remote peer data present */
647 #define LNET_PEER_NIDS_UPTODATE (1 << 7)        /* Remote peer info uptodate */
648 #define LNET_PEER_PING_SENT     (1 << 8)        /* Waiting for REPLY to Ping */
649 #define LNET_PEER_PUSH_SENT     (1 << 9)        /* Waiting for ACK of Push */
650 #define LNET_PEER_PING_FAILED   (1 << 10)       /* Ping send failure */
651 #define LNET_PEER_PUSH_FAILED   (1 << 11)       /* Push send failure */
652 /*
653  * A ping can be forced as a way to fix up state, or as a manual
654  * intervention by an admin.
655  * A push can be forced in circumstances that would normally not
656  * allow for one to happen.
657  */
658 #define LNET_PEER_FORCE_PING    (1 << 12)       /* Forced Ping */
659 #define LNET_PEER_FORCE_PUSH    (1 << 13)       /* Forced Push */
660
661 struct lnet_peer_net {
662         /* chain on lp_peer_nets */
663         struct list_head        lpn_peer_nets;
664
665         /* list of peer_nis on this network */
666         struct list_head        lpn_peer_nis;
667
668         /* pointer to the peer I'm part of */
669         struct lnet_peer        *lpn_peer;
670
671         /* Net ID */
672         __u32                   lpn_net_id;
673
674         /* reference count */
675         atomic_t                lpn_refcount;
676 };
677
678 /* peer hash size */
679 #define LNET_PEER_HASH_BITS     9
680 #define LNET_PEER_HASH_SIZE     (1 << LNET_PEER_HASH_BITS)
681
682 /*
683  * peer hash table - one per CPT
684  *
685  * protected by lnet_net_lock/EX for update
686  *    pt_version
687  *    pt_number
688  *    pt_hash[...]
689  *    pt_peer_list
690  *    pt_peers
691  * protected by pt_zombie_lock:
692  *    pt_zombie_list
693  *    pt_zombies
694  *
695  * pt_zombie lock nests inside lnet_net_lock
696  */
697 struct lnet_peer_table {
698         int                     pt_version;     /* /proc validity stamp */
699         int                     pt_number;      /* # peers_ni extant */
700         struct list_head        *pt_hash;       /* NID->peer hash */
701         struct list_head        pt_peer_list;   /* peers */
702         int                     pt_peers;       /* # peers */
703         struct list_head        pt_zombie_list; /* zombie peer_ni */
704         int                     pt_zombies;     /* # zombie peers_ni */
705         spinlock_t              pt_zombie_lock; /* protect list and count */
706 };
707
708 /* peer aliveness is enabled only on routers for peers in a network where the
709  * struct lnet_ni::ni_peertimeout has been set to a positive value
710  */
711 #define lnet_peer_aliveness_enabled(lp) (the_lnet.ln_routing != 0 && \
712                                         ((lp)->lpni_net) && \
713                                         (lp)->lpni_net->net_tunables.lct_peer_timeout > 0)
714
715 typedef struct lnet_route {
716         struct list_head        lr_list;        /* chain on net */
717         struct list_head        lr_gwlist;      /* chain on gateway */
718         struct lnet_peer_ni     *lr_gateway;    /* router node */
719         __u32                   lr_net;         /* remote network number */
720         int                     lr_seq;         /* sequence for round-robin */
721         unsigned int            lr_downis;      /* number of down NIs */
722         __u32                   lr_hops;        /* how far I am */
723         unsigned int            lr_priority;    /* route priority */
724 } lnet_route_t;
725
726 #define LNET_REMOTE_NETS_HASH_DEFAULT   (1U << 7)
727 #define LNET_REMOTE_NETS_HASH_MAX       (1U << 16)
728 #define LNET_REMOTE_NETS_HASH_SIZE      (1 << the_lnet.ln_remote_nets_hbits)
729
730 typedef struct lnet_remotenet {
731         /* chain on ln_remote_nets_hash */
732         struct list_head        lrn_list;
733         /* routes to me */
734         struct list_head        lrn_routes;
735         /* my net number */
736         __u32                   lrn_net;
737 } lnet_remotenet_t;
738
739 /** lnet message has credit and can be submitted to lnd for send/receive */
740 #define LNET_CREDIT_OK          0
741 /** lnet message is waiting for credit */
742 #define LNET_CREDIT_WAIT        1
743 /** lnet message is waiting for discovery */
744 #define LNET_DC_WAIT            2
745
746 typedef struct lnet_rtrbufpool {
747         /* my free buffer pool */
748         struct list_head        rbp_bufs;
749         /* messages blocking for a buffer */
750         struct list_head        rbp_msgs;
751         /* # pages in each buffer */
752         int                     rbp_npages;
753         /* requested number of buffers */
754         int                     rbp_req_nbuffers;
755         /* # buffers actually allocated */
756         int                     rbp_nbuffers;
757         /* # free buffers / blocked messages */
758         int                     rbp_credits;
759         /* low water mark */
760         int                     rbp_mincredits;
761 } lnet_rtrbufpool_t;
762
763 typedef struct lnet_rtrbuf {
764         struct list_head         rb_list;       /* chain on rbp_bufs */
765         struct lnet_rtrbufpool  *rb_pool;       /* owning pool */
766         lnet_kiov_t              rb_kiov[0];    /* the buffer space */
767 } lnet_rtrbuf_t;
768
769 #define LNET_PEER_HASHSIZE   503                /* prime! */
770
771 enum lnet_match_flags {
772         /* Didn't match anything */
773         LNET_MATCHMD_NONE       = (1 << 0),
774         /* Matched OK */
775         LNET_MATCHMD_OK         = (1 << 1),
776         /* Must be discarded */
777         LNET_MATCHMD_DROP       = (1 << 2),
778         /* match and buffer is exhausted */
779         LNET_MATCHMD_EXHAUSTED  = (1 << 3),
780         /* match or drop */
781         LNET_MATCHMD_FINISH     = (LNET_MATCHMD_OK | LNET_MATCHMD_DROP),
782 };
783
784 /* Options for struct lnet_portal::ptl_options */
785 #define LNET_PTL_LAZY               (1 << 0)
786 #define LNET_PTL_MATCH_UNIQUE       (1 << 1)    /* unique match, for RDMA */
787 #define LNET_PTL_MATCH_WILDCARD     (1 << 2)    /* wildcard match, request portal */
788
789 /* parameter for matching operations (GET, PUT) */
790 struct lnet_match_info {
791         __u64                   mi_mbits;
792         struct lnet_process_id  mi_id;
793         unsigned int            mi_cpt;
794         unsigned int            mi_opc;
795         unsigned int            mi_portal;
796         unsigned int            mi_rlength;
797         unsigned int            mi_roffset;
798 };
799
800 /* ME hash of RDMA portal */
801 #define LNET_MT_HASH_BITS               8
802 #define LNET_MT_HASH_SIZE               (1 << LNET_MT_HASH_BITS)
803 #define LNET_MT_HASH_MASK               (LNET_MT_HASH_SIZE - 1)
804 /* we allocate (LNET_MT_HASH_SIZE + 1) entries for lnet_match_table::mt_hash,
805  * the last entry is reserved for MEs with ignore-bits */
806 #define LNET_MT_HASH_IGNORE             LNET_MT_HASH_SIZE
807 /* __u64 has 2^6 bits, so need 2^(LNET_MT_HASH_BITS - LNET_MT_BITS_U64) which
808  * is 4 __u64s as bit-map, and add an extra __u64 (only use one bit) for the
809  * ME-list with ignore-bits, which is mtable::mt_hash[LNET_MT_HASH_IGNORE] */
810 #define LNET_MT_BITS_U64                6       /* 2^6 bits */
811 #define LNET_MT_EXHAUSTED_BITS          (LNET_MT_HASH_BITS - LNET_MT_BITS_U64)
812 #define LNET_MT_EXHAUSTED_BMAP          ((1 << LNET_MT_EXHAUSTED_BITS) + 1)
813
814 /* portal match table */
815 struct lnet_match_table {
816         /* reserved for upcoming patches, CPU partition ID */
817         unsigned int            mt_cpt;
818         unsigned int            mt_portal;      /* portal index */
819         /* match table is set as "enabled" if there's non-exhausted MD
820          * attached on mt_mhash, it's only valid for wildcard portal */
821         unsigned int            mt_enabled;
822         /* bitmap to flag whether MEs on mt_hash are exhausted or not */
823         __u64                   mt_exhausted[LNET_MT_EXHAUSTED_BMAP];
824         struct list_head        *mt_mhash;      /* matching hash */
825 };
826
827 /* these are only useful for wildcard portal */
828 /* Turn off message rotor for wildcard portals */
829 #define LNET_PTL_ROTOR_OFF      0
830 /* round-robin dispatch all PUT messages for wildcard portals */
831 #define LNET_PTL_ROTOR_ON       1
832 /* round-robin dispatch routed PUT message for wildcard portals */
833 #define LNET_PTL_ROTOR_RR_RT    2
834 /* dispatch routed PUT message by hashing source NID for wildcard portals */
835 #define LNET_PTL_ROTOR_HASH_RT  3
836
837 typedef struct lnet_portal {
838         spinlock_t              ptl_lock;
839         unsigned int            ptl_index;      /* portal ID, reserved */
840         /* flags on this portal: lazy, unique... */
841         unsigned int            ptl_options;
842         /* list of messages which are stealing buffer */
843         struct list_head        ptl_msg_stealing;
844         /* messages blocking for MD */
845         struct list_head        ptl_msg_delayed;
846         /* Match table for each CPT */
847         struct lnet_match_table **ptl_mtables;
848         /* spread rotor of incoming "PUT" */
849         unsigned int            ptl_rotor;
850         /* # active entries for this portal */
851         int                     ptl_mt_nmaps;
852         /* array of active entries' cpu-partition-id */
853         int                     ptl_mt_maps[0];
854 } lnet_portal_t;
855
856 #define LNET_LH_HASH_BITS       12
857 #define LNET_LH_HASH_SIZE       (1ULL << LNET_LH_HASH_BITS)
858 #define LNET_LH_HASH_MASK       (LNET_LH_HASH_SIZE - 1)
859
860 /* resource container (ME, MD, EQ) */
861 struct lnet_res_container {
862         unsigned int            rec_type;       /* container type */
863         __u64                   rec_lh_cookie;  /* cookie generator */
864         struct list_head        rec_active;     /* active resource list */
865         struct list_head        *rec_lh_hash;   /* handle hash */
866 };
867
868 /* message container */
869 struct lnet_msg_container {
870         int                     msc_init;       /* initialized or not */
871         /* max # threads finalizing */
872         int                     msc_nfinalizers;
873         /* msgs waiting to complete finalizing */
874         struct list_head        msc_finalizing;
875         struct list_head        msc_active;     /* active message list */
876         /* threads doing finalization */
877         void                    **msc_finalizers;
878 };
879
880 /* Peer Discovery states */
881 #define LNET_DC_STATE_SHUTDOWN          0       /* not started */
882 #define LNET_DC_STATE_RUNNING           1       /* started up OK */
883 #define LNET_DC_STATE_STOPPING          2       /* telling thread to stop */
884
885 /* Router Checker states */
886 #define LNET_RC_STATE_SHUTDOWN          0       /* not started */
887 #define LNET_RC_STATE_RUNNING           1       /* started up OK */
888 #define LNET_RC_STATE_STOPPING          2       /* telling thread to stop */
889
890 /* LNet states */
891 #define LNET_STATE_SHUTDOWN             0       /* not started */
892 #define LNET_STATE_RUNNING              1       /* started up OK */
893 #define LNET_STATE_STOPPING             2       /* telling thread to stop */
894
895 typedef struct lnet {
896         /* CPU partition table of LNet */
897         struct cfs_cpt_table            *ln_cpt_table;
898         /* number of CPTs in ln_cpt_table */
899         unsigned int                    ln_cpt_number;
900         unsigned int                    ln_cpt_bits;
901
902         /* protect LNet resources (ME/MD/EQ) */
903         struct cfs_percpt_lock          *ln_res_lock;
904         /* # portals */
905         int                             ln_nportals;
906         /* the vector of portals */
907         struct lnet_portal              **ln_portals;
908         /* percpt ME containers */
909         struct lnet_res_container       **ln_me_containers;
910         /* percpt MD container */
911         struct lnet_res_container       **ln_md_containers;
912
913         /* Event Queue container */
914         struct lnet_res_container       ln_eq_container;
915         wait_queue_head_t               ln_eq_waitq;
916         spinlock_t                      ln_eq_wait_lock;
917
918         unsigned int                    ln_remote_nets_hbits;
919
920         /* protect NI, peer table, credits, routers, rtrbuf... */
921         struct cfs_percpt_lock          *ln_net_lock;
922         /* percpt message containers for active/finalizing/freed message */
923         struct lnet_msg_container       **ln_msg_containers;
924         struct lnet_counters            **ln_counters;
925         struct lnet_peer_table          **ln_peer_tables;
926         /* list of peer nis not on a local network */
927         struct list_head                ln_remote_peer_ni_list;
928         /* failure simulation */
929         struct list_head                ln_test_peers;
930         struct list_head                ln_drop_rules;
931         struct list_head                ln_delay_rules;
932         /* LND instances */
933         struct list_head                ln_nets;
934         /* the loopback NI */
935         struct lnet_ni                  *ln_loni;
936         /* network zombie list */
937         struct list_head                ln_net_zombie;
938         /* resend messages list */
939         struct list_head                ln_msg_resend;
940         /* spin lock to protect the msg resend list */
941         spinlock_t                      ln_msg_resend_lock;
942
943         /* remote networks with routes to them */
944         struct list_head                *ln_remote_nets_hash;
945         /* validity stamp */
946         __u64                           ln_remote_nets_version;
947         /* list of all known routers */
948         struct list_head                ln_routers;
949         /* validity stamp */
950         __u64                           ln_routers_version;
951         /* percpt router buffer pools */
952         struct lnet_rtrbufpool          **ln_rtrpools;
953
954         /*
955          * Ping target / Push source
956          *
957          * The ping target and push source share a single buffer. The
958          * ln_ping_target is protected against concurrent updates by
959          * ln_api_mutex.
960          */
961         struct lnet_handle_md           ln_ping_target_md;
962         struct lnet_handle_eq           ln_ping_target_eq;
963         struct lnet_ping_buffer         *ln_ping_target;
964         atomic_t                        ln_ping_target_seqno;
965
966         /*
967          * Push Target
968          *
969          * ln_push_nnis contains the desired size of the push target.
970          * The lnet_net_lock is used to handle update races. The old
971          * buffer may linger a while after it has been unlinked, in
972          * which case the event handler cleans up.
973          */
974         lnet_handle_eq_t                ln_push_target_eq;
975         lnet_handle_md_t                ln_push_target_md;
976         struct lnet_ping_buffer         *ln_push_target;
977         int                             ln_push_target_nnis;
978
979         /* discovery event queue handle */
980         lnet_handle_eq_t                ln_dc_eqh;
981         /* discovery requests */
982         struct list_head                ln_dc_request;
983         /* discovery working list */
984         struct list_head                ln_dc_working;
985         /* discovery expired list */
986         struct list_head                ln_dc_expired;
987         /* discovery thread wait queue */
988         wait_queue_head_t               ln_dc_waitq;
989         /* discovery startup/shutdown state */
990         int                             ln_dc_state;
991
992         /* router checker startup/shutdown state */
993         int                             ln_rc_state;
994         /* router checker's event queue */
995         struct lnet_handle_eq           ln_rc_eqh;
996         /* rcd still pending on net */
997         struct list_head                ln_rcd_deathrow;
998         /* rcd ready for free */
999         struct list_head                ln_rcd_zombie;
1000         /* serialise startup/shutdown */
1001         struct semaphore                ln_rc_signal;
1002
1003         struct mutex                    ln_api_mutex;
1004         struct mutex                    ln_lnd_mutex;
1005         /* Have I called LNetNIInit myself? */
1006         int                             ln_niinit_self;
1007         /* LNetNIInit/LNetNIFini counter */
1008         int                             ln_refcount;
1009         /* SHUTDOWN/RUNNING/STOPPING */
1010         int                             ln_state;
1011
1012         int                             ln_routing;     /* am I a router? */
1013         lnet_pid_t                      ln_pid;         /* requested pid */
1014         /* uniquely identifies this ni in this epoch */
1015         __u64                           ln_interface_cookie;
1016         /* registered LNDs */
1017         struct list_head                ln_lnds;
1018
1019         /* test protocol compatibility flags */
1020         int                             ln_testprotocompat;
1021
1022         /* 0 - load the NIs from the mod params
1023          * 1 - do not load the NIs from the mod params
1024          * Reverse logic to ensure that other calls to LNetNIInit
1025          * need no change
1026          */
1027         bool                            ln_nis_from_mod_params;
1028
1029         /* waitq for router checker.  As long as there are no routes in
1030          * the list, the router checker will sleep on this queue.  when
1031          * routes are added the thread will wake up */
1032         wait_queue_head_t               ln_rc_waitq;
1033 } lnet_t;
1034
1035 #endif