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