iPXE
Macros | Functions | Variables
netfront.c File Reference

Xen netfront driver. More...

#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <ipxe/netdevice.h>
#include <ipxe/ethernet.h>
#include <ipxe/if_ether.h>
#include <ipxe/malloc.h>
#include <ipxe/base16.h>
#include <ipxe/xen.h>
#include <ipxe/xenstore.h>
#include <ipxe/xenbus.h>
#include <ipxe/xengrant.h>
#include <ipxe/xenevent.h>
#include "netfront.h"

Go to the source code of this file.

Macros

#define EIO_NETIF_RSP_ERROR   __einfo_error ( EINFO_EIO_NETIF_RSP_ERROR )
 
#define EINFO_EIO_NETIF_RSP_ERROR
 
#define EIO_NETIF_RSP_DROPPED   __einfo_error ( EINFO_EIO_NETIF_RSP_DROPPED )
 
#define EINFO_EIO_NETIF_RSP_DROPPED
 
#define EIO_NETIF_RSP(status)
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static int netfront_reset (struct netfront_nic *netfront)
 Reset device. More...
 
static int netfront_read_mac (struct netfront_nic *netfront, void *hw_addr)
 Fetch MAC address. More...
 
static int netfront_write_num (struct netfront_nic *netfront, const char *subkey, unsigned long num)
 Write XenStore numeric value. More...
 
static int netfront_write_flag (struct netfront_nic *netfront, const char *subkey)
 Write XenStore flag value. More...
 
static int netfront_rm (struct netfront_nic *netfront, const char *subkey)
 Delete XenStore value. More...
 
static int netfront_create_event (struct netfront_nic *netfront)
 Create event channel. More...
 
static int netfront_send_event (struct netfront_nic *netfront)
 Send event. More...
 
static void netfront_destroy_event (struct netfront_nic *netfront)
 Destroy event channel. More...
 
static int netfront_create_ring (struct netfront_nic *netfront, struct netfront_ring *ring)
 Create descriptor ring. More...
 
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. More...
 
static struct io_buffernetfront_pull (struct netfront_nic *netfront, struct netfront_ring *ring, unsigned int id)
 Remove buffer from descriptor ring. More...
 
static void netfront_destroy_ring (struct netfront_nic *netfront, struct netfront_ring *ring, void(*discard)(struct io_buffer *))
 Destroy descriptor ring. More...
 
static void netfront_refill_rx (struct net_device *netdev)
 Refill receive descriptor ring. More...
 
static int netfront_open (struct net_device *netdev)
 Open network device. More...
 
static void netfront_close (struct net_device *netdev)
 Close network device. More...
 
static int netfront_transmit (struct net_device *netdev, struct io_buffer *iobuf)
 Transmit packet. More...
 
static void netfront_poll_tx (struct net_device *netdev)
 Poll for completed packets. More...
 
static void netfront_poll_rx (struct net_device *netdev)
 Poll for received packets. More...
 
static void netfront_poll (struct net_device *netdev)
 Poll for completed and received packets. More...
 
static int netfront_probe (struct xen_device *xendev)
 Probe Xen device. More...
 
static void netfront_remove (struct xen_device *xendev)
 Remove Xen device. More...
 

Variables

static struct net_device_operations netfront_operations
 Network device operations. More...
 
struct xen_driver netfront_driver __xen_driver
 Xen netfront driver. More...
 

Detailed Description

Xen netfront driver.

Definition in file netfront.c.

Macro Definition Documentation

◆ EIO_NETIF_RSP_ERROR

#define EIO_NETIF_RSP_ERROR   __einfo_error ( EINFO_EIO_NETIF_RSP_ERROR )

Definition at line 48 of file netfront.c.

◆ EINFO_EIO_NETIF_RSP_ERROR

#define EINFO_EIO_NETIF_RSP_ERROR
Value:
"Unspecified network error" )
#define NETIF_RSP_ERROR
Definition: netif.h:292
#define EINFO_EIO
Definition: errno.h:434
#define __einfo_uniqify(einfo_base, uniq, desc)
Declare disambiguated error.
Definition: errno.h:180

Definition at line 50 of file netfront.c.

◆ EIO_NETIF_RSP_DROPPED

#define EIO_NETIF_RSP_DROPPED   __einfo_error ( EINFO_EIO_NETIF_RSP_DROPPED )

Definition at line 53 of file netfront.c.

◆ EINFO_EIO_NETIF_RSP_DROPPED

#define EINFO_EIO_NETIF_RSP_DROPPED
Value:
"Packet dropped" )
#define EINFO_EIO
Definition: errno.h:434
#define NETIF_RSP_DROPPED
Definition: netif.h:291
#define __einfo_uniqify(einfo_base, uniq, desc)
Declare disambiguated error.
Definition: errno.h:180

Definition at line 55 of file netfront.c.

◆ EIO_NETIF_RSP

#define EIO_NETIF_RSP (   status)
Value:
#define EIO_NETIF_RSP_ERROR
Definition: netfront.c:48
uint8_t status
Status.
Definition: ena.h:16
#define EINFO_EIO
Definition: errno.h:434
#define EUNIQ(einfo_base, uniq,...)
Disambiguate a base error based on non-constant information.
Definition: errno.h:225
#define EIO_NETIF_RSP_DROPPED
Definition: netfront.c:53

Definition at line 58 of file netfront.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ netfront_reset()

static int netfront_reset ( struct netfront_nic netfront)
static

Reset device.

Parameters
netfrontNetfront device
Return values
rcReturn status code

Definition at line 75 of file netfront.c.

75  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint8_t state
State.
Definition: eth_slow.h:47
int xenbus_backend_wait(struct xen_device *xendev, int state)
Wait for backend to reach a given state.
Definition: xenbus.c:147
#define DBGC(...)
Definition: compiler.h:505
A Xen device.
Definition: xenbus.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
char * key
XenStore key.
Definition: xenbus.h:24
int xenbus_backend_state(struct xen_device *xendev)
Get backend state.
Definition: xenbus.c:125
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
int xenbus_set_state(struct xen_device *xendev, int state)
Set device state.
Definition: xenbus.c:105

References DBGC, xen_device::key, rc, state, strerror(), xenbus_backend_state(), xenbus_backend_wait(), xenbus_set_state(), XenbusStateClosed, XenbusStateInitialising, XenbusStateInitWait, and netfront_nic::xendev.

Referenced by netfront_close(), netfront_open(), and netfront_probe().

◆ netfront_read_mac()

static int netfront_read_mac ( struct netfront_nic netfront,
void *  hw_addr 
)
static

Fetch MAC address.

Parameters
netfrontNetfront device
hw_addrHardware address to fill in
Return values
rcReturn status code

Definition at line 125 of file netfront.c.

125  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
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
A Xen hypervisor.
Definition: xen.h:51
A Xen device.
Definition: xenbus.h:18
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
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#define ETH_ALEN
Definition: if_ether.h:8
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
char * key
XenStore key.
Definition: xenbus.h:24
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22

References DBGC, DBGC2, ETH_ALEN, free, hex_decode(), xen_device::key, len, mac, NULL, rc, strerror(), xen_device::xen, netfront_nic::xendev, and xenstore_read().

Referenced by netfront_probe().

◆ netfront_write_num()

static int netfront_write_num ( struct netfront_nic netfront,
const char *  subkey,
unsigned long  num 
)
static

Write XenStore numeric value.

Parameters
netfrontNetfront device
subkeySubkey
numNumeric value
Return values
rcReturn status code

Definition at line 167 of file netfront.c.

168  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
int xenstore_write_num(struct xen_hypervisor *xen, unsigned long num,...)
Write XenStore numeric value.
Definition: xenstore.c:460
A Xen hypervisor.
Definition: xen.h:51
A Xen device.
Definition: xenbus.h:18
char unsigned long * num
Definition: xenstore.h:17
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
char * key
XenStore key.
Definition: xenbus.h:24
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22

References DBGC, xen_device::key, NULL, num, rc, strerror(), xen_device::xen, netfront_nic::xendev, and xenstore_write_num().

Referenced by netfront_create_event(), netfront_create_ring(), and netfront_write_flag().

◆ netfront_write_flag()

static int netfront_write_flag ( struct netfront_nic netfront,
const char *  subkey 
)
static

Write XenStore flag value.

Parameters
netfrontNetfront device
subkeySubkey
numNumeric value
Return values
rcReturn status code

Definition at line 192 of file netfront.c.

193  {
194 
195  return netfront_write_num ( netfront, subkey, 1 );
196 }
static int netfront_write_num(struct netfront_nic *netfront, const char *subkey, unsigned long num)
Write XenStore numeric value.
Definition: netfront.c:167

References netfront_write_num().

Referenced by netfront_open().

◆ netfront_rm()

static int netfront_rm ( struct netfront_nic netfront,
const char *  subkey 
)
static

Delete XenStore value.

Parameters
netfrontNetfront device
subkeySubkey
Return values
rcReturn status code

Definition at line 205 of file netfront.c.

205  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
A Xen hypervisor.
Definition: xen.h:51
A Xen device.
Definition: xenbus.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
char * key
XenStore key.
Definition: xenbus.h:24
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
int xenstore_rm(struct xen_hypervisor *xen,...)
Delete XenStore value.
Definition: xenstore.c:483
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22

References DBGC, xen_device::key, NULL, rc, strerror(), xen_device::xen, netfront_nic::xendev, and xenstore_rm().

Referenced by netfront_close(), netfront_create_event(), netfront_create_ring(), netfront_destroy_event(), netfront_destroy_ring(), and netfront_open().

◆ netfront_create_event()

static int netfront_create_event ( struct netfront_nic netfront)
static

Create event channel.

Parameters
netfrontNetfront device
Return values
rcReturn status code

Definition at line 233 of file netfront.c.

233  {
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 }
evtchn_port_t port
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int netfront_rm(struct netfront_nic *netfront, const char *subkey)
Delete XenStore value.
Definition: netfront.c:205
#define DBGC(...)
Definition: compiler.h:505
#define EXEN(xenrc)
Convert a Xen status code to an iPXE status code.
Definition: xen.h:87
A Xen hypervisor.
Definition: xen.h:51
struct evtchn_send event
Event channel.
Definition: netfront.h:169
A Xen device.
Definition: xenbus.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
unsigned long backend_id
Backend domain ID.
Definition: xenbus.h:28
static int netfront_write_num(struct netfront_nic *netfront, const char *subkey, unsigned long num)
Write XenStore numeric value.
Definition: netfront.c:167
evtchn_port_t port
Definition: event_channel.h:96
char * key
XenStore key.
Definition: xenbus.h:24
#define DOMID_SELF
Definition: xen.h:501
static struct evtchn_alloc_unbound * alloc_unbound
Definition: xenevent.h:52
static struct evtchn_close * close
Definition: xenevent.h:23
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
evtchn_port_t port
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22

References alloc_unbound, xen_device::backend_id, close, DBGC, evtchn_alloc_unbound::dom, DOMID_SELF, netfront_nic::event, EXEN, xen_device::key, netfront_rm(), netfront_write_num(), evtchn_alloc_unbound::port, evtchn_close::port, evtchn_send::port, rc, evtchn_alloc_unbound::remote_dom, strerror(), xen_device::xen, and netfront_nic::xendev.

Referenced by netfront_open().

◆ netfront_send_event()

static int netfront_send_event ( struct netfront_nic netfront)
inlinestatic

Send event.

Parameters
netfrontNetfront device
Return values
rcReturn status code

Definition at line 276 of file netfront.c.

276  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
#define EXEN(xenrc)
Convert a Xen status code to an iPXE status code.
Definition: xen.h:87
A Xen hypervisor.
Definition: xen.h:51
struct evtchn_send event
Event channel.
Definition: netfront.h:169
A Xen device.
Definition: xenbus.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
char * key
XenStore key.
Definition: xenbus.h:24
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22

References DBGC, netfront_nic::event, EXEN, xen_device::key, rc, strerror(), xen_device::xen, and netfront_nic::xendev.

Referenced by netfront_refill_rx(), and netfront_transmit().

◆ netfront_destroy_event()

static void netfront_destroy_event ( struct netfront_nic netfront)
static

Destroy event channel.

Parameters
netfrontNetfront device

Definition at line 298 of file netfront.c.

298  {
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 }
evtchn_port_t port
static int netfront_rm(struct netfront_nic *netfront, const char *subkey)
Delete XenStore value.
Definition: netfront.c:205
A Xen hypervisor.
Definition: xen.h:51
struct evtchn_send event
Event channel.
Definition: netfront.h:169
A Xen device.
Definition: xenbus.h:18
static struct evtchn_close * close
Definition: xenevent.h:23
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
evtchn_port_t port
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22

References close, netfront_nic::event, netfront_rm(), evtchn_close::port, evtchn_send::port, xen_device::xen, and netfront_nic::xendev.

Referenced by netfront_close(), and netfront_open().

◆ netfront_create_ring()

static int netfront_create_ring ( struct netfront_nic netfront,
struct netfront_ring ring 
)
static

Create descriptor ring.

Parameters
netfrontNetfront device
ringDescriptor ring
Return values
rcReturn status code

Definition at line 325 of file netfront.c.

326  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
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
static int netfront_rm(struct netfront_nic *netfront, const char *subkey)
Delete XenStore value.
Definition: netfront.c:205
grant_ref_t ref
Shared ring grant reference.
Definition: netfront.h:62
#define DBGC(...)
Definition: compiler.h:505
#define PAGE_SIZE
Page size.
Definition: io.h:27
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
A Xen hypervisor.
Definition: xen.h:51
void * raw
Raw pointer.
Definition: netfront.h:57
A Xen device.
Definition: xenbus.h:18
#define ENOMEM
Not enough space.
Definition: errno.h:534
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint8_t * ids
Buffer ID ring.
Definition: netfront.h:72
size_t count
Maximum number of used descriptors.
Definition: netfront.h:65
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
unsigned long backend_id
Backend domain ID.
Definition: xenbus.h:28
static int netfront_write_num(struct netfront_nic *netfront, const char *subkey, unsigned long num)
Write XenStore numeric value.
Definition: netfront.c:167
struct io_buffer ** iobufs
I/O buffers, indexed by buffer ID.
Definition: netfront.h:67
char * key
XenStore key.
Definition: xenbus.h:24
unsigned int id_prod
Buffer ID ring producer counter.
Definition: netfront.h:74
static void *__malloc malloc_dma(size_t size, size_t phys_align)
Allocate memory for DMA.
Definition: malloc.h:66
union netfront_ring::@78 sring
Shared ring.
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
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22

References assert(), xen_device::backend_id, netfront_ring::count, DBGC, ENOMEM, free_dma(), netfront_ring::id_cons, netfront_ring::id_prod, netfront_ring::ids, netfront_ring::iobufs, xen_device::key, malloc_dma(), netfront_rm(), netfront_write_num(), NULL, PAGE_SIZE, netfront_ring::raw, rc, netfront_ring::ref, netfront_ring::ref_key, netfront_ring::sring, strerror(), virt_to_phys(), xen_device::xen, and netfront_nic::xendev.

Referenced by netfront_open().

◆ netfront_push()

static int netfront_push ( struct netfront_nic netfront,
struct netfront_ring ring,
struct io_buffer iobuf,
uint16_t id,
grant_ref_t ref 
)
static

Add buffer to descriptor ring.

Parameters
netfrontNetfront device
ringDescriptor ring
iobufI/O buffer
idBuffer ID to fill in
refGrant reference to fill in
Return values
rcReturn status code

The caller is responsible for ensuring that there is space in the ring.

Definition at line 389 of file netfront.c.

391  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
A Xen hypervisor.
Definition: xen.h:51
A Xen device.
Definition: xenbus.h:18
grant_ref_t * refs
I/O buffer grant references, indexed by buffer ID.
Definition: netfront.h:69
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint8_t * ids
Buffer ID ring.
Definition: netfront.h:72
size_t count
Maximum number of used descriptors.
Definition: netfront.h:65
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
unsigned long backend_id
Backend domain ID.
Definition: xenbus.h:28
struct io_buffer ** iobufs
I/O buffers, indexed by buffer ID.
Definition: netfront.h:67
void * data
Start of data.
Definition: iobuf.h:44
char * key
XenStore key.
Definition: xenbus.h:24
unsigned int id_prod
Buffer ID ring producer counter.
Definition: netfront.h:74
static const char grant_ref_t ref
Definition: netfront.h:91
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22

References assert(), xen_device::backend_id, netfront_ring::count, io_buffer::data, DBGC, netfront_ring::id_prod, netfront_ring::ids, netfront_ring::iobufs, xen_device::key, NULL, rc, ref, netfront_ring::refs, strerror(), virt_to_phys(), xen_device::xen, and netfront_nic::xendev.

Referenced by netfront_refill_rx(), and netfront_transmit().

◆ netfront_pull()

static struct io_buffer* netfront_pull ( struct netfront_nic netfront,
struct netfront_ring ring,
unsigned int  id 
)
static

Remove buffer from descriptor ring.

Parameters
netfrontNetfront device
ringDescriptor ring
idBuffer ID
Return values
iobufI/O buffer

Definition at line 439 of file netfront.c.

441  {
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 }
unsigned int id_cons
Buffer ID ring consumer counter.
Definition: netfront.h:76
A Xen hypervisor.
Definition: xen.h:51
A Xen device.
Definition: xenbus.h:18
grant_ref_t * refs
I/O buffer grant references, indexed by buffer ID.
Definition: netfront.h:69
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint8_t * ids
Buffer ID ring.
Definition: netfront.h:72
uint8_t id
Request identifier.
Definition: ena.h:12
size_t count
Maximum number of used descriptors.
Definition: netfront.h:65
struct io_buffer ** iobufs
I/O buffers, indexed by buffer ID.
Definition: netfront.h:67
uint16_t count
Number of entries.
Definition: ena.h:22
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22
A persistent I/O buffer.
Definition: iobuf.h:32

References assert(), count, netfront_ring::count, id, netfront_ring::id_cons, netfront_ring::ids, netfront_ring::iobufs, NULL, netfront_ring::refs, xen_device::xen, and netfront_nic::xendev.

Referenced by netfront_destroy_ring(), netfront_poll_rx(), and netfront_poll_tx().

◆ netfront_destroy_ring()

static void netfront_destroy_ring ( struct netfront_nic netfront,
struct netfront_ring ring,
void(*)(struct io_buffer *)  discard 
)
static

Destroy descriptor ring.

Parameters
netfrontNetfront device
ringDescriptor ring
discardMethod used to discard outstanding buffer, or NULL

Definition at line 470 of file netfront.c.

472  {
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 }
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
static int netfront_rm(struct netfront_nic *netfront, const char *subkey)
Delete XenStore value.
Definition: netfront.c:205
grant_ref_t ref
Shared ring grant reference.
Definition: netfront.h:62
#define PAGE_SIZE
Page size.
Definition: io.h:27
A Xen hypervisor.
Definition: xen.h:51
void * raw
Raw pointer.
Definition: netfront.h:57
A Xen device.
Definition: xenbus.h:18
uint8_t * ids
Buffer ID ring.
Definition: netfront.h:72
uint8_t id
Request identifier.
Definition: ena.h:12
size_t count
Maximum number of used descriptors.
Definition: netfront.h:65
union netfront_ring::@78 sring
Shared ring.
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
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
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
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22
A persistent I/O buffer.
Definition: iobuf.h:32

References netfront_ring::count, free_dma(), id, netfront_ring::id_cons, netfront_ring::ids, netfront_pull(), netfront_rm(), NULL, PAGE_SIZE, netfront_ring::raw, netfront_ring::ref, netfront_ring::ref_key, netfront_ring::sring, xen_device::xen, and netfront_nic::xendev.

Referenced by netfront_close(), and netfront_open().

◆ netfront_refill_rx()

static void netfront_refill_rx ( struct net_device netdev)
static

Refill receive descriptor ring.

Parameters
netdevNetwork device

Definition at line 509 of file netfront.c.

509  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void netdev_rx_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Discard received packet.
Definition: netdevice.c:501
#define RING_GET_REQUEST(_r, _idx)
Definition: ring.h:214
#define PAGE_SIZE
Page size.
Definition: io.h:27
#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
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
A Xen device.
Definition: xenbus.h:18
void * priv
Driver private data.
Definition: netdevice.h:425
#define NETFRONT_RX_FILL
Receive ring fill level.
Definition: netfront.h:32
static struct net_device * netdev
Definition: gdbudp.c:52
A netfront NIC.
Definition: netfront.h:144
static size_t iob_tailroom(struct io_buffer *iobuf)
Calculate available space at end of an I/O buffer.
Definition: iobuf.h:171
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
struct netfront_ring rx
Receive ring.
Definition: netfront.h:160
#define DBGC2(...)
Definition: compiler.h:522
void * data
Start of data.
Definition: iobuf.h:44
char * key
XenStore key.
Definition: xenbus.h:24
u8 request[0]
List of IEs requested.
Definition: ieee80211.h:16
netif_rx_front_ring_t rx_fring
Receive front ring.
Definition: netfront.h:162
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
A persistent I/O buffer.
Definition: iobuf.h:32

References alloc_iob(), io_buffer::data, DBGC2, iob_tailroom(), xen_device::key, netdev, netdev_rx_err(), netfront_push(), NETFRONT_RX_FILL, netfront_send_event(), PAGE_SIZE, net_device::priv, rc, request, RING_GET_REQUEST, RING_PUSH_REQUESTS_AND_CHECK_NOTIFY, netfront_nic::rx, netfront_nic::rx_fring, virt_to_phys(), and netfront_nic::xendev.

Referenced by netfront_open(), and netfront_poll().

◆ netfront_open()

static int netfront_open ( struct net_device netdev)
static

Open network device.

Parameters
netdevNetwork device
Return values
rcReturn status code

Definition at line 562 of file netfront.c.

562  {
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 }
static void netfront_refill_rx(struct net_device *netdev)
Refill receive descriptor ring.
Definition: netfront.c:509
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
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_create_event(struct netfront_nic *netfront)
Create event channel.
Definition: netfront.c:233
#define DBGC(...)
Definition: compiler.h:505
#define PAGE_SIZE
Page size.
Definition: io.h:27
static void netfront_destroy_event(struct netfront_nic *netfront)
Destroy event channel.
Definition: netfront.c:298
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
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
void * priv
Driver private data.
Definition: netdevice.h:425
static void netdev_link_up(struct net_device *netdev)
Mark network device as having link up.
Definition: netdevice.h:768
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
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
struct netfront_ring rx
Receive ring.
Definition: netfront.h:160
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
char * key
XenStore key.
Definition: xenbus.h:24
netif_tx_front_ring_t tx_fring
Transmit front ring.
Definition: netfront.h:153
netif_rx_front_ring_t rx_fring
Receive front ring.
Definition: netfront.h:162
#define FRONT_RING_INIT(_r, _s, __size)
Definition: ring.h:165
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
struct netfront_ring tx
Transmit ring.
Definition: netfront.h:151
int xenbus_set_state(struct xen_device *xendev, int state)
Set device state.
Definition: xenbus.c:105
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References assert(), netfront_ring::count, DBGC, FRONT_RING_INIT, xen_device::key, netdev, netdev_link_up(), netfront_create_event(), netfront_create_ring(), netfront_destroy_event(), netfront_destroy_ring(), netfront_refill_rx(), netfront_reset(), netfront_rm(), netfront_write_flag(), NULL, PAGE_SIZE, net_device::priv, rc, RING_SIZE, netfront_nic::rx, netfront_nic::rx_fring, SHARED_RING_INIT, strerror(), netfront_nic::tx, netfront_nic::tx_fring, xenbus_backend_wait(), xenbus_set_state(), XenbusStateConnected, and netfront_nic::xendev.

◆ netfront_close()

static void netfront_close ( struct net_device netdev)
static

Close network device.

Parameters
netdevNetwork device

Definition at line 652 of file netfront.c.

652  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int netfront_rm(struct netfront_nic *netfront, const char *subkey)
Delete XenStore value.
Definition: netfront.c:205
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
#define DBGC(...)
Definition: compiler.h:505
void netdev_link_down(struct net_device *netdev)
Mark network device as having link down.
Definition: netdevice.c:186
static void netfront_destroy_event(struct netfront_nic *netfront)
Destroy event channel.
Definition: netfront.c:298
static int netfront_reset(struct netfront_nic *netfront)
Reset device.
Definition: netfront.c:75
A Xen device.
Definition: xenbus.h:18
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
A netfront NIC.
Definition: netfront.h:144
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
void netdev_link_err(struct net_device *netdev, int rc)
Mark network device as having a specific link state.
Definition: netdevice.c:163
struct netfront_ring rx
Receive ring.
Definition: netfront.h:160
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
char * key
XenStore key.
Definition: xenbus.h:24
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
struct netfront_ring tx
Transmit ring.
Definition: netfront.h:151
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References DBGC, free_iob(), xen_device::key, netdev, netdev_link_down(), netdev_link_err(), netfront_destroy_event(), netfront_destroy_ring(), netfront_reset(), netfront_rm(), NULL, net_device::priv, rc, netfront_nic::rx, strerror(), netfront_nic::tx, and netfront_nic::xendev.

◆ netfront_transmit()

static int netfront_transmit ( struct net_device netdev,
struct io_buffer iobuf 
)
static

Transmit packet.

Parameters
netdevNetwork device
iobufI/O buffer
Return values
rcReturn status code

Definition at line 701 of file netfront.c.

702  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
#define RING_GET_REQUEST(_r, _idx)
Definition: ring.h:214
#define PAGE_SIZE
Page size.
Definition: io.h:27
#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 __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
A Xen device.
Definition: xenbus.h:18
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
A netfront NIC.
Definition: netfront.h:144
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
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
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
#define DBGC2(...)
Definition: compiler.h:522
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
u8 request[0]
List of IEs requested.
Definition: ieee80211.h:16
#define NETTXF_data_validated
Definition: netif.h:157
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
struct netfront_ring tx
Transmit ring.
Definition: netfront.h:151

References io_buffer::data, DBGC, DBGC2, ENOBUFS, iob_len(), xen_device::key, netdev, netfront_push(), netfront_send_event(), NETTXF_data_validated, PAGE_SIZE, net_device::priv, rc, request, RING_GET_REQUEST, RING_PUSH_REQUESTS_AND_CHECK_NOTIFY, netfront_nic::tx, netfront_nic::tx_fring, virt_to_phys(), and netfront_nic::xendev.

◆ netfront_poll_tx()

static void netfront_poll_tx ( struct net_device netdev)
static

Poll for completed packets.

Parameters
netdevNetwork device

Definition at line 746 of file netfront.c.

746  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void netdev_tx_complete(struct net_device *netdev, struct io_buffer *iobuf)
Complete network transmission.
Definition: netdevice.h:746
#define EIO_NETIF_RSP(status)
Definition: netfront.c:58
uint8_t status
Status.
Definition: ena.h:16
A Xen device.
Definition: xenbus.h:18
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
A netfront NIC.
Definition: netfront.h:144
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#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
#define DBGC2(...)
Definition: compiler.h:522
#define RING_GET_RESPONSE(_r, _idx)
Definition: ring.h:217
char * key
XenStore key.
Definition: xenbus.h:24
netif_tx_front_ring_t tx_fring
Transmit front ring.
Definition: netfront.h:153
#define NETIF_RSP_OKAY
Definition: netif.h:293
uint16_t id
Definition: netif.h:249
int16_t status
Definition: netif.h:250
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
struct netfront_ring tx
Transmit ring.
Definition: netfront.h:151
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
A persistent I/O buffer.
Definition: iobuf.h:32

References DBGC2, EIO_NETIF_RSP, netif_tx_response::id, xen_device::key, netdev, netdev_tx_complete(), netdev_tx_complete_err(), netfront_pull(), NETIF_RSP_OKAY, net_device::priv, rc, RING_GET_RESPONSE, RING_HAS_UNCONSUMED_RESPONSES, status, netif_tx_response::status, strerror(), netfront_nic::tx, netfront_nic::tx_fring, and netfront_nic::xendev.

Referenced by netfront_poll().

◆ netfront_poll_rx()

static void netfront_poll_rx ( struct net_device netdev)
static

Poll for received packets.

Parameters
netdevNetwork device

Definition at line 783 of file netfront.c.

783  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#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
int16_t status
Definition: netif.h:280
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
#define EIO_NETIF_RSP(status)
Definition: netfront.c:58
uint8_t status
Status.
Definition: ena.h:16
A Xen device.
Definition: xenbus.h:18
uint16_t id
Definition: netif.h:277
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
A netfront NIC.
Definition: netfront.h:144
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
void netdev_rx(struct net_device *netdev, struct io_buffer *iobuf)
Add packet to receive queue.
Definition: netdevice.c:470
#define iob_reserve(iobuf, len)
Definition: iobuf.h:63
struct netfront_ring rx
Receive ring.
Definition: netfront.h:160
#define RING_HAS_UNCONSUMED_RESPONSES(_r)
Definition: ring.h:194
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
#define RING_GET_RESPONSE(_r, _idx)
Definition: ring.h:217
void * data
Start of data.
Definition: iobuf.h:44
char * key
XenStore key.
Definition: xenbus.h:24
netif_rx_front_ring_t rx_fring
Receive front ring.
Definition: netfront.h:162
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
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
A persistent I/O buffer.
Definition: iobuf.h:32

References io_buffer::data, DBGC2, EIO_NETIF_RSP, netif_rx_response::id, iob_put, iob_reserve, xen_device::key, len, netdev, netdev_rx(), netdev_rx_err(), netfront_pull(), netif_rx_response::offset, net_device::priv, rc, RING_GET_RESPONSE, RING_HAS_UNCONSUMED_RESPONSES, netfront_nic::rx, netfront_nic::rx_fring, status, netif_rx_response::status, strerror(), virt_to_phys(), and netfront_nic::xendev.

Referenced by netfront_poll().

◆ netfront_poll()

static void netfront_poll ( struct net_device netdev)
static

Poll for completed and received packets.

Parameters
netdevNetwork device

Definition at line 825 of file netfront.c.

825  {
826 
827  /* Poll for TX completions */
829 
830  /* Poll for RX completions */
832 
833  /* Refill RX descriptor ring */
835 }
static void netfront_refill_rx(struct net_device *netdev)
Refill receive descriptor ring.
Definition: netfront.c:509
static void netfront_poll_tx(struct net_device *netdev)
Poll for completed packets.
Definition: netfront.c:746
static struct net_device * netdev
Definition: gdbudp.c:52
static void netfront_poll_rx(struct net_device *netdev)
Poll for received packets.
Definition: netfront.c:783

References netdev, netfront_poll_rx(), netfront_poll_tx(), and netfront_refill_rx().

◆ netfront_probe()

static int netfront_probe ( struct xen_device xendev)
static

Probe Xen device.

Parameters
xendevXen device
Return values
rcReturn status code

Definition at line 858 of file netfront.c.

858  {
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 }
char * backend
Backend XenStore key.
Definition: xenbus.h:26
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
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
struct io_buffer * rx_iobufs[NETFRONT_NUM_RX_DESC]
Receive I/O buffers.
Definition: netfront.h:164
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
A Xen hypervisor.
Definition: xen.h:51
#define NETFRONT_NUM_TX_DESC
Number of transmit ring entries.
Definition: netfront.h:16
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
#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 netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition: netdevice.h:555
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
Receive ring grant reference index.
Definition: netfront.h:41
static struct net_device * netdev
Definition: gdbudp.c:52
Receive descriptor grant reference base index.
Definition: netfront.h:43
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition: netdevice.c:844
A netfront NIC.
Definition: netfront.h:144
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
int register_netdev(struct net_device *netdev)
Register network device.
Definition: netdevice.c:667
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
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition: netdevice.h:511
Transmit descriptor grant reference base index.
Definition: netfront.h:39
grant_ref_t refs[NETFRONT_REF_COUNT]
Grant references.
Definition: netfront.h:148
void xengrant_free(struct xen_hypervisor *xen, grant_ref_t *refs, unsigned int count)
Free grant references.
Definition: xengrant.c:214
struct device * dev
Underlying hardware device.
Definition: netdevice.h:360
uint8_t tx_ids[NETFRONT_NUM_TX_DESC]
Transmit I/O buffer IDs.
Definition: netfront.h:157
struct netfront_ring rx
Receive ring.
Definition: netfront.h:160
char * key
XenStore key.
Definition: xenbus.h:24
struct net_device * alloc_etherdev(size_t priv_size)
Allocate Ethernet device.
Definition: ethernet.c:264
struct device dev
Generic iPXE device.
Definition: xenbus.h:20
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
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
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:375
Transmit ring grant reference index.
Definition: netfront.h:37
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22

References alloc_etherdev(), xen_device::backend, xen_device::backend_id, DBGC, xen_device::dev, net_device::dev, ENOMEM, net_device::hw_addr, xen_device::key, netdev, netdev_init(), netdev_link_down(), netdev_nullify(), netdev_put(), NETFRONT_NUM_RX_DESC, NETFRONT_NUM_TX_DESC, netfront_operations, netfront_read_mac(), NETFRONT_REF_COUNT, NETFRONT_REF_RX_BASE, NETFRONT_REF_RX_RING, NETFRONT_REF_TX_BASE, NETFRONT_REF_TX_RING, netfront_reset(), net_device::priv, rc, netfront_nic::refs, register_netdev(), netfront_nic::rx, netfront_nic::rx_ids, netfront_nic::rx_iobufs, strerror(), netfront_nic::tx, netfront_nic::tx_ids, netfront_nic::tx_iobufs, unregister_netdev(), xen_device::xen, xen_set_drvdata(), netfront_nic::xendev, xengrant_alloc(), and xengrant_free().

◆ netfront_remove()

static void netfront_remove ( struct xen_device xendev)
static

Remove Xen device.

Parameters
xendevXen device

Definition at line 931 of file netfront.c.

931  {
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 }
Total number of grant references required.
Definition: netfront.h:45
A Xen hypervisor.
Definition: xen.h:51
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition: netdevice.h:555
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition: netdevice.c:844
A netfront NIC.
Definition: netfront.h:144
A network device.
Definition: netdevice.h:348
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition: netdevice.h:511
grant_ref_t refs[NETFRONT_REF_COUNT]
Grant references.
Definition: netfront.h:148
void xengrant_free(struct xen_hypervisor *xen, grant_ref_t *refs, unsigned int count)
Free grant references.
Definition: xengrant.c:214
static void * xen_get_drvdata(struct xen_device *xendev)
Get Xen device driver-private data.
Definition: xenbus.h:76
struct xen_hypervisor * xen
Xen hypervisor.
Definition: xenbus.h:22

References netdev, netdev_nullify(), netdev_put(), NETFRONT_REF_COUNT, net_device::priv, netfront_nic::refs, unregister_netdev(), xen_device::xen, xen_get_drvdata(), and xengrant_free().

Variable Documentation

◆ netfront_operations

struct net_device_operations netfront_operations
static
Initial value:
= {
.open = netfront_open,
.close = netfront_close,
.transmit = netfront_transmit,
.poll = netfront_poll,
}
static int netfront_open(struct net_device *netdev)
Open network device.
Definition: netfront.c:562
static int netfront_transmit(struct net_device *netdev, struct io_buffer *iobuf)
Transmit packet.
Definition: netfront.c:701
static void netfront_poll(struct net_device *netdev)
Poll for completed and received packets.
Definition: netfront.c:825
static void netfront_close(struct net_device *netdev)
Close network device.
Definition: netfront.c:652

Network device operations.

Definition at line 838 of file netfront.c.

Referenced by netfront_probe().

◆ __xen_driver

struct xen_driver netfront_driver __xen_driver
Initial value:
= {
.name = "netfront",
.type = "vif",
.probe = netfront_probe,
.remove = netfront_remove,
}
static void netfront_remove(struct xen_device *xendev)
Remove Xen device.
Definition: netfront.c:931
static int netfront_probe(struct xen_device *xendev)
Probe Xen device.
Definition: netfront.c:858

Xen netfront driver.

Definition at line 948 of file netfront.c.