Whamcloud - gitweb
LU-936 Remove LUSTRE_KERNEL_VERSION
[fs/lustre-release.git] / lnet / klnds / socklnd / socklnd_lib-linux.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include "socklnd.h"
38
39 # if defined(CONFIG_SYSCTL) && !CFS_SYSFS_MODULE_PARM
40
41 #ifndef HAVE_SYSCTL_UNNUMBERED
42
43 enum {
44         SOCKLND_TIMEOUT = 1,
45         SOCKLND_CREDITS,
46         SOCKLND_PEER_TXCREDITS,
47         SOCKLND_PEER_RTRCREDITS,
48         SOCKLND_PEER_TIMEOUT,
49         SOCKLND_NCONNDS,
50         SOCKLND_RECONNECTS_MIN,
51         SOCKLND_RECONNECTS_MAX,
52         SOCKLND_EAGER_ACK,
53         SOCKLND_ZERO_COPY,
54         SOCKLND_TYPED,
55         SOCKLND_BULK_MIN,
56         SOCKLND_RX_BUFFER_SIZE,
57         SOCKLND_TX_BUFFER_SIZE,
58         SOCKLND_NAGLE,
59         SOCKLND_IRQ_AFFINITY,
60         SOCKLND_ROUND_ROBIN,
61         SOCKLND_KEEPALIVE,
62         SOCKLND_KEEPALIVE_IDLE,
63         SOCKLND_KEEPALIVE_COUNT,
64         SOCKLND_KEEPALIVE_INTVL,
65         SOCKLND_BACKOFF_INIT,
66         SOCKLND_BACKOFF_MAX,
67         SOCKLND_PROTOCOL,
68         SOCKLND_ZERO_COPY_RECV,
69         SOCKLND_ZERO_COPY_RECV_MIN_NFRAGS
70 };
71 #else
72
73 #define SOCKLND_TIMEOUT         CTL_UNNUMBERED
74 #define SOCKLND_CREDITS         CTL_UNNUMBERED
75 #define SOCKLND_PEER_TXCREDITS  CTL_UNNUMBERED
76 #define SOCKLND_PEER_RTRCREDITS  CTL_UNNUMBERED
77 #define SOCKLND_PEER_TIMEOUT    CTL_UNNUMBERED
78 #define SOCKLND_NCONNDS         CTL_UNNUMBERED
79 #define SOCKLND_RECONNECTS_MIN  CTL_UNNUMBERED
80 #define SOCKLND_RECONNECTS_MAX  CTL_UNNUMBERED
81 #define SOCKLND_EAGER_ACK       CTL_UNNUMBERED
82 #define SOCKLND_ZERO_COPY       CTL_UNNUMBERED
83 #define SOCKLND_TYPED           CTL_UNNUMBERED
84 #define SOCKLND_BULK_MIN        CTL_UNNUMBERED
85 #define SOCKLND_RX_BUFFER_SIZE  CTL_UNNUMBERED
86 #define SOCKLND_TX_BUFFER_SIZE  CTL_UNNUMBERED
87 #define SOCKLND_NAGLE           CTL_UNNUMBERED
88 #define SOCKLND_IRQ_AFFINITY    CTL_UNNUMBERED
89 #define SOCKLND_ROUND_ROBIN     CTL_UNNUMBERED
90 #define SOCKLND_KEEPALIVE       CTL_UNNUMBERED
91 #define SOCKLND_KEEPALIVE_IDLE  CTL_UNNUMBERED
92 #define SOCKLND_KEEPALIVE_COUNT CTL_UNNUMBERED
93 #define SOCKLND_KEEPALIVE_INTVL CTL_UNNUMBERED
94 #define SOCKLND_BACKOFF_INIT    CTL_UNNUMBERED
95 #define SOCKLND_BACKOFF_MAX     CTL_UNNUMBERED
96 #define SOCKLND_PROTOCOL        CTL_UNNUMBERED
97 #define SOCKLND_ZERO_COPY_RECV  CTL_UNNUMBERED
98 #define SOCKLND_ZERO_COPY_RECV_MIN_NFRAGS CTL_UNNUMBERED
99 #endif
100
101 static cfs_sysctl_table_t ksocknal_ctl_table[] = {
102         {
103                 .ctl_name = SOCKLND_TIMEOUT,
104                 .procname = "timeout",
105                 .data     = &ksocknal_tunables.ksnd_timeout,
106                 .maxlen   = sizeof (int),
107                 .mode     = 0644,
108                 .proc_handler = &proc_dointvec,
109                 .strategy = &sysctl_intvec,
110         },
111         {
112                 .ctl_name = SOCKLND_CREDITS,
113                 .procname = "credits",
114                 .data     = &ksocknal_tunables.ksnd_credits,
115                 .maxlen   = sizeof (int),
116                 .mode     = 0444,
117                 .proc_handler = &proc_dointvec,
118                 .strategy = &sysctl_intvec,
119         },
120          {
121                 .ctl_name = SOCKLND_PEER_TXCREDITS,
122                 .procname = "peer_credits",
123                 .data     = &ksocknal_tunables.ksnd_peertxcredits,
124                 .maxlen   = sizeof (int),
125                 .mode     = 0444,
126                 .proc_handler = &proc_dointvec,
127                 .strategy = &sysctl_intvec,
128         },
129          {
130                 .ctl_name = SOCKLND_PEER_RTRCREDITS,
131                 .procname = "peer_buffer_credits",
132                 .data     = &ksocknal_tunables.ksnd_peerrtrcredits,
133                 .maxlen   = sizeof (int),
134                 .mode     = 0444,
135                 .proc_handler = &proc_dointvec,
136                 .strategy = &sysctl_intvec,
137         },
138         {
139                 .ctl_name = SOCKLND_PEER_TIMEOUT,
140                 .procname = "peer_timeout",
141                 .data     = &ksocknal_tunables.ksnd_peertimeout,
142                 .maxlen   = sizeof (int),
143                 .mode     = 0444,
144                 .proc_handler = &proc_dointvec
145                 .strategy = &sysctl_intvec,
146         },
147         {
148                 .ctl_name = SOCKLND_NCONNDS,
149                 .procname = "nconnds",
150                 .data     = &ksocknal_tunables.ksnd_nconnds,
151                 .maxlen   = sizeof (int),
152                 .mode     = 0444,
153                 .proc_handler = &proc_dointvec,
154                 .strategy = &sysctl_intvec,
155         },
156         {
157                 .ctl_name = SOCKLND_RECONNECTS_MIN,
158                 .procname = "min_reconnectms",
159                 .data     = &ksocknal_tunables.ksnd_min_reconnectms,
160                 .maxlen   = sizeof (int),
161                 .mode     = 0444,
162                 .proc_handler = &proc_dointvec,
163                 .strategy = &sysctl_intvec,
164         },
165         {
166                 .ctl_name = SOCKLND_RECONNECTS_MAX,
167                 .procname = "max_reconnectms",
168                 .data     = &ksocknal_tunables.ksnd_max_reconnectms,
169                 .maxlen   = sizeof (int),
170                 .mode     = 0444,
171                 .proc_handler = &proc_dointvec,
172                 .strategy = &sysctl_intvec,
173         },
174         {
175                 .ctl_name = SOCKLND_EAGER_ACK,
176                 .procname = "eager_ack",
177                 .data     = &ksocknal_tunables.ksnd_eager_ack,
178                 .maxlen   = sizeof (int),
179                 .mode     = 0644,
180                 .proc_handler = &proc_dointvec,
181                 .strategy = &sysctl_intvec,
182         },
183         {
184                 .ctl_name = SOCKLND_ZERO_COPY,
185                 .procname = "zero_copy",
186                 .data     = &ksocknal_tunables.ksnd_zc_min_payload,
187                 .maxlen   = sizeof (int),
188                 .mode     = 0644,
189                 .proc_handler = &proc_dointvec,
190                 .strategy = &sysctl_intvec,
191         },
192         {
193                 .ctl_name = SOCKLND_ZERO_COPY_RECV,
194                 .procname = "zero_copy_recv",
195                 .data     = &ksocknal_tunables.ksnd_zc_recv,
196                 .maxlen   = sizeof (int),
197                 .mode     = 0644,
198                 .proc_handler = &proc_dointvec,
199                 .strategy = &sysctl_intvec,
200         },
201
202         {
203                 .ctl_name = SOCKLND_ZERO_COPY_RECV_MIN_NFRAGS,
204                 .procname = "zero_copy_recv",
205                 .data     = &ksocknal_tunables.ksnd_zc_recv_min_nfrags,
206                 .maxlen   = sizeof (int),
207                 .mode     = 0644,
208                 .proc_handler = &proc_dointvec,
209                 .strategy = &sysctl_intvec,
210         },
211         {
212                 .ctl_name = SOCKLND_TYPED,
213                 .procname = "typed",
214                 .data     = &ksocknal_tunables.ksnd_typed_conns,
215                 .maxlen   = sizeof (int),
216                 .mode     = 0444,
217                 .proc_handler = &proc_dointvec,
218                 .strategy = &sysctl_intvec,
219         },
220         {
221                 .ctl_name = SOCKLND_BULK_MIN,
222                 .procname = "min_bulk",
223                 .data     = &ksocknal_tunables.ksnd_min_bulk,
224                 .maxlen   = sizeof (int),
225                 .mode     = 0644,
226                 .proc_handler = &proc_dointvec,
227                 .strategy = &sysctl_intvec,
228         },
229         {
230                 .ctl_name = SOCKLND_RX_BUFFER_SIZE,
231                 .procname = "rx_buffer_size",
232                 .data     = &ksocknal_tunables.ksnd_rx_buffer_size,
233                 .maxlen   = sizeof(int),
234                 .mode     = 0644,
235                 .proc_handler = &proc_dointvec,
236                 .strategy = &sysctl_intvec,
237         },
238         {
239                 .ctl_name = SOCKLND_TX_BUFFER_SIZE,
240                 .procname = "tx_buffer_size",
241                 .data     = &ksocknal_tunables.ksnd_tx_buffer_size,
242                 .maxlen   = sizeof(int),
243                 .mode     = 0644,
244                 .proc_handler = &proc_dointvec,
245                 .strategy = &sysctl_intvec,
246         },
247         {
248                 .ctl_name = SOCKLND_NAGLE,
249                 .procname = "nagle",
250                 .data     = &ksocknal_tunables.ksnd_nagle,
251                 .maxlen   = sizeof(int),
252                 .mode     = 0644,
253                 .proc_handler = &proc_dointvec,
254                 .strategy = &sysctl_intvec,
255         },
256 #ifdef CPU_AFFINITY
257         {
258                 .ctl_name = SOCKLND_IRQ_AFFINITY,
259                 .procname = "irq_affinity",
260                 .data     = &ksocknal_tunables.ksnd_irq_affinity,
261                 .maxlen   = sizeof(int),
262                 .mode     = 0644,
263                 .proc_handler = &proc_dointvec,
264                 .strategy = &sysctl_intvec,
265         },
266 #endif
267         {
268                 .ctl_name = SOCKLND_ROUND_ROBIN,
269                 .procname = "round_robin",
270                 .data     = &ksocknal_tunables.ksnd_round_robin,
271                 .maxlen   = sizeof(int),
272                 .mode     = 0644,
273                 .proc_handler = &proc_dointvec,
274                 .strategy = &sysctl_intvec,
275         },
276         {
277                 .ctl_name = SOCKLND_KEEPALIVE,
278                 .procname = "keepalive",
279                 .data     = &ksocknal_tunables.ksnd_keepalive,
280                 .maxlen   = sizeof(int),
281                 .mode     = 0644,
282                 .proc_handler = &proc_dointvec,
283                 .strategy = &sysctl_intvec,
284         },
285         {
286                 .ctl_name = SOCKLND_KEEPALIVE_IDLE,
287                 .procname = "keepalive_idle",
288                 .data     = &ksocknal_tunables.ksnd_keepalive_idle,
289                 .maxlen   = sizeof(int),
290                 .mode     = 0644,
291                 .proc_handler = &proc_dointvec,
292                 .strategy = &sysctl_intvec,
293         },
294         {
295                 .ctl_name = SOCKLND_KEEPALIVE_COUNT,
296                 .procname = "keepalive_count",
297                 .data     = &ksocknal_tunables.ksnd_keepalive_count,
298                 .maxlen   = sizeof(int),
299                 .mode     = 0644,
300                 .proc_handler = &proc_dointvec,
301                 .strategy = &sysctl_intvec,
302         },
303         {
304                 .ctl_name = SOCKLND_KEEPALIVE_INTVL,
305                 .procname = "keepalive_intvl",
306                 .data     = &ksocknal_tunables.ksnd_keepalive_intvl,
307                 .maxlen   = sizeof(int),
308                 .mode     = 0644,
309                 .proc_handler = &proc_dointvec,
310                 .strategy = &sysctl_intvec,
311         },
312 #ifdef SOCKNAL_BACKOFF
313         {
314                 .ctl_name = SOCKLND_BACKOFF_INIT,
315                 .procname = "backoff_init",
316                 .data     = &ksocknal_tunables.ksnd_backoff_init,
317                 .maxlen   = sizeof(int),
318                 .mode     = 0644,
319                 .proc_handler = &proc_dointvec,
320                 .strategy = &sysctl_intvec,
321         },
322         {
323                 .ctl_name = SOCKLND_BACKOFF_MAX,
324                 .procname = "backoff_max",
325                 .data     = &ksocknal_tunables.ksnd_backoff_max,
326                 .maxlen   = sizeof(int),
327                 .mode     = 0644,
328                 .proc_handler = &proc_dointvec,
329                 .strategy = &sysctl_intvec,
330         },
331 #endif
332 #if SOCKNAL_VERSION_DEBUG
333         {
334                 .ctl_name = SOCKLND_PROTOCOL,
335                 .procname = "protocol",
336                 .data     = &ksocknal_tunables.ksnd_protocol,
337                 .maxlen   = sizeof(int),
338                 .mode     = 0644,
339                 .proc_handler = &proc_dointvec,
340                 .strategy = &sysctl_intvec,
341         },
342 #endif
343         {0}
344 };
345
346
347 cfs_sysctl_table_t ksocknal_top_ctl_table[] = {
348         {
349                 .ctl_name = CTL_SOCKLND,
350                 .procname = "socknal",
351                 .data     = NULL,
352                 .maxlen   = 0,
353                 .mode     = 0555,
354                 .child    = ksocknal_ctl_table
355         },
356         { 0 }
357 };
358
359 int
360 ksocknal_lib_tunables_init ()
361 {
362         if (!*ksocknal_tunables.ksnd_typed_conns) {
363                 int rc = -EINVAL;
364 #if SOCKNAL_VERSION_DEBUG
365                 if (*ksocknal_tunables.ksnd_protocol < 3)
366                         rc = 0;
367 #endif
368                 if (rc != 0) {
369                         CERROR("Protocol V3.x MUST have typed connections\n");
370                         return rc;
371                 }
372         }
373
374         if (*ksocknal_tunables.ksnd_zc_recv_min_nfrags < 2)
375                 *ksocknal_tunables.ksnd_zc_recv_min_nfrags = 2;
376         if (*ksocknal_tunables.ksnd_zc_recv_min_nfrags > LNET_MAX_IOV)
377                 *ksocknal_tunables.ksnd_zc_recv_min_nfrags = LNET_MAX_IOV;
378
379         ksocknal_tunables.ksnd_sysctl =
380                 cfs_register_sysctl_table(ksocknal_top_ctl_table, 0);
381
382         if (ksocknal_tunables.ksnd_sysctl == NULL)
383                 CWARN("Can't setup /proc tunables\n");
384
385         return 0;
386 }
387
388 void
389 ksocknal_lib_tunables_fini ()
390 {
391         if (ksocknal_tunables.ksnd_sysctl != NULL)
392                 cfs_unregister_sysctl_table(ksocknal_tunables.ksnd_sysctl);
393 }
394 #else
395 int
396 ksocknal_lib_tunables_init ()
397 {
398         return 0;
399 }
400
401 void
402 ksocknal_lib_tunables_fini ()
403 {
404 }
405 #endif /* # if CONFIG_SYSCTL && !CFS_SYSFS_MODULE_PARM */
406
407 void
408 ksocknal_lib_bind_irq (unsigned int irq)
409 {
410 #if (defined(CONFIG_SMP) && defined(CPU_AFFINITY))
411         int              bind;
412         int              cpu;
413         char             cmdline[64];
414         ksock_irqinfo_t *info;
415         char            *argv[] = {"/bin/sh",
416                                    "-c",
417                                    cmdline,
418                                    NULL};
419         char            *envp[] = {"HOME=/",
420                                    "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
421                                    NULL};
422
423         LASSERT (irq < NR_IRQS);
424         if (irq == 0)              /* software NIC or affinity disabled */
425                 return;
426
427         info = &ksocknal_data.ksnd_irqinfo[irq];
428
429         cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
430
431         LASSERT (info->ksni_valid);
432         bind = !info->ksni_bound;
433         info->ksni_bound = 1;
434
435         cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
436
437         if (!bind)                              /* bound already */
438                 return;
439
440         cpu = ksocknal_irqsched2cpu(info->ksni_sched);
441         snprintf (cmdline, sizeof (cmdline),
442                   "echo %d > /proc/irq/%u/smp_affinity", 1 << cpu, irq);
443
444         LCONSOLE_INFO("Binding irq %u to CPU %d with cmd: %s\n",
445                       irq, cpu, cmdline);
446
447         /* FIXME: Find a better method of setting IRQ affinity...
448          */
449
450         USERMODEHELPER(argv[0], argv, envp);
451 #endif
452 }
453
454 int
455 ksocknal_lib_get_conn_addrs (ksock_conn_t *conn)
456 {
457         int rc = libcfs_sock_getaddr(conn->ksnc_sock, 1,
458                                      &conn->ksnc_ipaddr,
459                                      &conn->ksnc_port);
460
461         /* Didn't need the {get,put}connsock dance to deref ksnc_sock... */
462         LASSERT (!conn->ksnc_closing);
463
464         if (rc != 0) {
465                 CERROR ("Error %d getting sock peer IP\n", rc);
466                 return rc;
467         }
468
469         rc = libcfs_sock_getaddr(conn->ksnc_sock, 0,
470                                  &conn->ksnc_myipaddr, NULL);
471         if (rc != 0) {
472                 CERROR ("Error %d getting sock local IP\n", rc);
473                 return rc;
474         }
475
476         return 0;
477 }
478
479 unsigned int
480 ksocknal_lib_sock_irq (struct socket *sock)
481 {
482         int                irq = 0;
483 #ifdef CPU_AFFINITY
484         struct dst_entry  *dst;
485
486         if (!*ksocknal_tunables.ksnd_irq_affinity)
487                 return 0;
488
489         dst = sk_dst_get (sock->sk);
490         if (dst != NULL) {
491                 if (dst->dev != NULL) {
492                         irq = dst->dev->irq;
493                         if (irq >= NR_IRQS) {
494                                 CERROR ("Unexpected IRQ %x\n", irq);
495                                 irq = 0;
496                         }
497                 }
498                 dst_release (dst);
499         }
500
501 #endif
502         return irq;
503 }
504
505 int
506 ksocknal_lib_zc_capable(ksock_conn_t *conn)
507 {
508         int  caps = conn->ksnc_sock->sk->sk_route_caps;
509
510         if (conn->ksnc_proto == &ksocknal_protocol_v1x)
511                 return 0;
512
513         /* ZC if the socket supports scatter/gather and doesn't need software
514          * checksums */
515         return ((caps & NETIF_F_SG) != 0 &&
516                 (caps & (NETIF_F_IP_CSUM | NETIF_F_NO_CSUM | NETIF_F_HW_CSUM)) != 0);
517 }
518
519 int
520 ksocknal_lib_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
521 {
522         struct socket *sock = conn->ksnc_sock;
523         int            nob;
524         int            rc;
525
526         if (*ksocknal_tunables.ksnd_enable_csum        && /* checksum enabled */
527             conn->ksnc_proto == &ksocknal_protocol_v2x && /* V2.x connection  */
528             tx->tx_nob == tx->tx_resid                 && /* frist sending    */
529             tx->tx_msg.ksm_csum == 0)                     /* not checksummed  */
530                 ksocknal_lib_csum_tx(tx);
531
532         /* NB we can't trust socket ops to either consume our iovs
533          * or leave them alone. */
534
535         {
536 #if SOCKNAL_SINGLE_FRAG_TX
537                 struct iovec    scratch;
538                 struct iovec   *scratchiov = &scratch;
539                 unsigned int    niov = 1;
540 #else
541                 struct iovec   *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
542                 unsigned int    niov = tx->tx_niov;
543 #endif
544                 struct msghdr msg = {
545                         .msg_name       = NULL,
546                         .msg_namelen    = 0,
547                         .msg_iov        = scratchiov,
548                         .msg_iovlen     = niov,
549                         .msg_control    = NULL,
550                         .msg_controllen = 0,
551                         .msg_flags      = MSG_DONTWAIT
552                 };
553                 mm_segment_t oldmm = get_fs();
554                 int  i;
555
556                 for (nob = i = 0; i < niov; i++) {
557                         scratchiov[i] = tx->tx_iov[i];
558                         nob += scratchiov[i].iov_len;
559                 }
560
561                 if (!cfs_list_empty(&conn->ksnc_tx_queue) ||
562                     nob < tx->tx_resid)
563                         msg.msg_flags |= MSG_MORE;
564
565                 set_fs (KERNEL_DS);
566                 rc = sock_sendmsg(sock, &msg, nob);
567                 set_fs (oldmm);
568         }
569         return rc;
570 }
571
572 int
573 ksocknal_lib_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
574 {
575         struct socket *sock = conn->ksnc_sock;
576         lnet_kiov_t   *kiov = tx->tx_kiov;
577         int            rc;
578         int            nob;
579
580         /* Not NOOP message */
581         LASSERT (tx->tx_lnetmsg != NULL);
582
583         /* NB we can't trust socket ops to either consume our iovs
584          * or leave them alone. */
585         if (tx->tx_msg.ksm_zc_cookies[0] != 0) {
586                 /* Zero copy is enabled */
587                 struct sock   *sk = sock->sk;
588                 struct page   *page = kiov->kiov_page;
589                 int            offset = kiov->kiov_offset;
590                 int            fragsize = kiov->kiov_len;
591                 int            msgflg = MSG_DONTWAIT;
592
593                 CDEBUG(D_NET, "page %p + offset %x for %d\n",
594                                page, offset, kiov->kiov_len);
595
596                 if (!cfs_list_empty(&conn->ksnc_tx_queue) ||
597                     fragsize < tx->tx_resid)
598                         msgflg |= MSG_MORE;
599
600                 if (sk->sk_prot->sendpage != NULL) {
601                         rc = sk->sk_prot->sendpage(sk, page,
602                                                    offset, fragsize, msgflg);
603                 } else {
604                         rc = cfs_tcp_sendpage(sk, page, offset, fragsize,
605                                               msgflg);
606                 }
607         } else {
608 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
609                 struct iovec  scratch;
610                 struct iovec *scratchiov = &scratch;
611                 unsigned int  niov = 1;
612 #else
613 #ifdef CONFIG_HIGHMEM
614 #warning "XXX risk of kmap deadlock on multiple frags..."
615 #endif
616                 struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
617                 unsigned int  niov = tx->tx_nkiov;
618 #endif
619                 struct msghdr msg = {
620                         .msg_name       = NULL,
621                         .msg_namelen    = 0,
622                         .msg_iov        = scratchiov,
623                         .msg_iovlen     = niov,
624                         .msg_control    = NULL,
625                         .msg_controllen = 0,
626                         .msg_flags      = MSG_DONTWAIT
627                 };
628                 mm_segment_t  oldmm = get_fs();
629                 int           i;
630
631                 for (nob = i = 0; i < niov; i++) {
632                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
633                                                  kiov[i].kiov_offset;
634                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
635                 }
636
637                 if (!cfs_list_empty(&conn->ksnc_tx_queue) ||
638                     nob < tx->tx_resid)
639                         msg.msg_flags |= MSG_MORE;
640
641                 set_fs (KERNEL_DS);
642                 rc = sock_sendmsg(sock, &msg, nob);
643                 set_fs (oldmm);
644
645                 for (i = 0; i < niov; i++)
646                         kunmap(kiov[i].kiov_page);
647         }
648         return rc;
649 }
650
651 void
652 ksocknal_lib_eager_ack (ksock_conn_t *conn)
653 {
654         int            opt = 1;
655         mm_segment_t   oldmm = get_fs();
656         struct socket *sock = conn->ksnc_sock;
657
658         /* Remind the socket to ACK eagerly.  If I don't, the socket might
659          * think I'm about to send something it could piggy-back the ACK
660          * on, introducing delay in completing zero-copy sends in my
661          * peer. */
662
663         set_fs(KERNEL_DS);
664         sock->ops->setsockopt (sock, SOL_TCP, TCP_QUICKACK,
665                                (char *)&opt, sizeof (opt));
666         set_fs(oldmm);
667 }
668
669 int
670 ksocknal_lib_recv_iov (ksock_conn_t *conn)
671 {
672 #if SOCKNAL_SINGLE_FRAG_RX
673         struct iovec  scratch;
674         struct iovec *scratchiov = &scratch;
675         unsigned int  niov = 1;
676 #else
677         struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
678         unsigned int  niov = conn->ksnc_rx_niov;
679 #endif
680         struct iovec *iov = conn->ksnc_rx_iov;
681         struct msghdr msg = {
682                 .msg_name       = NULL,
683                 .msg_namelen    = 0,
684                 .msg_iov        = scratchiov,
685                 .msg_iovlen     = niov,
686                 .msg_control    = NULL,
687                 .msg_controllen = 0,
688                 .msg_flags      = 0
689         };
690         mm_segment_t oldmm = get_fs();
691         int          nob;
692         int          i;
693         int          rc;
694         int          fragnob;
695         int          sum;
696         __u32        saved_csum;
697
698         /* NB we can't trust socket ops to either consume our iovs
699          * or leave them alone. */
700         LASSERT (niov > 0);
701
702         for (nob = i = 0; i < niov; i++) {
703                 scratchiov[i] = iov[i];
704                 nob += scratchiov[i].iov_len;
705         }
706         LASSERT (nob <= conn->ksnc_rx_nob_wanted);
707
708         set_fs (KERNEL_DS);
709         rc = sock_recvmsg (conn->ksnc_sock, &msg, nob, MSG_DONTWAIT);
710         /* NB this is just a boolean..........................^ */
711         set_fs (oldmm);
712
713         saved_csum = 0;
714         if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
715                 saved_csum = conn->ksnc_msg.ksm_csum;
716                 conn->ksnc_msg.ksm_csum = 0;
717         }
718
719         if (saved_csum != 0) {
720                 /* accumulate checksum */
721                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
722                         LASSERT (i < niov);
723
724                         fragnob = iov[i].iov_len;
725                         if (fragnob > sum)
726                                 fragnob = sum;
727
728                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
729                                                            iov[i].iov_base, fragnob);
730                 }
731                 conn->ksnc_msg.ksm_csum = saved_csum;
732         }
733
734         return rc;
735 }
736
737 static void
738 ksocknal_lib_kiov_vunmap(void *addr)
739 {
740         if (addr == NULL)
741                 return;
742
743         vunmap(addr);
744 }
745
746 static void *
747 ksocknal_lib_kiov_vmap(lnet_kiov_t *kiov, int niov,
748                        struct iovec *iov, struct page **pages)
749 {
750         void             *addr;
751         int               nob;
752         int               i;
753
754         if (!*ksocknal_tunables.ksnd_zc_recv || pages == NULL)
755                 return NULL;
756
757         LASSERT (niov <= LNET_MAX_IOV);
758
759         if (niov < 2 ||
760             niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
761                 return NULL;
762
763         for (nob = i = 0; i < niov; i++) {
764                 if ((kiov[i].kiov_offset != 0 && i > 0) ||
765                     (kiov[i].kiov_offset + kiov[i].kiov_len != CFS_PAGE_SIZE && i < niov - 1))
766                         return NULL;
767
768                 pages[i] = kiov[i].kiov_page;
769                 nob += kiov[i].kiov_len;
770         }
771
772         addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
773         if (addr == NULL)
774                 return NULL;
775
776         iov->iov_base = addr + kiov[0].kiov_offset;
777         iov->iov_len = nob;
778
779         return addr;
780 }
781
782 int
783 ksocknal_lib_recv_kiov (ksock_conn_t *conn)
784 {
785 #if SOCKNAL_SINGLE_FRAG_RX || !SOCKNAL_RISK_KMAP_DEADLOCK
786         struct iovec   scratch;
787         struct iovec  *scratchiov = &scratch;
788         struct page  **pages      = NULL;
789         unsigned int   niov       = 1;
790 #else
791 #ifdef CONFIG_HIGHMEM
792 #warning "XXX risk of kmap deadlock on multiple frags..."
793 #endif
794         struct iovec  *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
795         struct page  **pages      = conn->ksnc_scheduler->kss_rx_scratch_pgs;
796         unsigned int   niov       = conn->ksnc_rx_nkiov;
797 #endif
798         lnet_kiov_t   *kiov = conn->ksnc_rx_kiov;
799         struct msghdr msg = {
800                 .msg_name       = NULL,
801                 .msg_namelen    = 0,
802                 .msg_iov        = scratchiov,
803                 .msg_control    = NULL,
804                 .msg_controllen = 0,
805                 .msg_flags      = 0
806         };
807         mm_segment_t oldmm = get_fs();
808         int          nob;
809         int          i;
810         int          rc;
811         void        *base;
812         void        *addr;
813         int          sum;
814         int          fragnob;
815
816         /* NB we can't trust socket ops to either consume our iovs
817          * or leave them alone. */
818         if ((addr = ksocknal_lib_kiov_vmap(kiov, niov, scratchiov, pages)) != NULL) {
819                 nob = scratchiov[0].iov_len;
820                 msg.msg_iovlen = 1;
821
822         } else {
823                 for (nob = i = 0; i < niov; i++) {
824                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
825                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
826                                                  kiov[i].kiov_offset;
827                 }
828                 msg.msg_iovlen = niov;
829         }
830
831         LASSERT (nob <= conn->ksnc_rx_nob_wanted);
832
833         set_fs (KERNEL_DS);
834         rc = sock_recvmsg (conn->ksnc_sock, &msg, nob, MSG_DONTWAIT);
835         /* NB this is just a boolean.......................^ */
836         set_fs (oldmm);
837
838         if (conn->ksnc_msg.ksm_csum != 0) {
839                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
840                         LASSERT (i < niov);
841
842                         /* Dang! have to kmap again because I have nowhere to stash the
843                          * mapped address.  But by doing it while the page is still
844                          * mapped, the kernel just bumps the map count and returns me
845                          * the address it stashed. */
846                         base = kmap(kiov[i].kiov_page) + kiov[i].kiov_offset;
847                         fragnob = kiov[i].kiov_len;
848                         if (fragnob > sum)
849                                 fragnob = sum;
850
851                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
852                                                            base, fragnob);
853
854                         kunmap(kiov[i].kiov_page);
855                 }
856         }
857
858         if (addr != NULL) {
859                 ksocknal_lib_kiov_vunmap(addr);
860         } else {
861                 for (i = 0; i < niov; i++)
862                         kunmap(kiov[i].kiov_page);
863         }
864
865         return (rc);
866 }
867
868 void
869 ksocknal_lib_csum_tx(ksock_tx_t *tx)
870 {
871         int          i;
872         __u32        csum;
873         void        *base;
874
875         LASSERT(tx->tx_iov[0].iov_base == (void *)&tx->tx_msg);
876         LASSERT(tx->tx_conn != NULL);
877         LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
878
879         tx->tx_msg.ksm_csum = 0;
880
881         csum = ksocknal_csum(~0, (void *)tx->tx_iov[0].iov_base,
882                              tx->tx_iov[0].iov_len);
883
884         if (tx->tx_kiov != NULL) {
885                 for (i = 0; i < tx->tx_nkiov; i++) {
886                         base = kmap(tx->tx_kiov[i].kiov_page) +
887                                tx->tx_kiov[i].kiov_offset;
888
889                         csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
890
891                         kunmap(tx->tx_kiov[i].kiov_page);
892                 }
893         } else {
894                 for (i = 1; i < tx->tx_niov; i++)
895                         csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
896                                              tx->tx_iov[i].iov_len);
897         }
898
899         if (*ksocknal_tunables.ksnd_inject_csum_error) {
900                 csum++;
901                 *ksocknal_tunables.ksnd_inject_csum_error = 0;
902         }
903
904         tx->tx_msg.ksm_csum = csum;
905 }
906
907 int
908 ksocknal_lib_get_conn_tunables (ksock_conn_t *conn, int *txmem, int *rxmem, int *nagle)
909 {
910         mm_segment_t   oldmm = get_fs ();
911         struct socket *sock = conn->ksnc_sock;
912         int            len;
913         int            rc;
914
915         rc = ksocknal_connsock_addref(conn);
916         if (rc != 0) {
917                 LASSERT (conn->ksnc_closing);
918                 *txmem = *rxmem = *nagle = 0;
919                 return (-ESHUTDOWN);
920         }
921
922         rc = libcfs_sock_getbuf(sock, txmem, rxmem);
923         if (rc == 0) {
924                 len = sizeof(*nagle);
925                 set_fs(KERNEL_DS);
926                 rc = sock->ops->getsockopt(sock, SOL_TCP, TCP_NODELAY,
927                                            (char *)nagle, &len);
928                 set_fs(oldmm);
929         }
930
931         ksocknal_connsock_decref(conn);
932
933         if (rc == 0)
934                 *nagle = !*nagle;
935         else
936                 *txmem = *rxmem = *nagle = 0;
937
938         return (rc);
939 }
940
941 int
942 ksocknal_lib_setup_sock (struct socket *sock)
943 {
944         mm_segment_t    oldmm = get_fs ();
945         int             rc;
946         int             option;
947         int             keep_idle;
948         int             keep_intvl;
949         int             keep_count;
950         int             do_keepalive;
951         struct linger   linger;
952
953         sock->sk->sk_allocation = GFP_NOFS;
954
955         /* Ensure this socket aborts active sends immediately when we close
956          * it. */
957
958         linger.l_onoff = 0;
959         linger.l_linger = 0;
960
961         set_fs (KERNEL_DS);
962         rc = sock_setsockopt (sock, SOL_SOCKET, SO_LINGER,
963                               (char *)&linger, sizeof (linger));
964         set_fs (oldmm);
965         if (rc != 0) {
966                 CERROR ("Can't set SO_LINGER: %d\n", rc);
967                 return (rc);
968         }
969
970         option = -1;
971         set_fs (KERNEL_DS);
972         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_LINGER2,
973                                     (char *)&option, sizeof (option));
974         set_fs (oldmm);
975         if (rc != 0) {
976                 CERROR ("Can't set SO_LINGER2: %d\n", rc);
977                 return (rc);
978         }
979
980         if (!*ksocknal_tunables.ksnd_nagle) {
981                 option = 1;
982
983                 set_fs (KERNEL_DS);
984                 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_NODELAY,
985                                             (char *)&option, sizeof (option));
986                 set_fs (oldmm);
987                 if (rc != 0) {
988                         CERROR ("Can't disable nagle: %d\n", rc);
989                         return (rc);
990                 }
991         }
992
993         rc = libcfs_sock_setbuf(sock,
994                                 *ksocknal_tunables.ksnd_tx_buffer_size,
995                                 *ksocknal_tunables.ksnd_rx_buffer_size);
996         if (rc != 0) {
997                 CERROR ("Can't set buffer tx %d, rx %d buffers: %d\n",
998                         *ksocknal_tunables.ksnd_tx_buffer_size,
999                         *ksocknal_tunables.ksnd_rx_buffer_size, rc);
1000                 return (rc);
1001         }
1002
1003 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
1004 #ifdef SOCKNAL_BACKOFF
1005         if (*ksocknal_tunables.ksnd_backoff_init > 0) {
1006                 option = *ksocknal_tunables.ksnd_backoff_init;
1007 #ifdef SOCKNAL_BACKOFF_MS
1008                 option *= 1000;
1009 #endif
1010
1011                 set_fs (KERNEL_DS);
1012                 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_BACKOFF_INIT,
1013                                             (char *)&option, sizeof (option));
1014                 set_fs (oldmm);
1015                 if (rc != 0) {
1016                         CERROR ("Can't set initial tcp backoff %d: %d\n",
1017                                 option, rc);
1018                         return (rc);
1019                 }
1020         }
1021
1022         if (*ksocknal_tunables.ksnd_backoff_max > 0) {
1023                 option = *ksocknal_tunables.ksnd_backoff_max;
1024 #ifdef SOCKNAL_BACKOFF_MS
1025                 option *= 1000;
1026 #endif
1027
1028                 set_fs (KERNEL_DS);
1029                 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_BACKOFF_MAX,
1030                                             (char *)&option, sizeof (option));
1031                 set_fs (oldmm);
1032                 if (rc != 0) {
1033                         CERROR ("Can't set maximum tcp backoff %d: %d\n",
1034                                 option, rc);
1035                         return (rc);
1036                 }
1037         }
1038 #endif
1039
1040         /* snapshot tunables */
1041         keep_idle  = *ksocknal_tunables.ksnd_keepalive_idle;
1042         keep_count = *ksocknal_tunables.ksnd_keepalive_count;
1043         keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
1044
1045         do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
1046
1047         option = (do_keepalive ? 1 : 0);
1048         set_fs (KERNEL_DS);
1049         rc = sock_setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE,
1050                               (char *)&option, sizeof (option));
1051         set_fs (oldmm);
1052         if (rc != 0) {
1053                 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
1054                 return (rc);
1055         }
1056
1057         if (!do_keepalive)
1058                 return (0);
1059
1060         set_fs (KERNEL_DS);
1061         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPIDLE,
1062                                     (char *)&keep_idle, sizeof (keep_idle));
1063         set_fs (oldmm);
1064         if (rc != 0) {
1065                 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
1066                 return (rc);
1067         }
1068
1069         set_fs (KERNEL_DS);
1070         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPINTVL,
1071                                     (char *)&keep_intvl, sizeof (keep_intvl));
1072         set_fs (oldmm);
1073         if (rc != 0) {
1074                 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
1075                 return (rc);
1076         }
1077
1078         set_fs (KERNEL_DS);
1079         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPCNT,
1080                                     (char *)&keep_count, sizeof (keep_count));
1081         set_fs (oldmm);
1082         if (rc != 0) {
1083                 CERROR ("Can't set TCP_KEEPCNT: %d\n", rc);
1084                 return (rc);
1085         }
1086
1087         return (0);
1088 }
1089
1090 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
1091 #define sock2tcp_opt(sk) tcp_sk(sk)
1092 #else
1093 struct tcp_opt *sock2tcp_opt(struct sock *sk)
1094 {
1095         struct tcp_sock *s = (struct tcp_sock *)sk;
1096         return &s->tcp;
1097 }
1098 #endif
1099
1100 void
1101 ksocknal_lib_push_conn (ksock_conn_t *conn)
1102 {
1103         struct sock    *sk;
1104 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11))
1105         struct tcp_opt *tp;
1106 #else
1107         struct tcp_sock *tp;
1108 #endif
1109         int             nonagle;
1110         int             val = 1;
1111         int             rc;
1112         mm_segment_t    oldmm;
1113
1114         rc = ksocknal_connsock_addref(conn);
1115         if (rc != 0)                            /* being shut down */
1116                 return;
1117
1118         sk = conn->ksnc_sock->sk;
1119         tp = sock2tcp_opt(sk);
1120
1121         lock_sock (sk);
1122         nonagle = tp->nonagle;
1123         tp->nonagle = 1;
1124         release_sock (sk);
1125
1126         oldmm = get_fs ();
1127         set_fs (KERNEL_DS);
1128
1129         rc = sk->sk_prot->setsockopt (sk, SOL_TCP, TCP_NODELAY,
1130                                       (char *)&val, sizeof (val));
1131         LASSERT (rc == 0);
1132
1133         set_fs (oldmm);
1134
1135         lock_sock (sk);
1136         tp->nonagle = nonagle;
1137         release_sock (sk);
1138
1139         ksocknal_connsock_decref(conn);
1140 }
1141
1142 extern void ksocknal_read_callback (ksock_conn_t *conn);
1143 extern void ksocknal_write_callback (ksock_conn_t *conn);
1144 /*
1145  * socket call back in Linux
1146  */
1147 static void
1148 ksocknal_data_ready (struct sock *sk, int n)
1149 {
1150         ksock_conn_t  *conn;
1151         ENTRY;
1152
1153         /* interleave correctly with closing sockets... */
1154         LASSERT(!in_irq());
1155         cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1156
1157         conn = sk->sk_user_data;
1158         if (conn == NULL) {             /* raced with ksocknal_terminate_conn */
1159                 LASSERT (sk->sk_data_ready != &ksocknal_data_ready);
1160                 sk->sk_data_ready (sk, n);
1161         } else
1162                 ksocknal_read_callback(conn);
1163
1164         cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1165
1166         EXIT;
1167 }
1168
1169 static void
1170 ksocknal_write_space (struct sock *sk)
1171 {
1172         ksock_conn_t  *conn;
1173         int            wspace;
1174         int            min_wpace;
1175
1176         /* interleave correctly with closing sockets... */
1177         LASSERT(!in_irq());
1178         cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1179
1180         conn = sk->sk_user_data;
1181         wspace = SOCKNAL_WSPACE(sk);
1182         min_wpace = SOCKNAL_MIN_WSPACE(sk);
1183
1184         CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
1185                sk, wspace, min_wpace, conn,
1186                (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
1187                                       " ready" : " blocked"),
1188                (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
1189                                       " scheduled" : " idle"),
1190                (conn == NULL) ? "" : (cfs_list_empty (&conn->ksnc_tx_queue) ?
1191                                       " empty" : " queued"));
1192
1193         if (conn == NULL) {             /* raced with ksocknal_terminate_conn */
1194                 LASSERT (sk->sk_write_space != &ksocknal_write_space);
1195                 sk->sk_write_space (sk);
1196
1197                 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1198                 return;
1199         }
1200
1201         if (wspace >= min_wpace) {              /* got enough space */
1202                 ksocknal_write_callback(conn);
1203
1204                 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
1205                  * ENOMEM check in ksocknal_transmit is race-free (think about
1206                  * it). */
1207
1208                 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
1209         }
1210
1211         cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1212 }
1213
1214 void
1215 ksocknal_lib_save_callback(struct socket *sock, ksock_conn_t *conn)
1216 {
1217         conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
1218         conn->ksnc_saved_write_space = sock->sk->sk_write_space;
1219 }
1220
1221 void
1222 ksocknal_lib_set_callback(struct socket *sock,  ksock_conn_t *conn)
1223 {
1224         sock->sk->sk_user_data = conn;
1225         sock->sk->sk_data_ready = ksocknal_data_ready;
1226         sock->sk->sk_write_space = ksocknal_write_space;
1227         return;
1228 }
1229
1230 void
1231 ksocknal_lib_reset_callback(struct socket *sock, ksock_conn_t *conn)
1232 {
1233         /* Remove conn's network callbacks.
1234          * NB I _have_ to restore the callback, rather than storing a noop,
1235          * since the socket could survive past this module being unloaded!! */
1236         sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
1237         sock->sk->sk_write_space = conn->ksnc_saved_write_space;
1238
1239         /* A callback could be in progress already; they hold a read lock
1240          * on ksnd_global_lock (to serialise with me) and NOOP if
1241          * sk_user_data is NULL. */
1242         sock->sk->sk_user_data = NULL;
1243
1244         return ;
1245 }
1246
1247 int
1248 ksocknal_lib_memory_pressure(ksock_conn_t *conn)
1249 {
1250         int            rc = 0;
1251         ksock_sched_t *sched;
1252         
1253         sched = conn->ksnc_scheduler;
1254         cfs_spin_lock_bh (&sched->kss_lock);
1255
1256         if (!SOCK_TEST_NOSPACE(conn->ksnc_sock) &&
1257             !conn->ksnc_tx_ready) {
1258                 /* SOCK_NOSPACE is set when the socket fills
1259                  * and cleared in the write_space callback
1260                  * (which also sets ksnc_tx_ready).  If
1261                  * SOCK_NOSPACE and ksnc_tx_ready are BOTH
1262                  * zero, I didn't fill the socket and
1263                  * write_space won't reschedule me, so I
1264                  * return -ENOMEM to get my caller to retry
1265                  * after a timeout */
1266                 rc = -ENOMEM;
1267         }
1268
1269         cfs_spin_unlock_bh (&sched->kss_lock);
1270
1271         return rc;
1272 }
1273
1274 int
1275 ksocknal_lib_bind_thread_to_cpu(int id)
1276 {
1277 #if defined(CONFIG_SMP) && defined(CPU_AFFINITY)
1278         id = ksocknal_sched2cpu(id);
1279         if (cpu_online(id)) {
1280                 cpumask_t m = CPU_MASK_NONE;
1281                 cpu_set(id, m);
1282                 set_cpus_allowed(current, m);
1283                 return 0;
1284         }
1285
1286         return -1;
1287
1288 #else
1289         return 0;
1290 #endif
1291 }