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