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

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