netdev.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. unsigned int netdev_count(void)
  88. {
  89. return sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
  90. }
  91. const struct netdev *netdev_at(unsigned int index)
  92. {
  93. if (index >= netdev_count())
  94. return 0;
  95. return netdev_drivers[index];
  96. }
  97. int netdev_probed(void)
  98. {
  99. return probed;
  100. }
  101. int netdev_link(const struct netdev *nd)
  102. {
  103. if (!nd)
  104. return 0;
  105. if (!nd->link_up)
  106. return 1;
  107. return nd->link_up() ? 1 : 0;
  108. }
  109. /* The ASIX dongle is the direct management link when present. Keeping
  110. * netlog/netkexec here lets the active driver be switched to e1000e for
  111. * testing without losing the netmon console or `make net-dev`. */
  112. const struct netdev *netdev_mgmt(void)
  113. {
  114. if (asix_netdev.init() == 0)
  115. return &asix_netdev;
  116. return netdev_active();
  117. }
  118. /* Called from a USB NIC probe after a successful hotplug enumeration. The
  119. * boot-time probe caches a NULL result, so without this a dongle or phone
  120. * plugged in later would only be usable through `netdev <name>`. */
  121. void netdev_hotplug(void)
  122. {
  123. if (active || !probed)
  124. return;
  125. if (asix_netdev.init() == 0) {
  126. active = &asix_netdev;
  127. netdev_apply_defaults(active);
  128. return;
  129. }
  130. if (rndis_netdev.init() == 0) {
  131. active = &rndis_netdev;
  132. netdev_apply_defaults(active);
  133. }
  134. }
  135. /* Stop the NICs before kexec overwrites the kernel. A still-running
  136. * receive ring can DMA into pages the new kernel has already reused. */
  137. void netdev_shutdown(void)
  138. {
  139. const struct netdev *mgmt = netdev_mgmt();
  140. const struct netdev *act = netdev_active();
  141. if (mgmt && mgmt->stop)
  142. mgmt->stop();
  143. if (act && act != mgmt && act->stop)
  144. act->stop();
  145. /* The WiFi card DMAs into its RX ring even when it is not the active
  146. * netdev; stop it so the next kernel does not get its pages written. */
  147. if (act != &wifi_netdev && mgmt != &wifi_netdev)
  148. wifi_shutdown();
  149. }
  150. int netdev_get_mac(unsigned char mac[6])
  151. {
  152. const struct netdev *nd = netdev_active();
  153. unsigned int i;
  154. if (!nd) {
  155. for (i = 0; i < 6; i++)
  156. mac[i] = 0;
  157. return -1;
  158. }
  159. nd->get_mac(mac);
  160. return 0;
  161. }
  162. static int name_eq(const char *a, const char *b)
  163. {
  164. while (*a && *b) {
  165. if (*a != *b)
  166. return 0;
  167. a++;
  168. b++;
  169. }
  170. return *a == 0 && *b == 0;
  171. }
  172. int netdev_select(const char *name)
  173. {
  174. unsigned int i;
  175. if (!name || !*name || busy || tcp_client_count() != 0)
  176. return -1;
  177. while (*name == ' ')
  178. name++;
  179. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
  180. i++) {
  181. if (!name_eq(netdev_drivers[i]->name, name))
  182. continue;
  183. if (active == netdev_drivers[i]) {
  184. probed = 1;
  185. return netdev_drivers[i]->init() == 0 ? 0 : -1;
  186. }
  187. if (netdev_drivers[i]->init() != 0)
  188. return -1;
  189. active = netdev_drivers[i];
  190. probed = 1;
  191. netdev_apply_defaults(active);
  192. return 0;
  193. }
  194. return -1;
  195. }
  196. static void print_netdev(const char *label, const struct netdev *nd)
  197. {
  198. io_putstr("netdev: ");
  199. io_putstr(label);
  200. io_putstr(nd->name);
  201. io_putstr(" (");
  202. io_putstr(nd->chip);
  203. io_putstr(")\n");
  204. }
  205. void program_netdev_main(const char *arg)
  206. {
  207. const struct netdev *mgmt;
  208. if (arg && *arg) {
  209. while (*arg == ' ')
  210. arg++;
  211. if (netdev_select(arg) != 0) {
  212. io_putstr("netdev: cannot select '");
  213. io_putstr(arg);
  214. io_putstr("'\n");
  215. return;
  216. }
  217. } else if (!netdev_active()) {
  218. io_putstr("netdev: no NIC\n");
  219. return;
  220. }
  221. print_netdev("active ", netdev_active());
  222. if (netdev_active()->print_info)
  223. netdev_active()->print_info();
  224. mgmt = netdev_mgmt();
  225. if (mgmt && mgmt != netdev_active())
  226. print_netdev("mgmt ", mgmt);
  227. if (!(arg && *arg))
  228. io_putstr("netdev: use netdev asix|rndis|e1000e|r8168|ne2000\n");
  229. }