Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * Copyright (c) 2006 Oracle.  All rights reserved.
  3 *
  4 * This software is available to you under a choice of one of two
  5 * licenses.  You may choose to be licensed under the terms of the GNU
  6 * General Public License (GPL) Version 2, available from the file
  7 * COPYING in the main directory of this source tree, or the
  8 * OpenIB.org BSD license below:
  9 *
 10 *     Redistribution and use in source and binary forms, with or
 11 *     without modification, are permitted provided that the following
 12 *     conditions are met:
 13 *
 14 *      - Redistributions of source code must retain the above
 15 *        copyright notice, this list of conditions and the following
 16 *        disclaimer.
 17 *
 18 *      - Redistributions in binary form must reproduce the above
 19 *        copyright notice, this list of conditions and the following
 20 *        disclaimer in the documentation and/or other materials
 21 *        provided with the distribution.
 22 *
 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 30 * SOFTWARE.
 31 *
 32 */
 33#include <linux/kernel.h>
 34#include <linux/in.h>
 35#include <net/tcp.h>
 36
 
 37#include "rds.h"
 38#include "tcp.h"
 39
 40static void rds_tcp_cork(struct socket *sock, int val)
 41{
 42	mm_segment_t oldfs;
 43
 44	oldfs = get_fs();
 45	set_fs(KERNEL_DS);
 46	sock->ops->setsockopt(sock, SOL_TCP, TCP_CORK, (char __user *)&val,
 47			      sizeof(val));
 48	set_fs(oldfs);
 49}
 50
 51void rds_tcp_xmit_prepare(struct rds_connection *conn)
 52{
 53	struct rds_tcp_connection *tc = conn->c_transport_data;
 54
 55	rds_tcp_cork(tc->t_sock, 1);
 56}
 57
 58void rds_tcp_xmit_complete(struct rds_connection *conn)
 59{
 60	struct rds_tcp_connection *tc = conn->c_transport_data;
 61
 62	rds_tcp_cork(tc->t_sock, 0);
 63}
 64
 65/* the core send_sem serializes this with other xmit and shutdown */
 66static int rds_tcp_sendmsg(struct socket *sock, void *data, unsigned int len)
 67{
 68	struct kvec vec = {
 69                .iov_base = data,
 70                .iov_len = len,
 
 
 
 71	};
 72        struct msghdr msg = {
 73                .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL,
 74        };
 75
 76	return kernel_sendmsg(sock, &msg, &vec, 1, vec.iov_len);
 77}
 78
 79/* the core send_sem serializes this with other xmit and shutdown */
 80int rds_tcp_xmit(struct rds_connection *conn, struct rds_message *rm,
 81	         unsigned int hdr_off, unsigned int sg, unsigned int off)
 82{
 83	struct rds_tcp_connection *tc = conn->c_transport_data;
 
 84	int done = 0;
 85	int ret = 0;
 86	int more;
 87
 88	if (hdr_off == 0) {
 89		/*
 90		 * m_ack_seq is set to the sequence number of the last byte of
 91		 * header and data.  see rds_tcp_is_acked().
 92		 */
 93		tc->t_last_sent_nxt = rds_tcp_snd_nxt(tc);
 94		rm->m_ack_seq = tc->t_last_sent_nxt +
 95				sizeof(struct rds_header) +
 96				be32_to_cpu(rm->m_inc.i_hdr.h_len) - 1;
 97		smp_mb__before_atomic();
 98		set_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags);
 99		tc->t_last_expected_una = rm->m_ack_seq + 1;
100
 
 
 
101		rdsdebug("rm %p tcp nxt %u ack_seq %llu\n",
102			 rm, rds_tcp_snd_nxt(tc),
103			 (unsigned long long)rm->m_ack_seq);
104	}
105
106	if (hdr_off < sizeof(struct rds_header)) {
107		/* see rds_tcp_write_space() */
108		set_bit(SOCK_NOSPACE, &tc->t_sock->sk->sk_socket->flags);
109
110		ret = rds_tcp_sendmsg(tc->t_sock,
111				      (void *)&rm->m_inc.i_hdr + hdr_off,
112				      sizeof(rm->m_inc.i_hdr) - hdr_off);
113		if (ret < 0)
114			goto out;
115		done += ret;
116		if (hdr_off + done != sizeof(struct rds_header))
117			goto out;
118	}
119
120	more = rm->data.op_nents > 1 ? (MSG_MORE | MSG_SENDPAGE_NOTLAST) : 0;
121	while (sg < rm->data.op_nents) {
122		int flags = MSG_DONTWAIT | MSG_NOSIGNAL | more;
123
124		ret = tc->t_sock->ops->sendpage(tc->t_sock,
125						sg_page(&rm->data.op_sg[sg]),
126						rm->data.op_sg[sg].offset + off,
127						rm->data.op_sg[sg].length - off,
128						flags);
129		rdsdebug("tcp sendpage %p:%u:%u ret %d\n", (void *)sg_page(&rm->data.op_sg[sg]),
130			 rm->data.op_sg[sg].offset + off, rm->data.op_sg[sg].length - off,
131			 ret);
132		if (ret <= 0)
133			break;
134
135		off += ret;
136		done += ret;
137		if (off == rm->data.op_sg[sg].length) {
138			off = 0;
139			sg++;
140		}
141		if (sg == rm->data.op_nents - 1)
142			more = 0;
143	}
144
145out:
146	if (ret <= 0) {
147		/* write_space will hit after EAGAIN, all else fatal */
148		if (ret == -EAGAIN) {
149			rds_tcp_stats_inc(s_tcp_sndbuf_full);
150			ret = 0;
151		} else {
152			printk(KERN_WARNING "RDS/tcp: send to %pI4 "
153			       "returned %d, disconnecting and reconnecting\n",
154			       &conn->c_faddr, ret);
155			rds_conn_drop(conn);
 
 
 
 
 
 
 
156		}
157	}
158	if (done == 0)
159		done = ret;
160	return done;
161}
162
163/*
164 * rm->m_ack_seq is set to the tcp sequence number that corresponds to the
165 * last byte of the message, including the header.  This means that the
166 * entire message has been received if rm->m_ack_seq is "before" the next
167 * unacked byte of the TCP sequence space.  We have to do very careful
168 * wrapping 32bit comparisons here.
169 */
170static int rds_tcp_is_acked(struct rds_message *rm, uint64_t ack)
171{
172	if (!test_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags))
173		return 0;
174	return (__s32)((u32)rm->m_ack_seq - (u32)ack) < 0;
175}
176
177void rds_tcp_write_space(struct sock *sk)
178{
179	void (*write_space)(struct sock *sk);
180	struct rds_connection *conn;
181	struct rds_tcp_connection *tc;
182
183	read_lock(&sk->sk_callback_lock);
184	conn = sk->sk_user_data;
185	if (!conn) {
186		write_space = sk->sk_write_space;
187		goto out;
188	}
189
190	tc = conn->c_transport_data;
191	rdsdebug("write_space for tc %p\n", tc);
192	write_space = tc->t_orig_write_space;
193	rds_tcp_stats_inc(s_tcp_write_space_calls);
194
195	rdsdebug("tcp una %u\n", rds_tcp_snd_una(tc));
196	tc->t_last_seen_una = rds_tcp_snd_una(tc);
197	rds_send_drop_acked(conn, rds_tcp_snd_una(tc), rds_tcp_is_acked);
198
199        if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf)
200		queue_delayed_work(rds_wq, &conn->c_send_w, 0);
201
202out:
203	read_unlock(&sk->sk_callback_lock);
204
205	/*
206	 * write_space is only called when data leaves tcp's send queue if
207	 * SOCK_NOSPACE is set.  We set SOCK_NOSPACE every time we put
208	 * data in tcp's send queue because we use write_space to parse the
209	 * sequence numbers and notice that rds messages have been fully
210	 * received.
211	 *
212	 * tcp's write_space clears SOCK_NOSPACE if the send queue has more
213	 * than a certain amount of space. So we need to set it again *after*
214	 * we call tcp's write_space or else we might only get called on the
215	 * first of a series of incoming tcp acks.
216	 */
217	write_space(sk);
218
219	if (sk->sk_socket)
220		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
221}
v4.10.11
  1/*
  2 * Copyright (c) 2006 Oracle.  All rights reserved.
  3 *
  4 * This software is available to you under a choice of one of two
  5 * licenses.  You may choose to be licensed under the terms of the GNU
  6 * General Public License (GPL) Version 2, available from the file
  7 * COPYING in the main directory of this source tree, or the
  8 * OpenIB.org BSD license below:
  9 *
 10 *     Redistribution and use in source and binary forms, with or
 11 *     without modification, are permitted provided that the following
 12 *     conditions are met:
 13 *
 14 *      - Redistributions of source code must retain the above
 15 *        copyright notice, this list of conditions and the following
 16 *        disclaimer.
 17 *
 18 *      - Redistributions in binary form must reproduce the above
 19 *        copyright notice, this list of conditions and the following
 20 *        disclaimer in the documentation and/or other materials
 21 *        provided with the distribution.
 22 *
 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 30 * SOFTWARE.
 31 *
 32 */
 33#include <linux/kernel.h>
 34#include <linux/in.h>
 35#include <net/tcp.h>
 36
 37#include "rds_single_path.h"
 38#include "rds.h"
 39#include "tcp.h"
 40
 41static void rds_tcp_cork(struct socket *sock, int val)
 42{
 43	mm_segment_t oldfs;
 44
 45	oldfs = get_fs();
 46	set_fs(KERNEL_DS);
 47	sock->ops->setsockopt(sock, SOL_TCP, TCP_CORK, (char __user *)&val,
 48			      sizeof(val));
 49	set_fs(oldfs);
 50}
 51
 52void rds_tcp_xmit_path_prepare(struct rds_conn_path *cp)
 53{
 54	struct rds_tcp_connection *tc = cp->cp_transport_data;
 55
 56	rds_tcp_cork(tc->t_sock, 1);
 57}
 58
 59void rds_tcp_xmit_path_complete(struct rds_conn_path *cp)
 60{
 61	struct rds_tcp_connection *tc = cp->cp_transport_data;
 62
 63	rds_tcp_cork(tc->t_sock, 0);
 64}
 65
 66/* the core send_sem serializes this with other xmit and shutdown */
 67static int rds_tcp_sendmsg(struct socket *sock, void *data, unsigned int len)
 68{
 69	struct kvec vec = {
 70		.iov_base = data,
 71		.iov_len = len,
 72	};
 73	struct msghdr msg = {
 74		.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL,
 75	};
 
 
 
 76
 77	return kernel_sendmsg(sock, &msg, &vec, 1, vec.iov_len);
 78}
 79
 80/* the core send_sem serializes this with other xmit and shutdown */
 81int rds_tcp_xmit(struct rds_connection *conn, struct rds_message *rm,
 82		 unsigned int hdr_off, unsigned int sg, unsigned int off)
 83{
 84	struct rds_conn_path *cp = rm->m_inc.i_conn_path;
 85	struct rds_tcp_connection *tc = cp->cp_transport_data;
 86	int done = 0;
 87	int ret = 0;
 88	int more;
 89
 90	if (hdr_off == 0) {
 91		/*
 92		 * m_ack_seq is set to the sequence number of the last byte of
 93		 * header and data.  see rds_tcp_is_acked().
 94		 */
 95		tc->t_last_sent_nxt = rds_tcp_snd_nxt(tc);
 96		rm->m_ack_seq = tc->t_last_sent_nxt +
 97				sizeof(struct rds_header) +
 98				be32_to_cpu(rm->m_inc.i_hdr.h_len) - 1;
 99		smp_mb__before_atomic();
100		set_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags);
101		tc->t_last_expected_una = rm->m_ack_seq + 1;
102
103		if (test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))
104			rm->m_inc.i_hdr.h_flags |= RDS_FLAG_RETRANSMITTED;
105
106		rdsdebug("rm %p tcp nxt %u ack_seq %llu\n",
107			 rm, rds_tcp_snd_nxt(tc),
108			 (unsigned long long)rm->m_ack_seq);
109	}
110
111	if (hdr_off < sizeof(struct rds_header)) {
112		/* see rds_tcp_write_space() */
113		set_bit(SOCK_NOSPACE, &tc->t_sock->sk->sk_socket->flags);
114
115		ret = rds_tcp_sendmsg(tc->t_sock,
116				      (void *)&rm->m_inc.i_hdr + hdr_off,
117				      sizeof(rm->m_inc.i_hdr) - hdr_off);
118		if (ret < 0)
119			goto out;
120		done += ret;
121		if (hdr_off + done != sizeof(struct rds_header))
122			goto out;
123	}
124
125	more = rm->data.op_nents > 1 ? (MSG_MORE | MSG_SENDPAGE_NOTLAST) : 0;
126	while (sg < rm->data.op_nents) {
127		int flags = MSG_DONTWAIT | MSG_NOSIGNAL | more;
128
129		ret = tc->t_sock->ops->sendpage(tc->t_sock,
130						sg_page(&rm->data.op_sg[sg]),
131						rm->data.op_sg[sg].offset + off,
132						rm->data.op_sg[sg].length - off,
133						flags);
134		rdsdebug("tcp sendpage %p:%u:%u ret %d\n", (void *)sg_page(&rm->data.op_sg[sg]),
135			 rm->data.op_sg[sg].offset + off, rm->data.op_sg[sg].length - off,
136			 ret);
137		if (ret <= 0)
138			break;
139
140		off += ret;
141		done += ret;
142		if (off == rm->data.op_sg[sg].length) {
143			off = 0;
144			sg++;
145		}
146		if (sg == rm->data.op_nents - 1)
147			more = 0;
148	}
149
150out:
151	if (ret <= 0) {
152		/* write_space will hit after EAGAIN, all else fatal */
153		if (ret == -EAGAIN) {
154			rds_tcp_stats_inc(s_tcp_sndbuf_full);
155			ret = 0;
156		} else {
157			/* No need to disconnect/reconnect if path_drop
158			 * has already been triggered, because, e.g., of
159			 * an incoming RST.
160			 */
161			if (rds_conn_path_up(cp)) {
162				pr_warn("RDS/tcp: send to %pI4 on cp [%d]"
163					"returned %d, "
164					"disconnecting and reconnecting\n",
165					&conn->c_faddr, cp->cp_index, ret);
166				rds_conn_path_drop(cp);
167			}
168		}
169	}
170	if (done == 0)
171		done = ret;
172	return done;
173}
174
175/*
176 * rm->m_ack_seq is set to the tcp sequence number that corresponds to the
177 * last byte of the message, including the header.  This means that the
178 * entire message has been received if rm->m_ack_seq is "before" the next
179 * unacked byte of the TCP sequence space.  We have to do very careful
180 * wrapping 32bit comparisons here.
181 */
182static int rds_tcp_is_acked(struct rds_message *rm, uint64_t ack)
183{
184	if (!test_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags))
185		return 0;
186	return (__s32)((u32)rm->m_ack_seq - (u32)ack) < 0;
187}
188
189void rds_tcp_write_space(struct sock *sk)
190{
191	void (*write_space)(struct sock *sk);
192	struct rds_conn_path *cp;
193	struct rds_tcp_connection *tc;
194
195	read_lock_bh(&sk->sk_callback_lock);
196	cp = sk->sk_user_data;
197	if (!cp) {
198		write_space = sk->sk_write_space;
199		goto out;
200	}
201
202	tc = cp->cp_transport_data;
203	rdsdebug("write_space for tc %p\n", tc);
204	write_space = tc->t_orig_write_space;
205	rds_tcp_stats_inc(s_tcp_write_space_calls);
206
207	rdsdebug("tcp una %u\n", rds_tcp_snd_una(tc));
208	tc->t_last_seen_una = rds_tcp_snd_una(tc);
209	rds_send_path_drop_acked(cp, rds_tcp_snd_una(tc), rds_tcp_is_acked);
210
211	if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf)
212		queue_delayed_work(rds_wq, &cp->cp_send_w, 0);
213
214out:
215	read_unlock_bh(&sk->sk_callback_lock);
216
217	/*
218	 * write_space is only called when data leaves tcp's send queue if
219	 * SOCK_NOSPACE is set.  We set SOCK_NOSPACE every time we put
220	 * data in tcp's send queue because we use write_space to parse the
221	 * sequence numbers and notice that rds messages have been fully
222	 * received.
223	 *
224	 * tcp's write_space clears SOCK_NOSPACE if the send queue has more
225	 * than a certain amount of space. So we need to set it again *after*
226	 * we call tcp's write_space or else we might only get called on the
227	 * first of a series of incoming tcp acks.
228	 */
229	write_space(sk);
230
231	if (sk->sk_socket)
232		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
233}