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, 2012, 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, 1, &conn->ksnc_ipaddr,
41 /* Didn't need the {get,put}connsock dance to deref ksnc_sock... */
42 LASSERT(!conn->ksnc_closing);
45 CERROR("Error %d getting sock peer IP\n", rc);
49 rc = lnet_sock_getaddr(conn->ksnc_sock, 0, &conn->ksnc_myipaddr, NULL);
51 CERROR("Error %d getting sock local IP\n", rc);
59 ksocknal_lib_zc_capable(struct ksock_conn *conn)
61 int caps = conn->ksnc_sock->sk->sk_route_caps;
63 if (conn->ksnc_proto == &ksocknal_protocol_v1x)
67 * ZC if the socket supports scatter/gather and doesn't need software
70 return ((caps & NETIF_F_SG) && (caps & NETIF_F_CSUM_MASK));
74 ksocknal_lib_send_iov(struct ksock_conn *conn, struct ksock_tx *tx)
76 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
77 struct socket *sock = conn->ksnc_sock;
80 if (*ksocknal_tunables.ksnd_enable_csum && /* checksum enabled */
81 conn->ksnc_proto == &ksocknal_protocol_v2x && /* V2.x connection */
82 tx->tx_nob == tx->tx_resid && /* frist sending */
83 !tx->tx_msg.ksm_csum) /* not checksummed */
84 ksocknal_lib_csum_tx(tx);
86 for (nob = i = 0; i < tx->tx_niov; i++)
87 nob += tx->tx_iov[i].iov_len;
89 if (!list_empty(&conn->ksnc_tx_queue) ||
91 msg.msg_flags |= MSG_MORE;
93 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC,
94 tx->tx_iov, tx->tx_niov, nob);
95 return sock_sendmsg(sock, &msg);
99 ksocknal_lib_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx)
101 struct socket *sock = conn->ksnc_sock;
102 struct bio_vec *kiov = tx->tx_kiov;
106 /* Not NOOP message */
107 LASSERT(tx->tx_lnetmsg);
109 if (tx->tx_msg.ksm_zc_cookies[0]) {
110 /* Zero copy is enabled */
111 struct sock *sk = sock->sk;
112 struct page *page = kiov->bv_page;
113 int offset = kiov->bv_offset;
114 int fragsize = kiov->bv_len;
115 int msgflg = MSG_DONTWAIT;
117 CDEBUG(D_NET, "page %p + offset %x for %d\n",
118 page, offset, kiov->bv_len);
120 if (!list_empty(&conn->ksnc_tx_queue) ||
121 fragsize < tx->tx_resid)
124 if (sk->sk_prot->sendpage) {
125 rc = sk->sk_prot->sendpage(sk, page,
126 offset, fragsize, msgflg);
128 rc = tcp_sendpage(sk, page, offset, fragsize, msgflg);
131 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
134 for (nob = i = 0; i < tx->tx_nkiov; i++)
135 nob += kiov[i].bv_len;
137 if (!list_empty(&conn->ksnc_tx_queue) ||
139 msg.msg_flags |= MSG_MORE;
141 iov_iter_bvec(&msg.msg_iter, WRITE | ITER_BVEC,
142 kiov, tx->tx_nkiov, nob);
143 rc = sock_sendmsg(sock, &msg);
149 ksocknal_lib_eager_ack(struct ksock_conn *conn)
152 struct socket *sock = conn->ksnc_sock;
155 * Remind the socket to ACK eagerly. If I don't, the socket might
156 * think I'm about to send something it could piggy-back the ACK
157 * on, introducing delay in completing zero-copy sends in my
160 kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK, (char *)&opt,
165 ksocknal_lib_recv_iov(struct ksock_conn *conn)
167 unsigned int niov = conn->ksnc_rx_niov;
168 struct kvec *iov = conn->ksnc_rx_iov;
169 struct msghdr msg = {
181 for (nob = i = 0; i < niov; i++)
182 nob += iov[i].iov_len;
184 LASSERT(nob <= conn->ksnc_rx_nob_wanted);
186 iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, iov, niov, nob);
187 rc = sock_recvmsg(conn->ksnc_sock, &msg, MSG_DONTWAIT);
190 if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
191 saved_csum = conn->ksnc_msg.ksm_csum;
192 conn->ksnc_msg.ksm_csum = 0;
196 /* accumulate checksum */
197 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
200 fragnob = iov[i].iov_len;
204 conn->ksnc_rx_csum = crc32_le(conn->ksnc_rx_csum,
208 conn->ksnc_msg.ksm_csum = saved_csum;
215 ksocknal_lib_recv_kiov(struct ksock_conn *conn)
217 unsigned int niov = conn->ksnc_rx_nkiov;
218 struct bio_vec *kiov = conn->ksnc_rx_kiov;
219 struct msghdr msg = {
229 for (nob = i = 0; i < niov; i++)
230 nob += kiov[i].bv_len;
232 LASSERT(nob <= conn->ksnc_rx_nob_wanted);
234 iov_iter_bvec(&msg.msg_iter, READ | ITER_BVEC, kiov, niov, nob);
235 rc = sock_recvmsg(conn->ksnc_sock, &msg, MSG_DONTWAIT);
237 if (conn->ksnc_msg.ksm_csum) {
238 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
241 base = kmap(kiov[i].bv_page) + kiov[i].bv_offset;
242 fragnob = kiov[i].bv_len;
246 conn->ksnc_rx_csum = crc32_le(conn->ksnc_rx_csum,
249 kunmap(kiov[i].bv_page);
256 ksocknal_lib_csum_tx(struct ksock_tx *tx)
262 LASSERT(tx->tx_iov[0].iov_base == &tx->tx_msg);
263 LASSERT(tx->tx_conn);
264 LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
266 tx->tx_msg.ksm_csum = 0;
268 csum = crc32_le(~0, tx->tx_iov[0].iov_base,
269 tx->tx_iov[0].iov_len);
272 for (i = 0; i < tx->tx_nkiov; i++) {
273 base = kmap(tx->tx_kiov[i].bv_page) +
274 tx->tx_kiov[i].bv_offset;
276 csum = crc32_le(csum, base, tx->tx_kiov[i].bv_len);
278 kunmap(tx->tx_kiov[i].bv_page);
281 for (i = 1; i < tx->tx_niov; i++)
282 csum = crc32_le(csum, tx->tx_iov[i].iov_base,
283 tx->tx_iov[i].iov_len);
286 if (*ksocknal_tunables.ksnd_inject_csum_error) {
288 *ksocknal_tunables.ksnd_inject_csum_error = 0;
291 tx->tx_msg.ksm_csum = csum;
295 ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem,
296 int *rxmem, int *nagle)
298 struct socket *sock = conn->ksnc_sock;
302 rc = ksocknal_connsock_addref(conn);
304 LASSERT(conn->ksnc_closing);
305 *txmem = *rxmem = *nagle = 0;
309 rc = lnet_sock_getbuf(sock, txmem, rxmem);
311 len = sizeof(*nagle);
312 rc = kernel_getsockopt(sock, SOL_TCP, TCP_NODELAY,
313 (char *)nagle, &len);
316 ksocknal_connsock_decref(conn);
321 *txmem = *rxmem = *nagle = 0;
327 ksocknal_lib_setup_sock(struct socket *sock)
335 struct linger linger;
337 sock->sk->sk_allocation = GFP_NOFS;
340 * Ensure this socket aborts active sends immediately when we close
346 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger,
349 CERROR("Can't set SO_LINGER: %d\n", rc);
354 rc = kernel_setsockopt(sock, SOL_TCP, TCP_LINGER2, (char *)&option,
357 CERROR("Can't set SO_LINGER2: %d\n", rc);
361 if (!*ksocknal_tunables.ksnd_nagle) {
364 rc = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
365 (char *)&option, sizeof(option));
367 CERROR("Can't disable nagle: %d\n", rc);
372 rc = lnet_sock_setbuf(sock, *ksocknal_tunables.ksnd_tx_buffer_size,
373 *ksocknal_tunables.ksnd_rx_buffer_size);
375 CERROR("Can't set buffer tx %d, rx %d buffers: %d\n",
376 *ksocknal_tunables.ksnd_tx_buffer_size,
377 *ksocknal_tunables.ksnd_rx_buffer_size, rc);
381 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
383 /* snapshot tunables */
384 keep_idle = *ksocknal_tunables.ksnd_keepalive_idle;
385 keep_count = *ksocknal_tunables.ksnd_keepalive_count;
386 keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
388 do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
390 option = (do_keepalive ? 1 : 0);
391 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&option,
394 CERROR("Can't set SO_KEEPALIVE: %d\n", rc);
401 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, (char *)&keep_idle,
404 CERROR("Can't set TCP_KEEPIDLE: %d\n", rc);
408 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
409 (char *)&keep_intvl, sizeof(keep_intvl));
411 CERROR("Can't set TCP_KEEPINTVL: %d\n", rc);
415 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, (char *)&keep_count,
418 CERROR("Can't set TCP_KEEPCNT: %d\n", rc);
426 ksocknal_lib_push_conn(struct ksock_conn *conn)
434 rc = ksocknal_connsock_addref(conn);
435 if (rc) /* being shut down */
438 sk = conn->ksnc_sock->sk;
442 nonagle = tp->nonagle;
446 rc = kernel_setsockopt(conn->ksnc_sock, SOL_TCP, TCP_NODELAY,
447 (char *)&val, sizeof(val));
451 tp->nonagle = nonagle;
454 ksocknal_connsock_decref(conn);
458 * socket call back in Linux
461 ksocknal_data_ready(struct sock *sk)
463 struct ksock_conn *conn;
465 /* interleave correctly with closing sockets... */
467 read_lock(&ksocknal_data.ksnd_global_lock);
469 conn = sk->sk_user_data;
470 if (!conn) { /* raced with ksocknal_terminate_conn */
471 LASSERT(sk->sk_data_ready != &ksocknal_data_ready);
472 sk->sk_data_ready(sk);
474 ksocknal_read_callback(conn);
477 read_unlock(&ksocknal_data.ksnd_global_lock);
481 ksocknal_write_space(struct sock *sk)
483 struct ksock_conn *conn;
487 /* interleave correctly with closing sockets... */
489 read_lock(&ksocknal_data.ksnd_global_lock);
491 conn = sk->sk_user_data;
492 wspace = sk_stream_wspace(sk);
493 min_wpace = sk_stream_min_wspace(sk);
495 CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
496 sk, wspace, min_wpace, conn,
497 !conn ? "" : (conn->ksnc_tx_ready ?
498 " ready" : " blocked"),
499 !conn ? "" : (conn->ksnc_tx_scheduled ?
500 " scheduled" : " idle"),
501 !conn ? "" : (list_empty(&conn->ksnc_tx_queue) ?
502 " empty" : " queued"));
504 if (!conn) { /* raced with ksocknal_terminate_conn */
505 LASSERT(sk->sk_write_space != &ksocknal_write_space);
506 sk->sk_write_space(sk);
508 read_unlock(&ksocknal_data.ksnd_global_lock);
512 if (wspace >= min_wpace) { /* got enough space */
513 ksocknal_write_callback(conn);
516 * Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
517 * ENOMEM check in ksocknal_transmit is race-free (think about
520 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
523 read_unlock(&ksocknal_data.ksnd_global_lock);
527 ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn)
529 conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
530 conn->ksnc_saved_write_space = sock->sk->sk_write_space;
534 ksocknal_lib_set_callback(struct socket *sock, struct ksock_conn *conn)
536 sock->sk->sk_user_data = conn;
537 sock->sk->sk_data_ready = ksocknal_data_ready;
538 sock->sk->sk_write_space = ksocknal_write_space;
542 ksocknal_lib_reset_callback(struct socket *sock, struct ksock_conn *conn)
545 * Remove conn's network callbacks.
546 * NB I _have_ to restore the callback, rather than storing a noop,
547 * since the socket could survive past this module being unloaded!!
549 sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
550 sock->sk->sk_write_space = conn->ksnc_saved_write_space;
553 * A callback could be in progress already; they hold a read lock
554 * on ksnd_global_lock (to serialise with me) and NOOP if
555 * sk_user_data is NULL.
557 sock->sk->sk_user_data = NULL;
561 ksocknal_lib_memory_pressure(struct ksock_conn *conn)
564 struct ksock_sched *sched;
566 sched = conn->ksnc_scheduler;
567 spin_lock_bh(&sched->kss_lock);
569 if (!test_bit(SOCK_NOSPACE, &conn->ksnc_sock->flags) &&
570 !conn->ksnc_tx_ready) {
572 * SOCK_NOSPACE is set when the socket fills
573 * and cleared in the write_space callback
574 * (which also sets ksnc_tx_ready). If
575 * SOCK_NOSPACE and ksnc_tx_ready are BOTH
576 * zero, I didn't fill the socket and
577 * write_space won't reschedule me, so I
578 * return -ENOMEM to get my caller to retry
584 spin_unlock_bh(&sched->kss_lock);