Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

netdevice.h 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #ifndef _GPXE_NETDEVICE_H
  2. #define _GPXE_NETDEVICE_H
  3. /** @file
  4. *
  5. * Network device management
  6. *
  7. */
  8. #include <stdint.h>
  9. #include <gpxe/list.h>
  10. #include <gpxe/tables.h>
  11. #include <gpxe/hotplug.h>
  12. struct pk_buff;
  13. struct net_device;
  14. struct net_protocol;
  15. struct ll_protocol;
  16. struct device;
  17. /** Maximum length of a link-layer address */
  18. #define MAX_LL_ADDR_LEN 6
  19. /** Maximum length of a link-layer header */
  20. #define MAX_LL_HEADER_LEN 16
  21. /** Maximum length of a network-layer address */
  22. #define MAX_NET_ADDR_LEN 4
  23. /**
  24. * A network-layer protocol
  25. *
  26. */
  27. struct net_protocol {
  28. /** Protocol name */
  29. const char *name;
  30. /**
  31. * Process received packet
  32. *
  33. * @v pkb Packet buffer
  34. * @v netdev Network device
  35. * @v ll_source Link-layer source address
  36. *
  37. * This method takes ownership of the packet buffer.
  38. */
  39. int ( * rx ) ( struct pk_buff *pkb, struct net_device *netdev,
  40. const void *ll_source );
  41. /**
  42. * Transcribe network-layer address
  43. *
  44. * @v net_addr Network-layer address
  45. * @ret string Human-readable transcription of address
  46. *
  47. * This method should convert the network-layer address into a
  48. * human-readable format (e.g. dotted quad notation for IPv4).
  49. *
  50. * The buffer used to hold the transcription is statically
  51. * allocated.
  52. */
  53. const char * ( *ntoa ) ( const void * net_addr );
  54. /** Network-layer protocol
  55. *
  56. * This is an ETH_P_XXX constant, in network-byte order
  57. */
  58. uint16_t net_proto;
  59. /** Network-layer address length */
  60. uint8_t net_addr_len;
  61. };
  62. /**
  63. * A link-layer protocol
  64. *
  65. */
  66. struct ll_protocol {
  67. /** Protocol name */
  68. const char *name;
  69. /**
  70. * Transmit network-layer packet via network device
  71. *
  72. * @v pkb Packet buffer
  73. * @v netdev Network device
  74. * @v net_protocol Network-layer protocol
  75. * @v ll_dest Link-layer destination address
  76. * @ret rc Return status code
  77. *
  78. * This method should prepend in the link-layer header
  79. * (e.g. the Ethernet DIX header) and transmit the packet.
  80. * This method takes ownership of the packet buffer.
  81. */
  82. int ( * tx ) ( struct pk_buff *pkb, struct net_device *netdev,
  83. struct net_protocol *net_protocol,
  84. const void *ll_dest );
  85. /**
  86. * Handle received packet
  87. *
  88. * @v pkb Packet buffer
  89. * @v netdev Network device
  90. *
  91. * This method should strip off the link-layer header
  92. * (e.g. the Ethernet DIX header) and pass the packet to
  93. * net_rx(). This method takes ownership of the packet
  94. * buffer.
  95. */
  96. int ( * rx ) ( struct pk_buff *pkb, struct net_device *netdev );
  97. /**
  98. * Transcribe link-layer address
  99. *
  100. * @v ll_addr Link-layer address
  101. * @ret string Human-readable transcription of address
  102. *
  103. * This method should convert the link-layer address into a
  104. * human-readable format.
  105. *
  106. * The buffer used to hold the transcription is statically
  107. * allocated.
  108. */
  109. const char * ( * ntoa ) ( const void * ll_addr );
  110. /** Link-layer protocol
  111. *
  112. * This is an ARPHRD_XXX constant, in network byte order.
  113. */
  114. uint16_t ll_proto;
  115. /** Link-layer address length */
  116. uint8_t ll_addr_len;
  117. /** Link-layer broadcast address */
  118. const uint8_t *ll_broadcast;
  119. };
  120. /**
  121. * A network device
  122. *
  123. * This structure represents a piece of networking hardware. It has
  124. * properties such as a link-layer address and methods for
  125. * transmitting and receiving raw packets.
  126. *
  127. * Note that this structure must represent a generic network device,
  128. * not just an Ethernet device.
  129. */
  130. struct net_device {
  131. /** List of network devices */
  132. struct list_head list;
  133. /** Name of this network device */
  134. char name[8];
  135. /** Underlying hardware device */
  136. struct device *dev;
  137. /** List of persistent reference holders */
  138. struct list_head references;
  139. /** Open network device
  140. *
  141. * @v netdev Network device
  142. * @ret rc Return status code
  143. *
  144. * This method should allocate RX packet buffers and enable
  145. * the hardware to start transmitting and receiving packets.
  146. */
  147. int ( * open ) ( struct net_device *netdev );
  148. /** Close network device
  149. *
  150. * @v netdev Network device
  151. *
  152. * This method should stop the flow of packets, and free up
  153. * any packets that are currently in the device's TX queue.
  154. */
  155. void ( * close ) ( struct net_device *netdev );
  156. /** Transmit packet
  157. *
  158. * @v netdev Network device
  159. * @v pkb Packet buffer
  160. * @ret rc Return status code
  161. *
  162. * This method should cause the hardware to initiate
  163. * transmission of the packet buffer.
  164. *
  165. * If this method returns success, the packet buffer remains
  166. * owned by the net device's TX queue, and the net device must
  167. * eventually call netdev_tx_complete() to free the buffer.
  168. * If this method returns failure, the packet buffer is
  169. * immediately released.
  170. *
  171. * This method is guaranteed to be called only when the device
  172. * is open.
  173. */
  174. int ( * transmit ) ( struct net_device *netdev, struct pk_buff *pkb );
  175. /** Poll for received packet
  176. *
  177. * @v netdev Network device
  178. * @v rx_quota Maximum number of packets to receive
  179. *
  180. * This method should cause the hardware to check for received
  181. * packets. Any received packets should be delivered via
  182. * netdev_rx(), up to a maximum of @c rx_quota packets.
  183. *
  184. * This method is guaranteed to be called only when the device
  185. * is open.
  186. */
  187. void ( * poll ) ( struct net_device *netdev, unsigned int rx_quota );
  188. /** Link-layer protocol */
  189. struct ll_protocol *ll_protocol;
  190. /** Link-layer address
  191. *
  192. * For Ethernet, this is the MAC address.
  193. */
  194. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  195. /** Current device state
  196. *
  197. * This is the bitwise-OR of zero or more NETDEV_XXX constants.
  198. */
  199. unsigned int state;
  200. /** TX packet queue */
  201. struct list_head tx_queue;
  202. /** RX packet queue */
  203. struct list_head rx_queue;
  204. /** Driver private data */
  205. void *priv;
  206. };
  207. /** Network device is open */
  208. #define NETDEV_OPEN 0x0001
  209. /** Declare a link-layer protocol */
  210. #define __ll_protocol __table ( struct ll_protocol, ll_protocols, 01 )
  211. /** Declare a network-layer protocol */
  212. #define __net_protocol __table ( struct net_protocol, net_protocols, 01 )
  213. extern struct list_head net_devices;
  214. /**
  215. * Get printable network device hardware address
  216. *
  217. * @v netdev Network device
  218. * @ret name Hardware address
  219. */
  220. static inline const char * netdev_hwaddr ( struct net_device *netdev ) {
  221. return netdev->ll_protocol->ntoa ( netdev->ll_addr );
  222. }
  223. /** Iterate over all network devices */
  224. #define for_each_netdev( netdev ) \
  225. list_for_each_entry ( (netdev), &net_devices, list )
  226. /** There exist some network devices
  227. *
  228. * @ret existence Existence of network devices
  229. */
  230. static inline int have_netdevs ( void ) {
  231. return ( ! list_empty ( &net_devices ) );
  232. }
  233. extern int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb );
  234. void netdev_tx_complete ( struct net_device *netdev, struct pk_buff *pkb );
  235. void netdev_tx_complete_next ( struct net_device *netdev );
  236. extern void netdev_rx ( struct net_device *netdev, struct pk_buff *pkb );
  237. extern int netdev_poll ( struct net_device *netdev, unsigned int rx_quota );
  238. extern struct pk_buff * netdev_rx_dequeue ( struct net_device *netdev );
  239. extern struct net_device * alloc_netdev ( size_t priv_size );
  240. extern int register_netdev ( struct net_device *netdev );
  241. extern int netdev_open ( struct net_device *netdev );
  242. extern void netdev_close ( struct net_device *netdev );
  243. extern void unregister_netdev ( struct net_device *netdev );
  244. extern void free_netdev ( struct net_device *netdev );
  245. struct net_device * find_netdev ( const char *name );
  246. struct net_device * find_pci_netdev ( unsigned int busdevfn );
  247. extern int net_tx ( struct pk_buff *pkb, struct net_device *netdev,
  248. struct net_protocol *net_protocol, const void *ll_dest );
  249. extern int net_rx ( struct pk_buff *pkb, struct net_device *netdev,
  250. uint16_t net_proto, const void *ll_source );
  251. #endif /* _GPXE_NETDEVICE_H */