ONE-11: Fix bugs in LISP API
[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_GRE_INPUT_NEXT_DROP:
32       return format (s, "drop");
33     case NSH_GRE_INPUT_NEXT_IP4_INPUT:
34       return format (s, "ip4");
35     case NSH_GRE_INPUT_NEXT_IP6_INPUT:
36       return format (s, "ip6");
37     case NSH_GRE_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->nsh_hdr.ver_o_c>>6));
61   if (t->nsh_hdr.ver_o_c & NSH_O_BIT)
62       s = format (s, "O-set ");
63
64   if (t->nsh_hdr.ver_o_c & NSH_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->nsh_hdr.length, t->nsh_hdr.length * 4, t->nsh_hdr.md_type, t->nsh_hdr.next_protocol);
69   
70   s = format (s, "  service path %d service index %d\n",
71               (t->nsh_hdr.spi_si>>NSH_SPI_SHIFT) & NSH_SPI_MASK,
72               t->nsh_hdr.spi_si & NSH_SINDEX_MASK);
73
74   s = format (s, "  c1 %d c2 %d c3 %d c4 %d\n",
75               t->nsh_hdr.c1, t->nsh_hdr.c2, t->nsh_hdr.c3, t->nsh_hdr.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
153
154 #define foreach_copy_nshhdr_field               \
155 _(ver_o_c)                                      \
156 _(length)                                       \
157 _(md_type)                                      \
158 _(next_protocol)                                \
159 _(spi_si)                                       \
160 _(c1)                                           \
161 _(c2)                                           \
162 _(c3)                                           \
163 _(c4)                                           \
164 _(tlvs)
165
166 #define foreach_32bit_field                     \
167 _(spi_si)                                       \
168 _(c1)                                           \
169 _(c2)                                           \
170 _(c3)                                           \
171 _(c4)
172
173 static int nsh_gre_rewrite (nsh_gre_tunnel_t * t)
174 {
175   u8 *rw = 0;
176   ip4_header_t * ip0;
177   nsh_header_t * nsh0;
178   ip4_gre_and_nsh_header_t * h0;
179   int len;
180
181   len = sizeof (*h0) + vec_len(t->nsh_hdr.tlvs)*4;
182
183   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
184
185   h0 = (ip4_gre_and_nsh_header_t *) rw;
186
187   /* Fixed portion of the (outer) ip4 header */
188   ip0 = &h0->ip4;
189   ip0->ip_version_and_header_length = 0x45;
190   ip0->ttl = 254;
191   ip0->protocol = IP_PROTOCOL_GRE;
192   /* we fix up the ip4 header length and checksum after-the-fact */
193   ip0->src_address.as_u32 = t->src.as_u32;
194   ip0->dst_address.as_u32 = t->dst.as_u32;
195   ip0->checksum = ip4_header_checksum (ip0);
196
197   /* GRE header, zero execpt for the NSH ethertype */
198   h0->gre.protocol = clib_host_to_net_u16(GRE_PROTOCOL_nsh);
199
200   /* NSH header */
201   nsh0 = &h0->nsh;
202   nsh0->ver_o_c = t->nsh_hdr.ver_o_c;
203   nsh0->md_type = t->nsh_hdr.md_type;
204   nsh0->next_protocol = t->nsh_hdr.next_protocol;
205   nsh0->spi_si = t->nsh_hdr.spi_si;
206   nsh0->c1 = t->nsh_hdr.c1;
207   nsh0->c2 = t->nsh_hdr.c2;
208   nsh0->c3 = t->nsh_hdr.c3;
209   nsh0->c4 = t->nsh_hdr.c4;
210   
211   /* Endian swap 32-bit fields */
212 #define _(x) nsh0->x = clib_host_to_net_u32(nsh0->x);
213   foreach_32bit_field;
214 #undef _
215
216   /* fix nsh header length */
217   t->nsh_hdr.length = 6 + vec_len(t->nsh_hdr.tlvs);
218   nsh0->length = t->nsh_hdr.length;
219
220   /* Copy any TLVs */
221   if (vec_len(t->nsh_hdr.tlvs))
222     clib_memcpy (nsh0->tlvs, t->nsh_hdr.tlvs, 4*vec_len(t->nsh_hdr.tlvs));
223
224   t->rewrite = rw;
225   return (0);
226 }
227
228 int vnet_nsh_gre_add_del_tunnel (vnet_nsh_gre_add_del_tunnel_args_t *a,
229                                  u32 * sw_if_indexp)
230 {
231   nsh_gre_main_t * ngm = &nsh_gre_main;
232   nsh_gre_tunnel_t *t = 0;
233   vnet_main_t * vnm = ngm->vnet_main;
234   vnet_hw_interface_t * hi;
235   uword * p;
236   u32 hw_if_index = ~0;
237   u32 sw_if_index = ~0;
238   int rv;
239   u64 key;
240   u32 spi_si_net_byte_order;
241
242   spi_si_net_byte_order = clib_host_to_net_u32(a->nsh_hdr.spi_si);
243
244   key = (((u64)(a->src.as_u32))<<32) | spi_si_net_byte_order;
245
246   p = hash_get (ngm->nsh_gre_tunnel_by_src_address, 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_GRE_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       /* copy from arg structure */
266 #define _(x) t->nsh_hdr.x = a->nsh_hdr.x;
267       foreach_copy_nshhdr_field;
268 #undef _
269
270       rv = nsh_gre_rewrite (t);
271
272       if (rv)
273         {
274           pool_put (ngm->tunnels, t);
275           return rv;
276         }
277
278       hash_set (ngm->nsh_gre_tunnel_by_src_address, key, t - ngm->tunnels);
279       
280       if (vec_len (ngm->free_nsh_gre_tunnel_hw_if_indices) > 0)
281         {
282           hw_if_index = ngm->free_nsh_gre_tunnel_hw_if_indices
283             [vec_len (ngm->free_nsh_gre_tunnel_hw_if_indices)-1];
284           _vec_len (ngm->free_nsh_gre_tunnel_hw_if_indices) -= 1;
285           
286           hi = vnet_get_hw_interface (vnm, hw_if_index);
287           hi->dev_instance = t - ngm->tunnels;
288           hi->hw_instance = hi->dev_instance;
289         }
290       else 
291         {
292           hw_if_index = vnet_register_interface
293             (vnm, nsh_gre_device_class.index, t - ngm->tunnels,
294              nsh_gre_hw_class.index, t - ngm->tunnels);
295           hi = vnet_get_hw_interface (vnm, hw_if_index);
296           hi->output_node_index = nsh_gre_encap_node.index;
297         }
298       
299       t->hw_if_index = hw_if_index;
300       t->sw_if_index = sw_if_index = hi->sw_if_index;
301       
302       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 
303                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
304     }
305   else
306     {
307       /* deleting a tunnel: tunnel must exist */
308       if (!p) 
309         return VNET_API_ERROR_NO_SUCH_ENTRY;
310
311       t = pool_elt_at_index (ngm->tunnels, p[0]);
312
313       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
314       vec_add1 (ngm->free_nsh_gre_tunnel_hw_if_indices, t->hw_if_index);
315
316       hash_unset (ngm->nsh_gre_tunnel_by_src_address, key);
317       vec_free (t->rewrite);
318       pool_put (ngm->tunnels, t);
319     }
320
321   if (sw_if_indexp)
322       *sw_if_indexp = sw_if_index;
323
324   return 0;
325 }
326
327 static u32 fib_index_from_fib_id (u32 fib_id)
328 {
329   ip4_main_t * im = &ip4_main;
330   uword * p;
331
332   p = hash_get (im->fib_index_by_table_id, fib_id);
333   if (!p)
334     return ~0;
335
336   return p[0];
337 }
338
339 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
340 {
341   u32 * result = va_arg (*args, u32 *);
342   u32 tmp;
343   
344   if (unformat (input, "drop"))
345     *result = NSH_GRE_INPUT_NEXT_DROP;
346   else if (unformat (input, "ip4"))
347     *result = NSH_GRE_INPUT_NEXT_IP4_INPUT;
348   else if (unformat (input, "ip6"))
349     *result = NSH_GRE_INPUT_NEXT_IP6_INPUT;
350   else if (unformat (input, "ethernet"))
351     *result = NSH_GRE_INPUT_NEXT_ETHERNET_INPUT;
352   else if (unformat (input, "%d", &tmp))
353     *result = tmp;
354   else
355     return 0;
356   return 1;
357 }
358
359 static clib_error_t *
360 nsh_gre_add_del_tunnel_command_fn (vlib_main_t * vm,
361                                    unformat_input_t * input,
362                                    vlib_cli_command_t * cmd)
363 {
364   unformat_input_t _line_input, * line_input = &_line_input;
365   ip4_address_t src, dst;
366   u8 is_add = 1;
367   u8 src_set = 0;
368   u8 dst_set = 0;
369   u32 encap_fib_index = 0;
370   u32 decap_fib_index = 0;
371   u8 ver_o_c = 0;
372   u8 length = 0;
373   u8 md_type = 0;
374   u8 next_protocol = 1; /* ip4 */
375   u32 spi;
376   u8 spi_set = 0;
377   u32 si;
378   u8 si_set = 0;
379   u32 spi_si;
380   u32 c1 = 0;
381   u32 c2 = 0;
382   u32 c3 = 0;
383   u32 c4 = 0;
384   u32 decap_next_index = 1; /* ip4_input */
385   u32 *tlvs = 0;
386   u32 tmp;
387   int rv;
388   vnet_nsh_gre_add_del_tunnel_args_t _a, * a = &_a;
389   
390   /* Get a line of input. */
391   if (! unformat_user (input, unformat_line_input, line_input))
392     return 0;
393
394   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
395     if (unformat (line_input, "del"))
396       is_add = 0;
397     else if (unformat (line_input, "src %U", 
398                        unformat_ip4_address, &src))
399       src_set = 1;
400     else if (unformat (line_input, "dst %U",
401                        unformat_ip4_address, &dst))
402       dst_set = 1;
403     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
404       {
405         encap_fib_index = fib_index_from_fib_id (tmp);
406         if (encap_fib_index == ~0)
407           return clib_error_return (0, "nonexistent encap fib id %d", tmp);
408       }
409     else if (unformat (line_input, "decap-vrf-id %d", &tmp))
410       {
411         decap_fib_index = fib_index_from_fib_id (tmp);
412         if (decap_fib_index == ~0)
413           return clib_error_return (0, "nonexistent decap fib id %d", tmp);
414       }
415     else if (unformat (line_input, "decap-next %U", unformat_decap_next, 
416                        &decap_next_index))
417         ;
418     else if (unformat (line_input, "version %d", &tmp))
419       ver_o_c |= (tmp & 3) << 6;
420     else if (unformat (line_input, "o-bit %d", &tmp))
421       ver_o_c |= (tmp & 1) << 5;
422     else if (unformat (line_input, "c-bit %d", &tmp))
423       ver_o_c |= (tmp & 1) << 4;
424     else if (unformat (line_input, "md-type %d", &tmp))
425       md_type = tmp;
426     else if (unformat(line_input, "next-ip4"))
427       next_protocol = 1;
428     else if (unformat(line_input, "next-ip6"))
429       next_protocol = 2;
430     else if (unformat(line_input, "next-ethernet"))
431       next_protocol = 3;
432     else if (unformat (line_input, "c1 %d", &c1))
433       ;
434     else if (unformat (line_input, "c2 %d", &c2))
435       ;
436     else if (unformat (line_input, "c3 %d", &c3))
437       ;
438     else if (unformat (line_input, "c4 %d", &c4))
439       ;
440     else if (unformat (line_input, "spi %d", &spi))
441       spi_set = 1;
442     else if (unformat (line_input, "si %d", &si))
443       si_set = 1;
444     else if (unformat (line_input, "tlv %x"))
445         vec_add1 (tlvs, tmp);
446     else 
447       return clib_error_return (0, "parse error: '%U'", 
448                                 format_unformat_error, line_input);
449   }
450
451   unformat_free (line_input);
452
453   if (src_set == 0)
454     return clib_error_return (0, "tunnel src address not specified");
455
456   if (dst_set == 0)
457     return clib_error_return (0, "tunnel dst address not specified");
458
459   if (spi_set == 0)
460     return clib_error_return (0, "spi not specified");
461   
462   if (si_set == 0)
463     return clib_error_return (0, "si not specified");
464
465   spi_si = (spi<<8) | si;
466   
467   memset (a, 0, sizeof (*a));
468
469   a->is_add = is_add;
470
471 #define _(x) a->x = x;
472   foreach_copy_field;
473 #undef _
474
475       /* copy from arg structure */
476 #define _(x) a->nsh_hdr.x = x;
477       foreach_copy_nshhdr_field;
478 #undef _
479
480   rv = vnet_nsh_gre_add_del_tunnel (a, 0 /* hw_if_indexp */);
481
482   switch(rv)
483     {
484     case 0:
485       break;
486     case VNET_API_ERROR_INVALID_DECAP_NEXT:
487       return clib_error_return (0, "invalid decap-next...");
488
489     case VNET_API_ERROR_TUNNEL_EXIST:
490       return clib_error_return (0, "tunnel already exists...");
491
492     case VNET_API_ERROR_NO_SUCH_ENTRY:
493       return clib_error_return (0, "session does not exist...");
494
495     default:
496       return clib_error_return 
497         (0, "vnet_nsh_gre_add_del_tunnel returned %d", rv);
498     }
499
500   return 0;
501 }
502
503 VLIB_CLI_COMMAND (create_nsh_gre_tunnel_command, static) = {
504   .path = "nsh gre tunnel",
505   .short_help = 
506   "nsh gre tunnel src <ip4-addr> dst <ip4-addr>" 
507   "    c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn>\n"
508   "    [encap-vrf-id <nn>] [decap-vrf-id <nn>] [o-bit <1|0>] [c-bit <1|0>]\n"
509   "    [md-type <nn>][next-ip4][next-ip6][next-ethernet]\n"
510   "    [tlv <xx>][decap-next [ip4|ip6|ethernet]][del]\n",
511   .function = nsh_gre_add_del_tunnel_command_fn,
512 };
513
514 static clib_error_t *
515 show_nsh_gre_tunnel_command_fn (vlib_main_t * vm,
516                                 unformat_input_t * input,
517                                 vlib_cli_command_t * cmd)
518 {
519   nsh_gre_main_t * ngm = &nsh_gre_main;
520   nsh_gre_tunnel_t * t;
521   
522   if (pool_elts (ngm->tunnels) == 0)
523     vlib_cli_output (vm, "No nsh-gre tunnels configured...");
524
525   pool_foreach (t, ngm->tunnels,
526   ({
527     vlib_cli_output (vm, "%U", format_nsh_gre_tunnel, t);
528   }));
529   
530   return 0;
531 }
532
533 VLIB_CLI_COMMAND (show_nsh_gre_tunnel_command, static) = {
534     .path = "show nsh gre tunnel",
535     .function = show_nsh_gre_tunnel_command_fn,
536 };
537
538 clib_error_t *nsh_gre_init (vlib_main_t *vm)
539 {
540   nsh_gre_main_t *ngm = &nsh_gre_main;
541   
542   ngm->vnet_main = vnet_get_main();
543   ngm->vlib_main = vm;
544   
545   ngm->nsh_gre_tunnel_by_src_address = hash_create (0, sizeof (uword));
546   gre_register_input_protocol (vm, GRE_PROTOCOL_nsh, 
547                                nsh_gre_input_node.index);
548   return 0;
549 }
550
551 VLIB_INIT_FUNCTION(nsh_gre_init);
552