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