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

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