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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #include <gpxe/settings.h>
  13. struct io_buffer;
  14. struct net_device;
  15. struct net_protocol;
  16. struct ll_protocol;
  17. struct device;
  18. /** Maximum length of a link-layer address */
  19. #define MAX_LL_ADDR_LEN 20
  20. /** Maximum length of a link-layer header */
  21. #define MAX_LL_HEADER_LEN 32
  22. /** Maximum length of a network-layer address */
  23. #define MAX_NET_ADDR_LEN 4
  24. /**
  25. * A network-layer protocol
  26. *
  27. */
  28. struct net_protocol {
  29. /** Protocol name */
  30. const char *name;
  31. /**
  32. * Process received packet
  33. *
  34. * @v iobuf I/O buffer
  35. * @v netdev Network device
  36. * @v ll_source Link-layer source address
  37. *
  38. * This method takes ownership of the I/O buffer.
  39. */
  40. int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev,
  41. const void *ll_source );
  42. /**
  43. * Transcribe network-layer address
  44. *
  45. * @v net_addr Network-layer address
  46. * @ret string Human-readable transcription of address
  47. *
  48. * This method should convert the network-layer address into a
  49. * human-readable format (e.g. dotted quad notation for IPv4).
  50. *
  51. * The buffer used to hold the transcription is statically
  52. * allocated.
  53. */
  54. const char * ( *ntoa ) ( const void * net_addr );
  55. /** Network-layer protocol
  56. *
  57. * This is an ETH_P_XXX constant, in network-byte order
  58. */
  59. uint16_t net_proto;
  60. /** Network-layer address length */
  61. uint8_t net_addr_len;
  62. };
  63. /**
  64. * A link-layer protocol
  65. *
  66. */
  67. struct ll_protocol {
  68. /** Protocol name */
  69. const char *name;
  70. /**
  71. * Add link-layer header
  72. *
  73. * @v iobuf I/O buffer
  74. * @v netdev Network device
  75. * @v net_protocol Network-layer protocol
  76. * @v ll_dest Link-layer destination address
  77. * @ret rc Return status code
  78. *
  79. * This method should prepend in the link-layer header
  80. * (e.g. the Ethernet DIX header).
  81. */
  82. int ( * push ) ( struct io_buffer *iobuf, struct net_device *netdev,
  83. struct net_protocol *net_protocol,
  84. const void *ll_dest );
  85. /**
  86. * Remove link-layer header
  87. *
  88. * @v iobuf I/O buffer
  89. * @v netdev Network device
  90. * @v net_proto Network-layer protocol, in network-byte order
  91. * @v ll_source Source link-layer address
  92. * @ret rc Return status code
  93. *
  94. * This method should strip off the link-layer header
  95. * (e.g. the Ethernet DIX header) and return the protocol and
  96. * source link-layer address. The method must not alter the
  97. * packet content, and may return the link-layer address as a
  98. * pointer to data within the packet.
  99. */
  100. int ( * pull ) ( struct io_buffer *iobuf, struct net_device *netdev,
  101. uint16_t *net_proto, const void **ll_source );
  102. /**
  103. * Transcribe link-layer address
  104. *
  105. * @v ll_addr Link-layer address
  106. * @ret string Human-readable transcription of address
  107. *
  108. * This method should convert the link-layer address into a
  109. * human-readable format.
  110. *
  111. * The buffer used to hold the transcription is statically
  112. * allocated.
  113. */
  114. const char * ( * ntoa ) ( const void * ll_addr );
  115. /** Link-layer protocol
  116. *
  117. * This is an ARPHRD_XXX constant, in network byte order.
  118. */
  119. uint16_t ll_proto;
  120. /** Link-layer address length */
  121. uint8_t ll_addr_len;
  122. /** Link-layer header length */
  123. uint8_t ll_header_len;
  124. /** Link-layer broadcast address */
  125. const uint8_t *ll_broadcast;
  126. };
  127. /** Network device operations */
  128. struct net_device_operations {
  129. /** Open network device
  130. *
  131. * @v netdev Network device
  132. * @ret rc Return status code
  133. *
  134. * This method should allocate RX I/O buffers and enable
  135. * the hardware to start transmitting and receiving packets.
  136. */
  137. int ( * open ) ( struct net_device *netdev );
  138. /** Close network device
  139. *
  140. * @v netdev Network device
  141. *
  142. * This method should stop the flow of packets, and free up
  143. * any packets that are currently in the device's TX queue.
  144. */
  145. void ( * close ) ( struct net_device *netdev );
  146. /** Transmit packet
  147. *
  148. * @v netdev Network device
  149. * @v iobuf I/O buffer
  150. * @ret rc Return status code
  151. *
  152. * This method should cause the hardware to initiate
  153. * transmission of the I/O buffer.
  154. *
  155. * If this method returns success, the I/O buffer remains
  156. * owned by the net device's TX queue, and the net device must
  157. * eventually call netdev_tx_complete() to free the buffer.
  158. * If this method returns failure, the I/O buffer is
  159. * immediately released; the failure is interpreted as
  160. * "failure to enqueue buffer".
  161. *
  162. * This method is guaranteed to be called only when the device
  163. * is open.
  164. */
  165. int ( * transmit ) ( struct net_device *netdev,
  166. struct io_buffer *iobuf );
  167. /** Poll for completed and received packets
  168. *
  169. * @v netdev Network device
  170. *
  171. * This method should cause the hardware to check for
  172. * completed transmissions and received packets. Any received
  173. * packets should be delivered via netdev_rx().
  174. *
  175. * This method is guaranteed to be called only when the device
  176. * is open.
  177. */
  178. void ( * poll ) ( struct net_device *netdev );
  179. /** Enable or disable interrupts
  180. *
  181. * @v netdev Network device
  182. * @v enable Interrupts should be enabled
  183. */
  184. void ( * irq ) ( struct net_device *netdev, int enable );
  185. };
  186. /** Network device statistics */
  187. struct net_device_stats {
  188. /** Count of successfully completed transmissions */
  189. unsigned int tx_ok;
  190. /** Count of transmission errors */
  191. unsigned int tx_err;
  192. /** Count of successfully received packets */
  193. unsigned int rx_ok;
  194. /** Count of reception errors */
  195. unsigned int rx_err;
  196. };
  197. /**
  198. * A network device
  199. *
  200. * This structure represents a piece of networking hardware. It has
  201. * properties such as a link-layer address and methods for
  202. * transmitting and receiving raw packets.
  203. *
  204. * Note that this structure must represent a generic network device,
  205. * not just an Ethernet device.
  206. */
  207. struct net_device {
  208. /** Reference counter */
  209. struct refcnt refcnt;
  210. /** List of network devices */
  211. struct list_head list;
  212. /** Name of this network device */
  213. char name[8];
  214. /** Underlying hardware device */
  215. struct device *dev;
  216. /** Network device operations */
  217. struct net_device_operations *op;
  218. /** Link-layer protocol */
  219. struct ll_protocol *ll_protocol;
  220. /** Link-layer address
  221. *
  222. * For Ethernet, this is the MAC address.
  223. */
  224. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  225. /** Current device state
  226. *
  227. * This is the bitwise-OR of zero or more NETDEV_XXX constants.
  228. */
  229. unsigned int state;
  230. /** TX packet queue */
  231. struct list_head tx_queue;
  232. /** RX packet queue */
  233. struct list_head rx_queue;
  234. /** Device statistics */
  235. struct net_device_stats stats;
  236. /** Configuration settings applicable to this device */
  237. struct simple_settings settings;
  238. /** Driver private data */
  239. void *priv;
  240. };
  241. /** Network device is open */
  242. #define NETDEV_OPEN 0x0001
  243. /** Network device has link */
  244. #define NETDEV_LINK_UP 0x0002
  245. /** Declare a link-layer protocol */
  246. #define __ll_protocol __table ( struct ll_protocol, ll_protocols, 01 )
  247. /** Declare a network-layer protocol */
  248. #define __net_protocol __table ( struct net_protocol, net_protocols, 01 )
  249. extern struct list_head net_devices;
  250. extern struct net_device_operations null_netdev_operations;
  251. /**
  252. * Initialise a network device
  253. *
  254. * @v netdev Network device
  255. * @v op Network device operations
  256. */
  257. static inline void netdev_init ( struct net_device *netdev,
  258. struct net_device_operations *op ) {
  259. netdev->op = op;
  260. }
  261. /**
  262. * Stop using a network device
  263. *
  264. * @v netdev Network device
  265. *
  266. * Drivers should call this method immediately before the final call
  267. * to netdev_put().
  268. */
  269. static inline void netdev_nullify ( struct net_device *netdev ) {
  270. netdev->op = &null_netdev_operations;
  271. }
  272. /**
  273. * Get printable network device hardware address
  274. *
  275. * @v netdev Network device
  276. * @ret name Hardware address
  277. */
  278. static inline const char * netdev_hwaddr ( struct net_device *netdev ) {
  279. return netdev->ll_protocol->ntoa ( netdev->ll_addr );
  280. }
  281. /** Iterate over all network devices */
  282. #define for_each_netdev( netdev ) \
  283. list_for_each_entry ( (netdev), &net_devices, list )
  284. /** There exist some network devices
  285. *
  286. * @ret existence Existence of network devices
  287. */
  288. static inline int have_netdevs ( void ) {
  289. return ( ! list_empty ( &net_devices ) );
  290. }
  291. /**
  292. * Get reference to network device
  293. *
  294. * @v netdev Network device
  295. * @ret netdev Network device
  296. */
  297. static inline __attribute__ (( always_inline )) struct net_device *
  298. netdev_get ( struct net_device *netdev ) {
  299. ref_get ( &netdev->refcnt );
  300. return netdev;
  301. }
  302. /**
  303. * Drop reference to network device
  304. *
  305. * @v netdev Network device
  306. */
  307. static inline __attribute__ (( always_inline )) void
  308. netdev_put ( struct net_device *netdev ) {
  309. ref_put ( &netdev->refcnt );
  310. }
  311. /**
  312. * Get driver private area for this network device
  313. *
  314. * @v netdev Network device
  315. * @ret priv Driver private area for this network device
  316. */
  317. static inline __attribute__ (( always_inline )) void *
  318. netdev_priv ( struct net_device *netdev ) {
  319. return netdev->priv;
  320. }
  321. /**
  322. * Get per-netdevice configuration settings block
  323. *
  324. * @v netdev Network device
  325. * @ret settings Settings block
  326. */
  327. static inline __attribute__ (( always_inline )) struct settings *
  328. netdev_settings ( struct net_device *netdev ) {
  329. return &netdev->settings.settings;
  330. }
  331. /**
  332. * Mark network device as having link up
  333. *
  334. * @v netdev Network device
  335. */
  336. static inline __attribute__ (( always_inline )) void
  337. netdev_link_up ( struct net_device *netdev ) {
  338. netdev->state |= NETDEV_LINK_UP;
  339. }
  340. /**
  341. * Mark network device as having link down
  342. *
  343. * @v netdev Network device
  344. */
  345. static inline __attribute__ (( always_inline )) void
  346. netdev_link_down ( struct net_device *netdev ) {
  347. netdev->state &= ~NETDEV_LINK_UP;
  348. }
  349. /**
  350. * Check link state of network device
  351. *
  352. * @v netdev Network device
  353. * @ret link_up Link is up
  354. */
  355. static inline __attribute__ (( always_inline )) int
  356. netdev_link_ok ( struct net_device *netdev ) {
  357. return ( netdev->state & NETDEV_LINK_UP );
  358. }
  359. extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
  360. extern void netdev_tx_complete_err ( struct net_device *netdev,
  361. struct io_buffer *iobuf, int rc );
  362. extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
  363. extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
  364. extern void netdev_rx_err ( struct net_device *netdev,
  365. struct io_buffer *iobuf, int rc );
  366. extern void netdev_poll ( struct net_device *netdev );
  367. extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
  368. extern struct net_device * alloc_netdev ( size_t priv_size );
  369. extern int register_netdev ( struct net_device *netdev );
  370. extern int netdev_open ( struct net_device *netdev );
  371. extern void netdev_close ( struct net_device *netdev );
  372. extern void unregister_netdev ( struct net_device *netdev );
  373. extern void netdev_irq ( struct net_device *netdev, int enable );
  374. extern struct net_device * find_netdev ( const char *name );
  375. extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
  376. unsigned int location );
  377. extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  378. struct net_protocol *net_protocol, const void *ll_dest );
  379. extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  380. uint16_t net_proto, const void *ll_source );
  381. extern struct settings_operations netdev_settings_operations;
  382. /**
  383. * Complete network transmission
  384. *
  385. * @v netdev Network device
  386. * @v iobuf I/O buffer
  387. *
  388. * The packet must currently be in the network device's TX queue.
  389. */
  390. static inline void netdev_tx_complete ( struct net_device *netdev,
  391. struct io_buffer *iobuf ) {
  392. netdev_tx_complete_err ( netdev, iobuf, 0 );
  393. }
  394. /**
  395. * Complete network transmission
  396. *
  397. * @v netdev Network device
  398. *
  399. * Completes the oldest outstanding packet in the TX queue.
  400. */
  401. static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
  402. netdev_tx_complete_next_err ( netdev, 0 );
  403. }
  404. #endif /* _GPXE_NETDEVICE_H */