eBPF for Windows
ebpf_utilities.h
Go to the documentation of this file.
1 // Copyright (c) eBPF for Windows contributors
2 // SPDX-License-Identifier: MIT
3 #pragma once
4 
5 #include "ebpf_result.h"
6 
7 #include <stdint.h>
8 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
9 // Windows Header Files
10 #include <windows.h>
11 
12 // This should be consistent with _ebpf_result_mapping[]
13 // in ebpf_error.c
14 _When_(error != ERROR_SUCCESS, _Ret_range_(1, 65535)) __forceinline ebpf_result_t
15  win32_error_code_to_ebpf_result(uint32_t error)
16 {
17  ebpf_result_t result;
18 
19  switch (error) {
20  case ERROR_SUCCESS:
21  result = EBPF_SUCCESS;
22  break;
23 
24  case ERROR_OUTOFMEMORY:
25  case ERROR_NOT_ENOUGH_MEMORY:
26  result = EBPF_NO_MEMORY;
27  break;
28 
29  case ERROR_PATH_NOT_FOUND:
30  result = EBPF_OBJECT_NOT_FOUND;
31  break;
32 
33  case ERROR_NOT_FOUND:
34  result = EBPF_KEY_NOT_FOUND;
35  break;
36 
37  case ERROR_INVALID_PARAMETER:
38  result = EBPF_INVALID_ARGUMENT;
39  break;
40 
41  case ERROR_NO_MORE_ITEMS:
42  case ERROR_NO_MORE_MATCHES:
43  result = EBPF_NO_MORE_KEYS;
44  break;
45 
46  case ERROR_INVALID_HANDLE:
47  result = EBPF_INVALID_FD;
48  break;
49 
50  case ERROR_NOT_SUPPORTED:
52  break;
53 
54  case ERROR_MORE_DATA:
55  result = EBPF_INSUFFICIENT_BUFFER;
56  break;
57 
58  case ERROR_FILE_NOT_FOUND:
59  result = EBPF_FILE_NOT_FOUND;
60  break;
61 
62  case ERROR_ALREADY_INITIALIZED:
63  result = EBPF_ALREADY_INITIALIZED;
64  break;
65 
66  case ERROR_OBJECT_ALREADY_EXISTS:
68  break;
69 
70  case ERROR_IO_PENDING:
71  result = EBPF_PENDING;
72  break;
73 
74  case ERROR_VERIFIER_STOP:
75  result = EBPF_VERIFICATION_FAILED;
76  break;
77 
78  case ERROR_NONE_MAPPED:
80  break;
81 
82  case ERROR_BAD_DRIVER:
84  break;
85 
86  case ERROR_INVALID_FUNCTION:
87  result = EBPF_INVALID_OBJECT;
88  break;
89 
90  case ERROR_OBJECT_NAME_EXISTS:
91  result = EBPF_ALREADY_PINNED;
92  break;
93 
94  case ERROR_TOO_MANY_CMDS:
95  result = EBPF_PROGRAM_TOO_LARGE;
96  break;
97 
98  case RPC_S_CALL_FAILED:
99  result = EBPF_RPC_EXCEPTION;
100  break;
101 
102  case ERROR_BAD_EXE_FORMAT:
103  result = EBPF_ELF_PARSING_FAILED;
104  break;
105 
106  case ERROR_ACCESS_DENIED:
107  result = EBPF_ACCESS_DENIED;
108  break;
109 
110  case ERROR_NOT_OWNER:
111  result = EBPF_NOT_PINNED;
112  break;
113 
114  case ERROR_CONTENT_BLOCKED:
115  result = EBPF_BLOCKED_BY_POLICY;
116  break;
117 
118  case ERROR_ARITHMETIC_OVERFLOW:
119  result = EBPF_ARITHMETIC_OVERFLOW;
120  break;
121 
122  case ERROR_GENERIC_COMMAND_FAILED:
123  result = EBPF_PROGRAM_LOAD_FAILED;
124  break;
125 
126  case ERROR_ALREADY_REGISTERED:
127  // Currently STATUS_ALREADY_REGISTERED is mapped to
128  // ERROR_INTERNAL_ERROR instead of ERROR_ALREADY_REGISTERED.
129  case ERROR_INTERNAL_ERROR:
130  result = EBPF_KEY_ALREADY_EXISTS;
131  break;
132 
133  case ERROR_TOO_MANY_NAMES:
134  result = EBPF_NO_MORE_TAIL_CALLS;
135  break;
136 
137  case ERROR_NO_SYSTEM_RESOURCES:
138  result = EBPF_OUT_OF_SPACE;
139  break;
140 
141  case ERROR_OPERATION_ABORTED:
142  result = EBPF_CANCELED;
143  break;
144 
145  case ERROR_NOACCESS:
146  result = EBPF_INVALID_POINTER;
147  break;
148 
149  case ERROR_TIMEOUT:
150  result = EBPF_TIMEOUT;
151  break;
152 
153  case ERROR_BAD_COMMAND:
154  result = EBPF_STALE_ID;
155  break;
156 
157  case ERROR_INVALID_STATE:
158  result = EBPF_INVALID_STATE;
159  break;
160 
161  default:
162  result = EBPF_FAILED;
163  break;
164  }
165 
166  return result;
167 }
@ EBPF_KEY_NOT_FOUND
The requested key was not found.
Definition: ebpf_result.h:75
@ EBPF_ALREADY_INITIALIZED
The handle was already initialized.
Definition: ebpf_result.h:63
@ EBPF_NO_MORE_KEYS
The enumeration found no more keys.
Definition: ebpf_result.h:93
@ EBPF_INVALID_OBJECT
Invalid object provided (ebpf_object, ebpf_map, ebpf_program).
Definition: ebpf_result.h:33
@ EBPF_INSUFFICIENT_BUFFER
A buffer of insufficient size was supplied.
Definition: ebpf_result.h:90
@ EBPF_BLOCKED_BY_POLICY
The operation was blocked for all requesters by policy.
Definition: ebpf_result.h:81
@ EBPF_FAILED
Generic failure code for all other errors.
Definition: ebpf_result.h:69
@ EBPF_OBJECT_NOT_FOUND
No pinned map or program exists for the path provided.
Definition: ebpf_result.h:39
@ EBPF_JIT_COMPILATION_FAILED
JIT compilation failed.
Definition: ebpf_result.h:24
@ EBPF_EXTENSION_FAILED_TO_LOAD
The eBPF extension failed to load.
Definition: ebpf_result.h:87
@ EBPF_INVALID_POINTER
Invalid pointer.
Definition: ebpf_result.h:111
@ EBPF_ACCESS_DENIED
Access was denied for the requested operation.
Definition: ebpf_result.h:78
@ EBPF_OPERATION_NOT_SUPPORTED
Operation is not supported.
Definition: ebpf_result.h:72
@ EBPF_NO_MEMORY
Low memory.
Definition: ebpf_result.h:54
@ EBPF_OUT_OF_SPACE
The container can not hold additional elements.
Definition: ebpf_result.h:105
@ EBPF_ALREADY_PINNED
The program or map already pinned to a different path.
Definition: ebpf_result.h:48
@ EBPF_FILE_NOT_FOUND
Invalid ELF file path.
Definition: ebpf_result.h:45
@ EBPF_SUCCESS
The operation was successful.
Definition: ebpf_result.h:18
@ EBPF_PROGRAM_TOO_LARGE
The program is too large.
Definition: ebpf_result.h:57
@ EBPF_PENDING
Requested action is still pending.
Definition: ebpf_result.h:102
@ EBPF_ARITHMETIC_OVERFLOW
Arithmetic overflow occurred.
Definition: ebpf_result.h:84
@ EBPF_OBJECT_ALREADY_EXISTS
A program or map is already pinned with the same path.
Definition: ebpf_result.h:42
@ EBPF_INVALID_ARGUMENT
An invalid argument was supplied.
Definition: ebpf_result.h:36
@ EBPF_RPC_EXCEPTION
An RPC exception occurred.
Definition: ebpf_result.h:60
@ EBPF_PROGRAM_LOAD_FAILED
Program load failed.
Definition: ebpf_result.h:27
@ EBPF_NO_MORE_TAIL_CALLS
Caller has reached tail call limit.
Definition: ebpf_result.h:99
@ EBPF_TIMEOUT
Operation timed out.
Definition: ebpf_result.h:114
@ EBPF_STALE_ID
ID is valid, but the object has been deleted.
Definition: ebpf_result.h:117
@ EBPF_INVALID_FD
Invalid FD provided.
Definition: ebpf_result.h:30
@ EBPF_CANCELED
Operation was canceled.
Definition: ebpf_result.h:108
@ EBPF_VERIFICATION_FAILED
Program verification failed.
Definition: ebpf_result.h:21
@ EBPF_ELF_PARSING_FAILED
A failure occurred in parsing the ELF file.
Definition: ebpf_result.h:66
@ EBPF_INVALID_STATE
The system is in an invalid state for this operation.
Definition: ebpf_result.h:120
@ EBPF_NOT_PINNED
The program or map is not pinned.
Definition: ebpf_result.h:51
@ EBPF_KEY_ALREADY_EXISTS
The requested key is already present.
Definition: ebpf_result.h:96
enum ebpf_result ebpf_result_t
_When_(error !=ERROR_SUCCESS, _Ret_range_(1, 65535)) __forceinline ebpf_result_t win32_error_code_to_ebpf_result(uint32_t error)
Definition: ebpf_utilities.h:14