1 /* 2 * Copyright (c) 2006, 2019 Oracle and/or its affiliates. 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/slab.h> 35 #include <net/sock.h> 36 #include <linux/in.h> 37 #include <linux/export.h> 38 #include <linux/sched/clock.h> 39 #include <linux/time.h> 40 #include <linux/rds.h> 41 42 #include "rds.h" 43 44 void rds_inc_init(struct rds_incoming *inc, struct rds_connection *conn, 45 struct in6_addr *saddr) 46 { 47 refcount_set(&inc->i_refcount, 1); 48 INIT_LIST_HEAD(&inc->i_item); 49 inc->i_conn = conn; 50 inc->i_saddr = *saddr; 51 inc->i_usercopy.rdma_cookie = 0; 52 inc->i_usercopy.rx_tstamp = ktime_set(0, 0); 53 54 memset(inc->i_rx_lat_trace, 0, sizeof(inc->i_rx_lat_trace)); 55 } 56 EXPORT_SYMBOL_GPL(rds_inc_init); 57 58 void rds_inc_path_init(struct rds_incoming *inc, struct rds_conn_path *cp, 59 struct in6_addr *saddr) 60 { 61 refcount_set(&inc->i_refcount, 1); 62 INIT_LIST_HEAD(&inc->i_item); 63 inc->i_conn = cp->cp_conn; 64 inc->i_conn_path = cp; 65 inc->i_saddr = *saddr; 66 inc->i_usercopy.rdma_cookie = 0; 67 inc->i_usercopy.rx_tstamp = ktime_set(0, 0); 68 } 69 EXPORT_SYMBOL_GPL(rds_inc_path_init); 70 71 static void rds_inc_addref(struct rds_incoming *inc) 72 { 73 rdsdebug("addref inc %p ref %d\n", inc, refcount_read(&inc->i_refcount)); 74 refcount_inc(&inc->i_refcount); 75 } 76 77 void rds_inc_put(struct rds_incoming *inc) 78 { 79 rdsdebug("put inc %p ref %d\n", inc, refcount_read(&inc->i_refcount)); 80 if (refcount_dec_and_test(&inc->i_refcount)) { 81 BUG_ON(!list_empty(&inc->i_item)); 82 83 inc->i_conn->c_trans->inc_free(inc); 84 } 85 } 86 EXPORT_SYMBOL_GPL(rds_inc_put); 87 88 static void rds_recv_rcvbuf_delta(struct rds_sock *rs, struct sock *sk, 89 struct rds_cong_map *map, 90 int delta, __be16 port) 91 { 92 int now_congested; 93 94 if (delta == 0) 95 return; 96 97 rs->rs_rcv_bytes += delta; 98 if (delta > 0) 99 rds_stats_add(s_recv_bytes_added_to_socket, delta); 100 else 101 rds_stats_add(s_recv_bytes_removed_from_socket, -delta); 102 103 /* loop transport doesn't send/recv congestion updates */ 104 if (rs->rs_transport->t_type == RDS_TRANS_LOOP) 105 return; 106 107 now_congested = rs->rs_rcv_bytes > rds_sk_rcvbuf(rs); 108 109 rdsdebug("rs %p (%pI6c:%u) recv bytes %d buf %d " 110 "now_cong %d delta %d\n", 111 rs, &rs->rs_bound_addr, 112 ntohs(rs->rs_bound_port), rs->rs_rcv_bytes, 113 rds_sk_rcvbuf(rs), now_congested, delta); 114 115 /* wasn't -> am congested */ 116 if (!rs->rs_congested && now_congested) { 117 rs->rs_congested = 1; 118 rds_cong_set_bit(map, port); 119 rds_cong_queue_updates(map); 120 } 121 /* was -> aren't congested */ 122 /* Require more free space before reporting uncongested to prevent 123 bouncing cong/uncong state too often */ 124 else if (rs->rs_congested && (rs->rs_rcv_bytes < (rds_sk_rcvbuf(rs)/2))) { 125 rs->rs_congested = 0; 126 rds_cong_clear_bit(map, port); 127 rds_cong_queue_updates(map); 128 } 129 130 /* do nothing if no change in cong state */ 131 } 132 133 static void rds_conn_peer_gen_update(struct rds_connection *conn, 134 u32 peer_gen_num) 135 { 136 int i; 137 struct rds_message *rm, *tmp; 138 unsigned long flags; 139 140 WARN_ON(conn->c_trans->t_type != RDS_TRANS_TCP); 141 if (peer_gen_num != 0) { 142 if (conn->c_peer_gen_num != 0 && 143 peer_gen_num != conn->c_peer_gen_num) { 144 for (i = 0; i < RDS_MPATH_WORKERS; i++) { 145 struct rds_conn_path *cp; 146 147 cp = &conn->c_path[i]; 148 spin_lock_irqsave(&cp->cp_lock, flags); 149 cp->cp_next_tx_seq = 1; 150 cp->cp_next_rx_seq = 0; 151 list_for_each_entry_safe(rm, tmp, 152 &cp->cp_retrans, 153 m_conn_item) { 154 set_bit(RDS_MSG_FLUSH, &rm->m_flags); 155 } 156 spin_unlock_irqrestore(&cp->cp_lock, flags); 157 } 158 } 159 conn->c_peer_gen_num = peer_gen_num; 160 } 161 } 162 163 /* 164 * Process all extension headers that come with this message. 165 */ 166 static void rds_recv_incoming_exthdrs(struct rds_incoming *inc, struct rds_sock *rs) 167 { 168 struct rds_header *hdr = &inc->i_hdr; 169 unsigned int pos = 0, type, len; 170 union { 171 struct rds_ext_header_version version; 172 struct rds_ext_header_rdma rdma; 173 struct rds_ext_header_rdma_dest rdma_dest; 174 } buffer; 175 176 while (1) { 177 len = sizeof(buffer); 178 type = rds_message_next_extension(hdr, &pos, &buffer, &len); 179 if (type == RDS_EXTHDR_NONE) 180 break; 181 /* Process extension header here */ 182 switch (type) { 183 case RDS_EXTHDR_RDMA: 184 rds_rdma_unuse(rs, be32_to_cpu(buffer.rdma.h_rdma_rkey), 0); 185 break; 186 187 case RDS_EXTHDR_RDMA_DEST: 188 /* We ignore the size for now. We could stash it 189 * somewhere and use it for error checking. */ 190 inc->i_usercopy.rdma_cookie = rds_rdma_make_cookie( 191 be32_to_cpu(buffer.rdma_dest.h_rdma_rkey), 192 be32_to_cpu(buffer.rdma_dest.h_rdma_offset)); 193 194 break; 195 } 196 } 197 } 198 199 static void rds_recv_hs_exthdrs(struct rds_header *hdr, 200 struct rds_connection *conn) 201 { 202 unsigned int pos = 0, type, len; 203 union { 204 struct rds_ext_header_version version; 205 __be16 rds_npaths; 206 __be32 rds_gen_num; 207 u8 dummy; 208 } buffer; 209 bool new_with_sport_idx = false; 210 u32 new_peer_gen_num = 0; 211 int new_npaths; 212 bool fan_out; 213 214 new_npaths = conn->c_npaths; 215 216 while (1) { 217 len = sizeof(buffer); 218 type = rds_message_next_extension(hdr, &pos, &buffer, &len); 219 if (type == RDS_EXTHDR_NONE) 220 break; 221 /* Process extension header here */ 222 switch (type) { 223 case RDS_EXTHDR_NPATHS: 224 new_npaths = min_t(int, RDS_MPATH_WORKERS, 225 be16_to_cpu(buffer.rds_npaths)); 226 break; 227 case RDS_EXTHDR_GEN_NUM: 228 new_peer_gen_num = be32_to_cpu(buffer.rds_gen_num); 229 break; 230 case RDS_EXTHDR_SPORT_IDX: 231 new_with_sport_idx = true; 232 break; 233 default: 234 pr_warn_ratelimited("ignoring unknown exthdr type " 235 "0x%x\n", type); 236 } 237 } 238 239 conn->c_with_sport_idx = new_with_sport_idx; 240 241 if (new_npaths > 1 && new_npaths != conn->c_npaths) { 242 /* We're about to fan-out. 243 * Make sure that messages from cp_index#0 244 * are sent prior to handling other lanes. 245 */ 246 struct rds_conn_path *cp0 = conn->c_path; 247 unsigned long flags; 248 249 spin_lock_irqsave(&cp0->cp_lock, flags); 250 conn->c_cp0_mprds_catchup_tx_seq = cp0->cp_next_tx_seq; 251 spin_unlock_irqrestore(&cp0->cp_lock, flags); 252 fan_out = true; 253 } else { 254 fan_out = false; 255 } 256 257 /* if RDS_EXTHDR_NPATHS was not found, default to a single-path */ 258 conn->c_npaths = max_t(int, new_npaths, 1); 259 260 conn->c_ping_triggered = 0; 261 rds_conn_peer_gen_update(conn, new_peer_gen_num); 262 263 if (conn->c_npaths > 1 && 264 conn->c_trans->conn_slots_available) 265 conn->c_trans->conn_slots_available(conn, fan_out); 266 } 267 268 /* rds_start_mprds() will synchronously start multiple paths when appropriate. 269 * The scheme is based on the following rules: 270 * 271 * 1. rds_sendmsg on first connect attempt sends the probe ping, with the 272 * sender's npaths (s_npaths) 273 * 2. rcvr of probe-ping knows the mprds_paths = min(s_npaths, r_npaths). It 274 * sends back a probe-pong with r_npaths. After that, if rcvr is the 275 * smaller ip addr, it starts rds_conn_path_connect_if_down on all 276 * mprds_paths. 277 * 3. sender gets woken up, and can move to rds_conn_path_connect_if_down. 278 * If it is the smaller ipaddr, rds_conn_path_connect_if_down can be 279 * called after reception of the probe-pong on all mprds_paths. 280 * Otherwise (sender of probe-ping is not the smaller ip addr): just call 281 * rds_conn_path_connect_if_down on the hashed path. (see rule 4) 282 * 4. rds_connect_worker must only trigger a connection if laddr < faddr. 283 * 5. sender may end up queuing the packet on the cp. will get sent out later. 284 * when connection is completed. 285 */ 286 static void rds_start_mprds(struct rds_connection *conn) 287 { 288 int i; 289 struct rds_conn_path *cp; 290 291 if (conn->c_npaths > 1 && 292 rds_addr_cmp(&conn->c_laddr, &conn->c_faddr) < 0) { 293 for (i = 0; i < conn->c_npaths; i++) { 294 cp = &conn->c_path[i]; 295 rds_conn_path_connect_if_down(cp); 296 } 297 } 298 } 299 300 /* 301 * The transport must make sure that this is serialized against other 302 * rx and conn reset on this specific conn. 303 * 304 * We currently assert that only one fragmented message will be sent 305 * down a connection at a time. This lets us reassemble in the conn 306 * instead of per-flow which means that we don't have to go digging through 307 * flows to tear down partial reassembly progress on conn failure and 308 * we save flow lookup and locking for each frag arrival. It does mean 309 * that small messages will wait behind large ones. Fragmenting at all 310 * is only to reduce the memory consumption of pre-posted buffers. 311 * 312 * The caller passes in saddr and daddr instead of us getting it from the 313 * conn. This lets loopback, who only has one conn for both directions, 314 * tell us which roles the addrs in the conn are playing for this message. 315 */ 316 void rds_recv_incoming(struct rds_connection *conn, struct in6_addr *saddr, 317 struct in6_addr *daddr, 318 struct rds_incoming *inc, gfp_t gfp) 319 { 320 struct rds_sock *rs = NULL; 321 struct sock *sk; 322 unsigned long flags; 323 struct rds_conn_path *cp; 324 325 inc->i_conn = conn; 326 inc->i_rx_jiffies = jiffies; 327 if (conn->c_trans->t_mp_capable) 328 cp = inc->i_conn_path; 329 else 330 cp = &conn->c_path[0]; 331 332 rdsdebug("conn %p next %llu inc %p seq %llu len %u sport %u dport %u " 333 "flags 0x%x rx_jiffies %lu\n", conn, 334 (unsigned long long)cp->cp_next_rx_seq, 335 inc, 336 (unsigned long long)be64_to_cpu(inc->i_hdr.h_sequence), 337 be32_to_cpu(inc->i_hdr.h_len), 338 be16_to_cpu(inc->i_hdr.h_sport), 339 be16_to_cpu(inc->i_hdr.h_dport), 340 inc->i_hdr.h_flags, 341 inc->i_rx_jiffies); 342 343 /* 344 * Sequence numbers should only increase. Messages get their 345 * sequence number as they're queued in a sending conn. They 346 * can be dropped, though, if the sending socket is closed before 347 * they hit the wire. So sequence numbers can skip forward 348 * under normal operation. They can also drop back in the conn 349 * failover case as previously sent messages are resent down the 350 * new instance of a conn. We drop those, otherwise we have 351 * to assume that the next valid seq does not come after a 352 * hole in the fragment stream. 353 * 354 * The headers don't give us a way to realize if fragments of 355 * a message have been dropped. We assume that frags that arrive 356 * to a flow are part of the current message on the flow that is 357 * being reassembled. This means that senders can't drop messages 358 * from the sending conn until all their frags are sent. 359 * 360 * XXX we could spend more on the wire to get more robust failure 361 * detection, arguably worth it to avoid data corruption. 362 */ 363 if (be64_to_cpu(inc->i_hdr.h_sequence) < cp->cp_next_rx_seq && 364 (inc->i_hdr.h_flags & RDS_FLAG_RETRANSMITTED)) { 365 rds_stats_inc(s_recv_drop_old_seq); 366 goto out; 367 } 368 cp->cp_next_rx_seq = be64_to_cpu(inc->i_hdr.h_sequence) + 1; 369 370 if (rds_sysctl_ping_enable && inc->i_hdr.h_dport == 0) { 371 if (inc->i_hdr.h_sport == 0) { 372 rdsdebug("ignore ping with 0 sport from %pI6c\n", 373 saddr); 374 goto out; 375 } 376 rds_stats_inc(s_recv_ping); 377 rds_send_pong(cp, inc->i_hdr.h_sport); 378 /* if this is a handshake ping, start multipath if necessary */ 379 if (RDS_HS_PROBE(be16_to_cpu(inc->i_hdr.h_sport), 380 be16_to_cpu(inc->i_hdr.h_dport))) { 381 rds_recv_hs_exthdrs(&inc->i_hdr, cp->cp_conn); 382 rds_start_mprds(cp->cp_conn); 383 } 384 goto out; 385 } 386 387 if (be16_to_cpu(inc->i_hdr.h_dport) == RDS_FLAG_PROBE_PORT && 388 inc->i_hdr.h_sport == 0) { 389 rds_recv_hs_exthdrs(&inc->i_hdr, cp->cp_conn); 390 /* if this is a handshake pong, start multipath if necessary */ 391 rds_start_mprds(cp->cp_conn); 392 wake_up(&cp->cp_conn->c_hs_waitq); 393 goto out; 394 } 395 396 rs = rds_find_bound(daddr, inc->i_hdr.h_dport, conn->c_bound_if); 397 if (!rs) { 398 rds_stats_inc(s_recv_drop_no_sock); 399 goto out; 400 } 401 402 /* 403 * rds_find_bound() uses a global (netns-agnostic) hash table. 404 * An RDS connection created in netns A can match a socket bound 405 * in the init netns, delivering inc cross-netns with inc->i_conn 406 * pointing into netns A. When cleanup_net() then frees that conn, 407 * any subsequent dereference of inc->i_conn is a use-after-free. 408 * Drop the inc if the receiving socket lives in a different netns. 409 */ 410 if (!net_eq(sock_net(rds_rs_to_sk(rs)), rds_conn_net(conn))) { 411 rds_stats_inc(s_recv_drop_no_sock); 412 rds_sock_put(rs); 413 rs = NULL; 414 goto out; 415 } 416 417 /* Process extension headers */ 418 rds_recv_incoming_exthdrs(inc, rs); 419 420 /* We can be racing with rds_release() which marks the socket dead. */ 421 sk = rds_rs_to_sk(rs); 422 423 /* serialize with rds_release -> sock_orphan */ 424 write_lock_irqsave(&rs->rs_recv_lock, flags); 425 if (!sock_flag(sk, SOCK_DEAD)) { 426 rdsdebug("adding inc %p to rs %p's recv queue\n", inc, rs); 427 rds_stats_inc(s_recv_queued); 428 rds_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong, 429 be32_to_cpu(inc->i_hdr.h_len), 430 inc->i_hdr.h_dport); 431 if (sock_flag(sk, SOCK_RCVTSTAMP)) 432 inc->i_usercopy.rx_tstamp = ktime_get_real(); 433 rds_inc_addref(inc); 434 inc->i_rx_lat_trace[RDS_MSG_RX_END] = local_clock(); 435 list_add_tail(&inc->i_item, &rs->rs_recv_queue); 436 __rds_wake_sk_sleep(sk); 437 } else { 438 rds_stats_inc(s_recv_drop_dead_sock); 439 } 440 write_unlock_irqrestore(&rs->rs_recv_lock, flags); 441 442 out: 443 if (rs) 444 rds_sock_put(rs); 445 } 446 EXPORT_SYMBOL_GPL(rds_recv_incoming); 447 448 /* 449 * be very careful here. This is being called as the condition in 450 * wait_event_*() needs to cope with being called many times. 451 */ 452 static int rds_next_incoming(struct rds_sock *rs, struct rds_incoming **inc) 453 { 454 unsigned long flags; 455 456 if (!*inc) { 457 read_lock_irqsave(&rs->rs_recv_lock, flags); 458 if (!list_empty(&rs->rs_recv_queue)) { 459 *inc = list_entry(rs->rs_recv_queue.next, 460 struct rds_incoming, 461 i_item); 462 rds_inc_addref(*inc); 463 } 464 read_unlock_irqrestore(&rs->rs_recv_lock, flags); 465 } 466 467 return *inc != NULL; 468 } 469 470 static int rds_still_queued(struct rds_sock *rs, struct rds_incoming *inc, 471 int drop) 472 { 473 struct sock *sk = rds_rs_to_sk(rs); 474 int ret = 0; 475 unsigned long flags; 476 struct rds_incoming *to_drop = NULL; 477 478 write_lock_irqsave(&rs->rs_recv_lock, flags); 479 if (!list_empty(&inc->i_item)) { 480 ret = 1; 481 if (drop) { 482 /* XXX make sure this i_conn is reliable */ 483 rds_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong, 484 -be32_to_cpu(inc->i_hdr.h_len), 485 inc->i_hdr.h_dport); 486 list_del_init(&inc->i_item); 487 to_drop = inc; 488 } 489 } 490 write_unlock_irqrestore(&rs->rs_recv_lock, flags); 491 492 if (to_drop) 493 rds_inc_put(to_drop); 494 495 rdsdebug("inc %p rs %p still %d dropped %d\n", inc, rs, ret, drop); 496 return ret; 497 } 498 499 /* 500 * Pull errors off the error queue. 501 * If msghdr is NULL, we will just purge the error queue. 502 */ 503 int rds_notify_queue_get(struct rds_sock *rs, struct msghdr *msghdr) 504 { 505 struct rds_notifier *notifier; 506 struct rds_rdma_notify cmsg; 507 unsigned int count = 0, max_messages = ~0U; 508 unsigned long flags; 509 LIST_HEAD(copy); 510 int err = 0; 511 512 memset(&cmsg, 0, sizeof(cmsg)); /* fill holes with zero */ 513 514 /* put_cmsg copies to user space and thus may sleep. We can't do this 515 * with rs_lock held, so first grab as many notifications as we can stuff 516 * in the user provided cmsg buffer. We don't try to copy more, to avoid 517 * losing notifications - except when the buffer is so small that it wouldn't 518 * even hold a single notification. Then we give him as much of this single 519 * msg as we can squeeze in, and set MSG_CTRUNC. 520 */ 521 if (msghdr) { 522 max_messages = msghdr->msg_controllen / CMSG_SPACE(sizeof(cmsg)); 523 if (!max_messages) 524 max_messages = 1; 525 } 526 527 spin_lock_irqsave(&rs->rs_lock, flags); 528 while (!list_empty(&rs->rs_notify_queue) && count < max_messages) { 529 notifier = list_entry(rs->rs_notify_queue.next, 530 struct rds_notifier, n_list); 531 list_move(¬ifier->n_list, ©); 532 count++; 533 } 534 spin_unlock_irqrestore(&rs->rs_lock, flags); 535 536 if (!count) 537 return 0; 538 539 while (!list_empty(©)) { 540 notifier = list_entry(copy.next, struct rds_notifier, n_list); 541 542 if (msghdr) { 543 cmsg.user_token = notifier->n_user_token; 544 cmsg.status = notifier->n_status; 545 546 err = put_cmsg(msghdr, SOL_RDS, RDS_CMSG_RDMA_STATUS, 547 sizeof(cmsg), &cmsg); 548 if (err) 549 break; 550 } 551 552 list_del_init(¬ifier->n_list); 553 kfree(notifier); 554 } 555 556 /* If we bailed out because of an error in put_cmsg, 557 * we may be left with one or more notifications that we 558 * didn't process. Return them to the head of the list. */ 559 if (!list_empty(©)) { 560 spin_lock_irqsave(&rs->rs_lock, flags); 561 list_splice(©, &rs->rs_notify_queue); 562 spin_unlock_irqrestore(&rs->rs_lock, flags); 563 } 564 565 return err; 566 } 567 568 /* 569 * Queue a congestion notification 570 */ 571 static int rds_notify_cong(struct rds_sock *rs, struct msghdr *msghdr) 572 { 573 uint64_t notify = rs->rs_cong_notify; 574 unsigned long flags; 575 int err; 576 577 err = put_cmsg(msghdr, SOL_RDS, RDS_CMSG_CONG_UPDATE, 578 sizeof(notify), ¬ify); 579 if (err) 580 return err; 581 582 spin_lock_irqsave(&rs->rs_lock, flags); 583 rs->rs_cong_notify &= ~notify; 584 spin_unlock_irqrestore(&rs->rs_lock, flags); 585 586 return 0; 587 } 588 589 /* 590 * Receive any control messages. 591 */ 592 static int rds_cmsg_recv(struct rds_incoming *inc, struct msghdr *msg, 593 struct rds_sock *rs) 594 { 595 int ret = 0; 596 597 if (inc->i_usercopy.rdma_cookie) { 598 ret = put_cmsg(msg, SOL_RDS, RDS_CMSG_RDMA_DEST, 599 sizeof(inc->i_usercopy.rdma_cookie), 600 &inc->i_usercopy.rdma_cookie); 601 if (ret) 602 goto out; 603 } 604 605 if ((inc->i_usercopy.rx_tstamp != 0) && 606 sock_flag(rds_rs_to_sk(rs), SOCK_RCVTSTAMP)) { 607 struct __kernel_old_timeval tv = 608 ns_to_kernel_old_timeval(inc->i_usercopy.rx_tstamp); 609 610 if (!sock_flag(rds_rs_to_sk(rs), SOCK_TSTAMP_NEW)) { 611 ret = put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD, 612 sizeof(tv), &tv); 613 } else { 614 struct __kernel_sock_timeval sk_tv; 615 616 sk_tv.tv_sec = tv.tv_sec; 617 sk_tv.tv_usec = tv.tv_usec; 618 619 ret = put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW, 620 sizeof(sk_tv), &sk_tv); 621 } 622 623 if (ret) 624 goto out; 625 } 626 627 if (rs->rs_rx_traces) { 628 struct rds_cmsg_rx_trace t; 629 int i, j; 630 631 memset(&t, 0, sizeof(t)); 632 inc->i_rx_lat_trace[RDS_MSG_RX_CMSG] = local_clock(); 633 t.rx_traces = rs->rs_rx_traces; 634 for (i = 0; i < rs->rs_rx_traces; i++) { 635 j = rs->rs_rx_trace[i]; 636 t.rx_trace_pos[i] = j; 637 t.rx_trace[i] = inc->i_rx_lat_trace[j + 1] - 638 inc->i_rx_lat_trace[j]; 639 } 640 641 ret = put_cmsg(msg, SOL_RDS, RDS_CMSG_RXPATH_LATENCY, 642 sizeof(t), &t); 643 if (ret) 644 goto out; 645 } 646 647 out: 648 return ret; 649 } 650 651 static bool rds_recvmsg_zcookie(struct rds_sock *rs, struct msghdr *msg) 652 { 653 struct rds_msg_zcopy_queue *q = &rs->rs_zcookie_queue; 654 struct rds_msg_zcopy_info *info = NULL; 655 struct rds_zcopy_cookies *done; 656 unsigned long flags; 657 658 if (!msg->msg_control) 659 return false; 660 661 if (!sock_flag(rds_rs_to_sk(rs), SOCK_ZEROCOPY) || 662 msg->msg_controllen < CMSG_SPACE(sizeof(*done))) 663 return false; 664 665 spin_lock_irqsave(&q->lock, flags); 666 if (!list_empty(&q->zcookie_head)) { 667 info = list_entry(q->zcookie_head.next, 668 struct rds_msg_zcopy_info, rs_zcookie_next); 669 list_del(&info->rs_zcookie_next); 670 } 671 spin_unlock_irqrestore(&q->lock, flags); 672 if (!info) 673 return false; 674 done = &info->zcookies; 675 if (put_cmsg(msg, SOL_RDS, RDS_CMSG_ZCOPY_COMPLETION, sizeof(*done), 676 done)) { 677 spin_lock_irqsave(&q->lock, flags); 678 list_add(&info->rs_zcookie_next, &q->zcookie_head); 679 spin_unlock_irqrestore(&q->lock, flags); 680 return false; 681 } 682 kfree(info); 683 return true; 684 } 685 686 int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 687 int msg_flags) 688 { 689 struct sock *sk = sock->sk; 690 struct rds_sock *rs = rds_sk_to_rs(sk); 691 long timeo; 692 int ret = 0, nonblock = msg_flags & MSG_DONTWAIT; 693 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 694 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 695 struct rds_incoming *inc = NULL; 696 697 /* udp_recvmsg()->sock_recvtimeo() gets away without locking too.. */ 698 timeo = sock_rcvtimeo(sk, nonblock); 699 700 rdsdebug("size %zu flags 0x%x timeo %ld\n", size, msg_flags, timeo); 701 702 if (msg_flags & MSG_OOB) 703 goto out; 704 if (msg_flags & MSG_ERRQUEUE) 705 return sock_recv_errqueue(sk, msg, size, SOL_IP, IP_RECVERR); 706 707 while (1) { 708 /* If there are pending notifications, do those - and nothing else */ 709 if (!list_empty(&rs->rs_notify_queue)) { 710 ret = rds_notify_queue_get(rs, msg); 711 break; 712 } 713 714 if (rs->rs_cong_notify) { 715 ret = rds_notify_cong(rs, msg); 716 break; 717 } 718 719 if (!rds_next_incoming(rs, &inc)) { 720 if (nonblock) { 721 bool reaped = rds_recvmsg_zcookie(rs, msg); 722 723 ret = reaped ? 0 : -EAGAIN; 724 break; 725 } 726 727 timeo = wait_event_interruptible_timeout(*sk_sleep(sk), 728 (!list_empty(&rs->rs_notify_queue) || 729 rs->rs_cong_notify || 730 rds_next_incoming(rs, &inc)), timeo); 731 rdsdebug("recvmsg woke inc %p timeo %ld\n", inc, 732 timeo); 733 if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT) 734 continue; 735 736 ret = timeo; 737 if (ret == 0) 738 ret = -ETIMEDOUT; 739 break; 740 } 741 742 rdsdebug("copying inc %p from %pI6c:%u to user\n", inc, 743 &inc->i_conn->c_faddr, 744 ntohs(inc->i_hdr.h_sport)); 745 ret = inc->i_conn->c_trans->inc_copy_to_user(inc, &msg->msg_iter); 746 if (ret < 0) 747 break; 748 749 /* 750 * if the message we just copied isn't at the head of the 751 * recv queue then someone else raced us to return it, try 752 * to get the next message. 753 */ 754 if (!rds_still_queued(rs, inc, !(msg_flags & MSG_PEEK))) { 755 rds_inc_put(inc); 756 inc = NULL; 757 rds_stats_inc(s_recv_deliver_raced); 758 iov_iter_revert(&msg->msg_iter, ret); 759 continue; 760 } 761 762 if (ret < be32_to_cpu(inc->i_hdr.h_len)) { 763 if (msg_flags & MSG_TRUNC) 764 ret = be32_to_cpu(inc->i_hdr.h_len); 765 msg->msg_flags |= MSG_TRUNC; 766 } 767 768 if (rds_cmsg_recv(inc, msg, rs)) { 769 ret = -EFAULT; 770 break; 771 } 772 rds_recvmsg_zcookie(rs, msg); 773 774 rds_stats_inc(s_recv_delivered); 775 776 if (msg->msg_name) { 777 if (ipv6_addr_v4mapped(&inc->i_saddr)) { 778 sin->sin_family = AF_INET; 779 sin->sin_port = inc->i_hdr.h_sport; 780 sin->sin_addr.s_addr = 781 inc->i_saddr.s6_addr32[3]; 782 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 783 msg->msg_namelen = sizeof(*sin); 784 } else { 785 sin6->sin6_family = AF_INET6; 786 sin6->sin6_port = inc->i_hdr.h_sport; 787 sin6->sin6_addr = inc->i_saddr; 788 sin6->sin6_flowinfo = 0; 789 sin6->sin6_scope_id = rs->rs_bound_scope_id; 790 msg->msg_namelen = sizeof(*sin6); 791 } 792 } 793 break; 794 } 795 796 if (inc) 797 rds_inc_put(inc); 798 799 out: 800 return ret; 801 } 802 803 /* 804 * The socket is being shut down and we're asked to drop messages that were 805 * queued for recvmsg. The caller has unbound the socket so the receive path 806 * won't queue any more incoming fragments or messages on the socket. 807 */ 808 void rds_clear_recv_queue(struct rds_sock *rs) 809 { 810 struct sock *sk = rds_rs_to_sk(rs); 811 struct rds_incoming *inc, *tmp; 812 unsigned long flags; 813 LIST_HEAD(to_drop); 814 815 write_lock_irqsave(&rs->rs_recv_lock, flags); 816 list_for_each_entry_safe(inc, tmp, &rs->rs_recv_queue, i_item) { 817 rds_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong, 818 -be32_to_cpu(inc->i_hdr.h_len), 819 inc->i_hdr.h_dport); 820 list_move(&inc->i_item, &to_drop); 821 } 822 write_unlock_irqrestore(&rs->rs_recv_lock, flags); 823 824 list_for_each_entry_safe(inc, tmp, &to_drop, i_item) { 825 list_del_init(&inc->i_item); 826 rds_inc_put(inc); 827 } 828 } 829 830 /* 831 * inc->i_saddr isn't used here because it is only set in the receive 832 * path. 833 */ 834 void rds_inc_info_copy(struct rds_incoming *inc, 835 struct rds_info_iterator *iter, 836 __be32 saddr, __be32 daddr, int flip) 837 { 838 struct rds_info_message minfo; 839 840 minfo.seq = be64_to_cpu(inc->i_hdr.h_sequence); 841 minfo.len = be32_to_cpu(inc->i_hdr.h_len); 842 minfo.tos = inc->i_conn->c_tos; 843 844 if (flip) { 845 minfo.laddr = daddr; 846 minfo.faddr = saddr; 847 minfo.lport = inc->i_hdr.h_dport; 848 minfo.fport = inc->i_hdr.h_sport; 849 } else { 850 minfo.laddr = saddr; 851 minfo.faddr = daddr; 852 minfo.lport = inc->i_hdr.h_sport; 853 minfo.fport = inc->i_hdr.h_dport; 854 } 855 856 minfo.flags = 0; 857 858 rds_info_copy(iter, &minfo, sizeof(minfo)); 859 } 860 861 #if IS_ENABLED(CONFIG_IPV6) 862 void rds6_inc_info_copy(struct rds_incoming *inc, 863 struct rds_info_iterator *iter, 864 struct in6_addr *saddr, struct in6_addr *daddr, 865 int flip) 866 { 867 struct rds6_info_message minfo6; 868 869 minfo6.seq = be64_to_cpu(inc->i_hdr.h_sequence); 870 minfo6.len = be32_to_cpu(inc->i_hdr.h_len); 871 minfo6.tos = inc->i_conn->c_tos; 872 873 if (flip) { 874 minfo6.laddr = *daddr; 875 minfo6.faddr = *saddr; 876 minfo6.lport = inc->i_hdr.h_dport; 877 minfo6.fport = inc->i_hdr.h_sport; 878 } else { 879 minfo6.laddr = *saddr; 880 minfo6.faddr = *daddr; 881 minfo6.lport = inc->i_hdr.h_sport; 882 minfo6.fport = inc->i_hdr.h_dport; 883 } 884 885 minfo6.flags = 0; 886 887 rds_info_copy(iter, &minfo6, sizeof(minfo6)); 888 } 889 #endif 890