Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

netdevice.h 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/refcnt.h>
  12. struct io_buffer;
  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 iobuf I/O buffer
  34. * @v netdev Network device
  35. * @v ll_source Link-layer source address
  36. *
  37. * This method takes ownership of the I/O buffer.
  38. */
  39. int ( * rx ) ( struct io_buffer *iobuf, 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 iobuf I/O 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 I/O buffer.
  81. */
  82. int ( * tx ) ( struct io_buffer *iobuf, struct net_device *netdev,
  83. struct net_protocol *net_protocol,
  84. const void *ll_dest );
  85. /**
  86. * Handle received packet
  87. *
  88. * @v iobuf I/O 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 io_buffer *iobuf, 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 header length */
  118. uint8_t ll_header_len;
  119. /** Link-layer broadcast address */
  120. const uint8_t *ll_broadcast;
  121. };
  122. /**
  123. * A network device
  124. *
  125. * This structure represents a piece of networking hardware. It has
  126. * properties such as a link-layer address and methods for
  127. * transmitting and receiving raw packets.
  128. *
  129. * Note that this structure must represent a generic network device,
  130. * not just an Ethernet device.
  131. */
  132. struct net_device {
  133. /** Reference counter */
  134. struct refcnt refcnt;
  135. /** List of network devices */
  136. struct list_head list;
  137. /** Name of this network device */
  138. char name[8];
  139. /** Underlying hardware device */
  140. struct device *dev;
  141. /** Open network device
  142. *
  143. * @v netdev Network device
  144. * @ret rc Return status code
  145. *
  146. * This method should allocate RX I/O buffers and enable
  147. * the hardware to start transmitting and receiving packets.
  148. */
  149. int ( * open ) ( struct net_device *netdev );
  150. /** Close network device
  151. *
  152. * @v netdev Network device
  153. *
  154. * This method should stop the flow of packets, and free up
  155. * any packets that are currently in the device's TX queue.
  156. */
  157. void ( * close ) ( struct net_device *netdev );
  158. /** Transmit packet
  159. *
  160. * @v netdev Network device
  161. * @v iobuf I/O buffer
  162. * @ret rc Return status code
  163. *
  164. * This method should cause the hardware to initiate
  165. * transmission of the I/O buffer.
  166. *
  167. * If this method returns success, the I/O buffer remains
  168. * owned by the net device's TX queue, and the net device must
  169. * eventually call netdev_tx_complete() to free the buffer.
  170. * If this method returns failure, the I/O buffer is
  171. * immediately released.
  172. *
  173. * This method is guaranteed to be called only when the device
  174. * is open.
  175. */
  176. int ( * transmit ) ( struct net_device *netdev, struct io_buffer *iobuf );
  177. /** Poll for received packet
  178. *
  179. * @v netdev Network device
  180. * @v rx_quota Maximum number of packets to receive
  181. *
  182. * This method should cause the hardware to check for received
  183. * packets. Any received packets should be delivered via
  184. * netdev_rx(), up to a maximum of @c rx_quota packets.
  185. *
  186. * This method is guaranteed to be called only when the device
  187. * is open.
  188. */
  189. void ( * poll ) ( struct net_device *netdev, unsigned int rx_quota );
  190. /** Link-layer protocol */
  191. struct ll_protocol *ll_protocol;
  192. /** Link-layer address
  193. *
  194. * For Ethernet, this is the MAC address.
  195. */
  196. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  197. /** Current device state
  198. *
  199. * This is the bitwise-OR of zero or more NETDEV_XXX constants.
  200. */
  201. unsigned int state;
  202. /** TX packet queue */
  203. struct list_head tx_queue;
  204. /** RX packet queue */
  205. struct list_head rx_queue;
  206. /** Driver private data */
  207. void *priv;
  208. };
  209. /** Network device is open */
  210. #define NETDEV_OPEN 0x0001
  211. /** Declare a link-layer protocol */
  212. #define __ll_protocol __table ( struct ll_protocol, ll_protocols, 01 )
  213. /** Declare a network-layer protocol */
  214. #define __net_protocol __table ( struct net_protocol, net_protocols, 01 )
  215. extern struct list_head net_devices;
  216. /**
  217. * Get printable network device hardware address
  218. *
  219. * @v netdev Network device
  220. * @ret name Hardware address
  221. */
  222. static inline const char * netdev_hwaddr ( struct net_device *netdev ) {
  223. return netdev->ll_protocol->ntoa ( netdev->ll_addr );
  224. }
  225. /** Iterate over all network devices */
  226. #define for_each_netdev( netdev ) \
  227. list_for_each_entry ( (netdev), &net_devices, list )
  228. /** There exist some network devices
  229. *
  230. * @ret existence Existence of network devices
  231. */
  232. static inline int have_netdevs ( void ) {
  233. return ( ! list_empty ( &net_devices ) );
  234. }
  235. /**
  236. * Get reference to network device
  237. *
  238. * @v netdev Network device
  239. * @ret netdev Network device
  240. */
  241. static inline __attribute__ (( always_inline )) struct net_device *
  242. netdev_get ( struct net_device *netdev ) {
  243. ref_get ( &netdev->refcnt );
  244. return netdev;
  245. }
  246. /**
  247. * Drop reference to network device
  248. *
  249. * @v netdev Network device
  250. */
  251. static inline __attribute__ (( always_inline )) void
  252. netdev_put ( struct net_device *netdev ) {
  253. ref_put ( &netdev->refcnt );
  254. }
  255. extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
  256. void netdev_tx_complete ( struct net_device *netdev, struct io_buffer *iobuf );
  257. void netdev_tx_complete_next ( struct net_device *netdev );
  258. extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
  259. extern int netdev_poll ( struct net_device *netdev, unsigned int rx_quota );
  260. extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
  261. extern struct net_device * alloc_netdev ( size_t priv_size );
  262. extern int register_netdev ( struct net_device *netdev );
  263. extern int netdev_open ( struct net_device *netdev );
  264. extern void netdev_close ( struct net_device *netdev );
  265. extern void unregister_netdev ( struct net_device *netdev );
  266. struct net_device * find_netdev ( const char *name );
  267. struct net_device * find_pci_netdev ( unsigned int busdevfn );
  268. extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  269. struct net_protocol *net_protocol, const void *ll_dest );
  270. extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  271. uint16_t net_proto, const void *ll_source );
  272. #endif /* _GPXE_NETDEVICE_H */