d96ba57b1cfb206510d6c4eb8e576b6b0efc4c93
[vpp.git] / vnet / vnet / nsh-vxlan-gpe / nsh_vxlan_gpe.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe.h>
16
17 nsh_vxlan_gpe_main_t nsh_vxlan_gpe_main;
18
19 static u8 * format_decap_next (u8 * s, va_list * args)
20 {
21   u32 next_index = va_arg (*args, u32);
22
23   switch (next_index)
24     {
25     case NSH_VXLAN_GPE_INPUT_NEXT_DROP:
26       return format (s, "drop");
27     case NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT:
28       return format (s, "ip4");
29     case NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT:
30       return format (s, "ip6");
31     case NSH_VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT:
32       return format (s, "ethernet");
33     case NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP:
34       return format (s, "nsh-vxlan-gpe");
35     default:
36       return format (s, "unknown %d", next_index);
37     }
38   return s;
39 }
40
41 u8 * format_nsh_vxlan_gpe_tunnel (u8 * s, va_list * args)
42 {
43   nsh_vxlan_gpe_tunnel_t * t = va_arg (*args, nsh_vxlan_gpe_tunnel_t *);
44   nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
45
46   s = format (s, 
47               "[%d] %U (src) %U (dst) fibs: encap %d, decap %d",
48               t - ngm->tunnels,
49               format_ip4_address, &t->src,
50               format_ip4_address, &t->dst,
51               t->encap_fib_index,
52               t->decap_fib_index);
53   s = format (s, " decap next %U\n", format_decap_next, t->decap_next_index);
54   s = format (s, "  vxlan VNI %d ", t->vni);
55   s = format (s, "nsh ver %d ", (t->ver_o_c>>6));
56   if (t->ver_o_c & NSH_GRE_O_BIT)
57       s = format (s, "O-set ");
58
59   if (t->ver_o_c & NSH_GRE_C_BIT)
60       s = format (s, "C-set ");
61
62   s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
63               t->length, t->length * 4, t->md_type, t->next_protocol);
64   
65   s = format (s, "  service path %d service index %d\n",
66               (t->spi_si>>NSH_GRE_SPI_SHIFT) & NSH_GRE_SPI_MASK,
67               t->spi_si & NSH_GRE_SINDEX_MASK);
68
69   s = format (s, "  c1 %d c2 %d c3 %d c4 %d\n",
70               t->c1, t->c2, t->c3, t->c4);
71
72   return s;
73 }
74
75 static u8 * format_nsh_vxlan_gpe_name (u8 * s, va_list * args)
76 {
77   nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
78   u32 i = va_arg (*args, u32);
79   u32 show_dev_instance = ~0;
80
81   if (i < vec_len (ngm->dev_inst_by_real))
82     show_dev_instance = ngm->dev_inst_by_real[i];
83
84   if (show_dev_instance != ~0)
85     i = show_dev_instance;
86
87   return format (s, "nsh_vxlan_gpe_tunnel%d", i);
88 }
89
90 static int nsh_vxlan_gpe_name_renumber (vnet_hw_interface_t * hi,
91                                         u32 new_dev_instance)
92 {
93   nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
94
95   vec_validate_init_empty (ngm->dev_inst_by_real, hi->dev_instance, ~0);
96
97   ngm->dev_inst_by_real [hi->dev_instance] = new_dev_instance;
98
99   return 0;
100 }
101
102 static uword dummy_interface_tx (vlib_main_t * vm,
103                                  vlib_node_runtime_t * node,
104                                  vlib_frame_t * frame)
105 {
106   clib_warning ("you shouldn't be here, leaking buffers...");
107   return frame->n_vectors;
108 }
109
110 VNET_DEVICE_CLASS (nsh_vxlan_gpe_device_class,static) = {
111   .name = "NSH_VXLAN_GPE",
112   .format_device_name = format_nsh_vxlan_gpe_name,
113   .format_tx_trace = format_nsh_vxlan_gpe_encap_trace,
114   .tx_function = dummy_interface_tx,
115   .name_renumber = nsh_vxlan_gpe_name_renumber,
116 };
117
118 static uword dummy_set_rewrite (vnet_main_t * vnm,
119                                 u32 sw_if_index,
120                                 u32 l3_type,
121                                 void * dst_address,
122                                 void * rewrite,
123                                 uword max_rewrite_bytes)
124 {
125   return 0;
126 }
127
128 static u8 * format_nsh_vxlan_gpe_header_with_length (u8 * s, va_list * args)
129 {
130   u32 dev_instance = va_arg (*args, u32);
131   s = format (s, "unimplemented dev %u", dev_instance);
132   return s;
133 }
134
135 VNET_HW_INTERFACE_CLASS (nsh_vxlan_gpe_hw_class) = {
136   .name = "NSH_VXLAN_GPE",
137   .format_header = format_nsh_vxlan_gpe_header_with_length,
138   .set_rewrite = dummy_set_rewrite,
139 };
140
141 #define foreach_copy_field                      \
142 _(src.as_u32)                                   \
143 _(dst.as_u32)                                   \
144 _(vni)                                          \
145 _(encap_fib_index)                              \
146 _(decap_fib_index)                              \
147 _(decap_next_index)                             \
148 _(ver_o_c)                                      \
149 _(length)                                       \
150 _(md_type)                                      \
151 _(next_protocol)                                \
152 _(spi_si)                                       \
153 _(c1)                                           \
154 _(c2)                                           \
155 _(c3)                                           \
156 _(c4)                                           \
157 _(tlvs)
158
159 #define foreach_32bit_field                     \
160 _(spi_si)                                       \
161 _(c1)                                           \
162 _(c2)                                           \
163 _(c3)                                           \
164 _(c4)
165
166 static int nsh_vxlan_gpe_rewrite (nsh_vxlan_gpe_tunnel_t * t)
167 {
168   u8 *rw = 0;
169   ip4_header_t * ip0;
170   nsh_header_t * nsh0;
171   ip4_vxlan_gpe_and_nsh_header_t * h0;
172   int len;
173
174   len = sizeof (*h0) + vec_len(t->tlvs)*4;
175
176   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
177
178   h0 = (ip4_vxlan_gpe_and_nsh_header_t *) rw;
179
180   /* Fixed portion of the (outer) ip4 header */
181   ip0 = &h0->ip4;
182   ip0->ip_version_and_header_length = 0x45;
183   ip0->ttl = 254;
184   ip0->protocol = IP_PROTOCOL_UDP;
185
186   /* we fix up the ip4 header length and checksum after-the-fact */
187   ip0->src_address.as_u32 = t->src.as_u32;
188   ip0->dst_address.as_u32 = t->dst.as_u32;
189   ip0->checksum = ip4_header_checksum (ip0);
190
191   /* UDP header, randomize src port on something, maybe? */
192   h0->udp.src_port = clib_host_to_net_u16 (4790);
193   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
194
195   /* VXLAN header. Are we having fun yet? */
196   h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
197   h0->vxlan.ver_res = VXLAN_GPE_VERSION;
198   h0->vxlan.next_protocol = VXLAN_NEXT_PROTOCOL_NSH;
199   h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
200
201   /* NSH header */
202   nsh0 = &h0->nsh;
203   nsh0->ver_o_c = t->ver_o_c;
204   nsh0->md_type = t->md_type;
205   nsh0->next_protocol = t->next_protocol;
206   nsh0->spi_si = t->spi_si;
207   nsh0->c1 = t->c1;
208   nsh0->c2 = t->c2;
209   nsh0->c3 = t->c3;
210   nsh0->c4 = t->c4;
211   
212   /* Endian swap 32-bit fields */
213 #define _(x) nsh0->x = clib_host_to_net_u32(nsh0->x);
214   foreach_32bit_field;
215 #undef _
216
217   /* fix nsh header length */
218   t->length = 6 + vec_len(t->tlvs);
219   nsh0->length = t->length;
220
221   /* Copy any TLVs */
222   if (vec_len(t->tlvs))
223     clib_memcpy (nsh0->tlvs, t->tlvs, 4*vec_len(t->tlvs));
224
225   t->rewrite = rw;
226   return (0);
227 }
228
229 int vnet_nsh_vxlan_gpe_add_del_tunnel 
230 (vnet_nsh_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
231 {
232   nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
233   nsh_vxlan_gpe_tunnel_t *t = 0;
234   vnet_main_t * vnm = ngm->vnet_main;
235   vnet_hw_interface_t * hi;
236   uword * p;
237   u32 hw_if_index = ~0;
238   u32 sw_if_index = ~0;
239   int rv;
240   nsh_vxlan_gpe_tunnel_key_t key, *key_copy;
241   hash_pair_t *hp;
242   
243   key.src = a->dst.as_u32; /* decap src in key is encap dst in config */
244   key.vni = clib_host_to_net_u32 (a->vni << 8);
245   key.spi_si = clib_host_to_net_u32(a->spi_si);
246   key.pad = 0;
247
248   p = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
249   
250   if (a->is_add)
251     {
252       /* adding a tunnel: tunnel must not already exist */
253       if (p) 
254         return VNET_API_ERROR_INVALID_VALUE;
255       
256       if (a->decap_next_index >= NSH_VXLAN_GPE_INPUT_N_NEXT)
257         return VNET_API_ERROR_INVALID_DECAP_NEXT;
258       
259       pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
260       memset (t, 0, sizeof (*t));
261       
262       /* copy from arg structure */
263 #define _(x) t->x = a->x;
264       foreach_copy_field;
265 #undef _
266       
267       rv = nsh_vxlan_gpe_rewrite (t);
268
269       if (rv)
270         {
271           pool_put (ngm->tunnels, t);
272           return rv;
273         }
274
275       key_copy = clib_mem_alloc (sizeof (*key_copy));
276       clib_memcpy (key_copy, &key, sizeof (*key_copy));
277
278       hash_set_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, key_copy, 
279                     t - ngm->tunnels);
280       
281       if (vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) > 0)
282         {
283           hw_if_index = ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices
284             [vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices)-1];
285           _vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) -= 1;
286           
287           hi = vnet_get_hw_interface (vnm, hw_if_index);
288           hi->dev_instance = t - ngm->tunnels;
289           hi->hw_instance = hi->dev_instance;
290         }
291       else 
292         {
293           hw_if_index = vnet_register_interface
294             (vnm, nsh_vxlan_gpe_device_class.index, t - ngm->tunnels,
295              nsh_vxlan_gpe_hw_class.index, t - ngm->tunnels);
296           hi = vnet_get_hw_interface (vnm, hw_if_index);
297           hi->output_node_index = nsh_vxlan_gpe_encap_node.index;
298         }
299       
300       t->hw_if_index = hw_if_index;
301       t->sw_if_index = sw_if_index = hi->sw_if_index;
302       
303       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 
304                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
305     }
306   else
307     {
308       /* deleting a tunnel: tunnel must exist */
309       if (!p) 
310         return VNET_API_ERROR_NO_SUCH_ENTRY;
311
312       t = pool_elt_at_index (ngm->tunnels, p[0]);
313
314       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
315       vec_add1 (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
316
317       hp = hash_get_pair (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
318       key_copy = (void *)(hp->key);
319       hash_unset_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
320       clib_mem_free (key_copy);
321
322       vec_free (t->rewrite);
323       pool_put (ngm->tunnels, t);
324     }
325
326   if (sw_if_indexp)
327       *sw_if_indexp = sw_if_index;
328
329   return 0;
330 }
331
332 static u32 fib_index_from_fib_id (u32 fib_id)
333 {
334   ip4_main_t * im = &ip4_main;
335   uword * p;
336
337   p = hash_get (im->fib_index_by_table_id, fib_id);
338   if (!p)
339     return ~0;
340
341   return p[0];
342 }
343
344 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
345 {
346   u32 * result = va_arg (*args, u32 *);
347   u32 tmp;
348   
349   if (unformat (input, "drop"))
350     *result = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
351   else if (unformat (input, "ip4"))
352     *result = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
353   else if (unformat (input, "ip6"))
354     *result = NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
355   else if (unformat (input, "ethernet"))
356     *result = NSH_VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
357   else if (unformat (input, "nsh-vxlan-gpe"))
358     *result = NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP;
359   else if (unformat (input, "%d", &tmp))
360     *result = tmp;
361   else
362     return 0;
363   return 1;
364 }
365
366 static clib_error_t *
367 nsh_vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
368                                    unformat_input_t * input,
369                                    vlib_cli_command_t * cmd)
370 {
371   unformat_input_t _line_input, * line_input = &_line_input;
372   ip4_address_t src, dst;
373   u8 is_add = 1;
374   u8 src_set = 0;
375   u8 dst_set = 0;
376   u32 encap_fib_index = 0;
377   u32 decap_fib_index = 0;
378   u8 ver_o_c = 0;
379   u8 length = 0;
380   u8 md_type = 0;
381   u8 next_protocol = 1; /* default: ip4 */
382   u32 decap_next_index = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
383   u32 spi;
384   u8 spi_set = 0;
385   u32 si;
386   u32 vni;
387   u8 vni_set = 0;
388   u8 si_set = 0;
389   u32 spi_si;
390   u32 c1 = 0;
391   u32 c2 = 0;
392   u32 c3 = 0;
393   u32 c4 = 0;
394   u32 *tlvs = 0;
395   u32 tmp;
396   int rv;
397   vnet_nsh_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
398   
399   /* Get a line of input. */
400   if (! unformat_user (input, unformat_line_input, line_input))
401     return 0;
402
403   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
404     if (unformat (line_input, "del"))
405       is_add = 0;
406     else if (unformat (line_input, "src %U", 
407                        unformat_ip4_address, &src))
408       src_set = 1;
409     else if (unformat (line_input, "dst %U",
410                        unformat_ip4_address, &dst))
411       dst_set = 1;
412     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
413       {
414         encap_fib_index = fib_index_from_fib_id (tmp);
415         if (encap_fib_index == ~0)
416           return clib_error_return (0, "nonexistent encap fib id %d", tmp);
417       }
418     else if (unformat (line_input, "decap-vrf-id %d", &tmp))
419       {
420         decap_fib_index = fib_index_from_fib_id (tmp);
421         if (decap_fib_index == ~0)
422           return clib_error_return (0, "nonexistent decap fib id %d", tmp);
423       }
424     else if (unformat (line_input, "decap-next %U", unformat_decap_next, 
425                        &decap_next_index))
426       ;
427     else if (unformat (line_input, "vni %d", &vni))
428       vni_set = 1;
429     else if (unformat (line_input, "version %d", &tmp))
430       ver_o_c |= (tmp & 3) << 6;
431     else if (unformat (line_input, "o-bit %d", &tmp))
432       ver_o_c |= (tmp & 1) << 5;
433     else if (unformat (line_input, "c-bit %d", &tmp))
434       ver_o_c |= (tmp & 1) << 4;
435     else if (unformat (line_input, "md-type %d", &tmp))
436       md_type = tmp;
437     else if (unformat(line_input, "next-ip4"))
438       next_protocol = 1;
439     else if (unformat(line_input, "next-ip6"))
440       next_protocol = 2;
441     else if (unformat(line_input, "next-ethernet"))
442       next_protocol = 3;
443     else if (unformat(line_input, "next-nsh"))
444       next_protocol = 4;
445     else if (unformat (line_input, "c1 %d", &c1))
446       ;
447     else if (unformat (line_input, "c2 %d", &c2))
448       ;
449     else if (unformat (line_input, "c3 %d", &c3))
450       ;
451     else if (unformat (line_input, "c4 %d", &c4))
452       ;
453     else if (unformat (line_input, "spi %d", &spi))
454       spi_set = 1;
455     else if (unformat (line_input, "si %d", &si))
456       si_set = 1;
457     else if (unformat (line_input, "tlv %x"))
458         vec_add1 (tlvs, tmp);
459     else 
460       return clib_error_return (0, "parse error: '%U'", 
461                                 format_unformat_error, line_input);
462   }
463
464   unformat_free (line_input);
465
466   if (src_set == 0)
467     return clib_error_return (0, "tunnel src address not specified");
468
469   if (dst_set == 0)
470     return clib_error_return (0, "tunnel dst address not specified");
471
472   if (vni_set == 0)
473     return clib_error_return (0, "vni not specified");
474
475   if (spi_set == 0)
476     return clib_error_return (0, "spi not specified");
477   
478   if (si_set == 0)
479     return clib_error_return (0, "si not specified");
480
481   spi_si = (spi<<8) | si;
482   
483   memset (a, 0, sizeof (*a));
484
485   a->is_add = is_add;
486
487 #define _(x) a->x = x;
488   foreach_copy_field;
489 #undef _
490   
491   rv = vnet_nsh_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
492
493   switch(rv)
494     {
495     case 0:
496       break;
497     case VNET_API_ERROR_INVALID_DECAP_NEXT:
498       return clib_error_return (0, "invalid decap-next...");
499
500     case VNET_API_ERROR_TUNNEL_EXIST:
501       return clib_error_return (0, "tunnel already exists...");
502
503     case VNET_API_ERROR_NO_SUCH_ENTRY:
504       return clib_error_return (0, "tunnel does not exist...");
505
506     default:
507       return clib_error_return 
508         (0, "vnet_nsh_vxlan_gpe_add_del_tunnel returned %d", rv);
509     }
510
511   return 0;
512 }
513
514 VLIB_CLI_COMMAND (create_nsh_vxlan_gpe_tunnel_command, static) = {
515   .path = "nsh vxlan tunnel",
516   .short_help = 
517   "nsh vxlan tunnel src <ip4-addr> dst <ip4-addr>" 
518   "    c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn> vni <nn>\n"
519   "    [encap-vrf-id <nn>] [decap-vrf-id <nn>] [o-bit <1|0>] [c-bit <1|0>]\n"
520   "    [md-type <nn>][next-ip4][next-ip6][next-ethernet][next-nsh]\n"
521   "    [tlv <xx>][decap-next [ip4|ip6|ethernet|nsh-vxlan-gpe]][del]\n",
522   .function = nsh_vxlan_gpe_add_del_tunnel_command_fn,
523 };
524
525 static clib_error_t *
526 show_nsh_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
527                                 unformat_input_t * input,
528                                 vlib_cli_command_t * cmd)
529 {
530   nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
531   nsh_vxlan_gpe_tunnel_t * t;
532   
533   if (pool_elts (ngm->tunnels) == 0)
534     vlib_cli_output (vm, "No nsh-vxlan-gpe tunnels configured...");
535
536   pool_foreach (t, ngm->tunnels,
537   ({
538     vlib_cli_output (vm, "%U", format_nsh_vxlan_gpe_tunnel, t);
539   }));
540   
541   return 0;
542 }
543
544 VLIB_CLI_COMMAND (show_nsh_vxlan_gpe_tunnel_command, static) = {
545     .path = "show nsh vxlan tunnel",
546     .function = show_nsh_vxlan_gpe_tunnel_command_fn,
547 };
548
549 clib_error_t *nsh_vxlan_gpe_init (vlib_main_t *vm)
550 {
551   nsh_vxlan_gpe_main_t *ngm = &nsh_vxlan_gpe_main;
552   
553   ngm->vnet_main = vnet_get_main();
554   ngm->vlib_main = vm;
555   
556   ngm->nsh_vxlan_gpe_tunnel_by_key 
557     = hash_create_mem (0, sizeof(nsh_vxlan_gpe_tunnel_key_t), sizeof (uword));
558
559   udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe, 
560                          nsh_vxlan_gpe_input_node.index, 1 /* is_ip4 */);
561   return 0;
562 }
563
564 VLIB_INIT_FUNCTION(nsh_vxlan_gpe_init);
565