aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/facemgr/includes/hicn/facemgr/facelet.h
blob: cfdc5540eeac0b32a7c4268bcc0beda219e103e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \file facelet.h
 * \brief Facelet
 *
 * A facelet consists in partial information and annotations collected towards
 * the contruction of the final face that will be sent to the forwarder.
 *
 * It might also consist in a pattern allowing the deletion of a group of face
 * for instance.
 */
#ifndef FACEMGR_FACELET_H
#define FACEMGR_FACELET_H

#include <stdbool.h>

#include <hicn/ctrl/face.h>
#include <hicn/ctrl/route.h>

#define MAXSZ_FACELET 1024

#define FACELET_MAX_ERRORS 10

/* NOTE: Any test should be sufficient */
#define IS_VALID_NETDEVICE(netdevice) ((netdevice.index != 0) && (netdevice.name[0] != '\0'))

typedef struct facelet_s facelet_t;

/* Face type */

#define foreach_face_type_layer \
    _(UNDEFINED)                \
    _(3)                        \
    _(4)                        \
    _(N)

typedef enum {
#define _(x) FACE_TYPE_LAYER_ ## x,
    foreach_face_type_layer
#undef _
} face_type_layer_t;

#define foreach_face_type_encap \
    _(UNDEFINED)                \
    _(TCP)                      \
    _(UDP)                      \
    _(N)

typedef enum {
#define _(x) FACE_TYPE_ENCAP_ ## x,
    foreach_face_type_encap
#undef _
} face_type_encap_t;

typedef struct {
   face_type_layer_t layer;
   face_type_encap_t encap;
} facemgr_face_type_t;


extern const char * face_type_layer_str[];
extern const char * face_type_encap_str[];

#define FACEMGR_FACE_TYPE_STR(x)                                \
    face_type_layer_str[x.layer], face_type_encap_str[x.encap]

#define FACEMGR_FACE_TYPE_UNDEFINED (facemgr_face_type_t) {     \
    .layer = FACE_TYPE_LAYER_UNDEFINED,                         \
    .encap = FACE_TYPE_ENCAP_UNDEFINED,                         \
}

#define FACEMGR_FACE_TYPE_NATIVE_UDP (facemgr_face_type_t) {    \
    .layer = FACE_TYPE_LAYER_3,                                 \
    .encap = FACE_TYPE_ENCAP_UDP,                               \
}

#define FACEMGR_FACE_TYPE_NATIVE_TCP (facemgr_face_type_t) {    \
    .layer = FACE_TYPE_LAYER_3,                                 \
    .encap = FACE_TYPE_ENCAP_TCP,                               \
}

#define FACEMGR_FACE_TYPE_OVERLAY_UDP (facemgr_face_type_t) {   \
    .layer = FACE_TYPE_LAYER_4,                                 \
    .encap = FACE_TYPE_ENCAP_UDP,                               \
}

#define FACEMGR_FACE_TYPE_OVERLAY_TCP (facemgr_face_type_t) {   \
    .layer = FACE_TYPE_LAYER_4,                                 \
    .encap = FACE_TYPE_ENCAP_TCP,                               \
}

/* Facelet status */
#define foreach_facelet_status  \
    _(UNDEFINED)                \
    _(DOWN)                     \
    _(UNCERTAIN)                \
    _(INCOMPLETE)               \
    _(CREATE)                   \
    _(CLEAN)                    \
    _(IGNORED)                  \
    _(UPDATE)                   \
    _(DELETE)                   \
    _(DELETED)                  \
    _(N)

typedef enum {
#define _(x) FACELET_STATUS_ ## x,
    foreach_facelet_status
#undef _
} facelet_status_t;

extern const char * facelet_status_str[];

/* Facelet error reason */
#define foreach_facelet_error_reason    \
    _(UNDEFINED)                        \
    _(UNSPECIFIED_ERROR)                \
    _(FORWARDER_OFFLINE)                \
    _(PERMISSION_DENIED)                \
    _(INTERNAL_ERROR)                   \
    _(N)

typedef enum {
#define _(x) FACELET_ERROR_REASON_ ## x,
    foreach_facelet_error_reason
#undef _
} facelet_error_reason_t;

extern const char * facelet_error_reason_str[];

/* Facelet attribute status */

/*
 * We expect an attribute in the cache to be able to take any value but
 * UNDEFINED and N, which facelet events should either be UNSET or CLEAN
 */
#define foreach_facelet_attr_status             \
    _(UNDEFINED, '?')                           \
    _(UNSET, 'X')                               \
    _(CLEAN, ' ')                               \
    _(DIRTY, '*')                               \
    _(PENDING, 'P')                             \
    _(CONFLICT, '!')                            \
    _(N, '-')

typedef enum {
#define _(x, y) FACELET_ATTR_STATUS_ ## x,
    foreach_facelet_attr_status
#undef _
} facelet_attr_status_t;

extern const char * facelet_attr_status_str[];
extern const char * facelet_attr_status_str_short[];

/* Facelet attribute */

#ifdef WITH_POLICY
#define foreach_facelet_attr                    \
    _(netdevice_type_t, netdevice_type)         \
    _(netdevice_t, netdevice)                   \
    _(int, family)                              \
    _(ip_address_t, local_addr)                 \
    _(u16, local_port)                          \
    _(ip_address_t, remote_addr)                \
    _(u16, remote_port)                         \
    _(face_state_t, admin_state)                \
    _(face_state_t, state)                      \
    _(u32, priority)                            \
    _(facemgr_face_type_t, face_type)
#else
#define foreach_facelet_attr                    \
    _(netdevice_type_t, netdevice_type)         \
    _(netdevice_t, netdevice)                   \
    _(int, family)                              \
    _(ip_address_t, local_addr)                 \
    _(u16, local_port)                          \
    _(ip_address_t, remote_addr)                \
    _(u16, remote_port)                         \
    _(face_state_t, admin_state)                \
    _(face_state_t, state)                      \
    _(facemgr_face_type_t, face_type)
#endif /* WITH_POLICY */

#define foreach_facelet_event   \
    _(UNDEFINED)                \
    _(GET)                      \
    _(CREATE)                   \
    _(UPDATE)                   \
    _(DELETE)                   \
    _(SET_UP)                   \
    _(SET_DOWN)                 \
    _(N)

#define MAXSZ_EVENT__ 10
#define MAXSZ_EVENT_ MAXSZ_EVENT_ + 1

/**
 * \brief Enumeration of the possible types of event
 */
typedef enum {
#define _(x) FACELET_EVENT_ ## x,
foreach_facelet_event
#undef _
} facelet_event_t;

extern const char * facelet_event_str[];

/**
 * \brief Create a facelet.
 */
facelet_t * facelet_create();

facelet_t * facelet_create_from_netdevice(netdevice_t * netdevice);

unsigned facelet_get_id(facelet_t * facelet);
void facelet_set_id(facelet_t * facelet, unsigned id);

int facelet_validate_face(const facelet_t * facelet);

facelet_t * facelet_create_from_face(face_t * face);

void facelet_free(facelet_t * facelet);

facelet_t * facelet_dup(const facelet_t * current_facelet);

int facelet_cmp(const facelet_t * f1, const facelet_t * f2);

bool facelet_equals(const facelet_t * facelet1, const facelet_t * facelet2);

/* NOTE: only clean attributes are matched */
bool facelet_match(const facelet_t * facelet, const facelet_t * facelet_match);

/**
 * \brief Returns whether the specified facelet has all key attributes defined.
 *
 * Key attributes are netdevice and family. If both are present, this allows to
 * uniquely identify a facelet, otherwise it is a 'wildcard' facelet
 * specification and might match several facelets.
 */
bool facelet_has_key(const facelet_t * facelet);

#define FACELET_ACCESSORS_H(TYPE, NAME)                                         \
bool facelet_has_ ## NAME(const facelet_t * facelet);                           \
facelet_attr_status_t facelet_get_ ## NAME ## _status(const facelet_t * facelet);\
void facelet_set_ ## NAME ## _status(facelet_t * facelet,                       \
        facelet_attr_status_t status);                                          \
int facelet_get_ ## NAME(const facelet_t * facelet, TYPE * NAME);               \
int facelet_set_ ## NAME(facelet_t * facelet, TYPE NAME);                       \
int facelet_unset_ ## NAME(facelet_t * facelet);

#define _(TYPE, NAME) FACELET_ACCESSORS_H(TYPE, NAME)
foreach_facelet_attr
#undef _

int facelet_get_face(const facelet_t * facelet, face_t ** pface);

int facelet_merge(facelet_t * facelet, facelet_t * facelet_to_merge);

facelet_status_t facelet_get_status(const facelet_t * facelet);
void facelet_set_status(facelet_t * facelet, facelet_status_t status);
void facelet_set_attr_clean(facelet_t * facelet);

void facelet_set_error(facelet_t * facelet, facelet_error_reason_t reason);
void facelet_unset_error(facelet_t * facelet);
bool facelet_get_error(const facelet_t * facelet);

void facelet_set_bj_done(facelet_t * facelet);
void facelet_unset_bj_done(facelet_t * facelet);
bool facelet_is_bj_done(const facelet_t * facelet);
void facelet_set_au_done(facelet_t * facelet);
bool facelet_is_au_done(const facelet_t * facelet);

facelet_event_t facelet_get_event(const facelet_t * facelet);
void facelet_set_event(facelet_t * facelet, facelet_event_t event);

int facelet_add_route(facelet_t * facelet, hicn_route_t * route);
int facelet_remove_route(facelet_t * facelet, hicn_route_t * route, hicn_route_t ** route_removed);
int facelet_clear_routes(facelet_t * facelet);
int facelet_get_route_array(const facelet_t * facelet, hicn_route_t *** route_array);

int facelet_snprintf(char * buf, size_t size, const facelet_t * facelet);

#define DUMP_FACELET(msg, facelet) do {                 \
    char buf[MAXSZ_FACELET];                            \
    facelet_snprintf(buf, MAXSZ_FACELET, facelet);      \
    DEBUG("%s : %s", msg, buf);                         \
} while(0)

int facelet_snprintf_json(char * buf, size_t size, const facelet_t * facelet, int indent);

#endif /* FACEMGR_FACELET_H */