]>
Commit | Line | Data |
---|---|---|
1c84efe4 CH |
1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | /* | |
3 | * Main routine of mgmt. | |
4 | * | |
5 | * Copyright (C) 2021 Vmware, Inc. | |
6 | * Pushpasis Sarkar | |
7 | */ | |
8 | ||
9 | #include <zebra.h> | |
10 | #include "lib/version.h" | |
11 | #include "routemap.h" | |
12 | #include "filter.h" | |
13 | #include "libfrr.h" | |
14 | #include "frr_pthread.h" | |
15 | #include "mgmtd/mgmt.h" | |
16 | #include "mgmtd/mgmt_ds.h" | |
17 | #include "routing_nb.h" | |
18 | ||
7d65b7b7 | 19 | |
8033bf39 CH |
20 | char const *const mgmt_daemons[] = { |
21 | #ifdef HAVE_STATICD | |
22 | "staticd", | |
23 | #endif | |
24 | }; | |
25 | uint mgmt_daemons_count = array_size(mgmt_daemons); | |
26 | ||
1c84efe4 CH |
27 | /* mgmt options, we use GNU getopt library. */ |
28 | static const struct option longopts[] = { | |
29 | {"skip_runas", no_argument, NULL, 'S'}, | |
30 | {"no_zebra", no_argument, NULL, 'Z'}, | |
31 | {"socket_size", required_argument, NULL, 's'}, | |
32 | {0} | |
33 | }; | |
34 | ||
35 | static void mgmt_exit(int); | |
36 | static void mgmt_vrf_terminate(void); | |
37 | ||
38 | /* privileges */ | |
39 | static zebra_capabilities_t _caps_p[] = {ZCAP_BIND, ZCAP_NET_RAW, | |
40 | ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN}; | |
41 | ||
42 | struct zebra_privs_t mgmt_privs = { | |
43 | #if defined(FRR_USER) && defined(FRR_GROUP) | |
44 | .user = FRR_USER, | |
45 | .group = FRR_GROUP, | |
46 | #endif | |
47 | #ifdef VTY_GROUP | |
48 | .vty_group = VTY_GROUP, | |
49 | #endif | |
50 | .caps_p = _caps_p, | |
51 | .cap_num_p = array_size(_caps_p), | |
52 | .cap_num_i = 0, | |
53 | }; | |
54 | ||
55 | static struct frr_daemon_info mgmtd_di; | |
56 | char backup_config_file[256]; | |
57 | ||
58 | /* SIGHUP handler. */ | |
59 | static void sighup(void) | |
60 | { | |
61 | zlog_info("SIGHUP received, ignoring"); | |
62 | ||
63 | return; | |
64 | ||
65 | /* | |
66 | * This is turned off for the moment. There is all | |
67 | * sorts of config turned off by mgmt_terminate | |
68 | * that is not setup properly again in mgmt_reset. | |
69 | * I see no easy way to do this nor do I see that | |
70 | * this is a desirable way to reload config | |
71 | * given the yang work. | |
72 | */ | |
73 | /* Terminate all thread. */ | |
74 | mgmt_terminate(); | |
75 | ||
76 | /* | |
77 | * mgmt_reset(); | |
78 | */ | |
79 | zlog_info("MGMTD restarting!"); | |
80 | ||
81 | /* | |
82 | * Reload config file. | |
83 | * vty_read_config(NULL, mgmtd_di.config_file, config_default); | |
84 | */ | |
85 | /* Try to return to normal operation. */ | |
86 | } | |
87 | ||
88 | /* SIGINT handler. */ | |
89 | static __attribute__((__noreturn__)) void sigint(void) | |
90 | { | |
91 | zlog_notice("Terminating on signal"); | |
92 | assert(mm->terminating == false); | |
93 | mm->terminating = true; /* global flag that shutting down */ | |
94 | ||
95 | mgmt_terminate(); | |
96 | ||
97 | mgmt_exit(0); | |
98 | ||
99 | exit(0); | |
100 | } | |
101 | ||
102 | /* SIGUSR1 handler. */ | |
103 | static void sigusr1(void) | |
104 | { | |
105 | zlog_rotate(); | |
106 | } | |
107 | ||
1c84efe4 CH |
108 | /* |
109 | * Try to free up allocations we know about so that diagnostic tools such as | |
110 | * valgrind are able to better illuminate leaks. | |
111 | * | |
112 | * Zebra route removal and protocol teardown are not meant to be done here. | |
113 | * For example, "retain_mode" may be set. | |
114 | */ | |
115 | static __attribute__((__noreturn__)) void mgmt_exit(int status) | |
116 | { | |
117 | /* it only makes sense for this to be called on a clean exit */ | |
118 | assert(status == 0); | |
119 | ||
120 | frr_early_fini(); | |
121 | ||
122 | /* stop pthreads (if any) */ | |
123 | frr_pthread_stop_all(); | |
124 | ||
125 | mgmt_vrf_terminate(); | |
126 | ||
127 | frr_fini(); | |
128 | exit(status); | |
129 | } | |
130 | ||
ef43a632 CH |
131 | static struct frr_signal_t mgmt_signals[] = { |
132 | { | |
133 | .signal = SIGHUP, | |
134 | .handler = &sighup, | |
135 | }, | |
136 | { | |
137 | .signal = SIGUSR1, | |
138 | .handler = &sigusr1, | |
139 | }, | |
140 | { | |
141 | .signal = SIGINT, | |
142 | .handler = &sigint, | |
143 | }, | |
144 | { | |
145 | .signal = SIGTERM, | |
146 | .handler = &sigint, | |
147 | }, | |
148 | }; | |
149 | ||
1c84efe4 CH |
150 | static int mgmt_vrf_new(struct vrf *vrf) |
151 | { | |
152 | zlog_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id); | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
157 | static int mgmt_vrf_delete(struct vrf *vrf) | |
158 | { | |
159 | zlog_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id); | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | static int mgmt_vrf_enable(struct vrf *vrf) | |
165 | { | |
166 | zlog_debug("VRF Enable: %s(%u)", vrf->name, vrf->vrf_id); | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | static int mgmt_vrf_disable(struct vrf *vrf) | |
172 | { | |
173 | zlog_debug("VRF Disable: %s(%u)", vrf->name, vrf->vrf_id); | |
174 | ||
175 | /* Note: This is a callback, the VRF will be deleted by the caller. */ | |
176 | return 0; | |
177 | } | |
178 | ||
179 | static int mgmt_vrf_config_write(struct vty *vty) | |
180 | { | |
181 | return 0; | |
182 | } | |
183 | ||
184 | static void mgmt_vrf_init(void) | |
185 | { | |
186 | vrf_init(mgmt_vrf_new, mgmt_vrf_enable, mgmt_vrf_disable, | |
187 | mgmt_vrf_delete); | |
188 | vrf_cmd_init(mgmt_vrf_config_write); | |
189 | } | |
190 | ||
191 | static void mgmt_vrf_terminate(void) | |
192 | { | |
193 | vrf_terminate(); | |
194 | } | |
195 | ||
196 | /* | |
197 | * List of YANG modules to be loaded in the process context of | |
198 | * MGMTd. | |
199 | * | |
200 | * NOTE: In future this will also include the YANG modules of | |
201 | * all individual Backend clients. | |
202 | */ | |
203 | static const struct frr_yang_module_info *const mgmt_yang_modules[] = { | |
204 | &frr_filter_info, &frr_interface_info, &frr_route_map_info, | |
205 | &frr_routing_info, &frr_vrf_info, | |
7d65b7b7 CH |
206 | /* |
207 | * YANG module info supported by backend clients get added here. | |
208 | * NOTE: Always set .ignore_cbs true for to avoid validating | |
209 | * backend northbound callbacks during loading. | |
210 | */ | |
7d65b7b7 CH |
211 | #ifdef HAVE_STATICD |
212 | &(struct frr_yang_module_info){.name = "frr-staticd", | |
213 | .ignore_cbs = true}, | |
214 | #endif | |
1c84efe4 CH |
215 | }; |
216 | ||
217 | FRR_DAEMON_INFO(mgmtd, MGMTD, .vty_port = MGMTD_VTY_PORT, | |
218 | ||
219 | .proghelp = "FRR Management Daemon.", | |
220 | ||
221 | .signals = mgmt_signals, .n_signals = array_size(mgmt_signals), | |
222 | ||
223 | .privs = &mgmt_privs, .yang_modules = mgmt_yang_modules, | |
224 | .n_yang_modules = array_size(mgmt_yang_modules), | |
225 | ); | |
226 | ||
227 | #define DEPRECATED_OPTIONS "" | |
228 | ||
229 | /* Main routine of mgmt. Treatment of argument and start mgmt finite | |
230 | * state machine is handled at here. | |
231 | */ | |
232 | int main(int argc, char **argv) | |
233 | { | |
234 | int opt; | |
235 | int buffer_size = MGMTD_SOCKET_BUF_SIZE; | |
236 | ||
237 | frr_preinit(&mgmtd_di, argc, argv); | |
238 | frr_opt_add( | |
239 | "s:" DEPRECATED_OPTIONS, longopts, | |
240 | " -s, --socket_size Set MGMTD peer socket send buffer size\n"); | |
241 | ||
242 | /* Command line argument treatment. */ | |
243 | while (1) { | |
244 | opt = frr_getopt(argc, argv, 0); | |
245 | ||
246 | if (opt && opt < 128 && strchr(DEPRECATED_OPTIONS, opt)) { | |
247 | fprintf(stderr, | |
248 | "The -%c option no longer exists.\nPlease refer to the manual.\n", | |
249 | opt); | |
250 | continue; | |
251 | } | |
252 | ||
253 | if (opt == EOF) | |
254 | break; | |
255 | ||
256 | switch (opt) { | |
257 | case 0: | |
258 | break; | |
259 | case 's': | |
260 | buffer_size = atoi(optarg); | |
261 | break; | |
262 | default: | |
263 | frr_help_exit(1); | |
264 | break; | |
265 | } | |
266 | } | |
267 | ||
268 | /* MGMTD master init. */ | |
269 | mgmt_master_init(frr_init(), buffer_size); | |
270 | ||
271 | /* VRF Initializations. */ | |
272 | mgmt_vrf_init(); | |
273 | ||
274 | /* MGMTD related initialization. */ | |
275 | mgmt_init(); | |
276 | ||
277 | snprintf(backup_config_file, sizeof(backup_config_file), | |
278 | "%s/zebra.conf", frr_sysconfdir); | |
279 | mgmtd_di.backup_config_file = backup_config_file; | |
280 | ||
281 | frr_config_fork(); | |
282 | ||
283 | frr_run(mm->master); | |
284 | ||
285 | /* Not reached. */ | |
286 | return 0; | |
287 | } |