Whamcloud - gitweb
LU-10391 lnet: migrate full LNet NI information collection
[fs/lustre-release.git] / lnet / klnds / socklnd / socklnd.h
1 /*
2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Copyright (c) 2011, 2017, Intel Corporation.
5  *
6  *   Author: Zach Brown <zab@zabbo.net>
7  *   Author: Peter J. Braam <braam@clusterfs.com>
8  *   Author: Phil Schwan <phil@clusterfs.com>
9  *   Author: Eric Barton <eric@bartonsoftware.com>
10  *
11  *   This file is part of Lustre, http://www.lustre.org
12  *
13  *   Portals is free software; you can redistribute it and/or
14  *   modify it under the terms of version 2 of the GNU General Public
15  *   License as published by the Free Software Foundation.
16  *
17  *   Portals is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with Portals; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  */
27
28 #ifndef _SOCKLND_SOCKLND_H_
29 #define _SOCKLND_SOCKLND_H_
30
31 #define DEBUG_PORTAL_ALLOC
32 #define DEBUG_SUBSYSTEM S_LND
33
34 #include <linux/crc32.h>
35 #include <linux/errno.h>
36 #include <linux/if.h>
37 #include <linux/init.h>
38 #include <linux/kernel.h>
39 #include <linux/kthread.h>
40 #include <linux/kmod.h>
41 #include <linux/list.h>
42 #include <linux/mm.h>
43 #include <linux/module.h>
44 #include <linux/pagemap.h>
45 #include <linux/refcount.h>
46 #include <linux/stat.h>
47 #include <linux/string.h>
48 #include <linux/syscalls.h>
49 #include <linux/sysctl.h>
50 #include <linux/uio.h>
51 #include <linux/unistd.h>
52 #include <linux/hashtable.h>
53 #include <net/sock.h>
54 #include <net/tcp.h>
55
56 #include <lnet/lib-lnet.h>
57 #include <lnet/socklnd.h>
58
59 #include <libcfs/linux/linux-net.h>
60
61 #ifndef NETIF_F_CSUM_MASK
62 # define NETIF_F_CSUM_MASK NETIF_F_ALL_CSUM
63 #endif
64
65 /* assume one thread for each connection type */
66 #define SOCKNAL_NSCHEDS         3
67 #define SOCKNAL_NSCHEDS_HIGH    (SOCKNAL_NSCHEDS << 1)
68
69 #define SOCKNAL_PEER_HASH_BITS  7       /* log2 of # peer_ni lists */
70 #define SOCKNAL_INSANITY_RECONN 5000    /* connd is trying on reconn infinitely */
71 #define SOCKNAL_ENOMEM_RETRY    1       /* seconds between retries */
72
73 #define SOCKNAL_SINGLE_FRAG_TX      0   /* disable multi-fragment sends */
74 #define SOCKNAL_SINGLE_FRAG_RX      0   /* disable multi-fragment receives */
75
76 #define SOCKNAL_VERSION_DEBUG       0   /* enable protocol version debugging */
77
78 /* risk kmap deadlock on multi-frag I/O (backs off to single-frag if disabled).
79  * no risk if we're not running on a CONFIG_HIGHMEM platform. */
80 #ifdef CONFIG_HIGHMEM
81 # define SOCKNAL_RISK_KMAP_DEADLOCK  0
82 #else
83 # define SOCKNAL_RISK_KMAP_DEADLOCK  1
84 #endif
85
86 enum ksocklnd_ni_lnd_tunables_attr {
87         LNET_NET_SOCKLND_TUNABLES_ATTR_UNSPEC = 0,
88
89         LNET_NET_SOCKLND_TUNABLES_ATTR_CONNS_PER_PEER,
90         LNET_NET_SOCKLND_TUNABLES_ATTR_LND_TIMEOUT,
91         __LNET_NET_SOCKLND_TUNABLES_ATTR_MAX_PLUS_ONE,
92 };
93
94 #define LNET_NET_SOCKLND_TUNABLES_ATTR_MAX (__LNET_NET_SOCKLND_TUNABLES_ATTR_MAX_PLUS_ONE - 1)
95
96 /* per scheduler state */
97 struct ksock_sched {
98         /* serialise */
99         spinlock_t kss_lock;
100         /* conn waiting to be written */
101         struct list_head kss_rx_conns;
102         struct list_head kss_tx_conns;
103         /* zombie noop tx list */
104         struct list_head kss_zombie_noop_txs;
105         /* where scheduler sleeps */
106         wait_queue_head_t kss_waitq;
107         /* # connections assigned to this scheduler */
108         int kss_nconns;
109         /* max allowed threads */
110         int kss_nthreads_max;
111         /* number of threads */
112         int kss_nthreads;
113         /* CPT id */
114         int kss_cpt;
115 };
116
117 #define KSOCK_CPT_SHIFT                 16
118 #define KSOCK_THREAD_ID(cpt, sid)       (((cpt) << KSOCK_CPT_SHIFT) | (sid))
119 #define KSOCK_THREAD_CPT(id)            ((id) >> KSOCK_CPT_SHIFT)
120 #define KSOCK_THREAD_SID(id)            ((id) & ((1UL << KSOCK_CPT_SHIFT) - 1))
121
122 struct ksock_interface {                        /* in-use interface */
123         int             ksni_index;             /* Linux interface index */
124         struct sockaddr_storage ksni_addr;      /* interface's address */
125         __u32           ksni_netmask;           /* interface's network mask */
126         int             ksni_nroutes;           /* # routes using (active) */
127         int             ksni_npeers;            /* # peers using (passive) */
128         char            ksni_name[IFNAMSIZ];    /* interface name */
129 };
130
131 struct ksock_tunables {
132         /* "stuck" socket timeout (seconds) */
133         int              *ksnd_timeout;
134         /* # scheduler threads in each pool while starting */
135         int              *ksnd_nscheds;
136         int              *ksnd_nconnds;         /* # connection daemons */
137         int              *ksnd_nconnds_max;     /* max # connection daemons */
138         int              *ksnd_min_reconnectms; /* first connection retry after (ms)... */
139         int              *ksnd_max_reconnectms; /* ...exponentially increasing to this */
140         int              *ksnd_eager_ack;       /* make TCP ack eagerly? */
141         int              *ksnd_typed_conns;     /* drive sockets by type? */
142         int              *ksnd_min_bulk;        /* smallest "large" message */
143         int              *ksnd_tx_buffer_size;  /* socket tx buffer size */
144         int              *ksnd_rx_buffer_size;  /* socket rx buffer size */
145         int              *ksnd_nagle;           /* enable NAGLE? */
146         int              *ksnd_round_robin;     /* round robin for multiple interfaces */
147         int              *ksnd_keepalive;       /* # secs for sending keepalive NOOP */
148         int              *ksnd_keepalive_idle;  /* # idle secs before 1st probe */
149         int              *ksnd_keepalive_count; /* # probes */
150         int              *ksnd_keepalive_intvl; /* time between probes */
151         int              *ksnd_credits;         /* # concurrent sends */
152         int              *ksnd_peertxcredits;   /* # concurrent sends to 1 peer_ni */
153         int              *ksnd_peerrtrcredits;  /* # per-peer_ni router buffer credits */
154         int              *ksnd_peertimeout;     /* seconds to consider peer_ni dead */
155         int              *ksnd_enable_csum;     /* enable check sum */
156         int              *ksnd_inject_csum_error; /* set non-zero to inject checksum error */
157         int              *ksnd_nonblk_zcack;    /* always send zc-ack on non-blocking connection */
158         unsigned int     *ksnd_zc_min_payload;  /* minimum zero copy payload size */
159         int              *ksnd_zc_recv;         /* enable ZC receive (for Chelsio TOE) */
160         int              *ksnd_zc_recv_min_nfrags; /* minimum # of fragments to enable ZC receive */
161         int              *ksnd_irq_affinity;    /* enable IRQ affinity? */
162 #ifdef SOCKNAL_BACKOFF
163         int              *ksnd_backoff_init;    /* initial TCP backoff */
164         int              *ksnd_backoff_max;     /* maximum TCP backoff */
165 #endif
166 #if SOCKNAL_VERSION_DEBUG
167         int              *ksnd_protocol;        /* protocol version */
168 #endif
169         int              *ksnd_conns_per_peer;  /* for typed mode, yields:
170                                                  * 1 + 2*conns_per_peer total
171                                                  * for untyped:
172                                                  * conns_per_peer total
173                                                  */
174 };
175
176 struct ksock_net {
177         __u64             ksnn_incarnation;     /* my epoch */
178         struct list_head  ksnn_list;            /* chain on global list */
179         atomic_t          ksnn_npeers;          /* # peers */
180         struct ksock_interface ksnn_interface;  /* IP interface */
181         struct lnet_ni    *ksnn_ni;
182 };
183 /* When the ksock_net is shut down, this (negative) bias is added to
184  * ksnn_npeers, which prevents new peers from being added.
185  */
186 #define SOCKNAL_SHUTDOWN_BIAS  (INT_MIN+1)
187
188 /** connd timeout */
189 #define SOCKNAL_CONND_TIMEOUT  120
190 /** reserved thread for accepting & creating new connd */
191 #define SOCKNAL_CONND_RESV     1
192
193 struct ksock_nal_data {
194         int                     ksnd_init;      /* initialisation state */
195         int                     ksnd_nnets;     /* # networks set up */
196         struct list_head        ksnd_nets;      /* list of nets */
197         /* stabilize peer_ni/conn ops */
198         rwlock_t                ksnd_global_lock;
199         /* hash table of all my known peers */
200         DECLARE_HASHTABLE(ksnd_peers, SOCKNAL_PEER_HASH_BITS);
201
202         atomic_t                ksnd_nthreads;  /* # live threads */
203         int                     ksnd_shuttingdown; /* tell threads to exit */
204         /* schedulers information */
205         struct ksock_sched      **ksnd_schedulers;
206
207         atomic_t      ksnd_nactive_txs;    /* #active txs */
208
209         /* conns to close: reaper_lock*/
210         struct list_head        ksnd_deathrow_conns;
211         /* conns to free: reaper_lock */
212         struct list_head        ksnd_zombie_conns;
213         /* conns to retry: reaper_lock*/
214         struct list_head        ksnd_enomem_conns;
215         /* reaper sleeps here */
216         wait_queue_head_t       ksnd_reaper_waitq;
217         /* when reaper will wake */
218         time64_t                ksnd_reaper_waketime;
219         /* serialise */
220         spinlock_t        ksnd_reaper_lock;
221
222         int               ksnd_enomem_tx;      /* test ENOMEM sender */
223         int               ksnd_stall_tx;       /* test sluggish sender */
224         int               ksnd_stall_rx;       /* test sluggish receiver */
225
226         /* incoming connection requests */
227         struct list_head        ksnd_connd_connreqs;
228         /* routes waiting to be connected */
229         struct list_head        ksnd_connd_routes;
230         /* connds sleep here */
231         wait_queue_head_t       ksnd_connd_waitq;
232         /* # connds connecting */
233         int                     ksnd_connd_connecting;
234         /** time stamp of the last failed connecting attempt */
235         time64_t                ksnd_connd_failed_stamp;
236         /** # starting connd */
237         unsigned                ksnd_connd_starting;
238         /** time stamp of the last starting connd */
239         time64_t                ksnd_connd_starting_stamp;
240         /** # running connd */
241         unsigned                ksnd_connd_running;
242         /* serialise */
243         spinlock_t              ksnd_connd_lock;
244
245         /* list head for freed noop tx */
246         struct list_head        ksnd_idle_noop_txs;
247         /* serialise, g_lock unsafe */
248         spinlock_t              ksnd_tx_lock;
249 };
250
251 #define SOCKNAL_INIT_NOTHING    0
252 #define SOCKNAL_INIT_DATA       1
253 #define SOCKNAL_INIT_ALL        2
254
255 /* A packet just assembled for transmission is represented by 1
256  * struct iovec fragment - the portals header -  followed by 0
257  * or more struct bio_vec fragments.
258  *
259  * On the receive side, initially 1 struct kvec fragment is posted for
260  * receive (the header).  Once the header has been received, the payload is
261  * received into struct bio_vec fragments.
262  */
263 struct ksock_conn;                              /* forward ref */
264 struct ksock_conn_cb;                           /* forward ref */
265 struct ksock_proto;                             /* forward ref */
266
267 struct ksock_tx {                       /* transmit packet */
268         struct list_head tx_list;       /* queue on conn for transmission etc */
269         struct list_head tx_zc_list;    /* queue on peer_ni for ZC request */
270         refcount_t      tx_refcount;    /* tx reference count */
271         int             tx_nob;         /* # packet bytes */
272         int             tx_resid;       /* residual bytes */
273         int             tx_niov;        /* # packet kvec frags */
274         int             tx_nkiov;       /* # packet page frags */
275         unsigned short  tx_zc_aborted;  /* aborted ZC request */
276         unsigned short  tx_zc_capable:1; /* payload is large enough for ZC */
277         unsigned short  tx_zc_checked:1; /* Have I checked if I should ZC? */
278         unsigned short  tx_nonblk:1;    /* it's a non-blocking ACK */
279         struct bio_vec *tx_kiov;        /* packet page frags */
280         struct ksock_conn *tx_conn;     /* owning conn */
281         struct lnet_msg *tx_lnetmsg;    /* lnet message for lnet_finalize() */
282         time64_t        tx_deadline;    /* when (in secs) tx times out */
283         struct ksock_msg tx_msg;        /* socklnd message buffer */
284         int             tx_desc_size;   /* size of this descriptor */
285         enum lnet_msg_hstatus tx_hstatus; /* health status of tx */
286         struct kvec     tx_hdr;         /* virt hdr */
287         struct bio_vec  tx_payload[0];  /* paged payload */
288 };
289
290 #define KSOCK_NOOP_TX_SIZE  ((int)offsetof(struct ksock_tx, tx_payload[0]))
291
292 /* space for the rx frag descriptors; we either read a single contiguous
293  * header, or up to LNET_MAX_IOV frags of payload of either type. */
294 union ksock_rxiovspace {
295         struct kvec     iov[LNET_MAX_IOV];
296         struct bio_vec  kiov[LNET_MAX_IOV];
297 };
298
299 #define SOCKNAL_RX_KSM_HEADER   1               /* reading ksock message header */
300 #define SOCKNAL_RX_LNET_HEADER  2               /* reading lnet message header */
301 #define SOCKNAL_RX_PARSE        3               /* Calling lnet_parse() */
302 #define SOCKNAL_RX_PARSE_WAIT   4               /* waiting to be told to read the body */
303 #define SOCKNAL_RX_LNET_PAYLOAD 5               /* reading lnet payload (to deliver here) */
304 #define SOCKNAL_RX_SLOP         6               /* skipping body */
305
306 struct ksock_conn {
307         struct ksock_peer_ni    *ksnc_peer;             /* owning peer_ni */
308         struct ksock_conn_cb    *ksnc_conn_cb;          /* owning conn control block */
309         struct list_head        ksnc_list;              /* on peer_ni's conn list */
310         struct socket           *ksnc_sock;             /* actual socket */
311         void                    *ksnc_saved_data_ready; /* socket's original
312                                                          * data_ready() cb */
313         void                    *ksnc_saved_write_space; /* socket's original
314                                                           * write_space() cb */
315         refcount_t              ksnc_conn_refcount;     /* conn refcount */
316         refcount_t              ksnc_sock_refcount;     /* sock refcount */
317         struct ksock_sched      *ksnc_scheduler;        /* who schedules this
318                                                          * connection */
319         struct sockaddr_storage ksnc_myaddr;            /* my address */
320         struct sockaddr_storage ksnc_peeraddr;          /*  peer_ni's address */
321         signed int              ksnc_type:3;            /* type of connection,
322                                                          * should be signed
323                                                          * value */
324         unsigned int            ksnc_closing:1;         /* being shut down */
325         unsigned int            ksnc_flip:1;            /* flip or not, only for V2.x */
326         unsigned int            ksnc_zc_capable:1;      /* enable to ZC */
327         const struct ksock_proto *ksnc_proto; /* protocol for the connection */
328
329         /* READER */
330
331         /* where I enq waiting input or a forwarding descriptor */
332         struct list_head   ksnc_rx_list;
333         time64_t                ksnc_rx_deadline; /* when (in seconds) receive times out */
334         __u8                  ksnc_rx_started;  /* started receiving a message */
335         __u8                  ksnc_rx_ready;    /* data ready to read */
336         __u8                  ksnc_rx_scheduled;/* being progressed */
337         __u8                  ksnc_rx_state;    /* what is being read */
338         int                   ksnc_rx_nob_left; /* # bytes to next hdr/body */
339         int                   ksnc_rx_nob_wanted; /* bytes actually wanted */
340         int                   ksnc_rx_niov;     /* # kvec frags */
341         struct kvec          *ksnc_rx_iov;      /* the kvec frags */
342         int                   ksnc_rx_nkiov;    /* # page frags */
343         struct bio_vec       *ksnc_rx_kiov;     /* the page frags */
344         union ksock_rxiovspace  ksnc_rx_iov_space;/* space for frag descriptors */
345         __u32                 ksnc_rx_csum;     /* partial checksum for incoming
346                                                  * data */
347         struct lnet_msg      *ksnc_lnet_msg;    /* rx lnet_finalize arg*/
348         struct ksock_msg        ksnc_msg;       /* incoming message buffer:
349                                                  * V2.x message takes the
350                                                  * whole struct
351                                                  * V1.x message is a bare
352                                                  * struct lnet_hdr_nid4, it's
353                                                  * stored in
354                                                  * ksnc_msg.ksm_u.lnetmsg
355                                                  */
356         /* -- WRITER -- */
357         /* where I enq waiting for output space */
358         struct list_head        ksnc_tx_list;
359         /* packets waiting to be sent */
360         struct list_head        ksnc_tx_queue;
361         /* next TX that can carry a LNet message or ZC-ACK */
362         struct ksock_tx         *ksnc_tx_carrier;
363         /* when (in seconds) tx times out */
364         time64_t                ksnc_tx_deadline;
365         /* send buffer marker */
366         int                     ksnc_tx_bufnob;
367         /* # bytes queued */
368         atomic_t                ksnc_tx_nob;
369         /* write space */
370         int                     ksnc_tx_ready;
371         /* being progressed */
372         int                     ksnc_tx_scheduled;
373         /* time stamp of the last posted TX */
374         time64_t                ksnc_tx_last_post;
375 };
376
377 #define SOCKNAL_CONN_COUNT_MAX_BITS     8       /* max conn count bits */
378 #define SOCKNAL_MAX_BUSY_RETRIES        3
379
380 struct ksock_conn_cb {
381         struct list_head        ksnr_connd_list;/* chain on ksnr_connd_routes */
382         struct ksock_peer_ni   *ksnr_peer;      /* owning peer_ni */
383         refcount_t              ksnr_refcount;  /* # users */
384         time64_t                ksnr_timeout;   /* when (in secs) reconnection
385                                                  * can happen next
386                                                  */
387         time64_t                ksnr_retry_interval;/* secs between retries */
388         struct sockaddr_storage ksnr_addr;      /* IP address to connect to */
389         unsigned int            ksnr_scheduled:1;/* scheduled for attention */
390         unsigned int            ksnr_connecting:1;/* connection in progress */
391         unsigned int            ksnr_connected:4;/* connections by type */
392         unsigned int            ksnr_deleted:1; /* been removed from peer_ni? */
393         unsigned int            ksnr_ctrl_conn_count:2; /* # conns by type */
394         unsigned int            ksnr_blki_conn_count:8;
395         unsigned int            ksnr_blko_conn_count:8;
396         int                     ksnr_conn_count;/* total # conns for this cb */
397         unsigned int            ksnr_max_conns; /* conns_per_peer at peer
398                                                  * creation
399                                                  */
400         unsigned int            ksnr_busy_retry_count;/* counts retry attempts
401                                                        * due to EALREADY rc
402                                                        */
403 };
404
405 #define SOCKNAL_KEEPALIVE_PING          1       /* cookie for keepalive ping */
406
407 struct ksock_peer_ni {
408         struct hlist_node       ksnp_list;      /* stash on global peer_ni list */
409         time64_t                ksnp_last_alive;/* when (in seconds) I was last alive */
410         struct lnet_processid   ksnp_id;        /* who's on the other end(s) */
411         refcount_t              ksnp_refcount;  /* # users */
412         int                     ksnp_closing;   /* being closed */
413         int                     ksnp_accepting; /* # passive connections pending */
414         int                     ksnp_error;     /* errno on closing last conn */
415         __u64                   ksnp_zc_next_cookie;/* ZC completion cookie */
416         __u64                   ksnp_incarnation;   /* latest known peer_ni incarnation */
417         const struct ksock_proto *ksnp_proto;   /* latest known protocol */
418         struct list_head        ksnp_conns;     /* all active connections */
419         struct ksock_conn_cb    *ksnp_conn_cb;  /* conn control block */
420         struct list_head        ksnp_tx_queue;  /* waiting packets */
421         spinlock_t              ksnp_lock;      /* serialize, g_lock unsafe */
422         /* zero copy requests wait for ACK  */
423         struct list_head        ksnp_zc_req_list;
424         time64_t                ksnp_send_keepalive; /* time to send keepalive */
425         struct lnet_ni          *ksnp_ni;       /* which network */
426 };
427
428 struct ksock_connreq {
429         /* stash on ksnd_connd_connreqs */
430         struct list_head        ksncr_list;
431         /* chosen NI */
432         struct lnet_ni          *ksncr_ni;
433         /* accepted socket */
434         struct socket           *ksncr_sock;
435 };
436
437 extern struct ksock_nal_data ksocknal_data;
438 extern struct ksock_tunables ksocknal_tunables;
439
440 #define SOCKNAL_MATCH_NO        0        /* TX can't match type of connection */
441 #define SOCKNAL_MATCH_YES       1        /* TX matches type of connection */
442 #define SOCKNAL_MATCH_MAY       2        /* TX can be sent on the connection, but not preferred */
443
444 struct ksock_proto {
445         int           pro_version;                                              /* version number of protocol */
446         int         (*pro_send_hello)(struct ksock_conn *, struct ksock_hello_msg *);     /* handshake function */
447         int         (*pro_recv_hello)(struct ksock_conn *, struct ksock_hello_msg *, int);/* handshake function */
448         void        (*pro_pack)(struct ksock_tx *);                                  /* message pack */
449         void        (*pro_unpack)(struct ksock_msg *, struct lnet_hdr *);       /* message unpack */
450         struct ksock_tx *(*pro_queue_tx_msg)(struct ksock_conn *, struct ksock_tx *);          /* queue tx on the connection */
451         int         (*pro_queue_tx_zcack)(struct ksock_conn *, struct ksock_tx *, __u64); /* queue ZC ack on the connection */
452         int         (*pro_handle_zcreq)(struct ksock_conn *, __u64, int);            /* handle ZC request */
453         int         (*pro_handle_zcack)(struct ksock_conn *, __u64, __u64);          /* handle ZC ACK */
454         int         (*pro_match_tx)(struct ksock_conn *, struct ksock_tx *, int);         /* msg type matches the connection type:
455                                                                                  * return value:
456                                                                                  *   return MATCH_NO  : no
457                                                                                  *   return MATCH_YES : matching type
458                                                                                  *   return MATCH_MAY : can be backup */
459 };
460
461 extern const struct ksock_proto ksocknal_protocol_v1x;
462 extern const struct ksock_proto ksocknal_protocol_v2x;
463 extern const struct ksock_proto ksocknal_protocol_v3x;
464 extern const struct ksock_proto ksocknal_protocol_v4x;
465
466 #define KSOCK_PROTO_V1_MAJOR    LNET_PROTO_TCP_VERSION_MAJOR
467 #define KSOCK_PROTO_V1_MINOR    LNET_PROTO_TCP_VERSION_MINOR
468 #define KSOCK_PROTO_V1          KSOCK_PROTO_V1_MAJOR
469
470 #ifndef CPU_MASK_NONE
471 #define CPU_MASK_NONE   0UL
472 #endif
473
474 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0)
475 #undef netdev_notifier_info_to_dev
476 #define netdev_notifier_info_to_dev(ndev) ndev
477 #endif
478
479 static inline __u32 ksocknal_csum(__u32 crc, unsigned char const *p, size_t len)
480 {
481 #if 1
482         return crc32_le(crc, p, len);
483 #else
484         while (len-- > 0)
485                 crc = ((crc + 0x100) & ~0xff) | ((crc + *p++) & 0xff) ;
486
487         return crc;
488 #endif
489 }
490
491 static inline int
492 ksocknal_conn_cb_mask(void)
493 {
494         if (!*ksocknal_tunables.ksnd_typed_conns)
495                 return BIT(SOCKLND_CONN_ANY);
496
497         return (BIT(SOCKLND_CONN_CONTROL) |
498                 BIT(SOCKLND_CONN_BULK_IN) |
499                 BIT(SOCKLND_CONN_BULK_OUT));
500 }
501
502 static inline void
503 ksocknal_conn_addref(struct ksock_conn *conn)
504 {
505         refcount_inc(&conn->ksnc_conn_refcount);
506 }
507
508 extern void ksocknal_queue_zombie_conn(struct ksock_conn *conn);
509 extern void ksocknal_finalize_zcreq(struct ksock_conn *conn);
510
511 static inline void
512 ksocknal_conn_decref(struct ksock_conn *conn)
513 {
514         if (refcount_dec_and_test(&conn->ksnc_conn_refcount))
515                 ksocknal_queue_zombie_conn(conn);
516 }
517
518 static inline int
519 ksocknal_connsock_addref(struct ksock_conn *conn)
520 {
521         int rc = -ESHUTDOWN;
522
523         read_lock(&ksocknal_data.ksnd_global_lock);
524         if (!conn->ksnc_closing) {
525                 refcount_inc(&conn->ksnc_sock_refcount);
526                 rc = 0;
527         }
528         read_unlock(&ksocknal_data.ksnd_global_lock);
529
530         return (rc);
531 }
532
533 static inline void
534 ksocknal_connsock_decref(struct ksock_conn *conn)
535 {
536         if (refcount_dec_and_test(&conn->ksnc_sock_refcount)) {
537                 LASSERT (conn->ksnc_closing);
538                 sock_release(conn->ksnc_sock);
539                 conn->ksnc_sock = NULL;
540                 ksocknal_finalize_zcreq(conn);
541         }
542 }
543
544 static inline void
545 ksocknal_tx_addref(struct ksock_tx *tx)
546 {
547         refcount_inc(&tx->tx_refcount);
548 }
549
550 extern void ksocknal_tx_prep(struct ksock_conn *, struct ksock_tx *tx);
551 extern void ksocknal_tx_done(struct lnet_ni *ni, struct ksock_tx *tx, int error);
552
553 static inline void
554 ksocknal_tx_decref(struct ksock_tx *tx)
555 {
556         if (refcount_dec_and_test(&tx->tx_refcount))
557                 ksocknal_tx_done(NULL, tx, 0);
558 }
559
560 static inline void
561 ksocknal_conn_cb_addref(struct ksock_conn_cb  *conn_cb)
562 {
563         refcount_inc(&conn_cb->ksnr_refcount);
564 }
565
566 extern void ksocknal_destroy_conn_cb(struct ksock_conn_cb *conn_cb);
567
568 static inline void
569 ksocknal_conn_cb_decref(struct ksock_conn_cb *conn_cb)
570 {
571         if (refcount_dec_and_test(&conn_cb->ksnr_refcount))
572                 ksocknal_destroy_conn_cb(conn_cb);
573 }
574
575 static inline void
576 ksocknal_peer_addref(struct ksock_peer_ni *peer_ni)
577 {
578         refcount_inc(&peer_ni->ksnp_refcount);
579 }
580
581 extern void ksocknal_destroy_peer(struct ksock_peer_ni *peer_ni);
582
583 static inline void
584 ksocknal_peer_decref(struct ksock_peer_ni *peer_ni)
585 {
586         if (refcount_dec_and_test(&peer_ni->ksnp_refcount))
587                 ksocknal_destroy_peer(peer_ni);
588 }
589
590 static inline int ksocknal_timeout(void)
591 {
592         return *ksocknal_tunables.ksnd_timeout ?: lnet_get_lnd_timeout();
593 }
594
595 static inline int ksocknal_conns_per_peer(void)
596 {
597         return *ksocknal_tunables.ksnd_conns_per_peer ?: 1;
598 }
599
600 int ksocknal_startup(struct lnet_ni *ni);
601 void ksocknal_shutdown(struct lnet_ni *ni);
602 int ksocknal_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg);
603 int ksocknal_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg);
604 int ksocknal_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
605                   int delayed, unsigned int niov,
606                   struct bio_vec *kiov,
607                   unsigned int offset, unsigned int mlen, unsigned int rlen);
608 int ksocknal_accept(struct lnet_ni *ni, struct socket *sock);
609
610 int ksocknal_add_peer(struct lnet_ni *ni, struct lnet_processid *id,
611                       struct sockaddr *addr);
612 struct ksock_peer_ni *ksocknal_find_peer_locked(struct lnet_ni *ni,
613                                            struct lnet_processid *id);
614 struct ksock_peer_ni *ksocknal_find_peer(struct lnet_ni *ni,
615                                          struct lnet_processid *id);
616 extern void ksocknal_peer_failed(struct ksock_peer_ni *peer_ni);
617 extern int ksocknal_create_conn(struct lnet_ni *ni,
618                                 struct ksock_conn_cb *conn_cb,
619                                 struct socket *sock, int type);
620 extern void ksocknal_close_conn_locked(struct ksock_conn *conn, int why);
621 extern void ksocknal_terminate_conn(struct ksock_conn *conn);
622 extern void ksocknal_destroy_conn(struct ksock_conn *conn);
623 extern int  ksocknal_close_peer_conns_locked(struct ksock_peer_ni *peer_ni,
624                                              struct sockaddr *peer, int why);
625 extern int ksocknal_close_conn_and_siblings(struct ksock_conn *conn, int why);
626 int ksocknal_close_matching_conns(struct lnet_processid *id, __u32 ipaddr);
627 extern struct ksock_conn *ksocknal_find_conn_locked(struct ksock_peer_ni *peer_ni,
628                                                     struct ksock_tx *tx, int nonblk);
629
630 extern int  ksocknal_launch_packet(struct lnet_ni *ni, struct ksock_tx *tx,
631                                    struct lnet_processid *id);
632 extern struct ksock_tx *ksocknal_alloc_tx(int type, int size);
633 extern void ksocknal_free_tx(struct ksock_tx *tx);
634 extern struct ksock_tx *ksocknal_alloc_tx_noop(__u64 cookie, int nonblk);
635 extern void ksocknal_next_tx_carrier(struct ksock_conn *conn);
636 extern void ksocknal_queue_tx_locked(struct ksock_tx *tx, struct ksock_conn *conn);
637 extern void ksocknal_txlist_done(struct lnet_ni *ni, struct list_head *txlist,
638                                  int error);
639 #define ksocknal_thread_start(fn, data, namefmt, arg...)                \
640         ({                                                              \
641                 struct task_struct *__task = kthread_run(fn, data,      \
642                                                          namefmt, ##arg); \
643                 if (!IS_ERR(__task))                                    \
644                         atomic_inc(&ksocknal_data.ksnd_nthreads);       \
645                 PTR_ERR_OR_ZERO(__task);                                \
646         })
647
648 extern void ksocknal_thread_fini(void);
649 extern void ksocknal_launch_all_connections_locked(struct ksock_peer_ni *peer_ni);
650 extern struct ksock_conn_cb *ksocknal_find_connectable_conn_cb_locked(struct ksock_peer_ni *peer_ni);
651 extern struct ksock_conn_cb *ksocknal_find_connecting_conn_cb_locked(struct ksock_peer_ni *peer_ni);
652 extern int ksocknal_new_packet(struct ksock_conn *conn, int skip);
653 extern int ksocknal_scheduler(void *arg);
654 extern int ksocknal_connd(void *arg);
655 extern int ksocknal_reaper(void *arg);
656 int ksocknal_send_hello(struct lnet_ni *ni, struct ksock_conn *conn,
657                         struct lnet_nid *peer_nid,
658                         struct ksock_hello_msg *hello);
659 int ksocknal_recv_hello(struct lnet_ni *ni, struct ksock_conn *conn,
660                         struct ksock_hello_msg *hello,
661                         struct lnet_processid *id,
662                         __u64 *incarnation);
663 extern void ksocknal_read_callback(struct ksock_conn *conn);
664 extern void ksocknal_write_callback(struct ksock_conn *conn);
665
666 extern int ksocknal_lib_zc_capable(struct ksock_conn *conn);
667 extern void ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn);
668 extern void ksocknal_lib_set_callback(struct socket *sock,  struct ksock_conn *conn);
669 extern void ksocknal_lib_reset_callback(struct socket *sock,
670                                         struct ksock_conn *conn);
671 extern void ksocknal_lib_push_conn(struct ksock_conn *conn);
672 extern int ksocknal_lib_get_conn_addrs(struct ksock_conn *conn);
673 extern int ksocknal_lib_setup_sock(struct socket *so);
674 extern int ksocknal_lib_send_hdr(struct ksock_conn *conn, struct ksock_tx *tx,
675                                  struct kvec *scratch_iov);
676 extern int ksocknal_lib_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx,
677                                   struct kvec *scratch_iov);
678 extern void ksocknal_lib_eager_ack(struct ksock_conn *conn);
679 extern int ksocknal_lib_recv_iov(struct ksock_conn *conn,
680                                  struct kvec *scratchiov);
681 extern int ksocknal_lib_recv_kiov(struct ksock_conn *conn, struct page **pages,
682                        struct kvec *scratchiov);
683 extern int ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem,
684                                           int *rxmem, int *nagle);
685
686 extern int ksocknal_tunables_init(void);
687 extern void ksocknal_tunables_setup(struct lnet_ni *ni);
688
689 extern void ksocknal_lib_csum_tx(struct ksock_tx *tx);
690
691 extern int ksocknal_lib_memory_pressure(struct ksock_conn *conn);
692
693 #endif /* _SOCKLND_SOCKLND_H_ */