You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <byteswap.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <gpxe/if_ether.h>
  25. #include <gpxe/iobuf.h>
  26. #include <gpxe/tables.h>
  27. #include <gpxe/process.h>
  28. #include <gpxe/init.h>
  29. #include <gpxe/device.h>
  30. #include <gpxe/netdevice.h>
  31. /** @file
  32. *
  33. * Network device management
  34. *
  35. */
  36. /** Registered network-layer protocols */
  37. static struct net_protocol net_protocols[0]
  38. __table_start ( struct net_protocol, net_protocols );
  39. static struct net_protocol net_protocols_end[0]
  40. __table_end ( struct net_protocol, net_protocols );
  41. /** List of network devices */
  42. struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
  43. /**
  44. * Record network device statistic
  45. *
  46. * @v stats Network device statistics
  47. * @v rc Status code
  48. */
  49. static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
  50. struct net_device_error *error;
  51. struct net_device_error *least_common_error;
  52. unsigned int i;
  53. /* If this is not an error, just update the good counter */
  54. if ( rc == 0 ) {
  55. stats->good++;
  56. return;
  57. }
  58. /* Update the bad counter */
  59. stats->bad++;
  60. /* Locate the appropriate error record */
  61. least_common_error = &stats->errors[0];
  62. for ( i = 0 ; i < ( sizeof ( stats->errors ) /
  63. sizeof ( stats->errors[0] ) ) ; i++ ) {
  64. error = &stats->errors[i];
  65. /* Update matching record, if found */
  66. if ( error->rc == rc ) {
  67. error->count++;
  68. return;
  69. }
  70. if ( error->count < least_common_error->count )
  71. least_common_error = error;
  72. }
  73. /* Overwrite the least common error record */
  74. least_common_error->rc = rc;
  75. least_common_error->count = 1;
  76. }
  77. /**
  78. * Transmit raw packet via network device
  79. *
  80. * @v netdev Network device
  81. * @v iobuf I/O buffer
  82. * @ret rc Return status code
  83. *
  84. * Transmits the packet via the specified network device. This
  85. * function takes ownership of the I/O buffer.
  86. */
  87. int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  88. int rc;
  89. DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
  90. netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
  91. list_add_tail ( &iobuf->list, &netdev->tx_queue );
  92. if ( ! ( netdev->state & NETDEV_OPEN ) ) {
  93. rc = -ENETUNREACH;
  94. goto err;
  95. }
  96. if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
  97. goto err;
  98. return 0;
  99. err:
  100. netdev_tx_complete_err ( netdev, iobuf, rc );
  101. return rc;
  102. }
  103. /**
  104. * Complete network transmission
  105. *
  106. * @v netdev Network device
  107. * @v iobuf I/O buffer
  108. * @v rc Packet status code
  109. *
  110. * The packet must currently be in the network device's TX queue.
  111. */
  112. void netdev_tx_complete_err ( struct net_device *netdev,
  113. struct io_buffer *iobuf, int rc ) {
  114. /* Update statistics counter */
  115. netdev_record_stat ( &netdev->tx_stats, rc );
  116. if ( rc == 0 ) {
  117. DBGC ( netdev, "NETDEV %p transmission %p complete\n",
  118. netdev, iobuf );
  119. } else {
  120. DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
  121. netdev, iobuf, strerror ( rc ) );
  122. }
  123. /* Catch data corruption as early as possible */
  124. assert ( iobuf->list.next != NULL );
  125. assert ( iobuf->list.prev != NULL );
  126. /* Dequeue and free I/O buffer */
  127. list_del ( &iobuf->list );
  128. free_iob ( iobuf );
  129. }
  130. /**
  131. * Complete network transmission
  132. *
  133. * @v netdev Network device
  134. * @v rc Packet status code
  135. *
  136. * Completes the oldest outstanding packet in the TX queue.
  137. */
  138. void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
  139. struct io_buffer *iobuf;
  140. list_for_each_entry ( iobuf, &netdev->tx_queue, list ) {
  141. netdev_tx_complete_err ( netdev, iobuf, rc );
  142. return;
  143. }
  144. }
  145. /**
  146. * Flush device's transmit queue
  147. *
  148. * @v netdev Network device
  149. */
  150. static void netdev_tx_flush ( struct net_device *netdev ) {
  151. /* Discard any packets in the TX queue */
  152. while ( ! list_empty ( &netdev->tx_queue ) ) {
  153. netdev_tx_complete_next_err ( netdev, -ECANCELED );
  154. }
  155. }
  156. /**
  157. * Add packet to receive queue
  158. *
  159. * @v netdev Network device
  160. * @v iobuf I/O buffer, or NULL
  161. *
  162. * The packet is added to the network device's RX queue. This
  163. * function takes ownership of the I/O buffer.
  164. */
  165. void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  166. DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
  167. netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
  168. /* Enqueue packet */
  169. list_add_tail ( &iobuf->list, &netdev->rx_queue );
  170. /* Update statistics counter */
  171. netdev_record_stat ( &netdev->rx_stats, 0 );
  172. }
  173. /**
  174. * Discard received packet
  175. *
  176. * @v netdev Network device
  177. * @v iobuf I/O buffer, or NULL
  178. * @v rc Packet status code
  179. *
  180. * The packet is discarded and an RX error is recorded. This function
  181. * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
  182. * example, the net device wishes to report an error due to being
  183. * unable to allocate an I/O buffer.
  184. */
  185. void netdev_rx_err ( struct net_device *netdev,
  186. struct io_buffer *iobuf, int rc ) {
  187. DBGC ( netdev, "NETDEV %p failed to receive %p: %s\n",
  188. netdev, iobuf, strerror ( rc ) );
  189. /* Discard packet */
  190. free_iob ( iobuf );
  191. /* Update statistics counter */
  192. netdev_record_stat ( &netdev->rx_stats, rc );
  193. }
  194. /**
  195. * Poll for completed and received packets on network device
  196. *
  197. * @v netdev Network device
  198. *
  199. * Polls the network device for completed transmissions and received
  200. * packets. Any received packets will be added to the RX packet queue
  201. * via netdev_rx().
  202. */
  203. void netdev_poll ( struct net_device *netdev ) {
  204. if ( netdev->state & NETDEV_OPEN )
  205. netdev->op->poll ( netdev );
  206. }
  207. /**
  208. * Remove packet from device's receive queue
  209. *
  210. * @v netdev Network device
  211. * @ret iobuf I/O buffer, or NULL
  212. *
  213. * Removes the first packet from the device's RX queue and returns it.
  214. * Ownership of the packet is transferred to the caller.
  215. */
  216. struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
  217. struct io_buffer *iobuf;
  218. list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
  219. list_del ( &iobuf->list );
  220. return iobuf;
  221. }
  222. return NULL;
  223. }
  224. /**
  225. * Flush device's receive queue
  226. *
  227. * @v netdev Network device
  228. */
  229. static void netdev_rx_flush ( struct net_device *netdev ) {
  230. struct io_buffer *iobuf;
  231. /* Discard any packets in the RX queue */
  232. while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  233. netdev_rx_err ( netdev, iobuf, -ECANCELED );
  234. }
  235. }
  236. /**
  237. * Free network device
  238. *
  239. * @v refcnt Network device reference counter
  240. */
  241. static void free_netdev ( struct refcnt *refcnt ) {
  242. struct net_device *netdev =
  243. container_of ( refcnt, struct net_device, refcnt );
  244. netdev_tx_flush ( netdev );
  245. netdev_rx_flush ( netdev );
  246. free ( netdev );
  247. }
  248. /**
  249. * Allocate network device
  250. *
  251. * @v priv_size Size of private data area (net_device::priv)
  252. * @ret netdev Network device, or NULL
  253. *
  254. * Allocates space for a network device and its private data area.
  255. */
  256. struct net_device * alloc_netdev ( size_t priv_size ) {
  257. struct net_device *netdev;
  258. size_t total_len;
  259. total_len = ( sizeof ( *netdev ) + priv_size );
  260. netdev = zalloc ( total_len );
  261. if ( netdev ) {
  262. netdev->refcnt.free = free_netdev;
  263. INIT_LIST_HEAD ( &netdev->tx_queue );
  264. INIT_LIST_HEAD ( &netdev->rx_queue );
  265. settings_init ( netdev_settings ( netdev ),
  266. &netdev_settings_operations, &netdev->refcnt,
  267. netdev->name, 0 );
  268. netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
  269. }
  270. return netdev;
  271. }
  272. /**
  273. * Register network device
  274. *
  275. * @v netdev Network device
  276. * @ret rc Return status code
  277. *
  278. * Gives the network device a name and adds it to the list of network
  279. * devices.
  280. */
  281. int register_netdev ( struct net_device *netdev ) {
  282. static unsigned int ifindex = 0;
  283. int rc;
  284. /* Create device name */
  285. snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
  286. ifindex++ );
  287. /* Register per-netdev configuration settings */
  288. if ( ( rc = register_settings ( netdev_settings ( netdev ),
  289. NULL ) ) != 0 ) {
  290. DBGC ( netdev, "NETDEV %p could not register settings: %s\n",
  291. netdev, strerror ( rc ) );
  292. return rc;
  293. }
  294. /* Add to device list */
  295. netdev_get ( netdev );
  296. list_add_tail ( &netdev->list, &net_devices );
  297. DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
  298. netdev, netdev->name, netdev->dev->name,
  299. netdev_hwaddr ( netdev ) );
  300. return 0;
  301. }
  302. /**
  303. * Open network device
  304. *
  305. * @v netdev Network device
  306. * @ret rc Return status code
  307. */
  308. int netdev_open ( struct net_device *netdev ) {
  309. int rc;
  310. /* Do nothing if device is already open */
  311. if ( netdev->state & NETDEV_OPEN )
  312. return 0;
  313. DBGC ( netdev, "NETDEV %p opening\n", netdev );
  314. /* Open the device */
  315. if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
  316. return rc;
  317. /* Mark as opened */
  318. netdev->state |= NETDEV_OPEN;
  319. return 0;
  320. }
  321. /**
  322. * Close network device
  323. *
  324. * @v netdev Network device
  325. */
  326. void netdev_close ( struct net_device *netdev ) {
  327. /* Do nothing if device is already closed */
  328. if ( ! ( netdev->state & NETDEV_OPEN ) )
  329. return;
  330. DBGC ( netdev, "NETDEV %p closing\n", netdev );
  331. /* Close the device */
  332. netdev->op->close ( netdev );
  333. /* Flush TX and RX queues */
  334. netdev_tx_flush ( netdev );
  335. netdev_rx_flush ( netdev );
  336. /* Mark as closed */
  337. netdev->state &= ~NETDEV_OPEN;
  338. }
  339. /**
  340. * Unregister network device
  341. *
  342. * @v netdev Network device
  343. *
  344. * Removes the network device from the list of network devices.
  345. */
  346. void unregister_netdev ( struct net_device *netdev ) {
  347. /* Ensure device is closed */
  348. netdev_close ( netdev );
  349. /* Unregister per-netdev configuration settings */
  350. unregister_settings ( netdev_settings ( netdev ) );
  351. /* Remove from device list */
  352. list_del ( &netdev->list );
  353. netdev_put ( netdev );
  354. DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
  355. }
  356. /** Enable or disable interrupts
  357. *
  358. * @v netdev Network device
  359. * @v enable Interrupts should be enabled
  360. */
  361. void netdev_irq ( struct net_device *netdev, int enable ) {
  362. netdev->op->irq ( netdev, enable );
  363. }
  364. /**
  365. * Get network device by name
  366. *
  367. * @v name Network device name
  368. * @ret netdev Network device, or NULL
  369. */
  370. struct net_device * find_netdev ( const char *name ) {
  371. struct net_device *netdev;
  372. list_for_each_entry ( netdev, &net_devices, list ) {
  373. if ( strcmp ( netdev->name, name ) == 0 )
  374. return netdev;
  375. }
  376. return NULL;
  377. }
  378. /**
  379. * Get network device by PCI bus:dev.fn address
  380. *
  381. * @v bus_type Bus type
  382. * @v location Bus location
  383. * @ret netdev Network device, or NULL
  384. */
  385. struct net_device * find_netdev_by_location ( unsigned int bus_type,
  386. unsigned int location ) {
  387. struct net_device *netdev;
  388. list_for_each_entry ( netdev, &net_devices, list ) {
  389. if ( ( netdev->dev->desc.bus_type == bus_type ) &&
  390. ( netdev->dev->desc.location == location ) )
  391. return netdev;
  392. }
  393. return NULL;
  394. }
  395. /**
  396. * Transmit network-layer packet
  397. *
  398. * @v iobuf I/O buffer
  399. * @v netdev Network device
  400. * @v net_protocol Network-layer protocol
  401. * @v ll_dest Destination link-layer address
  402. * @ret rc Return status code
  403. *
  404. * Prepends link-layer headers to the I/O buffer and transmits the
  405. * packet via the specified network device. This function takes
  406. * ownership of the I/O buffer.
  407. */
  408. int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  409. struct net_protocol *net_protocol, const void *ll_dest ) {
  410. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  411. int rc;
  412. /* Force a poll on the netdevice to (potentially) clear any
  413. * backed-up TX completions. This is needed on some network
  414. * devices to avoid excessive losses due to small TX ring
  415. * sizes.
  416. */
  417. netdev_poll ( netdev );
  418. /* Add link-layer header */
  419. if ( ( rc = ll_protocol->push ( iobuf, ll_dest, netdev->ll_addr,
  420. net_protocol->net_proto ) ) != 0 ) {
  421. free_iob ( iobuf );
  422. return rc;
  423. }
  424. /* Transmit packet */
  425. return netdev_tx ( netdev, iobuf );
  426. }
  427. /**
  428. * Process received network-layer packet
  429. *
  430. * @v iobuf I/O buffer
  431. * @v netdev Network device
  432. * @v net_proto Network-layer protocol, in network-byte order
  433. * @v ll_source Source link-layer address
  434. * @ret rc Return status code
  435. */
  436. int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  437. uint16_t net_proto, const void *ll_source ) {
  438. struct net_protocol *net_protocol;
  439. /* Hand off to network-layer protocol, if any */
  440. for ( net_protocol = net_protocols ; net_protocol < net_protocols_end ;
  441. net_protocol++ ) {
  442. if ( net_protocol->net_proto == net_proto ) {
  443. return net_protocol->rx ( iobuf, netdev, ll_source );
  444. }
  445. }
  446. free_iob ( iobuf );
  447. return 0;
  448. }
  449. /**
  450. * Single-step the network stack
  451. *
  452. * @v process Network stack process
  453. *
  454. * This polls all interfaces for received packets, and processes
  455. * packets from the RX queue.
  456. */
  457. static void net_step ( struct process *process __unused ) {
  458. struct net_device *netdev;
  459. struct io_buffer *iobuf;
  460. struct ll_protocol *ll_protocol;
  461. const void *ll_dest;
  462. const void *ll_source;
  463. uint16_t net_proto;
  464. int rc;
  465. /* Poll and process each network device */
  466. list_for_each_entry ( netdev, &net_devices, list ) {
  467. /* Poll for new packets */
  468. netdev_poll ( netdev );
  469. /* Process at most one received packet. Give priority
  470. * to getting packets out of the NIC over processing
  471. * the received packets, because we advertise a window
  472. * that assumes that we can receive packets from the
  473. * NIC faster than they arrive.
  474. */
  475. if ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  476. DBGC ( netdev, "NETDEV %p processing %p (%p+%zx)\n",
  477. netdev, iobuf, iobuf->data,
  478. iob_len ( iobuf ) );
  479. /* Remove link-layer header */
  480. ll_protocol = netdev->ll_protocol;
  481. if ( ( rc = ll_protocol->pull ( iobuf, &ll_dest,
  482. &ll_source,
  483. &net_proto ) ) != 0 ) {
  484. free_iob ( iobuf );
  485. continue;
  486. }
  487. net_rx ( iobuf, netdev, net_proto, ll_source );
  488. }
  489. }
  490. }
  491. /** Networking stack process */
  492. struct process net_process __permanent_process = {
  493. .step = net_step,
  494. };