選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

netdevice.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <gpxe/if_ether.h>
  24. #include <gpxe/pkbuff.h>
  25. #include <gpxe/tables.h>
  26. #include <gpxe/process.h>
  27. #include <gpxe/init.h>
  28. #include <gpxe/netdevice.h>
  29. /** @file
  30. *
  31. * Network device management
  32. *
  33. */
  34. /** Registered network-layer protocols */
  35. static struct net_protocol net_protocols[0] __table_start ( net_protocols );
  36. static struct net_protocol net_protocols_end[0] __table_end ( net_protocols );
  37. /** List of network devices */
  38. static LIST_HEAD ( net_devices );
  39. /**
  40. * Transmit raw packet via network device
  41. *
  42. * @v netdev Network device
  43. * @v pkb Packet buffer
  44. * @ret rc Return status code
  45. *
  46. * Transmits the packet via the specified network device. This
  47. * function takes ownership of the packet buffer.
  48. */
  49. int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb ) {
  50. DBG ( "%s transmitting %p+%zx\n", netdev_name ( netdev ),
  51. pkb->data, pkb_len ( pkb ) );
  52. return netdev->transmit ( netdev, pkb );
  53. }
  54. /**
  55. * Add packet to receive queue
  56. *
  57. * @v netdev Network device
  58. * @v pkb Packet buffer
  59. *
  60. * The packet is added to the network device's RX queue. This
  61. * function takes ownership of the packet buffer.
  62. */
  63. void netdev_rx ( struct net_device *netdev, struct pk_buff *pkb ) {
  64. DBG ( "%s received %p+%zx\n", netdev_name ( netdev ),
  65. pkb->data, pkb_len ( pkb ) );
  66. list_add_tail ( &pkb->list, &netdev->rx_queue );
  67. }
  68. /**
  69. * Transmit network-layer packet
  70. *
  71. * @v pkb Packet buffer
  72. * @v netdev Network device
  73. * @v net_protocol Network-layer protocol
  74. * @v ll_dest Destination link-layer address
  75. * @ret rc Return status code
  76. *
  77. * Prepends link-layer headers to the packet buffer and transmits the
  78. * packet via the specified network device. This function takes
  79. * ownership of the packet buffer.
  80. */
  81. int net_tx ( struct pk_buff *pkb, struct net_device *netdev,
  82. struct net_protocol *net_protocol, const void *ll_dest ) {
  83. return netdev->ll_protocol->tx ( pkb, netdev, net_protocol, ll_dest );
  84. }
  85. /**
  86. * Process received network-layer packet
  87. *
  88. * @v pkb Packet buffer
  89. * @v netdev Network device
  90. * @v net_proto Network-layer protocol, in network-byte order
  91. * @v ll_source Source link-layer address
  92. * @ret rc Return status code
  93. */
  94. int net_rx ( struct pk_buff *pkb, struct net_device *netdev,
  95. uint16_t net_proto, const void *ll_source ) {
  96. struct net_protocol *net_protocol;
  97. /* Hand off to network-layer protocol, if any */
  98. for ( net_protocol = net_protocols ; net_protocol < net_protocols_end ;
  99. net_protocol++ ) {
  100. if ( net_protocol->net_proto == net_proto ) {
  101. return net_protocol->rx ( pkb, netdev, ll_source );
  102. }
  103. }
  104. free_pkb ( pkb );
  105. return 0;
  106. }
  107. /**
  108. * Poll for packet on network device
  109. *
  110. * @v netdev Network device
  111. * @ret True There are packets present in the receive queue
  112. * @ret False There are no packets present in the receive queue
  113. *
  114. * Polls the network device for received packets. Any received
  115. * packets will be added to the RX packet queue via netdev_rx().
  116. */
  117. int netdev_poll ( struct net_device *netdev ) {
  118. netdev->poll ( netdev );
  119. return ( ! list_empty ( &netdev->rx_queue ) );
  120. }
  121. /**
  122. * Remove packet from device's receive queue
  123. *
  124. * @v netdev Network device
  125. * @ret pkb Packet buffer, or NULL
  126. *
  127. * Removes the first packet from the device's RX queue and returns it.
  128. * Ownership of the packet is transferred to the caller.
  129. */
  130. struct pk_buff * netdev_rx_dequeue ( struct net_device *netdev ) {
  131. struct pk_buff *pkb;
  132. list_for_each_entry ( pkb, &netdev->rx_queue, list ) {
  133. list_del ( &pkb->list );
  134. return pkb;
  135. }
  136. return NULL;
  137. }
  138. /**
  139. * Allocate network device
  140. *
  141. * @v priv_size Size of private data area (net_device::priv)
  142. * @ret netdev Network device, or NULL
  143. *
  144. * Allocates space for a network device and its private data area.
  145. */
  146. struct net_device * alloc_netdev ( size_t priv_size ) {
  147. struct net_device *netdev;
  148. netdev = calloc ( 1, sizeof ( *netdev ) + priv_size );
  149. if ( netdev ) {
  150. INIT_LIST_HEAD ( &netdev->rx_queue );
  151. netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
  152. }
  153. return netdev;
  154. }
  155. /**
  156. * Register network device
  157. *
  158. * @v netdev Network device
  159. * @ret rc Return status code
  160. *
  161. * Adds the network device to the list of network devices.
  162. */
  163. int register_netdev ( struct net_device *netdev ) {
  164. /* Add to device list */
  165. list_add_tail ( &netdev->list, &net_devices );
  166. DBG ( "%s registered\n", netdev_name ( netdev ) );
  167. return 0;
  168. }
  169. /**
  170. * Unregister network device
  171. *
  172. * @v netdev Network device
  173. *
  174. * Removes the network device from the list of network devices.
  175. */
  176. void unregister_netdev ( struct net_device *netdev ) {
  177. struct pk_buff *pkb;
  178. /* Discard any packets in the RX queue */
  179. while ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) {
  180. DBG ( "%s discarding %p+%zx\n", netdev_name ( netdev ),
  181. pkb->data, pkb_len ( pkb ) );
  182. free_pkb ( pkb );
  183. }
  184. /* Remove from device list */
  185. list_del ( &netdev->list );
  186. DBG ( "%s unregistered\n", netdev_name ( netdev ) );
  187. }
  188. /**
  189. * Free network device
  190. *
  191. * @v netdev Network device
  192. */
  193. void free_netdev ( struct net_device *netdev ) {
  194. free ( netdev );
  195. }
  196. /**
  197. * Iterate through network devices
  198. *
  199. * @ret netdev Network device, or NULL
  200. *
  201. * This returns the registered network devices in the order of
  202. * registration. If no network devices are registered, it will return
  203. * NULL.
  204. */
  205. struct net_device * next_netdev ( void ) {
  206. struct net_device *netdev;
  207. list_for_each_entry ( netdev, &net_devices, list ) {
  208. list_del ( &netdev->list );
  209. list_add_tail ( &netdev->list, &net_devices );
  210. return netdev;
  211. }
  212. return NULL;
  213. }
  214. /**
  215. * Single-step the network stack
  216. *
  217. * @v process Network stack process
  218. *
  219. * This polls all interfaces for any received packets, and processes
  220. * at most one packet from the RX queue.
  221. *
  222. * We avoid processing all received packets, because processing the
  223. * received packet can trigger transmission of a new packet (e.g. an
  224. * ARP response). Since TX completions will be processed as part of
  225. * the poll operation, it is easy to overflow small TX queues if
  226. * multiple packets are processed per poll.
  227. */
  228. static void net_step ( struct process *process ) {
  229. struct net_device *netdev;
  230. struct pk_buff *pkb;
  231. /* Poll and process each network device */
  232. list_for_each_entry ( netdev, &net_devices, list ) {
  233. /* Poll for new packets */
  234. netdev_poll ( netdev );
  235. /* Handle at most one received packet per poll */
  236. if ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) {
  237. DBG ( "%s processing %p+%zx\n", netdev_name ( netdev ),
  238. pkb->data, pkb_len ( pkb ) );
  239. netdev->ll_protocol->rx ( pkb, netdev );
  240. }
  241. }
  242. /* Re-schedule ourself */
  243. schedule ( process );
  244. }
  245. /** Networking stack process */
  246. static struct process net_process = {
  247. .step = net_step,
  248. };
  249. /** Initialise the networking stack process */
  250. static void init_net ( void ) {
  251. schedule ( &net_process );
  252. }
  253. INIT_FN ( INIT_PROCESS, init_net, NULL, NULL );