Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

netdevice.c 32KB

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