netdev.c 5.0 KB

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