Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

netfront.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * Copyright (C) 2014 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 (at your option) 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 <errno.h>
  27. #include <ipxe/netdevice.h>
  28. #include <ipxe/ethernet.h>
  29. #include <ipxe/if_ether.h>
  30. #include <ipxe/malloc.h>
  31. #include <ipxe/base16.h>
  32. #include <ipxe/xen.h>
  33. #include <ipxe/xenstore.h>
  34. #include <ipxe/xenbus.h>
  35. #include <ipxe/xengrant.h>
  36. #include <ipxe/xenevent.h>
  37. #include "netfront.h"
  38. /** @file
  39. *
  40. * Xen netfront driver
  41. *
  42. */
  43. /* Disambiguate the various error causes */
  44. #define EIO_NETIF_RSP_ERROR \
  45. __einfo_error ( EINFO_EIO_NETIF_RSP_ERROR )
  46. #define EINFO_EIO_NETIF_RSP_ERROR \
  47. __einfo_uniqify ( EINFO_EIO, -NETIF_RSP_ERROR, \
  48. "Unspecified network error" )
  49. #define EIO_NETIF_RSP_DROPPED \
  50. __einfo_error ( EINFO_EIO_NETIF_RSP_DROPPED )
  51. #define EINFO_EIO_NETIF_RSP_DROPPED \
  52. __einfo_uniqify ( EINFO_EIO, -NETIF_RSP_DROPPED, \
  53. "Packet dropped" )
  54. #define EIO_NETIF_RSP( status ) \
  55. EUNIQ ( EINFO_EIO, -(status), \
  56. EIO_NETIF_RSP_ERROR, EIO_NETIF_RSP_DROPPED )
  57. /******************************************************************************
  58. *
  59. * XenStore interface
  60. *
  61. ******************************************************************************
  62. */
  63. /**
  64. * Reset device
  65. *
  66. * @v netfront Netfront device
  67. * @ret rc Return status code
  68. */
  69. static int netfront_reset ( struct netfront_nic *netfront ) {
  70. struct xen_device *xendev = netfront->xendev;
  71. int state;
  72. int rc;
  73. /* Get current backend state */
  74. if ( ( state = xenbus_backend_state ( xendev ) ) < 0 ) {
  75. rc = state;
  76. DBGC ( netfront, "NETFRONT %s could not read backend state: "
  77. "%s\n", xendev->key, strerror ( rc ) );
  78. return rc;
  79. }
  80. /* If the backend is not already in InitWait, then mark
  81. * frontend as Closed to shut down the backend.
  82. */
  83. if ( state != XenbusStateInitWait ) {
  84. /* Set state to Closed */
  85. xenbus_set_state ( xendev, XenbusStateClosed );
  86. /* Wait for backend to reach Closed */
  87. if ( ( rc = xenbus_backend_wait ( xendev,
  88. XenbusStateClosed ) ) != 0 ) {
  89. DBGC ( netfront, "NETFRONT %s backend did not reach "
  90. "Closed: %s\n", xendev->key, strerror ( rc ) );
  91. return rc;
  92. }
  93. }
  94. /* Reset state to Initialising */
  95. xenbus_set_state ( xendev, XenbusStateInitialising );
  96. /* Wait for backend to reach InitWait */
  97. if ( ( rc = xenbus_backend_wait ( xendev, XenbusStateInitWait ) ) != 0){
  98. DBGC ( netfront, "NETFRONT %s backend did not reach InitWait: "
  99. "%s\n", xendev->key, strerror ( rc ) );
  100. return rc;
  101. }
  102. return 0;
  103. }
  104. /**
  105. * Fetch MAC address
  106. *
  107. * @v netfront Netfront device
  108. * @v hw_addr Hardware address to fill in
  109. * @ret rc Return status code
  110. */
  111. static int netfront_read_mac ( struct netfront_nic *netfront, void *hw_addr ) {
  112. struct xen_device *xendev = netfront->xendev;
  113. struct xen_hypervisor *xen = xendev->xen;
  114. char *mac;
  115. int len;
  116. int rc;
  117. /* Fetch MAC address */
  118. if ( ( rc = xenstore_read ( xen, &mac, xendev->key, "mac", NULL ) )!=0){
  119. DBGC ( netfront, "NETFRONT %s could not read MAC address: %s\n",
  120. xendev->key, strerror ( rc ) );
  121. goto err_xenstore_read;
  122. }
  123. DBGC2 ( netfront, "NETFRONT %s has MAC address \"%s\"\n",
  124. xendev->key, mac );
  125. /* Decode MAC address */
  126. len = hex_decode ( ':', mac, hw_addr, ETH_ALEN );
  127. if ( len < 0 ) {
  128. rc = len;
  129. DBGC ( netfront, "NETFRONT %s could not decode MAC address "
  130. "\"%s\": %s\n", xendev->key, mac, strerror ( rc ) );
  131. goto err_decode;
  132. }
  133. /* Success */
  134. rc = 0;
  135. err_decode:
  136. free ( mac );
  137. err_xenstore_read:
  138. return rc;
  139. }
  140. /**
  141. * Write XenStore numeric value
  142. *
  143. * @v netfront Netfront device
  144. * @v subkey Subkey
  145. * @v num Numeric value
  146. * @ret rc Return status code
  147. */
  148. static int netfront_write_num ( struct netfront_nic *netfront,
  149. const char *subkey, unsigned long num ) {
  150. struct xen_device *xendev = netfront->xendev;
  151. struct xen_hypervisor *xen = xendev->xen;
  152. int rc;
  153. /* Write value */
  154. if ( ( rc = xenstore_write_num ( xen, num, xendev->key, subkey,
  155. NULL ) ) != 0 ) {
  156. DBGC ( netfront, "NETFRONT %s could not set %s=\"%ld\": %s\n",
  157. xendev->key, subkey, num, strerror ( rc ) );
  158. return rc;
  159. }
  160. return 0;
  161. }
  162. /**
  163. * Write XenStore flag value
  164. *
  165. * @v netfront Netfront device
  166. * @v subkey Subkey
  167. * @v num Numeric value
  168. * @ret rc Return status code
  169. */
  170. static int netfront_write_flag ( struct netfront_nic *netfront,
  171. const char *subkey ) {
  172. return netfront_write_num ( netfront, subkey, 1 );
  173. }
  174. /**
  175. * Delete XenStore value
  176. *
  177. * @v netfront Netfront device
  178. * @v subkey Subkey
  179. * @ret rc Return status code
  180. */
  181. static int netfront_rm ( struct netfront_nic *netfront, const char *subkey ) {
  182. struct xen_device *xendev = netfront->xendev;
  183. struct xen_hypervisor *xen = xendev->xen;
  184. int rc;
  185. /* Remove value */
  186. if ( ( rc = xenstore_rm ( xen, xendev->key, subkey, NULL ) ) != 0 ) {
  187. DBGC ( netfront, "NETFRONT %s could not delete %s: %s\n",
  188. xendev->key, subkey, strerror ( rc ) );
  189. return rc;
  190. }
  191. return 0;
  192. }
  193. /******************************************************************************
  194. *
  195. * Events
  196. *
  197. ******************************************************************************
  198. */
  199. /**
  200. * Create event channel
  201. *
  202. * @v netfront Netfront device
  203. * @ret rc Return status code
  204. */
  205. static int netfront_create_event ( struct netfront_nic *netfront ) {
  206. struct xen_device *xendev = netfront->xendev;
  207. struct xen_hypervisor *xen = xendev->xen;
  208. struct evtchn_alloc_unbound alloc_unbound;
  209. struct evtchn_close close;
  210. int xenrc;
  211. int rc;
  212. /* Allocate event */
  213. alloc_unbound.dom = DOMID_SELF;
  214. alloc_unbound.remote_dom = xendev->backend_id;
  215. if ( ( xenrc = xenevent_alloc_unbound ( xen, &alloc_unbound ) ) != 0 ) {
  216. rc = -EXEN ( xenrc );
  217. DBGC ( netfront, "NETFRONT %s could not allocate event: %s\n",
  218. xendev->key, strerror ( rc ) );
  219. goto err_alloc_unbound;
  220. }
  221. netfront->event.port = alloc_unbound.port;
  222. /* Publish event channel */
  223. if ( ( rc = netfront_write_num ( netfront, "event-channel",
  224. netfront->event.port ) ) != 0 )
  225. goto err_write_num;
  226. DBGC ( netfront, "NETFRONT %s event-channel=\"%d\"\n",
  227. xendev->key, netfront->event.port );
  228. return 0;
  229. netfront_rm ( netfront, "event-channel" );
  230. err_write_num:
  231. close.port = netfront->event.port;
  232. xenevent_close ( xen, &close );
  233. err_alloc_unbound:
  234. return rc;
  235. }
  236. /**
  237. * Send event
  238. *
  239. * @v netfront Netfront device
  240. * @ret rc Return status code
  241. */
  242. static inline __attribute__ (( always_inline )) int
  243. netfront_send_event ( struct netfront_nic *netfront ) {
  244. struct xen_device *xendev = netfront->xendev;
  245. struct xen_hypervisor *xen = xendev->xen;
  246. int xenrc;
  247. int rc;
  248. /* Send event */
  249. if ( ( xenrc = xenevent_send ( xen, &netfront->event ) ) != 0 ) {
  250. rc = -EXEN ( xenrc );
  251. DBGC ( netfront, "NETFRONT %s could not send event: %s\n",
  252. xendev->key, strerror ( rc ) );
  253. return rc;
  254. }
  255. return 0;
  256. }
  257. /**
  258. * Destroy event channel
  259. *
  260. * @v netfront Netfront device
  261. */
  262. static void netfront_destroy_event ( struct netfront_nic *netfront ) {
  263. struct xen_device *xendev = netfront->xendev;
  264. struct xen_hypervisor *xen = xendev->xen;
  265. struct evtchn_close close;
  266. /* Unpublish event channel */
  267. netfront_rm ( netfront, "event-channel" );
  268. /* Close event channel */
  269. close.port = netfront->event.port;
  270. xenevent_close ( xen, &close );
  271. }
  272. /******************************************************************************
  273. *
  274. * Descriptor rings
  275. *
  276. ******************************************************************************
  277. */
  278. /**
  279. * Create descriptor ring
  280. *
  281. * @v netfront Netfront device
  282. * @v ring Descriptor ring
  283. * @ret rc Return status code
  284. */
  285. static int netfront_create_ring ( struct netfront_nic *netfront,
  286. struct netfront_ring *ring ) {
  287. struct xen_device *xendev = netfront->xendev;
  288. struct xen_hypervisor *xen = xendev->xen;
  289. unsigned int i;
  290. int rc;
  291. /* Initialise buffer ID ring */
  292. for ( i = 0 ; i < ring->count ; i++ ) {
  293. ring->ids[i] = i;
  294. assert ( ring->iobufs[i] == NULL );
  295. }
  296. ring->id_prod = 0;
  297. ring->id_cons = 0;
  298. /* Allocate and initialise shared ring */
  299. ring->sring.raw = malloc_dma ( PAGE_SIZE, PAGE_SIZE );
  300. if ( ! ring->sring.raw ) {
  301. rc = -ENOMEM;
  302. goto err_alloc;
  303. }
  304. /* Grant access to shared ring */
  305. if ( ( rc = xengrant_permit_access ( xen, ring->ref, xendev->backend_id,
  306. 0, ring->sring.raw ) ) != 0 ) {
  307. DBGC ( netfront, "NETFRONT %s could not permit access to "
  308. "%#08lx: %s\n", xendev->key,
  309. virt_to_phys ( ring->sring.raw ), strerror ( rc ) );
  310. goto err_permit_access;
  311. }
  312. /* Publish shared ring reference */
  313. if ( ( rc = netfront_write_num ( netfront, ring->ref_key,
  314. ring->ref ) ) != 0 )
  315. goto err_write_num;
  316. DBGC ( netfront, "NETFRONT %s %s=\"%d\" [%08lx,%08lx)\n",
  317. xendev->key, ring->ref_key, ring->ref,
  318. virt_to_phys ( ring->sring.raw ),
  319. ( virt_to_phys ( ring->sring.raw ) + PAGE_SIZE ) );
  320. return 0;
  321. netfront_rm ( netfront, ring->ref_key );
  322. err_write_num:
  323. xengrant_invalidate ( xen, ring->ref );
  324. err_permit_access:
  325. free_dma ( ring->sring.raw, PAGE_SIZE );
  326. err_alloc:
  327. return rc;
  328. }
  329. /**
  330. * Add buffer to descriptor ring
  331. *
  332. * @v netfront Netfront device
  333. * @v ring Descriptor ring
  334. * @v iobuf I/O buffer
  335. * @v id Buffer ID to fill in
  336. * @v ref Grant reference to fill in
  337. * @ret rc Return status code
  338. *
  339. * The caller is responsible for ensuring that there is space in the
  340. * ring.
  341. */
  342. static int netfront_push ( struct netfront_nic *netfront,
  343. struct netfront_ring *ring, struct io_buffer *iobuf,
  344. uint16_t *id, grant_ref_t *ref ) {
  345. struct xen_device *xendev = netfront->xendev;
  346. struct xen_hypervisor *xen = xendev->xen;
  347. unsigned int next_id;
  348. unsigned int next_ref;
  349. int rc;
  350. /* Sanity check */
  351. assert ( ! netfront_ring_is_full ( ring ) );
  352. /* Allocate buffer ID */
  353. next_id = ring->ids[ ring->id_prod & ( ring->count - 1 ) ];
  354. next_ref = ring->refs[next_id];
  355. /* Grant access to I/O buffer page. I/O buffers are naturally
  356. * aligned, so we never need to worry about crossing a page
  357. * boundary.
  358. */
  359. if ( ( rc = xengrant_permit_access ( xen, next_ref, xendev->backend_id,
  360. 0, iobuf->data ) ) != 0 ) {
  361. DBGC ( netfront, "NETFRONT %s could not permit access to "
  362. "%#08lx: %s\n", xendev->key,
  363. virt_to_phys ( iobuf->data ), strerror ( rc ) );
  364. return rc;
  365. }
  366. /* Store I/O buffer */
  367. assert ( ring->iobufs[next_id] == NULL );
  368. ring->iobufs[next_id] = iobuf;
  369. /* Consume buffer ID */
  370. ring->id_prod++;
  371. /* Return buffer ID and grant reference */
  372. *id = next_id;
  373. *ref = next_ref;
  374. return 0;
  375. }
  376. /**
  377. * Remove buffer from descriptor ring
  378. *
  379. * @v netfront Netfront device
  380. * @v ring Descriptor ring
  381. * @v id Buffer ID
  382. * @ret iobuf I/O buffer
  383. */
  384. static struct io_buffer * netfront_pull ( struct netfront_nic *netfront,
  385. struct netfront_ring *ring,
  386. unsigned int id ) {
  387. struct xen_device *xendev = netfront->xendev;
  388. struct xen_hypervisor *xen = xendev->xen;
  389. struct io_buffer *iobuf;
  390. /* Sanity check */
  391. assert ( id < ring->count );
  392. /* Revoke access from I/O buffer page */
  393. xengrant_invalidate ( xen, ring->refs[id] );
  394. /* Retrieve I/O buffer */
  395. iobuf = ring->iobufs[id];
  396. assert ( iobuf != NULL );
  397. ring->iobufs[id] = NULL;
  398. /* Free buffer ID */
  399. ring->ids[ ( ring->id_cons++ ) & ( ring->count - 1 ) ] = id;
  400. return iobuf;
  401. }
  402. /**
  403. * Destroy descriptor ring
  404. *
  405. * @v netfront Netfront device
  406. * @v ring Descriptor ring
  407. * @v discard Method used to discard outstanding buffer, or NULL
  408. */
  409. static void netfront_destroy_ring ( struct netfront_nic *netfront,
  410. struct netfront_ring *ring,
  411. void ( * discard ) ( struct io_buffer * ) ){
  412. struct xen_device *xendev = netfront->xendev;
  413. struct xen_hypervisor *xen = xendev->xen;
  414. struct io_buffer *iobuf;
  415. unsigned int id;
  416. /* Flush any outstanding buffers */
  417. while ( ! netfront_ring_is_empty ( ring ) ) {
  418. id = ring->ids[ ring->id_cons & ( ring->count - 1 ) ];
  419. iobuf = netfront_pull ( netfront, ring, id );
  420. if ( discard )
  421. discard ( iobuf );
  422. }
  423. /* Unpublish shared ring reference */
  424. netfront_rm ( netfront, ring->ref_key );
  425. /* Revoke access from shared ring */
  426. xengrant_invalidate ( xen, ring->ref );
  427. /* Free page */
  428. free_dma ( ring->sring.raw, PAGE_SIZE );
  429. ring->sring.raw = NULL;
  430. }
  431. /******************************************************************************
  432. *
  433. * Network device interface
  434. *
  435. ******************************************************************************
  436. */
  437. /**
  438. * Refill receive descriptor ring
  439. *
  440. * @v netdev Network device
  441. */
  442. static void netfront_refill_rx ( struct net_device *netdev ) {
  443. struct netfront_nic *netfront = netdev->priv;
  444. struct xen_device *xendev = netfront->xendev;
  445. struct io_buffer *iobuf;
  446. struct netif_rx_request *request;
  447. unsigned int refilled = 0;
  448. int notify;
  449. int rc;
  450. /* Refill ring */
  451. while ( netfront_ring_fill ( &netfront->rx ) < NETFRONT_RX_FILL ) {
  452. /* Allocate I/O buffer */
  453. iobuf = alloc_iob ( PAGE_SIZE );
  454. if ( ! iobuf ) {
  455. /* Wait for next refill */
  456. break;
  457. }
  458. /* Add to descriptor ring */
  459. request = RING_GET_REQUEST ( &netfront->rx_fring,
  460. netfront->rx_fring.req_prod_pvt );
  461. if ( ( rc = netfront_push ( netfront, &netfront->rx,
  462. iobuf, &request->id,
  463. &request->gref ) ) != 0 ) {
  464. netdev_rx_err ( netdev, iobuf, rc );
  465. break;
  466. }
  467. DBGC2 ( netfront, "NETFRONT %s RX id %d ref %d is %#08lx+%zx\n",
  468. xendev->key, request->id, request->gref,
  469. virt_to_phys ( iobuf->data ), iob_tailroom ( iobuf ) );
  470. /* Move to next descriptor */
  471. netfront->rx_fring.req_prod_pvt++;
  472. refilled++;
  473. }
  474. /* Push new descriptors and notify backend if applicable */
  475. if ( refilled ) {
  476. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->rx_fring,
  477. notify );
  478. if ( notify )
  479. netfront_send_event ( netfront );
  480. }
  481. }
  482. /**
  483. * Open network device
  484. *
  485. * @v netdev Network device
  486. * @ret rc Return status code
  487. */
  488. static int netfront_open ( struct net_device *netdev ) {
  489. struct netfront_nic *netfront = netdev->priv;
  490. struct xen_device *xendev = netfront->xendev;
  491. int rc;
  492. /* Ensure device is in a suitable initial state */
  493. if ( ( rc = netfront_reset ( netfront ) ) != 0 )
  494. goto err_reset;
  495. /* Create transmit descriptor ring */
  496. if ( ( rc = netfront_create_ring ( netfront, &netfront->tx ) ) != 0 )
  497. goto err_create_tx;
  498. SHARED_RING_INIT ( netfront->tx_sring );
  499. FRONT_RING_INIT ( &netfront->tx_fring, netfront->tx_sring, PAGE_SIZE );
  500. assert ( RING_SIZE ( &netfront->tx_fring ) >= netfront->tx.count );
  501. /* Create receive descriptor ring */
  502. if ( ( rc = netfront_create_ring ( netfront, &netfront->rx ) ) != 0 )
  503. goto err_create_rx;
  504. SHARED_RING_INIT ( netfront->rx_sring );
  505. FRONT_RING_INIT ( &netfront->rx_fring, netfront->rx_sring, PAGE_SIZE );
  506. assert ( RING_SIZE ( &netfront->rx_fring ) >= netfront->rx.count );
  507. /* Create event channel */
  508. if ( ( rc = netfront_create_event ( netfront ) ) != 0 )
  509. goto err_create_event;
  510. /* "Request" the rx-copy feature. Current versions of
  511. * xen_netback.ko will fail silently if this parameter is not
  512. * present.
  513. */
  514. if ( ( rc = netfront_write_flag ( netfront, "request-rx-copy" ) ) != 0 )
  515. goto err_request_rx_copy;
  516. /* Disable checksum offload, since we will always do the work anyway */
  517. if ( ( rc = netfront_write_flag ( netfront,
  518. "feature-no-csum-offload" ) ) != 0 )
  519. goto err_feature_no_csum_offload;
  520. /* Inform backend that we will send notifications for RX requests */
  521. if ( ( rc = netfront_write_flag ( netfront,
  522. "feature-rx-notify" ) ) != 0 )
  523. goto err_feature_rx_notify;
  524. /* Set state to Connected */
  525. if ( ( rc = xenbus_set_state ( xendev, XenbusStateConnected ) ) != 0 ) {
  526. DBGC ( netfront, "NETFRONT %s could not set state=\"%d\": %s\n",
  527. xendev->key, XenbusStateConnected, strerror ( rc ) );
  528. goto err_set_state;
  529. }
  530. /* Wait for backend to connect */
  531. if ( ( rc = xenbus_backend_wait ( xendev, XenbusStateConnected ) ) !=0){
  532. DBGC ( netfront, "NETFRONT %s could not connect to backend: "
  533. "%s\n", xendev->key, strerror ( rc ) );
  534. goto err_backend_wait;
  535. }
  536. /* Refill receive descriptor ring */
  537. netfront_refill_rx ( netdev );
  538. /* Set link up */
  539. netdev_link_up ( netdev );
  540. return 0;
  541. err_backend_wait:
  542. netfront_reset ( netfront );
  543. err_set_state:
  544. netfront_rm ( netfront, "feature-rx-notify" );
  545. err_feature_rx_notify:
  546. netfront_rm ( netfront, "feature-no-csum-offload" );
  547. err_feature_no_csum_offload:
  548. netfront_rm ( netfront, "request-rx-copy" );
  549. err_request_rx_copy:
  550. netfront_destroy_event ( netfront );
  551. err_create_event:
  552. netfront_destroy_ring ( netfront, &netfront->rx, NULL );
  553. err_create_rx:
  554. netfront_destroy_ring ( netfront, &netfront->tx, NULL );
  555. err_create_tx:
  556. err_reset:
  557. return rc;
  558. }
  559. /**
  560. * Close network device
  561. *
  562. * @v netdev Network device
  563. */
  564. static void netfront_close ( struct net_device *netdev ) {
  565. struct netfront_nic *netfront = netdev->priv;
  566. struct xen_device *xendev = netfront->xendev;
  567. int rc;
  568. /* Reset devic, thereby ensuring that grant references are no
  569. * longer in use, etc.
  570. */
  571. if ( ( rc = netfront_reset ( netfront ) ) != 0 ) {
  572. DBGC ( netfront, "NETFRONT %s could not disconnect from "
  573. "backend: %s\n", xendev->key, strerror ( rc ) );
  574. /* Things will probably go _very_ badly wrong if this
  575. * happens, since it means the backend may still write
  576. * to the outstanding RX buffers that we are about to
  577. * free. The best we can do is report the error via
  578. * the link status, but there's a good chance the
  579. * machine will crash soon.
  580. */
  581. netdev_link_err ( netdev, rc );
  582. } else {
  583. netdev_link_down ( netdev );
  584. }
  585. /* Delete flags */
  586. netfront_rm ( netfront, "feature-rx-notify" );
  587. netfront_rm ( netfront, "feature-no-csum-offload" );
  588. netfront_rm ( netfront, "request-rx-copy" );
  589. /* Destroy event channel */
  590. netfront_destroy_event ( netfront );
  591. /* Destroy receive descriptor ring, freeing any outstanding
  592. * I/O buffers.
  593. */
  594. netfront_destroy_ring ( netfront, &netfront->rx, free_iob );
  595. /* Destroy transmit descriptor ring. Leave any outstanding
  596. * I/O buffers to be freed by netdev_tx_flush().
  597. */
  598. netfront_destroy_ring ( netfront, &netfront->tx, NULL );
  599. }
  600. /**
  601. * Transmit packet
  602. *
  603. * @v netdev Network device
  604. * @v iobuf I/O buffer
  605. * @ret rc Return status code
  606. */
  607. static int netfront_transmit ( struct net_device *netdev,
  608. struct io_buffer *iobuf ) {
  609. struct netfront_nic *netfront = netdev->priv;
  610. struct xen_device *xendev = netfront->xendev;
  611. struct netif_tx_request *request;
  612. int notify;
  613. int rc;
  614. /* Check that we have space in the ring */
  615. if ( netfront_ring_is_full ( &netfront->tx ) ) {
  616. DBGC ( netfront, "NETFRONT %s out of transmit descriptors\n",
  617. xendev->key );
  618. return -ENOBUFS;
  619. }
  620. /* Add to descriptor ring */
  621. request = RING_GET_REQUEST ( &netfront->tx_fring,
  622. netfront->tx_fring.req_prod_pvt );
  623. if ( ( rc = netfront_push ( netfront, &netfront->tx, iobuf,
  624. &request->id, &request->gref ) ) != 0 ) {
  625. return rc;
  626. }
  627. request->offset = ( virt_to_phys ( iobuf->data ) & ( PAGE_SIZE - 1 ) );
  628. request->flags = NETTXF_data_validated;
  629. request->size = iob_len ( iobuf );
  630. DBGC2 ( netfront, "NETFRONT %s TX id %d ref %d is %#08lx+%zx\n",
  631. xendev->key, request->id, request->gref,
  632. virt_to_phys ( iobuf->data ), iob_len ( iobuf ) );
  633. /* Consume descriptor */
  634. netfront->tx_fring.req_prod_pvt++;
  635. /* Push new descriptor and notify backend if applicable */
  636. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->tx_fring, notify );
  637. if ( notify )
  638. netfront_send_event ( netfront );
  639. return 0;
  640. }
  641. /**
  642. * Poll for completed packets
  643. *
  644. * @v netdev Network device
  645. */
  646. static void netfront_poll_tx ( struct net_device *netdev ) {
  647. struct netfront_nic *netfront = netdev->priv;
  648. struct xen_device *xendev = netfront->xendev;
  649. struct netif_tx_response *response;
  650. struct io_buffer *iobuf;
  651. unsigned int status;
  652. int rc;
  653. /* Consume any unconsumed responses */
  654. while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->tx_fring ) ) {
  655. /* Get next response */
  656. response = RING_GET_RESPONSE ( &netfront->tx_fring,
  657. netfront->tx_fring.rsp_cons++ );
  658. /* Retrieve from descriptor ring */
  659. iobuf = netfront_pull ( netfront, &netfront->tx, response->id );
  660. status = response->status;
  661. if ( status == NETIF_RSP_OKAY ) {
  662. DBGC2 ( netfront, "NETFRONT %s TX id %d complete\n",
  663. xendev->key, response->id );
  664. netdev_tx_complete ( netdev, iobuf );
  665. } else {
  666. rc = -EIO_NETIF_RSP ( status );
  667. DBGC2 ( netfront, "NETFRONT %s TX id %d error %d: %s\n",
  668. xendev->key, response->id, status,
  669. strerror ( rc ) );
  670. netdev_tx_complete_err ( netdev, iobuf, rc );
  671. }
  672. }
  673. }
  674. /**
  675. * Poll for received packets
  676. *
  677. * @v netdev Network device
  678. */
  679. static void netfront_poll_rx ( struct net_device *netdev ) {
  680. struct netfront_nic *netfront = netdev->priv;
  681. struct xen_device *xendev = netfront->xendev;
  682. struct netif_rx_response *response;
  683. struct io_buffer *iobuf;
  684. int status;
  685. size_t len;
  686. int rc;
  687. /* Consume any unconsumed responses */
  688. while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->rx_fring ) ) {
  689. /* Get next response */
  690. response = RING_GET_RESPONSE ( &netfront->rx_fring,
  691. netfront->rx_fring.rsp_cons++ );
  692. /* Retrieve from descriptor ring */
  693. iobuf = netfront_pull ( netfront, &netfront->rx, response->id );
  694. status = response->status;
  695. if ( status >= 0 ) {
  696. len = status;
  697. iob_reserve ( iobuf, response->offset );
  698. iob_put ( iobuf, len );
  699. DBGC2 ( netfront, "NETFRONT %s RX id %d complete "
  700. "%#08lx+%zx\n", xendev->key, response->id,
  701. virt_to_phys ( iobuf->data ), len );
  702. netdev_rx ( netdev, iobuf );
  703. } else {
  704. rc = -EIO_NETIF_RSP ( status );
  705. DBGC2 ( netfront, "NETFRONT %s RX id %d error %d: %s\n",
  706. xendev->key, response->id, status,
  707. strerror ( rc ) );
  708. netdev_rx_err ( netdev, iobuf, rc );
  709. }
  710. }
  711. }
  712. /**
  713. * Poll for completed and received packets
  714. *
  715. * @v netdev Network device
  716. */
  717. static void netfront_poll ( struct net_device *netdev ) {
  718. /* Poll for TX completions */
  719. netfront_poll_tx ( netdev );
  720. /* Poll for RX completions */
  721. netfront_poll_rx ( netdev );
  722. /* Refill RX descriptor ring */
  723. netfront_refill_rx ( netdev );
  724. }
  725. /** Network device operations */
  726. static struct net_device_operations netfront_operations = {
  727. .open = netfront_open,
  728. .close = netfront_close,
  729. .transmit = netfront_transmit,
  730. .poll = netfront_poll,
  731. };
  732. /******************************************************************************
  733. *
  734. * Xen device bus interface
  735. *
  736. ******************************************************************************
  737. */
  738. /**
  739. * Probe Xen device
  740. *
  741. * @v xendev Xen device
  742. * @ret rc Return status code
  743. */
  744. static int netfront_probe ( struct xen_device *xendev ) {
  745. struct xen_hypervisor *xen = xendev->xen;
  746. struct net_device *netdev;
  747. struct netfront_nic *netfront;
  748. int rc;
  749. /* Allocate and initialise structure */
  750. netdev = alloc_etherdev ( sizeof ( *netfront ) );
  751. if ( ! netdev ) {
  752. rc = -ENOMEM;
  753. goto err_alloc;
  754. }
  755. netdev_init ( netdev, &netfront_operations );
  756. netdev->dev = &xendev->dev;
  757. netfront = netdev->priv;
  758. netfront->xendev = xendev;
  759. DBGC ( netfront, "NETFRONT %s backend=\"%s\" in domain %ld\n",
  760. xendev->key, xendev->backend, xendev->backend_id );
  761. /* Allocate grant references and initialise descriptor rings */
  762. if ( ( rc = xengrant_alloc ( xen, netfront->refs,
  763. NETFRONT_REF_COUNT ) ) != 0 ) {
  764. DBGC ( netfront, "NETFRONT %s could not allocate grant "
  765. "references: %s\n", xendev->key, strerror ( rc ) );
  766. goto err_grant_alloc;
  767. }
  768. netfront_init_ring ( &netfront->tx, "tx-ring-ref",
  769. netfront->refs[NETFRONT_REF_TX_RING],
  770. NETFRONT_NUM_TX_DESC, netfront->tx_iobufs,
  771. &netfront->refs[NETFRONT_REF_TX_BASE],
  772. netfront->tx_ids );
  773. netfront_init_ring ( &netfront->rx, "rx-ring-ref",
  774. netfront->refs[NETFRONT_REF_RX_RING],
  775. NETFRONT_NUM_RX_DESC, netfront->rx_iobufs,
  776. &netfront->refs[NETFRONT_REF_RX_BASE],
  777. netfront->rx_ids );
  778. /* Fetch MAC address */
  779. if ( ( rc = netfront_read_mac ( netfront, netdev->hw_addr ) ) != 0 )
  780. goto err_read_mac;
  781. /* Reset device. Ignore failures; allow the device to be
  782. * registered so that reset errors can be observed by the user
  783. * when attempting to open the device.
  784. */
  785. netfront_reset ( netfront );
  786. /* Register network device */
  787. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  788. goto err_register_netdev;
  789. /* Set initial link state */
  790. netdev_link_down ( netdev );
  791. xen_set_drvdata ( xendev, netdev );
  792. return 0;
  793. unregister_netdev ( netdev );
  794. err_register_netdev:
  795. err_read_mac:
  796. xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
  797. err_grant_alloc:
  798. netdev_nullify ( netdev );
  799. netdev_put ( netdev );
  800. err_alloc:
  801. return rc;
  802. }
  803. /**
  804. * Remove Xen device
  805. *
  806. * @v xendev Xen device
  807. */
  808. static void netfront_remove ( struct xen_device *xendev ) {
  809. struct net_device *netdev = xen_get_drvdata ( xendev );
  810. struct netfront_nic *netfront = netdev->priv;
  811. struct xen_hypervisor *xen = xendev->xen;
  812. /* Unregister network device */
  813. unregister_netdev ( netdev );
  814. /* Free resources */
  815. xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
  816. /* Free network device */
  817. netdev_nullify ( netdev );
  818. netdev_put ( netdev );
  819. }
  820. /** Xen netfront driver */
  821. struct xen_driver netfront_driver __xen_driver = {
  822. .name = "netfront",
  823. .type = "vif",
  824. .probe = netfront_probe,
  825. .remove = netfront_remove,
  826. };