FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
ip_neighbor_watch.c
Go to the documentation of this file.
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 
20 #include <vnet/ip/ip_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  */
34 {
37 
39 
40 static uword
43 {
44  ip_neighbor_event_t *ipne, *ipnes = NULL;
45  uword event_type = ~0;
46 
47  while (1)
48  {
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 /* *INDENT-OFF* */
71  .function = ip_neighbor_event_process,
72  .type = VLIB_NODE_TYPE_PROCESS,
73  .name = "ip-neighbor-event",
74 };
75 /* *INDENT-ON* */
76 
77 
78 static clib_error_t *
80 {
81  ip_neighbor_key_t *key, *empty_keys = NULL;
82  ip_neighbor_watcher_t *watchers;
83  uword *v;
84  i32 pos;
85 
86  /* walk the entire IP neighbour DB and removes the client's registrations */
87  /* *INDENT-OFF* */
88  mhash_foreach(key, v, &ipnw_db.ipnwdb_hash,
89  ({
90  watchers = (ip_neighbor_watcher_t*) *v;
91 
92  vec_foreach_index_backwards (pos, watchers) {
93  if (watchers[pos].ipw_client == client_index)
94  vec_del1(watchers, pos);
95  }
96 
97  if (vec_len(watchers) == 0)
98  vec_add1 (empty_keys, *key);
99  }));
100  /* *INDENT-OFF* */
101 
102  vec_foreach (key, empty_keys)
103  mhash_unset (&ipnw_db.ipnwdb_hash, key, NULL);
104  vec_free (empty_keys);
105  return (NULL);
106 }
107 
109 
110 static int
112  const ip_neighbor_watcher_t * w2)
113 {
114  return (0 == clib_memcmp (w1, w2, sizeof(*w1)));
115 }
116 
117 void
118 ip_neighbor_watch (const ip46_address_t * ip,
121  const ip_neighbor_watcher_t * watch)
122 {
124  .ipnk_ip = *ip,
125  .ipnk_sw_if_index = (sw_if_index == 0 ? ~0 : sw_if_index),
126  .ipnk_type = type,
127  };
128  ip_neighbor_watcher_t *ipws = NULL;
129  uword *p;
130 
131  p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
132 
133  if (p)
134  {
135  ipws = (ip_neighbor_watcher_t*) p[0];
136 
137  if (~0 != vec_search_with_function (ipws, watch,
139  /* duplicate */
140  return;
141  }
142 
143  vec_add1 (ipws, *watch);
144 
145  mhash_set (&ipnw_db.ipnwdb_hash, &key, (uword) ipws, NULL);
146 }
147 
148 void
149 ip_neighbor_unwatch (const ip46_address_t * ip,
152  const ip_neighbor_watcher_t * watch)
153 {
155  .ipnk_ip = *ip,
156  .ipnk_sw_if_index = (sw_if_index == 0 ? ~0 : sw_if_index),
157  .ipnk_type = type,
158  };
159  ip_neighbor_watcher_t *ipws = NULL;
160  uword *p;
161  u32 pos;
162 
163  p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
164 
165  if (!p)
166  return;
167 
168  ipws = (ip_neighbor_watcher_t*) p[0];
169 
170  pos = vec_search_with_function (ipws, watch, ip_neighbor_watch_cmp);
171 
172  if (~0 == pos)
173  return;
174 
175  vec_del1 (ipws, pos);
176 
177  if (vec_len(ipws) == 0)
178  mhash_unset (&ipnw_db.ipnwdb_hash, &key, NULL);
179 }
180 
181 static void
183  index_t ipni,
185 {
186  ip_neighbor_watcher_t *watcher;
187 
188  vec_foreach (watcher, watchers) {
189  ip_neighbor_event_t *ipne;
190 
193  0, 1, sizeof(*ipne));
194  ipne->ipne_watch = *watcher;
195  ipne->ipne_flags = flags;
197  }
198 }
199 
200 void
203 {
204  const ip_neighbor_t *ipn;
206  uword *p;
207 
208  ipn = ip_neighbor_get (ipni);
209 
210  clib_memcpy (&key, ipn->ipn_key, sizeof (key));
211 
212  /* Search the DB from longest to shortest key */
213  p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
214 
215  if (p) {
216  ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni, flags);
217  }
218 
220  p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
221 
222  if (p) {
223  ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni, flags);
224  }
225 
226  key.ipnk_sw_if_index = ~0;
227  p = mhash_get (&ipnw_db.ipnwdb_hash, &key);
228 
229  if (p) {
230  ip_neighbor_signal ((ip_neighbor_watcher_t*) p[0], ipni, flags);
231  }
232 }
233 
234 static clib_error_t *
236  unformat_input_t * input,
237  vlib_cli_command_t * cmd)
238 {
239  ip_neighbor_watcher_t *watchers, *watcher;
241  uword *v;
242 
243  /* *INDENT-OFF* */
244  mhash_foreach(key, v, &ipnw_db.ipnwdb_hash,
245  ({
246  watchers = (ip_neighbor_watcher_t*) *v;
247 
248  ASSERT(vec_len(watchers));
249  vlib_cli_output (vm, "Key: %U", format_ip_neighbor_key, key);
250 
251  vec_foreach (watcher, watchers)
252  vlib_cli_output (vm, " %U", format_ip_neighbor_watcher, watcher);
253  }));
254  /* *INDENT-ON* */
255  return (NULL);
256 }
257 
258 /* *INDENT-OFF* */
259 VLIB_CLI_COMMAND (show_ip_neighbor_watchers_cmd_node, static) = {
260  .path = "show ip neighbor-watcher",
261  .function = ip_neighbor_watchers_show,
262  .short_help = "show ip neighbors-watcher",
263 };
264 /* *INDENT-ON* */
265 
266 static clib_error_t *
268 {
269  mhash_init (&ipnw_db.ipnwdb_hash,
270  sizeof (ip_neighbor_watcher_t *), sizeof (ip_neighbor_key_t));
271  return (NULL);
272 }
273 
274 /* *INDENT-OFF* */
276 {
277  .runs_after = VLIB_INITS("ip_neighbor_init"),
278 };
279 /* *INDENT-ON* */
280 
281 
282 /*
283  * fd.io coding-style-patch-verification: ON
284  *
285  * Local Variables:
286  * eval: (c-set-style "gnu")
287  * End:
288  */
static clib_error_t * want_ip_neighbor_events_reaper(u32 client_index)
static uword ip_neighbor_event_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: mhash.h:46
static int ip_neighbor_watch_cmp(const ip_neighbor_watcher_t *w1, const ip_neighbor_watcher_t *w2)
static clib_error_t * ip_neighbor_watch_init(vlib_main_t *vm)
vlib_node_registration_t ip_neighbor_event_process_node
(constructor) VLIB_REGISTER_NODE (ip_neighbor_event_process_node)
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:103
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:656
ip46_address_t ipnk_ip
void ip_neighbor_clone(const ip_neighbor_t *ipn, ip_neighbor_t *clone)
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:346
struct ip_neighbor_watch_db_t_ ip_neighbor_watch_db_t
Database of registered watchers The key for a watcher is {type, sw_if_index, addreess} interface=~0 /...
#define mhash_foreach(k, v, mh, body)
Definition: mhash.h:159
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
vlib_main_t * vm
Definition: in2out_ed.c:1582
ip_neighbor_event_flags_t ipne_flags
void ip_neighbor_handle_event(ip_neighbor_event_t *ipne)
From the watcher to the API to publish a new neighbor.
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
#define clib_memcpy(d, s, n)
Definition: string.h:180
static void ip46_address_reset(ip46_address_t *ip46)
Definition: ip46_address.h:74
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
A representation of an IP neighbour/peer.
void ip_neighbor_publish(index_t ipni, ip_neighbor_event_flags_t flags)
unsigned int u32
Definition: types.h:88
VL_MSG_API_REAPER_FUNCTION(want_ip_neighbor_events_reaper)
void ip_neighbor_watch(const ip46_address_t *ip, ip46_type_t type, u32 sw_if_index, const ip_neighbor_watcher_t *watch)
vl_api_fib_path_type_t type
Definition: fib_types.api:123
struct _unformat_input_t unformat_input_t
Database of registered watchers The key for a watcher is {type, sw_if_index, addreess} interface=~0 /...
static void * vlib_process_signal_event_data(vlib_main_t *vm, uword node_index, uword type_opaque, uword n_data_elts, uword n_data_elt_bytes)
Definition: node_funcs.h:909
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:875
static void ip_neighbor_signal(ip_neighbor_watcher_t *watchers, index_t ipni, ip_neighbor_event_flags_t flags)
static uword mhash_set(mhash_t *h, void *key, uword new_value, uword *old_value)
Definition: mhash.h:117
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
void mhash_init(mhash_t *h, uword n_value_bytes, uword n_key_bytes)
Definition: mhash.c:168
#define clib_memcmp(s1, s2, m1)
Definition: string.h:720
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static ip_neighbor_watch_db_t ipnw_db
#define vec_search_with_function(v, E, fn)
Search a vector for the index of the entry that matches.
Definition: vec.h:1033
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
signed int i32
Definition: types.h:77
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
enum ip_neighbor_event_flags_t_ ip_neighbor_event_flags_t
ip_neighbor_key_t * ipn_key
The idempotent key.
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
typedef key
Definition: ipsec_types.api:85
static void * vlib_process_get_event_data(vlib_main_t *vm, uword *return_event_type_opaque)
Definition: node_funcs.h:526
vl_api_address_t ip
Definition: l2.api:501
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
ip_neighbor_t * ip_neighbor_get(index_t ipni)
Definition: ip_neighbor.c:88
#define vec_foreach(var, vec)
Vector iterator.
static clib_error_t * ip_neighbor_watchers_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
#define VLIB_INITS(...)
Definition: init.h:357
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
ip_neighbor_watcher_t ipne_watch
ip46_type_t
Definition: ip46_address.h:22
void ip_neighbor_unwatch(const ip46_address_t *ip, ip46_type_t type, u32 sw_if_index, const ip_neighbor_watcher_t *watch)