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