skbuff.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* ar-skbuff.c: socket buffer destruction handling
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/net.h>
  14. #include <linux/skbuff.h>
  15. #include <net/sock.h>
  16. #include <net/af_rxrpc.h>
  17. #include "ar-internal.h"
  18. /*
  19. * set up for the ACK at the end of the receive phase when we discard the final
  20. * receive phase data packet
  21. * - called with softirqs disabled
  22. */
  23. static void rxrpc_request_final_ACK(struct rxrpc_call *call)
  24. {
  25. /* the call may be aborted before we have a chance to ACK it */
  26. write_lock(&call->state_lock);
  27. switch (call->state) {
  28. case RXRPC_CALL_CLIENT_RECV_REPLY:
  29. call->state = RXRPC_CALL_CLIENT_FINAL_ACK;
  30. _debug("request final ACK");
  31. /* get an extra ref on the call for the final-ACK generator to
  32. * release */
  33. rxrpc_get_call(call);
  34. set_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events);
  35. if (try_to_del_timer_sync(&call->ack_timer) >= 0)
  36. rxrpc_queue_call(call);
  37. break;
  38. case RXRPC_CALL_SERVER_RECV_REQUEST:
  39. call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
  40. default:
  41. break;
  42. }
  43. write_unlock(&call->state_lock);
  44. }
  45. /*
  46. * drop the bottom ACK off of the call ACK window and advance the window
  47. */
  48. static void rxrpc_hard_ACK_data(struct rxrpc_call *call,
  49. struct rxrpc_skb_priv *sp)
  50. {
  51. int loop;
  52. u32 seq;
  53. spin_lock_bh(&call->lock);
  54. _debug("hard ACK #%u", sp->hdr.seq);
  55. for (loop = 0; loop < RXRPC_ACKR_WINDOW_ASZ; loop++) {
  56. call->ackr_window[loop] >>= 1;
  57. call->ackr_window[loop] |=
  58. call->ackr_window[loop + 1] << (BITS_PER_LONG - 1);
  59. }
  60. seq = sp->hdr.seq;
  61. ASSERTCMP(seq, ==, call->rx_data_eaten + 1);
  62. call->rx_data_eaten = seq;
  63. if (call->ackr_win_top < UINT_MAX)
  64. call->ackr_win_top++;
  65. ASSERTIFCMP(call->state <= RXRPC_CALL_COMPLETE,
  66. call->rx_data_post, >=, call->rx_data_recv);
  67. ASSERTIFCMP(call->state <= RXRPC_CALL_COMPLETE,
  68. call->rx_data_recv, >=, call->rx_data_eaten);
  69. if (sp->hdr.flags & RXRPC_LAST_PACKET) {
  70. rxrpc_request_final_ACK(call);
  71. } else if (atomic_dec_and_test(&call->ackr_not_idle) &&
  72. test_and_clear_bit(RXRPC_CALL_TX_SOFT_ACK, &call->flags)) {
  73. /* We previously soft-ACK'd some received packets that have now
  74. * been consumed, so send a hard-ACK if no more packets are
  75. * immediately forthcoming to allow the transmitter to free up
  76. * its Tx bufferage.
  77. */
  78. _debug("send Rx idle ACK");
  79. __rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, sp->hdr.serial,
  80. false);
  81. }
  82. spin_unlock_bh(&call->lock);
  83. }
  84. /**
  85. * rxrpc_kernel_data_consumed - Record consumption of data message
  86. * @call: The call to which the message pertains.
  87. * @skb: Message holding data
  88. *
  89. * Record the consumption of a data message and generate an ACK if appropriate.
  90. * The call state is shifted if this was the final packet. The caller must be
  91. * in process context with no spinlocks held.
  92. *
  93. * TODO: Actually generate the ACK here rather than punting this to the
  94. * workqueue.
  95. */
  96. void rxrpc_kernel_data_consumed(struct rxrpc_call *call, struct sk_buff *skb)
  97. {
  98. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  99. _enter("%d,%p{%u}", call->debug_id, skb, sp->hdr.seq);
  100. ASSERTCMP(sp->call, ==, call);
  101. ASSERTCMP(sp->hdr.type, ==, RXRPC_PACKET_TYPE_DATA);
  102. /* TODO: Fix the sequence number tracking */
  103. ASSERTCMP(sp->hdr.seq, >=, call->rx_data_recv);
  104. ASSERTCMP(sp->hdr.seq, <=, call->rx_data_recv + 1);
  105. ASSERTCMP(sp->hdr.seq, >, call->rx_data_eaten);
  106. call->rx_data_recv = sp->hdr.seq;
  107. rxrpc_hard_ACK_data(call, sp);
  108. }
  109. EXPORT_SYMBOL(rxrpc_kernel_data_consumed);
  110. /*
  111. * Destroy a packet that has an RxRPC control buffer
  112. */
  113. void rxrpc_packet_destructor(struct sk_buff *skb)
  114. {
  115. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  116. struct rxrpc_call *call = sp->call;
  117. _enter("%p{%p}", skb, call);
  118. if (call) {
  119. if (atomic_dec_return(&call->skb_count) < 0)
  120. BUG();
  121. rxrpc_put_call(call);
  122. sp->call = NULL;
  123. }
  124. if (skb->sk)
  125. sock_rfree(skb);
  126. _leave("");
  127. }
  128. /**
  129. * rxrpc_kernel_free_skb - Free an RxRPC socket buffer
  130. * @skb: The socket buffer to be freed
  131. *
  132. * Let RxRPC free its own socket buffer, permitting it to maintain debug
  133. * accounting.
  134. */
  135. void rxrpc_kernel_free_skb(struct sk_buff *skb)
  136. {
  137. rxrpc_free_skb(skb);
  138. }
  139. EXPORT_SYMBOL(rxrpc_kernel_free_skb);