ip: Replace Sematics for Interface IP addresses
[vpp.git] / src / vnet / ip / vtep.c
1 /*
2  * Copyright (c) 2020 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 #include <vnet/ip/vtep.h>
17
18 uword
19 vtep_addr_ref (vtep_table_t * t, u32 fib_index, ip46_address_t * ip)
20 {
21   vtep4_key_t key4 = {.addr = ip->ip4,.fib_index = fib_index };
22   vtep6_key_t key6 = {.addr = ip->ip6,.fib_index = fib_index };
23   uword *vtep = ip46_address_is_ip4 (ip) ?
24     hash_get (t->vtep4, key4.as_u64) : hash_get_mem (t->vtep6, &key6);
25   if (vtep)
26     return ++(*vtep);
27   ip46_address_is_ip4 (ip) ?
28     hash_set (t->vtep4, key4.as_u64, 1) :
29     hash_set_mem_alloc (&t->vtep6, &key6, 1);
30   return 1;
31 }
32
33 uword
34 vtep_addr_unref (vtep_table_t * t, u32 fib_index, ip46_address_t * ip)
35 {
36   vtep4_key_t key4 = {.addr = ip->ip4,.fib_index = fib_index };
37   vtep6_key_t key6 = {.addr = ip->ip6,.fib_index = fib_index };
38   uword *vtep = ip46_address_is_ip4 (ip) ?
39     hash_get (t->vtep4, key4.as_u64) : hash_get_mem (t->vtep6, &key6);
40   ALWAYS_ASSERT (vtep);
41   if (--(*vtep) != 0)
42     return *vtep;
43   ip46_address_is_ip4 (ip) ?
44     hash_unset (t->vtep4, key4.as_u64) :
45     hash_unset_mem_free (&t->vtep6, &key6);
46   return 0;
47 }
48
49 /*
50  * fd.io coding-style-patch-verification: ON
51  *
52  * Local Variables:
53  * eval: (c-set-style "gnu")
54  * End:
55  */