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