Commit | Line | Data |
---|---|---|
5c115590 AG |
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> | |
d9b93842 | 34 | #include <linux/moduleparam.h> |
5a0e3ad6 | 35 | #include <linux/gfp.h> |
5c115590 AG |
36 | #include <net/sock.h> |
37 | #include <linux/in.h> | |
38 | #include <linux/list.h> | |
cb0a6056 | 39 | #include <linux/ratelimit.h> |
bc3b2d7f | 40 | #include <linux/export.h> |
4bebdd7a | 41 | #include <linux/sizes.h> |
5c115590 | 42 | |
0cb43965 | 43 | #include "rds_single_path.h" |
5c115590 | 44 | #include "rds.h" |
5c115590 AG |
45 | |
46 | /* When transmitting messages in rds_send_xmit, we need to emerge from | |
47 | * time to time and briefly release the CPU. Otherwise the softlock watchdog | |
48 | * will kick our shin. | |
49 | * Also, it seems fairer to not let one busy connection stall all the | |
50 | * others. | |
51 | * | |
52 | * send_batch_count is the number of times we'll loop in send_xmit. Setting | |
53 | * it to 0 will restore the old behavior (where we looped until we had | |
54 | * drained the queue). | |
55 | */ | |
4bebdd7a | 56 | static int send_batch_count = SZ_1K; |
5c115590 AG |
57 | module_param(send_batch_count, int, 0444); |
58 | MODULE_PARM_DESC(send_batch_count, " batch factor when working the send queue"); | |
59 | ||
ff51bf84 | 60 | static void rds_send_remove_from_sock(struct list_head *messages, int status); |
61 | ||
5c115590 | 62 | /* |
0f4b1c7e ZB |
63 | * Reset the send state. Callers must ensure that this doesn't race with |
64 | * rds_send_xmit(). | |
5c115590 | 65 | */ |
4e9b551c | 66 | static void rds_send_path_reset(struct rds_conn_path *cp) |
5c115590 AG |
67 | { |
68 | struct rds_message *rm, *tmp; | |
69 | unsigned long flags; | |
70 | ||
4e9b551c SV |
71 | if (cp->cp_xmit_rm) { |
72 | rm = cp->cp_xmit_rm; | |
73 | cp->cp_xmit_rm = NULL; | |
5c115590 AG |
74 | /* Tell the user the RDMA op is no longer mapped by the |
75 | * transport. This isn't entirely true (it's flushed out | |
76 | * independently) but as the connection is down, there's | |
77 | * no ongoing RDMA to/from that memory */ | |
7e3f2952 | 78 | rds_message_unmapped(rm); |
7e3f2952 | 79 | rds_message_put(rm); |
5c115590 | 80 | } |
7e3f2952 | 81 | |
4e9b551c SV |
82 | cp->cp_xmit_sg = 0; |
83 | cp->cp_xmit_hdr_off = 0; | |
84 | cp->cp_xmit_data_off = 0; | |
85 | cp->cp_xmit_atomic_sent = 0; | |
86 | cp->cp_xmit_rdma_sent = 0; | |
87 | cp->cp_xmit_data_sent = 0; | |
5c115590 | 88 | |
4e9b551c | 89 | cp->cp_conn->c_map_queued = 0; |
5c115590 | 90 | |
4e9b551c SV |
91 | cp->cp_unacked_packets = rds_sysctl_max_unacked_packets; |
92 | cp->cp_unacked_bytes = rds_sysctl_max_unacked_bytes; | |
5c115590 AG |
93 | |
94 | /* Mark messages as retransmissions, and move them to the send q */ | |
4e9b551c SV |
95 | spin_lock_irqsave(&cp->cp_lock, flags); |
96 | list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) { | |
5c115590 AG |
97 | set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags); |
98 | set_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags); | |
99 | } | |
4e9b551c SV |
100 | list_splice_init(&cp->cp_retrans, &cp->cp_send_queue); |
101 | spin_unlock_irqrestore(&cp->cp_lock, flags); | |
102 | } | |
103 | ||
104 | void rds_send_reset(struct rds_connection *conn) | |
105 | { | |
106 | rds_send_path_reset(&conn->c_path[0]); | |
5c115590 | 107 | } |
0b6f760c | 108 | EXPORT_SYMBOL_GPL(rds_send_reset); |
5c115590 | 109 | |
1f9ecd7e | 110 | static int acquire_in_xmit(struct rds_conn_path *cp) |
0f4b1c7e | 111 | { |
1f9ecd7e | 112 | return test_and_set_bit(RDS_IN_XMIT, &cp->cp_flags) == 0; |
0f4b1c7e ZB |
113 | } |
114 | ||
1f9ecd7e | 115 | static void release_in_xmit(struct rds_conn_path *cp) |
0f4b1c7e | 116 | { |
1f9ecd7e | 117 | clear_bit(RDS_IN_XMIT, &cp->cp_flags); |
4e857c58 | 118 | smp_mb__after_atomic(); |
0f4b1c7e ZB |
119 | /* |
120 | * We don't use wait_on_bit()/wake_up_bit() because our waking is in a | |
121 | * hot path and finding waiters is very rare. We don't want to walk | |
122 | * the system-wide hashed waitqueue buckets in the fast path only to | |
123 | * almost never find waiters. | |
124 | */ | |
1f9ecd7e SV |
125 | if (waitqueue_active(&cp->cp_waitq)) |
126 | wake_up_all(&cp->cp_waitq); | |
0f4b1c7e ZB |
127 | } |
128 | ||
5c115590 | 129 | /* |
25985edc | 130 | * We're making the conscious trade-off here to only send one message |
5c115590 AG |
131 | * down the connection at a time. |
132 | * Pro: | |
133 | * - tx queueing is a simple fifo list | |
134 | * - reassembly is optional and easily done by transports per conn | |
135 | * - no per flow rx lookup at all, straight to the socket | |
136 | * - less per-frag memory and wire overhead | |
137 | * Con: | |
138 | * - queued acks can be delayed behind large messages | |
139 | * Depends: | |
140 | * - small message latency is higher behind queued large messages | |
141 | * - large message latency isn't starved by intervening small sends | |
142 | */ | |
1f9ecd7e | 143 | int rds_send_xmit(struct rds_conn_path *cp) |
5c115590 | 144 | { |
1f9ecd7e | 145 | struct rds_connection *conn = cp->cp_conn; |
5c115590 AG |
146 | struct rds_message *rm; |
147 | unsigned long flags; | |
148 | unsigned int tmp; | |
5c115590 AG |
149 | struct scatterlist *sg; |
150 | int ret = 0; | |
5c115590 | 151 | LIST_HEAD(to_be_dropped); |
443be0e5 SV |
152 | int batch_count; |
153 | unsigned long send_gen = 0; | |
5c115590 | 154 | |
fcc5450c | 155 | restart: |
443be0e5 | 156 | batch_count = 0; |
049ee3f5 | 157 | |
5c115590 AG |
158 | /* |
159 | * sendmsg calls here after having queued its message on the send | |
160 | * queue. We only have one task feeding the connection at a time. If | |
161 | * another thread is already feeding the queue then we back off. This | |
162 | * avoids blocking the caller and trading per-connection data between | |
163 | * caches per message. | |
5c115590 | 164 | */ |
1f9ecd7e | 165 | if (!acquire_in_xmit(cp)) { |
049ee3f5 | 166 | rds_stats_inc(s_send_lock_contention); |
5c115590 AG |
167 | ret = -ENOMEM; |
168 | goto out; | |
169 | } | |
0f4b1c7e | 170 | |
443be0e5 SV |
171 | /* |
172 | * we record the send generation after doing the xmit acquire. | |
173 | * if someone else manages to jump in and do some work, we'll use | |
174 | * this to avoid a goto restart farther down. | |
175 | * | |
176 | * The acquire_in_xmit() check above ensures that only one | |
177 | * caller can increment c_send_gen at any time. | |
178 | */ | |
1f9ecd7e SV |
179 | cp->cp_send_gen++; |
180 | send_gen = cp->cp_send_gen; | |
443be0e5 | 181 | |
0f4b1c7e ZB |
182 | /* |
183 | * rds_conn_shutdown() sets the conn state and then tests RDS_IN_XMIT, | |
184 | * we do the opposite to avoid races. | |
185 | */ | |
1f9ecd7e SV |
186 | if (!rds_conn_path_up(cp)) { |
187 | release_in_xmit(cp); | |
0f4b1c7e ZB |
188 | ret = 0; |
189 | goto out; | |
190 | } | |
5c115590 | 191 | |
1f9ecd7e SV |
192 | if (conn->c_trans->t_mp_capable) { |
193 | if (conn->c_trans->xmit_path_prepare) | |
194 | conn->c_trans->xmit_path_prepare(cp); | |
195 | } else if (conn->c_trans->xmit_prepare) { | |
5c115590 | 196 | conn->c_trans->xmit_prepare(conn); |
1f9ecd7e | 197 | } |
5c115590 AG |
198 | |
199 | /* | |
200 | * spin trying to push headers and data down the connection until | |
5b2366bd | 201 | * the connection doesn't make forward progress. |
5c115590 | 202 | */ |
fcc5450c | 203 | while (1) { |
5c115590 | 204 | |
1f9ecd7e | 205 | rm = cp->cp_xmit_rm; |
5c115590 | 206 | |
5b2366bd AG |
207 | /* |
208 | * If between sending messages, we can send a pending congestion | |
209 | * map update. | |
5c115590 | 210 | */ |
8690bfa1 | 211 | if (!rm && test_and_clear_bit(0, &conn->c_map_queued)) { |
77dd550e AG |
212 | rm = rds_cong_update_alloc(conn); |
213 | if (IS_ERR(rm)) { | |
214 | ret = PTR_ERR(rm); | |
215 | break; | |
5b2366bd | 216 | } |
77dd550e | 217 | rm->data.op_active = 1; |
1f9ecd7e SV |
218 | rm->m_inc.i_conn_path = cp; |
219 | rm->m_inc.i_conn = cp->cp_conn; | |
77dd550e | 220 | |
1f9ecd7e | 221 | cp->cp_xmit_rm = rm; |
5c115590 AG |
222 | } |
223 | ||
224 | /* | |
5b2366bd | 225 | * If not already working on one, grab the next message. |
5c115590 | 226 | * |
1f9ecd7e | 227 | * cp_xmit_rm holds a ref while we're sending this message down |
5c115590 AG |
228 | * the connction. We can use this ref while holding the |
229 | * send_sem.. rds_send_reset() is serialized with it. | |
230 | */ | |
8690bfa1 | 231 | if (!rm) { |
5c115590 AG |
232 | unsigned int len; |
233 | ||
443be0e5 SV |
234 | batch_count++; |
235 | ||
236 | /* we want to process as big a batch as we can, but | |
237 | * we also want to avoid softlockups. If we've been | |
238 | * through a lot of messages, lets back off and see | |
239 | * if anyone else jumps in | |
240 | */ | |
4bebdd7a | 241 | if (batch_count >= send_batch_count) |
443be0e5 SV |
242 | goto over_batch; |
243 | ||
1f9ecd7e | 244 | spin_lock_irqsave(&cp->cp_lock, flags); |
5c115590 | 245 | |
1f9ecd7e SV |
246 | if (!list_empty(&cp->cp_send_queue)) { |
247 | rm = list_entry(cp->cp_send_queue.next, | |
5c115590 AG |
248 | struct rds_message, |
249 | m_conn_item); | |
250 | rds_message_addref(rm); | |
251 | ||
252 | /* | |
253 | * Move the message from the send queue to the retransmit | |
254 | * list right away. | |
255 | */ | |
1f9ecd7e SV |
256 | list_move_tail(&rm->m_conn_item, |
257 | &cp->cp_retrans); | |
5c115590 AG |
258 | } |
259 | ||
1f9ecd7e | 260 | spin_unlock_irqrestore(&cp->cp_lock, flags); |
5c115590 | 261 | |
fcc5450c | 262 | if (!rm) |
5c115590 | 263 | break; |
5c115590 AG |
264 | |
265 | /* Unfortunately, the way Infiniband deals with | |
266 | * RDMA to a bad MR key is by moving the entire | |
267 | * queue pair to error state. We cold possibly | |
268 | * recover from that, but right now we drop the | |
269 | * connection. | |
270 | * Therefore, we never retransmit messages with RDMA ops. | |
271 | */ | |
f8b3aaf2 | 272 | if (rm->rdma.op_active && |
f64f9e71 | 273 | test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags)) { |
1f9ecd7e | 274 | spin_lock_irqsave(&cp->cp_lock, flags); |
5c115590 AG |
275 | if (test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) |
276 | list_move(&rm->m_conn_item, &to_be_dropped); | |
1f9ecd7e | 277 | spin_unlock_irqrestore(&cp->cp_lock, flags); |
5c115590 AG |
278 | continue; |
279 | } | |
280 | ||
281 | /* Require an ACK every once in a while */ | |
282 | len = ntohl(rm->m_inc.i_hdr.h_len); | |
1f9ecd7e SV |
283 | if (cp->cp_unacked_packets == 0 || |
284 | cp->cp_unacked_bytes < len) { | |
5c115590 AG |
285 | __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags); |
286 | ||
1f9ecd7e SV |
287 | cp->cp_unacked_packets = |
288 | rds_sysctl_max_unacked_packets; | |
289 | cp->cp_unacked_bytes = | |
290 | rds_sysctl_max_unacked_bytes; | |
5c115590 AG |
291 | rds_stats_inc(s_send_ack_required); |
292 | } else { | |
1f9ecd7e SV |
293 | cp->cp_unacked_bytes -= len; |
294 | cp->cp_unacked_packets--; | |
5c115590 AG |
295 | } |
296 | ||
1f9ecd7e | 297 | cp->cp_xmit_rm = rm; |
5c115590 AG |
298 | } |
299 | ||
2c3a5f9a | 300 | /* The transport either sends the whole rdma or none of it */ |
1f9ecd7e | 301 | if (rm->rdma.op_active && !cp->cp_xmit_rdma_sent) { |
ff3d7d36 | 302 | rm->m_final_op = &rm->rdma; |
4f73113c | 303 | /* The transport owns the mapped memory for now. |
304 | * You can't unmap it while it's on the send queue | |
305 | */ | |
306 | set_bit(RDS_MSG_MAPPED, &rm->m_flags); | |
2c3a5f9a | 307 | ret = conn->c_trans->xmit_rdma(conn, &rm->rdma); |
4f73113c | 308 | if (ret) { |
309 | clear_bit(RDS_MSG_MAPPED, &rm->m_flags); | |
310 | wake_up_interruptible(&rm->m_flush_wait); | |
15133f6e | 311 | break; |
4f73113c | 312 | } |
1f9ecd7e | 313 | cp->cp_xmit_rdma_sent = 1; |
2c3a5f9a | 314 | |
15133f6e AG |
315 | } |
316 | ||
1f9ecd7e | 317 | if (rm->atomic.op_active && !cp->cp_xmit_atomic_sent) { |
ff3d7d36 | 318 | rm->m_final_op = &rm->atomic; |
4f73113c | 319 | /* The transport owns the mapped memory for now. |
320 | * You can't unmap it while it's on the send queue | |
321 | */ | |
322 | set_bit(RDS_MSG_MAPPED, &rm->m_flags); | |
ff3d7d36 | 323 | ret = conn->c_trans->xmit_atomic(conn, &rm->atomic); |
4f73113c | 324 | if (ret) { |
325 | clear_bit(RDS_MSG_MAPPED, &rm->m_flags); | |
326 | wake_up_interruptible(&rm->m_flush_wait); | |
5c115590 | 327 | break; |
4f73113c | 328 | } |
1f9ecd7e | 329 | cp->cp_xmit_atomic_sent = 1; |
ff3d7d36 | 330 | |
5c115590 AG |
331 | } |
332 | ||
2c3a5f9a AG |
333 | /* |
334 | * A number of cases require an RDS header to be sent | |
335 | * even if there is no data. | |
336 | * We permit 0-byte sends; rds-ping depends on this. | |
337 | * However, if there are exclusively attached silent ops, | |
338 | * we skip the hdr/data send, to enable silent operation. | |
339 | */ | |
340 | if (rm->data.op_nents == 0) { | |
341 | int ops_present; | |
342 | int all_ops_are_silent = 1; | |
343 | ||
344 | ops_present = (rm->atomic.op_active || rm->rdma.op_active); | |
345 | if (rm->atomic.op_active && !rm->atomic.op_silent) | |
346 | all_ops_are_silent = 0; | |
347 | if (rm->rdma.op_active && !rm->rdma.op_silent) | |
348 | all_ops_are_silent = 0; | |
349 | ||
350 | if (ops_present && all_ops_are_silent | |
351 | && !rm->m_rdma_cookie) | |
352 | rm->data.op_active = 0; | |
353 | } | |
354 | ||
1f9ecd7e | 355 | if (rm->data.op_active && !cp->cp_xmit_data_sent) { |
ff3d7d36 | 356 | rm->m_final_op = &rm->data; |
1f9ecd7e | 357 | |
5c115590 | 358 | ret = conn->c_trans->xmit(conn, rm, |
1f9ecd7e SV |
359 | cp->cp_xmit_hdr_off, |
360 | cp->cp_xmit_sg, | |
361 | cp->cp_xmit_data_off); | |
5c115590 AG |
362 | if (ret <= 0) |
363 | break; | |
364 | ||
1f9ecd7e | 365 | if (cp->cp_xmit_hdr_off < sizeof(struct rds_header)) { |
5c115590 AG |
366 | tmp = min_t(int, ret, |
367 | sizeof(struct rds_header) - | |
1f9ecd7e SV |
368 | cp->cp_xmit_hdr_off); |
369 | cp->cp_xmit_hdr_off += tmp; | |
5c115590 AG |
370 | ret -= tmp; |
371 | } | |
372 | ||
1f9ecd7e | 373 | sg = &rm->data.op_sg[cp->cp_xmit_sg]; |
5c115590 AG |
374 | while (ret) { |
375 | tmp = min_t(int, ret, sg->length - | |
1f9ecd7e SV |
376 | cp->cp_xmit_data_off); |
377 | cp->cp_xmit_data_off += tmp; | |
5c115590 | 378 | ret -= tmp; |
1f9ecd7e SV |
379 | if (cp->cp_xmit_data_off == sg->length) { |
380 | cp->cp_xmit_data_off = 0; | |
5c115590 | 381 | sg++; |
1f9ecd7e SV |
382 | cp->cp_xmit_sg++; |
383 | BUG_ON(ret != 0 && cp->cp_xmit_sg == | |
384 | rm->data.op_nents); | |
5c115590 AG |
385 | } |
386 | } | |
5b2366bd | 387 | |
1f9ecd7e SV |
388 | if (cp->cp_xmit_hdr_off == sizeof(struct rds_header) && |
389 | (cp->cp_xmit_sg == rm->data.op_nents)) | |
390 | cp->cp_xmit_data_sent = 1; | |
5b2366bd AG |
391 | } |
392 | ||
393 | /* | |
394 | * A rm will only take multiple times through this loop | |
395 | * if there is a data op. Thus, if the data is sent (or there was | |
396 | * none), then we're done with the rm. | |
397 | */ | |
1f9ecd7e SV |
398 | if (!rm->data.op_active || cp->cp_xmit_data_sent) { |
399 | cp->cp_xmit_rm = NULL; | |
400 | cp->cp_xmit_sg = 0; | |
401 | cp->cp_xmit_hdr_off = 0; | |
402 | cp->cp_xmit_data_off = 0; | |
403 | cp->cp_xmit_rdma_sent = 0; | |
404 | cp->cp_xmit_atomic_sent = 0; | |
405 | cp->cp_xmit_data_sent = 0; | |
5b2366bd AG |
406 | |
407 | rds_message_put(rm); | |
5c115590 AG |
408 | } |
409 | } | |
410 | ||
443be0e5 | 411 | over_batch: |
1f9ecd7e SV |
412 | if (conn->c_trans->t_mp_capable) { |
413 | if (conn->c_trans->xmit_path_complete) | |
414 | conn->c_trans->xmit_path_complete(cp); | |
415 | } else if (conn->c_trans->xmit_complete) { | |
5c115590 | 416 | conn->c_trans->xmit_complete(conn); |
1f9ecd7e SV |
417 | } |
418 | release_in_xmit(cp); | |
5c115590 | 419 | |
2ad8099b AG |
420 | /* Nuke any messages we decided not to retransmit. */ |
421 | if (!list_empty(&to_be_dropped)) { | |
422 | /* irqs on here, so we can put(), unlike above */ | |
423 | list_for_each_entry(rm, &to_be_dropped, m_conn_item) | |
424 | rds_message_put(rm); | |
425 | rds_send_remove_from_sock(&to_be_dropped, RDS_RDMA_DROPPED); | |
426 | } | |
427 | ||
fcc5450c | 428 | /* |
0f4b1c7e ZB |
429 | * Other senders can queue a message after we last test the send queue |
430 | * but before we clear RDS_IN_XMIT. In that case they'd back off and | |
431 | * not try and send their newly queued message. We need to check the | |
432 | * send queue after having cleared RDS_IN_XMIT so that their message | |
433 | * doesn't get stuck on the send queue. | |
fcc5450c AG |
434 | * |
435 | * If the transport cannot continue (i.e ret != 0), then it must | |
436 | * call us when more room is available, such as from the tx | |
437 | * completion handler. | |
443be0e5 SV |
438 | * |
439 | * We have an extra generation check here so that if someone manages | |
440 | * to jump in after our release_in_xmit, we'll see that they have done | |
441 | * some work and we will skip our goto | |
fcc5450c AG |
442 | */ |
443 | if (ret == 0) { | |
9e29db0e | 444 | smp_mb(); |
0c484240 | 445 | if ((test_bit(0, &conn->c_map_queued) || |
1f9ecd7e SV |
446 | !list_empty(&cp->cp_send_queue)) && |
447 | send_gen == cp->cp_send_gen) { | |
049ee3f5 | 448 | rds_stats_inc(s_send_lock_queue_raced); |
4bebdd7a SS |
449 | if (batch_count < send_batch_count) |
450 | goto restart; | |
1f9ecd7e | 451 | queue_delayed_work(rds_wq, &cp->cp_send_w, 1); |
5c115590 | 452 | } |
5c115590 AG |
453 | } |
454 | out: | |
455 | return ret; | |
456 | } | |
0c28c045 | 457 | EXPORT_SYMBOL_GPL(rds_send_xmit); |
5c115590 AG |
458 | |
459 | static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm) | |
460 | { | |
461 | u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len); | |
462 | ||
463 | assert_spin_locked(&rs->rs_lock); | |
464 | ||
465 | BUG_ON(rs->rs_snd_bytes < len); | |
466 | rs->rs_snd_bytes -= len; | |
467 | ||
468 | if (rs->rs_snd_bytes == 0) | |
469 | rds_stats_inc(s_send_queue_empty); | |
470 | } | |
471 | ||
472 | static inline int rds_send_is_acked(struct rds_message *rm, u64 ack, | |
473 | is_acked_func is_acked) | |
474 | { | |
475 | if (is_acked) | |
476 | return is_acked(rm, ack); | |
477 | return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack; | |
478 | } | |
479 | ||
5c115590 AG |
480 | /* |
481 | * This is pretty similar to what happens below in the ACK | |
482 | * handling code - except that we call here as soon as we get | |
483 | * the IB send completion on the RDMA op and the accompanying | |
484 | * message. | |
485 | */ | |
486 | void rds_rdma_send_complete(struct rds_message *rm, int status) | |
487 | { | |
488 | struct rds_sock *rs = NULL; | |
f8b3aaf2 | 489 | struct rm_rdma_op *ro; |
5c115590 | 490 | struct rds_notifier *notifier; |
9de0864c | 491 | unsigned long flags; |
5c115590 | 492 | |
9de0864c | 493 | spin_lock_irqsave(&rm->m_rs_lock, flags); |
5c115590 | 494 | |
f8b3aaf2 | 495 | ro = &rm->rdma; |
f64f9e71 | 496 | if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) && |
f8b3aaf2 AG |
497 | ro->op_active && ro->op_notify && ro->op_notifier) { |
498 | notifier = ro->op_notifier; | |
5c115590 AG |
499 | rs = rm->m_rs; |
500 | sock_hold(rds_rs_to_sk(rs)); | |
501 | ||
502 | notifier->n_status = status; | |
503 | spin_lock(&rs->rs_lock); | |
504 | list_add_tail(¬ifier->n_list, &rs->rs_notify_queue); | |
505 | spin_unlock(&rs->rs_lock); | |
506 | ||
f8b3aaf2 | 507 | ro->op_notifier = NULL; |
5c115590 AG |
508 | } |
509 | ||
9de0864c | 510 | spin_unlock_irqrestore(&rm->m_rs_lock, flags); |
5c115590 AG |
511 | |
512 | if (rs) { | |
513 | rds_wake_sk_sleep(rs); | |
514 | sock_put(rds_rs_to_sk(rs)); | |
515 | } | |
516 | } | |
616b757a | 517 | EXPORT_SYMBOL_GPL(rds_rdma_send_complete); |
5c115590 | 518 | |
15133f6e AG |
519 | /* |
520 | * Just like above, except looks at atomic op | |
521 | */ | |
522 | void rds_atomic_send_complete(struct rds_message *rm, int status) | |
523 | { | |
524 | struct rds_sock *rs = NULL; | |
525 | struct rm_atomic_op *ao; | |
526 | struct rds_notifier *notifier; | |
cf4b7389 | 527 | unsigned long flags; |
15133f6e | 528 | |
cf4b7389 | 529 | spin_lock_irqsave(&rm->m_rs_lock, flags); |
15133f6e AG |
530 | |
531 | ao = &rm->atomic; | |
532 | if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) | |
533 | && ao->op_active && ao->op_notify && ao->op_notifier) { | |
534 | notifier = ao->op_notifier; | |
535 | rs = rm->m_rs; | |
536 | sock_hold(rds_rs_to_sk(rs)); | |
537 | ||
538 | notifier->n_status = status; | |
539 | spin_lock(&rs->rs_lock); | |
540 | list_add_tail(¬ifier->n_list, &rs->rs_notify_queue); | |
541 | spin_unlock(&rs->rs_lock); | |
542 | ||
543 | ao->op_notifier = NULL; | |
544 | } | |
545 | ||
cf4b7389 | 546 | spin_unlock_irqrestore(&rm->m_rs_lock, flags); |
15133f6e AG |
547 | |
548 | if (rs) { | |
549 | rds_wake_sk_sleep(rs); | |
550 | sock_put(rds_rs_to_sk(rs)); | |
551 | } | |
552 | } | |
553 | EXPORT_SYMBOL_GPL(rds_atomic_send_complete); | |
554 | ||
5c115590 AG |
555 | /* |
556 | * This is the same as rds_rdma_send_complete except we | |
557 | * don't do any locking - we have all the ingredients (message, | |
558 | * socket, socket lock) and can just move the notifier. | |
559 | */ | |
560 | static inline void | |
940786eb | 561 | __rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status) |
5c115590 | 562 | { |
f8b3aaf2 | 563 | struct rm_rdma_op *ro; |
940786eb | 564 | struct rm_atomic_op *ao; |
5c115590 | 565 | |
f8b3aaf2 AG |
566 | ro = &rm->rdma; |
567 | if (ro->op_active && ro->op_notify && ro->op_notifier) { | |
568 | ro->op_notifier->n_status = status; | |
569 | list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue); | |
570 | ro->op_notifier = NULL; | |
5c115590 AG |
571 | } |
572 | ||
940786eb AG |
573 | ao = &rm->atomic; |
574 | if (ao->op_active && ao->op_notify && ao->op_notifier) { | |
575 | ao->op_notifier->n_status = status; | |
576 | list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue); | |
577 | ao->op_notifier = NULL; | |
578 | } | |
579 | ||
5c115590 AG |
580 | /* No need to wake the app - caller does this */ |
581 | } | |
582 | ||
5c115590 AG |
583 | /* |
584 | * This removes messages from the socket's list if they're on it. The list | |
585 | * argument must be private to the caller, we must be able to modify it | |
586 | * without locks. The messages must have a reference held for their | |
587 | * position on the list. This function will drop that reference after | |
588 | * removing the messages from the 'messages' list regardless of if it found | |
589 | * the messages on the socket list or not. | |
590 | */ | |
ff51bf84 | 591 | static void rds_send_remove_from_sock(struct list_head *messages, int status) |
5c115590 | 592 | { |
561c7df6 | 593 | unsigned long flags; |
5c115590 AG |
594 | struct rds_sock *rs = NULL; |
595 | struct rds_message *rm; | |
596 | ||
5c115590 | 597 | while (!list_empty(messages)) { |
561c7df6 AG |
598 | int was_on_sock = 0; |
599 | ||
5c115590 AG |
600 | rm = list_entry(messages->next, struct rds_message, |
601 | m_conn_item); | |
602 | list_del_init(&rm->m_conn_item); | |
603 | ||
604 | /* | |
605 | * If we see this flag cleared then we're *sure* that someone | |
606 | * else beat us to removing it from the sock. If we race | |
607 | * with their flag update we'll get the lock and then really | |
608 | * see that the flag has been cleared. | |
609 | * | |
610 | * The message spinlock makes sure nobody clears rm->m_rs | |
611 | * while we're messing with it. It does not prevent the | |
612 | * message from being removed from the socket, though. | |
613 | */ | |
561c7df6 | 614 | spin_lock_irqsave(&rm->m_rs_lock, flags); |
5c115590 AG |
615 | if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) |
616 | goto unlock_and_drop; | |
617 | ||
618 | if (rs != rm->m_rs) { | |
619 | if (rs) { | |
5c115590 AG |
620 | rds_wake_sk_sleep(rs); |
621 | sock_put(rds_rs_to_sk(rs)); | |
622 | } | |
623 | rs = rm->m_rs; | |
593cbb3e HK |
624 | if (rs) |
625 | sock_hold(rds_rs_to_sk(rs)); | |
5c115590 | 626 | } |
593cbb3e HK |
627 | if (!rs) |
628 | goto unlock_and_drop; | |
048c15e6 | 629 | spin_lock(&rs->rs_lock); |
5c115590 AG |
630 | |
631 | if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) { | |
f8b3aaf2 | 632 | struct rm_rdma_op *ro = &rm->rdma; |
5c115590 AG |
633 | struct rds_notifier *notifier; |
634 | ||
635 | list_del_init(&rm->m_sock_item); | |
636 | rds_send_sndbuf_remove(rs, rm); | |
637 | ||
f8b3aaf2 AG |
638 | if (ro->op_active && ro->op_notifier && |
639 | (ro->op_notify || (ro->op_recverr && status))) { | |
640 | notifier = ro->op_notifier; | |
5c115590 AG |
641 | list_add_tail(¬ifier->n_list, |
642 | &rs->rs_notify_queue); | |
643 | if (!notifier->n_status) | |
644 | notifier->n_status = status; | |
f8b3aaf2 | 645 | rm->rdma.op_notifier = NULL; |
5c115590 | 646 | } |
561c7df6 | 647 | was_on_sock = 1; |
5c115590 AG |
648 | rm->m_rs = NULL; |
649 | } | |
048c15e6 | 650 | spin_unlock(&rs->rs_lock); |
5c115590 AG |
651 | |
652 | unlock_and_drop: | |
561c7df6 | 653 | spin_unlock_irqrestore(&rm->m_rs_lock, flags); |
5c115590 | 654 | rds_message_put(rm); |
561c7df6 AG |
655 | if (was_on_sock) |
656 | rds_message_put(rm); | |
5c115590 AG |
657 | } |
658 | ||
659 | if (rs) { | |
5c115590 AG |
660 | rds_wake_sk_sleep(rs); |
661 | sock_put(rds_rs_to_sk(rs)); | |
662 | } | |
5c115590 AG |
663 | } |
664 | ||
665 | /* | |
666 | * Transports call here when they've determined that the receiver queued | |
667 | * messages up to, and including, the given sequence number. Messages are | |
668 | * moved to the retrans queue when rds_send_xmit picks them off the send | |
669 | * queue. This means that in the TCP case, the message may not have been | |
670 | * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked | |
671 | * checks the RDS_MSG_HAS_ACK_SEQ bit. | |
5c115590 | 672 | */ |
5c3d274c SV |
673 | void rds_send_path_drop_acked(struct rds_conn_path *cp, u64 ack, |
674 | is_acked_func is_acked) | |
5c115590 AG |
675 | { |
676 | struct rds_message *rm, *tmp; | |
677 | unsigned long flags; | |
678 | LIST_HEAD(list); | |
679 | ||
5c3d274c | 680 | spin_lock_irqsave(&cp->cp_lock, flags); |
5c115590 | 681 | |
5c3d274c | 682 | list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) { |
5c115590 AG |
683 | if (!rds_send_is_acked(rm, ack, is_acked)) |
684 | break; | |
685 | ||
686 | list_move(&rm->m_conn_item, &list); | |
687 | clear_bit(RDS_MSG_ON_CONN, &rm->m_flags); | |
688 | } | |
689 | ||
690 | /* order flag updates with spin locks */ | |
691 | if (!list_empty(&list)) | |
4e857c58 | 692 | smp_mb__after_atomic(); |
5c115590 | 693 | |
5c3d274c | 694 | spin_unlock_irqrestore(&cp->cp_lock, flags); |
5c115590 AG |
695 | |
696 | /* now remove the messages from the sock list as needed */ | |
697 | rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS); | |
698 | } | |
5c3d274c SV |
699 | EXPORT_SYMBOL_GPL(rds_send_path_drop_acked); |
700 | ||
701 | void rds_send_drop_acked(struct rds_connection *conn, u64 ack, | |
702 | is_acked_func is_acked) | |
703 | { | |
704 | WARN_ON(conn->c_trans->t_mp_capable); | |
705 | rds_send_path_drop_acked(&conn->c_path[0], ack, is_acked); | |
706 | } | |
616b757a | 707 | EXPORT_SYMBOL_GPL(rds_send_drop_acked); |
5c115590 AG |
708 | |
709 | void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest) | |
710 | { | |
711 | struct rds_message *rm, *tmp; | |
712 | struct rds_connection *conn; | |
7c82eaf0 | 713 | unsigned long flags; |
5c115590 | 714 | LIST_HEAD(list); |
5c115590 AG |
715 | |
716 | /* get all the messages we're dropping under the rs lock */ | |
717 | spin_lock_irqsave(&rs->rs_lock, flags); | |
718 | ||
719 | list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) { | |
720 | if (dest && (dest->sin_addr.s_addr != rm->m_daddr || | |
721 | dest->sin_port != rm->m_inc.i_hdr.h_dport)) | |
722 | continue; | |
723 | ||
5c115590 AG |
724 | list_move(&rm->m_sock_item, &list); |
725 | rds_send_sndbuf_remove(rs, rm); | |
726 | clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags); | |
5c115590 AG |
727 | } |
728 | ||
729 | /* order flag updates with the rs lock */ | |
4e857c58 | 730 | smp_mb__after_atomic(); |
5c115590 AG |
731 | |
732 | spin_unlock_irqrestore(&rs->rs_lock, flags); | |
733 | ||
7c82eaf0 AG |
734 | if (list_empty(&list)) |
735 | return; | |
5c115590 | 736 | |
7c82eaf0 | 737 | /* Remove the messages from the conn */ |
5c115590 | 738 | list_for_each_entry(rm, &list, m_sock_item) { |
7c82eaf0 AG |
739 | |
740 | conn = rm->m_inc.i_conn; | |
5c115590 | 741 | |
9de0864c | 742 | spin_lock_irqsave(&conn->c_lock, flags); |
5c115590 | 743 | /* |
7c82eaf0 AG |
744 | * Maybe someone else beat us to removing rm from the conn. |
745 | * If we race with their flag update we'll get the lock and | |
746 | * then really see that the flag has been cleared. | |
5c115590 | 747 | */ |
7c82eaf0 AG |
748 | if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) { |
749 | spin_unlock_irqrestore(&conn->c_lock, flags); | |
593cbb3e HK |
750 | spin_lock_irqsave(&rm->m_rs_lock, flags); |
751 | rm->m_rs = NULL; | |
752 | spin_unlock_irqrestore(&rm->m_rs_lock, flags); | |
5c115590 | 753 | continue; |
5c115590 | 754 | } |
9de0864c AG |
755 | list_del_init(&rm->m_conn_item); |
756 | spin_unlock_irqrestore(&conn->c_lock, flags); | |
5c115590 | 757 | |
7c82eaf0 AG |
758 | /* |
759 | * Couldn't grab m_rs_lock in top loop (lock ordering), | |
760 | * but we can now. | |
761 | */ | |
9de0864c | 762 | spin_lock_irqsave(&rm->m_rs_lock, flags); |
5c115590 | 763 | |
7c82eaf0 | 764 | spin_lock(&rs->rs_lock); |
940786eb | 765 | __rds_send_complete(rs, rm, RDS_RDMA_CANCELED); |
7c82eaf0 AG |
766 | spin_unlock(&rs->rs_lock); |
767 | ||
768 | rm->m_rs = NULL; | |
9de0864c | 769 | spin_unlock_irqrestore(&rm->m_rs_lock, flags); |
7c82eaf0 | 770 | |
7c82eaf0 | 771 | rds_message_put(rm); |
7c82eaf0 | 772 | } |
5c115590 | 773 | |
7c82eaf0 | 774 | rds_wake_sk_sleep(rs); |
550a8002 | 775 | |
5c115590 AG |
776 | while (!list_empty(&list)) { |
777 | rm = list_entry(list.next, struct rds_message, m_sock_item); | |
778 | list_del_init(&rm->m_sock_item); | |
5c115590 | 779 | rds_message_wait(rm); |
dfcec251 | 780 | |
781 | /* just in case the code above skipped this message | |
782 | * because RDS_MSG_ON_CONN wasn't set, run it again here | |
783 | * taking m_rs_lock is the only thing that keeps us | |
784 | * from racing with ack processing. | |
785 | */ | |
786 | spin_lock_irqsave(&rm->m_rs_lock, flags); | |
787 | ||
788 | spin_lock(&rs->rs_lock); | |
789 | __rds_send_complete(rs, rm, RDS_RDMA_CANCELED); | |
790 | spin_unlock(&rs->rs_lock); | |
791 | ||
792 | rm->m_rs = NULL; | |
793 | spin_unlock_irqrestore(&rm->m_rs_lock, flags); | |
794 | ||
5c115590 AG |
795 | rds_message_put(rm); |
796 | } | |
797 | } | |
798 | ||
799 | /* | |
800 | * we only want this to fire once so we use the callers 'queued'. It's | |
801 | * possible that another thread can race with us and remove the | |
802 | * message from the flow with RDS_CANCEL_SENT_TO. | |
803 | */ | |
804 | static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn, | |
780a6d9e | 805 | struct rds_conn_path *cp, |
5c115590 AG |
806 | struct rds_message *rm, __be16 sport, |
807 | __be16 dport, int *queued) | |
808 | { | |
809 | unsigned long flags; | |
810 | u32 len; | |
811 | ||
812 | if (*queued) | |
813 | goto out; | |
814 | ||
815 | len = be32_to_cpu(rm->m_inc.i_hdr.h_len); | |
816 | ||
817 | /* this is the only place which holds both the socket's rs_lock | |
818 | * and the connection's c_lock */ | |
819 | spin_lock_irqsave(&rs->rs_lock, flags); | |
820 | ||
821 | /* | |
822 | * If there is a little space in sndbuf, we don't queue anything, | |
823 | * and userspace gets -EAGAIN. But poll() indicates there's send | |
824 | * room. This can lead to bad behavior (spinning) if snd_bytes isn't | |
825 | * freed up by incoming acks. So we check the *old* value of | |
826 | * rs_snd_bytes here to allow the last msg to exceed the buffer, | |
827 | * and poll() now knows no more data can be sent. | |
828 | */ | |
829 | if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) { | |
830 | rs->rs_snd_bytes += len; | |
831 | ||
832 | /* let recv side know we are close to send space exhaustion. | |
833 | * This is probably not the optimal way to do it, as this | |
834 | * means we set the flag on *all* messages as soon as our | |
835 | * throughput hits a certain threshold. | |
836 | */ | |
837 | if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2) | |
838 | __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags); | |
839 | ||
840 | list_add_tail(&rm->m_sock_item, &rs->rs_send_queue); | |
841 | set_bit(RDS_MSG_ON_SOCK, &rm->m_flags); | |
842 | rds_message_addref(rm); | |
843 | rm->m_rs = rs; | |
844 | ||
845 | /* The code ordering is a little weird, but we're | |
846 | trying to minimize the time we hold c_lock */ | |
847 | rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0); | |
848 | rm->m_inc.i_conn = conn; | |
780a6d9e | 849 | rm->m_inc.i_conn_path = cp; |
5c115590 AG |
850 | rds_message_addref(rm); |
851 | ||
780a6d9e SV |
852 | spin_lock(&cp->cp_lock); |
853 | rm->m_inc.i_hdr.h_sequence = cpu_to_be64(cp->cp_next_tx_seq++); | |
854 | list_add_tail(&rm->m_conn_item, &cp->cp_send_queue); | |
5c115590 | 855 | set_bit(RDS_MSG_ON_CONN, &rm->m_flags); |
780a6d9e | 856 | spin_unlock(&cp->cp_lock); |
5c115590 AG |
857 | |
858 | rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n", | |
859 | rm, len, rs, rs->rs_snd_bytes, | |
860 | (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence)); | |
861 | ||
862 | *queued = 1; | |
863 | } | |
864 | ||
865 | spin_unlock_irqrestore(&rs->rs_lock, flags); | |
866 | out: | |
867 | return *queued; | |
868 | } | |
869 | ||
fc445084 AG |
870 | /* |
871 | * rds_message is getting to be quite complicated, and we'd like to allocate | |
872 | * it all in one go. This figures out how big it needs to be up front. | |
873 | */ | |
874 | static int rds_rm_size(struct msghdr *msg, int data_len) | |
875 | { | |
ff87e97a | 876 | struct cmsghdr *cmsg; |
fc445084 | 877 | int size = 0; |
aa0a4ef4 | 878 | int cmsg_groups = 0; |
ff87e97a AG |
879 | int retval; |
880 | ||
f95b414e | 881 | for_each_cmsghdr(cmsg, msg) { |
ff87e97a AG |
882 | if (!CMSG_OK(msg, cmsg)) |
883 | return -EINVAL; | |
884 | ||
885 | if (cmsg->cmsg_level != SOL_RDS) | |
886 | continue; | |
887 | ||
888 | switch (cmsg->cmsg_type) { | |
889 | case RDS_CMSG_RDMA_ARGS: | |
aa0a4ef4 | 890 | cmsg_groups |= 1; |
ff87e97a AG |
891 | retval = rds_rdma_extra_size(CMSG_DATA(cmsg)); |
892 | if (retval < 0) | |
893 | return retval; | |
894 | size += retval; | |
aa0a4ef4 | 895 | |
ff87e97a AG |
896 | break; |
897 | ||
898 | case RDS_CMSG_RDMA_DEST: | |
899 | case RDS_CMSG_RDMA_MAP: | |
aa0a4ef4 | 900 | cmsg_groups |= 2; |
ff87e97a AG |
901 | /* these are valid but do no add any size */ |
902 | break; | |
903 | ||
15133f6e AG |
904 | case RDS_CMSG_ATOMIC_CSWP: |
905 | case RDS_CMSG_ATOMIC_FADD: | |
20c72bd5 AG |
906 | case RDS_CMSG_MASKED_ATOMIC_CSWP: |
907 | case RDS_CMSG_MASKED_ATOMIC_FADD: | |
aa0a4ef4 | 908 | cmsg_groups |= 1; |
15133f6e AG |
909 | size += sizeof(struct scatterlist); |
910 | break; | |
911 | ||
ff87e97a AG |
912 | default: |
913 | return -EINVAL; | |
914 | } | |
915 | ||
916 | } | |
fc445084 | 917 | |
ff87e97a | 918 | size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist); |
fc445084 | 919 | |
aa0a4ef4 AG |
920 | /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */ |
921 | if (cmsg_groups == 3) | |
922 | return -EINVAL; | |
923 | ||
fc445084 AG |
924 | return size; |
925 | } | |
926 | ||
5c115590 AG |
927 | static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm, |
928 | struct msghdr *msg, int *allocated_mr) | |
929 | { | |
930 | struct cmsghdr *cmsg; | |
931 | int ret = 0; | |
932 | ||
f95b414e | 933 | for_each_cmsghdr(cmsg, msg) { |
5c115590 AG |
934 | if (!CMSG_OK(msg, cmsg)) |
935 | return -EINVAL; | |
936 | ||
937 | if (cmsg->cmsg_level != SOL_RDS) | |
938 | continue; | |
939 | ||
940 | /* As a side effect, RDMA_DEST and RDMA_MAP will set | |
15133f6e | 941 | * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr. |
5c115590 AG |
942 | */ |
943 | switch (cmsg->cmsg_type) { | |
944 | case RDS_CMSG_RDMA_ARGS: | |
945 | ret = rds_cmsg_rdma_args(rs, rm, cmsg); | |
946 | break; | |
947 | ||
948 | case RDS_CMSG_RDMA_DEST: | |
949 | ret = rds_cmsg_rdma_dest(rs, rm, cmsg); | |
950 | break; | |
951 | ||
952 | case RDS_CMSG_RDMA_MAP: | |
953 | ret = rds_cmsg_rdma_map(rs, rm, cmsg); | |
954 | if (!ret) | |
955 | *allocated_mr = 1; | |
956 | break; | |
15133f6e AG |
957 | case RDS_CMSG_ATOMIC_CSWP: |
958 | case RDS_CMSG_ATOMIC_FADD: | |
20c72bd5 AG |
959 | case RDS_CMSG_MASKED_ATOMIC_CSWP: |
960 | case RDS_CMSG_MASKED_ATOMIC_FADD: | |
15133f6e AG |
961 | ret = rds_cmsg_atomic(rs, rm, cmsg); |
962 | break; | |
5c115590 AG |
963 | |
964 | default: | |
965 | return -EINVAL; | |
966 | } | |
967 | ||
968 | if (ret) | |
969 | break; | |
970 | } | |
971 | ||
972 | return ret; | |
973 | } | |
974 | ||
1b784140 | 975 | int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len) |
5c115590 AG |
976 | { |
977 | struct sock *sk = sock->sk; | |
978 | struct rds_sock *rs = rds_sk_to_rs(sk); | |
342dfc30 | 979 | DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); |
5c115590 AG |
980 | __be32 daddr; |
981 | __be16 dport; | |
982 | struct rds_message *rm = NULL; | |
983 | struct rds_connection *conn; | |
984 | int ret = 0; | |
985 | int queued = 0, allocated_mr = 0; | |
986 | int nonblock = msg->msg_flags & MSG_DONTWAIT; | |
1123fd73 | 987 | long timeo = sock_sndtimeo(sk, nonblock); |
780a6d9e | 988 | struct rds_conn_path *cpath; |
5c115590 AG |
989 | |
990 | /* Mirror Linux UDP mirror of BSD error message compatibility */ | |
991 | /* XXX: Perhaps MSG_MORE someday */ | |
992 | if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) { | |
5c115590 AG |
993 | ret = -EOPNOTSUPP; |
994 | goto out; | |
995 | } | |
996 | ||
997 | if (msg->msg_namelen) { | |
998 | /* XXX fail non-unicast destination IPs? */ | |
999 | if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) { | |
1000 | ret = -EINVAL; | |
1001 | goto out; | |
1002 | } | |
1003 | daddr = usin->sin_addr.s_addr; | |
1004 | dport = usin->sin_port; | |
1005 | } else { | |
1006 | /* We only care about consistency with ->connect() */ | |
1007 | lock_sock(sk); | |
1008 | daddr = rs->rs_conn_addr; | |
1009 | dport = rs->rs_conn_port; | |
1010 | release_sock(sk); | |
1011 | } | |
1012 | ||
8c7188b2 | 1013 | lock_sock(sk); |
5c115590 | 1014 | if (daddr == 0 || rs->rs_bound_addr == 0) { |
8c7188b2 | 1015 | release_sock(sk); |
5c115590 AG |
1016 | ret = -ENOTCONN; /* XXX not a great errno */ |
1017 | goto out; | |
1018 | } | |
8c7188b2 | 1019 | release_sock(sk); |
5c115590 | 1020 | |
06e8941e MK |
1021 | if (payload_len > rds_sk_sndbuf(rs)) { |
1022 | ret = -EMSGSIZE; | |
1023 | goto out; | |
1024 | } | |
1025 | ||
fc445084 AG |
1026 | /* size of rm including all sgs */ |
1027 | ret = rds_rm_size(msg, payload_len); | |
1028 | if (ret < 0) | |
1029 | goto out; | |
1030 | ||
1031 | rm = rds_message_alloc(ret, GFP_KERNEL); | |
1032 | if (!rm) { | |
1033 | ret = -ENOMEM; | |
5c115590 AG |
1034 | goto out; |
1035 | } | |
1036 | ||
372cd7de AG |
1037 | /* Attach data to the rm */ |
1038 | if (payload_len) { | |
1039 | rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE)); | |
d139ff09 AG |
1040 | if (!rm->data.op_sg) { |
1041 | ret = -ENOMEM; | |
1042 | goto out; | |
1043 | } | |
c0371da6 | 1044 | ret = rds_message_copy_from_user(rm, &msg->msg_iter); |
372cd7de AG |
1045 | if (ret) |
1046 | goto out; | |
1047 | } | |
1048 | rm->data.op_active = 1; | |
fc445084 | 1049 | |
5c115590 AG |
1050 | rm->m_daddr = daddr; |
1051 | ||
5c115590 AG |
1052 | /* rds_conn_create has a spinlock that runs with IRQ off. |
1053 | * Caching the conn in the socket helps a lot. */ | |
1054 | if (rs->rs_conn && rs->rs_conn->c_faddr == daddr) | |
1055 | conn = rs->rs_conn; | |
1056 | else { | |
d5a8ac28 SV |
1057 | conn = rds_conn_create_outgoing(sock_net(sock->sk), |
1058 | rs->rs_bound_addr, daddr, | |
5c115590 AG |
1059 | rs->rs_transport, |
1060 | sock->sk->sk_allocation); | |
1061 | if (IS_ERR(conn)) { | |
1062 | ret = PTR_ERR(conn); | |
1063 | goto out; | |
1064 | } | |
1065 | rs->rs_conn = conn; | |
1066 | } | |
1067 | ||
49f69691 AG |
1068 | /* Parse any control messages the user may have included. */ |
1069 | ret = rds_cmsg_send(rs, rm, msg, &allocated_mr); | |
1070 | if (ret) | |
1071 | goto out; | |
1072 | ||
2c3a5f9a | 1073 | if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) { |
cb0a6056 | 1074 | printk_ratelimited(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n", |
f8b3aaf2 | 1075 | &rm->rdma, conn->c_trans->xmit_rdma); |
15133f6e AG |
1076 | ret = -EOPNOTSUPP; |
1077 | goto out; | |
1078 | } | |
1079 | ||
1080 | if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) { | |
cb0a6056 | 1081 | printk_ratelimited(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n", |
15133f6e | 1082 | &rm->atomic, conn->c_trans->xmit_atomic); |
5c115590 AG |
1083 | ret = -EOPNOTSUPP; |
1084 | goto out; | |
1085 | } | |
1086 | ||
f3c6808d | 1087 | rds_conn_connect_if_down(conn); |
5c115590 AG |
1088 | |
1089 | ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs); | |
b98ba52f AG |
1090 | if (ret) { |
1091 | rs->rs_seen_congestion = 1; | |
5c115590 | 1092 | goto out; |
b98ba52f | 1093 | } |
5c115590 | 1094 | |
780a6d9e SV |
1095 | cpath = &conn->c_path[0]; |
1096 | ||
1097 | while (!rds_send_queue_rm(rs, conn, cpath, rm, rs->rs_bound_port, | |
5c115590 AG |
1098 | dport, &queued)) { |
1099 | rds_stats_inc(s_send_queue_full); | |
06e8941e | 1100 | |
5c115590 AG |
1101 | if (nonblock) { |
1102 | ret = -EAGAIN; | |
1103 | goto out; | |
1104 | } | |
1105 | ||
aa395145 | 1106 | timeo = wait_event_interruptible_timeout(*sk_sleep(sk), |
780a6d9e | 1107 | rds_send_queue_rm(rs, conn, cpath, rm, |
5c115590 AG |
1108 | rs->rs_bound_port, |
1109 | dport, | |
1110 | &queued), | |
1111 | timeo); | |
1112 | rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo); | |
1113 | if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT) | |
1114 | continue; | |
1115 | ||
1116 | ret = timeo; | |
1117 | if (ret == 0) | |
1118 | ret = -ETIMEDOUT; | |
1119 | goto out; | |
1120 | } | |
1121 | ||
1122 | /* | |
1123 | * By now we've committed to the send. We reuse rds_send_worker() | |
1124 | * to retry sends in the rds thread if the transport asks us to. | |
1125 | */ | |
1126 | rds_stats_inc(s_send_queued); | |
1127 | ||
1f9ecd7e | 1128 | ret = rds_send_xmit(cpath); |
db6526dc | 1129 | if (ret == -ENOMEM || ret == -EAGAIN) |
1f9ecd7e | 1130 | queue_delayed_work(rds_wq, &cpath->cp_send_w, 1); |
5c115590 AG |
1131 | |
1132 | rds_message_put(rm); | |
1133 | return payload_len; | |
1134 | ||
1135 | out: | |
1136 | /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly. | |
1137 | * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN | |
1138 | * or in any other way, we need to destroy the MR again */ | |
1139 | if (allocated_mr) | |
1140 | rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1); | |
1141 | ||
1142 | if (rm) | |
1143 | rds_message_put(rm); | |
1144 | return ret; | |
1145 | } | |
1146 | ||
1147 | /* | |
1148 | * Reply to a ping packet. | |
1149 | */ | |
1150 | int | |
1151 | rds_send_pong(struct rds_connection *conn, __be16 dport) | |
1152 | { | |
1153 | struct rds_message *rm; | |
1154 | unsigned long flags; | |
1155 | int ret = 0; | |
1156 | ||
1157 | rm = rds_message_alloc(0, GFP_ATOMIC); | |
8690bfa1 | 1158 | if (!rm) { |
5c115590 AG |
1159 | ret = -ENOMEM; |
1160 | goto out; | |
1161 | } | |
1162 | ||
1163 | rm->m_daddr = conn->c_faddr; | |
acfcd4d4 | 1164 | rm->data.op_active = 1; |
5c115590 | 1165 | |
f3c6808d | 1166 | rds_conn_connect_if_down(conn); |
5c115590 AG |
1167 | |
1168 | ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL); | |
1169 | if (ret) | |
1170 | goto out; | |
1171 | ||
1172 | spin_lock_irqsave(&conn->c_lock, flags); | |
1173 | list_add_tail(&rm->m_conn_item, &conn->c_send_queue); | |
1174 | set_bit(RDS_MSG_ON_CONN, &rm->m_flags); | |
1175 | rds_message_addref(rm); | |
1176 | rm->m_inc.i_conn = conn; | |
1177 | ||
1178 | rds_message_populate_header(&rm->m_inc.i_hdr, 0, dport, | |
1179 | conn->c_next_tx_seq); | |
1180 | conn->c_next_tx_seq++; | |
1181 | spin_unlock_irqrestore(&conn->c_lock, flags); | |
1182 | ||
1183 | rds_stats_inc(s_send_queued); | |
1184 | rds_stats_inc(s_send_pong); | |
1185 | ||
7b4b0009 | 1186 | /* schedule the send work on rds_wq */ |
1187 | queue_delayed_work(rds_wq, &conn->c_send_w, 1); | |
acfcd4d4 | 1188 | |
5c115590 AG |
1189 | rds_message_put(rm); |
1190 | return 0; | |
1191 | ||
1192 | out: | |
1193 | if (rm) | |
1194 | rds_message_put(rm); | |
1195 | return ret; | |
1196 | } |