teib: Use ip_address_t not ip46_address_t
[vpp.git] / src / vnet / ipip / ipip.c
1 /*
2  * ipip.c: ipip
3  *
4  * Copyright (c) 2018 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or aipiped to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stddef.h>
19 #include <vnet/adj/adj_midchain.h>
20 #include <vnet/ipip/ipip.h>
21 #include <vnet/vnet.h>
22 #include <vnet/adj/adj_nbr.h>
23 #include <vnet/adj/adj_midchain.h>
24 #include <vnet/fib/ip4_fib.h>
25 #include <vnet/fib/ip6_fib.h>
26 #include <vnet/ip/format.h>
27 #include <vnet/ipip/ipip.h>
28 #include <vnet/teib/teib.h>
29 #include <vnet/tunnel/tunnel_dp.h>
30
31 ipip_main_t ipip_main;
32
33 /* Packet trace structure */
34 typedef struct
35 {
36   u32 tunnel_id;
37   u32 length;
38   ip46_address_t src;
39   ip46_address_t dst;
40 } ipip_tx_trace_t;
41
42 u8 *
43 format_ipip_tx_trace (u8 * s, va_list * args)
44 {
45   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47   ipip_tx_trace_t *t = va_arg (*args, ipip_tx_trace_t *);
48
49   s =
50     format (s, "IPIP: tunnel %d len %d src %U dst %U", t->tunnel_id,
51             t->length, format_ip46_address, &t->src, IP46_TYPE_ANY,
52             format_ip46_address, &t->dst, IP46_TYPE_ANY);
53   return s;
54 }
55
56 static u8 *
57 ipip_build_rewrite (vnet_main_t * vnm, u32 sw_if_index,
58                     vnet_link_t link_type, const void *dst_address)
59 {
60   const ip46_address_t *dst;
61   ip4_header_t *ip4;
62   ip6_header_t *ip6;
63   u8 *rewrite = NULL;
64   ipip_tunnel_t *t;
65
66   dst = dst_address;
67   t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
68
69   if (!t)
70     /* not one of ours */
71     return (0);
72
73   switch (t->transport)
74     {
75     case IPIP_TRANSPORT_IP4:
76       vec_validate (rewrite, sizeof (*ip4) - 1);
77       ip4 = (ip4_header_t *) rewrite;
78       ip4->ip_version_and_header_length = 0x45;
79       ip4->ttl = 64;
80       /* fixup ip4 header length, protocol and checksum after-the-fact */
81       ip4->src_address.as_u32 = t->tunnel_src.ip4.as_u32;
82       ip4->dst_address.as_u32 = dst->ip4.as_u32;
83       if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
84         ip4_header_set_dscp (ip4, t->dscp);
85       if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_SET_DF)
86         ip4_header_set_df (ip4);
87
88       switch (link_type)
89         {
90         case VNET_LINK_IP6:
91           ip4->protocol = IP_PROTOCOL_IPV6;
92           break;
93         case VNET_LINK_IP4:
94           ip4->protocol = IP_PROTOCOL_IP_IN_IP;
95           break;
96         default:
97           break;
98         }
99       ip4->checksum = ip4_header_checksum (ip4);
100       break;
101
102     case IPIP_TRANSPORT_IP6:
103       vec_validate (rewrite, sizeof (*ip6) - 1);
104       ip6 = (ip6_header_t *) rewrite;
105       ip6->ip_version_traffic_class_and_flow_label =
106         clib_host_to_net_u32 (6 << 28);
107       ip6->hop_limit = 64;
108       /* fixup ip6 header length and protocol after-the-fact */
109       ip6->src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
110       ip6->src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
111       ip6->dst_address.as_u64[0] = dst->ip6.as_u64[0];
112       ip6->dst_address.as_u64[1] = dst->ip6.as_u64[1];
113       if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
114         ip6_set_dscp_network_order (ip6, t->dscp);
115
116       switch (link_type)
117         {
118         case VNET_LINK_IP6:
119           ip6->protocol = IP_PROTOCOL_IPV6;
120           break;
121         case VNET_LINK_IP4:
122           ip6->protocol = IP_PROTOCOL_IP_IN_IP;
123           break;
124         default:
125           break;
126         }
127       break;
128     }
129   return (rewrite);
130 }
131
132 static void
133 ipip64_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
134               const void *data)
135 {
136   tunnel_encap_decap_flags_t flags;
137   ip4_header_t *ip4;
138
139   flags = pointer_to_uword (data);
140
141   ip4 = vlib_buffer_get_current (b);
142   ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
143   tunnel_encap_fixup_6o4 (flags, ((ip6_header_t *) (ip4 + 1)), ip4);
144
145   ip4->checksum = ip4_header_checksum (ip4);
146 }
147
148 static void
149 ipip44_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
150               const void *data)
151 {
152   tunnel_encap_decap_flags_t flags;
153   ip4_header_t *ip4;
154
155   flags = pointer_to_uword (data);
156
157   ip4 = vlib_buffer_get_current (b);
158   ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
159   tunnel_encap_fixup_4o4 (flags, ip4 + 1, ip4);
160
161   ip4->checksum = ip4_header_checksum (ip4);
162 }
163
164 static void
165 ipip46_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
166               const void *data)
167 {
168   tunnel_encap_decap_flags_t flags;
169   ip6_header_t *ip6;
170
171   flags = pointer_to_uword (data);
172
173   /* Must set locally originated otherwise we're not allowed to
174      fragment the packet later */
175   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
176
177   ip6 = vlib_buffer_get_current (b);
178   ip6->payload_length =
179     clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) -
180                           sizeof (*ip6));
181   tunnel_encap_fixup_4o6 (flags, ((ip4_header_t *) (ip6 + 1)), ip6);
182 }
183
184 static void
185 ipip66_fixup (vlib_main_t * vm,
186               const ip_adjacency_t * adj, vlib_buffer_t * b, const void *data)
187 {
188   tunnel_encap_decap_flags_t flags;
189   ip6_header_t *ip6;
190
191   flags = pointer_to_uword (data);
192
193   /* Must set locally originated otherwise we're not allowed to
194      fragment the packet later */
195   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
196
197   ip6 = vlib_buffer_get_current (b);
198   ip6->payload_length =
199     clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) -
200                           sizeof (*ip6));
201   tunnel_encap_fixup_6o6 (flags, ip6 + 1, ip6);
202 }
203
204 static void
205 ipip_tunnel_stack (adj_index_t ai)
206 {
207   ip_adjacency_t *adj;
208   ipip_tunnel_t *t;
209   u32 sw_if_index;
210
211   adj = adj_get (ai);
212   sw_if_index = adj->rewrite_header.sw_if_index;
213
214   t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
215   if (!t)
216     return;
217
218   if ((vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) &
219        VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
220     {
221       adj_midchain_delegate_unstack (ai);
222     }
223   else
224     {
225       /* *INDENT-OFF* */
226       fib_prefix_t dst = {
227         .fp_len = t->transport == IPIP_TRANSPORT_IP6 ? 128 : 32,
228         .fp_proto = (t->transport == IPIP_TRANSPORT_IP6 ?
229                      FIB_PROTOCOL_IP6 :
230                      FIB_PROTOCOL_IP4),
231         .fp_addr = t->tunnel_dst
232       };
233       /* *INDENT-ON* */
234
235       adj_midchain_delegate_stack (ai, t->fib_index, &dst);
236     }
237 }
238
239 static adj_walk_rc_t
240 ipip_adj_walk_cb (adj_index_t ai, void *ctx)
241 {
242   ipip_tunnel_stack (ai);
243
244   return (ADJ_WALK_RC_CONTINUE);
245 }
246
247 static void
248 ipip_tunnel_restack (ipip_tunnel_t * gt)
249 {
250   fib_protocol_t proto;
251
252   /*
253    * walk all the adjacencies on th IPIP interface and restack them
254    */
255   FOR_EACH_FIB_IP_PROTOCOL (proto)
256   {
257     adj_nbr_walk (gt->sw_if_index, proto, ipip_adj_walk_cb, NULL);
258   }
259 }
260
261 static adj_midchain_fixup_t
262 ipip_get_fixup (const ipip_tunnel_t * t, vnet_link_t lt, adj_flags_t * aflags)
263 {
264   if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_IP6)
265     return (ipip66_fixup);
266   if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_IP4)
267     return (ipip46_fixup);
268   if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP6)
269     return (ipip64_fixup);
270   if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP4)
271     {
272       *aflags = *aflags | ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR;
273       return (ipip44_fixup);
274     }
275
276   ASSERT (0);
277   return (ipip44_fixup);
278 }
279
280 void
281 ipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
282 {
283   adj_midchain_fixup_t fixup;
284   ipip_tunnel_t *t;
285   adj_flags_t af;
286
287   t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
288   if (!t)
289     return;
290
291   af = ADJ_FLAG_MIDCHAIN_IP_STACK;
292   if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
293     af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
294
295   fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af);
296   adj_nbr_midchain_update_rewrite
297     (ai, fixup,
298      uword_to_pointer (t->flags, void *), af,
299      ipip_build_rewrite (vnm, sw_if_index,
300                          adj_get_link_type (ai), &t->tunnel_dst));
301   ipip_tunnel_stack (ai);
302 }
303
304 typedef struct mipip_walk_ctx_t_
305 {
306   const ipip_tunnel_t *t;
307   const teib_entry_t *ne;
308 } mipip_walk_ctx_t;
309
310 static adj_walk_rc_t
311 mipip_mk_complete_walk (adj_index_t ai, void *data)
312 {
313   adj_midchain_fixup_t fixup;
314   mipip_walk_ctx_t *ctx = data;
315   adj_flags_t af;
316
317   af = ADJ_FLAG_NONE;
318   fixup = ipip_get_fixup (ctx->t, adj_get_link_type (ai), &af);
319
320   adj_nbr_midchain_update_rewrite
321     (ai, fixup,
322      uword_to_pointer (ctx->t->flags, void *),
323      ADJ_FLAG_MIDCHAIN_IP_STACK, ipip_build_rewrite (vnet_get_main (),
324                                                      ctx->t->sw_if_index,
325                                                      adj_get_link_type (ai),
326                                                      &teib_entry_get_nh
327                                                      (ctx->ne)->fp_addr));
328
329   teib_entry_adj_stack (ctx->ne, ai);
330
331   return (ADJ_WALK_RC_CONTINUE);
332 }
333
334 static adj_walk_rc_t
335 mipip_mk_incomplete_walk (adj_index_t ai, void *data)
336 {
337   adj_midchain_fixup_t fixup;
338   ipip_tunnel_t *t = data;
339   adj_flags_t af;
340
341   af = ADJ_FLAG_NONE;
342   fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af);
343
344   adj_nbr_midchain_update_rewrite (ai, fixup, NULL, ADJ_FLAG_NONE, NULL);
345
346   adj_midchain_delegate_unstack (ai);
347
348   return (ADJ_WALK_RC_CONTINUE);
349 }
350
351 void
352 mipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
353 {
354   ipip_main_t *gm = &ipip_main;
355   adj_midchain_fixup_t fixup;
356   ip_adjacency_t *adj;
357   teib_entry_t *ne;
358   ipip_tunnel_t *t;
359   adj_flags_t af;
360   u32 ti;
361
362   af = ADJ_FLAG_NONE;
363   adj = adj_get (ai);
364   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
365   t = pool_elt_at_index (gm->tunnels, ti);
366
367   ne = teib_entry_find_46 (sw_if_index,
368                            adj->ia_nh_proto, &adj->sub_type.nbr.next_hop);
369
370   if (NULL == ne)
371     {
372       // no TEIB entry to provide the next-hop
373       fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af);
374       adj_nbr_midchain_update_rewrite
375         (ai, fixup, uword_to_pointer (t->flags, void *), ADJ_FLAG_NONE, NULL);
376       return;
377     }
378
379   mipip_walk_ctx_t ctx = {
380     .t = t,
381     .ne = ne
382   };
383   adj_nbr_walk_nh (sw_if_index,
384                    adj->ia_nh_proto,
385                    &adj->sub_type.nbr.next_hop, mipip_mk_complete_walk, &ctx);
386 }
387
388 static u8 *
389 format_ipip_tunnel_name (u8 * s, va_list * args)
390 {
391   u32 dev_instance = va_arg (*args, u32);
392   ipip_main_t *gm = &ipip_main;
393   ipip_tunnel_t *t;
394
395   if (dev_instance >= vec_len (gm->tunnels))
396     return format (s, "<improperly-referenced>");
397
398   t = pool_elt_at_index (gm->tunnels, dev_instance);
399   return format (s, "ipip%d", t->user_instance);
400 }
401
402 static u8 *
403 format_ipip_device (u8 * s, va_list * args)
404 {
405   u32 dev_instance = va_arg (*args, u32);
406   CLIB_UNUSED (int verbose) = va_arg (*args, int);
407
408   s = format (s, "IPIP tunnel: id %d\n", dev_instance);
409   return s;
410 }
411
412 static clib_error_t *
413 ipip_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
414 {
415   vnet_hw_interface_t *hi;
416   ipip_tunnel_t *t;
417
418   hi = vnet_get_hw_interface (vnm, hw_if_index);
419
420   t = ipip_tunnel_db_find_by_sw_if_index (hi->sw_if_index);
421   if (!t)
422     return 0;
423
424   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
425     vnet_hw_interface_set_flags (vnm, hw_if_index,
426                                  VNET_HW_INTERFACE_FLAG_LINK_UP);
427   else
428     vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
429
430   ipip_tunnel_restack (t);
431
432   return /* no error */ 0;
433 }
434
435 static int
436 ipip_tunnel_desc (u32 sw_if_index,
437                   ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
438 {
439   ipip_tunnel_t *t;
440
441   t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
442   if (!t)
443     return -1;
444
445   *src = t->tunnel_src;
446   *dst = t->tunnel_dst;
447   *is_l2 = 0;
448
449   return (0);
450 }
451
452 /* *INDENT-OFF* */
453 VNET_DEVICE_CLASS(ipip_device_class) = {
454     .name = "IPIP tunnel device",
455     .format_device_name = format_ipip_tunnel_name,
456     .format_device = format_ipip_device,
457     .format_tx_trace = format_ipip_tx_trace,
458     .admin_up_down_function = ipip_interface_admin_up_down,
459     .ip_tun_desc = ipip_tunnel_desc,
460 #ifdef SOON
461     .clear counter = 0;
462 #endif
463 };
464
465 VNET_HW_INTERFACE_CLASS(ipip_hw_interface_class) = {
466     .name = "IPIP",
467     //.format_header = format_ipip_header_with_length,
468     //.unformat_header = unformat_ipip_header,
469     .build_rewrite = ipip_build_rewrite,
470     .update_adjacency = ipip_update_adj,
471     .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
472 };
473
474 VNET_HW_INTERFACE_CLASS(mipip_hw_interface_class) = {
475     .name = "mIPIP",
476     //.format_header = format_ipip_header_with_length,
477     //.unformat_header = unformat_ipip_header,
478     .build_rewrite = ipip_build_rewrite,
479     .update_adjacency = mipip_update_adj,
480     .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
481 };
482 /* *INDENT-ON* */
483
484 ipip_tunnel_t *
485 ipip_tunnel_db_find (const ipip_tunnel_key_t * key)
486 {
487   ipip_main_t *gm = &ipip_main;
488   uword *p;
489
490   p = hash_get_mem (gm->tunnel_by_key, key);
491   if (!p)
492     return (NULL);
493   return (pool_elt_at_index (gm->tunnels, p[0]));
494 }
495
496 ipip_tunnel_t *
497 ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index)
498 {
499   ipip_main_t *gm = &ipip_main;
500   if (vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index)
501     return NULL;
502   u32 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
503   if (ti == ~0)
504     return NULL;
505   return pool_elt_at_index (gm->tunnels, ti);
506 }
507
508 void
509 ipip_tunnel_db_add (ipip_tunnel_t * t, const ipip_tunnel_key_t * key)
510 {
511   ipip_main_t *gm = &ipip_main;
512
513   hash_set_mem_alloc (&gm->tunnel_by_key, key, t->dev_instance);
514 }
515
516 void
517 ipip_tunnel_db_remove (ipip_tunnel_t * t, const ipip_tunnel_key_t * key)
518 {
519   ipip_main_t *gm = &ipip_main;
520
521   hash_unset_mem_free (&gm->tunnel_by_key, key);
522 }
523
524 void
525 ipip_mk_key_i (ipip_transport_t transport,
526                ipip_mode_t mode,
527                const ip46_address_t * src,
528                const ip46_address_t * dst,
529                u32 fib_index, ipip_tunnel_key_t * key)
530 {
531   key->transport = transport;
532   key->mode = mode;
533   key->src = *src;
534   key->dst = *dst;
535   key->fib_index = fib_index;
536   key->__pad = 0;;
537 }
538
539 void
540 ipip_mk_key (const ipip_tunnel_t * t, ipip_tunnel_key_t * key)
541 {
542   ipip_mk_key_i (t->transport, t->mode,
543                  &t->tunnel_src, &t->tunnel_dst, t->fib_index, key);
544 }
545
546 static void
547 ipip_teib_mk_key (const ipip_tunnel_t * t,
548                   const teib_entry_t * ne, ipip_tunnel_key_t * key)
549 {
550   const fib_prefix_t *nh;
551
552   nh = teib_entry_get_nh (ne);
553
554   /* construct the key using mode P2P so it can be found in the DP */
555   ipip_mk_key_i (t->transport, IPIP_MODE_P2P,
556                  &t->tunnel_src, &nh->fp_addr,
557                  teib_entry_get_fib_index (ne), key);
558 }
559
560 static void
561 ipip_teib_entry_added (const teib_entry_t * ne)
562 {
563   ipip_main_t *gm = &ipip_main;
564   const ip_address_t *nh;
565   ipip_tunnel_key_t key;
566   ipip_tunnel_t *t;
567   u32 sw_if_index;
568   u32 t_idx;
569
570   sw_if_index = teib_entry_get_sw_if_index (ne);
571   if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
572     return;
573
574   t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
575
576   if (INDEX_INVALID == t_idx)
577     return;
578
579   t = pool_elt_at_index (gm->tunnels, t_idx);
580
581   ipip_teib_mk_key (t, ne, &key);
582   ipip_tunnel_db_add (t, &key);
583
584   // update the rewrites for each of the adjacencies for this next-hop
585   mipip_walk_ctx_t ctx = {
586     .t = t,
587     .ne = ne
588   };
589   nh = teib_entry_get_peer (ne);
590   adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne),
591                    (AF_IP4 == ip_addr_version (nh) ?
592                     FIB_PROTOCOL_IP4 :
593                     FIB_PROTOCOL_IP6),
594                    &ip_addr_46 (nh), mipip_mk_complete_walk, &ctx);
595 }
596
597 static void
598 ipip_teib_entry_deleted (const teib_entry_t * ne)
599 {
600   ipip_main_t *gm = &ipip_main;
601   const ip_address_t *nh;
602   ipip_tunnel_key_t key;
603   ipip_tunnel_t *t;
604   u32 sw_if_index;
605   u32 t_idx;
606
607   sw_if_index = teib_entry_get_sw_if_index (ne);
608   if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
609     return;
610
611   t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
612
613   if (INDEX_INVALID == t_idx)
614     return;
615
616   t = pool_elt_at_index (gm->tunnels, t_idx);
617
618   ipip_teib_mk_key (t, ne, &key);
619   ipip_tunnel_db_remove (t, &key);
620
621   nh = teib_entry_get_peer (ne);
622
623   /* make all the adjacencies incomplete */
624   adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne),
625                    (AF_IP4 == ip_addr_version (nh) ?
626                     FIB_PROTOCOL_IP4 :
627                     FIB_PROTOCOL_IP6),
628                    &ip_addr_46 (nh), mipip_mk_incomplete_walk, t);
629 }
630
631 static walk_rc_t
632 ipip_tunnel_delete_teib_walk (index_t nei, void *ctx)
633 {
634   ipip_tunnel_t *t = ctx;
635   ipip_tunnel_key_t key;
636
637   ipip_teib_mk_key (t, teib_entry_get (nei), &key);
638   ipip_tunnel_db_remove (t, &key);
639
640   return (WALK_CONTINUE);
641 }
642
643 static walk_rc_t
644 ipip_tunnel_add_teib_walk (index_t nei, void *ctx)
645 {
646   ipip_tunnel_t *t = ctx;
647   ipip_tunnel_key_t key;
648
649   ipip_teib_mk_key (t, teib_entry_get (nei), &key);
650   ipip_tunnel_db_add (t, &key);
651
652   return (WALK_CONTINUE);
653 }
654
655 int
656 ipip_add_tunnel (ipip_transport_t transport,
657                  u32 instance, ip46_address_t * src, ip46_address_t * dst,
658                  u32 fib_index, tunnel_encap_decap_flags_t flags,
659                  ip_dscp_t dscp, tunnel_mode_t tmode, u32 * sw_if_indexp)
660 {
661   ipip_main_t *gm = &ipip_main;
662   vnet_main_t *vnm = gm->vnet_main;
663   ip4_main_t *im4 = &ip4_main;
664   ip6_main_t *im6 = &ip6_main;
665   ipip_tunnel_t *t;
666   vnet_hw_interface_t *hi;
667   u32 hw_if_index, sw_if_index;
668   ipip_tunnel_key_t key;
669   ipip_mode_t mode;
670
671   if (tmode == TUNNEL_MODE_MP && !ip46_address_is_zero (dst))
672     return (VNET_API_ERROR_INVALID_DST_ADDRESS);
673
674   mode = (tmode == TUNNEL_MODE_P2P ? IPIP_MODE_P2P : IPIP_MODE_P2MP);
675   ipip_mk_key_i (transport, mode, src, dst, fib_index, &key);
676
677   t = ipip_tunnel_db_find (&key);
678   if (t)
679     {
680       if (sw_if_indexp)
681         sw_if_indexp[0] = t->sw_if_index;
682       return VNET_API_ERROR_IF_ALREADY_EXISTS;
683     }
684
685   pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
686   clib_memset (t, 0, sizeof (*t));
687
688   /* Reconcile the real dev_instance and a possible requested instance */
689   u32 t_idx = t - gm->tunnels;  /* tunnel index (or instance) */
690   u32 u_idx = instance;         /* user specified instance */
691   if (u_idx == ~0)
692     u_idx = t_idx;
693   if (hash_get (gm->instance_used, u_idx))
694     {
695       pool_put (gm->tunnels, t);
696       return VNET_API_ERROR_INSTANCE_IN_USE;
697     }
698   hash_set (gm->instance_used, u_idx, 1);
699
700   t->dev_instance = t_idx;      /* actual */
701   t->user_instance = u_idx;     /* name */
702
703   hw_if_index = vnet_register_interface (vnm, ipip_device_class.index, t_idx,
704                                          (mode == IPIP_MODE_P2P ?
705                                           ipip_hw_interface_class.index :
706                                           mipip_hw_interface_class.index),
707                                          t_idx);
708
709   hi = vnet_get_hw_interface (vnm, hw_if_index);
710   sw_if_index = hi->sw_if_index;
711
712   t->mode = mode;
713   t->hw_if_index = hw_if_index;
714   t->fib_index = fib_index;
715   t->sw_if_index = sw_if_index;
716   t->dscp = dscp;
717   t->flags = flags;
718   t->transport = transport;
719
720   vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
721   gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx;
722
723   if (t->transport == IPIP_TRANSPORT_IP4)
724     {
725       vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
726       hi->min_packet_bytes = 64 + sizeof (ip4_header_t);
727     }
728   else
729     {
730       vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
731       hi->min_packet_bytes = 64 + sizeof (ip6_header_t);
732     }
733
734   /* Standard default ipip MTU. */
735   vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000);
736
737   t->tunnel_src = *src;
738   t->tunnel_dst = *dst;
739
740   ipip_tunnel_db_add (t, &key);
741
742   if (t->mode == IPIP_MODE_P2MP)
743     teib_walk_itf (t->sw_if_index, ipip_tunnel_add_teib_walk, t);
744
745   if (sw_if_indexp)
746     *sw_if_indexp = sw_if_index;
747
748   if (t->transport == IPIP_TRANSPORT_IP6 && !gm->ip6_protocol_registered)
749     {
750       ip6_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip6_input_node.index);
751       ip6_register_protocol (IP_PROTOCOL_IPV6, ipip6_input_node.index);
752       gm->ip6_protocol_registered = true;
753     }
754   else if (t->transport == IPIP_TRANSPORT_IP4 && !gm->ip4_protocol_registered)
755     {
756       ip4_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip4_input_node.index);
757       ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input_node.index);
758       gm->ip4_protocol_registered = true;
759     }
760   return 0;
761 }
762
763 int
764 ipip_del_tunnel (u32 sw_if_index)
765 {
766   ipip_main_t *gm = &ipip_main;
767   vnet_main_t *vnm = gm->vnet_main;
768   ipip_tunnel_t *t;
769   ipip_tunnel_key_t key;
770
771   t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
772   if (t == NULL)
773     return VNET_API_ERROR_NO_SUCH_ENTRY;
774
775   if (t->mode == IPIP_MODE_P2MP)
776     teib_walk_itf (t->sw_if_index, ipip_tunnel_delete_teib_walk, t);
777
778   vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
779   gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
780   vnet_delete_hw_interface (vnm, t->hw_if_index);
781   hash_unset (gm->instance_used, t->user_instance);
782
783   ipip_mk_key (t, &key);
784   ipip_tunnel_db_remove (t, &key);
785   pool_put (gm->tunnels, t);
786
787   return 0;
788 }
789
790 const static teib_vft_t ipip_teib_vft = {
791   .nv_added = ipip_teib_entry_added,
792   .nv_deleted = ipip_teib_entry_deleted,
793 };
794
795 static clib_error_t *
796 ipip_init (vlib_main_t * vm)
797 {
798   ipip_main_t *gm = &ipip_main;
799
800   clib_memset (gm, 0, sizeof (gm[0]));
801   gm->vlib_main = vm;
802   gm->vnet_main = vnet_get_main ();
803   gm->tunnel_by_key =
804     hash_create_mem (0, sizeof (ipip_tunnel_key_t), sizeof (uword));
805
806   teib_register (&ipip_teib_vft);
807
808   return 0;
809 }
810
811 VLIB_INIT_FUNCTION (ipip_init);
812
813 /*
814  * fd.io coding-style-patch-verification: ON
815  *
816  * Local Variables:
817  * eval: (c-set-style "gnu")
818  * End:
819  */