netdev.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* NIC driver registry: see netdev.h. */
  2. #include "netdev.h"
  3. #include "asix.h"
  4. #include "e1000e.h"
  5. #include "io.h"
  6. #include "ne2000.h"
  7. #include "net.h"
  8. #include "process.h"
  9. #include "r8168.h"
  10. #include "tcp.h"
  11. static const struct netdev *const netdev_drivers[] = {
  12. /* e1000e is the active test NIC; the ASIX is kept as the management
  13. * link by netdev_mgmt() even while e1000e is active. */
  14. &e1000e_netdev,
  15. &r8168_netdev,
  16. &ne2000_netdev,
  17. &asix_netdev,
  18. };
  19. static const struct netdev *active;
  20. static int probed;
  21. static unsigned int busy;
  22. static unsigned int owner_pid; /* process that took the NIC (busy > 0) */
  23. /* Shared receive scratch: see netdev.h. One buffer, reused by every protocol
  24. * that polls frames (they never run at the same time: the NIC is owned by one
  25. * command via netdev_acquire()). */
  26. static unsigned char rx_scratch[NETDEV_FRAME_MAX];
  27. unsigned char *netdev_rx_scratch(void)
  28. {
  29. return rx_scratch;
  30. }
  31. void netdev_acquire(void)
  32. {
  33. if (busy == 0 && current_proc())
  34. owner_pid = current_proc()->pid;
  35. busy++;
  36. }
  37. void netdev_release(void)
  38. {
  39. if (busy && --busy == 0)
  40. owner_pid = 0;
  41. }
  42. /* The process currently owning the NIC (0 when free), and a hard reset for
  43. * the reaper: a process killed while inside a net operation must not leave
  44. * the receiver owned forever (it would pause the kexec watcher). */
  45. unsigned int netdev_owner(void)
  46. {
  47. return owner_pid;
  48. }
  49. void netdev_release_all(void)
  50. {
  51. busy = 0;
  52. owner_pid = 0;
  53. }
  54. int netdev_in_use(void)
  55. {
  56. return busy != 0;
  57. }
  58. /* Apply the driver's link-local/static default when it becomes active.
  59. * The ASIX direct link uses 192.168.50.2/24; the e1000e keeps the router
  60. * subnet at 192.168.178.240. */
  61. static void netdev_apply_defaults(const struct netdev *nd)
  62. {
  63. if (nd && nd->default_ip)
  64. net_configure(nd->default_ip, nd->default_mask, 0);
  65. }
  66. const struct netdev *netdev_active(void)
  67. {
  68. unsigned int i;
  69. if (!probed) {
  70. probed = 1;
  71. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]); i++) {
  72. if (netdev_drivers[i]->init() == 0) {
  73. active = netdev_drivers[i];
  74. netdev_apply_defaults(active);
  75. break;
  76. }
  77. }
  78. }
  79. return active;
  80. }
  81. /* The ASIX dongle is the direct management link when present. Keeping
  82. * netlog/netkexec here lets the active driver be switched to e1000e for
  83. * testing without losing the netmon console or `make net-dev`. */
  84. const struct netdev *netdev_mgmt(void)
  85. {
  86. if (asix_netdev.init() == 0)
  87. return &asix_netdev;
  88. return netdev_active();
  89. }
  90. /* Called from the ASIX device probe after a successful hotplug
  91. * enumeration. The boot-time probe caches a NULL result, so without this a
  92. * dongle plugged in later would only be usable through `netdev asix`. */
  93. void netdev_hotplug(void)
  94. {
  95. if (active || !probed)
  96. return;
  97. if (asix_netdev.init() == 0) {
  98. active = &asix_netdev;
  99. netdev_apply_defaults(active);
  100. }
  101. }
  102. /* Stop the NICs before kexec overwrites the kernel. A still-running
  103. * receive ring can DMA into pages the new kernel has already reused. */
  104. void netdev_shutdown(void)
  105. {
  106. const struct netdev *mgmt = netdev_mgmt();
  107. const struct netdev *act = netdev_active();
  108. if (mgmt && mgmt->stop)
  109. mgmt->stop();
  110. if (act && act != mgmt && act->stop)
  111. act->stop();
  112. }
  113. int netdev_get_mac(unsigned char mac[6])
  114. {
  115. const struct netdev *nd = netdev_active();
  116. unsigned int i;
  117. if (!nd) {
  118. for (i = 0; i < 6; i++)
  119. mac[i] = 0;
  120. return -1;
  121. }
  122. nd->get_mac(mac);
  123. return 0;
  124. }
  125. static int name_eq(const char *a, const char *b)
  126. {
  127. while (*a && *b) {
  128. if (*a != *b)
  129. return 0;
  130. a++;
  131. b++;
  132. }
  133. return *a == 0 && *b == 0;
  134. }
  135. int netdev_select(const char *name)
  136. {
  137. unsigned int i;
  138. if (!name || !*name || busy || tcp_client_count() != 0)
  139. return -1;
  140. while (*name == ' ')
  141. name++;
  142. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
  143. i++) {
  144. if (!name_eq(netdev_drivers[i]->name, name))
  145. continue;
  146. if (active == netdev_drivers[i]) {
  147. probed = 1;
  148. return 0;
  149. }
  150. if (netdev_drivers[i]->init() != 0)
  151. return -1;
  152. active = netdev_drivers[i];
  153. probed = 1;
  154. netdev_apply_defaults(active);
  155. return 0;
  156. }
  157. return -1;
  158. }
  159. static void print_netdev(const char *label, const struct netdev *nd)
  160. {
  161. io_putstr("netdev: ");
  162. io_putstr(label);
  163. io_putstr(nd->name);
  164. io_putstr(" (");
  165. io_putstr(nd->chip);
  166. io_putstr(")\n");
  167. }
  168. void program_netdev_main(const char *arg)
  169. {
  170. const struct netdev *mgmt;
  171. if (arg && *arg) {
  172. while (*arg == ' ')
  173. arg++;
  174. if (netdev_select(arg) != 0) {
  175. io_putstr("netdev: cannot select '");
  176. io_putstr(arg);
  177. io_putstr("'\n");
  178. return;
  179. }
  180. } else if (!netdev_active()) {
  181. io_putstr("netdev: no NIC\n");
  182. return;
  183. }
  184. print_netdev("active ", netdev_active());
  185. if (netdev_active()->print_info)
  186. netdev_active()->print_info();
  187. mgmt = netdev_mgmt();
  188. if (mgmt && mgmt != netdev_active())
  189. print_netdev("mgmt ", mgmt);
  190. if (!(arg && *arg))
  191. io_putstr("netdev: use netdev asix|e1000e|r8168|ne2000\n");
  192. }