1 /*
2  * Label Manager header
3  *
4  * Copyright (C) 2017 by Bingen Eguzkitza,
5  *                       Volta Networks Inc.
6  *
7  * This file is part of FRRouting (FRR)
8  *
9  * FRR is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; either version 2, or (at your option) any
12  * later version.
13  *
14  * FRR is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * 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; see the file COPYING; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #ifndef _LABEL_MANAGER_H
25 #define _LABEL_MANAGER_H
26 
27 #include <stdint.h>
28 
29 #include "lib/linklist.h"
30 #include "lib/thread.h"
31 #include "lib/hook.h"
32 
33 #include "zebra/zserv.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #define NO_PROTO 0
40 
41 /*
42  * Label chunk struct
43  * Client daemon which the chunk belongs to can be identified by a tuple of:
44  * proto (daemon protocol) + instance + zapi session_id
45  * If the client then passes a non-empty value to keep field when it requests
46  * for chunks, the chunks won't be garbage collected and the client will be
47  * responsible for releasing them.
48  * Otherwise, if the keep field is not set (value 0) for the chunk, it will be
49  * automatically released when the client disconnects or when it reconnects
50  * (in case it died unexpectedly, we can know it's the same because it will have
51  * the same proto+instance+session values)
52  */
53 struct label_manager_chunk {
54 	uint8_t proto;
55 	unsigned short instance;
56 	uint32_t session_id;
57 	uint8_t keep;
58 	uint32_t start; /* First label of the chunk */
59 	uint32_t end;   /* Last label of the chunk */
60 };
61 
62 /* declare hooks for the basic API, so that it can be specialized or served
63  * externally. Also declare a hook when those functions have been registered,
64  * so that any external module wanting to replace those can react
65  */
66 
67 DECLARE_HOOK(lm_client_connect, (struct zserv *client, vrf_id_t vrf_id),
68 	     (client, vrf_id));
69 DECLARE_HOOK(lm_client_disconnect, (struct zserv *client), (client));
70 DECLARE_HOOK(lm_get_chunk,
71 	     (struct label_manager_chunk * *lmc, struct zserv *client,
72 	      uint8_t keep, uint32_t size, uint32_t base, vrf_id_t vrf_id),
73 	     (lmc, client, keep, size, base, vrf_id));
74 DECLARE_HOOK(lm_release_chunk,
75 	     (struct zserv *client, uint32_t start, uint32_t end),
76 	     (client, start, end));
77 DECLARE_HOOK(lm_cbs_inited, (), ());
78 
79 
80 /* declare wrappers to be called in zapi_msg.c (as hooks must be called in
81  * source file where they were defined)
82  */
83 void lm_client_connect_call(struct zserv *client, vrf_id_t vrf_id);
84 void lm_get_chunk_call(struct label_manager_chunk **lmc, struct zserv *client,
85 		       uint8_t keep, uint32_t size, uint32_t base,
86 		       vrf_id_t vrf_id);
87 void lm_release_chunk_call(struct zserv *client, uint32_t start,
88 			   uint32_t end);
89 
90 /* API for an external LM to return responses for requests */
91 int lm_client_connect_response(uint8_t proto, uint16_t instance,
92 			       uint32_t session_id, vrf_id_t vrf_id,
93 			       uint8_t result);
94 int lm_get_chunk_response(struct label_manager_chunk *lmc, struct zserv *client,
95 			  vrf_id_t vrf_id);
96 
97 /* convenience function to allocate an lmc to be consumed by the above API */
98 struct label_manager_chunk *create_label_chunk(uint8_t proto,
99 					       unsigned short instance,
100 					       uint32_t session_id,
101 					       uint8_t keep, uint32_t start,
102 					       uint32_t end);
103 void delete_label_chunk(void *val);
104 
105 /* register/unregister callbacks for hooks */
106 void lm_hooks_register(void);
107 void lm_hooks_unregister(void);
108 
109 /*
110  * Main label manager struct
111  * Holds a linked list of label chunks.
112  */
113 struct label_manager {
114 	struct list *lc_list;
115 };
116 
117 void label_manager_init(void);
118 struct label_manager_chunk *assign_label_chunk(uint8_t proto,
119 					       unsigned short instance,
120 					       uint32_t session_id,
121 					       uint8_t keep, uint32_t size,
122 					       uint32_t base);
123 int release_label_chunk(uint8_t proto, unsigned short instance,
124 			uint32_t session_id, uint32_t start, uint32_t end);
125 int lm_client_disconnect_cb(struct zserv *client);
126 int release_daemon_label_chunks(struct zserv *client);
127 void label_manager_close(void);
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 #endif /* _LABEL_MANAGER_H */
134