netdev.c 4.0 KB

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