Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

767 lines
20 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1999
  6. //
  7. // File: perfmon.c
  8. //
  9. // Contents: Schannel performance counter functions.
  10. //
  11. // Functions:
  12. //
  13. // History: 04-11-2000 jbanes Created
  14. //
  15. //------------------------------------------------------------------------
  16. #include "sslp.h"
  17. #include "perfmon.h"
  18. DWORD dwOpenCount = 0; // count of "Open" threads
  19. BOOL bInitOK = FALSE; // true = DLL initialized OK
  20. HANDLE LsaHandle;
  21. DWORD PackageNumber;
  22. PM_OPEN_PROC OpenSslPerformanceData;
  23. PM_COLLECT_PROC CollectSslPerformanceData;
  24. PM_CLOSE_PROC CloseSslPerformanceData;
  25. #define DWORD_MULTIPLE(x) (((x+sizeof(DWORD)-1)/sizeof(DWORD))*sizeof(DWORD))
  26. SSLPERF_DATA_DEFINITION SslPerfDataDefinition =
  27. {
  28. // PERF_OBJECT_TYPE
  29. {
  30. sizeof(SSLPERF_DATA_DEFINITION) + sizeof(SSLPERF_COUNTER),
  31. sizeof(SSLPERF_DATA_DEFINITION),
  32. sizeof(PERF_OBJECT_TYPE),
  33. SSLPERF_OBJ,
  34. 0,
  35. SSLPERF_OBJ,
  36. 0,
  37. PERF_DETAIL_NOVICE,
  38. (sizeof(SSLPERF_DATA_DEFINITION) - sizeof(PERF_OBJECT_TYPE)) /
  39. sizeof(PERF_COUNTER_DEFINITION),
  40. 0
  41. PERF_NO_INSTANCES,
  42. 0
  43. },
  44. // PERF_COUNTER_DEFINITION
  45. {
  46. sizeof(PERF_COUNTER_DEFINITION),
  47. SSL_CACHE_ENTRIES,
  48. 0,
  49. SSL_CACHE_ENTRIES,
  50. 0,
  51. 0,
  52. PERF_DETAIL_NOVICE,
  53. PERF_COUNTER_RAWCOUNT,
  54. sizeof(DWORD),
  55. FIELD_OFFSET(SSLPERF_COUNTER, dwCacheEntries)
  56. },
  57. // PERF_COUNTER_DEFINITION
  58. {
  59. sizeof(PERF_COUNTER_DEFINITION),
  60. SSL_ACTIVE_ENTRIES,
  61. 0,
  62. SSL_ACTIVE_ENTRIES,
  63. 0,
  64. 0,
  65. PERF_DETAIL_NOVICE,
  66. PERF_COUNTER_RAWCOUNT,
  67. sizeof(DWORD),
  68. FIELD_OFFSET(SSLPERF_COUNTER, dwActiveEntries)
  69. },
  70. // PERF_COUNTER_DEFINITION
  71. {
  72. sizeof(PERF_COUNTER_DEFINITION),
  73. SSL_HANDSHAKE_COUNT,
  74. 0,
  75. SSL_HANDSHAKE_COUNT,
  76. 0,
  77. 0,
  78. PERF_DETAIL_NOVICE,
  79. PERF_COUNTER_COUNTER,
  80. sizeof(DWORD),
  81. FIELD_OFFSET(SSLPERF_COUNTER, dwHandshakeCount)
  82. },
  83. // PERF_COUNTER_DEFINITION
  84. {
  85. sizeof(PERF_COUNTER_DEFINITION),
  86. SSL_RECONNECT_COUNT,
  87. 0,
  88. SSL_RECONNECT_COUNT,
  89. 0,
  90. 0,
  91. PERF_DETAIL_NOVICE,
  92. PERF_COUNTER_COUNTER,
  93. sizeof(DWORD),
  94. FIELD_OFFSET(SSLPERF_COUNTER, dwReconnectCount)
  95. }
  96. };
  97. typedef struct _INSTANCE_DATA
  98. {
  99. DWORD dwProtocol;
  100. LPWSTR szInstanceName;
  101. } INSTANCE_DATA, *PINSTANCE_DATA;
  102. static INSTANCE_DATA wdInstance[] =
  103. {
  104. {SP_PROT_CLIENTS, L"Client"},
  105. {SP_PROT_SERVERS, L"Server"},
  106. {0, L"_Total"}
  107. };
  108. static const DWORD NUM_INSTANCES =
  109. (sizeof(wdInstance)/sizeof(wdInstance[0]));
  110. /*++
  111. Routine Description:
  112. This routine will initialize the data structures used to pass
  113. data back to the registry
  114. Arguments:
  115. Pointer to object ID of each device to be opened (PerfGen)
  116. Return Value:
  117. None.
  118. --*/
  119. DWORD APIENTRY
  120. OpenSslPerformanceData(
  121. LPWSTR lpDeviceNames)
  122. {
  123. LONG Status;
  124. HKEY hKey = 0;
  125. DWORD size;
  126. DWORD type;
  127. DWORD dwFirstCounter;
  128. DWORD dwFirstHelp;
  129. LSA_STRING PackageName;
  130. //
  131. // Since WINLOGON is multi-threaded and will call this routine in
  132. // order to service remote performance queries, this library
  133. // must keep track of how many times it has been opened (i.e.
  134. // how many threads have accessed it). the registry routines will
  135. // limit access to the initialization routine to only one thread
  136. // at a time so synchronization (i.e. reentrancy) should not be
  137. // a problem
  138. //
  139. if (!dwOpenCount)
  140. {
  141. // get counter and help index base values from registry
  142. // Open key to registry entry
  143. // read First Counter and First Help values
  144. // update static data strucutures by adding base to
  145. // offset value in structure.
  146. Status = RegOpenKeyExA(
  147. HKEY_LOCAL_MACHINE,
  148. "SYSTEM\\CurrentControlSet\\Services\\Schannel\\Performance",
  149. 0L,
  150. KEY_READ,
  151. &hKey);
  152. if(Status != ERROR_SUCCESS)
  153. {
  154. // this is fatal, if we can't get the base values of the
  155. // counter or help names, then the names won't be available
  156. // to the requesting application so there's not much
  157. // point in continuing.
  158. goto cleanup;
  159. }
  160. size = sizeof (DWORD);
  161. Status = RegQueryValueExA(
  162. hKey,
  163. "First Counter",
  164. 0L,
  165. &type,
  166. (LPBYTE)&dwFirstCounter,
  167. &size);
  168. if(Status != ERROR_SUCCESS)
  169. {
  170. // this is fatal, if we can't get the base values of the
  171. // counter or help names, then the names won't be available
  172. // to the requesting application so there's not much
  173. // point in continuing.
  174. goto cleanup;
  175. }
  176. size = sizeof (DWORD);
  177. Status = RegQueryValueExA(
  178. hKey,
  179. "First Help",
  180. 0L,
  181. &type,
  182. (LPBYTE)&dwFirstHelp,
  183. &size);
  184. if(Status != ERROR_SUCCESS)
  185. {
  186. // this is fatal, if we can't get the base values of the
  187. // counter or help names, then the names won't be available
  188. // to the requesting application so there's not much
  189. // point in continuing.
  190. goto cleanup;
  191. }
  192. //
  193. // NOTE: the initialization program could also retrieve
  194. // LastCounter and LastHelp if they wanted to do
  195. // bounds checking on the new number. e.g.
  196. //
  197. // counter->CounterNameTitleIndex += dwFirstCounter;
  198. // if (counter->CounterNameTitleIndex > dwLastCounter) {
  199. // LogErrorToEventLog (INDEX_OUT_OF_BOUNDS);
  200. // }
  201. //
  202. // Establish connection to schannel.
  203. //
  204. Status = LsaConnectUntrusted(&LsaHandle);
  205. if(!NT_SUCCESS(Status))
  206. {
  207. goto cleanup;
  208. }
  209. PackageName.Buffer = UNISP_NAME_A;
  210. PackageName.Length = (USHORT)strlen(PackageName.Buffer);
  211. PackageName.MaximumLength = PackageName.Length + 1;
  212. Status = LsaLookupAuthenticationPackage(
  213. LsaHandle,
  214. &PackageName,
  215. &PackageNumber);
  216. if(FAILED(Status))
  217. {
  218. CloseHandle(LsaHandle);
  219. goto cleanup;
  220. }
  221. //
  222. // Initialize the performance counters.
  223. //
  224. SslPerfDataDefinition.SslPerfObjectType.ObjectNameTitleIndex += dwFirstCounter;
  225. SslPerfDataDefinition.SslPerfObjectType.ObjectHelpTitleIndex += dwFirstHelp;
  226. // assign index of default counter (Sine Wave)
  227. SslPerfDataDefinition.SslPerfObjectType.DefaultCounter = 0;
  228. SslPerfDataDefinition.CacheEntriesDef.CounterNameTitleIndex += dwFirstCounter;
  229. SslPerfDataDefinition.CacheEntriesDef.CounterHelpTitleIndex += dwFirstHelp;
  230. SslPerfDataDefinition.ActiveEntriesDef.CounterNameTitleIndex += dwFirstCounter;
  231. SslPerfDataDefinition.ActiveEntriesDef.CounterHelpTitleIndex += dwFirstHelp;
  232. SslPerfDataDefinition.HandshakeCountDef.CounterNameTitleIndex += dwFirstCounter;
  233. SslPerfDataDefinition.HandshakeCountDef.CounterHelpTitleIndex += dwFirstHelp;
  234. SslPerfDataDefinition.ReconnectCountDef.CounterNameTitleIndex += dwFirstCounter;
  235. SslPerfDataDefinition.ReconnectCountDef.CounterHelpTitleIndex += dwFirstHelp;
  236. bInitOK = TRUE;
  237. }
  238. dwOpenCount++;
  239. Status = ERROR_SUCCESS;
  240. cleanup:
  241. if(hKey)
  242. {
  243. RegCloseKey(hKey);
  244. }
  245. return Status;
  246. }
  247. DWORD
  248. GetCacheInfo(
  249. PSSL_PERFMON_INFO_RESPONSE pPerfmonInfo)
  250. {
  251. PSSL_PERFMON_INFO_REQUEST pRequest = NULL;
  252. PSSL_PERFMON_INFO_RESPONSE pResponse = NULL;
  253. DWORD cbResponse = 0;
  254. DWORD SubStatus;
  255. DWORD Status;
  256. pRequest = (PSSL_PERFMON_INFO_REQUEST)LocalAlloc(LPTR, sizeof(SSL_PERFMON_INFO_REQUEST));
  257. if(pRequest == NULL)
  258. {
  259. Status = STATUS_INSUFFICIENT_RESOURCES;
  260. goto cleanup;
  261. }
  262. pRequest->MessageType = SSL_PERFMON_INFO_MESSAGE;
  263. Status = LsaCallAuthenticationPackage(
  264. LsaHandle,
  265. PackageNumber,
  266. pRequest,
  267. sizeof(SSL_PERFMON_INFO_REQUEST),
  268. &pResponse,
  269. &cbResponse,
  270. &SubStatus);
  271. if(FAILED(Status))
  272. {
  273. goto cleanup;
  274. }
  275. *pPerfmonInfo = *pResponse;
  276. Status = STATUS_SUCCESS;
  277. cleanup:
  278. if(pRequest)
  279. {
  280. LocalFree(pRequest);
  281. }
  282. if (pResponse != NULL)
  283. {
  284. LsaFreeReturnBuffer(pResponse);
  285. }
  286. return Status;
  287. }
  288. /*++
  289. Routine Description:
  290. This routine will return the data for the ssl performance counters.
  291. Arguments:
  292. IN LPWSTR lpValueName
  293. pointer to a wide character string passed by registry.
  294. IN OUT LPVOID *lppData
  295. IN: pointer to the address of the buffer to receive the completed
  296. PerfDataBlock and subordinate structures. This routine will
  297. append its data to the buffer starting at the point referenced
  298. by *lppData.
  299. OUT: points to the first byte after the data structure added by this
  300. routine. This routine updated the value at lppdata after appending
  301. its data.
  302. IN OUT LPDWORD lpcbTotalBytes
  303. IN: the address of the DWORD that tells the size in bytes of the
  304. buffer referenced by the lppData argument
  305. OUT: the number of bytes added by this routine is writted to the
  306. DWORD pointed to by this argument
  307. IN OUT LPDWORD NumObjectTypes
  308. IN: the address of the DWORD to receive the number of objects added
  309. by this routine
  310. OUT: the number of objects added by this routine is writted to the
  311. DWORD pointed to by this argument
  312. Return Value:
  313. ERROR_MORE_DATA if buffer passed is too small to hold data.
  314. ERROR_SUCCESS if success or any other error.
  315. --*/
  316. DWORD APIENTRY
  317. CollectSslPerformanceData(
  318. IN LPWSTR lpValueName,
  319. IN OUT LPVOID *lppData,
  320. IN OUT LPDWORD lpcbTotalBytes,
  321. IN OUT LPDWORD lpNumObjectTypes)
  322. {
  323. PERF_INSTANCE_DEFINITION *pPerfInstanceDefinition;
  324. SSLPERF_DATA_DEFINITION *pSslPerfDataDefinition;
  325. PSSLPERF_COUNTER pSC;
  326. SSL_PERFMON_INFO_RESPONSE PerfmonInfo;
  327. DWORD dwThisInstance;
  328. ULONG SpaceNeeded;
  329. DWORD dwQueryType;
  330. DWORD Status;
  331. //
  332. // before doing anything else, see if Open went OK
  333. //
  334. if (!bInitOK)
  335. {
  336. // unable to continue because open failed.
  337. *lpcbTotalBytes = (DWORD) 0;
  338. *lpNumObjectTypes = (DWORD) 0;
  339. return ERROR_SUCCESS; // yes, this is a successful exit
  340. }
  341. //
  342. // see if this is a foreign (i.e. non-NT) computer data request
  343. //
  344. dwQueryType = GetQueryType (lpValueName);
  345. if (dwQueryType == QUERY_FOREIGN)
  346. {
  347. // this routine does not service requests for data from
  348. // Non-NT computers
  349. *lpcbTotalBytes = (DWORD) 0;
  350. *lpNumObjectTypes = (DWORD) 0;
  351. return ERROR_SUCCESS;
  352. }
  353. if (dwQueryType == QUERY_ITEMS)
  354. {
  355. if(!(IsNumberInUnicodeList(SslPerfDataDefinition.SslPerfObjectType.ObjectNameTitleIndex, lpValueName)))
  356. {
  357. // request received for data object not provided by this routine
  358. *lpcbTotalBytes = (DWORD) 0;
  359. *lpNumObjectTypes = (DWORD) 0;
  360. return ERROR_SUCCESS;
  361. }
  362. }
  363. pSslPerfDataDefinition = (SSLPERF_DATA_DEFINITION *) *lppData;
  364. SpaceNeeded = sizeof(SSLPERF_DATA_DEFINITION) +
  365. (NUM_INSTANCES * (sizeof(PERF_INSTANCE_DEFINITION) +
  366. (24) + // size of instance names
  367. sizeof (SSLPERF_COUNTER)));
  368. if ( *lpcbTotalBytes < SpaceNeeded )
  369. {
  370. *lpcbTotalBytes = (DWORD) 0;
  371. *lpNumObjectTypes = (DWORD) 0;
  372. return ERROR_MORE_DATA;
  373. }
  374. //
  375. // Copy the (constant, initialized) Object Type and counter definitions
  376. // to the caller's data buffer
  377. //
  378. memmove(pSslPerfDataDefinition,
  379. &SslPerfDataDefinition,
  380. sizeof(SSLPERF_DATA_DEFINITION));
  381. //
  382. // Get info from schannel.
  383. //
  384. Status = GetCacheInfo(&PerfmonInfo);
  385. if(!NT_SUCCESS(Status))
  386. {
  387. *lpcbTotalBytes = (DWORD) 0;
  388. *lpNumObjectTypes = (DWORD) 0;
  389. return ERROR_SUCCESS;
  390. }
  391. //
  392. // Create data for return for each instance
  393. //
  394. pPerfInstanceDefinition = (PERF_INSTANCE_DEFINITION *)
  395. &pSslPerfDataDefinition[1];
  396. for(dwThisInstance = 0; dwThisInstance < NUM_INSTANCES; dwThisInstance++)
  397. {
  398. MonBuildInstanceDefinition(
  399. pPerfInstanceDefinition,
  400. (PVOID *)&pSC,
  401. 0,
  402. 0,
  403. (DWORD)-1, // use name
  404. wdInstance[dwThisInstance].szInstanceName);
  405. pSC->CounterBlock.ByteLength = sizeof (SSLPERF_COUNTER);
  406. if(wdInstance[dwThisInstance].dwProtocol & SP_PROT_CLIENTS)
  407. {
  408. // client
  409. pSC->dwCacheEntries = PerfmonInfo.ClientCacheEntries;
  410. pSC->dwActiveEntries = PerfmonInfo.ClientActiveEntries;
  411. pSC->dwHandshakeCount = PerfmonInfo.ClientHandshakesPerSecond;
  412. pSC->dwReconnectCount = PerfmonInfo.ClientReconnectsPerSecond;
  413. }
  414. else if(wdInstance[dwThisInstance].dwProtocol & SP_PROT_SERVERS)
  415. {
  416. // server
  417. pSC->dwCacheEntries = PerfmonInfo.ServerCacheEntries;
  418. pSC->dwActiveEntries = PerfmonInfo.ServerActiveEntries;
  419. pSC->dwHandshakeCount = PerfmonInfo.ServerHandshakesPerSecond;
  420. pSC->dwReconnectCount = PerfmonInfo.ServerReconnectsPerSecond;
  421. }
  422. else
  423. {
  424. // total
  425. pSC->dwCacheEntries = PerfmonInfo.ClientCacheEntries +
  426. PerfmonInfo.ServerCacheEntries;
  427. pSC->dwActiveEntries = PerfmonInfo.ClientActiveEntries +
  428. PerfmonInfo.ServerActiveEntries;
  429. pSC->dwHandshakeCount = PerfmonInfo.ClientHandshakesPerSecond +
  430. PerfmonInfo.ServerHandshakesPerSecond;
  431. pSC->dwReconnectCount = PerfmonInfo.ClientReconnectsPerSecond +
  432. PerfmonInfo.ServerReconnectsPerSecond;
  433. }
  434. // update instance pointer for next instance
  435. pPerfInstanceDefinition = (PERF_INSTANCE_DEFINITION *)&pSC[1];
  436. }
  437. //
  438. // update arguments for return
  439. //
  440. *lppData = (PVOID)pPerfInstanceDefinition;
  441. *lpNumObjectTypes = 1;
  442. pSslPerfDataDefinition->SslPerfObjectType.TotalByteLength =
  443. *lpcbTotalBytes = (DWORD)((LONG_PTR)pPerfInstanceDefinition -
  444. (LONG_PTR)pSslPerfDataDefinition);
  445. // update instance count
  446. pSslPerfDataDefinition->SslPerfObjectType.NumInstances = NUM_INSTANCES;
  447. return ERROR_SUCCESS;
  448. }
  449. /*++
  450. Routine Description:
  451. This routine closes the open handles to the Signal Gen counters.
  452. Arguments:
  453. None.
  454. Return Value:
  455. ERROR_SUCCESS
  456. --*/
  457. DWORD APIENTRY
  458. CloseSslPerformanceData(void)
  459. {
  460. if(--dwOpenCount == 0)
  461. {
  462. // when this is the last thread...
  463. if(LsaHandle)
  464. {
  465. CloseHandle(LsaHandle);
  466. }
  467. }
  468. return ERROR_SUCCESS;
  469. }
  470. /*++
  471. GetQueryType
  472. returns the type of query described in the lpValue string so that
  473. the appropriate processing method may be used
  474. Arguments
  475. IN lpValue
  476. string passed to PerfRegQuery Value for processing
  477. Return Value
  478. QUERY_GLOBAL
  479. if lpValue == 0 (null pointer)
  480. lpValue == pointer to Null string
  481. lpValue == pointer to "Global" string
  482. QUERY_FOREIGN
  483. if lpValue == pointer to "Foreign" string
  484. QUERY_COSTLY
  485. if lpValue == pointer to "Costly" string
  486. otherwise:
  487. QUERY_ITEMS
  488. --*/
  489. DWORD
  490. GetQueryType (
  491. IN LPWSTR lpValue)
  492. {
  493. if(lpValue == NULL || *lpValue == 0)
  494. {
  495. return QUERY_GLOBAL;
  496. }
  497. if(lstrcmp(lpValue, L"Global") == 0)
  498. {
  499. return QUERY_GLOBAL;
  500. }
  501. if(lstrcmp(lpValue, L"Foreign") == 0)
  502. {
  503. return QUERY_FOREIGN;
  504. }
  505. if(lstrcmp(lpValue, L"Costly") == 0)
  506. {
  507. return QUERY_COSTLY;
  508. }
  509. // if not Global and not Foreign and not Costly,
  510. // then it must be an item list
  511. return QUERY_ITEMS;
  512. }
  513. /*++
  514. MonBuildInstanceDefinition - Build an instance of an object
  515. Inputs:
  516. pBuffer - pointer to buffer where instance is to
  517. be constructed
  518. pBufferNext - pointer to a pointer which will contain
  519. next available location, DWORD aligned
  520. ParentObjectTitleIndex
  521. - Title Index of parent object type; 0 if
  522. no parent object
  523. ParentObjectInstance
  524. - Index into instances of parent object
  525. type, starting at 0, for this instances
  526. parent object instance
  527. UniqueID - a unique identifier which should be used
  528. instead of the Name for identifying
  529. this instance
  530. Name - Name of this instance
  531. --*/
  532. BOOL
  533. MonBuildInstanceDefinition(
  534. PERF_INSTANCE_DEFINITION *pBuffer,
  535. PVOID *pBufferNext,
  536. DWORD ParentObjectTitleIndex,
  537. DWORD ParentObjectInstance,
  538. DWORD UniqueID,
  539. LPWSTR Name)
  540. {
  541. DWORD NameLength;
  542. LPWSTR pName;
  543. // Include trailing null in name size
  544. NameLength = (lstrlenW(Name) + 1) * sizeof(WCHAR);
  545. pBuffer->ByteLength = sizeof(PERF_INSTANCE_DEFINITION) +
  546. DWORD_MULTIPLE(NameLength);
  547. pBuffer->ParentObjectTitleIndex = ParentObjectTitleIndex;
  548. pBuffer->ParentObjectInstance = ParentObjectInstance;
  549. pBuffer->UniqueID = UniqueID;
  550. pBuffer->NameOffset = sizeof(PERF_INSTANCE_DEFINITION);
  551. pBuffer->NameLength = NameLength;
  552. // copy name to name buffer
  553. pName = (LPWSTR)&pBuffer[1];
  554. RtlMoveMemory(pName,Name,NameLength);
  555. // update "next byte" pointer
  556. *pBufferNext = (PVOID) ((PCHAR) pBuffer + pBuffer->ByteLength);
  557. return 0;
  558. }
  559. /*++
  560. IsNumberInUnicodeList
  561. Arguments:
  562. IN dwNumber
  563. DWORD number to find in list
  564. IN lpwszUnicodeList
  565. Null terminated, Space delimited list of decimal numbers
  566. Return Value:
  567. TRUE:
  568. dwNumber was found in the list of unicode number strings
  569. FALSE:
  570. dwNumber was not found in the list.
  571. --*/
  572. BOOL
  573. IsNumberInUnicodeList(
  574. IN DWORD dwNumber,
  575. IN LPWSTR lpwszUnicodeList)
  576. {
  577. DWORD dwThisNumber;
  578. DWORD cDigits;
  579. if(lpwszUnicodeList == 0) return FALSE;
  580. while(TRUE)
  581. {
  582. // Skip over leading whitespace.
  583. while(*lpwszUnicodeList && iswspace(*lpwszUnicodeList))
  584. {
  585. lpwszUnicodeList++;
  586. }
  587. // Get number.
  588. cDigits = 0;
  589. dwThisNumber = 0;
  590. while(iswdigit(*lpwszUnicodeList))
  591. {
  592. dwThisNumber *= 10;
  593. dwThisNumber += (*lpwszUnicodeList - L'0');
  594. cDigits++;
  595. lpwszUnicodeList++;
  596. }
  597. if(cDigits == 0)
  598. {
  599. return FALSE;
  600. }
  601. // Compare number to reference.
  602. if(dwThisNumber == dwNumber)
  603. {
  604. return TRUE;
  605. }
  606. }
  607. }