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