Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
policy.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
20 #ifndef HICN_POLICY_H
21 #define HICN_POLICY_H
22 
23 #ifndef _WIN32
24 #include <netinet/in.h> // INET*_ADDRSTRLEN
25 #else
26 #include <hicn/util/win_portability.h>
27 #endif
28 #include <string.h> // strcasecmp
29 #include <hicn/util/token.h>
30 
31 /* POLICY TAG */
32 
33 #define foreach_policy_tag \
34  /* Interface type */ \
35  _(WIRED, 'E') \
36  _(WIFI, 'W') \
37  _(CELLULAR, 'C') \
38  /* QoS */ \
39  _(BEST_EFFORT, 'b') \
40  _(REALTIME, 'r') \
41  _(MULTIPATH, 'M') \
42  /* Security */ \
43  _(TRUSTED, 'T')
44 
45 typedef enum {
46 #define _(x, y) POLICY_TAG_ ## x,
47 foreach_policy_tag
48 #undef _
49  POLICY_TAG_N
50 } policy_tag_t;
51 
52 #define MAXSZ_POLICY_TAG_ 11
53 #define MAXSZ_POLICY_TAG MAXSZ_POLICY_TAG_ + 1
54 
55 extern const char * policy_tag_str[];
56 extern const char policy_tag_short_str[];
57 
58 static inline
59 policy_tag_t
60 policy_tag_from_str(const char * str)
61 {
62 #define _(x, y) if (strcasecmp(str, policy_tag_str[POLICY_TAG_ ## x] ) == 0) { return POLICY_TAG_ ## x; } else
63 foreach_policy_tag
64 #undef _
65  return POLICY_TAG_N;
66 }
67 
68 /* POLICY_TAGS */
69 
70 typedef int policy_tags_t;
71 
72 static inline
73 void policy_tags_add(policy_tags_t * tags, policy_tag_t tag)
74 {
75  *tags |= (1 << tag);
76 }
77 
78 static inline
79 void policy_tags_remove(policy_tags_t * tags, policy_tag_t tag)
80 {
81  *tags &= ~(1 << tag);
82 }
83 
84 static inline
85 int policy_tags_has(policy_tags_t tags, policy_tag_t tag)
86 {
87  return tags & (1 << tag);
88 }
89 
90 static inline
91 void policy_tags_union(policy_tags_t * tags, policy_tags_t * tags_to_union)
92 {
93 #define _(x, y) *tags |= policy_tags_has(*tags_to_union, POLICY_TAG_ ## x) ? (1 << POLICY_TAG_ ## x) : 0;
94 foreach_policy_tag
95 #undef _
96 }
97 
98 #define POLICY_TAGS_EMPTY 0
99 
100 static inline
101 int
102 policy_tags_snprintf(char * s, size_t size, policy_tags_t tags)
103 {
104 #define _(x, y) s[POLICY_TAG_ ## x] = policy_tags_has(tags, POLICY_TAG_ ## x) ? y : '.';
105 foreach_policy_tag
106 #undef _
107  s[POLICY_TAG_N] = '\0';
108  return POLICY_TAG_N + 1;
109 }
110 
111 #define MAXSZ_POLICY_TAGS_ POLICY_TAG_N + 1
112 #define MAXSZ_POLICY_TAGS MAXSZ_POLICY_TAGS_ + 1
113 
114 /* POLICY STATE */
115 
116 /* TODO vs. weight */
117 
118 #define foreach_policy_state \
119  _(NEUTRAL) \
120  _(REQUIRE) \
121  _(PREFER) \
122  _(AVOID) \
123  _(PROHIBIT) \
124  _(N)
125 
126 typedef enum {
127 #define _(x) POLICY_STATE_ ## x,
128 foreach_policy_state
129 #undef _
130 } policy_state_t;
131 
132 #define MAXSZ_POLICY_STATE_ 8
133 #define MAXSZ_POLICY_STATE MAXSZ_POLICY_STATE_ + 1
134 
135 extern const char * policy_state_str[];
136 
137 
138 /* POLICY TAG STATE */
139 
140 typedef struct {
141  policy_state_t state;
142  uint8_t disabled;
144 
145 #define MAXSZ_POLICY_TAG_STATE_ 8
146 #define MAXSZ_POLICY_TAG_STATE MAXSZ_POLICY_TAG_STATE_ + 1
147 
148 int policy_tag_state_snprintf(char * s, size_t size, const policy_tag_state_t * tag_state);
149 
150 
151 /* INTERFACE STATS */
152 
153 typedef struct {
154  float throughput;
155  float latency;
156  float loss_rate;
158 
159 #define INTERFACE_STATS_NONE { \
160  .throughput = 0, \
161  .latency = 0, \
162  .loss_rate = 0, \
163 }
164 
165 
166 /* POLICY STATS */
167 
168 typedef struct {
169  interface_stats_t wired;
170  interface_stats_t wifi;
171  interface_stats_t cellular;
172  interface_stats_t all;
174 
175 #define POLICY_STATS_NONE { \
176  .wired = INTERFACE_STATS_NONE, \
177  .wifi = INTERFACE_STATS_NONE, \
178  .cellular = INTERFACE_STATS_NONE, \
179  .all = INTERFACE_STATS_NONE, \
180 }
181 
182 typedef struct {
183  uint32_t num_packets;
184  uint32_t num_bytes;
185  uint32_t num_losses;
186  uint32_t latency_idle;
188 
189 #define INTERFACE_COUNTERS_NONE { \
190  .num_packets = 0, \
191  .num_bytes = 0, \
192  .num_losses = 0, \
193  .latency_idle = 0, \
194 }
195 
196 typedef struct {
197  interface_counters_t wired;
199  interface_counters_t cellular;
201  uint64_t last_update;
203 
204 #define POLICY_COUNTERS_NONE (policy_counters_t) { \
205  .wired = INTERFACE_COUNTERS_NONE, \
206  .wifi = INTERFACE_COUNTERS_NONE, \
207  .cellular = INTERFACE_COUNTERS_NONE, \
208  .all = INTERFACE_COUNTERS_NONE, \
209  .last_update = 0, \
210 }
211 
212 /* POLICY */
213 
214 #define APP_NAME_LEN 128
215 
216 typedef struct {
217  char app_name[APP_NAME_LEN];
218  policy_tag_state_t tags[POLICY_TAG_N];
219  policy_stats_t stats;
220 } hicn_policy_t;
221 
222 static const hicn_policy_t POLICY_NONE = {
223  .app_name = { 0 },
224  .tags = {
225 #ifdef __ANDROID__
226 #define _(x, y) { POLICY_STATE_NEUTRAL, 0 },
227 #else
228 #define _(x, y) [POLICY_TAG_ ## x] = { POLICY_STATE_NEUTRAL, 0 },
229 #endif
230 foreach_policy_tag
231 #undef _
232  },
233  .stats = POLICY_STATS_NONE,
234 };
235 
236 
237 /* POLICY DESCRIPTION */
238 
239 #define PFX_STRLEN 4 /* eg. /128 */
240 
241 typedef struct {
242  int family;
243  union {
244  char ipv4_prefix[INET_ADDRSTRLEN + PFX_STRLEN];
245  char ipv6_prefix[INET6_ADDRSTRLEN + PFX_STRLEN];
246  };
247  hicn_policy_t policy;
249 
250 #endif /* HICN_POLICY_H */
interface_counters_t
Definition: policy.h:182
policy_description_t
Definition: policy.h:241
hicn_policy_t
Definition: policy.h:216
policy_stats_t
Definition: policy.h:168
policy_counters_t
Definition: policy.h:196
policy_tag_state_t
Definition: policy.h:140
interface_stats_t
Definition: policy.h:153