| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- /* NIC driver registry: see netdev.h. */
- #include "netdev.h"
- #include "asix.h"
- #include "e1000e.h"
- #include "io.h"
- #include "ne2000.h"
- #include "net.h"
- #include "process.h"
- #include "r8168.h"
- #include "rndis.h"
- #include "tcp.h"
- #include "wifi.h"
- static const struct netdev *const netdev_drivers[] = {
- /* e1000e is the active test NIC; the ASIX is kept as the management
- * link by netdev_mgmt() even while e1000e is active. */
- &e1000e_netdev,
- &r8168_netdev,
- &ne2000_netdev,
- &asix_netdev,
- &rndis_netdev,
- /* Last: it only wins when nothing wired is present, and its link is
- * down until `wifi connect` (boot DHCP checks link_up). */
- &wifi_netdev,
- };
- static const struct netdev *active;
- static int probed;
- static unsigned int busy;
- static unsigned int owner_pid; /* process that took the NIC (busy > 0) */
- /* Shared receive scratch: see netdev.h. One buffer, reused by every protocol
- * that polls frames (they never run at the same time: the NIC is owned by one
- * command via netdev_acquire()). */
- static unsigned char rx_scratch[NETDEV_FRAME_MAX];
- unsigned char *netdev_rx_scratch(void)
- {
- return rx_scratch;
- }
- void netdev_acquire(void)
- {
- if (busy == 0 && current_proc())
- owner_pid = current_proc()->pid;
- busy++;
- }
- void netdev_release(void)
- {
- if (busy && --busy == 0)
- owner_pid = 0;
- }
- /* The process currently owning the NIC (0 when free), and a hard reset for
- * the reaper: a process killed while inside a net operation must not leave
- * the receiver owned forever (it would pause the kexec watcher). */
- unsigned int netdev_owner(void)
- {
- return owner_pid;
- }
- void netdev_release_all(void)
- {
- busy = 0;
- owner_pid = 0;
- }
- int netdev_in_use(void)
- {
- return busy != 0;
- }
- /* Apply the driver's link-local/static default when it becomes active.
- * The ASIX direct link uses 192.168.50.2/24; the e1000e keeps the router
- * subnet at 192.168.178.240. */
- static void netdev_apply_defaults(const struct netdev *nd)
- {
- if (nd && nd->default_ip)
- net_configure(nd->default_ip, nd->default_mask, 0);
- }
- const struct netdev *netdev_active(void)
- {
- unsigned int i;
- if (!probed) {
- probed = 1;
- for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]); i++) {
- if (netdev_drivers[i]->init() == 0) {
- active = netdev_drivers[i];
- netdev_apply_defaults(active);
- break;
- }
- }
- }
- return active;
- }
- unsigned int netdev_count(void)
- {
- return sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
- }
- const struct netdev *netdev_at(unsigned int index)
- {
- if (index >= netdev_count())
- return 0;
- return netdev_drivers[index];
- }
- int netdev_probed(void)
- {
- return probed;
- }
- int netdev_link(const struct netdev *nd)
- {
- if (!nd)
- return 0;
- if (!nd->link_up)
- return 1;
- return nd->link_up() ? 1 : 0;
- }
- /* The ASIX dongle is the direct management link when present. Keeping
- * netlog/netkexec here lets the active driver be switched to e1000e for
- * testing without losing the netmon console or `make net-dev`. */
- const struct netdev *netdev_mgmt(void)
- {
- if (asix_netdev.init() == 0)
- return &asix_netdev;
- return netdev_active();
- }
- /* Called from a USB NIC probe after a successful hotplug enumeration. The
- * boot-time probe caches a NULL result, so without this a dongle or phone
- * plugged in later would only be usable through `netdev <name>`. */
- void netdev_hotplug(void)
- {
- if (active || !probed)
- return;
- if (asix_netdev.init() == 0) {
- active = &asix_netdev;
- netdev_apply_defaults(active);
- return;
- }
- if (rndis_netdev.init() == 0) {
- active = &rndis_netdev;
- netdev_apply_defaults(active);
- }
- }
- /* Stop the NICs before kexec overwrites the kernel. A still-running
- * receive ring can DMA into pages the new kernel has already reused. */
- void netdev_shutdown(void)
- {
- const struct netdev *mgmt = netdev_mgmt();
- const struct netdev *act = netdev_active();
- if (mgmt && mgmt->stop)
- mgmt->stop();
- if (act && act != mgmt && act->stop)
- act->stop();
- /* The WiFi card DMAs into its RX ring even when it is not the active
- * netdev; stop it so the next kernel does not get its pages written. */
- if (act != &wifi_netdev && mgmt != &wifi_netdev)
- wifi_shutdown();
- }
- int netdev_get_mac(unsigned char mac[6])
- {
- const struct netdev *nd = netdev_active();
- unsigned int i;
- if (!nd) {
- for (i = 0; i < 6; i++)
- mac[i] = 0;
- return -1;
- }
- nd->get_mac(mac);
- return 0;
- }
- static int name_eq(const char *a, const char *b)
- {
- while (*a && *b) {
- if (*a != *b)
- return 0;
- a++;
- b++;
- }
- return *a == 0 && *b == 0;
- }
- int netdev_select(const char *name)
- {
- unsigned int i;
- if (!name || !*name || busy || tcp_client_count() != 0)
- return -1;
- while (*name == ' ')
- name++;
- for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
- i++) {
- if (!name_eq(netdev_drivers[i]->name, name))
- continue;
- if (active == netdev_drivers[i]) {
- probed = 1;
- return netdev_drivers[i]->init() == 0 ? 0 : -1;
- }
- if (netdev_drivers[i]->init() != 0)
- return -1;
- active = netdev_drivers[i];
- probed = 1;
- netdev_apply_defaults(active);
- return 0;
- }
- return -1;
- }
- static void print_netdev(const char *label, const struct netdev *nd)
- {
- io_putstr("netdev: ");
- io_putstr(label);
- io_putstr(nd->name);
- io_putstr(" (");
- io_putstr(nd->chip);
- io_putstr(")\n");
- }
- void program_netdev_main(const char *arg)
- {
- const struct netdev *mgmt;
- if (arg && *arg) {
- while (*arg == ' ')
- arg++;
- if (netdev_select(arg) != 0) {
- io_putstr("netdev: cannot select '");
- io_putstr(arg);
- io_putstr("'\n");
- return;
- }
- } else if (!netdev_active()) {
- io_putstr("netdev: no NIC\n");
- return;
- }
- print_netdev("active ", netdev_active());
- if (netdev_active()->print_info)
- netdev_active()->print_info();
- mgmt = netdev_mgmt();
- if (mgmt && mgmt != netdev_active())
- print_netdev("mgmt ", mgmt);
- if (!(arg && *arg))
- io_putstr("netdev: use netdev asix|rndis|e1000e|r8168|ne2000\n");
- }
|