FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
vrrp.h
Go to the documentation of this file.
1 
2 /*
3  * vrrp.h - vrrp plug-in header file
4  *
5  * Copyright 2019-2020 Rubicon Communications, LLC (Netgate)
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  *
9  */
10 #ifndef __included_vrrp_h__
11 #define __included_vrrp_h__
12 
13 #include <vnet/vnet.h>
14 #include <vnet/ip/ip.h>
15 #include <vnet/ethernet/ethernet.h>
16 
17 #include <vppinfra/hash.h>
18 #include <vppinfra/error.h>
19 
20 /* VRRP configuration */
21 typedef enum vrrp_vr_flags
22 {
26  VRRP_VR_IPV6 = 0x8,
28 
29 typedef struct vrrp_vr_key
30 {
35 
36 /* *INDENT-OFF* */
37 typedef CLIB_PACKED
38 (struct vrrp4_arp_key {
39  union {
40  struct {
43  };
44  u64 as_u64;
45  };
46 }) vrrp4_arp_key_t;
47 /* *INDENT-ON* */
48 
49 /* *INDENT-OFF* */
50 typedef CLIB_PACKED
51 (struct vrrp6_nd_key {
53  ip6_address_t addr;
54 }) vrrp6_nd_key_t;
55 /* *INDENT-ON* */
56 
57 typedef struct vrrp_vr_tracking_if
58 {
62 
63 typedef struct vrrp_vr_tracking
64 {
68 
69 typedef struct vrrp_vr_config
70 {
76  ip46_address_t *vr_addrs;
77  ip46_address_t *peer_addrs;
79 
80 #define foreach_vrrp_vr_state \
81 _(0, INIT, "Initialize") \
82 _(1, BACKUP, "Backup") \
83 _(2, MASTER, "Master") \
84 _(3, INTF_DOWN, "Interface Down")
85 
86 /* VRRP runtime data */
87 typedef enum vrrp_vr_state
88 {
89 #define _(v,f,n) VRRP_VR_STATE_##f = v,
91 #undef _
93 
94 typedef struct vrrp_vr_runtime
95 {
104 
105 /* Per-VR data */
106 typedef struct vrrp_vr
107 {
111 } vrrp_vr_t;
112 
113 /* Timers */
114 typedef enum vrrp_vr_timer_type
115 {
119 
120 typedef struct vrrp_vr_timer
121 {
123  f64 expire_time; /* monotonic, relative to vlib_time_now() */
126 
127 typedef struct
128 {
129  /* vectors of vr indices which are configured on this interface
130  * 0 -> ipv4, 1 -> ipv6 */
131  u32 *vr_indices[2];
132 
133  /* vector of VR indices which track the state of this interface
134  * 0 -> ipv4, 1*/
135  u32 *tracking_vrs[2];
136 
137  /* multicast adjacency indices. 0 -> ipv4, 1 -> ipv6 */
138  adj_index_t mcast_adj_index[2];
139 
140  /* number of VRs in master state on sw intf. 0 -> ipv4, 1 -> ipv6 */
141  u8 n_master_vrs[2];
142 
143 } vrrp_intf_t;
144 
145 typedef struct
146 {
147  /* API message ID base */
149 
150  /* pool of VRs */
152 
153  /* pool of timers and ordered vector of pool indices */
156 
157  /* number of running VRs - don't register for VRRP proto if not running */
159 
160  /* hash mapping a VR key to a pool entry */
162 
163  /* hashes mapping sw_if_index and address to a vr index */
166 
167  /* vector of interface data indexed by sw_if_index */
169 
170  /* convenience */
174 
176 } vrrp_main_t;
177 
178 extern vrrp_main_t vrrp_main;
179 
182 
183 /* Periodic function events */
184 #define VRRP_EVENT_VR_TIMER_UPDATE 1
185 #define VRRP_EVENT_VR_STOP 2
186 #define VRRP_EVENT_PERIODIC_ENABLE_DISABLE 3
187 
189 
190 int vrrp_vr_add_del (u8 is_add, vrrp_vr_config_t * conf);
191 int vrrp_vr_start_stop (u8 is_start, vrrp_vr_key_t * vr_key);
192 extern u8 *format_vrrp_vr (u8 * s, va_list * args);
193 extern u8 *format_vrrp_vr_key (u8 * s, va_list * args);
194 extern u8 *format_vrrp_vr_state (u8 * s, va_list * args);
195 extern u8 *format_vrrp_packet_hdr (u8 * s, va_list * args);
197 void vrrp_vr_timer_cancel (vrrp_vr_t * vr);
198 void vrrp_vr_transition (vrrp_vr_t * vr, vrrp_vr_state_t new_state,
199  void *data);
200 int vrrp_vr_set_peers (vrrp_vr_key_t * key, ip46_address_t * peers);
202 int vrrp_adv_send (vrrp_vr_t * vr, int shutdown);
204 u16 vrrp_adv_csum (void *l3_hdr, void *payload, u8 is_ipv6, u16 len);
206  u8 priority, u8 is_add);
208  vrrp_vr_tracking_if_t * track_ifs,
209  u8 is_add);
210 void vrrp_vr_event (vrrp_vr_t * vr, vrrp_vr_state_t new_state);
211 
212 
213 always_inline void
215 {
216  vrrp_vr_config_t *vrc = &vr->config;
217  vrrp_vr_runtime_t *vrt = &vr->runtime;
218 
219  vrt->skew = (((256 - vrc->priority) * vrt->master_adv_int) / 256);
220 }
221 
222 always_inline void
224 {
225  vrrp_vr_runtime_t *vrt = &vr->runtime;
226 
227  vrt->master_down_int = (3 * vrt->master_adv_int) + vrt->skew;
228 }
229 
232 {
233  vrrp_main_t *vmp = &vrrp_main;
235  uword *p;
236 
237  clib_memset (&key, 0, sizeof (key));
238 
239  key.sw_if_index = sw_if_index;
240  key.vr_id = vr_id;
241  key.is_ipv6 = (is_ipv6 != 0);
242 
243  p = mhash_get (&vmp->vr_index_by_key, &key);
244  if (p)
245  return pool_elt_at_index (vmp->vrs, p[0]);
246 
247  return 0;
248 }
249 
252 {
253  vrrp_main_t *vmp = &vrrp_main;
254 
255  if (pool_is_free_index (vmp->vrs, vr_index))
256  return 0;
257 
258  return pool_elt_at_index (vmp->vrs, vr_index);
259 }
260 
263 {
264  vrrp_main_t *vmp = &vrrp_main;
265  uword *p;
266  vrrp4_arp_key_t key4;
267  vrrp6_nd_key_t key6;
268 
269  if (is_ipv6)
270  {
271  key6.sw_if_index = sw_if_index;
272  key6.addr = ((ip6_address_t *) addr)[0];
273  p = hash_get_mem (vmp->vrrp6_nd_lookup, &key6);
274  }
275  else
276  {
277  key4.sw_if_index = sw_if_index;
278  key4.addr = ((ip4_address_t *) addr)[0];
279  p = hash_get (vmp->vrrp4_arp_lookup, key4.as_u64);
280  }
281 
282  if (p)
283  return p[0];
284 
285  return ~0;
286 }
287 
290 {
291  vrrp_main_t *vrm = &vrrp_main;
292 
293  if (sw_if_index == ~0)
294  return NULL;
295 
297  return vec_elt_at_index (vrm->vrrp_intfs, sw_if_index);
298 }
299 
300 always_inline int
302 {
304 
305  if (intf)
306  return vec_len (intf->vr_indices[is_ipv6]);
307 
308  return 0;
309 }
310 
313 {
314  return ((vr->config.flags & VRRP_VR_IPV6) != 0);
315 }
316 
319 {
320  return ((vr->config.flags & VRRP_VR_UNICAST) != 0);
321 }
322 
325 {
326  return (vr->config.priority == 255);
327 }
328 
331 {
332  return vec_len (vr->config.vr_addrs);
333 }
334 
337 {
338  return vec_len (vr->config.peer_addrs);
339 }
340 
343 {
344  return ((vr->config.flags & VRRP_VR_ACCEPT) != 0);
345 }
346 
349 {
350  vrrp_main_t *vmp = &vrrp_main;
351 
352  return vr - vmp->vrs;
353 }
354 
357 {
358  u8 rv;
359 
360  if (vr->tracking.interfaces_dec < (u32) vr->config.priority)
362  else
363  rv = 1;
364 
365  return rv;
366 }
367 
368 #endif /* __included_vrrp_h__ */
369 
370 /*
371  * fd.io coding-style-patch-verification: ON
372  *
373  * Local Variables:
374  * eval: (c-set-style "gnu")
375  * End:
376  */
vrrp_vr_runtime_t
struct vrrp_vr_runtime vrrp_vr_runtime_t
vrrp_vr_config
Definition: vrrp.h:69
vrrp_vr
Definition: vrrp.h:106
vrrp_vr_start_stop
int vrrp_vr_start_stop(u8 is_start, vrrp_vr_key_t *vr_key)
Definition: vrrp.c:710
vrrp_vr::config
vrrp_vr_config_t config
Definition: vrrp.h:108
vrrp_vr_flags
vrrp_vr_flags
Definition: vrrp.api:21
is_ipv6
bool is_ipv6
Definition: dhcp.api:202
vrrp_vr_config::vr_addrs
ip46_address_t * vr_addrs
Definition: vrrp.h:76
vrrp_main_t::vrrp4_arp_lookup
uword * vrrp4_arp_lookup
Definition: vrrp.h:164
vrrp_adv_send
int vrrp_adv_send(vrrp_vr_t *vr, int shutdown)
Definition: vrrp_packet.c:279
format_vrrp_packet_hdr
u8 * format_vrrp_packet_hdr(u8 *s, va_list *args)
Definition: vrrp_format.c:124
vrrp_vr_n_vr_addrs
static u8 vrrp_vr_n_vr_addrs(vrrp_vr_t *vr)
Definition: vrrp.h:330
vrrp_vr_config::peer_addrs
ip46_address_t * peer_addrs
Definition: vrrp.h:77
vrrp_vr_runtime::master_down_int
u16 master_down_int
Definition: vrrp.h:99
vrrp_vr_event
void vrrp_vr_event(vrrp_vr_t *vr, vrrp_vr_state_t new_state)
Definition: vrrp_api.c:507
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
format_vrrp_vr_key
u8 * format_vrrp_vr_key(u8 *s, va_list *args)
Definition: vrrp_format.c:65
vrrp_vr_t
struct vrrp_vr vrrp_vr_t
VRRP_VR_UNICAST
@ VRRP_VR_UNICAST
Definition: vrrp.h:25
mhash_t
Definition: mhash.h:46
vrrp_vr_is_owner
static u8 vrrp_vr_is_owner(vrrp_vr_t *vr)
Definition: vrrp.h:324
vrrp_vr_timer_t
struct vrrp_vr_timer vrrp_vr_timer_t
vrrp_vr_tracking_ifs_add_del
int vrrp_vr_tracking_ifs_add_del(vrrp_vr_t *vr, vrrp_vr_tracking_if_t *track_ifs, u8 is_add)
Definition: vrrp.c:998
vrrp_vr_skew_compute
static void vrrp_vr_skew_compute(vrrp_vr_t *vr)
Definition: vrrp.h:214
u16
unsigned short u16
Definition: types.h:57
vrrp_vr_config::flags
vrrp_vr_flags_t flags
Definition: vrrp.h:75
CLIB_PACKED
typedef CLIB_PACKED(struct vrrp4_arp_key { union { struct { u32 sw_if_index;ip4_address_t addr;};u64 as_u64;};}) vrrp4_arp_key_t
mhash_get
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
VRRP_VR_ACCEPT
@ VRRP_VR_ACCEPT
Definition: vrrp.h:24
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vrrp_main_t::vr_timers
vrrp_vr_timer_t * vr_timers
Definition: vrrp.h:154
vrrp_vr::runtime
vrrp_vr_runtime_t runtime
Definition: vrrp.h:109
vrrp_vr_is_ipv6
static u8 vrrp_vr_is_ipv6(vrrp_vr_t *vr)
Definition: vrrp.h:312
vrrp_main_t::vr_index_by_key
mhash_t vr_index_by_key
Definition: vrrp.h:161
vrrp_vr_master_down_compute
static void vrrp_vr_master_down_compute(vrrp_vr_t *vr)
Definition: vrrp.h:223
vrrp_vr_tracking_if
Definition: vrrp.h:57
vrrp_vr_config::priority
u8 priority
Definition: vrrp.h:73
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
vrrp_vr_tracking_if::priority
u8 priority
Definition: vrrp.h:60
vrrp_vr_is_unicast
static u8 vrrp_vr_is_unicast(vrrp_vr_t *vr)
Definition: vrrp.h:318
ethernet.h
key
typedef key
Definition: ipsec_types.api:91
vrrp_vr_tracking_t
struct vrrp_vr_tracking vrrp_vr_tracking_t
vrrp_vr_timer_set
void vrrp_vr_timer_set(vrrp_vr_t *vr, vrrp_vr_timer_type_t type)
Definition: vrrp_periodic.c:89
vr_id
u8 vr_id
Definition: vrrp.api:17
pool_is_free_index
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:302
vrrp_vr_n_peer_addrs
static u8 vrrp_vr_n_peer_addrs(vrrp_vr_t *vr)
Definition: vrrp.h:336
vrrp_vr_runtime::timer_index
u32 timer_index
Definition: vrrp.h:102
hash.h
vrrp_intf_t
Definition: vrrp.h:127
vrrp_vr_timer::expire_time
f64 expire_time
Definition: vrrp.h:123
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vrrp_vr_key_t
struct vrrp_vr_key vrrp_vr_key_t
len
u8 len
Definition: ip_types.api:103
error.h
vrrp_vr_transition
void vrrp_vr_transition(vrrp_vr_t *vr, vrrp_vr_state_t new_state, void *data)
Definition: vrrp.c:282
ethernet_main_t_
Definition: ethernet.h:288
vrrp_vr_tracking_if_add_del
int vrrp_vr_tracking_if_add_del(vrrp_vr_t *vr, u32 sw_if_index, u8 priority, u8 is_add)
Definition: vrrp.c:955
vrrp_vr_timer
Definition: vrrp.h:120
vrrp_main_t::vrrp6_nd_lookup
uword * vrrp6_nd_lookup
Definition: vrrp.h:165
vrrp_vr_config_t
struct vrrp_vr_config vrrp_vr_config_t
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
priority
i32 priority
Definition: ipsec.api:95
vrrp_vr_tracking
Definition: vrrp.h:63
hash_get
#define hash_get(h, key)
Definition: hash.h:249
vrrp_periodic_node
vlib_node_registration_t vrrp_periodic_node
(constructor) VLIB_REGISTER_NODE (vrrp_periodic_node)
Definition: vrrp_periodic.c:214
vrrp_vr_key::sw_if_index
u32 sw_if_index
Definition: vrrp.h:31
vrrp_vr_runtime::master_adv_int
u16 master_adv_int
Definition: vrrp.h:97
vrrp_vr_tracking_if_t
struct vrrp_vr_tracking_if vrrp_vr_tracking_if_t
uword
u64 uword
Definition: types.h:112
vrrp_vr_tracking_if::sw_if_index
u32 sw_if_index
Definition: vrrp.h:59
vrrp_vr_timer_cancel
void vrrp_vr_timer_cancel(vrrp_vr_t *vr)
Definition: vrrp_periodic.c:58
vrrp_vr_timer_type_t
enum vrrp_vr_timer_type vrrp_vr_timer_type_t
f64
double f64
Definition: types.h:142
vrrp_main_t::n_vrs_started
u16 n_vrs_started
Definition: vrrp.h:158
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
vrrp_vr_lookup_index
static vrrp_vr_t * vrrp_vr_lookup_index(u32 vr_index)
Definition: vrrp.h:251
vrrp_vr::tracking
vrrp_vr_tracking_t tracking
Definition: vrrp.h:110
vrrp_intf_num_vrs
static int vrrp_intf_num_vrs(u32 sw_if_index, u8 is_ipv6)
Definition: vrrp.h:301
vrrp_main_t
Definition: vrrp.h:145
vrrp_vr_accept_mode_enabled
static u8 vrrp_vr_accept_mode_enabled(vrrp_vr_t *vr)
Definition: vrrp.h:342
vrrp_plugin_api_hookup
clib_error_t * vrrp_plugin_api_hookup(vlib_main_t *vm)
Definition: vrrp_api.c:528
ip4_address_t
Definition: ip4_packet.h:50
VRRP_VR_IPV6
@ VRRP_VR_IPV6
Definition: vrrp.h:26
vrrp_vr_state_t
enum vrrp_vr_state vrrp_vr_state_t
vrrp_intf_t::vr_indices
u32 * vr_indices[2]
Definition: vrrp.h:131
format_vrrp_vr
u8 * format_vrrp_vr(u8 *s, va_list *args)
Definition: vrrp_format.c:92
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
vrrp_vr_lookup
static vrrp_vr_t * vrrp_vr_lookup(u32 sw_if_index, u8 vr_id, u8 is_ipv6)
Definition: vrrp.h:231
vrrp_vr_runtime::state
vrrp_vr_state_t state
Definition: vrrp.h:96
vrrp_vr_lookup_address
static u32 vrrp_vr_lookup_address(u32 sw_if_index, u8 is_ipv6, void *addr)
Definition: vrrp.h:262
vrrp_vr_runtime::mac
mac_address_t mac
Definition: vrrp.h:100
vrrp_vr_timer::vr_index
u32 vr_index
Definition: vrrp.h:122
vrrp_vr_runtime::last_sent
f64 last_sent
Definition: vrrp.h:101
data
u8 data[128]
Definition: ipsec_types.api:95
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
vnet_main_t
Definition: vnet.h:76
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
vrrp_vr_config::vr_id
u8 vr_id
Definition: vrrp.h:72
vrrp_vr_key
Definition: vrrp.h:29
u64
unsigned long u64
Definition: types.h:89
vrrp_vr_index
static u32 vrrp_vr_index(vrrp_vr_t *vr)
Definition: vrrp.h:348
vrrp_adv_csum
u16 vrrp_adv_csum(void *l3_hdr, void *payload, u8 is_ipv6, u16 len)
Definition: vrrp_packet.c:150
vrrp_vr_key::is_ipv6
u8 is_ipv6
Definition: vrrp.h:33
ip.h
u32
unsigned int u32
Definition: types.h:88
vrrp_main_t::pending_timers
u32 * pending_timers
Definition: vrrp.h:155
vrrp_vr_config::adv_interval
u16 adv_interval
Definition: vrrp.h:74
as_u64
u64 as_u64
Definition: bihash_doc.h:63
vrrp_main_t::msg_id_base
u16 msg_id_base
Definition: vrrp.h:148
format_vrrp_vr_state
u8 * format_vrrp_vr_state(u8 *s, va_list *args)
Definition: vrrp_format.c:47
vrrp_main_t::vlib_main
vlib_main_t * vlib_main
Definition: vrrp.h:171
mac_address_t_
Definition: mac_address.h:21
vrrp_node
vlib_node_registration_t vrrp_node
adj_index_t
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vrrp_vr_multicast_group_join
int vrrp_vr_multicast_group_join(vrrp_vr_t *vr)
Definition: vrrp_packet.c:690
vlib_main_t
Definition: main.h:102
vrrp_vr_runtime
Definition: vrrp.h:94
vrrp_vr_tracking::interfaces_dec
u32 interfaces_dec
Definition: vrrp.h:66
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vrrp_main
vrrp_main_t vrrp_main
Definition: vrrp.c:25
VRRP_VR_TIMER_MASTER_DOWN
@ VRRP_VR_TIMER_MASTER_DOWN
Definition: vrrp.h:117
vrrp_vr_runtime::skew
u16 skew
Definition: vrrp.h:98
vrrp_vr_flags_t
enum vrrp_vr_flags vrrp_vr_flags_t
vrrp_garp_or_na_send
int vrrp_garp_or_na_send(vrrp_vr_t *vr)
Definition: vrrp_packet.c:465
vrrp_main_t::vrs
vrrp_vr_t * vrs
Definition: vrrp.h:151
vrrp_vr_set_peers
int vrrp_vr_set_peers(vrrp_vr_key_t *key, ip46_address_t *peers)
Definition: vrrp.c:803
rv
int __clib_unused rv
Definition: application.c:491
vrrp_vr_state
vrrp_vr_state
Definition: vrrp.api:74
vrrp_vr_config::sw_if_index
u32 sw_if_index
Definition: vrrp.h:71
vrrp_main_t::intf_output_node_idx
u32 intf_output_node_idx
Definition: vrrp.h:175
vnet.h
vrrp_main_t::ethernet_main
ethernet_main_t * ethernet_main
Definition: vrrp.h:173
vrrp_vr_key::vr_id
u8 vr_id
Definition: vrrp.h:32
vrrp_vr_tracking::interfaces
vrrp_vr_tracking_if_t * interfaces
Definition: vrrp.h:65
vrrp_vr_timer::type
vrrp_vr_timer_type_t type
Definition: vrrp.h:124
foreach_vrrp_vr_state
#define foreach_vrrp_vr_state
Definition: vrrp.h:80
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
vrrp_main_t::vnet_main
vnet_main_t * vnet_main
Definition: vrrp.h:172
vrrp_vr_priority
static u8 vrrp_vr_priority(vrrp_vr_t *vr)
Definition: vrrp.h:356
vrrp_vr_add_del
int vrrp_vr_add_del(u8 is_add, vrrp_vr_config_t *conf)
Definition: vrrp.c:622
vrrp_intf_get
static vrrp_intf_t * vrrp_intf_get(u32 sw_if_index)
Definition: vrrp.h:289
vrrp_main_t::vrrp_intfs
vrrp_intf_t * vrrp_intfs
Definition: vrrp.h:168
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
VRRP_VR_TIMER_ADV
@ VRRP_VR_TIMER_ADV
Definition: vrrp.h:116
VRRP_VR_PREEMPT
@ VRRP_VR_PREEMPT
Definition: vrrp.h:23
vrrp_vr_timer_type
vrrp_vr_timer_type
Definition: vrrp.h:114