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.

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