VPP-117: Fix ip4 and ip6 lookup and rewrite traces
[vpp.git] / vnet / vnet / ip / ip4_forward.c
1 /*
2  * Copyright (c) 2015 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  * ip/ip4_forward.c: IP v4 forwarding
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vnet/vnet.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/ethernet/ethernet.h>     /* for ethernet_header_t */
43 #include <vnet/ethernet/arp_packet.h>   /* for ethernet_arp_header_t */
44 #include <vnet/ppp/ppp.h>
45 #include <vnet/srp/srp.h>       /* for srp_hw_interface_class */
46 #include <vnet/api_errno.h>     /* for API error numbers */
47
48 /* This is really, really simple but stupid fib. */
49 u32
50 ip4_fib_lookup_with_table (ip4_main_t * im, u32 fib_index,
51                            ip4_address_t * dst,
52                            u32 disable_default_route)
53 {
54   ip_lookup_main_t * lm = &im->lookup_main;
55   ip4_fib_t * fib = vec_elt_at_index (im->fibs, fib_index);
56   uword * p, * hash, key;
57   i32 i, i_min, dst_address, ai;
58
59   i_min = disable_default_route ? 1 : 0;
60   dst_address = clib_mem_unaligned (&dst->data_u32, u32);
61   for (i = ARRAY_LEN (fib->adj_index_by_dst_address) - 1; i >= i_min; i--)
62     {
63       hash = fib->adj_index_by_dst_address[i];
64       if (! hash)
65         continue;
66
67       key = dst_address & im->fib_masks[i];
68       if ((p = hash_get (hash, key)) != 0)
69         {
70           ai = p[0];
71           goto done;
72         }
73     }
74     
75   /* Nothing matches in table. */
76   ai = lm->miss_adj_index;
77
78  done:
79   return ai;
80 }
81
82 static ip4_fib_t *
83 create_fib_with_table_id (ip4_main_t * im, u32 table_id)
84 {
85   ip4_fib_t * fib;
86   hash_set (im->fib_index_by_table_id, table_id, vec_len (im->fibs));
87   vec_add2 (im->fibs, fib, 1);
88   fib->table_id = table_id;
89   fib->index = fib - im->fibs;
90   fib->flow_hash_config = IP_FLOW_HASH_DEFAULT;
91   fib->fwd_classify_table_index = ~0;
92   fib->rev_classify_table_index = ~0;
93   ip4_mtrie_init (&fib->mtrie);
94   return fib;
95 }
96
97 ip4_fib_t *
98 find_ip4_fib_by_table_index_or_id (ip4_main_t * im, 
99                                    u32 table_index_or_id, u32 flags)
100 {
101   uword * p, fib_index;
102
103   fib_index = table_index_or_id;
104   if (! (flags & IP4_ROUTE_FLAG_FIB_INDEX))
105     {
106       if (table_index_or_id == ~0) {
107         table_index_or_id = 0;
108         while ((p = hash_get (im->fib_index_by_table_id, table_index_or_id))) {
109           table_index_or_id++;
110         }
111         return create_fib_with_table_id (im, table_index_or_id);
112       }
113
114       p = hash_get (im->fib_index_by_table_id, table_index_or_id);
115       if (! p)
116         return create_fib_with_table_id (im, table_index_or_id);
117       fib_index = p[0];
118     }
119   return vec_elt_at_index (im->fibs, fib_index);
120 }
121
122 static void
123 ip4_fib_init_adj_index_by_dst_address (ip_lookup_main_t * lm,
124                                        ip4_fib_t * fib,
125                                        u32 address_length)
126 {
127   hash_t * h;
128   uword max_index;
129
130   ASSERT (lm->fib_result_n_bytes >= sizeof (uword));
131   lm->fib_result_n_words = round_pow2 (lm->fib_result_n_bytes, sizeof (uword)) / sizeof (uword);
132
133   fib->adj_index_by_dst_address[address_length] =
134     hash_create (32 /* elts */, lm->fib_result_n_words * sizeof (uword));
135
136   hash_set_flags (fib->adj_index_by_dst_address[address_length],
137                   HASH_FLAG_NO_AUTO_SHRINK);
138
139   h = hash_header (fib->adj_index_by_dst_address[address_length]);
140   max_index = (hash_value_bytes (h) / sizeof (fib->new_hash_values[0])) - 1;
141
142   /* Initialize new/old hash value vectors. */
143   vec_validate_init_empty (fib->new_hash_values, max_index, ~0);
144   vec_validate_init_empty (fib->old_hash_values, max_index, ~0);
145 }
146
147 static void
148 ip4_fib_set_adj_index (ip4_main_t * im,
149                        ip4_fib_t * fib,
150                        u32 flags,
151                        u32 dst_address_u32,
152                        u32 dst_address_length,
153                        u32 adj_index)
154 {
155   ip_lookup_main_t * lm = &im->lookup_main;
156   uword * hash;
157
158   if (vec_bytes(fib->old_hash_values))
159     memset (fib->old_hash_values, ~0, vec_bytes (fib->old_hash_values));
160   if (vec_bytes(fib->new_hash_values))
161     memset (fib->new_hash_values, ~0, vec_bytes (fib->new_hash_values));
162   fib->new_hash_values[0] = adj_index;
163
164   /* Make sure adj index is valid. */
165   if (CLIB_DEBUG > 0)
166     (void) ip_get_adjacency (lm, adj_index);
167
168   hash = fib->adj_index_by_dst_address[dst_address_length];
169
170   hash = _hash_set3 (hash, dst_address_u32,
171                      fib->new_hash_values,
172                      fib->old_hash_values);
173
174   fib->adj_index_by_dst_address[dst_address_length] = hash;
175
176   if (vec_len (im->add_del_route_callbacks) > 0)
177     {
178       ip4_add_del_route_callback_t * cb;
179       ip4_address_t d;
180       uword * p;
181
182       d.data_u32 = dst_address_u32;
183       vec_foreach (cb, im->add_del_route_callbacks)
184         if ((flags & cb->required_flags) == cb->required_flags)
185           cb->function (im, cb->function_opaque,
186                         fib, flags,
187                         &d, dst_address_length,
188                         fib->old_hash_values,
189                         fib->new_hash_values);
190
191       p = hash_get (hash, dst_address_u32);
192       clib_memcpy (p, fib->new_hash_values, vec_bytes (fib->new_hash_values));
193     }
194 }
195
196 void ip4_add_del_route (ip4_main_t * im, ip4_add_del_route_args_t * a)
197 {
198   ip_lookup_main_t * lm = &im->lookup_main;
199   ip4_fib_t * fib;
200   u32 dst_address, dst_address_length, adj_index, old_adj_index;
201   uword * hash, is_del;
202   ip4_add_del_route_callback_t * cb;
203
204   /* Either create new adjacency or use given one depending on arguments. */
205   if (a->n_add_adj > 0)
206     {
207       ip_add_adjacency (lm, a->add_adj, a->n_add_adj, &adj_index);
208       ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
209     }
210   else
211     adj_index = a->adj_index;
212
213   dst_address = a->dst_address.data_u32;
214   dst_address_length = a->dst_address_length;
215   fib = find_ip4_fib_by_table_index_or_id (im, a->table_index_or_table_id, a->flags);
216
217   ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
218   dst_address &= im->fib_masks[dst_address_length];
219
220   if (! fib->adj_index_by_dst_address[dst_address_length])
221     ip4_fib_init_adj_index_by_dst_address (lm, fib, dst_address_length);
222
223   hash = fib->adj_index_by_dst_address[dst_address_length];
224
225   is_del = (a->flags & IP4_ROUTE_FLAG_DEL) != 0;
226
227   if (is_del)
228     {
229       fib->old_hash_values[0] = ~0;
230       hash = _hash_unset (hash, dst_address, fib->old_hash_values);
231       fib->adj_index_by_dst_address[dst_address_length] = hash;
232
233       if (vec_len (im->add_del_route_callbacks) > 0
234           && fib->old_hash_values[0] != ~0) /* make sure destination was found in hash */
235         {
236           fib->new_hash_values[0] = ~0;
237           vec_foreach (cb, im->add_del_route_callbacks)
238             if ((a->flags & cb->required_flags) == cb->required_flags)
239               cb->function (im, cb->function_opaque,
240                             fib, a->flags,
241                             &a->dst_address, dst_address_length,
242                             fib->old_hash_values,
243                             fib->new_hash_values);
244         }
245     }
246   else
247     ip4_fib_set_adj_index (im, fib, a->flags, dst_address, dst_address_length,
248                            adj_index);
249
250   old_adj_index = fib->old_hash_values[0];
251
252   /* Avoid spurious reference count increments */
253   if (old_adj_index == adj_index
254       && adj_index != ~0
255       && !(a->flags & IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY))
256     {
257       ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
258       if (adj->share_count > 0)
259         adj->share_count --;
260     }
261
262   ip4_fib_mtrie_add_del_route (fib, a->dst_address, dst_address_length,
263                                is_del ? old_adj_index : adj_index,
264                                is_del);
265
266   /* Delete old adjacency index if present and changed. */
267   if (! (a->flags & IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY)
268       && old_adj_index != ~0
269       && old_adj_index != adj_index)
270     ip_del_adjacency (lm, old_adj_index);
271 }
272
273 void
274 ip4_add_del_route_next_hop (ip4_main_t * im,
275                             u32 flags,
276                             ip4_address_t * dst_address,
277                             u32 dst_address_length,
278                             ip4_address_t * next_hop,
279                             u32 next_hop_sw_if_index,
280                             u32 next_hop_weight, u32 adj_index, 
281                             u32 explicit_fib_index)
282 {
283   vnet_main_t * vnm = vnet_get_main();
284   ip_lookup_main_t * lm = &im->lookup_main;
285   u32 fib_index;
286   ip4_fib_t * fib;
287   u32 dst_address_u32, old_mp_adj_index, new_mp_adj_index;
288   u32 dst_adj_index, nh_adj_index;
289   uword * dst_hash, * dst_result;
290   uword * nh_hash, * nh_result;
291   ip_adjacency_t * dst_adj;
292   ip_multipath_adjacency_t * old_mp, * new_mp;
293   int is_del = (flags & IP4_ROUTE_FLAG_DEL) != 0;
294   int is_interface_next_hop;
295   clib_error_t * error = 0;
296
297   if (explicit_fib_index == (u32)~0)
298       fib_index = vec_elt (im->fib_index_by_sw_if_index, next_hop_sw_if_index);
299   else
300       fib_index = explicit_fib_index;
301
302   fib = vec_elt_at_index (im->fibs, fib_index);
303   
304   /* Lookup next hop to be added or deleted. */
305   is_interface_next_hop = next_hop->data_u32 == 0;
306   if (adj_index == (u32)~0)
307     {
308       if (is_interface_next_hop)
309         {
310           nh_result = hash_get (im->interface_route_adj_index_by_sw_if_index, next_hop_sw_if_index);
311           if (nh_result)
312             nh_adj_index = *nh_result;
313           else
314             {
315               ip_adjacency_t * adj;
316               adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
317                                       &nh_adj_index);
318               ip4_adjacency_set_interface_route (vnm, adj, next_hop_sw_if_index, /* if_address_index */ ~0);
319               ip_call_add_del_adjacency_callbacks (lm, nh_adj_index, /* is_del */ 0);
320               hash_set (im->interface_route_adj_index_by_sw_if_index, next_hop_sw_if_index, nh_adj_index);
321             }
322         }
323       else
324         {
325           nh_hash = fib->adj_index_by_dst_address[32];
326           nh_result = hash_get (nh_hash, next_hop->data_u32);
327           
328           /* Next hop must be known. */
329           if (! nh_result)
330             {
331               ip_adjacency_t * adj;
332
333               nh_adj_index = ip4_fib_lookup_with_table (im, fib_index,
334                                                         next_hop, 0);
335               adj = ip_get_adjacency (lm, nh_adj_index);
336               /* if ARP interface adjacencty is present, we need to
337                  install ARP adjaceny for specific next hop */
338               if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
339                   adj->arp.next_hop.ip4.as_u32 == 0)
340                 {
341                   nh_adj_index = vnet_arp_glean_add(fib_index, next_hop);
342                 }
343               else
344                 {
345                   /* Next hop is not known, so create indirect adj */
346                   ip_adjacency_t add_adj;
347                   add_adj.lookup_next_index = IP_LOOKUP_NEXT_INDIRECT;
348                   add_adj.indirect.next_hop.ip4.as_u32 = next_hop->as_u32;
349                   add_adj.explicit_fib_index = explicit_fib_index;
350                   ip_add_adjacency (lm, &add_adj, 1, &nh_adj_index);
351                 }
352             }
353           else
354             nh_adj_index = *nh_result;
355         }
356     }
357   else
358     {
359       nh_adj_index = adj_index;
360     }
361   ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
362   dst_address_u32 = dst_address->data_u32 & im->fib_masks[dst_address_length];
363
364   dst_hash = fib->adj_index_by_dst_address[dst_address_length];
365   dst_result = hash_get (dst_hash, dst_address_u32);
366   if (dst_result)
367     {
368       dst_adj_index = dst_result[0];
369       dst_adj = ip_get_adjacency (lm, dst_adj_index);
370     }
371   else
372     {
373       /* For deletes destination must be known. */
374       if (is_del)
375         {
376           vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
377           error = clib_error_return (0, "unknown destination %U/%d",
378                                      format_ip4_address, dst_address,
379                                      dst_address_length);
380           goto done;
381         }
382
383       dst_adj_index = ~0;
384       dst_adj = 0;
385     }
386
387   /* Ignore adds of X/32 with next hop of X. */
388   if (! is_del
389       && dst_address_length == 32
390       && dst_address->data_u32 == next_hop->data_u32 
391       && adj_index != (u32)~0)
392     {
393       vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
394       error = clib_error_return (0, "prefix matches next hop %U/%d",
395                                  format_ip4_address, dst_address,
396                                  dst_address_length);
397       goto done;
398     }
399
400   /* Destination is not known and default weight is set so add route
401      to existing non-multipath adjacency */
402   if (dst_adj_index == ~0 && next_hop_weight == 1 && next_hop_sw_if_index == ~0)
403     {
404       /* create new adjacency */
405       ip4_add_del_route_args_t a;
406       a.table_index_or_table_id = fib_index;
407       a.flags = ((is_del ? IP4_ROUTE_FLAG_DEL : IP4_ROUTE_FLAG_ADD)
408                  | IP4_ROUTE_FLAG_FIB_INDEX
409                  | IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY
410                  | (flags & (IP4_ROUTE_FLAG_NO_REDISTRIBUTE
411                              | IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP)));
412       a.dst_address = dst_address[0];
413       a.dst_address_length = dst_address_length;
414       a.adj_index = nh_adj_index;
415       a.add_adj = 0;
416       a.n_add_adj = 0;
417
418       ip4_add_del_route (im, &a);
419
420       goto done;
421     }
422
423   old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
424
425   if (! ip_multipath_adjacency_add_del_next_hop
426       (lm, is_del,
427        old_mp_adj_index,
428        nh_adj_index,
429        next_hop_weight,
430        &new_mp_adj_index))
431     {
432       vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
433       error = clib_error_return (0, "requested deleting next-hop %U not found in multi-path",
434                                  format_ip4_address, next_hop);
435       goto done;
436     }
437   
438   old_mp = new_mp = 0;
439   if (old_mp_adj_index != ~0)
440     old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
441   if (new_mp_adj_index != ~0)
442     new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
443
444   if (old_mp != new_mp)
445     {
446       ip4_add_del_route_args_t a;
447       a.table_index_or_table_id = fib_index;
448       a.flags = ((is_del && ! new_mp ? IP4_ROUTE_FLAG_DEL : IP4_ROUTE_FLAG_ADD)
449                  | IP4_ROUTE_FLAG_FIB_INDEX
450                  | IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY
451                  | (flags & (IP4_ROUTE_FLAG_NO_REDISTRIBUTE | IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP)));
452       a.dst_address = dst_address[0];
453       a.dst_address_length = dst_address_length;
454       a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
455       a.add_adj = 0;
456       a.n_add_adj = 0;
457
458       ip4_add_del_route (im, &a);
459     }
460
461  done:
462   if (error)
463     clib_error_report (error);
464 }
465
466 void *
467 ip4_get_route (ip4_main_t * im,
468                u32 table_index_or_table_id,
469                u32 flags,
470                u8 * address,
471                u32 address_length)
472 {
473   ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
474   u32 dst_address = * (u32 *) address;
475   uword * hash, * p;
476
477   ASSERT (address_length < ARRAY_LEN (im->fib_masks));
478   dst_address &= im->fib_masks[address_length];
479
480   hash = fib->adj_index_by_dst_address[address_length];
481   p = hash_get (hash, dst_address);
482   return (void *) p;
483 }
484
485 void
486 ip4_foreach_matching_route (ip4_main_t * im,
487                             u32 table_index_or_table_id,
488                             u32 flags,
489                             ip4_address_t * address,
490                             u32 address_length,
491                             ip4_address_t ** results,
492                             u8 ** result_lengths)
493 {
494   ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
495   u32 dst_address = address->data_u32;
496   u32 this_length = address_length;
497   
498   if (*results)
499     _vec_len (*results) = 0;
500   if (*result_lengths)
501     _vec_len (*result_lengths) = 0;
502
503   while (this_length <= 32 && vec_len (results) == 0)
504     {
505       uword k, v;
506       hash_foreach (k, v, fib->adj_index_by_dst_address[this_length], ({
507         if (0 == ((k ^ dst_address) & im->fib_masks[address_length]))
508           {
509             ip4_address_t a;
510             a.data_u32 = k;
511             vec_add1 (*results, a);
512             vec_add1 (*result_lengths, this_length);
513           }
514       }));
515
516       this_length++;
517     }
518 }
519
520 void ip4_maybe_remap_adjacencies (ip4_main_t * im,
521                                   u32 table_index_or_table_id,
522                                   u32 flags)
523 {
524   ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
525   ip_lookup_main_t * lm = &im->lookup_main;
526   u32 i, l;
527   ip4_address_t a;
528   ip4_add_del_route_callback_t * cb;
529   static ip4_address_t * to_delete;
530
531   if (lm->n_adjacency_remaps == 0)
532     return;
533
534   for (l = 0; l <= 32; l++)
535     {
536       hash_pair_t * p;
537       uword * hash = fib->adj_index_by_dst_address[l];
538
539       if (hash_elts (hash) == 0)
540         continue;
541
542       if (to_delete)
543         _vec_len (to_delete) = 0;
544
545       hash_foreach_pair (p, hash, ({
546         u32 adj_index = p->value[0];
547         u32 m = vec_elt (lm->adjacency_remap_table, adj_index);
548
549         if (m)
550           {
551             /* Record destination address from hash key. */
552             a.data_u32 = p->key;
553
554             /* New adjacency points to nothing: so delete prefix. */
555             if (m == ~0)
556               vec_add1 (to_delete, a);
557             else
558               {
559                 /* Remap to new adjacency. */
560                 clib_memcpy (fib->old_hash_values, p->value, vec_bytes (fib->old_hash_values));
561
562                 /* Set new adjacency value. */
563                 fib->new_hash_values[0] = p->value[0] = m - 1;
564
565                 vec_foreach (cb, im->add_del_route_callbacks)
566                   if ((flags & cb->required_flags) == cb->required_flags)
567                     cb->function (im, cb->function_opaque,
568                                   fib, flags | IP4_ROUTE_FLAG_ADD,
569                                   &a, l,
570                                   fib->old_hash_values,
571                                   fib->new_hash_values);
572               }
573           }
574       }));
575
576       fib->new_hash_values[0] = ~0;
577       for (i = 0; i < vec_len (to_delete); i++)
578         {
579           hash = _hash_unset (hash, to_delete[i].data_u32, fib->old_hash_values);
580           vec_foreach (cb, im->add_del_route_callbacks)
581             if ((flags & cb->required_flags) == cb->required_flags)
582               cb->function (im, cb->function_opaque,
583                             fib, flags | IP4_ROUTE_FLAG_DEL,
584                             &a, l,
585                             fib->old_hash_values,
586                             fib->new_hash_values);
587         }
588     }
589
590   /* Also remap adjacencies in mtrie. */
591   ip4_mtrie_maybe_remap_adjacencies (lm, &fib->mtrie);
592
593   /* Reset mapping table. */
594   vec_zero (lm->adjacency_remap_table);
595
596   /* All remaps have been performed. */
597   lm->n_adjacency_remaps = 0;
598 }
599
600 void ip4_delete_matching_routes (ip4_main_t * im,
601                                  u32 table_index_or_table_id,
602                                  u32 flags,
603                                  ip4_address_t * address,
604                                  u32 address_length)
605 {
606   static ip4_address_t * matching_addresses;
607   static u8 * matching_address_lengths;
608   u32 l, i;
609   ip4_add_del_route_args_t a;
610
611   a.flags = IP4_ROUTE_FLAG_DEL | IP4_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
612   a.table_index_or_table_id = table_index_or_table_id;
613   a.adj_index = ~0;
614   a.add_adj = 0;
615   a.n_add_adj = 0;
616
617   for (l = address_length + 1; l <= 32; l++)
618     {
619       ip4_foreach_matching_route (im, table_index_or_table_id, flags,
620                                   address,
621                                   l,
622                                   &matching_addresses,
623                                   &matching_address_lengths);
624       for (i = 0; i < vec_len (matching_addresses); i++)
625         {
626           a.dst_address = matching_addresses[i];
627           a.dst_address_length = matching_address_lengths[i];
628           ip4_add_del_route (im, &a);
629         }
630     }
631
632   ip4_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
633 }
634
635 void
636 ip4_forward_next_trace (vlib_main_t * vm,
637                         vlib_node_runtime_t * node,
638                         vlib_frame_t * frame,
639                         vlib_rx_or_tx_t which_adj_index);
640
641 always_inline uword
642 ip4_lookup_inline (vlib_main_t * vm,
643                    vlib_node_runtime_t * node,
644                    vlib_frame_t * frame,
645                    int lookup_for_responses_to_locally_received_packets,
646                    int is_indirect)
647 {
648   ip4_main_t * im = &ip4_main;
649   ip_lookup_main_t * lm = &im->lookup_main;
650   vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
651   u32 n_left_from, n_left_to_next, * from, * to_next;
652   ip_lookup_next_t next;
653   u32 cpu_index = os_get_cpu_number();
654
655   from = vlib_frame_vector_args (frame);
656   n_left_from = frame->n_vectors;
657   next = node->cached_next_index;
658
659   while (n_left_from > 0)
660     {
661       vlib_get_next_frame (vm, node, next,
662                            to_next, n_left_to_next);
663
664       while (n_left_from >= 4 && n_left_to_next >= 2)
665         {
666           vlib_buffer_t * p0, * p1;
667           ip4_header_t * ip0, * ip1;
668           __attribute__((unused)) tcp_header_t * tcp0, * tcp1;
669           ip_lookup_next_t next0, next1;
670           ip_adjacency_t * adj0, * adj1;
671           ip4_fib_mtrie_t * mtrie0, * mtrie1;
672           ip4_fib_mtrie_leaf_t leaf0, leaf1;
673           ip4_address_t * dst_addr0, *dst_addr1;
674           __attribute__((unused)) u32 pi0, fib_index0, adj_index0, is_tcp_udp0;
675           __attribute__((unused)) u32 pi1, fib_index1, adj_index1, is_tcp_udp1;
676           u32 flow_hash_config0, flow_hash_config1;
677           u32 hash_c0, hash_c1;
678           u32 wrong_next;
679
680           /* Prefetch next iteration. */
681           {
682             vlib_buffer_t * p2, * p3;
683
684             p2 = vlib_get_buffer (vm, from[2]);
685             p3 = vlib_get_buffer (vm, from[3]);
686
687             vlib_prefetch_buffer_header (p2, LOAD);
688             vlib_prefetch_buffer_header (p3, LOAD);
689
690             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
691             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
692           }
693
694           pi0 = to_next[0] = from[0];
695           pi1 = to_next[1] = from[1];
696
697           p0 = vlib_get_buffer (vm, pi0);
698           p1 = vlib_get_buffer (vm, pi1);
699
700           ip0 = vlib_buffer_get_current (p0);
701           ip1 = vlib_buffer_get_current (p1);
702
703           if (is_indirect)
704             {
705               ip_adjacency_t * iadj0, * iadj1;
706               iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
707               iadj1 = ip_get_adjacency (lm, vnet_buffer(p1)->ip.adj_index[VLIB_TX]);
708               dst_addr0 = &iadj0->indirect.next_hop.ip4;
709               dst_addr1 = &iadj1->indirect.next_hop.ip4;
710             }
711           else
712             {
713               dst_addr0 = &ip0->dst_address;
714               dst_addr1 = &ip1->dst_address;
715             }
716
717           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
718           fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
719           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
720             fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
721           fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
722             fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
723
724
725           if (! lookup_for_responses_to_locally_received_packets)
726             {
727               mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
728               mtrie1 = &vec_elt_at_index (im->fibs, fib_index1)->mtrie;
729
730               leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
731
732               leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 0);
733               leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 0);
734             }
735
736           tcp0 = (void *) (ip0 + 1);
737           tcp1 = (void *) (ip1 + 1);
738
739           is_tcp_udp0 = (ip0->protocol == IP_PROTOCOL_TCP
740                          || ip0->protocol == IP_PROTOCOL_UDP);
741           is_tcp_udp1 = (ip1->protocol == IP_PROTOCOL_TCP
742                          || ip1->protocol == IP_PROTOCOL_UDP);
743
744           if (! lookup_for_responses_to_locally_received_packets)
745             {
746               leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 1);
747               leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 1);
748             }
749
750           if (! lookup_for_responses_to_locally_received_packets)
751             {
752               leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
753               leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 2);
754             }
755
756           if (! lookup_for_responses_to_locally_received_packets)
757             {
758               leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
759               leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 3);
760             }
761
762           if (lookup_for_responses_to_locally_received_packets)
763             {
764               adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_RX];
765               adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_RX];
766             }
767           else
768             {
769               /* Handle default route. */
770               leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
771               leaf1 = (leaf1 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie1->default_leaf : leaf1);
772
773               adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
774               adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
775             }
776
777           ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
778                                                            dst_addr0,
779                                                            /* no_default_route */ 0));
780           ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1,
781                                                            dst_addr1,
782                                                            /* no_default_route */ 0));
783           adj0 = ip_get_adjacency (lm, adj_index0);
784           adj1 = ip_get_adjacency (lm, adj_index1);
785
786           next0 = adj0->lookup_next_index;
787           next1 = adj1->lookup_next_index;
788
789           /* Use flow hash to compute multipath adjacency. */
790           hash_c0 = vnet_buffer (p0)->ip.flow_hash = 0;
791           hash_c1 = vnet_buffer (p1)->ip.flow_hash = 0;
792           if (PREDICT_FALSE (adj0->n_adj > 1))
793             {
794               flow_hash_config0 = 
795                 vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
796               hash_c0 = vnet_buffer (p0)->ip.flow_hash = 
797                 ip4_compute_flow_hash (ip0, flow_hash_config0);
798             }
799           if (PREDICT_FALSE(adj1->n_adj > 1))
800             {
801               flow_hash_config1 = 
802                 vec_elt_at_index (im->fibs, fib_index1)->flow_hash_config;
803               hash_c1 = vnet_buffer (p1)->ip.flow_hash = 
804                 ip4_compute_flow_hash (ip1, flow_hash_config1);
805             }
806
807           ASSERT (adj0->n_adj > 0);
808           ASSERT (adj1->n_adj > 0);
809           ASSERT (is_pow2 (adj0->n_adj));
810           ASSERT (is_pow2 (adj1->n_adj));
811           adj_index0 += (hash_c0 & (adj0->n_adj - 1));
812           adj_index1 += (hash_c1 & (adj1->n_adj - 1));
813
814           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
815           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
816
817           vlib_increment_combined_counter 
818               (cm, cpu_index, adj_index0, 1,
819                vlib_buffer_length_in_chain (vm, p0) 
820                + sizeof(ethernet_header_t));
821           vlib_increment_combined_counter 
822               (cm, cpu_index, adj_index1, 1,
823                vlib_buffer_length_in_chain (vm, p1)
824                + sizeof(ethernet_header_t));
825
826           from += 2;
827           to_next += 2;
828           n_left_to_next -= 2;
829           n_left_from -= 2;
830
831           wrong_next = (next0 != next) + 2*(next1 != next);
832           if (PREDICT_FALSE (wrong_next != 0))
833             {
834               switch (wrong_next)
835                 {
836                 case 1:
837                   /* A B A */
838                   to_next[-2] = pi1;
839                   to_next -= 1;
840                   n_left_to_next += 1;
841                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
842                   break;
843
844                 case 2:
845                   /* A A B */
846                   to_next -= 1;
847                   n_left_to_next += 1;
848                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
849                   break;
850
851                 case 3:
852                   /* A B C */
853                   to_next -= 2;
854                   n_left_to_next += 2;
855                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
856                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
857                   if (next0 == next1)
858                     {
859                       /* A B B */
860                       vlib_put_next_frame (vm, node, next, n_left_to_next);
861                       next = next1;
862                       vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
863                     }
864                 }
865             }
866         }
867     
868       while (n_left_from > 0 && n_left_to_next > 0)
869         {
870           vlib_buffer_t * p0;
871           ip4_header_t * ip0;
872           __attribute__((unused)) tcp_header_t * tcp0;
873           ip_lookup_next_t next0;
874           ip_adjacency_t * adj0;
875           ip4_fib_mtrie_t * mtrie0;
876           ip4_fib_mtrie_leaf_t leaf0;
877           ip4_address_t * dst_addr0;
878           __attribute__((unused)) u32 pi0, fib_index0, adj_index0, is_tcp_udp0;
879           u32 flow_hash_config0, hash_c0;
880
881           pi0 = from[0];
882           to_next[0] = pi0;
883
884           p0 = vlib_get_buffer (vm, pi0);
885
886           ip0 = vlib_buffer_get_current (p0);
887
888           if (is_indirect)
889             {
890               ip_adjacency_t * iadj0;
891               iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
892               dst_addr0 = &iadj0->indirect.next_hop.ip4;
893             }
894           else
895             {
896               dst_addr0 = &ip0->dst_address;
897             }
898
899           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
900           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
901             fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
902
903           if (! lookup_for_responses_to_locally_received_packets)
904             {
905               mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
906
907               leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
908
909               leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 0);
910             }
911
912           tcp0 = (void *) (ip0 + 1);
913
914           is_tcp_udp0 = (ip0->protocol == IP_PROTOCOL_TCP
915                          || ip0->protocol == IP_PROTOCOL_UDP);
916
917           if (! lookup_for_responses_to_locally_received_packets)
918             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 1);
919
920           if (! lookup_for_responses_to_locally_received_packets)
921             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
922
923           if (! lookup_for_responses_to_locally_received_packets)
924             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
925
926           if (lookup_for_responses_to_locally_received_packets)
927             adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_RX];
928           else
929             {
930               /* Handle default route. */
931               leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
932               adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
933             }
934
935           ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
936                                                            dst_addr0,
937                                                            /* no_default_route */ 0));
938
939           adj0 = ip_get_adjacency (lm, adj_index0);
940
941           next0 = adj0->lookup_next_index;
942
943           /* Use flow hash to compute multipath adjacency. */
944           hash_c0 = vnet_buffer (p0)->ip.flow_hash = 0;
945           if (PREDICT_FALSE(adj0->n_adj > 1))
946             {
947               flow_hash_config0 = 
948                 vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
949
950               hash_c0 = vnet_buffer (p0)->ip.flow_hash = 
951                 ip4_compute_flow_hash (ip0, flow_hash_config0);
952             }
953
954           ASSERT (adj0->n_adj > 0);
955           ASSERT (is_pow2 (adj0->n_adj));
956           adj_index0 += (hash_c0 & (adj0->n_adj - 1));
957
958           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
959
960           vlib_increment_combined_counter 
961               (cm, cpu_index, adj_index0, 1,
962                vlib_buffer_length_in_chain (vm, p0)
963                + sizeof(ethernet_header_t));
964
965           from += 1;
966           to_next += 1;
967           n_left_to_next -= 1;
968           n_left_from -= 1;
969
970           if (PREDICT_FALSE (next0 != next))
971             {
972               n_left_to_next += 1;
973               vlib_put_next_frame (vm, node, next, n_left_to_next);
974               next = next0;
975               vlib_get_next_frame (vm, node, next,
976                                    to_next, n_left_to_next);
977               to_next[0] = pi0;
978               to_next += 1;
979               n_left_to_next -= 1;
980             }
981         }
982
983       vlib_put_next_frame (vm, node, next, n_left_to_next);
984     }
985
986   if (node->flags & VLIB_NODE_FLAG_TRACE)
987     ip4_forward_next_trace(vm, node, frame, VLIB_TX);
988
989   return frame->n_vectors;
990 }
991
992 static uword
993 ip4_lookup (vlib_main_t * vm,
994             vlib_node_runtime_t * node,
995             vlib_frame_t * frame)
996 {
997   return ip4_lookup_inline (vm, node, frame,
998                             /* lookup_for_responses_to_locally_received_packets */ 0,
999                             /* is_indirect */ 0);
1000
1001 }
1002
1003 void ip4_adjacency_set_interface_route (vnet_main_t * vnm,
1004                                         ip_adjacency_t * adj,
1005                                         u32 sw_if_index,
1006                                         u32 if_address_index)
1007 {
1008   vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
1009   ip_lookup_next_t n;
1010   vnet_l3_packet_type_t packet_type;
1011   u32 node_index;
1012
1013   if (hw->hw_class_index == ethernet_hw_interface_class.index
1014       || hw->hw_class_index == srp_hw_interface_class.index)
1015     {
1016       /* 
1017        * We have a bit of a problem in this case. ip4-arp uses
1018        * the rewrite_header.next_index to hand pkts to the
1019        * indicated inteface output node. We can end up in
1020        * ip4_rewrite_local, too, which also pays attention to 
1021        * rewrite_header.next index. Net result: a hack in
1022        * ip4_rewrite_local...
1023        */
1024       n = IP_LOOKUP_NEXT_ARP;
1025       node_index = ip4_arp_node.index;
1026       adj->if_address_index = if_address_index;
1027       adj->arp.next_hop.ip4.as_u32 = 0;
1028       ip46_address_reset(&adj->arp.next_hop);
1029       packet_type = VNET_L3_PACKET_TYPE_ARP;
1030     }
1031   else
1032     {
1033       n = IP_LOOKUP_NEXT_REWRITE;
1034       node_index = ip4_rewrite_node.index;
1035       packet_type = VNET_L3_PACKET_TYPE_IP4;
1036     }
1037
1038   adj->lookup_next_index = n;
1039   vnet_rewrite_for_sw_interface
1040     (vnm,
1041      packet_type,
1042      sw_if_index,
1043      node_index,
1044      VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
1045      &adj->rewrite_header,
1046      sizeof (adj->rewrite_data));
1047 }
1048
1049 static void
1050 ip4_add_interface_routes (u32 sw_if_index,
1051                           ip4_main_t * im, u32 fib_index,
1052                           ip_interface_address_t * a)
1053 {
1054   vnet_main_t * vnm = vnet_get_main();
1055   ip_lookup_main_t * lm = &im->lookup_main;
1056   ip_adjacency_t * adj;
1057   ip4_address_t * address = ip_interface_address_get_address (lm, a);
1058   ip4_add_del_route_args_t x;
1059   vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
1060   u32 classify_table_index;
1061
1062   /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1063   x.table_index_or_table_id = fib_index;
1064   x.flags = (IP4_ROUTE_FLAG_ADD
1065              | IP4_ROUTE_FLAG_FIB_INDEX
1066              | IP4_ROUTE_FLAG_NO_REDISTRIBUTE);
1067   x.dst_address = address[0];
1068   x.dst_address_length = a->address_length;
1069   x.n_add_adj = 0;
1070   x.add_adj = 0;
1071
1072   a->neighbor_probe_adj_index = ~0;
1073   if (a->address_length < 32)
1074     {
1075       adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1076                               &x.adj_index);
1077       ip4_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
1078       ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1079       ip4_add_del_route (im, &x);
1080       a->neighbor_probe_adj_index = x.adj_index;
1081     }
1082   
1083   /* Add e.g. 1.1.1.1/32 as local to this host. */
1084   adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1085                           &x.adj_index);
1086   
1087   classify_table_index = ~0;
1088   if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
1089     classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
1090   if (classify_table_index != (u32) ~0)
1091     {
1092       adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
1093       adj->classify.table_index = classify_table_index;
1094     }
1095   else
1096     adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
1097   
1098   adj->if_address_index = a - lm->if_address_pool;
1099   adj->rewrite_header.sw_if_index = sw_if_index;
1100   adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
1101   /* 
1102    * Local adjs are never to be rewritten. Spoofed pkts w/ src = dst = local
1103    * fail an RPF-ish check, but still go thru the rewrite code...
1104    */
1105   adj->rewrite_header.data_bytes = 0;
1106
1107   ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1108   x.dst_address_length = 32;
1109   ip4_add_del_route (im, &x);
1110 }
1111
1112 static void
1113 ip4_del_interface_routes (ip4_main_t * im, u32 fib_index, ip4_address_t * address, u32 address_length)
1114 {
1115   ip4_add_del_route_args_t x;
1116
1117   /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1118   x.table_index_or_table_id = fib_index;
1119   x.flags = (IP4_ROUTE_FLAG_DEL
1120              | IP4_ROUTE_FLAG_FIB_INDEX
1121              | IP4_ROUTE_FLAG_NO_REDISTRIBUTE);
1122   x.dst_address = address[0];
1123   x.dst_address_length = address_length;
1124   x.adj_index = ~0;
1125   x.n_add_adj = 0;
1126   x.add_adj = 0;
1127
1128   if (address_length < 32)
1129     ip4_add_del_route (im, &x);
1130
1131   x.dst_address_length = 32;
1132   ip4_add_del_route (im, &x);
1133
1134   ip4_delete_matching_routes (im,
1135                               fib_index,
1136                               IP4_ROUTE_FLAG_FIB_INDEX,
1137                               address,
1138                               address_length);
1139 }
1140
1141 typedef struct {
1142     u32 sw_if_index;
1143     ip4_address_t address;
1144     u32 length;
1145 } ip4_interface_address_t;
1146
1147 static clib_error_t *
1148 ip4_add_del_interface_address_internal (vlib_main_t * vm,
1149                                         u32 sw_if_index,
1150                                         ip4_address_t * new_address,
1151                                         u32 new_length,
1152                                         u32 redistribute,
1153                                         u32 insert_routes,
1154                                         u32 is_del);
1155
1156 static clib_error_t *
1157 ip4_add_del_interface_address_internal (vlib_main_t * vm,
1158                                         u32 sw_if_index,
1159                                         ip4_address_t * address,
1160                                         u32 address_length,
1161                                         u32 redistribute,
1162                                         u32 insert_routes,
1163                                         u32 is_del)
1164 {
1165   vnet_main_t * vnm = vnet_get_main();
1166   ip4_main_t * im = &ip4_main;
1167   ip_lookup_main_t * lm = &im->lookup_main;
1168   clib_error_t * error = 0;
1169   u32 if_address_index, elts_before;
1170   ip4_address_fib_t ip4_af, * addr_fib = 0;
1171
1172   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1173   ip4_addr_fib_init (&ip4_af, address,
1174                      vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1175   vec_add1 (addr_fib, ip4_af);
1176
1177   /* When adding an address check that it does not conflict with an existing address. */
1178   if (! is_del)
1179     {
1180       ip_interface_address_t * ia;
1181       foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index, 
1182                                     0 /* honor unnumbered */,
1183       ({
1184         ip4_address_t * x = ip_interface_address_get_address (&im->lookup_main, ia);
1185
1186         if (ip4_destination_matches_route (im, address, x, ia->address_length)
1187             || ip4_destination_matches_route (im, x, address, address_length))
1188           return clib_error_create ("failed to add %U which conflicts with %U for interface %U",
1189                                     format_ip4_address_and_length, address, address_length,
1190                                     format_ip4_address_and_length, x, ia->address_length,
1191                                     format_vnet_sw_if_index_name, vnm, sw_if_index);
1192       }));
1193     }
1194
1195   elts_before = pool_elts (lm->if_address_pool);
1196
1197   error = ip_interface_address_add_del
1198     (lm,
1199      sw_if_index,
1200      addr_fib,
1201      address_length,
1202      is_del,
1203      &if_address_index);
1204   if (error)
1205     goto done;
1206   
1207   if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1208     {
1209       if (is_del)
1210         ip4_del_interface_routes (im, ip4_af.fib_index, address,
1211                                   address_length);
1212       
1213       else
1214           ip4_add_interface_routes (sw_if_index,
1215                                     im, ip4_af.fib_index,
1216                                     pool_elt_at_index 
1217                                     (lm->if_address_pool, if_address_index));
1218     }
1219
1220   /* If pool did not grow/shrink: add duplicate address. */
1221   if (elts_before != pool_elts (lm->if_address_pool))
1222     {
1223       ip4_add_del_interface_address_callback_t * cb;
1224       vec_foreach (cb, im->add_del_interface_address_callbacks)
1225         cb->function (im, cb->function_opaque, sw_if_index,
1226                       address, address_length,
1227                       if_address_index,
1228                       is_del);
1229     }
1230
1231  done:
1232   vec_free (addr_fib);
1233   return error;
1234 }
1235
1236 clib_error_t *
1237 ip4_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1238                                ip4_address_t * address, u32 address_length,
1239                                u32 is_del)
1240 {
1241   return ip4_add_del_interface_address_internal
1242     (vm, sw_if_index, address, address_length,
1243      /* redistribute */ 1,
1244      /* insert_routes */ 1,
1245      is_del);
1246 }
1247
1248 static clib_error_t *
1249 ip4_sw_interface_admin_up_down (vnet_main_t * vnm,
1250                                 u32 sw_if_index,
1251                                 u32 flags)
1252 {
1253   ip4_main_t * im = &ip4_main;
1254   ip_interface_address_t * ia;
1255   ip4_address_t * a;
1256   u32 is_admin_up, fib_index;
1257   
1258   /* Fill in lookup tables with default table (0). */
1259   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1260   
1261   vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1262   
1263   is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1264   
1265   fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1266
1267   foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index, 
1268                                 0 /* honor unnumbered */,
1269   ({
1270     a = ip_interface_address_get_address (&im->lookup_main, ia);
1271     if (is_admin_up)
1272       ip4_add_interface_routes (sw_if_index,
1273                                 im, fib_index,
1274                                 ia);
1275     else
1276       ip4_del_interface_routes (im, fib_index,
1277                                 a, ia->address_length);
1278   }));
1279
1280   return 0;
1281 }
1282  
1283 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip4_sw_interface_admin_up_down);
1284
1285 static clib_error_t *
1286 ip4_sw_interface_add_del (vnet_main_t * vnm,
1287                           u32 sw_if_index,
1288                           u32 is_add)
1289 {
1290   vlib_main_t * vm = vnm->vlib_main;
1291   ip4_main_t * im = &ip4_main;
1292   ip_lookup_main_t * lm = &im->lookup_main;
1293   u32 ci, cast;
1294
1295   for (cast = 0; cast < VNET_N_CAST; cast++)
1296     {
1297       ip_config_main_t * cm = &lm->rx_config_mains[cast];
1298       vnet_config_main_t * vcm = &cm->config_main;
1299
1300       if (! vcm->node_index_by_feature_index)
1301         {
1302           if (cast == VNET_UNICAST)
1303             {
1304               static char * start_nodes[] = { "ip4-input", "ip4-input-no-checksum", };
1305               static char * feature_nodes[] = {
1306                 [IP4_RX_FEATURE_CHECK_ACCESS] = "ip4-inacl",
1307                 [IP4_RX_FEATURE_SOURCE_CHECK_REACHABLE_VIA_RX] = "ip4-source-check-via-rx",
1308                 [IP4_RX_FEATURE_SOURCE_CHECK_REACHABLE_VIA_ANY] = "ip4-source-check-via-any",
1309                 [IP4_RX_FEATURE_IPSEC] = "ipsec-input-ip4",
1310                 [IP4_RX_FEATURE_VPATH] = "vpath-input-ip4",
1311                 [IP4_RX_FEATURE_LOOKUP] = "ip4-lookup",
1312               };
1313
1314               vnet_config_init (vm, vcm,
1315                                 start_nodes, ARRAY_LEN (start_nodes),
1316                                 feature_nodes, ARRAY_LEN (feature_nodes));
1317             }
1318           else
1319             {
1320               static char * start_nodes[] = { "ip4-input", "ip4-input-no-checksum", };
1321               static char * feature_nodes[] = {
1322                 [IP4_RX_FEATURE_VPATH] = "vpath-input-ip4",
1323                 [IP4_RX_FEATURE_LOOKUP] = "ip4-lookup-multicast",
1324               };
1325
1326               vnet_config_init (vm, vcm,
1327                                 start_nodes, ARRAY_LEN (start_nodes),
1328                                 feature_nodes, ARRAY_LEN (feature_nodes));
1329             }
1330         }
1331
1332       vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1333       ci = cm->config_index_by_sw_if_index[sw_if_index];
1334
1335       if (is_add)
1336         ci = vnet_config_add_feature (vm, vcm,
1337                                       ci,
1338                                       IP4_RX_FEATURE_LOOKUP,
1339                                       /* config data */ 0,
1340                                       /* # bytes of config data */ 0);
1341       else
1342         ci = vnet_config_del_feature (vm, vcm,
1343                                       ci,
1344                                       IP4_RX_FEATURE_LOOKUP,
1345                                       /* config data */ 0,
1346                                       /* # bytes of config data */ 0);
1347
1348       cm->config_index_by_sw_if_index[sw_if_index] = ci;
1349     }
1350
1351   return /* no error */ 0;
1352 }
1353
1354 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip4_sw_interface_add_del);
1355
1356 static u8 * format_ip4_lookup_trace (u8 * s, va_list * args);
1357
1358 VLIB_REGISTER_NODE (ip4_lookup_node) = {
1359   .function = ip4_lookup,
1360   .name = "ip4-lookup",
1361   .vector_size = sizeof (u32),
1362
1363   .format_trace = format_ip4_lookup_trace,
1364
1365   .n_next_nodes = IP_LOOKUP_N_NEXT,
1366   .next_nodes = IP4_LOOKUP_NEXT_NODES,
1367 };
1368
1369 VLIB_NODE_FUNCTION_MULTIARCH (ip4_lookup_node, ip4_lookup)
1370
1371 static uword
1372 ip4_indirect (vlib_main_t * vm,
1373                vlib_node_runtime_t * node,
1374                vlib_frame_t * frame)
1375 {
1376   return ip4_lookup_inline (vm, node, frame,
1377                             /* lookup_for_responses_to_locally_received_packets */ 0,
1378                             /* is_indirect */ 1);
1379 }
1380
1381 VLIB_REGISTER_NODE (ip4_indirect_node) = {
1382   .function = ip4_indirect,
1383   .name = "ip4-indirect",
1384   .vector_size = sizeof (u32),
1385
1386   .format_trace = format_ip4_lookup_trace,
1387
1388   .n_next_nodes = IP_LOOKUP_N_NEXT,
1389   .next_nodes = IP4_LOOKUP_NEXT_NODES,
1390 };
1391
1392 VLIB_NODE_FUNCTION_MULTIARCH (ip4_indirect_node, ip4_indirect)
1393
1394
1395 /* Global IP4 main. */
1396 ip4_main_t ip4_main;
1397
1398 clib_error_t *
1399 ip4_lookup_init (vlib_main_t * vm)
1400 {
1401   ip4_main_t * im = &ip4_main;
1402   uword i;
1403
1404   for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
1405     {
1406       u32 m;
1407
1408       if (i < 32)
1409         m = pow2_mask (i) << (32 - i);
1410       else 
1411         m = ~0;
1412       im->fib_masks[i] = clib_host_to_net_u32 (m);
1413     }
1414
1415   /* Create FIB with index 0 and table id of 0. */
1416   find_ip4_fib_by_table_index_or_id (im, /* table id */ 0, IP4_ROUTE_FLAG_TABLE_ID);
1417
1418   ip_lookup_init (&im->lookup_main, /* is_ip6 */ 0);
1419
1420   {
1421     pg_node_t * pn;
1422     pn = pg_get_node (ip4_lookup_node.index);
1423     pn->unformat_edit = unformat_pg_ip4_header;
1424   }
1425
1426   {
1427     ethernet_arp_header_t h;
1428
1429     memset (&h, 0, sizeof (h));
1430
1431     /* Set target ethernet address to all zeros. */
1432     memset (h.ip4_over_ethernet[1].ethernet, 0, sizeof (h.ip4_over_ethernet[1].ethernet));
1433
1434 #define _16(f,v) h.f = clib_host_to_net_u16 (v);
1435 #define _8(f,v) h.f = v;
1436     _16 (l2_type, ETHERNET_ARP_HARDWARE_TYPE_ethernet);
1437     _16 (l3_type, ETHERNET_TYPE_IP4);
1438     _8 (n_l2_address_bytes, 6);
1439     _8 (n_l3_address_bytes, 4);
1440     _16 (opcode, ETHERNET_ARP_OPCODE_request);
1441 #undef _16
1442 #undef _8
1443
1444     vlib_packet_template_init (vm,
1445                                &im->ip4_arp_request_packet_template,
1446                                /* data */ &h,
1447                                sizeof (h),
1448                                /* alloc chunk size */ 8,
1449                                "ip4 arp");
1450   }
1451
1452   return 0;
1453 }
1454
1455 VLIB_INIT_FUNCTION (ip4_lookup_init);
1456
1457 typedef struct {
1458   /* Adjacency taken. */
1459   u32 adj_index;
1460   u32 flow_hash;
1461   u32 fib_index;
1462
1463   /* Packet data, possibly *after* rewrite. */
1464   u8 packet_data[64 - 1*sizeof(u32)];
1465 } ip4_forward_next_trace_t;
1466
1467 static u8 * format_ip4_forward_next_trace (u8 * s, va_list * args)
1468 {
1469   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1470   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1471   ip4_forward_next_trace_t * t = va_arg (*args, ip4_forward_next_trace_t *);
1472   uword indent = format_get_indent (s);
1473   s = format (s, "%U%U",
1474                 format_white_space, indent,
1475                 format_ip4_header, t->packet_data);
1476   return s;
1477 }
1478
1479 static u8 * format_ip4_lookup_trace (u8 * s, va_list * args)
1480 {
1481   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1482   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1483   ip4_forward_next_trace_t * t = va_arg (*args, ip4_forward_next_trace_t *);
1484   vnet_main_t * vnm = vnet_get_main();
1485   ip4_main_t * im = &ip4_main;
1486   uword indent = format_get_indent (s);
1487
1488   s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1489               t->fib_index, t->adj_index, format_ip_adjacency,
1490               vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1491   s = format (s, "\n%U%U",
1492               format_white_space, indent,
1493               format_ip4_header, t->packet_data);
1494   return s;
1495 }
1496
1497 static u8 * format_ip4_rewrite_trace (u8 * s, va_list * args)
1498 {
1499   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1500   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1501   ip4_forward_next_trace_t * t = va_arg (*args, ip4_forward_next_trace_t *);
1502   vnet_main_t * vnm = vnet_get_main();
1503   ip4_main_t * im = &ip4_main;
1504   uword indent = format_get_indent (s);
1505
1506   s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
1507               t->fib_index, t->adj_index, format_ip_adjacency,
1508               vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1509   s = format (s, "\n%U%U",
1510               format_white_space, indent,
1511               format_ip_adjacency_packet_data,
1512               vnm, &im->lookup_main, t->adj_index,
1513               t->packet_data, sizeof (t->packet_data));
1514   return s;
1515 }
1516
1517 /* Common trace function for all ip4-forward next nodes. */
1518 void
1519 ip4_forward_next_trace (vlib_main_t * vm,
1520                         vlib_node_runtime_t * node,
1521                         vlib_frame_t * frame,
1522                         vlib_rx_or_tx_t which_adj_index)
1523 {
1524   u32 * from, n_left;
1525   ip4_main_t * im = &ip4_main;
1526
1527   n_left = frame->n_vectors;
1528   from = vlib_frame_vector_args (frame);
1529   
1530   while (n_left >= 4)
1531     {
1532       u32 bi0, bi1;
1533       vlib_buffer_t * b0, * b1;
1534       ip4_forward_next_trace_t * t0, * t1;
1535
1536       /* Prefetch next iteration. */
1537       vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1538       vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1539
1540       bi0 = from[0];
1541       bi1 = from[1];
1542
1543       b0 = vlib_get_buffer (vm, bi0);
1544       b1 = vlib_get_buffer (vm, bi1);
1545
1546       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1547         {
1548           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1549           t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1550           t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1551           t0->fib_index = (vnet_buffer(b0)->sw_if_index[VLIB_TX] != (u32)~0) ?
1552               vnet_buffer(b0)->sw_if_index[VLIB_TX] :
1553               vec_elt (im->fib_index_by_sw_if_index,
1554                        vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1555
1556           clib_memcpy (t0->packet_data,
1557                   vlib_buffer_get_current (b0),
1558                   sizeof (t0->packet_data));
1559         }
1560       if (b1->flags & VLIB_BUFFER_IS_TRACED)
1561         {
1562           t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1563           t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1564           t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1565           t1->fib_index = (vnet_buffer(b1)->sw_if_index[VLIB_TX] != (u32)~0) ?
1566               vnet_buffer(b1)->sw_if_index[VLIB_TX] :
1567               vec_elt (im->fib_index_by_sw_if_index,
1568                        vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1569           clib_memcpy (t1->packet_data,
1570                   vlib_buffer_get_current (b1),
1571                   sizeof (t1->packet_data));
1572         }
1573       from += 2;
1574       n_left -= 2;
1575     }
1576
1577   while (n_left >= 1)
1578     {
1579       u32 bi0;
1580       vlib_buffer_t * b0;
1581       ip4_forward_next_trace_t * t0;
1582
1583       bi0 = from[0];
1584
1585       b0 = vlib_get_buffer (vm, bi0);
1586
1587       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1588         {
1589           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1590           t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1591           t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1592           t0->fib_index = (vnet_buffer(b0)->sw_if_index[VLIB_TX] != (u32)~0) ?
1593               vnet_buffer(b0)->sw_if_index[VLIB_TX] :
1594               vec_elt (im->fib_index_by_sw_if_index,
1595                        vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1596           clib_memcpy (t0->packet_data,
1597                   vlib_buffer_get_current (b0),
1598                   sizeof (t0->packet_data));
1599         }
1600       from += 1;
1601       n_left -= 1;
1602     }
1603 }
1604
1605 static uword
1606 ip4_drop_or_punt (vlib_main_t * vm,
1607                   vlib_node_runtime_t * node,
1608                   vlib_frame_t * frame,
1609                   ip4_error_t error_code)
1610 {
1611   u32 * buffers = vlib_frame_vector_args (frame);
1612   uword n_packets = frame->n_vectors;
1613
1614   vlib_error_drop_buffers (vm, node,
1615                            buffers,
1616                            /* stride */ 1,
1617                            n_packets,
1618                            /* next */ 0,
1619                            ip4_input_node.index,
1620                            error_code);
1621
1622   if (node->flags & VLIB_NODE_FLAG_TRACE)
1623     ip4_forward_next_trace (vm, node, frame, VLIB_TX);
1624
1625   return n_packets;
1626 }
1627
1628 static uword
1629 ip4_drop (vlib_main_t * vm,
1630           vlib_node_runtime_t * node,
1631           vlib_frame_t * frame)
1632 { return ip4_drop_or_punt (vm, node, frame, IP4_ERROR_ADJACENCY_DROP); }
1633
1634 static uword
1635 ip4_punt (vlib_main_t * vm,
1636           vlib_node_runtime_t * node,
1637           vlib_frame_t * frame)
1638 { return ip4_drop_or_punt (vm, node, frame, IP4_ERROR_ADJACENCY_PUNT); }
1639
1640 static uword
1641 ip4_miss (vlib_main_t * vm,
1642           vlib_node_runtime_t * node,
1643           vlib_frame_t * frame)
1644 { return ip4_drop_or_punt (vm, node, frame, IP4_ERROR_DST_LOOKUP_MISS); }
1645
1646 VLIB_REGISTER_NODE (ip4_drop_node,static) = {
1647   .function = ip4_drop,
1648   .name = "ip4-drop",
1649   .vector_size = sizeof (u32),
1650
1651   .format_trace = format_ip4_forward_next_trace,
1652
1653   .n_next_nodes = 1,
1654   .next_nodes = {
1655     [0] = "error-drop",
1656   },
1657 };
1658
1659 VLIB_NODE_FUNCTION_MULTIARCH (ip4_drop_node, ip4_drop)
1660
1661 VLIB_REGISTER_NODE (ip4_punt_node,static) = {
1662   .function = ip4_punt,
1663   .name = "ip4-punt",
1664   .vector_size = sizeof (u32),
1665
1666   .format_trace = format_ip4_forward_next_trace,
1667
1668   .n_next_nodes = 1,
1669   .next_nodes = {
1670     [0] = "error-punt",
1671   },
1672 };
1673
1674 VLIB_NODE_FUNCTION_MULTIARCH (ip4_punt_node, ip4_punt)
1675
1676 VLIB_REGISTER_NODE (ip4_miss_node,static) = {
1677   .function = ip4_miss,
1678   .name = "ip4-miss",
1679   .vector_size = sizeof (u32),
1680
1681   .format_trace = format_ip4_forward_next_trace,
1682
1683   .n_next_nodes = 1,
1684   .next_nodes = {
1685     [0] = "error-drop",
1686   },
1687 };
1688
1689 VLIB_NODE_FUNCTION_MULTIARCH (ip4_miss_node, ip4_miss)
1690
1691 /* Compute TCP/UDP/ICMP4 checksum in software. */
1692 u16
1693 ip4_tcp_udp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1694                               ip4_header_t * ip0)
1695 {
1696   ip_csum_t sum0;
1697   u32 ip_header_length, payload_length_host_byte_order;
1698   u32 n_this_buffer, n_bytes_left;
1699   u16 sum16;
1700   void * data_this_buffer;
1701   
1702   /* Initialize checksum with ip header. */
1703   ip_header_length = ip4_header_bytes (ip0);
1704   payload_length_host_byte_order = clib_net_to_host_u16 (ip0->length) - ip_header_length;
1705   sum0 = clib_host_to_net_u32 (payload_length_host_byte_order + (ip0->protocol << 16));
1706
1707   if (BITS (uword) == 32)
1708     {
1709       sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->src_address, u32));
1710       sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->dst_address, u32));
1711     }
1712   else
1713     sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->src_address, u64));
1714
1715   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1716   data_this_buffer = (void *) ip0 + ip_header_length;
1717   if (n_this_buffer + ip_header_length > p0->current_length)
1718     n_this_buffer = p0->current_length > ip_header_length ? p0->current_length - ip_header_length : 0;
1719   while (1)
1720     {
1721       sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1722       n_bytes_left -= n_this_buffer;
1723       if (n_bytes_left == 0)
1724         break;
1725
1726       ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
1727       p0 = vlib_get_buffer (vm, p0->next_buffer);
1728       data_this_buffer = vlib_buffer_get_current (p0);
1729       n_this_buffer = p0->current_length;
1730     }
1731
1732   sum16 = ~ ip_csum_fold (sum0);
1733
1734   return sum16;
1735 }
1736
1737 static u32
1738 ip4_tcp_udp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1739 {
1740   ip4_header_t * ip0 = vlib_buffer_get_current (p0);
1741   udp_header_t * udp0;
1742   u16 sum16;
1743
1744   ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1745           || ip0->protocol == IP_PROTOCOL_UDP);
1746
1747   udp0 = (void *) (ip0 + 1);
1748   if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1749     {
1750       p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1751                     | IP_BUFFER_L4_CHECKSUM_CORRECT);
1752       return p0->flags;
1753     }
1754
1755   sum16 = ip4_tcp_udp_compute_checksum (vm, p0, ip0);
1756
1757   p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1758                 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1759
1760   return p0->flags;
1761 }
1762
1763 static uword
1764 ip4_local (vlib_main_t * vm,
1765            vlib_node_runtime_t * node,
1766            vlib_frame_t * frame)
1767 {
1768   ip4_main_t * im = &ip4_main;
1769   ip_lookup_main_t * lm = &im->lookup_main;
1770   ip_local_next_t next_index;
1771   u32 * from, * to_next, n_left_from, n_left_to_next;
1772   vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
1773
1774   from = vlib_frame_vector_args (frame);
1775   n_left_from = frame->n_vectors;
1776   next_index = node->cached_next_index;
1777   
1778   if (node->flags & VLIB_NODE_FLAG_TRACE)
1779     ip4_forward_next_trace (vm, node, frame, VLIB_TX);
1780
1781   while (n_left_from > 0)
1782     {
1783       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1784
1785       while (n_left_from >= 4 && n_left_to_next >= 2)
1786         {
1787           vlib_buffer_t * p0, * p1;
1788           ip4_header_t * ip0, * ip1;
1789           udp_header_t * udp0, * udp1;
1790           ip4_fib_mtrie_t * mtrie0, * mtrie1;
1791           ip4_fib_mtrie_leaf_t leaf0, leaf1;
1792           ip_adjacency_t * adj0, * adj1;
1793           u32 pi0, ip_len0, udp_len0, flags0, next0, fib_index0, adj_index0;
1794           u32 pi1, ip_len1, udp_len1, flags1, next1, fib_index1, adj_index1;
1795           i32 len_diff0, len_diff1;
1796           u8 error0, is_udp0, is_tcp_udp0, good_tcp_udp0, proto0;
1797           u8 error1, is_udp1, is_tcp_udp1, good_tcp_udp1, proto1;
1798           u8 enqueue_code;
1799       
1800           pi0 = to_next[0] = from[0];
1801           pi1 = to_next[1] = from[1];
1802           from += 2;
1803           n_left_from -= 2;
1804           to_next += 2;
1805           n_left_to_next -= 2;
1806       
1807           p0 = vlib_get_buffer (vm, pi0);
1808           p1 = vlib_get_buffer (vm, pi1);
1809
1810           ip0 = vlib_buffer_get_current (p0);
1811           ip1 = vlib_buffer_get_current (p1);
1812
1813           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, 
1814                                 vnet_buffer(p0)->sw_if_index[VLIB_RX]);
1815           fib_index1 = vec_elt (im->fib_index_by_sw_if_index, 
1816                                 vnet_buffer(p1)->sw_if_index[VLIB_RX]);
1817
1818           mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
1819           mtrie1 = &vec_elt_at_index (im->fibs, fib_index1)->mtrie;
1820
1821           leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
1822
1823           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 0);
1824           leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 0);
1825
1826           /* Treat IP frag packets as "experimental" protocol for now
1827              until support of IP frag reassembly is implemented */
1828           proto0 = ip4_is_fragment(ip0) ? 0xfe : ip0->protocol;
1829           proto1 = ip4_is_fragment(ip1) ? 0xfe : ip1->protocol;
1830           is_udp0 = proto0 == IP_PROTOCOL_UDP;
1831           is_udp1 = proto1 == IP_PROTOCOL_UDP;
1832           is_tcp_udp0 = is_udp0 || proto0 == IP_PROTOCOL_TCP;
1833           is_tcp_udp1 = is_udp1 || proto1 == IP_PROTOCOL_TCP;
1834
1835           flags0 = p0->flags;
1836           flags1 = p1->flags;
1837
1838           good_tcp_udp0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1839           good_tcp_udp1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1840
1841           udp0 = ip4_next_header (ip0);
1842           udp1 = ip4_next_header (ip1);
1843
1844           /* Don't verify UDP checksum for packets with explicit zero checksum. */
1845           good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
1846           good_tcp_udp1 |= is_udp1 && udp1->checksum == 0;
1847
1848           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 1);
1849           leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 1);
1850
1851           /* Verify UDP length. */
1852           ip_len0 = clib_net_to_host_u16 (ip0->length);
1853           ip_len1 = clib_net_to_host_u16 (ip1->length);
1854           udp_len0 = clib_net_to_host_u16 (udp0->length);
1855           udp_len1 = clib_net_to_host_u16 (udp1->length);
1856
1857           len_diff0 = ip_len0 - udp_len0;
1858           len_diff1 = ip_len1 - udp_len1;
1859
1860           len_diff0 = is_udp0 ? len_diff0 : 0;
1861           len_diff1 = is_udp1 ? len_diff1 : 0;
1862
1863           if (PREDICT_FALSE (! (is_tcp_udp0 & is_tcp_udp1
1864                                 & good_tcp_udp0 & good_tcp_udp1)))
1865             {
1866               if (is_tcp_udp0)
1867                 {
1868                   if (is_tcp_udp0
1869                       && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
1870                     flags0 = ip4_tcp_udp_validate_checksum (vm, p0);
1871                   good_tcp_udp0 =
1872                     (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1873                   good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
1874                 }
1875               if (is_tcp_udp1)
1876                 {
1877                   if (is_tcp_udp1
1878                       && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
1879                     flags1 = ip4_tcp_udp_validate_checksum (vm, p1);
1880                   good_tcp_udp1 =
1881                     (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1882                   good_tcp_udp1 |= is_udp1 && udp1->checksum == 0;
1883                 }
1884             }
1885
1886           good_tcp_udp0 &= len_diff0 >= 0;
1887           good_tcp_udp1 &= len_diff1 >= 0;
1888
1889           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
1890           leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 2);
1891
1892           error0 = error1 = IP4_ERROR_UNKNOWN_PROTOCOL;
1893
1894           error0 = len_diff0 < 0 ? IP4_ERROR_UDP_LENGTH : error0;
1895           error1 = len_diff1 < 0 ? IP4_ERROR_UDP_LENGTH : error1;
1896
1897           ASSERT (IP4_ERROR_TCP_CHECKSUM + 1 == IP4_ERROR_UDP_CHECKSUM);
1898           error0 = (is_tcp_udp0 && ! good_tcp_udp0
1899                     ? IP4_ERROR_TCP_CHECKSUM + is_udp0
1900                     : error0);
1901           error1 = (is_tcp_udp1 && ! good_tcp_udp1
1902                     ? IP4_ERROR_TCP_CHECKSUM + is_udp1
1903                     : error1);
1904
1905           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
1906           leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 3);
1907
1908           vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
1909           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
1910
1911           vnet_buffer (p1)->ip.adj_index[VLIB_RX] = adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
1912           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
1913
1914           ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
1915                                                            &ip0->src_address,
1916                                                            /* no_default_route */ 1));
1917           ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1,
1918                                                            &ip1->src_address,
1919                                                            /* no_default_route */ 1));
1920
1921           adj0 = ip_get_adjacency (lm, adj_index0);
1922           adj1 = ip_get_adjacency (lm, adj_index1);
1923
1924           /* 
1925            * Must have a route to source otherwise we drop the packet.
1926            * ip4 broadcasts are accepted, e.g. to make dhcp client work
1927            */
1928           error0 = (error0 == IP4_ERROR_UNKNOWN_PROTOCOL
1929                     && adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE
1930                     && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP
1931                     && adj0->lookup_next_index != IP_LOOKUP_NEXT_LOCAL
1932                     && ip0->dst_address.as_u32 != 0xFFFFFFFF
1933                     ? IP4_ERROR_SRC_LOOKUP_MISS
1934                     : error0);
1935           error1 = (error1 == IP4_ERROR_UNKNOWN_PROTOCOL
1936                     && adj1->lookup_next_index != IP_LOOKUP_NEXT_REWRITE
1937                     && adj1->lookup_next_index != IP_LOOKUP_NEXT_ARP
1938                     && adj1->lookup_next_index != IP_LOOKUP_NEXT_LOCAL
1939                     && ip0->dst_address.as_u32 != 0xFFFFFFFF
1940                     ? IP4_ERROR_SRC_LOOKUP_MISS
1941                     : error1);
1942
1943           next0 = lm->local_next_by_ip_protocol[proto0];
1944           next1 = lm->local_next_by_ip_protocol[proto1];
1945
1946           next0 = error0 != IP4_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1947           next1 = error1 != IP4_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1948
1949           p0->error = error0 ? error_node->errors[error0] : 0;
1950           p1->error = error1 ? error_node->errors[error1] : 0;
1951
1952           enqueue_code = (next0 != next_index) + 2*(next1 != next_index);
1953
1954           if (PREDICT_FALSE (enqueue_code != 0))
1955             {
1956               switch (enqueue_code)
1957                 {
1958                 case 1:
1959                   /* A B A */
1960                   to_next[-2] = pi1;
1961                   to_next -= 1;
1962                   n_left_to_next += 1;
1963                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
1964                   break;
1965
1966                 case 2:
1967                   /* A A B */
1968                   to_next -= 1;
1969                   n_left_to_next += 1;
1970                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
1971                   break;
1972
1973                 case 3:
1974                   /* A B B or A B C */
1975                   to_next -= 2;
1976                   n_left_to_next += 2;
1977                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
1978                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
1979                   if (next0 == next1)
1980                     {
1981                       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1982                       next_index = next1;
1983                       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1984                     }
1985                   break;
1986                 }
1987             }
1988         }
1989
1990       while (n_left_from > 0 && n_left_to_next > 0)
1991         {
1992           vlib_buffer_t * p0;
1993           ip4_header_t * ip0;
1994           udp_header_t * udp0;
1995           ip4_fib_mtrie_t * mtrie0;
1996           ip4_fib_mtrie_leaf_t leaf0;
1997           ip_adjacency_t * adj0;
1998           u32 pi0, next0, ip_len0, udp_len0, flags0, fib_index0, adj_index0;
1999           i32 len_diff0;
2000           u8 error0, is_udp0, is_tcp_udp0, good_tcp_udp0, proto0;
2001       
2002           pi0 = to_next[0] = from[0];
2003           from += 1;
2004           n_left_from -= 1;
2005           to_next += 1;
2006           n_left_to_next -= 1;
2007       
2008           p0 = vlib_get_buffer (vm, pi0);
2009
2010           ip0 = vlib_buffer_get_current (p0);
2011
2012           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, 
2013                                 vnet_buffer(p0)->sw_if_index[VLIB_RX]);
2014
2015           mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
2016
2017           leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
2018
2019           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 0);
2020
2021           /* Treat IP frag packets as "experimental" protocol for now
2022              until support of IP frag reassembly is implemented */
2023           proto0 = ip4_is_fragment(ip0) ? 0xfe : ip0->protocol;
2024           is_udp0 = proto0 == IP_PROTOCOL_UDP;
2025           is_tcp_udp0 = is_udp0 || proto0 == IP_PROTOCOL_TCP;
2026
2027           flags0 = p0->flags;
2028
2029           good_tcp_udp0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
2030
2031           udp0 = ip4_next_header (ip0);
2032
2033           /* Don't verify UDP checksum for packets with explicit zero checksum. */
2034           good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
2035
2036           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 1);
2037
2038           /* Verify UDP length. */
2039           ip_len0 = clib_net_to_host_u16 (ip0->length);
2040           udp_len0 = clib_net_to_host_u16 (udp0->length);
2041
2042           len_diff0 = ip_len0 - udp_len0;
2043
2044           len_diff0 = is_udp0 ? len_diff0 : 0;
2045
2046           if (PREDICT_FALSE (! (is_tcp_udp0 & good_tcp_udp0)))
2047             {
2048               if (is_tcp_udp0)
2049                 {
2050                   if (is_tcp_udp0
2051                       && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
2052                     flags0 = ip4_tcp_udp_validate_checksum (vm, p0);
2053                   good_tcp_udp0 =
2054                     (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
2055                   good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
2056                 }
2057             }
2058
2059           good_tcp_udp0 &= len_diff0 >= 0;
2060
2061           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
2062
2063           error0 = IP4_ERROR_UNKNOWN_PROTOCOL;
2064
2065           error0 = len_diff0 < 0 ? IP4_ERROR_UDP_LENGTH : error0;
2066
2067           ASSERT (IP4_ERROR_TCP_CHECKSUM + 1 == IP4_ERROR_UDP_CHECKSUM);
2068           error0 = (is_tcp_udp0 && ! good_tcp_udp0
2069                     ? IP4_ERROR_TCP_CHECKSUM + is_udp0
2070                     : error0);
2071
2072           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
2073
2074           vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
2075           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
2076
2077           ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
2078                                                            &ip0->src_address,
2079                                                            /* no_default_route */ 1));
2080
2081           adj0 = ip_get_adjacency (lm, adj_index0);
2082
2083           /* Must have a route to source otherwise we drop the packet. */
2084           error0 = (error0 == IP4_ERROR_UNKNOWN_PROTOCOL
2085                     && adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE
2086                     && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP
2087                     && adj0->lookup_next_index != IP_LOOKUP_NEXT_LOCAL
2088                     && ip0->dst_address.as_u32 != 0xFFFFFFFF
2089                     ? IP4_ERROR_SRC_LOOKUP_MISS
2090                     : error0);
2091
2092           next0 = lm->local_next_by_ip_protocol[proto0];
2093
2094           next0 = error0 != IP4_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
2095
2096           p0->error = error0? error_node->errors[error0] : 0;
2097
2098           if (PREDICT_FALSE (next0 != next_index))
2099             {
2100               n_left_to_next += 1;
2101               vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2102
2103               next_index = next0;
2104               vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2105               to_next[0] = pi0;
2106               to_next += 1;
2107               n_left_to_next -= 1;
2108             }
2109         }
2110   
2111       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2112     }
2113
2114   return frame->n_vectors;
2115 }
2116
2117 VLIB_REGISTER_NODE (ip4_local_node,static) = {
2118   .function = ip4_local,
2119   .name = "ip4-local",
2120   .vector_size = sizeof (u32),
2121
2122   .format_trace = format_ip4_forward_next_trace,
2123
2124   .n_next_nodes = IP_LOCAL_N_NEXT,
2125   .next_nodes = {
2126     [IP_LOCAL_NEXT_DROP] = "error-drop",
2127     [IP_LOCAL_NEXT_PUNT] = "error-punt",
2128     [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip4-udp-lookup",
2129     [IP_LOCAL_NEXT_ICMP] = "ip4-icmp-input",
2130   },
2131 };
2132
2133 VLIB_NODE_FUNCTION_MULTIARCH (ip4_local_node, ip4_local)
2134
2135 void ip4_register_protocol (u32 protocol, u32 node_index)
2136 {
2137   vlib_main_t * vm = vlib_get_main();
2138   ip4_main_t * im = &ip4_main;
2139   ip_lookup_main_t * lm = &im->lookup_main;
2140
2141   ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
2142   lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip4_local_node.index, node_index);
2143 }
2144
2145 static clib_error_t *
2146 show_ip_local_command_fn (vlib_main_t * vm,
2147                           unformat_input_t * input,
2148                          vlib_cli_command_t * cmd)
2149 {
2150   ip4_main_t * im = &ip4_main;
2151   ip_lookup_main_t * lm = &im->lookup_main;
2152   int i;
2153
2154   vlib_cli_output (vm, "Protocols handled by ip4_local");
2155   for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2156     {
2157       if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2158         vlib_cli_output (vm, "%d", i);
2159     }
2160   return 0;
2161 }
2162
2163
2164
2165 VLIB_CLI_COMMAND (show_ip_local, static) = {
2166   .path = "show ip local",
2167   .function = show_ip_local_command_fn,
2168   .short_help = "Show ip local protocol table",
2169 };
2170
2171 static uword
2172 ip4_arp (vlib_main_t * vm,
2173          vlib_node_runtime_t * node,
2174          vlib_frame_t * frame)
2175 {
2176   vnet_main_t * vnm = vnet_get_main();
2177   ip4_main_t * im = &ip4_main;
2178   ip_lookup_main_t * lm = &im->lookup_main;
2179   u32 * from, * to_next_drop;
2180   uword n_left_from, n_left_to_next_drop, next_index;
2181   static f64 time_last_seed_change = -1e100;
2182   static u32 hash_seeds[3];
2183   static uword hash_bitmap[256 / BITS (uword)]; 
2184   f64 time_now;
2185
2186   if (node->flags & VLIB_NODE_FLAG_TRACE)
2187     ip4_forward_next_trace (vm, node, frame, VLIB_TX);
2188
2189   time_now = vlib_time_now (vm);
2190   if (time_now - time_last_seed_change > 1e-3)
2191     {
2192       uword i;
2193       u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
2194                                              sizeof (hash_seeds));
2195       for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
2196         hash_seeds[i] = r[i];
2197
2198       /* Mark all hash keys as been no-seen before. */
2199       for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
2200         hash_bitmap[i] = 0;
2201
2202       time_last_seed_change = time_now;
2203     }
2204
2205   from = vlib_frame_vector_args (frame);
2206   n_left_from = frame->n_vectors;
2207   next_index = node->cached_next_index;
2208   if (next_index == IP4_ARP_NEXT_DROP)
2209     next_index = IP4_ARP_N_NEXT; /* point to first interface */
2210
2211   while (n_left_from > 0)
2212     {
2213       vlib_get_next_frame (vm, node, IP4_ARP_NEXT_DROP,
2214                            to_next_drop, n_left_to_next_drop);
2215
2216       while (n_left_from > 0 && n_left_to_next_drop > 0)
2217         {
2218           vlib_buffer_t * p0;
2219           ip4_header_t * ip0;
2220           ethernet_header_t * eh0;
2221           u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
2222           uword bm0;
2223           ip_adjacency_t * adj0;
2224
2225           pi0 = from[0];
2226
2227           p0 = vlib_get_buffer (vm, pi0);
2228
2229           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2230           adj0 = ip_get_adjacency (lm, adj_index0);
2231           ip0 = vlib_buffer_get_current (p0);
2232
2233           /* If packet destination is not local, send ARP to next hop */
2234           if (adj0->arp.next_hop.ip4.as_u32)
2235             ip0->dst_address.data_u32 = adj0->arp.next_hop.ip4.as_u32;
2236
2237           /* 
2238            * if ip4_rewrite_local applied the IP_LOOKUP_NEXT_ARP
2239            * rewrite to this packet, we need to skip it here.
2240            * Note, to distinguish from src IP addr *.8.6.*, we
2241            * check for a bcast eth dest instead of IPv4 version.
2242            */
2243           eh0 = (ethernet_header_t*)ip0;
2244           if ((ip0->ip_version_and_header_length & 0xF0) != 0x40)
2245             {
2246               u32 vlan_num = 0;
2247               u16 * etype = &eh0->type;
2248               while ((*etype == clib_host_to_net_u16 (0x8100)) //dot1q 
2249                   || (*etype == clib_host_to_net_u16 (0x88a8)))//dot1ad 
2250                 {
2251                   vlan_num += 1;
2252                   etype += 2; //vlan tag also 16 bits, same as etype
2253                 }
2254               if (*etype == clib_host_to_net_u16 (0x0806))     //arp
2255                 {
2256                   vlib_buffer_advance (
2257                       p0, sizeof(ethernet_header_t) + (4*vlan_num));
2258                   ip0 = vlib_buffer_get_current (p0);
2259                 }
2260             }
2261
2262           a0 = hash_seeds[0];
2263           b0 = hash_seeds[1];
2264           c0 = hash_seeds[2];
2265
2266           sw_if_index0 = adj0->rewrite_header.sw_if_index;
2267           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
2268
2269           a0 ^= ip0->dst_address.data_u32;
2270           b0 ^= sw_if_index0;
2271
2272           hash_v3_finalize32 (a0, b0, c0);
2273
2274           c0 &= BITS (hash_bitmap) - 1;
2275           c0 = c0 / BITS (uword);
2276           m0 = (uword) 1 << (c0 % BITS (uword));
2277
2278           bm0 = hash_bitmap[c0];
2279           drop0 = (bm0 & m0) != 0;
2280
2281           /* Mark it as seen. */
2282           hash_bitmap[c0] = bm0 | m0;
2283
2284           from += 1;
2285           n_left_from -= 1;
2286           to_next_drop[0] = pi0;
2287           to_next_drop += 1;
2288           n_left_to_next_drop -= 1;
2289
2290           p0->error = node->errors[drop0 ? IP4_ARP_ERROR_DROP : IP4_ARP_ERROR_REQUEST_SENT];
2291
2292           if (drop0)
2293             continue;
2294
2295           /* 
2296            * Can happen if the control-plane is programming tables
2297            * with traffic flowing; at least that's today's lame excuse.
2298            */
2299           if (adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP) 
2300             {
2301               p0->error = node->errors[IP4_ARP_ERROR_NON_ARP_ADJ];
2302             }
2303           else
2304           /* Send ARP request. */
2305           {
2306             u32 bi0 = 0;
2307             vlib_buffer_t * b0;
2308             ethernet_arp_header_t * h0;
2309             vnet_hw_interface_t * hw_if0;
2310
2311             h0 = vlib_packet_template_get_packet (vm, &im->ip4_arp_request_packet_template, &bi0);
2312
2313             /* Add rewrite/encap string for ARP packet. */
2314             vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
2315
2316             hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
2317
2318             /* Src ethernet address in ARP header. */
2319             clib_memcpy (h0->ip4_over_ethernet[0].ethernet, hw_if0->hw_address,
2320                     sizeof (h0->ip4_over_ethernet[0].ethernet));
2321
2322             ip4_src_address_for_packet (im, p0, &h0->ip4_over_ethernet[0].ip4, sw_if_index0);
2323
2324             /* Copy in destination address we are requesting. */
2325             h0->ip4_over_ethernet[1].ip4.data_u32 = ip0->dst_address.data_u32;
2326
2327             vlib_buffer_copy_trace_flag (vm, p0, bi0);
2328             b0 = vlib_get_buffer (vm, bi0);
2329             vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
2330
2331             vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
2332
2333             vlib_set_next_frame_buffer (vm, node, adj0->rewrite_header.next_index, bi0);
2334           }
2335         }
2336
2337       vlib_put_next_frame (vm, node, IP4_ARP_NEXT_DROP, n_left_to_next_drop);
2338     }
2339
2340   return frame->n_vectors;
2341 }
2342
2343 static char * ip4_arp_error_strings[] = {
2344   [IP4_ARP_ERROR_DROP] = "address overflow drops",
2345   [IP4_ARP_ERROR_REQUEST_SENT] = "ARP requests sent",
2346   [IP4_ARP_ERROR_NON_ARP_ADJ] = "ARPs to non-ARP adjacencies",
2347   [IP4_ARP_ERROR_REPLICATE_DROP] = "ARP replication completed",
2348   [IP4_ARP_ERROR_REPLICATE_FAIL] = "ARP replication failed",
2349 };
2350
2351 VLIB_REGISTER_NODE (ip4_arp_node) = {
2352   .function = ip4_arp,
2353   .name = "ip4-arp",
2354   .vector_size = sizeof (u32),
2355
2356   .format_trace = format_ip4_forward_next_trace,
2357
2358   .n_errors = ARRAY_LEN (ip4_arp_error_strings),
2359   .error_strings = ip4_arp_error_strings,
2360
2361   .n_next_nodes = IP4_ARP_N_NEXT,
2362   .next_nodes = {
2363     [IP4_ARP_NEXT_DROP] = "error-drop",
2364   },
2365 };
2366
2367 #define foreach_notrace_ip4_arp_error           \
2368 _(DROP)                                         \
2369 _(REQUEST_SENT)                                 \
2370 _(REPLICATE_DROP)                               \
2371 _(REPLICATE_FAIL)
2372
2373 clib_error_t * arp_notrace_init (vlib_main_t * vm)
2374 {
2375   vlib_node_runtime_t *rt = 
2376     vlib_node_get_runtime (vm, ip4_arp_node.index);
2377
2378   /* don't trace ARP request packets */
2379 #define _(a)                                    \
2380     vnet_pcap_drop_trace_filter_add_del         \
2381         (rt->errors[IP4_ARP_ERROR_##a],         \
2382          1 /* is_add */);
2383     foreach_notrace_ip4_arp_error;
2384 #undef _
2385   return 0;
2386 }
2387
2388 VLIB_INIT_FUNCTION(arp_notrace_init);
2389
2390
2391 /* Send an ARP request to see if given destination is reachable on given interface. */
2392 clib_error_t *
2393 ip4_probe_neighbor (vlib_main_t * vm, ip4_address_t * dst, u32 sw_if_index)
2394 {
2395   vnet_main_t * vnm = vnet_get_main();
2396   ip4_main_t * im = &ip4_main;
2397   ethernet_arp_header_t * h;
2398   ip4_address_t * src;
2399   ip_interface_address_t * ia;
2400   ip_adjacency_t * adj;
2401   vnet_hw_interface_t * hi;
2402   vnet_sw_interface_t * si;
2403   vlib_buffer_t * b;
2404   u32 bi = 0;
2405
2406   si = vnet_get_sw_interface (vnm, sw_if_index);
2407
2408   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2409     {
2410       return clib_error_return (0, "%U: interface %U down",
2411                                 format_ip4_address, dst, 
2412                                 format_vnet_sw_if_index_name, vnm, 
2413                                 sw_if_index);
2414     }
2415
2416   src = ip4_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2417   if (! src)
2418     {
2419       vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2420       return clib_error_return 
2421         (0, "no matching interface address for destination %U (interface %U)",
2422          format_ip4_address, dst,
2423          format_vnet_sw_if_index_name, vnm, sw_if_index);
2424     }
2425
2426   adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2427
2428   h = vlib_packet_template_get_packet (vm, &im->ip4_arp_request_packet_template, &bi);
2429
2430   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2431
2432   clib_memcpy (h->ip4_over_ethernet[0].ethernet, hi->hw_address, sizeof (h->ip4_over_ethernet[0].ethernet));
2433
2434   h->ip4_over_ethernet[0].ip4 = src[0];
2435   h->ip4_over_ethernet[1].ip4 = dst[0];
2436
2437   b = vlib_get_buffer (vm, bi);
2438   vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2439
2440   /* Add encapsulation string for software interface (e.g. ethernet header). */
2441   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2442   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2443
2444   {
2445     vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2446     u32 * to_next = vlib_frame_vector_args (f);
2447     to_next[0] = bi;
2448     f->n_vectors = 1;
2449     vlib_put_frame_to_node (vm, hi->output_node_index, f);
2450   }
2451
2452   return /* no error */ 0;
2453 }
2454
2455 typedef enum {
2456   IP4_REWRITE_NEXT_DROP,
2457   IP4_REWRITE_NEXT_ARP,
2458 } ip4_rewrite_next_t;
2459
2460 always_inline uword
2461 ip4_rewrite_inline (vlib_main_t * vm,
2462                     vlib_node_runtime_t * node,
2463                     vlib_frame_t * frame,
2464                     int rewrite_for_locally_received_packets)
2465 {
2466   ip_lookup_main_t * lm = &ip4_main.lookup_main;
2467   u32 * from = vlib_frame_vector_args (frame);
2468   u32 n_left_from, n_left_to_next, * to_next, next_index;
2469   vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
2470   vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2471
2472   n_left_from = frame->n_vectors;
2473   next_index = node->cached_next_index;
2474   u32 cpu_index = os_get_cpu_number();
2475   
2476   while (n_left_from > 0)
2477     {
2478       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2479
2480       while (n_left_from >= 4 && n_left_to_next >= 2)
2481         {
2482           ip_adjacency_t * adj0, * adj1;
2483           vlib_buffer_t * p0, * p1;
2484           ip4_header_t * ip0, * ip1;
2485           u32 pi0, rw_len0, next0, error0, checksum0, adj_index0;
2486           u32 pi1, rw_len1, next1, error1, checksum1, adj_index1;
2487           u32 next0_override, next1_override;
2488       
2489           if (rewrite_for_locally_received_packets)
2490               next0_override = next1_override = 0;
2491
2492           /* Prefetch next iteration. */
2493           {
2494             vlib_buffer_t * p2, * p3;
2495
2496             p2 = vlib_get_buffer (vm, from[2]);
2497             p3 = vlib_get_buffer (vm, from[3]);
2498
2499             vlib_prefetch_buffer_header (p2, STORE);
2500             vlib_prefetch_buffer_header (p3, STORE);
2501
2502             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2503             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2504           }
2505
2506           pi0 = to_next[0] = from[0];
2507           pi1 = to_next[1] = from[1];
2508
2509           from += 2;
2510           n_left_from -= 2;
2511           to_next += 2;
2512           n_left_to_next -= 2;
2513       
2514           p0 = vlib_get_buffer (vm, pi0);
2515           p1 = vlib_get_buffer (vm, pi1);
2516
2517           adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2518           adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2519
2520           /* We should never rewrite a pkt using the MISS adjacency */
2521           ASSERT(adj_index0 && adj_index1);
2522
2523           ip0 = vlib_buffer_get_current (p0);
2524           ip1 = vlib_buffer_get_current (p1);
2525
2526           error0 = error1 = IP4_ERROR_NONE;
2527
2528           /* Decrement TTL & update checksum.
2529              Works either endian, so no need for byte swap. */
2530           if (! rewrite_for_locally_received_packets)
2531             {
2532               i32 ttl0 = ip0->ttl, ttl1 = ip1->ttl;
2533
2534               /* Input node should have reject packets with ttl 0. */
2535               ASSERT (ip0->ttl > 0);
2536               ASSERT (ip1->ttl > 0);
2537
2538               checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100);
2539               checksum1 = ip1->checksum + clib_host_to_net_u16 (0x0100);
2540
2541               checksum0 += checksum0 >= 0xffff;
2542               checksum1 += checksum1 >= 0xffff;
2543
2544               ip0->checksum = checksum0;
2545               ip1->checksum = checksum1;
2546
2547               ttl0 -= 1;
2548               ttl1 -= 1;
2549
2550               ip0->ttl = ttl0;
2551               ip1->ttl = ttl1;
2552
2553               error0 = ttl0 <= 0 ? IP4_ERROR_TIME_EXPIRED : error0;
2554               error1 = ttl1 <= 0 ? IP4_ERROR_TIME_EXPIRED : error1;
2555
2556               /* Verify checksum. */
2557               ASSERT (ip0->checksum == ip4_header_checksum (ip0));
2558               ASSERT (ip1->checksum == ip4_header_checksum (ip1));
2559             }
2560
2561           /* Rewrite packet header and updates lengths. */
2562           adj0 = ip_get_adjacency (lm, adj_index0);
2563           adj1 = ip_get_adjacency (lm, adj_index1);
2564       
2565           if (rewrite_for_locally_received_packets)
2566             {
2567               /*
2568                * If someone sends e.g. an icmp4 w/ src = dst = interface addr,
2569                * we end up here with a local adjacency in hand
2570                * The local adj rewrite data is 0xfefe on purpose.
2571                * Bad engineer, no donut for you.
2572                */
2573               if (PREDICT_FALSE(adj0->lookup_next_index 
2574                                 == IP_LOOKUP_NEXT_LOCAL))
2575                 error0 = IP4_ERROR_SPOOFED_LOCAL_PACKETS;
2576               if (PREDICT_FALSE(adj0->lookup_next_index
2577                                 == IP_LOOKUP_NEXT_ARP))
2578                 next0_override = IP4_REWRITE_NEXT_ARP;
2579               if (PREDICT_FALSE(adj1->lookup_next_index 
2580                                 == IP_LOOKUP_NEXT_LOCAL))
2581                 error1 = IP4_ERROR_SPOOFED_LOCAL_PACKETS;
2582               if (PREDICT_FALSE(adj1->lookup_next_index
2583                                 == IP_LOOKUP_NEXT_ARP))
2584                 next1_override = IP4_REWRITE_NEXT_ARP;
2585             }
2586
2587           /* Worth pipelining. No guarantee that adj0,1 are hot... */
2588           rw_len0 = adj0[0].rewrite_header.data_bytes;
2589           rw_len1 = adj1[0].rewrite_header.data_bytes;
2590           next0 = (error0 == IP4_ERROR_NONE) 
2591             ? adj0[0].rewrite_header.next_index : 0;
2592
2593           if (rewrite_for_locally_received_packets)
2594               next0 = next0 && next0_override ? next0_override : next0;
2595
2596           next1 = (error1 == IP4_ERROR_NONE)
2597             ? adj1[0].rewrite_header.next_index : 0;
2598
2599           if (rewrite_for_locally_received_packets)
2600               next1 = next1 && next1_override ? next1_override : next1;
2601
2602           /* 
2603            * We've already accounted for an ethernet_header_t elsewhere
2604            */
2605           if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t)))
2606               vlib_increment_combined_counter 
2607                   (&lm->adjacency_counters,
2608                    cpu_index, adj_index0, 
2609                    /* packet increment */ 0,
2610                    /* byte increment */ rw_len0-sizeof(ethernet_header_t));
2611
2612           if (PREDICT_FALSE (rw_len1 > sizeof(ethernet_header_t)))
2613               vlib_increment_combined_counter 
2614                   (&lm->adjacency_counters,
2615                    cpu_index, adj_index1, 
2616                    /* packet increment */ 0,
2617                    /* byte increment */ rw_len1-sizeof(ethernet_header_t));
2618
2619           /* Check MTU of outgoing interface. */
2620           error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2621                     ? IP4_ERROR_MTU_EXCEEDED
2622                     : error0);
2623           error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2624                     ? IP4_ERROR_MTU_EXCEEDED
2625                     : error1);
2626
2627           p0->current_data -= rw_len0;
2628           p1->current_data -= rw_len1;
2629
2630           p0->current_length += rw_len0;
2631           p1->current_length += rw_len1;
2632
2633           vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2634           vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2635       
2636           p0->error = error_node->errors[error0];
2637           p1->error = error_node->errors[error1];
2638
2639           /* Guess we are only writing on simple Ethernet header. */
2640           vnet_rewrite_two_headers (adj0[0], adj1[0],
2641                                     ip0, ip1,
2642                                     sizeof (ethernet_header_t));
2643       
2644           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2645                                            to_next, n_left_to_next,
2646                                            pi0, pi1, next0, next1);
2647         }
2648
2649       while (n_left_from > 0 && n_left_to_next > 0)
2650         {
2651           ip_adjacency_t * adj0;
2652           vlib_buffer_t * p0;
2653           ip4_header_t * ip0;
2654           u32 pi0, rw_len0, adj_index0, next0, error0, checksum0;
2655           u32 next0_override;
2656       
2657           if (rewrite_for_locally_received_packets)
2658               next0_override = 0;
2659
2660           pi0 = to_next[0] = from[0];
2661
2662           p0 = vlib_get_buffer (vm, pi0);
2663
2664           adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2665
2666           /* We should never rewrite a pkt using the MISS adjacency */
2667           ASSERT(adj_index0);
2668
2669           adj0 = ip_get_adjacency (lm, adj_index0);
2670       
2671           ip0 = vlib_buffer_get_current (p0);
2672
2673           error0 = IP4_ERROR_NONE;
2674           next0 = 0;            /* drop on error */
2675
2676           /* Decrement TTL & update checksum. */
2677           if (! rewrite_for_locally_received_packets)
2678             {
2679               i32 ttl0 = ip0->ttl;
2680
2681               checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100);
2682
2683               checksum0 += checksum0 >= 0xffff;
2684
2685               ip0->checksum = checksum0;
2686
2687               ASSERT (ip0->ttl > 0);
2688
2689               ttl0 -= 1;
2690
2691               ip0->ttl = ttl0;
2692
2693               ASSERT (ip0->checksum == ip4_header_checksum (ip0));
2694
2695               error0 = ttl0 <= 0 ? IP4_ERROR_TIME_EXPIRED : error0;
2696             }
2697
2698           if (rewrite_for_locally_received_packets)
2699             {
2700               /*
2701                * If someone sends e.g. an icmp4 w/ src = dst = interface addr,
2702                * we end up here with a local adjacency in hand
2703                * The local adj rewrite data is 0xfefe on purpose.
2704                * Bad engineer, no donut for you.
2705                */
2706               if (PREDICT_FALSE(adj0->lookup_next_index 
2707                                 == IP_LOOKUP_NEXT_LOCAL))
2708                 error0 = IP4_ERROR_SPOOFED_LOCAL_PACKETS;
2709               /* 
2710                * We have to override the next_index in ARP adjacencies,
2711                * because they're set up for ip4-arp, not this node...
2712                */
2713               if (PREDICT_FALSE(adj0->lookup_next_index
2714                                 == IP_LOOKUP_NEXT_ARP))
2715                 next0_override = IP4_REWRITE_NEXT_ARP;
2716             }
2717
2718           /* Guess we are only writing on simple Ethernet header. */
2719           vnet_rewrite_one_header (adj0[0], ip0, 
2720                                    sizeof (ethernet_header_t));
2721           
2722           /* Update packet buffer attributes/set output interface. */
2723           rw_len0 = adj0[0].rewrite_header.data_bytes;
2724           
2725           if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t)))
2726               vlib_increment_combined_counter 
2727                   (&lm->adjacency_counters,
2728                    cpu_index, adj_index0, 
2729                    /* packet increment */ 0,
2730                    /* byte increment */ rw_len0-sizeof(ethernet_header_t));
2731           
2732           /* Check MTU of outgoing interface. */
2733           error0 = (vlib_buffer_length_in_chain (vm, p0) 
2734                     > adj0[0].rewrite_header.max_l3_packet_bytes
2735                     ? IP4_ERROR_MTU_EXCEEDED
2736                     : error0);
2737           
2738           p0->error = error_node->errors[error0];
2739           p0->current_data -= rw_len0;
2740           p0->current_length += rw_len0;
2741           vnet_buffer (p0)->sw_if_index[VLIB_TX] = 
2742             adj0[0].rewrite_header.sw_if_index;
2743           
2744           next0 = (error0 == IP4_ERROR_NONE)
2745             ? adj0[0].rewrite_header.next_index : 0;
2746
2747           if (rewrite_for_locally_received_packets)
2748               next0 = next0 && next0_override ? next0_override : next0;
2749
2750           from += 1;
2751           n_left_from -= 1;
2752           to_next += 1;
2753           n_left_to_next -= 1;
2754       
2755           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2756                                            to_next, n_left_to_next,
2757                                            pi0, next0);
2758         }
2759   
2760       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2761     }
2762
2763   /* Need to do trace after rewrites to pick up new packet data. */
2764   if (node->flags & VLIB_NODE_FLAG_TRACE)
2765     ip4_forward_next_trace (vm, node, frame, adj_rx_tx);
2766
2767   return frame->n_vectors;
2768 }
2769
2770 static uword
2771 ip4_rewrite_transit (vlib_main_t * vm,
2772                      vlib_node_runtime_t * node,
2773                      vlib_frame_t * frame)
2774 {
2775   return ip4_rewrite_inline (vm, node, frame,
2776                              /* rewrite_for_locally_received_packets */ 0);
2777 }
2778
2779 static uword
2780 ip4_rewrite_local (vlib_main_t * vm,
2781                    vlib_node_runtime_t * node,
2782                    vlib_frame_t * frame)
2783 {
2784   return ip4_rewrite_inline (vm, node, frame,
2785                              /* rewrite_for_locally_received_packets */ 1);
2786 }
2787
2788 VLIB_REGISTER_NODE (ip4_rewrite_node) = {
2789   .function = ip4_rewrite_transit,
2790   .name = "ip4-rewrite-transit",
2791   .vector_size = sizeof (u32),
2792
2793   .format_trace = format_ip4_rewrite_trace,
2794
2795   .n_next_nodes = 2,
2796   .next_nodes = {
2797     [IP4_REWRITE_NEXT_DROP] = "error-drop",
2798     [IP4_REWRITE_NEXT_ARP] = "ip4-arp",
2799   },
2800 };
2801
2802 VLIB_NODE_FUNCTION_MULTIARCH (ip4_rewrite_node, ip4_rewrite_transit)
2803
2804 VLIB_REGISTER_NODE (ip4_rewrite_local_node) = {
2805   .function = ip4_rewrite_local,
2806   .name = "ip4-rewrite-local",
2807   .vector_size = sizeof (u32),
2808
2809   .sibling_of = "ip4-rewrite-transit",
2810
2811   .format_trace = format_ip4_rewrite_trace,
2812
2813   .n_next_nodes = 2,
2814   .next_nodes = {
2815     [IP4_REWRITE_NEXT_DROP] = "error-drop",
2816     [IP4_REWRITE_NEXT_ARP] = "ip4-arp",
2817   },
2818 };
2819
2820 VLIB_NODE_FUNCTION_MULTIARCH (ip4_rewrite_local_node, ip4_rewrite_local)
2821
2822 static clib_error_t *
2823 add_del_interface_table (vlib_main_t * vm,
2824                          unformat_input_t * input,
2825                          vlib_cli_command_t * cmd)
2826 {
2827   vnet_main_t * vnm = vnet_get_main();
2828   clib_error_t * error = 0;
2829   u32 sw_if_index, table_id;
2830
2831   sw_if_index = ~0;
2832
2833   if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2834     {
2835       error = clib_error_return (0, "unknown interface `%U'",
2836                                  format_unformat_error, input);
2837       goto done;
2838     }
2839
2840   if (unformat (input, "%d", &table_id))
2841     ;
2842   else
2843     {
2844       error = clib_error_return (0, "expected table id `%U'",
2845                                  format_unformat_error, input);
2846       goto done;
2847     }
2848
2849   {
2850     ip4_main_t * im = &ip4_main;
2851     ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id (im, table_id, IP4_ROUTE_FLAG_TABLE_ID);
2852
2853     if (fib) 
2854       {
2855         vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2856         im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2857     }
2858   }
2859
2860  done:
2861   return error;
2862 }
2863
2864 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2865   .path = "set interface ip table",
2866   .function = add_del_interface_table,
2867   .short_help = "Add/delete FIB table id for interface",
2868 };
2869
2870
2871 static uword
2872 ip4_lookup_multicast (vlib_main_t * vm,
2873                       vlib_node_runtime_t * node,
2874                       vlib_frame_t * frame)
2875 {
2876   ip4_main_t * im = &ip4_main;
2877   ip_lookup_main_t * lm = &im->lookup_main;
2878   vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
2879   u32 n_left_from, n_left_to_next, * from, * to_next;
2880   ip_lookup_next_t next;
2881   u32 cpu_index = os_get_cpu_number();
2882
2883   from = vlib_frame_vector_args (frame);
2884   n_left_from = frame->n_vectors;
2885   next = node->cached_next_index;
2886
2887   while (n_left_from > 0)
2888     {
2889       vlib_get_next_frame (vm, node, next,
2890                            to_next, n_left_to_next);
2891
2892       while (n_left_from >= 4 && n_left_to_next >= 2)
2893         {
2894           vlib_buffer_t * p0, * p1;
2895           u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
2896           ip_lookup_next_t next0, next1;
2897           ip4_header_t * ip0, * ip1;
2898           ip_adjacency_t * adj0, * adj1;
2899           u32 fib_index0, fib_index1;
2900           u32 flow_hash_config0, flow_hash_config1;
2901
2902           /* Prefetch next iteration. */
2903           {
2904             vlib_buffer_t * p2, * p3;
2905
2906             p2 = vlib_get_buffer (vm, from[2]);
2907             p3 = vlib_get_buffer (vm, from[3]);
2908
2909             vlib_prefetch_buffer_header (p2, LOAD);
2910             vlib_prefetch_buffer_header (p3, LOAD);
2911
2912             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
2913             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
2914           }
2915
2916           pi0 = to_next[0] = from[0];
2917           pi1 = to_next[1] = from[1];
2918
2919           p0 = vlib_get_buffer (vm, pi0);
2920           p1 = vlib_get_buffer (vm, pi1);
2921
2922           ip0 = vlib_buffer_get_current (p0);
2923           ip1 = vlib_buffer_get_current (p1);
2924
2925           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
2926           fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
2927           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
2928             fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
2929           fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
2930             fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
2931
2932           adj_index0 = ip4_fib_lookup_buffer (im, fib_index0, 
2933                                               &ip0->dst_address, p0);
2934           adj_index1 = ip4_fib_lookup_buffer (im, fib_index1, 
2935                                               &ip1->dst_address, p1);
2936
2937           adj0 = ip_get_adjacency (lm, adj_index0);
2938           adj1 = ip_get_adjacency (lm, adj_index1);
2939
2940           next0 = adj0->lookup_next_index;
2941           next1 = adj1->lookup_next_index;
2942
2943           flow_hash_config0 = 
2944               vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
2945
2946           flow_hash_config1 = 
2947               vec_elt_at_index (im->fibs, fib_index1)->flow_hash_config;
2948
2949           vnet_buffer (p0)->ip.flow_hash = ip4_compute_flow_hash 
2950               (ip0, flow_hash_config0);
2951                                                                   
2952           vnet_buffer (p1)->ip.flow_hash = ip4_compute_flow_hash 
2953               (ip1, flow_hash_config1);
2954
2955           ASSERT (adj0->n_adj > 0);
2956           ASSERT (adj1->n_adj > 0);
2957           ASSERT (is_pow2 (adj0->n_adj));
2958           ASSERT (is_pow2 (adj1->n_adj));
2959           adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
2960           adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
2961
2962           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
2963           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
2964
2965           if (1) /* $$$$$$ HACK FIXME */
2966           vlib_increment_combined_counter 
2967               (cm, cpu_index, adj_index0, 1,
2968                vlib_buffer_length_in_chain (vm, p0));
2969           if (1) /* $$$$$$ HACK FIXME */
2970           vlib_increment_combined_counter 
2971               (cm, cpu_index, adj_index1, 1,
2972                vlib_buffer_length_in_chain (vm, p1));
2973
2974           from += 2;
2975           to_next += 2;
2976           n_left_to_next -= 2;
2977           n_left_from -= 2;
2978
2979           wrong_next = (next0 != next) + 2*(next1 != next);
2980           if (PREDICT_FALSE (wrong_next != 0))
2981             {
2982               switch (wrong_next)
2983                 {
2984                 case 1:
2985                   /* A B A */
2986                   to_next[-2] = pi1;
2987                   to_next -= 1;
2988                   n_left_to_next += 1;
2989                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
2990                   break;
2991
2992                 case 2:
2993                   /* A A B */
2994                   to_next -= 1;
2995                   n_left_to_next += 1;
2996                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
2997                   break;
2998
2999                 case 3:
3000                   /* A B C */
3001                   to_next -= 2;
3002                   n_left_to_next += 2;
3003                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
3004                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
3005                   if (next0 == next1)
3006                     {
3007                       /* A B B */
3008                       vlib_put_next_frame (vm, node, next, n_left_to_next);
3009                       next = next1;
3010                       vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
3011                     }
3012                 }
3013             }
3014         }
3015     
3016       while (n_left_from > 0 && n_left_to_next > 0)
3017         {
3018           vlib_buffer_t * p0;
3019           ip4_header_t * ip0;
3020           u32 pi0, adj_index0;
3021           ip_lookup_next_t next0;
3022           ip_adjacency_t * adj0;
3023           u32 fib_index0;
3024           u32 flow_hash_config0;
3025
3026           pi0 = from[0];
3027           to_next[0] = pi0;
3028
3029           p0 = vlib_get_buffer (vm, pi0);
3030
3031           ip0 = vlib_buffer_get_current (p0);
3032
3033           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, 
3034                                 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
3035           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
3036               fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
3037           
3038           adj_index0 = ip4_fib_lookup_buffer (im, fib_index0, 
3039                                               &ip0->dst_address, p0);
3040
3041           adj0 = ip_get_adjacency (lm, adj_index0);
3042
3043           next0 = adj0->lookup_next_index;
3044
3045           flow_hash_config0 = 
3046               vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
3047
3048           vnet_buffer (p0)->ip.flow_hash = 
3049             ip4_compute_flow_hash (ip0, flow_hash_config0);
3050
3051           ASSERT (adj0->n_adj > 0);
3052           ASSERT (is_pow2 (adj0->n_adj));
3053           adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
3054
3055           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
3056
3057           if (1) /* $$$$$$ HACK FIXME */
3058               vlib_increment_combined_counter 
3059                   (cm, cpu_index, adj_index0, 1,
3060                    vlib_buffer_length_in_chain (vm, p0));
3061
3062           from += 1;
3063           to_next += 1;
3064           n_left_to_next -= 1;
3065           n_left_from -= 1;
3066
3067           if (PREDICT_FALSE (next0 != next))
3068             {
3069               n_left_to_next += 1;
3070               vlib_put_next_frame (vm, node, next, n_left_to_next);
3071               next = next0;
3072               vlib_get_next_frame (vm, node, next,
3073                                    to_next, n_left_to_next);
3074               to_next[0] = pi0;
3075               to_next += 1;
3076               n_left_to_next -= 1;
3077             }
3078         }
3079
3080       vlib_put_next_frame (vm, node, next, n_left_to_next);
3081     }
3082
3083   if (node->flags & VLIB_NODE_FLAG_TRACE)
3084       ip4_forward_next_trace(vm, node, frame, VLIB_TX);
3085
3086   return frame->n_vectors;
3087 }
3088
3089 VLIB_REGISTER_NODE (ip4_lookup_multicast_node,static) = {
3090   .function = ip4_lookup_multicast,
3091   .name = "ip4-lookup-multicast",
3092   .vector_size = sizeof (u32),
3093
3094   .format_trace = format_ip4_lookup_trace,
3095
3096   .n_next_nodes = IP_LOOKUP_N_NEXT,
3097   .next_nodes = IP4_LOOKUP_NEXT_NODES,
3098 };
3099
3100 VLIB_NODE_FUNCTION_MULTIARCH (ip4_lookup_multicast_node, ip4_lookup_multicast)
3101
3102 VLIB_REGISTER_NODE (ip4_multicast_node,static) = {
3103   .function = ip4_drop,
3104   .name = "ip4-multicast",
3105   .vector_size = sizeof (u32),
3106
3107   .format_trace = format_ip4_forward_next_trace,
3108
3109   .n_next_nodes = 1,
3110   .next_nodes = {
3111     [0] = "error-drop",
3112   },
3113 };
3114
3115 int ip4_lookup_validate (ip4_address_t *a, u32 fib_index0)
3116 {
3117   ip4_main_t * im = &ip4_main;
3118   ip4_fib_mtrie_t * mtrie0;
3119   ip4_fib_mtrie_leaf_t leaf0;
3120   u32 adj_index0;
3121     
3122   mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
3123
3124   leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
3125   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 0);
3126   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 1);
3127   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 2);
3128   leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 3);
3129   
3130   /* Handle default route. */
3131   leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
3132   
3133   adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
3134   
3135   return adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
3136                                                   a, 
3137                                                   /* no_default_route */ 0);
3138 }
3139  
3140 static clib_error_t *
3141 test_lookup_command_fn (vlib_main_t * vm,
3142                         unformat_input_t * input,
3143                         vlib_cli_command_t * cmd)
3144 {
3145   u32 table_id = 0;
3146   f64 count = 1;
3147   u32 n;
3148   int i;
3149   ip4_address_t ip4_base_address;
3150   u64 errors = 0;
3151
3152   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3153       if (unformat (input, "table %d", &table_id))
3154         ;
3155       else if (unformat (input, "count %f", &count))
3156         ;
3157
3158       else if (unformat (input, "%U",
3159                          unformat_ip4_address, &ip4_base_address))
3160         ;
3161       else
3162         return clib_error_return (0, "unknown input `%U'",
3163                                   format_unformat_error, input);
3164   }
3165
3166   n = count;
3167
3168   for (i = 0; i < n; i++)
3169     {
3170       if (!ip4_lookup_validate (&ip4_base_address, table_id))
3171         errors++;
3172
3173       ip4_base_address.as_u32 = 
3174         clib_host_to_net_u32 (1 + 
3175                               clib_net_to_host_u32 (ip4_base_address.as_u32));
3176     }
3177
3178   if (errors) 
3179     vlib_cli_output (vm, "%llu errors out of %d lookups\n", errors, n);
3180   else
3181     vlib_cli_output (vm, "No errors in %d lookups\n", n);
3182
3183   return 0;
3184 }
3185
3186 VLIB_CLI_COMMAND (lookup_test_command, static) = {
3187     .path = "test lookup",
3188     .short_help = "test lookup",
3189     .function = test_lookup_command_fn,
3190 };
3191
3192 int vnet_set_ip4_flow_hash (u32 table_id, u32 flow_hash_config)
3193 {
3194   ip4_main_t * im4 = &ip4_main;
3195   ip4_fib_t * fib;
3196   uword * p = hash_get (im4->fib_index_by_table_id, table_id);
3197
3198   if (p == 0)
3199     return VNET_API_ERROR_NO_SUCH_FIB;
3200
3201   fib = vec_elt_at_index (im4->fibs, p[0]);
3202
3203   fib->flow_hash_config = flow_hash_config;
3204   return 0;
3205 }
3206  
3207 static clib_error_t *
3208 set_ip_flow_hash_command_fn (vlib_main_t * vm,
3209                              unformat_input_t * input,
3210                              vlib_cli_command_t * cmd)
3211 {
3212   int matched = 0;
3213   u32 table_id = 0;
3214   u32 flow_hash_config = 0;
3215   int rv;
3216
3217   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3218     if (unformat (input, "table %d", &table_id))
3219       matched = 1;
3220 #define _(a,v) \
3221     else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3222     foreach_flow_hash_bit
3223 #undef _
3224     else break;
3225   }
3226   
3227   if (matched == 0)
3228     return clib_error_return (0, "unknown input `%U'",
3229                               format_unformat_error, input);
3230   
3231   rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3232   switch (rv)
3233     {
3234     case 0:
3235       break;
3236       
3237     case VNET_API_ERROR_NO_SUCH_FIB:
3238       return clib_error_return (0, "no such FIB table %d", table_id);
3239       
3240     default:
3241       clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3242       break;
3243     }
3244   
3245   return 0;
3246 }
3247  
3248 VLIB_CLI_COMMAND (set_ip_flow_hash_command, static) = {
3249   .path = "set ip flow-hash",
3250   .short_help = 
3251   "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
3252   .function = set_ip_flow_hash_command_fn,
3253 };
3254  
3255 int vnet_set_ip4_classify_intfc (vlib_main_t * vm, u32 sw_if_index, 
3256                                  u32 table_index)
3257 {
3258   vnet_main_t * vnm = vnet_get_main();
3259   vnet_interface_main_t * im = &vnm->interface_main;
3260   ip4_main_t * ipm = &ip4_main;
3261   ip_lookup_main_t * lm = &ipm->lookup_main;
3262   vnet_classify_main_t * cm = &vnet_classify_main;
3263
3264   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3265     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3266
3267   if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3268     return VNET_API_ERROR_NO_SUCH_ENTRY;
3269
3270   vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3271   lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
3272
3273   return 0;
3274 }
3275
3276 static clib_error_t *
3277 set_ip_classify_command_fn (vlib_main_t * vm,
3278                             unformat_input_t * input,
3279                             vlib_cli_command_t * cmd)
3280 {
3281   u32 table_index = ~0;
3282   int table_index_set = 0;
3283   u32 sw_if_index = ~0;
3284   int rv;
3285   
3286   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3287     if (unformat (input, "table-index %d", &table_index))
3288       table_index_set = 1;
3289     else if (unformat (input, "intfc %U", unformat_vnet_sw_interface, 
3290                        vnet_get_main(), &sw_if_index))
3291       ;
3292     else
3293       break;
3294   }
3295       
3296   if (table_index_set == 0)
3297     return clib_error_return (0, "classify table-index must be specified");
3298
3299   if (sw_if_index == ~0)
3300     return clib_error_return (0, "interface / subif must be specified");
3301
3302   rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3303
3304   switch (rv)
3305     {
3306     case 0:
3307       break;
3308
3309     case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3310       return clib_error_return (0, "No such interface");
3311
3312     case VNET_API_ERROR_NO_SUCH_ENTRY:
3313       return clib_error_return (0, "No such classifier table");
3314     }
3315   return 0;
3316 }
3317
3318 VLIB_CLI_COMMAND (set_ip_classify_command, static) = {
3319     .path = "set ip classify",
3320     .short_help = 
3321     "set ip classify intfc <int> table-index <index>",
3322     .function = set_ip_classify_command_fn,
3323 };
3324