txring.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* $Id$ */
  2. /* Copyright (c) 2010 Dmitriy Gerasimov <gesser@demlabs.ru>
  3. * Copyright (c) 2010 Aaron Turner.
  4. * Copyright (c) 2023 Fred Klassen - AppNet Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright owners nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  26. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  28. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "defines.h"
  33. #include "config.h"
  34. #ifdef HAVE_TX_RING
  35. #include "txring.h"
  36. #include "err.h"
  37. #include "utils.h"
  38. #include <errno.h>
  39. #include <string.h>
  40. #include <sys/mman.h>
  41. #include <unistd.h>
  42. volatile int shutdown_flag = 0;
  43. int tdata_offset = TPACKET_HDRLEN - sizeof(struct sockaddr_ll);
  44. /**
  45. * This task will call send() procedure
  46. */
  47. void *
  48. txring_send(void *arg)
  49. {
  50. int ec_send;
  51. static int total = 0;
  52. int fd_socket = (int)arg;
  53. do {
  54. /* send all buffers with TP_STATUS_SEND_REQUEST */
  55. ec_send = sendto(fd_socket, NULL, 0, MSG_DONTWAIT, (struct sockaddr *)NULL, sizeof(struct sockaddr_ll));
  56. if (ec_send > 0) {
  57. total += ec_send;
  58. dbgx(2, "Sent %d bytes (+%d bytes)", total, ec_send);
  59. } else {
  60. /* nothing to do => schedule : useful if no SMP */
  61. usleep(100);
  62. }
  63. } while (!shutdown_flag);
  64. // if(blocking) printf("end of task send()\n");
  65. // printf("end of task send(ec=%x)\n", ec_send);
  66. return (void *)ec_send;
  67. }
  68. /**
  69. * Put data in TX ring buffer and rotate it if necessary
  70. */
  71. int
  72. txring_put(txring_t *txp, const void *data, size_t length)
  73. {
  74. struct tpacket_hdr *ps_header;
  75. char *to_data;
  76. int loop = 1;
  77. int first_loop = 1;
  78. unsigned int start_index = txp->tx_index;
  79. do {
  80. ps_header = ((struct tpacket_hdr *)((void *)txp->tx_head + (txp->treq->tp_frame_size * txp->tx_index)));
  81. to_data = ((void *)ps_header) + tdata_offset;
  82. switch ((volatile uint32_t)ps_header->tp_status) {
  83. case TP_STATUS_WRONG_FORMAT:
  84. warnx("TP_STATUS_WRONG_FORMAT occures O_o. Frame %d, pkt len %d\n", txp->tx_index, length);
  85. break;
  86. case TP_STATUS_AVAILABLE:
  87. if (length > txp->treq->tp_frame_size) {
  88. // TODO Fragment packet
  89. warnx("[!] %d bytes from %d packet truncated\n", length - txp->treq->tp_frame_size, length);
  90. length = txp->treq->tp_frame_size;
  91. }
  92. memcpy(to_data, data, length);
  93. ps_header->tp_len = length;
  94. ps_header->tp_status = TP_STATUS_SEND_REQUEST;
  95. loop = 0;
  96. break;
  97. default:
  98. dbgx(2,
  99. "TPACKET status %u at frame %d with length %d\n",
  100. ps_header->tp_status,
  101. txp->tx_index,
  102. ps_header->tp_len);
  103. usleep(0);
  104. break;
  105. }
  106. txp->tx_index++;
  107. if (txp->tx_index >= txp->treq->tp_frame_nr) {
  108. txp->tx_index = 0;
  109. first_loop = 0;
  110. }
  111. /* check if we've ran over all ring */
  112. if ((txp->tx_index == start_index) && !first_loop) {
  113. errno = ENOBUFS;
  114. return -1;
  115. }
  116. } while (loop == 1);
  117. return ps_header->tp_len;
  118. }
  119. /**
  120. * \brief Build TX ring buffer request structure
  121. *
  122. * This builds a ring buffer request structure making sure
  123. * that we have buffers big enough so that a frame which
  124. * is the size of the MTU doesn't get truncated. We also
  125. * need to structure things with minimum memory wastage
  126. */
  127. void
  128. txring_mkreq(struct tpacket_req *treq, unsigned int mtu)
  129. {
  130. unsigned int pg, bs;
  131. unsigned int s;
  132. unsigned int mult = 1;
  133. unsigned nr_blocks = 1000;
  134. bs = pg = getpagesize();
  135. s = mtu + TPACKET_HDRLEN;
  136. memset(treq, 0, sizeof(struct tpacket_req));
  137. if (bs <= s) {
  138. while (bs < s) {
  139. bs += pg;
  140. mult++;
  141. }
  142. treq->tp_block_size = bs;
  143. treq->tp_frame_size = bs / mult;
  144. treq->tp_block_nr = nr_blocks;
  145. treq->tp_frame_nr = mult * nr_blocks;
  146. } else {
  147. while ((s * (mult + 1)) <= pg) {
  148. mult++;
  149. }
  150. treq->tp_block_size = pg;
  151. treq->tp_frame_size = pg / mult;
  152. treq->tp_block_nr = nr_blocks;
  153. treq->tp_frame_nr = mult * nr_blocks;
  154. }
  155. dbgx(1,
  156. "txring: block_size=%d block_nr=%d frame_size=%d frame_nr=%d",
  157. treq->tp_block_size,
  158. treq->tp_block_nr,
  159. treq->tp_frame_size,
  160. treq->tp_frame_nr);
  161. }
  162. /**
  163. * \brief Create TX ring for socket and init indexes
  164. *
  165. * Creates our pthread for sending, currently hardcoded for priority = 20
  166. */
  167. txring_t *
  168. txring_init(int fd, unsigned int mtu)
  169. {
  170. pthread_attr_t t_attr_send;
  171. struct sched_param para_send;
  172. int mode_loss = 0;
  173. txring_t *txp;
  174. /* allocate memory for structure and fill it with different stuff*/
  175. txp = (txring_t *)safe_malloc(sizeof(txring_t));
  176. txp->treq = (struct tpacket_req *)safe_malloc(sizeof(struct tpacket_req));
  177. txring_mkreq(txp->treq, mtu);
  178. txp->tx_size = txp->treq->tp_block_size * txp->treq->tp_block_nr;
  179. txp->tx_index = 0; /* Set index on start*/
  180. /* Set PACKET_LOSS sockoption */
  181. if (setsockopt(fd, SOL_PACKET, PACKET_LOSS, (char *)&mode_loss, sizeof(mode_loss)) < 0) {
  182. perror("setsockopt: PACKET_LOSS");
  183. return NULL;
  184. }
  185. /* Enable TX Ring */
  186. if (setsockopt(fd, SOL_PACKET, PACKET_TX_RING, (char *)txp->treq, sizeof(struct tpacket_req)) < 0) {
  187. perror("Can't setsockopt PACKET_TX_RING");
  188. return NULL;
  189. }
  190. /* mmap unswapped memory with TX ring buffer*/
  191. txp->tx_head = mmap(0, txp->tx_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  192. if (txp->tx_head == MAP_FAILED) {
  193. perror("mmap() failed ");
  194. return NULL;
  195. }
  196. /* Start poll thread*/
  197. pthread_attr_init(&t_attr_send);
  198. pthread_attr_setschedpolicy(&t_attr_send, SCHED_RR);
  199. para_send.sched_priority = 20;
  200. pthread_attr_setschedparam(&t_attr_send, &para_send);
  201. if (pthread_create(&txp->tx_send, &t_attr_send, txring_send, (void *)fd) != 0) {
  202. perror("pthread_create() failed\n");
  203. abort();
  204. }
  205. return txp;
  206. }
  207. #endif /* HAVE_TX_RING */