Add l3 routing for GTP-U decap
[vpp.git] / src / plugins / gtpu / gtpu.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Intel and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 #include <stdint.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20 #include <inttypes.h>
21
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/fib_entry.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/mfib/mfib_table.h>
28 #include <vnet/adj/adj_mcast.h>
29 #include <vnet/dpo/dpo.h>
30 #include <vnet/plugin/plugin.h>
31 #include <vpp/app/version.h>
32 #include <gtpu/gtpu.h>
33
34
35 gtpu_main_t gtpu_main;
36
37 static u8 *
38 format_decap_next (u8 * s, va_list * args)
39 {
40   u32 next_index = va_arg (*args, u32);
41
42   switch (next_index)
43     {
44     case GTPU_INPUT_NEXT_DROP:
45       return format (s, "drop");
46     case GTPU_INPUT_NEXT_L2_INPUT:
47       return format (s, "l2");
48     case GTPU_INPUT_NEXT_IP4_INPUT:
49       return format (s, "ip4");
50     case GTPU_INPUT_NEXT_IP6_INPUT:
51       return format (s, "ip6");
52     default:
53       return format (s, "index %d", next_index);
54     }
55   return s;
56 }
57
58 u8 *
59 format_gtpu_tunnel (u8 * s, va_list * args)
60 {
61   gtpu_tunnel_t *t = va_arg (*args, gtpu_tunnel_t *);
62   gtpu_main_t *ngm = &gtpu_main;
63
64   s = format (s, "[%d] src %U dst %U teid %d sw_if_index %d ",
65               t - ngm->tunnels,
66               format_ip46_address, &t->src, IP46_TYPE_ANY,
67               format_ip46_address, &t->dst, IP46_TYPE_ANY,
68               t->teid, t->sw_if_index);
69
70   if (ip46_address_is_multicast (&t->dst))
71     s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index);
72
73   s = format (s, "encap_fib_index %d fib_entry_index %d decap_next %U\n",
74               t->encap_fib_index, t->fib_entry_index,
75               format_decap_next, t->decap_next_index);
76   return s;
77 }
78
79 static u8 *
80 format_gtpu_name (u8 * s, va_list * args)
81 {
82   u32 dev_instance = va_arg (*args, u32);
83   return format (s, "gtpu_tunnel%d", dev_instance);
84 }
85
86 static uword
87 dummy_interface_tx (vlib_main_t * vm,
88                     vlib_node_runtime_t * node, vlib_frame_t * frame)
89 {
90   clib_warning ("you shouldn't be here, leaking buffers...");
91   return frame->n_vectors;
92 }
93
94 static clib_error_t *
95 gtpu_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
96 {
97   u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
98     VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
99   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
100
101   return /* no error */ 0;
102 }
103
104 /* *INDENT-OFF* */
105 VNET_DEVICE_CLASS (gtpu_device_class,static) = {
106   .name = "GTPU",
107   .format_device_name = format_gtpu_name,
108   .format_tx_trace = format_gtpu_encap_trace,
109   .tx_function = dummy_interface_tx,
110   .admin_up_down_function = gtpu_interface_admin_up_down,
111 };
112 /* *INDENT-ON* */
113
114 static u8 *
115 format_gtpu_header_with_length (u8 * s, va_list * args)
116 {
117   u32 dev_instance = va_arg (*args, u32);
118   s = format (s, "unimplemented dev %u", dev_instance);
119   return s;
120 }
121
122 /* *INDENT-OFF* */
123 VNET_HW_INTERFACE_CLASS (gtpu_hw_class) =
124 {
125   .name = "GTPU",
126   .format_header = format_gtpu_header_with_length,
127   .build_rewrite = default_build_rewrite,
128 };
129 /* *INDENT-ON* */
130
131 static void
132 gtpu_tunnel_restack_dpo (gtpu_tunnel_t * t)
133 {
134   dpo_id_t dpo = DPO_INVALID;
135   u32 encap_index = ip46_address_is_ip4 (&t->dst) ?
136     gtpu4_encap_node.index : gtpu6_encap_node.index;
137   fib_forward_chain_type_t forw_type = ip46_address_is_ip4 (&t->dst) ?
138     FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
139
140   fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
141   dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
142   dpo_reset (&dpo);
143 }
144
145 static gtpu_tunnel_t *
146 gtpu_tunnel_from_fib_node (fib_node_t * node)
147 {
148   return ((gtpu_tunnel_t *) (((char *) node) -
149                              STRUCT_OFFSET_OF (gtpu_tunnel_t, node)));
150 }
151
152 /**
153  * Function definition to backwalk a FIB node -
154  * Here we will restack the new dpo of GTPU DIP to encap node.
155  */
156 static fib_node_back_walk_rc_t
157 gtpu_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
158 {
159   gtpu_tunnel_restack_dpo (gtpu_tunnel_from_fib_node (node));
160   return (FIB_NODE_BACK_WALK_CONTINUE);
161 }
162
163 /**
164  * Function definition to get a FIB node from its index
165  */
166 static fib_node_t *
167 gtpu_tunnel_fib_node_get (fib_node_index_t index)
168 {
169   gtpu_tunnel_t *t;
170   gtpu_main_t *gtm = &gtpu_main;
171
172   t = pool_elt_at_index (gtm->tunnels, index);
173
174   return (&t->node);
175 }
176
177 /**
178  * Function definition to inform the FIB node that its last lock has gone.
179  */
180 static void
181 gtpu_tunnel_last_lock_gone (fib_node_t * node)
182 {
183   /*
184    * The GTPU tunnel is a root of the graph. As such
185    * it never has children and thus is never locked.
186    */
187   ASSERT (0);
188 }
189
190 /*
191  * Virtual function table registered by GTPU tunnels
192  * for participation in the FIB object graph.
193  */
194 const static fib_node_vft_t gtpu_vft = {
195   .fnv_get = gtpu_tunnel_fib_node_get,
196   .fnv_last_lock = gtpu_tunnel_last_lock_gone,
197   .fnv_back_walk = gtpu_tunnel_back_walk,
198 };
199
200
201 #define foreach_copy_field                      \
202 _(teid)                                          \
203 _(mcast_sw_if_index)                            \
204 _(encap_fib_index)                              \
205 _(decap_next_index)                             \
206 _(src)                                          \
207 _(dst)
208
209 static void
210 ip_udp_gtpu_rewrite (gtpu_tunnel_t * t, bool is_ip6)
211 {
212   union
213   {
214     ip4_gtpu_header_t *h4;
215     ip6_gtpu_header_t *h6;
216     u8 *rw;
217   } r =
218   {
219   .rw = 0};
220   int len = is_ip6 ? sizeof *r.h6 : sizeof *r.h4;
221
222   vec_validate_aligned (r.rw, len - 1, CLIB_CACHE_LINE_BYTES);
223
224   udp_header_t *udp;
225   gtpu_header_t *gtpu;
226   /* Fixed portion of the (outer) ip header */
227   if (!is_ip6)
228     {
229       ip4_header_t *ip = &r.h4->ip4;
230       udp = &r.h4->udp;
231       gtpu = &r.h4->gtpu;
232       ip->ip_version_and_header_length = 0x45;
233       ip->ttl = 254;
234       ip->protocol = IP_PROTOCOL_UDP;
235
236       ip->src_address = t->src.ip4;
237       ip->dst_address = t->dst.ip4;
238
239       /* we fix up the ip4 header length and checksum after-the-fact */
240       ip->checksum = ip4_header_checksum (ip);
241     }
242   else
243     {
244       ip6_header_t *ip = &r.h6->ip6;
245       udp = &r.h6->udp;
246       gtpu = &r.h6->gtpu;
247       ip->ip_version_traffic_class_and_flow_label =
248         clib_host_to_net_u32 (6 << 28);
249       ip->hop_limit = 255;
250       ip->protocol = IP_PROTOCOL_UDP;
251
252       ip->src_address = t->src.ip6;
253       ip->dst_address = t->dst.ip6;
254     }
255
256   /* UDP header, randomize src port on something, maybe? */
257   udp->src_port = clib_host_to_net_u16 (2152);
258   udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_GTPU);
259
260   /* GTPU header */
261   gtpu->ver_flags = GTPU_V1_VER | GTPU_PT_GTP;
262   gtpu->type = GTPU_TYPE_GTPU;
263   gtpu->teid = clib_host_to_net_u32 (t->teid);
264
265   t->rewrite = r.rw;
266   /* Now only support 8-byte gtpu header. TBD */
267   _vec_len (t->rewrite) = sizeof (ip4_gtpu_header_t) - 4;
268
269   return;
270 }
271
272 static bool
273 gtpu_decap_next_is_valid (gtpu_main_t * gtm, u32 is_ip6, u32 decap_next_index)
274 {
275   vlib_main_t *vm = gtm->vlib_main;
276   u32 input_idx = (!is_ip6) ? gtpu4_input_node.index : gtpu6_input_node.index;
277   vlib_node_runtime_t *r = vlib_node_get_runtime (vm, input_idx);
278
279   return decap_next_index < r->n_next_nodes;
280 }
281
282 static void
283 hash_set_key_copy (uword ** h, void *key, uword v)
284 {
285   size_t ksz = hash_header (*h)->user;
286   void *copy = clib_mem_alloc (ksz);
287   clib_memcpy (copy, key, ksz);
288   hash_set_mem (*h, copy, v);
289 }
290
291 static void
292 hash_unset_key_free (uword ** h, void *key)
293 {
294   hash_pair_t *hp = hash_get_pair_mem (*h, key);
295   ASSERT (hp);
296   key = uword_to_pointer (hp->key, void *);
297   hash_unset_mem (*h, key);
298   clib_mem_free (key);
299 }
300
301 static uword
302 vtep_addr_ref (ip46_address_t * ip)
303 {
304   uword *vtep = ip46_address_is_ip4 (ip) ?
305     hash_get (gtpu_main.vtep4, ip->ip4.as_u32) :
306     hash_get_mem (gtpu_main.vtep6, &ip->ip6);
307   if (vtep)
308     return ++(*vtep);
309   ip46_address_is_ip4 (ip) ?
310     hash_set (gtpu_main.vtep4, ip->ip4.as_u32, 1) :
311     hash_set_key_copy (&gtpu_main.vtep6, &ip->ip6, 1);
312   return 1;
313 }
314
315 static uword
316 vtep_addr_unref (ip46_address_t * ip)
317 {
318   uword *vtep = ip46_address_is_ip4 (ip) ?
319     hash_get (gtpu_main.vtep4, ip->ip4.as_u32) :
320     hash_get_mem (gtpu_main.vtep6, &ip->ip6);
321   ASSERT (vtep);
322   if (--(*vtep) != 0)
323     return *vtep;
324   ip46_address_is_ip4 (ip) ?
325     hash_unset (gtpu_main.vtep4, ip->ip4.as_u32) :
326     hash_unset_key_free (&gtpu_main.vtep6, &ip->ip6);
327   return 0;
328 }
329
330 typedef CLIB_PACKED (union
331                      {
332                      struct
333                      {
334                      fib_node_index_t mfib_entry_index;
335                      adj_index_t mcast_adj_index;
336                      }; u64 as_u64;
337                      }) mcast_shared_t;
338
339 static inline mcast_shared_t
340 mcast_shared_get (ip46_address_t * ip)
341 {
342   ASSERT (ip46_address_is_multicast (ip));
343   uword *p = hash_get_mem (gtpu_main.mcast_shared, ip);
344   ASSERT (p);
345   return (mcast_shared_t)
346   {
347   .as_u64 = *p};
348 }
349
350 static inline void
351 mcast_shared_add (ip46_address_t * dst, fib_node_index_t mfei, adj_index_t ai)
352 {
353   mcast_shared_t new_ep = {
354     .mcast_adj_index = ai,
355     .mfib_entry_index = mfei,
356   };
357
358   hash_set_key_copy (&gtpu_main.mcast_shared, dst, new_ep.as_u64);
359 }
360
361 static inline void
362 mcast_shared_remove (ip46_address_t * dst)
363 {
364   mcast_shared_t ep = mcast_shared_get (dst);
365
366   adj_unlock (ep.mcast_adj_index);
367   mfib_table_entry_delete_index (ep.mfib_entry_index, MFIB_SOURCE_GTPU);
368
369   hash_unset_key_free (&gtpu_main.mcast_shared, dst);
370 }
371
372 static inline fib_protocol_t
373 fib_ip_proto (bool is_ip6)
374 {
375   return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
376 }
377
378 int vnet_gtpu_add_del_tunnel
379   (vnet_gtpu_add_del_tunnel_args_t * a, u32 * sw_if_indexp)
380 {
381   gtpu_main_t *gtm = &gtpu_main;
382   gtpu_tunnel_t *t = 0;
383   vnet_main_t *vnm = gtm->vnet_main;
384   uword *p;
385   u32 hw_if_index = ~0;
386   u32 sw_if_index = ~0;
387   gtpu4_tunnel_key_t key4;
388   gtpu6_tunnel_key_t key6;
389   u32 is_ip6 = a->is_ip6;
390
391   if (!is_ip6)
392     {
393       key4.src = a->dst.ip4.as_u32;     /* decap src in key is encap dst in config */
394       key4.teid = clib_host_to_net_u32 (a->teid);
395       p = hash_get (gtm->gtpu4_tunnel_by_key, key4.as_u64);
396     }
397   else
398     {
399       key6.src = a->dst.ip6;
400       key6.teid = clib_host_to_net_u32 (a->teid);
401       p = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6);
402     }
403
404   if (a->is_add)
405     {
406       l2input_main_t *l2im = &l2input_main;
407
408       /* adding a tunnel: tunnel must not already exist */
409       if (p)
410         return VNET_API_ERROR_TUNNEL_EXIST;
411
412       /*if not set explicitly, default to l2 */
413       if (a->decap_next_index == ~0)
414         a->decap_next_index = GTPU_INPUT_NEXT_L2_INPUT;
415       if (!gtpu_decap_next_is_valid (gtm, is_ip6, a->decap_next_index))
416         return VNET_API_ERROR_INVALID_DECAP_NEXT;
417
418       pool_get_aligned (gtm->tunnels, t, CLIB_CACHE_LINE_BYTES);
419       memset (t, 0, sizeof (*t));
420
421       /* copy from arg structure */
422 #define _(x) t->x = a->x;
423       foreach_copy_field;
424 #undef _
425
426       ip_udp_gtpu_rewrite (t, is_ip6);
427
428       /* copy the key */
429       if (is_ip6)
430         hash_set_key_copy (&gtm->gtpu6_tunnel_by_key, &key6,
431                            t - gtm->tunnels);
432       else
433         hash_set (gtm->gtpu4_tunnel_by_key, key4.as_u64, t - gtm->tunnels);
434
435       vnet_hw_interface_t *hi;
436       if (vec_len (gtm->free_gtpu_tunnel_hw_if_indices) > 0)
437         {
438           vnet_interface_main_t *im = &vnm->interface_main;
439           hw_if_index = gtm->free_gtpu_tunnel_hw_if_indices
440             [vec_len (gtm->free_gtpu_tunnel_hw_if_indices) - 1];
441           _vec_len (gtm->free_gtpu_tunnel_hw_if_indices) -= 1;
442
443           hi = vnet_get_hw_interface (vnm, hw_if_index);
444           hi->dev_instance = t - gtm->tunnels;
445           hi->hw_instance = hi->dev_instance;
446
447           /* clear old stats of freed tunnel before reuse */
448           sw_if_index = hi->sw_if_index;
449           vnet_interface_counter_lock (im);
450           vlib_zero_combined_counter
451             (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
452              sw_if_index);
453           vlib_zero_combined_counter (&im->combined_sw_if_counters
454                                       [VNET_INTERFACE_COUNTER_RX],
455                                       sw_if_index);
456           vlib_zero_simple_counter (&im->sw_if_counters
457                                     [VNET_INTERFACE_COUNTER_DROP],
458                                     sw_if_index);
459           vnet_interface_counter_unlock (im);
460         }
461       else
462         {
463           hw_if_index = vnet_register_interface
464             (vnm, gtpu_device_class.index, t - gtm->tunnels,
465              gtpu_hw_class.index, t - gtm->tunnels);
466           hi = vnet_get_hw_interface (vnm, hw_if_index);
467         }
468
469       t->hw_if_index = hw_if_index;
470       t->sw_if_index = sw_if_index = hi->sw_if_index;
471
472       vec_validate_init_empty (gtm->tunnel_index_by_sw_if_index, sw_if_index,
473                                ~0);
474       gtm->tunnel_index_by_sw_if_index[sw_if_index] = t - gtm->tunnels;
475
476       /* setup l2 input config with l2 feature and bd 0 to drop packet */
477       vec_validate (l2im->configs, sw_if_index);
478       l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
479       l2im->configs[sw_if_index].bd_index = 0;
480
481       vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
482       si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
483       vnet_sw_interface_set_flags (vnm, sw_if_index,
484                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
485
486       fib_node_init (&t->node, gtm->fib_node_type);
487       fib_prefix_t tun_dst_pfx;
488       u32 encap_index = !is_ip6 ?
489         gtpu4_encap_node.index : gtpu6_encap_node.index;
490       vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
491
492       fib_prefix_from_ip46_addr (&t->dst, &tun_dst_pfx);
493       if (!ip46_address_is_multicast (&t->dst))
494         {
495           /* Unicast tunnel -
496            * source the FIB entry for the tunnel's destination
497            * and become a child thereof. The tunnel will then get poked
498            * when the forwarding for the entry updates, and the tunnel can
499            * re-stack accordingly
500            */
501           vtep_addr_ref (&t->src);
502           t->fib_entry_index = fib_table_entry_special_add
503             (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR,
504              FIB_ENTRY_FLAG_NONE);
505           t->sibling_index = fib_entry_child_add
506             (t->fib_entry_index, gtm->fib_node_type, t - gtm->tunnels);
507           gtpu_tunnel_restack_dpo (t);
508         }
509       else
510         {
511           /* Multicast tunnel -
512            * as the same mcast group can be used for mutiple mcast tunnels
513            * with different VNIs, create the output fib adjecency only if
514            * it does not already exist
515            */
516           fib_protocol_t fp = fib_ip_proto (is_ip6);
517
518           if (vtep_addr_ref (&t->dst) == 1)
519             {
520               fib_node_index_t mfei;
521               adj_index_t ai;
522               fib_route_path_t path = {
523                 .frp_proto = fp,
524                 .frp_addr = zero_addr,
525                 .frp_sw_if_index = 0xffffffff,
526                 .frp_fib_index = ~0,
527                 .frp_weight = 0,
528                 .frp_flags = FIB_ROUTE_PATH_LOCAL,
529               };
530               const mfib_prefix_t mpfx = {
531                 .fp_proto = fp,
532                 .fp_len = (is_ip6 ? 128 : 32),
533                 .fp_grp_addr = tun_dst_pfx.fp_addr,
534               };
535
536               /*
537                * Setup the (*,G) to receive traffic on the mcast group
538                *  - the forwarding interface is for-us
539                *  - the accepting interface is that from the API
540                */
541               mfib_table_entry_path_update (t->encap_fib_index,
542                                             &mpfx,
543                                             MFIB_SOURCE_GTPU,
544                                             &path, MFIB_ITF_FLAG_FORWARD);
545
546               path.frp_sw_if_index = a->mcast_sw_if_index;
547               path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
548               mfei = mfib_table_entry_path_update (t->encap_fib_index,
549                                                    &mpfx,
550                                                    MFIB_SOURCE_GTPU,
551                                                    &path,
552                                                    MFIB_ITF_FLAG_ACCEPT);
553
554               /*
555                * Create the mcast adjacency to send traffic to the group
556                */
557               ai = adj_mcast_add_or_lock (fp,
558                                           fib_proto_to_link (fp),
559                                           a->mcast_sw_if_index);
560
561               /*
562                * create a new end-point
563                */
564               mcast_shared_add (&t->dst, mfei, ai);
565             }
566
567           dpo_id_t dpo = DPO_INVALID;
568           mcast_shared_t ep = mcast_shared_get (&t->dst);
569
570           /* Stack shared mcast dst mac addr rewrite on encap */
571           dpo_set (&dpo, DPO_ADJACENCY_MCAST,
572                    fib_proto_to_dpo (fp), ep.mcast_adj_index);
573
574           dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
575
576           dpo_reset (&dpo);
577           flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
578         }
579
580       /* Set gtpu tunnel output node */
581       hi->output_node_index = encap_index;
582
583       vnet_get_sw_interface (vnet_get_main (), sw_if_index)->flood_class =
584         flood_class;
585     }
586   else
587     {
588       /* deleting a tunnel: tunnel must exist */
589       if (!p)
590         return VNET_API_ERROR_NO_SUCH_ENTRY;
591
592       t = pool_elt_at_index (gtm->tunnels, p[0]);
593       sw_if_index = t->sw_if_index;
594
595       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */ );
596       vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, t->sw_if_index);
597       si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
598
599       /* make sure tunnel is removed from l2 bd or xconnect */
600       set_int_l2_mode (gtm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0,
601                        0);
602       vec_add1 (gtm->free_gtpu_tunnel_hw_if_indices, t->hw_if_index);
603
604       gtm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
605
606       if (!is_ip6)
607         hash_unset (gtm->gtpu4_tunnel_by_key, key4.as_u64);
608       else
609         hash_unset_key_free (&gtm->gtpu6_tunnel_by_key, &key6);
610
611       if (!ip46_address_is_multicast (&t->dst))
612         {
613           vtep_addr_unref (&t->src);
614           fib_entry_child_remove (t->fib_entry_index, t->sibling_index);
615           fib_table_entry_delete_index (t->fib_entry_index, FIB_SOURCE_RR);
616         }
617       else if (vtep_addr_unref (&t->dst) == 0)
618         {
619           mcast_shared_remove (&t->dst);
620         }
621
622       fib_node_deinit (&t->node);
623       vec_free (t->rewrite);
624       pool_put (gtm->tunnels, t);
625     }
626
627   if (sw_if_indexp)
628     *sw_if_indexp = sw_if_index;
629
630   return 0;
631 }
632
633 static uword
634 get_decap_next_for_node (u32 node_index, u32 ipv4_set)
635 {
636   gtpu_main_t *gtm = &gtpu_main;
637   vlib_main_t *vm = gtm->vlib_main;
638   uword input_node = (ipv4_set) ? gtpu4_input_node.index :
639     gtpu6_input_node.index;
640
641   return vlib_node_add_next (vm, input_node, node_index);
642 }
643
644 static uword
645 unformat_decap_next (unformat_input_t * input, va_list * args)
646 {
647   u32 *result = va_arg (*args, u32 *);
648   u32 ipv4_set = va_arg (*args, int);
649   gtpu_main_t *gtm = &gtpu_main;
650   vlib_main_t *vm = gtm->vlib_main;
651   u32 node_index;
652   u32 tmp;
653
654   if (unformat (input, "l2"))
655     *result = GTPU_INPUT_NEXT_L2_INPUT;
656   else if (unformat (input, "ip4"))
657     *result = GTPU_INPUT_NEXT_IP4_INPUT;
658   else if (unformat (input, "ip6"))
659     *result = GTPU_INPUT_NEXT_IP6_INPUT;
660   else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
661     *result = get_decap_next_for_node (node_index, ipv4_set);
662   else if (unformat (input, "%d", &tmp))
663     *result = tmp;
664   else
665     return 0;
666
667   return 1;
668 }
669
670 static clib_error_t *
671 gtpu_add_del_tunnel_command_fn (vlib_main_t * vm,
672                                 unformat_input_t * input,
673                                 vlib_cli_command_t * cmd)
674 {
675   unformat_input_t _line_input, *line_input = &_line_input;
676   ip46_address_t src, dst;
677   u8 is_add = 1;
678   u8 src_set = 0;
679   u8 dst_set = 0;
680   u8 grp_set = 0;
681   u8 ipv4_set = 0;
682   u8 ipv6_set = 0;
683   u32 encap_fib_index = 0;
684   u32 mcast_sw_if_index = ~0;
685   u32 decap_next_index = GTPU_INPUT_NEXT_L2_INPUT;
686   u32 teid = 0;
687   u32 tmp;
688   int rv;
689   vnet_gtpu_add_del_tunnel_args_t _a, *a = &_a;
690   u32 tunnel_sw_if_index;
691   clib_error_t *error = NULL;
692
693   /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
694   memset (&src, 0, sizeof src);
695   memset (&dst, 0, sizeof dst);
696
697   /* Get a line of input. */
698   if (!unformat_user (input, unformat_line_input, line_input))
699     return 0;
700
701   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
702     {
703       if (unformat (line_input, "del"))
704         {
705           is_add = 0;
706         }
707       else if (unformat (line_input, "src %U",
708                          unformat_ip4_address, &src.ip4))
709         {
710           src_set = 1;
711           ipv4_set = 1;
712         }
713       else if (unformat (line_input, "dst %U",
714                          unformat_ip4_address, &dst.ip4))
715         {
716           dst_set = 1;
717           ipv4_set = 1;
718         }
719       else if (unformat (line_input, "src %U",
720                          unformat_ip6_address, &src.ip6))
721         {
722           src_set = 1;
723           ipv6_set = 1;
724         }
725       else if (unformat (line_input, "dst %U",
726                          unformat_ip6_address, &dst.ip6))
727         {
728           dst_set = 1;
729           ipv6_set = 1;
730         }
731       else if (unformat (line_input, "group %U %U",
732                          unformat_ip4_address, &dst.ip4,
733                          unformat_vnet_sw_interface,
734                          vnet_get_main (), &mcast_sw_if_index))
735         {
736           grp_set = dst_set = 1;
737           ipv4_set = 1;
738         }
739       else if (unformat (line_input, "group %U %U",
740                          unformat_ip6_address, &dst.ip6,
741                          unformat_vnet_sw_interface,
742                          vnet_get_main (), &mcast_sw_if_index))
743         {
744           grp_set = dst_set = 1;
745           ipv6_set = 1;
746         }
747       else if (unformat (line_input, "encap-vrf-id %d", &tmp))
748         {
749           encap_fib_index = fib_table_find (fib_ip_proto (ipv6_set), tmp);
750           if (encap_fib_index == ~0)
751             {
752               error =
753                 clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
754               goto done;
755             }
756         }
757       else if (unformat (line_input, "decap-next %U", unformat_decap_next,
758                          &decap_next_index, ipv4_set))
759         ;
760       else if (unformat (line_input, "teid %d", &teid))
761         ;
762       else
763         {
764           error = clib_error_return (0, "parse error: '%U'",
765                                      format_unformat_error, line_input);
766           goto done;
767         }
768     }
769
770   if (src_set == 0)
771     {
772       error = clib_error_return (0, "tunnel src address not specified");
773       goto done;
774     }
775
776   if (dst_set == 0)
777     {
778       error = clib_error_return (0, "tunnel dst address not specified");
779       goto done;
780     }
781
782   if (grp_set && !ip46_address_is_multicast (&dst))
783     {
784       error = clib_error_return (0, "tunnel group address not multicast");
785       goto done;
786     }
787
788   if (grp_set == 0 && ip46_address_is_multicast (&dst))
789     {
790       error = clib_error_return (0, "dst address must be unicast");
791       goto done;
792     }
793
794   if (grp_set && mcast_sw_if_index == ~0)
795     {
796       error = clib_error_return (0, "tunnel nonexistent multicast device");
797       goto done;
798     }
799
800   if (ipv4_set && ipv6_set)
801     {
802       error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
803       goto done;
804     }
805
806   if (ip46_address_cmp (&src, &dst) == 0)
807     {
808       error = clib_error_return (0, "src and dst addresses are identical");
809       goto done;
810     }
811
812   if (decap_next_index == ~0)
813     {
814       error = clib_error_return (0, "next node not found");
815       goto done;
816     }
817
818   memset (a, 0, sizeof (*a));
819
820   a->is_add = is_add;
821   a->is_ip6 = ipv6_set;
822
823 #define _(x) a->x = x;
824   foreach_copy_field;
825 #undef _
826
827   rv = vnet_gtpu_add_del_tunnel (a, &tunnel_sw_if_index);
828
829   switch (rv)
830     {
831     case 0:
832       if (is_add)
833         vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
834                          vnet_get_main (), tunnel_sw_if_index);
835       break;
836
837     case VNET_API_ERROR_TUNNEL_EXIST:
838       error = clib_error_return (0, "tunnel already exists...");
839       goto done;
840
841     case VNET_API_ERROR_NO_SUCH_ENTRY:
842       error = clib_error_return (0, "tunnel does not exist...");
843       goto done;
844
845     default:
846       error = clib_error_return
847         (0, "vnet_gtpu_add_del_tunnel returned %d", rv);
848       goto done;
849     }
850
851 done:
852   unformat_free (line_input);
853
854   return error;
855 }
856
857 /*?
858  * Add or delete a GTPU Tunnel.
859  *
860  * GTPU provides the features needed to allow L2 bridge domains (BDs)
861  * to span multiple servers. This is done by building an L2 overlay on
862  * top of an L3 network underlay using GTPU tunnels.
863  *
864  * This makes it possible for servers to be co-located in the same data
865  * center or be separated geographically as long as they are reachable
866  * through the underlay L3 network.
867  *
868  * You can refer to this kind of L2 overlay bridge domain as a GTPU
869  * (Virtual eXtensible VLAN) segment.
870  *
871  * @cliexpar
872  * Example of how to create a GTPU Tunnel:
873  * @cliexcmd{create gtpu tunnel src 10.0.3.1 dst 10.0.3.3 teid 13 encap-vrf-id 7}
874  * Example of how to delete a GTPU Tunnel:
875  * @cliexcmd{create gtpu tunnel src 10.0.3.1 dst 10.0.3.3 teid 13 del}
876  ?*/
877 /* *INDENT-OFF* */
878 VLIB_CLI_COMMAND (create_gtpu_tunnel_command, static) = {
879   .path = "create gtpu tunnel",
880   .short_help =
881   "create gtpu tunnel src <local-vtep-addr>"
882   " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} teid <nn>"
883   " [encap-vrf-id <nn>] [decap-next [l2|ip4|ip6|node <name>]] [del]",
884   .function = gtpu_add_del_tunnel_command_fn,
885 };
886 /* *INDENT-ON* */
887
888 static clib_error_t *
889 show_gtpu_tunnel_command_fn (vlib_main_t * vm,
890                              unformat_input_t * input,
891                              vlib_cli_command_t * cmd)
892 {
893   gtpu_main_t *gtm = &gtpu_main;
894   gtpu_tunnel_t *t;
895
896   if (pool_elts (gtm->tunnels) == 0)
897     vlib_cli_output (vm, "No gtpu tunnels configured...");
898
899   pool_foreach (t, gtm->tunnels, (
900                                    {
901                                    vlib_cli_output (vm, "%U",
902                                                     format_gtpu_tunnel, t);
903                                    }
904                 ));
905
906   return 0;
907 }
908
909 /*?
910  * Display all the GTPU Tunnel entries.
911  *
912  * @cliexpar
913  * Example of how to display the GTPU Tunnel entries:
914  * @cliexstart{show gtpu tunnel}
915  * [0] src 10.0.3.1 dst 10.0.3.3 teid 13 encap_fib_index 0 sw_if_index 5 decap_next l2
916  * @cliexend
917  ?*/
918 /* *INDENT-OFF* */
919 VLIB_CLI_COMMAND (show_gtpu_tunnel_command, static) = {
920     .path = "show gtpu tunnel",
921     .short_help = "show gtpu tunnel",
922     .function = show_gtpu_tunnel_command_fn,
923 };
924 /* *INDENT-ON* */
925
926 void
927 vnet_int_gtpu_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable)
928 {
929   if (is_ip6)
930     vnet_feature_enable_disable ("ip6-unicast", "ip6-gtpu-bypass",
931                                  sw_if_index, is_enable, 0, 0);
932   else
933     vnet_feature_enable_disable ("ip4-unicast", "ip4-gtpu-bypass",
934                                  sw_if_index, is_enable, 0, 0);
935 }
936
937 static clib_error_t *
938 set_ip_gtpu_bypass (u32 is_ip6,
939                     unformat_input_t * input, vlib_cli_command_t * cmd)
940 {
941   unformat_input_t _line_input, *line_input = &_line_input;
942   vnet_main_t *vnm = vnet_get_main ();
943   clib_error_t *error = 0;
944   u32 sw_if_index, is_enable;
945
946   sw_if_index = ~0;
947   is_enable = 1;
948
949   if (!unformat_user (input, unformat_line_input, line_input))
950     return 0;
951
952   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
953     {
954       if (unformat_user
955           (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
956         ;
957       else if (unformat (line_input, "del"))
958         is_enable = 0;
959       else
960         {
961           error = unformat_parse_error (line_input);
962           goto done;
963         }
964     }
965
966   if (~0 == sw_if_index)
967     {
968       error = clib_error_return (0, "unknown interface `%U'",
969                                  format_unformat_error, line_input);
970       goto done;
971     }
972
973   vnet_int_gtpu_bypass_mode (sw_if_index, is_ip6, is_enable);
974
975 done:
976   unformat_free (line_input);
977
978   return error;
979 }
980
981 static clib_error_t *
982 set_ip4_gtpu_bypass (vlib_main_t * vm,
983                      unformat_input_t * input, vlib_cli_command_t * cmd)
984 {
985   return set_ip_gtpu_bypass (0, input, cmd);
986 }
987
988 /*?
989  * This command adds the 'ip4-gtpu-bypass' graph node for a given interface.
990  * By adding the IPv4 gtpu-bypass graph node to an interface, the node checks
991  *  for and validate input gtpu packet and bypass ip4-lookup, ip4-local,
992  * ip4-udp-lookup nodes to speedup gtpu packet forwarding. This node will
993  * cause extra overhead to for non-gtpu packets which is kept at a minimum.
994  *
995  * @cliexpar
996  * @parblock
997  * Example of graph node before ip4-gtpu-bypass is enabled:
998  * @cliexstart{show vlib graph ip4-gtpu-bypass}
999  *            Name                      Next                    Previous
1000  * ip4-gtpu-bypass                error-drop [0]
1001  *                                gtpu4-input [1]
1002  *                                 ip4-lookup [2]
1003  * @cliexend
1004  *
1005  * Example of how to enable ip4-gtpu-bypass on an interface:
1006  * @cliexcmd{set interface ip gtpu-bypass GigabitEthernet2/0/0}
1007  *
1008  * Example of graph node after ip4-gtpu-bypass is enabled:
1009  * @cliexstart{show vlib graph ip4-gtpu-bypass}
1010  *            Name                      Next                    Previous
1011  * ip4-gtpu-bypass                error-drop [0]               ip4-input
1012  *                                gtpu4-input [1]        ip4-input-no-checksum
1013  *                                 ip4-lookup [2]
1014  * @cliexend
1015  *
1016  * Example of how to display the feature enabed on an interface:
1017  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1018  * IP feature paths configured on GigabitEthernet2/0/0...
1019  * ...
1020  * ipv4 unicast:
1021  *   ip4-gtpu-bypass
1022  *   ip4-lookup
1023  * ...
1024  * @cliexend
1025  *
1026  * Example of how to disable ip4-gtpu-bypass on an interface:
1027  * @cliexcmd{set interface ip gtpu-bypass GigabitEthernet2/0/0 del}
1028  * @endparblock
1029 ?*/
1030 /* *INDENT-OFF* */
1031 VLIB_CLI_COMMAND (set_interface_ip_gtpu_bypass_command, static) = {
1032   .path = "set interface ip gtpu-bypass",
1033   .function = set_ip4_gtpu_bypass,
1034   .short_help = "set interface ip gtpu-bypass <interface> [del]",
1035 };
1036 /* *INDENT-ON* */
1037
1038 static clib_error_t *
1039 set_ip6_gtpu_bypass (vlib_main_t * vm,
1040                      unformat_input_t * input, vlib_cli_command_t * cmd)
1041 {
1042   return set_ip_gtpu_bypass (1, input, cmd);
1043 }
1044
1045 /*?
1046  * This command adds the 'ip6-gtpu-bypass' graph node for a given interface.
1047  * By adding the IPv6 gtpu-bypass graph node to an interface, the node checks
1048  *  for and validate input gtpu packet and bypass ip6-lookup, ip6-local,
1049  * ip6-udp-lookup nodes to speedup gtpu packet forwarding. This node will
1050  * cause extra overhead to for non-gtpu packets which is kept at a minimum.
1051  *
1052  * @cliexpar
1053  * @parblock
1054  * Example of graph node before ip6-gtpu-bypass is enabled:
1055  * @cliexstart{show vlib graph ip6-gtpu-bypass}
1056  *            Name                      Next                    Previous
1057  * ip6-gtpu-bypass                error-drop [0]
1058  *                                gtpu6-input [1]
1059  *                                 ip6-lookup [2]
1060  * @cliexend
1061  *
1062  * Example of how to enable ip6-gtpu-bypass on an interface:
1063  * @cliexcmd{set interface ip6 gtpu-bypass GigabitEthernet2/0/0}
1064  *
1065  * Example of graph node after ip6-gtpu-bypass is enabled:
1066  * @cliexstart{show vlib graph ip6-gtpu-bypass}
1067  *            Name                      Next                    Previous
1068  * ip6-gtpu-bypass                error-drop [0]               ip6-input
1069  *                                gtpu6-input [1]        ip4-input-no-checksum
1070  *                                 ip6-lookup [2]
1071  * @cliexend
1072  *
1073  * Example of how to display the feature enabed on an interface:
1074  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1075  * IP feature paths configured on GigabitEthernet2/0/0...
1076  * ...
1077  * ipv6 unicast:
1078  *   ip6-gtpu-bypass
1079  *   ip6-lookup
1080  * ...
1081  * @cliexend
1082  *
1083  * Example of how to disable ip6-gtpu-bypass on an interface:
1084  * @cliexcmd{set interface ip6 gtpu-bypass GigabitEthernet2/0/0 del}
1085  * @endparblock
1086 ?*/
1087 /* *INDENT-OFF* */
1088 VLIB_CLI_COMMAND (set_interface_ip6_gtpu_bypass_command, static) = {
1089   .path = "set interface ip6 gtpu-bypass",
1090   .function = set_ip6_gtpu_bypass,
1091   .short_help = "set interface ip gtpu-bypass <interface> [del]",
1092 };
1093 /* *INDENT-ON* */
1094
1095 clib_error_t *
1096 gtpu_init (vlib_main_t * vm)
1097 {
1098   gtpu_main_t *gtm = &gtpu_main;
1099
1100   gtm->vnet_main = vnet_get_main ();
1101   gtm->vlib_main = vm;
1102
1103   /* initialize the ip6 hash */
1104   gtm->gtpu6_tunnel_by_key = hash_create_mem (0,
1105                                               sizeof (gtpu6_tunnel_key_t),
1106                                               sizeof (uword));
1107   gtm->vtep6 = hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
1108   gtm->mcast_shared = hash_create_mem (0,
1109                                        sizeof (ip46_address_t),
1110                                        sizeof (mcast_shared_t));
1111
1112   udp_register_dst_port (vm, UDP_DST_PORT_GTPU,
1113                          gtpu4_input_node.index, /* is_ip4 */ 1);
1114   udp_register_dst_port (vm, UDP_DST_PORT_GTPU6,
1115                          gtpu6_input_node.index, /* is_ip4 */ 0);
1116
1117   gtm->fib_node_type = fib_node_register_new_type (&gtpu_vft);
1118
1119   return 0;
1120 }
1121
1122 VLIB_INIT_FUNCTION (gtpu_init);
1123
1124 /* *INDENT-OFF* */
1125 VLIB_PLUGIN_REGISTER () = {
1126     .version = VPP_BUILD_VER,
1127     .description = "GTPv1-U",
1128 };
1129 /* *INDENT-ON* */
1130
1131 /*
1132  * fd.io coding-style-patch-verification: ON
1133  *
1134  * Local Variables:
1135  * eval: (c-set-style "gnu")
1136  * End:
1137  */