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