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