win-pvdrivers

view xennet/xennet.h @ 1048:fba0ce4d9e54

Fix checksum problem on lso for xp/2003. Tidy up.
author James Harper <james.harper@bendigoit.com.au>
date Mon May 13 21:14:35 2013 +1000 (2013-05-13)
parents c21a4feb4a27
children 2ef536c2d9fe
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_DFL_MIN_TARGET 256
173 #define RX_MAX_TARGET min(NET_RX_RING_SIZE, 256)
174 #define RX_MAX_PB_FREELIST (RX_MAX_TARGET * 4)
176 //#define MAX_BUFFERS_PER_PACKET NET_RX_RING_SIZE
178 #define MIN_ETH_HEADER_LENGTH 14
179 #define MAX_ETH_HEADER_LENGTH 14
180 #define MIN_IP4_HEADER_LENGTH 20
181 #define MAX_IP4_HEADER_LENGTH (15 * 4)
182 #define MIN_TCP_HEADER_LENGTH 20
183 #define MAX_TCP_HEADER_LENGTH (15 * 4)
184 #define MAX_PKT_HEADER_LENGTH (MAX_ETH_HEADER_LENGTH + MAX_IP4_HEADER_LENGTH + MAX_TCP_HEADER_LENGTH)
186 #define MIN_LOOKAHEAD_LENGTH (MAX_IP4_HEADER_LENGTH + MAX_TCP_HEADER_LENGTH)
187 #define MAX_LOOKAHEAD_LENGTH PAGE_SIZE
189 #define LINUX_MAX_SG_ELEMENTS 18
191 #define PAGE_LIST_SIZE (max(NET_RX_RING_SIZE, NET_TX_RING_SIZE) * 4)
192 #define MULTICAST_LIST_MAX_SIZE 32
194 #define TX_HEADER_BUFFER_SIZE 512
195 #define TX_COALESCE_BUFFERS (NET_TX_RING_SIZE)
197 /* split incoming large packets into MSS sized chunks */
198 #define RX_LSO_SPLIT_MSS 0
199 /* split incoming large packets in half, to not invoke the delayed ack timer */
200 #define RX_LSO_SPLIT_HALF 1
201 /* don't split incoming large packets. not really useful */
202 #define RX_LSO_SPLIT_NONE 2
204 #define DEVICE_STATE_DISCONNECTED 0 /* -> INITIALISING */
205 #define DEVICE_STATE_INITIALISING 1 /* -> ACTIVE or INACTIVE */
206 #define DEVICE_STATE_INACTIVE 2
207 #define DEVICE_STATE_ACTIVE 3 /* -> DISCONNECTING */
208 #define DEVICE_STATE_DISCONNECTING 4 /* -> DISCONNECTED */
210 struct _shared_buffer_t;
212 typedef struct _shared_buffer_t shared_buffer_t;
214 struct _shared_buffer_t {
215 struct netif_rx_response rsp;
216 shared_buffer_t *next;
217 grant_ref_t gref;
218 //USHORT offset;
219 PVOID virtual;
220 PMDL mdl;
221 //USHORT id;
222 volatile LONG ref_count;
223 };
225 typedef struct {
226 #if NTDDI_VERSION < NTDDI_VISTA
227 PNDIS_PACKET packet; /* only set on the last packet */
228 #else
229 PNET_BUFFER packet; /* only set on the last packet */
230 #endif
231 PVOID *cb;
232 grant_ref_t gref;
233 } tx_shadow_t;
235 typedef struct {
236 ULONG parse_result;
237 PMDL first_mdl;
238 MDL first_mdl_storage;
239 PPFN_NUMBER first_mdl_pfns[17]; /* maximum possible packet size */
240 PMDL curr_mdl;
241 shared_buffer_t *first_pb;
242 shared_buffer_t *curr_pb;
243 PUCHAR first_mdl_virtual;
244 //ULONG mdl_count;
245 ULONG first_mdl_offset;
246 ULONG first_mdl_length;
247 ULONG curr_mdl_offset;
248 USHORT mss;
249 //NDIS_TCP_IP_CHECKSUM_PACKET_INFO csum_info;
250 BOOLEAN csum_blank;
251 BOOLEAN data_validated;
252 BOOLEAN split_required;
253 UCHAR ip_version;
254 PUCHAR header;
255 ULONG header_length;
256 UCHAR ip_proto;
257 BOOLEAN ip_has_options;
258 ULONG total_length;
259 USHORT ip4_header_length;
260 USHORT ip4_length;
261 USHORT tcp_header_length;
262 BOOLEAN tcp_has_options;
263 USHORT tcp_length;
264 USHORT tcp_remaining;
265 ULONG tcp_seq;
266 BOOLEAN is_multicast;
267 BOOLEAN is_broadcast;
268 /* anything past here doesn't get cleared automatically by the ClearPacketInfo */
269 UCHAR header_data[MAX_LOOKAHEAD_LENGTH + MAX_ETH_HEADER_LENGTH];
270 } packet_info_t;
272 struct xennet_info
273 {
274 ULONG device_state;
276 /* Base device vars */
277 PDEVICE_OBJECT pdo;
278 PDEVICE_OBJECT fdo;
279 PDEVICE_OBJECT lower_do;
280 // WCHAR dev_desc[NAME_SIZE];
282 /* NDIS-related vars */
283 NDIS_HANDLE adapter_handle;
284 ULONG packet_filter;
285 uint8_t perm_mac_addr[ETH_ALEN];
286 uint8_t curr_mac_addr[ETH_ALEN];
287 ULONG current_lookahead;
289 /* Misc. Xen vars */
290 XN_HANDLE handle;
292 evtchn_port_t event_channel;
293 ULONG backend_state;
294 KEVENT backend_event;
295 UCHAR multicast_list[MULTICAST_LIST_MAX_SIZE][6];
296 ULONG multicast_list_size;
297 KDPC rxtx_dpc;
299 /* tx related - protected by tx_lock */
300 KSPIN_LOCK tx_lock; /* always acquire rx_lock before tx_lock */
301 LIST_ENTRY tx_waiting_pkt_list;
302 netif_tx_sring_t *tx_sring;
303 grant_ref_t tx_sring_gref;
304 struct netif_tx_front_ring tx_ring;
305 ULONG tx_ring_free;
306 tx_shadow_t tx_shadows[NET_TX_RING_SIZE];
307 ULONG tx_outstanding;
308 ULONG tx_id_free;
309 USHORT tx_id_list[NET_TX_RING_SIZE];
310 NPAGED_LOOKASIDE_LIST tx_lookaside_list;
311 KEVENT tx_idle_event;
313 /* rx_related - protected by rx_lock */
314 KSPIN_LOCK rx_lock; /* always acquire rx_lock before tx_lock */
315 netif_rx_sring_t *rx_sring;
316 grant_ref_t rx_sring_gref;
317 struct netif_rx_front_ring rx_ring;
318 ULONG rx_id_free;
319 packet_info_t *rxpi;
320 #if NTDDI_VERSION < NTDDI_VISTA
321 #else
322 NDIS_HANDLE rx_nbl_pool;
323 #endif
324 NDIS_HANDLE rx_packet_pool;
325 volatile LONG rx_pb_free;
326 struct stack_state *rx_pb_stack;
327 volatile LONG rx_hb_free;
328 struct stack_state *rx_hb_stack;
329 shared_buffer_t *rx_ring_pbs[NET_RX_RING_SIZE];
330 /* Receive-ring batched refills. */
331 ULONG rx_target;
332 ULONG rx_max_target;
333 ULONG rx_min_target;
334 shared_buffer_t *rx_partial_buf;
335 BOOLEAN rx_partial_extra_info_flag ;
336 BOOLEAN rx_partial_more_data_flag;
337 KEVENT rx_idle_event;
338 /* how many packets are in the net stack atm */
339 LONG rx_outstanding;
342 /* config vars from registry */
343 /* the frontend_* indicate our willingness to support */
344 BOOLEAN frontend_sg_supported;
345 BOOLEAN frontend_csum_supported;
346 ULONG frontend_gso_value;
347 ULONG frontend_mtu_value;
348 ULONG frontend_gso_rx_split_type; /* RX_LSO_SPLIT_* */
350 BOOLEAN backend_sg_supported;
351 BOOLEAN backend_csum_supported;
352 ULONG backend_gso_value;
354 BOOLEAN current_sg_supported;
355 BOOLEAN current_csum_supported;
356 ULONG current_gso_value;
357 ULONG current_mtu_value;
358 ULONG current_gso_rx_split_type;
360 BOOLEAN config_csum_rx_check;
361 BOOLEAN config_csum_rx_dont_fix;
363 #if NTDDI_VERSION < NTDDI_VISTA
364 NDIS_TASK_TCP_IP_CHECKSUM setting_csum;
365 #else
366 #endif
368 /* config stuff calculated from the above */
369 ULONG config_max_pkt_size;
371 /* stats */
372 #if NTDDI_VERSION < NTDDI_VISTA
373 ULONG64 stat_tx_ok;
374 ULONG64 stat_rx_ok;
375 ULONG64 stat_tx_error;
376 ULONG64 stat_rx_error;
377 ULONG64 stat_rx_no_buffer;
378 #else
379 NDIS_STATISTICS_INFO stats;
380 #endif
382 } typedef xennet_info_t;
384 extern USHORT ndis_os_major_version;
385 extern USHORT ndis_os_minor_version;
387 typedef NDIS_STATUS (*XEN_OID_REQUEST)(NDIS_HANDLE context, PVOID information_buffer, ULONG information_buffer_length, PULONG bytes_read, PULONG bytes_needed);
389 struct xennet_oids_t {
390 ULONG oid;
391 char *oid_name;
392 ULONG min_length;
393 XEN_OID_REQUEST query_routine;
394 XEN_OID_REQUEST set_routine;
395 };
397 extern struct xennet_oids_t xennet_oids[];
399 #if NTDDI_VERSION < NTDDI_VISTA
400 NDIS_STATUS
401 XenNet_QueryInformation(
402 IN NDIS_HANDLE MiniportAdapterContext,
403 IN NDIS_OID Oid,
404 IN PVOID InformationBuffer,
405 IN ULONG InformationBufferLength,
406 OUT PULONG BytesWritten,
407 OUT PULONG BytesNeeded);
409 NDIS_STATUS
410 XenNet_SetInformation(
411 IN NDIS_HANDLE MiniportAdapterContext,
412 IN NDIS_OID Oid,
413 IN PVOID InformationBuffer,
414 IN ULONG InformationBufferLength,
415 OUT PULONG BytesRead,
416 OUT PULONG BytesNeeded
417 );
419 VOID
420 XenNet_SendPackets(
421 IN NDIS_HANDLE MiniportAdapterContext,
422 IN PPNDIS_PACKET PacketArray,
423 IN UINT NumberOfPackets
424 );
426 VOID
427 XenNet_ReturnPacket(
428 IN NDIS_HANDLE MiniportAdapterContext,
429 IN PNDIS_PACKET Packet
430 );
431 #else
433 MINIPORT_OID_REQUEST XenNet_OidRequest;
434 MINIPORT_CANCEL_OID_REQUEST XenNet_CancelOidRequest;
436 MINIPORT_SEND_NET_BUFFER_LISTS XenNet_SendNetBufferLists;
437 MINIPORT_CANCEL_SEND XenNet_CancelSend;
439 MINIPORT_RETURN_NET_BUFFER_LISTS XenNet_ReturnNetBufferLists;
440 #endif
442 NTSTATUS XenNet_Connect(PVOID context, BOOLEAN suspend);
443 NTSTATUS XenNet_Disconnect(PVOID context, BOOLEAN suspend);
444 VOID XenNet_DeviceCallback(PVOID context, ULONG callback_type, PVOID value);
447 BOOLEAN XenNet_RxInit(xennet_info_t *xi);
448 VOID XenNet_RxShutdown(xennet_info_t *xi);
449 BOOLEAN XenNet_RxBufferCheck(struct xennet_info *xi);
451 BOOLEAN XenNet_TxInit(xennet_info_t *xi);
452 BOOLEAN XenNet_TxShutdown(xennet_info_t *xi);
453 VOID XenNet_TxBufferGC(struct xennet_info *xi, BOOLEAN dont_set_event);
456 /* return values */
457 #define PARSE_OK 0
458 #define PARSE_TOO_SMALL 1 /* first buffer is too small */
459 #define PARSE_UNKNOWN_TYPE 2
461 BOOLEAN XenNet_BuildHeader(packet_info_t *pi, PVOID header, ULONG new_header_size);
462 VOID XenNet_ParsePacketHeader(packet_info_t *pi, PUCHAR buffer, ULONG min_header_size);
463 BOOLEAN XenNet_FilterAcceptPacket(struct xennet_info *xi, packet_info_t *pi);
465 BOOLEAN XenNet_CheckIpHeaderSum(PUCHAR header, USHORT ip4_header_length);
466 VOID XenNet_SumIpHeader(PUCHAR header, USHORT ip4_header_length);
468 static __forceinline VOID
469 XenNet_ClearPacketInfo(packet_info_t *pi) {
470 RtlZeroMemory(pi, sizeof(packet_info_t) - FIELD_OFFSET(packet_info_t, header_data));
471 }
473 /* Get some data from the current packet, but don't cross a page boundry. */
474 static __forceinline ULONG
475 XenNet_QueryData(packet_info_t *pi, ULONG length) {
476 ULONG offset_in_page;
478 if (length > MmGetMdlByteCount(pi->curr_mdl) - pi->curr_mdl_offset)
479 length = MmGetMdlByteCount(pi->curr_mdl) - pi->curr_mdl_offset;
481 offset_in_page = (MmGetMdlByteOffset(pi->curr_mdl) + pi->curr_mdl_offset) & (PAGE_SIZE - 1);
482 if (offset_in_page + length > PAGE_SIZE)
483 length = PAGE_SIZE - offset_in_page;
485 return length;
486 }
488 /* Move the pointers forward by the given amount. No error checking is done. */
489 static __forceinline VOID
490 XenNet_EatData(packet_info_t *pi, ULONG length) {
491 pi->curr_mdl_offset += length;
492 if (pi->curr_mdl_offset >= MmGetMdlByteCount(pi->curr_mdl)) {
493 pi->curr_mdl_offset -= MmGetMdlByteCount(pi->curr_mdl);
494 #if NTDDI_VERSION < NTDDI_VISTA
495 NdisGetNextBuffer(pi->curr_mdl, &pi->curr_mdl);
496 #else
497 NdisGetNextMdl(pi->curr_mdl, &pi->curr_mdl);
498 #endif
499 }
500 }