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