Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

netdevice.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <byteswap.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <vsprintf.h>
  24. #include <gpxe/if_ether.h>
  25. #include <gpxe/pkbuff.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 pkb Packet buffer
  48. * @ret rc Return status code
  49. *
  50. * Transmits the packet via the specified network device. This
  51. * function takes ownership of the packet buffer.
  52. */
  53. int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb ) {
  54. int rc;
  55. DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
  56. netdev, pkb, pkb->data, pkb_len ( pkb ) );
  57. list_add_tail ( &pkb->list, &netdev->tx_queue );
  58. if ( ! ( netdev->state & NETDEV_OPEN ) ) {
  59. rc = -ENETUNREACH;
  60. goto err;
  61. }
  62. if ( ( rc = netdev->transmit ( netdev, pkb ) ) != 0 )
  63. goto err;
  64. return 0;
  65. err:
  66. DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
  67. netdev, pkb, strerror ( rc ) );
  68. netdev_tx_complete ( netdev, pkb );
  69. return rc;
  70. }
  71. /**
  72. * Complete network transmission
  73. *
  74. * @v netdev Network device
  75. * @v pkb Packet 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 pk_buff *pkb ) {
  80. DBGC ( netdev, "NETDEV %p transmission %p complete\n", netdev, pkb );
  81. /* Catch data corruption as early as possible */
  82. assert ( pkb->list.next != NULL );
  83. assert ( pkb->list.prev != NULL );
  84. list_del ( &pkb->list );
  85. free_pkb ( pkb );
  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 pk_buff *pkb;
  96. list_for_each_entry ( pkb, &netdev->tx_queue, list ) {
  97. netdev_tx_complete ( netdev, pkb );
  98. return;
  99. }
  100. }
  101. /**
  102. * Add packet to receive queue
  103. *
  104. * @v netdev Network device
  105. * @v pkb Packet buffer
  106. *
  107. * The packet is added to the network device's RX queue. This
  108. * function takes ownership of the packet buffer.
  109. */
  110. void netdev_rx ( struct net_device *netdev, struct pk_buff *pkb ) {
  111. DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
  112. netdev, pkb, pkb->data, pkb_len ( pkb ) );
  113. list_add_tail ( &pkb->list, &netdev->rx_queue );
  114. }
  115. /**
  116. * Poll for packet on network device
  117. *
  118. * @v netdev Network device
  119. * @v rx_quota Maximum number of packets to receive
  120. * @ret True There are packets present in the receive queue
  121. * @ret False There are no packets present in the receive queue
  122. *
  123. * Polls the network device for received packets. Any received
  124. * packets will be added to the RX packet queue via netdev_rx().
  125. */
  126. int netdev_poll ( struct net_device *netdev, unsigned int rx_quota ) {
  127. if ( netdev->state & NETDEV_OPEN )
  128. netdev->poll ( netdev, rx_quota );
  129. return ( ! list_empty ( &netdev->rx_queue ) );
  130. }
  131. /**
  132. * Remove packet from device's receive queue
  133. *
  134. * @v netdev Network device
  135. * @ret pkb Packet buffer, or NULL
  136. *
  137. * Removes the first packet from the device's RX queue and returns it.
  138. * Ownership of the packet is transferred to the caller.
  139. */
  140. struct pk_buff * netdev_rx_dequeue ( struct net_device *netdev ) {
  141. struct pk_buff *pkb;
  142. list_for_each_entry ( pkb, &netdev->rx_queue, list ) {
  143. list_del ( &pkb->list );
  144. return pkb;
  145. }
  146. return NULL;
  147. }
  148. /**
  149. * Allocate network device
  150. *
  151. * @v priv_size Size of private data area (net_device::priv)
  152. * @ret netdev Network device, or NULL
  153. *
  154. * Allocates space for a network device and its private data area.
  155. */
  156. struct net_device * alloc_netdev ( size_t priv_size ) {
  157. struct net_device *netdev;
  158. netdev = calloc ( 1, sizeof ( *netdev ) + priv_size );
  159. if ( netdev ) {
  160. INIT_LIST_HEAD ( &netdev->references );
  161. INIT_LIST_HEAD ( &netdev->tx_queue );
  162. INIT_LIST_HEAD ( &netdev->rx_queue );
  163. netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
  164. }
  165. return netdev;
  166. }
  167. /**
  168. * Register network device
  169. *
  170. * @v netdev Network device
  171. * @ret rc Return status code
  172. *
  173. * Gives the network device a name and adds it to the list of network
  174. * devices.
  175. */
  176. int register_netdev ( struct net_device *netdev ) {
  177. static unsigned int ifindex = 0;
  178. /* Create device name */
  179. snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
  180. ifindex++ );
  181. /* Add to device list */
  182. list_add_tail ( &netdev->list, &net_devices );
  183. DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
  184. netdev, netdev->name, netdev->dev->name,
  185. netdev_hwaddr ( netdev ) );
  186. return 0;
  187. }
  188. /**
  189. * Open network device
  190. *
  191. * @v netdev Network device
  192. * @ret rc Return status code
  193. */
  194. int netdev_open ( struct net_device *netdev ) {
  195. int rc;
  196. /* Do nothing if device is already open */
  197. if ( netdev->state & NETDEV_OPEN )
  198. return 0;
  199. DBGC ( netdev, "NETDEV %p opening\n", netdev );
  200. /* Open the device */
  201. if ( ( rc = netdev->open ( netdev ) ) != 0 )
  202. return rc;
  203. /* Mark as opened */
  204. netdev->state |= NETDEV_OPEN;
  205. return 0;
  206. }
  207. /**
  208. * Close network device
  209. *
  210. * @v netdev Network device
  211. */
  212. void netdev_close ( struct net_device *netdev ) {
  213. struct pk_buff *pkb;
  214. /* Do nothing if device is already closed */
  215. if ( ! ( netdev->state & NETDEV_OPEN ) )
  216. return;
  217. DBGC ( netdev, "NETDEV %p closing\n", netdev );
  218. /* Close the device */
  219. netdev->close ( netdev );
  220. /* Discard any packets in the TX queue */
  221. while ( ! list_empty ( &netdev->tx_queue ) ) {
  222. netdev_tx_complete_next ( netdev );
  223. }
  224. /* Discard any packets in the RX queue */
  225. while ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) {
  226. DBGC ( netdev, "NETDEV %p discarding received %p\n",
  227. netdev, pkb );
  228. free_pkb ( pkb );
  229. }
  230. /* Mark as closed */
  231. netdev->state &= ~NETDEV_OPEN;
  232. }
  233. /**
  234. * Unregister network device
  235. *
  236. * @v netdev Network device
  237. *
  238. * Removes the network device from the list of network devices.
  239. */
  240. void unregister_netdev ( struct net_device *netdev ) {
  241. /* Ensure device is closed */
  242. netdev_close ( netdev );
  243. /* Kill off any persistent references to this device */
  244. forget_references ( &netdev->references );
  245. /* Remove from device list */
  246. list_del ( &netdev->list );
  247. DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
  248. }
  249. /**
  250. * Free network device
  251. *
  252. * @v netdev Network device
  253. */
  254. void free_netdev ( struct net_device *netdev ) {
  255. free ( netdev );
  256. }
  257. /**
  258. * Get network device by name
  259. *
  260. * @v name Network device name
  261. * @ret netdev Network device, or NULL
  262. */
  263. struct net_device * find_netdev ( const char *name ) {
  264. struct net_device *netdev;
  265. list_for_each_entry ( netdev, &net_devices, list ) {
  266. if ( strcmp ( netdev->name, name ) == 0 )
  267. return netdev;
  268. }
  269. return NULL;
  270. }
  271. /**
  272. * Get network device by PCI bus:dev.fn address
  273. *
  274. * @v busdevfn PCI bus:dev.fn address
  275. * @ret netdev Network device, or NULL
  276. */
  277. struct net_device * find_pci_netdev ( unsigned int busdevfn ) {
  278. struct net_device *netdev;
  279. list_for_each_entry ( netdev, &net_devices, list ) {
  280. if ( ( netdev->dev->desc.bus_type == BUS_TYPE_PCI ) &&
  281. ( netdev->dev->desc.pci.busdevfn == busdevfn ) )
  282. return netdev;
  283. }
  284. return NULL;
  285. }
  286. /**
  287. * Transmit network-layer packet
  288. *
  289. * @v pkb Packet buffer
  290. * @v netdev Network device
  291. * @v net_protocol Network-layer protocol
  292. * @v ll_dest Destination link-layer address
  293. * @ret rc Return status code
  294. *
  295. * Prepends link-layer headers to the packet buffer and transmits the
  296. * packet via the specified network device. This function takes
  297. * ownership of the packet buffer.
  298. */
  299. int net_tx ( struct pk_buff *pkb, struct net_device *netdev,
  300. struct net_protocol *net_protocol, const void *ll_dest ) {
  301. return netdev->ll_protocol->tx ( pkb, netdev, net_protocol, ll_dest );
  302. }
  303. /**
  304. * Process received network-layer packet
  305. *
  306. * @v pkb Packet buffer
  307. * @v netdev Network device
  308. * @v net_proto Network-layer protocol, in network-byte order
  309. * @v ll_source Source link-layer address
  310. * @ret rc Return status code
  311. */
  312. int net_rx ( struct pk_buff *pkb, struct net_device *netdev,
  313. uint16_t net_proto, const void *ll_source ) {
  314. struct net_protocol *net_protocol;
  315. /* Hand off to network-layer protocol, if any */
  316. for ( net_protocol = net_protocols ; net_protocol < net_protocols_end ;
  317. net_protocol++ ) {
  318. if ( net_protocol->net_proto == net_proto ) {
  319. return net_protocol->rx ( pkb, netdev, ll_source );
  320. }
  321. }
  322. free_pkb ( pkb );
  323. return 0;
  324. }
  325. /**
  326. * Single-step the network stack
  327. *
  328. * @v process Network stack process
  329. *
  330. * This polls all interfaces for received packets, and processes
  331. * packets from the RX queue.
  332. *
  333. */
  334. static void net_step ( struct process *process ) {
  335. struct net_device *netdev;
  336. struct pk_buff *pkb;
  337. /* Poll and process each network device */
  338. list_for_each_entry ( netdev, &net_devices, list ) {
  339. /* Poll for new packets. Limit RX queue size to a
  340. * single packet, because otherwise most drivers are
  341. * in serious danger of running out of memory and
  342. * having to drop packets.
  343. *
  344. * This limitation isn't relevant to devices that
  345. * preallocate packet buffers (i.e. devices with
  346. * descriptor-based RX datapaths). We might at some
  347. * point want to relax the quota for such devices.
  348. */
  349. netdev_poll ( netdev,
  350. ( list_empty ( &netdev->rx_queue ) ? 1 : 0 ) );
  351. /* Handle at most one received packet per poll. We
  352. * avoid processing more than one packet per call to
  353. * netdev_poll(), because processing the received
  354. * packet can trigger transmission of a new packet
  355. * (e.g. an ARP response). Since TX completions will
  356. * be processed as part of the poll operation, it is
  357. * easy to overflow small TX queues if multiple
  358. * packets are processed per poll.
  359. */
  360. if ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) {
  361. DBGC ( netdev, "NETDEV %p processing %p\n",
  362. netdev, pkb );
  363. netdev->ll_protocol->rx ( pkb, netdev );
  364. }
  365. }
  366. /* Re-schedule ourself */
  367. schedule ( process );
  368. }
  369. /** Networking stack process */
  370. static struct process net_process = {
  371. .step = net_step,
  372. };
  373. /** Initialise the networking stack process */
  374. static void init_net ( void ) {
  375. schedule ( &net_process );
  376. }
  377. INIT_FN ( INIT_PROCESS, init_net, NULL, NULL );