writeback: split writeback_inodes_wb
[linux-2.6/next.git] / net / rds / tcp.c
blobbabf4577ff7d3f06835e073e48733edeceb97db3
1 /*
2 * Copyright (c) 2006 Oracle. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/in.h>
36 #include <net/tcp.h>
38 #include "rds.h"
39 #include "tcp.h"
41 /* only for info exporting */
42 static DEFINE_SPINLOCK(rds_tcp_tc_list_lock);
43 static LIST_HEAD(rds_tcp_tc_list);
44 unsigned int rds_tcp_tc_count;
46 /* Track rds_tcp_connection structs so they can be cleaned up */
47 static DEFINE_SPINLOCK(rds_tcp_conn_lock);
48 static LIST_HEAD(rds_tcp_conn_list);
50 static struct kmem_cache *rds_tcp_conn_slab;
52 #define RDS_TCP_DEFAULT_BUFSIZE (128 * 1024)
54 /* doing it this way avoids calling tcp_sk() */
55 void rds_tcp_nonagle(struct socket *sock)
57 mm_segment_t oldfs = get_fs();
58 int val = 1;
60 set_fs(KERNEL_DS);
61 sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY, (char __user *)&val,
62 sizeof(val));
63 set_fs(oldfs);
66 void rds_tcp_tune(struct socket *sock)
68 struct sock *sk = sock->sk;
70 rds_tcp_nonagle(sock);
73 * We're trying to saturate gigabit with the default,
74 * see svc_sock_setbufsize().
76 lock_sock(sk);
77 sk->sk_sndbuf = RDS_TCP_DEFAULT_BUFSIZE;
78 sk->sk_rcvbuf = RDS_TCP_DEFAULT_BUFSIZE;
79 sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
80 release_sock(sk);
83 u32 rds_tcp_snd_nxt(struct rds_tcp_connection *tc)
85 return tcp_sk(tc->t_sock->sk)->snd_nxt;
88 u32 rds_tcp_snd_una(struct rds_tcp_connection *tc)
90 return tcp_sk(tc->t_sock->sk)->snd_una;
93 void rds_tcp_restore_callbacks(struct socket *sock,
94 struct rds_tcp_connection *tc)
96 rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc);
97 write_lock_bh(&sock->sk->sk_callback_lock);
99 /* done under the callback_lock to serialize with write_space */
100 spin_lock(&rds_tcp_tc_list_lock);
101 list_del_init(&tc->t_list_item);
102 rds_tcp_tc_count--;
103 spin_unlock(&rds_tcp_tc_list_lock);
105 tc->t_sock = NULL;
107 sock->sk->sk_write_space = tc->t_orig_write_space;
108 sock->sk->sk_data_ready = tc->t_orig_data_ready;
109 sock->sk->sk_state_change = tc->t_orig_state_change;
110 sock->sk->sk_user_data = NULL;
112 write_unlock_bh(&sock->sk->sk_callback_lock);
116 * This is the only path that sets tc->t_sock. Send and receive trust that
117 * it is set. The RDS_CONN_CONNECTED bit protects those paths from being
118 * called while it isn't set.
120 void rds_tcp_set_callbacks(struct socket *sock, struct rds_connection *conn)
122 struct rds_tcp_connection *tc = conn->c_transport_data;
124 rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc);
125 write_lock_bh(&sock->sk->sk_callback_lock);
127 /* done under the callback_lock to serialize with write_space */
128 spin_lock(&rds_tcp_tc_list_lock);
129 list_add_tail(&tc->t_list_item, &rds_tcp_tc_list);
130 rds_tcp_tc_count++;
131 spin_unlock(&rds_tcp_tc_list_lock);
133 /* accepted sockets need our listen data ready undone */
134 if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready)
135 sock->sk->sk_data_ready = sock->sk->sk_user_data;
137 tc->t_sock = sock;
138 tc->conn = conn;
139 tc->t_orig_data_ready = sock->sk->sk_data_ready;
140 tc->t_orig_write_space = sock->sk->sk_write_space;
141 tc->t_orig_state_change = sock->sk->sk_state_change;
143 sock->sk->sk_user_data = conn;
144 sock->sk->sk_data_ready = rds_tcp_data_ready;
145 sock->sk->sk_write_space = rds_tcp_write_space;
146 sock->sk->sk_state_change = rds_tcp_state_change;
148 write_unlock_bh(&sock->sk->sk_callback_lock);
151 static void rds_tcp_tc_info(struct socket *sock, unsigned int len,
152 struct rds_info_iterator *iter,
153 struct rds_info_lengths *lens)
155 struct rds_info_tcp_socket tsinfo;
156 struct rds_tcp_connection *tc;
157 unsigned long flags;
158 struct sockaddr_in sin;
159 int sinlen;
161 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
163 if (len / sizeof(tsinfo) < rds_tcp_tc_count)
164 goto out;
166 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
168 sock->ops->getname(sock, (struct sockaddr *)&sin, &sinlen, 0);
169 tsinfo.local_addr = sin.sin_addr.s_addr;
170 tsinfo.local_port = sin.sin_port;
171 sock->ops->getname(sock, (struct sockaddr *)&sin, &sinlen, 1);
172 tsinfo.peer_addr = sin.sin_addr.s_addr;
173 tsinfo.peer_port = sin.sin_port;
175 tsinfo.hdr_rem = tc->t_tinc_hdr_rem;
176 tsinfo.data_rem = tc->t_tinc_data_rem;
177 tsinfo.last_sent_nxt = tc->t_last_sent_nxt;
178 tsinfo.last_expected_una = tc->t_last_expected_una;
179 tsinfo.last_seen_una = tc->t_last_seen_una;
181 rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
184 out:
185 lens->nr = rds_tcp_tc_count;
186 lens->each = sizeof(tsinfo);
188 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
191 static int rds_tcp_laddr_check(__be32 addr)
193 if (inet_addr_type(&init_net, addr) == RTN_LOCAL)
194 return 0;
195 return -EADDRNOTAVAIL;
198 static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
200 struct rds_tcp_connection *tc;
202 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
203 if (tc == NULL)
204 return -ENOMEM;
206 tc->t_sock = NULL;
207 tc->t_tinc = NULL;
208 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
209 tc->t_tinc_data_rem = 0;
211 conn->c_transport_data = tc;
213 spin_lock_irq(&rds_tcp_conn_lock);
214 list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
215 spin_unlock_irq(&rds_tcp_conn_lock);
217 rdsdebug("alloced tc %p\n", conn->c_transport_data);
218 return 0;
221 static void rds_tcp_conn_free(void *arg)
223 struct rds_tcp_connection *tc = arg;
224 rdsdebug("freeing tc %p\n", tc);
225 kmem_cache_free(rds_tcp_conn_slab, tc);
228 static void rds_tcp_destroy_conns(void)
230 struct rds_tcp_connection *tc, *_tc;
231 LIST_HEAD(tmp_list);
233 /* avoid calling conn_destroy with irqs off */
234 spin_lock_irq(&rds_tcp_conn_lock);
235 list_splice(&rds_tcp_conn_list, &tmp_list);
236 INIT_LIST_HEAD(&rds_tcp_conn_list);
237 spin_unlock_irq(&rds_tcp_conn_lock);
239 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) {
240 if (tc->conn->c_passive)
241 rds_conn_destroy(tc->conn->c_passive);
242 rds_conn_destroy(tc->conn);
246 void rds_tcp_exit(void)
248 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
249 rds_tcp_listen_stop();
250 rds_tcp_destroy_conns();
251 rds_trans_unregister(&rds_tcp_transport);
252 rds_tcp_recv_exit();
253 kmem_cache_destroy(rds_tcp_conn_slab);
255 module_exit(rds_tcp_exit);
257 struct rds_transport rds_tcp_transport = {
258 .laddr_check = rds_tcp_laddr_check,
259 .xmit_prepare = rds_tcp_xmit_prepare,
260 .xmit_complete = rds_tcp_xmit_complete,
261 .xmit_cong_map = rds_tcp_xmit_cong_map,
262 .xmit = rds_tcp_xmit,
263 .recv = rds_tcp_recv,
264 .conn_alloc = rds_tcp_conn_alloc,
265 .conn_free = rds_tcp_conn_free,
266 .conn_connect = rds_tcp_conn_connect,
267 .conn_shutdown = rds_tcp_conn_shutdown,
268 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
269 .inc_purge = rds_tcp_inc_purge,
270 .inc_free = rds_tcp_inc_free,
271 .stats_info_copy = rds_tcp_stats_info_copy,
272 .exit = rds_tcp_exit,
273 .t_owner = THIS_MODULE,
274 .t_name = "tcp",
275 .t_type = RDS_TRANS_TCP,
276 .t_prefer_loopback = 1,
279 int __init rds_tcp_init(void)
281 int ret;
283 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
284 sizeof(struct rds_tcp_connection),
285 0, 0, NULL);
286 if (rds_tcp_conn_slab == NULL) {
287 ret = -ENOMEM;
288 goto out;
291 ret = rds_tcp_recv_init();
292 if (ret)
293 goto out_slab;
295 ret = rds_trans_register(&rds_tcp_transport);
296 if (ret)
297 goto out_recv;
299 ret = rds_tcp_listen_init();
300 if (ret)
301 goto out_register;
303 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
305 goto out;
307 out_register:
308 rds_trans_unregister(&rds_tcp_transport);
309 out_recv:
310 rds_tcp_recv_exit();
311 out_slab:
312 kmem_cache_destroy(rds_tcp_conn_slab);
313 out:
314 return ret;
316 module_init(rds_tcp_init);
318 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
319 MODULE_DESCRIPTION("RDS: TCP transport");
320 MODULE_LICENSE("Dual BSD/GPL");