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