Whamcloud - gitweb
7f9ffe05419fa8133ba8c51d94c06c8651235ffb
[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         struct bio_vec *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->bv_page;
133                 int            offset = kiov->bv_offset;
134                 int            fragsize = kiov->bv_len;
135                 int            msgflg = MSG_DONTWAIT;
136
137                 CDEBUG(D_NET, "page %p + offset %x for %d\n",
138                                page, offset, kiov->bv_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].bv_page) +
166                                                  kiov[i].bv_offset;
167                         nob += scratchiov[i].iov_len = kiov[i].bv_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].bv_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(struct bio_vec *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].bv_offset != 0 && i > 0) ||
283                     (kiov[i].bv_offset + kiov[i].bv_len !=
284                      PAGE_SIZE && i < niov - 1))
285                         return NULL;
286
287                 pages[i] = kiov[i].bv_page;
288                 nob += kiov[i].bv_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].bv_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         struct bio_vec *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].bv_len;
338                         scratchiov[i].iov_base = kmap(kiov[i].bv_page) +
339                                                  kiov[i].bv_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
354                          * stash the mapped address.  But by doing it while the
355                          * page is still mapped, the kernel just bumps the map
356                          * count and returns me the address it stashed.
357                          */
358                         base = kmap(kiov[i].bv_page) + kiov[i].bv_offset;
359                         fragnob = kiov[i].bv_len;
360                         if (fragnob > sum)
361                                 fragnob = sum;
362
363                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
364                                                            base, fragnob);
365
366                         kunmap(kiov[i].bv_page);
367                 }
368         }
369
370         if (addr != NULL) {
371                 ksocknal_lib_kiov_vunmap(addr);
372         } else {
373                 for (i = 0; i < niov; i++)
374                         kunmap(kiov[i].bv_page);
375         }
376
377         return rc;
378 }
379
380 void
381 ksocknal_lib_csum_tx(struct ksock_tx *tx)
382 {
383         int          i;
384         __u32        csum;
385         void        *base;
386
387         LASSERT(tx->tx_iov[0].iov_base == (void *)&tx->tx_msg);
388         LASSERT(tx->tx_conn != NULL);
389         LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
390
391         tx->tx_msg.ksm_csum = 0;
392
393         csum = ksocknal_csum(~0, (void *)tx->tx_iov[0].iov_base,
394                              tx->tx_iov[0].iov_len);
395
396         if (tx->tx_kiov != NULL) {
397                 for (i = 0; i < tx->tx_nkiov; i++) {
398                         base = kmap(tx->tx_kiov[i].bv_page) +
399                                tx->tx_kiov[i].bv_offset;
400
401                         csum = ksocknal_csum(csum, base, tx->tx_kiov[i].bv_len);
402
403                         kunmap(tx->tx_kiov[i].bv_page);
404                 }
405         } else {
406                 for (i = 1; i < tx->tx_niov; i++)
407                         csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
408                                              tx->tx_iov[i].iov_len);
409         }
410
411         if (*ksocknal_tunables.ksnd_inject_csum_error) {
412                 csum++;
413                 *ksocknal_tunables.ksnd_inject_csum_error = 0;
414         }
415
416         tx->tx_msg.ksm_csum = csum;
417 }
418
419 int
420 ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem, int *rxmem, int *nagle)
421 {
422         struct socket *sock = conn->ksnc_sock;
423         int            len;
424         int            rc;
425
426         rc = ksocknal_connsock_addref(conn);
427         if (rc != 0) {
428                 LASSERT (conn->ksnc_closing);
429                 *txmem = *rxmem = *nagle = 0;
430                 return (-ESHUTDOWN);
431         }
432
433         rc = lnet_sock_getbuf(sock, txmem, rxmem);
434         if (rc == 0) {
435                 len = sizeof(*nagle);
436                 rc = kernel_getsockopt(sock, SOL_TCP, TCP_NODELAY,
437                                        (char *)nagle, &len);
438         }
439
440         ksocknal_connsock_decref(conn);
441
442         if (rc == 0)
443                 *nagle = !*nagle;
444         else
445                 *txmem = *rxmem = *nagle = 0;
446
447         return (rc);
448 }
449
450 int
451 ksocknal_lib_setup_sock (struct socket *sock)
452 {
453         int             rc;
454         int             option;
455         int             keep_idle;
456         int             keep_intvl;
457         int             keep_count;
458         int             do_keepalive;
459         struct linger   linger;
460
461         sock->sk->sk_allocation = GFP_NOFS;
462
463         /* Ensure this socket aborts active sends immediately when we close
464          * it. */
465
466         linger.l_onoff = 0;
467         linger.l_linger = 0;
468
469         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
470                                (char *)&linger, sizeof(linger));
471         if (rc != 0) {
472                 CERROR ("Can't set SO_LINGER: %d\n", rc);
473                 return (rc);
474         }
475
476         option = -1;
477         rc = kernel_setsockopt(sock, SOL_TCP, TCP_LINGER2,
478                                (char *)&option, sizeof(option));
479         if (rc != 0) {
480                 CERROR ("Can't set SO_LINGER2: %d\n", rc);
481                 return (rc);
482         }
483
484         if (!*ksocknal_tunables.ksnd_nagle) {
485                 option = 1;
486
487                 rc = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
488                                        (char *)&option, sizeof(option));
489                 if (rc != 0) {
490                         CERROR ("Can't disable nagle: %d\n", rc);
491                         return (rc);
492                 }
493         }
494
495         rc = lnet_sock_setbuf(sock,
496                               *ksocknal_tunables.ksnd_tx_buffer_size,
497                               *ksocknal_tunables.ksnd_rx_buffer_size);
498         if (rc != 0) {
499                 CERROR ("Can't set buffer tx %d, rx %d buffers: %d\n",
500                         *ksocknal_tunables.ksnd_tx_buffer_size,
501                         *ksocknal_tunables.ksnd_rx_buffer_size, rc);
502                 return (rc);
503         }
504
505 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
506 #ifdef SOCKNAL_BACKOFF
507         if (*ksocknal_tunables.ksnd_backoff_init > 0) {
508                 option = *ksocknal_tunables.ksnd_backoff_init;
509 #ifdef SOCKNAL_BACKOFF_MS
510                 option *= 1000;
511 #endif
512
513                 rc = kernel_setsockopt(sock, SOL_TCP, TCP_BACKOFF_INIT,
514                                        (char *)&option, sizeof(option));
515                 if (rc != 0) {
516                         CERROR ("Can't set initial tcp backoff %d: %d\n",
517                                 option, rc);
518                         return (rc);
519                 }
520         }
521
522         if (*ksocknal_tunables.ksnd_backoff_max > 0) {
523                 option = *ksocknal_tunables.ksnd_backoff_max;
524 #ifdef SOCKNAL_BACKOFF_MS
525                 option *= 1000;
526 #endif
527
528                 rc = kernel_setsockopt(sock, SOL_TCP, TCP_BACKOFF_MAX,
529                                        (char *)&option, sizeof(option));
530                 if (rc != 0) {
531                         CERROR ("Can't set maximum tcp backoff %d: %d\n",
532                                 option, rc);
533                         return (rc);
534                 }
535         }
536 #endif
537
538         /* snapshot tunables */
539         keep_idle  = *ksocknal_tunables.ksnd_keepalive_idle;
540         keep_count = *ksocknal_tunables.ksnd_keepalive_count;
541         keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
542
543         do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
544
545         option = (do_keepalive ? 1 : 0);
546         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
547                                (char *)&option, sizeof(option));
548         if (rc != 0) {
549                 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
550                 return (rc);
551         }
552
553         if (!do_keepalive)
554                 return (0);
555
556         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
557                                (char *)&keep_idle, sizeof(keep_idle));
558         if (rc != 0) {
559                 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
560                 return (rc);
561         }
562
563         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
564                                (char *)&keep_intvl, sizeof(keep_intvl));
565         if (rc != 0) {
566                 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
567                 return (rc);
568         }
569
570         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
571                                (char *)&keep_count, sizeof(keep_count));
572         if (rc != 0) {
573                 CERROR ("Can't set TCP_KEEPCNT: %d\n", rc);
574                 return (rc);
575         }
576
577         return (0);
578 }
579
580 void
581 ksocknal_lib_push_conn(struct ksock_conn *conn)
582 {
583         struct sock *sk;
584         struct tcp_sock *tp;
585         int nonagle;
586         int val = 1;
587         int rc;
588
589         rc = ksocknal_connsock_addref(conn);
590         if (rc != 0)                            /* being shut down */
591                 return;
592
593         sk = conn->ksnc_sock->sk;
594         tp = tcp_sk(sk);
595
596         lock_sock (sk);
597         nonagle = tp->nonagle;
598         tp->nonagle = 1;
599         release_sock (sk);
600
601         rc = kernel_setsockopt(conn->ksnc_sock, SOL_TCP, TCP_NODELAY,
602                                (char *)&val, sizeof(val));
603         LASSERT (rc == 0);
604
605         lock_sock (sk);
606         tp->nonagle = nonagle;
607         release_sock (sk);
608
609         ksocknal_connsock_decref(conn);
610 }
611
612 void ksocknal_read_callback(struct ksock_conn *conn);
613 void ksocknal_write_callback(struct ksock_conn *conn);
614 /*
615  * socket call back in Linux
616  */
617 static void
618 #ifdef HAVE_SK_DATA_READY_ONE_ARG
619 ksocknal_data_ready(struct sock *sk)
620 #else
621 ksocknal_data_ready(struct sock *sk, int n)
622 #endif
623 {
624         struct ksock_conn  *conn;
625         ENTRY;
626
627         /* interleave correctly with closing sockets... */
628         LASSERT(!in_irq());
629         read_lock(&ksocknal_data.ksnd_global_lock);
630
631         conn = sk->sk_user_data;
632         if (conn == NULL) {     /* raced with ksocknal_terminate_conn */
633                 LASSERT(sk->sk_data_ready != &ksocknal_data_ready);
634 #ifdef HAVE_SK_DATA_READY_ONE_ARG
635                 sk->sk_data_ready(sk);
636 #else
637                 sk->sk_data_ready(sk, n);
638 #endif
639         } else
640                 ksocknal_read_callback(conn);
641
642         read_unlock(&ksocknal_data.ksnd_global_lock);
643
644         EXIT;
645 }
646
647 static void
648 ksocknal_write_space (struct sock *sk)
649 {
650         struct ksock_conn  *conn;
651         int            wspace;
652         int            min_wpace;
653
654         /* interleave correctly with closing sockets... */
655         LASSERT(!in_irq());
656         read_lock(&ksocknal_data.ksnd_global_lock);
657
658         conn = sk->sk_user_data;
659         wspace = sk_stream_wspace(sk);
660         min_wpace = sk_stream_min_wspace(sk);
661
662         CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
663                sk, wspace, min_wpace, conn,
664                (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
665                                       " ready" : " blocked"),
666                (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
667                                       " scheduled" : " idle"),
668                (conn == NULL) ? "" : (list_empty(&conn->ksnc_tx_queue) ?
669                                       " empty" : " queued"));
670
671         if (conn == NULL) {             /* raced with ksocknal_terminate_conn */
672                 LASSERT (sk->sk_write_space != &ksocknal_write_space);
673                 sk->sk_write_space (sk);
674
675                 read_unlock(&ksocknal_data.ksnd_global_lock);
676                 return;
677         }
678
679         if (wspace >= min_wpace) {              /* got enough space */
680                 ksocknal_write_callback(conn);
681
682                 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
683                  * ENOMEM check in ksocknal_transmit is race-free (think about
684                  * it). */
685
686                 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
687         }
688
689         read_unlock(&ksocknal_data.ksnd_global_lock);
690 }
691
692 void
693 ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn)
694 {
695         conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
696         conn->ksnc_saved_write_space = sock->sk->sk_write_space;
697 }
698
699 void
700 ksocknal_lib_set_callback(struct socket *sock,  struct ksock_conn *conn)
701 {
702         sock->sk->sk_user_data = conn;
703         sock->sk->sk_data_ready = ksocknal_data_ready;
704         sock->sk->sk_write_space = ksocknal_write_space;
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 }