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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <byteswap.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <config/general.h>
  27. #include <ipxe/if_ether.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/tables.h>
  30. #include <ipxe/process.h>
  31. #include <ipxe/init.h>
  32. #include <ipxe/malloc.h>
  33. #include <ipxe/device.h>
  34. #include <ipxe/errortab.h>
  35. #include <ipxe/vlan.h>
  36. #include <ipxe/netdevice.h>
  37. /** @file
  38. *
  39. * Network device management
  40. *
  41. */
  42. /** List of network devices */
  43. struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
  44. /** List of open network devices, in reverse order of opening */
  45. static struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
  46. /** Default unknown link status code */
  47. #define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
  48. #define EINFO_EUNKNOWN_LINK_STATUS \
  49. __einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )
  50. /** Default link-down status code */
  51. #define ENOTCONN_LINK_DOWN __einfo_error ( EINFO_ENOTCONN_LINK_DOWN )
  52. #define EINFO_ENOTCONN_LINK_DOWN \
  53. __einfo_uniqify ( EINFO_ENOTCONN, 0x01, "Down" )
  54. /** Human-readable message for the default link statuses */
  55. struct errortab netdev_errors[] __errortab = {
  56. __einfo_errortab ( EINFO_EUNKNOWN_LINK_STATUS ),
  57. __einfo_errortab ( EINFO_ENOTCONN_LINK_DOWN ),
  58. };
  59. /**
  60. * Check whether or not network device has a link-layer address
  61. *
  62. * @v netdev Network device
  63. * @ret has_ll_addr Network device has a link-layer address
  64. */
  65. static int netdev_has_ll_addr ( struct net_device *netdev ) {
  66. uint8_t *ll_addr = netdev->ll_addr;
  67. size_t remaining = sizeof ( netdev->ll_addr );
  68. while ( remaining-- ) {
  69. if ( *(ll_addr++) != 0 )
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. /**
  75. * Notify drivers of network device or link state change
  76. *
  77. * @v netdev Network device
  78. */
  79. static void netdev_notify ( struct net_device *netdev ) {
  80. struct net_driver *driver;
  81. for_each_table_entry ( driver, NET_DRIVERS )
  82. driver->notify ( netdev );
  83. }
  84. /**
  85. * Mark network device as having a specific link state
  86. *
  87. * @v netdev Network device
  88. * @v rc Link status code
  89. */
  90. void netdev_link_err ( struct net_device *netdev, int rc ) {
  91. /* Record link state */
  92. netdev->link_rc = rc;
  93. if ( netdev->link_rc == 0 ) {
  94. DBGC ( netdev, "NETDEV %s link is up\n", netdev->name );
  95. } else {
  96. DBGC ( netdev, "NETDEV %s link is down: %s\n",
  97. netdev->name, strerror ( netdev->link_rc ) );
  98. }
  99. /* Notify drivers of link state change */
  100. netdev_notify ( netdev );
  101. }
  102. /**
  103. * Mark network device as having link down
  104. *
  105. * @v netdev Network device
  106. */
  107. void netdev_link_down ( struct net_device *netdev ) {
  108. /* Avoid clobbering a more detailed link status code, if one
  109. * is already set.
  110. */
  111. if ( ( netdev->link_rc == 0 ) ||
  112. ( netdev->link_rc == -EUNKNOWN_LINK_STATUS ) ) {
  113. netdev_link_err ( netdev, -ENOTCONN_LINK_DOWN );
  114. }
  115. }
  116. /**
  117. * Record network device statistic
  118. *
  119. * @v stats Network device statistics
  120. * @v rc Status code
  121. */
  122. static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
  123. struct net_device_error *error;
  124. struct net_device_error *least_common_error;
  125. unsigned int i;
  126. /* If this is not an error, just update the good counter */
  127. if ( rc == 0 ) {
  128. stats->good++;
  129. return;
  130. }
  131. /* Update the bad counter */
  132. stats->bad++;
  133. /* Locate the appropriate error record */
  134. least_common_error = &stats->errors[0];
  135. for ( i = 0 ; i < ( sizeof ( stats->errors ) /
  136. sizeof ( stats->errors[0] ) ) ; i++ ) {
  137. error = &stats->errors[i];
  138. /* Update matching record, if found */
  139. if ( error->rc == rc ) {
  140. error->count++;
  141. return;
  142. }
  143. if ( error->count < least_common_error->count )
  144. least_common_error = error;
  145. }
  146. /* Overwrite the least common error record */
  147. least_common_error->rc = rc;
  148. least_common_error->count = 1;
  149. }
  150. /**
  151. * Transmit raw packet via network device
  152. *
  153. * @v netdev Network device
  154. * @v iobuf I/O buffer
  155. * @ret rc Return status code
  156. *
  157. * Transmits the packet via the specified network device. This
  158. * function takes ownership of the I/O buffer.
  159. */
  160. int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  161. int rc;
  162. DBGC2 ( netdev, "NETDEV %s transmitting %p (%p+%zx)\n",
  163. netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
  164. /* Enqueue packet */
  165. list_add_tail ( &iobuf->list, &netdev->tx_queue );
  166. /* Avoid calling transmit() on unopened network devices */
  167. if ( ! netdev_is_open ( netdev ) ) {
  168. rc = -ENETUNREACH;
  169. goto err;
  170. }
  171. /* Discard packet (for test purposes) if applicable */
  172. if ( ( NETDEV_DISCARD_RATE > 0 ) &&
  173. ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
  174. rc = -EAGAIN;
  175. goto err;
  176. }
  177. /* Transmit packet */
  178. if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
  179. goto err;
  180. return 0;
  181. err:
  182. netdev_tx_complete_err ( netdev, iobuf, rc );
  183. return rc;
  184. }
  185. /**
  186. * Defer transmitted packet
  187. *
  188. * @v netdev Network device
  189. * @v iobuf I/O buffer
  190. *
  191. * Drivers may call netdev_tx_defer() if there is insufficient space
  192. * in the transmit descriptor ring. Any packets deferred in this way
  193. * will be automatically retransmitted as soon as space becomes
  194. * available (i.e. as soon as the driver calls netdev_tx_complete()).
  195. *
  196. * The packet must currently be in the network device's TX queue.
  197. *
  198. * Drivers utilising netdev_tx_defer() must ensure that space in the
  199. * transmit descriptor ring is freed up @b before calling
  200. * netdev_tx_complete(). For example, if the ring is modelled using a
  201. * producer counter and a consumer counter, then the consumer counter
  202. * must be incremented before the call to netdev_tx_complete().
  203. * Failure to do this will cause the retransmitted packet to be
  204. * immediately redeferred (which will result in out-of-order
  205. * transmissions and other nastiness).
  206. */
  207. void netdev_tx_defer ( struct net_device *netdev, struct io_buffer *iobuf ) {
  208. /* Catch data corruption as early as possible */
  209. list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
  210. /* Remove from transmit queue */
  211. list_del ( &iobuf->list );
  212. /* Add to deferred transmit queue */
  213. list_add_tail ( &iobuf->list, &netdev->tx_deferred );
  214. /* Record "out of space" statistic */
  215. netdev_tx_err ( netdev, NULL, -ENOBUFS );
  216. }
  217. /**
  218. * Discard transmitted packet
  219. *
  220. * @v netdev Network device
  221. * @v iobuf I/O buffer, or NULL
  222. * @v rc Packet status code
  223. *
  224. * The packet is discarded and a TX error is recorded. This function
  225. * takes ownership of the I/O buffer.
  226. */
  227. void netdev_tx_err ( struct net_device *netdev,
  228. struct io_buffer *iobuf, int rc ) {
  229. /* Update statistics counter */
  230. netdev_record_stat ( &netdev->tx_stats, rc );
  231. if ( rc == 0 ) {
  232. DBGC2 ( netdev, "NETDEV %s transmission %p complete\n",
  233. netdev->name, iobuf );
  234. } else {
  235. DBGC ( netdev, "NETDEV %s transmission %p failed: %s\n",
  236. netdev->name, iobuf, strerror ( rc ) );
  237. }
  238. /* Discard packet */
  239. free_iob ( iobuf );
  240. }
  241. /**
  242. * Complete network transmission
  243. *
  244. * @v netdev Network device
  245. * @v iobuf I/O buffer
  246. * @v rc Packet status code
  247. *
  248. * The packet must currently be in the network device's TX queue.
  249. */
  250. void netdev_tx_complete_err ( struct net_device *netdev,
  251. struct io_buffer *iobuf, int rc ) {
  252. /* Catch data corruption as early as possible */
  253. list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
  254. /* Dequeue and free I/O buffer */
  255. list_del ( &iobuf->list );
  256. netdev_tx_err ( netdev, iobuf, rc );
  257. /* Transmit first pending packet, if any */
  258. if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
  259. struct io_buffer, list ) ) != NULL ) {
  260. list_del ( &iobuf->list );
  261. netdev_tx ( netdev, iobuf );
  262. }
  263. }
  264. /**
  265. * Complete network transmission
  266. *
  267. * @v netdev Network device
  268. * @v rc Packet status code
  269. *
  270. * Completes the oldest outstanding packet in the TX queue.
  271. */
  272. void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
  273. struct io_buffer *iobuf;
  274. if ( ( iobuf = list_first_entry ( &netdev->tx_queue, struct io_buffer,
  275. list ) ) != NULL ) {
  276. netdev_tx_complete_err ( netdev, iobuf, rc );
  277. }
  278. }
  279. /**
  280. * Flush device's transmit queue
  281. *
  282. * @v netdev Network device
  283. */
  284. static void netdev_tx_flush ( struct net_device *netdev ) {
  285. /* Discard any packets in the TX queue. This will also cause
  286. * any packets in the deferred TX queue to be discarded
  287. * automatically.
  288. */
  289. while ( ! list_empty ( &netdev->tx_queue ) ) {
  290. netdev_tx_complete_next_err ( netdev, -ECANCELED );
  291. }
  292. assert ( list_empty ( &netdev->tx_queue ) );
  293. assert ( list_empty ( &netdev->tx_deferred ) );
  294. }
  295. /**
  296. * Add packet to receive queue
  297. *
  298. * @v netdev Network device
  299. * @v iobuf I/O buffer, or NULL
  300. *
  301. * The packet is added to the network device's RX queue. This
  302. * function takes ownership of the I/O buffer.
  303. */
  304. void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  305. DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
  306. netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
  307. /* Discard packet (for test purposes) if applicable */
  308. if ( ( NETDEV_DISCARD_RATE > 0 ) &&
  309. ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
  310. netdev_rx_err ( netdev, iobuf, -EAGAIN );
  311. return;
  312. }
  313. /* Enqueue packet */
  314. list_add_tail ( &iobuf->list, &netdev->rx_queue );
  315. /* Update statistics counter */
  316. netdev_record_stat ( &netdev->rx_stats, 0 );
  317. }
  318. /**
  319. * Discard received packet
  320. *
  321. * @v netdev Network device
  322. * @v iobuf I/O buffer, or NULL
  323. * @v rc Packet status code
  324. *
  325. * The packet is discarded and an RX error is recorded. This function
  326. * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
  327. * example, the net device wishes to report an error due to being
  328. * unable to allocate an I/O buffer.
  329. */
  330. void netdev_rx_err ( struct net_device *netdev,
  331. struct io_buffer *iobuf, int rc ) {
  332. DBGC ( netdev, "NETDEV %s failed to receive %p: %s\n",
  333. netdev->name, iobuf, strerror ( rc ) );
  334. /* Discard packet */
  335. free_iob ( iobuf );
  336. /* Update statistics counter */
  337. netdev_record_stat ( &netdev->rx_stats, rc );
  338. }
  339. /**
  340. * Poll for completed and received packets on network device
  341. *
  342. * @v netdev Network device
  343. *
  344. * Polls the network device for completed transmissions and received
  345. * packets. Any received packets will be added to the RX packet queue
  346. * via netdev_rx().
  347. */
  348. void netdev_poll ( struct net_device *netdev ) {
  349. if ( netdev_is_open ( netdev ) )
  350. netdev->op->poll ( netdev );
  351. }
  352. /**
  353. * Remove packet from device's receive queue
  354. *
  355. * @v netdev Network device
  356. * @ret iobuf I/O buffer, or NULL
  357. *
  358. * Removes the first packet from the device's RX queue and returns it.
  359. * Ownership of the packet is transferred to the caller.
  360. */
  361. struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
  362. struct io_buffer *iobuf;
  363. iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
  364. if ( ! iobuf )
  365. return NULL;
  366. list_del ( &iobuf->list );
  367. return iobuf;
  368. }
  369. /**
  370. * Flush device's receive queue
  371. *
  372. * @v netdev Network device
  373. */
  374. static void netdev_rx_flush ( struct net_device *netdev ) {
  375. struct io_buffer *iobuf;
  376. /* Discard any packets in the RX queue */
  377. while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  378. netdev_rx_err ( netdev, iobuf, -ECANCELED );
  379. }
  380. }
  381. /**
  382. * Free network device
  383. *
  384. * @v refcnt Network device reference counter
  385. */
  386. static void free_netdev ( struct refcnt *refcnt ) {
  387. struct net_device *netdev =
  388. container_of ( refcnt, struct net_device, refcnt );
  389. netdev_tx_flush ( netdev );
  390. netdev_rx_flush ( netdev );
  391. clear_settings ( netdev_settings ( netdev ) );
  392. free ( netdev );
  393. }
  394. /**
  395. * Allocate network device
  396. *
  397. * @v priv_size Size of private data area (net_device::priv)
  398. * @ret netdev Network device, or NULL
  399. *
  400. * Allocates space for a network device and its private data area.
  401. */
  402. struct net_device * alloc_netdev ( size_t priv_size ) {
  403. struct net_device *netdev;
  404. size_t total_len;
  405. total_len = ( sizeof ( *netdev ) + priv_size );
  406. netdev = zalloc ( total_len );
  407. if ( netdev ) {
  408. ref_init ( &netdev->refcnt, free_netdev );
  409. netdev->link_rc = -EUNKNOWN_LINK_STATUS;
  410. INIT_LIST_HEAD ( &netdev->tx_queue );
  411. INIT_LIST_HEAD ( &netdev->tx_deferred );
  412. INIT_LIST_HEAD ( &netdev->rx_queue );
  413. netdev_settings_init ( netdev );
  414. netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
  415. }
  416. return netdev;
  417. }
  418. /**
  419. * Register network device
  420. *
  421. * @v netdev Network device
  422. * @ret rc Return status code
  423. *
  424. * Gives the network device a name and adds it to the list of network
  425. * devices.
  426. */
  427. int register_netdev ( struct net_device *netdev ) {
  428. static unsigned int ifindex = 0;
  429. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  430. struct net_driver *driver;
  431. uint32_t seed;
  432. int rc;
  433. /* Create device name */
  434. if ( netdev->name[0] == '\0' ) {
  435. snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
  436. ifindex++ );
  437. }
  438. /* Set initial link-layer address, if not already set */
  439. if ( ! netdev_has_ll_addr ( netdev ) ) {
  440. ll_protocol->init_addr ( netdev->hw_addr, netdev->ll_addr );
  441. }
  442. /* Use least significant bits of the link-layer address to
  443. * improve the randomness of the (non-cryptographic) random
  444. * number generator.
  445. */
  446. memcpy ( &seed, ( netdev->ll_addr + ll_protocol->ll_addr_len
  447. - sizeof ( seed ) ), sizeof ( seed ) );
  448. srand ( rand() ^ seed );
  449. /* Add to device list */
  450. netdev_get ( netdev );
  451. list_add_tail ( &netdev->list, &net_devices );
  452. DBGC ( netdev, "NETDEV %s registered (phys %s hwaddr %s)\n",
  453. netdev->name, netdev->dev->name,
  454. netdev_addr ( netdev ) );
  455. /* Register per-netdev configuration settings */
  456. if ( ( rc = register_settings ( netdev_settings ( netdev ),
  457. NULL, netdev->name ) ) != 0 ) {
  458. DBGC ( netdev, "NETDEV %s could not register settings: %s\n",
  459. netdev->name, strerror ( rc ) );
  460. goto err_register_settings;
  461. }
  462. /* Probe device */
  463. for_each_table_entry ( driver, NET_DRIVERS ) {
  464. if ( ( rc = driver->probe ( netdev ) ) != 0 ) {
  465. DBGC ( netdev, "NETDEV %s could not add %s device: "
  466. "%s\n", netdev->name, driver->name,
  467. strerror ( rc ) );
  468. goto err_probe;
  469. }
  470. }
  471. return 0;
  472. err_probe:
  473. for_each_table_entry_continue_reverse ( driver, NET_DRIVERS )
  474. driver->remove ( netdev );
  475. clear_settings ( netdev_settings ( netdev ) );
  476. unregister_settings ( netdev_settings ( netdev ) );
  477. err_register_settings:
  478. return rc;
  479. }
  480. /**
  481. * Open network device
  482. *
  483. * @v netdev Network device
  484. * @ret rc Return status code
  485. */
  486. int netdev_open ( struct net_device *netdev ) {
  487. int rc;
  488. /* Do nothing if device is already open */
  489. if ( netdev->state & NETDEV_OPEN )
  490. return 0;
  491. DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
  492. /* Open the device */
  493. if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
  494. return rc;
  495. /* Mark as opened */
  496. netdev->state |= NETDEV_OPEN;
  497. /* Add to head of open devices list */
  498. list_add ( &netdev->open_list, &open_net_devices );
  499. /* Notify drivers of device state change */
  500. netdev_notify ( netdev );
  501. return 0;
  502. }
  503. /**
  504. * Close network device
  505. *
  506. * @v netdev Network device
  507. */
  508. void netdev_close ( struct net_device *netdev ) {
  509. /* Do nothing if device is already closed */
  510. if ( ! ( netdev->state & NETDEV_OPEN ) )
  511. return;
  512. DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
  513. /* Remove from open devices list */
  514. list_del ( &netdev->open_list );
  515. /* Mark as closed */
  516. netdev->state &= ~NETDEV_OPEN;
  517. /* Notify drivers of device state change */
  518. netdev_notify ( netdev );
  519. /* Close the device */
  520. netdev->op->close ( netdev );
  521. /* Flush TX and RX queues */
  522. netdev_tx_flush ( netdev );
  523. netdev_rx_flush ( netdev );
  524. }
  525. /**
  526. * Unregister network device
  527. *
  528. * @v netdev Network device
  529. *
  530. * Removes the network device from the list of network devices.
  531. */
  532. void unregister_netdev ( struct net_device *netdev ) {
  533. struct net_driver *driver;
  534. /* Ensure device is closed */
  535. netdev_close ( netdev );
  536. /* Remove device */
  537. for_each_table_entry_reverse ( driver, NET_DRIVERS )
  538. driver->remove ( netdev );
  539. /* Unregister per-netdev configuration settings */
  540. clear_settings ( netdev_settings ( netdev ) );
  541. unregister_settings ( netdev_settings ( netdev ) );
  542. /* Remove from device list */
  543. list_del ( &netdev->list );
  544. netdev_put ( netdev );
  545. DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
  546. }
  547. /** Enable or disable interrupts
  548. *
  549. * @v netdev Network device
  550. * @v enable Interrupts should be enabled
  551. */
  552. void netdev_irq ( struct net_device *netdev, int enable ) {
  553. /* Do nothing if device does not support interrupts */
  554. if ( ! netdev_irq_supported ( netdev ) )
  555. return;
  556. /* Enable or disable device interrupts */
  557. netdev->op->irq ( netdev, enable );
  558. /* Record interrupt enabled state */
  559. netdev->state &= ~NETDEV_IRQ_ENABLED;
  560. if ( enable )
  561. netdev->state |= NETDEV_IRQ_ENABLED;
  562. }
  563. /**
  564. * Get network device by name
  565. *
  566. * @v name Network device name
  567. * @ret netdev Network device, or NULL
  568. */
  569. struct net_device * find_netdev ( const char *name ) {
  570. struct net_device *netdev;
  571. /* Allow "netX" shortcut */
  572. if ( strcmp ( name, "netX" ) == 0 )
  573. return last_opened_netdev();
  574. /* Identify network device by name */
  575. list_for_each_entry ( netdev, &net_devices, list ) {
  576. if ( strcmp ( netdev->name, name ) == 0 )
  577. return netdev;
  578. }
  579. return NULL;
  580. }
  581. /**
  582. * Get network device by PCI bus:dev.fn address
  583. *
  584. * @v bus_type Bus type
  585. * @v location Bus location
  586. * @ret netdev Network device, or NULL
  587. */
  588. struct net_device * find_netdev_by_location ( unsigned int bus_type,
  589. unsigned int location ) {
  590. struct net_device *netdev;
  591. list_for_each_entry ( netdev, &net_devices, list ) {
  592. if ( ( netdev->dev->desc.bus_type == bus_type ) &&
  593. ( netdev->dev->desc.location == location ) )
  594. return netdev;
  595. }
  596. return NULL;
  597. }
  598. /**
  599. * Get most recently opened network device
  600. *
  601. * @ret netdev Most recently opened network device, or NULL
  602. */
  603. struct net_device * last_opened_netdev ( void ) {
  604. struct net_device *netdev;
  605. netdev = list_first_entry ( &open_net_devices, struct net_device,
  606. open_list );
  607. if ( ! netdev )
  608. return NULL;
  609. assert ( netdev_is_open ( netdev ) );
  610. return netdev;
  611. }
  612. /**
  613. * Transmit network-layer packet
  614. *
  615. * @v iobuf I/O buffer
  616. * @v netdev Network device
  617. * @v net_protocol Network-layer protocol
  618. * @v ll_dest Destination link-layer address
  619. * @v ll_source Source link-layer address
  620. * @ret rc Return status code
  621. *
  622. * Prepends link-layer headers to the I/O buffer and transmits the
  623. * packet via the specified network device. This function takes
  624. * ownership of the I/O buffer.
  625. */
  626. int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  627. struct net_protocol *net_protocol, const void *ll_dest,
  628. const void *ll_source ) {
  629. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  630. int rc;
  631. /* Add link-layer header */
  632. if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, ll_source,
  633. net_protocol->net_proto ) ) != 0 ) {
  634. /* Record error for diagnosis */
  635. netdev_tx_err ( netdev, iobuf, rc );
  636. return rc;
  637. }
  638. /* Transmit packet */
  639. return netdev_tx ( netdev, iobuf );
  640. }
  641. /**
  642. * Process received network-layer packet
  643. *
  644. * @v iobuf I/O buffer
  645. * @v netdev Network device
  646. * @v net_proto Network-layer protocol, in network-byte order
  647. * @v ll_dest Destination link-layer address
  648. * @v ll_source Source link-layer address
  649. * @v flags Packet flags
  650. * @ret rc Return status code
  651. */
  652. int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  653. uint16_t net_proto, const void *ll_dest, const void *ll_source,
  654. unsigned int flags ) {
  655. struct net_protocol *net_protocol;
  656. /* Hand off to network-layer protocol, if any */
  657. for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
  658. if ( net_protocol->net_proto == net_proto )
  659. return net_protocol->rx ( iobuf, netdev, ll_dest,
  660. ll_source, flags );
  661. }
  662. DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
  663. netdev->name, ntohs ( net_proto ) );
  664. free_iob ( iobuf );
  665. return -ENOTSUP;
  666. }
  667. /**
  668. * Poll the network stack
  669. *
  670. * This polls all interfaces for received packets, and processes
  671. * packets from the RX queue.
  672. */
  673. void net_poll ( void ) {
  674. struct net_device *netdev;
  675. struct io_buffer *iobuf;
  676. struct ll_protocol *ll_protocol;
  677. const void *ll_dest;
  678. const void *ll_source;
  679. uint16_t net_proto;
  680. unsigned int flags;
  681. int rc;
  682. /* Poll and process each network device */
  683. list_for_each_entry ( netdev, &net_devices, list ) {
  684. /* Poll for new packets */
  685. netdev_poll ( netdev );
  686. /* Leave received packets on the queue if receive
  687. * queue processing is currently frozen. This will
  688. * happen when the raw packets are to be manually
  689. * dequeued using netdev_rx_dequeue(), rather than
  690. * processed via the usual networking stack.
  691. */
  692. if ( netdev_rx_frozen ( netdev ) )
  693. continue;
  694. /* Process all received packets */
  695. while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  696. DBGC2 ( netdev, "NETDEV %s processing %p (%p+%zx)\n",
  697. netdev->name, iobuf, iobuf->data,
  698. iob_len ( iobuf ) );
  699. /* Remove link-layer header */
  700. ll_protocol = netdev->ll_protocol;
  701. if ( ( rc = ll_protocol->pull ( netdev, iobuf,
  702. &ll_dest, &ll_source,
  703. &net_proto,
  704. &flags ) ) != 0 ) {
  705. free_iob ( iobuf );
  706. continue;
  707. }
  708. /* Hand packet to network layer */
  709. if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
  710. net_proto, ll_dest,
  711. ll_source, flags ) ) != 0 ) {
  712. /* Record error for diagnosis */
  713. netdev_rx_err ( netdev, NULL, rc );
  714. }
  715. }
  716. }
  717. }
  718. /**
  719. * Single-step the network stack
  720. *
  721. * @v process Network stack process
  722. */
  723. static void net_step ( struct process *process __unused ) {
  724. net_poll();
  725. }
  726. /**
  727. * Get the VLAN tag (when VLAN support is not present)
  728. *
  729. * @v netdev Network device
  730. * @ret tag 0, indicating that device is not a VLAN device
  731. */
  732. __weak unsigned int vlan_tag ( struct net_device *netdev __unused ) {
  733. return 0;
  734. }
  735. /**
  736. * Identify VLAN device (when VLAN support is not present)
  737. *
  738. * @v trunk Trunk network device
  739. * @v tag VLAN tag
  740. * @ret netdev VLAN device, if any
  741. */
  742. __weak struct net_device * vlan_find ( struct net_device *trunk __unused,
  743. unsigned int tag __unused ) {
  744. return NULL;
  745. }
  746. /** Networking stack process */
  747. PERMANENT_PROCESS ( net_process, net_step );
  748. /**
  749. * Discard some cached network device data
  750. *
  751. * @ret discarded Number of cached items discarded
  752. */
  753. static unsigned int net_discard ( void ) {
  754. struct net_device *netdev;
  755. struct io_buffer *iobuf;
  756. unsigned int discarded = 0;
  757. /* Try to drop one deferred TX packet from each network device */
  758. for_each_netdev ( netdev ) {
  759. if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
  760. struct io_buffer,
  761. list ) ) != NULL ) {
  762. /* Discard first deferred packet */
  763. list_del ( &iobuf->list );
  764. free ( iobuf );
  765. /* Report discard */
  766. discarded++;
  767. }
  768. }
  769. return discarded;
  770. }
  771. /** Network device cache discarder */
  772. struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
  773. .discard = net_discard,
  774. };