OpenVPN 3 Core Library
Loading...
Searching...
No Matches
core.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Linux method for binding a thread to a particular core.
13
14#ifndef OPENVPN_LINUX_CORE_H
15#define OPENVPN_LINUX_CORE_H
16
17#include <pthread.h>
18
20
21namespace openvpn {
22
23inline int bind_to_core(const int core_id)
24{
25 const int num_cores = n_cores();
26 if (core_id >= num_cores)
27 return EINVAL;
28
29 cpu_set_t cpuset;
30 CPU_ZERO(&cpuset);
31 CPU_SET(core_id, &cpuset);
32
33 pthread_t current_thread = pthread_self();
34 return pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);
35}
36
37inline int exclude_from_core(const int core_id)
38{
39 const int num_cores = n_cores();
40 if (num_cores <= 1 || core_id >= num_cores)
41 return EINVAL;
42
43 cpu_set_t cpuset;
44 CPU_ZERO(&cpuset);
45 for (int i = 0; i < num_cores; ++i)
46 if (i != core_id)
47 CPU_SET(i, &cpuset);
48
49 pthread_t current_thread = pthread_self();
50 return pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);
51}
52} // namespace openvpn
53
54#endif
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
int n_cores()
Definition core.hpp:32
int bind_to_core(const int core_id)
Definition core.hpp:23
int exclude_from_core(const int core_id)
Definition core.hpp:37