VPP43 - NSH refactoring: Added nsh-map nodes
[vpp.git] / vnet / vnet / vxlan-gpe / 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/vxlan-gpe/vxlan_gpe.h>
16
17 vxlan_gpe_main_t vxlan_gpe_main;
18
19 u8 * format_vxlan_gpe_tunnel (u8 * s, va_list * args)
20 {
21   vxlan_gpe_tunnel_t * t = va_arg (*args, vxlan_gpe_tunnel_t *);
22   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
23
24   s = format (s, "[%d] local: %U remote: %U ",
25               t - gm->tunnels,
26               format_ip4_address, &t->local,
27               format_ip4_address, &t->remote);    
28
29   switch (t->protocol)
30     {
31     case VXLAN_GPE_PROTOCOL_IP4:
32       s = format (s, "next-protocol ip4");
33     case VXLAN_GPE_PROTOCOL_IP6:
34       s = format (s, "next-protocol ip6");
35     case VXLAN_GPE_PROTOCOL_ETHERNET:
36       s = format (s, "next-protocol ethernet");
37     case VXLAN_GPE_PROTOCOL_NSH:
38       s = format (s, "next-protocol nsh");
39     default:
40       s = format (s, "next-protocol unknown %d", t->protocol);
41     }
42
43   s = format (s, " fibs: (encap %d, decap %d)",
44               t->encap_fib_index,
45               t->decap_fib_index);
46
47   s = format (s, "  vxlan VNI %d ", t->vni);
48
49   return s;
50 }
51
52 static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
53 {
54   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
55   u32 i = va_arg (*args, u32);
56   u32 show_dev_instance = ~0;
57
58   if (i < vec_len (gm->dev_inst_by_real))
59     show_dev_instance = gm->dev_inst_by_real[i];
60
61   if (show_dev_instance != ~0)
62     i = show_dev_instance;
63
64   return format (s, "vxlan_gpe_tunnel%d", i);
65 }
66
67 static int vxlan_gpe_name_renumber (vnet_hw_interface_t * hi,
68                                         u32 new_dev_instance)
69 {
70   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
71
72   vec_validate_init_empty (gm->dev_inst_by_real, hi->dev_instance, ~0);
73
74   gm->dev_inst_by_real [hi->dev_instance] = new_dev_instance;
75
76   return 0;
77 }
78
79 static uword dummy_interface_tx (vlib_main_t * vm,
80                                  vlib_node_runtime_t * node,
81                                  vlib_frame_t * frame)
82 {
83   clib_warning ("you shouldn't be here, leaking buffers...");
84   return frame->n_vectors;
85 }
86
87 static uword dummy_set_rewrite (vnet_main_t * vnm,
88                                 u32 sw_if_index,
89                                 u32 l3_type,
90                                 void * dst_address,
91                                 void * rewrite,
92                                 uword max_rewrite_bytes)
93 {
94   return 0;
95 }
96
97 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
98   .name = "VXLAN_GPE",
99   .format_device_name = format_vxlan_gpe_name,
100   .format_tx_trace = format_vxlan_gpe_encap_trace,
101   .tx_function = dummy_interface_tx,
102   .name_renumber = vxlan_gpe_name_renumber,
103 };
104
105 static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
106 {
107   u32 dev_instance = va_arg (*args, u32);
108   s = format (s, "unimplemented dev %u", dev_instance);
109   return s;
110 }
111
112 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
113   .name = "VXLAN_GPE",
114   .format_header = format_vxlan_gpe_header_with_length,
115   .set_rewrite = dummy_set_rewrite,
116 };
117
118
119 #define foreach_gpe_copy_field                  \
120 _(local.as_u32)                                 \
121 _(remote.as_u32)                                \
122 _(vni)                                          \
123 _(protocol)                                \
124 _(encap_fib_index)                              \
125 _(decap_fib_index)                              
126
127 #define foreach_copy_field                      \
128 _(src.as_u32)                                   \
129 _(dst.as_u32)                                   \
130 _(vni)                                          \
131 _(encap_fib_index)                              \
132 _(decap_fib_index)                              \
133 _(decap_next_index)
134
135
136
137 static int vxlan_gpe_rewrite (vxlan_gpe_tunnel_t * t)
138 {
139   u8 *rw = 0;
140   ip4_header_t * ip0;
141   ip4_vxlan_gpe_header_t * h0;
142   int len;
143
144   len = sizeof (*h0);
145
146   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
147
148   h0 = (ip4_vxlan_gpe_header_t *) rw;
149
150   /* Fixed portion of the (outer) ip4 header */
151   ip0 = &h0->ip4;
152   ip0->ip_version_and_header_length = 0x45;
153   ip0->ttl = 254;
154   ip0->protocol = IP_PROTOCOL_UDP;
155
156   /* we fix up the ip4 header length and checksum after-the-fact */
157   ip0->src_address.as_u32 = t->local.as_u32; 
158   ip0->dst_address.as_u32 = t->remote.as_u32; 
159   ip0->checksum = ip4_header_checksum (ip0);
160
161   /* UDP header, randomize src port on something, maybe? */
162   h0->udp.src_port = clib_host_to_net_u16 (4790);
163   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
164
165   /* VXLAN header. Are we having fun yet? */
166   h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
167   h0->vxlan.ver_res = VXLAN_GPE_VERSION;
168   h0->vxlan.protocol = VXLAN_GPE_PROTOCOL_IP4;
169   h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
170
171   t->rewrite = rw;
172   return (0);
173 }
174
175 int vnet_vxlan_gpe_add_del_tunnel 
176 (vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
177 {
178   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
179   vxlan_gpe_tunnel_t *t = 0;
180   vnet_main_t * vnm = gm->vnet_main;
181   vnet_hw_interface_t * hi;
182   uword * p;
183   u32 hw_if_index = ~0;
184   u32 sw_if_index = ~0;
185   int rv;
186   vxlan_gpe_tunnel_key_t key, *key_copy;
187   hash_pair_t *hp;
188   
189   key.local = a->local.as_u32; 
190   key.remote = a->remote.as_u32; 
191   key.vni = clib_host_to_net_u32 (a->vni << 8);
192   key.pad = 0;
193
194   p = hash_get_mem (gm->vxlan_gpe_tunnel_by_key, &key);
195   
196   if (a->is_add)
197     {
198       /* adding a tunnel: tunnel must not already exist */
199       if (p) 
200         return VNET_API_ERROR_INVALID_VALUE;
201       
202       if (a->decap_next_index >= VXLAN_GPE_INPUT_N_NEXT)
203         return VNET_API_ERROR_INVALID_DECAP_NEXT;
204       
205       pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
206       memset (t, 0, sizeof (*t));
207       
208       /* copy from arg structure */
209 #define _(x) t->x = a->x;
210       foreach_gpe_copy_field;
211 #undef _
212
213       rv = vxlan_gpe_rewrite (t);
214
215       if (rv)
216         {
217           pool_put (gm->tunnels, t);
218           return rv;
219         }
220
221       key_copy = clib_mem_alloc (sizeof (*key_copy));
222       clib_memcpy (key_copy, &key, sizeof (*key_copy));
223
224       hash_set_mem (gm->vxlan_gpe_tunnel_by_key, key_copy, 
225                     t - gm->tunnels);
226       
227       if (vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
228         {
229           hw_if_index = gm->free_vxlan_gpe_tunnel_hw_if_indices
230             [vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
231           _vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
232           
233           hi = vnet_get_hw_interface (vnm, hw_if_index);
234           hi->dev_instance = t - gm->tunnels;
235           hi->hw_instance = hi->dev_instance;
236         }
237       else 
238         {
239           hw_if_index = vnet_register_interface
240             (vnm, vxlan_gpe_device_class.index, t - gm->tunnels,
241              vxlan_gpe_hw_class.index, t - gm->tunnels);
242           hi = vnet_get_hw_interface (vnm, hw_if_index);
243           hi->output_node_index = vxlan_gpe_encap_node.index;
244         }
245       
246       t->hw_if_index = hw_if_index;
247       t->sw_if_index = sw_if_index = hi->sw_if_index;
248       
249       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 
250                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
251     }
252   else
253     {
254       /* deleting a tunnel: tunnel must exist */
255       if (!p) 
256         return VNET_API_ERROR_NO_SUCH_ENTRY;
257
258       t = pool_elt_at_index (gm->tunnels, p[0]);
259
260       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
261       vec_add1 (gm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
262
263       hp = hash_get_pair (gm->vxlan_gpe_tunnel_by_key, &key);
264       key_copy = (void *)(hp->key);
265       hash_unset_mem (gm->vxlan_gpe_tunnel_by_key, &key);
266       clib_mem_free (key_copy);
267
268       vec_free (t->rewrite);
269       pool_put (gm->tunnels, t);
270     }
271
272   if (sw_if_indexp)
273       *sw_if_indexp = sw_if_index;
274
275   return 0;
276 }
277
278 static u32 fib_index_from_fib_id (u32 fib_id)
279 {
280   ip4_main_t * im = &ip4_main;
281   uword * p;
282
283   p = hash_get (im->fib_index_by_table_id, fib_id);
284   if (!p)
285     return ~0;
286
287   return p[0];
288 }
289
290 static uword unformat_gpe_decap_next (unformat_input_t * input, va_list * args)
291 {
292   u32 * result = va_arg (*args, u32 *);
293   u32 tmp;
294   
295   if (unformat (input, "drop"))
296     *result = VXLAN_GPE_INPUT_NEXT_DROP;
297   else if (unformat (input, "ip4"))
298     *result = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
299   else if (unformat (input, "ip6"))
300     *result = VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
301   else if (unformat (input, "ethernet"))
302     *result = VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
303   else if (unformat (input, "nsh"))
304     *result = VXLAN_GPE_INPUT_NEXT_NSH_INPUT;
305   else if (unformat (input, "%d", &tmp))
306     *result = tmp;
307   else
308     return 0;
309   return 1;
310 }
311
312 static clib_error_t *
313 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
314                                    unformat_input_t * input,
315                                    vlib_cli_command_t * cmd)
316 {
317   unformat_input_t _line_input, * line_input = &_line_input;
318   u8 is_add = 1;
319   ip4_address_t local, remote;
320   u8 local_set = 0;
321   u8 remote_set = 0;
322   u32 encap_fib_index = 0;
323   u32 decap_fib_index = 0;
324   u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
325   u32 decap_next_index = VXLAN_GPE_INPUT_NEXT_IP4_INPUT; 
326   u32 vni;
327   u8 vni_set = 0;
328   int rv;
329   u32 tmp;
330   vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
331   
332   /* Get a line of input. */
333   if (! unformat_user (input, unformat_line_input, line_input))
334     return 0;
335
336   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
337     if (unformat (line_input, "del"))
338       is_add = 0;
339     else if (unformat (line_input, "local %U", 
340                        unformat_ip4_address, &local))
341       local_set = 1;
342     else if (unformat (line_input, "remote %U",
343                        unformat_ip4_address, &remote))
344       remote_set = 1;
345     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
346       {
347         encap_fib_index = fib_index_from_fib_id (tmp);
348         if (encap_fib_index == ~0)
349           return clib_error_return (0, "nonexistent encap fib id %d", tmp);
350       }
351     else if (unformat (line_input, "decap-vrf-id %d", &tmp))
352       {
353         decap_fib_index = fib_index_from_fib_id (tmp);
354         if (decap_fib_index == ~0)
355           return clib_error_return (0, "nonexistent decap fib id %d", tmp);
356       }
357     else if (unformat (line_input, "decap-next %U", unformat_gpe_decap_next, 
358                        &decap_next_index))
359       ;
360     else if (unformat (line_input, "vni %d", &vni))
361       vni_set = 1;
362     else if (unformat(line_input, "next-ip4"))
363       protocol = VXLAN_GPE_PROTOCOL_IP4;
364     else if (unformat(line_input, "next-ip6"))
365       protocol = VXLAN_GPE_PROTOCOL_IP6;
366     else if (unformat(line_input, "next-ethernet"))
367       protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
368     else if (unformat(line_input, "next-nsh"))
369       protocol = VXLAN_GPE_PROTOCOL_NSH;
370     else 
371       return clib_error_return (0, "parse error: '%U'", 
372                                 format_unformat_error, line_input);
373   }
374
375   unformat_free (line_input);
376
377   if (local_set == 0)
378     return clib_error_return (0, "tunnel local address not specified");
379
380   if (remote_set == 0)
381     return clib_error_return (0, "tunnel remote address not specified");
382
383   if (vni_set == 0)
384     return clib_error_return (0, "vni not specified");
385
386   memset (a, 0, sizeof (*a));
387
388   a->is_add = is_add;
389
390 #define _(x) a->x = x;
391   foreach_gpe_copy_field;
392 #undef _
393
394   rv = vnet_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
395
396   switch(rv)
397     {
398     case 0:
399       break;
400     case VNET_API_ERROR_INVALID_DECAP_NEXT:
401       return clib_error_return (0, "invalid decap-next...");
402
403     case VNET_API_ERROR_TUNNEL_EXIST:
404       return clib_error_return (0, "tunnel already exists...");
405
406     case VNET_API_ERROR_NO_SUCH_ENTRY:
407       return clib_error_return (0, "tunnel does not exist...");
408
409     default:
410       return clib_error_return 
411         (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
412     }
413
414   return 0;
415 }
416
417 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
418   .path = "create vxlan-gpe tunnel",
419   .short_help = 
420   "create vxlan-gpe tunnel local <ip4-addr> remote <ip4-addr>"
421   " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
422   " [encap-vrf-id <nn>] [decap-vrf-id <nn>]"
423   " [del]\n",
424   .function = vxlan_gpe_add_del_tunnel_command_fn,
425 };
426
427 static clib_error_t *
428 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
429                                 unformat_input_t * input,
430                                 vlib_cli_command_t * cmd)
431 {
432   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
433   vxlan_gpe_tunnel_t * t;
434   
435   if (pool_elts (gm->tunnels) == 0)
436     vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
437
438   pool_foreach (t, gm->tunnels,
439   ({
440     vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
441   }));
442   
443   return 0;
444 }
445
446 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
447     .path = "show vxlan-gpe",
448     .function = show_vxlan_gpe_tunnel_command_fn,
449 };
450
451 clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
452 {
453   vxlan_gpe_main_t *gm = &vxlan_gpe_main;
454   
455   gm->vnet_main = vnet_get_main();
456   gm->vlib_main = vm;
457   
458   gm->vxlan_gpe_tunnel_by_key 
459     = hash_create_mem (0, sizeof(vxlan_gpe_tunnel_key_t), sizeof (uword));
460
461   udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe, 
462                          vxlan_gpe_input_node.index, 1 /* is_ip4 */);
463   return 0;
464 }
465
466 VLIB_INIT_FUNCTION(vxlan_gpe_init);
467