serve_image.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #define PROGRAM_NAME "serve_image"
  2. #define _POSIX_C_SOURCE 200112L
  3. #include <time.h>
  4. #include <errno.h>
  5. #include <netdb.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <sys/socket.h>
  14. #include <sys/mman.h>
  15. #include <netinet/in.h>
  16. #include <sys/time.h>
  17. #include <crc32.h>
  18. #include <inttypes.h>
  19. #include <common.h>
  20. #include "mcast_image.h"
  21. #include "libfec.h"
  22. int tx_rate = 80000;
  23. int pkt_delay;
  24. #undef RANDOMDROP
  25. int main(int argc, char **argv)
  26. {
  27. struct addrinfo *ai;
  28. struct addrinfo hints;
  29. struct addrinfo *runp;
  30. int ret;
  31. int sock;
  32. struct image_pkt pktbuf;
  33. int rfd;
  34. struct stat st;
  35. int writeerrors = 0;
  36. uint32_t erasesize;
  37. unsigned char *image, *blockptr = NULL;
  38. uint32_t block_nr, pkt_nr;
  39. int nr_blocks;
  40. struct timeval then, now, nextpkt;
  41. long time_msecs;
  42. int pkts_per_block;
  43. int total_pkts_per_block;
  44. struct fec_parms *fec;
  45. unsigned char *last_block;
  46. uint32_t *block_crcs;
  47. long tosleep;
  48. uint32_t sequence = 0;
  49. if (argc == 6) {
  50. tx_rate = atol(argv[5]) * 1024;
  51. if (tx_rate < PKT_SIZE || tx_rate > 20000000) {
  52. fprintf(stderr, "Bogus TX rate %d KiB/s\n", tx_rate);
  53. exit(1);
  54. }
  55. argc = 5;
  56. }
  57. if (argc != 5) {
  58. fprintf(stderr, "usage: %s <host> <port> <image> <erasesize> [<tx_rate>]\n",
  59. PROGRAM_NAME);
  60. exit(1);
  61. }
  62. pkt_delay = (sizeof(pktbuf) * 1000000) / tx_rate;
  63. printf("Inter-packet delay (avg): %dµs\n", pkt_delay);
  64. printf("Transmit rate: %d KiB/s\n", tx_rate / 1024);
  65. erasesize = atol(argv[4]);
  66. if (!erasesize) {
  67. fprintf(stderr, "erasesize cannot be zero\n");
  68. exit(1);
  69. }
  70. pkts_per_block = (erasesize + PKT_SIZE - 1) / PKT_SIZE;
  71. total_pkts_per_block = pkts_per_block * 3 / 2;
  72. /* We have to pad it with zeroes, so can't use it in-place */
  73. last_block = malloc(pkts_per_block * PKT_SIZE);
  74. if (!last_block) {
  75. fprintf(stderr, "Failed to allocate last-block buffer\n");
  76. exit(1);
  77. }
  78. fec = fec_new(pkts_per_block, total_pkts_per_block);
  79. if (!fec) {
  80. fprintf(stderr, "Error initialising FEC\n");
  81. exit(1);
  82. }
  83. memset(&hints, 0, sizeof(hints));
  84. hints.ai_flags = AI_ADDRCONFIG;
  85. hints.ai_socktype = SOCK_DGRAM;
  86. ret = getaddrinfo(argv[1], argv[2], &hints, &ai);
  87. if (ret) {
  88. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
  89. exit(1);
  90. }
  91. runp = ai;
  92. for (runp = ai; runp; runp = runp->ai_next) {
  93. sock = socket(runp->ai_family, runp->ai_socktype,
  94. runp->ai_protocol);
  95. if (sock == -1) {
  96. perror("socket");
  97. continue;
  98. }
  99. if (connect(sock, runp->ai_addr, runp->ai_addrlen) == 0)
  100. break;
  101. perror("connect");
  102. close(sock);
  103. }
  104. if (!runp)
  105. exit(1);
  106. rfd = open(argv[3], O_RDONLY);
  107. if (rfd < 0) {
  108. perror("open");
  109. exit(1);
  110. }
  111. if (fstat(rfd, &st)) {
  112. perror("fstat");
  113. exit(1);
  114. }
  115. if (st.st_size % erasesize) {
  116. fprintf(stderr, "Image size %lld bytes is not a multiple of erasesize %d bytes\n",
  117. (long long)st.st_size, erasesize);
  118. exit(1);
  119. }
  120. image = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, rfd, 0);
  121. if (image == MAP_FAILED) {
  122. perror("mmap");
  123. exit(1);
  124. }
  125. nr_blocks = st.st_size / erasesize;
  126. block_crcs = malloc(nr_blocks * sizeof(uint32_t));
  127. if (!block_crcs) {
  128. fprintf(stderr, "Failed to allocate memory for CRCs\n");
  129. exit(1);
  130. }
  131. memcpy(last_block, image + (nr_blocks - 1) * erasesize, erasesize);
  132. memset(last_block + erasesize, 0, (PKT_SIZE * pkts_per_block) - erasesize);
  133. printf("Checking CRC....");
  134. fflush(stdout);
  135. pktbuf.hdr.resend = 0;
  136. pktbuf.hdr.totcrc = htonl(mtd_crc32(-1, image, st.st_size));
  137. pktbuf.hdr.nr_blocks = htonl(nr_blocks);
  138. pktbuf.hdr.blocksize = htonl(erasesize);
  139. pktbuf.hdr.thislen = htonl(PKT_SIZE);
  140. pktbuf.hdr.nr_pkts = htons(total_pkts_per_block);
  141. printf("%08x\n", ntohl(pktbuf.hdr.totcrc));
  142. printf("Checking block CRCs....");
  143. fflush(stdout);
  144. for (block_nr=0; block_nr < nr_blocks; block_nr++) {
  145. printf("\rChecking block CRCS.... %d/%d",
  146. block_nr + 1, nr_blocks);
  147. fflush(stdout);
  148. block_crcs[block_nr] = mtd_crc32(-1, image + (block_nr * erasesize), erasesize);
  149. }
  150. printf("\nImage size %ld KiB (0x%08lx). %d blocks at %d pkts/block\n"
  151. "Estimated transmit time per cycle: %ds\n",
  152. (long)st.st_size / 1024, (long) st.st_size,
  153. nr_blocks, pkts_per_block,
  154. nr_blocks * pkts_per_block * pkt_delay / 1000000);
  155. gettimeofday(&then, NULL);
  156. nextpkt = then;
  157. #ifdef RANDOMDROP
  158. srand((unsigned)then.tv_usec);
  159. printf("Random seed %u\n", (unsigned)then.tv_usec);
  160. #endif
  161. while (1) for (pkt_nr=0; pkt_nr < total_pkts_per_block; pkt_nr++) {
  162. if (blockptr && pkt_nr == 0) {
  163. unsigned long amt_sent = total_pkts_per_block * nr_blocks * sizeof(pktbuf);
  164. gettimeofday(&now, NULL);
  165. time_msecs = (now.tv_sec - then.tv_sec) * 1000;
  166. time_msecs += ((int)(now.tv_usec - then.tv_usec)) / 1000;
  167. printf("\n%ld KiB sent in %ldms (%ld KiB/s)\n",
  168. amt_sent / 1024, time_msecs,
  169. amt_sent / 1024 * 1000 / time_msecs);
  170. then = now;
  171. }
  172. for (block_nr = 0; block_nr < nr_blocks; block_nr++) {
  173. int actualpkt;
  174. /* Calculating the redundant FEC blocks is expensive;
  175. the first $pkts_per_block are cheap enough though
  176. because they're just copies. So alternate between
  177. simple and complex stuff, so that we don't start
  178. to choke and fail to keep up with the expected
  179. bitrate in the second half of the sequence */
  180. if (block_nr & 1)
  181. actualpkt = pkt_nr;
  182. else
  183. actualpkt = total_pkts_per_block - 1 - pkt_nr;
  184. blockptr = image + (erasesize * block_nr);
  185. if (block_nr == nr_blocks - 1)
  186. blockptr = last_block;
  187. fec_encode_linear(fec, blockptr, pktbuf.data, actualpkt, PKT_SIZE);
  188. pktbuf.hdr.thiscrc = htonl(mtd_crc32(-1, pktbuf.data, PKT_SIZE));
  189. pktbuf.hdr.block_crc = htonl(block_crcs[block_nr]);
  190. pktbuf.hdr.block_nr = htonl(block_nr);
  191. pktbuf.hdr.pkt_nr = htons(actualpkt);
  192. pktbuf.hdr.pkt_sequence = htonl(sequence++);
  193. printf("\rSending data block %08x packet %3d/%d",
  194. block_nr * erasesize,
  195. pkt_nr, total_pkts_per_block);
  196. if (pkt_nr && !block_nr) {
  197. unsigned long amt_sent = pkt_nr * nr_blocks * sizeof(pktbuf);
  198. gettimeofday(&now, NULL);
  199. time_msecs = (now.tv_sec - then.tv_sec) * 1000;
  200. time_msecs += ((int)(now.tv_usec - then.tv_usec)) / 1000;
  201. printf(" (%ld KiB/s) ",
  202. amt_sent / 1024 * 1000 / time_msecs);
  203. }
  204. fflush(stdout);
  205. #ifdef RANDOMDROP
  206. if ((rand() % 1000) < 20) {
  207. printf("\nDropping packet %d of block %08x\n", pkt_nr+1, block_nr * erasesize);
  208. continue;
  209. }
  210. #endif
  211. gettimeofday(&now, NULL);
  212. #if 1
  213. tosleep = nextpkt.tv_usec - now.tv_usec +
  214. (1000000 * (nextpkt.tv_sec - now.tv_sec));
  215. /* We need hrtimers for this to actually work */
  216. if (tosleep > 0) {
  217. struct timespec req;
  218. req.tv_nsec = (tosleep % 1000000) * 1000;
  219. req.tv_sec = tosleep / 1000000;
  220. nanosleep(&req, NULL);
  221. }
  222. #else
  223. while (now.tv_sec < nextpkt.tv_sec ||
  224. (now.tv_sec == nextpkt.tv_sec &&
  225. now.tv_usec < nextpkt.tv_usec)) {
  226. gettimeofday(&now, NULL);
  227. }
  228. #endif
  229. nextpkt.tv_usec += pkt_delay;
  230. if (nextpkt.tv_usec >= 1000000) {
  231. nextpkt.tv_sec += nextpkt.tv_usec / 1000000;
  232. nextpkt.tv_usec %= 1000000;
  233. }
  234. /* If the time for the next packet has already
  235. passed (by some margin), then we've lost time
  236. Adjust our expected timings accordingly. If
  237. we're only a little way behind, don't slip yet */
  238. if (now.tv_usec > (now.tv_usec + (5 * pkt_delay) +
  239. 1000000 * (nextpkt.tv_sec - now.tv_sec))) {
  240. nextpkt = now;
  241. }
  242. if (write(sock, &pktbuf, sizeof(pktbuf)) < 0) {
  243. perror("write");
  244. writeerrors++;
  245. if (writeerrors > 10) {
  246. fprintf(stderr, "Too many consecutive write errors\n");
  247. exit(1);
  248. }
  249. } else
  250. writeerrors = 0;
  251. }
  252. }
  253. munmap(image, st.st_size);
  254. close(rfd);
  255. close(sock);
  256. return 0;
  257. }