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