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