You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netfront.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. int notify;
  448. int rc;
  449. /* Do nothing if ring is already full */
  450. if ( netfront_ring_is_full ( &netfront->rx ) )
  451. return;
  452. /* Refill ring */
  453. do {
  454. /* Allocate I/O buffer */
  455. iobuf = alloc_iob ( PAGE_SIZE );
  456. if ( ! iobuf ) {
  457. /* Wait for next refill */
  458. break;
  459. }
  460. /* Add to descriptor ring */
  461. request = RING_GET_REQUEST ( &netfront->rx_fring,
  462. netfront->rx_fring.req_prod_pvt );
  463. if ( ( rc = netfront_push ( netfront, &netfront->rx,
  464. iobuf, &request->id,
  465. &request->gref ) ) != 0 ) {
  466. netdev_rx_err ( netdev, iobuf, rc );
  467. break;
  468. }
  469. DBGC2 ( netfront, "NETFRONT %s RX id %d ref %d is %#08lx+%zx\n",
  470. xendev->key, request->id, request->gref,
  471. virt_to_phys ( iobuf->data ), iob_tailroom ( iobuf ) );
  472. /* Move to next descriptor */
  473. netfront->rx_fring.req_prod_pvt++;
  474. } while ( ! netfront_ring_is_full ( &netfront->rx ) );
  475. /* Push new descriptors and notify backend if applicable */
  476. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->rx_fring, notify );
  477. if ( notify )
  478. netfront_send_event ( netfront );
  479. }
  480. /**
  481. * Open network device
  482. *
  483. * @v netdev Network device
  484. * @ret rc Return status code
  485. */
  486. static int netfront_open ( struct net_device *netdev ) {
  487. struct netfront_nic *netfront = netdev->priv;
  488. struct xen_device *xendev = netfront->xendev;
  489. int rc;
  490. /* Ensure device is in a suitable initial state */
  491. if ( ( rc = netfront_reset ( netfront ) ) != 0 )
  492. goto err_reset;
  493. /* Create transmit descriptor ring */
  494. if ( ( rc = netfront_create_ring ( netfront, &netfront->tx ) ) != 0 )
  495. goto err_create_tx;
  496. SHARED_RING_INIT ( netfront->tx_sring );
  497. FRONT_RING_INIT ( &netfront->tx_fring, netfront->tx_sring, PAGE_SIZE );
  498. assert ( RING_SIZE ( &netfront->tx_fring ) >= netfront->tx.count );
  499. /* Create receive descriptor ring */
  500. if ( ( rc = netfront_create_ring ( netfront, &netfront->rx ) ) != 0 )
  501. goto err_create_rx;
  502. SHARED_RING_INIT ( netfront->rx_sring );
  503. FRONT_RING_INIT ( &netfront->rx_fring, netfront->rx_sring, PAGE_SIZE );
  504. assert ( RING_SIZE ( &netfront->rx_fring ) >= netfront->rx.count );
  505. /* Create event channel */
  506. if ( ( rc = netfront_create_event ( netfront ) ) != 0 )
  507. goto err_create_event;
  508. /* "Request" the rx-copy feature. Current versions of
  509. * xen_netback.ko will fail silently if this parameter is not
  510. * present.
  511. */
  512. if ( ( rc = netfront_write_flag ( netfront, "request-rx-copy" ) ) != 0 )
  513. goto err_request_rx_copy;
  514. /* Disable checksum offload, since we will always do the work anyway */
  515. if ( ( rc = netfront_write_flag ( netfront,
  516. "feature-no-csum-offload" ) ) != 0 )
  517. goto err_feature_no_csum_offload;
  518. /* Inform backend that we will send notifications for RX requests */
  519. if ( ( rc = netfront_write_flag ( netfront,
  520. "feature-rx-notify" ) ) != 0 )
  521. goto err_feature_rx_notify;
  522. /* Set state to Connected */
  523. if ( ( rc = xenbus_set_state ( xendev, XenbusStateConnected ) ) != 0 ) {
  524. DBGC ( netfront, "NETFRONT %s could not set state=\"%d\": %s\n",
  525. xendev->key, XenbusStateConnected, strerror ( rc ) );
  526. goto err_set_state;
  527. }
  528. /* Wait for backend to connect */
  529. if ( ( rc = xenbus_backend_wait ( xendev, XenbusStateConnected ) ) !=0){
  530. DBGC ( netfront, "NETFRONT %s could not connect to backend: "
  531. "%s\n", xendev->key, strerror ( rc ) );
  532. goto err_backend_wait;
  533. }
  534. /* Refill receive descriptor ring */
  535. netfront_refill_rx ( netdev );
  536. /* Set link up */
  537. netdev_link_up ( netdev );
  538. return 0;
  539. err_backend_wait:
  540. netfront_reset ( netfront );
  541. err_set_state:
  542. netfront_rm ( netfront, "feature-rx-notify" );
  543. err_feature_rx_notify:
  544. netfront_rm ( netfront, "feature-no-csum-offload" );
  545. err_feature_no_csum_offload:
  546. netfront_rm ( netfront, "request-rx-copy" );
  547. err_request_rx_copy:
  548. netfront_destroy_event ( netfront );
  549. err_create_event:
  550. netfront_destroy_ring ( netfront, &netfront->rx, NULL );
  551. err_create_rx:
  552. netfront_destroy_ring ( netfront, &netfront->tx, NULL );
  553. err_create_tx:
  554. err_reset:
  555. return rc;
  556. }
  557. /**
  558. * Close network device
  559. *
  560. * @v netdev Network device
  561. */
  562. static void netfront_close ( struct net_device *netdev ) {
  563. struct netfront_nic *netfront = netdev->priv;
  564. struct xen_device *xendev = netfront->xendev;
  565. int rc;
  566. /* Reset devic, thereby ensuring that grant references are no
  567. * longer in use, etc.
  568. */
  569. if ( ( rc = netfront_reset ( netfront ) ) != 0 ) {
  570. DBGC ( netfront, "NETFRONT %s could not disconnect from "
  571. "backend: %s\n", xendev->key, strerror ( rc ) );
  572. /* Things will probably go _very_ badly wrong if this
  573. * happens, since it means the backend may still write
  574. * to the outstanding RX buffers that we are about to
  575. * free. The best we can do is report the error via
  576. * the link status, but there's a good chance the
  577. * machine will crash soon.
  578. */
  579. netdev_link_err ( netdev, rc );
  580. } else {
  581. netdev_link_down ( netdev );
  582. }
  583. /* Delete flags */
  584. netfront_rm ( netfront, "feature-rx-notify" );
  585. netfront_rm ( netfront, "feature-no-csum-offload" );
  586. netfront_rm ( netfront, "request-rx-copy" );
  587. /* Destroy event channel */
  588. netfront_destroy_event ( netfront );
  589. /* Destroy receive descriptor ring, freeing any outstanding
  590. * I/O buffers.
  591. */
  592. netfront_destroy_ring ( netfront, &netfront->rx, free_iob );
  593. /* Destroy transmit descriptor ring. Leave any outstanding
  594. * I/O buffers to be freed by netdev_tx_flush().
  595. */
  596. netfront_destroy_ring ( netfront, &netfront->tx, NULL );
  597. }
  598. /**
  599. * Transmit packet
  600. *
  601. * @v netdev Network device
  602. * @v iobuf I/O buffer
  603. * @ret rc Return status code
  604. */
  605. static int netfront_transmit ( struct net_device *netdev,
  606. struct io_buffer *iobuf ) {
  607. struct netfront_nic *netfront = netdev->priv;
  608. struct xen_device *xendev = netfront->xendev;
  609. struct netif_tx_request *request;
  610. int notify;
  611. int rc;
  612. /* Check that we have space in the ring */
  613. if ( netfront_ring_is_full ( &netfront->tx ) ) {
  614. DBGC ( netfront, "NETFRONT %s out of transmit descriptors\n",
  615. xendev->key );
  616. return -ENOBUFS;
  617. }
  618. /* Add to descriptor ring */
  619. request = RING_GET_REQUEST ( &netfront->tx_fring,
  620. netfront->tx_fring.req_prod_pvt );
  621. if ( ( rc = netfront_push ( netfront, &netfront->tx, iobuf,
  622. &request->id, &request->gref ) ) != 0 ) {
  623. return rc;
  624. }
  625. request->offset = ( virt_to_phys ( iobuf->data ) & ( PAGE_SIZE - 1 ) );
  626. request->flags = NETTXF_data_validated;
  627. request->size = iob_len ( iobuf );
  628. DBGC2 ( netfront, "NETFRONT %s TX id %d ref %d is %#08lx+%zx\n",
  629. xendev->key, request->id, request->gref,
  630. virt_to_phys ( iobuf->data ), iob_len ( iobuf ) );
  631. /* Consume descriptor */
  632. netfront->tx_fring.req_prod_pvt++;
  633. /* Push new descriptor and notify backend if applicable */
  634. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->tx_fring, notify );
  635. if ( notify )
  636. netfront_send_event ( netfront );
  637. return 0;
  638. }
  639. /**
  640. * Poll for completed packets
  641. *
  642. * @v netdev Network device
  643. */
  644. static void netfront_poll_tx ( struct net_device *netdev ) {
  645. struct netfront_nic *netfront = netdev->priv;
  646. struct xen_device *xendev = netfront->xendev;
  647. struct netif_tx_response *response;
  648. struct io_buffer *iobuf;
  649. unsigned int status;
  650. int rc;
  651. /* Consume any unconsumed responses */
  652. while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->tx_fring ) ) {
  653. /* Get next response */
  654. response = RING_GET_RESPONSE ( &netfront->tx_fring,
  655. netfront->tx_fring.rsp_cons++ );
  656. /* Retrieve from descriptor ring */
  657. iobuf = netfront_pull ( netfront, &netfront->tx, response->id );
  658. status = response->status;
  659. if ( status == NETIF_RSP_OKAY ) {
  660. DBGC2 ( netfront, "NETFRONT %s TX id %d complete\n",
  661. xendev->key, response->id );
  662. netdev_tx_complete ( netdev, iobuf );
  663. } else {
  664. rc = -EIO_NETIF_RSP ( status );
  665. DBGC2 ( netfront, "NETFRONT %s TX id %d error %d: %s\n",
  666. xendev->key, response->id, status,
  667. strerror ( rc ) );
  668. netdev_tx_complete_err ( netdev, iobuf, rc );
  669. }
  670. }
  671. }
  672. /**
  673. * Poll for received packets
  674. *
  675. * @v netdev Network device
  676. */
  677. static void netfront_poll_rx ( struct net_device *netdev ) {
  678. struct netfront_nic *netfront = netdev->priv;
  679. struct xen_device *xendev = netfront->xendev;
  680. struct netif_rx_response *response;
  681. struct io_buffer *iobuf;
  682. int status;
  683. size_t len;
  684. int rc;
  685. /* Consume any unconsumed responses */
  686. while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->rx_fring ) ) {
  687. /* Get next response */
  688. response = RING_GET_RESPONSE ( &netfront->rx_fring,
  689. netfront->rx_fring.rsp_cons++ );
  690. /* Retrieve from descriptor ring */
  691. iobuf = netfront_pull ( netfront, &netfront->rx, response->id );
  692. status = response->status;
  693. if ( status >= 0 ) {
  694. len = status;
  695. iob_reserve ( iobuf, response->offset );
  696. iob_put ( iobuf, len );
  697. DBGC2 ( netfront, "NETFRONT %s RX id %d complete "
  698. "%#08lx+%zx\n", xendev->key, response->id,
  699. virt_to_phys ( iobuf->data ), len );
  700. netdev_rx ( netdev, iobuf );
  701. } else {
  702. rc = -EIO_NETIF_RSP ( status );
  703. DBGC2 ( netfront, "NETFRONT %s RX id %d error %d: %s\n",
  704. xendev->key, response->id, status,
  705. strerror ( rc ) );
  706. netdev_rx_err ( netdev, iobuf, rc );
  707. }
  708. }
  709. }
  710. /**
  711. * Poll for completed and received packets
  712. *
  713. * @v netdev Network device
  714. */
  715. static void netfront_poll ( struct net_device *netdev ) {
  716. /* Poll for TX completions */
  717. netfront_poll_tx ( netdev );
  718. /* Poll for RX completions */
  719. netfront_poll_rx ( netdev );
  720. /* Refill RX descriptor ring */
  721. netfront_refill_rx ( netdev );
  722. }
  723. /** Network device operations */
  724. static struct net_device_operations netfront_operations = {
  725. .open = netfront_open,
  726. .close = netfront_close,
  727. .transmit = netfront_transmit,
  728. .poll = netfront_poll,
  729. };
  730. /******************************************************************************
  731. *
  732. * Xen device bus interface
  733. *
  734. ******************************************************************************
  735. */
  736. /**
  737. * Probe Xen device
  738. *
  739. * @v xendev Xen device
  740. * @ret rc Return status code
  741. */
  742. static int netfront_probe ( struct xen_device *xendev ) {
  743. struct xen_hypervisor *xen = xendev->xen;
  744. struct net_device *netdev;
  745. struct netfront_nic *netfront;
  746. int rc;
  747. /* Allocate and initialise structure */
  748. netdev = alloc_etherdev ( sizeof ( *netfront ) );
  749. if ( ! netdev ) {
  750. rc = -ENOMEM;
  751. goto err_alloc;
  752. }
  753. netdev_init ( netdev, &netfront_operations );
  754. netdev->dev = &xendev->dev;
  755. netfront = netdev->priv;
  756. netfront->xendev = xendev;
  757. DBGC ( netfront, "NETFRONT %s backend=\"%s\" in domain %ld\n",
  758. xendev->key, xendev->backend, xendev->backend_id );
  759. /* Allocate grant references and initialise descriptor rings */
  760. if ( ( rc = xengrant_alloc ( xen, netfront->refs,
  761. NETFRONT_REF_COUNT ) ) != 0 ) {
  762. DBGC ( netfront, "NETFRONT %s could not allocate grant "
  763. "references: %s\n", xendev->key, strerror ( rc ) );
  764. goto err_grant_alloc;
  765. }
  766. netfront_init_ring ( &netfront->tx, "tx-ring-ref",
  767. netfront->refs[NETFRONT_REF_TX_RING],
  768. NETFRONT_NUM_TX_DESC, netfront->tx_iobufs,
  769. &netfront->refs[NETFRONT_REF_TX_BASE],
  770. netfront->tx_ids );
  771. netfront_init_ring ( &netfront->rx, "rx-ring-ref",
  772. netfront->refs[NETFRONT_REF_RX_RING],
  773. NETFRONT_NUM_RX_DESC, netfront->rx_iobufs,
  774. &netfront->refs[NETFRONT_REF_RX_BASE],
  775. netfront->rx_ids );
  776. /* Fetch MAC address */
  777. if ( ( rc = netfront_read_mac ( netfront, netdev->hw_addr ) ) != 0 )
  778. goto err_read_mac;
  779. /* Reset device. Ignore failures; allow the device to be
  780. * registered so that reset errors can be observed by the user
  781. * when attempting to open the device.
  782. */
  783. netfront_reset ( netfront );
  784. /* Register network device */
  785. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  786. goto err_register_netdev;
  787. /* Set initial link state */
  788. netdev_link_down ( netdev );
  789. xen_set_drvdata ( xendev, netdev );
  790. return 0;
  791. unregister_netdev ( netdev );
  792. err_register_netdev:
  793. err_read_mac:
  794. xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
  795. err_grant_alloc:
  796. netdev_nullify ( netdev );
  797. netdev_put ( netdev );
  798. err_alloc:
  799. return rc;
  800. }
  801. /**
  802. * Remove Xen device
  803. *
  804. * @v xendev Xen device
  805. */
  806. static void netfront_remove ( struct xen_device *xendev ) {
  807. struct net_device *netdev = xen_get_drvdata ( xendev );
  808. struct netfront_nic *netfront = netdev->priv;
  809. struct xen_hypervisor *xen = xendev->xen;
  810. /* Unregister network device */
  811. unregister_netdev ( netdev );
  812. /* Free resources */
  813. xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
  814. /* Free network device */
  815. netdev_nullify ( netdev );
  816. netdev_put ( netdev );
  817. }
  818. /** Xen netfront driver */
  819. struct xen_driver netfront_driver __xen_driver = {
  820. .name = "netfront",
  821. .type = "vif",
  822. .probe = netfront_probe,
  823. .remove = netfront_remove,
  824. };