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