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

netdevice.h 7.1KB

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