c3b67165883bd4bc5ab26b07ee3baa0dd45cc2d8
[linux-2.6-microblaze.git] / drivers / staging / lustre / lnet / klnds / socklnd / socklnd_lib-linux.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include "socklnd.h"
38
39 int
40 ksocknal_lib_get_conn_addrs (ksock_conn_t *conn)
41 {
42         int rc = libcfs_sock_getaddr(conn->ksnc_sock, 1,
43                                      &conn->ksnc_ipaddr,
44                                      &conn->ksnc_port);
45
46         /* Didn't need the {get,put}connsock dance to deref ksnc_sock... */
47         LASSERT (!conn->ksnc_closing);
48
49         if (rc != 0) {
50                 CERROR ("Error %d getting sock peer IP\n", rc);
51                 return rc;
52         }
53
54         rc = libcfs_sock_getaddr(conn->ksnc_sock, 0,
55                                  &conn->ksnc_myipaddr, NULL);
56         if (rc != 0) {
57                 CERROR ("Error %d getting sock local IP\n", rc);
58                 return rc;
59         }
60
61         return 0;
62 }
63
64 int
65 ksocknal_lib_zc_capable(ksock_conn_t *conn)
66 {
67         int  caps = conn->ksnc_sock->sk->sk_route_caps;
68
69         if (conn->ksnc_proto == &ksocknal_protocol_v1x)
70                 return 0;
71
72         /* ZC if the socket supports scatter/gather and doesn't need software
73          * checksums */
74         return ((caps & NETIF_F_SG) != 0 && (caps & NETIF_F_ALL_CSUM) != 0);
75 }
76
77 int
78 ksocknal_lib_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
79 {
80         struct socket *sock = conn->ksnc_sock;
81         int         nob;
82         int         rc;
83
84         if (*ksocknal_tunables.ksnd_enable_csum && /* checksum enabled */
85             conn->ksnc_proto == &ksocknal_protocol_v2x && /* V2.x connection  */
86             tx->tx_nob == tx->tx_resid           && /* frist sending    */
87             tx->tx_msg.ksm_csum == 0)                /* not checksummed  */
88                 ksocknal_lib_csum_tx(tx);
89
90         /* NB we can't trust socket ops to either consume our iovs
91          * or leave them alone. */
92
93         {
94 #if SOCKNAL_SINGLE_FRAG_TX
95                 struct iovec    scratch;
96                 struct iovec   *scratchiov = &scratch;
97                 unsigned int    niov = 1;
98 #else
99                 struct iovec   *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
100                 unsigned int    niov = tx->tx_niov;
101 #endif
102                 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
103                 int  i;
104
105                 for (nob = i = 0; i < niov; i++) {
106                         scratchiov[i] = tx->tx_iov[i];
107                         nob += scratchiov[i].iov_len;
108                 }
109
110                 if (!list_empty(&conn->ksnc_tx_queue) ||
111                     nob < tx->tx_resid)
112                         msg.msg_flags |= MSG_MORE;
113
114                 rc = kernel_sendmsg(sock, &msg, (struct kvec *)scratchiov, niov, nob);
115         }
116         return rc;
117 }
118
119 int
120 ksocknal_lib_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
121 {
122         struct socket *sock = conn->ksnc_sock;
123         lnet_kiov_t   *kiov = tx->tx_kiov;
124         int         rc;
125         int         nob;
126
127         /* Not NOOP message */
128         LASSERT (tx->tx_lnetmsg != NULL);
129
130         /* NB we can't trust socket ops to either consume our iovs
131          * or leave them alone. */
132         if (tx->tx_msg.ksm_zc_cookies[0] != 0) {
133                 /* Zero copy is enabled */
134                 struct sock   *sk = sock->sk;
135                 struct page   *page = kiov->kiov_page;
136                 int         offset = kiov->kiov_offset;
137                 int         fragsize = kiov->kiov_len;
138                 int         msgflg = MSG_DONTWAIT;
139
140                 CDEBUG(D_NET, "page %p + offset %x for %d\n",
141                                page, offset, kiov->kiov_len);
142
143                 if (!list_empty(&conn->ksnc_tx_queue) ||
144                     fragsize < tx->tx_resid)
145                         msgflg |= MSG_MORE;
146
147                 if (sk->sk_prot->sendpage != NULL) {
148                         rc = sk->sk_prot->sendpage(sk, page,
149                                                    offset, fragsize, msgflg);
150                 } else {
151                         rc = cfs_tcp_sendpage(sk, page, offset, fragsize,
152                                               msgflg);
153                 }
154         } else {
155 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
156                 struct iovec  scratch;
157                 struct iovec *scratchiov = &scratch;
158                 unsigned int  niov = 1;
159 #else
160 #ifdef CONFIG_HIGHMEM
161 #warning "XXX risk of kmap deadlock on multiple frags..."
162 #endif
163                 struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
164                 unsigned int  niov = tx->tx_nkiov;
165 #endif
166                 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
167                 int        i;
168
169                 for (nob = i = 0; i < niov; i++) {
170                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
171                                                  kiov[i].kiov_offset;
172                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
173                 }
174
175                 if (!list_empty(&conn->ksnc_tx_queue) ||
176                     nob < tx->tx_resid)
177                         msg.msg_flags |= MSG_MORE;
178
179                 rc = kernel_sendmsg(sock, &msg, (struct kvec *)scratchiov, niov, nob);
180
181                 for (i = 0; i < niov; i++)
182                         kunmap(kiov[i].kiov_page);
183         }
184         return rc;
185 }
186
187 void
188 ksocknal_lib_eager_ack (ksock_conn_t *conn)
189 {
190         int         opt = 1;
191         mm_segment_t   oldmm = get_fs();
192         struct socket *sock = conn->ksnc_sock;
193
194         /* Remind the socket to ACK eagerly.  If I don't, the socket might
195          * think I'm about to send something it could piggy-back the ACK
196          * on, introducing delay in completing zero-copy sends in my
197          * peer. */
198
199         set_fs(KERNEL_DS);
200         sock->ops->setsockopt (sock, SOL_TCP, TCP_QUICKACK,
201                                (char *)&opt, sizeof (opt));
202         set_fs(oldmm);
203 }
204
205 int
206 ksocknal_lib_recv_iov (ksock_conn_t *conn)
207 {
208 #if SOCKNAL_SINGLE_FRAG_RX
209         struct iovec  scratch;
210         struct iovec *scratchiov = &scratch;
211         unsigned int  niov = 1;
212 #else
213         struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
214         unsigned int  niov = conn->ksnc_rx_niov;
215 #endif
216         struct iovec *iov = conn->ksnc_rx_iov;
217         struct msghdr msg = {
218                 .msg_name       = NULL,
219                 .msg_namelen    = 0,
220                 .msg_iov        = scratchiov,
221                 .msg_iovlen     = niov,
222                 .msg_control    = NULL,
223                 .msg_controllen = 0,
224                 .msg_flags      = 0
225         };
226         mm_segment_t oldmm = get_fs();
227         int       nob;
228         int       i;
229         int       rc;
230         int       fragnob;
231         int       sum;
232         __u32   saved_csum;
233
234         /* NB we can't trust socket ops to either consume our iovs
235          * or leave them alone. */
236         LASSERT (niov > 0);
237
238         for (nob = i = 0; i < niov; i++) {
239                 scratchiov[i] = iov[i];
240                 nob += scratchiov[i].iov_len;
241         }
242         LASSERT (nob <= conn->ksnc_rx_nob_wanted);
243
244         set_fs (KERNEL_DS);
245         rc = sock_recvmsg (conn->ksnc_sock, &msg, nob, MSG_DONTWAIT);
246         /* NB this is just a boolean..........................^ */
247         set_fs (oldmm);
248
249         saved_csum = 0;
250         if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
251                 saved_csum = conn->ksnc_msg.ksm_csum;
252                 conn->ksnc_msg.ksm_csum = 0;
253         }
254
255         if (saved_csum != 0) {
256                 /* accumulate checksum */
257                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
258                         LASSERT (i < niov);
259
260                         fragnob = iov[i].iov_len;
261                         if (fragnob > sum)
262                                 fragnob = sum;
263
264                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
265                                                            iov[i].iov_base, fragnob);
266                 }
267                 conn->ksnc_msg.ksm_csum = saved_csum;
268         }
269
270         return rc;
271 }
272
273 static void
274 ksocknal_lib_kiov_vunmap(void *addr)
275 {
276         if (addr == NULL)
277                 return;
278
279         vunmap(addr);
280 }
281
282 static void *
283 ksocknal_lib_kiov_vmap(lnet_kiov_t *kiov, int niov,
284                        struct iovec *iov, struct page **pages)
285 {
286         void         *addr;
287         int            nob;
288         int            i;
289
290         if (!*ksocknal_tunables.ksnd_zc_recv || pages == NULL)
291                 return NULL;
292
293         LASSERT (niov <= LNET_MAX_IOV);
294
295         if (niov < 2 ||
296             niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
297                 return NULL;
298
299         for (nob = i = 0; i < niov; i++) {
300                 if ((kiov[i].kiov_offset != 0 && i > 0) ||
301                     (kiov[i].kiov_offset + kiov[i].kiov_len != PAGE_CACHE_SIZE && i < niov - 1))
302                         return NULL;
303
304                 pages[i] = kiov[i].kiov_page;
305                 nob += kiov[i].kiov_len;
306         }
307
308         addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
309         if (addr == NULL)
310                 return NULL;
311
312         iov->iov_base = addr + kiov[0].kiov_offset;
313         iov->iov_len = nob;
314
315         return addr;
316 }
317
318 int
319 ksocknal_lib_recv_kiov (ksock_conn_t *conn)
320 {
321 #if SOCKNAL_SINGLE_FRAG_RX || !SOCKNAL_RISK_KMAP_DEADLOCK
322         struct iovec   scratch;
323         struct iovec  *scratchiov = &scratch;
324         struct page  **pages      = NULL;
325         unsigned int   niov       = 1;
326 #else
327 #ifdef CONFIG_HIGHMEM
328 #warning "XXX risk of kmap deadlock on multiple frags..."
329 #endif
330         struct iovec  *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
331         struct page  **pages      = conn->ksnc_scheduler->kss_rx_scratch_pgs;
332         unsigned int   niov       = conn->ksnc_rx_nkiov;
333 #endif
334         lnet_kiov_t   *kiov = conn->ksnc_rx_kiov;
335         struct msghdr msg = {
336                 .msg_name       = NULL,
337                 .msg_namelen    = 0,
338                 .msg_iov        = scratchiov,
339                 .msg_control    = NULL,
340                 .msg_controllen = 0,
341                 .msg_flags      = 0
342         };
343         mm_segment_t oldmm = get_fs();
344         int       nob;
345         int       i;
346         int       rc;
347         void    *base;
348         void    *addr;
349         int       sum;
350         int       fragnob;
351
352         /* NB we can't trust socket ops to either consume our iovs
353          * or leave them alone. */
354         if ((addr = ksocknal_lib_kiov_vmap(kiov, niov, scratchiov, pages)) != NULL) {
355                 nob = scratchiov[0].iov_len;
356                 msg.msg_iovlen = 1;
357
358         } else {
359                 for (nob = i = 0; i < niov; i++) {
360                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
361                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
362                                                  kiov[i].kiov_offset;
363                 }
364                 msg.msg_iovlen = niov;
365         }
366
367         LASSERT (nob <= conn->ksnc_rx_nob_wanted);
368
369         set_fs (KERNEL_DS);
370         rc = sock_recvmsg (conn->ksnc_sock, &msg, nob, MSG_DONTWAIT);
371         /* NB this is just a boolean.......................^ */
372         set_fs (oldmm);
373
374         if (conn->ksnc_msg.ksm_csum != 0) {
375                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
376                         LASSERT (i < niov);
377
378                         /* Dang! have to kmap again because I have nowhere to stash the
379                          * mapped address.  But by doing it while the page is still
380                          * mapped, the kernel just bumps the map count and returns me
381                          * the address it stashed. */
382                         base = kmap(kiov[i].kiov_page) + kiov[i].kiov_offset;
383                         fragnob = kiov[i].kiov_len;
384                         if (fragnob > sum)
385                                 fragnob = sum;
386
387                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
388                                                            base, fragnob);
389
390                         kunmap(kiov[i].kiov_page);
391                 }
392         }
393
394         if (addr != NULL) {
395                 ksocknal_lib_kiov_vunmap(addr);
396         } else {
397                 for (i = 0; i < niov; i++)
398                         kunmap(kiov[i].kiov_page);
399         }
400
401         return (rc);
402 }
403
404 void
405 ksocknal_lib_csum_tx(ksock_tx_t *tx)
406 {
407         int       i;
408         __u32   csum;
409         void    *base;
410
411         LASSERT(tx->tx_iov[0].iov_base == (void *)&tx->tx_msg);
412         LASSERT(tx->tx_conn != NULL);
413         LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
414
415         tx->tx_msg.ksm_csum = 0;
416
417         csum = ksocknal_csum(~0, (void *)tx->tx_iov[0].iov_base,
418                              tx->tx_iov[0].iov_len);
419
420         if (tx->tx_kiov != NULL) {
421                 for (i = 0; i < tx->tx_nkiov; i++) {
422                         base = kmap(tx->tx_kiov[i].kiov_page) +
423                                tx->tx_kiov[i].kiov_offset;
424
425                         csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
426
427                         kunmap(tx->tx_kiov[i].kiov_page);
428                 }
429         } else {
430                 for (i = 1; i < tx->tx_niov; i++)
431                         csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
432                                              tx->tx_iov[i].iov_len);
433         }
434
435         if (*ksocknal_tunables.ksnd_inject_csum_error) {
436                 csum++;
437                 *ksocknal_tunables.ksnd_inject_csum_error = 0;
438         }
439
440         tx->tx_msg.ksm_csum = csum;
441 }
442
443 int
444 ksocknal_lib_get_conn_tunables (ksock_conn_t *conn, int *txmem, int *rxmem, int *nagle)
445 {
446         mm_segment_t   oldmm = get_fs ();
447         struct socket *sock = conn->ksnc_sock;
448         int         len;
449         int         rc;
450
451         rc = ksocknal_connsock_addref(conn);
452         if (rc != 0) {
453                 LASSERT (conn->ksnc_closing);
454                 *txmem = *rxmem = *nagle = 0;
455                 return (-ESHUTDOWN);
456         }
457
458         rc = libcfs_sock_getbuf(sock, txmem, rxmem);
459         if (rc == 0) {
460                 len = sizeof(*nagle);
461                 set_fs(KERNEL_DS);
462                 rc = sock->ops->getsockopt(sock, SOL_TCP, TCP_NODELAY,
463                                            (char *)nagle, &len);
464                 set_fs(oldmm);
465         }
466
467         ksocknal_connsock_decref(conn);
468
469         if (rc == 0)
470                 *nagle = !*nagle;
471         else
472                 *txmem = *rxmem = *nagle = 0;
473
474         return (rc);
475 }
476
477 int
478 ksocknal_lib_setup_sock (struct socket *sock)
479 {
480         mm_segment_t    oldmm = get_fs ();
481         int          rc;
482         int          option;
483         int          keep_idle;
484         int          keep_intvl;
485         int          keep_count;
486         int          do_keepalive;
487         struct linger   linger;
488
489         sock->sk->sk_allocation = GFP_NOFS;
490
491         /* Ensure this socket aborts active sends immediately when we close
492          * it. */
493
494         linger.l_onoff = 0;
495         linger.l_linger = 0;
496
497         set_fs (KERNEL_DS);
498         rc = sock_setsockopt (sock, SOL_SOCKET, SO_LINGER,
499                               (char *)&linger, sizeof (linger));
500         set_fs (oldmm);
501         if (rc != 0) {
502                 CERROR ("Can't set SO_LINGER: %d\n", rc);
503                 return (rc);
504         }
505
506         option = -1;
507         set_fs (KERNEL_DS);
508         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_LINGER2,
509                                     (char *)&option, sizeof (option));
510         set_fs (oldmm);
511         if (rc != 0) {
512                 CERROR ("Can't set SO_LINGER2: %d\n", rc);
513                 return (rc);
514         }
515
516         if (!*ksocknal_tunables.ksnd_nagle) {
517                 option = 1;
518
519                 set_fs (KERNEL_DS);
520                 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_NODELAY,
521                                             (char *)&option, sizeof (option));
522                 set_fs (oldmm);
523                 if (rc != 0) {
524                         CERROR ("Can't disable nagle: %d\n", rc);
525                         return (rc);
526                 }
527         }
528
529         rc = libcfs_sock_setbuf(sock,
530                                 *ksocknal_tunables.ksnd_tx_buffer_size,
531                                 *ksocknal_tunables.ksnd_rx_buffer_size);
532         if (rc != 0) {
533                 CERROR ("Can't set buffer tx %d, rx %d buffers: %d\n",
534                         *ksocknal_tunables.ksnd_tx_buffer_size,
535                         *ksocknal_tunables.ksnd_rx_buffer_size, rc);
536                 return (rc);
537         }
538
539 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
540
541         /* snapshot tunables */
542         keep_idle  = *ksocknal_tunables.ksnd_keepalive_idle;
543         keep_count = *ksocknal_tunables.ksnd_keepalive_count;
544         keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
545
546         do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
547
548         option = (do_keepalive ? 1 : 0);
549         set_fs (KERNEL_DS);
550         rc = sock_setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE,
551                               (char *)&option, sizeof (option));
552         set_fs (oldmm);
553         if (rc != 0) {
554                 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
555                 return (rc);
556         }
557
558         if (!do_keepalive)
559                 return (0);
560
561         set_fs (KERNEL_DS);
562         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPIDLE,
563                                     (char *)&keep_idle, sizeof (keep_idle));
564         set_fs (oldmm);
565         if (rc != 0) {
566                 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
567                 return (rc);
568         }
569
570         set_fs (KERNEL_DS);
571         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPINTVL,
572                                     (char *)&keep_intvl, sizeof (keep_intvl));
573         set_fs (oldmm);
574         if (rc != 0) {
575                 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
576                 return (rc);
577         }
578
579         set_fs (KERNEL_DS);
580         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPCNT,
581                                     (char *)&keep_count, sizeof (keep_count));
582         set_fs (oldmm);
583         if (rc != 0) {
584                 CERROR ("Can't set TCP_KEEPCNT: %d\n", rc);
585                 return (rc);
586         }
587
588         return (0);
589 }
590
591 void
592 ksocknal_lib_push_conn (ksock_conn_t *conn)
593 {
594         struct sock    *sk;
595         struct tcp_sock *tp;
596         int          nonagle;
597         int          val = 1;
598         int          rc;
599         mm_segment_t    oldmm;
600
601         rc = ksocknal_connsock_addref(conn);
602         if (rc != 0)                        /* being shut down */
603                 return;
604
605         sk = conn->ksnc_sock->sk;
606         tp = tcp_sk(sk);
607
608         lock_sock (sk);
609         nonagle = tp->nonagle;
610         tp->nonagle = 1;
611         release_sock (sk);
612
613         oldmm = get_fs ();
614         set_fs (KERNEL_DS);
615
616         rc = sk->sk_prot->setsockopt (sk, SOL_TCP, TCP_NODELAY,
617                                       (char *)&val, sizeof (val));
618         LASSERT (rc == 0);
619
620         set_fs (oldmm);
621
622         lock_sock (sk);
623         tp->nonagle = nonagle;
624         release_sock (sk);
625
626         ksocknal_connsock_decref(conn);
627 }
628
629 extern void ksocknal_read_callback (ksock_conn_t *conn);
630 extern void ksocknal_write_callback (ksock_conn_t *conn);
631 /*
632  * socket call back in Linux
633  */
634 static void
635 ksocknal_data_ready (struct sock *sk, int n)
636 {
637         ksock_conn_t  *conn;
638
639         /* interleave correctly with closing sockets... */
640         LASSERT(!in_irq());
641         read_lock(&ksocknal_data.ksnd_global_lock);
642
643         conn = sk->sk_user_data;
644         if (conn == NULL) {          /* raced with ksocknal_terminate_conn */
645                 LASSERT (sk->sk_data_ready != &ksocknal_data_ready);
646                 sk->sk_data_ready (sk, n);
647         } else
648                 ksocknal_read_callback(conn);
649
650         read_unlock(&ksocknal_data.ksnd_global_lock);
651 }
652
653 static void
654 ksocknal_write_space (struct sock *sk)
655 {
656         ksock_conn_t  *conn;
657         int         wspace;
658         int         min_wpace;
659
660         /* interleave correctly with closing sockets... */
661         LASSERT(!in_irq());
662         read_lock(&ksocknal_data.ksnd_global_lock);
663
664         conn = sk->sk_user_data;
665         wspace = SOCKNAL_WSPACE(sk);
666         min_wpace = SOCKNAL_MIN_WSPACE(sk);
667
668         CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
669                sk, wspace, min_wpace, conn,
670                (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
671                                       " ready" : " blocked"),
672                (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
673                                       " scheduled" : " idle"),
674                (conn == NULL) ? "" : (list_empty (&conn->ksnc_tx_queue) ?
675                                       " empty" : " queued"));
676
677         if (conn == NULL) {          /* raced with ksocknal_terminate_conn */
678                 LASSERT (sk->sk_write_space != &ksocknal_write_space);
679                 sk->sk_write_space (sk);
680
681                 read_unlock(&ksocknal_data.ksnd_global_lock);
682                 return;
683         }
684
685         if (wspace >= min_wpace) {            /* got enough space */
686                 ksocknal_write_callback(conn);
687
688                 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
689                  * ENOMEM check in ksocknal_transmit is race-free (think about
690                  * it). */
691
692                 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
693         }
694
695         read_unlock(&ksocknal_data.ksnd_global_lock);
696 }
697
698 void
699 ksocknal_lib_save_callback(struct socket *sock, ksock_conn_t *conn)
700 {
701         conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
702         conn->ksnc_saved_write_space = sock->sk->sk_write_space;
703 }
704
705 void
706 ksocknal_lib_set_callback(struct socket *sock,  ksock_conn_t *conn)
707 {
708         sock->sk->sk_user_data = conn;
709         sock->sk->sk_data_ready = ksocknal_data_ready;
710         sock->sk->sk_write_space = ksocknal_write_space;
711         return;
712 }
713
714 void
715 ksocknal_lib_reset_callback(struct socket *sock, ksock_conn_t *conn)
716 {
717         /* Remove conn's network callbacks.
718          * NB I _have_ to restore the callback, rather than storing a noop,
719          * since the socket could survive past this module being unloaded!! */
720         sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
721         sock->sk->sk_write_space = conn->ksnc_saved_write_space;
722
723         /* A callback could be in progress already; they hold a read lock
724          * on ksnd_global_lock (to serialise with me) and NOOP if
725          * sk_user_data is NULL. */
726         sock->sk->sk_user_data = NULL;
727
728         return ;
729 }
730
731 int
732 ksocknal_lib_memory_pressure(ksock_conn_t *conn)
733 {
734         int         rc = 0;
735         ksock_sched_t *sched;
736
737         sched = conn->ksnc_scheduler;
738         spin_lock_bh(&sched->kss_lock);
739
740         if (!SOCK_TEST_NOSPACE(conn->ksnc_sock) &&
741             !conn->ksnc_tx_ready) {
742                 /* SOCK_NOSPACE is set when the socket fills
743                  * and cleared in the write_space callback
744                  * (which also sets ksnc_tx_ready).  If
745                  * SOCK_NOSPACE and ksnc_tx_ready are BOTH
746                  * zero, I didn't fill the socket and
747                  * write_space won't reschedule me, so I
748                  * return -ENOMEM to get my caller to retry
749                  * after a timeout */
750                 rc = -ENOMEM;
751         }
752
753         spin_unlock_bh(&sched->kss_lock);
754
755         return rc;
756 }