netcore.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Shared kernel receive pump: see netcore.h. */
  2. #include "netcore.h"
  3. #include "dns.h"
  4. #include "net.h"
  5. #include "netdev.h"
  6. #include "netkexec.h"
  7. #include "process.h"
  8. #include "spinlock.h"
  9. #include "tcp.h"
  10. /* Reply scratch for ARP/ICMP answered by the pump. The received frame is
  11. * still in netdev_rx_scratch(), so the reply cannot reuse it. */
  12. static unsigned char reply_buf[NETDEV_FRAME_MAX];
  13. /* Stack operations that may sleep between pumps, tracked per process so a
  14. * killed waiter cannot leave the netkexec/netlog gates stuck. */
  15. #define NET_STACK_MAX 8
  16. static struct {
  17. unsigned int pid;
  18. unsigned int depth;
  19. } stack_ops[NET_STACK_MAX];
  20. static int stack_slot(unsigned int pid)
  21. {
  22. unsigned int i;
  23. for (i = 0; i < NET_STACK_MAX; i++)
  24. if (stack_ops[i].depth && stack_ops[i].pid == pid)
  25. return (int)i;
  26. for (i = 0; i < NET_STACK_MAX; i++)
  27. if (!stack_ops[i].depth) {
  28. stack_ops[i].pid = pid;
  29. return (int)i;
  30. }
  31. return -1;
  32. }
  33. void net_stack_enter(void)
  34. {
  35. struct process *p = current_proc();
  36. int slot;
  37. if (!p)
  38. return;
  39. slot = stack_slot(p->pid);
  40. if (slot >= 0)
  41. stack_ops[slot].depth++;
  42. }
  43. void net_stack_leave(void)
  44. {
  45. struct process *p = current_proc();
  46. int slot;
  47. if (!p)
  48. return;
  49. slot = stack_slot(p->pid);
  50. if (slot >= 0 && stack_ops[slot].depth)
  51. stack_ops[slot].depth--;
  52. }
  53. int net_stack_busy(void)
  54. {
  55. unsigned int i;
  56. for (i = 0; i < NET_STACK_MAX; i++)
  57. if (stack_ops[i].depth)
  58. return 1;
  59. return 0;
  60. }
  61. void net_stack_reap(unsigned int pid)
  62. {
  63. unsigned int i;
  64. for (i = 0; i < NET_STACK_MAX; i++)
  65. if (stack_ops[i].pid == pid)
  66. stack_ops[i].depth = 0;
  67. }
  68. unsigned int net_irq_enter(void)
  69. {
  70. unsigned int flags = spin_irq_save_disable();
  71. __asm__ volatile ("sti");
  72. return flags;
  73. }
  74. void net_irq_exit(unsigned int flags)
  75. {
  76. spin_irq_restore(flags);
  77. }
  78. int net_pump(unsigned int budget)
  79. {
  80. const struct netdev *nd = netdev_active();
  81. unsigned char *rx;
  82. unsigned int n, flags;
  83. if (!nd)
  84. return 0;
  85. if (netdev_in_use())
  86. return 0;
  87. rx = netdev_rx_scratch();
  88. flags = net_irq_enter();
  89. for (n = 0; n < budget; n++) {
  90. int len = nd->rx(rx, NETDEV_FRAME_MAX);
  91. if (len < 0)
  92. break;
  93. if (tcp_client_input(rx, (unsigned int)len))
  94. continue;
  95. if (tcp_srv_input(rx, (unsigned int)len))
  96. continue;
  97. if (dns_input(rx, (unsigned int)len))
  98. continue;
  99. if (netkexec_capture_frame(rx, (unsigned int)len))
  100. continue;
  101. net_arp_input(rx, (unsigned int)len);
  102. {
  103. int rlen = net_build_reply(rx, (unsigned int)len,
  104. reply_buf, sizeof(reply_buf));
  105. if (rlen > 0)
  106. nd->tx(reply_buf, (unsigned int)rlen);
  107. }
  108. }
  109. net_irq_exit(flags);
  110. return (int)n;
  111. }