3 * vrrp.h - vrrp plug-in header file
5 * Copyright 2019-2020 Rubicon Communications, LLC (Netgate)
7 * SPDX-License-Identifier: Apache-2.0
10 #ifndef __included_vrrp_h__
11 #define __included_vrrp_h__
13 #include <vnet/vnet.h>
14 #include <vnet/ip/ip.h>
15 #include <vnet/ethernet/ethernet.h>
17 #include <vppinfra/hash.h>
18 #include <vppinfra/error.h>
20 /* VRRP configuration */
21 typedef enum vrrp_vr_flags
23 VRRP_VR_PREEMPT = 0x1,
25 VRRP_VR_UNICAST = 0x4,
29 typedef struct vrrp_vr_key
38 (struct vrrp4_arp_key {
51 (struct vrrp6_nd_key {
57 typedef struct vrrp_vr_tracking_if
61 } vrrp_vr_tracking_if_t;
63 typedef struct vrrp_vr_tracking
65 vrrp_vr_tracking_if_t *interfaces;
69 typedef struct vrrp_vr_config
75 vrrp_vr_flags_t flags;
76 ip46_address_t *vr_addrs;
77 ip46_address_t *peer_addrs;
80 #define foreach_vrrp_vr_state \
81 _(0, INIT, "Initialize") \
82 _(1, BACKUP, "Backup") \
83 _(2, MASTER, "Master") \
84 _(3, INTF_DOWN, "Interface Down")
86 /* VRRP runtime data */
87 typedef enum vrrp_vr_state
89 #define _(v,f,n) VRRP_VR_STATE_##f = v,
94 typedef struct vrrp_vr_runtime
96 vrrp_vr_state_t state;
106 typedef struct vrrp_vr
108 vrrp_vr_config_t config;
109 vrrp_vr_runtime_t runtime;
110 vrrp_vr_tracking_t tracking;
115 typedef enum vrrp_vr_timer_type
118 VRRP_VR_TIMER_MASTER_DOWN,
119 } vrrp_vr_timer_type_t;
121 typedef struct vrrp_vr_timer
124 f64 expire_time; /* monotonic, relative to vlib_time_now() */
125 vrrp_vr_timer_type_t type;
130 /* vectors of vr indices which are configured on this interface
131 * 0 -> ipv4, 1 -> ipv6 */
134 /* vector of VR indices which track the state of this interface
136 u32 *tracking_vrs[2];
138 /* multicast adjacency indices. 0 -> ipv4, 1 -> ipv6 */
139 adj_index_t mcast_adj_index[2];
141 /* number of VRs in master state on sw intf. 0 -> ipv4, 1 -> ipv6 */
148 /* API message ID base */
154 /* pool of timers and ordered vector of pool indices */
155 vrrp_vr_timer_t *vr_timers;
158 /* number of running VRs - don't register for VRRP proto if not running */
161 /* hash mapping a VR key to a pool entry */
162 mhash_t vr_index_by_key;
164 /* hashes mapping sw_if_index and address to a vr index */
165 uword *vrrp4_arp_lookup;
166 uword *vrrp6_nd_lookup;
168 /* vector of interface data indexed by sw_if_index */
169 vrrp_intf_t *vrrp_intfs;
172 vlib_main_t *vlib_main;
173 vnet_main_t *vnet_main;
174 ethernet_main_t *ethernet_main;
176 u32 intf_output_node_idx;
179 extern vrrp_main_t vrrp_main;
181 extern vlib_node_registration_t vrrp_node;
182 extern vlib_node_registration_t vrrp_periodic_node;
184 /* Periodic function events */
185 #define VRRP_EVENT_VR_TIMER_UPDATE 1
186 #define VRRP_EVENT_VR_STOP 2
187 #define VRRP_EVENT_PERIODIC_ENABLE_DISABLE 3
189 /* global error counter types */
190 #define foreach_vrrp_err_counter \
198 typedef enum vrrp_err_counter_
200 #define _(sym, val) VRRP_ERR_COUNTER_##sym = val,
201 foreach_vrrp_err_counter
203 } vrrp_err_counter_t;
205 #define VRRP_ERR_COUNTER_MAX 6
207 /* per-instance stats */
208 #define foreach_vrrp_stat_counter \
209 _ (MASTER_TRANS, 0) \
215 typedef enum vrrp_stat_counter_
217 #define _(sym, val) VRRP_STAT_COUNTER_##sym = val,
218 foreach_vrrp_stat_counter
220 } vrrp_stat_counter_t;
222 #define VRRP_STAT_COUNTER_MAX 5
224 clib_error_t *vrrp_plugin_api_hookup (vlib_main_t * vm);
226 int vrrp_vr_add_del (u8 is_add, vrrp_vr_config_t *conf, index_t *ret_index);
227 int vrrp_vr_update (index_t *vrrp_index, vrrp_vr_config_t *vr_conf);
228 int vrrp_vr_del (index_t vrrp_index);
229 int vrrp_vr_start_stop (u8 is_start, vrrp_vr_key_t * vr_key);
230 extern u8 *format_vrrp_vr (u8 * s, va_list * args);
231 extern u8 *format_vrrp_vr_key (u8 * s, va_list * args);
232 extern u8 *format_vrrp_vr_state (u8 * s, va_list * args);
233 extern u8 *format_vrrp_packet_hdr (u8 * s, va_list * args);
234 void vrrp_vr_timer_set (vrrp_vr_t * vr, vrrp_vr_timer_type_t type);
235 void vrrp_vr_timer_cancel (vrrp_vr_t * vr);
236 void vrrp_vr_transition (vrrp_vr_t * vr, vrrp_vr_state_t new_state,
238 int vrrp_vr_set_peers (vrrp_vr_key_t * key, ip46_address_t * peers);
239 int vrrp_vr_multicast_group_join (vrrp_vr_t * vr);
240 int vrrp_adv_send (vrrp_vr_t * vr, int shutdown);
241 int vrrp_garp_or_na_send (vrrp_vr_t * vr);
242 u16 vrrp_adv_csum (void *l3_hdr, void *payload, u8 is_ipv6, u16 len);
243 int vrrp_vr_tracking_if_add_del (vrrp_vr_t * vr, u32 sw_if_index,
244 u8 priority, u8 is_add);
245 int vrrp_vr_tracking_ifs_add_del (vrrp_vr_t * vr,
246 vrrp_vr_tracking_if_t * track_ifs,
248 void vrrp_vr_event (vrrp_vr_t * vr, vrrp_vr_state_t new_state);
251 void vrrp_incr_err_counter (vrrp_err_counter_t err_type);
252 void vrrp_incr_stat_counter (vrrp_stat_counter_t stat_type, u32 stat_index);
255 vrrp_vr_skew_compute (vrrp_vr_t * vr)
257 vrrp_vr_config_t *vrc = &vr->config;
258 vrrp_vr_runtime_t *vrt = &vr->runtime;
260 vrt->skew = (((256 - vrc->priority) * vrt->master_adv_int) / 256);
264 vrrp_vr_master_down_compute (vrrp_vr_t * vr)
266 vrrp_vr_runtime_t *vrt = &vr->runtime;
268 vrt->master_down_int = (3 * vrt->master_adv_int) + vrt->skew;
271 always_inline vrrp_vr_t *
272 vrrp_vr_lookup (u32 sw_if_index, u8 vr_id, u8 is_ipv6)
274 vrrp_main_t *vmp = &vrrp_main;
278 clib_memset (&key, 0, sizeof (key));
280 key.sw_if_index = sw_if_index;
282 key.is_ipv6 = (is_ipv6 != 0);
284 p = mhash_get (&vmp->vr_index_by_key, &key);
286 return pool_elt_at_index (vmp->vrs, p[0]);
291 always_inline vrrp_vr_t *
292 vrrp_vr_lookup_index (u32 vr_index)
294 vrrp_main_t *vmp = &vrrp_main;
296 if (pool_is_free_index (vmp->vrs, vr_index))
299 return pool_elt_at_index (vmp->vrs, vr_index);
303 vrrp_vr_lookup_address (u32 sw_if_index, u8 is_ipv6, void *addr)
305 vrrp_main_t *vmp = &vrrp_main;
307 vrrp4_arp_key_t key4;
312 key6.sw_if_index = sw_if_index;
313 key6.addr = ((ip6_address_t *) addr)[0];
314 p = hash_get_mem (vmp->vrrp6_nd_lookup, &key6);
318 key4.sw_if_index = sw_if_index;
319 key4.addr = ((ip4_address_t *) addr)[0];
320 p = hash_get (vmp->vrrp4_arp_lookup, key4.as_u64);
329 always_inline vrrp_intf_t *
330 vrrp_intf_get (u32 sw_if_index)
332 vrrp_main_t *vrm = &vrrp_main;
334 if (sw_if_index == ~0)
337 vec_validate (vrm->vrrp_intfs, sw_if_index);
338 return vec_elt_at_index (vrm->vrrp_intfs, sw_if_index);
342 vrrp_intf_num_vrs (u32 sw_if_index, u8 is_ipv6)
344 vrrp_intf_t *intf = vrrp_intf_get (sw_if_index);
347 return vec_len (intf->vr_indices[is_ipv6]);
353 vrrp_vr_is_ipv6 (vrrp_vr_t * vr)
355 return ((vr->config.flags & VRRP_VR_IPV6) != 0);
359 vrrp_vr_is_unicast (vrrp_vr_t * vr)
361 return ((vr->config.flags & VRRP_VR_UNICAST) != 0);
365 vrrp_vr_is_owner (vrrp_vr_t * vr)
367 return (vr->config.priority == 255);
371 vrrp_vr_n_vr_addrs (vrrp_vr_t * vr)
373 return vec_len (vr->config.vr_addrs);
377 vrrp_vr_n_peer_addrs (vrrp_vr_t * vr)
379 return vec_len (vr->config.peer_addrs);
383 vrrp_vr_accept_mode_enabled (vrrp_vr_t * vr)
385 return ((vr->config.flags & VRRP_VR_ACCEPT) != 0);
389 vrrp_vr_index (vrrp_vr_t * vr)
391 vrrp_main_t *vmp = &vrrp_main;
393 return vr - vmp->vrs;
397 vrrp_vr_priority (vrrp_vr_t * vr)
401 if (vr->tracking.interfaces_dec < (u32) vr->config.priority)
402 rv = vr->config.priority - vr->tracking.interfaces_dec;
409 #endif /* __included_vrrp_h__ */
412 * fd.io coding-style-patch-verification: ON
415 * eval: (c-set-style "gnu")