]> git.proxmox.com Git - mirror_frr.git/blame - vrrpd/vrrp.h
Merge pull request #4877 from mjstapp/dplane_neighs
[mirror_frr.git] / vrrpd / vrrp.h
CommitLineData
5435a2bf 1/*
63d4bd12
QY
2 * VRRP global definitions and state machine.
3 * Copyright (C) 2018-2019 Cumulus Networks, Inc.
4 * Quentin Young
5435a2bf
QY
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
63d4bd12
QY
20#ifndef __VRRP_H__
21#define __VRRP_H__
5435a2bf
QY
22
23#include <zebra.h>
91188ca6 24#include <netinet/ip.h>
1d21789e 25
7c136b08 26#include "lib/memory.h"
1d21789e
QY
27#include "lib/hash.h"
28#include "lib/hook.h"
29#include "lib/if.h"
30#include "lib/linklist.h"
31#include "lib/privs.h"
91188ca6 32#include "lib/stream.h"
1d21789e 33#include "lib/thread.h"
f828842a 34#include "lib/vty.h"
5435a2bf
QY
35
36/* Global definitions */
f3fe0047 37#define VRRP_RADV_INT 16
5435a2bf 38#define VRRP_PRIO_MASTER 255
862f2f37
QY
39#define VRRP_MCASTV4_GROUP_STR "224.0.0.18"
40#define VRRP_MCASTV6_GROUP_STR "ff02:0:0:0:0:0:0:12"
41#define VRRP_MCASTV4_GROUP 0xe0000012
42#define VRRP_MCASTV6_GROUP 0xff020000000000000000000000000012
5435a2bf
QY
43#define IPPROTO_VRRP 112
44
613b45b0
QY
45#define VRRP_LOGPFX_VRID "[VRID %u] "
46#define VRRP_LOGPFX_FAM "[%s] "
4ec94408 47
8cd1d277
QY
48/* Default defaults */
49#define VRRP_DEFAULT_PRIORITY 100
50#define VRRP_DEFAULT_ADVINT 100
51#define VRRP_DEFAULT_PREEMPT true
52#define VRRP_DEFAULT_ACCEPT true
53#define VRRP_DEFAULT_SHUTDOWN false
54
cb44d476
QY
55/* User compatibility constant */
56#define CS2MS 10
57
7c136b08
DL
58DECLARE_MGROUP(VRRPD)
59
8cd1d277
QY
60/* Configured defaults */
61struct vrrp_defaults {
62 uint8_t priority;
63 uint16_t advertisement_interval;
64 bool preempt_mode;
65 bool accept_mode;
66 bool shutdown;
67};
68
69extern struct vrrp_defaults vd;
70
5435a2bf
QY
71/* threadmaster */
72extern struct thread_master *master;
73
b6029d6a
QY
74/* privileges */
75extern struct zebra_privs_t vrrp_privs;
76
5435a2bf 77/* Global hash of all Virtual Routers */
27fd8827 78extern struct hash *vrrp_vrouters_hash;
5435a2bf 79
27fd8827
QY
80/*
81 * VRRP Router.
82 *
83 * This struct contains all state for a particular VRRP Router operating
84 * in a Virtual Router for either IPv4 or IPv6.
85 */
86struct vrrp_router {
862f2f37
QY
87 /*
88 * Whether this VRRP Router is active.
89 */
90 bool is_active;
91
10133a59
QY
92 /* Whether we are the address owner */
93 bool is_owner;
94
dad18a2f
QY
95 /* Rx socket: Rx from parent of mvl_ifp */
96 int sock_rx;
97 /* Tx socket; Tx from mvl_ifp */
98 int sock_tx;
99
100 /* macvlan interface */
101 struct interface *mvl_ifp;
5435a2bf 102
8071d5c3
QY
103 /* Source address for advertisements */
104 struct ipaddr src;
105
91188ca6
QY
106 /* Socket read buffer */
107 uint8_t ibuf[IP_MAXPACKET];
108
862f2f37
QY
109 /*
110 * Address family of this Virtual Router.
111 * Either AF_INET or AF_INET6.
112 */
113 int family;
5435a2bf
QY
114
115 /*
862f2f37 116 * Virtual Router this VRRP Router is participating in.
5435a2bf 117 */
862f2f37 118 struct vrrp_vrouter *vr;
5435a2bf 119
862f2f37
QY
120 /*
121 * One or more IPvX addresses associated with this Virtual
122 * Router. The first address must be the "primary" address this
123 * Virtual Router is backing up in the case of IPv4. In the case of
124 * IPv6 it must be the link-local address of vr->ifp.
125 *
126 * Type: struct ipaddr *
127 */
128 struct list *addrs;
5435a2bf 129
d60b2ffd
QY
130 /*
131 * This flag says whether we are waiting on an interface up
132 * notification from Zebra before we send an ADVERTISEMENT.
133 */
134 bool advert_pending;
135
136 /*
137 * If this is an IPv4 VRRP router, this flag says whether we are
138 * waiting on an interface up notification from Zebra before we send
139 * gratuitous ARP packets for all our addresses. Should never be true
140 * if family == AF_INET6.
141 */
142 bool garp_pending;
143 /*
144 * If this is an IPv6 VRRP router, this flag says whether we are
145 * waiting on an interface up notification from Zebra before we send
146 * Unsolicited Neighbor Advertisement packets for all our addresses.
147 * Should never be true if family == AF_INET.
148 */
149 bool ndisc_pending;
150
5d3730c5
QY
151 /*
152 * Effective priority
862f2f37 153 * => vr->priority if we are Backup
5d3730c5
QY
154 * => 255 if we are Master
155 */
5435a2bf
QY
156 uint8_t priority;
157
5435a2bf
QY
158 /*
159 * Advertisement interval contained in ADVERTISEMENTS received from the
160 * Master (centiseconds)
161 */
162 uint16_t master_adver_interval;
163
164 /*
165 * Time to skew Master_Down_Interval in centiseconds. Calculated as:
166 * (((256 - priority) * Master_Adver_Interval) / 256)
167 */
168 uint16_t skew_time;
169
170 /*
171 * Time interval for Backup to declare Master down (centiseconds).
172 * Calculated as:
173 * (3 * Master_Adver_Interval) + Skew_time
174 */
175 uint16_t master_down_interval;
176
862f2f37
QY
177 /*
178 * The MAC address used for the source MAC address in VRRP
179 * advertisements, advertised in ARP requests/responses, and advertised
180 * in ND Neighbor Advertisements.
181 */
182 struct ethaddr vmac;
183
184 struct {
185 int state;
186 } fsm;
187
6332c77f
QY
188 struct {
189 /* Total number of advertisements sent and received */
190 uint32_t adver_tx_cnt;
191 uint32_t adver_rx_cnt;
192 /* Total number of gratuitous ARPs sent */
193 uint32_t garp_tx_cnt;
194 /* Total number of unsolicited Neighbor Advertisements sent */
195 uint32_t una_tx_cnt;
196 /* Total number of state transitions */
197 uint32_t trans_cnt;
198 } stats;
199
862f2f37
QY
200 struct thread *t_master_down_timer;
201 struct thread *t_adver_timer;
91188ca6
QY
202 struct thread *t_read;
203 struct thread *t_write;
862f2f37
QY
204};
205
206/*
207 * VRRP Virtual Router.
208 *
209 * This struct contains all state and configuration for a given Virtual Router
210 * Identifier on a given interface, both v4 and v6.
211 *
212 * RFC5798 s. 1 states:
213 * "Within a VRRP router, the virtual routers in each of the IPv4 and IPv6
214 * address families are a domain unto themselves and do not overlap."
215 *
216 * This implementation has chosen the tuple (interface, VRID) as the key for a
217 * particular VRRP Router, and the rest of the program is designed around this
218 * assumption. Additionally, base protocol configuration parameters such as the
219 * advertisement interval and (configured) priority are shared between v4 and
220 * v6 instances. This corresponds to the choice made by other industrial
221 * implementations.
222 */
223struct vrrp_vrouter {
53e60e5c
QY
224 /* Whether this instance was automatically configured */
225 bool autoconf;
226
6e93585e
QY
227 /* Whether this VRRP router is in administrative shutdown */
228 bool shutdown;
229
862f2f37
QY
230 /* Interface */
231 struct interface *ifp;
232
91188ca6
QY
233 /* Version */
234 uint8_t version;
235
862f2f37
QY
236 /* Virtual Router Identifier */
237 uint32_t vrid;
238
239 /* Configured priority */
240 uint8_t priority;
241
242 /*
243 * Time interval between ADVERTISEMENTS (centiseconds). Default is 100
244 * centiseconds (1 second).
245 */
246 uint16_t advertisement_interval;
247
5435a2bf
QY
248 /*
249 * Controls whether a (starting or restarting) higher-priority Backup
250 * router preempts a lower-priority Master router. Values are True to
251 * allow preemption and False to prohibit preemption. Default is True.
252 */
253 bool preempt_mode;
254
255 /*
256 * Controls whether a virtual router in Master state will accept
257 * packets addressed to the address owner's IPvX address as its own if
258 * it is not the IPvX address owner. The default is False.
259 */
260 bool accept_mode;
261
862f2f37
QY
262 struct vrrp_router *v4;
263 struct vrrp_router *v6;
5435a2bf
QY
264};
265
5435a2bf
QY
266/*
267 * Initialize VRRP global datastructures.
268 */
269void vrrp_init(void);
270
f1175ba9
QY
271/*
272 * Destroy all VRRP instances and gracefully shutdown.
273 *
274 * For instances in Master state, VRRP advertisements with 0 priority will be
275 * sent if possible to notify Backup routers that we are going away.
276 */
277void vrrp_fini(void);
278
1d21789e
QY
279
280/* Creation and destruction ------------------------------------------------ */
281
5435a2bf
QY
282/*
283 * Create and register a new VRRP Virtual Router.
1d21789e
QY
284 *
285 * ifp
286 * Base interface to configure VRRP on
287 *
288 * vrid
289 * Virtual Router Identifier
5435a2bf 290 */
99966840
QY
291struct vrrp_vrouter *vrrp_vrouter_create(struct interface *ifp, uint8_t vrid,
292 uint8_t version);
5435a2bf 293
c23edd74 294/*
6287cefe
QY
295 * Destroy a VRRP Virtual Router, freeing all its resources.
296 *
297 * If there are any running VRRP instances, these are stopped and destroyed.
c23edd74
QY
298 */
299void vrrp_vrouter_destroy(struct vrrp_vrouter *vr);
300
1d21789e
QY
301
302/* Configuration controllers ----------------------------------------------- */
303
6e93585e
QY
304/*
305 * Check if a Virtual Router ought to be started, and if so, start it.
306 *
307 * vr
308 * Virtual Router to checkstart
309 */
310void vrrp_check_start(struct vrrp_vrouter *vr);
311
c23edd74 312/*
5d3730c5 313 * Change the configured priority of a VRRP Virtual Router.
c23edd74 314 *
5d3730c5
QY
315 * Note that this only changes the configured priority of the Virtual Router.
316 * The currently effective priority will not be changed; to change the
317 * effective priority, the Virtual Router must be restarted by issuing a
318 * VRRP_EVENT_SHUTDOWN followed by a VRRP_EVENT_STARTUP.
c23edd74 319 *
1d21789e
QY
320 * vr
321 * Virtual Router to change priority of
c23edd74 322 *
1d21789e
QY
323 * priority
324 * New priority
c23edd74 325 */
1d21789e 326void vrrp_set_priority(struct vrrp_vrouter *vr, uint8_t priority);
c23edd74
QY
327
328/*
1d21789e 329 * Set Advertisement Interval on this Virtual Router.
c23edd74
QY
330 *
331 * vr
332 * Virtual Router to change priority of
333 *
1d21789e
QY
334 * advertisement_interval
335 * New advertisement interval
c23edd74 336 */
1d21789e
QY
337void vrrp_set_advertisement_interval(struct vrrp_vrouter *vr,
338 uint16_t advertisement_interval);
c23edd74
QY
339
340/*
862f2f37
QY
341 * Add an IPvX address to a VRRP Virtual Router.
342 *
2cd90902 343 * r
862f2f37
QY
344 * Virtual Router to add IPvx address to
345 *
346 * ip
347 * Address to add
2cd90902
QY
348 *
349 * activate
350 * Whether to automatically start the VRRP router if this is the first IP
351 * address added.
352 *
353 * Returns:
354 * -1 on error
355 * 0 otherwise
862f2f37 356 */
6e93585e 357int vrrp_add_ip(struct vrrp_router *r, struct ipaddr *ip);
862f2f37
QY
358
359/*
360 * Add an IPv4 address to a VRRP Virtual Router.
c23edd74
QY
361 *
362 * vr
363 * Virtual Router to add IPv4 address to
364 *
365 * v4
366 * Address to add
2cd90902
QY
367 *
368 * activate
369 * Whether to automatically start the VRRP router if this is the first IP
370 * address added.
371 *
372 * Returns:
373 * -1 on error
374 * 0 otherwise
c23edd74 375 */
6e93585e 376int vrrp_add_ipv4(struct vrrp_vrouter *vr, struct in_addr v4);
862f2f37
QY
377
378/*
379 * Add an IPv6 address to a VRRP Virtual Router.
380 *
381 * vr
382 * Virtual Router to add IPv6 address to
383 *
384 * v6
385 * Address to add
2cd90902
QY
386 *
387 * activate
388 * Whether to automatically start the VRRP router if this is the first IP
389 * address added.
390 *
391 * Returns:
392 * -1 on error
393 * 0 otherwise
394 */
6e93585e 395int vrrp_add_ipv6(struct vrrp_vrouter *vr, struct in6_addr v6);
2cd90902
QY
396
397/*
398 * Remove an IP address from a VRRP Virtual Router.
399 *
400 * r
401 * Virtual Router to remove IP address from
402 *
403 * ip
404 * Address to remove
405 *
406 * deactivate
407 * Whether to automatically stop the VRRP router if removing v4 would leave
408 * us with an empty address list. If this is not true and ip is the only IP
409 * address backed up by this virtual router, this function will not remove
410 * the address and return failure.
411 *
412 * Returns:
413 * -1 on error
414 * 0 otherwise
415 */
6e93585e 416int vrrp_del_ip(struct vrrp_router *r, struct ipaddr *ip);
2cd90902
QY
417
418/*
419 * Remove an IPv4 address from a VRRP Virtual Router.
420 *
421 * vr
422 * Virtual Router to remove IPv4 address from
423 *
424 * v4
425 * Address to remove
426 *
427 * deactivate
428 * Whether to automatically stop the VRRP router if removing v4 would leave
429 * us with an empty address list. If this is not true and v4 is the only
430 * IPv4 address backed up by this virtual router, this function will not
431 * remove the address and return failure.
432 *
433 * Returns:
434 * -1 on error
435 * 0 otherwise
862f2f37 436 */
6e93585e 437int vrrp_del_ipv4(struct vrrp_vrouter *vr, struct in_addr v4);
c23edd74 438
2cd90902
QY
439/*
440 * Remove an IPv6 address from a VRRP Virtual Router.
441 *
442 * vr
443 * Virtual Router to remove IPv6 address from
444 *
445 * v6
446 * Address to remove
447 *
448 * deactivate
449 * Whether to automatically stop the VRRP router if removing v5 would leave
450 * us with an empty address list. If this is not true and v4 is the only
451 * IPv6 address backed up by this virtual router, this function will not
452 * remove the address and return failure.
453 *
454 * Returns:
455 * -1 on error
456 * 0 otherwise
457 */
6e93585e 458int vrrp_del_ipv6(struct vrrp_vrouter *vr, struct in6_addr v6);
1d21789e
QY
459
460/* State machine ----------------------------------------------------------- */
461
4ec94408
QY
462#define VRRP_STATE_INITIALIZE 0
463#define VRRP_STATE_MASTER 1
464#define VRRP_STATE_BACKUP 2
465#define VRRP_EVENT_STARTUP 0
466#define VRRP_EVENT_SHUTDOWN 1
467
468extern const char *vrrp_state_names[3];
469extern const char *vrrp_event_names[2];
1d21789e 470
5435a2bf 471/*
1d21789e
QY
472 * This hook called whenever the state of a Virtual Router changes, after the
473 * specific internal state handlers have run.
474 *
475 * Use this if you need to react to state changes to perform non-critical
476 * tasks. Critical tasks should go in the internal state change handlers.
5435a2bf 477 */
2fff50ec 478DECLARE_HOOK(vrrp_change_state_hook, (struct vrrp_router *r, int to), (r, to));
5435a2bf
QY
479
480/*
1d21789e
QY
481 * Trigger a VRRP event on a given Virtual Router..
482 *
483 * vr
484 * Virtual Router to operate on
485 *
486 * event
487 * Event to kick off. All event related processing will have completed upon
488 * return of this function.
489 *
490 * Returns:
491 * < 0 if the event created an error
492 * 0 otherwise
5435a2bf 493 */
862f2f37 494int vrrp_event(struct vrrp_router *r, int event);
5435a2bf 495
27fd8827 496/* Autoconfig -------------------------------------------------------------- */
1d21789e 497
53e60e5c
QY
498/*
499 * Search for and automatically configure VRRP instances on interfaces.
500 *
501 * ifp
502 * Interface to autoconfig. If it is a macvlan interface and has a VRRP MAC,
503 * a VRRP instance corresponding to VMAC assigned to macvlan will be created
504 * on the parent interface and all addresses on the macvlan interface except
505 * the v6 link local will be configured as VRRP addresses. If NULL, this
506 * treatment will be applied to all existing interfaces matching the above
507 * criterion.
508 *
509 * Returns:
510 * -1 on failure
511 * 0 otherwise
512 */
27fd8827
QY
513int vrrp_autoconfig(void);
514
515/*
516 * Enable autoconfiguration.
517 *
518 * Calling this function will cause vrrpd to automatically configure VRRP
519 * instances on existing compatible macvlan interfaces. These instances will
520 * react to interface up/down and address add/delete events to keep themselves
521 * in sync with the available interfaces.
522 *
523 * version
524 * VRRP version to use for autoconfigured instances. Must be 2 or 3.
525 */
526void vrrp_autoconfig_on(int version);
527
528/*
529 * Disable autoconfiguration.
530 *
531 * Calling this function will delete all existing autoconfigured VRRP instances.
532 */
533void vrrp_autoconfig_off(void);
534
6e93585e 535/* Interface Tracking ------------------------------------------------------ */
27fd8827 536
6e93585e
QY
537void vrrp_if_add(struct interface *ifp);
538void vrrp_if_del(struct interface *ifp);
539void vrrp_if_up(struct interface *ifp);
540void vrrp_if_down(struct interface *ifp);
541void vrrp_if_address_add(struct interface *ifp);
542void vrrp_if_address_del(struct interface *ifp);
27fd8827
QY
543
544/* Other ------------------------------------------------------------------- */
53e60e5c 545
f828842a
QY
546/*
547 * Write interface block-level configuration to vty.
548 *
549 * vty
550 * vty to write config to
551 *
552 * Returns:
553 * # of lines written
554 */
555int vrrp_config_write_interface(struct vty *vty);
556
557/*
558 * Write global level configuration to vty.
559 *
560 * vty
561 * vty to write config to
562 *
563 * Returns:
564 * # of lines written
565 */
566int vrrp_config_write_global(struct vty *vty);
567
1d21789e
QY
568/*
569 * Find VRRP Virtual Router by Virtual Router ID
570 */
4f0b6b45 571struct vrrp_vrouter *vrrp_lookup(struct interface *ifp, uint8_t vrid);
1d21789e 572
63d4bd12 573#endif /* __VRRP_H__ */