netlog.c 2.5 KB

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