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