Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

netdevice.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. /** Hardware address
  242. *
  243. * This is an address which is an intrinsic property of the
  244. * hardware, e.g. an address held in EEPROM.
  245. */
  246. uint8_t hw_addr[MAX_LL_ADDR_LEN];
  247. /** Link-layer address
  248. *
  249. * This is the current link-layer address assigned to the
  250. * device. It can be changed at runtime.
  251. */
  252. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  253. /** Link-layer broadcast address */
  254. const uint8_t *ll_broadcast;
  255. /** Current device state
  256. *
  257. * This is the bitwise-OR of zero or more NETDEV_XXX constants.
  258. */
  259. unsigned int state;
  260. /** Link status code
  261. *
  262. * Zero indicates that the link is up; any other value
  263. * indicates the error preventing link-up.
  264. */
  265. int link_rc;
  266. /** Maximum packet length
  267. *
  268. * This length includes any link-layer headers.
  269. */
  270. size_t max_pkt_len;
  271. /** TX packet queue */
  272. struct list_head tx_queue;
  273. /** RX packet queue */
  274. struct list_head rx_queue;
  275. /** TX statistics */
  276. struct net_device_stats tx_stats;
  277. /** RX statistics */
  278. struct net_device_stats rx_stats;
  279. /** Configuration settings applicable to this device */
  280. struct generic_settings settings;
  281. /** Driver private data */
  282. void *priv;
  283. };
  284. /** Network device is open */
  285. #define NETDEV_OPEN 0x0001
  286. /** Link-layer protocol table */
  287. #define LL_PROTOCOLS __table ( struct ll_protocol, "ll_protocols" )
  288. /** Declare a link-layer protocol */
  289. #define __ll_protocol __table_entry ( LL_PROTOCOLS, 01 )
  290. /** Network-layer protocol table */
  291. #define NET_PROTOCOLS __table ( struct net_protocol, "net_protocols" )
  292. /** Declare a network-layer protocol */
  293. #define __net_protocol __table_entry ( NET_PROTOCOLS, 01 )
  294. extern struct list_head net_devices;
  295. extern struct net_device_operations null_netdev_operations;
  296. extern struct settings_operations netdev_settings_operations;
  297. /**
  298. * Initialise a network device
  299. *
  300. * @v netdev Network device
  301. * @v op Network device operations
  302. */
  303. static inline void netdev_init ( struct net_device *netdev,
  304. struct net_device_operations *op ) {
  305. netdev->op = op;
  306. }
  307. /**
  308. * Stop using a network device
  309. *
  310. * @v netdev Network device
  311. *
  312. * Drivers should call this method immediately before the final call
  313. * to netdev_put().
  314. */
  315. static inline void netdev_nullify ( struct net_device *netdev ) {
  316. netdev->op = &null_netdev_operations;
  317. }
  318. /**
  319. * Get printable network device link-layer address
  320. *
  321. * @v netdev Network device
  322. * @ret name Link-layer address
  323. */
  324. static inline const char * netdev_addr ( struct net_device *netdev ) {
  325. return netdev->ll_protocol->ntoa ( netdev->ll_addr );
  326. }
  327. /** Iterate over all network devices */
  328. #define for_each_netdev( netdev ) \
  329. list_for_each_entry ( (netdev), &net_devices, list )
  330. /** There exist some network devices
  331. *
  332. * @ret existence Existence of network devices
  333. */
  334. static inline int have_netdevs ( void ) {
  335. return ( ! list_empty ( &net_devices ) );
  336. }
  337. /**
  338. * Get reference to network device
  339. *
  340. * @v netdev Network device
  341. * @ret netdev Network device
  342. */
  343. static inline __attribute__ (( always_inline )) struct net_device *
  344. netdev_get ( struct net_device *netdev ) {
  345. ref_get ( &netdev->refcnt );
  346. return netdev;
  347. }
  348. /**
  349. * Drop reference to network device
  350. *
  351. * @v netdev Network device
  352. */
  353. static inline __attribute__ (( always_inline )) void
  354. netdev_put ( struct net_device *netdev ) {
  355. ref_put ( &netdev->refcnt );
  356. }
  357. /**
  358. * Get driver private area for this network device
  359. *
  360. * @v netdev Network device
  361. * @ret priv Driver private area for this network device
  362. */
  363. static inline __attribute__ (( always_inline )) void *
  364. netdev_priv ( struct net_device *netdev ) {
  365. return netdev->priv;
  366. }
  367. /**
  368. * Get per-netdevice configuration settings block
  369. *
  370. * @v netdev Network device
  371. * @ret settings Settings block
  372. */
  373. static inline __attribute__ (( always_inline )) struct settings *
  374. netdev_settings ( struct net_device *netdev ) {
  375. return &netdev->settings.settings;
  376. }
  377. /**
  378. * Initialise a per-netdevice configuration settings block
  379. *
  380. * @v generics Generic settings block
  381. * @v refcnt Containing object reference counter, or NULL
  382. * @v name Settings block name
  383. */
  384. static inline __attribute__ (( always_inline )) void
  385. netdev_settings_init ( struct net_device *netdev ) {
  386. generic_settings_init ( &netdev->settings,
  387. &netdev->refcnt, netdev->name );
  388. netdev->settings.settings.op = &netdev_settings_operations;
  389. }
  390. /**
  391. * Mark network device as having link up
  392. *
  393. * @v netdev Network device
  394. */
  395. static inline __attribute__ (( always_inline )) void
  396. netdev_link_up ( struct net_device *netdev ) {
  397. netdev->link_rc = 0;
  398. }
  399. /**
  400. * Mark network device as having link down due to a specific error
  401. *
  402. * @v netdev Network device
  403. * @v rc Link status code
  404. */
  405. static inline __attribute__ (( always_inline )) void
  406. netdev_link_err ( struct net_device *netdev, int rc ) {
  407. netdev->link_rc = rc;
  408. }
  409. /**
  410. * Check link state of network device
  411. *
  412. * @v netdev Network device
  413. * @ret link_up Link is up
  414. */
  415. static inline __attribute__ (( always_inline )) int
  416. netdev_link_ok ( struct net_device *netdev ) {
  417. return ( netdev->link_rc == 0 );
  418. }
  419. extern void netdev_link_down ( struct net_device *netdev );
  420. extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
  421. extern void netdev_tx_complete_err ( struct net_device *netdev,
  422. struct io_buffer *iobuf, int rc );
  423. extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
  424. extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
  425. extern void netdev_rx_err ( struct net_device *netdev,
  426. struct io_buffer *iobuf, int rc );
  427. extern void netdev_poll ( struct net_device *netdev );
  428. extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
  429. extern struct net_device * alloc_netdev ( size_t priv_size );
  430. extern int register_netdev ( struct net_device *netdev );
  431. extern int netdev_open ( struct net_device *netdev );
  432. extern void netdev_close ( struct net_device *netdev );
  433. extern void unregister_netdev ( struct net_device *netdev );
  434. extern void netdev_irq ( struct net_device *netdev, int enable );
  435. extern struct net_device * find_netdev ( const char *name );
  436. extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
  437. unsigned int location );
  438. extern struct net_device * last_opened_netdev ( void );
  439. extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  440. struct net_protocol *net_protocol, const void *ll_dest );
  441. extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  442. uint16_t net_proto, const void *ll_source );
  443. /**
  444. * Complete network transmission
  445. *
  446. * @v netdev Network device
  447. * @v iobuf I/O buffer
  448. *
  449. * The packet must currently be in the network device's TX queue.
  450. */
  451. static inline void netdev_tx_complete ( struct net_device *netdev,
  452. struct io_buffer *iobuf ) {
  453. netdev_tx_complete_err ( netdev, iobuf, 0 );
  454. }
  455. /**
  456. * Complete network transmission
  457. *
  458. * @v netdev Network device
  459. *
  460. * Completes the oldest outstanding packet in the TX queue.
  461. */
  462. static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
  463. netdev_tx_complete_next_err ( netdev, 0 );
  464. }
  465. #endif /* _GPXE_NETDEVICE_H */