Whamcloud - gitweb
LU-56 ksocklnd: CPT affinity socklnd
[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 int
408 ksocknal_lib_get_conn_addrs (ksock_conn_t *conn)
409 {
410         int rc = libcfs_sock_getaddr(conn->ksnc_sock, 1,
411                                      &conn->ksnc_ipaddr,
412                                      &conn->ksnc_port);
413
414         /* Didn't need the {get,put}connsock dance to deref ksnc_sock... */
415         LASSERT (!conn->ksnc_closing);
416
417         if (rc != 0) {
418                 CERROR ("Error %d getting sock peer IP\n", rc);
419                 return rc;
420         }
421
422         rc = libcfs_sock_getaddr(conn->ksnc_sock, 0,
423                                  &conn->ksnc_myipaddr, NULL);
424         if (rc != 0) {
425                 CERROR ("Error %d getting sock local IP\n", rc);
426                 return rc;
427         }
428
429         return 0;
430 }
431
432 int
433 ksocknal_lib_zc_capable(ksock_conn_t *conn)
434 {
435         int  caps = conn->ksnc_sock->sk->sk_route_caps;
436
437         if (conn->ksnc_proto == &ksocknal_protocol_v1x)
438                 return 0;
439
440         /* ZC if the socket supports scatter/gather and doesn't need software
441          * checksums */
442         return ((caps & NETIF_F_SG) != 0 &&
443                 (caps & (NETIF_F_IP_CSUM | NETIF_F_NO_CSUM | NETIF_F_HW_CSUM)) != 0);
444 }
445
446 int
447 ksocknal_lib_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
448 {
449         struct socket *sock = conn->ksnc_sock;
450         int            nob;
451         int            rc;
452
453         if (*ksocknal_tunables.ksnd_enable_csum        && /* checksum enabled */
454             conn->ksnc_proto == &ksocknal_protocol_v2x && /* V2.x connection  */
455             tx->tx_nob == tx->tx_resid                 && /* frist sending    */
456             tx->tx_msg.ksm_csum == 0)                     /* not checksummed  */
457                 ksocknal_lib_csum_tx(tx);
458
459         /* NB we can't trust socket ops to either consume our iovs
460          * or leave them alone. */
461
462         {
463 #if SOCKNAL_SINGLE_FRAG_TX
464                 struct iovec    scratch;
465                 struct iovec   *scratchiov = &scratch;
466                 unsigned int    niov = 1;
467 #else
468                 struct iovec   *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
469                 unsigned int    niov = tx->tx_niov;
470 #endif
471                 struct msghdr msg = {
472                         .msg_name       = NULL,
473                         .msg_namelen    = 0,
474                         .msg_iov        = scratchiov,
475                         .msg_iovlen     = niov,
476                         .msg_control    = NULL,
477                         .msg_controllen = 0,
478                         .msg_flags      = MSG_DONTWAIT
479                 };
480                 mm_segment_t oldmm = get_fs();
481                 int  i;
482
483                 for (nob = i = 0; i < niov; i++) {
484                         scratchiov[i] = tx->tx_iov[i];
485                         nob += scratchiov[i].iov_len;
486                 }
487
488                 if (!cfs_list_empty(&conn->ksnc_tx_queue) ||
489                     nob < tx->tx_resid)
490                         msg.msg_flags |= MSG_MORE;
491
492                 set_fs (KERNEL_DS);
493                 rc = sock_sendmsg(sock, &msg, nob);
494                 set_fs (oldmm);
495         }
496         return rc;
497 }
498
499 int
500 ksocknal_lib_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
501 {
502         struct socket *sock = conn->ksnc_sock;
503         lnet_kiov_t   *kiov = tx->tx_kiov;
504         int            rc;
505         int            nob;
506
507         /* Not NOOP message */
508         LASSERT (tx->tx_lnetmsg != NULL);
509
510         /* NB we can't trust socket ops to either consume our iovs
511          * or leave them alone. */
512         if (tx->tx_msg.ksm_zc_cookies[0] != 0) {
513                 /* Zero copy is enabled */
514                 struct sock   *sk = sock->sk;
515                 struct page   *page = kiov->kiov_page;
516                 int            offset = kiov->kiov_offset;
517                 int            fragsize = kiov->kiov_len;
518                 int            msgflg = MSG_DONTWAIT;
519
520                 CDEBUG(D_NET, "page %p + offset %x for %d\n",
521                                page, offset, kiov->kiov_len);
522
523                 if (!cfs_list_empty(&conn->ksnc_tx_queue) ||
524                     fragsize < tx->tx_resid)
525                         msgflg |= MSG_MORE;
526
527                 if (sk->sk_prot->sendpage != NULL) {
528                         rc = sk->sk_prot->sendpage(sk, page,
529                                                    offset, fragsize, msgflg);
530                 } else {
531                         rc = cfs_tcp_sendpage(sk, page, offset, fragsize,
532                                               msgflg);
533                 }
534         } else {
535 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
536                 struct iovec  scratch;
537                 struct iovec *scratchiov = &scratch;
538                 unsigned int  niov = 1;
539 #else
540 #ifdef CONFIG_HIGHMEM
541 #warning "XXX risk of kmap deadlock on multiple frags..."
542 #endif
543                 struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
544                 unsigned int  niov = tx->tx_nkiov;
545 #endif
546                 struct msghdr msg = {
547                         .msg_name       = NULL,
548                         .msg_namelen    = 0,
549                         .msg_iov        = scratchiov,
550                         .msg_iovlen     = niov,
551                         .msg_control    = NULL,
552                         .msg_controllen = 0,
553                         .msg_flags      = MSG_DONTWAIT
554                 };
555                 mm_segment_t  oldmm = get_fs();
556                 int           i;
557
558                 for (nob = i = 0; i < niov; i++) {
559                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
560                                                  kiov[i].kiov_offset;
561                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
562                 }
563
564                 if (!cfs_list_empty(&conn->ksnc_tx_queue) ||
565                     nob < tx->tx_resid)
566                         msg.msg_flags |= MSG_MORE;
567
568                 set_fs (KERNEL_DS);
569                 rc = sock_sendmsg(sock, &msg, nob);
570                 set_fs (oldmm);
571
572                 for (i = 0; i < niov; i++)
573                         kunmap(kiov[i].kiov_page);
574         }
575         return rc;
576 }
577
578 void
579 ksocknal_lib_eager_ack (ksock_conn_t *conn)
580 {
581         int            opt = 1;
582         mm_segment_t   oldmm = get_fs();
583         struct socket *sock = conn->ksnc_sock;
584
585         /* Remind the socket to ACK eagerly.  If I don't, the socket might
586          * think I'm about to send something it could piggy-back the ACK
587          * on, introducing delay in completing zero-copy sends in my
588          * peer. */
589
590         set_fs(KERNEL_DS);
591         sock->ops->setsockopt (sock, SOL_TCP, TCP_QUICKACK,
592                                (char *)&opt, sizeof (opt));
593         set_fs(oldmm);
594 }
595
596 int
597 ksocknal_lib_recv_iov (ksock_conn_t *conn)
598 {
599 #if SOCKNAL_SINGLE_FRAG_RX
600         struct iovec  scratch;
601         struct iovec *scratchiov = &scratch;
602         unsigned int  niov = 1;
603 #else
604         struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
605         unsigned int  niov = conn->ksnc_rx_niov;
606 #endif
607         struct iovec *iov = conn->ksnc_rx_iov;
608         struct msghdr msg = {
609                 .msg_name       = NULL,
610                 .msg_namelen    = 0,
611                 .msg_iov        = scratchiov,
612                 .msg_iovlen     = niov,
613                 .msg_control    = NULL,
614                 .msg_controllen = 0,
615                 .msg_flags      = 0
616         };
617         mm_segment_t oldmm = get_fs();
618         int          nob;
619         int          i;
620         int          rc;
621         int          fragnob;
622         int          sum;
623         __u32        saved_csum;
624
625         /* NB we can't trust socket ops to either consume our iovs
626          * or leave them alone. */
627         LASSERT (niov > 0);
628
629         for (nob = i = 0; i < niov; i++) {
630                 scratchiov[i] = iov[i];
631                 nob += scratchiov[i].iov_len;
632         }
633         LASSERT (nob <= conn->ksnc_rx_nob_wanted);
634
635         set_fs (KERNEL_DS);
636         rc = sock_recvmsg (conn->ksnc_sock, &msg, nob, MSG_DONTWAIT);
637         /* NB this is just a boolean..........................^ */
638         set_fs (oldmm);
639
640         saved_csum = 0;
641         if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
642                 saved_csum = conn->ksnc_msg.ksm_csum;
643                 conn->ksnc_msg.ksm_csum = 0;
644         }
645
646         if (saved_csum != 0) {
647                 /* accumulate checksum */
648                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
649                         LASSERT (i < niov);
650
651                         fragnob = iov[i].iov_len;
652                         if (fragnob > sum)
653                                 fragnob = sum;
654
655                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
656                                                            iov[i].iov_base, fragnob);
657                 }
658                 conn->ksnc_msg.ksm_csum = saved_csum;
659         }
660
661         return rc;
662 }
663
664 static void
665 ksocknal_lib_kiov_vunmap(void *addr)
666 {
667         if (addr == NULL)
668                 return;
669
670         vunmap(addr);
671 }
672
673 static void *
674 ksocknal_lib_kiov_vmap(lnet_kiov_t *kiov, int niov,
675                        struct iovec *iov, struct page **pages)
676 {
677         void             *addr;
678         int               nob;
679         int               i;
680
681         if (!*ksocknal_tunables.ksnd_zc_recv || pages == NULL)
682                 return NULL;
683
684         LASSERT (niov <= LNET_MAX_IOV);
685
686         if (niov < 2 ||
687             niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
688                 return NULL;
689
690         for (nob = i = 0; i < niov; i++) {
691                 if ((kiov[i].kiov_offset != 0 && i > 0) ||
692                     (kiov[i].kiov_offset + kiov[i].kiov_len != CFS_PAGE_SIZE && i < niov - 1))
693                         return NULL;
694
695                 pages[i] = kiov[i].kiov_page;
696                 nob += kiov[i].kiov_len;
697         }
698
699         addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
700         if (addr == NULL)
701                 return NULL;
702
703         iov->iov_base = addr + kiov[0].kiov_offset;
704         iov->iov_len = nob;
705
706         return addr;
707 }
708
709 int
710 ksocknal_lib_recv_kiov (ksock_conn_t *conn)
711 {
712 #if SOCKNAL_SINGLE_FRAG_RX || !SOCKNAL_RISK_KMAP_DEADLOCK
713         struct iovec   scratch;
714         struct iovec  *scratchiov = &scratch;
715         struct page  **pages      = NULL;
716         unsigned int   niov       = 1;
717 #else
718 #ifdef CONFIG_HIGHMEM
719 #warning "XXX risk of kmap deadlock on multiple frags..."
720 #endif
721         struct iovec  *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
722         struct page  **pages      = conn->ksnc_scheduler->kss_rx_scratch_pgs;
723         unsigned int   niov       = conn->ksnc_rx_nkiov;
724 #endif
725         lnet_kiov_t   *kiov = conn->ksnc_rx_kiov;
726         struct msghdr msg = {
727                 .msg_name       = NULL,
728                 .msg_namelen    = 0,
729                 .msg_iov        = scratchiov,
730                 .msg_control    = NULL,
731                 .msg_controllen = 0,
732                 .msg_flags      = 0
733         };
734         mm_segment_t oldmm = get_fs();
735         int          nob;
736         int          i;
737         int          rc;
738         void        *base;
739         void        *addr;
740         int          sum;
741         int          fragnob;
742
743         /* NB we can't trust socket ops to either consume our iovs
744          * or leave them alone. */
745         if ((addr = ksocknal_lib_kiov_vmap(kiov, niov, scratchiov, pages)) != NULL) {
746                 nob = scratchiov[0].iov_len;
747                 msg.msg_iovlen = 1;
748
749         } else {
750                 for (nob = i = 0; i < niov; i++) {
751                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
752                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
753                                                  kiov[i].kiov_offset;
754                 }
755                 msg.msg_iovlen = niov;
756         }
757
758         LASSERT (nob <= conn->ksnc_rx_nob_wanted);
759
760         set_fs (KERNEL_DS);
761         rc = sock_recvmsg (conn->ksnc_sock, &msg, nob, MSG_DONTWAIT);
762         /* NB this is just a boolean.......................^ */
763         set_fs (oldmm);
764
765         if (conn->ksnc_msg.ksm_csum != 0) {
766                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
767                         LASSERT (i < niov);
768
769                         /* Dang! have to kmap again because I have nowhere to stash the
770                          * mapped address.  But by doing it while the page is still
771                          * mapped, the kernel just bumps the map count and returns me
772                          * the address it stashed. */
773                         base = kmap(kiov[i].kiov_page) + kiov[i].kiov_offset;
774                         fragnob = kiov[i].kiov_len;
775                         if (fragnob > sum)
776                                 fragnob = sum;
777
778                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
779                                                            base, fragnob);
780
781                         kunmap(kiov[i].kiov_page);
782                 }
783         }
784
785         if (addr != NULL) {
786                 ksocknal_lib_kiov_vunmap(addr);
787         } else {
788                 for (i = 0; i < niov; i++)
789                         kunmap(kiov[i].kiov_page);
790         }
791
792         return (rc);
793 }
794
795 void
796 ksocknal_lib_csum_tx(ksock_tx_t *tx)
797 {
798         int          i;
799         __u32        csum;
800         void        *base;
801
802         LASSERT(tx->tx_iov[0].iov_base == (void *)&tx->tx_msg);
803         LASSERT(tx->tx_conn != NULL);
804         LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
805
806         tx->tx_msg.ksm_csum = 0;
807
808         csum = ksocknal_csum(~0, (void *)tx->tx_iov[0].iov_base,
809                              tx->tx_iov[0].iov_len);
810
811         if (tx->tx_kiov != NULL) {
812                 for (i = 0; i < tx->tx_nkiov; i++) {
813                         base = kmap(tx->tx_kiov[i].kiov_page) +
814                                tx->tx_kiov[i].kiov_offset;
815
816                         csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
817
818                         kunmap(tx->tx_kiov[i].kiov_page);
819                 }
820         } else {
821                 for (i = 1; i < tx->tx_niov; i++)
822                         csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
823                                              tx->tx_iov[i].iov_len);
824         }
825
826         if (*ksocknal_tunables.ksnd_inject_csum_error) {
827                 csum++;
828                 *ksocknal_tunables.ksnd_inject_csum_error = 0;
829         }
830
831         tx->tx_msg.ksm_csum = csum;
832 }
833
834 int
835 ksocknal_lib_get_conn_tunables (ksock_conn_t *conn, int *txmem, int *rxmem, int *nagle)
836 {
837         mm_segment_t   oldmm = get_fs ();
838         struct socket *sock = conn->ksnc_sock;
839         int            len;
840         int            rc;
841
842         rc = ksocknal_connsock_addref(conn);
843         if (rc != 0) {
844                 LASSERT (conn->ksnc_closing);
845                 *txmem = *rxmem = *nagle = 0;
846                 return (-ESHUTDOWN);
847         }
848
849         rc = libcfs_sock_getbuf(sock, txmem, rxmem);
850         if (rc == 0) {
851                 len = sizeof(*nagle);
852                 set_fs(KERNEL_DS);
853                 rc = sock->ops->getsockopt(sock, SOL_TCP, TCP_NODELAY,
854                                            (char *)nagle, &len);
855                 set_fs(oldmm);
856         }
857
858         ksocknal_connsock_decref(conn);
859
860         if (rc == 0)
861                 *nagle = !*nagle;
862         else
863                 *txmem = *rxmem = *nagle = 0;
864
865         return (rc);
866 }
867
868 int
869 ksocknal_lib_setup_sock (struct socket *sock)
870 {
871         mm_segment_t    oldmm = get_fs ();
872         int             rc;
873         int             option;
874         int             keep_idle;
875         int             keep_intvl;
876         int             keep_count;
877         int             do_keepalive;
878         struct linger   linger;
879
880         sock->sk->sk_allocation = GFP_NOFS;
881
882         /* Ensure this socket aborts active sends immediately when we close
883          * it. */
884
885         linger.l_onoff = 0;
886         linger.l_linger = 0;
887
888         set_fs (KERNEL_DS);
889         rc = sock_setsockopt (sock, SOL_SOCKET, SO_LINGER,
890                               (char *)&linger, sizeof (linger));
891         set_fs (oldmm);
892         if (rc != 0) {
893                 CERROR ("Can't set SO_LINGER: %d\n", rc);
894                 return (rc);
895         }
896
897         option = -1;
898         set_fs (KERNEL_DS);
899         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_LINGER2,
900                                     (char *)&option, sizeof (option));
901         set_fs (oldmm);
902         if (rc != 0) {
903                 CERROR ("Can't set SO_LINGER2: %d\n", rc);
904                 return (rc);
905         }
906
907         if (!*ksocknal_tunables.ksnd_nagle) {
908                 option = 1;
909
910                 set_fs (KERNEL_DS);
911                 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_NODELAY,
912                                             (char *)&option, sizeof (option));
913                 set_fs (oldmm);
914                 if (rc != 0) {
915                         CERROR ("Can't disable nagle: %d\n", rc);
916                         return (rc);
917                 }
918         }
919
920         rc = libcfs_sock_setbuf(sock,
921                                 *ksocknal_tunables.ksnd_tx_buffer_size,
922                                 *ksocknal_tunables.ksnd_rx_buffer_size);
923         if (rc != 0) {
924                 CERROR ("Can't set buffer tx %d, rx %d buffers: %d\n",
925                         *ksocknal_tunables.ksnd_tx_buffer_size,
926                         *ksocknal_tunables.ksnd_rx_buffer_size, rc);
927                 return (rc);
928         }
929
930 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
931 #ifdef SOCKNAL_BACKOFF
932         if (*ksocknal_tunables.ksnd_backoff_init > 0) {
933                 option = *ksocknal_tunables.ksnd_backoff_init;
934 #ifdef SOCKNAL_BACKOFF_MS
935                 option *= 1000;
936 #endif
937
938                 set_fs (KERNEL_DS);
939                 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_BACKOFF_INIT,
940                                             (char *)&option, sizeof (option));
941                 set_fs (oldmm);
942                 if (rc != 0) {
943                         CERROR ("Can't set initial tcp backoff %d: %d\n",
944                                 option, rc);
945                         return (rc);
946                 }
947         }
948
949         if (*ksocknal_tunables.ksnd_backoff_max > 0) {
950                 option = *ksocknal_tunables.ksnd_backoff_max;
951 #ifdef SOCKNAL_BACKOFF_MS
952                 option *= 1000;
953 #endif
954
955                 set_fs (KERNEL_DS);
956                 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_BACKOFF_MAX,
957                                             (char *)&option, sizeof (option));
958                 set_fs (oldmm);
959                 if (rc != 0) {
960                         CERROR ("Can't set maximum tcp backoff %d: %d\n",
961                                 option, rc);
962                         return (rc);
963                 }
964         }
965 #endif
966
967         /* snapshot tunables */
968         keep_idle  = *ksocknal_tunables.ksnd_keepalive_idle;
969         keep_count = *ksocknal_tunables.ksnd_keepalive_count;
970         keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
971
972         do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
973
974         option = (do_keepalive ? 1 : 0);
975         set_fs (KERNEL_DS);
976         rc = sock_setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE,
977                               (char *)&option, sizeof (option));
978         set_fs (oldmm);
979         if (rc != 0) {
980                 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
981                 return (rc);
982         }
983
984         if (!do_keepalive)
985                 return (0);
986
987         set_fs (KERNEL_DS);
988         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPIDLE,
989                                     (char *)&keep_idle, sizeof (keep_idle));
990         set_fs (oldmm);
991         if (rc != 0) {
992                 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
993                 return (rc);
994         }
995
996         set_fs (KERNEL_DS);
997         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPINTVL,
998                                     (char *)&keep_intvl, sizeof (keep_intvl));
999         set_fs (oldmm);
1000         if (rc != 0) {
1001                 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
1002                 return (rc);
1003         }
1004
1005         set_fs (KERNEL_DS);
1006         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPCNT,
1007                                     (char *)&keep_count, sizeof (keep_count));
1008         set_fs (oldmm);
1009         if (rc != 0) {
1010                 CERROR ("Can't set TCP_KEEPCNT: %d\n", rc);
1011                 return (rc);
1012         }
1013
1014         return (0);
1015 }
1016
1017 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
1018 #define sock2tcp_opt(sk) tcp_sk(sk)
1019 #else
1020 struct tcp_opt *sock2tcp_opt(struct sock *sk)
1021 {
1022         struct tcp_sock *s = (struct tcp_sock *)sk;
1023         return &s->tcp;
1024 }
1025 #endif
1026
1027 void
1028 ksocknal_lib_push_conn (ksock_conn_t *conn)
1029 {
1030         struct sock    *sk;
1031 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11))
1032         struct tcp_opt *tp;
1033 #else
1034         struct tcp_sock *tp;
1035 #endif
1036         int             nonagle;
1037         int             val = 1;
1038         int             rc;
1039         mm_segment_t    oldmm;
1040
1041         rc = ksocknal_connsock_addref(conn);
1042         if (rc != 0)                            /* being shut down */
1043                 return;
1044
1045         sk = conn->ksnc_sock->sk;
1046         tp = sock2tcp_opt(sk);
1047
1048         lock_sock (sk);
1049         nonagle = tp->nonagle;
1050         tp->nonagle = 1;
1051         release_sock (sk);
1052
1053         oldmm = get_fs ();
1054         set_fs (KERNEL_DS);
1055
1056         rc = sk->sk_prot->setsockopt (sk, SOL_TCP, TCP_NODELAY,
1057                                       (char *)&val, sizeof (val));
1058         LASSERT (rc == 0);
1059
1060         set_fs (oldmm);
1061
1062         lock_sock (sk);
1063         tp->nonagle = nonagle;
1064         release_sock (sk);
1065
1066         ksocknal_connsock_decref(conn);
1067 }
1068
1069 extern void ksocknal_read_callback (ksock_conn_t *conn);
1070 extern void ksocknal_write_callback (ksock_conn_t *conn);
1071 /*
1072  * socket call back in Linux
1073  */
1074 static void
1075 ksocknal_data_ready (struct sock *sk, int n)
1076 {
1077         ksock_conn_t  *conn;
1078         ENTRY;
1079
1080         /* interleave correctly with closing sockets... */
1081         LASSERT(!in_irq());
1082         cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1083
1084         conn = sk->sk_user_data;
1085         if (conn == NULL) {             /* raced with ksocknal_terminate_conn */
1086                 LASSERT (sk->sk_data_ready != &ksocknal_data_ready);
1087                 sk->sk_data_ready (sk, n);
1088         } else
1089                 ksocknal_read_callback(conn);
1090
1091         cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1092
1093         EXIT;
1094 }
1095
1096 static void
1097 ksocknal_write_space (struct sock *sk)
1098 {
1099         ksock_conn_t  *conn;
1100         int            wspace;
1101         int            min_wpace;
1102
1103         /* interleave correctly with closing sockets... */
1104         LASSERT(!in_irq());
1105         cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1106
1107         conn = sk->sk_user_data;
1108         wspace = SOCKNAL_WSPACE(sk);
1109         min_wpace = SOCKNAL_MIN_WSPACE(sk);
1110
1111         CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
1112                sk, wspace, min_wpace, conn,
1113                (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
1114                                       " ready" : " blocked"),
1115                (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
1116                                       " scheduled" : " idle"),
1117                (conn == NULL) ? "" : (cfs_list_empty (&conn->ksnc_tx_queue) ?
1118                                       " empty" : " queued"));
1119
1120         if (conn == NULL) {             /* raced with ksocknal_terminate_conn */
1121                 LASSERT (sk->sk_write_space != &ksocknal_write_space);
1122                 sk->sk_write_space (sk);
1123
1124                 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1125                 return;
1126         }
1127
1128         if (wspace >= min_wpace) {              /* got enough space */
1129                 ksocknal_write_callback(conn);
1130
1131                 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
1132                  * ENOMEM check in ksocknal_transmit is race-free (think about
1133                  * it). */
1134
1135                 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
1136         }
1137
1138         cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1139 }
1140
1141 void
1142 ksocknal_lib_save_callback(struct socket *sock, ksock_conn_t *conn)
1143 {
1144         conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
1145         conn->ksnc_saved_write_space = sock->sk->sk_write_space;
1146 }
1147
1148 void
1149 ksocknal_lib_set_callback(struct socket *sock,  ksock_conn_t *conn)
1150 {
1151         sock->sk->sk_user_data = conn;
1152         sock->sk->sk_data_ready = ksocknal_data_ready;
1153         sock->sk->sk_write_space = ksocknal_write_space;
1154         return;
1155 }
1156
1157 void
1158 ksocknal_lib_reset_callback(struct socket *sock, ksock_conn_t *conn)
1159 {
1160         /* Remove conn's network callbacks.
1161          * NB I _have_ to restore the callback, rather than storing a noop,
1162          * since the socket could survive past this module being unloaded!! */
1163         sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
1164         sock->sk->sk_write_space = conn->ksnc_saved_write_space;
1165
1166         /* A callback could be in progress already; they hold a read lock
1167          * on ksnd_global_lock (to serialise with me) and NOOP if
1168          * sk_user_data is NULL. */
1169         sock->sk->sk_user_data = NULL;
1170
1171         return ;
1172 }
1173
1174 int
1175 ksocknal_lib_memory_pressure(ksock_conn_t *conn)
1176 {
1177         int            rc = 0;
1178         ksock_sched_t *sched;
1179         
1180         sched = conn->ksnc_scheduler;
1181         cfs_spin_lock_bh (&sched->kss_lock);
1182
1183         if (!SOCK_TEST_NOSPACE(conn->ksnc_sock) &&
1184             !conn->ksnc_tx_ready) {
1185                 /* SOCK_NOSPACE is set when the socket fills
1186                  * and cleared in the write_space callback
1187                  * (which also sets ksnc_tx_ready).  If
1188                  * SOCK_NOSPACE and ksnc_tx_ready are BOTH
1189                  * zero, I didn't fill the socket and
1190                  * write_space won't reschedule me, so I
1191                  * return -ENOMEM to get my caller to retry
1192                  * after a timeout */
1193                 rc = -ENOMEM;
1194         }
1195
1196         cfs_spin_unlock_bh (&sched->kss_lock);
1197
1198         return rc;
1199 }