netlog.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* netlog: see netlog.h. */
  2. #include "net.h"
  3. #include "netcore.h"
  4. #include "netdev.h"
  5. #include "tcp.h"
  6. #include "timer.h"
  7. #include "netlog.h"
  8. #define NETLOG_RING 4096 /* producer/consumer byte ring */
  9. #define NETLOG_FLUSH_MIN 256 /* flush even without a newline at this size */
  10. #define NETLOG_IDLE_MS 100 /* flush a partial line after this idle time */
  11. static volatile unsigned char ring[NETLOG_RING];
  12. static volatile unsigned int head;
  13. static volatile unsigned int tail;
  14. static volatile unsigned int dropped;
  15. static volatile unsigned int last_append_ms;
  16. static int enabled;
  17. static unsigned char host_ip[4] = { 255, 255, 255, 255 };
  18. void netlog_init(void)
  19. {
  20. head = tail = 0;
  21. dropped = 0;
  22. last_append_ms = get_ticks_ms();
  23. enabled = 1;
  24. }
  25. void netlog_set_enabled(int on)
  26. {
  27. enabled = on ? 1 : 0;
  28. }
  29. int netlog_get_enabled(void)
  30. {
  31. return enabled;
  32. }
  33. void netlog_set_host(const unsigned char ip[4])
  34. {
  35. unsigned int i;
  36. for (i = 0; i < 4; i++)
  37. host_ip[i] = ip[i];
  38. }
  39. void netlog_get_host(unsigned char ip[4])
  40. {
  41. unsigned int i;
  42. for (i = 0; i < 4; i++)
  43. ip[i] = host_ip[i];
  44. }
  45. void netlog_char(char c)
  46. {
  47. unsigned int next;
  48. if (!enabled)
  49. return;
  50. next = (head + 1) % NETLOG_RING;
  51. if (next == tail) {
  52. dropped++; /* full: drop, never block */
  53. return;
  54. }
  55. ring[head] = (unsigned char)c;
  56. head = next;
  57. last_append_ms = get_ticks_ms();
  58. }
  59. static unsigned int pending(void)
  60. {
  61. if (head >= tail)
  62. return head - tail;
  63. return NETLOG_RING - tail + head;
  64. }
  65. static void send_line(const unsigned char *buf, unsigned int len)
  66. {
  67. static const unsigned char bcast_mac[6] = {
  68. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  69. };
  70. const struct netdev *nd = netdev_mgmt();
  71. unsigned char src[4], dst[4];
  72. if (!nd)
  73. return;
  74. netlog_get_host(dst);
  75. /* Use the address the interface actually has: after DHCP the
  76. * driver's static default is no longer what is on the wire, and a
  77. * source address from another subnet is dropped by some paths. */
  78. net_get_ip(src);
  79. net_udp_send_from_dev(nd, src, bcast_mac, dst,
  80. NETLOG_PORT, NETLOG_PORT, buf, len);
  81. }
  82. void netlog_flush(void)
  83. {
  84. unsigned char buf[NET_UDP_MAX];
  85. unsigned int n = 0, avail, idle, now;
  86. static const char dropmsg[] = "[netlog: output dropped, buffer full]\n";
  87. unsigned int i;
  88. if (!enabled || netdev_in_use() || tcp_client_count() != 0 ||
  89. net_stack_busy() || !netdev_mgmt())
  90. return;
  91. avail = pending();
  92. if (avail == 0)
  93. return;
  94. now = get_ticks_ms();
  95. idle = now - last_append_ms;
  96. if (avail < NETLOG_FLUSH_MIN && idle < NETLOG_IDLE_MS)
  97. return; /* wait for a complete line */
  98. if (dropped) {
  99. send_line((const unsigned char *)dropmsg, sizeof(dropmsg) - 1);
  100. dropped = 0;
  101. }
  102. /* One datagram, ending at a newline when possible. */
  103. while (n < NET_UDP_MAX && tail != head) {
  104. unsigned char c = ring[tail];
  105. tail = (tail + 1) % NETLOG_RING;
  106. buf[n++] = c;
  107. if (c == '\n')
  108. break;
  109. }
  110. if (n == 0)
  111. return;
  112. send_line(buf, n);
  113. (void)i;
  114. }