Whamcloud - gitweb
LU-10391 lnet: allow ping packet to contain large nids
[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  *
31  * lnet/include/lnet/lib-types.h
32  *
33  * Types used by the library side routines that do not need to be
34  * exposed to the user application
35  */
36
37 #ifndef __LNET_LIB_TYPES_H__
38 #define __LNET_LIB_TYPES_H__
39
40 #ifndef __KERNEL__
41 # error This include is only for kernel use.
42 #endif
43
44 #include <linux/kthread.h>
45 #include <linux/uio.h>
46 #include <linux/semaphore.h>
47 #include <linux/types.h>
48 #include <linux/kref.h>
49 #include <net/genetlink.h>
50
51 #include <uapi/linux/lnet/lnet-nl.h>
52 #include <uapi/linux/lnet/lnet-dlc.h>
53 #include <uapi/linux/lnet/lnetctl.h>
54 #include <uapi/linux/lnet/nidstr.h>
55
56 char *libcfs_nidstr_r(const struct lnet_nid *nid,
57                       char *buf, size_t buf_size);
58
59 static inline char *libcfs_nidstr(const struct lnet_nid *nid)
60 {
61         return libcfs_nidstr_r(nid, libcfs_next_nidstring(),
62                                LNET_NIDSTR_SIZE);
63 }
64
65 int libcfs_strnid(struct lnet_nid *nid, const char *str);
66 char *libcfs_idstr(struct lnet_processid *id);
67
68 int cfs_match_nid_net(struct lnet_nid *nid, u32 net,
69                       struct list_head *net_num_list,
70                       struct list_head *addr);
71
72 /* Max payload size */
73 #define LNET_MAX_PAYLOAD        LNET_MTU
74
75 /** limit on the number of fragments in discontiguous MDs */
76 #define LNET_MAX_IOV    256
77
78 /*
79  * This is the maximum health value.
80  * All local and peer NIs created have their health default to this value.
81  */
82 #define LNET_MAX_HEALTH_VALUE 1000
83 #define LNET_MAX_SELECTION_PRIORITY UINT_MAX
84
85 /* forward refs */
86 struct lnet_libmd;
87
88 enum lnet_msg_hstatus {
89         LNET_MSG_STATUS_OK = 0,
90         LNET_MSG_STATUS_LOCAL_INTERRUPT,
91         LNET_MSG_STATUS_LOCAL_DROPPED,
92         LNET_MSG_STATUS_LOCAL_ABORTED,
93         LNET_MSG_STATUS_LOCAL_NO_ROUTE,
94         LNET_MSG_STATUS_LOCAL_ERROR,
95         LNET_MSG_STATUS_LOCAL_TIMEOUT,
96         LNET_MSG_STATUS_REMOTE_ERROR,
97         LNET_MSG_STATUS_REMOTE_DROPPED,
98         LNET_MSG_STATUS_REMOTE_TIMEOUT,
99         LNET_MSG_STATUS_NETWORK_TIMEOUT,
100         LNET_MSG_STATUS_END,
101 };
102
103 struct lnet_rsp_tracker {
104         /* chain on the waiting list */
105         struct list_head rspt_on_list;
106         /* cpt to lock */
107         int rspt_cpt;
108         /* nid of next hop */
109         struct lnet_nid rspt_next_hop_nid;
110         /* deadline of the REPLY/ACK */
111         ktime_t rspt_deadline;
112         /* parent MD */
113         struct lnet_handle_md rspt_mdh;
114 };
115
116 struct lnet_msg {
117         struct list_head        msg_activelist;
118         struct list_head        msg_list;       /* Q for credits/MD */
119
120         struct lnet_processid   msg_target;
121         /* Primary NID of the source. */
122         struct lnet_nid         msg_initiator;
123         /* where is it from, it's only for building event */
124         struct lnet_nid         msg_from;
125         __u32                   msg_type;
126
127         /*
128          * hold parameters in case message is with held due
129          * to discovery
130          */
131         struct lnet_nid         msg_src_nid_param;
132         struct lnet_nid         msg_rtr_nid_param;
133
134         /*
135          * Deadline for the message after which it will be finalized if it
136          * has not completed.
137          */
138         ktime_t                 msg_deadline;
139
140         /* The message health status. */
141         enum lnet_msg_hstatus   msg_health_status;
142         /* This is a recovery message */
143         bool                    msg_recovery;
144         /* the number of times a transmission has been retried */
145         int                     msg_retry_count;
146         /* flag to indicate that we do not want to resend this message */
147         bool                    msg_no_resend;
148
149         /* committed for sending */
150         unsigned int            msg_tx_committed:1;
151         /* CPT # this message committed for sending */
152         unsigned int            msg_tx_cpt:15;
153         /* committed for receiving */
154         unsigned int            msg_rx_committed:1;
155         /* CPT # this message committed for receiving */
156         unsigned int            msg_rx_cpt:15;
157         /* queued for tx credit */
158         unsigned int            msg_tx_delayed:1;
159         /* queued for RX buffer */
160         unsigned int            msg_rx_delayed:1;
161         /* ready for pending on RX delay list */
162         unsigned int            msg_rx_ready_delay:1;
163
164         unsigned int          msg_vmflush:1;      /* VM trying to free memory */
165         unsigned int          msg_target_is_router:1; /* sending to a router */
166         unsigned int          msg_routing:1;      /* being forwarded */
167         unsigned int          msg_ack:1;          /* ack on finalize (PUT) */
168         unsigned int          msg_sending:1;      /* outgoing message */
169         unsigned int          msg_receiving:1;    /* being received */
170         unsigned int          msg_txcredit:1;     /* taken an NI send credit */
171         unsigned int          msg_peertxcredit:1; /* taken a peer send credit */
172         unsigned int          msg_rtrcredit:1;    /* taken a globel router credit */
173         unsigned int          msg_peerrtrcredit:1; /* taken a peer router credit */
174         unsigned int          msg_onactivelist:1; /* on the activelist */
175         unsigned int          msg_rdma_get:1;
176
177         struct lnet_peer_ni  *msg_txpeer;         /* peer I'm sending to */
178         struct lnet_peer_ni  *msg_rxpeer;         /* peer I received from */
179
180         void                 *msg_private;
181         struct lnet_libmd    *msg_md;
182         /* the NI the message was sent or received over */
183         struct lnet_ni       *msg_txni;
184         struct lnet_ni       *msg_rxni;
185
186         unsigned int          msg_len;
187         unsigned int          msg_wanted;
188         unsigned int          msg_offset;
189         unsigned int          msg_niov;
190         struct bio_vec       *msg_kiov;
191
192         struct lnet_event       msg_ev;
193         struct lnet_hdr         msg_hdr;
194 };
195
196 struct lnet_libhandle {
197         struct list_head        lh_hash_chain;
198         __u64                   lh_cookie;
199 };
200
201 #define lh_entry(ptr, type, member) \
202         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
203
204 struct lnet_me {
205         struct list_head        me_list;
206         int                     me_cpt;
207         struct lnet_processid   me_match_id;
208         unsigned int            me_portal;
209         unsigned int            me_pos;         /* hash offset in mt_hash */
210         __u64                   me_match_bits;
211         __u64                   me_ignore_bits;
212         enum lnet_unlink        me_unlink;
213         struct lnet_libmd      *me_md;
214 };
215
216 struct lnet_libmd {
217         struct list_head         md_list;
218         struct lnet_libhandle    md_lh;
219         struct lnet_me          *md_me;
220         char                    *md_start;
221         unsigned int             md_offset;
222         unsigned int             md_length;
223         unsigned int             md_max_size;
224         int                      md_threshold;
225         int                      md_refcount;
226         unsigned int             md_options;
227         unsigned int             md_flags;
228         unsigned int             md_niov;       /* # frags at end of struct */
229         void                    *md_user_ptr;
230         struct lnet_rsp_tracker *md_rspt_ptr;
231         lnet_handler_t           md_handler;
232         struct lnet_handle_md    md_bulk_handle;
233         struct bio_vec           md_kiov[LNET_MAX_IOV];
234 };
235
236 #define LNET_MD_FLAG_ZOMBIE      BIT(0)
237 #define LNET_MD_FLAG_AUTO_UNLINK BIT(1)
238 #define LNET_MD_FLAG_ABORTED     BIT(2)
239 /* LNET_MD_FLAG_HANDLING is set when a non-unlink event handler
240  * is being called for an event relating to the md.
241  * It ensures only one such handler runs at a time.
242  * The final "unlink" event is only called once the
243  * md_refcount has reached zero, and this flag has been cleared,
244  * ensuring that it doesn't race with any other event handler
245  * call.
246  */
247 #define LNET_MD_FLAG_HANDLING    BIT(3)
248 #define LNET_MD_FLAG_DISCARD     BIT(4)
249 #define LNET_MD_FLAG_GPU         BIT(5) /**< Special mapping needs */
250
251 struct lnet_test_peer {
252         /* info about peers we are trying to fail */
253         struct list_head        tp_list;        /* ln_test_peers */
254         struct lnet_nid         tp_nid;         /* matching nid */
255         unsigned int            tp_threshold;   /* # failures to simulate */
256 };
257
258 #define LNET_COOKIE_TYPE_MD    1
259 #define LNET_COOKIE_TYPE_ME    2
260 #define LNET_COOKIE_TYPE_EQ    3
261 #define LNET_COOKIE_TYPE_BITS  2
262 #define LNET_COOKIE_MASK        ((1ULL << LNET_COOKIE_TYPE_BITS) - 1ULL)
263
264 struct netstrfns {
265         u32     nf_type;
266         char    *nf_name;
267         char    *nf_modname;
268         void    (*nf_addr2str)(u32 addr, char *str, size_t size);
269         void    (*nf_addr2str_size)(const __be32 *addr, size_t asize,
270                                     char *str, size_t size);
271         int     (*nf_str2addr)(const char *str, int nob, u32 *addr);
272         int     (*nf_str2addr_size)(const char *str, int nob,
273                                     __be32 *addr, size_t *asize);
274         int     (*nf_parse_addrlist)(char *str, int len,
275                                      struct list_head *list);
276         int     (*nf_print_addrlist)(char *buffer, int count,
277                                      struct list_head *list);
278         int     (*nf_match_addr)(u32 addr, struct list_head *list);
279         int     (*nf_min_max)(struct list_head *nidlist, u32 *min_nid,
280                               u32 *max_nid);
281 };
282
283 struct lnet_ni;                                  /* forward ref */
284 struct socket;
285
286 struct lnet_lnd {
287         /* fields initialized by the LND */
288         __u32                   lnd_type;
289
290         int  (*lnd_startup)(struct lnet_ni *ni);
291         void (*lnd_shutdown)(struct lnet_ni *ni);
292         int  (*lnd_ctl)(struct lnet_ni *ni, unsigned int cmd, void *arg);
293
294         /* In data movement APIs below, payload buffers are described as a set
295          * of 'niov' fragments which are in pages.
296          * The LND may NOT overwrite these fragment descriptors.
297          * An 'offset' and may specify a byte offset within the set of
298          * fragments to start from
299          */
300
301         /* Start sending a preformatted message.  'private' is NULL for PUT and
302          * GET messages; otherwise this is a response to an incoming message
303          * and 'private' is the 'private' passed to lnet_parse().  Return
304          * non-zero for immediate failure, otherwise complete later with
305          * lnet_finalize() */
306         int (*lnd_send)(struct lnet_ni *ni, void *private,
307                         struct lnet_msg *msg);
308
309         /* Start receiving 'mlen' bytes of payload data, skipping the following
310          * 'rlen' - 'mlen' bytes. 'private' is the 'private' passed to
311          * lnet_parse().  Return non-zero for immedaite failure, otherwise
312          * complete later with lnet_finalize().  This also gives back a receive
313          * credit if the LND does flow control. */
314         int (*lnd_recv)(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
315                         int delayed, unsigned int niov,
316                         struct bio_vec *kiov,
317                         unsigned int offset, unsigned int mlen, unsigned int rlen);
318
319         /* lnet_parse() has had to delay processing of this message
320          * (e.g. waiting for a forwarding buffer or send credits).  Give the
321          * LND a chance to free urgently needed resources.  If called, return 0
322          * for success and do NOT give back a receive credit; that has to wait
323          * until lnd_recv() gets called.  On failure return < 0 and
324          * release resources; lnd_recv() will not be called. */
325         int (*lnd_eager_recv)(struct lnet_ni *ni, void *private,
326                               struct lnet_msg *msg, void **new_privatep);
327
328         /* notification of peer down */
329         void (*lnd_notify_peer_down)(struct lnet_nid *peer);
330
331         /* accept a new connection */
332         int (*lnd_accept)(struct lnet_ni *ni, struct socket *sock);
333
334         /* get dma_dev priority */
335         unsigned int (*lnd_get_dev_prio)(struct lnet_ni *ni,
336                                          unsigned int dev_idx);
337
338         /* Handle LND specific Netlink handling */
339         int (*lnd_nl_set)(int cmd, struct nlattr *attr, int type, void *data);
340
341         const struct ln_key_list *lnd_keys;
342 };
343
344 struct lnet_tx_queue {
345         int                     tq_credits;     /* # tx credits free */
346         int                     tq_credits_min; /* lowest it's been */
347         int                     tq_credits_max; /* total # tx credits */
348         struct list_head        tq_delayed;     /* delayed TXs */
349 };
350
351 enum lnet_net_state {
352         /* set when net block is allocated */
353         LNET_NET_STATE_INIT = 0,
354         /* set when NIs in net are started successfully */
355         LNET_NET_STATE_ACTIVE,
356         /* set if all NIs in net are in FAILED state */
357         LNET_NET_STATE_INACTIVE,
358         /* set when shutting down a NET */
359         LNET_NET_STATE_DELETING
360 };
361
362 enum lnet_ni_state {
363         /* initial state when NI is created */
364         LNET_NI_STATE_INIT = 0,
365         /* set when NI is brought up */
366         LNET_NI_STATE_ACTIVE,
367         /* set when NI is being shutdown */
368         LNET_NI_STATE_DELETING,
369 };
370
371 #define LNET_NI_RECOVERY_PENDING        BIT(0)
372 #define LNET_NI_RECOVERY_FAILED         BIT(1)
373
374 enum lnet_stats_type {
375         LNET_STATS_TYPE_SEND = 0,
376         LNET_STATS_TYPE_RECV,
377         LNET_STATS_TYPE_DROP
378 };
379
380 struct lnet_comm_count {
381         atomic_t co_get_count;
382         atomic_t co_put_count;
383         atomic_t co_reply_count;
384         atomic_t co_ack_count;
385         atomic_t co_hello_count;
386 };
387
388 struct lnet_element_stats {
389         struct lnet_comm_count el_send_stats;
390         struct lnet_comm_count el_recv_stats;
391         struct lnet_comm_count el_drop_stats;
392 };
393
394 struct lnet_health_local_stats {
395         atomic_t hlt_local_interrupt;
396         atomic_t hlt_local_dropped;
397         atomic_t hlt_local_aborted;
398         atomic_t hlt_local_no_route;
399         atomic_t hlt_local_timeout;
400         atomic_t hlt_local_error;
401 };
402
403 struct lnet_health_remote_stats {
404         atomic_t hlt_remote_dropped;
405         atomic_t hlt_remote_timeout;
406         atomic_t hlt_remote_error;
407         atomic_t hlt_network_timeout;
408 };
409
410 struct lnet_net {
411         /* chain on the ln_nets */
412         struct list_head        net_list;
413
414         /* net ID, which is composed of
415          * (net_type << 16) | net_num.
416          * net_type can be one of the enumerated types defined in
417          * lnet/include/lnet/nidstr.h */
418         __u32                   net_id;
419
420         /* round robin selection */
421         __u32                   net_seq;
422
423         /* total number of CPTs in the array */
424         __u32                   net_ncpts;
425
426         /* cumulative CPTs of all NIs in this net */
427         __u32                   *net_cpts;
428
429         /* relative net selection priority */
430         __u32                   net_sel_priority;
431
432         /* network tunables */
433         struct lnet_ioctl_config_lnd_cmn_tunables net_tunables;
434
435         /*
436          * boolean to indicate that the tunables have been set and
437          * shouldn't be reset
438          */
439         bool                    net_tunables_set;
440
441         /* procedural interface */
442         const struct lnet_lnd   *net_lnd;
443
444         /* list of NIs on this net */
445         struct list_head        net_ni_list;
446
447         /* list of NIs being added, but not started yet */
448         struct list_head        net_ni_added;
449
450         /* dying LND instances */
451         struct list_head        net_ni_zombie;
452
453         /* when I was last alive */
454         time64_t                net_last_alive;
455
456         /* protects access to net_last_alive */
457         spinlock_t              net_lock;
458
459         /* list of router nids preferred for this network */
460         struct list_head        net_rtr_pref_nids;
461 };
462
463 /* Normally Netlink atttributes are defined in UAPI headers but Lustre is
464  * different in that the ABI is in a constant state of change unlike other
465  * Netlink interfaces. LNet sends a special header to help user land handle
466  * the differences.
467  */
468
469 /** enum lnet_net_attrs               - LNet NI netlink properties
470  *                                      attributes that describe LNet 'NI'
471  *                                      These values are used to piece together
472  *                                      messages for sending and receiving.
473  *
474  * @LNET_NET_ATTR_UNSPEC:               unspecified attribute to catch errors
475  *
476  * @LNET_NET_ATTR_HDR:                  grouping for LNet net data (NLA_NESTED)
477  * @LNET_NET_ATTR_TYPE:                 LNet net this NI belongs to (NLA_STRING)
478  * @LNET_NET_ATTR_LOCAL:                Local NI information (NLA_NESTED)
479  */
480 enum lnet_net_attrs {
481         LNET_NET_ATTR_UNSPEC = 0,
482
483         LNET_NET_ATTR_HDR,
484         LNET_NET_ATTR_TYPE,
485         LNET_NET_ATTR_LOCAL,
486
487         __LNET_NET_ATTR_MAX_PLUS_ONE,
488 };
489
490 #define LNET_NET_ATTR_MAX (__LNET_NET_ATTR_MAX_PLUS_ONE - 1)
491
492 /** enum lnet_net_local_ni_attrs      - LNet local NI netlink properties
493  *                                      attributes that describe local NI
494  *
495  * @LNET_NET_LOCAL_NI_ATTR_UNSPEC:      unspecified attribute to catch errors
496  *
497  * @LNET_NET_LOCAL_NI_ATTR_NID:         NID that represents this NI (NLA_STRING)
498  * @LNET_NET_LOCAL_NI_ATTR_STATUS:      State of this NI (NLA_STRING)
499  * @LNET_NET_LOCAL_NI_ATTR_INTERFACE:   Defines physical devices (NLA_NESTED)
500  *                                      Used to be many devices but no longer.
501  */
502 enum lnet_net_local_ni_attrs {
503         LNET_NET_LOCAL_NI_ATTR_UNSPEC = 0,
504
505         LNET_NET_LOCAL_NI_ATTR_NID,
506         LNET_NET_LOCAL_NI_ATTR_STATUS,
507         LNET_NET_LOCAL_NI_ATTR_INTERFACE,
508
509         __LNET_NET_LOCAL_NI_ATTR_MAX_PLUS_ONE,
510 };
511
512 #define LNET_NET_LOCAL_NI_ATTR_MAX (__LNET_NET_LOCAL_NI_ATTR_MAX_PLUS_ONE - 1)
513
514 /** enum lnet_net_local_ni_intf_attrs - LNet NI device netlink properties
515  *                                      attribute that reports the device
516  *                                      in use
517  *
518  * @LNET_NET_LOCAL_NI_INTF_ATTR_UNSPEC: unspecified attribute to catch errors
519  *
520  * @LNET_NET_LOCAL_NI_INTF_ATTR_TYPE:   Physcial device interface (NLA_STRING)
521  */
522 enum lnet_net_local_ni_intf_attrs {
523         LNET_NET_LOCAL_NI_INTF_ATTR_UNSPEC = 0,
524
525         LNET_NET_LOCAL_NI_INTF_ATTR_TYPE,
526
527         __LNET_NET_LOCAL_NI_INTF_ATTR_MAX_PLUS_ONE,
528 };
529
530 #define LNET_NET_LOCAL_NI_INTF_ATTR_MAX (__LNET_NET_LOCAL_NI_INTF_ATTR_MAX_PLUS_ONE - 1)
531
532 /** enum lnet_net_local_ni_tunables_attrs             - LNet NI tunables
533  *                                                      netlink properties.
534  *                                                      Performance options
535  *                                                      for your NI.
536  *
537  * @LNET_NET_LOCAL_NI_TUNABLES_ATTR_UNSPEC:             unspecified attribute
538  *                                                      to catch errors
539  *
540  * @LNET_NET_LOCAL_NI_TUNABLES_ATTR_PEER_TIMEOUT:       Timeout for LNet peer.
541  *                                                      (NLA_S32)
542  * @LNET_NET_LOCAL_NI_TUNABLES_ATTR_PEER_CREDITS:       Credits for LNet peer.
543  *                                                      (NLA_S32)
544  * @LNET_NET_LOCAL_NI_TUNABLES_ATTR_PEER_BUFFER_CREDITS: Buffer credits for
545  *                                                       LNet peer. (NLA_S32)
546  * @LNET_NET_LOCAL_NI_TUNABLES_ATTR_CREDITS:            Credits for LNet peer
547  *                                                      TX. (NLA_S32)
548  */
549 enum lnet_net_local_ni_tunables_attr {
550         LNET_NET_LOCAL_NI_TUNABLES_ATTR_UNSPEC = 0,
551
552         LNET_NET_LOCAL_NI_TUNABLES_ATTR_PEER_TIMEOUT,
553         LNET_NET_LOCAL_NI_TUNABLES_ATTR_PEER_CREDITS,
554         LNET_NET_LOCAL_NI_TUNABLES_ATTR_PEER_BUFFER_CREDITS,
555         LNET_NET_LOCAL_NI_TUNABLES_ATTR_CREDITS,
556         __LNET_NET_LOCAL_NI_TUNABLES_ATTR_MAX_PLUS_ONE,
557 };
558
559 #define LNET_NET_LOCAL_NI_TUNABLES_ATTR_MAX (__LNET_NET_LOCAL_NI_TUNABLES_ATTR_MAX_PLUS_ONE - 1)
560
561 struct lnet_ni {
562         /* chain on the lnet_net structure */
563         struct list_head        ni_netlist;
564
565         /* chain on the recovery queue */
566         struct list_head        ni_recovery;
567
568         /* MD handle for recovery ping */
569         struct lnet_handle_md   ni_ping_mdh;
570
571         spinlock_t              ni_lock;
572
573         /* number of CPTs */
574         int                     ni_ncpts;
575
576         /* bond NI on some CPTs */
577         __u32                   *ni_cpts;
578
579         /* interface's NID */
580         struct lnet_nid         ni_nid;
581
582         /* instance-specific data */
583         void                    *ni_data;
584
585         /* per ni credits */
586         atomic_t                ni_tx_credits;
587
588         /* percpt TX queues */
589         struct lnet_tx_queue    **ni_tx_queues;
590
591         /* percpt reference count */
592         int                     **ni_refs;
593
594         /* pointer to parent network */
595         struct lnet_net         *ni_net;
596
597         /* my health status */
598         u32                     *ni_status;
599
600         /* NI FSM. Protected by lnet_ni_lock() */
601         enum lnet_ni_state      ni_state;
602
603         /* Recovery state. Protected by lnet_ni_lock() */
604         __u32                   ni_recovery_state;
605
606         /* When to send the next recovery ping */
607         time64_t                ni_next_ping;
608         /* How many pings sent during current recovery period did not receive
609          * a reply. NB: reset whenever _any_ message arrives on this NI
610          */
611         unsigned int            ni_ping_count;
612
613         /* per NI LND tunables */
614         struct lnet_lnd_tunables ni_lnd_tunables;
615
616         /* lnd tunables set explicitly */
617         bool ni_lnd_tunables_set;
618
619         /* NI statistics */
620         struct lnet_element_stats ni_stats;
621         struct lnet_health_local_stats ni_hstats;
622
623         /* physical device CPT */
624         int                     ni_dev_cpt;
625
626         /* sequence number used to round robin over nis within a net */
627         __u32                   ni_seq;
628
629         /*
630          * health value
631          *      initialized to LNET_MAX_HEALTH_VALUE
632          * Value is decremented every time we fail to send a message over
633          * this NI because of a NI specific failure.
634          * Value is incremented if we successfully send a message.
635          */
636         atomic_t                ni_healthv;
637
638         /*
639          * Set to 1 by the LND when it receives an event telling it the device
640          * has gone into a fatal state. Set to 0 when the LND receives an
641          * even telling it the device is back online.
642          */
643         atomic_t                ni_fatal_error_on;
644
645         /* the relative selection priority of this NI */
646         __u32                   ni_sel_priority;
647
648         /*
649          * equivalent interface to use
650          */
651         char                    *ni_interface;
652         struct net              *ni_net_ns;     /* original net namespace */
653 };
654
655 #define LNET_PROTO_PING_MATCHBITS       0x8000000000000000LL
656
657 /*
658  * Descriptor of a ping info buffer: keep a separate indicator of the
659  * size and a reference count. The type is used both as a source and
660  * sink of data, so we need to keep some information outside of the
661  * area that may be overwritten by network data.
662  */
663 struct lnet_ping_buffer {
664         int                     pb_nbytes;      /* sizeof pb_info */
665         atomic_t                pb_refcnt;
666         bool                    pb_needs_post;
667         struct lnet_ping_info   pb_info;
668 };
669
670 #define LNET_PING_BUFFER_SIZE(bytes) \
671         (offsetof(struct lnet_ping_buffer, pb_info) + bytes)
672 #define LNET_PING_BUFFER_LONI(PBUF)     ((PBUF)->pb_info.pi_ni[0].ns_nid)
673 #define LNET_PING_BUFFER_SEQNO(PBUF)    ((PBUF)->pb_info.pi_ni[0].ns_status)
674
675 #define LNET_PING_INFO_TO_BUFFER(PINFO) \
676         container_of((PINFO), struct lnet_ping_buffer, pb_info)
677
678 static inline int
679 lnet_ping_sts_size(const struct lnet_nid *nid)
680 {
681         int size;
682
683         if (nid_is_nid4(nid))
684                 return sizeof(struct lnet_ni_status);
685
686         size = offsetof(struct lnet_ni_large_status, ns_nid) +
687                NID_BYTES(nid);
688
689         return round_up(size, 4);
690 }
691
692 static inline struct lnet_ni_large_status *
693 lnet_ping_sts_next(const struct lnet_ni_large_status *nis)
694 {
695         return (void *)nis + lnet_ping_sts_size(&nis->ns_nid);
696 }
697
698 static inline bool
699 lnet_ping_at_least_two_entries(const struct lnet_ping_info *pi)
700 {
701         /* Return true if we have at lease two entries.  There is always a
702          * least one, a 4-byte lo0 interface.
703          */
704         struct lnet_ni_large_status *lns;
705
706         if ((pi->pi_features & LNET_PING_FEAT_LARGE_ADDR) == 0)
707                 return pi->pi_nnis <= 2;
708         /* There is at least 1 large-address entry */
709         if (pi->pi_nnis != 1)
710                 return false;
711         lns = (void *)&pi->pi_ni[1];
712         lns = lnet_ping_sts_next(lns);
713
714         return ((void *)pi + lnet_ping_info_size(pi) <= (void *)lns);
715 }
716
717 struct lnet_nid_list {
718         struct list_head nl_list;
719         struct lnet_nid nl_nid;
720 };
721
722 struct lnet_peer_ni {
723         /* chain on lpn_peer_nis */
724         struct list_head        lpni_peer_nis;
725         /* chain on remote peer list */
726         struct list_head        lpni_on_remote_peer_ni_list;
727         /* chain on recovery queue */
728         struct list_head        lpni_recovery;
729         /* chain on peer hash */
730         struct list_head        lpni_hashlist;
731         /* messages blocking for tx credits */
732         struct list_head        lpni_txq;
733         /* pointer to peer net I'm part of */
734         struct lnet_peer_net    *lpni_peer_net;
735         /* statistics kept on each peer NI */
736         struct lnet_element_stats lpni_stats;
737         struct lnet_health_remote_stats lpni_hstats;
738         /* spin lock protecting credits and lpni_txq */
739         spinlock_t              lpni_lock;
740         /* # tx credits available */
741         int                     lpni_txcredits;
742         /* low water mark */
743         int                     lpni_mintxcredits;
744         /*
745          * Each peer_ni in a gateway maintains its own credits. This
746          * allows more traffic to gateways that have multiple interfaces.
747          */
748         /* # router credits */
749         int                     lpni_rtrcredits;
750         /* low water mark */
751         int                     lpni_minrtrcredits;
752         /* bytes queued for sending */
753         long                    lpni_txqnob;
754         /* network peer is on */
755         struct lnet_net         *lpni_net;
756         /* peer's NID */
757         struct lnet_nid         lpni_nid;
758         /* # refs */
759         struct kref             lpni_kref;
760         /* health value for the peer */
761         atomic_t                lpni_healthv;
762         /* recovery ping mdh */
763         struct lnet_handle_md   lpni_recovery_ping_mdh;
764         /* When to send the next recovery ping */
765         time64_t                lpni_next_ping;
766         /* How many pings sent during current recovery period did not receive
767          * a reply. NB: reset whenever _any_ message arrives from this peer NI
768          */
769         unsigned int            lpni_ping_count;
770         /* CPT this peer attached on */
771         int                     lpni_cpt;
772         /* state flags -- protected by lpni_lock */
773         unsigned                lpni_state;
774         /* status of the peer NI as reported by the peer */
775         __u32                   lpni_ns_status;
776         /* sequence number used to round robin over peer nis within a net */
777         __u32                   lpni_seq;
778         /* sequence number used to round robin over gateways */
779         __u32                   lpni_gw_seq;
780         /* returned RC ping features. Protected with lpni_lock */
781         unsigned int            lpni_ping_feats;
782         /* time last message was received from the peer */
783         time64_t                lpni_last_alive;
784         /* preferred local nids: if only one, use lpni_pref.nid */
785         union lpni_pref {
786                 struct lnet_nid nid;
787                 struct list_head nids;
788         } lpni_pref;
789         /* list of router nids preferred for this peer NI */
790         struct list_head        lpni_rtr_pref_nids;
791         /* The relative selection priority of this peer NI */
792         __u32                   lpni_sel_priority;
793         /* number of preferred NIDs in lnpi_pref_nids */
794         __u32                   lpni_pref_nnids;
795 };
796
797 /* Preferred path added due to traffic on non-MR peer_ni */
798 #define LNET_PEER_NI_NON_MR_PREF        BIT(0)
799 /* peer is being recovered. */
800 #define LNET_PEER_NI_RECOVERY_PENDING   BIT(1)
801 /* recovery ping failed */
802 #define LNET_PEER_NI_RECOVERY_FAILED    BIT(2)
803 /* peer is being deleted */
804 #define LNET_PEER_NI_DELETING           BIT(3)
805
806 struct lnet_peer {
807         /* chain on pt_peer_list */
808         struct list_head        lp_peer_list;
809
810         /* list of peer nets */
811         struct list_head        lp_peer_nets;
812
813         /* list of messages pending discovery*/
814         struct list_head        lp_dc_pendq;
815
816         /* chain on router list */
817         struct list_head        lp_rtr_list;
818
819         /* primary NID of the peer */
820         struct lnet_nid         lp_primary_nid;
821
822         /* source NID to use during discovery */
823         struct lnet_nid         lp_disc_src_nid;
824         /* destination NID to use during discovery */
825         struct lnet_nid         lp_disc_dst_nid;
826
827         /* net to perform discovery on */
828         __u32                   lp_disc_net_id;
829
830         /* CPT of peer_table */
831         int                     lp_cpt;
832
833         /* number of NIDs on this peer */
834         int                     lp_nnis;
835
836         /* # refs from lnet_route::lr_gateway */
837         int                     lp_rtr_refcount;
838
839         /*
840          * peer specific health sensitivity value to decrement peer nis in
841          * this peer with if set to something other than 0
842          */
843         __u32                   lp_health_sensitivity;
844
845         /* messages blocking for router credits */
846         struct list_head        lp_rtrq;
847
848         /* routes on this peer */
849         struct list_head        lp_routes;
850
851         /* reference count */
852         atomic_t                lp_refcount;
853
854         /* lock protecting peer state flags and lpni_rtrq */
855         spinlock_t              lp_lock;
856
857         /* peer state flags */
858         unsigned                lp_state;
859
860         /* buffer for data pushed by peer */
861         struct lnet_ping_buffer *lp_data;
862
863         /* MD handle for ping in progress */
864         struct lnet_handle_md   lp_ping_mdh;
865
866         /* MD handle for push in progress */
867         struct lnet_handle_md   lp_push_mdh;
868
869         /* number of bytes for sizing pb_info in push data */
870         int                     lp_data_bytes;
871
872         /* NI config sequence number of peer */
873         __u32                   lp_peer_seqno;
874
875         /* Local NI config sequence number acked by peer */
876         __u32                   lp_node_seqno;
877
878         /* Local NI config sequence number sent to peer */
879         __u32                   lp_node_seqno_sent;
880
881         /* Ping error encountered during discovery. */
882         int                     lp_ping_error;
883
884         /* Push error encountered during discovery. */
885         int                     lp_push_error;
886
887         /* Error encountered during discovery. */
888         int                     lp_dc_error;
889
890         /* time it was put on the ln_dc_working queue */
891         time64_t                lp_last_queued;
892
893         /* link on discovery-related lists */
894         struct list_head        lp_dc_list;
895
896         /* tasks waiting on discovery of this peer */
897         wait_queue_head_t       lp_dc_waitq;
898
899         /* cached peer aliveness */
900         bool                    lp_alive;
901
902         /* sequence number used to round robin traffic to this peer's
903          * nets/NIs
904          */
905         __u32                   lp_send_seq;
906 };
907
908 /*
909  * The status flags in lp_state. Their semantics have chosen so that
910  * lp_state can be zero-initialized.
911  *
912  * A peer is marked MULTI_RAIL in two cases: it was configured using DLC
913  * as multi-rail aware, or the LNET_PING_FEAT_MULTI_RAIL bit was set.
914  *
915  * A peer is marked NO_DISCOVERY if the LNET_PING_FEAT_DISCOVERY bit was
916  * NOT set when the peer was pinged by discovery.
917  *
918  * A peer is marked ROUTER if it indicates so in the feature bit.
919  */
920 #define LNET_PEER_MULTI_RAIL            BIT(0)  /* Multi-rail aware */
921 #define LNET_PEER_NO_DISCOVERY          BIT(1)  /* Peer disabled discovery */
922 #define LNET_PEER_ROUTER_ENABLED        BIT(2)  /* router feature enabled */
923
924 /*
925  * A peer is marked CONFIGURED if it was configured by DLC.
926  *
927  * In addition, a peer is marked DISCOVERED if it has fully passed
928  * through Peer Discovery.
929  *
930  * When Peer Discovery is disabled, the discovery thread will mark
931  * peers REDISCOVER to indicate that they should be re-examined if
932  * discovery is (re)enabled on the node.
933  *
934  * A peer that was created as the result of inbound traffic will not
935  * be marked at all.
936  */
937 #define LNET_PEER_CONFIGURED            BIT(3)  /* Configured via DLC */
938 #define LNET_PEER_DISCOVERED            BIT(4)  /* Peer was discovered */
939 #define LNET_PEER_REDISCOVER            BIT(5)  /* Discovery was disabled */
940 /*
941  * A peer is marked DISCOVERING when discovery is in progress.
942  * The other flags below correspond to stages of discovery.
943  */
944 #define LNET_PEER_DISCOVERING           BIT(6)  /* Discovering */
945 #define LNET_PEER_DATA_PRESENT          BIT(7)  /* Remote peer data present */
946 #define LNET_PEER_NIDS_UPTODATE         BIT(8)  /* Remote peer info uptodate */
947 #define LNET_PEER_PING_SENT             BIT(9)  /* Waiting for REPLY to Ping */
948 #define LNET_PEER_PUSH_SENT             BIT(10) /* Waiting for ACK of Push */
949 #define LNET_PEER_PING_FAILED           BIT(11) /* Ping send failure */
950 #define LNET_PEER_PUSH_FAILED           BIT(12) /* Push send failure */
951 /*
952  * A ping can be forced as a way to fix up state, or as a manual
953  * intervention by an admin.
954  * A push can be forced in circumstances that would normally not
955  * allow for one to happen.
956  */
957 #define LNET_PEER_FORCE_PING            BIT(13) /* Forced Ping */
958 #define LNET_PEER_FORCE_PUSH            BIT(14) /* Forced Push */
959
960 /* force delete even if router */
961 #define LNET_PEER_RTR_NI_FORCE_DEL      BIT(15)
962
963 /* gw undergoing alive discovery */
964 #define LNET_PEER_RTR_DISCOVERY         BIT(16)
965 /* gw has undergone discovery (does not indicate success or failure) */
966 #define LNET_PEER_RTR_DISCOVERED        BIT(17)
967
968 /* peer is marked for deletion */
969 #define LNET_PEER_MARK_DELETION         BIT(18)
970 /* lnet_peer_del()/lnet_peer_del_locked() has been called on the peer */
971 #define LNET_PEER_MARK_DELETED          BIT(19)
972 /* lock primary NID to what's requested by ULP */
973 #define LNET_PEER_LOCK_PRIMARY          BIT(20)
974 /* this is for informational purposes only. It is set if a peer gets
975  * configured from Lustre with a primary NID which belongs to another peer
976  * which is also configured by Lustre as the primary NID.
977  */
978 #define LNET_PEER_BAD_CONFIG            BIT(21)
979
980 struct lnet_peer_net {
981         /* chain on lp_peer_nets */
982         struct list_head        lpn_peer_nets;
983
984         /* list of peer_nis on this network */
985         struct list_head        lpn_peer_nis;
986
987         /* pointer to the peer I'm part of */
988         struct lnet_peer        *lpn_peer;
989
990         /* Net ID */
991         __u32                   lpn_net_id;
992
993         /* peer net health */
994         int                     lpn_healthv;
995
996         /* time of next router ping on this net */
997         time64_t                lpn_next_ping;
998
999         /* selection sequence number */
1000         __u32                   lpn_seq;
1001
1002         /* relative peer net selection priority */
1003         __u32                   lpn_sel_priority;
1004
1005         /* reference count */
1006         atomic_t                lpn_refcount;
1007 };
1008
1009 /* peer hash size */
1010 #define LNET_PEER_HASH_BITS     9
1011 #define LNET_PEER_HASH_SIZE     (1 << LNET_PEER_HASH_BITS)
1012
1013 /*
1014  * peer hash table - one per CPT
1015  *
1016  * protected by lnet_net_lock/EX for update
1017  *    pt_version
1018  *    pt_hash[...]
1019  *    pt_peer_list
1020  *    pt_peers
1021  * protected by pt_zombie_lock:
1022  *    pt_zombie_list
1023  *    pt_zombies
1024  *
1025  * pt_zombie lock nests inside lnet_net_lock
1026  */
1027 struct lnet_peer_table {
1028         int                     pt_version;     /* /proc validity stamp */
1029         struct list_head        *pt_hash;       /* NID->peer hash */
1030         struct list_head        pt_peer_list;   /* peers */
1031         int                     pt_peers;       /* # peers */
1032         struct list_head        pt_zombie_list; /* zombie peer_ni */
1033         int                     pt_zombies;     /* # zombie peers_ni */
1034         spinlock_t              pt_zombie_lock; /* protect list and count */
1035 };
1036
1037 /* peer aliveness is enabled only on routers for peers in a network where the
1038  * struct lnet_ni::ni_peertimeout has been set to a positive value
1039  */
1040 #define lnet_peer_aliveness_enabled(lp) (the_lnet.ln_routing != 0 && \
1041                                         ((lp)->lpni_net) && \
1042                                         (lp)->lpni_net->net_tunables.lct_peer_timeout > 0)
1043
1044 struct lnet_route {
1045         struct list_head        lr_list;        /* chain on net */
1046         struct list_head        lr_gwlist;      /* chain on gateway */
1047         struct lnet_peer        *lr_gateway;    /* router node */
1048         struct lnet_nid         lr_nid;         /* NID used to add route */
1049         __u32                   lr_net;         /* remote network number */
1050         __u32                   lr_lnet;        /* local network number */
1051         int                     lr_seq;         /* sequence for round-robin */
1052         __u32                   lr_hops;        /* how far I am */
1053         unsigned int            lr_priority;    /* route priority */
1054         atomic_t                lr_alive;       /* cached route aliveness */
1055         bool                    lr_single_hop;  /* this route is single-hop */
1056 };
1057
1058 #define LNET_REMOTE_NETS_HASH_DEFAULT   (1U << 7)
1059 #define LNET_REMOTE_NETS_HASH_MAX       (1U << 16)
1060 #define LNET_REMOTE_NETS_HASH_SIZE      (1 << the_lnet.ln_remote_nets_hbits)
1061
1062 struct lnet_remotenet {
1063         /* chain on ln_remote_nets_hash */
1064         struct list_head        lrn_list;
1065         /* routes to me */
1066         struct list_head        lrn_routes;
1067         /* my net number */
1068         __u32                   lrn_net;
1069 };
1070
1071 /** lnet message has credit and can be submitted to lnd for send/receive */
1072 #define LNET_CREDIT_OK          0
1073 /** lnet message is waiting for credit */
1074 #define LNET_CREDIT_WAIT        1
1075 /** lnet message is waiting for discovery */
1076 #define LNET_DC_WAIT            2
1077
1078 struct lnet_rtrbufpool {
1079         /* my free buffer pool */
1080         struct list_head        rbp_bufs;
1081         /* messages blocking for a buffer */
1082         struct list_head        rbp_msgs;
1083         /* # pages in each buffer */
1084         int                     rbp_npages;
1085         /* requested number of buffers */
1086         int                     rbp_req_nbuffers;
1087         /* # buffers actually allocated */
1088         int                     rbp_nbuffers;
1089         /* # free buffers / blocked messages */
1090         int                     rbp_credits;
1091         /* low water mark */
1092         int                     rbp_mincredits;
1093 };
1094
1095 struct lnet_rtrbuf {
1096         struct list_head         rb_list;       /* chain on rbp_bufs */
1097         struct lnet_rtrbufpool  *rb_pool;       /* owning pool */
1098         struct bio_vec           rb_kiov[0];    /* the buffer space */
1099 };
1100
1101 #define LNET_PEER_HASHSIZE   503                /* prime! */
1102
1103 enum lnet_match_flags {
1104         /* Didn't match anything */
1105         LNET_MATCHMD_NONE       = BIT(0),
1106         /* Matched OK */
1107         LNET_MATCHMD_OK         = BIT(1),
1108         /* Must be discarded */
1109         LNET_MATCHMD_DROP       = BIT(2),
1110         /* match and buffer is exhausted */
1111         LNET_MATCHMD_EXHAUSTED  = BIT(3),
1112         /* match or drop */
1113         LNET_MATCHMD_FINISH     = (LNET_MATCHMD_OK | LNET_MATCHMD_DROP),
1114 };
1115
1116 /* Options for struct lnet_portal::ptl_options */
1117 #define LNET_PTL_LAZY           BIT(0)
1118 #define LNET_PTL_MATCH_UNIQUE   BIT(1)  /* unique match, for RDMA */
1119 #define LNET_PTL_MATCH_WILDCARD BIT(2)  /* wildcard match, request portal */
1120
1121 /* parameter for matching operations (GET, PUT) */
1122 struct lnet_match_info {
1123         __u64                   mi_mbits;
1124         struct lnet_processid   mi_id;
1125         unsigned int            mi_cpt;
1126         unsigned int            mi_opc;
1127         unsigned int            mi_portal;
1128         unsigned int            mi_rlength;
1129         unsigned int            mi_roffset;
1130 };
1131
1132 /* ME hash of RDMA portal */
1133 #define LNET_MT_HASH_BITS               8
1134 #define LNET_MT_HASH_SIZE               (1 << LNET_MT_HASH_BITS)
1135 #define LNET_MT_HASH_MASK               (LNET_MT_HASH_SIZE - 1)
1136 /* we allocate (LNET_MT_HASH_SIZE + 1) entries for lnet_match_table::mt_hash,
1137  * the last entry is reserved for MEs with ignore-bits */
1138 #define LNET_MT_HASH_IGNORE             LNET_MT_HASH_SIZE
1139 /* __u64 has 2^6 bits, so need 2^(LNET_MT_HASH_BITS - LNET_MT_BITS_U64) which
1140  * is 4 __u64s as bit-map, and add an extra __u64 (only use one bit) for the
1141  * ME-list with ignore-bits, which is mtable::mt_hash[LNET_MT_HASH_IGNORE] */
1142 #define LNET_MT_BITS_U64                6       /* 2^6 bits */
1143 #define LNET_MT_EXHAUSTED_BITS          (LNET_MT_HASH_BITS - LNET_MT_BITS_U64)
1144 #define LNET_MT_EXHAUSTED_BMAP          ((1 << LNET_MT_EXHAUSTED_BITS) + 1)
1145
1146 /* portal match table */
1147 struct lnet_match_table {
1148         /* reserved for upcoming patches, CPU partition ID */
1149         unsigned int            mt_cpt;
1150         unsigned int            mt_portal;      /* portal index */
1151         /* match table is set as "enabled" if there's non-exhausted MD
1152          * attached on mt_mhash, it's only valid for wildcard portal */
1153         unsigned int            mt_enabled;
1154         /* bitmap to flag whether MEs on mt_hash are exhausted or not */
1155         __u64                   mt_exhausted[LNET_MT_EXHAUSTED_BMAP];
1156         struct list_head        *mt_mhash;      /* matching hash */
1157 };
1158
1159 /* these are only useful for wildcard portal */
1160 /* Turn off message rotor for wildcard portals */
1161 #define LNET_PTL_ROTOR_OFF      0
1162 /* round-robin dispatch all PUT messages for wildcard portals */
1163 #define LNET_PTL_ROTOR_ON       1
1164 /* round-robin dispatch routed PUT message for wildcard portals */
1165 #define LNET_PTL_ROTOR_RR_RT    2
1166 /* dispatch routed PUT message by hashing source NID for wildcard portals */
1167 #define LNET_PTL_ROTOR_HASH_RT  3
1168
1169 struct lnet_portal {
1170         spinlock_t              ptl_lock;
1171         unsigned int            ptl_index;      /* portal ID, reserved */
1172         /* flags on this portal: lazy, unique... */
1173         unsigned int            ptl_options;
1174         /* list of messages which are stealing buffer */
1175         struct list_head        ptl_msg_stealing;
1176         /* messages blocking for MD */
1177         struct list_head        ptl_msg_delayed;
1178         /* Match table for each CPT */
1179         struct lnet_match_table **ptl_mtables;
1180         /* spread rotor of incoming "PUT" */
1181         unsigned int            ptl_rotor;
1182         /* # active entries for this portal */
1183         int                     ptl_mt_nmaps;
1184         /* array of active entries' cpu-partition-id */
1185         int                     ptl_mt_maps[0];
1186 };
1187
1188 #define LNET_LH_HASH_BITS       12
1189 #define LNET_LH_HASH_SIZE       (1ULL << LNET_LH_HASH_BITS)
1190 #define LNET_LH_HASH_MASK       (LNET_LH_HASH_SIZE - 1)
1191
1192 /* resource container (ME, MD, EQ) */
1193 struct lnet_res_container {
1194         unsigned int            rec_type;       /* container type */
1195         __u64                   rec_lh_cookie;  /* cookie generator */
1196         struct list_head        rec_active;     /* active resource list */
1197         struct list_head        *rec_lh_hash;   /* handle hash */
1198 };
1199
1200 /* message container */
1201 struct lnet_msg_container {
1202         int                     msc_init;       /* initialized or not */
1203         /* max # threads finalizing */
1204         int                     msc_nfinalizers;
1205         /* msgs waiting to complete finalizing */
1206         struct list_head        msc_finalizing;
1207         /* msgs waiting to be resent */
1208         struct list_head        msc_resending;
1209         struct list_head        msc_active;     /* active message list */
1210         /* threads doing finalization */
1211         void                    **msc_finalizers;
1212         /* threads doing resends */
1213         void                    **msc_resenders;
1214 };
1215
1216 /* This UDSP structures need to match the user space liblnetconfig structures
1217  * in order for the marshall and unmarshall functions to be common.
1218  */
1219
1220 /* Net is described as a
1221  *  1. net type
1222  *  2. num range
1223  */
1224 struct lnet_ud_net_descr {
1225         __u32 udn_net_type;
1226         struct list_head udn_net_num_range;
1227 };
1228
1229 /* each NID range is defined as
1230  *  1. net descriptor
1231  *  2. address range descriptor
1232  */
1233 struct lnet_ud_nid_descr {
1234         struct lnet_ud_net_descr ud_net_id;
1235         struct list_head ud_addr_range;
1236         __u32 ud_mem_size;
1237 };
1238
1239 /* a UDSP rule can have up to three user defined NID descriptors
1240  *      - src: defines the local NID range for the rule
1241  *      - dst: defines the peer NID range for the rule
1242  *      - rte: defines the router NID range for the rule
1243  *
1244  * An action union defines the action to take when the rule
1245  * is matched
1246  */
1247 struct lnet_udsp {
1248         struct list_head udsp_on_list;
1249         __u32 udsp_idx;
1250         struct lnet_ud_nid_descr udsp_src;
1251         struct lnet_ud_nid_descr udsp_dst;
1252         struct lnet_ud_nid_descr udsp_rte;
1253         enum lnet_udsp_action_type udsp_action_type;
1254         union {
1255                 __u32 udsp_priority;
1256         } udsp_action;
1257 };
1258
1259 /* Peer Discovery states */
1260 #define LNET_DC_STATE_SHUTDOWN          0       /* not started */
1261 #define LNET_DC_STATE_RUNNING           1       /* started up OK */
1262 #define LNET_DC_STATE_STOPPING          2       /* telling thread to stop */
1263
1264 /* Router Checker states */
1265 #define LNET_MT_STATE_SHUTDOWN          0       /* not started */
1266 #define LNET_MT_STATE_RUNNING           1       /* started up OK */
1267 #define LNET_MT_STATE_STOPPING          2       /* telling thread to stop */
1268
1269 /* LNet states */
1270 #define LNET_STATE_SHUTDOWN             0       /* not started */
1271 #define LNET_STATE_RUNNING              1       /* started up OK */
1272 #define LNET_STATE_STOPPING             2       /* telling thread to stop */
1273
1274 struct lnet {
1275         /* CPU partition table of LNet */
1276         struct cfs_cpt_table            *ln_cpt_table;
1277         /* number of CPTs in ln_cpt_table */
1278         unsigned int                    ln_cpt_number;
1279         unsigned int                    ln_cpt_bits;
1280
1281         /* protect LNet resources (ME/MD/EQ) */
1282         struct cfs_percpt_lock          *ln_res_lock;
1283         /* # portals */
1284         int                             ln_nportals;
1285         /* the vector of portals */
1286         struct lnet_portal              **ln_portals;
1287         /* percpt MD container */
1288         struct lnet_res_container       **ln_md_containers;
1289
1290         /* Event Queue container */
1291         struct lnet_res_container       ln_eq_container;
1292         spinlock_t                      ln_eq_wait_lock;
1293
1294         unsigned int                    ln_remote_nets_hbits;
1295
1296         /* protect NI, peer table, credits, routers, rtrbuf... */
1297         struct cfs_percpt_lock          *ln_net_lock;
1298         /* percpt message containers for active/finalizing/freed message */
1299         struct lnet_msg_container       **ln_msg_containers;
1300         struct lnet_counters            **ln_counters;
1301         struct lnet_peer_table          **ln_peer_tables;
1302         /* list of peer nis not on a local network */
1303         struct list_head                ln_remote_peer_ni_list;
1304         /* failure simulation */
1305         struct list_head                ln_test_peers;
1306         struct list_head                ln_drop_rules;
1307         struct list_head                ln_delay_rules;
1308         /* LND instances */
1309         struct list_head                ln_nets;
1310         /* Sequence number used to round robin sends across all nets */
1311         __u32                           ln_net_seq;
1312         /* the loopback NI */
1313         struct lnet_ni                  *ln_loni;
1314         /* network zombie list */
1315         struct list_head                ln_net_zombie;
1316         /* resend messages list */
1317         struct list_head                ln_msg_resend;
1318         /* spin lock to protect the msg resend list */
1319         spinlock_t                      ln_msg_resend_lock;
1320
1321         /* remote networks with routes to them */
1322         struct list_head                *ln_remote_nets_hash;
1323         /* validity stamp */
1324         __u64                           ln_remote_nets_version;
1325         /* list of all known routers */
1326         struct list_head                ln_routers;
1327         /* validity stamp */
1328         __u64                           ln_routers_version;
1329         /* percpt router buffer pools */
1330         struct lnet_rtrbufpool          **ln_rtrpools;
1331
1332         /*
1333          * Ping target / Push source
1334          *
1335          * The ping target and push source share a single buffer. The
1336          * ln_ping_target is protected against concurrent updates by
1337          * ln_api_mutex.
1338          */
1339         struct lnet_handle_md           ln_ping_target_md;
1340         lnet_handler_t                  ln_ping_target_handler;
1341         struct lnet_ping_buffer         *ln_ping_target;
1342         atomic_t                        ln_ping_target_seqno;
1343
1344         /*
1345          * Push Target
1346          *
1347          * ln_push_nnis contains the desired size of the push target.
1348          * The lnet_net_lock is used to handle update races. The old
1349          * buffer may linger a while after it has been unlinked, in
1350          * which case the event handler cleans up.
1351          */
1352         lnet_handler_t                  ln_push_target_handler;
1353         struct lnet_handle_md           ln_push_target_md;
1354         struct lnet_ping_buffer         *ln_push_target;
1355         /* bytes needed for pb_info to receive push */
1356         int                             ln_push_target_nbytes;
1357
1358         /* discovery event queue handle */
1359         lnet_handler_t                  ln_dc_handler;
1360         /* discovery requests */
1361         struct list_head                ln_dc_request;
1362         /* discovery working list */
1363         struct list_head                ln_dc_working;
1364         /* discovery expired list */
1365         struct list_head                ln_dc_expired;
1366         /* discovery thread wait queue */
1367         wait_queue_head_t               ln_dc_waitq;
1368         /* discovery startup/shutdown state */
1369         int                             ln_dc_state;
1370
1371         /* monitor thread startup/shutdown state */
1372         int                             ln_mt_state;
1373         /* serialise startup/shutdown */
1374         struct semaphore                ln_mt_signal;
1375
1376         struct mutex                    ln_api_mutex;
1377         struct mutex                    ln_lnd_mutex;
1378         /* Have I called LNetNIInit myself? */
1379         int                             ln_niinit_self;
1380         /* LNetNIInit/LNetNIFini counter */
1381         int                             ln_refcount;
1382         /* SHUTDOWN/RUNNING/STOPPING */
1383         int                             ln_state;
1384
1385         int                             ln_routing;     /* am I a router? */
1386         lnet_pid_t                      ln_pid;         /* requested pid */
1387         /* uniquely identifies this ni in this epoch */
1388         __u64                           ln_interface_cookie;
1389         /* registered LNDs */
1390         const struct lnet_lnd           *ln_lnds[NUM_LNDS];
1391
1392         /* test protocol compatibility flags */
1393         unsigned long                   ln_testprotocompat;
1394
1395         /* 0 - load the NIs from the mod params
1396          * 1 - do not load the NIs from the mod params
1397          * Reverse logic to ensure that other calls to LNetNIInit
1398          * need no change
1399          */
1400         bool                            ln_nis_from_mod_params;
1401
1402         /*
1403          * completion for the monitor thread. The monitor thread takes care of
1404          * checking routes, timedout messages and resending messages.
1405          */
1406         struct completion               ln_mt_wait_complete;
1407
1408         /* per-cpt resend queues */
1409         struct list_head                **ln_mt_resendqs;
1410         /* local NIs to recover */
1411         struct list_head                ln_mt_localNIRecovq;
1412         /* local NIs to recover */
1413         struct list_head                ln_mt_peerNIRecovq;
1414         /*
1415          * An array of queues for GET/PUT waiting for REPLY/ACK respectively.
1416          * There are CPT number of queues. Since response trackers will be
1417          * added on the fast path we can't afford to grab the exclusive
1418          * net lock to protect these queues. The CPT will be calculated
1419          * based on the mdh cookie.
1420          */
1421         struct list_head                **ln_mt_rstq;
1422         /*
1423          * A response tracker becomes a zombie when the associated MD is queued
1424          * for unlink before the response tracker is detached from the MD. An
1425          * entry on a zombie list can be freed when either the remaining
1426          * operations on the MD complete or when LNet has shut down.
1427          */
1428         struct list_head                **ln_mt_zombie_rstqs;
1429         /* recovery handler */
1430         lnet_handler_t                  ln_mt_handler;
1431
1432         /*
1433          * Completed when the discovery and monitor threads can enter their
1434          * work loops
1435          */
1436         struct completion               ln_started;
1437         /* UDSP list */
1438         struct list_head                ln_udsp_list;
1439 };
1440
1441 struct genl_filter_list {
1442         struct list_head         lp_list;
1443         void                    *lp_cursor;
1444         bool                     lp_first;
1445 };
1446
1447 static const struct nla_policy scalar_attr_policy[LN_SCALAR_MAX + 1] = {
1448         [LN_SCALAR_ATTR_LIST]           = { .type = NLA_NESTED },
1449         [LN_SCALAR_ATTR_LIST_SIZE]      = { .type = NLA_U16 },
1450         [LN_SCALAR_ATTR_INDEX]          = { .type = NLA_U16 },
1451         [LN_SCALAR_ATTR_NLA_TYPE]       = { .type = NLA_U16 },
1452         [LN_SCALAR_ATTR_VALUE]          = { .type = NLA_STRING },
1453         [LN_SCALAR_ATTR_KEY_FORMAT]     = { .type = NLA_U16 },
1454 };
1455
1456 int lnet_genl_send_scalar_list(struct sk_buff *msg, u32 portid, u32 seq,
1457                                const struct genl_family *family, int flags,
1458                                u8 cmd, const struct ln_key_list *data[]);
1459
1460 /* Special workaround for pre-4.19 kernels to send error messages
1461  * from dumpit routines. Newer kernels will send message with
1462  * NL_SET_ERR_MSG information by default if NETLINK_EXT_ACK is set.
1463  */
1464 static inline int lnet_nl_send_error(struct sk_buff *msg, int portid, int seq,
1465                                      int error)
1466 {
1467 #ifndef HAVE_NL_DUMP_WITH_EXT_ACK
1468         struct nlmsghdr *nlh;
1469
1470         if (!error)
1471                 return 0;
1472
1473         nlh = nlmsg_put(msg, portid, seq, NLMSG_ERROR, sizeof(error), 0);
1474         if (!nlh)
1475                 return -ENOMEM;
1476 #ifdef HAVE_NL_PARSE_WITH_EXT_ACK
1477         netlink_ack(msg, nlh, error, NULL);
1478 #else
1479         netlink_ack(msg, nlh, error);
1480 #endif
1481         return nlmsg_len(nlh);
1482 #else
1483         return error;
1484 #endif
1485 }
1486
1487 #endif