Initial commit of vpp code.
[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     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
245   p = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
246   
247   if (a->is_add)
248     {
249       /* adding a tunnel: tunnel must not already exist */
250       if (p) 
251         return VNET_API_ERROR_INVALID_VALUE;
252       
253       if (a->decap_next_index >= NSH_VXLAN_GPE_INPUT_N_NEXT)
254         return VNET_API_ERROR_INVALID_DECAP_NEXT;
255       
256       pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
257       memset (t, 0, sizeof (*t));
258       
259       /* copy from arg structure */
260 #define _(x) t->x = a->x;
261       foreach_copy_field;
262 #undef _
263       
264       rv = nsh_vxlan_gpe_rewrite (t);
265
266       if (rv)
267         {
268           pool_put (ngm->tunnels, t);
269           return rv;
270         }
271
272       key_copy = clib_mem_alloc (sizeof (*key_copy));
273       memcpy (key_copy, &key, sizeof (*key_copy));
274
275       hash_set_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, key_copy, 
276                     t - ngm->tunnels);
277       
278       if (vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) > 0)
279         {
280           hw_if_index = ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices
281             [vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices)-1];
282           _vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) -= 1;
283           
284           hi = vnet_get_hw_interface (vnm, hw_if_index);
285           hi->dev_instance = t - ngm->tunnels;
286           hi->hw_instance = hi->dev_instance;
287         }
288       else 
289         {
290           hw_if_index = vnet_register_interface
291             (vnm, nsh_vxlan_gpe_device_class.index, t - ngm->tunnels,
292              nsh_vxlan_gpe_hw_class.index, t - ngm->tunnels);
293           hi = vnet_get_hw_interface (vnm, hw_if_index);
294           hi->output_node_index = nsh_vxlan_gpe_encap_node.index;
295         }
296       
297       t->hw_if_index = hw_if_index;
298       t->sw_if_index = sw_if_index = hi->sw_if_index;
299       
300       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 
301                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
302     }
303   else
304     {
305       /* deleting a tunnel: tunnel must exist */
306       if (!p) 
307         return VNET_API_ERROR_NO_SUCH_ENTRY;
308
309       t = pool_elt_at_index (ngm->tunnels, p[0]);
310
311       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
312       vec_add1 (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
313
314       hp = hash_get_pair (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
315       key_copy = (void *)(hp->key);
316       hash_unset_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
317       clib_mem_free (key_copy);
318
319       vec_free (t->rewrite);
320       pool_put (ngm->tunnels, t);
321     }
322
323   if (sw_if_indexp)
324       *sw_if_indexp = sw_if_index;
325
326   return 0;
327 }
328
329 static u32 fib_index_from_fib_id (u32 fib_id)
330 {
331   ip4_main_t * im = &ip4_main;
332   uword * p;
333
334   p = hash_get (im->fib_index_by_table_id, fib_id);
335   if (!p)
336     return ~0;
337
338   return p[0];
339 }
340
341 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
342 {
343   u32 * result = va_arg (*args, u32 *);
344   u32 tmp;
345   
346   if (unformat (input, "drop"))
347     *result = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
348   else if (unformat (input, "ip4"))
349     *result = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
350   else if (unformat (input, "ip6"))
351     *result = NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
352   else if (unformat (input, "ethernet"))
353     *result = NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
354   else if (unformat (input, "nsh-vxlan-gpe"))
355     *result = NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP;
356   else if (unformat (input, "%d", &tmp))
357     *result = tmp;
358   else
359     return 0;
360   return 1;
361 }
362
363 static clib_error_t *
364 nsh_vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
365                                    unformat_input_t * input,
366                                    vlib_cli_command_t * cmd)
367 {
368   unformat_input_t _line_input, * line_input = &_line_input;
369   ip4_address_t src, dst;
370   u8 is_add = 1;
371   u8 src_set = 0;
372   u8 dst_set = 0;
373   u32 encap_fib_index = 0;
374   u32 decap_fib_index = 0;
375   u8 ver_o_c = 0;
376   u8 length = 0;
377   u8 md_type = 0;
378   u8 next_protocol = 1; /* default: ip4 */
379   u32 decap_next_index = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
380   u32 spi;
381   u8 spi_set = 0;
382   u32 si;
383   u32 vni;
384   u8 vni_set = 0;
385   u8 si_set = 0;
386   u32 spi_si;
387   u32 c1 = 0;
388   u32 c2 = 0;
389   u32 c3 = 0;
390   u32 c4 = 0;
391   u32 *tlvs = 0;
392   u32 tmp;
393   int rv;
394   vnet_nsh_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
395   
396   /* Get a line of input. */
397   if (! unformat_user (input, unformat_line_input, line_input))
398     return 0;
399
400   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
401     if (unformat (line_input, "del"))
402       is_add = 0;
403     else if (unformat (line_input, "src %U", 
404                        unformat_ip4_address, &src))
405       src_set = 1;
406     else if (unformat (line_input, "dst %U",
407                        unformat_ip4_address, &dst))
408       dst_set = 1;
409     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
410       {
411         encap_fib_index = fib_index_from_fib_id (tmp);
412         if (encap_fib_index == ~0)
413           return clib_error_return (0, "nonexistent encap fib id %d", tmp);
414       }
415     else if (unformat (line_input, "decap-vrf-id %d", &tmp))
416       {
417         decap_fib_index = fib_index_from_fib_id (tmp);
418         if (decap_fib_index == ~0)
419           return clib_error_return (0, "nonexistent decap fib id %d", tmp);
420       }
421     else if (unformat (line_input, "decap-next %U", unformat_decap_next, 
422                        &decap_next_index))
423       ;
424     else if (unformat (line_input, "vni %d", &vni))
425       vni_set = 1;
426     else if (unformat (line_input, "version %d", &tmp))
427       ver_o_c |= (tmp & 3) << 6;
428     else if (unformat (line_input, "o-bit %d", &tmp))
429       ver_o_c |= (tmp & 1) << 5;
430     else if (unformat (line_input, "c-bit %d", &tmp))
431       ver_o_c |= (tmp & 1) << 4;
432     else if (unformat (line_input, "md-type %d", &tmp))
433       md_type = tmp;
434     else if (unformat(line_input, "next-ip4"))
435       next_protocol = 1;
436     else if (unformat(line_input, "next-ip6"))
437       next_protocol = 2;
438     else if (unformat(line_input, "next-ethernet"))
439       next_protocol = 3;
440     else if (unformat(line_input, "next-nsh"))
441       next_protocol = 4;
442     else if (unformat (line_input, "c1 %d", &c1))
443       ;
444     else if (unformat (line_input, "c2 %d", &c2))
445       ;
446     else if (unformat (line_input, "c3 %d", &c3))
447       ;
448     else if (unformat (line_input, "c4 %d", &c4))
449       ;
450     else if (unformat (line_input, "spi %d", &spi))
451       spi_set = 1;
452     else if (unformat (line_input, "si %d", &si))
453       si_set = 1;
454     else if (unformat (line_input, "tlv %x"))
455         vec_add1 (tlvs, tmp);
456     else 
457       return clib_error_return (0, "parse error: '%U'", 
458                                 format_unformat_error, line_input);
459   }
460
461   unformat_free (line_input);
462
463   if (src_set == 0)
464     return clib_error_return (0, "tunnel src address not specified");
465
466   if (dst_set == 0)
467     return clib_error_return (0, "tunnel dst address not specified");
468
469   if (vni_set == 0)
470     return clib_error_return (0, "vni not specified");
471
472   if (spi_set == 0)
473     return clib_error_return (0, "spi not specified");
474   
475   if (si_set == 0)
476     return clib_error_return (0, "si not specified");
477
478   spi_si = (spi<<8) | si;
479   
480   memset (a, 0, sizeof (*a));
481
482   a->is_add = is_add;
483
484 #define _(x) a->x = x;
485   foreach_copy_field;
486 #undef _
487   
488   rv = vnet_nsh_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
489
490   switch(rv)
491     {
492     case 0:
493       break;
494     case VNET_API_ERROR_INVALID_DECAP_NEXT:
495       return clib_error_return (0, "invalid decap-next...");
496
497     case VNET_API_ERROR_TUNNEL_EXIST:
498       return clib_error_return (0, "tunnel already exists...");
499
500     case VNET_API_ERROR_NO_SUCH_ENTRY:
501       return clib_error_return (0, "tunnel does not exist...");
502
503     default:
504       return clib_error_return 
505         (0, "vnet_nsh_vxlan_gpe_add_del_tunnel returned %d", rv);
506     }
507
508   return 0;
509 }
510
511 VLIB_CLI_COMMAND (create_nsh_vxlan_gpe_tunnel_command, static) = {
512   .path = "nsh vxlan tunnel",
513   .short_help = 
514   "nsh vxlan tunnel src <ip4-addr> dst <ip4-addr>" 
515   "    c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn> vni <nn>\n"
516   "    [encap-fib-id <nn>] [decap-fib-id <nn>] [o-bit <1|0>] [c-bit <1|0>]\n"
517   "    [md-type <nn>][next-ip4][next-ip6][next-ethernet][next-nsh]\n"
518   "    [tlv <xx>][decap-next [ip4|ip6|ethernet|nsh-encap]][del]\n",
519   .function = nsh_vxlan_gpe_add_del_tunnel_command_fn,
520 };
521
522 static clib_error_t *
523 show_nsh_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
524                                 unformat_input_t * input,
525                                 vlib_cli_command_t * cmd)
526 {
527   nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
528   nsh_vxlan_gpe_tunnel_t * t;
529   
530   if (pool_elts (ngm->tunnels) == 0)
531     vlib_cli_output (vm, "No nsh-vxlan-gpe tunnels configured...");
532
533   pool_foreach (t, ngm->tunnels,
534   ({
535     vlib_cli_output (vm, "%U", format_nsh_vxlan_gpe_tunnel, t);
536   }));
537   
538   return 0;
539 }
540
541 VLIB_CLI_COMMAND (show_nsh_vxlan_gpe_tunnel_command, static) = {
542     .path = "show nsh vxlan tunnel",
543     .function = show_nsh_vxlan_gpe_tunnel_command_fn,
544 };
545
546 clib_error_t *nsh_vxlan_gpe_init (vlib_main_t *vm)
547 {
548   nsh_vxlan_gpe_main_t *ngm = &nsh_vxlan_gpe_main;
549   
550   ngm->vnet_main = vnet_get_main();
551   ngm->vlib_main = vm;
552   
553   ngm->nsh_vxlan_gpe_tunnel_by_key 
554     = hash_create_mem (0, sizeof(nsh_vxlan_gpe_tunnel_key_t), sizeof (uword));
555
556   udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe, 
557                          nsh_vxlan_gpe_input_node.index, 1 /* is_ip4 */);
558   return 0;
559 }
560
561 VLIB_INIT_FUNCTION(nsh_vxlan_gpe_init);
562