libklvanc
Loading...
Searching...
No Matches
klrestricted_code_path.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Kernel Labs Inc. All Rights Reserved
3 *
4 * Address: Kernel Labs Inc., PO Box 745, St James, NY. 11780
5 * Contact: sales@kernellabs.com
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
42
43#ifndef _KLRESTRICTED_CODE_PATH_H
44#define _KLRESTRICTED_CODE_PATH_H
45
46#include <stdint.h>
47#include <string.h>
48#include <sys/time.h>
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
55{
56 int enableChecking;
57 int id;
58 uint64_t minimumIntervalMs;
59
60 struct timeval lastExecuteTime;
61 uint64_t countBlockEntered;
62 uint64_t countBlockAvoided;
63};
64
65static __inline__ int klrcp_timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
66{
67 /* Perform the carry for the later subtraction by updating y. */
68 if (x->tv_usec < y->tv_usec)
69 {
70 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
71 y->tv_usec -= 1000000 * nsec;
72 y->tv_sec += nsec;
73 }
74 if (x->tv_usec - y->tv_usec > 1000000)
75 {
76 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
77 y->tv_usec += 1000000 * nsec;
78 y->tv_sec -= nsec;
79 }
80
81 /* Compute the time remaining to wait. tv_usec is certainly positive. */
82 result->tv_sec = x->tv_sec - y->tv_sec;
83 result->tv_usec = x->tv_usec - y->tv_usec;
84
85 /* Return 1 if result is negative. */
86 return x->tv_sec < y->tv_sec;
87}
88
89static __inline uint64_t klrcp_timediff_to_msecs(struct timeval *tv)
90{
91 return (tv->tv_sec * 1000) + (tv->tv_usec / 1000);
92}
93
94static __inline__ void klrestricted_code_path_block_initialize(struct klrestricted_code_path_block_s *blk, int id,
95 int enableChecking, uint64_t minimumIntervalMs)
96{
97 memset(blk, 0, sizeof(*blk));
98 blk->id = id;
99 blk->enableChecking = enableChecking;
100 blk->minimumIntervalMs = minimumIntervalMs;
101};
102
103static __inline__ int klrestricted_code_path_block_execute(struct klrestricted_code_path_block_s *blk)
104{
105 if (blk->enableChecking == 0) {
106 blk->countBlockEntered++;
107 return 1;
108 }
109
110 struct timeval now, diff;
111 gettimeofday(&now, 0);
112 klrcp_timeval_subtract(&diff, &now, &blk->lastExecuteTime);
113 if (klrcp_timediff_to_msecs(&diff) < blk->minimumIntervalMs) {
114 blk->countBlockAvoided++;
115 return 0;
116 }
117
118 blk->lastExecuteTime = now;
119 blk->countBlockEntered++;
120 return 1;
121}
122
123#ifdef __cplusplus
124};
125#endif
126
127#endif /* _KLRESTRICTED_CODE_PATH_H */
Definition klrestricted_code_path.h:55