ip-neighbor: Use ip_address_t rather than ip46_address_t
[vpp.git] / src / vnet / ip-neighbor / ip_neighbor_watch.c
1 /*
2  * ip_neighboor_watch.c; IP neighbor watching
3  *
4  * Copyright (c) 2019 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/ip-neighbor/ip_neighbor.h>
19 #include <vnet/ip/ip_types_api.h>
20 #include <vnet/ethernet/ethernet_types_api.h>
21
22 #include <vnet/ip-neighbor/ip_neighbor.api_enum.h>
23 #include <vnet/ip-neighbor/ip_neighbor.api_types.h>
24
25 #include <vlibmemory/api.h>
26
27 /**
28  * Database of registered watchers
29  * The key for a watcher is {type, sw_if_index, addreess}
30  * interface=~0 / address=all-zeros imples any.
31  */
32 typedef struct ip_neighbor_watch_db_t_
33 {
34   mhash_t ipnwdb_hash;
35 } ip_neighbor_watch_db_t;
36
37 static ip_neighbor_watch_db_t ipnw_db;
38
39 static uword
40 ip_neighbor_event_process (vlib_main_t * vm,
41                            vlib_node_runtime_t * rt, vlib_frame_t * f)
42 {
43   ip_neighbor_event_t *ipne, *ipnes = NULL;
44   uword event_type = ~0;
45
46   while (1)
47     {
48       vlib_process_wait_for_event (vm);
49
50       ipnes = vlib_process_get_event_data (vm, &event_type);
51
52       switch (event_type)
53         {
54         default:
55           vec_foreach (ipne, ipnes) ip_neighbor_handle_event (ipne);
56           break;
57
58         case ~0:
59           /* timeout - */
60           break;
61         }
62
63       vec_reset_length (ipnes);
64     }
65   return 0;
66 }
67
68 /* *INDENT-OFF* */
69 VLIB_REGISTER_NODE (ip_neighbor_event_process_node) = {
70   .function = ip_neighbor_event_process,
71   .type = VLIB_NODE_TYPE_PROCESS,
72   .name = "ip-neighbor-event",
73 };
74 /* *INDENT-ON* */
75
76
77 static clib_error_t *
78 want_ip_neighbor_events_reaper (u32 client_index)
79 {
80   ip_neighbor_key_t *key, *empty_keys = NULL;
81   ip_neighbor_watcher_t *watchers;
82   uword *v;
83   i32 pos;
84
85   /* walk the entire IP neighbour DB and removes the client's registrations */
86   /* *INDENT-OFF* */
87   mhash_foreach(key, v, &ipnw_db.ipnwdb_hash,
88   ({
89     watchers = (ip_neighbor_watcher_t*) *v;
90
91     vec_foreach_index_backwards (pos, watchers) {
92       if (watchers[pos].ipw_client == client_index)
93         vec_del1(watchers, pos);
94     }
95
96     if (vec_len(watchers) == 0)
97       vec_add1 (empty_keys, *key);
98   }));
99   /* *INDENT-OFF* */
100
101   vec_foreach (key, empty_keys)
102     mhash_unset (&ipnw_db.ipnwdb_hash, key, NULL);
103   vec_free (empty_keys);
104   return (NULL);
105 }
106
107 VL_MSG_API_REAPER_FUNCTION (want_ip_neighbor_events_reaper);
108
109 static int
110 ip_neighbor_watch_cmp (const ip_neighbor_watcher_t * w1,
111                        const ip_neighbor_watcher_t * w2)
112 {
113   return (0 == clib_memcmp (w1, w2, sizeof(*w1)));
114 }
115
116 void
117 ip_neighbor_watch (const ip_address_t * ip,
118                    u32 sw_if_index,
119                    const ip_neighbor_watcher_t * watch)
120 {
121   ip_neighbor_key_t key = {
122     .ipnk_ip = *ip,
123     .ipnk_sw_if_index = (sw_if_index == 0 ? ~0 : sw_if_index),
124   };
125   ip_neighbor_watcher_t *ipws = NULL;
126   uword *p;
127
128   p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
129
130   if (p)
131     {
132       ipws = (ip_neighbor_watcher_t*) p[0];
133
134       if (~0 != vec_search_with_function (ipws, watch,
135                                           ip_neighbor_watch_cmp))
136         /* duplicate */
137         return;
138     }
139
140   vec_add1 (ipws, *watch);
141
142   mhash_set (&ipnw_db.ipnwdb_hash, &key, (uword) ipws, NULL);
143 }
144
145 void
146 ip_neighbor_unwatch (const ip_address_t * ip,
147                      u32 sw_if_index,
148                      const ip_neighbor_watcher_t * watch)
149 {
150   ip_neighbor_key_t key = {
151     .ipnk_ip = *ip,
152     .ipnk_sw_if_index = (sw_if_index == 0 ? ~0 : sw_if_index),
153   };
154   ip_neighbor_watcher_t *ipws = NULL;
155   uword *p;
156   u32 pos;
157
158   p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
159
160   if (!p)
161     return;
162
163   ipws = (ip_neighbor_watcher_t*) p[0];
164
165   pos = vec_search_with_function (ipws, watch, ip_neighbor_watch_cmp);
166
167   if (~0 == pos)
168     return;
169
170   vec_del1 (ipws, pos);
171
172   if (vec_len(ipws) == 0)
173     mhash_unset (&ipnw_db.ipnwdb_hash, &key, NULL);
174 }
175
176 static void
177 ip_neighbor_signal (ip_neighbor_watcher_t *watchers, index_t ipni)
178 {
179   ip_neighbor_watcher_t *watcher;
180
181   vec_foreach (watcher, watchers) {
182     ip_neighbor_event_t *ipne;
183
184     ipne = vlib_process_signal_event_data (vlib_get_main(),
185                                            ip_neighbor_event_process_node.index,
186                                            0, 1, sizeof(*ipne));
187     ipne->ipne_watch = *watcher;
188     ipne->ipne_index = ipni;
189   }
190 }
191
192 void
193 ip_neighbor_publish (index_t ipni)
194 {
195   const ip_neighbor_t *ipn;
196   ip_neighbor_key_t key;
197   uword *p;
198
199   ipn = ip_neighbor_get (ipni);
200
201   clib_memcpy (&key, ipn->ipn_key, sizeof (key));
202
203   /* Search the DB from longest to shortest key */
204   p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
205
206   if (p) {
207     ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni);
208   }
209
210   ip_address_reset (&key.ipnk_ip);
211   p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
212
213   if (p) {
214     ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni);
215   }
216
217   key.ipnk_sw_if_index = ~0;
218   p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
219
220   if (p) {
221     ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni);
222   }
223 }
224
225 static clib_error_t *
226 ip_neighbor_watchers_show (vlib_main_t * vm,
227                            unformat_input_t * input,
228                            vlib_cli_command_t * cmd)
229 {
230   ip_neighbor_watcher_t *watchers, *watcher;
231   ip_neighbor_key_t *key;
232   uword *v;
233
234   /* *INDENT-OFF* */
235   mhash_foreach(key, v, &ipnw_db.ipnwdb_hash,
236   ({
237     watchers = (ip_neighbor_watcher_t*) *v;
238
239     ASSERT(vec_len(watchers));
240     vlib_cli_output (vm, "Key: %U", format_ip_neighbor_key, key);
241
242     vec_foreach (watcher, watchers)
243       vlib_cli_output (vm, "  %U", format_ip_neighbor_watcher, watcher);
244   }));
245   /* *INDENT-ON* */
246   return (NULL);
247 }
248
249 /* *INDENT-OFF* */
250 VLIB_CLI_COMMAND (show_ip_neighbor_watchers_cmd_node, static) = {
251   .path = "show ip neighbor-watcher",
252   .function = ip_neighbor_watchers_show,
253   .short_help = "show ip neighbors-watcher",
254 };
255 /* *INDENT-ON* */
256
257 static clib_error_t *
258 ip_neighbor_watch_init (vlib_main_t * vm)
259 {
260   mhash_init (&ipnw_db.ipnwdb_hash,
261               sizeof (ip_neighbor_watcher_t *), sizeof (ip_neighbor_key_t));
262   return (NULL);
263 }
264
265 /* *INDENT-OFF* */
266 VLIB_INIT_FUNCTION (ip_neighbor_watch_init) =
267 {
268   .runs_after = VLIB_INITS("ip_neighbor_init"),
269 };
270 /* *INDENT-ON* */
271
272
273 /*
274  * fd.io coding-style-patch-verification: ON
275  *
276  * Local Variables:
277  * eval: (c-set-style "gnu")
278  * End:
279  */