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