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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <byteswap.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <config/general.h>
  27. #include <ipxe/if_ether.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/tables.h>
  30. #include <ipxe/process.h>
  31. #include <ipxe/init.h>
  32. #include <ipxe/malloc.h>
  33. #include <ipxe/device.h>
  34. #include <ipxe/errortab.h>
  35. #include <ipxe/profile.h>
  36. #include <ipxe/vlan.h>
  37. #include <ipxe/netdevice.h>
  38. /** @file
  39. *
  40. * Network device management
  41. *
  42. */
  43. /** List of network devices */
  44. struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
  45. /** List of open network devices, in reverse order of opening */
  46. static struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
  47. /** Network polling profiler */
  48. static struct profiler net_poll_profiler __profiler = { .name = "net.poll" };
  49. /** Network receive profiler */
  50. static struct profiler net_rx_profiler __profiler = { .name = "net.rx" };
  51. /** Network transmit profiler */
  52. static struct profiler net_tx_profiler __profiler = { .name = "net.tx" };
  53. /** Default unknown link status code */
  54. #define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
  55. #define EINFO_EUNKNOWN_LINK_STATUS \
  56. __einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )
  57. /** Default not-yet-attempted-configuration status code */
  58. #define EUNUSED_CONFIG __einfo_error ( EINFO_EUNUSED_CONFIG )
  59. #define EINFO_EUNUSED_CONFIG \
  60. __einfo_uniqify ( EINFO_EINPROGRESS, 0x02, "Unused" )
  61. /** Default configuration-in-progress status code */
  62. #define EINPROGRESS_CONFIG __einfo_error ( EINFO_EINPROGRESS_CONFIG )
  63. #define EINFO_EINPROGRESS_CONFIG \
  64. __einfo_uniqify ( EINFO_EINPROGRESS, 0x03, "Incomplete" )
  65. /** Default link-down status code */
  66. #define ENOTCONN_LINK_DOWN __einfo_error ( EINFO_ENOTCONN_LINK_DOWN )
  67. #define EINFO_ENOTCONN_LINK_DOWN \
  68. __einfo_uniqify ( EINFO_ENOTCONN, 0x01, "Down" )
  69. /** Human-readable message for the default link statuses */
  70. struct errortab netdev_errors[] __errortab = {
  71. __einfo_errortab ( EINFO_EUNKNOWN_LINK_STATUS ),
  72. __einfo_errortab ( EINFO_ENOTCONN_LINK_DOWN ),
  73. __einfo_errortab ( EINFO_EUNUSED_CONFIG ),
  74. __einfo_errortab ( EINFO_EINPROGRESS_CONFIG ),
  75. };
  76. /**
  77. * Check whether or not network device has a link-layer address
  78. *
  79. * @v netdev Network device
  80. * @ret has_ll_addr Network device has a link-layer address
  81. */
  82. static int netdev_has_ll_addr ( struct net_device *netdev ) {
  83. uint8_t *ll_addr = netdev->ll_addr;
  84. size_t remaining = sizeof ( netdev->ll_addr );
  85. while ( remaining-- ) {
  86. if ( *(ll_addr++) != 0 )
  87. return 1;
  88. }
  89. return 0;
  90. }
  91. /**
  92. * Notify drivers of network device or link state change
  93. *
  94. * @v netdev Network device
  95. */
  96. static void netdev_notify ( struct net_device *netdev ) {
  97. struct net_driver *driver;
  98. for_each_table_entry ( driver, NET_DRIVERS ) {
  99. if ( driver->notify )
  100. driver->notify ( netdev );
  101. }
  102. }
  103. /**
  104. * Freeze network device receive queue processing
  105. *
  106. * @v netdev Network device
  107. */
  108. void netdev_rx_freeze ( struct net_device *netdev ) {
  109. /* Mark receive queue processing as frozen */
  110. netdev->state |= NETDEV_RX_FROZEN;
  111. /* Notify drivers of change */
  112. netdev_notify ( netdev );
  113. }
  114. /**
  115. * Unfreeze network device receive queue processing
  116. *
  117. * @v netdev Network device
  118. */
  119. void netdev_rx_unfreeze ( struct net_device *netdev ) {
  120. /* Mark receive queue processing as not frozen */
  121. netdev->state &= ~NETDEV_RX_FROZEN;
  122. /* Notify drivers of change */
  123. netdev_notify ( netdev );
  124. }
  125. /**
  126. * Mark network device as having a specific link state
  127. *
  128. * @v netdev Network device
  129. * @v rc Link status code
  130. */
  131. void netdev_link_err ( struct net_device *netdev, int rc ) {
  132. /* Record link state */
  133. netdev->link_rc = rc;
  134. if ( netdev->link_rc == 0 ) {
  135. DBGC ( netdev, "NETDEV %s link is up\n", netdev->name );
  136. } else {
  137. DBGC ( netdev, "NETDEV %s link is down: %s\n",
  138. netdev->name, strerror ( netdev->link_rc ) );
  139. }
  140. /* Notify drivers of link state change */
  141. netdev_notify ( netdev );
  142. }
  143. /**
  144. * Mark network device as having link down
  145. *
  146. * @v netdev Network device
  147. */
  148. void netdev_link_down ( struct net_device *netdev ) {
  149. /* Avoid clobbering a more detailed link status code, if one
  150. * is already set.
  151. */
  152. if ( ( netdev->link_rc == 0 ) ||
  153. ( netdev->link_rc == -EUNKNOWN_LINK_STATUS ) ) {
  154. netdev_link_err ( netdev, -ENOTCONN_LINK_DOWN );
  155. }
  156. }
  157. /**
  158. * Record network device statistic
  159. *
  160. * @v stats Network device statistics
  161. * @v rc Status code
  162. */
  163. static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
  164. struct net_device_error *error;
  165. struct net_device_error *least_common_error;
  166. unsigned int i;
  167. /* If this is not an error, just update the good counter */
  168. if ( rc == 0 ) {
  169. stats->good++;
  170. return;
  171. }
  172. /* Update the bad counter */
  173. stats->bad++;
  174. /* Locate the appropriate error record */
  175. least_common_error = &stats->errors[0];
  176. for ( i = 0 ; i < ( sizeof ( stats->errors ) /
  177. sizeof ( stats->errors[0] ) ) ; i++ ) {
  178. error = &stats->errors[i];
  179. /* Update matching record, if found */
  180. if ( error->rc == rc ) {
  181. error->count++;
  182. return;
  183. }
  184. if ( error->count < least_common_error->count )
  185. least_common_error = error;
  186. }
  187. /* Overwrite the least common error record */
  188. least_common_error->rc = rc;
  189. least_common_error->count = 1;
  190. }
  191. /**
  192. * Transmit raw packet via network device
  193. *
  194. * @v netdev Network device
  195. * @v iobuf I/O buffer
  196. * @ret rc Return status code
  197. *
  198. * Transmits the packet via the specified network device. This
  199. * function takes ownership of the I/O buffer.
  200. */
  201. int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  202. int rc;
  203. DBGC2 ( netdev, "NETDEV %s transmitting %p (%p+%zx)\n",
  204. netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
  205. profile_start ( &net_tx_profiler );
  206. /* Enqueue packet */
  207. list_add_tail ( &iobuf->list, &netdev->tx_queue );
  208. /* Avoid calling transmit() on unopened network devices */
  209. if ( ! netdev_is_open ( netdev ) ) {
  210. rc = -ENETUNREACH;
  211. goto err;
  212. }
  213. /* Discard packet (for test purposes) if applicable */
  214. if ( ( NETDEV_DISCARD_RATE > 0 ) &&
  215. ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
  216. rc = -EAGAIN;
  217. goto err;
  218. }
  219. /* Transmit packet */
  220. if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
  221. goto err;
  222. profile_stop ( &net_tx_profiler );
  223. return 0;
  224. err:
  225. netdev_tx_complete_err ( netdev, iobuf, rc );
  226. return rc;
  227. }
  228. /**
  229. * Defer transmitted packet
  230. *
  231. * @v netdev Network device
  232. * @v iobuf I/O buffer
  233. *
  234. * Drivers may call netdev_tx_defer() if there is insufficient space
  235. * in the transmit descriptor ring. Any packets deferred in this way
  236. * will be automatically retransmitted as soon as space becomes
  237. * available (i.e. as soon as the driver calls netdev_tx_complete()).
  238. *
  239. * The packet must currently be in the network device's TX queue.
  240. *
  241. * Drivers utilising netdev_tx_defer() must ensure that space in the
  242. * transmit descriptor ring is freed up @b before calling
  243. * netdev_tx_complete(). For example, if the ring is modelled using a
  244. * producer counter and a consumer counter, then the consumer counter
  245. * must be incremented before the call to netdev_tx_complete().
  246. * Failure to do this will cause the retransmitted packet to be
  247. * immediately redeferred (which will result in out-of-order
  248. * transmissions and other nastiness).
  249. */
  250. void netdev_tx_defer ( struct net_device *netdev, struct io_buffer *iobuf ) {
  251. /* Catch data corruption as early as possible */
  252. list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
  253. /* Remove from transmit queue */
  254. list_del ( &iobuf->list );
  255. /* Add to deferred transmit queue */
  256. list_add_tail ( &iobuf->list, &netdev->tx_deferred );
  257. /* Record "out of space" statistic */
  258. netdev_tx_err ( netdev, NULL, -ENOBUFS );
  259. }
  260. /**
  261. * Discard transmitted packet
  262. *
  263. * @v netdev Network device
  264. * @v iobuf I/O buffer, or NULL
  265. * @v rc Packet status code
  266. *
  267. * The packet is discarded and a TX error is recorded. This function
  268. * takes ownership of the I/O buffer.
  269. */
  270. void netdev_tx_err ( struct net_device *netdev,
  271. struct io_buffer *iobuf, int rc ) {
  272. /* Update statistics counter */
  273. netdev_record_stat ( &netdev->tx_stats, rc );
  274. if ( rc == 0 ) {
  275. DBGC2 ( netdev, "NETDEV %s transmission %p complete\n",
  276. netdev->name, iobuf );
  277. } else {
  278. DBGC ( netdev, "NETDEV %s transmission %p failed: %s\n",
  279. netdev->name, iobuf, strerror ( rc ) );
  280. }
  281. /* Discard packet */
  282. free_iob ( iobuf );
  283. }
  284. /**
  285. * Complete network transmission
  286. *
  287. * @v netdev Network device
  288. * @v iobuf I/O buffer
  289. * @v rc Packet status code
  290. *
  291. * The packet must currently be in the network device's TX queue.
  292. */
  293. void netdev_tx_complete_err ( struct net_device *netdev,
  294. struct io_buffer *iobuf, int rc ) {
  295. /* Catch data corruption as early as possible */
  296. list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
  297. /* Dequeue and free I/O buffer */
  298. list_del ( &iobuf->list );
  299. netdev_tx_err ( netdev, iobuf, rc );
  300. /* Transmit first pending packet, if any */
  301. if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
  302. struct io_buffer, list ) ) != NULL ) {
  303. list_del ( &iobuf->list );
  304. netdev_tx ( netdev, iobuf );
  305. }
  306. }
  307. /**
  308. * Complete network transmission
  309. *
  310. * @v netdev Network device
  311. * @v rc Packet status code
  312. *
  313. * Completes the oldest outstanding packet in the TX queue.
  314. */
  315. void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
  316. struct io_buffer *iobuf;
  317. if ( ( iobuf = list_first_entry ( &netdev->tx_queue, struct io_buffer,
  318. list ) ) != NULL ) {
  319. netdev_tx_complete_err ( netdev, iobuf, rc );
  320. }
  321. }
  322. /**
  323. * Flush device's transmit queue
  324. *
  325. * @v netdev Network device
  326. */
  327. static void netdev_tx_flush ( struct net_device *netdev ) {
  328. /* Discard any packets in the TX queue. This will also cause
  329. * any packets in the deferred TX queue to be discarded
  330. * automatically.
  331. */
  332. while ( ! list_empty ( &netdev->tx_queue ) ) {
  333. netdev_tx_complete_next_err ( netdev, -ECANCELED );
  334. }
  335. assert ( list_empty ( &netdev->tx_queue ) );
  336. assert ( list_empty ( &netdev->tx_deferred ) );
  337. }
  338. /**
  339. * Add packet to receive queue
  340. *
  341. * @v netdev Network device
  342. * @v iobuf I/O buffer, or NULL
  343. *
  344. * The packet is added to the network device's RX queue. This
  345. * function takes ownership of the I/O buffer.
  346. */
  347. void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  348. DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
  349. netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
  350. /* Discard packet (for test purposes) if applicable */
  351. if ( ( NETDEV_DISCARD_RATE > 0 ) &&
  352. ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
  353. netdev_rx_err ( netdev, iobuf, -EAGAIN );
  354. return;
  355. }
  356. /* Enqueue packet */
  357. list_add_tail ( &iobuf->list, &netdev->rx_queue );
  358. /* Update statistics counter */
  359. netdev_record_stat ( &netdev->rx_stats, 0 );
  360. }
  361. /**
  362. * Discard received packet
  363. *
  364. * @v netdev Network device
  365. * @v iobuf I/O buffer, or NULL
  366. * @v rc Packet status code
  367. *
  368. * The packet is discarded and an RX error is recorded. This function
  369. * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
  370. * example, the net device wishes to report an error due to being
  371. * unable to allocate an I/O buffer.
  372. */
  373. void netdev_rx_err ( struct net_device *netdev,
  374. struct io_buffer *iobuf, int rc ) {
  375. DBGC ( netdev, "NETDEV %s failed to receive %p: %s\n",
  376. netdev->name, iobuf, strerror ( rc ) );
  377. /* Discard packet */
  378. free_iob ( iobuf );
  379. /* Update statistics counter */
  380. netdev_record_stat ( &netdev->rx_stats, rc );
  381. }
  382. /**
  383. * Poll for completed and received packets on network device
  384. *
  385. * @v netdev Network device
  386. *
  387. * Polls the network device for completed transmissions and received
  388. * packets. Any received packets will be added to the RX packet queue
  389. * via netdev_rx().
  390. */
  391. void netdev_poll ( struct net_device *netdev ) {
  392. if ( netdev_is_open ( netdev ) )
  393. netdev->op->poll ( netdev );
  394. }
  395. /**
  396. * Remove packet from device's receive queue
  397. *
  398. * @v netdev Network device
  399. * @ret iobuf I/O buffer, or NULL
  400. *
  401. * Removes the first packet from the device's RX queue and returns it.
  402. * Ownership of the packet is transferred to the caller.
  403. */
  404. struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
  405. struct io_buffer *iobuf;
  406. iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
  407. if ( ! iobuf )
  408. return NULL;
  409. list_del ( &iobuf->list );
  410. return iobuf;
  411. }
  412. /**
  413. * Flush device's receive queue
  414. *
  415. * @v netdev Network device
  416. */
  417. static void netdev_rx_flush ( struct net_device *netdev ) {
  418. struct io_buffer *iobuf;
  419. /* Discard any packets in the RX queue */
  420. while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  421. netdev_rx_err ( netdev, iobuf, -ECANCELED );
  422. }
  423. }
  424. /**
  425. * Finish network device configuration
  426. *
  427. * @v config Network device configuration
  428. * @v rc Reason for completion
  429. */
  430. static void netdev_config_close ( struct net_device_configuration *config,
  431. int rc ) {
  432. struct net_device_configurator *configurator = config->configurator;
  433. struct net_device *netdev = config->netdev;
  434. /* Restart interface */
  435. intf_restart ( &config->job, rc );
  436. /* Record configuration result */
  437. config->rc = rc;
  438. if ( rc == 0 ) {
  439. DBGC ( netdev, "NETDEV %s configured via %s\n",
  440. netdev->name, configurator->name );
  441. } else {
  442. DBGC ( netdev, "NETDEV %s configuration via %s failed: %s\n",
  443. netdev->name, configurator->name, strerror ( rc ) );
  444. }
  445. }
  446. /** Network device configuration interface operations */
  447. static struct interface_operation netdev_config_ops[] = {
  448. INTF_OP ( intf_close, struct net_device_configuration *,
  449. netdev_config_close ),
  450. };
  451. /** Network device configuration interface descriptor */
  452. static struct interface_descriptor netdev_config_desc =
  453. INTF_DESC ( struct net_device_configuration, job, netdev_config_ops );
  454. /**
  455. * Free network device
  456. *
  457. * @v refcnt Network device reference counter
  458. */
  459. static void free_netdev ( struct refcnt *refcnt ) {
  460. struct net_device *netdev =
  461. container_of ( refcnt, struct net_device, refcnt );
  462. netdev_tx_flush ( netdev );
  463. netdev_rx_flush ( netdev );
  464. clear_settings ( netdev_settings ( netdev ) );
  465. free ( netdev );
  466. }
  467. /**
  468. * Allocate network device
  469. *
  470. * @v priv_len Length of private data area (net_device::priv)
  471. * @ret netdev Network device, or NULL
  472. *
  473. * Allocates space for a network device and its private data area.
  474. */
  475. struct net_device * alloc_netdev ( size_t priv_len ) {
  476. struct net_device *netdev;
  477. struct net_device_configurator *configurator;
  478. struct net_device_configuration *config;
  479. unsigned int num_configs;
  480. size_t confs_len;
  481. size_t total_len;
  482. num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
  483. confs_len = ( num_configs * sizeof ( netdev->configs[0] ) );
  484. total_len = ( sizeof ( *netdev ) + confs_len + priv_len );
  485. netdev = zalloc ( total_len );
  486. if ( netdev ) {
  487. ref_init ( &netdev->refcnt, free_netdev );
  488. netdev->link_rc = -EUNKNOWN_LINK_STATUS;
  489. INIT_LIST_HEAD ( &netdev->tx_queue );
  490. INIT_LIST_HEAD ( &netdev->tx_deferred );
  491. INIT_LIST_HEAD ( &netdev->rx_queue );
  492. netdev_settings_init ( netdev );
  493. config = netdev->configs;
  494. for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ){
  495. config->netdev = netdev;
  496. config->configurator = configurator;
  497. config->rc = -EUNUSED_CONFIG;
  498. intf_init ( &config->job, &netdev_config_desc,
  499. &netdev->refcnt );
  500. config++;
  501. }
  502. netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) +
  503. confs_len );
  504. }
  505. return netdev;
  506. }
  507. /**
  508. * Register network device
  509. *
  510. * @v netdev Network device
  511. * @ret rc Return status code
  512. *
  513. * Gives the network device a name and adds it to the list of network
  514. * devices.
  515. */
  516. int register_netdev ( struct net_device *netdev ) {
  517. static unsigned int ifindex = 0;
  518. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  519. struct net_driver *driver;
  520. uint32_t seed;
  521. int rc;
  522. /* Record device index and create device name */
  523. netdev->index = ifindex++;
  524. if ( netdev->name[0] == '\0' ) {
  525. snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
  526. netdev->index );
  527. }
  528. /* Set initial link-layer address, if not already set */
  529. if ( ! netdev_has_ll_addr ( netdev ) ) {
  530. ll_protocol->init_addr ( netdev->hw_addr, netdev->ll_addr );
  531. }
  532. /* Use least significant bits of the link-layer address to
  533. * improve the randomness of the (non-cryptographic) random
  534. * number generator.
  535. */
  536. memcpy ( &seed, ( netdev->ll_addr + ll_protocol->ll_addr_len
  537. - sizeof ( seed ) ), sizeof ( seed ) );
  538. srand ( rand() ^ seed );
  539. /* Add to device list */
  540. netdev_get ( netdev );
  541. list_add_tail ( &netdev->list, &net_devices );
  542. DBGC ( netdev, "NETDEV %s registered (phys %s hwaddr %s)\n",
  543. netdev->name, netdev->dev->name,
  544. netdev_addr ( netdev ) );
  545. /* Register per-netdev configuration settings */
  546. if ( ( rc = register_settings ( netdev_settings ( netdev ),
  547. NULL, netdev->name ) ) != 0 ) {
  548. DBGC ( netdev, "NETDEV %s could not register settings: %s\n",
  549. netdev->name, strerror ( rc ) );
  550. goto err_register_settings;
  551. }
  552. /* Probe device */
  553. for_each_table_entry ( driver, NET_DRIVERS ) {
  554. if ( driver->probe && ( rc = driver->probe ( netdev ) ) != 0 ) {
  555. DBGC ( netdev, "NETDEV %s could not add %s device: "
  556. "%s\n", netdev->name, driver->name,
  557. strerror ( rc ) );
  558. goto err_probe;
  559. }
  560. }
  561. return 0;
  562. err_probe:
  563. for_each_table_entry_continue_reverse ( driver, NET_DRIVERS ) {
  564. if ( driver->remove )
  565. driver->remove ( netdev );
  566. }
  567. clear_settings ( netdev_settings ( netdev ) );
  568. unregister_settings ( netdev_settings ( netdev ) );
  569. err_register_settings:
  570. return rc;
  571. }
  572. /**
  573. * Open network device
  574. *
  575. * @v netdev Network device
  576. * @ret rc Return status code
  577. */
  578. int netdev_open ( struct net_device *netdev ) {
  579. int rc;
  580. /* Do nothing if device is already open */
  581. if ( netdev->state & NETDEV_OPEN )
  582. return 0;
  583. DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
  584. /* Mark as opened */
  585. netdev->state |= NETDEV_OPEN;
  586. /* Open the device */
  587. if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
  588. goto err;
  589. /* Add to head of open devices list */
  590. list_add ( &netdev->open_list, &open_net_devices );
  591. /* Notify drivers of device state change */
  592. netdev_notify ( netdev );
  593. return 0;
  594. err:
  595. netdev->state &= ~NETDEV_OPEN;
  596. return rc;
  597. }
  598. /**
  599. * Close network device
  600. *
  601. * @v netdev Network device
  602. */
  603. void netdev_close ( struct net_device *netdev ) {
  604. unsigned int num_configs;
  605. unsigned int i;
  606. /* Do nothing if device is already closed */
  607. if ( ! ( netdev->state & NETDEV_OPEN ) )
  608. return;
  609. DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
  610. /* Terminate any ongoing configurations. Use intf_close()
  611. * rather than intf_restart() to allow the cancellation to be
  612. * reported back to us if a configuration is actually in
  613. * progress.
  614. */
  615. num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
  616. for ( i = 0 ; i < num_configs ; i++ )
  617. intf_close ( &netdev->configs[i].job, -ECANCELED );
  618. /* Remove from open devices list */
  619. list_del ( &netdev->open_list );
  620. /* Mark as closed */
  621. netdev->state &= ~NETDEV_OPEN;
  622. /* Notify drivers of device state change */
  623. netdev_notify ( netdev );
  624. /* Close the device */
  625. netdev->op->close ( netdev );
  626. /* Flush TX and RX queues */
  627. netdev_tx_flush ( netdev );
  628. netdev_rx_flush ( netdev );
  629. }
  630. /**
  631. * Unregister network device
  632. *
  633. * @v netdev Network device
  634. *
  635. * Removes the network device from the list of network devices.
  636. */
  637. void unregister_netdev ( struct net_device *netdev ) {
  638. struct net_driver *driver;
  639. /* Ensure device is closed */
  640. netdev_close ( netdev );
  641. /* Remove device */
  642. for_each_table_entry_reverse ( driver, NET_DRIVERS ) {
  643. if ( driver->remove )
  644. driver->remove ( netdev );
  645. }
  646. /* Unregister per-netdev configuration settings */
  647. clear_settings ( netdev_settings ( netdev ) );
  648. unregister_settings ( netdev_settings ( netdev ) );
  649. /* Remove from device list */
  650. DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
  651. list_del ( &netdev->list );
  652. netdev_put ( netdev );
  653. }
  654. /** Enable or disable interrupts
  655. *
  656. * @v netdev Network device
  657. * @v enable Interrupts should be enabled
  658. */
  659. void netdev_irq ( struct net_device *netdev, int enable ) {
  660. /* Do nothing if device does not support interrupts */
  661. if ( ! netdev_irq_supported ( netdev ) )
  662. return;
  663. /* Enable or disable device interrupts */
  664. netdev->op->irq ( netdev, enable );
  665. /* Record interrupt enabled state */
  666. netdev->state &= ~NETDEV_IRQ_ENABLED;
  667. if ( enable )
  668. netdev->state |= NETDEV_IRQ_ENABLED;
  669. }
  670. /**
  671. * Get network device by name
  672. *
  673. * @v name Network device name
  674. * @ret netdev Network device, or NULL
  675. */
  676. struct net_device * find_netdev ( const char *name ) {
  677. struct net_device *netdev;
  678. /* Allow "netX" shortcut */
  679. if ( strcmp ( name, "netX" ) == 0 )
  680. return last_opened_netdev();
  681. /* Identify network device by name */
  682. list_for_each_entry ( netdev, &net_devices, list ) {
  683. if ( strcmp ( netdev->name, name ) == 0 )
  684. return netdev;
  685. }
  686. return NULL;
  687. }
  688. /**
  689. * Get network device by index
  690. *
  691. * @v index Network device index
  692. * @ret netdev Network device, or NULL
  693. */
  694. struct net_device * find_netdev_by_index ( unsigned int index ) {
  695. struct net_device *netdev;
  696. /* Identify network device by index */
  697. list_for_each_entry ( netdev, &net_devices, list ) {
  698. if ( netdev->index == index )
  699. return netdev;
  700. }
  701. return NULL;
  702. }
  703. /**
  704. * Get network device by PCI bus:dev.fn address
  705. *
  706. * @v bus_type Bus type
  707. * @v location Bus location
  708. * @ret netdev Network device, or NULL
  709. */
  710. struct net_device * find_netdev_by_location ( unsigned int bus_type,
  711. unsigned int location ) {
  712. struct net_device *netdev;
  713. list_for_each_entry ( netdev, &net_devices, list ) {
  714. if ( ( netdev->dev->desc.bus_type == bus_type ) &&
  715. ( netdev->dev->desc.location == location ) )
  716. return netdev;
  717. }
  718. return NULL;
  719. }
  720. /**
  721. * Get most recently opened network device
  722. *
  723. * @ret netdev Most recently opened network device, or NULL
  724. */
  725. struct net_device * last_opened_netdev ( void ) {
  726. struct net_device *netdev;
  727. netdev = list_first_entry ( &open_net_devices, struct net_device,
  728. open_list );
  729. if ( ! netdev )
  730. return NULL;
  731. assert ( netdev_is_open ( netdev ) );
  732. return netdev;
  733. }
  734. /**
  735. * Transmit network-layer packet
  736. *
  737. * @v iobuf I/O buffer
  738. * @v netdev Network device
  739. * @v net_protocol Network-layer protocol
  740. * @v ll_dest Destination link-layer address
  741. * @v ll_source Source link-layer address
  742. * @ret rc Return status code
  743. *
  744. * Prepends link-layer headers to the I/O buffer and transmits the
  745. * packet via the specified network device. This function takes
  746. * ownership of the I/O buffer.
  747. */
  748. int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  749. struct net_protocol *net_protocol, const void *ll_dest,
  750. const void *ll_source ) {
  751. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  752. int rc;
  753. /* Add link-layer header */
  754. if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, ll_source,
  755. net_protocol->net_proto ) ) != 0 ) {
  756. /* Record error for diagnosis */
  757. netdev_tx_err ( netdev, iobuf, rc );
  758. return rc;
  759. }
  760. /* Transmit packet */
  761. return netdev_tx ( netdev, iobuf );
  762. }
  763. /**
  764. * Process received network-layer packet
  765. *
  766. * @v iobuf I/O buffer
  767. * @v netdev Network device
  768. * @v net_proto Network-layer protocol, in network-byte order
  769. * @v ll_dest Destination link-layer address
  770. * @v ll_source Source link-layer address
  771. * @v flags Packet flags
  772. * @ret rc Return status code
  773. */
  774. int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  775. uint16_t net_proto, const void *ll_dest, const void *ll_source,
  776. unsigned int flags ) {
  777. struct net_protocol *net_protocol;
  778. /* Hand off to network-layer protocol, if any */
  779. for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
  780. if ( net_protocol->net_proto == net_proto )
  781. return net_protocol->rx ( iobuf, netdev, ll_dest,
  782. ll_source, flags );
  783. }
  784. DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
  785. netdev->name, ntohs ( net_proto ) );
  786. free_iob ( iobuf );
  787. return -ENOTSUP;
  788. }
  789. /**
  790. * Poll the network stack
  791. *
  792. * This polls all interfaces for received packets, and processes
  793. * packets from the RX queue.
  794. */
  795. void net_poll ( void ) {
  796. struct net_device *netdev;
  797. struct io_buffer *iobuf;
  798. struct ll_protocol *ll_protocol;
  799. const void *ll_dest;
  800. const void *ll_source;
  801. uint16_t net_proto;
  802. unsigned int flags;
  803. int rc;
  804. /* Poll and process each network device */
  805. list_for_each_entry ( netdev, &net_devices, list ) {
  806. /* Poll for new packets */
  807. profile_start ( &net_poll_profiler );
  808. netdev_poll ( netdev );
  809. profile_stop ( &net_poll_profiler );
  810. /* Leave received packets on the queue if receive
  811. * queue processing is currently frozen. This will
  812. * happen when the raw packets are to be manually
  813. * dequeued using netdev_rx_dequeue(), rather than
  814. * processed via the usual networking stack.
  815. */
  816. if ( netdev_rx_frozen ( netdev ) )
  817. continue;
  818. /* Process all received packets */
  819. while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  820. DBGC2 ( netdev, "NETDEV %s processing %p (%p+%zx)\n",
  821. netdev->name, iobuf, iobuf->data,
  822. iob_len ( iobuf ) );
  823. profile_start ( &net_rx_profiler );
  824. /* Remove link-layer header */
  825. ll_protocol = netdev->ll_protocol;
  826. if ( ( rc = ll_protocol->pull ( netdev, iobuf,
  827. &ll_dest, &ll_source,
  828. &net_proto,
  829. &flags ) ) != 0 ) {
  830. free_iob ( iobuf );
  831. continue;
  832. }
  833. /* Hand packet to network layer */
  834. if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
  835. net_proto, ll_dest,
  836. ll_source, flags ) ) != 0 ) {
  837. /* Record error for diagnosis */
  838. netdev_rx_err ( netdev, NULL, rc );
  839. }
  840. profile_stop ( &net_rx_profiler );
  841. }
  842. }
  843. }
  844. /**
  845. * Single-step the network stack
  846. *
  847. * @v process Network stack process
  848. */
  849. static void net_step ( struct process *process __unused ) {
  850. net_poll();
  851. }
  852. /**
  853. * Get the VLAN tag (when VLAN support is not present)
  854. *
  855. * @v netdev Network device
  856. * @ret tag 0, indicating that device is not a VLAN device
  857. */
  858. __weak unsigned int vlan_tag ( struct net_device *netdev __unused ) {
  859. return 0;
  860. }
  861. /**
  862. * Identify VLAN device (when VLAN support is not present)
  863. *
  864. * @v trunk Trunk network device
  865. * @v tag VLAN tag
  866. * @ret netdev VLAN device, if any
  867. */
  868. __weak struct net_device * vlan_find ( struct net_device *trunk __unused,
  869. unsigned int tag __unused ) {
  870. return NULL;
  871. }
  872. /** Networking stack process */
  873. PERMANENT_PROCESS ( net_process, net_step );
  874. /**
  875. * Discard some cached network device data
  876. *
  877. * @ret discarded Number of cached items discarded
  878. */
  879. static unsigned int net_discard ( void ) {
  880. struct net_device *netdev;
  881. struct io_buffer *iobuf;
  882. unsigned int discarded = 0;
  883. /* Try to drop one deferred TX packet from each network device */
  884. for_each_netdev ( netdev ) {
  885. if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
  886. struct io_buffer,
  887. list ) ) != NULL ) {
  888. /* Discard first deferred packet */
  889. list_del ( &iobuf->list );
  890. free ( iobuf );
  891. /* Report discard */
  892. discarded++;
  893. }
  894. }
  895. return discarded;
  896. }
  897. /** Network device cache discarder */
  898. struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
  899. .discard = net_discard,
  900. };
  901. /**
  902. * Find network device configurator
  903. *
  904. * @v name Name
  905. * @ret configurator Network device configurator, or NULL
  906. */
  907. struct net_device_configurator * find_netdev_configurator ( const char *name ) {
  908. struct net_device_configurator *configurator;
  909. for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
  910. if ( strcmp ( configurator->name, name ) == 0 )
  911. return configurator;
  912. }
  913. return NULL;
  914. }
  915. /**
  916. * Start network device configuration
  917. *
  918. * @v netdev Network device
  919. * @v configurator Network device configurator
  920. * @ret rc Return status code
  921. */
  922. int netdev_configure ( struct net_device *netdev,
  923. struct net_device_configurator *configurator ) {
  924. struct net_device_configuration *config =
  925. netdev_configuration ( netdev, configurator );
  926. int rc;
  927. /* Check applicability of configurator */
  928. if ( ! netdev_configurator_applies ( netdev, configurator ) ) {
  929. DBGC ( netdev, "NETDEV %s does not support configuration via "
  930. "%s\n", netdev->name, configurator->name );
  931. return -ENOTSUP;
  932. }
  933. /* Terminate any ongoing configuration */
  934. intf_restart ( &config->job, -ECANCELED );
  935. /* Mark configuration as being in progress */
  936. config->rc = -EINPROGRESS_CONFIG;
  937. DBGC ( netdev, "NETDEV %s starting configuration via %s\n",
  938. netdev->name, configurator->name );
  939. /* Start configuration */
  940. if ( ( rc = configurator->start ( &config->job, netdev ) ) != 0 ) {
  941. DBGC ( netdev, "NETDEV %s could not start configuration via "
  942. "%s: %s\n", netdev->name, configurator->name,
  943. strerror ( rc ) );
  944. config->rc = rc;
  945. return rc;
  946. }
  947. return 0;
  948. }
  949. /**
  950. * Start network device configuration via all supported configurators
  951. *
  952. * @v netdev Network device
  953. * @ret rc Return status code
  954. */
  955. int netdev_configure_all ( struct net_device *netdev ) {
  956. struct net_device_configurator *configurator;
  957. int rc;
  958. /* Start configuration for each configurator */
  959. for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
  960. /* Skip any inapplicable configurators */
  961. if ( ! netdev_configurator_applies ( netdev, configurator ) )
  962. continue;
  963. /* Start configuration */
  964. if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 )
  965. return rc;
  966. }
  967. return 0;
  968. }
  969. /**
  970. * Check if network device has a configuration with a specified status code
  971. *
  972. * @v netdev Network device
  973. * @v rc Status code
  974. * @ret has_rc Network device has a configuration with this status code
  975. */
  976. static int netdev_has_configuration_rc ( struct net_device *netdev, int rc ) {
  977. unsigned int num_configs;
  978. unsigned int i;
  979. num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
  980. for ( i = 0 ; i < num_configs ; i++ ) {
  981. if ( netdev->configs[i].rc == rc )
  982. return 1;
  983. }
  984. return 0;
  985. }
  986. /**
  987. * Check if network device configuration is in progress
  988. *
  989. * @v netdev Network device
  990. * @ret is_in_progress Network device configuration is in progress
  991. */
  992. int netdev_configuration_in_progress ( struct net_device *netdev ) {
  993. return netdev_has_configuration_rc ( netdev, -EINPROGRESS_CONFIG );
  994. }
  995. /**
  996. * Check if network device has at least one successful configuration
  997. *
  998. * @v netdev Network device
  999. * @v configurator Configurator
  1000. * @ret rc Return status code
  1001. */
  1002. int netdev_configuration_ok ( struct net_device *netdev ) {
  1003. return netdev_has_configuration_rc ( netdev, 0 );
  1004. }