2 * ip_neighboor_watch.c; IP neighbor watching
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/ip-neighbor/ip_neighbor.h>
19 #include <vnet/ip-neighbor/ip_neighbor_watch.h>
20 #include <vnet/ip/ip_types_api.h>
21 #include <vnet/ethernet/ethernet_types_api.h>
23 #include <vnet/ip-neighbor/ip_neighbor.api_enum.h>
24 #include <vnet/ip-neighbor/ip_neighbor.api_types.h>
26 #include <vlibmemory/api.h>
29 * Database of registered watchers
30 * The key for a watcher is {type, sw_if_index, addreess}
31 * interface=~0 / address=all-zeros imples any.
33 typedef struct ip_neighbor_watch_db_t_
36 } ip_neighbor_watch_db_t;
38 static ip_neighbor_watch_db_t ipnw_db;
41 ip_neighbor_event_process (vlib_main_t * vm,
42 vlib_node_runtime_t * rt, vlib_frame_t * f)
44 ip_neighbor_event_t *ipne, *ipnes = NULL;
45 uword event_type = ~0;
49 vlib_process_wait_for_event (vm);
51 ipnes = vlib_process_get_event_data (vm, &event_type);
56 vec_foreach (ipne, ipnes) ip_neighbor_handle_event (ipne);
64 vec_reset_length (ipnes);
70 VLIB_REGISTER_NODE (ip_neighbor_event_process_node) = {
71 .function = ip_neighbor_event_process,
72 .type = VLIB_NODE_TYPE_PROCESS,
73 .name = "ip-neighbor-event",
79 want_ip_neighbor_events_reaper (u32 client_index)
81 ip_neighbor_key_t *key, *empty_keys = NULL;
82 ip_neighbor_watcher_t *watchers;
86 /* walk the entire IP neighbour DB and removes the client's registrations */
88 mhash_foreach(key, v, &ipnw_db.ipnwdb_hash,
90 watchers = (ip_neighbor_watcher_t*) *v;
92 vec_foreach_index_backwards (pos, watchers) {
93 if (watchers[pos].ipw_client == client_index)
94 vec_del1(watchers, pos);
97 if (vec_len(watchers) == 0)
98 vec_add1 (empty_keys, *key);
102 vec_foreach (key, empty_keys)
103 mhash_unset (&ipnw_db.ipnwdb_hash, key, NULL);
104 vec_free (empty_keys);
108 VL_MSG_API_REAPER_FUNCTION (want_ip_neighbor_events_reaper);
111 ip_neighbor_watch_cmp (const ip_neighbor_watcher_t * w1,
112 const ip_neighbor_watcher_t * w2)
114 return (0 == clib_memcmp (w1, w2, sizeof(*w1)));
118 ip_neighbor_watch (const ip_address_t * ip,
120 const ip_neighbor_watcher_t * watch)
122 ip_neighbor_key_t key = {
124 .ipnk_sw_if_index = (sw_if_index == 0 ? ~0 : sw_if_index),
126 ip_neighbor_watcher_t *ipws = NULL;
129 p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
133 ipws = (ip_neighbor_watcher_t*) p[0];
135 if (~0 != vec_search_with_function (ipws, watch,
136 ip_neighbor_watch_cmp))
141 vec_add1 (ipws, *watch);
143 mhash_set (&ipnw_db.ipnwdb_hash, &key, (uword) ipws, NULL);
147 ip_neighbor_unwatch (const ip_address_t * ip,
149 const ip_neighbor_watcher_t * watch)
151 ip_neighbor_key_t key = {
153 .ipnk_sw_if_index = (sw_if_index == 0 ? ~0 : sw_if_index),
155 ip_neighbor_watcher_t *ipws = NULL;
159 p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
164 ipws = (ip_neighbor_watcher_t*) p[0];
166 pos = vec_search_with_function (ipws, watch, ip_neighbor_watch_cmp);
171 vec_del1 (ipws, pos);
173 if (vec_len(ipws) == 0)
174 mhash_unset (&ipnw_db.ipnwdb_hash, &key, NULL);
178 ip_neighbor_signal (ip_neighbor_watcher_t *watchers,
180 ip_neighbor_event_flags_t flags)
182 ip_neighbor_watcher_t *watcher;
184 vec_foreach (watcher, watchers) {
185 ip_neighbor_event_t *ipne;
187 ipne = vlib_process_signal_event_data (vlib_get_main(),
188 ip_neighbor_event_process_node.index,
189 0, 1, sizeof(*ipne));
190 ipne->ipne_watch = *watcher;
191 ipne->ipne_flags = flags;
192 ip_neighbor_clone(ip_neighbor_get(ipni), &ipne->ipne_nbr);
197 ip_neighbor_publish (index_t ipni,
198 ip_neighbor_event_flags_t flags)
200 const ip_neighbor_t *ipn;
201 ip_neighbor_key_t key;
204 ipn = ip_neighbor_get (ipni);
206 clib_memcpy (&key, ipn->ipn_key, sizeof (key));
208 /* Search the DB from longest to shortest key */
209 p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
212 ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni, flags);
215 ip_address_reset (&key.ipnk_ip);
216 p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
219 ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni, flags);
222 key.ipnk_sw_if_index = ~0;
223 p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
226 ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni, flags);
230 static clib_error_t *
231 ip_neighbor_watchers_show (vlib_main_t * vm,
232 unformat_input_t * input,
233 vlib_cli_command_t * cmd)
235 ip_neighbor_watcher_t *watchers, *watcher;
236 ip_neighbor_key_t *key;
240 mhash_foreach(key, v, &ipnw_db.ipnwdb_hash,
242 watchers = (ip_neighbor_watcher_t*) *v;
244 ASSERT(vec_len(watchers));
245 vlib_cli_output (vm, "Key: %U", format_ip_neighbor_key, key);
247 vec_foreach (watcher, watchers)
248 vlib_cli_output (vm, " %U", format_ip_neighbor_watcher, watcher);
255 VLIB_CLI_COMMAND (show_ip_neighbor_watchers_cmd_node, static) = {
256 .path = "show ip neighbor-watcher",
257 .function = ip_neighbor_watchers_show,
258 .short_help = "show ip neighbors-watcher",
262 static clib_error_t *
263 ip_neighbor_watch_init (vlib_main_t * vm)
265 mhash_init (&ipnw_db.ipnwdb_hash,
266 sizeof (ip_neighbor_watcher_t *), sizeof (ip_neighbor_key_t));
271 VLIB_INIT_FUNCTION (ip_neighbor_watch_init) =
273 .runs_after = VLIB_INITS("ip_neighbor_init"),
279 * fd.io coding-style-patch-verification: ON
282 * eval: (c-set-style "gnu")