cfr_test_app.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2019 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /**
  19. * DOC: cfr capture test application
  20. * This file provides test application to dump cfr capture from driver
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <getopt.h>
  35. #include <stdint.h>
  36. #include <sys/time.h>
  37. #include <time.h>
  38. #include <signal.h>
  39. #define CFR_DUMP_STREAMFS_FILE "/sys/kernel/debug/%s/cfr_dump0"
  40. #define CFR_DUMP_FILE "/tmp/cfr_dump_%s.bin"
  41. #define MAX_CAPTURE_SIZE 2048
  42. static char rbuffer[MAX_CAPTURE_SIZE];
  43. int stop_capture;
  44. void print_usage(char *argv[])
  45. {
  46. printf("Usage:cfr_test_app -i <interfacename>\n");
  47. }
  48. void streamfs_read_handler(int sfd, int cfd)
  49. {
  50. int rlen = 0;
  51. memset(rbuffer, 0, sizeof(rbuffer));
  52. rlen = read(sfd, rbuffer, sizeof(rbuffer));
  53. if (rlen <= 0)
  54. return;
  55. write(cfd, rbuffer, rlen);
  56. }
  57. int start_cfr_capture_daemon(int streamfs_fd, int cfr_dump_fd)
  58. {
  59. fd_set sfs_fdset;
  60. int maxfd = 0, retval = 0;
  61. FD_ZERO(&sfs_fdset);
  62. FD_SET(streamfs_fd, &sfs_fdset);
  63. maxfd = streamfs_fd;
  64. while (!stop_capture) {
  65. retval = select(maxfd + 1, &sfs_fdset, NULL, NULL, NULL);
  66. if (retval < 0) {
  67. perror("select()");
  68. exit(EXIT_FAILURE);
  69. }
  70. if (FD_ISSET(streamfs_fd, &sfs_fdset))
  71. streamfs_read_handler(streamfs_fd, cfr_dump_fd);
  72. }
  73. close(streamfs_fd);
  74. close(cfr_dump_fd);
  75. return 0;
  76. }
  77. int open_streamfs_file(char *iface)
  78. {
  79. int fd = -1;
  80. char filename[128];
  81. snprintf(filename, sizeof(filename), CFR_DUMP_STREAMFS_FILE, iface);
  82. fd = open(filename, O_RDONLY);
  83. return fd;
  84. }
  85. int initialize_cfr_dump_file(char *iface)
  86. {
  87. int fd = -1;
  88. static char filename[128];
  89. char time[50] = {0};
  90. struct timeval tv = {0};
  91. time_t curtime = 0;
  92. struct tm *tm_val = NULL;
  93. gettimeofday(&tv, NULL);
  94. curtime = tv.tv_sec;
  95. tm_val = localtime(&curtime);
  96. if (tm_val) {
  97. strftime(time, 50, "%Y_%m_%d_%T", tm_val);
  98. snprintf(filename, sizeof(filename), CFR_DUMP_FILE, time);
  99. fd = open(filename, O_WRONLY | O_CREAT);
  100. } else {
  101. perror("Unable to get time value to generate filename \n");
  102. }
  103. return fd;
  104. }
  105. /* Signal Hnadler to overide CTRL+C */
  106. void sig_handler(int signum)
  107. {
  108. stop_capture = 1;
  109. }
  110. int main(int argc, char *argv[])
  111. {
  112. int option = 0;
  113. char *iface = NULL;
  114. int streamfs_fd = -1, cfr_dump_fd = -1;
  115. while ((option = getopt(argc, argv, "i:")) != -1) {
  116. switch (option) {
  117. case 'i':
  118. iface = optarg;
  119. break;
  120. default:
  121. printf("Invalid argument\n");
  122. print_usage(argv);
  123. exit(EXIT_FAILURE);
  124. break;
  125. }
  126. }
  127. if (iface == NULL) {
  128. printf("Invalid interface option\n");
  129. exit(EXIT_FAILURE);
  130. }
  131. streamfs_fd = open_streamfs_file(iface);
  132. if (streamfs_fd < 0) {
  133. printf("Invalid interface: %s, CFR capture file not found\n", iface);
  134. exit(EXIT_FAILURE);
  135. }
  136. cfr_dump_fd = initialize_cfr_dump_file(iface);
  137. if (cfr_dump_fd < 0) {
  138. printf(" Could not open CFR dump file for write\n");
  139. close(streamfs_fd);
  140. exit(EXIT_FAILURE);
  141. }
  142. printf("Starting CFR capture deamon, Press CTRL+C to exit \n");
  143. signal(SIGINT, sig_handler);
  144. start_cfr_capture_daemon(streamfs_fd, cfr_dump_fd);
  145. return 0;
  146. }