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

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