您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

netdevice.c 33KB

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