summaryrefslogtreecommitdiffstats
path: root/fpm/fpm_pb.h
blob: 23d7e43993bea8040926fa0696f466794a33d520 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * fpm_pb.h
 *
 * @copyright Copyright (C) 2016 Sproute Networks, Inc.
 *
 * @author Avneesh Sachdev <avneesh@sproute.com>
 *
 * Portions:
 *   Copyright (C) 2024 Carmine Scarpitta (for SRv6)
 */

/*
 * Public header file for fpm protobuf definitions.
 */

#ifndef _FPM_PB_H
#define _FPM_PB_H

#include "lib/route_types.h"
#include "lib/vrf.h"
#include "qpb/qpb.h"

#include "fpm/fpm.pb-c.h"

/*
 * fpm__route_key__create
 */
#define fpm_route_key_create fpm__route_key__create
static inline Fpm__RouteKey *fpm__route_key__create(qpb_allocator_t *allocator,
						    struct prefix *prefix)
{
	Fpm__RouteKey *key;

	key = QPB_ALLOC(allocator, typeof(*key));
	if (!key) {
		return NULL;
	}
	fpm__route_key__init(key);

	key->prefix = qpb__l3_prefix__create(allocator, prefix);
	if (!key->prefix) {
		return NULL;
	}

	return key;
}

/*
 * fpm__nexthop__create
 */
#define fpm_nexthop_create fpm__nexthop__create
static inline Fpm__Nexthop *
fpm__nexthop__create(qpb_allocator_t *allocator, struct nexthop *nh)
{
	Fpm__Nexthop *nexthop;
	uint8_t family;

	nexthop = QPB_ALLOC(allocator, typeof(*nexthop));
	if (!nexthop)
		return NULL;

	fpm__nexthop__init(nexthop);

	if (nh->type == NEXTHOP_TYPE_IPV4 ||
	    nh->type == NEXTHOP_TYPE_IPV4_IFINDEX)
		family = AF_INET;
	else if (nh->type == NEXTHOP_TYPE_IPV6 ||
		 nh->type == NEXTHOP_TYPE_IPV6_IFINDEX)
		family = AF_INET6;
	else
		return NULL;

	nexthop->if_id = qpb__if_identifier__create(allocator, nh->ifindex);
	if (!nexthop->if_id)
		return NULL;

	nexthop->address = qpb__l3_address__create(allocator, &nh->gate, family);
	if (!nexthop->address)
		return NULL;


	return nexthop;
}

/*
 * fpm__nexthop__get
 *
 * Read out information from a protobuf nexthop structure.
 */
#define fpm_nexthop_get fpm__nexthop__get
static inline int fpm__nexthop__get(const Fpm__Nexthop *nh,
				    struct nexthop *nexthop)
{
	struct in_addr ipv4;
	struct in6_addr ipv6;
	uint32_t ifindex;
	char *ifname;

	if (!nh)
		return 0;

	if (!qpb_if_identifier_get(nh->if_id, &ifindex, &ifname))
		return 0;

	if (nh->address) {
		if (nh->address->v4) {
			memset(&ipv4, 0, sizeof(ipv4));
			if (!qpb__ipv4_address__get(nh->address->v4, &ipv4))
				return 0;

			nexthop->vrf_id = VRF_DEFAULT;
			nexthop->type = NEXTHOP_TYPE_IPV4;
			nexthop->gate.ipv4 = ipv4;
			if (ifindex) {
				nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
				nexthop->ifindex = ifindex;
			}
			return 1;
		}

		if (nh->address->v6) {
			memset(&ipv6, 0, sizeof(ipv6));
			if (!qpb__ipv6_address__get(nh->address->v6, &ipv6))
				return 0;
			nexthop->vrf_id = VRF_DEFAULT;
			nexthop->type = NEXTHOP_TYPE_IPV6;
			nexthop->gate.ipv6 = ipv6;
			if (ifindex) {
				nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
				nexthop->ifindex = ifindex;
			}
			return 1;
		}
	}

	return 0;
}

/*
 * fpm__srv6_sid_format__create
 */
#define fpm_srv6_sid_format_create fpm__srv6_sid_format__create
static inline Fpm__SRv6SIDFormat *
fpm__srv6_sid_format__create(qpb_allocator_t *allocator,
			     uint8_t locator_block_length,
			     uint8_t locator_node_length,
			     uint8_t function_length, uint8_t argument_length)
{
	Fpm__SRv6SIDFormat *sid_format;

	sid_format = QPB_ALLOC(allocator, typeof(*sid_format));
	if (!sid_format)
		return NULL;
	fpm__srv6_sidformat__init(sid_format);

	sid_format->locator_block_length = locator_block_length;
	sid_format->locator_node_length = locator_node_length;
	sid_format->function_length = function_length;
	sid_format->argument_length = argument_length;

	return sid_format;
}

/*
 * fpm__srv6_local_sid_end_behavior__create
 */
#define fpm_srv6_local_sid_end_behavior_create                                 \
	fpm__srv6_local_sid_end_behavior__create
static inline Fpm__SRv6LocalSID__End *
fpm__srv6_local_sid_end_behavior__create(qpb_allocator_t *allocator)
{
	Fpm__SRv6LocalSID__End *end;

	end = QPB_ALLOC(allocator, typeof(*end));
	if (!end)
		return NULL;

	fpm__srv6_local_sid__end__init(end);

	return end;
}

/*
 * fpm__srv6_local_sid_end_x_behavior__create
 */
#define fpm_srv6_local_sid_end_x_behavior_create                               \
	fpm__srv6_local_sid_end_x_behavior__create
static inline Fpm__SRv6LocalSID__EndX *
fpm__srv6_local_sid_end_x_behavior__create(qpb_allocator_t *allocator,
					   struct nexthop *nexthop)
{
	Fpm__SRv6LocalSID__EndX *end_x;

	end_x = QPB_ALLOC(allocator, typeof(*end_x));
	if (!end_x)
		return NULL;

	fpm__srv6_local_sid__end_x__init(end_x);

	end_x->nexthop = fpm_nexthop_create(allocator, nexthop);

	return end_x;
}

/*
 * fpm__srv6_local_sid_end_t_behavior__create
 */
#define fpm_srv6_local_sid_end_t_behavior_create                               \
	fpm__srv6_local_sid_end_t_behavior__create
static inline Fpm__SRv6LocalSID__EndT *
fpm__srv6_local_sid_end_t_behavior__create(qpb_allocator_t *allocator,
					   vrf_id_t vrf_id)
{
	Fpm__SRv6LocalSID__EndT *end_t;

	end_t = QPB_ALLOC(allocator, typeof(*end_t));
	if (!end_t)
		return NULL;

	fpm__srv6_local_sid__end_t__init(end_t);

	end_t->vrf_id = vrf_id;

	return end_t;
}

/*
 * fpm__srv6_local_sid_end_dx6_behavior__create
 */
#define fpm_srv6_local_sid_end_dx6_behavior_create                             \
	fpm__srv6_local_sid_end_dx6_behavior__create
static inline Fpm__SRv6LocalSID__EndDX6 *
fpm__srv6_local_sid_end_dx6_behavior__create(qpb_allocator_t *allocator,
					     struct nexthop *nexthop)
{
	Fpm__SRv6LocalSID__EndDX6 *end_dx6;

	end_dx6 = QPB_ALLOC(allocator, typeof(*end_dx6));
	if (!end_dx6)
		return NULL;

	fpm__srv6_local_sid__end_dx6__init(end_dx6);

	end_dx6->nexthop = fpm_nexthop_create(allocator, nexthop);

	return end_dx6;
}

/*
 * fpm__srv6_local_sid_end_dx4_behavior__create
 */
#define fpm_srv6_local_sid_end_dx4_behavior_create                             \
	fpm__srv6_local_sid_end_dx4_behavior__create
static inline Fpm__SRv6LocalSID__EndDX4 *
fpm__srv6_local_sid_end_dx4_behavior__create(qpb_allocator_t *allocator,
					     struct nexthop *nexthop)
{
	Fpm__SRv6LocalSID__EndDX4 *end_dx4;

	end_dx4 = QPB_ALLOC(allocator, typeof(*end_dx4));
	if (!end_dx4)
		return NULL;

	fpm__srv6_local_sid__end_dx4__init(end_dx4);

	end_dx4->nexthop = fpm_nexthop_create(allocator, nexthop);

	return end_dx4;
}

/*
 * fpm__srv6_local_sid_end_dt6_behavior__create
 */
#define fpm_srv6_local_sid_end_dt6_behavior_create                             \
	fpm__srv6_local_sid_end_dt6_behavior__create
static inline Fpm__SRv6LocalSID__EndDT6 *
fpm__srv6_local_sid_end_dt6_behavior__create(qpb_allocator_t *allocator,
					     vrf_id_t vrf_id)
{
	Fpm__SRv6LocalSID__EndDT6 *end_dt6;

	end_dt6 = QPB_ALLOC(allocator, typeof(*end_dt6));
	if (!end_dt6)
		return NULL;

	fpm__srv6_local_sid__end_dt6__init(end_dt6);

	end_dt6->vrf_id = vrf_id;

	return end_dt6;
}

/*
 * fpm__srv6_local_sid_end_dt4_behavior__create
 */
#define fpm_srv6_local_sid_end_dt4_behavior_create                             \
	fpm__srv6_local_sid_end_dt4_behavior__create
static inline Fpm__SRv6LocalSID__EndDT4 *
fpm__srv6_local_sid_end_dt4_behavior__create(qpb_allocator_t *allocator,
					     vrf_id_t vrf_id)
{
	Fpm__SRv6LocalSID__EndDT4 *end_dt4;

	end_dt4 = QPB_ALLOC(allocator, typeof(*end_dt4));
	if (!end_dt4)
		return NULL;

	fpm__srv6_local_sid__end_dt4__init(end_dt4);

	end_dt4->vrf_id = vrf_id;

	return end_dt4;
}

/*
 * fpm__srv6_local_sid_end_dt46_behavior__create
 */
#define fpm_srv6_local_sid_end_dt46_behavior_create                            \
	fpm__srv6_local_sid_end_dt46_behavior__create
static inline Fpm__SRv6LocalSID__EndDT46 *
fpm__srv6_local_sid_end_dt46_behavior__create(qpb_allocator_t *allocator,
					      vrf_id_t vrf_id)
{
	Fpm__SRv6LocalSID__EndDT46 *end_dt46;

	end_dt46 = QPB_ALLOC(allocator, typeof(*end_dt46));
	if (!end_dt46)
		return NULL;

	fpm__srv6_local_sid__end_dt46__init(end_dt46);

	end_dt46->vrf_id = vrf_id;

	return end_dt46;
}

/*
 * fpm__srv6_local_sid_un_behavior__create
 */
#define fpm_srv6_local_sid_un_behavior_create                                  \
	fpm__srv6_local_sid_un_behavior__create
static inline Fpm__SRv6LocalSID__UN *
fpm__srv6_local_sid_un_behavior__create(qpb_allocator_t *allocator)
{
	Fpm__SRv6LocalSID__UN *un;

	un = QPB_ALLOC(allocator, typeof(*un));
	if (!un)
		return NULL;

	fpm__srv6_local_sid__un__init(un);

	return un;
}

/*
 * fpm__srv6_local_sid_ua_behavior__create
 */
#define fpm_srv6_local_sid_ua_behavior_create                                  \
	fpm__srv6_local_sid_ua_behavior__create
static inline Fpm__SRv6LocalSID__UA *
fpm__srv6_local_sid_ua_behavior__create(qpb_allocator_t *allocator,
					struct nexthop *nexthop)
{
	Fpm__SRv6LocalSID__UA *ua;

	ua = QPB_ALLOC(allocator, typeof(*ua));
	if (!ua)
		return NULL;

	fpm__srv6_local_sid__ua__init(ua);

	ua->nexthop = fpm_nexthop_create(allocator, nexthop);

	return ua;
}

/*
 * fpm__srv6_local_sid_udt6_behavior__create
 */
#define fpm_srv6_local_sid_udt6_behavior_create                                \
	fpm__srv6_local_sid_udt6_behavior__create
static inline Fpm__SRv6LocalSID__UDT6 *
fpm__srv6_local_sid_udt6_behavior__create(qpb_allocator_t *allocator,
					  vrf_id_t vrf_id)
{
	Fpm__SRv6LocalSID__UDT6 *udt6;

	udt6 = QPB_ALLOC(allocator, typeof(*udt6));
	if (!udt6)
		return NULL;

	fpm__srv6_local_sid__udt6__init(udt6);

	udt6->vrf_id = vrf_id;

	return udt6;
}

/*
 * fpm__srv6_local_sid_udt4_behavior__create
 */
#define fpm_srv6_local_sid_udt4_behavior_create                                \
	fpm__srv6_local_sid_udt4_behavior__create
static inline Fpm__SRv6LocalSID__UDT4 *
fpm__srv6_local_sid_udt4_behavior__create(qpb_allocator_t *allocator,
					  vrf_id_t vrf_id)
{
	Fpm__SRv6LocalSID__UDT4 *udt4;

	udt4 = QPB_ALLOC(allocator, typeof(*udt4));
	if (!udt4)
		return NULL;

	fpm__srv6_local_sid__udt4__init(udt4);

	udt4->vrf_id = vrf_id;

	return udt4;
}

/*
 * fpm__srv6_local_sid_udt46_behavior__create
 */
#define fpm_srv6_local_sid_udt46_behavior_create                               \
	fpm__srv6_local_sid_udt46_behavior__create
static inline Fpm__SRv6LocalSID__UDT46 *
fpm__srv6_local_sid_udt46_behavior__create(qpb_allocator_t *allocator,
					   vrf_id_t vrf_id)
{
	Fpm__SRv6LocalSID__UDT46 *udt46;

	udt46 = QPB_ALLOC(allocator, typeof(*udt46));
	if (!udt46)
		return NULL;

	fpm__srv6_local_sid__udt46__init(udt46);

	udt46->vrf_id = vrf_id;

	return udt46;
}

#endif