PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/vnet/bfd/bfd_main.h

https://gitlab.com/x1046882802/flexiroutervpp
C Header | 473 lines | 240 code | 100 blank | 133 comment | 6 complexity | a986debbca5e1cf8a402e254b031b524 MD5 | raw file
  1. /*
  2. * Copyright (c) 2011-2016 Cisco and/or its affiliates.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at:
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /**
  16. * @file
  17. * @brief BFD global declarations
  18. */
  19. #ifndef __included_bfd_main_h__
  20. #define __included_bfd_main_h__
  21. #include <vnet/vnet.h>
  22. #include <vnet/bfd/bfd_protocol.h>
  23. #include <vnet/bfd/bfd_udp.h>
  24. #include <vlib/log.h>
  25. #include <vppinfra/os.h>
  26. #include <vppinfra/tw_timer_1t_3w_1024sl_ov.h>
  27. #define foreach_bfd_mode(F) \
  28. F (asynchronous) \
  29. F (demand)
  30. typedef enum
  31. {
  32. #define F(x) BFD_MODE_##x,
  33. foreach_bfd_mode (F)
  34. #undef F
  35. } bfd_mode_e;
  36. typedef struct
  37. {
  38. /** global configuration key ID */
  39. u32 conf_key_id;
  40. /** keeps track of how many sessions reference this key */
  41. u32 use_count;
  42. /**
  43. * key data directly usable for bfd purposes - already padded with zeroes
  44. * (so we don't need the actual length)
  45. */
  46. u8 key[20];
  47. /** authentication type for this key */
  48. bfd_auth_type_e auth_type;
  49. } bfd_auth_key_t;
  50. #define foreach_bfd_poll_state(F) \
  51. F (NOT_NEEDED) \
  52. F (NEEDED) \
  53. F (IN_PROGRESS) \
  54. F (IN_PROGRESS_AND_QUEUED)
  55. typedef enum
  56. {
  57. #define F(x) BFD_POLL_##x,
  58. foreach_bfd_poll_state (F)
  59. #undef F
  60. } bfd_poll_state_e;
  61. /**
  62. * hop types
  63. */
  64. #define foreach_bfd_hop(F) \
  65. F (SINGLE, "single") \
  66. F (MULTI, "multi") \
  67. typedef enum
  68. {
  69. #define F(sym, str) BFD_HOP_TYPE_##sym,
  70. foreach_bfd_hop (F)
  71. #undef F
  72. } bfd_hop_type_e;
  73. typedef struct bfd_session_s
  74. {
  75. /** index in bfd_main.sessions pool */
  76. u32 bs_idx;
  77. /** session state */
  78. bfd_state_e local_state;
  79. /** remote session state */
  80. bfd_state_e remote_state;
  81. /** BFD hop type */
  82. bfd_hop_type_e hop_type;
  83. /** local diagnostics */
  84. bfd_diag_code_e local_diag;
  85. /** remote diagnostics */
  86. bfd_diag_code_e remote_diag;
  87. /** local discriminator */
  88. u32 local_discr;
  89. /** remote discriminator */
  90. u32 remote_discr;
  91. /** configured desired min tx interval (microseconds) */
  92. u32 config_desired_min_tx_usec;
  93. /** configured desired min tx interval (nsec) */
  94. u64 config_desired_min_tx_nsec;
  95. /** effective desired min tx interval (nsec) */
  96. u64 effective_desired_min_tx_nsec;
  97. /** configured required min rx interval (microseconds) */
  98. u32 config_required_min_rx_usec;
  99. /** configured required min rx interval (nsec) */
  100. u64 config_required_min_rx_nsec;
  101. /** effective required min rx interval (nsec) */
  102. u64 effective_required_min_rx_nsec;
  103. /** remote min rx interval (microseconds) */
  104. u64 remote_min_rx_usec;
  105. /** remote min rx interval (nsec) */
  106. u64 remote_min_rx_nsec;
  107. /** remote min echo rx interval (microseconds) */
  108. u64 remote_min_echo_rx_usec;
  109. /** remote min echo rx interval (nsec) */
  110. u64 remote_min_echo_rx_nsec;
  111. /** remote desired min tx interval (nsec) */
  112. u64 remote_desired_min_tx_nsec;
  113. /** configured detect multiplier */
  114. u8 local_detect_mult;
  115. /** 1 if remote system sets demand mode, 0 otherwise */
  116. u8 remote_demand;
  117. /** remote detect multiplier */
  118. u8 remote_detect_mult;
  119. /** 1 is echo function is active, 0 otherwise */
  120. u8 echo;
  121. /** next event time in nsec for this session (0 if no event) */
  122. u64 event_time_nsec;
  123. /** timing wheel internal id used to manipulate timer (if set) */
  124. u32 tw_id;
  125. /** transmit interval */
  126. u64 transmit_interval_nsec;
  127. /** next time at which to transmit a packet */
  128. u64 tx_timeout_nsec;
  129. /** timestamp of last packet transmitted */
  130. u64 last_tx_nsec;
  131. /** timestamp of last packet received */
  132. u64 last_rx_nsec;
  133. /** transmit interval for echo packets */
  134. u64 echo_transmit_interval_nsec;
  135. /** next time at which to transmit echo packet */
  136. u64 echo_tx_timeout_nsec;
  137. /** timestamp of last echo packet transmitted */
  138. u64 echo_last_tx_nsec;
  139. /** timestamp of last echo packet received */
  140. u64 echo_last_rx_nsec;
  141. /** secret used for calculating/checking checksum of echo packets */
  142. u32 echo_secret;
  143. /** detection time */
  144. u64 detection_time_nsec;
  145. /** state info regarding poll sequence */
  146. bfd_poll_state_e poll_state;
  147. /**
  148. * helper for delayed poll sequence - marks either start of running poll
  149. * sequence or timeout, after which we can start the next poll sequnce
  150. */
  151. u64 poll_state_start_or_timeout_nsec;
  152. /** authentication information */
  153. struct
  154. {
  155. /** current key in use */
  156. bfd_auth_key_t *curr_key;
  157. /**
  158. * set to next key to use if delayed switch is enabled - in that case
  159. * the key is switched when first incoming packet is signed with next_key
  160. */
  161. bfd_auth_key_t *next_key;
  162. /** sequence number incremented occasionally or always (if meticulous) */
  163. u32 local_seq_number;
  164. /** remote sequence number */
  165. u32 remote_seq_number;
  166. /** set to 1 if remote sequence number is known */
  167. u8 remote_seq_number_known;
  168. /** current key ID sent out in bfd packet */
  169. u8 curr_bfd_key_id;
  170. /** key ID to use when switched to next_key */
  171. u8 next_bfd_key_id;
  172. /**
  173. * set to 1 if delayed action is pending, which might be activation
  174. * of authentication, change of key or deactivation
  175. */
  176. u8 is_delayed;
  177. } auth;
  178. /** transport type for this session */
  179. bfd_transport_e transport;
  180. /** union of transport-specific data */
  181. union
  182. {
  183. bfd_udp_session_t udp;
  184. };
  185. } bfd_session_t;
  186. /**
  187. * listener events
  188. */
  189. #define foreach_bfd_listen_event(F) \
  190. F (CREATE, "sesion-created") \
  191. F (UPDATE, "session-updated") \
  192. F (DELETE, "session-deleted")
  193. typedef enum
  194. {
  195. #define F(sym, str) BFD_LISTEN_EVENT_##sym,
  196. foreach_bfd_listen_event (F)
  197. #undef F
  198. } bfd_listen_event_e;
  199. /**
  200. * session nitification call back function type
  201. */
  202. typedef void (*bfd_notify_fn_t) (bfd_listen_event_e, const bfd_session_t *);
  203. typedef struct
  204. {
  205. /** lock to protect data structures */
  206. clib_spinlock_t lock;
  207. int lock_recursion_count;
  208. uword owner_thread_index;
  209. /** Number of event wakeup RPCs in flight. Should be 0 or 1 */
  210. int bfd_process_wakeup_events_in_flight;
  211. /** The timestamp of last wakeup event being sent */
  212. u64 bfd_process_wakeup_event_start_nsec;
  213. /** The time it took the last wakeup event to make it to handling */
  214. u64 bfd_process_wakeup_event_delay_nsec;
  215. /** When the bfd process is supposed to wake up next */
  216. u64 bfd_process_next_wakeup_nsec;
  217. /** pool of bfd sessions context data */
  218. bfd_session_t *sessions;
  219. /** timing wheel for scheduling timeouts */
  220. TWT (tw_timer_wheel) wheel;
  221. /** hashmap - bfd session by discriminator */
  222. u32 *session_by_disc;
  223. /** background process node index */
  224. u32 bfd_process_node_index;
  225. /** convenience variables */
  226. vlib_main_t *vlib_main;
  227. vnet_main_t *vnet_main;
  228. /** how many nanoseconds is one timing wheel tick */
  229. u64 nsec_per_tw_tick;
  230. /** default desired min tx in nsec */
  231. u64 default_desired_min_tx_nsec;
  232. /** minimum required min rx while echo function is active - nsec */
  233. u64 min_required_min_rx_while_echo_nsec;
  234. /** for generating random numbers */
  235. u32 random_seed;
  236. /** pool of authentication keys */
  237. bfd_auth_key_t *auth_keys;
  238. /** hashmap - index in pool auth_keys by conf_key_id */
  239. u32 *auth_key_by_conf_key_id;
  240. /** vector of callback notification functions */
  241. bfd_notify_fn_t *listeners;
  242. /** log class */
  243. vlib_log_class_t log_class;
  244. } bfd_main_t;
  245. extern bfd_main_t bfd_main;
  246. /** Packet counters */
  247. #define foreach_bfd_error(F) \
  248. F (NONE, "good bfd packets (processed)") \
  249. F (BAD, "invalid bfd packets") \
  250. F (DISABLED, "bfd packets received on disabled interfaces")
  251. typedef enum
  252. {
  253. #define F(sym, str) BFD_ERROR_##sym,
  254. foreach_bfd_error (F)
  255. #undef F
  256. BFD_N_ERROR,
  257. } bfd_error_t;
  258. /** bfd packet trace capture */
  259. typedef struct
  260. {
  261. u32 len;
  262. u8 data[400];
  263. } bfd_input_trace_t;
  264. typedef enum
  265. {
  266. BFD_EVENT_RESCHEDULE = 1,
  267. BFD_EVENT_NEW_SESSION,
  268. BFD_EVENT_CONFIG_CHANGED,
  269. } bfd_process_event_e;
  270. /* *INDENT-OFF* */
  271. /** echo packet structure */
  272. typedef CLIB_PACKED (struct {
  273. /** local discriminator */
  274. u32 discriminator;
  275. /** expire time of this packet - nsec */
  276. u64 expire_time_nsec;
  277. /** checksum - based on discriminator, local secret and expire time */
  278. u64 checksum;
  279. }) bfd_echo_pkt_t;
  280. /* *INDENT-ON* */
  281. static inline void
  282. bfd_lock (bfd_main_t * bm)
  283. {
  284. uword my_thread_index = __os_thread_index;
  285. if (bm->owner_thread_index == my_thread_index
  286. && bm->lock_recursion_count > 0)
  287. {
  288. bm->lock_recursion_count++;
  289. return;
  290. }
  291. clib_spinlock_lock_if_init (&bm->lock);
  292. bm->lock_recursion_count = 1;
  293. bm->owner_thread_index = my_thread_index;
  294. }
  295. static inline void
  296. bfd_unlock (bfd_main_t * bm)
  297. {
  298. uword my_thread_index = __os_thread_index;
  299. ASSERT (bm->owner_thread_index == my_thread_index);
  300. if (bm->lock_recursion_count > 1)
  301. {
  302. bm->lock_recursion_count--;
  303. return;
  304. }
  305. bm->lock_recursion_count = 0;
  306. bm->owner_thread_index = ~0;
  307. clib_spinlock_unlock_if_init (&bm->lock);
  308. }
  309. static inline void
  310. bfd_lock_check (bfd_main_t * bm)
  311. {
  312. if (PREDICT_FALSE (bm->lock_recursion_count < 1))
  313. clib_warning ("lock check failure");
  314. }
  315. u8 *bfd_input_format_trace (u8 * s, va_list * args);
  316. bfd_session_t *bfd_get_session (bfd_main_t * bm, bfd_transport_e t);
  317. void bfd_put_session (bfd_main_t * bm, bfd_session_t * bs);
  318. bfd_session_t *bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx);
  319. bfd_session_t *bfd_find_session_by_disc (bfd_main_t * bm, u32 disc);
  320. void bfd_session_start (bfd_main_t * bm, bfd_session_t * bs);
  321. void bfd_consume_pkt (vlib_main_t * vm, bfd_main_t * bm,
  322. const bfd_pkt_t * bfd, u32 bs_idx);
  323. int bfd_consume_echo_pkt (vlib_main_t * vm, bfd_main_t * bm,
  324. vlib_buffer_t * b);
  325. int bfd_verify_pkt_common (const bfd_pkt_t * pkt);
  326. int bfd_verify_pkt_auth (vlib_main_t * vm, const bfd_pkt_t * pkt,
  327. u16 pkt_size, bfd_session_t * bs);
  328. void bfd_event (bfd_main_t * bm, bfd_session_t * bs);
  329. void bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
  330. bfd_main_t * bm, bfd_session_t * bs,
  331. int is_local);
  332. u8 *format_bfd_session (u8 * s, va_list * args);
  333. u8 *format_bfd_session_brief (u8 * s, va_list * args);
  334. u8 *format_bfd_auth_key (u8 * s, va_list * args);
  335. void bfd_session_set_flags (vlib_main_t * vm, bfd_session_t * bs,
  336. u8 admin_up_down);
  337. unsigned bfd_auth_type_supported (bfd_auth_type_e auth_type);
  338. vnet_api_error_t bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
  339. u8 bfd_key_id, u8 is_delayed);
  340. vnet_api_error_t bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed);
  341. vnet_api_error_t bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
  342. u32 desired_min_tx_usec,
  343. u32 required_min_rx_usec,
  344. u8 detect_mult);
  345. u32 bfd_nsec_to_usec (u64 nsec);
  346. const char *bfd_poll_state_string (bfd_poll_state_e state);
  347. #define USEC_PER_MS (1000LL)
  348. #define MSEC_PER_SEC (1000LL)
  349. #define NSEC_PER_USEC (1000LL)
  350. #define USEC_PER_SEC (MSEC_PER_SEC * USEC_PER_MS)
  351. #define NSEC_PER_SEC (NSEC_PER_USEC * USEC_PER_SEC)
  352. #define SEC_PER_NSEC ((f64)1/NSEC_PER_SEC)
  353. /** timing wheel tick-rate, 1ms should be good enough */
  354. #define BFD_TW_TPS (MSEC_PER_SEC)
  355. /** default, slow transmission interval for BFD packets, per spec at least 1s */
  356. #define BFD_DEFAULT_DESIRED_MIN_TX_USEC USEC_PER_SEC
  357. /**
  358. * minimum required min rx set locally when echo function is used, per spec
  359. * should be set to at least 1s
  360. */
  361. #define BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO USEC_PER_SEC
  362. /**
  363. * Register a callback function to receive session notifications.
  364. */
  365. void bfd_register_listener (bfd_notify_fn_t fn);
  366. #endif /* __included_bfd_main_h__ */
  367. /*
  368. * fd.io coding-style-patch-verification: ON
  369. *
  370. * Local Variables:
  371. * eval: (c-set-style "gnu")
  372. * End:
  373. */