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, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
40 ksocknal_lib_get_conn_addrs(ksock_conn_t *conn)
42 int rc = lnet_sock_getaddr(conn->ksnc_sock, 1, &conn->ksnc_ipaddr,
45 /* Didn't need the {get,put}connsock dance to deref ksnc_sock... */
46 LASSERT(!conn->ksnc_closing);
49 CERROR("Error %d getting sock peer IP\n", rc);
53 rc = lnet_sock_getaddr(conn->ksnc_sock, 0, &conn->ksnc_myipaddr, NULL);
55 CERROR("Error %d getting sock local IP\n", rc);
63 ksocknal_lib_zc_capable(ksock_conn_t *conn)
65 int caps = conn->ksnc_sock->sk->sk_route_caps;
67 if (conn->ksnc_proto == &ksocknal_protocol_v1x)
70 /* ZC if the socket supports scatter/gather and doesn't need software
72 return ((caps & NETIF_F_SG) != 0 && (caps & NETIF_F_ALL_CSUM) != 0);
76 ksocknal_lib_send_iov(ksock_conn_t *conn, ksock_tx_t *tx)
78 struct socket *sock = conn->ksnc_sock;
82 if (*ksocknal_tunables.ksnd_enable_csum && /* checksum enabled */
83 conn->ksnc_proto == &ksocknal_protocol_v2x && /* V2.x connection */
84 tx->tx_nob == tx->tx_resid && /* frist sending */
85 tx->tx_msg.ksm_csum == 0) /* not checksummed */
86 ksocknal_lib_csum_tx(tx);
88 /* NB we can't trust socket ops to either consume our iovs
89 * or leave them alone. */
92 #if SOCKNAL_SINGLE_FRAG_TX
94 struct kvec *scratchiov = &scratch;
95 unsigned int niov = 1;
97 struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
98 unsigned int niov = tx->tx_niov;
100 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
103 for (nob = i = 0; i < niov; i++) {
104 scratchiov[i] = tx->tx_iov[i];
105 nob += scratchiov[i].iov_len;
108 if (!list_empty(&conn->ksnc_tx_queue) ||
110 msg.msg_flags |= MSG_MORE;
112 rc = kernel_sendmsg(sock, &msg, scratchiov, niov, nob);
118 ksocknal_lib_send_kiov(ksock_conn_t *conn, ksock_tx_t *tx)
120 struct socket *sock = conn->ksnc_sock;
121 lnet_kiov_t *kiov = tx->tx_kiov;
125 /* Not NOOP message */
126 LASSERT(tx->tx_lnetmsg != NULL);
128 /* NB we can't trust socket ops to either consume our iovs
129 * or leave them alone. */
130 if (tx->tx_msg.ksm_zc_cookies[0] != 0) {
131 /* Zero copy is enabled */
132 struct sock *sk = sock->sk;
133 struct page *page = kiov->kiov_page;
134 int offset = kiov->kiov_offset;
135 int fragsize = kiov->kiov_len;
136 int msgflg = MSG_DONTWAIT;
138 CDEBUG(D_NET, "page %p + offset %x for %d\n",
139 page, offset, kiov->kiov_len);
141 if (!list_empty(&conn->ksnc_tx_queue) ||
142 fragsize < tx->tx_resid)
145 if (sk->sk_prot->sendpage != NULL) {
146 rc = sk->sk_prot->sendpage(sk, page,
147 offset, fragsize, msgflg);
149 rc = cfs_tcp_sendpage(sk, page, offset, fragsize,
153 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
155 struct kvec *scratchiov = &scratch;
156 unsigned int niov = 1;
158 #ifdef CONFIG_HIGHMEM
159 #warning "XXX risk of kmap deadlock on multiple frags..."
161 struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
162 unsigned int niov = tx->tx_nkiov;
164 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
167 for (nob = i = 0; i < niov; i++) {
168 scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
170 nob += scratchiov[i].iov_len = kiov[i].kiov_len;
173 if (!list_empty(&conn->ksnc_tx_queue) ||
175 msg.msg_flags |= MSG_MORE;
177 rc = kernel_sendmsg(sock, &msg, (struct kvec *)scratchiov, niov, nob);
179 for (i = 0; i < niov; i++)
180 kunmap(kiov[i].kiov_page);
186 ksocknal_lib_eager_ack(ksock_conn_t *conn)
189 struct socket *sock = conn->ksnc_sock;
191 /* Remind the socket to ACK eagerly. If I don't, the socket might
192 * think I'm about to send something it could piggy-back the ACK
193 * on, introducing delay in completing zero-copy sends in my
196 kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
197 (char *)&opt, sizeof(opt));
201 ksocknal_lib_recv_iov(ksock_conn_t *conn)
203 #if SOCKNAL_SINGLE_FRAG_RX
205 struct kvec *scratchiov = &scratch;
206 unsigned int niov = 1;
208 struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
209 unsigned int niov = conn->ksnc_rx_niov;
211 struct kvec *iov = conn->ksnc_rx_iov;
212 struct msghdr msg = {
222 /* NB we can't trust socket ops to either consume our iovs
223 * or leave them alone. */
226 for (nob = i = 0; i < niov; i++) {
227 scratchiov[i] = iov[i];
228 nob += scratchiov[i].iov_len;
230 LASSERT(nob <= conn->ksnc_rx_nob_wanted);
232 rc = kernel_recvmsg(conn->ksnc_sock, &msg,
233 scratchiov, niov, nob, MSG_DONTWAIT);
236 if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
237 saved_csum = conn->ksnc_msg.ksm_csum;
238 conn->ksnc_msg.ksm_csum = 0;
241 if (saved_csum != 0) {
242 /* accumulate checksum */
243 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
246 fragnob = iov[i].iov_len;
250 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
251 iov[i].iov_base, fragnob);
253 conn->ksnc_msg.ksm_csum = saved_csum;
260 ksocknal_lib_kiov_vunmap(void *addr)
269 ksocknal_lib_kiov_vmap(lnet_kiov_t *kiov, int niov,
270 struct kvec *iov, struct page **pages)
276 if (!*ksocknal_tunables.ksnd_zc_recv || pages == NULL)
279 LASSERT(niov <= LNET_MAX_IOV);
282 niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
285 for (nob = i = 0; i < niov; i++) {
286 if ((kiov[i].kiov_offset != 0 && i > 0) ||
287 (kiov[i].kiov_offset + kiov[i].kiov_len != PAGE_CACHE_SIZE && i < niov - 1))
290 pages[i] = kiov[i].kiov_page;
291 nob += kiov[i].kiov_len;
294 addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
298 iov->iov_base = addr + kiov[0].kiov_offset;
305 ksocknal_lib_recv_kiov(ksock_conn_t *conn)
307 #if SOCKNAL_SINGLE_FRAG_RX || !SOCKNAL_RISK_KMAP_DEADLOCK
309 struct kvec *scratchiov = &scratch;
310 struct page **pages = NULL;
311 unsigned int niov = 1;
313 #ifdef CONFIG_HIGHMEM
314 #warning "XXX risk of kmap deadlock on multiple frags..."
316 struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
317 struct page **pages = conn->ksnc_scheduler->kss_rx_scratch_pgs;
318 unsigned int niov = conn->ksnc_rx_nkiov;
320 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
321 struct msghdr msg = {
333 /* NB we can't trust socket ops to either consume our iovs
334 * or leave them alone. */
335 addr = ksocknal_lib_kiov_vmap(kiov, niov, scratchiov, pages);
337 nob = scratchiov[0].iov_len;
341 for (nob = i = 0; i < niov; i++) {
342 nob += scratchiov[i].iov_len = kiov[i].kiov_len;
343 scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
349 LASSERT(nob <= conn->ksnc_rx_nob_wanted);
351 rc = kernel_recvmsg(conn->ksnc_sock, &msg,
352 (struct kvec *)scratchiov, n, nob, MSG_DONTWAIT);
354 if (conn->ksnc_msg.ksm_csum != 0) {
355 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
358 /* Dang! have to kmap again because I have nowhere to
359 * stash the mapped address. But by doing it while the
360 * page is still mapped, the kernel just bumps the map
361 * count and returns me the address it stashed. */
362 base = kmap(kiov[i].kiov_page) + kiov[i].kiov_offset;
363 fragnob = kiov[i].kiov_len;
367 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
370 kunmap(kiov[i].kiov_page);
375 ksocknal_lib_kiov_vunmap(addr);
377 for (i = 0; i < niov; i++)
378 kunmap(kiov[i].kiov_page);
385 ksocknal_lib_csum_tx(ksock_tx_t *tx)
391 LASSERT(tx->tx_iov[0].iov_base == &tx->tx_msg);
392 LASSERT(tx->tx_conn != NULL);
393 LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
395 tx->tx_msg.ksm_csum = 0;
397 csum = ksocknal_csum(~0, tx->tx_iov[0].iov_base,
398 tx->tx_iov[0].iov_len);
400 if (tx->tx_kiov != NULL) {
401 for (i = 0; i < tx->tx_nkiov; i++) {
402 base = kmap(tx->tx_kiov[i].kiov_page) +
403 tx->tx_kiov[i].kiov_offset;
405 csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
407 kunmap(tx->tx_kiov[i].kiov_page);
410 for (i = 1; i < tx->tx_niov; i++)
411 csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
412 tx->tx_iov[i].iov_len);
415 if (*ksocknal_tunables.ksnd_inject_csum_error) {
417 *ksocknal_tunables.ksnd_inject_csum_error = 0;
420 tx->tx_msg.ksm_csum = csum;
424 ksocknal_lib_get_conn_tunables(ksock_conn_t *conn, int *txmem, int *rxmem, int *nagle)
426 struct socket *sock = conn->ksnc_sock;
430 rc = ksocknal_connsock_addref(conn);
432 LASSERT(conn->ksnc_closing);
433 *txmem = *rxmem = *nagle = 0;
437 rc = lnet_sock_getbuf(sock, txmem, rxmem);
439 len = sizeof(*nagle);
440 rc = kernel_getsockopt(sock, SOL_TCP, TCP_NODELAY,
441 (char *)nagle, &len);
444 ksocknal_connsock_decref(conn);
449 *txmem = *rxmem = *nagle = 0;
455 ksocknal_lib_setup_sock(struct socket *sock)
463 struct linger linger;
465 sock->sk->sk_allocation = GFP_NOFS;
467 /* Ensure this socket aborts active sends immediately when we close
473 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
474 (char *)&linger, sizeof(linger));
476 CERROR("Can't set SO_LINGER: %d\n", rc);
481 rc = kernel_setsockopt(sock, SOL_TCP, TCP_LINGER2,
482 (char *)&option, sizeof(option));
484 CERROR("Can't set SO_LINGER2: %d\n", rc);
488 if (!*ksocknal_tunables.ksnd_nagle) {
491 rc = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
492 (char *)&option, sizeof(option));
494 CERROR("Can't disable nagle: %d\n", rc);
499 rc = lnet_sock_setbuf(sock, *ksocknal_tunables.ksnd_tx_buffer_size,
500 *ksocknal_tunables.ksnd_rx_buffer_size);
502 CERROR("Can't set buffer tx %d, rx %d buffers: %d\n",
503 *ksocknal_tunables.ksnd_tx_buffer_size,
504 *ksocknal_tunables.ksnd_rx_buffer_size, rc);
508 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
510 /* snapshot tunables */
511 keep_idle = *ksocknal_tunables.ksnd_keepalive_idle;
512 keep_count = *ksocknal_tunables.ksnd_keepalive_count;
513 keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
515 do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
517 option = (do_keepalive ? 1 : 0);
518 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
519 (char *)&option, sizeof(option));
521 CERROR("Can't set SO_KEEPALIVE: %d\n", rc);
528 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
529 (char *)&keep_idle, sizeof(keep_idle));
531 CERROR("Can't set TCP_KEEPIDLE: %d\n", rc);
535 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
536 (char *)&keep_intvl, sizeof(keep_intvl));
538 CERROR("Can't set TCP_KEEPINTVL: %d\n", rc);
542 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
543 (char *)&keep_count, sizeof(keep_count));
545 CERROR("Can't set TCP_KEEPCNT: %d\n", rc);
553 ksocknal_lib_push_conn(ksock_conn_t *conn)
561 rc = ksocknal_connsock_addref(conn);
562 if (rc != 0) /* being shut down */
565 sk = conn->ksnc_sock->sk;
569 nonagle = tp->nonagle;
573 rc = kernel_setsockopt(conn->ksnc_sock, SOL_TCP, TCP_NODELAY,
574 (char *)&val, sizeof(val));
578 tp->nonagle = nonagle;
581 ksocknal_connsock_decref(conn);
584 extern void ksocknal_read_callback(ksock_conn_t *conn);
585 extern void ksocknal_write_callback(ksock_conn_t *conn);
587 * socket call back in Linux
590 ksocknal_data_ready(struct sock *sk)
594 /* interleave correctly with closing sockets... */
596 read_lock(&ksocknal_data.ksnd_global_lock);
598 conn = sk->sk_user_data;
599 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
600 LASSERT(sk->sk_data_ready != &ksocknal_data_ready);
601 sk->sk_data_ready(sk);
603 ksocknal_read_callback(conn);
605 read_unlock(&ksocknal_data.ksnd_global_lock);
609 ksocknal_write_space(struct sock *sk)
615 /* interleave correctly with closing sockets... */
617 read_lock(&ksocknal_data.ksnd_global_lock);
619 conn = sk->sk_user_data;
620 wspace = SOCKNAL_WSPACE(sk);
621 min_wpace = SOCKNAL_MIN_WSPACE(sk);
623 CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
624 sk, wspace, min_wpace, conn,
625 (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
626 " ready" : " blocked"),
627 (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
628 " scheduled" : " idle"),
629 (conn == NULL) ? "" : (list_empty(&conn->ksnc_tx_queue) ?
630 " empty" : " queued"));
632 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
633 LASSERT(sk->sk_write_space != &ksocknal_write_space);
634 sk->sk_write_space(sk);
636 read_unlock(&ksocknal_data.ksnd_global_lock);
640 if (wspace >= min_wpace) { /* got enough space */
641 ksocknal_write_callback(conn);
643 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
644 * ENOMEM check in ksocknal_transmit is race-free (think about
647 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
650 read_unlock(&ksocknal_data.ksnd_global_lock);
654 ksocknal_lib_save_callback(struct socket *sock, ksock_conn_t *conn)
656 conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
657 conn->ksnc_saved_write_space = sock->sk->sk_write_space;
661 ksocknal_lib_set_callback(struct socket *sock, ksock_conn_t *conn)
663 sock->sk->sk_user_data = conn;
664 sock->sk->sk_data_ready = ksocknal_data_ready;
665 sock->sk->sk_write_space = ksocknal_write_space;
670 ksocknal_lib_reset_callback(struct socket *sock, ksock_conn_t *conn)
672 /* Remove conn's network callbacks.
673 * NB I _have_ to restore the callback, rather than storing a noop,
674 * since the socket could survive past this module being unloaded!! */
675 sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
676 sock->sk->sk_write_space = conn->ksnc_saved_write_space;
678 /* A callback could be in progress already; they hold a read lock
679 * on ksnd_global_lock (to serialise with me) and NOOP if
680 * sk_user_data is NULL. */
681 sock->sk->sk_user_data = NULL;
687 ksocknal_lib_memory_pressure(ksock_conn_t *conn)
690 ksock_sched_t *sched;
692 sched = conn->ksnc_scheduler;
693 spin_lock_bh(&sched->kss_lock);
695 if (!test_bit(SOCK_NOSPACE, &conn->ksnc_sock->flags) &&
696 !conn->ksnc_tx_ready) {
697 /* SOCK_NOSPACE is set when the socket fills
698 * and cleared in the write_space callback
699 * (which also sets ksnc_tx_ready). If
700 * SOCK_NOSPACE and ksnc_tx_ready are BOTH
701 * zero, I didn't fill the socket and
702 * write_space won't reschedule me, so I
703 * return -ENOMEM to get my caller to retry
708 spin_unlock_bh(&sched->kss_lock);