test_main.c 8.3 KB

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