Whamcloud - gitweb
9e9aafac5340b78753b497ab8c8279e3ca8d775d
[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 /* per scheduler state */
87 struct ksock_sched {
88         /* serialise */
89         spinlock_t kss_lock;
90         /* conn waiting to be written */
91         struct list_head kss_rx_conns;
92         struct list_head kss_tx_conns;
93         /* zombie noop tx list */
94         struct list_head kss_zombie_noop_txs;
95         /* where scheduler sleeps */
96         wait_queue_head_t kss_waitq;
97         /* # connections assigned to this scheduler */
98         int kss_nconns;
99         /* max allowed threads */
100         int kss_nthreads_max;
101         /* number of threads */
102         int kss_nthreads;
103         /* CPT id */
104         int kss_cpt;
105 };
106
107 #define KSOCK_CPT_SHIFT                 16
108 #define KSOCK_THREAD_ID(cpt, sid)       (((cpt) << KSOCK_CPT_SHIFT) | (sid))
109 #define KSOCK_THREAD_CPT(id)            ((id) >> KSOCK_CPT_SHIFT)
110 #define KSOCK_THREAD_SID(id)            ((id) & ((1UL << KSOCK_CPT_SHIFT) - 1))
111
112 struct ksock_interface {                        /* in-use interface */
113         int             ksni_index;             /* Linux interface index */
114         struct sockaddr_storage ksni_addr;      /* interface's address */
115         __u32           ksni_netmask;           /* interface's network mask */
116         int             ksni_nroutes;           /* # routes using (active) */
117         int             ksni_npeers;            /* # peers using (passive) */
118         char            ksni_name[IFNAMSIZ];    /* interface name */
119 };
120
121 struct ksock_tunables {
122         /* "stuck" socket timeout (seconds) */
123         int              *ksnd_timeout;
124         /* # scheduler threads in each pool while starting */
125         int              *ksnd_nscheds;
126         int              *ksnd_nconnds;         /* # connection daemons */
127         int              *ksnd_nconnds_max;     /* max # connection daemons */
128         int              *ksnd_min_reconnectms; /* first connection retry after (ms)... */
129         int              *ksnd_max_reconnectms; /* ...exponentially increasing to this */
130         int              *ksnd_eager_ack;       /* make TCP ack eagerly? */
131         int              *ksnd_typed_conns;     /* drive sockets by type? */
132         int              *ksnd_min_bulk;        /* smallest "large" message */
133         int              *ksnd_tx_buffer_size;  /* socket tx buffer size */
134         int              *ksnd_rx_buffer_size;  /* socket rx buffer size */
135         int              *ksnd_nagle;           /* enable NAGLE? */
136         int              *ksnd_round_robin;     /* round robin for multiple interfaces */
137         int              *ksnd_keepalive;       /* # secs for sending keepalive NOOP */
138         int              *ksnd_keepalive_idle;  /* # idle secs before 1st probe */
139         int              *ksnd_keepalive_count; /* # probes */
140         int              *ksnd_keepalive_intvl; /* time between probes */
141         int              *ksnd_credits;         /* # concurrent sends */
142         int              *ksnd_peertxcredits;   /* # concurrent sends to 1 peer_ni */
143         int              *ksnd_peerrtrcredits;  /* # per-peer_ni router buffer credits */
144         int              *ksnd_peertimeout;     /* seconds to consider peer_ni dead */
145         int              *ksnd_enable_csum;     /* enable check sum */
146         int              *ksnd_inject_csum_error; /* set non-zero to inject checksum error */
147         int              *ksnd_nonblk_zcack;    /* always send zc-ack on non-blocking connection */
148         unsigned int     *ksnd_zc_min_payload;  /* minimum zero copy payload size */
149         int              *ksnd_zc_recv;         /* enable ZC receive (for Chelsio TOE) */
150         int              *ksnd_zc_recv_min_nfrags; /* minimum # of fragments to enable ZC receive */
151         int              *ksnd_irq_affinity;    /* enable IRQ affinity? */
152 #ifdef SOCKNAL_BACKOFF
153         int              *ksnd_backoff_init;    /* initial TCP backoff */
154         int              *ksnd_backoff_max;     /* maximum TCP backoff */
155 #endif
156 #if SOCKNAL_VERSION_DEBUG
157         int              *ksnd_protocol;        /* protocol version */
158 #endif
159 };
160
161 struct ksock_net {
162         __u64             ksnn_incarnation;     /* my epoch */
163         struct list_head  ksnn_list;            /* chain on global list */
164         atomic_t          ksnn_npeers;          /* # peers */
165         int               ksnn_ninterfaces;     /* IP interfaces */
166         struct ksock_interface ksnn_interfaces[LNET_INTERFACES_NUM];
167 };
168 /* When the ksock_net is shut down, this (negative) bias is added to
169  * ksnn_npeers, which prevents new peers from being added.
170  */
171 #define SOCKNAL_SHUTDOWN_BIAS  (INT_MIN+1)
172
173 /** connd timeout */
174 #define SOCKNAL_CONND_TIMEOUT  120
175 /** reserved thread for accepting & creating new connd */
176 #define SOCKNAL_CONND_RESV     1
177
178 struct ksock_nal_data {
179         int                     ksnd_init;      /* initialisation state */
180         int                     ksnd_nnets;     /* # networks set up */
181         struct list_head        ksnd_nets;      /* list of nets */
182         /* stabilize peer_ni/conn ops */
183         rwlock_t                ksnd_global_lock;
184         /* hash table of all my known peers */
185         DECLARE_HASHTABLE(ksnd_peers, SOCKNAL_PEER_HASH_BITS);
186
187         atomic_t                ksnd_nthreads;  /* # live threads */
188         int                     ksnd_shuttingdown; /* tell threads to exit */
189         /* schedulers information */
190         struct ksock_sched      **ksnd_schedulers;
191
192         atomic_t      ksnd_nactive_txs;    /* #active txs */
193
194         /* conns to close: reaper_lock*/
195         struct list_head        ksnd_deathrow_conns;
196         /* conns to free: reaper_lock */
197         struct list_head        ksnd_zombie_conns;
198         /* conns to retry: reaper_lock*/
199         struct list_head        ksnd_enomem_conns;
200         /* reaper sleeps here */
201         wait_queue_head_t       ksnd_reaper_waitq;
202         /* when reaper will wake */
203         time64_t                ksnd_reaper_waketime;
204         /* serialise */
205         spinlock_t        ksnd_reaper_lock;
206
207         int               ksnd_enomem_tx;      /* test ENOMEM sender */
208         int               ksnd_stall_tx;       /* test sluggish sender */
209         int               ksnd_stall_rx;       /* test sluggish receiver */
210
211         /* incoming connection requests */
212         struct list_head        ksnd_connd_connreqs;
213         /* routes waiting to be connected */
214         struct list_head        ksnd_connd_routes;
215         /* connds sleep here */
216         wait_queue_head_t       ksnd_connd_waitq;
217         /* # connds connecting */
218         int                     ksnd_connd_connecting;
219         /** time stamp of the last failed connecting attempt */
220         time64_t                ksnd_connd_failed_stamp;
221         /** # starting connd */
222         unsigned                ksnd_connd_starting;
223         /** time stamp of the last starting connd */
224         time64_t                ksnd_connd_starting_stamp;
225         /** # running connd */
226         unsigned                ksnd_connd_running;
227         /* serialise */
228         spinlock_t              ksnd_connd_lock;
229
230         /* list head for freed noop tx */
231         struct list_head        ksnd_idle_noop_txs;
232         /* serialise, g_lock unsafe */
233         spinlock_t              ksnd_tx_lock;
234 };
235
236 #define SOCKNAL_INIT_NOTHING    0
237 #define SOCKNAL_INIT_DATA       1
238 #define SOCKNAL_INIT_ALL        2
239
240 /* A packet just assembled for transmission is represented by 1
241  * struct iovec fragment - the portals header -  followed by 0
242  * or more struct bio_vec fragments.
243  *
244  * On the receive side, initially 1 struct kvec fragment is posted for
245  * receive (the header).  Once the header has been received, the payload is
246  * received into struct bio_vec fragments.
247  */
248 struct ksock_conn;                              /* forward ref */
249 struct ksock_route;                             /* forward ref */
250 struct ksock_proto;                             /* forward ref */
251
252 struct ksock_tx {                       /* transmit packet */
253         struct list_head tx_list;       /* queue on conn for transmission etc */
254         struct list_head tx_zc_list;    /* queue on peer_ni for ZC request */
255         refcount_t      tx_refcount;    /* tx reference count */
256         int             tx_nob;         /* # packet bytes */
257         int             tx_resid;       /* residual bytes */
258         int             tx_niov;        /* # packet kvec frags */
259         int             tx_nkiov;       /* # packet page frags */
260         unsigned short  tx_zc_aborted;  /* aborted ZC request */
261         unsigned short  tx_zc_capable:1; /* payload is large enough for ZC */
262         unsigned short  tx_zc_checked:1; /* Have I checked if I should ZC? */
263         unsigned short  tx_nonblk:1;    /* it's a non-blocking ACK */
264         struct bio_vec *tx_kiov;        /* packet page frags */
265         struct ksock_conn *tx_conn;     /* owning conn */
266         struct lnet_msg *tx_lnetmsg;    /* lnet message for lnet_finalize() */
267         time64_t        tx_deadline;    /* when (in secs) tx times out */
268         struct ksock_msg tx_msg;        /* socklnd message buffer */
269         int             tx_desc_size;   /* size of this descriptor */
270         enum lnet_msg_hstatus tx_hstatus; /* health status of tx */
271         struct kvec     tx_hdr;         /* virt hdr */
272         struct bio_vec  tx_payload[0];  /* paged payload */
273 };
274
275 #define KSOCK_NOOP_TX_SIZE  ((int)offsetof(struct ksock_tx, tx_payload[0]))
276
277 /* space for the rx frag descriptors; we either read a single contiguous
278  * header, or up to LNET_MAX_IOV frags of payload of either type. */
279 union ksock_rxiovspace {
280         struct kvec     iov[LNET_MAX_IOV];
281         struct bio_vec  kiov[LNET_MAX_IOV];
282 };
283
284 #define SOCKNAL_RX_KSM_HEADER   1               /* reading ksock message header */
285 #define SOCKNAL_RX_LNET_HEADER  2               /* reading lnet message header */
286 #define SOCKNAL_RX_PARSE        3               /* Calling lnet_parse() */
287 #define SOCKNAL_RX_PARSE_WAIT   4               /* waiting to be told to read the body */
288 #define SOCKNAL_RX_LNET_PAYLOAD 5               /* reading lnet payload (to deliver here) */
289 #define SOCKNAL_RX_SLOP         6               /* skipping body */
290
291 struct ksock_conn {
292         struct ksock_peer_ni    *ksnc_peer;             /* owning peer_ni */
293         struct ksock_route      *ksnc_route;            /* owning route */
294         struct list_head        ksnc_list;              /* on peer_ni's conn list */
295         struct socket           *ksnc_sock;             /* actual socket */
296         void                    *ksnc_saved_data_ready; /* socket's original
297                                                          * data_ready() cb */
298         void                    *ksnc_saved_write_space; /* socket's original
299                                                           * write_space() cb */
300         refcount_t              ksnc_conn_refcount;     /* conn refcount */
301         refcount_t              ksnc_sock_refcount;     /* sock refcount */
302         struct ksock_sched      *ksnc_scheduler;        /* who schedules this
303                                                          * connection */
304         struct sockaddr_storage ksnc_myaddr;            /* my address */
305         struct sockaddr_storage ksnc_peeraddr;          /*  peer_ni's address */
306         signed int              ksnc_type:3;            /* type of connection,
307                                                          * should be signed
308                                                          * value */
309         unsigned int            ksnc_closing:1;         /* being shut down */
310         unsigned int            ksnc_flip:1;            /* flip or not, only for V2.x */
311         unsigned int            ksnc_zc_capable:1;      /* enable to ZC */
312         const struct ksock_proto *ksnc_proto; /* protocol for the connection */
313
314         /* READER */
315
316         /* where I enq waiting input or a forwarding descriptor */
317         struct list_head   ksnc_rx_list;
318         time64_t                ksnc_rx_deadline; /* when (in seconds) receive times out */
319         __u8                  ksnc_rx_started;  /* started receiving a message */
320         __u8                  ksnc_rx_ready;    /* data ready to read */
321         __u8                  ksnc_rx_scheduled;/* being progressed */
322         __u8                  ksnc_rx_state;    /* what is being read */
323         int                   ksnc_rx_nob_left; /* # bytes to next hdr/body */
324         int                   ksnc_rx_nob_wanted; /* bytes actually wanted */
325         int                   ksnc_rx_niov;     /* # kvec frags */
326         struct kvec          *ksnc_rx_iov;      /* the kvec frags */
327         int                   ksnc_rx_nkiov;    /* # page frags */
328         struct bio_vec       *ksnc_rx_kiov;     /* the page frags */
329         union ksock_rxiovspace  ksnc_rx_iov_space;/* space for frag descriptors */
330         __u32                 ksnc_rx_csum;     /* partial checksum for incoming
331                                                  * data */
332         struct lnet_msg      *ksnc_lnet_msg;    /* rx lnet_finalize arg*/
333         struct ksock_msg        ksnc_msg;       /* incoming message buffer:
334                                                  * V2.x message takes the
335                                                  * whole struct
336                                                  * V1.x message is a bare
337                                                  * struct lnet_hdr, it's stored
338                                                  * in ksnc_msg.ksm_u.lnetmsg
339                                                  */
340         /* -- WRITER -- */
341         /* where I enq waiting for output space */
342         struct list_head        ksnc_tx_list;
343         /* packets waiting to be sent */
344         struct list_head        ksnc_tx_queue;
345         /* next TX that can carry a LNet message or ZC-ACK */
346         struct ksock_tx         *ksnc_tx_carrier;
347         /* when (in seconds) tx times out */
348         time64_t                ksnc_tx_deadline;
349         /* send buffer marker */
350         int                     ksnc_tx_bufnob;
351         /* # bytes queued */
352         atomic_t                ksnc_tx_nob;
353         /* write space */
354         int                     ksnc_tx_ready;
355         /* being progressed */
356         int                     ksnc_tx_scheduled;
357         /* time stamp of the last posted TX */
358         time64_t                ksnc_tx_last_post;
359 };
360
361 struct ksock_route {
362         struct list_head        ksnr_list;      /* chain on peer_ni route list*/
363         struct list_head        ksnr_connd_list;/* chain on ksnr_connd_routes */
364         struct ksock_peer_ni   *ksnr_peer;      /* owning peer_ni */
365         refcount_t              ksnr_refcount;  /* # users */
366         time64_t                ksnr_timeout;   /* when (in secs) reconnection
367                                                  * can happen next
368                                                  */
369         time64_t                ksnr_retry_interval;/* secs between retries */
370         int                     ksnr_myiface;   /* interface index */
371         struct sockaddr_storage ksnr_addr;      /* IP address to connect to */
372         unsigned int            ksnr_scheduled:1;/* scheduled for attention */
373         unsigned int            ksnr_connecting:1;/* connection in progress */
374         unsigned int            ksnr_connected:4;/* connections by type */
375         unsigned int            ksnr_deleted:1; /* been removed from peer_ni? */
376         unsigned int            ksnr_share_count;/* created explicitly? */
377         int                     ksnr_conn_count;/* # conns for this route */
378 };
379
380 #define SOCKNAL_KEEPALIVE_PING          1       /* cookie for keepalive ping */
381
382 struct ksock_peer_ni {
383         struct hlist_node       ksnp_list;      /* stash on global peer_ni list */
384         time64_t                ksnp_last_alive;/* when (in seconds) I was last alive */
385         struct lnet_process_id  ksnp_id;        /* who's on the other end(s) */
386         refcount_t              ksnp_refcount;  /* # users */
387         int                     ksnp_closing;   /* being closed */
388         int                     ksnp_accepting; /* # passive connections pending */
389         int                     ksnp_error;     /* errno on closing last conn */
390         __u64                   ksnp_zc_next_cookie;/* ZC completion cookie */
391         __u64                   ksnp_incarnation;   /* latest known peer_ni incarnation */
392         const struct ksock_proto *ksnp_proto;   /* latest known protocol */
393         struct list_head        ksnp_conns;     /* all active connections */
394         struct list_head        ksnp_routes;    /* routes */
395         struct list_head        ksnp_tx_queue;  /* waiting packets */
396         spinlock_t              ksnp_lock;      /* serialize, g_lock unsafe */
397         /* zero copy requests wait for ACK  */
398         struct list_head        ksnp_zc_req_list;
399         time64_t                ksnp_send_keepalive; /* time to send keepalive */
400         struct lnet_ni          *ksnp_ni;       /* which network */
401         int                     ksnp_n_passive_ips; /* # of... */
402         __u32                   ksnp_passive_ips[LNET_INTERFACES_NUM]; /* preferred local interfaces */
403 };
404
405 struct ksock_connreq {
406         /* stash on ksnd_connd_connreqs */
407         struct list_head        ksncr_list;
408         /* chosen NI */
409         struct lnet_ni          *ksncr_ni;
410         /* accepted socket */
411         struct socket           *ksncr_sock;
412 };
413
414 extern struct ksock_nal_data ksocknal_data;
415 extern struct ksock_tunables ksocknal_tunables;
416
417 #define SOCKNAL_MATCH_NO        0        /* TX can't match type of connection */
418 #define SOCKNAL_MATCH_YES       1        /* TX matches type of connection */
419 #define SOCKNAL_MATCH_MAY       2        /* TX can be sent on the connection, but not preferred */
420
421 struct ksock_proto {
422         int           pro_version;                                              /* version number of protocol */
423         int         (*pro_send_hello)(struct ksock_conn *, struct ksock_hello_msg *);     /* handshake function */
424         int         (*pro_recv_hello)(struct ksock_conn *, struct ksock_hello_msg *, int);/* handshake function */
425         void        (*pro_pack)(struct ksock_tx *);                                  /* message pack */
426         void        (*pro_unpack)(struct ksock_msg *);                          /* message unpack */
427         struct ksock_tx *(*pro_queue_tx_msg)(struct ksock_conn *, struct ksock_tx *);          /* queue tx on the connection */
428         int         (*pro_queue_tx_zcack)(struct ksock_conn *, struct ksock_tx *, __u64); /* queue ZC ack on the connection */
429         int         (*pro_handle_zcreq)(struct ksock_conn *, __u64, int);            /* handle ZC request */
430         int         (*pro_handle_zcack)(struct ksock_conn *, __u64, __u64);          /* handle ZC ACK */
431         int         (*pro_match_tx)(struct ksock_conn *, struct ksock_tx *, int);         /* msg type matches the connection type:
432                                                                                  * return value:
433                                                                                  *   return MATCH_NO  : no
434                                                                                  *   return MATCH_YES : matching type
435                                                                                  *   return MATCH_MAY : can be backup */
436 };
437
438 extern const struct ksock_proto ksocknal_protocol_v1x;
439 extern const struct ksock_proto ksocknal_protocol_v2x;
440 extern const struct ksock_proto ksocknal_protocol_v3x;
441
442 #define KSOCK_PROTO_V1_MAJOR    LNET_PROTO_TCP_VERSION_MAJOR
443 #define KSOCK_PROTO_V1_MINOR    LNET_PROTO_TCP_VERSION_MINOR
444 #define KSOCK_PROTO_V1          KSOCK_PROTO_V1_MAJOR
445
446 #ifndef CPU_MASK_NONE
447 #define CPU_MASK_NONE   0UL
448 #endif
449
450 static inline __u32 ksocknal_csum(__u32 crc, unsigned char const *p, size_t len)
451 {
452 #if 1
453         return crc32_le(crc, p, len);
454 #else
455         while (len-- > 0)
456                 crc = ((crc + 0x100) & ~0xff) | ((crc + *p++) & 0xff) ;
457
458         return crc;
459 #endif
460 }
461
462 static inline int
463 ksocknal_route_mask(void)
464 {
465         if (!*ksocknal_tunables.ksnd_typed_conns)
466                 return BIT(SOCKLND_CONN_ANY);
467
468         return (BIT(SOCKLND_CONN_CONTROL) |
469                 BIT(SOCKLND_CONN_BULK_IN) |
470                 BIT(SOCKLND_CONN_BULK_OUT));
471 }
472
473 static inline void
474 ksocknal_conn_addref(struct ksock_conn *conn)
475 {
476         refcount_inc(&conn->ksnc_conn_refcount);
477 }
478
479 extern void ksocknal_queue_zombie_conn(struct ksock_conn *conn);
480 extern void ksocknal_finalize_zcreq(struct ksock_conn *conn);
481
482 static inline void
483 ksocknal_conn_decref(struct ksock_conn *conn)
484 {
485         if (refcount_dec_and_test(&conn->ksnc_conn_refcount))
486                 ksocknal_queue_zombie_conn(conn);
487 }
488
489 static inline int
490 ksocknal_connsock_addref(struct ksock_conn *conn)
491 {
492         int rc = -ESHUTDOWN;
493
494         read_lock(&ksocknal_data.ksnd_global_lock);
495         if (!conn->ksnc_closing) {
496                 refcount_inc(&conn->ksnc_sock_refcount);
497                 rc = 0;
498         }
499         read_unlock(&ksocknal_data.ksnd_global_lock);
500
501         return (rc);
502 }
503
504 static inline void
505 ksocknal_connsock_decref(struct ksock_conn *conn)
506 {
507         if (refcount_dec_and_test(&conn->ksnc_sock_refcount)) {
508                 LASSERT (conn->ksnc_closing);
509                 sock_release(conn->ksnc_sock);
510                 conn->ksnc_sock = NULL;
511                 ksocknal_finalize_zcreq(conn);
512         }
513 }
514
515 static inline void
516 ksocknal_tx_addref(struct ksock_tx *tx)
517 {
518         refcount_inc(&tx->tx_refcount);
519 }
520
521 extern void ksocknal_tx_prep(struct ksock_conn *, struct ksock_tx *tx);
522 extern void ksocknal_tx_done(struct lnet_ni *ni, struct ksock_tx *tx, int error);
523
524 static inline void
525 ksocknal_tx_decref(struct ksock_tx *tx)
526 {
527         if (refcount_dec_and_test(&tx->tx_refcount))
528                 ksocknal_tx_done(NULL, tx, 0);
529 }
530
531 static inline void
532 ksocknal_route_addref(struct ksock_route *route)
533 {
534         refcount_inc(&route->ksnr_refcount);
535 }
536
537 extern void ksocknal_destroy_route(struct ksock_route *route);
538
539 static inline void
540 ksocknal_route_decref(struct ksock_route *route)
541 {
542         if (refcount_dec_and_test(&route->ksnr_refcount))
543                 ksocknal_destroy_route (route);
544 }
545
546 static inline void
547 ksocknal_peer_addref(struct ksock_peer_ni *peer_ni)
548 {
549         refcount_inc(&peer_ni->ksnp_refcount);
550 }
551
552 extern void ksocknal_destroy_peer(struct ksock_peer_ni *peer_ni);
553
554 static inline void
555 ksocknal_peer_decref(struct ksock_peer_ni *peer_ni)
556 {
557         if (refcount_dec_and_test(&peer_ni->ksnp_refcount))
558                 ksocknal_destroy_peer(peer_ni);
559 }
560
561 static inline int ksocknal_timeout(void)
562 {
563         return *ksocknal_tunables.ksnd_timeout ?
564                 *ksocknal_tunables.ksnd_timeout :
565                 lnet_get_lnd_timeout();
566 }
567
568 int ksocknal_startup(struct lnet_ni *ni);
569 void ksocknal_shutdown(struct lnet_ni *ni);
570 int ksocknal_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg);
571 int ksocknal_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg);
572 int ksocknal_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
573                   int delayed, unsigned int niov,
574                   struct bio_vec *kiov,
575                   unsigned int offset, unsigned int mlen, unsigned int rlen);
576 int ksocknal_accept(struct lnet_ni *ni, struct socket *sock);
577
578 int ksocknal_add_peer(struct lnet_ni *ni, struct lnet_process_id id,
579                       struct sockaddr *addr);
580 struct ksock_peer_ni *ksocknal_find_peer_locked(struct lnet_ni *ni,
581                                            struct lnet_process_id id);
582 struct ksock_peer_ni *ksocknal_find_peer(struct lnet_ni *ni,
583                                     struct lnet_process_id id);
584 extern void ksocknal_peer_failed(struct ksock_peer_ni *peer_ni);
585 extern int ksocknal_create_conn(struct lnet_ni *ni, struct ksock_route *route,
586                                 struct socket *sock, int type);
587 extern void ksocknal_close_conn_locked(struct ksock_conn *conn, int why);
588 extern void ksocknal_terminate_conn(struct ksock_conn *conn);
589 extern void ksocknal_destroy_conn(struct ksock_conn *conn);
590 extern int  ksocknal_close_peer_conns_locked(struct ksock_peer_ni *peer_ni,
591                                              struct sockaddr *peer, int why);
592 extern int ksocknal_close_conn_and_siblings(struct ksock_conn *conn, int why);
593 int ksocknal_close_matching_conns(struct lnet_process_id id, __u32 ipaddr);
594 extern struct ksock_conn *ksocknal_find_conn_locked(struct ksock_peer_ni *peer_ni,
595                                                     struct ksock_tx *tx, int nonblk);
596
597 extern int  ksocknal_launch_packet(struct lnet_ni *ni, struct ksock_tx *tx,
598                                    struct lnet_process_id id);
599 extern struct ksock_tx *ksocknal_alloc_tx(int type, int size);
600 extern void ksocknal_free_tx(struct ksock_tx *tx);
601 extern struct ksock_tx *ksocknal_alloc_tx_noop(__u64 cookie, int nonblk);
602 extern void ksocknal_next_tx_carrier(struct ksock_conn *conn);
603 extern void ksocknal_queue_tx_locked(struct ksock_tx *tx, struct ksock_conn *conn);
604 extern void ksocknal_txlist_done(struct lnet_ni *ni, struct list_head *txlist,
605                                  int error);
606 extern int ksocknal_thread_start(int (*fn)(void *arg), void *arg, char *name);
607 extern void ksocknal_thread_fini(void);
608 extern void ksocknal_launch_all_connections_locked(struct ksock_peer_ni *peer_ni);
609 extern struct ksock_route *ksocknal_find_connectable_route_locked(struct ksock_peer_ni *peer_ni);
610 extern struct ksock_route *ksocknal_find_connecting_route_locked(struct ksock_peer_ni *peer_ni);
611 extern int ksocknal_new_packet(struct ksock_conn *conn, int skip);
612 extern int ksocknal_scheduler(void *arg);
613 extern int ksocknal_connd(void *arg);
614 extern int ksocknal_reaper(void *arg);
615 int ksocknal_send_hello(struct lnet_ni *ni, struct ksock_conn *conn,
616                         lnet_nid_t peer_nid, struct ksock_hello_msg *hello);
617 int ksocknal_recv_hello(struct lnet_ni *ni, struct ksock_conn *conn,
618                         struct ksock_hello_msg *hello,
619                         struct lnet_process_id *id,
620                         __u64 *incarnation);
621 extern void ksocknal_read_callback(struct ksock_conn *conn);
622 extern void ksocknal_write_callback(struct ksock_conn *conn);
623
624 extern int ksocknal_lib_zc_capable(struct ksock_conn *conn);
625 extern void ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn);
626 extern void ksocknal_lib_set_callback(struct socket *sock,  struct ksock_conn *conn);
627 extern void ksocknal_lib_reset_callback(struct socket *sock,
628                                         struct ksock_conn *conn);
629 extern void ksocknal_lib_push_conn(struct ksock_conn *conn);
630 extern int ksocknal_lib_get_conn_addrs(struct ksock_conn *conn);
631 extern int ksocknal_lib_setup_sock(struct socket *so);
632 extern int ksocknal_lib_send_hdr(struct ksock_conn *conn, struct ksock_tx *tx,
633                                  struct kvec *scratch_iov);
634 extern int ksocknal_lib_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx,
635                                   struct kvec *scratch_iov);
636 extern void ksocknal_lib_eager_ack(struct ksock_conn *conn);
637 extern int ksocknal_lib_recv_iov(struct ksock_conn *conn,
638                                  struct kvec *scratchiov);
639 extern int ksocknal_lib_recv_kiov(struct ksock_conn *conn, struct page **pages,
640                        struct kvec *scratchiov);
641 extern int ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem,
642                                           int *rxmem, int *nagle);
643
644 extern int ksocknal_tunables_init(void);
645
646 extern void ksocknal_lib_csum_tx(struct ksock_tx *tx);
647
648 extern int ksocknal_lib_memory_pressure(struct ksock_conn *conn);
649
650 #endif /* _SOCKLND_SOCKLND_H_ */