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