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_iov(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 for (nob = i = 0; i < niov; i++) {
102 scratchiov[i] = tx->tx_iov[i];
103 nob += scratchiov[i].iov_len;
106 if (!list_empty(&conn->ksnc_tx_queue) ||
108 msg.msg_flags |= MSG_MORE;
110 rc = kernel_sendmsg(sock, &msg, scratchiov, niov, nob);
116 ksocknal_lib_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx,
117 struct kvec *scratchiov)
119 struct socket *sock = conn->ksnc_sock;
120 lnet_kiov_t *kiov = tx->tx_kiov;
124 /* Not NOOP message */
125 LASSERT(tx->tx_lnetmsg != NULL);
127 /* NB we can't trust socket ops to either consume our iovs
128 * or leave them alone. */
129 if (tx->tx_msg.ksm_zc_cookies[0] != 0) {
130 /* Zero copy is enabled */
131 struct sock *sk = sock->sk;
132 struct page *page = kiov->kiov_page;
133 int offset = kiov->kiov_offset;
134 int fragsize = kiov->kiov_len;
135 int msgflg = MSG_DONTWAIT;
137 CDEBUG(D_NET, "page %p + offset %x for %d\n",
138 page, offset, kiov->kiov_len);
140 if (!list_empty(&conn->ksnc_tx_queue) ||
141 fragsize < tx->tx_resid)
144 if (sk->sk_prot->sendpage != NULL) {
145 rc = sk->sk_prot->sendpage(sk, page,
146 offset, fragsize, msgflg);
148 rc = cfs_tcp_sendpage(sk, page, offset, fragsize,
152 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
154 struct kvec *scratchiov = &scratch;
155 unsigned int niov = 1;
157 #ifdef CONFIG_HIGHMEM
158 #warning "XXX risk of kmap deadlock on multiple frags..."
160 unsigned int niov = tx->tx_nkiov;
162 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
165 for (nob = i = 0; i < niov; i++) {
166 scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
168 nob += scratchiov[i].iov_len = kiov[i].kiov_len;
171 if (!list_empty(&conn->ksnc_tx_queue) ||
173 msg.msg_flags |= MSG_MORE;
175 rc = kernel_sendmsg(sock, &msg, scratchiov, niov, nob);
177 for (i = 0; i < niov; i++)
178 kunmap(kiov[i].kiov_page);
184 ksocknal_lib_eager_ack(struct ksock_conn *conn)
187 struct socket *sock = conn->ksnc_sock;
189 /* Remind the socket to ACK eagerly. If I don't, the socket might
190 * think I'm about to send something it could piggy-back the ACK
191 * on, introducing delay in completing zero-copy sends in my
194 kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
195 (char *)&opt, sizeof(opt));
199 ksocknal_lib_recv_iov(struct ksock_conn *conn, struct kvec *scratchiov)
201 #if SOCKNAL_SINGLE_FRAG_RX
203 struct kvec *scratchiov = &scratch;
204 unsigned int niov = 1;
206 unsigned int niov = conn->ksnc_rx_niov;
208 struct kvec *iov = conn->ksnc_rx_iov;
209 struct msghdr msg = {
219 /* NB we can't trust socket ops to either consume our iovs
220 * or leave them alone. */
223 for (nob = i = 0; i < niov; i++) {
224 scratchiov[i] = iov[i];
225 nob += scratchiov[i].iov_len;
227 LASSERT (nob <= conn->ksnc_rx_nob_wanted);
229 rc = kernel_recvmsg(conn->ksnc_sock, &msg, scratchiov, niov, nob,
233 if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
234 saved_csum = conn->ksnc_msg.ksm_csum;
235 conn->ksnc_msg.ksm_csum = 0;
238 if (saved_csum != 0) {
239 /* accumulate checksum */
240 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
243 fragnob = iov[i].iov_len;
247 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
248 iov[i].iov_base, fragnob);
250 conn->ksnc_msg.ksm_csum = saved_csum;
257 ksocknal_lib_kiov_vunmap(void *addr)
266 ksocknal_lib_kiov_vmap(lnet_kiov_t *kiov, int niov,
267 struct kvec *iov, struct page **pages)
273 if (!*ksocknal_tunables.ksnd_zc_recv || pages == NULL)
276 LASSERT (niov <= LNET_MAX_IOV);
279 niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
282 for (nob = i = 0; i < niov; i++) {
283 if ((kiov[i].kiov_offset != 0 && i > 0) ||
284 (kiov[i].kiov_offset + kiov[i].kiov_len !=
285 PAGE_SIZE && i < niov - 1))
288 pages[i] = kiov[i].kiov_page;
289 nob += kiov[i].kiov_len;
292 addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
296 iov->iov_base = addr + kiov[0].kiov_offset;
303 ksocknal_lib_recv_kiov(struct ksock_conn *conn, struct page **pages,
304 struct kvec *scratchiov)
306 #if SOCKNAL_SINGLE_FRAG_RX || !SOCKNAL_RISK_KMAP_DEADLOCK
308 struct kvec *scratchiov = &scratch;
309 struct page **pages = NULL;
310 unsigned int niov = 1;
312 #ifdef CONFIG_HIGHMEM
313 #warning "XXX risk of kmap deadlock on multiple frags..."
315 unsigned int niov = conn->ksnc_rx_nkiov;
317 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
318 struct msghdr msg = {
330 /* NB we can't trust socket ops to either consume our iovs
331 * or leave them alone. */
332 if ((addr = ksocknal_lib_kiov_vmap(kiov, niov, scratchiov, pages)) != NULL) {
333 nob = scratchiov[0].iov_len;
337 for (nob = i = 0; i < niov; i++) {
338 nob += scratchiov[i].iov_len = kiov[i].kiov_len;
339 scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
345 LASSERT (nob <= conn->ksnc_rx_nob_wanted);
347 rc = kernel_recvmsg(conn->ksnc_sock, &msg, scratchiov, n, nob,
350 if (conn->ksnc_msg.ksm_csum != 0) {
351 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
354 /* Dang! have to kmap again because I have nowhere to stash the
355 * mapped address. But by doing it while the page is still
356 * mapped, the kernel just bumps the map count and returns me
357 * the address it stashed. */
358 base = kmap(kiov[i].kiov_page) + kiov[i].kiov_offset;
359 fragnob = kiov[i].kiov_len;
363 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
366 kunmap(kiov[i].kiov_page);
371 ksocknal_lib_kiov_vunmap(addr);
373 for (i = 0; i < niov; i++)
374 kunmap(kiov[i].kiov_page);
381 ksocknal_lib_csum_tx(struct ksock_tx *tx)
387 LASSERT(tx->tx_iov[0].iov_base == (void *)&tx->tx_msg);
388 LASSERT(tx->tx_conn != NULL);
389 LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
391 tx->tx_msg.ksm_csum = 0;
393 csum = ksocknal_csum(~0, (void *)tx->tx_iov[0].iov_base,
394 tx->tx_iov[0].iov_len);
396 if (tx->tx_kiov != NULL) {
397 for (i = 0; i < tx->tx_nkiov; i++) {
398 base = kmap(tx->tx_kiov[i].kiov_page) +
399 tx->tx_kiov[i].kiov_offset;
401 csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
403 kunmap(tx->tx_kiov[i].kiov_page);
406 for (i = 1; i < tx->tx_niov; i++)
407 csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
408 tx->tx_iov[i].iov_len);
411 if (*ksocknal_tunables.ksnd_inject_csum_error) {
413 *ksocknal_tunables.ksnd_inject_csum_error = 0;
416 tx->tx_msg.ksm_csum = csum;
420 ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem, int *rxmem, int *nagle)
422 struct socket *sock = conn->ksnc_sock;
426 rc = ksocknal_connsock_addref(conn);
428 LASSERT (conn->ksnc_closing);
429 *txmem = *rxmem = *nagle = 0;
433 rc = lnet_sock_getbuf(sock, txmem, rxmem);
435 len = sizeof(*nagle);
436 rc = kernel_getsockopt(sock, SOL_TCP, TCP_NODELAY,
437 (char *)nagle, &len);
440 ksocknal_connsock_decref(conn);
445 *txmem = *rxmem = *nagle = 0;
451 ksocknal_lib_setup_sock (struct socket *sock)
459 struct linger linger;
461 sock->sk->sk_allocation = GFP_NOFS;
463 /* Ensure this socket aborts active sends immediately when we close
469 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
470 (char *)&linger, sizeof(linger));
472 CERROR ("Can't set SO_LINGER: %d\n", rc);
477 rc = kernel_setsockopt(sock, SOL_TCP, TCP_LINGER2,
478 (char *)&option, sizeof(option));
480 CERROR ("Can't set SO_LINGER2: %d\n", rc);
484 if (!*ksocknal_tunables.ksnd_nagle) {
487 rc = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
488 (char *)&option, sizeof(option));
490 CERROR ("Can't disable nagle: %d\n", rc);
495 rc = lnet_sock_setbuf(sock,
496 *ksocknal_tunables.ksnd_tx_buffer_size,
497 *ksocknal_tunables.ksnd_rx_buffer_size);
499 CERROR ("Can't set buffer tx %d, rx %d buffers: %d\n",
500 *ksocknal_tunables.ksnd_tx_buffer_size,
501 *ksocknal_tunables.ksnd_rx_buffer_size, rc);
505 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
506 #ifdef SOCKNAL_BACKOFF
507 if (*ksocknal_tunables.ksnd_backoff_init > 0) {
508 option = *ksocknal_tunables.ksnd_backoff_init;
509 #ifdef SOCKNAL_BACKOFF_MS
513 rc = kernel_setsockopt(sock, SOL_TCP, TCP_BACKOFF_INIT,
514 (char *)&option, sizeof(option));
516 CERROR ("Can't set initial tcp backoff %d: %d\n",
522 if (*ksocknal_tunables.ksnd_backoff_max > 0) {
523 option = *ksocknal_tunables.ksnd_backoff_max;
524 #ifdef SOCKNAL_BACKOFF_MS
528 rc = kernel_setsockopt(sock, SOL_TCP, TCP_BACKOFF_MAX,
529 (char *)&option, sizeof(option));
531 CERROR ("Can't set maximum tcp backoff %d: %d\n",
538 /* snapshot tunables */
539 keep_idle = *ksocknal_tunables.ksnd_keepalive_idle;
540 keep_count = *ksocknal_tunables.ksnd_keepalive_count;
541 keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
543 do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
545 option = (do_keepalive ? 1 : 0);
546 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
547 (char *)&option, sizeof(option));
549 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
556 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
557 (char *)&keep_idle, sizeof(keep_idle));
559 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
563 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
564 (char *)&keep_intvl, sizeof(keep_intvl));
566 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
570 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
571 (char *)&keep_count, sizeof(keep_count));
573 CERROR ("Can't set TCP_KEEPCNT: %d\n", rc);
581 ksocknal_lib_push_conn(struct ksock_conn *conn)
589 rc = ksocknal_connsock_addref(conn);
590 if (rc != 0) /* being shut down */
593 sk = conn->ksnc_sock->sk;
597 nonagle = tp->nonagle;
601 rc = kernel_setsockopt(conn->ksnc_sock, SOL_TCP, TCP_NODELAY,
602 (char *)&val, sizeof(val));
606 tp->nonagle = nonagle;
609 ksocknal_connsock_decref(conn);
612 void ksocknal_read_callback(struct ksock_conn *conn);
613 void ksocknal_write_callback(struct ksock_conn *conn);
615 * socket call back in Linux
618 #ifdef HAVE_SK_DATA_READY_ONE_ARG
619 ksocknal_data_ready(struct sock *sk)
621 ksocknal_data_ready(struct sock *sk, int n)
624 struct ksock_conn *conn;
627 /* interleave correctly with closing sockets... */
629 read_lock(&ksocknal_data.ksnd_global_lock);
631 conn = sk->sk_user_data;
632 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
633 LASSERT(sk->sk_data_ready != &ksocknal_data_ready);
634 #ifdef HAVE_SK_DATA_READY_ONE_ARG
635 sk->sk_data_ready(sk);
637 sk->sk_data_ready(sk, n);
640 ksocknal_read_callback(conn);
642 read_unlock(&ksocknal_data.ksnd_global_lock);
648 ksocknal_write_space (struct sock *sk)
650 struct ksock_conn *conn;
654 /* interleave correctly with closing sockets... */
656 read_lock(&ksocknal_data.ksnd_global_lock);
658 conn = sk->sk_user_data;
659 wspace = sk_stream_wspace(sk);
660 min_wpace = sk_stream_min_wspace(sk);
662 CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
663 sk, wspace, min_wpace, conn,
664 (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
665 " ready" : " blocked"),
666 (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
667 " scheduled" : " idle"),
668 (conn == NULL) ? "" : (list_empty(&conn->ksnc_tx_queue) ?
669 " empty" : " queued"));
671 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
672 LASSERT (sk->sk_write_space != &ksocknal_write_space);
673 sk->sk_write_space (sk);
675 read_unlock(&ksocknal_data.ksnd_global_lock);
679 if (wspace >= min_wpace) { /* got enough space */
680 ksocknal_write_callback(conn);
682 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
683 * ENOMEM check in ksocknal_transmit is race-free (think about
686 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
689 read_unlock(&ksocknal_data.ksnd_global_lock);
693 ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn)
695 conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
696 conn->ksnc_saved_write_space = sock->sk->sk_write_space;
700 ksocknal_lib_set_callback(struct socket *sock, struct ksock_conn *conn)
702 sock->sk->sk_user_data = conn;
703 sock->sk->sk_data_ready = ksocknal_data_ready;
704 sock->sk->sk_write_space = ksocknal_write_space;
709 ksocknal_lib_reset_callback(struct socket *sock, struct ksock_conn *conn)
711 /* Remove conn's network callbacks.
712 * NB I _have_ to restore the callback, rather than storing a noop,
713 * since the socket could survive past this module being unloaded!! */
714 sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
715 sock->sk->sk_write_space = conn->ksnc_saved_write_space;
717 /* A callback could be in progress already; they hold a read lock
718 * on ksnd_global_lock (to serialise with me) and NOOP if
719 * sk_user_data is NULL. */
720 sock->sk->sk_user_data = NULL;
726 ksocknal_lib_memory_pressure(struct ksock_conn *conn)
729 struct ksock_sched *sched;
731 sched = conn->ksnc_scheduler;
732 spin_lock_bh(&sched->kss_lock);
734 if (!test_bit(SOCK_NOSPACE, &conn->ksnc_sock->flags) &&
735 !conn->ksnc_tx_ready) {
736 /* SOCK_NOSPACE is set when the socket fills
737 * and cleared in the write_space callback
738 * (which also sets ksnc_tx_ready). If
739 * SOCK_NOSPACE and ksnc_tx_ready are BOTH
740 * zero, I didn't fill the socket and
741 * write_space won't reschedule me, so I
742 * return -ENOMEM to get my caller to retry
747 spin_unlock_bh(&sched->kss_lock);