88945cd87621f04da5b8fcd3ac07a1c1b99744e5
[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->nsh_hdr.ver_o_c>>6));
56   if (t->nsh_hdr.ver_o_c & NSH_O_BIT)
57       s = format (s, "O-set ");
58
59   if (t->nsh_hdr.ver_o_c & NSH_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->nsh_hdr.length, t->nsh_hdr.length * 4, t->nsh_hdr.md_type, t->nsh_hdr.next_protocol);
64   
65   s = format (s, "  service path %d service index %d\n",
66               (t->nsh_hdr.spi_si>>NSH_SPI_SHIFT) & NSH_SPI_MASK,
67               t->nsh_hdr.spi_si & NSH_SINDEX_MASK);
68
69   s = format (s, "  c1 %d c2 %d c3 %d c4 %d\n",
70               t->nsh_hdr.c1, t->nsh_hdr.c2, t->nsh_hdr.c3, t->nsh_hdr.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
149
150 #define foreach_copy_nshhdr_field               \
151 _(ver_o_c)                                      \
152 _(length)                                       \
153 _(md_type)                                      \
154 _(next_protocol)                                \
155 _(spi_si)                                       \
156 _(c1)                                           \
157 _(c2)                                           \
158 _(c3)                                           \
159 _(c4)                                           \
160 _(tlvs)
161
162 #define foreach_32bit_field                     \
163 _(spi_si)                                       \
164 _(c1)                                           \
165 _(c2)                                           \
166 _(c3)                                           \
167 _(c4)
168
169 static int nsh_vxlan_gpe_rewrite (nsh_vxlan_gpe_tunnel_t * t)
170 {
171   u8 *rw = 0;
172   ip4_header_t * ip0;
173   nsh_header_t * nsh0;
174   ip4_vxlan_gpe_and_nsh_header_t * h0;
175   int len;
176
177   len = sizeof (*h0) + vec_len(t->nsh_hdr.tlvs)*4;
178
179   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
180
181   h0 = (ip4_vxlan_gpe_and_nsh_header_t *) rw;
182
183   /* Fixed portion of the (outer) ip4 header */
184   ip0 = &h0->ip4;
185   ip0->ip_version_and_header_length = 0x45;
186   ip0->ttl = 254;
187   ip0->protocol = IP_PROTOCOL_UDP;
188
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   /* UDP header, randomize src port on something, maybe? */
195   h0->udp.src_port = clib_host_to_net_u16 (4790);
196   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
197
198   /* VXLAN header. Are we having fun yet? */
199   h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
200   h0->vxlan.ver_res = VXLAN_GPE_VERSION;
201   h0->vxlan.next_protocol = VXLAN_NEXT_PROTOCOL_NSH;
202   h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
203
204   /* NSH header */
205   nsh0 = &h0->nsh;
206   nsh0->ver_o_c = t->nsh_hdr.ver_o_c;
207   nsh0->md_type = t->nsh_hdr.md_type;
208   nsh0->next_protocol = t->nsh_hdr.next_protocol;
209   nsh0->spi_si = t->nsh_hdr.spi_si;
210   nsh0->c1 = t->nsh_hdr.c1;
211   nsh0->c2 = t->nsh_hdr.c2;
212   nsh0->c3 = t->nsh_hdr.c3;
213   nsh0->c4 = t->nsh_hdr.c4;
214   
215   /* Endian swap 32-bit fields */
216 #define _(x) nsh0->x = clib_host_to_net_u32(nsh0->x);
217   foreach_32bit_field;
218 #undef _
219
220   /* fix nsh header length */
221   t->nsh_hdr.length = 6 + vec_len(t->nsh_hdr.tlvs);
222   nsh0->length = t->nsh_hdr.length;
223
224   /* Copy any TLVs */
225   if (vec_len(t->nsh_hdr.tlvs))
226     clib_memcpy (nsh0->tlvs, t->nsh_hdr.tlvs, 4*vec_len(t->nsh_hdr.tlvs));
227
228   t->rewrite = rw;
229   return (0);
230 }
231
232 int vnet_nsh_vxlan_gpe_add_del_tunnel 
233 (vnet_nsh_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
234 {
235   nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
236   nsh_vxlan_gpe_tunnel_t *t = 0;
237   vnet_main_t * vnm = ngm->vnet_main;
238   vnet_hw_interface_t * hi;
239   uword * p;
240   u32 hw_if_index = ~0;
241   u32 sw_if_index = ~0;
242   int rv;
243   nsh_vxlan_gpe_tunnel_key_t key, *key_copy;
244   hash_pair_t *hp;
245   
246   key.src = a->dst.as_u32; /* decap src in key is encap dst in config */
247   key.vni = clib_host_to_net_u32 (a->vni << 8);
248   key.spi_si = clib_host_to_net_u32(a->nsh_hdr.spi_si);
249   key.pad = 0;
250
251   p = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
252   
253   if (a->is_add)
254     {
255       /* adding a tunnel: tunnel must not already exist */
256       if (p) 
257         return VNET_API_ERROR_INVALID_VALUE;
258       
259       if (a->decap_next_index >= NSH_VXLAN_GPE_INPUT_N_NEXT)
260         return VNET_API_ERROR_INVALID_DECAP_NEXT;
261       
262       pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
263       memset (t, 0, sizeof (*t));
264       
265       /* copy from arg structure */
266 #define _(x) t->x = a->x;
267       foreach_copy_field;
268 #undef _
269
270       /* copy from arg structure */
271 #define _(x) t->nsh_hdr.x = a->nsh_hdr.x;
272       foreach_copy_nshhdr_field;
273 #undef _
274       
275       rv = nsh_vxlan_gpe_rewrite (t);
276
277       if (rv)
278         {
279           pool_put (ngm->tunnels, t);
280           return rv;
281         }
282
283       key_copy = clib_mem_alloc (sizeof (*key_copy));
284       clib_memcpy (key_copy, &key, sizeof (*key_copy));
285
286       hash_set_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, key_copy, 
287                     t - ngm->tunnels);
288       
289       if (vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) > 0)
290         {
291           hw_if_index = ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices
292             [vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices)-1];
293           _vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) -= 1;
294           
295           hi = vnet_get_hw_interface (vnm, hw_if_index);
296           hi->dev_instance = t - ngm->tunnels;
297           hi->hw_instance = hi->dev_instance;
298         }
299       else 
300         {
301           hw_if_index = vnet_register_interface
302             (vnm, nsh_vxlan_gpe_device_class.index, t - ngm->tunnels,
303              nsh_vxlan_gpe_hw_class.index, t - ngm->tunnels);
304           hi = vnet_get_hw_interface (vnm, hw_if_index);
305           hi->output_node_index = nsh_vxlan_gpe_encap_node.index;
306         }
307       
308       t->hw_if_index = hw_if_index;
309       t->sw_if_index = sw_if_index = hi->sw_if_index;
310       
311       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 
312                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
313     }
314   else
315     {
316       /* deleting a tunnel: tunnel must exist */
317       if (!p) 
318         return VNET_API_ERROR_NO_SUCH_ENTRY;
319
320       t = pool_elt_at_index (ngm->tunnels, p[0]);
321
322       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
323       vec_add1 (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
324
325       hp = hash_get_pair (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
326       key_copy = (void *)(hp->key);
327       hash_unset_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
328       clib_mem_free (key_copy);
329
330       vec_free (t->rewrite);
331       pool_put (ngm->tunnels, t);
332     }
333
334   if (sw_if_indexp)
335       *sw_if_indexp = sw_if_index;
336
337   return 0;
338 }
339
340 static u32 fib_index_from_fib_id (u32 fib_id)
341 {
342   ip4_main_t * im = &ip4_main;
343   uword * p;
344
345   p = hash_get (im->fib_index_by_table_id, fib_id);
346   if (!p)
347     return ~0;
348
349   return p[0];
350 }
351
352 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
353 {
354   u32 * result = va_arg (*args, u32 *);
355   u32 tmp;
356   
357   if (unformat (input, "drop"))
358     *result = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
359   else if (unformat (input, "ip4"))
360     *result = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
361   else if (unformat (input, "ip6"))
362     *result = NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
363   else if (unformat (input, "ethernet"))
364     *result = NSH_VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
365   else if (unformat (input, "nsh-vxlan-gpe"))
366     *result = NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP;
367   else if (unformat (input, "%d", &tmp))
368     *result = tmp;
369   else
370     return 0;
371   return 1;
372 }
373
374 static clib_error_t *
375 nsh_vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
376                                    unformat_input_t * input,
377                                    vlib_cli_command_t * cmd)
378 {
379   unformat_input_t _line_input, * line_input = &_line_input;
380   ip4_address_t src, dst;
381   u8 is_add = 1;
382   u8 src_set = 0;
383   u8 dst_set = 0;
384   u32 encap_fib_index = 0;
385   u32 decap_fib_index = 0;
386   u8 ver_o_c = 0;
387   u8 length = 0;
388   u8 md_type = 0;
389   u8 next_protocol = 1; /* default: ip4 */
390   u32 decap_next_index = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
391   u32 spi;
392   u8 spi_set = 0;
393   u32 si;
394   u32 vni;
395   u8 vni_set = 0;
396   u8 si_set = 0;
397   u32 spi_si;
398   u32 c1 = 0;
399   u32 c2 = 0;
400   u32 c3 = 0;
401   u32 c4 = 0;
402   u32 *tlvs = 0;
403   u32 tmp;
404   int rv;
405   vnet_nsh_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
406   
407   /* Get a line of input. */
408   if (! unformat_user (input, unformat_line_input, line_input))
409     return 0;
410
411   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
412     if (unformat (line_input, "del"))
413       is_add = 0;
414     else if (unformat (line_input, "src %U", 
415                        unformat_ip4_address, &src))
416       src_set = 1;
417     else if (unformat (line_input, "dst %U",
418                        unformat_ip4_address, &dst))
419       dst_set = 1;
420     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
421       {
422         encap_fib_index = fib_index_from_fib_id (tmp);
423         if (encap_fib_index == ~0)
424           return clib_error_return (0, "nonexistent encap fib id %d", tmp);
425       }
426     else if (unformat (line_input, "decap-vrf-id %d", &tmp))
427       {
428         decap_fib_index = fib_index_from_fib_id (tmp);
429         if (decap_fib_index == ~0)
430           return clib_error_return (0, "nonexistent decap fib id %d", tmp);
431       }
432     else if (unformat (line_input, "decap-next %U", unformat_decap_next, 
433                        &decap_next_index))
434       ;
435     else if (unformat (line_input, "vni %d", &vni))
436       vni_set = 1;
437     else if (unformat (line_input, "version %d", &tmp))
438       ver_o_c |= (tmp & 3) << 6;
439     else if (unformat (line_input, "o-bit %d", &tmp))
440       ver_o_c |= (tmp & 1) << 5;
441     else if (unformat (line_input, "c-bit %d", &tmp))
442       ver_o_c |= (tmp & 1) << 4;
443     else if (unformat (line_input, "md-type %d", &tmp))
444       md_type = tmp;
445     else if (unformat(line_input, "next-ip4"))
446       next_protocol = 1;
447     else if (unformat(line_input, "next-ip6"))
448       next_protocol = 2;
449     else if (unformat(line_input, "next-ethernet"))
450       next_protocol = 3;
451     else if (unformat(line_input, "next-nsh"))
452       next_protocol = 4;
453     else if (unformat (line_input, "c1 %d", &c1))
454       ;
455     else if (unformat (line_input, "c2 %d", &c2))
456       ;
457     else if (unformat (line_input, "c3 %d", &c3))
458       ;
459     else if (unformat (line_input, "c4 %d", &c4))
460       ;
461     else if (unformat (line_input, "spi %d", &spi))
462       spi_set = 1;
463     else if (unformat (line_input, "si %d", &si))
464       si_set = 1;
465     else if (unformat (line_input, "tlv %x"))
466         vec_add1 (tlvs, tmp);
467     else 
468       return clib_error_return (0, "parse error: '%U'", 
469                                 format_unformat_error, line_input);
470   }
471
472   unformat_free (line_input);
473
474   if (src_set == 0)
475     return clib_error_return (0, "tunnel src address not specified");
476
477   if (dst_set == 0)
478     return clib_error_return (0, "tunnel dst address not specified");
479
480   if (vni_set == 0)
481     return clib_error_return (0, "vni not specified");
482
483   if (spi_set == 0)
484     return clib_error_return (0, "spi not specified");
485   
486   if (si_set == 0)
487     return clib_error_return (0, "si not specified");
488
489   spi_si = (spi<<8) | si;
490   
491   memset (a, 0, sizeof (*a));
492
493   a->is_add = is_add;
494
495 #define _(x) a->x = x;
496   foreach_copy_field;
497 #undef _
498
499 #define _(x) a->nsh_hdr.x = x;
500   foreach_copy_nshhdr_field;
501 #undef _
502   
503   rv = vnet_nsh_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
504
505   switch(rv)
506     {
507     case 0:
508       break;
509     case VNET_API_ERROR_INVALID_DECAP_NEXT:
510       return clib_error_return (0, "invalid decap-next...");
511
512     case VNET_API_ERROR_TUNNEL_EXIST:
513       return clib_error_return (0, "tunnel already exists...");
514
515     case VNET_API_ERROR_NO_SUCH_ENTRY:
516       return clib_error_return (0, "tunnel does not exist...");
517
518     default:
519       return clib_error_return 
520         (0, "vnet_nsh_vxlan_gpe_add_del_tunnel returned %d", rv);
521     }
522
523   return 0;
524 }
525
526 VLIB_CLI_COMMAND (create_nsh_vxlan_gpe_tunnel_command, static) = {
527   .path = "nsh vxlan tunnel",
528   .short_help = 
529   "nsh vxlan tunnel src <ip4-addr> dst <ip4-addr>" 
530   "    c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn> vni <nn>\n"
531   "    [encap-vrf-id <nn>] [decap-vrf-id <nn>] [o-bit <1|0>] [c-bit <1|0>]\n"
532   "    [md-type <nn>][next-ip4][next-ip6][next-ethernet][next-nsh]\n"
533   "    [tlv <xx>][decap-next [ip4|ip6|ethernet|nsh-vxlan-gpe]][del]\n",
534   .function = nsh_vxlan_gpe_add_del_tunnel_command_fn,
535 };
536
537 static clib_error_t *
538 show_nsh_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
539                                 unformat_input_t * input,
540                                 vlib_cli_command_t * cmd)
541 {
542   nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
543   nsh_vxlan_gpe_tunnel_t * t;
544   
545   if (pool_elts (ngm->tunnels) == 0)
546     vlib_cli_output (vm, "No nsh-vxlan-gpe tunnels configured...");
547
548   pool_foreach (t, ngm->tunnels,
549   ({
550     vlib_cli_output (vm, "%U", format_nsh_vxlan_gpe_tunnel, t);
551   }));
552   
553   return 0;
554 }
555
556 VLIB_CLI_COMMAND (show_nsh_vxlan_gpe_tunnel_command, static) = {
557     .path = "show nsh vxlan tunnel",
558     .function = show_nsh_vxlan_gpe_tunnel_command_fn,
559 };
560
561 clib_error_t *nsh_vxlan_gpe_init (vlib_main_t *vm)
562 {
563   nsh_vxlan_gpe_main_t *ngm = &nsh_vxlan_gpe_main;
564   
565   ngm->vnet_main = vnet_get_main();
566   ngm->vlib_main = vm;
567   
568   ngm->nsh_vxlan_gpe_tunnel_by_key 
569     = hash_create_mem (0, sizeof(nsh_vxlan_gpe_tunnel_key_t), sizeof (uword));
570
571   udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe, 
572                          nsh_vxlan_gpe_input_node.index, 1 /* is_ip4 */);
573   return 0;
574 }
575
576 VLIB_INIT_FUNCTION(nsh_vxlan_gpe_init);
577