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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. /* Handle pending transmit queue */
  345. while ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
  346. struct io_buffer, list ) ) ) {
  347. /* Remove from pending transmit queue */
  348. list_del ( &iobuf->list );
  349. /* When any transmit completion fails, cancel all
  350. * pending transmissions.
  351. */
  352. if ( rc != 0 ) {
  353. netdev_tx_err ( netdev, iobuf, -ECANCELED );
  354. continue;
  355. }
  356. /* Otherwise, attempt to transmit the first pending packet */
  357. netdev_tx ( netdev, iobuf );
  358. break;
  359. }
  360. }
  361. /**
  362. * Complete network transmission
  363. *
  364. * @v netdev Network device
  365. * @v rc Packet status code
  366. *
  367. * Completes the oldest outstanding packet in the TX queue.
  368. */
  369. void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
  370. struct io_buffer *iobuf;
  371. if ( ( iobuf = list_first_entry ( &netdev->tx_queue, struct io_buffer,
  372. list ) ) != NULL ) {
  373. netdev_tx_complete_err ( netdev, iobuf, rc );
  374. }
  375. }
  376. /**
  377. * Flush device's transmit queue
  378. *
  379. * @v netdev Network device
  380. */
  381. static void netdev_tx_flush ( struct net_device *netdev ) {
  382. /* Discard any packets in the TX queue. This will also cause
  383. * any packets in the deferred TX queue to be discarded
  384. * automatically.
  385. */
  386. while ( ! list_empty ( &netdev->tx_queue ) ) {
  387. netdev_tx_complete_next_err ( netdev, -ECANCELED );
  388. }
  389. assert ( list_empty ( &netdev->tx_queue ) );
  390. assert ( list_empty ( &netdev->tx_deferred ) );
  391. }
  392. /**
  393. * Add packet to receive queue
  394. *
  395. * @v netdev Network device
  396. * @v iobuf I/O buffer, or NULL
  397. *
  398. * The packet is added to the network device's RX queue. This
  399. * function takes ownership of the I/O buffer.
  400. */
  401. void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
  402. int rc;
  403. DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
  404. netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
  405. /* Discard packet (for test purposes) if applicable */
  406. if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 ) {
  407. netdev_rx_err ( netdev, iobuf, rc );
  408. return;
  409. }
  410. /* Enqueue packet */
  411. list_add_tail ( &iobuf->list, &netdev->rx_queue );
  412. /* Update statistics counter */
  413. netdev_record_stat ( &netdev->rx_stats, 0 );
  414. }
  415. /**
  416. * Discard received packet
  417. *
  418. * @v netdev Network device
  419. * @v iobuf I/O buffer, or NULL
  420. * @v rc Packet status code
  421. *
  422. * The packet is discarded and an RX error is recorded. This function
  423. * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
  424. * example, the net device wishes to report an error due to being
  425. * unable to allocate an I/O buffer.
  426. */
  427. void netdev_rx_err ( struct net_device *netdev,
  428. struct io_buffer *iobuf, int rc ) {
  429. DBGC ( netdev, "NETDEV %s failed to receive %p: %s\n",
  430. netdev->name, iobuf, strerror ( rc ) );
  431. /* Discard packet */
  432. free_iob ( iobuf );
  433. /* Update statistics counter */
  434. netdev_record_stat ( &netdev->rx_stats, rc );
  435. }
  436. /**
  437. * Poll for completed and received packets on network device
  438. *
  439. * @v netdev Network device
  440. *
  441. * Polls the network device for completed transmissions and received
  442. * packets. Any received packets will be added to the RX packet queue
  443. * via netdev_rx().
  444. */
  445. void netdev_poll ( struct net_device *netdev ) {
  446. if ( netdev_is_open ( netdev ) )
  447. netdev->op->poll ( netdev );
  448. }
  449. /**
  450. * Remove packet from device's receive queue
  451. *
  452. * @v netdev Network device
  453. * @ret iobuf I/O buffer, or NULL
  454. *
  455. * Removes the first packet from the device's RX queue and returns it.
  456. * Ownership of the packet is transferred to the caller.
  457. */
  458. struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
  459. struct io_buffer *iobuf;
  460. iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
  461. if ( ! iobuf )
  462. return NULL;
  463. list_del ( &iobuf->list );
  464. return iobuf;
  465. }
  466. /**
  467. * Flush device's receive queue
  468. *
  469. * @v netdev Network device
  470. */
  471. static void netdev_rx_flush ( struct net_device *netdev ) {
  472. struct io_buffer *iobuf;
  473. /* Discard any packets in the RX queue */
  474. while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  475. netdev_rx_err ( netdev, iobuf, -ECANCELED );
  476. }
  477. }
  478. /**
  479. * Finish network device configuration
  480. *
  481. * @v config Network device configuration
  482. * @v rc Reason for completion
  483. */
  484. static void netdev_config_close ( struct net_device_configuration *config,
  485. int rc ) {
  486. struct net_device_configurator *configurator = config->configurator;
  487. struct net_device *netdev = config->netdev;
  488. /* Restart interface */
  489. intf_restart ( &config->job, rc );
  490. /* Record configuration result */
  491. config->rc = rc;
  492. if ( rc == 0 ) {
  493. DBGC ( netdev, "NETDEV %s configured via %s\n",
  494. netdev->name, configurator->name );
  495. } else {
  496. DBGC ( netdev, "NETDEV %s configuration via %s failed: %s\n",
  497. netdev->name, configurator->name, strerror ( rc ) );
  498. }
  499. }
  500. /** Network device configuration interface operations */
  501. static struct interface_operation netdev_config_ops[] = {
  502. INTF_OP ( intf_close, struct net_device_configuration *,
  503. netdev_config_close ),
  504. };
  505. /** Network device configuration interface descriptor */
  506. static struct interface_descriptor netdev_config_desc =
  507. INTF_DESC ( struct net_device_configuration, job, netdev_config_ops );
  508. /**
  509. * Free network device
  510. *
  511. * @v refcnt Network device reference counter
  512. */
  513. static void free_netdev ( struct refcnt *refcnt ) {
  514. struct net_device *netdev =
  515. container_of ( refcnt, struct net_device, refcnt );
  516. stop_timer ( &netdev->link_block );
  517. netdev_tx_flush ( netdev );
  518. netdev_rx_flush ( netdev );
  519. clear_settings ( netdev_settings ( netdev ) );
  520. free ( netdev );
  521. }
  522. /**
  523. * Allocate network device
  524. *
  525. * @v priv_len Length of private data area (net_device::priv)
  526. * @ret netdev Network device, or NULL
  527. *
  528. * Allocates space for a network device and its private data area.
  529. */
  530. struct net_device * alloc_netdev ( size_t priv_len ) {
  531. struct net_device *netdev;
  532. struct net_device_configurator *configurator;
  533. struct net_device_configuration *config;
  534. unsigned int num_configs;
  535. size_t confs_len;
  536. size_t total_len;
  537. num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
  538. confs_len = ( num_configs * sizeof ( netdev->configs[0] ) );
  539. total_len = ( sizeof ( *netdev ) + confs_len + priv_len );
  540. netdev = zalloc ( total_len );
  541. if ( netdev ) {
  542. ref_init ( &netdev->refcnt, free_netdev );
  543. netdev->link_rc = -EUNKNOWN_LINK_STATUS;
  544. timer_init ( &netdev->link_block, netdev_link_block_expired,
  545. &netdev->refcnt );
  546. INIT_LIST_HEAD ( &netdev->tx_queue );
  547. INIT_LIST_HEAD ( &netdev->tx_deferred );
  548. INIT_LIST_HEAD ( &netdev->rx_queue );
  549. netdev_settings_init ( netdev );
  550. config = netdev->configs;
  551. for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ){
  552. config->netdev = netdev;
  553. config->configurator = configurator;
  554. config->rc = -EUNUSED_CONFIG;
  555. intf_init ( &config->job, &netdev_config_desc,
  556. &netdev->refcnt );
  557. config++;
  558. }
  559. netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) +
  560. confs_len );
  561. }
  562. return netdev;
  563. }
  564. /**
  565. * Register network device
  566. *
  567. * @v netdev Network device
  568. * @ret rc Return status code
  569. *
  570. * Gives the network device a name and adds it to the list of network
  571. * devices.
  572. */
  573. int register_netdev ( struct net_device *netdev ) {
  574. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  575. struct net_driver *driver;
  576. struct net_device *duplicate;
  577. uint32_t seed;
  578. int rc;
  579. /* Set initial link-layer address, if not already set */
  580. if ( ! netdev_has_ll_addr ( netdev ) ) {
  581. ll_protocol->init_addr ( netdev->hw_addr, netdev->ll_addr );
  582. }
  583. /* Set MTU, if not already set */
  584. if ( ! netdev->mtu ) {
  585. netdev->mtu = ( netdev->max_pkt_len -
  586. ll_protocol->ll_header_len );
  587. }
  588. /* Reject network devices that are already available via a
  589. * different hardware device.
  590. */
  591. duplicate = find_netdev_by_ll_addr ( ll_protocol, netdev->ll_addr );
  592. if ( duplicate && ( duplicate->dev != netdev->dev ) ) {
  593. DBGC ( netdev, "NETDEV rejecting duplicate (phys %s) of %s "
  594. "(phys %s)\n", netdev->dev->name, duplicate->name,
  595. duplicate->dev->name );
  596. rc = -EEXIST;
  597. goto err_duplicate;
  598. }
  599. /* Reject named network devices that already exist */
  600. if ( netdev->name[0] && ( duplicate = find_netdev ( netdev->name ) ) ) {
  601. DBGC ( netdev, "NETDEV rejecting duplicate name %s\n",
  602. duplicate->name );
  603. rc = -EEXIST;
  604. goto err_duplicate;
  605. }
  606. /* Record device index and create device name */
  607. if ( netdev->name[0] == '\0' ) {
  608. snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
  609. netdev_index );
  610. }
  611. netdev->index = ++netdev_index;
  612. /* Use least significant bits of the link-layer address to
  613. * improve the randomness of the (non-cryptographic) random
  614. * number generator.
  615. */
  616. memcpy ( &seed, ( netdev->ll_addr + ll_protocol->ll_addr_len
  617. - sizeof ( seed ) ), sizeof ( seed ) );
  618. srand ( rand() ^ seed );
  619. /* Add to device list */
  620. netdev_get ( netdev );
  621. list_add_tail ( &netdev->list, &net_devices );
  622. DBGC ( netdev, "NETDEV %s registered (phys %s hwaddr %s)\n",
  623. netdev->name, netdev->dev->name,
  624. netdev_addr ( netdev ) );
  625. /* Register per-netdev configuration settings */
  626. if ( ( rc = register_settings ( netdev_settings ( netdev ),
  627. NULL, netdev->name ) ) != 0 ) {
  628. DBGC ( netdev, "NETDEV %s could not register settings: %s\n",
  629. netdev->name, strerror ( rc ) );
  630. goto err_register_settings;
  631. }
  632. /* Probe device */
  633. for_each_table_entry ( driver, NET_DRIVERS ) {
  634. if ( driver->probe && ( rc = driver->probe ( netdev ) ) != 0 ) {
  635. DBGC ( netdev, "NETDEV %s could not add %s device: "
  636. "%s\n", netdev->name, driver->name,
  637. strerror ( rc ) );
  638. goto err_probe;
  639. }
  640. }
  641. return 0;
  642. err_probe:
  643. for_each_table_entry_continue_reverse ( driver, NET_DRIVERS ) {
  644. if ( driver->remove )
  645. driver->remove ( netdev );
  646. }
  647. clear_settings ( netdev_settings ( netdev ) );
  648. unregister_settings ( netdev_settings ( netdev ) );
  649. err_register_settings:
  650. list_del ( &netdev->list );
  651. netdev_put ( netdev );
  652. err_duplicate:
  653. return rc;
  654. }
  655. /**
  656. * Open network device
  657. *
  658. * @v netdev Network device
  659. * @ret rc Return status code
  660. */
  661. int netdev_open ( struct net_device *netdev ) {
  662. int rc;
  663. /* Do nothing if device is already open */
  664. if ( netdev->state & NETDEV_OPEN )
  665. return 0;
  666. DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
  667. /* Mark as opened */
  668. netdev->state |= NETDEV_OPEN;
  669. /* Open the device */
  670. if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
  671. goto err;
  672. /* Add to head of open devices list */
  673. list_add ( &netdev->open_list, &open_net_devices );
  674. /* Notify drivers of device state change */
  675. netdev_notify ( netdev );
  676. return 0;
  677. err:
  678. netdev->state &= ~NETDEV_OPEN;
  679. return rc;
  680. }
  681. /**
  682. * Close network device
  683. *
  684. * @v netdev Network device
  685. */
  686. void netdev_close ( struct net_device *netdev ) {
  687. unsigned int num_configs;
  688. unsigned int i;
  689. /* Do nothing if device is already closed */
  690. if ( ! ( netdev->state & NETDEV_OPEN ) )
  691. return;
  692. DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
  693. /* Terminate any ongoing configurations. Use intf_close()
  694. * rather than intf_restart() to allow the cancellation to be
  695. * reported back to us if a configuration is actually in
  696. * progress.
  697. */
  698. num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
  699. for ( i = 0 ; i < num_configs ; i++ )
  700. intf_close ( &netdev->configs[i].job, -ECANCELED );
  701. /* Remove from open devices list */
  702. list_del ( &netdev->open_list );
  703. /* Mark as closed */
  704. netdev->state &= ~NETDEV_OPEN;
  705. /* Notify drivers of device state change */
  706. netdev_notify ( netdev );
  707. /* Close the device */
  708. netdev->op->close ( netdev );
  709. /* Flush TX and RX queues */
  710. netdev_tx_flush ( netdev );
  711. netdev_rx_flush ( netdev );
  712. }
  713. /**
  714. * Unregister network device
  715. *
  716. * @v netdev Network device
  717. *
  718. * Removes the network device from the list of network devices.
  719. */
  720. void unregister_netdev ( struct net_device *netdev ) {
  721. struct net_driver *driver;
  722. /* Ensure device is closed */
  723. netdev_close ( netdev );
  724. /* Remove device */
  725. for_each_table_entry_reverse ( driver, NET_DRIVERS ) {
  726. if ( driver->remove )
  727. driver->remove ( netdev );
  728. }
  729. /* Unregister per-netdev configuration settings */
  730. clear_settings ( netdev_settings ( netdev ) );
  731. unregister_settings ( netdev_settings ( netdev ) );
  732. /* Remove from device list */
  733. DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
  734. list_del ( &netdev->list );
  735. netdev_put ( netdev );
  736. /* Reset network device index if no devices remain */
  737. if ( list_empty ( &net_devices ) )
  738. netdev_index = 0;
  739. }
  740. /** Enable or disable interrupts
  741. *
  742. * @v netdev Network device
  743. * @v enable Interrupts should be enabled
  744. */
  745. void netdev_irq ( struct net_device *netdev, int enable ) {
  746. /* Do nothing if device does not support interrupts */
  747. if ( ! netdev_irq_supported ( netdev ) )
  748. return;
  749. /* Enable or disable device interrupts */
  750. netdev->op->irq ( netdev, enable );
  751. /* Record interrupt enabled state */
  752. netdev->state &= ~NETDEV_IRQ_ENABLED;
  753. if ( enable )
  754. netdev->state |= NETDEV_IRQ_ENABLED;
  755. }
  756. /**
  757. * Get network device by name
  758. *
  759. * @v name Network device name
  760. * @ret netdev Network device, or NULL
  761. */
  762. struct net_device * find_netdev ( const char *name ) {
  763. struct net_device *netdev;
  764. /* Allow "netX" shortcut */
  765. if ( strcmp ( name, "netX" ) == 0 )
  766. return last_opened_netdev();
  767. /* Identify network device by name */
  768. list_for_each_entry ( netdev, &net_devices, list ) {
  769. if ( strcmp ( netdev->name, name ) == 0 )
  770. return netdev;
  771. }
  772. return NULL;
  773. }
  774. /**
  775. * Get network device by index
  776. *
  777. * @v index Network device index
  778. * @ret netdev Network device, or NULL
  779. */
  780. struct net_device * find_netdev_by_index ( unsigned int index ) {
  781. struct net_device *netdev;
  782. /* Identify network device by index */
  783. list_for_each_entry ( netdev, &net_devices, list ) {
  784. if ( netdev->index == index )
  785. return netdev;
  786. }
  787. return NULL;
  788. }
  789. /**
  790. * Get network device by PCI bus:dev.fn address
  791. *
  792. * @v bus_type Bus type
  793. * @v location Bus location
  794. * @ret netdev Network device, or NULL
  795. */
  796. struct net_device * find_netdev_by_location ( unsigned int bus_type,
  797. unsigned int location ) {
  798. struct net_device *netdev;
  799. list_for_each_entry ( netdev, &net_devices, list ) {
  800. if ( ( netdev->dev->desc.bus_type == bus_type ) &&
  801. ( netdev->dev->desc.location == location ) )
  802. return netdev;
  803. }
  804. return NULL;
  805. }
  806. /**
  807. * Get network device by link-layer address
  808. *
  809. * @v ll_protocol Link-layer protocol
  810. * @v ll_addr Link-layer address
  811. * @ret netdev Network device, or NULL
  812. */
  813. struct net_device * find_netdev_by_ll_addr ( struct ll_protocol *ll_protocol,
  814. const void *ll_addr ) {
  815. struct net_device *netdev;
  816. list_for_each_entry ( netdev, &net_devices, list ) {
  817. if ( ( netdev->ll_protocol == ll_protocol ) &&
  818. ( memcmp ( netdev->ll_addr, ll_addr,
  819. ll_protocol->ll_addr_len ) == 0 ) )
  820. return netdev;
  821. }
  822. return NULL;
  823. }
  824. /**
  825. * Get most recently opened network device
  826. *
  827. * @ret netdev Most recently opened network device, or NULL
  828. */
  829. struct net_device * last_opened_netdev ( void ) {
  830. struct net_device *netdev;
  831. netdev = list_first_entry ( &open_net_devices, struct net_device,
  832. open_list );
  833. if ( ! netdev )
  834. return NULL;
  835. assert ( netdev_is_open ( netdev ) );
  836. return netdev;
  837. }
  838. /**
  839. * Transmit network-layer packet
  840. *
  841. * @v iobuf I/O buffer
  842. * @v netdev Network device
  843. * @v net_protocol Network-layer protocol
  844. * @v ll_dest Destination link-layer address
  845. * @v ll_source Source link-layer address
  846. * @ret rc Return status code
  847. *
  848. * Prepends link-layer headers to the I/O buffer and transmits the
  849. * packet via the specified network device. This function takes
  850. * ownership of the I/O buffer.
  851. */
  852. int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  853. struct net_protocol *net_protocol, const void *ll_dest,
  854. const void *ll_source ) {
  855. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  856. int rc;
  857. /* Add link-layer header */
  858. if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, ll_source,
  859. net_protocol->net_proto ) ) != 0 ) {
  860. /* Record error for diagnosis */
  861. netdev_tx_err ( netdev, iobuf, rc );
  862. return rc;
  863. }
  864. /* Transmit packet */
  865. return netdev_tx ( netdev, iobuf );
  866. }
  867. /**
  868. * Process received network-layer packet
  869. *
  870. * @v iobuf I/O buffer
  871. * @v netdev Network device
  872. * @v net_proto Network-layer protocol, in network-byte order
  873. * @v ll_dest Destination link-layer address
  874. * @v ll_source Source link-layer address
  875. * @v flags Packet flags
  876. * @ret rc Return status code
  877. */
  878. int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  879. uint16_t net_proto, const void *ll_dest, const void *ll_source,
  880. unsigned int flags ) {
  881. struct net_protocol *net_protocol;
  882. /* Hand off to network-layer protocol, if any */
  883. for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
  884. if ( net_protocol->net_proto == net_proto )
  885. return net_protocol->rx ( iobuf, netdev, ll_dest,
  886. ll_source, flags );
  887. }
  888. DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
  889. netdev->name, ntohs ( net_proto ) );
  890. free_iob ( iobuf );
  891. return -ENOTSUP;
  892. }
  893. /**
  894. * Poll the network stack
  895. *
  896. * This polls all interfaces for received packets, and processes
  897. * packets from the RX queue.
  898. */
  899. void net_poll ( void ) {
  900. struct net_device *netdev;
  901. struct io_buffer *iobuf;
  902. struct ll_protocol *ll_protocol;
  903. const void *ll_dest;
  904. const void *ll_source;
  905. uint16_t net_proto;
  906. unsigned int flags;
  907. int rc;
  908. /* Poll and process each network device */
  909. list_for_each_entry ( netdev, &net_devices, list ) {
  910. /* Poll for new packets */
  911. profile_start ( &net_poll_profiler );
  912. netdev_poll ( netdev );
  913. profile_stop ( &net_poll_profiler );
  914. /* Leave received packets on the queue if receive
  915. * queue processing is currently frozen. This will
  916. * happen when the raw packets are to be manually
  917. * dequeued using netdev_rx_dequeue(), rather than
  918. * processed via the usual networking stack.
  919. */
  920. if ( netdev_rx_frozen ( netdev ) )
  921. continue;
  922. /* Process all received packets */
  923. while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
  924. DBGC2 ( netdev, "NETDEV %s processing %p (%p+%zx)\n",
  925. netdev->name, iobuf, iobuf->data,
  926. iob_len ( iobuf ) );
  927. profile_start ( &net_rx_profiler );
  928. /* Remove link-layer header */
  929. ll_protocol = netdev->ll_protocol;
  930. if ( ( rc = ll_protocol->pull ( netdev, iobuf,
  931. &ll_dest, &ll_source,
  932. &net_proto,
  933. &flags ) ) != 0 ) {
  934. free_iob ( iobuf );
  935. continue;
  936. }
  937. /* Hand packet to network layer */
  938. if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
  939. net_proto, ll_dest,
  940. ll_source, flags ) ) != 0 ) {
  941. /* Record error for diagnosis */
  942. netdev_rx_err ( netdev, NULL, rc );
  943. }
  944. profile_stop ( &net_rx_profiler );
  945. }
  946. }
  947. }
  948. /**
  949. * Single-step the network stack
  950. *
  951. * @v process Network stack process
  952. */
  953. static void net_step ( struct process *process __unused ) {
  954. net_poll();
  955. }
  956. /**
  957. * Get the VLAN tag (when VLAN support is not present)
  958. *
  959. * @v netdev Network device
  960. * @ret tag 0, indicating that device is not a VLAN device
  961. */
  962. __weak unsigned int vlan_tag ( struct net_device *netdev __unused ) {
  963. return 0;
  964. }
  965. /**
  966. * Identify VLAN device (when VLAN support is not present)
  967. *
  968. * @v trunk Trunk network device
  969. * @v tag VLAN tag
  970. * @ret netdev VLAN device, if any
  971. */
  972. __weak struct net_device * vlan_find ( struct net_device *trunk __unused,
  973. unsigned int tag __unused ) {
  974. return NULL;
  975. }
  976. /** Networking stack process */
  977. PERMANENT_PROCESS ( net_process, net_step );
  978. /**
  979. * Discard some cached network device data
  980. *
  981. * @ret discarded Number of cached items discarded
  982. */
  983. static unsigned int net_discard ( void ) {
  984. struct net_device *netdev;
  985. struct io_buffer *iobuf;
  986. unsigned int discarded = 0;
  987. /* Try to drop one deferred TX packet from each network device */
  988. for_each_netdev ( netdev ) {
  989. if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
  990. struct io_buffer,
  991. list ) ) != NULL ) {
  992. /* Discard first deferred packet */
  993. list_del ( &iobuf->list );
  994. free_iob ( iobuf );
  995. /* Report discard */
  996. discarded++;
  997. }
  998. }
  999. return discarded;
  1000. }
  1001. /** Network device cache discarder */
  1002. struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
  1003. .discard = net_discard,
  1004. };
  1005. /**
  1006. * Find network device configurator
  1007. *
  1008. * @v name Name
  1009. * @ret configurator Network device configurator, or NULL
  1010. */
  1011. struct net_device_configurator * find_netdev_configurator ( const char *name ) {
  1012. struct net_device_configurator *configurator;
  1013. for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
  1014. if ( strcmp ( configurator->name, name ) == 0 )
  1015. return configurator;
  1016. }
  1017. return NULL;
  1018. }
  1019. /**
  1020. * Start network device configuration
  1021. *
  1022. * @v netdev Network device
  1023. * @v configurator Network device configurator
  1024. * @ret rc Return status code
  1025. */
  1026. int netdev_configure ( struct net_device *netdev,
  1027. struct net_device_configurator *configurator ) {
  1028. struct net_device_configuration *config =
  1029. netdev_configuration ( netdev, configurator );
  1030. int rc;
  1031. /* Check applicability of configurator */
  1032. if ( ! netdev_configurator_applies ( netdev, configurator ) ) {
  1033. DBGC ( netdev, "NETDEV %s does not support configuration via "
  1034. "%s\n", netdev->name, configurator->name );
  1035. return -ENOTSUP;
  1036. }
  1037. /* Terminate any ongoing configuration */
  1038. intf_restart ( &config->job, -ECANCELED );
  1039. /* Mark configuration as being in progress */
  1040. config->rc = -EINPROGRESS_CONFIG;
  1041. DBGC ( netdev, "NETDEV %s starting configuration via %s\n",
  1042. netdev->name, configurator->name );
  1043. /* Start configuration */
  1044. if ( ( rc = configurator->start ( &config->job, netdev ) ) != 0 ) {
  1045. DBGC ( netdev, "NETDEV %s could not start configuration via "
  1046. "%s: %s\n", netdev->name, configurator->name,
  1047. strerror ( rc ) );
  1048. config->rc = rc;
  1049. return rc;
  1050. }
  1051. return 0;
  1052. }
  1053. /**
  1054. * Start network device configuration via all supported configurators
  1055. *
  1056. * @v netdev Network device
  1057. * @ret rc Return status code
  1058. */
  1059. int netdev_configure_all ( struct net_device *netdev ) {
  1060. struct net_device_configurator *configurator;
  1061. int rc;
  1062. /* Start configuration for each configurator */
  1063. for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
  1064. /* Skip any inapplicable configurators */
  1065. if ( ! netdev_configurator_applies ( netdev, configurator ) )
  1066. continue;
  1067. /* Start configuration */
  1068. if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 )
  1069. return rc;
  1070. }
  1071. return 0;
  1072. }
  1073. /**
  1074. * Check if network device has a configuration with a specified status code
  1075. *
  1076. * @v netdev Network device
  1077. * @v rc Status code
  1078. * @ret has_rc Network device has a configuration with this status code
  1079. */
  1080. static int netdev_has_configuration_rc ( struct net_device *netdev, int rc ) {
  1081. unsigned int num_configs;
  1082. unsigned int i;
  1083. num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
  1084. for ( i = 0 ; i < num_configs ; i++ ) {
  1085. if ( netdev->configs[i].rc == rc )
  1086. return 1;
  1087. }
  1088. return 0;
  1089. }
  1090. /**
  1091. * Check if network device configuration is in progress
  1092. *
  1093. * @v netdev Network device
  1094. * @ret is_in_progress Network device configuration is in progress
  1095. */
  1096. int netdev_configuration_in_progress ( struct net_device *netdev ) {
  1097. return netdev_has_configuration_rc ( netdev, -EINPROGRESS_CONFIG );
  1098. }
  1099. /**
  1100. * Check if network device has at least one successful configuration
  1101. *
  1102. * @v netdev Network device
  1103. * @v configurator Configurator
  1104. * @ret rc Return status code
  1105. */
  1106. int netdev_configuration_ok ( struct net_device *netdev ) {
  1107. return netdev_has_configuration_rc ( netdev, 0 );
  1108. }