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