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

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