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

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