netdev.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. /* Kernel-wide frame byte counters behind SYS_NET_INFO; every driver feeds
  35. * them. Only the Ethernet header and payload are counted, a failed send adds
  36. * nothing and a zero-length receive leaves them alone. */
  37. static unsigned int rx_total, tx_total;
  38. unsigned char *netdev_rx_scratch(void)
  39. {
  40. return rx_scratch;
  41. }
  42. int netdev_rx_frame(const struct netdev *nd, unsigned char *buf,
  43. unsigned int maxlen)
  44. {
  45. int len;
  46. if (!nd || !nd->rx)
  47. return -1;
  48. len = nd->rx(buf, maxlen);
  49. if (len > 0)
  50. rx_total += (unsigned int)len;
  51. return len;
  52. }
  53. int netdev_tx_frame(const struct netdev *nd, const unsigned char *buf,
  54. unsigned int len)
  55. {
  56. int ret;
  57. if (!nd || !nd->tx)
  58. return -1;
  59. ret = nd->tx(buf, len);
  60. if (ret == 0)
  61. tx_total += len;
  62. return ret;
  63. }
  64. void netdev_byte_counters(unsigned int *rx, unsigned int *tx)
  65. {
  66. if (rx)
  67. *rx = rx_total;
  68. if (tx)
  69. *tx = tx_total;
  70. }
  71. void netdev_acquire(void)
  72. {
  73. if (busy == 0 && current_proc())
  74. owner_pid = current_proc()->pid;
  75. busy++;
  76. }
  77. void netdev_release(void)
  78. {
  79. if (busy && --busy == 0)
  80. owner_pid = 0;
  81. }
  82. /* The process currently owning the NIC (0 when free), and a hard reset for
  83. * the reaper: a process killed while inside a net operation must not leave
  84. * the receiver owned forever (it would pause the kexec watcher). */
  85. unsigned int netdev_owner(void)
  86. {
  87. return owner_pid;
  88. }
  89. void netdev_release_all(void)
  90. {
  91. busy = 0;
  92. owner_pid = 0;
  93. }
  94. int netdev_in_use(void)
  95. {
  96. return busy != 0;
  97. }
  98. /* Apply the driver's link-local/static default when it becomes active.
  99. * The ASIX direct link uses 192.168.50.2/24; the e1000e keeps the router
  100. * subnet at 192.168.178.240. */
  101. static void netdev_apply_defaults(const struct netdev *nd)
  102. {
  103. if (nd && nd->default_ip)
  104. net_configure(nd->default_ip, nd->default_mask, 0);
  105. }
  106. const struct netdev *netdev_active(void)
  107. {
  108. unsigned int i;
  109. if (!probed) {
  110. probed = 1;
  111. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]); i++) {
  112. if (netdev_drivers[i]->init() == 0) {
  113. active = netdev_drivers[i];
  114. netdev_apply_defaults(active);
  115. break;
  116. }
  117. }
  118. }
  119. return active;
  120. }
  121. unsigned int netdev_count(void)
  122. {
  123. return sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
  124. }
  125. const struct netdev *netdev_at(unsigned int index)
  126. {
  127. if (index >= netdev_count())
  128. return 0;
  129. return netdev_drivers[index];
  130. }
  131. int netdev_probed(void)
  132. {
  133. return probed;
  134. }
  135. int netdev_link(const struct netdev *nd)
  136. {
  137. if (!nd)
  138. return 0;
  139. if (!nd->link_up)
  140. return 1;
  141. return nd->link_up() ? 1 : 0;
  142. }
  143. /* The ASIX dongle is the direct management link when present. Keeping
  144. * netlog/netkexec here lets the active driver be switched to e1000e for
  145. * testing without losing the netmon console or `make net-dev`. */
  146. const struct netdev *netdev_mgmt(void)
  147. {
  148. if (asix_netdev.init() == 0)
  149. return &asix_netdev;
  150. return netdev_active();
  151. }
  152. /* Called from a USB NIC probe after a successful hotplug enumeration. The
  153. * boot-time probe caches a NULL result, so without this a dongle or phone
  154. * plugged in later would only be usable through `netdev <name>`. */
  155. void netdev_hotplug(void)
  156. {
  157. if (active || !probed)
  158. return;
  159. if (asix_netdev.init() == 0) {
  160. active = &asix_netdev;
  161. netdev_apply_defaults(active);
  162. return;
  163. }
  164. if (rndis_netdev.init() == 0) {
  165. active = &rndis_netdev;
  166. netdev_apply_defaults(active);
  167. }
  168. }
  169. /* Stop the NICs before kexec overwrites the kernel. A still-running
  170. * receive ring can DMA into pages the new kernel has already reused. */
  171. void netdev_shutdown(void)
  172. {
  173. const struct netdev *mgmt = netdev_mgmt();
  174. const struct netdev *act = netdev_active();
  175. if (mgmt && mgmt->stop)
  176. mgmt->stop();
  177. if (act && act != mgmt && act->stop)
  178. act->stop();
  179. /* The WiFi card DMAs into its RX ring even when it is not the active
  180. * netdev; stop it so the next kernel does not get its pages written. */
  181. if (act != &wifi_netdev && mgmt != &wifi_netdev)
  182. wifi_shutdown();
  183. }
  184. int netdev_get_mac(unsigned char mac[6])
  185. {
  186. const struct netdev *nd = netdev_active();
  187. unsigned int i;
  188. if (!nd) {
  189. for (i = 0; i < 6; i++)
  190. mac[i] = 0;
  191. return -1;
  192. }
  193. nd->get_mac(mac);
  194. return 0;
  195. }
  196. static int name_eq(const char *a, const char *b)
  197. {
  198. while (*a && *b) {
  199. if (*a != *b)
  200. return 0;
  201. a++;
  202. b++;
  203. }
  204. return *a == 0 && *b == 0;
  205. }
  206. int netdev_select(const char *name)
  207. {
  208. unsigned int i;
  209. if (!name || !*name || busy || tcp_client_count() != 0)
  210. return -1;
  211. while (*name == ' ')
  212. name++;
  213. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
  214. i++) {
  215. if (!name_eq(netdev_drivers[i]->name, name))
  216. continue;
  217. if (active == netdev_drivers[i]) {
  218. probed = 1;
  219. return netdev_drivers[i]->init() == 0 ? 0 : -1;
  220. }
  221. if (netdev_drivers[i]->init() != 0)
  222. return -1;
  223. active = netdev_drivers[i];
  224. probed = 1;
  225. netdev_apply_defaults(active);
  226. return 0;
  227. }
  228. return -1;
  229. }
  230. const char *netdev_chip(const struct netdev *nd)
  231. {
  232. if (nd == &wifi_netdev) {
  233. const char *c = wififw_chip_name();
  234. return c[0] != 'n' ? c : nd->chip; /* "none" */
  235. }
  236. return nd->chip;
  237. }
  238. static void print_netdev(const char *label, const struct netdev *nd)
  239. {
  240. io_putstr("netdev: ");
  241. io_putstr(label);
  242. io_putstr(nd->name);
  243. io_putstr(" (");
  244. io_putstr(netdev_chip(nd));
  245. io_putstr(")\n");
  246. }
  247. void program_netdev_main(const char *arg)
  248. {
  249. const struct netdev *mgmt;
  250. if (arg && *arg) {
  251. while (*arg == ' ')
  252. arg++;
  253. if (netdev_select(arg) != 0) {
  254. io_putstr("netdev: cannot select '");
  255. io_putstr(arg);
  256. io_putstr("'\n");
  257. return;
  258. }
  259. } else if (!netdev_active()) {
  260. io_putstr("netdev: no NIC\n");
  261. return;
  262. }
  263. print_netdev("active ", netdev_active());
  264. if (netdev_active()->print_info)
  265. netdev_active()->print_info();
  266. mgmt = netdev_mgmt();
  267. if (mgmt && mgmt != netdev_active())
  268. print_netdev("mgmt ", mgmt);
  269. if (!(arg && *arg))
  270. io_putstr("netdev: use netdev asix|rndis|e1000e|r8168|ne2000\n");
  271. }