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