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