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.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <byteswap.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <ipxe/if_ether.h>
  26. #include <ipxe/iobuf.h>
  27. #include <ipxe/tables.h>
  28. #include <ipxe/process.h>
  29. #include <ipxe/init.h>
  30. #include <ipxe/device.h>
  31. #include <ipxe/errortab.h>
  32. #include <ipxe/netdevice.h>
  33. /** @file
  34. *
  35. * Network device management
  36. *
  37. */
  38. /** List of network devices */
  39. struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
  40. /** List of open network devices, in reverse order of opening */
  41. static struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
  42. /** Default link status code */
  43. #define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
  44. #define EINFO_EUNKNOWN_LINK_STATUS \
  45. __einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )
  46. /** Human-readable message for the default link status */
  47. struct errortab netdev_errors[] __errortab = {
  48. __einfo_errortab ( EINFO_EUNKNOWN_LINK_STATUS ),
  49. };
  50. /**
  51. * Mark network device as having link down
  52. *
  53. * @v netdev Network device
  54. */
  55. void netdev_link_down ( struct net_device *netdev ) {
  56. /* Avoid clobbering a more detailed link status code, if one
  57. * is already set.
  58. */
  59. if ( ( netdev->link_rc == 0 ) ||
  60. ( netdev->link_rc == -EUNKNOWN_LINK_STATUS ) ) {
  61. netdev->link_rc = -ENOTCONN;
  62. }
  63. }
  64. /**
  65. * Record network device statistic
  66. *
  67. * @v stats Network device statistics
  68. * @v rc Status code
  69. */
  70. static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
  71. struct net_device_error *error;
  72. struct net_device_error *least_common_error;
  73. unsigned int i;
  74. /* If this is not an error, just update the good counter */
  75. if ( rc == 0 ) {
  76. stats->good++;
  77. return;
  78. }
  79. /* Update the bad counter */
  80. stats->bad++;
  81. /* Locate the appropriate error record */
  82. least_common_error = &stats->errors[0];
  83. for ( i = 0 ; i < ( sizeof ( stats->errors ) /
  84. sizeof ( stats->errors[0] ) ) ; i++ ) {
  85. error = &stats->errors[i];
  86. /* Update matching record, if found */
  87. if ( error->rc == rc ) {
  88. error->count++;
  89. return;
  90. }
  91. if ( error->count < least_common_error->count )
  92. least_common_error = error;
  93. }
  94. /* Overwrite the least common error record */
  95. least_common_error->rc = rc;
  96. least_common_error->count = 1;
  97. }
  98. /**
  99. * Transmit raw packet via network device
  100. *
  101. * @v netdev Network device
  102. * @v iobuf I/O buffer
  103. * @ret rc Return status code
  104. *
  105. * Transmits the packet via the specified network device. This
  106. * function takes ownership of the I/O buffer.
  107. */
  108. int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  109. int rc;
  110. DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
  111. netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
  112. list_add_tail ( &iobuf->list, &netdev->tx_queue );
  113. if ( ! netdev_is_open ( netdev ) ) {
  114. rc = -ENETUNREACH;
  115. goto err;
  116. }
  117. if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
  118. goto err;
  119. return 0;
  120. err:
  121. netdev_tx_complete_err ( netdev, iobuf, rc );
  122. return rc;
  123. }
  124. /**
  125. * Complete network transmission
  126. *
  127. * @v netdev Network device
  128. * @v iobuf I/O buffer
  129. * @v rc Packet status code
  130. *
  131. * The packet must currently be in the network device's TX queue.
  132. */
  133. void netdev_tx_complete_err ( struct net_device *netdev,
  134. struct io_buffer *iobuf, int rc ) {
  135. /* Update statistics counter */
  136. netdev_record_stat ( &netdev->tx_stats, rc );
  137. if ( rc == 0 ) {
  138. DBGC ( netdev, "NETDEV %p transmission %p complete\n",
  139. netdev, iobuf );
  140. } else {
  141. DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
  142. netdev, iobuf, strerror ( rc ) );
  143. }
  144. /* Catch data corruption as early as possible */
  145. assert ( iobuf->list.next != NULL );
  146. assert ( iobuf->list.prev != NULL );
  147. /* Dequeue and free I/O buffer */
  148. list_del ( &iobuf->list );
  149. free_iob ( iobuf );
  150. }
  151. /**
  152. * Complete network transmission
  153. *
  154. * @v netdev Network device
  155. * @v rc Packet status code
  156. *
  157. * Completes the oldest outstanding packet in the TX queue.
  158. */
  159. void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
  160. struct io_buffer *iobuf;
  161. list_for_each_entry ( iobuf, &netdev->tx_queue, list ) {
  162. netdev_tx_complete_err ( netdev, iobuf, rc );
  163. return;
  164. }
  165. }
  166. /**
  167. * Flush device's transmit queue
  168. *
  169. * @v netdev Network device
  170. */
  171. static void netdev_tx_flush ( struct net_device *netdev ) {
  172. /* Discard any packets in the TX queue */
  173. while ( ! list_empty ( &netdev->tx_queue ) ) {
  174. netdev_tx_complete_next_err ( netdev, -ECANCELED );
  175. }
  176. }
  177. /**
  178. * Add packet to receive queue
  179. *
  180. * @v netdev Network device
  181. * @v iobuf I/O buffer, or NULL
  182. *
  183. * The packet is added to the network device's RX queue. This
  184. * function takes ownership of the I/O buffer.
  185. */
  186. void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  187. DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
  188. netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
  189. /* Enqueue packet */
  190. list_add_tail ( &iobuf->list, &netdev->rx_queue );
  191. /* Update statistics counter */
  192. netdev_record_stat ( &netdev->rx_stats, 0 );
  193. }
  194. /**
  195. * Discard received packet
  196. *
  197. * @v netdev Network device
  198. * @v iobuf I/O buffer, or NULL
  199. * @v rc Packet status code
  200. *
  201. * The packet is discarded and an RX error is recorded. This function
  202. * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
  203. * example, the net device wishes to report an error due to being
  204. * unable to allocate an I/O buffer.
  205. */
  206. void netdev_rx_err ( struct net_device *netdev,
  207. struct io_buffer *iobuf, int rc ) {
  208. DBGC ( netdev, "NETDEV %p failed to receive %p: %s\n",
  209. netdev, iobuf, strerror ( rc ) );
  210. /* Discard packet */
  211. free_iob ( iobuf );
  212. /* Update statistics counter */
  213. netdev_record_stat ( &netdev->rx_stats, rc );
  214. }
  215. /**
  216. * Poll for completed and received packets on network device
  217. *
  218. * @v netdev Network device
  219. *
  220. * Polls the network device for completed transmissions and received
  221. * packets. Any received packets will be added to the RX packet queue
  222. * via netdev_rx().
  223. */
  224. void netdev_poll ( struct net_device *netdev ) {
  225. if ( netdev_is_open ( netdev ) )
  226. netdev->op->poll ( netdev );
  227. }
  228. /**
  229. * Remove packet from device's receive queue
  230. *
  231. * @v netdev Network device
  232. * @ret iobuf I/O buffer, or NULL
  233. *
  234. * Removes the first packet from the device's RX queue and returns it.
  235. * Ownership of the packet is transferred to the caller.
  236. */
  237. struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
  238. struct io_buffer *iobuf;
  239. list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
  240. list_del ( &iobuf->list );
  241. return iobuf;
  242. }
  243. return NULL;
  244. }
  245. /**
  246. * Flush device's receive queue
  247. *
  248. * @v netdev Network device
  249. */
  250. static void netdev_rx_flush ( struct net_device *netdev ) {
  251. struct io_buffer *iobuf;
  252. /* Discard any packets in the RX queue */
  253. while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  254. netdev_rx_err ( netdev, iobuf, -ECANCELED );
  255. }
  256. }
  257. /**
  258. * Free network device
  259. *
  260. * @v refcnt Network device reference counter
  261. */
  262. static void free_netdev ( struct refcnt *refcnt ) {
  263. struct net_device *netdev =
  264. container_of ( refcnt, struct net_device, refcnt );
  265. netdev_tx_flush ( netdev );
  266. netdev_rx_flush ( netdev );
  267. clear_settings ( netdev_settings ( netdev ) );
  268. free ( netdev );
  269. }
  270. /**
  271. * Allocate network device
  272. *
  273. * @v priv_size Size of private data area (net_device::priv)
  274. * @ret netdev Network device, or NULL
  275. *
  276. * Allocates space for a network device and its private data area.
  277. */
  278. struct net_device * alloc_netdev ( size_t priv_size ) {
  279. struct net_device *netdev;
  280. size_t total_len;
  281. total_len = ( sizeof ( *netdev ) + priv_size );
  282. netdev = zalloc ( total_len );
  283. if ( netdev ) {
  284. netdev->refcnt.free = free_netdev;
  285. netdev->link_rc = -EUNKNOWN_LINK_STATUS;
  286. INIT_LIST_HEAD ( &netdev->tx_queue );
  287. INIT_LIST_HEAD ( &netdev->rx_queue );
  288. netdev_settings_init ( netdev );
  289. netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
  290. }
  291. return netdev;
  292. }
  293. /**
  294. * Register network device
  295. *
  296. * @v netdev Network device
  297. * @ret rc Return status code
  298. *
  299. * Gives the network device a name and adds it to the list of network
  300. * devices.
  301. */
  302. int register_netdev ( struct net_device *netdev ) {
  303. static unsigned int ifindex = 0;
  304. int rc;
  305. /* Create device name */
  306. snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
  307. ifindex++ );
  308. /* Set initial link-layer address */
  309. netdev->ll_protocol->init_addr ( netdev->hw_addr, netdev->ll_addr );
  310. /* Register per-netdev configuration settings */
  311. if ( ( rc = register_settings ( netdev_settings ( netdev ),
  312. NULL ) ) != 0 ) {
  313. DBGC ( netdev, "NETDEV %p could not register settings: %s\n",
  314. netdev, strerror ( rc ) );
  315. return rc;
  316. }
  317. /* Add to device list */
  318. netdev_get ( netdev );
  319. list_add_tail ( &netdev->list, &net_devices );
  320. DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
  321. netdev, netdev->name, netdev->dev->name,
  322. netdev_addr ( netdev ) );
  323. return 0;
  324. }
  325. /**
  326. * Open network device
  327. *
  328. * @v netdev Network device
  329. * @ret rc Return status code
  330. */
  331. int netdev_open ( struct net_device *netdev ) {
  332. int rc;
  333. /* Do nothing if device is already open */
  334. if ( netdev->state & NETDEV_OPEN )
  335. return 0;
  336. DBGC ( netdev, "NETDEV %p opening\n", netdev );
  337. /* Open the device */
  338. if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
  339. return rc;
  340. /* Mark as opened */
  341. netdev->state |= NETDEV_OPEN;
  342. /* Add to head of open devices list */
  343. list_add ( &netdev->open_list, &open_net_devices );
  344. return 0;
  345. }
  346. /**
  347. * Close network device
  348. *
  349. * @v netdev Network device
  350. */
  351. void netdev_close ( struct net_device *netdev ) {
  352. /* Do nothing if device is already closed */
  353. if ( ! ( netdev->state & NETDEV_OPEN ) )
  354. return;
  355. DBGC ( netdev, "NETDEV %p closing\n", netdev );
  356. /* Close the device */
  357. netdev->op->close ( netdev );
  358. /* Flush TX and RX queues */
  359. netdev_tx_flush ( netdev );
  360. netdev_rx_flush ( netdev );
  361. /* Mark as closed */
  362. netdev->state &= ~NETDEV_OPEN;
  363. /* Remove from open devices list */
  364. list_del ( &netdev->open_list );
  365. }
  366. /**
  367. * Unregister network device
  368. *
  369. * @v netdev Network device
  370. *
  371. * Removes the network device from the list of network devices.
  372. */
  373. void unregister_netdev ( struct net_device *netdev ) {
  374. /* Ensure device is closed */
  375. netdev_close ( netdev );
  376. /* Unregister per-netdev configuration settings */
  377. unregister_settings ( netdev_settings ( netdev ) );
  378. /* Remove from device list */
  379. list_del ( &netdev->list );
  380. netdev_put ( netdev );
  381. DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
  382. }
  383. /** Enable or disable interrupts
  384. *
  385. * @v netdev Network device
  386. * @v enable Interrupts should be enabled
  387. */
  388. void netdev_irq ( struct net_device *netdev, int enable ) {
  389. /* Enable or disable device interrupts */
  390. netdev->op->irq ( netdev, enable );
  391. /* Record interrupt enabled state */
  392. netdev->state &= ~NETDEV_IRQ_ENABLED;
  393. if ( enable )
  394. netdev->state |= NETDEV_IRQ_ENABLED;
  395. }
  396. /**
  397. * Get network device by name
  398. *
  399. * @v name Network device name
  400. * @ret netdev Network device, or NULL
  401. */
  402. struct net_device * find_netdev ( const char *name ) {
  403. struct net_device *netdev;
  404. list_for_each_entry ( netdev, &net_devices, list ) {
  405. if ( strcmp ( netdev->name, name ) == 0 )
  406. return netdev;
  407. }
  408. return NULL;
  409. }
  410. /**
  411. * Get network device by PCI bus:dev.fn address
  412. *
  413. * @v bus_type Bus type
  414. * @v location Bus location
  415. * @ret netdev Network device, or NULL
  416. */
  417. struct net_device * find_netdev_by_location ( unsigned int bus_type,
  418. unsigned int location ) {
  419. struct net_device *netdev;
  420. list_for_each_entry ( netdev, &net_devices, list ) {
  421. if ( ( netdev->dev->desc.bus_type == bus_type ) &&
  422. ( netdev->dev->desc.location == location ) )
  423. return netdev;
  424. }
  425. return NULL;
  426. }
  427. /**
  428. * Get most recently opened network device
  429. *
  430. * @ret netdev Most recently opened network device, or NULL
  431. */
  432. struct net_device * last_opened_netdev ( void ) {
  433. struct net_device *netdev;
  434. list_for_each_entry ( netdev, &open_net_devices, open_list ) {
  435. assert ( netdev_is_open ( netdev ) );
  436. return netdev;
  437. }
  438. return NULL;
  439. }
  440. /**
  441. * Transmit network-layer packet
  442. *
  443. * @v iobuf I/O buffer
  444. * @v netdev Network device
  445. * @v net_protocol Network-layer protocol
  446. * @v ll_dest Destination link-layer address
  447. * @ret rc Return status code
  448. *
  449. * Prepends link-layer headers to the I/O buffer and transmits the
  450. * packet via the specified network device. This function takes
  451. * ownership of the I/O buffer.
  452. */
  453. int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  454. struct net_protocol *net_protocol, const void *ll_dest ) {
  455. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  456. int rc;
  457. /* Force a poll on the netdevice to (potentially) clear any
  458. * backed-up TX completions. This is needed on some network
  459. * devices to avoid excessive losses due to small TX ring
  460. * sizes.
  461. */
  462. netdev_poll ( netdev );
  463. /* Add link-layer header */
  464. if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, netdev->ll_addr,
  465. net_protocol->net_proto ) ) != 0 ) {
  466. free_iob ( iobuf );
  467. return rc;
  468. }
  469. /* Transmit packet */
  470. return netdev_tx ( netdev, iobuf );
  471. }
  472. /**
  473. * Process received network-layer packet
  474. *
  475. * @v iobuf I/O buffer
  476. * @v netdev Network device
  477. * @v net_proto Network-layer protocol, in network-byte order
  478. * @v ll_source Source link-layer address
  479. * @ret rc Return status code
  480. */
  481. int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  482. uint16_t net_proto, const void *ll_source ) {
  483. struct net_protocol *net_protocol;
  484. /* Hand off to network-layer protocol, if any */
  485. for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
  486. if ( net_protocol->net_proto == net_proto )
  487. return net_protocol->rx ( iobuf, netdev, ll_source );
  488. }
  489. DBGC ( netdev, "NETDEV %p unknown network protocol %04x\n",
  490. netdev, ntohs ( net_proto ) );
  491. free_iob ( iobuf );
  492. return 0;
  493. }
  494. /**
  495. * Single-step the network stack
  496. *
  497. * @v process Network stack process
  498. *
  499. * This polls all interfaces for received packets, and processes
  500. * packets from the RX queue.
  501. */
  502. static void net_step ( struct process *process __unused ) {
  503. struct net_device *netdev;
  504. struct io_buffer *iobuf;
  505. struct ll_protocol *ll_protocol;
  506. const void *ll_dest;
  507. const void *ll_source;
  508. uint16_t net_proto;
  509. int rc;
  510. /* Poll and process each network device */
  511. list_for_each_entry ( netdev, &net_devices, list ) {
  512. /* Poll for new packets */
  513. netdev_poll ( netdev );
  514. /* Process at most one received packet. Give priority
  515. * to getting packets out of the NIC over processing
  516. * the received packets, because we advertise a window
  517. * that assumes that we can receive packets from the
  518. * NIC faster than they arrive.
  519. */
  520. if ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  521. DBGC ( netdev, "NETDEV %p processing %p (%p+%zx)\n",
  522. netdev, iobuf, iobuf->data,
  523. iob_len ( iobuf ) );
  524. /* Remove link-layer header */
  525. ll_protocol = netdev->ll_protocol;
  526. if ( ( rc = ll_protocol->pull ( netdev, iobuf,
  527. &ll_dest, &ll_source,
  528. &net_proto ) ) != 0 ) {
  529. free_iob ( iobuf );
  530. continue;
  531. }
  532. net_rx ( iobuf, netdev, net_proto, ll_source );
  533. }
  534. }
  535. }
  536. /** Networking stack process */
  537. struct process net_process __permanent_process = {
  538. .list = LIST_HEAD_INIT ( net_process.list ),
  539. .step = net_step,
  540. };