vrrp: fix thread synchronization issue
[vpp.git] / src / plugins / vrrp / vrrp.c
1 /*
2  * vrrp.c - vrrp plugin action functions
3  *
4  * Copyright 2019-2020 Rubicon Communications, LLC (Netgate)
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  */
9
10 #include <vnet/vnet.h>
11 #include <vnet/plugin/plugin.h>
12 #include <vnet/mfib/mfib_entry.h>
13 #include <vnet/mfib/mfib_table.h>
14 #include <vnet/adj/adj.h>
15 #include <vnet/adj/adj_mcast.h>
16 #include <vnet/fib/fib_table.h>
17 #include <vnet/ip/igmp_packet.h>
18 #include <vnet/ip/ip6_link.h>
19
20 #include <vrrp/vrrp.h>
21 #include <vrrp/vrrp_packet.h>
22
23 #include <vpp/app/version.h>
24
25 vrrp_main_t vrrp_main;
26
27 static const mac_address_t ipv4_vmac = {
28   .bytes = {0x00, 0x00, 0x5e, 0x00, 0x01, 0x00}
29 };
30
31 static const mac_address_t ipv6_vmac = {
32   .bytes = {0x00, 0x00, 0x5e, 0x00, 0x02, 0x00}
33 };
34
35 typedef struct
36 {
37   vrrp_vr_key_t key;
38   u32 count;
39 } vrrp_hwif_vr_count_t;
40
41 typedef enum
42 {
43   VRRP_IF_UPDATE_IP,
44   VRRP_IF_UPDATE_HW_LINK,
45   VRRP_IF_UPDATE_SW_ADMIN,
46 } vrrp_intf_update_type_t;
47
48 typedef struct
49 {
50   vrrp_intf_update_type_t type;
51   u32 sw_if_index;
52   u32 hw_if_index;
53   int intf_up;
54 } vrrp_intf_update_t;
55
56 static int vrrp_intf_is_up (u32 sw_if_index, u8 is_ipv6,
57                             vrrp_intf_update_t * pending);
58
59 static walk_rc_t
60 vrrp_hwif_master_count_walk (vnet_main_t * vnm, u32 sw_if_index, void *arg)
61 {
62   vrrp_hwif_vr_count_t *vr_count = arg;
63   vrrp_vr_t *vr;
64
65   vr = vrrp_vr_lookup (sw_if_index, vr_count->key.vr_id,
66                        vr_count->key.is_ipv6);
67
68   if (vr && (vr->runtime.state == VRRP_VR_STATE_MASTER))
69     vr_count->count++;
70
71   return WALK_CONTINUE;
72 }
73
74 /*
75  * Get a count of VRs in master state on a given hardware interface with
76  * the provided VR ID and AF.
77  */
78 static u32
79 vrrp_vr_hwif_master_vrs_by_vrid (u32 hw_if_index, u8 vr_id, u8 is_ipv6)
80 {
81   vnet_main_t *vnm = vnet_get_main ();
82   vrrp_hwif_vr_count_t vr_count;
83
84   clib_memset (&vr_count, 0, sizeof (vr_count));
85
86   vr_count.key.vr_id = vr_id;
87   vr_count.key.is_ipv6 = is_ipv6;
88
89   vnet_hw_interface_walk_sw (vnm, hw_if_index,
90                              vrrp_hwif_master_count_walk, &vr_count);
91
92   return vr_count.count;
93 }
94
95 /*
96  * Add or delete the VR virtual MAC address on the hardware interface
97  * when a VR enters or leaves the master state.
98  *
99  * Multiple subinterfaces may host the same VR ID. We should only add or
100  * delete the virtual MAC if this is the first VR being enabled on the
101  * hardware interface or the last one being disabled, respectively.
102  */
103 void
104 vrrp_vr_transition_vmac (vrrp_vr_t * vr, vrrp_vr_state_t new_state)
105 {
106   vnet_main_t *vnm = vnet_get_main ();
107   clib_error_t *error = 0;
108   vnet_hw_interface_t *hw;
109   u8 enable = (new_state == VRRP_VR_STATE_MASTER);
110   u32 n_master_vrs;
111
112   hw = vnet_get_sup_hw_interface (vnm, vr->config.sw_if_index);
113   n_master_vrs =
114     vrrp_vr_hwif_master_vrs_by_vrid (hw->hw_if_index, vr->config.vr_id,
115                                      vrrp_vr_is_ipv6 (vr));
116
117   /* enable only if current master vrs is 0, disable only if 0 or 1 */
118   if ((enable && !n_master_vrs) || (!enable && (n_master_vrs < 2)))
119     {
120       clib_warning ("%s virtual MAC address %U on hardware interface %u",
121                     (enable) ? "Adding" : "Deleting",
122                     format_ethernet_address, vr->runtime.mac.bytes,
123                     hw->hw_if_index);
124
125       error = vnet_hw_interface_add_del_mac_address
126         (vnm, hw->hw_if_index, vr->runtime.mac.bytes, enable);
127     }
128
129   if (error)
130     clib_error_report (error);
131 }
132
133 /*
134  * Manage VR interface data on transition to/from master:
135  *  - enable or disable ARP/ND input feature if appropriate
136  *  - update count of VRs in master state
137  */
138 static void
139 vrrp_vr_transition_intf (vrrp_vr_t * vr, vrrp_vr_state_t new_state)
140 {
141   vrrp_intf_t *intf;
142   const char *arc_name = 0, *node_name = 0;
143   const char *mc_arc_name = 0, *mc_node_name = 0;
144   u8 is_ipv6 = vrrp_vr_is_ipv6 (vr);
145   u32 *vr_index;
146   int n_master_accept = 0;
147   int n_started = 0;
148
149   if (is_ipv6)
150     {
151       arc_name = "ip6-local";
152       node_name = "vrrp6-nd-input";
153       mc_arc_name = "ip6-multicast";
154       mc_node_name = "vrrp6-accept-owner-input";
155     }
156   else
157     {
158       arc_name = "arp";
159       node_name = "vrrp4-arp-input";
160       mc_arc_name = "ip4-multicast";
161       mc_node_name = "vrrp4-accept-owner-input";
162     }
163
164   intf = vrrp_intf_get (vr->config.sw_if_index);
165
166   /* Check other VRs on this intf to see if features need to be toggled */
167   vec_foreach (vr_index, intf->vr_indices[is_ipv6])
168   {
169     vrrp_vr_t *intf_vr = vrrp_vr_lookup_index (*vr_index);
170
171     if (intf_vr == vr)
172       continue;
173
174     if (intf_vr->runtime.state == VRRP_VR_STATE_INIT)
175       continue;
176
177     n_started++;
178
179     if ((intf_vr->runtime.state == VRRP_VR_STATE_MASTER) &&
180         vrrp_vr_accept_mode_enabled (intf_vr))
181       n_master_accept++;
182   }
183
184   /* If entering/leaving init state, start/stop ARP or ND feature if no other
185    * VRs are active on the interface.
186    */
187   if (((vr->runtime.state == VRRP_VR_STATE_INIT) ||
188        (new_state == VRRP_VR_STATE_INIT)) && (n_started == 0))
189     vnet_feature_enable_disable (arc_name, node_name,
190                                  vr->config.sw_if_index,
191                                  (new_state != VRRP_VR_STATE_INIT), NULL, 0);
192
193   /* Special housekeeping when entering/leaving master mode */
194   if ((vr->runtime.state == VRRP_VR_STATE_MASTER) ||
195       (new_state == VRRP_VR_STATE_MASTER))
196     {
197       /* Maintain count of master state VRs on interface */
198       if (new_state == VRRP_VR_STATE_MASTER)
199         intf->n_master_vrs[is_ipv6]++;
200       else if (intf->n_master_vrs[is_ipv6] > 0)
201         intf->n_master_vrs[is_ipv6]--;
202
203       /* If accept mode is enabled and no other master on intf has accept
204        * mode enabled, enable/disable feature node to avoid spurious drops by
205        * spoofing check.
206        */
207       if (vrrp_vr_accept_mode_enabled (vr) && !n_master_accept)
208         vnet_feature_enable_disable (mc_arc_name, mc_node_name,
209                                      vr->config.sw_if_index,
210                                      (new_state == VRRP_VR_STATE_MASTER),
211                                      NULL, 0);
212     }
213 }
214
215 /* If accept mode enabled, add/remove VR addresses from interface */
216 static void
217 vrrp_vr_transition_addrs (vrrp_vr_t * vr, vrrp_vr_state_t new_state)
218 {
219   vlib_main_t *vm = vlib_get_main ();
220   u8 is_del;
221   ip46_address_t *vr_addr;
222
223   if (!vrrp_vr_accept_mode_enabled (vr))
224     return;
225
226   /* owner always has VR addresses configured, should never remove them */
227   if (vrrp_vr_is_owner (vr))
228     return;
229
230   /* only need to do something if entering or leaving master state */
231   if ((vr->runtime.state != VRRP_VR_STATE_MASTER) &&
232       (new_state != VRRP_VR_STATE_MASTER))
233     return;
234
235   is_del = (new_state != VRRP_VR_STATE_MASTER);
236
237   clib_warning ("%s VR addresses on sw_if_index %u",
238                 (is_del) ? "Deleting" : "Adding", vr->config.sw_if_index);
239
240   vec_foreach (vr_addr, vr->config.vr_addrs)
241   {
242     ip_interface_address_t *ia = NULL;
243
244     /* We need to know the address length to use, find it from another
245      * address on the interface. Or use a default (/24, /64).
246      */
247     if (!vrrp_vr_is_ipv6 (vr))
248       {
249         ip4_main_t *im = &ip4_main;
250         ip4_address_t *intf4;
251
252         intf4 =
253           ip4_interface_address_matching_destination
254           (im, &vr_addr->ip4, vr->config.sw_if_index, &ia);
255
256         ip4_add_del_interface_address (vm, vr->config.sw_if_index,
257                                        &vr_addr->ip4,
258                                        (intf4 ? ia->address_length : 24),
259                                        is_del);
260       }
261     else
262       {
263         ip6_main_t *im = &ip6_main;
264         ip6_address_t *intf6;
265
266         intf6 =
267           ip6_interface_address_matching_destination
268           (im, &vr_addr->ip6, vr->config.sw_if_index, &ia);
269
270         ip6_add_del_interface_address (vm, vr->config.sw_if_index,
271                                        &vr_addr->ip6,
272                                        (intf6 ? ia->address_length : 64),
273                                        is_del);
274       }
275   }
276 }
277
278 void
279 vrrp_vr_transition (vrrp_vr_t * vr, vrrp_vr_state_t new_state, void *data)
280 {
281
282   clib_warning ("VR %U transitioning to %U", format_vrrp_vr_key, vr,
283                 format_vrrp_vr_state, new_state);
284
285   /* Don't do anything if transitioning to the state VR is already in.
286    * This should never happen, just covering our bases.
287    */
288   if (new_state == vr->runtime.state)
289     return;
290
291   if (new_state == VRRP_VR_STATE_MASTER)
292     {
293       /* RFC 5798 sec 6.4.1 (105) - startup event for VR with priority 255
294        *          sec 6.4.2 (365) - master down timer fires on backup VR
295        */
296
297       vrrp_vr_multicast_group_join (vr);
298       vrrp_adv_send (vr, 0);
299       vrrp_garp_or_na_send (vr);
300
301       vrrp_vr_timer_set (vr, VRRP_VR_TIMER_ADV);
302     }
303   else if (new_state == VRRP_VR_STATE_BACKUP)
304     {
305       /* RFC 5798 sec 6.4.1 (150) - startup event for VR with priority < 255
306        *          sec 6.4.3 (735) - master preempted by higher priority VR
307        */
308
309       vrrp_vr_multicast_group_join (vr);
310
311       if (vr->runtime.state == VRRP_VR_STATE_MASTER)
312         {
313           vrrp_input_process_args_t *args = data;
314
315           if (args)
316             vr->runtime.master_adv_int = args->max_adv_int;
317         }
318       else                      /* INIT, INTF_DOWN */
319         vr->runtime.master_adv_int = vr->config.adv_interval;
320
321       vrrp_vr_skew_compute (vr);
322       vrrp_vr_master_down_compute (vr);
323       vrrp_vr_timer_set (vr, VRRP_VR_TIMER_MASTER_DOWN);
324
325     }
326   else if (new_state == VRRP_VR_STATE_INIT)
327     {
328       /* RFC 5798 sec 6.4.2 (345) - shutdown event for backup VR
329        *          sec 6.4.3 (655) - shutdown event for master VR
330        */
331
332       vrrp_vr_timer_cancel (vr);
333       if (vr->runtime.state == VRRP_VR_STATE_MASTER)
334         vrrp_adv_send (vr, 1);
335     }
336   else if (new_state == VRRP_VR_STATE_INTF_DOWN)
337     /* State is not specified by RFC. This is to avoid attempting to
338      * send packets on an interface that's down and to avoid having a
339      * VR believe it is already the master when an interface is brought up
340      */
341     vrrp_vr_timer_cancel (vr);
342
343   /* add/delete virtual IP addrs if accept_mode is true */
344   vrrp_vr_transition_addrs (vr, new_state);
345
346   /* enable/disable input features if necessary */
347   vrrp_vr_transition_intf (vr, new_state);
348
349   /* add/delete virtual MAC address on NIC if necessary */
350   vrrp_vr_transition_vmac (vr, new_state);
351
352   vrrp_vr_event (vr, new_state);
353
354   vr->runtime.state = new_state;
355 }
356
357 #define VRRP4_MCAST_ADDR_AS_U8 { 224, 0, 0, 18 }
358 #define VRRP6_MCAST_ADDR_AS_U8 \
359 { 0xff, 0x2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12 }
360
361 static const mfib_prefix_t all_vrrp4_routers = {
362   .fp_proto = FIB_PROTOCOL_IP4,
363   .fp_len = 32,
364   .fp_grp_addr = {
365                   .ip4 = {
366                           .as_u8 = VRRP4_MCAST_ADDR_AS_U8,
367                           },
368                   },
369 };
370
371 static const mfib_prefix_t all_vrrp6_routers = {
372   .fp_proto = FIB_PROTOCOL_IP6,
373   .fp_len = 128,
374   .fp_grp_addr = {
375                   .ip6 = {
376                           .as_u8 = VRRP6_MCAST_ADDR_AS_U8,
377                           },
378                   },
379 };
380
381 static int
382 vrrp_intf_enable_disable_mcast (u8 enable, u32 sw_if_index, u8 is_ipv6)
383 {
384   vrrp_main_t *vrm = &vrrp_main;
385   vrrp_intf_t *intf;
386   u32 fib_index, i;
387   u32 n_vrs_in_fib = 0;
388   const mfib_prefix_t *vrrp_prefix;
389   fib_protocol_t proto;
390   vnet_link_t link_type;
391   fib_route_path_t for_us = {
392     .frp_sw_if_index = 0xffffffff,
393     .frp_weight = 1,
394     .frp_flags = FIB_ROUTE_PATH_LOCAL,
395     .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
396   };
397   fib_route_path_t via_itf = {
398     .frp_sw_if_index = sw_if_index,
399     .frp_weight = 1,
400     .frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT,
401   };
402
403   intf = vrrp_intf_get (sw_if_index);
404
405   if (is_ipv6)
406     {
407       proto = FIB_PROTOCOL_IP6;
408       link_type = VNET_LINK_IP6;
409       vrrp_prefix = &all_vrrp6_routers;
410     }
411   else
412     {
413       proto = FIB_PROTOCOL_IP4;
414       link_type = VNET_LINK_IP4;
415       vrrp_prefix = &all_vrrp4_routers;
416     }
417
418   for_us.frp_proto = fib_proto_to_dpo (proto);
419   via_itf.frp_proto = fib_proto_to_dpo (proto);
420   fib_index = mfib_table_get_index_for_sw_if_index (proto, sw_if_index);
421
422   vec_foreach_index (i, vrm->vrrp_intfs)
423     {
424       if (mfib_table_get_index_for_sw_if_index (proto, i) != fib_index)
425         continue;
426
427       n_vrs_in_fib += vrrp_intf_num_vrs (i, is_ipv6);
428     }
429
430   if (enable)
431     {
432       /* ensure that the local mcast route exists */
433       mfib_table_entry_path_update (fib_index, vrrp_prefix, MFIB_SOURCE_API,
434                                     MFIB_ENTRY_FLAG_NONE, &for_us);
435
436       mfib_table_entry_path_update (fib_index, vrrp_prefix, MFIB_SOURCE_API,
437                                     MFIB_ENTRY_FLAG_NONE, &via_itf);
438       intf->mcast_adj_index[! !is_ipv6] =
439         adj_mcast_add_or_lock (proto, link_type, sw_if_index);
440     }
441   else
442     {
443       /* Remove mcast local routes if this is the last VR being deleted */
444       if (n_vrs_in_fib == 0)
445         mfib_table_entry_path_remove (fib_index, vrrp_prefix, MFIB_SOURCE_API,
446                                       &for_us);
447
448       mfib_table_entry_path_remove (fib_index, vrrp_prefix, MFIB_SOURCE_API,
449                                     &via_itf);
450     }
451
452   return 0;
453 }
454
455 static int
456 vrrp_intf_vr_add_del (u8 is_add, u32 sw_if_index, u32 vr_index, u8 is_ipv6)
457 {
458   vrrp_intf_t *vr_intf;
459
460   vr_intf = vrrp_intf_get (sw_if_index);
461   if (!vr_intf)
462     return -1;
463
464   if (is_add)
465     {
466       if (!vec_len (vr_intf->vr_indices[is_ipv6]))
467         vrrp_intf_enable_disable_mcast (1, sw_if_index, is_ipv6);
468
469       vec_add1 (vr_intf->vr_indices[is_ipv6], vr_index);
470     }
471   else
472     {
473       u32 per_intf_index =
474         vec_search (vr_intf->vr_indices[is_ipv6], vr_index);
475
476       if (per_intf_index != ~0)
477         vec_del1 (vr_intf->vr_indices[is_ipv6], per_intf_index);
478
479       /* no more VRs on this interface, disable multicast */
480       if (!vec_len (vr_intf->vr_indices[is_ipv6]))
481         vrrp_intf_enable_disable_mcast (0, sw_if_index, is_ipv6);
482     }
483
484   return 0;
485 }
486
487 /* RFC 5798 section 8.3.2 says to take care not to configure more than
488  * one VRRP router as the "IPvX address owner" of a VRID. Make sure that
489  * all of the addresses configured for this VR are configured on the
490  * interface.
491  */
492 static int
493 vrrp_vr_valid_addrs_owner (vrrp_vr_config_t * vr_conf)
494 {
495   ip46_address_t *addr;
496   u8 is_ipv6 = (vr_conf->flags & VRRP_VR_IPV6) != 0;
497
498   vec_foreach (addr, vr_conf->vr_addrs)
499   {
500     if (!ip_interface_has_address (vr_conf->sw_if_index, addr, !is_ipv6))
501       return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
502   }
503
504   return 0;
505 }
506
507 static int
508 vrrp_vr_valid_addrs_unused (vrrp_vr_config_t * vr_conf)
509 {
510   ip46_address_t *vr_addr;
511   u8 is_ipv6 = (vr_conf->flags & VRRP_VR_IPV6) != 0;
512
513   vec_foreach (vr_addr, vr_conf->vr_addrs)
514   {
515     u32 vr_index;
516     void *addr;
517
518     addr = (is_ipv6) ? (void *) &vr_addr->ip6 : (void *) &vr_addr->ip4;
519     vr_index = vrrp_vr_lookup_address (vr_conf->sw_if_index, is_ipv6, addr);
520     if (vr_index != ~0)
521       return VNET_API_ERROR_ADDRESS_IN_USE;
522   }
523
524   return 0;
525 }
526
527 static int
528 vrrp_vr_valid_addrs (vrrp_vr_config_t * vr_conf)
529 {
530   int ret = 0;
531
532   /* If the VR owns the addresses, make sure they are configured */
533   if (vr_conf->priority == 255 &&
534       (ret = vrrp_vr_valid_addrs_owner (vr_conf)) < 0)
535     return ret;
536
537   /* make sure no other VR has already configured any of the VR addresses */
538   ret = vrrp_vr_valid_addrs_unused (vr_conf);
539
540   return ret;
541 }
542
543 int
544 vrrp_vr_addr_add_del (vrrp_vr_t * vr, u8 is_add, ip46_address_t * vr_addr)
545 {
546   vrrp_main_t *vmp = &vrrp_main;
547   u32 vr_index;
548   vrrp4_arp_key_t key4;
549   vrrp6_nd_key_t key6;
550   ip46_address_t *addr;
551
552   if (!vr || !vr_addr)
553     return VNET_API_ERROR_INVALID_ARGUMENT;
554
555   vr_index = vr - vmp->vrs;
556
557   if (vrrp_vr_is_ipv6 (vr))
558     {
559       key6.sw_if_index = vr->config.sw_if_index;
560       key6.addr = vr_addr->ip6;
561       if (is_add)
562         {
563           hash_set_mem_alloc (&vmp->vrrp6_nd_lookup, &key6, vr_index);
564           vec_add1 (vr->config.vr_addrs, vr_addr[0]);
565         }
566       else
567         {
568           hash_unset_mem_free (&vmp->vrrp6_nd_lookup, &key6);
569           vec_foreach (addr, vr->config.vr_addrs)
570           {
571             if (!ip46_address_cmp (addr, vr_addr))
572               {
573                 vec_del1 (vr->config.vr_addrs, vr->config.vr_addrs - addr);
574                 break;
575               }
576           }
577         }
578     }
579   else
580     {
581       key4.sw_if_index = vr->config.sw_if_index;
582       key4.addr = vr_addr->ip4;
583       if (is_add)
584         {
585           hash_set (vmp->vrrp4_arp_lookup, key4.as_u64, vr_index);
586           vec_add1 (vr->config.vr_addrs, vr_addr[0]);
587         }
588       else
589         {
590           hash_unset (vmp->vrrp4_arp_lookup, key4.as_u64);
591           vec_foreach (addr, vr->config.vr_addrs)
592           {
593             if (!ip46_address_cmp (addr, vr_addr))
594               {
595                 vec_del1 (vr->config.vr_addrs, vr->config.vr_addrs - addr);
596                 break;
597               }
598           }
599         }
600     }
601
602   return 0;
603 }
604
605 static void
606 vrrp_vr_addrs_add_del (vrrp_vr_t * vr, u8 is_add, ip46_address_t * vr_addrs)
607 {
608   ip46_address_t *vr_addr;
609
610   vec_foreach (vr_addr, vr_addrs)
611   {
612     vrrp_vr_addr_add_del (vr, is_add, vr_addr);
613   }
614 }
615
616 /* Action function shared between message handler and debug CLI */
617 int
618 vrrp_vr_add_del (u8 is_add, vrrp_vr_config_t * vr_conf)
619 {
620   vrrp_main_t *vrm = &vrrp_main;
621   vnet_main_t *vnm = vnet_get_main ();
622   vrrp_vr_key_t key;
623   uword *p;
624   u32 vr_index;
625   vrrp_vr_t *vr = 0;
626   int ret;
627
628   if (vr_conf->sw_if_index == ~0 ||
629       !vnet_sw_interface_is_valid (vnm, vr_conf->sw_if_index))
630     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
631
632   clib_memset (&key, 0, sizeof (key));
633
634   key.sw_if_index = vr_conf->sw_if_index;
635   key.vr_id = vr_conf->vr_id;
636   key.is_ipv6 = ((vr_conf->flags & VRRP_VR_IPV6) != 0);
637
638   p = mhash_get (&vrm->vr_index_by_key, &key);
639
640   if (is_add)
641     {
642       /* does a VR matching this key already exist ? */
643       if (p)
644         {
645           clib_warning ("VR %u for IPv%d already exists on sw_if_index %u",
646                         key.vr_id, (key.is_ipv6) ? 6 : 4, key.sw_if_index);
647           return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
648         }
649
650       /* were IPvX addresses included ? */
651       if (!vec_len (vr_conf->vr_addrs))
652         {
653           clib_warning ("Conf of VR %u for IPv%d on sw_if_index %u "
654                         " does not contain IP addresses",
655                         key.vr_id, (key.is_ipv6) ? 6 : 4, key.sw_if_index);
656           return VNET_API_ERROR_INVALID_SRC_ADDRESS;
657         }
658
659       /* Make sure the addresses are ok to use */
660       if ((ret = vrrp_vr_valid_addrs (vr_conf)) < 0)
661         return ret;
662
663       pool_get_zero (vrm->vrs, vr);
664       vr_index = vr - vrm->vrs;
665
666       clib_memcpy (&vr->config, vr_conf, sizeof (vrrp_vr_config_t));
667
668       vr->config.vr_addrs = 0;  /* allocate our own memory */
669       vrrp_vr_addrs_add_del (vr, is_add, vr_conf->vr_addrs);
670
671       vr->runtime.state = VRRP_VR_STATE_INIT;
672       vr->runtime.timer_index = ~0;
673
674       /* set virtual MAC based on IP version and VR ID */
675       vr->runtime.mac = (key.is_ipv6) ? ipv6_vmac : ipv4_vmac;
676       vr->runtime.mac.bytes[5] = vr_conf->vr_id;
677
678       mhash_set (&vrm->vr_index_by_key, &key, vr_index, 0);
679     }
680   else
681     {
682       if (!p)
683         {
684           clib_warning ("No VR %u for IPv%d exists on sw_if_index %u",
685                         key.vr_id, (key.is_ipv6) ? 6 : 4, key.sw_if_index);
686           return VNET_API_ERROR_NO_SUCH_ENTRY;
687         }
688
689       vr_index = p[0];
690       vr = pool_elt_at_index (vrm->vrs, vr_index);
691
692       vrrp_vr_tracking_ifs_add_del (vr, vr->tracking.interfaces, is_add);
693       vrrp_vr_addrs_add_del (vr, is_add, vr->config.vr_addrs);
694       mhash_unset (&vrm->vr_index_by_key, &key, 0);
695       vec_free (vr->config.peer_addrs);
696       vec_free (vr->config.vr_addrs);
697       vec_free (vr->tracking.interfaces);
698       pool_put (vrm->vrs, vr);
699     }
700
701   vrrp_intf_vr_add_del (is_add, vr_conf->sw_if_index, vr_index, key.is_ipv6);
702
703   return 0;
704 }
705
706 int
707 vrrp_vr_start_stop (u8 is_start, vrrp_vr_key_t * vr_key)
708 {
709   vrrp_main_t *vmp = &vrrp_main;
710   uword *p;
711   vrrp_vr_t *vr = 0;
712
713   p = mhash_get (&vmp->vr_index_by_key, vr_key);
714   if (!p)
715     return VNET_API_ERROR_NO_SUCH_ENTRY;
716
717   vr = pool_elt_at_index (vmp->vrs, p[0]);
718
719   /* return success if already in the desired state */
720   switch (vr->runtime.state)
721     {
722     case VRRP_VR_STATE_INIT:
723       if (!is_start)
724         {
725           clib_warning ("Attempting to stop already stopped VR (%U)",
726                         format_vrrp_vr_key, vr);
727           return 0;
728         }
729       break;
730     default:
731       if (is_start)
732         {
733           clib_warning ("Attempting to start already started VR (%U)",
734                         format_vrrp_vr_key, vr);
735           return 0;
736         }
737       break;
738     }
739
740   if (is_start)
741     {
742       if (vrrp_vr_is_unicast (vr) && vec_len (vr->config.peer_addrs) == 0)
743         {
744           clib_warning ("Cannot start unicast VR without peers");
745           return VNET_API_ERROR_INIT_FAILED;
746         }
747
748       vmp->n_vrs_started++;
749
750       if (!vrrp_intf_is_up (vr->config.sw_if_index, vrrp_vr_is_ipv6 (vr),
751                             NULL))
752         {
753           clib_warning ("VRRP VR started on down interface (%U)",
754                         format_vrrp_vr_key, vr);
755           vrrp_vr_transition (vr, VRRP_VR_STATE_INTF_DOWN, NULL);
756         }
757       else if (vrrp_vr_is_owner (vr))
758         vrrp_vr_transition (vr, VRRP_VR_STATE_MASTER, NULL);
759       else
760         vrrp_vr_transition (vr, VRRP_VR_STATE_BACKUP, NULL);
761     }
762   else
763     {
764       vmp->n_vrs_started--;
765
766       vrrp_vr_transition (vr, VRRP_VR_STATE_INIT, NULL);
767     }
768
769   clib_warning ("%d VRs configured, %d VRs running",
770                 pool_elts (vmp->vrs), vmp->n_vrs_started);
771
772   return 0;
773 }
774
775 static int
776 vrrp_vr_set_peers_validate (vrrp_vr_t * vr, ip46_address_t * peers)
777 {
778   if (!vrrp_vr_is_unicast (vr))
779     {
780       clib_warning ("Peers can only be set on a unicast VR");
781       return VNET_API_ERROR_INVALID_ARGUMENT;
782     }
783
784   if (vr->runtime.state != VRRP_VR_STATE_INIT)
785     {
786       clib_warning ("Cannot set peers on a running VR");
787       return VNET_API_ERROR_RSRC_IN_USE;
788     }
789
790   if (vec_len (peers) == 0)
791     {
792       clib_warning ("No peer addresses provided");
793       return VNET_API_ERROR_INVALID_DST_ADDRESS;
794     }
795
796   return 0;
797 }
798
799 int
800 vrrp_vr_set_peers (vrrp_vr_key_t * vr_key, ip46_address_t * peers)
801 {
802   vrrp_main_t *vmp = &vrrp_main;
803   uword *p;
804   vrrp_vr_t *vr = 0;
805   int ret = 0;
806
807   p = mhash_get (&vmp->vr_index_by_key, vr_key);
808   if (!p)
809     return VNET_API_ERROR_NO_SUCH_ENTRY;
810
811   vr = pool_elt_at_index (vmp->vrs, p[0]);
812
813   ret = vrrp_vr_set_peers_validate (vr, peers);
814   if (ret < 0)
815     return ret;
816
817   if (vr->config.peer_addrs)
818     vec_free (vr->config.peer_addrs);
819
820   vr->config.peer_addrs = vec_dup (peers);
821
822   return 0;
823 }
824
825 /* Manage reference on the interface to the VRs which track that interface */
826 static void
827 vrrp_intf_tracking_vr_add_del (u32 sw_if_index, vrrp_vr_t * vr, u8 is_add)
828 {
829   vrrp_intf_t *intf;
830   u32 vr_index;
831   u8 is_ipv6 = vrrp_vr_is_ipv6 (vr);
832   int i;
833
834   intf = vrrp_intf_get (sw_if_index);
835   vr_index = vrrp_vr_index (vr);
836
837   /* Try to find the VR index in the list of tracking VRs */
838   vec_foreach_index (i, intf->tracking_vrs[is_ipv6])
839   {
840     if (vec_elt (intf->tracking_vrs[is_ipv6], i) != vr_index)
841       continue;
842
843     /* Current index matches VR index */
844     if (!is_add)
845       vec_delete (intf->tracking_vrs[is_ipv6], 1, i);
846
847     /* If deleting, the job is done. If adding, it's already here */
848     return;
849   }
850
851   /* vr index was not found. */
852   if (is_add)
853     vec_add1 (intf->tracking_vrs[is_ipv6], vr_index);
854 }
855
856 /* Check if sw intf admin state is up or in the process of coming up */
857 static int
858 vrrp_intf_sw_admin_up (u32 sw_if_index, vrrp_intf_update_t * pending)
859 {
860   vnet_main_t *vnm = vnet_get_main ();
861   int admin_up;
862
863   if (pending && (pending->type == VRRP_IF_UPDATE_SW_ADMIN))
864     admin_up = pending->intf_up;
865   else
866     admin_up = vnet_sw_interface_is_admin_up (vnm, sw_if_index);
867
868   return admin_up;
869 }
870
871 /* Check if hw intf link state is up or int the process of coming up */
872 static int
873 vrrp_intf_hw_link_up (u32 sw_if_index, vrrp_intf_update_t * pending)
874 {
875   vnet_main_t *vnm = vnet_get_main ();
876   vnet_sw_interface_t *sup_sw;
877   int link_up;
878
879   sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
880
881   if (pending && (pending->type == VRRP_IF_UPDATE_HW_LINK) &&
882       (pending->hw_if_index == sup_sw->hw_if_index))
883     link_up = pending->intf_up;
884   else
885     link_up = vnet_hw_interface_is_link_up (vnm, sup_sw->hw_if_index);
886
887   return link_up;
888 }
889
890 /* Check if interface has ability to send IP packets. */
891 static int
892 vrrp_intf_ip_up (u32 sw_if_index, u8 is_ipv6, vrrp_intf_update_t * pending)
893 {
894   int ip_up;
895
896   if (pending && pending->type == VRRP_IF_UPDATE_IP)
897     ip_up = pending->intf_up;
898   else
899     /* Either a unicast address has to be explicitly assigned, or
900      * for IPv6 only, a link local assigned and multicast/ND enabled
901      */
902     ip_up =
903       ((ip_interface_get_first_ip (sw_if_index, !is_ipv6) != 0) ||
904        (is_ipv6 && ip6_link_is_enabled (sw_if_index)));
905
906   return ip_up;
907 }
908
909 static int
910 vrrp_intf_is_up (u32 sw_if_index, u8 is_ipv6, vrrp_intf_update_t * pending)
911 {
912   int admin_up, link_up, ip_up;
913
914   admin_up = vrrp_intf_sw_admin_up (sw_if_index, pending);
915   link_up = vrrp_intf_hw_link_up (sw_if_index, pending);
916   ip_up = vrrp_intf_ip_up (sw_if_index, is_ipv6, pending);
917
918   return (admin_up && link_up && ip_up);
919 }
920
921 /* Examine the state of interfaces tracked by a VR and compute the priority
922  * adjustment that should be applied to the VR. If this is being called
923  * by the hw_link_up_down callback, the pending new flags on the sup hw
924  * interface have not been updated yet, so accept those as an optional
925  * argument.
926  */
927 void
928 vrrp_vr_tracking_ifs_compute (vrrp_vr_t * vr, vrrp_intf_update_t * pending)
929 {
930   vrrp_vr_tracking_if_t *intf;
931   u32 total_priority = 0;
932
933   vec_foreach (intf, vr->tracking.interfaces)
934   {
935     if (vrrp_intf_is_up (intf->sw_if_index, vrrp_vr_is_ipv6 (vr), pending))
936       continue;
937
938     total_priority += intf->priority;
939   }
940
941   if (total_priority != vr->tracking.interfaces_dec)
942     {
943       clib_warning ("VR %U interface track adjustment change from %u to %u",
944                     format_vrrp_vr_key, vr, vr->tracking.interfaces_dec,
945                     total_priority);
946       vr->tracking.interfaces_dec = total_priority;
947     }
948 }
949
950 /* Manage tracked interfaces on a VR */
951 int
952 vrrp_vr_tracking_if_add_del (vrrp_vr_t * vr, u32 sw_if_index, u8 prio,
953                              u8 is_add)
954 {
955   vnet_main_t *vnm = vnet_get_main ();
956   vrrp_vr_tracking_if_t *track_intf;
957
958   /* VR can't track non-existent interface */
959   if (!vnet_sw_interface_is_valid (vnm, sw_if_index))
960     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
961
962   /* VR can't track it's own interface */
963   if (sw_if_index == vr->config.sw_if_index)
964     return VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
965
966   /* update intf vector of tracking VRs */
967   vrrp_intf_tracking_vr_add_del (sw_if_index, vr, is_add);
968
969   /* update VR vector of tracked interfaces */
970   vec_foreach (track_intf, vr->tracking.interfaces)
971   {
972     if (track_intf->sw_if_index != sw_if_index)
973       continue;
974
975     /* found it */
976     if (!is_add)
977       vec_delete
978         (vr->tracking.interfaces, 1, track_intf - vr->tracking.interfaces);
979
980     return 0;
981   }
982
983   if (is_add)
984     {
985       vec_add2 (vr->tracking.interfaces, track_intf, 1);
986
987       track_intf->sw_if_index = sw_if_index;
988       track_intf->priority = prio;
989     }
990
991   return 0;
992 }
993
994 int
995 vrrp_vr_tracking_ifs_add_del (vrrp_vr_t * vr,
996                               vrrp_vr_tracking_if_t * track_ifs, u8 is_add)
997 {
998   vrrp_vr_tracking_if_t *track_if, *ifs_copy;
999   int rv = 0;
1000
1001   /* if deleting & track_ifs points to the VR list of tracked intfs, the
1002    * vector could be modified as we iterate it. make a copy instead */
1003   ifs_copy = vec_dup (track_ifs);
1004
1005   /* add each tracked interface in the vector */
1006   vec_foreach (track_if, ifs_copy)
1007   {
1008     rv = vrrp_vr_tracking_if_add_del (vr, track_if->sw_if_index,
1009                                       track_if->priority, (is_add != 0));
1010
1011     /* if operation failed, undo the previous changes */
1012     if (rv)
1013       {
1014         vrrp_vr_tracking_if_t *rb_if;
1015
1016         for (rb_if = track_if - 1; rb_if >= track_ifs; rb_if -= 1)
1017           vrrp_vr_tracking_if_add_del (vr, rb_if->sw_if_index,
1018                                        rb_if->priority, !(is_add != 0));
1019         break;
1020       }
1021   }
1022
1023   vec_free (ifs_copy);
1024
1025   vrrp_vr_tracking_ifs_compute (vr, 0);
1026
1027   return rv;
1028 }
1029
1030 /* Compute priority to advertise on all VRs which track the given interface
1031  * and address family. The flags on an HW interface are not updated until
1032  * after link up/down callbacks are invoked, so if this function is called
1033  * by the link up/down callback, the flags about to be set will be passed
1034  * via the 'pending' argument. Otherwise, pending will be NULL.
1035  */
1036 static void
1037 vrrp_intf_tracking_vrs_compute (u32 sw_if_index,
1038                                 vrrp_intf_update_t * pending, u8 is_ipv6)
1039 {
1040   u32 *vr_index;
1041   vrrp_vr_t *vr;
1042   vrrp_intf_t *intf = vrrp_intf_get (sw_if_index);
1043
1044   vec_foreach (vr_index, intf->tracking_vrs[is_ipv6])
1045   {
1046     vr = vrrp_vr_lookup_index (*vr_index);
1047     if (vr)
1048       vrrp_vr_tracking_ifs_compute (vr, pending);
1049   }
1050 }
1051
1052 /* Interface being brought up/down is a quasi-{startup/shutdown} event.
1053  * Execute an appropriate state transition for all VRs on the interface.
1054  * This function may be invoked by:
1055  *    sw interface admin up/down event
1056  *    hw interface link up/down event
1057  */
1058 clib_error_t *
1059 vrrp_sw_interface_up_down (vrrp_intf_update_t * pending)
1060 {
1061   vrrp_intf_t *intf;
1062   int i;
1063   u32 *vr_index;
1064   vrrp_vr_t *vr;
1065
1066   intf = vrrp_intf_get (pending->sw_if_index);
1067   if (!intf)
1068     return 0;
1069
1070   /* adjust state of VR's configured on this interface */
1071   for (i = 0; i < 2; i++)
1072     {
1073       int is_up;
1074
1075       if (!intf->vr_indices[i])
1076         continue;
1077
1078       is_up = vrrp_intf_is_up (pending->sw_if_index, i, pending);
1079
1080       vec_foreach (vr_index, intf->vr_indices[i])
1081       {
1082         vrrp_vr_state_t vr_state;
1083
1084         vr = vrrp_vr_lookup_index (*vr_index);
1085         if (!vr)
1086           continue;
1087
1088         if (vr->runtime.state == VRRP_VR_STATE_INIT)
1089           continue;             /* VR not started yet, no transition */
1090
1091         if (!is_up)
1092           vr_state = VRRP_VR_STATE_INTF_DOWN;
1093         else
1094           {
1095             if (vr->runtime.state != VRRP_VR_STATE_INTF_DOWN)
1096               continue;         /* shouldn't happen */
1097
1098             vr_state = (vrrp_vr_is_owner (vr)) ?
1099               VRRP_VR_STATE_MASTER : VRRP_VR_STATE_BACKUP;
1100           }
1101
1102         vrrp_vr_transition (vr, vr_state, NULL);
1103       }
1104     }
1105
1106   /* compute adjustments on any VR's tracking this interface */
1107   vrrp_intf_tracking_vrs_compute (pending->sw_if_index, pending,
1108                                   0 /* is_ipv6 */ );
1109   vrrp_intf_tracking_vrs_compute (pending->sw_if_index, pending,
1110                                   1 /* is_ipv6 */ );
1111
1112   return 0;
1113 }
1114
1115 /* Process change in admin status on an interface */
1116 clib_error_t *
1117 vrrp_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index,
1118                                  u32 flags)
1119 {
1120   vrrp_intf_update_t pending = {
1121     .type = VRRP_IF_UPDATE_SW_ADMIN,
1122     .sw_if_index = sw_if_index,
1123     .intf_up = ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0),
1124   };
1125
1126   return vrrp_sw_interface_up_down (&pending);
1127 }
1128
1129 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (vrrp_sw_interface_admin_up_down);
1130
1131 static walk_rc_t
1132 vrrp_hw_interface_link_up_down_walk (vnet_main_t * vnm,
1133                                      u32 sw_if_index, void *arg)
1134 {
1135   vrrp_intf_update_t *pending = arg;
1136
1137   pending->sw_if_index = sw_if_index;
1138   vrrp_sw_interface_up_down (pending);
1139
1140   return WALK_CONTINUE;
1141 }
1142
1143 static clib_error_t *
1144 vrrp_hw_interface_link_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1145 {
1146   vrrp_intf_update_t pending = {
1147     .type = VRRP_IF_UPDATE_HW_LINK,
1148     .hw_if_index = hw_if_index,
1149     .intf_up = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) != 0),
1150   };
1151
1152   /* walk the sw interface and sub interfaces to adjust interface tracking */
1153   vnet_hw_interface_walk_sw (vnm, hw_if_index,
1154                              vrrp_hw_interface_link_up_down_walk, &pending);
1155
1156   return 0;
1157 }
1158
1159 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (vrrp_hw_interface_link_up_down);
1160
1161 static void
1162 vrrp_ip4_add_del_interface_addr (ip4_main_t * im,
1163                                  uword opaque,
1164                                  u32 sw_if_index,
1165                                  ip4_address_t * address,
1166                                  u32 address_length,
1167                                  u32 if_address_index, u32 is_del)
1168 {
1169   vrrp_intf_tracking_vrs_compute (sw_if_index, NULL, 0 /* is_ipv6 */ );
1170 }
1171
1172 static ip6_link_delegate_id_t vrrp_ip6_delegate_id;
1173
1174 static u8 *
1175 format_vrrp_ip6_link (u8 * s, va_list * args)
1176 {
1177   index_t indi = va_arg (*args, index_t);
1178   u32 indent = va_arg (*args, u32);
1179   vrrp_intf_t *intf;
1180   u32 *vr_index;
1181
1182   intf = vrrp_intf_get ((u32) indi);
1183
1184   s = format (s, "%UVRRP VRs monitoring this link:\n",
1185               format_white_space, indent);
1186
1187   vec_foreach (vr_index, intf->tracking_vrs[1])
1188   {
1189     vrrp_vr_t *vr = vrrp_vr_lookup_index (*vr_index);
1190
1191     s = format (s, "%U%U\n", format_white_space, indent + 2,
1192                 format_vrrp_vr_key, vr);
1193   }
1194
1195   return s;
1196 }
1197
1198 static void
1199 vrrp_intf_ip6_enable_disable (u32 sw_if_index, int enable)
1200 {
1201   vrrp_intf_update_t pending = {
1202     .type = VRRP_IF_UPDATE_IP,
1203     .sw_if_index = sw_if_index,
1204     .intf_up = enable,
1205   };
1206
1207   vrrp_intf_tracking_vrs_compute (sw_if_index, &pending, 1 /* is_ipv6 */ );
1208 }
1209
1210 static void
1211 vrrp_intf_ip6_enable (u32 sw_if_index)
1212 {
1213   vrrp_intf_ip6_enable_disable (sw_if_index, 1 /* enable */ );
1214   ip6_link_delegate_update (sw_if_index, vrrp_ip6_delegate_id, sw_if_index);
1215 }
1216
1217 static void
1218 vrrp_intf_ip6_disable (index_t indi)
1219 {
1220   vrrp_intf_ip6_enable_disable (indi, 0 /* enable */ );
1221 }
1222
1223 const static ip6_link_delegate_vft_t vrrp_ip6_delegate_vft = {
1224   .ildv_enable = vrrp_intf_ip6_enable,
1225   .ildv_disable = vrrp_intf_ip6_disable,
1226   .ildv_format = format_vrrp_ip6_link,
1227 };
1228
1229 static clib_error_t *
1230 vrrp_init (vlib_main_t * vm)
1231 {
1232   vrrp_main_t *vmp = &vrrp_main;
1233   clib_error_t *error = 0;
1234   ip4_main_t *im4 = &ip4_main;
1235   ip4_add_del_interface_address_callback_t cb4;
1236   vlib_node_t *intf_output_node;
1237
1238   clib_memset (vmp, 0, sizeof (*vmp));
1239
1240   if ((error = vlib_call_init_function (vm, ip4_lookup_init)) ||
1241       (error = vlib_call_init_function (vm, ip6_lookup_init)))
1242     return error;
1243
1244   vmp->vlib_main = vm;
1245   vmp->vnet_main = vnet_get_main ();
1246
1247   intf_output_node = vlib_get_node_by_name (vm, (u8 *) "interface-output");
1248   vmp->intf_output_node_idx = intf_output_node->index;
1249
1250   error = vrrp_plugin_api_hookup (vm);
1251
1252   if (error)
1253     return error;
1254
1255   mhash_init (&vmp->vr_index_by_key, sizeof (u32), sizeof (vrrp_vr_key_t));
1256   vmp->vrrp4_arp_lookup = hash_create (0, sizeof (uword));
1257   vmp->vrrp6_nd_lookup = hash_create_mem (0, sizeof (vrrp6_nd_key_t),
1258                                           sizeof (uword));
1259
1260   cb4.function = vrrp_ip4_add_del_interface_addr;
1261   cb4.function_opaque = 0;
1262   vec_add1 (im4->add_del_interface_address_callbacks, cb4);
1263
1264   vrrp_ip6_delegate_id = ip6_link_delegate_register (&vrrp_ip6_delegate_vft);
1265
1266   return error;
1267 }
1268
1269 VLIB_INIT_FUNCTION (vrrp_init);
1270
1271
1272 /* *INDENT-OFF* */
1273 VLIB_PLUGIN_REGISTER () =
1274 {
1275   .version = VPP_BUILD_VER,
1276   .description = "VRRP v3 (RFC 5798)",
1277 };
1278 /* *INDENT-ON* */
1279
1280 /*
1281  * fd.io coding-style-patch-verification: ON
1282  *
1283  * Local Variables:
1284  * eval: (c-set-style "gnu")
1285  * End:
1286  */