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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. /** Network device operations */
  123. struct net_device_operations {
  124. /** Open network device
  125. *
  126. * @v netdev Network device
  127. * @ret rc Return status code
  128. *
  129. * This method should allocate RX I/O buffers and enable
  130. * the hardware to start transmitting and receiving packets.
  131. */
  132. int ( * open ) ( struct net_device *netdev );
  133. /** Close network device
  134. *
  135. * @v netdev Network device
  136. *
  137. * This method should stop the flow of packets, and free up
  138. * any packets that are currently in the device's TX queue.
  139. */
  140. void ( * close ) ( struct net_device *netdev );
  141. /** Transmit packet
  142. *
  143. * @v netdev Network device
  144. * @v iobuf I/O buffer
  145. * @ret rc Return status code
  146. *
  147. * This method should cause the hardware to initiate
  148. * transmission of the I/O buffer.
  149. *
  150. * If this method returns success, the I/O buffer remains
  151. * owned by the net device's TX queue, and the net device must
  152. * eventually call netdev_tx_complete() to free the buffer.
  153. * If this method returns failure, the I/O buffer is
  154. * immediately released; the failure is interpreted as
  155. * "failure to enqueue buffer".
  156. *
  157. * This method is guaranteed to be called only when the device
  158. * is open.
  159. */
  160. int ( * transmit ) ( struct net_device *netdev,
  161. struct io_buffer *iobuf );
  162. /** Poll for completed and received packets
  163. *
  164. * @v netdev Network device
  165. *
  166. * This method should cause the hardware to check for
  167. * completed transmissions and received packets. Any received
  168. * packets should be delivered via netdev_rx().
  169. *
  170. * This method is guaranteed to be called only when the device
  171. * is open.
  172. */
  173. void ( * poll ) ( struct net_device *netdev );
  174. /** Enable or disable interrupts
  175. *
  176. * @v netdev Network device
  177. * @v enable Interrupts should be enabled
  178. */
  179. void ( * irq ) ( struct net_device *netdev, int enable );
  180. };
  181. /** Network device statistics */
  182. struct net_device_stats {
  183. /** Count of successfully completed transmissions */
  184. unsigned int tx_ok;
  185. /** Count of transmission errors */
  186. unsigned int tx_err;
  187. /** Count of successfully received packets */
  188. unsigned int rx_ok;
  189. /** Count of reception errors */
  190. unsigned int rx_err;
  191. };
  192. /**
  193. * A network device
  194. *
  195. * This structure represents a piece of networking hardware. It has
  196. * properties such as a link-layer address and methods for
  197. * transmitting and receiving raw packets.
  198. *
  199. * Note that this structure must represent a generic network device,
  200. * not just an Ethernet device.
  201. */
  202. struct net_device {
  203. /** Reference counter */
  204. struct refcnt refcnt;
  205. /** List of network devices */
  206. struct list_head list;
  207. /** Name of this network device */
  208. char name[8];
  209. /** Underlying hardware device */
  210. struct device *dev;
  211. /** Network device operations */
  212. struct net_device_operations *op;
  213. /** Link-layer protocol */
  214. struct ll_protocol *ll_protocol;
  215. /** Link-layer address
  216. *
  217. * For Ethernet, this is the MAC address.
  218. */
  219. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  220. /** Current device state
  221. *
  222. * This is the bitwise-OR of zero or more NETDEV_XXX constants.
  223. */
  224. unsigned int state;
  225. /** TX packet queue */
  226. struct list_head tx_queue;
  227. /** RX packet queue */
  228. struct list_head rx_queue;
  229. /** Device statistics */
  230. struct net_device_stats stats;
  231. /** Driver private data */
  232. void *priv;
  233. };
  234. /** Network device is open */
  235. #define NETDEV_OPEN 0x0001
  236. /** Declare a link-layer protocol */
  237. #define __ll_protocol __table ( struct ll_protocol, ll_protocols, 01 )
  238. /** Declare a network-layer protocol */
  239. #define __net_protocol __table ( struct net_protocol, net_protocols, 01 )
  240. extern struct list_head net_devices;
  241. extern struct net_device_operations null_netdev_operations;
  242. /**
  243. * Initialise a network device
  244. *
  245. * @v netdev Network device
  246. * @v op Network device operations
  247. */
  248. static inline void netdev_init ( struct net_device *netdev,
  249. struct net_device_operations *op ) {
  250. netdev->op = op;
  251. }
  252. /**
  253. * Stop using a network device
  254. *
  255. * @v netdev Network device
  256. *
  257. * Drivers should call this method immediately before the final call
  258. * to netdev_put().
  259. */
  260. static inline void netdev_nullify ( struct net_device *netdev ) {
  261. netdev->op = &null_netdev_operations;
  262. }
  263. /**
  264. * Get printable network device hardware address
  265. *
  266. * @v netdev Network device
  267. * @ret name Hardware address
  268. */
  269. static inline const char * netdev_hwaddr ( struct net_device *netdev ) {
  270. return netdev->ll_protocol->ntoa ( netdev->ll_addr );
  271. }
  272. /** Iterate over all network devices */
  273. #define for_each_netdev( netdev ) \
  274. list_for_each_entry ( (netdev), &net_devices, list )
  275. /** There exist some network devices
  276. *
  277. * @ret existence Existence of network devices
  278. */
  279. static inline int have_netdevs ( void ) {
  280. return ( ! list_empty ( &net_devices ) );
  281. }
  282. /**
  283. * Get reference to network device
  284. *
  285. * @v netdev Network device
  286. * @ret netdev Network device
  287. */
  288. static inline __attribute__ (( always_inline )) struct net_device *
  289. netdev_get ( struct net_device *netdev ) {
  290. ref_get ( &netdev->refcnt );
  291. return netdev;
  292. }
  293. /**
  294. * Drop reference to network device
  295. *
  296. * @v netdev Network device
  297. */
  298. static inline __attribute__ (( always_inline )) void
  299. netdev_put ( struct net_device *netdev ) {
  300. ref_put ( &netdev->refcnt );
  301. }
  302. extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
  303. extern void netdev_tx_complete_err ( struct net_device *netdev,
  304. struct io_buffer *iobuf, int rc );
  305. extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
  306. extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
  307. extern void netdev_rx_err ( struct net_device *netdev,
  308. struct io_buffer *iobuf, int rc );
  309. extern void netdev_poll ( struct net_device *netdev );
  310. extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
  311. extern struct net_device * alloc_netdev ( size_t priv_size );
  312. extern int register_netdev ( struct net_device *netdev );
  313. extern int netdev_open ( struct net_device *netdev );
  314. extern void netdev_close ( struct net_device *netdev );
  315. extern void unregister_netdev ( struct net_device *netdev );
  316. extern void netdev_irq ( struct net_device *netdev, int enable );
  317. extern struct net_device * find_netdev ( const char *name );
  318. extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
  319. unsigned int location );
  320. extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  321. struct net_protocol *net_protocol, const void *ll_dest );
  322. extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  323. uint16_t net_proto, const void *ll_source );
  324. /**
  325. * Complete network transmission
  326. *
  327. * @v netdev Network device
  328. * @v iobuf I/O buffer
  329. *
  330. * The packet must currently be in the network device's TX queue.
  331. */
  332. static inline void netdev_tx_complete ( struct net_device *netdev,
  333. struct io_buffer *iobuf ) {
  334. netdev_tx_complete_err ( netdev, iobuf, 0 );
  335. }
  336. /**
  337. * Complete network transmission
  338. *
  339. * @v netdev Network device
  340. *
  341. * Completes the oldest outstanding packet in the TX queue.
  342. */
  343. static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
  344. netdev_tx_complete_next_err ( netdev, 0 );
  345. }
  346. #endif /* _GPXE_NETDEVICE_H */