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.

netdevice.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. * Transmit raw packet via network device
  45. *
  46. * @v netdev Network device
  47. * @v iobuf I/O buffer
  48. * @ret rc Return status code
  49. *
  50. * Transmits the packet via the specified network device. This
  51. * function takes ownership of the I/O buffer.
  52. */
  53. int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  54. int rc;
  55. DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
  56. netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
  57. list_add_tail ( &iobuf->list, &netdev->tx_queue );
  58. if ( ! ( netdev->state & NETDEV_OPEN ) ) {
  59. rc = -ENETUNREACH;
  60. goto err;
  61. }
  62. if ( ( rc = netdev->transmit ( netdev, iobuf ) ) != 0 )
  63. goto err;
  64. return 0;
  65. err:
  66. DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
  67. netdev, iobuf, strerror ( rc ) );
  68. netdev_tx_complete ( netdev, iobuf );
  69. return rc;
  70. }
  71. /**
  72. * Complete network transmission
  73. *
  74. * @v netdev Network device
  75. * @v iobuf I/O buffer
  76. *
  77. * The packet must currently be in the network device's TX queue.
  78. */
  79. void netdev_tx_complete ( struct net_device *netdev, struct io_buffer *iobuf ) {
  80. DBGC ( netdev, "NETDEV %p transmission %p complete\n", netdev, iobuf );
  81. /* Catch data corruption as early as possible */
  82. assert ( iobuf->list.next != NULL );
  83. assert ( iobuf->list.prev != NULL );
  84. list_del ( &iobuf->list );
  85. free_iob ( iobuf );
  86. }
  87. /**
  88. * Complete network transmission
  89. *
  90. * @v netdev Network device
  91. *
  92. * Completes the oldest outstanding packet in the TX queue.
  93. */
  94. void netdev_tx_complete_next ( struct net_device *netdev ) {
  95. struct io_buffer *iobuf;
  96. list_for_each_entry ( iobuf, &netdev->tx_queue, list ) {
  97. netdev_tx_complete ( netdev, iobuf );
  98. return;
  99. }
  100. }
  101. /**
  102. * Flush device's transmit queue
  103. *
  104. * @v netdev Network device
  105. */
  106. static void netdev_tx_flush ( struct net_device *netdev ) {
  107. /* Discard any packets in the TX queue */
  108. while ( ! list_empty ( &netdev->tx_queue ) ) {
  109. netdev_tx_complete_next ( netdev );
  110. }
  111. }
  112. /**
  113. * Add packet to receive queue
  114. *
  115. * @v netdev Network device
  116. * @v iobuf I/O buffer
  117. *
  118. * The packet is added to the network device's RX queue. This
  119. * function takes ownership of the I/O buffer.
  120. */
  121. void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  122. DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
  123. netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
  124. list_add_tail ( &iobuf->list, &netdev->rx_queue );
  125. }
  126. /**
  127. * Poll for packet on network device
  128. *
  129. * @v netdev Network device
  130. * @v rx_quota Maximum number of packets to receive
  131. * @ret True There are packets present in the receive queue
  132. * @ret False There are no packets present in the receive queue
  133. *
  134. * Polls the network device for received packets. Any received
  135. * packets will be added to the RX packet queue via netdev_rx().
  136. */
  137. int netdev_poll ( struct net_device *netdev, unsigned int rx_quota ) {
  138. if ( netdev->state & NETDEV_OPEN )
  139. netdev->poll ( netdev, rx_quota );
  140. return ( ! list_empty ( &netdev->rx_queue ) );
  141. }
  142. /**
  143. * Remove packet from device's receive queue
  144. *
  145. * @v netdev Network device
  146. * @ret iobuf I/O buffer, or NULL
  147. *
  148. * Removes the first packet from the device's RX queue and returns it.
  149. * Ownership of the packet is transferred to the caller.
  150. */
  151. struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
  152. struct io_buffer *iobuf;
  153. list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
  154. list_del ( &iobuf->list );
  155. return iobuf;
  156. }
  157. return NULL;
  158. }
  159. /**
  160. * Flush device's receive queue
  161. *
  162. * @v netdev Network device
  163. */
  164. static void netdev_rx_flush ( struct net_device *netdev ) {
  165. struct io_buffer *iobuf;
  166. /* Discard any packets in the RX queue */
  167. while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  168. DBGC ( netdev, "NETDEV %p discarding received %p\n",
  169. netdev, iobuf );
  170. free_iob ( iobuf );
  171. }
  172. }
  173. /**
  174. * Free network device
  175. *
  176. * @v refcnt Network device reference counter
  177. */
  178. static void free_netdev ( struct refcnt *refcnt ) {
  179. struct net_device *netdev =
  180. container_of ( refcnt, struct net_device, refcnt );
  181. netdev_tx_flush ( netdev );
  182. netdev_rx_flush ( netdev );
  183. free ( netdev );
  184. }
  185. /**
  186. * Allocate network device
  187. *
  188. * @v priv_size Size of private data area (net_device::priv)
  189. * @ret netdev Network device, or NULL
  190. *
  191. * Allocates space for a network device and its private data area.
  192. */
  193. struct net_device * alloc_netdev ( size_t priv_size ) {
  194. struct net_device *netdev;
  195. size_t total_len;
  196. total_len = ( sizeof ( *netdev ) + priv_size );
  197. netdev = malloc ( total_len );
  198. if ( netdev ) {
  199. memset ( netdev, 0, total_len );
  200. netdev->refcnt.free = free_netdev;
  201. INIT_LIST_HEAD ( &netdev->tx_queue );
  202. INIT_LIST_HEAD ( &netdev->rx_queue );
  203. netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
  204. }
  205. return netdev;
  206. }
  207. /**
  208. * Register network device
  209. *
  210. * @v netdev Network device
  211. * @ret rc Return status code
  212. *
  213. * Gives the network device a name and adds it to the list of network
  214. * devices.
  215. */
  216. int register_netdev ( struct net_device *netdev ) {
  217. static unsigned int ifindex = 0;
  218. /* Create device name */
  219. snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
  220. ifindex++ );
  221. /* Add to device list */
  222. netdev_get ( netdev );
  223. list_add_tail ( &netdev->list, &net_devices );
  224. DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
  225. netdev, netdev->name, netdev->dev->name,
  226. netdev_hwaddr ( netdev ) );
  227. return 0;
  228. }
  229. /**
  230. * Open network device
  231. *
  232. * @v netdev Network device
  233. * @ret rc Return status code
  234. */
  235. int netdev_open ( struct net_device *netdev ) {
  236. int rc;
  237. /* Do nothing if device is already open */
  238. if ( netdev->state & NETDEV_OPEN )
  239. return 0;
  240. DBGC ( netdev, "NETDEV %p opening\n", netdev );
  241. /* Open the device */
  242. if ( ( rc = netdev->open ( netdev ) ) != 0 )
  243. return rc;
  244. /* Mark as opened */
  245. netdev->state |= NETDEV_OPEN;
  246. return 0;
  247. }
  248. /**
  249. * Close network device
  250. *
  251. * @v netdev Network device
  252. */
  253. void netdev_close ( struct net_device *netdev ) {
  254. /* Do nothing if device is already closed */
  255. if ( ! ( netdev->state & NETDEV_OPEN ) )
  256. return;
  257. DBGC ( netdev, "NETDEV %p closing\n", netdev );
  258. /* Close the device */
  259. netdev->close ( netdev );
  260. /* Flush TX and RX queues */
  261. netdev_tx_flush ( netdev );
  262. netdev_rx_flush ( netdev );
  263. /* Mark as closed */
  264. netdev->state &= ~NETDEV_OPEN;
  265. }
  266. /**
  267. * Unregister network device
  268. *
  269. * @v netdev Network device
  270. *
  271. * Removes the network device from the list of network devices.
  272. */
  273. void unregister_netdev ( struct net_device *netdev ) {
  274. /* Ensure device is closed */
  275. netdev_close ( netdev );
  276. /* Remove from device list */
  277. list_del ( &netdev->list );
  278. netdev_put ( netdev );
  279. DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
  280. }
  281. /**
  282. * Get network device by name
  283. *
  284. * @v name Network device name
  285. * @ret netdev Network device, or NULL
  286. */
  287. struct net_device * find_netdev ( const char *name ) {
  288. struct net_device *netdev;
  289. list_for_each_entry ( netdev, &net_devices, list ) {
  290. if ( strcmp ( netdev->name, name ) == 0 )
  291. return netdev;
  292. }
  293. return NULL;
  294. }
  295. /**
  296. * Get network device by PCI bus:dev.fn address
  297. *
  298. * @v busdevfn PCI bus:dev.fn address
  299. * @ret netdev Network device, or NULL
  300. */
  301. struct net_device * find_pci_netdev ( unsigned int busdevfn ) {
  302. struct net_device *netdev;
  303. list_for_each_entry ( netdev, &net_devices, list ) {
  304. if ( ( netdev->dev->desc.bus_type == BUS_TYPE_PCI ) &&
  305. ( netdev->dev->desc.location == busdevfn ) )
  306. return netdev;
  307. }
  308. return NULL;
  309. }
  310. /**
  311. * Transmit network-layer packet
  312. *
  313. * @v iobuf I/O buffer
  314. * @v netdev Network device
  315. * @v net_protocol Network-layer protocol
  316. * @v ll_dest Destination link-layer address
  317. * @ret rc Return status code
  318. *
  319. * Prepends link-layer headers to the I/O buffer and transmits the
  320. * packet via the specified network device. This function takes
  321. * ownership of the I/O buffer.
  322. */
  323. int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  324. struct net_protocol *net_protocol, const void *ll_dest ) {
  325. return netdev->ll_protocol->tx ( iobuf, netdev, net_protocol, ll_dest );
  326. }
  327. /**
  328. * Process received network-layer packet
  329. *
  330. * @v iobuf I/O buffer
  331. * @v netdev Network device
  332. * @v net_proto Network-layer protocol, in network-byte order
  333. * @v ll_source Source link-layer address
  334. * @ret rc Return status code
  335. */
  336. int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  337. uint16_t net_proto, const void *ll_source ) {
  338. struct net_protocol *net_protocol;
  339. /* Hand off to network-layer protocol, if any */
  340. for ( net_protocol = net_protocols ; net_protocol < net_protocols_end ;
  341. net_protocol++ ) {
  342. if ( net_protocol->net_proto == net_proto ) {
  343. return net_protocol->rx ( iobuf, netdev, ll_source );
  344. }
  345. }
  346. free_iob ( iobuf );
  347. return 0;
  348. }
  349. /**
  350. * Single-step the network stack
  351. *
  352. * @v process Network stack process
  353. *
  354. * This polls all interfaces for received packets, and processes
  355. * packets from the RX queue.
  356. */
  357. static void net_step ( struct process *process __unused ) {
  358. struct net_device *netdev;
  359. struct io_buffer *iobuf;
  360. /* Poll and process each network device */
  361. list_for_each_entry ( netdev, &net_devices, list ) {
  362. /* Poll for new packets */
  363. netdev_poll ( netdev, -1U );
  364. /* Process at most one received packet. Give priority
  365. * to getting packets out of the NIC over processing
  366. * the received packets, because we advertise a window
  367. * that assumes that we can receive packets from the
  368. * NIC faster than they arrive.
  369. */
  370. if ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  371. DBGC ( netdev, "NETDEV %p processing %p\n",
  372. netdev, iobuf );
  373. netdev->ll_protocol->rx ( iobuf, netdev );
  374. }
  375. }
  376. }
  377. /** Networking stack process */
  378. static struct process net_process = {
  379. .step = net_step,
  380. };
  381. /** Initialise the networking stack process */
  382. static void init_net ( void ) {
  383. process_add ( &net_process );
  384. }
  385. INIT_FN ( INIT_PROCESS, init_net, NULL, NULL );