]> git.proxmox.com Git - mirror_frr.git/blob - zebra/test_main.c
Merge branch 'cmaster' of ssh://stash.cumulusnetworks.com:7999/quag/quagga into cmaster
[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 "prefix.h"
29 #include "log.h"
30 #include "privs.h"
31 #include "sigevent.h"
32
33 #include "zebra/rib.h"
34 #include "zebra/zserv.h"
35 #include "zebra/debug.h"
36 #include "zebra/router-id.h"
37 #include "zebra/interface.h"
38
39 /* Zebra instance */
40 struct zebra_t zebrad =
41 {
42 .rtm_table_default = 0,
43 };
44
45 /* process id. */
46 pid_t pid;
47
48 /* Allow non-quagga entities to delete quagga routes */
49 int allow_delete = 0;
50
51 /* zebra_rib's workqueue hold time. Private export for use by test code only */
52 extern int rib_process_hold_time;
53
54 /* Pacify zclient.o in libzebra, which expects this variable. */
55 struct thread_master *master;
56
57 /* Command line options. */
58 struct option longopts[] =
59 {
60 { "batch", no_argument, NULL, 'b'},
61 { "daemon", no_argument, NULL, 'd'},
62 { "allow_delete", no_argument, NULL, 'a'},
63 { "config_file", required_argument, NULL, 'f'},
64 { "help", no_argument, NULL, 'h'},
65 { "vty_addr", required_argument, NULL, 'A'},
66 { "vty_port", required_argument, NULL, 'P'},
67 { "version", no_argument, NULL, 'v'},
68 { "rib_hold", required_argument, NULL, 'r'},
69 { 0 }
70 };
71
72 zebra_capabilities_t _caps_p [] =
73 {
74 ZCAP_NET_ADMIN,
75 ZCAP_SYS_ADMIN,
76 ZCAP_NET_RAW,
77 };
78
79 /* Default configuration file path. */
80 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
81
82 /* Process ID saved for use by init system */
83 const char *pid_file = PATH_ZEBRA_PID;
84
85 /* Help information display. */
86 static void
87 usage (char *progname, int status)
88 {
89 if (status != 0)
90 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
91 else
92 {
93 printf ("Usage : %s [OPTION...]\n\n"\
94 "Daemon which manages kernel routing table management and "\
95 "redistribution between different routing protocols.\n\n"\
96 "-b, --batch Runs in batch mode\n"\
97 "-d, --daemon Runs in daemon mode\n"\
98 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
99 "-f, --config_file Set configuration file name\n"\
100 "-A, --vty_addr Set vty's bind address\n"\
101 "-P, --vty_port Set vty's port number\n"\
102 "-r, --rib_hold Set rib-queue hold time\n"\
103 "-v, --version Print program version\n"\
104 "-h, --help Display this help and exit\n"\
105 "\n"\
106 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
107 }
108
109 exit (status);
110 }
111
112 static unsigned int test_ifindex = 0;
113
114 /* testrib commands */
115 DEFUN (test_interface_state,
116 test_interface_state_cmd,
117 "state (up|down)",
118 "configure interface\n"
119 "up\n"
120 "down\n")
121 {
122 struct interface *ifp;
123 if (argc < 1)
124 return CMD_WARNING;
125
126 ifp = vty->index;
127 if (ifp->ifindex == IFINDEX_INTERNAL)
128 {
129 ifp->ifindex = ++test_ifindex;
130 ifp->mtu = 1500;
131 ifp->flags = IFF_BROADCAST|IFF_MULTICAST;
132 }
133
134 switch (argv[0][0])
135 {
136 case 'u':
137 SET_FLAG (ifp->flags, IFF_UP);
138 if_add_update (ifp);
139 printf ("up\n");
140 break;
141 case 'd':
142 UNSET_FLAG (ifp->flags, IFF_UP);
143 if_delete_update (ifp);
144 printf ("down\n");
145 break;
146 default:
147 return CMD_WARNING;
148 }
149 return CMD_SUCCESS;
150 }
151
152 static void
153 test_cmd_init (void)
154 {
155 install_element (INTERFACE_NODE, &test_interface_state_cmd);
156 }
157
158 /* SIGHUP handler. */
159 static void
160 sighup (void)
161 {
162 zlog_info ("SIGHUP received");
163
164 /* Reload of config file. */
165 ;
166 }
167
168 /* SIGINT handler. */
169 static void
170 sigint (void)
171 {
172 zlog_notice ("Terminating on signal");
173
174 exit (0);
175 }
176
177 /* SIGUSR1 handler. */
178 static void
179 sigusr1 (void)
180 {
181 zlog_rotate (NULL);
182 }
183
184 struct quagga_signal_t zebra_signals[] =
185 {
186 {
187 .signal = SIGHUP,
188 .handler = &sighup,
189 },
190 {
191 .signal = SIGUSR1,
192 .handler = &sigusr1,
193 },
194 {
195 .signal = SIGINT,
196 .handler = &sigint,
197 },
198 {
199 .signal = SIGTERM,
200 .handler = &sigint,
201 },
202 };
203
204 /* Main startup routine. */
205 int
206 main (int argc, char **argv)
207 {
208 char *p;
209 char *vty_addr = NULL;
210 int vty_port = 0;
211 int batch_mode = 0;
212 int daemon_mode = 0;
213 char *config_file = NULL;
214 char *progname;
215 struct thread thread;
216
217 /* Set umask before anything for security */
218 umask (0027);
219
220 /* preserve my name */
221 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
222
223 zlog_default = openzlog (progname, ZLOG_ZEBRA, 0,
224 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
225
226 while (1)
227 {
228 int opt;
229
230 opt = getopt_long (argc, argv, "bdaf:hA:P:r:v", longopts, 0);
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;
244 case 'a':
245 allow_delete =1;
246 break;
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)
281 {
282 fprintf (stderr, "Error: --vty_port and --config_file arguments"
283 " are both required\n");
284 usage (progname, 1);
285 }
286
287 /* Make master thread emulator. */
288 zebrad.master = thread_master_create ();
289
290 /* Vty related initialize. */
291 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
292 cmd_init (1);
293 vty_init (zebrad.master);
294 memory_init ();
295 if_init();
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. */
305 kernel_init ();
306 route_read ();
307 zebra_vty_init();
308
309 /* Configuration file read*/
310 vty_read_config (config_file, config_default);
311
312 /* Clean up rib. */
313 rib_weed_tables ();
314
315 /* Exit when zebra is working in batch mode. */
316 if (batch_mode)
317 exit (0);
318
319 /* Daemonize. */
320 if (daemon_mode && daemon (0, 0) < 0)
321 {
322 perror("daemon start failed");
323 exit (1);
324 }
325
326 /* Needed for BSD routing socket. */
327 pid = getpid ();
328
329 /* Make vty server socket. */
330 vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
331
332 /* Print banner. */
333 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
334
335 while (thread_fetch (zebrad.master, &thread))
336 thread_call (&thread);
337
338 /* Not reached... */
339 return 0;
340 }