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

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