1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/kernel.h>
34#include <linux/slab.h>
35#include <linux/in.h>
36#include <net/tcp.h>
37
38#include "rds.h"
39#include "tcp.h"
40
41
42static DEFINE_SPINLOCK(rds_tcp_tc_list_lock);
43static LIST_HEAD(rds_tcp_tc_list);
44static unsigned int rds_tcp_tc_count;
45
46
47static DEFINE_SPINLOCK(rds_tcp_conn_lock);
48static LIST_HEAD(rds_tcp_conn_list);
49
50static struct kmem_cache *rds_tcp_conn_slab;
51
52#define RDS_TCP_DEFAULT_BUFSIZE (128 * 1024)
53
54
55void rds_tcp_nonagle(struct socket *sock)
56{
57 mm_segment_t oldfs = get_fs();
58 int val = 1;
59
60 set_fs(KERNEL_DS);
61 sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY, (char __user *)&val,
62 sizeof(val));
63 set_fs(oldfs);
64}
65
66void rds_tcp_tune(struct socket *sock)
67{
68 struct sock *sk = sock->sk;
69
70 rds_tcp_nonagle(sock);
71
72
73
74
75
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);
81}
82
83u32 rds_tcp_snd_nxt(struct rds_tcp_connection *tc)
84{
85 return tcp_sk(tc->t_sock->sk)->snd_nxt;
86}
87
88u32 rds_tcp_snd_una(struct rds_tcp_connection *tc)
89{
90 return tcp_sk(tc->t_sock->sk)->snd_una;
91}
92
93void rds_tcp_restore_callbacks(struct socket *sock,
94 struct rds_tcp_connection *tc)
95{
96 rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc);
97 write_lock_bh(&sock->sk->sk_callback_lock);
98
99
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);
104
105 tc->t_sock = NULL;
106
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;
111
112 write_unlock_bh(&sock->sk->sk_callback_lock);
113}
114
115
116
117
118
119
120void rds_tcp_set_callbacks(struct socket *sock, struct rds_connection *conn)
121{
122 struct rds_tcp_connection *tc = conn->c_transport_data;
123
124 rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc);
125 write_lock_bh(&sock->sk->sk_callback_lock);
126
127
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);
132
133
134 if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready)
135 sock->sk->sk_data_ready = sock->sk->sk_user_data;
136
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;
142
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;
147
148 write_unlock_bh(&sock->sk->sk_callback_lock);
149}
150
151static void rds_tcp_tc_info(struct socket *sock, unsigned int len,
152 struct rds_info_iterator *iter,
153 struct rds_info_lengths *lens)
154{
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;
160
161 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
162
163 if (len / sizeof(tsinfo) < rds_tcp_tc_count)
164 goto out;
165
166 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
167
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;
174
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;
180
181 rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
182 }
183
184out:
185 lens->nr = rds_tcp_tc_count;
186 lens->each = sizeof(tsinfo);
187
188 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
189}
190
191static int rds_tcp_laddr_check(__be32 addr)
192{
193 if (inet_addr_type(&init_net, addr) == RTN_LOCAL)
194 return 0;
195 return -EADDRNOTAVAIL;
196}
197
198static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
199{
200 struct rds_tcp_connection *tc;
201
202 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
203 if (!tc)
204 return -ENOMEM;
205
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;
210
211 conn->c_transport_data = tc;
212
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);
216
217 rdsdebug("alloced tc %p\n", conn->c_transport_data);
218 return 0;
219}
220
221static void rds_tcp_conn_free(void *arg)
222{
223 struct rds_tcp_connection *tc = arg;
224 unsigned long flags;
225 rdsdebug("freeing tc %p\n", tc);
226
227 spin_lock_irqsave(&rds_tcp_conn_lock, flags);
228 list_del(&tc->t_tcp_node);
229 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
230
231 kmem_cache_free(rds_tcp_conn_slab, tc);
232}
233
234static void rds_tcp_destroy_conns(void)
235{
236 struct rds_tcp_connection *tc, *_tc;
237 LIST_HEAD(tmp_list);
238
239
240 spin_lock_irq(&rds_tcp_conn_lock);
241 list_splice(&rds_tcp_conn_list, &tmp_list);
242 INIT_LIST_HEAD(&rds_tcp_conn_list);
243 spin_unlock_irq(&rds_tcp_conn_lock);
244
245 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) {
246 if (tc->conn->c_passive)
247 rds_conn_destroy(tc->conn->c_passive);
248 rds_conn_destroy(tc->conn);
249 }
250}
251
252static void rds_tcp_exit(void)
253{
254 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
255 rds_tcp_listen_stop();
256 rds_tcp_destroy_conns();
257 rds_trans_unregister(&rds_tcp_transport);
258 rds_tcp_recv_exit();
259 kmem_cache_destroy(rds_tcp_conn_slab);
260}
261module_exit(rds_tcp_exit);
262
263struct rds_transport rds_tcp_transport = {
264 .laddr_check = rds_tcp_laddr_check,
265 .xmit_prepare = rds_tcp_xmit_prepare,
266 .xmit_complete = rds_tcp_xmit_complete,
267 .xmit = rds_tcp_xmit,
268 .recv = rds_tcp_recv,
269 .conn_alloc = rds_tcp_conn_alloc,
270 .conn_free = rds_tcp_conn_free,
271 .conn_connect = rds_tcp_conn_connect,
272 .conn_shutdown = rds_tcp_conn_shutdown,
273 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
274 .inc_free = rds_tcp_inc_free,
275 .stats_info_copy = rds_tcp_stats_info_copy,
276 .exit = rds_tcp_exit,
277 .t_owner = THIS_MODULE,
278 .t_name = "tcp",
279 .t_type = RDS_TRANS_TCP,
280 .t_prefer_loopback = 1,
281};
282
283static int rds_tcp_init(void)
284{
285 int ret;
286
287 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
288 sizeof(struct rds_tcp_connection),
289 0, 0, NULL);
290 if (!rds_tcp_conn_slab) {
291 ret = -ENOMEM;
292 goto out;
293 }
294
295 ret = rds_tcp_recv_init();
296 if (ret)
297 goto out_slab;
298
299 ret = rds_trans_register(&rds_tcp_transport);
300 if (ret)
301 goto out_recv;
302
303 ret = rds_tcp_listen_init();
304 if (ret)
305 goto out_register;
306
307 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
308
309 goto out;
310
311out_register:
312 rds_trans_unregister(&rds_tcp_transport);
313out_recv:
314 rds_tcp_recv_exit();
315out_slab:
316 kmem_cache_destroy(rds_tcp_conn_slab);
317out:
318 return ret;
319}
320module_init(rds_tcp_init);
321
322MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
323MODULE_DESCRIPTION("RDS: TCP transport");
324MODULE_LICENSE("Dual BSD/GPL");
325
326