iPXE
netfront.h
Go to the documentation of this file.
1 #ifndef _NETFRONT_H
2 #define _NETFRONT_H
3 
4 /** @file
5  *
6  * Xen netfront driver
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <ipxe/xen.h>
13 #include <xen/io/netif.h>
14 
15 /** Number of transmit ring entries */
16 #define NETFRONT_NUM_TX_DESC 16
17 
18 /** Number of receive ring entries */
19 #define NETFRONT_NUM_RX_DESC 32
20 
21 /** Receive ring fill level
22  *
23  * The xen-netback driver from kernels 3.18 to 4.2 inclusive have a
24  * bug (CA-163395) which prevents packet reception if fewer than 18
25  * receive descriptors are available. This was fixed in upstream
26  * kernel commit d5d4852 ("xen-netback: require fewer guest Rx slots
27  * when not using GSO").
28  *
29  * We provide 18 receive descriptors to avoid unpleasant silent
30  * failures on these kernel versions.
31  */
32 #define NETFRONT_RX_FILL 18
33 
34 /** Grant reference indices */
36  /** Transmit ring grant reference index */
38  /** Transmit descriptor grant reference base index */
40  /** Receive ring grant reference index */
42  /** Receive descriptor grant reference base index */
44  /** Total number of grant references required */
46 };
47 
48 /** A netfront descriptor ring */
49 struct netfront_ring {
50  /** Shared ring */
51  union {
52  /** Transmit shared ring */
53  netif_tx_sring_t *tx;
54  /** Receive shared ring */
55  netif_rx_sring_t *rx;
56  /** Raw pointer */
57  void *raw;
58  } sring;
59  /** Shared ring grant reference key */
60  const char *ref_key;
61  /** Shared ring grant reference */
63 
64  /** Maximum number of used descriptors */
65  size_t count;
66  /** I/O buffers, indexed by buffer ID */
67  struct io_buffer **iobufs;
68  /** I/O buffer grant references, indexed by buffer ID */
70 
71  /** Buffer ID ring */
73  /** Buffer ID ring producer counter */
74  unsigned int id_prod;
75  /** Buffer ID ring consumer counter */
76  unsigned int id_cons;
77 };
78 
79 /**
80  * Initialise descriptor ring
81  *
82  * @v ring Descriptor ring
83  * @v ref_key Shared ring grant reference key
84  * @v ref Shared ring grant reference
85  * @v count Maxium number of used descriptors
86  * @v iobufs I/O buffers
87  * @v refs I/O buffer grant references
88  * @v ids Buffer IDs
89  */
90 static inline __attribute__ (( always_inline )) void
91 netfront_init_ring ( struct netfront_ring *ring, const char *ref_key,
92  grant_ref_t ref, unsigned int count,
94  uint8_t *ids ) {
95 
96  ring->ref_key = ref_key;
97  ring->ref = ref;
98  ring->count = count;
99  ring->iobufs = iobufs;
100  ring->refs = refs;
101  ring->ids = ids;
102 }
103 
104 /**
105  * Calculate descriptor ring fill level
106  *
107  * @v ring Descriptor ring
108  * @v fill Fill level
109  */
110 static inline __attribute__ (( always_inline )) unsigned int
111 netfront_ring_fill ( struct netfront_ring *ring ) {
112  unsigned int fill_level;
113 
114  fill_level = ( ring->id_prod - ring->id_cons );
115  assert ( fill_level <= ring->count );
116  return fill_level;
117 }
118 
119 /**
120  * Check whether or not descriptor ring is full
121  *
122  * @v ring Descriptor ring
123  * @v is_full Ring is full
124  */
125 static inline __attribute__ (( always_inline )) int
126 netfront_ring_is_full ( struct netfront_ring *ring ) {
127 
128  return ( netfront_ring_fill ( ring ) >= ring->count );
129 }
130 
131 /**
132  * Check whether or not descriptor ring is empty
133  *
134  * @v ring Descriptor ring
135  * @v is_empty Ring is empty
136  */
137 static inline __attribute__ (( always_inline )) int
138 netfront_ring_is_empty ( struct netfront_ring *ring ) {
139 
140  return ( netfront_ring_fill ( ring ) == 0 );
141 }
142 
143 /** A netfront NIC */
144 struct netfront_nic {
145  /** Xen device */
147  /** Grant references */
149 
150  /** Transmit ring */
152  /** Transmit front ring */
153  netif_tx_front_ring_t tx_fring;
154  /** Transmit I/O buffers */
156  /** Transmit I/O buffer IDs */
158 
159  /** Receive ring */
161  /** Receive front ring */
162  netif_rx_front_ring_t rx_fring;
163  /** Receive I/O buffers */
165  /** Receive I/O buffer IDs */
167 
168  /** Event channel */
170 };
171 
172 /** Transmit shared ring field */
173 #define tx_sring tx.sring.tx
174 
175 /** Receive shared ring field */
176 #define rx_sring rx.sring.rx
177 
178 #endif /* _NETFRONT_H */
netif_rx_sring_t * rx
Receive shared ring.
Definition: netfront.h:55
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
grant_ref_t ref
Shared ring grant reference.
Definition: netfront.h:62
static const char * ref_key
Definition: netfront.h:91
static const char grant_ref_t unsigned int struct io_buffer grant_ref_t uint8_t * ids
Definition: netfront.h:94
struct io_buffer * rx_iobufs[NETFRONT_NUM_RX_DESC]
Receive I/O buffers.
Definition: netfront.h:164
netfront_ref_index
Grant reference indices.
Definition: netfront.h:35
uint32_t grant_ref_t
Definition: grant_table.h:116
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
Xen interface.
#define NETFRONT_NUM_TX_DESC
Number of transmit ring entries.
Definition: netfront.h:16
void * raw
Raw pointer.
Definition: netfront.h:57
struct evtchn_send event
Event channel.
Definition: netfront.h:169
A Xen device.
Definition: xenbus.h:18
grant_ref_t * refs
I/O buffer grant references, indexed by buffer ID.
Definition: netfront.h:69
#define NETFRONT_NUM_RX_DESC
Number of receive ring entries.
Definition: netfront.h:19
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
Receive ring grant reference index.
Definition: netfront.h:41
Receive descriptor grant reference base index.
Definition: netfront.h:43
uint8_t * ids
Buffer ID ring.
Definition: netfront.h:72
size_t count
Maximum number of used descriptors.
Definition: netfront.h:65
A netfront NIC.
Definition: netfront.h:144
static const char grant_ref_t unsigned int struct io_buffer grant_ref_t * refs
Definition: netfront.h:93
netif_tx_sring_t * tx
Transmit shared ring.
Definition: netfront.h:53
struct io_buffer * tx_iobufs[NETFRONT_NUM_TX_DESC]
Transmit I/O buffers.
Definition: netfront.h:155
static const char grant_ref_t unsigned int struct io_buffer ** iobufs
Definition: netfront.h:93
unsigned char uint8_t
Definition: stdint.h:10
Transmit descriptor grant reference base index.
Definition: netfront.h:39
grant_ref_t refs[NETFRONT_REF_COUNT]
Grant references.
Definition: netfront.h:148
static const char grant_ref_t unsigned int count
Definition: netfront.h:91
static __attribute__((always_inline)) void netfront_init_ring(struct netfront_ring *ring
Initialise descriptor ring.
Definition: netfront.h:110
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
struct netfront_ring rx
Receive ring.
Definition: netfront.h:160
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
netif_rx_front_ring_t rx_fring
Receive front ring.
Definition: netfront.h:162
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
union netfront_ring::@78 sring
Shared ring.
static const char grant_ref_t ref
Definition: netfront.h:91
struct xen_device * xendev
Xen device.
Definition: netfront.h:146
struct netfront_ring tx
Transmit ring.
Definition: netfront.h:151
Transmit ring grant reference index.
Definition: netfront.h:37
A persistent I/O buffer.
Definition: iobuf.h:32