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

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