win-pvdrivers

view xennet/xennet.h @ 1067:2ef536c2d9fe

Fix packet exhaustion issue in 2003 where windows doesn't return packets
author James Harper <james.harper@bendigoit.com.au>
date Tue Oct 29 19:39:52 2013 +1100 (2013-10-29)
parents fba0ce4d9e54
children 05ece536b204
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
4 Copyright (C) 2007 Andrew Grover <andy.grover@oracle.com>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
21 #pragma warning(disable: 4201)
22 #pragma warning(disable: 4214)
24 #include <ntddk.h>
25 #include <wdm.h>
26 #define NDIS_MINIPORT_DRIVER 1
27 #if NTDDI_VERSION < NTDDI_WINXP
28 # define NDIS50_MINIPORT 1
29 #elif NTDDI_VERSION < NTDDI_VISTA
30 # define NDIS51_MINIPORT 1
31 #else
32 # define NDIS61_MINIPORT 1
33 #endif
34 #include <ndis.h>
35 #define NTSTRSAFE_LIB
36 #include <ntstrsafe.h>
37 #include <liblfds.h>
39 #define VENDOR_DRIVER_VERSION_MAJOR 0
40 #define VENDOR_DRIVER_VERSION_MINOR 11
42 #define MAX_LINK_SPEED 10000000000L /* there is not really any theoretical maximum... */
44 #define VENDOR_DRIVER_VERSION (((VENDOR_DRIVER_VERSION_MAJOR) << 16) | (VENDOR_DRIVER_VERSION_MINOR))
46 #define __DRIVER_NAME "XenNet"
48 //#define PACKET_NEXT_PACKET_FIELD MiniportReservedEx[sizeof(PVOID)] // RX & TX
49 //#define PACKET_FIRST_PB_FIELD MiniportReservedEx[0] // RX
50 #define PACKET_NEXT_PACKET_FIELD MiniportReservedEx[0] // RX & TX
51 #define PACKET_FIRST_PB_FIELD MiniportReservedEx[sizeof(PVOID)] // RX
52 #define PACKET_LIST_ENTRY_FIELD MiniportReservedEx[sizeof(PVOID)] // TX (2 entries)
53 #define PACKET_NEXT_PACKET(_packet) (*(PNDIS_PACKET *)&(_packet)->PACKET_NEXT_PACKET_FIELD)
54 #define PACKET_LIST_ENTRY(_packet) (*(PLIST_ENTRY)&(_packet)->PACKET_LIST_ENTRY_FIELD)
55 #define PACKET_FIRST_PB(_packet) (*(shared_buffer_t **)&(_packet)->PACKET_FIRST_PB_FIELD)
57 #define NB_LIST_ENTRY_FIELD MiniportReserved[0] // TX (2 entries)
58 #define NB_FIRST_PB_FIELD MiniportReserved[0] // RX
59 #define NB_NBL_FIELD MiniportReserved[2] // TX
60 #define NB_LIST_ENTRY(_nb) (*(PLIST_ENTRY)&(_nb)->NB_LIST_ENTRY_FIELD)
61 #define NB_NBL(_nb) (*(PNET_BUFFER_LIST *)&(_nb)->NB_NBL_FIELD)
62 #define NB_FIRST_PB(_nb) (*(shared_buffer_t **)&(_nb)->NB_FIRST_PB_FIELD)
64 #define NBL_REF_FIELD MiniportReserved[0] // TX
65 #define NBL_REF(_nbl) (*(ULONG_PTR *)&(_nbl)->NBL_REF_FIELD)
67 #define NDIS_STATUS_RESOURCES_MAX_LENGTH 64
69 #include <xen_windows.h>
70 #include <memory.h>
71 #include <grant_table.h>
72 #include <event_channel.h>
73 #include <hvm/params.h>
74 #include <hvm/hvm_op.h>
75 #include <xen_public.h>
76 #include <io/ring.h>
77 #include <io/netif.h>
78 #include <io/xenbus.h>
79 #include <stdlib.h>
80 #define XENNET_POOL_TAG (ULONG) 'XenN'
82 /* Xen macros use these, so they need to be redefined to Win equivs */
83 #define wmb() KeMemoryBarrier()
84 #define mb() KeMemoryBarrier()
86 #define GRANT_INVALID_REF 0
88 #define NAME_SIZE 64
90 #define ETH_ALEN 6
92 static FORCEINLINE USHORT
93 GET_NET_USHORT(USHORT data) {
94 return (data << 8) | (data >> 8);
95 }
97 static FORCEINLINE USHORT
98 GET_NET_PUSHORT(PVOID pdata) {
99 return (*((PUSHORT)pdata) << 8) | (*((PUSHORT)pdata) >> 8);
100 }
102 static FORCEINLINE VOID
103 SET_NET_USHORT(PVOID ptr, USHORT data) {
104 *((PUSHORT)ptr) = GET_NET_USHORT(data);
105 }
107 static FORCEINLINE ULONG
108 GET_NET_ULONG(ULONG data) {
109 ULONG tmp;
111 tmp = ((data & 0x00ff00ff) << 8) | ((data & 0xff00ff00) >> 8);
112 return (tmp << 16) | (tmp >> 16);
113 }
115 static FORCEINLINE ULONG
116 GET_NET_PULONG(PVOID pdata) {
117 ULONG tmp;
119 tmp = ((*((PULONG)pdata) & 0x00ff00ff) << 8) | ((*((PULONG)pdata) & 0xff00ff00) >> 8);
120 return (tmp << 16) | (tmp >> 16);
121 }
123 static FORCEINLINE VOID
124 SET_NET_ULONG(PVOID ptr, ULONG data) {
125 *((PULONG)ptr) = GET_NET_ULONG(data);
126 }
127 /*
128 #define GET_NET_ULONG(x) ((GET_NET_USHORT(x) << 16) | GET_NET_USHORT(((PUCHAR)&x)[2]))
129 #define SET_NET_ULONG(y, x) *((ULONG *)&(y)) = ((GET_NET_USHORT(x) << 16) | GET_NET_USHORT(((PUCHAR)&x)[2]))
130 */
132 #define SUPPORTED_PACKET_FILTERS (\
133 NDIS_PACKET_TYPE_DIRECTED | \
134 NDIS_PACKET_TYPE_MULTICAST | \
135 NDIS_PACKET_TYPE_BROADCAST | \
136 NDIS_PACKET_TYPE_PROMISCUOUS | \
137 NDIS_PACKET_TYPE_ALL_MULTICAST)
139 /* couldn't get regular xen ring macros to work...*/
140 #define __NET_RING_SIZE(type, _sz) \
141 (__RD32( \
142 (_sz - sizeof(struct type##_sring) + sizeof(union type##_sring_entry)) \
143 / sizeof(union type##_sring_entry)))
145 #define NET_TX_RING_SIZE __NET_RING_SIZE(netif_tx, PAGE_SIZE)
146 #define NET_RX_RING_SIZE __NET_RING_SIZE(netif_rx, PAGE_SIZE)
148 #pragma warning(disable: 4127) // conditional expression is constant
150 #define MIN_LARGE_SEND_SEGMENTS 4
152 /* TODO: crank this up if we support higher mtus? */
153 #define XN_HDR_SIZE 14
154 #define XN_MAX_DATA_SIZE 1500
155 #define XN_MIN_FRAME_SIZE 60
156 #define XN_MAX_FRAME_SIZE (XN_HDR_SIZE + XN_DATA_SIZE)
157 /*
158 #if !defined(OFFLOAD_LARGE_SEND)
159 #define XN_MAX_PKT_SIZE (XN_HDR_SIZE + XN_DATA_SIZE)
160 #else
161 #define XN_MAX_PKT_SIZE MAX_LARGE_SEND_OFFLOAD
162 #endif
163 */
165 #define XN_MAX_SEND_PKTS 16
167 #define XENSOURCE_MAC_HDR 0x00163E
168 #define XN_VENDOR_DESC "Xensource"
169 #define MAX_XENBUS_STR_LEN 128
171 //#define RX_MIN_TARGET 8
172 #define RX_DEFAULT_TARGET 256
173 //#define RX_MAX_TARGET min(NET_RX_RING_SIZE, 256)
174 #define RX_MAX_PB_FREELIST (NET_RX_RING_SIZE * 4)
175 #define RX_PACKET_MAX (NET_RX_RING_SIZE * 4)
176 #define RX_PACKET_HIGH_WATER_MARK (RX_PACKET_MAX * 3 / 4)
178 //#define MAX_BUFFERS_PER_PACKET NET_RX_RING_SIZE
180 #define MIN_ETH_HEADER_LENGTH 14
181 #define MAX_ETH_HEADER_LENGTH 14
182 #define MIN_IP4_HEADER_LENGTH 20
183 #define MAX_IP4_HEADER_LENGTH (15 * 4)
184 #define MIN_TCP_HEADER_LENGTH 20
185 #define MAX_TCP_HEADER_LENGTH (15 * 4)
186 #define MAX_PKT_HEADER_LENGTH (MAX_ETH_HEADER_LENGTH + MAX_IP4_HEADER_LENGTH + MAX_TCP_HEADER_LENGTH)
188 #define MIN_LOOKAHEAD_LENGTH (MAX_IP4_HEADER_LENGTH + MAX_TCP_HEADER_LENGTH)
189 #define MAX_LOOKAHEAD_LENGTH PAGE_SIZE
191 #define LINUX_MAX_SG_ELEMENTS 18
193 #define PAGE_LIST_SIZE (max(NET_RX_RING_SIZE, NET_TX_RING_SIZE) * 4)
194 #define MULTICAST_LIST_MAX_SIZE 32
196 #define TX_HEADER_BUFFER_SIZE 512
197 #define TX_COALESCE_BUFFERS (NET_TX_RING_SIZE)
199 /* split incoming large packets into MSS sized chunks */
200 #define RX_LSO_SPLIT_MSS 0
201 /* split incoming large packets in half, to not invoke the delayed ack timer */
202 #define RX_LSO_SPLIT_HALF 1
203 /* don't split incoming large packets. not really useful */
204 #define RX_LSO_SPLIT_NONE 2
206 #define DEVICE_STATE_DISCONNECTED 0 /* -> INITIALISING */
207 #define DEVICE_STATE_INITIALISING 1 /* -> ACTIVE or INACTIVE */
208 #define DEVICE_STATE_INACTIVE 2
209 #define DEVICE_STATE_ACTIVE 3 /* -> DISCONNECTING */
210 #define DEVICE_STATE_DISCONNECTING 4 /* -> DISCONNECTED */
212 struct _shared_buffer_t;
214 typedef struct _shared_buffer_t shared_buffer_t;
216 struct _shared_buffer_t {
217 struct netif_rx_response rsp;
218 shared_buffer_t *next;
219 grant_ref_t gref;
220 //USHORT offset;
221 PVOID virtual;
222 PMDL mdl;
223 //USHORT id;
224 volatile LONG ref_count;
225 };
227 typedef struct {
228 #if NTDDI_VERSION < NTDDI_VISTA
229 PNDIS_PACKET packet; /* only set on the last packet */
230 #else
231 PNET_BUFFER packet; /* only set on the last packet */
232 #endif
233 PVOID *cb;
234 grant_ref_t gref;
235 } tx_shadow_t;
237 typedef struct {
238 ULONG parse_result;
239 PMDL first_mdl;
240 MDL first_mdl_storage;
241 PPFN_NUMBER first_mdl_pfns[17]; /* maximum possible packet size */
242 PMDL curr_mdl;
243 shared_buffer_t *first_pb;
244 shared_buffer_t *curr_pb;
245 PUCHAR first_mdl_virtual;
246 //ULONG mdl_count;
247 ULONG first_mdl_offset;
248 ULONG first_mdl_length;
249 ULONG curr_mdl_offset;
250 USHORT mss;
251 //NDIS_TCP_IP_CHECKSUM_PACKET_INFO csum_info;
252 BOOLEAN csum_blank;
253 BOOLEAN data_validated;
254 BOOLEAN split_required;
255 UCHAR ip_version;
256 PUCHAR header;
257 ULONG header_length;
258 UCHAR ip_proto;
259 BOOLEAN ip_has_options;
260 ULONG total_length;
261 USHORT ip4_header_length;
262 USHORT ip4_length;
263 USHORT tcp_header_length;
264 BOOLEAN tcp_has_options;
265 USHORT tcp_length;
266 USHORT tcp_remaining;
267 ULONG tcp_seq;
268 BOOLEAN is_multicast;
269 BOOLEAN is_broadcast;
270 /* anything past here doesn't get cleared automatically by the ClearPacketInfo */
271 UCHAR header_data[MAX_LOOKAHEAD_LENGTH + MAX_ETH_HEADER_LENGTH];
272 } packet_info_t;
274 struct xennet_info
275 {
276 ULONG device_state;
278 /* Base device vars */
279 PDEVICE_OBJECT pdo;
280 PDEVICE_OBJECT fdo;
281 PDEVICE_OBJECT lower_do;
282 // WCHAR dev_desc[NAME_SIZE];
284 /* NDIS-related vars */
285 NDIS_HANDLE adapter_handle;
286 ULONG packet_filter;
287 uint8_t perm_mac_addr[ETH_ALEN];
288 uint8_t curr_mac_addr[ETH_ALEN];
289 ULONG current_lookahead;
291 /* Misc. Xen vars */
292 XN_HANDLE handle;
294 evtchn_port_t event_channel;
295 ULONG backend_state;
296 KEVENT backend_event;
297 UCHAR multicast_list[MULTICAST_LIST_MAX_SIZE][6];
298 ULONG multicast_list_size;
299 KDPC rxtx_dpc;
301 /* tx related - protected by tx_lock */
302 KSPIN_LOCK tx_lock; /* always acquire rx_lock before tx_lock */
303 LIST_ENTRY tx_waiting_pkt_list;
304 netif_tx_sring_t *tx_sring;
305 grant_ref_t tx_sring_gref;
306 struct netif_tx_front_ring tx_ring;
307 ULONG tx_ring_free;
308 tx_shadow_t tx_shadows[NET_TX_RING_SIZE];
309 ULONG tx_outstanding;
310 ULONG tx_id_free;
311 USHORT tx_id_list[NET_TX_RING_SIZE];
312 NPAGED_LOOKASIDE_LIST tx_lookaside_list;
313 KEVENT tx_idle_event;
315 /* rx_related - protected by rx_lock */
316 KSPIN_LOCK rx_lock; /* always acquire rx_lock before tx_lock */
317 netif_rx_sring_t *rx_sring;
318 grant_ref_t rx_sring_gref;
319 struct netif_rx_front_ring rx_ring;
320 ULONG rx_id_free;
321 packet_info_t *rxpi;
322 #if NTDDI_VERSION < NTDDI_VISTA
323 #else
324 NDIS_HANDLE rx_nbl_pool;
325 #endif
326 NDIS_HANDLE rx_packet_pool;
327 volatile LONG rx_pb_free;
328 struct stack_state *rx_pb_stack;
329 volatile LONG rx_hb_free;
330 struct stack_state *rx_hb_stack;
331 shared_buffer_t *rx_ring_pbs[NET_RX_RING_SIZE];
332 /* Receive-ring batched refills. */
333 ULONG rx_target;
334 ULONG rx_max_target;
335 ULONG rx_min_target;
336 shared_buffer_t *rx_partial_buf;
337 BOOLEAN rx_partial_extra_info_flag ;
338 BOOLEAN rx_partial_more_data_flag;
339 KEVENT rx_idle_event;
340 /* how many packets are in the net stack atm */
341 LONG rx_outstanding;
344 /* config vars from registry */
345 /* the frontend_* indicate our willingness to support */
346 BOOLEAN frontend_sg_supported;
347 BOOLEAN frontend_csum_supported;
348 ULONG frontend_gso_value;
349 ULONG frontend_mtu_value;
350 ULONG frontend_gso_rx_split_type; /* RX_LSO_SPLIT_* */
352 BOOLEAN backend_sg_supported;
353 BOOLEAN backend_csum_supported;
354 ULONG backend_gso_value;
356 BOOLEAN current_sg_supported;
357 BOOLEAN current_csum_supported;
358 ULONG current_gso_value;
359 ULONG current_mtu_value;
360 ULONG current_gso_rx_split_type;
362 BOOLEAN config_csum_rx_check;
363 BOOLEAN config_csum_rx_dont_fix;
365 #if NTDDI_VERSION < NTDDI_VISTA
366 NDIS_TASK_TCP_IP_CHECKSUM setting_csum;
367 #else
368 #endif
370 /* config stuff calculated from the above */
371 ULONG config_max_pkt_size;
373 /* stats */
374 #if NTDDI_VERSION < NTDDI_VISTA
375 ULONG64 stat_tx_ok;
376 ULONG64 stat_rx_ok;
377 ULONG64 stat_tx_error;
378 ULONG64 stat_rx_error;
379 ULONG64 stat_rx_no_buffer;
380 #else
381 NDIS_STATISTICS_INFO stats;
382 #endif
384 } typedef xennet_info_t;
386 extern USHORT ndis_os_major_version;
387 extern USHORT ndis_os_minor_version;
389 typedef NDIS_STATUS (*XEN_OID_REQUEST)(NDIS_HANDLE context, PVOID information_buffer, ULONG information_buffer_length, PULONG bytes_read, PULONG bytes_needed);
391 struct xennet_oids_t {
392 ULONG oid;
393 char *oid_name;
394 ULONG min_length;
395 XEN_OID_REQUEST query_routine;
396 XEN_OID_REQUEST set_routine;
397 };
399 extern struct xennet_oids_t xennet_oids[];
401 #if NTDDI_VERSION < NTDDI_VISTA
402 NDIS_STATUS
403 XenNet_QueryInformation(
404 IN NDIS_HANDLE MiniportAdapterContext,
405 IN NDIS_OID Oid,
406 IN PVOID InformationBuffer,
407 IN ULONG InformationBufferLength,
408 OUT PULONG BytesWritten,
409 OUT PULONG BytesNeeded);
411 NDIS_STATUS
412 XenNet_SetInformation(
413 IN NDIS_HANDLE MiniportAdapterContext,
414 IN NDIS_OID Oid,
415 IN PVOID InformationBuffer,
416 IN ULONG InformationBufferLength,
417 OUT PULONG BytesRead,
418 OUT PULONG BytesNeeded
419 );
421 VOID
422 XenNet_SendPackets(
423 IN NDIS_HANDLE MiniportAdapterContext,
424 IN PPNDIS_PACKET PacketArray,
425 IN UINT NumberOfPackets
426 );
428 VOID
429 XenNet_ReturnPacket(
430 IN NDIS_HANDLE MiniportAdapterContext,
431 IN PNDIS_PACKET Packet
432 );
433 #else
435 MINIPORT_OID_REQUEST XenNet_OidRequest;
436 MINIPORT_CANCEL_OID_REQUEST XenNet_CancelOidRequest;
438 MINIPORT_SEND_NET_BUFFER_LISTS XenNet_SendNetBufferLists;
439 MINIPORT_CANCEL_SEND XenNet_CancelSend;
441 MINIPORT_RETURN_NET_BUFFER_LISTS XenNet_ReturnNetBufferLists;
442 #endif
444 NTSTATUS XenNet_Connect(PVOID context, BOOLEAN suspend);
445 NTSTATUS XenNet_Disconnect(PVOID context, BOOLEAN suspend);
446 VOID XenNet_DeviceCallback(PVOID context, ULONG callback_type, PVOID value);
449 BOOLEAN XenNet_RxInit(xennet_info_t *xi);
450 VOID XenNet_RxShutdown(xennet_info_t *xi);
451 BOOLEAN XenNet_RxBufferCheck(struct xennet_info *xi);
453 BOOLEAN XenNet_TxInit(xennet_info_t *xi);
454 BOOLEAN XenNet_TxShutdown(xennet_info_t *xi);
455 VOID XenNet_TxBufferGC(struct xennet_info *xi, BOOLEAN dont_set_event);
458 /* return values */
459 #define PARSE_OK 0
460 #define PARSE_TOO_SMALL 1 /* first buffer is too small */
461 #define PARSE_UNKNOWN_TYPE 2
463 BOOLEAN XenNet_BuildHeader(packet_info_t *pi, PVOID header, ULONG new_header_size);
464 VOID XenNet_ParsePacketHeader(packet_info_t *pi, PUCHAR buffer, ULONG min_header_size);
465 BOOLEAN XenNet_FilterAcceptPacket(struct xennet_info *xi, packet_info_t *pi);
467 BOOLEAN XenNet_CheckIpHeaderSum(PUCHAR header, USHORT ip4_header_length);
468 VOID XenNet_SumIpHeader(PUCHAR header, USHORT ip4_header_length);
470 static __forceinline VOID
471 XenNet_ClearPacketInfo(packet_info_t *pi) {
472 RtlZeroMemory(pi, sizeof(packet_info_t) - FIELD_OFFSET(packet_info_t, header_data));
473 }
475 /* Get some data from the current packet, but don't cross a page boundry. */
476 static __forceinline ULONG
477 XenNet_QueryData(packet_info_t *pi, ULONG length) {
478 ULONG offset_in_page;
480 if (length > MmGetMdlByteCount(pi->curr_mdl) - pi->curr_mdl_offset)
481 length = MmGetMdlByteCount(pi->curr_mdl) - pi->curr_mdl_offset;
483 offset_in_page = (MmGetMdlByteOffset(pi->curr_mdl) + pi->curr_mdl_offset) & (PAGE_SIZE - 1);
484 if (offset_in_page + length > PAGE_SIZE)
485 length = PAGE_SIZE - offset_in_page;
487 return length;
488 }
490 /* Move the pointers forward by the given amount. No error checking is done. */
491 static __forceinline VOID
492 XenNet_EatData(packet_info_t *pi, ULONG length) {
493 pi->curr_mdl_offset += length;
494 if (pi->curr_mdl_offset >= MmGetMdlByteCount(pi->curr_mdl)) {
495 pi->curr_mdl_offset -= MmGetMdlByteCount(pi->curr_mdl);
496 #if NTDDI_VERSION < NTDDI_VISTA
497 NdisGetNextBuffer(pi->curr_mdl, &pi->curr_mdl);
498 #else
499 NdisGetNextMdl(pi->curr_mdl, &pi->curr_mdl);
500 #endif
501 }
502 }