fib feature: Code mechanics to decouple dependency of feature on adj
[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
29 ipip_main_t ipip_main;
30
31 /* Packet trace structure */
32 typedef struct
33 {
34   u32 tunnel_id;
35   u32 length;
36   ip46_address_t src;
37   ip46_address_t dst;
38 } ipip_tx_trace_t;
39
40 u8 *
41 format_ipip_tx_trace (u8 * s, va_list * args)
42 {
43   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
44   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
45   ipip_tx_trace_t *t = va_arg (*args, ipip_tx_trace_t *);
46
47   s =
48     format (s, "IPIP: tunnel %d len %d src %U dst %U", t->tunnel_id,
49             t->length, format_ip46_address, &t->src, IP46_TYPE_ANY,
50             format_ip46_address, &t->dst, IP46_TYPE_ANY);
51   return s;
52 }
53
54 static u8 *
55 ipip_build_rewrite (vnet_main_t * vnm, u32 sw_if_index,
56                     vnet_link_t link_type, const void *dst_address)
57 {
58   ip4_header_t *ip4;
59   ip6_header_t *ip6;
60   u8 *rewrite = NULL;
61   ipip_tunnel_t *t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
62
63   if (!t)
64     /* not one of ours */
65     return (0);
66
67   switch (t->transport)
68     {
69     case IPIP_TRANSPORT_IP4:
70       vec_validate (rewrite, sizeof (*ip4) - 1);
71       ip4 = (ip4_header_t *) rewrite;
72       ip4->ip_version_and_header_length = 0x45;
73       ip4->ttl = 64;
74       /* fixup ip4 header length, protocol and checksum after-the-fact */
75       ip4->src_address.as_u32 = t->tunnel_src.ip4.as_u32;
76       ip4->dst_address.as_u32 = t->tunnel_dst.ip4.as_u32;
77       ip4->checksum = ip4_header_checksum (ip4);
78       if (t->tc_tos != 0xFF)
79         ip4->tos = t->tc_tos;
80       break;
81
82     case IPIP_TRANSPORT_IP6:
83       vec_validate (rewrite, sizeof (*ip6) - 1);
84       ip6 = (ip6_header_t *) rewrite;
85       ip6->ip_version_traffic_class_and_flow_label =
86         clib_host_to_net_u32 (6 << 28);
87       if (t->tc_tos != 0xFF)
88         ip6_set_traffic_class_network_order (ip6, t->tc_tos);
89       ip6->hop_limit = 64;
90       /* fixup ip6 header length and protocol after-the-fact */
91       ip6->src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
92       ip6->src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
93       ip6->dst_address.as_u64[0] = t->tunnel_dst.ip6.as_u64[0];
94       ip6->dst_address.as_u64[1] = t->tunnel_dst.ip6.as_u64[1];
95       break;
96
97     default:
98       /* pass through */
99       ;
100     }
101   return (rewrite);
102 }
103
104 static void
105 ipip4_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b,
106              const void *data)
107 {
108   ip4_header_t *ip4;
109   const ipip_tunnel_t *t = data;
110
111   ip4 = vlib_buffer_get_current (b);
112   ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
113   switch (adj->ia_link)
114     {
115     case VNET_LINK_IP6:
116       ip4->protocol = IP_PROTOCOL_IPV6;
117       if (t->tc_tos == 0xFF)
118         ip4->tos =
119           ip6_traffic_class_network_order ((const ip6_header_t *) (ip4 + 1));
120       break;
121
122     case VNET_LINK_IP4:
123       ip4->protocol = IP_PROTOCOL_IP_IN_IP;
124       if (t->tc_tos == 0xFF)
125         ip4->tos = ((ip4_header_t *) (ip4 + 1))->tos;
126       break;
127
128     default:
129       break;
130     }
131
132   ip4->checksum = ip4_header_checksum (ip4);
133 }
134
135 static void
136 ipip6_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b,
137              const void *data)
138 {
139   ip6_header_t *ip6;
140   const ipip_tunnel_t *t = data;
141
142   /* Must set locally originated otherwise we're not allowed to
143      fragment the packet later */
144   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
145
146   ip6 = vlib_buffer_get_current (b);
147   ip6->payload_length =
148     clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) -
149                           sizeof (*ip6));
150   switch (adj->ia_link)
151     {
152     case VNET_LINK_IP6:
153       ip6->protocol = IP_PROTOCOL_IPV6;
154       if (t->tc_tos == 0xFF)
155         ip6_set_traffic_class_network_order (ip6,
156                                              ip6_traffic_class_network_order ((const ip6_header_t *) (ip6 + 1)));
157       break;
158
159     case VNET_LINK_IP4:
160       ip6->protocol = IP_PROTOCOL_IP_IN_IP;
161       if (t->tc_tos == 0xFF)
162         ip6_set_traffic_class_network_order (ip6,
163                                              ((ip4_header_t *) (ip6 +
164                                                                 1))->tos);
165       break;
166
167     default:
168       break;
169     }
170 }
171
172 static void
173 ipip_tunnel_stack (adj_index_t ai)
174 {
175   ip_adjacency_t *adj;
176   ipip_tunnel_t *t;
177   u32 sw_if_index;
178
179   adj = adj_get (ai);
180   sw_if_index = adj->rewrite_header.sw_if_index;
181
182   t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
183   if (!t)
184     return;
185
186   if ((vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) &
187        VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
188     {
189       adj_midchain_delegate_unstack (ai);
190     }
191   else
192     {
193       /* *INDENT-OFF* */
194       fib_prefix_t dst = {
195         .fp_len = t->transport == IPIP_TRANSPORT_IP6 ? 128 : 32,
196         .fp_proto = (t->transport == IPIP_TRANSPORT_IP6 ?
197                      FIB_PROTOCOL_IP6 :
198                      FIB_PROTOCOL_IP4),
199         .fp_addr = t->tunnel_dst
200       };
201       /* *INDENT-ON* */
202
203       adj_midchain_delegate_stack (ai, t->fib_index, &dst);
204     }
205 }
206
207 static adj_walk_rc_t
208 ipip_adj_walk_cb (adj_index_t ai, void *ctx)
209 {
210   ipip_tunnel_stack (ai);
211
212   return (ADJ_WALK_RC_CONTINUE);
213 }
214
215 static void
216 ipip_tunnel_restack (ipip_tunnel_t * gt)
217 {
218   fib_protocol_t proto;
219
220   /*
221    * walk all the adjacencies on th IPIP interface and restack them
222    */
223   FOR_EACH_FIB_IP_PROTOCOL (proto)
224   {
225     adj_nbr_walk (gt->sw_if_index, proto, ipip_adj_walk_cb, NULL);
226   }
227 }
228
229 void
230 ipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
231 {
232   adj_midchain_fixup_t f;
233   ipip_tunnel_t *t;
234   adj_flags_t af;
235
236   t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
237   if (!t)
238     return;
239
240   f = t->transport == IPIP_TRANSPORT_IP6 ? ipip6_fixup : ipip4_fixup;
241   af = ADJ_FLAG_MIDCHAIN_IP_STACK;
242   if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
243     af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
244
245   adj_nbr_midchain_update_rewrite (ai, f, t, af,
246                                    ipip_build_rewrite (vnm,
247                                                        sw_if_index,
248                                                        adj_get_link_type
249                                                        (ai), NULL));
250   ipip_tunnel_stack (ai);
251 }
252
253 static u8 *
254 format_ipip_tunnel_name (u8 * s, va_list * args)
255 {
256   u32 dev_instance = va_arg (*args, u32);
257   ipip_main_t *gm = &ipip_main;
258   ipip_tunnel_t *t;
259
260   if (dev_instance >= vec_len (gm->tunnels))
261     return format (s, "<improperly-referenced>");
262
263   t = pool_elt_at_index (gm->tunnels, dev_instance);
264   return format (s, "ipip%d", t->user_instance);
265 }
266
267 static u8 *
268 format_ipip_device (u8 * s, va_list * args)
269 {
270   u32 dev_instance = va_arg (*args, u32);
271   CLIB_UNUSED (int verbose) = va_arg (*args, int);
272
273   s = format (s, "IPIP tunnel: id %d\n", dev_instance);
274   return s;
275 }
276
277 static clib_error_t *
278 ipip_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
279 {
280   vnet_hw_interface_t *hi;
281   ipip_tunnel_t *t;
282
283   hi = vnet_get_hw_interface (vnm, hw_if_index);
284
285   t = ipip_tunnel_db_find_by_sw_if_index (hi->sw_if_index);
286   if (!t)
287     return 0;
288
289   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
290     vnet_hw_interface_set_flags (vnm, hw_if_index,
291                                  VNET_HW_INTERFACE_FLAG_LINK_UP);
292   else
293     vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
294
295   ipip_tunnel_restack (t);
296
297   return /* no error */ 0;
298 }
299
300 static int
301 ipip_tunnel_desc (u32 sw_if_index,
302                   ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
303 {
304   ipip_tunnel_t *t;
305
306   t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
307   if (!t)
308     return -1;
309
310   *src = t->tunnel_src;
311   *dst = t->tunnel_dst;
312   *is_l2 = 0;
313
314   return (0);
315 }
316
317 /* *INDENT-OFF* */
318 VNET_DEVICE_CLASS(ipip_device_class) = {
319     .name = "IPIP tunnel device",
320     .format_device_name = format_ipip_tunnel_name,
321     .format_device = format_ipip_device,
322     .format_tx_trace = format_ipip_tx_trace,
323     .admin_up_down_function = ipip_interface_admin_up_down,
324     .ip_tun_desc = ipip_tunnel_desc,
325 #ifdef SOON
326     .clear counter = 0;
327 #endif
328 };
329
330 VNET_HW_INTERFACE_CLASS(ipip_hw_interface_class) = {
331     .name = "IPIP",
332     //.format_header = format_ipip_header_with_length,
333     //.unformat_header = unformat_ipip_header,
334     .build_rewrite = ipip_build_rewrite,
335     .update_adjacency = ipip_update_adj,
336     .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
337 };
338 /* *INDENT-ON* */
339
340 ipip_tunnel_t *
341 ipip_tunnel_db_find (ipip_tunnel_key_t * key)
342 {
343   ipip_main_t *gm = &ipip_main;
344   uword *p;
345
346   p = hash_get_mem (gm->tunnel_by_key, key);
347   if (!p)
348     return (NULL);
349   return (pool_elt_at_index (gm->tunnels, p[0]));
350 }
351
352 ipip_tunnel_t *
353 ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index)
354 {
355   ipip_main_t *gm = &ipip_main;
356   if (vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index)
357     return NULL;
358   u32 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
359   if (ti == ~0)
360     return NULL;
361   return pool_elt_at_index (gm->tunnels, ti);
362 }
363
364 void
365 ipip_tunnel_db_add (ipip_tunnel_t * t, ipip_tunnel_key_t * key)
366 {
367   ipip_main_t *gm = &ipip_main;
368
369   t->key = clib_mem_alloc (sizeof (*t->key));
370   clib_memcpy (t->key, key, sizeof (*key));
371   hash_set_mem (gm->tunnel_by_key, t->key, t->dev_instance);
372 }
373
374 void
375 ipip_tunnel_db_remove (ipip_tunnel_t * t)
376 {
377   ipip_main_t *gm = &ipip_main;
378
379   hash_unset_mem (gm->tunnel_by_key, t->key);
380   clib_mem_free (t->key);
381   t->key = NULL;
382 }
383
384 int
385 ipip_add_tunnel (ipip_transport_t transport,
386                  u32 instance, ip46_address_t * src, ip46_address_t * dst,
387                  u32 fib_index, u8 tc_tos, u32 * sw_if_indexp)
388 {
389   ipip_main_t *gm = &ipip_main;
390   vnet_main_t *vnm = gm->vnet_main;
391   ip4_main_t *im4 = &ip4_main;
392   ip6_main_t *im6 = &ip6_main;
393   ipip_tunnel_t *t;
394   vnet_hw_interface_t *hi;
395   u32 hw_if_index, sw_if_index;
396   ipip_tunnel_key_t key = {.transport = transport,
397     .fib_index = fib_index,
398     .src = *src,
399     .dst = *dst
400   };
401   t = ipip_tunnel_db_find (&key);
402   if (t)
403     return VNET_API_ERROR_IF_ALREADY_EXISTS;
404
405   pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
406   clib_memset (t, 0, sizeof (*t));
407
408   /* Reconcile the real dev_instance and a possible requested instance */
409   u32 t_idx = t - gm->tunnels;  /* tunnel index (or instance) */
410   u32 u_idx = instance;         /* user specified instance */
411   if (u_idx == ~0)
412     u_idx = t_idx;
413   if (hash_get (gm->instance_used, u_idx))
414     {
415       pool_put (gm->tunnels, t);
416       return VNET_API_ERROR_INSTANCE_IN_USE;
417     }
418   hash_set (gm->instance_used, u_idx, 1);
419
420   t->dev_instance = t_idx;      /* actual */
421   t->user_instance = u_idx;     /* name */
422
423   hw_if_index = vnet_register_interface (vnm, ipip_device_class.index, t_idx,
424                                          ipip_hw_interface_class.index,
425                                          t_idx);
426
427   hi = vnet_get_hw_interface (vnm, hw_if_index);
428   sw_if_index = hi->sw_if_index;
429
430   t->hw_if_index = hw_if_index;
431   t->fib_index = fib_index;
432   t->sw_if_index = sw_if_index;
433   t->tc_tos = tc_tos;
434
435   t->transport = transport;
436   vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
437   gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx;
438
439   if (t->transport == IPIP_TRANSPORT_IP4)
440     {
441       vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
442       hi->min_packet_bytes = 64 + sizeof (ip4_header_t);
443     }
444   else
445     {
446       vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
447       hi->min_packet_bytes = 64 + sizeof (ip6_header_t);
448     }
449
450   /* Standard default ipip MTU. */
451   vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000);
452
453   t->tunnel_src = *src;
454   t->tunnel_dst = *dst;
455
456   ipip_tunnel_db_add (t, &key);
457
458   if (sw_if_indexp)
459     *sw_if_indexp = sw_if_index;
460
461   if (t->transport == IPIP_TRANSPORT_IP6 && !gm->ip6_protocol_registered)
462     {
463       ip6_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip6_input_node.index);
464       ip6_register_protocol (IP_PROTOCOL_IPV6, ipip6_input_node.index);
465       gm->ip6_protocol_registered = true;
466     }
467   else if (t->transport == IPIP_TRANSPORT_IP4 && !gm->ip4_protocol_registered)
468     {
469       ip4_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip4_input_node.index);
470       ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input_node.index);
471       gm->ip4_protocol_registered = true;
472     }
473   return 0;
474 }
475
476 int
477 ipip_del_tunnel (u32 sw_if_index)
478 {
479   ipip_main_t *gm = &ipip_main;
480   vnet_main_t *vnm = gm->vnet_main;
481   ipip_tunnel_t *t;
482
483
484   t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
485   if (t == NULL)
486     return VNET_API_ERROR_NO_SUCH_ENTRY;
487
488   vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
489   gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
490   vnet_delete_hw_interface (vnm, t->hw_if_index);
491   hash_unset (gm->instance_used, t->user_instance);
492   ipip_tunnel_db_remove (t);
493   pool_put (gm->tunnels, t);
494
495   return 0;
496 }
497
498 static clib_error_t *
499 ipip_init (vlib_main_t * vm)
500 {
501   ipip_main_t *gm = &ipip_main;
502
503   clib_memset (gm, 0, sizeof (gm[0]));
504   gm->vlib_main = vm;
505   gm->vnet_main = vnet_get_main ();
506   gm->tunnel_by_key =
507     hash_create_mem (0, sizeof (ipip_tunnel_key_t), sizeof (uword));
508
509   return 0;
510 }
511
512 VLIB_INIT_FUNCTION (ipip_init);
513
514 /*
515  * fd.io coding-style-patch-verification: ON
516  *
517  * Local Variables:
518  * eval: (c-set-style "gnu")
519  * End:
520  */