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 lnet_kiov_t *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 = ksocknal_csum(conn->ksnc_rx_csum,
205 iov[i].iov_base, fragnob);
207 conn->ksnc_msg.ksm_csum = saved_csum;
214 ksocknal_lib_recv_kiov(struct ksock_conn *conn)
216 unsigned int niov = conn->ksnc_rx_nkiov;
217 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
218 struct msghdr msg = {
228 for (nob = i = 0; i < niov; i++)
229 nob += kiov[i].bv_len;
231 LASSERT(nob <= conn->ksnc_rx_nob_wanted);
233 iov_iter_bvec(&msg.msg_iter, READ | ITER_BVEC, kiov, niov, nob);
234 rc = sock_recvmsg(conn->ksnc_sock, &msg, MSG_DONTWAIT);
236 if (conn->ksnc_msg.ksm_csum) {
237 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
240 base = kmap(kiov[i].bv_page) + kiov[i].bv_offset;
241 fragnob = kiov[i].bv_len;
245 conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
248 kunmap(kiov[i].bv_page);
255 ksocknal_lib_csum_tx(struct ksock_tx *tx)
261 LASSERT(tx->tx_iov[0].iov_base == &tx->tx_msg);
262 LASSERT(tx->tx_conn);
263 LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
265 tx->tx_msg.ksm_csum = 0;
267 csum = ksocknal_csum(~0, tx->tx_iov[0].iov_base,
268 tx->tx_iov[0].iov_len);
271 for (i = 0; i < tx->tx_nkiov; i++) {
272 base = kmap(tx->tx_kiov[i].bv_page) +
273 tx->tx_kiov[i].bv_offset;
275 csum = ksocknal_csum(csum, base, tx->tx_kiov[i].bv_len);
277 kunmap(tx->tx_kiov[i].bv_page);
280 for (i = 1; i < tx->tx_niov; i++)
281 csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
282 tx->tx_iov[i].iov_len);
285 if (*ksocknal_tunables.ksnd_inject_csum_error) {
287 *ksocknal_tunables.ksnd_inject_csum_error = 0;
290 tx->tx_msg.ksm_csum = csum;
294 ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem, int *rxmem, int *nagle)
296 struct socket *sock = conn->ksnc_sock;
300 rc = ksocknal_connsock_addref(conn);
302 LASSERT(conn->ksnc_closing);
303 *txmem = *rxmem = *nagle = 0;
307 rc = lnet_sock_getbuf(sock, txmem, rxmem);
309 len = sizeof(*nagle);
310 rc = kernel_getsockopt(sock, SOL_TCP, TCP_NODELAY,
311 (char *)nagle, &len);
314 ksocknal_connsock_decref(conn);
319 *txmem = *rxmem = *nagle = 0;
325 ksocknal_lib_setup_sock(struct socket *sock)
333 struct linger linger;
335 sock->sk->sk_allocation = GFP_NOFS;
338 * Ensure this socket aborts active sends immediately when we close
344 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger,
347 CERROR("Can't set SO_LINGER: %d\n", rc);
352 rc = kernel_setsockopt(sock, SOL_TCP, TCP_LINGER2, (char *)&option,
355 CERROR("Can't set SO_LINGER2: %d\n", rc);
359 if (!*ksocknal_tunables.ksnd_nagle) {
362 rc = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
363 (char *)&option, sizeof(option));
365 CERROR("Can't disable nagle: %d\n", rc);
370 rc = lnet_sock_setbuf(sock, *ksocknal_tunables.ksnd_tx_buffer_size,
371 *ksocknal_tunables.ksnd_rx_buffer_size);
373 CERROR("Can't set buffer tx %d, rx %d buffers: %d\n",
374 *ksocknal_tunables.ksnd_tx_buffer_size,
375 *ksocknal_tunables.ksnd_rx_buffer_size, rc);
379 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
381 /* snapshot tunables */
382 keep_idle = *ksocknal_tunables.ksnd_keepalive_idle;
383 keep_count = *ksocknal_tunables.ksnd_keepalive_count;
384 keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
386 do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
388 option = (do_keepalive ? 1 : 0);
389 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&option,
392 CERROR("Can't set SO_KEEPALIVE: %d\n", rc);
399 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, (char *)&keep_idle,
402 CERROR("Can't set TCP_KEEPIDLE: %d\n", rc);
406 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
407 (char *)&keep_intvl, sizeof(keep_intvl));
409 CERROR("Can't set TCP_KEEPINTVL: %d\n", rc);
413 rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, (char *)&keep_count,
416 CERROR("Can't set TCP_KEEPCNT: %d\n", rc);
424 ksocknal_lib_push_conn(struct ksock_conn *conn)
432 rc = ksocknal_connsock_addref(conn);
433 if (rc) /* being shut down */
436 sk = conn->ksnc_sock->sk;
440 nonagle = tp->nonagle;
444 rc = kernel_setsockopt(conn->ksnc_sock, SOL_TCP, TCP_NODELAY,
445 (char *)&val, sizeof(val));
449 tp->nonagle = nonagle;
452 ksocknal_connsock_decref(conn);
456 * socket call back in Linux
459 ksocknal_data_ready(struct sock *sk)
461 struct ksock_conn *conn;
463 /* interleave correctly with closing sockets... */
465 read_lock(&ksocknal_data.ksnd_global_lock);
467 conn = sk->sk_user_data;
468 if (!conn) { /* raced with ksocknal_terminate_conn */
469 LASSERT(sk->sk_data_ready != &ksocknal_data_ready);
470 sk->sk_data_ready(sk);
472 ksocknal_read_callback(conn);
475 read_unlock(&ksocknal_data.ksnd_global_lock);
479 ksocknal_write_space(struct sock *sk)
481 struct ksock_conn *conn;
485 /* interleave correctly with closing sockets... */
487 read_lock(&ksocknal_data.ksnd_global_lock);
489 conn = sk->sk_user_data;
490 wspace = sk_stream_wspace(sk);
491 min_wpace = sk_stream_min_wspace(sk);
493 CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
494 sk, wspace, min_wpace, conn,
495 !conn ? "" : (conn->ksnc_tx_ready ?
496 " ready" : " blocked"),
497 !conn ? "" : (conn->ksnc_tx_scheduled ?
498 " scheduled" : " idle"),
499 !conn ? "" : (list_empty(&conn->ksnc_tx_queue) ?
500 " empty" : " queued"));
502 if (!conn) { /* raced with ksocknal_terminate_conn */
503 LASSERT(sk->sk_write_space != &ksocknal_write_space);
504 sk->sk_write_space(sk);
506 read_unlock(&ksocknal_data.ksnd_global_lock);
510 if (wspace >= min_wpace) { /* got enough space */
511 ksocknal_write_callback(conn);
514 * Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
515 * ENOMEM check in ksocknal_transmit is race-free (think about
518 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
521 read_unlock(&ksocknal_data.ksnd_global_lock);
525 ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn)
527 conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
528 conn->ksnc_saved_write_space = sock->sk->sk_write_space;
532 ksocknal_lib_set_callback(struct socket *sock, struct ksock_conn *conn)
534 sock->sk->sk_user_data = conn;
535 sock->sk->sk_data_ready = ksocknal_data_ready;
536 sock->sk->sk_write_space = ksocknal_write_space;
540 ksocknal_lib_reset_callback(struct socket *sock, struct ksock_conn *conn)
543 * Remove conn's network callbacks.
544 * NB I _have_ to restore the callback, rather than storing a noop,
545 * since the socket could survive past this module being unloaded!!
547 sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
548 sock->sk->sk_write_space = conn->ksnc_saved_write_space;
551 * A callback could be in progress already; they hold a read lock
552 * on ksnd_global_lock (to serialise with me) and NOOP if
553 * sk_user_data is NULL.
555 sock->sk->sk_user_data = NULL;
559 ksocknal_lib_memory_pressure(struct ksock_conn *conn)
562 struct ksock_sched *sched;
564 sched = conn->ksnc_scheduler;
565 spin_lock_bh(&sched->kss_lock);
567 if (!test_bit(SOCK_NOSPACE, &conn->ksnc_sock->flags) &&
568 !conn->ksnc_tx_ready) {
570 * SOCK_NOSPACE is set when the socket fills
571 * and cleared in the write_space callback
572 * (which also sets ksnc_tx_ready). If
573 * SOCK_NOSPACE and ksnc_tx_ready are BOTH
574 * zero, I didn't fill the socket and
575 * write_space won't reschedule me, so I
576 * return -ENOMEM to get my caller to retry
582 spin_unlock_bh(&sched->kss_lock);