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