[WSD] Add IBWSD_POLL environment variable to allow provider to poll for
[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, err;\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                 g_ibsp.up_call_table.lpWPUCloseSocketHandle(\r
180                         socket_info->switch_socket, &err );\r
181                 socket_info->switch_socket = INVALID_SOCKET;\r
182                 STAT_DEC( wpusocket_num );\r
183 \r
184                 ib_destroy_socket( socket_info );\r
185         }\r
186         else\r
187         {\r
188                 cl_spinlock_release( &socket_info->mutex );\r
189         }\r
190 \r
191 err1:\r
192         if( h )\r
193                 CloseHandle( h );\r
194 \r
195         if( socket_info->h_event )\r
196         {\r
197                 CloseHandle( socket_info->h_event );\r
198                 socket_info->h_event = NULL;\r
199         }\r
200 \r
201         IBSP_EXIT( IBSP_DBG_CONN );\r
202         return ret;\r
203 }\r
204 \r
205 \r
206 /*\r
207  * This function is called by the previous controlling process.\r
208  * Called with the socket_info->mutex held.\r
209  */\r
210 int\r
211 prepare_duplicate_socket(\r
212         IN                              struct ibsp_socket_info         *socket_info,\r
213         IN                              DWORD                                           dwProcessId )\r
214 {\r
215         struct ibsp_duplicate_info *dup_info = NULL;\r
216         int i;\r
217         DWORD identifier = 0;\r
218         struct disconnect_reason reason;\r
219 \r
220         fzprint(("%s():%d:0x%x:0x%x: socket=0x%p dwProcessId=0x%x \n", __FUNCTION__,\r
221                          __LINE__, GetCurrentProcessId(),\r
222                          GetCurrentThreadId(), socket_info, dwProcessId));\r
223 \r
224         /* First, flush all the receive buffers. There should be no send/rdma buffers left. */\r
225         CL_ASSERT( socket_info->socket_state == IBSP_CONNECTED );\r
226         IBSP_CHANGE_SOCKET_STATE( socket_info, IBSP_DUPLICATING_OLD );\r
227 \r
228         /* We changed the state - remove from connection map. */\r
229         ibsp_conn_remove( socket_info );\r
230 \r
231         /* We might have a left-over from the previous duplicate. */\r
232         if( socket_info->duplicate.mmap_handle != NULL )\r
233         {\r
234                 CloseHandle( socket_info->duplicate.mmap_handle );\r
235                 socket_info->duplicate.mmap_handle = NULL;\r
236         }\r
237 \r
238         /* \r
239          * Share this socket_info so the next-controlling process can get the necessary\r
240          * information to create the duplicated socket. Ensure a unique identifier. \r
241          */\r
242         for( i = 0; i < 50; i++ )\r
243         {\r
244                 char fname[100];\r
245                 GUID guid;\r
246 \r
247                 /* Create a GUID and collapse it into 32 bits. */\r
248                 UuidCreate( &guid );\r
249                 identifier = guid.Data1 ^ (guid.Data2 << 16 | guid.Data3) ^\r
250                         (guid.Data4[7] << 24 | guid.Data4[6] << 16 | guid.Data4[5] << 8 | guid.\r
251                          Data4[4]) ^ (guid.Data4[3] << 24 | guid.Data4[2] << 16 | guid.\r
252                                                   Data4[1] << 8 | guid.Data4[0]);\r
253 \r
254                 create_name( fname, dwProcessId, identifier );\r
255 \r
256                 socket_info->duplicate.mmap_handle = CreateFileMapping( INVALID_HANDLE_VALUE,   // use paging file\r
257                                                                                                                            NULL,        // default security attributes\r
258                                                                                                                            PAGE_READWRITE,      // read access\r
259                                                                                                                            0,   // size: high 32-bits\r
260                                                                                                                            sizeof(struct ibsp_duplicate_info),  // size: low 32-bits\r
261                                                                                                                            fname );     // name of map object\r
262 \r
263                 if( socket_info->duplicate.mmap_handle == NULL )\r
264                 {\r
265                         IBSP_ERROR_EXIT(\r
266                                 ("CreateFileMapping failed with %d\n", GetLastError()) );\r
267                         return WSAENETDOWN;\r
268                 }\r
269 \r
270                 /* Make sure this identifier is unique for that process.  */\r
271                 if( GetLastError() == ERROR_ALREADY_EXISTS )\r
272                 {\r
273                         CloseHandle( socket_info->duplicate.mmap_handle );\r
274                         socket_info->duplicate.mmap_handle = NULL;\r
275 \r
276                         /* Try again. */\r
277                         continue;\r
278                 }\r
279 \r
280                 /* Get a pointer to the file-mapped shared memory. */\r
281                 dup_info = MapViewOfFile( socket_info->duplicate.mmap_handle,   // object to map view of\r
282                                                                  FILE_MAP_WRITE,        // read/write access\r
283                                                                  0,     // high offset:  map from\r
284                                                                  0,     // low offset:   beginning\r
285                                                                  0 );   // default: map entire file\r
286                 if( dup_info == NULL )\r
287                 {\r
288                         IBSP_ERROR_EXIT(\r
289                                 ("MapViewOfFile failed with %d\n", GetLastError()) );\r
290                         CloseHandle( socket_info->duplicate.mmap_handle );\r
291                         socket_info->duplicate.mmap_handle = NULL;\r
292                         return WSAENETDOWN;\r
293                 }\r
294         }\r
295 \r
296         if( dup_info == NULL )\r
297         {\r
298                 IBSP_ERROR_EXIT( ("failed to get a unique identifier\n") );\r
299                 CloseHandle( socket_info->duplicate.mmap_handle );\r
300                 socket_info->duplicate.mmap_handle = NULL;\r
301                 return WSAENETDOWN;\r
302         }\r
303 \r
304         socket_info->duplicate.identifier = identifier;\r
305 \r
306         memset( &reason, 0, sizeof(reason) );\r
307         reason.type = DISC_DUPLICATING;\r
308         reason.duplicating.identifier = identifier;\r
309         reason.duplicating.dwProcessId = dwProcessId;\r
310 \r
311         cl_spinlock_release( &socket_info->mutex );\r
312         ib_disconnect( socket_info, &reason );\r
313         cl_spinlock_acquire( &socket_info->mutex );\r
314 \r
315         wait_cq_drain( socket_info );\r
316 \r
317         ib_destroy_socket( socket_info );\r
318 \r
319         /* Put enough info in dup_info so that the remote socket can recreate the connection. */\r
320         dup_info->port_guid = socket_info->port->guid;\r
321         dup_info->socket_options = socket_info->socket_options;\r
322         dup_info->peer_addr = socket_info->peer_addr;\r
323         dup_info->local_addr = socket_info->local_addr;\r
324         dup_info->dwProcessId = dwProcessId;\r
325 \r
326         /* And that's it */\r
327         IBSP_EXIT( IBSP_DBG_CONN );\r
328         return 0;\r
329 }\r