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

netdevice.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 6
  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 ll_dest Link-layer destination address
  75. * @v ll_source Source link-layer address
  76. * @v net_proto Network-layer protocol, in network-byte order
  77. * @ret rc Return status code
  78. */
  79. int ( * push ) ( struct io_buffer *iobuf, const void *ll_dest,
  80. const void *ll_source, uint16_t net_proto );
  81. /**
  82. * Remove link-layer header
  83. *
  84. * @v iobuf I/O buffer
  85. * @ret ll_dest Link-layer destination address
  86. * @ret ll_source Source link-layer address
  87. * @ret net_proto Network-layer protocol, in network-byte order
  88. * @ret rc Return status code
  89. */
  90. int ( * pull ) ( struct io_buffer *iobuf, const void **ll_dest,
  91. const void **ll_source, uint16_t *net_proto );
  92. /**
  93. * Transcribe link-layer address
  94. *
  95. * @v ll_addr Link-layer address
  96. * @ret string Human-readable transcription of address
  97. *
  98. * This method should convert the link-layer address into a
  99. * human-readable format.
  100. *
  101. * The buffer used to hold the transcription is statically
  102. * allocated.
  103. */
  104. const char * ( * ntoa ) ( const void * ll_addr );
  105. /**
  106. * Hash multicast address
  107. *
  108. * @v af Address family
  109. * @v net_addr Network-layer address
  110. * @v ll_addr Link-layer address to fill in
  111. * @ret rc Return status code
  112. */
  113. int ( * mc_hash ) ( unsigned int af, const void *net_addr,
  114. 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 error */
  187. struct net_device_error {
  188. /** Error status code */
  189. int rc;
  190. /** Error count */
  191. unsigned int count;
  192. };
  193. /** Maximum number of unique errors that we will keep track of */
  194. #define NETDEV_MAX_UNIQUE_ERRORS 4
  195. /** Network device statistics */
  196. struct net_device_stats {
  197. /** Count of successful completions */
  198. unsigned int good;
  199. /** Count of error completions */
  200. unsigned int bad;
  201. /** Error breakdowns */
  202. struct net_device_error errors[NETDEV_MAX_UNIQUE_ERRORS];
  203. };
  204. /**
  205. * A network device
  206. *
  207. * This structure represents a piece of networking hardware. It has
  208. * properties such as a link-layer address and methods for
  209. * transmitting and receiving raw packets.
  210. *
  211. * Note that this structure must represent a generic network device,
  212. * not just an Ethernet device.
  213. */
  214. struct net_device {
  215. /** Reference counter */
  216. struct refcnt refcnt;
  217. /** List of network devices */
  218. struct list_head list;
  219. /** List of open network devices */
  220. struct list_head open_list;
  221. /** Name of this network device */
  222. char name[8];
  223. /** Underlying hardware device */
  224. struct device *dev;
  225. /** Network device operations */
  226. struct net_device_operations *op;
  227. /** Link-layer protocol */
  228. struct ll_protocol *ll_protocol;
  229. /** Link-layer address
  230. *
  231. * For Ethernet, this is the MAC address.
  232. */
  233. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  234. /** Current device state
  235. *
  236. * This is the bitwise-OR of zero or more NETDEV_XXX constants.
  237. */
  238. unsigned int state;
  239. /** Maximum packet length
  240. *
  241. * This length includes any link-layer headers.
  242. */
  243. size_t max_pkt_len;
  244. /** TX packet queue */
  245. struct list_head tx_queue;
  246. /** RX packet queue */
  247. struct list_head rx_queue;
  248. /** TX statistics */
  249. struct net_device_stats tx_stats;
  250. /** RX statistics */
  251. struct net_device_stats rx_stats;
  252. /** Configuration settings applicable to this device */
  253. struct simple_settings settings;
  254. /** Driver private data */
  255. void *priv;
  256. };
  257. /** Network device is open */
  258. #define NETDEV_OPEN 0x0001
  259. /** Network device has link */
  260. #define NETDEV_LINK_UP 0x0002
  261. /** Link-layer protocol table */
  262. #define LL_PROTOCOLS "ll_protocols"
  263. /** Declare a link-layer protocol */
  264. #define __ll_protocol __table ( struct ll_protocol, LL_PROTOCOLS, 01 )
  265. /** Network-layer protocol table */
  266. #define NET_PROTOCOLS "net_protocols"
  267. /** Declare a network-layer protocol */
  268. #define __net_protocol __table ( struct net_protocol, NET_PROTOCOLS, 01 )
  269. extern struct list_head net_devices;
  270. extern struct net_device_operations null_netdev_operations;
  271. /**
  272. * Initialise a network device
  273. *
  274. * @v netdev Network device
  275. * @v op Network device operations
  276. */
  277. static inline void netdev_init ( struct net_device *netdev,
  278. struct net_device_operations *op ) {
  279. netdev->op = op;
  280. }
  281. /**
  282. * Stop using a network device
  283. *
  284. * @v netdev Network device
  285. *
  286. * Drivers should call this method immediately before the final call
  287. * to netdev_put().
  288. */
  289. static inline void netdev_nullify ( struct net_device *netdev ) {
  290. netdev->op = &null_netdev_operations;
  291. }
  292. /**
  293. * Get printable network device hardware address
  294. *
  295. * @v netdev Network device
  296. * @ret name Hardware address
  297. */
  298. static inline const char * netdev_hwaddr ( struct net_device *netdev ) {
  299. return netdev->ll_protocol->ntoa ( netdev->ll_addr );
  300. }
  301. /** Iterate over all network devices */
  302. #define for_each_netdev( netdev ) \
  303. list_for_each_entry ( (netdev), &net_devices, list )
  304. /** There exist some network devices
  305. *
  306. * @ret existence Existence of network devices
  307. */
  308. static inline int have_netdevs ( void ) {
  309. return ( ! list_empty ( &net_devices ) );
  310. }
  311. /**
  312. * Get reference to network device
  313. *
  314. * @v netdev Network device
  315. * @ret netdev Network device
  316. */
  317. static inline __attribute__ (( always_inline )) struct net_device *
  318. netdev_get ( struct net_device *netdev ) {
  319. ref_get ( &netdev->refcnt );
  320. return netdev;
  321. }
  322. /**
  323. * Drop reference to network device
  324. *
  325. * @v netdev Network device
  326. */
  327. static inline __attribute__ (( always_inline )) void
  328. netdev_put ( struct net_device *netdev ) {
  329. ref_put ( &netdev->refcnt );
  330. }
  331. /**
  332. * Get driver private area for this network device
  333. *
  334. * @v netdev Network device
  335. * @ret priv Driver private area for this network device
  336. */
  337. static inline __attribute__ (( always_inline )) void *
  338. netdev_priv ( struct net_device *netdev ) {
  339. return netdev->priv;
  340. }
  341. /**
  342. * Get per-netdevice configuration settings block
  343. *
  344. * @v netdev Network device
  345. * @ret settings Settings block
  346. */
  347. static inline __attribute__ (( always_inline )) struct settings *
  348. netdev_settings ( struct net_device *netdev ) {
  349. return &netdev->settings.settings;
  350. }
  351. /**
  352. * Mark network device as having link up
  353. *
  354. * @v netdev Network device
  355. */
  356. static inline __attribute__ (( always_inline )) void
  357. netdev_link_up ( struct net_device *netdev ) {
  358. netdev->state |= NETDEV_LINK_UP;
  359. }
  360. /**
  361. * Mark network device as having link down
  362. *
  363. * @v netdev Network device
  364. */
  365. static inline __attribute__ (( always_inline )) void
  366. netdev_link_down ( struct net_device *netdev ) {
  367. netdev->state &= ~NETDEV_LINK_UP;
  368. }
  369. /**
  370. * Check link state of network device
  371. *
  372. * @v netdev Network device
  373. * @ret link_up Link is up
  374. */
  375. static inline __attribute__ (( always_inline )) int
  376. netdev_link_ok ( struct net_device *netdev ) {
  377. return ( netdev->state & NETDEV_LINK_UP );
  378. }
  379. extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
  380. extern void netdev_tx_complete_err ( struct net_device *netdev,
  381. struct io_buffer *iobuf, int rc );
  382. extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
  383. extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
  384. extern void netdev_rx_err ( struct net_device *netdev,
  385. struct io_buffer *iobuf, int rc );
  386. extern void netdev_poll ( struct net_device *netdev );
  387. extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
  388. extern struct net_device * alloc_netdev ( size_t priv_size );
  389. extern int register_netdev ( struct net_device *netdev );
  390. extern int netdev_open ( struct net_device *netdev );
  391. extern void netdev_close ( struct net_device *netdev );
  392. extern void unregister_netdev ( struct net_device *netdev );
  393. extern void netdev_irq ( struct net_device *netdev, int enable );
  394. extern struct net_device * find_netdev ( const char *name );
  395. extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
  396. unsigned int location );
  397. extern struct net_device * last_opened_netdev ( void );
  398. extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  399. struct net_protocol *net_protocol, const void *ll_dest );
  400. extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  401. uint16_t net_proto, const void *ll_source );
  402. extern struct settings_operations netdev_settings_operations;
  403. /**
  404. * Complete network transmission
  405. *
  406. * @v netdev Network device
  407. * @v iobuf I/O buffer
  408. *
  409. * The packet must currently be in the network device's TX queue.
  410. */
  411. static inline void netdev_tx_complete ( struct net_device *netdev,
  412. struct io_buffer *iobuf ) {
  413. netdev_tx_complete_err ( netdev, iobuf, 0 );
  414. }
  415. /**
  416. * Complete network transmission
  417. *
  418. * @v netdev Network device
  419. *
  420. * Completes the oldest outstanding packet in the TX queue.
  421. */
  422. static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
  423. netdev_tx_complete_next_err ( netdev, 0 );
  424. }
  425. #endif /* _GPXE_NETDEVICE_H */