netdev.c 5.4 KB

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