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