d8b37c8d46b0bee592db798d69009730d4439f4d
[mirror/winof/.git] / tools / ndinstall / user / installsp.c
1 /*\r
2  * Copyright (c) 2005 SilverStorm Technologies.  All rights reserved.\r
3  * Portions Copyright (c) 2008 Microsoft Corporation.  All rights reserved.\r
4  * Copyright (c) 2009 Intel Corp, Inc.  All rights reserved.\r
5  *\r
6  * This software is available to you under the OpenIB.org BSD license\r
7  * below:\r
8  *\r
9  *     Redistribution and use in source and binary forms, with or\r
10  *     without modification, are permitted provided that the following\r
11  *     conditions are met:\r
12  *\r
13  *      - Redistributions of source code must retain the above\r
14  *        copyright notice, this list of conditions and the following\r
15  *        disclaimer.\r
16  *\r
17  *      - Redistributions in binary form must reproduce the above\r
18  *        copyright notice, this list of conditions and the following\r
19  *        disclaimer in the documentation and/or other materials\r
20  *        provided with the distribution.\r
21  *\r
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
26  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
27  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
28  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
29  * SOFTWARE.\r
30  *\r
31  * $Id$\r
32  */\r
33 \r
34 /*\r
35  *      Module Name: installsp.c\r
36  *      Description: This module installs/removes the NetworkDirect provider for infiniband.\r
37  *      execute:\r
38  *      To install the service provider\r
39  *              installsp -i\r
40  *      To remove the service provider\r
41  *              installsp -r\r
42  */\r
43 \r
44 #include <winsock2.h>\r
45 #include <ws2tcpip.h>\r
46 #include <ws2spi.h>\r
47 #include <stdio.h>\r
48 #include <string.h>\r
49 #include "..\..\..\..\etc\user\getopt.c"\r
50 \r
51 #ifndef PFL_NETWORKDIRECT_PROVIDER\r
52 #define PFL_NETWORKDIRECT_PROVIDER          0x00000010\r
53 #endif\r
54 \r
55 /* Initialize the LSP's provider path for Infiband Service Provider dll */\r
56 #define IBAL_INDEX                      0\r
57 #define IBAL_DEBUG_INDEX        1\r
58 #define WV_INDEX                        2\r
59 #define WV_DEBUG_INDEX          3\r
60 #define MAX_INDEX                       4\r
61 \r
62 int index;\r
63 \r
64 static const char * const provider_name[MAX_INDEX] = {\r
65         "IBAL", "IBAL (debug)", "WinVerbs", "WinVerbs (debug)"\r
66 };\r
67 \r
68 static const WCHAR * const provider_path[MAX_INDEX] = {\r
69         L"%SYSTEMROOT%\\system32\\ibndprov.dll",\r
70         L"%SYSTEMROOT%\\system32\\ibndprov.dll",\r
71         L"%SYSTEMROOT%\\system32\\wvndprov.dll",\r
72         L"%SYSTEMROOT%\\system32\\wvndprovd.dll"\r
73 };\r
74 \r
75 static const WCHAR * const provider_desc[MAX_INDEX] = {\r
76         L"OpenFabrics Network Direct Provider",\r
77         L"OpenFabrics Network Direct Provider (Debug)",\r
78         L"OpenFabrics Winverbs Network Direct Provider",\r
79         L"OpenFabrics Winverbs Network Direct Provider (Debug)"\r
80 };\r
81 \r
82 static GUID provider_guid[MAX_INDEX] = {\r
83         // {52CDAA00-29D0-46be-8FC6-E51D7075C338}\r
84         { 0x52CDAA00, 0x29D0, 0x46be, { 0x8f, 0xc6, 0xe5, 0x1d, 0x70, 0x75, 0xc3, 0x38 } },\r
85         // {52CDAA00-29D0-46be-8FC6-E51D7075C338}\r
86         { 0x52CDAA00, 0x29D0, 0x46be, { 0x8f, 0xc6, 0xe5, 0x1d, 0x70, 0x75, 0xc3, 0x38 } },\r
87         // {854DCE83-C872-4462-A3EB-C961C40E59D0}\r
88         { 0x854dce83, 0xc872, 0x4462, { 0xa3, 0xeb, 0xc9, 0x61, 0xc4, 0x0e, 0x59, 0xd0 } },\r
89         // {1B8F1692-EDD9-4153-A159-605A73BCFFCF}\r
90         { 0x1b8f1692, 0xedd9, 0x4153, { 0xa1, 0x59, 0x60, 0x5a, 0x73, 0xbc, 0xff, 0xcf } }\r
91 };\r
92 \r
93 #ifdef _WIN64\r
94 #define WSCInstallProvider      WSCInstallProvider64_32\r
95 #endif  /* _WIN64 */\r
96 \r
97 static void\r
98 show_usage (char *progname)\r
99 {\r
100         printf("usage: %s\n", progname);\r
101         printf("\tRemove, install, and list OFA Network Direct providers\n\n");\r
102         printf("\t[-[i|r] provider_name]  Install/remove the specified provider\n");\r
103         printf("\t[-d]                    Install/remove debug version of provider\n");\r
104         printf("\tprovider_name must be one of the following:\n");\r
105         printf("\t\tibal\n");\r
106         printf("\t\twinverbs\n");\r
107 }\r
108 \r
109 \r
110 static void print_providers(void)\r
111 {\r
112         WSAPROTOCOL_INFOW *protocol_info;\r
113         unsigned int protocol_count;\r
114         unsigned int i;\r
115         DWORD protocol_size;\r
116         INT err_no;\r
117         int rc;\r
118 \r
119         /* Find the size of the buffer */\r
120         protocol_size = 0;\r
121         rc = WSCEnumProtocols (NULL, NULL, &protocol_size, &err_no);\r
122         if (rc == SOCKET_ERROR && err_no != WSAENOBUFS) {\r
123                 printf("WSCEnumProtocols() returned error (%d)\n", err_no);\r
124                 return;\r
125         }\r
126 \r
127         protocol_info = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, protocol_size);\r
128         if (protocol_info == NULL) {\r
129                 printf("HeapAlloc() failed\n");\r
130                 return;\r
131         }\r
132 \r
133         /* Enumerate the catalog for real */\r
134         rc = WSCEnumProtocols (NULL, protocol_info, &protocol_size, &err_no);\r
135         if (rc == SOCKET_ERROR) {\r
136                 printf("WSCEnumProtocols returned error for real enumeration (%d)\n",\r
137                          err_no);\r
138                 HeapFree (GetProcessHeap (), 0, protocol_info);\r
139                 return;\r
140         }\r
141 \r
142         protocol_count = rc;\r
143 \r
144         printf("\nCurrent providers:\n");\r
145         for (i = 0; i < protocol_count; i++) {\r
146                 printf ("\t%010d - %S\n", protocol_info[i].dwCatalogEntryId,\r
147                                 protocol_info[i].szProtocol);\r
148         }\r
149 \r
150         HeapFree (GetProcessHeap (), 0, protocol_info);\r
151 \r
152         return;\r
153 }\r
154 \r
155 /*\r
156  * Function: install_provider\r
157  *   Description: installs the service provider\r
158  *\r
159  * Note: most of the information setup here comes from "MSDN Home >\r
160  * MSDN Library > Windows Development > Network Devices and\r
161  * Protocols > Design Guide > System Area Networks > Windows Sockets\r
162  * Direct > Windows Sockets Direct Component Operation > Installing\r
163  * Windows Sockets Direct Components".\r
164  * The direct link is http://msdn.microsoft.com/library/default.asp?url=/library/en-us/network/hh/network/wsdp_2xrb.asp\r
165  */\r
166 static void install_provider(void)\r
167 {\r
168         int rc;\r
169         INT err_no;\r
170         LONG reg_error;\r
171         WSAPROTOCOL_INFOW provider;\r
172         HKEY hkey;\r
173 \r
174         /* Setup the values in PROTOCOL_INFO */\r
175         provider.dwServiceFlags1 = \r
176                 XP1_GUARANTEED_DELIVERY | \r
177                 XP1_GUARANTEED_ORDER | \r
178                 XP1_MESSAGE_ORIENTED |\r
179                 XP1_CONNECT_DATA;  /*XP1_GRACEFUL_CLOSE;*/\r
180         provider.dwServiceFlags2 = 0;   /* Reserved */\r
181         provider.dwServiceFlags3 = 0;   /* Reserved */\r
182         provider.dwServiceFlags4 = 0;   /* Reserved */\r
183         provider.dwProviderFlags = PFL_HIDDEN | PFL_NETWORKDIRECT_PROVIDER;\r
184         provider.ProviderId = provider_guid[index];\r
185         provider.dwCatalogEntryId = 0;\r
186         provider.ProtocolChain.ChainLen = 1;    /* Base Protocol Service Provider */\r
187         provider.iVersion = 1;\r
188         if (index == IBAL_INDEX || index == IBAL_DEBUG_INDEX) {\r
189                 provider.iAddressFamily = AF_INET;\r
190                 provider.iMaxSockAddr = sizeof(SOCKADDR_IN);\r
191         } else {\r
192                 provider.iAddressFamily = AF_INET6;\r
193                 provider.iMaxSockAddr = sizeof(SOCKADDR_IN6);\r
194         }\r
195         provider.iMinSockAddr = 16;\r
196         provider.iSocketType = -1;\r
197         provider.iProtocol = 0;\r
198         provider.iProtocolMaxOffset = 0;\r
199         provider.iNetworkByteOrder = BIGENDIAN;\r
200         provider.iSecurityScheme = SECURITY_PROTOCOL_NONE;\r
201         provider.dwMessageSize = 0xFFFFFFFF; /* IB supports 32-bit lengths for data transfers on RC */\r
202         provider.dwProviderReserved = 0;\r
203         wcscpy( provider.szProtocol, provider_desc[index] );\r
204 \r
205         printf("\nInstalling %s provider: ", provider_name[index]);\r
206         rc = WSCInstallProvider(\r
207                 &provider_guid[index], provider_path[index], &provider, 1, &err_no );\r
208         if( rc == SOCKET_ERROR )\r
209         {\r
210                 if( err_no == WSANO_RECOVERY )\r
211                         printf("already installed\n");\r
212                 else\r
213                         printf("WSCInstallProvider failed: %d\n", err_no);\r
214         } else {\r
215                 printf("successful\n");\r
216         }\r
217 }\r
218 \r
219 /*\r
220  * Function: remove_provider\r
221  *   Description: removes our provider.\r
222  */\r
223 static void remove_provider(void)\r
224 {\r
225         int rc;\r
226         int err_no;\r
227         LONG reg_error;\r
228         HKEY hkey;\r
229 \r
230         /* Remove from the catalog */\r
231         printf("\nRemoving %s provider: ", provider_name[index]);\r
232         rc = WSCDeinstallProvider(&provider_guid[index], &err_no);\r
233         if (rc == SOCKET_ERROR) {\r
234                 printf ("WSCDeinstallProvider failed: %d\n", err_no);\r
235         } else {\r
236                 printf ("successful\n");\r
237         }\r
238 \r
239 #ifdef _WIN64\r
240         /* Remove from the 32-bit catalog too! */\r
241         printf("Removing 32-bit %s provider: ", provider_name[index]);\r
242         rc = WSCDeinstallProvider32(&provider_guid[index], &err_no);\r
243         if (rc == SOCKET_ERROR) {\r
244                 printf ("WSCDeinstallProvider32 failed: %d\n", err_no);\r
245         } else {\r
246                 printf ("successful\n");\r
247         }\r
248 #endif  /* _WIN64 */\r
249 }\r
250 \r
251 static int get_prov_index(char *name)\r
252 {\r
253         int i;\r
254 \r
255         for (i = 0; i < MAX_INDEX; i++) {\r
256                 if (!_stricmp(provider_name[i], name)) {\r
257                         index = i;\r
258                         return 0;\r
259                 }\r
260         }\r
261         return -1;\r
262 }\r
263 \r
264 int __cdecl main (int argc, char *argv[])\r
265 {\r
266         int ret, op;\r
267         int install = 0, remove = 0, debug = 0;\r
268         WSADATA wsd;\r
269         char *prov;\r
270 \r
271         while ((op = getopt(argc, argv, "i:r:d")) != -1) {\r
272                 switch (op) {\r
273                 case 'i':\r
274                         if (install || remove || get_prov_index(optarg)) {\r
275                                 goto usage;\r
276                         }\r
277                         install = 1;\r
278                         break;\r
279                 case 'r':\r
280                         if (install || remove || get_prov_index(optarg)) {\r
281                                 goto usage;\r
282                         }\r
283                         remove = 1;\r
284                         break;\r
285                 case 'd':\r
286                         debug = 1;\r
287                         break;\r
288                 default:\r
289                         goto usage;\r
290                 }\r
291         }\r
292 \r
293         index += debug;\r
294 \r
295         if (WSAStartup (MAKEWORD (2, 2), &wsd) != 0) {\r
296                 printf ("InstallSP: Unable to load Winsock: %d\n", GetLastError ());\r
297                 return -1;\r
298         }\r
299 \r
300         if (LOBYTE (wsd.wVersion) != 2 || HIBYTE (wsd.wVersion) != 2) {\r
301                 WSACleanup ();\r
302                 printf("InstallSP: Unable to find a usable version of Winsock DLL\n");\r
303                 ret = -1;\r
304                 goto exit;\r
305         }\r
306 \r
307         if (install) {\r
308                 install_provider();\r
309 #ifdef PERFMON_ENABLED\r
310                 _IBSPPerfmonIniFilesGenerate();\r
311                 if ( _IBSPPerfmonRegisterKeys() == ERROR_SUCCESS )\r
312                                 _IBSPPerfmonRegisterCounters();\r
313 #endif\r
314         } else if (remove) {\r
315                 remove_provider();\r
316 #ifdef PERFMON_ENABLED\r
317                 _IBSPPerfmonIniFilesRemove();\r
318                 if ( _IBSPPerfmonDeregisterCounters() == ERROR_SUCCESS )\r
319                         _IBSPPerfmonDeregisterKeys();\r
320 #endif\r
321         }\r
322 \r
323         print_providers();\r
324 \r
325 exit:\r
326         WSACleanup();\r
327         return ret;\r
328 \r
329 usage:\r
330         show_usage(argv[0]);\r
331         return -1;\r
332 }\r