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