misc: remove GNU Indent directives
[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-neighbor/ip_neighbor_watch.h>
20 #include <vnet/ip/ip_types_api.h>
21 #include <vnet/ethernet/ethernet_types_api.h>
22
23 #include <vnet/ip-neighbor/ip_neighbor.api_enum.h>
24 #include <vnet/ip-neighbor/ip_neighbor.api_types.h>
25
26 #include <vlibmemory/api.h>
27
28 /**
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.
32  */
33 typedef struct ip_neighbor_watch_db_t_
34 {
35   mhash_t ipnwdb_hash;
36 } ip_neighbor_watch_db_t;
37
38 static ip_neighbor_watch_db_t ipnw_db;
39
40 static uword
41 ip_neighbor_event_process (vlib_main_t * vm,
42                            vlib_node_runtime_t * rt, vlib_frame_t * f)
43 {
44   ip_neighbor_event_t *ipne, *ipnes = NULL;
45   uword event_type = ~0;
46
47   while (1)
48     {
49       vlib_process_wait_for_event (vm);
50
51       ipnes = vlib_process_get_event_data (vm, &event_type);
52
53       switch (event_type)
54         {
55         default:
56           vec_foreach (ipne, ipnes) ip_neighbor_handle_event (ipne);
57           break;
58
59         case ~0:
60           /* timeout - */
61           break;
62         }
63
64       vec_reset_length (ipnes);
65     }
66   return 0;
67 }
68
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
75
76 static clib_error_t *
77 want_ip_neighbor_events_reaper (u32 client_index)
78 {
79   ip_neighbor_key_t *key, *empty_keys = NULL;
80   ip_neighbor_watcher_t *watchers;
81   uword *v;
82   i32 pos;
83
84   /* walk the entire IP neighbour DB and removes the client's registrations */
85   mhash_foreach(key, v, &ipnw_db.ipnwdb_hash,
86   ({
87     watchers = (ip_neighbor_watcher_t*) *v;
88
89     vec_foreach_index_backwards (pos, watchers) {
90       if (watchers[pos].ipw_client == client_index)
91         vec_del1(watchers, pos);
92     }
93
94     if (vec_len(watchers) == 0)
95       vec_add1 (empty_keys, *key);
96   }));
97
98   vec_foreach (key, empty_keys)
99     mhash_unset (&ipnw_db.ipnwdb_hash, key, NULL);
100   vec_free (empty_keys);
101   return (NULL);
102 }
103
104 VL_MSG_API_REAPER_FUNCTION (want_ip_neighbor_events_reaper);
105
106 static int
107 ip_neighbor_watch_cmp (const ip_neighbor_watcher_t * w1,
108                        const ip_neighbor_watcher_t * w2)
109 {
110   return (0 == clib_memcmp (w1, w2, sizeof(*w1)));
111 }
112
113 void
114 ip_neighbor_watch (const ip_address_t * ip,
115                    u32 sw_if_index,
116                    const ip_neighbor_watcher_t * watch)
117 {
118   ip_neighbor_key_t key = {
119     .ipnk_ip = *ip,
120     .ipnk_sw_if_index = (sw_if_index == 0 ? ~0 : sw_if_index),
121   };
122   ip_neighbor_watcher_t *ipws = NULL;
123   uword *p;
124
125   p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
126
127   if (p)
128     {
129       ipws = (ip_neighbor_watcher_t*) p[0];
130
131       if (~0 != vec_search_with_function (ipws, watch,
132                                           ip_neighbor_watch_cmp))
133         /* duplicate */
134         return;
135     }
136
137   vec_add1 (ipws, *watch);
138
139   mhash_set (&ipnw_db.ipnwdb_hash, &key, (uword) ipws, NULL);
140 }
141
142 void
143 ip_neighbor_unwatch (const ip_address_t * ip,
144                      u32 sw_if_index,
145                      const ip_neighbor_watcher_t * watch)
146 {
147   ip_neighbor_key_t key = {
148     .ipnk_ip = *ip,
149     .ipnk_sw_if_index = (sw_if_index == 0 ? ~0 : sw_if_index),
150   };
151   ip_neighbor_watcher_t *ipws = NULL;
152   uword *p;
153   u32 pos;
154
155   p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
156
157   if (!p)
158     return;
159
160   ipws = (ip_neighbor_watcher_t*) p[0];
161
162   pos = vec_search_with_function (ipws, watch, ip_neighbor_watch_cmp);
163
164   if (~0 == pos)
165     return;
166
167   vec_del1 (ipws, pos);
168
169   if (vec_len(ipws) == 0)
170     mhash_unset (&ipnw_db.ipnwdb_hash, &key, NULL);
171 }
172
173 static void
174 ip_neighbor_signal (ip_neighbor_watcher_t *watchers,
175                     index_t ipni,
176                     ip_neighbor_event_flags_t flags)
177 {
178   ip_neighbor_watcher_t *watcher;
179
180   vec_foreach (watcher, watchers) {
181     ip_neighbor_event_t *ipne;
182
183     ipne = vlib_process_signal_event_data (vlib_get_main(),
184                                            ip_neighbor_event_process_node.index,
185                                            0, 1, sizeof(*ipne));
186     ipne->ipne_watch = *watcher;
187     ipne->ipne_flags = flags;
188     ip_neighbor_clone(ip_neighbor_get(ipni), &ipne->ipne_nbr);
189   }
190 }
191
192 void
193 ip_neighbor_publish (index_t ipni,
194                      ip_neighbor_event_flags_t flags)
195 {
196   const ip_neighbor_t *ipn;
197   ip_neighbor_key_t key;
198   uword *p;
199
200   ipn = ip_neighbor_get (ipni);
201
202   clib_memcpy (&key, ipn->ipn_key, sizeof (key));
203
204   /* Search the DB from longest to shortest key */
205   p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
206
207   if (p) {
208     ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni, flags);
209   }
210
211   ip_address_reset (&key.ipnk_ip);
212   p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
213
214   if (p) {
215     ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni, flags);
216   }
217
218   key.ipnk_sw_if_index = ~0;
219   p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
220
221   if (p) {
222     ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni, flags);
223   }
224 }
225
226 static clib_error_t *
227 ip_neighbor_watchers_show (vlib_main_t * vm,
228                            unformat_input_t * input,
229                            vlib_cli_command_t * cmd)
230 {
231   ip_neighbor_watcher_t *watchers, *watcher;
232   ip_neighbor_key_t *key;
233   uword *v;
234
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   return (NULL);
246 }
247
248 VLIB_CLI_COMMAND (show_ip_neighbor_watchers_cmd_node, static) = {
249   .path = "show ip neighbor-watcher",
250   .function = ip_neighbor_watchers_show,
251   .short_help = "show ip neighbors-watcher",
252 };
253
254 static clib_error_t *
255 ip_neighbor_watch_init (vlib_main_t * vm)
256 {
257   mhash_init (&ipnw_db.ipnwdb_hash,
258               sizeof (ip_neighbor_watcher_t *), sizeof (ip_neighbor_key_t));
259   return (NULL);
260 }
261
262 VLIB_INIT_FUNCTION (ip_neighbor_watch_init) =
263 {
264   .runs_after = VLIB_INITS("ip_neighbor_init"),
265 };
266
267
268 /*
269  * fd.io coding-style-patch-verification: ON
270  *
271  * Local Variables:
272  * eval: (c-set-style "gnu")
273  * End:
274  */