4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
39 # if defined(CONFIG_SYSCTL) && !CFS_SYSFS_MODULE_PARM
41 #ifndef HAVE_SYSCTL_UNNUMBERED
46 SOCKLND_PEER_TXCREDITS,
47 SOCKLND_PEER_RTRCREDITS,
50 SOCKLND_RECONNECTS_MIN,
51 SOCKLND_RECONNECTS_MAX,
56 SOCKLND_RX_BUFFER_SIZE,
57 SOCKLND_TX_BUFFER_SIZE,
62 SOCKLND_KEEPALIVE_IDLE,
63 SOCKLND_KEEPALIVE_COUNT,
64 SOCKLND_KEEPALIVE_INTVL,
68 SOCKLND_ZERO_COPY_RECV,
69 SOCKLND_ZERO_COPY_RECV_MIN_NFRAGS
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
101 static cfs_sysctl_table_t ksocknal_ctl_table[] = {
103 .ctl_name = SOCKLND_TIMEOUT,
104 .procname = "timeout",
105 .data = &ksocknal_tunables.ksnd_timeout,
106 .maxlen = sizeof (int),
108 .proc_handler = &proc_dointvec,
109 .strategy = &sysctl_intvec,
112 .ctl_name = SOCKLND_CREDITS,
113 .procname = "credits",
114 .data = &ksocknal_tunables.ksnd_credits,
115 .maxlen = sizeof (int),
117 .proc_handler = &proc_dointvec,
118 .strategy = &sysctl_intvec,
121 .ctl_name = SOCKLND_PEER_TXCREDITS,
122 .procname = "peer_credits",
123 .data = &ksocknal_tunables.ksnd_peertxcredits,
124 .maxlen = sizeof (int),
126 .proc_handler = &proc_dointvec,
127 .strategy = &sysctl_intvec,
130 .ctl_name = SOCKLND_PEER_RTRCREDITS,
131 .procname = "peer_buffer_credits",
132 .data = &ksocknal_tunables.ksnd_peerrtrcredits,
133 .maxlen = sizeof (int),
135 .proc_handler = &proc_dointvec,
136 .strategy = &sysctl_intvec,
139 .ctl_name = SOCKLND_PEER_TIMEOUT,
140 .procname = "peer_timeout",
141 .data = &ksocknal_tunables.ksnd_peertimeout,
142 .maxlen = sizeof (int),
144 .proc_handler = &proc_dointvec
145 .strategy = &sysctl_intvec,
148 .ctl_name = SOCKLND_NCONNDS,
149 .procname = "nconnds",
150 .data = &ksocknal_tunables.ksnd_nconnds,
151 .maxlen = sizeof (int),
153 .proc_handler = &proc_dointvec,
154 .strategy = &sysctl_intvec,
157 .ctl_name = SOCKLND_RECONNECTS_MIN,
158 .procname = "min_reconnectms",
159 .data = &ksocknal_tunables.ksnd_min_reconnectms,
160 .maxlen = sizeof (int),
162 .proc_handler = &proc_dointvec,
163 .strategy = &sysctl_intvec,
166 .ctl_name = SOCKLND_RECONNECTS_MAX,
167 .procname = "max_reconnectms",
168 .data = &ksocknal_tunables.ksnd_max_reconnectms,
169 .maxlen = sizeof (int),
171 .proc_handler = &proc_dointvec,
172 .strategy = &sysctl_intvec,
175 .ctl_name = SOCKLND_EAGER_ACK,
176 .procname = "eager_ack",
177 .data = &ksocknal_tunables.ksnd_eager_ack,
178 .maxlen = sizeof (int),
180 .proc_handler = &proc_dointvec,
181 .strategy = &sysctl_intvec,
184 .ctl_name = SOCKLND_ZERO_COPY,
185 .procname = "zero_copy",
186 .data = &ksocknal_tunables.ksnd_zc_min_payload,
187 .maxlen = sizeof (int),
189 .proc_handler = &proc_dointvec,
190 .strategy = &sysctl_intvec,
193 .ctl_name = SOCKLND_ZERO_COPY_RECV,
194 .procname = "zero_copy_recv",
195 .data = &ksocknal_tunables.ksnd_zc_recv,
196 .maxlen = sizeof (int),
198 .proc_handler = &proc_dointvec,
199 .strategy = &sysctl_intvec,
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),
208 .proc_handler = &proc_dointvec,
209 .strategy = &sysctl_intvec,
212 .ctl_name = SOCKLND_TYPED,
214 .data = &ksocknal_tunables.ksnd_typed_conns,
215 .maxlen = sizeof (int),
217 .proc_handler = &proc_dointvec,
218 .strategy = &sysctl_intvec,
221 .ctl_name = SOCKLND_BULK_MIN,
222 .procname = "min_bulk",
223 .data = &ksocknal_tunables.ksnd_min_bulk,
224 .maxlen = sizeof (int),
226 .proc_handler = &proc_dointvec,
227 .strategy = &sysctl_intvec,
230 .ctl_name = SOCKLND_RX_BUFFER_SIZE,
231 .procname = "rx_buffer_size",
232 .data = &ksocknal_tunables.ksnd_rx_buffer_size,
233 .maxlen = sizeof(int),
235 .proc_handler = &proc_dointvec,
236 .strategy = &sysctl_intvec,
239 .ctl_name = SOCKLND_TX_BUFFER_SIZE,
240 .procname = "tx_buffer_size",
241 .data = &ksocknal_tunables.ksnd_tx_buffer_size,
242 .maxlen = sizeof(int),
244 .proc_handler = &proc_dointvec,
245 .strategy = &sysctl_intvec,
248 .ctl_name = SOCKLND_NAGLE,
250 .data = &ksocknal_tunables.ksnd_nagle,
251 .maxlen = sizeof(int),
253 .proc_handler = &proc_dointvec,
254 .strategy = &sysctl_intvec,
258 .ctl_name = SOCKLND_IRQ_AFFINITY,
259 .procname = "irq_affinity",
260 .data = &ksocknal_tunables.ksnd_irq_affinity,
261 .maxlen = sizeof(int),
263 .proc_handler = &proc_dointvec,
264 .strategy = &sysctl_intvec,
268 .ctl_name = SOCKLND_ROUND_ROBIN,
269 .procname = "round_robin",
270 .data = &ksocknal_tunables.ksnd_round_robin,
271 .maxlen = sizeof(int),
273 .proc_handler = &proc_dointvec,
274 .strategy = &sysctl_intvec,
277 .ctl_name = SOCKLND_KEEPALIVE,
278 .procname = "keepalive",
279 .data = &ksocknal_tunables.ksnd_keepalive,
280 .maxlen = sizeof(int),
282 .proc_handler = &proc_dointvec,
283 .strategy = &sysctl_intvec,
286 .ctl_name = SOCKLND_KEEPALIVE_IDLE,
287 .procname = "keepalive_idle",
288 .data = &ksocknal_tunables.ksnd_keepalive_idle,
289 .maxlen = sizeof(int),
291 .proc_handler = &proc_dointvec,
292 .strategy = &sysctl_intvec,
295 .ctl_name = SOCKLND_KEEPALIVE_COUNT,
296 .procname = "keepalive_count",
297 .data = &ksocknal_tunables.ksnd_keepalive_count,
298 .maxlen = sizeof(int),
300 .proc_handler = &proc_dointvec,
301 .strategy = &sysctl_intvec,
304 .ctl_name = SOCKLND_KEEPALIVE_INTVL,
305 .procname = "keepalive_intvl",
306 .data = &ksocknal_tunables.ksnd_keepalive_intvl,
307 .maxlen = sizeof(int),
309 .proc_handler = &proc_dointvec,
310 .strategy = &sysctl_intvec,
312 #ifdef SOCKNAL_BACKOFF
314 .ctl_name = SOCKLND_BACKOFF_INIT,
315 .procname = "backoff_init",
316 .data = &ksocknal_tunables.ksnd_backoff_init,
317 .maxlen = sizeof(int),
319 .proc_handler = &proc_dointvec,
320 .strategy = &sysctl_intvec,
323 .ctl_name = SOCKLND_BACKOFF_MAX,
324 .procname = "backoff_max",
325 .data = &ksocknal_tunables.ksnd_backoff_max,
326 .maxlen = sizeof(int),
328 .proc_handler = &proc_dointvec,
329 .strategy = &sysctl_intvec,
332 #if SOCKNAL_VERSION_DEBUG
334 .ctl_name = SOCKLND_PROTOCOL,
335 .procname = "protocol",
336 .data = &ksocknal_tunables.ksnd_protocol,
337 .maxlen = sizeof(int),
339 .proc_handler = &proc_dointvec,
340 .strategy = &sysctl_intvec,
347 cfs_sysctl_table_t ksocknal_top_ctl_table[] = {
349 .ctl_name = CTL_SOCKLND,
350 .procname = "socknal",
354 .child = ksocknal_ctl_table
360 ksocknal_lib_tunables_init ()
362 if (!*ksocknal_tunables.ksnd_typed_conns) {
364 #if SOCKNAL_VERSION_DEBUG
365 if (*ksocknal_tunables.ksnd_protocol < 3)
369 CERROR("Protocol V3.x MUST have typed connections\n");
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;
379 ksocknal_tunables.ksnd_sysctl =
380 cfs_register_sysctl_table(ksocknal_top_ctl_table, 0);
382 if (ksocknal_tunables.ksnd_sysctl == NULL)
383 CWARN("Can't setup /proc tunables\n");
389 ksocknal_lib_tunables_fini ()
391 if (ksocknal_tunables.ksnd_sysctl != NULL)
392 cfs_unregister_sysctl_table(ksocknal_tunables.ksnd_sysctl);
396 ksocknal_lib_tunables_init ()
402 ksocknal_lib_tunables_fini ()
405 #endif /* # if CONFIG_SYSCTL && !CFS_SYSFS_MODULE_PARM */
408 ksocknal_lib_bind_irq (unsigned int irq)
410 #if (defined(CONFIG_SMP) && defined(CPU_AFFINITY))
414 ksock_irqinfo_t *info;
415 char *argv[] = {"/bin/sh",
419 char *envp[] = {"HOME=/",
420 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
423 LASSERT (irq < NR_IRQS);
424 if (irq == 0) /* software NIC or affinity disabled */
427 info = &ksocknal_data.ksnd_irqinfo[irq];
429 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
431 LASSERT (info->ksni_valid);
432 bind = !info->ksni_bound;
433 info->ksni_bound = 1;
435 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
437 if (!bind) /* bound already */
440 cpu = ksocknal_irqsched2cpu(info->ksni_sched);
441 snprintf (cmdline, sizeof (cmdline),
442 "echo %d > /proc/irq/%u/smp_affinity", 1 << cpu, irq);
444 LCONSOLE_INFO("Binding irq %u to CPU %d with cmd: %s\n",
447 /* FIXME: Find a better method of setting IRQ affinity...
450 USERMODEHELPER(argv[0], argv, envp);
455 ksocknal_lib_get_conn_addrs (ksock_conn_t *conn)
457 int rc = libcfs_sock_getaddr(conn->ksnc_sock, 1,
461 /* Didn't need the {get,put}connsock dance to deref ksnc_sock... */
462 LASSERT (!conn->ksnc_closing);
465 CERROR ("Error %d getting sock peer IP\n", rc);
469 rc = libcfs_sock_getaddr(conn->ksnc_sock, 0,
470 &conn->ksnc_myipaddr, NULL);
472 CERROR ("Error %d getting sock local IP\n", rc);
480 ksocknal_lib_sock_irq (struct socket *sock)
484 struct dst_entry *dst;
486 if (!*ksocknal_tunables.ksnd_irq_affinity)
489 dst = sk_dst_get (sock->sk);
491 if (dst->dev != NULL) {
493 if (irq >= NR_IRQS) {
494 CERROR ("Unexpected IRQ %x\n", irq);
506 ksocknal_lib_zc_capable(ksock_conn_t *conn)
508 int caps = conn->ksnc_sock->sk->sk_route_caps;
510 if (conn->ksnc_proto == &ksocknal_protocol_v1x)
513 /* ZC if the socket supports scatter/gather and doesn't need software
515 return ((caps & NETIF_F_SG) != 0 &&
516 (caps & (NETIF_F_IP_CSUM | NETIF_F_NO_CSUM | NETIF_F_HW_CSUM)) != 0);
520 ksocknal_lib_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
522 struct socket *sock = conn->ksnc_sock;
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);
532 /* NB we can't trust socket ops to either consume our iovs
533 * or leave them alone. */
536 #if SOCKNAL_SINGLE_FRAG_TX
537 struct iovec scratch;
538 struct iovec *scratchiov = &scratch;
539 unsigned int niov = 1;
541 struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
542 unsigned int niov = tx->tx_niov;
544 struct msghdr msg = {
547 .msg_iov = scratchiov,
551 .msg_flags = MSG_DONTWAIT
553 mm_segment_t oldmm = get_fs();
556 for (nob = i = 0; i < niov; i++) {
557 scratchiov[i] = tx->tx_iov[i];
558 nob += scratchiov[i].iov_len;
561 if (!cfs_list_empty(&conn->ksnc_tx_queue) ||
563 msg.msg_flags |= MSG_MORE;
566 rc = sock_sendmsg(sock, &msg, nob);
573 ksocknal_lib_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
575 struct socket *sock = conn->ksnc_sock;
576 lnet_kiov_t *kiov = tx->tx_kiov;
580 /* Not NOOP message */
581 LASSERT (tx->tx_lnetmsg != NULL);
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;
593 CDEBUG(D_NET, "page %p + offset %x for %d\n",
594 page, offset, kiov->kiov_len);
596 if (!cfs_list_empty(&conn->ksnc_tx_queue) ||
597 fragsize < tx->tx_resid)
600 if (sk->sk_prot->sendpage != NULL) {
601 rc = sk->sk_prot->sendpage(sk, page,
602 offset, fragsize, msgflg);
604 rc = cfs_tcp_sendpage(sk, page, offset, fragsize,
608 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
609 struct iovec scratch;
610 struct iovec *scratchiov = &scratch;
611 unsigned int niov = 1;
613 #ifdef CONFIG_HIGHMEM
614 #warning "XXX risk of kmap deadlock on multiple frags..."
616 struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
617 unsigned int niov = tx->tx_nkiov;
619 struct msghdr msg = {
622 .msg_iov = scratchiov,
626 .msg_flags = MSG_DONTWAIT
628 mm_segment_t oldmm = get_fs();
631 for (nob = i = 0; i < niov; i++) {
632 scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
634 nob += scratchiov[i].iov_len = kiov[i].kiov_len;
637 if (!cfs_list_empty(&conn->ksnc_tx_queue) ||
639 msg.msg_flags |= MSG_MORE;
642 rc = sock_sendmsg(sock, &msg, nob);
645 for (i = 0; i < niov; i++)
646 kunmap(kiov[i].kiov_page);
652 ksocknal_lib_eager_ack (ksock_conn_t *conn)
655 mm_segment_t oldmm = get_fs();
656 struct socket *sock = conn->ksnc_sock;
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
664 sock->ops->setsockopt (sock, SOL_TCP, TCP_QUICKACK,
665 (char *)&opt, sizeof (opt));
670 ksocknal_lib_recv_iov (ksock_conn_t *conn)
672 #if SOCKNAL_SINGLE_FRAG_RX
673 struct iovec scratch;
674 struct iovec *scratchiov = &scratch;
675 unsigned int niov = 1;
677 struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
678 unsigned int niov = conn->ksnc_rx_niov;
680 struct iovec *iov = conn->ksnc_rx_iov;
681 struct msghdr msg = {
684 .msg_iov = scratchiov,
690 mm_segment_t oldmm = get_fs();
698 /* NB we can't trust socket ops to either consume our iovs
699 * or leave them alone. */
702 for (nob = i = 0; i < niov; i++) {
703 scratchiov[i] = iov[i];
704 nob += scratchiov[i].iov_len;
706 LASSERT (nob <= conn->ksnc_rx_nob_wanted);
709 rc = sock_recvmsg (conn->ksnc_sock, &msg, nob, MSG_DONTWAIT);
710 /* NB this is just a boolean..........................^ */
714 if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
715 saved_csum = conn->ksnc_msg.ksm_csum;
716 conn->ksnc_msg.ksm_csum = 0;
719 if (saved_csum != 0) {
720 /* accumulate checksum */
721 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
724 fragnob = iov[i].iov_len;
728 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
729 iov[i].iov_base, fragnob);
731 conn->ksnc_msg.ksm_csum = saved_csum;
738 ksocknal_lib_kiov_vunmap(void *addr)
747 ksocknal_lib_kiov_vmap(lnet_kiov_t *kiov, int niov,
748 struct iovec *iov, struct page **pages)
754 if (!*ksocknal_tunables.ksnd_zc_recv || pages == NULL)
757 LASSERT (niov <= LNET_MAX_IOV);
760 niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
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))
768 pages[i] = kiov[i].kiov_page;
769 nob += kiov[i].kiov_len;
772 addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
776 iov->iov_base = addr + kiov[0].kiov_offset;
783 ksocknal_lib_recv_kiov (ksock_conn_t *conn)
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;
791 #ifdef CONFIG_HIGHMEM
792 #warning "XXX risk of kmap deadlock on multiple frags..."
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;
798 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
799 struct msghdr msg = {
802 .msg_iov = scratchiov,
807 mm_segment_t oldmm = get_fs();
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;
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) +
828 msg.msg_iovlen = niov;
831 LASSERT (nob <= conn->ksnc_rx_nob_wanted);
834 rc = sock_recvmsg (conn->ksnc_sock, &msg, nob, MSG_DONTWAIT);
835 /* NB this is just a boolean.......................^ */
838 if (conn->ksnc_msg.ksm_csum != 0) {
839 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
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;
851 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
854 kunmap(kiov[i].kiov_page);
859 ksocknal_lib_kiov_vunmap(addr);
861 for (i = 0; i < niov; i++)
862 kunmap(kiov[i].kiov_page);
869 ksocknal_lib_csum_tx(ksock_tx_t *tx)
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);
879 tx->tx_msg.ksm_csum = 0;
881 csum = ksocknal_csum(~0, (void *)tx->tx_iov[0].iov_base,
882 tx->tx_iov[0].iov_len);
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;
889 csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
891 kunmap(tx->tx_kiov[i].kiov_page);
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);
899 if (*ksocknal_tunables.ksnd_inject_csum_error) {
901 *ksocknal_tunables.ksnd_inject_csum_error = 0;
904 tx->tx_msg.ksm_csum = csum;
908 ksocknal_lib_get_conn_tunables (ksock_conn_t *conn, int *txmem, int *rxmem, int *nagle)
910 mm_segment_t oldmm = get_fs ();
911 struct socket *sock = conn->ksnc_sock;
915 rc = ksocknal_connsock_addref(conn);
917 LASSERT (conn->ksnc_closing);
918 *txmem = *rxmem = *nagle = 0;
922 rc = libcfs_sock_getbuf(sock, txmem, rxmem);
924 len = sizeof(*nagle);
926 rc = sock->ops->getsockopt(sock, SOL_TCP, TCP_NODELAY,
927 (char *)nagle, &len);
931 ksocknal_connsock_decref(conn);
936 *txmem = *rxmem = *nagle = 0;
942 ksocknal_lib_setup_sock (struct socket *sock)
944 mm_segment_t oldmm = get_fs ();
951 struct linger linger;
953 sock->sk->sk_allocation = GFP_NOFS;
955 /* Ensure this socket aborts active sends immediately when we close
962 rc = sock_setsockopt (sock, SOL_SOCKET, SO_LINGER,
963 (char *)&linger, sizeof (linger));
966 CERROR ("Can't set SO_LINGER: %d\n", rc);
972 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_LINGER2,
973 (char *)&option, sizeof (option));
976 CERROR ("Can't set SO_LINGER2: %d\n", rc);
980 if (!*ksocknal_tunables.ksnd_nagle) {
984 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_NODELAY,
985 (char *)&option, sizeof (option));
988 CERROR ("Can't disable nagle: %d\n", rc);
993 rc = libcfs_sock_setbuf(sock,
994 *ksocknal_tunables.ksnd_tx_buffer_size,
995 *ksocknal_tunables.ksnd_rx_buffer_size);
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);
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
1012 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_BACKOFF_INIT,
1013 (char *)&option, sizeof (option));
1016 CERROR ("Can't set initial tcp backoff %d: %d\n",
1022 if (*ksocknal_tunables.ksnd_backoff_max > 0) {
1023 option = *ksocknal_tunables.ksnd_backoff_max;
1024 #ifdef SOCKNAL_BACKOFF_MS
1029 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_BACKOFF_MAX,
1030 (char *)&option, sizeof (option));
1033 CERROR ("Can't set maximum tcp backoff %d: %d\n",
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;
1045 do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
1047 option = (do_keepalive ? 1 : 0);
1049 rc = sock_setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE,
1050 (char *)&option, sizeof (option));
1053 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
1061 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPIDLE,
1062 (char *)&keep_idle, sizeof (keep_idle));
1065 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
1070 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPINTVL,
1071 (char *)&keep_intvl, sizeof (keep_intvl));
1074 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
1079 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPCNT,
1080 (char *)&keep_count, sizeof (keep_count));
1083 CERROR ("Can't set TCP_KEEPCNT: %d\n", rc);
1090 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
1091 #define sock2tcp_opt(sk) tcp_sk(sk)
1093 struct tcp_opt *sock2tcp_opt(struct sock *sk)
1095 struct tcp_sock *s = (struct tcp_sock *)sk;
1101 ksocknal_lib_push_conn (ksock_conn_t *conn)
1104 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11))
1107 struct tcp_sock *tp;
1114 rc = ksocknal_connsock_addref(conn);
1115 if (rc != 0) /* being shut down */
1118 sk = conn->ksnc_sock->sk;
1119 tp = sock2tcp_opt(sk);
1122 nonagle = tp->nonagle;
1129 rc = sk->sk_prot->setsockopt (sk, SOL_TCP, TCP_NODELAY,
1130 (char *)&val, sizeof (val));
1136 tp->nonagle = nonagle;
1139 ksocknal_connsock_decref(conn);
1142 extern void ksocknal_read_callback (ksock_conn_t *conn);
1143 extern void ksocknal_write_callback (ksock_conn_t *conn);
1145 * socket call back in Linux
1148 ksocknal_data_ready (struct sock *sk, int n)
1153 /* interleave correctly with closing sockets... */
1155 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
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);
1162 ksocknal_read_callback(conn);
1164 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1170 ksocknal_write_space (struct sock *sk)
1176 /* interleave correctly with closing sockets... */
1178 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1180 conn = sk->sk_user_data;
1181 wspace = SOCKNAL_WSPACE(sk);
1182 min_wpace = SOCKNAL_MIN_WSPACE(sk);
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"));
1193 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
1194 LASSERT (sk->sk_write_space != &ksocknal_write_space);
1195 sk->sk_write_space (sk);
1197 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1201 if (wspace >= min_wpace) { /* got enough space */
1202 ksocknal_write_callback(conn);
1204 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
1205 * ENOMEM check in ksocknal_transmit is race-free (think about
1208 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
1211 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1215 ksocknal_lib_save_callback(struct socket *sock, ksock_conn_t *conn)
1217 conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
1218 conn->ksnc_saved_write_space = sock->sk->sk_write_space;
1222 ksocknal_lib_set_callback(struct socket *sock, ksock_conn_t *conn)
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;
1231 ksocknal_lib_reset_callback(struct socket *sock, ksock_conn_t *conn)
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;
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;
1248 ksocknal_lib_memory_pressure(ksock_conn_t *conn)
1251 ksock_sched_t *sched;
1253 sched = conn->ksnc_scheduler;
1254 cfs_spin_lock_bh (&sched->kss_lock);
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 */
1269 cfs_spin_unlock_bh (&sched->kss_lock);
1275 ksocknal_lib_bind_thread_to_cpu(int id)
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;
1282 set_cpus_allowed(current, m);