OpenVPN
shaper.c
Go to the documentation of this file.
1 /*
2  * OpenVPN -- An application to securely tunnel IP networks
3  * over a single UDP port, with support for SSL/TLS-based
4  * session authentication and key exchange,
5  * packet encryption, packet authentication, and
6  * packet compression.
7  *
8  * Copyright (C) 2002-2021 OpenVPN Inc <sales@openvpn.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2
12  * as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29 
30 #include "syshead.h"
31 #include "shaper.h"
32 #include "memdbg.h"
33 
34 /*
35  * We want to wake up in delay microseconds. If timeval is larger
36  * than delay, set timeval to delay.
37  */
38 bool
39 shaper_soonest_event(struct timeval *tv, int delay)
40 {
41  bool ret = false;
42  if (delay < 1000000)
43  {
44  if (tv->tv_sec)
45  {
46  tv->tv_sec = 0;
47  tv->tv_usec = delay;
48  ret = true;
49  }
50  else if (delay < tv->tv_usec)
51  {
52  tv->tv_usec = delay;
53  ret = true;
54  }
55  }
56  else
57  {
58  const int sec = delay / 1000000;
59  const int usec = delay % 1000000;
60 
61  if (sec < tv->tv_sec)
62  {
63  tv->tv_sec = sec;
64  tv->tv_usec = usec;
65  ret = true;
66  }
67  else if (sec == tv->tv_sec)
68  {
69  if (usec < tv->tv_usec)
70  {
71  tv->tv_usec = usec;
72  ret = true;
73  }
74  }
75  }
76 #ifdef SHAPER_DEBUG
77  dmsg(D_SHAPER_DEBUG, "SHAPER shaper_soonest_event sec=%" PRIi64 " usec=%ld ret=%d",
78  (int64_t)tv->tv_sec, (long)tv->tv_usec, (int)ret);
79 #endif
80  return ret;
81 }
82 
83 void
85 {
86  CLEAR(s->wakeup);
87 }
88 
89 void
90 shaper_msg(struct shaper *s)
91 {
92  msg(M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
93  s->bytes_per_second);
94 }
#define M_INFO
Definition: errlevel.h:55
#define dmsg(flags,...)
Definition: error.h:157
#define D_SHAPER_DEBUG
Definition: errlevel.h:170
#define CLEAR(x)
Definition: basic.h:33
Definition: shaper.h:47
void shaper_reset_wakeup(struct shaper *s)
Definition: shaper.c:84
bool shaper_soonest_event(struct timeval *tv, int delay)
Definition: shaper.c:39
#define msg(flags,...)
Definition: error.h:153
struct timeval wakeup
Definition: shaper.h:50
void shaper_msg(struct shaper *s)
Definition: shaper.c:90
int bytes_per_second
Definition: shaper.h:49