netdev.c 6.0 KB

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