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

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