zebra_fpm.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  1. /*
  2. * Main implementation file for interface to Forwarding Plane Manager.
  3. *
  4. * Copyright (C) 2012 by Open Source Routing.
  5. * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
  6. *
  7. * This file is part of GNU Zebra.
  8. *
  9. * GNU Zebra is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2, or (at your option) any
  12. * later version.
  13. *
  14. * GNU Zebra is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  21. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  22. * 02111-1307, USA.
  23. */
  24. #include <zebra.h>
  25. #include "log.h"
  26. #include "stream.h"
  27. #include "thread.h"
  28. #include "network.h"
  29. #include "command.h"
  30. #include "zebra/rib.h"
  31. #include "fpm/fpm.h"
  32. #include "zebra_fpm.h"
  33. #include "zebra_fpm_private.h"
  34. /*
  35. * Interval at which we attempt to connect to the FPM.
  36. */
  37. #define ZFPM_CONNECT_RETRY_IVL 5
  38. /*
  39. * Sizes of outgoing and incoming stream buffers for writing/reading
  40. * FPM messages.
  41. */
  42. #define ZFPM_OBUF_SIZE (2 * FPM_MAX_MSG_LEN)
  43. #define ZFPM_IBUF_SIZE (FPM_MAX_MSG_LEN)
  44. /*
  45. * The maximum number of times the FPM socket write callback can call
  46. * 'write' before it yields.
  47. */
  48. #define ZFPM_MAX_WRITES_PER_RUN 10
  49. /*
  50. * Interval over which we collect statistics.
  51. */
  52. #define ZFPM_STATS_IVL_SECS 10
  53. /*
  54. * Structure that holds state for iterating over all route_node
  55. * structures that are candidates for being communicated to the FPM.
  56. */
  57. typedef struct zfpm_rnodes_iter_t_
  58. {
  59. rib_tables_iter_t tables_iter;
  60. route_table_iter_t iter;
  61. } zfpm_rnodes_iter_t;
  62. /*
  63. * Statistics.
  64. */
  65. typedef struct zfpm_stats_t_ {
  66. unsigned long connect_calls;
  67. unsigned long connect_no_sock;
  68. unsigned long read_cb_calls;
  69. unsigned long write_cb_calls;
  70. unsigned long write_calls;
  71. unsigned long partial_writes;
  72. unsigned long max_writes_hit;
  73. unsigned long t_write_yields;
  74. unsigned long nop_deletes_skipped;
  75. unsigned long route_adds;
  76. unsigned long route_dels;
  77. unsigned long updates_triggered;
  78. unsigned long redundant_triggers;
  79. unsigned long non_fpm_table_triggers;
  80. unsigned long dests_del_after_update;
  81. unsigned long t_conn_down_starts;
  82. unsigned long t_conn_down_dests_processed;
  83. unsigned long t_conn_down_yields;
  84. unsigned long t_conn_down_finishes;
  85. unsigned long t_conn_up_starts;
  86. unsigned long t_conn_up_dests_processed;
  87. unsigned long t_conn_up_yields;
  88. unsigned long t_conn_up_aborts;
  89. unsigned long t_conn_up_finishes;
  90. } zfpm_stats_t;
  91. /*
  92. * States for the FPM state machine.
  93. */
  94. typedef enum {
  95. /*
  96. * In this state we are not yet ready to connect to the FPM. This
  97. * can happen when this module is disabled, or if we're cleaning up
  98. * after a connection has gone down.
  99. */
  100. ZFPM_STATE_IDLE,
  101. /*
  102. * Ready to talk to the FPM and periodically trying to connect to
  103. * it.
  104. */
  105. ZFPM_STATE_ACTIVE,
  106. /*
  107. * In the middle of bringing up a TCP connection. Specifically,
  108. * waiting for a connect() call to complete asynchronously.
  109. */
  110. ZFPM_STATE_CONNECTING,
  111. /*
  112. * TCP connection to the FPM is up.
  113. */
  114. ZFPM_STATE_ESTABLISHED
  115. } zfpm_state_t;
  116. /*
  117. * Message format to be used to communicate with the FPM.
  118. */
  119. typedef enum
  120. {
  121. ZFPM_MSG_FORMAT_NONE,
  122. ZFPM_MSG_FORMAT_NETLINK,
  123. ZFPM_MSG_FORMAT_PROTOBUF,
  124. } zfpm_msg_format_e;
  125. /*
  126. * Globals.
  127. */
  128. typedef struct zfpm_glob_t_
  129. {
  130. /*
  131. * True if the FPM module has been enabled.
  132. */
  133. int enabled;
  134. /*
  135. * Message format to be used to communicate with the fpm.
  136. */
  137. zfpm_msg_format_e message_format;
  138. struct thread_master *master;
  139. zfpm_state_t state;
  140. in_addr_t fpm_server;
  141. /*
  142. * Port on which the FPM is running.
  143. */
  144. int fpm_port;
  145. /*
  146. * List of rib_dest_t structures to be processed
  147. */
  148. TAILQ_HEAD (zfpm_dest_q, rib_dest_t_) dest_q;
  149. /*
  150. * Stream socket to the FPM.
  151. */
  152. int sock;
  153. /*
  154. * Buffers for messages to/from the FPM.
  155. */
  156. struct stream *obuf;
  157. struct stream *ibuf;
  158. /*
  159. * Threads for I/O.
  160. */
  161. struct thread *t_connect;
  162. struct thread *t_write;
  163. struct thread *t_read;
  164. /*
  165. * Thread to clean up after the TCP connection to the FPM goes down
  166. * and the state that belongs to it.
  167. */
  168. struct thread *t_conn_down;
  169. struct {
  170. zfpm_rnodes_iter_t iter;
  171. } t_conn_down_state;
  172. /*
  173. * Thread to take actions once the TCP conn to the FPM comes up, and
  174. * the state that belongs to it.
  175. */
  176. struct thread *t_conn_up;
  177. struct {
  178. zfpm_rnodes_iter_t iter;
  179. } t_conn_up_state;
  180. unsigned long connect_calls;
  181. time_t last_connect_call_time;
  182. /*
  183. * Stats from the start of the current statistics interval up to
  184. * now. These are the counters we typically update in the code.
  185. */
  186. zfpm_stats_t stats;
  187. /*
  188. * Statistics that were gathered in the last collection interval.
  189. */
  190. zfpm_stats_t last_ivl_stats;
  191. /*
  192. * Cumulative stats from the last clear to the start of the current
  193. * statistics interval.
  194. */
  195. zfpm_stats_t cumulative_stats;
  196. /*
  197. * Stats interval timer.
  198. */
  199. struct thread *t_stats;
  200. /*
  201. * If non-zero, the last time when statistics were cleared.
  202. */
  203. time_t last_stats_clear_time;
  204. } zfpm_glob_t;
  205. static zfpm_glob_t zfpm_glob_space;
  206. static zfpm_glob_t *zfpm_g = &zfpm_glob_space;
  207. static int zfpm_read_cb (struct thread *thread);
  208. static int zfpm_write_cb (struct thread *thread);
  209. static void zfpm_set_state (zfpm_state_t state, const char *reason);
  210. static void zfpm_start_connect_timer (const char *reason);
  211. static void zfpm_start_stats_timer (void);
  212. /*
  213. * zfpm_thread_should_yield
  214. */
  215. static inline int
  216. zfpm_thread_should_yield (struct thread *t)
  217. {
  218. return thread_should_yield (t);
  219. }
  220. /*
  221. * zfpm_state_to_str
  222. */
  223. static const char *
  224. zfpm_state_to_str (zfpm_state_t state)
  225. {
  226. switch (state)
  227. {
  228. case ZFPM_STATE_IDLE:
  229. return "idle";
  230. case ZFPM_STATE_ACTIVE:
  231. return "active";
  232. case ZFPM_STATE_CONNECTING:
  233. return "connecting";
  234. case ZFPM_STATE_ESTABLISHED:
  235. return "established";
  236. default:
  237. return "unknown";
  238. }
  239. }
  240. /*
  241. * zfpm_get_time
  242. */
  243. static time_t
  244. zfpm_get_time (void)
  245. {
  246. struct timeval tv;
  247. if (quagga_gettime (QUAGGA_CLK_MONOTONIC, &tv) < 0)
  248. zlog_warn ("FPM: quagga_gettime failed!!");
  249. return tv.tv_sec;
  250. }
  251. /*
  252. * zfpm_get_elapsed_time
  253. *
  254. * Returns the time elapsed (in seconds) since the given time.
  255. */
  256. static time_t
  257. zfpm_get_elapsed_time (time_t reference)
  258. {
  259. time_t now;
  260. now = zfpm_get_time ();
  261. if (now < reference)
  262. {
  263. assert (0);
  264. return 0;
  265. }
  266. return now - reference;
  267. }
  268. /*
  269. * zfpm_is_table_for_fpm
  270. *
  271. * Returns TRUE if the the given table is to be communicated to the
  272. * FPM.
  273. */
  274. static inline int
  275. zfpm_is_table_for_fpm (struct route_table *table)
  276. {
  277. rib_table_info_t *info;
  278. info = rib_table_info (table);
  279. /*
  280. * We only send the unicast tables in the main instance to the FPM
  281. * at this point.
  282. */
  283. if (info->zvrf->vrf_id != 0)
  284. return 0;
  285. if (info->safi != SAFI_UNICAST)
  286. return 0;
  287. return 1;
  288. }
  289. /*
  290. * zfpm_rnodes_iter_init
  291. */
  292. static inline void
  293. zfpm_rnodes_iter_init (zfpm_rnodes_iter_t *iter)
  294. {
  295. memset (iter, 0, sizeof (*iter));
  296. rib_tables_iter_init (&iter->tables_iter);
  297. /*
  298. * This is a hack, but it makes implementing 'next' easier by
  299. * ensuring that route_table_iter_next() will return NULL the first
  300. * time we call it.
  301. */
  302. route_table_iter_init (&iter->iter, NULL);
  303. route_table_iter_cleanup (&iter->iter);
  304. }
  305. /*
  306. * zfpm_rnodes_iter_next
  307. */
  308. static inline struct route_node *
  309. zfpm_rnodes_iter_next (zfpm_rnodes_iter_t *iter)
  310. {
  311. struct route_node *rn;
  312. struct route_table *table;
  313. while (1)
  314. {
  315. rn = route_table_iter_next (&iter->iter);
  316. if (rn)
  317. return rn;
  318. /*
  319. * We've made our way through this table, go to the next one.
  320. */
  321. route_table_iter_cleanup (&iter->iter);
  322. while ((table = rib_tables_iter_next (&iter->tables_iter)))
  323. {
  324. if (zfpm_is_table_for_fpm (table))
  325. break;
  326. }
  327. if (!table)
  328. return NULL;
  329. route_table_iter_init (&iter->iter, table);
  330. }
  331. return NULL;
  332. }
  333. /*
  334. * zfpm_rnodes_iter_pause
  335. */
  336. static inline void
  337. zfpm_rnodes_iter_pause (zfpm_rnodes_iter_t *iter)
  338. {
  339. route_table_iter_pause (&iter->iter);
  340. }
  341. /*
  342. * zfpm_rnodes_iter_cleanup
  343. */
  344. static inline void
  345. zfpm_rnodes_iter_cleanup (zfpm_rnodes_iter_t *iter)
  346. {
  347. route_table_iter_cleanup (&iter->iter);
  348. rib_tables_iter_cleanup (&iter->tables_iter);
  349. }
  350. /*
  351. * zfpm_stats_init
  352. *
  353. * Initialize a statistics block.
  354. */
  355. static inline void
  356. zfpm_stats_init (zfpm_stats_t *stats)
  357. {
  358. memset (stats, 0, sizeof (*stats));
  359. }
  360. /*
  361. * zfpm_stats_reset
  362. */
  363. static inline void
  364. zfpm_stats_reset (zfpm_stats_t *stats)
  365. {
  366. zfpm_stats_init (stats);
  367. }
  368. /*
  369. * zfpm_stats_copy
  370. */
  371. static inline void
  372. zfpm_stats_copy (const zfpm_stats_t *src, zfpm_stats_t *dest)
  373. {
  374. memcpy (dest, src, sizeof (*dest));
  375. }
  376. /*
  377. * zfpm_stats_compose
  378. *
  379. * Total up the statistics in two stats structures ('s1 and 's2') and
  380. * return the result in the third argument, 'result'. Note that the
  381. * pointer 'result' may be the same as 's1' or 's2'.
  382. *
  383. * For simplicity, the implementation below assumes that the stats
  384. * structure is composed entirely of counters. This can easily be
  385. * changed when necessary.
  386. */
  387. static void
  388. zfpm_stats_compose (const zfpm_stats_t *s1, const zfpm_stats_t *s2,
  389. zfpm_stats_t *result)
  390. {
  391. const unsigned long *p1, *p2;
  392. unsigned long *result_p;
  393. int i, num_counters;
  394. p1 = (const unsigned long *) s1;
  395. p2 = (const unsigned long *) s2;
  396. result_p = (unsigned long *) result;
  397. num_counters = (sizeof (zfpm_stats_t) / sizeof (unsigned long));
  398. for (i = 0; i < num_counters; i++)
  399. {
  400. result_p[i] = p1[i] + p2[i];
  401. }
  402. }
  403. /*
  404. * zfpm_read_on
  405. */
  406. static inline void
  407. zfpm_read_on (void)
  408. {
  409. assert (!zfpm_g->t_read);
  410. assert (zfpm_g->sock >= 0);
  411. THREAD_READ_ON (zfpm_g->master, zfpm_g->t_read, zfpm_read_cb, 0,
  412. zfpm_g->sock);
  413. }
  414. /*
  415. * zfpm_write_on
  416. */
  417. static inline void
  418. zfpm_write_on (void)
  419. {
  420. assert (!zfpm_g->t_write);
  421. assert (zfpm_g->sock >= 0);
  422. THREAD_WRITE_ON (zfpm_g->master, zfpm_g->t_write, zfpm_write_cb, 0,
  423. zfpm_g->sock);
  424. }
  425. /*
  426. * zfpm_read_off
  427. */
  428. static inline void
  429. zfpm_read_off (void)
  430. {
  431. THREAD_READ_OFF (zfpm_g->t_read);
  432. }
  433. /*
  434. * zfpm_write_off
  435. */
  436. static inline void
  437. zfpm_write_off (void)
  438. {
  439. THREAD_WRITE_OFF (zfpm_g->t_write);
  440. }
  441. /*
  442. * zfpm_conn_up_thread_cb
  443. *
  444. * Callback for actions to be taken when the connection to the FPM
  445. * comes up.
  446. */
  447. static int
  448. zfpm_conn_up_thread_cb (struct thread *thread)
  449. {
  450. struct route_node *rnode;
  451. zfpm_rnodes_iter_t *iter;
  452. rib_dest_t *dest;
  453. assert (zfpm_g->t_conn_up);
  454. zfpm_g->t_conn_up = NULL;
  455. iter = &zfpm_g->t_conn_up_state.iter;
  456. if (zfpm_g->state != ZFPM_STATE_ESTABLISHED)
  457. {
  458. zfpm_debug ("Connection not up anymore, conn_up thread aborting");
  459. zfpm_g->stats.t_conn_up_aborts++;
  460. goto done;
  461. }
  462. while ((rnode = zfpm_rnodes_iter_next (iter)))
  463. {
  464. dest = rib_dest_from_rnode (rnode);
  465. if (dest)
  466. {
  467. zfpm_g->stats.t_conn_up_dests_processed++;
  468. zfpm_trigger_update (rnode, NULL);
  469. }
  470. /*
  471. * Yield if need be.
  472. */
  473. if (!zfpm_thread_should_yield (thread))
  474. continue;
  475. zfpm_g->stats.t_conn_up_yields++;
  476. zfpm_rnodes_iter_pause (iter);
  477. zfpm_g->t_conn_up = thread_add_background (zfpm_g->master,
  478. zfpm_conn_up_thread_cb,
  479. 0, 0);
  480. return 0;
  481. }
  482. zfpm_g->stats.t_conn_up_finishes++;
  483. done:
  484. zfpm_rnodes_iter_cleanup (iter);
  485. return 0;
  486. }
  487. /*
  488. * zfpm_connection_up
  489. *
  490. * Called when the connection to the FPM comes up.
  491. */
  492. static void
  493. zfpm_connection_up (const char *detail)
  494. {
  495. assert (zfpm_g->sock >= 0);
  496. zfpm_read_on ();
  497. zfpm_write_on ();
  498. zfpm_set_state (ZFPM_STATE_ESTABLISHED, detail);
  499. /*
  500. * Start thread to push existing routes to the FPM.
  501. */
  502. assert (!zfpm_g->t_conn_up);
  503. zfpm_rnodes_iter_init (&zfpm_g->t_conn_up_state.iter);
  504. zfpm_debug ("Starting conn_up thread");
  505. zfpm_g->t_conn_up = thread_add_background (zfpm_g->master,
  506. zfpm_conn_up_thread_cb, 0, 0);
  507. zfpm_g->stats.t_conn_up_starts++;
  508. }
  509. /*
  510. * zfpm_connect_check
  511. *
  512. * Check if an asynchronous connect() to the FPM is complete.
  513. */
  514. static void
  515. zfpm_connect_check ()
  516. {
  517. int status;
  518. socklen_t slen;
  519. int ret;
  520. zfpm_read_off ();
  521. zfpm_write_off ();
  522. slen = sizeof (status);
  523. ret = getsockopt (zfpm_g->sock, SOL_SOCKET, SO_ERROR, (void *) &status,
  524. &slen);
  525. if (ret >= 0 && status == 0)
  526. {
  527. zfpm_connection_up ("async connect complete");
  528. return;
  529. }
  530. /*
  531. * getsockopt() failed or indicated an error on the socket.
  532. */
  533. close (zfpm_g->sock);
  534. zfpm_g->sock = -1;
  535. zfpm_start_connect_timer ("getsockopt() after async connect failed");
  536. return;
  537. }
  538. /*
  539. * zfpm_conn_down_thread_cb
  540. *
  541. * Callback that is invoked to clean up state after the TCP connection
  542. * to the FPM goes down.
  543. */
  544. static int
  545. zfpm_conn_down_thread_cb (struct thread *thread)
  546. {
  547. struct route_node *rnode;
  548. zfpm_rnodes_iter_t *iter;
  549. rib_dest_t *dest;
  550. assert (zfpm_g->state == ZFPM_STATE_IDLE);
  551. assert (zfpm_g->t_conn_down);
  552. zfpm_g->t_conn_down = NULL;
  553. iter = &zfpm_g->t_conn_down_state.iter;
  554. while ((rnode = zfpm_rnodes_iter_next (iter)))
  555. {
  556. dest = rib_dest_from_rnode (rnode);
  557. if (dest)
  558. {
  559. if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM))
  560. {
  561. TAILQ_REMOVE (&zfpm_g->dest_q, dest, fpm_q_entries);
  562. }
  563. UNSET_FLAG (dest->flags, RIB_DEST_UPDATE_FPM);
  564. UNSET_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM);
  565. zfpm_g->stats.t_conn_down_dests_processed++;
  566. /*
  567. * Check if the dest should be deleted.
  568. */
  569. rib_gc_dest(rnode);
  570. }
  571. /*
  572. * Yield if need be.
  573. */
  574. if (!zfpm_thread_should_yield (thread))
  575. continue;
  576. zfpm_g->stats.t_conn_down_yields++;
  577. zfpm_rnodes_iter_pause (iter);
  578. zfpm_g->t_conn_down = thread_add_background (zfpm_g->master,
  579. zfpm_conn_down_thread_cb,
  580. 0, 0);
  581. return 0;
  582. }
  583. zfpm_g->stats.t_conn_down_finishes++;
  584. zfpm_rnodes_iter_cleanup (iter);
  585. /*
  586. * Start the process of connecting to the FPM again.
  587. */
  588. zfpm_start_connect_timer ("cleanup complete");
  589. return 0;
  590. }
  591. /*
  592. * zfpm_connection_down
  593. *
  594. * Called when the connection to the FPM has gone down.
  595. */
  596. static void
  597. zfpm_connection_down (const char *detail)
  598. {
  599. if (!detail)
  600. detail = "unknown";
  601. assert (zfpm_g->state == ZFPM_STATE_ESTABLISHED);
  602. zlog_info ("connection to the FPM has gone down: %s", detail);
  603. zfpm_read_off ();
  604. zfpm_write_off ();
  605. stream_reset (zfpm_g->ibuf);
  606. stream_reset (zfpm_g->obuf);
  607. if (zfpm_g->sock >= 0) {
  608. close (zfpm_g->sock);
  609. zfpm_g->sock = -1;
  610. }
  611. /*
  612. * Start thread to clean up state after the connection goes down.
  613. */
  614. assert (!zfpm_g->t_conn_down);
  615. zfpm_debug ("Starting conn_down thread");
  616. zfpm_rnodes_iter_init (&zfpm_g->t_conn_down_state.iter);
  617. zfpm_g->t_conn_down = thread_add_background (zfpm_g->master,
  618. zfpm_conn_down_thread_cb, 0, 0);
  619. zfpm_g->stats.t_conn_down_starts++;
  620. zfpm_set_state (ZFPM_STATE_IDLE, detail);
  621. }
  622. /*
  623. * zfpm_read_cb
  624. */
  625. static int
  626. zfpm_read_cb (struct thread *thread)
  627. {
  628. size_t already;
  629. struct stream *ibuf;
  630. uint16_t msg_len;
  631. fpm_msg_hdr_t *hdr;
  632. zfpm_g->stats.read_cb_calls++;
  633. assert (zfpm_g->t_read);
  634. zfpm_g->t_read = NULL;
  635. /*
  636. * Check if async connect is now done.
  637. */
  638. if (zfpm_g->state == ZFPM_STATE_CONNECTING)
  639. {
  640. zfpm_connect_check();
  641. return 0;
  642. }
  643. assert (zfpm_g->state == ZFPM_STATE_ESTABLISHED);
  644. assert (zfpm_g->sock >= 0);
  645. ibuf = zfpm_g->ibuf;
  646. already = stream_get_endp (ibuf);
  647. if (already < FPM_MSG_HDR_LEN)
  648. {
  649. ssize_t nbyte;
  650. nbyte = stream_read_try (ibuf, zfpm_g->sock, FPM_MSG_HDR_LEN - already);
  651. if (nbyte == 0 || nbyte == -1)
  652. {
  653. zfpm_connection_down ("closed socket in read");
  654. return 0;
  655. }
  656. if (nbyte != (ssize_t) (FPM_MSG_HDR_LEN - already))
  657. goto done;
  658. already = FPM_MSG_HDR_LEN;
  659. }
  660. stream_set_getp (ibuf, 0);
  661. hdr = (fpm_msg_hdr_t *) stream_pnt (ibuf);
  662. if (!fpm_msg_hdr_ok (hdr))
  663. {
  664. zfpm_connection_down ("invalid message header");
  665. return 0;
  666. }
  667. msg_len = fpm_msg_len (hdr);
  668. /*
  669. * Read out the rest of the packet.
  670. */
  671. if (already < msg_len)
  672. {
  673. ssize_t nbyte;
  674. nbyte = stream_read_try (ibuf, zfpm_g->sock, msg_len - already);
  675. if (nbyte == 0 || nbyte == -1)
  676. {
  677. zfpm_connection_down ("failed to read message");
  678. return 0;
  679. }
  680. if (nbyte != (ssize_t) (msg_len - already))
  681. goto done;
  682. }
  683. zfpm_debug ("Read out a full fpm message");
  684. /*
  685. * Just throw it away for now.
  686. */
  687. stream_reset (ibuf);
  688. done:
  689. zfpm_read_on ();
  690. return 0;
  691. }
  692. /*
  693. * zfpm_writes_pending
  694. *
  695. * Returns TRUE if we may have something to write to the FPM.
  696. */
  697. static int
  698. zfpm_writes_pending (void)
  699. {
  700. /*
  701. * Check if there is any data in the outbound buffer that has not
  702. * been written to the socket yet.
  703. */
  704. if (stream_get_endp (zfpm_g->obuf) - stream_get_getp (zfpm_g->obuf))
  705. return 1;
  706. /*
  707. * Check if there are any prefixes on the outbound queue.
  708. */
  709. if (!TAILQ_EMPTY (&zfpm_g->dest_q))
  710. return 1;
  711. return 0;
  712. }
  713. /*
  714. * zfpm_encode_route
  715. *
  716. * Encode a message to the FPM with information about the given route.
  717. *
  718. * Returns the number of bytes written to the buffer. 0 or a negative
  719. * value indicates an error.
  720. */
  721. static inline int
  722. zfpm_encode_route (rib_dest_t *dest, struct rib *rib, char *in_buf,
  723. size_t in_buf_len, fpm_msg_type_e *msg_type)
  724. {
  725. size_t len;
  726. int cmd;
  727. len = 0;
  728. *msg_type = FPM_MSG_TYPE_NONE;
  729. switch (zfpm_g->message_format) {
  730. case ZFPM_MSG_FORMAT_PROTOBUF:
  731. #ifdef HAVE_PROTOBUF
  732. len = zfpm_protobuf_encode_route (dest, rib, (uint8_t *) in_buf,
  733. in_buf_len);
  734. *msg_type = FPM_MSG_TYPE_PROTOBUF;
  735. #endif
  736. break;
  737. case ZFPM_MSG_FORMAT_NETLINK:
  738. #ifdef HAVE_NETLINK
  739. *msg_type = FPM_MSG_TYPE_NETLINK;
  740. cmd = rib ? RTM_NEWROUTE : RTM_DELROUTE;
  741. len = zfpm_netlink_encode_route (cmd, dest, rib, in_buf, in_buf_len);
  742. assert(fpm_msg_align(len) == len);
  743. *msg_type = FPM_MSG_TYPE_NETLINK;
  744. #endif /* HAVE_NETLINK */
  745. break;
  746. default:
  747. break;
  748. }
  749. return len;
  750. }
  751. /*
  752. * zfpm_route_for_update
  753. *
  754. * Returns the rib that is to be sent to the FPM for a given dest.
  755. */
  756. struct rib *
  757. zfpm_route_for_update (rib_dest_t *dest)
  758. {
  759. struct rib *rib;
  760. RIB_DEST_FOREACH_ROUTE (dest, rib)
  761. {
  762. if (!CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB))
  763. continue;
  764. return rib;
  765. }
  766. /*
  767. * We have no route for this destination.
  768. */
  769. return NULL;
  770. }
  771. /*
  772. * zfpm_build_updates
  773. *
  774. * Process the outgoing queue and write messages to the outbound
  775. * buffer.
  776. */
  777. static void
  778. zfpm_build_updates (void)
  779. {
  780. struct stream *s;
  781. rib_dest_t *dest;
  782. unsigned char *buf, *data, *buf_end;
  783. size_t msg_len;
  784. size_t data_len;
  785. fpm_msg_hdr_t *hdr;
  786. struct rib *rib;
  787. int is_add, write_msg;
  788. fpm_msg_type_e msg_type;
  789. s = zfpm_g->obuf;
  790. assert (stream_empty (s));
  791. do {
  792. /*
  793. * Make sure there is enough space to write another message.
  794. */
  795. if (STREAM_WRITEABLE (s) < FPM_MAX_MSG_LEN)
  796. break;
  797. buf = STREAM_DATA (s) + stream_get_endp (s);
  798. buf_end = buf + STREAM_WRITEABLE (s);
  799. dest = TAILQ_FIRST (&zfpm_g->dest_q);
  800. if (!dest)
  801. break;
  802. assert (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM));
  803. hdr = (fpm_msg_hdr_t *) buf;
  804. hdr->version = FPM_PROTO_VERSION;
  805. data = fpm_msg_data (hdr);
  806. rib = zfpm_route_for_update (dest);
  807. is_add = rib ? 1 : 0;
  808. write_msg = 1;
  809. /*
  810. * If this is a route deletion, and we have not sent the route to
  811. * the FPM previously, skip it.
  812. */
  813. if (!is_add && !CHECK_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM))
  814. {
  815. write_msg = 0;
  816. zfpm_g->stats.nop_deletes_skipped++;
  817. }
  818. if (write_msg) {
  819. data_len = zfpm_encode_route (dest, rib, (char *) data, buf_end - data,
  820. &msg_type);
  821. assert (data_len);
  822. if (data_len)
  823. {
  824. hdr->msg_type = msg_type;
  825. msg_len = fpm_data_len_to_msg_len (data_len);
  826. hdr->msg_len = htons (msg_len);
  827. stream_forward_endp (s, msg_len);
  828. if (is_add)
  829. zfpm_g->stats.route_adds++;
  830. else
  831. zfpm_g->stats.route_dels++;
  832. }
  833. }
  834. /*
  835. * Remove the dest from the queue, and reset the flag.
  836. */
  837. UNSET_FLAG (dest->flags, RIB_DEST_UPDATE_FPM);
  838. TAILQ_REMOVE (&zfpm_g->dest_q, dest, fpm_q_entries);
  839. if (is_add)
  840. {
  841. SET_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM);
  842. }
  843. else
  844. {
  845. UNSET_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM);
  846. }
  847. /*
  848. * Delete the destination if necessary.
  849. */
  850. if (rib_gc_dest (dest->rnode))
  851. zfpm_g->stats.dests_del_after_update++;
  852. } while (1);
  853. }
  854. /*
  855. * zfpm_write_cb
  856. */
  857. static int
  858. zfpm_write_cb (struct thread *thread)
  859. {
  860. struct stream *s;
  861. int num_writes;
  862. zfpm_g->stats.write_cb_calls++;
  863. assert (zfpm_g->t_write);
  864. zfpm_g->t_write = NULL;
  865. /*
  866. * Check if async connect is now done.
  867. */
  868. if (zfpm_g->state == ZFPM_STATE_CONNECTING)
  869. {
  870. zfpm_connect_check ();
  871. return 0;
  872. }
  873. assert (zfpm_g->state == ZFPM_STATE_ESTABLISHED);
  874. assert (zfpm_g->sock >= 0);
  875. num_writes = 0;
  876. do
  877. {
  878. int bytes_to_write, bytes_written;
  879. s = zfpm_g->obuf;
  880. /*
  881. * If the stream is empty, try fill it up with data.
  882. */
  883. if (stream_empty (s))
  884. {
  885. zfpm_build_updates ();
  886. }
  887. bytes_to_write = stream_get_endp (s) - stream_get_getp (s);
  888. if (!bytes_to_write)
  889. break;
  890. bytes_written = write (zfpm_g->sock, STREAM_PNT (s), bytes_to_write);
  891. zfpm_g->stats.write_calls++;
  892. num_writes++;
  893. if (bytes_written < 0)
  894. {
  895. if (ERRNO_IO_RETRY (errno))
  896. break;
  897. zfpm_connection_down ("failed to write to socket");
  898. return 0;
  899. }
  900. if (bytes_written != bytes_to_write)
  901. {
  902. /*
  903. * Partial write.
  904. */
  905. stream_forward_getp (s, bytes_written);
  906. zfpm_g->stats.partial_writes++;
  907. break;
  908. }
  909. /*
  910. * We've written out the entire contents of the stream.
  911. */
  912. stream_reset (s);
  913. if (num_writes >= ZFPM_MAX_WRITES_PER_RUN)
  914. {
  915. zfpm_g->stats.max_writes_hit++;
  916. break;
  917. }
  918. if (zfpm_thread_should_yield (thread))
  919. {
  920. zfpm_g->stats.t_write_yields++;
  921. break;
  922. }
  923. } while (1);
  924. if (zfpm_writes_pending ())
  925. zfpm_write_on ();
  926. return 0;
  927. }
  928. /*
  929. * zfpm_connect_cb
  930. */
  931. static int
  932. zfpm_connect_cb (struct thread *t)
  933. {
  934. int sock, ret;
  935. struct sockaddr_in serv;
  936. assert (zfpm_g->t_connect);
  937. zfpm_g->t_connect = NULL;
  938. assert (zfpm_g->state == ZFPM_STATE_ACTIVE);
  939. sock = socket (AF_INET, SOCK_STREAM, 0);
  940. if (sock < 0)
  941. {
  942. zfpm_debug ("Failed to create socket for connect(): %s", strerror(errno));
  943. zfpm_g->stats.connect_no_sock++;
  944. return 0;
  945. }
  946. set_nonblocking(sock);
  947. /* Make server socket. */
  948. memset (&serv, 0, sizeof (serv));
  949. serv.sin_family = AF_INET;
  950. serv.sin_port = htons (zfpm_g->fpm_port);
  951. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  952. serv.sin_len = sizeof (struct sockaddr_in);
  953. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  954. if (!zfpm_g->fpm_server)
  955. serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  956. else
  957. serv.sin_addr.s_addr = (zfpm_g->fpm_server);
  958. /*
  959. * Connect to the FPM.
  960. */
  961. zfpm_g->connect_calls++;
  962. zfpm_g->stats.connect_calls++;
  963. zfpm_g->last_connect_call_time = zfpm_get_time ();
  964. ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
  965. if (ret >= 0)
  966. {
  967. zfpm_g->sock = sock;
  968. zfpm_connection_up ("connect succeeded");
  969. return 1;
  970. }
  971. if (errno == EINPROGRESS)
  972. {
  973. zfpm_g->sock = sock;
  974. zfpm_read_on ();
  975. zfpm_write_on ();
  976. zfpm_set_state (ZFPM_STATE_CONNECTING, "async connect in progress");
  977. return 0;
  978. }
  979. zlog_info ("can't connect to FPM %d: %s", sock, safe_strerror (errno));
  980. close (sock);
  981. /*
  982. * Restart timer for retrying connection.
  983. */
  984. zfpm_start_connect_timer ("connect() failed");
  985. return 0;
  986. }
  987. /*
  988. * zfpm_set_state
  989. *
  990. * Move state machine into the given state.
  991. */
  992. static void
  993. zfpm_set_state (zfpm_state_t state, const char *reason)
  994. {
  995. zfpm_state_t cur_state = zfpm_g->state;
  996. if (!reason)
  997. reason = "Unknown";
  998. if (state == cur_state)
  999. return;
  1000. zfpm_debug("beginning state transition %s -> %s. Reason: %s",
  1001. zfpm_state_to_str (cur_state), zfpm_state_to_str (state),
  1002. reason);
  1003. switch (state) {
  1004. case ZFPM_STATE_IDLE:
  1005. assert (cur_state == ZFPM_STATE_ESTABLISHED);
  1006. break;
  1007. case ZFPM_STATE_ACTIVE:
  1008. assert (cur_state == ZFPM_STATE_IDLE ||
  1009. cur_state == ZFPM_STATE_CONNECTING);
  1010. assert (zfpm_g->t_connect);
  1011. break;
  1012. case ZFPM_STATE_CONNECTING:
  1013. assert (zfpm_g->sock);
  1014. assert (cur_state == ZFPM_STATE_ACTIVE);
  1015. assert (zfpm_g->t_read);
  1016. assert (zfpm_g->t_write);
  1017. break;
  1018. case ZFPM_STATE_ESTABLISHED:
  1019. assert (cur_state == ZFPM_STATE_ACTIVE ||
  1020. cur_state == ZFPM_STATE_CONNECTING);
  1021. assert (zfpm_g->sock);
  1022. assert (zfpm_g->t_read);
  1023. assert (zfpm_g->t_write);
  1024. break;
  1025. }
  1026. zfpm_g->state = state;
  1027. }
  1028. /*
  1029. * zfpm_calc_connect_delay
  1030. *
  1031. * Returns the number of seconds after which we should attempt to
  1032. * reconnect to the FPM.
  1033. */
  1034. static long
  1035. zfpm_calc_connect_delay (void)
  1036. {
  1037. time_t elapsed;
  1038. /*
  1039. * Return 0 if this is our first attempt to connect.
  1040. */
  1041. if (zfpm_g->connect_calls == 0)
  1042. {
  1043. return 0;
  1044. }
  1045. elapsed = zfpm_get_elapsed_time (zfpm_g->last_connect_call_time);
  1046. if (elapsed > ZFPM_CONNECT_RETRY_IVL) {
  1047. return 0;
  1048. }
  1049. return ZFPM_CONNECT_RETRY_IVL - elapsed;
  1050. }
  1051. /*
  1052. * zfpm_start_connect_timer
  1053. */
  1054. static void
  1055. zfpm_start_connect_timer (const char *reason)
  1056. {
  1057. long delay_secs;
  1058. assert (!zfpm_g->t_connect);
  1059. assert (zfpm_g->sock < 0);
  1060. assert(zfpm_g->state == ZFPM_STATE_IDLE ||
  1061. zfpm_g->state == ZFPM_STATE_ACTIVE ||
  1062. zfpm_g->state == ZFPM_STATE_CONNECTING);
  1063. delay_secs = zfpm_calc_connect_delay();
  1064. zfpm_debug ("scheduling connect in %ld seconds", delay_secs);
  1065. THREAD_TIMER_ON (zfpm_g->master, zfpm_g->t_connect, zfpm_connect_cb, 0,
  1066. delay_secs);
  1067. zfpm_set_state (ZFPM_STATE_ACTIVE, reason);
  1068. }
  1069. /*
  1070. * zfpm_is_enabled
  1071. *
  1072. * Returns TRUE if the zebra FPM module has been enabled.
  1073. */
  1074. static inline int
  1075. zfpm_is_enabled (void)
  1076. {
  1077. return zfpm_g->enabled;
  1078. }
  1079. /*
  1080. * zfpm_conn_is_up
  1081. *
  1082. * Returns TRUE if the connection to the FPM is up.
  1083. */
  1084. static inline int
  1085. zfpm_conn_is_up (void)
  1086. {
  1087. if (zfpm_g->state != ZFPM_STATE_ESTABLISHED)
  1088. return 0;
  1089. assert (zfpm_g->sock >= 0);
  1090. return 1;
  1091. }
  1092. /*
  1093. * zfpm_trigger_update
  1094. *
  1095. * The zebra code invokes this function to indicate that we should
  1096. * send an update to the FPM about the given route_node.
  1097. */
  1098. void
  1099. zfpm_trigger_update (struct route_node *rn, const char *reason)
  1100. {
  1101. rib_dest_t *dest;
  1102. char buf[PREFIX_STRLEN];
  1103. /*
  1104. * Ignore if the connection is down. We will update the FPM about
  1105. * all destinations once the connection comes up.
  1106. */
  1107. if (!zfpm_conn_is_up ())
  1108. return;
  1109. dest = rib_dest_from_rnode (rn);
  1110. /*
  1111. * Ignore the trigger if the dest is not in a table that we would
  1112. * send to the FPM.
  1113. */
  1114. if (!zfpm_is_table_for_fpm (rib_dest_table (dest)))
  1115. {
  1116. zfpm_g->stats.non_fpm_table_triggers++;
  1117. return;
  1118. }
  1119. if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM)) {
  1120. zfpm_g->stats.redundant_triggers++;
  1121. return;
  1122. }
  1123. if (reason)
  1124. {
  1125. zfpm_debug ("%s triggering update to FPM - Reason: %s",
  1126. prefix2str (&rn->p, buf, sizeof(buf)), reason);
  1127. }
  1128. SET_FLAG (dest->flags, RIB_DEST_UPDATE_FPM);
  1129. TAILQ_INSERT_TAIL (&zfpm_g->dest_q, dest, fpm_q_entries);
  1130. zfpm_g->stats.updates_triggered++;
  1131. /*
  1132. * Make sure that writes are enabled.
  1133. */
  1134. if (zfpm_g->t_write)
  1135. return;
  1136. zfpm_write_on ();
  1137. }
  1138. /*
  1139. * zfpm_stats_timer_cb
  1140. */
  1141. static int
  1142. zfpm_stats_timer_cb (struct thread *t)
  1143. {
  1144. assert (zfpm_g->t_stats);
  1145. zfpm_g->t_stats = NULL;
  1146. /*
  1147. * Remember the stats collected in the last interval for display
  1148. * purposes.
  1149. */
  1150. zfpm_stats_copy (&zfpm_g->stats, &zfpm_g->last_ivl_stats);
  1151. /*
  1152. * Add the current set of stats into the cumulative statistics.
  1153. */
  1154. zfpm_stats_compose (&zfpm_g->cumulative_stats, &zfpm_g->stats,
  1155. &zfpm_g->cumulative_stats);
  1156. /*
  1157. * Start collecting stats afresh over the next interval.
  1158. */
  1159. zfpm_stats_reset (&zfpm_g->stats);
  1160. zfpm_start_stats_timer ();
  1161. return 0;
  1162. }
  1163. /*
  1164. * zfpm_stop_stats_timer
  1165. */
  1166. static void
  1167. zfpm_stop_stats_timer (void)
  1168. {
  1169. if (!zfpm_g->t_stats)
  1170. return;
  1171. zfpm_debug ("Stopping existing stats timer");
  1172. THREAD_TIMER_OFF (zfpm_g->t_stats);
  1173. }
  1174. /*
  1175. * zfpm_start_stats_timer
  1176. */
  1177. void
  1178. zfpm_start_stats_timer (void)
  1179. {
  1180. assert (!zfpm_g->t_stats);
  1181. THREAD_TIMER_ON (zfpm_g->master, zfpm_g->t_stats, zfpm_stats_timer_cb, 0,
  1182. ZFPM_STATS_IVL_SECS);
  1183. }
  1184. /*
  1185. * Helper macro for zfpm_show_stats() below.
  1186. */
  1187. #define ZFPM_SHOW_STAT(counter) \
  1188. do { \
  1189. vty_out (vty, "%-40s %10lu %16lu%s", #counter, total_stats.counter, \
  1190. zfpm_g->last_ivl_stats.counter, VTY_NEWLINE); \
  1191. } while (0)
  1192. /*
  1193. * zfpm_show_stats
  1194. */
  1195. static void
  1196. zfpm_show_stats (struct vty *vty)
  1197. {
  1198. zfpm_stats_t total_stats;
  1199. time_t elapsed;
  1200. vty_out (vty, "%s%-40s %10s Last %2d secs%s%s", VTY_NEWLINE, "Counter",
  1201. "Total", ZFPM_STATS_IVL_SECS, VTY_NEWLINE, VTY_NEWLINE);
  1202. /*
  1203. * Compute the total stats up to this instant.
  1204. */
  1205. zfpm_stats_compose (&zfpm_g->cumulative_stats, &zfpm_g->stats,
  1206. &total_stats);
  1207. ZFPM_SHOW_STAT (connect_calls);
  1208. ZFPM_SHOW_STAT (connect_no_sock);
  1209. ZFPM_SHOW_STAT (read_cb_calls);
  1210. ZFPM_SHOW_STAT (write_cb_calls);
  1211. ZFPM_SHOW_STAT (write_calls);
  1212. ZFPM_SHOW_STAT (partial_writes);
  1213. ZFPM_SHOW_STAT (max_writes_hit);
  1214. ZFPM_SHOW_STAT (t_write_yields);
  1215. ZFPM_SHOW_STAT (nop_deletes_skipped);
  1216. ZFPM_SHOW_STAT (route_adds);
  1217. ZFPM_SHOW_STAT (route_dels);
  1218. ZFPM_SHOW_STAT (updates_triggered);
  1219. ZFPM_SHOW_STAT (non_fpm_table_triggers);
  1220. ZFPM_SHOW_STAT (redundant_triggers);
  1221. ZFPM_SHOW_STAT (dests_del_after_update);
  1222. ZFPM_SHOW_STAT (t_conn_down_starts);
  1223. ZFPM_SHOW_STAT (t_conn_down_dests_processed);
  1224. ZFPM_SHOW_STAT (t_conn_down_yields);
  1225. ZFPM_SHOW_STAT (t_conn_down_finishes);
  1226. ZFPM_SHOW_STAT (t_conn_up_starts);
  1227. ZFPM_SHOW_STAT (t_conn_up_dests_processed);
  1228. ZFPM_SHOW_STAT (t_conn_up_yields);
  1229. ZFPM_SHOW_STAT (t_conn_up_aborts);
  1230. ZFPM_SHOW_STAT (t_conn_up_finishes);
  1231. if (!zfpm_g->last_stats_clear_time)
  1232. return;
  1233. elapsed = zfpm_get_elapsed_time (zfpm_g->last_stats_clear_time);
  1234. vty_out (vty, "%sStats were cleared %lu seconds ago%s", VTY_NEWLINE,
  1235. (unsigned long) elapsed, VTY_NEWLINE);
  1236. }
  1237. /*
  1238. * zfpm_clear_stats
  1239. */
  1240. static void
  1241. zfpm_clear_stats (struct vty *vty)
  1242. {
  1243. if (!zfpm_is_enabled ())
  1244. {
  1245. vty_out (vty, "The FPM module is not enabled...%s", VTY_NEWLINE);
  1246. return;
  1247. }
  1248. zfpm_stats_reset (&zfpm_g->stats);
  1249. zfpm_stats_reset (&zfpm_g->last_ivl_stats);
  1250. zfpm_stats_reset (&zfpm_g->cumulative_stats);
  1251. zfpm_stop_stats_timer ();
  1252. zfpm_start_stats_timer ();
  1253. zfpm_g->last_stats_clear_time = zfpm_get_time();
  1254. vty_out (vty, "Cleared FPM stats%s", VTY_NEWLINE);
  1255. }
  1256. /*
  1257. * show_zebra_fpm_stats
  1258. */
  1259. DEFUN (show_zebra_fpm_stats,
  1260. show_zebra_fpm_stats_cmd,
  1261. "show zebra fpm stats",
  1262. SHOW_STR
  1263. "Zebra information\n"
  1264. "Forwarding Path Manager information\n"
  1265. "Statistics\n")
  1266. {
  1267. zfpm_show_stats (vty);
  1268. return CMD_SUCCESS;
  1269. }
  1270. /*
  1271. * clear_zebra_fpm_stats
  1272. */
  1273. DEFUN (clear_zebra_fpm_stats,
  1274. clear_zebra_fpm_stats_cmd,
  1275. "clear zebra fpm stats",
  1276. CLEAR_STR
  1277. "Zebra information\n"
  1278. "Clear Forwarding Path Manager information\n"
  1279. "Statistics\n")
  1280. {
  1281. zfpm_clear_stats (vty);
  1282. return CMD_SUCCESS;
  1283. }
  1284. /*
  1285. * update fpm connection information
  1286. */
  1287. DEFUN ( fpm_remote_ip,
  1288. fpm_remote_ip_cmd,
  1289. "fpm connection ip A.B.C.D port <1-65535>",
  1290. "fpm connection remote ip and port\n"
  1291. "Remote fpm server ip A.B.C.D\n"
  1292. "Enter ip ")
  1293. {
  1294. in_addr_t fpm_server;
  1295. uint32_t port_no;
  1296. fpm_server = inet_addr (argv[0]);
  1297. if (fpm_server == INADDR_NONE)
  1298. return CMD_ERR_INCOMPLETE;
  1299. port_no = atoi (argv[1]);
  1300. if (port_no < TCP_MIN_PORT || port_no > TCP_MAX_PORT)
  1301. return CMD_ERR_INCOMPLETE;
  1302. zfpm_g->fpm_server = fpm_server;
  1303. zfpm_g->fpm_port = port_no;
  1304. return CMD_SUCCESS;
  1305. }
  1306. DEFUN ( no_fpm_remote_ip,
  1307. no_fpm_remote_ip_cmd,
  1308. "no fpm connection ip A.B.C.D port <1-65535>",
  1309. "fpm connection remote ip and port\n"
  1310. "Connection\n"
  1311. "Remote fpm server ip A.B.C.D\n"
  1312. "Enter ip ")
  1313. {
  1314. if (zfpm_g->fpm_server != inet_addr (argv[0]) ||
  1315. zfpm_g->fpm_port != atoi (argv[1]))
  1316. return CMD_ERR_NO_MATCH;
  1317. zfpm_g->fpm_server = FPM_DEFAULT_IP;
  1318. zfpm_g->fpm_port = FPM_DEFAULT_PORT;
  1319. return CMD_SUCCESS;
  1320. }
  1321. /*
  1322. * zfpm_init_message_format
  1323. */
  1324. static inline void
  1325. zfpm_init_message_format (const char *format)
  1326. {
  1327. int have_netlink, have_protobuf;
  1328. have_netlink = have_protobuf = 0;
  1329. #ifdef HAVE_NETLINK
  1330. have_netlink = 1;
  1331. #endif
  1332. #ifdef HAVE_PROTOBUF
  1333. have_protobuf = 1;
  1334. #endif
  1335. zfpm_g->message_format = ZFPM_MSG_FORMAT_NONE;
  1336. if (!format)
  1337. {
  1338. if (have_netlink)
  1339. {
  1340. zfpm_g->message_format = ZFPM_MSG_FORMAT_NETLINK;
  1341. }
  1342. else if (have_protobuf)
  1343. {
  1344. zfpm_g->message_format = ZFPM_MSG_FORMAT_PROTOBUF;
  1345. }
  1346. return;
  1347. }
  1348. if (!strcmp ("netlink", format))
  1349. {
  1350. if (!have_netlink)
  1351. {
  1352. zlog_err ("FPM netlink message format is not available");
  1353. return;
  1354. }
  1355. zfpm_g->message_format = ZFPM_MSG_FORMAT_NETLINK;
  1356. return;
  1357. }
  1358. if (!strcmp ("protobuf", format))
  1359. {
  1360. if (!have_protobuf)
  1361. {
  1362. zlog_err ("FPM protobuf message format is not available");
  1363. return;
  1364. }
  1365. zfpm_g->message_format = ZFPM_MSG_FORMAT_PROTOBUF;
  1366. return;
  1367. }
  1368. zlog_warn ("Unknown fpm format '%s'", format);
  1369. }
  1370. /**
  1371. * fpm_remote_srv_write
  1372. *
  1373. * Module to write remote fpm connection
  1374. *
  1375. * Returns ZERO on success.
  1376. */
  1377. int fpm_remote_srv_write (struct vty *vty )
  1378. {
  1379. struct in_addr in;
  1380. in.s_addr = zfpm_g->fpm_server;
  1381. if (zfpm_g->fpm_server != FPM_DEFAULT_IP ||
  1382. zfpm_g->fpm_port != FPM_DEFAULT_PORT)
  1383. vty_out (vty,"fpm connection ip %s port %d%s", inet_ntoa (in),zfpm_g->fpm_port,VTY_NEWLINE);
  1384. return 0;
  1385. }
  1386. /**
  1387. * zfpm_init
  1388. *
  1389. * One-time initialization of the Zebra FPM module.
  1390. *
  1391. * @param[in] port port at which FPM is running.
  1392. * @param[in] enable TRUE if the zebra FPM module should be enabled
  1393. * @param[in] format to use to talk to the FPM. Can be 'netink' or 'protobuf'.
  1394. *
  1395. * Returns TRUE on success.
  1396. */
  1397. int
  1398. zfpm_init (struct thread_master *master, int enable, uint16_t port,
  1399. const char *format)
  1400. {
  1401. static int initialized = 0;
  1402. if (initialized) {
  1403. return 1;
  1404. }
  1405. initialized = 1;
  1406. memset (zfpm_g, 0, sizeof (*zfpm_g));
  1407. zfpm_g->master = master;
  1408. TAILQ_INIT(&zfpm_g->dest_q);
  1409. zfpm_g->sock = -1;
  1410. zfpm_g->state = ZFPM_STATE_IDLE;
  1411. zfpm_stats_init (&zfpm_g->stats);
  1412. zfpm_stats_init (&zfpm_g->last_ivl_stats);
  1413. zfpm_stats_init (&zfpm_g->cumulative_stats);
  1414. install_element (ENABLE_NODE, &show_zebra_fpm_stats_cmd);
  1415. install_element (ENABLE_NODE, &clear_zebra_fpm_stats_cmd);
  1416. install_element (CONFIG_NODE, &fpm_remote_ip_cmd);
  1417. install_element (CONFIG_NODE, &no_fpm_remote_ip_cmd);
  1418. zfpm_init_message_format(format);
  1419. /*
  1420. * Disable FPM interface if no suitable format is available.
  1421. */
  1422. if (zfpm_g->message_format == ZFPM_MSG_FORMAT_NONE)
  1423. enable = 0;
  1424. zfpm_g->enabled = enable;
  1425. if (!enable) {
  1426. return 1;
  1427. }
  1428. if (!zfpm_g->fpm_server)
  1429. zfpm_g->fpm_server = FPM_DEFAULT_IP;
  1430. if (!port)
  1431. port = FPM_DEFAULT_PORT;
  1432. zfpm_g->fpm_port = port;
  1433. zfpm_g->obuf = stream_new (ZFPM_OBUF_SIZE);
  1434. zfpm_g->ibuf = stream_new (ZFPM_IBUF_SIZE);
  1435. zfpm_start_stats_timer ();
  1436. zfpm_start_connect_timer ("initialized");
  1437. return 1;
  1438. }