You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netdevice.h 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /** List of persistent reference holders */
  133. struct list_head references;
  134. /** Open network device
  135. *
  136. * @v netdev Network device
  137. * @ret rc Return status code
  138. *
  139. * This method should allocate RX packet buffers and enable
  140. * the hardware to start transmitting and receiving packets.
  141. */
  142. int ( * open ) ( struct net_device *netdev );
  143. /** Close network device
  144. *
  145. * @v netdev Network device
  146. *
  147. * This method should stop the flow of packets, and free up
  148. * any packets that are currently in the device's TX queue.
  149. */
  150. void ( * close ) ( struct net_device *netdev );
  151. /** Transmit packet
  152. *
  153. * @v netdev Network device
  154. * @v pkb Packet buffer
  155. * @ret rc Return status code
  156. *
  157. * This method should cause the hardware to initiate
  158. * transmission of the packet buffer.
  159. *
  160. * If this method returns success, the packet buffer remains
  161. * owned by the net device's TX queue, and the net device must
  162. * eventually call netdev_tx_complete() to free the buffer.
  163. * If this method returns failure, the packet buffer is
  164. * immediately released.
  165. *
  166. * This method is guaranteed to be called only when the device
  167. * is open.
  168. */
  169. int ( * transmit ) ( struct net_device *netdev, struct pk_buff *pkb );
  170. /** Poll for received packet
  171. *
  172. * @v netdev Network device
  173. *
  174. * This method should cause the hardware to check for received
  175. * packets. Any received packets should be delivered via
  176. * netdev_rx().
  177. *
  178. * This method is guaranteed to be called only when the device
  179. * is open.
  180. */
  181. void ( * poll ) ( struct net_device *netdev );
  182. /** Link-layer protocol */
  183. struct ll_protocol *ll_protocol;
  184. /** Link-layer address
  185. *
  186. * For Ethernet, this is the MAC address.
  187. */
  188. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  189. /** Current device state
  190. *
  191. * This is the bitwise-OR of zero or more NETDEV_XXX constants.
  192. */
  193. unsigned int state;
  194. /** TX packet queue */
  195. struct list_head tx_queue;
  196. /** RX packet queue */
  197. struct list_head rx_queue;
  198. /** Driver private data */
  199. void *priv;
  200. };
  201. /** Network device is open */
  202. #define NETDEV_OPEN 0x0001
  203. /** Declare a link-layer protocol */
  204. #define __ll_protocol __table ( ll_protocols, 01 )
  205. /** Declare a network-layer protocol */
  206. #define __net_protocol __table ( net_protocols, 01 )
  207. /**
  208. * Get network device name
  209. *
  210. * @v netdev Network device
  211. * @ret name Network device name
  212. *
  213. * The name will be the device's link-layer address.
  214. */
  215. static inline const char * netdev_name ( struct net_device *netdev ) {
  216. return netdev->ll_protocol->ntoa ( netdev->ll_addr );
  217. }
  218. extern int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb );
  219. void netdev_tx_complete ( struct net_device *netdev, struct pk_buff *pkb );
  220. void netdev_tx_complete_next ( struct net_device *netdev );
  221. extern void netdev_rx ( struct net_device *netdev, struct pk_buff *pkb );
  222. extern int netdev_poll ( struct net_device *netdev );
  223. extern struct pk_buff * netdev_rx_dequeue ( struct net_device *netdev );
  224. extern struct net_device * alloc_netdev ( size_t priv_size );
  225. extern int register_netdev ( struct net_device *netdev );
  226. extern int netdev_open ( struct net_device *netdev );
  227. extern void netdev_close ( struct net_device *netdev );
  228. extern void unregister_netdev ( struct net_device *netdev );
  229. extern void free_netdev ( struct net_device *netdev );
  230. extern struct net_device * next_netdev ( void );
  231. extern int net_tx ( struct pk_buff *pkb, struct net_device *netdev,
  232. struct net_protocol *net_protocol, const void *ll_dest );
  233. extern int net_rx ( struct pk_buff *pkb, struct net_device *netdev,
  234. uint16_t net_proto, const void *ll_source );
  235. #endif /* _GPXE_NETDEVICE_H */