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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
36 ksocknal_lib_get_conn_addrs(struct ksock_conn *conn)
38 int rc = lnet_sock_getaddr(conn->ksnc_sock, true,
42 /* Didn't need the {get,put}connsock dance to deref ksnc_sock... */
43 LASSERT (!conn->ksnc_closing);
46 CERROR ("Error %d getting sock peer_ni IP\n", rc);
50 rc = lnet_sock_getaddr(conn->ksnc_sock, false,
51 &conn->ksnc_myipaddr, NULL);
53 CERROR ("Error %d getting sock local IP\n", rc);
61 ksocknal_lib_zc_capable(struct ksock_conn *conn)
63 int caps = conn->ksnc_sock->sk->sk_route_caps;
65 if (conn->ksnc_proto == &ksocknal_protocol_v1x)
68 /* ZC if the socket supports scatter/gather and doesn't need software
70 return ((caps & NETIF_F_SG) != 0 && (caps & NETIF_F_CSUM_MASK) != 0);
74 ksocknal_lib_send_hdr(struct ksock_conn *conn, struct ksock_tx *tx,
75 struct kvec *scratchiov)
77 struct socket *sock = conn->ksnc_sock;
81 if (*ksocknal_tunables.ksnd_enable_csum && /* checksum enabled */
82 conn->ksnc_proto == &ksocknal_protocol_v2x && /* V2.x connection */
83 tx->tx_nob == tx->tx_resid && /* frist sending */
84 tx->tx_msg.ksm_csum == 0) /* not checksummed */
85 ksocknal_lib_csum_tx(tx);
87 /* NB we can't trust socket ops to either consume our iovs
88 * or leave them alone. */
91 #if SOCKNAL_SINGLE_FRAG_TX
93 struct kvec *scratchiov = &scratch;
94 unsigned int niov = 1;
96 unsigned int niov = tx->tx_niov;
98 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
101 scratchiov[0] = tx->tx_hdr;
102 nob += scratchiov[0].iov_len;
105 if (!list_empty(&conn->ksnc_tx_queue) ||
107 msg.msg_flags |= MSG_MORE;
109 rc = kernel_sendmsg(sock, &msg, scratchiov, niov, nob);
115 ksocknal_lib_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx,
116 struct kvec *scratchiov)
118 struct socket *sock = conn->ksnc_sock;
119 struct bio_vec *kiov = tx->tx_kiov;
123 /* Not NOOP message */
124 LASSERT(tx->tx_lnetmsg != NULL);
126 /* NB we can't trust socket ops to either consume our iovs
127 * or leave them alone. */
128 if (tx->tx_msg.ksm_zc_cookies[0] != 0) {
129 /* Zero copy is enabled */
130 struct sock *sk = sock->sk;
131 struct page *page = kiov->bv_page;
132 int offset = kiov->bv_offset;
133 int fragsize = kiov->bv_len;
134 int msgflg = MSG_DONTWAIT;
136 CDEBUG(D_NET, "page %p + offset %x for %d\n",
137 page, offset, kiov->bv_len);
139 if (!list_empty(&conn->ksnc_tx_queue) ||
140 fragsize < tx->tx_resid)
143 rc = sk->sk_prot->sendpage(sk, page,
144 offset, fragsize, msgflg);
146 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
148 struct kvec *scratchiov = &scratch;
149 unsigned int niov = 1;
151 #ifdef CONFIG_HIGHMEM
152 #warning "XXX risk of kmap deadlock on multiple frags..."
154 unsigned int niov = tx->tx_nkiov;
156 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
159 for (nob = i = 0; i < niov; i++) {
160 scratchiov[i].iov_base = kmap(kiov[i].bv_page) +
162 nob += scratchiov[i].iov_len = kiov[i].bv_len;
165 if (!list_empty(&conn->ksnc_tx_queue) ||
167 msg.msg_flags |= MSG_MORE;
169 rc = kernel_sendmsg(sock, &msg, scratchiov, niov, nob);
171 for (i = 0; i < niov; i++)
172 kunmap(kiov[i].bv_page);
178 ksocknal_lib_eager_ack(struct ksock_conn *conn)
181 struct socket *sock = conn->ksnc_sock;
183 /* Remind the socket to ACK eagerly. If I don't, the socket might
184 * think I'm about to send something it could piggy-back the ACK
185 * on, introducing delay in completing zero-copy sends in my
188 kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
189 (char *)&opt, sizeof(opt));
193 ksocknal_lib_recv_iov(struct ksock_conn *conn, struct kvec *scratchiov)
195 #if SOCKNAL_SINGLE_FRAG_RX
197 struct kvec *scratchiov = &scratch;
198 unsigned int niov = 1;
200 unsigned int niov = conn->ksnc_rx_niov;
202 struct kvec *iov = conn->ksnc_rx_iov;
203 struct msghdr msg = {
213 /* NB we can't trust socket ops to either consume our iovs
214 * or leave them alone. */
217 for (nob = i = 0; i < niov; i++) {
218 scratchiov[i] = iov[i];
219 nob += scratchiov[i].iov_len;
221 LASSERT (nob <= conn->ksnc_rx_nob_wanted);
223 rc = kernel_recvmsg(conn->ksnc_sock, &msg, scratchiov, niov, nob,
227 if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
228 saved_csum = conn->ksnc_msg.ksm_csum;
229 conn->ksnc_msg.ksm_csum = 0;
232 if (saved_csum != 0) {
233 /* accumulate checksum */
234 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
237 fragnob = iov[i].iov_len;
241 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
242 iov[i].iov_base, fragnob);
244 conn->ksnc_msg.ksm_csum = saved_csum;
251 ksocknal_lib_kiov_vunmap(void *addr)
260 ksocknal_lib_kiov_vmap(struct bio_vec *kiov, int niov,
261 struct kvec *iov, struct page **pages)
267 if (!*ksocknal_tunables.ksnd_zc_recv || pages == NULL)
270 LASSERT (niov <= LNET_MAX_IOV);
273 niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
276 for (nob = i = 0; i < niov; i++) {
277 if ((kiov[i].bv_offset != 0 && i > 0) ||
278 (kiov[i].bv_offset + kiov[i].bv_len !=
279 PAGE_SIZE && i < niov - 1))
282 pages[i] = kiov[i].bv_page;
283 nob += kiov[i].bv_len;
286 addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
290 iov->iov_base = addr + kiov[0].bv_offset;
297 ksocknal_lib_recv_kiov(struct ksock_conn *conn, struct page **pages,
298 struct kvec *scratchiov)
300 #if SOCKNAL_SINGLE_FRAG_RX || !SOCKNAL_RISK_KMAP_DEADLOCK
302 struct kvec *scratchiov = &scratch;
303 struct page **pages = NULL;
304 unsigned int niov = 1;
306 #ifdef CONFIG_HIGHMEM
307 #warning "XXX risk of kmap deadlock on multiple frags..."
309 unsigned int niov = conn->ksnc_rx_nkiov;
311 struct bio_vec *kiov = conn->ksnc_rx_kiov;
312 struct msghdr msg = {
324 /* NB we can't trust socket ops to either consume our iovs
325 * or leave them alone. */
326 if ((addr = ksocknal_lib_kiov_vmap(kiov, niov, scratchiov, pages)) != NULL) {
327 nob = scratchiov[0].iov_len;
331 for (nob = i = 0; i < niov; i++) {
332 nob += scratchiov[i].iov_len = kiov[i].bv_len;
333 scratchiov[i].iov_base = kmap(kiov[i].bv_page) +
339 LASSERT (nob <= conn->ksnc_rx_nob_wanted);
341 rc = kernel_recvmsg(conn->ksnc_sock, &msg, scratchiov, n, nob,
344 if (conn->ksnc_msg.ksm_csum != 0) {
345 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
348 /* Dang! have to kmap again because I have nowhere to
349 * stash the mapped address. But by doing it while the
350 * page is still mapped, the kernel just bumps the map
351 * count and returns me the address it stashed.
353 base = kmap(kiov[i].bv_page) + kiov[i].bv_offset;
354 fragnob = kiov[i].bv_len;
358 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
361 kunmap(kiov[i].bv_page);
366 ksocknal_lib_kiov_vunmap(addr);
368 for (i = 0; i < niov; i++)
369 kunmap(kiov[i].bv_page);
376 ksocknal_lib_csum_tx(struct ksock_tx *tx)
382 LASSERT(tx->tx_hdr.iov_base == (void *)&tx->tx_msg);
383 LASSERT(tx->tx_conn != NULL);
384 LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
386 tx->tx_msg.ksm_csum = 0;
388 csum = ksocknal_csum(~0, (void *)tx->tx_hdr.iov_base,
391 for (i = 0; i < tx->tx_nkiov; i++) {
392 base = kmap(tx->tx_kiov[i].bv_page) +
393 tx->tx_kiov[i].bv_offset;
395 csum = ksocknal_csum(csum, base, tx->tx_kiov[i].bv_len);
397 kunmap(tx->tx_kiov[i].bv_page);
400 if (*ksocknal_tunables.ksnd_inject_csum_error) {
402 *ksocknal_tunables.ksnd_inject_csum_error = 0;
405 tx->tx_msg.ksm_csum = csum;
409 ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem, int *rxmem, int *nagle)
411 struct socket *sock = conn->ksnc_sock;
415 rc = ksocknal_connsock_addref(conn);
417 LASSERT (conn->ksnc_closing);
418 *txmem = *rxmem = *nagle = 0;
422 rc = lnet_sock_getbuf(sock, txmem, rxmem);
424 len = sizeof(*nagle);
425 rc = kernel_getsockopt(sock, SOL_TCP, TCP_NODELAY,
426 (char *)nagle, &len);
429 ksocknal_connsock_decref(conn);
434 *txmem = *rxmem = *nagle = 0;
440 ksocknal_lib_setup_sock (struct socket *sock)
448 struct linger linger;
450 sock->sk->sk_allocation = GFP_NOFS;
452 /* Ensure this socket aborts active sends immediately when we close
458 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
459 (char *)&linger, sizeof(linger));
461 CERROR ("Can't set SO_LINGER: %d\n", rc);
466 rc = kernel_setsockopt(sock, SOL_TCP, TCP_LINGER2,
467 (char *)&option, sizeof(option));
469 CERROR ("Can't set SO_LINGER2: %d\n", rc);
473 if (!*ksocknal_tunables.ksnd_nagle) {
476 rc = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
477 (char *)&option, sizeof(option));
479 CERROR ("Can't disable nagle: %d\n", rc);
484 rc = lnet_sock_setbuf(sock,
485 *ksocknal_tunables.ksnd_tx_buffer_size,
486 *ksocknal_tunables.ksnd_rx_buffer_size);
488 CERROR ("Can't set buffer tx %d, rx %d buffers: %d\n",
489 *ksocknal_tunables.ksnd_tx_buffer_size,
490 *ksocknal_tunables.ksnd_rx_buffer_size, rc);
494 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
495 #ifdef SOCKNAL_BACKOFF
496 if (*ksocknal_tunables.ksnd_backoff_init > 0) {
497 option = *ksocknal_tunables.ksnd_backoff_init;
498 #ifdef SOCKNAL_BACKOFF_MS
502 rc = kernel_setsockopt(sock, SOL_TCP, TCP_BACKOFF_INIT,
503 (char *)&option, sizeof(option));
505 CERROR ("Can't set initial tcp backoff %d: %d\n",
511 if (*ksocknal_tunables.ksnd_backoff_max > 0) {
512 option = *ksocknal_tunables.ksnd_backoff_max;
513 #ifdef SOCKNAL_BACKOFF_MS
517 rc = kernel_setsockopt(sock, SOL_TCP, TCP_BACKOFF_MAX,
518 (char *)&option, sizeof(option));
520 CERROR ("Can't set maximum tcp backoff %d: %d\n",
527 /* snapshot tunables */
528 keep_idle = *ksocknal_tunables.ksnd_keepalive_idle;
529 keep_count = *ksocknal_tunables.ksnd_keepalive_count;
530 keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
532 do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
534 option = (do_keepalive ? 1 : 0);
535 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
536 (char *)&option, sizeof(option));
538 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
545 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
546 (char *)&keep_idle, sizeof(keep_idle));
548 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
552 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
553 (char *)&keep_intvl, sizeof(keep_intvl));
555 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
559 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
560 (char *)&keep_count, sizeof(keep_count));
562 CERROR ("Can't set TCP_KEEPCNT: %d\n", rc);
570 ksocknal_lib_push_conn(struct ksock_conn *conn)
578 rc = ksocknal_connsock_addref(conn);
579 if (rc != 0) /* being shut down */
582 sk = conn->ksnc_sock->sk;
586 nonagle = tp->nonagle;
590 rc = kernel_setsockopt(conn->ksnc_sock, SOL_TCP, TCP_NODELAY,
591 (char *)&val, sizeof(val));
595 tp->nonagle = nonagle;
598 ksocknal_connsock_decref(conn);
601 void ksocknal_read_callback(struct ksock_conn *conn);
602 void ksocknal_write_callback(struct ksock_conn *conn);
604 * socket call back in Linux
607 #ifdef HAVE_SK_DATA_READY_ONE_ARG
608 ksocknal_data_ready(struct sock *sk)
610 ksocknal_data_ready(struct sock *sk, int n)
613 struct ksock_conn *conn;
616 /* interleave correctly with closing sockets... */
618 read_lock(&ksocknal_data.ksnd_global_lock);
620 conn = sk->sk_user_data;
621 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
622 LASSERT(sk->sk_data_ready != &ksocknal_data_ready);
623 #ifdef HAVE_SK_DATA_READY_ONE_ARG
624 sk->sk_data_ready(sk);
626 sk->sk_data_ready(sk, n);
629 ksocknal_read_callback(conn);
631 read_unlock(&ksocknal_data.ksnd_global_lock);
637 ksocknal_write_space (struct sock *sk)
639 struct ksock_conn *conn;
643 /* interleave correctly with closing sockets... */
645 read_lock(&ksocknal_data.ksnd_global_lock);
647 conn = sk->sk_user_data;
648 wspace = sk_stream_wspace(sk);
649 min_wpace = sk_stream_min_wspace(sk);
651 CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
652 sk, wspace, min_wpace, conn,
653 (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
654 " ready" : " blocked"),
655 (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
656 " scheduled" : " idle"),
657 (conn == NULL) ? "" : (list_empty(&conn->ksnc_tx_queue) ?
658 " empty" : " queued"));
660 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
661 LASSERT (sk->sk_write_space != &ksocknal_write_space);
662 sk->sk_write_space (sk);
664 read_unlock(&ksocknal_data.ksnd_global_lock);
668 if (wspace >= min_wpace) { /* got enough space */
669 ksocknal_write_callback(conn);
671 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
672 * ENOMEM check in ksocknal_transmit is race-free (think about
675 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
678 read_unlock(&ksocknal_data.ksnd_global_lock);
682 ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn)
684 conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
685 conn->ksnc_saved_write_space = sock->sk->sk_write_space;
689 ksocknal_lib_set_callback(struct socket *sock, struct ksock_conn *conn)
691 sock->sk->sk_user_data = conn;
692 sock->sk->sk_data_ready = ksocknal_data_ready;
693 sock->sk->sk_write_space = ksocknal_write_space;
697 ksocknal_lib_reset_callback(struct socket *sock, struct ksock_conn *conn)
699 /* Remove conn's network callbacks.
700 * NB I _have_ to restore the callback, rather than storing a noop,
701 * since the socket could survive past this module being unloaded!! */
702 sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
703 sock->sk->sk_write_space = conn->ksnc_saved_write_space;
705 /* A callback could be in progress already; they hold a read lock
706 * on ksnd_global_lock (to serialise with me) and NOOP if
707 * sk_user_data is NULL. */
708 sock->sk->sk_user_data = NULL;
714 ksocknal_lib_memory_pressure(struct ksock_conn *conn)
717 struct ksock_sched *sched;
719 sched = conn->ksnc_scheduler;
720 spin_lock_bh(&sched->kss_lock);
722 if (!test_bit(SOCK_NOSPACE, &conn->ksnc_sock->flags) &&
723 !conn->ksnc_tx_ready) {
724 /* SOCK_NOSPACE is set when the socket fills
725 * and cleared in the write_space callback
726 * (which also sets ksnc_tx_ready). If
727 * SOCK_NOSPACE and ksnc_tx_ready are BOTH
728 * zero, I didn't fill the socket and
729 * write_space won't reschedule me, so I
730 * return -ENOMEM to get my caller to retry
735 spin_unlock_bh(&sched->kss_lock);