Fix function name in debug message.
[mirror/winof/.git] / ulp / wsd / user / ibsp_duplicate.c
1 /*\r
2  * Copyright (c) 2005 SilverStorm Technologies.  All rights reserved.\r
3  *\r
4  * This software is available to you under the OpenIB.org BSD license\r
5  * below:\r
6  *\r
7  *     Redistribution and use in source and binary forms, with or\r
8  *     without modification, are permitted provided that the following\r
9  *     conditions are met:\r
10  *\r
11  *      - Redistributions of source code must retain the above\r
12  *        copyright notice, this list of conditions and the following\r
13  *        disclaimer.\r
14  *\r
15  *      - Redistributions in binary form must reproduce the above\r
16  *        copyright notice, this list of conditions and the following\r
17  *        disclaimer in the documentation and/or other materials\r
18  *        provided with the distribution.\r
19  *\r
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
27  * SOFTWARE.\r
28  *\r
29  * $Id$\r
30  */\r
31 \r
32 #include "ibspdll.h"\r
33 #include "rpc.h"\r
34 \r
35 /* \r
36 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/using_shared_memory_in_a_dynamic_link_library.asp\r
37 */\r
38 \r
39 \r
40 static void\r
41 create_name(\r
42                 OUT                     char                                            *fname,\r
43         IN              const   DWORD                                           dwProcessId,\r
44         IN              const   DWORD                                           identifier )\r
45 {\r
46         sprintf( fname, "SilverStorm-WSD-%08lx-%08lx", dwProcessId, identifier );\r
47 }\r
48 \r
49 \r
50 /* Create a duplicated socket. param is given by the other process through the \r
51  * lpProtocolInfo->dwProviderReserved field.\r
52  * This function is called by the next-controlling process. */\r
53 int\r
54 setup_duplicate_socket(\r
55         IN                              struct ibsp_socket_info         *socket_info,\r
56         IN                              DWORD                                           identifier )\r
57 {\r
58         char fname[100];\r
59         HANDLE h = NULL;\r
60         int ret;\r
61         struct ibsp_duplicate_info *dup_info;\r
62         ib_net64_t dest_port_guid;\r
63         ib_path_rec_t path_rec;\r
64 \r
65         CL_ASSERT( socket_info->socket_state == IBSP_CREATE );\r
66 \r
67         fzprint(("%s():%d:0x%x:0x%x: socket=0x%p dwProcessId=0x%x\n", __FUNCTION__,\r
68                          __LINE__, GetCurrentProcessId(), GetCurrentThreadId(), socket_info));\r
69 \r
70 \r
71         /* Find the info created and shared by the previous controlling socket. */\r
72         create_name( fname, GetCurrentProcessId(), identifier );\r
73 \r
74         h = CreateFileMapping( INVALID_HANDLE_VALUE,    // use paging file\r
75                                                   NULL, // default security attributes\r
76                                                   PAGE_READWRITE,       // read access\r
77                                                   0,    // size: high 32-bits\r
78                                                   sizeof(struct ibsp_duplicate_info),   // size: low 32-bits\r
79                                                   fname );      // name of map object\r
80         if( h == NULL )\r
81         {\r
82                 IBSP_ERROR( ("CreateFileMapping failed with %d\n", GetLastError()) );\r
83                 ret = WSAENETDOWN;\r
84                 goto err1;\r
85         }\r
86 \r
87         /* Make sure this file already existed. */\r
88         if( GetLastError() != ERROR_ALREADY_EXISTS )\r
89         {\r
90                 IBSP_ERROR( ("not mapping for socket duplicate info\n") );\r
91                 ret = WSAENETDOWN;\r
92                 goto err1;\r
93         }\r
94 \r
95         /* Get a pointer to the file-mapped shared memory. */\r
96         dup_info = MapViewOfFile( h,    // object to map view of\r
97                                                          FILE_MAP_WRITE,        // read/write access\r
98                                                          0,     // high offset:  map from\r
99                                                          0,     // low offset:   beginning\r
100                                                          0 );   // default: map entire file\r
101         if( dup_info == NULL )\r
102         {\r
103                 IBSP_ERROR( ("MapViewOfFile failed with %d\n", GetLastError()) );\r
104                 ret = WSAENETDOWN;\r
105                 goto err1;\r
106         }\r
107 \r
108         socket_info->peer_addr = dup_info->peer_addr;\r
109         socket_info->local_addr = dup_info->local_addr;\r
110         socket_info->socket_options = dup_info->socket_options;\r
111         socket_info->duplicate.dwProcessId = dup_info->dwProcessId;\r
112         socket_info->duplicate.identifier = identifier;\r
113 \r
114         /* Get the port info */\r
115         /* Find the destination IP address */\r
116         socket_info->port = get_port_from_ip_address( dup_info->local_addr.sin_addr );\r
117         if( socket_info->port == NULL )\r
118         {\r
119                 IBSP_ERROR( ("incoming destination IP address not local (%s)\n",\r
120                         inet_ntoa( dup_info->local_addr.sin_addr )) );\r
121                 ret = WSAENETDOWN;\r
122                 goto err1;\r
123         }\r
124 \r
125         /* Get the GUID for the remote IP address. */\r
126         ret = query_guid_address( socket_info->port,\r
127                 socket_info->peer_addr.sin_addr.S_un.S_addr, &dest_port_guid );\r
128         if( ret )\r
129         {\r
130                 IBSP_ERROR( ("query_guid_address failed for IP %08x\n",\r
131                         socket_info->peer_addr.sin_addr.s_addr) );\r
132                 ret = WSAENETDOWN;\r
133                 goto err1;\r
134         }\r
135 \r
136         /* Get the path record */\r
137         ret = query_pr( socket_info->port, dest_port_guid, &path_rec );\r
138         if( ret )\r
139         {\r
140                 IBSP_ERROR( ("query_pr failed for IP %08x\n",\r
141                         socket_info->peer_addr.sin_addr.s_addr) );\r
142                 ret = WSAENETDOWN;\r
143                 goto err1;\r
144         }\r
145 \r
146         IBSP_CHANGE_SOCKET_STATE( socket_info, IBSP_DUPLICATING_NEW );\r
147         socket_info->h_event = CreateEvent( NULL, FALSE, FALSE, NULL );\r
148         if( !socket_info->h_event )\r
149         {\r
150                 IBSP_ERROR( ("CreateEvent failed (%d)\n", GetLastError()) );\r
151                 goto err1;\r
152         }\r
153 \r
154         ret = ib_create_socket( socket_info );\r
155         if( ret )\r
156         {\r
157                 IBSP_ERROR( ("ib_create socket failed with %d\n", ret) );\r
158                 goto err1;\r
159         }\r
160 \r
161         /* Connects the QP. */\r
162         ret = ib_connect( socket_info, &path_rec );\r
163         if( ret != WSAEWOULDBLOCK )\r
164         {\r
165                 IBSP_ERROR( ("ib_connect failed (%d)\n", ret) );\r
166                 goto err2;\r
167         }\r
168 \r
169         if( WaitForSingleObject( socket_info->h_event, INFINITE ) != WAIT_OBJECT_0 )\r
170                 IBSP_ERROR( ("WaitForSingleObject failed\n") );\r
171 \r
172         cl_spinlock_acquire( &socket_info->mutex );\r
173         if( socket_info->socket_state != IBSP_CONNECTED )\r
174         {\r
175                 cl_spinlock_release( &socket_info->mutex );\r
176                 IBSP_ERROR( ("Failed to connect\n") );\r
177                 ret = WSAENETDOWN;\r
178 err2:\r
179                 ib_destroy_socket( socket_info );\r
180         }\r
181         else\r
182         {\r
183                 cl_spinlock_release( &socket_info->mutex );\r
184         }\r
185 \r
186 err1:\r
187         if( h )\r
188                 CloseHandle( h );\r
189 \r
190         if( socket_info->h_event )\r
191         {\r
192                 CloseHandle( socket_info->h_event );\r
193                 socket_info->h_event = NULL;\r
194         }\r
195 \r
196         IBSP_EXIT( IBSP_DBG_CONN );\r
197         return ret;\r
198 }\r
199 \r
200 \r
201 /*\r
202  * This function is called by the previous controlling process.\r
203  * Called with the socket_info->mutex held.\r
204  */\r
205 int\r
206 prepare_duplicate_socket(\r
207         IN                              struct ibsp_socket_info         *socket_info,\r
208         IN                              DWORD                                           dwProcessId )\r
209 {\r
210         struct ibsp_duplicate_info *dup_info = NULL;\r
211         int i;\r
212         DWORD identifier = 0;\r
213         struct disconnect_reason reason;\r
214 \r
215         fzprint(("%s():%d:0x%x:0x%x: socket=0x%p dwProcessId=0x%x \n", __FUNCTION__,\r
216                          __LINE__, GetCurrentProcessId(),\r
217                          GetCurrentThreadId(), socket_info, dwProcessId));\r
218 \r
219         /* First, flush all the receive buffers. There should be no send/rdma buffers left. */\r
220         CL_ASSERT( socket_info->socket_state == IBSP_CONNECTED );\r
221         IBSP_CHANGE_SOCKET_STATE( socket_info, IBSP_DUPLICATING_OLD );\r
222 \r
223         /* We changed the state - remove from connection map. */\r
224         ibsp_conn_remove( socket_info );\r
225 \r
226         /* We might have a left-over from the previous duplicate. */\r
227         if( socket_info->duplicate.mmap_handle != NULL )\r
228         {\r
229                 CloseHandle( socket_info->duplicate.mmap_handle );\r
230                 socket_info->duplicate.mmap_handle = NULL;\r
231         }\r
232 \r
233         /* \r
234          * Share this socket_info so the next-controlling process can get the necessary\r
235          * information to create the duplicated socket. Ensure a unique identifier. \r
236          */\r
237         for( i = 0; i < 50; i++ )\r
238         {\r
239                 char fname[100];\r
240                 GUID guid;\r
241 \r
242                 /* Create a GUID and collapse it into 32 bits. */\r
243                 UuidCreate( &guid );\r
244                 identifier = guid.Data1 ^ (guid.Data2 << 16 | guid.Data3) ^\r
245                         (guid.Data4[7] << 24 | guid.Data4[6] << 16 | guid.Data4[5] << 8 | guid.\r
246                          Data4[4]) ^ (guid.Data4[3] << 24 | guid.Data4[2] << 16 | guid.\r
247                                                   Data4[1] << 8 | guid.Data4[0]);\r
248 \r
249                 create_name( fname, dwProcessId, identifier );\r
250 \r
251                 socket_info->duplicate.mmap_handle = CreateFileMapping( INVALID_HANDLE_VALUE,   // use paging file\r
252                                                                                                                            NULL,        // default security attributes\r
253                                                                                                                            PAGE_READWRITE,      // read access\r
254                                                                                                                            0,   // size: high 32-bits\r
255                                                                                                                            sizeof(struct ibsp_duplicate_info),  // size: low 32-bits\r
256                                                                                                                            fname );     // name of map object\r
257 \r
258                 if( socket_info->duplicate.mmap_handle == NULL )\r
259                 {\r
260                         IBSP_ERROR_EXIT(\r
261                                 ("CreateFileMapping failed with %d\n", GetLastError()) );\r
262                         return WSAENETDOWN;\r
263                 }\r
264 \r
265                 /* Make sure this identifier is unique for that process.  */\r
266                 if( GetLastError() == ERROR_ALREADY_EXISTS )\r
267                 {\r
268                         CloseHandle( socket_info->duplicate.mmap_handle );\r
269                         socket_info->duplicate.mmap_handle = NULL;\r
270 \r
271                         /* Try again. */\r
272                         continue;\r
273                 }\r
274 \r
275                 /* Get a pointer to the file-mapped shared memory. */\r
276                 dup_info = MapViewOfFile( socket_info->duplicate.mmap_handle,   // object to map view of\r
277                                                                  FILE_MAP_WRITE,        // read/write access\r
278                                                                  0,     // high offset:  map from\r
279                                                                  0,     // low offset:   beginning\r
280                                                                  0 );   // default: map entire file\r
281                 if( dup_info == NULL )\r
282                 {\r
283                         IBSP_ERROR_EXIT(\r
284                                 ("MapViewOfFile failed with %d\n", GetLastError()) );\r
285                         CloseHandle( socket_info->duplicate.mmap_handle );\r
286                         socket_info->duplicate.mmap_handle = NULL;\r
287                         return WSAENETDOWN;\r
288                 }\r
289         }\r
290 \r
291         if( dup_info == NULL )\r
292         {\r
293                 IBSP_ERROR_EXIT( ("failed to get a unique identifier\n") );\r
294                 CloseHandle( socket_info->duplicate.mmap_handle );\r
295                 socket_info->duplicate.mmap_handle = NULL;\r
296                 return WSAENETDOWN;\r
297         }\r
298 \r
299         socket_info->duplicate.identifier = identifier;\r
300 \r
301         memset( &reason, 0, sizeof(reason) );\r
302         reason.type = DISC_DUPLICATING;\r
303         reason.duplicating.identifier = identifier;\r
304         reason.duplicating.dwProcessId = dwProcessId;\r
305 \r
306         cl_spinlock_release( &socket_info->mutex );\r
307         ib_disconnect( socket_info, &reason );\r
308         cl_spinlock_acquire( &socket_info->mutex );\r
309 \r
310         wait_cq_drain( socket_info );\r
311 \r
312         ib_destroy_socket( socket_info );\r
313 \r
314         /* Put enough info in dup_info so that the remote socket can recreate the connection. */\r
315         dup_info->port_guid = socket_info->port->guid;\r
316         dup_info->socket_options = socket_info->socket_options;\r
317         dup_info->peer_addr = socket_info->peer_addr;\r
318         dup_info->local_addr = socket_info->local_addr;\r
319         dup_info->dwProcessId = dwProcessId;\r
320 \r
321         /* And that's it */\r
322         IBSP_EXIT( IBSP_DBG_CONN );\r
323         return 0;\r
324 }\r