]> git.proxmox.com Git - mirror_frr.git/blob - zebra/test_main.c
*: split & distribute memtypes and stop (re|ab)using lib/ MTYPEs
[mirror_frr.git] / zebra / test_main.c
1 /* main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
3 *
4 * GNU Zebra is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2, or (at your option) any
7 * later version.
8 *
9 * GNU Zebra is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with GNU Zebra; see the file COPYING. If not, write to the Free
16 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
18 */
19
20 #include <zebra.h>
21
22 #include <lib/version.h>
23 #include "getopt.h"
24 #include "command.h"
25 #include "thread.h"
26 #include "filter.h"
27 #include "memory.h"
28 #include "zebra_memory.h"
29 #include "memory_vty.h"
30 #include "prefix.h"
31 #include "log.h"
32 #include "privs.h"
33 #include "sigevent.h"
34 #include "vrf.h"
35
36 #include "zebra/rib.h"
37 #include "zebra/zebra_ns.h"
38 #include "zebra/zserv.h"
39 #include "zebra/zebra_vrf.h"
40 #include "zebra/debug.h"
41 #include "zebra/router-id.h"
42 #include "zebra/interface.h"
43
44 /* Zebra instance */
45 struct zebra_t zebrad =
46 {
47 .rtm_table_default = 0,
48 };
49
50 /* process id. */
51 pid_t pid;
52
53 /* Allow non-quagga entities to delete quagga routes */
54 int allow_delete = 0;
55
56 /* zebra_rib's workqueue hold time. Private export for use by test code only */
57 extern int rib_process_hold_time;
58
59 /* Pacify zclient.o in libzebra, which expects this variable. */
60 struct thread_master *master;
61
62 /* Command line options. */
63 struct option longopts[] =
64 {
65 { "batch", no_argument, NULL, 'b'},
66 { "daemon", no_argument, NULL, 'd'},
67 { "allow_delete", no_argument, NULL, 'a'},
68 { "config_file", required_argument, NULL, 'f'},
69 { "help", no_argument, NULL, 'h'},
70 { "vty_addr", required_argument, NULL, 'A'},
71 { "vty_port", required_argument, NULL, 'P'},
72 { "version", no_argument, NULL, 'v'},
73 { "rib_hold", required_argument, NULL, 'r'},
74 { 0 }
75 };
76
77 zebra_capabilities_t _caps_p [] =
78 {
79 ZCAP_NET_ADMIN,
80 ZCAP_SYS_ADMIN,
81 ZCAP_NET_RAW,
82 };
83
84 /* Default configuration file path. */
85 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
86
87 /* Process ID saved for use by init system */
88 const char *pid_file = PATH_ZEBRA_PID;
89
90 /* Help information display. */
91 static void
92 usage (char *progname, int status)
93 {
94 if (status != 0)
95 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
96 else
97 {
98 printf ("Usage : %s [OPTION...]\n\n"\
99 "Daemon which manages kernel routing table management and "\
100 "redistribution between different routing protocols.\n\n"\
101 "-b, --batch Runs in batch mode\n"\
102 "-d, --daemon Runs in daemon mode\n"\
103 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
104 "-f, --config_file Set configuration file name\n"\
105 "-A, --vty_addr Set vty's bind address\n"\
106 "-P, --vty_port Set vty's port number\n"\
107 "-r, --rib_hold Set rib-queue hold time\n"\
108 "-v, --version Print program version\n"\
109 "-h, --help Display this help and exit\n"\
110 "\n"\
111 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
112 }
113
114 exit (status);
115 }
116
117 static ifindex_t test_ifindex = 0;
118
119 /* testrib commands */
120 DEFUN (test_interface_state,
121 test_interface_state_cmd,
122 "state (up|down)",
123 "configure interface\n"
124 "up\n"
125 "down\n")
126 {
127 struct interface *ifp;
128 if (argc < 1)
129 return CMD_WARNING;
130
131 ifp = vty->index;
132 if (ifp->ifindex == IFINDEX_INTERNAL)
133 {
134 ifp->ifindex = ++test_ifindex;
135 ifp->mtu = 1500;
136 ifp->flags = IFF_BROADCAST|IFF_MULTICAST;
137 }
138
139 switch (argv[0][0])
140 {
141 case 'u':
142 SET_FLAG (ifp->flags, IFF_UP);
143 if_add_update (ifp);
144 printf ("up\n");
145 break;
146 case 'd':
147 UNSET_FLAG (ifp->flags, IFF_UP);
148 if_delete_update (ifp);
149 printf ("down\n");
150 break;
151 default:
152 return CMD_WARNING;
153 }
154 return CMD_SUCCESS;
155 }
156
157 static void
158 test_cmd_init (void)
159 {
160 install_element (INTERFACE_NODE, &test_interface_state_cmd);
161 }
162
163 /* SIGHUP handler. */
164 static void
165 sighup (void)
166 {
167 zlog_info ("SIGHUP received");
168
169 /* Reload of config file. */
170 ;
171 }
172
173 /* SIGINT handler. */
174 static void
175 sigint (void)
176 {
177 zlog_notice ("Terminating on signal");
178
179 exit (0);
180 }
181
182 /* SIGUSR1 handler. */
183 static void
184 sigusr1 (void)
185 {
186 zlog_rotate (NULL);
187 }
188
189 struct quagga_signal_t zebra_signals[] =
190 {
191 {
192 .signal = SIGHUP,
193 .handler = &sighup,
194 },
195 {
196 .signal = SIGUSR1,
197 .handler = &sigusr1,
198 },
199 {
200 .signal = SIGINT,
201 .handler = &sigint,
202 },
203 {
204 .signal = SIGTERM,
205 .handler = &sigint,
206 },
207 };
208 /* Main startup routine. */
209 int
210 main (int argc, char **argv)
211 {
212 char *p;
213 char *vty_addr = NULL;
214 int vty_port = 0;
215 int batch_mode = 0;
216 int daemon_mode = 0;
217 char *config_file = NULL;
218 char *progname;
219 struct thread thread;
220
221 /* Set umask before anything for security */
222 umask (0027);
223
224 /* preserve my name */
225 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
226
227 zlog_default = openzlog (progname, ZLOG_ZEBRA, 0,
228 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
229
230 while (1)
231 {
232 int opt;
233
234 opt = getopt_long (argc, argv, "bdaf:hA:P:r:v", longopts, 0);
235
236 if (opt == EOF)
237 break;
238
239 switch (opt)
240 {
241 case 0:
242 break;
243 case 'b':
244 batch_mode = 1;
245 case 'd':
246 daemon_mode = 1;
247 break;
248 case 'a':
249 allow_delete =1;
250 break;
251 case 'f':
252 config_file = optarg;
253 break;
254 case 'A':
255 vty_addr = optarg;
256 break;
257 case 'P':
258 /* Deal with atoi() returning 0 on failure, and zebra not
259 listening on zebra port... */
260 if (strcmp(optarg, "0") == 0)
261 {
262 vty_port = 0;
263 break;
264 }
265 vty_port = atoi (optarg);
266 break;
267 case 'r':
268 rib_process_hold_time = atoi(optarg);
269 break;
270 case 'v':
271 print_version (progname);
272 exit (0);
273 break;
274 case 'h':
275 usage (progname, 0);
276 break;
277 default:
278 usage (progname, 1);
279 break;
280 }
281 }
282
283 /* port and conf file mandatory */
284 if (!vty_port || !config_file)
285 {
286 fprintf (stderr, "Error: --vty_port and --config_file arguments"
287 " are both required\n");
288 usage (progname, 1);
289 }
290
291 /* Make master thread emulator. */
292 zebrad.master = thread_master_create ();
293
294 /* Vty related initialize. */
295 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
296 cmd_init (1);
297 vty_init (zebrad.master);
298 memory_init ();
299 zebra_debug_init ();
300 zebra_if_init ();
301 test_cmd_init ();
302
303 /* Zebra related initialize. */
304 rib_init ();
305 access_list_init ();
306
307 /* Make kernel routing socket. */
308 zebra_vrf_init ();
309 zebra_vty_init();
310
311 /* Configuration file read*/
312 vty_read_config (config_file, config_default);
313
314 /* Clean up rib. */
315 rib_weed_tables ();
316
317 /* Exit when zebra is working in batch mode. */
318 if (batch_mode)
319 exit (0);
320
321 /* Daemonize. */
322 if (daemon_mode && daemon (0, 0) < 0)
323 {
324 perror("daemon start failed");
325 exit (1);
326 }
327
328 /* Needed for BSD routing socket. */
329 pid = getpid ();
330
331 /* Make vty server socket. */
332 vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
333
334 /* Print banner. */
335 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
336
337 while (thread_fetch (zebrad.master, &thread))
338 thread_call (&thread);
339
340 /* Not reached... */
341 return 0;
342 }