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