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 = tcp_sendpage(sk, page, offset, fragsize, msgflg);
151 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
153 struct kvec *scratchiov = &scratch;
154 unsigned int niov = 1;
156 #ifdef CONFIG_HIGHMEM
157 #warning "XXX risk of kmap deadlock on multiple frags..."
159 unsigned int niov = tx->tx_nkiov;
161 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
164 for (nob = i = 0; i < niov; i++) {
165 scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
167 nob += scratchiov[i].iov_len = kiov[i].kiov_len;
170 if (!list_empty(&conn->ksnc_tx_queue) ||
172 msg.msg_flags |= MSG_MORE;
174 rc = kernel_sendmsg(sock, &msg, scratchiov, niov, nob);
176 for (i = 0; i < niov; i++)
177 kunmap(kiov[i].kiov_page);
183 ksocknal_lib_eager_ack(struct ksock_conn *conn)
186 struct socket *sock = conn->ksnc_sock;
188 /* Remind the socket to ACK eagerly. If I don't, the socket might
189 * think I'm about to send something it could piggy-back the ACK
190 * on, introducing delay in completing zero-copy sends in my
193 kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
194 (char *)&opt, sizeof(opt));
198 ksocknal_lib_recv_iov(struct ksock_conn *conn, struct kvec *scratchiov)
200 #if SOCKNAL_SINGLE_FRAG_RX
202 struct kvec *scratchiov = &scratch;
203 unsigned int niov = 1;
205 unsigned int niov = conn->ksnc_rx_niov;
207 struct kvec *iov = conn->ksnc_rx_iov;
208 struct msghdr msg = {
218 /* NB we can't trust socket ops to either consume our iovs
219 * or leave them alone. */
222 for (nob = i = 0; i < niov; i++) {
223 scratchiov[i] = iov[i];
224 nob += scratchiov[i].iov_len;
226 LASSERT (nob <= conn->ksnc_rx_nob_wanted);
228 rc = kernel_recvmsg(conn->ksnc_sock, &msg, scratchiov, niov, nob,
232 if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
233 saved_csum = conn->ksnc_msg.ksm_csum;
234 conn->ksnc_msg.ksm_csum = 0;
237 if (saved_csum != 0) {
238 /* accumulate checksum */
239 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
242 fragnob = iov[i].iov_len;
246 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
247 iov[i].iov_base, fragnob);
249 conn->ksnc_msg.ksm_csum = saved_csum;
256 ksocknal_lib_kiov_vunmap(void *addr)
265 ksocknal_lib_kiov_vmap(lnet_kiov_t *kiov, int niov,
266 struct kvec *iov, struct page **pages)
272 if (!*ksocknal_tunables.ksnd_zc_recv || pages == NULL)
275 LASSERT (niov <= LNET_MAX_IOV);
278 niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
281 for (nob = i = 0; i < niov; i++) {
282 if ((kiov[i].kiov_offset != 0 && i > 0) ||
283 (kiov[i].kiov_offset + kiov[i].kiov_len !=
284 PAGE_SIZE && i < niov - 1))
287 pages[i] = kiov[i].kiov_page;
288 nob += kiov[i].kiov_len;
291 addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
295 iov->iov_base = addr + kiov[0].kiov_offset;
302 ksocknal_lib_recv_kiov(struct ksock_conn *conn, struct page **pages,
303 struct kvec *scratchiov)
305 #if SOCKNAL_SINGLE_FRAG_RX || !SOCKNAL_RISK_KMAP_DEADLOCK
307 struct kvec *scratchiov = &scratch;
308 struct page **pages = NULL;
309 unsigned int niov = 1;
311 #ifdef CONFIG_HIGHMEM
312 #warning "XXX risk of kmap deadlock on multiple frags..."
314 unsigned int niov = conn->ksnc_rx_nkiov;
316 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
317 struct msghdr msg = {
329 /* NB we can't trust socket ops to either consume our iovs
330 * or leave them alone. */
331 if ((addr = ksocknal_lib_kiov_vmap(kiov, niov, scratchiov, pages)) != NULL) {
332 nob = scratchiov[0].iov_len;
336 for (nob = i = 0; i < niov; i++) {
337 nob += scratchiov[i].iov_len = kiov[i].kiov_len;
338 scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
344 LASSERT (nob <= conn->ksnc_rx_nob_wanted);
346 rc = kernel_recvmsg(conn->ksnc_sock, &msg, scratchiov, n, nob,
349 if (conn->ksnc_msg.ksm_csum != 0) {
350 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
353 /* Dang! have to kmap again because I have nowhere to stash the
354 * mapped address. But by doing it while the page is still
355 * mapped, the kernel just bumps the map count and returns me
356 * the address it stashed. */
357 base = kmap(kiov[i].kiov_page) + kiov[i].kiov_offset;
358 fragnob = kiov[i].kiov_len;
362 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
365 kunmap(kiov[i].kiov_page);
370 ksocknal_lib_kiov_vunmap(addr);
372 for (i = 0; i < niov; i++)
373 kunmap(kiov[i].kiov_page);
380 ksocknal_lib_csum_tx(struct ksock_tx *tx)
386 LASSERT(tx->tx_iov[0].iov_base == (void *)&tx->tx_msg);
387 LASSERT(tx->tx_conn != NULL);
388 LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
390 tx->tx_msg.ksm_csum = 0;
392 csum = ksocknal_csum(~0, (void *)tx->tx_iov[0].iov_base,
393 tx->tx_iov[0].iov_len);
395 if (tx->tx_kiov != NULL) {
396 for (i = 0; i < tx->tx_nkiov; i++) {
397 base = kmap(tx->tx_kiov[i].kiov_page) +
398 tx->tx_kiov[i].kiov_offset;
400 csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
402 kunmap(tx->tx_kiov[i].kiov_page);
405 for (i = 1; i < tx->tx_niov; i++)
406 csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
407 tx->tx_iov[i].iov_len);
410 if (*ksocknal_tunables.ksnd_inject_csum_error) {
412 *ksocknal_tunables.ksnd_inject_csum_error = 0;
415 tx->tx_msg.ksm_csum = csum;
419 ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem, int *rxmem, int *nagle)
421 struct socket *sock = conn->ksnc_sock;
425 rc = ksocknal_connsock_addref(conn);
427 LASSERT (conn->ksnc_closing);
428 *txmem = *rxmem = *nagle = 0;
432 rc = lnet_sock_getbuf(sock, txmem, rxmem);
434 len = sizeof(*nagle);
435 rc = kernel_getsockopt(sock, SOL_TCP, TCP_NODELAY,
436 (char *)nagle, &len);
439 ksocknal_connsock_decref(conn);
444 *txmem = *rxmem = *nagle = 0;
450 ksocknal_lib_setup_sock (struct socket *sock)
458 struct linger linger;
460 sock->sk->sk_allocation = GFP_NOFS;
462 /* Ensure this socket aborts active sends immediately when we close
468 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
469 (char *)&linger, sizeof(linger));
471 CERROR ("Can't set SO_LINGER: %d\n", rc);
476 rc = kernel_setsockopt(sock, SOL_TCP, TCP_LINGER2,
477 (char *)&option, sizeof(option));
479 CERROR ("Can't set SO_LINGER2: %d\n", rc);
483 if (!*ksocknal_tunables.ksnd_nagle) {
486 rc = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
487 (char *)&option, sizeof(option));
489 CERROR ("Can't disable nagle: %d\n", rc);
494 rc = lnet_sock_setbuf(sock,
495 *ksocknal_tunables.ksnd_tx_buffer_size,
496 *ksocknal_tunables.ksnd_rx_buffer_size);
498 CERROR ("Can't set buffer tx %d, rx %d buffers: %d\n",
499 *ksocknal_tunables.ksnd_tx_buffer_size,
500 *ksocknal_tunables.ksnd_rx_buffer_size, rc);
504 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
505 #ifdef SOCKNAL_BACKOFF
506 if (*ksocknal_tunables.ksnd_backoff_init > 0) {
507 option = *ksocknal_tunables.ksnd_backoff_init;
508 #ifdef SOCKNAL_BACKOFF_MS
512 rc = kernel_setsockopt(sock, SOL_TCP, TCP_BACKOFF_INIT,
513 (char *)&option, sizeof(option));
515 CERROR ("Can't set initial tcp backoff %d: %d\n",
521 if (*ksocknal_tunables.ksnd_backoff_max > 0) {
522 option = *ksocknal_tunables.ksnd_backoff_max;
523 #ifdef SOCKNAL_BACKOFF_MS
527 rc = kernel_setsockopt(sock, SOL_TCP, TCP_BACKOFF_MAX,
528 (char *)&option, sizeof(option));
530 CERROR ("Can't set maximum tcp backoff %d: %d\n",
537 /* snapshot tunables */
538 keep_idle = *ksocknal_tunables.ksnd_keepalive_idle;
539 keep_count = *ksocknal_tunables.ksnd_keepalive_count;
540 keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
542 do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
544 option = (do_keepalive ? 1 : 0);
545 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
546 (char *)&option, sizeof(option));
548 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
555 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
556 (char *)&keep_idle, sizeof(keep_idle));
558 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
562 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
563 (char *)&keep_intvl, sizeof(keep_intvl));
565 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
569 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
570 (char *)&keep_count, sizeof(keep_count));
572 CERROR ("Can't set TCP_KEEPCNT: %d\n", rc);
580 ksocknal_lib_push_conn(struct ksock_conn *conn)
588 rc = ksocknal_connsock_addref(conn);
589 if (rc != 0) /* being shut down */
592 sk = conn->ksnc_sock->sk;
596 nonagle = tp->nonagle;
600 rc = kernel_setsockopt(conn->ksnc_sock, SOL_TCP, TCP_NODELAY,
601 (char *)&val, sizeof(val));
605 tp->nonagle = nonagle;
608 ksocknal_connsock_decref(conn);
611 void ksocknal_read_callback(struct ksock_conn *conn);
612 void ksocknal_write_callback(struct ksock_conn *conn);
614 * socket call back in Linux
617 #ifdef HAVE_SK_DATA_READY_ONE_ARG
618 ksocknal_data_ready(struct sock *sk)
620 ksocknal_data_ready(struct sock *sk, int n)
623 struct ksock_conn *conn;
626 /* interleave correctly with closing sockets... */
628 read_lock(&ksocknal_data.ksnd_global_lock);
630 conn = sk->sk_user_data;
631 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
632 LASSERT(sk->sk_data_ready != &ksocknal_data_ready);
633 #ifdef HAVE_SK_DATA_READY_ONE_ARG
634 sk->sk_data_ready(sk);
636 sk->sk_data_ready(sk, n);
639 ksocknal_read_callback(conn);
641 read_unlock(&ksocknal_data.ksnd_global_lock);
647 ksocknal_write_space (struct sock *sk)
649 struct ksock_conn *conn;
653 /* interleave correctly with closing sockets... */
655 read_lock(&ksocknal_data.ksnd_global_lock);
657 conn = sk->sk_user_data;
658 wspace = sk_stream_wspace(sk);
659 min_wpace = sk_stream_min_wspace(sk);
661 CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
662 sk, wspace, min_wpace, conn,
663 (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
664 " ready" : " blocked"),
665 (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
666 " scheduled" : " idle"),
667 (conn == NULL) ? "" : (list_empty(&conn->ksnc_tx_queue) ?
668 " empty" : " queued"));
670 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
671 LASSERT (sk->sk_write_space != &ksocknal_write_space);
672 sk->sk_write_space (sk);
674 read_unlock(&ksocknal_data.ksnd_global_lock);
678 if (wspace >= min_wpace) { /* got enough space */
679 ksocknal_write_callback(conn);
681 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
682 * ENOMEM check in ksocknal_transmit is race-free (think about
685 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
688 read_unlock(&ksocknal_data.ksnd_global_lock);
692 ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn)
694 conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
695 conn->ksnc_saved_write_space = sock->sk->sk_write_space;
699 ksocknal_lib_set_callback(struct socket *sock, struct ksock_conn *conn)
701 sock->sk->sk_user_data = conn;
702 sock->sk->sk_data_ready = ksocknal_data_ready;
703 sock->sk->sk_write_space = ksocknal_write_space;
707 ksocknal_lib_reset_callback(struct socket *sock, struct ksock_conn *conn)
709 /* Remove conn's network callbacks.
710 * NB I _have_ to restore the callback, rather than storing a noop,
711 * since the socket could survive past this module being unloaded!! */
712 sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
713 sock->sk->sk_write_space = conn->ksnc_saved_write_space;
715 /* A callback could be in progress already; they hold a read lock
716 * on ksnd_global_lock (to serialise with me) and NOOP if
717 * sk_user_data is NULL. */
718 sock->sk->sk_user_data = NULL;
724 ksocknal_lib_memory_pressure(struct ksock_conn *conn)
727 struct ksock_sched *sched;
729 sched = conn->ksnc_scheduler;
730 spin_lock_bh(&sched->kss_lock);
732 if (!test_bit(SOCK_NOSPACE, &conn->ksnc_sock->flags) &&
733 !conn->ksnc_tx_ready) {
734 /* SOCK_NOSPACE is set when the socket fills
735 * and cleared in the write_space callback
736 * (which also sets ksnc_tx_ready). If
737 * SOCK_NOSPACE and ksnc_tx_ready are BOTH
738 * zero, I didn't fill the socket and
739 * write_space won't reschedule me, so I
740 * return -ENOMEM to get my caller to retry
745 spin_unlock_bh(&sched->kss_lock);