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