Changing the IP table for an interface is an error if the interface already has an...
[vpp.git] / src / vnet / adj / adj.c
1 /*
2  * Copyright (c) 2016 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/adj/adj.h>
17 #include <vnet/adj/adj_internal.h>
18 #include <vnet/adj/adj_glean.h>
19 #include <vnet/adj/adj_midchain.h>
20 #include <vnet/adj/adj_mcast.h>
21 #include <vnet/fib/fib_node_list.h>
22
23 /*
24  * Special Adj with index zero. we need to define this since the v4 mtrie
25  * assumes an index of 0 implies the ply is empty. therefore all 'real'
26  * adjs need a non-zero index.
27  */
28 static ip_adjacency_t *special_v4_miss_adj_with_index_zero;
29
30 /* Adjacency packet/byte counters indexed by adjacency index. */
31 vlib_combined_counter_main_t adjacency_counters;
32
33 /*
34  * the single adj pool
35  */
36 ip_adjacency_t *adj_pool;
37
38 always_inline void
39 adj_poison (ip_adjacency_t * adj)
40 {
41     if (CLIB_DEBUG > 0)
42     {
43         memset (adj, 0xfe, sizeof (adj[0]));
44     }
45 }
46
47 ip_adjacency_t *
48 adj_alloc (fib_protocol_t proto)
49 {
50     ip_adjacency_t *adj;
51
52     pool_get(adj_pool, adj);
53
54     adj_poison(adj);
55
56     /* Make sure certain fields are always initialized. */
57     /* Validate adjacency counters. */
58     vlib_validate_combined_counter(&adjacency_counters,
59                                    adj_get_index(adj));
60
61     adj->rewrite_header.sw_if_index = ~0;
62     adj->n_adj = 1;
63     adj->lookup_next_index = 0;
64
65     fib_node_init(&adj->ia_node,
66                   FIB_NODE_TYPE_ADJ);
67     adj->ia_nh_proto = proto;
68     adj->ia_flags = 0;
69
70     ip4_main.lookup_main.adjacency_heap = adj_pool;
71     ip6_main.lookup_main.adjacency_heap = adj_pool;
72
73     return (adj);
74 }
75
76 static int
77 adj_index_is_special (adj_index_t adj_index)
78 {
79     if (ADJ_INDEX_INVALID == adj_index)
80         return (!0);
81
82     return (0);
83 }
84
85 /**
86  * @brief Pretty print helper function for formatting specific adjacencies.
87  * @param s - input string to format
88  * @param args - other args passed to format function such as:
89  *                 - vnet_main_t
90  *                 - ip_lookup_main_t
91  *                 - adj_index
92  */
93 u8 *
94 format_ip_adjacency (u8 * s, va_list * args)
95 {
96     format_ip_adjacency_flags_t fiaf;
97     ip_adjacency_t * adj;
98     u32 adj_index;
99
100     adj_index = va_arg (*args, u32);
101     fiaf = va_arg (*args, format_ip_adjacency_flags_t);
102     adj = adj_get(adj_index);
103   
104     switch (adj->lookup_next_index)
105     {
106     case IP_LOOKUP_NEXT_REWRITE:
107         s = format (s, "%U", format_adj_nbr, adj_index, 0);
108         break;
109     case IP_LOOKUP_NEXT_ARP:
110         s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
111         break;
112     case IP_LOOKUP_NEXT_GLEAN:
113         s = format (s, "%U", format_adj_glean, adj_index, 0);
114         break;
115     case IP_LOOKUP_NEXT_MIDCHAIN:
116         s = format (s, "%U", format_adj_midchain, adj_index, 2);
117         break;
118     case IP_LOOKUP_NEXT_MCAST:
119         s = format (s, "%U", format_adj_mcast, adj_index, 0);
120         break;
121     default:
122         break;
123     }
124
125     if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
126     {
127         vlib_counter_t counts;
128
129         vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
130         s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes);
131         s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
132         s = format (s, " node:[%d]:%U",
133                     adj->rewrite_header.node_index,
134                     format_vlib_node_name, vlib_get_main(),
135                     adj->rewrite_header.node_index);
136         s = format (s, " next:[%d]:%U",
137                     adj->rewrite_header.next_index,
138                     format_vlib_next_node_name,
139                     vlib_get_main(),
140                     adj->rewrite_header.node_index,
141                     adj->rewrite_header.next_index);
142         s = format(s, "\n children:\n  ");
143         s = fib_node_children_format(adj->ia_node.fn_children, s);
144     }
145
146     return s;
147 }
148
149 /*
150  * adj_last_lock_gone
151  *
152  * last lock/reference to the adj has gone, we no longer need it.
153  */
154 static void
155 adj_last_lock_gone (ip_adjacency_t *adj)
156 {
157     vlib_main_t * vm = vlib_get_main();
158
159     ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
160     ADJ_DBG(adj, "last-lock-gone");
161
162     vlib_worker_thread_barrier_sync (vm);
163
164     switch (adj->lookup_next_index)
165     {
166     case IP_LOOKUP_NEXT_MIDCHAIN:
167         dpo_reset(&adj->sub_type.midchain.next_dpo);
168         /* FALL THROUGH */
169     case IP_LOOKUP_NEXT_ARP:
170     case IP_LOOKUP_NEXT_REWRITE:
171         /*
172          * complete and incomplete nbr adjs
173          */
174         adj_nbr_remove(adj_get_index(adj),
175                        adj->ia_nh_proto,
176                        adj->ia_link,
177                        &adj->sub_type.nbr.next_hop,
178                        adj->rewrite_header.sw_if_index);
179         break;
180     case IP_LOOKUP_NEXT_GLEAN:
181         adj_glean_remove(adj->ia_nh_proto,
182                          adj->rewrite_header.sw_if_index);
183         break;
184     case IP_LOOKUP_NEXT_MCAST:
185         adj_mcast_remove(adj->ia_nh_proto,
186                          adj->rewrite_header.sw_if_index);
187         break;
188     default:
189         /*
190          * type not stored in any DB from which we need to remove it
191          */
192         break;
193     }
194
195     vlib_worker_thread_barrier_release(vm);
196
197     fib_node_deinit(&adj->ia_node);
198     pool_put(adj_pool, adj);
199 }
200
201 void
202 adj_lock (adj_index_t adj_index)
203 {
204     ip_adjacency_t *adj;
205
206     if (adj_index_is_special(adj_index))
207     {
208         return;
209     }
210
211     adj = adj_get(adj_index);
212     ASSERT(adj);
213
214     ADJ_DBG(adj, "lock");
215     fib_node_lock(&adj->ia_node);
216 }
217
218 void
219 adj_unlock (adj_index_t adj_index)
220 {
221     ip_adjacency_t *adj;
222
223     if (adj_index_is_special(adj_index))
224     {
225         return;
226     }
227
228     adj = adj_get(adj_index);
229     ASSERT(adj);
230
231     ADJ_DBG(adj, "unlock");
232     ASSERT(adj);
233
234     fib_node_unlock(&adj->ia_node);
235 }
236
237 u32
238 adj_child_add (adj_index_t adj_index,
239                fib_node_type_t child_type,
240                fib_node_index_t child_index)
241 {
242     ASSERT(ADJ_INDEX_INVALID != adj_index);
243     if (adj_index_is_special(adj_index))
244     {
245         return (~0);
246     }
247
248     return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
249                                adj_index,
250                                child_type,
251                                child_index));
252 }
253
254 void
255 adj_child_remove (adj_index_t adj_index,
256                   u32 sibling_index)
257 {
258     if (adj_index_is_special(adj_index))
259     {
260         return;
261     }
262
263     fib_node_child_remove(FIB_NODE_TYPE_ADJ,
264                           adj_index,
265                           sibling_index);
266 }
267
268 /**
269  * @brief Return the link type of the adjacency
270  */
271 vnet_link_t
272 adj_get_link_type (adj_index_t ai)
273 {
274     const ip_adjacency_t *adj;
275
276     adj = adj_get(ai);
277
278     return (adj->ia_link); 
279 }
280
281 /**
282  * @brief Return the sw interface index of the adjacency.
283  */
284 u32
285 adj_get_sw_if_index (adj_index_t ai)
286 {
287     const ip_adjacency_t *adj;
288
289     adj = adj_get(ai);
290
291     return (adj->rewrite_header.sw_if_index);
292 }
293
294 /**
295  * @brief Return the link type of the adjacency
296  */
297 const u8*
298 adj_get_rewrite (adj_index_t ai)
299 {
300     vnet_rewrite_header_t *rw;
301     ip_adjacency_t *adj;
302
303     adj = adj_get(ai);
304     rw = &adj->rewrite_header;
305
306     ASSERT (rw->data_bytes != 0xfefe);
307
308     return (rw->data - rw->data_bytes);
309 }
310
311 static fib_node_t *
312 adj_get_node (fib_node_index_t index)
313 {
314     ip_adjacency_t *adj;
315
316     adj = adj_get(index);
317
318     return (&adj->ia_node);
319 }
320
321 #define ADJ_FROM_NODE(_node)                                            \
322     ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
323
324 static void
325 adj_node_last_lock_gone (fib_node_t *node)
326 {
327     adj_last_lock_gone(ADJ_FROM_NODE(node));
328 }
329
330 static fib_node_back_walk_rc_t
331 adj_back_walk_notify (fib_node_t *node,
332                       fib_node_back_walk_ctx_t *ctx)
333 {
334     /*
335      * Que pasa. yo soj en el final!
336      */
337     ASSERT(0);
338
339     return (FIB_NODE_BACK_WALK_CONTINUE);
340 }
341
342 /*
343  * Adjacency's graph node virtual function table
344  */
345 static const fib_node_vft_t adj_vft = {
346     .fnv_get = adj_get_node,
347     .fnv_last_lock = adj_node_last_lock_gone,
348     .fnv_back_walk = adj_back_walk_notify,
349 };
350
351 static clib_error_t *
352 adj_module_init (vlib_main_t * vm)
353 {
354     fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
355
356     adj_nbr_module_init();
357     adj_glean_module_init();
358     adj_midchain_module_init();
359     adj_mcast_module_init();
360
361     /*
362      * one special adj to reserve index 0
363      */
364     special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
365
366     return (NULL);
367 }
368
369 VLIB_INIT_FUNCTION (adj_module_init);
370
371 static clib_error_t *
372 adj_show (vlib_main_t * vm,
373           unformat_input_t * input,
374           vlib_cli_command_t * cmd)
375 {
376     adj_index_t ai = ADJ_INDEX_INVALID;
377     u32 sw_if_index = ~0;
378
379     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
380     {
381         if (unformat (input, "%d", &ai))
382             ;
383         else if (unformat (input, "%U",
384                            unformat_vnet_sw_interface, vnet_get_main(),
385                            &sw_if_index))
386             ;
387         else
388             break;
389     }
390
391     if (ADJ_INDEX_INVALID != ai)
392     {
393         if (pool_is_free_index(adj_pool, ai))
394         {
395             vlib_cli_output (vm, "adjacency %d invalid", ai);
396             return 0;
397         }
398
399         vlib_cli_output (vm, "[@%d] %U",
400                          ai,
401                          format_ip_adjacency,  ai,
402                          FORMAT_IP_ADJACENCY_DETAIL);
403     }
404     else
405     {
406         /* *INDENT-OFF* */
407         pool_foreach_index(ai, adj_pool,
408         ({
409             if (~0 != sw_if_index &&
410                 sw_if_index != adj_get_sw_if_index(ai))
411             {
412             }
413             else
414             {
415                 vlib_cli_output (vm, "[@%d] %U",
416                                  ai,
417                                  format_ip_adjacency, ai,
418                                  FORMAT_IP_ADJACENCY_NONE);
419             }
420         }));
421         /* *INDENT-ON* */
422     }
423
424     return 0;
425 }
426
427 /*?
428  * Show all adjacencies.
429  * @cliexpar
430  * @cliexstart{sh adj}
431  * [@0]
432  * [@1]  glean: loop0
433  * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
434  * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
435  * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
436  * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
437  * @cliexend
438  ?*/
439 VLIB_CLI_COMMAND (adj_show_command, static) = {
440     .path = "show adj",
441     .short_help = "show adj [<adj_index>] [interface]",
442     .function = adj_show,
443 };