1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
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 = tcp_sendpage(sock, page, offset, fragsize, msgflg);
607 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
608 struct iovec scratch;
609 struct iovec *scratchiov = &scratch;
610 unsigned int niov = 1;
612 #ifdef CONFIG_HIGHMEM
613 #warning "XXX risk of kmap deadlock on multiple frags..."
615 struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
616 unsigned int niov = tx->tx_nkiov;
618 struct msghdr msg = {
621 .msg_iov = scratchiov,
625 .msg_flags = MSG_DONTWAIT
627 mm_segment_t oldmm = get_fs();
630 for (nob = i = 0; i < niov; i++) {
631 scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
633 nob += scratchiov[i].iov_len = kiov[i].kiov_len;
636 if (!cfs_list_empty(&conn->ksnc_tx_queue) ||
638 msg.msg_flags |= MSG_MORE;
641 rc = sock_sendmsg(sock, &msg, nob);
644 for (i = 0; i < niov; i++)
645 kunmap(kiov[i].kiov_page);
651 ksocknal_lib_eager_ack (ksock_conn_t *conn)
654 mm_segment_t oldmm = get_fs();
655 struct socket *sock = conn->ksnc_sock;
657 /* Remind the socket to ACK eagerly. If I don't, the socket might
658 * think I'm about to send something it could piggy-back the ACK
659 * on, introducing delay in completing zero-copy sends in my
663 sock->ops->setsockopt (sock, SOL_TCP, TCP_QUICKACK,
664 (char *)&opt, sizeof (opt));
669 ksocknal_lib_recv_iov (ksock_conn_t *conn)
671 #if SOCKNAL_SINGLE_FRAG_RX
672 struct iovec scratch;
673 struct iovec *scratchiov = &scratch;
674 unsigned int niov = 1;
676 struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
677 unsigned int niov = conn->ksnc_rx_niov;
679 struct iovec *iov = conn->ksnc_rx_iov;
680 struct msghdr msg = {
683 .msg_iov = scratchiov,
689 mm_segment_t oldmm = get_fs();
697 /* NB we can't trust socket ops to either consume our iovs
698 * or leave them alone. */
701 for (nob = i = 0; i < niov; i++) {
702 scratchiov[i] = iov[i];
703 nob += scratchiov[i].iov_len;
705 LASSERT (nob <= conn->ksnc_rx_nob_wanted);
708 rc = sock_recvmsg (conn->ksnc_sock, &msg, nob, MSG_DONTWAIT);
709 /* NB this is just a boolean..........................^ */
713 if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
714 saved_csum = conn->ksnc_msg.ksm_csum;
715 conn->ksnc_msg.ksm_csum = 0;
718 if (saved_csum != 0) {
719 /* accumulate checksum */
720 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
723 fragnob = iov[i].iov_len;
727 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
728 iov[i].iov_base, fragnob);
730 conn->ksnc_msg.ksm_csum = saved_csum;
737 ksocknal_lib_kiov_vunmap(void *addr)
746 ksocknal_lib_kiov_vmap(lnet_kiov_t *kiov, int niov,
747 struct iovec *iov, struct page **pages)
753 if (!*ksocknal_tunables.ksnd_zc_recv || pages == NULL)
756 LASSERT (niov <= LNET_MAX_IOV);
759 niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
762 for (nob = i = 0; i < niov; i++) {
763 if ((kiov[i].kiov_offset != 0 && i > 0) ||
764 (kiov[i].kiov_offset + kiov[i].kiov_len != CFS_PAGE_SIZE && i < niov - 1))
767 pages[i] = kiov[i].kiov_page;
768 nob += kiov[i].kiov_len;
771 addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
775 iov->iov_base = addr + kiov[0].kiov_offset;
782 ksocknal_lib_recv_kiov (ksock_conn_t *conn)
784 #if SOCKNAL_SINGLE_FRAG_RX || !SOCKNAL_RISK_KMAP_DEADLOCK
785 struct iovec scratch;
786 struct iovec *scratchiov = &scratch;
787 struct page **pages = NULL;
788 unsigned int niov = 1;
790 #ifdef CONFIG_HIGHMEM
791 #warning "XXX risk of kmap deadlock on multiple frags..."
793 struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
794 struct page **pages = conn->ksnc_scheduler->kss_rx_scratch_pgs;
795 unsigned int niov = conn->ksnc_rx_nkiov;
797 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
798 struct msghdr msg = {
801 .msg_iov = scratchiov,
806 mm_segment_t oldmm = get_fs();
815 /* NB we can't trust socket ops to either consume our iovs
816 * or leave them alone. */
817 if ((addr = ksocknal_lib_kiov_vmap(kiov, niov, scratchiov, pages)) != NULL) {
818 nob = scratchiov[0].iov_len;
822 for (nob = i = 0; i < niov; i++) {
823 nob += scratchiov[i].iov_len = kiov[i].kiov_len;
824 scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
827 msg.msg_iovlen = niov;
830 LASSERT (nob <= conn->ksnc_rx_nob_wanted);
833 rc = sock_recvmsg (conn->ksnc_sock, &msg, nob, MSG_DONTWAIT);
834 /* NB this is just a boolean.......................^ */
837 if (conn->ksnc_msg.ksm_csum != 0) {
838 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
841 /* Dang! have to kmap again because I have nowhere to stash the
842 * mapped address. But by doing it while the page is still
843 * mapped, the kernel just bumps the map count and returns me
844 * the address it stashed. */
845 base = kmap(kiov[i].kiov_page) + kiov[i].kiov_offset;
846 fragnob = kiov[i].kiov_len;
850 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
853 kunmap(kiov[i].kiov_page);
858 ksocknal_lib_kiov_vunmap(addr);
860 for (i = 0; i < niov; i++)
861 kunmap(kiov[i].kiov_page);
868 ksocknal_lib_csum_tx(ksock_tx_t *tx)
874 LASSERT(tx->tx_iov[0].iov_base == (void *)&tx->tx_msg);
875 LASSERT(tx->tx_conn != NULL);
876 LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
878 tx->tx_msg.ksm_csum = 0;
880 csum = ksocknal_csum(~0, (void *)tx->tx_iov[0].iov_base,
881 tx->tx_iov[0].iov_len);
883 if (tx->tx_kiov != NULL) {
884 for (i = 0; i < tx->tx_nkiov; i++) {
885 base = kmap(tx->tx_kiov[i].kiov_page) +
886 tx->tx_kiov[i].kiov_offset;
888 csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
890 kunmap(tx->tx_kiov[i].kiov_page);
893 for (i = 1; i < tx->tx_niov; i++)
894 csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
895 tx->tx_iov[i].iov_len);
898 if (*ksocknal_tunables.ksnd_inject_csum_error) {
900 *ksocknal_tunables.ksnd_inject_csum_error = 0;
903 tx->tx_msg.ksm_csum = csum;
907 ksocknal_lib_get_conn_tunables (ksock_conn_t *conn, int *txmem, int *rxmem, int *nagle)
909 mm_segment_t oldmm = get_fs ();
910 struct socket *sock = conn->ksnc_sock;
914 rc = ksocknal_connsock_addref(conn);
916 LASSERT (conn->ksnc_closing);
917 *txmem = *rxmem = *nagle = 0;
921 rc = libcfs_sock_getbuf(sock, txmem, rxmem);
923 len = sizeof(*nagle);
925 rc = sock->ops->getsockopt(sock, SOL_TCP, TCP_NODELAY,
926 (char *)nagle, &len);
930 ksocknal_connsock_decref(conn);
935 *txmem = *rxmem = *nagle = 0;
941 ksocknal_lib_setup_sock (struct socket *sock)
943 mm_segment_t oldmm = get_fs ();
950 struct linger linger;
952 sock->sk->sk_allocation = GFP_NOFS;
954 /* Ensure this socket aborts active sends immediately when we close
961 rc = sock_setsockopt (sock, SOL_SOCKET, SO_LINGER,
962 (char *)&linger, sizeof (linger));
965 CERROR ("Can't set SO_LINGER: %d\n", rc);
971 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_LINGER2,
972 (char *)&option, sizeof (option));
975 CERROR ("Can't set SO_LINGER2: %d\n", rc);
979 if (!*ksocknal_tunables.ksnd_nagle) {
983 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_NODELAY,
984 (char *)&option, sizeof (option));
987 CERROR ("Can't disable nagle: %d\n", rc);
992 rc = libcfs_sock_setbuf(sock,
993 *ksocknal_tunables.ksnd_tx_buffer_size,
994 *ksocknal_tunables.ksnd_rx_buffer_size);
996 CERROR ("Can't set buffer tx %d, rx %d buffers: %d\n",
997 *ksocknal_tunables.ksnd_tx_buffer_size,
998 *ksocknal_tunables.ksnd_rx_buffer_size, rc);
1002 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
1003 #ifdef SOCKNAL_BACKOFF
1004 if (*ksocknal_tunables.ksnd_backoff_init > 0) {
1005 option = *ksocknal_tunables.ksnd_backoff_init;
1006 #ifdef SOCKNAL_BACKOFF_MS
1011 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_BACKOFF_INIT,
1012 (char *)&option, sizeof (option));
1015 CERROR ("Can't set initial tcp backoff %d: %d\n",
1021 if (*ksocknal_tunables.ksnd_backoff_max > 0) {
1022 option = *ksocknal_tunables.ksnd_backoff_max;
1023 #ifdef SOCKNAL_BACKOFF_MS
1028 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_BACKOFF_MAX,
1029 (char *)&option, sizeof (option));
1032 CERROR ("Can't set maximum tcp backoff %d: %d\n",
1039 /* snapshot tunables */
1040 keep_idle = *ksocknal_tunables.ksnd_keepalive_idle;
1041 keep_count = *ksocknal_tunables.ksnd_keepalive_count;
1042 keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
1044 do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
1046 option = (do_keepalive ? 1 : 0);
1048 rc = sock_setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE,
1049 (char *)&option, sizeof (option));
1052 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
1060 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPIDLE,
1061 (char *)&keep_idle, sizeof (keep_idle));
1064 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
1069 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPINTVL,
1070 (char *)&keep_intvl, sizeof (keep_intvl));
1073 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
1078 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPCNT,
1079 (char *)&keep_count, sizeof (keep_count));
1082 CERROR ("Can't set TCP_KEEPCNT: %d\n", rc);
1089 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
1090 #define sock2tcp_opt(sk) tcp_sk(sk)
1092 struct tcp_opt *sock2tcp_opt(struct sock *sk)
1094 struct tcp_sock *s = (struct tcp_sock *)sk;
1100 ksocknal_lib_push_conn (ksock_conn_t *conn)
1103 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11))
1106 struct tcp_sock *tp;
1113 rc = ksocknal_connsock_addref(conn);
1114 if (rc != 0) /* being shut down */
1117 sk = conn->ksnc_sock->sk;
1118 tp = sock2tcp_opt(sk);
1121 nonagle = tp->nonagle;
1128 rc = sk->sk_prot->setsockopt (sk, SOL_TCP, TCP_NODELAY,
1129 (char *)&val, sizeof (val));
1135 tp->nonagle = nonagle;
1138 ksocknal_connsock_decref(conn);
1141 extern void ksocknal_read_callback (ksock_conn_t *conn);
1142 extern void ksocknal_write_callback (ksock_conn_t *conn);
1144 * socket call back in Linux
1147 ksocknal_data_ready (struct sock *sk, int n)
1152 /* interleave correctly with closing sockets... */
1154 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1156 conn = sk->sk_user_data;
1157 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
1158 LASSERT (sk->sk_data_ready != &ksocknal_data_ready);
1159 sk->sk_data_ready (sk, n);
1161 ksocknal_read_callback(conn);
1163 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1169 ksocknal_write_space (struct sock *sk)
1175 /* interleave correctly with closing sockets... */
1177 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1179 conn = sk->sk_user_data;
1180 wspace = SOCKNAL_WSPACE(sk);
1181 min_wpace = SOCKNAL_MIN_WSPACE(sk);
1183 CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
1184 sk, wspace, min_wpace, conn,
1185 (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
1186 " ready" : " blocked"),
1187 (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
1188 " scheduled" : " idle"),
1189 (conn == NULL) ? "" : (cfs_list_empty (&conn->ksnc_tx_queue) ?
1190 " empty" : " queued"));
1192 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
1193 LASSERT (sk->sk_write_space != &ksocknal_write_space);
1194 sk->sk_write_space (sk);
1196 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1200 if (wspace >= min_wpace) { /* got enough space */
1201 ksocknal_write_callback(conn);
1203 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
1204 * ENOMEM check in ksocknal_transmit is race-free (think about
1207 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
1210 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1214 ksocknal_lib_save_callback(struct socket *sock, ksock_conn_t *conn)
1216 conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
1217 conn->ksnc_saved_write_space = sock->sk->sk_write_space;
1221 ksocknal_lib_set_callback(struct socket *sock, ksock_conn_t *conn)
1223 sock->sk->sk_user_data = conn;
1224 sock->sk->sk_data_ready = ksocknal_data_ready;
1225 sock->sk->sk_write_space = ksocknal_write_space;
1230 ksocknal_lib_reset_callback(struct socket *sock, ksock_conn_t *conn)
1232 /* Remove conn's network callbacks.
1233 * NB I _have_ to restore the callback, rather than storing a noop,
1234 * since the socket could survive past this module being unloaded!! */
1235 sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
1236 sock->sk->sk_write_space = conn->ksnc_saved_write_space;
1238 /* A callback could be in progress already; they hold a read lock
1239 * on ksnd_global_lock (to serialise with me) and NOOP if
1240 * sk_user_data is NULL. */
1241 sock->sk->sk_user_data = NULL;
1247 ksocknal_lib_memory_pressure(ksock_conn_t *conn)
1250 ksock_sched_t *sched;
1252 sched = conn->ksnc_scheduler;
1253 cfs_spin_lock_bh (&sched->kss_lock);
1255 if (!SOCK_TEST_NOSPACE(conn->ksnc_sock) &&
1256 !conn->ksnc_tx_ready) {
1257 /* SOCK_NOSPACE is set when the socket fills
1258 * and cleared in the write_space callback
1259 * (which also sets ksnc_tx_ready). If
1260 * SOCK_NOSPACE and ksnc_tx_ready are BOTH
1261 * zero, I didn't fill the socket and
1262 * write_space won't reschedule me, so I
1263 * return -ENOMEM to get my caller to retry
1264 * after a timeout */
1268 cfs_spin_unlock_bh (&sched->kss_lock);
1274 ksocknal_lib_bind_thread_to_cpu(int id)
1276 #if defined(CONFIG_SMP) && defined(CPU_AFFINITY)
1277 id = ksocknal_sched2cpu(id);
1278 if (cpu_online(id)) {
1279 cpumask_t m = CPU_MASK_NONE;
1281 set_cpus_allowed(current, m);