win-pvdrivers

view xennet/xennet_common.c @ 538:e75bb8d68370

Lots more changes...
author James Harper <james.harper@bendigoit.com.au>
date Tue Mar 03 09:51:24 2009 +1100 (2009-03-03)
parents 1d39de3ab8d6
children 2447875396d0
line source
1 /*
2 PV Net Driver 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 #include "xennet.h"
23 /*
24 Increase the header to a certain size
25 */
27 BOOLEAN
28 XenNet_BuildHeader(packet_info_t *pi, ULONG new_header_size)
29 {
30 ULONG bytes_remaining;
32 //FUNCTION_ENTER();
34 if (new_header_size <= pi->header_length)
35 {
36 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " new_header_size <= pi->header_length\n"));
37 return TRUE;
38 }
40 if (new_header_size > ARRAY_SIZE(pi->header_data))
41 {
42 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " new_header_size > ARRAY_SIZE(pi->header_data)\n"));
43 return FALSE;
44 }
46 if (new_header_size <= pi->first_buffer_length)
47 {
48 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " new_header_size <= pi->first_buffer_length\n"));
49 pi->header_length = new_header_size;
50 if (pi->header_length == pi->first_buffer_length)
51 {
52 NdisGetNextBuffer(pi->curr_buffer, &pi->curr_buffer);
53 pi->curr_mdl_offset = 0;
54 }
55 else
56 {
57 pi->curr_mdl_offset = (USHORT)new_header_size;
58 if (pi->curr_pb)
59 pi->curr_pb = pi->curr_pb->next;
60 }
62 return TRUE;
63 }
64 else if (pi->header != pi->header_data)
65 {
66 //KdPrint((__DRIVER_NAME " Switching to header_data\n"));
67 memcpy(pi->header_data, pi->header, pi->header_length);
68 pi->header = pi->header_data;
69 }
71 bytes_remaining = new_header_size - pi->header_length;
73 //KdPrint((__DRIVER_NAME " A bytes_remaining = %d, pi->curr_buffer = %p, pi->mdl_count = %d\n", bytes_remaining, pi->curr_buffer, pi->mdl_count));
74 while (bytes_remaining && pi->curr_buffer)
75 {
76 ULONG copy_size;
78 ASSERT(pi->curr_buffer);
79 //KdPrint((__DRIVER_NAME " B bytes_remaining = %d, pi->curr_buffer = %p, pi->mdl_count = %d\n", bytes_remaining, pi->curr_buffer, pi->mdl_count));
80 if (MmGetMdlByteCount(pi->curr_buffer))
81 {
82 copy_size = min(bytes_remaining, MmGetMdlByteCount(pi->curr_buffer) - pi->curr_mdl_offset);
83 //KdPrint((__DRIVER_NAME " B copy_size = %d\n", copy_size));
84 memcpy(pi->header + pi->header_length,
85 (PUCHAR)MmGetMdlVirtualAddress(pi->curr_buffer) + pi->curr_mdl_offset, copy_size);
86 pi->curr_mdl_offset = (USHORT)(pi->curr_mdl_offset + copy_size);
87 pi->header_length += copy_size;
88 bytes_remaining -= copy_size;
89 }
90 if (pi->curr_mdl_offset == MmGetMdlByteCount(pi->curr_buffer))
91 {
92 NdisGetNextBuffer(pi->curr_buffer, &pi->curr_buffer);
93 if (pi->curr_pb)
94 pi->curr_pb = pi->curr_pb->next;
95 pi->curr_mdl_offset = 0;
96 }
97 }
98 //KdPrint((__DRIVER_NAME " C bytes_remaining = %d, pi->curr_buffer = %p, pi->mdl_count = %d\n", bytes_remaining, pi->curr_buffer, pi->mdl_count));
99 if (bytes_remaining)
100 {
101 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " bytes_remaining\n"));
102 return FALSE;
103 }
104 //FUNCTION_EXIT();
105 return TRUE;
106 }
108 ULONG
109 XenNet_ParsePacketHeader(packet_info_t *pi)
110 {
111 //FUNCTION_ENTER();
113 ASSERT(pi->first_buffer);
115 NdisQueryBufferSafe(pi->first_buffer, (PVOID)&pi->header, &pi->first_buffer_length, NormalPagePriority);
116 pi->curr_buffer = pi->first_buffer;
118 pi->header_length = 0;
119 pi->curr_mdl_offset = 0;
121 if (!XenNet_BuildHeader(pi, (ULONG)XN_HDR_SIZE))
122 {
123 KdPrint((__DRIVER_NAME " packet too small (Ethernet Header)\n"));
124 return PARSE_TOO_SMALL;
125 }
127 switch (GET_NET_PUSHORT(&pi->header[12])) // L2 protocol field
128 {
129 case 0x0800:
130 //KdPrint((__DRIVER_NAME " IP\n"));
131 if (pi->header_length < (ULONG)(XN_HDR_SIZE + 20))
132 {
133 if (!XenNet_BuildHeader(pi, (ULONG)(XN_HDR_SIZE + 20)))
134 {
135 KdPrint((__DRIVER_NAME " packet too small (IP Header)\n"));
136 return PARSE_TOO_SMALL;
137 }
138 }
139 pi->ip_version = (pi->header[XN_HDR_SIZE + 0] & 0xF0) >> 4;
140 if (pi->ip_version != 4)
141 {
142 KdPrint((__DRIVER_NAME " ip_version = %d\n", pi->ip_version));
143 return PARSE_UNKNOWN_TYPE;
144 }
145 pi->ip4_header_length = (pi->header[XN_HDR_SIZE + 0] & 0x0F) << 2;
146 if (pi->header_length < (ULONG)(XN_HDR_SIZE + 20 + pi->ip4_header_length))
147 {
148 if (!XenNet_BuildHeader(pi, (ULONG)(XN_HDR_SIZE + pi->ip4_header_length + 20)))
149 {
150 //KdPrint((__DRIVER_NAME " packet too small (IP Header + IP Options + TCP Header)\n"));
151 return PARSE_TOO_SMALL;
152 }
153 }
154 break;
155 default:
156 //KdPrint((__DRIVER_NAME " Not IP (%d)\n", GET_NET_PUSHORT(&pi->header[12])));
157 return PARSE_UNKNOWN_TYPE;
158 }
159 pi->ip_proto = pi->header[XN_HDR_SIZE + 9];
160 switch (pi->ip_proto)
161 {
162 case 6: // TCP
163 case 17: // UDP
164 break;
165 default:
166 //KdPrint((__DRIVER_NAME " Not TCP/UDP (%d)\n", pi->ip_proto));
167 return PARSE_UNKNOWN_TYPE;
168 }
169 pi->ip4_length = GET_NET_PUSHORT(&pi->header[XN_HDR_SIZE + 2]);
170 pi->tcp_header_length = (pi->header[XN_HDR_SIZE + pi->ip4_header_length + 12] & 0xf0) >> 2;
172 if (pi->header_length < (ULONG)(XN_HDR_SIZE + pi->ip4_header_length + pi->tcp_header_length))
173 {
174 if (!XenNet_BuildHeader(pi, (ULONG)(XN_HDR_SIZE + pi->ip4_header_length + pi->tcp_header_length)))
175 {
176 //KdPrint((__DRIVER_NAME " packet too small (IP Header + IP Options + TCP Header + TCP Options)\n"));
177 return PARSE_TOO_SMALL;
178 }
179 }
181 pi->tcp_length = pi->ip4_length - pi->ip4_header_length - pi->tcp_header_length;
182 pi->tcp_remaining = pi->tcp_length;
183 pi->tcp_seq = GET_NET_PULONG(&pi->header[XN_HDR_SIZE + pi->ip4_header_length + 4]);
184 pi->tcp_has_options = (BOOLEAN)(pi->tcp_header_length > 20);
185 if (pi->mss > 0 && pi->tcp_length > pi->mss)
186 pi->split_required = TRUE;
188 //KdPrint((__DRIVER_NAME " ip4_length = %d\n", pi->ip4_length));
189 //KdPrint((__DRIVER_NAME " tcp_length = %d\n", pi->tcp_length));
190 //FUNCTION_EXIT();
192 return PARSE_OK;
193 }
195 VOID
196 XenNet_SumIpHeader(
197 PUCHAR header,
198 USHORT ip4_header_length
199 )
200 {
201 ULONG csum = 0;
202 USHORT i;
204 ASSERT(ip4_header_length > 12);
205 ASSERT(!(ip4_header_length & 1));
207 header[XN_HDR_SIZE + 10] = 0;
208 header[XN_HDR_SIZE + 11] = 0;
209 for (i = 0; i < ip4_header_length; i += 2)
210 {
211 csum += GET_NET_PUSHORT(&header[XN_HDR_SIZE + i]);
212 }
213 while (csum & 0xFFFF0000)
214 csum = (csum & 0xFFFF) + (csum >> 16);
215 csum = ~csum;
216 SET_NET_USHORT(&header[XN_HDR_SIZE + 10], (USHORT)csum);
217 }