linux/net/rxrpc/output.c
<<
>>
Prefs
   1/* RxRPC packet transmission
   2 *
   3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
   4 * Written by David Howells (dhowells@redhat.com)
   5 *
   6 * This program is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU General Public License
   8 * as published by the Free Software Foundation; either version
   9 * 2 of the License, or (at your option) any later version.
  10 */
  11
  12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13
  14#include <linux/net.h>
  15#include <linux/gfp.h>
  16#include <linux/skbuff.h>
  17#include <linux/export.h>
  18#include <net/sock.h>
  19#include <net/af_rxrpc.h>
  20#include "ar-internal.h"
  21
  22struct rxrpc_ack_buffer {
  23        struct rxrpc_wire_header whdr;
  24        struct rxrpc_ackpacket ack;
  25        u8 acks[255];
  26        u8 pad[3];
  27        struct rxrpc_ackinfo ackinfo;
  28};
  29
  30struct rxrpc_abort_buffer {
  31        struct rxrpc_wire_header whdr;
  32        __be32 abort_code;
  33};
  34
  35/*
  36 * Fill out an ACK packet.
  37 */
  38static size_t rxrpc_fill_out_ack(struct rxrpc_call *call,
  39                                 struct rxrpc_ack_buffer *pkt,
  40                                 rxrpc_seq_t *_hard_ack,
  41                                 rxrpc_seq_t *_top,
  42                                 u8 reason)
  43{
  44        rxrpc_serial_t serial;
  45        rxrpc_seq_t hard_ack, top, seq;
  46        int ix;
  47        u32 mtu, jmax;
  48        u8 *ackp = pkt->acks;
  49
  50        /* Barrier against rxrpc_input_data(). */
  51        serial = call->ackr_serial;
  52        hard_ack = READ_ONCE(call->rx_hard_ack);
  53        top = smp_load_acquire(&call->rx_top);
  54        *_hard_ack = hard_ack;
  55        *_top = top;
  56
  57        pkt->ack.bufferSpace    = htons(8);
  58        pkt->ack.maxSkew        = htons(call->ackr_skew);
  59        pkt->ack.firstPacket    = htonl(hard_ack + 1);
  60        pkt->ack.previousPacket = htonl(call->ackr_prev_seq);
  61        pkt->ack.serial         = htonl(serial);
  62        pkt->ack.reason         = reason;
  63        pkt->ack.nAcks          = top - hard_ack;
  64
  65        if (reason == RXRPC_ACK_PING)
  66                pkt->whdr.flags |= RXRPC_REQUEST_ACK;
  67
  68        if (after(top, hard_ack)) {
  69                seq = hard_ack + 1;
  70                do {
  71                        ix = seq & RXRPC_RXTX_BUFF_MASK;
  72                        if (call->rxtx_buffer[ix])
  73                                *ackp++ = RXRPC_ACK_TYPE_ACK;
  74                        else
  75                                *ackp++ = RXRPC_ACK_TYPE_NACK;
  76                        seq++;
  77                } while (before_eq(seq, top));
  78        }
  79
  80        mtu = call->conn->params.peer->if_mtu;
  81        mtu -= call->conn->params.peer->hdrsize;
  82        jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
  83        pkt->ackinfo.rxMTU      = htonl(rxrpc_rx_mtu);
  84        pkt->ackinfo.maxMTU     = htonl(mtu);
  85        pkt->ackinfo.rwind      = htonl(call->rx_winsize);
  86        pkt->ackinfo.jumbo_max  = htonl(jmax);
  87
  88        *ackp++ = 0;
  89        *ackp++ = 0;
  90        *ackp++ = 0;
  91        return top - hard_ack + 3;
  92}
  93
  94/*
  95 * Send an ACK call packet.
  96 */
  97int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping)
  98{
  99        struct rxrpc_connection *conn = NULL;
 100        struct rxrpc_ack_buffer *pkt;
 101        struct msghdr msg;
 102        struct kvec iov[2];
 103        rxrpc_serial_t serial;
 104        rxrpc_seq_t hard_ack, top;
 105        size_t len, n;
 106        int ret;
 107        u8 reason;
 108
 109        spin_lock_bh(&call->lock);
 110        if (call->conn)
 111                conn = rxrpc_get_connection_maybe(call->conn);
 112        spin_unlock_bh(&call->lock);
 113        if (!conn)
 114                return -ECONNRESET;
 115
 116        pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
 117        if (!pkt) {
 118                rxrpc_put_connection(conn);
 119                return -ENOMEM;
 120        }
 121
 122        msg.msg_name    = &call->peer->srx.transport;
 123        msg.msg_namelen = call->peer->srx.transport_len;
 124        msg.msg_control = NULL;
 125        msg.msg_controllen = 0;
 126        msg.msg_flags   = 0;
 127
 128        pkt->whdr.epoch         = htonl(conn->proto.epoch);
 129        pkt->whdr.cid           = htonl(call->cid);
 130        pkt->whdr.callNumber    = htonl(call->call_id);
 131        pkt->whdr.seq           = 0;
 132        pkt->whdr.type          = RXRPC_PACKET_TYPE_ACK;
 133        pkt->whdr.flags         = RXRPC_SLOW_START_OK | conn->out_clientflag;
 134        pkt->whdr.userStatus    = 0;
 135        pkt->whdr.securityIndex = call->security_ix;
 136        pkt->whdr._rsvd         = 0;
 137        pkt->whdr.serviceId     = htons(call->service_id);
 138
 139        spin_lock_bh(&call->lock);
 140        if (ping) {
 141                reason = RXRPC_ACK_PING;
 142        } else {
 143                reason = call->ackr_reason;
 144                if (!call->ackr_reason) {
 145                        spin_unlock_bh(&call->lock);
 146                        ret = 0;
 147                        goto out;
 148                }
 149                call->ackr_reason = 0;
 150        }
 151        n = rxrpc_fill_out_ack(call, pkt, &hard_ack, &top, reason);
 152
 153        spin_unlock_bh(&call->lock);
 154
 155        iov[0].iov_base = pkt;
 156        iov[0].iov_len  = sizeof(pkt->whdr) + sizeof(pkt->ack) + n;
 157        iov[1].iov_base = &pkt->ackinfo;
 158        iov[1].iov_len  = sizeof(pkt->ackinfo);
 159        len = iov[0].iov_len + iov[1].iov_len;
 160
 161        serial = atomic_inc_return(&conn->serial);
 162        pkt->whdr.serial = htonl(serial);
 163        trace_rxrpc_tx_ack(call, serial,
 164                           ntohl(pkt->ack.firstPacket),
 165                           ntohl(pkt->ack.serial),
 166                           pkt->ack.reason, pkt->ack.nAcks);
 167
 168        if (ping) {
 169                call->ping_serial = serial;
 170                smp_wmb();
 171                /* We need to stick a time in before we send the packet in case
 172                 * the reply gets back before kernel_sendmsg() completes - but
 173                 * asking UDP to send the packet can take a relatively long
 174                 * time, so we update the time after, on the assumption that
 175                 * the packet transmission is more likely to happen towards the
 176                 * end of the kernel_sendmsg() call.
 177                 */
 178                call->ping_time = ktime_get_real();
 179                set_bit(RXRPC_CALL_PINGING, &call->flags);
 180                trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
 181        }
 182
 183        ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
 184        if (ping)
 185                call->ping_time = ktime_get_real();
 186
 187        if (call->state < RXRPC_CALL_COMPLETE) {
 188                if (ret < 0) {
 189                        if (ping)
 190                                clear_bit(RXRPC_CALL_PINGING, &call->flags);
 191                        rxrpc_propose_ACK(call, pkt->ack.reason,
 192                                          ntohs(pkt->ack.maxSkew),
 193                                          ntohl(pkt->ack.serial),
 194                                          true, true,
 195                                          rxrpc_propose_ack_retry_tx);
 196                } else {
 197                        spin_lock_bh(&call->lock);
 198                        if (after(hard_ack, call->ackr_consumed))
 199                                call->ackr_consumed = hard_ack;
 200                        if (after(top, call->ackr_seen))
 201                                call->ackr_seen = top;
 202                        spin_unlock_bh(&call->lock);
 203                }
 204        }
 205
 206out:
 207        rxrpc_put_connection(conn);
 208        kfree(pkt);
 209        return ret;
 210}
 211
 212/*
 213 * Send an ABORT call packet.
 214 */
 215int rxrpc_send_abort_packet(struct rxrpc_call *call)
 216{
 217        struct rxrpc_connection *conn = NULL;
 218        struct rxrpc_abort_buffer pkt;
 219        struct msghdr msg;
 220        struct kvec iov[1];
 221        rxrpc_serial_t serial;
 222        int ret;
 223
 224        spin_lock_bh(&call->lock);
 225        if (call->conn)
 226                conn = rxrpc_get_connection_maybe(call->conn);
 227        spin_unlock_bh(&call->lock);
 228        if (!conn)
 229                return -ECONNRESET;
 230
 231        msg.msg_name    = &call->peer->srx.transport;
 232        msg.msg_namelen = call->peer->srx.transport_len;
 233        msg.msg_control = NULL;
 234        msg.msg_controllen = 0;
 235        msg.msg_flags   = 0;
 236
 237        pkt.whdr.epoch          = htonl(conn->proto.epoch);
 238        pkt.whdr.cid            = htonl(call->cid);
 239        pkt.whdr.callNumber     = htonl(call->call_id);
 240        pkt.whdr.seq            = 0;
 241        pkt.whdr.type           = RXRPC_PACKET_TYPE_ABORT;
 242        pkt.whdr.flags          = conn->out_clientflag;
 243        pkt.whdr.userStatus     = 0;
 244        pkt.whdr.securityIndex  = call->security_ix;
 245        pkt.whdr._rsvd          = 0;
 246        pkt.whdr.serviceId      = htons(call->service_id);
 247        pkt.abort_code          = htonl(call->abort_code);
 248
 249        iov[0].iov_base = &pkt;
 250        iov[0].iov_len  = sizeof(pkt);
 251
 252        serial = atomic_inc_return(&conn->serial);
 253        pkt.whdr.serial = htonl(serial);
 254
 255        ret = kernel_sendmsg(conn->params.local->socket,
 256                             &msg, iov, 1, sizeof(pkt));
 257
 258        rxrpc_put_connection(conn);
 259        return ret;
 260}
 261
 262/*
 263 * send a packet through the transport endpoint
 264 */
 265int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
 266                           bool retrans)
 267{
 268        struct rxrpc_connection *conn = call->conn;
 269        struct rxrpc_wire_header whdr;
 270        struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
 271        struct msghdr msg;
 272        struct kvec iov[2];
 273        rxrpc_serial_t serial;
 274        size_t len;
 275        bool lost = false;
 276        int ret, opt;
 277
 278        _enter(",{%d}", skb->len);
 279
 280        /* Each transmission of a Tx packet needs a new serial number */
 281        serial = atomic_inc_return(&conn->serial);
 282
 283        whdr.epoch      = htonl(conn->proto.epoch);
 284        whdr.cid        = htonl(call->cid);
 285        whdr.callNumber = htonl(call->call_id);
 286        whdr.seq        = htonl(sp->hdr.seq);
 287        whdr.serial     = htonl(serial);
 288        whdr.type       = RXRPC_PACKET_TYPE_DATA;
 289        whdr.flags      = sp->hdr.flags;
 290        whdr.userStatus = 0;
 291        whdr.securityIndex = call->security_ix;
 292        whdr._rsvd      = htons(sp->hdr._rsvd);
 293        whdr.serviceId  = htons(call->service_id);
 294
 295        if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
 296            sp->hdr.seq == 1)
 297                whdr.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
 298
 299        iov[0].iov_base = &whdr;
 300        iov[0].iov_len = sizeof(whdr);
 301        iov[1].iov_base = skb->head;
 302        iov[1].iov_len = skb->len;
 303        len = iov[0].iov_len + iov[1].iov_len;
 304
 305        msg.msg_name = &call->peer->srx.transport;
 306        msg.msg_namelen = call->peer->srx.transport_len;
 307        msg.msg_control = NULL;
 308        msg.msg_controllen = 0;
 309        msg.msg_flags = 0;
 310
 311        /* If our RTT cache needs working on, request an ACK.  Also request
 312         * ACKs if a DATA packet appears to have been lost.
 313         */
 314        if (!(sp->hdr.flags & RXRPC_LAST_PACKET) &&
 315            (retrans ||
 316             call->cong_mode == RXRPC_CALL_SLOW_START ||
 317             (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
 318             ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
 319                          ktime_get_real())))
 320                whdr.flags |= RXRPC_REQUEST_ACK;
 321
 322        if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
 323                static int lose;
 324                if ((lose++ & 7) == 7) {
 325                        ret = 0;
 326                        lost = true;
 327                        goto done;
 328                }
 329        }
 330
 331        _proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
 332
 333        /* send the packet with the don't fragment bit set if we currently
 334         * think it's small enough */
 335        if (iov[1].iov_len >= call->peer->maxdata)
 336                goto send_fragmentable;
 337
 338        down_read(&conn->params.local->defrag_sem);
 339        /* send the packet by UDP
 340         * - returns -EMSGSIZE if UDP would have to fragment the packet
 341         *   to go out of the interface
 342         *   - in which case, we'll have processed the ICMP error
 343         *     message and update the peer record
 344         */
 345        ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
 346
 347        up_read(&conn->params.local->defrag_sem);
 348        if (ret == -EMSGSIZE)
 349                goto send_fragmentable;
 350
 351done:
 352        trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags,
 353                            retrans, lost);
 354        if (ret >= 0) {
 355                ktime_t now = ktime_get_real();
 356                skb->tstamp = now;
 357                smp_wmb();
 358                sp->hdr.serial = serial;
 359                if (whdr.flags & RXRPC_REQUEST_ACK) {
 360                        call->peer->rtt_last_req = now;
 361                        trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
 362                }
 363        }
 364        _leave(" = %d [%u]", ret, call->peer->maxdata);
 365        return ret;
 366
 367send_fragmentable:
 368        /* attempt to send this message with fragmentation enabled */
 369        _debug("send fragment");
 370
 371        down_write(&conn->params.local->defrag_sem);
 372
 373        switch (conn->params.local->srx.transport.family) {
 374        case AF_INET:
 375                opt = IP_PMTUDISC_DONT;
 376                ret = kernel_setsockopt(conn->params.local->socket,
 377                                        SOL_IP, IP_MTU_DISCOVER,
 378                                        (char *)&opt, sizeof(opt));
 379                if (ret == 0) {
 380                        ret = kernel_sendmsg(conn->params.local->socket, &msg,
 381                                             iov, 2, len);
 382
 383                        opt = IP_PMTUDISC_DO;
 384                        kernel_setsockopt(conn->params.local->socket, SOL_IP,
 385                                          IP_MTU_DISCOVER,
 386                                          (char *)&opt, sizeof(opt));
 387                }
 388                break;
 389
 390#ifdef CONFIG_AF_RXRPC_IPV6
 391        case AF_INET6:
 392                opt = IPV6_PMTUDISC_DONT;
 393                ret = kernel_setsockopt(conn->params.local->socket,
 394                                        SOL_IPV6, IPV6_MTU_DISCOVER,
 395                                        (char *)&opt, sizeof(opt));
 396                if (ret == 0) {
 397                        ret = kernel_sendmsg(conn->params.local->socket, &msg,
 398                                             iov, 1, iov[0].iov_len);
 399
 400                        opt = IPV6_PMTUDISC_DO;
 401                        kernel_setsockopt(conn->params.local->socket,
 402                                          SOL_IPV6, IPV6_MTU_DISCOVER,
 403                                          (char *)&opt, sizeof(opt));
 404                }
 405                break;
 406#endif
 407        }
 408
 409        up_write(&conn->params.local->defrag_sem);
 410        goto done;
 411}
 412
 413/*
 414 * reject packets through the local endpoint
 415 */
 416void rxrpc_reject_packets(struct rxrpc_local *local)
 417{
 418        struct sockaddr_rxrpc srx;
 419        struct rxrpc_skb_priv *sp;
 420        struct rxrpc_wire_header whdr;
 421        struct sk_buff *skb;
 422        struct msghdr msg;
 423        struct kvec iov[2];
 424        size_t size;
 425        __be32 code;
 426
 427        _enter("%d", local->debug_id);
 428
 429        iov[0].iov_base = &whdr;
 430        iov[0].iov_len = sizeof(whdr);
 431        iov[1].iov_base = &code;
 432        iov[1].iov_len = sizeof(code);
 433        size = sizeof(whdr) + sizeof(code);
 434
 435        msg.msg_name = &srx.transport;
 436        msg.msg_control = NULL;
 437        msg.msg_controllen = 0;
 438        msg.msg_flags = 0;
 439
 440        memset(&whdr, 0, sizeof(whdr));
 441        whdr.type = RXRPC_PACKET_TYPE_ABORT;
 442
 443        while ((skb = skb_dequeue(&local->reject_queue))) {
 444                rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
 445                sp = rxrpc_skb(skb);
 446
 447                if (rxrpc_extract_addr_from_skb(local, &srx, skb) == 0) {
 448                        msg.msg_namelen = srx.transport_len;
 449
 450                        code = htonl(skb->priority);
 451
 452                        whdr.epoch      = htonl(sp->hdr.epoch);
 453                        whdr.cid        = htonl(sp->hdr.cid);
 454                        whdr.callNumber = htonl(sp->hdr.callNumber);
 455                        whdr.serviceId  = htons(sp->hdr.serviceId);
 456                        whdr.flags      = sp->hdr.flags;
 457                        whdr.flags      ^= RXRPC_CLIENT_INITIATED;
 458                        whdr.flags      &= RXRPC_CLIENT_INITIATED;
 459
 460                        kernel_sendmsg(local->socket, &msg, iov, 2, size);
 461                }
 462
 463                rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
 464        }
 465
 466        _leave("");
 467}
 468