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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. #ifndef _IPXE_NETDEVICE_H
  2. #define _IPXE_NETDEVICE_H
  3. /** @file
  4. *
  5. * Network device management
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER );
  9. #include <stdint.h>
  10. #include <ipxe/list.h>
  11. #include <ipxe/tables.h>
  12. #include <ipxe/refcnt.h>
  13. #include <ipxe/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 hardware address
  20. *
  21. * The longest currently-supported link-layer address is for IPoIB.
  22. */
  23. #define MAX_HW_ADDR_LEN 8
  24. /** Maximum length of a link-layer address
  25. *
  26. * The longest currently-supported link-layer address is for IPoIB.
  27. */
  28. #define MAX_LL_ADDR_LEN 20
  29. /** Maximum length of a link-layer header
  30. *
  31. * The longest currently-supported link-layer header is for 802.11: a
  32. * 24-byte frame header plus an 8-byte 802.3 LLC/SNAP header, plus a
  33. * possible 4-byte VLAN header. (The IPoIB link-layer pseudo-header
  34. * doesn't actually include link-layer addresses; see ipoib.c for
  35. * details.)
  36. */
  37. #define MAX_LL_HEADER_LEN 36
  38. /** Maximum length of a network-layer address */
  39. #define MAX_NET_ADDR_LEN 4
  40. /** Maximum length of a network-layer header
  41. *
  42. * The longest currently-supported network-layer header is for IPv6 at
  43. * 40 bytes.
  44. */
  45. #define MAX_NET_HEADER_LEN 40
  46. /** Maximum combined length of a link-layer and network-layer header */
  47. #define MAX_LL_NET_HEADER_LEN ( MAX_LL_HEADER_LEN + MAX_NET_HEADER_LEN )
  48. /**
  49. * A network-layer protocol
  50. *
  51. */
  52. struct net_protocol {
  53. /** Protocol name */
  54. const char *name;
  55. /**
  56. * Process received packet
  57. *
  58. * @v iobuf I/O buffer
  59. * @v netdev Network device
  60. * @v ll_dest Link-layer destination address
  61. * @v ll_source Link-layer source address
  62. * @v flags Packet flags
  63. * @ret rc Return status code
  64. *
  65. * This method takes ownership of the I/O buffer.
  66. */
  67. int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev,
  68. const void *ll_dest, const void *ll_source,
  69. unsigned int flags );
  70. /**
  71. * Transcribe network-layer address
  72. *
  73. * @v net_addr Network-layer address
  74. * @ret string Human-readable transcription of address
  75. *
  76. * This method should convert the network-layer address into a
  77. * human-readable format (e.g. dotted quad notation for IPv4).
  78. *
  79. * The buffer used to hold the transcription is statically
  80. * allocated.
  81. */
  82. const char * ( *ntoa ) ( const void * net_addr );
  83. /** Network-layer protocol
  84. *
  85. * This is an ETH_P_XXX constant, in network-byte order
  86. */
  87. uint16_t net_proto;
  88. /** Network-layer address length */
  89. uint8_t net_addr_len;
  90. };
  91. /** Packet is a multicast (including broadcast) packet */
  92. #define LL_MULTICAST 0x0001
  93. /** Packet is a broadcast packet */
  94. #define LL_BROADCAST 0x0002
  95. /**
  96. * A link-layer protocol
  97. *
  98. */
  99. struct ll_protocol {
  100. /** Protocol name */
  101. const char *name;
  102. /**
  103. * Add link-layer header
  104. *
  105. * @v netdev Network device
  106. * @v iobuf I/O buffer
  107. * @v ll_dest Link-layer destination address
  108. * @v ll_source Source link-layer address
  109. * @v net_proto Network-layer protocol, in network-byte order
  110. * @ret rc Return status code
  111. */
  112. int ( * push ) ( struct net_device *netdev, struct io_buffer *iobuf,
  113. const void *ll_dest, const void *ll_source,
  114. uint16_t net_proto );
  115. /**
  116. * Remove link-layer header
  117. *
  118. * @v netdev Network device
  119. * @v iobuf I/O buffer
  120. * @ret ll_dest Link-layer destination address
  121. * @ret ll_source Source link-layer address
  122. * @ret net_proto Network-layer protocol, in network-byte order
  123. * @ret flags Packet flags
  124. * @ret rc Return status code
  125. */
  126. int ( * pull ) ( struct net_device *netdev, struct io_buffer *iobuf,
  127. const void **ll_dest, const void **ll_source,
  128. uint16_t *net_proto, unsigned int *flags );
  129. /**
  130. * Initialise link-layer address
  131. *
  132. * @v hw_addr Hardware address
  133. * @v ll_addr Link-layer address to fill in
  134. */
  135. void ( * init_addr ) ( const void *hw_addr, void *ll_addr );
  136. /**
  137. * Transcribe link-layer address
  138. *
  139. * @v ll_addr Link-layer address
  140. * @ret string Human-readable transcription of address
  141. *
  142. * This method should convert the link-layer address into a
  143. * human-readable format.
  144. *
  145. * The buffer used to hold the transcription is statically
  146. * allocated.
  147. */
  148. const char * ( * ntoa ) ( const void *ll_addr );
  149. /**
  150. * Hash multicast address
  151. *
  152. * @v af Address family
  153. * @v net_addr Network-layer address
  154. * @v ll_addr Link-layer address to fill in
  155. * @ret rc Return status code
  156. */
  157. int ( * mc_hash ) ( unsigned int af, const void *net_addr,
  158. void *ll_addr );
  159. /**
  160. * Generate Ethernet-compatible compressed link-layer address
  161. *
  162. * @v ll_addr Link-layer address
  163. * @v eth_addr Ethernet-compatible address to fill in
  164. */
  165. int ( * eth_addr ) ( const void *ll_addr, void *eth_addr );
  166. /** Link-layer protocol
  167. *
  168. * This is an ARPHRD_XXX constant, in network byte order.
  169. */
  170. uint16_t ll_proto;
  171. /** Hardware address length */
  172. uint8_t hw_addr_len;
  173. /** Link-layer address length */
  174. uint8_t ll_addr_len;
  175. /** Link-layer header length */
  176. uint8_t ll_header_len;
  177. };
  178. /** Network device operations */
  179. struct net_device_operations {
  180. /** Open network device
  181. *
  182. * @v netdev Network device
  183. * @ret rc Return status code
  184. *
  185. * This method should allocate RX I/O buffers and enable
  186. * the hardware to start transmitting and receiving packets.
  187. */
  188. int ( * open ) ( struct net_device *netdev );
  189. /** Close network device
  190. *
  191. * @v netdev Network device
  192. *
  193. * This method should stop the flow of packets, and free up
  194. * any packets that are currently in the device's TX queue.
  195. */
  196. void ( * close ) ( struct net_device *netdev );
  197. /** Transmit packet
  198. *
  199. * @v netdev Network device
  200. * @v iobuf I/O buffer
  201. * @ret rc Return status code
  202. *
  203. * This method should cause the hardware to initiate
  204. * transmission of the I/O buffer.
  205. *
  206. * If this method returns success, the I/O buffer remains
  207. * owned by the net device's TX queue, and the net device must
  208. * eventually call netdev_tx_complete() to free the buffer.
  209. * If this method returns failure, the I/O buffer is
  210. * immediately released; the failure is interpreted as
  211. * "failure to enqueue buffer".
  212. *
  213. * This method is guaranteed to be called only when the device
  214. * is open.
  215. */
  216. int ( * transmit ) ( struct net_device *netdev,
  217. struct io_buffer *iobuf );
  218. /** Poll for completed and received packets
  219. *
  220. * @v netdev Network device
  221. *
  222. * This method should cause the hardware to check for
  223. * completed transmissions and received packets. Any received
  224. * packets should be delivered via netdev_rx().
  225. *
  226. * This method is guaranteed to be called only when the device
  227. * is open.
  228. */
  229. void ( * poll ) ( struct net_device *netdev );
  230. /** Enable or disable interrupts
  231. *
  232. * @v netdev Network device
  233. * @v enable Interrupts should be enabled
  234. *
  235. * This method may be NULL to indicate that interrupts are not
  236. * supported.
  237. */
  238. void ( * irq ) ( struct net_device *netdev, int enable );
  239. };
  240. /** Network device error */
  241. struct net_device_error {
  242. /** Error status code */
  243. int rc;
  244. /** Error count */
  245. unsigned int count;
  246. };
  247. /** Maximum number of unique errors that we will keep track of */
  248. #define NETDEV_MAX_UNIQUE_ERRORS 4
  249. /** Network device statistics */
  250. struct net_device_stats {
  251. /** Count of successful completions */
  252. unsigned int good;
  253. /** Count of error completions */
  254. unsigned int bad;
  255. /** Error breakdowns */
  256. struct net_device_error errors[NETDEV_MAX_UNIQUE_ERRORS];
  257. };
  258. /**
  259. * A network device
  260. *
  261. * This structure represents a piece of networking hardware. It has
  262. * properties such as a link-layer address and methods for
  263. * transmitting and receiving raw packets.
  264. *
  265. * Note that this structure must represent a generic network device,
  266. * not just an Ethernet device.
  267. */
  268. struct net_device {
  269. /** Reference counter */
  270. struct refcnt refcnt;
  271. /** List of network devices */
  272. struct list_head list;
  273. /** List of open network devices */
  274. struct list_head open_list;
  275. /** Name of this network device */
  276. char name[12];
  277. /** Underlying hardware device */
  278. struct device *dev;
  279. /** Network device operations */
  280. struct net_device_operations *op;
  281. /** Link-layer protocol */
  282. struct ll_protocol *ll_protocol;
  283. /** Hardware address
  284. *
  285. * This is an address which is an intrinsic property of the
  286. * hardware, e.g. an address held in EEPROM.
  287. *
  288. * Note that the hardware address may not be the same length
  289. * as the link-layer address.
  290. */
  291. uint8_t hw_addr[MAX_HW_ADDR_LEN];
  292. /** Link-layer address
  293. *
  294. * This is the current link-layer address assigned to the
  295. * device. It can be changed at runtime.
  296. */
  297. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  298. /** Link-layer broadcast address */
  299. const uint8_t *ll_broadcast;
  300. /** Current device state
  301. *
  302. * This is the bitwise-OR of zero or more NETDEV_XXX constants.
  303. */
  304. unsigned int state;
  305. /** Link status code
  306. *
  307. * Zero indicates that the link is up; any other value
  308. * indicates the error preventing link-up.
  309. */
  310. int link_rc;
  311. /** Maximum packet length
  312. *
  313. * This length includes any link-layer headers.
  314. */
  315. size_t max_pkt_len;
  316. /** TX packet queue */
  317. struct list_head tx_queue;
  318. /** RX packet queue */
  319. struct list_head rx_queue;
  320. /** TX statistics */
  321. struct net_device_stats tx_stats;
  322. /** RX statistics */
  323. struct net_device_stats rx_stats;
  324. /** Configuration settings applicable to this device */
  325. struct generic_settings settings;
  326. /** Driver private data */
  327. void *priv;
  328. };
  329. /** Network device is open */
  330. #define NETDEV_OPEN 0x0001
  331. /** Network device interrupts are enabled */
  332. #define NETDEV_IRQ_ENABLED 0x0002
  333. /** Network device receive queue processing is frozen */
  334. #define NETDEV_RX_FROZEN 0x0004
  335. /** Link-layer protocol table */
  336. #define LL_PROTOCOLS __table ( struct ll_protocol, "ll_protocols" )
  337. /** Declare a link-layer protocol */
  338. #define __ll_protocol __table_entry ( LL_PROTOCOLS, 01 )
  339. /** Network-layer protocol table */
  340. #define NET_PROTOCOLS __table ( struct net_protocol, "net_protocols" )
  341. /** Declare a network-layer protocol */
  342. #define __net_protocol __table_entry ( NET_PROTOCOLS, 01 )
  343. /** A network upper-layer driver */
  344. struct net_driver {
  345. /** Name */
  346. const char *name;
  347. /** Probe device
  348. *
  349. * @v netdev Network device
  350. * @ret rc Return status code
  351. */
  352. int ( * probe ) ( struct net_device *netdev );
  353. /** Notify of device or link state change
  354. *
  355. * @v netdev Network device
  356. */
  357. void ( * notify ) ( struct net_device *netdev );
  358. /** Remove device
  359. *
  360. * @v netdev Network device
  361. */
  362. void ( * remove ) ( struct net_device *netdev );
  363. };
  364. /** Network driver table */
  365. #define NET_DRIVERS __table ( struct net_driver, "net_drivers" )
  366. /** Declare a network driver */
  367. #define __net_driver __table_entry ( NET_DRIVERS, 01 )
  368. /** Network device setting tag magic
  369. *
  370. * All DHCP option settings are deemed to be valid as network device
  371. * settings. There are also some extra non-DHCP settings (such as
  372. * "mac"), which are marked as being valid network device settings by
  373. * using a magic tag value.
  374. */
  375. #define NETDEV_SETTING_TAG_MAGIC 0xeb
  376. /**
  377. * Construct network device setting tag
  378. *
  379. * @v id Unique identifier
  380. * @ret tag Setting tag
  381. */
  382. #define NETDEV_SETTING_TAG( id ) ( ( NETDEV_SETTING_TAG_MAGIC << 24 ) | (id) )
  383. /**
  384. * Check if tag is a network device setting tag
  385. *
  386. * @v tag Setting tag
  387. * @ret is_ours Tag is a network device setting tag
  388. */
  389. #define IS_NETDEV_SETTING_TAG( tag ) \
  390. ( ( (tag) >> 24 ) == NETDEV_SETTING_TAG_MAGIC )
  391. /** MAC address setting tag */
  392. #define NETDEV_SETTING_TAG_MAC NETDEV_SETTING_TAG ( 0x01 )
  393. /** Bus ID setting tag */
  394. #define NETDEV_SETTING_TAG_BUS_ID NETDEV_SETTING_TAG ( 0x02 )
  395. extern struct list_head net_devices;
  396. extern struct net_device_operations null_netdev_operations;
  397. extern struct settings_operations netdev_settings_operations;
  398. /**
  399. * Initialise a network device
  400. *
  401. * @v netdev Network device
  402. * @v op Network device operations
  403. */
  404. static inline void netdev_init ( struct net_device *netdev,
  405. struct net_device_operations *op ) {
  406. netdev->op = op;
  407. }
  408. /**
  409. * Stop using a network device
  410. *
  411. * @v netdev Network device
  412. *
  413. * Drivers should call this method immediately before the final call
  414. * to netdev_put().
  415. */
  416. static inline void netdev_nullify ( struct net_device *netdev ) {
  417. netdev->op = &null_netdev_operations;
  418. }
  419. /**
  420. * Get printable network device link-layer address
  421. *
  422. * @v netdev Network device
  423. * @ret name Link-layer address
  424. */
  425. static inline const char * netdev_addr ( struct net_device *netdev ) {
  426. return netdev->ll_protocol->ntoa ( netdev->ll_addr );
  427. }
  428. /** Iterate over all network devices */
  429. #define for_each_netdev( netdev ) \
  430. list_for_each_entry ( (netdev), &net_devices, list )
  431. /** There exist some network devices
  432. *
  433. * @ret existence Existence of network devices
  434. */
  435. static inline int have_netdevs ( void ) {
  436. return ( ! list_empty ( &net_devices ) );
  437. }
  438. /**
  439. * Get reference to network device
  440. *
  441. * @v netdev Network device
  442. * @ret netdev Network device
  443. */
  444. static inline __attribute__ (( always_inline )) struct net_device *
  445. netdev_get ( struct net_device *netdev ) {
  446. ref_get ( &netdev->refcnt );
  447. return netdev;
  448. }
  449. /**
  450. * Drop reference to network device
  451. *
  452. * @v netdev Network device
  453. */
  454. static inline __attribute__ (( always_inline )) void
  455. netdev_put ( struct net_device *netdev ) {
  456. ref_put ( &netdev->refcnt );
  457. }
  458. /**
  459. * Get driver private area for this network device
  460. *
  461. * @v netdev Network device
  462. * @ret priv Driver private area for this network device
  463. */
  464. static inline __attribute__ (( always_inline )) void *
  465. netdev_priv ( struct net_device *netdev ) {
  466. return netdev->priv;
  467. }
  468. /**
  469. * Get per-netdevice configuration settings block
  470. *
  471. * @v netdev Network device
  472. * @ret settings Settings block
  473. */
  474. static inline __attribute__ (( always_inline )) struct settings *
  475. netdev_settings ( struct net_device *netdev ) {
  476. return &netdev->settings.settings;
  477. }
  478. /**
  479. * Initialise a per-netdevice configuration settings block
  480. *
  481. * @v generics Generic settings block
  482. * @v refcnt Containing object reference counter, or NULL
  483. * @v name Settings block name
  484. */
  485. static inline __attribute__ (( always_inline )) void
  486. netdev_settings_init ( struct net_device *netdev ) {
  487. generic_settings_init ( &netdev->settings, &netdev->refcnt );
  488. netdev->settings.settings.op = &netdev_settings_operations;
  489. }
  490. /**
  491. * Check link state of network device
  492. *
  493. * @v netdev Network device
  494. * @ret link_up Link is up
  495. */
  496. static inline __attribute__ (( always_inline )) int
  497. netdev_link_ok ( struct net_device *netdev ) {
  498. return ( netdev->link_rc == 0 );
  499. }
  500. /**
  501. * Check whether or not network device is open
  502. *
  503. * @v netdev Network device
  504. * @ret is_open Network device is open
  505. */
  506. static inline __attribute__ (( always_inline )) int
  507. netdev_is_open ( struct net_device *netdev ) {
  508. return ( netdev->state & NETDEV_OPEN );
  509. }
  510. /**
  511. * Check whether or not network device supports interrupts
  512. *
  513. * @v netdev Network device
  514. * @ret irq_supported Network device supports interrupts
  515. */
  516. static inline __attribute__ (( always_inline )) int
  517. netdev_irq_supported ( struct net_device *netdev ) {
  518. return ( netdev->op->irq != NULL );
  519. }
  520. /**
  521. * Check whether or not network device interrupts are currently enabled
  522. *
  523. * @v netdev Network device
  524. * @ret irq_enabled Network device interrupts are enabled
  525. */
  526. static inline __attribute__ (( always_inline )) int
  527. netdev_irq_enabled ( struct net_device *netdev ) {
  528. return ( netdev->state & NETDEV_IRQ_ENABLED );
  529. }
  530. /**
  531. * Check whether or not network device receive queue processing is frozen
  532. *
  533. * @v netdev Network device
  534. * @ret rx_frozen Network device receive queue processing is frozen
  535. */
  536. static inline __attribute__ (( always_inline )) int
  537. netdev_rx_frozen ( struct net_device *netdev ) {
  538. return ( netdev->state & NETDEV_RX_FROZEN );
  539. }
  540. extern void netdev_link_err ( struct net_device *netdev, int rc );
  541. extern void netdev_link_down ( struct net_device *netdev );
  542. extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
  543. extern void netdev_tx_err ( struct net_device *netdev,
  544. struct io_buffer *iobuf, int rc );
  545. extern void netdev_tx_complete_err ( struct net_device *netdev,
  546. struct io_buffer *iobuf, int rc );
  547. extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
  548. extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
  549. extern void netdev_rx_err ( struct net_device *netdev,
  550. struct io_buffer *iobuf, int rc );
  551. extern void netdev_poll ( struct net_device *netdev );
  552. extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
  553. extern struct net_device * alloc_netdev ( size_t priv_size );
  554. extern int register_netdev ( struct net_device *netdev );
  555. extern int netdev_open ( struct net_device *netdev );
  556. extern void netdev_close ( struct net_device *netdev );
  557. extern void unregister_netdev ( struct net_device *netdev );
  558. extern void netdev_irq ( struct net_device *netdev, int enable );
  559. extern struct net_device * find_netdev ( const char *name );
  560. extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
  561. unsigned int location );
  562. extern struct net_device * last_opened_netdev ( void );
  563. extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  564. struct net_protocol *net_protocol, const void *ll_dest,
  565. const void *ll_source );
  566. extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  567. uint16_t net_proto, const void *ll_dest,
  568. const void *ll_source, unsigned int flags );
  569. extern void net_poll ( void );
  570. /**
  571. * Complete network transmission
  572. *
  573. * @v netdev Network device
  574. * @v iobuf I/O buffer
  575. *
  576. * The packet must currently be in the network device's TX queue.
  577. */
  578. static inline void netdev_tx_complete ( struct net_device *netdev,
  579. struct io_buffer *iobuf ) {
  580. netdev_tx_complete_err ( netdev, iobuf, 0 );
  581. }
  582. /**
  583. * Complete network transmission
  584. *
  585. * @v netdev Network device
  586. *
  587. * Completes the oldest outstanding packet in the TX queue.
  588. */
  589. static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
  590. netdev_tx_complete_next_err ( netdev, 0 );
  591. }
  592. /**
  593. * Mark network device as having link up
  594. *
  595. * @v netdev Network device
  596. */
  597. static inline __attribute__ (( always_inline )) void
  598. netdev_link_up ( struct net_device *netdev ) {
  599. netdev_link_err ( netdev, 0 );
  600. }
  601. /**
  602. * Freeze network device receive queue processing
  603. *
  604. * @v netdev Network device
  605. */
  606. static inline __attribute__ (( always_inline )) void
  607. netdev_rx_freeze ( struct net_device *netdev ) {
  608. netdev->state |= NETDEV_RX_FROZEN;
  609. }
  610. /**
  611. * Unfreeze network device receive queue processing
  612. *
  613. * @v netdev Network device
  614. */
  615. static inline __attribute__ (( always_inline )) void
  616. netdev_rx_unfreeze ( struct net_device *netdev ) {
  617. netdev->state &= ~NETDEV_RX_FROZEN;
  618. }
  619. #endif /* _IPXE_NETDEVICE_H */