IP6 FIB: Micro optimization in lookup
[vpp.git] / vnet / vnet / ip / ip6_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/ip6_forward.c: IP v6 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/srp/srp.h>       /* for srp_hw_interface_class */
44 #include <vppinfra/cache.h>
45
46 #include <vppinfra/bihash_template.c>
47
48 static void compute_prefix_lengths_in_search_order (ip6_main_t * im)
49 {
50   int i;
51   vec_reset_length (im->prefix_lengths_in_search_order);
52   /* Note: bitmap reversed so this is in fact a longest prefix match */
53   clib_bitmap_foreach (i, im->non_empty_dst_address_length_bitmap,
54   ({
55     int dst_address_length = 128 - i;
56     vec_add1 (im->prefix_lengths_in_search_order, dst_address_length);
57   }));
58 }
59
60 u32 
61 ip6_fib_lookup_with_table (ip6_main_t * im, u32 fib_index, ip6_address_t * dst)
62 {
63   ip_lookup_main_t * lm = &im->lookup_main;
64   int i, len;
65   int rv;
66   BVT(clib_bihash_kv) kv, value;
67   u64 fib;
68
69   len = vec_len (im->prefix_lengths_in_search_order);
70
71   kv.key[0] = dst->as_u64[0];
72   kv.key[1] = dst->as_u64[1];
73   fib = ((u64)((fib_index))<<32);
74
75   for (i = 0; i < len; i++)
76     {
77       int dst_address_length = im->prefix_lengths_in_search_order[i];
78       ip6_address_t * mask = &im->fib_masks[dst_address_length];
79       
80       ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
81       //As lengths are decreasing, masks are increasingly specific.
82       kv.key[0] &= mask->as_u64[0];
83       kv.key[1] &= mask->as_u64[1];
84       kv.key[2] = fib | dst_address_length;
85       
86       rv = BV(clib_bihash_search_inline_2)(&im->ip6_lookup_table, &kv, &value);
87       if (rv == 0)
88         return value.value;
89     }
90
91   return lm->miss_adj_index;
92 }
93
94 u32 ip6_fib_lookup (ip6_main_t * im, u32 sw_if_index, ip6_address_t * dst)
95 {
96     u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
97     return ip6_fib_lookup_with_table (im, fib_index, dst);
98 }
99
100 void
101 vnet_ip6_fib_init (ip6_main_t * im, u32 fib_index)
102 {
103   ip_lookup_main_t * lm = &im->lookup_main;
104   ip6_add_del_route_args_t a;
105   ip_adjacency_t * adj;
106
107   memset(&a, 0x0, sizeof(ip6_add_del_route_args_t));
108
109   a.table_index_or_table_id = fib_index;
110   a.flags = (IP6_ROUTE_FLAG_ADD
111              | IP6_ROUTE_FLAG_FIB_INDEX
112              | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
113              | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
114
115   /* Add ff02::1:ff00:0/104 via local route for all tables.
116      This is required for neighbor discovery to work. */
117   adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
118                           &a.adj_index);
119   adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
120   adj->if_address_index = ~0;
121   adj->rewrite_header.data_bytes = 0;
122
123   ip6_set_solicited_node_multicast_address (&a.dst_address, 0);
124
125   a.dst_address_length = 104;
126   ip6_add_del_route (im, &a);
127
128   /* Add all-routers multicast address via local route for all tables */
129   adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
130                           &a.adj_index);
131   adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
132   adj->if_address_index = ~0;
133   adj->rewrite_header.data_bytes = 0;
134
135   ip6_set_reserved_multicast_address (&a.dst_address,
136                                       IP6_MULTICAST_SCOPE_link_local,
137                                       IP6_MULTICAST_GROUP_ID_all_routers);
138   
139   a.dst_address_length = 128;  
140   ip6_add_del_route (im, &a);
141
142   /* Add all-nodes multicast address via local route for all tables */
143   adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
144                           &a.adj_index);
145   adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
146   adj->if_address_index = ~0;
147   adj->rewrite_header.data_bytes = 0;
148
149   ip6_set_reserved_multicast_address (&a.dst_address,
150                                       IP6_MULTICAST_SCOPE_link_local,
151                                       IP6_MULTICAST_GROUP_ID_all_hosts);
152
153   a.dst_address_length = 128;
154   ip6_add_del_route (im, &a);
155
156   /* Add all-mldv2  multicast address via local route for all tables */
157   adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
158                           &a.adj_index);
159   adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
160   adj->if_address_index = ~0;
161   adj->rewrite_header.data_bytes = 0;
162   
163   ip6_set_reserved_multicast_address (&a.dst_address,
164                                       IP6_MULTICAST_SCOPE_link_local,
165                                       IP6_MULTICAST_GROUP_ID_mldv2_routers);
166
167   a.dst_address_length = 128;
168   ip6_add_del_route (im, &a);
169 }
170
171 static ip6_fib_t *
172 create_fib_with_table_id (ip6_main_t * im, u32 table_id)
173 {
174   ip6_fib_t * fib;
175   hash_set (im->fib_index_by_table_id, table_id, vec_len (im->fibs));
176   vec_add2 (im->fibs, fib, 1);
177   fib->table_id = table_id;
178   fib->index = fib - im->fibs;
179   fib->flow_hash_config = IP_FLOW_HASH_DEFAULT;
180   vnet_ip6_fib_init (im, fib->index);
181   return fib;
182 }
183
184 ip6_fib_t *
185 find_ip6_fib_by_table_index_or_id (ip6_main_t * im, u32 table_index_or_id, u32 flags)
186 {
187   uword * p, fib_index;
188
189   fib_index = table_index_or_id;
190   if (! (flags & IP6_ROUTE_FLAG_FIB_INDEX))
191     {
192       p = hash_get (im->fib_index_by_table_id, table_index_or_id);
193       if (! p)
194         return create_fib_with_table_id (im, table_index_or_id);
195       fib_index = p[0];
196     }
197   return vec_elt_at_index (im->fibs, fib_index);
198 }
199
200 void ip6_add_del_route (ip6_main_t * im, ip6_add_del_route_args_t * a)
201 {
202   ip_lookup_main_t * lm = &im->lookup_main;
203   ip6_fib_t * fib;
204   ip6_address_t dst_address;
205   u32 dst_address_length, adj_index;
206   uword is_del;
207   u32 old_adj_index = ~0;
208   BVT(clib_bihash_kv) kv, value;
209
210   vlib_smp_unsafe_warning();
211
212   is_del = (a->flags & IP6_ROUTE_FLAG_DEL) != 0;
213
214   /* Either create new adjacency or use given one depending on arguments. */
215   if (a->n_add_adj > 0)
216     {
217       ip_add_adjacency (lm, a->add_adj, a->n_add_adj, &adj_index);
218       ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
219     }
220   else
221     adj_index = a->adj_index;
222
223   dst_address = a->dst_address;
224   dst_address_length = a->dst_address_length;
225   fib = find_ip6_fib_by_table_index_or_id (im, a->table_index_or_table_id, 
226                                            a->flags);
227
228   ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
229   ip6_address_mask (&dst_address, &im->fib_masks[dst_address_length]);
230
231   /* refcount accounting */
232   if (is_del)
233     {
234       ASSERT (im->dst_address_length_refcounts[dst_address_length] > 0);
235       if (--im->dst_address_length_refcounts[dst_address_length] == 0)
236         {
237           im->non_empty_dst_address_length_bitmap =
238             clib_bitmap_set (im->non_empty_dst_address_length_bitmap, 
239                              128 - dst_address_length, 0);
240           compute_prefix_lengths_in_search_order (im);
241         }
242     }
243   else
244     {
245       im->dst_address_length_refcounts[dst_address_length]++;
246
247       im->non_empty_dst_address_length_bitmap =
248         clib_bitmap_set (im->non_empty_dst_address_length_bitmap, 
249                              128 - dst_address_length, 1);
250       compute_prefix_lengths_in_search_order (im);
251     }
252     
253   kv.key[0] = dst_address.as_u64[0];
254   kv.key[1] = dst_address.as_u64[1];
255   kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
256
257   if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
258     old_adj_index = value.value;
259
260   if (is_del)
261     BV(clib_bihash_add_del) (&im->ip6_lookup_table, &kv, 0 /* is_add */);
262   else
263     {
264       /* Make sure adj index is valid. */
265       if (CLIB_DEBUG > 0)
266         (void) ip_get_adjacency (lm, adj_index);
267
268       kv.value = adj_index;
269
270       BV(clib_bihash_add_del) (&im->ip6_lookup_table, &kv, 1 /* is_add */);
271     }
272
273   /* Avoid spurious reference count increments */
274   if (old_adj_index == adj_index && !(a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY))
275     {
276       ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
277       if (adj->share_count > 0)
278         adj->share_count --;
279     }
280
281   /* Delete old adjacency index if present and changed. */
282   {
283     if (! (a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY)
284         && old_adj_index != ~0
285         && old_adj_index != adj_index)
286       ip_del_adjacency (lm, old_adj_index);
287   }
288 }
289
290 void
291 ip6_add_del_route_next_hop (ip6_main_t * im,
292                             u32 flags,
293                             ip6_address_t * dst_address,
294                             u32 dst_address_length,
295                             ip6_address_t * next_hop,
296                             u32 next_hop_sw_if_index,
297                             u32 next_hop_weight, u32 adj_index,
298                             u32 explicit_fib_index)
299 {
300   vnet_main_t * vnm = vnet_get_main();
301   ip_lookup_main_t * lm = &im->lookup_main;
302   u32 fib_index;
303   ip6_fib_t * fib;
304   ip6_address_t masked_dst_address;
305   u32 old_mp_adj_index, new_mp_adj_index;
306   u32 dst_adj_index, nh_adj_index;
307   int rv;
308   ip_adjacency_t * dst_adj;
309   ip_multipath_adjacency_t * old_mp, * new_mp;
310   int is_del = (flags & IP6_ROUTE_FLAG_DEL) != 0;
311   int is_interface_next_hop;
312   clib_error_t * error = 0;
313   uword * nh_result;
314   BVT(clib_bihash_kv) kv, value;
315
316   vlib_smp_unsafe_warning();
317
318   if (explicit_fib_index == (u32)~0)
319     fib_index = vec_elt (im->fib_index_by_sw_if_index, next_hop_sw_if_index);
320   else
321     fib_index = explicit_fib_index;
322
323   fib = vec_elt_at_index (im->fibs, fib_index);
324
325   /* Lookup next hop to be added or deleted. */
326   is_interface_next_hop = ip6_address_is_zero (next_hop);
327   if (adj_index == (u32)~0)
328     {
329       if (is_interface_next_hop)
330         {
331           nh_result = hash_get (im->interface_route_adj_index_by_sw_if_index, 
332                                 next_hop_sw_if_index);
333           if (nh_result)
334             nh_adj_index = *nh_result;
335           else
336             {
337               ip_adjacency_t * adj;
338               adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
339                                       &nh_adj_index);
340               ip6_adjacency_set_interface_route (vnm, adj, 
341                                                  next_hop_sw_if_index, ~0);
342               ip_call_add_del_adjacency_callbacks 
343                 (lm, next_hop_sw_if_index, /* is_del */ 0);
344               hash_set (im->interface_route_adj_index_by_sw_if_index, 
345                         next_hop_sw_if_index, nh_adj_index);
346             }
347         }
348       else
349         {
350           /* Look for the interface /128 route */
351           kv.key[0] = next_hop->as_u64[0];
352           kv.key[1] = next_hop->as_u64[1];
353           kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
354
355           if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
356             {
357               vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
358               error = clib_error_return (0, "next-hop %U/128 not in FIB",
359                                          format_ip6_address, next_hop);
360               goto done;
361             }
362           
363           nh_adj_index = value.value;
364         }
365     }
366   else
367     {
368       /* Look for the interface /128 route */
369       kv.key[0] = next_hop->as_u64[0];
370       kv.key[1] = next_hop->as_u64[1];
371       kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
372       
373       if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
374         {
375           vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
376           error = clib_error_return (0, "next-hop %U/128 not in FIB",
377                                      format_ip6_address, next_hop);
378           goto done;
379         }
380       
381       nh_adj_index = value.value;
382     }
383
384   ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
385   masked_dst_address = dst_address[0];
386   ip6_address_mask (&masked_dst_address, &im->fib_masks[dst_address_length]);
387
388   kv.key[0] = masked_dst_address.as_u64[0];
389   kv.key[1] = masked_dst_address.as_u64[1];
390   kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
391
392   rv = BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value);
393
394   if (rv == 0)
395     {
396       dst_adj_index = value.value;
397       dst_adj = ip_get_adjacency (lm, dst_adj_index);
398     }
399   else
400     {
401       /* For deletes destination must be known. */
402       if (is_del)
403         {
404           vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
405           error = clib_error_return (0, "unknown destination %U/%d",
406                                      format_ip6_address, dst_address,
407                                      dst_address_length);
408           goto done;
409         }
410
411       dst_adj_index = ~0;
412       dst_adj = 0;
413     }
414
415   /* Ignore adds of X/128 with next hop of X. */
416   if (! is_del
417       && dst_address_length == 128
418       && ip6_address_is_equal (dst_address, next_hop))
419     {
420       vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
421       error = clib_error_return (0, "prefix matches next hop %U/%d",
422                                  format_ip6_address, dst_address,
423                                  dst_address_length);
424       goto done;
425     }
426
427   old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
428
429   if (! ip_multipath_adjacency_add_del_next_hop
430       (lm, is_del,
431        dst_adj ? dst_adj->heap_handle : ~0,
432        nh_adj_index,
433        next_hop_weight,
434        &new_mp_adj_index))
435     {
436       vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
437       error = clib_error_return 
438         (0, "requested deleting next-hop %U not found in multi-path",
439          format_ip6_address, next_hop);
440       goto done;
441     }
442   
443   old_mp = new_mp = 0;
444   if (old_mp_adj_index != ~0)
445     old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
446   if (new_mp_adj_index != ~0)
447     new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
448
449   if (old_mp != new_mp)
450     {
451       ip6_add_del_route_args_t a;
452       a.table_index_or_table_id = fib_index;
453       a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
454                  | IP6_ROUTE_FLAG_FIB_INDEX
455                  | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
456                  | (flags & IP6_ROUTE_FLAG_NO_REDISTRIBUTE));
457       a.dst_address = dst_address[0];
458       a.dst_address_length = dst_address_length;
459       a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
460       a.add_adj = 0;
461       a.n_add_adj = 0;
462
463       ip6_add_del_route (im, &a);
464     }
465
466  done:
467   if (error)
468     clib_error_report (error);
469 }
470
471 u32
472 ip6_get_route (ip6_main_t * im,
473                u32 table_index_or_table_id,
474                u32 flags,
475                ip6_address_t * address,
476                u32 address_length)
477 {
478   ip6_fib_t * fib = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
479   ip6_address_t masked_address;
480   BVT(clib_bihash_kv) kv, value;
481
482   ASSERT (address_length < ARRAY_LEN (im->fib_masks));
483   memcpy (&masked_address, address, sizeof (masked_address));
484   ip6_address_mask (&masked_address, &im->fib_masks[address_length]);
485
486   kv.key[0] = masked_address.as_u64[0];
487   kv.key[1] = masked_address.as_u64[1];
488   kv.key[2] = ((u64)((fib - im->fibs))<<32) | address_length;
489
490   if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
491     return (value.value);
492   return 0;
493 }
494
495 void
496 ip6_foreach_matching_route (ip6_main_t * im,
497                             u32 table_index_or_table_id,
498                             u32 flags,
499                             ip6_address_t * dst_address,
500                             u32 address_length,
501                             ip6_address_t ** results,
502                             u8 ** result_lengths)
503 {
504   ip6_fib_t * fib = 
505     find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
506   BVT(clib_bihash) * h = &im->ip6_lookup_table;
507   BVT(clib_bihash_value) * v;
508   clib_bihash_bucket_t * b;
509   int i, j, k;
510   
511   if (*results)
512     _vec_len (*results) = 0;
513   if (*result_lengths)
514     _vec_len (*result_lengths) = 0;
515
516   /* Walk the table looking for routes which match the supplied address */
517   for (i = 0; i < h->nbuckets; i++)
518     {
519       b = &h->buckets [i];
520       if (b->offset == 0)
521           continue;
522
523       v = BV(clib_bihash_get_value) (h, b->offset);
524       for (j = 0; j < (1<<b->log2_pages); j++)
525         {
526           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
527             {
528               if (BV(clib_bihash_is_free)(&v->kvp[k]))
529                 continue;
530               
531               if ((v->kvp[k].key[2] 
532                    == (((u64)((fib - im->fibs))<<32) | address_length))
533                   && ip6_destination_matches_route 
534                   (im, dst_address, (ip6_address_t *) &v->kvp[k], 
535                    address_length))
536                 {
537                   ip6_address_t * a;
538
539                   a = (ip6_address_t *)(&v->kvp[k]);
540
541                   vec_add1 (*results, a[0]);
542                   vec_add1 (*result_lengths, address_length);
543                 }
544             }
545           v++;
546         }
547     }
548 }
549
550 void ip6_maybe_remap_adjacencies (ip6_main_t * im,
551                                   u32 table_index_or_table_id,
552                                   u32 flags)
553 {
554 #if SOONE
555   ip6_fib_t * fib 
556     = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
557 #endif
558   ip_lookup_main_t * lm = &im->lookup_main;
559
560   if (lm->n_adjacency_remaps == 0)
561     return;
562
563   clib_warning ("unimplemented, please report to vpp-dev@cisco.com");
564
565   /* All remaps have been performed. */
566   lm->n_adjacency_remaps = 0;
567 }
568
569 void ip6_delete_matching_routes (ip6_main_t * im,
570                                  u32 table_index_or_table_id,
571                                  u32 flags,
572                                  ip6_address_t * address,
573                                  u32 address_length)
574 {
575   /* $$$$ static may be OK - this should happen only on thread 0 */
576   static ip6_address_t * matching_addresses;
577   static u8 * matching_address_lengths;
578   u32 l, i;
579   ip6_add_del_route_args_t a;
580
581   vlib_smp_unsafe_warning();
582
583   a.flags = IP6_ROUTE_FLAG_DEL | IP6_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
584   a.table_index_or_table_id = table_index_or_table_id;
585   a.adj_index = ~0;
586   a.add_adj = 0;
587   a.n_add_adj = 0;
588
589   for (l = address_length + 1; l <= 128; l++)
590     {
591       ip6_foreach_matching_route (im, table_index_or_table_id, flags,
592                                   address,
593                                   l,
594                                   &matching_addresses,
595                                   &matching_address_lengths);
596       for (i = 0; i < vec_len (matching_addresses); i++)
597         {
598           a.dst_address = matching_addresses[i];
599           a.dst_address_length = matching_address_lengths[i];
600           ip6_add_del_route (im, &a);
601         }
602     }
603
604   ip6_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
605 }
606
607 static uword
608 ip6_lookup (vlib_main_t * vm,
609             vlib_node_runtime_t * node,
610             vlib_frame_t * frame)
611 {
612   ip6_main_t * im = &ip6_main;
613   ip_lookup_main_t * lm = &im->lookup_main;
614   vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
615   u32 n_left_from, n_left_to_next, * from, * to_next;
616   ip_lookup_next_t next;
617   u32 cpu_index = os_get_cpu_number();
618
619   from = vlib_frame_vector_args (frame);
620   n_left_from = frame->n_vectors;
621   next = node->cached_next_index;
622
623   while (n_left_from > 0)
624     {
625       vlib_get_next_frame (vm, node, next,
626                            to_next, n_left_to_next);
627
628       while (n_left_from >= 4 && n_left_to_next >= 2)
629         {
630           vlib_buffer_t * p0, * p1;
631           u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
632           ip_lookup_next_t next0, next1;
633           ip6_header_t * ip0, * ip1;
634           ip_adjacency_t * adj0, * adj1;
635           u32 fib_index0, fib_index1;
636           u32 flow_hash_config0, flow_hash_config1;
637
638           /* Prefetch next iteration. */
639           {
640             vlib_buffer_t * p2, * p3;
641
642             p2 = vlib_get_buffer (vm, from[2]);
643             p3 = vlib_get_buffer (vm, from[3]);
644
645             vlib_prefetch_buffer_header (p2, LOAD);
646             vlib_prefetch_buffer_header (p3, LOAD);
647             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
648             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
649           }
650
651           pi0 = to_next[0] = from[0];
652           pi1 = to_next[1] = from[1];
653
654           p0 = vlib_get_buffer (vm, pi0);
655           p1 = vlib_get_buffer (vm, pi1);
656
657           ip0 = vlib_buffer_get_current (p0);
658           ip1 = vlib_buffer_get_current (p1);
659
660           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
661           fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
662
663           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
664             fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
665           fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
666             fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
667
668           adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, 
669                                                   &ip0->dst_address);
670           adj_index1 = ip6_fib_lookup_with_table (im, fib_index1, 
671                                                   &ip1->dst_address);
672
673           adj0 = ip_get_adjacency (lm, adj_index0);
674           adj1 = ip_get_adjacency (lm, adj_index1);
675
676           if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
677             {
678               adj_index0 = ip6_fib_lookup_with_table 
679                 (im, adj0->explicit_fib_index, &ip0->dst_address);
680               adj0 = ip_get_adjacency (lm, adj_index0);
681             }
682           if (PREDICT_FALSE (adj1->explicit_fib_index != ~0))
683             {
684               adj_index1 = ip6_fib_lookup_with_table 
685                 (im, adj1->explicit_fib_index, &ip1->dst_address);
686               adj1 = ip_get_adjacency (lm, adj_index1);
687             }
688
689           next0 = adj0->lookup_next_index;
690           next1 = adj1->lookup_next_index;
691
692           /* Process hop-by-hop options if present */
693           next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
694               IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
695           next1 = (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
696               IP_LOOKUP_NEXT_HOP_BY_HOP : next1;
697
698           vnet_buffer (p0)->ip.flow_hash = 
699             vnet_buffer(p1)->ip.flow_hash = 0;
700
701           if (PREDICT_FALSE(adj0->n_adj > 1))
702             {
703               flow_hash_config0 = 
704                 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
705               vnet_buffer (p0)->ip.flow_hash = 
706                 ip6_compute_flow_hash (ip0, flow_hash_config0);
707             }
708
709           if (PREDICT_FALSE(adj1->n_adj > 1))
710             {
711               flow_hash_config1 = 
712                 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
713
714               vnet_buffer (p1)->ip.flow_hash = 
715                 ip6_compute_flow_hash (ip1, flow_hash_config1);
716             }
717
718           ASSERT (adj0->n_adj > 0);
719           ASSERT (adj1->n_adj > 0);
720           ASSERT (is_pow2 (adj0->n_adj));
721           ASSERT (is_pow2 (adj1->n_adj));
722           adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
723           adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
724
725           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
726           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
727
728           vlib_increment_combined_counter 
729               (cm, cpu_index, adj_index0, 1,
730                vlib_buffer_length_in_chain (vm, p0));
731           vlib_increment_combined_counter 
732               (cm, cpu_index, adj_index1, 1,
733                vlib_buffer_length_in_chain (vm, p1));
734
735           from += 2;
736           to_next += 2;
737           n_left_to_next -= 2;
738           n_left_from -= 2;
739
740           wrong_next = (next0 != next) + 2*(next1 != next);
741           if (PREDICT_FALSE (wrong_next != 0))
742             {
743               switch (wrong_next)
744                 {
745                 case 1:
746                   /* A B A */
747                   to_next[-2] = pi1;
748                   to_next -= 1;
749                   n_left_to_next += 1;
750                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
751                   break;
752
753                 case 2:
754                   /* A A B */
755                   to_next -= 1;
756                   n_left_to_next += 1;
757                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
758                   break;
759
760                 case 3:
761                   /* A B C */
762                   to_next -= 2;
763                   n_left_to_next += 2;
764                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
765                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
766                   if (next0 == next1)
767                     {
768                       /* A B B */
769                       vlib_put_next_frame (vm, node, next, n_left_to_next);
770                       next = next1;
771                       vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
772                     }
773                 }
774             }
775         }
776     
777       while (n_left_from > 0 && n_left_to_next > 0)
778         {
779           vlib_buffer_t * p0;
780           ip6_header_t * ip0;
781           u32 pi0, adj_index0;
782           ip_lookup_next_t next0;
783           ip_adjacency_t * adj0;
784           u32 fib_index0, flow_hash_config0;
785
786           pi0 = from[0];
787           to_next[0] = pi0;
788
789           p0 = vlib_get_buffer (vm, pi0);
790
791           ip0 = vlib_buffer_get_current (p0);
792
793           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
794           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
795             fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
796
797           flow_hash_config0 = 
798               vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
799
800           adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, 
801                                                   &ip0->dst_address);
802
803           adj0 = ip_get_adjacency (lm, adj_index0);
804
805           if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
806             {
807               adj_index0 = ip6_fib_lookup_with_table 
808                 (im, adj0->explicit_fib_index, &ip0->dst_address);
809               adj0 = ip_get_adjacency (lm, adj_index0);
810             }
811
812           next0 = adj0->lookup_next_index;
813           next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
814               IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
815
816           vnet_buffer (p0)->ip.flow_hash = 0;
817
818           if (PREDICT_FALSE(adj0->n_adj > 1))
819             {
820               flow_hash_config0 = 
821                 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
822               vnet_buffer (p0)->ip.flow_hash = 
823                 ip6_compute_flow_hash (ip0, flow_hash_config0);
824             }
825
826           ASSERT (adj0->n_adj > 0);
827           ASSERT (is_pow2 (adj0->n_adj));
828           adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
829
830           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
831
832           vlib_increment_combined_counter 
833               (cm, cpu_index, adj_index0, 1,
834                vlib_buffer_length_in_chain (vm, p0));
835
836           from += 1;
837           to_next += 1;
838           n_left_to_next -= 1;
839           n_left_from -= 1;
840
841           if (PREDICT_FALSE (next0 != next))
842             {
843               n_left_to_next += 1;
844               vlib_put_next_frame (vm, node, next, n_left_to_next);
845               next = next0;
846               vlib_get_next_frame (vm, node, next,
847                                    to_next, n_left_to_next);
848               to_next[0] = pi0;
849               to_next += 1;
850               n_left_to_next -= 1;
851             }
852         }
853
854       vlib_put_next_frame (vm, node, next, n_left_to_next);
855     }
856
857   return frame->n_vectors;
858 }
859
860 void ip6_adjacency_set_interface_route (vnet_main_t * vnm,
861                                         ip_adjacency_t * adj,
862                                         u32 sw_if_index,
863                                         u32 if_address_index)
864 {
865   vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
866   ip_lookup_next_t n;
867   u32 node_index;
868
869   if (hw->hw_class_index == ethernet_hw_interface_class.index
870       || hw->hw_class_index == srp_hw_interface_class.index)
871     {
872       n = IP_LOOKUP_NEXT_ARP;
873       node_index = ip6_discover_neighbor_node.index;
874       adj->if_address_index = if_address_index;
875   }
876   else
877     {
878       n = IP_LOOKUP_NEXT_REWRITE;
879       node_index = ip6_rewrite_node.index;
880     }
881
882  adj->lookup_next_index = n;
883  adj->explicit_fib_index = ~0;
884
885  vnet_rewrite_for_sw_interface
886    (vnm,
887     VNET_L3_PACKET_TYPE_IP6,
888     sw_if_index,
889     node_index,
890     VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
891     &adj->rewrite_header,
892     sizeof (adj->rewrite_data));
893 }
894
895 static void
896 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
897                           ip6_main_t * im, u32 fib_index,
898                           ip_interface_address_t * a)
899 {
900   ip_lookup_main_t * lm = &im->lookup_main;
901   ip_adjacency_t * adj;
902   ip6_address_t * address = ip_interface_address_get_address (lm, a);
903   ip6_add_del_route_args_t x;
904   vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
905   u32 classify_table_index;
906
907   /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
908   x.table_index_or_table_id = fib_index;
909   x.flags = (IP6_ROUTE_FLAG_ADD
910              | IP6_ROUTE_FLAG_FIB_INDEX
911              | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
912   x.dst_address = address[0];
913   x.dst_address_length = a->address_length;
914   x.n_add_adj = 0;
915   x.add_adj = 0;
916
917   a->neighbor_probe_adj_index = ~0;
918   if (a->address_length < 128)
919     {
920       adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
921                               &x.adj_index);
922       ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
923       ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
924       ip6_add_del_route (im, &x);
925       a->neighbor_probe_adj_index = x.adj_index;
926     }
927
928   /* Add e.g. ::1/128 as local to this host. */
929   adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
930                           &x.adj_index);
931
932   classify_table_index = ~0;
933   if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
934     classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
935   if (classify_table_index != (u32) ~0)
936     {
937       adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
938       adj->classify.table_index = classify_table_index;
939     }
940   else
941     adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
942   
943   adj->if_address_index = a - lm->if_address_pool;
944   adj->rewrite_header.sw_if_index = sw_if_index;
945   adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
946   adj->rewrite_header.data_bytes = 0;
947   ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
948   x.dst_address_length = 128;
949   ip6_add_del_route (im, &x);
950 }
951
952 static void
953 ip6_del_interface_routes (ip6_main_t * im, u32 fib_index,
954                           ip6_address_t * address, u32 address_length)
955 {
956   ip6_add_del_route_args_t x;
957
958   /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
959   x.table_index_or_table_id = fib_index;
960   x.flags = (IP6_ROUTE_FLAG_DEL
961              | IP6_ROUTE_FLAG_FIB_INDEX
962              | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
963   x.dst_address = address[0];
964   x.dst_address_length = address_length;
965   x.adj_index = ~0;
966   x.n_add_adj = 0;
967   x.add_adj = 0;
968
969   if (address_length < 128)
970     {
971       /* Don't wipe out fe80::0/64 */
972       if (address_length != 64 || 
973           address[0].as_u64[0] != clib_net_to_host_u64(0xfe80000000000000ULL))
974         ip6_add_del_route (im, &x);
975     }
976
977   x.dst_address_length = 128;
978   ip6_add_del_route (im, &x);
979
980   ip6_delete_matching_routes (im,
981                               fib_index,
982                               IP6_ROUTE_FLAG_FIB_INDEX,
983                               address,
984                               address_length);
985 }
986
987 typedef struct {
988     u32 sw_if_index;
989     ip6_address_t address;
990     u32 length;
991 } ip6_interface_address_t;
992
993 static clib_error_t *
994 ip6_add_del_interface_address_internal (vlib_main_t * vm,
995                                         u32 sw_if_index,
996                                         ip6_address_t * new_address,
997                                         u32 new_length,
998                                         u32 redistribute,
999                                         u32 insert_routes,
1000                                         u32 is_del);
1001
1002 static clib_error_t *
1003 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1004                                         u32 sw_if_index,
1005                                         ip6_address_t * address,
1006                                         u32 address_length,
1007                                         u32 redistribute,
1008                                         u32 insert_routes,
1009                                         u32 is_del)
1010 {
1011   vnet_main_t * vnm = vnet_get_main();
1012   ip6_main_t * im = &ip6_main;
1013   ip_lookup_main_t * lm = &im->lookup_main;
1014   clib_error_t * error;
1015   u32 if_address_index;
1016   ip6_address_fib_t ip6_af, * addr_fib = 0;
1017
1018   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1019   ip6_addr_fib_init (&ip6_af, address,
1020                      vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1021   vec_add1 (addr_fib, ip6_af);
1022
1023   {
1024     uword elts_before = pool_elts (lm->if_address_pool);
1025
1026     error = ip_interface_address_add_del
1027       (lm,
1028        sw_if_index,
1029        addr_fib,
1030        address_length,
1031        is_del,
1032        &if_address_index);
1033     if (error)
1034       goto done;
1035
1036     /* Pool did not grow: add duplicate address. */
1037     if (elts_before == pool_elts (lm->if_address_pool))
1038       goto done;
1039   }
1040
1041   if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1042     {
1043       if (is_del)
1044         ip6_del_interface_routes (im, ip6_af.fib_index, address,
1045                                   address_length);
1046
1047       else
1048         ip6_add_interface_routes (vnm, sw_if_index,
1049                                   im, ip6_af.fib_index,
1050                                   pool_elt_at_index (lm->if_address_pool, if_address_index));
1051     }
1052
1053   {
1054     ip6_add_del_interface_address_callback_t * cb;
1055     vec_foreach (cb, im->add_del_interface_address_callbacks)
1056       cb->function (im, cb->function_opaque, sw_if_index,
1057                     address, address_length,
1058                     if_address_index,
1059                     is_del);
1060   }
1061
1062  done:
1063   vec_free (addr_fib);
1064   return error;
1065 }
1066
1067 clib_error_t *
1068 ip6_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1069                                ip6_address_t * address, u32 address_length,
1070                                u32 is_del)
1071 {
1072   return ip6_add_del_interface_address_internal
1073     (vm, sw_if_index, address, address_length,
1074      /* redistribute */ 1,
1075      /* insert_routes */ 1,
1076      is_del);
1077 }
1078
1079 clib_error_t *
1080 ip6_sw_interface_admin_up_down (vnet_main_t * vnm,
1081                                 u32 sw_if_index,
1082                                 u32 flags)
1083 {
1084   ip6_main_t * im = &ip6_main;
1085   ip_interface_address_t * ia;
1086   ip6_address_t * a;
1087   u32 is_admin_up, fib_index;
1088
1089   /* Fill in lookup tables with default table (0). */
1090   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1091
1092   vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1093
1094   is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1095
1096   fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1097
1098   foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index, 
1099                                 0 /* honor unnumbered */,
1100   ({
1101     a = ip_interface_address_get_address (&im->lookup_main, ia);
1102     if (is_admin_up)
1103       ip6_add_interface_routes (vnm, sw_if_index,
1104                                 im, fib_index,
1105                                 ia);
1106     else
1107       ip6_del_interface_routes (im, fib_index,
1108                                 a, ia->address_length);
1109   }));
1110
1111   return 0;
1112 }
1113
1114 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
1115
1116 clib_error_t *
1117 ip6_sw_interface_add_del (vnet_main_t * vnm,
1118                           u32 sw_if_index,
1119                           u32 is_add)
1120 {
1121   vlib_main_t * vm = vnm->vlib_main;
1122   ip6_main_t * im = &ip6_main;
1123   ip_lookup_main_t * lm = &im->lookup_main;
1124   u32 ci, cast;
1125
1126   for (cast = 0; cast < VNET_N_CAST; cast++)
1127     {
1128       ip_config_main_t * cm = &lm->rx_config_mains[cast];
1129       vnet_config_main_t * vcm = &cm->config_main;
1130
1131       /* FIXME multicast. */
1132       if (! vcm->node_index_by_feature_index)
1133         {
1134           char * start_nodes[] = { "ip6-input", };
1135           char * feature_nodes[] = {
1136             [IP6_RX_FEATURE_CHECK_ACCESS] = "ip6-inacl",
1137             [IP6_RX_FEATURE_IPSEC] = "ipsec-input-ip6",
1138             [IP6_RX_FEATURE_L2TPV3] = "l2tp-decap",
1139             [IP6_RX_FEATURE_VPATH]  = "vpath-input-ip6",
1140             [IP6_RX_FEATURE_LOOKUP] = "ip6-lookup",
1141           };
1142           vnet_config_init (vm, vcm,
1143                             start_nodes, ARRAY_LEN (start_nodes),
1144                             feature_nodes, ARRAY_LEN (feature_nodes));
1145         }
1146
1147       vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1148       ci = cm->config_index_by_sw_if_index[sw_if_index];
1149
1150       if (is_add)
1151         ci = vnet_config_add_feature (vm, vcm,
1152                                       ci,
1153                                       IP6_RX_FEATURE_LOOKUP,
1154                                       /* config data */ 0,
1155                                       /* # bytes of config data */ 0);
1156       else
1157         ci = vnet_config_del_feature (vm, vcm,
1158                                       ci,
1159                                       IP6_RX_FEATURE_LOOKUP,
1160                                       /* config data */ 0,
1161                                       /* # bytes of config data */ 0);
1162
1163       cm->config_index_by_sw_if_index[sw_if_index] = ci;
1164     }
1165   return /* no error */ 0;
1166 }
1167
1168 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
1169
1170 VLIB_REGISTER_NODE (ip6_lookup_node) = {
1171   .function = ip6_lookup,
1172   .name = "ip6-lookup",
1173   .vector_size = sizeof (u32),
1174
1175   .n_next_nodes = IP_LOOKUP_N_NEXT,
1176   .next_nodes = {
1177     [IP_LOOKUP_NEXT_MISS] = "ip6-miss",
1178     [IP_LOOKUP_NEXT_DROP] = "ip6-drop",
1179     [IP_LOOKUP_NEXT_PUNT] = "ip6-punt",
1180     [IP_LOOKUP_NEXT_LOCAL] = "ip6-local",
1181     [IP_LOOKUP_NEXT_ARP] = "ip6-discover-neighbor",
1182     [IP_LOOKUP_NEXT_REWRITE] = "ip6-rewrite",
1183     [IP_LOOKUP_NEXT_CLASSIFY] = "ip6-classify",
1184     [IP_LOOKUP_NEXT_MAP] = "ip6-map",
1185     [IP_LOOKUP_NEXT_MAP_T] = "ip6-map-t",
1186     [IP_LOOKUP_NEXT_SIXRD] = "ip6-sixrd",
1187     [IP_LOOKUP_NEXT_HOP_BY_HOP] = "ip6-hop-by-hop",
1188     [IP_LOOKUP_NEXT_ADD_HOP_BY_HOP] = "ip6-add-hop-by-hop", 
1189     [IP_LOOKUP_NEXT_POP_HOP_BY_HOP] = "ip6-pop-hop-by-hop", 
1190   },
1191 };
1192
1193 typedef struct {
1194   /* Adjacency taken. */
1195   u32 adj_index;
1196   u32 flow_hash;
1197   u32 fib_index;
1198
1199   /* Packet data, possibly *after* rewrite. */
1200   u8 packet_data[64 - 1*sizeof(u32)];
1201 } ip6_forward_next_trace_t;
1202
1203 static u8 * format_ip6_forward_next_trace (u8 * s, va_list * args)
1204 {
1205   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1206   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1207   ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1208   vnet_main_t * vnm = vnet_get_main();
1209   ip6_main_t * im = &ip6_main;
1210   ip_adjacency_t * adj;
1211   uword indent = format_get_indent (s);
1212
1213   adj = ip_get_adjacency (&im->lookup_main, t->adj_index);
1214   s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1215               t->fib_index, t->adj_index, format_ip_adjacency,
1216               vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1217   switch (adj->lookup_next_index)
1218     {
1219     case IP_LOOKUP_NEXT_REWRITE:
1220       s = format (s, "\n%U%U",
1221                   format_white_space, indent,
1222                   format_ip_adjacency_packet_data,
1223                   vnm, &im->lookup_main, t->adj_index,
1224                   t->packet_data, sizeof (t->packet_data));
1225       break;
1226
1227     default:
1228       break;
1229     }
1230
1231   return s;
1232 }
1233
1234 /* Common trace function for all ip6-forward next nodes. */
1235 void
1236 ip6_forward_next_trace (vlib_main_t * vm,
1237                         vlib_node_runtime_t * node,
1238                         vlib_frame_t * frame,
1239                         vlib_rx_or_tx_t which_adj_index)
1240 {
1241   u32 * from, n_left;
1242   ip6_main_t * im = &ip6_main;
1243
1244   n_left = frame->n_vectors;
1245   from = vlib_frame_vector_args (frame);
1246   
1247   while (n_left >= 4)
1248     {
1249       u32 bi0, bi1;
1250       vlib_buffer_t * b0, * b1;
1251       ip6_forward_next_trace_t * t0, * t1;
1252
1253       /* Prefetch next iteration. */
1254       vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1255       vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1256
1257       bi0 = from[0];
1258       bi1 = from[1];
1259
1260       b0 = vlib_get_buffer (vm, bi0);
1261       b1 = vlib_get_buffer (vm, bi1);
1262
1263       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1264         {
1265           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1266           t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1267           t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1268           t0->fib_index = vec_elt (im->fib_index_by_sw_if_index, 
1269                              vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1270           memcpy (t0->packet_data,
1271                   vlib_buffer_get_current (b0),
1272                   sizeof (t0->packet_data));
1273         }
1274       if (b1->flags & VLIB_BUFFER_IS_TRACED)
1275         {
1276           t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1277           t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1278           t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1279           t1->fib_index = vec_elt (im->fib_index_by_sw_if_index, 
1280                              vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1281           memcpy (t1->packet_data,
1282                   vlib_buffer_get_current (b1),
1283                   sizeof (t1->packet_data));
1284         }
1285       from += 2;
1286       n_left -= 2;
1287     }
1288
1289   while (n_left >= 1)
1290     {
1291       u32 bi0;
1292       vlib_buffer_t * b0;
1293       ip6_forward_next_trace_t * t0;
1294
1295       bi0 = from[0];
1296
1297       b0 = vlib_get_buffer (vm, bi0);
1298
1299       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1300         {
1301           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1302           t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1303           t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1304           t0->fib_index = vec_elt (im->fib_index_by_sw_if_index, 
1305                              vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1306           memcpy (t0->packet_data,
1307                   vlib_buffer_get_current (b0),
1308                   sizeof (t0->packet_data));
1309         }
1310       from += 1;
1311       n_left -= 1;
1312     }
1313 }
1314
1315 static uword
1316 ip6_drop_or_punt (vlib_main_t * vm,
1317                   vlib_node_runtime_t * node,
1318                   vlib_frame_t * frame,
1319                   ip6_error_t error_code)
1320 {
1321   u32 * buffers = vlib_frame_vector_args (frame);
1322   uword n_packets = frame->n_vectors;
1323
1324   vlib_error_drop_buffers (vm, node,
1325                            buffers,
1326                            /* stride */ 1,
1327                            n_packets,
1328                            /* next */ 0,
1329                            ip6_input_node.index,
1330                            error_code);
1331
1332   if (node->flags & VLIB_NODE_FLAG_TRACE)
1333     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1334
1335   return n_packets;
1336 }
1337
1338 static uword
1339 ip6_drop (vlib_main_t * vm,
1340           vlib_node_runtime_t * node,
1341           vlib_frame_t * frame)
1342 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1343
1344 static uword
1345 ip6_punt (vlib_main_t * vm,
1346           vlib_node_runtime_t * node,
1347           vlib_frame_t * frame)
1348 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1349
1350 static uword
1351 ip6_miss (vlib_main_t * vm,
1352           vlib_node_runtime_t * node,
1353           vlib_frame_t * frame)
1354 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1355
1356 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1357   .function = ip6_drop,
1358   .name = "ip6-drop",
1359   .vector_size = sizeof (u32),
1360
1361   .format_trace = format_ip6_forward_next_trace,
1362
1363   .n_next_nodes = 1,
1364   .next_nodes = {
1365     [0] = "error-drop",
1366   },
1367 };
1368
1369 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1370   .function = ip6_punt,
1371   .name = "ip6-punt",
1372   .vector_size = sizeof (u32),
1373
1374   .format_trace = format_ip6_forward_next_trace,
1375
1376   .n_next_nodes = 1,
1377   .next_nodes = {
1378     [0] = "error-punt",
1379   },
1380 };
1381
1382 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1383   .function = ip6_miss,
1384   .name = "ip6-miss",
1385   .vector_size = sizeof (u32),
1386
1387   .format_trace = format_ip6_forward_next_trace,
1388
1389   .n_next_nodes = 1,
1390   .next_nodes = {
1391     [0] = "error-drop",
1392   },
1393 };
1394
1395 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1396   .function = ip6_drop,
1397   .name = "ip6-multicast",
1398   .vector_size = sizeof (u32),
1399
1400   .format_trace = format_ip6_forward_next_trace,
1401
1402   .n_next_nodes = 1,
1403   .next_nodes = {
1404     [0] = "error-drop",
1405   },
1406 };
1407
1408 /* Compute TCP/UDP/ICMP6 checksum in software. */
1409 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1410 {
1411   ip_csum_t sum0;
1412   u16 sum16, payload_length_host_byte_order;
1413   u32 i, n_this_buffer, n_bytes_left;
1414   u32 headers_size = sizeof(ip0[0]);
1415   void * data_this_buffer;
1416
1417   ASSERT(bogus_lengthp);
1418   *bogus_lengthp = 0;
1419
1420   /* Initialize checksum with ip header. */
1421   sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1422   payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1423   data_this_buffer = (void *) (ip0 + 1);
1424  
1425   for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1426     {
1427       sum0 = ip_csum_with_carry (sum0,
1428                                  clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1429       sum0 = ip_csum_with_carry (sum0,
1430                                  clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1431     }
1432
1433   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1434   if (PREDICT_FALSE (ip0->protocol ==  IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1435     {
1436       u32  skip_bytes;
1437       ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t  *)data_this_buffer;
1438
1439       /* validate really icmp6 next */
1440       ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1441
1442       skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1443       data_this_buffer  = (void *)((u8 *)data_this_buffer + skip_bytes);
1444  
1445       payload_length_host_byte_order  -= skip_bytes;
1446       headers_size += skip_bytes;
1447    }
1448
1449   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1450 #if DPDK > 0
1451   if (p0) 
1452   {
1453     struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer(p0);
1454     u8 nb_segs = mb->nb_segs;
1455
1456     n_this_buffer = (p0->current_length > headers_size ?
1457                      p0->current_length - headers_size : 0);
1458     while (n_bytes_left)
1459       {
1460         sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1461         n_bytes_left -= n_this_buffer;
1462
1463         mb = mb->next;
1464         nb_segs--;
1465         if ((nb_segs == 0) || (mb == 0))
1466           break;
1467
1468         data_this_buffer = rte_ctrlmbuf_data(mb);
1469         n_this_buffer = mb->data_len;
1470       }
1471     if (n_bytes_left || nb_segs)
1472       {
1473         *bogus_lengthp = 1;
1474         return 0xfefe;
1475       }
1476   } 
1477   else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1478 #else
1479   if (p0 && n_this_buffer + headers_size  > p0->current_length)
1480     n_this_buffer = p0->current_length > headers_size  ? p0->current_length - headers_size  : 0;
1481   while (1)
1482     {
1483       sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1484       n_bytes_left -= n_this_buffer;
1485       if (n_bytes_left == 0)
1486         break;
1487
1488       if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1489         {
1490           *bogus_lengthp = 1;
1491           return 0xfefe;
1492         }
1493       p0 = vlib_get_buffer (vm, p0->next_buffer);
1494       data_this_buffer = vlib_buffer_get_current (p0);
1495       n_this_buffer = p0->current_length;
1496     }
1497 #endif /* DPDK */
1498
1499   sum16 = ~ ip_csum_fold (sum0);
1500
1501   return sum16;
1502 }
1503
1504 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1505 {
1506   ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1507   udp_header_t * udp0;
1508   u16 sum16;
1509   int bogus_length;
1510
1511   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1512   ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1513           || ip0->protocol == IP_PROTOCOL_ICMP6
1514           || ip0->protocol == IP_PROTOCOL_UDP
1515           || ip0->protocol ==  IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1516
1517   udp0 = (void *) (ip0 + 1);
1518   if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1519     {
1520       p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1521                     | IP_BUFFER_L4_CHECKSUM_CORRECT);
1522       return p0->flags;
1523     }
1524
1525   sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1526
1527   p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1528                 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1529
1530   return p0->flags;
1531 }
1532
1533 static uword
1534 ip6_local (vlib_main_t * vm,
1535            vlib_node_runtime_t * node,
1536            vlib_frame_t * frame)
1537 {
1538   ip6_main_t * im = &ip6_main;
1539   ip_lookup_main_t * lm = &im->lookup_main;
1540   ip_local_next_t next_index;
1541   u32 * from, * to_next, n_left_from, n_left_to_next;
1542   vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1543
1544   from = vlib_frame_vector_args (frame);
1545   n_left_from = frame->n_vectors;
1546   next_index = node->cached_next_index;
1547   
1548   if (node->flags & VLIB_NODE_FLAG_TRACE)
1549     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1550
1551   while (n_left_from > 0)
1552     {
1553       vlib_get_next_frame (vm, node, next_index,
1554                            to_next, n_left_to_next);
1555
1556       while (n_left_from >= 4 && n_left_to_next >= 2)
1557         {
1558           vlib_buffer_t * p0, * p1;
1559           ip6_header_t * ip0, * ip1;
1560           udp_header_t * udp0, * udp1;
1561           u32 pi0, ip_len0, udp_len0, flags0, next0;
1562           u32 pi1, ip_len1, udp_len1, flags1, next1;
1563           i32 len_diff0, len_diff1;
1564           u8 error0, type0, good_l4_checksum0;
1565           u8 error1, type1, good_l4_checksum1;
1566       
1567           pi0 = to_next[0] = from[0];
1568           pi1 = to_next[1] = from[1];
1569           from += 2;
1570           n_left_from -= 2;
1571           to_next += 2;
1572           n_left_to_next -= 2;
1573       
1574           p0 = vlib_get_buffer (vm, pi0);
1575           p1 = vlib_get_buffer (vm, pi1);
1576
1577           ip0 = vlib_buffer_get_current (p0);
1578           ip1 = vlib_buffer_get_current (p1);
1579
1580           type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1581           type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1582
1583           next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1584           next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1585
1586           flags0 = p0->flags;
1587           flags1 = p1->flags;
1588
1589           good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1590           good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1591
1592           udp0 = ip6_next_header (ip0);
1593           udp1 = ip6_next_header (ip1);
1594
1595           /* Don't verify UDP checksum for packets with explicit zero checksum. */
1596           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1597           good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1598
1599           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1600           good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1601
1602           /* Verify UDP length. */
1603           ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1604           ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1605           udp_len0 = clib_net_to_host_u16 (udp0->length);
1606           udp_len1 = clib_net_to_host_u16 (udp1->length);
1607
1608           len_diff0 = ip_len0 - udp_len0;
1609           len_diff1 = ip_len1 - udp_len1;
1610
1611           len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1612           len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1613
1614           if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1615                              && ! good_l4_checksum0
1616                              && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1617             {
1618               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1619               good_l4_checksum0 =
1620                 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1621             }
1622           if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1623                              && ! good_l4_checksum1
1624                              && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1625             {
1626               flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1627               good_l4_checksum1 =
1628                 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1629             }
1630
1631           error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1632
1633           error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1634           error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1635
1636           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1637           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1638           error0 = (! good_l4_checksum0
1639                     ? IP6_ERROR_UDP_CHECKSUM + type0
1640                     : error0);
1641           error1 = (! good_l4_checksum1
1642                     ? IP6_ERROR_UDP_CHECKSUM + type1
1643                     : error1);
1644
1645           /* Drop packets from unroutable hosts. */
1646           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1647           if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1648             {
1649               u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1650               error0 = (lm->miss_adj_index == src_adj_index0
1651                         ? IP6_ERROR_SRC_LOOKUP_MISS
1652                         : error0);
1653             }
1654           if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1655             {
1656               u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1657               error1 = (lm->miss_adj_index == src_adj_index1
1658                         ? IP6_ERROR_SRC_LOOKUP_MISS
1659                         : error1);
1660             }
1661
1662           next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1663           next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1664
1665           p0->error = error_node->errors[error0];
1666           p1->error = error_node->errors[error1];
1667
1668           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1669                                            to_next, n_left_to_next,
1670                                            pi0, pi1, next0, next1);
1671         }
1672
1673       while (n_left_from > 0 && n_left_to_next > 0)
1674         {
1675           vlib_buffer_t * p0;
1676           ip6_header_t * ip0;
1677           udp_header_t * udp0;
1678           u32 pi0, ip_len0, udp_len0, flags0, next0;
1679           i32 len_diff0;
1680           u8 error0, type0, good_l4_checksum0;
1681       
1682           pi0 = to_next[0] = from[0];
1683           from += 1;
1684           n_left_from -= 1;
1685           to_next += 1;
1686           n_left_to_next -= 1;
1687       
1688           p0 = vlib_get_buffer (vm, pi0);
1689
1690           ip0 = vlib_buffer_get_current (p0);
1691
1692           type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1693           next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1694
1695           flags0 = p0->flags;
1696
1697           good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1698
1699           udp0 = ip6_next_header (ip0);
1700
1701           /* Don't verify UDP checksum for packets with explicit zero checksum. */
1702           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1703
1704           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1705
1706           /* Verify UDP length. */
1707           ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1708           udp_len0 = clib_net_to_host_u16 (udp0->length);
1709
1710           len_diff0 = ip_len0 - udp_len0;
1711
1712           len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1713
1714           if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1715                              && ! good_l4_checksum0
1716                              && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1717             {
1718               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1719               good_l4_checksum0 =
1720                 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1721             }
1722
1723           error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1724
1725           error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1726
1727           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1728           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1729           error0 = (! good_l4_checksum0
1730                     ? IP6_ERROR_UDP_CHECKSUM + type0
1731                     : error0);
1732
1733           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1734           if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1735             {
1736               u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1737               error0 = (lm->miss_adj_index == src_adj_index0
1738                         ? IP6_ERROR_SRC_LOOKUP_MISS
1739                         : error0);
1740             }
1741
1742           next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1743
1744           p0->error = error_node->errors[error0];
1745
1746           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1747                                            to_next, n_left_to_next,
1748                                            pi0, next0);
1749         }
1750   
1751       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1752     }
1753
1754   return frame->n_vectors;
1755 }
1756
1757 VLIB_REGISTER_NODE (ip6_local_node,static) = {
1758   .function = ip6_local,
1759   .name = "ip6-local",
1760   .vector_size = sizeof (u32),
1761
1762   .format_trace = format_ip6_forward_next_trace,
1763
1764   .n_next_nodes = IP_LOCAL_N_NEXT,
1765   .next_nodes = {
1766     [IP_LOCAL_NEXT_DROP] = "error-drop",
1767     [IP_LOCAL_NEXT_PUNT] = "error-punt",
1768     [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1769     [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1770   },
1771 };
1772
1773 void ip6_register_protocol (u32 protocol, u32 node_index)
1774 {
1775   vlib_main_t * vm = vlib_get_main();
1776   ip6_main_t * im = &ip6_main;
1777   ip_lookup_main_t * lm = &im->lookup_main;
1778
1779   ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1780   lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
1781 }
1782
1783 typedef enum {
1784   IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1785   IP6_DISCOVER_NEIGHBOR_N_NEXT,
1786 } ip6_discover_neighbor_next_t;
1787
1788 typedef enum {
1789   IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1790   IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1791 } ip6_discover_neighbor_error_t;
1792
1793 static uword
1794 ip6_discover_neighbor (vlib_main_t * vm,
1795                        vlib_node_runtime_t * node,
1796                        vlib_frame_t * frame)
1797 {
1798   vnet_main_t * vnm = vnet_get_main();
1799   ip6_main_t * im = &ip6_main;
1800   ip_lookup_main_t * lm = &im->lookup_main;
1801   u32 * from, * to_next_drop;
1802   uword n_left_from, n_left_to_next_drop;
1803   static f64 time_last_seed_change = -1e100;
1804   static u32 hash_seeds[3];
1805   static uword hash_bitmap[256 / BITS (uword)]; 
1806   f64 time_now;
1807   int bogus_length;
1808
1809   if (node->flags & VLIB_NODE_FLAG_TRACE)
1810     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1811
1812   time_now = vlib_time_now (vm);
1813   if (time_now - time_last_seed_change > 1e-3)
1814     {
1815       uword i;
1816       u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1817                                              sizeof (hash_seeds));
1818       for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1819         hash_seeds[i] = r[i];
1820
1821       /* Mark all hash keys as been not-seen before. */
1822       for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1823         hash_bitmap[i] = 0;
1824
1825       time_last_seed_change = time_now;
1826     }
1827
1828   from = vlib_frame_vector_args (frame);
1829   n_left_from = frame->n_vectors;
1830
1831   while (n_left_from > 0)
1832     {
1833       vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1834                            to_next_drop, n_left_to_next_drop);
1835
1836       while (n_left_from > 0 && n_left_to_next_drop > 0)
1837         {
1838           vlib_buffer_t * p0;
1839           ip6_header_t * ip0;
1840           u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1841           uword bm0;
1842           ip_adjacency_t * adj0;
1843           vnet_hw_interface_t * hw_if0;
1844           u32 next0;
1845
1846           pi0 = from[0];
1847
1848           p0 = vlib_get_buffer (vm, pi0);
1849
1850           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1851
1852           ip0 = vlib_buffer_get_current (p0);
1853
1854           adj0 = ip_get_adjacency (lm, adj_index0);
1855
1856           a0 = hash_seeds[0];
1857           b0 = hash_seeds[1];
1858           c0 = hash_seeds[2];
1859
1860           sw_if_index0 = adj0->rewrite_header.sw_if_index;
1861           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1862
1863           a0 ^= sw_if_index0;
1864           b0 ^= ip0->dst_address.as_u32[0];
1865           c0 ^= ip0->dst_address.as_u32[1];
1866
1867           hash_v3_mix32 (a0, b0, c0);
1868
1869           b0 ^= ip0->dst_address.as_u32[2];
1870           c0 ^= ip0->dst_address.as_u32[3];
1871
1872           hash_v3_finalize32 (a0, b0, c0);
1873
1874           c0 &= BITS (hash_bitmap) - 1;
1875           c0 = c0 / BITS (uword);
1876           m0 = (uword) 1 << (c0 % BITS (uword));
1877
1878           bm0 = hash_bitmap[c0];
1879           drop0 = (bm0 & m0) != 0;
1880
1881           /* Mark it as seen. */
1882           hash_bitmap[c0] = bm0 | m0;
1883
1884           from += 1;
1885           n_left_from -= 1;
1886           to_next_drop[0] = pi0;
1887           to_next_drop += 1;
1888           n_left_to_next_drop -= 1;
1889
1890           hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1891
1892           /* If the interface is link-down, drop the pkt */
1893           if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1894             drop0 = 1;
1895
1896           p0->error = 
1897             node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP 
1898                          : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1899           if (drop0)
1900             continue;
1901
1902           {
1903             u32 bi0 = 0;
1904             icmp6_neighbor_solicitation_header_t * h0;
1905             vlib_buffer_t * b0;
1906
1907             h0 = vlib_packet_template_get_packet 
1908               (vm, &im->discover_neighbor_packet_template, &bi0);
1909
1910             /* 
1911              * Build ethernet header.
1912              * Choose source address based on destination lookup 
1913              * adjacency. 
1914              */
1915             ip6_src_address_for_packet (im, p0, &h0->ip.src_address, 
1916                                         sw_if_index0);
1917
1918             /* 
1919              * Destination address is a solicited node multicast address.  
1920              * We need to fill in
1921              * the low 24 bits with low 24 bits of target's address. 
1922              */
1923             h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1924             h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1925             h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1926
1927             h0->neighbor.target_address = ip0->dst_address;
1928
1929             memcpy (h0->link_layer_option.ethernet_address, 
1930                     hw_if0->hw_address, vec_len (hw_if0->hw_address));
1931
1932             /* $$$$ appears we need this; why is the checksum non-zero? */
1933             h0->neighbor.icmp.checksum = 0;
1934             h0->neighbor.icmp.checksum = 
1935               ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip, 
1936                                                  &bogus_length);
1937
1938             ASSERT (bogus_length == 0);
1939
1940             vlib_buffer_copy_trace_flag (vm, p0, bi0);
1941             b0 = vlib_get_buffer (vm, bi0);
1942             vnet_buffer (b0)->sw_if_index[VLIB_TX] 
1943               = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1944
1945             /* Add rewrite/encap string. */
1946             vnet_rewrite_one_header (adj0[0], h0, 
1947                                      sizeof (ethernet_header_t));
1948             vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1949
1950             /* $$$$ hack in case next0 == 0 */
1951             b0->error = node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_DROP];
1952             next0 = 
1953               vec_elt (im->discover_neighbor_next_index_by_hw_if_index, 
1954                        hw_if0->hw_if_index);
1955
1956             vlib_set_next_frame_buffer (vm, node, next0, bi0);
1957           }
1958         }
1959
1960       vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP, 
1961                            n_left_to_next_drop);
1962     }
1963
1964   return frame->n_vectors;
1965 }
1966
1967 static char * ip6_discover_neighbor_error_strings[] = {
1968   [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1969   [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] 
1970   = "neighbor solicitations sent",
1971 };
1972
1973 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
1974   .function = ip6_discover_neighbor,
1975   .name = "ip6-discover-neighbor",
1976   .vector_size = sizeof (u32),
1977
1978   .format_trace = format_ip6_forward_next_trace,
1979
1980   .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1981   .error_strings = ip6_discover_neighbor_error_strings,
1982
1983   .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1984   .next_nodes = {
1985     [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1986   },
1987 };
1988
1989 clib_error_t *
1990 ip6_discover_neighbor_hw_interface_link_up_down (vnet_main_t * vnm,
1991                                                  u32 hw_if_index,
1992                                                  u32 flags)
1993 {
1994   vlib_main_t * vm = vnm->vlib_main;
1995   ip6_main_t * im = &ip6_main;
1996   vnet_hw_interface_t * hw_if;
1997
1998   hw_if = vnet_get_hw_interface (vnm, hw_if_index);
1999
2000   vec_validate_init_empty 
2001     (im->discover_neighbor_next_index_by_hw_if_index, hw_if_index, 0);
2002   im->discover_neighbor_next_index_by_hw_if_index[hw_if_index]
2003     = vlib_node_add_next (vm, ip6_discover_neighbor_node.index,
2004                           hw_if->output_node_index);
2005   return 0;
2006 }
2007
2008 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION 
2009 (ip6_discover_neighbor_hw_interface_link_up_down);
2010
2011 clib_error_t *
2012 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
2013 {
2014   vnet_main_t * vnm = vnet_get_main();
2015   ip6_main_t * im = &ip6_main;
2016   icmp6_neighbor_solicitation_header_t * h;
2017   ip6_address_t * src;
2018   ip_interface_address_t * ia;
2019   ip_adjacency_t * adj;
2020   vnet_hw_interface_t * hi;
2021   vnet_sw_interface_t * si;
2022   vlib_buffer_t * b;
2023   u32 bi = 0;
2024   int bogus_length;
2025
2026   si = vnet_get_sw_interface (vnm, sw_if_index);
2027
2028   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2029     {
2030       return clib_error_return (0, "%U: interface %U down",
2031                                 format_ip6_address, dst, 
2032                                 format_vnet_sw_if_index_name, vnm, 
2033                                 sw_if_index);
2034     }
2035
2036   src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2037   if (! src)
2038     {
2039       vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2040       return clib_error_return 
2041         (0, "no matching interface address for destination %U (interface %U)",
2042          format_ip6_address, dst,
2043          format_vnet_sw_if_index_name, vnm, sw_if_index);
2044     }
2045
2046   h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2047
2048   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2049
2050   /* Destination address is a solicited node multicast address.  We need to fill in
2051      the low 24 bits with low 24 bits of target's address. */
2052   h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2053   h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2054   h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2055
2056   h->ip.src_address = src[0];
2057   h->neighbor.target_address = dst[0];
2058
2059   memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2060
2061   h->neighbor.icmp.checksum = 
2062     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2063   ASSERT(bogus_length == 0);
2064
2065   b = vlib_get_buffer (vm, bi);
2066   vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2067
2068   /* Add encapsulation string for software interface (e.g. ethernet header). */
2069   adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2070   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2071   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2072
2073   {
2074     vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2075     u32 * to_next = vlib_frame_vector_args (f);
2076     to_next[0] = bi;
2077     f->n_vectors = 1;
2078     vlib_put_frame_to_node (vm, hi->output_node_index, f);
2079   }
2080
2081   return /* no error */ 0;
2082 }
2083
2084 typedef enum {
2085   IP6_REWRITE_NEXT_DROP,
2086 } ip6_rewrite_next_t;
2087
2088 always_inline uword
2089 ip6_rewrite_inline (vlib_main_t * vm,
2090                     vlib_node_runtime_t * node,
2091                     vlib_frame_t * frame,
2092                     int rewrite_for_locally_received_packets)
2093 {
2094   ip_lookup_main_t * lm = &ip6_main.lookup_main;
2095   u32 * from = vlib_frame_vector_args (frame);
2096   u32 n_left_from, n_left_to_next, * to_next, next_index;
2097   vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2098   vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2099
2100   n_left_from = frame->n_vectors;
2101   next_index = node->cached_next_index;
2102   u32 cpu_index = os_get_cpu_number();
2103   
2104   while (n_left_from > 0)
2105     {
2106       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2107
2108       while (n_left_from >= 4 && n_left_to_next >= 2)
2109         {
2110           ip_adjacency_t * adj0, * adj1;
2111           vlib_buffer_t * p0, * p1;
2112           ip6_header_t * ip0, * ip1;
2113           u32 pi0, rw_len0, next0, error0, adj_index0;
2114           u32 pi1, rw_len1, next1, error1, adj_index1;
2115       
2116           /* Prefetch next iteration. */
2117           {
2118             vlib_buffer_t * p2, * p3;
2119
2120             p2 = vlib_get_buffer (vm, from[2]);
2121             p3 = vlib_get_buffer (vm, from[3]);
2122
2123             vlib_prefetch_buffer_header (p2, LOAD);
2124             vlib_prefetch_buffer_header (p3, LOAD);
2125
2126             CLIB_PREFETCH (p2->pre_data, 32, STORE);
2127             CLIB_PREFETCH (p3->pre_data, 32, STORE);
2128
2129             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2130             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2131           }
2132
2133           pi0 = to_next[0] = from[0];
2134           pi1 = to_next[1] = from[1];
2135
2136           from += 2;
2137           n_left_from -= 2;
2138           to_next += 2;
2139           n_left_to_next -= 2;
2140       
2141           p0 = vlib_get_buffer (vm, pi0);
2142           p1 = vlib_get_buffer (vm, pi1);
2143
2144           adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2145           adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2146
2147           /* We should never rewrite a pkt using the MISS adjacency */
2148           ASSERT(adj_index0 && adj_index1);
2149
2150           ip0 = vlib_buffer_get_current (p0);
2151           ip1 = vlib_buffer_get_current (p1);
2152
2153           error0 = error1 = IP6_ERROR_NONE;
2154
2155           if (! rewrite_for_locally_received_packets)
2156             {
2157               i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2158
2159               /* Input node should have reject packets with hop limit 0. */
2160               ASSERT (ip0->hop_limit > 0);
2161               ASSERT (ip1->hop_limit > 0);
2162
2163               hop_limit0 -= 1;
2164               hop_limit1 -= 1;
2165
2166               ip0->hop_limit = hop_limit0;
2167               ip1->hop_limit = hop_limit1;
2168
2169               error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2170               error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2171             }
2172
2173           adj0 = ip_get_adjacency (lm, adj_index0);
2174           adj1 = ip_get_adjacency (lm, adj_index1);
2175
2176           if (rewrite_for_locally_received_packets)
2177             {
2178               /*
2179                * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2180                * we end up here with a local adjacency in hand
2181                */
2182               if (PREDICT_FALSE(adj0->lookup_next_index 
2183                                 == IP_LOOKUP_NEXT_LOCAL))
2184                 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2185               if (PREDICT_FALSE(adj1->lookup_next_index 
2186                                 == IP_LOOKUP_NEXT_LOCAL))
2187                 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2188             }
2189
2190           rw_len0 = adj0[0].rewrite_header.data_bytes;
2191           rw_len1 = adj1[0].rewrite_header.data_bytes;
2192
2193           vlib_increment_combined_counter (&lm->adjacency_counters,
2194                                            cpu_index, 
2195                                            adj_index0,
2196                                            /* packet increment */ 0,
2197                                            /* byte increment */ rw_len0);
2198           vlib_increment_combined_counter (&lm->adjacency_counters,
2199                                            cpu_index, 
2200                                            adj_index1,
2201                                            /* packet increment */ 0,
2202                                            /* byte increment */ rw_len1);
2203
2204           /* Check MTU of outgoing interface. */
2205           error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2206                     ? IP6_ERROR_MTU_EXCEEDED
2207                     : error0);
2208           error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2209                     ? IP6_ERROR_MTU_EXCEEDED
2210                     : error1);
2211
2212           p0->current_data -= rw_len0;
2213           p1->current_data -= rw_len1;
2214
2215           p0->current_length += rw_len0;
2216           p1->current_length += rw_len1;
2217
2218           vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2219           vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2220       
2221           next0 = (error0 == IP6_ERROR_NONE) ? 
2222             adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2223           next1 = (error1 == IP6_ERROR_NONE) ? 
2224             adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2225
2226           /* Guess we are only writing on simple Ethernet header. */
2227           vnet_rewrite_two_headers (adj0[0], adj1[0],
2228                                     ip0, ip1,
2229                                     sizeof (ethernet_header_t));
2230       
2231           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2232                                            to_next, n_left_to_next,
2233                                            pi0, pi1, next0, next1);
2234         }
2235
2236       while (n_left_from > 0 && n_left_to_next > 0)
2237         {
2238           ip_adjacency_t * adj0;
2239           vlib_buffer_t * p0;
2240           ip6_header_t * ip0;
2241           u32 pi0, rw_len0;
2242           u32 adj_index0, next0, error0;
2243       
2244           pi0 = to_next[0] = from[0];
2245
2246           p0 = vlib_get_buffer (vm, pi0);
2247
2248           adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2249
2250           /* We should never rewrite a pkt using the MISS adjacency */
2251           ASSERT(adj_index0);
2252
2253           adj0 = ip_get_adjacency (lm, adj_index0);
2254       
2255           ip0 = vlib_buffer_get_current (p0);
2256
2257           error0 = IP6_ERROR_NONE;
2258
2259           /* Check hop limit */
2260           if (! rewrite_for_locally_received_packets)
2261             {
2262               i32 hop_limit0 = ip0->hop_limit;
2263
2264               ASSERT (ip0->hop_limit > 0);
2265
2266               hop_limit0 -= 1;
2267
2268               ip0->hop_limit = hop_limit0;
2269
2270               error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2271             }
2272
2273           if (rewrite_for_locally_received_packets)
2274             {
2275               if (PREDICT_FALSE(adj0->lookup_next_index 
2276                                 == IP_LOOKUP_NEXT_LOCAL))
2277                 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2278             }
2279
2280           /* Guess we are only writing on simple Ethernet header. */
2281           vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2282       
2283           /* Update packet buffer attributes/set output interface. */
2284           rw_len0 = adj0[0].rewrite_header.data_bytes;
2285
2286           vlib_increment_combined_counter (&lm->adjacency_counters,
2287                                            cpu_index, 
2288                                            adj_index0,
2289                                            /* packet increment */ 0,
2290                                            /* byte increment */ rw_len0);
2291
2292           /* Check MTU of outgoing interface. */
2293           error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2294                     ? IP6_ERROR_MTU_EXCEEDED
2295                     : error0);
2296
2297           p0->current_data -= rw_len0;
2298           p0->current_length += rw_len0;
2299           vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2300       
2301           next0 = (error0 == IP6_ERROR_NONE) ?
2302             adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2303
2304           p0->error = error_node->errors[error0];
2305
2306           from += 1;
2307           n_left_from -= 1;
2308           to_next += 1;
2309           n_left_to_next -= 1;
2310       
2311           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2312                                            to_next, n_left_to_next,
2313                                            pi0, next0);
2314         }
2315
2316       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2317     }
2318
2319   /* Need to do trace after rewrites to pick up new packet data. */
2320   if (node->flags & VLIB_NODE_FLAG_TRACE)
2321     ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2322
2323   return frame->n_vectors;
2324 }
2325
2326 static uword
2327 ip6_rewrite_transit (vlib_main_t * vm,
2328                      vlib_node_runtime_t * node,
2329                      vlib_frame_t * frame)
2330 {
2331   return ip6_rewrite_inline (vm, node, frame,
2332                              /* rewrite_for_locally_received_packets */ 0);
2333 }
2334
2335 static uword
2336 ip6_rewrite_local (vlib_main_t * vm,
2337                    vlib_node_runtime_t * node,
2338                    vlib_frame_t * frame)
2339 {
2340   return ip6_rewrite_inline (vm, node, frame,
2341                              /* rewrite_for_locally_received_packets */ 1);
2342 }
2343
2344 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2345   .function = ip6_rewrite_transit,
2346   .name = "ip6-rewrite",
2347   .vector_size = sizeof (u32),
2348
2349   .format_trace = format_ip6_forward_next_trace,
2350
2351   .n_next_nodes = 1,
2352   .next_nodes = {
2353     [IP6_REWRITE_NEXT_DROP] = "error-drop",
2354   },
2355 };
2356
2357 VLIB_REGISTER_NODE (ip6_rewrite_local_node,static) = {
2358   .function = ip6_rewrite_local,
2359   .name = "ip6-rewrite-local",
2360   .vector_size = sizeof (u32),
2361
2362   .sibling_of = "ip6-rewrite",
2363
2364   .format_trace = format_ip6_forward_next_trace,
2365
2366   .n_next_nodes = 1,
2367   .next_nodes = {
2368     [IP6_REWRITE_NEXT_DROP] = "error-drop",
2369   },
2370 };
2371
2372 /* Global IP6 main. */
2373 ip6_main_t ip6_main;
2374
2375 static clib_error_t *
2376 ip6_lookup_init (vlib_main_t * vm)
2377 {
2378   ip6_main_t * im = &ip6_main;
2379   uword i;
2380
2381   for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2382     {
2383       u32 j, i0, i1;
2384
2385       i0 = i / 32;
2386       i1 = i % 32;
2387
2388       for (j = 0; j < i0; j++)
2389         im->fib_masks[i].as_u32[j] = ~0;
2390
2391       if (i1)
2392         im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2393     }
2394
2395   ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2396
2397   if (im->lookup_table_nbuckets == 0)
2398     im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2399
2400   im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2401
2402   if (im->lookup_table_size == 0)
2403     im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2404   
2405   BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2406                         im->lookup_table_nbuckets,
2407                         im->lookup_table_size);
2408   
2409   /* Create FIB with index 0 and table id of 0. */
2410   find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2411
2412   {
2413     pg_node_t * pn;
2414     pn = pg_get_node (ip6_lookup_node.index);
2415     pn->unformat_edit = unformat_pg_ip6_header;
2416   }
2417
2418   {
2419     icmp6_neighbor_solicitation_header_t p;
2420
2421     memset (&p, 0, sizeof (p));
2422
2423     p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2424     p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2425                                                 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2426     p.ip.protocol = IP_PROTOCOL_ICMP6;
2427     p.ip.hop_limit = 255;
2428     ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2429
2430     p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2431
2432     p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2433     p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2434
2435     vlib_packet_template_init (vm,
2436                                &im->discover_neighbor_packet_template,
2437                                &p, sizeof (p),
2438                                /* alloc chunk size */ 8,
2439                                "ip6 neighbor discovery");
2440   }
2441
2442   return 0;
2443 }
2444
2445 VLIB_INIT_FUNCTION (ip6_lookup_init);
2446
2447 static clib_error_t *
2448 add_del_ip6_interface_table (vlib_main_t * vm,
2449                              unformat_input_t * input,
2450                              vlib_cli_command_t * cmd)
2451 {
2452   vnet_main_t * vnm = vnet_get_main();
2453   clib_error_t * error = 0;
2454   u32 sw_if_index, table_id;
2455
2456   sw_if_index = ~0;
2457
2458   if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2459     {
2460       error = clib_error_return (0, "unknown interface `%U'",
2461                                  format_unformat_error, input);
2462       goto done;
2463     }
2464
2465   if (unformat (input, "%d", &table_id))
2466     ;
2467   else
2468     {
2469       error = clib_error_return (0, "expected table id `%U'",
2470                                  format_unformat_error, input);
2471       goto done;
2472     }
2473
2474   {
2475     ip6_main_t * im = &ip6_main;
2476     ip6_fib_t * fib = 
2477       find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2478
2479     if (fib) 
2480       {
2481         vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2482         im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2483     }
2484   }
2485
2486  done:
2487   return error;
2488 }
2489
2490 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2491   .path = "set interface ip6 table",
2492   .function = add_del_ip6_interface_table,
2493   .short_help = "set interface ip6 table <intfc> <table-id>"
2494 };
2495
2496 void 
2497 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2498                                                   u8 *mac)
2499 {
2500   ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2501   /* Invert the "u" bit */
2502   ip->as_u8 [8] = mac[0] ^ (1<<1);
2503   ip->as_u8 [9] = mac[1];
2504   ip->as_u8 [10] = mac[2];
2505   ip->as_u8 [11] = 0xFF;
2506   ip->as_u8 [12] = 0xFE;
2507   ip->as_u8 [13] = mac[3];
2508   ip->as_u8 [14] = mac[4];
2509   ip->as_u8 [15] = mac[5];
2510 }
2511
2512 void 
2513 ip6_ethernet_mac_address_from_link_local_address (u8 *mac, 
2514                                                   ip6_address_t *ip)
2515 {
2516   /* Invert the previously inverted "u" bit */
2517   mac[0] = ip->as_u8 [8] ^ (1<<1);
2518   mac[1] = ip->as_u8 [9];
2519   mac[2] = ip->as_u8 [10];
2520   mac[3] = ip->as_u8 [13];
2521   mac[4] = ip->as_u8 [14];
2522   mac[5] = ip->as_u8 [15];
2523 }
2524
2525 static clib_error_t * 
2526 test_ip6_link_command_fn (vlib_main_t * vm,
2527                           unformat_input_t * input,
2528                           vlib_cli_command_t * cmd)
2529 {
2530   u8 mac[6];
2531   ip6_address_t _a, *a = &_a;
2532
2533   if (unformat (input, "%U", unformat_ethernet_address, mac))
2534     {
2535       ip6_link_local_address_from_ethernet_mac_address (a, mac);
2536       vlib_cli_output (vm, "Link local address: %U",
2537                        format_ip6_address, a);
2538       ip6_ethernet_mac_address_from_link_local_address (mac, a);
2539       vlib_cli_output (vm, "Original MAC address: %U",
2540                        format_ethernet_address, mac);
2541     }
2542                 
2543   return 0;
2544 }
2545
2546 VLIB_CLI_COMMAND (test_link_command, static) = {
2547   .path = "test ip6 link",
2548   .function = test_ip6_link_command_fn, 
2549   .short_help = "test ip6 link <mac-address>",
2550 };
2551
2552 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2553 {
2554   ip6_main_t * im6 = &ip6_main;
2555   ip6_fib_t * fib;
2556   uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2557
2558   if (p == 0)
2559     return -1;
2560
2561   fib = vec_elt_at_index (im6->fibs, p[0]);
2562
2563   fib->flow_hash_config = flow_hash_config;
2564   return 1;
2565 }
2566
2567 static clib_error_t *
2568 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2569                               unformat_input_t * input,
2570                               vlib_cli_command_t * cmd)
2571 {
2572   int matched = 0;
2573   u32 table_id = 0;
2574   u32 flow_hash_config = 0;
2575   int rv;
2576
2577   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2578     if (unformat (input, "table %d", &table_id))
2579       matched = 1;
2580 #define _(a,v) \
2581     else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2582     foreach_flow_hash_bit
2583 #undef _
2584     else break;
2585   }
2586
2587   if (matched == 0)
2588     return clib_error_return (0, "unknown input `%U'",
2589                               format_unformat_error, input);
2590   
2591   rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2592   switch (rv)
2593     {
2594     case 1:
2595       break;
2596
2597     case -1:
2598       return clib_error_return (0, "no such FIB table %d", table_id);
2599       
2600     default:
2601       clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2602       break;
2603     }
2604   
2605   return 0;
2606 }
2607
2608 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
2609     .path = "set ip6 flow-hash",
2610     .short_help = 
2611     "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
2612     .function = set_ip6_flow_hash_command_fn,
2613 };
2614
2615 static clib_error_t *
2616 show_ip6_local_command_fn (vlib_main_t * vm,
2617                            unformat_input_t * input,
2618                            vlib_cli_command_t * cmd)
2619 {
2620   ip6_main_t * im = &ip6_main;
2621   ip_lookup_main_t * lm = &im->lookup_main;
2622   int i;
2623   
2624   vlib_cli_output (vm, "Protocols handled by ip6_local");
2625   for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2626     {
2627       if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2628         vlib_cli_output (vm, "%d", i);
2629     }
2630   return 0;
2631 }
2632
2633
2634
2635 VLIB_CLI_COMMAND (show_ip_local, static) = {
2636   .path = "show ip6 local",
2637   .function = show_ip6_local_command_fn,
2638   .short_help = "Show ip6 local protocol table",
2639 };
2640
2641 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index, 
2642                                  u32 table_index)
2643 {
2644   vnet_main_t * vnm = vnet_get_main();
2645   vnet_interface_main_t * im = &vnm->interface_main;
2646   ip6_main_t * ipm = &ip6_main;
2647   ip_lookup_main_t * lm = &ipm->lookup_main;
2648   vnet_classify_main_t * cm = &vnet_classify_main;
2649
2650   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2651     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2652
2653   if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2654     return VNET_API_ERROR_NO_SUCH_ENTRY;
2655
2656   vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
2657   lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
2658
2659   return 0;
2660 }
2661
2662 static clib_error_t *
2663 set_ip6_classify_command_fn (vlib_main_t * vm,
2664                              unformat_input_t * input,
2665                              vlib_cli_command_t * cmd)
2666 {
2667   u32 table_index = ~0;
2668   int table_index_set = 0;
2669   u32 sw_if_index = ~0;
2670   int rv;
2671   
2672   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2673     if (unformat (input, "table-index %d", &table_index))
2674       table_index_set = 1;
2675     else if (unformat (input, "intfc %U", unformat_vnet_sw_interface, 
2676                        vnet_get_main(), &sw_if_index))
2677         ;
2678     else
2679         break;
2680   }
2681   
2682   if (table_index_set == 0)
2683       return clib_error_return (0, "classify table-index must be specified");
2684   
2685   if (sw_if_index == ~0)
2686     return clib_error_return (0, "interface / subif must be specified");
2687
2688   rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2689
2690   switch (rv)
2691     {
2692     case 0:
2693       break;
2694
2695     case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2696       return clib_error_return (0, "No such interface");
2697
2698     case VNET_API_ERROR_NO_SUCH_ENTRY:
2699       return clib_error_return (0, "No such classifier table");
2700     }
2701   return 0;
2702 }
2703
2704 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
2705     .path = "set ip6 classify",
2706     .short_help = 
2707     "set ip6 classify intfc <int> table-index <index>",
2708     .function = set_ip6_classify_command_fn,
2709 };
2710
2711 static clib_error_t *
2712 ip6_config (vlib_main_t * vm, unformat_input_t * input)
2713 {
2714   ip6_main_t * im = &ip6_main;
2715   uword heapsize = 0;
2716   u32 tmp;
2717   u32 nbuckets = 0;
2718
2719   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2720     if (unformat (input, "hash-buckets %d", &tmp))
2721       nbuckets = tmp;
2722     else if (unformat (input, "heap-size %dm", &tmp))
2723       heapsize = ((u64)tmp) << 20;
2724     else if (unformat (input, "heap-size %dM", &tmp))
2725       heapsize = ((u64)tmp) << 20;
2726     else if (unformat (input, "heap-size %dg", &tmp))
2727       heapsize = ((u64)tmp) << 30;
2728     else if (unformat (input, "heap-size %dG", &tmp))
2729       heapsize = ((u64)tmp) << 30;
2730     else
2731       return clib_error_return (0, "unknown input '%U'",
2732                                 format_unformat_error, input);
2733   }
2734
2735   im->lookup_table_nbuckets = nbuckets;
2736   im->lookup_table_size = heapsize;
2737
2738   return 0;
2739 }
2740
2741 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
2742