5779cd325db99577e43952df656021a605bd3fbc
[dpdk.git] / lib / librte_eal / windows / eal_hugepages.c
1 #include <rte_errno.h>
2 #include <rte_log.h>
3 #include <rte_memory.h>
4 #include <rte_memzone.h>
5 #include <rte_os.h>
6
7 #include "eal_private.h"
8 #include "eal_filesystem.h"
9 #include "eal_hugepages.h"
10 #include "eal_internal_cfg.h"
11 #include "eal_windows.h"
12
13 static int
14 hugepage_claim_privilege(void)
15 {
16         static const wchar_t privilege[] = L"SeLockMemoryPrivilege";
17
18         HANDLE token;
19         LUID luid;
20         TOKEN_PRIVILEGES tp;
21         int ret = -1;
22
23         if (!OpenProcessToken(GetCurrentProcess(),
24                         TOKEN_ADJUST_PRIVILEGES, &token)) {
25                 RTE_LOG_WIN32_ERR("OpenProcessToken()");
26                 return -1;
27         }
28
29         if (!LookupPrivilegeValueW(NULL, privilege, &luid)) {
30                 RTE_LOG_WIN32_ERR("LookupPrivilegeValue(\"%S\")", privilege);
31                 goto exit;
32         }
33
34         tp.PrivilegeCount = 1;
35         tp.Privileges[0].Luid = luid;
36         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
37
38         if (!AdjustTokenPrivileges(
39                         token, FALSE, &tp, sizeof(tp), NULL, NULL)) {
40                 RTE_LOG_WIN32_ERR("AdjustTokenPrivileges()");
41                 goto exit;
42         }
43
44         ret = 0;
45
46 exit:
47         CloseHandle(token);
48
49         return ret;
50 }
51
52 static int
53 hugepage_info_init(void)
54 {
55         struct hugepage_info *hpi;
56         unsigned int socket_id;
57         int ret = 0;
58         struct internal_config *internal_conf =
59                 eal_get_internal_configuration();
60
61         /* Only one hugepage size available on Windows. */
62         internal_conf->num_hugepage_sizes = 1;
63         hpi = &internal_conf->hugepage_info[0];
64
65         hpi->hugepage_sz = GetLargePageMinimum();
66         if (hpi->hugepage_sz == 0)
67                 return -ENOTSUP;
68
69         /* Assume all memory on each NUMA node available for hugepages,
70          * because Windows neither advertises additional limits,
71          * nor provides an API to query them.
72          */
73         for (socket_id = 0; socket_id < rte_socket_count(); socket_id++) {
74                 ULONGLONG bytes;
75                 unsigned int numa_node;
76
77                 numa_node = eal_socket_numa_node(socket_id);
78                 if (!GetNumaAvailableMemoryNodeEx(numa_node, &bytes)) {
79                         RTE_LOG_WIN32_ERR("GetNumaAvailableMemoryNodeEx(%u)",
80                                 numa_node);
81                         continue;
82                 }
83
84                 hpi->num_pages[socket_id] = bytes / hpi->hugepage_sz;
85                 RTE_LOG(DEBUG, EAL,
86                         "Found %u hugepages of %zu bytes on socket %u\n",
87                         hpi->num_pages[socket_id], hpi->hugepage_sz, socket_id);
88         }
89
90         /* No hugepage filesystem on Windows. */
91         hpi->lock_descriptor = -1;
92         memset(hpi->hugedir, 0, sizeof(hpi->hugedir));
93
94         return ret;
95 }
96
97 int
98 eal_hugepage_info_init(void)
99 {
100         if (hugepage_claim_privilege() < 0) {
101                 RTE_LOG(ERR, EAL, "Cannot claim hugepage privilege\n");
102                 return -1;
103         }
104
105         if (hugepage_info_init() < 0) {
106                 RTE_LOG(ERR, EAL, "Cannot get hugepage information\n");
107                 return -1;
108         }
109
110         return 0;
111 }